3 * File name: ir/ir/firmstat.c
4 * Purpose: Statistics for Firm.
8 * Copyright: (c) 2004 Universität Karlsruhe
9 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
16 #ifdef FIRM_STATISTICS
30 #include "firmstat_t.h"
38 * need this to be static:
39 * Special pseudo Opcodes that we need to count some interesting cases
43 * The Phi0, a node that is created during SSA construction
45 static ir_op _op_Phi0;
47 /** The PhiM, just to count memory Phi's. */
48 static ir_op _op_PhiM;
50 /** The Mul by Const node. */
51 static ir_op _op_MulC;
53 /** The Div by Const node. */
54 static ir_op _op_DivC;
56 /** The Div by Const node. */
57 static ir_op _op_ModC;
59 /** The Div by Const node. */
60 static ir_op _op_DivModC;
62 /** The memory Proj node. */
63 static ir_op _op_ProjM;
66 static ir_op _op_SelSel;
68 /** A Sel of a Sel of a Sel */
69 static ir_op _op_SelSelSel;
71 /* ---------------------------------------------------------------------------------- */
73 /** Marks the begin of a statistic (hook) function. */
74 #define STAT_ENTER ++status->recursive
76 /** Marks the end of a statistic (hook) functions. */
77 #define STAT_LEAVE --status->recursive
79 /** Allows to enter a statistic function only when we are not already in a hook. */
80 #define STAT_ENTER_SINGLE do { if (status->recursive > 0) return; ++status->recursive; } while (0)
85 static const int status_disable = 0;
86 static stat_info_t *status = (stat_info_t *)&status_disable;
89 * Compare two elements of the opcode hash.
91 static int opcode_cmp(const void *elt, const void *key) {
92 const node_entry_t *e1 = elt;
93 const node_entry_t *e2 = key;
95 return e1->op->code - e2->op->code;
99 * Compare two elements of the graph hash.
101 static int graph_cmp(const void *elt, const void *key) {
102 const graph_entry_t *e1 = elt;
103 const graph_entry_t *e2 = key;
105 return e1->irg != e2->irg;
109 * Compare two elements of the optimization hash.
111 static int opt_cmp(const void *elt, const void *key) {
112 const opt_entry_t *e1 = elt;
113 const opt_entry_t *e2 = key;
115 return e1->op->code != e2->op->code;
119 * Compare two elements of the block/extbb hash.
121 static int block_cmp(const void *elt, const void *key) {
122 const block_entry_t *e1 = elt;
123 const block_entry_t *e2 = key;
125 return e1->block_nr != e2->block_nr;
129 * Compare two elements of the be_block hash.
131 static int be_block_cmp(const void *elt, const void *key) {
132 const be_block_entry_t *e1 = elt;
133 const be_block_entry_t *e2 = key;
135 return e1->block_nr != e2->block_nr;
139 * Compare two elements of reg pressure hash.
141 static int reg_pressure_cmp(const void *elt, const void *key) {
142 const reg_pressure_entry_t *e1 = elt;
143 const reg_pressure_entry_t *e2 = key;
145 return e1->class_name != e2->class_name;
146 } /* reg_pressure_cmp */
149 * Compare two elements of the perm_stat hash.
151 static int perm_stat_cmp(const void *elt, const void *key) {
152 const perm_stat_entry_t *e1 = elt;
153 const perm_stat_entry_t *e2 = key;
155 return e1->perm != e2->perm;
156 } /* perm_stat_cmp */
159 * Compare two elements of the perm_class hash.
161 static int perm_class_cmp(const void *elt, const void *key) {
162 const perm_class_entry_t *e1 = elt;
163 const perm_class_entry_t *e2 = key;
165 return e1->class_name != e2->class_name;
166 } /* perm_class_cmp */
169 * Compare two elements of the ir_op hash.
171 static int opcode_cmp_2(const void *elt, const void *key) {
172 const ir_op *e1 = elt;
173 const ir_op *e2 = key;
175 return e1->code != e2->code;
179 * Compare two elements of the address_mark set.
181 static int address_mark_cmp(const void *elt, const void *key, size_t size) {
182 const address_mark_entry_t *e1 = elt;
183 const address_mark_entry_t *e2 = key;
185 /* compare only the nodes, the rest is used as data container */
186 return e1->node != e2->node;
187 } /* address_mark_cmp */
190 * Clear all counter in a node_entry_t.
192 static void opcode_clear_entry(node_entry_t *elem) {
193 cnt_clr(&elem->cnt_alive);
194 cnt_clr(&elem->new_node);
195 cnt_clr(&elem->into_Id);
196 } /* opcode_clear_entry */
199 * Returns the associates node_entry_t for an ir_op
201 * @param op the IR operation
202 * @param hmap a hash map containing ir_op* -> node_entry_t*
204 static node_entry_t *opcode_get_entry(const ir_op *op, hmap_node_entry_t *hmap) {
210 elem = pset_find(hmap, &key, op->code);
214 elem = obstack_alloc(&status->cnts, sizeof(*elem));
215 memset(elem, 0, sizeof(*elem));
218 opcode_clear_entry(elem);
222 return pset_insert(hmap, elem, op->code);
223 } /* opcode_get_entry */
226 * Returns the associates ir_op for an opcode
228 * @param code the IR opcode
229 * @param hmap the hash map containing opcode -> ir_op*
231 static ir_op *opcode_find_entry(ir_opcode code, hmap_ir_op *hmap) {
235 return pset_find(hmap, &key, code);
236 } /* opcode_find_entry */
239 * Clears all counter in a graph_entry_t.
241 * @param elem the graph entry
242 * @param all if non-zero, clears all counters, else leave accumulated ones
244 static void graph_clear_entry(graph_entry_t *elem, int all) {
247 /* clear accumulated / non-acumulated counter */
248 for (i = all ? 0 : _gcnt_non_acc; i < _gcnt_last; ++i) {
249 cnt_clr(&elem->cnt[i]);
252 if (elem->block_hash) {
253 del_pset(elem->block_hash);
254 elem->block_hash = NULL;
257 if (elem->extbb_hash) {
258 del_pset(elem->extbb_hash);
259 elem->extbb_hash = NULL;
262 obstack_free(&elem->recalc_cnts, NULL);
263 obstack_init(&elem->recalc_cnts);
264 } /* graph_clear_entry */
267 * Returns the associated graph_entry_t for an IR graph.
269 * @param irg the IR graph
270 * @param hmap the hash map containing ir_graph* -> graph_entry_t*
272 static graph_entry_t *graph_get_entry(ir_graph *irg, hmap_graph_entry_t *hmap)
280 elem = pset_find(hmap, &key, HASH_PTR(irg));
283 /* create hash map backend block information */
284 if (! elem->be_block_hash)
285 elem->be_block_hash = new_pset(be_block_cmp, 5);
290 /* allocate a new one */
291 elem = obstack_alloc(&status->cnts, sizeof(*elem));
292 memset(elem, 0, sizeof(*elem));
293 obstack_init(&elem->recalc_cnts);
296 graph_clear_entry(elem, 1);
298 /* new hash table for opcodes here */
299 elem->opcode_hash = new_pset(opcode_cmp, 5);
300 elem->address_mark = new_set(address_mark_cmp, 5);
303 /* these hash tables are created on demand */
304 elem->block_hash = NULL;
305 elem->extbb_hash = NULL;
307 for (i = 0; i < sizeof(elem->opt_hash)/sizeof(elem->opt_hash[0]); ++i)
308 elem->opt_hash[i] = new_pset(opt_cmp, 4);
310 return pset_insert(hmap, elem, HASH_PTR(irg));
311 } /* graph_get_entry */
314 * Clear all counter in an opt_entry_t.
316 static void opt_clear_entry(opt_entry_t *elem) {
317 cnt_clr(&elem->count);
318 } /* opt_clear_entry */
321 * Returns the associated opt_entry_t for an IR operation.
323 * @param op the IR operation
324 * @param hmap the hash map containing ir_op* -> opt_entry_t*
326 static opt_entry_t *opt_get_entry(const ir_op *op, hmap_opt_entry_t *hmap)
333 elem = pset_find(hmap, &key, op->code);
337 elem = obstack_alloc(&status->cnts, sizeof(*elem));
338 memset(elem, 0, sizeof(*elem));
340 /* clear new counter */
341 opt_clear_entry(elem);
345 return pset_insert(hmap, elem, op->code);
346 } /* opt_get_entry */
349 * clears all counter in a block_entry_t
351 static void block_clear_entry(block_entry_t *elem) {
354 for (i = 0; i < _bcnt_last; ++i)
355 cnt_clr(&elem->cnt[i]);
356 } /* block_clear_entry */
359 * Returns the associated block_entry_t for an block.
361 * @param block_nr an IR block number
362 * @param hmap a hash map containing long -> block_entry_t
364 static block_entry_t *block_get_entry(struct obstack *obst, long block_nr, hmap_block_entry_t *hmap)
369 key.block_nr = block_nr;
371 elem = pset_find(hmap, &key, block_nr);
375 elem = obstack_alloc(obst, sizeof(*elem));
376 memset(elem, 0, sizeof(*elem));
378 /* clear new counter */
379 block_clear_entry(elem);
381 elem->block_nr = block_nr;
383 return pset_insert(hmap, elem, block_nr);
384 } /* block_get_entry */
387 * Clear all sets in be_block_entry_t.
389 static void be_block_clear_entry(be_block_entry_t *elem)
391 if (elem->reg_pressure)
392 del_pset(elem->reg_pressure);
394 if (elem->sched_ready)
395 stat_delete_distrib_tbl(elem->sched_ready);
397 if (elem->perm_class_stat)
398 del_pset(elem->perm_class_stat);
400 elem->reg_pressure = new_pset(reg_pressure_cmp, 5);
401 elem->sched_ready = stat_new_int_distrib_tbl();
402 elem->perm_class_stat = new_pset(perm_class_cmp, 5);
403 } /* be_block_clear_entry */
406 * Returns the associated be_block_entry_t for an block.
408 * @param block_nr an IR block number
409 * @param hmap a hash map containing long -> be_block_entry_t
411 static be_block_entry_t *be_block_get_entry(struct obstack *obst, long block_nr, hmap_be_block_entry_t *hmap)
413 be_block_entry_t key;
414 be_block_entry_t *elem;
416 key.block_nr = block_nr;
418 elem = pset_find(hmap, &key, block_nr);
422 elem = obstack_alloc(obst, sizeof(*elem));
423 memset(elem, 0, sizeof(*elem));
425 /* clear new counter */
426 be_block_clear_entry(elem);
428 elem->block_nr = block_nr;
430 return pset_insert(hmap, elem, block_nr);
431 } /* be_block_get_entry */
434 * clears all sets in perm_class_entry_t
436 static void perm_class_clear_entry(perm_class_entry_t *elem) {
438 del_pset(elem->perm_stat);
440 elem->perm_stat = new_pset(perm_stat_cmp, 5);
441 } /* perm_class_clear_entry */
444 * Returns the associated perm_class entry for a register class.
446 * @param class_name the register class name
447 * @param hmap a hash map containing class_name -> perm_class_entry_t
449 static perm_class_entry_t *perm_class_get_entry(struct obstack *obst, const char *class_name,
450 hmap_perm_class_entry_t *hmap)
452 perm_class_entry_t key;
453 perm_class_entry_t *elem;
455 key.class_name = class_name;
457 elem = pset_find(hmap, &key, HASH_PTR(class_name));
461 elem = obstack_alloc(obst, sizeof(*elem));
462 memset(elem, 0, sizeof(*elem));
464 /* clear new counter */
465 perm_class_clear_entry(elem);
467 elem->class_name = class_name;
469 return pset_insert(hmap, elem, HASH_PTR(class_name));
470 } /* perm_class_get_entry */
473 * clears all sets in perm_stat_entry_t
475 static void perm_stat_clear_entry(perm_stat_entry_t *elem) {
477 stat_delete_distrib_tbl(elem->chains);
480 stat_delete_distrib_tbl(elem->cycles);
482 elem->chains = stat_new_int_distrib_tbl();
483 elem->cycles = stat_new_int_distrib_tbl();
484 } /* perm_stat_clear_entry */
487 * Returns the associated perm_stat entry for a perm.
489 * @param perm the perm node
490 * @param hmap a hash map containing perm -> perm_stat_entry_t
492 static perm_stat_entry_t *perm_stat_get_entry(struct obstack *obst, ir_node *perm, hmap_perm_stat_entry_t *hmap)
494 perm_stat_entry_t key;
495 perm_stat_entry_t *elem;
499 elem = pset_find(hmap, &key, HASH_PTR(perm));
503 elem = obstack_alloc(obst, sizeof(*elem));
504 memset(elem, 0, sizeof(*elem));
506 /* clear new counter */
507 perm_stat_clear_entry(elem);
511 return pset_insert(hmap, elem, HASH_PTR(perm));
512 } /* perm_stat_get_entry */
515 * Returns the ir_op for an IR-node,
516 * handles special cases and return pseudo op codes.
518 * @param none an IR node
520 static ir_op *stat_get_irn_op(ir_node *node)
522 ir_op *op = get_irn_op(node);
523 ir_opcode opc = op->code;
527 if (get_irn_arity(node) == 0) {
528 /* special case, a Phi0 node, count on extra counter */
529 op = status->op_Phi0 ? status->op_Phi0 : op;
530 } else if (get_irn_mode(node) == mode_M) {
531 /* special case, a Memory Phi node, count on extra counter */
532 op = status->op_PhiM ? status->op_PhiM : op;
536 if (get_irn_mode(node) == mode_M) {
537 /* special case, a Memory Proj node, count on extra counter */
538 op = status->op_ProjM ? status->op_ProjM : op;
542 if (get_irn_op(get_Mul_left(node)) == op_Const || get_irn_op(get_Mul_right(node)) == op_Const) {
543 /* special case, a Multiply by a const, count on extra counter */
544 op = status->op_MulC ? status->op_MulC : op;
548 if (get_irn_op(get_Div_right(node)) == op_Const) {
549 /* special case, a division by a const, count on extra counter */
550 op = status->op_DivC ? status->op_DivC : op;
554 if (get_irn_op(get_Mod_right(node)) == op_Const) {
555 /* special case, a module by a const, count on extra counter */
556 op = status->op_ModC ? status->op_ModC : op;
560 if (get_irn_op(get_DivMod_right(node)) == op_Const) {
561 /* special case, a division/modulo by a const, count on extra counter */
562 op = status->op_DivModC ? status->op_DivModC : op;
566 if (get_irn_op(get_Sel_ptr(node)) == op_Sel) {
567 /* special case, a Sel of a Sel, count on extra counter */
568 op = status->op_SelSel ? status->op_SelSel : op;
569 if (get_irn_op(get_Sel_ptr(get_Sel_ptr(node))) == op_Sel) {
570 /* special case, a Sel of a Sel of a Sel, count on extra counter */
571 op = status->op_SelSelSel ? status->op_SelSelSel : op;
580 } /* stat_get_irn_op */
583 * update the block counter
585 static void undate_block_info(ir_node *node, graph_entry_t *graph)
587 ir_op *op = get_irn_op(node);
589 block_entry_t *b_entry;
592 /* check for block */
593 if (op == op_Block) {
594 arity = get_irn_arity(node);
595 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(node), graph->block_hash);
597 /* count all incoming edges */
598 for (i = 0; i < arity; ++i) {
599 ir_node *pred = get_irn_n(node, i);
600 ir_node *other_block = get_nodes_block(pred);
601 block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
603 cnt_inc(&b_entry->cnt[bcnt_in_edges]); /* an edge coming from another block */
604 cnt_inc(&b_entry_other->cnt[bcnt_out_edges]);
609 block = get_nodes_block(node);
610 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(block), graph->block_hash);
612 if (op == op_Phi && mode_is_datab(get_irn_mode(node))) {
613 /* count data Phi per block */
614 cnt_inc(&b_entry->cnt[bcnt_phi_data]);
617 /* we have a new node in our block */
618 cnt_inc(&b_entry->cnt[bcnt_nodes]);
620 /* don't count keep-alive edges */
621 if (get_irn_op(node) == op_End)
624 arity = get_irn_arity(node);
626 for (i = 0; i < arity; ++i) {
627 ir_node *pred = get_irn_n(node, i);
628 ir_node *other_block;
630 other_block = get_nodes_block(pred);
632 if (other_block == block)
633 cnt_inc(&b_entry->cnt[bcnt_edges]); /* a in block edge */
635 block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
637 cnt_inc(&b_entry->cnt[bcnt_in_edges]); /* an edge coming from another block */
638 cnt_inc(&b_entry_other->cnt[bcnt_out_edges]);
641 } /* undate_block_info */
644 * Update the extended block counter.
646 static void update_extbb_info(ir_node *node, graph_entry_t *graph)
648 ir_op *op = get_irn_op(node);
650 extbb_entry_t *eb_entry;
653 /* check for block */
654 if (op == op_Block) {
655 extbb = get_nodes_extbb(node);
656 arity = get_irn_arity(node);
657 eb_entry = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(extbb), graph->extbb_hash);
659 /* count all incoming edges */
660 for (i = 0; i < arity; ++i) {
661 ir_node *pred = get_irn_n(node, i);
662 ir_extblk *other_extbb = get_nodes_extbb(pred);
664 if (extbb != other_extbb) {
665 extbb_entry_t *eb_entry_other = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(other_extbb), graph->extbb_hash);
667 cnt_inc(&eb_entry->cnt[bcnt_in_edges]); /* an edge coming from another extbb */
668 cnt_inc(&eb_entry_other->cnt[bcnt_out_edges]);
674 extbb = get_nodes_extbb(node);
675 eb_entry = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(extbb), graph->extbb_hash);
677 if (op == op_Phi && mode_is_datab(get_irn_mode(node))) {
678 /* count data Phi per extbb */
679 cnt_inc(&eb_entry->cnt[bcnt_phi_data]);
682 /* we have a new node in our block */
683 cnt_inc(&eb_entry->cnt[bcnt_nodes]);
685 /* don't count keep-alive edges */
686 if (get_irn_op(node) == op_End)
689 arity = get_irn_arity(node);
691 for (i = 0; i < arity; ++i) {
692 ir_node *pred = get_irn_n(node, i);
693 ir_extblk *other_extbb = get_nodes_extbb(pred);
695 if (other_extbb == extbb)
696 cnt_inc(&eb_entry->cnt[bcnt_edges]); /* a in extbb edge */
698 extbb_entry_t *eb_entry_other = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(other_extbb), graph->extbb_hash);
700 cnt_inc(&eb_entry->cnt[bcnt_in_edges]); /* an edge coming from another extbb */
701 cnt_inc(&eb_entry_other->cnt[bcnt_out_edges]);
704 } /* update_extbb_info */
707 * Calculates how many arguments of the call are const, updates
708 * param distribution.
710 static void analyse_params_of_Call(graph_entry_t *graph, ir_node *call) {
711 int i, num_const_args = 0, num_local_adr = 0;
712 int n = get_Call_n_params(call);
714 for (i = 0; i < n; ++i) {
715 ir_node *param = get_Call_param(call, i);
717 if (is_irn_constlike(param))
719 else if (is_Sel(param)) {
720 ir_node *base = param;
723 base = get_Sel_ptr(base);
724 } while (is_Sel(base));
726 if (base == get_irg_frame(current_ir_graph))
732 if (num_const_args > 0)
733 cnt_inc(&graph->cnt[gcnt_call_with_cnst_arg]);
734 if (num_const_args == n)
735 cnt_inc(&graph->cnt[gcnt_call_with_all_cnst_arg]);
736 if (num_local_adr > 0)
737 cnt_inc(&graph->cnt[gcnt_call_with_local_adr]);
739 stat_inc_int_distrib_tbl(status->dist_param_cnt, n);
740 } /* analyse_params_of_Call */
743 * Update info on calls.
745 * @param call The call
746 * @param graph The graph entry containing the call
748 static void stat_update_call(ir_node *call, graph_entry_t *graph)
750 ir_node *block = get_nodes_block(call);
751 ir_node *ptr = get_Call_ptr(call);
752 ir_entity *ent = NULL;
753 ir_graph *callee = NULL;
756 * If the block is bad, the whole subgraph will collapse later
757 * so do not count this call.
758 * This happens in dead code.
763 cnt_inc(&graph->cnt[gcnt_all_calls]);
765 /* found a call, this function is not a leaf */
768 if (get_irn_op(ptr) == op_SymConst) {
769 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
770 /* ok, we seems to know the entity */
771 ent = get_SymConst_entity(ptr);
772 callee = get_entity_irg(ent);
774 /* it is recursive, if it calls at least once */
775 if (callee == graph->irg)
776 graph->is_recursive = 1;
779 /* indirect call, be could not predict */
780 cnt_inc(&graph->cnt[gcnt_indirect_calls]);
782 /* NOT a leaf call */
783 graph->is_leaf_call = LCS_NON_LEAF_CALL;
786 /* check, if it's a chain-call: Then, the call-block
787 * must dominate the end block. */
789 ir_node *curr = get_irg_end_block(graph->irg);
790 int depth = get_Block_dom_depth(block);
792 for (; curr != block && get_Block_dom_depth(curr) > depth;) {
793 curr = get_Block_idom(curr);
795 if (! curr || is_no_Block(curr))
800 graph->is_chain_call = 0;
803 /* check, if the callee is a leaf */
805 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
807 if (called->is_analyzed) {
808 if (! called->is_leaf)
809 graph->is_leaf_call = LCS_NON_LEAF_CALL;
813 analyse_params_of_Call(graph, call);
814 } /* stat_update_call */
817 * Update info on calls for graphs on the wait queue.
819 static void stat_update_call_2(ir_node *call, graph_entry_t *graph)
821 ir_node *block = get_nodes_block(call);
822 ir_node *ptr = get_Call_ptr(call);
823 ir_entity *ent = NULL;
824 ir_graph *callee = NULL;
827 * If the block is bad, the whole subgraph will collapse later
828 * so do not count this call.
829 * This happens in dead code.
834 if (get_irn_op(ptr) == op_SymConst) {
835 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
836 /* ok, we seems to know the entity */
837 ent = get_SymConst_entity(ptr);
838 callee = get_entity_irg(ent);
842 /* check, if the callee is a leaf */
844 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
846 assert(called->is_analyzed);
848 if (! called->is_leaf)
849 graph->is_leaf_call = LCS_NON_LEAF_CALL;
851 graph->is_leaf_call = LCS_NON_LEAF_CALL;
852 } /* stat_update_call_2 */
855 * Find the base address and entity of an Sel node.
857 * @param sel the node
859 * @return the base address.
861 static ir_node *find_base_adr(ir_node *sel) {
862 ir_node *ptr = get_Sel_ptr(sel);
864 while (is_Sel(ptr)) {
866 ptr = get_Sel_ptr(sel);
869 } /* find_base_adr */
872 * Update info on Load/Store address statistics.
874 static void stat_update_address(ir_node *node, graph_entry_t *graph) {
875 ir_opcode opc = get_irn_opcode(node);
881 /* a global address */
882 cnt_inc(&graph->cnt[gcnt_global_adr]);
885 base = find_base_adr(node);
886 irg = current_ir_graph;
887 if (base == get_irg_tls(irg)) {
888 /* a TLS variable, like a global. */
889 cnt_inc(&graph->cnt[gcnt_global_adr]);
890 } else if (base == get_irg_frame(irg)) {
891 /* a local Variable. */
892 cnt_inc(&graph->cnt[gcnt_local_adr]);
895 if (is_Proj(base) && skip_Proj(get_Proj_pred(base)) == get_irg_start(irg)) {
896 /* pointer access through parameter, check for THIS */
897 ir_entity *ent = get_irg_entity(irg);
900 ir_type *ent_tp = get_entity_type(ent);
902 if (get_method_calling_convention(ent_tp) & cc_this_call) {
903 if (get_Proj_proj(base) == 0) {
905 cnt_inc(&graph->cnt[gcnt_this_adr]);
910 /* other parameter */
911 cnt_inc(&graph->cnt[gcnt_param_adr]);
914 /* unknown Pointer access */
915 cnt_inc(&graph->cnt[gcnt_other_adr]);
921 } /* stat_update_address */
924 * Walker for reachable nodes count.
926 static void update_node_stat(ir_node *node, void *env)
928 graph_entry_t *graph = env;
931 ir_op *op = stat_get_irn_op(node);
932 int arity = get_irn_arity(node);
934 entry = opcode_get_entry(op, graph->opcode_hash);
936 cnt_inc(&entry->cnt_alive);
937 cnt_add_i(&graph->cnt[gcnt_edges], arity);
939 /* count block edges */
940 undate_block_info(node, graph);
942 /* count extended block edges */
943 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
944 if (graph->irg != get_const_code_irg())
945 update_extbb_info(node, graph);
948 /* handle statistics for special node types */
952 if (status->stat_options & FIRMSTAT_COUNT_CONSTS) {
953 /* check properties of constants */
954 stat_update_const(status, node, graph);
958 /* check for properties that depends on calls like recursion/leaf/indirect call */
959 stat_update_call(node, graph);
962 /* check address properties */
963 stat_update_address(get_Load_ptr(node), graph);
966 /* check address properties */
967 stat_update_address(get_Store_ptr(node), graph);
972 } /* update_node_stat */
975 * Walker for reachable nodes count for graphs on the wait_q.
977 static void update_node_stat_2(ir_node *node, void *env) {
978 graph_entry_t *graph = env;
980 /* check for properties that depends on calls like recursion/leaf/indirect call */
982 stat_update_call_2(node, graph);
983 } /* update_node_stat_2 */
986 * Get the current address mark.
988 static unsigned get_adr_mark(graph_entry_t *graph, ir_node *node) {
989 address_mark_entry_t *value = set_find(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
991 return value ? value->mark : 0;
995 * Set the current address mark.
997 static void set_adr_mark(graph_entry_t *graph, ir_node *node, unsigned val) {
998 address_mark_entry_t *value = set_insert(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
1001 } /* set_adr_mark */
1003 #undef DUMP_ADR_MODE
1005 #ifdef DUMP_ADR_MODE
1007 * a vcg attribute hook: Color a node with a different color if
1008 * it's identified as a part of an address expression or at least referenced
1009 * by an address expression.
1011 static int stat_adr_mark_hook(FILE *F, ir_node *node, ir_node *local)
1013 ir_node *n = local ? local : node;
1014 ir_graph *irg = get_irn_irg(n);
1015 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1016 unsigned mark = get_adr_mark(graph, n);
1018 if (mark & MARK_ADDRESS_CALC)
1019 fprintf(F, "color: purple");
1020 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1021 fprintf(F, "color: pink");
1022 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1023 fprintf(F, "color: lightblue");
1027 /* I know the color! */
1029 } /* stat_adr_mark_hook */
1030 #endif /* DUMP_ADR_MODE */
1033 * Return the "operational" mode of a Firm node.
1035 static ir_mode *get_irn_op_mode(ir_node *node) {
1036 switch (get_irn_opcode(node)) {
1038 return get_Load_mode(node);
1040 return get_irn_mode(get_Store_value(node));
1042 return get_irn_mode(get_DivMod_left(node));
1044 return get_irn_mode(get_Div_left(node));
1046 return get_irn_mode(get_Mod_left(node));
1048 /* Cmp is no address calculation, or is it? */
1050 return get_irn_mode(node);
1052 } /* get_irn_op_mode */
1055 * Post-walker that marks every node that is an address calculation.
1057 * Users of a node must be visited first. We ensure this by
1058 * calling it in the post of an outs walk. This should work even in cycles,
1059 * while the normal pre-walk will not.
1061 static void mark_address_calc(ir_node *node, void *env) {
1062 graph_entry_t *graph = env;
1063 ir_mode *mode = get_irn_op_mode(node);
1065 unsigned mark_preds = MARK_REF_NON_ADR;
1067 if (! mode_is_numP(mode))
1070 if (mode_is_reference(mode)) {
1071 /* a reference is calculated here, we are sure */
1072 set_adr_mark(graph, node, MARK_ADDRESS_CALC);
1074 mark_preds = MARK_REF_ADR;
1076 unsigned mark = get_adr_mark(graph, node);
1078 if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR) {
1080 * this node has no reference mode, but is only
1081 * referenced by address calculations
1083 mark_preds = MARK_REF_ADR;
1087 /* mark all predecessors */
1088 for (i = 0, n = get_irn_arity(node); i < n; ++i) {
1089 ir_node *pred = get_irn_n(node, i);
1091 mode = get_irn_op_mode(pred);
1092 if (! mode_is_numP(mode))
1095 set_adr_mark(graph, pred, get_adr_mark(graph, pred) | mark_preds);
1097 } /* mark_address_calc */
1100 * Post-walker that marks every node that is an address calculation.
1102 * Users of a node must be visited first. We ensure this by
1103 * calling it in the post of an outs walk. This should work even in cycles,
1104 * while the normal pre-walk will not.
1106 static void count_adr_ops(ir_node *node, void *env) {
1107 graph_entry_t *graph = env;
1108 unsigned mark = get_adr_mark(graph, node);
1110 if (mark & MARK_ADDRESS_CALC)
1111 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1112 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1113 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1114 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1115 cnt_inc(&graph->cnt[gcnt_all_adr_ops]);
1116 } /* count_adr_ops */
1119 * Called for every graph when the graph is either deleted or stat_dump_snapshot()
1120 * is called, must recalculate all statistic info.
1122 * @param global The global entry
1123 * @param graph The current entry
1125 static void update_graph_stat(graph_entry_t *global, graph_entry_t *graph)
1127 node_entry_t *entry;
1130 /* clear first the alive counter in the graph */
1131 foreach_pset(graph->opcode_hash, entry) {
1132 cnt_clr(&entry->cnt_alive);
1133 } /* foreach_pset */
1135 /* set pessimistic values */
1137 graph->is_leaf_call = LCS_UNKNOWN;
1138 graph->is_recursive = 0;
1139 graph->is_chain_call = 1;
1141 /* create new block counter */
1142 graph->block_hash = new_pset(block_cmp, 5);
1144 /* we need dominator info */
1145 if (graph->irg != get_const_code_irg()) {
1146 assure_doms(graph->irg);
1148 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
1149 /* we need extended basic blocks */
1150 compute_extbb(graph->irg);
1152 /* create new extbb counter */
1153 graph->extbb_hash = new_pset(block_cmp, 5);
1157 /* count the nodes in the graph */
1158 irg_walk_graph(graph->irg, update_node_stat, NULL, graph);
1161 /* Uncomment this code if chain-call means call exact one. */
1162 entry = opcode_get_entry(op_Call, graph->opcode_hash);
1164 /* check if we have more than 1 call */
1165 if (cnt_gt(entry->cnt_alive, 1))
1166 graph->is_chain_call = 0;
1169 /* recursive functions are never chain calls, leafs don't have calls */
1170 if (graph->is_recursive || graph->is_leaf)
1171 graph->is_chain_call = 0;
1173 /* assume we walk every graph only ONCE, we could sum here the global count */
1174 foreach_pset(graph->opcode_hash, entry) {
1175 node_entry_t *g_entry = opcode_get_entry(entry->op, global->opcode_hash);
1177 /* update the node counter */
1178 cnt_add(&g_entry->cnt_alive, &entry->cnt_alive);
1179 } /* foreach_pset */
1181 /* count the number of address calculation */
1182 if (graph->irg != get_const_code_irg()) {
1183 ir_graph *rem = current_ir_graph;
1185 assure_irg_outs(graph->irg);
1187 /* Must be done an the outs graph */
1188 current_ir_graph = graph->irg;
1189 irg_out_walk(get_irg_start(graph->irg), NULL, mark_address_calc, graph);
1190 current_ir_graph = rem;
1192 #ifdef DUMP_ADR_MODE
1193 /* register the vcg hook and dump the graph for test */
1194 set_dump_node_vcgattr_hook(stat_adr_mark_hook);
1195 dump_ir_block_graph(graph->irg, "-adr");
1196 set_dump_node_vcgattr_hook(NULL);
1197 #endif /* DUMP_ADR_MODE */
1199 irg_walk_graph(graph->irg, NULL, count_adr_ops, graph);
1202 /* count the DAG's */
1203 if (status->stat_options & FIRMSTAT_COUNT_DAG)
1204 count_dags_in_graph(global, graph);
1206 /* calculate the patterns of this graph */
1207 stat_calc_pattern_history(graph->irg);
1209 /* leaf function did not call others */
1211 graph->is_leaf_call = LCS_NON_LEAF_CALL;
1212 else if (graph->is_leaf_call == LCS_UNKNOWN) {
1213 /* we still don't know if this graph calls leaf-functions, so enqueue */
1214 pdeq_putl(status->wait_q, graph);
1217 /* we have analyzed this graph */
1218 graph->is_analyzed = 1;
1220 /* accumulate all counter's */
1221 for (i = 0; i < _gcnt_last; ++i)
1222 cnt_add(&global->cnt[i], &graph->cnt[i]);
1223 } /* update_graph_stat */
1226 * Called for every graph that was on the wait_q in stat_dump_snapshot()
1227 * must finish all statistic info calculations.
1229 * @param global The global entry
1230 * @param graph The current entry
1232 static void update_graph_stat_2(graph_entry_t *global, graph_entry_t *graph)
1234 if (graph->is_deleted) {
1235 /* deleted, ignore */
1240 /* count the nodes in the graph */
1241 irg_walk_graph(graph->irg, update_node_stat_2, NULL, graph);
1243 if (graph->is_leaf_call == LCS_UNKNOWN)
1244 graph->is_leaf_call = LCS_LEAF_CALL;
1246 } /* update_graph_stat_2 */
1249 * Register a dumper.
1251 static void stat_register_dumper(const dumper_t *dumper) {
1252 dumper_t *p = xmalloc(sizeof(*p));
1255 memcpy(p, dumper, sizeof(*p));
1257 p->next = status->dumper;
1262 /* FIXME: memory leak */
1263 } /* stat_register_dumper */
1266 * Dumps the statistics of an IR graph.
1268 static void stat_dump_graph(graph_entry_t *entry) {
1271 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1272 if (dumper->dump_graph)
1273 dumper->dump_graph(dumper, entry);
1275 } /* stat_dump_graph */
1278 * Calls all registered dumper functions.
1280 static void stat_dump_registered(graph_entry_t *entry) {
1283 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1284 if (dumper->func_map) {
1285 dump_graph_FUNC func;
1287 foreach_pset(dumper->func_map, func)
1288 func(dumper, entry);
1291 } /* stat_dump_registered */
1294 * Dumps a constant table.
1296 static void stat_dump_consts(const constant_info_t *tbl) {
1299 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1300 if (dumper->dump_const_tbl)
1301 dumper->dump_const_tbl(dumper, tbl);
1303 } /* stat_dump_consts */
1306 * Dumps the parameter distribution
1308 static void stat_dump_param_tbl(const distrib_tbl_t *tbl, graph_entry_t *global) {
1311 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1312 if (dumper->dump_const_tbl)
1313 dumper->dump_param_tbl(dumper, tbl, global);
1318 * Initialize the dumper.
1320 static void stat_dump_init(const char *name) {
1323 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1325 dumper->init(dumper, name);
1327 } /* stat_dump_init */
1330 * Finish the dumper.
1332 static void stat_dump_finish(void) {
1335 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1337 dumper->finish(dumper);
1339 } /* stat_dump_finish */
1342 * Register an additional function for all dumper.
1344 void stat_register_dumper_func(dump_graph_FUNC func) {
1347 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1348 if (! dumper->func_map)
1349 dumper->func_map = pset_new_ptr(3);
1350 pset_insert_ptr(dumper->func_map, func);
1352 } /* stat_register_dumper_func */
1354 /* ---------------------------------------------------------------------- */
1357 * Helper: get an ir_op from an opcode.
1359 ir_op *stat_get_op_from_opcode(ir_opcode code) {
1360 return opcode_find_entry(code, status->ir_op_hash);
1361 } /* stat_get_op_from_opcode */
1364 * Hook: A new IR op is registered.
1366 * @param ctx the hook context
1367 * @param op the new IR opcode that was created.
1369 static void stat_new_ir_op(void *ctx, ir_op *op) {
1370 if (! status->stat_options)
1375 graph_entry_t *graph = graph_get_entry(NULL, status->irg_hash);
1377 /* execute for side effect :-) */
1378 (void)opcode_get_entry(op, graph->opcode_hash);
1380 pset_insert(status->ir_op_hash, op, op->code);
1383 } /* stat_new_ir_op */
1386 * Hook: An IR op is freed.
1388 * @param ctx the hook context
1389 * @param op the IR opcode that is freed
1391 static void stat_free_ir_op(void *ctx, ir_op *op) {
1392 if (! status->stat_options)
1399 } /* stat_free_ir_op */
1402 * Hook: A new node is created.
1404 * @param ctx the hook context
1405 * @param irg the IR graph on which the node is created
1406 * @param node the new IR node that was created
1408 static void stat_new_node(void *ctx, ir_graph *irg, ir_node *node) {
1409 if (! status->stat_options)
1412 /* do NOT count during dead node elimination */
1413 if (status->in_dead_node_elim)
1418 node_entry_t *entry;
1419 graph_entry_t *graph;
1420 ir_op *op = stat_get_irn_op(node);
1422 /* increase global value */
1423 graph = graph_get_entry(NULL, status->irg_hash);
1424 entry = opcode_get_entry(op, graph->opcode_hash);
1425 cnt_inc(&entry->new_node);
1427 /* increase local value */
1428 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1429 entry = opcode_get_entry(op, graph->opcode_hash);
1430 cnt_inc(&entry->new_node);
1433 } /* stat_new_node */
1436 * Hook: A node is changed into a Id node
1438 * @param ctx the hook context
1439 * @param node the IR node that will be turned into an ID
1441 static void stat_turn_into_id(void *ctx, ir_node *node) {
1442 if (! status->stat_options)
1447 node_entry_t *entry;
1448 graph_entry_t *graph;
1449 ir_op *op = stat_get_irn_op(node);
1451 /* increase global value */
1452 graph = graph_get_entry(NULL, status->irg_hash);
1453 entry = opcode_get_entry(op, graph->opcode_hash);
1454 cnt_inc(&entry->into_Id);
1456 /* increase local value */
1457 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1458 entry = opcode_get_entry(op, graph->opcode_hash);
1459 cnt_inc(&entry->into_Id);
1462 } /* stat_turn_into_id */
1465 * Hook: A new graph was created
1467 * @param ctx the hook context
1468 * @param irg the new IR graph that was created
1469 * @param ent the entity of this graph
1471 static void stat_new_graph(void *ctx, ir_graph *irg, ir_entity *ent) {
1472 if (! status->stat_options)
1477 /* execute for side effect :-) */
1478 graph_entry_t * graph = graph_get_entry(irg, status->irg_hash);
1481 graph->is_deleted = 0;
1483 graph->is_leaf_call = 0;
1484 graph->is_recursive = 0;
1485 graph->is_chain_call = 0;
1486 graph->is_analyzed = 0;
1489 } /* stat_new_graph */
1492 * Hook: A graph will be deleted
1494 * @param ctx the hook context
1495 * @param irg the IR graph that will be deleted
1497 * Note that we still hold the information for this graph
1498 * in our hash maps, only a flag is set which prevents this
1499 * information from being changed, it's "frozen" from now.
1501 static void stat_free_graph(void *ctx, ir_graph *irg) {
1502 if (! status->stat_options)
1507 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1508 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1510 graph->is_deleted = 1;
1512 if (status->stat_options & FIRMSTAT_COUNT_DELETED) {
1513 /* count the nodes of the graph yet, it will be destroyed later */
1514 update_graph_stat(global, graph);
1518 } /* stat_free_graph */
1521 * Hook: A walk over a graph is initiated. Do not count walks from statistic code.
1523 * @param ctx the hook context
1524 * @param irg the IR graph that will be walked
1525 * @param pre the pre walker
1526 * @param post the post walker
1528 static void stat_irg_walk(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1530 if (! status->stat_options)
1535 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1537 cnt_inc(&graph->cnt[gcnt_acc_walked]);
1540 } /* stat_irg_walk */
1543 * Hook: A walk over a graph in block-wise order is initiated. Do not count walks from statistic code.
1545 * @param ctx the hook context
1546 * @param irg the IR graph that will be walked
1547 * @param pre the pre walker
1548 * @param post the post walker
1550 static void stat_irg_walk_blkwise(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1552 /* for now, do NOT differentiate between blockwise and normal */
1553 stat_irg_walk(ctx, irg, pre, post);
1554 } /* stat_irg_walk_blkwise */
1557 * Hook: A walk over the graph's blocks is initiated. Do not count walks from statistic code.
1559 * @param ctx the hook context
1560 * @param irg the IR graph that will be walked
1561 * @param node the IR node
1562 * @param pre the pre walker
1563 * @param post the post walker
1565 static void stat_irg_block_walk(void *ctx, ir_graph *irg, ir_node *node, generic_func *pre, generic_func *post)
1567 if (! status->stat_options)
1572 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1574 cnt_inc(&graph->cnt[gcnt_acc_walked_blocks]);
1577 } /* stat_irg_block_walk */
1580 * Called for every node that is removed due to an optimization.
1582 * @param n the IR node that will be removed
1583 * @param hmap the hash map containing ir_op* -> opt_entry_t*
1585 static void removed_due_opt(ir_node *n, hmap_opt_entry_t *hmap) {
1586 ir_op *op = stat_get_irn_op(n);
1587 opt_entry_t *entry = opt_get_entry(op, hmap);
1589 /* increase global value */
1590 cnt_inc(&entry->count);
1591 } /* removed_due_opt */
1594 * Hook: Some nodes were optimized into some others due to an optimization.
1596 * @param ctx the hook context
1598 static void stat_merge_nodes(
1600 ir_node **new_node_array, int new_num_entries,
1601 ir_node **old_node_array, int old_num_entries,
1604 if (! status->stat_options)
1610 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1612 if (status->reassoc_run)
1613 opt = HOOK_OPT_REASSOC;
1615 for (i = 0; i < old_num_entries; ++i) {
1616 /* nodes might be in new and old, so if we found a node
1617 in both sets, this one is NOT removed */
1618 for (j = 0; j < new_num_entries; ++j) {
1619 if (old_node_array[i] == new_node_array[j])
1622 if (j >= new_num_entries) {
1625 /* sometimes we did not detect, that it is replaced by a Const */
1626 if (opt == HOOK_OPT_CONFIRM && new_num_entries == 1) {
1627 ir_op *op = get_irn_op(new_node_array[0]);
1629 if (op == op_Const || op == op_SymConst)
1630 xopt = HOOK_OPT_CONFIRM_C;
1633 removed_due_opt(old_node_array[i], graph->opt_hash[xopt]);
1638 } /* stat_merge_nodes */
1641 * Hook: Reassociation is started/stopped.
1643 * @param ctx the hook context
1644 * @param flag if non-zero, reassociation is started else stopped
1646 static void stat_reassociate(void *ctx, int flag) {
1647 if (! status->stat_options)
1652 status->reassoc_run = flag;
1655 } /* stat_reassociate */
1658 * Hook: A node was lowered into other nodes
1660 * @param ctx the hook context
1661 * @param node the IR node that will be lowered
1663 static void stat_lower(void *ctx, ir_node *node) {
1664 if (! status->stat_options)
1669 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1671 removed_due_opt(node, graph->opt_hash[HOOK_LOWERED]);
1677 * Hook: A graph was inlined.
1679 * @param ctx the hook context
1680 * @param call the IR call that will re changed into the body of
1681 * the called IR graph
1682 * @param called_irg the IR graph representing the called routine
1684 static void stat_inline(void *ctx, ir_node *call, ir_graph *called_irg)
1686 if (! status->stat_options)
1691 ir_graph *irg = get_irn_irg(call);
1692 graph_entry_t *i_graph = graph_get_entry(called_irg, status->irg_hash);
1693 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1695 cnt_inc(&graph->cnt[gcnt_acc_got_inlined]);
1696 cnt_inc(&i_graph->cnt[gcnt_acc_was_inlined]);
1702 * Hook: A graph with tail-recursions was optimized.
1704 * @param ctx the hook context
1706 static void stat_tail_rec(void *ctx, ir_graph *irg, int n_calls) {
1707 if (! status->stat_options)
1712 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1714 graph->num_tail_recursion += n_calls;
1717 } /* stat_tail_rec */
1720 * Strength reduction was performed on an iteration variable.
1722 * @param ctx the hook context
1724 static void stat_strength_red(void *ctx, ir_graph *irg, ir_node *strong) {
1725 if (! status->stat_options)
1730 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1731 cnt_inc(&graph->cnt[gcnt_acc_strength_red]);
1733 removed_due_opt(strong, graph->opt_hash[HOOK_OPT_STRENGTH_RED]);
1736 } /* stat_strength_red */
1739 * Hook: Start/Stop the dead node elimination.
1741 * @param ctx the hook context
1743 static void stat_dead_node_elim(void *ctx, ir_graph *irg, int start) {
1744 if (! status->stat_options)
1747 status->in_dead_node_elim = (start != 0);
1748 } /* stat_dead_node_elim */
1751 * Hook: if-conversion was tried.
1753 static void stat_if_conversion(void *context, ir_graph *irg, ir_node *phi,
1754 int pos, ir_node *mux, if_result_t reason)
1756 if (! status->stat_options)
1761 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1763 cnt_inc(&graph->cnt[gcnt_if_conv + reason]);
1766 } /* stat_if_conversion */
1769 * Hook: real function call was optimized.
1771 static void stat_func_call(void *context, ir_graph *irg, ir_node *call)
1773 if (! status->stat_options)
1778 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1780 cnt_inc(&graph->cnt[gcnt_acc_real_func_call]);
1783 } /* stat_func_call */
1786 * Hook: A multiply was replaced by a series of Shifts/Adds/Subs.
1788 * @param ctx the hook context
1790 static void stat_arch_dep_replace_mul_with_shifts(void *ctx, ir_node *mul) {
1791 if (! status->stat_options)
1796 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1797 removed_due_opt(mul, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1800 } /* stat_arch_dep_replace_mul_with_shifts */
1803 * Hook: A division by const was replaced.
1805 * @param ctx the hook context
1806 * @param node the division node that will be optimized
1808 static void stat_arch_dep_replace_division_by_const(void *ctx, ir_node *node) {
1809 if (! status->stat_options)
1814 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1815 removed_due_opt(node, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1818 } /* stat_arch_dep_replace_division_by_const */
1821 * Update the register pressure of a block.
1823 * @param irg the irg containing the block
1824 * @param block the block for which the reg pressure should be set
1825 * @param pressure the pressure
1826 * @param class_name the name of the register class
1828 void stat_be_block_regpressure(ir_graph *irg, ir_node *block, int pressure, const char *class_name)
1830 if (! status->stat_options)
1835 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1836 be_block_entry_t *block_ent;
1837 reg_pressure_entry_t *rp_ent;
1839 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1840 rp_ent = obstack_alloc(&status->be_data, sizeof(*rp_ent));
1841 memset(rp_ent, 0, sizeof(*rp_ent));
1843 rp_ent->class_name = class_name;
1844 rp_ent->pressure = pressure;
1846 pset_insert(block_ent->reg_pressure, rp_ent, HASH_PTR(class_name));
1849 } /* stat_be_block_regpressure */
1852 * Update the distribution of ready nodes of a block
1854 * @param irg the irg containing the block
1855 * @param block the block for which the reg pressure should be set
1856 * @param num_ready the number of ready nodes
1858 void stat_be_block_sched_ready(ir_graph *irg, ir_node *block, int num_ready)
1860 if (! status->stat_options)
1865 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1866 be_block_entry_t *block_ent;
1868 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1870 /* increase the counter of corresponding number of ready nodes */
1871 stat_inc_int_distrib_tbl(block_ent->sched_ready, num_ready);
1874 } /* stat_be_block_sched_ready */
1877 * Update the permutation statistic of a block.
1879 * @param class_name the name of the register class
1880 * @param n_regs number of registers in the register class
1881 * @param perm the perm node
1882 * @param block the block containing the perm
1883 * @param size the size of the perm
1884 * @param real_size number of pairs with different registers
1886 void stat_be_block_stat_perm(const char *class_name, int n_regs, ir_node *perm, ir_node *block,
1887 int size, int real_size)
1889 if (! status->stat_options)
1894 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
1895 be_block_entry_t *block_ent;
1896 perm_class_entry_t *pc_ent;
1897 perm_stat_entry_t *ps_ent;
1899 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1900 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
1901 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
1903 pc_ent->n_regs = n_regs;
1905 /* update information */
1906 ps_ent->size = size;
1907 ps_ent->real_size = real_size;
1910 } /* stat_be_block_stat_perm */
1913 * Update the permutation statistic of a single perm.
1915 * @param class_name the name of the register class
1916 * @param perm the perm node
1917 * @param block the block containing the perm
1918 * @param is_chain 1 if chain, 0 if cycle
1919 * @param size length of the cycle/chain
1920 * @param n_ops the number of ops representing this cycle/chain after lowering
1922 void stat_be_block_stat_permcycle(const char *class_name, ir_node *perm, ir_node *block,
1923 int is_chain, int size, int n_ops)
1925 if (! status->stat_options)
1930 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
1931 be_block_entry_t *block_ent;
1932 perm_class_entry_t *pc_ent;
1933 perm_stat_entry_t *ps_ent;
1935 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1936 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
1937 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
1940 ps_ent->n_copies += n_ops;
1941 stat_inc_int_distrib_tbl(ps_ent->chains, size);
1943 ps_ent->n_exchg += n_ops;
1944 stat_inc_int_distrib_tbl(ps_ent->cycles, size);
1948 } /* stat_be_block_stat_permcycle */
1950 /* Dumps a statistics snapshot. */
1951 void stat_dump_snapshot(const char *name, const char *phase)
1957 if (! status->stat_options)
1962 graph_entry_t *entry;
1963 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1966 * The constant counter is only global, so we clear it here.
1967 * Note that it does NOT contain the constants in DELETED
1968 * graphs due to this.
1970 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
1971 stat_const_clear(status);
1973 /* build the name */
1974 p = strrchr(name, '/');
1979 q = strrchr(name, '\\');
1981 /* NULL might be not the smallest pointer */
1982 if (q && (!p || q > p))
1990 if (l > sizeof(fname) - 1)
1991 l = sizeof(fname) - 1;
1993 memcpy(fname, name, l);
1999 strncat(fname, "firmstat-", sizeof(fname));
2000 strncat(fname, phase, sizeof(fname));
2001 strncat(fname, "-", sizeof(fname));
2002 strncat(fname, p, sizeof(fname));
2004 stat_dump_init(fname);
2006 /* calculate the graph statistics */
2007 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
2008 if (entry->irg == NULL) {
2009 /* special entry for the global count */
2012 if (! entry->is_deleted) {
2013 /* the graph is still alive, count the nodes on it */
2014 update_graph_stat(global, entry);
2018 /* some calculations are dependent, we pushed them on the wait_q */
2019 while (! pdeq_empty(status->wait_q)) {
2020 entry = pdeq_getr(status->wait_q);
2022 update_graph_stat_2(global, entry);
2025 /* dump per graph */
2026 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
2027 if (entry->irg == NULL) {
2028 /* special entry for the global count */
2032 if (! entry->is_deleted || status->stat_options & FIRMSTAT_COUNT_DELETED) {
2033 stat_dump_graph(entry);
2034 stat_dump_registered(entry);
2037 if (! entry->is_deleted) {
2038 /* clear the counter that are not accumulated */
2039 graph_clear_entry(entry, 0);
2044 stat_dump_graph(global);
2046 /* dump the const info */
2047 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2048 stat_dump_consts(&status->const_info);
2050 /* dump the parameter distribution */
2051 stat_dump_param_tbl(status->dist_param_cnt, global);
2055 stat_finish_pattern_history(fname);
2057 /* clear the global counter here */
2059 node_entry_t *entry;
2061 for (entry = pset_first(global->opcode_hash); entry; entry = pset_next(global->opcode_hash)) {
2062 opcode_clear_entry(entry);
2064 /* clear all global counter */
2065 graph_clear_entry(global, 1);
2069 } /* stat_dump_snapshot */
2071 /** the hook entries for the Firm statistics module */
2072 static hook_entry_t stat_hooks[hook_last];
2074 /* initialize the statistics module. */
2075 void firm_init_stat(unsigned enable_options)
2077 #define X(a) a, sizeof(a)-1
2078 #define HOOK(h, fkt) \
2079 stat_hooks[h].hook._##h = fkt; register_hook(h, &stat_hooks[h])
2082 if (! (enable_options & FIRMSTAT_ENABLED))
2085 status = xmalloc(sizeof(*status));
2086 memset(status, 0, sizeof(*status));
2088 /* enable statistics */
2089 status->stat_options = enable_options & FIRMSTAT_ENABLED ? enable_options : 0;
2091 /* register all hooks */
2092 HOOK(hook_new_ir_op, stat_new_ir_op);
2093 HOOK(hook_free_ir_op, stat_free_ir_op);
2094 HOOK(hook_new_node, stat_new_node);
2095 HOOK(hook_turn_into_id, stat_turn_into_id);
2096 HOOK(hook_new_graph, stat_new_graph);
2097 HOOK(hook_free_graph, stat_free_graph);
2098 HOOK(hook_irg_walk, stat_irg_walk);
2099 HOOK(hook_irg_walk_blkwise, stat_irg_walk_blkwise);
2100 HOOK(hook_irg_block_walk, stat_irg_block_walk);
2101 HOOK(hook_merge_nodes, stat_merge_nodes);
2102 HOOK(hook_reassociate, stat_reassociate);
2103 HOOK(hook_lower, stat_lower);
2104 HOOK(hook_inline, stat_inline);
2105 HOOK(hook_tail_rec, stat_tail_rec);
2106 HOOK(hook_strength_red, stat_strength_red);
2107 HOOK(hook_dead_node_elim, stat_dead_node_elim);
2108 HOOK(hook_if_conversion, stat_if_conversion);
2109 HOOK(hook_func_call, stat_func_call);
2110 HOOK(hook_arch_dep_replace_mul_with_shifts, stat_arch_dep_replace_mul_with_shifts);
2111 HOOK(hook_arch_dep_replace_division_by_const, stat_arch_dep_replace_division_by_const);
2113 obstack_init(&status->cnts);
2114 obstack_init(&status->be_data);
2116 /* create the hash-tables */
2117 status->irg_hash = new_pset(graph_cmp, 8);
2118 status->ir_op_hash = new_pset(opcode_cmp_2, 1);
2120 /* create the wait queue */
2121 status->wait_q = new_pdeq();
2123 if (enable_options & FIRMSTAT_COUNT_STRONG_OP) {
2124 /* build the pseudo-ops */
2126 _op_Phi0.code = --num;
2127 _op_Phi0.name = new_id_from_chars(X("Phi0"));
2129 _op_PhiM.code = --num;
2130 _op_PhiM.name = new_id_from_chars(X("PhiM"));
2132 _op_ProjM.code = --num;
2133 _op_ProjM.name = new_id_from_chars(X("ProjM"));
2135 _op_MulC.code = --num;
2136 _op_MulC.name = new_id_from_chars(X("MulC"));
2138 _op_DivC.code = --num;
2139 _op_DivC.name = new_id_from_chars(X("DivC"));
2141 _op_ModC.code = --num;
2142 _op_ModC.name = new_id_from_chars(X("ModC"));
2144 _op_DivModC.code = --num;
2145 _op_DivModC.name = new_id_from_chars(X("DivModC"));
2147 status->op_Phi0 = &_op_Phi0;
2148 status->op_PhiM = &_op_PhiM;
2149 status->op_ProjM = &_op_ProjM;
2150 status->op_MulC = &_op_MulC;
2151 status->op_DivC = &_op_DivC;
2152 status->op_ModC = &_op_ModC;
2153 status->op_DivModC = &_op_DivModC;
2155 status->op_Phi0 = NULL;
2156 status->op_PhiM = NULL;
2157 status->op_ProjM = NULL;
2158 status->op_MulC = NULL;
2159 status->op_DivC = NULL;
2160 status->op_ModC = NULL;
2161 status->op_DivModC = NULL;
2164 /* for Florian: count the Sel depth */
2165 if (enable_options & FIRMSTAT_COUNT_SELS) {
2166 _op_SelSel.code = --num;
2167 _op_SelSel.name = new_id_from_chars(X("Sel(Sel)"));
2169 _op_SelSelSel.code = --num;
2170 _op_SelSelSel.name = new_id_from_chars(X("Sel(Sel(Sel))"));
2172 status->op_SelSel = &_op_SelSel;
2173 status->op_SelSelSel = &_op_SelSelSel;
2175 status->op_SelSel = NULL;
2176 status->op_SelSelSel = NULL;
2179 /* register the dumper */
2180 stat_register_dumper(&simple_dumper);
2182 if (enable_options & FIRMSTAT_CSV_OUTPUT)
2183 stat_register_dumper(&csv_dumper);
2185 /* initialize the pattern hash */
2186 stat_init_pattern_history(enable_options & FIRMSTAT_PATTERN_ENABLED);
2188 /* initialize the Const options */
2189 if (enable_options & FIRMSTAT_COUNT_CONSTS)
2190 stat_init_const_cnt(status);
2192 /* distribution table for parameter counts */
2193 status->dist_param_cnt = stat_new_int_distrib_tbl();
2197 } /* firm_init_stat */
2200 * Frees all dumper structures.
2202 static void stat_term_dumper(void) {
2203 dumper_t *dumper, *next_dumper;
2205 for (dumper = status->dumper; dumper; /* iteration done in loop body */ ) {
2206 if (dumper->func_map)
2207 del_pset(dumper->func_map);
2209 next_dumper = dumper->next;
2211 dumper = next_dumper;
2213 } /* stat_term_dumper */
2216 /* Terminates the statistics module, frees all memory. */
2217 void stat_term(void) {
2218 if (status != (stat_info_t *)&status_disable) {
2219 obstack_free(&status->be_data, NULL);
2220 obstack_free(&status->cnts, NULL);
2225 status = (stat_info_t *)&status_disable;
2229 /* returns 1 if statistics were initialized, 0 otherwise */
2230 int stat_is_active(void) {
2231 return status != (stat_info_t *)&status_disable;
2232 } /* stat_is_active */
2236 /* initialize the statistics module. */
2237 void firm_init_stat(unsigned enable_options) {}
2239 /* Dumps a statistics snapshot */
2240 void stat_dump_snapshot(const char *name, const char *phase) {}
2242 /* terminates the statistics module, frees all memory */
2243 void stat_term(void);
2245 #endif /* FIRM_STATISTICS */