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;
65 /* ---------------------------------------------------------------------------------- */
67 /** Marks the begin of a statistic (hook) function. */
68 #define STAT_ENTER ++status->recursive
70 /** Marks the end of a statistic (hook) functions. */
71 #define STAT_LEAVE --status->recursive
73 /** Allows to enter a statistic function only when we are not already in a hook. */
74 #define STAT_ENTER_SINGLE do { if (status->recursive > 0) return; ++status->recursive; } while (0)
79 static stat_info_t _status, *status = &_status;
82 * compare two elements of the opcode hash
84 static int opcode_cmp(const void *elt, const void *key)
86 const node_entry_t *e1 = elt;
87 const node_entry_t *e2 = key;
89 return e1->op->code - e2->op->code;
93 * compare two elements of the graph hash
95 static int graph_cmp(const void *elt, const void *key)
97 const graph_entry_t *e1 = elt;
98 const graph_entry_t *e2 = key;
100 return e1->irg != e2->irg;
104 * compare two elements of the optimization hash
106 static int opt_cmp(const void *elt, const void *key)
108 const opt_entry_t *e1 = elt;
109 const opt_entry_t *e2 = key;
111 return e1->op->code != e2->op->code;
115 * compare two elements of the block hash
117 static int block_cmp(const void *elt, const void *key)
119 const block_entry_t *e1 = elt;
120 const block_entry_t *e2 = key;
122 return e1->block_nr != e2->block_nr;
126 * compare two elements of the ir_op hash
128 static int opcode_cmp_2(const void *elt, const void *key)
130 const ir_op *e1 = elt;
131 const ir_op *e2 = key;
133 return e1->code != e2->code;
137 * compare two elements of the address_mark set
139 static int address_mark_cmp(const void *elt, const void *key, size_t size)
141 const address_mark_entry_t *e1 = elt;
142 const address_mark_entry_t *e2 = key;
144 /* compare only the nodes, the rest is used as data container */
145 return e1->node != e2->node;
149 * clears all counter in a node_entry_t
151 static void opcode_clear_entry(node_entry_t *elem)
153 cnt_clr(&elem->cnt_alive);
154 cnt_clr(&elem->new_node);
155 cnt_clr(&elem->into_Id);
159 * Returns the associates node_entry_t for an ir_op
161 * @param op the IR operation
162 * @param hmap a hash map containing ir_op* -> node_entry_t*
164 static node_entry_t *opcode_get_entry(const ir_op *op, hmap_node_entry_t *hmap)
171 elem = pset_find(hmap, &key, op->code);
175 elem = obstack_alloc(&status->cnts, sizeof(*elem));
176 memset(elem, 0, sizeof(*elem));
179 opcode_clear_entry(elem);
183 return pset_insert(hmap, elem, op->code);
187 * Returns the associates ir_op for an opcode
189 * @param code the IR opcode
190 * @param hmap the hash map containing opcode -> ir_op*
192 static ir_op *opcode_find_entry(opcode code, hmap_ir_op *hmap)
197 return pset_find(hmap, &key, code);
201 * clears all counter in a graph_entry_t
203 static void graph_clear_entry(graph_entry_t *elem, int all)
206 cnt_clr(&elem->cnt_walked);
207 cnt_clr(&elem->cnt_walked_blocks);
208 cnt_clr(&elem->cnt_was_inlined);
209 cnt_clr(&elem->cnt_got_inlined);
210 cnt_clr(&elem->cnt_strength_red);
211 cnt_clr(&elem->cnt_real_func_call);
213 cnt_clr(&elem->cnt_edges);
214 cnt_clr(&elem->cnt_all_calls);
215 cnt_clr(&elem->cnt_call_with_cnst_arg);
216 cnt_clr(&elem->cnt_indirect_calls);
220 * Returns the associated graph_entry_t for an IR graph.
222 * @param irg the IR graph
223 * @param hmap the hash map containing ir_graph* -> graph_entry_t*
225 static graph_entry_t *graph_get_entry(ir_graph *irg, hmap_graph_entry_t *hmap)
233 elem = pset_find(hmap, &key, HASH_PTR(irg));
237 /* allocate a new one */
238 elem = obstack_alloc(&status->cnts, sizeof(*elem));
239 memset(elem, 0, sizeof(*elem));
242 graph_clear_entry(elem, 1);
244 /* new hash table for opcodes here */
245 elem->opcode_hash = new_pset(opcode_cmp, 5);
246 elem->block_hash = new_pset(block_cmp, 5);
247 elem->address_mark = new_set(address_mark_cmp, 5);
250 for (i = 0; i < sizeof(elem->opt_hash)/sizeof(elem->opt_hash[0]); ++i)
251 elem->opt_hash[i] = new_pset(opt_cmp, 4);
253 return pset_insert(hmap, elem, HASH_PTR(irg));
257 * clears all counter in an opt_entry_t
259 static void opt_clear_entry(opt_entry_t *elem)
261 cnt_clr(&elem->count);
265 * Returns the associated opt_entry_t for an IR operation.
267 * @param op the IR operation
268 * @param hmap the hash map containing ir_op* -> opt_entry_t*
270 static opt_entry_t *opt_get_entry(const ir_op *op, hmap_opt_entry_t *hmap)
277 elem = pset_find(hmap, &key, op->code);
281 elem = obstack_alloc(&status->cnts, sizeof(*elem));
282 memset(elem, 0, sizeof(*elem));
284 /* clear new counter */
285 opt_clear_entry(elem);
289 return pset_insert(hmap, elem, op->code);
293 * clears all counter in a block_entry_t
295 static void block_clear_entry(block_entry_t *elem)
297 cnt_clr(&elem->cnt_nodes);
298 cnt_clr(&elem->cnt_edges);
299 cnt_clr(&elem->cnt_in_edges);
300 cnt_clr(&elem->cnt_out_edges);
304 * Returns the associated block_entry_t for an block.
306 * @param block_nr an IR block number
307 * @param hmap a hash map containing long -> block_entry_t
309 static block_entry_t *block_get_entry(long block_nr, hmap_block_entry_t *hmap)
314 key.block_nr = block_nr;
316 elem = pset_find(hmap, &key, block_nr);
320 elem = obstack_alloc(&status->cnts, sizeof(*elem));
321 memset(elem, 0, sizeof(*elem));
323 /* clear new counter */
324 block_clear_entry(elem);
326 elem->block_nr = block_nr;
328 return pset_insert(hmap, elem, block_nr);
333 * Returns the ir_op for an IR-node,
334 * handles special cases and return pseudo op codes.
336 * @param none an IR node
338 static ir_op *stat_get_irn_op(ir_node *node)
340 ir_op *op = get_irn_op(node);
342 if (op == op_Phi && get_irn_arity(node) == 0) {
343 /* special case, a Phi0 node, count on extra counter */
344 op = status->op_Phi0 ? status->op_Phi0 : op;
346 else if (op == op_Phi && get_irn_mode(node) == mode_M) {
347 /* special case, a Memory Phi node, count on extra counter */
348 op = status->op_PhiM ? status->op_PhiM : op;
350 else if (op == op_Proj && get_irn_mode(node) == mode_M) {
351 /* special case, a Memory Proj node, count on extra counter */
352 op = status->op_ProjM ? status->op_ProjM : op;
354 else if (op == op_Mul &&
355 (get_irn_op(get_Mul_left(node)) == op_Const || get_irn_op(get_Mul_right(node)) == op_Const)) {
356 /* special case, a Multiply by a const, count on extra counter */
357 op = status->op_MulC ? status->op_MulC : op;
359 else if (op == op_Div && get_irn_op(get_Div_right(node)) == op_Const) {
360 /* special case, a division by a const, count on extra counter */
361 op = status->op_DivC ? status->op_DivC : op;
363 else if (op == op_Mod && get_irn_op(get_Mod_right(node)) == op_Const) {
364 /* special case, a module by a const, count on extra counter */
365 op = status->op_ModC ? status->op_ModC : op;
367 else if (op == op_DivMod && get_irn_op(get_DivMod_right(node)) == op_Const) {
368 /* special case, a division/modulo by a const, count on extra counter */
369 op = status->op_DivModC ? status->op_DivModC : op;
376 * update the block counter
378 static void undate_block_info(ir_node *node, graph_entry_t *graph)
380 ir_op *op = get_irn_op(node);
382 block_entry_t *b_entry;
385 /* check for block */
386 if (op == op_Block) {
387 arity = get_irn_arity(node);
388 b_entry = block_get_entry(get_irn_node_nr(node), graph->block_hash);
390 /* count all incoming edges */
391 for (i = 0; i < arity; ++i) {
392 ir_node *pred = get_irn_n(node, i);
393 ir_node *other_block = get_nodes_block(pred);
394 block_entry_t *b_entry_other = block_get_entry(get_irn_node_nr(other_block), graph->block_hash);
396 cnt_inc(&b_entry->cnt_in_edges); /* an edge coming from another block */
397 cnt_inc(&b_entry_other->cnt_out_edges);
402 block = get_nodes_block(node);
403 b_entry = block_get_entry(get_irn_node_nr(block), graph->block_hash);
405 /* we have a new nodes */
406 cnt_inc(&b_entry->cnt_nodes);
408 arity = get_irn_arity(node);
410 for (i = 0; i < arity; ++i) {
411 ir_node *pred = get_irn_n(node, i);
412 ir_node *other_block;
414 if (get_irn_op(pred) == op_Block)
417 other_block = get_nodes_block(pred);
419 if (other_block == block)
420 cnt_inc(&b_entry->cnt_edges); /* a in block edge */
422 block_entry_t *b_entry_other = block_get_entry(get_irn_node_nr(other_block), graph->block_hash);
424 cnt_inc(&b_entry->cnt_in_edges); /* an edge coming from another block */
425 cnt_inc(&b_entry_other->cnt_out_edges);
430 /** calculates how many arguments of the call are const */
431 static int cnt_const_args(ir_node *call)
434 int n = get_Call_n_params(call);
436 for (i = 0; i < n; ++i) {
437 ir_node *param = get_Call_param(call, i);
438 ir_op *op = get_irn_op(param);
440 if (op == op_Const || op == op_SymConst)
447 * update info on calls
449 * @param call The call
450 * @param graph The graph entry containing the call
452 static void update_call_stat(ir_node *call, graph_entry_t *graph)
454 ir_node *block = get_nodes_block(call);
455 ir_node *ptr = get_Call_ptr(call);
457 ir_graph *callee = NULL;
461 * If the block is bad, the whole subgraph will collapse later
462 * so do not count this call.
463 * This happens in dead code.
468 cnt_inc(&graph->cnt_all_calls);
470 /* found a call, this function is not a leaf */
473 if (get_irn_op(ptr) == op_SymConst) {
474 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
475 /* ok, we seems to know the entity */
476 ent = get_SymConst_entity(ptr);
477 callee = get_entity_irg(ent);
479 /* it is recursive, if it calls at least once */
480 if (callee == graph->irg)
481 graph->is_recursive = 1;
485 /* indirect call, be could not predict */
486 cnt_inc(&graph->cnt_indirect_calls);
488 /* NOT a leaf call */
489 graph->is_leaf_call = LCS_NON_LEAF_CALL;
492 /* check, if it's a chain-call: Then, the call-block
493 * must dominate the end block. */
495 ir_node *curr = get_irg_end_block(graph->irg);
496 int depth = get_Block_dom_depth(block);
498 for (; curr != block && get_Block_dom_depth(curr) > depth;) {
499 curr = get_Block_idom(curr);
501 if (! curr || is_no_Block(curr))
506 graph->is_chain_call = 0;
509 /* check, if the callee is a leaf */
511 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
513 if (called->is_analyzed) {
514 if (! called->is_leaf)
515 graph->is_leaf_call = LCS_NON_LEAF_CALL;
519 /* check, if arguments of the call are const */
520 num_const_args = cnt_const_args(call);
522 if (num_const_args > 0)
523 cnt_inc(&graph->cnt_call_with_cnst_arg);
527 * update info on calls for graphs on the wait queue
529 static void update_call_stat_2(ir_node *call, graph_entry_t *graph)
531 ir_node *block = get_nodes_block(call);
532 ir_node *ptr = get_Call_ptr(call);
534 ir_graph *callee = NULL;
537 * If the block is bad, the whole subgraph will collapse later
538 * so do not count this call.
539 * This happens in dead code.
544 if (get_irn_op(ptr) == op_SymConst) {
545 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
546 /* ok, we seems to know the entity */
547 ent = get_SymConst_entity(ptr);
548 callee = get_entity_irg(ent);
552 /* check, if the callee is a leaf */
554 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
556 assert(called->is_analyzed);
558 if (! called->is_leaf)
559 graph->is_leaf_call = LCS_NON_LEAF_CALL;
562 graph->is_leaf_call = LCS_NON_LEAF_CALL;
566 * walker for reachable nodes count
568 static void update_node_stat(ir_node *node, void *env)
570 graph_entry_t *graph = env;
573 ir_op *op = stat_get_irn_op(node);
574 int arity = get_irn_arity(node);
576 entry = opcode_get_entry(op, graph->opcode_hash);
578 cnt_inc(&entry->cnt_alive);
579 cnt_add_i(&graph->cnt_edges, arity);
581 /* count block edges */
582 undate_block_info(node, graph);
584 /* check for properties that depends on calls like recursion/leaf/indirect call */
586 update_call_stat(node, graph);
590 * walker for reachable nodes count for graphs on the wait_q
592 static void update_node_stat_2(ir_node *node, void *env)
594 graph_entry_t *graph = env;
596 /* check for properties that depends on calls like recursion/leaf/indirect call */
597 if (get_irn_op(node) == op_Call)
598 update_call_stat_2(node, graph);
602 * get the current address mark
604 static unsigned get_adr_mark(graph_entry_t *graph, ir_node *node)
606 address_mark_entry_t *value = set_find(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
608 return value ? value->mark : 0;
612 * set the current address mark
614 static void set_adr_mark(graph_entry_t *graph, ir_node *node, unsigned val)
616 address_mark_entry_t *value = set_insert(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
622 * a vcg attribute hook: Color a node with a different color if
623 * it's identified as a part of an address expression or at least referenced
624 * by an address expression.
626 static int stat_adr_mark_hook(FILE *F, ir_node *node, ir_node *local)
628 ir_node *n = local ? local : node;
629 ir_graph *irg = get_irn_irg(n);
630 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
631 unsigned mark = get_adr_mark(graph, n);
633 if (mark & MARK_ADDRESS_CALC)
634 fprintf(F, "color: purple");
635 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
636 fprintf(F, "color: pink");
637 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
638 fprintf(F, "color: lightblue");
642 /* I know the color! */
647 * walker that marks every node that is an address calculation
649 * predecessor nodes must be visited first. We ensure this by
650 * calling in in the post of an outs walk. This should work even in cycles,
651 * while the pre in a normal walk will not.
653 static void mark_address_calc(ir_node *node, void *env)
655 graph_entry_t *graph = env;
656 ir_mode *mode = get_irn_mode(node);
658 unsigned mark_preds = MARK_REF_NON_ADR;
660 if (! mode_is_numP(mode))
663 if (mode_is_reference(mode)) {
664 /* a reference is calculated here, we are sure */
665 set_adr_mark(graph, node, MARK_ADDRESS_CALC);
667 mark_preds = MARK_REF_ADR;
670 unsigned mark = get_adr_mark(graph, node);
672 if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR) {
674 * this node has not an reference mode, but is only
675 * referenced by address calculations
677 mark_preds = MARK_REF_ADR;
681 /* mark all predecessors */
682 for (i = 0, n = get_irn_arity(node); i < n; ++i) {
683 ir_node *pred = get_irn_n(node, i);
685 set_adr_mark(graph, pred, get_adr_mark(graph, pred) | mark_preds);
690 * Called for every graph when the graph is either deleted or stat_finish()
691 * is called, must recalculate all statistic info.
693 * @param global The global entry
694 * @param graph The current entry
696 static void update_graph_stat(graph_entry_t *global, graph_entry_t *graph)
700 /* clear first the alive counter in the graph */
701 for (entry = pset_first(graph->opcode_hash); entry; entry = pset_next(graph->opcode_hash)) {
702 cnt_clr(&entry->cnt_alive);
705 /* set pessimistic values */
707 graph->is_leaf_call = LCS_UNKNOWN;
708 graph->is_recursive = 0;
709 graph->is_chain_call = 1;
711 /* we need dominator info */
712 if (graph->irg != get_const_code_irg())
713 if (get_irg_dom_state(graph->irg) != dom_consistent)
714 compute_doms(graph->irg);
716 /* count the nodes in the graph */
717 irg_walk_graph(graph->irg, update_node_stat, NULL, graph);
720 entry = opcode_get_entry(op_Call, graph->opcode_hash);
722 /* check if we have more than 1 call */
723 if (cnt_gt(entry->cnt_alive, 1))
724 graph->is_chain_call = 0;
727 /* recursive functions are never chain calls, leafs don't have calls */
728 if (graph->is_recursive || graph->is_leaf)
729 graph->is_chain_call = 0;
731 /* assume we walk every graph only ONCE, we could sum here the global count */
732 for (entry = pset_first(graph->opcode_hash); entry; entry = pset_next(graph->opcode_hash)) {
733 node_entry_t *g_entry = opcode_get_entry(entry->op, global->opcode_hash);
735 /* update the node counter */
736 cnt_add(&g_entry->cnt_alive, &entry->cnt_alive);
739 /* update the edge counter */
740 cnt_add(&global->cnt_edges, &graph->cnt_edges);
742 /* count the number of address calculation */
743 if (graph->irg != get_const_code_irg()) {
744 ir_graph *rem = current_ir_graph;
746 if (get_irg_outs_state(graph->irg) != outs_consistent)
747 compute_outs(graph->irg);
749 /* Must be done an the outs graph */
750 current_ir_graph = graph->irg;
751 irg_out_walk(get_irg_start(graph->irg), NULL, mark_address_calc, graph);
752 current_ir_graph = rem;
755 set_dump_node_vcgattr_hook(stat_adr_mark_hook);
756 dump_ir_block_graph(graph->irg, "-adr");
757 set_dump_node_vcgattr_hook(NULL);
761 /* count the DAG's */
762 if (status->stat_options & FIRMSTAT_COUNT_DAG)
763 count_dags_in_graph(global, graph);
765 /* calculate the patterns of this graph */
766 stat_calc_pattern_history(graph->irg);
768 /* leaf function did not call others */
770 graph->is_leaf_call = LCS_NON_LEAF_CALL;
771 else if (graph->is_leaf_call == LCS_UNKNOWN) {
772 /* we still don't know if this graph calls leaf-functions, so enqueue */
773 pdeq_putl(status->wait_q, graph);
776 /* we have analyzed this graph */
777 graph->is_analyzed = 1;
781 * Called for every graph that was on the wait_q in stat_finish()
782 * must finish all statistic info calculations.
784 * @param global The global entry
785 * @param graph The current entry
787 static void update_graph_stat_2(graph_entry_t *global, graph_entry_t *graph)
789 if (graph->is_deleted) {
790 /* deleted, ignore */
795 /* count the nodes in the graph */
796 irg_walk_graph(graph->irg, update_node_stat_2, NULL, graph);
798 if (graph->is_leaf_call == LCS_UNKNOWN)
799 graph->is_leaf_call = LCS_LEAF_CALL;
806 static void stat_register_dumper(const dumper_t *dumper)
808 dumper_t *p = xmalloc(sizeof(*p));
813 p->next = status->dumper;
818 /* FIXME: memory leak */
824 static void stat_dump_graph(graph_entry_t *entry)
828 for (dumper = status->dumper; dumper; dumper = dumper->next) {
829 if (dumper->dump_graph)
830 dumper->dump_graph(dumper, entry);
835 * initialize the dumper
837 static void stat_dump_init(const char *name)
841 for (dumper = status->dumper; dumper; dumper = dumper->next) {
843 dumper->init(dumper, name);
850 static void stat_dump_finish(void)
854 for (dumper = status->dumper; dumper; dumper = dumper->next) {
856 dumper->finish(dumper);
860 /* ---------------------------------------------------------------------- */
863 * helper: get an ir_op from an opcode
865 ir_op *stat_get_op_from_opcode(opcode code)
867 return opcode_find_entry(code, status->ir_op_hash);
871 * A new IR op is registered.
873 * @param ctx the hook context
874 * @param op the new IR opcode that was created.
876 static void stat_new_ir_op(void *ctx, ir_op *op)
878 if (! status->stat_options)
883 graph_entry_t *graph = graph_get_entry(NULL, status->irg_hash);
885 /* execute for side effect :-) */
886 opcode_get_entry(op, graph->opcode_hash);
888 pset_insert(status->ir_op_hash, op, op->code);
896 * @param ctx the hook context
897 * @param op the IR opcode that is freed
899 static void stat_free_ir_op(void *ctx, ir_op *op)
901 if (! status->stat_options)
911 * A new node is created.
913 * @param ctx the hook context
914 * @param irg the IR graph on which the node is created
915 * @param node the new IR node that was created
917 static void stat_new_node(void *ctx, ir_graph *irg, ir_node *node)
919 if (! status->stat_options)
922 /* do NOT count during dead node elimination */
923 if (status->in_dead_node_elim > 0)
929 graph_entry_t *graph;
930 ir_op *op = stat_get_irn_op(node);
932 /* increase global value */
933 graph = graph_get_entry(NULL, status->irg_hash);
934 entry = opcode_get_entry(op, graph->opcode_hash);
935 cnt_inc(&entry->new_node);
937 /* increase local value */
938 graph = graph_get_entry(current_ir_graph, status->irg_hash);
939 entry = opcode_get_entry(op, graph->opcode_hash);
940 cnt_inc(&entry->new_node);
946 * A node is changed into a Id node
948 * @param ctx the hook context
949 * @param node the IR node that will be turned into an ID
951 static void stat_turn_into_id(void *ctx, ir_node *node)
953 if (! status->stat_options)
959 graph_entry_t *graph;
960 ir_op *op = stat_get_irn_op(node);
962 /* increase global value */
963 graph = graph_get_entry(NULL, status->irg_hash);
964 entry = opcode_get_entry(op, graph->opcode_hash);
965 cnt_inc(&entry->into_Id);
967 /* increase local value */
968 graph = graph_get_entry(current_ir_graph, status->irg_hash);
969 entry = opcode_get_entry(op, graph->opcode_hash);
970 cnt_inc(&entry->into_Id);
976 * A new graph was created
978 * @param ctx the hook context
979 * @param irg the new IR graph that was created
980 * @param ent the entity of this graph
982 static void stat_new_graph(void *ctx, ir_graph *irg, entity *ent)
984 if (! status->stat_options)
989 /* execute for side effect :-) */
990 graph_entry_t * graph = graph_get_entry(irg, status->irg_hash);
993 graph->is_deleted = 0;
995 graph->is_leaf_call = 0;
996 graph->is_recursive = 0;
997 graph->is_chain_call = 0;
998 graph->is_analyzed = 0;
1004 * A graph will be deleted
1006 * @param ctx the hook context
1007 * @param irg the IR graph that will be deleted
1009 * Note that we still hold the information for this graph
1010 * in our hash maps, only a flag is set which prevents this
1011 * information from being changed, it's "frozen" from now.
1013 static void stat_free_graph(void *ctx, ir_graph *irg)
1015 if (! status->stat_options)
1020 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1021 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1023 graph->is_deleted = 1;
1025 if (status->stat_options & FIRMSTAT_COUNT_DELETED) {
1026 /* count the nodes of the graph yet, it will be destroyed later */
1027 update_graph_stat(global, graph);
1034 * A walk over a graph is initiated. Do not count walks from statistic code.
1036 * @param ctx the hook context
1037 * @param irg the IR graph that will be walked
1038 * @param pre the pre walker
1039 * @param post the post walker
1041 static void stat_irg_walk(void *ctx, ir_graph *irg, void *pre, void *post)
1043 if (! status->stat_options)
1048 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1050 cnt_inc(&graph->cnt_walked);
1056 * A walk over a graph in block-wise order is initiated. Do not count walks from statistic code.
1058 * @param ctx the hook context
1059 * @param irg the IR graph that will be walked
1060 * @param pre the pre walker
1061 * @param post the post walker
1063 static void stat_irg_walk_blkwise(void *ctx, ir_graph *irg, void *pre, void *post)
1065 /* for now, do NOT differentiate between blockwise and normal */
1066 stat_irg_walk(ctx, irg, pre, post);
1070 * A walk over the graph's blocks is initiated. Do not count walks from statistic code.
1072 * @param ctx the hook context
1073 * @param irg the IR graph that will be walked
1074 * @param node the IR node
1075 * @param pre the pre walker
1076 * @param post the post walker
1078 static void stat_irg_block_walk(void *ctx, ir_graph *irg, ir_node *node, void *pre, void *post)
1080 if (! status->stat_options)
1085 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1087 cnt_inc(&graph->cnt_walked_blocks);
1093 * called for every node that is removed due to an optimization.
1095 * @param n the IR node that will be removed
1096 * @param hmap the hash map containing ir_op* -> opt_entry_t*
1098 static void removed_due_opt(ir_node *n, hmap_opt_entry_t *hmap)
1100 ir_op *op = stat_get_irn_op(n);
1101 opt_entry_t *entry = opt_get_entry(op, hmap);
1103 /* increase global value */
1104 cnt_inc(&entry->count);
1108 * Some nodes were optimized into some others due to an optimization.
1110 * @param ctx the hook context
1112 static void stat_merge_nodes(
1114 ir_node **new_node_array, int new_num_entries,
1115 ir_node **old_node_array, int old_num_entries,
1118 if (! status->stat_options)
1124 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1126 if (status->reassoc_run)
1127 opt = HOOK_OPT_REASSOC;
1129 for (i = 0; i < old_num_entries; ++i) {
1130 for (j = 0; j < new_num_entries; ++j)
1131 if (old_node_array[i] == new_node_array[j])
1134 /* nodes might be in new and old, these are NOT removed */
1135 if (j >= new_num_entries) {
1136 removed_due_opt(old_node_array[i], graph->opt_hash[opt]);
1144 * Reassociation is started/stopped.
1146 * @param ctx the hook context
1147 * @param flag if non-zero, reassociation is started else stopped
1149 static void stat_reassociate(void *ctx, int flag)
1151 if (! status->stat_options)
1156 status->reassoc_run = flag;
1162 * A node was lowered into other nodes
1164 * @param ctx the hook context
1165 * @param node the IR node that will be lowered
1167 static void stat_lower(void *ctx, ir_node *node)
1169 if (! status->stat_options)
1174 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1176 removed_due_opt(node, graph->opt_hash[HOOK_LOWERED]);
1182 * A graph was inlined.
1184 * @param ctx the hook context
1185 * @param call the IR call that will re changed into the body of
1186 * the called IR graph
1187 * @param called_irg the IR graph representing the called routine
1189 static void stat_inline(void *ctx, ir_node *call, ir_graph *called_irg)
1191 if (! status->stat_options)
1196 ir_graph *irg = get_irn_irg(call);
1197 graph_entry_t *i_graph = graph_get_entry(called_irg, status->irg_hash);
1198 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1200 cnt_inc(&graph->cnt_got_inlined);
1201 cnt_inc(&i_graph->cnt_was_inlined);
1207 * A graph with tail-recursions was optimized.
1209 * @param ctx the hook context
1211 static void stat_tail_rec(void *ctx, ir_graph *irg)
1213 if (! status->stat_options)
1218 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1220 graph->num_tail_recursion++;
1226 * Strength reduction was performed on an iteration variable.
1228 * @param ctx the hook context
1230 static void stat_strength_red(void *ctx, ir_graph *irg, ir_node *strong, ir_node *cmp)
1232 if (! status->stat_options)
1237 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1238 cnt_inc(&graph->cnt_strength_red);
1240 removed_due_opt(strong, graph->opt_hash[HOOK_OPT_STRENGTH_RED]);
1246 * Start the dead node elimination.
1248 * @param ctx the hook context
1250 static void stat_dead_node_elim_start(void *ctx, ir_graph *irg)
1252 if (! status->stat_options)
1255 ++status->in_dead_node_elim;
1259 * Stops the dead node elimination.
1261 * @param ctx the hook context
1263 static void stat_dead_node_elim_stop(void *ctx, ir_graph *irg)
1265 if (! status->stat_options)
1268 --status->in_dead_node_elim;
1272 * if-conversion was tried
1274 static void stat_if_conversion(void *context, ir_graph *irg, ir_node *phi,
1275 int pos, ir_node *mux, if_result_t reason)
1277 if (! status->stat_options)
1282 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1284 cnt_inc(&graph->cnt_if_conv[reason]);
1290 * real function call was optimized
1292 static void stat_func_call(void *context, ir_graph *irg, ir_node *call)
1294 if (! status->stat_options)
1299 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1301 cnt_inc(&graph->cnt_real_func_call);
1307 * A multiply was replaced by a series of Shifts/Adds/Subs
1309 * @param ctx the hook context
1311 static void stat_arch_dep_replace_mul_with_shifts(void *ctx, ir_node *mul)
1313 if (! status->stat_options)
1318 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1319 removed_due_opt(mul, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1325 * A division was replaced by a series of Shifts/Muls
1327 * @param ctx the hook context
1328 * @param div the div node that will be optimized
1330 static void stat_arch_dep_replace_div_by_const(void *ctx, ir_node *div)
1332 if (! status->stat_options)
1337 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1338 removed_due_opt(div, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1344 * A modulo was replaced by a series of Shifts/Muls
1346 * @param ctx the hook context
1347 * @param mod the mod node that will be optimized
1349 static void stat_arch_dep_replace_mod_by_const(void *ctx, ir_node *mod)
1351 if (! status->stat_options)
1356 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1357 removed_due_opt(mod, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1363 * A DivMod was replaced by a series of Shifts/Muls
1365 * @param ctx the hook context
1366 * @param divmod the divmod node that will be optimized
1368 static void stat_arch_dep_replace_DivMod_by_const(void *ctx, ir_node *divmod)
1370 if (! status->stat_options)
1375 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1376 removed_due_opt(divmod, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1381 /* Finish the statistics */
1382 void stat_finish(const char *name)
1384 if (! status->stat_options)
1389 graph_entry_t *entry;
1390 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1392 stat_dump_init(name);
1394 /* calculate the graph statistics */
1395 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
1397 if (entry->irg == NULL) {
1398 /* special entry for the global count */
1402 if (! entry->is_deleted) {
1403 /* the graph is still alive, count the nodes on it */
1404 update_graph_stat(global, entry);
1408 /* some calculations are dependent, we pushed them on the wait_q */
1409 while (! pdeq_empty(status->wait_q)) {
1410 entry = pdeq_getr(status->wait_q);
1412 update_graph_stat_2(global, entry);
1416 /* dump per graph */
1417 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
1419 if (entry->irg == NULL) {
1420 /* special entry for the global count */
1424 if (! entry->is_deleted || status->stat_options & FIRMSTAT_COUNT_DELETED)
1425 stat_dump_graph(entry);
1427 if (! entry->is_deleted) {
1428 /* clear the counter that are not accumulated */
1429 graph_clear_entry(entry, 0);
1434 stat_dump_graph(global);
1437 stat_finish_pattern_history();
1439 /* clear the global counter here */
1441 node_entry_t *entry;
1443 for (entry = pset_first(global->opcode_hash); entry; entry = pset_next(global->opcode_hash)) {
1444 opcode_clear_entry(entry);
1446 /* clear all global counter */
1447 graph_clear_entry(global, 1);
1451 // status->stat_options = 0;
1456 /** the hook entries for the Firm statistics module */
1457 static hook_entry_t stat_hooks[hook_last];
1459 /* initialize the statistics module. */
1460 void init_stat(unsigned enable_options)
1462 #define X(a) a, sizeof(a)-1
1463 #define HOOK(h, fkt) \
1464 stat_hooks[h].hook._##h = fkt; register_hook(h, &stat_hooks[h])
1466 /* enable statistics */
1467 status->stat_options = enable_options & FIRMSTAT_ENABLED ? enable_options : 0;
1469 if (! status->stat_options)
1472 /* register all hooks */
1473 HOOK(hook_new_ir_op, stat_new_ir_op);
1474 HOOK(hook_free_ir_op, stat_free_ir_op);
1475 HOOK(hook_new_node, stat_new_node);
1476 HOOK(hook_turn_into_id, stat_turn_into_id);
1477 HOOK(hook_new_graph, stat_new_graph);
1478 HOOK(hook_free_graph, stat_free_graph);
1479 HOOK(hook_irg_walk, stat_irg_walk);
1480 HOOK(hook_irg_walk_blkwise, stat_irg_walk_blkwise);
1481 HOOK(hook_irg_block_walk, stat_irg_block_walk);
1482 HOOK(hook_merge_nodes, stat_merge_nodes);
1483 HOOK(hook_reassociate, stat_reassociate);
1484 HOOK(hook_lower, stat_lower);
1485 HOOK(hook_inline, stat_inline);
1486 HOOK(hook_tail_rec, stat_tail_rec);
1487 HOOK(hook_strength_red, stat_strength_red);
1488 HOOK(hook_dead_node_elim_start, stat_dead_node_elim_start);
1489 HOOK(hook_dead_node_elim_stop, stat_dead_node_elim_stop);
1490 HOOK(hook_if_conversion, stat_if_conversion);
1491 HOOK(hook_func_call, stat_func_call);
1492 HOOK(hook_arch_dep_replace_mul_with_shifts, stat_arch_dep_replace_mul_with_shifts);
1493 HOOK(hook_arch_dep_replace_div_by_const, stat_arch_dep_replace_div_by_const);
1494 HOOK(hook_arch_dep_replace_mod_by_const, stat_arch_dep_replace_mod_by_const);
1495 HOOK(hook_arch_dep_replace_DivMod_by_const, stat_arch_dep_replace_DivMod_by_const);
1497 obstack_init(&status->cnts);
1499 /* create the hash-tables */
1500 status->irg_hash = new_pset(graph_cmp, 8);
1501 status->ir_op_hash = new_pset(opcode_cmp_2, 1);
1503 /* create the wait queue */
1504 status->wait_q = new_pdeq();
1506 if (enable_options & FIRMSTAT_COUNT_STRONG_OP) {
1507 /* build the pseudo-ops */
1508 _op_Phi0.code = get_next_ir_opcode();
1509 _op_Phi0.name = new_id_from_chars(X("Phi0"));
1511 _op_PhiM.code = get_next_ir_opcode();
1512 _op_PhiM.name = new_id_from_chars(X("PhiM"));
1514 _op_ProjM.code = get_next_ir_opcode();
1515 _op_ProjM.name = new_id_from_chars(X("ProjM"));
1517 _op_MulC.code = get_next_ir_opcode();
1518 _op_MulC.name = new_id_from_chars(X("MulC"));
1520 _op_DivC.code = get_next_ir_opcode();
1521 _op_DivC.name = new_id_from_chars(X("DivC"));
1523 _op_ModC.code = get_next_ir_opcode();
1524 _op_ModC.name = new_id_from_chars(X("ModC"));
1526 _op_DivModC.code = get_next_ir_opcode();
1527 _op_DivModC.name = new_id_from_chars(X("DivModC"));
1529 status->op_Phi0 = &_op_Phi0;
1530 status->op_PhiM = &_op_PhiM;
1531 status->op_ProjM = &_op_ProjM;
1532 status->op_MulC = &_op_MulC;
1533 status->op_DivC = &_op_DivC;
1534 status->op_ModC = &_op_ModC;
1535 status->op_DivModC = &_op_DivModC;
1538 status->op_Phi0 = NULL;
1539 status->op_PhiM = NULL;
1540 status->op_ProjM = NULL;
1541 status->op_MulC = NULL;
1542 status->op_DivC = NULL;
1543 status->op_ModC = NULL;
1544 status->op_DivModC = NULL;
1547 /* register the dumper */
1548 stat_register_dumper(&simple_dumper);
1550 if (enable_options & FIRMSTAT_CSV_OUTPUT)
1551 stat_register_dumper(&csv_dumper);
1553 /* initialize the pattern hash */
1554 stat_init_pattern_history(enable_options & FIRMSTAT_PATTERN_ENABLED);
1561 /* Finish the statistics */
1562 void stat_finish(const char *name) {}
1564 #endif /* FIRM_STATISTICS */