Also track extended live ranges.
[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(new_node));
153         assert(value_dominates(new_node, old_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
164         if (!mode_is_data(get_irn_mode(old_node)))
165                 return;
166
167         reg = arch_get_irn_register(old_node);
168         if (reg == NULL) {
169                 panic("No register assigned at %+F", old_node);
170         }
171         assert(reg == arch_get_irn_register(new_node) &&
172               "KILLING a node and replacing by different register is not allowed");
173
174         cls     = arch_register_get_class(reg);
175         reg_idx = arch_register_get_index(reg);
176         cls_idx = arch_register_class_index(cls);
177
178         if (register_values[cls_idx][reg_idx] == old_node || old_is_current) {
179                 register_values[cls_idx][reg_idx] = new_node;
180         }
181
182         be_liveness_remove(lv, old_node);
183 }
184
185 void be_peephole_exchange(ir_node *old, ir_node *nw)
186 {
187         be_peephole_before_exchange(old, nw);
188         sched_remove(old);
189         exchange(old, nw);
190         be_peephole_new_node(nw);
191 }
192
193 /**
194  * block-walker: run peephole optimization on the given block.
195  */
196 static void process_block(ir_node *block, void *data)
197 {
198         unsigned n_classes;
199         unsigned i;
200         int l;
201         (void) data;
202
203         /* construct initial register assignment */
204         n_classes = arch_env->n_register_classes;
205         for (i = 0; i < n_classes; ++i) {
206                 const arch_register_class_t *cls    = &arch_env->register_classes[i];
207                 unsigned                     n_regs = arch_register_class_n_regs(cls);
208                 memset(register_values[i], 0, sizeof(ir_node*) * n_regs);
209         }
210
211         assert(lv->nodes && "live sets must be computed");
212         DB((dbg, LEVEL_1, "\nProcessing block %+F (from end)\n", block));
213         be_lv_foreach(lv, block, be_lv_state_end, l) {
214                 ir_node *node = be_lv_get_irn(lv, block, l);
215                 set_reg_value(node);
216         }
217         DB((dbg, LEVEL_1, "\nstart processing\n"));
218
219         /* walk the block from last insn to the first */
220         current_node = sched_last(block);
221         for ( ; !sched_is_begin(current_node);
222                         current_node = sched_prev(current_node)) {
223                 ir_op             *op;
224                 peephole_opt_func  peephole_node;
225
226                 assert(!is_Bad(current_node));
227                 if (is_Phi(current_node))
228                         break;
229
230                 clear_defs(current_node);
231                 set_uses(current_node);
232
233                 op            = get_irn_op(current_node);
234                 peephole_node = (peephole_opt_func)op->ops.generic;
235                 if (peephole_node == NULL)
236                         continue;
237
238                 DB((dbg, LEVEL_2, "optimize %+F\n", current_node));
239                 peephole_node(current_node);
240                 assert(!is_Bad(current_node));
241         }
242 }
243
244 /**
245  * Check whether the node has only one user.  Explicitly ignore the anchor.
246  */
247 bool be_has_only_one_user(ir_node *node)
248 {
249         int              n = get_irn_n_edges(node);
250         int              n_users;
251         const ir_edge_t *edge;
252
253         if (n <= 1)
254                 return 1;
255
256         n_users = 0;
257         foreach_out_edge(node, edge) {
258                 ir_node *src = get_edge_src_irn(edge);
259                 /* ignore anchor and keep-alive edges */
260                 if (is_Anchor(src) || is_End(src))
261                         continue;
262                 n_users++;
263         }
264
265         return n_users == 1;
266 }
267
268 /*
269  * Tries to optimize a beIncSP node with its previous IncSP node.
270  * Must be run from a be_peephole_opt() context.
271  */
272 ir_node *be_peephole_IncSP_IncSP(ir_node *node)
273 {
274         int      pred_offs;
275         int      curr_offs;
276         int      offs;
277         ir_node *pred = be_get_IncSP_pred(node);
278
279         if (!be_is_IncSP(pred))
280                 return node;
281
282         if (!be_has_only_one_user(pred))
283                 return node;
284
285         pred_offs = be_get_IncSP_offset(pred);
286         curr_offs = be_get_IncSP_offset(node);
287         offs = curr_offs + pred_offs;
288
289         /* add node offset to pred and remove our IncSP */
290         be_set_IncSP_offset(pred, offs);
291
292         be_peephole_exchange(node, pred);
293         return pred;
294 }
295
296 void be_peephole_opt(ir_graph *irg)
297 {
298         unsigned  n_classes;
299         unsigned  i;
300
301         /* we sometimes find BadE nodes in float apps like optest_float.c or
302          * kahansum.c for example... */
303         be_liveness_invalidate(be_get_irg_liveness(irg));
304         be_liveness_assure_sets(be_assure_liveness(irg));
305
306         arch_env = be_get_irg_arch_env(irg);
307         lv       = be_get_irg_liveness(irg);
308
309         n_classes = arch_env->n_register_classes;
310         register_values = XMALLOCN(ir_node**, n_classes);
311         for (i = 0; i < n_classes; ++i) {
312                 const arch_register_class_t *cls    = &arch_env->register_classes[i];
313                 unsigned                     n_regs = arch_register_class_n_regs(cls);
314                 register_values[i] = XMALLOCN(ir_node*, n_regs);
315         }
316
317         irg_block_walk_graph(irg, process_block, NULL, NULL);
318
319         for (i = 0; i < n_classes; ++i) {
320                 xfree(register_values[i]);
321         }
322         xfree(register_values);
323 }
324
325 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_peephole)
326 void be_init_peephole(void)
327 {
328         FIRM_DBG_REGISTER(dbg, "firm.be.peephole");
329 }