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