Remove the unused parameter const arch_env_t *env from arch_irn_get_flags(), arch_irn...
[libfirm] / ir / be / bestat.c
1 /*
2  * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
3  *
4  * This file is part of libFirm.
5  *
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.
10  *
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.
14  *
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
17  * PURPOSE.
18  */
19
20 /**
21  * @file
22  * @brief       Provides several statistic functions for the backend.
23  * @author      Christian Wuerdig, Matthias Braun
24  * @version     $Id$
25  */
26 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include <time.h>
31
32 #include "irnode_t.h"
33 #include "irprintf.h"
34 #include "irgwalk.h"
35 #include "irhooks.h"
36 #include "execfreq.h"
37 #include "firmstat_t.h"
38 #include "irtools.h"
39 #include "statev.h"
40 #include "error.h"
41
42 #include "bearch_t.h"
43 #include "beirg_t.h"
44 #include "bestat.h"
45 #include "belive_t.h"
46 #include "besched.h"
47 #include "benode_t.h"
48
49
50
51 typedef struct pressure_walker_env_t pressure_walker_env_t;
52 struct pressure_walker_env_t {
53         be_irg_t *birg;
54         be_lv_t  *lv;
55         double    insn_count;
56         double    regpressure;
57         int       max_pressure;
58         const arch_register_class_t *cls;
59 };
60
61 static void check_reg_pressure_class(pressure_walker_env_t *env,
62                                      ir_node *block,
63                                      const arch_register_class_t *cls)
64 {
65         be_irg_t         *birg = env->birg;
66         ir_graph         *irg  = be_get_birg_irg(birg);
67         const arch_env_t *aenv = be_get_birg_arch_env(birg);
68         ir_node          *irn;
69         ir_nodeset_t      live_nodes;
70         int               max_live;
71
72         ir_nodeset_init(&live_nodes);
73         be_liveness_end_of_block(env->lv, aenv, cls, block, &live_nodes);
74         max_live = ir_nodeset_size(&live_nodes);
75         env->regpressure += max_live;
76
77         sched_foreach_reverse(block, irn) {
78                 int cnt;
79
80                 if(is_Phi(irn))
81                         break;
82
83                 be_liveness_transfer(aenv, cls, irn, &live_nodes);
84                 cnt      = ir_nodeset_size(&live_nodes);
85                 max_live = cnt < max_live ? max_live : cnt;
86                 env->regpressure += cnt;
87                 env->insn_count++;
88         }
89
90         if(max_live > env->max_pressure)
91                 env->max_pressure = max_live;
92
93         stat_be_block_regpressure(irg, block, max_live, cls->name);
94         ir_nodeset_destroy(&live_nodes);
95 }
96
97 static void stat_reg_pressure_block(ir_node *block, void *data) {
98         pressure_walker_env_t *env = data;
99
100         check_reg_pressure_class(env, block, env->cls);
101 }
102
103 void be_do_stat_reg_pressure(be_irg_t *birg, const arch_register_class_t *cls) {
104         pressure_walker_env_t  env;
105         ir_graph              *irg = be_get_birg_irg(birg);
106         double                 average_pressure;
107
108         env.birg         = birg;
109         env.insn_count   = 0;
110         env.max_pressure = 0;
111         env.regpressure  = 0;
112         be_liveness_assure_sets(be_assure_liveness(birg));
113         env.lv           = be_get_birg_liveness(birg);
114         env.cls          = cls;
115
116         /* Collect register pressure information for each block */
117         irg_block_walk_graph(irg, stat_reg_pressure_block, NULL, &env);
118
119         average_pressure = env.regpressure / env.insn_count;
120         stat_ev_emit("bechordal_average_register_pressure", average_pressure);
121         stat_ev_emit("bechordal_maximum_register_pressure", env.max_pressure);
122 }
123
124
125
126
127 typedef struct _estimate_irg_costs_env_t {
128         const arch_env_t *arch_env;
129         ir_exec_freq     *execfreqs;
130         double           costs;
131 } estimate_irg_costs_env_t;
132
133 static void estimate_block_costs(ir_node *block, void *data)
134 {
135         estimate_irg_costs_env_t *env = data;
136         ir_node *node;
137         double  costs = 0.0;
138
139         sched_foreach(block, node) {
140                 costs += arch_get_op_estimated_cost(node);
141         }
142
143         env->costs += costs * get_block_execfreq(env->execfreqs, block);
144 }
145
146 double be_estimate_irg_costs(ir_graph *irg, const arch_env_t *arch_env, ir_exec_freq *execfreqs)
147 {
148         estimate_irg_costs_env_t env;
149
150         env.arch_env  = arch_env;
151         env.execfreqs = execfreqs;
152         env.costs     = 0.0;
153
154         irg_block_walk_graph(irg, estimate_block_costs, NULL, &env);
155
156         return env.costs;
157 }
158
159
160
161 static be_node_stats_t *stats;
162
163 static void node_stat_walker(ir_node *irn, void *data)
164 {
165         (void) data;
166
167         /* if the node is a normal phi */
168         if(is_Phi(irn)) {
169                 if (get_irn_mode(irn) == mode_M) {
170                         (*stats)[BE_STAT_MEM_PHIS]++;
171                 } else {
172                         (*stats)[BE_STAT_PHIS]++;
173                 }
174         } else {
175                 arch_irn_class_t classify = arch_irn_classify(irn);
176
177                 if(classify & arch_irn_class_spill)
178                         (*stats)[BE_STAT_SPILLS]++;
179                 if(classify & arch_irn_class_reload)
180                         (*stats)[BE_STAT_RELOADS]++;
181                 if(classify & arch_irn_class_remat)
182                         (*stats)[BE_STAT_REMATS]++;
183                 if(classify & arch_irn_class_copy)
184                         (*stats)[BE_STAT_COPIES]++;
185                 if(classify & arch_irn_class_perm)
186                         (*stats)[BE_STAT_PERMS]++;
187         }
188 }
189
190 void be_collect_node_stats(be_node_stats_t *new_stats, be_irg_t *birg)
191 {
192         stats = new_stats;
193
194         memset(stats, 0, sizeof(*stats));
195         irg_walk_graph(birg->irg, NULL, node_stat_walker, NULL);
196 }
197
198 void be_subtract_node_stats(be_node_stats_t *stats, be_node_stats_t *sub)
199 {
200         int i;
201         for (i = 0; i < BE_STAT_COUNT; ++i) {
202                 (*stats)[i] -= (*sub)[i];
203         }
204 }
205
206 void be_copy_node_stats(be_node_stats_t *dest, be_node_stats_t *src)
207 {
208         memcpy(dest, src, sizeof(be_node_stats_t));
209 }
210
211 static const char *get_stat_name(enum be_stat_tag_t tag)
212 {
213         switch(tag) {
214         case BE_STAT_PHIS:     return "phis";
215         case BE_STAT_MEM_PHIS: return "mem_phis";
216         case BE_STAT_COPIES:   return "copies";
217         case BE_STAT_PERMS:    return "perms";
218         case BE_STAT_SPILLS:   return "spills";
219         case BE_STAT_RELOADS:  return "reloads";
220         case BE_STAT_REMATS:   return "remats";
221         default:               panic("unknown stat tag found");
222         }
223 }
224
225 void be_emit_node_stats(be_node_stats_t *stats, const char *prefix)
226 {
227         static char buf[256];
228         int         i;
229
230         for (i = 0; i < BE_STAT_COUNT; ++i) {
231                 snprintf(buf, sizeof(buf), "%s%s", prefix, get_stat_name(i));
232                 stat_ev_dbl(buf, (*stats)[i]);
233         }
234 }
235
236
237
238 static void insn_count_walker(ir_node *irn, void *data)
239 {
240         unsigned long *cnt = data;
241
242         switch(get_irn_opcode(irn)) {
243         case iro_Proj:
244         case iro_Phi:
245         case iro_Start:
246         case iro_End:
247                 break;
248         default:
249                 (*cnt)++;
250         }
251 }
252
253 unsigned long be_count_insns(ir_graph *irg)
254 {
255         unsigned long cnt = 0;
256         irg_walk_graph(irg, insn_count_walker, NULL, &cnt);
257         return cnt;
258 }
259
260 static void block_count_walker(ir_node *node, void *data)
261 {
262         unsigned long *cnt = data;
263         if (node == get_irg_end_block(current_ir_graph))
264                 return;
265         (*cnt)++;
266 }
267
268 unsigned long be_count_blocks(ir_graph *irg)
269 {
270         unsigned long cnt = 0;
271         irg_block_walk_graph(irg, block_count_walker, NULL, &cnt);
272         return cnt;
273 }