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"
36 * need this to be static:
37 * Special pseudo Opcodes that we need to count some interesting cases
41 * The Phi0, a node that is created during SSA construction
43 static ir_op _op_Phi0;
45 /** The PhiM, just to count memorty Phi's. */
46 static ir_op _op_PhiM;
48 /** The Mul by Const node. */
49 static ir_op _op_MulC;
51 /** The Div by Const node. */
52 static ir_op _op_DivC;
54 /** The Div by Const node. */
55 static ir_op _op_ModC;
57 /** The Div by Const node. */
58 static ir_op _op_DivModC;
60 /** The memory Proj node. */
61 static ir_op _op_ProjM;
63 /* ---------------------------------------------------------------------------------- */
65 /** Marks the begin of a statistic (hook) function. */
66 #define STAT_ENTER ++status->recursive
68 /** Marks the end of a statistic (hook) functions. */
69 #define STAT_LEAVE --status->recursive
71 /** Allows to enter a statistic function only when we are not already in a hook. */
72 #define STAT_ENTER_SINGLE do { if (status->recursive > 0) return; ++status->recursive; } while (0)
77 static stat_info_t _status, *status = &_status;
80 * compare two elements of the opcode hash
82 static int opcode_cmp(const void *elt, const void *key)
84 const node_entry_t *e1 = elt;
85 const node_entry_t *e2 = key;
87 return e1->op->code - e2->op->code;
91 * compare two elements of the graph hash
93 static int graph_cmp(const void *elt, const void *key)
95 const graph_entry_t *e1 = elt;
96 const graph_entry_t *e2 = key;
98 return e1->irg != e2->irg;
102 * compare two elements of the optimization hash
104 static int opt_cmp(const void *elt, const void *key)
106 const opt_entry_t *e1 = elt;
107 const opt_entry_t *e2 = key;
109 return e1->op->code != e2->op->code;
113 * compare two elements of the block hash
115 static int block_cmp(const void *elt, const void *key)
117 const block_entry_t *e1 = elt;
118 const block_entry_t *e2 = key;
120 return e1->block_nr != e2->block_nr;
124 * compare two elements of the ir_op hash
126 static int opcode_cmp_2(const void *elt, const void *key)
128 const ir_op *e1 = elt;
129 const ir_op *e2 = key;
131 return e1->code != e2->code;
135 * compare two elements of the address_mark set
137 static int address_mark_cmp(const void *elt, const void *key, size_t size)
139 const address_mark_entry_t *e1 = elt;
140 const address_mark_entry_t *e2 = key;
142 /* compare only the nodes, the rest is used as data container */
143 return e1->node != e2->node;
147 * clears all counter in a node_entry_t
149 static void opcode_clear_entry(node_entry_t *elem)
151 cnt_clr(&elem->cnt_alive);
152 cnt_clr(&elem->new_node);
153 cnt_clr(&elem->into_Id);
157 * Returns the associates node_entry_t for an ir_op
159 static node_entry_t *opcode_get_entry(const ir_op *op, pset *set)
166 elem = pset_find(set, &key, op->code);
170 elem = obstack_alloc(&status->cnts, sizeof(*elem));
173 opcode_clear_entry(elem);
177 return pset_insert(set, elem, op->code);
181 * Returns the associates ir_op for an opcode
183 static ir_op *opcode_find_entry(opcode code, pset *set)
188 return pset_find(set, &key, code);
192 * clears all counter in a graph_entry_t
194 static void graph_clear_entry(graph_entry_t *elem, int all)
197 cnt_clr(&elem->cnt_walked);
198 cnt_clr(&elem->cnt_walked_blocks);
199 cnt_clr(&elem->cnt_was_inlined);
200 cnt_clr(&elem->cnt_got_inlined);
201 cnt_clr(&elem->cnt_strength_red);
203 cnt_clr(&elem->cnt_edges);
204 cnt_clr(&elem->cnt_all_calls);
205 cnt_clr(&elem->cnt_indirect_calls);
209 * Returns the acssociates graph_entry_t for an irg
211 static graph_entry_t *graph_get_entry(ir_graph *irg, pset *set)
219 elem = pset_find(set, &key, HASH_PTR(irg));
223 /* allocate a new one */
224 elem = obstack_alloc(&status->cnts, sizeof(*elem));
227 graph_clear_entry(elem, 1);
229 /* new hash table for opcodes here */
230 elem->opcode_hash = new_pset(opcode_cmp, 5);
231 elem->block_hash = new_pset(block_cmp, 5);
232 elem->address_mark = new_set(address_mark_cmp, 5);
235 for (i = 0; i < sizeof(elem->opt_hash)/sizeof(elem->opt_hash[0]); ++i)
236 elem->opt_hash[i] = new_pset(opt_cmp, 4);
238 return pset_insert(set, elem, HASH_PTR(irg));
242 * clears all counter in an opt_entry_t
244 static void opt_clear_entry(opt_entry_t *elem)
246 cnt_clr(&elem->count);
250 * Returns the associates opt_entry_t for an ir_op
252 static opt_entry_t *opt_get_entry(const ir_op *op, pset *set)
259 elem = pset_find(set, &key, op->code);
263 elem = obstack_alloc(&status->cnts, sizeof(*elem));
265 /* clear new counter */
266 opt_clear_entry(elem);
270 return pset_insert(set, elem, op->code);
274 * clears all counter in a block_entry_t
276 static void block_clear_entry(block_entry_t *elem)
278 cnt_clr(&elem->cnt_nodes);
279 cnt_clr(&elem->cnt_edges);
280 cnt_clr(&elem->cnt_in_edges);
281 cnt_clr(&elem->cnt_out_edges);
285 * Returns the associates block_entry_t for an block
287 static block_entry_t *block_get_entry(long block_nr, pset *set)
292 key.block_nr = block_nr;
294 elem = pset_find(set, &key, block_nr);
298 elem = obstack_alloc(&status->cnts, sizeof(*elem));
300 /* clear new counter */
301 block_clear_entry(elem);
303 elem->block_nr = block_nr;
305 return pset_insert(set, elem, block_nr);
310 * Returns the ir_op for an IR-node,
311 * handles special cases and return pseudo op codes
313 static ir_op *stat_get_irn_op(ir_node *node)
315 ir_op *op = get_irn_op(node);
317 if (op == op_Phi && get_irn_arity(node) == 0) {
318 /* special case, a Phi0 node, count on extra counter */
319 op = status->op_Phi0 ? status->op_Phi0 : op;
321 else if (op == op_Phi && get_irn_mode(node) == mode_M) {
322 /* special case, a Memory Phi node, count on extra counter */
323 op = status->op_PhiM ? status->op_PhiM : op;
325 else if (op == op_Proj && get_irn_mode(node) == mode_M) {
326 /* special case, a Memory Proj node, count on extra counter */
327 op = status->op_ProjM ? status->op_ProjM : op;
329 else if (op == op_Mul &&
330 (get_irn_op(get_Mul_left(node)) == op_Const || get_irn_op(get_Mul_right(node)) == op_Const)) {
331 /* special case, a Multiply by a const, count on extra counter */
332 op = status->op_MulC ? status->op_MulC : op;
334 else if (op == op_Div && get_irn_op(get_Div_right(node)) == op_Const) {
335 /* special case, a division by a const, count on extra counter */
336 op = status->op_DivC ? status->op_DivC : op;
338 else if (op == op_Mod && get_irn_op(get_Mod_right(node)) == op_Const) {
339 /* special case, a module by a const, count on extra counter */
340 op = status->op_ModC ? status->op_ModC : op;
342 else if (op == op_DivMod && get_irn_op(get_DivMod_right(node)) == op_Const) {
343 /* special case, a division/modulo by a const, count on extra counter */
344 op = status->op_DivModC ? status->op_DivModC : op;
351 * update the block counter
353 static void count_block_info(ir_node *node, graph_entry_t *graph)
355 ir_op *op = get_irn_op(node);
357 block_entry_t *b_entry;
360 /* check for block */
361 if (op == op_Block) {
362 arity = get_irn_arity(node);
363 b_entry = block_get_entry(get_irn_node_nr(node), graph->block_hash);
365 /* count all incoming edges */
366 for (i = 0; i < arity; ++i) {
367 ir_node *pred = get_irn_n(node, i);
368 ir_node *other_block = get_nodes_block(pred);
369 block_entry_t *b_entry_other = block_get_entry(get_irn_node_nr(other_block), graph->block_hash);
371 cnt_inc(&b_entry->cnt_in_edges); /* an edge coming from another block */
372 cnt_inc(&b_entry_other->cnt_out_edges);
377 block = get_nodes_block(node);
378 b_entry = block_get_entry(get_irn_node_nr(block), graph->block_hash);
380 /* we have a new nodes */
381 cnt_inc(&b_entry->cnt_nodes);
383 arity = get_irn_arity(node);
385 for (i = 0; i < arity; ++i) {
386 ir_node *pred = get_irn_n(node, i);
387 ir_node *other_block;
389 if (get_irn_op(pred) == op_Block)
392 other_block = get_nodes_block(pred);
394 if (other_block == block)
395 cnt_inc(&b_entry->cnt_edges); /* a in block edge */
397 block_entry_t *b_entry_other = block_get_entry(get_irn_node_nr(other_block), graph->block_hash);
399 cnt_inc(&b_entry->cnt_in_edges); /* an edge coming from another block */
400 cnt_inc(&b_entry_other->cnt_out_edges);
406 * update info on calls
408 static void update_call_stat(ir_node *call, graph_entry_t *graph)
410 ir_node *block = get_nodes_block(call);
411 ir_node *ptr = get_Call_ptr(call);
413 ir_graph *callee = NULL;
416 * If the block is bad, the whole subgraph will colabse later
417 * so do not count this call.
418 * This happens in dead code.
423 cnt_inc(&graph->cnt_all_calls);
425 /* found a call, this function is not a leaf */
428 if (get_irn_op(ptr) == op_SymConst) {
429 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
430 /* ok, we seems to know the entity */
431 ent = get_SymConst_entity(ptr);
432 callee = get_entity_irg(ent);
434 /* it is recursive, if it calls at least once */
435 if (callee == graph->irg)
436 graph->is_recursive = 1;
440 /* indirect call, be could not predict */
441 cnt_inc(&graph->cnt_indirect_calls);
443 /* NOT a leaf call */
444 graph->is_leaf_call = LCS_NON_LEAF_CALL;
447 /* check, if it's a chain-call: Then, the call-block
448 * must dominate the end block. */
450 ir_node *curr = get_irg_end_block(graph->irg);
451 int depth = get_Block_dom_depth(block);
453 for (; curr != block && get_Block_dom_depth(curr) > depth;) {
454 curr = get_Block_idom(curr);
456 if (! curr || is_no_Block(curr))
461 graph->is_chain_call = 0;
464 /* check, if the callee is a leaf */
466 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
468 if (called->is_analyzed) {
469 if (! called->is_leaf)
470 graph->is_leaf_call = LCS_NON_LEAF_CALL;
476 * update info on calls for graphs on the wait queue
478 static void update_call_stat_2(ir_node *call, graph_entry_t *graph)
480 ir_node *block = get_nodes_block(call);
481 ir_node *ptr = get_Call_ptr(call);
483 ir_graph *callee = NULL;
486 * If the block is bad, the whole subgraph will colabse later
487 * so do not count this call.
488 * This happens in dead code.
493 if (get_irn_op(ptr) == op_SymConst) {
494 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
495 /* ok, we seems to know the entity */
496 ent = get_SymConst_entity(ptr);
497 callee = get_entity_irg(ent);
501 /* check, if the callee is a leaf */
503 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
505 assert(called->is_analyzed);
507 if (! called->is_leaf)
508 graph->is_leaf_call = LCS_NON_LEAF_CALL;
511 graph->is_leaf_call = LCS_NON_LEAF_CALL;
515 * walker for reachable nodes count
517 static void update_node_stat(ir_node *node, void *env)
519 graph_entry_t *graph = env;
522 ir_op *op = stat_get_irn_op(node);
523 int arity = get_irn_arity(node);
525 entry = opcode_get_entry(op, graph->opcode_hash);
527 cnt_inc(&entry->cnt_alive);
528 cnt_add_i(&graph->cnt_edges, arity);
530 /* count block edges */
531 count_block_info(node, graph);
533 /* check for properties that depends on calls like recursion/leaf/indirect call */
535 update_call_stat(node, graph);
539 * walker for reachable nodes count for graphs on teh wait_q
541 static void update_node_stat_2(ir_node *node, void *env)
543 graph_entry_t *graph = env;
545 /* check for properties that depends on calls like recursion/leaf/indirect call */
546 if (get_irn_op(node) == op_Call)
547 update_call_stat_2(node, graph);
551 * get the current address mark
553 static unsigned get_adr_mark(graph_entry_t *graph, ir_node *node)
555 address_mark_entry_t *value = set_find(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
557 return value ? value->mark : 0;
561 * set the current address mark
563 static void set_adr_mark(graph_entry_t *graph, ir_node *node, unsigned val)
565 address_mark_entry_t *value = set_insert(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
571 * a vcg attribute hook: Color a node with a different color if
572 * it's identified as a part of an address expression or at least referenced
573 * by an address expression.
575 static int stat_adr_mark_hook(FILE *F, ir_node *node, ir_node *local)
577 ir_node *n = local ? local : node;
578 ir_graph *irg = get_irn_irg(n);
579 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
580 unsigned mark = get_adr_mark(graph, n);
582 if (mark & MARK_ADDRESS_CALC)
583 fprintf(F, "color: purple");
584 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
585 fprintf(F, "color: pink");
586 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
587 fprintf(F, "color: lightblue");
591 /* I know the color! */
596 * walker that marks every node that is an address calculation
598 * predecessor nodes must be visited first. We ensure this by
599 * calling in in the post of an outs walk. This should work even in cycles,
600 * while the pre in a normal walk will not.
602 static void mark_address_calc(ir_node *node, void *env)
604 graph_entry_t *graph = env;
605 ir_mode *mode = get_irn_mode(node);
607 unsigned mark_preds = MARK_REF_NON_ADR;
609 if (! mode_is_numP(mode))
612 if (mode_is_reference(mode)) {
613 /* a reference is calculated here, we are sure */
614 set_adr_mark(graph, node, MARK_ADDRESS_CALC);
616 mark_preds = MARK_REF_ADR;
619 unsigned mark = get_adr_mark(graph, node);
621 if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR) {
623 * this node has not an reference mode, but is only
624 * referenced by address calculations
626 mark_preds = MARK_REF_ADR;
630 /* mark all predecessors */
631 for (i = 0, n = get_irn_arity(node); i < n; ++i) {
632 ir_node *pred = get_irn_n(node, i);
634 set_adr_mark(graph, pred, get_adr_mark(graph, pred) | mark_preds);
639 * Called for every graph when the graph is either deleted or stat_finish()
640 * is called, must recalculate all statistic info.
642 * @param global The global entry
643 * @param graph The current entry
645 static void update_graph_stat(graph_entry_t *global, graph_entry_t *graph)
649 /* clear first the alive counter in the graph */
650 for (entry = pset_first(graph->opcode_hash); entry; entry = pset_next(graph->opcode_hash)) {
651 cnt_clr(&entry->cnt_alive);
654 /* set pessimistic values */
656 graph->is_leaf_call = LCS_UNKNOWN;
657 graph->is_recursive = 0;
658 graph->is_chain_call = 1;
660 /* we need dominator info */
661 if (graph->irg != get_const_code_irg())
662 if (get_irg_dom_state(graph->irg) != dom_consistent)
663 compute_doms(graph->irg);
665 /* count the nodes in the graph */
666 irg_walk_graph(graph->irg, update_node_stat, NULL, graph);
669 entry = opcode_get_entry(op_Call, graph->opcode_hash);
671 /* check if we have more than 1 call */
672 if (cnt_gt(entry->cnt_alive, 1))
673 graph->is_chain_call = 0;
676 /* recursive functions are never chain calls, leafs don't have calls */
677 if (graph->is_recursive || graph->is_leaf)
678 graph->is_chain_call = 0;
680 /* assume we walk every graph only ONCE, we could sum here the global count */
681 for (entry = pset_first(graph->opcode_hash); entry; entry = pset_next(graph->opcode_hash)) {
682 node_entry_t *g_entry = opcode_get_entry(entry->op, global->opcode_hash);
684 /* update the node counter */
685 cnt_add(&g_entry->cnt_alive, &entry->cnt_alive);
688 /* update the edge counter */
689 cnt_add(&global->cnt_edges, &graph->cnt_edges);
691 /* count the number of address calculation */
692 if (graph->irg != get_const_code_irg()) {
693 ir_graph *rem = current_ir_graph;
695 if (get_irg_outs_state(graph->irg) != outs_consistent)
696 compute_outs(graph->irg);
698 /* Must be done an the outs graph */
699 current_ir_graph = graph->irg;
700 irg_out_walk(get_irg_start(graph->irg), NULL, mark_address_calc, graph);
701 current_ir_graph = rem;
704 set_dump_node_vcgattr_hook(stat_adr_mark_hook);
705 dump_ir_block_graph(graph->irg, "-adr");
706 set_dump_node_vcgattr_hook(NULL);
710 /* count the DAG's */
711 if (status->stat_options & FIRMSTAT_COUNT_DAG)
712 count_dags_in_graph(global, graph);
714 /* calculate the patterns of this graph */
715 stat_calc_pattern_history(graph->irg);
717 /* leaf function did not call others */
719 graph->is_leaf_call = LCS_NON_LEAF_CALL;
720 else if (graph->is_leaf_call == LCS_UNKNOWN) {
721 /* we still don't know if this graph calls leaf-functions, so enqueue */
722 pdeq_putl(status->wait_q, graph);
725 /* we have analyzed this graph */
726 graph->is_analyzed = 1;
730 * Called for every graph that was on the wait_q in stat_finish()
731 * must finish all statistic info calculations.
733 * @param global The global entry
734 * @param graph The current entry
736 static void update_graph_stat_2(graph_entry_t *global, graph_entry_t *graph)
738 if (graph->is_deleted) {
739 /* deleted, ignore */
744 /* count the nodes in the graph */
745 irg_walk_graph(graph->irg, update_node_stat_2, NULL, graph);
747 if (graph->is_leaf_call == LCS_UNKNOWN)
748 graph->is_leaf_call = LCS_LEAF_CALL;
755 static void stat_register_dumper(const dumper_t *dumper)
757 dumper_t *p = malloc(sizeof(*p));
762 p->next = status->dumper;
767 /* FIXME: memory leak */
773 static void stat_dump_graph(graph_entry_t *entry)
777 for (dumper = status->dumper; dumper; dumper = dumper->next) {
778 if (dumper->dump_graph)
779 dumper->dump_graph(dumper, entry);
784 * initialise the dumper
786 static void stat_dump_init(const char *name)
790 for (dumper = status->dumper; dumper; dumper = dumper->next) {
792 dumper->init(dumper, name);
799 static void stat_dump_finish(void)
803 for (dumper = status->dumper; dumper; dumper = dumper->next) {
805 dumper->finish(dumper);
809 /* ---------------------------------------------------------------------- */
812 * helper: get an ir_op from an opcode
814 ir_op *stat_get_op_from_opcode(opcode code)
816 return opcode_find_entry(code, status->ir_op_hash);
819 /* initialize the statistics module. */
820 void init_stat(unsigned enable_options)
822 #define X(a) a, sizeof(a)-1
824 /* enable statistics */
825 status->stat_options = enable_options & FIRMSTAT_ENABLED ? enable_options : 0;
827 if (! status->stat_options)
830 obstack_init(&status->cnts);
832 /* create the hash-tables */
833 status->irg_hash = new_pset(graph_cmp, 8);
834 status->ir_op_hash = new_pset(opcode_cmp_2, 1);
836 /* create the wait queue */
837 status->wait_q = new_pdeq();
839 if (enable_options & FIRMSTAT_COUNT_STRONG_OP) {
840 /* build the pseudo-ops */
841 _op_Phi0.code = get_next_ir_opcode();
842 _op_Phi0.name = new_id_from_chars(X("Phi0"));
844 _op_PhiM.code = get_next_ir_opcode();
845 _op_PhiM.name = new_id_from_chars(X("PhiM"));
847 _op_ProjM.code = get_next_ir_opcode();
848 _op_ProjM.name = new_id_from_chars(X("ProjM"));
850 _op_MulC.code = get_next_ir_opcode();
851 _op_MulC.name = new_id_from_chars(X("MulC"));
853 _op_DivC.code = get_next_ir_opcode();
854 _op_DivC.name = new_id_from_chars(X("DivC"));
856 _op_ModC.code = get_next_ir_opcode();
857 _op_ModC.name = new_id_from_chars(X("ModC"));
859 _op_DivModC.code = get_next_ir_opcode();
860 _op_DivModC.name = new_id_from_chars(X("DivModC"));
862 status->op_Phi0 = &_op_Phi0;
863 status->op_PhiM = &_op_PhiM;
864 status->op_ProjM = &_op_ProjM;
865 status->op_MulC = &_op_MulC;
866 status->op_DivC = &_op_DivC;
867 status->op_ModC = &_op_ModC;
868 status->op_DivModC = &_op_DivModC;
871 status->op_Phi0 = NULL;
872 status->op_PhiM = NULL;
873 status->op_ProjM = NULL;
874 status->op_MulC = NULL;
875 status->op_DivC = NULL;
876 status->op_ModC = NULL;
877 status->op_DivModC = NULL;
880 /* register the dumper */
881 stat_register_dumper(&simple_dumper);
883 if (enable_options & FIRMSTAT_CSV_OUTPUT)
884 stat_register_dumper(&csv_dumper);
886 /* initialize the pattern hash */
887 stat_init_pattern_history(enable_options & FIRMSTAT_PATTERN_ENABLED);
891 /* A new IR op is registered. */
892 void stat_new_ir_op(const ir_op *op)
894 if (! status->stat_options)
899 graph_entry_t *graph = graph_get_entry(NULL, status->irg_hash);
901 /* execute for side effect :-) */
902 opcode_get_entry(op, graph->opcode_hash);
904 pset_insert(status->ir_op_hash, op, op->code);
909 /* An IR op is freed. */
910 void stat_free_ir_op(const ir_op *op)
912 if (! status->stat_options)
921 /* A new node is created. */
922 void stat_new_node(ir_node *node)
924 if (! status->stat_options)
927 /* do NOT count during dead node elimination */
928 if (status->in_dead_node_elim > 0)
934 graph_entry_t *graph;
935 ir_op *op = stat_get_irn_op(node);
937 /* increase global value */
938 graph = graph_get_entry(NULL, status->irg_hash);
939 entry = opcode_get_entry(op, graph->opcode_hash);
940 cnt_inc(&entry->new_node);
942 /* increase local value */
943 graph = graph_get_entry(current_ir_graph, status->irg_hash);
944 entry = opcode_get_entry(op, graph->opcode_hash);
945 cnt_inc(&entry->new_node);
950 /* A node is changed into a Id node */
951 void stat_turn_into_id(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);
975 /* A new graph was created */
976 void stat_new_graph(ir_graph *irg, entity *ent)
978 if (! status->stat_options)
983 /* execute for side effect :-) */
984 graph_entry_t * graph = graph_get_entry(irg, status->irg_hash);
987 graph->is_deleted = 0;
989 graph->is_leaf_call = 0;
990 graph->is_recursive = 0;
991 graph->is_chain_call = 0;
992 graph->is_analyzed = 0;
998 * A graph will be deleted
1000 void stat_free_graph(ir_graph *irg)
1002 if (! status->stat_options)
1007 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1008 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1010 graph->is_deleted = 1;
1012 if (status->stat_options & FIRMSTAT_COUNT_DELETED) {
1013 /* count the nodes of the graph yet, it will be destroyed later */
1014 update_graph_stat(global, graph);
1021 * A walk over a graph is initiated. Do not count walks from statistic code.
1023 void stat_irg_walk(ir_graph *irg, void *pre, void *post)
1025 if (! status->stat_options)
1030 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1032 cnt_inc(&graph->cnt_walked);
1038 * A walk over a graph in block-wise order is initiated. Do not count walks from statistic code.
1040 void stat_irg_walk_blkwise(ir_graph *irg, void *pre, void *post)
1042 /* for now, do NOT differentiate between blockwise and normal */
1043 stat_irg_walk(irg, pre, post);
1047 * A walk over the graph's blocks is initiated. Do not count walks from statistic code.
1049 void stat_irg_block_walk(ir_graph *irg, const ir_node *node, void *pre, void *post)
1051 if (! status->stat_options)
1056 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1058 cnt_inc(&graph->cnt_walked_blocks);
1064 * called for every node that is removed due to an optimization
1066 static void removed_due_opt(ir_node *n, pset *set)
1068 ir_op *op = stat_get_irn_op(n);
1069 opt_entry_t *entry = opt_get_entry(op, set);
1071 /* increase global value */
1072 cnt_inc(&entry->count);
1076 * Some nodes were optimized into some others due to an optimization
1078 void stat_merge_nodes(
1079 ir_node **new_node_array, int new_num_entries,
1080 ir_node **old_node_array, int old_num_entries,
1083 if (! status->stat_options)
1089 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1091 if (status->reassoc_run)
1092 opt = STAT_OPT_REASSOC;
1094 for (i = 0; i < old_num_entries; ++i) {
1095 for (j = 0; j < new_num_entries; ++j)
1096 if (old_node_array[i] == new_node_array[j])
1099 /* nodes might be in new and old, these are NOT removed */
1100 if (j >= new_num_entries) {
1101 removed_due_opt(old_node_array[i], graph->opt_hash[opt]);
1109 * reassociation started/stopped.
1111 void stat_reassociate(int flag)
1113 if (! status->stat_options)
1118 status->reassoc_run = flag;
1124 * A node was lowered into other nodes
1126 void stat_lower(ir_node *node)
1128 if (! status->stat_options)
1133 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1135 removed_due_opt(node, graph->opt_hash[STAT_LOWERED]);
1141 * A graph was inlined
1143 void stat_inline(ir_node *call, ir_graph *called_irg)
1145 if (! status->stat_options)
1150 ir_graph *irg = get_irn_irg(call);
1151 graph_entry_t *i_graph = graph_get_entry(called_irg, status->irg_hash);
1152 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1154 cnt_inc(&graph->cnt_got_inlined);
1155 cnt_inc(&i_graph->cnt_was_inlined);
1161 * A graph with tail-recursions was optimized.
1163 void stat_tail_rec(ir_graph *irg)
1165 if (! status->stat_options)
1175 * Strength reduction was performed on an iteration variable.
1177 void stat_strength_red(ir_graph *irg, ir_node *strong, ir_node *cmp)
1179 if (! status->stat_options)
1184 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1185 cnt_inc(&graph->cnt_strength_red);
1187 removed_due_opt(strong, graph->opt_hash[STAT_OPT_STRENGTH_RED]);
1193 * Start the dead node elimination.
1195 void stat_dead_node_elim_start(ir_graph *irg)
1197 if (! status->stat_options)
1200 ++status->in_dead_node_elim;
1204 * Stops the dead node elimination.
1206 void stat_dead_node_elim_stop(ir_graph *irg)
1208 if (! status->stat_options)
1211 --status->in_dead_node_elim;
1215 * A multiply was replaced by a series of Shifts/Adds/Subs
1217 void stat_arch_dep_replace_mul_with_shifts(ir_node *mul)
1219 if (! status->stat_options)
1224 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1225 removed_due_opt(mul, graph->opt_hash[STAT_OPT_ARCH_DEP]);
1231 * A division was replaced by a series of Shifts/Muls
1233 void stat_arch_dep_replace_div_by_const(ir_node *div)
1235 if (! status->stat_options)
1240 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1241 removed_due_opt(div, graph->opt_hash[STAT_OPT_ARCH_DEP]);
1247 * A modulo was replaced by a series of Shifts/Muls
1249 void stat_arch_dep_replace_mod_by_const(ir_node *mod)
1251 if (! status->stat_options)
1256 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1257 removed_due_opt(mod, graph->opt_hash[STAT_OPT_ARCH_DEP]);
1263 * A DivMod was replaced by a series of Shifts/Muls
1265 void stat_arch_dep_replace_DivMod_by_const(ir_node *divmod)
1267 if (! status->stat_options)
1272 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1273 removed_due_opt(divmod, graph->opt_hash[STAT_OPT_ARCH_DEP]);
1278 /* Finish the statistics */
1279 void stat_finish(const char *name)
1281 if (! status->stat_options)
1286 graph_entry_t *entry;
1287 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1289 stat_dump_init(name);
1291 /* calculate the graph statistics */
1292 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
1294 if (entry->irg == NULL) {
1295 /* special entry for the global count */
1299 if (! entry->is_deleted) {
1300 /* the graph is still alive, count the nodes on it */
1301 update_graph_stat(global, entry);
1305 /* some calculations are dependant, we pushed them on the wait_q */
1306 while (! pdeq_empty(status->wait_q)) {
1307 entry = pdeq_getr(status->wait_q);
1309 update_graph_stat_2(global, entry);
1313 /* dump per graph */
1314 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
1316 if (entry->irg == NULL) {
1317 /* special entry for the global count */
1321 if (! entry->is_deleted || status->stat_options & FIRMSTAT_COUNT_DELETED)
1322 stat_dump_graph(entry);
1324 if (! entry->is_deleted) {
1325 /* clear the counter that are not accumulated */
1326 graph_clear_entry(entry, 0);
1331 stat_dump_graph(global);
1334 stat_finish_pattern_history();
1336 /* clear the global counter here */
1338 node_entry_t *entry;
1340 for (entry = pset_first(global->opcode_hash); entry; entry = pset_next(global->opcode_hash)) {
1341 opcode_clear_entry(entry);
1343 /* clear all global counter */
1344 graph_clear_entry(global, 1);
1348 // status->stat_options = 0;
1355 /* need this for prototypes */
1356 #define FIRM_STATISTICS
1357 #include "firmstat.h"
1359 void init_stat(unsigned enable_options) {}
1361 void stat_finish(const char *name) {}
1363 void stat_new_ir_op(const ir_op *op) {}
1365 void stat_free_ir_op(const ir_op *op) {}
1367 void stat_new_node(ir_node *node) {}
1369 void stat_turn_into_id(ir_node *node) {}
1371 void stat_new_graph(ir_graph *irg, entity *ent) {}
1373 void stat_free_graph(ir_graph *irg) {}
1375 void stat_irg_walk(ir_graph *irg, void *pre, void *post) {}
1377 void stat_irg_block_walk(ir_graph *irg, const ir_node *node, void *pre, void *post) {}
1379 void stat_merge_nodes(
1380 ir_node **new_node_array, int new_num_entries,
1381 ir_node **old_node_array, int old_num_entries,
1382 stat_opt_kind opt) {}
1384 void stat_reassociate(int start) {}
1386 void stat_lower(ir_node *node) {}
1388 void stat_inline(ir_node *call, ir_graph *irg) {}
1390 void stat_tail_rec(ir_graph *irg) {}
1392 void stat_strength_red(ir_graph *irg, ir_node *strong, ir_node *cmp) {}
1394 void stat_dead_node_elim_start(ir_graph *irg) {}
1396 void stat_dead_node_elim_stop(ir_graph *irg) {}
1398 void stat_arch_dep_replace_mul_with_shifts(ir_node *mul) {}
1400 void stat_arch_dep_replace_div_by_const(ir_node *div) {}
1402 void stat_arch_dep_replace_mod_by_const(ir_node *mod) {}
1404 void stat_arch_dep_replace_DivMod_by_const(ir_node *divmod) {}