2 * Copyright (C) 1995-2008 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
42 #include "firmstat_t.h"
50 * need this to be static:
51 * Special pseudo Opcodes that we need to count some interesting cases
55 * The Phi0, a node that is created during SSA construction
57 static ir_op _op_Phi0;
59 /** The PhiM, just to count memory Phi's. */
60 static ir_op _op_PhiM;
62 /** The Mul by Const node. */
63 static ir_op _op_MulC;
65 /** The Div by Const node. */
66 static ir_op _op_DivC;
68 /** The Div by Const node. */
69 static ir_op _op_ModC;
71 /** The Div by Const node. */
72 static ir_op _op_DivModC;
74 /** The Quot by Const node. */
75 static ir_op _op_QuotC;
77 /** The memory Proj node. */
78 static ir_op _op_ProjM;
81 static ir_op _op_SelSel;
83 /** A Sel of a Sel of a Sel */
84 static ir_op _op_SelSelSel;
86 /* ---------------------------------------------------------------------------------- */
88 /** Marks the begin of a statistic (hook) function. */
89 #define STAT_ENTER ++status->recursive
91 /** Marks the end of a statistic (hook) functions. */
92 #define STAT_LEAVE --status->recursive
94 /** Allows to enter a statistic function only when we are not already in a hook. */
95 #define STAT_ENTER_SINGLE do { if (status->recursive > 0) return; ++status->recursive; } while (0)
100 static const unsigned status_disable = 0;
101 static stat_info_t *status = (stat_info_t *)&status_disable;
104 * Compare two elements of the opcode hash.
106 static int opcode_cmp(const void *elt, const void *key) {
107 const node_entry_t *e1 = elt;
108 const node_entry_t *e2 = key;
110 return e1->op->code - e2->op->code;
114 * Compare two elements of the graph hash.
116 static int graph_cmp(const void *elt, const void *key) {
117 const graph_entry_t *e1 = elt;
118 const graph_entry_t *e2 = key;
120 return e1->irg != e2->irg;
124 * Compare two elements of the optimization hash.
126 static int opt_cmp(const void *elt, const void *key) {
127 const opt_entry_t *e1 = elt;
128 const opt_entry_t *e2 = 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) {
137 const block_entry_t *e1 = elt;
138 const block_entry_t *e2 = key;
140 /* it's enough to compare the block number */
141 return e1->block_nr != e2->block_nr;
145 * Compare two elements of the be_block hash.
147 static int be_block_cmp(const void *elt, const void *key) {
148 const be_block_entry_t *e1 = elt;
149 const be_block_entry_t *e2 = key;
151 return e1->block_nr != e2->block_nr;
155 * Compare two elements of reg pressure hash.
157 static int reg_pressure_cmp(const void *elt, const void *key) {
158 const reg_pressure_entry_t *e1 = elt;
159 const reg_pressure_entry_t *e2 = key;
161 return e1->class_name != e2->class_name;
162 } /* reg_pressure_cmp */
165 * Compare two elements of the perm_stat hash.
167 static int perm_stat_cmp(const void *elt, const void *key) {
168 const perm_stat_entry_t *e1 = elt;
169 const perm_stat_entry_t *e2 = key;
171 return e1->perm != e2->perm;
172 } /* perm_stat_cmp */
175 * Compare two elements of the perm_class hash.
177 static int perm_class_cmp(const void *elt, const void *key) {
178 const perm_class_entry_t *e1 = elt;
179 const perm_class_entry_t *e2 = key;
181 return e1->class_name != e2->class_name;
182 } /* perm_class_cmp */
185 * Compare two elements of the ir_op hash.
187 static int opcode_cmp_2(const void *elt, const void *key) {
188 const ir_op *e1 = elt;
189 const ir_op *e2 = key;
191 return e1->code != e2->code;
195 * Compare two elements of the address_mark set.
197 static int address_mark_cmp(const void *elt, const void *key, size_t size) {
198 const address_mark_entry_t *e1 = elt;
199 const address_mark_entry_t *e2 = key;
202 /* compare only the nodes, the rest is used as data container */
203 return e1->node != e2->node;
204 } /* address_mark_cmp */
207 * Clear all counter in a node_entry_t.
209 static void opcode_clear_entry(node_entry_t *elem) {
210 cnt_clr(&elem->cnt_alive);
211 cnt_clr(&elem->new_node);
212 cnt_clr(&elem->into_Id);
213 } /* opcode_clear_entry */
216 * Returns the associates node_entry_t for an ir_op
218 * @param op the IR operation
219 * @param hmap a hash map containing ir_op* -> node_entry_t*
221 static node_entry_t *opcode_get_entry(const ir_op *op, hmap_node_entry_t *hmap) {
227 elem = pset_find(hmap, &key, op->code);
231 elem = obstack_alloc(&status->cnts, sizeof(*elem));
232 memset(elem, 0, sizeof(*elem));
235 opcode_clear_entry(elem);
239 return pset_insert(hmap, elem, op->code);
240 } /* opcode_get_entry */
243 * Returns the associates ir_op for an opcode
245 * @param code the IR opcode
246 * @param hmap the hash map containing opcode -> ir_op*
248 static ir_op *opcode_find_entry(ir_opcode code, hmap_ir_op *hmap) {
252 return pset_find(hmap, &key, code);
253 } /* opcode_find_entry */
256 * Clears all counter in a graph_entry_t.
258 * @param elem the graph entry
259 * @param all if non-zero, clears all counters, else leave accumulated ones
261 static void graph_clear_entry(graph_entry_t *elem, int all) {
264 /* clear accumulated / non-accumulated counter */
265 for (i = all ? 0 : _gcnt_non_acc; i < _gcnt_last; ++i) {
266 cnt_clr(&elem->cnt[i]);
269 if (elem->block_hash) {
270 del_pset(elem->block_hash);
271 elem->block_hash = NULL;
274 if (elem->extbb_hash) {
275 del_pset(elem->extbb_hash);
276 elem->extbb_hash = NULL;
279 obstack_free(&elem->recalc_cnts, NULL);
280 obstack_init(&elem->recalc_cnts);
281 } /* graph_clear_entry */
284 * Returns the associated graph_entry_t for an IR graph.
286 * @param irg the IR graph
287 * @param hmap the hash map containing ir_graph* -> graph_entry_t*
289 static graph_entry_t *graph_get_entry(ir_graph *irg, hmap_graph_entry_t *hmap)
297 elem = pset_find(hmap, &key, HASH_PTR(irg));
300 /* create hash map backend block information */
301 if (! elem->be_block_hash)
302 elem->be_block_hash = new_pset(be_block_cmp, 5);
307 /* allocate a new one */
308 elem = obstack_alloc(&status->cnts, sizeof(*elem));
309 memset(elem, 0, sizeof(*elem));
310 obstack_init(&elem->recalc_cnts);
313 graph_clear_entry(elem, 1);
315 /* new hash table for opcodes here */
316 elem->opcode_hash = new_pset(opcode_cmp, 5);
317 elem->address_mark = new_set(address_mark_cmp, 5);
320 /* these hash tables are created on demand */
321 elem->block_hash = NULL;
322 elem->extbb_hash = NULL;
324 for (i = 0; i < sizeof(elem->opt_hash)/sizeof(elem->opt_hash[0]); ++i)
325 elem->opt_hash[i] = new_pset(opt_cmp, 4);
327 return pset_insert(hmap, elem, HASH_PTR(irg));
328 } /* graph_get_entry */
331 * Clear all counter in an opt_entry_t.
333 static void opt_clear_entry(opt_entry_t *elem) {
334 cnt_clr(&elem->count);
335 } /* opt_clear_entry */
338 * Returns the associated opt_entry_t for an IR operation.
340 * @param op the IR operation
341 * @param hmap the hash map containing ir_op* -> opt_entry_t*
343 static opt_entry_t *opt_get_entry(const ir_op *op, hmap_opt_entry_t *hmap)
350 elem = pset_find(hmap, &key, op->code);
354 elem = obstack_alloc(&status->cnts, sizeof(*elem));
355 memset(elem, 0, sizeof(*elem));
357 /* clear new counter */
358 opt_clear_entry(elem);
362 return pset_insert(hmap, elem, op->code);
363 } /* opt_get_entry */
366 * clears all counter in a block_entry_t
368 static void block_clear_entry(block_entry_t *elem) {
371 for (i = 0; i < _bcnt_last; ++i)
372 cnt_clr(&elem->cnt[i]);
373 } /* block_clear_entry */
376 * Returns the associated block_entry_t for an block.
378 * @param block_nr an IR block number
379 * @param hmap a hash map containing long -> block_entry_t
381 static block_entry_t *block_get_entry(struct obstack *obst, long block_nr, hmap_block_entry_t *hmap)
386 key.block_nr = block_nr;
388 elem = pset_find(hmap, &key, block_nr);
392 elem = obstack_alloc(obst, sizeof(*elem));
393 memset(elem, 0, sizeof(*elem));
395 /* clear new counter */
396 block_clear_entry(elem);
398 elem->block_nr = block_nr;
400 return pset_insert(hmap, elem, block_nr);
401 } /* block_get_entry */
404 * Clear all sets in be_block_entry_t.
406 static void be_block_clear_entry(be_block_entry_t *elem)
408 if (elem->reg_pressure)
409 del_pset(elem->reg_pressure);
411 if (elem->sched_ready)
412 stat_delete_distrib_tbl(elem->sched_ready);
414 if (elem->perm_class_stat)
415 del_pset(elem->perm_class_stat);
417 elem->reg_pressure = new_pset(reg_pressure_cmp, 5);
418 elem->sched_ready = stat_new_int_distrib_tbl();
419 elem->perm_class_stat = new_pset(perm_class_cmp, 5);
420 } /* be_block_clear_entry */
423 * Returns the associated be_block_entry_t for an block.
425 * @param block_nr an IR block number
426 * @param hmap a hash map containing long -> be_block_entry_t
428 static be_block_entry_t *be_block_get_entry(struct obstack *obst, long block_nr, hmap_be_block_entry_t *hmap)
430 be_block_entry_t key;
431 be_block_entry_t *elem;
433 key.block_nr = block_nr;
435 elem = pset_find(hmap, &key, block_nr);
439 elem = obstack_alloc(obst, sizeof(*elem));
440 memset(elem, 0, sizeof(*elem));
442 /* clear new counter */
443 be_block_clear_entry(elem);
445 elem->block_nr = block_nr;
447 return pset_insert(hmap, elem, block_nr);
448 } /* be_block_get_entry */
451 * clears all sets in perm_class_entry_t
453 static void perm_class_clear_entry(perm_class_entry_t *elem) {
455 del_pset(elem->perm_stat);
457 elem->perm_stat = new_pset(perm_stat_cmp, 5);
458 } /* perm_class_clear_entry */
461 * Returns the associated perm_class entry for a register class.
463 * @param class_name the register class name
464 * @param hmap a hash map containing class_name -> perm_class_entry_t
466 static perm_class_entry_t *perm_class_get_entry(struct obstack *obst, const char *class_name,
467 hmap_perm_class_entry_t *hmap)
469 perm_class_entry_t key;
470 perm_class_entry_t *elem;
472 key.class_name = class_name;
474 elem = pset_find(hmap, &key, HASH_PTR(class_name));
478 elem = obstack_alloc(obst, sizeof(*elem));
479 memset(elem, 0, sizeof(*elem));
481 /* clear new counter */
482 perm_class_clear_entry(elem);
484 elem->class_name = class_name;
486 return pset_insert(hmap, elem, HASH_PTR(class_name));
487 } /* perm_class_get_entry */
490 * clears all sets in perm_stat_entry_t
492 static void perm_stat_clear_entry(perm_stat_entry_t *elem) {
494 stat_delete_distrib_tbl(elem->chains);
497 stat_delete_distrib_tbl(elem->cycles);
499 elem->chains = stat_new_int_distrib_tbl();
500 elem->cycles = stat_new_int_distrib_tbl();
501 } /* perm_stat_clear_entry */
504 * Returns the associated perm_stat entry for a perm.
506 * @param perm the perm node
507 * @param hmap a hash map containing perm -> perm_stat_entry_t
509 static perm_stat_entry_t *perm_stat_get_entry(struct obstack *obst, ir_node *perm, hmap_perm_stat_entry_t *hmap)
511 perm_stat_entry_t key;
512 perm_stat_entry_t *elem;
516 elem = pset_find(hmap, &key, HASH_PTR(perm));
520 elem = obstack_alloc(obst, sizeof(*elem));
521 memset(elem, 0, sizeof(*elem));
523 /* clear new counter */
524 perm_stat_clear_entry(elem);
528 return pset_insert(hmap, elem, HASH_PTR(perm));
529 } /* perm_stat_get_entry */
532 * Clear optimizations counter,
534 static void clear_optimization_counter(void) {
536 for (i = 0; i < FS_OPT_MAX; ++i)
537 cnt_clr(&status->num_opts[i]);
541 * Returns the ir_op for an IR-node,
542 * handles special cases and return pseudo op codes.
544 * @param none an IR node
546 static ir_op *stat_get_irn_op(ir_node *node)
548 ir_op *op = get_irn_op(node);
549 ir_opcode opc = op->code;
553 if (get_irn_arity(node) == 0) {
554 /* special case, a Phi0 node, count on extra counter */
555 op = status->op_Phi0 ? status->op_Phi0 : op;
556 } else if (get_irn_mode(node) == mode_M) {
557 /* special case, a Memory Phi node, count on extra counter */
558 op = status->op_PhiM ? status->op_PhiM : op;
562 if (get_irn_mode(node) == mode_M) {
563 /* special case, a Memory Proj node, count on extra counter */
564 op = status->op_ProjM ? status->op_ProjM : op;
568 if (is_Const(get_Mul_left(node)) || is_Const(get_Mul_right(node))) {
569 /* special case, a Multiply by a const, count on extra counter */
570 op = status->op_MulC ? status->op_MulC : op;
574 if (is_Const(get_Div_right(node))) {
575 /* special case, a division by a const, count on extra counter */
576 op = status->op_DivC ? status->op_DivC : op;
580 if (is_Const(get_Mod_right(node))) {
581 /* special case, a module by a const, count on extra counter */
582 op = status->op_ModC ? status->op_ModC : op;
586 if (is_Const(get_DivMod_right(node))) {
587 /* special case, a division/modulo by a const, count on extra counter */
588 op = status->op_DivModC ? status->op_DivModC : op;
592 if (is_Const(get_Quot_right(node))) {
593 /* special case, a floating point division by a const, count on extra counter */
594 op = status->op_QuotC ? status->op_QuotC : op;
598 if (is_Sel(get_Sel_ptr(node))) {
599 /* special case, a Sel of a Sel, count on extra counter */
600 op = status->op_SelSel ? status->op_SelSel : op;
601 if (is_Sel(get_Sel_ptr(get_Sel_ptr(node)))) {
602 /* special case, a Sel of a Sel of a Sel, count on extra counter */
603 op = status->op_SelSelSel ? status->op_SelSelSel : op;
612 } /* stat_get_irn_op */
615 * update the block counter
617 static void undate_block_info(ir_node *node, graph_entry_t *graph)
619 ir_op *op = get_irn_op(node);
621 block_entry_t *b_entry;
624 /* check for block */
625 if (op == op_Block) {
626 arity = get_irn_arity(node);
627 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(node), graph->block_hash);
628 /* mark start end block to allow to filter them out */
629 if (node == get_irg_start_block(graph->irg))
630 b_entry->is_start = 1;
631 else if (node == get_irg_end_block(graph->irg))
634 /* count all incoming edges */
635 for (i = 0; i < arity; ++i) {
636 ir_node *pred = get_irn_n(node, i);
637 ir_node *other_block = get_nodes_block(pred);
638 block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
640 cnt_inc(&b_entry->cnt[bcnt_in_edges]); /* an edge coming from another block */
641 cnt_inc(&b_entry_other->cnt[bcnt_out_edges]);
646 block = get_nodes_block(node);
647 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(block), graph->block_hash);
649 if (op == op_Phi && mode_is_datab(get_irn_mode(node))) {
650 /* count data Phi per block */
651 cnt_inc(&b_entry->cnt[bcnt_phi_data]);
654 /* we have a new node in our block */
655 cnt_inc(&b_entry->cnt[bcnt_nodes]);
657 /* don't count keep-alive edges */
661 arity = get_irn_arity(node);
663 for (i = 0; i < arity; ++i) {
664 ir_node *pred = get_irn_n(node, i);
665 ir_node *other_block;
667 other_block = get_nodes_block(pred);
669 if (other_block == block)
670 cnt_inc(&b_entry->cnt[bcnt_edges]); /* a in block edge */
672 block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
674 cnt_inc(&b_entry->cnt[bcnt_in_edges]); /* an edge coming from another block */
675 cnt_inc(&b_entry_other->cnt[bcnt_out_edges]);
678 } /* undate_block_info */
681 * Update the extended block counter.
683 static void update_extbb_info(ir_node *node, graph_entry_t *graph)
685 ir_op *op = get_irn_op(node);
687 extbb_entry_t *eb_entry;
690 /* check for block */
691 if (op == op_Block) {
692 extbb = get_nodes_extbb(node);
693 arity = get_irn_arity(node);
694 eb_entry = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(extbb), graph->extbb_hash);
696 /* count all incoming edges */
697 for (i = 0; i < arity; ++i) {
698 ir_node *pred = get_irn_n(node, i);
699 ir_extblk *other_extbb = get_nodes_extbb(pred);
701 if (extbb != other_extbb) {
702 extbb_entry_t *eb_entry_other = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(other_extbb), graph->extbb_hash);
704 cnt_inc(&eb_entry->cnt[bcnt_in_edges]); /* an edge coming from another extbb */
705 cnt_inc(&eb_entry_other->cnt[bcnt_out_edges]);
711 extbb = get_nodes_extbb(node);
712 eb_entry = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(extbb), graph->extbb_hash);
714 if (op == op_Phi && mode_is_datab(get_irn_mode(node))) {
715 /* count data Phi per extbb */
716 cnt_inc(&eb_entry->cnt[bcnt_phi_data]);
719 /* we have a new node in our block */
720 cnt_inc(&eb_entry->cnt[bcnt_nodes]);
722 /* don't count keep-alive edges */
726 arity = get_irn_arity(node);
728 for (i = 0; i < arity; ++i) {
729 ir_node *pred = get_irn_n(node, i);
730 ir_extblk *other_extbb = get_nodes_extbb(pred);
732 if (other_extbb == extbb)
733 cnt_inc(&eb_entry->cnt[bcnt_edges]); /* a in extbb edge */
735 extbb_entry_t *eb_entry_other = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(other_extbb), graph->extbb_hash);
737 cnt_inc(&eb_entry->cnt[bcnt_in_edges]); /* an edge coming from another extbb */
738 cnt_inc(&eb_entry_other->cnt[bcnt_out_edges]);
741 } /* update_extbb_info */
744 * Calculates how many arguments of the call are const, updates
745 * param distribution.
747 static void analyse_params_of_Call(graph_entry_t *graph, ir_node *call) {
748 int i, num_const_args = 0, num_local_adr = 0;
749 int n = get_Call_n_params(call);
751 for (i = 0; i < n; ++i) {
752 ir_node *param = get_Call_param(call, i);
754 if (is_irn_constlike(param))
756 else if (is_Sel(param)) {
757 ir_node *base = param;
760 base = get_Sel_ptr(base);
761 } while (is_Sel(base));
763 if (base == get_irg_frame(current_ir_graph))
769 if (num_const_args > 0)
770 cnt_inc(&graph->cnt[gcnt_call_with_cnst_arg]);
771 if (num_const_args == n)
772 cnt_inc(&graph->cnt[gcnt_call_with_all_cnst_arg]);
773 if (num_local_adr > 0)
774 cnt_inc(&graph->cnt[gcnt_call_with_local_adr]);
776 stat_inc_int_distrib_tbl(status->dist_param_cnt, n);
777 } /* analyse_params_of_Call */
780 * Update info on calls.
782 * @param call The call
783 * @param graph The graph entry containing the call
785 static void stat_update_call(ir_node *call, graph_entry_t *graph)
787 ir_node *block = get_nodes_block(call);
788 ir_node *ptr = get_Call_ptr(call);
789 ir_entity *ent = NULL;
790 ir_graph *callee = NULL;
793 * If the block is bad, the whole subgraph will collapse later
794 * so do not count this call.
795 * This happens in dead code.
800 cnt_inc(&graph->cnt[gcnt_all_calls]);
802 /* found a call, this function is not a leaf */
805 if (is_SymConst(ptr)) {
806 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
807 /* ok, we seems to know the entity */
808 ent = get_SymConst_entity(ptr);
809 callee = get_entity_irg(ent);
811 /* it is recursive, if it calls at least once */
812 if (callee == graph->irg)
813 graph->is_recursive = 1;
815 cnt_inc(&graph->cnt[gcnt_external_calls]);
818 /* indirect call, be could not predict */
819 cnt_inc(&graph->cnt[gcnt_indirect_calls]);
821 /* NOT a leaf call */
822 graph->is_leaf_call = LCS_NON_LEAF_CALL;
825 /* check, if it's a chain-call: Then, the call-block
826 * must dominate the end block. */
828 ir_node *curr = get_irg_end_block(graph->irg);
829 int depth = get_Block_dom_depth(block);
831 for (; curr != block && get_Block_dom_depth(curr) > depth;) {
832 curr = get_Block_idom(curr);
834 if (! curr || is_no_Block(curr))
839 graph->is_chain_call = 0;
842 /* check, if the callee is a leaf */
844 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
846 if (called->is_analyzed) {
847 if (! called->is_leaf)
848 graph->is_leaf_call = LCS_NON_LEAF_CALL;
852 analyse_params_of_Call(graph, call);
853 } /* stat_update_call */
856 * Update info on calls for graphs on the wait queue.
858 static void stat_update_call_2(ir_node *call, graph_entry_t *graph)
860 ir_node *block = get_nodes_block(call);
861 ir_node *ptr = get_Call_ptr(call);
862 ir_entity *ent = NULL;
863 ir_graph *callee = NULL;
866 * If the block is bad, the whole subgraph will collapse later
867 * so do not count this call.
868 * This happens in dead code.
873 if (is_SymConst(ptr)) {
874 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
875 /* ok, we seems to know the entity */
876 ent = get_SymConst_entity(ptr);
877 callee = get_entity_irg(ent);
881 /* check, if the callee is a leaf */
883 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
885 assert(called->is_analyzed);
887 if (! called->is_leaf)
888 graph->is_leaf_call = LCS_NON_LEAF_CALL;
890 graph->is_leaf_call = LCS_NON_LEAF_CALL;
891 } /* stat_update_call_2 */
894 * Find the base address and entity of an Sel node.
896 * @param sel the node
898 * @return the base address.
900 static ir_node *find_base_adr(ir_node *sel) {
901 ir_node *ptr = get_Sel_ptr(sel);
903 while (is_Sel(ptr)) {
905 ptr = get_Sel_ptr(sel);
908 } /* find_base_adr */
911 * Update info on Load/Store address statistics.
913 static void stat_update_address(ir_node *node, graph_entry_t *graph) {
914 ir_opcode opc = get_irn_opcode(node);
920 /* a global address */
921 cnt_inc(&graph->cnt[gcnt_global_adr]);
924 base = find_base_adr(node);
925 irg = current_ir_graph;
926 if (base == get_irg_tls(irg)) {
927 /* a TLS variable, like a global. */
928 cnt_inc(&graph->cnt[gcnt_global_adr]);
929 } else if (base == get_irg_frame(irg)) {
930 /* a local Variable. */
931 cnt_inc(&graph->cnt[gcnt_local_adr]);
934 if (is_Proj(base) && skip_Proj(get_Proj_pred(base)) == get_irg_start(irg)) {
935 /* pointer access through parameter, check for THIS */
936 ir_entity *ent = get_irg_entity(irg);
939 ir_type *ent_tp = get_entity_type(ent);
941 if (get_method_calling_convention(ent_tp) & cc_this_call) {
942 if (get_Proj_proj(base) == 0) {
944 cnt_inc(&graph->cnt[gcnt_this_adr]);
949 /* other parameter */
950 cnt_inc(&graph->cnt[gcnt_param_adr]);
953 /* unknown Pointer access */
954 cnt_inc(&graph->cnt[gcnt_other_adr]);
960 } /* stat_update_address */
963 * Walker for reachable nodes count.
965 static void update_node_stat(ir_node *node, void *env)
967 graph_entry_t *graph = env;
970 ir_op *op = stat_get_irn_op(node);
971 int i, arity = get_irn_arity(node);
973 entry = opcode_get_entry(op, graph->opcode_hash);
975 cnt_inc(&entry->cnt_alive);
976 cnt_add_i(&graph->cnt[gcnt_edges], arity);
978 /* count block edges */
979 undate_block_info(node, graph);
981 /* count extended block edges */
982 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
983 if (graph->irg != get_const_code_irg())
984 update_extbb_info(node, graph);
987 /* handle statistics for special node types */
991 /* check for properties that depends on calls like recursion/leaf/indirect call */
992 stat_update_call(node, graph);
995 /* check address properties */
996 stat_update_address(get_Load_ptr(node), graph);
999 /* check address properties */
1000 stat_update_address(get_Store_ptr(node), graph);
1003 /* check for non-strict Phi nodes */
1004 for (i = arity - 1; i >= 0; --i) {
1005 ir_node *pred = get_Phi_pred(node, i);
1006 if (is_Unknown(pred)) {
1007 /* found an Unknown predecessor, graph is not strict */
1008 graph->is_strict = 0;
1016 /* we want to count the constant IN nodes, not the CSE'ed constant's itself */
1017 if (status->stat_options & FIRMSTAT_COUNT_CONSTS) {
1020 for (i = get_irn_arity(node) - 1; i >= 0; --i) {
1021 ir_node *pred = get_irn_n(node, i);
1023 if (is_Const(pred)) {
1024 /* check properties of constants */
1025 stat_update_const(status, pred, graph);
1029 } /* update_node_stat */
1032 * Walker for reachable nodes count for graphs on the wait_q.
1034 static void update_node_stat_2(ir_node *node, void *env) {
1035 graph_entry_t *graph = env;
1037 /* check for properties that depends on calls like recursion/leaf/indirect call */
1039 stat_update_call_2(node, graph);
1040 } /* update_node_stat_2 */
1043 * Get the current address mark.
1045 static unsigned get_adr_mark(graph_entry_t *graph, ir_node *node) {
1046 address_mark_entry_t *value = set_find(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
1048 return value ? value->mark : 0;
1049 } /* get_adr_mark */
1052 * Set the current address mark.
1054 static void set_adr_mark(graph_entry_t *graph, ir_node *node, unsigned val) {
1055 address_mark_entry_t *value = set_insert(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
1058 } /* set_adr_mark */
1060 #undef DUMP_ADR_MODE
1062 #ifdef DUMP_ADR_MODE
1064 * a vcg attribute hook: Color a node with a different color if
1065 * it's identified as a part of an address expression or at least referenced
1066 * by an address expression.
1068 static int stat_adr_mark_hook(FILE *F, ir_node *node, ir_node *local)
1070 ir_node *n = local ? local : node;
1071 ir_graph *irg = get_irn_irg(n);
1072 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1073 unsigned mark = get_adr_mark(graph, n);
1075 if (mark & MARK_ADDRESS_CALC)
1076 fprintf(F, "color: purple");
1077 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1078 fprintf(F, "color: pink");
1079 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1080 fprintf(F, "color: lightblue");
1084 /* I know the color! */
1086 } /* stat_adr_mark_hook */
1087 #endif /* DUMP_ADR_MODE */
1090 * Return the "operational" mode of a Firm node.
1092 static ir_mode *get_irn_op_mode(ir_node *node) {
1093 switch (get_irn_opcode(node)) {
1095 return get_Load_mode(node);
1097 return get_irn_mode(get_Store_value(node));
1099 return get_irn_mode(get_DivMod_left(node));
1101 return get_irn_mode(get_Div_left(node));
1103 return get_irn_mode(get_Mod_left(node));
1105 /* Cmp is no address calculation, or is it? */
1107 return get_irn_mode(node);
1109 } /* get_irn_op_mode */
1112 * Post-walker that marks every node that is an address calculation.
1114 * Users of a node must be visited first. We ensure this by
1115 * calling it in the post of an outs walk. This should work even in cycles,
1116 * while the normal pre-walk will not.
1118 static void mark_address_calc(ir_node *node, void *env) {
1119 graph_entry_t *graph = env;
1120 ir_mode *mode = get_irn_op_mode(node);
1122 unsigned mark_preds = MARK_REF_NON_ADR;
1124 if (! mode_is_data(mode))
1127 if (mode_is_reference(mode)) {
1128 /* a reference is calculated here, we are sure */
1129 set_adr_mark(graph, node, MARK_ADDRESS_CALC);
1131 mark_preds = MARK_REF_ADR;
1133 unsigned mark = get_adr_mark(graph, node);
1135 if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR) {
1137 * this node has no reference mode, but is only
1138 * referenced by address calculations
1140 mark_preds = MARK_REF_ADR;
1144 /* mark all predecessors */
1145 for (i = 0, n = get_irn_arity(node); i < n; ++i) {
1146 ir_node *pred = get_irn_n(node, i);
1148 mode = get_irn_op_mode(pred);
1149 if (! mode_is_data(mode))
1152 set_adr_mark(graph, pred, get_adr_mark(graph, pred) | mark_preds);
1154 } /* mark_address_calc */
1157 * Post-walker that marks every node that is an address calculation.
1159 * Users of a node must be visited first. We ensure this by
1160 * calling it in the post of an outs walk. This should work even in cycles,
1161 * while the normal pre-walk will not.
1163 static void count_adr_ops(ir_node *node, void *env) {
1164 graph_entry_t *graph = env;
1165 unsigned mark = get_adr_mark(graph, node);
1167 if (mark & MARK_ADDRESS_CALC)
1168 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1169 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1170 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1171 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1172 cnt_inc(&graph->cnt[gcnt_all_adr_ops]);
1173 } /* count_adr_ops */
1176 * Called for every graph when the graph is either deleted or stat_dump_snapshot()
1177 * is called, must recalculate all statistic info.
1179 * @param global The global entry
1180 * @param graph The current entry
1182 static void update_graph_stat(graph_entry_t *global, graph_entry_t *graph)
1184 node_entry_t *entry;
1187 /* clear first the alive counter in the graph */
1188 foreach_pset(graph->opcode_hash, entry) {
1189 cnt_clr(&entry->cnt_alive);
1190 } /* foreach_pset */
1192 /* set pessimistic values */
1194 graph->is_leaf_call = LCS_UNKNOWN;
1195 graph->is_recursive = 0;
1196 graph->is_chain_call = 1;
1197 graph->is_strict = 1;
1199 /* create new block counter */
1200 graph->block_hash = new_pset(block_cmp, 5);
1202 /* we need dominator info */
1203 if (graph->irg != get_const_code_irg()) {
1204 assure_doms(graph->irg);
1206 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
1207 /* we need extended basic blocks */
1208 compute_extbb(graph->irg);
1210 /* create new extbb counter */
1211 graph->extbb_hash = new_pset(block_cmp, 5);
1215 /* count the nodes in the graph */
1216 irg_walk_graph(graph->irg, update_node_stat, NULL, graph);
1219 /* Uncomment this code if chain-call means call exact one. */
1220 entry = opcode_get_entry(op_Call, graph->opcode_hash);
1222 /* check if we have more than 1 call */
1223 if (cnt_gt(entry->cnt_alive, 1))
1224 graph->is_chain_call = 0;
1227 /* recursive functions are never chain calls, leafs don't have calls */
1228 if (graph->is_recursive || graph->is_leaf)
1229 graph->is_chain_call = 0;
1231 /* assume we walk every graph only ONCE, we could sum here the global count */
1232 foreach_pset(graph->opcode_hash, entry) {
1233 node_entry_t *g_entry = opcode_get_entry(entry->op, global->opcode_hash);
1235 /* update the node counter */
1236 cnt_add(&g_entry->cnt_alive, &entry->cnt_alive);
1237 } /* foreach_pset */
1239 /* count the number of address calculation */
1240 if (graph->irg != get_const_code_irg()) {
1241 ir_graph *rem = current_ir_graph;
1243 assure_irg_outs(graph->irg);
1245 /* Must be done an the outs graph */
1246 current_ir_graph = graph->irg;
1247 irg_out_walk(get_irg_start(graph->irg), NULL, mark_address_calc, graph);
1248 current_ir_graph = rem;
1250 #ifdef DUMP_ADR_MODE
1251 /* register the vcg hook and dump the graph for test */
1252 set_dump_node_vcgattr_hook(stat_adr_mark_hook);
1253 dump_ir_block_graph(graph->irg, "-adr");
1254 set_dump_node_vcgattr_hook(NULL);
1255 #endif /* DUMP_ADR_MODE */
1257 irg_walk_graph(graph->irg, NULL, count_adr_ops, graph);
1260 /* count the DAG's */
1261 if (status->stat_options & FIRMSTAT_COUNT_DAG)
1262 count_dags_in_graph(global, graph);
1264 /* calculate the patterns of this graph */
1265 stat_calc_pattern_history(graph->irg);
1267 /* leaf function did not call others */
1269 graph->is_leaf_call = LCS_NON_LEAF_CALL;
1270 else if (graph->is_leaf_call == LCS_UNKNOWN) {
1271 /* we still don't know if this graph calls leaf-functions, so enqueue */
1272 pdeq_putl(status->wait_q, graph);
1275 /* we have analyzed this graph */
1276 graph->is_analyzed = 1;
1278 /* accumulate all counter's */
1279 for (i = 0; i < _gcnt_last; ++i)
1280 cnt_add(&global->cnt[i], &graph->cnt[i]);
1281 } /* update_graph_stat */
1284 * Called for every graph that was on the wait_q in stat_dump_snapshot()
1285 * must finish all statistic info calculations.
1287 * @param global The global entry
1288 * @param graph The current entry
1290 static void update_graph_stat_2(graph_entry_t *global, graph_entry_t *graph)
1293 if (graph->is_deleted) {
1294 /* deleted, ignore */
1299 /* count the nodes in the graph */
1300 irg_walk_graph(graph->irg, update_node_stat_2, NULL, graph);
1302 if (graph->is_leaf_call == LCS_UNKNOWN)
1303 graph->is_leaf_call = LCS_LEAF_CALL;
1305 } /* update_graph_stat_2 */
1308 * Register a dumper.
1310 static void stat_register_dumper(const dumper_t *dumper) {
1311 dumper_t *p = XMALLOC(dumper_t);
1313 memcpy(p, dumper, sizeof(*p));
1315 p->next = status->dumper;
1319 /* FIXME: memory leak */
1320 } /* stat_register_dumper */
1323 * Dumps the statistics of an IR graph.
1325 static void stat_dump_graph(graph_entry_t *entry) {
1328 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1329 if (dumper->dump_graph)
1330 dumper->dump_graph(dumper, entry);
1332 } /* stat_dump_graph */
1335 * Calls all registered dumper functions.
1337 static void stat_dump_registered(graph_entry_t *entry) {
1340 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1341 if (dumper->func_map) {
1342 dump_graph_FUNC func;
1344 foreach_pset(dumper->func_map, func)
1345 func(dumper, entry);
1348 } /* stat_dump_registered */
1351 * Dumps a constant table.
1353 static void stat_dump_consts(const constant_info_t *tbl) {
1356 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1357 if (dumper->dump_const_tbl)
1358 dumper->dump_const_tbl(dumper, tbl);
1360 } /* stat_dump_consts */
1363 * Dumps the parameter distribution
1365 static void stat_dump_param_tbl(const distrib_tbl_t *tbl, graph_entry_t *global) {
1368 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1369 if (dumper->dump_param_tbl)
1370 dumper->dump_param_tbl(dumper, tbl, global);
1372 } /* stat_dump_param_tbl */
1375 * Dumps the optimization counter
1377 static void stat_dump_opt_cnt(const counter_t *tbl, unsigned len) {
1380 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1381 if (dumper->dump_opt_cnt)
1382 dumper->dump_opt_cnt(dumper, tbl, len);
1384 } /* stat_dump_opt_cnt */
1387 * Initialize the dumper.
1389 static void stat_dump_init(const char *name) {
1392 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1394 dumper->init(dumper, name);
1396 } /* stat_dump_init */
1399 * Finish the dumper.
1401 static void stat_dump_finish(void) {
1404 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1406 dumper->finish(dumper);
1408 } /* stat_dump_finish */
1411 * Register an additional function for all dumper.
1413 void stat_register_dumper_func(dump_graph_FUNC func) {
1416 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1417 if (! dumper->func_map)
1418 dumper->func_map = pset_new_ptr(3);
1419 pset_insert_ptr(dumper->func_map, func);
1421 } /* stat_register_dumper_func */
1423 /* ---------------------------------------------------------------------- */
1426 * Helper: get an ir_op from an opcode.
1428 ir_op *stat_get_op_from_opcode(ir_opcode code) {
1429 return opcode_find_entry(code, status->ir_op_hash);
1430 } /* stat_get_op_from_opcode */
1433 * Hook: A new IR op is registered.
1435 * @param ctx the hook context
1436 * @param op the new IR opcode that was created.
1438 static void stat_new_ir_op(void *ctx, ir_op *op) {
1440 if (! status->stat_options)
1445 graph_entry_t *graph = graph_get_entry(NULL, status->irg_hash);
1447 /* execute for side effect :-) */
1448 (void)opcode_get_entry(op, graph->opcode_hash);
1450 pset_insert(status->ir_op_hash, op, op->code);
1453 } /* stat_new_ir_op */
1456 * Hook: An IR op is freed.
1458 * @param ctx the hook context
1459 * @param op the IR opcode that is freed
1461 static void stat_free_ir_op(void *ctx, ir_op *op) {
1464 if (! status->stat_options)
1471 } /* stat_free_ir_op */
1474 * Hook: A new node is created.
1476 * @param ctx the hook context
1477 * @param irg the IR graph on which the node is created
1478 * @param node the new IR node that was created
1480 static void stat_new_node(void *ctx, ir_graph *irg, ir_node *node) {
1483 if (! status->stat_options)
1486 /* do NOT count during dead node elimination */
1487 if (status->in_dead_node_elim)
1492 node_entry_t *entry;
1493 graph_entry_t *graph;
1494 ir_op *op = stat_get_irn_op(node);
1496 /* increase global value */
1497 graph = graph_get_entry(NULL, status->irg_hash);
1498 entry = opcode_get_entry(op, graph->opcode_hash);
1499 cnt_inc(&entry->new_node);
1501 /* increase local value */
1502 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1503 entry = opcode_get_entry(op, graph->opcode_hash);
1504 cnt_inc(&entry->new_node);
1507 } /* stat_new_node */
1510 * Hook: A node is changed into a Id node
1512 * @param ctx the hook context
1513 * @param node the IR node that will be turned into an ID
1515 static void stat_turn_into_id(void *ctx, ir_node *node) {
1517 if (! status->stat_options)
1522 node_entry_t *entry;
1523 graph_entry_t *graph;
1524 ir_op *op = stat_get_irn_op(node);
1526 /* increase global value */
1527 graph = graph_get_entry(NULL, status->irg_hash);
1528 entry = opcode_get_entry(op, graph->opcode_hash);
1529 cnt_inc(&entry->into_Id);
1531 /* increase local value */
1532 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1533 entry = opcode_get_entry(op, graph->opcode_hash);
1534 cnt_inc(&entry->into_Id);
1537 } /* stat_turn_into_id */
1540 * Hook: A new graph was created
1542 * @param ctx the hook context
1543 * @param irg the new IR graph that was created
1544 * @param ent the entity of this graph
1546 static void stat_new_graph(void *ctx, ir_graph *irg, ir_entity *ent) {
1548 if (! status->stat_options)
1553 /* execute for side effect :-) */
1554 graph_entry_t * graph = graph_get_entry(irg, status->irg_hash);
1557 graph->is_deleted = 0;
1559 graph->is_leaf_call = 0;
1560 graph->is_recursive = 0;
1561 graph->is_chain_call = 0;
1562 graph->is_strict = 1;
1563 graph->is_analyzed = 0;
1566 } /* stat_new_graph */
1569 * Hook: A graph will be deleted
1571 * @param ctx the hook context
1572 * @param irg the IR graph that will be deleted
1574 * Note that we still hold the information for this graph
1575 * in our hash maps, only a flag is set which prevents this
1576 * information from being changed, it's "frozen" from now.
1578 static void stat_free_graph(void *ctx, ir_graph *irg) {
1580 if (! status->stat_options)
1585 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1586 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1588 graph->is_deleted = 1;
1590 if (status->stat_options & FIRMSTAT_COUNT_DELETED) {
1591 /* count the nodes of the graph yet, it will be destroyed later */
1592 update_graph_stat(global, graph);
1596 } /* stat_free_graph */
1599 * Hook: A walk over a graph is initiated. Do not count walks from statistic code.
1601 * @param ctx the hook context
1602 * @param irg the IR graph that will be walked
1603 * @param pre the pre walker
1604 * @param post the post walker
1606 static void stat_irg_walk(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1611 if (! status->stat_options)
1616 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1618 cnt_inc(&graph->cnt[gcnt_acc_walked]);
1621 } /* stat_irg_walk */
1624 * Hook: A walk over a graph in block-wise order is initiated. Do not count walks from statistic code.
1626 * @param ctx the hook context
1627 * @param irg the IR graph that will be walked
1628 * @param pre the pre walker
1629 * @param post the post walker
1631 static void stat_irg_walk_blkwise(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1633 /* for now, do NOT differentiate between blockwise and normal */
1634 stat_irg_walk(ctx, irg, pre, post);
1635 } /* stat_irg_walk_blkwise */
1638 * Hook: A walk over the graph's blocks is initiated. Do not count walks from statistic code.
1640 * @param ctx the hook context
1641 * @param irg the IR graph that will be walked
1642 * @param node the IR node
1643 * @param pre the pre walker
1644 * @param post the post walker
1646 static void stat_irg_block_walk(void *ctx, ir_graph *irg, ir_node *node, generic_func *pre, generic_func *post)
1652 if (! status->stat_options)
1657 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1659 cnt_inc(&graph->cnt[gcnt_acc_walked_blocks]);
1662 } /* stat_irg_block_walk */
1665 * Called for every node that is removed due to an optimization.
1667 * @param n the IR node that will be removed
1668 * @param hmap the hash map containing ir_op* -> opt_entry_t*
1669 * @param kind the optimization kind
1671 static void removed_due_opt(ir_node *n, hmap_opt_entry_t *hmap, hook_opt_kind kind) {
1673 ir_op *op = stat_get_irn_op(n);
1675 /* ignore CSE for Constants */
1676 if (kind == HOOK_OPT_CSE && (is_Const(n) || is_SymConst(n)))
1679 /* increase global value */
1680 entry = opt_get_entry(op, hmap);
1681 cnt_inc(&entry->count);
1682 } /* removed_due_opt */
1685 * Hook: Some nodes were optimized into some others due to an optimization.
1687 * @param ctx the hook context
1689 static void stat_merge_nodes(
1691 ir_node **new_node_array, int new_num_entries,
1692 ir_node **old_node_array, int old_num_entries,
1696 if (! status->stat_options)
1702 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1704 cnt_inc(&status->num_opts[opt]);
1705 if (status->reassoc_run)
1706 opt = HOOK_OPT_REASSOC;
1708 for (i = 0; i < old_num_entries; ++i) {
1709 /* nodes might be in new and old, so if we found a node
1710 in both sets, this one is NOT removed */
1711 for (j = 0; j < new_num_entries; ++j) {
1712 if (old_node_array[i] == new_node_array[j])
1715 if (j >= new_num_entries) {
1718 /* sometimes we did not detect, that it is replaced by a Const */
1719 if (opt == HOOK_OPT_CONFIRM && new_num_entries == 1) {
1720 ir_op *op = get_irn_op(new_node_array[0]);
1722 if (op == op_Const || op == op_SymConst)
1723 xopt = HOOK_OPT_CONFIRM_C;
1726 removed_due_opt(old_node_array[i], graph->opt_hash[xopt], xopt);
1731 } /* stat_merge_nodes */
1734 * Hook: Reassociation is started/stopped.
1736 * @param ctx the hook context
1737 * @param flag if non-zero, reassociation is started else stopped
1739 static void stat_reassociate(void *ctx, int flag) {
1741 if (! status->stat_options)
1746 status->reassoc_run = flag;
1749 } /* stat_reassociate */
1752 * Hook: A node was lowered into other nodes
1754 * @param ctx the hook context
1755 * @param node the IR node that will be lowered
1757 static void stat_lower(void *ctx, ir_node *node) {
1759 if (! status->stat_options)
1764 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1766 removed_due_opt(node, graph->opt_hash[HOOK_LOWERED], HOOK_LOWERED);
1772 * Hook: A graph was inlined.
1774 * @param ctx the hook context
1775 * @param call the IR call that will re changed into the body of
1776 * the called IR graph
1777 * @param called_irg the IR graph representing the called routine
1779 static void stat_inline(void *ctx, ir_node *call, ir_graph *called_irg)
1782 if (! status->stat_options)
1787 ir_graph *irg = get_irn_irg(call);
1788 graph_entry_t *i_graph = graph_get_entry(called_irg, status->irg_hash);
1789 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1791 cnt_inc(&graph->cnt[gcnt_acc_got_inlined]);
1792 cnt_inc(&i_graph->cnt[gcnt_acc_was_inlined]);
1798 * Hook: A graph with tail-recursions was optimized.
1800 * @param ctx the hook context
1802 static void stat_tail_rec(void *ctx, ir_graph *irg, int n_calls) {
1804 if (! status->stat_options)
1809 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1811 graph->num_tail_recursion += n_calls;
1814 } /* stat_tail_rec */
1817 * Strength reduction was performed on an iteration variable.
1819 * @param ctx the hook context
1821 static void stat_strength_red(void *ctx, ir_graph *irg, ir_node *strong) {
1823 if (! status->stat_options)
1828 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1829 cnt_inc(&graph->cnt[gcnt_acc_strength_red]);
1831 removed_due_opt(strong, graph->opt_hash[HOOK_OPT_STRENGTH_RED], HOOK_OPT_STRENGTH_RED);
1834 } /* stat_strength_red */
1837 * Hook: Start/Stop the dead node elimination.
1839 * @param ctx the hook context
1841 static void stat_dead_node_elim(void *ctx, ir_graph *irg, int start) {
1844 if (! status->stat_options)
1847 status->in_dead_node_elim = (start != 0);
1848 } /* stat_dead_node_elim */
1851 * Hook: if-conversion was tried.
1853 static void stat_if_conversion(void *context, ir_graph *irg, ir_node *phi,
1854 int pos, ir_node *mux, if_result_t reason)
1860 if (! status->stat_options)
1865 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1867 cnt_inc(&graph->cnt[gcnt_if_conv + reason]);
1870 } /* stat_if_conversion */
1873 * Hook: real function call was optimized.
1875 static void stat_func_call(void *context, ir_graph *irg, ir_node *call)
1879 if (! status->stat_options)
1884 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1886 cnt_inc(&graph->cnt[gcnt_acc_real_func_call]);
1889 } /* stat_func_call */
1892 * Hook: A multiply was replaced by a series of Shifts/Adds/Subs.
1894 * @param ctx the hook context
1896 static void stat_arch_dep_replace_mul_with_shifts(void *ctx, ir_node *mul) {
1898 if (! status->stat_options)
1903 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1904 removed_due_opt(mul, graph->opt_hash[HOOK_OPT_ARCH_DEP], HOOK_OPT_ARCH_DEP);
1907 } /* stat_arch_dep_replace_mul_with_shifts */
1910 * Hook: A division by const was replaced.
1912 * @param ctx the hook context
1913 * @param node the division node that will be optimized
1915 static void stat_arch_dep_replace_division_by_const(void *ctx, ir_node *node) {
1917 if (! status->stat_options)
1922 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1923 removed_due_opt(node, graph->opt_hash[HOOK_OPT_ARCH_DEP], HOOK_OPT_ARCH_DEP);
1926 } /* stat_arch_dep_replace_division_by_const */
1929 * Update the register pressure of a block.
1931 * @param irg the irg containing the block
1932 * @param block the block for which the reg pressure should be set
1933 * @param pressure the pressure
1934 * @param class_name the name of the register class
1936 void stat_be_block_regpressure(ir_graph *irg, ir_node *block, int pressure, const char *class_name)
1938 if (! status->stat_options)
1943 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1944 be_block_entry_t *block_ent;
1945 reg_pressure_entry_t *rp_ent;
1947 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1948 rp_ent = obstack_alloc(&status->be_data, sizeof(*rp_ent));
1949 memset(rp_ent, 0, sizeof(*rp_ent));
1951 rp_ent->class_name = class_name;
1952 rp_ent->pressure = pressure;
1954 pset_insert(block_ent->reg_pressure, rp_ent, HASH_PTR(class_name));
1957 } /* stat_be_block_regpressure */
1960 * Update the distribution of ready nodes of a block
1962 * @param irg the irg containing the block
1963 * @param block the block for which the reg pressure should be set
1964 * @param num_ready the number of ready nodes
1966 void stat_be_block_sched_ready(ir_graph *irg, ir_node *block, int num_ready)
1968 if (! status->stat_options)
1973 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1974 be_block_entry_t *block_ent;
1976 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1978 /* increase the counter of corresponding number of ready nodes */
1979 stat_inc_int_distrib_tbl(block_ent->sched_ready, num_ready);
1982 } /* stat_be_block_sched_ready */
1985 * Update the permutation statistic of a block.
1987 * @param class_name the name of the register class
1988 * @param n_regs number of registers in the register class
1989 * @param perm the perm node
1990 * @param block the block containing the perm
1991 * @param size the size of the perm
1992 * @param real_size number of pairs with different registers
1994 void stat_be_block_stat_perm(const char *class_name, int n_regs, ir_node *perm, ir_node *block,
1995 int size, int real_size)
1997 if (! status->stat_options)
2002 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
2003 be_block_entry_t *block_ent;
2004 perm_class_entry_t *pc_ent;
2005 perm_stat_entry_t *ps_ent;
2007 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
2008 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
2009 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
2011 pc_ent->n_regs = n_regs;
2013 /* update information */
2014 ps_ent->size = size;
2015 ps_ent->real_size = real_size;
2018 } /* stat_be_block_stat_perm */
2021 * Update the permutation statistic of a single perm.
2023 * @param class_name the name of the register class
2024 * @param perm the perm node
2025 * @param block the block containing the perm
2026 * @param is_chain 1 if chain, 0 if cycle
2027 * @param size length of the cycle/chain
2028 * @param n_ops the number of ops representing this cycle/chain after lowering
2030 void stat_be_block_stat_permcycle(const char *class_name, ir_node *perm, ir_node *block,
2031 int is_chain, int size, int n_ops)
2033 if (! status->stat_options)
2038 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
2039 be_block_entry_t *block_ent;
2040 perm_class_entry_t *pc_ent;
2041 perm_stat_entry_t *ps_ent;
2043 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
2044 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
2045 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
2048 ps_ent->n_copies += n_ops;
2049 stat_inc_int_distrib_tbl(ps_ent->chains, size);
2051 ps_ent->n_exchg += n_ops;
2052 stat_inc_int_distrib_tbl(ps_ent->cycles, size);
2056 } /* stat_be_block_stat_permcycle */
2058 /* Dumps a statistics snapshot. */
2059 void stat_dump_snapshot(const char *name, const char *phase)
2065 if (! status->stat_options)
2070 graph_entry_t *entry;
2071 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
2074 * The constant counter is only global, so we clear it here.
2075 * Note that it does NOT contain the constants in DELETED
2076 * graphs due to this.
2078 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2079 stat_const_clear(status);
2081 /* build the name */
2082 p = strrchr(name, '/');
2087 q = strrchr(name, '\\');
2089 /* NULL might be not the smallest pointer */
2090 if (q && (!p || q > p))
2098 if (l > (int) (sizeof(fname) - 1))
2099 l = sizeof(fname) - 1;
2101 memcpy(fname, name, l);
2107 strncat(fname, "firmstat-", sizeof(fname));
2108 strncat(fname, phase, sizeof(fname));
2109 strncat(fname, "-", sizeof(fname));
2110 strncat(fname, p, sizeof(fname));
2112 stat_dump_init(fname);
2114 /* calculate the graph statistics */
2115 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
2116 if (entry->irg == NULL) {
2117 /* special entry for the global count */
2120 if (! entry->is_deleted) {
2121 /* the graph is still alive, count the nodes on it */
2122 update_graph_stat(global, entry);
2126 /* some calculations are dependent, we pushed them on the wait_q */
2127 while (! pdeq_empty(status->wait_q)) {
2128 entry = pdeq_getr(status->wait_q);
2130 update_graph_stat_2(global, entry);
2133 /* dump per graph */
2134 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
2135 if (entry->irg == NULL) {
2136 /* special entry for the global count */
2140 if (! entry->is_deleted || status->stat_options & FIRMSTAT_COUNT_DELETED) {
2141 stat_dump_graph(entry);
2142 stat_dump_registered(entry);
2145 if (! entry->is_deleted) {
2146 /* clear the counter that are not accumulated */
2147 graph_clear_entry(entry, 0);
2152 stat_dump_graph(global);
2154 /* dump the const info */
2155 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2156 stat_dump_consts(&status->const_info);
2158 /* dump the parameter distribution */
2159 stat_dump_param_tbl(status->dist_param_cnt, global);
2161 /* dump the optimization counter and clear them */
2162 stat_dump_opt_cnt(status->num_opts, ARR_SIZE(status->num_opts));
2163 clear_optimization_counter();
2167 stat_finish_pattern_history(fname);
2169 /* clear the global counter here */
2171 node_entry_t *entry;
2173 for (entry = pset_first(global->opcode_hash); entry; entry = pset_next(global->opcode_hash)) {
2174 opcode_clear_entry(entry);
2176 /* clear all global counter */
2177 graph_clear_entry(global, 1);
2181 } /* stat_dump_snapshot */
2183 /** the hook entries for the Firm statistics module */
2184 static hook_entry_t stat_hooks[hook_last];
2186 /* initialize the statistics module. */
2187 void firm_init_stat(unsigned enable_options)
2189 #define X(a) a, sizeof(a)-1
2190 #define HOOK(h, fkt) \
2191 stat_hooks[h].hook._##h = fkt; register_hook(h, &stat_hooks[h])
2194 if (! (enable_options & FIRMSTAT_ENABLED))
2197 status = XMALLOCZ(stat_info_t);
2199 /* enable statistics */
2200 status->stat_options = enable_options & FIRMSTAT_ENABLED ? enable_options : 0;
2202 /* register all hooks */
2203 HOOK(hook_new_ir_op, stat_new_ir_op);
2204 HOOK(hook_free_ir_op, stat_free_ir_op);
2205 HOOK(hook_new_node, stat_new_node);
2206 HOOK(hook_turn_into_id, stat_turn_into_id);
2207 HOOK(hook_new_graph, stat_new_graph);
2208 HOOK(hook_free_graph, stat_free_graph);
2209 HOOK(hook_irg_walk, stat_irg_walk);
2210 HOOK(hook_irg_walk_blkwise, stat_irg_walk_blkwise);
2211 HOOK(hook_irg_block_walk, stat_irg_block_walk);
2212 HOOK(hook_merge_nodes, stat_merge_nodes);
2213 HOOK(hook_reassociate, stat_reassociate);
2214 HOOK(hook_lower, stat_lower);
2215 HOOK(hook_inline, stat_inline);
2216 HOOK(hook_tail_rec, stat_tail_rec);
2217 HOOK(hook_strength_red, stat_strength_red);
2218 HOOK(hook_dead_node_elim, stat_dead_node_elim);
2219 HOOK(hook_if_conversion, stat_if_conversion);
2220 HOOK(hook_func_call, stat_func_call);
2221 HOOK(hook_arch_dep_replace_mul_with_shifts, stat_arch_dep_replace_mul_with_shifts);
2222 HOOK(hook_arch_dep_replace_division_by_const, stat_arch_dep_replace_division_by_const);
2224 obstack_init(&status->cnts);
2225 obstack_init(&status->be_data);
2227 /* create the hash-tables */
2228 status->irg_hash = new_pset(graph_cmp, 8);
2229 status->ir_op_hash = new_pset(opcode_cmp_2, 1);
2231 /* create the wait queue */
2232 status->wait_q = new_pdeq();
2234 if (enable_options & FIRMSTAT_COUNT_STRONG_OP) {
2235 /* build the pseudo-ops */
2237 _op_Phi0.code = --num;
2238 _op_Phi0.name = new_id_from_chars(X("Phi0"));
2240 _op_PhiM.code = --num;
2241 _op_PhiM.name = new_id_from_chars(X("PhiM"));
2243 _op_ProjM.code = --num;
2244 _op_ProjM.name = new_id_from_chars(X("ProjM"));
2246 _op_MulC.code = --num;
2247 _op_MulC.name = new_id_from_chars(X("MulC"));
2249 _op_DivC.code = --num;
2250 _op_DivC.name = new_id_from_chars(X("DivC"));
2252 _op_ModC.code = --num;
2253 _op_ModC.name = new_id_from_chars(X("ModC"));
2255 _op_DivModC.code = --num;
2256 _op_DivModC.name = new_id_from_chars(X("DivModC"));
2258 _op_QuotC.code = --num;
2259 _op_QuotC.name = new_id_from_chars(X("QuotC"));
2261 status->op_Phi0 = &_op_Phi0;
2262 status->op_PhiM = &_op_PhiM;
2263 status->op_ProjM = &_op_ProjM;
2264 status->op_MulC = &_op_MulC;
2265 status->op_DivC = &_op_DivC;
2266 status->op_ModC = &_op_ModC;
2267 status->op_DivModC = &_op_DivModC;
2268 status->op_QuotC = &_op_QuotC;
2270 status->op_Phi0 = NULL;
2271 status->op_PhiM = NULL;
2272 status->op_ProjM = NULL;
2273 status->op_MulC = NULL;
2274 status->op_DivC = NULL;
2275 status->op_ModC = NULL;
2276 status->op_DivModC = NULL;
2277 status->op_QuotC = NULL;
2280 /* for Florian: count the Sel depth */
2281 if (enable_options & FIRMSTAT_COUNT_SELS) {
2282 _op_SelSel.code = --num;
2283 _op_SelSel.name = new_id_from_chars(X("Sel(Sel)"));
2285 _op_SelSelSel.code = --num;
2286 _op_SelSelSel.name = new_id_from_chars(X("Sel(Sel(Sel))"));
2288 status->op_SelSel = &_op_SelSel;
2289 status->op_SelSelSel = &_op_SelSelSel;
2291 status->op_SelSel = NULL;
2292 status->op_SelSelSel = NULL;
2295 /* register the dumper */
2296 stat_register_dumper(&simple_dumper);
2298 if (enable_options & FIRMSTAT_CSV_OUTPUT)
2299 stat_register_dumper(&csv_dumper);
2301 /* initialize the pattern hash */
2302 stat_init_pattern_history(enable_options & FIRMSTAT_PATTERN_ENABLED);
2304 /* initialize the Const options */
2305 if (enable_options & FIRMSTAT_COUNT_CONSTS)
2306 stat_init_const_cnt(status);
2308 /* distribution table for parameter counts */
2309 status->dist_param_cnt = stat_new_int_distrib_tbl();
2311 clear_optimization_counter();
2315 } /* firm_init_stat */
2318 * Frees all dumper structures.
2320 static void stat_term_dumper(void) {
2321 dumper_t *dumper, *next_dumper;
2323 for (dumper = status->dumper; dumper; /* iteration done in loop body */ ) {
2324 if (dumper->func_map)
2325 del_pset(dumper->func_map);
2327 next_dumper = dumper->next;
2329 dumper = next_dumper;
2331 } /* stat_term_dumper */
2334 /* Terminates the statistics module, frees all memory. */
2335 void stat_term(void) {
2336 if (status != (stat_info_t *)&status_disable) {
2337 obstack_free(&status->be_data, NULL);
2338 obstack_free(&status->cnts, NULL);
2343 status = (stat_info_t *)&status_disable;
2347 /* returns 1 if statistics were initialized, 0 otherwise */
2348 int stat_is_active(void) {
2349 return status != (stat_info_t *)&status_disable;
2350 } /* stat_is_active */
2354 /* initialize the statistics module. */
2355 void firm_init_stat(unsigned enable_options) {}
2357 /* Dumps a statistics snapshot */
2358 void stat_dump_snapshot(const char *name, const char *phase) {}
2360 /* terminates the statistics module, frees all memory */
2361 void stat_term(void);
2363 #endif /* FIRM_STATISTICS */