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));
178 opcode_clear_entry(elem);
182 return pset_insert(hmap, elem, op->code);
186 * Returns the associates ir_op for an opcode
188 * @param code the IR opcode
189 * @param hmap the hash map containing opcode -> ir_op*
191 static ir_op *opcode_find_entry(opcode code, hmap_ir_op *hmap)
196 return pset_find(hmap, &key, code);
200 * clears all counter in a graph_entry_t
202 static void graph_clear_entry(graph_entry_t *elem, int all)
205 cnt_clr(&elem->cnt_walked);
206 cnt_clr(&elem->cnt_walked_blocks);
207 cnt_clr(&elem->cnt_was_inlined);
208 cnt_clr(&elem->cnt_got_inlined);
209 cnt_clr(&elem->cnt_strength_red);
211 cnt_clr(&elem->cnt_edges);
212 cnt_clr(&elem->cnt_all_calls);
213 cnt_clr(&elem->cnt_call_with_cnst_arg);
214 cnt_clr(&elem->cnt_indirect_calls);
218 * Returns the associated graph_entry_t for an IR graph.
220 * @param irg the IR graph
221 * @param hmap the hash map containing ir_graph* -> graph_entry_t*
223 static graph_entry_t *graph_get_entry(ir_graph *irg, hmap_graph_entry_t *hmap)
231 elem = pset_find(hmap, &key, HASH_PTR(irg));
235 /* allocate a new one */
236 elem = obstack_alloc(&status->cnts, sizeof(*elem));
239 graph_clear_entry(elem, 1);
241 /* new hash table for opcodes here */
242 elem->opcode_hash = new_pset(opcode_cmp, 5);
243 elem->block_hash = new_pset(block_cmp, 5);
244 elem->address_mark = new_set(address_mark_cmp, 5);
247 for (i = 0; i < sizeof(elem->opt_hash)/sizeof(elem->opt_hash[0]); ++i)
248 elem->opt_hash[i] = new_pset(opt_cmp, 4);
250 return pset_insert(hmap, elem, HASH_PTR(irg));
254 * clears all counter in an opt_entry_t
256 static void opt_clear_entry(opt_entry_t *elem)
258 cnt_clr(&elem->count);
262 * Returns the associated opt_entry_t for an IR operation.
264 * @param op the IR operation
265 * @param hmap the hash map containing ir_op* -> opt_entry_t*
267 static opt_entry_t *opt_get_entry(const ir_op *op, hmap_opt_entry_t *hmap)
274 elem = pset_find(hmap, &key, op->code);
278 elem = obstack_alloc(&status->cnts, sizeof(*elem));
280 /* clear new counter */
281 opt_clear_entry(elem);
285 return pset_insert(hmap, elem, op->code);
289 * clears all counter in a block_entry_t
291 static void block_clear_entry(block_entry_t *elem)
293 cnt_clr(&elem->cnt_nodes);
294 cnt_clr(&elem->cnt_edges);
295 cnt_clr(&elem->cnt_in_edges);
296 cnt_clr(&elem->cnt_out_edges);
300 * Returns the associated block_entry_t for an block.
302 * @param block_nr an IR block number
303 * @param hmap a hash map containing long -> block_entry_t
305 static block_entry_t *block_get_entry(long block_nr, hmap_block_entry_t *hmap)
310 key.block_nr = block_nr;
312 elem = pset_find(hmap, &key, block_nr);
316 elem = obstack_alloc(&status->cnts, sizeof(*elem));
318 /* clear new counter */
319 block_clear_entry(elem);
321 elem->block_nr = block_nr;
323 return pset_insert(hmap, elem, block_nr);
328 * Returns the ir_op for an IR-node,
329 * handles special cases and return pseudo op codes.
331 * @param none an IR node
333 static ir_op *stat_get_irn_op(ir_node *node)
335 ir_op *op = get_irn_op(node);
337 if (op == op_Phi && get_irn_arity(node) == 0) {
338 /* special case, a Phi0 node, count on extra counter */
339 op = status->op_Phi0 ? status->op_Phi0 : op;
341 else if (op == op_Phi && get_irn_mode(node) == mode_M) {
342 /* special case, a Memory Phi node, count on extra counter */
343 op = status->op_PhiM ? status->op_PhiM : op;
345 else if (op == op_Proj && get_irn_mode(node) == mode_M) {
346 /* special case, a Memory Proj node, count on extra counter */
347 op = status->op_ProjM ? status->op_ProjM : op;
349 else if (op == op_Mul &&
350 (get_irn_op(get_Mul_left(node)) == op_Const || get_irn_op(get_Mul_right(node)) == op_Const)) {
351 /* special case, a Multiply by a const, count on extra counter */
352 op = status->op_MulC ? status->op_MulC : op;
354 else if (op == op_Div && get_irn_op(get_Div_right(node)) == op_Const) {
355 /* special case, a division by a const, count on extra counter */
356 op = status->op_DivC ? status->op_DivC : op;
358 else if (op == op_Mod && get_irn_op(get_Mod_right(node)) == op_Const) {
359 /* special case, a module by a const, count on extra counter */
360 op = status->op_ModC ? status->op_ModC : op;
362 else if (op == op_DivMod && get_irn_op(get_DivMod_right(node)) == op_Const) {
363 /* special case, a division/modulo by a const, count on extra counter */
364 op = status->op_DivModC ? status->op_DivModC : op;
371 * update the block counter
373 static void undate_block_info(ir_node *node, graph_entry_t *graph)
375 ir_op *op = get_irn_op(node);
377 block_entry_t *b_entry;
380 /* check for block */
381 if (op == op_Block) {
382 arity = get_irn_arity(node);
383 b_entry = block_get_entry(get_irn_node_nr(node), graph->block_hash);
385 /* count all incoming edges */
386 for (i = 0; i < arity; ++i) {
387 ir_node *pred = get_irn_n(node, i);
388 ir_node *other_block = get_nodes_block(pred);
389 block_entry_t *b_entry_other = block_get_entry(get_irn_node_nr(other_block), graph->block_hash);
391 cnt_inc(&b_entry->cnt_in_edges); /* an edge coming from another block */
392 cnt_inc(&b_entry_other->cnt_out_edges);
397 block = get_nodes_block(node);
398 b_entry = block_get_entry(get_irn_node_nr(block), graph->block_hash);
400 /* we have a new nodes */
401 cnt_inc(&b_entry->cnt_nodes);
403 arity = get_irn_arity(node);
405 for (i = 0; i < arity; ++i) {
406 ir_node *pred = get_irn_n(node, i);
407 ir_node *other_block;
409 if (get_irn_op(pred) == op_Block)
412 other_block = get_nodes_block(pred);
414 if (other_block == block)
415 cnt_inc(&b_entry->cnt_edges); /* a in block edge */
417 block_entry_t *b_entry_other = block_get_entry(get_irn_node_nr(other_block), graph->block_hash);
419 cnt_inc(&b_entry->cnt_in_edges); /* an edge coming from another block */
420 cnt_inc(&b_entry_other->cnt_out_edges);
425 /** calculates how many arguments of the call are const */
426 static int cnt_const_args(ir_node *call)
429 int n = get_Call_n_params(call);
431 for (i = 0; i < n; ++i) {
432 ir_node *param = get_Call_param(call, i);
433 ir_op *op = get_irn_op(param);
435 if (op == op_Const || op == op_SymConst)
442 * update info on calls
444 * @param call The call
445 * @param graph The graph entry containing the call
447 static void update_call_stat(ir_node *call, graph_entry_t *graph)
449 ir_node *block = get_nodes_block(call);
450 ir_node *ptr = get_Call_ptr(call);
452 ir_graph *callee = NULL;
456 * If the block is bad, the whole subgraph will collapse later
457 * so do not count this call.
458 * This happens in dead code.
463 cnt_inc(&graph->cnt_all_calls);
465 /* found a call, this function is not a leaf */
468 if (get_irn_op(ptr) == op_SymConst) {
469 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
470 /* ok, we seems to know the entity */
471 ent = get_SymConst_entity(ptr);
472 callee = get_entity_irg(ent);
474 /* it is recursive, if it calls at least once */
475 if (callee == graph->irg)
476 graph->is_recursive = 1;
480 /* indirect call, be could not predict */
481 cnt_inc(&graph->cnt_indirect_calls);
483 /* NOT a leaf call */
484 graph->is_leaf_call = LCS_NON_LEAF_CALL;
487 /* check, if it's a chain-call: Then, the call-block
488 * must dominate the end block. */
490 ir_node *curr = get_irg_end_block(graph->irg);
491 int depth = get_Block_dom_depth(block);
493 for (; curr != block && get_Block_dom_depth(curr) > depth;) {
494 curr = get_Block_idom(curr);
496 if (! curr || is_no_Block(curr))
501 graph->is_chain_call = 0;
504 /* check, if the callee is a leaf */
506 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
508 if (called->is_analyzed) {
509 if (! called->is_leaf)
510 graph->is_leaf_call = LCS_NON_LEAF_CALL;
514 /* check, if arguments of the call are const */
515 num_const_args = cnt_const_args(call);
517 if (num_const_args > 0)
518 cnt_inc(&graph->cnt_call_with_cnst_arg);
522 * update info on calls for graphs on the wait queue
524 static void update_call_stat_2(ir_node *call, graph_entry_t *graph)
526 ir_node *block = get_nodes_block(call);
527 ir_node *ptr = get_Call_ptr(call);
529 ir_graph *callee = NULL;
532 * If the block is bad, the whole subgraph will collapse later
533 * so do not count this call.
534 * This happens in dead code.
539 if (get_irn_op(ptr) == op_SymConst) {
540 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
541 /* ok, we seems to know the entity */
542 ent = get_SymConst_entity(ptr);
543 callee = get_entity_irg(ent);
547 /* check, if the callee is a leaf */
549 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
551 assert(called->is_analyzed);
553 if (! called->is_leaf)
554 graph->is_leaf_call = LCS_NON_LEAF_CALL;
557 graph->is_leaf_call = LCS_NON_LEAF_CALL;
561 * walker for reachable nodes count
563 static void update_node_stat(ir_node *node, void *env)
565 graph_entry_t *graph = env;
568 ir_op *op = stat_get_irn_op(node);
569 int arity = get_irn_arity(node);
571 entry = opcode_get_entry(op, graph->opcode_hash);
573 cnt_inc(&entry->cnt_alive);
574 cnt_add_i(&graph->cnt_edges, arity);
576 /* count block edges */
577 undate_block_info(node, graph);
579 /* check for properties that depends on calls like recursion/leaf/indirect call */
581 update_call_stat(node, graph);
585 * walker for reachable nodes count for graphs on the wait_q
587 static void update_node_stat_2(ir_node *node, void *env)
589 graph_entry_t *graph = env;
591 /* check for properties that depends on calls like recursion/leaf/indirect call */
592 if (get_irn_op(node) == op_Call)
593 update_call_stat_2(node, graph);
597 * get the current address mark
599 static unsigned get_adr_mark(graph_entry_t *graph, ir_node *node)
601 address_mark_entry_t *value = set_find(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
603 return value ? value->mark : 0;
607 * set the current address mark
609 static void set_adr_mark(graph_entry_t *graph, ir_node *node, unsigned val)
611 address_mark_entry_t *value = set_insert(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
617 * a vcg attribute hook: Color a node with a different color if
618 * it's identified as a part of an address expression or at least referenced
619 * by an address expression.
621 static int stat_adr_mark_hook(FILE *F, ir_node *node, ir_node *local)
623 ir_node *n = local ? local : node;
624 ir_graph *irg = get_irn_irg(n);
625 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
626 unsigned mark = get_adr_mark(graph, n);
628 if (mark & MARK_ADDRESS_CALC)
629 fprintf(F, "color: purple");
630 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
631 fprintf(F, "color: pink");
632 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
633 fprintf(F, "color: lightblue");
637 /* I know the color! */
642 * walker that marks every node that is an address calculation
644 * predecessor nodes must be visited first. We ensure this by
645 * calling in in the post of an outs walk. This should work even in cycles,
646 * while the pre in a normal walk will not.
648 static void mark_address_calc(ir_node *node, void *env)
650 graph_entry_t *graph = env;
651 ir_mode *mode = get_irn_mode(node);
653 unsigned mark_preds = MARK_REF_NON_ADR;
655 if (! mode_is_numP(mode))
658 if (mode_is_reference(mode)) {
659 /* a reference is calculated here, we are sure */
660 set_adr_mark(graph, node, MARK_ADDRESS_CALC);
662 mark_preds = MARK_REF_ADR;
665 unsigned mark = get_adr_mark(graph, node);
667 if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR) {
669 * this node has not an reference mode, but is only
670 * referenced by address calculations
672 mark_preds = MARK_REF_ADR;
676 /* mark all predecessors */
677 for (i = 0, n = get_irn_arity(node); i < n; ++i) {
678 ir_node *pred = get_irn_n(node, i);
680 set_adr_mark(graph, pred, get_adr_mark(graph, pred) | mark_preds);
685 * Called for every graph when the graph is either deleted or stat_finish()
686 * is called, must recalculate all statistic info.
688 * @param global The global entry
689 * @param graph The current entry
691 static void update_graph_stat(graph_entry_t *global, graph_entry_t *graph)
695 /* clear first the alive counter in the graph */
696 for (entry = pset_first(graph->opcode_hash); entry; entry = pset_next(graph->opcode_hash)) {
697 cnt_clr(&entry->cnt_alive);
700 /* set pessimistic values */
702 graph->is_leaf_call = LCS_UNKNOWN;
703 graph->is_recursive = 0;
704 graph->is_chain_call = 1;
706 /* we need dominator info */
707 if (graph->irg != get_const_code_irg())
708 if (get_irg_dom_state(graph->irg) != dom_consistent)
709 compute_doms(graph->irg);
711 /* count the nodes in the graph */
712 irg_walk_graph(graph->irg, update_node_stat, NULL, graph);
715 entry = opcode_get_entry(op_Call, graph->opcode_hash);
717 /* check if we have more than 1 call */
718 if (cnt_gt(entry->cnt_alive, 1))
719 graph->is_chain_call = 0;
722 /* recursive functions are never chain calls, leafs don't have calls */
723 if (graph->is_recursive || graph->is_leaf)
724 graph->is_chain_call = 0;
726 /* assume we walk every graph only ONCE, we could sum here the global count */
727 for (entry = pset_first(graph->opcode_hash); entry; entry = pset_next(graph->opcode_hash)) {
728 node_entry_t *g_entry = opcode_get_entry(entry->op, global->opcode_hash);
730 /* update the node counter */
731 cnt_add(&g_entry->cnt_alive, &entry->cnt_alive);
734 /* update the edge counter */
735 cnt_add(&global->cnt_edges, &graph->cnt_edges);
737 /* count the number of address calculation */
738 if (graph->irg != get_const_code_irg()) {
739 ir_graph *rem = current_ir_graph;
741 if (get_irg_outs_state(graph->irg) != outs_consistent)
742 compute_outs(graph->irg);
744 /* Must be done an the outs graph */
745 current_ir_graph = graph->irg;
746 irg_out_walk(get_irg_start(graph->irg), NULL, mark_address_calc, graph);
747 current_ir_graph = rem;
750 set_dump_node_vcgattr_hook(stat_adr_mark_hook);
751 dump_ir_block_graph(graph->irg, "-adr");
752 set_dump_node_vcgattr_hook(NULL);
756 /* count the DAG's */
757 if (status->stat_options & FIRMSTAT_COUNT_DAG)
758 count_dags_in_graph(global, graph);
760 /* calculate the patterns of this graph */
761 stat_calc_pattern_history(graph->irg);
763 /* leaf function did not call others */
765 graph->is_leaf_call = LCS_NON_LEAF_CALL;
766 else if (graph->is_leaf_call == LCS_UNKNOWN) {
767 /* we still don't know if this graph calls leaf-functions, so enqueue */
768 pdeq_putl(status->wait_q, graph);
771 /* we have analyzed this graph */
772 graph->is_analyzed = 1;
776 * Called for every graph that was on the wait_q in stat_finish()
777 * must finish all statistic info calculations.
779 * @param global The global entry
780 * @param graph The current entry
782 static void update_graph_stat_2(graph_entry_t *global, graph_entry_t *graph)
784 if (graph->is_deleted) {
785 /* deleted, ignore */
790 /* count the nodes in the graph */
791 irg_walk_graph(graph->irg, update_node_stat_2, NULL, graph);
793 if (graph->is_leaf_call == LCS_UNKNOWN)
794 graph->is_leaf_call = LCS_LEAF_CALL;
801 static void stat_register_dumper(const dumper_t *dumper)
803 dumper_t *p = xmalloc(sizeof(*p));
808 p->next = status->dumper;
813 /* FIXME: memory leak */
819 static void stat_dump_graph(graph_entry_t *entry)
823 for (dumper = status->dumper; dumper; dumper = dumper->next) {
824 if (dumper->dump_graph)
825 dumper->dump_graph(dumper, entry);
830 * initialize the dumper
832 static void stat_dump_init(const char *name)
836 for (dumper = status->dumper; dumper; dumper = dumper->next) {
838 dumper->init(dumper, name);
845 static void stat_dump_finish(void)
849 for (dumper = status->dumper; dumper; dumper = dumper->next) {
851 dumper->finish(dumper);
855 /* ---------------------------------------------------------------------- */
858 * helper: get an ir_op from an opcode
860 ir_op *stat_get_op_from_opcode(opcode code)
862 return opcode_find_entry(code, status->ir_op_hash);
866 * A new IR op is registered.
868 * @param ctx the hook context
869 * @param op the new IR opcode that was created.
871 static void stat_new_ir_op(void *ctx, ir_op *op)
873 if (! status->stat_options)
878 graph_entry_t *graph = graph_get_entry(NULL, status->irg_hash);
880 /* execute for side effect :-) */
881 opcode_get_entry(op, graph->opcode_hash);
883 pset_insert(status->ir_op_hash, op, op->code);
891 * @param ctx the hook context
892 * @param op the IR opcode that is freed
894 static void stat_free_ir_op(void *ctx, ir_op *op)
896 if (! status->stat_options)
906 * A new node is created.
908 * @param ctx the hook context
909 * @param irg the IR graph on which the node is created
910 * @param node the new IR node that was created
912 static void stat_new_node(void *ctx, ir_graph *irg, ir_node *node)
914 if (! status->stat_options)
917 /* do NOT count during dead node elimination */
918 if (status->in_dead_node_elim > 0)
924 graph_entry_t *graph;
925 ir_op *op = stat_get_irn_op(node);
927 /* increase global value */
928 graph = graph_get_entry(NULL, status->irg_hash);
929 entry = opcode_get_entry(op, graph->opcode_hash);
930 cnt_inc(&entry->new_node);
932 /* increase local value */
933 graph = graph_get_entry(current_ir_graph, status->irg_hash);
934 entry = opcode_get_entry(op, graph->opcode_hash);
935 cnt_inc(&entry->new_node);
941 * A node is changed into a Id node
943 * @param ctx the hook context
944 * @param node the IR node that will be turned into an ID
946 static void stat_turn_into_id(void *ctx, ir_node *node)
948 if (! status->stat_options)
954 graph_entry_t *graph;
955 ir_op *op = stat_get_irn_op(node);
957 /* increase global value */
958 graph = graph_get_entry(NULL, status->irg_hash);
959 entry = opcode_get_entry(op, graph->opcode_hash);
960 cnt_inc(&entry->into_Id);
962 /* increase local value */
963 graph = graph_get_entry(current_ir_graph, status->irg_hash);
964 entry = opcode_get_entry(op, graph->opcode_hash);
965 cnt_inc(&entry->into_Id);
971 * A new graph was created
973 * @param ctx the hook context
974 * @param irg the new IR graph that was created
975 * @param ent the entity of this graph
977 static void stat_new_graph(void *ctx, ir_graph *irg, entity *ent)
979 if (! status->stat_options)
984 /* execute for side effect :-) */
985 graph_entry_t * graph = graph_get_entry(irg, status->irg_hash);
988 graph->is_deleted = 0;
990 graph->is_leaf_call = 0;
991 graph->is_recursive = 0;
992 graph->is_chain_call = 0;
993 graph->is_analyzed = 0;
999 * A graph will be deleted
1001 * @param ctx the hook context
1002 * @param irg the IR graph that will be deleted
1004 * Note that we still hold the information for this graph
1005 * in our hash maps, only a flag is set which prevents this
1006 * information from being changed, it's "frozen" from now.
1008 static void stat_free_graph(void *ctx, ir_graph *irg)
1010 if (! status->stat_options)
1015 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1016 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1018 graph->is_deleted = 1;
1020 if (status->stat_options & FIRMSTAT_COUNT_DELETED) {
1021 /* count the nodes of the graph yet, it will be destroyed later */
1022 update_graph_stat(global, graph);
1029 * A walk over a graph is initiated. Do not count walks from statistic code.
1031 * @param ctx the hook context
1032 * @param irg the IR graph that will be walked
1033 * @param pre the pre walker
1034 * @param post the post walker
1036 static void stat_irg_walk(void *ctx, ir_graph *irg, void *pre, void *post)
1038 if (! status->stat_options)
1043 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1045 cnt_inc(&graph->cnt_walked);
1051 * A walk over a graph in block-wise order is initiated. Do not count walks from statistic code.
1053 * @param ctx the hook context
1054 * @param irg the IR graph that will be walked
1055 * @param pre the pre walker
1056 * @param post the post walker
1058 static void stat_irg_walk_blkwise(void *ctx, ir_graph *irg, void *pre, void *post)
1060 /* for now, do NOT differentiate between blockwise and normal */
1061 stat_irg_walk(ctx, irg, pre, post);
1065 * A walk over the graph's blocks is initiated. Do not count walks from statistic code.
1067 * @param ctx the hook context
1068 * @param irg the IR graph that will be walked
1069 * @param node the IR node
1070 * @param pre the pre walker
1071 * @param post the post walker
1073 static void stat_irg_block_walk(void *ctx, ir_graph *irg, ir_node *node, void *pre, void *post)
1075 if (! status->stat_options)
1080 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1082 cnt_inc(&graph->cnt_walked_blocks);
1088 * called for every node that is removed due to an optimization.
1090 * @param n the IR node that will be removed
1091 * @param hmap the hash map containing ir_op* -> opt_entry_t*
1093 static void removed_due_opt(ir_node *n, hmap_opt_entry_t *hmap)
1095 ir_op *op = stat_get_irn_op(n);
1096 opt_entry_t *entry = opt_get_entry(op, hmap);
1098 /* increase global value */
1099 cnt_inc(&entry->count);
1103 * Some nodes were optimized into some others due to an optimization.
1105 * @param ctx the hook context
1107 static void stat_merge_nodes(
1109 ir_node **new_node_array, int new_num_entries,
1110 ir_node **old_node_array, int old_num_entries,
1113 if (! status->stat_options)
1119 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1121 if (status->reassoc_run)
1122 opt = HOOK_OPT_REASSOC;
1124 for (i = 0; i < old_num_entries; ++i) {
1125 for (j = 0; j < new_num_entries; ++j)
1126 if (old_node_array[i] == new_node_array[j])
1129 /* nodes might be in new and old, these are NOT removed */
1130 if (j >= new_num_entries) {
1131 removed_due_opt(old_node_array[i], graph->opt_hash[opt]);
1139 * Reassociation is started/stopped.
1141 * @param ctx the hook context
1142 * @param flag if non-zero, reassociation is started else stopped
1144 static void stat_reassociate(void *ctx, int flag)
1146 if (! status->stat_options)
1151 status->reassoc_run = flag;
1157 * A node was lowered into other nodes
1159 * @param ctx the hook context
1160 * @param node the IR node that will be lowered
1162 static void stat_lower(void *ctx, ir_node *node)
1164 if (! status->stat_options)
1169 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1171 removed_due_opt(node, graph->opt_hash[HOOK_LOWERED]);
1177 * A graph was inlined.
1179 * @param ctx the hook context
1180 * @param call the IR call that will re changed into the body of
1181 * the called IR graph
1182 * @param called_irg the IR graph representing the called routine
1184 static void stat_inline(void *ctx, ir_node *call, ir_graph *called_irg)
1186 if (! status->stat_options)
1191 ir_graph *irg = get_irn_irg(call);
1192 graph_entry_t *i_graph = graph_get_entry(called_irg, status->irg_hash);
1193 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1195 cnt_inc(&graph->cnt_got_inlined);
1196 cnt_inc(&i_graph->cnt_was_inlined);
1202 * A graph with tail-recursions was optimized.
1204 * @param ctx the hook context
1206 static void stat_tail_rec(void *ctx, ir_graph *irg)
1208 if (! status->stat_options)
1218 * Strength reduction was performed on an iteration variable.
1220 * @param ctx the hook context
1222 static void stat_strength_red(void *ctx, ir_graph *irg, ir_node *strong, ir_node *cmp)
1224 if (! status->stat_options)
1229 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1230 cnt_inc(&graph->cnt_strength_red);
1232 removed_due_opt(strong, graph->opt_hash[HOOK_OPT_STRENGTH_RED]);
1238 * Start the dead node elimination.
1240 * @param ctx the hook context
1242 static void stat_dead_node_elim_start(void *ctx, ir_graph *irg)
1244 if (! status->stat_options)
1247 ++status->in_dead_node_elim;
1251 * Stops the dead node elimination.
1253 * @param ctx the hook context
1255 static void stat_dead_node_elim_stop(void *ctx, ir_graph *irg)
1257 if (! status->stat_options)
1260 --status->in_dead_node_elim;
1264 * A multiply was replaced by a series of Shifts/Adds/Subs
1266 * @param ctx the hook context
1268 static void stat_arch_dep_replace_mul_with_shifts(void *ctx, ir_node *mul)
1270 if (! status->stat_options)
1275 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1276 removed_due_opt(mul, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1282 * A division was replaced by a series of Shifts/Muls
1284 * @param ctx the hook context
1285 * @param div the div node that will be optimized
1287 static void stat_arch_dep_replace_div_by_const(void *ctx, ir_node *div)
1289 if (! status->stat_options)
1294 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1295 removed_due_opt(div, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1301 * A modulo was replaced by a series of Shifts/Muls
1303 * @param ctx the hook context
1304 * @param mod the mod node that will be optimized
1306 static void stat_arch_dep_replace_mod_by_const(void *ctx, ir_node *mod)
1308 if (! status->stat_options)
1313 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1314 removed_due_opt(mod, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1320 * A DivMod was replaced by a series of Shifts/Muls
1322 * @param ctx the hook context
1323 * @param divmod the divmod node that will be optimized
1325 static void stat_arch_dep_replace_DivMod_by_const(void *ctx, ir_node *divmod)
1327 if (! status->stat_options)
1332 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1333 removed_due_opt(divmod, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1338 /* Finish the statistics */
1339 void stat_finish(const char *name)
1341 if (! status->stat_options)
1346 graph_entry_t *entry;
1347 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1349 stat_dump_init(name);
1351 /* calculate the graph statistics */
1352 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
1354 if (entry->irg == NULL) {
1355 /* special entry for the global count */
1359 if (! entry->is_deleted) {
1360 /* the graph is still alive, count the nodes on it */
1361 update_graph_stat(global, entry);
1365 /* some calculations are dependent, we pushed them on the wait_q */
1366 while (! pdeq_empty(status->wait_q)) {
1367 entry = pdeq_getr(status->wait_q);
1369 update_graph_stat_2(global, entry);
1373 /* dump per graph */
1374 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
1376 if (entry->irg == NULL) {
1377 /* special entry for the global count */
1381 if (! entry->is_deleted || status->stat_options & FIRMSTAT_COUNT_DELETED)
1382 stat_dump_graph(entry);
1384 if (! entry->is_deleted) {
1385 /* clear the counter that are not accumulated */
1386 graph_clear_entry(entry, 0);
1391 stat_dump_graph(global);
1394 stat_finish_pattern_history();
1396 /* clear the global counter here */
1398 node_entry_t *entry;
1400 for (entry = pset_first(global->opcode_hash); entry; entry = pset_next(global->opcode_hash)) {
1401 opcode_clear_entry(entry);
1403 /* clear all global counter */
1404 graph_clear_entry(global, 1);
1408 // status->stat_options = 0;
1413 /** the hook entries for the Firm statistics module */
1414 static hook_entry_t stat_hooks[hook_last];
1416 /* initialize the statistics module. */
1417 void init_stat(unsigned enable_options)
1419 #define X(a) a, sizeof(a)-1
1420 #define HOOK(h, fkt) \
1421 stat_hooks[h].hook._##h = fkt; register_hook(h, &stat_hooks[h])
1423 /* enable statistics */
1424 status->stat_options = enable_options & FIRMSTAT_ENABLED ? enable_options : 0;
1426 if (! status->stat_options)
1429 /* register all hooks */
1430 HOOK(hook_new_ir_op, stat_new_ir_op);
1431 HOOK(hook_free_ir_op, stat_free_ir_op);
1432 HOOK(hook_new_node, stat_new_node);
1433 HOOK(hook_turn_into_id, stat_turn_into_id);
1434 HOOK(hook_new_graph, stat_new_graph);
1435 HOOK(hook_free_graph, stat_free_graph);
1436 HOOK(hook_irg_walk, stat_irg_walk);
1437 HOOK(hook_irg_walk_blkwise, stat_irg_walk_blkwise);
1438 HOOK(hook_irg_block_walk, stat_irg_block_walk);
1439 HOOK(hook_merge_nodes, stat_merge_nodes);
1440 HOOK(hook_reassociate, stat_reassociate);
1441 HOOK(hook_lower, stat_lower);
1442 HOOK(hook_inline, stat_inline);
1443 HOOK(hook_tail_rec, stat_tail_rec);
1444 HOOK(hook_strength_red, stat_strength_red);
1445 HOOK(hook_dead_node_elim_start, stat_dead_node_elim_start);
1446 HOOK(hook_dead_node_elim_stop, stat_dead_node_elim_stop);
1447 HOOK(hook_arch_dep_replace_mul_with_shifts, stat_arch_dep_replace_mul_with_shifts);
1448 HOOK(hook_arch_dep_replace_div_by_const, stat_arch_dep_replace_div_by_const);
1449 HOOK(hook_arch_dep_replace_mod_by_const, stat_arch_dep_replace_mod_by_const);
1450 HOOK(hook_arch_dep_replace_DivMod_by_const, stat_arch_dep_replace_DivMod_by_const);
1452 obstack_init(&status->cnts);
1454 /* create the hash-tables */
1455 status->irg_hash = new_pset(graph_cmp, 8);
1456 status->ir_op_hash = new_pset(opcode_cmp_2, 1);
1458 /* create the wait queue */
1459 status->wait_q = new_pdeq();
1461 if (enable_options & FIRMSTAT_COUNT_STRONG_OP) {
1462 /* build the pseudo-ops */
1463 _op_Phi0.code = get_next_ir_opcode();
1464 _op_Phi0.name = new_id_from_chars(X("Phi0"));
1466 _op_PhiM.code = get_next_ir_opcode();
1467 _op_PhiM.name = new_id_from_chars(X("PhiM"));
1469 _op_ProjM.code = get_next_ir_opcode();
1470 _op_ProjM.name = new_id_from_chars(X("ProjM"));
1472 _op_MulC.code = get_next_ir_opcode();
1473 _op_MulC.name = new_id_from_chars(X("MulC"));
1475 _op_DivC.code = get_next_ir_opcode();
1476 _op_DivC.name = new_id_from_chars(X("DivC"));
1478 _op_ModC.code = get_next_ir_opcode();
1479 _op_ModC.name = new_id_from_chars(X("ModC"));
1481 _op_DivModC.code = get_next_ir_opcode();
1482 _op_DivModC.name = new_id_from_chars(X("DivModC"));
1484 status->op_Phi0 = &_op_Phi0;
1485 status->op_PhiM = &_op_PhiM;
1486 status->op_ProjM = &_op_ProjM;
1487 status->op_MulC = &_op_MulC;
1488 status->op_DivC = &_op_DivC;
1489 status->op_ModC = &_op_ModC;
1490 status->op_DivModC = &_op_DivModC;
1493 status->op_Phi0 = NULL;
1494 status->op_PhiM = NULL;
1495 status->op_ProjM = NULL;
1496 status->op_MulC = NULL;
1497 status->op_DivC = NULL;
1498 status->op_ModC = NULL;
1499 status->op_DivModC = NULL;
1502 /* register the dumper */
1503 stat_register_dumper(&simple_dumper);
1505 if (enable_options & FIRMSTAT_CSV_OUTPUT)
1506 stat_register_dumper(&csv_dumper);
1508 /* initialize the pattern hash */
1509 stat_init_pattern_history(enable_options & FIRMSTAT_PATTERN_ENABLED);
1516 /* Finish the statistics */
1517 void stat_finish(const char *name) {}
1519 #endif /* FIRM_STATISTICS */