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