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