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 memory Proj node. */
77 static ir_op _op_ProjM;
80 static ir_op _op_SelSel;
82 /** A Sel of a Sel of a Sel */
83 static ir_op _op_SelSelSel;
85 /* ---------------------------------------------------------------------------------- */
87 /** Marks the begin of a statistic (hook) function. */
88 #define STAT_ENTER ++status->recursive
90 /** Marks the end of a statistic (hook) functions. */
91 #define STAT_LEAVE --status->recursive
93 /** Allows to enter a statistic function only when we are not already in a hook. */
94 #define STAT_ENTER_SINGLE do { if (status->recursive > 0) return; ++status->recursive; } while (0)
99 static const int status_disable = 0;
100 static stat_info_t *status = (stat_info_t *)&status_disable;
103 * Compare two elements of the opcode hash.
105 static int opcode_cmp(const void *elt, const void *key) {
106 const node_entry_t *e1 = elt;
107 const node_entry_t *e2 = key;
109 return e1->op->code - e2->op->code;
113 * Compare two elements of the graph hash.
115 static int graph_cmp(const void *elt, const void *key) {
116 const graph_entry_t *e1 = elt;
117 const graph_entry_t *e2 = key;
119 return e1->irg != e2->irg;
123 * Compare two elements of the optimization hash.
125 static int opt_cmp(const void *elt, const void *key) {
126 const opt_entry_t *e1 = elt;
127 const opt_entry_t *e2 = key;
129 return e1->op->code != e2->op->code;
133 * Compare two elements of the block/extbb hash.
135 static int block_cmp(const void *elt, const void *key) {
136 const block_entry_t *e1 = elt;
137 const block_entry_t *e2 = key;
139 /* it's enough to compare the block number */
140 return e1->block_nr != e2->block_nr;
144 * Compare two elements of the be_block hash.
146 static int be_block_cmp(const void *elt, const void *key) {
147 const be_block_entry_t *e1 = elt;
148 const be_block_entry_t *e2 = key;
150 return e1->block_nr != e2->block_nr;
154 * Compare two elements of reg pressure hash.
156 static int reg_pressure_cmp(const void *elt, const void *key) {
157 const reg_pressure_entry_t *e1 = elt;
158 const reg_pressure_entry_t *e2 = key;
160 return e1->class_name != e2->class_name;
161 } /* reg_pressure_cmp */
164 * Compare two elements of the perm_stat hash.
166 static int perm_stat_cmp(const void *elt, const void *key) {
167 const perm_stat_entry_t *e1 = elt;
168 const perm_stat_entry_t *e2 = key;
170 return e1->perm != e2->perm;
171 } /* perm_stat_cmp */
174 * Compare two elements of the perm_class hash.
176 static int perm_class_cmp(const void *elt, const void *key) {
177 const perm_class_entry_t *e1 = elt;
178 const perm_class_entry_t *e2 = 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) {
187 const ir_op *e1 = elt;
188 const ir_op *e2 = key;
190 return e1->code != e2->code;
194 * Compare two elements of the address_mark set.
196 static int address_mark_cmp(const void *elt, const void *key, size_t size) {
197 const address_mark_entry_t *e1 = elt;
198 const address_mark_entry_t *e2 = key;
201 /* compare only the nodes, the rest is used as data container */
202 return e1->node != e2->node;
203 } /* address_mark_cmp */
206 * Clear all counter in a node_entry_t.
208 static void opcode_clear_entry(node_entry_t *elem) {
209 cnt_clr(&elem->cnt_alive);
210 cnt_clr(&elem->new_node);
211 cnt_clr(&elem->into_Id);
212 } /* opcode_clear_entry */
215 * Returns the associates node_entry_t for an ir_op
217 * @param op the IR operation
218 * @param hmap a hash map containing ir_op* -> node_entry_t*
220 static node_entry_t *opcode_get_entry(const ir_op *op, hmap_node_entry_t *hmap) {
226 elem = pset_find(hmap, &key, op->code);
230 elem = obstack_alloc(&status->cnts, sizeof(*elem));
231 memset(elem, 0, sizeof(*elem));
234 opcode_clear_entry(elem);
238 return pset_insert(hmap, elem, op->code);
239 } /* opcode_get_entry */
242 * Returns the associates ir_op for an opcode
244 * @param code the IR opcode
245 * @param hmap the hash map containing opcode -> ir_op*
247 static ir_op *opcode_find_entry(ir_opcode code, hmap_ir_op *hmap) {
251 return pset_find(hmap, &key, code);
252 } /* opcode_find_entry */
255 * Clears all counter in a graph_entry_t.
257 * @param elem the graph entry
258 * @param all if non-zero, clears all counters, else leave accumulated ones
260 static void graph_clear_entry(graph_entry_t *elem, int all) {
263 /* clear accumulated / non-accumulated counter */
264 for (i = all ? 0 : _gcnt_non_acc; i < _gcnt_last; ++i) {
265 cnt_clr(&elem->cnt[i]);
268 if (elem->block_hash) {
269 del_pset(elem->block_hash);
270 elem->block_hash = NULL;
273 if (elem->extbb_hash) {
274 del_pset(elem->extbb_hash);
275 elem->extbb_hash = NULL;
278 obstack_free(&elem->recalc_cnts, NULL);
279 obstack_init(&elem->recalc_cnts);
280 } /* graph_clear_entry */
283 * Returns the associated graph_entry_t for an IR graph.
285 * @param irg the IR graph
286 * @param hmap the hash map containing ir_graph* -> graph_entry_t*
288 static graph_entry_t *graph_get_entry(ir_graph *irg, hmap_graph_entry_t *hmap)
296 elem = pset_find(hmap, &key, HASH_PTR(irg));
299 /* create hash map backend block information */
300 if (! elem->be_block_hash)
301 elem->be_block_hash = new_pset(be_block_cmp, 5);
306 /* allocate a new one */
307 elem = obstack_alloc(&status->cnts, sizeof(*elem));
308 memset(elem, 0, sizeof(*elem));
309 obstack_init(&elem->recalc_cnts);
312 graph_clear_entry(elem, 1);
314 /* new hash table for opcodes here */
315 elem->opcode_hash = new_pset(opcode_cmp, 5);
316 elem->address_mark = new_set(address_mark_cmp, 5);
319 /* these hash tables are created on demand */
320 elem->block_hash = NULL;
321 elem->extbb_hash = NULL;
323 for (i = 0; i < sizeof(elem->opt_hash)/sizeof(elem->opt_hash[0]); ++i)
324 elem->opt_hash[i] = new_pset(opt_cmp, 4);
326 return pset_insert(hmap, elem, HASH_PTR(irg));
327 } /* graph_get_entry */
330 * Clear all counter in an opt_entry_t.
332 static void opt_clear_entry(opt_entry_t *elem) {
333 cnt_clr(&elem->count);
334 } /* opt_clear_entry */
337 * Returns the associated opt_entry_t for an IR operation.
339 * @param op the IR operation
340 * @param hmap the hash map containing ir_op* -> opt_entry_t*
342 static opt_entry_t *opt_get_entry(const ir_op *op, hmap_opt_entry_t *hmap)
349 elem = pset_find(hmap, &key, op->code);
353 elem = obstack_alloc(&status->cnts, sizeof(*elem));
354 memset(elem, 0, sizeof(*elem));
356 /* clear new counter */
357 opt_clear_entry(elem);
361 return pset_insert(hmap, elem, op->code);
362 } /* opt_get_entry */
365 * clears all counter in a block_entry_t
367 static void block_clear_entry(block_entry_t *elem) {
370 for (i = 0; i < _bcnt_last; ++i)
371 cnt_clr(&elem->cnt[i]);
372 } /* block_clear_entry */
375 * Returns the associated block_entry_t for an block.
377 * @param block_nr an IR block number
378 * @param hmap a hash map containing long -> block_entry_t
380 static block_entry_t *block_get_entry(struct obstack *obst, long block_nr, hmap_block_entry_t *hmap)
385 key.block_nr = block_nr;
387 elem = pset_find(hmap, &key, block_nr);
391 elem = obstack_alloc(obst, sizeof(*elem));
392 memset(elem, 0, sizeof(*elem));
394 /* clear new counter */
395 block_clear_entry(elem);
397 elem->block_nr = block_nr;
399 return pset_insert(hmap, elem, block_nr);
400 } /* block_get_entry */
403 * Clear all sets in be_block_entry_t.
405 static void be_block_clear_entry(be_block_entry_t *elem)
407 if (elem->reg_pressure)
408 del_pset(elem->reg_pressure);
410 if (elem->sched_ready)
411 stat_delete_distrib_tbl(elem->sched_ready);
413 if (elem->perm_class_stat)
414 del_pset(elem->perm_class_stat);
416 elem->reg_pressure = new_pset(reg_pressure_cmp, 5);
417 elem->sched_ready = stat_new_int_distrib_tbl();
418 elem->perm_class_stat = new_pset(perm_class_cmp, 5);
419 } /* be_block_clear_entry */
422 * Returns the associated be_block_entry_t for an block.
424 * @param block_nr an IR block number
425 * @param hmap a hash map containing long -> be_block_entry_t
427 static be_block_entry_t *be_block_get_entry(struct obstack *obst, long block_nr, hmap_be_block_entry_t *hmap)
429 be_block_entry_t key;
430 be_block_entry_t *elem;
432 key.block_nr = block_nr;
434 elem = pset_find(hmap, &key, block_nr);
438 elem = obstack_alloc(obst, sizeof(*elem));
439 memset(elem, 0, sizeof(*elem));
441 /* clear new counter */
442 be_block_clear_entry(elem);
444 elem->block_nr = block_nr;
446 return pset_insert(hmap, elem, block_nr);
447 } /* be_block_get_entry */
450 * clears all sets in perm_class_entry_t
452 static void perm_class_clear_entry(perm_class_entry_t *elem) {
454 del_pset(elem->perm_stat);
456 elem->perm_stat = new_pset(perm_stat_cmp, 5);
457 } /* perm_class_clear_entry */
460 * Returns the associated perm_class entry for a register class.
462 * @param class_name the register class name
463 * @param hmap a hash map containing class_name -> perm_class_entry_t
465 static perm_class_entry_t *perm_class_get_entry(struct obstack *obst, const char *class_name,
466 hmap_perm_class_entry_t *hmap)
468 perm_class_entry_t key;
469 perm_class_entry_t *elem;
471 key.class_name = class_name;
473 elem = pset_find(hmap, &key, HASH_PTR(class_name));
477 elem = obstack_alloc(obst, sizeof(*elem));
478 memset(elem, 0, sizeof(*elem));
480 /* clear new counter */
481 perm_class_clear_entry(elem);
483 elem->class_name = class_name;
485 return pset_insert(hmap, elem, HASH_PTR(class_name));
486 } /* perm_class_get_entry */
489 * clears all sets in perm_stat_entry_t
491 static void perm_stat_clear_entry(perm_stat_entry_t *elem) {
493 stat_delete_distrib_tbl(elem->chains);
496 stat_delete_distrib_tbl(elem->cycles);
498 elem->chains = stat_new_int_distrib_tbl();
499 elem->cycles = stat_new_int_distrib_tbl();
500 } /* perm_stat_clear_entry */
503 * Returns the associated perm_stat entry for a perm.
505 * @param perm the perm node
506 * @param hmap a hash map containing perm -> perm_stat_entry_t
508 static perm_stat_entry_t *perm_stat_get_entry(struct obstack *obst, ir_node *perm, hmap_perm_stat_entry_t *hmap)
510 perm_stat_entry_t key;
511 perm_stat_entry_t *elem;
515 elem = pset_find(hmap, &key, HASH_PTR(perm));
519 elem = obstack_alloc(obst, sizeof(*elem));
520 memset(elem, 0, sizeof(*elem));
522 /* clear new counter */
523 perm_stat_clear_entry(elem);
527 return pset_insert(hmap, elem, HASH_PTR(perm));
528 } /* perm_stat_get_entry */
531 * Clear optimizations counter,
533 static void clear_optimization_counter(void) {
535 for (i = 0; i < FS_OPT_MAX; ++i)
536 cnt_clr(&status->num_opts[i]);
540 * Returns the ir_op for an IR-node,
541 * handles special cases and return pseudo op codes.
543 * @param none an IR node
545 static ir_op *stat_get_irn_op(ir_node *node)
547 ir_op *op = get_irn_op(node);
548 ir_opcode opc = op->code;
552 if (get_irn_arity(node) == 0) {
553 /* special case, a Phi0 node, count on extra counter */
554 op = status->op_Phi0 ? status->op_Phi0 : op;
555 } else if (get_irn_mode(node) == mode_M) {
556 /* special case, a Memory Phi node, count on extra counter */
557 op = status->op_PhiM ? status->op_PhiM : op;
561 if (get_irn_mode(node) == mode_M) {
562 /* special case, a Memory Proj node, count on extra counter */
563 op = status->op_ProjM ? status->op_ProjM : op;
567 if (get_irn_op(get_Mul_left(node)) == op_Const || get_irn_op(get_Mul_right(node)) == op_Const) {
568 /* special case, a Multiply by a const, count on extra counter */
569 op = status->op_MulC ? status->op_MulC : op;
573 if (get_irn_op(get_Div_right(node)) == op_Const) {
574 /* special case, a division by a const, count on extra counter */
575 op = status->op_DivC ? status->op_DivC : op;
579 if (get_irn_op(get_Mod_right(node)) == op_Const) {
580 /* special case, a module by a const, count on extra counter */
581 op = status->op_ModC ? status->op_ModC : op;
585 if (get_irn_op(get_DivMod_right(node)) == op_Const) {
586 /* special case, a division/modulo by a const, count on extra counter */
587 op = status->op_DivModC ? status->op_DivModC : op;
591 if (get_irn_op(get_Sel_ptr(node)) == op_Sel) {
592 /* special case, a Sel of a Sel, count on extra counter */
593 op = status->op_SelSel ? status->op_SelSel : op;
594 if (get_irn_op(get_Sel_ptr(get_Sel_ptr(node))) == op_Sel) {
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 */
716 if (get_irn_op(node) == op_End)
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) {
741 int i, num_const_args = 0, num_local_adr = 0;
742 int n = get_Call_n_params(call);
744 for (i = 0; i < n; ++i) {
745 ir_node *param = get_Call_param(call, i);
747 if (is_irn_constlike(param))
749 else if (is_Sel(param)) {
750 ir_node *base = param;
753 base = get_Sel_ptr(base);
754 } while (is_Sel(base));
756 if (base == get_irg_frame(current_ir_graph))
762 if (num_const_args > 0)
763 cnt_inc(&graph->cnt[gcnt_call_with_cnst_arg]);
764 if (num_const_args == n)
765 cnt_inc(&graph->cnt[gcnt_call_with_all_cnst_arg]);
766 if (num_local_adr > 0)
767 cnt_inc(&graph->cnt[gcnt_call_with_local_adr]);
769 stat_inc_int_distrib_tbl(status->dist_param_cnt, n);
770 } /* analyse_params_of_Call */
773 * Update info on calls.
775 * @param call The call
776 * @param graph The graph entry containing the call
778 static void stat_update_call(ir_node *call, graph_entry_t *graph)
780 ir_node *block = get_nodes_block(call);
781 ir_node *ptr = get_Call_ptr(call);
782 ir_entity *ent = NULL;
783 ir_graph *callee = NULL;
786 * If the block is bad, the whole subgraph will collapse later
787 * so do not count this call.
788 * This happens in dead code.
793 cnt_inc(&graph->cnt[gcnt_all_calls]);
795 /* found a call, this function is not a leaf */
798 if (get_irn_op(ptr) == op_SymConst) {
799 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
800 /* ok, we seems to know the entity */
801 ent = get_SymConst_entity(ptr);
802 callee = get_entity_irg(ent);
804 /* it is recursive, if it calls at least once */
805 if (callee == graph->irg)
806 graph->is_recursive = 1;
808 cnt_inc(&graph->cnt[gcnt_external_calls]);
811 /* indirect call, be could not predict */
812 cnt_inc(&graph->cnt[gcnt_indirect_calls]);
814 /* NOT a leaf call */
815 graph->is_leaf_call = LCS_NON_LEAF_CALL;
818 /* check, if it's a chain-call: Then, the call-block
819 * must dominate the end block. */
821 ir_node *curr = get_irg_end_block(graph->irg);
822 int depth = get_Block_dom_depth(block);
824 for (; curr != block && get_Block_dom_depth(curr) > depth;) {
825 curr = get_Block_idom(curr);
827 if (! curr || is_no_Block(curr))
832 graph->is_chain_call = 0;
835 /* check, if the callee is a leaf */
837 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
839 if (called->is_analyzed) {
840 if (! called->is_leaf)
841 graph->is_leaf_call = LCS_NON_LEAF_CALL;
845 analyse_params_of_Call(graph, call);
846 } /* stat_update_call */
849 * Update info on calls for graphs on the wait queue.
851 static void stat_update_call_2(ir_node *call, graph_entry_t *graph)
853 ir_node *block = get_nodes_block(call);
854 ir_node *ptr = get_Call_ptr(call);
855 ir_entity *ent = NULL;
856 ir_graph *callee = NULL;
859 * If the block is bad, the whole subgraph will collapse later
860 * so do not count this call.
861 * This happens in dead code.
866 if (get_irn_op(ptr) == op_SymConst) {
867 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
868 /* ok, we seems to know the entity */
869 ent = get_SymConst_entity(ptr);
870 callee = get_entity_irg(ent);
874 /* check, if the callee is a leaf */
876 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
878 assert(called->is_analyzed);
880 if (! called->is_leaf)
881 graph->is_leaf_call = LCS_NON_LEAF_CALL;
883 graph->is_leaf_call = LCS_NON_LEAF_CALL;
884 } /* stat_update_call_2 */
887 * Find the base address and entity of an Sel node.
889 * @param sel the node
891 * @return the base address.
893 static ir_node *find_base_adr(ir_node *sel) {
894 ir_node *ptr = get_Sel_ptr(sel);
896 while (is_Sel(ptr)) {
898 ptr = get_Sel_ptr(sel);
901 } /* find_base_adr */
904 * Update info on Load/Store address statistics.
906 static void stat_update_address(ir_node *node, graph_entry_t *graph) {
907 ir_opcode opc = get_irn_opcode(node);
913 /* a global address */
914 cnt_inc(&graph->cnt[gcnt_global_adr]);
917 base = find_base_adr(node);
918 irg = current_ir_graph;
919 if (base == get_irg_tls(irg)) {
920 /* a TLS variable, like a global. */
921 cnt_inc(&graph->cnt[gcnt_global_adr]);
922 } else if (base == get_irg_frame(irg)) {
923 /* a local Variable. */
924 cnt_inc(&graph->cnt[gcnt_local_adr]);
927 if (is_Proj(base) && skip_Proj(get_Proj_pred(base)) == get_irg_start(irg)) {
928 /* pointer access through parameter, check for THIS */
929 ir_entity *ent = get_irg_entity(irg);
932 ir_type *ent_tp = get_entity_type(ent);
934 if (get_method_calling_convention(ent_tp) & cc_this_call) {
935 if (get_Proj_proj(base) == 0) {
937 cnt_inc(&graph->cnt[gcnt_this_adr]);
942 /* other parameter */
943 cnt_inc(&graph->cnt[gcnt_param_adr]);
946 /* unknown Pointer access */
947 cnt_inc(&graph->cnt[gcnt_other_adr]);
953 } /* stat_update_address */
956 * Walker for reachable nodes count.
958 static void update_node_stat(ir_node *node, void *env)
960 graph_entry_t *graph = env;
963 ir_op *op = stat_get_irn_op(node);
964 int arity = get_irn_arity(node);
966 entry = opcode_get_entry(op, graph->opcode_hash);
968 cnt_inc(&entry->cnt_alive);
969 cnt_add_i(&graph->cnt[gcnt_edges], arity);
971 /* count block edges */
972 undate_block_info(node, graph);
974 /* count extended block edges */
975 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
976 if (graph->irg != get_const_code_irg())
977 update_extbb_info(node, graph);
980 /* handle statistics for special node types */
984 if (status->stat_options & FIRMSTAT_COUNT_CONSTS) {
985 /* check properties of constants */
986 stat_update_const(status, node, graph);
990 /* check for properties that depends on calls like recursion/leaf/indirect call */
991 stat_update_call(node, graph);
994 /* check address properties */
995 stat_update_address(get_Load_ptr(node), graph);
998 /* check address properties */
999 stat_update_address(get_Store_ptr(node), graph);
1004 } /* update_node_stat */
1007 * Walker for reachable nodes count for graphs on the wait_q.
1009 static void update_node_stat_2(ir_node *node, void *env) {
1010 graph_entry_t *graph = env;
1012 /* check for properties that depends on calls like recursion/leaf/indirect call */
1014 stat_update_call_2(node, graph);
1015 } /* update_node_stat_2 */
1018 * Get the current address mark.
1020 static unsigned get_adr_mark(graph_entry_t *graph, ir_node *node) {
1021 address_mark_entry_t *value = set_find(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
1023 return value ? value->mark : 0;
1024 } /* get_adr_mark */
1027 * Set the current address mark.
1029 static void set_adr_mark(graph_entry_t *graph, ir_node *node, unsigned val) {
1030 address_mark_entry_t *value = set_insert(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
1033 } /* set_adr_mark */
1035 #undef DUMP_ADR_MODE
1037 #ifdef DUMP_ADR_MODE
1039 * a vcg attribute hook: Color a node with a different color if
1040 * it's identified as a part of an address expression or at least referenced
1041 * by an address expression.
1043 static int stat_adr_mark_hook(FILE *F, ir_node *node, ir_node *local)
1045 ir_node *n = local ? local : node;
1046 ir_graph *irg = get_irn_irg(n);
1047 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1048 unsigned mark = get_adr_mark(graph, n);
1050 if (mark & MARK_ADDRESS_CALC)
1051 fprintf(F, "color: purple");
1052 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1053 fprintf(F, "color: pink");
1054 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1055 fprintf(F, "color: lightblue");
1059 /* I know the color! */
1061 } /* stat_adr_mark_hook */
1062 #endif /* DUMP_ADR_MODE */
1065 * Return the "operational" mode of a Firm node.
1067 static ir_mode *get_irn_op_mode(ir_node *node) {
1068 switch (get_irn_opcode(node)) {
1070 return get_Load_mode(node);
1072 return get_irn_mode(get_Store_value(node));
1074 return get_irn_mode(get_DivMod_left(node));
1076 return get_irn_mode(get_Div_left(node));
1078 return get_irn_mode(get_Mod_left(node));
1080 /* Cmp is no address calculation, or is it? */
1082 return get_irn_mode(node);
1084 } /* get_irn_op_mode */
1087 * Post-walker that marks every node that is an address calculation.
1089 * Users of a node must be visited first. We ensure this by
1090 * calling it in the post of an outs walk. This should work even in cycles,
1091 * while the normal pre-walk will not.
1093 static void mark_address_calc(ir_node *node, void *env) {
1094 graph_entry_t *graph = env;
1095 ir_mode *mode = get_irn_op_mode(node);
1097 unsigned mark_preds = MARK_REF_NON_ADR;
1099 if (! mode_is_data(mode))
1102 if (mode_is_reference(mode)) {
1103 /* a reference is calculated here, we are sure */
1104 set_adr_mark(graph, node, MARK_ADDRESS_CALC);
1106 mark_preds = MARK_REF_ADR;
1108 unsigned mark = get_adr_mark(graph, node);
1110 if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR) {
1112 * this node has no reference mode, but is only
1113 * referenced by address calculations
1115 mark_preds = MARK_REF_ADR;
1119 /* mark all predecessors */
1120 for (i = 0, n = get_irn_arity(node); i < n; ++i) {
1121 ir_node *pred = get_irn_n(node, i);
1123 mode = get_irn_op_mode(pred);
1124 if (! mode_is_data(mode))
1127 set_adr_mark(graph, pred, get_adr_mark(graph, pred) | mark_preds);
1129 } /* mark_address_calc */
1132 * Post-walker that marks every node that is an address calculation.
1134 * Users of a node must be visited first. We ensure this by
1135 * calling it in the post of an outs walk. This should work even in cycles,
1136 * while the normal pre-walk will not.
1138 static void count_adr_ops(ir_node *node, void *env) {
1139 graph_entry_t *graph = env;
1140 unsigned mark = get_adr_mark(graph, node);
1142 if (mark & MARK_ADDRESS_CALC)
1143 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1144 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1145 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1146 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1147 cnt_inc(&graph->cnt[gcnt_all_adr_ops]);
1148 } /* count_adr_ops */
1151 * Called for every graph when the graph is either deleted or stat_dump_snapshot()
1152 * is called, must recalculate all statistic info.
1154 * @param global The global entry
1155 * @param graph The current entry
1157 static void update_graph_stat(graph_entry_t *global, graph_entry_t *graph)
1159 node_entry_t *entry;
1162 /* clear first the alive counter in the graph */
1163 foreach_pset(graph->opcode_hash, entry) {
1164 cnt_clr(&entry->cnt_alive);
1165 } /* foreach_pset */
1167 /* set pessimistic values */
1169 graph->is_leaf_call = LCS_UNKNOWN;
1170 graph->is_recursive = 0;
1171 graph->is_chain_call = 1;
1173 /* create new block counter */
1174 graph->block_hash = new_pset(block_cmp, 5);
1176 /* we need dominator info */
1177 if (graph->irg != get_const_code_irg()) {
1178 assure_doms(graph->irg);
1180 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
1181 /* we need extended basic blocks */
1182 compute_extbb(graph->irg);
1184 /* create new extbb counter */
1185 graph->extbb_hash = new_pset(block_cmp, 5);
1189 /* count the nodes in the graph */
1190 irg_walk_graph(graph->irg, update_node_stat, NULL, graph);
1193 /* Uncomment this code if chain-call means call exact one. */
1194 entry = opcode_get_entry(op_Call, graph->opcode_hash);
1196 /* check if we have more than 1 call */
1197 if (cnt_gt(entry->cnt_alive, 1))
1198 graph->is_chain_call = 0;
1201 /* recursive functions are never chain calls, leafs don't have calls */
1202 if (graph->is_recursive || graph->is_leaf)
1203 graph->is_chain_call = 0;
1205 /* assume we walk every graph only ONCE, we could sum here the global count */
1206 foreach_pset(graph->opcode_hash, entry) {
1207 node_entry_t *g_entry = opcode_get_entry(entry->op, global->opcode_hash);
1209 /* update the node counter */
1210 cnt_add(&g_entry->cnt_alive, &entry->cnt_alive);
1211 } /* foreach_pset */
1213 /* count the number of address calculation */
1214 if (graph->irg != get_const_code_irg()) {
1215 ir_graph *rem = current_ir_graph;
1217 assure_irg_outs(graph->irg);
1219 /* Must be done an the outs graph */
1220 current_ir_graph = graph->irg;
1221 irg_out_walk(get_irg_start(graph->irg), NULL, mark_address_calc, graph);
1222 current_ir_graph = rem;
1224 #ifdef DUMP_ADR_MODE
1225 /* register the vcg hook and dump the graph for test */
1226 set_dump_node_vcgattr_hook(stat_adr_mark_hook);
1227 dump_ir_block_graph(graph->irg, "-adr");
1228 set_dump_node_vcgattr_hook(NULL);
1229 #endif /* DUMP_ADR_MODE */
1231 irg_walk_graph(graph->irg, NULL, count_adr_ops, graph);
1234 /* count the DAG's */
1235 if (status->stat_options & FIRMSTAT_COUNT_DAG)
1236 count_dags_in_graph(global, graph);
1238 /* calculate the patterns of this graph */
1239 stat_calc_pattern_history(graph->irg);
1241 /* leaf function did not call others */
1243 graph->is_leaf_call = LCS_NON_LEAF_CALL;
1244 else if (graph->is_leaf_call == LCS_UNKNOWN) {
1245 /* we still don't know if this graph calls leaf-functions, so enqueue */
1246 pdeq_putl(status->wait_q, graph);
1249 /* we have analyzed this graph */
1250 graph->is_analyzed = 1;
1252 /* accumulate all counter's */
1253 for (i = 0; i < _gcnt_last; ++i)
1254 cnt_add(&global->cnt[i], &graph->cnt[i]);
1255 } /* update_graph_stat */
1258 * Called for every graph that was on the wait_q in stat_dump_snapshot()
1259 * must finish all statistic info calculations.
1261 * @param global The global entry
1262 * @param graph The current entry
1264 static void update_graph_stat_2(graph_entry_t *global, graph_entry_t *graph)
1267 if (graph->is_deleted) {
1268 /* deleted, ignore */
1273 /* count the nodes in the graph */
1274 irg_walk_graph(graph->irg, update_node_stat_2, NULL, graph);
1276 if (graph->is_leaf_call == LCS_UNKNOWN)
1277 graph->is_leaf_call = LCS_LEAF_CALL;
1279 } /* update_graph_stat_2 */
1282 * Register a dumper.
1284 static void stat_register_dumper(const dumper_t *dumper) {
1285 dumper_t *p = xmalloc(sizeof(*p));
1288 memcpy(p, dumper, sizeof(*p));
1290 p->next = status->dumper;
1295 /* FIXME: memory leak */
1296 } /* stat_register_dumper */
1299 * Dumps the statistics of an IR graph.
1301 static void stat_dump_graph(graph_entry_t *entry) {
1304 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1305 if (dumper->dump_graph)
1306 dumper->dump_graph(dumper, entry);
1308 } /* stat_dump_graph */
1311 * Calls all registered dumper functions.
1313 static void stat_dump_registered(graph_entry_t *entry) {
1316 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1317 if (dumper->func_map) {
1318 dump_graph_FUNC func;
1320 foreach_pset(dumper->func_map, func)
1321 func(dumper, entry);
1324 } /* stat_dump_registered */
1327 * Dumps a constant table.
1329 static void stat_dump_consts(const constant_info_t *tbl) {
1332 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1333 if (dumper->dump_const_tbl)
1334 dumper->dump_const_tbl(dumper, tbl);
1336 } /* stat_dump_consts */
1339 * Dumps the parameter distribution
1341 static void stat_dump_param_tbl(const distrib_tbl_t *tbl, graph_entry_t *global) {
1344 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1345 if (dumper->dump_param_tbl)
1346 dumper->dump_param_tbl(dumper, tbl, global);
1348 } /* stat_dump_param_tbl */
1351 * Dumps the optimization counter
1353 static void stat_dump_opt_cnt(const counter_t *tbl, unsigned len) {
1356 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1357 if (dumper->dump_opt_cnt)
1358 dumper->dump_opt_cnt(dumper, tbl, len);
1360 } /* stat_dump_opt_cnt */
1363 * Initialize the dumper.
1365 static void stat_dump_init(const char *name) {
1368 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1370 dumper->init(dumper, name);
1372 } /* stat_dump_init */
1375 * Finish the dumper.
1377 static void stat_dump_finish(void) {
1380 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1382 dumper->finish(dumper);
1384 } /* stat_dump_finish */
1387 * Register an additional function for all dumper.
1389 void stat_register_dumper_func(dump_graph_FUNC func) {
1392 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1393 if (! dumper->func_map)
1394 dumper->func_map = pset_new_ptr(3);
1395 pset_insert_ptr(dumper->func_map, func);
1397 } /* stat_register_dumper_func */
1399 /* ---------------------------------------------------------------------- */
1402 * Helper: get an ir_op from an opcode.
1404 ir_op *stat_get_op_from_opcode(ir_opcode code) {
1405 return opcode_find_entry(code, status->ir_op_hash);
1406 } /* stat_get_op_from_opcode */
1409 * Hook: A new IR op is registered.
1411 * @param ctx the hook context
1412 * @param op the new IR opcode that was created.
1414 static void stat_new_ir_op(void *ctx, ir_op *op) {
1416 if (! status->stat_options)
1421 graph_entry_t *graph = graph_get_entry(NULL, status->irg_hash);
1423 /* execute for side effect :-) */
1424 (void)opcode_get_entry(op, graph->opcode_hash);
1426 pset_insert(status->ir_op_hash, op, op->code);
1429 } /* stat_new_ir_op */
1432 * Hook: An IR op is freed.
1434 * @param ctx the hook context
1435 * @param op the IR opcode that is freed
1437 static void stat_free_ir_op(void *ctx, ir_op *op) {
1440 if (! status->stat_options)
1447 } /* stat_free_ir_op */
1450 * Hook: A new node is created.
1452 * @param ctx the hook context
1453 * @param irg the IR graph on which the node is created
1454 * @param node the new IR node that was created
1456 static void stat_new_node(void *ctx, ir_graph *irg, ir_node *node) {
1459 if (! status->stat_options)
1462 /* do NOT count during dead node elimination */
1463 if (status->in_dead_node_elim)
1468 node_entry_t *entry;
1469 graph_entry_t *graph;
1470 ir_op *op = stat_get_irn_op(node);
1472 /* increase global value */
1473 graph = graph_get_entry(NULL, status->irg_hash);
1474 entry = opcode_get_entry(op, graph->opcode_hash);
1475 cnt_inc(&entry->new_node);
1477 /* increase local value */
1478 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1479 entry = opcode_get_entry(op, graph->opcode_hash);
1480 cnt_inc(&entry->new_node);
1483 } /* stat_new_node */
1486 * Hook: A node is changed into a Id node
1488 * @param ctx the hook context
1489 * @param node the IR node that will be turned into an ID
1491 static void stat_turn_into_id(void *ctx, ir_node *node) {
1493 if (! status->stat_options)
1498 node_entry_t *entry;
1499 graph_entry_t *graph;
1500 ir_op *op = stat_get_irn_op(node);
1502 /* increase global value */
1503 graph = graph_get_entry(NULL, status->irg_hash);
1504 entry = opcode_get_entry(op, graph->opcode_hash);
1505 cnt_inc(&entry->into_Id);
1507 /* increase local value */
1508 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1509 entry = opcode_get_entry(op, graph->opcode_hash);
1510 cnt_inc(&entry->into_Id);
1513 } /* stat_turn_into_id */
1516 * Hook: A new graph was created
1518 * @param ctx the hook context
1519 * @param irg the new IR graph that was created
1520 * @param ent the entity of this graph
1522 static void stat_new_graph(void *ctx, ir_graph *irg, ir_entity *ent) {
1524 if (! status->stat_options)
1529 /* execute for side effect :-) */
1530 graph_entry_t * graph = graph_get_entry(irg, status->irg_hash);
1533 graph->is_deleted = 0;
1535 graph->is_leaf_call = 0;
1536 graph->is_recursive = 0;
1537 graph->is_chain_call = 0;
1538 graph->is_analyzed = 0;
1541 } /* stat_new_graph */
1544 * Hook: A graph will be deleted
1546 * @param ctx the hook context
1547 * @param irg the IR graph that will be deleted
1549 * Note that we still hold the information for this graph
1550 * in our hash maps, only a flag is set which prevents this
1551 * information from being changed, it's "frozen" from now.
1553 static void stat_free_graph(void *ctx, ir_graph *irg) {
1555 if (! status->stat_options)
1560 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1561 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1563 graph->is_deleted = 1;
1565 if (status->stat_options & FIRMSTAT_COUNT_DELETED) {
1566 /* count the nodes of the graph yet, it will be destroyed later */
1567 update_graph_stat(global, graph);
1571 } /* stat_free_graph */
1574 * Hook: A walk over a graph is initiated. Do not count walks from statistic code.
1576 * @param ctx the hook context
1577 * @param irg the IR graph that will be walked
1578 * @param pre the pre walker
1579 * @param post the post walker
1581 static void stat_irg_walk(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1586 if (! status->stat_options)
1591 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1593 cnt_inc(&graph->cnt[gcnt_acc_walked]);
1596 } /* stat_irg_walk */
1599 * Hook: A walk over a graph in block-wise order is initiated. Do not count walks from statistic code.
1601 * @param ctx the hook context
1602 * @param irg the IR graph that will be walked
1603 * @param pre the pre walker
1604 * @param post the post walker
1606 static void stat_irg_walk_blkwise(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1608 /* for now, do NOT differentiate between blockwise and normal */
1609 stat_irg_walk(ctx, irg, pre, post);
1610 } /* stat_irg_walk_blkwise */
1613 * Hook: A walk over the graph's blocks is initiated. Do not count walks from statistic code.
1615 * @param ctx the hook context
1616 * @param irg the IR graph that will be walked
1617 * @param node the IR node
1618 * @param pre the pre walker
1619 * @param post the post walker
1621 static void stat_irg_block_walk(void *ctx, ir_graph *irg, ir_node *node, generic_func *pre, generic_func *post)
1627 if (! status->stat_options)
1632 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1634 cnt_inc(&graph->cnt[gcnt_acc_walked_blocks]);
1637 } /* stat_irg_block_walk */
1640 * Called for every node that is removed due to an optimization.
1642 * @param n the IR node that will be removed
1643 * @param hmap the hash map containing ir_op* -> opt_entry_t*
1644 * @param kind the optimization kind
1646 static void removed_due_opt(ir_node *n, hmap_opt_entry_t *hmap, hook_opt_kind kind) {
1647 ir_op *op = stat_get_irn_op(n);
1648 opt_entry_t *entry = opt_get_entry(op, hmap);
1650 /* ignore CSE for Constants */
1651 if (kind == HOOK_OPT_CSE && (is_Const(n) || is_SymConst(n)))
1654 /* increase global value */
1655 cnt_inc(&entry->count);
1656 } /* removed_due_opt */
1659 * Hook: Some nodes were optimized into some others due to an optimization.
1661 * @param ctx the hook context
1663 static void stat_merge_nodes(
1665 ir_node **new_node_array, int new_num_entries,
1666 ir_node **old_node_array, int old_num_entries,
1670 if (! status->stat_options)
1676 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1678 cnt_inc(&status->num_opts[opt]);
1679 if (status->reassoc_run)
1680 opt = HOOK_OPT_REASSOC;
1682 for (i = 0; i < old_num_entries; ++i) {
1683 /* nodes might be in new and old, so if we found a node
1684 in both sets, this one is NOT removed */
1685 for (j = 0; j < new_num_entries; ++j) {
1686 if (old_node_array[i] == new_node_array[j])
1689 if (j >= new_num_entries) {
1692 /* sometimes we did not detect, that it is replaced by a Const */
1693 if (opt == HOOK_OPT_CONFIRM && new_num_entries == 1) {
1694 ir_op *op = get_irn_op(new_node_array[0]);
1696 if (op == op_Const || op == op_SymConst)
1697 xopt = HOOK_OPT_CONFIRM_C;
1700 removed_due_opt(old_node_array[i], graph->opt_hash[xopt], xopt);
1705 } /* stat_merge_nodes */
1708 * Hook: Reassociation is started/stopped.
1710 * @param ctx the hook context
1711 * @param flag if non-zero, reassociation is started else stopped
1713 static void stat_reassociate(void *ctx, int flag) {
1715 if (! status->stat_options)
1720 status->reassoc_run = flag;
1723 } /* stat_reassociate */
1726 * Hook: A node was lowered into other nodes
1728 * @param ctx the hook context
1729 * @param node the IR node that will be lowered
1731 static void stat_lower(void *ctx, ir_node *node) {
1733 if (! status->stat_options)
1738 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1740 removed_due_opt(node, graph->opt_hash[HOOK_LOWERED], HOOK_LOWERED);
1746 * Hook: A graph was inlined.
1748 * @param ctx the hook context
1749 * @param call the IR call that will re changed into the body of
1750 * the called IR graph
1751 * @param called_irg the IR graph representing the called routine
1753 static void stat_inline(void *ctx, ir_node *call, ir_graph *called_irg)
1756 if (! status->stat_options)
1761 ir_graph *irg = get_irn_irg(call);
1762 graph_entry_t *i_graph = graph_get_entry(called_irg, status->irg_hash);
1763 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1765 cnt_inc(&graph->cnt[gcnt_acc_got_inlined]);
1766 cnt_inc(&i_graph->cnt[gcnt_acc_was_inlined]);
1772 * Hook: A graph with tail-recursions was optimized.
1774 * @param ctx the hook context
1776 static void stat_tail_rec(void *ctx, ir_graph *irg, int n_calls) {
1778 if (! status->stat_options)
1783 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1785 graph->num_tail_recursion += n_calls;
1788 } /* stat_tail_rec */
1791 * Strength reduction was performed on an iteration variable.
1793 * @param ctx the hook context
1795 static void stat_strength_red(void *ctx, ir_graph *irg, ir_node *strong) {
1797 if (! status->stat_options)
1802 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1803 cnt_inc(&graph->cnt[gcnt_acc_strength_red]);
1805 removed_due_opt(strong, graph->opt_hash[HOOK_OPT_STRENGTH_RED], HOOK_OPT_STRENGTH_RED);
1808 } /* stat_strength_red */
1811 * Hook: Start/Stop the dead node elimination.
1813 * @param ctx the hook context
1815 static void stat_dead_node_elim(void *ctx, ir_graph *irg, int start) {
1818 if (! status->stat_options)
1821 status->in_dead_node_elim = (start != 0);
1822 } /* stat_dead_node_elim */
1825 * Hook: if-conversion was tried.
1827 static void stat_if_conversion(void *context, ir_graph *irg, ir_node *phi,
1828 int pos, ir_node *mux, if_result_t reason)
1834 if (! status->stat_options)
1839 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1841 cnt_inc(&graph->cnt[gcnt_if_conv + reason]);
1844 } /* stat_if_conversion */
1847 * Hook: real function call was optimized.
1849 static void stat_func_call(void *context, ir_graph *irg, ir_node *call)
1853 if (! status->stat_options)
1858 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1860 cnt_inc(&graph->cnt[gcnt_acc_real_func_call]);
1863 } /* stat_func_call */
1866 * Hook: A multiply was replaced by a series of Shifts/Adds/Subs.
1868 * @param ctx the hook context
1870 static void stat_arch_dep_replace_mul_with_shifts(void *ctx, ir_node *mul) {
1872 if (! status->stat_options)
1877 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1878 removed_due_opt(mul, graph->opt_hash[HOOK_OPT_ARCH_DEP], HOOK_OPT_ARCH_DEP);
1881 } /* stat_arch_dep_replace_mul_with_shifts */
1884 * Hook: A division by const was replaced.
1886 * @param ctx the hook context
1887 * @param node the division node that will be optimized
1889 static void stat_arch_dep_replace_division_by_const(void *ctx, ir_node *node) {
1891 if (! status->stat_options)
1896 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1897 removed_due_opt(node, graph->opt_hash[HOOK_OPT_ARCH_DEP], HOOK_OPT_ARCH_DEP);
1900 } /* stat_arch_dep_replace_division_by_const */
1903 * Update the register pressure of a block.
1905 * @param irg the irg containing the block
1906 * @param block the block for which the reg pressure should be set
1907 * @param pressure the pressure
1908 * @param class_name the name of the register class
1910 void stat_be_block_regpressure(ir_graph *irg, ir_node *block, int pressure, const char *class_name)
1912 if (! status->stat_options)
1917 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1918 be_block_entry_t *block_ent;
1919 reg_pressure_entry_t *rp_ent;
1921 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1922 rp_ent = obstack_alloc(&status->be_data, sizeof(*rp_ent));
1923 memset(rp_ent, 0, sizeof(*rp_ent));
1925 rp_ent->class_name = class_name;
1926 rp_ent->pressure = pressure;
1928 pset_insert(block_ent->reg_pressure, rp_ent, HASH_PTR(class_name));
1931 } /* stat_be_block_regpressure */
1934 * Update the distribution of ready nodes of a block
1936 * @param irg the irg containing the block
1937 * @param block the block for which the reg pressure should be set
1938 * @param num_ready the number of ready nodes
1940 void stat_be_block_sched_ready(ir_graph *irg, ir_node *block, int num_ready)
1942 if (! status->stat_options)
1947 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1948 be_block_entry_t *block_ent;
1950 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1952 /* increase the counter of corresponding number of ready nodes */
1953 stat_inc_int_distrib_tbl(block_ent->sched_ready, num_ready);
1956 } /* stat_be_block_sched_ready */
1959 * Update the permutation statistic of a block.
1961 * @param class_name the name of the register class
1962 * @param n_regs number of registers in the register class
1963 * @param perm the perm node
1964 * @param block the block containing the perm
1965 * @param size the size of the perm
1966 * @param real_size number of pairs with different registers
1968 void stat_be_block_stat_perm(const char *class_name, int n_regs, ir_node *perm, ir_node *block,
1969 int size, int real_size)
1971 if (! status->stat_options)
1976 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
1977 be_block_entry_t *block_ent;
1978 perm_class_entry_t *pc_ent;
1979 perm_stat_entry_t *ps_ent;
1981 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1982 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
1983 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
1985 pc_ent->n_regs = n_regs;
1987 /* update information */
1988 ps_ent->size = size;
1989 ps_ent->real_size = real_size;
1992 } /* stat_be_block_stat_perm */
1995 * Update the permutation statistic of a single perm.
1997 * @param class_name the name of the register class
1998 * @param perm the perm node
1999 * @param block the block containing the perm
2000 * @param is_chain 1 if chain, 0 if cycle
2001 * @param size length of the cycle/chain
2002 * @param n_ops the number of ops representing this cycle/chain after lowering
2004 void stat_be_block_stat_permcycle(const char *class_name, ir_node *perm, ir_node *block,
2005 int is_chain, int size, int n_ops)
2007 if (! status->stat_options)
2012 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
2013 be_block_entry_t *block_ent;
2014 perm_class_entry_t *pc_ent;
2015 perm_stat_entry_t *ps_ent;
2017 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
2018 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
2019 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
2022 ps_ent->n_copies += n_ops;
2023 stat_inc_int_distrib_tbl(ps_ent->chains, size);
2025 ps_ent->n_exchg += n_ops;
2026 stat_inc_int_distrib_tbl(ps_ent->cycles, size);
2030 } /* stat_be_block_stat_permcycle */
2032 /* Dumps a statistics snapshot. */
2033 void stat_dump_snapshot(const char *name, const char *phase)
2039 if (! status->stat_options)
2044 graph_entry_t *entry;
2045 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
2048 * The constant counter is only global, so we clear it here.
2049 * Note that it does NOT contain the constants in DELETED
2050 * graphs due to this.
2052 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2053 stat_const_clear(status);
2055 /* build the name */
2056 p = strrchr(name, '/');
2061 q = strrchr(name, '\\');
2063 /* NULL might be not the smallest pointer */
2064 if (q && (!p || q > p))
2072 if (l > (int) (sizeof(fname) - 1))
2073 l = sizeof(fname) - 1;
2075 memcpy(fname, name, l);
2081 strncat(fname, "firmstat-", sizeof(fname));
2082 strncat(fname, phase, sizeof(fname));
2083 strncat(fname, "-", sizeof(fname));
2084 strncat(fname, p, sizeof(fname));
2086 stat_dump_init(fname);
2088 /* calculate the graph statistics */
2089 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
2090 if (entry->irg == NULL) {
2091 /* special entry for the global count */
2094 if (! entry->is_deleted) {
2095 /* the graph is still alive, count the nodes on it */
2096 update_graph_stat(global, entry);
2100 /* some calculations are dependent, we pushed them on the wait_q */
2101 while (! pdeq_empty(status->wait_q)) {
2102 entry = pdeq_getr(status->wait_q);
2104 update_graph_stat_2(global, entry);
2107 /* dump per graph */
2108 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
2109 if (entry->irg == NULL) {
2110 /* special entry for the global count */
2114 if (! entry->is_deleted || status->stat_options & FIRMSTAT_COUNT_DELETED) {
2115 stat_dump_graph(entry);
2116 stat_dump_registered(entry);
2119 if (! entry->is_deleted) {
2120 /* clear the counter that are not accumulated */
2121 graph_clear_entry(entry, 0);
2126 stat_dump_graph(global);
2128 /* dump the const info */
2129 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2130 stat_dump_consts(&status->const_info);
2132 /* dump the parameter distribution */
2133 stat_dump_param_tbl(status->dist_param_cnt, global);
2135 /* dump the optimization counter and clear them */
2136 stat_dump_opt_cnt(status->num_opts, ARR_SIZE(status->num_opts));
2137 clear_optimization_counter();
2141 stat_finish_pattern_history(fname);
2143 /* clear the global counter here */
2145 node_entry_t *entry;
2147 for (entry = pset_first(global->opcode_hash); entry; entry = pset_next(global->opcode_hash)) {
2148 opcode_clear_entry(entry);
2150 /* clear all global counter */
2151 graph_clear_entry(global, 1);
2155 } /* stat_dump_snapshot */
2157 /** the hook entries for the Firm statistics module */
2158 static hook_entry_t stat_hooks[hook_last];
2160 /* initialize the statistics module. */
2161 void firm_init_stat(unsigned enable_options)
2163 #define X(a) a, sizeof(a)-1
2164 #define HOOK(h, fkt) \
2165 stat_hooks[h].hook._##h = fkt; register_hook(h, &stat_hooks[h])
2168 if (! (enable_options & FIRMSTAT_ENABLED))
2171 status = xmalloc(sizeof(*status));
2172 memset(status, 0, sizeof(*status));
2174 /* enable statistics */
2175 status->stat_options = enable_options & FIRMSTAT_ENABLED ? enable_options : 0;
2177 /* register all hooks */
2178 HOOK(hook_new_ir_op, stat_new_ir_op);
2179 HOOK(hook_free_ir_op, stat_free_ir_op);
2180 HOOK(hook_new_node, stat_new_node);
2181 HOOK(hook_turn_into_id, stat_turn_into_id);
2182 HOOK(hook_new_graph, stat_new_graph);
2183 HOOK(hook_free_graph, stat_free_graph);
2184 HOOK(hook_irg_walk, stat_irg_walk);
2185 HOOK(hook_irg_walk_blkwise, stat_irg_walk_blkwise);
2186 HOOK(hook_irg_block_walk, stat_irg_block_walk);
2187 HOOK(hook_merge_nodes, stat_merge_nodes);
2188 HOOK(hook_reassociate, stat_reassociate);
2189 HOOK(hook_lower, stat_lower);
2190 HOOK(hook_inline, stat_inline);
2191 HOOK(hook_tail_rec, stat_tail_rec);
2192 HOOK(hook_strength_red, stat_strength_red);
2193 HOOK(hook_dead_node_elim, stat_dead_node_elim);
2194 HOOK(hook_if_conversion, stat_if_conversion);
2195 HOOK(hook_func_call, stat_func_call);
2196 HOOK(hook_arch_dep_replace_mul_with_shifts, stat_arch_dep_replace_mul_with_shifts);
2197 HOOK(hook_arch_dep_replace_division_by_const, stat_arch_dep_replace_division_by_const);
2199 obstack_init(&status->cnts);
2200 obstack_init(&status->be_data);
2202 /* create the hash-tables */
2203 status->irg_hash = new_pset(graph_cmp, 8);
2204 status->ir_op_hash = new_pset(opcode_cmp_2, 1);
2206 /* create the wait queue */
2207 status->wait_q = new_pdeq();
2209 if (enable_options & FIRMSTAT_COUNT_STRONG_OP) {
2210 /* build the pseudo-ops */
2212 _op_Phi0.code = --num;
2213 _op_Phi0.name = new_id_from_chars(X("Phi0"));
2215 _op_PhiM.code = --num;
2216 _op_PhiM.name = new_id_from_chars(X("PhiM"));
2218 _op_ProjM.code = --num;
2219 _op_ProjM.name = new_id_from_chars(X("ProjM"));
2221 _op_MulC.code = --num;
2222 _op_MulC.name = new_id_from_chars(X("MulC"));
2224 _op_DivC.code = --num;
2225 _op_DivC.name = new_id_from_chars(X("DivC"));
2227 _op_ModC.code = --num;
2228 _op_ModC.name = new_id_from_chars(X("ModC"));
2230 _op_DivModC.code = --num;
2231 _op_DivModC.name = new_id_from_chars(X("DivModC"));
2233 status->op_Phi0 = &_op_Phi0;
2234 status->op_PhiM = &_op_PhiM;
2235 status->op_ProjM = &_op_ProjM;
2236 status->op_MulC = &_op_MulC;
2237 status->op_DivC = &_op_DivC;
2238 status->op_ModC = &_op_ModC;
2239 status->op_DivModC = &_op_DivModC;
2241 status->op_Phi0 = NULL;
2242 status->op_PhiM = NULL;
2243 status->op_ProjM = NULL;
2244 status->op_MulC = NULL;
2245 status->op_DivC = NULL;
2246 status->op_ModC = NULL;
2247 status->op_DivModC = NULL;
2250 /* for Florian: count the Sel depth */
2251 if (enable_options & FIRMSTAT_COUNT_SELS) {
2252 _op_SelSel.code = --num;
2253 _op_SelSel.name = new_id_from_chars(X("Sel(Sel)"));
2255 _op_SelSelSel.code = --num;
2256 _op_SelSelSel.name = new_id_from_chars(X("Sel(Sel(Sel))"));
2258 status->op_SelSel = &_op_SelSel;
2259 status->op_SelSelSel = &_op_SelSelSel;
2261 status->op_SelSel = NULL;
2262 status->op_SelSelSel = NULL;
2265 /* register the dumper */
2266 stat_register_dumper(&simple_dumper);
2268 if (enable_options & FIRMSTAT_CSV_OUTPUT)
2269 stat_register_dumper(&csv_dumper);
2271 /* initialize the pattern hash */
2272 stat_init_pattern_history(enable_options & FIRMSTAT_PATTERN_ENABLED);
2274 /* initialize the Const options */
2275 if (enable_options & FIRMSTAT_COUNT_CONSTS)
2276 stat_init_const_cnt(status);
2278 /* distribution table for parameter counts */
2279 status->dist_param_cnt = stat_new_int_distrib_tbl();
2281 clear_optimization_counter();
2285 } /* firm_init_stat */
2288 * Frees all dumper structures.
2290 static void stat_term_dumper(void) {
2291 dumper_t *dumper, *next_dumper;
2293 for (dumper = status->dumper; dumper; /* iteration done in loop body */ ) {
2294 if (dumper->func_map)
2295 del_pset(dumper->func_map);
2297 next_dumper = dumper->next;
2299 dumper = next_dumper;
2301 } /* stat_term_dumper */
2304 /* Terminates the statistics module, frees all memory. */
2305 void stat_term(void) {
2306 if (status != (stat_info_t *)&status_disable) {
2307 obstack_free(&status->be_data, NULL);
2308 obstack_free(&status->cnts, NULL);
2313 status = (stat_info_t *)&status_disable;
2317 /* returns 1 if statistics were initialized, 0 otherwise */
2318 int stat_is_active(void) {
2319 return status != (stat_info_t *)&status_disable;
2320 } /* stat_is_active */
2324 /* initialize the statistics module. */
2325 void firm_init_stat(unsigned enable_options) {}
2327 /* Dumps a statistics snapshot */
2328 void stat_dump_snapshot(const char *name, const char *phase) {}
2330 /* terminates the statistics module, frees all memory */
2331 void stat_term(void);
2333 #endif /* FIRM_STATISTICS */