add an is_Quot function
[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 /** Calculates how many arguments of the call are const. */
707 static int cnt_const_args(ir_node *call) {
708         int i, res = 0;
709         int n = get_Call_n_params(call);
710
711         for (i = 0; i < n; ++i) {
712                 ir_node *param = get_Call_param(call, i);
713                 ir_op   *op = get_irn_op(param);
714
715                 if (op == op_Const || op == op_SymConst)
716                         ++res;
717         }  /* for */
718         return res;
719 }  /* cnt_const_args */
720
721 /**
722  * Update info on calls.
723  *
724  * @param call   The call
725  * @param graph  The graph entry containing the call
726  */
727 static void stat_update_call(ir_node *call, graph_entry_t *graph)
728 {
729         ir_node   *block = get_nodes_block(call);
730         ir_node   *ptr = get_Call_ptr(call);
731         ir_entity *ent = NULL;
732         ir_graph  *callee = NULL;
733         int       num_const_args;
734
735         /*
736          * If the block is bad, the whole subgraph will collapse later
737          * so do not count this call.
738          * This happens in dead code.
739          */
740         if (is_Bad(block))
741                 return;
742
743         cnt_inc(&graph->cnt[gcnt_all_calls]);
744
745         /* found a call, this function is not a leaf */
746         graph->is_leaf = 0;
747
748         if (get_irn_op(ptr) == op_SymConst) {
749                 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
750                         /* ok, we seems to know the entity */
751                         ent = get_SymConst_entity(ptr);
752                         callee = get_entity_irg(ent);
753
754                         /* it is recursive, if it calls at least once */
755                         if (callee == graph->irg)
756                                 graph->is_recursive = 1;
757                 }  /* if */
758         } else {
759                 /* indirect call, be could not predict */
760                 cnt_inc(&graph->cnt[gcnt_indirect_calls]);
761
762                 /* NOT a leaf call */
763                 graph->is_leaf_call = LCS_NON_LEAF_CALL;
764         }  /* if */
765
766         /* check, if it's a chain-call: Then, the call-block
767         * must dominate the end block. */
768         {
769                 ir_node *curr = get_irg_end_block(graph->irg);
770                 int depth = get_Block_dom_depth(block);
771
772                 for (; curr != block && get_Block_dom_depth(curr) > depth;) {
773                         curr = get_Block_idom(curr);
774
775                         if (! curr || is_no_Block(curr))
776                                 break;
777                 }  /* for */
778
779                 if (curr != block)
780                         graph->is_chain_call = 0;
781         }
782
783         /* check, if the callee is a leaf */
784         if (callee) {
785                 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
786
787                 if (called->is_analyzed) {
788                         if (! called->is_leaf)
789                                 graph->is_leaf_call = LCS_NON_LEAF_CALL;
790                 }  /* if */
791         }  /* if */
792
793         /* check, if arguments of the call are const */
794         num_const_args = cnt_const_args(call);
795
796         if (num_const_args > 0)
797                 cnt_inc(&graph->cnt[gcnt_call_with_cnst_arg]);
798 }  /* stat_update_call */
799
800 /**
801  * Update info on calls for graphs on the wait queue.
802  */
803 static void stat_update_call_2(ir_node *call, graph_entry_t *graph)
804 {
805         ir_node   *block = get_nodes_block(call);
806         ir_node   *ptr = get_Call_ptr(call);
807         ir_entity *ent = NULL;
808         ir_graph  *callee = NULL;
809
810         /*
811          * If the block is bad, the whole subgraph will collapse later
812          * so do not count this call.
813          * This happens in dead code.
814          */
815         if (is_Bad(block))
816                 return;
817
818         if (get_irn_op(ptr) == op_SymConst) {
819                 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
820                         /* ok, we seems to know the entity */
821                         ent = get_SymConst_entity(ptr);
822                         callee = get_entity_irg(ent);
823                 }  /* if */
824         }  /* if */
825
826         /* check, if the callee is a leaf */
827         if (callee) {
828                 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
829
830                 assert(called->is_analyzed);
831
832                 if (! called->is_leaf)
833                         graph->is_leaf_call = LCS_NON_LEAF_CALL;
834         } else
835                 graph->is_leaf_call = LCS_NON_LEAF_CALL;
836 }  /* stat_update_call_2 */
837
838 /**
839  * Find the base address and entity of an Sel node.
840  *
841  * @param sel  the node
842  *
843  * @return the base address.
844  */
845 static ir_node *find_base_adr(ir_node *sel) {
846         ir_node *ptr = get_Sel_ptr(sel);
847
848         while (is_Sel(ptr)) {
849                 sel = ptr;
850                 ptr = get_Sel_ptr(sel);
851         }
852         return ptr;
853 }  /* find_base_adr */
854
855 /**
856  * Update info on Load/Store address statistics.
857  */
858 static void stat_update_address(ir_node *node, graph_entry_t *graph) {
859         ir_opcode opc = get_irn_opcode(node);
860         ir_node *base;
861         ir_graph *irg;
862
863         switch (opc) {
864         case iro_SymConst:
865                 /* a global address */
866                 cnt_inc(&graph->cnt[gcnt_global_adr]);
867                 break;
868         case iro_Sel:
869                 base = find_base_adr(node);
870                 irg = current_ir_graph;
871                 if (base == get_irg_tls(irg)) {
872                         /* a TLS variable, like a global. */
873                         cnt_inc(&graph->cnt[gcnt_global_adr]);
874                 } else if (base == get_irg_frame(irg)) {
875                         /* a local Variable. */
876                         cnt_inc(&graph->cnt[gcnt_local_adr]);
877                 } else {
878                         /* Pointer access */
879                         if (is_Proj(base) && skip_Proj(get_Proj_pred(base)) == get_irg_start(irg)) {
880                                 /* pointer access through parameter, check for THIS */
881                                 ir_entity *ent = get_irg_entity(irg);
882
883                                 if (ent != NULL) {
884                                         ir_type *ent_tp = get_entity_type(ent);
885
886                                         if (get_method_calling_convention(ent_tp) & cc_this_call) {
887                                                 if (get_Proj_proj(base) == 0) {
888                                                         /* THIS pointer */
889                                                         cnt_inc(&graph->cnt[gcnt_this_adr]);
890                                                         goto end_parameter;
891                                                 }  /* if */
892                                         }  /* if */
893                                 }  /* if */
894                                 /* other parameter */
895                                 cnt_inc(&graph->cnt[gcnt_param_adr]);
896 end_parameter:  ;
897                         } else {
898                                 /* unknown Pointer access */
899                                 cnt_inc(&graph->cnt[gcnt_other_adr]);
900                         }  /* if */
901                 }  /* if */
902         default:
903                 ;
904         }  /* switch */
905 }  /* stat_update_address */
906
907 /**
908  * Walker for reachable nodes count.
909  */
910 static void update_node_stat(ir_node *node, void *env)
911 {
912         graph_entry_t *graph = env;
913         node_entry_t *entry;
914
915         ir_op *op = stat_get_irn_op(node);
916         int arity = get_irn_arity(node);
917
918         entry = opcode_get_entry(op, graph->opcode_hash);
919
920         cnt_inc(&entry->cnt_alive);
921         cnt_add_i(&graph->cnt[gcnt_edges], arity);
922
923         /* count block edges */
924         undate_block_info(node, graph);
925
926         /* count extended block edges */
927         if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
928                 if (graph->irg != get_const_code_irg())
929                         update_extbb_info(node, graph);
930         }  /* if */
931
932         /* handle statistics for special node types */
933
934         switch (op->code) {
935         case iro_Const:
936                 if (status->stat_options & FIRMSTAT_COUNT_CONSTS) {
937                         /* check properties of constants */
938                         stat_update_const(status, node, graph);
939                 }  /* if */
940                 break;
941         case iro_Call:
942                 /* check for properties that depends on calls like recursion/leaf/indirect call */
943                 stat_update_call(node, graph);
944                 break;
945         case iro_Load:
946                 /* check address properties */
947                 stat_update_address(get_Load_ptr(node), graph);
948                 break;
949         case iro_Store:
950                 /* check address properties */
951                 stat_update_address(get_Store_ptr(node), graph);
952                 break;
953         default:
954                 ;
955         }  /* switch */
956 }  /* update_node_stat */
957
958 /**
959  * Walker for reachable nodes count for graphs on the wait_q.
960  */
961 static void update_node_stat_2(ir_node *node, void *env) {
962         graph_entry_t *graph = env;
963
964         /* check for properties that depends on calls like recursion/leaf/indirect call */
965         if (is_Call(node))
966                 stat_update_call_2(node, graph);
967 }  /* update_node_stat_2 */
968
969 /**
970  * Get the current address mark.
971  */
972 static unsigned get_adr_mark(graph_entry_t *graph, ir_node *node) {
973         address_mark_entry_t *value = set_find(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
974
975         return value ? value->mark : 0;
976 }  /* get_adr_mark */
977
978 /**
979  * Set the current address mark.
980  */
981 static void set_adr_mark(graph_entry_t *graph, ir_node *node, unsigned val) {
982         address_mark_entry_t *value = set_insert(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
983
984         value->mark = val;
985 }  /* set_adr_mark */
986
987 #undef DUMP_ADR_MODE
988
989 #ifdef DUMP_ADR_MODE
990 /**
991  * a vcg attribute hook: Color a node with a different color if
992  * it's identified as a part of an address expression or at least referenced
993  * by an address expression.
994  */
995 static int stat_adr_mark_hook(FILE *F, ir_node *node, ir_node *local)
996 {
997         ir_node *n           = local ? local : node;
998         ir_graph *irg        = get_irn_irg(n);
999         graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1000         unsigned mark        = get_adr_mark(graph, n);
1001
1002         if (mark & MARK_ADDRESS_CALC)
1003                 fprintf(F, "color: purple");
1004         else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1005                 fprintf(F, "color: pink");
1006         else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1007                 fprintf(F, "color: lightblue");
1008         else
1009                 return 0;
1010
1011         /* I know the color! */
1012         return 1;
1013 }  /* stat_adr_mark_hook */
1014 #endif /* DUMP_ADR_MODE */
1015
1016 /**
1017  * Return the "operational" mode of a Firm node.
1018  */
1019 static ir_mode *get_irn_op_mode(ir_node *node) {
1020         switch (get_irn_opcode(node)) {
1021         case iro_Load:
1022                 return get_Load_mode(node);
1023         case iro_Store:
1024                 return get_irn_mode(get_Store_value(node));
1025         case iro_DivMod:
1026                 return get_irn_mode(get_DivMod_left(node));
1027         case iro_Div:
1028                 return get_irn_mode(get_Div_left(node));
1029         case iro_Mod:
1030                 return get_irn_mode(get_Mod_left(node));
1031         case iro_Cmp:
1032                 /* Cmp is no address calculation, or is it? */
1033         default:
1034                 return get_irn_mode(node);
1035         }  /* switch */
1036 }  /* get_irn_op_mode */
1037
1038 /**
1039  * Post-walker that marks every node that is an address calculation.
1040  *
1041  * Users of a node must be visited first. We ensure this by
1042  * calling it in the post of an outs walk. This should work even in cycles,
1043  * while the normal pre-walk will not.
1044  */
1045 static void mark_address_calc(ir_node *node, void *env) {
1046         graph_entry_t *graph = env;
1047         ir_mode *mode = get_irn_op_mode(node);
1048         int i, n;
1049         unsigned mark_preds = MARK_REF_NON_ADR;
1050
1051         if (! mode_is_numP(mode))
1052                 return;
1053
1054         if (mode_is_reference(mode)) {
1055                 /* a reference is calculated here, we are sure */
1056                 set_adr_mark(graph, node, MARK_ADDRESS_CALC);
1057
1058                 mark_preds = MARK_REF_ADR;
1059         } else {
1060                 unsigned mark = get_adr_mark(graph, node);
1061
1062                 if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR) {
1063                         /*
1064                          * this node has no reference mode, but is only
1065                          * referenced by address calculations
1066                          */
1067                         mark_preds = MARK_REF_ADR;
1068                 }  /* if */
1069         }  /* if */
1070
1071         /* mark all predecessors */
1072         for (i = 0, n = get_irn_arity(node); i < n; ++i) {
1073                 ir_node *pred = get_irn_n(node, i);
1074
1075                 mode = get_irn_op_mode(pred);
1076                 if (! mode_is_numP(mode))
1077                         continue;
1078
1079                 set_adr_mark(graph, pred, get_adr_mark(graph, pred) | mark_preds);
1080         }  /* for */
1081 }  /* mark_address_calc */
1082
1083 /**
1084  * Post-walker that marks every node that is an address calculation.
1085  *
1086  * Users of a node must be visited first. We ensure this by
1087  * calling it in the post of an outs walk. This should work even in cycles,
1088  * while the normal pre-walk will not.
1089  */
1090 static void count_adr_ops(ir_node *node, void *env) {
1091         graph_entry_t *graph = env;
1092         unsigned mark        = get_adr_mark(graph, node);
1093
1094         if (mark & MARK_ADDRESS_CALC)
1095                 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1096         else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1097                 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1098         else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1099                 cnt_inc(&graph->cnt[gcnt_all_adr_ops]);
1100 }  /* count_adr_ops */
1101
1102 /**
1103  * Called for every graph when the graph is either deleted or stat_dump_snapshot()
1104  * is called, must recalculate all statistic info.
1105  *
1106  * @param global    The global entry
1107  * @param graph     The current entry
1108  */
1109 static void update_graph_stat(graph_entry_t *global, graph_entry_t *graph)
1110 {
1111         node_entry_t *entry;
1112         int i;
1113
1114         /* clear first the alive counter in the graph */
1115         foreach_pset(graph->opcode_hash, entry) {
1116                 cnt_clr(&entry->cnt_alive);
1117         }  /* foreach_pset */
1118
1119         /* set pessimistic values */
1120         graph->is_leaf       = 1;
1121         graph->is_leaf_call  = LCS_UNKNOWN;
1122         graph->is_recursive  = 0;
1123         graph->is_chain_call = 1;
1124
1125         /* create new block counter */
1126         graph->block_hash = new_pset(block_cmp, 5);
1127
1128         /* we need dominator info */
1129         if (graph->irg != get_const_code_irg()) {
1130                 assure_doms(graph->irg);
1131
1132                 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
1133                         /* we need extended basic blocks */
1134                         compute_extbb(graph->irg);
1135
1136                         /* create new extbb counter */
1137                         graph->extbb_hash = new_pset(block_cmp, 5);
1138                 }  /* if */
1139         }  /* if */
1140
1141         /* count the nodes in the graph */
1142         irg_walk_graph(graph->irg, update_node_stat, NULL, graph);
1143
1144 #if 0
1145         /* Uncomment this code if chain-call means call exact one. */
1146         entry = opcode_get_entry(op_Call, graph->opcode_hash);
1147
1148         /* check if we have more than 1 call */
1149         if (cnt_gt(entry->cnt_alive, 1))
1150                 graph->is_chain_call = 0;
1151 #endif
1152
1153         /* recursive functions are never chain calls, leafs don't have calls */
1154         if (graph->is_recursive || graph->is_leaf)
1155                 graph->is_chain_call = 0;
1156
1157         /* assume we walk every graph only ONCE, we could sum here the global count */
1158         foreach_pset(graph->opcode_hash, entry) {
1159                 node_entry_t *g_entry = opcode_get_entry(entry->op, global->opcode_hash);
1160
1161                 /* update the node counter */
1162                 cnt_add(&g_entry->cnt_alive, &entry->cnt_alive);
1163         }  /* foreach_pset */
1164
1165         /* count the number of address calculation */
1166         if (graph->irg != get_const_code_irg()) {
1167                 ir_graph *rem = current_ir_graph;
1168
1169                 assure_irg_outs(graph->irg);
1170
1171                 /* Must be done an the outs graph */
1172                 current_ir_graph = graph->irg;
1173                 irg_out_walk(get_irg_start(graph->irg), NULL, mark_address_calc, graph);
1174                 current_ir_graph = rem;
1175
1176 #ifdef DUMP_ADR_MODE
1177                 /* register the vcg hook and dump the graph for test */
1178                 set_dump_node_vcgattr_hook(stat_adr_mark_hook);
1179                 dump_ir_block_graph(graph->irg, "-adr");
1180                 set_dump_node_vcgattr_hook(NULL);
1181 #endif /* DUMP_ADR_MODE */
1182
1183                 irg_walk_graph(graph->irg, NULL, count_adr_ops, graph);
1184         }  /* if */
1185
1186         /* count the DAG's */
1187         if (status->stat_options & FIRMSTAT_COUNT_DAG)
1188                 count_dags_in_graph(global, graph);
1189
1190         /* calculate the patterns of this graph */
1191         stat_calc_pattern_history(graph->irg);
1192
1193         /* leaf function did not call others */
1194         if (graph->is_leaf)
1195                 graph->is_leaf_call = LCS_NON_LEAF_CALL;
1196         else if (graph->is_leaf_call == LCS_UNKNOWN) {
1197                 /* we still don't know if this graph calls leaf-functions, so enqueue */
1198                 pdeq_putl(status->wait_q, graph);
1199         }  /* if */
1200
1201         /* we have analyzed this graph */
1202         graph->is_analyzed = 1;
1203
1204         /* accumulate all counter's */
1205         for (i = 0; i < _gcnt_last; ++i)
1206                 cnt_add(&global->cnt[i], &graph->cnt[i]);
1207 }  /* update_graph_stat */
1208
1209 /**
1210  * Called for every graph that was on the wait_q in stat_dump_snapshot()
1211  * must finish all statistic info calculations.
1212  *
1213  * @param global    The global entry
1214  * @param graph     The current entry
1215  */
1216 static void update_graph_stat_2(graph_entry_t *global, graph_entry_t *graph)
1217 {
1218         if (graph->is_deleted) {
1219                 /* deleted, ignore */
1220                 return;
1221         }
1222
1223         if (graph->irg) {
1224                 /* count the nodes in the graph */
1225                 irg_walk_graph(graph->irg, update_node_stat_2, NULL, graph);
1226
1227                 if (graph->is_leaf_call == LCS_UNKNOWN)
1228                         graph->is_leaf_call = LCS_LEAF_CALL;
1229         }  /* if */
1230 }  /* update_graph_stat_2 */
1231
1232 /**
1233  * Register a dumper.
1234  */
1235 static void stat_register_dumper(const dumper_t *dumper) {
1236         dumper_t *p = xmalloc(sizeof(*p));
1237
1238         if (p) {
1239                 memcpy(p, dumper, sizeof(*p));
1240
1241                 p->next        = status->dumper;
1242                 p->status      = status;
1243                 status->dumper = p;
1244         }
1245
1246         /* FIXME: memory leak */
1247 }  /* stat_register_dumper */
1248
1249 /**
1250  * Dumps the statistics of an IR graph.
1251  */
1252 static void stat_dump_graph(graph_entry_t *entry) {
1253         dumper_t *dumper;
1254
1255         for (dumper = status->dumper; dumper; dumper = dumper->next) {
1256                 if (dumper->dump_graph)
1257                         dumper->dump_graph(dumper, entry);
1258         }  /* for */
1259 }  /* stat_dump_graph */
1260
1261 /**
1262  * Calls all registered dumper functions.
1263  */
1264 static void stat_dump_registered(graph_entry_t *entry) {
1265         dumper_t *dumper;
1266
1267         for (dumper = status->dumper; dumper; dumper = dumper->next) {
1268                 if (dumper->func_map) {
1269                         dump_graph_FUNC func;
1270
1271                         foreach_pset(dumper->func_map, func)
1272                                 func(dumper, entry);
1273                 }  /* if */
1274         }  /* for */
1275 }  /* stat_dump_registered */
1276
1277 /**
1278  * Dumps a constant table.
1279  */
1280 static void stat_dump_consts(const constant_info_t *tbl) {
1281         dumper_t *dumper;
1282
1283         for (dumper = status->dumper; dumper; dumper = dumper->next) {
1284                 if (dumper->dump_const_tbl)
1285                         dumper->dump_const_tbl(dumper, tbl);
1286         }  /* for */
1287 }  /* stat_dump_consts */
1288
1289 /**
1290  * Initialize the dumper.
1291  */
1292 static void stat_dump_init(const char *name) {
1293         dumper_t *dumper;
1294
1295         for (dumper = status->dumper; dumper; dumper = dumper->next) {
1296                 if (dumper->init)
1297                         dumper->init(dumper, name);
1298         }  /* for */
1299 }  /* stat_dump_init */
1300
1301 /**
1302  * Finish the dumper.
1303  */
1304 static void stat_dump_finish(void) {
1305         dumper_t *dumper;
1306
1307         for (dumper = status->dumper; dumper; dumper = dumper->next) {
1308                 if (dumper->finish)
1309                         dumper->finish(dumper);
1310         }  /* for */
1311 }  /* stat_dump_finish */
1312
1313 /**
1314  * Register an additional function for all dumper.
1315  */
1316 void stat_register_dumper_func(dump_graph_FUNC func) {
1317         dumper_t *dumper;
1318
1319         for (dumper = status->dumper; dumper; dumper = dumper->next) {
1320                 if (! dumper->func_map)
1321                         dumper->func_map = pset_new_ptr(3);
1322                 pset_insert_ptr(dumper->func_map, func);
1323         }  /* for */
1324 }  /* stat_register_dumper_func */
1325
1326 /* ---------------------------------------------------------------------- */
1327
1328 /*
1329  * Helper: get an ir_op from an opcode.
1330  */
1331 ir_op *stat_get_op_from_opcode(ir_opcode code) {
1332         return opcode_find_entry(code, status->ir_op_hash);
1333 }  /* stat_get_op_from_opcode */
1334
1335 /**
1336  * Hook: A new IR op is registered.
1337  *
1338  * @param ctx  the hook context
1339  * @param op   the new IR opcode that was created.
1340  */
1341 static void stat_new_ir_op(void *ctx, ir_op *op) {
1342         if (! status->stat_options)
1343                 return;
1344
1345         STAT_ENTER;
1346         {
1347                 graph_entry_t *graph = graph_get_entry(NULL, status->irg_hash);
1348
1349                 /* execute for side effect :-) */
1350                 (void)opcode_get_entry(op, graph->opcode_hash);
1351
1352                 pset_insert(status->ir_op_hash, op, op->code);
1353         }
1354         STAT_LEAVE;
1355 }  /* stat_new_ir_op */
1356
1357 /**
1358  * Hook: An IR op is freed.
1359  *
1360  * @param ctx  the hook context
1361  * @param op   the IR opcode that is freed
1362  */
1363 static void stat_free_ir_op(void *ctx, ir_op *op) {
1364         if (! status->stat_options)
1365                 return;
1366
1367         STAT_ENTER;
1368         {
1369         }
1370         STAT_LEAVE;
1371 }  /* stat_free_ir_op */
1372
1373 /**
1374  * Hook: A new node is created.
1375  *
1376  * @param ctx   the hook context
1377  * @param irg   the IR graph on which the node is created
1378  * @param node  the new IR node that was created
1379  */
1380 static void stat_new_node(void *ctx, ir_graph *irg, ir_node *node) {
1381         if (! status->stat_options)
1382                 return;
1383
1384         /* do NOT count during dead node elimination */
1385         if (status->in_dead_node_elim > 0)
1386                 return;
1387
1388         STAT_ENTER;
1389         {
1390                 node_entry_t *entry;
1391                 graph_entry_t *graph;
1392                 ir_op *op = stat_get_irn_op(node);
1393
1394                 /* increase global value */
1395                 graph = graph_get_entry(NULL, status->irg_hash);
1396                 entry = opcode_get_entry(op, graph->opcode_hash);
1397                 cnt_inc(&entry->new_node);
1398
1399                 /* increase local value */
1400                 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1401                 entry = opcode_get_entry(op, graph->opcode_hash);
1402                 cnt_inc(&entry->new_node);
1403         }
1404         STAT_LEAVE;
1405 }  /* stat_new_node */
1406
1407 /**
1408  * Hook: A node is changed into a Id node
1409  *
1410  * @param ctx   the hook context
1411  * @param node  the IR node that will be turned into an ID
1412  */
1413 static void stat_turn_into_id(void *ctx, ir_node *node) {
1414         if (! status->stat_options)
1415                 return;
1416
1417         STAT_ENTER;
1418         {
1419                 node_entry_t *entry;
1420                 graph_entry_t *graph;
1421                 ir_op *op = stat_get_irn_op(node);
1422
1423                 /* increase global value */
1424                 graph = graph_get_entry(NULL, status->irg_hash);
1425                 entry = opcode_get_entry(op, graph->opcode_hash);
1426                 cnt_inc(&entry->into_Id);
1427
1428                 /* increase local value */
1429                 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1430                 entry = opcode_get_entry(op, graph->opcode_hash);
1431                 cnt_inc(&entry->into_Id);
1432         }
1433         STAT_LEAVE;
1434 }  /* stat_turn_into_id */
1435
1436 /**
1437  * Hook: A new graph was created
1438  *
1439  * @param ctx  the hook context
1440  * @param irg  the new IR graph that was created
1441  * @param ent  the entity of this graph
1442  */
1443 static void stat_new_graph(void *ctx, ir_graph *irg, ir_entity *ent) {
1444         if (! status->stat_options)
1445                 return;
1446
1447         STAT_ENTER;
1448         {
1449                 /* execute for side effect :-) */
1450                 graph_entry_t * graph = graph_get_entry(irg, status->irg_hash);
1451
1452                 graph->ent           = ent;
1453                 graph->is_deleted    = 0;
1454                 graph->is_leaf       = 0;
1455                 graph->is_leaf_call  = 0;
1456                 graph->is_recursive  = 0;
1457                 graph->is_chain_call = 0;
1458                 graph->is_analyzed   = 0;
1459         }
1460         STAT_LEAVE;
1461 }  /* stat_new_graph */
1462
1463 /**
1464  * Hook: A graph will be deleted
1465  *
1466  * @param ctx  the hook context
1467  * @param irg  the IR graph that will be deleted
1468  *
1469  * Note that we still hold the information for this graph
1470  * in our hash maps, only a flag is set which prevents this
1471  * information from being changed, it's "frozen" from now.
1472  */
1473 static void stat_free_graph(void *ctx, ir_graph *irg) {
1474         if (! status->stat_options)
1475                 return;
1476
1477         STAT_ENTER;
1478         {
1479                 graph_entry_t *graph  = graph_get_entry(irg, status->irg_hash);
1480                 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1481
1482                 graph->is_deleted = 1;
1483
1484                 if (status->stat_options & FIRMSTAT_COUNT_DELETED) {
1485                         /* count the nodes of the graph yet, it will be destroyed later */
1486                         update_graph_stat(global, graph);
1487                 }  /* if */
1488         }
1489         STAT_LEAVE;
1490 }  /* stat_free_graph */
1491
1492 /**
1493  * Hook: A walk over a graph is initiated. Do not count walks from statistic code.
1494  *
1495  * @param ctx  the hook context
1496  * @param irg  the IR graph that will be walked
1497  * @param pre  the pre walker
1498  * @param post the post walker
1499  */
1500 static void stat_irg_walk(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1501 {
1502         if (! status->stat_options)
1503                 return;
1504
1505         STAT_ENTER_SINGLE;
1506         {
1507                 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1508
1509                 cnt_inc(&graph->cnt[gcnt_acc_walked]);
1510         }
1511         STAT_LEAVE;
1512 }  /* stat_irg_walk */
1513
1514 /**
1515  * Hook: A walk over a graph in block-wise order is initiated. Do not count walks from statistic code.
1516  *
1517  * @param ctx  the hook context
1518  * @param irg  the IR graph that will be walked
1519  * @param pre  the pre walker
1520  * @param post the post walker
1521  */
1522 static void stat_irg_walk_blkwise(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1523 {
1524         /* for now, do NOT differentiate between blockwise and normal */
1525         stat_irg_walk(ctx, irg, pre, post);
1526 }  /* stat_irg_walk_blkwise */
1527
1528 /**
1529  * Hook: A walk over the graph's blocks is initiated. Do not count walks from statistic code.
1530  *
1531  * @param ctx  the hook context
1532  * @param irg  the IR graph that will be walked
1533  * @param node the IR node
1534  * @param pre  the pre walker
1535  * @param post the post walker
1536  */
1537 static void stat_irg_block_walk(void *ctx, ir_graph *irg, ir_node *node, generic_func *pre, generic_func *post)
1538 {
1539         if (! status->stat_options)
1540                 return;
1541
1542         STAT_ENTER_SINGLE;
1543         {
1544                 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1545
1546                 cnt_inc(&graph->cnt[gcnt_acc_walked_blocks]);
1547         }
1548         STAT_LEAVE;
1549 }  /* stat_irg_block_walk */
1550
1551 /**
1552  * Called for every node that is removed due to an optimization.
1553  *
1554  * @param n     the IR node that will be removed
1555  * @param hmap  the hash map containing ir_op* -> opt_entry_t*
1556  */
1557 static void removed_due_opt(ir_node *n, hmap_opt_entry_t *hmap) {
1558         ir_op *op          = stat_get_irn_op(n);
1559         opt_entry_t *entry = opt_get_entry(op, hmap);
1560
1561         /* increase global value */
1562         cnt_inc(&entry->count);
1563 }  /* removed_due_opt */
1564
1565 /**
1566  * Hook: Some nodes were optimized into some others due to an optimization.
1567  *
1568  * @param ctx  the hook context
1569  */
1570 static void stat_merge_nodes(
1571     void *ctx,
1572     ir_node **new_node_array, int new_num_entries,
1573     ir_node **old_node_array, int old_num_entries,
1574     hook_opt_kind opt)
1575 {
1576         if (! status->stat_options)
1577                 return;
1578
1579         STAT_ENTER;
1580         {
1581                 int i, j;
1582                 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1583
1584                 if (status->reassoc_run)
1585                         opt = HOOK_OPT_REASSOC;
1586
1587                 for (i = 0; i < old_num_entries; ++i) {
1588                         /* nodes might be in new and old, so if we found a node
1589                            in both sets, this one  is NOT removed */
1590                         for (j = 0; j < new_num_entries; ++j) {
1591                                 if (old_node_array[i] == new_node_array[j])
1592                                         break;
1593                         }  /* for */
1594                         if (j >= new_num_entries) {
1595                                 int xopt = opt;
1596
1597                                 /* sometimes we did not detect, that it is replaced by a Const */
1598                                 if (opt == HOOK_OPT_CONFIRM && new_num_entries == 1) {
1599                                         ir_op *op = get_irn_op(new_node_array[0]);
1600
1601                                         if (op == op_Const || op == op_SymConst)
1602                                                 xopt = HOOK_OPT_CONFIRM_C;
1603                                 }  /* if */
1604
1605                                 removed_due_opt(old_node_array[i], graph->opt_hash[xopt]);
1606                         }  /* if */
1607                 }  /* for */
1608         }
1609         STAT_LEAVE;
1610 }  /* stat_merge_nodes */
1611
1612 /**
1613  * Hook: Reassociation is started/stopped.
1614  *
1615  * @param ctx   the hook context
1616  * @param flag  if non-zero, reassociation is started else stopped
1617  */
1618 static void stat_reassociate(void *ctx, int flag) {
1619         if (! status->stat_options)
1620                 return;
1621
1622         STAT_ENTER;
1623         {
1624                 status->reassoc_run = flag;
1625         }
1626         STAT_LEAVE;
1627 }  /* stat_reassociate */
1628
1629 /**
1630  * Hook: A node was lowered into other nodes
1631  *
1632  * @param ctx  the hook context
1633  * @param node the IR node that will be lowered
1634  */
1635 static void stat_lower(void *ctx, ir_node *node) {
1636         if (! status->stat_options)
1637                 return;
1638
1639         STAT_ENTER;
1640         {
1641                 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1642
1643                 removed_due_opt(node, graph->opt_hash[HOOK_LOWERED]);
1644         }
1645         STAT_LEAVE;
1646 }  /* stat_lower */
1647
1648 /**
1649  * Hook: A graph was inlined.
1650  *
1651  * @param ctx  the hook context
1652  * @param call the IR call that will re changed into the body of
1653  *             the called IR graph
1654  * @param called_irg  the IR graph representing the called routine
1655  */
1656 static void stat_inline(void *ctx, ir_node *call, ir_graph *called_irg)
1657 {
1658         if (! status->stat_options)
1659                 return;
1660
1661         STAT_ENTER;
1662         {
1663                 ir_graph *irg = get_irn_irg(call);
1664                 graph_entry_t *i_graph = graph_get_entry(called_irg, status->irg_hash);
1665                 graph_entry_t *graph   = graph_get_entry(irg, status->irg_hash);
1666
1667                 cnt_inc(&graph->cnt[gcnt_acc_got_inlined]);
1668                 cnt_inc(&i_graph->cnt[gcnt_acc_was_inlined]);
1669         }
1670         STAT_LEAVE;
1671 }  /* stat_inline */
1672
1673 /**
1674  * Hook: A graph with tail-recursions was optimized.
1675  *
1676  * @param ctx  the hook context
1677  */
1678 static void stat_tail_rec(void *ctx, ir_graph *irg, int n_calls) {
1679         if (! status->stat_options)
1680                 return;
1681
1682         STAT_ENTER;
1683         {
1684                 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1685
1686                 graph->num_tail_recursion += n_calls;
1687         }
1688         STAT_LEAVE;
1689 }  /* stat_tail_rec */
1690
1691 /**
1692  * Strength reduction was performed on an iteration variable.
1693  *
1694  * @param ctx  the hook context
1695  */
1696 static void stat_strength_red(void *ctx, ir_graph *irg, ir_node *strong) {
1697         if (! status->stat_options)
1698                 return;
1699
1700         STAT_ENTER;
1701         {
1702                 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1703                 cnt_inc(&graph->cnt[gcnt_acc_strength_red]);
1704
1705                 removed_due_opt(strong, graph->opt_hash[HOOK_OPT_STRENGTH_RED]);
1706         }
1707         STAT_LEAVE;
1708 }  /* stat_strength_red */
1709
1710 /**
1711  * Hook: Start/Stop the dead node elimination.
1712  *
1713  * @param ctx  the hook context
1714  */
1715 static void stat_dead_node_elim(void *ctx, ir_graph *irg, int start) {
1716         if (! status->stat_options)
1717                 return;
1718
1719         if (start)
1720                 ++status->in_dead_node_elim;
1721         else
1722                 --status->in_dead_node_elim;
1723 }  /* stat_dead_node_elim */
1724
1725 /**
1726  * Hook: if-conversion was tried.
1727  */
1728 static void stat_if_conversion(void *context, ir_graph *irg, ir_node *phi,
1729                                int pos, ir_node *mux, if_result_t reason)
1730 {
1731         if (! status->stat_options)
1732                 return;
1733
1734         STAT_ENTER;
1735         {
1736                 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1737
1738                 cnt_inc(&graph->cnt[gcnt_if_conv + reason]);
1739         }
1740         STAT_LEAVE;
1741 }  /* stat_if_conversion */
1742
1743 /**
1744  * Hook: real function call was optimized.
1745  */
1746 static void stat_func_call(void *context, ir_graph *irg, ir_node *call)
1747 {
1748         if (! status->stat_options)
1749                 return;
1750
1751         STAT_ENTER;
1752         {
1753                 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1754
1755                 cnt_inc(&graph->cnt[gcnt_acc_real_func_call]);
1756         }
1757         STAT_LEAVE;
1758 }  /* stat_func_call */
1759
1760 /**
1761  * Hook: A multiply was replaced by a series of Shifts/Adds/Subs.
1762  *
1763  * @param ctx  the hook context
1764  */
1765 static void stat_arch_dep_replace_mul_with_shifts(void *ctx, ir_node *mul) {
1766         if (! status->stat_options)
1767                 return;
1768
1769         STAT_ENTER;
1770         {
1771                 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1772                 removed_due_opt(mul, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1773         }
1774         STAT_LEAVE;
1775 }  /* stat_arch_dep_replace_mul_with_shifts */
1776
1777 /**
1778  * Hook: A division by const was replaced.
1779  *
1780  * @param ctx   the hook context
1781  * @param node  the division node that will be optimized
1782  */
1783 static void stat_arch_dep_replace_division_by_const(void *ctx, ir_node *node) {
1784         if (! status->stat_options)
1785                 return;
1786
1787         STAT_ENTER;
1788         {
1789                 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1790                 removed_due_opt(node, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1791         }
1792         STAT_LEAVE;
1793 }  /* stat_arch_dep_replace_division_by_const */
1794
1795 /*
1796  * Update the register pressure of a block.
1797  *
1798  * @param irg        the irg containing the block
1799  * @param block      the block for which the reg pressure should be set
1800  * @param pressure   the pressure
1801  * @param class_name the name of the register class
1802  */
1803 void stat_be_block_regpressure(ir_graph *irg, ir_node *block, int pressure, const char *class_name)
1804 {
1805         if (! status->stat_options)
1806                 return;
1807
1808         STAT_ENTER;
1809         {
1810                 graph_entry_t        *graph = graph_get_entry(irg, status->irg_hash);
1811                 be_block_entry_t     *block_ent;
1812                 reg_pressure_entry_t *rp_ent;
1813
1814                 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1815                 rp_ent    = obstack_alloc(&status->be_data, sizeof(*rp_ent));
1816                 memset(rp_ent, 0, sizeof(*rp_ent));
1817
1818                 rp_ent->class_name = class_name;
1819                 rp_ent->pressure   = pressure;
1820
1821                 pset_insert(block_ent->reg_pressure, rp_ent, HASH_PTR(class_name));
1822         }
1823         STAT_LEAVE;
1824 }  /* stat_be_block_regpressure */
1825
1826 /**
1827  * Update the distribution of ready nodes of a block
1828  *
1829  * @param irg        the irg containing the block
1830  * @param block      the block for which the reg pressure should be set
1831  * @param num_ready  the number of ready nodes
1832  */
1833 void stat_be_block_sched_ready(ir_graph *irg, ir_node *block, int num_ready)
1834 {
1835         if (! status->stat_options)
1836                 return;
1837
1838         STAT_ENTER;
1839         {
1840                 graph_entry_t    *graph = graph_get_entry(irg, status->irg_hash);
1841                 be_block_entry_t *block_ent;
1842
1843                 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1844
1845                 /* increase the counter of corresponding number of ready nodes */
1846                 stat_inc_int_distrib_tbl(block_ent->sched_ready, num_ready);
1847         }
1848         STAT_LEAVE;
1849 }  /* stat_be_block_sched_ready */
1850
1851 /**
1852  * Update the permutation statistic of a block.
1853  *
1854  * @param class_name the name of the register class
1855  * @param n_regs     number of registers in the register class
1856  * @param perm       the perm node
1857  * @param block      the block containing the perm
1858  * @param size       the size of the perm
1859  * @param real_size  number of pairs with different registers
1860  */
1861 void stat_be_block_stat_perm(const char *class_name, int n_regs, ir_node *perm, ir_node *block,
1862                              int size, int real_size)
1863 {
1864         if (! status->stat_options)
1865                 return;
1866
1867         STAT_ENTER;
1868         {
1869                 graph_entry_t      *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
1870                 be_block_entry_t   *block_ent;
1871                 perm_class_entry_t *pc_ent;
1872                 perm_stat_entry_t  *ps_ent;
1873
1874                 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1875                 pc_ent    = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
1876                 ps_ent    = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
1877
1878                 pc_ent->n_regs = n_regs;
1879
1880                 /* update information */
1881                 ps_ent->size      = size;
1882                 ps_ent->real_size = real_size;
1883         }
1884         STAT_LEAVE;
1885 }  /* stat_be_block_stat_perm */
1886
1887 /**
1888  * Update the permutation statistic of a single perm.
1889  *
1890  * @param class_name the name of the register class
1891  * @param perm       the perm node
1892  * @param block      the block containing the perm
1893  * @param is_chain   1 if chain, 0 if cycle
1894  * @param size       length of the cycle/chain
1895  * @param n_ops      the number of ops representing this cycle/chain after lowering
1896  */
1897 void stat_be_block_stat_permcycle(const char *class_name, ir_node *perm, ir_node *block,
1898                                   int is_chain, int size, int n_ops)
1899 {
1900         if (! status->stat_options)
1901                 return;
1902
1903         STAT_ENTER;
1904         {
1905                 graph_entry_t      *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
1906                 be_block_entry_t   *block_ent;
1907                 perm_class_entry_t *pc_ent;
1908                 perm_stat_entry_t  *ps_ent;
1909
1910                 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1911                 pc_ent    = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
1912                 ps_ent    = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
1913
1914                 if (is_chain) {
1915                         ps_ent->n_copies += n_ops;
1916                         stat_inc_int_distrib_tbl(ps_ent->chains, size);
1917                 } else {
1918                         ps_ent->n_exchg += n_ops;
1919                         stat_inc_int_distrib_tbl(ps_ent->cycles, size);
1920                 }  /* if */
1921         }
1922         STAT_LEAVE;
1923 }  /* stat_be_block_stat_permcycle */
1924
1925 /* Dumps a statistics snapshot. */
1926 void stat_dump_snapshot(const char *name, const char *phase)
1927 {
1928         char fname[2048];
1929         const char *p;
1930         int l;
1931
1932         if (! status->stat_options)
1933                 return;
1934
1935         STAT_ENTER;
1936         {
1937                 graph_entry_t *entry;
1938                 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1939
1940                 /*
1941                  * The constant counter is only global, so we clear it here.
1942                  * Note that it does NOT contain the constants in DELETED
1943                  * graphs due to this.
1944                  */
1945                 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
1946                         stat_const_clear(status);
1947
1948                 /* build the name */
1949                 p = strrchr(name, '/');
1950 #ifdef _WIN32
1951                 {
1952                         const char *q;
1953
1954                         q = strrchr(name, '\\');
1955
1956                         /* NULL might be not the smallest pointer */
1957                         if (q && (!p || q > p))
1958                                 p = q;
1959                 }
1960 #endif /* _WIN32 */
1961                 if (p) {
1962                         ++p;
1963                         l = p - name;
1964
1965                         if (l > sizeof(fname) - 1)
1966                                 l = sizeof(fname) - 1;
1967
1968                         memcpy(fname, name, l);
1969                         fname[l] = '\0';
1970                 } else {
1971                         fname[0] = '\0';
1972                         p = name;
1973                 }  /* if */
1974                 strncat(fname, "firmstat-", sizeof(fname));
1975                 strncat(fname, phase, sizeof(fname));
1976                 strncat(fname, "-", sizeof(fname));
1977                 strncat(fname, p, sizeof(fname));
1978
1979                 stat_dump_init(fname);
1980
1981                 /* calculate the graph statistics */
1982                 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
1983                         if (entry->irg == NULL) {
1984                                 /* special entry for the global count */
1985                                 continue;
1986                         }  /* if */
1987                         if (! entry->is_deleted) {
1988                                 /* the graph is still alive, count the nodes on it */
1989                                 update_graph_stat(global, entry);
1990                         }  /* if */
1991                 }  /* for */
1992
1993                 /* some calculations are dependent, we pushed them on the wait_q */
1994                 while (! pdeq_empty(status->wait_q)) {
1995                         entry = pdeq_getr(status->wait_q);
1996
1997                         update_graph_stat_2(global, entry);
1998                 }  /* while */
1999
2000                 /* dump per graph */
2001                 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
2002                         if (entry->irg == NULL) {
2003                                 /* special entry for the global count */
2004                                 continue;
2005                         }  /* if */
2006
2007                         if (! entry->is_deleted || status->stat_options & FIRMSTAT_COUNT_DELETED) {
2008                                 stat_dump_graph(entry);
2009                                 stat_dump_registered(entry);
2010                         }  /* if */
2011
2012                         if (! entry->is_deleted) {
2013                                 /* clear the counter that are not accumulated */
2014                                 graph_clear_entry(entry, 0);
2015                         }  /* if */
2016                 }  /* for */
2017
2018                 /* dump global */
2019                 stat_dump_graph(global);
2020
2021                 /* dump the const info */
2022                 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2023                         stat_dump_consts(&status->const_info);
2024
2025                 stat_dump_finish();
2026
2027                 stat_finish_pattern_history(fname);
2028
2029                 /* clear the global counter here */
2030                 {
2031                         node_entry_t *entry;
2032
2033                         for (entry = pset_first(global->opcode_hash); entry; entry = pset_next(global->opcode_hash)) {
2034                                 opcode_clear_entry(entry);
2035                         }  /* for */
2036                         /* clear all global counter */
2037                         graph_clear_entry(global, 1);
2038                 }
2039         }
2040         STAT_LEAVE;
2041 }  /* stat_dump_snapshot */
2042
2043 /** the hook entries for the Firm statistics module */
2044 static hook_entry_t stat_hooks[hook_last];
2045
2046 /* initialize the statistics module. */
2047 void firm_init_stat(unsigned enable_options)
2048 {
2049 #define X(a)  a, sizeof(a)-1
2050 #define HOOK(h, fkt) \
2051         stat_hooks[h].hook._##h = fkt; register_hook(h, &stat_hooks[h])
2052         unsigned num = 0;
2053
2054         if (! (enable_options & FIRMSTAT_ENABLED))
2055                 return;
2056
2057         status = xmalloc(sizeof(*status));
2058         memset(status, 0, sizeof(*status));
2059
2060         /* enable statistics */
2061         status->stat_options = enable_options & FIRMSTAT_ENABLED ? enable_options : 0;
2062
2063         /* register all hooks */
2064         HOOK(hook_new_ir_op,                          stat_new_ir_op);
2065         HOOK(hook_free_ir_op,                         stat_free_ir_op);
2066         HOOK(hook_new_node,                           stat_new_node);
2067         HOOK(hook_turn_into_id,                       stat_turn_into_id);
2068         HOOK(hook_new_graph,                          stat_new_graph);
2069         HOOK(hook_free_graph,                         stat_free_graph);
2070         HOOK(hook_irg_walk,                           stat_irg_walk);
2071         HOOK(hook_irg_walk_blkwise,                   stat_irg_walk_blkwise);
2072         HOOK(hook_irg_block_walk,                     stat_irg_block_walk);
2073         HOOK(hook_merge_nodes,                        stat_merge_nodes);
2074         HOOK(hook_reassociate,                        stat_reassociate);
2075         HOOK(hook_lower,                              stat_lower);
2076         HOOK(hook_inline,                             stat_inline);
2077         HOOK(hook_tail_rec,                           stat_tail_rec);
2078         HOOK(hook_strength_red,                       stat_strength_red);
2079         HOOK(hook_dead_node_elim,                     stat_dead_node_elim);
2080         HOOK(hook_if_conversion,                      stat_if_conversion);
2081         HOOK(hook_func_call,                          stat_func_call);
2082         HOOK(hook_arch_dep_replace_mul_with_shifts,   stat_arch_dep_replace_mul_with_shifts);
2083         HOOK(hook_arch_dep_replace_division_by_const, stat_arch_dep_replace_division_by_const);
2084
2085         obstack_init(&status->cnts);
2086         obstack_init(&status->be_data);
2087
2088         /* create the hash-tables */
2089         status->irg_hash   = new_pset(graph_cmp, 8);
2090         status->ir_op_hash = new_pset(opcode_cmp_2, 1);
2091
2092         /* create the wait queue */
2093         status->wait_q     = new_pdeq();
2094
2095         if (enable_options & FIRMSTAT_COUNT_STRONG_OP) {
2096                 /* build the pseudo-ops */
2097
2098                 _op_Phi0.code    = --num;
2099                 _op_Phi0.name    = new_id_from_chars(X("Phi0"));
2100
2101                 _op_PhiM.code    = --num;
2102                 _op_PhiM.name    = new_id_from_chars(X("PhiM"));
2103
2104                 _op_ProjM.code   = --num;
2105                 _op_ProjM.name   = new_id_from_chars(X("ProjM"));
2106
2107                 _op_MulC.code    = --num;
2108                 _op_MulC.name    = new_id_from_chars(X("MulC"));
2109
2110                 _op_DivC.code    = --num;
2111                 _op_DivC.name    = new_id_from_chars(X("DivC"));
2112
2113                 _op_ModC.code    = --num;
2114                 _op_ModC.name    = new_id_from_chars(X("ModC"));
2115
2116                 _op_DivModC.code = --num;
2117                 _op_DivModC.name = new_id_from_chars(X("DivModC"));
2118
2119                 status->op_Phi0    = &_op_Phi0;
2120                 status->op_PhiM    = &_op_PhiM;
2121                 status->op_ProjM   = &_op_ProjM;
2122                 status->op_MulC    = &_op_MulC;
2123                 status->op_DivC    = &_op_DivC;
2124                 status->op_ModC    = &_op_ModC;
2125                 status->op_DivModC = &_op_DivModC;
2126         } else {
2127                 status->op_Phi0    = NULL;
2128                 status->op_PhiM    = NULL;
2129                 status->op_ProjM   = NULL;
2130                 status->op_MulC    = NULL;
2131                 status->op_DivC    = NULL;
2132                 status->op_ModC    = NULL;
2133                 status->op_DivModC = NULL;
2134         }  /* if */
2135
2136         /* for Florian: count the Sel depth */
2137         if (enable_options & FIRMSTAT_COUNT_SELS) {
2138                 _op_SelSel.code    = --num;
2139                 _op_SelSel.name    = new_id_from_chars(X("Sel(Sel)"));
2140
2141                 _op_SelSelSel.code = --num;
2142                 _op_SelSelSel.name = new_id_from_chars(X("Sel(Sel(Sel))"));
2143
2144                 status->op_SelSel    = &_op_SelSel;
2145                 status->op_SelSelSel = &_op_SelSelSel;
2146         } else {
2147                 status->op_SelSel    = NULL;
2148                 status->op_SelSelSel = NULL;
2149         }  /* if */
2150
2151         /* register the dumper */
2152         stat_register_dumper(&simple_dumper);
2153
2154         if (enable_options & FIRMSTAT_CSV_OUTPUT)
2155                 stat_register_dumper(&csv_dumper);
2156
2157         /* initialize the pattern hash */
2158         stat_init_pattern_history(enable_options & FIRMSTAT_PATTERN_ENABLED);
2159
2160         /* initialize the Const options */
2161         if (enable_options & FIRMSTAT_COUNT_CONSTS)
2162                 stat_init_const_cnt(status);
2163
2164 #undef HOOK
2165 #undef X
2166 }  /* firm_init_stat */
2167
2168 /**
2169  * Frees all dumper structures.
2170  */
2171 static void stat_term_dumper(void) {
2172         dumper_t *dumper, *next_dumper;
2173
2174         for (dumper = status->dumper; dumper; /* iteration done in loop body */ ) {
2175                 if (dumper->func_map)
2176                         del_pset(dumper->func_map);
2177
2178                 next_dumper = dumper->next;
2179                 free(dumper);
2180                 dumper = next_dumper;
2181         }  /* for */
2182 }  /* stat_term_dumper */
2183
2184
2185 /* Terminates the statistics module, frees all memory. */
2186 void stat_term(void) {
2187         if (status != (stat_info_t *)&status_disable) {
2188                 obstack_free(&status->be_data, NULL);
2189                 obstack_free(&status->cnts, NULL);
2190
2191                 stat_term_dumper();
2192
2193                 xfree(status);
2194                 status = (stat_info_t *)&status_disable;
2195         }
2196 }  /* stat_term */
2197
2198 /* returns 1 if statistics were initialized, 0 otherwise */
2199 int stat_is_active(void) {
2200         return status != (stat_info_t *)&status_disable;
2201 }  /* stat_is_active */
2202
2203 #else
2204
2205 /* initialize the statistics module. */
2206 void firm_init_stat(unsigned enable_options) {}
2207
2208 /* Dumps a statistics snapshot */
2209 void stat_dump_snapshot(const char *name, const char *phase) {}
2210
2211 /* terminates the statistics module, frees all memory */
2212 void stat_term(void);
2213
2214 #endif /* FIRM_STATISTICS */