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 static node_entry_t *opcode_get_entry(const ir_op *op, pset *set)
168 elem = pset_find(set, &key, op->code);
172 elem = obstack_alloc(&status->cnts, sizeof(*elem));
175 opcode_clear_entry(elem);
179 return pset_insert(set, elem, op->code);
183 * Returns the associates ir_op for an opcode
185 static ir_op *opcode_find_entry(opcode code, pset *set)
190 return pset_find(set, &key, code);
194 * clears all counter in a graph_entry_t
196 static void graph_clear_entry(graph_entry_t *elem, int all)
199 cnt_clr(&elem->cnt_walked);
200 cnt_clr(&elem->cnt_walked_blocks);
201 cnt_clr(&elem->cnt_was_inlined);
202 cnt_clr(&elem->cnt_got_inlined);
203 cnt_clr(&elem->cnt_strength_red);
205 cnt_clr(&elem->cnt_edges);
206 cnt_clr(&elem->cnt_all_calls);
207 cnt_clr(&elem->cnt_indirect_calls);
211 * Returns the associated graph_entry_t for an irg
213 static graph_entry_t *graph_get_entry(ir_graph *irg, pset *set)
221 elem = pset_find(set, &key, HASH_PTR(irg));
225 /* allocate a new one */
226 elem = obstack_alloc(&status->cnts, sizeof(*elem));
229 graph_clear_entry(elem, 1);
231 /* new hash table for opcodes here */
232 elem->opcode_hash = new_pset(opcode_cmp, 5);
233 elem->block_hash = new_pset(block_cmp, 5);
234 elem->address_mark = new_set(address_mark_cmp, 5);
237 for (i = 0; i < sizeof(elem->opt_hash)/sizeof(elem->opt_hash[0]); ++i)
238 elem->opt_hash[i] = new_pset(opt_cmp, 4);
240 return pset_insert(set, elem, HASH_PTR(irg));
244 * clears all counter in an opt_entry_t
246 static void opt_clear_entry(opt_entry_t *elem)
248 cnt_clr(&elem->count);
252 * Returns the associated opt_entry_t for an ir_op
254 static opt_entry_t *opt_get_entry(const ir_op *op, pset *set)
261 elem = pset_find(set, &key, op->code);
265 elem = obstack_alloc(&status->cnts, sizeof(*elem));
267 /* clear new counter */
268 opt_clear_entry(elem);
272 return pset_insert(set, elem, op->code);
276 * clears all counter in a block_entry_t
278 static void block_clear_entry(block_entry_t *elem)
280 cnt_clr(&elem->cnt_nodes);
281 cnt_clr(&elem->cnt_edges);
282 cnt_clr(&elem->cnt_in_edges);
283 cnt_clr(&elem->cnt_out_edges);
287 * Returns the associated block_entry_t for an block
289 static block_entry_t *block_get_entry(long block_nr, pset *set)
294 key.block_nr = block_nr;
296 elem = pset_find(set, &key, block_nr);
300 elem = obstack_alloc(&status->cnts, sizeof(*elem));
302 /* clear new counter */
303 block_clear_entry(elem);
305 elem->block_nr = block_nr;
307 return pset_insert(set, elem, block_nr);
312 * Returns the ir_op for an IR-node,
313 * handles special cases and return pseudo op codes
315 static ir_op *stat_get_irn_op(ir_node *node)
317 ir_op *op = get_irn_op(node);
319 if (op == op_Phi && get_irn_arity(node) == 0) {
320 /* special case, a Phi0 node, count on extra counter */
321 op = status->op_Phi0 ? status->op_Phi0 : op;
323 else if (op == op_Phi && get_irn_mode(node) == mode_M) {
324 /* special case, a Memory Phi node, count on extra counter */
325 op = status->op_PhiM ? status->op_PhiM : op;
327 else if (op == op_Proj && get_irn_mode(node) == mode_M) {
328 /* special case, a Memory Proj node, count on extra counter */
329 op = status->op_ProjM ? status->op_ProjM : op;
331 else if (op == op_Mul &&
332 (get_irn_op(get_Mul_left(node)) == op_Const || get_irn_op(get_Mul_right(node)) == op_Const)) {
333 /* special case, a Multiply by a const, count on extra counter */
334 op = status->op_MulC ? status->op_MulC : op;
336 else if (op == op_Div && get_irn_op(get_Div_right(node)) == op_Const) {
337 /* special case, a division by a const, count on extra counter */
338 op = status->op_DivC ? status->op_DivC : op;
340 else if (op == op_Mod && get_irn_op(get_Mod_right(node)) == op_Const) {
341 /* special case, a module by a const, count on extra counter */
342 op = status->op_ModC ? status->op_ModC : op;
344 else if (op == op_DivMod && get_irn_op(get_DivMod_right(node)) == op_Const) {
345 /* special case, a division/modulo by a const, count on extra counter */
346 op = status->op_DivModC ? status->op_DivModC : op;
353 * update the block counter
355 static void count_block_info(ir_node *node, graph_entry_t *graph)
357 ir_op *op = get_irn_op(node);
359 block_entry_t *b_entry;
362 /* check for block */
363 if (op == op_Block) {
364 arity = get_irn_arity(node);
365 b_entry = block_get_entry(get_irn_node_nr(node), graph->block_hash);
367 /* count all incoming edges */
368 for (i = 0; i < arity; ++i) {
369 ir_node *pred = get_irn_n(node, i);
370 ir_node *other_block = get_nodes_block(pred);
371 block_entry_t *b_entry_other = block_get_entry(get_irn_node_nr(other_block), graph->block_hash);
373 cnt_inc(&b_entry->cnt_in_edges); /* an edge coming from another block */
374 cnt_inc(&b_entry_other->cnt_out_edges);
379 block = get_nodes_block(node);
380 b_entry = block_get_entry(get_irn_node_nr(block), graph->block_hash);
382 /* we have a new nodes */
383 cnt_inc(&b_entry->cnt_nodes);
385 arity = get_irn_arity(node);
387 for (i = 0; i < arity; ++i) {
388 ir_node *pred = get_irn_n(node, i);
389 ir_node *other_block;
391 if (get_irn_op(pred) == op_Block)
394 other_block = get_nodes_block(pred);
396 if (other_block == block)
397 cnt_inc(&b_entry->cnt_edges); /* a in block edge */
399 block_entry_t *b_entry_other = block_get_entry(get_irn_node_nr(other_block), graph->block_hash);
401 cnt_inc(&b_entry->cnt_in_edges); /* an edge coming from another block */
402 cnt_inc(&b_entry_other->cnt_out_edges);
408 * update info on calls
410 static void update_call_stat(ir_node *call, graph_entry_t *graph)
412 ir_node *block = get_nodes_block(call);
413 ir_node *ptr = get_Call_ptr(call);
415 ir_graph *callee = NULL;
418 * If the block is bad, the whole subgraph will collapse later
419 * so do not count this call.
420 * This happens in dead code.
425 cnt_inc(&graph->cnt_all_calls);
427 /* found a call, this function is not a leaf */
430 if (get_irn_op(ptr) == op_SymConst) {
431 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
432 /* ok, we seems to know the entity */
433 ent = get_SymConst_entity(ptr);
434 callee = get_entity_irg(ent);
436 /* it is recursive, if it calls at least once */
437 if (callee == graph->irg)
438 graph->is_recursive = 1;
442 /* indirect call, be could not predict */
443 cnt_inc(&graph->cnt_indirect_calls);
445 /* NOT a leaf call */
446 graph->is_leaf_call = LCS_NON_LEAF_CALL;
449 /* check, if it's a chain-call: Then, the call-block
450 * must dominate the end block. */
452 ir_node *curr = get_irg_end_block(graph->irg);
453 int depth = get_Block_dom_depth(block);
455 for (; curr != block && get_Block_dom_depth(curr) > depth;) {
456 curr = get_Block_idom(curr);
458 if (! curr || is_no_Block(curr))
463 graph->is_chain_call = 0;
466 /* check, if the callee is a leaf */
468 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
470 if (called->is_analyzed) {
471 if (! called->is_leaf)
472 graph->is_leaf_call = LCS_NON_LEAF_CALL;
478 * update info on calls for graphs on the wait queue
480 static void update_call_stat_2(ir_node *call, graph_entry_t *graph)
482 ir_node *block = get_nodes_block(call);
483 ir_node *ptr = get_Call_ptr(call);
485 ir_graph *callee = NULL;
488 * If the block is bad, the whole subgraph will collapse later
489 * so do not count this call.
490 * This happens in dead code.
495 if (get_irn_op(ptr) == op_SymConst) {
496 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
497 /* ok, we seems to know the entity */
498 ent = get_SymConst_entity(ptr);
499 callee = get_entity_irg(ent);
503 /* check, if the callee is a leaf */
505 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
507 assert(called->is_analyzed);
509 if (! called->is_leaf)
510 graph->is_leaf_call = LCS_NON_LEAF_CALL;
513 graph->is_leaf_call = LCS_NON_LEAF_CALL;
517 * walker for reachable nodes count
519 static void update_node_stat(ir_node *node, void *env)
521 graph_entry_t *graph = env;
524 ir_op *op = stat_get_irn_op(node);
525 int arity = get_irn_arity(node);
527 entry = opcode_get_entry(op, graph->opcode_hash);
529 cnt_inc(&entry->cnt_alive);
530 cnt_add_i(&graph->cnt_edges, arity);
532 /* count block edges */
533 count_block_info(node, graph);
535 /* check for properties that depends on calls like recursion/leaf/indirect call */
537 update_call_stat(node, graph);
541 * walker for reachable nodes count for graphs on the wait_q
543 static void update_node_stat_2(ir_node *node, void *env)
545 graph_entry_t *graph = env;
547 /* check for properties that depends on calls like recursion/leaf/indirect call */
548 if (get_irn_op(node) == op_Call)
549 update_call_stat_2(node, graph);
553 * get the current address mark
555 static unsigned get_adr_mark(graph_entry_t *graph, ir_node *node)
557 address_mark_entry_t *value = set_find(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
559 return value ? value->mark : 0;
563 * set the current address mark
565 static void set_adr_mark(graph_entry_t *graph, ir_node *node, unsigned val)
567 address_mark_entry_t *value = set_insert(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
573 * a vcg attribute hook: Color a node with a different color if
574 * it's identified as a part of an address expression or at least referenced
575 * by an address expression.
577 static int stat_adr_mark_hook(FILE *F, ir_node *node, ir_node *local)
579 ir_node *n = local ? local : node;
580 ir_graph *irg = get_irn_irg(n);
581 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
582 unsigned mark = get_adr_mark(graph, n);
584 if (mark & MARK_ADDRESS_CALC)
585 fprintf(F, "color: purple");
586 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
587 fprintf(F, "color: pink");
588 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
589 fprintf(F, "color: lightblue");
593 /* I know the color! */
598 * walker that marks every node that is an address calculation
600 * predecessor nodes must be visited first. We ensure this by
601 * calling in in the post of an outs walk. This should work even in cycles,
602 * while the pre in a normal walk will not.
604 static void mark_address_calc(ir_node *node, void *env)
606 graph_entry_t *graph = env;
607 ir_mode *mode = get_irn_mode(node);
609 unsigned mark_preds = MARK_REF_NON_ADR;
611 if (! mode_is_numP(mode))
614 if (mode_is_reference(mode)) {
615 /* a reference is calculated here, we are sure */
616 set_adr_mark(graph, node, MARK_ADDRESS_CALC);
618 mark_preds = MARK_REF_ADR;
621 unsigned mark = get_adr_mark(graph, node);
623 if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR) {
625 * this node has not an reference mode, but is only
626 * referenced by address calculations
628 mark_preds = MARK_REF_ADR;
632 /* mark all predecessors */
633 for (i = 0, n = get_irn_arity(node); i < n; ++i) {
634 ir_node *pred = get_irn_n(node, i);
636 set_adr_mark(graph, pred, get_adr_mark(graph, pred) | mark_preds);
641 * Called for every graph when the graph is either deleted or stat_finish()
642 * is called, must recalculate all statistic info.
644 * @param global The global entry
645 * @param graph The current entry
647 static void update_graph_stat(graph_entry_t *global, graph_entry_t *graph)
651 /* clear first the alive counter in the graph */
652 for (entry = pset_first(graph->opcode_hash); entry; entry = pset_next(graph->opcode_hash)) {
653 cnt_clr(&entry->cnt_alive);
656 /* set pessimistic values */
658 graph->is_leaf_call = LCS_UNKNOWN;
659 graph->is_recursive = 0;
660 graph->is_chain_call = 1;
662 /* we need dominator info */
663 if (graph->irg != get_const_code_irg())
664 if (get_irg_dom_state(graph->irg) != dom_consistent)
665 compute_doms(graph->irg);
667 /* count the nodes in the graph */
668 irg_walk_graph(graph->irg, update_node_stat, NULL, graph);
671 entry = opcode_get_entry(op_Call, graph->opcode_hash);
673 /* check if we have more than 1 call */
674 if (cnt_gt(entry->cnt_alive, 1))
675 graph->is_chain_call = 0;
678 /* recursive functions are never chain calls, leafs don't have calls */
679 if (graph->is_recursive || graph->is_leaf)
680 graph->is_chain_call = 0;
682 /* assume we walk every graph only ONCE, we could sum here the global count */
683 for (entry = pset_first(graph->opcode_hash); entry; entry = pset_next(graph->opcode_hash)) {
684 node_entry_t *g_entry = opcode_get_entry(entry->op, global->opcode_hash);
686 /* update the node counter */
687 cnt_add(&g_entry->cnt_alive, &entry->cnt_alive);
690 /* update the edge counter */
691 cnt_add(&global->cnt_edges, &graph->cnt_edges);
693 /* count the number of address calculation */
694 if (graph->irg != get_const_code_irg()) {
695 ir_graph *rem = current_ir_graph;
697 if (get_irg_outs_state(graph->irg) != outs_consistent)
698 compute_outs(graph->irg);
700 /* Must be done an the outs graph */
701 current_ir_graph = graph->irg;
702 irg_out_walk(get_irg_start(graph->irg), NULL, mark_address_calc, graph);
703 current_ir_graph = rem;
706 set_dump_node_vcgattr_hook(stat_adr_mark_hook);
707 dump_ir_block_graph(graph->irg, "-adr");
708 set_dump_node_vcgattr_hook(NULL);
712 /* count the DAG's */
713 if (status->stat_options & FIRMSTAT_COUNT_DAG)
714 count_dags_in_graph(global, graph);
716 /* calculate the patterns of this graph */
717 stat_calc_pattern_history(graph->irg);
719 /* leaf function did not call others */
721 graph->is_leaf_call = LCS_NON_LEAF_CALL;
722 else if (graph->is_leaf_call == LCS_UNKNOWN) {
723 /* we still don't know if this graph calls leaf-functions, so enqueue */
724 pdeq_putl(status->wait_q, graph);
727 /* we have analyzed this graph */
728 graph->is_analyzed = 1;
732 * Called for every graph that was on the wait_q in stat_finish()
733 * must finish all statistic info calculations.
735 * @param global The global entry
736 * @param graph The current entry
738 static void update_graph_stat_2(graph_entry_t *global, graph_entry_t *graph)
740 if (graph->is_deleted) {
741 /* deleted, ignore */
746 /* count the nodes in the graph */
747 irg_walk_graph(graph->irg, update_node_stat_2, NULL, graph);
749 if (graph->is_leaf_call == LCS_UNKNOWN)
750 graph->is_leaf_call = LCS_LEAF_CALL;
757 static void stat_register_dumper(const dumper_t *dumper)
759 dumper_t *p = xmalloc(sizeof(*p));
764 p->next = status->dumper;
769 /* FIXME: memory leak */
775 static void stat_dump_graph(graph_entry_t *entry)
779 for (dumper = status->dumper; dumper; dumper = dumper->next) {
780 if (dumper->dump_graph)
781 dumper->dump_graph(dumper, entry);
786 * initialize the dumper
788 static void stat_dump_init(const char *name)
792 for (dumper = status->dumper; dumper; dumper = dumper->next) {
794 dumper->init(dumper, name);
801 static void stat_dump_finish(void)
805 for (dumper = status->dumper; dumper; dumper = dumper->next) {
807 dumper->finish(dumper);
811 /* ---------------------------------------------------------------------- */
814 * helper: get an ir_op from an opcode
816 ir_op *stat_get_op_from_opcode(opcode code)
818 return opcode_find_entry(code, status->ir_op_hash);
821 /** A new IR op is registered. */
822 static void stat_new_ir_op(void *ctx, ir_op *op)
824 if (! status->stat_options)
829 graph_entry_t *graph = graph_get_entry(NULL, status->irg_hash);
831 /* execute for side effect :-) */
832 opcode_get_entry(op, graph->opcode_hash);
834 pset_insert(status->ir_op_hash, op, op->code);
839 /** An IR op is freed. */
840 static void stat_free_ir_op(void *ctx, ir_op *op)
842 if (! status->stat_options)
851 /** A new node is created. */
852 static void stat_new_node(void *ctx, ir_node *node)
854 if (! status->stat_options)
857 /* do NOT count during dead node elimination */
858 if (status->in_dead_node_elim > 0)
864 graph_entry_t *graph;
865 ir_op *op = stat_get_irn_op(node);
867 /* increase global value */
868 graph = graph_get_entry(NULL, status->irg_hash);
869 entry = opcode_get_entry(op, graph->opcode_hash);
870 cnt_inc(&entry->new_node);
872 /* increase local value */
873 graph = graph_get_entry(current_ir_graph, status->irg_hash);
874 entry = opcode_get_entry(op, graph->opcode_hash);
875 cnt_inc(&entry->new_node);
880 /** A node is changed into a Id node */
881 static void stat_turn_into_id(void *ctx, ir_node *node)
883 if (! status->stat_options)
889 graph_entry_t *graph;
890 ir_op *op = stat_get_irn_op(node);
892 /* increase global value */
893 graph = graph_get_entry(NULL, status->irg_hash);
894 entry = opcode_get_entry(op, graph->opcode_hash);
895 cnt_inc(&entry->into_Id);
897 /* increase local value */
898 graph = graph_get_entry(current_ir_graph, status->irg_hash);
899 entry = opcode_get_entry(op, graph->opcode_hash);
900 cnt_inc(&entry->into_Id);
905 /** A new graph was created */
906 static void stat_new_graph(void *ctx, ir_graph *irg, entity *ent)
908 if (! status->stat_options)
913 /* execute for side effect :-) */
914 graph_entry_t * graph = graph_get_entry(irg, status->irg_hash);
917 graph->is_deleted = 0;
919 graph->is_leaf_call = 0;
920 graph->is_recursive = 0;
921 graph->is_chain_call = 0;
922 graph->is_analyzed = 0;
928 * A graph will be deleted
930 static void stat_free_graph(void *ctx, ir_graph *irg)
932 if (! status->stat_options)
937 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
938 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
940 graph->is_deleted = 1;
942 if (status->stat_options & FIRMSTAT_COUNT_DELETED) {
943 /* count the nodes of the graph yet, it will be destroyed later */
944 update_graph_stat(global, graph);
951 * A walk over a graph is initiated. Do not count walks from statistic code.
953 static void stat_irg_walk(void *ctx, ir_graph *irg, void *pre, void *post)
955 if (! status->stat_options)
960 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
962 cnt_inc(&graph->cnt_walked);
968 * A walk over a graph in block-wise order is initiated. Do not count walks from statistic code.
970 static void stat_irg_walk_blkwise(void *ctx, ir_graph *irg, void *pre, void *post)
972 /* for now, do NOT differentiate between blockwise and normal */
973 stat_irg_walk(ctx, irg, pre, post);
977 * A walk over the graph's blocks is initiated. Do not count walks from statistic code.
979 static void stat_irg_block_walk(void *ctx, ir_graph *irg, ir_node *node, void *pre, void *post)
981 if (! status->stat_options)
986 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
988 cnt_inc(&graph->cnt_walked_blocks);
994 * called for every node that is removed due to an optimization
996 static void removed_due_opt(ir_node *n, pset *set)
998 ir_op *op = stat_get_irn_op(n);
999 opt_entry_t *entry = opt_get_entry(op, set);
1001 /* increase global value */
1002 cnt_inc(&entry->count);
1006 * Some nodes were optimized into some others due to an optimization
1008 static void stat_merge_nodes(
1010 ir_node **new_node_array, int new_num_entries,
1011 ir_node **old_node_array, int old_num_entries,
1014 if (! status->stat_options)
1020 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1022 if (status->reassoc_run)
1023 opt = HOOK_OPT_REASSOC;
1025 for (i = 0; i < old_num_entries; ++i) {
1026 for (j = 0; j < new_num_entries; ++j)
1027 if (old_node_array[i] == new_node_array[j])
1030 /* nodes might be in new and old, these are NOT removed */
1031 if (j >= new_num_entries) {
1032 removed_due_opt(old_node_array[i], graph->opt_hash[opt]);
1040 * reassociation started/stopped.
1042 static void stat_reassociate(void *ctx, int flag)
1044 if (! status->stat_options)
1049 status->reassoc_run = flag;
1055 * A node was lowered into other nodes
1057 static void stat_lower(void *ctx, ir_node *node)
1059 if (! status->stat_options)
1064 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1066 removed_due_opt(node, graph->opt_hash[HOOK_LOWERED]);
1072 * A graph was inlined
1074 static void stat_inline(void *ctx, ir_node *call, ir_graph *called_irg)
1076 if (! status->stat_options)
1081 ir_graph *irg = get_irn_irg(call);
1082 graph_entry_t *i_graph = graph_get_entry(called_irg, status->irg_hash);
1083 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1085 cnt_inc(&graph->cnt_got_inlined);
1086 cnt_inc(&i_graph->cnt_was_inlined);
1092 * A graph with tail-recursions was optimized.
1094 static void stat_tail_rec(void *ctx, ir_graph *irg)
1096 if (! status->stat_options)
1106 * Strength reduction was performed on an iteration variable.
1108 static void stat_strength_red(void *ctx, ir_graph *irg, ir_node *strong, ir_node *cmp)
1110 if (! status->stat_options)
1115 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1116 cnt_inc(&graph->cnt_strength_red);
1118 removed_due_opt(strong, graph->opt_hash[HOOK_OPT_STRENGTH_RED]);
1124 * Start the dead node elimination.
1126 static void stat_dead_node_elim_start(void *ctx, ir_graph *irg)
1128 if (! status->stat_options)
1131 ++status->in_dead_node_elim;
1135 * Stops the dead node elimination.
1137 static void stat_dead_node_elim_stop(void *ctx, ir_graph *irg)
1139 if (! status->stat_options)
1142 --status->in_dead_node_elim;
1146 * A multiply was replaced by a series of Shifts/Adds/Subs
1148 static void stat_arch_dep_replace_mul_with_shifts(void *ctx, ir_node *mul)
1150 if (! status->stat_options)
1155 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1156 removed_due_opt(mul, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1162 * A division was replaced by a series of Shifts/Muls
1164 static void stat_arch_dep_replace_div_by_const(void *ctx, ir_node *div)
1166 if (! status->stat_options)
1171 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1172 removed_due_opt(div, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1178 * A modulo was replaced by a series of Shifts/Muls
1180 static void stat_arch_dep_replace_mod_by_const(void *ctx, ir_node *mod)
1182 if (! status->stat_options)
1187 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1188 removed_due_opt(mod, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1194 * A DivMod was replaced by a series of Shifts/Muls
1196 static void stat_arch_dep_replace_DivMod_by_const(void *ctx, ir_node *divmod)
1198 if (! status->stat_options)
1203 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1204 removed_due_opt(divmod, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1209 /* Finish the statistics */
1210 void stat_finish(const char *name)
1212 if (! status->stat_options)
1217 graph_entry_t *entry;
1218 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1220 stat_dump_init(name);
1222 /* calculate the graph statistics */
1223 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
1225 if (entry->irg == NULL) {
1226 /* special entry for the global count */
1230 if (! entry->is_deleted) {
1231 /* the graph is still alive, count the nodes on it */
1232 update_graph_stat(global, entry);
1236 /* some calculations are dependent, we pushed them on the wait_q */
1237 while (! pdeq_empty(status->wait_q)) {
1238 entry = pdeq_getr(status->wait_q);
1240 update_graph_stat_2(global, entry);
1244 /* dump per graph */
1245 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
1247 if (entry->irg == NULL) {
1248 /* special entry for the global count */
1252 if (! entry->is_deleted || status->stat_options & FIRMSTAT_COUNT_DELETED)
1253 stat_dump_graph(entry);
1255 if (! entry->is_deleted) {
1256 /* clear the counter that are not accumulated */
1257 graph_clear_entry(entry, 0);
1262 stat_dump_graph(global);
1265 stat_finish_pattern_history();
1267 /* clear the global counter here */
1269 node_entry_t *entry;
1271 for (entry = pset_first(global->opcode_hash); entry; entry = pset_next(global->opcode_hash)) {
1272 opcode_clear_entry(entry);
1274 /* clear all global counter */
1275 graph_clear_entry(global, 1);
1279 // status->stat_options = 0;
1284 static hook_entry_t stat_hooks[hook_last];
1286 /* initialize the statistics module. */
1287 void init_stat(unsigned enable_options)
1289 #define X(a) a, sizeof(a)-1
1290 #define HOOK(h, fkt) \
1291 stat_hooks[h].hook._##h = fkt; register_hook(h, &stat_hooks[h])
1293 /* enable statistics */
1294 status->stat_options = enable_options & FIRMSTAT_ENABLED ? enable_options : 0;
1296 if (! status->stat_options)
1299 /* register all hooks */
1300 HOOK(hook_new_ir_op, stat_new_ir_op);
1301 HOOK(hook_free_ir_op, stat_free_ir_op);
1302 HOOK(hook_new_node, stat_new_node);
1303 HOOK(hook_turn_into_id, stat_turn_into_id);
1304 HOOK(hook_new_graph, stat_new_graph);
1305 HOOK(hook_free_graph, stat_free_graph);
1306 HOOK(hook_irg_walk, stat_irg_walk);
1307 HOOK(hook_irg_walk_blkwise, stat_irg_walk_blkwise);
1308 HOOK(hook_irg_block_walk, stat_irg_block_walk);
1309 HOOK(hook_merge_nodes, stat_merge_nodes);
1310 HOOK(hook_reassociate, stat_reassociate);
1311 HOOK(hook_lower, stat_lower);
1312 HOOK(hook_inline, stat_inline);
1313 HOOK(hook_tail_rec, stat_tail_rec);
1314 HOOK(hook_strength_red, stat_strength_red);
1315 HOOK(hook_dead_node_elim_start, stat_dead_node_elim_start);
1316 HOOK(hook_dead_node_elim_stop, stat_dead_node_elim_stop);
1317 HOOK(hook_arch_dep_replace_mul_with_shifts, stat_arch_dep_replace_mul_with_shifts);
1318 HOOK(hook_arch_dep_replace_div_by_const, stat_arch_dep_replace_div_by_const);
1319 HOOK(hook_arch_dep_replace_mod_by_const, stat_arch_dep_replace_mod_by_const);
1320 HOOK(hook_arch_dep_replace_DivMod_by_const, stat_arch_dep_replace_DivMod_by_const);
1322 obstack_init(&status->cnts);
1324 /* create the hash-tables */
1325 status->irg_hash = new_pset(graph_cmp, 8);
1326 status->ir_op_hash = new_pset(opcode_cmp_2, 1);
1328 /* create the wait queue */
1329 status->wait_q = new_pdeq();
1331 if (enable_options & FIRMSTAT_COUNT_STRONG_OP) {
1332 /* build the pseudo-ops */
1333 _op_Phi0.code = get_next_ir_opcode();
1334 _op_Phi0.name = new_id_from_chars(X("Phi0"));
1336 _op_PhiM.code = get_next_ir_opcode();
1337 _op_PhiM.name = new_id_from_chars(X("PhiM"));
1339 _op_ProjM.code = get_next_ir_opcode();
1340 _op_ProjM.name = new_id_from_chars(X("ProjM"));
1342 _op_MulC.code = get_next_ir_opcode();
1343 _op_MulC.name = new_id_from_chars(X("MulC"));
1345 _op_DivC.code = get_next_ir_opcode();
1346 _op_DivC.name = new_id_from_chars(X("DivC"));
1348 _op_ModC.code = get_next_ir_opcode();
1349 _op_ModC.name = new_id_from_chars(X("ModC"));
1351 _op_DivModC.code = get_next_ir_opcode();
1352 _op_DivModC.name = new_id_from_chars(X("DivModC"));
1354 status->op_Phi0 = &_op_Phi0;
1355 status->op_PhiM = &_op_PhiM;
1356 status->op_ProjM = &_op_ProjM;
1357 status->op_MulC = &_op_MulC;
1358 status->op_DivC = &_op_DivC;
1359 status->op_ModC = &_op_ModC;
1360 status->op_DivModC = &_op_DivModC;
1363 status->op_Phi0 = NULL;
1364 status->op_PhiM = NULL;
1365 status->op_ProjM = NULL;
1366 status->op_MulC = NULL;
1367 status->op_DivC = NULL;
1368 status->op_ModC = NULL;
1369 status->op_DivModC = NULL;
1372 /* register the dumper */
1373 stat_register_dumper(&simple_dumper);
1375 if (enable_options & FIRMSTAT_CSV_OUTPUT)
1376 stat_register_dumper(&csv_dumper);
1378 /* initialize the pattern hash */
1379 stat_init_pattern_history(enable_options & FIRMSTAT_PATTERN_ENABLED);
1384 #endif /* FIRM_STATISTICS */