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)
93 const node_entry_t *e1 = elt;
94 const node_entry_t *e2 = key;
96 return e1->op->code - e2->op->code;
100 * compare two elements of the graph hash
102 static int graph_cmp(const void *elt, const void *key)
104 const graph_entry_t *e1 = elt;
105 const graph_entry_t *e2 = key;
107 return e1->irg != e2->irg;
111 * compare two elements of the optimization hash
113 static int opt_cmp(const void *elt, const void *key)
115 const opt_entry_t *e1 = elt;
116 const opt_entry_t *e2 = key;
118 return e1->op->code != e2->op->code;
122 * compare two elements of the block/extbb hash
124 static int block_cmp(const void *elt, const void *key)
126 const block_entry_t *e1 = elt;
127 const block_entry_t *e2 = key;
129 return e1->block_nr != e2->block_nr;
133 * compare two elements of the block/extbb hash
135 static int reg_pressure_cmp(const void *elt, const void *key)
137 const reg_pressure_entry_t *e1 = elt;
138 const reg_pressure_entry_t *e2 = key;
140 return e1->id_name != e2->id_name;
144 * compare two elements of the ir_op hash
146 static int opcode_cmp_2(const void *elt, const void *key)
148 const ir_op *e1 = elt;
149 const ir_op *e2 = key;
151 return e1->code != e2->code;
155 * compare two elements of the address_mark set
157 static int address_mark_cmp(const void *elt, const void *key, size_t size)
159 const address_mark_entry_t *e1 = elt;
160 const address_mark_entry_t *e2 = key;
162 /* compare only the nodes, the rest is used as data container */
163 return e1->node != e2->node;
167 * clears all counter in a node_entry_t
169 static void opcode_clear_entry(node_entry_t *elem)
171 cnt_clr(&elem->cnt_alive);
172 cnt_clr(&elem->new_node);
173 cnt_clr(&elem->into_Id);
177 * Returns the associates node_entry_t for an ir_op
179 * @param op the IR operation
180 * @param hmap a hash map containing ir_op* -> node_entry_t*
182 static node_entry_t *opcode_get_entry(const ir_op *op, hmap_node_entry_t *hmap)
189 elem = pset_find(hmap, &key, op->code);
193 elem = obstack_alloc(&status->cnts, sizeof(*elem));
194 memset(elem, 0, sizeof(*elem));
197 opcode_clear_entry(elem);
201 return pset_insert(hmap, elem, op->code);
205 * Returns the associates ir_op for an opcode
207 * @param code the IR opcode
208 * @param hmap the hash map containing opcode -> ir_op*
210 static ir_op *opcode_find_entry(opcode code, hmap_ir_op *hmap)
215 return pset_find(hmap, &key, code);
219 * clears all counter in a graph_entry_t
221 static void graph_clear_entry(graph_entry_t *elem, int all)
224 cnt_clr(&elem->cnt_walked);
225 cnt_clr(&elem->cnt_walked_blocks);
226 cnt_clr(&elem->cnt_was_inlined);
227 cnt_clr(&elem->cnt_got_inlined);
228 cnt_clr(&elem->cnt_strength_red);
229 cnt_clr(&elem->cnt_real_func_call);
231 cnt_clr(&elem->cnt_edges);
232 cnt_clr(&elem->cnt_all_calls);
233 cnt_clr(&elem->cnt_call_with_cnst_arg);
234 cnt_clr(&elem->cnt_indirect_calls);
236 if (elem->block_hash) {
237 del_pset(elem->block_hash);
238 elem->block_hash = NULL;
241 if (elem->extbb_hash) {
242 del_pset(elem->extbb_hash);
243 elem->extbb_hash = NULL;
246 obstack_free(&elem->recalc_cnts, NULL);
247 obstack_init(&elem->recalc_cnts);
251 * Returns the associated graph_entry_t for an IR graph.
253 * @param irg the IR graph
254 * @param hmap the hash map containing ir_graph* -> graph_entry_t*
256 static graph_entry_t *graph_get_entry(ir_graph *irg, hmap_graph_entry_t *hmap)
264 elem = pset_find(hmap, &key, HASH_PTR(irg));
268 /* allocate a new one */
269 elem = obstack_alloc(&status->cnts, sizeof(*elem));
270 memset(elem, 0, sizeof(*elem));
271 obstack_init(&elem->recalc_cnts);
274 graph_clear_entry(elem, 1);
276 /* new hash table for opcodes here */
277 elem->opcode_hash = new_pset(opcode_cmp, 5);
278 elem->address_mark = new_set(address_mark_cmp, 5);
281 /* create hash map for reg pressure */
282 elem->rp_block_hash = new_pset(block_cmp, 5);
284 /* these hash tables are created on demand */
285 elem->block_hash = NULL;
286 elem->extbb_hash = NULL;
288 for (i = 0; i < sizeof(elem->opt_hash)/sizeof(elem->opt_hash[0]); ++i)
289 elem->opt_hash[i] = new_pset(opt_cmp, 4);
291 return pset_insert(hmap, elem, HASH_PTR(irg));
295 * clears all counter in an opt_entry_t
297 static void opt_clear_entry(opt_entry_t *elem)
299 cnt_clr(&elem->count);
303 * Returns the associated opt_entry_t for an IR operation.
305 * @param op the IR operation
306 * @param hmap the hash map containing ir_op* -> opt_entry_t*
308 static opt_entry_t *opt_get_entry(const ir_op *op, hmap_opt_entry_t *hmap)
315 elem = pset_find(hmap, &key, op->code);
319 elem = obstack_alloc(&status->cnts, sizeof(*elem));
320 memset(elem, 0, sizeof(*elem));
322 /* clear new counter */
323 opt_clear_entry(elem);
327 return pset_insert(hmap, elem, op->code);
331 * clears all counter in a block_entry_t
333 static void block_clear_entry(block_entry_t *elem)
335 cnt_clr(&elem->cnt_nodes);
336 cnt_clr(&elem->cnt_edges);
337 cnt_clr(&elem->cnt_in_edges);
338 cnt_clr(&elem->cnt_out_edges);
339 cnt_clr(&elem->cnt_phi_data);
340 elem->reg_pressure = new_pset(reg_pressure_cmp, 2);
344 * Returns the associated block_entry_t for an block.
346 * @param block_nr an IR block number
347 * @param hmap a hash map containing long -> block_entry_t
349 static block_entry_t *block_get_entry(struct obstack *obst, long block_nr, hmap_block_entry_t *hmap)
354 key.block_nr = block_nr;
356 elem = pset_find(hmap, &key, block_nr);
360 elem = obstack_alloc(obst, sizeof(*elem));
361 memset(elem, 0, sizeof(*elem));
363 /* clear new counter */
364 block_clear_entry(elem);
366 elem->block_nr = block_nr;
368 return pset_insert(hmap, elem, block_nr);
373 * Returns the ir_op for an IR-node,
374 * handles special cases and return pseudo op codes.
376 * @param none an IR node
378 static ir_op *stat_get_irn_op(ir_node *node)
380 ir_op *op = get_irn_op(node);
382 if (op == op_Phi && get_irn_arity(node) == 0) {
383 /* special case, a Phi0 node, count on extra counter */
384 op = status->op_Phi0 ? status->op_Phi0 : op;
386 else if (op == op_Phi && get_irn_mode(node) == mode_M) {
387 /* special case, a Memory Phi node, count on extra counter */
388 op = status->op_PhiM ? status->op_PhiM : op;
390 else if (op == op_Proj && get_irn_mode(node) == mode_M) {
391 /* special case, a Memory Proj node, count on extra counter */
392 op = status->op_ProjM ? status->op_ProjM : op;
394 else if (op == op_Mul &&
395 (get_irn_op(get_Mul_left(node)) == op_Const || get_irn_op(get_Mul_right(node)) == op_Const)) {
396 /* special case, a Multiply by a const, count on extra counter */
397 op = status->op_MulC ? status->op_MulC : op;
399 else if (op == op_Div && get_irn_op(get_Div_right(node)) == op_Const) {
400 /* special case, a division by a const, count on extra counter */
401 op = status->op_DivC ? status->op_DivC : op;
403 else if (op == op_Mod && get_irn_op(get_Mod_right(node)) == op_Const) {
404 /* special case, a module by a const, count on extra counter */
405 op = status->op_ModC ? status->op_ModC : op;
407 else if (op == op_DivMod && get_irn_op(get_DivMod_right(node)) == op_Const) {
408 /* special case, a division/modulo by a const, count on extra counter */
409 op = status->op_DivModC ? status->op_DivModC : op;
411 else if (op == op_Sel && get_irn_op(get_Sel_ptr(node)) == op_Sel) {
412 /* special case, a Sel of a Sel, count on extra counter */
413 op = status->op_SelSel ? status->op_SelSel : op;
415 if (get_irn_op(get_Sel_ptr(get_Sel_ptr(node))) == op_Sel) {
416 /* special case, a Sel of a Sel of a Sel, count on extra counter */
417 op = status->op_SelSelSel ? status->op_SelSelSel : op;
425 * update the block counter
427 static void undate_block_info(ir_node *node, graph_entry_t *graph)
429 ir_op *op = get_irn_op(node);
431 block_entry_t *b_entry;
434 /* check for block */
435 if (op == op_Block) {
436 arity = get_irn_arity(node);
437 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(node), graph->block_hash);
439 /* count all incoming edges */
440 for (i = 0; i < arity; ++i) {
441 ir_node *pred = get_irn_n(node, i);
442 ir_node *other_block = get_nodes_block(pred);
443 block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
445 cnt_inc(&b_entry->cnt_in_edges); /* an edge coming from another block */
446 cnt_inc(&b_entry_other->cnt_out_edges);
451 block = get_nodes_block(node);
452 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(block), graph->block_hash);
454 if (op == op_Phi && mode_is_datab(get_irn_mode(node))) {
455 /* count data Phi per block */
456 cnt_inc(&b_entry->cnt_phi_data);
459 /* we have a new node in our block */
460 cnt_inc(&b_entry->cnt_nodes);
462 /* don't count keep-alive edges */
463 if (get_irn_op(node) == op_End)
466 arity = get_irn_arity(node);
468 for (i = 0; i < arity; ++i) {
469 ir_node *pred = get_irn_n(node, i);
470 ir_node *other_block;
472 other_block = get_nodes_block(pred);
474 if (other_block == block)
475 cnt_inc(&b_entry->cnt_edges); /* a in block edge */
477 block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
479 cnt_inc(&b_entry->cnt_in_edges); /* an edge coming from another block */
480 cnt_inc(&b_entry_other->cnt_out_edges);
486 * update the extended block counter
488 static void undate_extbb_info(ir_node *node, graph_entry_t *graph)
490 ir_op *op = get_irn_op(node);
492 extbb_entry_t *eb_entry;
495 /* check for block */
496 if (op == op_Block) {
497 extbb = get_nodes_extbb(node);
498 arity = get_irn_arity(node);
499 eb_entry = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(extbb), graph->extbb_hash);
501 /* count all incoming edges */
502 for (i = 0; i < arity; ++i) {
503 ir_node *pred = get_irn_n(node, i);
504 ir_extblk *other_extbb = get_nodes_extbb(pred);
506 if (extbb != other_extbb) {
507 extbb_entry_t *eb_entry_other = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(other_extbb), graph->extbb_hash);
509 cnt_inc(&eb_entry->cnt_in_edges); /* an edge coming from another extbb */
510 cnt_inc(&eb_entry_other->cnt_out_edges);
516 extbb = get_nodes_extbb(node);
517 eb_entry = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(extbb), graph->extbb_hash);
519 if (op == op_Phi && mode_is_datab(get_irn_mode(node))) {
520 /* count data Phi per extbb */
521 cnt_inc(&eb_entry->cnt_phi_data);
524 /* we have a new node in our block */
525 cnt_inc(&eb_entry->cnt_nodes);
527 /* don't count keep-alive edges */
528 if (get_irn_op(node) == op_End)
531 arity = get_irn_arity(node);
533 for (i = 0; i < arity; ++i) {
534 ir_node *pred = get_irn_n(node, i);
535 ir_extblk *other_extbb = get_nodes_extbb(pred);
537 if (other_extbb == extbb)
538 cnt_inc(&eb_entry->cnt_edges); /* a in extbb edge */
540 extbb_entry_t *eb_entry_other = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(other_extbb), graph->extbb_hash);
542 cnt_inc(&eb_entry->cnt_in_edges); /* an edge coming from another extbb */
543 cnt_inc(&eb_entry_other->cnt_out_edges);
548 /** calculates how many arguments of the call are const */
549 static int cnt_const_args(ir_node *call)
552 int n = get_Call_n_params(call);
554 for (i = 0; i < n; ++i) {
555 ir_node *param = get_Call_param(call, i);
556 ir_op *op = get_irn_op(param);
558 if (op == op_Const || op == op_SymConst)
565 * update info on calls
567 * @param call The call
568 * @param graph The graph entry containing the call
570 static void stat_update_call(ir_node *call, graph_entry_t *graph)
572 ir_node *block = get_nodes_block(call);
573 ir_node *ptr = get_Call_ptr(call);
575 ir_graph *callee = NULL;
579 * If the block is bad, the whole subgraph will collapse later
580 * so do not count this call.
581 * This happens in dead code.
586 cnt_inc(&graph->cnt_all_calls);
588 /* found a call, this function is not a leaf */
591 if (get_irn_op(ptr) == op_SymConst) {
592 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
593 /* ok, we seems to know the entity */
594 ent = get_SymConst_entity(ptr);
595 callee = get_entity_irg(ent);
597 /* it is recursive, if it calls at least once */
598 if (callee == graph->irg)
599 graph->is_recursive = 1;
603 /* indirect call, be could not predict */
604 cnt_inc(&graph->cnt_indirect_calls);
606 /* NOT a leaf call */
607 graph->is_leaf_call = LCS_NON_LEAF_CALL;
610 /* check, if it's a chain-call: Then, the call-block
611 * must dominate the end block. */
613 ir_node *curr = get_irg_end_block(graph->irg);
614 int depth = get_Block_dom_depth(block);
616 for (; curr != block && get_Block_dom_depth(curr) > depth;) {
617 curr = get_Block_idom(curr);
619 if (! curr || is_no_Block(curr))
624 graph->is_chain_call = 0;
627 /* check, if the callee is a leaf */
629 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
631 if (called->is_analyzed) {
632 if (! called->is_leaf)
633 graph->is_leaf_call = LCS_NON_LEAF_CALL;
637 /* check, if arguments of the call are const */
638 num_const_args = cnt_const_args(call);
640 if (num_const_args > 0)
641 cnt_inc(&graph->cnt_call_with_cnst_arg);
645 * update info on calls for graphs on the wait queue
647 static void stat_update_call_2(ir_node *call, graph_entry_t *graph)
649 ir_node *block = get_nodes_block(call);
650 ir_node *ptr = get_Call_ptr(call);
652 ir_graph *callee = NULL;
655 * If the block is bad, the whole subgraph will collapse later
656 * so do not count this call.
657 * This happens in dead code.
662 if (get_irn_op(ptr) == op_SymConst) {
663 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
664 /* ok, we seems to know the entity */
665 ent = get_SymConst_entity(ptr);
666 callee = get_entity_irg(ent);
670 /* check, if the callee is a leaf */
672 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
674 assert(called->is_analyzed);
676 if (! called->is_leaf)
677 graph->is_leaf_call = LCS_NON_LEAF_CALL;
680 graph->is_leaf_call = LCS_NON_LEAF_CALL;
684 * walker for reachable nodes count
686 static void update_node_stat(ir_node *node, void *env)
688 graph_entry_t *graph = env;
691 ir_op *op = stat_get_irn_op(node);
692 int arity = get_irn_arity(node);
694 entry = opcode_get_entry(op, graph->opcode_hash);
696 cnt_inc(&entry->cnt_alive);
697 cnt_add_i(&graph->cnt_edges, arity);
699 /* count block edges */
700 undate_block_info(node, graph);
702 /* count extended block edges */
703 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
704 undate_extbb_info(node, graph);
707 /* handle statistics for special node types */
709 if (op == op_Const) {
710 if (status->stat_options & FIRMSTAT_COUNT_CONSTS) {
711 /* check properties of constants */
712 stat_update_const(status, node, graph);
715 else if (op == op_Call) {
716 /* check for properties that depends on calls like recursion/leaf/indirect call */
717 stat_update_call(node, graph);
722 * walker for reachable nodes count for graphs on the wait_q
724 static void update_node_stat_2(ir_node *node, void *env)
726 graph_entry_t *graph = env;
728 /* check for properties that depends on calls like recursion/leaf/indirect call */
730 stat_update_call_2(node, graph);
734 * get the current address mark
736 static unsigned get_adr_mark(graph_entry_t *graph, ir_node *node)
738 address_mark_entry_t *value = set_find(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
740 return value ? value->mark : 0;
744 * set the current address mark
746 static void set_adr_mark(graph_entry_t *graph, ir_node *node, unsigned val)
748 address_mark_entry_t *value = set_insert(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
754 * a vcg attribute hook: Color a node with a different color if
755 * it's identified as a part of an address expression or at least referenced
756 * by an address expression.
758 static int stat_adr_mark_hook(FILE *F, ir_node *node, ir_node *local)
760 ir_node *n = local ? local : node;
761 ir_graph *irg = get_irn_irg(n);
762 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
763 unsigned mark = get_adr_mark(graph, n);
765 if (mark & MARK_ADDRESS_CALC)
766 fprintf(F, "color: purple");
767 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
768 fprintf(F, "color: pink");
769 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
770 fprintf(F, "color: lightblue");
774 /* I know the color! */
779 * walker that marks every node that is an address calculation
781 * predecessor nodes must be visited first. We ensure this by
782 * calling in in the post of an outs walk. This should work even in cycles,
783 * while the pre in a normal walk will not.
785 static void mark_address_calc(ir_node *node, void *env)
787 graph_entry_t *graph = env;
788 ir_mode *mode = get_irn_mode(node);
790 unsigned mark_preds = MARK_REF_NON_ADR;
792 if (! mode_is_numP(mode))
795 if (mode_is_reference(mode)) {
796 /* a reference is calculated here, we are sure */
797 set_adr_mark(graph, node, MARK_ADDRESS_CALC);
799 mark_preds = MARK_REF_ADR;
802 unsigned mark = get_adr_mark(graph, node);
804 if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR) {
806 * this node has not an reference mode, but is only
807 * referenced by address calculations
809 mark_preds = MARK_REF_ADR;
813 /* mark all predecessors */
814 for (i = 0, n = get_irn_arity(node); i < n; ++i) {
815 ir_node *pred = get_irn_n(node, i);
817 set_adr_mark(graph, pred, get_adr_mark(graph, pred) | mark_preds);
822 * Called for every graph when the graph is either deleted or stat_dump_snapshot()
823 * is called, must recalculate all statistic info.
825 * @param global The global entry
826 * @param graph The current entry
828 static void update_graph_stat(graph_entry_t *global, graph_entry_t *graph)
832 /* clear first the alive counter in the graph */
833 for (entry = pset_first(graph->opcode_hash); entry; entry = pset_next(graph->opcode_hash)) {
834 cnt_clr(&entry->cnt_alive);
837 /* set pessimistic values */
839 graph->is_leaf_call = LCS_UNKNOWN;
840 graph->is_recursive = 0;
841 graph->is_chain_call = 1;
843 /* create new block counter */
844 graph->block_hash = new_pset(block_cmp, 5);
846 /* we need dominator info */
847 if (graph->irg != get_const_code_irg())
848 if (get_irg_dom_state(graph->irg) != dom_consistent)
849 compute_doms(graph->irg);
851 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
852 /* we need extended basic blocks */
853 compute_extbb(graph->irg);
855 /* create new extbb counter */
856 graph->extbb_hash = new_pset(block_cmp, 5);
859 /* count the nodes in the graph */
860 irg_walk_graph(graph->irg, update_node_stat, NULL, graph);
863 /* Uncomment this code if chain-call means call exact one */
864 entry = opcode_get_entry(op_Call, graph->opcode_hash);
866 /* check if we have more than 1 call */
867 if (cnt_gt(entry->cnt_alive, 1))
868 graph->is_chain_call = 0;
871 /* recursive functions are never chain calls, leafs don't have calls */
872 if (graph->is_recursive || graph->is_leaf)
873 graph->is_chain_call = 0;
875 /* assume we walk every graph only ONCE, we could sum here the global count */
876 for (entry = pset_first(graph->opcode_hash); entry; entry = pset_next(graph->opcode_hash)) {
877 node_entry_t *g_entry = opcode_get_entry(entry->op, global->opcode_hash);
879 /* update the node counter */
880 cnt_add(&g_entry->cnt_alive, &entry->cnt_alive);
883 /* update the edge counter */
884 cnt_add(&global->cnt_edges, &graph->cnt_edges);
886 /* count the number of address calculation */
887 if (graph->irg != get_const_code_irg()) {
888 ir_graph *rem = current_ir_graph;
890 if (get_irg_outs_state(graph->irg) != outs_consistent)
891 compute_irg_outs(graph->irg);
893 /* Must be done an the outs graph */
894 current_ir_graph = graph->irg;
895 irg_out_walk(get_irg_start(graph->irg), NULL, mark_address_calc, graph);
896 current_ir_graph = rem;
899 set_dump_node_vcgattr_hook(stat_adr_mark_hook);
900 dump_ir_block_graph(graph->irg, "-adr");
901 set_dump_node_vcgattr_hook(NULL);
905 /* count the DAG's */
906 if (status->stat_options & FIRMSTAT_COUNT_DAG)
907 count_dags_in_graph(global, graph);
909 /* calculate the patterns of this graph */
910 stat_calc_pattern_history(graph->irg);
912 /* leaf function did not call others */
914 graph->is_leaf_call = LCS_NON_LEAF_CALL;
915 else if (graph->is_leaf_call == LCS_UNKNOWN) {
916 /* we still don't know if this graph calls leaf-functions, so enqueue */
917 pdeq_putl(status->wait_q, graph);
920 /* we have analyzed this graph */
921 graph->is_analyzed = 1;
925 * Called for every graph that was on the wait_q in stat_dump_snapshot()
926 * must finish all statistic info calculations.
928 * @param global The global entry
929 * @param graph The current entry
931 static void update_graph_stat_2(graph_entry_t *global, graph_entry_t *graph)
933 if (graph->is_deleted) {
934 /* deleted, ignore */
939 /* count the nodes in the graph */
940 irg_walk_graph(graph->irg, update_node_stat_2, NULL, graph);
942 if (graph->is_leaf_call == LCS_UNKNOWN)
943 graph->is_leaf_call = LCS_LEAF_CALL;
950 static void stat_register_dumper(const dumper_t *dumper)
952 dumper_t *p = xmalloc(sizeof(*p));
957 p->next = status->dumper;
962 /* FIXME: memory leak */
968 static void stat_dump_graph(graph_entry_t *entry)
972 for (dumper = status->dumper; dumper; dumper = dumper->next) {
973 if (dumper->dump_graph)
974 dumper->dump_graph(dumper, entry);
979 * dumps a constant table
981 static void stat_dump_consts(const constant_info_t *tbl)
985 for (dumper = status->dumper; dumper; dumper = dumper->next) {
986 if (dumper->dump_const_tbl)
987 dumper->dump_const_tbl(dumper, tbl);
992 * initialize the dumper
994 static void stat_dump_init(const char *name)
998 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1000 dumper->init(dumper, name);
1007 static void stat_dump_finish(void)
1011 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1013 dumper->finish(dumper);
1017 /* ---------------------------------------------------------------------- */
1020 * helper: get an ir_op from an opcode
1022 ir_op *stat_get_op_from_opcode(opcode code)
1024 return opcode_find_entry(code, status->ir_op_hash);
1028 * A new IR op is registered.
1030 * @param ctx the hook context
1031 * @param op the new IR opcode that was created.
1033 static void stat_new_ir_op(void *ctx, ir_op *op)
1035 if (! status->stat_options)
1040 graph_entry_t *graph = graph_get_entry(NULL, status->irg_hash);
1042 /* execute for side effect :-) */
1043 opcode_get_entry(op, graph->opcode_hash);
1045 pset_insert(status->ir_op_hash, op, op->code);
1051 * An IR op is freed.
1053 * @param ctx the hook context
1054 * @param op the IR opcode that is freed
1056 static void stat_free_ir_op(void *ctx, ir_op *op)
1058 if (! status->stat_options)
1068 * A new node is created.
1070 * @param ctx the hook context
1071 * @param irg the IR graph on which the node is created
1072 * @param node the new IR node that was created
1074 static void stat_new_node(void *ctx, ir_graph *irg, ir_node *node)
1076 if (! status->stat_options)
1079 /* do NOT count during dead node elimination */
1080 if (status->in_dead_node_elim > 0)
1085 node_entry_t *entry;
1086 graph_entry_t *graph;
1087 ir_op *op = stat_get_irn_op(node);
1089 /* increase global value */
1090 graph = graph_get_entry(NULL, status->irg_hash);
1091 entry = opcode_get_entry(op, graph->opcode_hash);
1092 cnt_inc(&entry->new_node);
1094 /* increase local value */
1095 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1096 entry = opcode_get_entry(op, graph->opcode_hash);
1097 cnt_inc(&entry->new_node);
1103 * A node is changed into a Id node
1105 * @param ctx the hook context
1106 * @param node the IR node that will be turned into an ID
1108 static void stat_turn_into_id(void *ctx, ir_node *node)
1110 if (! status->stat_options)
1115 node_entry_t *entry;
1116 graph_entry_t *graph;
1117 ir_op *op = stat_get_irn_op(node);
1119 /* increase global value */
1120 graph = graph_get_entry(NULL, status->irg_hash);
1121 entry = opcode_get_entry(op, graph->opcode_hash);
1122 cnt_inc(&entry->into_Id);
1124 /* increase local value */
1125 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1126 entry = opcode_get_entry(op, graph->opcode_hash);
1127 cnt_inc(&entry->into_Id);
1133 * A new graph was created
1135 * @param ctx the hook context
1136 * @param irg the new IR graph that was created
1137 * @param ent the entity of this graph
1139 static void stat_new_graph(void *ctx, ir_graph *irg, entity *ent)
1141 if (! status->stat_options)
1146 /* execute for side effect :-) */
1147 graph_entry_t * graph = graph_get_entry(irg, status->irg_hash);
1150 graph->is_deleted = 0;
1152 graph->is_leaf_call = 0;
1153 graph->is_recursive = 0;
1154 graph->is_chain_call = 0;
1155 graph->is_analyzed = 0;
1161 * A graph will be deleted
1163 * @param ctx the hook context
1164 * @param irg the IR graph that will be deleted
1166 * Note that we still hold the information for this graph
1167 * in our hash maps, only a flag is set which prevents this
1168 * information from being changed, it's "frozen" from now.
1170 static void stat_free_graph(void *ctx, ir_graph *irg)
1172 if (! status->stat_options)
1177 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1178 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1180 graph->is_deleted = 1;
1182 if (status->stat_options & FIRMSTAT_COUNT_DELETED) {
1183 /* count the nodes of the graph yet, it will be destroyed later */
1184 update_graph_stat(global, graph);
1191 * A walk over a graph is initiated. Do not count walks from statistic code.
1193 * @param ctx the hook context
1194 * @param irg the IR graph that will be walked
1195 * @param pre the pre walker
1196 * @param post the post walker
1198 static void stat_irg_walk(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1200 if (! status->stat_options)
1205 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1207 cnt_inc(&graph->cnt_walked);
1213 * A walk over a graph in block-wise order is initiated. Do not count walks from statistic code.
1215 * @param ctx the hook context
1216 * @param irg the IR graph that will be walked
1217 * @param pre the pre walker
1218 * @param post the post walker
1220 static void stat_irg_walk_blkwise(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1222 /* for now, do NOT differentiate between blockwise and normal */
1223 stat_irg_walk(ctx, irg, pre, post);
1227 * A walk over the graph's blocks is initiated. Do not count walks from statistic code.
1229 * @param ctx the hook context
1230 * @param irg the IR graph that will be walked
1231 * @param node the IR node
1232 * @param pre the pre walker
1233 * @param post the post walker
1235 static void stat_irg_block_walk(void *ctx, ir_graph *irg, ir_node *node, generic_func *pre, generic_func *post)
1237 if (! status->stat_options)
1242 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1244 cnt_inc(&graph->cnt_walked_blocks);
1250 * called for every node that is removed due to an optimization.
1252 * @param n the IR node that will be removed
1253 * @param hmap the hash map containing ir_op* -> opt_entry_t*
1255 static void removed_due_opt(ir_node *n, hmap_opt_entry_t *hmap)
1257 ir_op *op = stat_get_irn_op(n);
1258 opt_entry_t *entry = opt_get_entry(op, hmap);
1260 /* increase global value */
1261 cnt_inc(&entry->count);
1265 * Some nodes were optimized into some others due to an optimization.
1267 * @param ctx the hook context
1269 static void stat_merge_nodes(
1271 ir_node **new_node_array, int new_num_entries,
1272 ir_node **old_node_array, int old_num_entries,
1275 if (! status->stat_options)
1281 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1283 if (status->reassoc_run)
1284 opt = HOOK_OPT_REASSOC;
1286 for (i = 0; i < old_num_entries; ++i) {
1287 for (j = 0; j < new_num_entries; ++j)
1288 if (old_node_array[i] == new_node_array[j])
1291 /* nodes might be in new and old, these are NOT removed */
1292 if (j >= new_num_entries) {
1295 /* sometimes we did not detect, that it is replaced by a Const */
1296 if (opt == HOOK_OPT_CONFIRM && new_num_entries == 1) {
1297 ir_op *op = get_irn_op(new_node_array[0]);
1299 if (op == op_Const || op == op_SymConst)
1300 xopt = HOOK_OPT_CONFIRM_C;
1303 removed_due_opt(old_node_array[i], graph->opt_hash[xopt]);
1311 * Reassociation is started/stopped.
1313 * @param ctx the hook context
1314 * @param flag if non-zero, reassociation is started else stopped
1316 static void stat_reassociate(void *ctx, int flag)
1318 if (! status->stat_options)
1323 status->reassoc_run = flag;
1329 * A node was lowered into other nodes
1331 * @param ctx the hook context
1332 * @param node the IR node that will be lowered
1334 static void stat_lower(void *ctx, ir_node *node)
1336 if (! status->stat_options)
1341 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1343 removed_due_opt(node, graph->opt_hash[HOOK_LOWERED]);
1349 * A graph was inlined.
1351 * @param ctx the hook context
1352 * @param call the IR call that will re changed into the body of
1353 * the called IR graph
1354 * @param called_irg the IR graph representing the called routine
1356 static void stat_inline(void *ctx, ir_node *call, ir_graph *called_irg)
1358 if (! status->stat_options)
1363 ir_graph *irg = get_irn_irg(call);
1364 graph_entry_t *i_graph = graph_get_entry(called_irg, status->irg_hash);
1365 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1367 cnt_inc(&graph->cnt_got_inlined);
1368 cnt_inc(&i_graph->cnt_was_inlined);
1374 * A graph with tail-recursions was optimized.
1376 * @param ctx the hook context
1378 static void stat_tail_rec(void *ctx, ir_graph *irg, int n_calls)
1380 if (! status->stat_options)
1385 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1387 graph->num_tail_recursion += n_calls;
1393 * Strength reduction was performed on an iteration variable.
1395 * @param ctx the hook context
1397 static void stat_strength_red(void *ctx, ir_graph *irg, ir_node *strong, ir_node *cmp)
1399 if (! status->stat_options)
1404 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1405 cnt_inc(&graph->cnt_strength_red);
1407 removed_due_opt(strong, graph->opt_hash[HOOK_OPT_STRENGTH_RED]);
1413 * Start/Stop the dead node elimination.
1415 * @param ctx the hook context
1417 static void stat_dead_node_elim(void *ctx, ir_graph *irg, int start)
1419 if (! status->stat_options)
1423 ++status->in_dead_node_elim;
1425 --status->in_dead_node_elim;
1429 * if-conversion was tried
1431 static void stat_if_conversion(void *context, ir_graph *irg, ir_node *phi,
1432 int pos, ir_node *mux, if_result_t reason)
1434 if (! status->stat_options)
1439 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1441 cnt_inc(&graph->cnt_if_conv[reason]);
1447 * real function call was optimized
1449 static void stat_func_call(void *context, ir_graph *irg, ir_node *call)
1451 if (! status->stat_options)
1456 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1458 cnt_inc(&graph->cnt_real_func_call);
1464 * A multiply was replaced by a series of Shifts/Adds/Subs
1466 * @param ctx the hook context
1468 static void stat_arch_dep_replace_mul_with_shifts(void *ctx, ir_node *mul)
1470 if (! status->stat_options)
1475 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1476 removed_due_opt(mul, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1482 * A division by const was replaced
1484 * @param ctx the hook context
1485 * @param node the division node that will be optimized
1487 static void stat_arch_dep_replace_division_by_const(void *ctx, ir_node *node)
1489 if (! status->stat_options)
1494 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1495 removed_due_opt(node, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1501 * Update the register pressure of a block
1503 * @param ctx the hook context
1504 * @param block the block for which the reg pressure should be set
1505 * @param irg the irg containing the block
1506 * @param pressure the pressure
1507 * @param class_name the ident name of the register class
1509 static void stat_be_block_regpressure(void *ctx, ir_node *block, ir_graph *irg, int pressure, ident *class_name)
1511 if (! status->stat_options)
1516 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1517 block_entry_t *block_ent;
1518 reg_pressure_entry_t *rp_ent;
1520 /* create new block counter */
1521 if (! graph->rp_block_hash)
1522 graph->rp_block_hash = new_pset(block_cmp, 5);
1524 block_ent = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(block), graph->rp_block_hash);
1525 rp_ent = obstack_alloc(&status->cnts, sizeof(*rp_ent));
1526 memset(rp_ent, 0, sizeof(*rp_ent));
1528 rp_ent->id_name = class_name;
1529 rp_ent->pressure = pressure;
1531 pset_insert(block_ent->reg_pressure, rp_ent, HASH_PTR(class_name));
1536 /* Dumps a statistics snapshot */
1537 void stat_dump_snapshot(const char *name, const char *phase)
1543 if (! status->stat_options)
1548 graph_entry_t *entry;
1549 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1552 * The constant counter is only global, so we clear it here.
1553 * Note that it does NOT contain the constants in DELETED
1554 * graphs due to this.
1556 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
1557 stat_const_clear(status);
1559 /* build the name */
1560 p = strrchr(name, '/');
1565 q = strrchr(name, '\\');
1567 /* NULL might be not the smallest pointer */
1568 if (q && (!p || q > p))
1576 if (l > sizeof(fname) - 1)
1577 l = sizeof(fname) - 1;
1579 memcpy(fname, name, l);
1586 strncat(fname, "firmstat-", sizeof(fname));
1587 strncat(fname, phase, sizeof(fname));
1588 strncat(fname, "-", sizeof(fname));
1589 strncat(fname, p, sizeof(fname));
1591 stat_dump_init(fname);
1593 /* calculate the graph statistics */
1594 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
1596 if (entry->irg == NULL) {
1597 /* special entry for the global count */
1601 if (! entry->is_deleted) {
1602 /* the graph is still alive, count the nodes on it */
1603 update_graph_stat(global, entry);
1607 /* some calculations are dependent, we pushed them on the wait_q */
1608 while (! pdeq_empty(status->wait_q)) {
1609 entry = pdeq_getr(status->wait_q);
1611 update_graph_stat_2(global, entry);
1615 /* dump per graph */
1616 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
1618 if (entry->irg == NULL) {
1619 /* special entry for the global count */
1623 if (! entry->is_deleted || status->stat_options & FIRMSTAT_COUNT_DELETED) {
1624 stat_dump_graph(entry);
1627 if (! entry->is_deleted) {
1628 /* clear the counter that are not accumulated */
1629 graph_clear_entry(entry, 0);
1634 stat_dump_graph(global);
1636 /* dump the const info */
1637 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
1638 stat_dump_consts(&status->const_info);
1642 stat_finish_pattern_history();
1644 /* clear the global counter here */
1646 node_entry_t *entry;
1648 for (entry = pset_first(global->opcode_hash); entry; entry = pset_next(global->opcode_hash)) {
1649 opcode_clear_entry(entry);
1651 /* clear all global counter */
1652 graph_clear_entry(global, 1);
1658 /** the hook entries for the Firm statistics module */
1659 static hook_entry_t stat_hooks[hook_last];
1661 /* initialize the statistics module. */
1662 void firm_init_stat(unsigned enable_options)
1664 #define X(a) a, sizeof(a)-1
1665 #define HOOK(h, fkt) \
1666 stat_hooks[h].hook._##h = fkt; register_hook(h, &stat_hooks[h])
1669 if (! (enable_options & FIRMSTAT_ENABLED))
1672 status = xmalloc(sizeof(*status));
1673 memset(status, 0, sizeof(*status));
1675 /* enable statistics */
1676 status->stat_options = enable_options & FIRMSTAT_ENABLED ? enable_options : 0;
1678 /* register all hooks */
1679 HOOK(hook_new_ir_op, stat_new_ir_op);
1680 HOOK(hook_free_ir_op, stat_free_ir_op);
1681 HOOK(hook_new_node, stat_new_node);
1682 HOOK(hook_turn_into_id, stat_turn_into_id);
1683 HOOK(hook_new_graph, stat_new_graph);
1684 HOOK(hook_free_graph, stat_free_graph);
1685 HOOK(hook_irg_walk, stat_irg_walk);
1686 HOOK(hook_irg_walk_blkwise, stat_irg_walk_blkwise);
1687 HOOK(hook_irg_block_walk, stat_irg_block_walk);
1688 HOOK(hook_merge_nodes, stat_merge_nodes);
1689 HOOK(hook_reassociate, stat_reassociate);
1690 HOOK(hook_lower, stat_lower);
1691 HOOK(hook_inline, stat_inline);
1692 HOOK(hook_tail_rec, stat_tail_rec);
1693 HOOK(hook_strength_red, stat_strength_red);
1694 HOOK(hook_dead_node_elim, stat_dead_node_elim);
1695 HOOK(hook_if_conversion, stat_if_conversion);
1696 HOOK(hook_func_call, stat_func_call);
1697 HOOK(hook_arch_dep_replace_mul_with_shifts, stat_arch_dep_replace_mul_with_shifts);
1698 HOOK(hook_arch_dep_replace_division_by_const, stat_arch_dep_replace_division_by_const);
1699 HOOK(hook_be_block_regpressure, stat_be_block_regpressure);
1701 obstack_init(&status->cnts);
1703 /* create the hash-tables */
1704 status->irg_hash = new_pset(graph_cmp, 8);
1705 status->ir_op_hash = new_pset(opcode_cmp_2, 1);
1707 /* create the wait queue */
1708 status->wait_q = new_pdeq();
1710 if (enable_options & FIRMSTAT_COUNT_STRONG_OP) {
1711 /* build the pseudo-ops */
1713 _op_Phi0.code = --num;
1714 _op_Phi0.name = new_id_from_chars(X("Phi0"));
1716 _op_PhiM.code = --num;
1717 _op_PhiM.name = new_id_from_chars(X("PhiM"));
1719 _op_ProjM.code = --num;
1720 _op_ProjM.name = new_id_from_chars(X("ProjM"));
1722 _op_MulC.code = --num;
1723 _op_MulC.name = new_id_from_chars(X("MulC"));
1725 _op_DivC.code = --num;
1726 _op_DivC.name = new_id_from_chars(X("DivC"));
1728 _op_ModC.code = --num;
1729 _op_ModC.name = new_id_from_chars(X("ModC"));
1731 _op_DivModC.code = --num;
1732 _op_DivModC.name = new_id_from_chars(X("DivModC"));
1734 status->op_Phi0 = &_op_Phi0;
1735 status->op_PhiM = &_op_PhiM;
1736 status->op_ProjM = &_op_ProjM;
1737 status->op_MulC = &_op_MulC;
1738 status->op_DivC = &_op_DivC;
1739 status->op_ModC = &_op_ModC;
1740 status->op_DivModC = &_op_DivModC;
1743 status->op_Phi0 = NULL;
1744 status->op_PhiM = NULL;
1745 status->op_ProjM = NULL;
1746 status->op_MulC = NULL;
1747 status->op_DivC = NULL;
1748 status->op_ModC = NULL;
1749 status->op_DivModC = NULL;
1752 if (enable_options & FIRMSTAT_COUNT_SELS) {
1753 _op_SelSel.code = --num;
1754 _op_SelSel.name = new_id_from_chars(X("Sel(Sel)"));
1756 _op_SelSelSel.code = --num;
1757 _op_SelSelSel.name = new_id_from_chars(X("Sel(Sel(Sel))"));
1759 status->op_SelSel = &_op_SelSel;
1760 status->op_SelSelSel = &_op_SelSelSel;
1763 status->op_SelSel = NULL;
1764 status->op_SelSelSel = NULL;
1767 /* register the dumper */
1768 stat_register_dumper(&simple_dumper);
1770 if (enable_options & FIRMSTAT_CSV_OUTPUT)
1771 stat_register_dumper(&csv_dumper);
1773 /* initialize the pattern hash */
1774 stat_init_pattern_history(enable_options & FIRMSTAT_PATTERN_ENABLED);
1776 /* initialize the Const options */
1777 if (enable_options & FIRMSTAT_COUNT_CONSTS)
1778 stat_init_const_cnt(status);
1784 /* terminates the statistics module, frees all memory */
1785 void stat_term(void) {
1786 if (status != (stat_info_t *)&status_disable) {
1788 status = (stat_info_t *)&status_disable;
1794 /* initialize the statistics module. */
1795 void firm_init_stat(unsigned enable_options) {}
1797 /* Dumps a statistics snapshot */
1798 void stat_dump_snapshot(const char *name, const char *phase) {}
1800 /* terminates the statistics module, frees all memory */
1801 void stat_term(void);
1803 #endif /* FIRM_STATISTICS */