2 * This file is part of libFirm.
3 * Copyright (C) 2012 University of Karlsruhe.
8 * @brief Provides several statistic functions for the backend.
9 * @author Christian Wuerdig, Matthias Braun
20 #include "firmstat_t.h"
34 typedef struct pressure_walker_env_t pressure_walker_env_t;
35 struct pressure_walker_env_t {
41 const arch_register_class_t *cls;
44 static void check_reg_pressure_class(pressure_walker_env_t *env,
46 const arch_register_class_t *cls)
48 ir_graph *irg = env->irg;
49 ir_nodeset_t live_nodes;
52 ir_nodeset_init(&live_nodes);
53 be_liveness_end_of_block(env->lv, cls, block, &live_nodes);
54 max_live = ir_nodeset_size(&live_nodes);
55 env->regpressure += max_live;
57 sched_foreach_reverse(block, irn) {
63 be_liveness_transfer(cls, irn, &live_nodes);
64 cnt = ir_nodeset_size(&live_nodes);
65 max_live = cnt < max_live ? max_live : cnt;
66 env->regpressure += cnt;
70 if (max_live > env->max_pressure)
71 env->max_pressure = max_live;
73 stat_be_block_regpressure(irg, block, max_live, cls->name);
74 ir_nodeset_destroy(&live_nodes);
77 static void stat_reg_pressure_block(ir_node *block, void *data)
79 pressure_walker_env_t *env = (pressure_walker_env_t*)data;
81 check_reg_pressure_class(env, block, env->cls);
84 void be_do_stat_reg_pressure(ir_graph *irg, const arch_register_class_t *cls)
86 pressure_walker_env_t env;
87 double average_pressure;
93 be_assure_live_sets(irg);
94 env.lv = be_get_irg_liveness(irg);
97 /* Collect register pressure information for each block */
98 irg_block_walk_graph(irg, stat_reg_pressure_block, NULL, &env);
100 average_pressure = env.regpressure / env.insn_count;
101 stat_ev_dbl("bechordal_average_register_pressure", average_pressure);
102 stat_ev_dbl("bechordal_maximum_register_pressure", env.max_pressure);
108 typedef struct estimate_irg_costs_env_t {
110 } estimate_irg_costs_env_t;
112 static void estimate_block_costs(ir_node *block, void *data)
114 estimate_irg_costs_env_t *env = (estimate_irg_costs_env_t*)data;
117 sched_foreach(block, node) {
118 costs += arch_get_op_estimated_cost(node);
121 env->costs += costs * get_block_execfreq(block);
124 double be_estimate_irg_costs(ir_graph *irg)
126 estimate_irg_costs_env_t env;
129 irg_block_walk_graph(irg, estimate_block_costs, NULL, &env);
136 static void node_stat_walker(ir_node *irn, void *data)
138 be_node_stats_t *const stats = (be_node_stats_t*)data;
140 /* if the node is a normal phi */
142 if (get_irn_mode(irn) == mode_M) {
143 (*stats)[BE_STAT_MEM_PHIS]++;
145 (*stats)[BE_STAT_PHIS]++;
147 } else if (be_is_Perm(irn)) {
148 (*stats)[BE_STAT_PERMS]++;
149 } else if (be_is_Copy(irn)) {
150 (*stats)[BE_STAT_COPIES]++;
154 void be_collect_node_stats(be_node_stats_t *new_stats, ir_graph *irg)
156 memset(new_stats, 0, sizeof(*new_stats));
157 irg_walk_graph(irg, NULL, node_stat_walker, new_stats);
160 void be_subtract_node_stats(be_node_stats_t *stats, be_node_stats_t *sub)
163 for (i = 0; i < BE_STAT_COUNT; ++i) {
164 (*stats)[i] -= (*sub)[i];
168 void be_copy_node_stats(be_node_stats_t *dest, be_node_stats_t *src)
170 memcpy(dest, src, sizeof(be_node_stats_t));
173 static const char *get_stat_name(enum be_stat_tag_t tag)
176 case BE_STAT_PHIS: return "phis";
177 case BE_STAT_MEM_PHIS: return "mem_phis";
178 case BE_STAT_COPIES: return "copies";
179 case BE_STAT_PERMS: return "perms";
180 default: panic("unknown stat tag found");
184 void be_emit_node_stats(be_node_stats_t *stats, const char *prefix)
186 static char buf[256];
189 for (i = BE_STAT_FIRST; i < BE_STAT_COUNT; ++i) {
190 snprintf(buf, sizeof(buf), "%s%s", prefix, get_stat_name(i));
191 stat_ev_dbl(buf, (*stats)[i]);
197 static void insn_count_walker(ir_node *irn, void *data)
199 unsigned long *cnt = (unsigned long*)data;
201 switch (get_irn_opcode(irn)) {
212 unsigned long be_count_insns(ir_graph *irg)
214 unsigned long cnt = 0;
215 irg_walk_graph(irg, insn_count_walker, NULL, &cnt);
219 static void block_count_walker(ir_node *node, void *data)
221 unsigned long *cnt = (unsigned long*)data;
222 if (node == get_irg_end_block(get_irn_irg(node)))
227 unsigned long be_count_blocks(ir_graph *irg)
229 unsigned long cnt = 0;
230 irg_block_walk_graph(irg, block_count_walker, NULL, &cnt);