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