Replace be_peephole_before_exchange()+sched_remove()+exchange()+be_peephole_new_node...
[libfirm] / ir / be / bepeephole.c
1 /*
2  * Copyright (C) 1995-2008 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 #ifdef HAVE_CONFIG_H
27 #include "config.h"
28 #endif
29
30 #include "bepeephole.h"
31
32 #include "iredges_t.h"
33 #include "irgwalk.h"
34 #include "irprintf.h"
35 #include "irgmod.h"
36 #include "error.h"
37
38 #include "beirg_t.h"
39 #include "belive_t.h"
40 #include "bearch_t.h"
41 #include "benode_t.h"
42 #include "besched_t.h"
43 #include "bemodule.h"
44
45 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
46
47 static const arch_env_t *arch_env;
48 static be_lv_t          *lv;
49 static ir_node          *current_node;
50 static ir_node          *prev_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(arch_env, node);
64         if(reg == NULL) {
65                 panic("No register assigned at %+F\n", node);
66         }
67         if(arch_register_type_is(reg, 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         DBG((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(arch_env, node);
89         if(reg == NULL) {
90                 panic("No register assigned at %+F\n", node);
91         }
92         if(arch_register_type_is(reg, 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         DBG((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 *const 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 it's internal state.
137  *
138  * Note: killing a node and rewiring os only allowed if new_node produces
139  * the same registers as old_node.
140  */
141 void be_peephole_before_exchange(const ir_node *old_node, ir_node *new_node)
142 {
143         const arch_register_t       *reg;
144         const arch_register_class_t *cls;
145         unsigned                     reg_idx;
146         unsigned                     cls_idx;
147
148         DBG((dbg, LEVEL_1, "About to exchange and kill %+F with %+F\n", old_node, new_node));
149
150         if (current_node == old_node) {
151           /* next node to be processed will be killed. Its scheduling predecessor
152            * must be processed next. */
153                 prev_node = sched_prev(current_node);
154         }
155
156         if (!mode_is_data(get_irn_mode(old_node)))
157                 return;
158
159         reg = arch_get_irn_register(arch_env, old_node);
160         if (reg == NULL) {
161                 panic("No register assigned at %+F\n", old_node);
162         }
163         assert(reg == arch_get_irn_register(arch_env, new_node) &&
164               "KILLING a node and replacing by different register is not allowed");
165
166         cls     = arch_register_get_class(reg);
167         reg_idx = arch_register_get_index(reg);
168         cls_idx = arch_register_class_index(cls);
169
170         if (register_values[cls_idx][reg_idx] == old_node) {
171                 register_values[cls_idx][reg_idx] = new_node;
172         }
173
174         be_liveness_remove(lv, old_node);
175 }
176
177 void be_peephole_exchange(ir_node *const old, ir_node *const nw)
178 {
179         be_peephole_before_exchange(old, nw);
180         sched_remove(old);
181         exchange(old, nw);
182         be_peephole_new_node(nw);
183 }
184
185 /**
186  * block-walker: run peephole optimization on the given block.
187  */
188 static void process_block(ir_node *block, void *data)
189 {
190         unsigned n_classes;
191         unsigned i;
192         int l;
193         (void) data;
194
195         /* construct initial register assignment */
196         n_classes = arch_env_get_n_reg_class(arch_env);
197         for(i = 0; i < n_classes; ++i) {
198                 const arch_register_class_t *cls    = arch_env_get_reg_class(arch_env, i);
199                 unsigned                     n_regs = arch_register_class_n_regs(cls);
200                 memset(register_values[i], 0, sizeof(ir_node*) * n_regs);
201         }
202
203         assert(lv->nodes && "live sets must be computed");
204         DBG((dbg, LEVEL_1, "\nProcessing block %+F (from end)\n", block));
205         be_lv_foreach(lv, block, be_lv_state_end, l) {
206                 ir_node *node = be_lv_get_irn(lv, block, l);
207                 set_reg_value(node);
208         }
209         DBG((dbg, LEVEL_1, "\nstart processing\n"));
210
211         /* walk the block from last insn to the first */
212         current_node = sched_last(block);
213         for( ; !sched_is_begin(current_node);
214                 current_node = prev_node != NULL ? prev_node : sched_prev(current_node)) {
215                 ir_op             *op;
216                 ir_node           *last;
217                 peephole_opt_func  peephole_node;
218
219                 assert(!is_Bad(current_node));
220                 prev_node = NULL;
221                 if (is_Phi(current_node))
222                         break;
223
224                 clear_defs(current_node);
225                 set_uses(current_node);
226
227                 op   = get_irn_op(current_node);
228                 peephole_node = (peephole_opt_func)op->ops.generic;
229                 if (peephole_node == NULL)
230                         continue;
231
232                 last = current_node;
233                 peephole_node(current_node);
234                 /* was the current node replaced? */
235                 if (current_node != last)
236                         set_uses(current_node);
237         }
238 }
239
240 /**
241  * Walk through the block schedule and skip all barrier nodes.
242  */
243 static void skip_barrier(ir_node *ret_blk, ir_graph *irg) {
244         ir_node *irn;
245
246         sched_foreach_reverse(ret_blk, irn) {
247                 if (be_is_Barrier(irn)) {
248                         const ir_edge_t *edge, *next;
249
250                         foreach_out_edge_safe(irn, edge, next) {
251                                 ir_node *proj = get_edge_src_irn(edge);
252                                 int      pn   = (int)get_Proj_proj(proj);
253                                 ir_node *pred = get_irn_n(irn, pn);
254
255                                 edges_reroute_kind(proj, pred, EDGE_KIND_NORMAL, irg);
256                                 edges_reroute_kind(proj, pred, EDGE_KIND_DEP, irg);
257                         }
258                         sched_remove(irn);
259                         kill_node(irn);
260                         break;
261                 }
262         }
263 }
264
265 /**
266  * Kill the Barrier nodes for better peephole optimization.
267  */
268 static void     kill_barriers(ir_graph *irg) {
269         ir_node *end_blk = get_irg_end_block(irg);
270         ir_node *start_blk;
271         int i;
272
273         /* skip the barrier on all return blocks */
274         for (i = get_Block_n_cfgpreds(end_blk) - 1; i >= 0; --i) {
275                 ir_node *be_ret = get_Block_cfgpred(end_blk, i);
276                 ir_node *ret_blk = get_nodes_block(be_ret);
277
278                 skip_barrier(ret_blk, irg);
279         }
280
281         /* skip the barrier on the start block */
282         start_blk = get_irg_start_block(irg);
283         skip_barrier(start_blk, irg);
284 }
285
286 /*
287  * Tries to optimize a beIncSp node with it's previous IncSP node.
288  * Must be run from a be_peephole_opt() context.
289  */
290 ir_node *be_peephole_IncSP_IncSP(ir_node *node)
291 {
292         int      pred_offs;
293         int      curr_offs;
294         int      offs;
295         ir_node *pred = be_get_IncSP_pred(node);
296
297         if (!be_is_IncSP(pred))
298                 return node;
299
300         if (get_irn_n_edges(pred) > 1)
301                 return node;
302
303         pred_offs = be_get_IncSP_offset(pred);
304         curr_offs = be_get_IncSP_offset(node);
305
306         if (pred_offs == BE_STACK_FRAME_SIZE_EXPAND) {
307                 if (curr_offs != BE_STACK_FRAME_SIZE_SHRINK) {
308                         return node;
309                 }
310                 offs = 0;
311         } else if (pred_offs == BE_STACK_FRAME_SIZE_SHRINK) {
312                 if (curr_offs != BE_STACK_FRAME_SIZE_EXPAND) {
313                         return node;
314                 }
315                 offs = 0;
316         } else if (curr_offs == BE_STACK_FRAME_SIZE_EXPAND ||
317                    curr_offs == BE_STACK_FRAME_SIZE_SHRINK) {
318                 return node;
319         } else {
320                 offs = curr_offs + pred_offs;
321         }
322
323         /* add node offset to pred and remove our IncSP */
324         be_set_IncSP_offset(pred, offs);
325
326         be_peephole_exchange(node, pred);
327         return pred;
328 }
329
330 void be_peephole_opt(be_irg_t *birg)
331 {
332         ir_graph   *irg = be_get_birg_irg(birg);
333         unsigned n_classes;
334         unsigned i;
335
336         /* barrier nodes are used for register allocations. They hinders
337          * peephole optimizations, so remove them here. */
338         kill_barriers(irg);
339
340         /* we sometimes find BadE nodes in float apps like optest_float.c or
341          * kahansum.c for example... */
342         be_liveness_invalidate(birg->lv);
343         be_liveness_assure_sets(be_assure_liveness(birg));
344
345         arch_env = be_get_birg_arch_env(birg);
346         lv       = be_get_birg_liveness(birg);
347
348         n_classes = arch_env_get_n_reg_class(arch_env);
349         register_values = alloca(sizeof(register_values[0]) * n_classes);
350         for(i = 0; i < n_classes; ++i) {
351                 const arch_register_class_t *cls    = arch_env_get_reg_class(arch_env, i);
352                 unsigned                     n_regs = arch_register_class_n_regs(cls);
353                 register_values[i] = alloca(sizeof(ir_node*) * n_regs);
354         }
355
356         irg_block_walk_graph(irg, process_block, NULL, NULL);
357 }
358
359 void be_peephole_init(void)
360 {
361         clear_irp_opcodes_generic_func();
362 }
363
364 void be_init_peephole(void)
365 {
366         FIRM_DBG_REGISTER(dbg, "firm.be.peephole");
367 }
368
369 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_spillbelady);