better doxygen docu
[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 #include <stdlib.h>
20 #include <string.h>
21
22 #include "firmstat_t.h"
23 #include "pattern.h"
24 #include "dags.h"
25 #include "stat_dmp.h"
26
27 /*
28  * need this to be static:
29  * Special pseudo Opcodes that we need to count some interesting cases
30  */
31
32 /**
33  * The Phi0, a node that is created during SSA construction
34  */
35 static ir_op _op_Phi0;
36
37 /** The PhiM, just to count memorty Phi's. */
38 static ir_op _op_PhiM;
39
40 /** The Mul by Const node. */
41 static ir_op _op_MulC;
42
43 /** The Div by Const node. */
44 static ir_op _op_DivC;
45
46 /** The Div by Const node. */
47 static ir_op _op_ModC;
48
49 /** The Div by Const node. */
50 static ir_op _op_DivModC;
51
52 /* ---------------------------------------------------------------------------------- */
53
54 #define STAT_ENTER              ++status->recursive
55 #define STAT_LEAVE              --status->recursive
56 #define STAT_ENTER_SINGLE       do { if (status->recursive > 0) return; ++status->recursive; } while (0)
57
58 /**
59  * global status
60  */
61 static stat_info_t _status, *status = &_status;
62
63 /**
64  * compare two elements of the opcode hash
65  */
66 static int opcode_cmp(const void *elt, const void *key)
67 {
68   const node_entry_t *e1 = elt;
69   const node_entry_t *e2 = key;
70
71   return e1->op->code - e2->op->code;
72 }
73
74 /**
75  * compare two elements of the graph hash
76  */
77 static int graph_cmp(const void *elt, const void *key)
78 {
79   const graph_entry_t *e1 = elt;
80   const graph_entry_t *e2 = key;
81
82   return e1->irg != e2->irg;
83 }
84
85 /**
86  * compare two elements of the optimization hash
87  */
88 static int opt_cmp(const void *elt, const void *key)
89 {
90   const opt_entry_t *e1 = elt;
91   const opt_entry_t *e2 = key;
92
93   return e1->op->code != e2->op->code;
94 }
95
96 /**
97  * compare two elements of the block hash
98  */
99 static int block_cmp(const void *elt, const void *key)
100 {
101   const block_entry_t *e1 = elt;
102   const block_entry_t *e2 = key;
103
104   return e1->block_nr != e2->block_nr;
105 }
106
107 /**
108  * compare two elements of the ir_op hash
109  */
110 static int opcode_cmp_2(const void *elt, const void *key)
111 {
112   const ir_op *e1 = elt;
113   const ir_op *e2 = key;
114
115   return e1->code != e2->code;
116 }
117
118 /**
119  * clears all counter in a node_entry_t
120  */
121 static void opcode_clear_entry(node_entry_t *elem)
122 {
123   cnt_clr(&elem->cnt_alive);
124   cnt_clr(&elem->new_node);
125   cnt_clr(&elem->into_Id);
126 }
127
128 /**
129  * Returns the associates node_entry_t for an ir_op
130  */
131 static node_entry_t *opcode_get_entry(const ir_op *op, pset *set)
132 {
133   node_entry_t key;
134   node_entry_t *elem;
135
136   key.op = op;
137
138   elem = pset_find(set, &key, op->code);
139   if (elem)
140     return elem;
141
142   elem = obstack_alloc(&status->cnts, sizeof(*elem));
143
144   /* clear counter */
145   opcode_clear_entry(elem);
146
147   elem->op = op;
148
149   return pset_insert(set, elem, op->code);
150 }
151
152 /**
153  * Returns the associates ir_op for an opcode
154  */
155 static ir_op *opcode_find_entry(opcode code, pset *set)
156 {
157   ir_op key;
158
159   key.code = code;
160   return pset_find(set, &key, code);
161 }
162
163 /**
164  * calculates a hash value for an irg
165  * Addresses are typically aligned at 32bit, so we ignore the lowest bits
166  */
167 static INLINE unsigned irg_hash(const ir_graph *irg)
168 {
169   return (unsigned)irg >> 3;
170 }
171
172 /**
173  * clears all counter in a graph_entry_t
174  */
175 static void graph_clear_entry(graph_entry_t *elem, int all)
176 {
177   if (all) {
178     cnt_clr(&elem->cnt_walked);
179     cnt_clr(&elem->cnt_walked_blocks);
180     cnt_clr(&elem->cnt_was_inlined);
181     cnt_clr(&elem->cnt_got_inlined);
182     cnt_clr(&elem->cnt_strength_red);
183   }
184   cnt_clr(&elem->cnt_edges);
185   cnt_clr(&elem->cnt_all_calls);
186   cnt_clr(&elem->cnt_indirect_calls);
187 }
188
189 /**
190  * Returns the acssociates graph_entry_t for an irg
191  */
192 static graph_entry_t *graph_get_entry(ir_graph *irg, pset *set)
193 {
194   graph_entry_t key;
195   graph_entry_t *elem;
196   int i;
197
198   key.irg = irg;
199
200   elem = pset_find(set, &key, irg_hash(irg));
201   if (elem)
202     return elem;
203
204   /* allocate a new one */
205   elem = obstack_alloc(&status->cnts, sizeof(*elem));
206
207   /* clear counter */
208   graph_clear_entry(elem, 1);
209
210   /* new hash table for opcodes here  */
211   elem->opcode_hash  = new_pset(opcode_cmp, 5);
212   elem->block_hash   = new_pset(block_cmp, 5);
213   elem->irg          = irg;
214
215   for (i = 0; i < sizeof(elem->opt_hash)/sizeof(elem->opt_hash[0]); ++i)
216     elem->opt_hash[i] = new_pset(opt_cmp, 4);
217
218   return pset_insert(set, elem, irg_hash(irg));
219 }
220
221 /**
222  * clears all counter in an opt_entry_t
223  */
224 static void opt_clear_entry(opt_entry_t *elem)
225 {
226   cnt_clr(&elem->count);
227 }
228
229 /**
230  * Returns the associates opt_entry_t for an ir_op
231  */
232 static opt_entry_t *opt_get_entry(const ir_op *op, pset *set)
233 {
234   opt_entry_t key;
235   opt_entry_t *elem;
236
237   key.op = op;
238
239   elem = pset_find(set, &key, op->code);
240   if (elem)
241     return elem;
242
243   elem = obstack_alloc(&status->cnts, sizeof(*elem));
244
245   /* clear new counter */
246   opt_clear_entry(elem);
247
248   elem->op = op;
249
250   return pset_insert(set, elem, op->code);
251 }
252
253 /**
254  * clears all counter in a block_entry_t
255  */
256 static void block_clear_entry(block_entry_t *elem)
257 {
258   cnt_clr(&elem->cnt_nodes);
259   cnt_clr(&elem->cnt_edges);
260   cnt_clr(&elem->cnt_in_edges);
261   cnt_clr(&elem->cnt_out_edges);
262 }
263
264 /**
265  * Returns the associates block_entry_t for an block
266  */
267 static block_entry_t *block_get_entry(long block_nr, pset *set)
268 {
269   block_entry_t key;
270   block_entry_t *elem;
271
272   key.block_nr = block_nr;
273
274   elem = pset_find(set, &key, block_nr);
275   if (elem)
276     return elem;
277
278   elem = obstack_alloc(&status->cnts, sizeof(*elem));
279
280   /* clear new counter */
281   block_clear_entry(elem);
282
283   elem->block_nr = block_nr;
284
285   return pset_insert(set, elem, block_nr);
286 }
287
288
289 /**
290  * Returns the ir_op for an IR-node,
291  * handles special cases and return pseudo op codes
292  */
293 static ir_op *stat_get_irn_op(ir_node *node)
294 {
295   ir_op *op = get_irn_op(node);
296
297   if (op->code == iro_Phi && get_irn_arity(node) == 0) {
298     /* special case, a Phi0 node, count on extra counter */
299     op = status->op_Phi0;
300   }
301   else if (op->code == iro_Phi && get_irn_mode(node) == mode_M) {
302     /* special case, a Memory Phi node, count on extra counter */
303     op = status->op_PhiM;
304   }
305   else if (op->code == iro_Mul &&
306            (get_irn_op(get_Mul_left(node)) == op_Const || get_irn_op(get_Mul_right(node)) == op_Const)) {
307     /* special case, a Multiply by a const, count on extra counter */
308     op = status->op_MulC ? status->op_MulC : op_Mul;
309   }
310   else if (op->code == iro_Div && get_irn_op(get_Div_right(node)) == op_Const) {
311     /* special case, a division by a const, count on extra counter */
312     op = status->op_DivC ? status->op_DivC : op_Div;
313   }
314   else if (op->code == iro_Mod && get_irn_op(get_Mod_right(node)) == op_Const) {
315     /* special case, a module by a const, count on extra counter */
316     op = status->op_ModC ? status->op_ModC : op_Mod;
317   }
318   else if (op->code == iro_DivMod && get_irn_op(get_DivMod_right(node)) == op_Const) {
319     /* special case, a division/modulo by a const, count on extra counter */
320     op = status->op_DivModC ? status->op_DivModC : op_DivMod;
321   }
322
323   return op;
324 }
325
326 /**
327  * update the block counter
328  */
329 static void count_block_info(ir_node *node, graph_entry_t *graph)
330 {
331   ir_op *op = get_irn_op(node);
332   ir_node *block;
333   block_entry_t *b_entry;
334   int i, arity;
335
336   /* check for block */
337   if (op == op_Block) {
338     arity = get_irn_arity(node);
339     b_entry = block_get_entry(get_irn_node_nr(node), graph->block_hash);
340
341     /* count all incoming edges */
342     for (i = 0; i < arity; ++i) {
343       ir_node *pred = get_irn_n(node, i);
344       ir_node *other_block = get_nodes_block(pred);
345       block_entry_t *b_entry_other = block_get_entry(get_irn_node_nr(other_block), graph->block_hash);
346
347       cnt_inc(&b_entry->cnt_in_edges);  /* an edge coming from another block */
348       cnt_inc(&b_entry_other->cnt_out_edges);
349     }
350     return;
351   }
352   else if (op == op_Call) {
353     // return;
354   }
355
356   block   = get_nodes_block(node);
357   b_entry = block_get_entry(get_irn_node_nr(block), graph->block_hash);
358
359   /* we have a new nodes */
360   cnt_inc(&b_entry->cnt_nodes);
361
362   arity = get_irn_arity(node);
363
364   for (i = 0; i < arity; ++i) {
365     ir_node *pred = get_irn_n(node, i);
366     ir_node *other_block;
367
368     if (get_irn_op(pred) == op_Block)
369       continue;
370
371     other_block = get_nodes_block(pred);
372
373     if (other_block == block)
374       cnt_inc(&b_entry->cnt_edges);     /* a in block edge */
375     else {
376       block_entry_t *b_entry_other = block_get_entry(get_irn_node_nr(other_block), graph->block_hash);
377
378       cnt_inc(&b_entry->cnt_in_edges);  /* an edge coming from another block */
379       cnt_inc(&b_entry_other->cnt_out_edges);
380     }
381   }
382 }
383
384 /**
385  * update info on calls
386  */
387 static void update_call_stat(ir_node *call, graph_entry_t *graph)
388 {
389   ir_node *ptr = get_Call_ptr(call);
390   entity *ent = NULL;
391
392   cnt_inc(&graph->cnt_all_calls);
393
394   /* found a call, is not a leaf function */
395   graph->is_leaf = 0;
396
397   if (get_irn_op(ptr) == op_SymConst) {
398     if (get_SymConst_kind(ptr) == symconst_addr_ent) {
399       /* ok, we seems to know the entity */
400       ent = get_SymConst_entity(ptr);
401
402       if (get_entity_irg(ent) == graph->irg)
403         graph->is_recursive = 1;
404     }
405   }
406   else {
407     /* indirect call */
408     cnt_inc(&graph->cnt_indirect_calls);
409   }
410
411   /* check, if it's a chain-call: Then, the call-block
412    * must dominate the end block. */
413   {
414     ir_node *curr = get_irg_end_block(graph->irg);
415     ir_node *block  = get_nodes_block(call);
416     int depth = get_Block_dom_depth(block);
417
418     for (; curr != block && get_Block_dom_depth(curr) > depth;) {
419       curr = get_Block_idom(curr);
420
421       if (! curr || is_no_Block(curr))
422         break;
423     }
424
425     if (curr != block)
426       graph->is_chain_call = 0;
427   }
428 }
429
430 /**
431  * walker for reachable nodes count
432  */
433 static void update_node_stat(ir_node *node, void *env)
434 {
435   graph_entry_t *graph = env;
436   node_entry_t *entry;
437
438   ir_op *op = stat_get_irn_op(node);
439   int arity = get_irn_arity(node);
440
441   entry = opcode_get_entry(op, graph->opcode_hash);
442
443   cnt_inc(&entry->cnt_alive);
444   cnt_add_i(&graph->cnt_edges, arity);
445
446   /* count block edges */
447   count_block_info(node, graph);
448
449   /* check for properties that depends on calls like recursion/leaf/indirect call */
450   if (get_irn_op(node) == op_Call)
451     update_call_stat(node, graph);
452 }
453
454 /**
455  * called for every graph when the graph is either deleted or stat_finish
456  * is called, must recalculate all statistic info
457  */
458 static void update_graph_stat(graph_entry_t *global, graph_entry_t *graph)
459 {
460   node_entry_t *entry;
461
462   /* clear first the alive counter in the graph */
463   for (entry = pset_first(graph->opcode_hash); entry; entry = pset_next(graph->opcode_hash)) {
464     cnt_clr(&entry->cnt_alive);
465   }
466
467   /* set pessimistic values */
468   graph->is_leaf       = 1;
469   graph->is_recursive  = 0;
470   graph->is_chain_call = 1;
471
472   /* we need dominator info */
473   if (graph->irg != get_const_code_irg())
474     if (get_irg_dom_state(graph->irg) != dom_consistent)
475       compute_doms(graph->irg);
476
477   /* count the nodes in the graph */
478   irg_walk_graph(graph->irg, update_node_stat, NULL, graph);
479
480 #if 0
481   entry = opcode_get_entry(op_Call, graph->opcode_hash);
482
483   /* check if we have more than 1 call */
484   if (cnt_gt(entry->cnt_alive, 1))
485     graph->is_chain_call = 0;
486 #endif
487
488   /* recursive functions are never chain calls, leafs don't have calls */
489   if (graph->is_recursive || graph->is_leaf)
490     graph->is_chain_call = 0;
491
492   /* assume we walk every graph only ONCE, we could sum here the global count */
493   for (entry = pset_first(graph->opcode_hash); entry; entry = pset_next(graph->opcode_hash)) {
494     node_entry_t *g_entry = opcode_get_entry(entry->op, global->opcode_hash);
495
496     /* update the node counter */
497     cnt_add(&g_entry->cnt_alive, &entry->cnt_alive);
498   }
499
500   /* update the edge counter */
501   cnt_add(&global->cnt_edges, &graph->cnt_edges);
502 }
503
504 /**
505  * register a dumper
506  */
507 static void stat_register_dumper(const dumper_t *dumper)
508 {
509   dumper_t *p = malloc(sizeof(*p));
510
511   if (p) {
512     *p = *dumper;
513
514     p->next        = status->dumper;
515     p->status      = status;
516     status->dumper = p;
517   }
518
519   /* FIXME: memory leak */
520 }
521
522 /**
523  * dumps an irg
524  */
525 static void dump_graph(graph_entry_t *entry)
526 {
527   dumper_t *dumper;
528
529   for (dumper = status->dumper; dumper; dumper = dumper->next) {
530     if (dumper->dump_graph)
531       dumper->dump_graph(dumper, entry);
532   }
533 }
534
535 /**
536  * initialise the dumper
537  */
538 static void dump_init(const char *name)
539 {
540   dumper_t *dumper;
541
542   for (dumper = status->dumper; dumper; dumper = dumper->next) {
543     if (dumper->init)
544       dumper->init(dumper, name);
545   }
546 }
547
548 /**
549  * finish the dumper
550  */
551 static void dump_finish(void)
552 {
553   dumper_t *dumper;
554
555   for (dumper = status->dumper; dumper; dumper = dumper->next) {
556     if (dumper->finish)
557       dumper->finish(dumper);
558   }
559 }
560
561 /* ---------------------------------------------------------------------- */
562
563 /*
564  * helper: get an ir_op from an opcode
565  */
566 ir_op *stat_get_op_from_opcode(opcode code)
567 {
568   return opcode_find_entry(code, status->ir_op_hash);
569 }
570
571 /* initialize the statistics module. */
572 void init_stat(unsigned enable_options)
573 {
574 #define X(a)  a, sizeof(a)-1
575
576   int pseudo_id = 0;
577
578   /* enable statistics */
579   status->enable = enable_options & FIRMSTAT_ENABLED;
580
581   if (! status->enable)
582    return;
583
584   obstack_init(&status->cnts);
585
586   /* build the pseudo-ops */
587   _op_Phi0.code = --pseudo_id;
588   _op_Phi0.name = new_id_from_chars(X("Phi0"));
589
590   _op_PhiM.code = --pseudo_id;
591   _op_PhiM.name = new_id_from_chars(X("PhiM"));
592
593   _op_MulC.code = --pseudo_id;
594   _op_MulC.name = new_id_from_chars(X("MulC"));
595
596   _op_DivC.code = --pseudo_id;
597   _op_DivC.name = new_id_from_chars(X("DivC"));
598
599   _op_ModC.code = --pseudo_id;
600   _op_ModC.name = new_id_from_chars(X("ModC"));
601
602   _op_DivModC.code = --pseudo_id;
603   _op_DivModC.name = new_id_from_chars(X("DivModC"));
604
605   /* create the hash-tables */
606   status->irg_hash   = new_pset(graph_cmp, 8);
607   status->ir_op_hash = new_pset(opcode_cmp_2, 1);
608
609   status->op_Phi0    = &_op_Phi0;
610   status->op_PhiM    = &_op_PhiM;
611
612   if (enable_options & FIRMSTAT_COUNT_STRONG_OP) {
613     status->op_MulC    = &_op_MulC;
614     status->op_DivC    = &_op_DivC;
615     status->op_ModC    = &_op_ModC;
616     status->op_DivModC = &_op_DivModC;
617   }
618   else {
619     status->op_MulC    = NULL;
620     status->op_DivC    = NULL;
621     status->op_ModC    = NULL;
622     status->op_DivModC = NULL;
623   }
624
625   /* register the dumper */
626   stat_register_dumper(&simple_dumper);
627
628   if (enable_options & FIRMSTAT_CSV_OUTPUT)
629     stat_register_dumper(&csv_dumper);
630
631   /* initialize the pattern hash */
632   stat_init_pattern_history(enable_options & FIRMSTAT_PATTERN_ENABLED);
633 #undef X
634 }
635
636 /* A new IR op is registered. */
637 void stat_new_ir_op(const ir_op *op)
638 {
639   if (! status->enable)
640     return;
641
642   STAT_ENTER;
643   {
644     graph_entry_t *graph = graph_get_entry(NULL, status->irg_hash);
645
646     /* execute for side effect :-) */
647     opcode_get_entry(op, graph->opcode_hash);
648
649     pset_insert(status->ir_op_hash, op, op->code);
650   }
651   STAT_LEAVE;
652 }
653
654 /* An IR op is freed. */
655 void stat_free_ir_op(const ir_op *op)
656 {
657   if (! status->enable)
658     return;
659
660   STAT_ENTER;
661   {
662   }
663   STAT_LEAVE;
664 }
665
666 /* A new node is created. */
667 void stat_new_node(ir_node *node)
668 {
669   if (! status->enable)
670     return;
671
672   /* do NOT count during dead node elimination */
673   if (status->in_dead_node_elim > 0)
674     return;
675
676   STAT_ENTER;
677   {
678     node_entry_t *entry;
679     graph_entry_t *graph;
680     ir_op *op = stat_get_irn_op(node);
681
682     /* increase global value */
683     graph = graph_get_entry(NULL, status->irg_hash);
684     entry = opcode_get_entry(op, graph->opcode_hash);
685     cnt_inc(&entry->new_node);
686
687     /* increase local value */
688     graph = graph_get_entry(current_ir_graph, status->irg_hash);
689     entry = opcode_get_entry(op, graph->opcode_hash);
690     cnt_inc(&entry->new_node);
691   }
692   STAT_LEAVE;
693 }
694
695 /* A node is changed into a Id node */
696 void stat_turn_into_id(ir_node *node)
697 {
698   if (! status->enable)
699     return;
700
701   STAT_ENTER;
702   {
703     node_entry_t *entry;
704     graph_entry_t *graph;
705     ir_op *op = stat_get_irn_op(node);
706
707     /* increase global value */
708     graph = graph_get_entry(NULL, status->irg_hash);
709     entry = opcode_get_entry(op, graph->opcode_hash);
710     cnt_inc(&entry->into_Id);
711
712     /* increase local value */
713     graph = graph_get_entry(current_ir_graph, status->irg_hash);
714     entry = opcode_get_entry(op, graph->opcode_hash);
715     cnt_inc(&entry->into_Id);
716   }
717   STAT_LEAVE;
718 }
719
720 /* A new graph was created */
721 void stat_new_graph(ir_graph *irg, entity *ent)
722 {
723   if (! status->enable)
724     return;
725
726   STAT_ENTER;
727   {
728     /* execute for side effect :-) */
729     graph_entry_t * graph = graph_get_entry(irg, status->irg_hash);
730
731     graph->ent           = ent;
732     graph->is_deleted    = 0;
733     graph->is_leaf       = 0;
734     graph->is_recursive  = 0;
735     graph->is_chain_call = 0;
736   }
737   STAT_LEAVE;
738 }
739
740 /*
741  * A graph will be deleted
742  */
743 void stat_free_graph(ir_graph *irg)
744 {
745   if (! status->enable)
746     return;
747
748   STAT_ENTER;
749   {
750     graph_entry_t *graph  = graph_get_entry(irg, status->irg_hash);
751     graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
752
753     graph->is_deleted = 1;
754
755     /* count the nodes of the graph yet, it will be destroyed later */
756     update_graph_stat(global, graph);
757
758     /* count the DAG's */
759 //    count_dags_in_graph(global, graph);
760
761     /* calculate the pattern */
762     stat_calc_pattern_history(irg);
763   }
764   STAT_LEAVE;
765 }
766
767 /*
768  * A walk over a graph is initiated. Do not count walks from statistic code.
769  */
770 void stat_irg_walk(ir_graph *irg, void *pre, void *post)
771 {
772   if (! status->enable)
773     return;
774
775   STAT_ENTER_SINGLE;
776   {
777     graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
778
779     cnt_inc(&graph->cnt_walked);
780   }
781   STAT_LEAVE;
782 }
783
784 /*
785  * A walk over a graph in block-wise order is initiated. Do not count walks from statistic code.
786  */
787 void stat_irg_walk_blkwise(ir_graph *irg, void *pre, void *post)
788 {
789   /* for now, do NOT differentiate between blockwise and normal */
790   stat_irg_walk(irg, pre, post);
791 }
792
793 /*
794  * A walk over the graph's blocks is initiated. Do not count walks from statistic code.
795  */
796 void stat_irg_block_walk(ir_graph *irg, const ir_node *node, void *pre, void *post)
797 {
798   if (! status->enable)
799     return;
800
801   STAT_ENTER_SINGLE;
802   {
803     graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
804
805     cnt_inc(&graph->cnt_walked_blocks);
806   }
807   STAT_LEAVE;
808 }
809
810 /**
811  * called for every node that is removed due to an optimization
812  */
813 static void removed_due_opt(ir_node *n, pset *set)
814 {
815   ir_op *op          = stat_get_irn_op(n);
816   opt_entry_t *entry = opt_get_entry(op, set);
817
818   /* increase global value */
819   cnt_inc(&entry->count);
820 }
821
822 /*
823  * Some nodes were optimized into some others due to an optimization
824  */
825 void stat_merge_nodes(
826     ir_node **new_node_array, int new_num_entries,
827     ir_node **old_node_array, int old_num_entries,
828     stat_opt_kind opt)
829 {
830   if (! status->enable)
831     return;
832
833   STAT_ENTER;
834   {
835     int i, j;
836     graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
837
838     if (status->reassoc_run)
839       opt = STAT_OPT_REASSOC;
840
841     for (i = 0; i < old_num_entries; ++i) {
842       for (j = 0; j < new_num_entries; ++j)
843         if (old_node_array[i] == new_node_array[j])
844           break;
845
846       /* nodes might be in new and old, these are NOT removed */
847       if (j >= new_num_entries) {
848         removed_due_opt(old_node_array[i], graph->opt_hash[opt]);
849       }
850     }
851   }
852   STAT_LEAVE;
853 }
854
855 /*
856  * reassociation started/stopped.
857  */
858 void stat_reassociate(int flag)
859 {
860   if (! status->enable)
861     return;
862
863   STAT_ENTER;
864   {
865     status->reassoc_run = flag;
866   }
867   STAT_LEAVE;
868 }
869
870 /*
871  * A node was lowered into other nodes
872  */
873 void stat_lower(ir_node *node)
874 {
875   if (! status->enable)
876     return;
877
878   STAT_ENTER;
879   {
880     graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
881
882     removed_due_opt(node, graph->opt_hash[STAT_LOWERED]);
883   }
884   STAT_LEAVE;
885 }
886
887 /*
888  * A graph was inlined
889  */
890 void stat_inline(ir_node *call, ir_graph *called_irg)
891 {
892   if (! status->enable)
893     return;
894
895   STAT_ENTER;
896   {
897     ir_graph *irg = get_irn_irg(call);
898     graph_entry_t *i_graph = graph_get_entry(called_irg, status->irg_hash);
899     graph_entry_t *graph   = graph_get_entry(irg, status->irg_hash);
900
901     cnt_inc(&graph->cnt_got_inlined);
902     cnt_inc(&i_graph->cnt_was_inlined);
903   }
904   STAT_LEAVE;
905 }
906
907 /*
908  * A graph with tail-recursions was optimized.
909  */
910 void stat_tail_rec(ir_graph *irg)
911 {
912   if (! status->enable)
913     return;
914
915   STAT_ENTER;
916   {
917   }
918   STAT_LEAVE;
919 }
920
921 /*
922  * Strength reduction was performed on an iteration variable.
923  */
924 void stat_strength_red(ir_graph *irg, ir_node *strong, ir_node *cmp)
925 {
926   if (! status->enable)
927     return;
928
929   STAT_ENTER;
930   {
931     graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
932     cnt_inc(&graph->cnt_strength_red);
933
934     removed_due_opt(strong, graph->opt_hash[STAT_OPT_STRENGTH_RED]);
935   }
936   STAT_LEAVE;
937 }
938
939 /*
940  * Start the dead node elimination.
941  */
942 void stat_dead_node_elim_start(ir_graph *irg)
943 {
944   if (! status->enable)
945     return;
946
947   ++status->in_dead_node_elim;
948 }
949
950 /*
951  * Stops the dead node elimination.
952  */
953 void stat_dead_node_elim_stop(ir_graph *irg)
954 {
955   if (! status->enable)
956     return;
957
958   --status->in_dead_node_elim;
959 }
960
961 /*
962  * A multiply was replaced by a series of Shifts/Adds/Subs
963  */
964 void stat_arch_dep_replace_mul_with_shifts(ir_node *mul)
965 {
966   if (! status->enable)
967     return;
968
969   STAT_ENTER;
970   {
971     graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
972     removed_due_opt(mul, graph->opt_hash[STAT_OPT_ARCH_DEP]);
973   }
974   STAT_LEAVE;
975 }
976
977 /**
978  * A division was replaced by a series of Shifts/Muls
979  */
980 void stat_arch_dep_replace_div_by_const(ir_node *div)
981 {
982   if (! status->enable)
983     return;
984
985   STAT_ENTER;
986   {
987     graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
988     removed_due_opt(div, graph->opt_hash[STAT_OPT_ARCH_DEP]);
989   }
990   STAT_LEAVE;
991 }
992
993 /**
994  * A modulo was replaced by a series of Shifts/Muls
995  */
996 void stat_arch_dep_replace_mod_by_const(ir_node *mod)
997 {
998   if (! status->enable)
999     return;
1000
1001   STAT_ENTER;
1002   {
1003     graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1004     removed_due_opt(mod, graph->opt_hash[STAT_OPT_ARCH_DEP]);
1005   }
1006   STAT_LEAVE;
1007 }
1008
1009 /**
1010  * A DivMod was replaced by a series of Shifts/Muls
1011  */
1012 void stat_arch_dep_replace_DivMod_by_const(ir_node *divmod)
1013 {
1014   if (! status->enable)
1015     return;
1016
1017   STAT_ENTER;
1018   {
1019     graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1020     removed_due_opt(divmod, graph->opt_hash[STAT_OPT_ARCH_DEP]);
1021   }
1022   STAT_LEAVE;
1023 }
1024
1025 /* Finish the statistics */
1026 void stat_finish(const char *name)
1027 {
1028   if (! status->enable)
1029     return;
1030
1031   STAT_ENTER;
1032   {
1033     graph_entry_t *entry;
1034     graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1035
1036     dump_init(name);
1037
1038     /* dump per graph */
1039     for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
1040
1041       if (entry->irg == NULL) {
1042         /* special entry for the global count */
1043         continue;
1044       }
1045
1046       if (! entry->is_deleted) {
1047         /* the graph is still alive, count the nodes on it */
1048         update_graph_stat(global, entry);
1049
1050         /* count the DAG's */
1051 //        count_dags_in_graph(global, entry);
1052
1053         /* calculate the pattern */
1054         stat_calc_pattern_history(entry->irg);
1055       }
1056
1057       dump_graph(entry);
1058
1059       /* clear the counter that are not accumulated */
1060       graph_clear_entry(entry, 0);
1061     }
1062
1063     /* dump global */
1064     dump_graph(global);
1065     dump_finish();
1066
1067     stat_finish_pattern_history();
1068
1069     /* clear the global counter here */
1070     {
1071       node_entry_t *entry;
1072
1073       for (entry = pset_first(global->opcode_hash); entry; entry = pset_next(global->opcode_hash)) {
1074         opcode_clear_entry(entry);
1075       }
1076       /* clear all global counter */
1077       graph_clear_entry(global, 1);
1078     }
1079
1080     /* finished */
1081 //    status->enable = 0;
1082   }
1083   STAT_LEAVE;
1084 }
1085
1086 #else
1087
1088 /* need this for prototypes */
1089 #define FIRM_STATISTICS
1090 #include "firmstat.h"
1091
1092 void init_stat(unsigned enable_options) {}
1093
1094 void stat_finish(const char *name) {}
1095
1096 void stat_new_ir_op(const ir_op *op) {}
1097
1098 void stat_free_ir_op(const ir_op *op) {}
1099
1100 void stat_new_node(ir_node *node) {}
1101
1102 void stat_turn_into_id(ir_node *node) {}
1103
1104 void stat_new_graph(ir_graph *irg, entity *ent) {}
1105
1106 void stat_free_graph(ir_graph *irg) {}
1107
1108 void stat_irg_walk(ir_graph *irg, void *pre, void *post) {}
1109
1110 void stat_irg_block_walk(ir_graph *irg, const ir_node *node, void *pre, void *post) {}
1111
1112 void stat_merge_nodes(
1113     ir_node **new_node_array, int new_num_entries,
1114     ir_node **old_node_array, int old_num_entries,
1115     stat_opt_kind opt) {}
1116
1117 void stat_reassociate(int start) {}
1118
1119 void stat_lower(ir_node *node) {}
1120
1121 void stat_inline(ir_node *call, ir_graph *irg) {}
1122
1123 void stat_tail_rec(ir_graph *irg) {}
1124
1125 void stat_strength_red(ir_graph *irg, ir_node *strong, ir_node *cmp) {}
1126
1127 void stat_dead_node_elim_start(ir_graph *irg) {}
1128
1129 void stat_dead_node_elim_stop(ir_graph *irg) {}
1130
1131 void stat_arch_dep_replace_mul_with_shifts(ir_node *mul) {}
1132
1133 void stat_arch_dep_replace_div_by_const(ir_node *div) {}
1134
1135 void stat_arch_dep_replace_mod_by_const(ir_node *mod) {}
1136
1137 void stat_arch_dep_replace_DivMod_by_const(ir_node *divmod) {}
1138
1139 #endif