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