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