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