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