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
37 #include "firmstat_t.h"
46 * need this to be static:
47 * Special pseudo Opcodes that we need to count some interesting cases
51 * The Phi0, a node that is created during SSA construction
53 static ir_op _op_Phi0;
55 /** The PhiM, just to count memory Phi's. */
56 static ir_op _op_PhiM;
58 /** The Mul by Const node. */
59 static ir_op _op_MulC;
61 /** The Div by Const node. */
62 static ir_op _op_DivC;
64 /** The Div by Const node. */
65 static ir_op _op_ModC;
67 /** The Div by Const node. */
68 static ir_op _op_DivModC;
70 /** The Quot by Const node. */
71 static ir_op _op_QuotC;
73 /** The memory Proj node. */
74 static ir_op _op_ProjM;
77 static ir_op _op_SelSel;
79 /** A Sel of a Sel of a Sel */
80 static ir_op _op_SelSelSel;
82 /* ---------------------------------------------------------------------------------- */
84 /** Marks the begin of a statistic (hook) function. */
85 #define STAT_ENTER ++status->recursive
87 /** Marks the end of a statistic (hook) functions. */
88 #define STAT_LEAVE --status->recursive
90 /** Allows to enter a statistic function only when we are not already in a hook. */
91 #define STAT_ENTER_SINGLE do { if (status->recursive > 0) return; ++status->recursive; } while (0)
96 static const unsigned status_disable = 0;
97 static stat_info_t *status = (stat_info_t *)&status_disable;
100 * Compare two elements of the opcode hash.
102 static int opcode_cmp(const void *elt, const void *key) {
103 const node_entry_t *e1 = elt;
104 const node_entry_t *e2 = key;
106 return e1->op->code - e2->op->code;
110 * Compare two elements of the graph hash.
112 static int graph_cmp(const void *elt, const void *key) {
113 const graph_entry_t *e1 = elt;
114 const graph_entry_t *e2 = key;
116 return e1->irg != e2->irg;
120 * Compare two elements of the optimization hash.
122 static int opt_cmp(const void *elt, const void *key) {
123 const opt_entry_t *e1 = elt;
124 const opt_entry_t *e2 = key;
126 return e1->op->code != e2->op->code;
130 * Compare two elements of the block/extbb hash.
132 static int block_cmp(const void *elt, const void *key) {
133 const block_entry_t *e1 = elt;
134 const block_entry_t *e2 = key;
136 /* it's enough to compare the block number */
137 return e1->block_nr != e2->block_nr;
141 * Compare two elements of the be_block hash.
143 static int be_block_cmp(const void *elt, const void *key) {
144 const be_block_entry_t *e1 = elt;
145 const be_block_entry_t *e2 = key;
147 return e1->block_nr != e2->block_nr;
151 * Compare two elements of reg pressure hash.
153 static int reg_pressure_cmp(const void *elt, const void *key) {
154 const reg_pressure_entry_t *e1 = elt;
155 const reg_pressure_entry_t *e2 = key;
157 return e1->class_name != e2->class_name;
158 } /* reg_pressure_cmp */
161 * Compare two elements of the perm_stat hash.
163 static int perm_stat_cmp(const void *elt, const void *key) {
164 const perm_stat_entry_t *e1 = elt;
165 const perm_stat_entry_t *e2 = 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) {
174 const perm_class_entry_t *e1 = elt;
175 const perm_class_entry_t *e2 = key;
177 return e1->class_name != e2->class_name;
178 } /* perm_class_cmp */
181 * Compare two elements of the ir_op hash.
183 static int opcode_cmp_2(const void *elt, const void *key) {
184 const ir_op *e1 = elt;
185 const ir_op *e2 = key;
187 return e1->code != e2->code;
191 * Compare two elements of the address_mark set.
193 static int address_mark_cmp(const void *elt, const void *key, size_t size) {
194 const address_mark_entry_t *e1 = elt;
195 const address_mark_entry_t *e2 = key;
198 /* compare only the nodes, the rest is used as data container */
199 return e1->node != e2->node;
200 } /* address_mark_cmp */
203 * Clear all counter in a node_entry_t.
205 static void opcode_clear_entry(node_entry_t *elem) {
206 cnt_clr(&elem->cnt_alive);
207 cnt_clr(&elem->new_node);
208 cnt_clr(&elem->into_Id);
209 cnt_clr(&elem->normalized);
210 } /* opcode_clear_entry */
213 * Returns the associates node_entry_t for an ir_op (and allocates
214 * one if not yet available).
216 * @param op the IR operation
217 * @param hmap a hash map containing ir_op* -> node_entry_t*
219 static node_entry_t *opcode_get_entry(const ir_op *op, hmap_node_entry_t *hmap) {
225 elem = pset_find(hmap, &key, op->code);
229 elem = OALLOCZ(&status->cnts, node_entry_t);
232 opcode_clear_entry(elem);
236 return pset_insert(hmap, elem, op->code);
237 } /* opcode_get_entry */
240 * Returns the associates ir_op for an opcode
242 * @param code the IR opcode
243 * @param hmap the hash map containing opcode -> ir_op*
245 static ir_op *opcode_find_entry(ir_opcode code, hmap_ir_op *hmap) {
249 return pset_find(hmap, &key, code);
250 } /* opcode_find_entry */
253 * Clears all counter in a graph_entry_t.
255 * @param elem the graph entry
256 * @param all if non-zero, clears all counters, else leave accumulated ones
258 static void graph_clear_entry(graph_entry_t *elem, int all) {
261 /* clear accumulated / non-accumulated counter */
262 for (i = all ? 0 : _gcnt_non_acc; i < _gcnt_last; ++i) {
263 cnt_clr(&elem->cnt[i]);
266 if (elem->block_hash) {
267 del_pset(elem->block_hash);
268 elem->block_hash = NULL;
271 if (elem->extbb_hash) {
272 del_pset(elem->extbb_hash);
273 elem->extbb_hash = NULL;
276 obstack_free(&elem->recalc_cnts, NULL);
277 obstack_init(&elem->recalc_cnts);
278 } /* graph_clear_entry */
281 * Returns the associated graph_entry_t for an IR graph.
283 * @param irg the IR graph, NULL for the global counter
284 * @param hmap the hash map containing ir_graph* -> graph_entry_t*
286 static graph_entry_t *graph_get_entry(ir_graph *irg, hmap_graph_entry_t *hmap)
294 elem = pset_find(hmap, &key, HASH_PTR(irg));
297 /* create hash map backend block information */
298 if (! elem->be_block_hash)
299 elem->be_block_hash = new_pset(be_block_cmp, 5);
304 /* allocate a new one */
305 elem = OALLOCZ(&status->cnts, graph_entry_t);
306 obstack_init(&elem->recalc_cnts);
309 graph_clear_entry(elem, 1);
311 /* new hash table for opcodes here */
312 elem->opcode_hash = new_pset(opcode_cmp, 5);
313 elem->address_mark = new_set(address_mark_cmp, 5);
316 /* these hash tables are created on demand */
317 elem->block_hash = NULL;
318 elem->extbb_hash = NULL;
320 for (i = 0; i < sizeof(elem->opt_hash)/sizeof(elem->opt_hash[0]); ++i)
321 elem->opt_hash[i] = new_pset(opt_cmp, 4);
323 return pset_insert(hmap, elem, HASH_PTR(irg));
324 } /* graph_get_entry */
327 * Clear all counter in an opt_entry_t.
329 static void opt_clear_entry(opt_entry_t *elem) {
330 cnt_clr(&elem->count);
331 } /* opt_clear_entry */
334 * Returns the associated opt_entry_t for an IR operation.
336 * @param op the IR operation
337 * @param hmap the hash map containing ir_op* -> opt_entry_t*
339 static opt_entry_t *opt_get_entry(const ir_op *op, hmap_opt_entry_t *hmap)
346 elem = pset_find(hmap, &key, op->code);
350 elem = OALLOCZ(&status->cnts, opt_entry_t);
352 /* clear new counter */
353 opt_clear_entry(elem);
357 return pset_insert(hmap, elem, op->code);
358 } /* opt_get_entry */
361 * clears all counter in a block_entry_t
363 static void block_clear_entry(block_entry_t *elem) {
366 for (i = 0; i < _bcnt_last; ++i)
367 cnt_clr(&elem->cnt[i]);
368 } /* block_clear_entry */
371 * Returns the associated block_entry_t for an block.
373 * @param block_nr an IR block number
374 * @param hmap a hash map containing long -> block_entry_t
376 static block_entry_t *block_get_entry(struct obstack *obst, long block_nr, hmap_block_entry_t *hmap)
381 key.block_nr = block_nr;
383 elem = pset_find(hmap, &key, block_nr);
387 elem = OALLOCZ(obst, block_entry_t);
389 /* clear new counter */
390 block_clear_entry(elem);
392 elem->block_nr = block_nr;
394 return pset_insert(hmap, elem, block_nr);
395 } /* block_get_entry */
398 * Clear all sets in be_block_entry_t.
400 static void be_block_clear_entry(be_block_entry_t *elem)
402 if (elem->reg_pressure)
403 del_pset(elem->reg_pressure);
405 if (elem->sched_ready)
406 stat_delete_distrib_tbl(elem->sched_ready);
408 if (elem->perm_class_stat)
409 del_pset(elem->perm_class_stat);
411 elem->reg_pressure = new_pset(reg_pressure_cmp, 5);
412 elem->sched_ready = stat_new_int_distrib_tbl();
413 elem->perm_class_stat = new_pset(perm_class_cmp, 5);
414 } /* be_block_clear_entry */
417 * Returns the associated be_block_entry_t for an block.
419 * @param block_nr an IR block number
420 * @param hmap a hash map containing long -> be_block_entry_t
422 static be_block_entry_t *be_block_get_entry(struct obstack *obst, long block_nr, hmap_be_block_entry_t *hmap)
424 be_block_entry_t key;
425 be_block_entry_t *elem;
427 key.block_nr = block_nr;
429 elem = pset_find(hmap, &key, block_nr);
433 elem = OALLOCZ(obst, be_block_entry_t);
435 /* clear new counter */
436 be_block_clear_entry(elem);
438 elem->block_nr = block_nr;
440 return pset_insert(hmap, elem, block_nr);
441 } /* be_block_get_entry */
444 * clears all sets in perm_class_entry_t
446 static void perm_class_clear_entry(perm_class_entry_t *elem) {
448 del_pset(elem->perm_stat);
450 elem->perm_stat = new_pset(perm_stat_cmp, 5);
451 } /* perm_class_clear_entry */
454 * Returns the associated perm_class entry for a register class.
456 * @param class_name the register class name
457 * @param hmap a hash map containing class_name -> perm_class_entry_t
459 static perm_class_entry_t *perm_class_get_entry(struct obstack *obst, const char *class_name,
460 hmap_perm_class_entry_t *hmap)
462 perm_class_entry_t key;
463 perm_class_entry_t *elem;
465 key.class_name = class_name;
467 elem = pset_find(hmap, &key, HASH_PTR(class_name));
471 elem = OALLOCZ(obst, perm_class_entry_t);
473 /* clear new counter */
474 perm_class_clear_entry(elem);
476 elem->class_name = class_name;
478 return pset_insert(hmap, elem, HASH_PTR(class_name));
479 } /* perm_class_get_entry */
482 * clears all sets in perm_stat_entry_t
484 static void perm_stat_clear_entry(perm_stat_entry_t *elem) {
486 stat_delete_distrib_tbl(elem->chains);
489 stat_delete_distrib_tbl(elem->cycles);
491 elem->chains = stat_new_int_distrib_tbl();
492 elem->cycles = stat_new_int_distrib_tbl();
493 } /* perm_stat_clear_entry */
496 * Returns the associated perm_stat entry for a perm.
498 * @param perm the perm node
499 * @param hmap a hash map containing perm -> perm_stat_entry_t
501 static perm_stat_entry_t *perm_stat_get_entry(struct obstack *obst, ir_node *perm, hmap_perm_stat_entry_t *hmap)
503 perm_stat_entry_t key;
504 perm_stat_entry_t *elem;
508 elem = pset_find(hmap, &key, HASH_PTR(perm));
512 elem = OALLOCZ(obst, perm_stat_entry_t);
514 /* clear new counter */
515 perm_stat_clear_entry(elem);
519 return pset_insert(hmap, elem, HASH_PTR(perm));
520 } /* perm_stat_get_entry */
523 * Clear optimizations counter,
525 static void clear_optimization_counter(void) {
527 for (i = 0; i < FS_OPT_MAX; ++i)
528 cnt_clr(&status->num_opts[i]);
532 * Returns the ir_op for an IR-node,
533 * handles special cases and return pseudo op codes.
535 * @param none an IR node
537 static ir_op *stat_get_irn_op(ir_node *node)
539 ir_op *op = get_irn_op(node);
540 ir_opcode opc = op->code;
544 if (get_irn_arity(node) == 0) {
545 /* special case, a Phi0 node, count on extra counter */
546 op = status->op_Phi0 ? status->op_Phi0 : op;
547 } else if (get_irn_mode(node) == mode_M) {
548 /* special case, a Memory Phi node, count on extra counter */
549 op = status->op_PhiM ? status->op_PhiM : op;
553 if (get_irn_mode(node) == mode_M) {
554 /* special case, a Memory Proj node, count on extra counter */
555 op = status->op_ProjM ? status->op_ProjM : op;
559 if (is_Const(get_Mul_left(node)) || is_Const(get_Mul_right(node))) {
560 /* special case, a Multiply by a const, count on extra counter */
561 op = status->op_MulC ? status->op_MulC : op;
565 if (is_Const(get_Div_right(node))) {
566 /* special case, a division by a const, count on extra counter */
567 op = status->op_DivC ? status->op_DivC : op;
571 if (is_Const(get_Mod_right(node))) {
572 /* special case, a module by a const, count on extra counter */
573 op = status->op_ModC ? status->op_ModC : op;
577 if (is_Const(get_DivMod_right(node))) {
578 /* special case, a division/modulo by a const, count on extra counter */
579 op = status->op_DivModC ? status->op_DivModC : op;
583 if (is_Const(get_Quot_right(node))) {
584 /* special case, a floating point division by a const, count on extra counter */
585 op = status->op_QuotC ? status->op_QuotC : 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) {
739 int i, num_const_args = 0, num_local_adr = 0;
740 int n = get_Call_n_params(call);
742 for (i = 0; i < n; ++i) {
743 ir_node *param = get_Call_param(call, i);
745 if (is_irn_constlike(param))
747 else if (is_Sel(param)) {
748 ir_node *base = param;
751 base = get_Sel_ptr(base);
752 } while (is_Sel(base));
754 if (base == get_irg_frame(current_ir_graph))
760 if (num_const_args > 0)
761 cnt_inc(&graph->cnt[gcnt_call_with_cnst_arg]);
762 if (num_const_args == n)
763 cnt_inc(&graph->cnt[gcnt_call_with_all_cnst_arg]);
764 if (num_local_adr > 0)
765 cnt_inc(&graph->cnt[gcnt_call_with_local_adr]);
767 stat_inc_int_distrib_tbl(status->dist_param_cnt, n);
768 } /* analyse_params_of_Call */
771 * Update info on calls.
773 * @param call The call
774 * @param graph The graph entry containing the call
776 static void stat_update_call(ir_node *call, graph_entry_t *graph)
778 ir_node *block = get_nodes_block(call);
779 ir_node *ptr = get_Call_ptr(call);
780 ir_entity *ent = NULL;
781 ir_graph *callee = NULL;
784 * If the block is bad, the whole subgraph will collapse later
785 * so do not count this call.
786 * This happens in dead code.
791 cnt_inc(&graph->cnt[gcnt_all_calls]);
793 /* found a call, this function is not a leaf */
796 if (is_SymConst(ptr)) {
797 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
798 /* ok, we seems to know the entity */
799 ent = get_SymConst_entity(ptr);
800 callee = get_entity_irg(ent);
802 /* it is recursive, if it calls at least once */
803 if (callee == graph->irg)
804 graph->is_recursive = 1;
806 cnt_inc(&graph->cnt[gcnt_external_calls]);
809 /* indirect call, be could not predict */
810 cnt_inc(&graph->cnt[gcnt_indirect_calls]);
812 /* NOT a leaf call */
813 graph->is_leaf_call = LCS_NON_LEAF_CALL;
816 /* check, if it's a chain-call: Then, the call-block
817 * must dominate the end block. */
819 ir_node *curr = get_irg_end_block(graph->irg);
820 int depth = get_Block_dom_depth(block);
822 for (; curr != block && get_Block_dom_depth(curr) > depth;) {
823 curr = get_Block_idom(curr);
825 if (! curr || is_no_Block(curr))
830 graph->is_chain_call = 0;
833 /* check, if the callee is a leaf */
835 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
837 if (called->is_analyzed) {
838 if (! called->is_leaf)
839 graph->is_leaf_call = LCS_NON_LEAF_CALL;
843 analyse_params_of_Call(graph, call);
844 } /* stat_update_call */
847 * Update info on calls for graphs on the wait queue.
849 static void stat_update_call_2(ir_node *call, graph_entry_t *graph)
851 ir_node *block = get_nodes_block(call);
852 ir_node *ptr = get_Call_ptr(call);
853 ir_entity *ent = NULL;
854 ir_graph *callee = NULL;
857 * If the block is bad, the whole subgraph will collapse later
858 * so do not count this call.
859 * This happens in dead code.
864 if (is_SymConst(ptr)) {
865 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
866 /* ok, we seems to know the entity */
867 ent = get_SymConst_entity(ptr);
868 callee = get_entity_irg(ent);
872 /* check, if the callee is a leaf */
874 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
876 assert(called->is_analyzed);
878 if (! called->is_leaf)
879 graph->is_leaf_call = LCS_NON_LEAF_CALL;
881 graph->is_leaf_call = LCS_NON_LEAF_CALL;
882 } /* stat_update_call_2 */
885 * Find the base address and entity of an Sel node.
887 * @param sel the node
889 * @return the base address.
891 static ir_node *find_base_adr(ir_node *sel) {
892 ir_node *ptr = get_Sel_ptr(sel);
894 while (is_Sel(ptr)) {
896 ptr = get_Sel_ptr(sel);
899 } /* find_base_adr */
902 * Update info on Load/Store address statistics.
904 static void stat_update_address(ir_node *node, graph_entry_t *graph) {
905 ir_opcode opc = get_irn_opcode(node);
911 /* a global address */
912 cnt_inc(&graph->cnt[gcnt_global_adr]);
915 base = find_base_adr(node);
916 irg = current_ir_graph;
917 if (base == get_irg_tls(irg)) {
918 /* a TLS variable, like a global. */
919 cnt_inc(&graph->cnt[gcnt_global_adr]);
920 } else 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 = 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) {
1026 graph_entry_t *graph = env;
1028 /* check for properties that depends on calls like recursion/leaf/indirect call */
1030 stat_update_call_2(node, graph);
1031 } /* update_node_stat_2 */
1034 * Get the current address mark.
1036 static unsigned get_adr_mark(graph_entry_t *graph, ir_node *node) {
1037 address_mark_entry_t *value = set_find(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
1039 return value ? value->mark : 0;
1040 } /* get_adr_mark */
1043 * Set the current address mark.
1045 static void set_adr_mark(graph_entry_t *graph, ir_node *node, unsigned val) {
1046 address_mark_entry_t *value = set_insert(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
1049 } /* set_adr_mark */
1051 #undef DUMP_ADR_MODE
1053 #ifdef DUMP_ADR_MODE
1055 * a vcg attribute hook: Color a node with a different color if
1056 * it's identified as a part of an address expression or at least referenced
1057 * by an address expression.
1059 static int stat_adr_mark_hook(FILE *F, ir_node *node, ir_node *local)
1061 ir_node *n = local ? local : node;
1062 ir_graph *irg = get_irn_irg(n);
1063 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1064 unsigned mark = get_adr_mark(graph, n);
1066 if (mark & MARK_ADDRESS_CALC)
1067 fprintf(F, "color: purple");
1068 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1069 fprintf(F, "color: pink");
1070 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1071 fprintf(F, "color: lightblue");
1075 /* I know the color! */
1077 } /* stat_adr_mark_hook */
1078 #endif /* DUMP_ADR_MODE */
1081 * Return the "operational" mode of a Firm node.
1083 static ir_mode *get_irn_op_mode(ir_node *node) {
1084 switch (get_irn_opcode(node)) {
1086 return get_Load_mode(node);
1088 return get_irn_mode(get_Store_value(node));
1090 return get_irn_mode(get_DivMod_left(node));
1092 return get_irn_mode(get_Div_left(node));
1094 return get_irn_mode(get_Mod_left(node));
1096 /* Cmp is no address calculation, or is it? */
1098 return get_irn_mode(node);
1100 } /* get_irn_op_mode */
1103 * Post-walker that marks every node that is an address calculation.
1105 * Users of a node must be visited first. We ensure this by
1106 * calling it in the post of an outs walk. This should work even in cycles,
1107 * while the normal pre-walk will not.
1109 static void mark_address_calc(ir_node *node, void *env) {
1110 graph_entry_t *graph = env;
1111 ir_mode *mode = get_irn_op_mode(node);
1113 unsigned mark_preds = MARK_REF_NON_ADR;
1115 if (! mode_is_data(mode))
1118 if (mode_is_reference(mode)) {
1119 /* a reference is calculated here, we are sure */
1120 set_adr_mark(graph, node, MARK_ADDRESS_CALC);
1122 mark_preds = MARK_REF_ADR;
1124 unsigned mark = get_adr_mark(graph, node);
1126 if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR) {
1128 * this node has no reference mode, but is only
1129 * referenced by address calculations
1131 mark_preds = MARK_REF_ADR;
1135 /* mark all predecessors */
1136 for (i = 0, n = get_irn_arity(node); i < n; ++i) {
1137 ir_node *pred = get_irn_n(node, i);
1139 mode = get_irn_op_mode(pred);
1140 if (! mode_is_data(mode))
1143 set_adr_mark(graph, pred, get_adr_mark(graph, pred) | mark_preds);
1145 } /* mark_address_calc */
1148 * Post-walker that marks every node that is an address calculation.
1150 * Users of a node must be visited first. We ensure this by
1151 * calling it in the post of an outs walk. This should work even in cycles,
1152 * while the normal pre-walk will not.
1154 static void count_adr_ops(ir_node *node, void *env) {
1155 graph_entry_t *graph = env;
1156 unsigned mark = get_adr_mark(graph, node);
1158 if (mark & MARK_ADDRESS_CALC)
1159 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1160 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1161 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1162 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1163 cnt_inc(&graph->cnt[gcnt_all_adr_ops]);
1164 } /* count_adr_ops */
1167 * Called for every graph when the graph is either deleted or stat_dump_snapshot()
1168 * is called, must recalculate all statistic info.
1170 * @param global The global entry
1171 * @param graph The current entry
1173 static void update_graph_stat(graph_entry_t *global, graph_entry_t *graph)
1175 node_entry_t *entry;
1178 /* clear first the alive counter in the graph */
1179 foreach_pset(graph->opcode_hash, entry) {
1180 cnt_clr(&entry->cnt_alive);
1181 } /* foreach_pset */
1183 /* set pessimistic values */
1185 graph->is_leaf_call = LCS_UNKNOWN;
1186 graph->is_recursive = 0;
1187 graph->is_chain_call = 1;
1188 graph->is_strict = 1;
1190 /* create new block counter */
1191 graph->block_hash = new_pset(block_cmp, 5);
1193 /* we need dominator info */
1194 if (graph->irg != get_const_code_irg()) {
1195 assure_doms(graph->irg);
1197 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
1198 /* we need extended basic blocks */
1199 compute_extbb(graph->irg);
1201 /* create new extbb counter */
1202 graph->extbb_hash = new_pset(block_cmp, 5);
1206 /* count the nodes in the graph */
1207 irg_walk_graph(graph->irg, update_node_stat, NULL, graph);
1210 /* Uncomment this code if chain-call means call exact one. */
1211 entry = opcode_get_entry(op_Call, graph->opcode_hash);
1213 /* check if we have more than 1 call */
1214 if (cnt_gt(entry->cnt_alive, 1))
1215 graph->is_chain_call = 0;
1218 /* recursive functions are never chain calls, leafs don't have calls */
1219 if (graph->is_recursive || graph->is_leaf)
1220 graph->is_chain_call = 0;
1222 /* assume we walk every graph only ONCE, we could sum here the global count */
1223 foreach_pset(graph->opcode_hash, entry) {
1224 node_entry_t *g_entry = opcode_get_entry(entry->op, global->opcode_hash);
1226 /* update the node counter */
1227 cnt_add(&g_entry->cnt_alive, &entry->cnt_alive);
1228 } /* foreach_pset */
1230 /* count the number of address calculation */
1231 if (graph->irg != get_const_code_irg()) {
1232 ir_graph *rem = current_ir_graph;
1234 assure_irg_outs(graph->irg);
1236 /* Must be done an the outs graph */
1237 current_ir_graph = graph->irg;
1238 irg_out_walk(get_irg_start(graph->irg), NULL, mark_address_calc, graph);
1239 current_ir_graph = rem;
1241 #ifdef DUMP_ADR_MODE
1242 /* register the vcg hook and dump the graph for test */
1243 set_dump_node_vcgattr_hook(stat_adr_mark_hook);
1244 dump_ir_block_graph(graph->irg, "-adr");
1245 set_dump_node_vcgattr_hook(NULL);
1246 #endif /* DUMP_ADR_MODE */
1248 irg_walk_graph(graph->irg, NULL, count_adr_ops, graph);
1251 /* count the DAG's */
1252 if (status->stat_options & FIRMSTAT_COUNT_DAG)
1253 count_dags_in_graph(global, graph);
1255 /* calculate the patterns of this graph */
1256 stat_calc_pattern_history(graph->irg);
1258 /* leaf function did not call others */
1260 graph->is_leaf_call = LCS_NON_LEAF_CALL;
1261 else if (graph->is_leaf_call == LCS_UNKNOWN) {
1262 /* we still don't know if this graph calls leaf-functions, so enqueue */
1263 pdeq_putl(status->wait_q, graph);
1266 /* we have analyzed this graph */
1267 graph->is_analyzed = 1;
1269 /* accumulate all counter's */
1270 for (i = 0; i < _gcnt_last; ++i)
1271 cnt_add(&global->cnt[i], &graph->cnt[i]);
1272 } /* update_graph_stat */
1275 * Called for every graph that was on the wait_q in stat_dump_snapshot()
1276 * must finish all statistic info calculations.
1278 * @param global The global entry
1279 * @param graph The current entry
1281 static void update_graph_stat_2(graph_entry_t *global, graph_entry_t *graph)
1284 if (graph->is_deleted) {
1285 /* deleted, ignore */
1290 /* count the nodes in the graph */
1291 irg_walk_graph(graph->irg, update_node_stat_2, NULL, graph);
1293 if (graph->is_leaf_call == LCS_UNKNOWN)
1294 graph->is_leaf_call = LCS_LEAF_CALL;
1296 } /* update_graph_stat_2 */
1299 * Register a dumper.
1301 static void stat_register_dumper(const dumper_t *dumper) {
1302 dumper_t *p = XMALLOC(dumper_t);
1304 memcpy(p, dumper, sizeof(*p));
1306 p->next = status->dumper;
1310 /* FIXME: memory leak */
1311 } /* stat_register_dumper */
1314 * Dumps the statistics of an IR graph.
1316 static void stat_dump_graph(graph_entry_t *entry) {
1319 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1320 if (dumper->dump_graph)
1321 dumper->dump_graph(dumper, entry);
1323 } /* stat_dump_graph */
1326 * Calls all registered dumper functions.
1328 static void stat_dump_registered(graph_entry_t *entry) {
1331 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1332 if (dumper->func_map) {
1333 dump_graph_FUNC func;
1335 foreach_pset(dumper->func_map, func)
1336 func(dumper, entry);
1339 } /* stat_dump_registered */
1342 * Dumps a constant table.
1344 static void stat_dump_consts(const constant_info_t *tbl) {
1347 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1348 if (dumper->dump_const_tbl)
1349 dumper->dump_const_tbl(dumper, tbl);
1351 } /* stat_dump_consts */
1354 * Dumps the parameter distribution
1356 static void stat_dump_param_tbl(const distrib_tbl_t *tbl, graph_entry_t *global) {
1359 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1360 if (dumper->dump_param_tbl)
1361 dumper->dump_param_tbl(dumper, tbl, global);
1363 } /* stat_dump_param_tbl */
1366 * Dumps the optimization counter
1368 static void stat_dump_opt_cnt(const counter_t *tbl, unsigned len) {
1371 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1372 if (dumper->dump_opt_cnt)
1373 dumper->dump_opt_cnt(dumper, tbl, len);
1375 } /* stat_dump_opt_cnt */
1378 * Initialize the dumper.
1380 static void stat_dump_init(const char *name) {
1383 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1385 dumper->init(dumper, name);
1387 } /* stat_dump_init */
1390 * Finish the dumper.
1392 static void stat_dump_finish(void) {
1395 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1397 dumper->finish(dumper);
1399 } /* stat_dump_finish */
1402 * Register an additional function for all dumper.
1404 void stat_register_dumper_func(dump_graph_FUNC func) {
1407 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1408 if (! dumper->func_map)
1409 dumper->func_map = pset_new_ptr(3);
1410 pset_insert_ptr(dumper->func_map, func);
1412 } /* stat_register_dumper_func */
1414 /* ---------------------------------------------------------------------- */
1417 * Helper: get an ir_op from an opcode.
1419 ir_op *stat_get_op_from_opcode(ir_opcode code) {
1420 return opcode_find_entry(code, status->ir_op_hash);
1421 } /* stat_get_op_from_opcode */
1424 * Hook: A new IR op is registered.
1426 * @param ctx the hook context
1427 * @param op the new IR opcode that was created.
1429 static void stat_new_ir_op(void *ctx, ir_op *op) {
1431 if (! status->stat_options)
1436 graph_entry_t *graph = graph_get_entry(NULL, status->irg_hash);
1438 /* execute for side effect :-) */
1439 (void)opcode_get_entry(op, graph->opcode_hash);
1441 pset_insert(status->ir_op_hash, op, op->code);
1444 } /* stat_new_ir_op */
1447 * Hook: An IR op is freed.
1449 * @param ctx the hook context
1450 * @param op the IR opcode that is freed
1452 static void stat_free_ir_op(void *ctx, ir_op *op) {
1455 if (! status->stat_options)
1462 } /* stat_free_ir_op */
1465 * Hook: A new node is created.
1467 * @param ctx the hook context
1468 * @param irg the IR graph on which the node is created
1469 * @param node the new IR node that was created
1471 static void stat_new_node(void *ctx, ir_graph *irg, ir_node *node) {
1474 if (! status->stat_options)
1477 /* do NOT count during dead node elimination */
1478 if (status->in_dead_node_elim)
1483 node_entry_t *entry;
1484 graph_entry_t *graph;
1485 ir_op *op = stat_get_irn_op(node);
1487 /* increase global value */
1488 graph = graph_get_entry(NULL, status->irg_hash);
1489 entry = opcode_get_entry(op, graph->opcode_hash);
1490 cnt_inc(&entry->new_node);
1492 /* increase local value */
1493 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1494 entry = opcode_get_entry(op, graph->opcode_hash);
1495 cnt_inc(&entry->new_node);
1498 } /* stat_new_node */
1501 * Hook: A node is changed into a Id node
1503 * @param ctx the hook context
1504 * @param node the IR node that will be turned into an ID
1506 static void stat_turn_into_id(void *ctx, ir_node *node) {
1508 if (! status->stat_options)
1513 node_entry_t *entry;
1514 graph_entry_t *graph;
1515 ir_op *op = stat_get_irn_op(node);
1517 /* increase global value */
1518 graph = graph_get_entry(NULL, status->irg_hash);
1519 entry = opcode_get_entry(op, graph->opcode_hash);
1520 cnt_inc(&entry->into_Id);
1522 /* increase local value */
1523 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1524 entry = opcode_get_entry(op, graph->opcode_hash);
1525 cnt_inc(&entry->into_Id);
1528 } /* stat_turn_into_id */
1531 * Hook: A node is normalized
1533 * @param ctx the hook context
1534 * @param node the IR node that was normalized
1536 static void stat_normalize(void *ctx, ir_node *node) {
1538 if (! status->stat_options)
1543 node_entry_t *entry;
1544 graph_entry_t *graph;
1545 ir_op *op = stat_get_irn_op(node);
1547 /* increase global value */
1548 graph = graph_get_entry(NULL, status->irg_hash);
1549 entry = opcode_get_entry(op, graph->opcode_hash);
1550 cnt_inc(&entry->normalized);
1552 /* increase local value */
1553 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1554 entry = opcode_get_entry(op, graph->opcode_hash);
1555 cnt_inc(&entry->normalized);
1558 } /* stat_normalize */
1561 * Hook: A new graph was created
1563 * @param ctx the hook context
1564 * @param irg the new IR graph that was created
1565 * @param ent the entity of this graph
1567 static void stat_new_graph(void *ctx, ir_graph *irg, ir_entity *ent) {
1569 if (! status->stat_options)
1574 /* execute for side effect :-) */
1575 graph_entry_t * graph = graph_get_entry(irg, status->irg_hash);
1578 graph->is_deleted = 0;
1580 graph->is_leaf_call = 0;
1581 graph->is_recursive = 0;
1582 graph->is_chain_call = 0;
1583 graph->is_strict = 1;
1584 graph->is_analyzed = 0;
1587 } /* stat_new_graph */
1590 * Hook: A graph will be deleted
1592 * @param ctx the hook context
1593 * @param irg the IR graph that will be deleted
1595 * Note that we still hold the information for this graph
1596 * in our hash maps, only a flag is set which prevents this
1597 * information from being changed, it's "frozen" from now.
1599 static void stat_free_graph(void *ctx, ir_graph *irg) {
1601 if (! status->stat_options)
1606 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1607 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1609 graph->is_deleted = 1;
1611 if (status->stat_options & FIRMSTAT_COUNT_DELETED) {
1612 /* count the nodes of the graph yet, it will be destroyed later */
1613 update_graph_stat(global, graph);
1617 } /* stat_free_graph */
1620 * Hook: A walk over a graph is initiated. Do not count walks from statistic code.
1622 * @param ctx the hook context
1623 * @param irg the IR graph that will be walked
1624 * @param pre the pre walker
1625 * @param post the post walker
1627 static void stat_irg_walk(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1632 if (! status->stat_options)
1637 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1639 cnt_inc(&graph->cnt[gcnt_acc_walked]);
1642 } /* stat_irg_walk */
1645 * Hook: A walk over a graph in block-wise order is initiated. Do not count walks from statistic code.
1647 * @param ctx the hook context
1648 * @param irg the IR graph that will be walked
1649 * @param pre the pre walker
1650 * @param post the post walker
1652 static void stat_irg_walk_blkwise(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1654 /* for now, do NOT differentiate between blockwise and normal */
1655 stat_irg_walk(ctx, irg, pre, post);
1656 } /* stat_irg_walk_blkwise */
1659 * Hook: A walk over the graph's blocks is initiated. Do not count walks from statistic code.
1661 * @param ctx the hook context
1662 * @param irg the IR graph that will be walked
1663 * @param node the IR node
1664 * @param pre the pre walker
1665 * @param post the post walker
1667 static void stat_irg_block_walk(void *ctx, ir_graph *irg, ir_node *node, generic_func *pre, generic_func *post)
1673 if (! status->stat_options)
1678 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1680 cnt_inc(&graph->cnt[gcnt_acc_walked_blocks]);
1683 } /* stat_irg_block_walk */
1686 * Called for every node that is removed due to an optimization.
1688 * @param n the IR node that will be removed
1689 * @param hmap the hash map containing ir_op* -> opt_entry_t*
1690 * @param kind the optimization kind
1692 static void removed_due_opt(ir_node *n, hmap_opt_entry_t *hmap, hook_opt_kind kind) {
1694 ir_op *op = stat_get_irn_op(n);
1696 /* ignore CSE for Constants */
1697 if (kind == HOOK_OPT_CSE && (is_Const(n) || is_SymConst(n)))
1700 /* increase global value */
1701 entry = opt_get_entry(op, hmap);
1702 cnt_inc(&entry->count);
1703 } /* removed_due_opt */
1706 * Hook: Some nodes were optimized into some others due to an optimization.
1708 * @param ctx the hook context
1710 static void stat_merge_nodes(
1712 ir_node **new_node_array, int new_num_entries,
1713 ir_node **old_node_array, int old_num_entries,
1717 if (! status->stat_options)
1723 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1725 cnt_inc(&status->num_opts[opt]);
1726 if (status->reassoc_run)
1727 opt = HOOK_OPT_REASSOC;
1729 for (i = 0; i < old_num_entries; ++i) {
1730 /* nodes might be in new and old, so if we found a node
1731 in both sets, this one is NOT removed */
1732 for (j = 0; j < new_num_entries; ++j) {
1733 if (old_node_array[i] == new_node_array[j])
1736 if (j >= new_num_entries) {
1739 /* sometimes we did not detect, that it is replaced by a Const */
1740 if (opt == HOOK_OPT_CONFIRM && new_num_entries == 1) {
1741 ir_op *op = get_irn_op(new_node_array[0]);
1743 if (op == op_Const || op == op_SymConst)
1744 xopt = HOOK_OPT_CONFIRM_C;
1747 removed_due_opt(old_node_array[i], graph->opt_hash[xopt], xopt);
1752 } /* stat_merge_nodes */
1755 * Hook: Reassociation is started/stopped.
1757 * @param ctx the hook context
1758 * @param flag if non-zero, reassociation is started else stopped
1760 static void stat_reassociate(void *ctx, int flag) {
1762 if (! status->stat_options)
1767 status->reassoc_run = flag;
1770 } /* stat_reassociate */
1773 * Hook: A node was lowered into other nodes
1775 * @param ctx the hook context
1776 * @param node the IR node that will be lowered
1778 static void stat_lower(void *ctx, ir_node *node) {
1780 if (! status->stat_options)
1785 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1787 removed_due_opt(node, graph->opt_hash[HOOK_LOWERED], HOOK_LOWERED);
1793 * Hook: A graph was inlined.
1795 * @param ctx the hook context
1796 * @param call the IR call that will re changed into the body of
1797 * the called IR graph
1798 * @param called_irg the IR graph representing the called routine
1800 static void stat_inline(void *ctx, ir_node *call, ir_graph *called_irg)
1803 if (! status->stat_options)
1808 ir_graph *irg = get_irn_irg(call);
1809 graph_entry_t *i_graph = graph_get_entry(called_irg, status->irg_hash);
1810 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1812 cnt_inc(&graph->cnt[gcnt_acc_got_inlined]);
1813 cnt_inc(&i_graph->cnt[gcnt_acc_was_inlined]);
1819 * Hook: A graph with tail-recursions was optimized.
1821 * @param ctx the hook context
1823 static void stat_tail_rec(void *ctx, ir_graph *irg, int n_calls) {
1825 if (! status->stat_options)
1830 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1832 graph->num_tail_recursion += n_calls;
1835 } /* stat_tail_rec */
1838 * Strength reduction was performed on an iteration variable.
1840 * @param ctx the hook context
1842 static void stat_strength_red(void *ctx, ir_graph *irg, ir_node *strong) {
1844 if (! status->stat_options)
1849 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1850 cnt_inc(&graph->cnt[gcnt_acc_strength_red]);
1852 removed_due_opt(strong, graph->opt_hash[HOOK_OPT_STRENGTH_RED], HOOK_OPT_STRENGTH_RED);
1855 } /* stat_strength_red */
1858 * Hook: Start/Stop the dead node elimination.
1860 * @param ctx the hook context
1862 static void stat_dead_node_elim(void *ctx, ir_graph *irg, int start) {
1865 if (! status->stat_options)
1868 status->in_dead_node_elim = (start != 0);
1869 } /* stat_dead_node_elim */
1872 * Hook: if-conversion was tried.
1874 static void stat_if_conversion(void *context, ir_graph *irg, ir_node *phi,
1875 int pos, ir_node *mux, if_result_t reason)
1881 if (! status->stat_options)
1886 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1888 cnt_inc(&graph->cnt[gcnt_if_conv + reason]);
1891 } /* stat_if_conversion */
1894 * Hook: real function call was optimized.
1896 static void stat_func_call(void *context, ir_graph *irg, ir_node *call)
1900 if (! status->stat_options)
1905 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1907 cnt_inc(&graph->cnt[gcnt_acc_real_func_call]);
1910 } /* stat_func_call */
1913 * Hook: A multiply was replaced by a series of Shifts/Adds/Subs.
1915 * @param ctx the hook context
1917 static void stat_arch_dep_replace_mul_with_shifts(void *ctx, ir_node *mul) {
1919 if (! status->stat_options)
1924 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1925 removed_due_opt(mul, graph->opt_hash[HOOK_OPT_ARCH_DEP], HOOK_OPT_ARCH_DEP);
1928 } /* stat_arch_dep_replace_mul_with_shifts */
1931 * Hook: A division by const was replaced.
1933 * @param ctx the hook context
1934 * @param node the division node that will be optimized
1936 static void stat_arch_dep_replace_division_by_const(void *ctx, ir_node *node) {
1938 if (! status->stat_options)
1943 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1944 removed_due_opt(node, graph->opt_hash[HOOK_OPT_ARCH_DEP], HOOK_OPT_ARCH_DEP);
1947 } /* stat_arch_dep_replace_division_by_const */
1950 * Update the register pressure of a block.
1952 * @param irg the irg containing the block
1953 * @param block the block for which the reg pressure should be set
1954 * @param pressure the pressure
1955 * @param class_name the name of the register class
1957 void stat_be_block_regpressure(ir_graph *irg, ir_node *block, int pressure, const char *class_name)
1959 if (! status->stat_options)
1964 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1965 be_block_entry_t *block_ent;
1966 reg_pressure_entry_t *rp_ent;
1968 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1969 rp_ent = OALLOCZ(&status->be_data, reg_pressure_entry_t);
1971 rp_ent->class_name = class_name;
1972 rp_ent->pressure = pressure;
1974 pset_insert(block_ent->reg_pressure, rp_ent, HASH_PTR(class_name));
1977 } /* stat_be_block_regpressure */
1980 * Update the distribution of ready nodes of a block
1982 * @param irg the irg containing the block
1983 * @param block the block for which the reg pressure should be set
1984 * @param num_ready the number of ready nodes
1986 void stat_be_block_sched_ready(ir_graph *irg, ir_node *block, int num_ready)
1988 if (! status->stat_options)
1993 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1994 be_block_entry_t *block_ent;
1996 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1998 /* increase the counter of corresponding number of ready nodes */
1999 stat_inc_int_distrib_tbl(block_ent->sched_ready, num_ready);
2002 } /* stat_be_block_sched_ready */
2005 * Update the permutation statistic of a block.
2007 * @param class_name the name of the register class
2008 * @param n_regs number of registers in the register class
2009 * @param perm the perm node
2010 * @param block the block containing the perm
2011 * @param size the size of the perm
2012 * @param real_size number of pairs with different registers
2014 void stat_be_block_stat_perm(const char *class_name, int n_regs, ir_node *perm, ir_node *block,
2015 int size, int real_size)
2017 if (! status->stat_options)
2022 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
2023 be_block_entry_t *block_ent;
2024 perm_class_entry_t *pc_ent;
2025 perm_stat_entry_t *ps_ent;
2027 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
2028 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
2029 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
2031 pc_ent->n_regs = n_regs;
2033 /* update information */
2034 ps_ent->size = size;
2035 ps_ent->real_size = real_size;
2038 } /* stat_be_block_stat_perm */
2041 * Update the permutation statistic of a single perm.
2043 * @param class_name the name of the register class
2044 * @param perm the perm node
2045 * @param block the block containing the perm
2046 * @param is_chain 1 if chain, 0 if cycle
2047 * @param size length of the cycle/chain
2048 * @param n_ops the number of ops representing this cycle/chain after lowering
2050 void stat_be_block_stat_permcycle(const char *class_name, ir_node *perm, ir_node *block,
2051 int is_chain, int size, int n_ops)
2053 if (! status->stat_options)
2058 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
2059 be_block_entry_t *block_ent;
2060 perm_class_entry_t *pc_ent;
2061 perm_stat_entry_t *ps_ent;
2063 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
2064 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
2065 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
2068 ps_ent->n_copies += n_ops;
2069 stat_inc_int_distrib_tbl(ps_ent->chains, size);
2071 ps_ent->n_exchg += n_ops;
2072 stat_inc_int_distrib_tbl(ps_ent->cycles, size);
2076 } /* stat_be_block_stat_permcycle */
2078 /* Dumps a statistics snapshot. */
2079 void stat_dump_snapshot(const char *name, const char *phase)
2085 if (! status->stat_options)
2090 graph_entry_t *entry;
2091 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
2094 * The constant counter is only global, so we clear it here.
2095 * Note that it does NOT contain the constants in DELETED
2096 * graphs due to this.
2098 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2099 stat_const_clear(status);
2101 /* build the name */
2102 p = strrchr(name, '/');
2107 q = strrchr(name, '\\');
2109 /* NULL might be not the smallest pointer */
2110 if (q && (!p || q > p))
2118 if (l > (int) (sizeof(fname) - 1))
2119 l = sizeof(fname) - 1;
2121 memcpy(fname, name, l);
2127 strncat(fname, "firmstat-", sizeof(fname)-1);
2128 strncat(fname, phase, sizeof(fname)-1);
2129 strncat(fname, "-", sizeof(fname)-1);
2130 strncat(fname, p, sizeof(fname)-1);
2132 stat_dump_init(fname);
2134 /* calculate the graph statistics */
2135 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
2136 if (entry->irg == NULL) {
2137 /* special entry for the global count */
2140 if (! entry->is_deleted) {
2141 /* the graph is still alive, count the nodes on it */
2142 update_graph_stat(global, entry);
2146 /* some calculations are dependent, we pushed them on the wait_q */
2147 while (! pdeq_empty(status->wait_q)) {
2148 entry = pdeq_getr(status->wait_q);
2150 update_graph_stat_2(global, entry);
2153 /* dump per graph */
2154 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
2155 if (entry->irg == NULL) {
2156 /* special entry for the global count */
2160 if (! entry->is_deleted || status->stat_options & FIRMSTAT_COUNT_DELETED) {
2161 stat_dump_graph(entry);
2162 stat_dump_registered(entry);
2165 if (! entry->is_deleted) {
2166 /* clear the counter that are not accumulated */
2167 graph_clear_entry(entry, 0);
2172 stat_dump_graph(global);
2174 /* dump the const info */
2175 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2176 stat_dump_consts(&status->const_info);
2178 /* dump the parameter distribution */
2179 stat_dump_param_tbl(status->dist_param_cnt, global);
2181 /* dump the optimization counter and clear them */
2182 stat_dump_opt_cnt(status->num_opts, ARR_SIZE(status->num_opts));
2183 clear_optimization_counter();
2187 stat_finish_pattern_history(fname);
2189 /* clear the global counters here */
2191 node_entry_t *entry;
2193 for (entry = pset_first(global->opcode_hash); entry; entry = pset_next(global->opcode_hash)) {
2194 opcode_clear_entry(entry);
2196 /* clear all global counter */
2197 graph_clear_entry(global, /*all=*/1);
2201 } /* stat_dump_snapshot */
2204 ir_prog_pass_t pass;
2210 * Wrapper to run stat_dump_snapshot() as a ir_prog wrapper.
2212 static int stat_dump_snapshot_wrapper(ir_prog *irp, void *context) {
2213 struct pass_t *pass = context;
2216 stat_dump_snapshot(pass->fname, pass->phase);
2218 } /* stat_dump_snapshot_wrapper */
2221 * Ensure that no verifier is run from the wrapper.
2223 static int no_verify(ir_prog *prog, void *ctx)
2231 * Ensure that no dumper is run from the wrapper.
2233 static void no_dump(ir_prog *prog, void *ctx, unsigned idx)
2240 /* create an ir_pog pass */
2241 ir_prog_pass_t *stat_dump_snapshot_pass(
2242 const char *name, const char *fname, const char *phase) {
2243 struct pass_t *pass = XMALLOCZ(struct pass_t);
2245 def_prog_pass_constructor(
2246 &pass->pass, name ? name : "stat_snapshot", stat_dump_snapshot_wrapper);
2247 pass->fname = fname;
2248 pass->phase = phase;
2250 /* no dump/verify */
2251 pass->pass.dump_irprog = no_dump;
2252 pass->pass.verify_irprog = no_verify;
2255 } /* stat_dump_snapshot_pass */
2257 /** the hook entries for the Firm statistics module */
2258 static hook_entry_t stat_hooks[hook_last];
2260 /* initialize the statistics module. */
2261 void firm_init_stat(unsigned enable_options)
2263 #define X(a) a, sizeof(a)-1
2264 #define HOOK(h, fkt) \
2265 stat_hooks[h].hook._##h = fkt; register_hook(h, &stat_hooks[h])
2268 if (! (enable_options & FIRMSTAT_ENABLED))
2271 status = XMALLOCZ(stat_info_t);
2273 /* enable statistics */
2274 status->stat_options = enable_options & FIRMSTAT_ENABLED ? enable_options : 0;
2276 /* register all hooks */
2277 HOOK(hook_new_ir_op, stat_new_ir_op);
2278 HOOK(hook_free_ir_op, stat_free_ir_op);
2279 HOOK(hook_new_node, stat_new_node);
2280 HOOK(hook_turn_into_id, stat_turn_into_id);
2281 HOOK(hook_normalize, stat_normalize);
2282 HOOK(hook_new_graph, stat_new_graph);
2283 HOOK(hook_free_graph, stat_free_graph);
2284 HOOK(hook_irg_walk, stat_irg_walk);
2285 HOOK(hook_irg_walk_blkwise, stat_irg_walk_blkwise);
2286 HOOK(hook_irg_block_walk, stat_irg_block_walk);
2287 HOOK(hook_merge_nodes, stat_merge_nodes);
2288 HOOK(hook_reassociate, stat_reassociate);
2289 HOOK(hook_lower, stat_lower);
2290 HOOK(hook_inline, stat_inline);
2291 HOOK(hook_tail_rec, stat_tail_rec);
2292 HOOK(hook_strength_red, stat_strength_red);
2293 HOOK(hook_dead_node_elim, stat_dead_node_elim);
2294 HOOK(hook_if_conversion, stat_if_conversion);
2295 HOOK(hook_func_call, stat_func_call);
2296 HOOK(hook_arch_dep_replace_mul_with_shifts, stat_arch_dep_replace_mul_with_shifts);
2297 HOOK(hook_arch_dep_replace_division_by_const, stat_arch_dep_replace_division_by_const);
2299 obstack_init(&status->cnts);
2300 obstack_init(&status->be_data);
2302 /* create the hash-tables */
2303 status->irg_hash = new_pset(graph_cmp, 8);
2304 status->ir_op_hash = new_pset(opcode_cmp_2, 1);
2306 /* create the wait queue */
2307 status->wait_q = new_pdeq();
2309 if (enable_options & FIRMSTAT_COUNT_STRONG_OP) {
2310 /* build the pseudo-ops */
2312 _op_Phi0.code = --num;
2313 _op_Phi0.name = new_id_from_chars(X("Phi0"));
2315 _op_PhiM.code = --num;
2316 _op_PhiM.name = new_id_from_chars(X("PhiM"));
2318 _op_ProjM.code = --num;
2319 _op_ProjM.name = new_id_from_chars(X("ProjM"));
2321 _op_MulC.code = --num;
2322 _op_MulC.name = new_id_from_chars(X("MulC"));
2324 _op_DivC.code = --num;
2325 _op_DivC.name = new_id_from_chars(X("DivC"));
2327 _op_ModC.code = --num;
2328 _op_ModC.name = new_id_from_chars(X("ModC"));
2330 _op_DivModC.code = --num;
2331 _op_DivModC.name = new_id_from_chars(X("DivModC"));
2333 _op_QuotC.code = --num;
2334 _op_QuotC.name = new_id_from_chars(X("QuotC"));
2336 status->op_Phi0 = &_op_Phi0;
2337 status->op_PhiM = &_op_PhiM;
2338 status->op_ProjM = &_op_ProjM;
2339 status->op_MulC = &_op_MulC;
2340 status->op_DivC = &_op_DivC;
2341 status->op_ModC = &_op_ModC;
2342 status->op_DivModC = &_op_DivModC;
2343 status->op_QuotC = &_op_QuotC;
2345 status->op_Phi0 = NULL;
2346 status->op_PhiM = NULL;
2347 status->op_ProjM = NULL;
2348 status->op_MulC = NULL;
2349 status->op_DivC = NULL;
2350 status->op_ModC = NULL;
2351 status->op_DivModC = NULL;
2352 status->op_QuotC = NULL;
2355 /* for Florian: count the Sel depth */
2356 if (enable_options & FIRMSTAT_COUNT_SELS) {
2357 _op_SelSel.code = --num;
2358 _op_SelSel.name = new_id_from_chars(X("Sel(Sel)"));
2360 _op_SelSelSel.code = --num;
2361 _op_SelSelSel.name = new_id_from_chars(X("Sel(Sel(Sel))"));
2363 status->op_SelSel = &_op_SelSel;
2364 status->op_SelSelSel = &_op_SelSelSel;
2366 status->op_SelSel = NULL;
2367 status->op_SelSelSel = NULL;
2370 /* register the dumper */
2371 stat_register_dumper(&simple_dumper);
2373 if (enable_options & FIRMSTAT_CSV_OUTPUT)
2374 stat_register_dumper(&csv_dumper);
2376 /* initialize the pattern hash */
2377 stat_init_pattern_history(enable_options & FIRMSTAT_PATTERN_ENABLED);
2379 /* initialize the Const options */
2380 if (enable_options & FIRMSTAT_COUNT_CONSTS)
2381 stat_init_const_cnt(status);
2383 /* distribution table for parameter counts */
2384 status->dist_param_cnt = stat_new_int_distrib_tbl();
2386 clear_optimization_counter();
2390 } /* firm_init_stat */
2393 * Frees all dumper structures.
2395 static void stat_term_dumper(void) {
2396 dumper_t *dumper, *next_dumper;
2398 for (dumper = status->dumper; dumper; /* iteration done in loop body */ ) {
2399 if (dumper->func_map)
2400 del_pset(dumper->func_map);
2402 next_dumper = dumper->next;
2404 dumper = next_dumper;
2406 } /* stat_term_dumper */
2409 /* Terminates the statistics module, frees all memory. */
2410 void stat_term(void) {
2411 if (status != (stat_info_t *)&status_disable) {
2412 obstack_free(&status->be_data, NULL);
2413 obstack_free(&status->cnts, NULL);
2418 status = (stat_info_t *)&status_disable;
2422 /* returns 1 if statistics were initialized, 0 otherwise */
2423 int stat_is_active(void) {
2424 return status != (stat_info_t *)&status_disable;
2425 } /* stat_is_active */
2429 /* initialize the statistics module. */
2430 void firm_init_stat(unsigned enable_options) {}
2432 /* Dumps a statistics snapshot */
2433 void stat_dump_snapshot(const char *name, const char *phase) {}
2435 /* terminates the statistics module, frees all memory */
2436 void stat_term(void);
2438 #endif /* FIRM_STATISTICS */