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) {
92 const node_entry_t *e1 = elt;
93 const node_entry_t *e2 = key;
95 return e1->op->code - e2->op->code;
99 * Compare two elements of the graph hash.
101 static int graph_cmp(const void *elt, const void *key) {
102 const graph_entry_t *e1 = elt;
103 const graph_entry_t *e2 = key;
105 return e1->irg != e2->irg;
109 * Compare two elements of the optimization hash.
111 static int opt_cmp(const void *elt, const void *key) {
112 const opt_entry_t *e1 = elt;
113 const opt_entry_t *e2 = key;
115 return e1->op->code != e2->op->code;
119 * Compare two elements of the block/extbb hash.
121 static int block_cmp(const void *elt, const void *key) {
122 const block_entry_t *e1 = elt;
123 const block_entry_t *e2 = key;
125 return e1->block_nr != e2->block_nr;
129 * Compare two elements of the be_block hash.
131 static int be_block_cmp(const void *elt, const void *key) {
132 const be_block_entry_t *e1 = elt;
133 const be_block_entry_t *e2 = key;
135 return e1->block_nr != e2->block_nr;
139 * Compare two elements of reg pressure hash.
141 static int reg_pressure_cmp(const void *elt, const void *key) {
142 const reg_pressure_entry_t *e1 = elt;
143 const reg_pressure_entry_t *e2 = key;
145 return e1->class_name != e2->class_name;
146 } /* reg_pressure_cmp */
149 * Compare two elements of the perm_stat hash.
151 static int perm_stat_cmp(const void *elt, const void *key) {
152 const perm_stat_entry_t *e1 = elt;
153 const perm_stat_entry_t *e2 = key;
155 return e1->perm != e2->perm;
156 } /* perm_stat_cmp */
159 * Compare two elements of the perm_class hash.
161 static int perm_class_cmp(const void *elt, const void *key) {
162 const perm_class_entry_t *e1 = elt;
163 const perm_class_entry_t *e2 = key;
165 return e1->class_name != e2->class_name;
166 } /* perm_class_cmp */
169 * Compare two elements of the ir_op hash.
171 static int opcode_cmp_2(const void *elt, const void *key) {
172 const ir_op *e1 = elt;
173 const ir_op *e2 = key;
175 return e1->code != e2->code;
179 * Compare two elements of the address_mark set.
181 static int address_mark_cmp(const void *elt, const void *key, size_t size) {
182 const address_mark_entry_t *e1 = elt;
183 const address_mark_entry_t *e2 = key;
185 /* compare only the nodes, the rest is used as data container */
186 return e1->node != e2->node;
187 } /* address_mark_cmp */
190 * Clear all counter in a node_entry_t.
192 static void opcode_clear_entry(node_entry_t *elem) {
193 cnt_clr(&elem->cnt_alive);
194 cnt_clr(&elem->new_node);
195 cnt_clr(&elem->into_Id);
196 } /* opcode_clear_entry */
199 * Returns the associates node_entry_t for an ir_op
201 * @param op the IR operation
202 * @param hmap a hash map containing ir_op* -> node_entry_t*
204 static node_entry_t *opcode_get_entry(const ir_op *op, hmap_node_entry_t *hmap) {
210 elem = pset_find(hmap, &key, op->code);
214 elem = obstack_alloc(&status->cnts, sizeof(*elem));
215 memset(elem, 0, sizeof(*elem));
218 opcode_clear_entry(elem);
222 return pset_insert(hmap, elem, op->code);
223 } /* opcode_get_entry */
226 * Returns the associates ir_op for an opcode
228 * @param code the IR opcode
229 * @param hmap the hash map containing opcode -> ir_op*
231 static ir_op *opcode_find_entry(opcode code, hmap_ir_op *hmap) {
235 return pset_find(hmap, &key, code);
236 } /* opcode_find_entry */
239 * Clears all counter in a graph_entry_t.
241 * @param elem the graph entry
242 * @param all if non-zero, clears all counters, else leave accumulated ones
244 static void graph_clear_entry(graph_entry_t *elem, int all) {
246 cnt_clr(&elem->cnt_walked);
247 cnt_clr(&elem->cnt_walked_blocks);
248 cnt_clr(&elem->cnt_was_inlined);
249 cnt_clr(&elem->cnt_got_inlined);
250 cnt_clr(&elem->cnt_strength_red);
251 cnt_clr(&elem->cnt_real_func_call);
253 cnt_clr(&elem->cnt_edges);
254 cnt_clr(&elem->cnt_all_calls);
255 cnt_clr(&elem->cnt_call_with_cnst_arg);
256 cnt_clr(&elem->cnt_indirect_calls);
257 cnt_clr(&elem->cnt_pure_adr_ops);
258 cnt_clr(&elem->cnt_all_adr_ops);
260 if (elem->block_hash) {
261 del_pset(elem->block_hash);
262 elem->block_hash = NULL;
265 if (elem->extbb_hash) {
266 del_pset(elem->extbb_hash);
267 elem->extbb_hash = NULL;
270 obstack_free(&elem->recalc_cnts, NULL);
271 obstack_init(&elem->recalc_cnts);
272 } /* graph_clear_entry */
275 * Returns the associated graph_entry_t for an IR graph.
277 * @param irg the IR graph
278 * @param hmap the hash map containing ir_graph* -> graph_entry_t*
280 static graph_entry_t *graph_get_entry(ir_graph *irg, hmap_graph_entry_t *hmap)
288 elem = pset_find(hmap, &key, HASH_PTR(irg));
291 /* create hash map backend block information */
292 if (! elem->be_block_hash)
293 elem->be_block_hash = new_pset(be_block_cmp, 5);
298 /* allocate a new one */
299 elem = obstack_alloc(&status->cnts, sizeof(*elem));
300 memset(elem, 0, sizeof(*elem));
301 obstack_init(&elem->recalc_cnts);
304 graph_clear_entry(elem, 1);
306 /* new hash table for opcodes here */
307 elem->opcode_hash = new_pset(opcode_cmp, 5);
308 elem->address_mark = new_set(address_mark_cmp, 5);
311 /* these hash tables are created on demand */
312 elem->block_hash = NULL;
313 elem->extbb_hash = NULL;
315 for (i = 0; i < sizeof(elem->opt_hash)/sizeof(elem->opt_hash[0]); ++i)
316 elem->opt_hash[i] = new_pset(opt_cmp, 4);
318 return pset_insert(hmap, elem, HASH_PTR(irg));
319 } /* graph_get_entry */
322 * Clear all counter in an opt_entry_t.
324 static void opt_clear_entry(opt_entry_t *elem) {
325 cnt_clr(&elem->count);
326 } /* opt_clear_entry */
329 * Returns the associated opt_entry_t for an IR operation.
331 * @param op the IR operation
332 * @param hmap the hash map containing ir_op* -> opt_entry_t*
334 static opt_entry_t *opt_get_entry(const ir_op *op, hmap_opt_entry_t *hmap)
341 elem = pset_find(hmap, &key, op->code);
345 elem = obstack_alloc(&status->cnts, sizeof(*elem));
346 memset(elem, 0, sizeof(*elem));
348 /* clear new counter */
349 opt_clear_entry(elem);
353 return pset_insert(hmap, elem, op->code);
354 } /* opt_get_entry */
357 * clears all counter in a block_entry_t
359 static void block_clear_entry(block_entry_t *elem) {
360 cnt_clr(&elem->cnt_nodes);
361 cnt_clr(&elem->cnt_edges);
362 cnt_clr(&elem->cnt_in_edges);
363 cnt_clr(&elem->cnt_out_edges);
364 cnt_clr(&elem->cnt_phi_data);
365 } /* block_clear_entry */
368 * Returns the associated block_entry_t for an block.
370 * @param block_nr an IR block number
371 * @param hmap a hash map containing long -> block_entry_t
373 static block_entry_t *block_get_entry(struct obstack *obst, long block_nr, hmap_block_entry_t *hmap)
378 key.block_nr = block_nr;
380 elem = pset_find(hmap, &key, block_nr);
384 elem = obstack_alloc(obst, sizeof(*elem));
385 memset(elem, 0, sizeof(*elem));
387 /* clear new counter */
388 block_clear_entry(elem);
390 elem->block_nr = block_nr;
392 return pset_insert(hmap, elem, block_nr);
393 } /* block_get_entry */
396 * Clear all sets in be_block_entry_t.
398 static void be_block_clear_entry(be_block_entry_t *elem)
400 if (elem->reg_pressure)
401 del_pset(elem->reg_pressure);
403 if (elem->sched_ready)
404 stat_delete_distrib_tbl(elem->sched_ready);
406 if (elem->perm_class_stat)
407 del_pset(elem->perm_class_stat);
409 elem->reg_pressure = new_pset(reg_pressure_cmp, 5);
410 elem->sched_ready = stat_new_int_distrib_tbl();
411 elem->perm_class_stat = new_pset(perm_class_cmp, 5);
412 } /* be_block_clear_entry */
415 * Returns the associated be_block_entry_t for an block.
417 * @param block_nr an IR block number
418 * @param hmap a hash map containing long -> be_block_entry_t
420 static be_block_entry_t *be_block_get_entry(struct obstack *obst, long block_nr, hmap_be_block_entry_t *hmap)
422 be_block_entry_t key;
423 be_block_entry_t *elem;
425 key.block_nr = block_nr;
427 elem = pset_find(hmap, &key, block_nr);
431 elem = obstack_alloc(obst, sizeof(*elem));
432 memset(elem, 0, sizeof(*elem));
434 /* clear new counter */
435 be_block_clear_entry(elem);
437 elem->block_nr = block_nr;
439 return pset_insert(hmap, elem, block_nr);
440 } /* be_block_get_entry */
443 * clears all sets in perm_class_entry_t
445 static void perm_class_clear_entry(perm_class_entry_t *elem) {
447 del_pset(elem->perm_stat);
449 elem->perm_stat = new_pset(perm_stat_cmp, 5);
450 } /* perm_class_clear_entry */
453 * Returns the associated perm_class entry for a register class.
455 * @param class_name the register class name
456 * @param hmap a hash map containing class_name -> perm_class_entry_t
458 static perm_class_entry_t *perm_class_get_entry(struct obstack *obst, const char *class_name,
459 hmap_perm_class_entry_t *hmap)
461 perm_class_entry_t key;
462 perm_class_entry_t *elem;
464 key.class_name = class_name;
466 elem = pset_find(hmap, &key, HASH_PTR(class_name));
470 elem = obstack_alloc(obst, sizeof(*elem));
471 memset(elem, 0, sizeof(*elem));
473 /* clear new counter */
474 perm_class_clear_entry(elem);
476 elem->class_name = class_name;
478 return pset_insert(hmap, elem, HASH_PTR(class_name));
479 } /* perm_class_get_entry */
482 * clears all sets in perm_stat_entry_t
484 static void perm_stat_clear_entry(perm_stat_entry_t *elem) {
486 stat_delete_distrib_tbl(elem->chains);
489 stat_delete_distrib_tbl(elem->cycles);
491 elem->chains = stat_new_int_distrib_tbl();
492 elem->cycles = stat_new_int_distrib_tbl();
493 } /* perm_stat_clear_entry */
496 * Returns the associated perm_stat entry for a perm.
498 * @param perm the perm node
499 * @param hmap a hash map containing perm -> perm_stat_entry_t
501 static perm_stat_entry_t *perm_stat_get_entry(struct obstack *obst, ir_node *perm, hmap_perm_stat_entry_t *hmap)
503 perm_stat_entry_t key;
504 perm_stat_entry_t *elem;
508 elem = pset_find(hmap, &key, HASH_PTR(perm));
512 elem = obstack_alloc(obst, sizeof(*elem));
513 memset(elem, 0, sizeof(*elem));
515 /* clear new counter */
516 perm_stat_clear_entry(elem);
520 return pset_insert(hmap, elem, HASH_PTR(perm));
521 } /* perm_stat_get_entry */
524 * Returns the ir_op for an IR-node,
525 * handles special cases and return pseudo op codes.
527 * @param none an IR node
529 static ir_op *stat_get_irn_op(ir_node *node)
531 ir_op *op = get_irn_op(node);
533 if (op == op_Phi && get_irn_arity(node) == 0) {
534 /* special case, a Phi0 node, count on extra counter */
535 op = status->op_Phi0 ? status->op_Phi0 : op;
536 } else if (op == op_Phi && get_irn_mode(node) == mode_M) {
537 /* special case, a Memory Phi node, count on extra counter */
538 op = status->op_PhiM ? status->op_PhiM : op;
539 } else if (op == op_Proj && get_irn_mode(node) == mode_M) {
540 /* special case, a Memory Proj node, count on extra counter */
541 op = status->op_ProjM ? status->op_ProjM : op;
542 } else if (op == op_Mul &&
543 (get_irn_op(get_Mul_left(node)) == op_Const || get_irn_op(get_Mul_right(node)) == op_Const)) {
544 /* special case, a Multiply by a const, count on extra counter */
545 op = status->op_MulC ? status->op_MulC : op;
546 } else if (op == op_Div && get_irn_op(get_Div_right(node)) == op_Const) {
547 /* special case, a division by a const, count on extra counter */
548 op = status->op_DivC ? status->op_DivC : op;
549 } else if (op == op_Mod && get_irn_op(get_Mod_right(node)) == op_Const) {
550 /* special case, a module by a const, count on extra counter */
551 op = status->op_ModC ? status->op_ModC : op;
552 } else if (op == op_DivMod && get_irn_op(get_DivMod_right(node)) == op_Const) {
553 /* special case, a division/modulo by a const, count on extra counter */
554 op = status->op_DivModC ? status->op_DivModC : op;
555 } else if (op == op_Sel && get_irn_op(get_Sel_ptr(node)) == op_Sel) {
556 /* special case, a Sel of a Sel, count on extra counter */
557 op = status->op_SelSel ? status->op_SelSel : op;
559 if (get_irn_op(get_Sel_ptr(get_Sel_ptr(node))) == op_Sel) {
560 /* special case, a Sel of a Sel of a Sel, count on extra counter */
561 op = status->op_SelSelSel ? status->op_SelSelSel : op;
566 } /* stat_get_irn_op */
569 * update the block counter
571 static void undate_block_info(ir_node *node, graph_entry_t *graph)
573 ir_op *op = get_irn_op(node);
575 block_entry_t *b_entry;
578 /* check for block */
579 if (op == op_Block) {
580 arity = get_irn_arity(node);
581 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(node), graph->block_hash);
583 /* count all incoming edges */
584 for (i = 0; i < arity; ++i) {
585 ir_node *pred = get_irn_n(node, i);
586 ir_node *other_block = get_nodes_block(pred);
587 block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
589 cnt_inc(&b_entry->cnt_in_edges); /* an edge coming from another block */
590 cnt_inc(&b_entry_other->cnt_out_edges);
595 block = get_nodes_block(node);
596 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(block), graph->block_hash);
598 if (op == op_Phi && mode_is_datab(get_irn_mode(node))) {
599 /* count data Phi per block */
600 cnt_inc(&b_entry->cnt_phi_data);
603 /* we have a new node in our block */
604 cnt_inc(&b_entry->cnt_nodes);
606 /* don't count keep-alive edges */
607 if (get_irn_op(node) == op_End)
610 arity = get_irn_arity(node);
612 for (i = 0; i < arity; ++i) {
613 ir_node *pred = get_irn_n(node, i);
614 ir_node *other_block;
616 other_block = get_nodes_block(pred);
618 if (other_block == block)
619 cnt_inc(&b_entry->cnt_edges); /* a in block edge */
621 block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
623 cnt_inc(&b_entry->cnt_in_edges); /* an edge coming from another block */
624 cnt_inc(&b_entry_other->cnt_out_edges);
627 } /* undate_block_info */
630 * Update the extended block counter.
632 static void update_extbb_info(ir_node *node, graph_entry_t *graph)
634 ir_op *op = get_irn_op(node);
636 extbb_entry_t *eb_entry;
639 /* check for block */
640 if (op == op_Block) {
641 extbb = get_nodes_extbb(node);
642 arity = get_irn_arity(node);
643 eb_entry = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(extbb), graph->extbb_hash);
645 /* count all incoming edges */
646 for (i = 0; i < arity; ++i) {
647 ir_node *pred = get_irn_n(node, i);
648 ir_extblk *other_extbb = get_nodes_extbb(pred);
650 if (extbb != other_extbb) {
651 extbb_entry_t *eb_entry_other = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(other_extbb), graph->extbb_hash);
653 cnt_inc(&eb_entry->cnt_in_edges); /* an edge coming from another extbb */
654 cnt_inc(&eb_entry_other->cnt_out_edges);
660 extbb = get_nodes_extbb(node);
661 eb_entry = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(extbb), graph->extbb_hash);
663 if (op == op_Phi && mode_is_datab(get_irn_mode(node))) {
664 /* count data Phi per extbb */
665 cnt_inc(&eb_entry->cnt_phi_data);
668 /* we have a new node in our block */
669 cnt_inc(&eb_entry->cnt_nodes);
671 /* don't count keep-alive edges */
672 if (get_irn_op(node) == op_End)
675 arity = get_irn_arity(node);
677 for (i = 0; i < arity; ++i) {
678 ir_node *pred = get_irn_n(node, i);
679 ir_extblk *other_extbb = get_nodes_extbb(pred);
681 if (other_extbb == extbb)
682 cnt_inc(&eb_entry->cnt_edges); /* a in extbb edge */
684 extbb_entry_t *eb_entry_other = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(other_extbb), graph->extbb_hash);
686 cnt_inc(&eb_entry->cnt_in_edges); /* an edge coming from another extbb */
687 cnt_inc(&eb_entry_other->cnt_out_edges);
690 } /* update_extbb_info */
692 /** Calculates how many arguments of the call are const. */
693 static int cnt_const_args(ir_node *call) {
695 int n = get_Call_n_params(call);
697 for (i = 0; i < n; ++i) {
698 ir_node *param = get_Call_param(call, i);
699 ir_op *op = get_irn_op(param);
701 if (op == op_Const || op == op_SymConst)
705 } /* cnt_const_args */
708 * Update info on calls.
710 * @param call The call
711 * @param graph The graph entry containing the call
713 static void stat_update_call(ir_node *call, graph_entry_t *graph)
715 ir_node *block = get_nodes_block(call);
716 ir_node *ptr = get_Call_ptr(call);
717 ir_entity *ent = NULL;
718 ir_graph *callee = NULL;
722 * If the block is bad, the whole subgraph will collapse later
723 * so do not count this call.
724 * This happens in dead code.
729 cnt_inc(&graph->cnt_all_calls);
731 /* found a call, this function is not a leaf */
734 if (get_irn_op(ptr) == op_SymConst) {
735 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
736 /* ok, we seems to know the entity */
737 ent = get_SymConst_entity(ptr);
738 callee = get_entity_irg(ent);
740 /* it is recursive, if it calls at least once */
741 if (callee == graph->irg)
742 graph->is_recursive = 1;
745 /* indirect call, be could not predict */
746 cnt_inc(&graph->cnt_indirect_calls);
748 /* NOT a leaf call */
749 graph->is_leaf_call = LCS_NON_LEAF_CALL;
752 /* check, if it's a chain-call: Then, the call-block
753 * must dominate the end block. */
755 ir_node *curr = get_irg_end_block(graph->irg);
756 int depth = get_Block_dom_depth(block);
758 for (; curr != block && get_Block_dom_depth(curr) > depth;) {
759 curr = get_Block_idom(curr);
761 if (! curr || is_no_Block(curr))
766 graph->is_chain_call = 0;
769 /* check, if the callee is a leaf */
771 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
773 if (called->is_analyzed) {
774 if (! called->is_leaf)
775 graph->is_leaf_call = LCS_NON_LEAF_CALL;
779 /* check, if arguments of the call are const */
780 num_const_args = cnt_const_args(call);
782 if (num_const_args > 0)
783 cnt_inc(&graph->cnt_call_with_cnst_arg);
784 } /* stat_update_call */
787 * Update info on calls for graphs on the wait queue.
789 static void stat_update_call_2(ir_node *call, graph_entry_t *graph)
791 ir_node *block = get_nodes_block(call);
792 ir_node *ptr = get_Call_ptr(call);
793 ir_entity *ent = NULL;
794 ir_graph *callee = NULL;
797 * If the block is bad, the whole subgraph will collapse later
798 * so do not count this call.
799 * This happens in dead code.
804 if (get_irn_op(ptr) == op_SymConst) {
805 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
806 /* ok, we seems to know the entity */
807 ent = get_SymConst_entity(ptr);
808 callee = get_entity_irg(ent);
812 /* check, if the callee is a leaf */
814 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
816 assert(called->is_analyzed);
818 if (! called->is_leaf)
819 graph->is_leaf_call = LCS_NON_LEAF_CALL;
821 graph->is_leaf_call = LCS_NON_LEAF_CALL;
822 } /* stat_update_call_2 */
825 * Walker for reachable nodes count.
827 static void update_node_stat(ir_node *node, void *env)
829 graph_entry_t *graph = env;
832 ir_op *op = stat_get_irn_op(node);
833 int arity = get_irn_arity(node);
835 entry = opcode_get_entry(op, graph->opcode_hash);
837 cnt_inc(&entry->cnt_alive);
838 cnt_add_i(&graph->cnt_edges, arity);
840 /* count block edges */
841 undate_block_info(node, graph);
843 /* count extended block edges */
844 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
845 if (graph->irg != get_const_code_irg())
846 update_extbb_info(node, graph);
849 /* handle statistics for special node types */
851 if (op == op_Const) {
852 if (status->stat_options & FIRMSTAT_COUNT_CONSTS) {
853 /* check properties of constants */
854 stat_update_const(status, node, graph);
856 } else if (op == op_Call) {
857 /* check for properties that depends on calls like recursion/leaf/indirect call */
858 stat_update_call(node, graph);
860 } /* update_node_stat */
863 * Walker for reachable nodes count for graphs on the wait_q.
865 static void update_node_stat_2(ir_node *node, void *env) {
866 graph_entry_t *graph = env;
868 /* check for properties that depends on calls like recursion/leaf/indirect call */
870 stat_update_call_2(node, graph);
871 } /* update_node_stat_2 */
874 * Get the current address mark.
876 static unsigned get_adr_mark(graph_entry_t *graph, ir_node *node) {
877 address_mark_entry_t *value = set_find(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
879 return value ? value->mark : 0;
883 * Set the current address mark.
885 static void set_adr_mark(graph_entry_t *graph, ir_node *node, unsigned val) {
886 address_mark_entry_t *value = set_insert(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
895 * a vcg attribute hook: Color a node with a different color if
896 * it's identified as a part of an address expression or at least referenced
897 * by an address expression.
899 static int stat_adr_mark_hook(FILE *F, ir_node *node, ir_node *local)
901 ir_node *n = local ? local : node;
902 ir_graph *irg = get_irn_irg(n);
903 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
904 unsigned mark = get_adr_mark(graph, n);
906 if (mark & MARK_ADDRESS_CALC)
907 fprintf(F, "color: purple");
908 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
909 fprintf(F, "color: pink");
910 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
911 fprintf(F, "color: lightblue");
915 /* I know the color! */
917 } /* stat_adr_mark_hook */
918 #endif /* DUMP_ADR_MODE */
921 * Return the "operational" mode of a Firm node.
923 static ir_mode *get_irn_op_mode(ir_node *node) {
924 switch (get_irn_opcode(node)) {
926 return get_Load_mode(node);
928 return get_irn_mode(get_Store_value(node));
930 return get_irn_mode(get_DivMod_left(node));
932 return get_irn_mode(get_Div_left(node));
934 return get_irn_mode(get_Mod_left(node));
936 /* Cmp is no address calculation, or is it? */
938 return get_irn_mode(node);
940 } /* get_irn_op_mode */
943 * Post-walker that marks every node that is an address calculation.
945 * Users of a node must be visited first. We ensure this by
946 * calling it in the post of an outs walk. This should work even in cycles,
947 * while the normal pre-walk will not.
949 static void mark_address_calc(ir_node *node, void *env) {
950 graph_entry_t *graph = env;
951 ir_mode *mode = get_irn_op_mode(node);
953 unsigned mark_preds = MARK_REF_NON_ADR;
955 if (! mode_is_numP(mode))
958 if (mode_is_reference(mode)) {
959 /* a reference is calculated here, we are sure */
960 set_adr_mark(graph, node, MARK_ADDRESS_CALC);
962 mark_preds = MARK_REF_ADR;
964 unsigned mark = get_adr_mark(graph, node);
966 if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR) {
968 * this node has no reference mode, but is only
969 * referenced by address calculations
971 mark_preds = MARK_REF_ADR;
975 /* mark all predecessors */
976 for (i = 0, n = get_irn_arity(node); i < n; ++i) {
977 ir_node *pred = get_irn_n(node, i);
979 mode = get_irn_op_mode(pred);
980 if (! mode_is_numP(mode))
983 set_adr_mark(graph, pred, get_adr_mark(graph, pred) | mark_preds);
985 } /* mark_address_calc */
988 * Post-walker that marks every node that is an address calculation.
990 * Users of a node must be visited first. We ensure this by
991 * calling it in the post of an outs walk. This should work even in cycles,
992 * while the normal pre-walk will not.
994 static void count_adr_ops(ir_node *node, void *env) {
995 graph_entry_t *graph = env;
996 unsigned mark = get_adr_mark(graph, node);
998 if (mark & MARK_ADDRESS_CALC)
999 cnt_inc(&graph->cnt_pure_adr_ops);
1000 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1001 cnt_inc(&graph->cnt_pure_adr_ops);
1002 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1003 cnt_inc(&graph->cnt_all_adr_ops);
1004 } /* count_adr_ops */
1007 * Called for every graph when the graph is either deleted or stat_dump_snapshot()
1008 * is called, must recalculate all statistic info.
1010 * @param global The global entry
1011 * @param graph The current entry
1013 static void update_graph_stat(graph_entry_t *global, graph_entry_t *graph)
1015 node_entry_t *entry;
1017 /* clear first the alive counter in the graph */
1018 for (entry = pset_first(graph->opcode_hash); entry; entry = pset_next(graph->opcode_hash)) {
1019 cnt_clr(&entry->cnt_alive);
1022 /* set pessimistic values */
1024 graph->is_leaf_call = LCS_UNKNOWN;
1025 graph->is_recursive = 0;
1026 graph->is_chain_call = 1;
1028 /* create new block counter */
1029 graph->block_hash = new_pset(block_cmp, 5);
1031 /* we need dominator info */
1032 if (graph->irg != get_const_code_irg()) {
1033 assure_doms(graph->irg);
1035 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
1036 /* we need extended basic blocks */
1037 compute_extbb(graph->irg);
1039 /* create new extbb counter */
1040 graph->extbb_hash = new_pset(block_cmp, 5);
1044 /* count the nodes in the graph */
1045 irg_walk_graph(graph->irg, update_node_stat, NULL, graph);
1048 /* Uncomment this code if chain-call means call exact one. */
1049 entry = opcode_get_entry(op_Call, graph->opcode_hash);
1051 /* check if we have more than 1 call */
1052 if (cnt_gt(entry->cnt_alive, 1))
1053 graph->is_chain_call = 0;
1056 /* recursive functions are never chain calls, leafs don't have calls */
1057 if (graph->is_recursive || graph->is_leaf)
1058 graph->is_chain_call = 0;
1060 /* assume we walk every graph only ONCE, we could sum here the global count */
1061 for (entry = pset_first(graph->opcode_hash); entry; entry = pset_next(graph->opcode_hash)) {
1062 node_entry_t *g_entry = opcode_get_entry(entry->op, global->opcode_hash);
1064 /* update the node counter */
1065 cnt_add(&g_entry->cnt_alive, &entry->cnt_alive);
1068 /* update the edge counter */
1069 cnt_add(&global->cnt_edges, &graph->cnt_edges);
1071 /* count the number of address calculation */
1072 if (graph->irg != get_const_code_irg()) {
1073 ir_graph *rem = current_ir_graph;
1075 assure_irg_outs(graph->irg);
1077 /* Must be done an the outs graph */
1078 current_ir_graph = graph->irg;
1079 irg_out_walk(get_irg_start(graph->irg), NULL, mark_address_calc, graph);
1080 current_ir_graph = rem;
1082 #ifdef DUMP_ADR_MODE
1083 /* register the vcg hook and dump the graph for test */
1084 set_dump_node_vcgattr_hook(stat_adr_mark_hook);
1085 dump_ir_block_graph(graph->irg, "-adr");
1086 set_dump_node_vcgattr_hook(NULL);
1087 #endif /* DUMP_ADR_MODE */
1089 irg_walk_graph(graph->irg, NULL, count_adr_ops, graph);
1092 /* count the DAG's */
1093 if (status->stat_options & FIRMSTAT_COUNT_DAG)
1094 count_dags_in_graph(global, graph);
1096 /* calculate the patterns of this graph */
1097 stat_calc_pattern_history(graph->irg);
1099 /* leaf function did not call others */
1101 graph->is_leaf_call = LCS_NON_LEAF_CALL;
1102 else if (graph->is_leaf_call == LCS_UNKNOWN) {
1103 /* we still don't know if this graph calls leaf-functions, so enqueue */
1104 pdeq_putl(status->wait_q, graph);
1107 /* we have analyzed this graph */
1108 graph->is_analyzed = 1;
1109 } /* update_graph_stat */
1112 * Called for every graph that was on the wait_q in stat_dump_snapshot()
1113 * must finish all statistic info calculations.
1115 * @param global The global entry
1116 * @param graph The current entry
1118 static void update_graph_stat_2(graph_entry_t *global, graph_entry_t *graph)
1120 if (graph->is_deleted) {
1121 /* deleted, ignore */
1126 /* count the nodes in the graph */
1127 irg_walk_graph(graph->irg, update_node_stat_2, NULL, graph);
1129 if (graph->is_leaf_call == LCS_UNKNOWN)
1130 graph->is_leaf_call = LCS_LEAF_CALL;
1132 } /* update_graph_stat_2 */
1135 * Register a dumper.
1137 static void stat_register_dumper(const dumper_t *dumper) {
1138 dumper_t *p = xmalloc(sizeof(*p));
1141 memcpy(p, dumper, sizeof(*p));
1143 p->next = status->dumper;
1148 /* FIXME: memory leak */
1149 } /* stat_register_dumper */
1152 * Dumps the statistics of an IR graph.
1154 static void stat_dump_graph(graph_entry_t *entry) {
1157 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1158 if (dumper->dump_graph)
1159 dumper->dump_graph(dumper, entry);
1161 } /* stat_dump_graph */
1164 * Calls all registered dumper functions.
1166 static void stat_dump_registered(graph_entry_t *entry) {
1169 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1170 if (dumper->func_map) {
1171 dump_graph_FUNC func;
1173 foreach_pset(dumper->func_map, func)
1174 func(dumper, entry);
1177 } /* stat_dump_registered */
1180 * Dumps a constant table.
1182 static void stat_dump_consts(const constant_info_t *tbl) {
1185 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1186 if (dumper->dump_const_tbl)
1187 dumper->dump_const_tbl(dumper, tbl);
1189 } /* stat_dump_consts */
1192 * Initialize the dumper.
1194 static void stat_dump_init(const char *name) {
1197 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1199 dumper->init(dumper, name);
1201 } /* stat_dump_init */
1204 * Finish the dumper.
1206 static void stat_dump_finish(void) {
1209 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1211 dumper->finish(dumper);
1213 } /* stat_dump_finish */
1216 * Register an additional function for all dumper.
1218 void stat_register_dumper_func(dump_graph_FUNC func) {
1221 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1222 if (! dumper->func_map)
1223 dumper->func_map = pset_new_ptr(3);
1224 pset_insert_ptr(dumper->func_map, func);
1226 } /* stat_register_dumper_func */
1228 /* ---------------------------------------------------------------------- */
1231 * Helper: get an ir_op from an opcode.
1233 ir_op *stat_get_op_from_opcode(opcode code) {
1234 return opcode_find_entry(code, status->ir_op_hash);
1235 } /* stat_get_op_from_opcode */
1238 * Hook: A new IR op is registered.
1240 * @param ctx the hook context
1241 * @param op the new IR opcode that was created.
1243 static void stat_new_ir_op(void *ctx, ir_op *op) {
1244 if (! status->stat_options)
1249 graph_entry_t *graph = graph_get_entry(NULL, status->irg_hash);
1251 /* execute for side effect :-) */
1252 (void)opcode_get_entry(op, graph->opcode_hash);
1254 pset_insert(status->ir_op_hash, op, op->code);
1257 } /* stat_new_ir_op */
1260 * Hook: An IR op is freed.
1262 * @param ctx the hook context
1263 * @param op the IR opcode that is freed
1265 static void stat_free_ir_op(void *ctx, ir_op *op) {
1266 if (! status->stat_options)
1273 } /* stat_free_ir_op */
1276 * Hook: A new node is created.
1278 * @param ctx the hook context
1279 * @param irg the IR graph on which the node is created
1280 * @param node the new IR node that was created
1282 static void stat_new_node(void *ctx, ir_graph *irg, ir_node *node) {
1283 if (! status->stat_options)
1286 /* do NOT count during dead node elimination */
1287 if (status->in_dead_node_elim > 0)
1292 node_entry_t *entry;
1293 graph_entry_t *graph;
1294 ir_op *op = stat_get_irn_op(node);
1296 /* increase global value */
1297 graph = graph_get_entry(NULL, status->irg_hash);
1298 entry = opcode_get_entry(op, graph->opcode_hash);
1299 cnt_inc(&entry->new_node);
1301 /* increase local value */
1302 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1303 entry = opcode_get_entry(op, graph->opcode_hash);
1304 cnt_inc(&entry->new_node);
1307 } /* stat_new_node */
1310 * Hook: A node is changed into a Id node
1312 * @param ctx the hook context
1313 * @param node the IR node that will be turned into an ID
1315 static void stat_turn_into_id(void *ctx, ir_node *node) {
1316 if (! status->stat_options)
1321 node_entry_t *entry;
1322 graph_entry_t *graph;
1323 ir_op *op = stat_get_irn_op(node);
1325 /* increase global value */
1326 graph = graph_get_entry(NULL, status->irg_hash);
1327 entry = opcode_get_entry(op, graph->opcode_hash);
1328 cnt_inc(&entry->into_Id);
1330 /* increase local value */
1331 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1332 entry = opcode_get_entry(op, graph->opcode_hash);
1333 cnt_inc(&entry->into_Id);
1336 } /* stat_turn_into_id */
1339 * Hook: A new graph was created
1341 * @param ctx the hook context
1342 * @param irg the new IR graph that was created
1343 * @param ent the entity of this graph
1345 static void stat_new_graph(void *ctx, ir_graph *irg, ir_entity *ent) {
1346 if (! status->stat_options)
1351 /* execute for side effect :-) */
1352 graph_entry_t * graph = graph_get_entry(irg, status->irg_hash);
1355 graph->is_deleted = 0;
1357 graph->is_leaf_call = 0;
1358 graph->is_recursive = 0;
1359 graph->is_chain_call = 0;
1360 graph->is_analyzed = 0;
1363 } /* stat_new_graph */
1366 * Hook: A graph will be deleted
1368 * @param ctx the hook context
1369 * @param irg the IR graph that will be deleted
1371 * Note that we still hold the information for this graph
1372 * in our hash maps, only a flag is set which prevents this
1373 * information from being changed, it's "frozen" from now.
1375 static void stat_free_graph(void *ctx, ir_graph *irg) {
1376 if (! status->stat_options)
1381 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1382 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1384 graph->is_deleted = 1;
1386 if (status->stat_options & FIRMSTAT_COUNT_DELETED) {
1387 /* count the nodes of the graph yet, it will be destroyed later */
1388 update_graph_stat(global, graph);
1392 } /* stat_free_graph */
1395 * Hook: A walk over a graph is initiated. Do not count walks from statistic code.
1397 * @param ctx the hook context
1398 * @param irg the IR graph that will be walked
1399 * @param pre the pre walker
1400 * @param post the post walker
1402 static void stat_irg_walk(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1404 if (! status->stat_options)
1409 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1411 cnt_inc(&graph->cnt_walked);
1414 } /* stat_irg_walk */
1417 * Hook: A walk over a graph in block-wise order is initiated. Do not count walks from statistic code.
1419 * @param ctx the hook context
1420 * @param irg the IR graph that will be walked
1421 * @param pre the pre walker
1422 * @param post the post walker
1424 static void stat_irg_walk_blkwise(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1426 /* for now, do NOT differentiate between blockwise and normal */
1427 stat_irg_walk(ctx, irg, pre, post);
1428 } /* stat_irg_walk_blkwise */
1431 * Hook: A walk over the graph's blocks is initiated. Do not count walks from statistic code.
1433 * @param ctx the hook context
1434 * @param irg the IR graph that will be walked
1435 * @param node the IR node
1436 * @param pre the pre walker
1437 * @param post the post walker
1439 static void stat_irg_block_walk(void *ctx, ir_graph *irg, ir_node *node, generic_func *pre, generic_func *post)
1441 if (! status->stat_options)
1446 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1448 cnt_inc(&graph->cnt_walked_blocks);
1451 } /* stat_irg_block_walk */
1454 * Called for every node that is removed due to an optimization.
1456 * @param n the IR node that will be removed
1457 * @param hmap the hash map containing ir_op* -> opt_entry_t*
1459 static void removed_due_opt(ir_node *n, hmap_opt_entry_t *hmap) {
1460 ir_op *op = stat_get_irn_op(n);
1461 opt_entry_t *entry = opt_get_entry(op, hmap);
1463 /* increase global value */
1464 cnt_inc(&entry->count);
1465 } /* removed_due_opt */
1468 * Hook: Some nodes were optimized into some others due to an optimization.
1470 * @param ctx the hook context
1472 static void stat_merge_nodes(
1474 ir_node **new_node_array, int new_num_entries,
1475 ir_node **old_node_array, int old_num_entries,
1478 if (! status->stat_options)
1484 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1486 if (status->reassoc_run)
1487 opt = HOOK_OPT_REASSOC;
1489 for (i = 0; i < old_num_entries; ++i) {
1490 for (j = 0; j < new_num_entries; ++j) {
1491 if (old_node_array[i] == new_node_array[j])
1494 /* nodes might be in new and old, these are NOT removed */
1495 if (j >= new_num_entries) {
1498 /* sometimes we did not detect, that it is replaced by a Const */
1499 if (opt == HOOK_OPT_CONFIRM && new_num_entries == 1) {
1500 ir_op *op = get_irn_op(new_node_array[0]);
1502 if (op == op_Const || op == op_SymConst)
1503 xopt = HOOK_OPT_CONFIRM_C;
1506 removed_due_opt(old_node_array[i], graph->opt_hash[xopt]);
1512 } /* stat_merge_nodes */
1515 * Hook: Reassociation is started/stopped.
1517 * @param ctx the hook context
1518 * @param flag if non-zero, reassociation is started else stopped
1520 static void stat_reassociate(void *ctx, int flag) {
1521 if (! status->stat_options)
1526 status->reassoc_run = flag;
1529 } /* stat_reassociate */
1532 * Hook: A node was lowered into other nodes
1534 * @param ctx the hook context
1535 * @param node the IR node that will be lowered
1537 static void stat_lower(void *ctx, ir_node *node) {
1538 if (! status->stat_options)
1543 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1545 removed_due_opt(node, graph->opt_hash[HOOK_LOWERED]);
1551 * Hook: A graph was inlined.
1553 * @param ctx the hook context
1554 * @param call the IR call that will re changed into the body of
1555 * the called IR graph
1556 * @param called_irg the IR graph representing the called routine
1558 static void stat_inline(void *ctx, ir_node *call, ir_graph *called_irg)
1560 if (! status->stat_options)
1565 ir_graph *irg = get_irn_irg(call);
1566 graph_entry_t *i_graph = graph_get_entry(called_irg, status->irg_hash);
1567 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1569 cnt_inc(&graph->cnt_got_inlined);
1570 cnt_inc(&i_graph->cnt_was_inlined);
1576 * Hook: A graph with tail-recursions was optimized.
1578 * @param ctx the hook context
1580 static void stat_tail_rec(void *ctx, ir_graph *irg, int n_calls) {
1581 if (! status->stat_options)
1586 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1588 graph->num_tail_recursion += n_calls;
1591 } /* stat_tail_rec */
1594 * Strength reduction was performed on an iteration variable.
1596 * @param ctx the hook context
1598 static void stat_strength_red(void *ctx, ir_graph *irg, ir_node *strong) {
1599 if (! status->stat_options)
1604 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1605 cnt_inc(&graph->cnt_strength_red);
1607 removed_due_opt(strong, graph->opt_hash[HOOK_OPT_STRENGTH_RED]);
1610 } /* stat_strength_red */
1613 * Hook: Start/Stop the dead node elimination.
1615 * @param ctx the hook context
1617 static void stat_dead_node_elim(void *ctx, ir_graph *irg, int start) {
1618 if (! status->stat_options)
1622 ++status->in_dead_node_elim;
1624 --status->in_dead_node_elim;
1625 } /* stat_dead_node_elim */
1628 * Hook: if-conversion was tried.
1630 static void stat_if_conversion(void *context, ir_graph *irg, ir_node *phi,
1631 int pos, ir_node *mux, if_result_t reason)
1633 if (! status->stat_options)
1638 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1640 cnt_inc(&graph->cnt_if_conv[reason]);
1643 } /* stat_if_conversion */
1646 * Hook: real function call was optimized.
1648 static void stat_func_call(void *context, ir_graph *irg, ir_node *call)
1650 if (! status->stat_options)
1655 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1657 cnt_inc(&graph->cnt_real_func_call);
1660 } /* stat_func_call */
1663 * Hook: A multiply was replaced by a series of Shifts/Adds/Subs.
1665 * @param ctx the hook context
1667 static void stat_arch_dep_replace_mul_with_shifts(void *ctx, ir_node *mul) {
1668 if (! status->stat_options)
1673 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1674 removed_due_opt(mul, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1677 } /* stat_arch_dep_replace_mul_with_shifts */
1680 * Hook: A division by const was replaced.
1682 * @param ctx the hook context
1683 * @param node the division node that will be optimized
1685 static void stat_arch_dep_replace_division_by_const(void *ctx, ir_node *node) {
1686 if (! status->stat_options)
1691 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1692 removed_due_opt(node, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1695 } /* stat_arch_dep_replace_division_by_const */
1698 * Update the register pressure of a block.
1700 * @param irg the irg containing the block
1701 * @param block the block for which the reg pressure should be set
1702 * @param pressure the pressure
1703 * @param class_name the name of the register class
1705 void stat_be_block_regpressure(ir_graph *irg, ir_node *block, int pressure, const char *class_name)
1707 if (! status->stat_options)
1712 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1713 be_block_entry_t *block_ent;
1714 reg_pressure_entry_t *rp_ent;
1716 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1717 rp_ent = obstack_alloc(&status->be_data, sizeof(*rp_ent));
1718 memset(rp_ent, 0, sizeof(*rp_ent));
1720 rp_ent->class_name = class_name;
1721 rp_ent->pressure = pressure;
1723 pset_insert(block_ent->reg_pressure, rp_ent, HASH_PTR(class_name));
1726 } /* stat_be_block_regpressure */
1729 * Update the distribution of ready nodes of a block
1731 * @param irg the irg containing the block
1732 * @param block the block for which the reg pressure should be set
1733 * @param num_ready the number of ready nodes
1735 void stat_be_block_sched_ready(ir_graph *irg, ir_node *block, int num_ready)
1737 if (! status->stat_options)
1742 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1743 be_block_entry_t *block_ent;
1745 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1747 /* increase the counter of corresponding number of ready nodes */
1748 stat_inc_int_distrib_tbl(block_ent->sched_ready, num_ready);
1751 } /* stat_be_block_sched_ready */
1754 * Update the permutation statistic of a block.
1756 * @param class_name the name of the register class
1757 * @param n_regs number of registers in the register class
1758 * @param perm the perm node
1759 * @param block the block containing the perm
1760 * @param size the size of the perm
1761 * @param real_size number of pairs with different registers
1763 void stat_be_block_stat_perm(const char *class_name, int n_regs, ir_node *perm, ir_node *block,
1764 int size, int real_size)
1766 if (! status->stat_options)
1771 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
1772 be_block_entry_t *block_ent;
1773 perm_class_entry_t *pc_ent;
1774 perm_stat_entry_t *ps_ent;
1776 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1777 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
1778 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
1780 pc_ent->n_regs = n_regs;
1782 /* update information */
1783 ps_ent->size = size;
1784 ps_ent->real_size = real_size;
1787 } /* stat_be_block_stat_perm */
1790 * Update the permutation statistic of a single perm.
1792 * @param class_name the name of the register class
1793 * @param perm the perm node
1794 * @param block the block containing the perm
1795 * @param is_chain 1 if chain, 0 if cycle
1796 * @param size length of the cycle/chain
1797 * @param n_ops the number of ops representing this cycle/chain after lowering
1799 void stat_be_block_stat_permcycle(const char *class_name, ir_node *perm, ir_node *block,
1800 int is_chain, int size, int n_ops)
1802 if (! status->stat_options)
1807 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
1808 be_block_entry_t *block_ent;
1809 perm_class_entry_t *pc_ent;
1810 perm_stat_entry_t *ps_ent;
1812 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1813 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
1814 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
1817 ps_ent->n_copies += n_ops;
1818 stat_inc_int_distrib_tbl(ps_ent->chains, size);
1820 ps_ent->n_exchg += n_ops;
1821 stat_inc_int_distrib_tbl(ps_ent->cycles, size);
1825 } /* stat_be_block_stat_permcycle */
1827 /* Dumps a statistics snapshot. */
1828 void stat_dump_snapshot(const char *name, const char *phase)
1834 if (! status->stat_options)
1839 graph_entry_t *entry;
1840 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1843 * The constant counter is only global, so we clear it here.
1844 * Note that it does NOT contain the constants in DELETED
1845 * graphs due to this.
1847 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
1848 stat_const_clear(status);
1850 /* build the name */
1851 p = strrchr(name, '/');
1856 q = strrchr(name, '\\');
1858 /* NULL might be not the smallest pointer */
1859 if (q && (!p || q > p))
1867 if (l > sizeof(fname) - 1)
1868 l = sizeof(fname) - 1;
1870 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)) {
1885 if (entry->irg == NULL) {
1886 /* special entry for the global count */
1889 if (! entry->is_deleted) {
1890 /* the graph is still alive, count the nodes on it */
1891 update_graph_stat(global, entry);
1895 /* some calculations are dependent, we pushed them on the wait_q */
1896 while (! pdeq_empty(status->wait_q)) {
1897 entry = pdeq_getr(status->wait_q);
1899 update_graph_stat_2(global, entry);
1902 /* dump per graph */
1903 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
1904 if (entry->irg == NULL) {
1905 /* special entry for the global count */
1909 if (! entry->is_deleted || status->stat_options & FIRMSTAT_COUNT_DELETED) {
1910 stat_dump_graph(entry);
1911 stat_dump_registered(entry);
1914 if (! entry->is_deleted) {
1915 /* clear the counter that are not accumulated */
1916 graph_clear_entry(entry, 0);
1921 stat_dump_graph(global);
1923 /* dump the const info */
1924 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
1925 stat_dump_consts(&status->const_info);
1929 stat_finish_pattern_history(fname);
1931 /* clear the global counter here */
1933 node_entry_t *entry;
1935 for (entry = pset_first(global->opcode_hash); entry; entry = pset_next(global->opcode_hash)) {
1936 opcode_clear_entry(entry);
1938 /* clear all global counter */
1939 graph_clear_entry(global, 1);
1943 } /* stat_dump_snapshot */
1945 /** the hook entries for the Firm statistics module */
1946 static hook_entry_t stat_hooks[hook_last];
1948 /* initialize the statistics module. */
1949 void firm_init_stat(unsigned enable_options)
1951 #define X(a) a, sizeof(a)-1
1952 #define HOOK(h, fkt) \
1953 stat_hooks[h].hook._##h = fkt; register_hook(h, &stat_hooks[h])
1956 if (! (enable_options & FIRMSTAT_ENABLED))
1959 status = xmalloc(sizeof(*status));
1960 memset(status, 0, sizeof(*status));
1962 /* enable statistics */
1963 status->stat_options = enable_options & FIRMSTAT_ENABLED ? enable_options : 0;
1965 /* register all hooks */
1966 HOOK(hook_new_ir_op, stat_new_ir_op);
1967 HOOK(hook_free_ir_op, stat_free_ir_op);
1968 HOOK(hook_new_node, stat_new_node);
1969 HOOK(hook_turn_into_id, stat_turn_into_id);
1970 HOOK(hook_new_graph, stat_new_graph);
1971 HOOK(hook_free_graph, stat_free_graph);
1972 HOOK(hook_irg_walk, stat_irg_walk);
1973 HOOK(hook_irg_walk_blkwise, stat_irg_walk_blkwise);
1974 HOOK(hook_irg_block_walk, stat_irg_block_walk);
1975 HOOK(hook_merge_nodes, stat_merge_nodes);
1976 HOOK(hook_reassociate, stat_reassociate);
1977 HOOK(hook_lower, stat_lower);
1978 HOOK(hook_inline, stat_inline);
1979 HOOK(hook_tail_rec, stat_tail_rec);
1980 HOOK(hook_strength_red, stat_strength_red);
1981 HOOK(hook_dead_node_elim, stat_dead_node_elim);
1982 HOOK(hook_if_conversion, stat_if_conversion);
1983 HOOK(hook_func_call, stat_func_call);
1984 HOOK(hook_arch_dep_replace_mul_with_shifts, stat_arch_dep_replace_mul_with_shifts);
1985 HOOK(hook_arch_dep_replace_division_by_const, stat_arch_dep_replace_division_by_const);
1987 obstack_init(&status->cnts);
1988 obstack_init(&status->be_data);
1990 /* create the hash-tables */
1991 status->irg_hash = new_pset(graph_cmp, 8);
1992 status->ir_op_hash = new_pset(opcode_cmp_2, 1);
1994 /* create the wait queue */
1995 status->wait_q = new_pdeq();
1997 if (enable_options & FIRMSTAT_COUNT_STRONG_OP) {
1998 /* build the pseudo-ops */
2000 _op_Phi0.code = --num;
2001 _op_Phi0.name = new_id_from_chars(X("Phi0"));
2003 _op_PhiM.code = --num;
2004 _op_PhiM.name = new_id_from_chars(X("PhiM"));
2006 _op_ProjM.code = --num;
2007 _op_ProjM.name = new_id_from_chars(X("ProjM"));
2009 _op_MulC.code = --num;
2010 _op_MulC.name = new_id_from_chars(X("MulC"));
2012 _op_DivC.code = --num;
2013 _op_DivC.name = new_id_from_chars(X("DivC"));
2015 _op_ModC.code = --num;
2016 _op_ModC.name = new_id_from_chars(X("ModC"));
2018 _op_DivModC.code = --num;
2019 _op_DivModC.name = new_id_from_chars(X("DivModC"));
2021 status->op_Phi0 = &_op_Phi0;
2022 status->op_PhiM = &_op_PhiM;
2023 status->op_ProjM = &_op_ProjM;
2024 status->op_MulC = &_op_MulC;
2025 status->op_DivC = &_op_DivC;
2026 status->op_ModC = &_op_ModC;
2027 status->op_DivModC = &_op_DivModC;
2029 status->op_Phi0 = NULL;
2030 status->op_PhiM = NULL;
2031 status->op_ProjM = NULL;
2032 status->op_MulC = NULL;
2033 status->op_DivC = NULL;
2034 status->op_ModC = NULL;
2035 status->op_DivModC = NULL;
2038 /* for Florian: count the Sel depth */
2039 if (enable_options & FIRMSTAT_COUNT_SELS) {
2040 _op_SelSel.code = --num;
2041 _op_SelSel.name = new_id_from_chars(X("Sel(Sel)"));
2043 _op_SelSelSel.code = --num;
2044 _op_SelSelSel.name = new_id_from_chars(X("Sel(Sel(Sel))"));
2046 status->op_SelSel = &_op_SelSel;
2047 status->op_SelSelSel = &_op_SelSelSel;
2049 status->op_SelSel = NULL;
2050 status->op_SelSelSel = NULL;
2053 /* register the dumper */
2054 stat_register_dumper(&simple_dumper);
2056 if (enable_options & FIRMSTAT_CSV_OUTPUT)
2057 stat_register_dumper(&csv_dumper);
2059 /* initialize the pattern hash */
2060 stat_init_pattern_history(enable_options & FIRMSTAT_PATTERN_ENABLED);
2062 /* initialize the Const options */
2063 if (enable_options & FIRMSTAT_COUNT_CONSTS)
2064 stat_init_const_cnt(status);
2068 } /* firm_init_stat */
2071 * Frees all dumper structures.
2073 static void stat_term_dumper(void) {
2074 dumper_t *dumper, *next_dumper;
2076 for (dumper = status->dumper; dumper; /* iteration done in loop body */ ) {
2077 if (dumper->func_map)
2078 del_pset(dumper->func_map);
2080 next_dumper = dumper->next;
2082 dumper = next_dumper;
2084 } /* stat_term_dumper */
2087 /* Terminates the statistics module, frees all memory. */
2088 void stat_term(void) {
2089 if (status != (stat_info_t *)&status_disable) {
2090 obstack_free(&status->be_data, NULL);
2091 obstack_free(&status->cnts, NULL);
2096 status = (stat_info_t *)&status_disable;
2100 /* returns 1 if statistics were initialized, 0 otherwise */
2101 int stat_is_active(void) {
2102 return status != (stat_info_t *)&status_disable;
2103 } /* stat_is_active */
2107 /* initialize the statistics module. */
2108 void firm_init_stat(unsigned enable_options) {}
2110 /* Dumps a statistics snapshot */
2111 void stat_dump_snapshot(const char *name, const char *phase) {}
2113 /* terminates the statistics module, frees all memory */
2114 void stat_term(void);
2116 #endif /* FIRM_STATISTICS */