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