2 * Copyright (C) 1995-2007 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 return e1->block_nr != e2->block_nr;
143 * Compare two elements of the be_block hash.
145 static int be_block_cmp(const void *elt, const void *key) {
146 const be_block_entry_t *e1 = elt;
147 const be_block_entry_t *e2 = key;
149 return e1->block_nr != e2->block_nr;
153 * Compare two elements of reg pressure hash.
155 static int reg_pressure_cmp(const void *elt, const void *key) {
156 const reg_pressure_entry_t *e1 = elt;
157 const reg_pressure_entry_t *e2 = key;
159 return e1->class_name != e2->class_name;
160 } /* reg_pressure_cmp */
163 * Compare two elements of the perm_stat hash.
165 static int perm_stat_cmp(const void *elt, const void *key) {
166 const perm_stat_entry_t *e1 = elt;
167 const perm_stat_entry_t *e2 = key;
169 return e1->perm != e2->perm;
170 } /* perm_stat_cmp */
173 * Compare two elements of the perm_class hash.
175 static int perm_class_cmp(const void *elt, const void *key) {
176 const perm_class_entry_t *e1 = elt;
177 const perm_class_entry_t *e2 = key;
179 return e1->class_name != e2->class_name;
180 } /* perm_class_cmp */
183 * Compare two elements of the ir_op hash.
185 static int opcode_cmp_2(const void *elt, const void *key) {
186 const ir_op *e1 = elt;
187 const ir_op *e2 = key;
189 return e1->code != e2->code;
193 * Compare two elements of the address_mark set.
195 static int address_mark_cmp(const void *elt, const void *key, size_t size) {
196 const address_mark_entry_t *e1 = elt;
197 const address_mark_entry_t *e2 = key;
199 /* compare only the nodes, the rest is used as data container */
200 return e1->node != e2->node;
201 } /* address_mark_cmp */
204 * Clear all counter in a node_entry_t.
206 static void opcode_clear_entry(node_entry_t *elem) {
207 cnt_clr(&elem->cnt_alive);
208 cnt_clr(&elem->new_node);
209 cnt_clr(&elem->into_Id);
210 } /* opcode_clear_entry */
213 * Returns the associates node_entry_t for an ir_op
215 * @param op the IR operation
216 * @param hmap a hash map containing ir_op* -> node_entry_t*
218 static node_entry_t *opcode_get_entry(const ir_op *op, hmap_node_entry_t *hmap) {
224 elem = pset_find(hmap, &key, op->code);
228 elem = obstack_alloc(&status->cnts, sizeof(*elem));
229 memset(elem, 0, sizeof(*elem));
232 opcode_clear_entry(elem);
236 return pset_insert(hmap, elem, op->code);
237 } /* opcode_get_entry */
240 * Returns the associates ir_op for an opcode
242 * @param code the IR opcode
243 * @param hmap the hash map containing opcode -> ir_op*
245 static ir_op *opcode_find_entry(ir_opcode code, hmap_ir_op *hmap) {
249 return pset_find(hmap, &key, code);
250 } /* opcode_find_entry */
253 * Clears all counter in a graph_entry_t.
255 * @param elem the graph entry
256 * @param all if non-zero, clears all counters, else leave accumulated ones
258 static void graph_clear_entry(graph_entry_t *elem, int all) {
261 /* clear accumulated / non-acumulated counter */
262 for (i = all ? 0 : _gcnt_non_acc; i < _gcnt_last; ++i) {
263 cnt_clr(&elem->cnt[i]);
266 if (elem->block_hash) {
267 del_pset(elem->block_hash);
268 elem->block_hash = NULL;
271 if (elem->extbb_hash) {
272 del_pset(elem->extbb_hash);
273 elem->extbb_hash = NULL;
276 obstack_free(&elem->recalc_cnts, NULL);
277 obstack_init(&elem->recalc_cnts);
278 } /* graph_clear_entry */
281 * Returns the associated graph_entry_t for an IR graph.
283 * @param irg the IR graph
284 * @param hmap the hash map containing ir_graph* -> graph_entry_t*
286 static graph_entry_t *graph_get_entry(ir_graph *irg, hmap_graph_entry_t *hmap)
294 elem = pset_find(hmap, &key, HASH_PTR(irg));
297 /* create hash map backend block information */
298 if (! elem->be_block_hash)
299 elem->be_block_hash = new_pset(be_block_cmp, 5);
304 /* allocate a new one */
305 elem = obstack_alloc(&status->cnts, sizeof(*elem));
306 memset(elem, 0, sizeof(*elem));
307 obstack_init(&elem->recalc_cnts);
310 graph_clear_entry(elem, 1);
312 /* new hash table for opcodes here */
313 elem->opcode_hash = new_pset(opcode_cmp, 5);
314 elem->address_mark = new_set(address_mark_cmp, 5);
317 /* these hash tables are created on demand */
318 elem->block_hash = NULL;
319 elem->extbb_hash = NULL;
321 for (i = 0; i < sizeof(elem->opt_hash)/sizeof(elem->opt_hash[0]); ++i)
322 elem->opt_hash[i] = new_pset(opt_cmp, 4);
324 return pset_insert(hmap, elem, HASH_PTR(irg));
325 } /* graph_get_entry */
328 * Clear all counter in an opt_entry_t.
330 static void opt_clear_entry(opt_entry_t *elem) {
331 cnt_clr(&elem->count);
332 } /* opt_clear_entry */
335 * Returns the associated opt_entry_t for an IR operation.
337 * @param op the IR operation
338 * @param hmap the hash map containing ir_op* -> opt_entry_t*
340 static opt_entry_t *opt_get_entry(const ir_op *op, hmap_opt_entry_t *hmap)
347 elem = pset_find(hmap, &key, op->code);
351 elem = obstack_alloc(&status->cnts, sizeof(*elem));
352 memset(elem, 0, sizeof(*elem));
354 /* clear new counter */
355 opt_clear_entry(elem);
359 return pset_insert(hmap, elem, op->code);
360 } /* opt_get_entry */
363 * clears all counter in a block_entry_t
365 static void block_clear_entry(block_entry_t *elem) {
368 for (i = 0; i < _bcnt_last; ++i)
369 cnt_clr(&elem->cnt[i]);
370 } /* block_clear_entry */
373 * Returns the associated block_entry_t for an block.
375 * @param block_nr an IR block number
376 * @param hmap a hash map containing long -> block_entry_t
378 static block_entry_t *block_get_entry(struct obstack *obst, long block_nr, hmap_block_entry_t *hmap)
383 key.block_nr = block_nr;
385 elem = pset_find(hmap, &key, block_nr);
389 elem = obstack_alloc(obst, sizeof(*elem));
390 memset(elem, 0, sizeof(*elem));
392 /* clear new counter */
393 block_clear_entry(elem);
395 elem->block_nr = block_nr;
397 return pset_insert(hmap, elem, block_nr);
398 } /* block_get_entry */
401 * Clear all sets in be_block_entry_t.
403 static void be_block_clear_entry(be_block_entry_t *elem)
405 if (elem->reg_pressure)
406 del_pset(elem->reg_pressure);
408 if (elem->sched_ready)
409 stat_delete_distrib_tbl(elem->sched_ready);
411 if (elem->perm_class_stat)
412 del_pset(elem->perm_class_stat);
414 elem->reg_pressure = new_pset(reg_pressure_cmp, 5);
415 elem->sched_ready = stat_new_int_distrib_tbl();
416 elem->perm_class_stat = new_pset(perm_class_cmp, 5);
417 } /* be_block_clear_entry */
420 * Returns the associated be_block_entry_t for an block.
422 * @param block_nr an IR block number
423 * @param hmap a hash map containing long -> be_block_entry_t
425 static be_block_entry_t *be_block_get_entry(struct obstack *obst, long block_nr, hmap_be_block_entry_t *hmap)
427 be_block_entry_t key;
428 be_block_entry_t *elem;
430 key.block_nr = block_nr;
432 elem = pset_find(hmap, &key, block_nr);
436 elem = obstack_alloc(obst, sizeof(*elem));
437 memset(elem, 0, sizeof(*elem));
439 /* clear new counter */
440 be_block_clear_entry(elem);
442 elem->block_nr = block_nr;
444 return pset_insert(hmap, elem, block_nr);
445 } /* be_block_get_entry */
448 * clears all sets in perm_class_entry_t
450 static void perm_class_clear_entry(perm_class_entry_t *elem) {
452 del_pset(elem->perm_stat);
454 elem->perm_stat = new_pset(perm_stat_cmp, 5);
455 } /* perm_class_clear_entry */
458 * Returns the associated perm_class entry for a register class.
460 * @param class_name the register class name
461 * @param hmap a hash map containing class_name -> perm_class_entry_t
463 static perm_class_entry_t *perm_class_get_entry(struct obstack *obst, const char *class_name,
464 hmap_perm_class_entry_t *hmap)
466 perm_class_entry_t key;
467 perm_class_entry_t *elem;
469 key.class_name = class_name;
471 elem = pset_find(hmap, &key, HASH_PTR(class_name));
475 elem = obstack_alloc(obst, sizeof(*elem));
476 memset(elem, 0, sizeof(*elem));
478 /* clear new counter */
479 perm_class_clear_entry(elem);
481 elem->class_name = class_name;
483 return pset_insert(hmap, elem, HASH_PTR(class_name));
484 } /* perm_class_get_entry */
487 * clears all sets in perm_stat_entry_t
489 static void perm_stat_clear_entry(perm_stat_entry_t *elem) {
491 stat_delete_distrib_tbl(elem->chains);
494 stat_delete_distrib_tbl(elem->cycles);
496 elem->chains = stat_new_int_distrib_tbl();
497 elem->cycles = stat_new_int_distrib_tbl();
498 } /* perm_stat_clear_entry */
501 * Returns the associated perm_stat entry for a perm.
503 * @param perm the perm node
504 * @param hmap a hash map containing perm -> perm_stat_entry_t
506 static perm_stat_entry_t *perm_stat_get_entry(struct obstack *obst, ir_node *perm, hmap_perm_stat_entry_t *hmap)
508 perm_stat_entry_t key;
509 perm_stat_entry_t *elem;
513 elem = pset_find(hmap, &key, HASH_PTR(perm));
517 elem = obstack_alloc(obst, sizeof(*elem));
518 memset(elem, 0, sizeof(*elem));
520 /* clear new counter */
521 perm_stat_clear_entry(elem);
525 return pset_insert(hmap, elem, HASH_PTR(perm));
526 } /* perm_stat_get_entry */
529 * Returns the ir_op for an IR-node,
530 * handles special cases and return pseudo op codes.
532 * @param none an IR node
534 static ir_op *stat_get_irn_op(ir_node *node)
536 ir_op *op = get_irn_op(node);
537 ir_opcode opc = op->code;
541 if (get_irn_arity(node) == 0) {
542 /* special case, a Phi0 node, count on extra counter */
543 op = status->op_Phi0 ? status->op_Phi0 : op;
544 } else if (get_irn_mode(node) == mode_M) {
545 /* special case, a Memory Phi node, count on extra counter */
546 op = status->op_PhiM ? status->op_PhiM : op;
550 if (get_irn_mode(node) == mode_M) {
551 /* special case, a Memory Proj node, count on extra counter */
552 op = status->op_ProjM ? status->op_ProjM : op;
556 if (get_irn_op(get_Mul_left(node)) == op_Const || get_irn_op(get_Mul_right(node)) == op_Const) {
557 /* special case, a Multiply by a const, count on extra counter */
558 op = status->op_MulC ? status->op_MulC : op;
562 if (get_irn_op(get_Div_right(node)) == op_Const) {
563 /* special case, a division by a const, count on extra counter */
564 op = status->op_DivC ? status->op_DivC : op;
568 if (get_irn_op(get_Mod_right(node)) == op_Const) {
569 /* special case, a module by a const, count on extra counter */
570 op = status->op_ModC ? status->op_ModC : op;
574 if (get_irn_op(get_DivMod_right(node)) == op_Const) {
575 /* special case, a division/modulo by a const, count on extra counter */
576 op = status->op_DivModC ? status->op_DivModC : op;
580 if (get_irn_op(get_Sel_ptr(node)) == op_Sel) {
581 /* special case, a Sel of a Sel, count on extra counter */
582 op = status->op_SelSel ? status->op_SelSel : op;
583 if (get_irn_op(get_Sel_ptr(get_Sel_ptr(node))) == op_Sel) {
584 /* special case, a Sel of a Sel of a Sel, count on extra counter */
585 op = status->op_SelSelSel ? status->op_SelSelSel : op;
594 } /* stat_get_irn_op */
597 * update the block counter
599 static void undate_block_info(ir_node *node, graph_entry_t *graph)
601 ir_op *op = get_irn_op(node);
603 block_entry_t *b_entry;
606 /* check for block */
607 if (op == op_Block) {
608 arity = get_irn_arity(node);
609 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(node), graph->block_hash);
611 /* count all incoming edges */
612 for (i = 0; i < arity; ++i) {
613 ir_node *pred = get_irn_n(node, i);
614 ir_node *other_block = get_nodes_block(pred);
615 block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
617 cnt_inc(&b_entry->cnt[bcnt_in_edges]); /* an edge coming from another block */
618 cnt_inc(&b_entry_other->cnt[bcnt_out_edges]);
623 block = get_nodes_block(node);
624 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(block), graph->block_hash);
626 if (op == op_Phi && mode_is_datab(get_irn_mode(node))) {
627 /* count data Phi per block */
628 cnt_inc(&b_entry->cnt[bcnt_phi_data]);
631 /* we have a new node in our block */
632 cnt_inc(&b_entry->cnt[bcnt_nodes]);
634 /* don't count keep-alive edges */
635 if (get_irn_op(node) == op_End)
638 arity = get_irn_arity(node);
640 for (i = 0; i < arity; ++i) {
641 ir_node *pred = get_irn_n(node, i);
642 ir_node *other_block;
644 other_block = get_nodes_block(pred);
646 if (other_block == block)
647 cnt_inc(&b_entry->cnt[bcnt_edges]); /* a in block edge */
649 block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
651 cnt_inc(&b_entry->cnt[bcnt_in_edges]); /* an edge coming from another block */
652 cnt_inc(&b_entry_other->cnt[bcnt_out_edges]);
655 } /* undate_block_info */
658 * Update the extended block counter.
660 static void update_extbb_info(ir_node *node, graph_entry_t *graph)
662 ir_op *op = get_irn_op(node);
664 extbb_entry_t *eb_entry;
667 /* check for block */
668 if (op == op_Block) {
669 extbb = get_nodes_extbb(node);
670 arity = get_irn_arity(node);
671 eb_entry = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(extbb), graph->extbb_hash);
673 /* count all incoming edges */
674 for (i = 0; i < arity; ++i) {
675 ir_node *pred = get_irn_n(node, i);
676 ir_extblk *other_extbb = get_nodes_extbb(pred);
678 if (extbb != other_extbb) {
679 extbb_entry_t *eb_entry_other = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(other_extbb), graph->extbb_hash);
681 cnt_inc(&eb_entry->cnt[bcnt_in_edges]); /* an edge coming from another extbb */
682 cnt_inc(&eb_entry_other->cnt[bcnt_out_edges]);
688 extbb = get_nodes_extbb(node);
689 eb_entry = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(extbb), graph->extbb_hash);
691 if (op == op_Phi && mode_is_datab(get_irn_mode(node))) {
692 /* count data Phi per extbb */
693 cnt_inc(&eb_entry->cnt[bcnt_phi_data]);
696 /* we have a new node in our block */
697 cnt_inc(&eb_entry->cnt[bcnt_nodes]);
699 /* don't count keep-alive edges */
700 if (get_irn_op(node) == op_End)
703 arity = get_irn_arity(node);
705 for (i = 0; i < arity; ++i) {
706 ir_node *pred = get_irn_n(node, i);
707 ir_extblk *other_extbb = get_nodes_extbb(pred);
709 if (other_extbb == extbb)
710 cnt_inc(&eb_entry->cnt[bcnt_edges]); /* a in extbb edge */
712 extbb_entry_t *eb_entry_other = block_get_entry(&graph->recalc_cnts, get_extbb_node_nr(other_extbb), graph->extbb_hash);
714 cnt_inc(&eb_entry->cnt[bcnt_in_edges]); /* an edge coming from another extbb */
715 cnt_inc(&eb_entry_other->cnt[bcnt_out_edges]);
718 } /* update_extbb_info */
721 * Calculates how many arguments of the call are const, updates
722 * param distribution.
724 static void analyse_params_of_Call(graph_entry_t *graph, ir_node *call) {
725 int i, num_const_args = 0, num_local_adr = 0;
726 int n = get_Call_n_params(call);
728 for (i = 0; i < n; ++i) {
729 ir_node *param = get_Call_param(call, i);
731 if (is_irn_constlike(param))
733 else if (is_Sel(param)) {
734 ir_node *base = param;
737 base = get_Sel_ptr(base);
738 } while (is_Sel(base));
740 if (base == get_irg_frame(current_ir_graph))
746 if (num_const_args > 0)
747 cnt_inc(&graph->cnt[gcnt_call_with_cnst_arg]);
748 if (num_const_args == n)
749 cnt_inc(&graph->cnt[gcnt_call_with_all_cnst_arg]);
750 if (num_local_adr > 0)
751 cnt_inc(&graph->cnt[gcnt_call_with_local_adr]);
753 stat_inc_int_distrib_tbl(status->dist_param_cnt, n);
754 } /* analyse_params_of_Call */
757 * Update info on calls.
759 * @param call The call
760 * @param graph The graph entry containing the call
762 static void stat_update_call(ir_node *call, graph_entry_t *graph)
764 ir_node *block = get_nodes_block(call);
765 ir_node *ptr = get_Call_ptr(call);
766 ir_entity *ent = NULL;
767 ir_graph *callee = NULL;
770 * If the block is bad, the whole subgraph will collapse later
771 * so do not count this call.
772 * This happens in dead code.
777 cnt_inc(&graph->cnt[gcnt_all_calls]);
779 /* found a call, this function is not a leaf */
782 if (get_irn_op(ptr) == op_SymConst) {
783 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
784 /* ok, we seems to know the entity */
785 ent = get_SymConst_entity(ptr);
786 callee = get_entity_irg(ent);
788 /* it is recursive, if it calls at least once */
789 if (callee == graph->irg)
790 graph->is_recursive = 1;
793 /* indirect call, be could not predict */
794 cnt_inc(&graph->cnt[gcnt_indirect_calls]);
796 /* NOT a leaf call */
797 graph->is_leaf_call = LCS_NON_LEAF_CALL;
800 /* check, if it's a chain-call: Then, the call-block
801 * must dominate the end block. */
803 ir_node *curr = get_irg_end_block(graph->irg);
804 int depth = get_Block_dom_depth(block);
806 for (; curr != block && get_Block_dom_depth(curr) > depth;) {
807 curr = get_Block_idom(curr);
809 if (! curr || is_no_Block(curr))
814 graph->is_chain_call = 0;
817 /* check, if the callee is a leaf */
819 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
821 if (called->is_analyzed) {
822 if (! called->is_leaf)
823 graph->is_leaf_call = LCS_NON_LEAF_CALL;
827 analyse_params_of_Call(graph, call);
828 } /* stat_update_call */
831 * Update info on calls for graphs on the wait queue.
833 static void stat_update_call_2(ir_node *call, graph_entry_t *graph)
835 ir_node *block = get_nodes_block(call);
836 ir_node *ptr = get_Call_ptr(call);
837 ir_entity *ent = NULL;
838 ir_graph *callee = NULL;
841 * If the block is bad, the whole subgraph will collapse later
842 * so do not count this call.
843 * This happens in dead code.
848 if (get_irn_op(ptr) == op_SymConst) {
849 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
850 /* ok, we seems to know the entity */
851 ent = get_SymConst_entity(ptr);
852 callee = get_entity_irg(ent);
856 /* check, if the callee is a leaf */
858 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
860 assert(called->is_analyzed);
862 if (! called->is_leaf)
863 graph->is_leaf_call = LCS_NON_LEAF_CALL;
865 graph->is_leaf_call = LCS_NON_LEAF_CALL;
866 } /* stat_update_call_2 */
869 * Find the base address and entity of an Sel node.
871 * @param sel the node
873 * @return the base address.
875 static ir_node *find_base_adr(ir_node *sel) {
876 ir_node *ptr = get_Sel_ptr(sel);
878 while (is_Sel(ptr)) {
880 ptr = get_Sel_ptr(sel);
883 } /* find_base_adr */
886 * Update info on Load/Store address statistics.
888 static void stat_update_address(ir_node *node, graph_entry_t *graph) {
889 ir_opcode opc = get_irn_opcode(node);
895 /* a global address */
896 cnt_inc(&graph->cnt[gcnt_global_adr]);
899 base = find_base_adr(node);
900 irg = current_ir_graph;
901 if (base == get_irg_tls(irg)) {
902 /* a TLS variable, like a global. */
903 cnt_inc(&graph->cnt[gcnt_global_adr]);
904 } else if (base == get_irg_frame(irg)) {
905 /* a local Variable. */
906 cnt_inc(&graph->cnt[gcnt_local_adr]);
909 if (is_Proj(base) && skip_Proj(get_Proj_pred(base)) == get_irg_start(irg)) {
910 /* pointer access through parameter, check for THIS */
911 ir_entity *ent = get_irg_entity(irg);
914 ir_type *ent_tp = get_entity_type(ent);
916 if (get_method_calling_convention(ent_tp) & cc_this_call) {
917 if (get_Proj_proj(base) == 0) {
919 cnt_inc(&graph->cnt[gcnt_this_adr]);
924 /* other parameter */
925 cnt_inc(&graph->cnt[gcnt_param_adr]);
928 /* unknown Pointer access */
929 cnt_inc(&graph->cnt[gcnt_other_adr]);
935 } /* stat_update_address */
938 * Walker for reachable nodes count.
940 static void update_node_stat(ir_node *node, void *env)
942 graph_entry_t *graph = env;
945 ir_op *op = stat_get_irn_op(node);
946 int arity = get_irn_arity(node);
948 entry = opcode_get_entry(op, graph->opcode_hash);
950 cnt_inc(&entry->cnt_alive);
951 cnt_add_i(&graph->cnt[gcnt_edges], arity);
953 /* count block edges */
954 undate_block_info(node, graph);
956 /* count extended block edges */
957 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
958 if (graph->irg != get_const_code_irg())
959 update_extbb_info(node, graph);
962 /* handle statistics for special node types */
966 if (status->stat_options & FIRMSTAT_COUNT_CONSTS) {
967 /* check properties of constants */
968 stat_update_const(status, node, graph);
972 /* check for properties that depends on calls like recursion/leaf/indirect call */
973 stat_update_call(node, graph);
976 /* check address properties */
977 stat_update_address(get_Load_ptr(node), graph);
980 /* check address properties */
981 stat_update_address(get_Store_ptr(node), graph);
986 } /* update_node_stat */
989 * Walker for reachable nodes count for graphs on the wait_q.
991 static void update_node_stat_2(ir_node *node, void *env) {
992 graph_entry_t *graph = env;
994 /* check for properties that depends on calls like recursion/leaf/indirect call */
996 stat_update_call_2(node, graph);
997 } /* update_node_stat_2 */
1000 * Get the current address mark.
1002 static unsigned get_adr_mark(graph_entry_t *graph, ir_node *node) {
1003 address_mark_entry_t *value = set_find(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
1005 return value ? value->mark : 0;
1006 } /* get_adr_mark */
1009 * Set the current address mark.
1011 static void set_adr_mark(graph_entry_t *graph, ir_node *node, unsigned val) {
1012 address_mark_entry_t *value = set_insert(graph->address_mark, &node, sizeof(*value), HASH_PTR(node));
1015 } /* set_adr_mark */
1017 #undef DUMP_ADR_MODE
1019 #ifdef DUMP_ADR_MODE
1021 * a vcg attribute hook: Color a node with a different color if
1022 * it's identified as a part of an address expression or at least referenced
1023 * by an address expression.
1025 static int stat_adr_mark_hook(FILE *F, ir_node *node, ir_node *local)
1027 ir_node *n = local ? local : node;
1028 ir_graph *irg = get_irn_irg(n);
1029 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1030 unsigned mark = get_adr_mark(graph, n);
1032 if (mark & MARK_ADDRESS_CALC)
1033 fprintf(F, "color: purple");
1034 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1035 fprintf(F, "color: pink");
1036 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1037 fprintf(F, "color: lightblue");
1041 /* I know the color! */
1043 } /* stat_adr_mark_hook */
1044 #endif /* DUMP_ADR_MODE */
1047 * Return the "operational" mode of a Firm node.
1049 static ir_mode *get_irn_op_mode(ir_node *node) {
1050 switch (get_irn_opcode(node)) {
1052 return get_Load_mode(node);
1054 return get_irn_mode(get_Store_value(node));
1056 return get_irn_mode(get_DivMod_left(node));
1058 return get_irn_mode(get_Div_left(node));
1060 return get_irn_mode(get_Mod_left(node));
1062 /* Cmp is no address calculation, or is it? */
1064 return get_irn_mode(node);
1066 } /* get_irn_op_mode */
1069 * Post-walker that marks every node that is an address calculation.
1071 * Users of a node must be visited first. We ensure this by
1072 * calling it in the post of an outs walk. This should work even in cycles,
1073 * while the normal pre-walk will not.
1075 static void mark_address_calc(ir_node *node, void *env) {
1076 graph_entry_t *graph = env;
1077 ir_mode *mode = get_irn_op_mode(node);
1079 unsigned mark_preds = MARK_REF_NON_ADR;
1081 if (! mode_is_numP(mode))
1084 if (mode_is_reference(mode)) {
1085 /* a reference is calculated here, we are sure */
1086 set_adr_mark(graph, node, MARK_ADDRESS_CALC);
1088 mark_preds = MARK_REF_ADR;
1090 unsigned mark = get_adr_mark(graph, node);
1092 if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR) {
1094 * this node has no reference mode, but is only
1095 * referenced by address calculations
1097 mark_preds = MARK_REF_ADR;
1101 /* mark all predecessors */
1102 for (i = 0, n = get_irn_arity(node); i < n; ++i) {
1103 ir_node *pred = get_irn_n(node, i);
1105 mode = get_irn_op_mode(pred);
1106 if (! mode_is_numP(mode))
1109 set_adr_mark(graph, pred, get_adr_mark(graph, pred) | mark_preds);
1111 } /* mark_address_calc */
1114 * Post-walker that marks every node that is an address calculation.
1116 * Users of a node must be visited first. We ensure this by
1117 * calling it in the post of an outs walk. This should work even in cycles,
1118 * while the normal pre-walk will not.
1120 static void count_adr_ops(ir_node *node, void *env) {
1121 graph_entry_t *graph = env;
1122 unsigned mark = get_adr_mark(graph, node);
1124 if (mark & MARK_ADDRESS_CALC)
1125 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1126 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1127 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1128 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1129 cnt_inc(&graph->cnt[gcnt_all_adr_ops]);
1130 } /* count_adr_ops */
1133 * Called for every graph when the graph is either deleted or stat_dump_snapshot()
1134 * is called, must recalculate all statistic info.
1136 * @param global The global entry
1137 * @param graph The current entry
1139 static void update_graph_stat(graph_entry_t *global, graph_entry_t *graph)
1141 node_entry_t *entry;
1144 /* clear first the alive counter in the graph */
1145 foreach_pset(graph->opcode_hash, entry) {
1146 cnt_clr(&entry->cnt_alive);
1147 } /* foreach_pset */
1149 /* set pessimistic values */
1151 graph->is_leaf_call = LCS_UNKNOWN;
1152 graph->is_recursive = 0;
1153 graph->is_chain_call = 1;
1155 /* create new block counter */
1156 graph->block_hash = new_pset(block_cmp, 5);
1158 /* we need dominator info */
1159 if (graph->irg != get_const_code_irg()) {
1160 assure_doms(graph->irg);
1162 if (status->stat_options & FIRMSTAT_COUNT_EXTBB) {
1163 /* we need extended basic blocks */
1164 compute_extbb(graph->irg);
1166 /* create new extbb counter */
1167 graph->extbb_hash = new_pset(block_cmp, 5);
1171 /* count the nodes in the graph */
1172 irg_walk_graph(graph->irg, update_node_stat, NULL, graph);
1175 /* Uncomment this code if chain-call means call exact one. */
1176 entry = opcode_get_entry(op_Call, graph->opcode_hash);
1178 /* check if we have more than 1 call */
1179 if (cnt_gt(entry->cnt_alive, 1))
1180 graph->is_chain_call = 0;
1183 /* recursive functions are never chain calls, leafs don't have calls */
1184 if (graph->is_recursive || graph->is_leaf)
1185 graph->is_chain_call = 0;
1187 /* assume we walk every graph only ONCE, we could sum here the global count */
1188 foreach_pset(graph->opcode_hash, entry) {
1189 node_entry_t *g_entry = opcode_get_entry(entry->op, global->opcode_hash);
1191 /* update the node counter */
1192 cnt_add(&g_entry->cnt_alive, &entry->cnt_alive);
1193 } /* foreach_pset */
1195 /* count the number of address calculation */
1196 if (graph->irg != get_const_code_irg()) {
1197 ir_graph *rem = current_ir_graph;
1199 assure_irg_outs(graph->irg);
1201 /* Must be done an the outs graph */
1202 current_ir_graph = graph->irg;
1203 irg_out_walk(get_irg_start(graph->irg), NULL, mark_address_calc, graph);
1204 current_ir_graph = rem;
1206 #ifdef DUMP_ADR_MODE
1207 /* register the vcg hook and dump the graph for test */
1208 set_dump_node_vcgattr_hook(stat_adr_mark_hook);
1209 dump_ir_block_graph(graph->irg, "-adr");
1210 set_dump_node_vcgattr_hook(NULL);
1211 #endif /* DUMP_ADR_MODE */
1213 irg_walk_graph(graph->irg, NULL, count_adr_ops, graph);
1216 /* count the DAG's */
1217 if (status->stat_options & FIRMSTAT_COUNT_DAG)
1218 count_dags_in_graph(global, graph);
1220 /* calculate the patterns of this graph */
1221 stat_calc_pattern_history(graph->irg);
1223 /* leaf function did not call others */
1225 graph->is_leaf_call = LCS_NON_LEAF_CALL;
1226 else if (graph->is_leaf_call == LCS_UNKNOWN) {
1227 /* we still don't know if this graph calls leaf-functions, so enqueue */
1228 pdeq_putl(status->wait_q, graph);
1231 /* we have analyzed this graph */
1232 graph->is_analyzed = 1;
1234 /* accumulate all counter's */
1235 for (i = 0; i < _gcnt_last; ++i)
1236 cnt_add(&global->cnt[i], &graph->cnt[i]);
1237 } /* update_graph_stat */
1240 * Called for every graph that was on the wait_q in stat_dump_snapshot()
1241 * must finish all statistic info calculations.
1243 * @param global The global entry
1244 * @param graph The current entry
1246 static void update_graph_stat_2(graph_entry_t *global, graph_entry_t *graph)
1248 if (graph->is_deleted) {
1249 /* deleted, ignore */
1254 /* count the nodes in the graph */
1255 irg_walk_graph(graph->irg, update_node_stat_2, NULL, graph);
1257 if (graph->is_leaf_call == LCS_UNKNOWN)
1258 graph->is_leaf_call = LCS_LEAF_CALL;
1260 } /* update_graph_stat_2 */
1263 * Register a dumper.
1265 static void stat_register_dumper(const dumper_t *dumper) {
1266 dumper_t *p = xmalloc(sizeof(*p));
1269 memcpy(p, dumper, sizeof(*p));
1271 p->next = status->dumper;
1276 /* FIXME: memory leak */
1277 } /* stat_register_dumper */
1280 * Dumps the statistics of an IR graph.
1282 static void stat_dump_graph(graph_entry_t *entry) {
1285 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1286 if (dumper->dump_graph)
1287 dumper->dump_graph(dumper, entry);
1289 } /* stat_dump_graph */
1292 * Calls all registered dumper functions.
1294 static void stat_dump_registered(graph_entry_t *entry) {
1297 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1298 if (dumper->func_map) {
1299 dump_graph_FUNC func;
1301 foreach_pset(dumper->func_map, func)
1302 func(dumper, entry);
1305 } /* stat_dump_registered */
1308 * Dumps a constant table.
1310 static void stat_dump_consts(const constant_info_t *tbl) {
1313 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1314 if (dumper->dump_const_tbl)
1315 dumper->dump_const_tbl(dumper, tbl);
1317 } /* stat_dump_consts */
1320 * Dumps the parameter distribution
1322 static void stat_dump_param_tbl(const distrib_tbl_t *tbl, graph_entry_t *global) {
1325 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1326 if (dumper->dump_const_tbl)
1327 dumper->dump_param_tbl(dumper, tbl, global);
1332 * Initialize the dumper.
1334 static void stat_dump_init(const char *name) {
1337 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1339 dumper->init(dumper, name);
1341 } /* stat_dump_init */
1344 * Finish the dumper.
1346 static void stat_dump_finish(void) {
1349 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1351 dumper->finish(dumper);
1353 } /* stat_dump_finish */
1356 * Register an additional function for all dumper.
1358 void stat_register_dumper_func(dump_graph_FUNC func) {
1361 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1362 if (! dumper->func_map)
1363 dumper->func_map = pset_new_ptr(3);
1364 pset_insert_ptr(dumper->func_map, func);
1366 } /* stat_register_dumper_func */
1368 /* ---------------------------------------------------------------------- */
1371 * Helper: get an ir_op from an opcode.
1373 ir_op *stat_get_op_from_opcode(ir_opcode code) {
1374 return opcode_find_entry(code, status->ir_op_hash);
1375 } /* stat_get_op_from_opcode */
1378 * Hook: A new IR op is registered.
1380 * @param ctx the hook context
1381 * @param op the new IR opcode that was created.
1383 static void stat_new_ir_op(void *ctx, ir_op *op) {
1384 if (! status->stat_options)
1389 graph_entry_t *graph = graph_get_entry(NULL, status->irg_hash);
1391 /* execute for side effect :-) */
1392 (void)opcode_get_entry(op, graph->opcode_hash);
1394 pset_insert(status->ir_op_hash, op, op->code);
1397 } /* stat_new_ir_op */
1400 * Hook: An IR op is freed.
1402 * @param ctx the hook context
1403 * @param op the IR opcode that is freed
1405 static void stat_free_ir_op(void *ctx, ir_op *op) {
1406 if (! status->stat_options)
1413 } /* stat_free_ir_op */
1416 * Hook: A new node is created.
1418 * @param ctx the hook context
1419 * @param irg the IR graph on which the node is created
1420 * @param node the new IR node that was created
1422 static void stat_new_node(void *ctx, ir_graph *irg, ir_node *node) {
1423 if (! status->stat_options)
1426 /* do NOT count during dead node elimination */
1427 if (status->in_dead_node_elim)
1432 node_entry_t *entry;
1433 graph_entry_t *graph;
1434 ir_op *op = stat_get_irn_op(node);
1436 /* increase global value */
1437 graph = graph_get_entry(NULL, status->irg_hash);
1438 entry = opcode_get_entry(op, graph->opcode_hash);
1439 cnt_inc(&entry->new_node);
1441 /* increase local value */
1442 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1443 entry = opcode_get_entry(op, graph->opcode_hash);
1444 cnt_inc(&entry->new_node);
1447 } /* stat_new_node */
1450 * Hook: A node is changed into a Id node
1452 * @param ctx the hook context
1453 * @param node the IR node that will be turned into an ID
1455 static void stat_turn_into_id(void *ctx, ir_node *node) {
1456 if (! status->stat_options)
1461 node_entry_t *entry;
1462 graph_entry_t *graph;
1463 ir_op *op = stat_get_irn_op(node);
1465 /* increase global value */
1466 graph = graph_get_entry(NULL, status->irg_hash);
1467 entry = opcode_get_entry(op, graph->opcode_hash);
1468 cnt_inc(&entry->into_Id);
1470 /* increase local value */
1471 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1472 entry = opcode_get_entry(op, graph->opcode_hash);
1473 cnt_inc(&entry->into_Id);
1476 } /* stat_turn_into_id */
1479 * Hook: A new graph was created
1481 * @param ctx the hook context
1482 * @param irg the new IR graph that was created
1483 * @param ent the entity of this graph
1485 static void stat_new_graph(void *ctx, ir_graph *irg, ir_entity *ent) {
1486 if (! status->stat_options)
1491 /* execute for side effect :-) */
1492 graph_entry_t * graph = graph_get_entry(irg, status->irg_hash);
1495 graph->is_deleted = 0;
1497 graph->is_leaf_call = 0;
1498 graph->is_recursive = 0;
1499 graph->is_chain_call = 0;
1500 graph->is_analyzed = 0;
1503 } /* stat_new_graph */
1506 * Hook: A graph will be deleted
1508 * @param ctx the hook context
1509 * @param irg the IR graph that will be deleted
1511 * Note that we still hold the information for this graph
1512 * in our hash maps, only a flag is set which prevents this
1513 * information from being changed, it's "frozen" from now.
1515 static void stat_free_graph(void *ctx, ir_graph *irg) {
1516 if (! status->stat_options)
1521 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1522 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1524 graph->is_deleted = 1;
1526 if (status->stat_options & FIRMSTAT_COUNT_DELETED) {
1527 /* count the nodes of the graph yet, it will be destroyed later */
1528 update_graph_stat(global, graph);
1532 } /* stat_free_graph */
1535 * Hook: A walk over a graph is initiated. Do not count walks from statistic code.
1537 * @param ctx the hook context
1538 * @param irg the IR graph that will be walked
1539 * @param pre the pre walker
1540 * @param post the post walker
1542 static void stat_irg_walk(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1544 if (! status->stat_options)
1549 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1551 cnt_inc(&graph->cnt[gcnt_acc_walked]);
1554 } /* stat_irg_walk */
1557 * Hook: A walk over a graph in block-wise order is initiated. Do not count walks from statistic code.
1559 * @param ctx the hook context
1560 * @param irg the IR graph that will be walked
1561 * @param pre the pre walker
1562 * @param post the post walker
1564 static void stat_irg_walk_blkwise(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1566 /* for now, do NOT differentiate between blockwise and normal */
1567 stat_irg_walk(ctx, irg, pre, post);
1568 } /* stat_irg_walk_blkwise */
1571 * Hook: A walk over the graph's blocks is initiated. Do not count walks from statistic code.
1573 * @param ctx the hook context
1574 * @param irg the IR graph that will be walked
1575 * @param node the IR node
1576 * @param pre the pre walker
1577 * @param post the post walker
1579 static void stat_irg_block_walk(void *ctx, ir_graph *irg, ir_node *node, generic_func *pre, generic_func *post)
1581 if (! status->stat_options)
1586 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1588 cnt_inc(&graph->cnt[gcnt_acc_walked_blocks]);
1591 } /* stat_irg_block_walk */
1594 * Called for every node that is removed due to an optimization.
1596 * @param n the IR node that will be removed
1597 * @param hmap the hash map containing ir_op* -> opt_entry_t*
1599 static void removed_due_opt(ir_node *n, hmap_opt_entry_t *hmap) {
1600 ir_op *op = stat_get_irn_op(n);
1601 opt_entry_t *entry = opt_get_entry(op, hmap);
1603 /* increase global value */
1604 cnt_inc(&entry->count);
1605 } /* removed_due_opt */
1608 * Hook: Some nodes were optimized into some others due to an optimization.
1610 * @param ctx the hook context
1612 static void stat_merge_nodes(
1614 ir_node **new_node_array, int new_num_entries,
1615 ir_node **old_node_array, int old_num_entries,
1618 if (! status->stat_options)
1624 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1626 if (status->reassoc_run)
1627 opt = HOOK_OPT_REASSOC;
1629 for (i = 0; i < old_num_entries; ++i) {
1630 /* nodes might be in new and old, so if we found a node
1631 in both sets, this one is NOT removed */
1632 for (j = 0; j < new_num_entries; ++j) {
1633 if (old_node_array[i] == new_node_array[j])
1636 if (j >= new_num_entries) {
1639 /* sometimes we did not detect, that it is replaced by a Const */
1640 if (opt == HOOK_OPT_CONFIRM && new_num_entries == 1) {
1641 ir_op *op = get_irn_op(new_node_array[0]);
1643 if (op == op_Const || op == op_SymConst)
1644 xopt = HOOK_OPT_CONFIRM_C;
1647 removed_due_opt(old_node_array[i], graph->opt_hash[xopt]);
1652 } /* stat_merge_nodes */
1655 * Hook: Reassociation is started/stopped.
1657 * @param ctx the hook context
1658 * @param flag if non-zero, reassociation is started else stopped
1660 static void stat_reassociate(void *ctx, int flag) {
1661 if (! status->stat_options)
1666 status->reassoc_run = flag;
1669 } /* stat_reassociate */
1672 * Hook: A node was lowered into other nodes
1674 * @param ctx the hook context
1675 * @param node the IR node that will be lowered
1677 static void stat_lower(void *ctx, ir_node *node) {
1678 if (! status->stat_options)
1683 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1685 removed_due_opt(node, graph->opt_hash[HOOK_LOWERED]);
1691 * Hook: A graph was inlined.
1693 * @param ctx the hook context
1694 * @param call the IR call that will re changed into the body of
1695 * the called IR graph
1696 * @param called_irg the IR graph representing the called routine
1698 static void stat_inline(void *ctx, ir_node *call, ir_graph *called_irg)
1700 if (! status->stat_options)
1705 ir_graph *irg = get_irn_irg(call);
1706 graph_entry_t *i_graph = graph_get_entry(called_irg, status->irg_hash);
1707 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1709 cnt_inc(&graph->cnt[gcnt_acc_got_inlined]);
1710 cnt_inc(&i_graph->cnt[gcnt_acc_was_inlined]);
1716 * Hook: A graph with tail-recursions was optimized.
1718 * @param ctx the hook context
1720 static void stat_tail_rec(void *ctx, ir_graph *irg, int n_calls) {
1721 if (! status->stat_options)
1726 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1728 graph->num_tail_recursion += n_calls;
1731 } /* stat_tail_rec */
1734 * Strength reduction was performed on an iteration variable.
1736 * @param ctx the hook context
1738 static void stat_strength_red(void *ctx, ir_graph *irg, ir_node *strong) {
1739 if (! status->stat_options)
1744 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1745 cnt_inc(&graph->cnt[gcnt_acc_strength_red]);
1747 removed_due_opt(strong, graph->opt_hash[HOOK_OPT_STRENGTH_RED]);
1750 } /* stat_strength_red */
1753 * Hook: Start/Stop the dead node elimination.
1755 * @param ctx the hook context
1757 static void stat_dead_node_elim(void *ctx, ir_graph *irg, int start) {
1758 if (! status->stat_options)
1761 status->in_dead_node_elim = (start != 0);
1762 } /* stat_dead_node_elim */
1765 * Hook: if-conversion was tried.
1767 static void stat_if_conversion(void *context, ir_graph *irg, ir_node *phi,
1768 int pos, ir_node *mux, if_result_t reason)
1770 if (! status->stat_options)
1775 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1777 cnt_inc(&graph->cnt[gcnt_if_conv + reason]);
1780 } /* stat_if_conversion */
1783 * Hook: real function call was optimized.
1785 static void stat_func_call(void *context, ir_graph *irg, ir_node *call)
1787 if (! status->stat_options)
1792 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1794 cnt_inc(&graph->cnt[gcnt_acc_real_func_call]);
1797 } /* stat_func_call */
1800 * Hook: A multiply was replaced by a series of Shifts/Adds/Subs.
1802 * @param ctx the hook context
1804 static void stat_arch_dep_replace_mul_with_shifts(void *ctx, ir_node *mul) {
1805 if (! status->stat_options)
1810 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1811 removed_due_opt(mul, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1814 } /* stat_arch_dep_replace_mul_with_shifts */
1817 * Hook: A division by const was replaced.
1819 * @param ctx the hook context
1820 * @param node the division node that will be optimized
1822 static void stat_arch_dep_replace_division_by_const(void *ctx, ir_node *node) {
1823 if (! status->stat_options)
1828 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1829 removed_due_opt(node, graph->opt_hash[HOOK_OPT_ARCH_DEP]);
1832 } /* stat_arch_dep_replace_division_by_const */
1835 * Update the register pressure of a block.
1837 * @param irg the irg containing the block
1838 * @param block the block for which the reg pressure should be set
1839 * @param pressure the pressure
1840 * @param class_name the name of the register class
1842 void stat_be_block_regpressure(ir_graph *irg, ir_node *block, int pressure, const char *class_name)
1844 if (! status->stat_options)
1849 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1850 be_block_entry_t *block_ent;
1851 reg_pressure_entry_t *rp_ent;
1853 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1854 rp_ent = obstack_alloc(&status->be_data, sizeof(*rp_ent));
1855 memset(rp_ent, 0, sizeof(*rp_ent));
1857 rp_ent->class_name = class_name;
1858 rp_ent->pressure = pressure;
1860 pset_insert(block_ent->reg_pressure, rp_ent, HASH_PTR(class_name));
1863 } /* stat_be_block_regpressure */
1866 * Update the distribution of ready nodes of a block
1868 * @param irg the irg containing the block
1869 * @param block the block for which the reg pressure should be set
1870 * @param num_ready the number of ready nodes
1872 void stat_be_block_sched_ready(ir_graph *irg, ir_node *block, int num_ready)
1874 if (! status->stat_options)
1879 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1880 be_block_entry_t *block_ent;
1882 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1884 /* increase the counter of corresponding number of ready nodes */
1885 stat_inc_int_distrib_tbl(block_ent->sched_ready, num_ready);
1888 } /* stat_be_block_sched_ready */
1891 * Update the permutation statistic of a block.
1893 * @param class_name the name of the register class
1894 * @param n_regs number of registers in the register class
1895 * @param perm the perm node
1896 * @param block the block containing the perm
1897 * @param size the size of the perm
1898 * @param real_size number of pairs with different registers
1900 void stat_be_block_stat_perm(const char *class_name, int n_regs, ir_node *perm, ir_node *block,
1901 int size, int real_size)
1903 if (! status->stat_options)
1908 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
1909 be_block_entry_t *block_ent;
1910 perm_class_entry_t *pc_ent;
1911 perm_stat_entry_t *ps_ent;
1913 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1914 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
1915 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
1917 pc_ent->n_regs = n_regs;
1919 /* update information */
1920 ps_ent->size = size;
1921 ps_ent->real_size = real_size;
1924 } /* stat_be_block_stat_perm */
1927 * Update the permutation statistic of a single perm.
1929 * @param class_name the name of the register class
1930 * @param perm the perm node
1931 * @param block the block containing the perm
1932 * @param is_chain 1 if chain, 0 if cycle
1933 * @param size length of the cycle/chain
1934 * @param n_ops the number of ops representing this cycle/chain after lowering
1936 void stat_be_block_stat_permcycle(const char *class_name, ir_node *perm, ir_node *block,
1937 int is_chain, int size, int n_ops)
1939 if (! status->stat_options)
1944 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
1945 be_block_entry_t *block_ent;
1946 perm_class_entry_t *pc_ent;
1947 perm_stat_entry_t *ps_ent;
1949 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1950 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
1951 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
1954 ps_ent->n_copies += n_ops;
1955 stat_inc_int_distrib_tbl(ps_ent->chains, size);
1957 ps_ent->n_exchg += n_ops;
1958 stat_inc_int_distrib_tbl(ps_ent->cycles, size);
1962 } /* stat_be_block_stat_permcycle */
1964 /* Dumps a statistics snapshot. */
1965 void stat_dump_snapshot(const char *name, const char *phase)
1971 if (! status->stat_options)
1976 graph_entry_t *entry;
1977 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1980 * The constant counter is only global, so we clear it here.
1981 * Note that it does NOT contain the constants in DELETED
1982 * graphs due to this.
1984 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
1985 stat_const_clear(status);
1987 /* build the name */
1988 p = strrchr(name, '/');
1993 q = strrchr(name, '\\');
1995 /* NULL might be not the smallest pointer */
1996 if (q && (!p || q > p))
2004 if (l > sizeof(fname) - 1)
2005 l = sizeof(fname) - 1;
2007 memcpy(fname, name, l);
2013 strncat(fname, "firmstat-", sizeof(fname));
2014 strncat(fname, phase, sizeof(fname));
2015 strncat(fname, "-", sizeof(fname));
2016 strncat(fname, p, sizeof(fname));
2018 stat_dump_init(fname);
2020 /* calculate the graph statistics */
2021 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
2022 if (entry->irg == NULL) {
2023 /* special entry for the global count */
2026 if (! entry->is_deleted) {
2027 /* the graph is still alive, count the nodes on it */
2028 update_graph_stat(global, entry);
2032 /* some calculations are dependent, we pushed them on the wait_q */
2033 while (! pdeq_empty(status->wait_q)) {
2034 entry = pdeq_getr(status->wait_q);
2036 update_graph_stat_2(global, entry);
2039 /* dump per graph */
2040 for (entry = pset_first(status->irg_hash); entry; entry = pset_next(status->irg_hash)) {
2041 if (entry->irg == NULL) {
2042 /* special entry for the global count */
2046 if (! entry->is_deleted || status->stat_options & FIRMSTAT_COUNT_DELETED) {
2047 stat_dump_graph(entry);
2048 stat_dump_registered(entry);
2051 if (! entry->is_deleted) {
2052 /* clear the counter that are not accumulated */
2053 graph_clear_entry(entry, 0);
2058 stat_dump_graph(global);
2060 /* dump the const info */
2061 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2062 stat_dump_consts(&status->const_info);
2064 /* dump the parameter distribution */
2065 stat_dump_param_tbl(status->dist_param_cnt, global);
2069 stat_finish_pattern_history(fname);
2071 /* clear the global counter here */
2073 node_entry_t *entry;
2075 for (entry = pset_first(global->opcode_hash); entry; entry = pset_next(global->opcode_hash)) {
2076 opcode_clear_entry(entry);
2078 /* clear all global counter */
2079 graph_clear_entry(global, 1);
2083 } /* stat_dump_snapshot */
2085 /** the hook entries for the Firm statistics module */
2086 static hook_entry_t stat_hooks[hook_last];
2088 /* initialize the statistics module. */
2089 void firm_init_stat(unsigned enable_options)
2091 #define X(a) a, sizeof(a)-1
2092 #define HOOK(h, fkt) \
2093 stat_hooks[h].hook._##h = fkt; register_hook(h, &stat_hooks[h])
2096 if (! (enable_options & FIRMSTAT_ENABLED))
2099 status = xmalloc(sizeof(*status));
2100 memset(status, 0, sizeof(*status));
2102 /* enable statistics */
2103 status->stat_options = enable_options & FIRMSTAT_ENABLED ? enable_options : 0;
2105 /* register all hooks */
2106 HOOK(hook_new_ir_op, stat_new_ir_op);
2107 HOOK(hook_free_ir_op, stat_free_ir_op);
2108 HOOK(hook_new_node, stat_new_node);
2109 HOOK(hook_turn_into_id, stat_turn_into_id);
2110 HOOK(hook_new_graph, stat_new_graph);
2111 HOOK(hook_free_graph, stat_free_graph);
2112 HOOK(hook_irg_walk, stat_irg_walk);
2113 HOOK(hook_irg_walk_blkwise, stat_irg_walk_blkwise);
2114 HOOK(hook_irg_block_walk, stat_irg_block_walk);
2115 HOOK(hook_merge_nodes, stat_merge_nodes);
2116 HOOK(hook_reassociate, stat_reassociate);
2117 HOOK(hook_lower, stat_lower);
2118 HOOK(hook_inline, stat_inline);
2119 HOOK(hook_tail_rec, stat_tail_rec);
2120 HOOK(hook_strength_red, stat_strength_red);
2121 HOOK(hook_dead_node_elim, stat_dead_node_elim);
2122 HOOK(hook_if_conversion, stat_if_conversion);
2123 HOOK(hook_func_call, stat_func_call);
2124 HOOK(hook_arch_dep_replace_mul_with_shifts, stat_arch_dep_replace_mul_with_shifts);
2125 HOOK(hook_arch_dep_replace_division_by_const, stat_arch_dep_replace_division_by_const);
2127 obstack_init(&status->cnts);
2128 obstack_init(&status->be_data);
2130 /* create the hash-tables */
2131 status->irg_hash = new_pset(graph_cmp, 8);
2132 status->ir_op_hash = new_pset(opcode_cmp_2, 1);
2134 /* create the wait queue */
2135 status->wait_q = new_pdeq();
2137 if (enable_options & FIRMSTAT_COUNT_STRONG_OP) {
2138 /* build the pseudo-ops */
2140 _op_Phi0.code = --num;
2141 _op_Phi0.name = new_id_from_chars(X("Phi0"));
2143 _op_PhiM.code = --num;
2144 _op_PhiM.name = new_id_from_chars(X("PhiM"));
2146 _op_ProjM.code = --num;
2147 _op_ProjM.name = new_id_from_chars(X("ProjM"));
2149 _op_MulC.code = --num;
2150 _op_MulC.name = new_id_from_chars(X("MulC"));
2152 _op_DivC.code = --num;
2153 _op_DivC.name = new_id_from_chars(X("DivC"));
2155 _op_ModC.code = --num;
2156 _op_ModC.name = new_id_from_chars(X("ModC"));
2158 _op_DivModC.code = --num;
2159 _op_DivModC.name = new_id_from_chars(X("DivModC"));
2161 status->op_Phi0 = &_op_Phi0;
2162 status->op_PhiM = &_op_PhiM;
2163 status->op_ProjM = &_op_ProjM;
2164 status->op_MulC = &_op_MulC;
2165 status->op_DivC = &_op_DivC;
2166 status->op_ModC = &_op_ModC;
2167 status->op_DivModC = &_op_DivModC;
2169 status->op_Phi0 = NULL;
2170 status->op_PhiM = NULL;
2171 status->op_ProjM = NULL;
2172 status->op_MulC = NULL;
2173 status->op_DivC = NULL;
2174 status->op_ModC = NULL;
2175 status->op_DivModC = NULL;
2178 /* for Florian: count the Sel depth */
2179 if (enable_options & FIRMSTAT_COUNT_SELS) {
2180 _op_SelSel.code = --num;
2181 _op_SelSel.name = new_id_from_chars(X("Sel(Sel)"));
2183 _op_SelSelSel.code = --num;
2184 _op_SelSelSel.name = new_id_from_chars(X("Sel(Sel(Sel))"));
2186 status->op_SelSel = &_op_SelSel;
2187 status->op_SelSelSel = &_op_SelSelSel;
2189 status->op_SelSel = NULL;
2190 status->op_SelSelSel = NULL;
2193 /* register the dumper */
2194 stat_register_dumper(&simple_dumper);
2196 if (enable_options & FIRMSTAT_CSV_OUTPUT)
2197 stat_register_dumper(&csv_dumper);
2199 /* initialize the pattern hash */
2200 stat_init_pattern_history(enable_options & FIRMSTAT_PATTERN_ENABLED);
2202 /* initialize the Const options */
2203 if (enable_options & FIRMSTAT_COUNT_CONSTS)
2204 stat_init_const_cnt(status);
2206 /* distribution table for parameter counts */
2207 status->dist_param_cnt = stat_new_int_distrib_tbl();
2211 } /* firm_init_stat */
2214 * Frees all dumper structures.
2216 static void stat_term_dumper(void) {
2217 dumper_t *dumper, *next_dumper;
2219 for (dumper = status->dumper; dumper; /* iteration done in loop body */ ) {
2220 if (dumper->func_map)
2221 del_pset(dumper->func_map);
2223 next_dumper = dumper->next;
2225 dumper = next_dumper;
2227 } /* stat_term_dumper */
2230 /* Terminates the statistics module, frees all memory. */
2231 void stat_term(void) {
2232 if (status != (stat_info_t *)&status_disable) {
2233 obstack_free(&status->be_data, NULL);
2234 obstack_free(&status->cnts, NULL);
2239 status = (stat_info_t *)&status_disable;
2243 /* returns 1 if statistics were initialized, 0 otherwise */
2244 int stat_is_active(void) {
2245 return status != (stat_info_t *)&status_disable;
2246 } /* stat_is_active */
2250 /* initialize the statistics module. */
2251 void firm_init_stat(unsigned enable_options) {}
2253 /* Dumps a statistics snapshot */
2254 void stat_dump_snapshot(const char *name, const char *phase) {}
2256 /* terminates the statistics module, frees all memory */
2257 void stat_term(void);
2259 #endif /* FIRM_STATISTICS */