combos constant dataflow analysis has to be consistent with the localopt; this should...
[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 #include "config.h"
27
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 "error.h"
36
37 #include "beirg.h"
38 #include "belive_t.h"
39 #include "bearch.h"
40 #include "benode.h"
41 #include "besched.h"
42 #include "bemodule.h"
43
44 DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)
45
46 static const arch_env_t *arch_env;
47 static be_lv_t          *lv;
48 static ir_node          *current_node;
49 ir_node               ***register_values;
50
51 static void clear_reg_value(ir_node *node)
52 {
53         const arch_register_t       *reg;
54         const arch_register_class_t *cls;
55         unsigned                     reg_idx;
56         unsigned                     cls_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 (arch_register_type_is(reg, virtual))
66                 return;
67         cls     = arch_register_get_class(reg);
68         reg_idx = arch_register_get_index(reg);
69         cls_idx = arch_register_class_index(cls);
70
71         //assert(register_values[cls_idx][reg_idx] != NULL);
72         DB((dbg, LEVEL_1, "Clear Register %s\n", reg->name));
73         register_values[cls_idx][reg_idx] = NULL;
74 }
75
76 static void set_reg_value(ir_node *node)
77 {
78         const arch_register_t       *reg;
79         const arch_register_class_t *cls;
80         unsigned                     reg_idx;
81         unsigned                     cls_idx;
82
83         if (!mode_is_data(get_irn_mode(node)))
84                 return;
85
86         reg = arch_get_irn_register(node);
87         if (reg == NULL) {
88                 panic("No register assigned at %+F", node);
89         }
90         if (arch_register_type_is(reg, virtual))
91                 return;
92         cls     = arch_register_get_class(reg);
93         reg_idx = arch_register_get_index(reg);
94         cls_idx = arch_register_class_index(cls);
95
96         DB((dbg, LEVEL_1, "Set Register %s: %+F\n", reg->name, node));
97         register_values[cls_idx][reg_idx] = node;
98 }
99
100 static void clear_defs(ir_node *node)
101 {
102         /* clear values defined */
103         if (get_irn_mode(node) == mode_T) {
104                 const ir_edge_t *edge;
105                 foreach_out_edge(node, edge) {
106                         ir_node *proj = get_edge_src_irn(edge);
107                         clear_reg_value(proj);
108                 }
109         } else {
110                 clear_reg_value(node);
111         }
112 }
113
114 static void set_uses(ir_node *node)
115 {
116         int i, arity;
117
118         /* set values used */
119         arity = get_irn_arity(node);
120         for (i = 0; i < arity; ++i) {
121                 ir_node *in = get_irn_n(node, i);
122                 set_reg_value(in);
123         }
124 }
125
126 void be_peephole_new_node(ir_node * nw)
127 {
128         be_liveness_introduce(lv, nw);
129 }
130
131 /**
132  * must be called from peephole optimisations before a node will be killed
133  * and its users will be redirected to new_node.
134  * so bepeephole can update it's internal state.
135  *
136  * Note: killing a node and rewiring os only allowed if new_node produces
137  * the same registers as old_node.
138  */
139 static void be_peephole_before_exchange(const ir_node *old_node,
140                                         ir_node *new_node)
141 {
142         const arch_register_t       *reg;
143         const arch_register_class_t *cls;
144         unsigned                     reg_idx;
145         unsigned                     cls_idx;
146
147         DB((dbg, LEVEL_1, "About to exchange and kill %+F with %+F\n", old_node, new_node));
148
149         if (current_node == old_node) {
150                 /* next node to be processed will be killed. Its scheduling predecessor
151                  * must be processed next. */
152                 current_node = sched_next(current_node);
153                 assert (!is_Bad(current_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         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 *old, ir_node *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         DB((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         DB((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 = sched_prev(current_node)) {
215                 ir_op             *op;
216                 peephole_opt_func  peephole_node;
217
218                 assert(!is_Bad(current_node));
219                 if (is_Phi(current_node))
220                         break;
221
222                 clear_defs(current_node);
223                 set_uses(current_node);
224
225                 op            = get_irn_op(current_node);
226                 peephole_node = (peephole_opt_func)op->ops.generic;
227                 if (peephole_node == NULL)
228                         continue;
229
230                 DB((dbg, LEVEL_2, "optimize %+F\n", current_node));
231                 peephole_node(current_node);
232                 assert(!is_Bad(current_node));
233         }
234 }
235
236 static void kill_node_and_preds(ir_node *node)
237 {
238         int arity, i;
239
240         arity = get_irn_arity(node);
241         for (i = 0; i < arity; ++i) {
242                 ir_node *pred = get_irn_n(node, i);
243
244                 set_irn_n(node, i, new_Bad());
245                 if (get_irn_n_edges(pred) != 0)
246                         continue;
247
248                 kill_node_and_preds(pred);
249         }
250
251         if (!is_Proj(node))
252                 sched_remove(node);
253         kill_node(node);
254 }
255
256 /**
257  * Walk through the block schedule and skip all barrier nodes.
258  */
259 static void skip_barrier(ir_node *ret_blk, ir_graph *irg)
260 {
261         ir_node *irn;
262
263         sched_foreach_reverse(ret_blk, irn) {
264                 const ir_edge_t *edge, *next;
265
266                 if (!be_is_Barrier(irn))
267                         continue;
268
269                 foreach_out_edge_safe(irn, edge, next) {
270                         ir_node *proj = get_edge_src_irn(edge);
271                         int      pn;
272                         ir_node *pred;
273
274                         if (is_Anchor(proj))
275                                 continue;
276
277                         pn   = (int) get_Proj_proj(proj);
278                         pred = get_irn_n(irn, pn);
279
280                         edges_reroute_kind(proj, pred, EDGE_KIND_NORMAL, irg);
281                         edges_reroute_kind(proj, pred, EDGE_KIND_DEP, irg);
282                 }
283
284                 kill_node_and_preds(irn);
285                 break;
286         }
287 }
288
289 /**
290  * Kill the Barrier nodes for better peephole optimization.
291  */
292 static void     kill_barriers(ir_graph *irg)
293 {
294         ir_node *end_blk = get_irg_end_block(irg);
295         ir_node *start_blk;
296         int i;
297
298         /* skip the barrier on all return blocks */
299         for (i = get_Block_n_cfgpreds(end_blk) - 1; i >= 0; --i) {
300                 ir_node *be_ret = get_Block_cfgpred(end_blk, i);
301                 ir_node *ret_blk = get_nodes_block(be_ret);
302
303                 skip_barrier(ret_blk, irg);
304         }
305
306         /* skip the barrier on the start block */
307         start_blk = get_irg_start_block(irg);
308         skip_barrier(start_blk, irg);
309 }
310
311 /**
312  * Check whether the node has only one user.  Explicitly ignore the anchor.
313  */
314 static int has_only_one_user(ir_node *node)
315 {
316         int              n = get_irn_n_edges(node);
317         const ir_edge_t *edge;
318
319         if (n <= 1)
320                 return 1;
321
322         if (n > 2)
323                 return 0;
324
325         foreach_out_edge(node, edge) {
326                 ir_node *src = get_edge_src_irn(edge);
327                 if (is_Anchor(src))
328                         return 1;
329         }
330
331         return 0;
332 }
333
334 /*
335  * Tries to optimize a beIncSP node with its previous IncSP node.
336  * Must be run from a be_peephole_opt() context.
337  */
338 ir_node *be_peephole_IncSP_IncSP(ir_node *node)
339 {
340         int      pred_offs;
341         int      curr_offs;
342         int      offs;
343         ir_node *pred = be_get_IncSP_pred(node);
344
345         if (!be_is_IncSP(pred))
346                 return node;
347
348         if (!has_only_one_user(pred))
349                 return node;
350
351         pred_offs = be_get_IncSP_offset(pred);
352         curr_offs = be_get_IncSP_offset(node);
353
354         if (pred_offs == BE_STACK_FRAME_SIZE_EXPAND) {
355                 if (curr_offs != BE_STACK_FRAME_SIZE_SHRINK) {
356                         return node;
357                 }
358                 offs = 0;
359         } else if (pred_offs == BE_STACK_FRAME_SIZE_SHRINK) {
360                 if (curr_offs != BE_STACK_FRAME_SIZE_EXPAND) {
361                         return node;
362                 }
363                 offs = 0;
364         } else if (curr_offs == BE_STACK_FRAME_SIZE_EXPAND ||
365                    curr_offs == BE_STACK_FRAME_SIZE_SHRINK) {
366                 return node;
367         } else {
368                 offs = curr_offs + pred_offs;
369         }
370
371         /* add node offset to pred and remove our IncSP */
372         be_set_IncSP_offset(pred, offs);
373
374         be_peephole_exchange(node, pred);
375         return pred;
376 }
377
378 void be_peephole_opt(be_irg_t *birg)
379 {
380         ir_graph   *irg = be_get_birg_irg(birg);
381         unsigned n_classes;
382         unsigned i;
383
384         /* barrier nodes are used for register allocations. They hinders
385          * peephole optimizations, so remove them here. */
386         kill_barriers(irg);
387
388         /* we sometimes find BadE nodes in float apps like optest_float.c or
389          * kahansum.c for example... */
390         be_liveness_invalidate(birg->lv);
391         be_liveness_assure_sets(be_assure_liveness(birg));
392
393         arch_env = be_get_birg_arch_env(birg);
394         lv       = be_get_birg_liveness(birg);
395
396         n_classes = arch_env_get_n_reg_class(arch_env);
397         register_values = XMALLOCN(ir_node**, n_classes);
398         for (i = 0; i < n_classes; ++i) {
399                 const arch_register_class_t *cls    = arch_env_get_reg_class(arch_env, i);
400                 unsigned                     n_regs = arch_register_class_n_regs(cls);
401                 register_values[i] = XMALLOCN(ir_node*, n_regs);
402         }
403
404         irg_block_walk_graph(irg, process_block, NULL, NULL);
405
406         for (i = 0; i < n_classes; ++i) {
407                 xfree(register_values[i]);
408         }
409         xfree(register_values);
410 }
411
412 BE_REGISTER_MODULE_CONSTRUCTOR(be_init_peephole);
413 void be_init_peephole(void)
414 {
415         FIRM_DBG_REGISTER(dbg, "firm.be.peephole");
416 }