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)
605 block_entry_t *b_entry;
608 /* check for block */
609 if (is_Block(node)) {
610 arity = get_irn_arity(node);
611 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(node), graph->block_hash);
612 /* mark start end block to allow to filter them out */
613 if (node == get_irg_start_block(graph->irg))
614 b_entry->is_start = 1;
615 else if (node == get_irg_end_block(graph->irg))
618 /* count all incoming edges */
619 for (i = 0; i < arity; ++i) {
620 ir_node *pred = get_irn_n(node, i);
621 ir_node *other_block = get_nodes_block(pred);
622 block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
624 cnt_inc(&b_entry->cnt[bcnt_in_edges]); /* an edge coming from another block */
625 cnt_inc(&b_entry_other->cnt[bcnt_out_edges]);
630 block = get_nodes_block(node);
631 b_entry = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(block), graph->block_hash);
633 if (is_Phi(node) && mode_is_datab(get_irn_mode(node))) {
634 /* count data Phi per block */
635 cnt_inc(&b_entry->cnt[bcnt_phi_data]);
638 /* we have a new node in our block */
639 cnt_inc(&b_entry->cnt[bcnt_nodes]);
641 /* don't count keep-alive edges */
645 arity = get_irn_arity(node);
647 for (i = 0; i < arity; ++i) {
648 ir_node *pred = get_irn_n(node, i);
649 ir_node *other_block;
651 other_block = get_nodes_block(pred);
653 if (other_block == block)
654 cnt_inc(&b_entry->cnt[bcnt_edges]); /* a in block edge */
656 block_entry_t *b_entry_other = block_get_entry(&graph->recalc_cnts, get_irn_node_nr(other_block), graph->block_hash);
658 cnt_inc(&b_entry->cnt[bcnt_in_edges]); /* an edge coming from another block */
659 cnt_inc(&b_entry_other->cnt[bcnt_out_edges]);
662 } /* undate_block_info */
665 * Calculates how many arguments of the call are const, updates
666 * param distribution.
668 static void analyse_params_of_Call(graph_entry_t *graph, ir_node *call)
670 int i, num_const_args = 0, num_local_adr = 0;
671 int n = get_Call_n_params(call);
673 for (i = 0; i < n; ++i) {
674 ir_node *param = get_Call_param(call, i);
676 if (is_irn_constlike(param))
678 else if (is_Sel(param)) {
679 ir_node *base = param;
682 base = get_Sel_ptr(base);
683 } while (is_Sel(base));
685 if (base == get_irg_frame(current_ir_graph))
691 if (num_const_args > 0)
692 cnt_inc(&graph->cnt[gcnt_call_with_cnst_arg]);
693 if (num_const_args == n)
694 cnt_inc(&graph->cnt[gcnt_call_with_all_cnst_arg]);
695 if (num_local_adr > 0)
696 cnt_inc(&graph->cnt[gcnt_call_with_local_adr]);
698 stat_inc_int_distrib_tbl(status->dist_param_cnt, n);
699 } /* analyse_params_of_Call */
702 * Update info on calls.
704 * @param call The call
705 * @param graph The graph entry containing the call
707 static void stat_update_call(ir_node *call, graph_entry_t *graph)
709 ir_node *block = get_nodes_block(call);
710 ir_node *ptr = get_Call_ptr(call);
711 ir_entity *ent = NULL;
712 ir_graph *callee = NULL;
715 * If the block is bad, the whole subgraph will collapse later
716 * so do not count this call.
717 * This happens in dead code.
722 cnt_inc(&graph->cnt[gcnt_all_calls]);
724 /* found a call, this function is not a leaf */
727 if (is_SymConst(ptr)) {
728 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
729 /* ok, we seems to know the entity */
730 ent = get_SymConst_entity(ptr);
731 callee = get_entity_irg(ent);
733 /* it is recursive, if it calls at least once */
734 if (callee == graph->irg)
735 graph->is_recursive = 1;
737 cnt_inc(&graph->cnt[gcnt_external_calls]);
740 /* indirect call, be could not predict */
741 cnt_inc(&graph->cnt[gcnt_indirect_calls]);
743 /* NOT a leaf call */
744 graph->is_leaf_call = LCS_NON_LEAF_CALL;
747 /* check, if it's a chain-call: Then, the call-block
748 * must dominate the end block. */
750 ir_node *curr = get_irg_end_block(graph->irg);
751 int depth = get_Block_dom_depth(block);
753 for (; curr != block && get_Block_dom_depth(curr) > depth;) {
754 curr = get_Block_idom(curr);
756 if (! curr || !is_Block(curr))
761 graph->is_chain_call = 0;
764 /* check, if the callee is a leaf */
766 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
768 if (called->is_analyzed) {
769 if (! called->is_leaf)
770 graph->is_leaf_call = LCS_NON_LEAF_CALL;
774 analyse_params_of_Call(graph, call);
775 } /* stat_update_call */
778 * Update info on calls for graphs on the wait queue.
780 static void stat_update_call_2(ir_node *call, graph_entry_t *graph)
782 ir_node *block = get_nodes_block(call);
783 ir_node *ptr = get_Call_ptr(call);
784 ir_entity *ent = NULL;
785 ir_graph *callee = NULL;
788 * If the block is bad, the whole subgraph will collapse later
789 * so do not count this call.
790 * This happens in dead code.
795 if (is_SymConst(ptr)) {
796 if (get_SymConst_kind(ptr) == symconst_addr_ent) {
797 /* ok, we seems to know the entity */
798 ent = get_SymConst_entity(ptr);
799 callee = get_entity_irg(ent);
803 /* check, if the callee is a leaf */
805 graph_entry_t *called = graph_get_entry(callee, status->irg_hash);
807 assert(called->is_analyzed);
809 if (! called->is_leaf)
810 graph->is_leaf_call = LCS_NON_LEAF_CALL;
812 graph->is_leaf_call = LCS_NON_LEAF_CALL;
813 } /* stat_update_call_2 */
816 * Find the base address and entity of an Sel node.
818 * @param sel the node
820 * @return the base address.
822 static ir_node *find_base_adr(ir_node *sel)
824 ir_node *ptr = get_Sel_ptr(sel);
826 while (is_Sel(ptr)) {
828 ptr = get_Sel_ptr(sel);
831 } /* find_base_adr */
834 * Update info on Load/Store address statistics.
836 static void stat_update_address(ir_node *node, graph_entry_t *graph)
838 unsigned opc = get_irn_opcode(node);
844 /* a global address */
845 cnt_inc(&graph->cnt[gcnt_global_adr]);
848 base = find_base_adr(node);
849 irg = current_ir_graph;
850 if (base == get_irg_frame(irg)) {
851 /* a local Variable. */
852 cnt_inc(&graph->cnt[gcnt_local_adr]);
855 if (is_Proj(base) && skip_Proj(get_Proj_pred(base)) == get_irg_start(irg)) {
856 /* pointer access through parameter, check for THIS */
857 ir_entity *ent = get_irg_entity(irg);
860 ir_type *ent_tp = get_entity_type(ent);
862 if (get_method_calling_convention(ent_tp) & cc_this_call) {
863 if (get_Proj_proj(base) == 0) {
865 cnt_inc(&graph->cnt[gcnt_this_adr]);
870 /* other parameter */
871 cnt_inc(&graph->cnt[gcnt_param_adr]);
874 /* unknown Pointer access */
875 cnt_inc(&graph->cnt[gcnt_other_adr]);
881 } /* stat_update_address */
884 * Walker for reachable nodes count.
886 static void update_node_stat(ir_node *node, void *env)
888 graph_entry_t *graph = (graph_entry_t*)env;
891 ir_op *op = stat_get_irn_op(node);
892 int i, arity = get_irn_arity(node);
894 entry = opcode_get_entry(op, graph->opcode_hash);
896 cnt_inc(&entry->cnt_alive);
897 cnt_add_i(&graph->cnt[gcnt_edges], arity);
899 /* count block edges */
900 undate_block_info(node, graph);
902 /* handle statistics for special node types */
906 /* check for properties that depends on calls like recursion/leaf/indirect call */
907 stat_update_call(node, graph);
910 /* check address properties */
911 stat_update_address(get_Load_ptr(node), graph);
914 /* check address properties */
915 stat_update_address(get_Store_ptr(node), graph);
918 /* check for non-strict Phi nodes */
919 for (i = arity - 1; i >= 0; --i) {
920 ir_node *pred = get_Phi_pred(node, i);
921 if (is_Unknown(pred)) {
922 /* found an Unknown predecessor, graph is not strict */
923 graph->is_strict = 0;
931 /* we want to count the constant IN nodes, not the CSE'ed constant's itself */
932 if (status->stat_options & FIRMSTAT_COUNT_CONSTS) {
935 for (i = get_irn_arity(node) - 1; i >= 0; --i) {
936 ir_node *pred = get_irn_n(node, i);
938 if (is_Const(pred)) {
939 /* check properties of constants */
940 stat_update_const(status, pred, graph);
944 } /* update_node_stat */
947 * Walker for reachable nodes count for graphs on the wait_q.
949 static void update_node_stat_2(ir_node *node, void *env)
951 graph_entry_t *graph = (graph_entry_t*)env;
953 /* check for properties that depends on calls like recursion/leaf/indirect call */
955 stat_update_call_2(node, graph);
956 } /* update_node_stat_2 */
959 * Get the current address mark.
961 static unsigned get_adr_mark(graph_entry_t *graph, ir_node *node)
963 address_mark_entry_t const val = { node, 0 };
964 address_mark_entry_t *value = set_find(address_mark_entry_t, graph->address_mark, &val, sizeof(val), 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 const value = { node, val };
975 (void)set_insert(address_mark_entry_t, graph->address_mark, &value, sizeof(value), hash_ptr(node));
982 * a vcg attribute hook: Color a node with a different color if
983 * it's identified as a part of an address expression or at least referenced
984 * by an address expression.
986 static int stat_adr_mark_hook(FILE *F, ir_node *node, ir_node *local)
988 ir_node *n = local ? local : node;
989 ir_graph *irg = get_irn_irg(n);
990 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
991 unsigned mark = get_adr_mark(graph, n);
993 if (mark & MARK_ADDRESS_CALC)
994 fprintf(F, "color: purple");
995 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
996 fprintf(F, "color: pink");
997 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
998 fprintf(F, "color: lightblue");
1002 /* I know the color! */
1004 } /* stat_adr_mark_hook */
1005 #endif /* DUMP_ADR_MODE */
1008 * Return the "operational" mode of a Firm node.
1010 static ir_mode *get_irn_op_mode(ir_node *node)
1012 switch (get_irn_opcode(node)) {
1014 return get_Load_mode(node);
1016 return get_irn_mode(get_Store_value(node));
1018 return get_irn_mode(get_Div_left(node));
1020 return get_irn_mode(get_Mod_left(node));
1022 /* Cmp is no address calculation, or is it? */
1024 return get_irn_mode(node);
1026 } /* get_irn_op_mode */
1029 * Post-walker that marks every node that is an address calculation.
1031 * Users of a node must be visited first. We ensure this by
1032 * calling it in the post of an outs walk. This should work even in cycles,
1033 * while the normal pre-walk will not.
1035 static void mark_address_calc(ir_node *node, void *env)
1037 graph_entry_t *graph = (graph_entry_t*)env;
1038 ir_mode *mode = get_irn_op_mode(node);
1040 unsigned mark_preds = MARK_REF_NON_ADR;
1042 if (! mode_is_data(mode))
1045 if (mode_is_reference(mode)) {
1046 /* a reference is calculated here, we are sure */
1047 set_adr_mark(graph, node, MARK_ADDRESS_CALC);
1049 mark_preds = MARK_REF_ADR;
1051 unsigned mark = get_adr_mark(graph, node);
1053 if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR) {
1055 * this node has no reference mode, but is only
1056 * referenced by address calculations
1058 mark_preds = MARK_REF_ADR;
1062 /* mark all predecessors */
1063 for (i = 0, n = get_irn_arity(node); i < n; ++i) {
1064 ir_node *pred = get_irn_n(node, i);
1066 mode = get_irn_op_mode(pred);
1067 if (! mode_is_data(mode))
1070 set_adr_mark(graph, pred, get_adr_mark(graph, pred) | mark_preds);
1072 } /* mark_address_calc */
1075 * Post-walker that marks every node that is an address calculation.
1077 * Users of a node must be visited first. We ensure this by
1078 * calling it in the post of an outs walk. This should work even in cycles,
1079 * while the normal pre-walk will not.
1081 static void count_adr_ops(ir_node *node, void *env)
1083 graph_entry_t *graph = (graph_entry_t*)env;
1084 unsigned mark = get_adr_mark(graph, node);
1086 if (mark & MARK_ADDRESS_CALC)
1087 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1088 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == MARK_REF_ADR)
1089 cnt_inc(&graph->cnt[gcnt_pure_adr_ops]);
1090 else if ((mark & (MARK_REF_ADR | MARK_REF_NON_ADR)) == (MARK_REF_ADR|MARK_REF_NON_ADR))
1091 cnt_inc(&graph->cnt[gcnt_all_adr_ops]);
1092 } /* count_adr_ops */
1095 * Called for every graph when the graph is either deleted or stat_dump_snapshot()
1096 * is called, must recalculate all statistic info.
1098 * @param global The global entry
1099 * @param graph The current entry
1101 static void update_graph_stat(graph_entry_t *global, graph_entry_t *graph)
1105 /* clear first the alive counter in the graph */
1106 foreach_pset(graph->opcode_hash, node_entry_t, entry) {
1107 cnt_clr(&entry->cnt_alive);
1108 } /* foreach_pset */
1110 /* set pessimistic values */
1112 graph->is_leaf_call = LCS_UNKNOWN;
1113 graph->is_recursive = 0;
1114 graph->is_chain_call = 1;
1115 graph->is_strict = 1;
1117 /* create new block counter */
1118 graph->block_hash = new_pset(block_cmp, 5);
1120 /* we need dominator info */
1121 if (graph->irg != get_const_code_irg()) {
1122 assure_doms(graph->irg);
1125 /* count the nodes in the graph */
1126 irg_walk_graph(graph->irg, update_node_stat, NULL, graph);
1129 /* Uncomment this code if chain-call means call exact one. */
1130 entry = opcode_get_entry(op_Call, graph->opcode_hash);
1132 /* check if we have more than 1 call */
1133 if (cnt_gt(entry->cnt_alive, 1))
1134 graph->is_chain_call = 0;
1137 /* recursive functions are never chain calls, leafs don't have calls */
1138 if (graph->is_recursive || graph->is_leaf)
1139 graph->is_chain_call = 0;
1141 /* assume we walk every graph only ONCE, we could sum here the global count */
1142 foreach_pset(graph->opcode_hash, node_entry_t, entry) {
1143 node_entry_t *g_entry = opcode_get_entry(entry->op, global->opcode_hash);
1145 /* update the node counter */
1146 cnt_add(&g_entry->cnt_alive, &entry->cnt_alive);
1147 } /* foreach_pset */
1149 /* count the number of address calculation */
1150 if (graph->irg != get_const_code_irg()) {
1151 ir_graph *rem = current_ir_graph;
1153 assure_irg_outs(graph->irg);
1155 /* Must be done an the outs graph */
1156 current_ir_graph = graph->irg;
1157 irg_out_walk(get_irg_start(graph->irg), NULL, mark_address_calc, graph);
1158 current_ir_graph = rem;
1160 #ifdef DUMP_ADR_MODE
1161 /* register the vcg hook and dump the graph for test */
1162 set_dump_node_vcgattr_hook(stat_adr_mark_hook);
1163 dump_ir_block_graph(graph->irg, "-adr");
1164 set_dump_node_vcgattr_hook(NULL);
1165 #endif /* DUMP_ADR_MODE */
1167 irg_walk_graph(graph->irg, NULL, count_adr_ops, graph);
1170 /* count the DAG's */
1171 if (status->stat_options & FIRMSTAT_COUNT_DAG)
1172 count_dags_in_graph(global, graph);
1174 /* calculate the patterns of this graph */
1175 stat_calc_pattern_history(graph->irg);
1177 /* leaf function did not call others */
1179 graph->is_leaf_call = LCS_NON_LEAF_CALL;
1180 else if (graph->is_leaf_call == LCS_UNKNOWN) {
1181 /* we still don't know if this graph calls leaf-functions, so enqueue */
1182 pdeq_putl(status->wait_q, graph);
1185 /* we have analyzed this graph */
1186 graph->is_analyzed = 1;
1188 /* accumulate all counter's */
1189 for (i = 0; i < _gcnt_last; ++i)
1190 cnt_add(&global->cnt[i], &graph->cnt[i]);
1191 } /* update_graph_stat */
1194 * Called for every graph that was on the wait_q in stat_dump_snapshot()
1195 * must finish all statistic info calculations.
1197 * @param global The global entry
1198 * @param graph The current entry
1200 static void update_graph_stat_2(graph_entry_t *global, graph_entry_t *graph)
1203 if (graph->is_deleted) {
1204 /* deleted, ignore */
1209 /* count the nodes in the graph */
1210 irg_walk_graph(graph->irg, update_node_stat_2, NULL, graph);
1212 if (graph->is_leaf_call == LCS_UNKNOWN)
1213 graph->is_leaf_call = LCS_LEAF_CALL;
1215 } /* update_graph_stat_2 */
1218 * Register a dumper.
1220 static void stat_register_dumper(const dumper_t *dumper)
1222 dumper_t *p = XMALLOC(dumper_t);
1225 p->next = status->dumper;
1229 /* FIXME: memory leak */
1230 } /* stat_register_dumper */
1233 * Dumps the statistics of an IR graph.
1235 static void stat_dump_graph(graph_entry_t *entry)
1239 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1240 if (dumper->dump_graph)
1241 dumper->dump_graph(dumper, entry);
1243 } /* stat_dump_graph */
1246 * Calls all registered dumper functions.
1248 static void stat_dump_registered(graph_entry_t *entry)
1252 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1253 if (dumper->func_map) {
1254 foreach_pset(dumper->func_map, dump_graph_FUNC, func)
1255 func(dumper, entry);
1258 } /* stat_dump_registered */
1261 * Dumps a constant table.
1263 static void stat_dump_consts(const constant_info_t *tbl)
1267 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1268 if (dumper->dump_const_tbl)
1269 dumper->dump_const_tbl(dumper, tbl);
1271 } /* stat_dump_consts */
1274 * Dumps the parameter distribution
1276 static void stat_dump_param_tbl(const distrib_tbl_t *tbl, graph_entry_t *global)
1280 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1281 if (dumper->dump_param_tbl)
1282 dumper->dump_param_tbl(dumper, tbl, global);
1284 } /* stat_dump_param_tbl */
1287 * Dumps the optimization counter
1289 static void stat_dump_opt_cnt(const counter_t *tbl, unsigned len)
1293 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1294 if (dumper->dump_opt_cnt)
1295 dumper->dump_opt_cnt(dumper, tbl, len);
1297 } /* stat_dump_opt_cnt */
1300 * Initialize the dumper.
1302 static void stat_dump_init(const char *name)
1306 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1308 dumper->init(dumper, name);
1310 } /* stat_dump_init */
1313 * Finish the dumper.
1315 static void stat_dump_finish(void)
1319 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1321 dumper->finish(dumper);
1323 } /* stat_dump_finish */
1326 * Register an additional function for all dumper.
1328 void stat_register_dumper_func(dump_graph_FUNC *const func)
1332 for (dumper = status->dumper; dumper; dumper = dumper->next) {
1333 if (! dumper->func_map)
1334 dumper->func_map = pset_new_ptr(3);
1335 pset_insert_ptr(dumper->func_map, (void*)func);
1337 } /* stat_register_dumper_func */
1339 /* ---------------------------------------------------------------------- */
1342 * Helper: get an ir_op from an opcode.
1344 ir_op *stat_get_op_from_opcode(unsigned code)
1346 return opcode_find_entry((ir_opcode)code, status->ir_op_hash);
1347 } /* stat_get_op_from_opcode */
1350 * Hook: A new IR op is registered.
1352 * @param ctx the hook context
1353 * @param op the new IR opcode that was created.
1355 static void stat_new_ir_op(void *ctx, ir_op *op)
1358 if (! status->stat_options)
1363 graph_entry_t *graph = graph_get_entry(NULL, status->irg_hash);
1365 /* execute for side effect :-) */
1366 (void)opcode_get_entry(op, graph->opcode_hash);
1368 pset_insert(status->ir_op_hash, op, op->code);
1371 } /* stat_new_ir_op */
1374 * Hook: An IR op is freed.
1376 * @param ctx the hook context
1377 * @param op the IR opcode that is freed
1379 static void stat_free_ir_op(void *ctx, ir_op *op)
1383 if (! status->stat_options)
1390 } /* stat_free_ir_op */
1393 * Hook: A new node is created.
1395 * @param ctx the hook context
1396 * @param irg the IR graph on which the node is created
1397 * @param node the new IR node that was created
1399 static void stat_new_node(void *ctx, ir_graph *irg, ir_node *node)
1403 if (! status->stat_options)
1406 /* do NOT count during dead node elimination */
1407 if (status->in_dead_node_elim)
1412 node_entry_t *entry;
1413 graph_entry_t *graph;
1414 ir_op *op = stat_get_irn_op(node);
1416 /* increase global value */
1417 graph = graph_get_entry(NULL, status->irg_hash);
1418 entry = opcode_get_entry(op, graph->opcode_hash);
1419 cnt_inc(&entry->new_node);
1421 /* increase local value */
1422 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1423 entry = opcode_get_entry(op, graph->opcode_hash);
1424 cnt_inc(&entry->new_node);
1427 } /* stat_new_node */
1430 * Hook: A node is changed into a Id node
1432 * @param ctx the hook context
1433 * @param node the IR node that will be turned into an ID
1435 static void stat_turn_into_id(void *ctx, ir_node *node)
1438 if (! status->stat_options)
1443 node_entry_t *entry;
1444 graph_entry_t *graph;
1445 ir_op *op = stat_get_irn_op(node);
1447 /* increase global value */
1448 graph = graph_get_entry(NULL, status->irg_hash);
1449 entry = opcode_get_entry(op, graph->opcode_hash);
1450 cnt_inc(&entry->into_Id);
1452 /* increase local value */
1453 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1454 entry = opcode_get_entry(op, graph->opcode_hash);
1455 cnt_inc(&entry->into_Id);
1458 } /* stat_turn_into_id */
1461 * Hook: A node is normalized
1463 * @param ctx the hook context
1464 * @param node the IR node that was normalized
1466 static void stat_normalize(void *ctx, ir_node *node)
1469 if (! status->stat_options)
1474 node_entry_t *entry;
1475 graph_entry_t *graph;
1476 ir_op *op = stat_get_irn_op(node);
1478 /* increase global value */
1479 graph = graph_get_entry(NULL, status->irg_hash);
1480 entry = opcode_get_entry(op, graph->opcode_hash);
1481 cnt_inc(&entry->normalized);
1483 /* increase local value */
1484 graph = graph_get_entry(current_ir_graph, status->irg_hash);
1485 entry = opcode_get_entry(op, graph->opcode_hash);
1486 cnt_inc(&entry->normalized);
1489 } /* stat_normalize */
1492 * Hook: A new graph was created
1494 * @param ctx the hook context
1495 * @param irg the new IR graph that was created
1496 * @param ent the entity of this graph
1498 static void stat_new_graph(void *ctx, ir_graph *irg, ir_entity *ent)
1501 if (! status->stat_options)
1506 /* execute for side effect :-) */
1507 graph_entry_t * graph = graph_get_entry(irg, status->irg_hash);
1510 graph->is_deleted = 0;
1512 graph->is_leaf_call = 0;
1513 graph->is_recursive = 0;
1514 graph->is_chain_call = 0;
1515 graph->is_strict = 1;
1516 graph->is_analyzed = 0;
1519 } /* stat_new_graph */
1522 * Hook: A graph will be deleted
1524 * @param ctx the hook context
1525 * @param irg the IR graph that will be deleted
1527 * Note that we still hold the information for this graph
1528 * in our hash maps, only a flag is set which prevents this
1529 * information from being changed, it's "frozen" from now.
1531 static void stat_free_graph(void *ctx, ir_graph *irg)
1534 if (! status->stat_options)
1539 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1540 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
1542 graph->is_deleted = 1;
1544 if (status->stat_options & FIRMSTAT_COUNT_DELETED) {
1545 /* count the nodes of the graph yet, it will be destroyed later */
1546 update_graph_stat(global, graph);
1550 } /* stat_free_graph */
1553 * Hook: A walk over a graph is initiated. Do not count walks from statistic code.
1555 * @param ctx the hook context
1556 * @param irg the IR graph that will be walked
1557 * @param pre the pre walker
1558 * @param post the post walker
1560 static void stat_irg_walk(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1565 if (! status->stat_options)
1570 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1572 cnt_inc(&graph->cnt[gcnt_acc_walked]);
1575 } /* stat_irg_walk */
1578 * Hook: A walk over a graph in block-wise order is initiated. Do not count walks from statistic code.
1580 * @param ctx the hook context
1581 * @param irg the IR graph that will be walked
1582 * @param pre the pre walker
1583 * @param post the post walker
1585 static void stat_irg_walk_blkwise(void *ctx, ir_graph *irg, generic_func *pre, generic_func *post)
1587 /* for now, do NOT differentiate between blockwise and normal */
1588 stat_irg_walk(ctx, irg, pre, post);
1589 } /* stat_irg_walk_blkwise */
1592 * Hook: A walk over the graph's blocks is initiated. Do not count walks from statistic code.
1594 * @param ctx the hook context
1595 * @param irg the IR graph that will be walked
1596 * @param node the IR node
1597 * @param pre the pre walker
1598 * @param post the post walker
1600 static void stat_irg_block_walk(void *ctx, ir_graph *irg, ir_node *node, generic_func *pre, generic_func *post)
1606 if (! status->stat_options)
1611 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1613 cnt_inc(&graph->cnt[gcnt_acc_walked_blocks]);
1616 } /* stat_irg_block_walk */
1619 * Called for every node that is removed due to an optimization.
1621 * @param n the IR node that will be removed
1622 * @param hmap the hash map containing ir_op* -> opt_entry_t*
1623 * @param kind the optimization kind
1625 static void removed_due_opt(ir_node *n, hmap_opt_entry_t *hmap, hook_opt_kind kind)
1628 ir_op *op = stat_get_irn_op(n);
1630 /* ignore CSE for Constants */
1631 if (kind == HOOK_OPT_CSE && (is_Const(n) || is_SymConst(n)))
1634 /* increase global value */
1635 entry = opt_get_entry(op, hmap);
1636 cnt_inc(&entry->count);
1637 } /* removed_due_opt */
1640 * Hook: Some nodes were optimized into some others due to an optimization.
1642 * @param ctx the hook context
1644 static void stat_merge_nodes(
1646 ir_node **new_node_array, int new_num_entries,
1647 ir_node **old_node_array, int old_num_entries,
1651 if (! status->stat_options)
1657 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1659 cnt_inc(&status->num_opts[opt]);
1660 if (status->reassoc_run)
1661 opt = HOOK_OPT_REASSOC;
1663 for (i = 0; i < old_num_entries; ++i) {
1664 /* nodes might be in new and old, so if we found a node
1665 in both sets, this one is NOT removed */
1666 for (j = 0; j < new_num_entries; ++j) {
1667 if (old_node_array[i] == new_node_array[j])
1670 if (j >= new_num_entries) {
1673 /* sometimes we did not detect, that it is replaced by a Const */
1674 if (opt == HOOK_OPT_CONFIRM && new_num_entries == 1) {
1675 ir_node *const irn = new_node_array[0];
1676 if (is_Const(irn) || is_SymConst(irn))
1677 xopt = HOOK_OPT_CONFIRM_C;
1680 removed_due_opt(old_node_array[i], graph->opt_hash[xopt], (hook_opt_kind)xopt);
1685 } /* stat_merge_nodes */
1688 * Hook: Reassociation is started/stopped.
1690 * @param ctx the hook context
1691 * @param flag if non-zero, reassociation is started else stopped
1693 static void stat_reassociate(void *ctx, int flag)
1696 if (! status->stat_options)
1701 status->reassoc_run = flag;
1704 } /* stat_reassociate */
1707 * Hook: A node was lowered into other nodes
1709 * @param ctx the hook context
1710 * @param node the IR node that will be lowered
1712 static void stat_lower(void *ctx, ir_node *node)
1715 if (! status->stat_options)
1720 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1722 removed_due_opt(node, graph->opt_hash[HOOK_LOWERED], HOOK_LOWERED);
1728 * Hook: A graph was inlined.
1730 * @param ctx the hook context
1731 * @param call the IR call that will re changed into the body of
1732 * the called IR graph
1733 * @param called_irg the IR graph representing the called routine
1735 static void stat_inline(void *ctx, ir_node *call, ir_graph *called_irg)
1738 if (! status->stat_options)
1743 ir_graph *irg = get_irn_irg(call);
1744 graph_entry_t *i_graph = graph_get_entry(called_irg, status->irg_hash);
1745 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1747 cnt_inc(&graph->cnt[gcnt_acc_got_inlined]);
1748 cnt_inc(&i_graph->cnt[gcnt_acc_was_inlined]);
1754 * Hook: A graph with tail-recursions was optimized.
1756 * @param ctx the hook context
1758 static void stat_tail_rec(void *ctx, ir_graph *irg, int n_calls)
1761 if (! status->stat_options)
1766 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1768 graph->num_tail_recursion += n_calls;
1771 } /* stat_tail_rec */
1774 * Strength reduction was performed on an iteration variable.
1776 * @param ctx the hook context
1778 static void stat_strength_red(void *ctx, ir_graph *irg, ir_node *strong)
1781 if (! status->stat_options)
1786 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1787 cnt_inc(&graph->cnt[gcnt_acc_strength_red]);
1789 removed_due_opt(strong, graph->opt_hash[HOOK_OPT_STRENGTH_RED], HOOK_OPT_STRENGTH_RED);
1792 } /* stat_strength_red */
1795 * Hook: Start/Stop the dead node elimination.
1797 * @param ctx the hook context
1799 static void stat_dead_node_elim(void *ctx, ir_graph *irg, int start)
1803 if (! status->stat_options)
1806 status->in_dead_node_elim = (start != 0);
1807 } /* stat_dead_node_elim */
1810 * Hook: if-conversion was tried.
1812 static void stat_if_conversion(void *context, ir_graph *irg, ir_node *phi,
1813 int pos, ir_node *mux, if_result_t reason)
1819 if (! status->stat_options)
1824 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1826 cnt_inc(&graph->cnt[gcnt_if_conv + reason]);
1829 } /* stat_if_conversion */
1832 * Hook: real function call was optimized.
1834 static void stat_func_call(void *context, ir_graph *irg, ir_node *call)
1838 if (! status->stat_options)
1843 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1845 cnt_inc(&graph->cnt[gcnt_acc_real_func_call]);
1848 } /* stat_func_call */
1851 * Hook: A multiply was replaced by a series of Shifts/Adds/Subs.
1853 * @param ctx the hook context
1855 static void stat_arch_dep_replace_mul_with_shifts(void *ctx, ir_node *mul)
1858 if (! status->stat_options)
1863 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1864 removed_due_opt(mul, graph->opt_hash[HOOK_OPT_ARCH_DEP], HOOK_OPT_ARCH_DEP);
1867 } /* stat_arch_dep_replace_mul_with_shifts */
1870 * Hook: A division by const was replaced.
1872 * @param ctx the hook context
1873 * @param node the division node that will be optimized
1875 static void stat_arch_dep_replace_division_by_const(void *ctx, ir_node *node)
1878 if (! status->stat_options)
1883 graph_entry_t *graph = graph_get_entry(current_ir_graph, status->irg_hash);
1884 removed_due_opt(node, graph->opt_hash[HOOK_OPT_ARCH_DEP], HOOK_OPT_ARCH_DEP);
1887 } /* stat_arch_dep_replace_division_by_const */
1890 * Update the register pressure of a block.
1892 * @param irg the irg containing the block
1893 * @param block the block for which the reg pressure should be set
1894 * @param pressure the pressure
1895 * @param class_name the name of the register class
1897 void stat_be_block_regpressure(ir_graph *irg, ir_node *block, int pressure, const char *class_name)
1899 if (! status->stat_options)
1904 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1905 be_block_entry_t *block_ent;
1906 reg_pressure_entry_t *rp_ent;
1908 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1909 rp_ent = OALLOCZ(&status->be_data, reg_pressure_entry_t);
1911 rp_ent->class_name = class_name;
1912 rp_ent->pressure = pressure;
1914 pset_insert(block_ent->reg_pressure, rp_ent, hash_ptr(class_name));
1917 } /* stat_be_block_regpressure */
1920 * Update the distribution of ready nodes of a block
1922 * @param irg the irg containing the block
1923 * @param block the block for which the reg pressure should be set
1924 * @param num_ready the number of ready nodes
1926 void stat_be_block_sched_ready(ir_graph *irg, ir_node *block, int num_ready)
1928 if (! status->stat_options)
1933 graph_entry_t *graph = graph_get_entry(irg, status->irg_hash);
1934 be_block_entry_t *block_ent;
1936 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1938 /* increase the counter of corresponding number of ready nodes */
1939 stat_inc_int_distrib_tbl(block_ent->sched_ready, num_ready);
1942 } /* stat_be_block_sched_ready */
1945 * Update the permutation statistic of a block.
1947 * @param class_name the name of the register class
1948 * @param n_regs number of registers in the register class
1949 * @param perm the perm node
1950 * @param block the block containing the perm
1951 * @param size the size of the perm
1952 * @param real_size number of pairs with different registers
1954 void stat_be_block_stat_perm(const char *class_name, int n_regs, ir_node *perm, ir_node *block,
1955 int size, int real_size)
1957 if (! status->stat_options)
1962 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
1963 be_block_entry_t *block_ent;
1964 perm_class_entry_t *pc_ent;
1965 perm_stat_entry_t *ps_ent;
1967 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
1968 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
1969 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
1971 pc_ent->n_regs = n_regs;
1973 /* update information */
1974 ps_ent->size = size;
1975 ps_ent->real_size = real_size;
1978 } /* stat_be_block_stat_perm */
1981 * Update the permutation statistic of a single perm.
1983 * @param class_name the name of the register class
1984 * @param perm the perm node
1985 * @param block the block containing the perm
1986 * @param is_chain 1 if chain, 0 if cycle
1987 * @param size length of the cycle/chain
1988 * @param n_ops the number of ops representing this cycle/chain after lowering
1990 void stat_be_block_stat_permcycle(const char *class_name, ir_node *perm, ir_node *block,
1991 int is_chain, int size, int n_ops)
1993 if (! status->stat_options)
1998 graph_entry_t *graph = graph_get_entry(get_irn_irg(block), status->irg_hash);
1999 be_block_entry_t *block_ent;
2000 perm_class_entry_t *pc_ent;
2001 perm_stat_entry_t *ps_ent;
2003 block_ent = be_block_get_entry(&status->be_data, get_irn_node_nr(block), graph->be_block_hash);
2004 pc_ent = perm_class_get_entry(&status->be_data, class_name, block_ent->perm_class_stat);
2005 ps_ent = perm_stat_get_entry(&status->be_data, perm, pc_ent->perm_stat);
2008 ps_ent->n_copies += n_ops;
2009 stat_inc_int_distrib_tbl(ps_ent->chains, size);
2011 ps_ent->n_exchg += n_ops;
2012 stat_inc_int_distrib_tbl(ps_ent->cycles, size);
2016 } /* stat_be_block_stat_permcycle */
2018 /* Dumps a statistics snapshot. */
2019 void stat_dump_snapshot(const char *name, const char *phase)
2025 if (! status->stat_options)
2030 graph_entry_t *global = graph_get_entry(NULL, status->irg_hash);
2033 * The constant counter is only global, so we clear it here.
2034 * Note that it does NOT contain the constants in DELETED
2035 * graphs due to this.
2037 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2038 stat_const_clear(status);
2040 /* build the name */
2041 p = strrchr(name, '/');
2046 q = strrchr(name, '\\');
2048 /* NULL might be not the smallest pointer */
2049 if (q && (!p || q > p))
2057 if (l > (int) (sizeof(fname) - 1))
2058 l = sizeof(fname) - 1;
2060 memcpy(fname, name, l);
2066 strncat(fname, "firmstat-", sizeof(fname)-strlen(fname)-1);
2067 strncat(fname, phase, sizeof(fname)-strlen(fname)-1);
2068 strncat(fname, "-", sizeof(fname)-strlen(fname)-1);
2069 strncat(fname, p, sizeof(fname)-strlen(fname)-1);
2071 stat_dump_init(fname);
2073 /* calculate the graph statistics */
2074 foreach_pset(status->irg_hash, graph_entry_t, entry) {
2075 if (entry->irg == NULL) {
2076 /* special entry for the global count */
2079 if (! entry->is_deleted) {
2080 /* the graph is still alive, count the nodes on it */
2081 update_graph_stat(global, entry);
2085 /* some calculations are dependent, we pushed them on the wait_q */
2086 while (! pdeq_empty(status->wait_q)) {
2087 graph_entry_t *const entry = (graph_entry_t*)pdeq_getr(status->wait_q);
2089 update_graph_stat_2(global, entry);
2092 /* dump per graph */
2093 foreach_pset(status->irg_hash, graph_entry_t, entry) {
2094 if (entry->irg == NULL) {
2095 /* special entry for the global count */
2099 if (! entry->is_deleted || status->stat_options & FIRMSTAT_COUNT_DELETED) {
2100 stat_dump_graph(entry);
2101 stat_dump_registered(entry);
2104 if (! entry->is_deleted) {
2105 /* clear the counter that are not accumulated */
2106 graph_clear_entry(entry, 0);
2111 stat_dump_graph(global);
2113 /* dump the const info */
2114 if (status->stat_options & FIRMSTAT_COUNT_CONSTS)
2115 stat_dump_consts(&status->const_info);
2117 /* dump the parameter distribution */
2118 stat_dump_param_tbl(status->dist_param_cnt, global);
2120 /* dump the optimization counter and clear them */
2121 stat_dump_opt_cnt(status->num_opts, ARRAY_SIZE(status->num_opts));
2122 clear_optimization_counter();
2126 stat_finish_pattern_history(fname);
2128 /* clear the global counters here */
2129 foreach_pset(global->opcode_hash, node_entry_t, entry) {
2130 opcode_clear_entry(entry);
2132 /* clear all global counter */
2133 graph_clear_entry(global, /*all=*/1);
2136 } /* stat_dump_snapshot */
2138 typedef struct pass_t {
2139 ir_prog_pass_t pass;
2145 * Wrapper to run stat_dump_snapshot() as a ir_prog wrapper.
2147 static int stat_dump_snapshot_wrapper(ir_prog *irp, void *context)
2149 pass_t *pass = (pass_t*)context;
2152 stat_dump_snapshot(pass->fname, pass->phase);
2154 } /* stat_dump_snapshot_wrapper */
2157 * Ensure that no verifier is run from the wrapper.
2159 static int no_verify(ir_prog *prog, void *ctx)
2167 * Ensure that no dumper is run from the wrapper.
2169 static void no_dump(ir_prog *prog, void *ctx, unsigned idx)
2176 /* create an ir_pog pass */
2177 ir_prog_pass_t *stat_dump_snapshot_pass(
2178 const char *name, const char *fname, const char *phase)
2180 pass_t *pass = XMALLOCZ(pass_t);
2182 def_prog_pass_constructor(
2183 &pass->pass, name ? name : "stat_snapshot", stat_dump_snapshot_wrapper);
2184 pass->fname = fname;
2185 pass->phase = phase;
2187 /* no dump/verify */
2188 pass->pass.dump_irprog = no_dump;
2189 pass->pass.verify_irprog = no_verify;
2192 } /* stat_dump_snapshot_pass */
2194 /** the hook entries for the Firm statistics module */
2195 static hook_entry_t stat_hooks[hook_last];
2197 /* initialize the statistics module. */
2198 void firm_init_stat(unsigned enable_options)
2200 #define X(a) a, sizeof(a)-1
2201 #define HOOK(h, fkt) \
2202 stat_hooks[h].hook._##h = fkt; register_hook(h, &stat_hooks[h])
2205 if (! (enable_options & FIRMSTAT_ENABLED))
2208 status = XMALLOCZ(stat_info_t);
2210 /* enable statistics */
2211 status->stat_options = enable_options & FIRMSTAT_ENABLED ? enable_options : 0;
2213 /* register all hooks */
2214 HOOK(hook_new_ir_op, stat_new_ir_op);
2215 HOOK(hook_free_ir_op, stat_free_ir_op);
2216 HOOK(hook_new_node, stat_new_node);
2217 HOOK(hook_turn_into_id, stat_turn_into_id);
2218 HOOK(hook_normalize, stat_normalize);
2219 HOOK(hook_new_graph, stat_new_graph);
2220 HOOK(hook_free_graph, stat_free_graph);
2221 HOOK(hook_irg_walk, stat_irg_walk);
2222 HOOK(hook_irg_walk_blkwise, stat_irg_walk_blkwise);
2223 HOOK(hook_irg_block_walk, stat_irg_block_walk);
2224 HOOK(hook_merge_nodes, stat_merge_nodes);
2225 HOOK(hook_reassociate, stat_reassociate);
2226 HOOK(hook_lower, stat_lower);
2227 HOOK(hook_inline, stat_inline);
2228 HOOK(hook_tail_rec, stat_tail_rec);
2229 HOOK(hook_strength_red, stat_strength_red);
2230 HOOK(hook_dead_node_elim, stat_dead_node_elim);
2231 HOOK(hook_if_conversion, stat_if_conversion);
2232 HOOK(hook_func_call, stat_func_call);
2233 HOOK(hook_arch_dep_replace_mul_with_shifts, stat_arch_dep_replace_mul_with_shifts);
2234 HOOK(hook_arch_dep_replace_division_by_const, stat_arch_dep_replace_division_by_const);
2236 obstack_init(&status->cnts);
2237 obstack_init(&status->be_data);
2239 /* create the hash-tables */
2240 status->irg_hash = new_pset(graph_cmp, 8);
2241 status->ir_op_hash = new_pset(opcode_cmp_2, 1);
2243 /* create the wait queue */
2244 status->wait_q = new_pdeq();
2246 if (enable_options & FIRMSTAT_COUNT_STRONG_OP) {
2247 /* build the pseudo-ops */
2249 _op_Phi0.code = --num;
2250 _op_Phi0.name = new_id_from_chars(X("Phi0"));
2252 _op_PhiM.code = --num;
2253 _op_PhiM.name = new_id_from_chars(X("PhiM"));
2255 _op_ProjM.code = --num;
2256 _op_ProjM.name = new_id_from_chars(X("ProjM"));
2258 _op_MulC.code = --num;
2259 _op_MulC.name = new_id_from_chars(X("MulC"));
2261 _op_DivC.code = --num;
2262 _op_DivC.name = new_id_from_chars(X("DivC"));
2264 _op_ModC.code = --num;
2265 _op_ModC.name = new_id_from_chars(X("ModC"));
2267 status->op_Phi0 = &_op_Phi0;
2268 status->op_PhiM = &_op_PhiM;
2269 status->op_ProjM = &_op_ProjM;
2270 status->op_MulC = &_op_MulC;
2271 status->op_DivC = &_op_DivC;
2272 status->op_ModC = &_op_ModC;
2274 status->op_Phi0 = NULL;
2275 status->op_PhiM = NULL;
2276 status->op_ProjM = NULL;
2277 status->op_MulC = NULL;
2278 status->op_DivC = NULL;
2279 status->op_ModC = NULL;
2282 /* for Florian: count the Sel depth */
2283 if (enable_options & FIRMSTAT_COUNT_SELS) {
2284 _op_SelSel.code = --num;
2285 _op_SelSel.name = new_id_from_chars(X("Sel(Sel)"));
2287 _op_SelSelSel.code = --num;
2288 _op_SelSelSel.name = new_id_from_chars(X("Sel(Sel(Sel))"));
2290 status->op_SelSel = &_op_SelSel;
2291 status->op_SelSelSel = &_op_SelSelSel;
2293 status->op_SelSel = NULL;
2294 status->op_SelSelSel = NULL;
2297 /* register the dumper */
2298 stat_register_dumper(&simple_dumper);
2300 if (enable_options & FIRMSTAT_CSV_OUTPUT)
2301 stat_register_dumper(&csv_dumper);
2303 /* initialize the pattern hash */
2304 stat_init_pattern_history(enable_options & FIRMSTAT_PATTERN_ENABLED);
2306 /* initialize the Const options */
2307 if (enable_options & FIRMSTAT_COUNT_CONSTS)
2308 stat_init_const_cnt(status);
2310 /* distribution table for parameter counts */
2311 status->dist_param_cnt = stat_new_int_distrib_tbl();
2313 clear_optimization_counter();
2317 } /* firm_init_stat */
2320 * Frees all dumper structures.
2322 static void stat_term_dumper(void)
2324 dumper_t *dumper, *next_dumper;
2326 for (dumper = status->dumper; dumper; /* iteration done in loop body */ ) {
2327 if (dumper->func_map)
2328 del_pset(dumper->func_map);
2330 next_dumper = dumper->next;
2332 dumper = next_dumper;
2334 } /* stat_term_dumper */
2337 /* Terminates the statistics module, frees all memory. */
2338 void stat_term(void)
2340 if (status != (stat_info_t *)&status_disable) {
2341 obstack_free(&status->be_data, NULL);
2342 obstack_free(&status->cnts, NULL);
2347 status = (stat_info_t *)&status_disable;
2351 /* returns 1 if statistics were initialized, 0 otherwise */
2352 int stat_is_active(void)
2354 return status != (stat_info_t *)&status_disable;
2355 } /* stat_is_active */