2a08fc44fce9fd62f518829dc18e026da8897b73
[libfirm] / ir / be / bepeephole.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       Peephole optimisation framework keeps track of which registers contain which values
23  * @author      Matthias Braun
24  */
25 #include "config.h"
26
27 #include "array_t.h"
28 #include "bepeephole.h"
29
30 #include "iredges_t.h"
31 #include "irgwalk.h"
32 #include "irprintf.h"
33 #include "ircons.h"
34 #include "irgmod.h"
35 #include "heights.h"
36 #include "error.h"
37
38 #include "beirg.h"
39 #include "belive_t.h"
40 #include "bearch.h"
41 #include "beintlive_t.h"
42 #include "benode.h"
43 #include "besched.h"
44 #include "bemodule.h"
45
46 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
47
48 static const arch_env_t *arch_env;
49 static be_lv_t          *lv;
50 static ir_node          *current_node;
51 ir_node                **register_values;
52
53 static void clear_reg_value(ir_node *node)
54 {
55         const arch_register_t *reg;
56         unsigned               reg_idx;
57
58         if (!mode_is_data(get_irn_mode(node)))
59                 return;
60
61         reg = arch_get_irn_register(node);
62         if (reg == NULL) {
63                 panic("No register assigned at %+F", node);
64         }
65         if (reg->type & arch_register_type_virtual)
66                 return;
67         reg_idx = reg->global_index;
68
69         DB((dbg, LEVEL_1, "Clear Register %s\n", reg->name));
70         register_values[reg_idx] = NULL;
71 }
72
73 static void set_reg_value(ir_node *node)
74 {
75         const arch_register_t *reg;
76         unsigned               reg_idx;
77
78         if (!mode_is_data(get_irn_mode(node)))
79                 return;
80
81         reg = arch_get_irn_register(node);
82         if (reg == NULL) {
83                 panic("No register assigned at %+F", node);
84         }
85         if (reg->type & arch_register_type_virtual)
86                 return;
87         reg_idx = reg->global_index;
88
89         DB((dbg, LEVEL_1, "Set Register %s: %+F\n", reg->name, node));
90         register_values[reg_idx] = node;
91 }
92
93 static void clear_defs(ir_node *node)
94 {
95         /* clear values defined */
96         if (get_irn_mode(node) == mode_T) {
97                 foreach_out_edge(node, edge) {
98                         ir_node *proj = get_edge_src_irn(edge);
99                         clear_reg_value(proj);
100                 }
101         } else {
102                 clear_reg_value(node);
103         }
104 }
105
106 static void set_uses(ir_node *node)
107 {
108         int i, arity;
109
110         /* set values used */
111         arity = get_irn_arity(node);
112         for (i = 0; i < arity; ++i) {
113                 ir_node *in = get_irn_n(node, i);
114                 set_reg_value(in);
115         }
116 }
117
118 void be_peephole_new_node(ir_node * nw)
119 {
120         be_liveness_introduce(lv, nw);
121 }
122
123 /**
124  * must be called from peephole optimisations before a node will be killed
125  * and its users will be redirected to new_node.
126  * so bepeephole can update its internal state.
127  *
128  * Note: killing a node and rewiring is only allowed if new_node produces
129  * the same registers as old_node.
130  */
131 static void be_peephole_before_exchange(const ir_node *old_node,
132                                         ir_node *new_node)
133 {
134         const arch_register_t *reg;
135         unsigned               reg_idx;
136         bool                   old_is_current = false;
137
138         DB((dbg, LEVEL_1, "About to exchange and kill %+F with %+F\n", old_node, new_node));
139
140         assert(sched_is_scheduled(skip_Proj_const(old_node)));
141         assert(sched_is_scheduled(skip_Proj(new_node)));
142
143         if (current_node == old_node) {
144                 old_is_current = true;
145
146                 /* next node to be processed will be killed. Its scheduling predecessor
147                  * must be processed next. */
148                 current_node = sched_next(current_node);
149                 assert (!is_Bad(current_node));
150
151                 /* we can't handle liveness updates correctly when exchange current node
152                  * with something behind it */
153                 assert(value_dominates(skip_Proj(new_node), skip_Proj_const(old_node)));
154         }
155
156         if (!mode_is_data(get_irn_mode(old_node)))
157                 return;
158
159         reg = arch_get_irn_register(old_node);
160         if (reg == NULL) {
161                 panic("No register assigned at %+F", old_node);
162         }
163         assert(reg == arch_get_irn_register(new_node) &&
164               "KILLING a node and replacing by different register is not allowed");
165
166         reg_idx = reg->global_index;
167         if (register_values[reg_idx] == old_node || old_is_current) {
168                 register_values[reg_idx] = new_node;
169         }
170
171         be_liveness_remove(lv, old_node);
172 }
173
174 void be_peephole_exchange(ir_node *old, ir_node *nw)
175 {
176         be_peephole_before_exchange(old, nw);
177         sched_remove(old);
178         exchange(old, nw);
179         be_peephole_new_node(nw);
180 }
181
182 /**
183  * block-walker: run peephole optimization on the given block.
184  */
185 static void process_block(ir_node *block, void *data)
186 {
187         int l;
188         (void) data;
189
190         /* construct initial register assignment */
191         memset(register_values, 0, sizeof(ir_node*) * arch_env->n_registers);
192
193         assert(lv->sets_valid && "live sets must be computed");
194         DB((dbg, LEVEL_1, "\nProcessing block %+F (from end)\n", block));
195         be_lv_foreach(lv, block, be_lv_state_end, l) {
196                 ir_node *node = be_lv_get_irn(lv, block, l);
197                 set_reg_value(node);
198         }
199         DB((dbg, LEVEL_1, "\nstart processing\n"));
200
201         /* walk the block from last insn to the first */
202         current_node = sched_last(block);
203         for ( ; !sched_is_begin(current_node);
204                         current_node = sched_prev(current_node)) {
205                 ir_op             *op;
206                 peephole_opt_func  peephole_node;
207
208                 assert(!is_Bad(current_node));
209                 if (is_Phi(current_node))
210                         break;
211
212                 clear_defs(current_node);
213                 set_uses(current_node);
214
215                 op            = get_irn_op(current_node);
216                 peephole_node = (peephole_opt_func)op->ops.generic;
217                 if (peephole_node == NULL)
218                         continue;
219
220                 DB((dbg, LEVEL_2, "optimize %+F\n", current_node));
221                 peephole_node(current_node);
222                 assert(!is_Bad(current_node));
223         }
224 }
225
226 /**
227  * Check whether the node has only one user.  Explicitly ignore the anchor.
228  */
229 bool be_has_only_one_user(ir_node *node)
230 {
231         int n = get_irn_n_edges(node);
232         int n_users;
233
234         if (n <= 1)
235                 return 1;
236
237         n_users = 0;
238         foreach_out_edge(node, edge) {
239                 ir_node *src = get_edge_src_irn(edge);
240                 /* ignore anchor and keep-alive edges */
241                 if (is_Anchor(src) || is_End(src))
242                         continue;
243                 n_users++;
244         }
245
246         return n_users == 1;
247 }
248
249 bool be_can_move_before(ir_heights_t *heights, const ir_node *node,
250                         const ir_node *before)
251 {
252         int      node_arity = get_irn_arity(node);
253         ir_node *schedpoint = sched_next(node);
254
255         while (schedpoint != before) {
256                 int      i;
257                 unsigned n_outs = arch_get_irn_n_outs(schedpoint);
258
259                 /* the node must not use our computed values */
260                 if (heights_reachable_in_block(heights, schedpoint, node))
261                         return false;
262
263                 /* the node must not overwrite registers of our inputs */
264                 for (i = 0; i < node_arity; ++i) {
265                         ir_node                   *in  = get_irn_n(node, i);
266                         const arch_register_t     *reg = arch_get_irn_register(in);
267                         const arch_register_req_t *in_req
268                                 = arch_get_irn_register_req_in(node, i);
269                         unsigned                   o;
270                         if (reg == NULL)
271                                 continue;
272                         for (o = 0; o < n_outs; ++o) {
273                                 const arch_register_t *outreg
274                                         = arch_get_irn_register_out(schedpoint, o);
275                                 const arch_register_req_t *outreq
276                                         = arch_get_irn_register_req_out(schedpoint, o);
277                                 if (outreg == NULL)
278                                         continue;
279                                 if (outreg->global_index >= reg->global_index
280                                         && outreg->global_index
281                                            < (unsigned)reg->global_index + in_req->width)
282                                         return false;
283                                 if (reg->global_index >= outreg->global_index
284                                         && reg->global_index
285                                            < (unsigned)outreg->global_index + outreq->width)
286                                         return false;
287                         }
288                 }
289
290                 schedpoint = sched_next(schedpoint);
291         }
292         return true;
293 }
294
295 /*
296  * Tries to optimize a beIncSP node with its previous IncSP node.
297  * Must be run from a be_peephole_opt() context.
298  */
299 ir_node *be_peephole_IncSP_IncSP(ir_node *node)
300 {
301         int      pred_offs;
302         int      curr_offs;
303         int      offs;
304         ir_node *pred = be_get_IncSP_pred(node);
305
306         if (!be_is_IncSP(pred))
307                 return node;
308
309         if (!be_has_only_one_user(pred))
310                 return node;
311
312         pred_offs = be_get_IncSP_offset(pred);
313         curr_offs = be_get_IncSP_offset(node);
314         offs = curr_offs + pred_offs;
315
316         /* add node offset to pred and remove our IncSP */
317         be_set_IncSP_offset(pred, offs);
318
319         be_peephole_exchange(node, pred);
320         return pred;
321 }
322
323 void be_peephole_opt(ir_graph *irg)
324 {
325 #if 0
326         /* we sometimes find BadE nodes in float apps like optest_float.c or
327          * kahansum.c for example... */
328         be_invalidate_live_sets(irg);
329 #endif
330         be_assure_live_sets(irg);
331
332         arch_env = be_get_irg_arch_env(irg);
333         lv       = be_get_irg_liveness(irg);
334
335         register_values = XMALLOCN(ir_node*, arch_env->n_registers);
336
337         irg_block_walk_graph(irg, process_block, NULL, NULL);
338
339         xfree(register_values);
340 }
341
342 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_peephole)
343 void be_init_peephole(void)
344 {
345         FIRM_DBG_REGISTER(dbg, "firm.be.peephole");
346 }