beifg: Factorise code to count interference components.
[libfirm] / ir / be / bestat.c
1 /*
2  * This file is part of libFirm.
3  * Copyright (C) 2012 University of Karlsruhe.
4  */
5
6 /**
7  * @file
8  * @brief       Provides several statistic functions for the backend.
9  * @author      Christian Wuerdig, Matthias Braun
10  */
11 #include "config.h"
12
13 #include <time.h>
14
15 #include "irnode_t.h"
16 #include "irprintf.h"
17 #include "irgwalk.h"
18 #include "irhooks.h"
19 #include "execfreq.h"
20 #include "firmstat_t.h"
21 #include "irtools.h"
22 #include "error.h"
23 #include "statev_t.h"
24
25 #include "bearch.h"
26 #include "beirg.h"
27 #include "bestat.h"
28 #include "belive_t.h"
29 #include "besched.h"
30 #include "benode.h"
31
32
33
34 typedef struct pressure_walker_env_t pressure_walker_env_t;
35 struct pressure_walker_env_t {
36         ir_graph *irg;
37         be_lv_t  *lv;
38         double    insn_count;
39         double    regpressure;
40         size_t    max_pressure;
41         const arch_register_class_t *cls;
42 };
43
44 static void check_reg_pressure_class(pressure_walker_env_t *env,
45                                      ir_node *block,
46                                      const arch_register_class_t *cls)
47 {
48         ir_graph    *irg  = env->irg;
49         ir_nodeset_t live_nodes;
50         size_t       max_live;
51
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;
56
57         sched_foreach_reverse(block, irn) {
58                 size_t cnt;
59
60                 if (is_Phi(irn))
61                         break;
62
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;
67                 env->insn_count++;
68         }
69
70         if (max_live > env->max_pressure)
71                 env->max_pressure = max_live;
72
73         stat_be_block_regpressure(irg, block, max_live, cls->name);
74         ir_nodeset_destroy(&live_nodes);
75 }
76
77 static void stat_reg_pressure_block(ir_node *block, void *data)
78 {
79         pressure_walker_env_t *env = (pressure_walker_env_t*)data;
80
81         check_reg_pressure_class(env, block, env->cls);
82 }
83
84 void be_do_stat_reg_pressure(ir_graph *irg, const arch_register_class_t *cls)
85 {
86         pressure_walker_env_t  env;
87         double                 average_pressure;
88
89         env.irg          = irg;
90         env.insn_count   = 0;
91         env.max_pressure = 0;
92         env.regpressure  = 0;
93         be_assure_live_sets(irg);
94         env.lv           = be_get_irg_liveness(irg);
95         env.cls          = cls;
96
97         /* Collect register pressure information for each block */
98         irg_block_walk_graph(irg, stat_reg_pressure_block, NULL, &env);
99
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);
103 }
104
105
106
107
108 typedef struct estimate_irg_costs_env_t {
109         double costs;
110 } estimate_irg_costs_env_t;
111
112 static void estimate_block_costs(ir_node *block, void *data)
113 {
114         estimate_irg_costs_env_t *env = (estimate_irg_costs_env_t*)data;
115         double costs = 0.0;
116
117         sched_foreach(block, node) {
118                 costs += arch_get_op_estimated_cost(node);
119         }
120
121         env->costs += costs * get_block_execfreq(block);
122 }
123
124 double be_estimate_irg_costs(ir_graph *irg)
125 {
126         estimate_irg_costs_env_t env;
127         env.costs = 0.0;
128
129         irg_block_walk_graph(irg, estimate_block_costs, NULL, &env);
130
131         return env.costs;
132 }
133
134
135
136 static void node_stat_walker(ir_node *irn, void *data)
137 {
138         be_node_stats_t *const stats = (be_node_stats_t*)data;
139
140         /* if the node is a normal phi */
141         if (is_Phi(irn)) {
142                 if (get_irn_mode(irn) == mode_M) {
143                         (*stats)[BE_STAT_MEM_PHIS]++;
144                 } else {
145                         (*stats)[BE_STAT_PHIS]++;
146                 }
147         } else if (be_is_Perm(irn)) {
148                 (*stats)[BE_STAT_PERMS]++;
149         } else if (be_is_Copy(irn)) {
150                 (*stats)[BE_STAT_COPIES]++;
151         }
152 }
153
154 void be_collect_node_stats(be_node_stats_t *new_stats, ir_graph *irg)
155 {
156         memset(new_stats, 0, sizeof(*new_stats));
157         irg_walk_graph(irg, NULL, node_stat_walker, new_stats);
158 }
159
160 void be_subtract_node_stats(be_node_stats_t *stats, be_node_stats_t *sub)
161 {
162         int i;
163         for (i = 0; i < BE_STAT_COUNT; ++i) {
164                 (*stats)[i] -= (*sub)[i];
165         }
166 }
167
168 void be_copy_node_stats(be_node_stats_t *dest, be_node_stats_t *src)
169 {
170         memcpy(dest, src, sizeof(be_node_stats_t));
171 }
172
173 static const char *get_stat_name(enum be_stat_tag_t tag)
174 {
175         switch (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");
181         }
182 }
183
184 void be_emit_node_stats(be_node_stats_t *stats, const char *prefix)
185 {
186         static char   buf[256];
187         be_stat_tag_t i;
188
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]);
192         }
193 }
194
195
196
197 static void insn_count_walker(ir_node *irn, void *data)
198 {
199         unsigned long *cnt = (unsigned long*)data;
200
201         switch (get_irn_opcode(irn)) {
202         case iro_Proj:
203         case iro_Phi:
204         case beo_Start:
205         case iro_End:
206                 break;
207         default:
208                 (*cnt)++;
209         }
210 }
211
212 unsigned long be_count_insns(ir_graph *irg)
213 {
214         unsigned long cnt = 0;
215         irg_walk_graph(irg, insn_count_walker, NULL, &cnt);
216         return cnt;
217 }
218
219 static void block_count_walker(ir_node *node, void *data)
220 {
221         unsigned long *cnt = (unsigned long*)data;
222         if (node == get_irg_end_block(get_irn_irg(node)))
223                 return;
224         (*cnt)++;
225 }
226
227 unsigned long be_count_blocks(ir_graph *irg)
228 {
229         unsigned long cnt = 0;
230         irg_block_walk_graph(irg, block_count_walker, NULL, &cnt);
231         return cnt;
232 }