2 * Copyright (C) 1995-2008 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 * @author Sebastian Hack, Matthias Braun
24 * Handling of the stack frame. It is composed of three types:
25 * 1) The type of the arguments which are pushed on the stack.
26 * 2) The "between type" which consists of stuff the call of the
27 * function pushes on the stack (like the return address and
28 * the old base pointer for ia32).
29 * 3) The Firm frame type which consists of all local variables
38 #include "bessaconstr.h"
45 int be_get_stack_entity_offset(be_stack_layout_t *frame, ir_entity *ent,
48 ir_type *t = get_entity_owner(ent);
49 int ofs = get_entity_offset(ent);
53 /* Find the type the entity is contained in. */
54 for (index = 0; index < N_FRAME_TYPES; ++index) {
55 if (frame->order[index] == t)
57 /* Add the size of all the types below the one of the entity to the entity's offset */
58 ofs += get_type_size_bytes(frame->order[index]);
61 /* correct the offset by the initial position of the frame pointer */
62 ofs -= frame->initial_offset;
64 /* correct the offset with the current bias. */
71 * Retrieve the entity with given offset from a frame type.
73 static ir_entity *search_ent_with_offset(ir_type *t, int offset)
77 for (i = 0, n = get_compound_n_members(t); i < n; ++i) {
78 ir_entity *ent = get_compound_member(t, i);
79 if (get_entity_offset(ent) == offset)
86 static void stack_frame_compute_initial_offset(be_stack_layout_t *frame)
88 ir_type *base = frame->between_type;
89 ir_entity *ent = search_ent_with_offset(base, 0);
92 frame->initial_offset = get_type_size_bytes(frame->frame_type);
94 frame->initial_offset = be_get_stack_entity_offset(frame, ent, 0);
99 * Walker: finally lower all Sels of outer frame or parameter
102 static void lower_outer_frame_sels(ir_node *sel, void *ctx)
107 be_stack_layout_t *layout;
114 ent = get_Sel_entity(sel);
115 owner = get_entity_owner(ent);
116 ptr = get_Sel_ptr(sel);
117 irg = get_irn_irg(sel);
118 layout = be_get_irg_stack_layout(irg);
120 if (owner == layout->frame_type || owner == layout->arg_type) {
121 /* found access to outer frame or arguments */
122 int offset = be_get_stack_entity_offset(layout, ent, 0);
125 ir_node *bl = get_nodes_block(sel);
126 dbg_info *dbgi = get_irn_dbg_info(sel);
127 ir_mode *mode = get_irn_mode(sel);
128 ir_mode *mode_UInt = get_reference_mode_unsigned_eq(mode);
129 ir_node *cnst = new_r_Const_long(irg, mode_UInt, offset);
131 ptr = new_rd_Add(dbgi, bl, ptr, cnst, mode);
138 * A helper struct for the bias walker.
140 typedef struct bias_walk {
141 int start_block_bias; /**< The bias at the end of the start block. */
142 ir_node *start_block; /**< The start block of the current graph. */
146 * Fix all stack accessing operations in the block bl.
148 * @param bl the block to process
149 * @param real_bias the bias value
151 * @return the bias at the end of this block
153 static int process_stack_bias(ir_node *bl, int real_bias)
155 int wanted_bias = real_bias;
156 ir_graph *irg = get_Block_irg(bl);
157 be_stack_layout_t *layout = be_get_irg_stack_layout(irg);
158 bool sp_relative = layout->sp_relative;
159 const arch_env_t *arch_env = be_get_irg_arch_env(irg);
161 sched_foreach(bl, irn) {
165 Check, if the node relates to an entity on the stack frame.
166 If so, set the true offset (including the bias) for that
169 ir_entity *ent = arch_get_frame_entity(irn);
171 int bias = sp_relative ? real_bias : 0;
172 int offset = be_get_stack_entity_offset(layout, ent, bias);
173 arch_set_frame_offset(irn, offset);
177 * If the node modifies the stack pointer by a constant offset,
178 * record that in the bias.
180 if (be_is_IncSP(irn)) {
181 ofs = be_get_IncSP_offset(irn);
182 /* fill in real stack frame size */
183 if (be_get_IncSP_align(irn)) {
184 /* patch IncSP to produce an aligned stack pointer */
185 int const between_size = get_type_size_bytes(layout->between_type);
186 int const alignment = 1 << arch_env->stack_alignment;
187 int const delta = (real_bias + ofs + between_size) & (alignment - 1);
190 be_set_IncSP_offset(irn, ofs + alignment - delta);
191 real_bias += alignment - delta;
194 /* adjust so real_bias corresponds with wanted_bias */
195 int delta = wanted_bias - real_bias;
198 be_set_IncSP_offset(irn, ofs + delta);
205 ofs = arch_get_sp_bias(irn);
206 if (ofs == SP_BIAS_RESET) {
216 assert(real_bias == wanted_bias);
221 * Block-Walker: fix all stack offsets for all blocks
222 * except the start block
224 static void stack_bias_walker(ir_node *bl, void *data)
226 bias_walk *bw = (bias_walk*)data;
227 if (bl != bw->start_block) {
228 process_stack_bias(bl, bw->start_block_bias);
232 void be_abi_fix_stack_bias(ir_graph *irg)
234 be_stack_layout_t *stack_layout = be_get_irg_stack_layout(irg);
239 stack_frame_compute_initial_offset(stack_layout);
241 /* Determine the stack bias at the end of the start block. */
242 bw.start_block = get_irg_start_block(irg);
243 bw.start_block_bias = process_stack_bias(bw.start_block, stack_layout->initial_bias);
245 /* fix the bias is all other blocks */
246 irg_block_walk_graph(irg, stack_bias_walker, NULL, &bw);
248 /* fix now inner functions: these still have Sel node to outer
249 frame and parameter entities */
250 frame_tp = get_irg_frame_type(irg);
251 for (i = get_class_n_members(frame_tp) - 1; i >= 0; --i) {
252 ir_entity *ent = get_class_member(frame_tp, i);
253 ir_graph *irg = get_entity_irg(ent);
256 irg_walk_graph(irg, NULL, lower_outer_frame_sels, NULL);
261 typedef struct fix_stack_walker_env_t {
263 } fix_stack_walker_env_t;
266 * Walker. Collect all stack modifying nodes.
268 static void collect_stack_nodes_walker(ir_node *node, void *data)
270 fix_stack_walker_env_t *const env = (fix_stack_walker_env_t*)data;
272 if (get_irn_mode(node) == mode_T)
275 arch_register_req_t const *const req = arch_get_irn_register_req(node);
276 if (!arch_register_req_is(req, produces_sp))
279 ARR_APP1(ir_node*, env->sp_nodes, node);
282 void be_abi_fix_stack_nodes(ir_graph *irg)
284 be_lv_t *lv = be_get_irg_liveness(irg);
285 const arch_env_t *arch_env = be_get_irg_arch_env(irg);
286 be_irg_t *birg = be_birg_from_irg(irg);
287 const arch_register_t *sp = arch_env->sp;
288 be_ssa_construction_env_t senv;
291 fix_stack_walker_env_t walker_env;
293 arch_register_req_t const *sp_req = birg->sp_req;
294 if (sp_req == NULL) {
295 arch_register_req_type_t type = arch_register_req_type_produces_sp;
296 if (!rbitset_is_set(birg->allocatable_regs, sp->global_index))
297 type |= arch_register_req_type_ignore;
299 struct obstack *const obst = be_get_be_obst(irg);
300 birg->sp_req = sp_req = be_create_reg_req(obst, sp, type);
303 walker_env.sp_nodes = NEW_ARR_F(ir_node*, 0);
305 irg_walk_graph(irg, collect_stack_nodes_walker, NULL, &walker_env);
307 /* nothing to be done if we didn't find any node, in fact we mustn't
308 * continue, as for endless loops incsp might have had no users and is bad
311 len = ARR_LEN(walker_env.sp_nodes);
313 DEL_ARR_F(walker_env.sp_nodes);
317 be_ssa_construction_init(&senv, irg);
318 be_ssa_construction_add_copies(&senv, walker_env.sp_nodes,
319 ARR_LEN(walker_env.sp_nodes));
320 be_ssa_construction_fix_users_array(&senv, walker_env.sp_nodes,
321 ARR_LEN(walker_env.sp_nodes));
324 len = ARR_LEN(walker_env.sp_nodes);
325 for (i = 0; i < len; ++i) {
326 be_liveness_update(lv, walker_env.sp_nodes[i]);
328 be_ssa_construction_update_liveness_phis(&senv, lv);
331 phis = be_ssa_construction_get_new_phis(&senv);
333 /* set register requirements for stack phis */
335 for (i = 0; i < len; ++i) {
336 ir_node *phi = phis[i];
337 be_set_phi_reg_req(phi, sp_req);
338 arch_set_irn_register(phi, arch_env->sp);
340 be_ssa_construction_destroy(&senv);
341 DEL_ARR_F(walker_env.sp_nodes);
343 /* when doing code with frame-pointers then often the last incsp-nodes are
344 * not used anymore because we copy the framepointer to the stack pointer
345 * when leaving the function. Though the last incsp is often kept (because
346 * you often don't know which incsp is the last one and fixstack should find
347 * them all). Remove unnecessary keeps and IncSP nodes */
349 ir_node *end = get_irg_end(irg);
350 int arity = get_irn_arity(end);
352 for (i = arity-1; i >= 0; --i) {
353 ir_node *in = get_irn_n(end, i);
354 if (!be_is_IncSP(in)) {
358 remove_End_keepalive(end, in);
359 if (get_irn_n_edges(in) == 0) {