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