Use foreach_set() instead of reimplementing it.
[libfirm] / ir / be / bestack.c
1 /*
2  * Copyright (C) 1995-2008 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  * @author      Sebastian Hack, Matthias Braun
23  *
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
30  *    and the spills.
31  */
32 #include "config.h"
33
34 #include "bestack.h"
35 #include "beirg.h"
36 #include "besched.h"
37 #include "benode.h"
38 #include "bessaconstr.h"
39
40 #include "ircons_t.h"
41 #include "irnode_t.h"
42 #include "irgwalk.h"
43 #include "irgmod.h"
44
45 int be_get_stack_entity_offset(be_stack_layout_t *frame, ir_entity *ent,
46                                int bias)
47 {
48         ir_type *t = get_entity_owner(ent);
49         int ofs    = get_entity_offset(ent);
50
51         int index;
52
53         /* Find the type the entity is contained in. */
54         for (index = 0; index < N_FRAME_TYPES; ++index) {
55                 if (frame->order[index] == t)
56                         break;
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]);
59         }
60
61         /* correct the offset by the initial position of the frame pointer */
62         ofs -= frame->initial_offset;
63
64         /* correct the offset with the current bias. */
65         ofs += bias;
66
67         return ofs;
68 }
69
70 /**
71  * Retrieve the entity with given offset from a frame type.
72  */
73 static ir_entity *search_ent_with_offset(ir_type *t, int offset)
74 {
75         int i, n;
76
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)
80                         return ent;
81         }
82
83         return NULL;
84 }
85
86 static int stack_frame_compute_initial_offset(be_stack_layout_t *frame)
87 {
88         ir_type  *base = frame->between_type;
89         ir_entity *ent = search_ent_with_offset(base, 0);
90
91         if (ent == NULL) {
92                 frame->initial_offset = get_type_size_bytes(frame->frame_type);
93         } else {
94                 frame->initial_offset = be_get_stack_entity_offset(frame, ent, 0);
95         }
96
97         return frame->initial_offset;
98 }
99
100 /**
101  * Walker: finally lower all Sels of outer frame or parameter
102  * entities.
103  */
104 static void lower_outer_frame_sels(ir_node *sel, void *ctx)
105 {
106         ir_node           *ptr;
107         ir_entity         *ent;
108         ir_type           *owner;
109         be_stack_layout_t *layout;
110         ir_graph          *irg;
111         (void) ctx;
112
113         if (! is_Sel(sel))
114                 return;
115
116         ent    = get_Sel_entity(sel);
117         owner  = get_entity_owner(ent);
118         ptr    = get_Sel_ptr(sel);
119         irg    = get_irn_irg(sel);
120         layout = be_get_irg_stack_layout(irg);
121
122         if (owner == layout->frame_type || owner == layout->arg_type) {
123                 /* found access to outer frame or arguments */
124                 int offset = be_get_stack_entity_offset(layout, ent, 0);
125
126                 if (offset != 0) {
127                         ir_node  *bl   = get_nodes_block(sel);
128                         dbg_info *dbgi = get_irn_dbg_info(sel);
129                         ir_mode  *mode = get_irn_mode(sel);
130                         ir_mode  *mode_UInt = get_reference_mode_unsigned_eq(mode);
131                         ir_node  *cnst = new_r_Const_long(current_ir_graph, mode_UInt, offset);
132
133                         ptr = new_rd_Add(dbgi, bl, ptr, cnst, mode);
134                 }
135                 exchange(sel, ptr);
136         }
137 }
138
139 /**
140  * A helper struct for the bias walker.
141  */
142 typedef struct bias_walk {
143         int           start_block_bias;  /**< The bias at the end of the start block. */
144         int           between_size;
145         ir_node      *start_block;  /**< The start block of the current graph. */
146 } bias_walk;
147
148 /**
149  * Fix all stack accessing operations in the block bl.
150  *
151  * @param bl         the block to process
152  * @param real_bias  the bias value
153  *
154  * @return the bias at the end of this block
155  */
156 static int process_stack_bias(ir_node *bl, int real_bias)
157 {
158         int                wanted_bias = real_bias;
159         ir_graph          *irg         = get_Block_irg(bl);
160         be_stack_layout_t *layout      = be_get_irg_stack_layout(irg);
161         bool               sp_relative = layout->sp_relative;
162         const arch_env_t  *arch_env    = be_get_irg_arch_env(irg);
163         ir_node           *irn;
164
165         sched_foreach(bl, irn) {
166                 int ofs;
167
168                 /*
169                    Check, if the node relates to an entity on the stack frame.
170                    If so, set the true offset (including the bias) for that
171                    node.
172                  */
173                 ir_entity *ent = arch_get_frame_entity(irn);
174                 if (ent != NULL) {
175                         int bias   = sp_relative ? real_bias : 0;
176                         int offset = be_get_stack_entity_offset(layout, ent, bias);
177                         arch_set_frame_offset(irn, offset);
178                 }
179
180                 /*
181                  * If the node modifies the stack pointer by a constant offset,
182                  * record that in the bias.
183                  */
184                 if (be_is_IncSP(irn)) {
185                         ofs = be_get_IncSP_offset(irn);
186                         /* fill in real stack frame size */
187                         if (be_get_IncSP_align(irn)) {
188                                 /* patch IncSP to produce an aligned stack pointer */
189                                 ir_type *between_type = layout->between_type;
190                                 int      between_size = get_type_size_bytes(between_type);
191                                 int      alignment    = 1 << arch_env->stack_alignment;
192                                 int      delta        = (real_bias + ofs + between_size) & (alignment - 1);
193                                 assert(ofs >= 0);
194                                 if (delta > 0) {
195                                         be_set_IncSP_offset(irn, ofs + alignment - delta);
196                                         real_bias += alignment - delta;
197                                 }
198                         } else {
199                                 /* adjust so real_bias corresponds with wanted_bias */
200                                 int delta = wanted_bias - real_bias;
201                                 assert(delta <= 0);
202                                 if (delta != 0) {
203                                         be_set_IncSP_offset(irn, ofs + delta);
204                                         real_bias += delta;
205                                 }
206                         }
207                         real_bias   += ofs;
208                         wanted_bias += ofs;
209                 } else {
210                         ofs = arch_get_sp_bias(irn);
211                         if (ofs == SP_BIAS_RESET) {
212                                 real_bias   = 0;
213                                 wanted_bias = 0;
214                         } else {
215                                 real_bias   += ofs;
216                                 wanted_bias += ofs;
217                         }
218                 }
219         }
220
221         assert(real_bias == wanted_bias);
222         return real_bias;
223 }
224
225 /**
226  * Block-Walker: fix all stack offsets for all blocks
227  * except the start block
228  */
229 static void stack_bias_walker(ir_node *bl, void *data)
230 {
231         bias_walk *bw = (bias_walk*)data;
232         if (bl != bw->start_block) {
233                 process_stack_bias(bl, bw->start_block_bias);
234         }
235 }
236
237 void be_abi_fix_stack_bias(ir_graph *irg)
238 {
239         be_stack_layout_t *stack_layout = be_get_irg_stack_layout(irg);
240         ir_type           *frame_tp;
241         int                i;
242         bias_walk          bw;
243
244         stack_frame_compute_initial_offset(stack_layout);
245
246         /* Determine the stack bias at the end of the start block. */
247         bw.start_block_bias = process_stack_bias(get_irg_start_block(irg),
248                                                  stack_layout->initial_bias);
249         bw.between_size     = get_type_size_bytes(stack_layout->between_type);
250
251         /* fix the bias is all other blocks */
252         bw.start_block = get_irg_start_block(irg);
253         irg_block_walk_graph(irg, stack_bias_walker, NULL, &bw);
254
255         /* fix now inner functions: these still have Sel node to outer
256            frame and parameter entities */
257         frame_tp = get_irg_frame_type(irg);
258         for (i = get_class_n_members(frame_tp) - 1; i >= 0; --i) {
259                 ir_entity *ent = get_class_member(frame_tp, i);
260                 ir_graph  *irg = get_entity_irg(ent);
261
262                 if (irg != NULL) {
263                         irg_walk_graph(irg, NULL, lower_outer_frame_sels, NULL);
264                 }
265         }
266 }
267
268 typedef struct fix_stack_walker_env_t {
269         ir_node **sp_nodes;
270 } fix_stack_walker_env_t;
271
272 /**
273  * Walker. Collect all stack modifying nodes.
274  */
275 static void collect_stack_nodes_walker(ir_node *node, void *data)
276 {
277         ir_node                   *insn = node;
278         fix_stack_walker_env_t    *env  = (fix_stack_walker_env_t*)data;
279         const arch_register_req_t *req;
280
281         if (is_Proj(node)) {
282                 insn = get_Proj_pred(node);
283         }
284
285         if (arch_get_irn_n_outs(insn) == 0)
286                 return;
287         if (get_irn_mode(node) == mode_T)
288                 return;
289
290         req = arch_get_irn_register_req(node);
291         if (! (req->type & arch_register_req_type_produces_sp))
292                 return;
293
294         ARR_APP1(ir_node*, env->sp_nodes, node);
295 }
296
297 void be_abi_fix_stack_nodes(ir_graph *irg)
298 {
299         be_lv_t                   *lv       = be_get_irg_liveness(irg);
300         const arch_env_t          *arch_env = be_get_irg_arch_env(irg);
301         be_irg_t                  *birg     = be_birg_from_irg(irg);
302         const arch_register_req_t *sp_req   = birg->sp_req;
303         const arch_register_t     *sp       = arch_env->sp;
304         be_ssa_construction_env_t  senv;
305         int i, len;
306         ir_node **phis;
307         fix_stack_walker_env_t walker_env;
308
309         if (sp_req == NULL) {
310                 struct obstack      *obst = be_get_be_obst(irg);
311                 arch_register_req_t *new_sp_req;
312                 unsigned            *limited_bitset;
313
314                 new_sp_req        = OALLOCZ(obst, arch_register_req_t);
315                 new_sp_req->type  = arch_register_req_type_limited
316                                   | arch_register_req_type_produces_sp;
317                 new_sp_req->cls   = arch_register_get_class(arch_env->sp);
318                 new_sp_req->width = 1;
319
320                 limited_bitset = rbitset_obstack_alloc(obst, new_sp_req->cls->n_regs);
321                 rbitset_set(limited_bitset, arch_register_get_index(sp));
322                 new_sp_req->limited = limited_bitset;
323
324                 if (!rbitset_is_set(birg->allocatable_regs, sp->global_index)) {
325                         new_sp_req->type |= arch_register_req_type_ignore;
326                 }
327
328                 sp_req = new_sp_req;
329                 birg->sp_req = new_sp_req;
330         }
331
332         walker_env.sp_nodes = NEW_ARR_F(ir_node*, 0);
333
334         irg_walk_graph(irg, collect_stack_nodes_walker, NULL, &walker_env);
335
336         /* nothing to be done if we didn't find any node, in fact we mustn't
337          * continue, as for endless loops incsp might have had no users and is bad
338          * now.
339          */
340         len = ARR_LEN(walker_env.sp_nodes);
341         if (len == 0) {
342                 DEL_ARR_F(walker_env.sp_nodes);
343                 return;
344         }
345
346         be_ssa_construction_init(&senv, irg);
347         be_ssa_construction_add_copies(&senv, walker_env.sp_nodes,
348                                    ARR_LEN(walker_env.sp_nodes));
349         be_ssa_construction_fix_users_array(&senv, walker_env.sp_nodes,
350                                             ARR_LEN(walker_env.sp_nodes));
351
352         if (lv != NULL) {
353                 len = ARR_LEN(walker_env.sp_nodes);
354                 for (i = 0; i < len; ++i) {
355                         be_liveness_update(lv, walker_env.sp_nodes[i]);
356                 }
357                 be_ssa_construction_update_liveness_phis(&senv, lv);
358         }
359
360         phis = be_ssa_construction_get_new_phis(&senv);
361
362         /* set register requirements for stack phis */
363         len = ARR_LEN(phis);
364         for (i = 0; i < len; ++i) {
365                 ir_node *phi = phis[i];
366                 be_set_phi_reg_req(phi, sp_req);
367                 arch_set_irn_register(phi, arch_env->sp);
368         }
369         be_ssa_construction_destroy(&senv);
370         DEL_ARR_F(walker_env.sp_nodes);
371
372         /* when doing code with frame-pointers then often the last incsp-nodes are
373          * not used anymore because we copy the framepointer to the stack pointer
374          * when leaving the function. Though the last incsp is often kept (because
375          * you often don't know which incsp is the last one and fixstack should find
376          * them all). Remove unnecessary keeps and IncSP nodes */
377         {
378                 ir_node  *end    = get_irg_end(irg);
379                 int       arity  = get_irn_arity(end);
380                 int       i;
381                 for (i = arity-1; i >= 0; --i) {
382                         ir_node *in = get_irn_n(end, i);
383                         if (!be_is_IncSP(in)) {
384                                 continue;
385                         }
386
387                         remove_End_keepalive(end, in);
388                         if (get_irn_n_edges(in) == 0) {
389                                 sched_remove(in);
390                                 kill_node(in);
391                         }
392                 }
393         }
394 }