- Split bearch.h correctly into bearch.h and bearch_t.h
[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         ir_graph         *irg  = be_get_birg_irg(birg);
105         const arch_env_t *aenv = be_get_birg_arch_env(birg);
106         int i, n = arch_isa_get_n_reg_class(aenv->isa);
107
108         for (i = 0; i < n; i++) {
109                 const arch_register_class_t *cls = arch_isa_get_reg_class(aenv->isa, i);
110                 ir_node  *irn;
111                 pset     *live_nodes = pset_new_ptr(64);
112                 int       max_live;
113
114                 live_nodes = be_liveness_end_of_block(env->lv, aenv, cls, block, live_nodes);
115                 max_live   = pset_count(live_nodes);
116
117                 sched_foreach_reverse(block, irn) {
118                         int cnt;
119
120                         if(is_Phi(irn))
121                                 break;
122
123                         live_nodes = be_liveness_transfer(aenv, cls, irn, live_nodes);
124                         cnt        = pset_count(live_nodes);
125                         max_live   = cnt < max_live ? max_live : cnt;
126                 }
127
128                 stat_be_block_regpressure(irg, block, max_live, cls->name);
129         }
130 }
131
132 void be_do_stat_reg_pressure(be_irg_t *birg) {
133         ir_graph *irg = be_get_birg_irg(birg);
134
135         if (stat_is_active()) {
136                 struct a_pressure_walker w;
137
138                 w.birg = birg;
139                 w.lv   = be_liveness(irg);
140                 /* Collect register pressure information for each block */
141                 irg_block_walk_graph(irg, stat_reg_pressure_block, NULL, &w);
142                 be_liveness_free(w.lv);
143         }
144 }
145
146 /**
147  * Notify statistic module about amount of ready nodes.
148  */
149 void be_do_stat_sched_ready(ir_node *block, const ir_nodeset_t *ready_set) {
150         if (stat_is_active()) {
151                 stat_be_block_sched_ready(get_irn_irg(block), block, MIN(ir_nodeset_size(ready_set), 5));
152         }
153 }
154
155 /**
156  * Pass information about a perm to the statistic module.
157  */
158 void be_do_stat_perm(const char *class_name, int n_regs, ir_node *perm, ir_node *block, int n, int real_size) {
159         if (stat_is_active()) {
160                 stat_be_block_stat_perm(class_name, n_regs, perm, block, n, real_size);
161         }
162 }
163
164 /**
165  * Pass information about a cycle or chain in a perm to the statistic module.
166  */
167 void be_do_stat_permcycle(const char *class_name, ir_node *perm, ir_node *block, int is_chain, int n_elems, int n_ops) {
168         if (stat_is_active()) {
169                 stat_be_block_stat_permcycle(class_name, perm, block, is_chain, n_elems, n_ops);
170         }
171 }
172
173 /**
174  * Updates nodes statistics.
175  */
176 static void do_nodes_stat(ir_node *irn, void *env) {
177         be_stat_phase_t  *phase = env;
178         ir_mode          *mode;
179         ir_opcode        opc;
180         arch_irn_class_t irn_class;
181
182         if (is_Block(irn))
183                 return;
184
185         mode = get_irn_mode(irn);
186         opc  = get_irn_opcode(irn);
187
188         phase->num_nodes++;
189
190         /* check for nodes we want to ignore */
191         if (be_is_Keep(irn)     ||
192                 be_is_CopyKeep(irn) ||
193                 opc == iro_Start    ||
194                 opc == iro_End)
195                 return;
196
197         if (is_Proj(irn) && (mode != mode_X)) {
198                 phase->num_proj++;
199                 return;
200         }
201         else if (is_Phi(irn)) {
202                 phase->num_phi++;
203                 return;
204         }
205         else if (mode_is_datab(mode) || ((mode == mode_T) && ! is_be_node(irn)) || (is_Proj(irn) && (mode == mode_X)))
206                 phase->num_data++;
207
208         if (opc == iro_Load)
209                 phase->num_load++;
210         else if (opc == iro_Store)
211                 phase->num_store++;
212
213         irn_class = arch_irn_classify(phase->arch_env, irn);
214         if (irn_class & arch_irn_class_spill)
215                 phase->num_spill++;
216         else if (irn_class & arch_irn_class_reload)
217                 phase->num_reload++;
218         else if (irn_class & arch_irn_class_stackparam)
219                 phase->num_load++;
220         else if (irn_class & arch_irn_class_load)
221                 phase->num_load++;
222         else if (irn_class & arch_irn_class_store)
223                 phase->num_store++;
224 }
225
226 /**
227  * Collects node statistics.
228  *
229  * @param irg      the to do statistics for
230  * @param phase    the phase to collect the statistic for
231  */
232 void be_do_stat_nodes(ir_graph *irg, const char *phase) {
233         be_stat_irg_t   *irg_entry;
234         be_stat_phase_t *phase_entry, phase_key;
235
236         irg_entry = find_stat_irg_entry(irg);
237
238         if (! irg_entry)
239                 return;
240
241         phase_key.phase = phase;
242         phase_entry     = pset_find_ptr(irg_entry->phases, &phase_key);
243
244         if (! phase_entry) {
245                 phase_entry = obstack_alloc(&irg_entry->obst, sizeof(*phase_entry));
246                 phase_entry = pset_insert(irg_entry->phases, phase_entry, HASH_PTR(phase));
247         }
248         memset(phase_entry, 0, sizeof(*phase_entry));
249
250         phase_entry->phase    = phase;
251         phase_entry->arch_env = irg_entry->arch_env;
252
253         irg_walk_blkwise_graph(irg_entry->irg, NULL, do_nodes_stat, phase_entry);
254 }
255
256 /**
257  * Dumps statistics about nodes (called from dump_snapshot)
258  */
259 static void be_dump_node_stat(dumper_t *dmp, graph_entry_t *entry) {
260         be_stat_irg_t   *stat_irg = find_stat_irg_entry(entry->irg);
261         be_stat_phase_t *phase;
262
263         if (! stat_irg || ! stat_irg->phases)
264                 return;
265
266         fprintf(dmp->f, "===> BE NODE STATISTIC BEGIN <===\n");
267
268         foreach_pset(stat_irg->phases, phase) {
269                 fprintf(dmp->f, "--> Phase: %s\n", phase->phase);
270                 fprintf(dmp->f, "# nodes:      %ld\n", phase->num_nodes);
271                 fprintf(dmp->f, "# data nodes: %ld\n", phase->num_data);
272                 fprintf(dmp->f, "# Proj:       %ld\n", phase->num_proj);
273                 fprintf(dmp->f, "# Phi:        %ld\n", phase->num_phi);
274                 fprintf(dmp->f, "# Load:       %ld\n", phase->num_load);
275                 fprintf(dmp->f, "# Store:      %ld\n", phase->num_store);
276                 fprintf(dmp->f, "# Spill:      %ld\n", phase->num_spill);
277                 fprintf(dmp->f, "# Reload:     %ld\n", phase->num_reload);
278         }
279
280         fprintf(dmp->f, "===> BE NODE STATISTIC END <===\n");
281 }
282
283 /**
284  * Returns a be statistic object for the given irg.
285  */
286 void be_stat_init_irg(const arch_env_t *arch_env, ir_graph *irg) {
287         static int reg_func  = 1;
288
289         if (stat_is_active()) {
290                 be_stat_irg_t *stat_irg;
291
292                 if (! be_stat_data)
293                         be_stat_data = new_set(cmp_stat_data, 8);
294
295                 stat_irg           = get_stat_irg_entry(irg);
296                 stat_irg->irg      = irg;
297                 stat_irg->phases   = new_pset(cmp_stat_phase, 8);
298                 stat_irg->arch_env = arch_env;
299                 obstack_init(&stat_irg->obst);
300
301                 if (reg_func) {
302                         /* first init: register dumper */
303                         stat_register_dumper_func(be_dump_node_stat);
304                         reg_func = 0;
305                 }
306         }
307 }
308 #endif /* FIRM_STATISTICS */
309
310 typedef struct _estimate_irg_costs_env_t {
311         const arch_env_t *arch_env;
312         ir_exec_freq     *execfreqs;
313         double           costs;
314 } estimate_irg_costs_env_t;
315
316 static void estimate_block_costs(ir_node *block, void *data)
317 {
318         estimate_irg_costs_env_t *env = data;
319         ir_node *node;
320         double  costs = 0.0;
321
322         sched_foreach(block, node) {
323                 costs += arch_get_op_estimated_cost(env->arch_env, node);
324         }
325
326         env->costs += costs * get_block_execfreq(env->execfreqs, block);
327 }
328
329 double be_estimate_irg_costs(ir_graph *irg, const arch_env_t *arch_env, ir_exec_freq *execfreqs)
330 {
331         estimate_irg_costs_env_t env;
332
333         env.arch_env  = arch_env;
334         env.execfreqs = execfreqs;
335         env.costs     = 0.0;
336
337         irg_block_walk_graph(irg, estimate_block_costs, NULL, &env);
338
339         return env.costs;
340 }
341
342 #ifdef FIRM_STATISTICS
343
344 const char *be_stat_tags[STAT_TAG_LAST];
345 FILE       *be_stat_file = NULL;
346
347 void be_init_stat_file(const char *stat_file_name, const char *sourcefilename)
348 {
349         static char time_str[32];
350
351         assert(be_stat_file == NULL);
352
353         /* if we want to do some statistics, push the environment. */
354         if (strlen(stat_file_name) == 0)
355                 return;
356
357         be_stat_file = fopen(stat_file_name, "at");
358         if (be_stat_file == NULL) {
359                 fprintf(stderr, "Warning couldn't open statfile '%s'\n", stat_file_name);
360                 return;
361         }
362
363         /* initialize the statistics tags */
364         ir_snprintf(time_str, sizeof(time_str),"%u", time(NULL));
365
366         be_stat_tags[STAT_TAG_FILE] = sourcefilename;
367         be_stat_tags[STAT_TAG_TIME] = time_str;
368         be_stat_tags[STAT_TAG_IRG]  = "<all>";
369         be_stat_tags[STAT_TAG_CLS]  = "<all>";
370
371         be_stat_ev_push(be_stat_tags, STAT_TAG_LAST, be_stat_file);
372 }
373
374 void be_close_stat_file()
375 {
376         be_stat_ev_pop();
377         if (be_stat_file != NULL) {
378                 fclose(be_stat_file);
379                 be_stat_file = NULL;
380         }
381 }
382
383 #else /* FIRM_STATISTICS */
384
385 void (be_stat_init_irg)(const arch_env_t *arch_env, ir_graph *irg) {}
386 void (be_do_stat_nodes)(ir_graph *irg, const char *phase) {}
387 void (be_do_stat_reg_pressure)(be_irg_t *birg) {}
388 void (be_do_stat_sched_ready)(ir_node *block, nodeset *ready_set) {}
389 void (be_do_stat_perm)(const char *class_name, int n_regs, ir_node *perm, ir_node *block, int n, int real_size) {}
390
391 #endif /* FIRM_STATISTICS */