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