2 * Copyright (C) 1995-2010 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Statistics for Firm.
23 * @author Michael Beck
28 #ifdef FIRM_STATISTICS
37 #include "firmstat_t.h"
47 * need this to be static:
48 * Special pseudo Opcodes that we need to count some interesting cases
52 * The Phi0, a node that is created during SSA construction
54 static ir_op _op_Phi0;
56 /** The PhiM, just to count memory Phi's. */
57 static ir_op _op_PhiM;
59 /** The Mul by Const node. */
60 static ir_op _op_MulC;
62 /** The Div by Const node. */
63 static ir_op _op_DivC;
65 /** The Div by Const node. */
66 static ir_op _op_ModC;
68 /** The Div by Const node. */
69 static ir_op _op_DivModC;
71 /** The Quot by Const node. */
72 static ir_op _op_QuotC;
74 /** The memory Proj node. */
75 static ir_op _op_ProjM;
78 static ir_op _op_SelSel;
80 /** A Sel of a Sel of a Sel */
81 static ir_op _op_SelSelSel;
83 /* ---------------------------------------------------------------------------------- */
85 /** Marks the begin of a statistic (hook) function. */
86 #define STAT_ENTER ++status->recursive
88 /** Marks the end of a statistic (hook) functions. */
89 #define STAT_LEAVE --status->recursive
91 /** Allows to enter a statistic function only when we are not already in a hook. */
92 #define STAT_ENTER_SINGLE do { if (status->recursive > 0) return; ++status->recursive; } while (0)
97 static const unsigned status_disable = 0;
98 static stat_info_t *status = (stat_info_t *)&status_disable;
101 * Compare two elements of the opcode hash.
103 static int opcode_cmp(const void *elt, const void *key)
105 const node_entry_t *e1 = (const node_entry_t*)elt;
106 const node_entry_t *e2 = (const node_entry_t*)key;
108 return e1->op->code - e2->op->code;
112 * Compare two elements of the graph hash.
114 static int graph_cmp(const void *elt, const void *key)
116 const graph_entry_t *e1 = (const graph_entry_t*)elt;
117 const graph_entry_t *e2 = (const graph_entry_t*)key;
119 return e1->irg != e2->irg;
123 * Compare two elements of the optimization hash.
125 static int opt_cmp(const void *elt, const void *key)
127 const opt_entry_t *e1 = (const opt_entry_t*)elt;
128 const opt_entry_t *e2 = (const opt_entry_t*)key;
130 return e1->op->code != e2->op->code;
134 * Compare two elements of the block/extbb hash.
136 static int block_cmp(const void *elt, const void *key)
138 const block_entry_t *e1 = (const block_entry_t*)elt;
139 const block_entry_t *e2 = (const block_entry_t*)key;
141 /* it's enough to compare the block number */
142 return e1->block_nr != e2->block_nr;
146 * Compare two elements of the be_block hash.
148 static int be_block_cmp(const void *elt, const void *key)
150 const be_block_entry_t *e1 = (const be_block_entry_t*)elt;
151 const be_block_entry_t *e2 = (const be_block_entry_t*)key;
153 return e1->block_nr != e2->block_nr;
157 * Compare two elements of reg pressure hash.
159 static int reg_pressure_cmp(const void *elt, const void *key)
161 const reg_pressure_entry_t *e1 = (const reg_pressure_entry_t*)elt;
162 const reg_pressure_entry_t *e2 = (const reg_pressure_entry_t*)key;
164 return e1->class_name != e2->class_name;
165 } /* reg_pressure_cmp */
168 * Compare two elements of the perm_stat hash.
170 static int perm_stat_cmp(const void *elt, const void *key)
172 const perm_stat_entry_t *e1 = (const perm_stat_entry_t*)elt;
173 const perm_stat_entry_t *e2 = (const perm_stat_entry_t*)key;
175 return e1->perm != e2->perm;
176 } /* perm_stat_cmp */
179 * Compare two elements of the perm_class hash.
181 static int perm_class_cmp(const void *elt, const void *key)
183 const perm_class_entry_t *e1 = (const perm_class_entry_t*)elt;
184 const perm_class_entry_t *e2 = (const perm_class_entry_t*)key;
186 return e1->class_name != e2->class_name;
187 } /* perm_class_cmp */
190 * Compare two elements of the ir_op hash.
192 static int opcode_cmp_2(const void *elt, const void *key)
194 const ir_op *e1 = (const ir_op*)elt;
195 const ir_op *e2 = (const ir_op*)key;
197 return e1->code != e2->code;
201 * Compare two elements of the address_mark set.
203 static int address_mark_cmp(const void *elt, const void *key, size_t size)
205 const address_mark_entry_t *e1 = (const address_mark_entry_t*)elt;
206 const address_mark_entry_t *e2 = (const address_mark_entry_t*)key;
209 /* compare only the nodes, the rest is used as data container */
210 return e1->node != e2->node;
211 } /* address_mark_cmp */
214 * Clear all counter in a node_entry_t.
216 static void opcode_clear_entry(node_entry_t *elem)
218 cnt_clr(&elem->cnt_alive);
219 cnt_clr(&elem->new_node);
220 cnt_clr(&elem->into_Id);
221 cnt_clr(&elem->normalized);
222 } /* opcode_clear_entry */
225 * Returns the associates node_entry_t for an ir_op (and allocates
226 * one if not yet available).
228 * @param op the IR operation
229 * @param hmap a hash map containing ir_op* -> node_entry_t*
231 static node_entry_t *opcode_get_entry(const ir_op *op, hmap_node_entry_t *hmap)
238 elem = (node_entry_t*)pset_find(hmap, &key, op->code);
242 elem = OALLOCZ(&status->cnts, node_entry_t);
245 opcode_clear_entry(elem);
249 return (node_entry_t*)pset_insert(hmap, elem, op->code);
250 } /* opcode_get_entry */
253 * Returns the associates ir_op for an opcode
255 * @param code the IR opcode
256 * @param hmap the hash map containing opcode -> ir_op*
258 static ir_op *opcode_find_entry(ir_opcode code, hmap_ir_op *hmap)
263 return (ir_op*)pset_find(hmap, &key, code);
264 } /* opcode_find_entry */
267 * Clears all counter in a graph_entry_t.
269 * @param elem the graph entry
270 * @param all if non-zero, clears all counters, else leave accumulated ones
272 static void graph_clear_entry(graph_entry_t *elem, int all)
276 /* clear accumulated / non-accumulated counter */
277 for (i = all ? 0 : _gcnt_non_acc; i < _gcnt_last; ++i) {
278 cnt_clr(&elem->cnt[i]);
281 if (elem->block_hash) {
282 del_pset(elem->block_hash);
283 elem->block_hash = NULL;
286 if (elem->extbb_hash) {
287 del_pset(elem->extbb_hash);
288 elem->extbb_hash = NULL;
291 obstack_free(&elem->recalc_cnts, NULL);
292 obstack_init(&elem->recalc_cnts);
293 } /* graph_clear_entry */
296 * Returns the associated graph_entry_t for an IR graph.
298 * @param irg the IR graph, NULL for the global counter
299 * @param hmap the hash map containing ir_graph* -> graph_entry_t*
301 static graph_entry_t *graph_get_entry(ir_graph *irg, hmap_graph_entry_t *hmap)
309 elem = (graph_entry_t*)pset_find(hmap, &key, HASH_PTR(irg));
312 /* create hash map backend block information */
313 if (! elem->be_block_hash)
314 elem->be_block_hash = new_pset(be_block_cmp, 5);
319 /* allocate a new one */
320 elem = OALLOCZ(&status->cnts, graph_entry_t);
321 obstack_init(&elem->recalc_cnts);
324 graph_clear_entry(elem, 1);
326 /* new hash table for opcodes here */
327 elem->opcode_hash = new_pset(opcode_cmp, 5);
328 elem->address_mark = new_set(address_mark_cmp, 5);
331 /* these hash tables are created on demand */
332 elem->block_hash = NULL;
333 elem->extbb_hash = NULL;
335 for (i = 0; i < sizeof(elem->opt_hash)/sizeof(elem->opt_hash[0]); ++i)
336 elem->opt_hash[i] = new_pset(opt_cmp, 4);
338 return (graph_entry_t*)pset_insert(hmap, elem, HASH_PTR(irg));
339 } /* graph_get_entry */
342 * Clear all counter in an opt_entry_t.
344 static void opt_clear_entry(opt_entry_t *elem)
346 cnt_clr(&elem->count);
347 } /* opt_clear_entry */
350 * Returns the associated opt_entry_t for an IR operation.
352 * @param op the IR operation
353 * @param hmap the hash map containing ir_op* -> opt_entry_t*
355 static opt_entry_t *opt_get_entry(const ir_op *op, hmap_opt_entry_t *hmap)
362 elem = (opt_entry_t*)pset_find(hmap, &key, op->code);
366 elem = OALLOCZ(&status->cnts, opt_entry_t);
368 /* clear new counter */
369 opt_clear_entry(elem);
373 return (opt_entry_t*)pset_insert(hmap, elem, op->code);
374 } /* opt_get_entry */
377 * clears all counter in a block_entry_t
379 static void block_clear_entry(block_entry_t *elem)
383 for (i = 0; i < _bcnt_last; ++i)
384 cnt_clr(&elem->cnt[i]);
385 } /* block_clear_entry */
388 * Returns the associated block_entry_t for an block.
390 * @param block_nr an IR block number
391 * @param hmap a hash map containing long -> block_entry_t
393 static block_entry_t *block_get_entry(struct obstack *obst, long block_nr, hmap_block_entry_t *hmap)
398 key.block_nr = block_nr;
400 elem = (block_entry_t*)pset_find(hmap, &key, block_nr);
404 elem = OALLOCZ(obst, block_entry_t);
406 /* clear new counter */
407 block_clear_entry(elem);
409 elem->block_nr = block_nr;
411 return (block_entry_t*)pset_insert(hmap, elem, block_nr);
412 } /* block_get_entry */
415 * Clear all sets in be_block_entry_t.
417 static void be_block_clear_entry(be_block_entry_t *elem)
419 if (elem->reg_pressure)
420 del_pset(elem->reg_pressure);
422 if (elem->sched_ready)
423 stat_delete_distrib_tbl(elem->sched_ready);
425 if (elem->perm_class_stat)
426 del_pset(elem->perm_class_stat);
428 elem->reg_pressure = new_pset(reg_pressure_cmp, 5);
429 elem->sched_ready = stat_new_int_distrib_tbl();
430 elem->perm_class_stat = new_pset(perm_class_cmp, 5);
431 } /* be_block_clear_entry */
434 * Returns the associated be_block_entry_t for an block.
436 * @param block_nr an IR block number
437 * @param hmap a hash map containing long -> be_block_entry_t
439 static be_block_entry_t *be_block_get_entry(struct obstack *obst, long block_nr, hmap_be_block_entry_t *hmap)
441 be_block_entry_t key;
442 be_block_entry_t *elem;
444 key.block_nr = block_nr;
446 elem = (be_block_entry_t*)pset_find(hmap, &key, block_nr);
450 elem = OALLOCZ(obst, be_block_entry_t);
452 /* clear new counter */
453 be_block_clear_entry(elem);
455 elem->block_nr = block_nr;
457 return (be_block_entry_t*)pset_insert(hmap, elem, block_nr);
458 } /* be_block_get_entry */
461 * clears all sets in perm_class_entry_t
463 static void perm_class_clear_entry(perm_class_entry_t *elem)
466 del_pset(elem->perm_stat);
468 elem->perm_stat = new_pset(perm_stat_cmp, 5);
469 } /* perm_class_clear_entry */
472 * Returns the associated perm_class entry for a register class.
474 * @param class_name the register class name
475 * @param hmap a hash map containing class_name -> perm_class_entry_t
477 static perm_class_entry_t *perm_class_get_entry(struct obstack *obst, const char *class_name,
478 hmap_perm_class_entry_t *hmap)
480 perm_class_entry_t key;
481 perm_class_entry_t *elem;
483 key.class_name = class_name;
485 elem = (perm_class_entry_t*)pset_find(hmap, &key, HASH_PTR(class_name));
489 elem = OALLOCZ(obst, perm_class_entry_t);
491 /* clear new counter */
492 perm_class_clear_entry(elem);
494 elem->class_name = class_name;
496 return (perm_class_entry_t*)pset_insert(hmap, elem, HASH_PTR(class_name));
497 } /* perm_class_get_entry */
500 * clears all sets in perm_stat_entry_t
502 static void perm_stat_clear_entry(perm_stat_entry_t *elem)
505 stat_delete_distrib_tbl(elem->chains);
508 stat_delete_distrib_tbl(elem->cycles);
510 elem->chains = stat_new_int_distrib_tbl();
511 elem->cycles = stat_new_int_distrib_tbl();
512 } /* perm_stat_clear_entry */
515 * Returns the associated perm_stat entry for a perm.
517 * @param perm the perm node
518 * @param hmap a hash map containing perm -> perm_stat_entry_t
520 static perm_stat_entry_t *perm_stat_get_entry(struct obstack *obst, ir_node *perm, hmap_perm_stat_entry_t *hmap)
522 perm_stat_entry_t key;
523 perm_stat_entry_t *elem;
527 elem = (perm_stat_entry_t*)pset_find(hmap, &key, HASH_PTR(perm));
531 elem = OALLOCZ(obst, perm_stat_entry_t);
533 /* clear new counter */
534 perm_stat_clear_entry(elem);
538 return (perm_stat_entry_t*)pset_insert(hmap, elem, HASH_PTR(perm));
539 } /* perm_stat_get_entry */
542 * Clear optimizations counter,
544 static void clear_optimization_counter(void)
547 for (i = 0; i < FS_OPT_MAX; ++i)
548 cnt_clr(&status->num_opts[i]);
552 * Returns the ir_op for an IR-node,
553 * handles special cases and return pseudo op codes.
555 * @param none an IR node
557 static ir_op *stat_get_irn_op(ir_node *node)
559 ir_op *op = get_irn_op(node);
560 unsigned opc = op->code;
564 if (get_irn_arity(node) == 0) {
565 /* special case, a Phi0 node, count on extra counter */
566 op = status->op_Phi0 ? status->op_Phi0 : op;
567 } else if (get_irn_mode(node) == mode_M) {
568 /* special case, a Memory Phi node, count on extra counter */
569 op = status->op_PhiM ? status->op_PhiM : op;
573 if (get_irn_mode(node) == mode_M) {
574 /* special case, a Memory Proj node, count on extra counter */
575 op = status->op_ProjM ? status->op_ProjM : op;
579 if (is_Const(get_Mul_left(node)) || is_Const(get_Mul_right(node))) {
580 /* special case, a Multiply by a const, count on extra counter */
581 op = status->op_MulC ? status->op_MulC : op;
585 if (is_Const(get_Div_right(node))) {
586 /* special case, a division by a const, count on extra counter */
587 op = status->op_DivC ? status->op_DivC : op;
591 if (is_Const(get_Mod_right(node))) {
592 /* special case, a module by a const, count on extra counter */
593 op = status->op_ModC ? status->op_ModC : op;
597 if (is_Const(get_DivMod_right(node))) {
598 /* special case, a division/modulo by a const, count on extra counter */
599 op = status->op_DivModC ? status->op_DivModC : op;
603 if (is_Const(get_Quot_right(node))) {
604 /* special case, a floating point division by a const, count on extra counter */
605 op = status->op_QuotC ? status->op_QuotC : op;
609 if (is_Sel(get_Sel_ptr(node))) {
610 /* special case, a Sel of a Sel, count on extra counter */
611 op = status->op_SelSel ? status->op_SelSel : op;
612 if (is_Sel(get_Sel_ptr(get_Sel_ptr(node)))) {
613 /* special case, a Sel of a Sel of a Sel, count on extra counter */
614 op = status->op_SelSelSel ? status->op_SelSelSel : op;
623 } /* stat_get_irn_op */
626 * update the block counter
628 static void undate_block_info(ir_node *node, graph_entry_t *graph)
630 ir_op *op = get_irn_op(node);
632 block_entry_t *b_entry;
635 /* check for block */
636 if (op == op_Block) {
637 arity = get_irn_arity(node);
638 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(node), graph->block_hash);
639 /* mark start end block to allow to filter them out */
640 if (node == get_irg_start_block(graph->irg))
641 b_entry->is_start = 1;
642 else if (node == get_irg_end_block(graph->irg))
645 /* count all incoming edges */
646 for (i = 0; i < arity; ++i) {
647 ir_node *pred = get_irn_n(node, i);
648 ir_node *other_block = get_nodes_block(pred);
649 block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
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]);
657 block = get_nodes_block(node);
658 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(block), graph->block_hash);
660 if (op == op_Phi && mode_is_datab(get_irn_mode(node))) {
661 /* count data Phi per block */
662 cnt_inc(&b_entry->cnt[bcnt_phi_data]);
665 /* we have a new node in our block */
666 cnt_inc(&b_entry->cnt[bcnt_nodes]);
668 /* don't count keep-alive edges */
672 arity = get_irn_arity(node);
674 for (i = 0; i < arity; ++i) {
675 ir_node *pred = get_irn_n(node, i);
676 ir_node *other_block;
678 other_block = get_nodes_block(pred);
680 if (other_block == block)
681 cnt_inc(&b_entry->cnt[bcnt_edges]); /* a in block edge */
683 block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
685 cnt_inc(&b_entry->cnt[bcnt_in_edges]); /* an edge coming from another block */
686 cnt_inc(&b_entry_other->cnt[bcnt_out_edges]);
689 } /* undate_block_info */
692 * Update the extended block counter.
694 static void update_extbb_info(ir_node *node, graph_entry_t *graph)
696 ir_op *op = get_irn_op(node);
698 extbb_entry_t *eb_entry;
701 /* check for block */
702 if (op == op_Block) {
703 extbb = get_nodes_extbb(node);
704 arity = get_irn_arity(node);
705 eb_entry = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(extbb), graph->extbb_hash);
707 /* count all incoming edges */
708 for (i = 0; i < arity; ++i) {
709 ir_node *pred = get_irn_n(node, i);
710 ir_extblk *other_extbb = get_nodes_extbb(pred);
712 if (extbb != other_extbb) {
713 extbb_entry_t *eb_entry_other = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(other_extbb), graph->extbb_hash);
715 cnt_inc(&eb_entry->cnt[bcnt_in_edges]); /* an edge coming from another extbb */
716 cnt_inc(&eb_entry_other->cnt[bcnt_out_edges]);
722 extbb = get_nodes_extbb(node);
723 eb_entry = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(extbb), graph->extbb_hash);
725 if (op == op_Phi && mode_is_datab(get_irn_mode(node))) {
726 /* count data Phi per extbb */
727 cnt_inc(&eb_entry->cnt[bcnt_phi_data]);
730 /* we have a new node in our block */
731 cnt_inc(&eb_entry->cnt[bcnt_nodes]);
733 /* don't count keep-alive edges */
737 arity = get_irn_arity(node);
739 for (i = 0; i < arity; ++i) {
740 ir_node *pred = get_irn_n(node, i);
741 ir_extblk *other_extbb = get_nodes_extbb(pred);
743 if (other_extbb == extbb)
744 cnt_inc(&eb_entry->cnt[bcnt_edges]); /* a in extbb edge */
746 extbb_entry_t *eb_entry_other = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(other_extbb), graph->extbb_hash);
748 cnt_inc(&eb_entry->cnt[bcnt_in_edges]); /* an edge coming from another extbb */
749 cnt_inc(&eb_entry_other->cnt[bcnt_out_edges]);
752 } /* update_extbb_info */
755 * Calculates how many arguments of the call are const, updates
756 * param distribution.
758 static void analyse_params_of_Call(graph_entry_t *graph, ir_node *call)
760 int i, num_const_args = 0, num_local_adr = 0;
761 int n = get_Call_n_params(call);
763 for (i = 0; i < n; ++i) {
764 ir_node *param = get_Call_param(call, i);
766 if (is_irn_constlike(param))
768 else if (is_Sel(param)) {
769 ir_node *base = param;
772 base = get_Sel_ptr(base);
773 } while (is_Sel(base));
775 if (base == get_irg_frame(current_ir_graph))
781 if (num_const_args > 0)
782 cnt_inc(&graph->cnt[gcnt_call_with_cnst_arg]);
783 if (num_const_args == n)
784 cnt_inc(&graph->cnt[gcnt_call_with_all_cnst_arg]);
785 if (num_local_adr > 0)
786 cnt_inc(&graph->cnt[gcnt_call_with_local_adr]);
788 stat_inc_int_distrib_tbl(status->dist_param_cnt, n);
789 } /* analyse_params_of_Call */
792 * Update info on calls.
794 * @param call The call
795 * @param graph The graph entry containing the call
797 static void stat_update_call(ir_node *call, graph_entry_t *graph)
799 ir_node *block = get_nodes_block(call);
800 ir_node *ptr = get_Call_ptr(call);
801 ir_entity *ent = NULL;
802 ir_graph *callee = NULL;
805 * If the block is bad, the whole subgraph will collapse later
806 * so do not count this call.
807 * This happens in dead code.
812 cnt_inc(&graph->cnt[gcnt_all_calls]);
814 /* found a call, this function is not a leaf */
817 if (is_SymConst(ptr)) {
818 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
819 /* ok, we seems to know the entity */
820 ent = get_SymConst_entity(ptr);
821 callee = get_entity_irg(ent);
823 /* it is recursive, if it calls at least once */
824 if (callee == graph->irg)
825 graph->is_recursive = 1;
827 cnt_inc(&graph->cnt[gcnt_external_calls]);
830 /* indirect call, be could not predict */
831 cnt_inc(&graph->cnt[gcnt_indirect_calls]);
833 /* NOT a leaf call */
834 graph->is_leaf_call = LCS_NON_LEAF_CALL;
837 /* check, if it's a chain-call: Then, the call-block
838 * must dominate the end block. */
840 ir_node *curr = get_irg_end_block(graph->irg);
841 int depth = get_Block_dom_depth(block);
843 for (; curr != block && get_Block_dom_depth(curr) > depth;) {
844 curr = get_Block_idom(curr);
846 if (! curr || !is_Block(curr))
851 graph->is_chain_call = 0;
854 /* check, if the callee is a leaf */
856 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
858 if (called->is_analyzed) {
859 if (! called->is_leaf)
860 graph->is_leaf_call = LCS_NON_LEAF_CALL;
864 analyse_params_of_Call(graph, call);
865 } /* stat_update_call */
868 * Update info on calls for graphs on the wait queue.
870 static void stat_update_call_2(ir_node *call, graph_entry_t *graph)
872 ir_node *block = get_nodes_block(call);
873 ir_node *ptr = get_Call_ptr(call);
874 ir_entity *ent = NULL;
875 ir_graph *callee = NULL;
878 * If the block is bad, the whole subgraph will collapse later
879 * so do not count this call.
880 * This happens in dead code.
885 if (is_SymConst(ptr)) {
886 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
887 /* ok, we seems to know the entity */
888 ent = get_SymConst_entity(ptr);
889 callee = get_entity_irg(ent);
893 /* check, if the callee is a leaf */
895 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
897 assert(called->is_analyzed);
899 if (! called->is_leaf)
900 graph->is_leaf_call = LCS_NON_LEAF_CALL;
902 graph->is_leaf_call = LCS_NON_LEAF_CALL;
903 } /* stat_update_call_2 */
906 * Find the base address and entity of an Sel node.
908 * @param sel the node
910 * @return the base address.
912 static ir_node *find_base_adr(ir_node *sel)
914 ir_node *ptr = get_Sel_ptr(sel);
916 while (is_Sel(ptr)) {
918 ptr = get_Sel_ptr(sel);
921 } /* find_base_adr */
924 * Update info on Load/Store address statistics.
926 static void stat_update_address(ir_node *node, graph_entry_t *graph)
928 unsigned opc = get_irn_opcode(node);
934 /* a global address */
935 cnt_inc(&graph->cnt[gcnt_global_adr]);
938 base = find_base_adr(node);
939 irg = current_ir_graph;
940 if (base == get_irg_tls(irg)) {
941 /* a TLS variable, like a global. */
942 cnt_inc(&graph->cnt[gcnt_global_adr]);
943 } else if (base == get_irg_frame(irg)) {
944 /* a local Variable. */
945 cnt_inc(&graph->cnt[gcnt_local_adr]);
948 if (is_Proj(base) && skip_Proj(get_Proj_pred(base)) == get_irg_start(irg)) {
949 /* pointer access through parameter, check for THIS */
950 ir_entity *ent = get_irg_entity(irg);
953 ir_type *ent_tp = get_entity_type(ent);
955 if (get_method_calling_convention(ent_tp) & cc_this_call) {
956 if (get_Proj_proj(base) == 0) {
958 cnt_inc(&graph->cnt[gcnt_this_adr]);
963 /* other parameter */
964 cnt_inc(&graph->cnt[gcnt_param_adr]);
967 /* unknown Pointer access */
968 cnt_inc(&graph->cnt[gcnt_other_adr]);
974 } /* stat_update_address */
977 * Walker for reachable nodes count.
979 static void update_node_stat(ir_node *node, void *env)
981 graph_entry_t *graph = (graph_entry_t*)env;
984 ir_op *op = stat_get_irn_op(node);
985 int i, arity = get_irn_arity(node);
987 entry = opcode_get_entry(op, graph->opcode_hash);
989 cnt_inc(&entry->cnt_alive);
990 cnt_add_i(&graph->cnt[gcnt_edges], arity);
992 /* count block edges */
993 undate_block_info(node, graph);
995 /* count extended block edges */
996 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
997 if (graph->irg != get_const_code_irg())
998 update_extbb_info(node, graph);
1001 /* handle statistics for special node types */
1005 /* check for properties that depends on calls like recursion/leaf/indirect call */
1006 stat_update_call(node, graph);
1009 /* check address properties */
1010 stat_update_address(get_Load_ptr(node), graph);
1013 /* check address properties */
1014 stat_update_address(get_Store_ptr(node), graph);
1017 /* check for non-strict Phi nodes */
1018 for (i = arity - 1; i >= 0; --i) {
1019 ir_node *pred = get_Phi_pred(node, i);
1020 if (is_Unknown(pred)) {
1021 /* found an Unknown predecessor, graph is not strict */
1022 graph->is_strict = 0;
1030 /* we want to count the constant IN nodes, not the CSE'ed constant's itself */
1031 if (status->stat_options & FIRMSTAT_COUNT_CONSTS) {
1034 for (i = get_irn_arity(node) - 1; i >= 0; --i) {
1035 ir_node *pred = get_irn_n(node, i);
1037 if (is_Const(pred)) {
1038 /* check properties of constants */
1039 stat_update_const(status, pred, graph);
1043 } /* update_node_stat */
1046 * Walker for reachable nodes count for graphs on the wait_q.
1048 static void update_node_stat_2(ir_node *node, void *env)
1050 graph_entry_t *graph = (graph_entry_t*)env;
1052 /* check for properties that depends on calls like recursion/leaf/indirect call */
1054 stat_update_call_2(node, graph);
1055 } /* update_node_stat_2 */
1058 * Get the current address mark.
1060 static unsigned get_adr_mark(graph_entry_t *graph, ir_node *node)
1062 address_mark_entry_t *value = (address_mark_entry_t*)set_find(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
1064 return value ? value->mark : 0;
1065 } /* get_adr_mark */
1068 * Set the current address mark.
1070 static void set_adr_mark(graph_entry_t *graph, ir_node *node, unsigned val)
1072 address_mark_entry_t *value = (address_mark_entry_t*)set_insert(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
1075 } /* set_adr_mark */
1077 #undef DUMP_ADR_MODE
1079 #ifdef DUMP_ADR_MODE
1081 * a vcg attribute hook: Color a node with a different color if
1082 * it's identified as a part of an address expression or at least referenced
1083 * by an address expression.
1085 static int stat_adr_mark_hook(FILE *F, ir_node *node, ir_node *local)
1087 ir_node *n = local ? local : node;
1088 ir_graph *irg = get_irn_irg(n);
1089 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1090 unsigned mark = get_adr_mark(graph, n);
1092 if (mark & MARK_ADDRESS_CALC)
1093 fprintf(F, "color: purple");
1094 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1095 fprintf(F, "color: pink");
1096 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1097 fprintf(F, "color: lightblue");
1101 /* I know the color! */
1103 } /* stat_adr_mark_hook */
1104 #endif /* DUMP_ADR_MODE */
1107 * Return the "operational" mode of a Firm node.
1109 static ir_mode *get_irn_op_mode(ir_node *node)
1111 switch (get_irn_opcode(node)) {
1113 return get_Load_mode(node);
1115 return get_irn_mode(get_Store_value(node));
1117 return get_irn_mode(get_DivMod_left(node));
1119 return get_irn_mode(get_Div_left(node));
1121 return get_irn_mode(get_Mod_left(node));
1123 /* Cmp is no address calculation, or is it? */
1125 return get_irn_mode(node);
1127 } /* get_irn_op_mode */
1130 * Post-walker that marks every node that is an address calculation.
1132 * Users of a node must be visited first. We ensure this by
1133 * calling it in the post of an outs walk. This should work even in cycles,
1134 * while the normal pre-walk will not.
1136 static void mark_address_calc(ir_node *node, void *env)
1138 graph_entry_t *graph = (graph_entry_t*)env;
1139 ir_mode *mode = get_irn_op_mode(node);
1141 unsigned mark_preds = MARK_REF_NON_ADR;
1143 if (! mode_is_data(mode))
1146 if (mode_is_reference(mode)) {
1147 /* a reference is calculated here, we are sure */
1148 set_adr_mark(graph, node, MARK_ADDRESS_CALC);
1150 mark_preds = MARK_REF_ADR;
1152 unsigned mark = get_adr_mark(graph, node);
1154 if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR) {
1156 * this node has no reference mode, but is only
1157 * referenced by address calculations
1159 mark_preds = MARK_REF_ADR;
1163 /* mark all predecessors */
1164 for (i = 0, n = get_irn_arity(node); i < n; ++i) {
1165 ir_node *pred = get_irn_n(node, i);
1167 mode = get_irn_op_mode(pred);
1168 if (! mode_is_data(mode))
1171 set_adr_mark(graph, pred, get_adr_mark(graph, pred) | mark_preds);
1173 } /* mark_address_calc */
1176 * Post-walker that marks every node that is an address calculation.
1178 * Users of a node must be visited first. We ensure this by
1179 * calling it in the post of an outs walk. This should work even in cycles,
1180 * while the normal pre-walk will not.
1182 static void count_adr_ops(ir_node *node, void *env)
1184 graph_entry_t *graph = (graph_entry_t*)env;
1185 unsigned mark = get_adr_mark(graph, node);
1187 if (mark & MARK_ADDRESS_CALC)
1188 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1189 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1190 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1191 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1192 cnt_inc(&graph->cnt[gcnt_all_adr_ops]);
1193 } /* count_adr_ops */
1196 * Called for every graph when the graph is either deleted or stat_dump_snapshot()
1197 * is called, must recalculate all statistic info.
1199 * @param global The global entry
1200 * @param graph The current entry
1202 static void update_graph_stat(graph_entry_t *global, graph_entry_t *graph)
1204 node_entry_t *entry;
1207 /* clear first the alive counter in the graph */
1208 foreach_pset(graph->opcode_hash, node_entry_t*, entry) {
1209 cnt_clr(&entry->cnt_alive);
1210 } /* foreach_pset */
1212 /* set pessimistic values */
1214 graph->is_leaf_call = LCS_UNKNOWN;
1215 graph->is_recursive = 0;
1216 graph->is_chain_call = 1;
1217 graph->is_strict = 1;
1219 /* create new block counter */
1220 graph->block_hash = new_pset(block_cmp, 5);
1222 /* we need dominator info */
1223 if (graph->irg != get_const_code_irg()) {
1224 assure_doms(graph->irg);
1226 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
1227 /* we need extended basic blocks */
1228 compute_extbb(graph->irg);
1230 /* create new extbb counter */
1231 graph->extbb_hash = new_pset(block_cmp, 5);
1235 /* count the nodes in the graph */
1236 irg_walk_graph(graph->irg, update_node_stat, NULL, graph);
1239 /* Uncomment this code if chain-call means call exact one. */
1240 entry = opcode_get_entry(op_Call, graph->opcode_hash);
1242 /* check if we have more than 1 call */
1243 if (cnt_gt(entry->cnt_alive, 1))
1244 graph->is_chain_call = 0;
1247 /* recursive functions are never chain calls, leafs don't have calls */
1248 if (graph->is_recursive || graph->is_leaf)
1249 graph->is_chain_call = 0;
1251 /* assume we walk every graph only ONCE, we could sum here the global count */
1252 foreach_pset(graph->opcode_hash, node_entry_t*, entry) {
1253 node_entry_t *g_entry = opcode_get_entry(entry->op, global->opcode_hash);
1255 /* update the node counter */
1256 cnt_add(&g_entry->cnt_alive, &entry->cnt_alive);
1257 } /* foreach_pset */
1259 /* count the number of address calculation */
1260 if (graph->irg != get_const_code_irg()) {
1261 ir_graph *rem = current_ir_graph;
1263 assure_irg_outs(graph->irg);
1265 /* Must be done an the outs graph */
1266 current_ir_graph = graph->irg;
1267 irg_out_walk(get_irg_start(graph->irg), NULL, mark_address_calc, graph);
1268 current_ir_graph = rem;
1270 #ifdef DUMP_ADR_MODE
1271 /* register the vcg hook and dump the graph for test */
1272 set_dump_node_vcgattr_hook(stat_adr_mark_hook);
1273 dump_ir_block_graph(graph->irg, "-adr");
1274 set_dump_node_vcgattr_hook(NULL);
1275 #endif /* DUMP_ADR_MODE */
1277 irg_walk_graph(graph->irg, NULL, count_adr_ops, graph);
1280 /* count the DAG's */
1281 if (status->stat_options & FIRMSTAT_COUNT_DAG)
1282 count_dags_in_graph(global, graph);
1284 /* calculate the patterns of this graph */
1285 stat_calc_pattern_history(graph->irg);
1287 /* leaf function did not call others */
1289 graph->is_leaf_call = LCS_NON_LEAF_CALL;
1290 else if (graph->is_leaf_call == LCS_UNKNOWN) {
1291 /* we still don't know if this graph calls leaf-functions, so enqueue */
1292 pdeq_putl(status->wait_q, graph);
1295 /* we have analyzed this graph */
1296 graph->is_analyzed = 1;
1298 /* accumulate all counter's */
1299 for (i = 0; i < _gcnt_last; ++i)
1300 cnt_add(&global->cnt[i], &graph->cnt[i]);
1301 } /* update_graph_stat */
1304 * Called for every graph that was on the wait_q in stat_dump_snapshot()
1305 * must finish all statistic info calculations.
1307 * @param global The global entry
1308 * @param graph The current entry
1310 static void update_graph_stat_2(graph_entry_t *global, graph_entry_t *graph)
1313 if (graph->is_deleted) {
1314 /* deleted, ignore */
1319 /* count the nodes in the graph */
1320 irg_walk_graph(graph->irg, update_node_stat_2, NULL, graph);
1322 if (graph->is_leaf_call == LCS_UNKNOWN)
1323 graph->is_leaf_call = LCS_LEAF_CALL;
1325 } /* update_graph_stat_2 */
1328 * Register a dumper.
1330 static void stat_register_dumper(const dumper_t *dumper)
1332 dumper_t *p = XMALLOC(dumper_t);
1334 memcpy(p, dumper, sizeof(*p));
1336 p->next = status->dumper;
1340 /* FIXME: memory leak */
1341 } /* stat_register_dumper */
1344 * Dumps the statistics of an IR graph.
1346 static void stat_dump_graph(graph_entry_t *entry)
1350 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1351 if (dumper->dump_graph)
1352 dumper->dump_graph(dumper, entry);
1354 } /* stat_dump_graph */
1357 * Calls all registered dumper functions.
1359 static void stat_dump_registered(graph_entry_t *entry)
1363 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1364 if (dumper->func_map) {
1365 dump_graph_FUNC func;
1367 foreach_pset(dumper->func_map, dump_graph_FUNC, func)
1368 func(dumper, entry);
1371 } /* stat_dump_registered */
1374 * Dumps a constant table.
1376 static void stat_dump_consts(const constant_info_t *tbl)
1380 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1381 if (dumper->dump_const_tbl)
1382 dumper->dump_const_tbl(dumper, tbl);
1384 } /* stat_dump_consts */
1387 * Dumps the parameter distribution
1389 static void stat_dump_param_tbl(const distrib_tbl_t *tbl, graph_entry_t *global)
1393 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1394 if (dumper->dump_param_tbl)
1395 dumper->dump_param_tbl(dumper, tbl, global);
1397 } /* stat_dump_param_tbl */
1400 * Dumps the optimization counter
1402 static void stat_dump_opt_cnt(const counter_t *tbl, unsigned len)
1406 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1407 if (dumper->dump_opt_cnt)
1408 dumper->dump_opt_cnt(dumper, tbl, len);
1410 } /* stat_dump_opt_cnt */
1413 * Initialize the dumper.
1415 static void stat_dump_init(const char *name)
1419 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1421 dumper->init(dumper, name);
1423 } /* stat_dump_init */
1426 * Finish the dumper.
1428 static void stat_dump_finish(void)
1432 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1434 dumper->finish(dumper);
1436 } /* stat_dump_finish */
1439 * Register an additional function for all dumper.
1441 void stat_register_dumper_func(dump_graph_FUNC func)
1445 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1446 if (! dumper->func_map)
1447 dumper->func_map = pset_new_ptr(3);
1448 pset_insert_ptr(dumper->func_map, (void*)func);
1450 } /* stat_register_dumper_func */
1452 /* ---------------------------------------------------------------------- */
1455 * Helper: get an ir_op from an opcode.
1457 ir_op *stat_get_op_from_opcode(ir_opcode code)
1459 return opcode_find_entry(code, status->ir_op_hash);
1460 } /* stat_get_op_from_opcode */
1463 * Hook: A new IR op is registered.
1465 * @param ctx the hook context
1466 * @param op the new IR opcode that was created.
1468 static void stat_new_ir_op(void *ctx, ir_op *op)
1471 if (! status->stat_options)
1476 graph_entry_t *graph = graph_get_entry(NULL, status->irg_hash);
1478 /* execute for side effect :-) */
1479 (void)opcode_get_entry(op, graph->opcode_hash);
1481 pset_insert(status->ir_op_hash, op, op->code);
1484 } /* stat_new_ir_op */
1487 * Hook: An IR op is freed.
1489 * @param ctx the hook context
1490 * @param op the IR opcode that is freed
1492 static void stat_free_ir_op(void *ctx, ir_op *op)
1496 if (! status->stat_options)
1503 } /* stat_free_ir_op */
1506 * Hook: A new node is created.
1508 * @param ctx the hook context
1509 * @param irg the IR graph on which the node is created
1510 * @param node the new IR node that was created
1512 static void stat_new_node(void *ctx, ir_graph *irg, ir_node *node)
1516 if (! status->stat_options)
1519 /* do NOT count during dead node elimination */
1520 if (status->in_dead_node_elim)
1525 node_entry_t *entry;
1526 graph_entry_t *graph;
1527 ir_op *op = stat_get_irn_op(node);
1529 /* increase global value */
1530 graph = graph_get_entry(NULL, status->irg_hash);
1531 entry = opcode_get_entry(op, graph->opcode_hash);
1532 cnt_inc(&entry->new_node);
1534 /* increase local value */
1535 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1536 entry = opcode_get_entry(op, graph->opcode_hash);
1537 cnt_inc(&entry->new_node);
1540 } /* stat_new_node */
1543 * Hook: A node is changed into a Id node
1545 * @param ctx the hook context
1546 * @param node the IR node that will be turned into an ID
1548 static void stat_turn_into_id(void *ctx, ir_node *node)
1551 if (! status->stat_options)
1556 node_entry_t *entry;
1557 graph_entry_t *graph;
1558 ir_op *op = stat_get_irn_op(node);
1560 /* increase global value */
1561 graph = graph_get_entry(NULL, status->irg_hash);
1562 entry = opcode_get_entry(op, graph->opcode_hash);
1563 cnt_inc(&entry->into_Id);
1565 /* increase local value */
1566 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1567 entry = opcode_get_entry(op, graph->opcode_hash);
1568 cnt_inc(&entry->into_Id);
1571 } /* stat_turn_into_id */
1574 * Hook: A node is normalized
1576 * @param ctx the hook context
1577 * @param node the IR node that was normalized
1579 static void stat_normalize(void *ctx, ir_node *node)
1582 if (! status->stat_options)
1587 node_entry_t *entry;
1588 graph_entry_t *graph;
1589 ir_op *op = stat_get_irn_op(node);
1591 /* increase global value */
1592 graph = graph_get_entry(NULL, status->irg_hash);
1593 entry = opcode_get_entry(op, graph->opcode_hash);
1594 cnt_inc(&entry->normalized);
1596 /* increase local value */
1597 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1598 entry = opcode_get_entry(op, graph->opcode_hash);
1599 cnt_inc(&entry->normalized);
1602 } /* stat_normalize */
1605 * Hook: A new graph was created
1607 * @param ctx the hook context
1608 * @param irg the new IR graph that was created
1609 * @param ent the entity of this graph
1611 static void stat_new_graph(void *ctx, ir_graph *irg, ir_entity *ent)
1614 if (! status->stat_options)
1619 /* execute for side effect :-) */
1620 graph_entry_t * graph = graph_get_entry(irg, status->irg_hash);
1623 graph->is_deleted = 0;
1625 graph->is_leaf_call = 0;
1626 graph->is_recursive = 0;
1627 graph->is_chain_call = 0;
1628 graph->is_strict = 1;
1629 graph->is_analyzed = 0;
1632 } /* stat_new_graph */
1635 * Hook: A graph will be deleted
1637 * @param ctx the hook context
1638 * @param irg the IR graph that will be deleted
1640 * Note that we still hold the information for this graph
1641 * in our hash maps, only a flag is set which prevents this
1642 * information from being changed, it's "frozen" from now.
1644 static void stat_free_graph(void *ctx, ir_graph *irg)
1647 if (! status->stat_options)
1652 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1653 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1655 graph->is_deleted = 1;
1657 if (status->stat_options & FIRMSTAT_COUNT_DELETED) {
1658 /* count the nodes of the graph yet, it will be destroyed later */
1659 update_graph_stat(global, graph);
1663 } /* stat_free_graph */
1666 * Hook: A walk over a graph is initiated. Do not count walks from statistic code.
1668 * @param ctx the hook context
1669 * @param irg the IR graph that will be walked
1670 * @param pre the pre walker
1671 * @param post the post walker
1673 static void stat_irg_walk(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1678 if (! status->stat_options)
1683 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1685 cnt_inc(&graph->cnt[gcnt_acc_walked]);
1688 } /* stat_irg_walk */
1691 * Hook: A walk over a graph in block-wise order is initiated. Do not count walks from statistic code.
1693 * @param ctx the hook context
1694 * @param irg the IR graph that will be walked
1695 * @param pre the pre walker
1696 * @param post the post walker
1698 static void stat_irg_walk_blkwise(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1700 /* for now, do NOT differentiate between blockwise and normal */
1701 stat_irg_walk(ctx, irg, pre, post);
1702 } /* stat_irg_walk_blkwise */
1705 * Hook: A walk over the graph's blocks is initiated. Do not count walks from statistic code.
1707 * @param ctx the hook context
1708 * @param irg the IR graph that will be walked
1709 * @param node the IR node
1710 * @param pre the pre walker
1711 * @param post the post walker
1713 static void stat_irg_block_walk(void *ctx, ir_graph *irg, ir_node *node, generic_func *pre, generic_func *post)
1719 if (! status->stat_options)
1724 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1726 cnt_inc(&graph->cnt[gcnt_acc_walked_blocks]);
1729 } /* stat_irg_block_walk */
1732 * Called for every node that is removed due to an optimization.
1734 * @param n the IR node that will be removed
1735 * @param hmap the hash map containing ir_op* -> opt_entry_t*
1736 * @param kind the optimization kind
1738 static void removed_due_opt(ir_node *n, hmap_opt_entry_t *hmap, hook_opt_kind kind)
1741 ir_op *op = stat_get_irn_op(n);
1743 /* ignore CSE for Constants */
1744 if (kind == HOOK_OPT_CSE && (is_Const(n) || is_SymConst(n)))
1747 /* increase global value */
1748 entry = opt_get_entry(op, hmap);
1749 cnt_inc(&entry->count);
1750 } /* removed_due_opt */
1753 * Hook: Some nodes were optimized into some others due to an optimization.
1755 * @param ctx the hook context
1757 static void stat_merge_nodes(
1759 ir_node **new_node_array, int new_num_entries,
1760 ir_node **old_node_array, int old_num_entries,
1764 if (! status->stat_options)
1770 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1772 cnt_inc(&status->num_opts[opt]);
1773 if (status->reassoc_run)
1774 opt = HOOK_OPT_REASSOC;
1776 for (i = 0; i < old_num_entries; ++i) {
1777 /* nodes might be in new and old, so if we found a node
1778 in both sets, this one is NOT removed */
1779 for (j = 0; j < new_num_entries; ++j) {
1780 if (old_node_array[i] == new_node_array[j])
1783 if (j >= new_num_entries) {
1786 /* sometimes we did not detect, that it is replaced by a Const */
1787 if (opt == HOOK_OPT_CONFIRM && new_num_entries == 1) {
1788 ir_op *op = get_irn_op(new_node_array[0]);
1790 if (op == op_Const || op == op_SymConst)
1791 xopt = HOOK_OPT_CONFIRM_C;
1794 removed_due_opt(old_node_array[i], graph->opt_hash[xopt], (hook_opt_kind)xopt);
1799 } /* stat_merge_nodes */
1802 * Hook: Reassociation is started/stopped.
1804 * @param ctx the hook context
1805 * @param flag if non-zero, reassociation is started else stopped
1807 static void stat_reassociate(void *ctx, int flag)
1810 if (! status->stat_options)
1815 status->reassoc_run = flag;
1818 } /* stat_reassociate */
1821 * Hook: A node was lowered into other nodes
1823 * @param ctx the hook context
1824 * @param node the IR node that will be lowered
1826 static void stat_lower(void *ctx, ir_node *node)
1829 if (! status->stat_options)
1834 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1836 removed_due_opt(node, graph->opt_hash[HOOK_LOWERED], HOOK_LOWERED);
1842 * Hook: A graph was inlined.
1844 * @param ctx the hook context
1845 * @param call the IR call that will re changed into the body of
1846 * the called IR graph
1847 * @param called_irg the IR graph representing the called routine
1849 static void stat_inline(void *ctx, ir_node *call, ir_graph *called_irg)
1852 if (! status->stat_options)
1857 ir_graph *irg = get_irn_irg(call);
1858 graph_entry_t *i_graph = graph_get_entry(called_irg, status->irg_hash);
1859 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1861 cnt_inc(&graph->cnt[gcnt_acc_got_inlined]);
1862 cnt_inc(&i_graph->cnt[gcnt_acc_was_inlined]);
1868 * Hook: A graph with tail-recursions was optimized.
1870 * @param ctx the hook context
1872 static void stat_tail_rec(void *ctx, ir_graph *irg, int n_calls)
1875 if (! status->stat_options)
1880 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1882 graph->num_tail_recursion += n_calls;
1885 } /* stat_tail_rec */
1888 * Strength reduction was performed on an iteration variable.
1890 * @param ctx the hook context
1892 static void stat_strength_red(void *ctx, ir_graph *irg, ir_node *strong)
1895 if (! status->stat_options)
1900 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1901 cnt_inc(&graph->cnt[gcnt_acc_strength_red]);
1903 removed_due_opt(strong, graph->opt_hash[HOOK_OPT_STRENGTH_RED], HOOK_OPT_STRENGTH_RED);
1906 } /* stat_strength_red */
1909 * Hook: Start/Stop the dead node elimination.
1911 * @param ctx the hook context
1913 static void stat_dead_node_elim(void *ctx, ir_graph *irg, int start)
1917 if (! status->stat_options)
1920 status->in_dead_node_elim = (start != 0);
1921 } /* stat_dead_node_elim */
1924 * Hook: if-conversion was tried.
1926 static void stat_if_conversion(void *context, ir_graph *irg, ir_node *phi,
1927 int pos, ir_node *mux, if_result_t reason)
1933 if (! status->stat_options)
1938 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1940 cnt_inc(&graph->cnt[gcnt_if_conv + reason]);
1943 } /* stat_if_conversion */
1946 * Hook: real function call was optimized.
1948 static void stat_func_call(void *context, ir_graph *irg, ir_node *call)
1952 if (! status->stat_options)
1957 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1959 cnt_inc(&graph->cnt[gcnt_acc_real_func_call]);
1962 } /* stat_func_call */
1965 * Hook: A multiply was replaced by a series of Shifts/Adds/Subs.
1967 * @param ctx the hook context
1969 static void stat_arch_dep_replace_mul_with_shifts(void *ctx, ir_node *mul)
1972 if (! status->stat_options)
1977 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1978 removed_due_opt(mul, graph->opt_hash[HOOK_OPT_ARCH_DEP], HOOK_OPT_ARCH_DEP);
1981 } /* stat_arch_dep_replace_mul_with_shifts */
1984 * Hook: A division by const was replaced.
1986 * @param ctx the hook context
1987 * @param node the division node that will be optimized
1989 static void stat_arch_dep_replace_division_by_const(void *ctx, ir_node *node)
1992 if (! status->stat_options)
1997 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1998 removed_due_opt(node, graph->opt_hash[HOOK_OPT_ARCH_DEP], HOOK_OPT_ARCH_DEP);
2001 } /* stat_arch_dep_replace_division_by_const */
2004 * Update the register pressure of a block.
2006 * @param irg the irg containing the block
2007 * @param block the block for which the reg pressure should be set
2008 * @param pressure the pressure
2009 * @param class_name the name of the register class
2011 void stat_be_block_regpressure(ir_graph *irg, ir_node *block, int pressure, const char *class_name)
2013 if (! status->stat_options)
2018 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
2019 be_block_entry_t *block_ent;
2020 reg_pressure_entry_t *rp_ent;
2022 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
2023 rp_ent = OALLOCZ(&status->be_data, reg_pressure_entry_t);
2025 rp_ent->class_name = class_name;
2026 rp_ent->pressure = pressure;
2028 pset_insert(block_ent->reg_pressure, rp_ent, HASH_PTR(class_name));
2031 } /* stat_be_block_regpressure */
2034 * Update the distribution of ready nodes of a block
2036 * @param irg the irg containing the block
2037 * @param block the block for which the reg pressure should be set
2038 * @param num_ready the number of ready nodes
2040 void stat_be_block_sched_ready(ir_graph *irg, ir_node *block, int num_ready)
2042 if (! status->stat_options)
2047 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
2048 be_block_entry_t *block_ent;
2050 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
2052 /* increase the counter of corresponding number of ready nodes */
2053 stat_inc_int_distrib_tbl(block_ent->sched_ready, num_ready);
2056 } /* stat_be_block_sched_ready */
2059 * Update the permutation statistic of a block.
2061 * @param class_name the name of the register class
2062 * @param n_regs number of registers in the register class
2063 * @param perm the perm node
2064 * @param block the block containing the perm
2065 * @param size the size of the perm
2066 * @param real_size number of pairs with different registers
2068 void stat_be_block_stat_perm(const char *class_name, int n_regs, ir_node *perm, ir_node *block,
2069 int size, int real_size)
2071 if (! status->stat_options)
2076 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
2077 be_block_entry_t *block_ent;
2078 perm_class_entry_t *pc_ent;
2079 perm_stat_entry_t *ps_ent;
2081 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
2082 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
2083 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
2085 pc_ent->n_regs = n_regs;
2087 /* update information */
2088 ps_ent->size = size;
2089 ps_ent->real_size = real_size;
2092 } /* stat_be_block_stat_perm */
2095 * Update the permutation statistic of a single perm.
2097 * @param class_name the name of the register class
2098 * @param perm the perm node
2099 * @param block the block containing the perm
2100 * @param is_chain 1 if chain, 0 if cycle
2101 * @param size length of the cycle/chain
2102 * @param n_ops the number of ops representing this cycle/chain after lowering
2104 void stat_be_block_stat_permcycle(const char *class_name, ir_node *perm, ir_node *block,
2105 int is_chain, int size, int n_ops)
2107 if (! status->stat_options)
2112 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
2113 be_block_entry_t *block_ent;
2114 perm_class_entry_t *pc_ent;
2115 perm_stat_entry_t *ps_ent;
2117 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
2118 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
2119 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
2122 ps_ent->n_copies += n_ops;
2123 stat_inc_int_distrib_tbl(ps_ent->chains, size);
2125 ps_ent->n_exchg += n_ops;
2126 stat_inc_int_distrib_tbl(ps_ent->cycles, size);
2130 } /* stat_be_block_stat_permcycle */
2132 /* Dumps a statistics snapshot. */
2133 void stat_dump_snapshot(const char *name, const char *phase)
2139 if (! status->stat_options)
2144 graph_entry_t *entry;
2145 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
2148 * The constant counter is only global, so we clear it here.
2149 * Note that it does NOT contain the constants in DELETED
2150 * graphs due to this.
2152 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2153 stat_const_clear(status);
2155 /* build the name */
2156 p = strrchr(name, '/');
2161 q = strrchr(name, '\\');
2163 /* NULL might be not the smallest pointer */
2164 if (q && (!p || q > p))
2172 if (l > (int) (sizeof(fname) - 1))
2173 l = sizeof(fname) - 1;
2175 memcpy(fname, name, l);
2181 strncat(fname, "firmstat-", sizeof(fname)-1);
2182 strncat(fname, phase, sizeof(fname)-1);
2183 strncat(fname, "-", sizeof(fname)-1);
2184 strncat(fname, p, sizeof(fname)-1);
2186 stat_dump_init(fname);
2188 /* calculate the graph statistics */
2189 for (entry = (graph_entry_t*)pset_first(status->irg_hash);
2190 entry != NULL; entry = (graph_entry_t*)pset_next(status->irg_hash)) {
2191 if (entry->irg == NULL) {
2192 /* special entry for the global count */
2195 if (! entry->is_deleted) {
2196 /* the graph is still alive, count the nodes on it */
2197 update_graph_stat(global, entry);
2201 /* some calculations are dependent, we pushed them on the wait_q */
2202 while (! pdeq_empty(status->wait_q)) {
2203 entry = (graph_entry_t*)pdeq_getr(status->wait_q);
2205 update_graph_stat_2(global, entry);
2208 /* dump per graph */
2209 for (entry = (graph_entry_t*)pset_first(status->irg_hash);
2210 entry != NULL; entry = (graph_entry_t*)pset_next(status->irg_hash)) {
2211 if (entry->irg == NULL) {
2212 /* special entry for the global count */
2216 if (! entry->is_deleted || status->stat_options & FIRMSTAT_COUNT_DELETED) {
2217 stat_dump_graph(entry);
2218 stat_dump_registered(entry);
2221 if (! entry->is_deleted) {
2222 /* clear the counter that are not accumulated */
2223 graph_clear_entry(entry, 0);
2228 stat_dump_graph(global);
2230 /* dump the const info */
2231 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2232 stat_dump_consts(&status->const_info);
2234 /* dump the parameter distribution */
2235 stat_dump_param_tbl(status->dist_param_cnt, global);
2237 /* dump the optimization counter and clear them */
2238 stat_dump_opt_cnt(status->num_opts, ARRAY_SIZE(status->num_opts));
2239 clear_optimization_counter();
2243 stat_finish_pattern_history(fname);
2245 /* clear the global counters here */
2247 node_entry_t *entry;
2249 for (entry = (node_entry_t*)pset_first(global->opcode_hash);
2250 entry != NULL; entry = (node_entry_t*)pset_next(global->opcode_hash)) {
2251 opcode_clear_entry(entry);
2253 /* clear all global counter */
2254 graph_clear_entry(global, /*all=*/1);
2258 } /* stat_dump_snapshot */
2260 typedef struct pass_t {
2261 ir_prog_pass_t pass;
2267 * Wrapper to run stat_dump_snapshot() as a ir_prog wrapper.
2269 static int stat_dump_snapshot_wrapper(ir_prog *irp, void *context)
2271 pass_t *pass = (pass_t*)context;
2274 stat_dump_snapshot(pass->fname, pass->phase);
2276 } /* stat_dump_snapshot_wrapper */
2279 * Ensure that no verifier is run from the wrapper.
2281 static int no_verify(ir_prog *prog, void *ctx)
2289 * Ensure that no dumper is run from the wrapper.
2291 static void no_dump(ir_prog *prog, void *ctx, unsigned idx)
2298 /* create an ir_pog pass */
2299 ir_prog_pass_t *stat_dump_snapshot_pass(
2300 const char *name, const char *fname, const char *phase)
2302 pass_t *pass = XMALLOCZ(pass_t);
2304 def_prog_pass_constructor(
2305 &pass->pass, name ? name : "stat_snapshot", stat_dump_snapshot_wrapper);
2306 pass->fname = fname;
2307 pass->phase = phase;
2309 /* no dump/verify */
2310 pass->pass.dump_irprog = no_dump;
2311 pass->pass.verify_irprog = no_verify;
2314 } /* stat_dump_snapshot_pass */
2316 /** the hook entries for the Firm statistics module */
2317 static hook_entry_t stat_hooks[hook_last];
2319 /* initialize the statistics module. */
2320 void firm_init_stat(unsigned enable_options)
2322 #define X(a) a, sizeof(a)-1
2323 #define HOOK(h, fkt) \
2324 stat_hooks[h].hook._##h = fkt; register_hook(h, &stat_hooks[h])
2327 if (! (enable_options & FIRMSTAT_ENABLED))
2330 status = XMALLOCZ(stat_info_t);
2332 /* enable statistics */
2333 status->stat_options = enable_options & FIRMSTAT_ENABLED ? enable_options : 0;
2335 /* register all hooks */
2336 HOOK(hook_new_ir_op, stat_new_ir_op);
2337 HOOK(hook_free_ir_op, stat_free_ir_op);
2338 HOOK(hook_new_node, stat_new_node);
2339 HOOK(hook_turn_into_id, stat_turn_into_id);
2340 HOOK(hook_normalize, stat_normalize);
2341 HOOK(hook_new_graph, stat_new_graph);
2342 HOOK(hook_free_graph, stat_free_graph);
2343 HOOK(hook_irg_walk, stat_irg_walk);
2344 HOOK(hook_irg_walk_blkwise, stat_irg_walk_blkwise);
2345 HOOK(hook_irg_block_walk, stat_irg_block_walk);
2346 HOOK(hook_merge_nodes, stat_merge_nodes);
2347 HOOK(hook_reassociate, stat_reassociate);
2348 HOOK(hook_lower, stat_lower);
2349 HOOK(hook_inline, stat_inline);
2350 HOOK(hook_tail_rec, stat_tail_rec);
2351 HOOK(hook_strength_red, stat_strength_red);
2352 HOOK(hook_dead_node_elim, stat_dead_node_elim);
2353 HOOK(hook_if_conversion, stat_if_conversion);
2354 HOOK(hook_func_call, stat_func_call);
2355 HOOK(hook_arch_dep_replace_mul_with_shifts, stat_arch_dep_replace_mul_with_shifts);
2356 HOOK(hook_arch_dep_replace_division_by_const, stat_arch_dep_replace_division_by_const);
2358 obstack_init(&status->cnts);
2359 obstack_init(&status->be_data);
2361 /* create the hash-tables */
2362 status->irg_hash = new_pset(graph_cmp, 8);
2363 status->ir_op_hash = new_pset(opcode_cmp_2, 1);
2365 /* create the wait queue */
2366 status->wait_q = new_pdeq();
2368 if (enable_options & FIRMSTAT_COUNT_STRONG_OP) {
2369 /* build the pseudo-ops */
2371 _op_Phi0.code = --num;
2372 _op_Phi0.name = new_id_from_chars(X("Phi0"));
2374 _op_PhiM.code = --num;
2375 _op_PhiM.name = new_id_from_chars(X("PhiM"));
2377 _op_ProjM.code = --num;
2378 _op_ProjM.name = new_id_from_chars(X("ProjM"));
2380 _op_MulC.code = --num;
2381 _op_MulC.name = new_id_from_chars(X("MulC"));
2383 _op_DivC.code = --num;
2384 _op_DivC.name = new_id_from_chars(X("DivC"));
2386 _op_ModC.code = --num;
2387 _op_ModC.name = new_id_from_chars(X("ModC"));
2389 _op_DivModC.code = --num;
2390 _op_DivModC.name = new_id_from_chars(X("DivModC"));
2392 _op_QuotC.code = --num;
2393 _op_QuotC.name = new_id_from_chars(X("QuotC"));
2395 status->op_Phi0 = &_op_Phi0;
2396 status->op_PhiM = &_op_PhiM;
2397 status->op_ProjM = &_op_ProjM;
2398 status->op_MulC = &_op_MulC;
2399 status->op_DivC = &_op_DivC;
2400 status->op_ModC = &_op_ModC;
2401 status->op_DivModC = &_op_DivModC;
2402 status->op_QuotC = &_op_QuotC;
2404 status->op_Phi0 = NULL;
2405 status->op_PhiM = NULL;
2406 status->op_ProjM = NULL;
2407 status->op_MulC = NULL;
2408 status->op_DivC = NULL;
2409 status->op_ModC = NULL;
2410 status->op_DivModC = NULL;
2411 status->op_QuotC = NULL;
2414 /* for Florian: count the Sel depth */
2415 if (enable_options & FIRMSTAT_COUNT_SELS) {
2416 _op_SelSel.code = --num;
2417 _op_SelSel.name = new_id_from_chars(X("Sel(Sel)"));
2419 _op_SelSelSel.code = --num;
2420 _op_SelSelSel.name = new_id_from_chars(X("Sel(Sel(Sel))"));
2422 status->op_SelSel = &_op_SelSel;
2423 status->op_SelSelSel = &_op_SelSelSel;
2425 status->op_SelSel = NULL;
2426 status->op_SelSelSel = NULL;
2429 /* register the dumper */
2430 stat_register_dumper(&simple_dumper);
2432 if (enable_options & FIRMSTAT_CSV_OUTPUT)
2433 stat_register_dumper(&csv_dumper);
2435 /* initialize the pattern hash */
2436 stat_init_pattern_history(enable_options & FIRMSTAT_PATTERN_ENABLED);
2438 /* initialize the Const options */
2439 if (enable_options & FIRMSTAT_COUNT_CONSTS)
2440 stat_init_const_cnt(status);
2442 /* distribution table for parameter counts */
2443 status->dist_param_cnt = stat_new_int_distrib_tbl();
2445 clear_optimization_counter();
2449 } /* firm_init_stat */
2452 * Frees all dumper structures.
2454 static void stat_term_dumper(void)
2456 dumper_t *dumper, *next_dumper;
2458 for (dumper = status->dumper; dumper; /* iteration done in loop body */ ) {
2459 if (dumper->func_map)
2460 del_pset(dumper->func_map);
2462 next_dumper = dumper->next;
2464 dumper = next_dumper;
2466 } /* stat_term_dumper */
2469 /* Terminates the statistics module, frees all memory. */
2470 void stat_term(void)
2472 if (status != (stat_info_t *)&status_disable) {
2473 obstack_free(&status->be_data, NULL);
2474 obstack_free(&status->cnts, NULL);
2479 status = (stat_info_t *)&status_disable;
2483 /* returns 1 if statistics were initialized, 0 otherwise */
2484 int stat_is_active(void)
2486 return status != (stat_info_t *)&status_disable;
2487 } /* stat_is_active */
2491 /* initialize the statistics module. */
2492 void firm_init_stat(unsigned enable_options) {}
2494 /* Dumps a statistics snapshot */
2495 void stat_dump_snapshot(const char *name, const char *phase) {}
2497 /* terminates the statistics module, frees all memory */
2498 void stat_term(void);
2500 #endif /* FIRM_STATISTICS */