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