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
35 #include "firmstat_t.h"
45 * need this to be static:
46 * Special pseudo Opcodes that we need to count some interesting cases
50 * The Phi0, a node that is created during SSA construction
52 static ir_op _op_Phi0;
54 /** The PhiM, just to count memory Phi's. */
55 static ir_op _op_PhiM;
57 /** The Mul by Const node. */
58 static ir_op _op_MulC;
60 /** The Div by Const node. */
61 static ir_op _op_DivC;
63 /** The Div by Const node. */
64 static ir_op _op_ModC;
66 /** The memory Proj node. */
67 static ir_op _op_ProjM;
70 static ir_op _op_SelSel;
72 /** A Sel of a Sel of a Sel */
73 static ir_op _op_SelSelSel;
75 /* ---------------------------------------------------------------------------------- */
77 /** Marks the begin of a statistic (hook) function. */
78 #define STAT_ENTER ++status->recursive
80 /** Marks the end of a statistic (hook) functions. */
81 #define STAT_LEAVE --status->recursive
83 /** Allows to enter a statistic function only when we are not already in a hook. */
84 #define STAT_ENTER_SINGLE do { if (status->recursive > 0) return; ++status->recursive; } while (0)
89 static const unsigned status_disable = 0;
90 static stat_info_t *status = (stat_info_t *)&status_disable;
93 * Compare two elements of the opcode hash.
95 static int opcode_cmp(const void *elt, const void *key)
97 const node_entry_t *e1 = (const node_entry_t*)elt;
98 const node_entry_t *e2 = (const node_entry_t*)key;
100 return e1->op->code - e2->op->code;
104 * Compare two elements of the graph hash.
106 static int graph_cmp(const void *elt, const void *key)
108 const graph_entry_t *e1 = (const graph_entry_t*)elt;
109 const graph_entry_t *e2 = (const graph_entry_t*)key;
111 return e1->irg != e2->irg;
115 * Compare two elements of the optimization hash.
117 static int opt_cmp(const void *elt, const void *key)
119 const opt_entry_t *e1 = (const opt_entry_t*)elt;
120 const opt_entry_t *e2 = (const opt_entry_t*)key;
122 return e1->op->code != e2->op->code;
126 * Compare two elements of the block/extbb hash.
128 static int block_cmp(const void *elt, const void *key)
130 const block_entry_t *e1 = (const block_entry_t*)elt;
131 const block_entry_t *e2 = (const block_entry_t*)key;
133 /* it's enough to compare the block number */
134 return e1->block_nr != e2->block_nr;
138 * Compare two elements of the be_block hash.
140 static int be_block_cmp(const void *elt, const void *key)
142 const be_block_entry_t *e1 = (const be_block_entry_t*)elt;
143 const be_block_entry_t *e2 = (const be_block_entry_t*)key;
145 return e1->block_nr != e2->block_nr;
149 * Compare two elements of reg pressure hash.
151 static int reg_pressure_cmp(const void *elt, const void *key)
153 const reg_pressure_entry_t *e1 = (const reg_pressure_entry_t*)elt;
154 const reg_pressure_entry_t *e2 = (const reg_pressure_entry_t*)key;
156 return e1->class_name != e2->class_name;
157 } /* reg_pressure_cmp */
160 * Compare two elements of the perm_stat hash.
162 static int perm_stat_cmp(const void *elt, const void *key)
164 const perm_stat_entry_t *e1 = (const perm_stat_entry_t*)elt;
165 const perm_stat_entry_t *e2 = (const perm_stat_entry_t*)key;
167 return e1->perm != e2->perm;
168 } /* perm_stat_cmp */
171 * Compare two elements of the perm_class hash.
173 static int perm_class_cmp(const void *elt, const void *key)
175 const perm_class_entry_t *e1 = (const perm_class_entry_t*)elt;
176 const perm_class_entry_t *e2 = (const perm_class_entry_t*)key;
178 return e1->class_name != e2->class_name;
179 } /* perm_class_cmp */
182 * Compare two elements of the ir_op hash.
184 static int opcode_cmp_2(const void *elt, const void *key)
186 const ir_op *e1 = (const ir_op*)elt;
187 const ir_op *e2 = (const ir_op*)key;
189 return e1->code != e2->code;
193 * Compare two elements of the address_mark set.
195 static int address_mark_cmp(const void *elt, const void *key, size_t size)
197 const address_mark_entry_t *e1 = (const address_mark_entry_t*)elt;
198 const address_mark_entry_t *e2 = (const address_mark_entry_t*)key;
201 /* compare only the nodes, the rest is used as data container */
202 return e1->node != e2->node;
203 } /* address_mark_cmp */
206 * Clear all counter in a node_entry_t.
208 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 cnt_clr(&elem->normalized);
214 } /* opcode_clear_entry */
217 * Returns the associates node_entry_t for an ir_op (and allocates
218 * one if not yet available).
220 * @param op the IR operation
221 * @param hmap a hash map containing ir_op* -> node_entry_t*
223 static node_entry_t *opcode_get_entry(const ir_op *op, hmap_node_entry_t *hmap)
230 elem = (node_entry_t*)pset_find(hmap, &key, op->code);
234 elem = OALLOCZ(&status->cnts, node_entry_t);
237 opcode_clear_entry(elem);
241 return (node_entry_t*)pset_insert(hmap, elem, op->code);
242 } /* opcode_get_entry */
245 * Returns the associates ir_op for an opcode
247 * @param code the IR opcode
248 * @param hmap the hash map containing opcode -> ir_op*
250 static ir_op *opcode_find_entry(ir_opcode code, hmap_ir_op *hmap)
255 return (ir_op*)pset_find(hmap, &key, code);
256 } /* opcode_find_entry */
259 * Clears all counter in a graph_entry_t.
261 * @param elem the graph entry
262 * @param all if non-zero, clears all counters, else leave accumulated ones
264 static void graph_clear_entry(graph_entry_t *elem, int all)
268 /* clear accumulated / non-accumulated counter */
269 for (i = all ? 0 : _gcnt_non_acc; i < _gcnt_last; ++i) {
270 cnt_clr(&elem->cnt[i]);
273 if (elem->block_hash) {
274 del_pset(elem->block_hash);
275 elem->block_hash = NULL;
278 if (elem->extbb_hash) {
279 del_pset(elem->extbb_hash);
280 elem->extbb_hash = NULL;
283 obstack_free(&elem->recalc_cnts, NULL);
284 obstack_init(&elem->recalc_cnts);
285 } /* graph_clear_entry */
288 * Returns the associated graph_entry_t for an IR graph.
290 * @param irg the IR graph, NULL for the global counter
291 * @param hmap the hash map containing ir_graph* -> graph_entry_t*
293 static graph_entry_t *graph_get_entry(ir_graph *irg, hmap_graph_entry_t *hmap)
301 elem = (graph_entry_t*)pset_find(hmap, &key, HASH_PTR(irg));
304 /* create hash map backend block information */
305 if (! elem->be_block_hash)
306 elem->be_block_hash = new_pset(be_block_cmp, 5);
311 /* allocate a new one */
312 elem = OALLOCZ(&status->cnts, graph_entry_t);
313 obstack_init(&elem->recalc_cnts);
316 graph_clear_entry(elem, 1);
318 /* new hash table for opcodes here */
319 elem->opcode_hash = new_pset(opcode_cmp, 5);
320 elem->address_mark = new_set(address_mark_cmp, 5);
323 /* these hash tables are created on demand */
324 elem->block_hash = NULL;
325 elem->extbb_hash = NULL;
327 for (i = 0; i < sizeof(elem->opt_hash)/sizeof(elem->opt_hash[0]); ++i)
328 elem->opt_hash[i] = new_pset(opt_cmp, 4);
330 return (graph_entry_t*)pset_insert(hmap, elem, HASH_PTR(irg));
331 } /* graph_get_entry */
334 * Clear all counter in an opt_entry_t.
336 static void opt_clear_entry(opt_entry_t *elem)
338 cnt_clr(&elem->count);
339 } /* opt_clear_entry */
342 * Returns the associated opt_entry_t for an IR operation.
344 * @param op the IR operation
345 * @param hmap the hash map containing ir_op* -> opt_entry_t*
347 static opt_entry_t *opt_get_entry(const ir_op *op, hmap_opt_entry_t *hmap)
354 elem = (opt_entry_t*)pset_find(hmap, &key, op->code);
358 elem = OALLOCZ(&status->cnts, opt_entry_t);
360 /* clear new counter */
361 opt_clear_entry(elem);
365 return (opt_entry_t*)pset_insert(hmap, elem, op->code);
366 } /* opt_get_entry */
369 * clears all counter in a block_entry_t
371 static void block_clear_entry(block_entry_t *elem)
375 for (i = 0; i < _bcnt_last; ++i)
376 cnt_clr(&elem->cnt[i]);
377 } /* block_clear_entry */
380 * Returns the associated block_entry_t for an block.
382 * @param block_nr an IR block number
383 * @param hmap a hash map containing long -> block_entry_t
385 static block_entry_t *block_get_entry(struct obstack *obst, long block_nr, hmap_block_entry_t *hmap)
390 key.block_nr = block_nr;
392 elem = (block_entry_t*)pset_find(hmap, &key, block_nr);
396 elem = OALLOCZ(obst, block_entry_t);
398 /* clear new counter */
399 block_clear_entry(elem);
401 elem->block_nr = block_nr;
403 return (block_entry_t*)pset_insert(hmap, elem, block_nr);
404 } /* block_get_entry */
407 * Clear all sets in be_block_entry_t.
409 static void be_block_clear_entry(be_block_entry_t *elem)
411 if (elem->reg_pressure)
412 del_pset(elem->reg_pressure);
414 if (elem->sched_ready)
415 stat_delete_distrib_tbl(elem->sched_ready);
417 if (elem->perm_class_stat)
418 del_pset(elem->perm_class_stat);
420 elem->reg_pressure = new_pset(reg_pressure_cmp, 5);
421 elem->sched_ready = stat_new_int_distrib_tbl();
422 elem->perm_class_stat = new_pset(perm_class_cmp, 5);
423 } /* be_block_clear_entry */
426 * Returns the associated be_block_entry_t for an block.
428 * @param block_nr an IR block number
429 * @param hmap a hash map containing long -> be_block_entry_t
431 static be_block_entry_t *be_block_get_entry(struct obstack *obst, long block_nr, hmap_be_block_entry_t *hmap)
433 be_block_entry_t key;
434 be_block_entry_t *elem;
436 key.block_nr = block_nr;
438 elem = (be_block_entry_t*)pset_find(hmap, &key, block_nr);
442 elem = OALLOCZ(obst, be_block_entry_t);
444 /* clear new counter */
445 be_block_clear_entry(elem);
447 elem->block_nr = block_nr;
449 return (be_block_entry_t*)pset_insert(hmap, elem, block_nr);
450 } /* be_block_get_entry */
453 * clears all sets in perm_class_entry_t
455 static void perm_class_clear_entry(perm_class_entry_t *elem)
458 del_pset(elem->perm_stat);
460 elem->perm_stat = new_pset(perm_stat_cmp, 5);
461 } /* perm_class_clear_entry */
464 * Returns the associated perm_class entry for a register class.
466 * @param class_name the register class name
467 * @param hmap a hash map containing class_name -> perm_class_entry_t
469 static perm_class_entry_t *perm_class_get_entry(struct obstack *obst, const char *class_name,
470 hmap_perm_class_entry_t *hmap)
472 perm_class_entry_t key;
473 perm_class_entry_t *elem;
475 key.class_name = class_name;
477 elem = (perm_class_entry_t*)pset_find(hmap, &key, HASH_PTR(class_name));
481 elem = OALLOCZ(obst, perm_class_entry_t);
483 /* clear new counter */
484 perm_class_clear_entry(elem);
486 elem->class_name = class_name;
488 return (perm_class_entry_t*)pset_insert(hmap, elem, HASH_PTR(class_name));
489 } /* perm_class_get_entry */
492 * clears all sets in perm_stat_entry_t
494 static void perm_stat_clear_entry(perm_stat_entry_t *elem)
497 stat_delete_distrib_tbl(elem->chains);
500 stat_delete_distrib_tbl(elem->cycles);
502 elem->chains = stat_new_int_distrib_tbl();
503 elem->cycles = stat_new_int_distrib_tbl();
504 } /* perm_stat_clear_entry */
507 * Returns the associated perm_stat entry for a perm.
509 * @param perm the perm node
510 * @param hmap a hash map containing perm -> perm_stat_entry_t
512 static perm_stat_entry_t *perm_stat_get_entry(struct obstack *obst, ir_node *perm, hmap_perm_stat_entry_t *hmap)
514 perm_stat_entry_t key;
515 perm_stat_entry_t *elem;
519 elem = (perm_stat_entry_t*)pset_find(hmap, &key, HASH_PTR(perm));
523 elem = OALLOCZ(obst, perm_stat_entry_t);
525 /* clear new counter */
526 perm_stat_clear_entry(elem);
530 return (perm_stat_entry_t*)pset_insert(hmap, elem, HASH_PTR(perm));
531 } /* perm_stat_get_entry */
534 * Clear optimizations counter,
536 static void clear_optimization_counter(void)
539 for (i = 0; i < FS_OPT_MAX; ++i)
540 cnt_clr(&status->num_opts[i]);
544 * Returns the ir_op for an IR-node,
545 * handles special cases and return pseudo op codes.
547 * @param none an IR node
549 static ir_op *stat_get_irn_op(ir_node *node)
551 ir_op *op = get_irn_op(node);
552 unsigned opc = op->code;
556 if (get_irn_arity(node) == 0) {
557 /* special case, a Phi0 node, count on extra counter */
558 op = status->op_Phi0 ? status->op_Phi0 : op;
559 } else if (get_irn_mode(node) == mode_M) {
560 /* special case, a Memory Phi node, count on extra counter */
561 op = status->op_PhiM ? status->op_PhiM : op;
565 if (get_irn_mode(node) == mode_M) {
566 /* special case, a Memory Proj node, count on extra counter */
567 op = status->op_ProjM ? status->op_ProjM : op;
571 if (is_Const(get_Mul_left(node)) || is_Const(get_Mul_right(node))) {
572 /* special case, a Multiply by a const, count on extra counter */
573 op = status->op_MulC ? status->op_MulC : op;
577 if (is_Const(get_Div_right(node))) {
578 /* special case, a division by a const, count on extra counter */
579 op = status->op_DivC ? status->op_DivC : op;
583 if (is_Const(get_Mod_right(node))) {
584 /* special case, a module by a const, count on extra counter */
585 op = status->op_ModC ? status->op_ModC : op;
589 if (is_Sel(get_Sel_ptr(node))) {
590 /* special case, a Sel of a Sel, count on extra counter */
591 op = status->op_SelSel ? status->op_SelSel : op;
592 if (is_Sel(get_Sel_ptr(get_Sel_ptr(node)))) {
593 /* special case, a Sel of a Sel of a Sel, count on extra counter */
594 op = status->op_SelSelSel ? status->op_SelSelSel : op;
603 } /* stat_get_irn_op */
606 * update the block counter
608 static void undate_block_info(ir_node *node, graph_entry_t *graph)
610 ir_op *op = get_irn_op(node);
612 block_entry_t *b_entry;
615 /* check for block */
616 if (op == op_Block) {
617 arity = get_irn_arity(node);
618 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(node), graph->block_hash);
619 /* mark start end block to allow to filter them out */
620 if (node == get_irg_start_block(graph->irg))
621 b_entry->is_start = 1;
622 else if (node == get_irg_end_block(graph->irg))
625 /* count all incoming edges */
626 for (i = 0; i < arity; ++i) {
627 ir_node *pred = get_irn_n(node, i);
628 ir_node *other_block = get_nodes_block(pred);
629 block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
631 cnt_inc(&b_entry->cnt[bcnt_in_edges]); /* an edge coming from another block */
632 cnt_inc(&b_entry_other->cnt[bcnt_out_edges]);
637 block = get_nodes_block(node);
638 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(block), graph->block_hash);
640 if (op == op_Phi && mode_is_datab(get_irn_mode(node))) {
641 /* count data Phi per block */
642 cnt_inc(&b_entry->cnt[bcnt_phi_data]);
645 /* we have a new node in our block */
646 cnt_inc(&b_entry->cnt[bcnt_nodes]);
648 /* don't count keep-alive edges */
652 arity = get_irn_arity(node);
654 for (i = 0; i < arity; ++i) {
655 ir_node *pred = get_irn_n(node, i);
656 ir_node *other_block;
658 other_block = get_nodes_block(pred);
660 if (other_block == block)
661 cnt_inc(&b_entry->cnt[bcnt_edges]); /* a in block edge */
663 block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
665 cnt_inc(&b_entry->cnt[bcnt_in_edges]); /* an edge coming from another block */
666 cnt_inc(&b_entry_other->cnt[bcnt_out_edges]);
669 } /* undate_block_info */
672 * Update the extended block counter.
674 static void update_extbb_info(ir_node *node, graph_entry_t *graph)
676 ir_op *op = get_irn_op(node);
678 extbb_entry_t *eb_entry;
681 /* check for block */
682 if (op == op_Block) {
683 extbb = get_nodes_extbb(node);
684 arity = get_irn_arity(node);
685 eb_entry = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(extbb), graph->extbb_hash);
687 /* count all incoming edges */
688 for (i = 0; i < arity; ++i) {
689 ir_node *pred = get_irn_n(node, i);
690 ir_extblk *other_extbb = get_nodes_extbb(pred);
692 if (extbb != other_extbb) {
693 extbb_entry_t *eb_entry_other = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(other_extbb), graph->extbb_hash);
695 cnt_inc(&eb_entry->cnt[bcnt_in_edges]); /* an edge coming from another extbb */
696 cnt_inc(&eb_entry_other->cnt[bcnt_out_edges]);
702 extbb = get_nodes_extbb(node);
703 eb_entry = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(extbb), graph->extbb_hash);
705 if (op == op_Phi && mode_is_datab(get_irn_mode(node))) {
706 /* count data Phi per extbb */
707 cnt_inc(&eb_entry->cnt[bcnt_phi_data]);
710 /* we have a new node in our block */
711 cnt_inc(&eb_entry->cnt[bcnt_nodes]);
713 /* don't count keep-alive edges */
717 arity = get_irn_arity(node);
719 for (i = 0; i < arity; ++i) {
720 ir_node *pred = get_irn_n(node, i);
721 ir_extblk *other_extbb = get_nodes_extbb(pred);
723 if (other_extbb == extbb)
724 cnt_inc(&eb_entry->cnt[bcnt_edges]); /* a in extbb edge */
726 extbb_entry_t *eb_entry_other = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(other_extbb), graph->extbb_hash);
728 cnt_inc(&eb_entry->cnt[bcnt_in_edges]); /* an edge coming from another extbb */
729 cnt_inc(&eb_entry_other->cnt[bcnt_out_edges]);
732 } /* update_extbb_info */
735 * Calculates how many arguments of the call are const, updates
736 * param distribution.
738 static void analyse_params_of_Call(graph_entry_t *graph, ir_node *call)
740 int i, num_const_args = 0, num_local_adr = 0;
741 int n = get_Call_n_params(call);
743 for (i = 0; i < n; ++i) {
744 ir_node *param = get_Call_param(call, i);
746 if (is_irn_constlike(param))
748 else if (is_Sel(param)) {
749 ir_node *base = param;
752 base = get_Sel_ptr(base);
753 } while (is_Sel(base));
755 if (base == get_irg_frame(current_ir_graph))
761 if (num_const_args > 0)
762 cnt_inc(&graph->cnt[gcnt_call_with_cnst_arg]);
763 if (num_const_args == n)
764 cnt_inc(&graph->cnt[gcnt_call_with_all_cnst_arg]);
765 if (num_local_adr > 0)
766 cnt_inc(&graph->cnt[gcnt_call_with_local_adr]);
768 stat_inc_int_distrib_tbl(status->dist_param_cnt, n);
769 } /* analyse_params_of_Call */
772 * Update info on calls.
774 * @param call The call
775 * @param graph The graph entry containing the call
777 static void stat_update_call(ir_node *call, graph_entry_t *graph)
779 ir_node *block = get_nodes_block(call);
780 ir_node *ptr = get_Call_ptr(call);
781 ir_entity *ent = NULL;
782 ir_graph *callee = NULL;
785 * If the block is bad, the whole subgraph will collapse later
786 * so do not count this call.
787 * This happens in dead code.
792 cnt_inc(&graph->cnt[gcnt_all_calls]);
794 /* found a call, this function is not a leaf */
797 if (is_SymConst(ptr)) {
798 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
799 /* ok, we seems to know the entity */
800 ent = get_SymConst_entity(ptr);
801 callee = get_entity_irg(ent);
803 /* it is recursive, if it calls at least once */
804 if (callee == graph->irg)
805 graph->is_recursive = 1;
807 cnt_inc(&graph->cnt[gcnt_external_calls]);
810 /* indirect call, be could not predict */
811 cnt_inc(&graph->cnt[gcnt_indirect_calls]);
813 /* NOT a leaf call */
814 graph->is_leaf_call = LCS_NON_LEAF_CALL;
817 /* check, if it's a chain-call: Then, the call-block
818 * must dominate the end block. */
820 ir_node *curr = get_irg_end_block(graph->irg);
821 int depth = get_Block_dom_depth(block);
823 for (; curr != block && get_Block_dom_depth(curr) > depth;) {
824 curr = get_Block_idom(curr);
826 if (! curr || !is_Block(curr))
831 graph->is_chain_call = 0;
834 /* check, if the callee is a leaf */
836 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
838 if (called->is_analyzed) {
839 if (! called->is_leaf)
840 graph->is_leaf_call = LCS_NON_LEAF_CALL;
844 analyse_params_of_Call(graph, call);
845 } /* stat_update_call */
848 * Update info on calls for graphs on the wait queue.
850 static void stat_update_call_2(ir_node *call, graph_entry_t *graph)
852 ir_node *block = get_nodes_block(call);
853 ir_node *ptr = get_Call_ptr(call);
854 ir_entity *ent = NULL;
855 ir_graph *callee = NULL;
858 * If the block is bad, the whole subgraph will collapse later
859 * so do not count this call.
860 * This happens in dead code.
865 if (is_SymConst(ptr)) {
866 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
867 /* ok, we seems to know the entity */
868 ent = get_SymConst_entity(ptr);
869 callee = get_entity_irg(ent);
873 /* check, if the callee is a leaf */
875 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
877 assert(called->is_analyzed);
879 if (! called->is_leaf)
880 graph->is_leaf_call = LCS_NON_LEAF_CALL;
882 graph->is_leaf_call = LCS_NON_LEAF_CALL;
883 } /* stat_update_call_2 */
886 * Find the base address and entity of an Sel node.
888 * @param sel the node
890 * @return the base address.
892 static ir_node *find_base_adr(ir_node *sel)
894 ir_node *ptr = get_Sel_ptr(sel);
896 while (is_Sel(ptr)) {
898 ptr = get_Sel_ptr(sel);
901 } /* find_base_adr */
904 * Update info on Load/Store address statistics.
906 static void stat_update_address(ir_node *node, graph_entry_t *graph)
908 unsigned opc = get_irn_opcode(node);
914 /* a global address */
915 cnt_inc(&graph->cnt[gcnt_global_adr]);
918 base = find_base_adr(node);
919 irg = current_ir_graph;
920 if (base == get_irg_frame(irg)) {
921 /* a local Variable. */
922 cnt_inc(&graph->cnt[gcnt_local_adr]);
925 if (is_Proj(base) && skip_Proj(get_Proj_pred(base)) == get_irg_start(irg)) {
926 /* pointer access through parameter, check for THIS */
927 ir_entity *ent = get_irg_entity(irg);
930 ir_type *ent_tp = get_entity_type(ent);
932 if (get_method_calling_convention(ent_tp) & cc_this_call) {
933 if (get_Proj_proj(base) == 0) {
935 cnt_inc(&graph->cnt[gcnt_this_adr]);
940 /* other parameter */
941 cnt_inc(&graph->cnt[gcnt_param_adr]);
944 /* unknown Pointer access */
945 cnt_inc(&graph->cnt[gcnt_other_adr]);
951 } /* stat_update_address */
954 * Walker for reachable nodes count.
956 static void update_node_stat(ir_node *node, void *env)
958 graph_entry_t *graph = (graph_entry_t*)env;
961 ir_op *op = stat_get_irn_op(node);
962 int i, arity = get_irn_arity(node);
964 entry = opcode_get_entry(op, graph->opcode_hash);
966 cnt_inc(&entry->cnt_alive);
967 cnt_add_i(&graph->cnt[gcnt_edges], arity);
969 /* count block edges */
970 undate_block_info(node, graph);
972 /* count extended block edges */
973 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
974 if (graph->irg != get_const_code_irg())
975 update_extbb_info(node, graph);
978 /* handle statistics for special node types */
982 /* check for properties that depends on calls like recursion/leaf/indirect call */
983 stat_update_call(node, graph);
986 /* check address properties */
987 stat_update_address(get_Load_ptr(node), graph);
990 /* check address properties */
991 stat_update_address(get_Store_ptr(node), graph);
994 /* check for non-strict Phi nodes */
995 for (i = arity - 1; i >= 0; --i) {
996 ir_node *pred = get_Phi_pred(node, i);
997 if (is_Unknown(pred)) {
998 /* found an Unknown predecessor, graph is not strict */
999 graph->is_strict = 0;
1007 /* we want to count the constant IN nodes, not the CSE'ed constant's itself */
1008 if (status->stat_options & FIRMSTAT_COUNT_CONSTS) {
1011 for (i = get_irn_arity(node) - 1; i >= 0; --i) {
1012 ir_node *pred = get_irn_n(node, i);
1014 if (is_Const(pred)) {
1015 /* check properties of constants */
1016 stat_update_const(status, pred, graph);
1020 } /* update_node_stat */
1023 * Walker for reachable nodes count for graphs on the wait_q.
1025 static void update_node_stat_2(ir_node *node, void *env)
1027 graph_entry_t *graph = (graph_entry_t*)env;
1029 /* check for properties that depends on calls like recursion/leaf/indirect call */
1031 stat_update_call_2(node, graph);
1032 } /* update_node_stat_2 */
1035 * Get the current address mark.
1037 static unsigned get_adr_mark(graph_entry_t *graph, ir_node *node)
1039 address_mark_entry_t *value = (address_mark_entry_t*)set_find(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
1041 return value ? value->mark : 0;
1042 } /* get_adr_mark */
1045 * Set the current address mark.
1047 static void set_adr_mark(graph_entry_t *graph, ir_node *node, unsigned val)
1049 address_mark_entry_t *value = (address_mark_entry_t*)set_insert(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
1052 } /* set_adr_mark */
1054 #undef DUMP_ADR_MODE
1056 #ifdef DUMP_ADR_MODE
1058 * a vcg attribute hook: Color a node with a different color if
1059 * it's identified as a part of an address expression or at least referenced
1060 * by an address expression.
1062 static int stat_adr_mark_hook(FILE *F, ir_node *node, ir_node *local)
1064 ir_node *n = local ? local : node;
1065 ir_graph *irg = get_irn_irg(n);
1066 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1067 unsigned mark = get_adr_mark(graph, n);
1069 if (mark & MARK_ADDRESS_CALC)
1070 fprintf(F, "color: purple");
1071 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1072 fprintf(F, "color: pink");
1073 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1074 fprintf(F, "color: lightblue");
1078 /* I know the color! */
1080 } /* stat_adr_mark_hook */
1081 #endif /* DUMP_ADR_MODE */
1084 * Return the "operational" mode of a Firm node.
1086 static ir_mode *get_irn_op_mode(ir_node *node)
1088 switch (get_irn_opcode(node)) {
1090 return get_Load_mode(node);
1092 return get_irn_mode(get_Store_value(node));
1094 return get_irn_mode(get_Div_left(node));
1096 return get_irn_mode(get_Mod_left(node));
1098 /* Cmp is no address calculation, or is it? */
1100 return get_irn_mode(node);
1102 } /* get_irn_op_mode */
1105 * Post-walker that marks every node that is an address calculation.
1107 * Users of a node must be visited first. We ensure this by
1108 * calling it in the post of an outs walk. This should work even in cycles,
1109 * while the normal pre-walk will not.
1111 static void mark_address_calc(ir_node *node, void *env)
1113 graph_entry_t *graph = (graph_entry_t*)env;
1114 ir_mode *mode = get_irn_op_mode(node);
1116 unsigned mark_preds = MARK_REF_NON_ADR;
1118 if (! mode_is_data(mode))
1121 if (mode_is_reference(mode)) {
1122 /* a reference is calculated here, we are sure */
1123 set_adr_mark(graph, node, MARK_ADDRESS_CALC);
1125 mark_preds = MARK_REF_ADR;
1127 unsigned mark = get_adr_mark(graph, node);
1129 if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR) {
1131 * this node has no reference mode, but is only
1132 * referenced by address calculations
1134 mark_preds = MARK_REF_ADR;
1138 /* mark all predecessors */
1139 for (i = 0, n = get_irn_arity(node); i < n; ++i) {
1140 ir_node *pred = get_irn_n(node, i);
1142 mode = get_irn_op_mode(pred);
1143 if (! mode_is_data(mode))
1146 set_adr_mark(graph, pred, get_adr_mark(graph, pred) | mark_preds);
1148 } /* mark_address_calc */
1151 * Post-walker that marks every node that is an address calculation.
1153 * Users of a node must be visited first. We ensure this by
1154 * calling it in the post of an outs walk. This should work even in cycles,
1155 * while the normal pre-walk will not.
1157 static void count_adr_ops(ir_node *node, void *env)
1159 graph_entry_t *graph = (graph_entry_t*)env;
1160 unsigned mark = get_adr_mark(graph, node);
1162 if (mark & MARK_ADDRESS_CALC)
1163 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1164 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1165 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1166 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1167 cnt_inc(&graph->cnt[gcnt_all_adr_ops]);
1168 } /* count_adr_ops */
1171 * Called for every graph when the graph is either deleted or stat_dump_snapshot()
1172 * is called, must recalculate all statistic info.
1174 * @param global The global entry
1175 * @param graph The current entry
1177 static void update_graph_stat(graph_entry_t *global, graph_entry_t *graph)
1179 node_entry_t *entry;
1182 /* clear first the alive counter in the graph */
1183 foreach_pset(graph->opcode_hash, node_entry_t*, entry) {
1184 cnt_clr(&entry->cnt_alive);
1185 } /* foreach_pset */
1187 /* set pessimistic values */
1189 graph->is_leaf_call = LCS_UNKNOWN;
1190 graph->is_recursive = 0;
1191 graph->is_chain_call = 1;
1192 graph->is_strict = 1;
1194 /* create new block counter */
1195 graph->block_hash = new_pset(block_cmp, 5);
1197 /* we need dominator info */
1198 if (graph->irg != get_const_code_irg()) {
1199 assure_doms(graph->irg);
1201 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
1202 /* we need extended basic blocks */
1203 compute_extbb(graph->irg);
1205 /* create new extbb counter */
1206 graph->extbb_hash = new_pset(block_cmp, 5);
1210 /* count the nodes in the graph */
1211 irg_walk_graph(graph->irg, update_node_stat, NULL, graph);
1214 /* Uncomment this code if chain-call means call exact one. */
1215 entry = opcode_get_entry(op_Call, graph->opcode_hash);
1217 /* check if we have more than 1 call */
1218 if (cnt_gt(entry->cnt_alive, 1))
1219 graph->is_chain_call = 0;
1222 /* recursive functions are never chain calls, leafs don't have calls */
1223 if (graph->is_recursive || graph->is_leaf)
1224 graph->is_chain_call = 0;
1226 /* assume we walk every graph only ONCE, we could sum here the global count */
1227 foreach_pset(graph->opcode_hash, node_entry_t*, entry) {
1228 node_entry_t *g_entry = opcode_get_entry(entry->op, global->opcode_hash);
1230 /* update the node counter */
1231 cnt_add(&g_entry->cnt_alive, &entry->cnt_alive);
1232 } /* foreach_pset */
1234 /* count the number of address calculation */
1235 if (graph->irg != get_const_code_irg()) {
1236 ir_graph *rem = current_ir_graph;
1238 assure_irg_outs(graph->irg);
1240 /* Must be done an the outs graph */
1241 current_ir_graph = graph->irg;
1242 irg_out_walk(get_irg_start(graph->irg), NULL, mark_address_calc, graph);
1243 current_ir_graph = rem;
1245 #ifdef DUMP_ADR_MODE
1246 /* register the vcg hook and dump the graph for test */
1247 set_dump_node_vcgattr_hook(stat_adr_mark_hook);
1248 dump_ir_block_graph(graph->irg, "-adr");
1249 set_dump_node_vcgattr_hook(NULL);
1250 #endif /* DUMP_ADR_MODE */
1252 irg_walk_graph(graph->irg, NULL, count_adr_ops, graph);
1255 /* count the DAG's */
1256 if (status->stat_options & FIRMSTAT_COUNT_DAG)
1257 count_dags_in_graph(global, graph);
1259 /* calculate the patterns of this graph */
1260 stat_calc_pattern_history(graph->irg);
1262 /* leaf function did not call others */
1264 graph->is_leaf_call = LCS_NON_LEAF_CALL;
1265 else if (graph->is_leaf_call == LCS_UNKNOWN) {
1266 /* we still don't know if this graph calls leaf-functions, so enqueue */
1267 pdeq_putl(status->wait_q, graph);
1270 /* we have analyzed this graph */
1271 graph->is_analyzed = 1;
1273 /* accumulate all counter's */
1274 for (i = 0; i < _gcnt_last; ++i)
1275 cnt_add(&global->cnt[i], &graph->cnt[i]);
1276 } /* update_graph_stat */
1279 * Called for every graph that was on the wait_q in stat_dump_snapshot()
1280 * must finish all statistic info calculations.
1282 * @param global The global entry
1283 * @param graph The current entry
1285 static void update_graph_stat_2(graph_entry_t *global, graph_entry_t *graph)
1288 if (graph->is_deleted) {
1289 /* deleted, ignore */
1294 /* count the nodes in the graph */
1295 irg_walk_graph(graph->irg, update_node_stat_2, NULL, graph);
1297 if (graph->is_leaf_call == LCS_UNKNOWN)
1298 graph->is_leaf_call = LCS_LEAF_CALL;
1300 } /* update_graph_stat_2 */
1303 * Register a dumper.
1305 static void stat_register_dumper(const dumper_t *dumper)
1307 dumper_t *p = XMALLOC(dumper_t);
1310 p->next = status->dumper;
1314 /* FIXME: memory leak */
1315 } /* stat_register_dumper */
1318 * Dumps the statistics of an IR graph.
1320 static void stat_dump_graph(graph_entry_t *entry)
1324 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1325 if (dumper->dump_graph)
1326 dumper->dump_graph(dumper, entry);
1328 } /* stat_dump_graph */
1331 * Calls all registered dumper functions.
1333 static void stat_dump_registered(graph_entry_t *entry)
1337 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1338 if (dumper->func_map) {
1339 dump_graph_FUNC func;
1341 foreach_pset(dumper->func_map, dump_graph_FUNC, func)
1342 func(dumper, entry);
1345 } /* stat_dump_registered */
1348 * Dumps a constant table.
1350 static void stat_dump_consts(const constant_info_t *tbl)
1354 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1355 if (dumper->dump_const_tbl)
1356 dumper->dump_const_tbl(dumper, tbl);
1358 } /* stat_dump_consts */
1361 * Dumps the parameter distribution
1363 static void stat_dump_param_tbl(const distrib_tbl_t *tbl, graph_entry_t *global)
1367 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1368 if (dumper->dump_param_tbl)
1369 dumper->dump_param_tbl(dumper, tbl, global);
1371 } /* stat_dump_param_tbl */
1374 * Dumps the optimization counter
1376 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)
1393 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1395 dumper->init(dumper, name);
1397 } /* stat_dump_init */
1400 * Finish the dumper.
1402 static void stat_dump_finish(void)
1406 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1408 dumper->finish(dumper);
1410 } /* stat_dump_finish */
1413 * Register an additional function for all dumper.
1415 void stat_register_dumper_func(dump_graph_FUNC func)
1419 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1420 if (! dumper->func_map)
1421 dumper->func_map = pset_new_ptr(3);
1422 pset_insert_ptr(dumper->func_map, (void*)func);
1424 } /* stat_register_dumper_func */
1426 /* ---------------------------------------------------------------------- */
1429 * Helper: get an ir_op from an opcode.
1431 ir_op *stat_get_op_from_opcode(unsigned code)
1433 return opcode_find_entry(code, status->ir_op_hash);
1434 } /* stat_get_op_from_opcode */
1437 * Hook: A new IR op is registered.
1439 * @param ctx the hook context
1440 * @param op the new IR opcode that was created.
1442 static void stat_new_ir_op(void *ctx, ir_op *op)
1445 if (! status->stat_options)
1450 graph_entry_t *graph = graph_get_entry(NULL, status->irg_hash);
1452 /* execute for side effect :-) */
1453 (void)opcode_get_entry(op, graph->opcode_hash);
1455 pset_insert(status->ir_op_hash, op, op->code);
1458 } /* stat_new_ir_op */
1461 * Hook: An IR op is freed.
1463 * @param ctx the hook context
1464 * @param op the IR opcode that is freed
1466 static void stat_free_ir_op(void *ctx, ir_op *op)
1470 if (! status->stat_options)
1477 } /* stat_free_ir_op */
1480 * Hook: A new node is created.
1482 * @param ctx the hook context
1483 * @param irg the IR graph on which the node is created
1484 * @param node the new IR node that was created
1486 static void stat_new_node(void *ctx, ir_graph *irg, ir_node *node)
1490 if (! status->stat_options)
1493 /* do NOT count during dead node elimination */
1494 if (status->in_dead_node_elim)
1499 node_entry_t *entry;
1500 graph_entry_t *graph;
1501 ir_op *op = stat_get_irn_op(node);
1503 /* increase global value */
1504 graph = graph_get_entry(NULL, status->irg_hash);
1505 entry = opcode_get_entry(op, graph->opcode_hash);
1506 cnt_inc(&entry->new_node);
1508 /* increase local value */
1509 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1510 entry = opcode_get_entry(op, graph->opcode_hash);
1511 cnt_inc(&entry->new_node);
1514 } /* stat_new_node */
1517 * Hook: A node is changed into a Id node
1519 * @param ctx the hook context
1520 * @param node the IR node that will be turned into an ID
1522 static void stat_turn_into_id(void *ctx, ir_node *node)
1525 if (! status->stat_options)
1530 node_entry_t *entry;
1531 graph_entry_t *graph;
1532 ir_op *op = stat_get_irn_op(node);
1534 /* increase global value */
1535 graph = graph_get_entry(NULL, status->irg_hash);
1536 entry = opcode_get_entry(op, graph->opcode_hash);
1537 cnt_inc(&entry->into_Id);
1539 /* increase local value */
1540 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1541 entry = opcode_get_entry(op, graph->opcode_hash);
1542 cnt_inc(&entry->into_Id);
1545 } /* stat_turn_into_id */
1548 * Hook: A node is normalized
1550 * @param ctx the hook context
1551 * @param node the IR node that was normalized
1553 static void stat_normalize(void *ctx, ir_node *node)
1556 if (! status->stat_options)
1561 node_entry_t *entry;
1562 graph_entry_t *graph;
1563 ir_op *op = stat_get_irn_op(node);
1565 /* increase global value */
1566 graph = graph_get_entry(NULL, status->irg_hash);
1567 entry = opcode_get_entry(op, graph->opcode_hash);
1568 cnt_inc(&entry->normalized);
1570 /* increase local value */
1571 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1572 entry = opcode_get_entry(op, graph->opcode_hash);
1573 cnt_inc(&entry->normalized);
1576 } /* stat_normalize */
1579 * Hook: A new graph was created
1581 * @param ctx the hook context
1582 * @param irg the new IR graph that was created
1583 * @param ent the entity of this graph
1585 static void stat_new_graph(void *ctx, ir_graph *irg, ir_entity *ent)
1588 if (! status->stat_options)
1593 /* execute for side effect :-) */
1594 graph_entry_t * graph = graph_get_entry(irg, status->irg_hash);
1597 graph->is_deleted = 0;
1599 graph->is_leaf_call = 0;
1600 graph->is_recursive = 0;
1601 graph->is_chain_call = 0;
1602 graph->is_strict = 1;
1603 graph->is_analyzed = 0;
1606 } /* stat_new_graph */
1609 * Hook: A graph will be deleted
1611 * @param ctx the hook context
1612 * @param irg the IR graph that will be deleted
1614 * Note that we still hold the information for this graph
1615 * in our hash maps, only a flag is set which prevents this
1616 * information from being changed, it's "frozen" from now.
1618 static void stat_free_graph(void *ctx, ir_graph *irg)
1621 if (! status->stat_options)
1626 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1627 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1629 graph->is_deleted = 1;
1631 if (status->stat_options & FIRMSTAT_COUNT_DELETED) {
1632 /* count the nodes of the graph yet, it will be destroyed later */
1633 update_graph_stat(global, graph);
1637 } /* stat_free_graph */
1640 * Hook: A walk over a graph is initiated. Do not count walks from statistic code.
1642 * @param ctx the hook context
1643 * @param irg the IR graph that will be walked
1644 * @param pre the pre walker
1645 * @param post the post walker
1647 static void stat_irg_walk(void *ctx, ir_graph *irg, 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]);
1662 } /* stat_irg_walk */
1665 * Hook: A walk over a graph in block-wise order is initiated. Do not count walks from statistic code.
1667 * @param ctx the hook context
1668 * @param irg the IR graph that will be walked
1669 * @param pre the pre walker
1670 * @param post the post walker
1672 static void stat_irg_walk_blkwise(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1674 /* for now, do NOT differentiate between blockwise and normal */
1675 stat_irg_walk(ctx, irg, pre, post);
1676 } /* stat_irg_walk_blkwise */
1679 * Hook: A walk over the graph's blocks is initiated. Do not count walks from statistic code.
1681 * @param ctx the hook context
1682 * @param irg the IR graph that will be walked
1683 * @param node the IR node
1684 * @param pre the pre walker
1685 * @param post the post walker
1687 static void stat_irg_block_walk(void *ctx, ir_graph *irg, ir_node *node, generic_func *pre, generic_func *post)
1693 if (! status->stat_options)
1698 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1700 cnt_inc(&graph->cnt[gcnt_acc_walked_blocks]);
1703 } /* stat_irg_block_walk */
1706 * Called for every node that is removed due to an optimization.
1708 * @param n the IR node that will be removed
1709 * @param hmap the hash map containing ir_op* -> opt_entry_t*
1710 * @param kind the optimization kind
1712 static void removed_due_opt(ir_node *n, hmap_opt_entry_t *hmap, hook_opt_kind kind)
1715 ir_op *op = stat_get_irn_op(n);
1717 /* ignore CSE for Constants */
1718 if (kind == HOOK_OPT_CSE && (is_Const(n) || is_SymConst(n)))
1721 /* increase global value */
1722 entry = opt_get_entry(op, hmap);
1723 cnt_inc(&entry->count);
1724 } /* removed_due_opt */
1727 * Hook: Some nodes were optimized into some others due to an optimization.
1729 * @param ctx the hook context
1731 static void stat_merge_nodes(
1733 ir_node **new_node_array, int new_num_entries,
1734 ir_node **old_node_array, int old_num_entries,
1738 if (! status->stat_options)
1744 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1746 cnt_inc(&status->num_opts[opt]);
1747 if (status->reassoc_run)
1748 opt = HOOK_OPT_REASSOC;
1750 for (i = 0; i < old_num_entries; ++i) {
1751 /* nodes might be in new and old, so if we found a node
1752 in both sets, this one is NOT removed */
1753 for (j = 0; j < new_num_entries; ++j) {
1754 if (old_node_array[i] == new_node_array[j])
1757 if (j >= new_num_entries) {
1760 /* sometimes we did not detect, that it is replaced by a Const */
1761 if (opt == HOOK_OPT_CONFIRM && new_num_entries == 1) {
1762 ir_op *op = get_irn_op(new_node_array[0]);
1764 if (op == op_Const || op == op_SymConst)
1765 xopt = HOOK_OPT_CONFIRM_C;
1768 removed_due_opt(old_node_array[i], graph->opt_hash[xopt], (hook_opt_kind)xopt);
1773 } /* stat_merge_nodes */
1776 * Hook: Reassociation is started/stopped.
1778 * @param ctx the hook context
1779 * @param flag if non-zero, reassociation is started else stopped
1781 static void stat_reassociate(void *ctx, int flag)
1784 if (! status->stat_options)
1789 status->reassoc_run = flag;
1792 } /* stat_reassociate */
1795 * Hook: A node was lowered into other nodes
1797 * @param ctx the hook context
1798 * @param node the IR node that will be lowered
1800 static void stat_lower(void *ctx, ir_node *node)
1803 if (! status->stat_options)
1808 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1810 removed_due_opt(node, graph->opt_hash[HOOK_LOWERED], HOOK_LOWERED);
1816 * Hook: A graph was inlined.
1818 * @param ctx the hook context
1819 * @param call the IR call that will re changed into the body of
1820 * the called IR graph
1821 * @param called_irg the IR graph representing the called routine
1823 static void stat_inline(void *ctx, ir_node *call, ir_graph *called_irg)
1826 if (! status->stat_options)
1831 ir_graph *irg = get_irn_irg(call);
1832 graph_entry_t *i_graph = graph_get_entry(called_irg, status->irg_hash);
1833 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1835 cnt_inc(&graph->cnt[gcnt_acc_got_inlined]);
1836 cnt_inc(&i_graph->cnt[gcnt_acc_was_inlined]);
1842 * Hook: A graph with tail-recursions was optimized.
1844 * @param ctx the hook context
1846 static void stat_tail_rec(void *ctx, ir_graph *irg, int n_calls)
1849 if (! status->stat_options)
1854 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1856 graph->num_tail_recursion += n_calls;
1859 } /* stat_tail_rec */
1862 * Strength reduction was performed on an iteration variable.
1864 * @param ctx the hook context
1866 static void stat_strength_red(void *ctx, ir_graph *irg, ir_node *strong)
1869 if (! status->stat_options)
1874 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1875 cnt_inc(&graph->cnt[gcnt_acc_strength_red]);
1877 removed_due_opt(strong, graph->opt_hash[HOOK_OPT_STRENGTH_RED], HOOK_OPT_STRENGTH_RED);
1880 } /* stat_strength_red */
1883 * Hook: Start/Stop the dead node elimination.
1885 * @param ctx the hook context
1887 static void stat_dead_node_elim(void *ctx, ir_graph *irg, int start)
1891 if (! status->stat_options)
1894 status->in_dead_node_elim = (start != 0);
1895 } /* stat_dead_node_elim */
1898 * Hook: if-conversion was tried.
1900 static void stat_if_conversion(void *context, ir_graph *irg, ir_node *phi,
1901 int pos, ir_node *mux, if_result_t reason)
1907 if (! status->stat_options)
1912 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1914 cnt_inc(&graph->cnt[gcnt_if_conv + reason]);
1917 } /* stat_if_conversion */
1920 * Hook: real function call was optimized.
1922 static void stat_func_call(void *context, ir_graph *irg, ir_node *call)
1926 if (! status->stat_options)
1931 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1933 cnt_inc(&graph->cnt[gcnt_acc_real_func_call]);
1936 } /* stat_func_call */
1939 * Hook: A multiply was replaced by a series of Shifts/Adds/Subs.
1941 * @param ctx the hook context
1943 static void stat_arch_dep_replace_mul_with_shifts(void *ctx, ir_node *mul)
1946 if (! status->stat_options)
1951 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1952 removed_due_opt(mul, graph->opt_hash[HOOK_OPT_ARCH_DEP], HOOK_OPT_ARCH_DEP);
1955 } /* stat_arch_dep_replace_mul_with_shifts */
1958 * Hook: A division by const was replaced.
1960 * @param ctx the hook context
1961 * @param node the division node that will be optimized
1963 static void stat_arch_dep_replace_division_by_const(void *ctx, ir_node *node)
1966 if (! status->stat_options)
1971 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1972 removed_due_opt(node, graph->opt_hash[HOOK_OPT_ARCH_DEP], HOOK_OPT_ARCH_DEP);
1975 } /* stat_arch_dep_replace_division_by_const */
1978 * Update the register pressure of a block.
1980 * @param irg the irg containing the block
1981 * @param block the block for which the reg pressure should be set
1982 * @param pressure the pressure
1983 * @param class_name the name of the register class
1985 void stat_be_block_regpressure(ir_graph *irg, ir_node *block, int pressure, const char *class_name)
1987 if (! status->stat_options)
1992 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1993 be_block_entry_t *block_ent;
1994 reg_pressure_entry_t *rp_ent;
1996 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1997 rp_ent = OALLOCZ(&status->be_data, reg_pressure_entry_t);
1999 rp_ent->class_name = class_name;
2000 rp_ent->pressure = pressure;
2002 pset_insert(block_ent->reg_pressure, rp_ent, HASH_PTR(class_name));
2005 } /* stat_be_block_regpressure */
2008 * Update the distribution of ready nodes of a block
2010 * @param irg the irg containing the block
2011 * @param block the block for which the reg pressure should be set
2012 * @param num_ready the number of ready nodes
2014 void stat_be_block_sched_ready(ir_graph *irg, ir_node *block, int num_ready)
2016 if (! status->stat_options)
2021 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
2022 be_block_entry_t *block_ent;
2024 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
2026 /* increase the counter of corresponding number of ready nodes */
2027 stat_inc_int_distrib_tbl(block_ent->sched_ready, num_ready);
2030 } /* stat_be_block_sched_ready */
2033 * Update the permutation statistic of a block.
2035 * @param class_name the name of the register class
2036 * @param n_regs number of registers in the register class
2037 * @param perm the perm node
2038 * @param block the block containing the perm
2039 * @param size the size of the perm
2040 * @param real_size number of pairs with different registers
2042 void stat_be_block_stat_perm(const char *class_name, int n_regs, ir_node *perm, ir_node *block,
2043 int size, int real_size)
2045 if (! status->stat_options)
2050 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
2051 be_block_entry_t *block_ent;
2052 perm_class_entry_t *pc_ent;
2053 perm_stat_entry_t *ps_ent;
2055 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
2056 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
2057 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
2059 pc_ent->n_regs = n_regs;
2061 /* update information */
2062 ps_ent->size = size;
2063 ps_ent->real_size = real_size;
2066 } /* stat_be_block_stat_perm */
2069 * Update the permutation statistic of a single perm.
2071 * @param class_name the name of the register class
2072 * @param perm the perm node
2073 * @param block the block containing the perm
2074 * @param is_chain 1 if chain, 0 if cycle
2075 * @param size length of the cycle/chain
2076 * @param n_ops the number of ops representing this cycle/chain after lowering
2078 void stat_be_block_stat_permcycle(const char *class_name, ir_node *perm, ir_node *block,
2079 int is_chain, int size, int n_ops)
2081 if (! status->stat_options)
2086 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
2087 be_block_entry_t *block_ent;
2088 perm_class_entry_t *pc_ent;
2089 perm_stat_entry_t *ps_ent;
2091 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
2092 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
2093 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
2096 ps_ent->n_copies += n_ops;
2097 stat_inc_int_distrib_tbl(ps_ent->chains, size);
2099 ps_ent->n_exchg += n_ops;
2100 stat_inc_int_distrib_tbl(ps_ent->cycles, size);
2104 } /* stat_be_block_stat_permcycle */
2106 /* Dumps a statistics snapshot. */
2107 void stat_dump_snapshot(const char *name, const char *phase)
2113 if (! status->stat_options)
2118 graph_entry_t *entry;
2119 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
2122 * The constant counter is only global, so we clear it here.
2123 * Note that it does NOT contain the constants in DELETED
2124 * graphs due to this.
2126 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2127 stat_const_clear(status);
2129 /* build the name */
2130 p = strrchr(name, '/');
2135 q = strrchr(name, '\\');
2137 /* NULL might be not the smallest pointer */
2138 if (q && (!p || q > p))
2146 if (l > (int) (sizeof(fname) - 1))
2147 l = sizeof(fname) - 1;
2149 memcpy(fname, name, l);
2155 strncat(fname, "firmstat-", sizeof(fname)-1);
2156 strncat(fname, phase, sizeof(fname)-1);
2157 strncat(fname, "-", sizeof(fname)-1);
2158 strncat(fname, p, sizeof(fname)-1);
2160 stat_dump_init(fname);
2162 /* calculate the graph statistics */
2163 for (entry = (graph_entry_t*)pset_first(status->irg_hash);
2164 entry != NULL; entry = (graph_entry_t*)pset_next(status->irg_hash)) {
2165 if (entry->irg == NULL) {
2166 /* special entry for the global count */
2169 if (! entry->is_deleted) {
2170 /* the graph is still alive, count the nodes on it */
2171 update_graph_stat(global, entry);
2175 /* some calculations are dependent, we pushed them on the wait_q */
2176 while (! pdeq_empty(status->wait_q)) {
2177 entry = (graph_entry_t*)pdeq_getr(status->wait_q);
2179 update_graph_stat_2(global, entry);
2182 /* dump per graph */
2183 for (entry = (graph_entry_t*)pset_first(status->irg_hash);
2184 entry != NULL; entry = (graph_entry_t*)pset_next(status->irg_hash)) {
2185 if (entry->irg == NULL) {
2186 /* special entry for the global count */
2190 if (! entry->is_deleted || status->stat_options & FIRMSTAT_COUNT_DELETED) {
2191 stat_dump_graph(entry);
2192 stat_dump_registered(entry);
2195 if (! entry->is_deleted) {
2196 /* clear the counter that are not accumulated */
2197 graph_clear_entry(entry, 0);
2202 stat_dump_graph(global);
2204 /* dump the const info */
2205 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2206 stat_dump_consts(&status->const_info);
2208 /* dump the parameter distribution */
2209 stat_dump_param_tbl(status->dist_param_cnt, global);
2211 /* dump the optimization counter and clear them */
2212 stat_dump_opt_cnt(status->num_opts, ARRAY_SIZE(status->num_opts));
2213 clear_optimization_counter();
2217 stat_finish_pattern_history(fname);
2219 /* clear the global counters here */
2221 node_entry_t *entry;
2223 for (entry = (node_entry_t*)pset_first(global->opcode_hash);
2224 entry != NULL; entry = (node_entry_t*)pset_next(global->opcode_hash)) {
2225 opcode_clear_entry(entry);
2227 /* clear all global counter */
2228 graph_clear_entry(global, /*all=*/1);
2232 } /* stat_dump_snapshot */
2234 typedef struct pass_t {
2235 ir_prog_pass_t pass;
2241 * Wrapper to run stat_dump_snapshot() as a ir_prog wrapper.
2243 static int stat_dump_snapshot_wrapper(ir_prog *irp, void *context)
2245 pass_t *pass = (pass_t*)context;
2248 stat_dump_snapshot(pass->fname, pass->phase);
2250 } /* stat_dump_snapshot_wrapper */
2253 * Ensure that no verifier is run from the wrapper.
2255 static int no_verify(ir_prog *prog, void *ctx)
2263 * Ensure that no dumper is run from the wrapper.
2265 static void no_dump(ir_prog *prog, void *ctx, unsigned idx)
2272 /* create an ir_pog pass */
2273 ir_prog_pass_t *stat_dump_snapshot_pass(
2274 const char *name, const char *fname, const char *phase)
2276 pass_t *pass = XMALLOCZ(pass_t);
2278 def_prog_pass_constructor(
2279 &pass->pass, name ? name : "stat_snapshot", stat_dump_snapshot_wrapper);
2280 pass->fname = fname;
2281 pass->phase = phase;
2283 /* no dump/verify */
2284 pass->pass.dump_irprog = no_dump;
2285 pass->pass.verify_irprog = no_verify;
2288 } /* stat_dump_snapshot_pass */
2290 /** the hook entries for the Firm statistics module */
2291 static hook_entry_t stat_hooks[hook_last];
2293 /* initialize the statistics module. */
2294 void firm_init_stat(unsigned enable_options)
2296 #define X(a) a, sizeof(a)-1
2297 #define HOOK(h, fkt) \
2298 stat_hooks[h].hook._##h = fkt; register_hook(h, &stat_hooks[h])
2301 if (! (enable_options & FIRMSTAT_ENABLED))
2304 status = XMALLOCZ(stat_info_t);
2306 /* enable statistics */
2307 status->stat_options = enable_options & FIRMSTAT_ENABLED ? enable_options : 0;
2309 /* register all hooks */
2310 HOOK(hook_new_ir_op, stat_new_ir_op);
2311 HOOK(hook_free_ir_op, stat_free_ir_op);
2312 HOOK(hook_new_node, stat_new_node);
2313 HOOK(hook_turn_into_id, stat_turn_into_id);
2314 HOOK(hook_normalize, stat_normalize);
2315 HOOK(hook_new_graph, stat_new_graph);
2316 HOOK(hook_free_graph, stat_free_graph);
2317 HOOK(hook_irg_walk, stat_irg_walk);
2318 HOOK(hook_irg_walk_blkwise, stat_irg_walk_blkwise);
2319 HOOK(hook_irg_block_walk, stat_irg_block_walk);
2320 HOOK(hook_merge_nodes, stat_merge_nodes);
2321 HOOK(hook_reassociate, stat_reassociate);
2322 HOOK(hook_lower, stat_lower);
2323 HOOK(hook_inline, stat_inline);
2324 HOOK(hook_tail_rec, stat_tail_rec);
2325 HOOK(hook_strength_red, stat_strength_red);
2326 HOOK(hook_dead_node_elim, stat_dead_node_elim);
2327 HOOK(hook_if_conversion, stat_if_conversion);
2328 HOOK(hook_func_call, stat_func_call);
2329 HOOK(hook_arch_dep_replace_mul_with_shifts, stat_arch_dep_replace_mul_with_shifts);
2330 HOOK(hook_arch_dep_replace_division_by_const, stat_arch_dep_replace_division_by_const);
2332 obstack_init(&status->cnts);
2333 obstack_init(&status->be_data);
2335 /* create the hash-tables */
2336 status->irg_hash = new_pset(graph_cmp, 8);
2337 status->ir_op_hash = new_pset(opcode_cmp_2, 1);
2339 /* create the wait queue */
2340 status->wait_q = new_pdeq();
2342 if (enable_options & FIRMSTAT_COUNT_STRONG_OP) {
2343 /* build the pseudo-ops */
2345 _op_Phi0.code = --num;
2346 _op_Phi0.name = new_id_from_chars(X("Phi0"));
2348 _op_PhiM.code = --num;
2349 _op_PhiM.name = new_id_from_chars(X("PhiM"));
2351 _op_ProjM.code = --num;
2352 _op_ProjM.name = new_id_from_chars(X("ProjM"));
2354 _op_MulC.code = --num;
2355 _op_MulC.name = new_id_from_chars(X("MulC"));
2357 _op_DivC.code = --num;
2358 _op_DivC.name = new_id_from_chars(X("DivC"));
2360 _op_ModC.code = --num;
2361 _op_ModC.name = new_id_from_chars(X("ModC"));
2363 status->op_Phi0 = &_op_Phi0;
2364 status->op_PhiM = &_op_PhiM;
2365 status->op_ProjM = &_op_ProjM;
2366 status->op_MulC = &_op_MulC;
2367 status->op_DivC = &_op_DivC;
2368 status->op_ModC = &_op_ModC;
2370 status->op_Phi0 = NULL;
2371 status->op_PhiM = NULL;
2372 status->op_ProjM = NULL;
2373 status->op_MulC = NULL;
2374 status->op_DivC = NULL;
2375 status->op_ModC = NULL;
2378 /* for Florian: count the Sel depth */
2379 if (enable_options & FIRMSTAT_COUNT_SELS) {
2380 _op_SelSel.code = --num;
2381 _op_SelSel.name = new_id_from_chars(X("Sel(Sel)"));
2383 _op_SelSelSel.code = --num;
2384 _op_SelSelSel.name = new_id_from_chars(X("Sel(Sel(Sel))"));
2386 status->op_SelSel = &_op_SelSel;
2387 status->op_SelSelSel = &_op_SelSelSel;
2389 status->op_SelSel = NULL;
2390 status->op_SelSelSel = NULL;
2393 /* register the dumper */
2394 stat_register_dumper(&simple_dumper);
2396 if (enable_options & FIRMSTAT_CSV_OUTPUT)
2397 stat_register_dumper(&csv_dumper);
2399 /* initialize the pattern hash */
2400 stat_init_pattern_history(enable_options & FIRMSTAT_PATTERN_ENABLED);
2402 /* initialize the Const options */
2403 if (enable_options & FIRMSTAT_COUNT_CONSTS)
2404 stat_init_const_cnt(status);
2406 /* distribution table for parameter counts */
2407 status->dist_param_cnt = stat_new_int_distrib_tbl();
2409 clear_optimization_counter();
2413 } /* firm_init_stat */
2416 * Frees all dumper structures.
2418 static void stat_term_dumper(void)
2420 dumper_t *dumper, *next_dumper;
2422 for (dumper = status->dumper; dumper; /* iteration done in loop body */ ) {
2423 if (dumper->func_map)
2424 del_pset(dumper->func_map);
2426 next_dumper = dumper->next;
2428 dumper = next_dumper;
2430 } /* stat_term_dumper */
2433 /* Terminates the statistics module, frees all memory. */
2434 void stat_term(void)
2436 if (status != (stat_info_t *)&status_disable) {
2437 obstack_free(&status->be_data, NULL);
2438 obstack_free(&status->cnts, NULL);
2443 status = (stat_info_t *)&status_disable;
2447 /* returns 1 if statistics were initialized, 0 otherwise */
2448 int stat_is_active(void)
2450 return status != (stat_info_t *)&status_disable;
2451 } /* stat_is_active */