Fixed some typos.
[libfirm] / ir / be / sparc / sparc_finish.c
1 /*
2  * Copyright (C) 1995-2010 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 optimization and legalization of a sparc function
23  * @author   Matthias Braun
24  * @version  $Id$
25  *
26  * A note on sparc stackpointer (sp) behaviour:
27  * The ABI expects SPARC_MIN_STACKSIZE bytes to be available at the
28  * stackpointer. This space will be used to spill register windows,
29  * and for spilling va_arg arguments (maybe we can optimize this away for
30  * statically known not-va-arg-functions...)
31  * This in effect means that we allocate that extra space at the function begin
32  * which is easy. But this space isn't really fixed at the beginning of the
33  * stackframe. Instead you should rather imagine the space as always being the
34  * last-thing on the stack.
35  * So when addressing anything stack-specific we have to account for this
36  * area, while our compiler thinks the space is occupied at the beginning
37  * of the stack frame. The code here among other things adjusts these offsets
38  * accordingly.
39  */
40 #include "config.h"
41
42 #include "bearch_sparc_t.h"
43 #include "gen_sparc_regalloc_if.h"
44 #include "sparc_new_nodes.h"
45 #include "sparc_transform.h"
46 #include "irprog.h"
47 #include "irgmod.h"
48 #include "ircons.h"
49 #include "irgwalk.h"
50
51 #include "../bepeephole.h"
52 #include "../benode.h"
53 #include "../besched.h"
54 #include "../bespillslots.h"
55 #include "../bestack.h"
56 #include "../beirgmod.h"
57
58 static void kill_unused_stacknodes(ir_node *node)
59 {
60         if (get_irn_n_edges(node) > 0)
61                 return;
62
63         if (be_is_IncSP(node)) {
64                 sched_remove(node);
65                 kill_node(node);
66         } else if (is_Phi(node)) {
67                 int       arity = get_irn_arity(node);
68                 ir_node **ins   = ALLOCAN(ir_node*, arity);
69                 int       i;
70                 sched_remove(node);
71                 memcpy(ins, get_irn_in(node), arity*sizeof(ins[0]));
72                 kill_node(node);
73
74                 for (i = 0; i < arity; ++i)
75                         kill_unused_stacknodes(ins[i]);
76         }
77 }
78
79 static void introduce_epilog(ir_node *ret)
80 {
81         const arch_register_t *sp_reg     = &sparc_registers[REG_SP];
82         ir_graph              *irg        = get_irn_irg(ret);
83         be_stack_layout_t     *layout     = be_get_irg_stack_layout(irg);
84         ir_node               *block      = get_nodes_block(ret);
85         ir_type               *frame_type = get_irg_frame_type(irg);
86         unsigned               frame_size = get_type_size_bytes(frame_type);
87         int                    sp_idx     = be_find_return_reg_input(ret, sp_reg);
88         ir_node               *sp         = get_irn_n(ret, sp_idx);
89
90         if (!layout->sp_relative) {
91                 const arch_register_t *fp_reg = &sparc_registers[REG_FRAME_POINTER];
92                 ir_node *fp      = be_get_initial_reg_value(irg, fp_reg);
93                 ir_node *restore = new_bd_sparc_RestoreZero(NULL, block, fp);
94                 sched_add_before(ret, restore);
95                 arch_set_irn_register(restore, sp_reg);
96                 set_irn_n(ret, sp_idx, restore);
97
98                 kill_unused_stacknodes(sp);
99         } else {
100                 ir_node *incsp  = be_new_IncSP(sp_reg, block, sp, -frame_size, 0);
101                 set_irn_n(ret, sp_idx, incsp);
102                 sched_add_before(ret, incsp);
103         }
104 }
105
106 void sparc_introduce_prolog_epilog(ir_graph *irg)
107 {
108         const arch_register_t *sp_reg     = &sparc_registers[REG_SP];
109         ir_node               *start      = get_irg_start(irg);
110         be_stack_layout_t     *layout     = be_get_irg_stack_layout(irg);
111         ir_node               *block      = get_nodes_block(start);
112         ir_node               *initial_sp = be_get_initial_reg_value(irg, sp_reg);
113         ir_node               *sp         = initial_sp;
114         ir_node               *schedpoint = start;
115         ir_type               *frame_type = get_irg_frame_type(irg);
116         unsigned               frame_size = get_type_size_bytes(frame_type);
117
118         /* introduce epilog for every return node */
119         {
120                 ir_node *end_block = get_irg_end_block(irg);
121                 int      arity     = get_irn_arity(end_block);
122                 int      i;
123
124                 for (i = 0; i < arity; ++i) {
125                         ir_node *ret = get_irn_n(end_block, i);
126                         assert(is_sparc_Return(ret));
127                         introduce_epilog(ret);
128                 }
129         }
130
131         while (be_is_Keep(sched_next(schedpoint)))
132                 schedpoint = sched_next(schedpoint);
133
134         if (!layout->sp_relative) {
135                 ir_node *save = new_bd_sparc_Save_imm(NULL, block, sp, NULL,
136                                                       -SPARC_MIN_STACKSIZE-frame_size);
137                 arch_set_irn_register(save, sp_reg);
138                 sched_add_after(schedpoint, save);
139                 schedpoint = save;
140
141                 edges_reroute(initial_sp, save);
142                 set_irn_n(save, n_sparc_Save_stack, initial_sp);
143
144                 /* we still need the Save even if noone is explicitely using the
145                  * value. (TODO: this isn't 100% correct yet, something at the end of
146                  * the function should hold the Save, even if we use a restore
147                  * which just overrides it instead of using the value)
148                  */
149                 if (get_irn_n_edges(save) == 0) {
150                         ir_node *in[] = { save };
151                         ir_node *keep = be_new_Keep(block, 1, in);
152                         sched_add_after(schedpoint, keep);
153                 }
154         } else {
155                 ir_node *incsp = be_new_IncSP(sp_reg, block, sp, frame_size, 0);
156                 edges_reroute(initial_sp, incsp);
157                 be_set_IncSP_pred(incsp, sp);
158                 sched_add_after(schedpoint, incsp);
159         }
160 }
161
162 static void finish_sparc_Save(ir_node *node)
163 {
164         sparc_attr_t *attr = get_sparc_attr(node);
165         int offset = attr->immediate_value;
166         ir_node  *schedpoint = node;
167         dbg_info *dbgi;
168         ir_node  *block;
169         ir_node  *new_save;
170         ir_node  *stack;
171         ir_entity *entity;
172
173         if (sparc_is_value_imm_encodeable(offset))
174                 return;
175
176         /* uhh only works for the imm variant yet */
177         assert(get_irn_arity(node) == 1);
178
179         block = get_nodes_block(node);
180         dbgi = get_irn_dbg_info(node);
181         stack = get_irn_n(node, n_sparc_Save_stack);
182         entity = attr->immediate_value_entity;
183         new_save = new_bd_sparc_Save_imm(dbgi, block, stack, entity, 0);
184         arch_set_irn_register(new_save, &sparc_registers[REG_SP]);
185         stack = new_save;
186
187         sched_add_after(node, new_save);
188         schedpoint = new_save;
189         while (offset > SPARC_IMMEDIATE_MAX || offset < SPARC_IMMEDIATE_MIN) {
190                 if (offset > 0) {
191                         stack = be_new_IncSP(&sparc_registers[REG_SP], block, stack,
192                                              SPARC_IMMEDIATE_MIN, 0);
193                         offset -= -SPARC_IMMEDIATE_MIN;
194                 } else {
195                         stack = be_new_IncSP(&sparc_registers[REG_SP], block, stack,
196                                              -SPARC_IMMEDIATE_MIN, 0);
197                         offset -= SPARC_IMMEDIATE_MIN;
198                 }
199                 sched_add_after(schedpoint, stack);
200                 schedpoint = stack;
201         }
202         attr = get_sparc_attr(new_save);
203         attr->immediate_value = offset;
204         be_peephole_exchange(node, stack);
205 }
206
207 /**
208  * sparc immediates are limited. Split IncSP with bigger immediates if
209  * necessary.
210  */
211 static void finish_be_IncSP(ir_node *node)
212 {
213         int      sign   = 1;
214         int      offset = be_get_IncSP_offset(node);
215         ir_node *sp     = be_get_IncSP_pred(node);
216         ir_node *block;
217
218         /* we might have to break the IncSP apart if the constant has become too
219          * big */
220         if (offset < 0) {
221                 offset = -offset;
222                 sign   = -1;
223         }
224
225         if (sparc_is_value_imm_encodeable(-offset))
226                 return;
227
228         /* split incsp into multiple instructions */
229         block = get_nodes_block(node);
230         while (offset > -SPARC_IMMEDIATE_MIN) {
231                 sp = be_new_IncSP(&sparc_registers[REG_SP], block, sp,
232                                   sign * -SPARC_IMMEDIATE_MIN, 0);
233                 sched_add_before(node, sp);
234                 offset -= -SPARC_IMMEDIATE_MIN;
235         }
236
237         be_set_IncSP_pred(node, sp);
238         be_set_IncSP_offset(node, sign*offset);
239 }
240
241 /**
242  * adjust sp-relative offsets. Split into multiple instructions if offset
243  * exceeds SPARC immediate range.
244  */
245 static void finish_sparc_FrameAddr(ir_node *node)
246 {
247         /* adapt to SPARC stack magic */
248         sparc_attr_t *attr   = get_sparc_attr(node);
249         int           offset = attr->immediate_value;
250         ir_node      *base   = get_irn_n(node, n_sparc_FrameAddr_base);
251         dbg_info     *dbgi   = get_irn_dbg_info(node);
252         ir_node      *block  = get_nodes_block(node);
253         int           sign   = 1;
254
255         if (offset < 0) {
256                 sign   = -1;
257                 offset = -offset;
258         }
259
260         if (offset > -SPARC_IMMEDIATE_MIN) {
261                 ir_entity *entity = attr->immediate_value_entity;
262                 ir_node   *new_frameaddr
263                         = new_bd_sparc_FrameAddr(dbgi, block, base, entity, 0);
264                 ir_node   *schedpoint = node;
265                 const arch_register_t *reg = arch_get_irn_register(node);
266
267                 sched_add_after(schedpoint, new_frameaddr);
268                 schedpoint = new_frameaddr;
269                 arch_set_irn_register(new_frameaddr, reg);
270                 base = new_frameaddr;
271
272                 while (offset > -SPARC_IMMEDIATE_MIN) {
273                         if (sign > 0) {
274                                 base = new_bd_sparc_Sub_imm(dbgi, block, base, NULL,
275                                                                                         SPARC_IMMEDIATE_MIN);
276                         } else {
277                                 base = new_bd_sparc_Add_imm(dbgi, block, base, NULL,
278                                                                                         SPARC_IMMEDIATE_MIN);
279                         }
280                         arch_set_irn_register(base, reg);
281                         sched_add_after(schedpoint, base);
282                         schedpoint = base;
283
284                         offset -= -SPARC_IMMEDIATE_MIN;
285                 }
286
287                 be_peephole_exchange(node, base);
288                 attr = get_sparc_attr(new_frameaddr);
289         }
290         attr->immediate_value = sign*offset;
291 }
292
293 static void peephole_be_IncSP(ir_node *node)
294 {
295         ir_node *pred;
296         node = be_peephole_IncSP_IncSP(node);
297         if (!be_is_IncSP(node))
298                 return;
299
300         pred = be_get_IncSP_pred(node);
301         if (is_sparc_Save(pred) && be_has_only_one_user(pred)) {
302                 int offset = -be_get_IncSP_offset(node);
303                 sparc_attr_t *attr = get_sparc_attr(pred);
304                 attr->immediate_value += offset;
305                 be_peephole_exchange(node, pred);
306         }
307 }
308
309 static void peephole_sparc_FrameAddr(ir_node *node)
310 {
311         /* the peephole code currently doesn't allow this since it changes
312          * the register. Find out why and how to workaround this... */
313 #if 0
314         const sparc_attr_t *attr = get_sparc_attr_const(node);
315         if (attr->immediate_value == 0) {
316                 ir_node *base = get_irn_n(node, n_sparc_FrameAddr_base);
317                 be_peephole_exchange(node, base);
318         }
319 #endif
320         (void) node;
321 }
322
323 static void finish_sparc_Return(ir_node *node)
324 {
325         ir_node *schedpoint = node;
326         ir_node *restore;
327         /* see that there is no code between Return and restore, if there is move
328          * it in front of the restore */
329         while (true) {
330                 if (!sched_has_prev(schedpoint))
331                         return;
332                 schedpoint = sched_prev(schedpoint);
333                 if (is_sparc_Restore(schedpoint) || is_sparc_RestoreZero(schedpoint))
334                         break;
335         }
336         restore = schedpoint;
337         schedpoint = sched_prev(node);
338         /* move all code between return and restore up */
339         while (schedpoint != restore) {
340                 ir_node *next_schedpoint = sched_prev(schedpoint);
341                 sched_remove(schedpoint);
342                 sched_add_before(restore, schedpoint);
343                 schedpoint = next_schedpoint;
344         }
345 }
346
347 static void register_peephole_optimisation(ir_op *op, peephole_opt_func func)
348 {
349         assert(op->ops.generic == NULL);
350         op->ops.generic = (op_func) func;
351 }
352
353 static void sparc_collect_frame_entity_nodes(ir_node *node, void *data)
354 {
355         be_fec_env_t  *env = (be_fec_env_t*)data;
356         const ir_mode *mode;
357         int            align;
358         ir_entity     *entity;
359         const sparc_load_store_attr_t *attr;
360
361         if (be_is_Reload(node) && be_get_frame_entity(node) == NULL) {
362                 mode  = get_irn_mode(node);
363                 align = get_mode_size_bytes(mode);
364                 be_node_needs_frame_entity(env, node, mode, align);
365                 return;
366         }
367
368         if (!is_sparc_Ld(node) && !is_sparc_Ldf(node))
369                 return;
370
371         attr   = get_sparc_load_store_attr_const(node);
372         entity = attr->base.immediate_value_entity;
373         mode   = attr->load_store_mode;
374         if (entity != NULL)
375                 return;
376         if (!attr->is_frame_entity)
377                 return;
378         if (arch_get_irn_flags(node) & sparc_arch_irn_flag_needs_64bit_spillslot)
379                 mode = mode_Lu;
380         align  = get_mode_size_bytes(mode);
381         be_node_needs_frame_entity(env, node, mode, align);
382 }
383
384 static void sparc_set_frame_entity(ir_node *node, ir_entity *entity)
385 {
386         if (is_be_node(node)) {
387                 be_node_set_frame_entity(node, entity);
388         } else {
389                 /* we only say be_node_needs_frame_entity on nodes with load_store
390                  * attributes, so this should be fine */
391                 sparc_load_store_attr_t *attr = get_sparc_load_store_attr(node);
392                 assert(attr->is_frame_entity);
393                 assert(attr->base.immediate_value_entity == NULL);
394                 attr->base.immediate_value_entity = entity;
395         }
396 }
397
398 void sparc_finish(ir_graph *irg)
399 {
400         be_stack_layout_t *stack_layout = be_get_irg_stack_layout(irg);
401         bool               at_begin     = stack_layout->sp_relative ? true : false;
402         be_fec_env_t      *fec_env      = be_new_frame_entity_coalescer(irg);
403
404         irg_walk_graph(irg, NULL, sparc_collect_frame_entity_nodes, fec_env);
405         be_assign_entities(fec_env, sparc_set_frame_entity, at_begin);
406         be_free_frame_entity_coalescer(fec_env);
407
408         sparc_introduce_prolog_epilog(irg);
409
410         /* fix stack entity offsets */
411         be_abi_fix_stack_nodes(irg);
412         sparc_fix_stack_bias(irg);
413
414         /* perform peephole optimizations */
415         clear_irp_opcodes_generic_func();
416         register_peephole_optimisation(op_be_IncSP,        peephole_be_IncSP);
417         register_peephole_optimisation(op_sparc_FrameAddr, peephole_sparc_FrameAddr);
418         be_peephole_opt(irg);
419
420         /* perform legalizations (mostly fix nodes with too big immediates) */
421         clear_irp_opcodes_generic_func();
422         register_peephole_optimisation(op_be_IncSP,        finish_be_IncSP);
423         register_peephole_optimisation(op_sparc_FrameAddr, finish_sparc_FrameAddr);
424         register_peephole_optimisation(op_sparc_Return,    finish_sparc_Return);
425         register_peephole_optimisation(op_sparc_Save,      finish_sparc_Save);
426         be_peephole_opt(irg);
427
428         be_remove_dead_nodes_from_schedule(irg);
429 }