2 * Copyright (C) 1995-2010 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
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.
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.
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
22 * @brief Peephole optimization and legalization of a sparc function
23 * @author Matthias Braun
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
42 #include "bearch_sparc_t.h"
43 #include "gen_sparc_regalloc_if.h"
44 #include "sparc_new_nodes.h"
45 #include "sparc_transform.h"
51 #include "../bepeephole.h"
52 #include "../benode.h"
53 #include "../besched.h"
54 #include "../bespillslots.h"
55 #include "../bestack.h"
56 #include "../beirgmod.h"
58 static void kill_unused_stacknodes(ir_node *node)
60 if (get_irn_n_edges(node) > 0)
63 if (be_is_IncSP(node)) {
66 } else if (is_Phi(node)) {
67 int arity = get_irn_arity(node);
68 ir_node **ins = ALLOCAN(ir_node*, arity);
71 memcpy(ins, get_irn_in(node), arity*sizeof(ins[0]));
74 for (i = 0; i < arity; ++i)
75 kill_unused_stacknodes(ins[i]);
79 static void introduce_epilog(ir_node *ret)
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);
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);
98 kill_unused_stacknodes(sp);
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);
106 void sparc_introduce_prolog_epilog(ir_graph *irg)
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);
118 /* introduce epilog for every return node */
120 ir_node *end_block = get_irg_end_block(irg);
121 int arity = get_irn_arity(end_block);
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);
131 while (be_is_Keep(sched_next(schedpoint)))
132 schedpoint = sched_next(schedpoint);
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);
141 edges_reroute(initial_sp, save);
142 set_irn_n(save, n_sparc_Save_stack, initial_sp);
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)
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);
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);
162 static void finish_sparc_Save(ir_node *node)
164 sparc_attr_t *attr = get_sparc_attr(node);
165 int offset = attr->immediate_value;
166 ir_node *schedpoint = node;
173 if (sparc_is_value_imm_encodeable(offset))
176 /* uhh only works for the imm variant yet */
177 assert(get_irn_arity(node) == 1);
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]);
187 sched_add_after(node, new_save);
188 schedpoint = new_save;
189 while (offset > SPARC_IMMEDIATE_MAX || offset < SPARC_IMMEDIATE_MIN) {
191 stack = be_new_IncSP(&sparc_registers[REG_SP], block, stack,
192 SPARC_IMMEDIATE_MIN, 0);
193 offset -= -SPARC_IMMEDIATE_MIN;
195 stack = be_new_IncSP(&sparc_registers[REG_SP], block, stack,
196 -SPARC_IMMEDIATE_MIN, 0);
197 offset -= SPARC_IMMEDIATE_MIN;
199 sched_add_after(schedpoint, stack);
202 attr = get_sparc_attr(new_save);
203 attr->immediate_value = offset;
204 be_peephole_exchange(node, stack);
208 * sparc immediates are limited. Split IncSP with bigger immediates if
211 static void finish_be_IncSP(ir_node *node)
214 int offset = be_get_IncSP_offset(node);
215 ir_node *sp = be_get_IncSP_pred(node);
218 /* we might have to break the IncSP apart if the constant has become too
225 if (sparc_is_value_imm_encodeable(-offset))
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;
237 be_set_IncSP_pred(node, sp);
238 be_set_IncSP_offset(node, sign*offset);
242 * adjust sp-relative offsets. Split into multiple instructions if offset
243 * exceeds SPARC immediate range.
245 static void finish_sparc_FrameAddr(ir_node *node)
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);
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);
267 sched_add_after(schedpoint, new_frameaddr);
268 schedpoint = new_frameaddr;
269 arch_set_irn_register(new_frameaddr, reg);
270 base = new_frameaddr;
272 while (offset > -SPARC_IMMEDIATE_MIN) {
274 base = new_bd_sparc_Sub_imm(dbgi, block, base, NULL,
275 SPARC_IMMEDIATE_MIN);
277 base = new_bd_sparc_Add_imm(dbgi, block, base, NULL,
278 SPARC_IMMEDIATE_MIN);
280 arch_set_irn_register(base, reg);
281 sched_add_after(schedpoint, base);
284 offset -= -SPARC_IMMEDIATE_MIN;
287 be_peephole_exchange(node, base);
288 attr = get_sparc_attr(new_frameaddr);
290 attr->immediate_value = sign*offset;
293 static void peephole_be_IncSP(ir_node *node)
296 node = be_peephole_IncSP_IncSP(node);
297 if (!be_is_IncSP(node))
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);
309 static void peephole_sparc_FrameAddr(ir_node *node)
311 /* the peephole code currently doesn't allow this since it changes
312 * the register. Find out why and how to workaround this... */
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);
323 static void finish_sparc_Return(ir_node *node)
325 ir_node *schedpoint = node;
327 /* see that there is no code between Return and restore, if there is move
328 * it in front of the restore */
330 if (!sched_has_prev(schedpoint))
332 schedpoint = sched_prev(schedpoint);
333 if (is_sparc_Restore(schedpoint) || is_sparc_RestoreZero(schedpoint))
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;
347 static void register_peephole_optimisation(ir_op *op, peephole_opt_func func)
349 assert(op->ops.generic == NULL);
350 op->ops.generic = (op_func) func;
353 static void sparc_collect_frame_entity_nodes(ir_node *node, void *data)
355 be_fec_env_t *env = (be_fec_env_t*)data;
359 const sparc_load_store_attr_t *attr;
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);
368 if (!is_sparc_Ld(node) && !is_sparc_Ldf(node))
371 attr = get_sparc_load_store_attr_const(node);
372 entity = attr->base.immediate_value_entity;
373 mode = attr->load_store_mode;
376 if (!attr->is_frame_entity)
378 if (arch_get_irn_flags(node) & sparc_arch_irn_flag_needs_64bit_spillslot)
380 align = get_mode_size_bytes(mode);
381 be_node_needs_frame_entity(env, node, mode, align);
384 static void sparc_set_frame_entity(ir_node *node, ir_entity *entity)
386 if (is_be_node(node)) {
387 be_node_set_frame_entity(node, entity);
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;
398 void sparc_finish(ir_graph *irg)
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);
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);
408 sparc_introduce_prolog_epilog(irg);
410 /* fix stack entity offsets */
411 be_abi_fix_stack_nodes(irg);
412 sparc_fix_stack_bias(irg);
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);
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);
428 be_remove_dead_nodes_from_schedule(irg);