3 * File name: ir/ir/firmstat.c
4 * Purpose: Statistics for Firm.
8 * Copyright: (c) 2004 Universität Karlsruhe
9 * Licence: This file protected by GPL - GNU GENERAL PUBLIC LICENSE.
16 #ifdef FIRM_STATISTICS
30 #include "firmstat_t.h"
38 * need this to be static:
39 * Special pseudo Opcodes that we need to count some interesting cases
43 * The Phi0, a node that is created during SSA construction
45 static ir_op _op_Phi0;
47 /** The PhiM, just to count memory Phi's. */
48 static ir_op _op_PhiM;
50 /** The Mul by Const node. */
51 static ir_op _op_MulC;
53 /** The Div by Const node. */
54 static ir_op _op_DivC;
56 /** The Div by Const node. */
57 static ir_op _op_ModC;
59 /** The Div by Const node. */
60 static ir_op _op_DivModC;
62 /** The memory Proj node. */
63 static ir_op _op_ProjM;
66 static ir_op _op_SelSel;
68 /** A Sel of a Sel of a Sel */
69 static ir_op _op_SelSelSel;
71 /* ---------------------------------------------------------------------------------- */
73 /** Marks the begin of a statistic (hook) function. */
74 #define STAT_ENTER ++status->recursive
76 /** Marks the end of a statistic (hook) functions. */
77 #define STAT_LEAVE --status->recursive
79 /** Allows to enter a statistic function only when we are not already in a hook. */
80 #define STAT_ENTER_SINGLE do { if (status->recursive > 0) return; ++status->recursive; } while (0)
85 static const int status_disable = 0;
86 static stat_info_t *status = (stat_info_t *)&status_disable;
89 * compare two elements of the opcode hash
91 static int opcode_cmp(const void *elt, const void *key)
93 const node_entry_t *e1 = elt;
94 const node_entry_t *e2 = key;
96 return e1->op->code - e2->op->code;
100 * compare two elements of the graph hash
102 static int graph_cmp(const void *elt, const void *key)
104 const graph_entry_t *e1 = elt;
105 const graph_entry_t *e2 = key;
107 return e1->irg != e2->irg;
111 * compare two elements of the optimization hash
113 static int opt_cmp(const void *elt, const void *key)
115 const opt_entry_t *e1 = elt;
116 const opt_entry_t *e2 = key;
118 return e1->op->code != e2->op->code;
122 * compare two elements of the block/extbb hash
124 static int block_cmp(const void *elt, const void *key)
126 const block_entry_t *e1 = elt;
127 const block_entry_t *e2 = key;
129 return e1->block_nr != e2->block_nr;
133 * compare two elements of the ir_op hash
135 static int opcode_cmp_2(const void *elt, const void *key)
137 const ir_op *e1 = elt;
138 const ir_op *e2 = key;
140 return e1->code != e2->code;
144 * compare two elements of the address_mark set
146 static int address_mark_cmp(const void *elt, const void *key, size_t size)
148 const address_mark_entry_t *e1 = elt;
149 const address_mark_entry_t *e2 = key;
151 /* compare only the nodes, the rest is used as data container */
152 return e1->node != e2->node;
156 * clears all counter in a node_entry_t
158 static void opcode_clear_entry(node_entry_t *elem)
160 cnt_clr(&elem->cnt_alive);
161 cnt_clr(&elem->new_node);
162 cnt_clr(&elem->into_Id);
166 * Returns the associates node_entry_t for an ir_op
168 * @param op the IR operation
169 * @param hmap a hash map containing ir_op* -> node_entry_t*
171 static node_entry_t *opcode_get_entry(const ir_op *op, hmap_node_entry_t *hmap)
178 elem = pset_find(hmap, &key, op->code);
182 elem = obstack_alloc(&status->cnts, sizeof(*elem));
183 memset(elem, 0, sizeof(*elem));
186 opcode_clear_entry(elem);
190 return pset_insert(hmap, elem, op->code);
194 * Returns the associates ir_op for an opcode
196 * @param code the IR opcode
197 * @param hmap the hash map containing opcode -> ir_op*
199 static ir_op *opcode_find_entry(opcode code, hmap_ir_op *hmap)
204 return pset_find(hmap, &key, code);
208 * clears all counter in a graph_entry_t
210 static void graph_clear_entry(graph_entry_t *elem, int all)
213 cnt_clr(&elem->cnt_walked);
214 cnt_clr(&elem->cnt_walked_blocks);
215 cnt_clr(&elem->cnt_was_inlined);
216 cnt_clr(&elem->cnt_got_inlined);
217 cnt_clr(&elem->cnt_strength_red);
218 cnt_clr(&elem->cnt_real_func_call);
220 cnt_clr(&elem->cnt_edges);
221 cnt_clr(&elem->cnt_all_calls);
222 cnt_clr(&elem->cnt_call_with_cnst_arg);
223 cnt_clr(&elem->cnt_indirect_calls);
225 if (elem->block_hash) {
226 del_pset(elem->block_hash);
227 elem->block_hash = NULL;
230 if (elem->extbb_hash) {
231 del_pset(elem->extbb_hash);
232 elem->extbb_hash = NULL;
235 obstack_free(&elem->recalc_cnts, NULL);
236 obstack_init(&elem->recalc_cnts);
240 * Returns the associated graph_entry_t for an IR graph.
242 * @param irg the IR graph
243 * @param hmap the hash map containing ir_graph* -> graph_entry_t*
245 static graph_entry_t *graph_get_entry(ir_graph *irg, hmap_graph_entry_t *hmap)
253 elem = pset_find(hmap, &key, HASH_PTR(irg));
257 /* allocate a new one */
258 elem = obstack_alloc(&status->cnts, sizeof(*elem));
259 memset(elem, 0, sizeof(*elem));
260 obstack_init(&elem->recalc_cnts);
263 graph_clear_entry(elem, 1);
265 /* new hash table for opcodes here */
266 elem->opcode_hash = new_pset(opcode_cmp, 5);
267 elem->address_mark = new_set(address_mark_cmp, 5);
270 /* these hash tables are created on demand */
271 elem->block_hash = NULL;
272 elem->extbb_hash = NULL;
274 for (i = 0; i < sizeof(elem->opt_hash)/sizeof(elem->opt_hash[0]); ++i)
275 elem->opt_hash[i] = new_pset(opt_cmp, 4);
277 return pset_insert(hmap, elem, HASH_PTR(irg));
281 * clears all counter in an opt_entry_t
283 static void opt_clear_entry(opt_entry_t *elem)
285 cnt_clr(&elem->count);
289 * Returns the associated opt_entry_t for an IR operation.
291 * @param op the IR operation
292 * @param hmap the hash map containing ir_op* -> opt_entry_t*
294 static opt_entry_t *opt_get_entry(const ir_op *op, hmap_opt_entry_t *hmap)
301 elem = pset_find(hmap, &key, op->code);
305 elem = obstack_alloc(&status->cnts, sizeof(*elem));
306 memset(elem, 0, sizeof(*elem));
308 /* clear new counter */
309 opt_clear_entry(elem);
313 return pset_insert(hmap, elem, op->code);
317 * clears all counter in a block_entry_t
319 static void block_clear_entry(block_entry_t *elem)
321 cnt_clr(&elem->cnt_nodes);
322 cnt_clr(&elem->cnt_edges);
323 cnt_clr(&elem->cnt_in_edges);
324 cnt_clr(&elem->cnt_out_edges);
325 cnt_clr(&elem->cnt_phi_data);
329 * Returns the associated block_entry_t for an block.
331 * @param block_nr an IR block number
332 * @param hmap a hash map containing long -> block_entry_t
334 static block_entry_t *block_get_entry(struct obstack *obst, long block_nr, hmap_block_entry_t *hmap)
339 key.block_nr = block_nr;
341 elem = pset_find(hmap, &key, block_nr);
345 elem = obstack_alloc(obst, sizeof(*elem));
346 memset(elem, 0, sizeof(*elem));
348 /* clear new counter */
349 block_clear_entry(elem);
351 elem->block_nr = block_nr;
353 return pset_insert(hmap, elem, block_nr);
358 * Returns the ir_op for an IR-node,
359 * handles special cases and return pseudo op codes.
361 * @param none an IR node
363 static ir_op *stat_get_irn_op(ir_node *node)
365 ir_op *op = get_irn_op(node);
367 if (op == op_Phi && get_irn_arity(node) == 0) {
368 /* special case, a Phi0 node, count on extra counter */
369 op = status->op_Phi0 ? status->op_Phi0 : op;
371 else if (op == op_Phi && get_irn_mode(node) == mode_M) {
372 /* special case, a Memory Phi node, count on extra counter */
373 op = status->op_PhiM ? status->op_PhiM : op;
375 else if (op == op_Proj && get_irn_mode(node) == mode_M) {
376 /* special case, a Memory Proj node, count on extra counter */
377 op = status->op_ProjM ? status->op_ProjM : op;
379 else if (op == op_Mul &&
380 (get_irn_op(get_Mul_left(node)) == op_Const || get_irn_op(get_Mul_right(node)) == op_Const)) {
381 /* special case, a Multiply by a const, count on extra counter */
382 op = status->op_MulC ? status->op_MulC : op;
384 else if (op == op_Div && get_irn_op(get_Div_right(node)) == op_Const) {
385 /* special case, a division by a const, count on extra counter */
386 op = status->op_DivC ? status->op_DivC : op;
388 else if (op == op_Mod && get_irn_op(get_Mod_right(node)) == op_Const) {
389 /* special case, a module by a const, count on extra counter */
390 op = status->op_ModC ? status->op_ModC : op;
392 else if (op == op_DivMod && get_irn_op(get_DivMod_right(node)) == op_Const) {
393 /* special case, a division/modulo by a const, count on extra counter */
394 op = status->op_DivModC ? status->op_DivModC : op;
396 else if (op == op_Sel && get_irn_op(get_Sel_ptr(node)) == op_Sel) {
397 /* special case, a Sel of a Sel, count on extra counter */
398 op = status->op_SelSel ? status->op_SelSel : op;
400 if (get_irn_op(get_Sel_ptr(get_Sel_ptr(node))) == op_Sel) {
401 /* special case, a Sel of a Sel of a Sel, count on extra counter */
402 op = status->op_SelSelSel ? status->op_SelSelSel : op;
410 * update the block counter
412 static void undate_block_info(ir_node *node, graph_entry_t *graph)
414 ir_op *op = get_irn_op(node);
416 block_entry_t *b_entry;
419 /* check for block */
420 if (op == op_Block) {
421 arity = get_irn_arity(node);
422 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(node), graph->block_hash);
424 /* count all incoming edges */
425 for (i = 0; i < arity; ++i) {
426 ir_node *pred = get_irn_n(node, i);
427 ir_node *other_block = get_nodes_block(pred);
428 block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
430 cnt_inc(&b_entry->cnt_in_edges); /* an edge coming from another block */
431 cnt_inc(&b_entry_other->cnt_out_edges);
436 block = get_nodes_block(node);
437 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(block), graph->block_hash);
439 if (op == op_Phi && mode_is_datab(get_irn_mode(node))) {
440 /* count data Phi per block */
441 cnt_inc(&b_entry->cnt_phi_data);
444 /* we have a new node in our block */
445 cnt_inc(&b_entry->cnt_nodes);
447 /* don't count keep-alive edges */
448 if (get_irn_op(node) == op_End)
451 arity = get_irn_arity(node);
453 for (i = 0; i < arity; ++i) {
454 ir_node *pred = get_irn_n(node, i);
455 ir_node *other_block;
457 other_block = get_nodes_block(pred);
459 if (other_block == block)
460 cnt_inc(&b_entry->cnt_edges); /* a in block edge */
462 block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
464 cnt_inc(&b_entry->cnt_in_edges); /* an edge coming from another block */
465 cnt_inc(&b_entry_other->cnt_out_edges);
471 * update the extended block counter
473 static void undate_extbb_info(ir_node *node, graph_entry_t *graph)
475 ir_op *op = get_irn_op(node);
477 extbb_entry_t *eb_entry;
480 /* check for block */
481 if (op == op_Block) {
482 extbb = get_nodes_extbb(node);
483 arity = get_irn_arity(node);
484 eb_entry = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(extbb), graph->extbb_hash);
486 /* count all incoming edges */
487 for (i = 0; i < arity; ++i) {
488 ir_node *pred = get_irn_n(node, i);
489 ir_extblk *other_extbb = get_nodes_extbb(pred);
491 if (extbb != other_extbb) {
492 extbb_entry_t *eb_entry_other = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(other_extbb), graph->extbb_hash);
494 cnt_inc(&eb_entry->cnt_in_edges); /* an edge coming from another extbb */
495 cnt_inc(&eb_entry_other->cnt_out_edges);
501 extbb = get_nodes_extbb(node);
502 eb_entry = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(extbb), graph->extbb_hash);
504 if (op == op_Phi && mode_is_datab(get_irn_mode(node))) {
505 /* count data Phi per extbb */
506 cnt_inc(&eb_entry->cnt_phi_data);
509 /* we have a new node in our block */
510 cnt_inc(&eb_entry->cnt_nodes);
512 /* don't count keep-alive edges */
513 if (get_irn_op(node) == op_End)
516 arity = get_irn_arity(node);
518 for (i = 0; i < arity; ++i) {
519 ir_node *pred = get_irn_n(node, i);
520 ir_extblk *other_extbb = get_nodes_extbb(pred);
522 if (other_extbb == extbb)
523 cnt_inc(&eb_entry->cnt_edges); /* a in extbb edge */
525 extbb_entry_t *eb_entry_other = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(other_extbb), graph->extbb_hash);
527 cnt_inc(&eb_entry->cnt_in_edges); /* an edge coming from another extbb */
528 cnt_inc(&eb_entry_other->cnt_out_edges);
533 /** calculates how many arguments of the call are const */
534 static int cnt_const_args(ir_node *call)
537 int n = get_Call_n_params(call);
539 for (i = 0; i < n; ++i) {
540 ir_node *param = get_Call_param(call, i);
541 ir_op *op = get_irn_op(param);
543 if (op == op_Const || op == op_SymConst)
550 * update info on calls
552 * @param call The call
553 * @param graph The graph entry containing the call
555 static void stat_update_call(ir_node *call, graph_entry_t *graph)
557 ir_node *block = get_nodes_block(call);
558 ir_node *ptr = get_Call_ptr(call);
560 ir_graph *callee = NULL;
564 * If the block is bad, the whole subgraph will collapse later
565 * so do not count this call.
566 * This happens in dead code.
571 cnt_inc(&graph->cnt_all_calls);
573 /* found a call, this function is not a leaf */
576 if (get_irn_op(ptr) == op_SymConst) {
577 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
578 /* ok, we seems to know the entity */
579 ent = get_SymConst_entity(ptr);
580 callee = get_entity_irg(ent);
582 /* it is recursive, if it calls at least once */
583 if (callee == graph->irg)
584 graph->is_recursive = 1;
588 /* indirect call, be could not predict */
589 cnt_inc(&graph->cnt_indirect_calls);
591 /* NOT a leaf call */
592 graph->is_leaf_call = LCS_NON_LEAF_CALL;
595 /* check, if it's a chain-call: Then, the call-block
596 * must dominate the end block. */
598 ir_node *curr = get_irg_end_block(graph->irg);
599 int depth = get_Block_dom_depth(block);
601 for (; curr != block && get_Block_dom_depth(curr) > depth;) {
602 curr = get_Block_idom(curr);
604 if (! curr || is_no_Block(curr))
609 graph->is_chain_call = 0;
612 /* check, if the callee is a leaf */
614 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
616 if (called->is_analyzed) {
617 if (! called->is_leaf)
618 graph->is_leaf_call = LCS_NON_LEAF_CALL;
622 /* check, if arguments of the call are const */
623 num_const_args = cnt_const_args(call);
625 if (num_const_args > 0)
626 cnt_inc(&graph->cnt_call_with_cnst_arg);
630 * update info on calls for graphs on the wait queue
632 static void stat_update_call_2(ir_node *call, graph_entry_t *graph)
634 ir_node *block = get_nodes_block(call);
635 ir_node *ptr = get_Call_ptr(call);
637 ir_graph *callee = NULL;
640 * If the block is bad, the whole subgraph will collapse later
641 * so do not count this call.
642 * This happens in dead code.
647 if (get_irn_op(ptr) == op_SymConst) {
648 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
649 /* ok, we seems to know the entity */
650 ent = get_SymConst_entity(ptr);
651 callee = get_entity_irg(ent);
655 /* check, if the callee is a leaf */
657 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
659 assert(called->is_analyzed);
661 if (! called->is_leaf)
662 graph->is_leaf_call = LCS_NON_LEAF_CALL;
665 graph->is_leaf_call = LCS_NON_LEAF_CALL;
669 * walker for reachable nodes count
671 static void update_node_stat(ir_node *node, void *env)
673 graph_entry_t *graph = env;
676 ir_op *op = stat_get_irn_op(node);
677 int arity = get_irn_arity(node);
679 entry = opcode_get_entry(op, graph->opcode_hash);
681 cnt_inc(&entry->cnt_alive);
682 cnt_add_i(&graph->cnt_edges, arity);
684 /* count block edges */
685 undate_block_info(node, graph);
687 /* count extended block edges */
688 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
689 undate_extbb_info(node, graph);
692 /* handle statistics for special node types */
694 if (op == op_Const) {
695 if (status->stat_options & FIRMSTAT_COUNT_CONSTS) {
696 /* check properties of constants */
697 stat_update_const(status, node, graph);
700 else if (op == op_Call) {
701 /* check for properties that depends on calls like recursion/leaf/indirect call */
702 stat_update_call(node, graph);
707 * walker for reachable nodes count for graphs on the wait_q
709 static void update_node_stat_2(ir_node *node, void *env)
711 graph_entry_t *graph = env;
713 /* check for properties that depends on calls like recursion/leaf/indirect call */
714 if (get_irn_op(node) == op_Call)
715 stat_update_call_2(node, graph);
719 * get the current address mark
721 static unsigned get_adr_mark(graph_entry_t *graph, ir_node *node)
723 address_mark_entry_t *value = set_find(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
725 return value ? value->mark : 0;
729 * set the current address mark
731 static void set_adr_mark(graph_entry_t *graph, ir_node *node, unsigned val)
733 address_mark_entry_t *value = set_insert(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
739 * a vcg attribute hook: Color a node with a different color if
740 * it's identified as a part of an address expression or at least referenced
741 * by an address expression.
743 static int stat_adr_mark_hook(FILE *F, ir_node *node, ir_node *local)
745 ir_node *n = local ? local : node;
746 ir_graph *irg = get_irn_irg(n);
747 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
748 unsigned mark = get_adr_mark(graph, n);
750 if (mark & MARK_ADDRESS_CALC)
751 fprintf(F, "color: purple");
752 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
753 fprintf(F, "color: pink");
754 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
755 fprintf(F, "color: lightblue");
759 /* I know the color! */
764 * walker that marks every node that is an address calculation
766 * predecessor nodes must be visited first. We ensure this by
767 * calling in in the post of an outs walk. This should work even in cycles,
768 * while the pre in a normal walk will not.
770 static void mark_address_calc(ir_node *node, void *env)
772 graph_entry_t *graph = env;
773 ir_mode *mode = get_irn_mode(node);
775 unsigned mark_preds = MARK_REF_NON_ADR;
777 if (! mode_is_numP(mode))
780 if (mode_is_reference(mode)) {
781 /* a reference is calculated here, we are sure */
782 set_adr_mark(graph, node, MARK_ADDRESS_CALC);
784 mark_preds = MARK_REF_ADR;
787 unsigned mark = get_adr_mark(graph, node);
789 if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR) {
791 * this node has not an reference mode, but is only
792 * referenced by address calculations
794 mark_preds = MARK_REF_ADR;
798 /* mark all predecessors */
799 for (i = 0, n = get_irn_arity(node); i < n; ++i) {
800 ir_node *pred = get_irn_n(node, i);
802 set_adr_mark(graph, pred, get_adr_mark(graph, pred) | mark_preds);
807 * Called for every graph when the graph is either deleted or stat_dump_snapshot()
808 * is called, must recalculate all statistic info.
810 * @param global The global entry
811 * @param graph The current entry
813 static void update_graph_stat(graph_entry_t *global, graph_entry_t *graph)
817 /* clear first the alive counter in the graph */
818 for (entry = pset_first(graph->opcode_hash); entry; entry = pset_next(graph->opcode_hash)) {
819 cnt_clr(&entry->cnt_alive);
822 /* set pessimistic values */
824 graph->is_leaf_call = LCS_UNKNOWN;
825 graph->is_recursive = 0;
826 graph->is_chain_call = 1;
828 /* create new block counter */
829 graph->block_hash = new_pset(block_cmp, 5);
831 /* we need dominator info */
832 if (graph->irg != get_const_code_irg())
833 if (get_irg_dom_state(graph->irg) != dom_consistent)
834 compute_doms(graph->irg);
836 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
837 /* we need extended basic blocks */
838 compute_extbb(graph->irg);
840 /* create new extbb counter */
841 graph->extbb_hash = new_pset(block_cmp, 5);
844 /* count the nodes in the graph */
845 irg_walk_graph(graph->irg, update_node_stat, NULL, graph);
848 /* Uncomment this code if chain-call means call exact one */
849 entry = opcode_get_entry(op_Call, graph->opcode_hash);
851 /* check if we have more than 1 call */
852 if (cnt_gt(entry->cnt_alive, 1))
853 graph->is_chain_call = 0;
856 /* recursive functions are never chain calls, leafs don't have calls */
857 if (graph->is_recursive || graph->is_leaf)
858 graph->is_chain_call = 0;
860 /* assume we walk every graph only ONCE, we could sum here the global count */
861 for (entry = pset_first(graph->opcode_hash); entry; entry = pset_next(graph->opcode_hash)) {
862 node_entry_t *g_entry = opcode_get_entry(entry->op, global->opcode_hash);
864 /* update the node counter */
865 cnt_add(&g_entry->cnt_alive, &entry->cnt_alive);
868 /* update the edge counter */
869 cnt_add(&global->cnt_edges, &graph->cnt_edges);
871 /* count the number of address calculation */
872 if (graph->irg != get_const_code_irg()) {
873 ir_graph *rem = current_ir_graph;
875 if (get_irg_outs_state(graph->irg) != outs_consistent)
876 compute_irg_outs(graph->irg);
878 /* Must be done an the outs graph */
879 current_ir_graph = graph->irg;
880 irg_out_walk(get_irg_start(graph->irg), NULL, mark_address_calc, graph);
881 current_ir_graph = rem;
884 set_dump_node_vcgattr_hook(stat_adr_mark_hook);
885 dump_ir_block_graph(graph->irg, "-adr");
886 set_dump_node_vcgattr_hook(NULL);
890 /* count the DAG's */
891 if (status->stat_options & FIRMSTAT_COUNT_DAG)
892 count_dags_in_graph(global, graph);
894 /* calculate the patterns of this graph */
895 stat_calc_pattern_history(graph->irg);
897 /* leaf function did not call others */
899 graph->is_leaf_call = LCS_NON_LEAF_CALL;
900 else if (graph->is_leaf_call == LCS_UNKNOWN) {
901 /* we still don't know if this graph calls leaf-functions, so enqueue */
902 pdeq_putl(status->wait_q, graph);
905 /* we have analyzed this graph */
906 graph->is_analyzed = 1;
910 * Called for every graph that was on the wait_q in stat_dump_snapshot()
911 * must finish all statistic info calculations.
913 * @param global The global entry
914 * @param graph The current entry
916 static void update_graph_stat_2(graph_entry_t *global, graph_entry_t *graph)
918 if (graph->is_deleted) {
919 /* deleted, ignore */
924 /* count the nodes in the graph */
925 irg_walk_graph(graph->irg, update_node_stat_2, NULL, graph);
927 if (graph->is_leaf_call == LCS_UNKNOWN)
928 graph->is_leaf_call = LCS_LEAF_CALL;
935 static void stat_register_dumper(const dumper_t *dumper)
937 dumper_t *p = xmalloc(sizeof(*p));
942 p->next = status->dumper;
947 /* FIXME: memory leak */
953 static void stat_dump_graph(graph_entry_t *entry)
957 for (dumper = status->dumper; dumper; dumper = dumper->next) {
958 if (dumper->dump_graph)
959 dumper->dump_graph(dumper, entry);
964 * dumps a constant table
966 static void stat_dump_consts(const constant_info_t *tbl)
970 for (dumper = status->dumper; dumper; dumper = dumper->next) {
971 if (dumper->dump_const_tbl)
972 dumper->dump_const_tbl(dumper, tbl);
977 * initialize the dumper
979 static void stat_dump_init(const char *name)
983 for (dumper = status->dumper; dumper; dumper = dumper->next) {
985 dumper->init(dumper, name);
992 static void stat_dump_finish(void)
996 for (dumper = status->dumper; dumper; dumper = dumper->next) {
998 dumper->finish(dumper);
1002 /* ---------------------------------------------------------------------- */
1005 * helper: get an ir_op from an opcode
1007 ir_op *stat_get_op_from_opcode(opcode code)
1009 return opcode_find_entry(code, status->ir_op_hash);
1013 * A new IR op is registered.
1015 * @param ctx the hook context
1016 * @param op the new IR opcode that was created.
1018 static void stat_new_ir_op(void *ctx, ir_op *op)
1020 if (! status->stat_options)
1025 graph_entry_t *graph = graph_get_entry(NULL, status->irg_hash);
1027 /* execute for side effect :-) */
1028 opcode_get_entry(op, graph->opcode_hash);
1030 pset_insert(status->ir_op_hash, op, op->code);
1036 * An IR op is freed.
1038 * @param ctx the hook context
1039 * @param op the IR opcode that is freed
1041 static void stat_free_ir_op(void *ctx, ir_op *op)
1043 if (! status->stat_options)
1053 * A new node is created.
1055 * @param ctx the hook context
1056 * @param irg the IR graph on which the node is created
1057 * @param node the new IR node that was created
1059 static void stat_new_node(void *ctx, ir_graph *irg, ir_node *node)
1061 if (! status->stat_options)
1064 /* do NOT count during dead node elimination */
1065 if (status->in_dead_node_elim > 0)
1070 node_entry_t *entry;
1071 graph_entry_t *graph;
1072 ir_op *op = stat_get_irn_op(node);
1074 /* increase global value */
1075 graph = graph_get_entry(NULL, status->irg_hash);
1076 entry = opcode_get_entry(op, graph->opcode_hash);
1077 cnt_inc(&entry->new_node);
1079 /* increase local value */
1080 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1081 entry = opcode_get_entry(op, graph->opcode_hash);
1082 cnt_inc(&entry->new_node);
1088 * A node is changed into a Id node
1090 * @param ctx the hook context
1091 * @param node the IR node that will be turned into an ID
1093 static void stat_turn_into_id(void *ctx, ir_node *node)
1095 if (! status->stat_options)
1100 node_entry_t *entry;
1101 graph_entry_t *graph;
1102 ir_op *op = stat_get_irn_op(node);
1104 /* increase global value */
1105 graph = graph_get_entry(NULL, status->irg_hash);
1106 entry = opcode_get_entry(op, graph->opcode_hash);
1107 cnt_inc(&entry->into_Id);
1109 /* increase local value */
1110 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1111 entry = opcode_get_entry(op, graph->opcode_hash);
1112 cnt_inc(&entry->into_Id);
1118 * A new graph was created
1120 * @param ctx the hook context
1121 * @param irg the new IR graph that was created
1122 * @param ent the entity of this graph
1124 static void stat_new_graph(void *ctx, ir_graph *irg, entity *ent)
1126 if (! status->stat_options)
1131 /* execute for side effect :-) */
1132 graph_entry_t * graph = graph_get_entry(irg, status->irg_hash);
1135 graph->is_deleted = 0;
1137 graph->is_leaf_call = 0;
1138 graph->is_recursive = 0;
1139 graph->is_chain_call = 0;
1140 graph->is_analyzed = 0;
1146 * A graph will be deleted
1148 * @param ctx the hook context
1149 * @param irg the IR graph that will be deleted
1151 * Note that we still hold the information for this graph
1152 * in our hash maps, only a flag is set which prevents this
1153 * information from being changed, it's "frozen" from now.
1155 static void stat_free_graph(void *ctx, ir_graph *irg)
1157 if (! status->stat_options)
1162 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1163 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1165 graph->is_deleted = 1;
1167 if (status->stat_options & FIRMSTAT_COUNT_DELETED) {
1168 /* count the nodes of the graph yet, it will be destroyed later */
1169 update_graph_stat(global, graph);
1176 * A walk over a graph is initiated. Do not count walks from statistic code.
1178 * @param ctx the hook context
1179 * @param irg the IR graph that will be walked
1180 * @param pre the pre walker
1181 * @param post the post walker
1183 static void stat_irg_walk(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1185 if (! status->stat_options)
1190 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1192 cnt_inc(&graph->cnt_walked);
1198 * A walk over a graph in block-wise order is initiated. Do not count walks from statistic code.
1200 * @param ctx the hook context
1201 * @param irg the IR graph that will be walked
1202 * @param pre the pre walker
1203 * @param post the post walker
1205 static void stat_irg_walk_blkwise(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1207 /* for now, do NOT differentiate between blockwise and normal */
1208 stat_irg_walk(ctx, irg, pre, post);
1212 * A walk over the graph's blocks is initiated. Do not count walks from statistic code.
1214 * @param ctx the hook context
1215 * @param irg the IR graph that will be walked
1216 * @param node the IR node
1217 * @param pre the pre walker
1218 * @param post the post walker
1220 static void stat_irg_block_walk(void *ctx, ir_graph *irg, ir_node *node, generic_func *pre, generic_func *post)
1222 if (! status->stat_options)
1227 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1229 cnt_inc(&graph->cnt_walked_blocks);
1235 * called for every node that is removed due to an optimization.
1237 * @param n the IR node that will be removed
1238 * @param hmap the hash map containing ir_op* -> opt_entry_t*
1240 static void removed_due_opt(ir_node *n, hmap_opt_entry_t *hmap)
1242 ir_op *op = stat_get_irn_op(n);
1243 opt_entry_t *entry = opt_get_entry(op, hmap);
1245 /* increase global value */
1246 cnt_inc(&entry->count);
1250 * Some nodes were optimized into some others due to an optimization.
1252 * @param ctx the hook context
1254 static void stat_merge_nodes(
1256 ir_node **new_node_array, int new_num_entries,
1257 ir_node **old_node_array, int old_num_entries,
1260 if (! status->stat_options)
1266 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1268 if (status->reassoc_run)
1269 opt = HOOK_OPT_REASSOC;
1271 for (i = 0; i < old_num_entries; ++i) {
1272 for (j = 0; j < new_num_entries; ++j)
1273 if (old_node_array[i] == new_node_array[j])
1276 /* nodes might be in new and old, these are NOT removed */
1277 if (j >= new_num_entries) {
1280 /* sometimes we did not detect, that it is replaced by a Const */
1281 if (opt == HOOK_OPT_CONFIRM && new_num_entries == 1) {
1282 ir_op *op = get_irn_op(new_node_array[0]);
1284 if (op == op_Const || op == op_SymConst)
1285 xopt = HOOK_OPT_CONFIRM_C;
1288 removed_due_opt(old_node_array[i], graph->opt_hash[xopt]);
1296 * Reassociation is started/stopped.
1298 * @param ctx the hook context
1299 * @param flag if non-zero, reassociation is started else stopped
1301 static void stat_reassociate(void *ctx, int flag)
1303 if (! status->stat_options)
1308 status->reassoc_run = flag;
1314 * A node was lowered into other nodes
1316 * @param ctx the hook context
1317 * @param node the IR node that will be lowered
1319 static void stat_lower(void *ctx, ir_node *node)
1321 if (! status->stat_options)
1326 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1328 removed_due_opt(node, graph->opt_hash[HOOK_LOWERED]);
1334 * A graph was inlined.
1336 * @param ctx the hook context
1337 * @param call the IR call that will re changed into the body of
1338 * the called IR graph
1339 * @param called_irg the IR graph representing the called routine
1341 static void stat_inline(void *ctx, ir_node *call, ir_graph *called_irg)
1343 if (! status->stat_options)
1348 ir_graph *irg = get_irn_irg(call);
1349 graph_entry_t *i_graph = graph_get_entry(called_irg, status->irg_hash);
1350 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1352 cnt_inc(&graph->cnt_got_inlined);
1353 cnt_inc(&i_graph->cnt_was_inlined);
1359 * A graph with tail-recursions was optimized.
1361 * @param ctx the hook context
1363 static void stat_tail_rec(void *ctx, ir_graph *irg, int n_calls)
1365 if (! status->stat_options)
1370 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1372 graph->num_tail_recursion += n_calls;
1378 * Strength reduction was performed on an iteration variable.
1380 * @param ctx the hook context
1382 static void stat_strength_red(void *ctx, ir_graph *irg, ir_node *strong, ir_node *cmp)
1384 if (! status->stat_options)
1389 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1390 cnt_inc(&graph->cnt_strength_red);
1392 removed_due_opt(strong, graph->opt_hash[HOOK_OPT_STRENGTH_RED]);
1398 * Start/Stop the dead node elimination.
1400 * @param ctx the hook context
1402 static void stat_dead_node_elim(void *ctx, ir_graph *irg, int start)
1404 if (! status->stat_options)
1408 ++status->in_dead_node_elim;
1410 --status->in_dead_node_elim;
1414 * if-conversion was tried
1416 static void stat_if_conversion(void *context, ir_graph *irg, ir_node *phi,
1417 int pos, ir_node *mux, if_result_t reason)
1419 if (! status->stat_options)
1424 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1426 cnt_inc(&graph->cnt_if_conv[reason]);
1432 * real function call was optimized
1434 static void stat_func_call(void *context, ir_graph *irg, ir_node *call)
1436 if (! status->stat_options)
1441 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1443 cnt_inc(&graph->cnt_real_func_call);
1449 * A multiply was replaced by a series of Shifts/Adds/Subs
1451 * @param ctx the hook context
1453 static void stat_arch_dep_replace_mul_with_shifts(void *ctx, ir_node *mul)
1455 if (! status->stat_options)
1460 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1461 removed_due_opt(mul, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1467 * A division by const was replaced
1469 * @param ctx the hook context
1470 * @param node the division node that will be optimized
1472 static void stat_arch_dep_replace_division_by_const(void *ctx, ir_node *node)
1474 if (! status->stat_options)
1479 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1480 removed_due_opt(node, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1485 /* Dumps a statistics snapshot */
1486 void stat_dump_snapshot(const char *name, const char *phase)
1492 if (! status->stat_options)
1497 graph_entry_t *entry;
1498 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1501 * The constant counter is only global, so we clear it here.
1502 * Note that it does NOT contain the constants in DELETED
1503 * graphs due to this.
1505 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
1506 stat_const_clear(status);
1508 /* build the name */
1509 p = strrchr(name, '/');
1514 q = strrchr(name, '\\');
1516 /* NULL might be not the smallest pointer */
1517 if (q && (!p || q > p))
1525 if (l > sizeof(fname) - 1)
1526 l = sizeof(fname) - 1;
1528 memcpy(fname, name, l);
1535 strncat(fname, "firmstat-", sizeof(fname));
1536 strncat(fname, phase, sizeof(fname));
1537 strncat(fname, "-", sizeof(fname));
1538 strncat(fname, p, sizeof(fname));
1540 stat_dump_init(fname);
1542 /* calculate the graph statistics */
1543 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
1545 if (entry->irg == NULL) {
1546 /* special entry for the global count */
1550 if (! entry->is_deleted) {
1551 /* the graph is still alive, count the nodes on it */
1552 update_graph_stat(global, entry);
1556 /* some calculations are dependent, we pushed them on the wait_q */
1557 while (! pdeq_empty(status->wait_q)) {
1558 entry = pdeq_getr(status->wait_q);
1560 update_graph_stat_2(global, entry);
1564 /* dump per graph */
1565 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
1567 if (entry->irg == NULL) {
1568 /* special entry for the global count */
1572 if (! entry->is_deleted || status->stat_options & FIRMSTAT_COUNT_DELETED) {
1573 stat_dump_graph(entry);
1576 if (! entry->is_deleted) {
1577 /* clear the counter that are not accumulated */
1578 graph_clear_entry(entry, 0);
1583 stat_dump_graph(global);
1585 /* dump the const info */
1586 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
1587 stat_dump_consts(&status->const_info);
1591 stat_finish_pattern_history();
1593 /* clear the global counter here */
1595 node_entry_t *entry;
1597 for (entry = pset_first(global->opcode_hash); entry; entry = pset_next(global->opcode_hash)) {
1598 opcode_clear_entry(entry);
1600 /* clear all global counter */
1601 graph_clear_entry(global, 1);
1607 /** the hook entries for the Firm statistics module */
1608 static hook_entry_t stat_hooks[hook_last];
1610 /* initialize the statistics module. */
1611 void init_stat(unsigned enable_options)
1613 #define X(a) a, sizeof(a)-1
1614 #define HOOK(h, fkt) \
1615 stat_hooks[h].hook._##h = fkt; register_hook(h, &stat_hooks[h])
1617 if (! (enable_options & FIRMSTAT_ENABLED))
1620 status = xmalloc(sizeof(*status));
1621 memset(status, 0, sizeof(*status));
1623 /* enable statistics */
1624 status->stat_options = enable_options & FIRMSTAT_ENABLED ? enable_options : 0;
1626 /* register all hooks */
1627 HOOK(hook_new_ir_op, stat_new_ir_op);
1628 HOOK(hook_free_ir_op, stat_free_ir_op);
1629 HOOK(hook_new_node, stat_new_node);
1630 HOOK(hook_turn_into_id, stat_turn_into_id);
1631 HOOK(hook_new_graph, stat_new_graph);
1632 HOOK(hook_free_graph, stat_free_graph);
1633 HOOK(hook_irg_walk, stat_irg_walk);
1634 HOOK(hook_irg_walk_blkwise, stat_irg_walk_blkwise);
1635 HOOK(hook_irg_block_walk, stat_irg_block_walk);
1636 HOOK(hook_merge_nodes, stat_merge_nodes);
1637 HOOK(hook_reassociate, stat_reassociate);
1638 HOOK(hook_lower, stat_lower);
1639 HOOK(hook_inline, stat_inline);
1640 HOOK(hook_tail_rec, stat_tail_rec);
1641 HOOK(hook_strength_red, stat_strength_red);
1642 HOOK(hook_dead_node_elim, stat_dead_node_elim);
1643 HOOK(hook_if_conversion, stat_if_conversion);
1644 HOOK(hook_func_call, stat_func_call);
1645 HOOK(hook_arch_dep_replace_mul_with_shifts, stat_arch_dep_replace_mul_with_shifts);
1646 HOOK(hook_arch_dep_replace_division_by_const, stat_arch_dep_replace_division_by_const);
1648 obstack_init(&status->cnts);
1650 /* create the hash-tables */
1651 status->irg_hash = new_pset(graph_cmp, 8);
1652 status->ir_op_hash = new_pset(opcode_cmp_2, 1);
1654 /* create the wait queue */
1655 status->wait_q = new_pdeq();
1657 if (enable_options & FIRMSTAT_COUNT_STRONG_OP) {
1658 /* build the pseudo-ops */
1659 _op_Phi0.code = get_next_ir_opcode();
1660 _op_Phi0.name = new_id_from_chars(X("Phi0"));
1662 _op_PhiM.code = get_next_ir_opcode();
1663 _op_PhiM.name = new_id_from_chars(X("PhiM"));
1665 _op_ProjM.code = get_next_ir_opcode();
1666 _op_ProjM.name = new_id_from_chars(X("ProjM"));
1668 _op_MulC.code = get_next_ir_opcode();
1669 _op_MulC.name = new_id_from_chars(X("MulC"));
1671 _op_DivC.code = get_next_ir_opcode();
1672 _op_DivC.name = new_id_from_chars(X("DivC"));
1674 _op_ModC.code = get_next_ir_opcode();
1675 _op_ModC.name = new_id_from_chars(X("ModC"));
1677 _op_DivModC.code = get_next_ir_opcode();
1678 _op_DivModC.name = new_id_from_chars(X("DivModC"));
1680 status->op_Phi0 = &_op_Phi0;
1681 status->op_PhiM = &_op_PhiM;
1682 status->op_ProjM = &_op_ProjM;
1683 status->op_MulC = &_op_MulC;
1684 status->op_DivC = &_op_DivC;
1685 status->op_ModC = &_op_ModC;
1686 status->op_DivModC = &_op_DivModC;
1689 status->op_Phi0 = NULL;
1690 status->op_PhiM = NULL;
1691 status->op_ProjM = NULL;
1692 status->op_MulC = NULL;
1693 status->op_DivC = NULL;
1694 status->op_ModC = NULL;
1695 status->op_DivModC = NULL;
1698 if (enable_options & FIRMSTAT_COUNT_SELS) {
1699 _op_SelSel.code = get_next_ir_opcode();
1700 _op_SelSel.name = new_id_from_chars(X("Sel(Sel)"));
1702 _op_SelSelSel.code = get_next_ir_opcode();
1703 _op_SelSelSel.name = new_id_from_chars(X("Sel(Sel(Sel))"));
1705 status->op_SelSel = &_op_SelSel;
1706 status->op_SelSelSel = &_op_SelSelSel;
1709 status->op_SelSel = NULL;
1710 status->op_SelSelSel = NULL;
1713 /* register the dumper */
1714 stat_register_dumper(&simple_dumper);
1716 if (enable_options & FIRMSTAT_CSV_OUTPUT)
1717 stat_register_dumper(&csv_dumper);
1719 /* initialize the pattern hash */
1720 stat_init_pattern_history(enable_options & FIRMSTAT_PATTERN_ENABLED);
1722 /* initialize the Const options */
1723 if (enable_options & FIRMSTAT_COUNT_CONSTS)
1724 stat_init_const_cnt(status);
1730 /* terminates the statistics module, frees all memory */
1731 void stat_term(void) {
1732 if (status != (stat_info_t *)&status_disable) {
1734 status = (stat_info_t *)&status_disable;
1740 /* initialize the statistics module. */
1741 void init_stat(unsigned enable_options) {}
1743 /* Dumps a statistics snapshot */
1744 void stat_dump_snapshot(const char *name, const char *phase) {}
1746 /* terminates the statistics module, frees all memory */
1747 void stat_term(void);
1749 #endif /* FIRM_STATISTICS */