2 * This file calls the corresponding statistic functions for
3 * some backend statistics.
4 * @author Christian Wuerdig
17 #include "dbginfo_t.h"
18 #include "firmstat_t.h"
27 #ifdef FIRM_STATISTICS
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 ir_graph *irg = be_get_birg_irg(birg);
105 const arch_env_t *aenv = be_get_birg_arch_env(birg);
106 int i, n = arch_isa_get_n_reg_class(aenv->isa);
108 for (i = 0; i < n; i++) {
109 const arch_register_class_t *cls = arch_isa_get_reg_class(aenv->isa, i);
111 pset *live_nodes = pset_new_ptr(64);
114 live_nodes = be_liveness_end_of_block(env->lv, aenv, cls, block, live_nodes);
115 max_live = pset_count(live_nodes);
117 sched_foreach_reverse(block, irn) {
123 live_nodes = be_liveness_transfer(aenv, cls, irn, live_nodes);
124 cnt = pset_count(live_nodes);
125 max_live = cnt < max_live ? max_live : cnt;
128 stat_be_block_regpressure(irg, block, max_live, cls->name);
132 void be_do_stat_reg_pressure(be_irg_t *birg) {
133 ir_graph *irg = be_get_birg_irg(birg);
135 if (stat_is_active()) {
136 struct a_pressure_walker w;
139 w.lv = be_liveness(irg);
140 /* Collect register pressure information for each block */
141 irg_block_walk_graph(irg, stat_reg_pressure_block, NULL, &w);
142 be_liveness_free(w.lv);
147 * Notify statistic module about amount of ready nodes.
149 void be_do_stat_sched_ready(ir_node *block, const ir_nodeset_t *ready_set) {
150 if (stat_is_active()) {
151 stat_be_block_sched_ready(get_irn_irg(block), block, MIN(ir_nodeset_size(ready_set), 5));
156 * Pass information about a perm to the statistic module.
158 void be_do_stat_perm(const char *class_name, int n_regs, ir_node *perm, ir_node *block, int n, int real_size) {
159 if (stat_is_active()) {
160 stat_be_block_stat_perm(class_name, n_regs, perm, block, n, real_size);
165 * Pass information about a cycle or chain in a perm to the statistic module.
167 void be_do_stat_permcycle(const char *class_name, ir_node *perm, ir_node *block, int is_chain, int n_elems, int n_ops) {
168 if (stat_is_active()) {
169 stat_be_block_stat_permcycle(class_name, perm, block, is_chain, n_elems, n_ops);
174 * Updates nodes statistics.
176 static void do_nodes_stat(ir_node *irn, void *env) {
177 be_stat_phase_t *phase = env;
180 arch_irn_class_t irn_class;
185 mode = get_irn_mode(irn);
186 opc = get_irn_opcode(irn);
190 /* check for nodes we want to ignore */
191 if (be_is_Keep(irn) ||
192 be_is_CopyKeep(irn) ||
197 if (is_Proj(irn) && (mode != mode_X)) {
201 else if (is_Phi(irn)) {
205 else if (mode_is_datab(mode) || ((mode == mode_T) && ! is_be_node(irn)) || (is_Proj(irn) && (mode == mode_X)))
210 else if (opc == iro_Store)
213 irn_class = arch_irn_classify(phase->arch_env, irn);
214 if (irn_class & arch_irn_class_spill)
216 else if (irn_class & arch_irn_class_reload)
218 else if (irn_class & arch_irn_class_stackparam)
220 else if (irn_class & arch_irn_class_load)
222 else if (irn_class & arch_irn_class_store)
227 * Collects node statistics.
229 * @param irg the to do statistics for
230 * @param phase the phase to collect the statistic for
232 void be_do_stat_nodes(ir_graph *irg, const char *phase) {
233 be_stat_irg_t *irg_entry;
234 be_stat_phase_t *phase_entry, phase_key;
236 irg_entry = find_stat_irg_entry(irg);
241 phase_key.phase = phase;
242 phase_entry = pset_find_ptr(irg_entry->phases, &phase_key);
245 phase_entry = obstack_alloc(&irg_entry->obst, sizeof(*phase_entry));
246 phase_entry = pset_insert(irg_entry->phases, phase_entry, HASH_PTR(phase));
248 memset(phase_entry, 0, sizeof(*phase_entry));
250 phase_entry->phase = phase;
251 phase_entry->arch_env = irg_entry->arch_env;
253 irg_walk_blkwise_graph(irg_entry->irg, NULL, do_nodes_stat, phase_entry);
257 * Dumps statistics about nodes (called from dump_snapshot)
259 static void be_dump_node_stat(dumper_t *dmp, graph_entry_t *entry) {
260 be_stat_irg_t *stat_irg = find_stat_irg_entry(entry->irg);
261 be_stat_phase_t *phase;
263 if (! stat_irg || ! stat_irg->phases)
266 fprintf(dmp->f, "===> BE NODE STATISTIC BEGIN <===\n");
268 foreach_pset(stat_irg->phases, phase) {
269 fprintf(dmp->f, "--> Phase: %s\n", phase->phase);
270 fprintf(dmp->f, "# nodes: %ld\n", phase->num_nodes);
271 fprintf(dmp->f, "# data nodes: %ld\n", phase->num_data);
272 fprintf(dmp->f, "# Proj: %ld\n", phase->num_proj);
273 fprintf(dmp->f, "# Phi: %ld\n", phase->num_phi);
274 fprintf(dmp->f, "# Load: %ld\n", phase->num_load);
275 fprintf(dmp->f, "# Store: %ld\n", phase->num_store);
276 fprintf(dmp->f, "# Spill: %ld\n", phase->num_spill);
277 fprintf(dmp->f, "# Reload: %ld\n", phase->num_reload);
280 fprintf(dmp->f, "===> BE NODE STATISTIC END <===\n");
284 * Returns a be statistic object for the given irg.
286 void be_stat_init_irg(const arch_env_t *arch_env, ir_graph *irg) {
287 static int reg_func = 1;
289 if (stat_is_active()) {
290 be_stat_irg_t *stat_irg;
293 be_stat_data = new_set(cmp_stat_data, 8);
295 stat_irg = get_stat_irg_entry(irg);
297 stat_irg->phases = new_pset(cmp_stat_phase, 8);
298 stat_irg->arch_env = arch_env;
299 obstack_init(&stat_irg->obst);
302 /* first init: register dumper */
303 stat_register_dumper_func(be_dump_node_stat);
308 #endif /* FIRM_STATISTICS */
310 typedef struct _estimate_irg_costs_env_t {
311 const arch_env_t *arch_env;
312 ir_exec_freq *execfreqs;
314 } estimate_irg_costs_env_t;
316 static void estimate_block_costs(ir_node *block, void *data)
318 estimate_irg_costs_env_t *env = data;
322 sched_foreach(block, node) {
323 costs += arch_get_op_estimated_cost(env->arch_env, node);
326 env->costs += costs * get_block_execfreq(env->execfreqs, block);
329 double be_estimate_irg_costs(ir_graph *irg, const arch_env_t *arch_env, ir_exec_freq *execfreqs)
331 estimate_irg_costs_env_t env;
333 env.arch_env = arch_env;
334 env.execfreqs = execfreqs;
337 irg_block_walk_graph(irg, estimate_block_costs, NULL, &env);
342 #ifdef FIRM_STATISTICS
344 const char *be_stat_tags[STAT_TAG_LAST];
345 FILE *be_stat_file = NULL;
347 void be_init_stat_file(const char *stat_file_name, const char *sourcefilename)
349 static char time_str[32];
351 assert(be_stat_file == NULL);
353 /* if we want to do some statistics, push the environment. */
354 if (strlen(stat_file_name) == 0)
357 be_stat_file = fopen(stat_file_name, "at");
358 if (be_stat_file == NULL) {
359 fprintf(stderr, "Warning couldn't open statfile '%s'\n", stat_file_name);
363 /* initialize the statistics tags */
364 ir_snprintf(time_str, sizeof(time_str),"%u", time(NULL));
366 be_stat_tags[STAT_TAG_FILE] = sourcefilename;
367 be_stat_tags[STAT_TAG_TIME] = time_str;
368 be_stat_tags[STAT_TAG_IRG] = "<all>";
369 be_stat_tags[STAT_TAG_CLS] = "<all>";
371 be_stat_ev_push(be_stat_tags, STAT_TAG_LAST, be_stat_file);
374 void be_close_stat_file()
377 if (be_stat_file != NULL) {
378 fclose(be_stat_file);
383 #else /* FIRM_STATISTICS */
385 void (be_stat_init_irg)(const arch_env_t *arch_env, ir_graph *irg) {}
386 void (be_do_stat_nodes)(ir_graph *irg, const char *phase) {}
387 void (be_do_stat_reg_pressure)(be_irg_t *birg) {}
388 void (be_do_stat_sched_ready)(ir_node *block, nodeset *ready_set) {}
389 void (be_do_stat_perm)(const char *class_name, int n_regs, ir_node *perm, ir_node *block, int n, int real_size) {}
391 #endif /* FIRM_STATISTICS */