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