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