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