2 * This file calls the corresponding statistic functions for
3 * some backend statistics.
4 * @author Christian Wuerdig
11 #ifdef FIRM_STATISTICS
19 #include "dbginfo_t.h"
20 #include "firmstat_t.h"
29 typedef struct _be_stat_irg_t {
30 ir_graph *irg; /**< the irg, the statistic is about */
31 pset *phases; /**< node statistics for each phase */
32 struct obstack obst; /**< the obstack containing the information */
33 const arch_env_t *arch_env; /**< the current arch env */
36 typedef struct _be_stat_phase_t {
37 const arch_env_t *arch_env; /**< the current arch env */
38 const char *phase; /**< the name of the phase the statistic is about */
39 unsigned long num_nodes; /**< overall number of reachable nodes in the irg */
40 unsigned long num_data; /**< number of data nodes ((mode_datab && ! Proj && ! Phi) || mode_T) */
41 unsigned long num_proj; /**< number of Projs */
42 unsigned long num_phi; /**< number of Phis */
43 unsigned long num_load; /**< number of Loads */
44 unsigned long num_store; /**< number of Stores */
45 unsigned long num_spill; /**< number of Spills */
46 unsigned long num_reload; /**< number of Reloads */
49 static set *be_stat_data = NULL;
51 static int cmp_stat_phase(const void *a, const void *b) {
52 const be_stat_phase_t *p1 = a;
53 const be_stat_phase_t *p2 = b;
55 return p1->phase != p2->phase;
58 static int cmp_stat_data(const void *a, const void *b, size_t len) {
59 const be_stat_irg_t *p1 = a;
60 const be_stat_irg_t *p2 = b;
62 return p1->irg != p2->irg;
65 static be_stat_irg_t *find_stat_irg_entry(ir_graph *irg) {
66 be_stat_irg_t *entry, key;
72 entry = set_find(be_stat_data, &key, sizeof(key), HASH_PTR(irg));
77 static be_stat_irg_t *get_stat_irg_entry(ir_graph *irg) {
78 be_stat_irg_t *entry, key;
83 entry = find_stat_irg_entry(irg);
87 entry = set_insert(be_stat_data, &key, sizeof(key), HASH_PTR(irg));
93 struct a_pressure_walker {
99 * Collect reg pressure statistics per block and per class.
101 static void stat_reg_pressure_block(ir_node *block, void *data) {
102 struct a_pressure_walker *env = data;
103 be_irg_t *birg = env->birg;
104 const arch_env_t *aenv = birg->main_env->arch_env;
105 int i, n = arch_isa_get_n_reg_class(aenv->isa);
107 for (i = 0; i < n; i++) {
108 const arch_register_class_t *cls = arch_isa_get_reg_class(aenv->isa, i);
110 pset *live_nodes = pset_new_ptr(64);
113 live_nodes = be_liveness_end_of_block(env->lv, aenv, cls, block, live_nodes);
114 max_live = pset_count(live_nodes);
116 sched_foreach_reverse(block, irn) {
122 live_nodes = be_liveness_transfer(aenv, cls, irn, live_nodes);
123 cnt = pset_count(live_nodes);
124 max_live = cnt < max_live ? max_live : cnt;
127 stat_be_block_regpressure(birg->irg, block, max_live, cls->name);
131 void be_do_stat_reg_pressure(be_irg_t *birg) {
132 if (stat_is_active()) {
133 struct a_pressure_walker w;
136 w.lv = be_liveness(birg->irg);
137 /* Collect register pressure information for each block */
138 irg_block_walk_graph(birg->irg, stat_reg_pressure_block, NULL, &w);
139 be_liveness_free(w.lv);
144 * Notify statistic module about amount of ready nodes.
146 void be_do_stat_sched_ready(ir_node *block, nodeset *ready_set) {
147 if (stat_is_active()) {
148 stat_be_block_sched_ready(get_irn_irg(block), block, MIN(nodeset_count(ready_set), 5));
153 * Pass information about a perm to the statistic module.
155 void be_do_stat_perm(const char *class_name, int n_regs, ir_node *perm, ir_node *block, int n, int real_size) {
156 if (stat_is_active()) {
157 stat_be_block_stat_perm(class_name, n_regs, perm, block, n, real_size);
162 * Pass information about a cycle or chain in a perm to the statistic module.
164 void be_do_stat_permcycle(const char *class_name, ir_node *perm, ir_node *block, int is_chain, int n_elems, int n_ops) {
165 if (stat_is_active()) {
166 stat_be_block_stat_permcycle(class_name, perm, block, is_chain, n_elems, n_ops);
171 * Updates nodes statistics.
173 static void do_nodes_stat(ir_node *irn, void *env) {
174 be_stat_phase_t *phase = env;
177 arch_irn_class_t irn_class;
182 mode = get_irn_mode(irn);
183 opc = get_irn_opcode(irn);
187 /* check for nodes we want to ignore */
188 if (be_is_Keep(irn) ||
189 be_is_CopyKeep(irn) ||
194 if (is_Proj(irn) && (mode != mode_X)) {
198 else if (is_Phi(irn)) {
202 else if (mode_is_datab(mode) || ((mode == mode_T) && ! is_be_node(irn)) || (is_Proj(irn) && (mode == mode_X)))
207 else if (opc == iro_Store)
210 irn_class = arch_irn_classify(phase->arch_env, irn);
211 if (irn_class & arch_irn_class_spill)
213 else if (irn_class & arch_irn_class_reload)
215 else if (irn_class & arch_irn_class_stackparam)
217 else if (irn_class & arch_irn_class_load)
219 else if (irn_class & arch_irn_class_store)
224 * Collects node statistics.
226 * @param irg the to do statistics for
227 * @param phase the phase to collect the statistic for
229 void be_do_stat_nodes(ir_graph *irg, const char *phase) {
230 be_stat_irg_t *irg_entry;
231 be_stat_phase_t *phase_entry, phase_key;
233 irg_entry = find_stat_irg_entry(irg);
238 phase_key.phase = phase;
239 phase_entry = pset_find_ptr(irg_entry->phases, &phase_key);
242 phase_entry = obstack_alloc(&irg_entry->obst, sizeof(*phase_entry));
243 phase_entry = pset_insert(irg_entry->phases, phase_entry, HASH_PTR(phase));
245 memset(phase_entry, 0, sizeof(*phase_entry));
247 phase_entry->phase = phase;
248 phase_entry->arch_env = irg_entry->arch_env;
250 irg_walk_blkwise_graph(irg_entry->irg, NULL, do_nodes_stat, phase_entry);
254 * Dumps statistics about nodes (called from dump_snapshot)
256 static void be_dump_node_stat(dumper_t *dmp, graph_entry_t *entry) {
257 be_stat_irg_t *stat_irg = find_stat_irg_entry(entry->irg);
258 be_stat_phase_t *phase;
260 if (! stat_irg || ! stat_irg->phases)
263 fprintf(dmp->f, "===> BE NODE STATISTIC BEGIN <===\n");
265 foreach_pset(stat_irg->phases, phase) {
266 fprintf(dmp->f, "--> Phase: %s\n", phase->phase);
267 fprintf(dmp->f, "# nodes: %ld\n", phase->num_nodes);
268 fprintf(dmp->f, "# data nodes: %ld\n", phase->num_data);
269 fprintf(dmp->f, "# Proj: %ld\n", phase->num_proj);
270 fprintf(dmp->f, "# Phi: %ld\n", phase->num_phi);
271 fprintf(dmp->f, "# Load: %ld\n", phase->num_load);
272 fprintf(dmp->f, "# Store: %ld\n", phase->num_store);
273 fprintf(dmp->f, "# Spill: %ld\n", phase->num_spill);
274 fprintf(dmp->f, "# Reload: %ld\n", phase->num_reload);
277 fprintf(dmp->f, "===> BE NODE STATISTIC END <===\n");
281 * Returns a be statistic object for the given irg.
283 void be_stat_init_irg(const arch_env_t *arch_env, ir_graph *irg) {
284 static int reg_func = 1;
286 if (stat_is_active()) {
287 be_stat_irg_t *stat_irg;
290 be_stat_data = new_set(cmp_stat_data, 8);
292 stat_irg = get_stat_irg_entry(irg);
294 stat_irg->phases = new_pset(cmp_stat_phase, 8);
295 stat_irg->arch_env = arch_env;
296 obstack_init(&stat_irg->obst);
299 /* first init: register dumper */
300 stat_register_dumper_func(be_dump_node_stat);
306 typedef struct _estimate_irg_costs_env_t {
307 const arch_env_t *arch_env;
308 ir_exec_freq *execfreqs;
310 } estimate_irg_costs_env_t;
312 static void estimate_block_costs(ir_node *block, void *data)
314 estimate_irg_costs_env_t *env = data;
318 sched_foreach(block, node) {
319 costs += arch_get_op_estimated_cost(env->arch_env, node);
322 env->costs += costs * get_block_execfreq(env->execfreqs, block);
325 double be_estimate_irg_costs(ir_graph *irg, const arch_env_t *arch_env, ir_exec_freq *execfreqs)
327 estimate_irg_costs_env_t env;
328 env.arch_env = arch_env;
329 env.execfreqs = execfreqs;
332 irg_block_walk_graph(irg, estimate_block_costs, NULL, &env);
337 const char *be_stat_tags[STAT_TAG_LAST];
338 FILE *be_stat_file = NULL;
340 void be_init_stat_file(const char *stat_file_name, const char *sourcefilename)
342 static char time_str[32];
344 assert(be_stat_file == NULL);
346 /* if we want to do some statistics, push the environment. */
347 if(strlen(stat_file_name) == 0)
350 be_stat_file = fopen(stat_file_name, "at");
351 if(be_stat_file == NULL) {
352 fprintf(stderr, "Warning couldn't open statfile '%s'\n", stat_file_name);
356 /* initialize the statistics tags */
357 ir_snprintf(time_str, sizeof(time_str),"%u", time(NULL));
359 be_stat_tags[STAT_TAG_FILE] = sourcefilename;
360 be_stat_tags[STAT_TAG_TIME] = time_str;
361 be_stat_tags[STAT_TAG_IRG] = "<all>";
362 be_stat_tags[STAT_TAG_CLS] = "<all>";
364 be_stat_ev_push(be_stat_tags, STAT_TAG_LAST, be_stat_file);
367 void be_close_stat_file()
370 if(be_stat_file != NULL) {
371 fclose(be_stat_file);
379 void (be_stat_init_irg)(const arch_env_t *arch_env, ir_graph *irg) {}
380 void (be_do_stat_nodes)(ir_graph *irg, const char *phase) {}
381 void (be_do_stat_reg_pressure)(be_irg_t *birg) {}
382 void (be_do_stat_sched_ready)(ir_node *block, nodeset *ready_set) {}
383 void (be_do_stat_perm)(const char *class_name, int n_regs, ir_node *perm, ir_node *block, int n, int real_size) {}
385 #endif /* FIRM_STATISTICS */