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