2 * Copyright (C) 1995-2011 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 Provides several statistic functions for the backend.
23 * @author Christian Wuerdig, Matthias Braun
34 #include "firmstat_t.h"
48 typedef struct pressure_walker_env_t pressure_walker_env_t;
49 struct pressure_walker_env_t {
55 const arch_register_class_t *cls;
58 static void check_reg_pressure_class(pressure_walker_env_t *env,
60 const arch_register_class_t *cls)
62 ir_graph *irg = env->irg;
64 ir_nodeset_t live_nodes;
67 ir_nodeset_init(&live_nodes);
68 be_liveness_end_of_block(env->lv, cls, block, &live_nodes);
69 max_live = ir_nodeset_size(&live_nodes);
70 env->regpressure += max_live;
72 sched_foreach_reverse(block, irn) {
78 be_liveness_transfer(cls, irn, &live_nodes);
79 cnt = ir_nodeset_size(&live_nodes);
80 max_live = cnt < max_live ? max_live : cnt;
81 env->regpressure += cnt;
85 if (max_live > env->max_pressure)
86 env->max_pressure = max_live;
88 stat_be_block_regpressure(irg, block, max_live, cls->name);
89 ir_nodeset_destroy(&live_nodes);
92 static void stat_reg_pressure_block(ir_node *block, void *data)
94 pressure_walker_env_t *env = (pressure_walker_env_t*)data;
96 check_reg_pressure_class(env, block, env->cls);
99 void be_do_stat_reg_pressure(ir_graph *irg, const arch_register_class_t *cls)
101 pressure_walker_env_t env;
102 double average_pressure;
106 env.max_pressure = 0;
108 be_liveness_assure_sets(be_assure_liveness(irg));
109 env.lv = be_get_irg_liveness(irg);
112 /* Collect register pressure information for each block */
113 irg_block_walk_graph(irg, stat_reg_pressure_block, NULL, &env);
115 average_pressure = env.regpressure / env.insn_count;
116 stat_ev_emit("bechordal_average_register_pressure", average_pressure);
117 stat_ev_emit("bechordal_maximum_register_pressure", env.max_pressure);
123 typedef struct estimate_irg_costs_env_t {
124 ir_exec_freq *execfreqs;
126 } estimate_irg_costs_env_t;
128 static void estimate_block_costs(ir_node *block, void *data)
130 estimate_irg_costs_env_t *env = (estimate_irg_costs_env_t*)data;
134 sched_foreach(block, node) {
135 costs += arch_get_op_estimated_cost(node);
138 env->costs += costs * get_block_execfreq(env->execfreqs, block);
141 double be_estimate_irg_costs(ir_graph *irg, ir_exec_freq *execfreqs)
143 estimate_irg_costs_env_t env;
145 env.execfreqs = execfreqs;
148 irg_block_walk_graph(irg, estimate_block_costs, NULL, &env);
155 static void node_stat_walker(ir_node *irn, void *data)
157 be_node_stats_t *const stats = (be_node_stats_t*)data;
159 /* if the node is a normal phi */
161 if (get_irn_mode(irn) == mode_M) {
162 (*stats)[BE_STAT_MEM_PHIS]++;
164 (*stats)[BE_STAT_PHIS]++;
166 } else if (!is_Proj(irn)) {
167 arch_irn_class_t classify = arch_irn_classify(irn);
169 if (classify & arch_irn_class_spill)
170 (*stats)[BE_STAT_SPILLS]++;
171 if (classify & arch_irn_class_reload)
172 (*stats)[BE_STAT_RELOADS]++;
173 if (classify & arch_irn_class_remat)
174 (*stats)[BE_STAT_REMATS]++;
175 if (classify & arch_irn_class_copy)
176 (*stats)[BE_STAT_COPIES]++;
177 if (classify & arch_irn_class_perm)
178 (*stats)[BE_STAT_PERMS]++;
182 void be_collect_node_stats(be_node_stats_t *new_stats, ir_graph *irg)
184 memset(new_stats, 0, sizeof(*new_stats));
185 irg_walk_graph(irg, NULL, node_stat_walker, new_stats);
188 void be_subtract_node_stats(be_node_stats_t *stats, be_node_stats_t *sub)
191 for (i = 0; i < BE_STAT_COUNT; ++i) {
192 (*stats)[i] -= (*sub)[i];
196 void be_copy_node_stats(be_node_stats_t *dest, be_node_stats_t *src)
198 memcpy(dest, src, sizeof(be_node_stats_t));
201 static const char *get_stat_name(enum be_stat_tag_t tag)
204 case BE_STAT_PHIS: return "phis";
205 case BE_STAT_MEM_PHIS: return "mem_phis";
206 case BE_STAT_COPIES: return "copies";
207 case BE_STAT_PERMS: return "perms";
208 case BE_STAT_SPILLS: return "spills";
209 case BE_STAT_RELOADS: return "reloads";
210 case BE_STAT_REMATS: return "remats";
211 default: panic("unknown stat tag found");
215 void be_emit_node_stats(be_node_stats_t *stats, const char *prefix)
217 static char buf[256];
220 for (i = BE_STAT_FIRST; i < BE_STAT_COUNT; ++i) {
221 snprintf(buf, sizeof(buf), "%s%s", prefix, get_stat_name(i));
222 stat_ev_dbl(buf, (*stats)[i]);
228 static void insn_count_walker(ir_node *irn, void *data)
230 unsigned long *cnt = (unsigned long*)data;
232 switch (get_irn_opcode(irn)) {
243 unsigned long be_count_insns(ir_graph *irg)
245 unsigned long cnt = 0;
246 irg_walk_graph(irg, insn_count_walker, NULL, &cnt);
250 static void block_count_walker(ir_node *node, void *data)
252 unsigned long *cnt = (unsigned long*)data;
253 if (node == get_irg_end_block(current_ir_graph))
258 unsigned long be_count_blocks(ir_graph *irg)
260 unsigned long cnt = 0;
261 irg_block_walk_graph(irg, block_count_walker, NULL, &cnt);