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-acumulated 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;
809 /* indirect call, be could not predict */
810 cnt_inc(&graph->cnt[gcnt_indirect_calls]);
812 /* NOT a leaf call */
813 graph->is_leaf_call = LCS_NON_LEAF_CALL;
816 /* check, if it's a chain-call: Then, the call-block
817 * must dominate the end block. */
819 ir_node *curr = get_irg_end_block(graph->irg);
820 int depth = get_Block_dom_depth(block);
822 for (; curr != block && get_Block_dom_depth(curr) > depth;) {
823 curr = get_Block_idom(curr);
825 if (! curr || is_no_Block(curr))
830 graph->is_chain_call = 0;
833 /* check, if the callee is a leaf */
835 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
837 if (called->is_analyzed) {
838 if (! called->is_leaf)
839 graph->is_leaf_call = LCS_NON_LEAF_CALL;
843 analyse_params_of_Call(graph, call);
844 } /* stat_update_call */
847 * Update info on calls for graphs on the wait queue.
849 static void stat_update_call_2(ir_node *call, graph_entry_t *graph)
851 ir_node *block = get_nodes_block(call);
852 ir_node *ptr = get_Call_ptr(call);
853 ir_entity *ent = NULL;
854 ir_graph *callee = NULL;
857 * If the block is bad, the whole subgraph will collapse later
858 * so do not count this call.
859 * This happens in dead code.
864 if (get_irn_op(ptr) == op_SymConst) {
865 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
866 /* ok, we seems to know the entity */
867 ent = get_SymConst_entity(ptr);
868 callee = get_entity_irg(ent);
872 /* check, if the callee is a leaf */
874 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
876 assert(called->is_analyzed);
878 if (! called->is_leaf)
879 graph->is_leaf_call = LCS_NON_LEAF_CALL;
881 graph->is_leaf_call = LCS_NON_LEAF_CALL;
882 } /* stat_update_call_2 */
885 * Find the base address and entity of an Sel node.
887 * @param sel the node
889 * @return the base address.
891 static ir_node *find_base_adr(ir_node *sel) {
892 ir_node *ptr = get_Sel_ptr(sel);
894 while (is_Sel(ptr)) {
896 ptr = get_Sel_ptr(sel);
899 } /* find_base_adr */
902 * Update info on Load/Store address statistics.
904 static void stat_update_address(ir_node *node, graph_entry_t *graph) {
905 ir_opcode opc = get_irn_opcode(node);
911 /* a global address */
912 cnt_inc(&graph->cnt[gcnt_global_adr]);
915 base = find_base_adr(node);
916 irg = current_ir_graph;
917 if (base == get_irg_tls(irg)) {
918 /* a TLS variable, like a global. */
919 cnt_inc(&graph->cnt[gcnt_global_adr]);
920 } else if (base == get_irg_frame(irg)) {
921 /* a local Variable. */
922 cnt_inc(&graph->cnt[gcnt_local_adr]);
925 if (is_Proj(base) && skip_Proj(get_Proj_pred(base)) == get_irg_start(irg)) {
926 /* pointer access through parameter, check for THIS */
927 ir_entity *ent = get_irg_entity(irg);
930 ir_type *ent_tp = get_entity_type(ent);
932 if (get_method_calling_convention(ent_tp) & cc_this_call) {
933 if (get_Proj_proj(base) == 0) {
935 cnt_inc(&graph->cnt[gcnt_this_adr]);
940 /* other parameter */
941 cnt_inc(&graph->cnt[gcnt_param_adr]);
944 /* unknown Pointer access */
945 cnt_inc(&graph->cnt[gcnt_other_adr]);
951 } /* stat_update_address */
954 * Walker for reachable nodes count.
956 static void update_node_stat(ir_node *node, void *env)
958 graph_entry_t *graph = env;
961 ir_op *op = stat_get_irn_op(node);
962 int arity = get_irn_arity(node);
964 entry = opcode_get_entry(op, graph->opcode_hash);
966 cnt_inc(&entry->cnt_alive);
967 cnt_add_i(&graph->cnt[gcnt_edges], arity);
969 /* count block edges */
970 undate_block_info(node, graph);
972 /* count extended block edges */
973 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
974 if (graph->irg != get_const_code_irg())
975 update_extbb_info(node, graph);
978 /* handle statistics for special node types */
982 if (status->stat_options & FIRMSTAT_COUNT_CONSTS) {
983 /* check properties of constants */
984 stat_update_const(status, node, graph);
988 /* check for properties that depends on calls like recursion/leaf/indirect call */
989 stat_update_call(node, graph);
992 /* check address properties */
993 stat_update_address(get_Load_ptr(node), graph);
996 /* check address properties */
997 stat_update_address(get_Store_ptr(node), graph);
1002 } /* update_node_stat */
1005 * Walker for reachable nodes count for graphs on the wait_q.
1007 static void update_node_stat_2(ir_node *node, void *env) {
1008 graph_entry_t *graph = env;
1010 /* check for properties that depends on calls like recursion/leaf/indirect call */
1012 stat_update_call_2(node, graph);
1013 } /* update_node_stat_2 */
1016 * Get the current address mark.
1018 static unsigned get_adr_mark(graph_entry_t *graph, ir_node *node) {
1019 address_mark_entry_t *value = set_find(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
1021 return value ? value->mark : 0;
1022 } /* get_adr_mark */
1025 * Set the current address mark.
1027 static void set_adr_mark(graph_entry_t *graph, ir_node *node, unsigned val) {
1028 address_mark_entry_t *value = set_insert(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
1031 } /* set_adr_mark */
1033 #undef DUMP_ADR_MODE
1035 #ifdef DUMP_ADR_MODE
1037 * a vcg attribute hook: Color a node with a different color if
1038 * it's identified as a part of an address expression or at least referenced
1039 * by an address expression.
1041 static int stat_adr_mark_hook(FILE *F, ir_node *node, ir_node *local)
1043 ir_node *n = local ? local : node;
1044 ir_graph *irg = get_irn_irg(n);
1045 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1046 unsigned mark = get_adr_mark(graph, n);
1048 if (mark & MARK_ADDRESS_CALC)
1049 fprintf(F, "color: purple");
1050 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1051 fprintf(F, "color: pink");
1052 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1053 fprintf(F, "color: lightblue");
1057 /* I know the color! */
1059 } /* stat_adr_mark_hook */
1060 #endif /* DUMP_ADR_MODE */
1063 * Return the "operational" mode of a Firm node.
1065 static ir_mode *get_irn_op_mode(ir_node *node) {
1066 switch (get_irn_opcode(node)) {
1068 return get_Load_mode(node);
1070 return get_irn_mode(get_Store_value(node));
1072 return get_irn_mode(get_DivMod_left(node));
1074 return get_irn_mode(get_Div_left(node));
1076 return get_irn_mode(get_Mod_left(node));
1078 /* Cmp is no address calculation, or is it? */
1080 return get_irn_mode(node);
1082 } /* get_irn_op_mode */
1085 * Post-walker that marks every node that is an address calculation.
1087 * Users of a node must be visited first. We ensure this by
1088 * calling it in the post of an outs walk. This should work even in cycles,
1089 * while the normal pre-walk will not.
1091 static void mark_address_calc(ir_node *node, void *env) {
1092 graph_entry_t *graph = env;
1093 ir_mode *mode = get_irn_op_mode(node);
1095 unsigned mark_preds = MARK_REF_NON_ADR;
1097 if (! mode_is_data(mode))
1100 if (mode_is_reference(mode)) {
1101 /* a reference is calculated here, we are sure */
1102 set_adr_mark(graph, node, MARK_ADDRESS_CALC);
1104 mark_preds = MARK_REF_ADR;
1106 unsigned mark = get_adr_mark(graph, node);
1108 if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR) {
1110 * this node has no reference mode, but is only
1111 * referenced by address calculations
1113 mark_preds = MARK_REF_ADR;
1117 /* mark all predecessors */
1118 for (i = 0, n = get_irn_arity(node); i < n; ++i) {
1119 ir_node *pred = get_irn_n(node, i);
1121 mode = get_irn_op_mode(pred);
1122 if (! mode_is_data(mode))
1125 set_adr_mark(graph, pred, get_adr_mark(graph, pred) | mark_preds);
1127 } /* mark_address_calc */
1130 * Post-walker that marks every node that is an address calculation.
1132 * Users of a node must be visited first. We ensure this by
1133 * calling it in the post of an outs walk. This should work even in cycles,
1134 * while the normal pre-walk will not.
1136 static void count_adr_ops(ir_node *node, void *env) {
1137 graph_entry_t *graph = env;
1138 unsigned mark = get_adr_mark(graph, node);
1140 if (mark & MARK_ADDRESS_CALC)
1141 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1142 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1143 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1144 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1145 cnt_inc(&graph->cnt[gcnt_all_adr_ops]);
1146 } /* count_adr_ops */
1149 * Called for every graph when the graph is either deleted or stat_dump_snapshot()
1150 * is called, must recalculate all statistic info.
1152 * @param global The global entry
1153 * @param graph The current entry
1155 static void update_graph_stat(graph_entry_t *global, graph_entry_t *graph)
1157 node_entry_t *entry;
1160 /* clear first the alive counter in the graph */
1161 foreach_pset(graph->opcode_hash, entry) {
1162 cnt_clr(&entry->cnt_alive);
1163 } /* foreach_pset */
1165 /* set pessimistic values */
1167 graph->is_leaf_call = LCS_UNKNOWN;
1168 graph->is_recursive = 0;
1169 graph->is_chain_call = 1;
1171 /* create new block counter */
1172 graph->block_hash = new_pset(block_cmp, 5);
1174 /* we need dominator info */
1175 if (graph->irg != get_const_code_irg()) {
1176 assure_doms(graph->irg);
1178 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
1179 /* we need extended basic blocks */
1180 compute_extbb(graph->irg);
1182 /* create new extbb counter */
1183 graph->extbb_hash = new_pset(block_cmp, 5);
1187 /* count the nodes in the graph */
1188 irg_walk_graph(graph->irg, update_node_stat, NULL, graph);
1191 /* Uncomment this code if chain-call means call exact one. */
1192 entry = opcode_get_entry(op_Call, graph->opcode_hash);
1194 /* check if we have more than 1 call */
1195 if (cnt_gt(entry->cnt_alive, 1))
1196 graph->is_chain_call = 0;
1199 /* recursive functions are never chain calls, leafs don't have calls */
1200 if (graph->is_recursive || graph->is_leaf)
1201 graph->is_chain_call = 0;
1203 /* assume we walk every graph only ONCE, we could sum here the global count */
1204 foreach_pset(graph->opcode_hash, entry) {
1205 node_entry_t *g_entry = opcode_get_entry(entry->op, global->opcode_hash);
1207 /* update the node counter */
1208 cnt_add(&g_entry->cnt_alive, &entry->cnt_alive);
1209 } /* foreach_pset */
1211 /* count the number of address calculation */
1212 if (graph->irg != get_const_code_irg()) {
1213 ir_graph *rem = current_ir_graph;
1215 assure_irg_outs(graph->irg);
1217 /* Must be done an the outs graph */
1218 current_ir_graph = graph->irg;
1219 irg_out_walk(get_irg_start(graph->irg), NULL, mark_address_calc, graph);
1220 current_ir_graph = rem;
1222 #ifdef DUMP_ADR_MODE
1223 /* register the vcg hook and dump the graph for test */
1224 set_dump_node_vcgattr_hook(stat_adr_mark_hook);
1225 dump_ir_block_graph(graph->irg, "-adr");
1226 set_dump_node_vcgattr_hook(NULL);
1227 #endif /* DUMP_ADR_MODE */
1229 irg_walk_graph(graph->irg, NULL, count_adr_ops, graph);
1232 /* count the DAG's */
1233 if (status->stat_options & FIRMSTAT_COUNT_DAG)
1234 count_dags_in_graph(global, graph);
1236 /* calculate the patterns of this graph */
1237 stat_calc_pattern_history(graph->irg);
1239 /* leaf function did not call others */
1241 graph->is_leaf_call = LCS_NON_LEAF_CALL;
1242 else if (graph->is_leaf_call == LCS_UNKNOWN) {
1243 /* we still don't know if this graph calls leaf-functions, so enqueue */
1244 pdeq_putl(status->wait_q, graph);
1247 /* we have analyzed this graph */
1248 graph->is_analyzed = 1;
1250 /* accumulate all counter's */
1251 for (i = 0; i < _gcnt_last; ++i)
1252 cnt_add(&global->cnt[i], &graph->cnt[i]);
1253 } /* update_graph_stat */
1256 * Called for every graph that was on the wait_q in stat_dump_snapshot()
1257 * must finish all statistic info calculations.
1259 * @param global The global entry
1260 * @param graph The current entry
1262 static void update_graph_stat_2(graph_entry_t *global, graph_entry_t *graph)
1265 if (graph->is_deleted) {
1266 /* deleted, ignore */
1271 /* count the nodes in the graph */
1272 irg_walk_graph(graph->irg, update_node_stat_2, NULL, graph);
1274 if (graph->is_leaf_call == LCS_UNKNOWN)
1275 graph->is_leaf_call = LCS_LEAF_CALL;
1277 } /* update_graph_stat_2 */
1280 * Register a dumper.
1282 static void stat_register_dumper(const dumper_t *dumper) {
1283 dumper_t *p = xmalloc(sizeof(*p));
1286 memcpy(p, dumper, sizeof(*p));
1288 p->next = status->dumper;
1293 /* FIXME: memory leak */
1294 } /* stat_register_dumper */
1297 * Dumps the statistics of an IR graph.
1299 static void stat_dump_graph(graph_entry_t *entry) {
1302 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1303 if (dumper->dump_graph)
1304 dumper->dump_graph(dumper, entry);
1306 } /* stat_dump_graph */
1309 * Calls all registered dumper functions.
1311 static void stat_dump_registered(graph_entry_t *entry) {
1314 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1315 if (dumper->func_map) {
1316 dump_graph_FUNC func;
1318 foreach_pset(dumper->func_map, func)
1319 func(dumper, entry);
1322 } /* stat_dump_registered */
1325 * Dumps a constant table.
1327 static void stat_dump_consts(const constant_info_t *tbl) {
1330 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1331 if (dumper->dump_const_tbl)
1332 dumper->dump_const_tbl(dumper, tbl);
1334 } /* stat_dump_consts */
1337 * Dumps the parameter distribution
1339 static void stat_dump_param_tbl(const distrib_tbl_t *tbl, graph_entry_t *global) {
1342 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1343 if (dumper->dump_param_tbl)
1344 dumper->dump_param_tbl(dumper, tbl, global);
1346 } /* stat_dump_param_tbl */
1349 * Dumps the optimization counter
1351 static void stat_dump_opt_cnt(const counter_t *tbl, unsigned len) {
1354 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1355 if (dumper->dump_opt_cnt)
1356 dumper->dump_opt_cnt(dumper, tbl, len);
1358 } /* stat_dump_opt_cnt */
1361 * Initialize the dumper.
1363 static void stat_dump_init(const char *name) {
1366 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1368 dumper->init(dumper, name);
1370 } /* stat_dump_init */
1373 * Finish the dumper.
1375 static void stat_dump_finish(void) {
1378 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1380 dumper->finish(dumper);
1382 } /* stat_dump_finish */
1385 * Register an additional function for all dumper.
1387 void stat_register_dumper_func(dump_graph_FUNC func) {
1390 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1391 if (! dumper->func_map)
1392 dumper->func_map = pset_new_ptr(3);
1393 pset_insert_ptr(dumper->func_map, func);
1395 } /* stat_register_dumper_func */
1397 /* ---------------------------------------------------------------------- */
1400 * Helper: get an ir_op from an opcode.
1402 ir_op *stat_get_op_from_opcode(ir_opcode code) {
1403 return opcode_find_entry(code, status->ir_op_hash);
1404 } /* stat_get_op_from_opcode */
1407 * Hook: A new IR op is registered.
1409 * @param ctx the hook context
1410 * @param op the new IR opcode that was created.
1412 static void stat_new_ir_op(void *ctx, ir_op *op) {
1414 if (! status->stat_options)
1419 graph_entry_t *graph = graph_get_entry(NULL, status->irg_hash);
1421 /* execute for side effect :-) */
1422 (void)opcode_get_entry(op, graph->opcode_hash);
1424 pset_insert(status->ir_op_hash, op, op->code);
1427 } /* stat_new_ir_op */
1430 * Hook: An IR op is freed.
1432 * @param ctx the hook context
1433 * @param op the IR opcode that is freed
1435 static void stat_free_ir_op(void *ctx, ir_op *op) {
1438 if (! status->stat_options)
1445 } /* stat_free_ir_op */
1448 * Hook: A new node is created.
1450 * @param ctx the hook context
1451 * @param irg the IR graph on which the node is created
1452 * @param node the new IR node that was created
1454 static void stat_new_node(void *ctx, ir_graph *irg, ir_node *node) {
1457 if (! status->stat_options)
1460 /* do NOT count during dead node elimination */
1461 if (status->in_dead_node_elim)
1466 node_entry_t *entry;
1467 graph_entry_t *graph;
1468 ir_op *op = stat_get_irn_op(node);
1470 /* increase global value */
1471 graph = graph_get_entry(NULL, status->irg_hash);
1472 entry = opcode_get_entry(op, graph->opcode_hash);
1473 cnt_inc(&entry->new_node);
1475 /* increase local value */
1476 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1477 entry = opcode_get_entry(op, graph->opcode_hash);
1478 cnt_inc(&entry->new_node);
1481 } /* stat_new_node */
1484 * Hook: A node is changed into a Id node
1486 * @param ctx the hook context
1487 * @param node the IR node that will be turned into an ID
1489 static void stat_turn_into_id(void *ctx, ir_node *node) {
1491 if (! status->stat_options)
1496 node_entry_t *entry;
1497 graph_entry_t *graph;
1498 ir_op *op = stat_get_irn_op(node);
1500 /* increase global value */
1501 graph = graph_get_entry(NULL, status->irg_hash);
1502 entry = opcode_get_entry(op, graph->opcode_hash);
1503 cnt_inc(&entry->into_Id);
1505 /* increase local value */
1506 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1507 entry = opcode_get_entry(op, graph->opcode_hash);
1508 cnt_inc(&entry->into_Id);
1511 } /* stat_turn_into_id */
1514 * Hook: A new graph was created
1516 * @param ctx the hook context
1517 * @param irg the new IR graph that was created
1518 * @param ent the entity of this graph
1520 static void stat_new_graph(void *ctx, ir_graph *irg, ir_entity *ent) {
1522 if (! status->stat_options)
1527 /* execute for side effect :-) */
1528 graph_entry_t * graph = graph_get_entry(irg, status->irg_hash);
1531 graph->is_deleted = 0;
1533 graph->is_leaf_call = 0;
1534 graph->is_recursive = 0;
1535 graph->is_chain_call = 0;
1536 graph->is_analyzed = 0;
1539 } /* stat_new_graph */
1542 * Hook: A graph will be deleted
1544 * @param ctx the hook context
1545 * @param irg the IR graph that will be deleted
1547 * Note that we still hold the information for this graph
1548 * in our hash maps, only a flag is set which prevents this
1549 * information from being changed, it's "frozen" from now.
1551 static void stat_free_graph(void *ctx, ir_graph *irg) {
1553 if (! status->stat_options)
1558 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1559 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1561 graph->is_deleted = 1;
1563 if (status->stat_options & FIRMSTAT_COUNT_DELETED) {
1564 /* count the nodes of the graph yet, it will be destroyed later */
1565 update_graph_stat(global, graph);
1569 } /* stat_free_graph */
1572 * Hook: A walk over a graph is initiated. Do not count walks from statistic code.
1574 * @param ctx the hook context
1575 * @param irg the IR graph that will be walked
1576 * @param pre the pre walker
1577 * @param post the post walker
1579 static void stat_irg_walk(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1584 if (! status->stat_options)
1589 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1591 cnt_inc(&graph->cnt[gcnt_acc_walked]);
1594 } /* stat_irg_walk */
1597 * Hook: A walk over a graph in block-wise order is initiated. Do not count walks from statistic code.
1599 * @param ctx the hook context
1600 * @param irg the IR graph that will be walked
1601 * @param pre the pre walker
1602 * @param post the post walker
1604 static void stat_irg_walk_blkwise(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1606 /* for now, do NOT differentiate between blockwise and normal */
1607 stat_irg_walk(ctx, irg, pre, post);
1608 } /* stat_irg_walk_blkwise */
1611 * Hook: A walk over the graph's blocks is initiated. Do not count walks from statistic code.
1613 * @param ctx the hook context
1614 * @param irg the IR graph that will be walked
1615 * @param node the IR node
1616 * @param pre the pre walker
1617 * @param post the post walker
1619 static void stat_irg_block_walk(void *ctx, ir_graph *irg, ir_node *node, generic_func *pre, generic_func *post)
1625 if (! status->stat_options)
1630 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1632 cnt_inc(&graph->cnt[gcnt_acc_walked_blocks]);
1635 } /* stat_irg_block_walk */
1638 * Called for every node that is removed due to an optimization.
1640 * @param n the IR node that will be removed
1641 * @param hmap the hash map containing ir_op* -> opt_entry_t*
1643 static void removed_due_opt(ir_node *n, hmap_opt_entry_t *hmap) {
1644 ir_op *op = stat_get_irn_op(n);
1645 opt_entry_t *entry = opt_get_entry(op, hmap);
1647 /* increase global value */
1648 cnt_inc(&entry->count);
1649 } /* removed_due_opt */
1652 * Hook: Some nodes were optimized into some others due to an optimization.
1654 * @param ctx the hook context
1656 static void stat_merge_nodes(
1658 ir_node **new_node_array, int new_num_entries,
1659 ir_node **old_node_array, int old_num_entries,
1663 if (! status->stat_options)
1669 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1671 cnt_inc(&status->num_opts[opt]);
1672 if (status->reassoc_run)
1673 opt = HOOK_OPT_REASSOC;
1675 for (i = 0; i < old_num_entries; ++i) {
1676 /* nodes might be in new and old, so if we found a node
1677 in both sets, this one is NOT removed */
1678 for (j = 0; j < new_num_entries; ++j) {
1679 if (old_node_array[i] == new_node_array[j])
1682 if (j >= new_num_entries) {
1685 /* sometimes we did not detect, that it is replaced by a Const */
1686 if (opt == HOOK_OPT_CONFIRM && new_num_entries == 1) {
1687 ir_op *op = get_irn_op(new_node_array[0]);
1689 if (op == op_Const || op == op_SymConst)
1690 xopt = HOOK_OPT_CONFIRM_C;
1693 removed_due_opt(old_node_array[i], graph->opt_hash[xopt]);
1698 } /* stat_merge_nodes */
1701 * Hook: Reassociation is started/stopped.
1703 * @param ctx the hook context
1704 * @param flag if non-zero, reassociation is started else stopped
1706 static void stat_reassociate(void *ctx, int flag) {
1708 if (! status->stat_options)
1713 status->reassoc_run = flag;
1716 } /* stat_reassociate */
1719 * Hook: A node was lowered into other nodes
1721 * @param ctx the hook context
1722 * @param node the IR node that will be lowered
1724 static void stat_lower(void *ctx, ir_node *node) {
1726 if (! status->stat_options)
1731 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1733 removed_due_opt(node, graph->opt_hash[HOOK_LOWERED]);
1739 * Hook: A graph was inlined.
1741 * @param ctx the hook context
1742 * @param call the IR call that will re changed into the body of
1743 * the called IR graph
1744 * @param called_irg the IR graph representing the called routine
1746 static void stat_inline(void *ctx, ir_node *call, ir_graph *called_irg)
1749 if (! status->stat_options)
1754 ir_graph *irg = get_irn_irg(call);
1755 graph_entry_t *i_graph = graph_get_entry(called_irg, status->irg_hash);
1756 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1758 cnt_inc(&graph->cnt[gcnt_acc_got_inlined]);
1759 cnt_inc(&i_graph->cnt[gcnt_acc_was_inlined]);
1765 * Hook: A graph with tail-recursions was optimized.
1767 * @param ctx the hook context
1769 static void stat_tail_rec(void *ctx, ir_graph *irg, int n_calls) {
1771 if (! status->stat_options)
1776 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1778 graph->num_tail_recursion += n_calls;
1781 } /* stat_tail_rec */
1784 * Strength reduction was performed on an iteration variable.
1786 * @param ctx the hook context
1788 static void stat_strength_red(void *ctx, ir_graph *irg, ir_node *strong) {
1790 if (! status->stat_options)
1795 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1796 cnt_inc(&graph->cnt[gcnt_acc_strength_red]);
1798 removed_due_opt(strong, graph->opt_hash[HOOK_OPT_STRENGTH_RED]);
1801 } /* stat_strength_red */
1804 * Hook: Start/Stop the dead node elimination.
1806 * @param ctx the hook context
1808 static void stat_dead_node_elim(void *ctx, ir_graph *irg, int start) {
1811 if (! status->stat_options)
1814 status->in_dead_node_elim = (start != 0);
1815 } /* stat_dead_node_elim */
1818 * Hook: if-conversion was tried.
1820 static void stat_if_conversion(void *context, ir_graph *irg, ir_node *phi,
1821 int pos, ir_node *mux, if_result_t reason)
1827 if (! status->stat_options)
1832 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1834 cnt_inc(&graph->cnt[gcnt_if_conv + reason]);
1837 } /* stat_if_conversion */
1840 * Hook: real function call was optimized.
1842 static void stat_func_call(void *context, ir_graph *irg, ir_node *call)
1846 if (! status->stat_options)
1851 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1853 cnt_inc(&graph->cnt[gcnt_acc_real_func_call]);
1856 } /* stat_func_call */
1859 * Hook: A multiply was replaced by a series of Shifts/Adds/Subs.
1861 * @param ctx the hook context
1863 static void stat_arch_dep_replace_mul_with_shifts(void *ctx, ir_node *mul) {
1865 if (! status->stat_options)
1870 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1871 removed_due_opt(mul, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1874 } /* stat_arch_dep_replace_mul_with_shifts */
1877 * Hook: A division by const was replaced.
1879 * @param ctx the hook context
1880 * @param node the division node that will be optimized
1882 static void stat_arch_dep_replace_division_by_const(void *ctx, ir_node *node) {
1884 if (! status->stat_options)
1889 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1890 removed_due_opt(node, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1893 } /* stat_arch_dep_replace_division_by_const */
1896 * Update the register pressure of a block.
1898 * @param irg the irg containing the block
1899 * @param block the block for which the reg pressure should be set
1900 * @param pressure the pressure
1901 * @param class_name the name of the register class
1903 void stat_be_block_regpressure(ir_graph *irg, ir_node *block, int pressure, const char *class_name)
1905 if (! status->stat_options)
1910 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1911 be_block_entry_t *block_ent;
1912 reg_pressure_entry_t *rp_ent;
1914 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1915 rp_ent = obstack_alloc(&status->be_data, sizeof(*rp_ent));
1916 memset(rp_ent, 0, sizeof(*rp_ent));
1918 rp_ent->class_name = class_name;
1919 rp_ent->pressure = pressure;
1921 pset_insert(block_ent->reg_pressure, rp_ent, HASH_PTR(class_name));
1924 } /* stat_be_block_regpressure */
1927 * Update the distribution of ready nodes of a block
1929 * @param irg the irg containing the block
1930 * @param block the block for which the reg pressure should be set
1931 * @param num_ready the number of ready nodes
1933 void stat_be_block_sched_ready(ir_graph *irg, ir_node *block, int num_ready)
1935 if (! status->stat_options)
1940 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1941 be_block_entry_t *block_ent;
1943 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1945 /* increase the counter of corresponding number of ready nodes */
1946 stat_inc_int_distrib_tbl(block_ent->sched_ready, num_ready);
1949 } /* stat_be_block_sched_ready */
1952 * Update the permutation statistic of a block.
1954 * @param class_name the name of the register class
1955 * @param n_regs number of registers in the register class
1956 * @param perm the perm node
1957 * @param block the block containing the perm
1958 * @param size the size of the perm
1959 * @param real_size number of pairs with different registers
1961 void stat_be_block_stat_perm(const char *class_name, int n_regs, ir_node *perm, ir_node *block,
1962 int size, int real_size)
1964 if (! status->stat_options)
1969 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
1970 be_block_entry_t *block_ent;
1971 perm_class_entry_t *pc_ent;
1972 perm_stat_entry_t *ps_ent;
1974 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1975 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
1976 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
1978 pc_ent->n_regs = n_regs;
1980 /* update information */
1981 ps_ent->size = size;
1982 ps_ent->real_size = real_size;
1985 } /* stat_be_block_stat_perm */
1988 * Update the permutation statistic of a single perm.
1990 * @param class_name the name of the register class
1991 * @param perm the perm node
1992 * @param block the block containing the perm
1993 * @param is_chain 1 if chain, 0 if cycle
1994 * @param size length of the cycle/chain
1995 * @param n_ops the number of ops representing this cycle/chain after lowering
1997 void stat_be_block_stat_permcycle(const char *class_name, ir_node *perm, ir_node *block,
1998 int is_chain, int size, int n_ops)
2000 if (! status->stat_options)
2005 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
2006 be_block_entry_t *block_ent;
2007 perm_class_entry_t *pc_ent;
2008 perm_stat_entry_t *ps_ent;
2010 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
2011 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
2012 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
2015 ps_ent->n_copies += n_ops;
2016 stat_inc_int_distrib_tbl(ps_ent->chains, size);
2018 ps_ent->n_exchg += n_ops;
2019 stat_inc_int_distrib_tbl(ps_ent->cycles, size);
2023 } /* stat_be_block_stat_permcycle */
2025 /* Dumps a statistics snapshot. */
2026 void stat_dump_snapshot(const char *name, const char *phase)
2032 if (! status->stat_options)
2037 graph_entry_t *entry;
2038 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
2041 * The constant counter is only global, so we clear it here.
2042 * Note that it does NOT contain the constants in DELETED
2043 * graphs due to this.
2045 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2046 stat_const_clear(status);
2048 /* build the name */
2049 p = strrchr(name, '/');
2054 q = strrchr(name, '\\');
2056 /* NULL might be not the smallest pointer */
2057 if (q && (!p || q > p))
2065 if (l > (int) (sizeof(fname) - 1))
2066 l = sizeof(fname) - 1;
2068 memcpy(fname, name, l);
2074 strncat(fname, "firmstat-", sizeof(fname));
2075 strncat(fname, phase, sizeof(fname));
2076 strncat(fname, "-", sizeof(fname));
2077 strncat(fname, p, sizeof(fname));
2079 stat_dump_init(fname);
2081 /* calculate the graph statistics */
2082 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
2083 if (entry->irg == NULL) {
2084 /* special entry for the global count */
2087 if (! entry->is_deleted) {
2088 /* the graph is still alive, count the nodes on it */
2089 update_graph_stat(global, entry);
2093 /* some calculations are dependent, we pushed them on the wait_q */
2094 while (! pdeq_empty(status->wait_q)) {
2095 entry = pdeq_getr(status->wait_q);
2097 update_graph_stat_2(global, entry);
2100 /* dump per graph */
2101 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
2102 if (entry->irg == NULL) {
2103 /* special entry for the global count */
2107 if (! entry->is_deleted || status->stat_options & FIRMSTAT_COUNT_DELETED) {
2108 stat_dump_graph(entry);
2109 stat_dump_registered(entry);
2112 if (! entry->is_deleted) {
2113 /* clear the counter that are not accumulated */
2114 graph_clear_entry(entry, 0);
2119 stat_dump_graph(global);
2121 /* dump the const info */
2122 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2123 stat_dump_consts(&status->const_info);
2125 /* dump the parameter distribution */
2126 stat_dump_param_tbl(status->dist_param_cnt, global);
2128 /* dump the optimization counter and clear them */
2129 stat_dump_opt_cnt(status->num_opts, ARR_SIZE(status->num_opts));
2130 clear_optimization_counter();
2134 stat_finish_pattern_history(fname);
2136 /* clear the global counter here */
2138 node_entry_t *entry;
2140 for (entry = pset_first(global->opcode_hash); entry; entry = pset_next(global->opcode_hash)) {
2141 opcode_clear_entry(entry);
2143 /* clear all global counter */
2144 graph_clear_entry(global, 1);
2148 } /* stat_dump_snapshot */
2150 /** the hook entries for the Firm statistics module */
2151 static hook_entry_t stat_hooks[hook_last];
2153 /* initialize the statistics module. */
2154 void firm_init_stat(unsigned enable_options)
2156 #define X(a) a, sizeof(a)-1
2157 #define HOOK(h, fkt) \
2158 stat_hooks[h].hook._##h = fkt; register_hook(h, &stat_hooks[h])
2161 if (! (enable_options & FIRMSTAT_ENABLED))
2164 status = xmalloc(sizeof(*status));
2165 memset(status, 0, sizeof(*status));
2167 /* enable statistics */
2168 status->stat_options = enable_options & FIRMSTAT_ENABLED ? enable_options : 0;
2170 /* register all hooks */
2171 HOOK(hook_new_ir_op, stat_new_ir_op);
2172 HOOK(hook_free_ir_op, stat_free_ir_op);
2173 HOOK(hook_new_node, stat_new_node);
2174 HOOK(hook_turn_into_id, stat_turn_into_id);
2175 HOOK(hook_new_graph, stat_new_graph);
2176 HOOK(hook_free_graph, stat_free_graph);
2177 HOOK(hook_irg_walk, stat_irg_walk);
2178 HOOK(hook_irg_walk_blkwise, stat_irg_walk_blkwise);
2179 HOOK(hook_irg_block_walk, stat_irg_block_walk);
2180 HOOK(hook_merge_nodes, stat_merge_nodes);
2181 HOOK(hook_reassociate, stat_reassociate);
2182 HOOK(hook_lower, stat_lower);
2183 HOOK(hook_inline, stat_inline);
2184 HOOK(hook_tail_rec, stat_tail_rec);
2185 HOOK(hook_strength_red, stat_strength_red);
2186 HOOK(hook_dead_node_elim, stat_dead_node_elim);
2187 HOOK(hook_if_conversion, stat_if_conversion);
2188 HOOK(hook_func_call, stat_func_call);
2189 HOOK(hook_arch_dep_replace_mul_with_shifts, stat_arch_dep_replace_mul_with_shifts);
2190 HOOK(hook_arch_dep_replace_division_by_const, stat_arch_dep_replace_division_by_const);
2192 obstack_init(&status->cnts);
2193 obstack_init(&status->be_data);
2195 /* create the hash-tables */
2196 status->irg_hash = new_pset(graph_cmp, 8);
2197 status->ir_op_hash = new_pset(opcode_cmp_2, 1);
2199 /* create the wait queue */
2200 status->wait_q = new_pdeq();
2202 if (enable_options & FIRMSTAT_COUNT_STRONG_OP) {
2203 /* build the pseudo-ops */
2205 _op_Phi0.code = --num;
2206 _op_Phi0.name = new_id_from_chars(X("Phi0"));
2208 _op_PhiM.code = --num;
2209 _op_PhiM.name = new_id_from_chars(X("PhiM"));
2211 _op_ProjM.code = --num;
2212 _op_ProjM.name = new_id_from_chars(X("ProjM"));
2214 _op_MulC.code = --num;
2215 _op_MulC.name = new_id_from_chars(X("MulC"));
2217 _op_DivC.code = --num;
2218 _op_DivC.name = new_id_from_chars(X("DivC"));
2220 _op_ModC.code = --num;
2221 _op_ModC.name = new_id_from_chars(X("ModC"));
2223 _op_DivModC.code = --num;
2224 _op_DivModC.name = new_id_from_chars(X("DivModC"));
2226 status->op_Phi0 = &_op_Phi0;
2227 status->op_PhiM = &_op_PhiM;
2228 status->op_ProjM = &_op_ProjM;
2229 status->op_MulC = &_op_MulC;
2230 status->op_DivC = &_op_DivC;
2231 status->op_ModC = &_op_ModC;
2232 status->op_DivModC = &_op_DivModC;
2234 status->op_Phi0 = NULL;
2235 status->op_PhiM = NULL;
2236 status->op_ProjM = NULL;
2237 status->op_MulC = NULL;
2238 status->op_DivC = NULL;
2239 status->op_ModC = NULL;
2240 status->op_DivModC = NULL;
2243 /* for Florian: count the Sel depth */
2244 if (enable_options & FIRMSTAT_COUNT_SELS) {
2245 _op_SelSel.code = --num;
2246 _op_SelSel.name = new_id_from_chars(X("Sel(Sel)"));
2248 _op_SelSelSel.code = --num;
2249 _op_SelSelSel.name = new_id_from_chars(X("Sel(Sel(Sel))"));
2251 status->op_SelSel = &_op_SelSel;
2252 status->op_SelSelSel = &_op_SelSelSel;
2254 status->op_SelSel = NULL;
2255 status->op_SelSelSel = NULL;
2258 /* register the dumper */
2259 stat_register_dumper(&simple_dumper);
2261 if (enable_options & FIRMSTAT_CSV_OUTPUT)
2262 stat_register_dumper(&csv_dumper);
2264 /* initialize the pattern hash */
2265 stat_init_pattern_history(enable_options & FIRMSTAT_PATTERN_ENABLED);
2267 /* initialize the Const options */
2268 if (enable_options & FIRMSTAT_COUNT_CONSTS)
2269 stat_init_const_cnt(status);
2271 /* distribution table for parameter counts */
2272 status->dist_param_cnt = stat_new_int_distrib_tbl();
2274 clear_optimization_counter();
2278 } /* firm_init_stat */
2281 * Frees all dumper structures.
2283 static void stat_term_dumper(void) {
2284 dumper_t *dumper, *next_dumper;
2286 for (dumper = status->dumper; dumper; /* iteration done in loop body */ ) {
2287 if (dumper->func_map)
2288 del_pset(dumper->func_map);
2290 next_dumper = dumper->next;
2292 dumper = next_dumper;
2294 } /* stat_term_dumper */
2297 /* Terminates the statistics module, frees all memory. */
2298 void stat_term(void) {
2299 if (status != (stat_info_t *)&status_disable) {
2300 obstack_free(&status->be_data, NULL);
2301 obstack_free(&status->cnts, NULL);
2306 status = (stat_info_t *)&status_disable;
2310 /* returns 1 if statistics were initialized, 0 otherwise */
2311 int stat_is_active(void) {
2312 return status != (stat_info_t *)&status_disable;
2313 } /* stat_is_active */
2317 /* initialize the statistics module. */
2318 void firm_init_stat(unsigned enable_options) {}
2320 /* Dumps a statistics snapshot */
2321 void stat_dump_snapshot(const char *name, const char *phase) {}
2323 /* terminates the statistics module, frees all memory */
2324 void stat_term(void);
2326 #endif /* FIRM_STATISTICS */