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