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 be_block hash
135 static int be_block_cmp(const void *elt, const void *key)
137 const be_block_entry_t *e1 = elt;
138 const be_block_entry_t *e2 = key;
140 return e1->block_nr != e2->block_nr;
144 * compare two elements of reg pressure hash
146 static int reg_pressure_cmp(const void *elt, const void *key)
148 const reg_pressure_entry_t *e1 = elt;
149 const reg_pressure_entry_t *e2 = key;
151 return e1->class_name != e2->class_name;
155 * compare two elements of the perm_stat hash
157 static int perm_stat_cmp(const void *elt, const void *key)
159 const perm_stat_entry_t *e1 = elt;
160 const perm_stat_entry_t *e2 = key;
162 return e1->perm != e2->perm;
166 * compare two elements of the perm_class hash
168 static int perm_class_cmp(const void *elt, const void *key)
170 const perm_class_entry_t *e1 = elt;
171 const perm_class_entry_t *e2 = key;
173 return e1->class_name != e2->class_name;
177 * compare two elements of the ir_op hash
179 static int opcode_cmp_2(const void *elt, const void *key)
181 const ir_op *e1 = elt;
182 const ir_op *e2 = key;
184 return e1->code != e2->code;
188 * compare two elements of the address_mark set
190 static int address_mark_cmp(const void *elt, const void *key, size_t size)
192 const address_mark_entry_t *e1 = elt;
193 const address_mark_entry_t *e2 = key;
195 /* compare only the nodes, the rest is used as data container */
196 return e1->node != e2->node;
200 * clears all counter in a node_entry_t
202 static void opcode_clear_entry(node_entry_t *elem)
204 cnt_clr(&elem->cnt_alive);
205 cnt_clr(&elem->new_node);
206 cnt_clr(&elem->into_Id);
210 * Returns the associates node_entry_t for an ir_op
212 * @param op the IR operation
213 * @param hmap a hash map containing ir_op* -> node_entry_t*
215 static node_entry_t *opcode_get_entry(const ir_op *op, hmap_node_entry_t *hmap)
222 elem = pset_find(hmap, &key, op->code);
226 elem = obstack_alloc(&status->cnts, sizeof(*elem));
227 memset(elem, 0, sizeof(*elem));
230 opcode_clear_entry(elem);
234 return pset_insert(hmap, elem, op->code);
238 * Returns the associates ir_op for an opcode
240 * @param code the IR opcode
241 * @param hmap the hash map containing opcode -> ir_op*
243 static ir_op *opcode_find_entry(opcode code, hmap_ir_op *hmap)
248 return pset_find(hmap, &key, code);
252 * clears all counter in a graph_entry_t
254 static void graph_clear_entry(graph_entry_t *elem, int all)
257 cnt_clr(&elem->cnt_walked);
258 cnt_clr(&elem->cnt_walked_blocks);
259 cnt_clr(&elem->cnt_was_inlined);
260 cnt_clr(&elem->cnt_got_inlined);
261 cnt_clr(&elem->cnt_strength_red);
262 cnt_clr(&elem->cnt_real_func_call);
264 cnt_clr(&elem->cnt_edges);
265 cnt_clr(&elem->cnt_all_calls);
266 cnt_clr(&elem->cnt_call_with_cnst_arg);
267 cnt_clr(&elem->cnt_indirect_calls);
269 if (elem->block_hash) {
270 del_pset(elem->block_hash);
271 elem->block_hash = NULL;
274 if (elem->extbb_hash) {
275 del_pset(elem->extbb_hash);
276 elem->extbb_hash = NULL;
279 obstack_free(&elem->recalc_cnts, NULL);
280 obstack_init(&elem->recalc_cnts);
284 * Returns the associated graph_entry_t for an IR graph.
286 * @param irg the IR graph
287 * @param hmap the hash map containing ir_graph* -> graph_entry_t*
289 static graph_entry_t *graph_get_entry(ir_graph *irg, hmap_graph_entry_t *hmap)
297 elem = pset_find(hmap, &key, HASH_PTR(irg));
300 /* create hash map backend block information */
301 if (! elem->be_block_hash)
302 elem->be_block_hash = new_pset(be_block_cmp, 5);
307 /* allocate a new one */
308 elem = obstack_alloc(&status->cnts, sizeof(*elem));
309 memset(elem, 0, sizeof(*elem));
310 obstack_init(&elem->recalc_cnts);
313 graph_clear_entry(elem, 1);
315 /* new hash table for opcodes here */
316 elem->opcode_hash = new_pset(opcode_cmp, 5);
317 elem->address_mark = new_set(address_mark_cmp, 5);
320 /* these hash tables are created on demand */
321 elem->block_hash = NULL;
322 elem->extbb_hash = NULL;
324 for (i = 0; i < sizeof(elem->opt_hash)/sizeof(elem->opt_hash[0]); ++i)
325 elem->opt_hash[i] = new_pset(opt_cmp, 4);
327 return pset_insert(hmap, elem, HASH_PTR(irg));
331 * clears all counter in an opt_entry_t
333 static void opt_clear_entry(opt_entry_t *elem)
335 cnt_clr(&elem->count);
339 * Returns the associated opt_entry_t for an IR operation.
341 * @param op the IR operation
342 * @param hmap the hash map containing ir_op* -> opt_entry_t*
344 static opt_entry_t *opt_get_entry(const ir_op *op, hmap_opt_entry_t *hmap)
351 elem = pset_find(hmap, &key, op->code);
355 elem = obstack_alloc(&status->cnts, sizeof(*elem));
356 memset(elem, 0, sizeof(*elem));
358 /* clear new counter */
359 opt_clear_entry(elem);
363 return pset_insert(hmap, elem, op->code);
367 * clears all counter in a block_entry_t
369 static void block_clear_entry(block_entry_t *elem)
371 cnt_clr(&elem->cnt_nodes);
372 cnt_clr(&elem->cnt_edges);
373 cnt_clr(&elem->cnt_in_edges);
374 cnt_clr(&elem->cnt_out_edges);
375 cnt_clr(&elem->cnt_phi_data);
379 * Returns the associated block_entry_t for an block.
381 * @param block_nr an IR block number
382 * @param hmap a hash map containing long -> block_entry_t
384 static block_entry_t *block_get_entry(struct obstack *obst, long block_nr, hmap_block_entry_t *hmap)
389 key.block_nr = block_nr;
391 elem = pset_find(hmap, &key, block_nr);
395 elem = obstack_alloc(obst, sizeof(*elem));
396 memset(elem, 0, sizeof(*elem));
398 /* clear new counter */
399 block_clear_entry(elem);
401 elem->block_nr = block_nr;
403 return pset_insert(hmap, elem, block_nr);
407 * clears all sets in be_block_entry_t
409 static void be_block_clear_entry(be_block_entry_t *elem)
411 if (elem->reg_pressure)
412 del_pset(elem->reg_pressure);
414 if (elem->sched_ready)
415 stat_delete_distrib_tbl(elem->sched_ready);
417 if (elem->perm_class_stat)
418 del_pset(elem->perm_class_stat);
420 elem->reg_pressure = new_pset(reg_pressure_cmp, 5);
421 elem->sched_ready = stat_new_int_distrib_tbl();
422 elem->perm_class_stat = new_pset(perm_class_cmp, 5);
426 * Returns the associated be_block_entry_t for an block.
428 * @param block_nr an IR block number
429 * @param hmap a hash map containing long -> be_block_entry_t
431 static be_block_entry_t *be_block_get_entry(struct obstack *obst, long block_nr, hmap_be_block_entry_t *hmap)
433 be_block_entry_t key;
434 be_block_entry_t *elem;
436 key.block_nr = block_nr;
438 elem = pset_find(hmap, &key, block_nr);
442 elem = obstack_alloc(obst, sizeof(*elem));
443 memset(elem, 0, sizeof(*elem));
445 /* clear new counter */
446 be_block_clear_entry(elem);
448 elem->block_nr = block_nr;
450 return pset_insert(hmap, elem, block_nr);
454 * clears all sets in perm_class_entry_t
456 static void perm_class_clear_entry(perm_class_entry_t *elem) {
458 del_pset(elem->perm_stat);
460 elem->perm_stat = new_pset(perm_stat_cmp, 5);
464 * Returns the associated perm_class entry for a register class.
466 * @param class_name the register class name
467 * @param hmap a hash map containing class_name -> perm_class_entry_t
469 static perm_class_entry_t *perm_class_get_entry(struct obstack *obst, const char *class_name,
470 hmap_perm_class_entry_t *hmap)
472 perm_class_entry_t key;
473 perm_class_entry_t *elem;
475 key.class_name = class_name;
477 elem = pset_find(hmap, &key, HASH_PTR(class_name));
481 elem = obstack_alloc(obst, sizeof(*elem));
482 memset(elem, 0, sizeof(*elem));
484 /* clear new counter */
485 perm_class_clear_entry(elem);
487 elem->class_name = class_name;
489 return pset_insert(hmap, elem, HASH_PTR(class_name));
493 * clears all sets in perm_stat_entry_t
495 static void perm_stat_clear_entry(perm_stat_entry_t *elem) {
497 stat_delete_distrib_tbl(elem->chains);
500 stat_delete_distrib_tbl(elem->cycles);
502 elem->chains = stat_new_int_distrib_tbl();
503 elem->cycles = stat_new_int_distrib_tbl();
507 * Returns the associated perm_stat entry for a perm.
509 * @param perm the perm node
510 * @param hmap a hash map containing perm -> perm_stat_entry_t
512 static perm_stat_entry_t *perm_stat_get_entry(struct obstack *obst, ir_node *perm, hmap_perm_stat_entry_t *hmap)
514 perm_stat_entry_t key;
515 perm_stat_entry_t *elem;
519 elem = pset_find(hmap, &key, HASH_PTR(perm));
523 elem = obstack_alloc(obst, sizeof(*elem));
524 memset(elem, 0, sizeof(*elem));
526 /* clear new counter */
527 perm_stat_clear_entry(elem);
531 return pset_insert(hmap, elem, HASH_PTR(perm));
535 * Returns the ir_op for an IR-node,
536 * handles special cases and return pseudo op codes.
538 * @param none an IR node
540 static ir_op *stat_get_irn_op(ir_node *node)
542 ir_op *op = get_irn_op(node);
544 if (op == op_Phi && get_irn_arity(node) == 0) {
545 /* special case, a Phi0 node, count on extra counter */
546 op = status->op_Phi0 ? status->op_Phi0 : op;
548 else if (op == op_Phi && get_irn_mode(node) == mode_M) {
549 /* special case, a Memory Phi node, count on extra counter */
550 op = status->op_PhiM ? status->op_PhiM : op;
552 else if (op == op_Proj && get_irn_mode(node) == mode_M) {
553 /* special case, a Memory Proj node, count on extra counter */
554 op = status->op_ProjM ? status->op_ProjM : op;
556 else if (op == op_Mul &&
557 (get_irn_op(get_Mul_left(node)) == op_Const || get_irn_op(get_Mul_right(node)) == op_Const)) {
558 /* special case, a Multiply by a const, count on extra counter */
559 op = status->op_MulC ? status->op_MulC : op;
561 else if (op == op_Div && get_irn_op(get_Div_right(node)) == op_Const) {
562 /* special case, a division by a const, count on extra counter */
563 op = status->op_DivC ? status->op_DivC : op;
565 else if (op == op_Mod && get_irn_op(get_Mod_right(node)) == op_Const) {
566 /* special case, a module by a const, count on extra counter */
567 op = status->op_ModC ? status->op_ModC : op;
569 else if (op == op_DivMod && get_irn_op(get_DivMod_right(node)) == op_Const) {
570 /* special case, a division/modulo by a const, count on extra counter */
571 op = status->op_DivModC ? status->op_DivModC : op;
573 else if (op == op_Sel && get_irn_op(get_Sel_ptr(node)) == op_Sel) {
574 /* special case, a Sel of a Sel, count on extra counter */
575 op = status->op_SelSel ? status->op_SelSel : op;
577 if (get_irn_op(get_Sel_ptr(get_Sel_ptr(node))) == op_Sel) {
578 /* special case, a Sel of a Sel of a Sel, count on extra counter */
579 op = status->op_SelSelSel ? status->op_SelSelSel : op;
587 * update the block counter
589 static void undate_block_info(ir_node *node, graph_entry_t *graph)
591 ir_op *op = get_irn_op(node);
593 block_entry_t *b_entry;
596 /* check for block */
597 if (op == op_Block) {
598 arity = get_irn_arity(node);
599 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(node), graph->block_hash);
601 /* count all incoming edges */
602 for (i = 0; i < arity; ++i) {
603 ir_node *pred = get_irn_n(node, i);
604 ir_node *other_block = get_nodes_block(pred);
605 block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
607 cnt_inc(&b_entry->cnt_in_edges); /* an edge coming from another block */
608 cnt_inc(&b_entry_other->cnt_out_edges);
613 block = get_nodes_block(node);
614 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(block), graph->block_hash);
616 if (op == op_Phi && mode_is_datab(get_irn_mode(node))) {
617 /* count data Phi per block */
618 cnt_inc(&b_entry->cnt_phi_data);
621 /* we have a new node in our block */
622 cnt_inc(&b_entry->cnt_nodes);
624 /* don't count keep-alive edges */
625 if (get_irn_op(node) == op_End)
628 arity = get_irn_arity(node);
630 for (i = 0; i < arity; ++i) {
631 ir_node *pred = get_irn_n(node, i);
632 ir_node *other_block;
634 other_block = get_nodes_block(pred);
636 if (other_block == block)
637 cnt_inc(&b_entry->cnt_edges); /* a in block edge */
639 block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
641 cnt_inc(&b_entry->cnt_in_edges); /* an edge coming from another block */
642 cnt_inc(&b_entry_other->cnt_out_edges);
648 * update the extended block counter
650 static void undate_extbb_info(ir_node *node, graph_entry_t *graph)
652 ir_op *op = get_irn_op(node);
654 extbb_entry_t *eb_entry;
657 /* check for block */
658 if (op == op_Block) {
659 extbb = get_nodes_extbb(node);
660 arity = get_irn_arity(node);
661 eb_entry = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(extbb), graph->extbb_hash);
663 /* count all incoming edges */
664 for (i = 0; i < arity; ++i) {
665 ir_node *pred = get_irn_n(node, i);
666 ir_extblk *other_extbb = get_nodes_extbb(pred);
668 if (extbb != other_extbb) {
669 extbb_entry_t *eb_entry_other = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(other_extbb), graph->extbb_hash);
671 cnt_inc(&eb_entry->cnt_in_edges); /* an edge coming from another extbb */
672 cnt_inc(&eb_entry_other->cnt_out_edges);
678 extbb = get_nodes_extbb(node);
679 eb_entry = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(extbb), graph->extbb_hash);
681 if (op == op_Phi && mode_is_datab(get_irn_mode(node))) {
682 /* count data Phi per extbb */
683 cnt_inc(&eb_entry->cnt_phi_data);
686 /* we have a new node in our block */
687 cnt_inc(&eb_entry->cnt_nodes);
689 /* don't count keep-alive edges */
690 if (get_irn_op(node) == op_End)
693 arity = get_irn_arity(node);
695 for (i = 0; i < arity; ++i) {
696 ir_node *pred = get_irn_n(node, i);
697 ir_extblk *other_extbb = get_nodes_extbb(pred);
699 if (other_extbb == extbb)
700 cnt_inc(&eb_entry->cnt_edges); /* a in extbb edge */
702 extbb_entry_t *eb_entry_other = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(other_extbb), graph->extbb_hash);
704 cnt_inc(&eb_entry->cnt_in_edges); /* an edge coming from another extbb */
705 cnt_inc(&eb_entry_other->cnt_out_edges);
710 /** calculates how many arguments of the call are const */
711 static int cnt_const_args(ir_node *call)
714 int n = get_Call_n_params(call);
716 for (i = 0; i < n; ++i) {
717 ir_node *param = get_Call_param(call, i);
718 ir_op *op = get_irn_op(param);
720 if (op == op_Const || op == op_SymConst)
727 * update info on calls
729 * @param call The call
730 * @param graph The graph entry containing the call
732 static void stat_update_call(ir_node *call, graph_entry_t *graph)
734 ir_node *block = get_nodes_block(call);
735 ir_node *ptr = get_Call_ptr(call);
737 ir_graph *callee = NULL;
741 * If the block is bad, the whole subgraph will collapse later
742 * so do not count this call.
743 * This happens in dead code.
748 cnt_inc(&graph->cnt_all_calls);
750 /* found a call, this function is not a leaf */
753 if (get_irn_op(ptr) == op_SymConst) {
754 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
755 /* ok, we seems to know the entity */
756 ent = get_SymConst_entity(ptr);
757 callee = get_entity_irg(ent);
759 /* it is recursive, if it calls at least once */
760 if (callee == graph->irg)
761 graph->is_recursive = 1;
765 /* indirect call, be could not predict */
766 cnt_inc(&graph->cnt_indirect_calls);
768 /* NOT a leaf call */
769 graph->is_leaf_call = LCS_NON_LEAF_CALL;
772 /* check, if it's a chain-call: Then, the call-block
773 * must dominate the end block. */
775 ir_node *curr = get_irg_end_block(graph->irg);
776 int depth = get_Block_dom_depth(block);
778 for (; curr != block && get_Block_dom_depth(curr) > depth;) {
779 curr = get_Block_idom(curr);
781 if (! curr || is_no_Block(curr))
786 graph->is_chain_call = 0;
789 /* check, if the callee is a leaf */
791 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
793 if (called->is_analyzed) {
794 if (! called->is_leaf)
795 graph->is_leaf_call = LCS_NON_LEAF_CALL;
799 /* check, if arguments of the call are const */
800 num_const_args = cnt_const_args(call);
802 if (num_const_args > 0)
803 cnt_inc(&graph->cnt_call_with_cnst_arg);
807 * update info on calls for graphs on the wait queue
809 static void stat_update_call_2(ir_node *call, graph_entry_t *graph)
811 ir_node *block = get_nodes_block(call);
812 ir_node *ptr = get_Call_ptr(call);
814 ir_graph *callee = NULL;
817 * If the block is bad, the whole subgraph will collapse later
818 * so do not count this call.
819 * This happens in dead code.
824 if (get_irn_op(ptr) == op_SymConst) {
825 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
826 /* ok, we seems to know the entity */
827 ent = get_SymConst_entity(ptr);
828 callee = get_entity_irg(ent);
832 /* check, if the callee is a leaf */
834 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
836 assert(called->is_analyzed);
838 if (! called->is_leaf)
839 graph->is_leaf_call = LCS_NON_LEAF_CALL;
842 graph->is_leaf_call = LCS_NON_LEAF_CALL;
846 * walker for reachable nodes count
848 static void update_node_stat(ir_node *node, void *env)
850 graph_entry_t *graph = env;
853 ir_op *op = stat_get_irn_op(node);
854 int arity = get_irn_arity(node);
856 entry = opcode_get_entry(op, graph->opcode_hash);
858 cnt_inc(&entry->cnt_alive);
859 cnt_add_i(&graph->cnt_edges, arity);
861 /* count block edges */
862 undate_block_info(node, graph);
864 /* count extended block edges */
865 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
866 if (graph->irg != get_const_code_irg())
867 undate_extbb_info(node, graph);
870 /* handle statistics for special node types */
872 if (op == op_Const) {
873 if (status->stat_options & FIRMSTAT_COUNT_CONSTS) {
874 /* check properties of constants */
875 stat_update_const(status, node, graph);
878 else if (op == op_Call) {
879 /* check for properties that depends on calls like recursion/leaf/indirect call */
880 stat_update_call(node, graph);
885 * walker for reachable nodes count for graphs on the wait_q
887 static void update_node_stat_2(ir_node *node, void *env)
889 graph_entry_t *graph = env;
891 /* check for properties that depends on calls like recursion/leaf/indirect call */
893 stat_update_call_2(node, graph);
897 * get the current address mark
899 static unsigned get_adr_mark(graph_entry_t *graph, ir_node *node)
901 address_mark_entry_t *value = set_find(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
903 return value ? value->mark : 0;
907 * set the current address mark
909 static void set_adr_mark(graph_entry_t *graph, ir_node *node, unsigned val)
911 address_mark_entry_t *value = set_insert(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
918 * a vcg attribute hook: Color a node with a different color if
919 * it's identified as a part of an address expression or at least referenced
920 * by an address expression.
922 static int stat_adr_mark_hook(FILE *F, ir_node *node, ir_node *local)
924 ir_node *n = local ? local : node;
925 ir_graph *irg = get_irn_irg(n);
926 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
927 unsigned mark = get_adr_mark(graph, n);
929 if (mark & MARK_ADDRESS_CALC)
930 fprintf(F, "color: purple");
931 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
932 fprintf(F, "color: pink");
933 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
934 fprintf(F, "color: lightblue");
938 /* I know the color! */
944 * walker that marks every node that is an address calculation
946 * predecessor nodes must be visited first. We ensure this by
947 * calling in in the post of an outs walk. This should work even in cycles,
948 * while the pre in a normal walk will not.
950 static void mark_address_calc(ir_node *node, void *env)
952 graph_entry_t *graph = env;
953 ir_mode *mode = get_irn_mode(node);
955 unsigned mark_preds = MARK_REF_NON_ADR;
957 if (! mode_is_numP(mode))
960 if (mode_is_reference(mode)) {
961 /* a reference is calculated here, we are sure */
962 set_adr_mark(graph, node, MARK_ADDRESS_CALC);
964 mark_preds = MARK_REF_ADR;
967 unsigned mark = get_adr_mark(graph, node);
969 if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR) {
971 * this node has not an reference mode, but is only
972 * referenced by address calculations
974 mark_preds = MARK_REF_ADR;
978 /* mark all predecessors */
979 for (i = 0, n = get_irn_arity(node); i < n; ++i) {
980 ir_node *pred = get_irn_n(node, i);
982 set_adr_mark(graph, pred, get_adr_mark(graph, pred) | mark_preds);
987 * Called for every graph when the graph is either deleted or stat_dump_snapshot()
988 * is called, must recalculate all statistic info.
990 * @param global The global entry
991 * @param graph The current entry
993 static void update_graph_stat(graph_entry_t *global, graph_entry_t *graph)
997 /* clear first the alive counter in the graph */
998 for (entry = pset_first(graph->opcode_hash); entry; entry = pset_next(graph->opcode_hash)) {
999 cnt_clr(&entry->cnt_alive);
1002 /* set pessimistic values */
1004 graph->is_leaf_call = LCS_UNKNOWN;
1005 graph->is_recursive = 0;
1006 graph->is_chain_call = 1;
1008 /* create new block counter */
1009 graph->block_hash = new_pset(block_cmp, 5);
1011 /* we need dominator info */
1012 if (graph->irg != get_const_code_irg()) {
1013 assure_doms(graph->irg);
1015 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
1016 /* we need extended basic blocks */
1017 compute_extbb(graph->irg);
1019 /* create new extbb counter */
1020 graph->extbb_hash = new_pset(block_cmp, 5);
1024 /* count the nodes in the graph */
1025 irg_walk_graph(graph->irg, update_node_stat, NULL, graph);
1028 /* Uncomment this code if chain-call means call exact one */
1029 entry = opcode_get_entry(op_Call, graph->opcode_hash);
1031 /* check if we have more than 1 call */
1032 if (cnt_gt(entry->cnt_alive, 1))
1033 graph->is_chain_call = 0;
1036 /* recursive functions are never chain calls, leafs don't have calls */
1037 if (graph->is_recursive || graph->is_leaf)
1038 graph->is_chain_call = 0;
1040 /* assume we walk every graph only ONCE, we could sum here the global count */
1041 for (entry = pset_first(graph->opcode_hash); entry; entry = pset_next(graph->opcode_hash)) {
1042 node_entry_t *g_entry = opcode_get_entry(entry->op, global->opcode_hash);
1044 /* update the node counter */
1045 cnt_add(&g_entry->cnt_alive, &entry->cnt_alive);
1048 /* update the edge counter */
1049 cnt_add(&global->cnt_edges, &graph->cnt_edges);
1051 /* count the number of address calculation */
1052 if (graph->irg != get_const_code_irg()) {
1053 ir_graph *rem = current_ir_graph;
1055 if (get_irg_outs_state(graph->irg) != outs_consistent)
1056 compute_irg_outs(graph->irg);
1058 /* Must be done an the outs graph */
1059 current_ir_graph = graph->irg;
1060 irg_out_walk(get_irg_start(graph->irg), NULL, mark_address_calc, graph);
1061 current_ir_graph = rem;
1064 set_dump_node_vcgattr_hook(stat_adr_mark_hook);
1065 dump_ir_block_graph(graph->irg, "-adr");
1066 set_dump_node_vcgattr_hook(NULL);
1070 /* count the DAG's */
1071 if (status->stat_options & FIRMSTAT_COUNT_DAG)
1072 count_dags_in_graph(global, graph);
1074 /* calculate the patterns of this graph */
1075 stat_calc_pattern_history(graph->irg);
1077 /* leaf function did not call others */
1079 graph->is_leaf_call = LCS_NON_LEAF_CALL;
1080 else if (graph->is_leaf_call == LCS_UNKNOWN) {
1081 /* we still don't know if this graph calls leaf-functions, so enqueue */
1082 pdeq_putl(status->wait_q, graph);
1085 /* we have analyzed this graph */
1086 graph->is_analyzed = 1;
1090 * Called for every graph that was on the wait_q in stat_dump_snapshot()
1091 * must finish all statistic info calculations.
1093 * @param global The global entry
1094 * @param graph The current entry
1096 static void update_graph_stat_2(graph_entry_t *global, graph_entry_t *graph)
1098 if (graph->is_deleted) {
1099 /* deleted, ignore */
1104 /* count the nodes in the graph */
1105 irg_walk_graph(graph->irg, update_node_stat_2, NULL, graph);
1107 if (graph->is_leaf_call == LCS_UNKNOWN)
1108 graph->is_leaf_call = LCS_LEAF_CALL;
1115 static void stat_register_dumper(const dumper_t *dumper)
1117 dumper_t *p = xmalloc(sizeof(*p));
1120 memcpy(p, dumper, sizeof(*p));
1122 p->next = status->dumper;
1127 /* FIXME: memory leak */
1131 * dumps an IR graph.
1133 static void stat_dump_graph(graph_entry_t *entry)
1137 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1138 if (dumper->dump_graph)
1139 dumper->dump_graph(dumper, entry);
1144 * calls all registered functions.
1146 static void stat_dump_registered(graph_entry_t *entry)
1150 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1151 if (dumper->func_map) {
1152 dump_graph_FUNC func;
1154 foreach_pset(dumper->func_map, func)
1155 func(dumper, entry);
1161 * dumps a constant table
1163 static void stat_dump_consts(const constant_info_t *tbl)
1167 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1168 if (dumper->dump_const_tbl)
1169 dumper->dump_const_tbl(dumper, tbl);
1174 * initialize the dumper
1176 static void stat_dump_init(const char *name)
1180 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1182 dumper->init(dumper, name);
1189 static void stat_dump_finish(void)
1193 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1195 dumper->finish(dumper);
1200 * register an additional function for all dumper
1202 void stat_register_dumper_func(dump_graph_FUNC func) {
1205 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1206 if (! dumper->func_map)
1207 dumper->func_map = pset_new_ptr(3);
1208 pset_insert_ptr(dumper->func_map, func);
1212 /* ---------------------------------------------------------------------- */
1215 * helper: get an ir_op from an opcode
1217 ir_op *stat_get_op_from_opcode(opcode code)
1219 return opcode_find_entry(code, status->ir_op_hash);
1223 * A new IR op is registered.
1225 * @param ctx the hook context
1226 * @param op the new IR opcode that was created.
1228 static void stat_new_ir_op(void *ctx, ir_op *op)
1230 if (! status->stat_options)
1235 graph_entry_t *graph = graph_get_entry(NULL, status->irg_hash);
1237 /* execute for side effect :-) */
1238 opcode_get_entry(op, graph->opcode_hash);
1240 pset_insert(status->ir_op_hash, op, op->code);
1246 * An IR op is freed.
1248 * @param ctx the hook context
1249 * @param op the IR opcode that is freed
1251 static void stat_free_ir_op(void *ctx, ir_op *op)
1253 if (! status->stat_options)
1263 * A new node is created.
1265 * @param ctx the hook context
1266 * @param irg the IR graph on which the node is created
1267 * @param node the new IR node that was created
1269 static void stat_new_node(void *ctx, ir_graph *irg, ir_node *node)
1271 if (! status->stat_options)
1274 /* do NOT count during dead node elimination */
1275 if (status->in_dead_node_elim > 0)
1280 node_entry_t *entry;
1281 graph_entry_t *graph;
1282 ir_op *op = stat_get_irn_op(node);
1284 /* increase global value */
1285 graph = graph_get_entry(NULL, status->irg_hash);
1286 entry = opcode_get_entry(op, graph->opcode_hash);
1287 cnt_inc(&entry->new_node);
1289 /* increase local value */
1290 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1291 entry = opcode_get_entry(op, graph->opcode_hash);
1292 cnt_inc(&entry->new_node);
1298 * A node is changed into a Id node
1300 * @param ctx the hook context
1301 * @param node the IR node that will be turned into an ID
1303 static void stat_turn_into_id(void *ctx, ir_node *node)
1305 if (! status->stat_options)
1310 node_entry_t *entry;
1311 graph_entry_t *graph;
1312 ir_op *op = stat_get_irn_op(node);
1314 /* increase global value */
1315 graph = graph_get_entry(NULL, status->irg_hash);
1316 entry = opcode_get_entry(op, graph->opcode_hash);
1317 cnt_inc(&entry->into_Id);
1319 /* increase local value */
1320 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1321 entry = opcode_get_entry(op, graph->opcode_hash);
1322 cnt_inc(&entry->into_Id);
1328 * A new graph was created
1330 * @param ctx the hook context
1331 * @param irg the new IR graph that was created
1332 * @param ent the entity of this graph
1334 static void stat_new_graph(void *ctx, ir_graph *irg, entity *ent)
1336 if (! status->stat_options)
1341 /* execute for side effect :-) */
1342 graph_entry_t * graph = graph_get_entry(irg, status->irg_hash);
1345 graph->is_deleted = 0;
1347 graph->is_leaf_call = 0;
1348 graph->is_recursive = 0;
1349 graph->is_chain_call = 0;
1350 graph->is_analyzed = 0;
1356 * A graph will be deleted
1358 * @param ctx the hook context
1359 * @param irg the IR graph that will be deleted
1361 * Note that we still hold the information for this graph
1362 * in our hash maps, only a flag is set which prevents this
1363 * information from being changed, it's "frozen" from now.
1365 static void stat_free_graph(void *ctx, ir_graph *irg)
1367 if (! status->stat_options)
1372 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1373 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1375 graph->is_deleted = 1;
1377 if (status->stat_options & FIRMSTAT_COUNT_DELETED) {
1378 /* count the nodes of the graph yet, it will be destroyed later */
1379 update_graph_stat(global, graph);
1386 * A walk over a graph is initiated. Do not count walks from statistic code.
1388 * @param ctx the hook context
1389 * @param irg the IR graph that will be walked
1390 * @param pre the pre walker
1391 * @param post the post walker
1393 static void stat_irg_walk(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1395 if (! status->stat_options)
1400 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1402 cnt_inc(&graph->cnt_walked);
1408 * A walk over a graph in block-wise order is initiated. Do not count walks from statistic code.
1410 * @param ctx the hook context
1411 * @param irg the IR graph that will be walked
1412 * @param pre the pre walker
1413 * @param post the post walker
1415 static void stat_irg_walk_blkwise(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1417 /* for now, do NOT differentiate between blockwise and normal */
1418 stat_irg_walk(ctx, irg, pre, post);
1422 * A walk over the graph's blocks is initiated. Do not count walks from statistic code.
1424 * @param ctx the hook context
1425 * @param irg the IR graph that will be walked
1426 * @param node the IR node
1427 * @param pre the pre walker
1428 * @param post the post walker
1430 static void stat_irg_block_walk(void *ctx, ir_graph *irg, ir_node *node, generic_func *pre, generic_func *post)
1432 if (! status->stat_options)
1437 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1439 cnt_inc(&graph->cnt_walked_blocks);
1445 * called for every node that is removed due to an optimization.
1447 * @param n the IR node that will be removed
1448 * @param hmap the hash map containing ir_op* -> opt_entry_t*
1450 static void removed_due_opt(ir_node *n, hmap_opt_entry_t *hmap)
1452 ir_op *op = stat_get_irn_op(n);
1453 opt_entry_t *entry = opt_get_entry(op, hmap);
1455 /* increase global value */
1456 cnt_inc(&entry->count);
1460 * Some nodes were optimized into some others due to an optimization.
1462 * @param ctx the hook context
1464 static void stat_merge_nodes(
1466 ir_node **new_node_array, int new_num_entries,
1467 ir_node **old_node_array, int old_num_entries,
1470 if (! status->stat_options)
1476 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1478 if (status->reassoc_run)
1479 opt = HOOK_OPT_REASSOC;
1481 for (i = 0; i < old_num_entries; ++i) {
1482 for (j = 0; j < new_num_entries; ++j)
1483 if (old_node_array[i] == new_node_array[j])
1486 /* nodes might be in new and old, these are NOT removed */
1487 if (j >= new_num_entries) {
1490 /* sometimes we did not detect, that it is replaced by a Const */
1491 if (opt == HOOK_OPT_CONFIRM && new_num_entries == 1) {
1492 ir_op *op = get_irn_op(new_node_array[0]);
1494 if (op == op_Const || op == op_SymConst)
1495 xopt = HOOK_OPT_CONFIRM_C;
1498 removed_due_opt(old_node_array[i], graph->opt_hash[xopt]);
1506 * Reassociation is started/stopped.
1508 * @param ctx the hook context
1509 * @param flag if non-zero, reassociation is started else stopped
1511 static void stat_reassociate(void *ctx, int flag)
1513 if (! status->stat_options)
1518 status->reassoc_run = flag;
1524 * A node was lowered into other nodes
1526 * @param ctx the hook context
1527 * @param node the IR node that will be lowered
1529 static void stat_lower(void *ctx, ir_node *node)
1531 if (! status->stat_options)
1536 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1538 removed_due_opt(node, graph->opt_hash[HOOK_LOWERED]);
1544 * A graph was inlined.
1546 * @param ctx the hook context
1547 * @param call the IR call that will re changed into the body of
1548 * the called IR graph
1549 * @param called_irg the IR graph representing the called routine
1551 static void stat_inline(void *ctx, ir_node *call, ir_graph *called_irg)
1553 if (! status->stat_options)
1558 ir_graph *irg = get_irn_irg(call);
1559 graph_entry_t *i_graph = graph_get_entry(called_irg, status->irg_hash);
1560 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1562 cnt_inc(&graph->cnt_got_inlined);
1563 cnt_inc(&i_graph->cnt_was_inlined);
1569 * A graph with tail-recursions was optimized.
1571 * @param ctx the hook context
1573 static void stat_tail_rec(void *ctx, ir_graph *irg, int n_calls)
1575 if (! status->stat_options)
1580 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1582 graph->num_tail_recursion += n_calls;
1588 * Strength reduction was performed on an iteration variable.
1590 * @param ctx the hook context
1592 static void stat_strength_red(void *ctx, ir_graph *irg, ir_node *strong)
1594 if (! status->stat_options)
1599 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1600 cnt_inc(&graph->cnt_strength_red);
1602 removed_due_opt(strong, graph->opt_hash[HOOK_OPT_STRENGTH_RED]);
1608 * Start/Stop the dead node elimination.
1610 * @param ctx the hook context
1612 static void stat_dead_node_elim(void *ctx, ir_graph *irg, int start)
1614 if (! status->stat_options)
1618 ++status->in_dead_node_elim;
1620 --status->in_dead_node_elim;
1624 * if-conversion was tried
1626 static void stat_if_conversion(void *context, ir_graph *irg, ir_node *phi,
1627 int pos, ir_node *mux, if_result_t reason)
1629 if (! status->stat_options)
1634 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1636 cnt_inc(&graph->cnt_if_conv[reason]);
1642 * real function call was optimized
1644 static void stat_func_call(void *context, ir_graph *irg, ir_node *call)
1646 if (! status->stat_options)
1651 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1653 cnt_inc(&graph->cnt_real_func_call);
1659 * A multiply was replaced by a series of Shifts/Adds/Subs
1661 * @param ctx the hook context
1663 static void stat_arch_dep_replace_mul_with_shifts(void *ctx, ir_node *mul)
1665 if (! status->stat_options)
1670 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1671 removed_due_opt(mul, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1677 * A division by const was replaced
1679 * @param ctx the hook context
1680 * @param node the division node that will be optimized
1682 static void stat_arch_dep_replace_division_by_const(void *ctx, ir_node *node)
1684 if (! status->stat_options)
1689 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1690 removed_due_opt(node, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1696 * Update the register pressure of a block
1698 * @param irg the irg containing the block
1699 * @param block the block for which the reg pressure should be set
1700 * @param pressure the pressure
1701 * @param class_name the name of the register class
1703 void stat_be_block_regpressure(ir_graph *irg, ir_node *block, int pressure, const char *class_name)
1705 if (! status->stat_options)
1710 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1711 be_block_entry_t *block_ent;
1712 reg_pressure_entry_t *rp_ent;
1714 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1715 rp_ent = obstack_alloc(&status->be_data, sizeof(*rp_ent));
1716 memset(rp_ent, 0, sizeof(*rp_ent));
1718 rp_ent->class_name = class_name;
1719 rp_ent->pressure = pressure;
1721 pset_insert(block_ent->reg_pressure, rp_ent, HASH_PTR(class_name));
1727 * Update the distribution of ready nodes of a block
1729 * @param irg the irg containing the block
1730 * @param block the block for which the reg pressure should be set
1731 * @param num_ready the number of ready nodes
1733 void stat_be_block_sched_ready(ir_graph *irg, ir_node *block, int num_ready)
1735 if (! status->stat_options)
1740 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1741 be_block_entry_t *block_ent;
1743 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1745 /* increase the counter of corresponding number of ready nodes */
1746 stat_inc_int_distrib_tbl(block_ent->sched_ready, num_ready);
1752 * Update the permutation statistic of a block
1754 * @param class_name the name of the register class
1755 * @param n_regs number of registers in the register class
1756 * @param perm the perm node
1757 * @param block the block containing the perm
1758 * @param size the size of the perm
1759 * @param real_size number of pairs with different registers
1761 void stat_be_block_stat_perm(const char *class_name, int n_regs, ir_node *perm, ir_node *block,
1762 int size, int real_size)
1764 if (! status->stat_options)
1769 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
1770 be_block_entry_t *block_ent;
1771 perm_class_entry_t *pc_ent;
1772 perm_stat_entry_t *ps_ent;
1774 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1775 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
1776 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
1778 pc_ent->n_regs = n_regs;
1780 /* update information */
1781 ps_ent->size = size;
1782 ps_ent->real_size = real_size;
1788 * Update the permutation statistic of a single perm
1790 * @param class_name the name of the register class
1791 * @param perm the perm node
1792 * @param block the block containing the perm
1793 * @param is_chain 1 if chain, 0 if cycle
1794 * @param size length of the cycle/chain
1795 * @param n_ops the number of ops representing this cycle/chain after lowering
1797 void stat_be_block_stat_permcycle(const char *class_name, ir_node *perm, ir_node *block,
1798 int is_chain, int size, int n_ops)
1800 if (! status->stat_options)
1805 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
1806 be_block_entry_t *block_ent;
1807 perm_class_entry_t *pc_ent;
1808 perm_stat_entry_t *ps_ent;
1810 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1811 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
1812 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
1815 ps_ent->n_copies += n_ops;
1816 stat_inc_int_distrib_tbl(ps_ent->chains, size);
1819 ps_ent->n_exchg += n_ops;
1820 stat_inc_int_distrib_tbl(ps_ent->cycles, size);
1826 /* Dumps a statistics snapshot */
1827 void stat_dump_snapshot(const char *name, const char *phase)
1833 if (! status->stat_options)
1838 graph_entry_t *entry;
1839 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1842 * The constant counter is only global, so we clear it here.
1843 * Note that it does NOT contain the constants in DELETED
1844 * graphs due to this.
1846 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
1847 stat_const_clear(status);
1849 /* build the name */
1850 p = strrchr(name, '/');
1855 q = strrchr(name, '\\');
1857 /* NULL might be not the smallest pointer */
1858 if (q && (!p || q > p))
1866 if (l > sizeof(fname) - 1)
1867 l = sizeof(fname) - 1;
1869 memcpy(fname, name, l);
1876 strncat(fname, "firmstat-", sizeof(fname));
1877 strncat(fname, phase, sizeof(fname));
1878 strncat(fname, "-", sizeof(fname));
1879 strncat(fname, p, sizeof(fname));
1881 stat_dump_init(fname);
1883 /* calculate the graph statistics */
1884 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
1886 if (entry->irg == NULL) {
1887 /* special entry for the global count */
1891 if (! entry->is_deleted) {
1892 /* the graph is still alive, count the nodes on it */
1893 update_graph_stat(global, entry);
1897 /* some calculations are dependent, we pushed them on the wait_q */
1898 while (! pdeq_empty(status->wait_q)) {
1899 entry = pdeq_getr(status->wait_q);
1901 update_graph_stat_2(global, entry);
1905 /* dump per graph */
1906 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
1908 if (entry->irg == NULL) {
1909 /* special entry for the global count */
1913 if (! entry->is_deleted || status->stat_options & FIRMSTAT_COUNT_DELETED) {
1914 stat_dump_graph(entry);
1915 stat_dump_registered(entry);
1918 if (! entry->is_deleted) {
1919 /* clear the counter that are not accumulated */
1920 graph_clear_entry(entry, 0);
1925 stat_dump_graph(global);
1927 /* dump the const info */
1928 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
1929 stat_dump_consts(&status->const_info);
1933 stat_finish_pattern_history();
1935 /* clear the global counter here */
1937 node_entry_t *entry;
1939 for (entry = pset_first(global->opcode_hash); entry; entry = pset_next(global->opcode_hash)) {
1940 opcode_clear_entry(entry);
1942 /* clear all global counter */
1943 graph_clear_entry(global, 1);
1949 /** the hook entries for the Firm statistics module */
1950 static hook_entry_t stat_hooks[hook_last];
1952 /* initialize the statistics module. */
1953 void firm_init_stat(unsigned enable_options)
1955 #define X(a) a, sizeof(a)-1
1956 #define HOOK(h, fkt) \
1957 stat_hooks[h].hook._##h = fkt; register_hook(h, &stat_hooks[h])
1960 if (! (enable_options & FIRMSTAT_ENABLED))
1963 status = xmalloc(sizeof(*status));
1964 memset(status, 0, sizeof(*status));
1966 /* enable statistics */
1967 status->stat_options = enable_options & FIRMSTAT_ENABLED ? enable_options : 0;
1969 /* register all hooks */
1970 HOOK(hook_new_ir_op, stat_new_ir_op);
1971 HOOK(hook_free_ir_op, stat_free_ir_op);
1972 HOOK(hook_new_node, stat_new_node);
1973 HOOK(hook_turn_into_id, stat_turn_into_id);
1974 HOOK(hook_new_graph, stat_new_graph);
1975 HOOK(hook_free_graph, stat_free_graph);
1976 HOOK(hook_irg_walk, stat_irg_walk);
1977 HOOK(hook_irg_walk_blkwise, stat_irg_walk_blkwise);
1978 HOOK(hook_irg_block_walk, stat_irg_block_walk);
1979 HOOK(hook_merge_nodes, stat_merge_nodes);
1980 HOOK(hook_reassociate, stat_reassociate);
1981 HOOK(hook_lower, stat_lower);
1982 HOOK(hook_inline, stat_inline);
1983 HOOK(hook_tail_rec, stat_tail_rec);
1984 HOOK(hook_strength_red, stat_strength_red);
1985 HOOK(hook_dead_node_elim, stat_dead_node_elim);
1986 HOOK(hook_if_conversion, stat_if_conversion);
1987 HOOK(hook_func_call, stat_func_call);
1988 HOOK(hook_arch_dep_replace_mul_with_shifts, stat_arch_dep_replace_mul_with_shifts);
1989 HOOK(hook_arch_dep_replace_division_by_const, stat_arch_dep_replace_division_by_const);
1991 obstack_init(&status->cnts);
1992 obstack_init(&status->be_data);
1994 /* create the hash-tables */
1995 status->irg_hash = new_pset(graph_cmp, 8);
1996 status->ir_op_hash = new_pset(opcode_cmp_2, 1);
1998 /* create the wait queue */
1999 status->wait_q = new_pdeq();
2001 if (enable_options & FIRMSTAT_COUNT_STRONG_OP) {
2002 /* build the pseudo-ops */
2004 _op_Phi0.code = --num;
2005 _op_Phi0.name = new_id_from_chars(X("Phi0"));
2007 _op_PhiM.code = --num;
2008 _op_PhiM.name = new_id_from_chars(X("PhiM"));
2010 _op_ProjM.code = --num;
2011 _op_ProjM.name = new_id_from_chars(X("ProjM"));
2013 _op_MulC.code = --num;
2014 _op_MulC.name = new_id_from_chars(X("MulC"));
2016 _op_DivC.code = --num;
2017 _op_DivC.name = new_id_from_chars(X("DivC"));
2019 _op_ModC.code = --num;
2020 _op_ModC.name = new_id_from_chars(X("ModC"));
2022 _op_DivModC.code = --num;
2023 _op_DivModC.name = new_id_from_chars(X("DivModC"));
2025 status->op_Phi0 = &_op_Phi0;
2026 status->op_PhiM = &_op_PhiM;
2027 status->op_ProjM = &_op_ProjM;
2028 status->op_MulC = &_op_MulC;
2029 status->op_DivC = &_op_DivC;
2030 status->op_ModC = &_op_ModC;
2031 status->op_DivModC = &_op_DivModC;
2034 status->op_Phi0 = NULL;
2035 status->op_PhiM = NULL;
2036 status->op_ProjM = NULL;
2037 status->op_MulC = NULL;
2038 status->op_DivC = NULL;
2039 status->op_ModC = NULL;
2040 status->op_DivModC = NULL;
2043 if (enable_options & FIRMSTAT_COUNT_SELS) {
2044 _op_SelSel.code = --num;
2045 _op_SelSel.name = new_id_from_chars(X("Sel(Sel)"));
2047 _op_SelSelSel.code = --num;
2048 _op_SelSelSel.name = new_id_from_chars(X("Sel(Sel(Sel))"));
2050 status->op_SelSel = &_op_SelSel;
2051 status->op_SelSelSel = &_op_SelSelSel;
2054 status->op_SelSel = NULL;
2055 status->op_SelSelSel = NULL;
2058 /* register the dumper */
2059 stat_register_dumper(&simple_dumper);
2061 if (enable_options & FIRMSTAT_CSV_OUTPUT)
2062 stat_register_dumper(&csv_dumper);
2064 /* initialize the pattern hash */
2065 stat_init_pattern_history(enable_options & FIRMSTAT_PATTERN_ENABLED);
2067 /* initialize the Const options */
2068 if (enable_options & FIRMSTAT_COUNT_CONSTS)
2069 stat_init_const_cnt(status);
2076 * Frees all dumper structures;
2078 static void stat_term_dumper(void) {
2079 dumper_t *dumper, *next_dumper;
2081 for (dumper = status->dumper; dumper; /* iteration done in loop body */ ) {
2082 if (dumper->func_map)
2083 del_pset(dumper->func_map);
2085 next_dumper = dumper->next;
2087 dumper = next_dumper;
2092 /* terminates the statistics module, frees all memory */
2093 void stat_term(void) {
2094 if (status != (stat_info_t *)&status_disable) {
2095 obstack_free(&status->be_data, NULL);
2096 obstack_free(&status->cnts, NULL);
2101 status = (stat_info_t *)&status_disable;
2105 /* returns 1 if statistics were initialized, 0 otherwise */
2106 int stat_is_active(void) {
2107 return status != (stat_info_t *)&status_disable;
2112 /* initialize the statistics module. */
2113 void firm_init_stat(unsigned enable_options) {}
2115 /* Dumps a statistics snapshot */
2116 void stat_dump_snapshot(const char *name, const char *phase) {}
2118 /* terminates the statistics module, frees all memory */
2119 void stat_term(void);
2121 #endif /* FIRM_STATISTICS */