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
34 #include "firmstat_t.h"
44 * need this to be static:
45 * Special pseudo Opcodes that we need to count some interesting cases
49 * The Phi0, a node that is created during SSA construction
51 static ir_op _op_Phi0;
53 /** The PhiM, just to count memory Phi's. */
54 static ir_op _op_PhiM;
56 /** The Mul by Const node. */
57 static ir_op _op_MulC;
59 /** The Div by Const node. */
60 static ir_op _op_DivC;
62 /** The Div by Const node. */
63 static ir_op _op_ModC;
65 /** The memory Proj node. */
66 static ir_op _op_ProjM;
69 static ir_op _op_SelSel;
71 /** A Sel of a Sel of a Sel */
72 static ir_op _op_SelSelSel;
74 /* ---------------------------------------------------------------------------------- */
76 /** Marks the begin of a statistic (hook) function. */
77 #define STAT_ENTER ++status->recursive
79 /** Marks the end of a statistic (hook) functions. */
80 #define STAT_LEAVE --status->recursive
82 /** Allows to enter a statistic function only when we are not already in a hook. */
83 #define STAT_ENTER_SINGLE do { if (status->recursive > 0) return; ++status->recursive; } while (0)
88 static const unsigned status_disable = 0;
89 static stat_info_t *status = (stat_info_t *)&status_disable;
92 * Compare two elements of the opcode hash.
94 static int opcode_cmp(const void *elt, const void *key)
96 const node_entry_t *e1 = (const node_entry_t*)elt;
97 const node_entry_t *e2 = (const node_entry_t*)key;
99 return e1->op->code - e2->op->code;
103 * Compare two elements of the graph hash.
105 static int graph_cmp(const void *elt, const void *key)
107 const graph_entry_t *e1 = (const graph_entry_t*)elt;
108 const graph_entry_t *e2 = (const graph_entry_t*)key;
110 return e1->irg != e2->irg;
114 * Compare two elements of the optimization hash.
116 static int opt_cmp(const void *elt, const void *key)
118 const opt_entry_t *e1 = (const opt_entry_t*)elt;
119 const opt_entry_t *e2 = (const opt_entry_t*)key;
121 return e1->op->code != e2->op->code;
125 * Compare two elements of the block/extbb hash.
127 static int block_cmp(const void *elt, const void *key)
129 const block_entry_t *e1 = (const block_entry_t*)elt;
130 const block_entry_t *e2 = (const block_entry_t*)key;
132 /* it's enough to compare the block number */
133 return e1->block_nr != e2->block_nr;
137 * Compare two elements of the be_block hash.
139 static int be_block_cmp(const void *elt, const void *key)
141 const be_block_entry_t *e1 = (const be_block_entry_t*)elt;
142 const be_block_entry_t *e2 = (const be_block_entry_t*)key;
144 return e1->block_nr != e2->block_nr;
148 * Compare two elements of reg pressure hash.
150 static int reg_pressure_cmp(const void *elt, const void *key)
152 const reg_pressure_entry_t *e1 = (const reg_pressure_entry_t*)elt;
153 const reg_pressure_entry_t *e2 = (const reg_pressure_entry_t*)key;
155 return e1->class_name != e2->class_name;
156 } /* reg_pressure_cmp */
159 * Compare two elements of the perm_stat hash.
161 static int perm_stat_cmp(const void *elt, const void *key)
163 const perm_stat_entry_t *e1 = (const perm_stat_entry_t*)elt;
164 const perm_stat_entry_t *e2 = (const perm_stat_entry_t*)key;
166 return e1->perm != e2->perm;
167 } /* perm_stat_cmp */
170 * Compare two elements of the perm_class hash.
172 static int perm_class_cmp(const void *elt, const void *key)
174 const perm_class_entry_t *e1 = (const perm_class_entry_t*)elt;
175 const perm_class_entry_t *e2 = (const perm_class_entry_t*)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)
185 const ir_op *e1 = (const ir_op*)elt;
186 const ir_op *e2 = (const ir_op*)key;
188 return e1->code != e2->code;
192 * Compare two elements of the address_mark set.
194 static int address_mark_cmp(const void *elt, const void *key, size_t size)
196 const address_mark_entry_t *e1 = (const address_mark_entry_t*)elt;
197 const address_mark_entry_t *e2 = (const address_mark_entry_t*)key;
200 /* compare only the nodes, the rest is used as data container */
201 return e1->node != e2->node;
202 } /* address_mark_cmp */
205 * Clear all counter in a node_entry_t.
207 static void opcode_clear_entry(node_entry_t *elem)
209 cnt_clr(&elem->cnt_alive);
210 cnt_clr(&elem->new_node);
211 cnt_clr(&elem->into_Id);
212 cnt_clr(&elem->normalized);
213 } /* opcode_clear_entry */
216 * Returns the associates node_entry_t for an ir_op (and allocates
217 * one if not yet available).
219 * @param op the IR operation
220 * @param hmap a hash map containing ir_op* -> node_entry_t*
222 static node_entry_t *opcode_get_entry(const ir_op *op, hmap_node_entry_t *hmap)
229 elem = (node_entry_t*)pset_find(hmap, &key, op->code);
233 elem = OALLOCZ(&status->cnts, node_entry_t);
236 opcode_clear_entry(elem);
240 return (node_entry_t*)pset_insert(hmap, elem, op->code);
241 } /* opcode_get_entry */
244 * Returns the associates ir_op for an opcode
246 * @param code the IR opcode
247 * @param hmap the hash map containing opcode -> ir_op*
249 static ir_op *opcode_find_entry(ir_opcode code, hmap_ir_op *hmap)
254 return (ir_op*)pset_find(hmap, &key, code);
255 } /* opcode_find_entry */
258 * Clears all counter in a graph_entry_t.
260 * @param elem the graph entry
261 * @param all if non-zero, clears all counters, else leave accumulated ones
263 static void graph_clear_entry(graph_entry_t *elem, int all)
267 /* clear accumulated / non-accumulated counter */
268 for (i = all ? 0 : _gcnt_non_acc; i < _gcnt_last; ++i) {
269 cnt_clr(&elem->cnt[i]);
272 if (elem->block_hash) {
273 del_pset(elem->block_hash);
274 elem->block_hash = NULL;
277 if (elem->extbb_hash) {
278 del_pset(elem->extbb_hash);
279 elem->extbb_hash = NULL;
282 obstack_free(&elem->recalc_cnts, NULL);
283 obstack_init(&elem->recalc_cnts);
284 } /* graph_clear_entry */
287 * Returns the associated graph_entry_t for an IR graph.
289 * @param irg the IR graph, NULL for the global counter
290 * @param hmap the hash map containing ir_graph* -> graph_entry_t*
292 static graph_entry_t *graph_get_entry(ir_graph *irg, hmap_graph_entry_t *hmap)
300 elem = (graph_entry_t*)pset_find(hmap, &key, HASH_PTR(irg));
303 /* create hash map backend block information */
304 if (! elem->be_block_hash)
305 elem->be_block_hash = new_pset(be_block_cmp, 5);
310 /* allocate a new one */
311 elem = OALLOCZ(&status->cnts, graph_entry_t);
312 obstack_init(&elem->recalc_cnts);
315 graph_clear_entry(elem, 1);
317 /* new hash table for opcodes here */
318 elem->opcode_hash = new_pset(opcode_cmp, 5);
319 elem->address_mark = new_set(address_mark_cmp, 5);
322 /* these hash tables are created on demand */
323 elem->block_hash = NULL;
324 elem->extbb_hash = NULL;
326 for (i = 0; i < sizeof(elem->opt_hash)/sizeof(elem->opt_hash[0]); ++i)
327 elem->opt_hash[i] = new_pset(opt_cmp, 4);
329 return (graph_entry_t*)pset_insert(hmap, elem, HASH_PTR(irg));
330 } /* graph_get_entry */
333 * Clear all counter in an opt_entry_t.
335 static void opt_clear_entry(opt_entry_t *elem)
337 cnt_clr(&elem->count);
338 } /* opt_clear_entry */
341 * Returns the associated opt_entry_t for an IR operation.
343 * @param op the IR operation
344 * @param hmap the hash map containing ir_op* -> opt_entry_t*
346 static opt_entry_t *opt_get_entry(const ir_op *op, hmap_opt_entry_t *hmap)
353 elem = (opt_entry_t*)pset_find(hmap, &key, op->code);
357 elem = OALLOCZ(&status->cnts, opt_entry_t);
359 /* clear new counter */
360 opt_clear_entry(elem);
364 return (opt_entry_t*)pset_insert(hmap, elem, op->code);
365 } /* opt_get_entry */
368 * clears all counter in a block_entry_t
370 static void block_clear_entry(block_entry_t *elem)
374 for (i = 0; i < _bcnt_last; ++i)
375 cnt_clr(&elem->cnt[i]);
376 } /* block_clear_entry */
379 * Returns the associated block_entry_t for an block.
381 * @param block_nr an IR block number
382 * @param hmap a hash map containing long -> block_entry_t
384 static block_entry_t *block_get_entry(struct obstack *obst, long block_nr, hmap_block_entry_t *hmap)
389 key.block_nr = block_nr;
391 elem = (block_entry_t*)pset_find(hmap, &key, block_nr);
395 elem = OALLOCZ(obst, block_entry_t);
397 /* clear new counter */
398 block_clear_entry(elem);
400 elem->block_nr = block_nr;
402 return (block_entry_t*)pset_insert(hmap, elem, block_nr);
403 } /* block_get_entry */
406 * Clear all sets in be_block_entry_t.
408 static void be_block_clear_entry(be_block_entry_t *elem)
410 if (elem->reg_pressure)
411 del_pset(elem->reg_pressure);
413 if (elem->sched_ready)
414 stat_delete_distrib_tbl(elem->sched_ready);
416 if (elem->perm_class_stat)
417 del_pset(elem->perm_class_stat);
419 elem->reg_pressure = new_pset(reg_pressure_cmp, 5);
420 elem->sched_ready = stat_new_int_distrib_tbl();
421 elem->perm_class_stat = new_pset(perm_class_cmp, 5);
422 } /* be_block_clear_entry */
425 * Returns the associated be_block_entry_t for an block.
427 * @param block_nr an IR block number
428 * @param hmap a hash map containing long -> be_block_entry_t
430 static be_block_entry_t *be_block_get_entry(struct obstack *obst, long block_nr, hmap_be_block_entry_t *hmap)
432 be_block_entry_t key;
433 be_block_entry_t *elem;
435 key.block_nr = block_nr;
437 elem = (be_block_entry_t*)pset_find(hmap, &key, block_nr);
441 elem = OALLOCZ(obst, be_block_entry_t);
443 /* clear new counter */
444 be_block_clear_entry(elem);
446 elem->block_nr = block_nr;
448 return (be_block_entry_t*)pset_insert(hmap, elem, block_nr);
449 } /* be_block_get_entry */
452 * clears all sets in perm_class_entry_t
454 static void perm_class_clear_entry(perm_class_entry_t *elem)
457 del_pset(elem->perm_stat);
459 elem->perm_stat = new_pset(perm_stat_cmp, 5);
460 } /* perm_class_clear_entry */
463 * Returns the associated perm_class entry for a register class.
465 * @param class_name the register class name
466 * @param hmap a hash map containing class_name -> perm_class_entry_t
468 static perm_class_entry_t *perm_class_get_entry(struct obstack *obst, const char *class_name,
469 hmap_perm_class_entry_t *hmap)
471 perm_class_entry_t key;
472 perm_class_entry_t *elem;
474 key.class_name = class_name;
476 elem = (perm_class_entry_t*)pset_find(hmap, &key, HASH_PTR(class_name));
480 elem = OALLOCZ(obst, perm_class_entry_t);
482 /* clear new counter */
483 perm_class_clear_entry(elem);
485 elem->class_name = class_name;
487 return (perm_class_entry_t*)pset_insert(hmap, elem, HASH_PTR(class_name));
488 } /* perm_class_get_entry */
491 * clears all sets in perm_stat_entry_t
493 static void perm_stat_clear_entry(perm_stat_entry_t *elem)
496 stat_delete_distrib_tbl(elem->chains);
499 stat_delete_distrib_tbl(elem->cycles);
501 elem->chains = stat_new_int_distrib_tbl();
502 elem->cycles = stat_new_int_distrib_tbl();
503 } /* perm_stat_clear_entry */
506 * Returns the associated perm_stat entry for a perm.
508 * @param perm the perm node
509 * @param hmap a hash map containing perm -> perm_stat_entry_t
511 static perm_stat_entry_t *perm_stat_get_entry(struct obstack *obst, ir_node *perm, hmap_perm_stat_entry_t *hmap)
513 perm_stat_entry_t key;
514 perm_stat_entry_t *elem;
518 elem = (perm_stat_entry_t*)pset_find(hmap, &key, HASH_PTR(perm));
522 elem = OALLOCZ(obst, perm_stat_entry_t);
524 /* clear new counter */
525 perm_stat_clear_entry(elem);
529 return (perm_stat_entry_t*)pset_insert(hmap, elem, HASH_PTR(perm));
530 } /* perm_stat_get_entry */
533 * Clear optimizations counter,
535 static void clear_optimization_counter(void)
538 for (i = 0; i < FS_OPT_MAX; ++i)
539 cnt_clr(&status->num_opts[i]);
543 * Returns the ir_op for an IR-node,
544 * handles special cases and return pseudo op codes.
546 * @param none an IR node
548 static ir_op *stat_get_irn_op(ir_node *node)
550 ir_op *op = get_irn_op(node);
551 unsigned opc = op->code;
555 if (get_irn_arity(node) == 0) {
556 /* special case, a Phi0 node, count on extra counter */
557 op = status->op_Phi0 ? status->op_Phi0 : op;
558 } else if (get_irn_mode(node) == mode_M) {
559 /* special case, a Memory Phi node, count on extra counter */
560 op = status->op_PhiM ? status->op_PhiM : op;
564 if (get_irn_mode(node) == mode_M) {
565 /* special case, a Memory Proj node, count on extra counter */
566 op = status->op_ProjM ? status->op_ProjM : op;
570 if (is_Const(get_Mul_left(node)) || is_Const(get_Mul_right(node))) {
571 /* special case, a Multiply by a const, count on extra counter */
572 op = status->op_MulC ? status->op_MulC : op;
576 if (is_Const(get_Div_right(node))) {
577 /* special case, a division by a const, count on extra counter */
578 op = status->op_DivC ? status->op_DivC : op;
582 if (is_Const(get_Mod_right(node))) {
583 /* special case, a module by a const, count on extra counter */
584 op = status->op_ModC ? status->op_ModC : op;
588 if (is_Sel(get_Sel_ptr(node))) {
589 /* special case, a Sel of a Sel, count on extra counter */
590 op = status->op_SelSel ? status->op_SelSel : op;
591 if (is_Sel(get_Sel_ptr(get_Sel_ptr(node)))) {
592 /* special case, a Sel of a Sel of a Sel, count on extra counter */
593 op = status->op_SelSelSel ? status->op_SelSelSel : op;
602 } /* stat_get_irn_op */
605 * update the block counter
607 static void undate_block_info(ir_node *node, graph_entry_t *graph)
609 ir_op *op = get_irn_op(node);
611 block_entry_t *b_entry;
614 /* check for block */
615 if (op == op_Block) {
616 arity = get_irn_arity(node);
617 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(node), graph->block_hash);
618 /* mark start end block to allow to filter them out */
619 if (node == get_irg_start_block(graph->irg))
620 b_entry->is_start = 1;
621 else if (node == get_irg_end_block(graph->irg))
624 /* count all incoming edges */
625 for (i = 0; i < arity; ++i) {
626 ir_node *pred = get_irn_n(node, i);
627 ir_node *other_block = get_nodes_block(pred);
628 block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
630 cnt_inc(&b_entry->cnt[bcnt_in_edges]); /* an edge coming from another block */
631 cnt_inc(&b_entry_other->cnt[bcnt_out_edges]);
636 block = get_nodes_block(node);
637 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(block), graph->block_hash);
639 if (op == op_Phi && mode_is_datab(get_irn_mode(node))) {
640 /* count data Phi per block */
641 cnt_inc(&b_entry->cnt[bcnt_phi_data]);
644 /* we have a new node in our block */
645 cnt_inc(&b_entry->cnt[bcnt_nodes]);
647 /* don't count keep-alive edges */
651 arity = get_irn_arity(node);
653 for (i = 0; i < arity; ++i) {
654 ir_node *pred = get_irn_n(node, i);
655 ir_node *other_block;
657 other_block = get_nodes_block(pred);
659 if (other_block == block)
660 cnt_inc(&b_entry->cnt[bcnt_edges]); /* a in block edge */
662 block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
664 cnt_inc(&b_entry->cnt[bcnt_in_edges]); /* an edge coming from another block */
665 cnt_inc(&b_entry_other->cnt[bcnt_out_edges]);
668 } /* undate_block_info */
671 * Update the extended block counter.
673 static void update_extbb_info(ir_node *node, graph_entry_t *graph)
675 ir_op *op = get_irn_op(node);
677 extbb_entry_t *eb_entry;
680 /* check for block */
681 if (op == op_Block) {
682 extbb = get_nodes_extbb(node);
683 arity = get_irn_arity(node);
684 eb_entry = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(extbb), graph->extbb_hash);
686 /* count all incoming edges */
687 for (i = 0; i < arity; ++i) {
688 ir_node *pred = get_irn_n(node, i);
689 ir_extblk *other_extbb = get_nodes_extbb(pred);
691 if (extbb != other_extbb) {
692 extbb_entry_t *eb_entry_other = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(other_extbb), graph->extbb_hash);
694 cnt_inc(&eb_entry->cnt[bcnt_in_edges]); /* an edge coming from another extbb */
695 cnt_inc(&eb_entry_other->cnt[bcnt_out_edges]);
701 extbb = get_nodes_extbb(node);
702 eb_entry = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(extbb), graph->extbb_hash);
704 if (op == op_Phi && mode_is_datab(get_irn_mode(node))) {
705 /* count data Phi per extbb */
706 cnt_inc(&eb_entry->cnt[bcnt_phi_data]);
709 /* we have a new node in our block */
710 cnt_inc(&eb_entry->cnt[bcnt_nodes]);
712 /* don't count keep-alive edges */
716 arity = get_irn_arity(node);
718 for (i = 0; i < arity; ++i) {
719 ir_node *pred = get_irn_n(node, i);
720 ir_extblk *other_extbb = get_nodes_extbb(pred);
722 if (other_extbb == extbb)
723 cnt_inc(&eb_entry->cnt[bcnt_edges]); /* a in extbb edge */
725 extbb_entry_t *eb_entry_other = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(other_extbb), graph->extbb_hash);
727 cnt_inc(&eb_entry->cnt[bcnt_in_edges]); /* an edge coming from another extbb */
728 cnt_inc(&eb_entry_other->cnt[bcnt_out_edges]);
731 } /* update_extbb_info */
734 * Calculates how many arguments of the call are const, updates
735 * param distribution.
737 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_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)
893 ir_node *ptr = get_Sel_ptr(sel);
895 while (is_Sel(ptr)) {
897 ptr = get_Sel_ptr(sel);
900 } /* find_base_adr */
903 * Update info on Load/Store address statistics.
905 static void stat_update_address(ir_node *node, graph_entry_t *graph)
907 unsigned opc = get_irn_opcode(node);
913 /* a global address */
914 cnt_inc(&graph->cnt[gcnt_global_adr]);
917 base = find_base_adr(node);
918 irg = current_ir_graph;
919 if (base == get_irg_frame(irg)) {
920 /* a local Variable. */
921 cnt_inc(&graph->cnt[gcnt_local_adr]);
924 if (is_Proj(base) && skip_Proj(get_Proj_pred(base)) == get_irg_start(irg)) {
925 /* pointer access through parameter, check for THIS */
926 ir_entity *ent = get_irg_entity(irg);
929 ir_type *ent_tp = get_entity_type(ent);
931 if (get_method_calling_convention(ent_tp) & cc_this_call) {
932 if (get_Proj_proj(base) == 0) {
934 cnt_inc(&graph->cnt[gcnt_this_adr]);
939 /* other parameter */
940 cnt_inc(&graph->cnt[gcnt_param_adr]);
943 /* unknown Pointer access */
944 cnt_inc(&graph->cnt[gcnt_other_adr]);
950 } /* stat_update_address */
953 * Walker for reachable nodes count.
955 static void update_node_stat(ir_node *node, void *env)
957 graph_entry_t *graph = (graph_entry_t*)env;
960 ir_op *op = stat_get_irn_op(node);
961 int i, arity = get_irn_arity(node);
963 entry = opcode_get_entry(op, graph->opcode_hash);
965 cnt_inc(&entry->cnt_alive);
966 cnt_add_i(&graph->cnt[gcnt_edges], arity);
968 /* count block edges */
969 undate_block_info(node, graph);
971 /* count extended block edges */
972 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
973 if (graph->irg != get_const_code_irg())
974 update_extbb_info(node, graph);
977 /* handle statistics for special node types */
981 /* check for properties that depends on calls like recursion/leaf/indirect call */
982 stat_update_call(node, graph);
985 /* check address properties */
986 stat_update_address(get_Load_ptr(node), graph);
989 /* check address properties */
990 stat_update_address(get_Store_ptr(node), graph);
993 /* check for non-strict Phi nodes */
994 for (i = arity - 1; i >= 0; --i) {
995 ir_node *pred = get_Phi_pred(node, i);
996 if (is_Unknown(pred)) {
997 /* found an Unknown predecessor, graph is not strict */
998 graph->is_strict = 0;
1006 /* we want to count the constant IN nodes, not the CSE'ed constant's itself */
1007 if (status->stat_options & FIRMSTAT_COUNT_CONSTS) {
1010 for (i = get_irn_arity(node) - 1; i >= 0; --i) {
1011 ir_node *pred = get_irn_n(node, i);
1013 if (is_Const(pred)) {
1014 /* check properties of constants */
1015 stat_update_const(status, pred, graph);
1019 } /* update_node_stat */
1022 * Walker for reachable nodes count for graphs on the wait_q.
1024 static void update_node_stat_2(ir_node *node, void *env)
1026 graph_entry_t *graph = (graph_entry_t*)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)
1038 address_mark_entry_t *value = (address_mark_entry_t*)set_find(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
1040 return value ? value->mark : 0;
1041 } /* get_adr_mark */
1044 * Set the current address mark.
1046 static void set_adr_mark(graph_entry_t *graph, ir_node *node, unsigned val)
1048 address_mark_entry_t *value = (address_mark_entry_t*)set_insert(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
1051 } /* set_adr_mark */
1053 #undef DUMP_ADR_MODE
1055 #ifdef DUMP_ADR_MODE
1057 * a vcg attribute hook: Color a node with a different color if
1058 * it's identified as a part of an address expression or at least referenced
1059 * by an address expression.
1061 static int stat_adr_mark_hook(FILE *F, ir_node *node, ir_node *local)
1063 ir_node *n = local ? local : node;
1064 ir_graph *irg = get_irn_irg(n);
1065 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1066 unsigned mark = get_adr_mark(graph, n);
1068 if (mark & MARK_ADDRESS_CALC)
1069 fprintf(F, "color: purple");
1070 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1071 fprintf(F, "color: pink");
1072 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1073 fprintf(F, "color: lightblue");
1077 /* I know the color! */
1079 } /* stat_adr_mark_hook */
1080 #endif /* DUMP_ADR_MODE */
1083 * Return the "operational" mode of a Firm node.
1085 static ir_mode *get_irn_op_mode(ir_node *node)
1087 switch (get_irn_opcode(node)) {
1089 return get_Load_mode(node);
1091 return get_irn_mode(get_Store_value(node));
1093 return get_irn_mode(get_Div_left(node));
1095 return get_irn_mode(get_Mod_left(node));
1097 /* Cmp is no address calculation, or is it? */
1099 return get_irn_mode(node);
1101 } /* get_irn_op_mode */
1104 * Post-walker that marks every node that is an address calculation.
1106 * Users of a node must be visited first. We ensure this by
1107 * calling it in the post of an outs walk. This should work even in cycles,
1108 * while the normal pre-walk will not.
1110 static void mark_address_calc(ir_node *node, void *env)
1112 graph_entry_t *graph = (graph_entry_t*)env;
1113 ir_mode *mode = get_irn_op_mode(node);
1115 unsigned mark_preds = MARK_REF_NON_ADR;
1117 if (! mode_is_data(mode))
1120 if (mode_is_reference(mode)) {
1121 /* a reference is calculated here, we are sure */
1122 set_adr_mark(graph, node, MARK_ADDRESS_CALC);
1124 mark_preds = MARK_REF_ADR;
1126 unsigned mark = get_adr_mark(graph, node);
1128 if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR) {
1130 * this node has no reference mode, but is only
1131 * referenced by address calculations
1133 mark_preds = MARK_REF_ADR;
1137 /* mark all predecessors */
1138 for (i = 0, n = get_irn_arity(node); i < n; ++i) {
1139 ir_node *pred = get_irn_n(node, i);
1141 mode = get_irn_op_mode(pred);
1142 if (! mode_is_data(mode))
1145 set_adr_mark(graph, pred, get_adr_mark(graph, pred) | mark_preds);
1147 } /* mark_address_calc */
1150 * Post-walker that marks every node that is an address calculation.
1152 * Users of a node must be visited first. We ensure this by
1153 * calling it in the post of an outs walk. This should work even in cycles,
1154 * while the normal pre-walk will not.
1156 static void count_adr_ops(ir_node *node, void *env)
1158 graph_entry_t *graph = (graph_entry_t*)env;
1159 unsigned mark = get_adr_mark(graph, node);
1161 if (mark & MARK_ADDRESS_CALC)
1162 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1163 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1164 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1165 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1166 cnt_inc(&graph->cnt[gcnt_all_adr_ops]);
1167 } /* count_adr_ops */
1170 * Called for every graph when the graph is either deleted or stat_dump_snapshot()
1171 * is called, must recalculate all statistic info.
1173 * @param global The global entry
1174 * @param graph The current entry
1176 static void update_graph_stat(graph_entry_t *global, graph_entry_t *graph)
1178 node_entry_t *entry;
1181 /* clear first the alive counter in the graph */
1182 foreach_pset(graph->opcode_hash, node_entry_t*, entry) {
1183 cnt_clr(&entry->cnt_alive);
1184 } /* foreach_pset */
1186 /* set pessimistic values */
1188 graph->is_leaf_call = LCS_UNKNOWN;
1189 graph->is_recursive = 0;
1190 graph->is_chain_call = 1;
1191 graph->is_strict = 1;
1193 /* create new block counter */
1194 graph->block_hash = new_pset(block_cmp, 5);
1196 /* we need dominator info */
1197 if (graph->irg != get_const_code_irg()) {
1198 assure_doms(graph->irg);
1200 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
1201 /* we need extended basic blocks */
1202 compute_extbb(graph->irg);
1204 /* create new extbb counter */
1205 graph->extbb_hash = new_pset(block_cmp, 5);
1209 /* count the nodes in the graph */
1210 irg_walk_graph(graph->irg, update_node_stat, NULL, graph);
1213 /* Uncomment this code if chain-call means call exact one. */
1214 entry = opcode_get_entry(op_Call, graph->opcode_hash);
1216 /* check if we have more than 1 call */
1217 if (cnt_gt(entry->cnt_alive, 1))
1218 graph->is_chain_call = 0;
1221 /* recursive functions are never chain calls, leafs don't have calls */
1222 if (graph->is_recursive || graph->is_leaf)
1223 graph->is_chain_call = 0;
1225 /* assume we walk every graph only ONCE, we could sum here the global count */
1226 foreach_pset(graph->opcode_hash, node_entry_t*, entry) {
1227 node_entry_t *g_entry = opcode_get_entry(entry->op, global->opcode_hash);
1229 /* update the node counter */
1230 cnt_add(&g_entry->cnt_alive, &entry->cnt_alive);
1231 } /* foreach_pset */
1233 /* count the number of address calculation */
1234 if (graph->irg != get_const_code_irg()) {
1235 ir_graph *rem = current_ir_graph;
1237 assure_irg_outs(graph->irg);
1239 /* Must be done an the outs graph */
1240 current_ir_graph = graph->irg;
1241 irg_out_walk(get_irg_start(graph->irg), NULL, mark_address_calc, graph);
1242 current_ir_graph = rem;
1244 #ifdef DUMP_ADR_MODE
1245 /* register the vcg hook and dump the graph for test */
1246 set_dump_node_vcgattr_hook(stat_adr_mark_hook);
1247 dump_ir_block_graph(graph->irg, "-adr");
1248 set_dump_node_vcgattr_hook(NULL);
1249 #endif /* DUMP_ADR_MODE */
1251 irg_walk_graph(graph->irg, NULL, count_adr_ops, graph);
1254 /* count the DAG's */
1255 if (status->stat_options & FIRMSTAT_COUNT_DAG)
1256 count_dags_in_graph(global, graph);
1258 /* calculate the patterns of this graph */
1259 stat_calc_pattern_history(graph->irg);
1261 /* leaf function did not call others */
1263 graph->is_leaf_call = LCS_NON_LEAF_CALL;
1264 else if (graph->is_leaf_call == LCS_UNKNOWN) {
1265 /* we still don't know if this graph calls leaf-functions, so enqueue */
1266 pdeq_putl(status->wait_q, graph);
1269 /* we have analyzed this graph */
1270 graph->is_analyzed = 1;
1272 /* accumulate all counter's */
1273 for (i = 0; i < _gcnt_last; ++i)
1274 cnt_add(&global->cnt[i], &graph->cnt[i]);
1275 } /* update_graph_stat */
1278 * Called for every graph that was on the wait_q in stat_dump_snapshot()
1279 * must finish all statistic info calculations.
1281 * @param global The global entry
1282 * @param graph The current entry
1284 static void update_graph_stat_2(graph_entry_t *global, graph_entry_t *graph)
1287 if (graph->is_deleted) {
1288 /* deleted, ignore */
1293 /* count the nodes in the graph */
1294 irg_walk_graph(graph->irg, update_node_stat_2, NULL, graph);
1296 if (graph->is_leaf_call == LCS_UNKNOWN)
1297 graph->is_leaf_call = LCS_LEAF_CALL;
1299 } /* update_graph_stat_2 */
1302 * Register a dumper.
1304 static void stat_register_dumper(const dumper_t *dumper)
1306 dumper_t *p = XMALLOC(dumper_t);
1309 p->next = status->dumper;
1313 /* FIXME: memory leak */
1314 } /* stat_register_dumper */
1317 * Dumps the statistics of an IR graph.
1319 static void stat_dump_graph(graph_entry_t *entry)
1323 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1324 if (dumper->dump_graph)
1325 dumper->dump_graph(dumper, entry);
1327 } /* stat_dump_graph */
1330 * Calls all registered dumper functions.
1332 static void stat_dump_registered(graph_entry_t *entry)
1336 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1337 if (dumper->func_map) {
1338 dump_graph_FUNC func;
1340 foreach_pset(dumper->func_map, dump_graph_FUNC, func)
1341 func(dumper, entry);
1344 } /* stat_dump_registered */
1347 * Dumps a constant table.
1349 static void stat_dump_consts(const constant_info_t *tbl)
1353 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1354 if (dumper->dump_const_tbl)
1355 dumper->dump_const_tbl(dumper, tbl);
1357 } /* stat_dump_consts */
1360 * Dumps the parameter distribution
1362 static void stat_dump_param_tbl(const distrib_tbl_t *tbl, graph_entry_t *global)
1366 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1367 if (dumper->dump_param_tbl)
1368 dumper->dump_param_tbl(dumper, tbl, global);
1370 } /* stat_dump_param_tbl */
1373 * Dumps the optimization counter
1375 static void stat_dump_opt_cnt(const counter_t *tbl, unsigned len)
1379 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1380 if (dumper->dump_opt_cnt)
1381 dumper->dump_opt_cnt(dumper, tbl, len);
1383 } /* stat_dump_opt_cnt */
1386 * Initialize the dumper.
1388 static void stat_dump_init(const char *name)
1392 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1394 dumper->init(dumper, name);
1396 } /* stat_dump_init */
1399 * Finish the dumper.
1401 static void stat_dump_finish(void)
1405 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1407 dumper->finish(dumper);
1409 } /* stat_dump_finish */
1412 * Register an additional function for all dumper.
1414 void stat_register_dumper_func(dump_graph_FUNC func)
1418 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1419 if (! dumper->func_map)
1420 dumper->func_map = pset_new_ptr(3);
1421 pset_insert_ptr(dumper->func_map, (void*)func);
1423 } /* stat_register_dumper_func */
1425 /* ---------------------------------------------------------------------- */
1428 * Helper: get an ir_op from an opcode.
1430 ir_op *stat_get_op_from_opcode(unsigned code)
1432 return opcode_find_entry(code, status->ir_op_hash);
1433 } /* stat_get_op_from_opcode */
1436 * Hook: A new IR op is registered.
1438 * @param ctx the hook context
1439 * @param op the new IR opcode that was created.
1441 static void stat_new_ir_op(void *ctx, ir_op *op)
1444 if (! status->stat_options)
1449 graph_entry_t *graph = graph_get_entry(NULL, status->irg_hash);
1451 /* execute for side effect :-) */
1452 (void)opcode_get_entry(op, graph->opcode_hash);
1454 pset_insert(status->ir_op_hash, op, op->code);
1457 } /* stat_new_ir_op */
1460 * Hook: An IR op is freed.
1462 * @param ctx the hook context
1463 * @param op the IR opcode that is freed
1465 static void stat_free_ir_op(void *ctx, ir_op *op)
1469 if (! status->stat_options)
1476 } /* stat_free_ir_op */
1479 * Hook: A new node is created.
1481 * @param ctx the hook context
1482 * @param irg the IR graph on which the node is created
1483 * @param node the new IR node that was created
1485 static void stat_new_node(void *ctx, ir_graph *irg, ir_node *node)
1489 if (! status->stat_options)
1492 /* do NOT count during dead node elimination */
1493 if (status->in_dead_node_elim)
1498 node_entry_t *entry;
1499 graph_entry_t *graph;
1500 ir_op *op = stat_get_irn_op(node);
1502 /* increase global value */
1503 graph = graph_get_entry(NULL, status->irg_hash);
1504 entry = opcode_get_entry(op, graph->opcode_hash);
1505 cnt_inc(&entry->new_node);
1507 /* increase local value */
1508 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1509 entry = opcode_get_entry(op, graph->opcode_hash);
1510 cnt_inc(&entry->new_node);
1513 } /* stat_new_node */
1516 * Hook: A node is changed into a Id node
1518 * @param ctx the hook context
1519 * @param node the IR node that will be turned into an ID
1521 static void stat_turn_into_id(void *ctx, ir_node *node)
1524 if (! status->stat_options)
1529 node_entry_t *entry;
1530 graph_entry_t *graph;
1531 ir_op *op = stat_get_irn_op(node);
1533 /* increase global value */
1534 graph = graph_get_entry(NULL, status->irg_hash);
1535 entry = opcode_get_entry(op, graph->opcode_hash);
1536 cnt_inc(&entry->into_Id);
1538 /* increase local value */
1539 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1540 entry = opcode_get_entry(op, graph->opcode_hash);
1541 cnt_inc(&entry->into_Id);
1544 } /* stat_turn_into_id */
1547 * Hook: A node is normalized
1549 * @param ctx the hook context
1550 * @param node the IR node that was normalized
1552 static void stat_normalize(void *ctx, ir_node *node)
1555 if (! status->stat_options)
1560 node_entry_t *entry;
1561 graph_entry_t *graph;
1562 ir_op *op = stat_get_irn_op(node);
1564 /* increase global value */
1565 graph = graph_get_entry(NULL, status->irg_hash);
1566 entry = opcode_get_entry(op, graph->opcode_hash);
1567 cnt_inc(&entry->normalized);
1569 /* increase local value */
1570 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1571 entry = opcode_get_entry(op, graph->opcode_hash);
1572 cnt_inc(&entry->normalized);
1575 } /* stat_normalize */
1578 * Hook: A new graph was created
1580 * @param ctx the hook context
1581 * @param irg the new IR graph that was created
1582 * @param ent the entity of this graph
1584 static void stat_new_graph(void *ctx, ir_graph *irg, ir_entity *ent)
1587 if (! status->stat_options)
1592 /* execute for side effect :-) */
1593 graph_entry_t * graph = graph_get_entry(irg, status->irg_hash);
1596 graph->is_deleted = 0;
1598 graph->is_leaf_call = 0;
1599 graph->is_recursive = 0;
1600 graph->is_chain_call = 0;
1601 graph->is_strict = 1;
1602 graph->is_analyzed = 0;
1605 } /* stat_new_graph */
1608 * Hook: A graph will be deleted
1610 * @param ctx the hook context
1611 * @param irg the IR graph that will be deleted
1613 * Note that we still hold the information for this graph
1614 * in our hash maps, only a flag is set which prevents this
1615 * information from being changed, it's "frozen" from now.
1617 static void stat_free_graph(void *ctx, ir_graph *irg)
1620 if (! status->stat_options)
1625 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1626 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1628 graph->is_deleted = 1;
1630 if (status->stat_options & FIRMSTAT_COUNT_DELETED) {
1631 /* count the nodes of the graph yet, it will be destroyed later */
1632 update_graph_stat(global, graph);
1636 } /* stat_free_graph */
1639 * Hook: A walk over a graph is initiated. Do not count walks from statistic code.
1641 * @param ctx the hook context
1642 * @param irg the IR graph that will be walked
1643 * @param pre the pre walker
1644 * @param post the post walker
1646 static void stat_irg_walk(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1651 if (! status->stat_options)
1656 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1658 cnt_inc(&graph->cnt[gcnt_acc_walked]);
1661 } /* stat_irg_walk */
1664 * Hook: A walk over a graph in block-wise order is initiated. Do not count walks from statistic code.
1666 * @param ctx the hook context
1667 * @param irg the IR graph that will be walked
1668 * @param pre the pre walker
1669 * @param post the post walker
1671 static void stat_irg_walk_blkwise(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1673 /* for now, do NOT differentiate between blockwise and normal */
1674 stat_irg_walk(ctx, irg, pre, post);
1675 } /* stat_irg_walk_blkwise */
1678 * Hook: A walk over the graph's blocks is initiated. Do not count walks from statistic code.
1680 * @param ctx the hook context
1681 * @param irg the IR graph that will be walked
1682 * @param node the IR node
1683 * @param pre the pre walker
1684 * @param post the post walker
1686 static void stat_irg_block_walk(void *ctx, ir_graph *irg, ir_node *node, generic_func *pre, generic_func *post)
1692 if (! status->stat_options)
1697 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1699 cnt_inc(&graph->cnt[gcnt_acc_walked_blocks]);
1702 } /* stat_irg_block_walk */
1705 * Called for every node that is removed due to an optimization.
1707 * @param n the IR node that will be removed
1708 * @param hmap the hash map containing ir_op* -> opt_entry_t*
1709 * @param kind the optimization kind
1711 static void removed_due_opt(ir_node *n, hmap_opt_entry_t *hmap, hook_opt_kind kind)
1714 ir_op *op = stat_get_irn_op(n);
1716 /* ignore CSE for Constants */
1717 if (kind == HOOK_OPT_CSE && (is_Const(n) || is_SymConst(n)))
1720 /* increase global value */
1721 entry = opt_get_entry(op, hmap);
1722 cnt_inc(&entry->count);
1723 } /* removed_due_opt */
1726 * Hook: Some nodes were optimized into some others due to an optimization.
1728 * @param ctx the hook context
1730 static void stat_merge_nodes(
1732 ir_node **new_node_array, int new_num_entries,
1733 ir_node **old_node_array, int old_num_entries,
1737 if (! status->stat_options)
1743 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1745 cnt_inc(&status->num_opts[opt]);
1746 if (status->reassoc_run)
1747 opt = HOOK_OPT_REASSOC;
1749 for (i = 0; i < old_num_entries; ++i) {
1750 /* nodes might be in new and old, so if we found a node
1751 in both sets, this one is NOT removed */
1752 for (j = 0; j < new_num_entries; ++j) {
1753 if (old_node_array[i] == new_node_array[j])
1756 if (j >= new_num_entries) {
1759 /* sometimes we did not detect, that it is replaced by a Const */
1760 if (opt == HOOK_OPT_CONFIRM && new_num_entries == 1) {
1761 ir_op *op = get_irn_op(new_node_array[0]);
1763 if (op == op_Const || op == op_SymConst)
1764 xopt = HOOK_OPT_CONFIRM_C;
1767 removed_due_opt(old_node_array[i], graph->opt_hash[xopt], (hook_opt_kind)xopt);
1772 } /* stat_merge_nodes */
1775 * Hook: Reassociation is started/stopped.
1777 * @param ctx the hook context
1778 * @param flag if non-zero, reassociation is started else stopped
1780 static void stat_reassociate(void *ctx, int flag)
1783 if (! status->stat_options)
1788 status->reassoc_run = flag;
1791 } /* stat_reassociate */
1794 * Hook: A node was lowered into other nodes
1796 * @param ctx the hook context
1797 * @param node the IR node that will be lowered
1799 static void stat_lower(void *ctx, ir_node *node)
1802 if (! status->stat_options)
1807 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1809 removed_due_opt(node, graph->opt_hash[HOOK_LOWERED], HOOK_LOWERED);
1815 * Hook: A graph was inlined.
1817 * @param ctx the hook context
1818 * @param call the IR call that will re changed into the body of
1819 * the called IR graph
1820 * @param called_irg the IR graph representing the called routine
1822 static void stat_inline(void *ctx, ir_node *call, ir_graph *called_irg)
1825 if (! status->stat_options)
1830 ir_graph *irg = get_irn_irg(call);
1831 graph_entry_t *i_graph = graph_get_entry(called_irg, status->irg_hash);
1832 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1834 cnt_inc(&graph->cnt[gcnt_acc_got_inlined]);
1835 cnt_inc(&i_graph->cnt[gcnt_acc_was_inlined]);
1841 * Hook: A graph with tail-recursions was optimized.
1843 * @param ctx the hook context
1845 static void stat_tail_rec(void *ctx, ir_graph *irg, int n_calls)
1848 if (! status->stat_options)
1853 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1855 graph->num_tail_recursion += n_calls;
1858 } /* stat_tail_rec */
1861 * Strength reduction was performed on an iteration variable.
1863 * @param ctx the hook context
1865 static void stat_strength_red(void *ctx, ir_graph *irg, ir_node *strong)
1868 if (! status->stat_options)
1873 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1874 cnt_inc(&graph->cnt[gcnt_acc_strength_red]);
1876 removed_due_opt(strong, graph->opt_hash[HOOK_OPT_STRENGTH_RED], HOOK_OPT_STRENGTH_RED);
1879 } /* stat_strength_red */
1882 * Hook: Start/Stop the dead node elimination.
1884 * @param ctx the hook context
1886 static void stat_dead_node_elim(void *ctx, ir_graph *irg, int start)
1890 if (! status->stat_options)
1893 status->in_dead_node_elim = (start != 0);
1894 } /* stat_dead_node_elim */
1897 * Hook: if-conversion was tried.
1899 static void stat_if_conversion(void *context, ir_graph *irg, ir_node *phi,
1900 int pos, ir_node *mux, if_result_t reason)
1906 if (! status->stat_options)
1911 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1913 cnt_inc(&graph->cnt[gcnt_if_conv + reason]);
1916 } /* stat_if_conversion */
1919 * Hook: real function call was optimized.
1921 static void stat_func_call(void *context, ir_graph *irg, ir_node *call)
1925 if (! status->stat_options)
1930 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1932 cnt_inc(&graph->cnt[gcnt_acc_real_func_call]);
1935 } /* stat_func_call */
1938 * Hook: A multiply was replaced by a series of Shifts/Adds/Subs.
1940 * @param ctx the hook context
1942 static void stat_arch_dep_replace_mul_with_shifts(void *ctx, ir_node *mul)
1945 if (! status->stat_options)
1950 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1951 removed_due_opt(mul, graph->opt_hash[HOOK_OPT_ARCH_DEP], HOOK_OPT_ARCH_DEP);
1954 } /* stat_arch_dep_replace_mul_with_shifts */
1957 * Hook: A division by const was replaced.
1959 * @param ctx the hook context
1960 * @param node the division node that will be optimized
1962 static void stat_arch_dep_replace_division_by_const(void *ctx, ir_node *node)
1965 if (! status->stat_options)
1970 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1971 removed_due_opt(node, graph->opt_hash[HOOK_OPT_ARCH_DEP], HOOK_OPT_ARCH_DEP);
1974 } /* stat_arch_dep_replace_division_by_const */
1977 * Update the register pressure of a block.
1979 * @param irg the irg containing the block
1980 * @param block the block for which the reg pressure should be set
1981 * @param pressure the pressure
1982 * @param class_name the name of the register class
1984 void stat_be_block_regpressure(ir_graph *irg, ir_node *block, int pressure, const char *class_name)
1986 if (! status->stat_options)
1991 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1992 be_block_entry_t *block_ent;
1993 reg_pressure_entry_t *rp_ent;
1995 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1996 rp_ent = OALLOCZ(&status->be_data, reg_pressure_entry_t);
1998 rp_ent->class_name = class_name;
1999 rp_ent->pressure = pressure;
2001 pset_insert(block_ent->reg_pressure, rp_ent, HASH_PTR(class_name));
2004 } /* stat_be_block_regpressure */
2007 * Update the distribution of ready nodes of a block
2009 * @param irg the irg containing the block
2010 * @param block the block for which the reg pressure should be set
2011 * @param num_ready the number of ready nodes
2013 void stat_be_block_sched_ready(ir_graph *irg, ir_node *block, int num_ready)
2015 if (! status->stat_options)
2020 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
2021 be_block_entry_t *block_ent;
2023 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
2025 /* increase the counter of corresponding number of ready nodes */
2026 stat_inc_int_distrib_tbl(block_ent->sched_ready, num_ready);
2029 } /* stat_be_block_sched_ready */
2032 * Update the permutation statistic of a block.
2034 * @param class_name the name of the register class
2035 * @param n_regs number of registers in the register class
2036 * @param perm the perm node
2037 * @param block the block containing the perm
2038 * @param size the size of the perm
2039 * @param real_size number of pairs with different registers
2041 void stat_be_block_stat_perm(const char *class_name, int n_regs, ir_node *perm, ir_node *block,
2042 int size, int real_size)
2044 if (! status->stat_options)
2049 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
2050 be_block_entry_t *block_ent;
2051 perm_class_entry_t *pc_ent;
2052 perm_stat_entry_t *ps_ent;
2054 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
2055 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
2056 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
2058 pc_ent->n_regs = n_regs;
2060 /* update information */
2061 ps_ent->size = size;
2062 ps_ent->real_size = real_size;
2065 } /* stat_be_block_stat_perm */
2068 * Update the permutation statistic of a single perm.
2070 * @param class_name the name of the register class
2071 * @param perm the perm node
2072 * @param block the block containing the perm
2073 * @param is_chain 1 if chain, 0 if cycle
2074 * @param size length of the cycle/chain
2075 * @param n_ops the number of ops representing this cycle/chain after lowering
2077 void stat_be_block_stat_permcycle(const char *class_name, ir_node *perm, ir_node *block,
2078 int is_chain, int size, int n_ops)
2080 if (! status->stat_options)
2085 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
2086 be_block_entry_t *block_ent;
2087 perm_class_entry_t *pc_ent;
2088 perm_stat_entry_t *ps_ent;
2090 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
2091 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
2092 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
2095 ps_ent->n_copies += n_ops;
2096 stat_inc_int_distrib_tbl(ps_ent->chains, size);
2098 ps_ent->n_exchg += n_ops;
2099 stat_inc_int_distrib_tbl(ps_ent->cycles, size);
2103 } /* stat_be_block_stat_permcycle */
2105 /* Dumps a statistics snapshot. */
2106 void stat_dump_snapshot(const char *name, const char *phase)
2112 if (! status->stat_options)
2117 graph_entry_t *entry;
2118 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
2121 * The constant counter is only global, so we clear it here.
2122 * Note that it does NOT contain the constants in DELETED
2123 * graphs due to this.
2125 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2126 stat_const_clear(status);
2128 /* build the name */
2129 p = strrchr(name, '/');
2134 q = strrchr(name, '\\');
2136 /* NULL might be not the smallest pointer */
2137 if (q && (!p || q > p))
2145 if (l > (int) (sizeof(fname) - 1))
2146 l = sizeof(fname) - 1;
2148 memcpy(fname, name, l);
2154 strncat(fname, "firmstat-", sizeof(fname)-1);
2155 strncat(fname, phase, sizeof(fname)-1);
2156 strncat(fname, "-", sizeof(fname)-1);
2157 strncat(fname, p, sizeof(fname)-1);
2159 stat_dump_init(fname);
2161 /* calculate the graph statistics */
2162 for (entry = (graph_entry_t*)pset_first(status->irg_hash);
2163 entry != NULL; entry = (graph_entry_t*)pset_next(status->irg_hash)) {
2164 if (entry->irg == NULL) {
2165 /* special entry for the global count */
2168 if (! entry->is_deleted) {
2169 /* the graph is still alive, count the nodes on it */
2170 update_graph_stat(global, entry);
2174 /* some calculations are dependent, we pushed them on the wait_q */
2175 while (! pdeq_empty(status->wait_q)) {
2176 entry = (graph_entry_t*)pdeq_getr(status->wait_q);
2178 update_graph_stat_2(global, entry);
2181 /* dump per graph */
2182 for (entry = (graph_entry_t*)pset_first(status->irg_hash);
2183 entry != NULL; entry = (graph_entry_t*)pset_next(status->irg_hash)) {
2184 if (entry->irg == NULL) {
2185 /* special entry for the global count */
2189 if (! entry->is_deleted || status->stat_options & FIRMSTAT_COUNT_DELETED) {
2190 stat_dump_graph(entry);
2191 stat_dump_registered(entry);
2194 if (! entry->is_deleted) {
2195 /* clear the counter that are not accumulated */
2196 graph_clear_entry(entry, 0);
2201 stat_dump_graph(global);
2203 /* dump the const info */
2204 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2205 stat_dump_consts(&status->const_info);
2207 /* dump the parameter distribution */
2208 stat_dump_param_tbl(status->dist_param_cnt, global);
2210 /* dump the optimization counter and clear them */
2211 stat_dump_opt_cnt(status->num_opts, ARRAY_SIZE(status->num_opts));
2212 clear_optimization_counter();
2216 stat_finish_pattern_history(fname);
2218 /* clear the global counters here */
2220 node_entry_t *entry;
2222 for (entry = (node_entry_t*)pset_first(global->opcode_hash);
2223 entry != NULL; entry = (node_entry_t*)pset_next(global->opcode_hash)) {
2224 opcode_clear_entry(entry);
2226 /* clear all global counter */
2227 graph_clear_entry(global, /*all=*/1);
2231 } /* stat_dump_snapshot */
2233 typedef struct pass_t {
2234 ir_prog_pass_t pass;
2240 * Wrapper to run stat_dump_snapshot() as a ir_prog wrapper.
2242 static int stat_dump_snapshot_wrapper(ir_prog *irp, void *context)
2244 pass_t *pass = (pass_t*)context;
2247 stat_dump_snapshot(pass->fname, pass->phase);
2249 } /* stat_dump_snapshot_wrapper */
2252 * Ensure that no verifier is run from the wrapper.
2254 static int no_verify(ir_prog *prog, void *ctx)
2262 * Ensure that no dumper is run from the wrapper.
2264 static void no_dump(ir_prog *prog, void *ctx, unsigned idx)
2271 /* create an ir_pog pass */
2272 ir_prog_pass_t *stat_dump_snapshot_pass(
2273 const char *name, const char *fname, const char *phase)
2275 pass_t *pass = XMALLOCZ(pass_t);
2277 def_prog_pass_constructor(
2278 &pass->pass, name ? name : "stat_snapshot", stat_dump_snapshot_wrapper);
2279 pass->fname = fname;
2280 pass->phase = phase;
2282 /* no dump/verify */
2283 pass->pass.dump_irprog = no_dump;
2284 pass->pass.verify_irprog = no_verify;
2287 } /* stat_dump_snapshot_pass */
2289 /** the hook entries for the Firm statistics module */
2290 static hook_entry_t stat_hooks[hook_last];
2292 /* initialize the statistics module. */
2293 void firm_init_stat(unsigned enable_options)
2295 #define X(a) a, sizeof(a)-1
2296 #define HOOK(h, fkt) \
2297 stat_hooks[h].hook._##h = fkt; register_hook(h, &stat_hooks[h])
2300 if (! (enable_options & FIRMSTAT_ENABLED))
2303 status = XMALLOCZ(stat_info_t);
2305 /* enable statistics */
2306 status->stat_options = enable_options & FIRMSTAT_ENABLED ? enable_options : 0;
2308 /* register all hooks */
2309 HOOK(hook_new_ir_op, stat_new_ir_op);
2310 HOOK(hook_free_ir_op, stat_free_ir_op);
2311 HOOK(hook_new_node, stat_new_node);
2312 HOOK(hook_turn_into_id, stat_turn_into_id);
2313 HOOK(hook_normalize, stat_normalize);
2314 HOOK(hook_new_graph, stat_new_graph);
2315 HOOK(hook_free_graph, stat_free_graph);
2316 HOOK(hook_irg_walk, stat_irg_walk);
2317 HOOK(hook_irg_walk_blkwise, stat_irg_walk_blkwise);
2318 HOOK(hook_irg_block_walk, stat_irg_block_walk);
2319 HOOK(hook_merge_nodes, stat_merge_nodes);
2320 HOOK(hook_reassociate, stat_reassociate);
2321 HOOK(hook_lower, stat_lower);
2322 HOOK(hook_inline, stat_inline);
2323 HOOK(hook_tail_rec, stat_tail_rec);
2324 HOOK(hook_strength_red, stat_strength_red);
2325 HOOK(hook_dead_node_elim, stat_dead_node_elim);
2326 HOOK(hook_if_conversion, stat_if_conversion);
2327 HOOK(hook_func_call, stat_func_call);
2328 HOOK(hook_arch_dep_replace_mul_with_shifts, stat_arch_dep_replace_mul_with_shifts);
2329 HOOK(hook_arch_dep_replace_division_by_const, stat_arch_dep_replace_division_by_const);
2331 obstack_init(&status->cnts);
2332 obstack_init(&status->be_data);
2334 /* create the hash-tables */
2335 status->irg_hash = new_pset(graph_cmp, 8);
2336 status->ir_op_hash = new_pset(opcode_cmp_2, 1);
2338 /* create the wait queue */
2339 status->wait_q = new_pdeq();
2341 if (enable_options & FIRMSTAT_COUNT_STRONG_OP) {
2342 /* build the pseudo-ops */
2344 _op_Phi0.code = --num;
2345 _op_Phi0.name = new_id_from_chars(X("Phi0"));
2347 _op_PhiM.code = --num;
2348 _op_PhiM.name = new_id_from_chars(X("PhiM"));
2350 _op_ProjM.code = --num;
2351 _op_ProjM.name = new_id_from_chars(X("ProjM"));
2353 _op_MulC.code = --num;
2354 _op_MulC.name = new_id_from_chars(X("MulC"));
2356 _op_DivC.code = --num;
2357 _op_DivC.name = new_id_from_chars(X("DivC"));
2359 _op_ModC.code = --num;
2360 _op_ModC.name = new_id_from_chars(X("ModC"));
2362 status->op_Phi0 = &_op_Phi0;
2363 status->op_PhiM = &_op_PhiM;
2364 status->op_ProjM = &_op_ProjM;
2365 status->op_MulC = &_op_MulC;
2366 status->op_DivC = &_op_DivC;
2367 status->op_ModC = &_op_ModC;
2369 status->op_Phi0 = NULL;
2370 status->op_PhiM = NULL;
2371 status->op_ProjM = NULL;
2372 status->op_MulC = NULL;
2373 status->op_DivC = NULL;
2374 status->op_ModC = NULL;
2377 /* for Florian: count the Sel depth */
2378 if (enable_options & FIRMSTAT_COUNT_SELS) {
2379 _op_SelSel.code = --num;
2380 _op_SelSel.name = new_id_from_chars(X("Sel(Sel)"));
2382 _op_SelSelSel.code = --num;
2383 _op_SelSelSel.name = new_id_from_chars(X("Sel(Sel(Sel))"));
2385 status->op_SelSel = &_op_SelSel;
2386 status->op_SelSelSel = &_op_SelSelSel;
2388 status->op_SelSel = NULL;
2389 status->op_SelSelSel = NULL;
2392 /* register the dumper */
2393 stat_register_dumper(&simple_dumper);
2395 if (enable_options & FIRMSTAT_CSV_OUTPUT)
2396 stat_register_dumper(&csv_dumper);
2398 /* initialize the pattern hash */
2399 stat_init_pattern_history(enable_options & FIRMSTAT_PATTERN_ENABLED);
2401 /* initialize the Const options */
2402 if (enable_options & FIRMSTAT_COUNT_CONSTS)
2403 stat_init_const_cnt(status);
2405 /* distribution table for parameter counts */
2406 status->dist_param_cnt = stat_new_int_distrib_tbl();
2408 clear_optimization_counter();
2412 } /* firm_init_stat */
2415 * Frees all dumper structures.
2417 static void stat_term_dumper(void)
2419 dumper_t *dumper, *next_dumper;
2421 for (dumper = status->dumper; dumper; /* iteration done in loop body */ ) {
2422 if (dumper->func_map)
2423 del_pset(dumper->func_map);
2425 next_dumper = dumper->next;
2427 dumper = next_dumper;
2429 } /* stat_term_dumper */
2432 /* Terminates the statistics module, frees all memory. */
2433 void stat_term(void)
2435 if (status != (stat_info_t *)&status_disable) {
2436 obstack_free(&status->be_data, NULL);
2437 obstack_free(&status->cnts, NULL);
2442 status = (stat_info_t *)&status_disable;
2446 /* returns 1 if statistics were initialized, 0 otherwise */
2447 int stat_is_active(void)
2449 return status != (stat_info_t *)&status_disable;
2450 } /* stat_is_active */