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