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 */
706 /** Calculates how many arguments of the call are const. */
707 static int cnt_const_args(ir_node *call) {
709 int n = get_Call_n_params(call);
711 for (i = 0; i < n; ++i) {
712 ir_node *param = get_Call_param(call, i);
713 ir_op *op = get_irn_op(param);
715 if (op == op_Const || op == op_SymConst)
719 } /* cnt_const_args */
722 * Update info on calls.
724 * @param call The call
725 * @param graph The graph entry containing the call
727 static void stat_update_call(ir_node *call, graph_entry_t *graph)
729 ir_node *block = get_nodes_block(call);
730 ir_node *ptr = get_Call_ptr(call);
731 ir_entity *ent = NULL;
732 ir_graph *callee = NULL;
736 * If the block is bad, the whole subgraph will collapse later
737 * so do not count this call.
738 * This happens in dead code.
743 cnt_inc(&graph->cnt[gcnt_all_calls]);
745 /* found a call, this function is not a leaf */
748 if (get_irn_op(ptr) == op_SymConst) {
749 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
750 /* ok, we seems to know the entity */
751 ent = get_SymConst_entity(ptr);
752 callee = get_entity_irg(ent);
754 /* it is recursive, if it calls at least once */
755 if (callee == graph->irg)
756 graph->is_recursive = 1;
759 /* indirect call, be could not predict */
760 cnt_inc(&graph->cnt[gcnt_indirect_calls]);
762 /* NOT a leaf call */
763 graph->is_leaf_call = LCS_NON_LEAF_CALL;
766 /* check, if it's a chain-call: Then, the call-block
767 * must dominate the end block. */
769 ir_node *curr = get_irg_end_block(graph->irg);
770 int depth = get_Block_dom_depth(block);
772 for (; curr != block && get_Block_dom_depth(curr) > depth;) {
773 curr = get_Block_idom(curr);
775 if (! curr || is_no_Block(curr))
780 graph->is_chain_call = 0;
783 /* check, if the callee is a leaf */
785 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
787 if (called->is_analyzed) {
788 if (! called->is_leaf)
789 graph->is_leaf_call = LCS_NON_LEAF_CALL;
793 /* check, if arguments of the call are const */
794 num_const_args = cnt_const_args(call);
796 if (num_const_args > 0)
797 cnt_inc(&graph->cnt[gcnt_call_with_cnst_arg]);
798 } /* stat_update_call */
801 * Update info on calls for graphs on the wait queue.
803 static void stat_update_call_2(ir_node *call, graph_entry_t *graph)
805 ir_node *block = get_nodes_block(call);
806 ir_node *ptr = get_Call_ptr(call);
807 ir_entity *ent = NULL;
808 ir_graph *callee = NULL;
811 * If the block is bad, the whole subgraph will collapse later
812 * so do not count this call.
813 * This happens in dead code.
818 if (get_irn_op(ptr) == op_SymConst) {
819 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
820 /* ok, we seems to know the entity */
821 ent = get_SymConst_entity(ptr);
822 callee = get_entity_irg(ent);
826 /* check, if the callee is a leaf */
828 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
830 assert(called->is_analyzed);
832 if (! called->is_leaf)
833 graph->is_leaf_call = LCS_NON_LEAF_CALL;
835 graph->is_leaf_call = LCS_NON_LEAF_CALL;
836 } /* stat_update_call_2 */
839 * Find the base address and entity of an Sel node.
841 * @param sel the node
843 * @return the base address.
845 static ir_node *find_base_adr(ir_node *sel) {
846 ir_node *ptr = get_Sel_ptr(sel);
848 while (is_Sel(ptr)) {
850 ptr = get_Sel_ptr(sel);
853 } /* find_base_adr */
856 * Update info on Load/Store address statistics.
858 static void stat_update_address(ir_node *node, graph_entry_t *graph) {
859 ir_opcode opc = get_irn_opcode(node);
865 /* a global address */
866 cnt_inc(&graph->cnt[gcnt_global_adr]);
869 base = find_base_adr(node);
870 irg = current_ir_graph;
871 if (base == get_irg_tls(irg)) {
872 /* a TLS variable, like a global. */
873 cnt_inc(&graph->cnt[gcnt_global_adr]);
874 } else if (base == get_irg_frame(irg)) {
875 /* a local Variable. */
876 cnt_inc(&graph->cnt[gcnt_local_adr]);
879 if (is_Proj(base) && skip_Proj(get_Proj_pred(base)) == get_irg_start(irg)) {
880 /* pointer access through parameter, check for THIS */
881 ir_entity *ent = get_irg_entity(irg);
884 ir_type *ent_tp = get_entity_type(ent);
886 if (get_method_calling_convention(ent_tp) & cc_this_call) {
887 if (get_Proj_proj(base) == 0) {
889 cnt_inc(&graph->cnt[gcnt_this_adr]);
894 /* other parameter */
895 cnt_inc(&graph->cnt[gcnt_param_adr]);
898 /* unknown Pointer access */
899 cnt_inc(&graph->cnt[gcnt_other_adr]);
905 } /* stat_update_address */
908 * Walker for reachable nodes count.
910 static void update_node_stat(ir_node *node, void *env)
912 graph_entry_t *graph = env;
915 ir_op *op = stat_get_irn_op(node);
916 int arity = get_irn_arity(node);
918 entry = opcode_get_entry(op, graph->opcode_hash);
920 cnt_inc(&entry->cnt_alive);
921 cnt_add_i(&graph->cnt[gcnt_edges], arity);
923 /* count block edges */
924 undate_block_info(node, graph);
926 /* count extended block edges */
927 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
928 if (graph->irg != get_const_code_irg())
929 update_extbb_info(node, graph);
932 /* handle statistics for special node types */
936 if (status->stat_options & FIRMSTAT_COUNT_CONSTS) {
937 /* check properties of constants */
938 stat_update_const(status, node, graph);
942 /* check for properties that depends on calls like recursion/leaf/indirect call */
943 stat_update_call(node, graph);
946 /* check address properties */
947 stat_update_address(get_Load_ptr(node), graph);
950 /* check address properties */
951 stat_update_address(get_Store_ptr(node), graph);
956 } /* update_node_stat */
959 * Walker for reachable nodes count for graphs on the wait_q.
961 static void update_node_stat_2(ir_node *node, void *env) {
962 graph_entry_t *graph = env;
964 /* check for properties that depends on calls like recursion/leaf/indirect call */
966 stat_update_call_2(node, graph);
967 } /* update_node_stat_2 */
970 * Get the current address mark.
972 static unsigned get_adr_mark(graph_entry_t *graph, ir_node *node) {
973 address_mark_entry_t *value = set_find(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
975 return value ? value->mark : 0;
979 * Set the current address mark.
981 static void set_adr_mark(graph_entry_t *graph, ir_node *node, unsigned val) {
982 address_mark_entry_t *value = set_insert(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
991 * a vcg attribute hook: Color a node with a different color if
992 * it's identified as a part of an address expression or at least referenced
993 * by an address expression.
995 static int stat_adr_mark_hook(FILE *F, ir_node *node, ir_node *local)
997 ir_node *n = local ? local : node;
998 ir_graph *irg = get_irn_irg(n);
999 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1000 unsigned mark = get_adr_mark(graph, n);
1002 if (mark & MARK_ADDRESS_CALC)
1003 fprintf(F, "color: purple");
1004 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1005 fprintf(F, "color: pink");
1006 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1007 fprintf(F, "color: lightblue");
1011 /* I know the color! */
1013 } /* stat_adr_mark_hook */
1014 #endif /* DUMP_ADR_MODE */
1017 * Return the "operational" mode of a Firm node.
1019 static ir_mode *get_irn_op_mode(ir_node *node) {
1020 switch (get_irn_opcode(node)) {
1022 return get_Load_mode(node);
1024 return get_irn_mode(get_Store_value(node));
1026 return get_irn_mode(get_DivMod_left(node));
1028 return get_irn_mode(get_Div_left(node));
1030 return get_irn_mode(get_Mod_left(node));
1032 /* Cmp is no address calculation, or is it? */
1034 return get_irn_mode(node);
1036 } /* get_irn_op_mode */
1039 * Post-walker that marks every node that is an address calculation.
1041 * Users of a node must be visited first. We ensure this by
1042 * calling it in the post of an outs walk. This should work even in cycles,
1043 * while the normal pre-walk will not.
1045 static void mark_address_calc(ir_node *node, void *env) {
1046 graph_entry_t *graph = env;
1047 ir_mode *mode = get_irn_op_mode(node);
1049 unsigned mark_preds = MARK_REF_NON_ADR;
1051 if (! mode_is_numP(mode))
1054 if (mode_is_reference(mode)) {
1055 /* a reference is calculated here, we are sure */
1056 set_adr_mark(graph, node, MARK_ADDRESS_CALC);
1058 mark_preds = MARK_REF_ADR;
1060 unsigned mark = get_adr_mark(graph, node);
1062 if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR) {
1064 * this node has no reference mode, but is only
1065 * referenced by address calculations
1067 mark_preds = MARK_REF_ADR;
1071 /* mark all predecessors */
1072 for (i = 0, n = get_irn_arity(node); i < n; ++i) {
1073 ir_node *pred = get_irn_n(node, i);
1075 mode = get_irn_op_mode(pred);
1076 if (! mode_is_numP(mode))
1079 set_adr_mark(graph, pred, get_adr_mark(graph, pred) | mark_preds);
1081 } /* mark_address_calc */
1084 * Post-walker that marks every node that is an address calculation.
1086 * Users of a node must be visited first. We ensure this by
1087 * calling it in the post of an outs walk. This should work even in cycles,
1088 * while the normal pre-walk will not.
1090 static void count_adr_ops(ir_node *node, void *env) {
1091 graph_entry_t *graph = env;
1092 unsigned mark = get_adr_mark(graph, node);
1094 if (mark & MARK_ADDRESS_CALC)
1095 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1096 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1097 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1098 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1099 cnt_inc(&graph->cnt[gcnt_all_adr_ops]);
1100 } /* count_adr_ops */
1103 * Called for every graph when the graph is either deleted or stat_dump_snapshot()
1104 * is called, must recalculate all statistic info.
1106 * @param global The global entry
1107 * @param graph The current entry
1109 static void update_graph_stat(graph_entry_t *global, graph_entry_t *graph)
1111 node_entry_t *entry;
1114 /* clear first the alive counter in the graph */
1115 foreach_pset(graph->opcode_hash, entry) {
1116 cnt_clr(&entry->cnt_alive);
1117 } /* foreach_pset */
1119 /* set pessimistic values */
1121 graph->is_leaf_call = LCS_UNKNOWN;
1122 graph->is_recursive = 0;
1123 graph->is_chain_call = 1;
1125 /* create new block counter */
1126 graph->block_hash = new_pset(block_cmp, 5);
1128 /* we need dominator info */
1129 if (graph->irg != get_const_code_irg()) {
1130 assure_doms(graph->irg);
1132 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
1133 /* we need extended basic blocks */
1134 compute_extbb(graph->irg);
1136 /* create new extbb counter */
1137 graph->extbb_hash = new_pset(block_cmp, 5);
1141 /* count the nodes in the graph */
1142 irg_walk_graph(graph->irg, update_node_stat, NULL, graph);
1145 /* Uncomment this code if chain-call means call exact one. */
1146 entry = opcode_get_entry(op_Call, graph->opcode_hash);
1148 /* check if we have more than 1 call */
1149 if (cnt_gt(entry->cnt_alive, 1))
1150 graph->is_chain_call = 0;
1153 /* recursive functions are never chain calls, leafs don't have calls */
1154 if (graph->is_recursive || graph->is_leaf)
1155 graph->is_chain_call = 0;
1157 /* assume we walk every graph only ONCE, we could sum here the global count */
1158 foreach_pset(graph->opcode_hash, entry) {
1159 node_entry_t *g_entry = opcode_get_entry(entry->op, global->opcode_hash);
1161 /* update the node counter */
1162 cnt_add(&g_entry->cnt_alive, &entry->cnt_alive);
1163 } /* foreach_pset */
1165 /* count the number of address calculation */
1166 if (graph->irg != get_const_code_irg()) {
1167 ir_graph *rem = current_ir_graph;
1169 assure_irg_outs(graph->irg);
1171 /* Must be done an the outs graph */
1172 current_ir_graph = graph->irg;
1173 irg_out_walk(get_irg_start(graph->irg), NULL, mark_address_calc, graph);
1174 current_ir_graph = rem;
1176 #ifdef DUMP_ADR_MODE
1177 /* register the vcg hook and dump the graph for test */
1178 set_dump_node_vcgattr_hook(stat_adr_mark_hook);
1179 dump_ir_block_graph(graph->irg, "-adr");
1180 set_dump_node_vcgattr_hook(NULL);
1181 #endif /* DUMP_ADR_MODE */
1183 irg_walk_graph(graph->irg, NULL, count_adr_ops, graph);
1186 /* count the DAG's */
1187 if (status->stat_options & FIRMSTAT_COUNT_DAG)
1188 count_dags_in_graph(global, graph);
1190 /* calculate the patterns of this graph */
1191 stat_calc_pattern_history(graph->irg);
1193 /* leaf function did not call others */
1195 graph->is_leaf_call = LCS_NON_LEAF_CALL;
1196 else if (graph->is_leaf_call == LCS_UNKNOWN) {
1197 /* we still don't know if this graph calls leaf-functions, so enqueue */
1198 pdeq_putl(status->wait_q, graph);
1201 /* we have analyzed this graph */
1202 graph->is_analyzed = 1;
1204 /* accumulate all counter's */
1205 for (i = 0; i < _gcnt_last; ++i)
1206 cnt_add(&global->cnt[i], &graph->cnt[i]);
1207 } /* update_graph_stat */
1210 * Called for every graph that was on the wait_q in stat_dump_snapshot()
1211 * must finish all statistic info calculations.
1213 * @param global The global entry
1214 * @param graph The current entry
1216 static void update_graph_stat_2(graph_entry_t *global, graph_entry_t *graph)
1218 if (graph->is_deleted) {
1219 /* deleted, ignore */
1224 /* count the nodes in the graph */
1225 irg_walk_graph(graph->irg, update_node_stat_2, NULL, graph);
1227 if (graph->is_leaf_call == LCS_UNKNOWN)
1228 graph->is_leaf_call = LCS_LEAF_CALL;
1230 } /* update_graph_stat_2 */
1233 * Register a dumper.
1235 static void stat_register_dumper(const dumper_t *dumper) {
1236 dumper_t *p = xmalloc(sizeof(*p));
1239 memcpy(p, dumper, sizeof(*p));
1241 p->next = status->dumper;
1246 /* FIXME: memory leak */
1247 } /* stat_register_dumper */
1250 * Dumps the statistics of an IR graph.
1252 static void stat_dump_graph(graph_entry_t *entry) {
1255 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1256 if (dumper->dump_graph)
1257 dumper->dump_graph(dumper, entry);
1259 } /* stat_dump_graph */
1262 * Calls all registered dumper functions.
1264 static void stat_dump_registered(graph_entry_t *entry) {
1267 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1268 if (dumper->func_map) {
1269 dump_graph_FUNC func;
1271 foreach_pset(dumper->func_map, func)
1272 func(dumper, entry);
1275 } /* stat_dump_registered */
1278 * Dumps a constant table.
1280 static void stat_dump_consts(const constant_info_t *tbl) {
1283 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1284 if (dumper->dump_const_tbl)
1285 dumper->dump_const_tbl(dumper, tbl);
1287 } /* stat_dump_consts */
1290 * Initialize the dumper.
1292 static void stat_dump_init(const char *name) {
1295 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1297 dumper->init(dumper, name);
1299 } /* stat_dump_init */
1302 * Finish the dumper.
1304 static void stat_dump_finish(void) {
1307 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1309 dumper->finish(dumper);
1311 } /* stat_dump_finish */
1314 * Register an additional function for all dumper.
1316 void stat_register_dumper_func(dump_graph_FUNC func) {
1319 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1320 if (! dumper->func_map)
1321 dumper->func_map = pset_new_ptr(3);
1322 pset_insert_ptr(dumper->func_map, func);
1324 } /* stat_register_dumper_func */
1326 /* ---------------------------------------------------------------------- */
1329 * Helper: get an ir_op from an opcode.
1331 ir_op *stat_get_op_from_opcode(ir_opcode code) {
1332 return opcode_find_entry(code, status->ir_op_hash);
1333 } /* stat_get_op_from_opcode */
1336 * Hook: A new IR op is registered.
1338 * @param ctx the hook context
1339 * @param op the new IR opcode that was created.
1341 static void stat_new_ir_op(void *ctx, ir_op *op) {
1342 if (! status->stat_options)
1347 graph_entry_t *graph = graph_get_entry(NULL, status->irg_hash);
1349 /* execute for side effect :-) */
1350 (void)opcode_get_entry(op, graph->opcode_hash);
1352 pset_insert(status->ir_op_hash, op, op->code);
1355 } /* stat_new_ir_op */
1358 * Hook: An IR op is freed.
1360 * @param ctx the hook context
1361 * @param op the IR opcode that is freed
1363 static void stat_free_ir_op(void *ctx, ir_op *op) {
1364 if (! status->stat_options)
1371 } /* stat_free_ir_op */
1374 * Hook: A new node is created.
1376 * @param ctx the hook context
1377 * @param irg the IR graph on which the node is created
1378 * @param node the new IR node that was created
1380 static void stat_new_node(void *ctx, ir_graph *irg, ir_node *node) {
1381 if (! status->stat_options)
1384 /* do NOT count during dead node elimination */
1385 if (status->in_dead_node_elim > 0)
1390 node_entry_t *entry;
1391 graph_entry_t *graph;
1392 ir_op *op = stat_get_irn_op(node);
1394 /* increase global value */
1395 graph = graph_get_entry(NULL, status->irg_hash);
1396 entry = opcode_get_entry(op, graph->opcode_hash);
1397 cnt_inc(&entry->new_node);
1399 /* increase local value */
1400 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1401 entry = opcode_get_entry(op, graph->opcode_hash);
1402 cnt_inc(&entry->new_node);
1405 } /* stat_new_node */
1408 * Hook: A node is changed into a Id node
1410 * @param ctx the hook context
1411 * @param node the IR node that will be turned into an ID
1413 static void stat_turn_into_id(void *ctx, ir_node *node) {
1414 if (! status->stat_options)
1419 node_entry_t *entry;
1420 graph_entry_t *graph;
1421 ir_op *op = stat_get_irn_op(node);
1423 /* increase global value */
1424 graph = graph_get_entry(NULL, status->irg_hash);
1425 entry = opcode_get_entry(op, graph->opcode_hash);
1426 cnt_inc(&entry->into_Id);
1428 /* increase local value */
1429 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1430 entry = opcode_get_entry(op, graph->opcode_hash);
1431 cnt_inc(&entry->into_Id);
1434 } /* stat_turn_into_id */
1437 * Hook: A new graph was created
1439 * @param ctx the hook context
1440 * @param irg the new IR graph that was created
1441 * @param ent the entity of this graph
1443 static void stat_new_graph(void *ctx, ir_graph *irg, ir_entity *ent) {
1444 if (! status->stat_options)
1449 /* execute for side effect :-) */
1450 graph_entry_t * graph = graph_get_entry(irg, status->irg_hash);
1453 graph->is_deleted = 0;
1455 graph->is_leaf_call = 0;
1456 graph->is_recursive = 0;
1457 graph->is_chain_call = 0;
1458 graph->is_analyzed = 0;
1461 } /* stat_new_graph */
1464 * Hook: A graph will be deleted
1466 * @param ctx the hook context
1467 * @param irg the IR graph that will be deleted
1469 * Note that we still hold the information for this graph
1470 * in our hash maps, only a flag is set which prevents this
1471 * information from being changed, it's "frozen" from now.
1473 static void stat_free_graph(void *ctx, ir_graph *irg) {
1474 if (! status->stat_options)
1479 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1480 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1482 graph->is_deleted = 1;
1484 if (status->stat_options & FIRMSTAT_COUNT_DELETED) {
1485 /* count the nodes of the graph yet, it will be destroyed later */
1486 update_graph_stat(global, graph);
1490 } /* stat_free_graph */
1493 * Hook: A walk over a graph is initiated. Do not count walks from statistic code.
1495 * @param ctx the hook context
1496 * @param irg the IR graph that will be walked
1497 * @param pre the pre walker
1498 * @param post the post walker
1500 static void stat_irg_walk(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1502 if (! status->stat_options)
1507 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1509 cnt_inc(&graph->cnt[gcnt_acc_walked]);
1512 } /* stat_irg_walk */
1515 * Hook: A walk over a graph in block-wise order is initiated. Do not count walks from statistic code.
1517 * @param ctx the hook context
1518 * @param irg the IR graph that will be walked
1519 * @param pre the pre walker
1520 * @param post the post walker
1522 static void stat_irg_walk_blkwise(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1524 /* for now, do NOT differentiate between blockwise and normal */
1525 stat_irg_walk(ctx, irg, pre, post);
1526 } /* stat_irg_walk_blkwise */
1529 * Hook: A walk over the graph's blocks is initiated. Do not count walks from statistic code.
1531 * @param ctx the hook context
1532 * @param irg the IR graph that will be walked
1533 * @param node the IR node
1534 * @param pre the pre walker
1535 * @param post the post walker
1537 static void stat_irg_block_walk(void *ctx, ir_graph *irg, ir_node *node, generic_func *pre, generic_func *post)
1539 if (! status->stat_options)
1544 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1546 cnt_inc(&graph->cnt[gcnt_acc_walked_blocks]);
1549 } /* stat_irg_block_walk */
1552 * Called for every node that is removed due to an optimization.
1554 * @param n the IR node that will be removed
1555 * @param hmap the hash map containing ir_op* -> opt_entry_t*
1557 static void removed_due_opt(ir_node *n, hmap_opt_entry_t *hmap) {
1558 ir_op *op = stat_get_irn_op(n);
1559 opt_entry_t *entry = opt_get_entry(op, hmap);
1561 /* increase global value */
1562 cnt_inc(&entry->count);
1563 } /* removed_due_opt */
1566 * Hook: Some nodes were optimized into some others due to an optimization.
1568 * @param ctx the hook context
1570 static void stat_merge_nodes(
1572 ir_node **new_node_array, int new_num_entries,
1573 ir_node **old_node_array, int old_num_entries,
1576 if (! status->stat_options)
1582 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1584 if (status->reassoc_run)
1585 opt = HOOK_OPT_REASSOC;
1587 for (i = 0; i < old_num_entries; ++i) {
1588 /* nodes might be in new and old, so if we found a node
1589 in both sets, this one is NOT removed */
1590 for (j = 0; j < new_num_entries; ++j) {
1591 if (old_node_array[i] == new_node_array[j])
1594 if (j >= new_num_entries) {
1597 /* sometimes we did not detect, that it is replaced by a Const */
1598 if (opt == HOOK_OPT_CONFIRM && new_num_entries == 1) {
1599 ir_op *op = get_irn_op(new_node_array[0]);
1601 if (op == op_Const || op == op_SymConst)
1602 xopt = HOOK_OPT_CONFIRM_C;
1605 removed_due_opt(old_node_array[i], graph->opt_hash[xopt]);
1610 } /* stat_merge_nodes */
1613 * Hook: Reassociation is started/stopped.
1615 * @param ctx the hook context
1616 * @param flag if non-zero, reassociation is started else stopped
1618 static void stat_reassociate(void *ctx, int flag) {
1619 if (! status->stat_options)
1624 status->reassoc_run = flag;
1627 } /* stat_reassociate */
1630 * Hook: A node was lowered into other nodes
1632 * @param ctx the hook context
1633 * @param node the IR node that will be lowered
1635 static void stat_lower(void *ctx, ir_node *node) {
1636 if (! status->stat_options)
1641 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1643 removed_due_opt(node, graph->opt_hash[HOOK_LOWERED]);
1649 * Hook: A graph was inlined.
1651 * @param ctx the hook context
1652 * @param call the IR call that will re changed into the body of
1653 * the called IR graph
1654 * @param called_irg the IR graph representing the called routine
1656 static void stat_inline(void *ctx, ir_node *call, ir_graph *called_irg)
1658 if (! status->stat_options)
1663 ir_graph *irg = get_irn_irg(call);
1664 graph_entry_t *i_graph = graph_get_entry(called_irg, status->irg_hash);
1665 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1667 cnt_inc(&graph->cnt[gcnt_acc_got_inlined]);
1668 cnt_inc(&i_graph->cnt[gcnt_acc_was_inlined]);
1674 * Hook: A graph with tail-recursions was optimized.
1676 * @param ctx the hook context
1678 static void stat_tail_rec(void *ctx, ir_graph *irg, int n_calls) {
1679 if (! status->stat_options)
1684 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1686 graph->num_tail_recursion += n_calls;
1689 } /* stat_tail_rec */
1692 * Strength reduction was performed on an iteration variable.
1694 * @param ctx the hook context
1696 static void stat_strength_red(void *ctx, ir_graph *irg, ir_node *strong) {
1697 if (! status->stat_options)
1702 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1703 cnt_inc(&graph->cnt[gcnt_acc_strength_red]);
1705 removed_due_opt(strong, graph->opt_hash[HOOK_OPT_STRENGTH_RED]);
1708 } /* stat_strength_red */
1711 * Hook: Start/Stop the dead node elimination.
1713 * @param ctx the hook context
1715 static void stat_dead_node_elim(void *ctx, ir_graph *irg, int start) {
1716 if (! status->stat_options)
1720 ++status->in_dead_node_elim;
1722 --status->in_dead_node_elim;
1723 } /* stat_dead_node_elim */
1726 * Hook: if-conversion was tried.
1728 static void stat_if_conversion(void *context, ir_graph *irg, ir_node *phi,
1729 int pos, ir_node *mux, if_result_t reason)
1731 if (! status->stat_options)
1736 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1738 cnt_inc(&graph->cnt[gcnt_if_conv + reason]);
1741 } /* stat_if_conversion */
1744 * Hook: real function call was optimized.
1746 static void stat_func_call(void *context, ir_graph *irg, ir_node *call)
1748 if (! status->stat_options)
1753 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1755 cnt_inc(&graph->cnt[gcnt_acc_real_func_call]);
1758 } /* stat_func_call */
1761 * Hook: A multiply was replaced by a series of Shifts/Adds/Subs.
1763 * @param ctx the hook context
1765 static void stat_arch_dep_replace_mul_with_shifts(void *ctx, ir_node *mul) {
1766 if (! status->stat_options)
1771 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1772 removed_due_opt(mul, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1775 } /* stat_arch_dep_replace_mul_with_shifts */
1778 * Hook: A division by const was replaced.
1780 * @param ctx the hook context
1781 * @param node the division node that will be optimized
1783 static void stat_arch_dep_replace_division_by_const(void *ctx, ir_node *node) {
1784 if (! status->stat_options)
1789 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1790 removed_due_opt(node, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1793 } /* stat_arch_dep_replace_division_by_const */
1796 * Update the register pressure of a block.
1798 * @param irg the irg containing the block
1799 * @param block the block for which the reg pressure should be set
1800 * @param pressure the pressure
1801 * @param class_name the name of the register class
1803 void stat_be_block_regpressure(ir_graph *irg, ir_node *block, int pressure, const char *class_name)
1805 if (! status->stat_options)
1810 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1811 be_block_entry_t *block_ent;
1812 reg_pressure_entry_t *rp_ent;
1814 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1815 rp_ent = obstack_alloc(&status->be_data, sizeof(*rp_ent));
1816 memset(rp_ent, 0, sizeof(*rp_ent));
1818 rp_ent->class_name = class_name;
1819 rp_ent->pressure = pressure;
1821 pset_insert(block_ent->reg_pressure, rp_ent, HASH_PTR(class_name));
1824 } /* stat_be_block_regpressure */
1827 * Update the distribution of ready nodes of a block
1829 * @param irg the irg containing the block
1830 * @param block the block for which the reg pressure should be set
1831 * @param num_ready the number of ready nodes
1833 void stat_be_block_sched_ready(ir_graph *irg, ir_node *block, int num_ready)
1835 if (! status->stat_options)
1840 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1841 be_block_entry_t *block_ent;
1843 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1845 /* increase the counter of corresponding number of ready nodes */
1846 stat_inc_int_distrib_tbl(block_ent->sched_ready, num_ready);
1849 } /* stat_be_block_sched_ready */
1852 * Update the permutation statistic of a block.
1854 * @param class_name the name of the register class
1855 * @param n_regs number of registers in the register class
1856 * @param perm the perm node
1857 * @param block the block containing the perm
1858 * @param size the size of the perm
1859 * @param real_size number of pairs with different registers
1861 void stat_be_block_stat_perm(const char *class_name, int n_regs, ir_node *perm, ir_node *block,
1862 int size, int real_size)
1864 if (! status->stat_options)
1869 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
1870 be_block_entry_t *block_ent;
1871 perm_class_entry_t *pc_ent;
1872 perm_stat_entry_t *ps_ent;
1874 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1875 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
1876 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
1878 pc_ent->n_regs = n_regs;
1880 /* update information */
1881 ps_ent->size = size;
1882 ps_ent->real_size = real_size;
1885 } /* stat_be_block_stat_perm */
1888 * Update the permutation statistic of a single perm.
1890 * @param class_name the name of the register class
1891 * @param perm the perm node
1892 * @param block the block containing the perm
1893 * @param is_chain 1 if chain, 0 if cycle
1894 * @param size length of the cycle/chain
1895 * @param n_ops the number of ops representing this cycle/chain after lowering
1897 void stat_be_block_stat_permcycle(const char *class_name, ir_node *perm, ir_node *block,
1898 int is_chain, int size, int n_ops)
1900 if (! status->stat_options)
1905 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
1906 be_block_entry_t *block_ent;
1907 perm_class_entry_t *pc_ent;
1908 perm_stat_entry_t *ps_ent;
1910 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1911 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
1912 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
1915 ps_ent->n_copies += n_ops;
1916 stat_inc_int_distrib_tbl(ps_ent->chains, size);
1918 ps_ent->n_exchg += n_ops;
1919 stat_inc_int_distrib_tbl(ps_ent->cycles, size);
1923 } /* stat_be_block_stat_permcycle */
1925 /* Dumps a statistics snapshot. */
1926 void stat_dump_snapshot(const char *name, const char *phase)
1932 if (! status->stat_options)
1937 graph_entry_t *entry;
1938 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1941 * The constant counter is only global, so we clear it here.
1942 * Note that it does NOT contain the constants in DELETED
1943 * graphs due to this.
1945 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
1946 stat_const_clear(status);
1948 /* build the name */
1949 p = strrchr(name, '/');
1954 q = strrchr(name, '\\');
1956 /* NULL might be not the smallest pointer */
1957 if (q && (!p || q > p))
1965 if (l > sizeof(fname) - 1)
1966 l = sizeof(fname) - 1;
1968 memcpy(fname, name, l);
1974 strncat(fname, "firmstat-", sizeof(fname));
1975 strncat(fname, phase, sizeof(fname));
1976 strncat(fname, "-", sizeof(fname));
1977 strncat(fname, p, sizeof(fname));
1979 stat_dump_init(fname);
1981 /* calculate the graph statistics */
1982 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
1983 if (entry->irg == NULL) {
1984 /* special entry for the global count */
1987 if (! entry->is_deleted) {
1988 /* the graph is still alive, count the nodes on it */
1989 update_graph_stat(global, entry);
1993 /* some calculations are dependent, we pushed them on the wait_q */
1994 while (! pdeq_empty(status->wait_q)) {
1995 entry = pdeq_getr(status->wait_q);
1997 update_graph_stat_2(global, entry);
2000 /* dump per graph */
2001 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
2002 if (entry->irg == NULL) {
2003 /* special entry for the global count */
2007 if (! entry->is_deleted || status->stat_options & FIRMSTAT_COUNT_DELETED) {
2008 stat_dump_graph(entry);
2009 stat_dump_registered(entry);
2012 if (! entry->is_deleted) {
2013 /* clear the counter that are not accumulated */
2014 graph_clear_entry(entry, 0);
2019 stat_dump_graph(global);
2021 /* dump the const info */
2022 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2023 stat_dump_consts(&status->const_info);
2027 stat_finish_pattern_history(fname);
2029 /* clear the global counter here */
2031 node_entry_t *entry;
2033 for (entry = pset_first(global->opcode_hash); entry; entry = pset_next(global->opcode_hash)) {
2034 opcode_clear_entry(entry);
2036 /* clear all global counter */
2037 graph_clear_entry(global, 1);
2041 } /* stat_dump_snapshot */
2043 /** the hook entries for the Firm statistics module */
2044 static hook_entry_t stat_hooks[hook_last];
2046 /* initialize the statistics module. */
2047 void firm_init_stat(unsigned enable_options)
2049 #define X(a) a, sizeof(a)-1
2050 #define HOOK(h, fkt) \
2051 stat_hooks[h].hook._##h = fkt; register_hook(h, &stat_hooks[h])
2054 if (! (enable_options & FIRMSTAT_ENABLED))
2057 status = xmalloc(sizeof(*status));
2058 memset(status, 0, sizeof(*status));
2060 /* enable statistics */
2061 status->stat_options = enable_options & FIRMSTAT_ENABLED ? enable_options : 0;
2063 /* register all hooks */
2064 HOOK(hook_new_ir_op, stat_new_ir_op);
2065 HOOK(hook_free_ir_op, stat_free_ir_op);
2066 HOOK(hook_new_node, stat_new_node);
2067 HOOK(hook_turn_into_id, stat_turn_into_id);
2068 HOOK(hook_new_graph, stat_new_graph);
2069 HOOK(hook_free_graph, stat_free_graph);
2070 HOOK(hook_irg_walk, stat_irg_walk);
2071 HOOK(hook_irg_walk_blkwise, stat_irg_walk_blkwise);
2072 HOOK(hook_irg_block_walk, stat_irg_block_walk);
2073 HOOK(hook_merge_nodes, stat_merge_nodes);
2074 HOOK(hook_reassociate, stat_reassociate);
2075 HOOK(hook_lower, stat_lower);
2076 HOOK(hook_inline, stat_inline);
2077 HOOK(hook_tail_rec, stat_tail_rec);
2078 HOOK(hook_strength_red, stat_strength_red);
2079 HOOK(hook_dead_node_elim, stat_dead_node_elim);
2080 HOOK(hook_if_conversion, stat_if_conversion);
2081 HOOK(hook_func_call, stat_func_call);
2082 HOOK(hook_arch_dep_replace_mul_with_shifts, stat_arch_dep_replace_mul_with_shifts);
2083 HOOK(hook_arch_dep_replace_division_by_const, stat_arch_dep_replace_division_by_const);
2085 obstack_init(&status->cnts);
2086 obstack_init(&status->be_data);
2088 /* create the hash-tables */
2089 status->irg_hash = new_pset(graph_cmp, 8);
2090 status->ir_op_hash = new_pset(opcode_cmp_2, 1);
2092 /* create the wait queue */
2093 status->wait_q = new_pdeq();
2095 if (enable_options & FIRMSTAT_COUNT_STRONG_OP) {
2096 /* build the pseudo-ops */
2098 _op_Phi0.code = --num;
2099 _op_Phi0.name = new_id_from_chars(X("Phi0"));
2101 _op_PhiM.code = --num;
2102 _op_PhiM.name = new_id_from_chars(X("PhiM"));
2104 _op_ProjM.code = --num;
2105 _op_ProjM.name = new_id_from_chars(X("ProjM"));
2107 _op_MulC.code = --num;
2108 _op_MulC.name = new_id_from_chars(X("MulC"));
2110 _op_DivC.code = --num;
2111 _op_DivC.name = new_id_from_chars(X("DivC"));
2113 _op_ModC.code = --num;
2114 _op_ModC.name = new_id_from_chars(X("ModC"));
2116 _op_DivModC.code = --num;
2117 _op_DivModC.name = new_id_from_chars(X("DivModC"));
2119 status->op_Phi0 = &_op_Phi0;
2120 status->op_PhiM = &_op_PhiM;
2121 status->op_ProjM = &_op_ProjM;
2122 status->op_MulC = &_op_MulC;
2123 status->op_DivC = &_op_DivC;
2124 status->op_ModC = &_op_ModC;
2125 status->op_DivModC = &_op_DivModC;
2127 status->op_Phi0 = NULL;
2128 status->op_PhiM = NULL;
2129 status->op_ProjM = NULL;
2130 status->op_MulC = NULL;
2131 status->op_DivC = NULL;
2132 status->op_ModC = NULL;
2133 status->op_DivModC = NULL;
2136 /* for Florian: count the Sel depth */
2137 if (enable_options & FIRMSTAT_COUNT_SELS) {
2138 _op_SelSel.code = --num;
2139 _op_SelSel.name = new_id_from_chars(X("Sel(Sel)"));
2141 _op_SelSelSel.code = --num;
2142 _op_SelSelSel.name = new_id_from_chars(X("Sel(Sel(Sel))"));
2144 status->op_SelSel = &_op_SelSel;
2145 status->op_SelSelSel = &_op_SelSelSel;
2147 status->op_SelSel = NULL;
2148 status->op_SelSelSel = NULL;
2151 /* register the dumper */
2152 stat_register_dumper(&simple_dumper);
2154 if (enable_options & FIRMSTAT_CSV_OUTPUT)
2155 stat_register_dumper(&csv_dumper);
2157 /* initialize the pattern hash */
2158 stat_init_pattern_history(enable_options & FIRMSTAT_PATTERN_ENABLED);
2160 /* initialize the Const options */
2161 if (enable_options & FIRMSTAT_COUNT_CONSTS)
2162 stat_init_const_cnt(status);
2166 } /* firm_init_stat */
2169 * Frees all dumper structures.
2171 static void stat_term_dumper(void) {
2172 dumper_t *dumper, *next_dumper;
2174 for (dumper = status->dumper; dumper; /* iteration done in loop body */ ) {
2175 if (dumper->func_map)
2176 del_pset(dumper->func_map);
2178 next_dumper = dumper->next;
2180 dumper = next_dumper;
2182 } /* stat_term_dumper */
2185 /* Terminates the statistics module, frees all memory. */
2186 void stat_term(void) {
2187 if (status != (stat_info_t *)&status_disable) {
2188 obstack_free(&status->be_data, NULL);
2189 obstack_free(&status->cnts, NULL);
2194 status = (stat_info_t *)&status_disable;
2198 /* returns 1 if statistics were initialized, 0 otherwise */
2199 int stat_is_active(void) {
2200 return status != (stat_info_t *)&status_disable;
2201 } /* stat_is_active */
2205 /* initialize the statistics module. */
2206 void firm_init_stat(unsigned enable_options) {}
2208 /* Dumps a statistics snapshot */
2209 void stat_dump_snapshot(const char *name, const char *phase) {}
2211 /* terminates the statistics module, frees all memory */
2212 void stat_term(void);
2214 #endif /* FIRM_STATISTICS */