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