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