2 * Copyright (C) 1995-2010 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Statistics for Firm.
23 * @author Michael Beck
28 #ifdef FIRM_STATISTICS
37 #include "firmstat_t.h"
47 * need this to be static:
48 * Special pseudo Opcodes that we need to count some interesting cases
52 * The Phi0, a node that is created during SSA construction
54 static ir_op _op_Phi0;
56 /** The PhiM, just to count memory Phi's. */
57 static ir_op _op_PhiM;
59 /** The Mul by Const node. */
60 static ir_op _op_MulC;
62 /** The Div by Const node. */
63 static ir_op _op_DivC;
65 /** The Div by Const node. */
66 static ir_op _op_ModC;
68 /** The memory Proj node. */
69 static ir_op _op_ProjM;
72 static ir_op _op_SelSel;
74 /** A Sel of a Sel of a Sel */
75 static ir_op _op_SelSelSel;
77 /* ---------------------------------------------------------------------------------- */
79 /** Marks the begin of a statistic (hook) function. */
80 #define STAT_ENTER ++status->recursive
82 /** Marks the end of a statistic (hook) functions. */
83 #define STAT_LEAVE --status->recursive
85 /** Allows to enter a statistic function only when we are not already in a hook. */
86 #define STAT_ENTER_SINGLE do { if (status->recursive > 0) return; ++status->recursive; } while (0)
91 static const unsigned status_disable = 0;
92 static stat_info_t *status = (stat_info_t *)&status_disable;
95 * Compare two elements of the opcode hash.
97 static int opcode_cmp(const void *elt, const void *key)
99 const node_entry_t *e1 = (const node_entry_t*)elt;
100 const node_entry_t *e2 = (const node_entry_t*)key;
102 return e1->op->code - e2->op->code;
106 * Compare two elements of the graph hash.
108 static int graph_cmp(const void *elt, const void *key)
110 const graph_entry_t *e1 = (const graph_entry_t*)elt;
111 const graph_entry_t *e2 = (const graph_entry_t*)key;
113 return e1->irg != e2->irg;
117 * Compare two elements of the optimization hash.
119 static int opt_cmp(const void *elt, const void *key)
121 const opt_entry_t *e1 = (const opt_entry_t*)elt;
122 const opt_entry_t *e2 = (const opt_entry_t*)key;
124 return e1->op->code != e2->op->code;
128 * Compare two elements of the block/extbb hash.
130 static int block_cmp(const void *elt, const void *key)
132 const block_entry_t *e1 = (const block_entry_t*)elt;
133 const block_entry_t *e2 = (const block_entry_t*)key;
135 /* it's enough to compare the block number */
136 return e1->block_nr != e2->block_nr;
140 * Compare two elements of the be_block hash.
142 static int be_block_cmp(const void *elt, const void *key)
144 const be_block_entry_t *e1 = (const be_block_entry_t*)elt;
145 const be_block_entry_t *e2 = (const be_block_entry_t*)key;
147 return e1->block_nr != e2->block_nr;
151 * Compare two elements of reg pressure hash.
153 static int reg_pressure_cmp(const void *elt, const void *key)
155 const reg_pressure_entry_t *e1 = (const reg_pressure_entry_t*)elt;
156 const reg_pressure_entry_t *e2 = (const reg_pressure_entry_t*)key;
158 return e1->class_name != e2->class_name;
159 } /* reg_pressure_cmp */
162 * Compare two elements of the perm_stat hash.
164 static int perm_stat_cmp(const void *elt, const void *key)
166 const perm_stat_entry_t *e1 = (const perm_stat_entry_t*)elt;
167 const perm_stat_entry_t *e2 = (const perm_stat_entry_t*)key;
169 return e1->perm != e2->perm;
170 } /* perm_stat_cmp */
173 * Compare two elements of the perm_class hash.
175 static int perm_class_cmp(const void *elt, const void *key)
177 const perm_class_entry_t *e1 = (const perm_class_entry_t*)elt;
178 const perm_class_entry_t *e2 = (const perm_class_entry_t*)key;
180 return e1->class_name != e2->class_name;
181 } /* perm_class_cmp */
184 * Compare two elements of the ir_op hash.
186 static int opcode_cmp_2(const void *elt, const void *key)
188 const ir_op *e1 = (const ir_op*)elt;
189 const ir_op *e2 = (const ir_op*)key;
191 return e1->code != e2->code;
195 * Compare two elements of the address_mark set.
197 static int address_mark_cmp(const void *elt, const void *key, size_t size)
199 const address_mark_entry_t *e1 = (const address_mark_entry_t*)elt;
200 const address_mark_entry_t *e2 = (const address_mark_entry_t*)key;
203 /* compare only the nodes, the rest is used as data container */
204 return e1->node != e2->node;
205 } /* address_mark_cmp */
208 * Clear all counter in a node_entry_t.
210 static void opcode_clear_entry(node_entry_t *elem)
212 cnt_clr(&elem->cnt_alive);
213 cnt_clr(&elem->new_node);
214 cnt_clr(&elem->into_Id);
215 cnt_clr(&elem->normalized);
216 } /* opcode_clear_entry */
219 * Returns the associates node_entry_t for an ir_op (and allocates
220 * one if not yet available).
222 * @param op the IR operation
223 * @param hmap a hash map containing ir_op* -> node_entry_t*
225 static node_entry_t *opcode_get_entry(const ir_op *op, hmap_node_entry_t *hmap)
232 elem = (node_entry_t*)pset_find(hmap, &key, op->code);
236 elem = OALLOCZ(&status->cnts, node_entry_t);
239 opcode_clear_entry(elem);
243 return (node_entry_t*)pset_insert(hmap, elem, op->code);
244 } /* opcode_get_entry */
247 * Returns the associates ir_op for an opcode
249 * @param code the IR opcode
250 * @param hmap the hash map containing opcode -> ir_op*
252 static ir_op *opcode_find_entry(ir_opcode code, hmap_ir_op *hmap)
257 return (ir_op*)pset_find(hmap, &key, code);
258 } /* opcode_find_entry */
261 * Clears all counter in a graph_entry_t.
263 * @param elem the graph entry
264 * @param all if non-zero, clears all counters, else leave accumulated ones
266 static void graph_clear_entry(graph_entry_t *elem, int all)
270 /* clear accumulated / non-accumulated counter */
271 for (i = all ? 0 : _gcnt_non_acc; i < _gcnt_last; ++i) {
272 cnt_clr(&elem->cnt[i]);
275 if (elem->block_hash) {
276 del_pset(elem->block_hash);
277 elem->block_hash = NULL;
280 if (elem->extbb_hash) {
281 del_pset(elem->extbb_hash);
282 elem->extbb_hash = NULL;
285 obstack_free(&elem->recalc_cnts, NULL);
286 obstack_init(&elem->recalc_cnts);
287 } /* graph_clear_entry */
290 * Returns the associated graph_entry_t for an IR graph.
292 * @param irg the IR graph, NULL for the global counter
293 * @param hmap the hash map containing ir_graph* -> graph_entry_t*
295 static graph_entry_t *graph_get_entry(ir_graph *irg, hmap_graph_entry_t *hmap)
303 elem = (graph_entry_t*)pset_find(hmap, &key, HASH_PTR(irg));
306 /* create hash map backend block information */
307 if (! elem->be_block_hash)
308 elem->be_block_hash = new_pset(be_block_cmp, 5);
313 /* allocate a new one */
314 elem = OALLOCZ(&status->cnts, graph_entry_t);
315 obstack_init(&elem->recalc_cnts);
318 graph_clear_entry(elem, 1);
320 /* new hash table for opcodes here */
321 elem->opcode_hash = new_pset(opcode_cmp, 5);
322 elem->address_mark = new_set(address_mark_cmp, 5);
325 /* these hash tables are created on demand */
326 elem->block_hash = NULL;
327 elem->extbb_hash = NULL;
329 for (i = 0; i < sizeof(elem->opt_hash)/sizeof(elem->opt_hash[0]); ++i)
330 elem->opt_hash[i] = new_pset(opt_cmp, 4);
332 return (graph_entry_t*)pset_insert(hmap, elem, HASH_PTR(irg));
333 } /* graph_get_entry */
336 * Clear all counter in an opt_entry_t.
338 static void opt_clear_entry(opt_entry_t *elem)
340 cnt_clr(&elem->count);
341 } /* opt_clear_entry */
344 * Returns the associated opt_entry_t for an IR operation.
346 * @param op the IR operation
347 * @param hmap the hash map containing ir_op* -> opt_entry_t*
349 static opt_entry_t *opt_get_entry(const ir_op *op, hmap_opt_entry_t *hmap)
356 elem = (opt_entry_t*)pset_find(hmap, &key, op->code);
360 elem = OALLOCZ(&status->cnts, opt_entry_t);
362 /* clear new counter */
363 opt_clear_entry(elem);
367 return (opt_entry_t*)pset_insert(hmap, elem, op->code);
368 } /* opt_get_entry */
371 * clears all counter in a block_entry_t
373 static void block_clear_entry(block_entry_t *elem)
377 for (i = 0; i < _bcnt_last; ++i)
378 cnt_clr(&elem->cnt[i]);
379 } /* block_clear_entry */
382 * Returns the associated block_entry_t for an block.
384 * @param block_nr an IR block number
385 * @param hmap a hash map containing long -> block_entry_t
387 static block_entry_t *block_get_entry(struct obstack *obst, long block_nr, hmap_block_entry_t *hmap)
392 key.block_nr = block_nr;
394 elem = (block_entry_t*)pset_find(hmap, &key, block_nr);
398 elem = OALLOCZ(obst, block_entry_t);
400 /* clear new counter */
401 block_clear_entry(elem);
403 elem->block_nr = block_nr;
405 return (block_entry_t*)pset_insert(hmap, elem, block_nr);
406 } /* block_get_entry */
409 * Clear all sets in be_block_entry_t.
411 static void be_block_clear_entry(be_block_entry_t *elem)
413 if (elem->reg_pressure)
414 del_pset(elem->reg_pressure);
416 if (elem->sched_ready)
417 stat_delete_distrib_tbl(elem->sched_ready);
419 if (elem->perm_class_stat)
420 del_pset(elem->perm_class_stat);
422 elem->reg_pressure = new_pset(reg_pressure_cmp, 5);
423 elem->sched_ready = stat_new_int_distrib_tbl();
424 elem->perm_class_stat = new_pset(perm_class_cmp, 5);
425 } /* be_block_clear_entry */
428 * Returns the associated be_block_entry_t for an block.
430 * @param block_nr an IR block number
431 * @param hmap a hash map containing long -> be_block_entry_t
433 static be_block_entry_t *be_block_get_entry(struct obstack *obst, long block_nr, hmap_be_block_entry_t *hmap)
435 be_block_entry_t key;
436 be_block_entry_t *elem;
438 key.block_nr = block_nr;
440 elem = (be_block_entry_t*)pset_find(hmap, &key, block_nr);
444 elem = OALLOCZ(obst, be_block_entry_t);
446 /* clear new counter */
447 be_block_clear_entry(elem);
449 elem->block_nr = block_nr;
451 return (be_block_entry_t*)pset_insert(hmap, elem, block_nr);
452 } /* be_block_get_entry */
455 * clears all sets in perm_class_entry_t
457 static void perm_class_clear_entry(perm_class_entry_t *elem)
460 del_pset(elem->perm_stat);
462 elem->perm_stat = new_pset(perm_stat_cmp, 5);
463 } /* perm_class_clear_entry */
466 * Returns the associated perm_class entry for a register class.
468 * @param class_name the register class name
469 * @param hmap a hash map containing class_name -> perm_class_entry_t
471 static perm_class_entry_t *perm_class_get_entry(struct obstack *obst, const char *class_name,
472 hmap_perm_class_entry_t *hmap)
474 perm_class_entry_t key;
475 perm_class_entry_t *elem;
477 key.class_name = class_name;
479 elem = (perm_class_entry_t*)pset_find(hmap, &key, HASH_PTR(class_name));
483 elem = OALLOCZ(obst, perm_class_entry_t);
485 /* clear new counter */
486 perm_class_clear_entry(elem);
488 elem->class_name = class_name;
490 return (perm_class_entry_t*)pset_insert(hmap, elem, HASH_PTR(class_name));
491 } /* perm_class_get_entry */
494 * clears all sets in perm_stat_entry_t
496 static void perm_stat_clear_entry(perm_stat_entry_t *elem)
499 stat_delete_distrib_tbl(elem->chains);
502 stat_delete_distrib_tbl(elem->cycles);
504 elem->chains = stat_new_int_distrib_tbl();
505 elem->cycles = stat_new_int_distrib_tbl();
506 } /* perm_stat_clear_entry */
509 * Returns the associated perm_stat entry for a perm.
511 * @param perm the perm node
512 * @param hmap a hash map containing perm -> perm_stat_entry_t
514 static perm_stat_entry_t *perm_stat_get_entry(struct obstack *obst, ir_node *perm, hmap_perm_stat_entry_t *hmap)
516 perm_stat_entry_t key;
517 perm_stat_entry_t *elem;
521 elem = (perm_stat_entry_t*)pset_find(hmap, &key, HASH_PTR(perm));
525 elem = OALLOCZ(obst, perm_stat_entry_t);
527 /* clear new counter */
528 perm_stat_clear_entry(elem);
532 return (perm_stat_entry_t*)pset_insert(hmap, elem, HASH_PTR(perm));
533 } /* perm_stat_get_entry */
536 * Clear optimizations counter,
538 static void clear_optimization_counter(void)
541 for (i = 0; i < FS_OPT_MAX; ++i)
542 cnt_clr(&status->num_opts[i]);
546 * Returns the ir_op for an IR-node,
547 * handles special cases and return pseudo op codes.
549 * @param none an IR node
551 static ir_op *stat_get_irn_op(ir_node *node)
553 ir_op *op = get_irn_op(node);
554 unsigned opc = op->code;
558 if (get_irn_arity(node) == 0) {
559 /* special case, a Phi0 node, count on extra counter */
560 op = status->op_Phi0 ? status->op_Phi0 : op;
561 } else if (get_irn_mode(node) == mode_M) {
562 /* special case, a Memory Phi node, count on extra counter */
563 op = status->op_PhiM ? status->op_PhiM : op;
567 if (get_irn_mode(node) == mode_M) {
568 /* special case, a Memory Proj node, count on extra counter */
569 op = status->op_ProjM ? status->op_ProjM : op;
573 if (is_Const(get_Mul_left(node)) || is_Const(get_Mul_right(node))) {
574 /* special case, a Multiply by a const, count on extra counter */
575 op = status->op_MulC ? status->op_MulC : op;
579 if (is_Const(get_Div_right(node))) {
580 /* special case, a division by a const, count on extra counter */
581 op = status->op_DivC ? status->op_DivC : op;
585 if (is_Const(get_Mod_right(node))) {
586 /* special case, a module by a const, count on extra counter */
587 op = status->op_ModC ? status->op_ModC : op;
591 if (is_Sel(get_Sel_ptr(node))) {
592 /* special case, a Sel of a Sel, count on extra counter */
593 op = status->op_SelSel ? status->op_SelSel : op;
594 if (is_Sel(get_Sel_ptr(get_Sel_ptr(node)))) {
595 /* special case, a Sel of a Sel of a Sel, count on extra counter */
596 op = status->op_SelSelSel ? status->op_SelSelSel : op;
605 } /* stat_get_irn_op */
608 * update the block counter
610 static void undate_block_info(ir_node *node, graph_entry_t *graph)
612 ir_op *op = get_irn_op(node);
614 block_entry_t *b_entry;
617 /* check for block */
618 if (op == op_Block) {
619 arity = get_irn_arity(node);
620 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(node), graph->block_hash);
621 /* mark start end block to allow to filter them out */
622 if (node == get_irg_start_block(graph->irg))
623 b_entry->is_start = 1;
624 else if (node == get_irg_end_block(graph->irg))
627 /* count all incoming edges */
628 for (i = 0; i < arity; ++i) {
629 ir_node *pred = get_irn_n(node, i);
630 ir_node *other_block = get_nodes_block(pred);
631 block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
633 cnt_inc(&b_entry->cnt[bcnt_in_edges]); /* an edge coming from another block */
634 cnt_inc(&b_entry_other->cnt[bcnt_out_edges]);
639 block = get_nodes_block(node);
640 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(block), graph->block_hash);
642 if (op == op_Phi && mode_is_datab(get_irn_mode(node))) {
643 /* count data Phi per block */
644 cnt_inc(&b_entry->cnt[bcnt_phi_data]);
647 /* we have a new node in our block */
648 cnt_inc(&b_entry->cnt[bcnt_nodes]);
650 /* don't count keep-alive edges */
654 arity = get_irn_arity(node);
656 for (i = 0; i < arity; ++i) {
657 ir_node *pred = get_irn_n(node, i);
658 ir_node *other_block;
660 other_block = get_nodes_block(pred);
662 if (other_block == block)
663 cnt_inc(&b_entry->cnt[bcnt_edges]); /* a in block edge */
665 block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
667 cnt_inc(&b_entry->cnt[bcnt_in_edges]); /* an edge coming from another block */
668 cnt_inc(&b_entry_other->cnt[bcnt_out_edges]);
671 } /* undate_block_info */
674 * Update the extended block counter.
676 static void update_extbb_info(ir_node *node, graph_entry_t *graph)
678 ir_op *op = get_irn_op(node);
680 extbb_entry_t *eb_entry;
683 /* check for block */
684 if (op == op_Block) {
685 extbb = get_nodes_extbb(node);
686 arity = get_irn_arity(node);
687 eb_entry = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(extbb), graph->extbb_hash);
689 /* count all incoming edges */
690 for (i = 0; i < arity; ++i) {
691 ir_node *pred = get_irn_n(node, i);
692 ir_extblk *other_extbb = get_nodes_extbb(pred);
694 if (extbb != other_extbb) {
695 extbb_entry_t *eb_entry_other = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(other_extbb), graph->extbb_hash);
697 cnt_inc(&eb_entry->cnt[bcnt_in_edges]); /* an edge coming from another extbb */
698 cnt_inc(&eb_entry_other->cnt[bcnt_out_edges]);
704 extbb = get_nodes_extbb(node);
705 eb_entry = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(extbb), graph->extbb_hash);
707 if (op == op_Phi && mode_is_datab(get_irn_mode(node))) {
708 /* count data Phi per extbb */
709 cnt_inc(&eb_entry->cnt[bcnt_phi_data]);
712 /* we have a new node in our block */
713 cnt_inc(&eb_entry->cnt[bcnt_nodes]);
715 /* don't count keep-alive edges */
719 arity = get_irn_arity(node);
721 for (i = 0; i < arity; ++i) {
722 ir_node *pred = get_irn_n(node, i);
723 ir_extblk *other_extbb = get_nodes_extbb(pred);
725 if (other_extbb == extbb)
726 cnt_inc(&eb_entry->cnt[bcnt_edges]); /* a in extbb edge */
728 extbb_entry_t *eb_entry_other = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(other_extbb), graph->extbb_hash);
730 cnt_inc(&eb_entry->cnt[bcnt_in_edges]); /* an edge coming from another extbb */
731 cnt_inc(&eb_entry_other->cnt[bcnt_out_edges]);
734 } /* update_extbb_info */
737 * Calculates how many arguments of the call are const, updates
738 * param distribution.
740 static void analyse_params_of_Call(graph_entry_t *graph, ir_node *call)
742 int i, num_const_args = 0, num_local_adr = 0;
743 int n = get_Call_n_params(call);
745 for (i = 0; i < n; ++i) {
746 ir_node *param = get_Call_param(call, i);
748 if (is_irn_constlike(param))
750 else if (is_Sel(param)) {
751 ir_node *base = param;
754 base = get_Sel_ptr(base);
755 } while (is_Sel(base));
757 if (base == get_irg_frame(current_ir_graph))
763 if (num_const_args > 0)
764 cnt_inc(&graph->cnt[gcnt_call_with_cnst_arg]);
765 if (num_const_args == n)
766 cnt_inc(&graph->cnt[gcnt_call_with_all_cnst_arg]);
767 if (num_local_adr > 0)
768 cnt_inc(&graph->cnt[gcnt_call_with_local_adr]);
770 stat_inc_int_distrib_tbl(status->dist_param_cnt, n);
771 } /* analyse_params_of_Call */
774 * Update info on calls.
776 * @param call The call
777 * @param graph The graph entry containing the call
779 static void stat_update_call(ir_node *call, graph_entry_t *graph)
781 ir_node *block = get_nodes_block(call);
782 ir_node *ptr = get_Call_ptr(call);
783 ir_entity *ent = NULL;
784 ir_graph *callee = NULL;
787 * If the block is bad, the whole subgraph will collapse later
788 * so do not count this call.
789 * This happens in dead code.
794 cnt_inc(&graph->cnt[gcnt_all_calls]);
796 /* found a call, this function is not a leaf */
799 if (is_SymConst(ptr)) {
800 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
801 /* ok, we seems to know the entity */
802 ent = get_SymConst_entity(ptr);
803 callee = get_entity_irg(ent);
805 /* it is recursive, if it calls at least once */
806 if (callee == graph->irg)
807 graph->is_recursive = 1;
809 cnt_inc(&graph->cnt[gcnt_external_calls]);
812 /* indirect call, be could not predict */
813 cnt_inc(&graph->cnt[gcnt_indirect_calls]);
815 /* NOT a leaf call */
816 graph->is_leaf_call = LCS_NON_LEAF_CALL;
819 /* check, if it's a chain-call: Then, the call-block
820 * must dominate the end block. */
822 ir_node *curr = get_irg_end_block(graph->irg);
823 int depth = get_Block_dom_depth(block);
825 for (; curr != block && get_Block_dom_depth(curr) > depth;) {
826 curr = get_Block_idom(curr);
828 if (! curr || !is_Block(curr))
833 graph->is_chain_call = 0;
836 /* check, if the callee is a leaf */
838 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
840 if (called->is_analyzed) {
841 if (! called->is_leaf)
842 graph->is_leaf_call = LCS_NON_LEAF_CALL;
846 analyse_params_of_Call(graph, call);
847 } /* stat_update_call */
850 * Update info on calls for graphs on the wait queue.
852 static void stat_update_call_2(ir_node *call, graph_entry_t *graph)
854 ir_node *block = get_nodes_block(call);
855 ir_node *ptr = get_Call_ptr(call);
856 ir_entity *ent = NULL;
857 ir_graph *callee = NULL;
860 * If the block is bad, the whole subgraph will collapse later
861 * so do not count this call.
862 * This happens in dead code.
867 if (is_SymConst(ptr)) {
868 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
869 /* ok, we seems to know the entity */
870 ent = get_SymConst_entity(ptr);
871 callee = get_entity_irg(ent);
875 /* check, if the callee is a leaf */
877 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
879 assert(called->is_analyzed);
881 if (! called->is_leaf)
882 graph->is_leaf_call = LCS_NON_LEAF_CALL;
884 graph->is_leaf_call = LCS_NON_LEAF_CALL;
885 } /* stat_update_call_2 */
888 * Find the base address and entity of an Sel node.
890 * @param sel the node
892 * @return the base address.
894 static ir_node *find_base_adr(ir_node *sel)
896 ir_node *ptr = get_Sel_ptr(sel);
898 while (is_Sel(ptr)) {
900 ptr = get_Sel_ptr(sel);
903 } /* find_base_adr */
906 * Update info on Load/Store address statistics.
908 static void stat_update_address(ir_node *node, graph_entry_t *graph)
910 unsigned opc = get_irn_opcode(node);
916 /* a global address */
917 cnt_inc(&graph->cnt[gcnt_global_adr]);
920 base = find_base_adr(node);
921 irg = current_ir_graph;
922 if (base == get_irg_tls(irg)) {
923 /* a TLS variable, like a global. */
924 cnt_inc(&graph->cnt[gcnt_global_adr]);
925 } else if (base == get_irg_frame(irg)) {
926 /* a local Variable. */
927 cnt_inc(&graph->cnt[gcnt_local_adr]);
930 if (is_Proj(base) && skip_Proj(get_Proj_pred(base)) == get_irg_start(irg)) {
931 /* pointer access through parameter, check for THIS */
932 ir_entity *ent = get_irg_entity(irg);
935 ir_type *ent_tp = get_entity_type(ent);
937 if (get_method_calling_convention(ent_tp) & cc_this_call) {
938 if (get_Proj_proj(base) == 0) {
940 cnt_inc(&graph->cnt[gcnt_this_adr]);
945 /* other parameter */
946 cnt_inc(&graph->cnt[gcnt_param_adr]);
949 /* unknown Pointer access */
950 cnt_inc(&graph->cnt[gcnt_other_adr]);
956 } /* stat_update_address */
959 * Walker for reachable nodes count.
961 static void update_node_stat(ir_node *node, void *env)
963 graph_entry_t *graph = (graph_entry_t*)env;
966 ir_op *op = stat_get_irn_op(node);
967 int i, arity = get_irn_arity(node);
969 entry = opcode_get_entry(op, graph->opcode_hash);
971 cnt_inc(&entry->cnt_alive);
972 cnt_add_i(&graph->cnt[gcnt_edges], arity);
974 /* count block edges */
975 undate_block_info(node, graph);
977 /* count extended block edges */
978 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
979 if (graph->irg != get_const_code_irg())
980 update_extbb_info(node, graph);
983 /* handle statistics for special node types */
987 /* check for properties that depends on calls like recursion/leaf/indirect call */
988 stat_update_call(node, graph);
991 /* check address properties */
992 stat_update_address(get_Load_ptr(node), graph);
995 /* check address properties */
996 stat_update_address(get_Store_ptr(node), graph);
999 /* check for non-strict Phi nodes */
1000 for (i = arity - 1; i >= 0; --i) {
1001 ir_node *pred = get_Phi_pred(node, i);
1002 if (is_Unknown(pred)) {
1003 /* found an Unknown predecessor, graph is not strict */
1004 graph->is_strict = 0;
1012 /* we want to count the constant IN nodes, not the CSE'ed constant's itself */
1013 if (status->stat_options & FIRMSTAT_COUNT_CONSTS) {
1016 for (i = get_irn_arity(node) - 1; i >= 0; --i) {
1017 ir_node *pred = get_irn_n(node, i);
1019 if (is_Const(pred)) {
1020 /* check properties of constants */
1021 stat_update_const(status, pred, graph);
1025 } /* update_node_stat */
1028 * Walker for reachable nodes count for graphs on the wait_q.
1030 static void update_node_stat_2(ir_node *node, void *env)
1032 graph_entry_t *graph = (graph_entry_t*)env;
1034 /* check for properties that depends on calls like recursion/leaf/indirect call */
1036 stat_update_call_2(node, graph);
1037 } /* update_node_stat_2 */
1040 * Get the current address mark.
1042 static unsigned get_adr_mark(graph_entry_t *graph, ir_node *node)
1044 address_mark_entry_t *value = (address_mark_entry_t*)set_find(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
1046 return value ? value->mark : 0;
1047 } /* get_adr_mark */
1050 * Set the current address mark.
1052 static void set_adr_mark(graph_entry_t *graph, ir_node *node, unsigned val)
1054 address_mark_entry_t *value = (address_mark_entry_t*)set_insert(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
1057 } /* set_adr_mark */
1059 #undef DUMP_ADR_MODE
1061 #ifdef DUMP_ADR_MODE
1063 * a vcg attribute hook: Color a node with a different color if
1064 * it's identified as a part of an address expression or at least referenced
1065 * by an address expression.
1067 static int stat_adr_mark_hook(FILE *F, ir_node *node, ir_node *local)
1069 ir_node *n = local ? local : node;
1070 ir_graph *irg = get_irn_irg(n);
1071 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1072 unsigned mark = get_adr_mark(graph, n);
1074 if (mark & MARK_ADDRESS_CALC)
1075 fprintf(F, "color: purple");
1076 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1077 fprintf(F, "color: pink");
1078 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1079 fprintf(F, "color: lightblue");
1083 /* I know the color! */
1085 } /* stat_adr_mark_hook */
1086 #endif /* DUMP_ADR_MODE */
1089 * Return the "operational" mode of a Firm node.
1091 static ir_mode *get_irn_op_mode(ir_node *node)
1093 switch (get_irn_opcode(node)) {
1095 return get_Load_mode(node);
1097 return get_irn_mode(get_Store_value(node));
1099 return get_irn_mode(get_Div_left(node));
1101 return get_irn_mode(get_Mod_left(node));
1103 /* Cmp is no address calculation, or is it? */
1105 return get_irn_mode(node);
1107 } /* get_irn_op_mode */
1110 * Post-walker that marks every node that is an address calculation.
1112 * Users of a node must be visited first. We ensure this by
1113 * calling it in the post of an outs walk. This should work even in cycles,
1114 * while the normal pre-walk will not.
1116 static void mark_address_calc(ir_node *node, void *env)
1118 graph_entry_t *graph = (graph_entry_t*)env;
1119 ir_mode *mode = get_irn_op_mode(node);
1121 unsigned mark_preds = MARK_REF_NON_ADR;
1123 if (! mode_is_data(mode))
1126 if (mode_is_reference(mode)) {
1127 /* a reference is calculated here, we are sure */
1128 set_adr_mark(graph, node, MARK_ADDRESS_CALC);
1130 mark_preds = MARK_REF_ADR;
1132 unsigned mark = get_adr_mark(graph, node);
1134 if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR) {
1136 * this node has no reference mode, but is only
1137 * referenced by address calculations
1139 mark_preds = MARK_REF_ADR;
1143 /* mark all predecessors */
1144 for (i = 0, n = get_irn_arity(node); i < n; ++i) {
1145 ir_node *pred = get_irn_n(node, i);
1147 mode = get_irn_op_mode(pred);
1148 if (! mode_is_data(mode))
1151 set_adr_mark(graph, pred, get_adr_mark(graph, pred) | mark_preds);
1153 } /* mark_address_calc */
1156 * Post-walker that marks every node that is an address calculation.
1158 * Users of a node must be visited first. We ensure this by
1159 * calling it in the post of an outs walk. This should work even in cycles,
1160 * while the normal pre-walk will not.
1162 static void count_adr_ops(ir_node *node, void *env)
1164 graph_entry_t *graph = (graph_entry_t*)env;
1165 unsigned mark = get_adr_mark(graph, node);
1167 if (mark & MARK_ADDRESS_CALC)
1168 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1169 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1170 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1171 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1172 cnt_inc(&graph->cnt[gcnt_all_adr_ops]);
1173 } /* count_adr_ops */
1176 * Called for every graph when the graph is either deleted or stat_dump_snapshot()
1177 * is called, must recalculate all statistic info.
1179 * @param global The global entry
1180 * @param graph The current entry
1182 static void update_graph_stat(graph_entry_t *global, graph_entry_t *graph)
1184 node_entry_t *entry;
1187 /* clear first the alive counter in the graph */
1188 foreach_pset(graph->opcode_hash, node_entry_t*, entry) {
1189 cnt_clr(&entry->cnt_alive);
1190 } /* foreach_pset */
1192 /* set pessimistic values */
1194 graph->is_leaf_call = LCS_UNKNOWN;
1195 graph->is_recursive = 0;
1196 graph->is_chain_call = 1;
1197 graph->is_strict = 1;
1199 /* create new block counter */
1200 graph->block_hash = new_pset(block_cmp, 5);
1202 /* we need dominator info */
1203 if (graph->irg != get_const_code_irg()) {
1204 assure_doms(graph->irg);
1206 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
1207 /* we need extended basic blocks */
1208 compute_extbb(graph->irg);
1210 /* create new extbb counter */
1211 graph->extbb_hash = new_pset(block_cmp, 5);
1215 /* count the nodes in the graph */
1216 irg_walk_graph(graph->irg, update_node_stat, NULL, graph);
1219 /* Uncomment this code if chain-call means call exact one. */
1220 entry = opcode_get_entry(op_Call, graph->opcode_hash);
1222 /* check if we have more than 1 call */
1223 if (cnt_gt(entry->cnt_alive, 1))
1224 graph->is_chain_call = 0;
1227 /* recursive functions are never chain calls, leafs don't have calls */
1228 if (graph->is_recursive || graph->is_leaf)
1229 graph->is_chain_call = 0;
1231 /* assume we walk every graph only ONCE, we could sum here the global count */
1232 foreach_pset(graph->opcode_hash, node_entry_t*, entry) {
1233 node_entry_t *g_entry = opcode_get_entry(entry->op, global->opcode_hash);
1235 /* update the node counter */
1236 cnt_add(&g_entry->cnt_alive, &entry->cnt_alive);
1237 } /* foreach_pset */
1239 /* count the number of address calculation */
1240 if (graph->irg != get_const_code_irg()) {
1241 ir_graph *rem = current_ir_graph;
1243 assure_irg_outs(graph->irg);
1245 /* Must be done an the outs graph */
1246 current_ir_graph = graph->irg;
1247 irg_out_walk(get_irg_start(graph->irg), NULL, mark_address_calc, graph);
1248 current_ir_graph = rem;
1250 #ifdef DUMP_ADR_MODE
1251 /* register the vcg hook and dump the graph for test */
1252 set_dump_node_vcgattr_hook(stat_adr_mark_hook);
1253 dump_ir_block_graph(graph->irg, "-adr");
1254 set_dump_node_vcgattr_hook(NULL);
1255 #endif /* DUMP_ADR_MODE */
1257 irg_walk_graph(graph->irg, NULL, count_adr_ops, graph);
1260 /* count the DAG's */
1261 if (status->stat_options & FIRMSTAT_COUNT_DAG)
1262 count_dags_in_graph(global, graph);
1264 /* calculate the patterns of this graph */
1265 stat_calc_pattern_history(graph->irg);
1267 /* leaf function did not call others */
1269 graph->is_leaf_call = LCS_NON_LEAF_CALL;
1270 else if (graph->is_leaf_call == LCS_UNKNOWN) {
1271 /* we still don't know if this graph calls leaf-functions, so enqueue */
1272 pdeq_putl(status->wait_q, graph);
1275 /* we have analyzed this graph */
1276 graph->is_analyzed = 1;
1278 /* accumulate all counter's */
1279 for (i = 0; i < _gcnt_last; ++i)
1280 cnt_add(&global->cnt[i], &graph->cnt[i]);
1281 } /* update_graph_stat */
1284 * Called for every graph that was on the wait_q in stat_dump_snapshot()
1285 * must finish all statistic info calculations.
1287 * @param global The global entry
1288 * @param graph The current entry
1290 static void update_graph_stat_2(graph_entry_t *global, graph_entry_t *graph)
1293 if (graph->is_deleted) {
1294 /* deleted, ignore */
1299 /* count the nodes in the graph */
1300 irg_walk_graph(graph->irg, update_node_stat_2, NULL, graph);
1302 if (graph->is_leaf_call == LCS_UNKNOWN)
1303 graph->is_leaf_call = LCS_LEAF_CALL;
1305 } /* update_graph_stat_2 */
1308 * Register a dumper.
1310 static void stat_register_dumper(const dumper_t *dumper)
1312 dumper_t *p = XMALLOC(dumper_t);
1314 memcpy(p, dumper, sizeof(*p));
1316 p->next = status->dumper;
1320 /* FIXME: memory leak */
1321 } /* stat_register_dumper */
1324 * Dumps the statistics of an IR graph.
1326 static void stat_dump_graph(graph_entry_t *entry)
1330 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1331 if (dumper->dump_graph)
1332 dumper->dump_graph(dumper, entry);
1334 } /* stat_dump_graph */
1337 * Calls all registered dumper functions.
1339 static void stat_dump_registered(graph_entry_t *entry)
1343 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1344 if (dumper->func_map) {
1345 dump_graph_FUNC func;
1347 foreach_pset(dumper->func_map, dump_graph_FUNC, func)
1348 func(dumper, entry);
1351 } /* stat_dump_registered */
1354 * Dumps a constant table.
1356 static void stat_dump_consts(const constant_info_t *tbl)
1360 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1361 if (dumper->dump_const_tbl)
1362 dumper->dump_const_tbl(dumper, tbl);
1364 } /* stat_dump_consts */
1367 * Dumps the parameter distribution
1369 static void stat_dump_param_tbl(const distrib_tbl_t *tbl, graph_entry_t *global)
1373 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1374 if (dumper->dump_param_tbl)
1375 dumper->dump_param_tbl(dumper, tbl, global);
1377 } /* stat_dump_param_tbl */
1380 * Dumps the optimization counter
1382 static void stat_dump_opt_cnt(const counter_t *tbl, unsigned len)
1386 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1387 if (dumper->dump_opt_cnt)
1388 dumper->dump_opt_cnt(dumper, tbl, len);
1390 } /* stat_dump_opt_cnt */
1393 * Initialize the dumper.
1395 static void stat_dump_init(const char *name)
1399 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1401 dumper->init(dumper, name);
1403 } /* stat_dump_init */
1406 * Finish the dumper.
1408 static void stat_dump_finish(void)
1412 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1414 dumper->finish(dumper);
1416 } /* stat_dump_finish */
1419 * Register an additional function for all dumper.
1421 void stat_register_dumper_func(dump_graph_FUNC func)
1425 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1426 if (! dumper->func_map)
1427 dumper->func_map = pset_new_ptr(3);
1428 pset_insert_ptr(dumper->func_map, (void*)func);
1430 } /* stat_register_dumper_func */
1432 /* ---------------------------------------------------------------------- */
1435 * Helper: get an ir_op from an opcode.
1437 ir_op *stat_get_op_from_opcode(ir_opcode code)
1439 return opcode_find_entry(code, status->ir_op_hash);
1440 } /* stat_get_op_from_opcode */
1443 * Hook: A new IR op is registered.
1445 * @param ctx the hook context
1446 * @param op the new IR opcode that was created.
1448 static void stat_new_ir_op(void *ctx, ir_op *op)
1451 if (! status->stat_options)
1456 graph_entry_t *graph = graph_get_entry(NULL, status->irg_hash);
1458 /* execute for side effect :-) */
1459 (void)opcode_get_entry(op, graph->opcode_hash);
1461 pset_insert(status->ir_op_hash, op, op->code);
1464 } /* stat_new_ir_op */
1467 * Hook: An IR op is freed.
1469 * @param ctx the hook context
1470 * @param op the IR opcode that is freed
1472 static void stat_free_ir_op(void *ctx, ir_op *op)
1476 if (! status->stat_options)
1483 } /* stat_free_ir_op */
1486 * Hook: A new node is created.
1488 * @param ctx the hook context
1489 * @param irg the IR graph on which the node is created
1490 * @param node the new IR node that was created
1492 static void stat_new_node(void *ctx, ir_graph *irg, ir_node *node)
1496 if (! status->stat_options)
1499 /* do NOT count during dead node elimination */
1500 if (status->in_dead_node_elim)
1505 node_entry_t *entry;
1506 graph_entry_t *graph;
1507 ir_op *op = stat_get_irn_op(node);
1509 /* increase global value */
1510 graph = graph_get_entry(NULL, status->irg_hash);
1511 entry = opcode_get_entry(op, graph->opcode_hash);
1512 cnt_inc(&entry->new_node);
1514 /* increase local value */
1515 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1516 entry = opcode_get_entry(op, graph->opcode_hash);
1517 cnt_inc(&entry->new_node);
1520 } /* stat_new_node */
1523 * Hook: A node is changed into a Id node
1525 * @param ctx the hook context
1526 * @param node the IR node that will be turned into an ID
1528 static void stat_turn_into_id(void *ctx, ir_node *node)
1531 if (! status->stat_options)
1536 node_entry_t *entry;
1537 graph_entry_t *graph;
1538 ir_op *op = stat_get_irn_op(node);
1540 /* increase global value */
1541 graph = graph_get_entry(NULL, status->irg_hash);
1542 entry = opcode_get_entry(op, graph->opcode_hash);
1543 cnt_inc(&entry->into_Id);
1545 /* increase local value */
1546 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1547 entry = opcode_get_entry(op, graph->opcode_hash);
1548 cnt_inc(&entry->into_Id);
1551 } /* stat_turn_into_id */
1554 * Hook: A node is normalized
1556 * @param ctx the hook context
1557 * @param node the IR node that was normalized
1559 static void stat_normalize(void *ctx, ir_node *node)
1562 if (! status->stat_options)
1567 node_entry_t *entry;
1568 graph_entry_t *graph;
1569 ir_op *op = stat_get_irn_op(node);
1571 /* increase global value */
1572 graph = graph_get_entry(NULL, status->irg_hash);
1573 entry = opcode_get_entry(op, graph->opcode_hash);
1574 cnt_inc(&entry->normalized);
1576 /* increase local value */
1577 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1578 entry = opcode_get_entry(op, graph->opcode_hash);
1579 cnt_inc(&entry->normalized);
1582 } /* stat_normalize */
1585 * Hook: A new graph was created
1587 * @param ctx the hook context
1588 * @param irg the new IR graph that was created
1589 * @param ent the entity of this graph
1591 static void stat_new_graph(void *ctx, ir_graph *irg, ir_entity *ent)
1594 if (! status->stat_options)
1599 /* execute for side effect :-) */
1600 graph_entry_t * graph = graph_get_entry(irg, status->irg_hash);
1603 graph->is_deleted = 0;
1605 graph->is_leaf_call = 0;
1606 graph->is_recursive = 0;
1607 graph->is_chain_call = 0;
1608 graph->is_strict = 1;
1609 graph->is_analyzed = 0;
1612 } /* stat_new_graph */
1615 * Hook: A graph will be deleted
1617 * @param ctx the hook context
1618 * @param irg the IR graph that will be deleted
1620 * Note that we still hold the information for this graph
1621 * in our hash maps, only a flag is set which prevents this
1622 * information from being changed, it's "frozen" from now.
1624 static void stat_free_graph(void *ctx, ir_graph *irg)
1627 if (! status->stat_options)
1632 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1633 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1635 graph->is_deleted = 1;
1637 if (status->stat_options & FIRMSTAT_COUNT_DELETED) {
1638 /* count the nodes of the graph yet, it will be destroyed later */
1639 update_graph_stat(global, graph);
1643 } /* stat_free_graph */
1646 * Hook: A walk over a graph is initiated. Do not count walks from statistic code.
1648 * @param ctx the hook context
1649 * @param irg the IR graph that will be walked
1650 * @param pre the pre walker
1651 * @param post the post walker
1653 static void stat_irg_walk(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1658 if (! status->stat_options)
1663 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1665 cnt_inc(&graph->cnt[gcnt_acc_walked]);
1668 } /* stat_irg_walk */
1671 * Hook: A walk over a graph in block-wise order is initiated. Do not count walks from statistic code.
1673 * @param ctx the hook context
1674 * @param irg the IR graph that will be walked
1675 * @param pre the pre walker
1676 * @param post the post walker
1678 static void stat_irg_walk_blkwise(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1680 /* for now, do NOT differentiate between blockwise and normal */
1681 stat_irg_walk(ctx, irg, pre, post);
1682 } /* stat_irg_walk_blkwise */
1685 * Hook: A walk over the graph's blocks is initiated. Do not count walks from statistic code.
1687 * @param ctx the hook context
1688 * @param irg the IR graph that will be walked
1689 * @param node the IR node
1690 * @param pre the pre walker
1691 * @param post the post walker
1693 static void stat_irg_block_walk(void *ctx, ir_graph *irg, ir_node *node, generic_func *pre, generic_func *post)
1699 if (! status->stat_options)
1704 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1706 cnt_inc(&graph->cnt[gcnt_acc_walked_blocks]);
1709 } /* stat_irg_block_walk */
1712 * Called for every node that is removed due to an optimization.
1714 * @param n the IR node that will be removed
1715 * @param hmap the hash map containing ir_op* -> opt_entry_t*
1716 * @param kind the optimization kind
1718 static void removed_due_opt(ir_node *n, hmap_opt_entry_t *hmap, hook_opt_kind kind)
1721 ir_op *op = stat_get_irn_op(n);
1723 /* ignore CSE for Constants */
1724 if (kind == HOOK_OPT_CSE && (is_Const(n) || is_SymConst(n)))
1727 /* increase global value */
1728 entry = opt_get_entry(op, hmap);
1729 cnt_inc(&entry->count);
1730 } /* removed_due_opt */
1733 * Hook: Some nodes were optimized into some others due to an optimization.
1735 * @param ctx the hook context
1737 static void stat_merge_nodes(
1739 ir_node **new_node_array, int new_num_entries,
1740 ir_node **old_node_array, int old_num_entries,
1744 if (! status->stat_options)
1750 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1752 cnt_inc(&status->num_opts[opt]);
1753 if (status->reassoc_run)
1754 opt = HOOK_OPT_REASSOC;
1756 for (i = 0; i < old_num_entries; ++i) {
1757 /* nodes might be in new and old, so if we found a node
1758 in both sets, this one is NOT removed */
1759 for (j = 0; j < new_num_entries; ++j) {
1760 if (old_node_array[i] == new_node_array[j])
1763 if (j >= new_num_entries) {
1766 /* sometimes we did not detect, that it is replaced by a Const */
1767 if (opt == HOOK_OPT_CONFIRM && new_num_entries == 1) {
1768 ir_op *op = get_irn_op(new_node_array[0]);
1770 if (op == op_Const || op == op_SymConst)
1771 xopt = HOOK_OPT_CONFIRM_C;
1774 removed_due_opt(old_node_array[i], graph->opt_hash[xopt], (hook_opt_kind)xopt);
1779 } /* stat_merge_nodes */
1782 * Hook: Reassociation is started/stopped.
1784 * @param ctx the hook context
1785 * @param flag if non-zero, reassociation is started else stopped
1787 static void stat_reassociate(void *ctx, int flag)
1790 if (! status->stat_options)
1795 status->reassoc_run = flag;
1798 } /* stat_reassociate */
1801 * Hook: A node was lowered into other nodes
1803 * @param ctx the hook context
1804 * @param node the IR node that will be lowered
1806 static void stat_lower(void *ctx, ir_node *node)
1809 if (! status->stat_options)
1814 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1816 removed_due_opt(node, graph->opt_hash[HOOK_LOWERED], HOOK_LOWERED);
1822 * Hook: A graph was inlined.
1824 * @param ctx the hook context
1825 * @param call the IR call that will re changed into the body of
1826 * the called IR graph
1827 * @param called_irg the IR graph representing the called routine
1829 static void stat_inline(void *ctx, ir_node *call, ir_graph *called_irg)
1832 if (! status->stat_options)
1837 ir_graph *irg = get_irn_irg(call);
1838 graph_entry_t *i_graph = graph_get_entry(called_irg, status->irg_hash);
1839 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1841 cnt_inc(&graph->cnt[gcnt_acc_got_inlined]);
1842 cnt_inc(&i_graph->cnt[gcnt_acc_was_inlined]);
1848 * Hook: A graph with tail-recursions was optimized.
1850 * @param ctx the hook context
1852 static void stat_tail_rec(void *ctx, ir_graph *irg, int n_calls)
1855 if (! status->stat_options)
1860 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1862 graph->num_tail_recursion += n_calls;
1865 } /* stat_tail_rec */
1868 * Strength reduction was performed on an iteration variable.
1870 * @param ctx the hook context
1872 static void stat_strength_red(void *ctx, ir_graph *irg, ir_node *strong)
1875 if (! status->stat_options)
1880 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1881 cnt_inc(&graph->cnt[gcnt_acc_strength_red]);
1883 removed_due_opt(strong, graph->opt_hash[HOOK_OPT_STRENGTH_RED], HOOK_OPT_STRENGTH_RED);
1886 } /* stat_strength_red */
1889 * Hook: Start/Stop the dead node elimination.
1891 * @param ctx the hook context
1893 static void stat_dead_node_elim(void *ctx, ir_graph *irg, int start)
1897 if (! status->stat_options)
1900 status->in_dead_node_elim = (start != 0);
1901 } /* stat_dead_node_elim */
1904 * Hook: if-conversion was tried.
1906 static void stat_if_conversion(void *context, ir_graph *irg, ir_node *phi,
1907 int pos, ir_node *mux, if_result_t reason)
1913 if (! status->stat_options)
1918 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1920 cnt_inc(&graph->cnt[gcnt_if_conv + reason]);
1923 } /* stat_if_conversion */
1926 * Hook: real function call was optimized.
1928 static void stat_func_call(void *context, ir_graph *irg, ir_node *call)
1932 if (! status->stat_options)
1937 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1939 cnt_inc(&graph->cnt[gcnt_acc_real_func_call]);
1942 } /* stat_func_call */
1945 * Hook: A multiply was replaced by a series of Shifts/Adds/Subs.
1947 * @param ctx the hook context
1949 static void stat_arch_dep_replace_mul_with_shifts(void *ctx, ir_node *mul)
1952 if (! status->stat_options)
1957 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1958 removed_due_opt(mul, graph->opt_hash[HOOK_OPT_ARCH_DEP], HOOK_OPT_ARCH_DEP);
1961 } /* stat_arch_dep_replace_mul_with_shifts */
1964 * Hook: A division by const was replaced.
1966 * @param ctx the hook context
1967 * @param node the division node that will be optimized
1969 static void stat_arch_dep_replace_division_by_const(void *ctx, ir_node *node)
1972 if (! status->stat_options)
1977 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1978 removed_due_opt(node, graph->opt_hash[HOOK_OPT_ARCH_DEP], HOOK_OPT_ARCH_DEP);
1981 } /* stat_arch_dep_replace_division_by_const */
1984 * Update the register pressure of a block.
1986 * @param irg the irg containing the block
1987 * @param block the block for which the reg pressure should be set
1988 * @param pressure the pressure
1989 * @param class_name the name of the register class
1991 void stat_be_block_regpressure(ir_graph *irg, ir_node *block, int pressure, const char *class_name)
1993 if (! status->stat_options)
1998 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1999 be_block_entry_t *block_ent;
2000 reg_pressure_entry_t *rp_ent;
2002 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
2003 rp_ent = OALLOCZ(&status->be_data, reg_pressure_entry_t);
2005 rp_ent->class_name = class_name;
2006 rp_ent->pressure = pressure;
2008 pset_insert(block_ent->reg_pressure, rp_ent, HASH_PTR(class_name));
2011 } /* stat_be_block_regpressure */
2014 * Update the distribution of ready nodes of a block
2016 * @param irg the irg containing the block
2017 * @param block the block for which the reg pressure should be set
2018 * @param num_ready the number of ready nodes
2020 void stat_be_block_sched_ready(ir_graph *irg, ir_node *block, int num_ready)
2022 if (! status->stat_options)
2027 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
2028 be_block_entry_t *block_ent;
2030 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
2032 /* increase the counter of corresponding number of ready nodes */
2033 stat_inc_int_distrib_tbl(block_ent->sched_ready, num_ready);
2036 } /* stat_be_block_sched_ready */
2039 * Update the permutation statistic of a block.
2041 * @param class_name the name of the register class
2042 * @param n_regs number of registers in the register class
2043 * @param perm the perm node
2044 * @param block the block containing the perm
2045 * @param size the size of the perm
2046 * @param real_size number of pairs with different registers
2048 void stat_be_block_stat_perm(const char *class_name, int n_regs, ir_node *perm, ir_node *block,
2049 int size, int real_size)
2051 if (! status->stat_options)
2056 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
2057 be_block_entry_t *block_ent;
2058 perm_class_entry_t *pc_ent;
2059 perm_stat_entry_t *ps_ent;
2061 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
2062 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
2063 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
2065 pc_ent->n_regs = n_regs;
2067 /* update information */
2068 ps_ent->size = size;
2069 ps_ent->real_size = real_size;
2072 } /* stat_be_block_stat_perm */
2075 * Update the permutation statistic of a single perm.
2077 * @param class_name the name of the register class
2078 * @param perm the perm node
2079 * @param block the block containing the perm
2080 * @param is_chain 1 if chain, 0 if cycle
2081 * @param size length of the cycle/chain
2082 * @param n_ops the number of ops representing this cycle/chain after lowering
2084 void stat_be_block_stat_permcycle(const char *class_name, ir_node *perm, ir_node *block,
2085 int is_chain, int size, int n_ops)
2087 if (! status->stat_options)
2092 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
2093 be_block_entry_t *block_ent;
2094 perm_class_entry_t *pc_ent;
2095 perm_stat_entry_t *ps_ent;
2097 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
2098 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
2099 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
2102 ps_ent->n_copies += n_ops;
2103 stat_inc_int_distrib_tbl(ps_ent->chains, size);
2105 ps_ent->n_exchg += n_ops;
2106 stat_inc_int_distrib_tbl(ps_ent->cycles, size);
2110 } /* stat_be_block_stat_permcycle */
2112 /* Dumps a statistics snapshot. */
2113 void stat_dump_snapshot(const char *name, const char *phase)
2119 if (! status->stat_options)
2124 graph_entry_t *entry;
2125 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
2128 * The constant counter is only global, so we clear it here.
2129 * Note that it does NOT contain the constants in DELETED
2130 * graphs due to this.
2132 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2133 stat_const_clear(status);
2135 /* build the name */
2136 p = strrchr(name, '/');
2141 q = strrchr(name, '\\');
2143 /* NULL might be not the smallest pointer */
2144 if (q && (!p || q > p))
2152 if (l > (int) (sizeof(fname) - 1))
2153 l = sizeof(fname) - 1;
2155 memcpy(fname, name, l);
2161 strncat(fname, "firmstat-", sizeof(fname)-1);
2162 strncat(fname, phase, sizeof(fname)-1);
2163 strncat(fname, "-", sizeof(fname)-1);
2164 strncat(fname, p, sizeof(fname)-1);
2166 stat_dump_init(fname);
2168 /* calculate the graph statistics */
2169 for (entry = (graph_entry_t*)pset_first(status->irg_hash);
2170 entry != NULL; entry = (graph_entry_t*)pset_next(status->irg_hash)) {
2171 if (entry->irg == NULL) {
2172 /* special entry for the global count */
2175 if (! entry->is_deleted) {
2176 /* the graph is still alive, count the nodes on it */
2177 update_graph_stat(global, entry);
2181 /* some calculations are dependent, we pushed them on the wait_q */
2182 while (! pdeq_empty(status->wait_q)) {
2183 entry = (graph_entry_t*)pdeq_getr(status->wait_q);
2185 update_graph_stat_2(global, entry);
2188 /* dump per graph */
2189 for (entry = (graph_entry_t*)pset_first(status->irg_hash);
2190 entry != NULL; entry = (graph_entry_t*)pset_next(status->irg_hash)) {
2191 if (entry->irg == NULL) {
2192 /* special entry for the global count */
2196 if (! entry->is_deleted || status->stat_options & FIRMSTAT_COUNT_DELETED) {
2197 stat_dump_graph(entry);
2198 stat_dump_registered(entry);
2201 if (! entry->is_deleted) {
2202 /* clear the counter that are not accumulated */
2203 graph_clear_entry(entry, 0);
2208 stat_dump_graph(global);
2210 /* dump the const info */
2211 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2212 stat_dump_consts(&status->const_info);
2214 /* dump the parameter distribution */
2215 stat_dump_param_tbl(status->dist_param_cnt, global);
2217 /* dump the optimization counter and clear them */
2218 stat_dump_opt_cnt(status->num_opts, ARRAY_SIZE(status->num_opts));
2219 clear_optimization_counter();
2223 stat_finish_pattern_history(fname);
2225 /* clear the global counters here */
2227 node_entry_t *entry;
2229 for (entry = (node_entry_t*)pset_first(global->opcode_hash);
2230 entry != NULL; entry = (node_entry_t*)pset_next(global->opcode_hash)) {
2231 opcode_clear_entry(entry);
2233 /* clear all global counter */
2234 graph_clear_entry(global, /*all=*/1);
2238 } /* stat_dump_snapshot */
2240 typedef struct pass_t {
2241 ir_prog_pass_t pass;
2247 * Wrapper to run stat_dump_snapshot() as a ir_prog wrapper.
2249 static int stat_dump_snapshot_wrapper(ir_prog *irp, void *context)
2251 pass_t *pass = (pass_t*)context;
2254 stat_dump_snapshot(pass->fname, pass->phase);
2256 } /* stat_dump_snapshot_wrapper */
2259 * Ensure that no verifier is run from the wrapper.
2261 static int no_verify(ir_prog *prog, void *ctx)
2269 * Ensure that no dumper is run from the wrapper.
2271 static void no_dump(ir_prog *prog, void *ctx, unsigned idx)
2278 /* create an ir_pog pass */
2279 ir_prog_pass_t *stat_dump_snapshot_pass(
2280 const char *name, const char *fname, const char *phase)
2282 pass_t *pass = XMALLOCZ(pass_t);
2284 def_prog_pass_constructor(
2285 &pass->pass, name ? name : "stat_snapshot", stat_dump_snapshot_wrapper);
2286 pass->fname = fname;
2287 pass->phase = phase;
2289 /* no dump/verify */
2290 pass->pass.dump_irprog = no_dump;
2291 pass->pass.verify_irprog = no_verify;
2294 } /* stat_dump_snapshot_pass */
2296 /** the hook entries for the Firm statistics module */
2297 static hook_entry_t stat_hooks[hook_last];
2299 /* initialize the statistics module. */
2300 void firm_init_stat(unsigned enable_options)
2302 #define X(a) a, sizeof(a)-1
2303 #define HOOK(h, fkt) \
2304 stat_hooks[h].hook._##h = fkt; register_hook(h, &stat_hooks[h])
2307 if (! (enable_options & FIRMSTAT_ENABLED))
2310 status = XMALLOCZ(stat_info_t);
2312 /* enable statistics */
2313 status->stat_options = enable_options & FIRMSTAT_ENABLED ? enable_options : 0;
2315 /* register all hooks */
2316 HOOK(hook_new_ir_op, stat_new_ir_op);
2317 HOOK(hook_free_ir_op, stat_free_ir_op);
2318 HOOK(hook_new_node, stat_new_node);
2319 HOOK(hook_turn_into_id, stat_turn_into_id);
2320 HOOK(hook_normalize, stat_normalize);
2321 HOOK(hook_new_graph, stat_new_graph);
2322 HOOK(hook_free_graph, stat_free_graph);
2323 HOOK(hook_irg_walk, stat_irg_walk);
2324 HOOK(hook_irg_walk_blkwise, stat_irg_walk_blkwise);
2325 HOOK(hook_irg_block_walk, stat_irg_block_walk);
2326 HOOK(hook_merge_nodes, stat_merge_nodes);
2327 HOOK(hook_reassociate, stat_reassociate);
2328 HOOK(hook_lower, stat_lower);
2329 HOOK(hook_inline, stat_inline);
2330 HOOK(hook_tail_rec, stat_tail_rec);
2331 HOOK(hook_strength_red, stat_strength_red);
2332 HOOK(hook_dead_node_elim, stat_dead_node_elim);
2333 HOOK(hook_if_conversion, stat_if_conversion);
2334 HOOK(hook_func_call, stat_func_call);
2335 HOOK(hook_arch_dep_replace_mul_with_shifts, stat_arch_dep_replace_mul_with_shifts);
2336 HOOK(hook_arch_dep_replace_division_by_const, stat_arch_dep_replace_division_by_const);
2338 obstack_init(&status->cnts);
2339 obstack_init(&status->be_data);
2341 /* create the hash-tables */
2342 status->irg_hash = new_pset(graph_cmp, 8);
2343 status->ir_op_hash = new_pset(opcode_cmp_2, 1);
2345 /* create the wait queue */
2346 status->wait_q = new_pdeq();
2348 if (enable_options & FIRMSTAT_COUNT_STRONG_OP) {
2349 /* build the pseudo-ops */
2351 _op_Phi0.code = --num;
2352 _op_Phi0.name = new_id_from_chars(X("Phi0"));
2354 _op_PhiM.code = --num;
2355 _op_PhiM.name = new_id_from_chars(X("PhiM"));
2357 _op_ProjM.code = --num;
2358 _op_ProjM.name = new_id_from_chars(X("ProjM"));
2360 _op_MulC.code = --num;
2361 _op_MulC.name = new_id_from_chars(X("MulC"));
2363 _op_DivC.code = --num;
2364 _op_DivC.name = new_id_from_chars(X("DivC"));
2366 _op_ModC.code = --num;
2367 _op_ModC.name = new_id_from_chars(X("ModC"));
2369 status->op_Phi0 = &_op_Phi0;
2370 status->op_PhiM = &_op_PhiM;
2371 status->op_ProjM = &_op_ProjM;
2372 status->op_MulC = &_op_MulC;
2373 status->op_DivC = &_op_DivC;
2374 status->op_ModC = &_op_ModC;
2376 status->op_Phi0 = NULL;
2377 status->op_PhiM = NULL;
2378 status->op_ProjM = NULL;
2379 status->op_MulC = NULL;
2380 status->op_DivC = NULL;
2381 status->op_ModC = NULL;
2384 /* for Florian: count the Sel depth */
2385 if (enable_options & FIRMSTAT_COUNT_SELS) {
2386 _op_SelSel.code = --num;
2387 _op_SelSel.name = new_id_from_chars(X("Sel(Sel)"));
2389 _op_SelSelSel.code = --num;
2390 _op_SelSelSel.name = new_id_from_chars(X("Sel(Sel(Sel))"));
2392 status->op_SelSel = &_op_SelSel;
2393 status->op_SelSelSel = &_op_SelSelSel;
2395 status->op_SelSel = NULL;
2396 status->op_SelSelSel = NULL;
2399 /* register the dumper */
2400 stat_register_dumper(&simple_dumper);
2402 if (enable_options & FIRMSTAT_CSV_OUTPUT)
2403 stat_register_dumper(&csv_dumper);
2405 /* initialize the pattern hash */
2406 stat_init_pattern_history(enable_options & FIRMSTAT_PATTERN_ENABLED);
2408 /* initialize the Const options */
2409 if (enable_options & FIRMSTAT_COUNT_CONSTS)
2410 stat_init_const_cnt(status);
2412 /* distribution table for parameter counts */
2413 status->dist_param_cnt = stat_new_int_distrib_tbl();
2415 clear_optimization_counter();
2419 } /* firm_init_stat */
2422 * Frees all dumper structures.
2424 static void stat_term_dumper(void)
2426 dumper_t *dumper, *next_dumper;
2428 for (dumper = status->dumper; dumper; /* iteration done in loop body */ ) {
2429 if (dumper->func_map)
2430 del_pset(dumper->func_map);
2432 next_dumper = dumper->next;
2434 dumper = next_dumper;
2436 } /* stat_term_dumper */
2439 /* Terminates the statistics module, frees all memory. */
2440 void stat_term(void)
2442 if (status != (stat_info_t *)&status_disable) {
2443 obstack_free(&status->be_data, NULL);
2444 obstack_free(&status->cnts, NULL);
2449 status = (stat_info_t *)&status_disable;
2453 /* returns 1 if statistics were initialized, 0 otherwise */
2454 int stat_is_active(void)
2456 return status != (stat_info_t *)&status_disable;
2457 } /* stat_is_active */
2461 /* initialize the statistics module. */
2462 void firm_init_stat(unsigned enable_options) {}
2464 /* Dumps a statistics snapshot */
2465 void stat_dump_snapshot(const char *name, const char *phase) {}
2467 /* terminates the statistics module, frees all memory */
2468 void stat_term(void);
2470 #endif /* FIRM_STATISTICS */