2 * Copyright (C) 1995-2010 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Statistics for Firm.
23 * @author Michael Beck
34 #include "firmstat_t.h"
45 * need this to be static:
46 * Special pseudo Opcodes that we need to count some interesting cases
50 * The Phi0, a node that is created during SSA construction
52 static ir_op _op_Phi0;
54 /** The PhiM, just to count memory Phi's. */
55 static ir_op _op_PhiM;
57 /** The Mul by Const node. */
58 static ir_op _op_MulC;
60 /** The Div by Const node. */
61 static ir_op _op_DivC;
63 /** The Div by Const node. */
64 static ir_op _op_ModC;
66 /** The memory Proj node. */
67 static ir_op _op_ProjM;
70 static ir_op _op_SelSel;
72 /** A Sel of a Sel of a Sel */
73 static ir_op _op_SelSelSel;
75 /* ---------------------------------------------------------------------------------- */
77 /** Marks the begin of a statistic (hook) function. */
78 #define STAT_ENTER ++status->recursive
80 /** Marks the end of a statistic (hook) functions. */
81 #define STAT_LEAVE --status->recursive
83 /** Allows to enter a statistic function only when we are not already in a hook. */
84 #define STAT_ENTER_SINGLE do { if (status->recursive > 0) return; ++status->recursive; } while (0)
89 static const unsigned status_disable = 0;
90 static stat_info_t *status = (stat_info_t *)&status_disable;
93 * Compare two elements of the opcode hash.
95 static int opcode_cmp(const void *elt, const void *key)
97 const node_entry_t *e1 = (const node_entry_t*)elt;
98 const node_entry_t *e2 = (const node_entry_t*)key;
100 return e1->op->code - e2->op->code;
104 * Compare two elements of the graph hash.
106 static int graph_cmp(const void *elt, const void *key)
108 const graph_entry_t *e1 = (const graph_entry_t*)elt;
109 const graph_entry_t *e2 = (const graph_entry_t*)key;
111 return e1->irg != e2->irg;
115 * Compare two elements of the optimization hash.
117 static int opt_cmp(const void *elt, const void *key)
119 const opt_entry_t *e1 = (const opt_entry_t*)elt;
120 const opt_entry_t *e2 = (const opt_entry_t*)key;
122 return e1->op->code != e2->op->code;
126 * Compare two elements of the block hash.
128 static int block_cmp(const void *elt, const void *key)
130 const block_entry_t *e1 = (const block_entry_t*)elt;
131 const block_entry_t *e2 = (const block_entry_t*)key;
133 /* it's enough to compare the block number */
134 return e1->block_nr != e2->block_nr;
138 * Compare two elements of the be_block hash.
140 static int be_block_cmp(const void *elt, const void *key)
142 const be_block_entry_t *e1 = (const be_block_entry_t*)elt;
143 const be_block_entry_t *e2 = (const be_block_entry_t*)key;
145 return e1->block_nr != e2->block_nr;
149 * Compare two elements of reg pressure hash.
151 static int reg_pressure_cmp(const void *elt, const void *key)
153 const reg_pressure_entry_t *e1 = (const reg_pressure_entry_t*)elt;
154 const reg_pressure_entry_t *e2 = (const reg_pressure_entry_t*)key;
156 return e1->class_name != e2->class_name;
157 } /* reg_pressure_cmp */
160 * Compare two elements of the perm_stat hash.
162 static int perm_stat_cmp(const void *elt, const void *key)
164 const perm_stat_entry_t *e1 = (const perm_stat_entry_t*)elt;
165 const perm_stat_entry_t *e2 = (const perm_stat_entry_t*)key;
167 return e1->perm != e2->perm;
168 } /* perm_stat_cmp */
171 * Compare two elements of the perm_class hash.
173 static int perm_class_cmp(const void *elt, const void *key)
175 const perm_class_entry_t *e1 = (const perm_class_entry_t*)elt;
176 const perm_class_entry_t *e2 = (const perm_class_entry_t*)key;
178 return e1->class_name != e2->class_name;
179 } /* perm_class_cmp */
182 * Compare two elements of the ir_op hash.
184 static int opcode_cmp_2(const void *elt, const void *key)
186 const ir_op *e1 = (const ir_op*)elt;
187 const ir_op *e2 = (const ir_op*)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)
197 const address_mark_entry_t *e1 = (const address_mark_entry_t*)elt;
198 const address_mark_entry_t *e2 = (const address_mark_entry_t*)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)
210 cnt_clr(&elem->cnt_alive);
211 cnt_clr(&elem->new_node);
212 cnt_clr(&elem->into_Id);
213 cnt_clr(&elem->normalized);
214 } /* opcode_clear_entry */
217 * Returns the associates node_entry_t for an ir_op (and allocates
218 * one if not yet available).
220 * @param op the IR operation
221 * @param hmap a hash map containing ir_op* -> node_entry_t*
223 static node_entry_t *opcode_get_entry(const ir_op *op, hmap_node_entry_t *hmap)
230 elem = (node_entry_t*)pset_find(hmap, &key, op->code);
234 elem = OALLOCZ(&status->cnts, node_entry_t);
237 opcode_clear_entry(elem);
241 return (node_entry_t*)pset_insert(hmap, elem, op->code);
242 } /* opcode_get_entry */
245 * Returns the associates ir_op for an opcode
247 * @param code the IR opcode
248 * @param hmap the hash map containing opcode -> ir_op*
250 static ir_op *opcode_find_entry(ir_opcode code, hmap_ir_op *hmap)
255 return (ir_op*)pset_find(hmap, &key, code);
256 } /* opcode_find_entry */
259 * Clears all counter in a graph_entry_t.
261 * @param elem the graph entry
262 * @param all if non-zero, clears all counters, else leave accumulated ones
264 static void graph_clear_entry(graph_entry_t *elem, int all)
268 /* clear accumulated / non-accumulated counter */
269 for (i = all ? 0 : _gcnt_non_acc; i < _gcnt_last; ++i) {
270 cnt_clr(&elem->cnt[i]);
273 if (elem->block_hash) {
274 del_pset(elem->block_hash);
275 elem->block_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, NULL for the global counter
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 = (graph_entry_t*)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 = OALLOCZ(&status->cnts, graph_entry_t);
308 obstack_init(&elem->recalc_cnts);
311 graph_clear_entry(elem, 1);
313 /* new hash table for opcodes here */
314 elem->opcode_hash = new_pset(opcode_cmp, 5);
315 elem->address_mark = new_set(address_mark_cmp, 5);
318 /* these hash tables are created on demand */
319 elem->block_hash = NULL;
321 for (i = 0; i != ARRAY_SIZE(elem->opt_hash); ++i)
322 elem->opt_hash[i] = new_pset(opt_cmp, 4);
324 return (graph_entry_t*)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)
332 cnt_clr(&elem->count);
333 } /* opt_clear_entry */
336 * Returns the associated opt_entry_t for an IR operation.
338 * @param op the IR operation
339 * @param hmap the hash map containing ir_op* -> opt_entry_t*
341 static opt_entry_t *opt_get_entry(const ir_op *op, hmap_opt_entry_t *hmap)
348 elem = (opt_entry_t*)pset_find(hmap, &key, op->code);
352 elem = OALLOCZ(&status->cnts, opt_entry_t);
354 /* clear new counter */
355 opt_clear_entry(elem);
359 return (opt_entry_t*)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)
369 for (i = 0; i < _bcnt_last; ++i)
370 cnt_clr(&elem->cnt[i]);
371 } /* block_clear_entry */
374 * Returns the associated block_entry_t for an block.
376 * @param block_nr an IR block number
377 * @param hmap a hash map containing long -> block_entry_t
379 static block_entry_t *block_get_entry(struct obstack *obst, long block_nr, hmap_block_entry_t *hmap)
384 key.block_nr = block_nr;
386 elem = (block_entry_t*)pset_find(hmap, &key, block_nr);
390 elem = OALLOCZ(obst, block_entry_t);
392 /* clear new counter */
393 block_clear_entry(elem);
395 elem->block_nr = block_nr;
397 return (block_entry_t*)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 = (be_block_entry_t*)pset_find(hmap, &key, block_nr);
436 elem = OALLOCZ(obst, be_block_entry_t);
438 /* clear new counter */
439 be_block_clear_entry(elem);
441 elem->block_nr = block_nr;
443 return (be_block_entry_t*)pset_insert(hmap, elem, block_nr);
444 } /* be_block_get_entry */
447 * clears all sets in perm_class_entry_t
449 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 = (perm_class_entry_t*)pset_find(hmap, &key, hash_ptr(class_name));
475 elem = OALLOCZ(obst, perm_class_entry_t);
477 /* clear new counter */
478 perm_class_clear_entry(elem);
480 elem->class_name = class_name;
482 return (perm_class_entry_t*)pset_insert(hmap, elem, hash_ptr(class_name));
483 } /* perm_class_get_entry */
486 * clears all sets in perm_stat_entry_t
488 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 = (perm_stat_entry_t*)pset_find(hmap, &key, hash_ptr(perm));
517 elem = OALLOCZ(obst, perm_stat_entry_t);
519 /* clear new counter */
520 perm_stat_clear_entry(elem);
524 return (perm_stat_entry_t*)pset_insert(hmap, elem, hash_ptr(perm));
525 } /* perm_stat_get_entry */
528 * Clear optimizations counter,
530 static void clear_optimization_counter(void)
533 for (i = 0; i < FS_OPT_MAX; ++i)
534 cnt_clr(&status->num_opts[i]);
538 * Returns the ir_op for an IR-node,
539 * handles special cases and return pseudo op codes.
541 * @param none an IR node
543 static ir_op *stat_get_irn_op(ir_node *node)
545 ir_op *op = get_irn_op(node);
546 unsigned opc = op->code;
550 if (get_irn_arity(node) == 0) {
551 /* special case, a Phi0 node, count on extra counter */
552 op = status->op_Phi0 ? status->op_Phi0 : op;
553 } else if (get_irn_mode(node) == mode_M) {
554 /* special case, a Memory Phi node, count on extra counter */
555 op = status->op_PhiM ? status->op_PhiM : op;
559 if (get_irn_mode(node) == mode_M) {
560 /* special case, a Memory Proj node, count on extra counter */
561 op = status->op_ProjM ? status->op_ProjM : op;
565 if (is_Const(get_Mul_left(node)) || is_Const(get_Mul_right(node))) {
566 /* special case, a Multiply by a const, count on extra counter */
567 op = status->op_MulC ? status->op_MulC : op;
571 if (is_Const(get_Div_right(node))) {
572 /* special case, a division by a const, count on extra counter */
573 op = status->op_DivC ? status->op_DivC : op;
577 if (is_Const(get_Mod_right(node))) {
578 /* special case, a module by a const, count on extra counter */
579 op = status->op_ModC ? status->op_ModC : op;
583 if (is_Sel(get_Sel_ptr(node))) {
584 /* special case, a Sel of a Sel, count on extra counter */
585 op = status->op_SelSel ? status->op_SelSel : op;
586 if (is_Sel(get_Sel_ptr(get_Sel_ptr(node)))) {
587 /* special case, a Sel of a Sel of a Sel, count on extra counter */
588 op = status->op_SelSelSel ? status->op_SelSelSel : op;
597 } /* stat_get_irn_op */
600 * update the block counter
602 static void undate_block_info(ir_node *node, graph_entry_t *graph)
604 ir_op *op = get_irn_op(node);
606 block_entry_t *b_entry;
609 /* check for block */
610 if (op == op_Block) {
611 arity = get_irn_arity(node);
612 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(node), graph->block_hash);
613 /* mark start end block to allow to filter them out */
614 if (node == get_irg_start_block(graph->irg))
615 b_entry->is_start = 1;
616 else if (node == get_irg_end_block(graph->irg))
619 /* count all incoming edges */
620 for (i = 0; i < arity; ++i) {
621 ir_node *pred = get_irn_n(node, i);
622 ir_node *other_block = get_nodes_block(pred);
623 block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
625 cnt_inc(&b_entry->cnt[bcnt_in_edges]); /* an edge coming from another block */
626 cnt_inc(&b_entry_other->cnt[bcnt_out_edges]);
631 block = get_nodes_block(node);
632 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(block), graph->block_hash);
634 if (op == op_Phi && mode_is_datab(get_irn_mode(node))) {
635 /* count data Phi per block */
636 cnt_inc(&b_entry->cnt[bcnt_phi_data]);
639 /* we have a new node in our block */
640 cnt_inc(&b_entry->cnt[bcnt_nodes]);
642 /* don't count keep-alive edges */
646 arity = get_irn_arity(node);
648 for (i = 0; i < arity; ++i) {
649 ir_node *pred = get_irn_n(node, i);
650 ir_node *other_block;
652 other_block = get_nodes_block(pred);
654 if (other_block == block)
655 cnt_inc(&b_entry->cnt[bcnt_edges]); /* a in block edge */
657 block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
659 cnt_inc(&b_entry->cnt[bcnt_in_edges]); /* an edge coming from another block */
660 cnt_inc(&b_entry_other->cnt[bcnt_out_edges]);
663 } /* undate_block_info */
666 * Calculates how many arguments of the call are const, updates
667 * param distribution.
669 static void analyse_params_of_Call(graph_entry_t *graph, ir_node *call)
671 int i, num_const_args = 0, num_local_adr = 0;
672 int n = get_Call_n_params(call);
674 for (i = 0; i < n; ++i) {
675 ir_node *param = get_Call_param(call, i);
677 if (is_irn_constlike(param))
679 else if (is_Sel(param)) {
680 ir_node *base = param;
683 base = get_Sel_ptr(base);
684 } while (is_Sel(base));
686 if (base == get_irg_frame(current_ir_graph))
692 if (num_const_args > 0)
693 cnt_inc(&graph->cnt[gcnt_call_with_cnst_arg]);
694 if (num_const_args == n)
695 cnt_inc(&graph->cnt[gcnt_call_with_all_cnst_arg]);
696 if (num_local_adr > 0)
697 cnt_inc(&graph->cnt[gcnt_call_with_local_adr]);
699 stat_inc_int_distrib_tbl(status->dist_param_cnt, n);
700 } /* analyse_params_of_Call */
703 * Update info on calls.
705 * @param call The call
706 * @param graph The graph entry containing the call
708 static void stat_update_call(ir_node *call, graph_entry_t *graph)
710 ir_node *block = get_nodes_block(call);
711 ir_node *ptr = get_Call_ptr(call);
712 ir_entity *ent = NULL;
713 ir_graph *callee = NULL;
716 * If the block is bad, the whole subgraph will collapse later
717 * so do not count this call.
718 * This happens in dead code.
723 cnt_inc(&graph->cnt[gcnt_all_calls]);
725 /* found a call, this function is not a leaf */
728 if (is_SymConst(ptr)) {
729 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
730 /* ok, we seems to know the entity */
731 ent = get_SymConst_entity(ptr);
732 callee = get_entity_irg(ent);
734 /* it is recursive, if it calls at least once */
735 if (callee == graph->irg)
736 graph->is_recursive = 1;
738 cnt_inc(&graph->cnt[gcnt_external_calls]);
741 /* indirect call, be could not predict */
742 cnt_inc(&graph->cnt[gcnt_indirect_calls]);
744 /* NOT a leaf call */
745 graph->is_leaf_call = LCS_NON_LEAF_CALL;
748 /* check, if it's a chain-call: Then, the call-block
749 * must dominate the end block. */
751 ir_node *curr = get_irg_end_block(graph->irg);
752 int depth = get_Block_dom_depth(block);
754 for (; curr != block && get_Block_dom_depth(curr) > depth;) {
755 curr = get_Block_idom(curr);
757 if (! curr || !is_Block(curr))
762 graph->is_chain_call = 0;
765 /* check, if the callee is a leaf */
767 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
769 if (called->is_analyzed) {
770 if (! called->is_leaf)
771 graph->is_leaf_call = LCS_NON_LEAF_CALL;
775 analyse_params_of_Call(graph, call);
776 } /* stat_update_call */
779 * Update info on calls for graphs on the wait queue.
781 static void stat_update_call_2(ir_node *call, graph_entry_t *graph)
783 ir_node *block = get_nodes_block(call);
784 ir_node *ptr = get_Call_ptr(call);
785 ir_entity *ent = NULL;
786 ir_graph *callee = NULL;
789 * If the block is bad, the whole subgraph will collapse later
790 * so do not count this call.
791 * This happens in dead code.
796 if (is_SymConst(ptr)) {
797 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
798 /* ok, we seems to know the entity */
799 ent = get_SymConst_entity(ptr);
800 callee = get_entity_irg(ent);
804 /* check, if the callee is a leaf */
806 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
808 assert(called->is_analyzed);
810 if (! called->is_leaf)
811 graph->is_leaf_call = LCS_NON_LEAF_CALL;
813 graph->is_leaf_call = LCS_NON_LEAF_CALL;
814 } /* stat_update_call_2 */
817 * Find the base address and entity of an Sel node.
819 * @param sel the node
821 * @return the base address.
823 static ir_node *find_base_adr(ir_node *sel)
825 ir_node *ptr = get_Sel_ptr(sel);
827 while (is_Sel(ptr)) {
829 ptr = get_Sel_ptr(sel);
832 } /* find_base_adr */
835 * Update info on Load/Store address statistics.
837 static void stat_update_address(ir_node *node, graph_entry_t *graph)
839 unsigned opc = get_irn_opcode(node);
845 /* a global address */
846 cnt_inc(&graph->cnt[gcnt_global_adr]);
849 base = find_base_adr(node);
850 irg = current_ir_graph;
851 if (base == get_irg_frame(irg)) {
852 /* a local Variable. */
853 cnt_inc(&graph->cnt[gcnt_local_adr]);
856 if (is_Proj(base) && skip_Proj(get_Proj_pred(base)) == get_irg_start(irg)) {
857 /* pointer access through parameter, check for THIS */
858 ir_entity *ent = get_irg_entity(irg);
861 ir_type *ent_tp = get_entity_type(ent);
863 if (get_method_calling_convention(ent_tp) & cc_this_call) {
864 if (get_Proj_proj(base) == 0) {
866 cnt_inc(&graph->cnt[gcnt_this_adr]);
871 /* other parameter */
872 cnt_inc(&graph->cnt[gcnt_param_adr]);
875 /* unknown Pointer access */
876 cnt_inc(&graph->cnt[gcnt_other_adr]);
882 } /* stat_update_address */
885 * Walker for reachable nodes count.
887 static void update_node_stat(ir_node *node, void *env)
889 graph_entry_t *graph = (graph_entry_t*)env;
892 ir_op *op = stat_get_irn_op(node);
893 int i, arity = get_irn_arity(node);
895 entry = opcode_get_entry(op, graph->opcode_hash);
897 cnt_inc(&entry->cnt_alive);
898 cnt_add_i(&graph->cnt[gcnt_edges], arity);
900 /* count block edges */
901 undate_block_info(node, graph);
903 /* handle statistics for special node types */
907 /* check for properties that depends on calls like recursion/leaf/indirect call */
908 stat_update_call(node, graph);
911 /* check address properties */
912 stat_update_address(get_Load_ptr(node), graph);
915 /* check address properties */
916 stat_update_address(get_Store_ptr(node), graph);
919 /* check for non-strict Phi nodes */
920 for (i = arity - 1; i >= 0; --i) {
921 ir_node *pred = get_Phi_pred(node, i);
922 if (is_Unknown(pred)) {
923 /* found an Unknown predecessor, graph is not strict */
924 graph->is_strict = 0;
932 /* we want to count the constant IN nodes, not the CSE'ed constant's itself */
933 if (status->stat_options & FIRMSTAT_COUNT_CONSTS) {
936 for (i = get_irn_arity(node) - 1; i >= 0; --i) {
937 ir_node *pred = get_irn_n(node, i);
939 if (is_Const(pred)) {
940 /* check properties of constants */
941 stat_update_const(status, pred, graph);
945 } /* update_node_stat */
948 * Walker for reachable nodes count for graphs on the wait_q.
950 static void update_node_stat_2(ir_node *node, void *env)
952 graph_entry_t *graph = (graph_entry_t*)env;
954 /* check for properties that depends on calls like recursion/leaf/indirect call */
956 stat_update_call_2(node, graph);
957 } /* update_node_stat_2 */
960 * Get the current address mark.
962 static unsigned get_adr_mark(graph_entry_t *graph, ir_node *node)
964 address_mark_entry_t *value = (address_mark_entry_t*)set_find(graph->address_mark, &node, sizeof(*value), hash_ptr(node));
966 return value ? value->mark : 0;
970 * Set the current address mark.
972 static void set_adr_mark(graph_entry_t *graph, ir_node *node, unsigned val)
974 address_mark_entry_t *value = (address_mark_entry_t*)set_insert(graph->address_mark, &node, sizeof(*value), hash_ptr(node));
983 * a vcg attribute hook: Color a node with a different color if
984 * it's identified as a part of an address expression or at least referenced
985 * by an address expression.
987 static int stat_adr_mark_hook(FILE *F, ir_node *node, ir_node *local)
989 ir_node *n = local ? local : node;
990 ir_graph *irg = get_irn_irg(n);
991 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
992 unsigned mark = get_adr_mark(graph, n);
994 if (mark & MARK_ADDRESS_CALC)
995 fprintf(F, "color: purple");
996 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
997 fprintf(F, "color: pink");
998 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
999 fprintf(F, "color: lightblue");
1003 /* I know the color! */
1005 } /* stat_adr_mark_hook */
1006 #endif /* DUMP_ADR_MODE */
1009 * Return the "operational" mode of a Firm node.
1011 static ir_mode *get_irn_op_mode(ir_node *node)
1013 switch (get_irn_opcode(node)) {
1015 return get_Load_mode(node);
1017 return get_irn_mode(get_Store_value(node));
1019 return get_irn_mode(get_Div_left(node));
1021 return get_irn_mode(get_Mod_left(node));
1023 /* Cmp is no address calculation, or is it? */
1025 return get_irn_mode(node);
1027 } /* get_irn_op_mode */
1030 * Post-walker that marks every node that is an address calculation.
1032 * Users of a node must be visited first. We ensure this by
1033 * calling it in the post of an outs walk. This should work even in cycles,
1034 * while the normal pre-walk will not.
1036 static void mark_address_calc(ir_node *node, void *env)
1038 graph_entry_t *graph = (graph_entry_t*)env;
1039 ir_mode *mode = get_irn_op_mode(node);
1041 unsigned mark_preds = MARK_REF_NON_ADR;
1043 if (! mode_is_data(mode))
1046 if (mode_is_reference(mode)) {
1047 /* a reference is calculated here, we are sure */
1048 set_adr_mark(graph, node, MARK_ADDRESS_CALC);
1050 mark_preds = MARK_REF_ADR;
1052 unsigned mark = get_adr_mark(graph, node);
1054 if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR) {
1056 * this node has no reference mode, but is only
1057 * referenced by address calculations
1059 mark_preds = MARK_REF_ADR;
1063 /* mark all predecessors */
1064 for (i = 0, n = get_irn_arity(node); i < n; ++i) {
1065 ir_node *pred = get_irn_n(node, i);
1067 mode = get_irn_op_mode(pred);
1068 if (! mode_is_data(mode))
1071 set_adr_mark(graph, pred, get_adr_mark(graph, pred) | mark_preds);
1073 } /* mark_address_calc */
1076 * Post-walker that marks every node that is an address calculation.
1078 * Users of a node must be visited first. We ensure this by
1079 * calling it in the post of an outs walk. This should work even in cycles,
1080 * while the normal pre-walk will not.
1082 static void count_adr_ops(ir_node *node, void *env)
1084 graph_entry_t *graph = (graph_entry_t*)env;
1085 unsigned mark = get_adr_mark(graph, node);
1087 if (mark & MARK_ADDRESS_CALC)
1088 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1089 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1090 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1091 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1092 cnt_inc(&graph->cnt[gcnt_all_adr_ops]);
1093 } /* count_adr_ops */
1096 * Called for every graph when the graph is either deleted or stat_dump_snapshot()
1097 * is called, must recalculate all statistic info.
1099 * @param global The global entry
1100 * @param graph The current entry
1102 static void update_graph_stat(graph_entry_t *global, graph_entry_t *graph)
1104 node_entry_t *entry;
1107 /* clear first the alive counter in the graph */
1108 foreach_pset(graph->opcode_hash, node_entry_t*, entry) {
1109 cnt_clr(&entry->cnt_alive);
1110 } /* foreach_pset */
1112 /* set pessimistic values */
1114 graph->is_leaf_call = LCS_UNKNOWN;
1115 graph->is_recursive = 0;
1116 graph->is_chain_call = 1;
1117 graph->is_strict = 1;
1119 /* create new block counter */
1120 graph->block_hash = new_pset(block_cmp, 5);
1122 /* we need dominator info */
1123 if (graph->irg != get_const_code_irg()) {
1124 assure_doms(graph->irg);
1127 /* count the nodes in the graph */
1128 irg_walk_graph(graph->irg, update_node_stat, NULL, graph);
1131 /* Uncomment this code if chain-call means call exact one. */
1132 entry = opcode_get_entry(op_Call, graph->opcode_hash);
1134 /* check if we have more than 1 call */
1135 if (cnt_gt(entry->cnt_alive, 1))
1136 graph->is_chain_call = 0;
1139 /* recursive functions are never chain calls, leafs don't have calls */
1140 if (graph->is_recursive || graph->is_leaf)
1141 graph->is_chain_call = 0;
1143 /* assume we walk every graph only ONCE, we could sum here the global count */
1144 foreach_pset(graph->opcode_hash, node_entry_t*, entry) {
1145 node_entry_t *g_entry = opcode_get_entry(entry->op, global->opcode_hash);
1147 /* update the node counter */
1148 cnt_add(&g_entry->cnt_alive, &entry->cnt_alive);
1149 } /* foreach_pset */
1151 /* count the number of address calculation */
1152 if (graph->irg != get_const_code_irg()) {
1153 ir_graph *rem = current_ir_graph;
1155 assure_irg_outs(graph->irg);
1157 /* Must be done an the outs graph */
1158 current_ir_graph = graph->irg;
1159 irg_out_walk(get_irg_start(graph->irg), NULL, mark_address_calc, graph);
1160 current_ir_graph = rem;
1162 #ifdef DUMP_ADR_MODE
1163 /* register the vcg hook and dump the graph for test */
1164 set_dump_node_vcgattr_hook(stat_adr_mark_hook);
1165 dump_ir_block_graph(graph->irg, "-adr");
1166 set_dump_node_vcgattr_hook(NULL);
1167 #endif /* DUMP_ADR_MODE */
1169 irg_walk_graph(graph->irg, NULL, count_adr_ops, graph);
1172 /* count the DAG's */
1173 if (status->stat_options & FIRMSTAT_COUNT_DAG)
1174 count_dags_in_graph(global, graph);
1176 /* calculate the patterns of this graph */
1177 stat_calc_pattern_history(graph->irg);
1179 /* leaf function did not call others */
1181 graph->is_leaf_call = LCS_NON_LEAF_CALL;
1182 else if (graph->is_leaf_call == LCS_UNKNOWN) {
1183 /* we still don't know if this graph calls leaf-functions, so enqueue */
1184 pdeq_putl(status->wait_q, graph);
1187 /* we have analyzed this graph */
1188 graph->is_analyzed = 1;
1190 /* accumulate all counter's */
1191 for (i = 0; i < _gcnt_last; ++i)
1192 cnt_add(&global->cnt[i], &graph->cnt[i]);
1193 } /* update_graph_stat */
1196 * Called for every graph that was on the wait_q in stat_dump_snapshot()
1197 * must finish all statistic info calculations.
1199 * @param global The global entry
1200 * @param graph The current entry
1202 static void update_graph_stat_2(graph_entry_t *global, graph_entry_t *graph)
1205 if (graph->is_deleted) {
1206 /* deleted, ignore */
1211 /* count the nodes in the graph */
1212 irg_walk_graph(graph->irg, update_node_stat_2, NULL, graph);
1214 if (graph->is_leaf_call == LCS_UNKNOWN)
1215 graph->is_leaf_call = LCS_LEAF_CALL;
1217 } /* update_graph_stat_2 */
1220 * Register a dumper.
1222 static void stat_register_dumper(const dumper_t *dumper)
1224 dumper_t *p = XMALLOC(dumper_t);
1227 p->next = status->dumper;
1231 /* FIXME: memory leak */
1232 } /* stat_register_dumper */
1235 * Dumps the statistics of an IR graph.
1237 static void stat_dump_graph(graph_entry_t *entry)
1241 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1242 if (dumper->dump_graph)
1243 dumper->dump_graph(dumper, entry);
1245 } /* stat_dump_graph */
1248 * Calls all registered dumper functions.
1250 static void stat_dump_registered(graph_entry_t *entry)
1254 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1255 if (dumper->func_map) {
1256 dump_graph_FUNC func;
1258 foreach_pset(dumper->func_map, dump_graph_FUNC, func)
1259 func(dumper, entry);
1262 } /* stat_dump_registered */
1265 * Dumps a constant table.
1267 static void stat_dump_consts(const constant_info_t *tbl)
1271 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1272 if (dumper->dump_const_tbl)
1273 dumper->dump_const_tbl(dumper, tbl);
1275 } /* stat_dump_consts */
1278 * Dumps the parameter distribution
1280 static void stat_dump_param_tbl(const distrib_tbl_t *tbl, graph_entry_t *global)
1284 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1285 if (dumper->dump_param_tbl)
1286 dumper->dump_param_tbl(dumper, tbl, global);
1288 } /* stat_dump_param_tbl */
1291 * Dumps the optimization counter
1293 static void stat_dump_opt_cnt(const counter_t *tbl, unsigned len)
1297 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1298 if (dumper->dump_opt_cnt)
1299 dumper->dump_opt_cnt(dumper, tbl, len);
1301 } /* stat_dump_opt_cnt */
1304 * Initialize the dumper.
1306 static void stat_dump_init(const char *name)
1310 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1312 dumper->init(dumper, name);
1314 } /* stat_dump_init */
1317 * Finish the dumper.
1319 static void stat_dump_finish(void)
1323 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1325 dumper->finish(dumper);
1327 } /* stat_dump_finish */
1330 * Register an additional function for all dumper.
1332 void stat_register_dumper_func(dump_graph_FUNC func)
1336 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1337 if (! dumper->func_map)
1338 dumper->func_map = pset_new_ptr(3);
1339 pset_insert_ptr(dumper->func_map, (void*)func);
1341 } /* stat_register_dumper_func */
1343 /* ---------------------------------------------------------------------- */
1346 * Helper: get an ir_op from an opcode.
1348 ir_op *stat_get_op_from_opcode(unsigned code)
1350 return opcode_find_entry((ir_opcode)code, status->ir_op_hash);
1351 } /* stat_get_op_from_opcode */
1354 * Hook: A new IR op is registered.
1356 * @param ctx the hook context
1357 * @param op the new IR opcode that was created.
1359 static void stat_new_ir_op(void *ctx, ir_op *op)
1362 if (! status->stat_options)
1367 graph_entry_t *graph = graph_get_entry(NULL, status->irg_hash);
1369 /* execute for side effect :-) */
1370 (void)opcode_get_entry(op, graph->opcode_hash);
1372 pset_insert(status->ir_op_hash, op, op->code);
1375 } /* stat_new_ir_op */
1378 * Hook: An IR op is freed.
1380 * @param ctx the hook context
1381 * @param op the IR opcode that is freed
1383 static void stat_free_ir_op(void *ctx, ir_op *op)
1387 if (! status->stat_options)
1394 } /* stat_free_ir_op */
1397 * Hook: A new node is created.
1399 * @param ctx the hook context
1400 * @param irg the IR graph on which the node is created
1401 * @param node the new IR node that was created
1403 static void stat_new_node(void *ctx, ir_graph *irg, ir_node *node)
1407 if (! status->stat_options)
1410 /* do NOT count during dead node elimination */
1411 if (status->in_dead_node_elim)
1416 node_entry_t *entry;
1417 graph_entry_t *graph;
1418 ir_op *op = stat_get_irn_op(node);
1420 /* increase global value */
1421 graph = graph_get_entry(NULL, status->irg_hash);
1422 entry = opcode_get_entry(op, graph->opcode_hash);
1423 cnt_inc(&entry->new_node);
1425 /* increase local value */
1426 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1427 entry = opcode_get_entry(op, graph->opcode_hash);
1428 cnt_inc(&entry->new_node);
1431 } /* stat_new_node */
1434 * Hook: A node is changed into a Id node
1436 * @param ctx the hook context
1437 * @param node the IR node that will be turned into an ID
1439 static void stat_turn_into_id(void *ctx, ir_node *node)
1442 if (! status->stat_options)
1447 node_entry_t *entry;
1448 graph_entry_t *graph;
1449 ir_op *op = stat_get_irn_op(node);
1451 /* increase global value */
1452 graph = graph_get_entry(NULL, status->irg_hash);
1453 entry = opcode_get_entry(op, graph->opcode_hash);
1454 cnt_inc(&entry->into_Id);
1456 /* increase local value */
1457 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1458 entry = opcode_get_entry(op, graph->opcode_hash);
1459 cnt_inc(&entry->into_Id);
1462 } /* stat_turn_into_id */
1465 * Hook: A node is normalized
1467 * @param ctx the hook context
1468 * @param node the IR node that was normalized
1470 static void stat_normalize(void *ctx, ir_node *node)
1473 if (! status->stat_options)
1478 node_entry_t *entry;
1479 graph_entry_t *graph;
1480 ir_op *op = stat_get_irn_op(node);
1482 /* increase global value */
1483 graph = graph_get_entry(NULL, status->irg_hash);
1484 entry = opcode_get_entry(op, graph->opcode_hash);
1485 cnt_inc(&entry->normalized);
1487 /* increase local value */
1488 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1489 entry = opcode_get_entry(op, graph->opcode_hash);
1490 cnt_inc(&entry->normalized);
1493 } /* stat_normalize */
1496 * Hook: A new graph was created
1498 * @param ctx the hook context
1499 * @param irg the new IR graph that was created
1500 * @param ent the entity of this graph
1502 static void stat_new_graph(void *ctx, ir_graph *irg, ir_entity *ent)
1505 if (! status->stat_options)
1510 /* execute for side effect :-) */
1511 graph_entry_t * graph = graph_get_entry(irg, status->irg_hash);
1514 graph->is_deleted = 0;
1516 graph->is_leaf_call = 0;
1517 graph->is_recursive = 0;
1518 graph->is_chain_call = 0;
1519 graph->is_strict = 1;
1520 graph->is_analyzed = 0;
1523 } /* stat_new_graph */
1526 * Hook: A graph will be deleted
1528 * @param ctx the hook context
1529 * @param irg the IR graph that will be deleted
1531 * Note that we still hold the information for this graph
1532 * in our hash maps, only a flag is set which prevents this
1533 * information from being changed, it's "frozen" from now.
1535 static void stat_free_graph(void *ctx, ir_graph *irg)
1538 if (! status->stat_options)
1543 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1544 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1546 graph->is_deleted = 1;
1548 if (status->stat_options & FIRMSTAT_COUNT_DELETED) {
1549 /* count the nodes of the graph yet, it will be destroyed later */
1550 update_graph_stat(global, graph);
1554 } /* stat_free_graph */
1557 * Hook: A walk over a graph 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(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1569 if (! status->stat_options)
1574 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1576 cnt_inc(&graph->cnt[gcnt_acc_walked]);
1579 } /* stat_irg_walk */
1582 * Hook: A walk over a graph in block-wise order is initiated. Do not count walks from statistic code.
1584 * @param ctx the hook context
1585 * @param irg the IR graph that will be walked
1586 * @param pre the pre walker
1587 * @param post the post walker
1589 static void stat_irg_walk_blkwise(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1591 /* for now, do NOT differentiate between blockwise and normal */
1592 stat_irg_walk(ctx, irg, pre, post);
1593 } /* stat_irg_walk_blkwise */
1596 * Hook: A walk over the graph's blocks is initiated. Do not count walks from statistic code.
1598 * @param ctx the hook context
1599 * @param irg the IR graph that will be walked
1600 * @param node the IR node
1601 * @param pre the pre walker
1602 * @param post the post walker
1604 static void stat_irg_block_walk(void *ctx, ir_graph *irg, ir_node *node, generic_func *pre, generic_func *post)
1610 if (! status->stat_options)
1615 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1617 cnt_inc(&graph->cnt[gcnt_acc_walked_blocks]);
1620 } /* stat_irg_block_walk */
1623 * Called for every node that is removed due to an optimization.
1625 * @param n the IR node that will be removed
1626 * @param hmap the hash map containing ir_op* -> opt_entry_t*
1627 * @param kind the optimization kind
1629 static void removed_due_opt(ir_node *n, hmap_opt_entry_t *hmap, hook_opt_kind kind)
1632 ir_op *op = stat_get_irn_op(n);
1634 /* ignore CSE for Constants */
1635 if (kind == HOOK_OPT_CSE && (is_Const(n) || is_SymConst(n)))
1638 /* increase global value */
1639 entry = opt_get_entry(op, hmap);
1640 cnt_inc(&entry->count);
1641 } /* removed_due_opt */
1644 * Hook: Some nodes were optimized into some others due to an optimization.
1646 * @param ctx the hook context
1648 static void stat_merge_nodes(
1650 ir_node **new_node_array, int new_num_entries,
1651 ir_node **old_node_array, int old_num_entries,
1655 if (! status->stat_options)
1661 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1663 cnt_inc(&status->num_opts[opt]);
1664 if (status->reassoc_run)
1665 opt = HOOK_OPT_REASSOC;
1667 for (i = 0; i < old_num_entries; ++i) {
1668 /* nodes might be in new and old, so if we found a node
1669 in both sets, this one is NOT removed */
1670 for (j = 0; j < new_num_entries; ++j) {
1671 if (old_node_array[i] == new_node_array[j])
1674 if (j >= new_num_entries) {
1677 /* sometimes we did not detect, that it is replaced by a Const */
1678 if (opt == HOOK_OPT_CONFIRM && new_num_entries == 1) {
1679 ir_op *op = get_irn_op(new_node_array[0]);
1681 if (op == op_Const || op == op_SymConst)
1682 xopt = HOOK_OPT_CONFIRM_C;
1685 removed_due_opt(old_node_array[i], graph->opt_hash[xopt], (hook_opt_kind)xopt);
1690 } /* stat_merge_nodes */
1693 * Hook: Reassociation is started/stopped.
1695 * @param ctx the hook context
1696 * @param flag if non-zero, reassociation is started else stopped
1698 static void stat_reassociate(void *ctx, int flag)
1701 if (! status->stat_options)
1706 status->reassoc_run = flag;
1709 } /* stat_reassociate */
1712 * Hook: A node was lowered into other nodes
1714 * @param ctx the hook context
1715 * @param node the IR node that will be lowered
1717 static void stat_lower(void *ctx, ir_node *node)
1720 if (! status->stat_options)
1725 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1727 removed_due_opt(node, graph->opt_hash[HOOK_LOWERED], HOOK_LOWERED);
1733 * Hook: A graph was inlined.
1735 * @param ctx the hook context
1736 * @param call the IR call that will re changed into the body of
1737 * the called IR graph
1738 * @param called_irg the IR graph representing the called routine
1740 static void stat_inline(void *ctx, ir_node *call, ir_graph *called_irg)
1743 if (! status->stat_options)
1748 ir_graph *irg = get_irn_irg(call);
1749 graph_entry_t *i_graph = graph_get_entry(called_irg, status->irg_hash);
1750 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1752 cnt_inc(&graph->cnt[gcnt_acc_got_inlined]);
1753 cnt_inc(&i_graph->cnt[gcnt_acc_was_inlined]);
1759 * Hook: A graph with tail-recursions was optimized.
1761 * @param ctx the hook context
1763 static void stat_tail_rec(void *ctx, ir_graph *irg, int n_calls)
1766 if (! status->stat_options)
1771 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1773 graph->num_tail_recursion += n_calls;
1776 } /* stat_tail_rec */
1779 * Strength reduction was performed on an iteration variable.
1781 * @param ctx the hook context
1783 static void stat_strength_red(void *ctx, ir_graph *irg, ir_node *strong)
1786 if (! status->stat_options)
1791 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1792 cnt_inc(&graph->cnt[gcnt_acc_strength_red]);
1794 removed_due_opt(strong, graph->opt_hash[HOOK_OPT_STRENGTH_RED], HOOK_OPT_STRENGTH_RED);
1797 } /* stat_strength_red */
1800 * Hook: Start/Stop the dead node elimination.
1802 * @param ctx the hook context
1804 static void stat_dead_node_elim(void *ctx, ir_graph *irg, int start)
1808 if (! status->stat_options)
1811 status->in_dead_node_elim = (start != 0);
1812 } /* stat_dead_node_elim */
1815 * Hook: if-conversion was tried.
1817 static void stat_if_conversion(void *context, ir_graph *irg, ir_node *phi,
1818 int pos, ir_node *mux, if_result_t reason)
1824 if (! status->stat_options)
1829 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1831 cnt_inc(&graph->cnt[gcnt_if_conv + reason]);
1834 } /* stat_if_conversion */
1837 * Hook: real function call was optimized.
1839 static void stat_func_call(void *context, ir_graph *irg, ir_node *call)
1843 if (! status->stat_options)
1848 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1850 cnt_inc(&graph->cnt[gcnt_acc_real_func_call]);
1853 } /* stat_func_call */
1856 * Hook: A multiply was replaced by a series of Shifts/Adds/Subs.
1858 * @param ctx the hook context
1860 static void stat_arch_dep_replace_mul_with_shifts(void *ctx, ir_node *mul)
1863 if (! status->stat_options)
1868 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1869 removed_due_opt(mul, graph->opt_hash[HOOK_OPT_ARCH_DEP], HOOK_OPT_ARCH_DEP);
1872 } /* stat_arch_dep_replace_mul_with_shifts */
1875 * Hook: A division by const was replaced.
1877 * @param ctx the hook context
1878 * @param node the division node that will be optimized
1880 static void stat_arch_dep_replace_division_by_const(void *ctx, ir_node *node)
1883 if (! status->stat_options)
1888 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1889 removed_due_opt(node, graph->opt_hash[HOOK_OPT_ARCH_DEP], HOOK_OPT_ARCH_DEP);
1892 } /* stat_arch_dep_replace_division_by_const */
1895 * Update the register pressure of a block.
1897 * @param irg the irg containing the block
1898 * @param block the block for which the reg pressure should be set
1899 * @param pressure the pressure
1900 * @param class_name the name of the register class
1902 void stat_be_block_regpressure(ir_graph *irg, ir_node *block, int pressure, const char *class_name)
1904 if (! status->stat_options)
1909 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1910 be_block_entry_t *block_ent;
1911 reg_pressure_entry_t *rp_ent;
1913 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1914 rp_ent = OALLOCZ(&status->be_data, reg_pressure_entry_t);
1916 rp_ent->class_name = class_name;
1917 rp_ent->pressure = pressure;
1919 pset_insert(block_ent->reg_pressure, rp_ent, hash_ptr(class_name));
1922 } /* stat_be_block_regpressure */
1925 * Update the distribution of ready nodes of a block
1927 * @param irg the irg containing the block
1928 * @param block the block for which the reg pressure should be set
1929 * @param num_ready the number of ready nodes
1931 void stat_be_block_sched_ready(ir_graph *irg, ir_node *block, int num_ready)
1933 if (! status->stat_options)
1938 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1939 be_block_entry_t *block_ent;
1941 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1943 /* increase the counter of corresponding number of ready nodes */
1944 stat_inc_int_distrib_tbl(block_ent->sched_ready, num_ready);
1947 } /* stat_be_block_sched_ready */
1950 * Update the permutation statistic of a block.
1952 * @param class_name the name of the register class
1953 * @param n_regs number of registers in the register class
1954 * @param perm the perm node
1955 * @param block the block containing the perm
1956 * @param size the size of the perm
1957 * @param real_size number of pairs with different registers
1959 void stat_be_block_stat_perm(const char *class_name, int n_regs, ir_node *perm, ir_node *block,
1960 int size, int real_size)
1962 if (! status->stat_options)
1967 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
1968 be_block_entry_t *block_ent;
1969 perm_class_entry_t *pc_ent;
1970 perm_stat_entry_t *ps_ent;
1972 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1973 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
1974 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
1976 pc_ent->n_regs = n_regs;
1978 /* update information */
1979 ps_ent->size = size;
1980 ps_ent->real_size = real_size;
1983 } /* stat_be_block_stat_perm */
1986 * Update the permutation statistic of a single perm.
1988 * @param class_name the name of the register class
1989 * @param perm the perm node
1990 * @param block the block containing the perm
1991 * @param is_chain 1 if chain, 0 if cycle
1992 * @param size length of the cycle/chain
1993 * @param n_ops the number of ops representing this cycle/chain after lowering
1995 void stat_be_block_stat_permcycle(const char *class_name, ir_node *perm, ir_node *block,
1996 int is_chain, int size, int n_ops)
1998 if (! status->stat_options)
2003 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
2004 be_block_entry_t *block_ent;
2005 perm_class_entry_t *pc_ent;
2006 perm_stat_entry_t *ps_ent;
2008 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
2009 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
2010 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
2013 ps_ent->n_copies += n_ops;
2014 stat_inc_int_distrib_tbl(ps_ent->chains, size);
2016 ps_ent->n_exchg += n_ops;
2017 stat_inc_int_distrib_tbl(ps_ent->cycles, size);
2021 } /* stat_be_block_stat_permcycle */
2023 /* Dumps a statistics snapshot. */
2024 void stat_dump_snapshot(const char *name, const char *phase)
2030 if (! status->stat_options)
2035 graph_entry_t *entry;
2036 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
2039 * The constant counter is only global, so we clear it here.
2040 * Note that it does NOT contain the constants in DELETED
2041 * graphs due to this.
2043 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2044 stat_const_clear(status);
2046 /* build the name */
2047 p = strrchr(name, '/');
2052 q = strrchr(name, '\\');
2054 /* NULL might be not the smallest pointer */
2055 if (q && (!p || q > p))
2063 if (l > (int) (sizeof(fname) - 1))
2064 l = sizeof(fname) - 1;
2066 memcpy(fname, name, l);
2072 strncat(fname, "firmstat-", sizeof(fname)-1);
2073 strncat(fname, phase, sizeof(fname)-1);
2074 strncat(fname, "-", sizeof(fname)-1);
2075 strncat(fname, p, sizeof(fname)-1);
2077 stat_dump_init(fname);
2079 /* calculate the graph statistics */
2080 for (entry = (graph_entry_t*)pset_first(status->irg_hash);
2081 entry != NULL; entry = (graph_entry_t*)pset_next(status->irg_hash)) {
2082 if (entry->irg == NULL) {
2083 /* special entry for the global count */
2086 if (! entry->is_deleted) {
2087 /* the graph is still alive, count the nodes on it */
2088 update_graph_stat(global, entry);
2092 /* some calculations are dependent, we pushed them on the wait_q */
2093 while (! pdeq_empty(status->wait_q)) {
2094 entry = (graph_entry_t*)pdeq_getr(status->wait_q);
2096 update_graph_stat_2(global, entry);
2099 /* dump per graph */
2100 for (entry = (graph_entry_t*)pset_first(status->irg_hash);
2101 entry != NULL; entry = (graph_entry_t*)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, ARRAY_SIZE(status->num_opts));
2130 clear_optimization_counter();
2134 stat_finish_pattern_history(fname);
2136 /* clear the global counters here */
2138 node_entry_t *entry;
2140 for (entry = (node_entry_t*)pset_first(global->opcode_hash);
2141 entry != NULL; entry = (node_entry_t*)pset_next(global->opcode_hash)) {
2142 opcode_clear_entry(entry);
2144 /* clear all global counter */
2145 graph_clear_entry(global, /*all=*/1);
2149 } /* stat_dump_snapshot */
2151 typedef struct pass_t {
2152 ir_prog_pass_t pass;
2158 * Wrapper to run stat_dump_snapshot() as a ir_prog wrapper.
2160 static int stat_dump_snapshot_wrapper(ir_prog *irp, void *context)
2162 pass_t *pass = (pass_t*)context;
2165 stat_dump_snapshot(pass->fname, pass->phase);
2167 } /* stat_dump_snapshot_wrapper */
2170 * Ensure that no verifier is run from the wrapper.
2172 static int no_verify(ir_prog *prog, void *ctx)
2180 * Ensure that no dumper is run from the wrapper.
2182 static void no_dump(ir_prog *prog, void *ctx, unsigned idx)
2189 /* create an ir_pog pass */
2190 ir_prog_pass_t *stat_dump_snapshot_pass(
2191 const char *name, const char *fname, const char *phase)
2193 pass_t *pass = XMALLOCZ(pass_t);
2195 def_prog_pass_constructor(
2196 &pass->pass, name ? name : "stat_snapshot", stat_dump_snapshot_wrapper);
2197 pass->fname = fname;
2198 pass->phase = phase;
2200 /* no dump/verify */
2201 pass->pass.dump_irprog = no_dump;
2202 pass->pass.verify_irprog = no_verify;
2205 } /* stat_dump_snapshot_pass */
2207 /** the hook entries for the Firm statistics module */
2208 static hook_entry_t stat_hooks[hook_last];
2210 /* initialize the statistics module. */
2211 void firm_init_stat(unsigned enable_options)
2213 #define X(a) a, sizeof(a)-1
2214 #define HOOK(h, fkt) \
2215 stat_hooks[h].hook._##h = fkt; register_hook(h, &stat_hooks[h])
2218 if (! (enable_options & FIRMSTAT_ENABLED))
2221 status = XMALLOCZ(stat_info_t);
2223 /* enable statistics */
2224 status->stat_options = enable_options & FIRMSTAT_ENABLED ? enable_options : 0;
2226 /* register all hooks */
2227 HOOK(hook_new_ir_op, stat_new_ir_op);
2228 HOOK(hook_free_ir_op, stat_free_ir_op);
2229 HOOK(hook_new_node, stat_new_node);
2230 HOOK(hook_turn_into_id, stat_turn_into_id);
2231 HOOK(hook_normalize, stat_normalize);
2232 HOOK(hook_new_graph, stat_new_graph);
2233 HOOK(hook_free_graph, stat_free_graph);
2234 HOOK(hook_irg_walk, stat_irg_walk);
2235 HOOK(hook_irg_walk_blkwise, stat_irg_walk_blkwise);
2236 HOOK(hook_irg_block_walk, stat_irg_block_walk);
2237 HOOK(hook_merge_nodes, stat_merge_nodes);
2238 HOOK(hook_reassociate, stat_reassociate);
2239 HOOK(hook_lower, stat_lower);
2240 HOOK(hook_inline, stat_inline);
2241 HOOK(hook_tail_rec, stat_tail_rec);
2242 HOOK(hook_strength_red, stat_strength_red);
2243 HOOK(hook_dead_node_elim, stat_dead_node_elim);
2244 HOOK(hook_if_conversion, stat_if_conversion);
2245 HOOK(hook_func_call, stat_func_call);
2246 HOOK(hook_arch_dep_replace_mul_with_shifts, stat_arch_dep_replace_mul_with_shifts);
2247 HOOK(hook_arch_dep_replace_division_by_const, stat_arch_dep_replace_division_by_const);
2249 obstack_init(&status->cnts);
2250 obstack_init(&status->be_data);
2252 /* create the hash-tables */
2253 status->irg_hash = new_pset(graph_cmp, 8);
2254 status->ir_op_hash = new_pset(opcode_cmp_2, 1);
2256 /* create the wait queue */
2257 status->wait_q = new_pdeq();
2259 if (enable_options & FIRMSTAT_COUNT_STRONG_OP) {
2260 /* build the pseudo-ops */
2262 _op_Phi0.code = --num;
2263 _op_Phi0.name = new_id_from_chars(X("Phi0"));
2265 _op_PhiM.code = --num;
2266 _op_PhiM.name = new_id_from_chars(X("PhiM"));
2268 _op_ProjM.code = --num;
2269 _op_ProjM.name = new_id_from_chars(X("ProjM"));
2271 _op_MulC.code = --num;
2272 _op_MulC.name = new_id_from_chars(X("MulC"));
2274 _op_DivC.code = --num;
2275 _op_DivC.name = new_id_from_chars(X("DivC"));
2277 _op_ModC.code = --num;
2278 _op_ModC.name = new_id_from_chars(X("ModC"));
2280 status->op_Phi0 = &_op_Phi0;
2281 status->op_PhiM = &_op_PhiM;
2282 status->op_ProjM = &_op_ProjM;
2283 status->op_MulC = &_op_MulC;
2284 status->op_DivC = &_op_DivC;
2285 status->op_ModC = &_op_ModC;
2287 status->op_Phi0 = NULL;
2288 status->op_PhiM = NULL;
2289 status->op_ProjM = NULL;
2290 status->op_MulC = NULL;
2291 status->op_DivC = NULL;
2292 status->op_ModC = NULL;
2295 /* for Florian: count the Sel depth */
2296 if (enable_options & FIRMSTAT_COUNT_SELS) {
2297 _op_SelSel.code = --num;
2298 _op_SelSel.name = new_id_from_chars(X("Sel(Sel)"));
2300 _op_SelSelSel.code = --num;
2301 _op_SelSelSel.name = new_id_from_chars(X("Sel(Sel(Sel))"));
2303 status->op_SelSel = &_op_SelSel;
2304 status->op_SelSelSel = &_op_SelSelSel;
2306 status->op_SelSel = NULL;
2307 status->op_SelSelSel = NULL;
2310 /* register the dumper */
2311 stat_register_dumper(&simple_dumper);
2313 if (enable_options & FIRMSTAT_CSV_OUTPUT)
2314 stat_register_dumper(&csv_dumper);
2316 /* initialize the pattern hash */
2317 stat_init_pattern_history(enable_options & FIRMSTAT_PATTERN_ENABLED);
2319 /* initialize the Const options */
2320 if (enable_options & FIRMSTAT_COUNT_CONSTS)
2321 stat_init_const_cnt(status);
2323 /* distribution table for parameter counts */
2324 status->dist_param_cnt = stat_new_int_distrib_tbl();
2326 clear_optimization_counter();
2330 } /* firm_init_stat */
2333 * Frees all dumper structures.
2335 static void stat_term_dumper(void)
2337 dumper_t *dumper, *next_dumper;
2339 for (dumper = status->dumper; dumper; /* iteration done in loop body */ ) {
2340 if (dumper->func_map)
2341 del_pset(dumper->func_map);
2343 next_dumper = dumper->next;
2345 dumper = next_dumper;
2347 } /* stat_term_dumper */
2350 /* Terminates the statistics module, frees all memory. */
2351 void stat_term(void)
2353 if (status != (stat_info_t *)&status_disable) {
2354 obstack_free(&status->be_data, NULL);
2355 obstack_free(&status->cnts, NULL);
2360 status = (stat_info_t *)&status_disable;
2364 /* returns 1 if statistics were initialized, 0 otherwise */
2365 int stat_is_active(void)
2367 return status != (stat_info_t *)&status_disable;
2368 } /* stat_is_active */