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 ir_op hash
135 static int opcode_cmp_2(const void *elt, const void *key)
137 const ir_op *e1 = elt;
138 const ir_op *e2 = key;
140 return e1->code != e2->code;
144 * compare two elements of the address_mark set
146 static int address_mark_cmp(const void *elt, const void *key, size_t size)
148 const address_mark_entry_t *e1 = elt;
149 const address_mark_entry_t *e2 = key;
151 /* compare only the nodes, the rest is used as data container */
152 return e1->node != e2->node;
156 * clears all counter in a node_entry_t
158 static void opcode_clear_entry(node_entry_t *elem)
160 cnt_clr(&elem->cnt_alive);
161 cnt_clr(&elem->new_node);
162 cnt_clr(&elem->into_Id);
166 * Returns the associates node_entry_t for an ir_op
168 * @param op the IR operation
169 * @param hmap a hash map containing ir_op* -> node_entry_t*
171 static node_entry_t *opcode_get_entry(const ir_op *op, hmap_node_entry_t *hmap)
178 elem = pset_find(hmap, &key, op->code);
182 elem = obstack_alloc(&status->cnts, sizeof(*elem));
183 memset(elem, 0, sizeof(*elem));
186 opcode_clear_entry(elem);
190 return pset_insert(hmap, elem, op->code);
194 * Returns the associates ir_op for an opcode
196 * @param code the IR opcode
197 * @param hmap the hash map containing opcode -> ir_op*
199 static ir_op *opcode_find_entry(opcode code, hmap_ir_op *hmap)
204 return pset_find(hmap, &key, code);
208 * clears all counter in a graph_entry_t
210 static void graph_clear_entry(graph_entry_t *elem, int all)
213 cnt_clr(&elem->cnt_walked);
214 cnt_clr(&elem->cnt_walked_blocks);
215 cnt_clr(&elem->cnt_was_inlined);
216 cnt_clr(&elem->cnt_got_inlined);
217 cnt_clr(&elem->cnt_strength_red);
218 cnt_clr(&elem->cnt_real_func_call);
220 cnt_clr(&elem->cnt_edges);
221 cnt_clr(&elem->cnt_all_calls);
222 cnt_clr(&elem->cnt_call_with_cnst_arg);
223 cnt_clr(&elem->cnt_indirect_calls);
225 if (elem->block_hash) {
226 del_pset(elem->block_hash);
227 elem->block_hash = NULL;
230 if (elem->extbb_hash) {
231 del_pset(elem->extbb_hash);
232 elem->extbb_hash = NULL;
235 obstack_free(&status->recalc_cnts, NULL);
236 obstack_init(&status->recalc_cnts);
240 * Returns the associated graph_entry_t for an IR graph.
242 * @param irg the IR graph
243 * @param hmap the hash map containing ir_graph* -> graph_entry_t*
245 static graph_entry_t *graph_get_entry(ir_graph *irg, hmap_graph_entry_t *hmap)
253 elem = pset_find(hmap, &key, HASH_PTR(irg));
257 /* allocate a new one */
258 elem = obstack_alloc(&status->cnts, sizeof(*elem));
259 memset(elem, 0, sizeof(*elem));
262 graph_clear_entry(elem, 1);
264 /* new hash table for opcodes here */
265 elem->opcode_hash = new_pset(opcode_cmp, 5);
266 elem->address_mark = new_set(address_mark_cmp, 5);
269 /* these hash tables are created on demand */
270 elem->block_hash = NULL;
271 elem->extbb_hash = NULL;
273 for (i = 0; i < sizeof(elem->opt_hash)/sizeof(elem->opt_hash[0]); ++i)
274 elem->opt_hash[i] = new_pset(opt_cmp, 4);
276 return pset_insert(hmap, elem, HASH_PTR(irg));
280 * clears all counter in an opt_entry_t
282 static void opt_clear_entry(opt_entry_t *elem)
284 cnt_clr(&elem->count);
288 * Returns the associated opt_entry_t for an IR operation.
290 * @param op the IR operation
291 * @param hmap the hash map containing ir_op* -> opt_entry_t*
293 static opt_entry_t *opt_get_entry(const ir_op *op, hmap_opt_entry_t *hmap)
300 elem = pset_find(hmap, &key, op->code);
304 elem = obstack_alloc(&status->cnts, sizeof(*elem));
305 memset(elem, 0, sizeof(*elem));
307 /* clear new counter */
308 opt_clear_entry(elem);
312 return pset_insert(hmap, elem, op->code);
316 * clears all counter in a block_entry_t
318 static void block_clear_entry(block_entry_t *elem)
320 cnt_clr(&elem->cnt_nodes);
321 cnt_clr(&elem->cnt_edges);
322 cnt_clr(&elem->cnt_in_edges);
323 cnt_clr(&elem->cnt_out_edges);
324 cnt_clr(&elem->cnt_phi_data);
328 * Returns the associated block_entry_t for an block.
330 * @param block_nr an IR block number
331 * @param hmap a hash map containing long -> block_entry_t
333 static block_entry_t *block_get_entry(long block_nr, hmap_block_entry_t *hmap)
338 key.block_nr = block_nr;
340 elem = pset_find(hmap, &key, block_nr);
344 elem = obstack_alloc(&status->recalc_cnts, sizeof(*elem));
345 memset(elem, 0, sizeof(*elem));
347 /* clear new counter */
348 block_clear_entry(elem);
350 elem->block_nr = block_nr;
352 return pset_insert(hmap, elem, block_nr);
357 * Returns the ir_op for an IR-node,
358 * handles special cases and return pseudo op codes.
360 * @param none an IR node
362 static ir_op *stat_get_irn_op(ir_node *node)
364 ir_op *op = get_irn_op(node);
366 if (op == op_Phi && get_irn_arity(node) == 0) {
367 /* special case, a Phi0 node, count on extra counter */
368 op = status->op_Phi0 ? status->op_Phi0 : op;
370 else if (op == op_Phi && get_irn_mode(node) == mode_M) {
371 /* special case, a Memory Phi node, count on extra counter */
372 op = status->op_PhiM ? status->op_PhiM : op;
374 else if (op == op_Proj && get_irn_mode(node) == mode_M) {
375 /* special case, a Memory Proj node, count on extra counter */
376 op = status->op_ProjM ? status->op_ProjM : op;
378 else if (op == op_Mul &&
379 (get_irn_op(get_Mul_left(node)) == op_Const || get_irn_op(get_Mul_right(node)) == op_Const)) {
380 /* special case, a Multiply by a const, count on extra counter */
381 op = status->op_MulC ? status->op_MulC : op;
383 else if (op == op_Div && get_irn_op(get_Div_right(node)) == op_Const) {
384 /* special case, a division by a const, count on extra counter */
385 op = status->op_DivC ? status->op_DivC : op;
387 else if (op == op_Mod && get_irn_op(get_Mod_right(node)) == op_Const) {
388 /* special case, a module by a const, count on extra counter */
389 op = status->op_ModC ? status->op_ModC : op;
391 else if (op == op_DivMod && get_irn_op(get_DivMod_right(node)) == op_Const) {
392 /* special case, a division/modulo by a const, count on extra counter */
393 op = status->op_DivModC ? status->op_DivModC : op;
395 else if (op == op_Sel && get_irn_op(get_Sel_ptr(node)) == op_Sel) {
396 /* special case, a Sel of a Sel, count on extra counter */
397 op = status->op_SelSel ? status->op_SelSel : op;
399 if (get_irn_op(get_Sel_ptr(get_Sel_ptr(node))) == op_Sel) {
400 /* special case, a Sel of a Sel of a Sel, count on extra counter */
401 op = status->op_SelSelSel ? status->op_SelSelSel : op;
409 * update the block counter
411 static void undate_block_info(ir_node *node, graph_entry_t *graph)
413 ir_op *op = get_irn_op(node);
415 block_entry_t *b_entry;
418 /* check for block */
419 if (op == op_Block) {
420 arity = get_irn_arity(node);
421 b_entry = block_get_entry(get_irn_node_nr(node), graph->block_hash);
423 /* count all incoming edges */
424 for (i = 0; i < arity; ++i) {
425 ir_node *pred = get_irn_n(node, i);
426 ir_node *other_block = get_nodes_block(pred);
427 block_entry_t *b_entry_other = block_get_entry(get_irn_node_nr(other_block), graph->block_hash);
429 cnt_inc(&b_entry->cnt_in_edges); /* an edge coming from another block */
430 cnt_inc(&b_entry_other->cnt_out_edges);
435 block = get_nodes_block(node);
436 b_entry = block_get_entry(get_irn_node_nr(block), graph->block_hash);
438 if (op == op_Phi && mode_is_datab(get_irn_mode(node))) {
439 /* count data Phi per block */
440 cnt_inc(&b_entry->cnt_phi_data);
443 /* we have a new node in our block */
444 cnt_inc(&b_entry->cnt_nodes);
446 /* don't count keep-alive edges */
447 if (get_irn_op(node) == op_End)
450 arity = get_irn_arity(node);
452 for (i = 0; i < arity; ++i) {
453 ir_node *pred = get_irn_n(node, i);
454 ir_node *other_block;
456 other_block = get_nodes_block(pred);
458 if (other_block == block)
459 cnt_inc(&b_entry->cnt_edges); /* a in block edge */
461 block_entry_t *b_entry_other = block_get_entry(get_irn_node_nr(other_block), graph->block_hash);
463 cnt_inc(&b_entry->cnt_in_edges); /* an edge coming from another block */
464 cnt_inc(&b_entry_other->cnt_out_edges);
470 * update the extended block counter
472 static void undate_extbb_info(ir_node *node, graph_entry_t *graph)
474 ir_op *op = get_irn_op(node);
476 extbb_entry_t *eb_entry;
479 /* check for block */
480 if (op == op_Block) {
481 extbb = get_nodes_extbb(node);
482 arity = get_irn_arity(node);
483 eb_entry = block_get_entry(get_extbb_node_nr(extbb), graph->extbb_hash);
485 /* count all incoming edges */
486 for (i = 0; i < arity; ++i) {
487 ir_node *pred = get_irn_n(node, i);
488 ir_extblk *other_extbb = get_nodes_extbb(pred);
490 if (extbb != other_extbb) {
491 extbb_entry_t *eb_entry_other = block_get_entry(get_extbb_node_nr(other_extbb), graph->extbb_hash);
493 cnt_inc(&eb_entry->cnt_in_edges); /* an edge coming from another extbb */
494 cnt_inc(&eb_entry_other->cnt_out_edges);
500 extbb = get_nodes_extbb(node);
501 eb_entry = block_get_entry(get_extbb_node_nr(extbb), graph->extbb_hash);
503 if (op == op_Phi && mode_is_datab(get_irn_mode(node))) {
504 /* count data Phi per extbb */
505 cnt_inc(&eb_entry->cnt_phi_data);
508 /* we have a new node in our block */
509 cnt_inc(&eb_entry->cnt_nodes);
511 /* don't count keep-alive edges */
512 if (get_irn_op(node) == op_End)
515 arity = get_irn_arity(node);
517 for (i = 0; i < arity; ++i) {
518 ir_node *pred = get_irn_n(node, i);
519 ir_extblk *other_extbb = get_nodes_extbb(pred);
521 if (other_extbb == extbb)
522 cnt_inc(&eb_entry->cnt_edges); /* a in extbb edge */
524 extbb_entry_t *eb_entry_other = block_get_entry(get_extbb_node_nr(other_extbb), graph->extbb_hash);
526 cnt_inc(&eb_entry->cnt_in_edges); /* an edge coming from another extbb */
527 cnt_inc(&eb_entry_other->cnt_out_edges);
532 /** calculates how many arguments of the call are const */
533 static int cnt_const_args(ir_node *call)
536 int n = get_Call_n_params(call);
538 for (i = 0; i < n; ++i) {
539 ir_node *param = get_Call_param(call, i);
540 ir_op *op = get_irn_op(param);
542 if (op == op_Const || op == op_SymConst)
549 * update info on calls
551 * @param call The call
552 * @param graph The graph entry containing the call
554 static void stat_update_call(ir_node *call, graph_entry_t *graph)
556 ir_node *block = get_nodes_block(call);
557 ir_node *ptr = get_Call_ptr(call);
559 ir_graph *callee = NULL;
563 * If the block is bad, the whole subgraph will collapse later
564 * so do not count this call.
565 * This happens in dead code.
570 cnt_inc(&graph->cnt_all_calls);
572 /* found a call, this function is not a leaf */
575 if (get_irn_op(ptr) == op_SymConst) {
576 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
577 /* ok, we seems to know the entity */
578 ent = get_SymConst_entity(ptr);
579 callee = get_entity_irg(ent);
581 /* it is recursive, if it calls at least once */
582 if (callee == graph->irg)
583 graph->is_recursive = 1;
587 /* indirect call, be could not predict */
588 cnt_inc(&graph->cnt_indirect_calls);
590 /* NOT a leaf call */
591 graph->is_leaf_call = LCS_NON_LEAF_CALL;
594 /* check, if it's a chain-call: Then, the call-block
595 * must dominate the end block. */
597 ir_node *curr = get_irg_end_block(graph->irg);
598 int depth = get_Block_dom_depth(block);
600 for (; curr != block && get_Block_dom_depth(curr) > depth;) {
601 curr = get_Block_idom(curr);
603 if (! curr || is_no_Block(curr))
608 graph->is_chain_call = 0;
611 /* check, if the callee is a leaf */
613 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
615 if (called->is_analyzed) {
616 if (! called->is_leaf)
617 graph->is_leaf_call = LCS_NON_LEAF_CALL;
621 /* check, if arguments of the call are const */
622 num_const_args = cnt_const_args(call);
624 if (num_const_args > 0)
625 cnt_inc(&graph->cnt_call_with_cnst_arg);
629 * update info on calls for graphs on the wait queue
631 static void stat_update_call_2(ir_node *call, graph_entry_t *graph)
633 ir_node *block = get_nodes_block(call);
634 ir_node *ptr = get_Call_ptr(call);
636 ir_graph *callee = NULL;
639 * If the block is bad, the whole subgraph will collapse later
640 * so do not count this call.
641 * This happens in dead code.
646 if (get_irn_op(ptr) == op_SymConst) {
647 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
648 /* ok, we seems to know the entity */
649 ent = get_SymConst_entity(ptr);
650 callee = get_entity_irg(ent);
654 /* check, if the callee is a leaf */
656 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
658 assert(called->is_analyzed);
660 if (! called->is_leaf)
661 graph->is_leaf_call = LCS_NON_LEAF_CALL;
664 graph->is_leaf_call = LCS_NON_LEAF_CALL;
668 * walker for reachable nodes count
670 static void update_node_stat(ir_node *node, void *env)
672 graph_entry_t *graph = env;
675 ir_op *op = stat_get_irn_op(node);
676 int arity = get_irn_arity(node);
678 entry = opcode_get_entry(op, graph->opcode_hash);
680 cnt_inc(&entry->cnt_alive);
681 cnt_add_i(&graph->cnt_edges, arity);
683 /* count block edges */
684 undate_block_info(node, graph);
686 /* count extended block edges */
687 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
688 undate_extbb_info(node, graph);
691 /* handle statistics for special node types */
693 if (op == op_Const) {
694 if (status->stat_options & FIRMSTAT_COUNT_CONSTS) {
695 /* check properties of constants */
696 stat_update_const(status, node, graph);
699 else if (op == op_Call) {
700 /* check for properties that depends on calls like recursion/leaf/indirect call */
701 stat_update_call(node, graph);
706 * walker for reachable nodes count for graphs on the wait_q
708 static void update_node_stat_2(ir_node *node, void *env)
710 graph_entry_t *graph = env;
712 /* check for properties that depends on calls like recursion/leaf/indirect call */
713 if (get_irn_op(node) == op_Call)
714 stat_update_call_2(node, graph);
718 * get the current address mark
720 static unsigned get_adr_mark(graph_entry_t *graph, ir_node *node)
722 address_mark_entry_t *value = set_find(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
724 return value ? value->mark : 0;
728 * set the current address mark
730 static void set_adr_mark(graph_entry_t *graph, ir_node *node, unsigned val)
732 address_mark_entry_t *value = set_insert(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
738 * a vcg attribute hook: Color a node with a different color if
739 * it's identified as a part of an address expression or at least referenced
740 * by an address expression.
742 static int stat_adr_mark_hook(FILE *F, ir_node *node, ir_node *local)
744 ir_node *n = local ? local : node;
745 ir_graph *irg = get_irn_irg(n);
746 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
747 unsigned mark = get_adr_mark(graph, n);
749 if (mark & MARK_ADDRESS_CALC)
750 fprintf(F, "color: purple");
751 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
752 fprintf(F, "color: pink");
753 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
754 fprintf(F, "color: lightblue");
758 /* I know the color! */
763 * walker that marks every node that is an address calculation
765 * predecessor nodes must be visited first. We ensure this by
766 * calling in in the post of an outs walk. This should work even in cycles,
767 * while the pre in a normal walk will not.
769 static void mark_address_calc(ir_node *node, void *env)
771 graph_entry_t *graph = env;
772 ir_mode *mode = get_irn_mode(node);
774 unsigned mark_preds = MARK_REF_NON_ADR;
776 if (! mode_is_numP(mode))
779 if (mode_is_reference(mode)) {
780 /* a reference is calculated here, we are sure */
781 set_adr_mark(graph, node, MARK_ADDRESS_CALC);
783 mark_preds = MARK_REF_ADR;
786 unsigned mark = get_adr_mark(graph, node);
788 if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR) {
790 * this node has not an reference mode, but is only
791 * referenced by address calculations
793 mark_preds = MARK_REF_ADR;
797 /* mark all predecessors */
798 for (i = 0, n = get_irn_arity(node); i < n; ++i) {
799 ir_node *pred = get_irn_n(node, i);
801 set_adr_mark(graph, pred, get_adr_mark(graph, pred) | mark_preds);
806 * Called for every graph when the graph is either deleted or stat_dump_snapshot()
807 * is called, must recalculate all statistic info.
809 * @param global The global entry
810 * @param graph The current entry
812 static void update_graph_stat(graph_entry_t *global, graph_entry_t *graph)
816 /* clear first the alive counter in the graph */
817 for (entry = pset_first(graph->opcode_hash); entry; entry = pset_next(graph->opcode_hash)) {
818 cnt_clr(&entry->cnt_alive);
821 /* set pessimistic values */
823 graph->is_leaf_call = LCS_UNKNOWN;
824 graph->is_recursive = 0;
825 graph->is_chain_call = 1;
827 /* create new block counter */
828 graph->block_hash = new_pset(block_cmp, 5);
830 /* we need dominator info */
831 if (graph->irg != get_const_code_irg())
832 if (get_irg_dom_state(graph->irg) != dom_consistent)
833 compute_doms(graph->irg);
835 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
836 /* we need extended basic blocks */
837 compute_extbb(graph->irg);
839 /* create new extbb counter */
840 graph->extbb_hash = new_pset(block_cmp, 5);
843 /* count the nodes in the graph */
844 irg_walk_graph(graph->irg, update_node_stat, NULL, graph);
847 /* Uncomment this code if chain-call means call exact one */
848 entry = opcode_get_entry(op_Call, graph->opcode_hash);
850 /* check if we have more than 1 call */
851 if (cnt_gt(entry->cnt_alive, 1))
852 graph->is_chain_call = 0;
855 /* recursive functions are never chain calls, leafs don't have calls */
856 if (graph->is_recursive || graph->is_leaf)
857 graph->is_chain_call = 0;
859 /* assume we walk every graph only ONCE, we could sum here the global count */
860 for (entry = pset_first(graph->opcode_hash); entry; entry = pset_next(graph->opcode_hash)) {
861 node_entry_t *g_entry = opcode_get_entry(entry->op, global->opcode_hash);
863 /* update the node counter */
864 cnt_add(&g_entry->cnt_alive, &entry->cnt_alive);
867 /* update the edge counter */
868 cnt_add(&global->cnt_edges, &graph->cnt_edges);
870 /* count the number of address calculation */
871 if (graph->irg != get_const_code_irg()) {
872 ir_graph *rem = current_ir_graph;
874 if (get_irg_outs_state(graph->irg) != outs_consistent)
875 compute_irg_outs(graph->irg);
877 /* Must be done an the outs graph */
878 current_ir_graph = graph->irg;
879 irg_out_walk(get_irg_start(graph->irg), NULL, mark_address_calc, graph);
880 current_ir_graph = rem;
883 set_dump_node_vcgattr_hook(stat_adr_mark_hook);
884 dump_ir_block_graph(graph->irg, "-adr");
885 set_dump_node_vcgattr_hook(NULL);
889 /* count the DAG's */
890 if (status->stat_options & FIRMSTAT_COUNT_DAG)
891 count_dags_in_graph(global, graph);
893 /* calculate the patterns of this graph */
894 stat_calc_pattern_history(graph->irg);
896 /* leaf function did not call others */
898 graph->is_leaf_call = LCS_NON_LEAF_CALL;
899 else if (graph->is_leaf_call == LCS_UNKNOWN) {
900 /* we still don't know if this graph calls leaf-functions, so enqueue */
901 pdeq_putl(status->wait_q, graph);
904 /* we have analyzed this graph */
905 graph->is_analyzed = 1;
909 * Called for every graph that was on the wait_q in stat_dump_snapshot()
910 * must finish all statistic info calculations.
912 * @param global The global entry
913 * @param graph The current entry
915 static void update_graph_stat_2(graph_entry_t *global, graph_entry_t *graph)
917 if (graph->is_deleted) {
918 /* deleted, ignore */
923 /* count the nodes in the graph */
924 irg_walk_graph(graph->irg, update_node_stat_2, NULL, graph);
926 if (graph->is_leaf_call == LCS_UNKNOWN)
927 graph->is_leaf_call = LCS_LEAF_CALL;
934 static void stat_register_dumper(const dumper_t *dumper)
936 dumper_t *p = xmalloc(sizeof(*p));
941 p->next = status->dumper;
946 /* FIXME: memory leak */
952 static void stat_dump_graph(graph_entry_t *entry)
956 for (dumper = status->dumper; dumper; dumper = dumper->next) {
957 if (dumper->dump_graph)
958 dumper->dump_graph(dumper, entry);
963 * dumps a constant table
965 static void stat_dump_consts(const constant_info_t *tbl)
969 for (dumper = status->dumper; dumper; dumper = dumper->next) {
970 if (dumper->dump_const_tbl)
971 dumper->dump_const_tbl(dumper, tbl);
976 * initialize the dumper
978 static void stat_dump_init(const char *name)
982 for (dumper = status->dumper; dumper; dumper = dumper->next) {
984 dumper->init(dumper, name);
991 static void stat_dump_finish(void)
995 for (dumper = status->dumper; dumper; dumper = dumper->next) {
997 dumper->finish(dumper);
1001 /* ---------------------------------------------------------------------- */
1004 * helper: get an ir_op from an opcode
1006 ir_op *stat_get_op_from_opcode(opcode code)
1008 return opcode_find_entry(code, status->ir_op_hash);
1012 * A new IR op is registered.
1014 * @param ctx the hook context
1015 * @param op the new IR opcode that was created.
1017 static void stat_new_ir_op(void *ctx, ir_op *op)
1019 if (! status->stat_options)
1024 graph_entry_t *graph = graph_get_entry(NULL, status->irg_hash);
1026 /* execute for side effect :-) */
1027 opcode_get_entry(op, graph->opcode_hash);
1029 pset_insert(status->ir_op_hash, op, op->code);
1035 * An IR op is freed.
1037 * @param ctx the hook context
1038 * @param op the IR opcode that is freed
1040 static void stat_free_ir_op(void *ctx, ir_op *op)
1042 if (! status->stat_options)
1052 * A new node is created.
1054 * @param ctx the hook context
1055 * @param irg the IR graph on which the node is created
1056 * @param node the new IR node that was created
1058 static void stat_new_node(void *ctx, ir_graph *irg, ir_node *node)
1060 if (! status->stat_options)
1063 /* do NOT count during dead node elimination */
1064 if (status->in_dead_node_elim > 0)
1069 node_entry_t *entry;
1070 graph_entry_t *graph;
1071 ir_op *op = stat_get_irn_op(node);
1073 /* increase global value */
1074 graph = graph_get_entry(NULL, status->irg_hash);
1075 entry = opcode_get_entry(op, graph->opcode_hash);
1076 cnt_inc(&entry->new_node);
1078 /* increase local value */
1079 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1080 entry = opcode_get_entry(op, graph->opcode_hash);
1081 cnt_inc(&entry->new_node);
1087 * A node is changed into a Id node
1089 * @param ctx the hook context
1090 * @param node the IR node that will be turned into an ID
1092 static void stat_turn_into_id(void *ctx, ir_node *node)
1094 if (! status->stat_options)
1099 node_entry_t *entry;
1100 graph_entry_t *graph;
1101 ir_op *op = stat_get_irn_op(node);
1103 /* increase global value */
1104 graph = graph_get_entry(NULL, status->irg_hash);
1105 entry = opcode_get_entry(op, graph->opcode_hash);
1106 cnt_inc(&entry->into_Id);
1108 /* increase local value */
1109 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1110 entry = opcode_get_entry(op, graph->opcode_hash);
1111 cnt_inc(&entry->into_Id);
1117 * A new graph was created
1119 * @param ctx the hook context
1120 * @param irg the new IR graph that was created
1121 * @param ent the entity of this graph
1123 static void stat_new_graph(void *ctx, ir_graph *irg, entity *ent)
1125 if (! status->stat_options)
1130 /* execute for side effect :-) */
1131 graph_entry_t * graph = graph_get_entry(irg, status->irg_hash);
1134 graph->is_deleted = 0;
1136 graph->is_leaf_call = 0;
1137 graph->is_recursive = 0;
1138 graph->is_chain_call = 0;
1139 graph->is_analyzed = 0;
1145 * A graph will be deleted
1147 * @param ctx the hook context
1148 * @param irg the IR graph that will be deleted
1150 * Note that we still hold the information for this graph
1151 * in our hash maps, only a flag is set which prevents this
1152 * information from being changed, it's "frozen" from now.
1154 static void stat_free_graph(void *ctx, ir_graph *irg)
1156 if (! status->stat_options)
1161 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1162 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1164 graph->is_deleted = 1;
1166 if (status->stat_options & FIRMSTAT_COUNT_DELETED) {
1167 /* count the nodes of the graph yet, it will be destroyed later */
1168 update_graph_stat(global, graph);
1175 * A walk over a graph is initiated. Do not count walks from statistic code.
1177 * @param ctx the hook context
1178 * @param irg the IR graph that will be walked
1179 * @param pre the pre walker
1180 * @param post the post walker
1182 static void stat_irg_walk(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1184 if (! status->stat_options)
1189 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1191 cnt_inc(&graph->cnt_walked);
1197 * A walk over a graph in block-wise order is initiated. Do not count walks from statistic code.
1199 * @param ctx the hook context
1200 * @param irg the IR graph that will be walked
1201 * @param pre the pre walker
1202 * @param post the post walker
1204 static void stat_irg_walk_blkwise(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1206 /* for now, do NOT differentiate between blockwise and normal */
1207 stat_irg_walk(ctx, irg, pre, post);
1211 * A walk over the graph's blocks is initiated. Do not count walks from statistic code.
1213 * @param ctx the hook context
1214 * @param irg the IR graph that will be walked
1215 * @param node the IR node
1216 * @param pre the pre walker
1217 * @param post the post walker
1219 static void stat_irg_block_walk(void *ctx, ir_graph *irg, ir_node *node, generic_func *pre, generic_func *post)
1221 if (! status->stat_options)
1226 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1228 cnt_inc(&graph->cnt_walked_blocks);
1234 * called for every node that is removed due to an optimization.
1236 * @param n the IR node that will be removed
1237 * @param hmap the hash map containing ir_op* -> opt_entry_t*
1239 static void removed_due_opt(ir_node *n, hmap_opt_entry_t *hmap)
1241 ir_op *op = stat_get_irn_op(n);
1242 opt_entry_t *entry = opt_get_entry(op, hmap);
1244 /* increase global value */
1245 cnt_inc(&entry->count);
1249 * Some nodes were optimized into some others due to an optimization.
1251 * @param ctx the hook context
1253 static void stat_merge_nodes(
1255 ir_node **new_node_array, int new_num_entries,
1256 ir_node **old_node_array, int old_num_entries,
1259 if (! status->stat_options)
1265 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1267 if (status->reassoc_run)
1268 opt = HOOK_OPT_REASSOC;
1270 for (i = 0; i < old_num_entries; ++i) {
1271 for (j = 0; j < new_num_entries; ++j)
1272 if (old_node_array[i] == new_node_array[j])
1275 /* nodes might be in new and old, these are NOT removed */
1276 if (j >= new_num_entries) {
1279 /* sometimes we did not detect, that it is replaced by a Const */
1280 if (opt == HOOK_OPT_CONFIRM && new_num_entries == 1) {
1281 ir_op *op = get_irn_op(new_node_array[0]);
1283 if (op == op_Const || op == op_SymConst)
1284 xopt = HOOK_OPT_CONFIRM_C;
1287 removed_due_opt(old_node_array[i], graph->opt_hash[xopt]);
1295 * Reassociation is started/stopped.
1297 * @param ctx the hook context
1298 * @param flag if non-zero, reassociation is started else stopped
1300 static void stat_reassociate(void *ctx, int flag)
1302 if (! status->stat_options)
1307 status->reassoc_run = flag;
1313 * A node was lowered into other nodes
1315 * @param ctx the hook context
1316 * @param node the IR node that will be lowered
1318 static void stat_lower(void *ctx, ir_node *node)
1320 if (! status->stat_options)
1325 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1327 removed_due_opt(node, graph->opt_hash[HOOK_LOWERED]);
1333 * A graph was inlined.
1335 * @param ctx the hook context
1336 * @param call the IR call that will re changed into the body of
1337 * the called IR graph
1338 * @param called_irg the IR graph representing the called routine
1340 static void stat_inline(void *ctx, ir_node *call, ir_graph *called_irg)
1342 if (! status->stat_options)
1347 ir_graph *irg = get_irn_irg(call);
1348 graph_entry_t *i_graph = graph_get_entry(called_irg, status->irg_hash);
1349 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1351 cnt_inc(&graph->cnt_got_inlined);
1352 cnt_inc(&i_graph->cnt_was_inlined);
1358 * A graph with tail-recursions was optimized.
1360 * @param ctx the hook context
1362 static void stat_tail_rec(void *ctx, ir_graph *irg, int n_calls)
1364 if (! status->stat_options)
1369 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1371 graph->num_tail_recursion += n_calls;
1377 * Strength reduction was performed on an iteration variable.
1379 * @param ctx the hook context
1381 static void stat_strength_red(void *ctx, ir_graph *irg, ir_node *strong, ir_node *cmp)
1383 if (! status->stat_options)
1388 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1389 cnt_inc(&graph->cnt_strength_red);
1391 removed_due_opt(strong, graph->opt_hash[HOOK_OPT_STRENGTH_RED]);
1397 * Start the dead node elimination.
1399 * @param ctx the hook context
1401 static void stat_dead_node_elim_start(void *ctx, ir_graph *irg)
1403 if (! status->stat_options)
1406 ++status->in_dead_node_elim;
1410 * Stops the dead node elimination.
1412 * @param ctx the hook context
1414 static void stat_dead_node_elim_stop(void *ctx, ir_graph *irg)
1416 if (! status->stat_options)
1419 --status->in_dead_node_elim;
1423 * if-conversion was tried
1425 static void stat_if_conversion(void *context, ir_graph *irg, ir_node *phi,
1426 int pos, ir_node *mux, if_result_t reason)
1428 if (! status->stat_options)
1433 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1435 cnt_inc(&graph->cnt_if_conv[reason]);
1441 * real function call was optimized
1443 static void stat_func_call(void *context, ir_graph *irg, ir_node *call)
1445 if (! status->stat_options)
1450 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1452 cnt_inc(&graph->cnt_real_func_call);
1458 * A multiply was replaced by a series of Shifts/Adds/Subs
1460 * @param ctx the hook context
1462 static void stat_arch_dep_replace_mul_with_shifts(void *ctx, ir_node *mul)
1464 if (! status->stat_options)
1469 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1470 removed_due_opt(mul, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1476 * A division was replaced by a series of Shifts/Muls
1478 * @param ctx the hook context
1479 * @param div the div node that will be optimized
1481 static void stat_arch_dep_replace_div_by_const(void *ctx, ir_node *div)
1483 if (! status->stat_options)
1488 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1489 removed_due_opt(div, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1495 * A modulo was replaced by a series of Shifts/Muls
1497 * @param ctx the hook context
1498 * @param mod the mod node that will be optimized
1500 static void stat_arch_dep_replace_mod_by_const(void *ctx, ir_node *mod)
1502 if (! status->stat_options)
1507 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1508 removed_due_opt(mod, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1514 * A DivMod was replaced by a series of Shifts/Muls
1516 * @param ctx the hook context
1517 * @param divmod the divmod node that will be optimized
1519 static void stat_arch_dep_replace_DivMod_by_const(void *ctx, ir_node *divmod)
1521 if (! status->stat_options)
1526 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1527 removed_due_opt(divmod, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1532 /* Dumps a statistics snapshot */
1533 void stat_dump_snapshot(const char *name, const char *phase)
1539 if (! status->stat_options)
1544 graph_entry_t *entry;
1545 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1548 * The constant counter is only global, so we clear it here.
1549 * Note that it does NOT contain the constants in DELETED
1550 * graphs due to this.
1552 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
1553 stat_const_clear(status);
1555 /* build the name */
1556 p = strrchr(name, '/');
1561 q = strrchr(name, '\\');
1563 /* NULL might be not the smallest pointer */
1564 if (q && (!p || q > p))
1572 if (l > sizeof(fname) - 1)
1573 l = sizeof(fname) - 1;
1575 memcpy(fname, name, l);
1582 strncat(fname, "firmstat-", sizeof(fname));
1583 strncat(fname, phase, sizeof(fname));
1584 strncat(fname, "-", sizeof(fname));
1585 strncat(fname, p, sizeof(fname));
1587 stat_dump_init(fname);
1589 /* calculate the graph statistics */
1590 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
1592 if (entry->irg == NULL) {
1593 /* special entry for the global count */
1597 if (! entry->is_deleted) {
1598 /* the graph is still alive, count the nodes on it */
1599 update_graph_stat(global, entry);
1603 /* some calculations are dependent, we pushed them on the wait_q */
1604 while (! pdeq_empty(status->wait_q)) {
1605 entry = pdeq_getr(status->wait_q);
1607 update_graph_stat_2(global, entry);
1611 /* dump per graph */
1612 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
1614 if (entry->irg == NULL) {
1615 /* special entry for the global count */
1619 if (! entry->is_deleted || status->stat_options & FIRMSTAT_COUNT_DELETED)
1620 stat_dump_graph(entry);
1622 if (! entry->is_deleted) {
1623 /* clear the counter that are not accumulated */
1624 graph_clear_entry(entry, 0);
1629 stat_dump_graph(global);
1631 /* dump the const info */
1632 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
1633 stat_dump_consts(&status->const_info);
1637 stat_finish_pattern_history();
1639 /* clear the global counter here */
1641 node_entry_t *entry;
1643 for (entry = pset_first(global->opcode_hash); entry; entry = pset_next(global->opcode_hash)) {
1644 opcode_clear_entry(entry);
1646 /* clear all global counter */
1647 graph_clear_entry(global, 1);
1653 /** the hook entries for the Firm statistics module */
1654 static hook_entry_t stat_hooks[hook_last];
1656 /* initialize the statistics module. */
1657 void init_stat(unsigned enable_options)
1659 #define X(a) a, sizeof(a)-1
1660 #define HOOK(h, fkt) \
1661 stat_hooks[h].hook._##h = fkt; register_hook(h, &stat_hooks[h])
1663 if (! (enable_options & FIRMSTAT_ENABLED))
1666 status = xmalloc(sizeof(*status));
1667 memset(status, 0, sizeof(*status));
1669 /* enable statistics */
1670 status->stat_options = enable_options & FIRMSTAT_ENABLED ? enable_options : 0;
1672 /* register all hooks */
1673 HOOK(hook_new_ir_op, stat_new_ir_op);
1674 HOOK(hook_free_ir_op, stat_free_ir_op);
1675 HOOK(hook_new_node, stat_new_node);
1676 HOOK(hook_turn_into_id, stat_turn_into_id);
1677 HOOK(hook_new_graph, stat_new_graph);
1678 HOOK(hook_free_graph, stat_free_graph);
1679 HOOK(hook_irg_walk, stat_irg_walk);
1680 HOOK(hook_irg_walk_blkwise, stat_irg_walk_blkwise);
1681 HOOK(hook_irg_block_walk, stat_irg_block_walk);
1682 HOOK(hook_merge_nodes, stat_merge_nodes);
1683 HOOK(hook_reassociate, stat_reassociate);
1684 HOOK(hook_lower, stat_lower);
1685 HOOK(hook_inline, stat_inline);
1686 HOOK(hook_tail_rec, stat_tail_rec);
1687 HOOK(hook_strength_red, stat_strength_red);
1688 HOOK(hook_dead_node_elim_start, stat_dead_node_elim_start);
1689 HOOK(hook_dead_node_elim_stop, stat_dead_node_elim_stop);
1690 HOOK(hook_if_conversion, stat_if_conversion);
1691 HOOK(hook_func_call, stat_func_call);
1692 HOOK(hook_arch_dep_replace_mul_with_shifts, stat_arch_dep_replace_mul_with_shifts);
1693 HOOK(hook_arch_dep_replace_div_by_const, stat_arch_dep_replace_div_by_const);
1694 HOOK(hook_arch_dep_replace_mod_by_const, stat_arch_dep_replace_mod_by_const);
1695 HOOK(hook_arch_dep_replace_DivMod_by_const, stat_arch_dep_replace_DivMod_by_const);
1697 obstack_init(&status->cnts);
1698 obstack_init(&status->recalc_cnts);
1700 /* create the hash-tables */
1701 status->irg_hash = new_pset(graph_cmp, 8);
1702 status->ir_op_hash = new_pset(opcode_cmp_2, 1);
1704 /* create the wait queue */
1705 status->wait_q = new_pdeq();
1707 if (enable_options & FIRMSTAT_COUNT_STRONG_OP) {
1708 /* build the pseudo-ops */
1709 _op_Phi0.code = get_next_ir_opcode();
1710 _op_Phi0.name = new_id_from_chars(X("Phi0"));
1712 _op_PhiM.code = get_next_ir_opcode();
1713 _op_PhiM.name = new_id_from_chars(X("PhiM"));
1715 _op_ProjM.code = get_next_ir_opcode();
1716 _op_ProjM.name = new_id_from_chars(X("ProjM"));
1718 _op_MulC.code = get_next_ir_opcode();
1719 _op_MulC.name = new_id_from_chars(X("MulC"));
1721 _op_DivC.code = get_next_ir_opcode();
1722 _op_DivC.name = new_id_from_chars(X("DivC"));
1724 _op_ModC.code = get_next_ir_opcode();
1725 _op_ModC.name = new_id_from_chars(X("ModC"));
1727 _op_DivModC.code = get_next_ir_opcode();
1728 _op_DivModC.name = new_id_from_chars(X("DivModC"));
1730 status->op_Phi0 = &_op_Phi0;
1731 status->op_PhiM = &_op_PhiM;
1732 status->op_ProjM = &_op_ProjM;
1733 status->op_MulC = &_op_MulC;
1734 status->op_DivC = &_op_DivC;
1735 status->op_ModC = &_op_ModC;
1736 status->op_DivModC = &_op_DivModC;
1739 status->op_Phi0 = NULL;
1740 status->op_PhiM = NULL;
1741 status->op_ProjM = NULL;
1742 status->op_MulC = NULL;
1743 status->op_DivC = NULL;
1744 status->op_ModC = NULL;
1745 status->op_DivModC = NULL;
1748 if (enable_options & FIRMSTAT_COUNT_SELS) {
1749 _op_SelSel.code = get_next_ir_opcode();
1750 _op_SelSel.name = new_id_from_chars(X("Sel(Sel)"));
1752 _op_SelSelSel.code = get_next_ir_opcode();
1753 _op_SelSelSel.name = new_id_from_chars(X("Sel(Sel(Sel))"));
1755 status->op_SelSel = &_op_SelSel;
1756 status->op_SelSelSel = &_op_SelSelSel;
1759 status->op_SelSel = NULL;
1760 status->op_SelSelSel = NULL;
1763 /* register the dumper */
1764 stat_register_dumper(&simple_dumper);
1766 if (enable_options & FIRMSTAT_CSV_OUTPUT)
1767 stat_register_dumper(&csv_dumper);
1769 /* initialize the pattern hash */
1770 stat_init_pattern_history(enable_options & FIRMSTAT_PATTERN_ENABLED);
1772 /* initialize the Const options */
1773 if (enable_options & FIRMSTAT_COUNT_CONSTS)
1774 stat_init_const_cnt(status);
1780 /* terminates the statistics module, frees all memory */
1781 void stat_term(void) {
1782 if (status != (stat_info_t *)&status_disable) {
1784 status = (stat_info_t *)&status_disable;
1790 /* initialize the statistics module. */
1791 void init_stat(unsigned enable_options) {}
1793 /* Dumps a statistics snapshot */
1794 void stat_dump_snapshot(const char *name, const char *phase) {}
1796 /* terminates the statistics module, frees all memory */
1797 void stat_term(void);
1799 #endif /* FIRM_STATISTICS */