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