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
30 #ifdef FIRM_STATISTICS
44 #include "firmstat_t.h"
52 * need this to be static:
53 * Special pseudo Opcodes that we need to count some interesting cases
57 * The Phi0, a node that is created during SSA construction
59 static ir_op _op_Phi0;
61 /** The PhiM, just to count memory Phi's. */
62 static ir_op _op_PhiM;
64 /** The Mul by Const node. */
65 static ir_op _op_MulC;
67 /** The Div by Const node. */
68 static ir_op _op_DivC;
70 /** The Div by Const node. */
71 static ir_op _op_ModC;
73 /** The Div by Const node. */
74 static ir_op _op_DivModC;
76 /** The memory Proj node. */
77 static ir_op _op_ProjM;
80 static ir_op _op_SelSel;
82 /** A Sel of a Sel of a Sel */
83 static ir_op _op_SelSelSel;
85 /* ---------------------------------------------------------------------------------- */
87 /** Marks the begin of a statistic (hook) function. */
88 #define STAT_ENTER ++status->recursive
90 /** Marks the end of a statistic (hook) functions. */
91 #define STAT_LEAVE --status->recursive
93 /** Allows to enter a statistic function only when we are not already in a hook. */
94 #define STAT_ENTER_SINGLE do { if (status->recursive > 0) return; ++status->recursive; } while (0)
99 static const int status_disable = 0;
100 static stat_info_t *status = (stat_info_t *)&status_disable;
103 * Compare two elements of the opcode hash.
105 static int opcode_cmp(const void *elt, const void *key) {
106 const node_entry_t *e1 = elt;
107 const node_entry_t *e2 = key;
109 return e1->op->code - e2->op->code;
113 * Compare two elements of the graph hash.
115 static int graph_cmp(const void *elt, const void *key) {
116 const graph_entry_t *e1 = elt;
117 const graph_entry_t *e2 = key;
119 return e1->irg != e2->irg;
123 * Compare two elements of the optimization hash.
125 static int opt_cmp(const void *elt, const void *key) {
126 const opt_entry_t *e1 = elt;
127 const opt_entry_t *e2 = key;
129 return e1->op->code != e2->op->code;
133 * Compare two elements of the block/extbb hash.
135 static int block_cmp(const void *elt, const void *key) {
136 const block_entry_t *e1 = elt;
137 const block_entry_t *e2 = key;
139 return e1->block_nr != e2->block_nr;
143 * Compare two elements of the be_block hash.
145 static int be_block_cmp(const void *elt, const void *key) {
146 const be_block_entry_t *e1 = elt;
147 const be_block_entry_t *e2 = key;
149 return e1->block_nr != e2->block_nr;
153 * Compare two elements of reg pressure hash.
155 static int reg_pressure_cmp(const void *elt, const void *key) {
156 const reg_pressure_entry_t *e1 = elt;
157 const reg_pressure_entry_t *e2 = key;
159 return e1->class_name != e2->class_name;
160 } /* reg_pressure_cmp */
163 * Compare two elements of the perm_stat hash.
165 static int perm_stat_cmp(const void *elt, const void *key) {
166 const perm_stat_entry_t *e1 = elt;
167 const perm_stat_entry_t *e2 = key;
169 return e1->perm != e2->perm;
170 } /* perm_stat_cmp */
173 * Compare two elements of the perm_class hash.
175 static int perm_class_cmp(const void *elt, const void *key) {
176 const perm_class_entry_t *e1 = elt;
177 const perm_class_entry_t *e2 = key;
179 return e1->class_name != e2->class_name;
180 } /* perm_class_cmp */
183 * Compare two elements of the ir_op hash.
185 static int opcode_cmp_2(const void *elt, const void *key) {
186 const ir_op *e1 = elt;
187 const ir_op *e2 = key;
189 return e1->code != e2->code;
193 * Compare two elements of the address_mark set.
195 static int address_mark_cmp(const void *elt, const void *key, size_t size) {
196 const address_mark_entry_t *e1 = elt;
197 const address_mark_entry_t *e2 = 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) {
208 cnt_clr(&elem->cnt_alive);
209 cnt_clr(&elem->new_node);
210 cnt_clr(&elem->into_Id);
211 } /* opcode_clear_entry */
214 * Returns the associates node_entry_t for an ir_op
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 = obstack_alloc(&status->cnts, sizeof(*elem));
230 memset(elem, 0, sizeof(*elem));
233 opcode_clear_entry(elem);
237 return pset_insert(hmap, elem, op->code);
238 } /* opcode_get_entry */
241 * Returns the associates ir_op for an opcode
243 * @param code the IR opcode
244 * @param hmap the hash map containing opcode -> ir_op*
246 static ir_op *opcode_find_entry(ir_opcode code, hmap_ir_op *hmap) {
250 return pset_find(hmap, &key, code);
251 } /* opcode_find_entry */
254 * Clears all counter in a graph_entry_t.
256 * @param elem the graph entry
257 * @param all if non-zero, clears all counters, else leave accumulated ones
259 static void graph_clear_entry(graph_entry_t *elem, int all) {
262 /* clear accumulated / non-acumulated counter */
263 for (i = all ? 0 : _gcnt_non_acc; i < _gcnt_last; ++i) {
264 cnt_clr(&elem->cnt[i]);
267 if (elem->block_hash) {
268 del_pset(elem->block_hash);
269 elem->block_hash = NULL;
272 if (elem->extbb_hash) {
273 del_pset(elem->extbb_hash);
274 elem->extbb_hash = NULL;
277 obstack_free(&elem->recalc_cnts, NULL);
278 obstack_init(&elem->recalc_cnts);
279 } /* graph_clear_entry */
282 * Returns the associated graph_entry_t for an IR graph.
284 * @param irg the IR graph
285 * @param hmap the hash map containing ir_graph* -> graph_entry_t*
287 static graph_entry_t *graph_get_entry(ir_graph *irg, hmap_graph_entry_t *hmap)
295 elem = pset_find(hmap, &key, HASH_PTR(irg));
298 /* create hash map backend block information */
299 if (! elem->be_block_hash)
300 elem->be_block_hash = new_pset(be_block_cmp, 5);
305 /* allocate a new one */
306 elem = obstack_alloc(&status->cnts, sizeof(*elem));
307 memset(elem, 0, sizeof(*elem));
308 obstack_init(&elem->recalc_cnts);
311 graph_clear_entry(elem, 1);
313 /* new hash table for opcodes here */
314 elem->opcode_hash = new_pset(opcode_cmp, 5);
315 elem->address_mark = new_set(address_mark_cmp, 5);
318 /* these hash tables are created on demand */
319 elem->block_hash = NULL;
320 elem->extbb_hash = NULL;
322 for (i = 0; i < sizeof(elem->opt_hash)/sizeof(elem->opt_hash[0]); ++i)
323 elem->opt_hash[i] = new_pset(opt_cmp, 4);
325 return pset_insert(hmap, elem, HASH_PTR(irg));
326 } /* graph_get_entry */
329 * Clear all counter in an opt_entry_t.
331 static void opt_clear_entry(opt_entry_t *elem) {
332 cnt_clr(&elem->count);
333 } /* opt_clear_entry */
336 * Returns the associated opt_entry_t for an IR operation.
338 * @param op the IR operation
339 * @param hmap the hash map containing ir_op* -> opt_entry_t*
341 static opt_entry_t *opt_get_entry(const ir_op *op, hmap_opt_entry_t *hmap)
348 elem = pset_find(hmap, &key, op->code);
352 elem = obstack_alloc(&status->cnts, sizeof(*elem));
353 memset(elem, 0, sizeof(*elem));
355 /* clear new counter */
356 opt_clear_entry(elem);
360 return pset_insert(hmap, elem, op->code);
361 } /* opt_get_entry */
364 * clears all counter in a block_entry_t
366 static void block_clear_entry(block_entry_t *elem) {
369 for (i = 0; i < _bcnt_last; ++i)
370 cnt_clr(&elem->cnt[i]);
371 } /* block_clear_entry */
374 * Returns the associated block_entry_t for an block.
376 * @param block_nr an IR block number
377 * @param hmap a hash map containing long -> block_entry_t
379 static block_entry_t *block_get_entry(struct obstack *obst, long block_nr, hmap_block_entry_t *hmap)
384 key.block_nr = block_nr;
386 elem = pset_find(hmap, &key, block_nr);
390 elem = obstack_alloc(obst, sizeof(*elem));
391 memset(elem, 0, sizeof(*elem));
393 /* clear new counter */
394 block_clear_entry(elem);
396 elem->block_nr = block_nr;
398 return pset_insert(hmap, elem, block_nr);
399 } /* block_get_entry */
402 * Clear all sets in be_block_entry_t.
404 static void be_block_clear_entry(be_block_entry_t *elem)
406 if (elem->reg_pressure)
407 del_pset(elem->reg_pressure);
409 if (elem->sched_ready)
410 stat_delete_distrib_tbl(elem->sched_ready);
412 if (elem->perm_class_stat)
413 del_pset(elem->perm_class_stat);
415 elem->reg_pressure = new_pset(reg_pressure_cmp, 5);
416 elem->sched_ready = stat_new_int_distrib_tbl();
417 elem->perm_class_stat = new_pset(perm_class_cmp, 5);
418 } /* be_block_clear_entry */
421 * Returns the associated be_block_entry_t for an block.
423 * @param block_nr an IR block number
424 * @param hmap a hash map containing long -> be_block_entry_t
426 static be_block_entry_t *be_block_get_entry(struct obstack *obst, long block_nr, hmap_be_block_entry_t *hmap)
428 be_block_entry_t key;
429 be_block_entry_t *elem;
431 key.block_nr = block_nr;
433 elem = pset_find(hmap, &key, block_nr);
437 elem = obstack_alloc(obst, sizeof(*elem));
438 memset(elem, 0, sizeof(*elem));
440 /* clear new counter */
441 be_block_clear_entry(elem);
443 elem->block_nr = block_nr;
445 return pset_insert(hmap, elem, block_nr);
446 } /* be_block_get_entry */
449 * clears all sets in perm_class_entry_t
451 static void perm_class_clear_entry(perm_class_entry_t *elem) {
453 del_pset(elem->perm_stat);
455 elem->perm_stat = new_pset(perm_stat_cmp, 5);
456 } /* perm_class_clear_entry */
459 * Returns the associated perm_class entry for a register class.
461 * @param class_name the register class name
462 * @param hmap a hash map containing class_name -> perm_class_entry_t
464 static perm_class_entry_t *perm_class_get_entry(struct obstack *obst, const char *class_name,
465 hmap_perm_class_entry_t *hmap)
467 perm_class_entry_t key;
468 perm_class_entry_t *elem;
470 key.class_name = class_name;
472 elem = pset_find(hmap, &key, HASH_PTR(class_name));
476 elem = obstack_alloc(obst, sizeof(*elem));
477 memset(elem, 0, sizeof(*elem));
479 /* clear new counter */
480 perm_class_clear_entry(elem);
482 elem->class_name = class_name;
484 return pset_insert(hmap, elem, HASH_PTR(class_name));
485 } /* perm_class_get_entry */
488 * clears all sets in perm_stat_entry_t
490 static void perm_stat_clear_entry(perm_stat_entry_t *elem) {
492 stat_delete_distrib_tbl(elem->chains);
495 stat_delete_distrib_tbl(elem->cycles);
497 elem->chains = stat_new_int_distrib_tbl();
498 elem->cycles = stat_new_int_distrib_tbl();
499 } /* perm_stat_clear_entry */
502 * Returns the associated perm_stat entry for a perm.
504 * @param perm the perm node
505 * @param hmap a hash map containing perm -> perm_stat_entry_t
507 static perm_stat_entry_t *perm_stat_get_entry(struct obstack *obst, ir_node *perm, hmap_perm_stat_entry_t *hmap)
509 perm_stat_entry_t key;
510 perm_stat_entry_t *elem;
514 elem = pset_find(hmap, &key, HASH_PTR(perm));
518 elem = obstack_alloc(obst, sizeof(*elem));
519 memset(elem, 0, sizeof(*elem));
521 /* clear new counter */
522 perm_stat_clear_entry(elem);
526 return pset_insert(hmap, elem, HASH_PTR(perm));
527 } /* perm_stat_get_entry */
530 * Clear optimizations counter,
532 static void clear_optimization_counter(void) {
534 for (i = 0; i < FS_OPT_MAX; ++i)
535 cnt_clr(&status->num_opts[i]);
539 * Returns the ir_op for an IR-node,
540 * handles special cases and return pseudo op codes.
542 * @param none an IR node
544 static ir_op *stat_get_irn_op(ir_node *node)
546 ir_op *op = get_irn_op(node);
547 ir_opcode opc = op->code;
551 if (get_irn_arity(node) == 0) {
552 /* special case, a Phi0 node, count on extra counter */
553 op = status->op_Phi0 ? status->op_Phi0 : op;
554 } else if (get_irn_mode(node) == mode_M) {
555 /* special case, a Memory Phi node, count on extra counter */
556 op = status->op_PhiM ? status->op_PhiM : op;
560 if (get_irn_mode(node) == mode_M) {
561 /* special case, a Memory Proj node, count on extra counter */
562 op = status->op_ProjM ? status->op_ProjM : op;
566 if (get_irn_op(get_Mul_left(node)) == op_Const || get_irn_op(get_Mul_right(node)) == op_Const) {
567 /* special case, a Multiply by a const, count on extra counter */
568 op = status->op_MulC ? status->op_MulC : op;
572 if (get_irn_op(get_Div_right(node)) == op_Const) {
573 /* special case, a division by a const, count on extra counter */
574 op = status->op_DivC ? status->op_DivC : op;
578 if (get_irn_op(get_Mod_right(node)) == op_Const) {
579 /* special case, a module by a const, count on extra counter */
580 op = status->op_ModC ? status->op_ModC : op;
584 if (get_irn_op(get_DivMod_right(node)) == op_Const) {
585 /* special case, a division/modulo by a const, count on extra counter */
586 op = status->op_DivModC ? status->op_DivModC : op;
590 if (get_irn_op(get_Sel_ptr(node)) == op_Sel) {
591 /* special case, a Sel of a Sel, count on extra counter */
592 op = status->op_SelSel ? status->op_SelSel : op;
593 if (get_irn_op(get_Sel_ptr(get_Sel_ptr(node))) == op_Sel) {
594 /* special case, a Sel of a Sel of a Sel, count on extra counter */
595 op = status->op_SelSelSel ? status->op_SelSelSel : op;
604 } /* stat_get_irn_op */
607 * update the block counter
609 static void undate_block_info(ir_node *node, graph_entry_t *graph)
611 ir_op *op = get_irn_op(node);
613 block_entry_t *b_entry;
616 /* check for block */
617 if (op == op_Block) {
618 arity = get_irn_arity(node);
619 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(node), graph->block_hash);
621 /* count all incoming edges */
622 for (i = 0; i < arity; ++i) {
623 ir_node *pred = get_irn_n(node, i);
624 ir_node *other_block = get_nodes_block(pred);
625 block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
627 cnt_inc(&b_entry->cnt[bcnt_in_edges]); /* an edge coming from another block */
628 cnt_inc(&b_entry_other->cnt[bcnt_out_edges]);
633 block = get_nodes_block(node);
634 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(block), graph->block_hash);
636 if (op == op_Phi && mode_is_datab(get_irn_mode(node))) {
637 /* count data Phi per block */
638 cnt_inc(&b_entry->cnt[bcnt_phi_data]);
641 /* we have a new node in our block */
642 cnt_inc(&b_entry->cnt[bcnt_nodes]);
644 /* don't count keep-alive edges */
645 if (get_irn_op(node) == op_End)
648 arity = get_irn_arity(node);
650 for (i = 0; i < arity; ++i) {
651 ir_node *pred = get_irn_n(node, i);
652 ir_node *other_block;
654 other_block = get_nodes_block(pred);
656 if (other_block == block)
657 cnt_inc(&b_entry->cnt[bcnt_edges]); /* a in block edge */
659 block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
661 cnt_inc(&b_entry->cnt[bcnt_in_edges]); /* an edge coming from another block */
662 cnt_inc(&b_entry_other->cnt[bcnt_out_edges]);
665 } /* undate_block_info */
668 * Update the extended block counter.
670 static void update_extbb_info(ir_node *node, graph_entry_t *graph)
672 ir_op *op = get_irn_op(node);
674 extbb_entry_t *eb_entry;
677 /* check for block */
678 if (op == op_Block) {
679 extbb = get_nodes_extbb(node);
680 arity = get_irn_arity(node);
681 eb_entry = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(extbb), graph->extbb_hash);
683 /* count all incoming edges */
684 for (i = 0; i < arity; ++i) {
685 ir_node *pred = get_irn_n(node, i);
686 ir_extblk *other_extbb = get_nodes_extbb(pred);
688 if (extbb != other_extbb) {
689 extbb_entry_t *eb_entry_other = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(other_extbb), graph->extbb_hash);
691 cnt_inc(&eb_entry->cnt[bcnt_in_edges]); /* an edge coming from another extbb */
692 cnt_inc(&eb_entry_other->cnt[bcnt_out_edges]);
698 extbb = get_nodes_extbb(node);
699 eb_entry = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(extbb), graph->extbb_hash);
701 if (op == op_Phi && mode_is_datab(get_irn_mode(node))) {
702 /* count data Phi per extbb */
703 cnt_inc(&eb_entry->cnt[bcnt_phi_data]);
706 /* we have a new node in our block */
707 cnt_inc(&eb_entry->cnt[bcnt_nodes]);
709 /* don't count keep-alive edges */
710 if (get_irn_op(node) == op_End)
713 arity = get_irn_arity(node);
715 for (i = 0; i < arity; ++i) {
716 ir_node *pred = get_irn_n(node, i);
717 ir_extblk *other_extbb = get_nodes_extbb(pred);
719 if (other_extbb == extbb)
720 cnt_inc(&eb_entry->cnt[bcnt_edges]); /* a in extbb edge */
722 extbb_entry_t *eb_entry_other = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(other_extbb), graph->extbb_hash);
724 cnt_inc(&eb_entry->cnt[bcnt_in_edges]); /* an edge coming from another extbb */
725 cnt_inc(&eb_entry_other->cnt[bcnt_out_edges]);
728 } /* update_extbb_info */
731 * Calculates how many arguments of the call are const, updates
732 * param distribution.
734 static void analyse_params_of_Call(graph_entry_t *graph, ir_node *call) {
735 int i, num_const_args = 0, num_local_adr = 0;
736 int n = get_Call_n_params(call);
738 for (i = 0; i < n; ++i) {
739 ir_node *param = get_Call_param(call, i);
741 if (is_irn_constlike(param))
743 else if (is_Sel(param)) {
744 ir_node *base = param;
747 base = get_Sel_ptr(base);
748 } while (is_Sel(base));
750 if (base == get_irg_frame(current_ir_graph))
756 if (num_const_args > 0)
757 cnt_inc(&graph->cnt[gcnt_call_with_cnst_arg]);
758 if (num_const_args == n)
759 cnt_inc(&graph->cnt[gcnt_call_with_all_cnst_arg]);
760 if (num_local_adr > 0)
761 cnt_inc(&graph->cnt[gcnt_call_with_local_adr]);
763 stat_inc_int_distrib_tbl(status->dist_param_cnt, n);
764 } /* analyse_params_of_Call */
767 * Update info on calls.
769 * @param call The call
770 * @param graph The graph entry containing the call
772 static void stat_update_call(ir_node *call, graph_entry_t *graph)
774 ir_node *block = get_nodes_block(call);
775 ir_node *ptr = get_Call_ptr(call);
776 ir_entity *ent = NULL;
777 ir_graph *callee = NULL;
780 * If the block is bad, the whole subgraph will collapse later
781 * so do not count this call.
782 * This happens in dead code.
787 cnt_inc(&graph->cnt[gcnt_all_calls]);
789 /* found a call, this function is not a leaf */
792 if (get_irn_op(ptr) == op_SymConst) {
793 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
794 /* ok, we seems to know the entity */
795 ent = get_SymConst_entity(ptr);
796 callee = get_entity_irg(ent);
798 /* it is recursive, if it calls at least once */
799 if (callee == graph->irg)
800 graph->is_recursive = 1;
803 /* indirect call, be could not predict */
804 cnt_inc(&graph->cnt[gcnt_indirect_calls]);
806 /* NOT a leaf call */
807 graph->is_leaf_call = LCS_NON_LEAF_CALL;
810 /* check, if it's a chain-call: Then, the call-block
811 * must dominate the end block. */
813 ir_node *curr = get_irg_end_block(graph->irg);
814 int depth = get_Block_dom_depth(block);
816 for (; curr != block && get_Block_dom_depth(curr) > depth;) {
817 curr = get_Block_idom(curr);
819 if (! curr || is_no_Block(curr))
824 graph->is_chain_call = 0;
827 /* check, if the callee is a leaf */
829 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
831 if (called->is_analyzed) {
832 if (! called->is_leaf)
833 graph->is_leaf_call = LCS_NON_LEAF_CALL;
837 analyse_params_of_Call(graph, call);
838 } /* stat_update_call */
841 * Update info on calls for graphs on the wait queue.
843 static void stat_update_call_2(ir_node *call, graph_entry_t *graph)
845 ir_node *block = get_nodes_block(call);
846 ir_node *ptr = get_Call_ptr(call);
847 ir_entity *ent = NULL;
848 ir_graph *callee = NULL;
851 * If the block is bad, the whole subgraph will collapse later
852 * so do not count this call.
853 * This happens in dead code.
858 if (get_irn_op(ptr) == op_SymConst) {
859 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
860 /* ok, we seems to know the entity */
861 ent = get_SymConst_entity(ptr);
862 callee = get_entity_irg(ent);
866 /* check, if the callee is a leaf */
868 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
870 assert(called->is_analyzed);
872 if (! called->is_leaf)
873 graph->is_leaf_call = LCS_NON_LEAF_CALL;
875 graph->is_leaf_call = LCS_NON_LEAF_CALL;
876 } /* stat_update_call_2 */
879 * Find the base address and entity of an Sel node.
881 * @param sel the node
883 * @return the base address.
885 static ir_node *find_base_adr(ir_node *sel) {
886 ir_node *ptr = get_Sel_ptr(sel);
888 while (is_Sel(ptr)) {
890 ptr = get_Sel_ptr(sel);
893 } /* find_base_adr */
896 * Update info on Load/Store address statistics.
898 static void stat_update_address(ir_node *node, graph_entry_t *graph) {
899 ir_opcode opc = get_irn_opcode(node);
905 /* a global address */
906 cnt_inc(&graph->cnt[gcnt_global_adr]);
909 base = find_base_adr(node);
910 irg = current_ir_graph;
911 if (base == get_irg_tls(irg)) {
912 /* a TLS variable, like a global. */
913 cnt_inc(&graph->cnt[gcnt_global_adr]);
914 } else if (base == get_irg_frame(irg)) {
915 /* a local Variable. */
916 cnt_inc(&graph->cnt[gcnt_local_adr]);
919 if (is_Proj(base) && skip_Proj(get_Proj_pred(base)) == get_irg_start(irg)) {
920 /* pointer access through parameter, check for THIS */
921 ir_entity *ent = get_irg_entity(irg);
924 ir_type *ent_tp = get_entity_type(ent);
926 if (get_method_calling_convention(ent_tp) & cc_this_call) {
927 if (get_Proj_proj(base) == 0) {
929 cnt_inc(&graph->cnt[gcnt_this_adr]);
934 /* other parameter */
935 cnt_inc(&graph->cnt[gcnt_param_adr]);
938 /* unknown Pointer access */
939 cnt_inc(&graph->cnt[gcnt_other_adr]);
945 } /* stat_update_address */
948 * Walker for reachable nodes count.
950 static void update_node_stat(ir_node *node, void *env)
952 graph_entry_t *graph = env;
955 ir_op *op = stat_get_irn_op(node);
956 int arity = get_irn_arity(node);
958 entry = opcode_get_entry(op, graph->opcode_hash);
960 cnt_inc(&entry->cnt_alive);
961 cnt_add_i(&graph->cnt[gcnt_edges], arity);
963 /* count block edges */
964 undate_block_info(node, graph);
966 /* count extended block edges */
967 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
968 if (graph->irg != get_const_code_irg())
969 update_extbb_info(node, graph);
972 /* handle statistics for special node types */
976 if (status->stat_options & FIRMSTAT_COUNT_CONSTS) {
977 /* check properties of constants */
978 stat_update_const(status, node, graph);
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);
996 } /* update_node_stat */
999 * Walker for reachable nodes count for graphs on the wait_q.
1001 static void update_node_stat_2(ir_node *node, void *env) {
1002 graph_entry_t *graph = env;
1004 /* check for properties that depends on calls like recursion/leaf/indirect call */
1006 stat_update_call_2(node, graph);
1007 } /* update_node_stat_2 */
1010 * Get the current address mark.
1012 static unsigned get_adr_mark(graph_entry_t *graph, ir_node *node) {
1013 address_mark_entry_t *value = set_find(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
1015 return value ? value->mark : 0;
1016 } /* get_adr_mark */
1019 * Set the current address mark.
1021 static void set_adr_mark(graph_entry_t *graph, ir_node *node, unsigned val) {
1022 address_mark_entry_t *value = set_insert(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
1025 } /* set_adr_mark */
1027 #undef DUMP_ADR_MODE
1029 #ifdef DUMP_ADR_MODE
1031 * a vcg attribute hook: Color a node with a different color if
1032 * it's identified as a part of an address expression or at least referenced
1033 * by an address expression.
1035 static int stat_adr_mark_hook(FILE *F, ir_node *node, ir_node *local)
1037 ir_node *n = local ? local : node;
1038 ir_graph *irg = get_irn_irg(n);
1039 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1040 unsigned mark = get_adr_mark(graph, n);
1042 if (mark & MARK_ADDRESS_CALC)
1043 fprintf(F, "color: purple");
1044 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1045 fprintf(F, "color: pink");
1046 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1047 fprintf(F, "color: lightblue");
1051 /* I know the color! */
1053 } /* stat_adr_mark_hook */
1054 #endif /* DUMP_ADR_MODE */
1057 * Return the "operational" mode of a Firm node.
1059 static ir_mode *get_irn_op_mode(ir_node *node) {
1060 switch (get_irn_opcode(node)) {
1062 return get_Load_mode(node);
1064 return get_irn_mode(get_Store_value(node));
1066 return get_irn_mode(get_DivMod_left(node));
1068 return get_irn_mode(get_Div_left(node));
1070 return get_irn_mode(get_Mod_left(node));
1072 /* Cmp is no address calculation, or is it? */
1074 return get_irn_mode(node);
1076 } /* get_irn_op_mode */
1079 * Post-walker that marks every node that is an address calculation.
1081 * Users of a node must be visited first. We ensure this by
1082 * calling it in the post of an outs walk. This should work even in cycles,
1083 * while the normal pre-walk will not.
1085 static void mark_address_calc(ir_node *node, void *env) {
1086 graph_entry_t *graph = env;
1087 ir_mode *mode = get_irn_op_mode(node);
1089 unsigned mark_preds = MARK_REF_NON_ADR;
1091 if (! mode_is_data(mode))
1094 if (mode_is_reference(mode)) {
1095 /* a reference is calculated here, we are sure */
1096 set_adr_mark(graph, node, MARK_ADDRESS_CALC);
1098 mark_preds = MARK_REF_ADR;
1100 unsigned mark = get_adr_mark(graph, node);
1102 if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR) {
1104 * this node has no reference mode, but is only
1105 * referenced by address calculations
1107 mark_preds = MARK_REF_ADR;
1111 /* mark all predecessors */
1112 for (i = 0, n = get_irn_arity(node); i < n; ++i) {
1113 ir_node *pred = get_irn_n(node, i);
1115 mode = get_irn_op_mode(pred);
1116 if (! mode_is_data(mode))
1119 set_adr_mark(graph, pred, get_adr_mark(graph, pred) | mark_preds);
1121 } /* mark_address_calc */
1124 * Post-walker that marks every node that is an address calculation.
1126 * Users of a node must be visited first. We ensure this by
1127 * calling it in the post of an outs walk. This should work even in cycles,
1128 * while the normal pre-walk will not.
1130 static void count_adr_ops(ir_node *node, void *env) {
1131 graph_entry_t *graph = env;
1132 unsigned mark = get_adr_mark(graph, node);
1134 if (mark & MARK_ADDRESS_CALC)
1135 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1136 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1137 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1138 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1139 cnt_inc(&graph->cnt[gcnt_all_adr_ops]);
1140 } /* count_adr_ops */
1143 * Called for every graph when the graph is either deleted or stat_dump_snapshot()
1144 * is called, must recalculate all statistic info.
1146 * @param global The global entry
1147 * @param graph The current entry
1149 static void update_graph_stat(graph_entry_t *global, graph_entry_t *graph)
1151 node_entry_t *entry;
1154 /* clear first the alive counter in the graph */
1155 foreach_pset(graph->opcode_hash, entry) {
1156 cnt_clr(&entry->cnt_alive);
1157 } /* foreach_pset */
1159 /* set pessimistic values */
1161 graph->is_leaf_call = LCS_UNKNOWN;
1162 graph->is_recursive = 0;
1163 graph->is_chain_call = 1;
1165 /* create new block counter */
1166 graph->block_hash = new_pset(block_cmp, 5);
1168 /* we need dominator info */
1169 if (graph->irg != get_const_code_irg()) {
1170 assure_doms(graph->irg);
1172 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
1173 /* we need extended basic blocks */
1174 compute_extbb(graph->irg);
1176 /* create new extbb counter */
1177 graph->extbb_hash = new_pset(block_cmp, 5);
1181 /* count the nodes in the graph */
1182 irg_walk_graph(graph->irg, update_node_stat, NULL, graph);
1185 /* Uncomment this code if chain-call means call exact one. */
1186 entry = opcode_get_entry(op_Call, graph->opcode_hash);
1188 /* check if we have more than 1 call */
1189 if (cnt_gt(entry->cnt_alive, 1))
1190 graph->is_chain_call = 0;
1193 /* recursive functions are never chain calls, leafs don't have calls */
1194 if (graph->is_recursive || graph->is_leaf)
1195 graph->is_chain_call = 0;
1197 /* assume we walk every graph only ONCE, we could sum here the global count */
1198 foreach_pset(graph->opcode_hash, entry) {
1199 node_entry_t *g_entry = opcode_get_entry(entry->op, global->opcode_hash);
1201 /* update the node counter */
1202 cnt_add(&g_entry->cnt_alive, &entry->cnt_alive);
1203 } /* foreach_pset */
1205 /* count the number of address calculation */
1206 if (graph->irg != get_const_code_irg()) {
1207 ir_graph *rem = current_ir_graph;
1209 assure_irg_outs(graph->irg);
1211 /* Must be done an the outs graph */
1212 current_ir_graph = graph->irg;
1213 irg_out_walk(get_irg_start(graph->irg), NULL, mark_address_calc, graph);
1214 current_ir_graph = rem;
1216 #ifdef DUMP_ADR_MODE
1217 /* register the vcg hook and dump the graph for test */
1218 set_dump_node_vcgattr_hook(stat_adr_mark_hook);
1219 dump_ir_block_graph(graph->irg, "-adr");
1220 set_dump_node_vcgattr_hook(NULL);
1221 #endif /* DUMP_ADR_MODE */
1223 irg_walk_graph(graph->irg, NULL, count_adr_ops, graph);
1226 /* count the DAG's */
1227 if (status->stat_options & FIRMSTAT_COUNT_DAG)
1228 count_dags_in_graph(global, graph);
1230 /* calculate the patterns of this graph */
1231 stat_calc_pattern_history(graph->irg);
1233 /* leaf function did not call others */
1235 graph->is_leaf_call = LCS_NON_LEAF_CALL;
1236 else if (graph->is_leaf_call == LCS_UNKNOWN) {
1237 /* we still don't know if this graph calls leaf-functions, so enqueue */
1238 pdeq_putl(status->wait_q, graph);
1241 /* we have analyzed this graph */
1242 graph->is_analyzed = 1;
1244 /* accumulate all counter's */
1245 for (i = 0; i < _gcnt_last; ++i)
1246 cnt_add(&global->cnt[i], &graph->cnt[i]);
1247 } /* update_graph_stat */
1250 * Called for every graph that was on the wait_q in stat_dump_snapshot()
1251 * must finish all statistic info calculations.
1253 * @param global The global entry
1254 * @param graph The current entry
1256 static void update_graph_stat_2(graph_entry_t *global, graph_entry_t *graph)
1259 if (graph->is_deleted) {
1260 /* deleted, ignore */
1265 /* count the nodes in the graph */
1266 irg_walk_graph(graph->irg, update_node_stat_2, NULL, graph);
1268 if (graph->is_leaf_call == LCS_UNKNOWN)
1269 graph->is_leaf_call = LCS_LEAF_CALL;
1271 } /* update_graph_stat_2 */
1274 * Register a dumper.
1276 static void stat_register_dumper(const dumper_t *dumper) {
1277 dumper_t *p = xmalloc(sizeof(*p));
1280 memcpy(p, dumper, sizeof(*p));
1282 p->next = status->dumper;
1287 /* FIXME: memory leak */
1288 } /* stat_register_dumper */
1291 * Dumps the statistics of an IR graph.
1293 static void stat_dump_graph(graph_entry_t *entry) {
1296 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1297 if (dumper->dump_graph)
1298 dumper->dump_graph(dumper, entry);
1300 } /* stat_dump_graph */
1303 * Calls all registered dumper functions.
1305 static void stat_dump_registered(graph_entry_t *entry) {
1308 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1309 if (dumper->func_map) {
1310 dump_graph_FUNC func;
1312 foreach_pset(dumper->func_map, func)
1313 func(dumper, entry);
1316 } /* stat_dump_registered */
1319 * Dumps a constant table.
1321 static void stat_dump_consts(const constant_info_t *tbl) {
1324 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1325 if (dumper->dump_const_tbl)
1326 dumper->dump_const_tbl(dumper, tbl);
1328 } /* stat_dump_consts */
1331 * Dumps the parameter distribution
1333 static void stat_dump_param_tbl(const distrib_tbl_t *tbl, graph_entry_t *global) {
1336 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1337 if (dumper->dump_param_tbl)
1338 dumper->dump_param_tbl(dumper, tbl, global);
1340 } /* stat_dump_param_tbl */
1343 * Dumps the optimization counter
1345 static void stat_dump_opt_cnt(const counter_t *tbl, unsigned len) {
1348 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1349 if (dumper->dump_opt_cnt)
1350 dumper->dump_opt_cnt(dumper, tbl, len);
1352 } /* stat_dump_opt_cnt */
1355 * Initialize the dumper.
1357 static void stat_dump_init(const char *name) {
1360 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1362 dumper->init(dumper, name);
1364 } /* stat_dump_init */
1367 * Finish the dumper.
1369 static void stat_dump_finish(void) {
1372 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1374 dumper->finish(dumper);
1376 } /* stat_dump_finish */
1379 * Register an additional function for all dumper.
1381 void stat_register_dumper_func(dump_graph_FUNC func) {
1384 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1385 if (! dumper->func_map)
1386 dumper->func_map = pset_new_ptr(3);
1387 pset_insert_ptr(dumper->func_map, func);
1389 } /* stat_register_dumper_func */
1391 /* ---------------------------------------------------------------------- */
1394 * Helper: get an ir_op from an opcode.
1396 ir_op *stat_get_op_from_opcode(ir_opcode code) {
1397 return opcode_find_entry(code, status->ir_op_hash);
1398 } /* stat_get_op_from_opcode */
1401 * Hook: A new IR op is registered.
1403 * @param ctx the hook context
1404 * @param op the new IR opcode that was created.
1406 static void stat_new_ir_op(void *ctx, ir_op *op) {
1408 if (! status->stat_options)
1413 graph_entry_t *graph = graph_get_entry(NULL, status->irg_hash);
1415 /* execute for side effect :-) */
1416 (void)opcode_get_entry(op, graph->opcode_hash);
1418 pset_insert(status->ir_op_hash, op, op->code);
1421 } /* stat_new_ir_op */
1424 * Hook: An IR op is freed.
1426 * @param ctx the hook context
1427 * @param op the IR opcode that is freed
1429 static void stat_free_ir_op(void *ctx, ir_op *op) {
1432 if (! status->stat_options)
1439 } /* stat_free_ir_op */
1442 * Hook: A new node is created.
1444 * @param ctx the hook context
1445 * @param irg the IR graph on which the node is created
1446 * @param node the new IR node that was created
1448 static void stat_new_node(void *ctx, ir_graph *irg, ir_node *node) {
1451 if (! status->stat_options)
1454 /* do NOT count during dead node elimination */
1455 if (status->in_dead_node_elim)
1460 node_entry_t *entry;
1461 graph_entry_t *graph;
1462 ir_op *op = stat_get_irn_op(node);
1464 /* increase global value */
1465 graph = graph_get_entry(NULL, status->irg_hash);
1466 entry = opcode_get_entry(op, graph->opcode_hash);
1467 cnt_inc(&entry->new_node);
1469 /* increase local value */
1470 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1471 entry = opcode_get_entry(op, graph->opcode_hash);
1472 cnt_inc(&entry->new_node);
1475 } /* stat_new_node */
1478 * Hook: A node is changed into a Id node
1480 * @param ctx the hook context
1481 * @param node the IR node that will be turned into an ID
1483 static void stat_turn_into_id(void *ctx, ir_node *node) {
1485 if (! status->stat_options)
1490 node_entry_t *entry;
1491 graph_entry_t *graph;
1492 ir_op *op = stat_get_irn_op(node);
1494 /* increase global value */
1495 graph = graph_get_entry(NULL, status->irg_hash);
1496 entry = opcode_get_entry(op, graph->opcode_hash);
1497 cnt_inc(&entry->into_Id);
1499 /* increase local value */
1500 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1501 entry = opcode_get_entry(op, graph->opcode_hash);
1502 cnt_inc(&entry->into_Id);
1505 } /* stat_turn_into_id */
1508 * Hook: A new graph was created
1510 * @param ctx the hook context
1511 * @param irg the new IR graph that was created
1512 * @param ent the entity of this graph
1514 static void stat_new_graph(void *ctx, ir_graph *irg, ir_entity *ent) {
1516 if (! status->stat_options)
1521 /* execute for side effect :-) */
1522 graph_entry_t * graph = graph_get_entry(irg, status->irg_hash);
1525 graph->is_deleted = 0;
1527 graph->is_leaf_call = 0;
1528 graph->is_recursive = 0;
1529 graph->is_chain_call = 0;
1530 graph->is_analyzed = 0;
1533 } /* stat_new_graph */
1536 * Hook: A graph will be deleted
1538 * @param ctx the hook context
1539 * @param irg the IR graph that will be deleted
1541 * Note that we still hold the information for this graph
1542 * in our hash maps, only a flag is set which prevents this
1543 * information from being changed, it's "frozen" from now.
1545 static void stat_free_graph(void *ctx, ir_graph *irg) {
1547 if (! status->stat_options)
1552 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1553 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1555 graph->is_deleted = 1;
1557 if (status->stat_options & FIRMSTAT_COUNT_DELETED) {
1558 /* count the nodes of the graph yet, it will be destroyed later */
1559 update_graph_stat(global, graph);
1563 } /* stat_free_graph */
1566 * Hook: A walk over a graph is initiated. Do not count walks from statistic code.
1568 * @param ctx the hook context
1569 * @param irg the IR graph that will be walked
1570 * @param pre the pre walker
1571 * @param post the post walker
1573 static void stat_irg_walk(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1578 if (! status->stat_options)
1583 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1585 cnt_inc(&graph->cnt[gcnt_acc_walked]);
1588 } /* stat_irg_walk */
1591 * Hook: A walk over a graph in block-wise order is initiated. Do not count walks from statistic code.
1593 * @param ctx the hook context
1594 * @param irg the IR graph that will be walked
1595 * @param pre the pre walker
1596 * @param post the post walker
1598 static void stat_irg_walk_blkwise(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1600 /* for now, do NOT differentiate between blockwise and normal */
1601 stat_irg_walk(ctx, irg, pre, post);
1602 } /* stat_irg_walk_blkwise */
1605 * Hook: A walk over the graph's blocks is initiated. Do not count walks from statistic code.
1607 * @param ctx the hook context
1608 * @param irg the IR graph that will be walked
1609 * @param node the IR node
1610 * @param pre the pre walker
1611 * @param post the post walker
1613 static void stat_irg_block_walk(void *ctx, ir_graph *irg, ir_node *node, generic_func *pre, generic_func *post)
1619 if (! status->stat_options)
1624 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1626 cnt_inc(&graph->cnt[gcnt_acc_walked_blocks]);
1629 } /* stat_irg_block_walk */
1632 * Called for every node that is removed due to an optimization.
1634 * @param n the IR node that will be removed
1635 * @param hmap the hash map containing ir_op* -> opt_entry_t*
1637 static void removed_due_opt(ir_node *n, hmap_opt_entry_t *hmap) {
1638 ir_op *op = stat_get_irn_op(n);
1639 opt_entry_t *entry = opt_get_entry(op, hmap);
1641 /* increase global value */
1642 cnt_inc(&entry->count);
1643 } /* removed_due_opt */
1646 * Hook: Some nodes were optimized into some others due to an optimization.
1648 * @param ctx the hook context
1650 static void stat_merge_nodes(
1652 ir_node **new_node_array, int new_num_entries,
1653 ir_node **old_node_array, int old_num_entries,
1657 if (! status->stat_options)
1663 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1665 cnt_inc(&status->num_opts[opt]);
1666 if (status->reassoc_run)
1667 opt = HOOK_OPT_REASSOC;
1669 for (i = 0; i < old_num_entries; ++i) {
1670 /* nodes might be in new and old, so if we found a node
1671 in both sets, this one is NOT removed */
1672 for (j = 0; j < new_num_entries; ++j) {
1673 if (old_node_array[i] == new_node_array[j])
1676 if (j >= new_num_entries) {
1679 /* sometimes we did not detect, that it is replaced by a Const */
1680 if (opt == HOOK_OPT_CONFIRM && new_num_entries == 1) {
1681 ir_op *op = get_irn_op(new_node_array[0]);
1683 if (op == op_Const || op == op_SymConst)
1684 xopt = HOOK_OPT_CONFIRM_C;
1687 removed_due_opt(old_node_array[i], graph->opt_hash[xopt]);
1692 } /* stat_merge_nodes */
1695 * Hook: Reassociation is started/stopped.
1697 * @param ctx the hook context
1698 * @param flag if non-zero, reassociation is started else stopped
1700 static void stat_reassociate(void *ctx, int flag) {
1702 if (! status->stat_options)
1707 status->reassoc_run = flag;
1710 } /* stat_reassociate */
1713 * Hook: A node was lowered into other nodes
1715 * @param ctx the hook context
1716 * @param node the IR node that will be lowered
1718 static void stat_lower(void *ctx, ir_node *node) {
1720 if (! status->stat_options)
1725 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1727 removed_due_opt(node, graph->opt_hash[HOOK_LOWERED]);
1733 * Hook: A graph was inlined.
1735 * @param ctx the hook context
1736 * @param call the IR call that will re changed into the body of
1737 * the called IR graph
1738 * @param called_irg the IR graph representing the called routine
1740 static void stat_inline(void *ctx, ir_node *call, ir_graph *called_irg)
1743 if (! status->stat_options)
1748 ir_graph *irg = get_irn_irg(call);
1749 graph_entry_t *i_graph = graph_get_entry(called_irg, status->irg_hash);
1750 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1752 cnt_inc(&graph->cnt[gcnt_acc_got_inlined]);
1753 cnt_inc(&i_graph->cnt[gcnt_acc_was_inlined]);
1759 * Hook: A graph with tail-recursions was optimized.
1761 * @param ctx the hook context
1763 static void stat_tail_rec(void *ctx, ir_graph *irg, int n_calls) {
1765 if (! status->stat_options)
1770 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1772 graph->num_tail_recursion += n_calls;
1775 } /* stat_tail_rec */
1778 * Strength reduction was performed on an iteration variable.
1780 * @param ctx the hook context
1782 static void stat_strength_red(void *ctx, ir_graph *irg, ir_node *strong) {
1784 if (! status->stat_options)
1789 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1790 cnt_inc(&graph->cnt[gcnt_acc_strength_red]);
1792 removed_due_opt(strong, graph->opt_hash[HOOK_OPT_STRENGTH_RED]);
1795 } /* stat_strength_red */
1798 * Hook: Start/Stop the dead node elimination.
1800 * @param ctx the hook context
1802 static void stat_dead_node_elim(void *ctx, ir_graph *irg, int start) {
1805 if (! status->stat_options)
1808 status->in_dead_node_elim = (start != 0);
1809 } /* stat_dead_node_elim */
1812 * Hook: if-conversion was tried.
1814 static void stat_if_conversion(void *context, ir_graph *irg, ir_node *phi,
1815 int pos, ir_node *mux, if_result_t reason)
1821 if (! status->stat_options)
1826 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1828 cnt_inc(&graph->cnt[gcnt_if_conv + reason]);
1831 } /* stat_if_conversion */
1834 * Hook: real function call was optimized.
1836 static void stat_func_call(void *context, ir_graph *irg, ir_node *call)
1840 if (! status->stat_options)
1845 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1847 cnt_inc(&graph->cnt[gcnt_acc_real_func_call]);
1850 } /* stat_func_call */
1853 * Hook: A multiply was replaced by a series of Shifts/Adds/Subs.
1855 * @param ctx the hook context
1857 static void stat_arch_dep_replace_mul_with_shifts(void *ctx, ir_node *mul) {
1859 if (! status->stat_options)
1864 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1865 removed_due_opt(mul, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1868 } /* stat_arch_dep_replace_mul_with_shifts */
1871 * Hook: A division by const was replaced.
1873 * @param ctx the hook context
1874 * @param node the division node that will be optimized
1876 static void stat_arch_dep_replace_division_by_const(void *ctx, ir_node *node) {
1878 if (! status->stat_options)
1883 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1884 removed_due_opt(node, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1887 } /* stat_arch_dep_replace_division_by_const */
1890 * Update the register pressure of a block.
1892 * @param irg the irg containing the block
1893 * @param block the block for which the reg pressure should be set
1894 * @param pressure the pressure
1895 * @param class_name the name of the register class
1897 void stat_be_block_regpressure(ir_graph *irg, ir_node *block, int pressure, const char *class_name)
1899 if (! status->stat_options)
1904 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1905 be_block_entry_t *block_ent;
1906 reg_pressure_entry_t *rp_ent;
1908 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1909 rp_ent = obstack_alloc(&status->be_data, sizeof(*rp_ent));
1910 memset(rp_ent, 0, sizeof(*rp_ent));
1912 rp_ent->class_name = class_name;
1913 rp_ent->pressure = pressure;
1915 pset_insert(block_ent->reg_pressure, rp_ent, HASH_PTR(class_name));
1918 } /* stat_be_block_regpressure */
1921 * Update the distribution of ready nodes of a block
1923 * @param irg the irg containing the block
1924 * @param block the block for which the reg pressure should be set
1925 * @param num_ready the number of ready nodes
1927 void stat_be_block_sched_ready(ir_graph *irg, ir_node *block, int num_ready)
1929 if (! status->stat_options)
1934 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1935 be_block_entry_t *block_ent;
1937 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1939 /* increase the counter of corresponding number of ready nodes */
1940 stat_inc_int_distrib_tbl(block_ent->sched_ready, num_ready);
1943 } /* stat_be_block_sched_ready */
1946 * Update the permutation statistic of a block.
1948 * @param class_name the name of the register class
1949 * @param n_regs number of registers in the register class
1950 * @param perm the perm node
1951 * @param block the block containing the perm
1952 * @param size the size of the perm
1953 * @param real_size number of pairs with different registers
1955 void stat_be_block_stat_perm(const char *class_name, int n_regs, ir_node *perm, ir_node *block,
1956 int size, int real_size)
1958 if (! status->stat_options)
1963 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
1964 be_block_entry_t *block_ent;
1965 perm_class_entry_t *pc_ent;
1966 perm_stat_entry_t *ps_ent;
1968 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1969 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
1970 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
1972 pc_ent->n_regs = n_regs;
1974 /* update information */
1975 ps_ent->size = size;
1976 ps_ent->real_size = real_size;
1979 } /* stat_be_block_stat_perm */
1982 * Update the permutation statistic of a single perm.
1984 * @param class_name the name of the register class
1985 * @param perm the perm node
1986 * @param block the block containing the perm
1987 * @param is_chain 1 if chain, 0 if cycle
1988 * @param size length of the cycle/chain
1989 * @param n_ops the number of ops representing this cycle/chain after lowering
1991 void stat_be_block_stat_permcycle(const char *class_name, ir_node *perm, ir_node *block,
1992 int is_chain, int size, int n_ops)
1994 if (! status->stat_options)
1999 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
2000 be_block_entry_t *block_ent;
2001 perm_class_entry_t *pc_ent;
2002 perm_stat_entry_t *ps_ent;
2004 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
2005 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
2006 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
2009 ps_ent->n_copies += n_ops;
2010 stat_inc_int_distrib_tbl(ps_ent->chains, size);
2012 ps_ent->n_exchg += n_ops;
2013 stat_inc_int_distrib_tbl(ps_ent->cycles, size);
2017 } /* stat_be_block_stat_permcycle */
2019 /* Dumps a statistics snapshot. */
2020 void stat_dump_snapshot(const char *name, const char *phase)
2026 if (! status->stat_options)
2031 graph_entry_t *entry;
2032 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
2035 * The constant counter is only global, so we clear it here.
2036 * Note that it does NOT contain the constants in DELETED
2037 * graphs due to this.
2039 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2040 stat_const_clear(status);
2042 /* build the name */
2043 p = strrchr(name, '/');
2048 q = strrchr(name, '\\');
2050 /* NULL might be not the smallest pointer */
2051 if (q && (!p || q > p))
2059 if (l > (int) (sizeof(fname) - 1))
2060 l = sizeof(fname) - 1;
2062 memcpy(fname, name, l);
2068 strncat(fname, "firmstat-", sizeof(fname));
2069 strncat(fname, phase, sizeof(fname));
2070 strncat(fname, "-", sizeof(fname));
2071 strncat(fname, p, sizeof(fname));
2073 stat_dump_init(fname);
2075 /* calculate the graph statistics */
2076 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
2077 if (entry->irg == NULL) {
2078 /* special entry for the global count */
2081 if (! entry->is_deleted) {
2082 /* the graph is still alive, count the nodes on it */
2083 update_graph_stat(global, entry);
2087 /* some calculations are dependent, we pushed them on the wait_q */
2088 while (! pdeq_empty(status->wait_q)) {
2089 entry = pdeq_getr(status->wait_q);
2091 update_graph_stat_2(global, entry);
2094 /* dump per graph */
2095 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
2096 if (entry->irg == NULL) {
2097 /* special entry for the global count */
2101 if (! entry->is_deleted || status->stat_options & FIRMSTAT_COUNT_DELETED) {
2102 stat_dump_graph(entry);
2103 stat_dump_registered(entry);
2106 if (! entry->is_deleted) {
2107 /* clear the counter that are not accumulated */
2108 graph_clear_entry(entry, 0);
2113 stat_dump_graph(global);
2115 /* dump the const info */
2116 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2117 stat_dump_consts(&status->const_info);
2119 /* dump the parameter distribution */
2120 stat_dump_param_tbl(status->dist_param_cnt, global);
2122 /* dump the optimization counter and clear them */
2123 stat_dump_opt_cnt(status->num_opts, ARR_SIZE(status->num_opts));
2124 clear_optimization_counter();
2128 stat_finish_pattern_history(fname);
2130 /* clear the global counter here */
2132 node_entry_t *entry;
2134 for (entry = pset_first(global->opcode_hash); entry; entry = pset_next(global->opcode_hash)) {
2135 opcode_clear_entry(entry);
2137 /* clear all global counter */
2138 graph_clear_entry(global, 1);
2142 } /* stat_dump_snapshot */
2144 /** the hook entries for the Firm statistics module */
2145 static hook_entry_t stat_hooks[hook_last];
2147 /* initialize the statistics module. */
2148 void firm_init_stat(unsigned enable_options)
2150 #define X(a) a, sizeof(a)-1
2151 #define HOOK(h, fkt) \
2152 stat_hooks[h].hook._##h = fkt; register_hook(h, &stat_hooks[h])
2155 if (! (enable_options & FIRMSTAT_ENABLED))
2158 status = xmalloc(sizeof(*status));
2159 memset(status, 0, sizeof(*status));
2161 /* enable statistics */
2162 status->stat_options = enable_options & FIRMSTAT_ENABLED ? enable_options : 0;
2164 /* register all hooks */
2165 HOOK(hook_new_ir_op, stat_new_ir_op);
2166 HOOK(hook_free_ir_op, stat_free_ir_op);
2167 HOOK(hook_new_node, stat_new_node);
2168 HOOK(hook_turn_into_id, stat_turn_into_id);
2169 HOOK(hook_new_graph, stat_new_graph);
2170 HOOK(hook_free_graph, stat_free_graph);
2171 HOOK(hook_irg_walk, stat_irg_walk);
2172 HOOK(hook_irg_walk_blkwise, stat_irg_walk_blkwise);
2173 HOOK(hook_irg_block_walk, stat_irg_block_walk);
2174 HOOK(hook_merge_nodes, stat_merge_nodes);
2175 HOOK(hook_reassociate, stat_reassociate);
2176 HOOK(hook_lower, stat_lower);
2177 HOOK(hook_inline, stat_inline);
2178 HOOK(hook_tail_rec, stat_tail_rec);
2179 HOOK(hook_strength_red, stat_strength_red);
2180 HOOK(hook_dead_node_elim, stat_dead_node_elim);
2181 HOOK(hook_if_conversion, stat_if_conversion);
2182 HOOK(hook_func_call, stat_func_call);
2183 HOOK(hook_arch_dep_replace_mul_with_shifts, stat_arch_dep_replace_mul_with_shifts);
2184 HOOK(hook_arch_dep_replace_division_by_const, stat_arch_dep_replace_division_by_const);
2186 obstack_init(&status->cnts);
2187 obstack_init(&status->be_data);
2189 /* create the hash-tables */
2190 status->irg_hash = new_pset(graph_cmp, 8);
2191 status->ir_op_hash = new_pset(opcode_cmp_2, 1);
2193 /* create the wait queue */
2194 status->wait_q = new_pdeq();
2196 if (enable_options & FIRMSTAT_COUNT_STRONG_OP) {
2197 /* build the pseudo-ops */
2199 _op_Phi0.code = --num;
2200 _op_Phi0.name = new_id_from_chars(X("Phi0"));
2202 _op_PhiM.code = --num;
2203 _op_PhiM.name = new_id_from_chars(X("PhiM"));
2205 _op_ProjM.code = --num;
2206 _op_ProjM.name = new_id_from_chars(X("ProjM"));
2208 _op_MulC.code = --num;
2209 _op_MulC.name = new_id_from_chars(X("MulC"));
2211 _op_DivC.code = --num;
2212 _op_DivC.name = new_id_from_chars(X("DivC"));
2214 _op_ModC.code = --num;
2215 _op_ModC.name = new_id_from_chars(X("ModC"));
2217 _op_DivModC.code = --num;
2218 _op_DivModC.name = new_id_from_chars(X("DivModC"));
2220 status->op_Phi0 = &_op_Phi0;
2221 status->op_PhiM = &_op_PhiM;
2222 status->op_ProjM = &_op_ProjM;
2223 status->op_MulC = &_op_MulC;
2224 status->op_DivC = &_op_DivC;
2225 status->op_ModC = &_op_ModC;
2226 status->op_DivModC = &_op_DivModC;
2228 status->op_Phi0 = NULL;
2229 status->op_PhiM = NULL;
2230 status->op_ProjM = NULL;
2231 status->op_MulC = NULL;
2232 status->op_DivC = NULL;
2233 status->op_ModC = NULL;
2234 status->op_DivModC = NULL;
2237 /* for Florian: count the Sel depth */
2238 if (enable_options & FIRMSTAT_COUNT_SELS) {
2239 _op_SelSel.code = --num;
2240 _op_SelSel.name = new_id_from_chars(X("Sel(Sel)"));
2242 _op_SelSelSel.code = --num;
2243 _op_SelSelSel.name = new_id_from_chars(X("Sel(Sel(Sel))"));
2245 status->op_SelSel = &_op_SelSel;
2246 status->op_SelSelSel = &_op_SelSelSel;
2248 status->op_SelSel = NULL;
2249 status->op_SelSelSel = NULL;
2252 /* register the dumper */
2253 stat_register_dumper(&simple_dumper);
2255 if (enable_options & FIRMSTAT_CSV_OUTPUT)
2256 stat_register_dumper(&csv_dumper);
2258 /* initialize the pattern hash */
2259 stat_init_pattern_history(enable_options & FIRMSTAT_PATTERN_ENABLED);
2261 /* initialize the Const options */
2262 if (enable_options & FIRMSTAT_COUNT_CONSTS)
2263 stat_init_const_cnt(status);
2265 /* distribution table for parameter counts */
2266 status->dist_param_cnt = stat_new_int_distrib_tbl();
2268 clear_optimization_counter();
2272 } /* firm_init_stat */
2275 * Frees all dumper structures.
2277 static void stat_term_dumper(void) {
2278 dumper_t *dumper, *next_dumper;
2280 for (dumper = status->dumper; dumper; /* iteration done in loop body */ ) {
2281 if (dumper->func_map)
2282 del_pset(dumper->func_map);
2284 next_dumper = dumper->next;
2286 dumper = next_dumper;
2288 } /* stat_term_dumper */
2291 /* Terminates the statistics module, frees all memory. */
2292 void stat_term(void) {
2293 if (status != (stat_info_t *)&status_disable) {
2294 obstack_free(&status->be_data, NULL);
2295 obstack_free(&status->cnts, NULL);
2300 status = (stat_info_t *)&status_disable;
2304 /* returns 1 if statistics were initialized, 0 otherwise */
2305 int stat_is_active(void) {
2306 return status != (stat_info_t *)&status_disable;
2307 } /* stat_is_active */
2311 /* initialize the statistics module. */
2312 void firm_init_stat(unsigned enable_options) {}
2314 /* Dumps a statistics snapshot */
2315 void stat_dump_snapshot(const char *name, const char *phase) {}
2317 /* terminates the statistics module, frees all memory */
2318 void stat_term(void);
2320 #endif /* FIRM_STATISTICS */