bechordal_draw: Remove the write-only attribute max_color from struct draw_chordal_env_t.
[libfirm] / ir / stat / firmstat.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief   Statistics for Firm.
9  * @author  Michael Beck
10  */
11 #include "config.h"
12
13 #include <stdio.h>
14 #include <stdlib.h>
15 #include <string.h>
16
17 #include "irouts.h"
18 #include "irdump.h"
19 #include "hashptr.h"
20 #include "firmstat_t.h"
21 #include "irpass_t.h"
22 #include "pattern.h"
23 #include "dags.h"
24 #include "stat_dmp.h"
25 #include "xmalloc.h"
26 #include "irhooks.h"
27 #include "util.h"
28 #include "ircons.h"
29 #include "irtools.h"
30
31 #include "lc_opts.h"
32 #include "lc_opts_enum.h"
33
34 /*
35  * need this to be static:
36  * Special pseudo Opcodes that we need to count some interesting cases
37  */
38
39 /**
40  * The Phi0, a node that is created during SSA construction
41  */
42 static ir_op _op_Phi0;
43
44 /** The PhiM, just to count memory Phi's. */
45 static ir_op _op_PhiM;
46
47 /** The Mul by Const node. */
48 static ir_op _op_MulC;
49
50 /** The Div by Const node. */
51 static ir_op _op_DivC;
52
53 /** The Div by Const node. */
54 static ir_op _op_ModC;
55
56 /** The memory Proj node. */
57 static ir_op _op_ProjM;
58
59 /** A Sel of a Sel */
60 static ir_op _op_SelSel;
61
62 /** A Sel of a Sel of a Sel */
63 static ir_op _op_SelSelSel;
64
65 static unsigned stat_options;
66
67 /* ---------------------------------------------------------------------------------- */
68
69 /** Marks the begin of a statistic (hook) function. */
70 #define STAT_ENTER    ++status->recursive
71
72 /** Marks the end of a statistic (hook) functions. */
73 #define STAT_LEAVE    --status->recursive
74
75 /** Allows to enter a statistic function only when we are not already in a hook. */
76 #define STAT_ENTER_SINGLE    do { if (status->recursive > 0) return; ++status->recursive; } while (0)
77
78 /**
79  * global status
80  */
81 static const unsigned status_disable = 0;
82 static stat_info_t *status = (stat_info_t *)&status_disable;
83
84 /**
85  * Compare two elements of the opcode hash.
86  */
87 static int opcode_cmp(const void *elt, const void *key)
88 {
89         const node_entry_t *e1 = (const node_entry_t*)elt;
90         const node_entry_t *e2 = (const node_entry_t*)key;
91
92         return e1->op->code - e2->op->code;
93 }
94
95 /**
96  * Compare two elements of the graph hash.
97  */
98 static int graph_cmp(const void *elt, const void *key)
99 {
100         const graph_entry_t *e1 = (const graph_entry_t*)elt;
101         const graph_entry_t *e2 = (const graph_entry_t*)key;
102
103         return e1->irg != e2->irg;
104 }
105
106 /**
107  * Compare two elements of the optimization hash.
108  */
109 static int opt_cmp(const void *elt, const void *key)
110 {
111         const opt_entry_t *e1 = (const opt_entry_t*)elt;
112         const opt_entry_t *e2 = (const opt_entry_t*)key;
113
114         return e1->op->code != e2->op->code;
115 }
116
117 /**
118  * Compare two elements of the block hash.
119  */
120 static int block_cmp(const void *elt, const void *key)
121 {
122         const block_entry_t *e1 = (const block_entry_t*)elt;
123         const block_entry_t *e2 = (const block_entry_t*)key;
124
125         /* it's enough to compare the block number */
126         return e1->block_nr != e2->block_nr;
127 }
128
129 /**
130  * Compare two elements of the be_block hash.
131  */
132 static int be_block_cmp(const void *elt, const void *key)
133 {
134         const be_block_entry_t *e1 = (const be_block_entry_t*)elt;
135         const be_block_entry_t *e2 = (const be_block_entry_t*)key;
136
137         return e1->block_nr != e2->block_nr;
138 }
139
140 /**
141  * Compare two elements of reg pressure hash.
142  */
143 static int reg_pressure_cmp(const void *elt, const void *key)
144 {
145         const reg_pressure_entry_t *e1 = (const reg_pressure_entry_t*)elt;
146         const reg_pressure_entry_t *e2 = (const reg_pressure_entry_t*)key;
147
148         return e1->class_name != e2->class_name;
149 }
150
151 /**
152  * Compare two elements of the perm_stat hash.
153  */
154 static int perm_stat_cmp(const void *elt, const void *key)
155 {
156         const perm_stat_entry_t *e1 = (const perm_stat_entry_t*)elt;
157         const perm_stat_entry_t *e2 = (const perm_stat_entry_t*)key;
158
159         return e1->perm != e2->perm;
160 }
161
162 /**
163  * Compare two elements of the perm_class hash.
164  */
165 static int perm_class_cmp(const void *elt, const void *key)
166 {
167         const perm_class_entry_t *e1 = (const perm_class_entry_t*)elt;
168         const perm_class_entry_t *e2 = (const perm_class_entry_t*)key;
169
170         return e1->class_name != e2->class_name;
171 }
172
173 /**
174  * Compare two elements of the ir_op hash.
175  */
176 static int opcode_cmp_2(const void *elt, const void *key)
177 {
178         const ir_op *e1 = (const ir_op*)elt;
179         const ir_op *e2 = (const ir_op*)key;
180
181         return e1->code != e2->code;
182 }
183
184 /**
185  * Compare two elements of the address_mark set.
186  */
187 static int address_mark_cmp(const void *elt, const void *key, size_t size)
188 {
189         const address_mark_entry_t *e1 = (const address_mark_entry_t*)elt;
190         const address_mark_entry_t *e2 = (const address_mark_entry_t*)key;
191         (void) size;
192
193         /* compare only the nodes, the rest is used as data container */
194         return e1->node != e2->node;
195 }
196
197 /**
198  * Clear all counter in a node_entry_t.
199  */
200 static void opcode_clear_entry(node_entry_t *elem)
201 {
202         cnt_clr(&elem->cnt_alive);
203         cnt_clr(&elem->new_node);
204         cnt_clr(&elem->into_Id);
205         cnt_clr(&elem->normalized);
206 }
207
208 /**
209  * Returns the associates node_entry_t for an ir_op (and allocates
210  * one if not yet available).
211  *
212  * @param op    the IR operation
213  * @param hmap  a hash map containing ir_op* -> node_entry_t*
214  */
215 static node_entry_t *opcode_get_entry(const ir_op *op, hmap_node_entry_t *hmap)
216 {
217         node_entry_t key;
218         node_entry_t *elem;
219
220         key.op = op;
221
222         elem = (node_entry_t*)pset_find(hmap, &key, op->code);
223         if (elem)
224                 return elem;
225
226         elem = OALLOCZ(&status->cnts, node_entry_t);
227
228         /* clear counter */
229         opcode_clear_entry(elem);
230
231         elem->op = op;
232
233         return (node_entry_t*)pset_insert(hmap, elem, op->code);
234 }
235
236 /**
237  * Returns the associates ir_op for an opcode
238  *
239  * @param code  the IR opcode
240  * @param hmap  the hash map containing opcode -> ir_op*
241  */
242 static ir_op *opcode_find_entry(ir_opcode code, hmap_ir_op *hmap)
243 {
244         ir_op key;
245
246         key.code = code;
247         return (ir_op*)pset_find(hmap, &key, code);
248 }
249
250 /**
251  * Clears all counter in a graph_entry_t.
252  *
253  * @param elem  the graph entry
254  * @param all   if non-zero, clears all counters, else leave accumulated ones
255  */
256 static void graph_clear_entry(graph_entry_t *elem, int all)
257 {
258         int i;
259
260         /* clear accumulated / non-accumulated counter */
261         for (i = all ? 0 : _gcnt_non_acc; i < _gcnt_last; ++i) {
262                 cnt_clr(&elem->cnt[i]);
263         }
264
265         if (elem->block_hash) {
266                 del_pset(elem->block_hash);
267                 elem->block_hash = NULL;
268         }
269
270         obstack_free(&elem->recalc_cnts, NULL);
271         obstack_init(&elem->recalc_cnts);
272 }
273
274 /**
275  * Returns the associated graph_entry_t for an IR graph.
276  *
277  * @param irg   the IR graph, NULL for the global counter
278  * @param hmap  the hash map containing ir_graph* -> graph_entry_t*
279  */
280 static graph_entry_t *graph_get_entry(ir_graph *irg, hmap_graph_entry_t *hmap)
281 {
282         graph_entry_t key;
283         graph_entry_t *elem;
284         size_t i;
285
286         key.irg = irg;
287
288         elem = (graph_entry_t*)pset_find(hmap, &key, hash_ptr(irg));
289
290         if (elem) {
291                 /* create hash map backend block information */
292                 if (! elem->be_block_hash)
293                         elem->be_block_hash = new_pset(be_block_cmp, 5);
294
295                 return elem;
296         }
297
298         /* allocate a new one */
299         elem = OALLOCZ(&status->cnts, graph_entry_t);
300         obstack_init(&elem->recalc_cnts);
301
302         /* clear counter */
303         graph_clear_entry(elem, 1);
304
305         /* new hash table for opcodes here  */
306         elem->opcode_hash   = new_pset(opcode_cmp, 5);
307         elem->address_mark  = new_set(address_mark_cmp, 5);
308         elem->irg           = irg;
309
310         /* these hash tables are created on demand */
311         elem->block_hash = NULL;
312
313         for (i = 0; i != ARRAY_SIZE(elem->opt_hash); ++i)
314                 elem->opt_hash[i] = new_pset(opt_cmp, 4);
315
316         return (graph_entry_t*)pset_insert(hmap, elem, hash_ptr(irg));
317 }
318
319 /**
320  * Clear all counter in an opt_entry_t.
321  */
322 static void opt_clear_entry(opt_entry_t *elem)
323 {
324         cnt_clr(&elem->count);
325 }
326
327 /**
328  * Returns the associated opt_entry_t for an IR operation.
329  *
330  * @param op    the IR operation
331  * @param hmap  the hash map containing ir_op* -> opt_entry_t*
332  */
333 static opt_entry_t *opt_get_entry(const ir_op *op, hmap_opt_entry_t *hmap)
334 {
335         opt_entry_t key;
336         opt_entry_t *elem;
337
338         key.op = op;
339
340         elem = (opt_entry_t*)pset_find(hmap, &key, op->code);
341         if (elem)
342                 return elem;
343
344         elem = OALLOCZ(&status->cnts, opt_entry_t);
345
346         /* clear new counter */
347         opt_clear_entry(elem);
348
349         elem->op = op;
350
351         return (opt_entry_t*)pset_insert(hmap, elem, op->code);
352 }
353
354 /**
355  * clears all counter in a block_entry_t
356  */
357 static void block_clear_entry(block_entry_t *elem)
358 {
359         int i;
360
361         for (i = 0; i < _bcnt_last; ++i)
362                 cnt_clr(&elem->cnt[i]);
363 }
364
365 /**
366  * Returns the associated block_entry_t for an block.
367  *
368  * @param block_nr  an IR  block number
369  * @param hmap      a hash map containing long -> block_entry_t
370  */
371 static block_entry_t *block_get_entry(struct obstack *obst, long block_nr, hmap_block_entry_t *hmap)
372 {
373         block_entry_t key;
374         block_entry_t *elem;
375
376         key.block_nr = block_nr;
377
378         elem = (block_entry_t*)pset_find(hmap, &key, block_nr);
379         if (elem)
380                 return elem;
381
382         elem = OALLOCZ(obst, block_entry_t);
383
384         /* clear new counter */
385         block_clear_entry(elem);
386
387         elem->block_nr = block_nr;
388
389         return (block_entry_t*)pset_insert(hmap, elem, block_nr);
390 }
391
392 /**
393  * Clear all sets in be_block_entry_t.
394  */
395 static void be_block_clear_entry(be_block_entry_t *elem)
396 {
397         if (elem->reg_pressure)
398                 del_pset(elem->reg_pressure);
399
400         if (elem->sched_ready)
401                 stat_delete_distrib_tbl(elem->sched_ready);
402
403         if (elem->perm_class_stat)
404                 del_pset(elem->perm_class_stat);
405
406         elem->reg_pressure    = new_pset(reg_pressure_cmp, 5);
407         elem->sched_ready     = stat_new_int_distrib_tbl();
408         elem->perm_class_stat = new_pset(perm_class_cmp, 5);
409 }
410
411 /**
412  * Returns the associated be_block_entry_t for an block.
413  *
414  * @param block_nr  an IR  block number
415  * @param hmap      a hash map containing long -> be_block_entry_t
416  */
417 static be_block_entry_t *be_block_get_entry(struct obstack *obst, long block_nr, hmap_be_block_entry_t *hmap)
418 {
419         be_block_entry_t key;
420         be_block_entry_t *elem;
421
422         key.block_nr = block_nr;
423
424         elem = (be_block_entry_t*)pset_find(hmap, &key, block_nr);
425         if (elem)
426                 return elem;
427
428         elem = OALLOCZ(obst, be_block_entry_t);
429
430         /* clear new counter */
431         be_block_clear_entry(elem);
432
433         elem->block_nr = block_nr;
434
435         return (be_block_entry_t*)pset_insert(hmap, elem, block_nr);
436 }
437
438 /**
439  * clears all sets in perm_class_entry_t
440  */
441 static void perm_class_clear_entry(perm_class_entry_t *elem)
442 {
443         if (elem->perm_stat)
444                 del_pset(elem->perm_stat);
445
446         elem->perm_stat = new_pset(perm_stat_cmp, 5);
447 }
448
449 /**
450  * Returns the associated perm_class entry for a register class.
451  *
452  * @param class_name  the register class name
453  * @param hmap        a hash map containing class_name -> perm_class_entry_t
454  */
455 static perm_class_entry_t *perm_class_get_entry(struct obstack *obst, const char *class_name,
456                                                 hmap_perm_class_entry_t *hmap)
457 {
458         perm_class_entry_t key;
459         perm_class_entry_t *elem;
460
461         key.class_name = class_name;
462
463         elem = (perm_class_entry_t*)pset_find(hmap, &key, hash_ptr(class_name));
464         if (elem)
465                 return elem;
466
467         elem = OALLOCZ(obst, perm_class_entry_t);
468
469         /* clear new counter */
470         perm_class_clear_entry(elem);
471
472         elem->class_name = class_name;
473
474         return (perm_class_entry_t*)pset_insert(hmap, elem, hash_ptr(class_name));
475 }
476
477 /**
478  * clears all sets in perm_stat_entry_t
479  */
480 static void perm_stat_clear_entry(perm_stat_entry_t *elem)
481 {
482         if (elem->chains)
483                 stat_delete_distrib_tbl(elem->chains);
484
485         if (elem->cycles)
486                 stat_delete_distrib_tbl(elem->cycles);
487
488         elem->chains = stat_new_int_distrib_tbl();
489         elem->cycles = stat_new_int_distrib_tbl();
490 }
491
492 /**
493  * Returns the associated perm_stat entry for a perm.
494  *
495  * @param perm      the perm node
496  * @param hmap      a hash map containing perm -> perm_stat_entry_t
497  */
498 static perm_stat_entry_t *perm_stat_get_entry(struct obstack *obst, ir_node *perm, hmap_perm_stat_entry_t *hmap)
499 {
500         perm_stat_entry_t key;
501         perm_stat_entry_t *elem;
502
503         key.perm = perm;
504
505         elem = (perm_stat_entry_t*)pset_find(hmap, &key, hash_ptr(perm));
506         if (elem)
507                 return elem;
508
509         elem = OALLOCZ(obst, perm_stat_entry_t);
510
511         /* clear new counter */
512         perm_stat_clear_entry(elem);
513
514         elem->perm = perm;
515
516         return (perm_stat_entry_t*)pset_insert(hmap, elem, hash_ptr(perm));
517 }
518
519 /**
520  * Clear optimizations counter,
521  */
522 static void clear_optimization_counter(void)
523 {
524         int i;
525         for (i = 0; i < FS_OPT_MAX; ++i)
526                 cnt_clr(&status->num_opts[i]);
527 }
528
529 /**
530  * Returns the ir_op for an IR-node,
531  * handles special cases and return pseudo op codes.
532  *
533  * @param none  an IR node
534  */
535 static ir_op *stat_get_irn_op(ir_node *node)
536 {
537         ir_op *op = get_irn_op(node);
538         unsigned opc = op->code;
539
540         switch (opc) {
541         case iro_Phi:
542                 if (get_irn_arity(node) == 0) {
543                         /* special case, a Phi0 node, count on extra counter */
544                         op = status->op_Phi0 ? status->op_Phi0 : op;
545                 } else if (get_irn_mode(node) == mode_M) {
546                         /* special case, a Memory Phi node, count on extra counter */
547                         op = status->op_PhiM ? status->op_PhiM : op;
548                 }
549                 break;
550         case iro_Proj:
551                 if (get_irn_mode(node) == mode_M) {
552                         /* special case, a Memory Proj node, count on extra counter */
553                         op = status->op_ProjM ? status->op_ProjM : op;
554                 }
555                 break;
556         case iro_Mul:
557                 if (is_Const(get_Mul_left(node)) || is_Const(get_Mul_right(node))) {
558                         /* special case, a Multiply by a const, count on extra counter */
559                         op = status->op_MulC ? status->op_MulC : op;
560                 }
561                 break;
562         case iro_Div:
563                 if (is_Const(get_Div_right(node))) {
564                         /* special case, a division by a const, count on extra counter */
565                         op = status->op_DivC ? status->op_DivC : op;
566                 }
567                 break;
568         case iro_Mod:
569                 if (is_Const(get_Mod_right(node))) {
570                         /* special case, a module by a const, count on extra counter */
571                         op = status->op_ModC ? status->op_ModC : op;
572                 }
573                 break;
574         case iro_Sel:
575                 if (is_Sel(get_Sel_ptr(node))) {
576                         /* special case, a Sel of a Sel, count on extra counter */
577                         op = status->op_SelSel ? status->op_SelSel : op;
578                         if (is_Sel(get_Sel_ptr(get_Sel_ptr(node)))) {
579                                 /* special case, a Sel of a Sel of a Sel, count on extra counter */
580                                 op = status->op_SelSelSel ? status->op_SelSelSel : op;
581                         }
582                 }
583                 break;
584         default:
585                 break;
586         }
587
588         return op;
589 }
590
591 /**
592  * update the block counter
593  */
594 static void undate_block_info(ir_node *node, graph_entry_t *graph)
595 {
596         ir_node *block;
597         block_entry_t *b_entry;
598         int i, arity;
599
600         /* check for block */
601         if (is_Block(node)) {
602                 arity = get_irn_arity(node);
603                 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(node), graph->block_hash);
604                 /* mark start end block to allow to filter them out */
605                 if (node == get_irg_start_block(graph->irg))
606                         b_entry->is_start = 1;
607                 else if (node == get_irg_end_block(graph->irg))
608                         b_entry->is_end = 1;
609
610                 /* count all incoming edges */
611                 for (i = 0; i < arity; ++i) {
612                         ir_node *pred = get_irn_n(node, i);
613                         ir_node *other_block = get_nodes_block(pred);
614                         block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
615
616                         cnt_inc(&b_entry->cnt[bcnt_in_edges]);  /* an edge coming from another block */
617                         cnt_inc(&b_entry_other->cnt[bcnt_out_edges]);
618                 }
619                 return;
620         }
621
622         block   = get_nodes_block(node);
623         b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(block), graph->block_hash);
624
625         if (is_Phi(node) && mode_is_datab(get_irn_mode(node))) {
626                 /* count data Phi per block */
627                 cnt_inc(&b_entry->cnt[bcnt_phi_data]);
628         }
629
630         /* we have a new node in our block */
631         cnt_inc(&b_entry->cnt[bcnt_nodes]);
632
633         /* don't count keep-alive edges */
634         if (is_End(node))
635                 return;
636
637         arity = get_irn_arity(node);
638
639         for (i = 0; i < arity; ++i) {
640                 ir_node *pred = get_irn_n(node, i);
641                 ir_node *other_block;
642
643                 other_block = get_nodes_block(pred);
644
645                 if (other_block == block)
646                         cnt_inc(&b_entry->cnt[bcnt_edges]); /* a in block edge */
647                 else {
648                         block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
649
650                         cnt_inc(&b_entry->cnt[bcnt_in_edges]);  /* an edge coming from another block */
651                         cnt_inc(&b_entry_other->cnt[bcnt_out_edges]);
652                 }
653         }
654 }
655
656 /**
657  * Calculates how many arguments of the call are const, updates
658  * param distribution.
659  */
660 static void analyse_params_of_Call(graph_entry_t *graph, ir_node *call)
661 {
662         int i, num_const_args = 0, num_local_adr = 0;
663         int n = get_Call_n_params(call);
664
665         for (i = 0; i < n; ++i) {
666                 ir_node *param = get_Call_param(call, i);
667
668                 if (is_irn_constlike(param))
669                         ++num_const_args;
670                 else if (is_Sel(param)) {
671                         ir_node *base = param;
672
673                         do {
674                                 base = get_Sel_ptr(base);
675                         } while (is_Sel(base));
676
677                         if (base == get_irg_frame(current_ir_graph))
678                                 ++num_local_adr;
679                 }
680
681         }
682
683         if (num_const_args > 0)
684                 cnt_inc(&graph->cnt[gcnt_call_with_cnst_arg]);
685         if (num_const_args == n)
686                 cnt_inc(&graph->cnt[gcnt_call_with_all_cnst_arg]);
687         if (num_local_adr > 0)
688                 cnt_inc(&graph->cnt[gcnt_call_with_local_adr]);
689
690         stat_inc_int_distrib_tbl(status->dist_param_cnt, n);
691 }
692
693 /**
694  * Update info on calls.
695  *
696  * @param call   The call
697  * @param graph  The graph entry containing the call
698  */
699 static void stat_update_call(ir_node *call, graph_entry_t *graph)
700 {
701         ir_node   *block = get_nodes_block(call);
702         ir_node   *ptr = get_Call_ptr(call);
703         ir_entity *ent = NULL;
704         ir_graph  *callee = NULL;
705
706         /*
707          * If the block is bad, the whole subgraph will collapse later
708          * so do not count this call.
709          * This happens in dead code.
710          */
711         if (is_Bad(block))
712                 return;
713
714         cnt_inc(&graph->cnt[gcnt_all_calls]);
715
716         /* found a call, this function is not a leaf */
717         graph->is_leaf = 0;
718
719         if (is_SymConst(ptr)) {
720                 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
721                         /* ok, we seems to know the entity */
722                         ent = get_SymConst_entity(ptr);
723                         callee = get_entity_irg(ent);
724
725                         /* it is recursive, if it calls at least once */
726                         if (callee == graph->irg)
727                                 graph->is_recursive = 1;
728                         if (callee == NULL)
729                                 cnt_inc(&graph->cnt[gcnt_external_calls]);
730                 }
731         } else {
732                 /* indirect call, be could not predict */
733                 cnt_inc(&graph->cnt[gcnt_indirect_calls]);
734
735                 /* NOT a leaf call */
736                 graph->is_leaf_call = LCS_NON_LEAF_CALL;
737         }
738
739         /* check, if it's a chain-call: Then, the call-block
740          * must dominate the end block. */
741         {
742                 ir_node *curr = get_irg_end_block(graph->irg);
743                 int depth = get_Block_dom_depth(block);
744
745                 for (; curr != block && get_Block_dom_depth(curr) > depth;) {
746                         curr = get_Block_idom(curr);
747
748                         if (! curr || !is_Block(curr))
749                                 break;
750                 }
751
752                 if (curr != block)
753                         graph->is_chain_call = 0;
754         }
755
756         /* check, if the callee is a leaf */
757         if (callee) {
758                 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
759
760                 if (called->is_analyzed) {
761                         if (! called->is_leaf)
762                                 graph->is_leaf_call = LCS_NON_LEAF_CALL;
763                 }
764         }
765
766         analyse_params_of_Call(graph, call);
767 }
768
769 /**
770  * Update info on calls for graphs on the wait queue.
771  */
772 static void stat_update_call_2(ir_node *call, graph_entry_t *graph)
773 {
774         ir_node   *block = get_nodes_block(call);
775         ir_node   *ptr = get_Call_ptr(call);
776         ir_entity *ent = NULL;
777         ir_graph  *callee = NULL;
778
779         /*
780          * If the block is bad, the whole subgraph will collapse later
781          * so do not count this call.
782          * This happens in dead code.
783          */
784         if (is_Bad(block))
785                 return;
786
787         if (is_SymConst(ptr)) {
788                 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
789                         /* ok, we seems to know the entity */
790                         ent = get_SymConst_entity(ptr);
791                         callee = get_entity_irg(ent);
792                 }
793         }
794
795         /* check, if the callee is a leaf */
796         if (callee) {
797                 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
798
799                 assert(called->is_analyzed);
800
801                 if (! called->is_leaf)
802                         graph->is_leaf_call = LCS_NON_LEAF_CALL;
803         } else
804                 graph->is_leaf_call = LCS_NON_LEAF_CALL;
805 }
806
807 /**
808  * Find the base address and entity of an Sel node.
809  *
810  * @param sel  the node
811  *
812  * @return the base address.
813  */
814 static ir_node *find_base_adr(ir_node *sel)
815 {
816         ir_node *ptr = get_Sel_ptr(sel);
817
818         while (is_Sel(ptr)) {
819                 sel = ptr;
820                 ptr = get_Sel_ptr(sel);
821         }
822         return ptr;
823 }
824
825 /**
826  * Update info on Load/Store address statistics.
827  */
828 static void stat_update_address(ir_node *node, graph_entry_t *graph)
829 {
830         unsigned opc = get_irn_opcode(node);
831         ir_node *base;
832         ir_graph *irg;
833
834         switch (opc) {
835         case iro_SymConst:
836                 /* a global address */
837                 cnt_inc(&graph->cnt[gcnt_global_adr]);
838                 break;
839         case iro_Sel:
840                 base = find_base_adr(node);
841                 irg = current_ir_graph;
842                 if (base == get_irg_frame(irg)) {
843                         /* a local Variable. */
844                         cnt_inc(&graph->cnt[gcnt_local_adr]);
845                 } else {
846                         /* Pointer access */
847                         if (is_Proj(base) && skip_Proj(get_Proj_pred(base)) == get_irg_start(irg)) {
848                                 /* pointer access through parameter, check for THIS */
849                                 ir_entity *ent = get_irg_entity(irg);
850
851                                 if (ent != NULL) {
852                                         ir_type *ent_tp = get_entity_type(ent);
853
854                                         if (get_method_calling_convention(ent_tp) & cc_this_call) {
855                                                 if (get_Proj_proj(base) == 0) {
856                                                         /* THIS pointer */
857                                                         cnt_inc(&graph->cnt[gcnt_this_adr]);
858                                                         goto end_parameter;
859                                                 }
860                                         }
861                                 }
862                                 /* other parameter */
863                                 cnt_inc(&graph->cnt[gcnt_param_adr]);
864 end_parameter: ;
865                         } else {
866                                 /* unknown Pointer access */
867                                 cnt_inc(&graph->cnt[gcnt_other_adr]);
868                         }
869                 }
870         default:
871                 break;
872         }
873 }
874
875 /**
876  * Walker for reachable nodes count.
877  */
878 static void update_node_stat(ir_node *node, void *env)
879 {
880         graph_entry_t *graph = (graph_entry_t*)env;
881         node_entry_t *entry;
882
883         ir_op *op = stat_get_irn_op(node);
884         int i, arity = get_irn_arity(node);
885
886         entry = opcode_get_entry(op, graph->opcode_hash);
887
888         cnt_inc(&entry->cnt_alive);
889         cnt_add_i(&graph->cnt[gcnt_edges], arity);
890
891         /* count block edges */
892         undate_block_info(node, graph);
893
894         /* handle statistics for special node types */
895
896         switch (op->code) {
897         case iro_Call:
898                 /* check for properties that depends on calls like recursion/leaf/indirect call */
899                 stat_update_call(node, graph);
900                 break;
901         case iro_Load:
902                 /* check address properties */
903                 stat_update_address(get_Load_ptr(node), graph);
904                 break;
905         case iro_Store:
906                 /* check address properties */
907                 stat_update_address(get_Store_ptr(node), graph);
908                 break;
909         case iro_Phi:
910                 /* check for non-strict Phi nodes */
911                 for (i = arity - 1; i >= 0; --i) {
912                         ir_node *pred = get_Phi_pred(node, i);
913                         if (is_Unknown(pred)) {
914                                 /* found an Unknown predecessor, graph is not strict */
915                                 graph->is_strict = 0;
916                                 break;
917                         }
918                 }
919         default:
920                 break;
921         }
922
923         /* we want to count the constant IN nodes, not the CSE'ed constant's itself */
924         if (status->stat_options & FIRMSTAT_COUNT_CONSTS) {
925                 int i;
926
927                 for (i = get_irn_arity(node) - 1; i >= 0; --i) {
928                         ir_node *pred = get_irn_n(node, i);
929
930                         if (is_Const(pred)) {
931                                 /* check properties of constants */
932                                 stat_update_const(status, pred, graph);
933                         }
934                 }
935         }
936 }
937
938 /**
939  * Walker for reachable nodes count for graphs on the wait_q.
940  */
941 static void update_node_stat_2(ir_node *node, void *env)
942 {
943         graph_entry_t *graph = (graph_entry_t*)env;
944
945         /* check for properties that depends on calls like recursion/leaf/indirect call */
946         if (is_Call(node))
947                 stat_update_call_2(node, graph);
948 }
949
950 /**
951  * Get the current address mark.
952  */
953 static unsigned get_adr_mark(graph_entry_t *graph, ir_node *node)
954 {
955         address_mark_entry_t const val = { node, 0 };
956         address_mark_entry_t *value = set_find(address_mark_entry_t, graph->address_mark, &val, sizeof(val), hash_ptr(node));
957
958         return value ? value->mark : 0;
959 }
960
961 /**
962  * Set the current address mark.
963  */
964 static void set_adr_mark(graph_entry_t *graph, ir_node *node, unsigned val)
965 {
966         address_mark_entry_t const value = { node, val };
967         (void)set_insert(address_mark_entry_t, graph->address_mark, &value, sizeof(value), hash_ptr(node));
968 }
969
970 #undef DUMP_ADR_MODE
971
972 #ifdef DUMP_ADR_MODE
973 /**
974  * a vcg attribute hook: Color a node with a different color if
975  * it's identified as a part of an address expression or at least referenced
976  * by an address expression.
977  */
978 static int stat_adr_mark_hook(FILE *F, ir_node *node, ir_node *local)
979 {
980         ir_node *n           = local ? local : node;
981         ir_graph *irg        = get_irn_irg(n);
982         graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
983         unsigned mark        = get_adr_mark(graph, n);
984
985         if (mark & MARK_ADDRESS_CALC)
986                 fprintf(F, "color: purple");
987         else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
988                 fprintf(F, "color: pink");
989         else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
990                 fprintf(F, "color: lightblue");
991         else
992                 return 0;
993
994         /* I know the color! */
995         return 1;
996 }
997 #endif /* DUMP_ADR_MODE */
998
999 /**
1000  * Return the "operational" mode of a Firm node.
1001  */
1002 static ir_mode *get_irn_op_mode(ir_node *node)
1003 {
1004         switch (get_irn_opcode(node)) {
1005         case iro_Load:
1006                 return get_Load_mode(node);
1007         case iro_Store:
1008                 return get_irn_mode(get_Store_value(node));
1009         case iro_Div:
1010                 return get_irn_mode(get_Div_left(node));
1011         case iro_Mod:
1012                 return get_irn_mode(get_Mod_left(node));
1013         case iro_Cmp:
1014                 /* Cmp is no address calculation, or is it? */
1015         default:
1016                 return get_irn_mode(node);
1017         }
1018 }
1019
1020 /**
1021  * Post-walker that marks every node that is an address calculation.
1022  *
1023  * Users of a node must be visited first. We ensure this by
1024  * calling it in the post of an outs walk. This should work even in cycles,
1025  * while the normal pre-walk will not.
1026  */
1027 static void mark_address_calc(ir_node *node, void *env)
1028 {
1029         graph_entry_t *graph = (graph_entry_t*)env;
1030         ir_mode *mode = get_irn_op_mode(node);
1031         int i, n;
1032         unsigned mark_preds = MARK_REF_NON_ADR;
1033
1034         if (! mode_is_data(mode))
1035                 return;
1036
1037         if (mode_is_reference(mode)) {
1038                 /* a reference is calculated here, we are sure */
1039                 set_adr_mark(graph, node, MARK_ADDRESS_CALC);
1040
1041                 mark_preds = MARK_REF_ADR;
1042         } else {
1043                 unsigned mark = get_adr_mark(graph, node);
1044
1045                 if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR) {
1046                         /*
1047                          * this node has no reference mode, but is only
1048                          * referenced by address calculations
1049                          */
1050                         mark_preds = MARK_REF_ADR;
1051                 }
1052         }
1053
1054         /* mark all predecessors */
1055         for (i = 0, n = get_irn_arity(node); i < n; ++i) {
1056                 ir_node *pred = get_irn_n(node, i);
1057
1058                 mode = get_irn_op_mode(pred);
1059                 if (! mode_is_data(mode))
1060                         continue;
1061
1062                 set_adr_mark(graph, pred, get_adr_mark(graph, pred) | mark_preds);
1063         }
1064 }
1065
1066 /**
1067  * Post-walker that marks every node that is an address calculation.
1068  *
1069  * Users of a node must be visited first. We ensure this by
1070  * calling it in the post of an outs walk. This should work even in cycles,
1071  * while the normal pre-walk will not.
1072  */
1073 static void count_adr_ops(ir_node *node, void *env)
1074 {
1075         graph_entry_t *graph = (graph_entry_t*)env;
1076         unsigned mark        = get_adr_mark(graph, node);
1077
1078         if (mark & MARK_ADDRESS_CALC)
1079                 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1080         else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1081                 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1082         else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1083                 cnt_inc(&graph->cnt[gcnt_all_adr_ops]);
1084 }
1085
1086 /**
1087  * Called for every graph when the graph is either deleted or stat_dump_snapshot()
1088  * is called, must recalculate all statistic info.
1089  *
1090  * @param global    The global entry
1091  * @param graph     The current entry
1092  */
1093 static void update_graph_stat(graph_entry_t *global, graph_entry_t *graph)
1094 {
1095         int i;
1096
1097         /* clear first the alive counter in the graph */
1098         foreach_pset(graph->opcode_hash, node_entry_t, entry) {
1099                 cnt_clr(&entry->cnt_alive);
1100         }
1101
1102         /* set pessimistic values */
1103         graph->is_leaf       = 1;
1104         graph->is_leaf_call  = LCS_UNKNOWN;
1105         graph->is_recursive  = 0;
1106         graph->is_chain_call = 1;
1107         graph->is_strict     = 1;
1108
1109         /* create new block counter */
1110         graph->block_hash = new_pset(block_cmp, 5);
1111
1112         /* we need dominator info */
1113         if (graph->irg != get_const_code_irg()) {
1114                 assure_doms(graph->irg);
1115         }
1116
1117         /* count the nodes in the graph */
1118         irg_walk_graph(graph->irg, update_node_stat, NULL, graph);
1119
1120         /* recursive functions are never chain calls, leafs don't have calls */
1121         if (graph->is_recursive || graph->is_leaf)
1122                 graph->is_chain_call = 0;
1123
1124         /* assume we walk every graph only ONCE, we could sum here the global count */
1125         foreach_pset(graph->opcode_hash, node_entry_t, entry) {
1126                 node_entry_t *g_entry = opcode_get_entry(entry->op, global->opcode_hash);
1127
1128                 /* update the node counter */
1129                 cnt_add(&g_entry->cnt_alive, &entry->cnt_alive);
1130         }
1131
1132         /* count the number of address calculation */
1133         if (graph->irg != get_const_code_irg()) {
1134                 ir_graph *rem = current_ir_graph;
1135
1136                 assure_irg_outs(graph->irg);
1137
1138                 /* Must be done an the outs graph */
1139                 current_ir_graph = graph->irg;
1140                 irg_out_walk(get_irg_start(graph->irg), NULL, mark_address_calc, graph);
1141                 current_ir_graph = rem;
1142
1143 #ifdef DUMP_ADR_MODE
1144                 /* register the vcg hook and dump the graph for test */
1145                 set_dump_node_vcgattr_hook(stat_adr_mark_hook);
1146                 dump_ir_block_graph(graph->irg, "-adr");
1147                 set_dump_node_vcgattr_hook(NULL);
1148 #endif /* DUMP_ADR_MODE */
1149
1150                 irg_walk_graph(graph->irg, NULL, count_adr_ops, graph);
1151         }
1152
1153         /* count the DAG's */
1154         if (status->stat_options & FIRMSTAT_COUNT_DAG)
1155                 count_dags_in_graph(global, graph);
1156
1157         /* calculate the patterns of this graph */
1158         stat_calc_pattern_history(graph->irg);
1159
1160         /* leaf function did not call others */
1161         if (graph->is_leaf)
1162                 graph->is_leaf_call = LCS_NON_LEAF_CALL;
1163         else if (graph->is_leaf_call == LCS_UNKNOWN) {
1164                 /* we still don't know if this graph calls leaf-functions, so enqueue */
1165                 pdeq_putl(status->wait_q, graph);
1166         }
1167
1168         /* we have analyzed this graph */
1169         graph->is_analyzed = 1;
1170
1171         /* accumulate all counter's */
1172         for (i = 0; i < _gcnt_last; ++i)
1173                 cnt_add(&global->cnt[i], &graph->cnt[i]);
1174 }
1175
1176 /**
1177  * Called for every graph that was on the wait_q in stat_dump_snapshot()
1178  * must finish all statistic info calculations.
1179  *
1180  * @param global    The global entry
1181  * @param graph     The current entry
1182  */
1183 static void update_graph_stat_2(graph_entry_t *global, graph_entry_t *graph)
1184 {
1185         (void) global;
1186         if (graph->is_deleted) {
1187                 /* deleted, ignore */
1188                 return;
1189         }
1190
1191         if (graph->irg) {
1192                 /* count the nodes in the graph */
1193                 irg_walk_graph(graph->irg, update_node_stat_2, NULL, graph);
1194
1195                 if (graph->is_leaf_call == LCS_UNKNOWN)
1196                         graph->is_leaf_call = LCS_LEAF_CALL;
1197         }
1198 }
1199
1200 /**
1201  * Register a dumper.
1202  */
1203 static void stat_register_dumper(const dumper_t *dumper)
1204 {
1205         dumper_t *p = XMALLOC(dumper_t);
1206         *p = *dumper;
1207
1208         p->next        = status->dumper;
1209         p->status      = status;
1210         status->dumper = p;
1211
1212         /* FIXME: memory leak */
1213 }
1214
1215 /**
1216  * Dumps the statistics of an IR graph.
1217  */
1218 static void stat_dump_graph(graph_entry_t *entry)
1219 {
1220         dumper_t *dumper;
1221
1222         for (dumper = status->dumper; dumper; dumper = dumper->next) {
1223                 if (dumper->dump_graph)
1224                         dumper->dump_graph(dumper, entry);
1225         }
1226 }
1227
1228 /**
1229  * Calls all registered dumper functions.
1230  */
1231 static void stat_dump_registered(graph_entry_t *entry)
1232 {
1233         dumper_t *dumper;
1234
1235         for (dumper = status->dumper; dumper; dumper = dumper->next) {
1236                 if (dumper->func_map) {
1237                         foreach_pset(dumper->func_map, dump_graph_FUNC, func)
1238                                 func(dumper, entry);
1239                 }
1240         }
1241 }
1242
1243 /**
1244  * Dumps a constant table.
1245  */
1246 static void stat_dump_consts(const constant_info_t *tbl)
1247 {
1248         dumper_t *dumper;
1249
1250         for (dumper = status->dumper; dumper; dumper = dumper->next) {
1251                 if (dumper->dump_const_tbl)
1252                         dumper->dump_const_tbl(dumper, tbl);
1253         }
1254 }
1255
1256 /**
1257  * Dumps the parameter distribution
1258  */
1259 static void stat_dump_param_tbl(const distrib_tbl_t *tbl, graph_entry_t *global)
1260 {
1261         dumper_t *dumper;
1262
1263         for (dumper = status->dumper; dumper; dumper = dumper->next) {
1264                 if (dumper->dump_param_tbl)
1265                         dumper->dump_param_tbl(dumper, tbl, global);
1266         }
1267 }
1268
1269 /**
1270  * Dumps the optimization counter
1271  */
1272 static void stat_dump_opt_cnt(const counter_t *tbl, unsigned len)
1273 {
1274         dumper_t *dumper;
1275
1276         for (dumper = status->dumper; dumper; dumper = dumper->next) {
1277                 if (dumper->dump_opt_cnt)
1278                         dumper->dump_opt_cnt(dumper, tbl, len);
1279         }
1280 }
1281
1282 /**
1283  * Initialize the dumper.
1284  */
1285 static void stat_dump_init(const char *name)
1286 {
1287         dumper_t *dumper;
1288
1289         for (dumper = status->dumper; dumper; dumper = dumper->next) {
1290                 if (dumper->init)
1291                         dumper->init(dumper, name);
1292         }
1293 }
1294
1295 /**
1296  * Finish the dumper.
1297  */
1298 static void stat_dump_finish(void)
1299 {
1300         dumper_t *dumper;
1301
1302         for (dumper = status->dumper; dumper; dumper = dumper->next) {
1303                 if (dumper->finish)
1304                         dumper->finish(dumper);
1305         }
1306 }
1307
1308 /**
1309  * Register an additional function for all dumper.
1310  */
1311 void stat_register_dumper_func(dump_graph_FUNC *const func)
1312 {
1313         dumper_t *dumper;
1314
1315         for (dumper = status->dumper; dumper; dumper = dumper->next) {
1316                 if (! dumper->func_map)
1317                         dumper->func_map = pset_new_ptr(3);
1318                 pset_insert_ptr(dumper->func_map, (void*)func);
1319         }
1320 }
1321
1322 /* ---------------------------------------------------------------------- */
1323
1324 /*
1325  * Helper: get an ir_op from an opcode.
1326  */
1327 ir_op *stat_get_op_from_opcode(unsigned code)
1328 {
1329         return opcode_find_entry((ir_opcode)code, status->ir_op_hash);
1330 }
1331
1332 /**
1333  * Hook: A new IR op is registered.
1334  *
1335  * @param ctx  the hook context
1336  * @param op   the new IR opcode that was created.
1337  */
1338 static void stat_new_ir_op(void *ctx, ir_op *op)
1339 {
1340         (void) ctx;
1341         if (! status->stat_options)
1342                 return;
1343
1344         STAT_ENTER;
1345         {
1346                 graph_entry_t *graph = graph_get_entry(NULL, status->irg_hash);
1347
1348                 /* execute for side effect :-) */
1349                 (void)opcode_get_entry(op, graph->opcode_hash);
1350
1351                 pset_insert(status->ir_op_hash, op, op->code);
1352         }
1353         STAT_LEAVE;
1354 }
1355
1356 /**
1357  * Hook: An IR op is freed.
1358  *
1359  * @param ctx  the hook context
1360  * @param op   the IR opcode that is freed
1361  */
1362 static void stat_free_ir_op(void *ctx, ir_op *op)
1363 {
1364         (void) ctx;
1365         (void) op;
1366         if (! status->stat_options)
1367                 return;
1368
1369         STAT_ENTER;
1370         {
1371         }
1372         STAT_LEAVE;
1373 }
1374
1375 /**
1376  * Hook: A new node is created.
1377  *
1378  * @param ctx   the hook context
1379  * @param irg   the IR graph on which the node is created
1380  * @param node  the new IR node that was created
1381  */
1382 static void stat_new_node(void *ctx, ir_graph *irg, ir_node *node)
1383 {
1384         (void) ctx;
1385         (void) irg;
1386         if (! status->stat_options)
1387                 return;
1388
1389         /* do NOT count during dead node elimination */
1390         if (status->in_dead_node_elim)
1391                 return;
1392
1393         STAT_ENTER;
1394         {
1395                 node_entry_t *entry;
1396                 graph_entry_t *graph;
1397                 ir_op *op = stat_get_irn_op(node);
1398
1399                 /* increase global value */
1400                 graph = graph_get_entry(NULL, status->irg_hash);
1401                 entry = opcode_get_entry(op, graph->opcode_hash);
1402                 cnt_inc(&entry->new_node);
1403
1404                 /* increase local value */
1405                 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1406                 entry = opcode_get_entry(op, graph->opcode_hash);
1407                 cnt_inc(&entry->new_node);
1408         }
1409         STAT_LEAVE;
1410 }
1411
1412 /**
1413  * Hook: A node is changed into a Id node
1414  *
1415  * @param ctx   the hook context
1416  * @param node  the IR node that will be turned into an ID
1417  */
1418 static void stat_turn_into_id(void *ctx, ir_node *node)
1419 {
1420         (void) ctx;
1421         if (! status->stat_options)
1422                 return;
1423
1424         STAT_ENTER;
1425         {
1426                 node_entry_t *entry;
1427                 graph_entry_t *graph;
1428                 ir_op *op = stat_get_irn_op(node);
1429
1430                 /* increase global value */
1431                 graph = graph_get_entry(NULL, status->irg_hash);
1432                 entry = opcode_get_entry(op, graph->opcode_hash);
1433                 cnt_inc(&entry->into_Id);
1434
1435                 /* increase local value */
1436                 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1437                 entry = opcode_get_entry(op, graph->opcode_hash);
1438                 cnt_inc(&entry->into_Id);
1439         }
1440         STAT_LEAVE;
1441 }
1442
1443 /**
1444  * Hook: A node is normalized
1445  *
1446  * @param ctx   the hook context
1447  * @param node  the IR node that was normalized
1448  */
1449 static void stat_normalize(void *ctx, ir_node *node)
1450 {
1451         (void) ctx;
1452         if (! status->stat_options)
1453                 return;
1454
1455         STAT_ENTER;
1456         {
1457                 node_entry_t *entry;
1458                 graph_entry_t *graph;
1459                 ir_op *op = stat_get_irn_op(node);
1460
1461                 /* increase global value */
1462                 graph = graph_get_entry(NULL, status->irg_hash);
1463                 entry = opcode_get_entry(op, graph->opcode_hash);
1464                 cnt_inc(&entry->normalized);
1465
1466                 /* increase local value */
1467                 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1468                 entry = opcode_get_entry(op, graph->opcode_hash);
1469                 cnt_inc(&entry->normalized);
1470         }
1471         STAT_LEAVE;
1472 }
1473
1474 /**
1475  * Hook: A new graph was created
1476  *
1477  * @param ctx  the hook context
1478  * @param irg  the new IR graph that was created
1479  * @param ent  the entity of this graph
1480  */
1481 static void stat_new_graph(void *ctx, ir_graph *irg, ir_entity *ent)
1482 {
1483         (void) ctx;
1484         if (! status->stat_options)
1485                 return;
1486
1487         STAT_ENTER;
1488         {
1489                 /* execute for side effect :-) */
1490                 graph_entry_t * graph = graph_get_entry(irg, status->irg_hash);
1491
1492                 graph->ent           = ent;
1493                 graph->is_deleted    = 0;
1494                 graph->is_leaf       = 0;
1495                 graph->is_leaf_call  = 0;
1496                 graph->is_recursive  = 0;
1497                 graph->is_chain_call = 0;
1498                 graph->is_strict     = 1;
1499                 graph->is_analyzed   = 0;
1500         }
1501         STAT_LEAVE;
1502 }
1503
1504 /**
1505  * Hook: A graph will be deleted
1506  *
1507  * @param ctx  the hook context
1508  * @param irg  the IR graph that will be deleted
1509  *
1510  * Note that we still hold the information for this graph
1511  * in our hash maps, only a flag is set which prevents this
1512  * information from being changed, it's "frozen" from now.
1513  */
1514 static void stat_free_graph(void *ctx, ir_graph *irg)
1515 {
1516         (void) ctx;
1517         if (! status->stat_options)
1518                 return;
1519
1520         STAT_ENTER;
1521         {
1522                 graph_entry_t *graph  = graph_get_entry(irg, status->irg_hash);
1523                 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1524
1525                 graph->is_deleted = 1;
1526
1527                 if (status->stat_options & FIRMSTAT_COUNT_DELETED) {
1528                         /* count the nodes of the graph yet, it will be destroyed later */
1529                         update_graph_stat(global, graph);
1530                 }
1531         }
1532         STAT_LEAVE;
1533 }
1534
1535 /**
1536  * Hook: A walk over a graph is initiated. Do not count walks from statistic code.
1537  *
1538  * @param ctx  the hook context
1539  * @param irg  the IR graph that will be walked
1540  * @param pre  the pre walker
1541  * @param post the post walker
1542  */
1543 static void stat_irg_walk(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1544 {
1545         (void) ctx;
1546         (void) pre;
1547         (void) post;
1548         if (! status->stat_options)
1549                 return;
1550
1551         STAT_ENTER_SINGLE;
1552         {
1553                 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1554
1555                 cnt_inc(&graph->cnt[gcnt_acc_walked]);
1556         }
1557         STAT_LEAVE;
1558 }
1559
1560 /**
1561  * Hook: A walk over a graph in block-wise order is initiated. Do not count walks from statistic code.
1562  *
1563  * @param ctx  the hook context
1564  * @param irg  the IR graph that will be walked
1565  * @param pre  the pre walker
1566  * @param post the post walker
1567  */
1568 static void stat_irg_walk_blkwise(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1569 {
1570         /* for now, do NOT differentiate between blockwise and normal */
1571         stat_irg_walk(ctx, irg, pre, post);
1572 }
1573
1574 /**
1575  * Hook: A walk over the graph's blocks is initiated. Do not count walks from statistic code.
1576  *
1577  * @param ctx  the hook context
1578  * @param irg  the IR graph that will be walked
1579  * @param node the IR node
1580  * @param pre  the pre walker
1581  * @param post the post walker
1582  */
1583 static void stat_irg_block_walk(void *ctx, ir_graph *irg, ir_node *node, generic_func *pre, generic_func *post)
1584 {
1585         (void) ctx;
1586         (void) node;
1587         (void) pre;
1588         (void) post;
1589         if (! status->stat_options)
1590                 return;
1591
1592         STAT_ENTER_SINGLE;
1593         {
1594                 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1595
1596                 cnt_inc(&graph->cnt[gcnt_acc_walked_blocks]);
1597         }
1598         STAT_LEAVE;
1599 }
1600
1601 /**
1602  * Called for every node that is removed due to an optimization.
1603  *
1604  * @param n     the IR node that will be removed
1605  * @param hmap  the hash map containing ir_op* -> opt_entry_t*
1606  * @param kind  the optimization kind
1607  */
1608 static void removed_due_opt(ir_node *n, hmap_opt_entry_t *hmap, hook_opt_kind kind)
1609 {
1610         opt_entry_t *entry;
1611         ir_op *op = stat_get_irn_op(n);
1612
1613         /* ignore CSE for Constants */
1614         if (kind == HOOK_OPT_CSE && (is_Const(n) || is_SymConst(n)))
1615                 return;
1616
1617         /* increase global value */
1618         entry = opt_get_entry(op, hmap);
1619         cnt_inc(&entry->count);
1620 }
1621
1622 /**
1623  * Hook: Some nodes were optimized into some others due to an optimization.
1624  *
1625  * @param ctx  the hook context
1626  */
1627 static void stat_merge_nodes(
1628     void *ctx,
1629     ir_node **new_node_array, int new_num_entries,
1630     ir_node **old_node_array, int old_num_entries,
1631     hook_opt_kind opt)
1632 {
1633         (void) ctx;
1634         if (! status->stat_options)
1635                 return;
1636
1637         STAT_ENTER;
1638         {
1639                 int i, j;
1640                 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1641
1642                 cnt_inc(&status->num_opts[opt]);
1643                 if (status->reassoc_run)
1644                         opt = HOOK_OPT_REASSOC;
1645
1646                 for (i = 0; i < old_num_entries; ++i) {
1647                         /* nodes might be in new and old, so if we found a node
1648                            in both sets, this one  is NOT removed */
1649                         for (j = 0; j < new_num_entries; ++j) {
1650                                 if (old_node_array[i] == new_node_array[j])
1651                                         break;
1652                         }
1653                         if (j >= new_num_entries) {
1654                                 int xopt = opt;
1655
1656                                 /* sometimes we did not detect, that it is replaced by a Const */
1657                                 if (opt == HOOK_OPT_CONFIRM && new_num_entries == 1) {
1658                                         ir_node *const irn = new_node_array[0];
1659                                         if (is_Const(irn) || is_SymConst(irn))
1660                                                 xopt = HOOK_OPT_CONFIRM_C;
1661                                 }
1662
1663                                 removed_due_opt(old_node_array[i], graph->opt_hash[xopt], (hook_opt_kind)xopt);
1664                         }
1665                 }
1666         }
1667         STAT_LEAVE;
1668 }
1669
1670 /**
1671  * Hook: Reassociation is started/stopped.
1672  *
1673  * @param ctx   the hook context
1674  * @param flag  if non-zero, reassociation is started else stopped
1675  */
1676 static void stat_reassociate(void *ctx, int flag)
1677 {
1678         (void) ctx;
1679         if (! status->stat_options)
1680                 return;
1681
1682         STAT_ENTER;
1683         {
1684                 status->reassoc_run = flag;
1685         }
1686         STAT_LEAVE;
1687 }
1688
1689 /**
1690  * Hook: A node was lowered into other nodes
1691  *
1692  * @param ctx  the hook context
1693  * @param node the IR node that will be lowered
1694  */
1695 static void stat_lower(void *ctx, ir_node *node)
1696 {
1697         (void) ctx;
1698         if (! status->stat_options)
1699                 return;
1700
1701         STAT_ENTER;
1702         {
1703                 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1704
1705                 removed_due_opt(node, graph->opt_hash[HOOK_LOWERED], HOOK_LOWERED);
1706         }
1707         STAT_LEAVE;
1708 }
1709
1710 /**
1711  * Hook: A graph was inlined.
1712  *
1713  * @param ctx  the hook context
1714  * @param call the IR call that will re changed into the body of
1715  *             the called IR graph
1716  * @param called_irg  the IR graph representing the called routine
1717  */
1718 static void stat_inline(void *ctx, ir_node *call, ir_graph *called_irg)
1719 {
1720         (void) ctx;
1721         if (! status->stat_options)
1722                 return;
1723
1724         STAT_ENTER;
1725         {
1726                 ir_graph *irg = get_irn_irg(call);
1727                 graph_entry_t *i_graph = graph_get_entry(called_irg, status->irg_hash);
1728                 graph_entry_t *graph   = graph_get_entry(irg, status->irg_hash);
1729
1730                 cnt_inc(&graph->cnt[gcnt_acc_got_inlined]);
1731                 cnt_inc(&i_graph->cnt[gcnt_acc_was_inlined]);
1732         }
1733         STAT_LEAVE;
1734 }
1735
1736 /**
1737  * Hook: A graph with tail-recursions was optimized.
1738  *
1739  * @param ctx  the hook context
1740  */
1741 static void stat_tail_rec(void *ctx, ir_graph *irg, int n_calls)
1742 {
1743         (void) ctx;
1744         if (! status->stat_options)
1745                 return;
1746
1747         STAT_ENTER;
1748         {
1749                 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1750
1751                 graph->num_tail_recursion += n_calls;
1752         }
1753         STAT_LEAVE;
1754 }
1755
1756 /**
1757  * Strength reduction was performed on an iteration variable.
1758  *
1759  * @param ctx  the hook context
1760  */
1761 static void stat_strength_red(void *ctx, ir_graph *irg, ir_node *strong)
1762 {
1763         (void) ctx;
1764         if (! status->stat_options)
1765                 return;
1766
1767         STAT_ENTER;
1768         {
1769                 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1770                 cnt_inc(&graph->cnt[gcnt_acc_strength_red]);
1771
1772                 removed_due_opt(strong, graph->opt_hash[HOOK_OPT_STRENGTH_RED], HOOK_OPT_STRENGTH_RED);
1773         }
1774         STAT_LEAVE;
1775 }
1776
1777 /**
1778  * Hook: Start/Stop the dead node elimination.
1779  *
1780  * @param ctx  the hook context
1781  */
1782 static void stat_dead_node_elim(void *ctx, ir_graph *irg, int start)
1783 {
1784         (void) ctx;
1785         (void) irg;
1786         if (! status->stat_options)
1787                 return;
1788
1789         status->in_dead_node_elim = (start != 0);
1790 }
1791
1792 /**
1793  * Hook: if-conversion was tried.
1794  */
1795 static void stat_if_conversion(void *context, ir_graph *irg, ir_node *phi,
1796                                int pos, ir_node *mux, if_result_t reason)
1797 {
1798         (void) context;
1799         (void) phi;
1800         (void) pos;
1801         (void) mux;
1802         if (! status->stat_options)
1803                 return;
1804
1805         STAT_ENTER;
1806         {
1807                 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1808
1809                 cnt_inc(&graph->cnt[gcnt_if_conv + reason]);
1810         }
1811         STAT_LEAVE;
1812 }
1813
1814 /**
1815  * Hook: real function call was optimized.
1816  */
1817 static void stat_func_call(void *context, ir_graph *irg, ir_node *call)
1818 {
1819         (void) context;
1820         (void) call;
1821         if (! status->stat_options)
1822                 return;
1823
1824         STAT_ENTER;
1825         {
1826                 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1827
1828                 cnt_inc(&graph->cnt[gcnt_acc_real_func_call]);
1829         }
1830         STAT_LEAVE;
1831 }
1832
1833 /**
1834  * Hook: A multiply was replaced by a series of Shifts/Adds/Subs.
1835  *
1836  * @param ctx  the hook context
1837  */
1838 static void stat_arch_dep_replace_mul_with_shifts(void *ctx, ir_node *mul)
1839 {
1840         (void) ctx;
1841         if (! status->stat_options)
1842                 return;
1843
1844         STAT_ENTER;
1845         {
1846                 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1847                 removed_due_opt(mul, graph->opt_hash[HOOK_OPT_ARCH_DEP], HOOK_OPT_ARCH_DEP);
1848         }
1849         STAT_LEAVE;
1850 }
1851
1852 /**
1853  * Hook: A division by const was replaced.
1854  *
1855  * @param ctx   the hook context
1856  * @param node  the division node that will be optimized
1857  */
1858 static void stat_arch_dep_replace_division_by_const(void *ctx, ir_node *node)
1859 {
1860         (void) ctx;
1861         if (! status->stat_options)
1862                 return;
1863
1864         STAT_ENTER;
1865         {
1866                 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1867                 removed_due_opt(node, graph->opt_hash[HOOK_OPT_ARCH_DEP], HOOK_OPT_ARCH_DEP);
1868         }
1869         STAT_LEAVE;
1870 }
1871
1872 /*
1873  * Update the register pressure of a block.
1874  *
1875  * @param irg        the irg containing the block
1876  * @param block      the block for which the reg pressure should be set
1877  * @param pressure   the pressure
1878  * @param class_name the name of the register class
1879  */
1880 void stat_be_block_regpressure(ir_graph *irg, ir_node *block, int pressure, const char *class_name)
1881 {
1882         if (! status->stat_options)
1883                 return;
1884
1885         STAT_ENTER;
1886         {
1887                 graph_entry_t        *graph = graph_get_entry(irg, status->irg_hash);
1888                 be_block_entry_t     *block_ent;
1889                 reg_pressure_entry_t *rp_ent;
1890
1891                 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1892                 rp_ent    = OALLOCZ(&status->be_data, reg_pressure_entry_t);
1893
1894                 rp_ent->class_name = class_name;
1895                 rp_ent->pressure   = pressure;
1896
1897                 pset_insert(block_ent->reg_pressure, rp_ent, hash_ptr(class_name));
1898         }
1899         STAT_LEAVE;
1900 }
1901
1902 /**
1903  * Update the distribution of ready nodes of a block
1904  *
1905  * @param irg        the irg containing the block
1906  * @param block      the block for which the reg pressure should be set
1907  * @param num_ready  the number of ready nodes
1908  */
1909 void stat_be_block_sched_ready(ir_graph *irg, ir_node *block, int num_ready)
1910 {
1911         if (! status->stat_options)
1912                 return;
1913
1914         STAT_ENTER;
1915         {
1916                 graph_entry_t    *graph = graph_get_entry(irg, status->irg_hash);
1917                 be_block_entry_t *block_ent;
1918
1919                 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1920
1921                 /* increase the counter of corresponding number of ready nodes */
1922                 stat_inc_int_distrib_tbl(block_ent->sched_ready, num_ready);
1923         }
1924         STAT_LEAVE;
1925 }
1926
1927 /**
1928  * Update the permutation statistic of a block.
1929  *
1930  * @param class_name the name of the register class
1931  * @param n_regs     number of registers in the register class
1932  * @param perm       the perm node
1933  * @param block      the block containing the perm
1934  * @param size       the size of the perm
1935  * @param real_size  number of pairs with different registers
1936  */
1937 void stat_be_block_stat_perm(const char *class_name, int n_regs, ir_node *perm, ir_node *block,
1938                              int size, int real_size)
1939 {
1940         if (! status->stat_options)
1941                 return;
1942
1943         STAT_ENTER;
1944         {
1945                 graph_entry_t      *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
1946                 be_block_entry_t   *block_ent;
1947                 perm_class_entry_t *pc_ent;
1948                 perm_stat_entry_t  *ps_ent;
1949
1950                 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1951                 pc_ent    = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
1952                 ps_ent    = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
1953
1954                 pc_ent->n_regs = n_regs;
1955
1956                 /* update information */
1957                 ps_ent->size      = size;
1958                 ps_ent->real_size = real_size;
1959         }
1960         STAT_LEAVE;
1961 }
1962
1963 /**
1964  * Update the permutation statistic of a single perm.
1965  *
1966  * @param class_name the name of the register class
1967  * @param perm       the perm node
1968  * @param block      the block containing the perm
1969  * @param is_chain   1 if chain, 0 if cycle
1970  * @param size       length of the cycle/chain
1971  * @param n_ops      the number of ops representing this cycle/chain after lowering
1972  */
1973 void stat_be_block_stat_permcycle(const char *class_name, ir_node *perm, ir_node *block,
1974                                   int is_chain, int size, int n_ops)
1975 {
1976         if (! status->stat_options)
1977                 return;
1978
1979         STAT_ENTER;
1980         {
1981                 graph_entry_t      *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
1982                 be_block_entry_t   *block_ent;
1983                 perm_class_entry_t *pc_ent;
1984                 perm_stat_entry_t  *ps_ent;
1985
1986                 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1987                 pc_ent    = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
1988                 ps_ent    = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
1989
1990                 if (is_chain) {
1991                         ps_ent->n_copies += n_ops;
1992                         stat_inc_int_distrib_tbl(ps_ent->chains, size);
1993                 } else {
1994                         ps_ent->n_exchg += n_ops;
1995                         stat_inc_int_distrib_tbl(ps_ent->cycles, size);
1996                 }
1997         }
1998         STAT_LEAVE;
1999 }
2000
2001 /* Dumps a statistics snapshot. */
2002 void stat_dump_snapshot(const char *name, const char *phase)
2003 {
2004         char fname[2048];
2005         const char *p;
2006         size_t l;
2007
2008         if (! status->stat_options)
2009                 return;
2010
2011         STAT_ENTER;
2012         {
2013                 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
2014
2015                 /*
2016                  * The constant counter is only global, so we clear it here.
2017                  * Note that it does NOT contain the constants in DELETED
2018                  * graphs due to this.
2019                  */
2020                 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2021                         stat_const_clear(status);
2022
2023                 /* build the name */
2024                 p = strrchr(name, '/');
2025 #ifdef _WIN32
2026                 {
2027                         const char *q;
2028
2029                         q = strrchr(name, '\\');
2030
2031                         /* NULL might be not the smallest pointer */
2032                         if (q && (!p || q > p))
2033                                 p = q;
2034                 }
2035 #endif /* _WIN32 */
2036                 if (p) {
2037                         ++p;
2038                         l = p - name;
2039
2040                         if (l > (int) (sizeof(fname) - 1))
2041                                 l = sizeof(fname) - 1;
2042
2043                         memcpy(fname, name, l);
2044                         fname[l] = '\0';
2045                 } else {
2046                         fname[0] = '\0';
2047                         p = name;
2048                 }
2049                 strncat(fname, "firmstat-", sizeof(fname)-strlen(fname)-1);
2050                 strncat(fname, phase,       sizeof(fname)-strlen(fname)-1);
2051                 strncat(fname, "-",         sizeof(fname)-strlen(fname)-1);
2052                 strncat(fname, p,           sizeof(fname)-strlen(fname)-1);
2053
2054                 stat_dump_init(fname);
2055
2056                 /* calculate the graph statistics */
2057                 foreach_pset(status->irg_hash, graph_entry_t, entry) {
2058                         if (entry->irg == NULL) {
2059                                 /* special entry for the global count */
2060                                 continue;
2061                         }
2062                         if (! entry->is_deleted) {
2063                                 /* the graph is still alive, count the nodes on it */
2064                                 update_graph_stat(global, entry);
2065                         }
2066                 }
2067
2068                 /* some calculations are dependent, we pushed them on the wait_q */
2069                 while (! pdeq_empty(status->wait_q)) {
2070                         graph_entry_t *const entry = (graph_entry_t*)pdeq_getr(status->wait_q);
2071
2072                         update_graph_stat_2(global, entry);
2073                 }
2074
2075                 /* dump per graph */
2076                 foreach_pset(status->irg_hash, graph_entry_t, entry) {
2077                         if (entry->irg == NULL) {
2078                                 /* special entry for the global count */
2079                                 continue;
2080                         }
2081
2082                         if (! entry->is_deleted || status->stat_options & FIRMSTAT_COUNT_DELETED) {
2083                                 stat_dump_graph(entry);
2084                                 stat_dump_registered(entry);
2085                         }
2086
2087                         if (! entry->is_deleted) {
2088                                 /* clear the counter that are not accumulated */
2089                                 graph_clear_entry(entry, 0);
2090                         }
2091                 }
2092
2093                 /* dump global */
2094                 stat_dump_graph(global);
2095
2096                 /* dump the const info */
2097                 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2098                         stat_dump_consts(&status->const_info);
2099
2100                 /* dump the parameter distribution */
2101                 stat_dump_param_tbl(status->dist_param_cnt, global);
2102
2103                 /* dump the optimization counter and clear them */
2104                 stat_dump_opt_cnt(status->num_opts, ARRAY_SIZE(status->num_opts));
2105                 clear_optimization_counter();
2106
2107                 stat_dump_finish();
2108
2109                 stat_finish_pattern_history(fname);
2110
2111                 /* clear the global counters here */
2112                 foreach_pset(global->opcode_hash, node_entry_t, entry) {
2113                         opcode_clear_entry(entry);
2114                 }
2115                 /* clear all global counter */
2116                 graph_clear_entry(global, /*all=*/1);
2117         }
2118         STAT_LEAVE;
2119 }
2120
2121 typedef struct pass_t {
2122         ir_prog_pass_t pass;
2123         const char     *fname;
2124         const char     *phase;
2125 } pass_t;
2126
2127 /**
2128  * Wrapper to run stat_dump_snapshot() as a ir_prog wrapper.
2129  */
2130 static int stat_dump_snapshot_wrapper(ir_prog *irp, void *context)
2131 {
2132         pass_t *pass = (pass_t*)context;
2133
2134         (void)irp;
2135         stat_dump_snapshot(pass->fname, pass->phase);
2136         return 0;
2137 }
2138
2139 /**
2140  * Ensure that no verifier is run from the wrapper.
2141  */
2142 static int no_verify(ir_prog *prog, void *ctx)
2143 {
2144         (void)prog;
2145         (void)ctx;
2146         return 0;
2147 }
2148
2149 /**
2150  * Ensure that no dumper is run from the wrapper.
2151  */
2152 static void no_dump(ir_prog *prog, void *ctx, unsigned idx)
2153 {
2154         (void)prog;
2155         (void)ctx;
2156         (void)idx;
2157 }
2158
2159 /* create an ir_pog pass */
2160 ir_prog_pass_t *stat_dump_snapshot_pass(
2161         const char *name, const char *fname, const char *phase)
2162 {
2163         pass_t *pass = XMALLOCZ(pass_t);
2164
2165         def_prog_pass_constructor(
2166                 &pass->pass, name ? name : "stat_snapshot", stat_dump_snapshot_wrapper);
2167         pass->fname = fname;
2168         pass->phase = phase;
2169
2170         /* no dump/verify */
2171         pass->pass.dump_irprog   = no_dump;
2172         pass->pass.verify_irprog = no_verify;
2173
2174         return &pass->pass;
2175 }
2176
2177 /** the hook entries for the Firm statistics module */
2178 static hook_entry_t stat_hooks[hook_last];
2179
2180 /* initialize the statistics module. */
2181 void firm_init_stat(void)
2182 {
2183 #define X(a)  a, sizeof(a)-1
2184 #define HOOK(h, fkt) \
2185         stat_hooks[h].hook._##h = fkt; register_hook(h, &stat_hooks[h])
2186         unsigned num = 0;
2187
2188         if (! (stat_options & FIRMSTAT_ENABLED))
2189                 return;
2190
2191         status = XMALLOCZ(stat_info_t);
2192
2193         /* enable statistics */
2194         status->stat_options = stat_options & FIRMSTAT_ENABLED ? stat_options : 0;
2195
2196         /* register all hooks */
2197         HOOK(hook_new_ir_op,                          stat_new_ir_op);
2198         HOOK(hook_free_ir_op,                         stat_free_ir_op);
2199         HOOK(hook_new_node,                           stat_new_node);
2200         HOOK(hook_turn_into_id,                       stat_turn_into_id);
2201         HOOK(hook_normalize,                          stat_normalize);
2202         HOOK(hook_new_graph,                          stat_new_graph);
2203         HOOK(hook_free_graph,                         stat_free_graph);
2204         HOOK(hook_irg_walk,                           stat_irg_walk);
2205         HOOK(hook_irg_walk_blkwise,                   stat_irg_walk_blkwise);
2206         HOOK(hook_irg_block_walk,                     stat_irg_block_walk);
2207         HOOK(hook_merge_nodes,                        stat_merge_nodes);
2208         HOOK(hook_reassociate,                        stat_reassociate);
2209         HOOK(hook_lower,                              stat_lower);
2210         HOOK(hook_inline,                             stat_inline);
2211         HOOK(hook_tail_rec,                           stat_tail_rec);
2212         HOOK(hook_strength_red,                       stat_strength_red);
2213         HOOK(hook_dead_node_elim,                     stat_dead_node_elim);
2214         HOOK(hook_if_conversion,                      stat_if_conversion);
2215         HOOK(hook_func_call,                          stat_func_call);
2216         HOOK(hook_arch_dep_replace_mul_with_shifts,   stat_arch_dep_replace_mul_with_shifts);
2217         HOOK(hook_arch_dep_replace_division_by_const, stat_arch_dep_replace_division_by_const);
2218
2219         obstack_init(&status->cnts);
2220         obstack_init(&status->be_data);
2221
2222         /* create the hash-tables */
2223         status->irg_hash   = new_pset(graph_cmp, 8);
2224         status->ir_op_hash = new_pset(opcode_cmp_2, 1);
2225
2226         /* create the wait queue */
2227         status->wait_q     = new_pdeq();
2228
2229         if (stat_options & FIRMSTAT_COUNT_STRONG_OP) {
2230                 /* build the pseudo-ops */
2231
2232                 _op_Phi0.code    = --num;
2233                 _op_Phi0.name    = new_id_from_chars(X("Phi0"));
2234
2235                 _op_PhiM.code    = --num;
2236                 _op_PhiM.name    = new_id_from_chars(X("PhiM"));
2237
2238                 _op_ProjM.code   = --num;
2239                 _op_ProjM.name   = new_id_from_chars(X("ProjM"));
2240
2241                 _op_MulC.code    = --num;
2242                 _op_MulC.name    = new_id_from_chars(X("MulC"));
2243
2244                 _op_DivC.code    = --num;
2245                 _op_DivC.name    = new_id_from_chars(X("DivC"));
2246
2247                 _op_ModC.code    = --num;
2248                 _op_ModC.name    = new_id_from_chars(X("ModC"));
2249
2250                 status->op_Phi0    = &_op_Phi0;
2251                 status->op_PhiM    = &_op_PhiM;
2252                 status->op_ProjM   = &_op_ProjM;
2253                 status->op_MulC    = &_op_MulC;
2254                 status->op_DivC    = &_op_DivC;
2255                 status->op_ModC    = &_op_ModC;
2256         } else {
2257                 status->op_Phi0    = NULL;
2258                 status->op_PhiM    = NULL;
2259                 status->op_ProjM   = NULL;
2260                 status->op_MulC    = NULL;
2261                 status->op_DivC    = NULL;
2262                 status->op_ModC    = NULL;
2263         }
2264
2265         /* for Florian: count the Sel depth */
2266         if (stat_options & FIRMSTAT_COUNT_SELS) {
2267                 _op_SelSel.code    = --num;
2268                 _op_SelSel.name    = new_id_from_chars(X("Sel(Sel)"));
2269
2270                 _op_SelSelSel.code = --num;
2271                 _op_SelSelSel.name = new_id_from_chars(X("Sel(Sel(Sel))"));
2272
2273                 status->op_SelSel    = &_op_SelSel;
2274                 status->op_SelSelSel = &_op_SelSelSel;
2275         } else {
2276                 status->op_SelSel    = NULL;
2277                 status->op_SelSelSel = NULL;
2278         }
2279
2280         /* register the dumper */
2281         stat_register_dumper(&simple_dumper);
2282
2283         if (stat_options & FIRMSTAT_CSV_OUTPUT)
2284                 stat_register_dumper(&csv_dumper);
2285
2286         /* initialize the pattern hash */
2287         stat_init_pattern_history(stat_options & FIRMSTAT_PATTERN_ENABLED);
2288
2289         /* initialize the Const options */
2290         if (stat_options & FIRMSTAT_COUNT_CONSTS)
2291                 stat_init_const_cnt(status);
2292
2293         /* distribution table for parameter counts */
2294         status->dist_param_cnt = stat_new_int_distrib_tbl();
2295
2296         clear_optimization_counter();
2297
2298 #undef HOOK
2299 #undef X
2300 }
2301
2302 /**
2303  * Frees all dumper structures.
2304  */
2305 static void stat_term_dumper(void)
2306 {
2307         dumper_t *dumper, *next_dumper;
2308
2309         for (dumper = status->dumper; dumper; /* iteration done in loop body */ ) {
2310                 if (dumper->func_map)
2311                         del_pset(dumper->func_map);
2312
2313                 next_dumper = dumper->next;
2314                 free(dumper);
2315                 dumper = next_dumper;
2316         }
2317 }
2318
2319
2320 /* Terminates the statistics module, frees all memory. */
2321 void stat_term(void)
2322 {
2323         if (status != (stat_info_t *)&status_disable) {
2324                 obstack_free(&status->be_data, NULL);
2325                 obstack_free(&status->cnts, NULL);
2326
2327                 stat_term_dumper();
2328
2329                 xfree(status);
2330                 status = (stat_info_t *)&status_disable;
2331         }
2332 }
2333
2334 /* returns 1 if statistics were initialized, 0 otherwise */
2335 int stat_is_active(void)
2336 {
2337         return status != (stat_info_t *)&status_disable;
2338 }
2339
2340 void init_stat(void)
2341 {
2342         lc_opt_entry_t *root_grp = firm_opt_get_root();
2343         lc_opt_entry_t *be_grp   = lc_opt_get_grp(root_grp, "be");
2344
2345         static const lc_opt_enum_mask_items_t stat_items[] = {
2346                 { "enabled",         FIRMSTAT_ENABLED         },
2347                 { "pattern",         FIRMSTAT_PATTERN_ENABLED },
2348                 { "count_strong_op", FIRMSTAT_COUNT_STRONG_OP },
2349                 { "count_dag",       FIRMSTAT_COUNT_DAG       },
2350                 { "count_deleted",   FIRMSTAT_COUNT_DELETED   },
2351                 { "count_sels",      FIRMSTAT_COUNT_SELS      },
2352                 { "count_consts",    FIRMSTAT_COUNT_CONSTS    },
2353                 { "csv_output",      FIRMSTAT_CSV_OUTPUT      },
2354                 { NULL,              0 }
2355         };
2356         static lc_opt_enum_mask_var_t statmask = { &stat_options, stat_items };
2357         static const lc_opt_table_entry_t stat_optionstable[] = {
2358                 LC_OPT_ENT_ENUM_MASK("statistics", "enable statistics",   &statmask),
2359                 LC_OPT_LAST
2360         };
2361         lc_opt_add_table(be_grp, stat_optionstable);
2362 }