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