The big committ:
[libfirm] / ir / be / bestat.c
1 /**
2  * This file calls the corresponding statistic functions for
3  * some backend statistics.
4  * @author Christian Wuerdig
5  * $Id$
6  */
7 #ifdef HAVE_CONFIG_H
8 #include "config.h"
9 #endif
10
11 #include <time.h>
12
13 #include "irnode_t.h"
14 #include "irprintf.h"
15 #include "irgwalk.h"
16 #include "irhooks.h"
17 #include "dbginfo_t.h"
18 #include "firmstat_t.h"
19 #include "irtools.h"
20 #include "pset.h"
21
22 #include "bestat.h"
23 #include "belive_t.h"
24 #include "besched.h"
25 #include "benode_t.h"
26
27 #ifdef FIRM_STATISTICS
28
29 typedef struct _be_stat_irg_t {
30         ir_graph         *irg;       /**< the irg, the statistic is about */
31         pset             *phases;    /**< node statistics for each phase  */
32         struct obstack   obst;       /**< the obstack containing the information */
33         const arch_env_t *arch_env;  /**< the current arch env */
34 } be_stat_irg_t;
35
36 typedef struct _be_stat_phase_t {
37         const arch_env_t *arch_env;  /**< the current arch env */
38         const char       *phase;     /**< the name of the phase the statistic is about */
39         unsigned long    num_nodes;  /**< overall number of reachable nodes in the irg */
40         unsigned long    num_data;   /**< number of data nodes ((mode_datab && ! Proj && ! Phi)  || mode_T) */
41         unsigned long    num_proj;   /**< number of Projs */
42         unsigned long    num_phi;    /**< number of Phis */
43         unsigned long    num_load;   /**< number of Loads */
44         unsigned long    num_store;  /**< number of Stores */
45         unsigned long    num_spill;  /**< number of Spills */
46         unsigned long    num_reload; /**< number of Reloads */
47 } be_stat_phase_t;
48
49 static set *be_stat_data = NULL;
50
51 static int cmp_stat_phase(const void *a, const void *b) {
52         const be_stat_phase_t *p1 = a;
53         const be_stat_phase_t *p2 = b;
54
55         return p1->phase != p2->phase;
56 }
57
58 static int cmp_stat_data(const void *a, const void *b, size_t len) {
59         const be_stat_irg_t *p1 = a;
60         const be_stat_irg_t *p2 = b;
61
62         return p1->irg != p2->irg;
63 }
64
65 static be_stat_irg_t *find_stat_irg_entry(ir_graph *irg) {
66         be_stat_irg_t *entry, key;
67
68         if (! be_stat_data)
69                 return NULL;
70
71         key.irg = irg;
72         entry   = set_find(be_stat_data, &key, sizeof(key), HASH_PTR(irg));
73
74         return entry;
75 }
76
77 static be_stat_irg_t *get_stat_irg_entry(ir_graph *irg) {
78         be_stat_irg_t *entry, key;
79
80         if (! be_stat_data)
81                 return NULL;
82
83         entry = find_stat_irg_entry(irg);
84
85         if (! entry) {
86                 key.irg = irg;
87                 entry   = set_insert(be_stat_data, &key, sizeof(key), HASH_PTR(irg));
88         }
89
90         return entry;
91 }
92
93 struct a_pressure_walker {
94         be_irg_t *birg;
95         be_lv_t *lv;
96 };
97
98 /**
99  * Collect reg pressure statistics per block and per class.
100  */
101 static void stat_reg_pressure_block(ir_node *block, void *data) {
102         struct a_pressure_walker *env = data;
103         be_irg_t         *birg = env->birg;
104         const arch_env_t *aenv = birg->main_env->arch_env;
105         int i, n = arch_isa_get_n_reg_class(aenv->isa);
106
107         for (i = 0; i < n; i++) {
108                 const arch_register_class_t *cls = arch_isa_get_reg_class(aenv->isa, i);
109                 ir_node  *irn;
110                 pset     *live_nodes = pset_new_ptr(64);
111                 int       max_live;
112
113                 live_nodes = be_liveness_end_of_block(env->lv, aenv, cls, block, live_nodes);
114                 max_live   = pset_count(live_nodes);
115
116                 sched_foreach_reverse(block, irn) {
117                         int cnt;
118
119                         if(is_Phi(irn))
120                                 break;
121
122                         live_nodes = be_liveness_transfer(aenv, cls, irn, live_nodes);
123                         cnt        = pset_count(live_nodes);
124                         max_live   = cnt < max_live ? max_live : cnt;
125                 }
126
127                 stat_be_block_regpressure(birg->irg, block, max_live, cls->name);
128         }
129 }
130
131 void be_do_stat_reg_pressure(be_irg_t *birg) {
132         if (stat_is_active()) {
133                 struct a_pressure_walker w;
134
135                 w.birg = birg;
136                 w.lv   = be_liveness(birg->irg);
137                 /* Collect register pressure information for each block */
138                 irg_block_walk_graph(birg->irg, stat_reg_pressure_block, NULL, &w);
139                 be_liveness_free(w.lv);
140         }
141 }
142
143 /**
144  * Notify statistic module about amount of ready nodes.
145  */
146 void be_do_stat_sched_ready(ir_node *block, nodeset *ready_set) {
147         if (stat_is_active()) {
148                 stat_be_block_sched_ready(get_irn_irg(block), block, MIN(nodeset_count(ready_set), 5));
149         }
150 }
151
152 /**
153  * Pass information about a perm to the statistic module.
154  */
155 void be_do_stat_perm(const char *class_name, int n_regs, ir_node *perm, ir_node *block, int n, int real_size) {
156         if (stat_is_active()) {
157                 stat_be_block_stat_perm(class_name, n_regs, perm, block, n, real_size);
158         }
159 }
160
161 /**
162  * Pass information about a cycle or chain in a perm to the statistic module.
163  */
164 void be_do_stat_permcycle(const char *class_name, ir_node *perm, ir_node *block, int is_chain, int n_elems, int n_ops) {
165         if (stat_is_active()) {
166                 stat_be_block_stat_permcycle(class_name, perm, block, is_chain, n_elems, n_ops);
167         }
168 }
169
170 /**
171  * Updates nodes statistics.
172  */
173 static void do_nodes_stat(ir_node *irn, void *env) {
174         be_stat_phase_t  *phase = env;
175         ir_mode          *mode;
176         ir_opcode        opc;
177         arch_irn_class_t irn_class;
178
179         if (is_Block(irn))
180                 return;
181
182         mode = get_irn_mode(irn);
183         opc  = get_irn_opcode(irn);
184
185         phase->num_nodes++;
186
187         /* check for nodes we want to ignore */
188         if (be_is_Keep(irn)     ||
189                 be_is_CopyKeep(irn) ||
190                 opc == iro_Start    ||
191                 opc == iro_End)
192                 return;
193
194         if (is_Proj(irn) && (mode != mode_X)) {
195                 phase->num_proj++;
196                 return;
197         }
198         else if (is_Phi(irn)) {
199                 phase->num_phi++;
200                 return;
201         }
202         else if (mode_is_datab(mode) || ((mode == mode_T) && ! is_be_node(irn)) || (is_Proj(irn) && (mode == mode_X)))
203                 phase->num_data++;
204
205         if (opc == iro_Load)
206                 phase->num_load++;
207         else if (opc == iro_Store)
208                 phase->num_store++;
209
210         irn_class = arch_irn_classify(phase->arch_env, irn);
211         if (irn_class & arch_irn_class_spill)
212                 phase->num_spill++;
213         else if (irn_class & arch_irn_class_reload)
214                 phase->num_reload++;
215         else if (irn_class & arch_irn_class_stackparam)
216                 phase->num_load++;
217         else if (irn_class & arch_irn_class_load)
218                 phase->num_load++;
219         else if (irn_class & arch_irn_class_store)
220                 phase->num_store++;
221 }
222
223 /**
224  * Collects node statistics.
225  *
226  * @param irg      the to do statistics for
227  * @param phase    the phase to collect the statistic for
228  */
229 void be_do_stat_nodes(ir_graph *irg, const char *phase) {
230         be_stat_irg_t   *irg_entry;
231         be_stat_phase_t *phase_entry, phase_key;
232
233         irg_entry = find_stat_irg_entry(irg);
234
235         if (! irg_entry)
236                 return;
237
238         phase_key.phase = phase;
239         phase_entry     = pset_find_ptr(irg_entry->phases, &phase_key);
240
241         if (! phase_entry) {
242                 phase_entry = obstack_alloc(&irg_entry->obst, sizeof(*phase_entry));
243                 phase_entry = pset_insert(irg_entry->phases, phase_entry, HASH_PTR(phase));
244         }
245         memset(phase_entry, 0, sizeof(*phase_entry));
246
247         phase_entry->phase    = phase;
248         phase_entry->arch_env = irg_entry->arch_env;
249
250         irg_walk_blkwise_graph(irg_entry->irg, NULL, do_nodes_stat, phase_entry);
251 }
252
253 /**
254  * Dumps statistics about nodes (called from dump_snapshot)
255  */
256 static void be_dump_node_stat(dumper_t *dmp, graph_entry_t *entry) {
257         be_stat_irg_t   *stat_irg = find_stat_irg_entry(entry->irg);
258         be_stat_phase_t *phase;
259
260         if (! stat_irg || ! stat_irg->phases)
261                 return;
262
263         fprintf(dmp->f, "===> BE NODE STATISTIC BEGIN <===\n");
264
265         foreach_pset(stat_irg->phases, phase) {
266                 fprintf(dmp->f, "--> Phase: %s\n", phase->phase);
267                 fprintf(dmp->f, "# nodes:      %ld\n", phase->num_nodes);
268                 fprintf(dmp->f, "# data nodes: %ld\n", phase->num_data);
269                 fprintf(dmp->f, "# Proj:       %ld\n", phase->num_proj);
270                 fprintf(dmp->f, "# Phi:        %ld\n", phase->num_phi);
271                 fprintf(dmp->f, "# Load:       %ld\n", phase->num_load);
272                 fprintf(dmp->f, "# Store:      %ld\n", phase->num_store);
273                 fprintf(dmp->f, "# Spill:      %ld\n", phase->num_spill);
274                 fprintf(dmp->f, "# Reload:     %ld\n", phase->num_reload);
275         }
276
277         fprintf(dmp->f, "===> BE NODE STATISTIC END <===\n");
278 }
279
280 /**
281  * Returns a be statistic object for the given irg.
282  */
283 void be_stat_init_irg(const arch_env_t *arch_env, ir_graph *irg) {
284         static int reg_func  = 1;
285
286         if (stat_is_active()) {
287                 be_stat_irg_t *stat_irg;
288
289                 if (! be_stat_data)
290                         be_stat_data = new_set(cmp_stat_data, 8);
291
292                 stat_irg           = get_stat_irg_entry(irg);
293                 stat_irg->irg      = irg;
294                 stat_irg->phases   = new_pset(cmp_stat_phase, 8);
295                 stat_irg->arch_env = arch_env;
296                 obstack_init(&stat_irg->obst);
297
298                 if (reg_func) {
299                         /* first init: register dumper */
300                         stat_register_dumper_func(be_dump_node_stat);
301                         reg_func = 0;
302                 }
303         }
304 }
305 #endif /* FIRM_STATISTICS */
306
307 typedef struct _estimate_irg_costs_env_t {
308         const arch_env_t *arch_env;
309         ir_exec_freq     *execfreqs;
310         double           costs;
311 } estimate_irg_costs_env_t;
312
313 static void estimate_block_costs(ir_node *block, void *data)
314 {
315         estimate_irg_costs_env_t *env = data;
316         ir_node *node;
317         double  costs = 0.0;
318
319         sched_foreach(block, node) {
320                 costs += arch_get_op_estimated_cost(env->arch_env, node);
321         }
322
323         env->costs += costs * get_block_execfreq(env->execfreqs, block);
324 }
325
326 double be_estimate_irg_costs(ir_graph *irg, const arch_env_t *arch_env, ir_exec_freq *execfreqs)
327 {
328         estimate_irg_costs_env_t env;
329
330         env.arch_env  = arch_env;
331         env.execfreqs = execfreqs;
332         env.costs     = 0.0;
333
334         irg_block_walk_graph(irg, estimate_block_costs, NULL, &env);
335
336         return env.costs;
337 }
338
339 #ifdef FIRM_STATISTICS
340
341 const char *be_stat_tags[STAT_TAG_LAST];
342 FILE       *be_stat_file = NULL;
343
344 void be_init_stat_file(const char *stat_file_name, const char *sourcefilename)
345 {
346         static char time_str[32];
347
348         assert(be_stat_file == NULL);
349
350         /* if we want to do some statistics, push the environment. */
351         if (strlen(stat_file_name) == 0)
352                 return;
353
354         be_stat_file = fopen(stat_file_name, "at");
355         if (be_stat_file == NULL) {
356                 fprintf(stderr, "Warning couldn't open statfile '%s'\n", stat_file_name);
357                 return;
358         }
359
360         /* initialize the statistics tags */
361         ir_snprintf(time_str, sizeof(time_str),"%u", time(NULL));
362
363         be_stat_tags[STAT_TAG_FILE] = sourcefilename;
364         be_stat_tags[STAT_TAG_TIME] = time_str;
365         be_stat_tags[STAT_TAG_IRG]  = "<all>";
366         be_stat_tags[STAT_TAG_CLS]  = "<all>";
367
368         be_stat_ev_push(be_stat_tags, STAT_TAG_LAST, be_stat_file);
369 }
370
371 void be_close_stat_file()
372 {
373         be_stat_ev_pop();
374         if (be_stat_file != NULL) {
375                 fclose(be_stat_file);
376                 be_stat_file = NULL;
377         }
378 }
379
380 #else /* FIRM_STATISTICS */
381
382 void (be_stat_init_irg)(const arch_env_t *arch_env, ir_graph *irg) {}
383 void (be_do_stat_nodes)(ir_graph *irg, const char *phase) {}
384 void (be_do_stat_reg_pressure)(be_irg_t *birg) {}
385 void (be_do_stat_sched_ready)(ir_node *block, nodeset *ready_set) {}
386 void (be_do_stat_perm)(const char *class_name, int n_regs, ir_node *perm, ir_node *block, int n, int real_size) {}
387
388 #endif /* FIRM_STATISTICS */