calling convetions are now read from the method type
[libfirm] / ir / stat / firmstat.c
1 /*
2  * Project:     libFIRM
3  * File name:   ir/ir/firmstat.c
4  * Purpose:     Statistics for Firm.
5  * Author:      Michael Beck
6  * Created:
7  * CVS-ID:      $Id$
8  * Copyright:   (c) 2004 Universität Karlsruhe
9  * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
10  */
11
12 #ifdef HAVE_CONFIG_H
13 # include "config.h"
14 #endif
15
16 #ifdef FIRM_STATISTICS
17
18 #include <stdio.h>
19
20 #ifdef HAVE_STDLIB_H
21 # include <stdlib.h>
22 #endif
23 #ifdef HAVE_STRING_H
24 # include <string.h>
25 #endif
26
27 #include "irouts.h"
28 #include "irdump.h"
29 #include "hashptr.h"
30 #include "firmstat_t.h"
31 #include "pattern.h"
32 #include "dags.h"
33 #include "stat_dmp.h"
34 #include "xmalloc.h"
35 #include "irhooks.h"
36
37 /*
38  * need this to be static:
39  * Special pseudo Opcodes that we need to count some interesting cases
40  */
41
42 /**
43  * The Phi0, a node that is created during SSA construction
44  */
45 static ir_op _op_Phi0;
46
47 /** The PhiM, just to count memory Phi's. */
48 static ir_op _op_PhiM;
49
50 /** The Mul by Const node. */
51 static ir_op _op_MulC;
52
53 /** The Div by Const node. */
54 static ir_op _op_DivC;
55
56 /** The Div by Const node. */
57 static ir_op _op_ModC;
58
59 /** The Div by Const node. */
60 static ir_op _op_DivModC;
61
62 /** The memory Proj node. */
63 static ir_op _op_ProjM;
64
65 /** A Sel of a Sel */
66 static ir_op _op_SelSel;
67
68 /** A Sel of a Sel of a Sel */
69 static ir_op _op_SelSelSel;
70
71 /* ---------------------------------------------------------------------------------- */
72
73 /** Marks the begin of a statistic (hook) function. */
74 #define STAT_ENTER              ++status->recursive
75
76 /** Marks the end of a statistic (hook) functions. */
77 #define STAT_LEAVE              --status->recursive
78
79 /** Allows to enter a statistic function only when we are not already in a hook. */
80 #define STAT_ENTER_SINGLE       do { if (status->recursive > 0) return; ++status->recursive; } while (0)
81
82 /**
83  * global status
84  */
85 static const int status_disable = 0;
86 static stat_info_t *status = (stat_info_t *)&status_disable;
87
88 /**
89  * compare two elements of the opcode hash
90  */
91 static int opcode_cmp(const void *elt, const void *key)
92 {
93   const node_entry_t *e1 = elt;
94   const node_entry_t *e2 = key;
95
96   return e1->op->code - e2->op->code;
97 }
98
99 /**
100  * compare two elements of the graph hash
101  */
102 static int graph_cmp(const void *elt, const void *key)
103 {
104   const graph_entry_t *e1 = elt;
105   const graph_entry_t *e2 = key;
106
107   return e1->irg != e2->irg;
108 }
109
110 /**
111  * compare two elements of the optimization hash
112  */
113 static int opt_cmp(const void *elt, const void *key)
114 {
115   const opt_entry_t *e1 = elt;
116   const opt_entry_t *e2 = key;
117
118   return e1->op->code != e2->op->code;
119 }
120
121 /**
122  * compare two elements of the block/extbb hash
123  */
124 static int block_cmp(const void *elt, const void *key)
125 {
126   const block_entry_t *e1 = elt;
127   const block_entry_t *e2 = key;
128
129   return e1->block_nr != e2->block_nr;
130 }
131
132 /**
133  * compare two elements of the ir_op hash
134  */
135 static int opcode_cmp_2(const void *elt, const void *key)
136 {
137   const ir_op *e1 = elt;
138   const ir_op *e2 = key;
139
140   return e1->code != e2->code;
141 }
142
143 /**
144  * compare two elements of the address_mark set
145  */
146 static int address_mark_cmp(const void *elt, const void *key, size_t size)
147 {
148   const address_mark_entry_t *e1 = elt;
149   const address_mark_entry_t *e2 = key;
150
151   /* compare only the nodes, the rest is used as data container */
152   return e1->node != e2->node;
153 }
154
155 /**
156  * clears all counter in a node_entry_t
157  */
158 static void opcode_clear_entry(node_entry_t *elem)
159 {
160   cnt_clr(&elem->cnt_alive);
161   cnt_clr(&elem->new_node);
162   cnt_clr(&elem->into_Id);
163 }
164
165 /**
166  * Returns the associates node_entry_t for an ir_op
167  *
168  * @param op    the IR operation
169  * @param hmap  a hash map containing ir_op* -> node_entry_t*
170  */
171 static node_entry_t *opcode_get_entry(const ir_op *op, hmap_node_entry_t *hmap)
172 {
173   node_entry_t key;
174   node_entry_t *elem;
175
176   key.op = op;
177
178   elem = pset_find(hmap, &key, op->code);
179   if (elem)
180     return elem;
181
182   elem = obstack_alloc(&status->cnts, sizeof(*elem));
183   memset(elem, 0, sizeof(*elem));
184
185   /* clear counter */
186   opcode_clear_entry(elem);
187
188   elem->op = op;
189
190   return pset_insert(hmap, elem, op->code);
191 }
192
193 /**
194  * Returns the associates ir_op for an opcode
195  *
196  * @param code  the IR opcode
197  * @param hmap  the hash map containing opcode -> ir_op*
198  */
199 static ir_op *opcode_find_entry(opcode code, hmap_ir_op *hmap)
200 {
201   ir_op key;
202
203   key.code = code;
204   return pset_find(hmap, &key, code);
205 }
206
207 /**
208  * clears all counter in a graph_entry_t
209  */
210 static void graph_clear_entry(graph_entry_t *elem, int all)
211 {
212   if (all) {
213     cnt_clr(&elem->cnt_walked);
214     cnt_clr(&elem->cnt_walked_blocks);
215     cnt_clr(&elem->cnt_was_inlined);
216     cnt_clr(&elem->cnt_got_inlined);
217     cnt_clr(&elem->cnt_strength_red);
218     cnt_clr(&elem->cnt_real_func_call);
219   }
220   cnt_clr(&elem->cnt_edges);
221   cnt_clr(&elem->cnt_all_calls);
222   cnt_clr(&elem->cnt_call_with_cnst_arg);
223   cnt_clr(&elem->cnt_indirect_calls);
224
225   if (elem->block_hash) {
226     del_pset(elem->block_hash);
227     elem->block_hash = NULL;
228   }
229
230   if (elem->extbb_hash) {
231     del_pset(elem->extbb_hash);
232     elem->extbb_hash = NULL;
233   }
234
235   obstack_free(&elem->recalc_cnts, NULL);
236   obstack_init(&elem->recalc_cnts);
237 }
238
239 /**
240  * Returns the associated graph_entry_t for an IR graph.
241  *
242  * @param irg   the IR graph
243  * @param hmap  the hash map containing ir_graph* -> graph_entry_t*
244  */
245 static graph_entry_t *graph_get_entry(ir_graph *irg, hmap_graph_entry_t *hmap)
246 {
247   graph_entry_t key;
248   graph_entry_t *elem;
249   int i;
250
251   key.irg = irg;
252
253   elem = pset_find(hmap, &key, HASH_PTR(irg));
254   if (elem)
255     return elem;
256
257   /* allocate a new one */
258   elem = obstack_alloc(&status->cnts, sizeof(*elem));
259   memset(elem, 0, sizeof(*elem));
260   obstack_init(&elem->recalc_cnts);
261
262   /* clear counter */
263   graph_clear_entry(elem, 1);
264
265   /* new hash table for opcodes here  */
266   elem->opcode_hash  = new_pset(opcode_cmp, 5);
267   elem->address_mark = new_set(address_mark_cmp, 5);
268   elem->irg          = irg;
269
270   /* these hash tables are created on demand */
271   elem->block_hash = NULL;
272   elem->extbb_hash = NULL;
273
274   for (i = 0; i < sizeof(elem->opt_hash)/sizeof(elem->opt_hash[0]); ++i)
275     elem->opt_hash[i] = new_pset(opt_cmp, 4);
276
277   return pset_insert(hmap, elem, HASH_PTR(irg));
278 }
279
280 /**
281  * clears all counter in an opt_entry_t
282  */
283 static void opt_clear_entry(opt_entry_t *elem)
284 {
285   cnt_clr(&elem->count);
286 }
287
288 /**
289  * Returns the associated opt_entry_t for an IR operation.
290  *
291  * @param op    the IR operation
292  * @param hmap  the hash map containing ir_op* -> opt_entry_t*
293  */
294 static opt_entry_t *opt_get_entry(const ir_op *op, hmap_opt_entry_t *hmap)
295 {
296   opt_entry_t key;
297   opt_entry_t *elem;
298
299   key.op = op;
300
301   elem = pset_find(hmap, &key, op->code);
302   if (elem)
303     return elem;
304
305   elem = obstack_alloc(&status->cnts, sizeof(*elem));
306   memset(elem, 0, sizeof(*elem));
307
308   /* clear new counter */
309   opt_clear_entry(elem);
310
311   elem->op = op;
312
313   return pset_insert(hmap, elem, op->code);
314 }
315
316 /**
317  * clears all counter in a block_entry_t
318  */
319 static void block_clear_entry(block_entry_t *elem)
320 {
321   cnt_clr(&elem->cnt_nodes);
322   cnt_clr(&elem->cnt_edges);
323   cnt_clr(&elem->cnt_in_edges);
324   cnt_clr(&elem->cnt_out_edges);
325   cnt_clr(&elem->cnt_phi_data);
326 }
327
328 /**
329  * Returns the associated block_entry_t for an block.
330  *
331  * @param block_nr  an IR  block number
332  * @param hmap      a hash map containing long -> block_entry_t
333  */
334 static block_entry_t *block_get_entry(struct obstack *obst, long block_nr, hmap_block_entry_t *hmap)
335 {
336   block_entry_t key;
337   block_entry_t *elem;
338
339   key.block_nr = block_nr;
340
341   elem = pset_find(hmap, &key, block_nr);
342   if (elem)
343     return elem;
344
345   elem = obstack_alloc(obst, sizeof(*elem));
346   memset(elem, 0, sizeof(*elem));
347
348   /* clear new counter */
349   block_clear_entry(elem);
350
351   elem->block_nr = block_nr;
352
353   return pset_insert(hmap, elem, block_nr);
354 }
355
356
357 /**
358  * Returns the ir_op for an IR-node,
359  * handles special cases and return pseudo op codes.
360  *
361  * @param none  an IR node
362  */
363 static ir_op *stat_get_irn_op(ir_node *node)
364 {
365   ir_op *op = get_irn_op(node);
366
367   if (op == op_Phi && get_irn_arity(node) == 0) {
368     /* special case, a Phi0 node, count on extra counter */
369     op = status->op_Phi0 ? status->op_Phi0 : op;
370   }
371   else if (op == op_Phi && get_irn_mode(node) == mode_M) {
372     /* special case, a Memory Phi node, count on extra counter */
373     op = status->op_PhiM ? status->op_PhiM : op;
374   }
375   else if (op == op_Proj && get_irn_mode(node) == mode_M) {
376     /* special case, a Memory Proj node, count on extra counter */
377     op = status->op_ProjM ? status->op_ProjM : op;
378   }
379   else if (op == op_Mul &&
380            (get_irn_op(get_Mul_left(node)) == op_Const || get_irn_op(get_Mul_right(node)) == op_Const)) {
381     /* special case, a Multiply by a const, count on extra counter */
382     op = status->op_MulC ? status->op_MulC : op;
383   }
384   else if (op == op_Div && get_irn_op(get_Div_right(node)) == op_Const) {
385     /* special case, a division by a const, count on extra counter */
386     op = status->op_DivC ? status->op_DivC : op;
387   }
388   else if (op == op_Mod && get_irn_op(get_Mod_right(node)) == op_Const) {
389     /* special case, a module by a const, count on extra counter */
390     op = status->op_ModC ? status->op_ModC : op;
391   }
392   else if (op == op_DivMod && get_irn_op(get_DivMod_right(node)) == op_Const) {
393     /* special case, a division/modulo by a const, count on extra counter */
394     op = status->op_DivModC ? status->op_DivModC : op;
395   }
396   else if (op == op_Sel && get_irn_op(get_Sel_ptr(node)) == op_Sel) {
397     /* special case, a Sel of a Sel, count on extra counter */
398     op = status->op_SelSel ? status->op_SelSel : op;
399
400     if (get_irn_op(get_Sel_ptr(get_Sel_ptr(node))) == op_Sel) {
401       /* special case, a Sel of a Sel of a Sel, count on extra counter */
402       op = status->op_SelSelSel ? status->op_SelSelSel : op;
403     }
404   }
405
406   return op;
407 }
408
409 /**
410  * update the block counter
411  */
412 static void undate_block_info(ir_node *node, graph_entry_t *graph)
413 {
414   ir_op *op = get_irn_op(node);
415   ir_node *block;
416   block_entry_t *b_entry;
417   int i, arity;
418
419   /* check for block */
420   if (op == op_Block) {
421     arity = get_irn_arity(node);
422     b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(node), graph->block_hash);
423
424     /* count all incoming edges */
425     for (i = 0; i < arity; ++i) {
426       ir_node *pred = get_irn_n(node, i);
427       ir_node *other_block = get_nodes_block(pred);
428       block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
429
430       cnt_inc(&b_entry->cnt_in_edges);  /* an edge coming from another block */
431       cnt_inc(&b_entry_other->cnt_out_edges);
432     }
433     return;
434   }
435
436   block   = get_nodes_block(node);
437   b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(block), graph->block_hash);
438
439   if (op == op_Phi && mode_is_datab(get_irn_mode(node))) {
440     /* count data Phi per block */
441     cnt_inc(&b_entry->cnt_phi_data);
442   }
443
444   /* we have a new node in our block */
445   cnt_inc(&b_entry->cnt_nodes);
446
447   /* don't count keep-alive edges */
448   if (get_irn_op(node) == op_End)
449     return;
450
451   arity = get_irn_arity(node);
452
453   for (i = 0; i < arity; ++i) {
454     ir_node *pred = get_irn_n(node, i);
455     ir_node *other_block;
456
457     other_block = get_nodes_block(pred);
458
459     if (other_block == block)
460       cnt_inc(&b_entry->cnt_edges);     /* a in block edge */
461     else {
462       block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
463
464       cnt_inc(&b_entry->cnt_in_edges);  /* an edge coming from another block */
465       cnt_inc(&b_entry_other->cnt_out_edges);
466     }
467   }
468 }
469
470 /**
471  * update the extended block counter
472  */
473 static void undate_extbb_info(ir_node *node, graph_entry_t *graph)
474 {
475   ir_op *op = get_irn_op(node);
476   ir_extblk *extbb;
477   extbb_entry_t *eb_entry;
478   int i, arity;
479
480   /* check for block */
481   if (op == op_Block) {
482     extbb = get_nodes_extbb(node);
483     arity = get_irn_arity(node);
484     eb_entry = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(extbb), graph->extbb_hash);
485
486     /* count all incoming edges */
487     for (i = 0; i < arity; ++i) {
488       ir_node *pred = get_irn_n(node, i);
489       ir_extblk *other_extbb = get_nodes_extbb(pred);
490
491       if (extbb != other_extbb) {
492         extbb_entry_t *eb_entry_other = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(other_extbb), graph->extbb_hash);
493
494         cnt_inc(&eb_entry->cnt_in_edges);       /* an edge coming from another extbb */
495         cnt_inc(&eb_entry_other->cnt_out_edges);
496       }
497     }
498     return;
499   }
500
501   extbb    = get_nodes_extbb(node);
502   eb_entry = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(extbb), graph->extbb_hash);
503
504   if (op == op_Phi && mode_is_datab(get_irn_mode(node))) {
505     /* count data Phi per extbb */
506     cnt_inc(&eb_entry->cnt_phi_data);
507   }
508
509   /* we have a new node in our block */
510   cnt_inc(&eb_entry->cnt_nodes);
511
512   /* don't count keep-alive edges */
513   if (get_irn_op(node) == op_End)
514     return;
515
516   arity = get_irn_arity(node);
517
518   for (i = 0; i < arity; ++i) {
519     ir_node *pred = get_irn_n(node, i);
520     ir_extblk *other_extbb = get_nodes_extbb(pred);
521
522     if (other_extbb == extbb)
523       cnt_inc(&eb_entry->cnt_edges);    /* a in extbb edge */
524     else {
525       extbb_entry_t *eb_entry_other = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(other_extbb), graph->extbb_hash);
526
527       cnt_inc(&eb_entry->cnt_in_edges); /* an edge coming from another extbb */
528       cnt_inc(&eb_entry_other->cnt_out_edges);
529     }
530   }
531 }
532
533 /** calculates how many arguments of the call are const */
534 static int cnt_const_args(ir_node *call)
535 {
536   int  i, res = 0;
537   int  n = get_Call_n_params(call);
538
539   for (i = 0; i < n; ++i) {
540     ir_node *param = get_Call_param(call, i);
541     ir_op   *op = get_irn_op(param);
542
543     if (op == op_Const || op == op_SymConst)
544       ++res;
545   }
546   return res;
547 }
548
549 /**
550  * update info on calls
551  *
552  * @param call   The call
553  * @param graph  The graph entry containing the call
554  */
555 static void stat_update_call(ir_node *call, graph_entry_t *graph)
556 {
557   ir_node  *block = get_nodes_block(call);
558   ir_node  *ptr = get_Call_ptr(call);
559   entity   *ent = NULL;
560   ir_graph *callee = NULL;
561   int      num_const_args;
562
563   /*
564    * If the block is bad, the whole subgraph will collapse later
565    * so do not count this call.
566    * This happens in dead code.
567    */
568   if (is_Bad(block))
569     return;
570
571   cnt_inc(&graph->cnt_all_calls);
572
573   /* found a call, this function is not a leaf */
574   graph->is_leaf = 0;
575
576   if (get_irn_op(ptr) == op_SymConst) {
577     if (get_SymConst_kind(ptr) == symconst_addr_ent) {
578       /* ok, we seems to know the entity */
579       ent = get_SymConst_entity(ptr);
580       callee = get_entity_irg(ent);
581
582       /* it is recursive, if it calls at least once */
583       if (callee == graph->irg)
584         graph->is_recursive = 1;
585     }
586   }
587   else {
588     /* indirect call, be could not predict */
589     cnt_inc(&graph->cnt_indirect_calls);
590
591     /* NOT a leaf call */
592     graph->is_leaf_call = LCS_NON_LEAF_CALL;
593   }
594
595   /* check, if it's a chain-call: Then, the call-block
596    * must dominate the end block. */
597   {
598     ir_node *curr = get_irg_end_block(graph->irg);
599     int depth = get_Block_dom_depth(block);
600
601     for (; curr != block && get_Block_dom_depth(curr) > depth;) {
602       curr = get_Block_idom(curr);
603
604       if (! curr || is_no_Block(curr))
605         break;
606     }
607
608     if (curr != block)
609       graph->is_chain_call = 0;
610   }
611
612   /* check, if the callee is a leaf */
613   if (callee) {
614     graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
615
616     if (called->is_analyzed) {
617       if (! called->is_leaf)
618         graph->is_leaf_call = LCS_NON_LEAF_CALL;
619     }
620   }
621
622   /* check, if arguments of the call are const */
623   num_const_args = cnt_const_args(call);
624
625   if (num_const_args > 0)
626     cnt_inc(&graph->cnt_call_with_cnst_arg);
627 }
628
629 /**
630  * update info on calls for graphs on the wait queue
631  */
632 static void stat_update_call_2(ir_node *call, graph_entry_t *graph)
633 {
634   ir_node  *block = get_nodes_block(call);
635   ir_node  *ptr = get_Call_ptr(call);
636   entity   *ent = NULL;
637   ir_graph *callee = NULL;
638
639   /*
640    * If the block is bad, the whole subgraph will collapse later
641    * so do not count this call.
642    * This happens in dead code.
643    */
644   if (is_Bad(block))
645     return;
646
647   if (get_irn_op(ptr) == op_SymConst) {
648     if (get_SymConst_kind(ptr) == symconst_addr_ent) {
649       /* ok, we seems to know the entity */
650       ent = get_SymConst_entity(ptr);
651       callee = get_entity_irg(ent);
652     }
653   }
654
655   /* check, if the callee is a leaf */
656   if (callee) {
657     graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
658
659     assert(called->is_analyzed);
660
661     if (! called->is_leaf)
662       graph->is_leaf_call = LCS_NON_LEAF_CALL;
663   }
664   else
665     graph->is_leaf_call = LCS_NON_LEAF_CALL;
666 }
667
668 /**
669  * walker for reachable nodes count
670  */
671 static void update_node_stat(ir_node *node, void *env)
672 {
673   graph_entry_t *graph = env;
674   node_entry_t *entry;
675
676   ir_op *op = stat_get_irn_op(node);
677   int arity = get_irn_arity(node);
678
679   entry = opcode_get_entry(op, graph->opcode_hash);
680
681   cnt_inc(&entry->cnt_alive);
682   cnt_add_i(&graph->cnt_edges, arity);
683
684   /* count block edges */
685   undate_block_info(node, graph);
686
687   /* count extended block edges */
688   if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
689     undate_extbb_info(node, graph);
690   }
691
692   /* handle statistics for special node types */
693
694   if (op == op_Const) {
695     if (status->stat_options & FIRMSTAT_COUNT_CONSTS) {
696       /* check properties of constants */
697       stat_update_const(status, node, graph);
698     }
699   }
700   else if (op == op_Call) {
701     /* check for properties that depends on calls like recursion/leaf/indirect call */
702     stat_update_call(node, graph);
703   }
704 }
705
706 /**
707  * walker for reachable nodes count for graphs on the wait_q
708  */
709 static void update_node_stat_2(ir_node *node, void *env)
710 {
711   graph_entry_t *graph = env;
712
713   /* check for properties that depends on calls like recursion/leaf/indirect call */
714   if (get_irn_op(node) == op_Call)
715     stat_update_call_2(node, graph);
716 }
717
718 /**
719  * get the current address mark
720  */
721 static unsigned get_adr_mark(graph_entry_t *graph, ir_node *node)
722 {
723   address_mark_entry_t *value = set_find(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
724
725   return value ? value->mark : 0;
726 }
727
728 /**
729  * set the current address mark
730  */
731 static void set_adr_mark(graph_entry_t *graph, ir_node *node, unsigned val)
732 {
733   address_mark_entry_t *value = set_insert(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
734
735   value->mark = val;
736 }
737
738 /**
739  * a vcg attribute hook: Color a node with a different color if
740  * it's identified as a part of an address expression or at least referenced
741  * by an address expression.
742  */
743 static int stat_adr_mark_hook(FILE *F, ir_node *node, ir_node *local)
744 {
745   ir_node *n           = local ? local : node;
746   ir_graph *irg        = get_irn_irg(n);
747   graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
748   unsigned mark        = get_adr_mark(graph, n);
749
750   if (mark & MARK_ADDRESS_CALC)
751     fprintf(F, "color: purple");
752   else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
753     fprintf(F, "color: pink");
754   else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
755     fprintf(F, "color: lightblue");
756   else
757     return 0;
758
759   /* I know the color! */
760   return 1;
761 }
762
763 /**
764  * walker that marks every node that is an address calculation
765  *
766  * predecessor nodes must be visited first. We ensure this by
767  * calling in in the post of an outs walk. This should work even in cycles,
768  * while the pre in a normal walk will not.
769  */
770 static void mark_address_calc(ir_node *node, void *env)
771 {
772   graph_entry_t *graph = env;
773   ir_mode *mode = get_irn_mode(node);
774   int i, n;
775   unsigned mark_preds = MARK_REF_NON_ADR;
776
777   if (! mode_is_numP(mode))
778     return;
779
780   if (mode_is_reference(mode)) {
781     /* a reference is calculated here, we are sure */
782     set_adr_mark(graph, node, MARK_ADDRESS_CALC);
783
784     mark_preds = MARK_REF_ADR;
785   }
786   else {
787     unsigned mark = get_adr_mark(graph, node);
788
789     if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR) {
790       /*
791        * this node has not an reference mode, but is only
792        * referenced by address calculations
793        */
794       mark_preds = MARK_REF_ADR;
795     }
796   }
797
798   /* mark all predecessors */
799   for (i = 0, n = get_irn_arity(node); i < n; ++i) {
800     ir_node *pred = get_irn_n(node, i);
801
802     set_adr_mark(graph, pred, get_adr_mark(graph, pred) | mark_preds);
803   }
804 }
805
806 /**
807  * Called for every graph when the graph is either deleted or stat_dump_snapshot()
808  * is called, must recalculate all statistic info.
809  *
810  * @param global    The global entry
811  * @param graph     The current entry
812  */
813 static void update_graph_stat(graph_entry_t *global, graph_entry_t *graph)
814 {
815   node_entry_t *entry;
816
817   /* clear first the alive counter in the graph */
818   for (entry = pset_first(graph->opcode_hash); entry; entry = pset_next(graph->opcode_hash)) {
819     cnt_clr(&entry->cnt_alive);
820   }
821
822   /* set pessimistic values */
823   graph->is_leaf       = 1;
824   graph->is_leaf_call  = LCS_UNKNOWN;
825   graph->is_recursive  = 0;
826   graph->is_chain_call = 1;
827
828   /* create new block counter */
829   graph->block_hash = new_pset(block_cmp, 5);
830
831   /* we need dominator info */
832   if (graph->irg != get_const_code_irg())
833     if (get_irg_dom_state(graph->irg) != dom_consistent)
834       compute_doms(graph->irg);
835
836   if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
837     /* we need extended basic blocks */
838     compute_extbb(graph->irg);
839
840     /* create new extbb counter */
841     graph->extbb_hash = new_pset(block_cmp, 5);
842   }
843
844   /* count the nodes in the graph */
845   irg_walk_graph(graph->irg, update_node_stat, NULL, graph);
846
847 #if 0
848   /* Uncomment this code if chain-call means call exact one */
849   entry = opcode_get_entry(op_Call, graph->opcode_hash);
850
851   /* check if we have more than 1 call */
852   if (cnt_gt(entry->cnt_alive, 1))
853     graph->is_chain_call = 0;
854 #endif
855
856   /* recursive functions are never chain calls, leafs don't have calls */
857   if (graph->is_recursive || graph->is_leaf)
858     graph->is_chain_call = 0;
859
860   /* assume we walk every graph only ONCE, we could sum here the global count */
861   for (entry = pset_first(graph->opcode_hash); entry; entry = pset_next(graph->opcode_hash)) {
862     node_entry_t *g_entry = opcode_get_entry(entry->op, global->opcode_hash);
863
864     /* update the node counter */
865     cnt_add(&g_entry->cnt_alive, &entry->cnt_alive);
866   }
867
868   /* update the edge counter */
869   cnt_add(&global->cnt_edges, &graph->cnt_edges);
870
871   /* count the number of address calculation */
872   if (graph->irg != get_const_code_irg()) {
873     ir_graph *rem = current_ir_graph;
874
875     if (get_irg_outs_state(graph->irg) != outs_consistent)
876       compute_irg_outs(graph->irg);
877
878     /* Must be done an the outs graph */
879     current_ir_graph = graph->irg;
880     irg_out_walk(get_irg_start(graph->irg), NULL, mark_address_calc, graph);
881     current_ir_graph = rem;
882
883 #if 0
884     set_dump_node_vcgattr_hook(stat_adr_mark_hook);
885     dump_ir_block_graph(graph->irg, "-adr");
886     set_dump_node_vcgattr_hook(NULL);
887 #endif
888   }
889
890   /* count the DAG's */
891   if (status->stat_options & FIRMSTAT_COUNT_DAG)
892     count_dags_in_graph(global, graph);
893
894   /* calculate the patterns of this graph */
895   stat_calc_pattern_history(graph->irg);
896
897   /* leaf function did not call others */
898   if (graph->is_leaf)
899     graph->is_leaf_call = LCS_NON_LEAF_CALL;
900   else if (graph->is_leaf_call == LCS_UNKNOWN) {
901     /* we still don't know if this graph calls leaf-functions, so enqueue */
902     pdeq_putl(status->wait_q, graph);
903   }
904
905   /* we have analyzed this graph */
906   graph->is_analyzed = 1;
907 }
908
909 /**
910  * Called for every graph that was on the wait_q in stat_dump_snapshot()
911  *  must finish all statistic info calculations.
912  *
913  * @param global    The global entry
914  * @param graph     The current entry
915  */
916 static void update_graph_stat_2(graph_entry_t *global, graph_entry_t *graph)
917 {
918   if (graph->is_deleted) {
919     /* deleted, ignore */
920     return;
921   }
922
923   if (graph->irg) {
924     /* count the nodes in the graph */
925     irg_walk_graph(graph->irg, update_node_stat_2, NULL, graph);
926
927     if (graph->is_leaf_call == LCS_UNKNOWN)
928       graph->is_leaf_call = LCS_LEAF_CALL;
929   }
930 }
931
932 /**
933  * register a dumper
934  */
935 static void stat_register_dumper(const dumper_t *dumper)
936 {
937   dumper_t *p = xmalloc(sizeof(*p));
938
939   if (p) {
940     *p = *dumper;
941
942     p->next        = status->dumper;
943     p->status      = status;
944     status->dumper = p;
945   }
946
947   /* FIXME: memory leak */
948 }
949
950 /**
951  * dumps an IR graph.
952  */
953 static void stat_dump_graph(graph_entry_t *entry)
954 {
955   dumper_t *dumper;
956
957   for (dumper = status->dumper; dumper; dumper = dumper->next) {
958     if (dumper->dump_graph)
959       dumper->dump_graph(dumper, entry);
960   }
961 }
962
963 /**
964  * dumps a constant table
965  */
966 static void stat_dump_consts(const constant_info_t *tbl)
967 {
968   dumper_t *dumper;
969
970   for (dumper = status->dumper; dumper; dumper = dumper->next) {
971     if (dumper->dump_const_tbl)
972       dumper->dump_const_tbl(dumper, tbl);
973   }
974 }
975
976 /**
977  * initialize the dumper
978  */
979 static void stat_dump_init(const char *name)
980 {
981   dumper_t *dumper;
982
983   for (dumper = status->dumper; dumper; dumper = dumper->next) {
984     if (dumper->init)
985       dumper->init(dumper, name);
986   }
987 }
988
989 /**
990  * finish the dumper
991  */
992 static void stat_dump_finish(void)
993 {
994   dumper_t *dumper;
995
996   for (dumper = status->dumper; dumper; dumper = dumper->next) {
997     if (dumper->finish)
998       dumper->finish(dumper);
999   }
1000 }
1001
1002 /* ---------------------------------------------------------------------- */
1003
1004 /*
1005  * helper: get an ir_op from an opcode
1006  */
1007 ir_op *stat_get_op_from_opcode(opcode code)
1008 {
1009   return opcode_find_entry(code, status->ir_op_hash);
1010 }
1011
1012 /**
1013  * A new IR op is registered.
1014  *
1015  * @param ctx  the hook context
1016  * @param op   the new IR opcode that was created.
1017  */
1018 static void stat_new_ir_op(void *ctx, ir_op *op)
1019 {
1020   if (! status->stat_options)
1021     return;
1022
1023   STAT_ENTER;
1024   {
1025     graph_entry_t *graph = graph_get_entry(NULL, status->irg_hash);
1026
1027     /* execute for side effect :-) */
1028     opcode_get_entry(op, graph->opcode_hash);
1029
1030     pset_insert(status->ir_op_hash, op, op->code);
1031   }
1032   STAT_LEAVE;
1033 }
1034
1035 /**
1036  * An IR op is freed.
1037  *
1038  * @param ctx  the hook context
1039  * @param op   the IR opcode that is freed
1040  */
1041 static void stat_free_ir_op(void *ctx, ir_op *op)
1042 {
1043   if (! status->stat_options)
1044     return;
1045
1046   STAT_ENTER;
1047   {
1048   }
1049   STAT_LEAVE;
1050 }
1051
1052 /**
1053  * A new node is created.
1054  *
1055  * @param ctx   the hook context
1056  * @param irg   the IR graph on which the node is created
1057  * @param node  the new IR node that was created
1058  */
1059 static void stat_new_node(void *ctx, ir_graph *irg, ir_node *node)
1060 {
1061   if (! status->stat_options)
1062     return;
1063
1064   /* do NOT count during dead node elimination */
1065   if (status->in_dead_node_elim > 0)
1066     return;
1067
1068   STAT_ENTER;
1069   {
1070     node_entry_t *entry;
1071     graph_entry_t *graph;
1072     ir_op *op = stat_get_irn_op(node);
1073
1074     /* increase global value */
1075     graph = graph_get_entry(NULL, status->irg_hash);
1076     entry = opcode_get_entry(op, graph->opcode_hash);
1077     cnt_inc(&entry->new_node);
1078
1079     /* increase local value */
1080     graph = graph_get_entry(current_ir_graph, status->irg_hash);
1081     entry = opcode_get_entry(op, graph->opcode_hash);
1082     cnt_inc(&entry->new_node);
1083   }
1084   STAT_LEAVE;
1085 }
1086
1087 /**
1088  * A node is changed into a Id node
1089  *
1090  * @param ctx   the hook context
1091  * @param node  the IR node that will be turned into an ID
1092  */
1093 static void stat_turn_into_id(void *ctx, ir_node *node)
1094 {
1095   if (! status->stat_options)
1096     return;
1097
1098   STAT_ENTER;
1099   {
1100     node_entry_t *entry;
1101     graph_entry_t *graph;
1102     ir_op *op = stat_get_irn_op(node);
1103
1104     /* increase global value */
1105     graph = graph_get_entry(NULL, status->irg_hash);
1106     entry = opcode_get_entry(op, graph->opcode_hash);
1107     cnt_inc(&entry->into_Id);
1108
1109     /* increase local value */
1110     graph = graph_get_entry(current_ir_graph, status->irg_hash);
1111     entry = opcode_get_entry(op, graph->opcode_hash);
1112     cnt_inc(&entry->into_Id);
1113   }
1114   STAT_LEAVE;
1115 }
1116
1117 /**
1118  * A new graph was created
1119  *
1120  * @param ctx  the hook context
1121  * @param irg  the new IR graph that was created
1122  * @param ent  the entity of this graph
1123  */
1124 static void stat_new_graph(void *ctx, ir_graph *irg, entity *ent)
1125 {
1126   if (! status->stat_options)
1127     return;
1128
1129   STAT_ENTER;
1130   {
1131     /* execute for side effect :-) */
1132     graph_entry_t * graph = graph_get_entry(irg, status->irg_hash);
1133
1134     graph->ent           = ent;
1135     graph->is_deleted    = 0;
1136     graph->is_leaf       = 0;
1137     graph->is_leaf_call  = 0;
1138     graph->is_recursive  = 0;
1139     graph->is_chain_call = 0;
1140     graph->is_analyzed   = 0;
1141   }
1142   STAT_LEAVE;
1143 }
1144
1145 /**
1146  * A graph will be deleted
1147  *
1148  * @param ctx  the hook context
1149  * @param irg  the IR graph that will be deleted
1150  *
1151  * Note that we still hold the information for this graph
1152  * in our hash maps, only a flag is set which prevents this
1153  * information from being changed, it's "frozen" from now.
1154  */
1155 static void stat_free_graph(void *ctx, ir_graph *irg)
1156 {
1157   if (! status->stat_options)
1158     return;
1159
1160   STAT_ENTER;
1161   {
1162     graph_entry_t *graph  = graph_get_entry(irg, status->irg_hash);
1163     graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1164
1165     graph->is_deleted = 1;
1166
1167     if (status->stat_options & FIRMSTAT_COUNT_DELETED) {
1168       /* count the nodes of the graph yet, it will be destroyed later */
1169       update_graph_stat(global, graph);
1170     }
1171   }
1172   STAT_LEAVE;
1173 }
1174
1175 /**
1176  * A walk over a graph is initiated. Do not count walks from statistic code.
1177  *
1178  * @param ctx  the hook context
1179  * @param irg  the IR graph that will be walked
1180  * @param pre  the pre walker
1181  * @param post the post walker
1182  */
1183 static void stat_irg_walk(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1184 {
1185   if (! status->stat_options)
1186     return;
1187
1188   STAT_ENTER_SINGLE;
1189   {
1190     graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1191
1192     cnt_inc(&graph->cnt_walked);
1193   }
1194   STAT_LEAVE;
1195 }
1196
1197 /**
1198  * A walk over a graph in block-wise order is initiated. Do not count walks from statistic code.
1199  *
1200  * @param ctx  the hook context
1201  * @param irg  the IR graph that will be walked
1202  * @param pre  the pre walker
1203  * @param post the post walker
1204  */
1205 static void stat_irg_walk_blkwise(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1206 {
1207   /* for now, do NOT differentiate between blockwise and normal */
1208   stat_irg_walk(ctx, irg, pre, post);
1209 }
1210
1211 /**
1212  * A walk over the graph's blocks is initiated. Do not count walks from statistic code.
1213  *
1214  * @param ctx  the hook context
1215  * @param irg  the IR graph that will be walked
1216  * @param node the IR node
1217  * @param pre  the pre walker
1218  * @param post the post walker
1219  */
1220 static void stat_irg_block_walk(void *ctx, ir_graph *irg, ir_node *node, generic_func *pre, generic_func *post)
1221 {
1222   if (! status->stat_options)
1223     return;
1224
1225   STAT_ENTER_SINGLE;
1226   {
1227     graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1228
1229     cnt_inc(&graph->cnt_walked_blocks);
1230   }
1231   STAT_LEAVE;
1232 }
1233
1234 /**
1235  * called for every node that is removed due to an optimization.
1236  *
1237  * @param n     the IR node that will be removed
1238  * @param hmap  the hash map containing ir_op* -> opt_entry_t*
1239  */
1240 static void removed_due_opt(ir_node *n, hmap_opt_entry_t *hmap)
1241 {
1242   ir_op *op          = stat_get_irn_op(n);
1243   opt_entry_t *entry = opt_get_entry(op, hmap);
1244
1245   /* increase global value */
1246   cnt_inc(&entry->count);
1247 }
1248
1249 /**
1250  * Some nodes were optimized into some others due to an optimization.
1251  *
1252  * @param ctx  the hook context
1253  */
1254 static void stat_merge_nodes(
1255     void *ctx,
1256     ir_node **new_node_array, int new_num_entries,
1257     ir_node **old_node_array, int old_num_entries,
1258     hook_opt_kind opt)
1259 {
1260   if (! status->stat_options)
1261     return;
1262
1263   STAT_ENTER;
1264   {
1265     int i, j;
1266     graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1267
1268     if (status->reassoc_run)
1269       opt = HOOK_OPT_REASSOC;
1270
1271     for (i = 0; i < old_num_entries; ++i) {
1272       for (j = 0; j < new_num_entries; ++j)
1273         if (old_node_array[i] == new_node_array[j])
1274           break;
1275
1276       /* nodes might be in new and old, these are NOT removed */
1277       if (j >= new_num_entries) {
1278         int xopt = opt;
1279
1280         /* sometimes we did not detect, that it is replaced by a Const */
1281         if (opt == HOOK_OPT_CONFIRM && new_num_entries == 1) {
1282           ir_op *op = get_irn_op(new_node_array[0]);
1283
1284           if (op == op_Const || op == op_SymConst)
1285             xopt = HOOK_OPT_CONFIRM_C;
1286         }
1287
1288         removed_due_opt(old_node_array[i], graph->opt_hash[xopt]);
1289       }
1290     }
1291   }
1292   STAT_LEAVE;
1293 }
1294
1295 /**
1296  * Reassociation is started/stopped.
1297  *
1298  * @param ctx   the hook context
1299  * @param flag  if non-zero, reassociation is started else stopped
1300  */
1301 static void stat_reassociate(void *ctx, int flag)
1302 {
1303   if (! status->stat_options)
1304     return;
1305
1306   STAT_ENTER;
1307   {
1308     status->reassoc_run = flag;
1309   }
1310   STAT_LEAVE;
1311 }
1312
1313 /**
1314  * A node was lowered into other nodes
1315  *
1316  * @param ctx  the hook context
1317  * @param node the IR node that will be lowered
1318  */
1319 static void stat_lower(void *ctx, ir_node *node)
1320 {
1321   if (! status->stat_options)
1322     return;
1323
1324   STAT_ENTER;
1325   {
1326     graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1327
1328     removed_due_opt(node, graph->opt_hash[HOOK_LOWERED]);
1329   }
1330   STAT_LEAVE;
1331 }
1332
1333 /**
1334  * A graph was inlined.
1335  *
1336  * @param ctx  the hook context
1337  * @param call the IR call that will re changed into the body of
1338  *             the called IR graph
1339  * @param called_irg  the IR graph representing the called routine
1340  */
1341 static void stat_inline(void *ctx, ir_node *call, ir_graph *called_irg)
1342 {
1343   if (! status->stat_options)
1344     return;
1345
1346   STAT_ENTER;
1347   {
1348     ir_graph *irg = get_irn_irg(call);
1349     graph_entry_t *i_graph = graph_get_entry(called_irg, status->irg_hash);
1350     graph_entry_t *graph   = graph_get_entry(irg, status->irg_hash);
1351
1352     cnt_inc(&graph->cnt_got_inlined);
1353     cnt_inc(&i_graph->cnt_was_inlined);
1354   }
1355   STAT_LEAVE;
1356 }
1357
1358 /**
1359  * A graph with tail-recursions was optimized.
1360  *
1361  * @param ctx  the hook context
1362  */
1363 static void stat_tail_rec(void *ctx, ir_graph *irg, int n_calls)
1364 {
1365   if (! status->stat_options)
1366     return;
1367
1368   STAT_ENTER;
1369   {
1370     graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1371
1372     graph->num_tail_recursion += n_calls;
1373   }
1374   STAT_LEAVE;
1375 }
1376
1377 /**
1378  * Strength reduction was performed on an iteration variable.
1379  *
1380  * @param ctx  the hook context
1381  */
1382 static void stat_strength_red(void *ctx, ir_graph *irg, ir_node *strong, ir_node *cmp)
1383 {
1384   if (! status->stat_options)
1385     return;
1386
1387   STAT_ENTER;
1388   {
1389     graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1390     cnt_inc(&graph->cnt_strength_red);
1391
1392     removed_due_opt(strong, graph->opt_hash[HOOK_OPT_STRENGTH_RED]);
1393   }
1394   STAT_LEAVE;
1395 }
1396
1397 /**
1398  * Start/Stop the dead node elimination.
1399  *
1400  * @param ctx  the hook context
1401  */
1402 static void stat_dead_node_elim(void *ctx, ir_graph *irg, int start)
1403 {
1404   if (! status->stat_options)
1405     return;
1406
1407   if (start)
1408     ++status->in_dead_node_elim;
1409   else
1410     --status->in_dead_node_elim;
1411 }
1412
1413 /**
1414  * if-conversion was tried
1415  */
1416 static void stat_if_conversion(void *context, ir_graph *irg, ir_node *phi,
1417                                int pos, ir_node *mux, if_result_t reason)
1418 {
1419   if (! status->stat_options)
1420     return;
1421
1422   STAT_ENTER;
1423   {
1424     graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1425
1426     cnt_inc(&graph->cnt_if_conv[reason]);
1427   }
1428   STAT_LEAVE;
1429 }
1430
1431 /**
1432  * real function call was optimized
1433  */
1434 static void stat_func_call(void *context, ir_graph *irg, ir_node *call)
1435 {
1436   if (! status->stat_options)
1437     return;
1438
1439   STAT_ENTER;
1440   {
1441     graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1442
1443     cnt_inc(&graph->cnt_real_func_call);
1444   }
1445   STAT_LEAVE;
1446 }
1447
1448 /**
1449  * A multiply was replaced by a series of Shifts/Adds/Subs
1450  *
1451  * @param ctx  the hook context
1452  */
1453 static void stat_arch_dep_replace_mul_with_shifts(void *ctx, ir_node *mul)
1454 {
1455   if (! status->stat_options)
1456     return;
1457
1458   STAT_ENTER;
1459   {
1460     graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1461     removed_due_opt(mul, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1462   }
1463   STAT_LEAVE;
1464 }
1465
1466 /**
1467  * A division by const was replaced
1468  *
1469  * @param ctx   the hook context
1470  * @param node  the division node that will be optimized
1471  */
1472 static void stat_arch_dep_replace_division_by_const(void *ctx, ir_node *node)
1473 {
1474   if (! status->stat_options)
1475     return;
1476
1477   STAT_ENTER;
1478   {
1479     graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1480     removed_due_opt(node, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1481   }
1482   STAT_LEAVE;
1483 }
1484
1485 /* Dumps a statistics snapshot */
1486 void stat_dump_snapshot(const char *name, const char *phase)
1487 {
1488   char fname[2048];
1489   const char *p;
1490   int l;
1491
1492   if (! status->stat_options)
1493     return;
1494
1495   STAT_ENTER;
1496   {
1497     graph_entry_t *entry;
1498     graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1499
1500     /*
1501      * The constant counter is only global, so we clear it here.
1502      * Note that it does NOT contain the constants in DELETED
1503      * graphs due to this.
1504      */
1505     if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
1506       stat_const_clear(status);
1507
1508     /* build the name */
1509     p = strrchr(name, '/');
1510 #ifdef _WIN32
1511     {
1512       const char *q;
1513
1514       q = strrchr(name, '\\');
1515
1516       /* NULL might be not the smallest pointer */
1517       if (q && (!p || q > p))
1518         p = q;
1519     }
1520 #endif
1521     if (p) {
1522       ++p;
1523       l = p - name;
1524
1525       if (l > sizeof(fname) - 1)
1526         l = sizeof(fname) - 1;
1527
1528       memcpy(fname, name, l);
1529       fname[l] = '\0';
1530     }
1531     else {
1532       fname[0] = '\0';
1533       p = name;
1534     }
1535     strncat(fname, "firmstat-", sizeof(fname));
1536     strncat(fname, phase, sizeof(fname));
1537     strncat(fname, "-", sizeof(fname));
1538     strncat(fname, p, sizeof(fname));
1539
1540     stat_dump_init(fname);
1541
1542     /* calculate the graph statistics */
1543     for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
1544
1545       if (entry->irg == NULL) {
1546         /* special entry for the global count */
1547         continue;
1548       }
1549
1550       if (! entry->is_deleted) {
1551         /* the graph is still alive, count the nodes on it */
1552         update_graph_stat(global, entry);
1553       }
1554     }
1555
1556     /* some calculations are dependent, we pushed them on the wait_q */
1557     while (! pdeq_empty(status->wait_q)) {
1558       entry = pdeq_getr(status->wait_q);
1559
1560       update_graph_stat_2(global, entry);
1561     }
1562
1563
1564     /* dump per graph */
1565     for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
1566
1567       if (entry->irg == NULL) {
1568         /* special entry for the global count */
1569         continue;
1570       }
1571
1572       if (! entry->is_deleted || status->stat_options & FIRMSTAT_COUNT_DELETED) {
1573         stat_dump_graph(entry);
1574       }
1575
1576       if (! entry->is_deleted) {
1577         /* clear the counter that are not accumulated */
1578         graph_clear_entry(entry, 0);
1579       }
1580     }
1581
1582     /* dump global */
1583     stat_dump_graph(global);
1584
1585     /* dump the const info */
1586     if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
1587       stat_dump_consts(&status->const_info);
1588
1589     stat_dump_finish();
1590
1591     stat_finish_pattern_history();
1592
1593     /* clear the global counter here */
1594     {
1595       node_entry_t *entry;
1596
1597       for (entry = pset_first(global->opcode_hash); entry; entry = pset_next(global->opcode_hash)) {
1598         opcode_clear_entry(entry);
1599       }
1600       /* clear all global counter */
1601       graph_clear_entry(global, 1);
1602     }
1603   }
1604   STAT_LEAVE;
1605 }
1606
1607 /** the hook entries for the Firm statistics module */
1608 static hook_entry_t stat_hooks[hook_last];
1609
1610 /* initialize the statistics module. */
1611 void init_stat(unsigned enable_options)
1612 {
1613 #define X(a)  a, sizeof(a)-1
1614 #define HOOK(h, fkt) \
1615   stat_hooks[h].hook._##h = fkt; register_hook(h, &stat_hooks[h])
1616
1617   if (! (enable_options & FIRMSTAT_ENABLED))
1618     return;
1619
1620   status = xmalloc(sizeof(*status));
1621   memset(status, 0, sizeof(*status));
1622
1623   /* enable statistics */
1624   status->stat_options = enable_options & FIRMSTAT_ENABLED ? enable_options : 0;
1625
1626   /* register all hooks */
1627   HOOK(hook_new_ir_op,                          stat_new_ir_op);
1628   HOOK(hook_free_ir_op,                         stat_free_ir_op);
1629   HOOK(hook_new_node,                           stat_new_node);
1630   HOOK(hook_turn_into_id,                       stat_turn_into_id);
1631   HOOK(hook_new_graph,                          stat_new_graph);
1632   HOOK(hook_free_graph,                         stat_free_graph);
1633   HOOK(hook_irg_walk,                           stat_irg_walk);
1634   HOOK(hook_irg_walk_blkwise,                   stat_irg_walk_blkwise);
1635   HOOK(hook_irg_block_walk,                     stat_irg_block_walk);
1636   HOOK(hook_merge_nodes,                        stat_merge_nodes);
1637   HOOK(hook_reassociate,                        stat_reassociate);
1638   HOOK(hook_lower,                              stat_lower);
1639   HOOK(hook_inline,                             stat_inline);
1640   HOOK(hook_tail_rec,                           stat_tail_rec);
1641   HOOK(hook_strength_red,                       stat_strength_red);
1642   HOOK(hook_dead_node_elim,                     stat_dead_node_elim);
1643   HOOK(hook_if_conversion,                      stat_if_conversion);
1644   HOOK(hook_func_call,                          stat_func_call);
1645   HOOK(hook_arch_dep_replace_mul_with_shifts,   stat_arch_dep_replace_mul_with_shifts);
1646   HOOK(hook_arch_dep_replace_division_by_const, stat_arch_dep_replace_division_by_const);
1647
1648   obstack_init(&status->cnts);
1649
1650   /* create the hash-tables */
1651   status->irg_hash   = new_pset(graph_cmp, 8);
1652   status->ir_op_hash = new_pset(opcode_cmp_2, 1);
1653
1654   /* create the wait queue */
1655   status->wait_q     = new_pdeq();
1656
1657   if (enable_options & FIRMSTAT_COUNT_STRONG_OP) {
1658     /* build the pseudo-ops */
1659     _op_Phi0.code    = get_next_ir_opcode();
1660     _op_Phi0.name    = new_id_from_chars(X("Phi0"));
1661
1662     _op_PhiM.code    = get_next_ir_opcode();
1663     _op_PhiM.name    = new_id_from_chars(X("PhiM"));
1664
1665     _op_ProjM.code   = get_next_ir_opcode();
1666     _op_ProjM.name   = new_id_from_chars(X("ProjM"));
1667
1668     _op_MulC.code    = get_next_ir_opcode();
1669     _op_MulC.name    = new_id_from_chars(X("MulC"));
1670
1671     _op_DivC.code    = get_next_ir_opcode();
1672     _op_DivC.name    = new_id_from_chars(X("DivC"));
1673
1674     _op_ModC.code    = get_next_ir_opcode();
1675     _op_ModC.name    = new_id_from_chars(X("ModC"));
1676
1677     _op_DivModC.code = get_next_ir_opcode();
1678     _op_DivModC.name = new_id_from_chars(X("DivModC"));
1679
1680     status->op_Phi0    = &_op_Phi0;
1681     status->op_PhiM    = &_op_PhiM;
1682     status->op_ProjM   = &_op_ProjM;
1683     status->op_MulC    = &_op_MulC;
1684     status->op_DivC    = &_op_DivC;
1685     status->op_ModC    = &_op_ModC;
1686     status->op_DivModC = &_op_DivModC;
1687   }
1688   else {
1689     status->op_Phi0    = NULL;
1690     status->op_PhiM    = NULL;
1691     status->op_ProjM   = NULL;
1692     status->op_MulC    = NULL;
1693     status->op_DivC    = NULL;
1694     status->op_ModC    = NULL;
1695     status->op_DivModC = NULL;
1696   }
1697
1698   if (enable_options & FIRMSTAT_COUNT_SELS) {
1699     _op_SelSel.code    = get_next_ir_opcode();
1700     _op_SelSel.name    = new_id_from_chars(X("Sel(Sel)"));
1701
1702     _op_SelSelSel.code = get_next_ir_opcode();
1703     _op_SelSelSel.name = new_id_from_chars(X("Sel(Sel(Sel))"));
1704
1705     status->op_SelSel    = &_op_SelSel;
1706     status->op_SelSelSel = &_op_SelSelSel;
1707   }
1708   else {
1709     status->op_SelSel    = NULL;
1710     status->op_SelSelSel = NULL;
1711   }
1712
1713   /* register the dumper */
1714   stat_register_dumper(&simple_dumper);
1715
1716   if (enable_options & FIRMSTAT_CSV_OUTPUT)
1717     stat_register_dumper(&csv_dumper);
1718
1719   /* initialize the pattern hash */
1720   stat_init_pattern_history(enable_options & FIRMSTAT_PATTERN_ENABLED);
1721
1722   /* initialize the Const options */
1723   if (enable_options & FIRMSTAT_COUNT_CONSTS)
1724     stat_init_const_cnt(status);
1725
1726 #undef HOOK
1727 #undef X
1728 }
1729
1730 /* terminates the statistics module, frees all memory */
1731 void stat_term(void) {
1732   if (status != (stat_info_t *)&status_disable) {
1733     xfree(status);
1734     status = (stat_info_t *)&status_disable;
1735   }
1736 }
1737
1738 #else
1739
1740 /* initialize the statistics module. */
1741 void init_stat(unsigned enable_options) {}
1742
1743 /* Dumps a statistics snapshot */
1744 void stat_dump_snapshot(const char *name, const char *phase) {}
1745
1746 /* terminates the statistics module, frees all memory */
1747 void stat_term(void);
1748
1749 #endif /* FIRM_STATISTICS */