sparc: Fix gen_Const on 64-bit machines.
[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_nodeset_t live_nodes;
64         size_t       max_live;
65
66         ir_nodeset_init(&live_nodes);
67         be_liveness_end_of_block(env->lv, cls, block, &live_nodes);
68         max_live = ir_nodeset_size(&live_nodes);
69         env->regpressure += max_live;
70
71         sched_foreach_reverse(block, irn) {
72                 size_t cnt;
73
74                 if (is_Phi(irn))
75                         break;
76
77                 be_liveness_transfer(cls, irn, &live_nodes);
78                 cnt      = ir_nodeset_size(&live_nodes);
79                 max_live = cnt < max_live ? max_live : cnt;
80                 env->regpressure += cnt;
81                 env->insn_count++;
82         }
83
84         if (max_live > env->max_pressure)
85                 env->max_pressure = max_live;
86
87         stat_be_block_regpressure(irg, block, max_live, cls->name);
88         ir_nodeset_destroy(&live_nodes);
89 }
90
91 static void stat_reg_pressure_block(ir_node *block, void *data)
92 {
93         pressure_walker_env_t *env = (pressure_walker_env_t*)data;
94
95         check_reg_pressure_class(env, block, env->cls);
96 }
97
98 void be_do_stat_reg_pressure(ir_graph *irg, const arch_register_class_t *cls)
99 {
100         pressure_walker_env_t  env;
101         double                 average_pressure;
102
103         env.irg          = irg;
104         env.insn_count   = 0;
105         env.max_pressure = 0;
106         env.regpressure  = 0;
107         be_assure_live_sets(irg);
108         env.lv           = be_get_irg_liveness(irg);
109         env.cls          = cls;
110
111         /* Collect register pressure information for each block */
112         irg_block_walk_graph(irg, stat_reg_pressure_block, NULL, &env);
113
114         average_pressure = env.regpressure / env.insn_count;
115         stat_ev_emit("bechordal_average_register_pressure", average_pressure);
116         stat_ev_emit("bechordal_maximum_register_pressure", env.max_pressure);
117 }
118
119
120
121
122 typedef struct estimate_irg_costs_env_t {
123         ir_exec_freq     *execfreqs;
124         double           costs;
125 } estimate_irg_costs_env_t;
126
127 static void estimate_block_costs(ir_node *block, void *data)
128 {
129         estimate_irg_costs_env_t *env = (estimate_irg_costs_env_t*)data;
130         double costs = 0.0;
131
132         sched_foreach(block, node) {
133                 costs += arch_get_op_estimated_cost(node);
134         }
135
136         env->costs += costs * get_block_execfreq(env->execfreqs, block);
137 }
138
139 double be_estimate_irg_costs(ir_graph *irg, ir_exec_freq *execfreqs)
140 {
141         estimate_irg_costs_env_t env;
142
143         env.execfreqs = execfreqs;
144         env.costs     = 0.0;
145
146         irg_block_walk_graph(irg, estimate_block_costs, NULL, &env);
147
148         return env.costs;
149 }
150
151
152
153 static void node_stat_walker(ir_node *irn, void *data)
154 {
155         be_node_stats_t *const stats = (be_node_stats_t*)data;
156
157         /* if the node is a normal phi */
158         if (is_Phi(irn)) {
159                 if (get_irn_mode(irn) == mode_M) {
160                         (*stats)[BE_STAT_MEM_PHIS]++;
161                 } else {
162                         (*stats)[BE_STAT_PHIS]++;
163                 }
164         } else if (be_is_Perm(irn)) {
165                 (*stats)[BE_STAT_PERMS]++;
166         } else if (be_is_Copy(irn)) {
167                 (*stats)[BE_STAT_COPIES]++;
168         }
169 }
170
171 void be_collect_node_stats(be_node_stats_t *new_stats, ir_graph *irg)
172 {
173         memset(new_stats, 0, sizeof(*new_stats));
174         irg_walk_graph(irg, NULL, node_stat_walker, new_stats);
175 }
176
177 void be_subtract_node_stats(be_node_stats_t *stats, be_node_stats_t *sub)
178 {
179         int i;
180         for (i = 0; i < BE_STAT_COUNT; ++i) {
181                 (*stats)[i] -= (*sub)[i];
182         }
183 }
184
185 void be_copy_node_stats(be_node_stats_t *dest, be_node_stats_t *src)
186 {
187         memcpy(dest, src, sizeof(be_node_stats_t));
188 }
189
190 static const char *get_stat_name(enum be_stat_tag_t tag)
191 {
192         switch (tag) {
193         case BE_STAT_PHIS:     return "phis";
194         case BE_STAT_MEM_PHIS: return "mem_phis";
195         case BE_STAT_COPIES:   return "copies";
196         case BE_STAT_PERMS:    return "perms";
197         default:               panic("unknown stat tag found");
198         }
199 }
200
201 void be_emit_node_stats(be_node_stats_t *stats, const char *prefix)
202 {
203         static char   buf[256];
204         be_stat_tag_t i;
205
206         for (i = BE_STAT_FIRST; i < BE_STAT_COUNT; ++i) {
207                 snprintf(buf, sizeof(buf), "%s%s", prefix, get_stat_name(i));
208                 stat_ev_dbl(buf, (*stats)[i]);
209         }
210 }
211
212
213
214 static void insn_count_walker(ir_node *irn, void *data)
215 {
216         unsigned long *cnt = (unsigned long*)data;
217
218         switch (get_irn_opcode(irn)) {
219         case iro_Proj:
220         case iro_Phi:
221         case beo_Start:
222         case iro_End:
223                 break;
224         default:
225                 (*cnt)++;
226         }
227 }
228
229 unsigned long be_count_insns(ir_graph *irg)
230 {
231         unsigned long cnt = 0;
232         irg_walk_graph(irg, insn_count_walker, NULL, &cnt);
233         return cnt;
234 }
235
236 static void block_count_walker(ir_node *node, void *data)
237 {
238         unsigned long *cnt = (unsigned long*)data;
239         if (node == get_irg_end_block(get_irn_irg(node)))
240                 return;
241         (*cnt)++;
242 }
243
244 unsigned long be_count_blocks(ir_graph *irg)
245 {
246         unsigned long cnt = 0;
247         irg_block_walk_graph(irg, block_count_walker, NULL, &cnt);
248         return cnt;
249 }