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