2 * Copyright (C) 1995-2008 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
30 #ifdef FIRM_STATISTICS
44 #include "firmstat_t.h"
52 * need this to be static:
53 * Special pseudo Opcodes that we need to count some interesting cases
57 * The Phi0, a node that is created during SSA construction
59 static ir_op _op_Phi0;
61 /** The PhiM, just to count memory Phi's. */
62 static ir_op _op_PhiM;
64 /** The Mul by Const node. */
65 static ir_op _op_MulC;
67 /** The Div by Const node. */
68 static ir_op _op_DivC;
70 /** The Div by Const node. */
71 static ir_op _op_ModC;
73 /** The Div by Const node. */
74 static ir_op _op_DivModC;
76 /** The Quot by Const node. */
77 static ir_op _op_QuotC;
79 /** The memory Proj node. */
80 static ir_op _op_ProjM;
83 static ir_op _op_SelSel;
85 /** A Sel of a Sel of a Sel */
86 static ir_op _op_SelSelSel;
88 /* ---------------------------------------------------------------------------------- */
90 /** Marks the begin of a statistic (hook) function. */
91 #define STAT_ENTER ++status->recursive
93 /** Marks the end of a statistic (hook) functions. */
94 #define STAT_LEAVE --status->recursive
96 /** Allows to enter a statistic function only when we are not already in a hook. */
97 #define STAT_ENTER_SINGLE do { if (status->recursive > 0) return; ++status->recursive; } while (0)
102 static const unsigned status_disable = 0;
103 static stat_info_t *status = (stat_info_t *)&status_disable;
106 * Compare two elements of the opcode hash.
108 static int opcode_cmp(const void *elt, const void *key) {
109 const node_entry_t *e1 = elt;
110 const node_entry_t *e2 = key;
112 return e1->op->code - e2->op->code;
116 * Compare two elements of the graph hash.
118 static int graph_cmp(const void *elt, const void *key) {
119 const graph_entry_t *e1 = elt;
120 const graph_entry_t *e2 = key;
122 return e1->irg != e2->irg;
126 * Compare two elements of the optimization hash.
128 static int opt_cmp(const void *elt, const void *key) {
129 const opt_entry_t *e1 = elt;
130 const opt_entry_t *e2 = key;
132 return e1->op->code != e2->op->code;
136 * Compare two elements of the block/extbb hash.
138 static int block_cmp(const void *elt, const void *key) {
139 const block_entry_t *e1 = elt;
140 const block_entry_t *e2 = key;
142 /* it's enough to compare the block number */
143 return e1->block_nr != e2->block_nr;
147 * Compare two elements of the be_block hash.
149 static int be_block_cmp(const void *elt, const void *key) {
150 const be_block_entry_t *e1 = elt;
151 const be_block_entry_t *e2 = key;
153 return e1->block_nr != e2->block_nr;
157 * Compare two elements of reg pressure hash.
159 static int reg_pressure_cmp(const void *elt, const void *key) {
160 const reg_pressure_entry_t *e1 = elt;
161 const reg_pressure_entry_t *e2 = key;
163 return e1->class_name != e2->class_name;
164 } /* reg_pressure_cmp */
167 * Compare two elements of the perm_stat hash.
169 static int perm_stat_cmp(const void *elt, const void *key) {
170 const perm_stat_entry_t *e1 = elt;
171 const perm_stat_entry_t *e2 = key;
173 return e1->perm != e2->perm;
174 } /* perm_stat_cmp */
177 * Compare two elements of the perm_class hash.
179 static int perm_class_cmp(const void *elt, const void *key) {
180 const perm_class_entry_t *e1 = elt;
181 const perm_class_entry_t *e2 = key;
183 return e1->class_name != e2->class_name;
184 } /* perm_class_cmp */
187 * Compare two elements of the ir_op hash.
189 static int opcode_cmp_2(const void *elt, const void *key) {
190 const ir_op *e1 = elt;
191 const ir_op *e2 = key;
193 return e1->code != e2->code;
197 * Compare two elements of the address_mark set.
199 static int address_mark_cmp(const void *elt, const void *key, size_t size) {
200 const address_mark_entry_t *e1 = elt;
201 const address_mark_entry_t *e2 = key;
204 /* compare only the nodes, the rest is used as data container */
205 return e1->node != e2->node;
206 } /* address_mark_cmp */
209 * Clear all counter in a node_entry_t.
211 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 } /* opcode_clear_entry */
218 * Returns the associates node_entry_t for an ir_op
220 * @param op the IR operation
221 * @param hmap a hash map containing ir_op* -> node_entry_t*
223 static node_entry_t *opcode_get_entry(const ir_op *op, hmap_node_entry_t *hmap) {
229 elem = pset_find(hmap, &key, op->code);
233 elem = obstack_alloc(&status->cnts, sizeof(*elem));
234 memset(elem, 0, sizeof(*elem));
237 opcode_clear_entry(elem);
241 return pset_insert(hmap, elem, op->code);
242 } /* opcode_get_entry */
245 * Returns the associates ir_op for an opcode
247 * @param code the IR opcode
248 * @param hmap the hash map containing opcode -> ir_op*
250 static ir_op *opcode_find_entry(ir_opcode code, hmap_ir_op *hmap) {
254 return pset_find(hmap, &key, code);
255 } /* opcode_find_entry */
258 * Clears all counter in a graph_entry_t.
260 * @param elem the graph entry
261 * @param all if non-zero, clears all counters, else leave accumulated ones
263 static void graph_clear_entry(graph_entry_t *elem, int all) {
266 /* clear accumulated / non-accumulated counter */
267 for (i = all ? 0 : _gcnt_non_acc; i < _gcnt_last; ++i) {
268 cnt_clr(&elem->cnt[i]);
271 if (elem->block_hash) {
272 del_pset(elem->block_hash);
273 elem->block_hash = NULL;
276 if (elem->extbb_hash) {
277 del_pset(elem->extbb_hash);
278 elem->extbb_hash = NULL;
281 obstack_free(&elem->recalc_cnts, NULL);
282 obstack_init(&elem->recalc_cnts);
283 } /* graph_clear_entry */
286 * Returns the associated graph_entry_t for an IR graph.
288 * @param irg the IR graph
289 * @param hmap the hash map containing ir_graph* -> graph_entry_t*
291 static graph_entry_t *graph_get_entry(ir_graph *irg, hmap_graph_entry_t *hmap)
299 elem = pset_find(hmap, &key, HASH_PTR(irg));
302 /* create hash map backend block information */
303 if (! elem->be_block_hash)
304 elem->be_block_hash = new_pset(be_block_cmp, 5);
309 /* allocate a new one */
310 elem = obstack_alloc(&status->cnts, sizeof(*elem));
311 memset(elem, 0, sizeof(*elem));
312 obstack_init(&elem->recalc_cnts);
315 graph_clear_entry(elem, 1);
317 /* new hash table for opcodes here */
318 elem->opcode_hash = new_pset(opcode_cmp, 5);
319 elem->address_mark = new_set(address_mark_cmp, 5);
322 /* these hash tables are created on demand */
323 elem->block_hash = NULL;
324 elem->extbb_hash = NULL;
326 for (i = 0; i < sizeof(elem->opt_hash)/sizeof(elem->opt_hash[0]); ++i)
327 elem->opt_hash[i] = new_pset(opt_cmp, 4);
329 return pset_insert(hmap, elem, HASH_PTR(irg));
330 } /* graph_get_entry */
333 * Clear all counter in an opt_entry_t.
335 static void opt_clear_entry(opt_entry_t *elem) {
336 cnt_clr(&elem->count);
337 } /* opt_clear_entry */
340 * Returns the associated opt_entry_t for an IR operation.
342 * @param op the IR operation
343 * @param hmap the hash map containing ir_op* -> opt_entry_t*
345 static opt_entry_t *opt_get_entry(const ir_op *op, hmap_opt_entry_t *hmap)
352 elem = pset_find(hmap, &key, op->code);
356 elem = obstack_alloc(&status->cnts, sizeof(*elem));
357 memset(elem, 0, sizeof(*elem));
359 /* clear new counter */
360 opt_clear_entry(elem);
364 return pset_insert(hmap, elem, op->code);
365 } /* opt_get_entry */
368 * clears all counter in a block_entry_t
370 static void block_clear_entry(block_entry_t *elem) {
373 for (i = 0; i < _bcnt_last; ++i)
374 cnt_clr(&elem->cnt[i]);
375 } /* block_clear_entry */
378 * Returns the associated block_entry_t for an block.
380 * @param block_nr an IR block number
381 * @param hmap a hash map containing long -> block_entry_t
383 static block_entry_t *block_get_entry(struct obstack *obst, long block_nr, hmap_block_entry_t *hmap)
388 key.block_nr = block_nr;
390 elem = pset_find(hmap, &key, block_nr);
394 elem = obstack_alloc(obst, sizeof(*elem));
395 memset(elem, 0, sizeof(*elem));
397 /* clear new counter */
398 block_clear_entry(elem);
400 elem->block_nr = block_nr;
402 return pset_insert(hmap, elem, block_nr);
403 } /* block_get_entry */
406 * Clear all sets in be_block_entry_t.
408 static void be_block_clear_entry(be_block_entry_t *elem)
410 if (elem->reg_pressure)
411 del_pset(elem->reg_pressure);
413 if (elem->sched_ready)
414 stat_delete_distrib_tbl(elem->sched_ready);
416 if (elem->perm_class_stat)
417 del_pset(elem->perm_class_stat);
419 elem->reg_pressure = new_pset(reg_pressure_cmp, 5);
420 elem->sched_ready = stat_new_int_distrib_tbl();
421 elem->perm_class_stat = new_pset(perm_class_cmp, 5);
422 } /* be_block_clear_entry */
425 * Returns the associated be_block_entry_t for an block.
427 * @param block_nr an IR block number
428 * @param hmap a hash map containing long -> be_block_entry_t
430 static be_block_entry_t *be_block_get_entry(struct obstack *obst, long block_nr, hmap_be_block_entry_t *hmap)
432 be_block_entry_t key;
433 be_block_entry_t *elem;
435 key.block_nr = block_nr;
437 elem = pset_find(hmap, &key, block_nr);
441 elem = obstack_alloc(obst, sizeof(*elem));
442 memset(elem, 0, sizeof(*elem));
444 /* clear new counter */
445 be_block_clear_entry(elem);
447 elem->block_nr = block_nr;
449 return pset_insert(hmap, elem, block_nr);
450 } /* be_block_get_entry */
453 * clears all sets in perm_class_entry_t
455 static void perm_class_clear_entry(perm_class_entry_t *elem) {
457 del_pset(elem->perm_stat);
459 elem->perm_stat = new_pset(perm_stat_cmp, 5);
460 } /* perm_class_clear_entry */
463 * Returns the associated perm_class entry for a register class.
465 * @param class_name the register class name
466 * @param hmap a hash map containing class_name -> perm_class_entry_t
468 static perm_class_entry_t *perm_class_get_entry(struct obstack *obst, const char *class_name,
469 hmap_perm_class_entry_t *hmap)
471 perm_class_entry_t key;
472 perm_class_entry_t *elem;
474 key.class_name = class_name;
476 elem = pset_find(hmap, &key, HASH_PTR(class_name));
480 elem = obstack_alloc(obst, sizeof(*elem));
481 memset(elem, 0, sizeof(*elem));
483 /* clear new counter */
484 perm_class_clear_entry(elem);
486 elem->class_name = class_name;
488 return pset_insert(hmap, elem, HASH_PTR(class_name));
489 } /* perm_class_get_entry */
492 * clears all sets in perm_stat_entry_t
494 static void perm_stat_clear_entry(perm_stat_entry_t *elem) {
496 stat_delete_distrib_tbl(elem->chains);
499 stat_delete_distrib_tbl(elem->cycles);
501 elem->chains = stat_new_int_distrib_tbl();
502 elem->cycles = stat_new_int_distrib_tbl();
503 } /* perm_stat_clear_entry */
506 * Returns the associated perm_stat entry for a perm.
508 * @param perm the perm node
509 * @param hmap a hash map containing perm -> perm_stat_entry_t
511 static perm_stat_entry_t *perm_stat_get_entry(struct obstack *obst, ir_node *perm, hmap_perm_stat_entry_t *hmap)
513 perm_stat_entry_t key;
514 perm_stat_entry_t *elem;
518 elem = pset_find(hmap, &key, HASH_PTR(perm));
522 elem = obstack_alloc(obst, sizeof(*elem));
523 memset(elem, 0, sizeof(*elem));
525 /* clear new counter */
526 perm_stat_clear_entry(elem);
530 return pset_insert(hmap, elem, HASH_PTR(perm));
531 } /* perm_stat_get_entry */
534 * Clear optimizations counter,
536 static void clear_optimization_counter(void) {
538 for (i = 0; i < FS_OPT_MAX; ++i)
539 cnt_clr(&status->num_opts[i]);
543 * Returns the ir_op for an IR-node,
544 * handles special cases and return pseudo op codes.
546 * @param none an IR node
548 static ir_op *stat_get_irn_op(ir_node *node)
550 ir_op *op = get_irn_op(node);
551 ir_opcode opc = op->code;
555 if (get_irn_arity(node) == 0) {
556 /* special case, a Phi0 node, count on extra counter */
557 op = status->op_Phi0 ? status->op_Phi0 : op;
558 } else if (get_irn_mode(node) == mode_M) {
559 /* special case, a Memory Phi node, count on extra counter */
560 op = status->op_PhiM ? status->op_PhiM : op;
564 if (get_irn_mode(node) == mode_M) {
565 /* special case, a Memory Proj node, count on extra counter */
566 op = status->op_ProjM ? status->op_ProjM : op;
570 if (is_Const(get_Mul_left(node)) || is_Const(get_Mul_right(node))) {
571 /* special case, a Multiply by a const, count on extra counter */
572 op = status->op_MulC ? status->op_MulC : op;
576 if (is_Const(get_Div_right(node))) {
577 /* special case, a division by a const, count on extra counter */
578 op = status->op_DivC ? status->op_DivC : op;
582 if (is_Const(get_Mod_right(node))) {
583 /* special case, a module by a const, count on extra counter */
584 op = status->op_ModC ? status->op_ModC : op;
588 if (is_Const(get_DivMod_right(node))) {
589 /* special case, a division/modulo by a const, count on extra counter */
590 op = status->op_DivModC ? status->op_DivModC : op;
594 if (is_Const(get_Quot_right(node))) {
595 /* special case, a floating point division by a const, count on extra counter */
596 op = status->op_QuotC ? status->op_QuotC : op;
600 if (get_irn_op(get_Sel_ptr(node)) == op_Sel) {
601 /* special case, a Sel of a Sel, count on extra counter */
602 op = status->op_SelSel ? status->op_SelSel : op;
603 if (get_irn_op(get_Sel_ptr(get_Sel_ptr(node))) == op_Sel) {
604 /* special case, a Sel of a Sel of a Sel, count on extra counter */
605 op = status->op_SelSelSel ? status->op_SelSelSel : op;
614 } /* stat_get_irn_op */
617 * update the block counter
619 static void undate_block_info(ir_node *node, graph_entry_t *graph)
621 ir_op *op = get_irn_op(node);
623 block_entry_t *b_entry;
626 /* check for block */
627 if (op == op_Block) {
628 arity = get_irn_arity(node);
629 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(node), graph->block_hash);
630 /* mark start end block to allow to filter them out */
631 if (node == get_irg_start_block(graph->irg))
632 b_entry->is_start = 1;
633 else if (node == get_irg_end_block(graph->irg))
636 /* count all incoming edges */
637 for (i = 0; i < arity; ++i) {
638 ir_node *pred = get_irn_n(node, i);
639 ir_node *other_block = get_nodes_block(pred);
640 block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
642 cnt_inc(&b_entry->cnt[bcnt_in_edges]); /* an edge coming from another block */
643 cnt_inc(&b_entry_other->cnt[bcnt_out_edges]);
648 block = get_nodes_block(node);
649 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(block), graph->block_hash);
651 if (op == op_Phi && mode_is_datab(get_irn_mode(node))) {
652 /* count data Phi per block */
653 cnt_inc(&b_entry->cnt[bcnt_phi_data]);
656 /* we have a new node in our block */
657 cnt_inc(&b_entry->cnt[bcnt_nodes]);
659 /* don't count keep-alive edges */
663 arity = get_irn_arity(node);
665 for (i = 0; i < arity; ++i) {
666 ir_node *pred = get_irn_n(node, i);
667 ir_node *other_block;
669 other_block = get_nodes_block(pred);
671 if (other_block == block)
672 cnt_inc(&b_entry->cnt[bcnt_edges]); /* a in block edge */
674 block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
676 cnt_inc(&b_entry->cnt[bcnt_in_edges]); /* an edge coming from another block */
677 cnt_inc(&b_entry_other->cnt[bcnt_out_edges]);
680 } /* undate_block_info */
683 * Update the extended block counter.
685 static void update_extbb_info(ir_node *node, graph_entry_t *graph)
687 ir_op *op = get_irn_op(node);
689 extbb_entry_t *eb_entry;
692 /* check for block */
693 if (op == op_Block) {
694 extbb = get_nodes_extbb(node);
695 arity = get_irn_arity(node);
696 eb_entry = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(extbb), graph->extbb_hash);
698 /* count all incoming edges */
699 for (i = 0; i < arity; ++i) {
700 ir_node *pred = get_irn_n(node, i);
701 ir_extblk *other_extbb = get_nodes_extbb(pred);
703 if (extbb != other_extbb) {
704 extbb_entry_t *eb_entry_other = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(other_extbb), graph->extbb_hash);
706 cnt_inc(&eb_entry->cnt[bcnt_in_edges]); /* an edge coming from another extbb */
707 cnt_inc(&eb_entry_other->cnt[bcnt_out_edges]);
713 extbb = get_nodes_extbb(node);
714 eb_entry = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(extbb), graph->extbb_hash);
716 if (op == op_Phi && mode_is_datab(get_irn_mode(node))) {
717 /* count data Phi per extbb */
718 cnt_inc(&eb_entry->cnt[bcnt_phi_data]);
721 /* we have a new node in our block */
722 cnt_inc(&eb_entry->cnt[bcnt_nodes]);
724 /* don't count keep-alive edges */
725 if (get_irn_op(node) == op_End)
728 arity = get_irn_arity(node);
730 for (i = 0; i < arity; ++i) {
731 ir_node *pred = get_irn_n(node, i);
732 ir_extblk *other_extbb = get_nodes_extbb(pred);
734 if (other_extbb == extbb)
735 cnt_inc(&eb_entry->cnt[bcnt_edges]); /* a in extbb edge */
737 extbb_entry_t *eb_entry_other = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(other_extbb), graph->extbb_hash);
739 cnt_inc(&eb_entry->cnt[bcnt_in_edges]); /* an edge coming from another extbb */
740 cnt_inc(&eb_entry_other->cnt[bcnt_out_edges]);
743 } /* update_extbb_info */
746 * Calculates how many arguments of the call are const, updates
747 * param distribution.
749 static void analyse_params_of_Call(graph_entry_t *graph, ir_node *call) {
750 int i, num_const_args = 0, num_local_adr = 0;
751 int n = get_Call_n_params(call);
753 for (i = 0; i < n; ++i) {
754 ir_node *param = get_Call_param(call, i);
756 if (is_irn_constlike(param))
758 else if (is_Sel(param)) {
759 ir_node *base = param;
762 base = get_Sel_ptr(base);
763 } while (is_Sel(base));
765 if (base == get_irg_frame(current_ir_graph))
771 if (num_const_args > 0)
772 cnt_inc(&graph->cnt[gcnt_call_with_cnst_arg]);
773 if (num_const_args == n)
774 cnt_inc(&graph->cnt[gcnt_call_with_all_cnst_arg]);
775 if (num_local_adr > 0)
776 cnt_inc(&graph->cnt[gcnt_call_with_local_adr]);
778 stat_inc_int_distrib_tbl(status->dist_param_cnt, n);
779 } /* analyse_params_of_Call */
782 * Update info on calls.
784 * @param call The call
785 * @param graph The graph entry containing the call
787 static void stat_update_call(ir_node *call, graph_entry_t *graph)
789 ir_node *block = get_nodes_block(call);
790 ir_node *ptr = get_Call_ptr(call);
791 ir_entity *ent = NULL;
792 ir_graph *callee = NULL;
795 * If the block is bad, the whole subgraph will collapse later
796 * so do not count this call.
797 * This happens in dead code.
802 cnt_inc(&graph->cnt[gcnt_all_calls]);
804 /* found a call, this function is not a leaf */
807 if (get_irn_op(ptr) == op_SymConst) {
808 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
809 /* ok, we seems to know the entity */
810 ent = get_SymConst_entity(ptr);
811 callee = get_entity_irg(ent);
813 /* it is recursive, if it calls at least once */
814 if (callee == graph->irg)
815 graph->is_recursive = 1;
817 cnt_inc(&graph->cnt[gcnt_external_calls]);
820 /* indirect call, be could not predict */
821 cnt_inc(&graph->cnt[gcnt_indirect_calls]);
823 /* NOT a leaf call */
824 graph->is_leaf_call = LCS_NON_LEAF_CALL;
827 /* check, if it's a chain-call: Then, the call-block
828 * must dominate the end block. */
830 ir_node *curr = get_irg_end_block(graph->irg);
831 int depth = get_Block_dom_depth(block);
833 for (; curr != block && get_Block_dom_depth(curr) > depth;) {
834 curr = get_Block_idom(curr);
836 if (! curr || is_no_Block(curr))
841 graph->is_chain_call = 0;
844 /* check, if the callee is a leaf */
846 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
848 if (called->is_analyzed) {
849 if (! called->is_leaf)
850 graph->is_leaf_call = LCS_NON_LEAF_CALL;
854 analyse_params_of_Call(graph, call);
855 } /* stat_update_call */
858 * Update info on calls for graphs on the wait queue.
860 static void stat_update_call_2(ir_node *call, graph_entry_t *graph)
862 ir_node *block = get_nodes_block(call);
863 ir_node *ptr = get_Call_ptr(call);
864 ir_entity *ent = NULL;
865 ir_graph *callee = NULL;
868 * If the block is bad, the whole subgraph will collapse later
869 * so do not count this call.
870 * This happens in dead code.
875 if (get_irn_op(ptr) == op_SymConst) {
876 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
877 /* ok, we seems to know the entity */
878 ent = get_SymConst_entity(ptr);
879 callee = get_entity_irg(ent);
883 /* check, if the callee is a leaf */
885 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
887 assert(called->is_analyzed);
889 if (! called->is_leaf)
890 graph->is_leaf_call = LCS_NON_LEAF_CALL;
892 graph->is_leaf_call = LCS_NON_LEAF_CALL;
893 } /* stat_update_call_2 */
896 * Find the base address and entity of an Sel node.
898 * @param sel the node
900 * @return the base address.
902 static ir_node *find_base_adr(ir_node *sel) {
903 ir_node *ptr = get_Sel_ptr(sel);
905 while (is_Sel(ptr)) {
907 ptr = get_Sel_ptr(sel);
910 } /* find_base_adr */
913 * Update info on Load/Store address statistics.
915 static void stat_update_address(ir_node *node, graph_entry_t *graph) {
916 ir_opcode opc = get_irn_opcode(node);
922 /* a global address */
923 cnt_inc(&graph->cnt[gcnt_global_adr]);
926 base = find_base_adr(node);
927 irg = current_ir_graph;
928 if (base == get_irg_tls(irg)) {
929 /* a TLS variable, like a global. */
930 cnt_inc(&graph->cnt[gcnt_global_adr]);
931 } else if (base == get_irg_frame(irg)) {
932 /* a local Variable. */
933 cnt_inc(&graph->cnt[gcnt_local_adr]);
936 if (is_Proj(base) && skip_Proj(get_Proj_pred(base)) == get_irg_start(irg)) {
937 /* pointer access through parameter, check for THIS */
938 ir_entity *ent = get_irg_entity(irg);
941 ir_type *ent_tp = get_entity_type(ent);
943 if (get_method_calling_convention(ent_tp) & cc_this_call) {
944 if (get_Proj_proj(base) == 0) {
946 cnt_inc(&graph->cnt[gcnt_this_adr]);
951 /* other parameter */
952 cnt_inc(&graph->cnt[gcnt_param_adr]);
955 /* unknown Pointer access */
956 cnt_inc(&graph->cnt[gcnt_other_adr]);
962 } /* stat_update_address */
965 * Walker for reachable nodes count.
967 static void update_node_stat(ir_node *node, void *env)
969 graph_entry_t *graph = env;
972 ir_op *op = stat_get_irn_op(node);
973 int arity = get_irn_arity(node);
975 entry = opcode_get_entry(op, graph->opcode_hash);
977 cnt_inc(&entry->cnt_alive);
978 cnt_add_i(&graph->cnt[gcnt_edges], arity);
980 /* count block edges */
981 undate_block_info(node, graph);
983 /* count extended block edges */
984 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
985 if (graph->irg != get_const_code_irg())
986 update_extbb_info(node, graph);
989 /* handle statistics for special node types */
993 if (status->stat_options & FIRMSTAT_COUNT_CONSTS) {
994 /* check properties of constants */
995 stat_update_const(status, node, graph);
999 /* check for properties that depends on calls like recursion/leaf/indirect call */
1000 stat_update_call(node, graph);
1003 /* check address properties */
1004 stat_update_address(get_Load_ptr(node), graph);
1007 /* check address properties */
1008 stat_update_address(get_Store_ptr(node), graph);
1013 } /* update_node_stat */
1016 * Walker for reachable nodes count for graphs on the wait_q.
1018 static void update_node_stat_2(ir_node *node, void *env) {
1019 graph_entry_t *graph = env;
1021 /* check for properties that depends on calls like recursion/leaf/indirect call */
1023 stat_update_call_2(node, graph);
1024 } /* update_node_stat_2 */
1027 * Get the current address mark.
1029 static unsigned get_adr_mark(graph_entry_t *graph, ir_node *node) {
1030 address_mark_entry_t *value = set_find(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
1032 return value ? value->mark : 0;
1033 } /* get_adr_mark */
1036 * Set the current address mark.
1038 static void set_adr_mark(graph_entry_t *graph, ir_node *node, unsigned val) {
1039 address_mark_entry_t *value = set_insert(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
1042 } /* set_adr_mark */
1044 #undef DUMP_ADR_MODE
1046 #ifdef DUMP_ADR_MODE
1048 * a vcg attribute hook: Color a node with a different color if
1049 * it's identified as a part of an address expression or at least referenced
1050 * by an address expression.
1052 static int stat_adr_mark_hook(FILE *F, ir_node *node, ir_node *local)
1054 ir_node *n = local ? local : node;
1055 ir_graph *irg = get_irn_irg(n);
1056 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1057 unsigned mark = get_adr_mark(graph, n);
1059 if (mark & MARK_ADDRESS_CALC)
1060 fprintf(F, "color: purple");
1061 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1062 fprintf(F, "color: pink");
1063 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1064 fprintf(F, "color: lightblue");
1068 /* I know the color! */
1070 } /* stat_adr_mark_hook */
1071 #endif /* DUMP_ADR_MODE */
1074 * Return the "operational" mode of a Firm node.
1076 static ir_mode *get_irn_op_mode(ir_node *node) {
1077 switch (get_irn_opcode(node)) {
1079 return get_Load_mode(node);
1081 return get_irn_mode(get_Store_value(node));
1083 return get_irn_mode(get_DivMod_left(node));
1085 return get_irn_mode(get_Div_left(node));
1087 return get_irn_mode(get_Mod_left(node));
1089 /* Cmp is no address calculation, or is it? */
1091 return get_irn_mode(node);
1093 } /* get_irn_op_mode */
1096 * Post-walker that marks every node that is an address calculation.
1098 * Users of a node must be visited first. We ensure this by
1099 * calling it in the post of an outs walk. This should work even in cycles,
1100 * while the normal pre-walk will not.
1102 static void mark_address_calc(ir_node *node, void *env) {
1103 graph_entry_t *graph = env;
1104 ir_mode *mode = get_irn_op_mode(node);
1106 unsigned mark_preds = MARK_REF_NON_ADR;
1108 if (! mode_is_data(mode))
1111 if (mode_is_reference(mode)) {
1112 /* a reference is calculated here, we are sure */
1113 set_adr_mark(graph, node, MARK_ADDRESS_CALC);
1115 mark_preds = MARK_REF_ADR;
1117 unsigned mark = get_adr_mark(graph, node);
1119 if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR) {
1121 * this node has no reference mode, but is only
1122 * referenced by address calculations
1124 mark_preds = MARK_REF_ADR;
1128 /* mark all predecessors */
1129 for (i = 0, n = get_irn_arity(node); i < n; ++i) {
1130 ir_node *pred = get_irn_n(node, i);
1132 mode = get_irn_op_mode(pred);
1133 if (! mode_is_data(mode))
1136 set_adr_mark(graph, pred, get_adr_mark(graph, pred) | mark_preds);
1138 } /* mark_address_calc */
1141 * Post-walker that marks every node that is an address calculation.
1143 * Users of a node must be visited first. We ensure this by
1144 * calling it in the post of an outs walk. This should work even in cycles,
1145 * while the normal pre-walk will not.
1147 static void count_adr_ops(ir_node *node, void *env) {
1148 graph_entry_t *graph = env;
1149 unsigned mark = get_adr_mark(graph, node);
1151 if (mark & MARK_ADDRESS_CALC)
1152 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1153 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1154 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1155 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1156 cnt_inc(&graph->cnt[gcnt_all_adr_ops]);
1157 } /* count_adr_ops */
1160 * Called for every graph when the graph is either deleted or stat_dump_snapshot()
1161 * is called, must recalculate all statistic info.
1163 * @param global The global entry
1164 * @param graph The current entry
1166 static void update_graph_stat(graph_entry_t *global, graph_entry_t *graph)
1168 node_entry_t *entry;
1171 /* clear first the alive counter in the graph */
1172 foreach_pset(graph->opcode_hash, entry) {
1173 cnt_clr(&entry->cnt_alive);
1174 } /* foreach_pset */
1176 /* set pessimistic values */
1178 graph->is_leaf_call = LCS_UNKNOWN;
1179 graph->is_recursive = 0;
1180 graph->is_chain_call = 1;
1182 /* create new block counter */
1183 graph->block_hash = new_pset(block_cmp, 5);
1185 /* we need dominator info */
1186 if (graph->irg != get_const_code_irg()) {
1187 assure_doms(graph->irg);
1189 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
1190 /* we need extended basic blocks */
1191 compute_extbb(graph->irg);
1193 /* create new extbb counter */
1194 graph->extbb_hash = new_pset(block_cmp, 5);
1198 /* count the nodes in the graph */
1199 irg_walk_graph(graph->irg, update_node_stat, NULL, graph);
1202 /* Uncomment this code if chain-call means call exact one. */
1203 entry = opcode_get_entry(op_Call, graph->opcode_hash);
1205 /* check if we have more than 1 call */
1206 if (cnt_gt(entry->cnt_alive, 1))
1207 graph->is_chain_call = 0;
1210 /* recursive functions are never chain calls, leafs don't have calls */
1211 if (graph->is_recursive || graph->is_leaf)
1212 graph->is_chain_call = 0;
1214 /* assume we walk every graph only ONCE, we could sum here the global count */
1215 foreach_pset(graph->opcode_hash, entry) {
1216 node_entry_t *g_entry = opcode_get_entry(entry->op, global->opcode_hash);
1218 /* update the node counter */
1219 cnt_add(&g_entry->cnt_alive, &entry->cnt_alive);
1220 } /* foreach_pset */
1222 /* count the number of address calculation */
1223 if (graph->irg != get_const_code_irg()) {
1224 ir_graph *rem = current_ir_graph;
1226 assure_irg_outs(graph->irg);
1228 /* Must be done an the outs graph */
1229 current_ir_graph = graph->irg;
1230 irg_out_walk(get_irg_start(graph->irg), NULL, mark_address_calc, graph);
1231 current_ir_graph = rem;
1233 #ifdef DUMP_ADR_MODE
1234 /* register the vcg hook and dump the graph for test */
1235 set_dump_node_vcgattr_hook(stat_adr_mark_hook);
1236 dump_ir_block_graph(graph->irg, "-adr");
1237 set_dump_node_vcgattr_hook(NULL);
1238 #endif /* DUMP_ADR_MODE */
1240 irg_walk_graph(graph->irg, NULL, count_adr_ops, graph);
1243 /* count the DAG's */
1244 if (status->stat_options & FIRMSTAT_COUNT_DAG)
1245 count_dags_in_graph(global, graph);
1247 /* calculate the patterns of this graph */
1248 stat_calc_pattern_history(graph->irg);
1250 /* leaf function did not call others */
1252 graph->is_leaf_call = LCS_NON_LEAF_CALL;
1253 else if (graph->is_leaf_call == LCS_UNKNOWN) {
1254 /* we still don't know if this graph calls leaf-functions, so enqueue */
1255 pdeq_putl(status->wait_q, graph);
1258 /* we have analyzed this graph */
1259 graph->is_analyzed = 1;
1261 /* accumulate all counter's */
1262 for (i = 0; i < _gcnt_last; ++i)
1263 cnt_add(&global->cnt[i], &graph->cnt[i]);
1264 } /* update_graph_stat */
1267 * Called for every graph that was on the wait_q in stat_dump_snapshot()
1268 * must finish all statistic info calculations.
1270 * @param global The global entry
1271 * @param graph The current entry
1273 static void update_graph_stat_2(graph_entry_t *global, graph_entry_t *graph)
1276 if (graph->is_deleted) {
1277 /* deleted, ignore */
1282 /* count the nodes in the graph */
1283 irg_walk_graph(graph->irg, update_node_stat_2, NULL, graph);
1285 if (graph->is_leaf_call == LCS_UNKNOWN)
1286 graph->is_leaf_call = LCS_LEAF_CALL;
1288 } /* update_graph_stat_2 */
1291 * Register a dumper.
1293 static void stat_register_dumper(const dumper_t *dumper) {
1294 dumper_t *p = xmalloc(sizeof(*p));
1297 memcpy(p, dumper, sizeof(*p));
1299 p->next = status->dumper;
1304 /* FIXME: memory leak */
1305 } /* stat_register_dumper */
1308 * Dumps the statistics of an IR graph.
1310 static void stat_dump_graph(graph_entry_t *entry) {
1313 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1314 if (dumper->dump_graph)
1315 dumper->dump_graph(dumper, entry);
1317 } /* stat_dump_graph */
1320 * Calls all registered dumper functions.
1322 static void stat_dump_registered(graph_entry_t *entry) {
1325 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1326 if (dumper->func_map) {
1327 dump_graph_FUNC func;
1329 foreach_pset(dumper->func_map, func)
1330 func(dumper, entry);
1333 } /* stat_dump_registered */
1336 * Dumps a constant table.
1338 static void stat_dump_consts(const constant_info_t *tbl) {
1341 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1342 if (dumper->dump_const_tbl)
1343 dumper->dump_const_tbl(dumper, tbl);
1345 } /* stat_dump_consts */
1348 * Dumps the parameter distribution
1350 static void stat_dump_param_tbl(const distrib_tbl_t *tbl, graph_entry_t *global) {
1353 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1354 if (dumper->dump_param_tbl)
1355 dumper->dump_param_tbl(dumper, tbl, global);
1357 } /* stat_dump_param_tbl */
1360 * Dumps the optimization counter
1362 static void stat_dump_opt_cnt(const counter_t *tbl, unsigned len) {
1365 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1366 if (dumper->dump_opt_cnt)
1367 dumper->dump_opt_cnt(dumper, tbl, len);
1369 } /* stat_dump_opt_cnt */
1372 * Initialize the dumper.
1374 static void stat_dump_init(const char *name) {
1377 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1379 dumper->init(dumper, name);
1381 } /* stat_dump_init */
1384 * Finish the dumper.
1386 static void stat_dump_finish(void) {
1389 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1391 dumper->finish(dumper);
1393 } /* stat_dump_finish */
1396 * Register an additional function for all dumper.
1398 void stat_register_dumper_func(dump_graph_FUNC func) {
1401 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1402 if (! dumper->func_map)
1403 dumper->func_map = pset_new_ptr(3);
1404 pset_insert_ptr(dumper->func_map, func);
1406 } /* stat_register_dumper_func */
1408 /* ---------------------------------------------------------------------- */
1411 * Helper: get an ir_op from an opcode.
1413 ir_op *stat_get_op_from_opcode(ir_opcode code) {
1414 return opcode_find_entry(code, status->ir_op_hash);
1415 } /* stat_get_op_from_opcode */
1418 * Hook: A new IR op is registered.
1420 * @param ctx the hook context
1421 * @param op the new IR opcode that was created.
1423 static void stat_new_ir_op(void *ctx, ir_op *op) {
1425 if (! status->stat_options)
1430 graph_entry_t *graph = graph_get_entry(NULL, status->irg_hash);
1432 /* execute for side effect :-) */
1433 (void)opcode_get_entry(op, graph->opcode_hash);
1435 pset_insert(status->ir_op_hash, op, op->code);
1438 } /* stat_new_ir_op */
1441 * Hook: An IR op is freed.
1443 * @param ctx the hook context
1444 * @param op the IR opcode that is freed
1446 static void stat_free_ir_op(void *ctx, ir_op *op) {
1449 if (! status->stat_options)
1456 } /* stat_free_ir_op */
1459 * Hook: A new node is created.
1461 * @param ctx the hook context
1462 * @param irg the IR graph on which the node is created
1463 * @param node the new IR node that was created
1465 static void stat_new_node(void *ctx, ir_graph *irg, ir_node *node) {
1468 if (! status->stat_options)
1471 /* do NOT count during dead node elimination */
1472 if (status->in_dead_node_elim)
1477 node_entry_t *entry;
1478 graph_entry_t *graph;
1479 ir_op *op = stat_get_irn_op(node);
1481 /* increase global value */
1482 graph = graph_get_entry(NULL, status->irg_hash);
1483 entry = opcode_get_entry(op, graph->opcode_hash);
1484 cnt_inc(&entry->new_node);
1486 /* increase local value */
1487 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1488 entry = opcode_get_entry(op, graph->opcode_hash);
1489 cnt_inc(&entry->new_node);
1492 } /* stat_new_node */
1495 * Hook: A node is changed into a Id node
1497 * @param ctx the hook context
1498 * @param node the IR node that will be turned into an ID
1500 static void stat_turn_into_id(void *ctx, ir_node *node) {
1502 if (! status->stat_options)
1507 node_entry_t *entry;
1508 graph_entry_t *graph;
1509 ir_op *op = stat_get_irn_op(node);
1511 /* increase global value */
1512 graph = graph_get_entry(NULL, status->irg_hash);
1513 entry = opcode_get_entry(op, graph->opcode_hash);
1514 cnt_inc(&entry->into_Id);
1516 /* increase local value */
1517 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1518 entry = opcode_get_entry(op, graph->opcode_hash);
1519 cnt_inc(&entry->into_Id);
1522 } /* stat_turn_into_id */
1525 * Hook: A new graph was created
1527 * @param ctx the hook context
1528 * @param irg the new IR graph that was created
1529 * @param ent the entity of this graph
1531 static void stat_new_graph(void *ctx, ir_graph *irg, ir_entity *ent) {
1533 if (! status->stat_options)
1538 /* execute for side effect :-) */
1539 graph_entry_t * graph = graph_get_entry(irg, status->irg_hash);
1542 graph->is_deleted = 0;
1544 graph->is_leaf_call = 0;
1545 graph->is_recursive = 0;
1546 graph->is_chain_call = 0;
1547 graph->is_analyzed = 0;
1550 } /* stat_new_graph */
1553 * Hook: A graph will be deleted
1555 * @param ctx the hook context
1556 * @param irg the IR graph that will be deleted
1558 * Note that we still hold the information for this graph
1559 * in our hash maps, only a flag is set which prevents this
1560 * information from being changed, it's "frozen" from now.
1562 static void stat_free_graph(void *ctx, ir_graph *irg) {
1564 if (! status->stat_options)
1569 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1570 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1572 graph->is_deleted = 1;
1574 if (status->stat_options & FIRMSTAT_COUNT_DELETED) {
1575 /* count the nodes of the graph yet, it will be destroyed later */
1576 update_graph_stat(global, graph);
1580 } /* stat_free_graph */
1583 * Hook: A walk over a graph is initiated. Do not count walks from statistic code.
1585 * @param ctx the hook context
1586 * @param irg the IR graph that will be walked
1587 * @param pre the pre walker
1588 * @param post the post walker
1590 static void stat_irg_walk(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1595 if (! status->stat_options)
1600 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1602 cnt_inc(&graph->cnt[gcnt_acc_walked]);
1605 } /* stat_irg_walk */
1608 * Hook: A walk over a graph in block-wise order is initiated. Do not count walks from statistic code.
1610 * @param ctx the hook context
1611 * @param irg the IR graph that will be walked
1612 * @param pre the pre walker
1613 * @param post the post walker
1615 static void stat_irg_walk_blkwise(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1617 /* for now, do NOT differentiate between blockwise and normal */
1618 stat_irg_walk(ctx, irg, pre, post);
1619 } /* stat_irg_walk_blkwise */
1622 * Hook: A walk over the graph's blocks is initiated. Do not count walks from statistic code.
1624 * @param ctx the hook context
1625 * @param irg the IR graph that will be walked
1626 * @param node the IR node
1627 * @param pre the pre walker
1628 * @param post the post walker
1630 static void stat_irg_block_walk(void *ctx, ir_graph *irg, ir_node *node, generic_func *pre, generic_func *post)
1636 if (! status->stat_options)
1641 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1643 cnt_inc(&graph->cnt[gcnt_acc_walked_blocks]);
1646 } /* stat_irg_block_walk */
1649 * Called for every node that is removed due to an optimization.
1651 * @param n the IR node that will be removed
1652 * @param hmap the hash map containing ir_op* -> opt_entry_t*
1653 * @param kind the optimization kind
1655 static void removed_due_opt(ir_node *n, hmap_opt_entry_t *hmap, hook_opt_kind kind) {
1656 ir_op *op = stat_get_irn_op(n);
1657 opt_entry_t *entry = opt_get_entry(op, hmap);
1659 /* ignore CSE for Constants */
1660 if (kind == HOOK_OPT_CSE && (is_Const(n) || is_SymConst(n)))
1663 /* increase global value */
1664 cnt_inc(&entry->count);
1665 } /* removed_due_opt */
1668 * Hook: Some nodes were optimized into some others due to an optimization.
1670 * @param ctx the hook context
1672 static void stat_merge_nodes(
1674 ir_node **new_node_array, int new_num_entries,
1675 ir_node **old_node_array, int old_num_entries,
1679 if (! status->stat_options)
1685 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1687 cnt_inc(&status->num_opts[opt]);
1688 if (status->reassoc_run)
1689 opt = HOOK_OPT_REASSOC;
1691 for (i = 0; i < old_num_entries; ++i) {
1692 /* nodes might be in new and old, so if we found a node
1693 in both sets, this one is NOT removed */
1694 for (j = 0; j < new_num_entries; ++j) {
1695 if (old_node_array[i] == new_node_array[j])
1698 if (j >= new_num_entries) {
1701 /* sometimes we did not detect, that it is replaced by a Const */
1702 if (opt == HOOK_OPT_CONFIRM && new_num_entries == 1) {
1703 ir_op *op = get_irn_op(new_node_array[0]);
1705 if (op == op_Const || op == op_SymConst)
1706 xopt = HOOK_OPT_CONFIRM_C;
1709 removed_due_opt(old_node_array[i], graph->opt_hash[xopt], xopt);
1714 } /* stat_merge_nodes */
1717 * Hook: Reassociation is started/stopped.
1719 * @param ctx the hook context
1720 * @param flag if non-zero, reassociation is started else stopped
1722 static void stat_reassociate(void *ctx, int flag) {
1724 if (! status->stat_options)
1729 status->reassoc_run = flag;
1732 } /* stat_reassociate */
1735 * Hook: A node was lowered into other nodes
1737 * @param ctx the hook context
1738 * @param node the IR node that will be lowered
1740 static void stat_lower(void *ctx, ir_node *node) {
1742 if (! status->stat_options)
1747 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1749 removed_due_opt(node, graph->opt_hash[HOOK_LOWERED], HOOK_LOWERED);
1755 * Hook: A graph was inlined.
1757 * @param ctx the hook context
1758 * @param call the IR call that will re changed into the body of
1759 * the called IR graph
1760 * @param called_irg the IR graph representing the called routine
1762 static void stat_inline(void *ctx, ir_node *call, ir_graph *called_irg)
1765 if (! status->stat_options)
1770 ir_graph *irg = get_irn_irg(call);
1771 graph_entry_t *i_graph = graph_get_entry(called_irg, status->irg_hash);
1772 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1774 cnt_inc(&graph->cnt[gcnt_acc_got_inlined]);
1775 cnt_inc(&i_graph->cnt[gcnt_acc_was_inlined]);
1781 * Hook: A graph with tail-recursions was optimized.
1783 * @param ctx the hook context
1785 static void stat_tail_rec(void *ctx, ir_graph *irg, int n_calls) {
1787 if (! status->stat_options)
1792 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1794 graph->num_tail_recursion += n_calls;
1797 } /* stat_tail_rec */
1800 * Strength reduction was performed on an iteration variable.
1802 * @param ctx the hook context
1804 static void stat_strength_red(void *ctx, ir_graph *irg, ir_node *strong) {
1806 if (! status->stat_options)
1811 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1812 cnt_inc(&graph->cnt[gcnt_acc_strength_red]);
1814 removed_due_opt(strong, graph->opt_hash[HOOK_OPT_STRENGTH_RED], HOOK_OPT_STRENGTH_RED);
1817 } /* stat_strength_red */
1820 * Hook: Start/Stop the dead node elimination.
1822 * @param ctx the hook context
1824 static void stat_dead_node_elim(void *ctx, ir_graph *irg, int start) {
1827 if (! status->stat_options)
1830 status->in_dead_node_elim = (start != 0);
1831 } /* stat_dead_node_elim */
1834 * Hook: if-conversion was tried.
1836 static void stat_if_conversion(void *context, ir_graph *irg, ir_node *phi,
1837 int pos, ir_node *mux, if_result_t reason)
1843 if (! status->stat_options)
1848 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1850 cnt_inc(&graph->cnt[gcnt_if_conv + reason]);
1853 } /* stat_if_conversion */
1856 * Hook: real function call was optimized.
1858 static void stat_func_call(void *context, ir_graph *irg, ir_node *call)
1862 if (! status->stat_options)
1867 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1869 cnt_inc(&graph->cnt[gcnt_acc_real_func_call]);
1872 } /* stat_func_call */
1875 * Hook: A multiply was replaced by a series of Shifts/Adds/Subs.
1877 * @param ctx the hook context
1879 static void stat_arch_dep_replace_mul_with_shifts(void *ctx, ir_node *mul) {
1881 if (! status->stat_options)
1886 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1887 removed_due_opt(mul, graph->opt_hash[HOOK_OPT_ARCH_DEP], HOOK_OPT_ARCH_DEP);
1890 } /* stat_arch_dep_replace_mul_with_shifts */
1893 * Hook: A division by const was replaced.
1895 * @param ctx the hook context
1896 * @param node the division node that will be optimized
1898 static void stat_arch_dep_replace_division_by_const(void *ctx, ir_node *node) {
1900 if (! status->stat_options)
1905 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1906 removed_due_opt(node, graph->opt_hash[HOOK_OPT_ARCH_DEP], HOOK_OPT_ARCH_DEP);
1909 } /* stat_arch_dep_replace_division_by_const */
1912 * Update the register pressure of a block.
1914 * @param irg the irg containing the block
1915 * @param block the block for which the reg pressure should be set
1916 * @param pressure the pressure
1917 * @param class_name the name of the register class
1919 void stat_be_block_regpressure(ir_graph *irg, ir_node *block, int pressure, const char *class_name)
1921 if (! status->stat_options)
1926 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1927 be_block_entry_t *block_ent;
1928 reg_pressure_entry_t *rp_ent;
1930 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1931 rp_ent = obstack_alloc(&status->be_data, sizeof(*rp_ent));
1932 memset(rp_ent, 0, sizeof(*rp_ent));
1934 rp_ent->class_name = class_name;
1935 rp_ent->pressure = pressure;
1937 pset_insert(block_ent->reg_pressure, rp_ent, HASH_PTR(class_name));
1940 } /* stat_be_block_regpressure */
1943 * Update the distribution of ready nodes of a block
1945 * @param irg the irg containing the block
1946 * @param block the block for which the reg pressure should be set
1947 * @param num_ready the number of ready nodes
1949 void stat_be_block_sched_ready(ir_graph *irg, ir_node *block, int num_ready)
1951 if (! status->stat_options)
1956 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1957 be_block_entry_t *block_ent;
1959 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1961 /* increase the counter of corresponding number of ready nodes */
1962 stat_inc_int_distrib_tbl(block_ent->sched_ready, num_ready);
1965 } /* stat_be_block_sched_ready */
1968 * Update the permutation statistic of a block.
1970 * @param class_name the name of the register class
1971 * @param n_regs number of registers in the register class
1972 * @param perm the perm node
1973 * @param block the block containing the perm
1974 * @param size the size of the perm
1975 * @param real_size number of pairs with different registers
1977 void stat_be_block_stat_perm(const char *class_name, int n_regs, ir_node *perm, ir_node *block,
1978 int size, int real_size)
1980 if (! status->stat_options)
1985 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
1986 be_block_entry_t *block_ent;
1987 perm_class_entry_t *pc_ent;
1988 perm_stat_entry_t *ps_ent;
1990 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1991 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
1992 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
1994 pc_ent->n_regs = n_regs;
1996 /* update information */
1997 ps_ent->size = size;
1998 ps_ent->real_size = real_size;
2001 } /* stat_be_block_stat_perm */
2004 * Update the permutation statistic of a single perm.
2006 * @param class_name the name of the register class
2007 * @param perm the perm node
2008 * @param block the block containing the perm
2009 * @param is_chain 1 if chain, 0 if cycle
2010 * @param size length of the cycle/chain
2011 * @param n_ops the number of ops representing this cycle/chain after lowering
2013 void stat_be_block_stat_permcycle(const char *class_name, ir_node *perm, ir_node *block,
2014 int is_chain, int size, int n_ops)
2016 if (! status->stat_options)
2021 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
2022 be_block_entry_t *block_ent;
2023 perm_class_entry_t *pc_ent;
2024 perm_stat_entry_t *ps_ent;
2026 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
2027 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
2028 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
2031 ps_ent->n_copies += n_ops;
2032 stat_inc_int_distrib_tbl(ps_ent->chains, size);
2034 ps_ent->n_exchg += n_ops;
2035 stat_inc_int_distrib_tbl(ps_ent->cycles, size);
2039 } /* stat_be_block_stat_permcycle */
2041 /* Dumps a statistics snapshot. */
2042 void stat_dump_snapshot(const char *name, const char *phase)
2048 if (! status->stat_options)
2053 graph_entry_t *entry;
2054 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
2057 * The constant counter is only global, so we clear it here.
2058 * Note that it does NOT contain the constants in DELETED
2059 * graphs due to this.
2061 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2062 stat_const_clear(status);
2064 /* build the name */
2065 p = strrchr(name, '/');
2070 q = strrchr(name, '\\');
2072 /* NULL might be not the smallest pointer */
2073 if (q && (!p || q > p))
2081 if (l > (int) (sizeof(fname) - 1))
2082 l = sizeof(fname) - 1;
2084 memcpy(fname, name, l);
2090 strncat(fname, "firmstat-", sizeof(fname));
2091 strncat(fname, phase, sizeof(fname));
2092 strncat(fname, "-", sizeof(fname));
2093 strncat(fname, p, sizeof(fname));
2095 stat_dump_init(fname);
2097 /* calculate the graph statistics */
2098 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
2099 if (entry->irg == NULL) {
2100 /* special entry for the global count */
2103 if (! entry->is_deleted) {
2104 /* the graph is still alive, count the nodes on it */
2105 update_graph_stat(global, entry);
2109 /* some calculations are dependent, we pushed them on the wait_q */
2110 while (! pdeq_empty(status->wait_q)) {
2111 entry = pdeq_getr(status->wait_q);
2113 update_graph_stat_2(global, entry);
2116 /* dump per graph */
2117 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
2118 if (entry->irg == NULL) {
2119 /* special entry for the global count */
2123 if (! entry->is_deleted || status->stat_options & FIRMSTAT_COUNT_DELETED) {
2124 stat_dump_graph(entry);
2125 stat_dump_registered(entry);
2128 if (! entry->is_deleted) {
2129 /* clear the counter that are not accumulated */
2130 graph_clear_entry(entry, 0);
2135 stat_dump_graph(global);
2137 /* dump the const info */
2138 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2139 stat_dump_consts(&status->const_info);
2141 /* dump the parameter distribution */
2142 stat_dump_param_tbl(status->dist_param_cnt, global);
2144 /* dump the optimization counter and clear them */
2145 stat_dump_opt_cnt(status->num_opts, ARR_SIZE(status->num_opts));
2146 clear_optimization_counter();
2150 stat_finish_pattern_history(fname);
2152 /* clear the global counter here */
2154 node_entry_t *entry;
2156 for (entry = pset_first(global->opcode_hash); entry; entry = pset_next(global->opcode_hash)) {
2157 opcode_clear_entry(entry);
2159 /* clear all global counter */
2160 graph_clear_entry(global, 1);
2164 } /* stat_dump_snapshot */
2166 /** the hook entries for the Firm statistics module */
2167 static hook_entry_t stat_hooks[hook_last];
2169 /* initialize the statistics module. */
2170 void firm_init_stat(unsigned enable_options)
2172 #define X(a) a, sizeof(a)-1
2173 #define HOOK(h, fkt) \
2174 stat_hooks[h].hook._##h = fkt; register_hook(h, &stat_hooks[h])
2177 if (! (enable_options & FIRMSTAT_ENABLED))
2180 status = xmalloc(sizeof(*status));
2181 memset(status, 0, sizeof(*status));
2183 /* enable statistics */
2184 status->stat_options = enable_options & FIRMSTAT_ENABLED ? enable_options : 0;
2186 /* register all hooks */
2187 HOOK(hook_new_ir_op, stat_new_ir_op);
2188 HOOK(hook_free_ir_op, stat_free_ir_op);
2189 HOOK(hook_new_node, stat_new_node);
2190 HOOK(hook_turn_into_id, stat_turn_into_id);
2191 HOOK(hook_new_graph, stat_new_graph);
2192 HOOK(hook_free_graph, stat_free_graph);
2193 HOOK(hook_irg_walk, stat_irg_walk);
2194 HOOK(hook_irg_walk_blkwise, stat_irg_walk_blkwise);
2195 HOOK(hook_irg_block_walk, stat_irg_block_walk);
2196 HOOK(hook_merge_nodes, stat_merge_nodes);
2197 HOOK(hook_reassociate, stat_reassociate);
2198 HOOK(hook_lower, stat_lower);
2199 HOOK(hook_inline, stat_inline);
2200 HOOK(hook_tail_rec, stat_tail_rec);
2201 HOOK(hook_strength_red, stat_strength_red);
2202 HOOK(hook_dead_node_elim, stat_dead_node_elim);
2203 HOOK(hook_if_conversion, stat_if_conversion);
2204 HOOK(hook_func_call, stat_func_call);
2205 HOOK(hook_arch_dep_replace_mul_with_shifts, stat_arch_dep_replace_mul_with_shifts);
2206 HOOK(hook_arch_dep_replace_division_by_const, stat_arch_dep_replace_division_by_const);
2208 obstack_init(&status->cnts);
2209 obstack_init(&status->be_data);
2211 /* create the hash-tables */
2212 status->irg_hash = new_pset(graph_cmp, 8);
2213 status->ir_op_hash = new_pset(opcode_cmp_2, 1);
2215 /* create the wait queue */
2216 status->wait_q = new_pdeq();
2218 if (enable_options & FIRMSTAT_COUNT_STRONG_OP) {
2219 /* build the pseudo-ops */
2221 _op_Phi0.code = --num;
2222 _op_Phi0.name = new_id_from_chars(X("Phi0"));
2224 _op_PhiM.code = --num;
2225 _op_PhiM.name = new_id_from_chars(X("PhiM"));
2227 _op_ProjM.code = --num;
2228 _op_ProjM.name = new_id_from_chars(X("ProjM"));
2230 _op_MulC.code = --num;
2231 _op_MulC.name = new_id_from_chars(X("MulC"));
2233 _op_DivC.code = --num;
2234 _op_DivC.name = new_id_from_chars(X("DivC"));
2236 _op_ModC.code = --num;
2237 _op_ModC.name = new_id_from_chars(X("ModC"));
2239 _op_DivModC.code = --num;
2240 _op_DivModC.name = new_id_from_chars(X("DivModC"));
2242 _op_QuotC.code = --num;
2243 _op_QuotC.name = new_id_from_chars(X("QuotC"));
2245 status->op_Phi0 = &_op_Phi0;
2246 status->op_PhiM = &_op_PhiM;
2247 status->op_ProjM = &_op_ProjM;
2248 status->op_MulC = &_op_MulC;
2249 status->op_DivC = &_op_DivC;
2250 status->op_ModC = &_op_ModC;
2251 status->op_DivModC = &_op_DivModC;
2252 status->op_QuotC = &_op_QuotC;
2254 status->op_Phi0 = NULL;
2255 status->op_PhiM = NULL;
2256 status->op_ProjM = NULL;
2257 status->op_MulC = NULL;
2258 status->op_DivC = NULL;
2259 status->op_ModC = NULL;
2260 status->op_DivModC = NULL;
2261 status->op_QuotC = NULL;
2264 /* for Florian: count the Sel depth */
2265 if (enable_options & FIRMSTAT_COUNT_SELS) {
2266 _op_SelSel.code = --num;
2267 _op_SelSel.name = new_id_from_chars(X("Sel(Sel)"));
2269 _op_SelSelSel.code = --num;
2270 _op_SelSelSel.name = new_id_from_chars(X("Sel(Sel(Sel))"));
2272 status->op_SelSel = &_op_SelSel;
2273 status->op_SelSelSel = &_op_SelSelSel;
2275 status->op_SelSel = NULL;
2276 status->op_SelSelSel = NULL;
2279 /* register the dumper */
2280 stat_register_dumper(&simple_dumper);
2282 if (enable_options & FIRMSTAT_CSV_OUTPUT)
2283 stat_register_dumper(&csv_dumper);
2285 /* initialize the pattern hash */
2286 stat_init_pattern_history(enable_options & FIRMSTAT_PATTERN_ENABLED);
2288 /* initialize the Const options */
2289 if (enable_options & FIRMSTAT_COUNT_CONSTS)
2290 stat_init_const_cnt(status);
2292 /* distribution table for parameter counts */
2293 status->dist_param_cnt = stat_new_int_distrib_tbl();
2295 clear_optimization_counter();
2299 } /* firm_init_stat */
2302 * Frees all dumper structures.
2304 static void stat_term_dumper(void) {
2305 dumper_t *dumper, *next_dumper;
2307 for (dumper = status->dumper; dumper; /* iteration done in loop body */ ) {
2308 if (dumper->func_map)
2309 del_pset(dumper->func_map);
2311 next_dumper = dumper->next;
2313 dumper = next_dumper;
2315 } /* stat_term_dumper */
2318 /* Terminates the statistics module, frees all memory. */
2319 void stat_term(void) {
2320 if (status != (stat_info_t *)&status_disable) {
2321 obstack_free(&status->be_data, NULL);
2322 obstack_free(&status->cnts, NULL);
2327 status = (stat_info_t *)&status_disable;
2331 /* returns 1 if statistics were initialized, 0 otherwise */
2332 int stat_is_active(void) {
2333 return status != (stat_info_t *)&status_disable;
2334 } /* stat_is_active */
2338 /* initialize the statistics module. */
2339 void firm_init_stat(unsigned enable_options) {}
2341 /* Dumps a statistics snapshot */
2342 void stat_dump_snapshot(const char *name, const char *phase) {}
2344 /* terminates the statistics module, frees all memory */
2345 void stat_term(void);
2347 #endif /* FIRM_STATISTICS */