added changes for new survice_dce code
[libfirm] / ir / be / beabi.c
1 /**
2  * ABI lowering.
3  *
4  * @author Sebastian Hack
5  * @date 7.3.2005
6  */
7
8 #ifdef HAVE_CONFIG_H
9 # include "config.h"
10 #endif
11 #include "obst.h"
12
13 #include "type.h"
14 #include "irgopt.h"
15
16 #include "irgraph_t.h"
17 #include "irnode_t.h"
18 #include "ircons_t.h"
19 #include "iredges_t.h"
20 #include "irgmod.h"
21 #include "irgwalk.h"
22 #include "irprintf_t.h"
23
24 #include "be.h"
25 #include "beabi.h"
26 #include "bearch.h"
27 #include "benode_t.h"
28 #include "besched_t.h"
29
30 #define MAX(x, y) ((x) > (y) ? (x) : (y))
31 #define MIN(x, y) ((x) < (y) ? (x) : (y))
32
33 typedef struct _be_abi_call_arg_t {
34         unsigned is_res : 1;
35         unsigned in_reg : 1;
36
37         int pos;
38         const arch_register_t *reg;
39         entity *stack_ent;
40 } be_abi_call_arg_t;
41
42 struct _be_abi_call_t {
43         be_abi_call_flags_t flags;
44         type *between_type;
45         set *params;
46 };
47
48 #define N_FRAME_TYPES 3
49
50 typedef struct _be_stack_frame_t {
51         type *arg_type;
52         type *between_type;
53         type *frame_type;
54
55         type *order[N_FRAME_TYPES];        /**< arg, between and frame types ordered. */
56
57         int initial_offset;
58         int stack_dir;
59 } be_stack_frame_t;
60
61 struct _be_stack_slot_t {
62         struct _be_stack_frame_t *frame;
63         entity *ent;
64 };
65
66 struct _be_abi_irg_t {
67         struct obstack       obst;
68         firm_dbg_module_t    *dbg;          /**< The debugging module. */
69         be_stack_frame_t     *frame;        /**< The stack frame model. */
70         const be_irg_t       *birg;         /**< The back end IRG. */
71         const arch_isa_t     *isa;          /**< The isa. */
72         survive_dce_t        *dce_survivor;
73
74         be_abi_call_t        *call;         /**< The ABI call information. */
75         type                 *method_type;  /**< The type of the method of the IRG. */
76
77         ir_node              *init_sp;      /**< The node representing the stack pointer
78                                                                              at the start of the function. */
79
80         ir_node              *reg_params;   /**< The reg params node. */
81         pmap                 *regs;         /**< A map of all callee-save and ignore regs to
82                                                                                         their Projs to the RegParams node. */
83
84         pset                 *stack_phis;   /**< The set of all Phi nodes inserted due to
85                                                                                         stack pointer modifying nodes. */
86
87         int                  start_block_bias;  /**< The stack bias at the end of the start block. */
88
89         arch_irn_handler_t irn_handler;
90         arch_irn_ops_t     irn_ops;
91 };
92
93 #define abi_offset_of(type,member) ((char *) &(((type *) 0)->member) - (char *) 0)
94 #define abi_get_relative(ptr, member) ((void *) ((char *) (ptr) - abi_offset_of(be_abi_irg_t, member)))
95 #define get_abi_from_handler(ptr) abi_get_relative(ptr, irn_handler)
96 #define get_abi_from_ops(ptr)     abi_get_relative(ptr, irn_ops)
97
98 /* Forward, since be need it in be_abi_introduce(). */
99 static const arch_irn_ops_if_t abi_irn_ops;
100 static const arch_irn_handler_t abi_irn_handler;
101
102 /*
103      _    ____ ___    ____      _ _ _                _
104     / \  | __ )_ _|  / ___|__ _| | | |__   __ _  ___| | _____
105    / _ \ |  _ \| |  | |   / _` | | | '_ \ / _` |/ __| |/ / __|
106   / ___ \| |_) | |  | |__| (_| | | | |_) | (_| | (__|   <\__ \
107  /_/   \_\____/___|  \____\__,_|_|_|_.__/ \__,_|\___|_|\_\___/
108
109   These callbacks are used by the backend to set the parameters
110   for a specific call type.
111 */
112
113 static int cmp_call_arg(const void *a, const void *b, size_t n)
114 {
115         const be_abi_call_arg_t *p = a, *q = b;
116         return !(p->is_res == q->is_res && p->pos == q->pos);
117 }
118
119 static be_abi_call_arg_t *get_or_set_call_arg(be_abi_call_t *call, int is_res, int pos, int do_insert)
120 {
121         be_abi_call_arg_t arg;
122         unsigned hash;
123
124         arg.is_res = is_res;
125         arg.pos    = pos;
126
127         hash = is_res * 100 + pos;
128
129         return do_insert
130                 ? set_insert(call->params, &arg, sizeof(arg), hash)
131                 : set_find(call->params, &arg, sizeof(arg), hash);
132 }
133
134 static INLINE be_abi_call_arg_t *get_call_arg(be_abi_call_t *call, int is_res, int pos)
135 {
136         return get_or_set_call_arg(call, is_res, pos, 0);
137 }
138
139 void be_abi_call_set_flags(be_abi_call_t *call, be_abi_call_flags_t flags, ir_type *between_type)
140 {
141         call->flags            = flags;
142         call->between_type     = between_type;
143 }
144
145 void be_abi_call_param_stack(be_abi_call_t *call, int arg_pos)
146 {
147         be_abi_call_arg_t *arg = get_or_set_call_arg(call, 0, arg_pos, 1);
148 }
149
150 void be_abi_call_param_reg(be_abi_call_t *call, int arg_pos, const arch_register_t *reg)
151 {
152         be_abi_call_arg_t *arg = get_or_set_call_arg(call, 0, arg_pos, 1);
153         arg->in_reg = 1;
154         arg->reg = reg;
155 }
156
157 void be_abi_call_res_reg(be_abi_call_t *call, int arg_pos, const arch_register_t *reg)
158 {
159         be_abi_call_arg_t *arg = get_or_set_call_arg(call, 1, arg_pos, 1);
160         arg->in_reg = 1;
161         arg->reg = reg;
162 }
163
164 be_abi_call_t *be_abi_call_new(void)
165 {
166         be_abi_call_t *call = malloc(sizeof(call[0]));
167         call->flags.val = 0;
168         call->params    = new_set(cmp_call_arg, 16);
169         return call;
170 }
171
172 void be_abi_call_free(be_abi_call_t *call)
173 {
174         del_set(call->params);
175         free(call);
176 }
177
178 /*
179   _____                           _   _                 _ _ _
180  |  ___| __ __ _ _ __ ___   ___  | | | | __ _ _ __   __| | (_)_ __   __ _
181  | |_ | '__/ _` | '_ ` _ \ / _ \ | |_| |/ _` | '_ \ / _` | | | '_ \ / _` |
182  |  _|| | | (_| | | | | | |  __/ |  _  | (_| | | | | (_| | | | | | | (_| |
183  |_|  |_|  \__,_|_| |_| |_|\___| |_| |_|\__,_|_| |_|\__,_|_|_|_| |_|\__, |
184                                                                     |___/
185
186   Handling of the stack frame. It is composed of three types:
187   1) The type of the arguments which are pushed on the stack.
188   2) The "between type" which consists of stuff the call of the
189      function pushes on the stack (like the return address and
190          the old base pointer for ia32).
191   3) The Firm frame type which consists of all local variables
192      and the spills.
193 */
194
195 static int get_stack_entity_offset(be_stack_frame_t *frame, entity *ent, int bias)
196 {
197         type *t = get_entity_owner(ent);
198         int ofs = get_entity_offset_bytes(ent);
199
200         int i, index;
201
202         /* Find the type the entity is contained in. */
203         for(index = 0; index < N_FRAME_TYPES; ++index) {
204                 if(frame->order[index] == t)
205                         break;
206         }
207
208         /* Add the size of all the types below the one of the entity to the entity's offset */
209         for(i = 0; i < index; ++i)
210                 ofs += get_type_size_bytes(frame->order[i]);
211
212         /* correct the offset by the initial position of the frame pointer */
213         ofs -= frame->initial_offset;
214
215         /* correct the offset with the current bias. */
216         ofs += bias;
217
218         return ofs;
219 }
220
221 static entity *search_ent_with_offset(type *t, int offset)
222 {
223         int i, n;
224
225         for(i = 0, n = get_class_n_members(t); i < n; ++i) {
226                 entity *ent = get_class_member(t, i);
227                 if(get_entity_offset_bytes(ent) == offset)
228                         return ent;
229         }
230
231         return NULL;
232 }
233
234 static int stack_frame_compute_initial_offset(be_stack_frame_t *frame)
235 {
236         type   *base = frame->stack_dir < 0 ? frame->between_type : frame->frame_type;
237         entity *ent  = search_ent_with_offset(base, 0);
238         frame->initial_offset = 0;
239         frame->initial_offset = get_stack_entity_offset(frame, ent, 0);
240         return frame->initial_offset;
241 }
242
243 static be_stack_frame_t *stack_frame_init(be_stack_frame_t *frame, type *args, type *between, type *locals, int stack_dir)
244 {
245         frame->arg_type       = args;
246         frame->between_type   = between;
247         frame->frame_type     = locals;
248         frame->initial_offset = 0;
249         frame->stack_dir      = stack_dir;
250         frame->order[1]       = between;
251
252         if(stack_dir > 0) {
253                 frame->order[0] = args;
254                 frame->order[2] = locals;
255         }
256
257         else {
258                 frame->order[0] = locals;
259                 frame->order[2] = args;
260         }
261
262         return frame;
263 }
264
265 static void stack_frame_dump(FILE *file, be_stack_frame_t *frame)
266 {
267         int i, j, n;
268
269         ir_fprintf(file, "initial offset: %d\n", frame->initial_offset);
270         for(j = 0; j < N_FRAME_TYPES; ++j) {
271                 type *t = frame->order[j];
272
273                 ir_fprintf(file, "type %d: %Fm size: %d\n", j, t, get_type_size_bytes(t));
274                 for(i = 0, n = get_class_n_members(t); i < n; ++i) {
275                         entity *ent = get_class_member(t, i);
276                         ir_fprintf(file, "\t%F int ofs: %d glob ofs: %d\n", ent, get_entity_offset_bytes(ent), get_stack_entity_offset(frame, ent, 0));
277                 }
278         }
279 }
280
281 /**
282  * If irn is a Sel node computing the address of an entity
283  * on the frame type return the entity, else NULL.
284  */
285 static INLINE entity *get_sel_ent(ir_node *irn)
286 {
287         if(get_irn_opcode(irn) == iro_Sel
288                 && get_Sel_ptr(irn) == get_irg_frame(get_irn_irg(irn))) {
289
290                 return get_Sel_entity(irn);
291         }
292
293         return NULL;
294 }
295
296 /**
297  * Walker: Replaces Loads, Stores and Sels of frame type entities
298  * by FrameLoad, FrameStore and FrameAdress.
299  */
300 static void lower_frame_sels_walker(ir_node *irn, void *data)
301 {
302         const arch_register_class_t *cls;
303         be_abi_irg_t *env = data;
304         const arch_isa_t *isa = env->birg->main_env->arch_env->isa;
305         ir_graph *irg = get_irn_irg(irn);
306         ir_node *frame = get_irg_frame(irg);
307         ir_node *nw  = NULL;
308         opcode opc   = get_irn_opcode(irn);
309
310         if(opc == iro_Load) {
311                 ir_node *bl  = get_nodes_block(irn);
312                 ir_node *sel = get_Load_ptr(irn);
313                 entity *ent  = get_sel_ent(sel);
314                 cls = arch_isa_get_reg_class_for_mode(isa, get_Load_mode(irn));
315                 if(ent != NULL)
316                         nw = be_new_FrameLoad(isa->sp->reg_class, cls, irg, bl, get_Load_mem(irn), frame, ent);
317         }
318
319         else if(opc == iro_Store) {
320                 ir_node *bl  = get_nodes_block(irn);
321                 ir_node *val = get_Store_value(irn);
322                 ir_node *sel = get_Store_ptr(irn);
323                 entity *ent  = get_sel_ent(sel);
324                 cls = arch_isa_get_reg_class_for_mode(isa, get_irn_mode(val));
325                 if(ent != NULL)
326                         nw = be_new_FrameStore(isa->sp->reg_class, cls, irg, bl, get_Store_mem(irn), frame, val, ent);
327         }
328
329         else {
330                 entity *ent = get_sel_ent(irn);
331                 if(ent != NULL) {
332                         ir_node *bl  = get_nodes_block(irn);
333                         nw = be_new_FrameAddr(isa->sp->reg_class, irg, bl, frame, ent);
334                 }
335         }
336
337         if(nw != NULL)
338                 exchange(irn, nw);
339 }
340
341 static INLINE int is_on_stack(be_abi_call_t *call, int pos)
342 {
343         be_abi_call_arg_t *arg = get_call_arg(call, 0, pos);
344         return arg && !arg->in_reg;
345 }
346
347 /*
348    ____      _ _
349   / ___|__ _| | |___
350  | |   / _` | | / __|
351  | |__| (_| | | \__ \
352   \____\__,_|_|_|___/
353
354   Adjustment of the calls inside a graph.
355
356 */
357
358 /**
359  * Transform a call node.
360  * @param env The ABI environment for the current irg.
361  * @param irn The call node.
362  * @param curr_sp The stack pointer node to use.
363  * @return The stack pointer after the call.
364  */
365 static ir_node *adjust_call(be_abi_irg_t *env, ir_node *irn, ir_node *curr_sp)
366 {
367         ir_graph *irg             = env->birg->irg;
368         const arch_isa_t *isa     = env->birg->main_env->arch_env->isa;
369         be_abi_call_t *call       = be_abi_call_new();
370         ir_type *mt               = get_Call_type(irn);
371         ir_node *call_ptr         = get_Call_ptr(irn);
372         int n_params              = get_method_n_params(mt);
373         ir_node *curr_mem         = get_Call_mem(irn);
374         ir_node *bl               = get_nodes_block(irn);
375         pset *results             = pset_new_ptr(8);
376         pset *caller_save         = pset_new_ptr(8);
377         int stack_size            = 0;
378         int stack_dir             = arch_isa_stack_dir(isa);
379         const arch_register_t *sp = arch_isa_sp(isa);
380         ir_mode *mach_mode        = sp->reg_class->mode;
381         struct obstack *obst      = &env->obst;
382         ir_node *no_mem           = get_irg_no_mem(irg);
383
384         ir_node *res_proj = NULL;
385         int curr_res_proj = pn_Call_max;
386         int n_low_args    = 0;
387         int n_pos         = 0;
388
389         ir_node *low_call;
390         ir_node **in;
391         ir_node **res_projs;
392         const ir_edge_t *edge;
393         int *low_args;
394         int *pos;
395         int i, n;
396
397         /* Let the isa fill out the abi description for that call node. */
398         arch_isa_get_call_abi(isa, mt, call);
399
400         /* Insert code to put the stack arguments on the stack. */
401         assert(get_Call_n_params(irn) == n_params);
402         for(i = 0; i < n_params; ++i) {
403                 be_abi_call_arg_t *arg = get_call_arg(call, 0, i);
404                 assert(arg);
405                 if(!arg->in_reg) {
406                         stack_size += get_type_size_bytes(get_method_param_type(mt, i));
407                         obstack_int_grow(obst, i);
408                         n_pos++;
409                 }
410         }
411         pos = obstack_finish(obst);
412
413         /* Collect all arguments which are passed in registers. */
414         for(i = 0, n = get_Call_n_params(irn); i < n; ++i) {
415                 be_abi_call_arg_t *arg = get_call_arg(call, 0, i);
416                 if(arg && arg->in_reg) {
417                         obstack_int_grow(obst, i);
418                         n_low_args++;
419                 }
420         }
421         low_args = obstack_finish(obst);
422
423         /* If there are some parameters which shall be passed on the stack. */
424         if(n_pos > 0) {
425                 int curr_ofs      = 0;
426                 int do_seq        = call->flags.bits.store_args_sequential;
427
428                 /* Reverse list of stack parameters if call arguments are from left to right */
429                 if(call->flags.bits.left_to_right) {
430                         for(i = 0; i < n_pos / 2; ++i) {
431                                 int other  = n_pos - i - 1;
432                                 int tmp    = pos[i];
433                                 pos[i]     = pos[other];
434                                 pos[other] = tmp;
435                         }
436                 }
437
438                 /*
439                  * If the stack is decreasing and we do not want to store sequentially,
440                  * we allocate as much space on the stack all parameters need, by
441                  * moving the stack pointer along the stack's direction.
442                  */
443                 if(stack_dir < 0 && !do_seq) {
444                         curr_sp = be_new_IncSP(sp, irg, bl, curr_sp, no_mem, stack_size, be_stack_dir_along);
445                 }
446
447                 assert(mode_is_reference(mach_mode) && "machine mode must be pointer");
448                 for(i = 0; i < n_pos; ++i) {
449                         int p            = pos[i];
450                         ir_node *param   = get_Call_param(irn, p);
451                         ir_node *addr    = curr_sp;
452                         ir_node *mem     = NULL;
453                         type *param_type = get_method_param_type(mt, p);
454                         int param_size   = get_type_size_bytes(param_type);
455
456                         /* Make the expression to compute the argument's offset. */
457                         if(curr_ofs > 0) {
458                                 addr = new_r_Const_long(irg, bl, mode_Is, curr_ofs);
459                                 addr = new_r_Add(irg, bl, curr_sp, addr, mach_mode);
460                         }
461
462                         /* Insert a store for primitive arguments. */
463                         if(is_atomic_type(param_type)) {
464                                 mem = new_r_Store(irg, bl, curr_mem, addr, param);
465                                 mem = new_r_Proj(irg, bl, mem, mode_M, pn_Store_M);
466                         }
467
468                         /* Make a memcopy for compound arguments. */
469                         else {
470                                 assert(mode_is_reference(get_irn_mode(param)));
471                                 mem = new_r_CopyB(irg, bl, curr_mem, addr, param, param_type);
472                                 mem = new_r_Proj(irg, bl, mem, mode_M, pn_CopyB_M_regular);
473                         }
474
475                         obstack_ptr_grow(obst, mem);
476
477                         curr_ofs += param_size;
478
479                         /*
480                         * If we wanted to build the arguments sequentially,
481                         * the stack pointer for the next must be incremented,
482                         * and the memory value propagated.
483                         */
484                         if(do_seq) {
485                                 curr_ofs = 0;
486                                 curr_sp  = be_new_IncSP(sp, irg, bl, curr_sp, no_mem, param_size, be_stack_dir_along);
487                                 curr_mem = mem;
488                         }
489                 }
490
491                 in = (ir_node **) obstack_finish(obst);
492
493                 /* We need the sync only, if we didn't build the stores sequentially. */
494                 if(!do_seq)
495                         curr_mem = new_r_Sync(irg, bl, n_pos, in);
496                 obstack_free(obst, in);
497         }
498
499         /* Collect caller save registers */
500         for(i = 0, n = arch_isa_get_n_reg_class(isa); i < n; ++i) {
501                 int j;
502                 const arch_register_class_t *cls = arch_isa_get_reg_class(isa, i);
503                 for(j = 0; j < cls->n_regs; ++j) {
504                         const arch_register_t *reg = arch_register_for_index(cls, j);
505                         if(arch_register_type_is(reg, caller_save))
506                                 pset_insert_ptr(caller_save, (void *) reg);
507                 }
508         }
509
510         /* search the greatest result proj number */
511         foreach_out_edge(irn, edge) {
512                 const ir_edge_t *res_edge;
513                 ir_node *irn = get_edge_src_irn(edge);
514
515                 if(is_Proj(irn) && get_irn_mode(irn) == mode_T) {
516                         res_proj = irn;
517                         foreach_out_edge(irn, res_edge) {
518                                 int proj;
519                                 be_abi_call_arg_t *arg;
520                                 ir_node *res = get_edge_src_irn(res_edge);
521
522                                 assert(is_Proj(res));
523
524                                 proj = get_Proj_proj(res);
525                                 arg = get_call_arg(call, 1, proj);
526
527                                 /*
528                                         shift the proj number to the right, since we will drop the
529                                         unspeakable Proj_T from the Call. Therefore, all real argument
530                                         Proj numbers must be increased by pn_Call_max
531                                 */
532                                 proj += pn_Call_max;
533                                 set_Proj_proj(res, proj);
534                                 obstack_ptr_grow(obst, res);
535
536                                 if(proj > curr_res_proj)
537                                         curr_res_proj = proj;
538                                 if(arg->in_reg)
539                                         pset_remove_ptr(caller_save, arg->reg);
540                         }
541                 }
542         }
543         curr_res_proj++;
544         obstack_ptr_grow(obst, NULL);
545         res_projs = obstack_finish(obst);
546
547         /* make the back end call node and set its register requirements. */
548         for(i = 0; i < n_low_args; ++i)
549                 obstack_ptr_grow(obst, get_Call_param(irn, low_args[i]));
550
551         in = obstack_finish(obst);
552         if(env->call->flags.bits.call_has_imm && get_irn_opcode(call_ptr) == iro_SymConst) {
553                 low_call = be_new_Call(irg, bl, curr_mem, curr_sp, curr_sp, curr_res_proj, n_low_args, in);
554                 be_Call_set_entity(low_call, get_SymConst_entity(call_ptr));
555         }
556
557         else
558                 low_call = be_new_Call(irg, bl, curr_mem, curr_sp, call_ptr, curr_res_proj, n_low_args, in);
559
560         /* Set the register classes and constraints of the Call parameters. */
561         for(i = 0; i < n_low_args; ++i) {
562                 int index = low_args[i];
563                 be_abi_call_arg_t *arg = get_call_arg(call, 0, index);
564                 assert(arg->reg != NULL);
565                 be_set_constr_single_reg(low_call, index, arg->reg);
566         }
567
568         /* Set the register constraints of the results. */
569         for(i = 0; res_projs[i]; ++i) {
570                 ir_node *irn                 = res_projs[i];
571                 int proj                     = get_Proj_proj(irn);
572
573                 /* Correct Proj number since it has been adjusted! (see above) */
574                 const be_abi_call_arg_t *arg = get_call_arg(call, 1, proj - pn_Call_max);
575
576                 assert(arg->in_reg);
577                 be_set_constr_single_reg(low_call, BE_OUT_POS(proj), arg->reg);
578         }
579         obstack_free(obst, in);
580         exchange(irn, low_call);
581
582         /* redirect the result projs to the lowered call instead of the Proj_T */
583         for(i = 0; res_projs[i]; ++i)
584                 set_Proj_pred(res_projs[i], low_call);
585
586         /* Make additional projs for the caller save registers
587            and the Keep node which keeps them alive. */
588         if(pset_count(caller_save) > 0) {
589                 const arch_register_t *reg;
590                 ir_node **in, *keep;
591                 int i, n;
592
593                 for(reg = pset_first(caller_save), n = 0; reg; reg = pset_next(caller_save), ++n) {
594                         ir_node *proj = new_r_Proj(irg, bl, low_call, reg->reg_class->mode, curr_res_proj++);
595
596                         /* memorize the register in the link field. we need afterwards to set the register class of the keep correctly. */
597                         set_irn_link(proj, (void *) reg);
598                         obstack_ptr_grow(obst, proj);
599                 }
600
601                 in   = (ir_node **) obstack_finish(obst);
602                 keep = be_new_Keep(NULL, irg, bl, n, in);
603                 for(i = 0; i < n; ++i) {
604                         const arch_register_t *reg = get_irn_link(in[i]);
605                         be_node_set_reg_class(keep, i, reg->reg_class);
606                 }
607                 obstack_free(obst, in);
608         }
609
610         /* Clean up the stack. */
611         if(stack_size > 0) {
612                 ir_node *mem_proj = NULL;
613
614                 foreach_out_edge(low_call, edge) {
615                         ir_node *irn = get_edge_src_irn(edge);
616                         if(is_Proj(irn) && get_Proj_proj(irn) == pn_Call_M) {
617                                 mem_proj = irn;
618                                 break;
619                         }
620                 }
621
622                 if(!mem_proj)
623                         mem_proj = new_r_Proj(irg, bl, low_call, mode_M, pn_Call_M);
624
625                 /* Make a Proj for the stack pointer. */
626                 curr_sp     = be_new_IncSP(sp, irg, bl, curr_sp, mem_proj, stack_size, be_stack_dir_against);
627         }
628
629         be_abi_call_free(call);
630         obstack_free(obst, pos);
631         del_pset(results);
632         del_pset(caller_save);
633
634         return curr_sp;
635 }
636
637 /**
638  * Adjust an alloca.
639  * The alloca is transformed into a back end alloca node and connected to the stack nodes.
640  */
641 static ir_node *adjust_alloc(be_abi_irg_t *env, ir_node *alloc, ir_node *curr_sp)
642 {
643         if(get_Alloc_where(alloc) == stack_alloc) {
644                 ir_node *new_alloc;
645
646                 ir_node *bl   = get_nodes_block(alloc);
647                 ir_graph *irg = get_irn_irg(bl);
648
649                 new_alloc = be_new_Alloca(env->isa->sp, irg, bl, get_Alloc_mem(alloc), curr_sp, get_Alloc_size(alloc));
650                 exchange(alloc, new_alloc);
651                 curr_sp = new_r_Proj(irg, bl, new_alloc, mode_P, pn_Alloc_res);
652         }
653
654         return curr_sp;
655 }
656
657 /**
658  * Walker for dependent_on().
659  * This function searches a node tgt recursively from a given node
660  * but is restricted to the given block.
661  * @return 1 if tgt was reachable from curr, 0 if not.
662  */
663 static int check_dependence(ir_node *curr, ir_node *tgt, ir_node *bl, unsigned long visited_nr)
664 {
665         int n, i;
666
667         if(get_irn_visited(curr) >= visited_nr)
668                 return 0;
669
670         set_irn_visited(curr, visited_nr);
671         if(get_nodes_block(curr) != bl)
672                 return 0;
673
674         if(curr == tgt)
675                 return 1;
676
677         for(i = 0, n = get_irn_arity(curr); i < n; ++i) {
678                 if(check_dependence(get_irn_n(curr, i), tgt, bl, visited_nr))
679                         return 1;
680         }
681
682         return 0;
683 }
684
685 /**
686  * Check if a node is somehow data dependent on another one.
687  * both nodes must be in the same basic block.
688  * @param n1 The first node.
689  * @param n2 The second node.
690  * @return 1, if n1 is data dependent (transitively) on n2, 0 if not.
691  */
692 static int dependent_on(ir_node *n1, ir_node *n2)
693 {
694         ir_node *bl   = get_nodes_block(n1);
695         ir_graph *irg = get_irn_irg(bl);
696         long vis_nr   = get_irg_visited(irg) + 1;
697
698         assert(bl == get_nodes_block(n2));
699         set_irg_visited(irg, vis_nr);
700         return check_dependence(n1, n2, bl, vis_nr);
701 }
702
703 static int cmp_call_dependecy(const void *c1, const void *c2)
704 {
705         ir_node *n1 = *(ir_node **) c1;
706         ir_node *n2 = *(ir_node **) c2;
707
708         /*
709                 Classical qsort() comparison function behavior:
710                 0  if both elements are equal
711                 1  if second is "smaller" that first
712                 -1 if first is "smaller" that second
713         */
714         return n1 == n2 ? 0 : (dependent_on(n1, n2) ? -1 : 1);
715 }
716
717 static void link_calls_in_block_walker(ir_node *irn, void *data)
718 {
719         if(is_Call(irn)) {
720                 ir_node *bl = get_nodes_block(irn);
721                 void *save  = get_irn_link(bl);
722
723                 set_irn_link(irn, save);
724                 set_irn_link(bl, irn);
725         }
726 }
727
728 /**
729  * Process all call nodes inside a basic block.
730  * Note that the link field of the block must contain a linked list of all
731  * Call nodes inside the block. We first order this list according to data dependency
732  * and that connect the calls together.
733  */
734 static void process_calls_in_block(ir_node *bl, void *data)
735 {
736         be_abi_irg_t *env = data;
737         ir_node *curr_sp  = env->init_sp;
738         ir_node *irn;
739         int n;
740
741         for(irn = get_irn_link(bl), n = 0; irn; irn = get_irn_link(irn), ++n)
742                 obstack_ptr_grow(&env->obst, irn);
743
744         /* If there were call nodes in the block. */
745         if(n > 0) {
746                 ir_node **nodes;
747                 int i;
748
749                 nodes = obstack_finish(&env->obst);
750
751                 /* order the call nodes according to data dependency */
752                 qsort(nodes, n, sizeof(nodes[0]), cmp_call_dependecy);
753
754                 for(i = n - 1; i >= 0; --i) {
755                         ir_node *irn = nodes[i];
756
757                         switch(get_irn_opcode(irn)) {
758                         case iro_Call:
759                                 curr_sp = adjust_call(env, irn, curr_sp);
760                                 break;
761                         case iro_Alloc:
762                                 curr_sp = adjust_alloc(env, irn, curr_sp);
763                                 break;
764                         default:
765                                 break;
766                         }
767                 }
768
769                 obstack_free(&env->obst, nodes);
770
771                 /* Keep the last stack state in the block by tying it to Keep node */
772                 nodes[0] = curr_sp;
773                 be_new_Keep(env->isa->sp->reg_class, get_irn_irg(bl), bl, 1, nodes);
774         }
775
776         set_irn_link(bl, curr_sp);
777 }
778
779 /**
780  * Adjust all call nodes in the graph to the ABI conventions.
781  */
782 static void process_calls(be_abi_irg_t *env)
783 {
784         ir_graph *irg = env->birg->irg;
785
786         irg_walk_graph(irg, firm_clear_link, link_calls_in_block_walker, NULL);
787         irg_block_walk_graph(irg, NULL, process_calls_in_block, env);
788 }
789
790 #if 0
791 /**
792  * Walker to implement alloca-style allocations.
793  * They are implemented using an add to the stack pointer
794  * and a copy instruction.
795  */
796 static void implement_stack_alloc(be_abi_irg_t *env, ir_node *irn)
797 {
798         const arch_isa_t *isa = env->birg->main_env->arch_env->isa;
799         ir_node *bl           = get_nodes_block(irn);
800         ir_node *res          = env->init_sp;
801         ir_node *size;
802
803         assert(get_irn_opcode(irn) == iro_Alloc && get_Alloc_where(irn) == stack_alloc);
804
805         size = get_Alloc_size(irn);
806         if(isa->stack_dir > 0)
807                 res = be_new_Copy(isa->sp->reg_class, env->birg->irg, bl, res);
808
809         res = be_new_AddSP(isa->sp, env->birg->irg, bl, res, size);
810
811         if(isa->stack_dir < 0)
812                 res = be_new_Copy(isa->sp->reg_class, env->birg->irg, bl, res);
813
814 }
815 #endif
816
817 static void collect_return_walker(ir_node *irn, void *data)
818 {
819         if(get_irn_opcode(irn) == iro_Return) {
820                 struct obstack *obst = data;
821                 obstack_ptr_grow(obst, irn);
822         }
823 }
824
825 static ir_node *setup_frame(be_abi_irg_t *env)
826 {
827         const arch_isa_t *isa = env->birg->main_env->arch_env->isa;
828         const arch_register_t *sp = isa->sp;
829         const arch_register_t *bp = isa->bp;
830         be_abi_call_flags_bits_t flags = env->call->flags.bits;
831         ir_graph *irg      = env->birg->irg;
832         ir_node *bl        = get_irg_start_block(irg);
833         ir_node *no_mem    = get_irg_no_mem(irg);
834         ir_node *old_frame = get_irg_frame(irg);
835         ir_node *stack     = pmap_get(env->regs, (void *) sp);
836         ir_node *frame     = pmap_get(env->regs, (void *) bp);
837
838         int stack_nr       = get_Proj_proj(stack);
839
840         if(flags.try_omit_fp) {
841                 stack = be_new_IncSP(sp, irg, bl, stack, no_mem, BE_STACK_FRAME_SIZE, be_stack_dir_along);
842                 frame = stack;
843         }
844
845         else {
846                 frame = be_new_Copy(bp->reg_class, irg, bl, stack);
847
848                 be_node_set_flags(frame, -1, arch_irn_flags_dont_spill);
849                 if(!flags.fp_free) {
850                         be_set_constr_single_reg(frame, -1, bp);
851                         be_node_set_flags(frame, -1, arch_irn_flags_ignore);
852                         arch_set_irn_register(env->birg->main_env->arch_env, frame, bp);
853                 }
854
855                 stack = be_new_IncSP(sp, irg, bl, stack, frame, BE_STACK_FRAME_SIZE, be_stack_dir_along);
856         }
857
858         be_node_set_flags(env->reg_params, -(stack_nr + 1), arch_irn_flags_ignore);
859         env->init_sp = stack;
860         set_irg_frame(irg, frame);
861         edges_reroute(old_frame, frame, irg);
862
863         return frame;
864 }
865
866 static void clearup_frame(be_abi_irg_t *env, ir_node *ret, struct obstack *obst)
867 {
868         const arch_isa_t *isa = env->birg->main_env->arch_env->isa;
869         const arch_register_t *sp = isa->sp;
870         const arch_register_t *bp = isa->bp;
871         ir_graph *irg      = env->birg->irg;
872         ir_node *ret_mem   = get_Return_mem(ret);
873         ir_node *frame     = get_irg_frame(irg);
874         ir_node *bl        = get_nodes_block(ret);
875         ir_node *stack     = get_irn_link(bl);
876
877         pmap_entry *ent;
878
879         if(env->call->flags.bits.try_omit_fp) {
880                 stack = be_new_IncSP(sp, irg, bl, stack, ret_mem, BE_STACK_FRAME_SIZE, be_stack_dir_against);
881         }
882
883         else {
884                 stack = be_new_SetSP(sp, irg, bl, stack, frame, ret_mem);
885                 be_set_constr_single_reg(stack, -1, sp);
886                 be_node_set_flags(stack, -1, arch_irn_flags_ignore);
887         }
888
889         pmap_foreach(env->regs, ent) {
890                 const arch_register_t *reg = ent->key;
891                 ir_node *irn               = ent->value;
892
893                 if(reg == sp)
894                         irn = stack;
895                 else if(reg == bp)
896                         irn = frame;
897
898                 obstack_ptr_grow(obst, irn);
899         }
900 }
901
902 static ir_type *compute_arg_type(be_abi_irg_t *env, be_abi_call_t *call, ir_type *method_type)
903 {
904         int dir  = env->call->flags.bits.left_to_right ? 1 : -1;
905         int inc  = env->birg->main_env->arch_env->isa->stack_dir * dir;
906         int n    = get_method_n_params(method_type);
907         int curr = inc > 0 ? 0 : n - 1;
908         int ofs  = 0;
909
910         char buf[128];
911         ir_type *res;
912         int i;
913
914         snprintf(buf, sizeof(buf), "%s_arg_type", get_entity_name(get_irg_entity(env->birg->irg)));
915         res = new_type_class(new_id_from_str(buf));
916
917         for(i = 0; i < n; ++i, curr += inc) {
918                 type *param_type       = get_method_param_type(method_type, curr);
919                 be_abi_call_arg_t *arg = get_call_arg(call, 0, curr);
920
921                 if(!arg->in_reg) {
922                         snprintf(buf, sizeof(buf), "param_%d", i);
923                         arg->stack_ent = new_entity(res, new_id_from_str(buf), param_type);
924                         set_entity_offset_bytes(arg->stack_ent, ofs);
925                         ofs += get_type_size_bytes(param_type);
926                 }
927         }
928
929         set_type_size_bytes(res, ofs);
930         return res;
931 }
932
933 /**
934  * Modify the irg itself and the frame type.
935  */
936 static void modify_irg(be_abi_irg_t *env)
937 {
938         firm_dbg_module_t *dbg    = env->dbg;
939         be_abi_call_t *call       = be_abi_call_new();
940         const arch_isa_t *isa     = env->birg->main_env->arch_env->isa;
941         const arch_register_t *sp = arch_isa_sp(isa);
942         ir_graph *irg             = env->birg->irg;
943         ir_node *bl               = get_irg_start_block(irg);
944         ir_node *end              = get_irg_end_block(irg);
945         ir_node *arg_tuple        = get_irg_args(irg);
946         ir_node *no_mem           = get_irg_no_mem(irg);
947         type *method_type         = get_entity_type(get_irg_entity(irg));
948         int n_params              = get_method_n_params(method_type);
949         int max_arg               = 0;
950         int reg_params_nr         = 0;
951         int arg_offset            = 0;
952
953         int i, j, n;
954
955         ir_node *frame_pointer;
956         ir_node *reg_params_bl;
957         ir_node **args, **args_repl;
958         const ir_edge_t *edge;
959         ir_type *arg_type;
960
961         pmap_entry *ent;
962
963         DBG((dbg, LEVEL_1, "introducing abi on %+F\n", irg));
964
965         /* Convert the Sel nodes in the irg to frame load/store/addr nodes. */
966         irg_walk_graph(irg, lower_frame_sels_walker, NULL, env);
967
968         env->frame = obstack_alloc(&env->obst, sizeof(env->frame[0]));
969         env->regs  = pmap_create();
970
971         /* Find the maximum proj number of the argument tuple proj */
972         foreach_out_edge(arg_tuple, edge)  {
973                 ir_node *irn = get_edge_src_irn(edge);
974                 int nr       = get_Proj_proj(irn);
975                 max_arg      = MAX(max_arg, nr);
976         }
977         max_arg += 1;
978         args      = obstack_alloc(&env->obst, max_arg * sizeof(args[0]));
979         args_repl = obstack_alloc(&env->obst, max_arg * sizeof(args[0]));
980         memset(args, 0, max_arg * sizeof(args[0]));
981         memset(args_repl, 0, max_arg * sizeof(args[0]));
982
983         /* Fill the argument vector */
984         foreach_out_edge(arg_tuple, edge) {
985                 ir_node *irn = get_edge_src_irn(edge);
986                 int nr       = get_Proj_proj(irn);
987                 args[nr]     = irn;
988                 DBG((dbg, LEVEL_2, "\treading arg: %d -> %+F\n", nr, irn));
989         }
990
991         /* Get the ABI constraints from the ISA */
992         arch_isa_get_call_abi(isa, method_type, call);
993
994         arg_type     = compute_arg_type(env, call, method_type);
995         stack_frame_init(env->frame, arg_type, call->between_type, get_irg_frame_type(irg), isa->stack_dir);
996
997         /* Count the register params and add them to the number of Projs for the RegParams node */
998         for(i = 0; i < n_params; ++i) {
999                 be_abi_call_arg_t *arg = get_call_arg(call, 0, i);
1000                 if(arg->in_reg) {
1001                         assert(arg->reg != sp && "cannot use stack pointer as parameter register");
1002                         pmap_insert(env->regs, (void *) arg->reg, NULL);
1003                         DBG((dbg, LEVEL_2, "\targ #%d -> reg %s\n", i, arg->reg->name));
1004                 }
1005         }
1006
1007         /* Collect all callee-save registers */
1008         for(i = 0, n = arch_isa_get_n_reg_class(isa); i < n; ++i) {
1009                 const arch_register_class_t *cls = arch_isa_get_reg_class(isa, i);
1010                 for(j = 0; j < cls->n_regs; ++j) {
1011                         const arch_register_t *reg = &cls->regs[j];
1012                         if(arch_register_type_is(reg, callee_save))
1013                                 pmap_insert(env->regs, (void *) reg, NULL);
1014                 }
1015         }
1016
1017         pmap_insert(env->regs, (void *) sp, NULL);
1018         pmap_insert(env->regs, (void *) isa->bp, NULL);
1019         reg_params_nr   = 0;
1020         reg_params_bl   = get_irg_start_block(irg);
1021         env->reg_params = be_new_RegParams(irg, reg_params_bl, pmap_count(env->regs));
1022
1023         /*
1024          * make proj nodes for the callee save registers.
1025          * memorize them, since Return nodes get those as inputs.
1026          */
1027         for(ent = pmap_first(env->regs); ent; ent = pmap_next(env->regs)) {
1028                 arch_register_t *reg = ent->key;
1029                 int pos = -(reg_params_nr + 1);
1030                 ent->value = new_r_Proj(irg, reg_params_bl, env->reg_params, reg->reg_class->mode, reg_params_nr);
1031                 be_set_constr_single_reg(env->reg_params, pos, reg);
1032                 arch_set_irn_register(env->birg->main_env->arch_env, ent->value, reg);
1033
1034                 /*
1035                  * If the register is an ignore register,
1036                  * The Proj for that register shall also be ignored during register allocation.
1037                  */
1038                 if(arch_register_type_is(reg, ignore))
1039                         be_node_set_flags(env->reg_params, pos, arch_irn_flags_ignore);
1040
1041                 reg_params_nr++;
1042
1043                 DBG((dbg, LEVEL_2, "\tregister save proj #%d -> reg %s\n", reg_params_nr - 1, reg->name));
1044         }
1045
1046         /* Insert the code to set up the stack frame */
1047         frame_pointer = setup_frame(env);
1048
1049         /* Now, introduce stack param nodes for all parameters passed on the stack */
1050         for(i = 0; i < max_arg; ++i) {
1051                 ir_node *arg_proj = args[i];
1052                 if(arg_proj != NULL) {
1053                         be_abi_call_arg_t *arg;
1054                         ir_type *param_type;
1055                         int nr = get_Proj_proj(arg_proj);
1056
1057                         nr         = MIN(nr, n_params);
1058                         arg        = get_call_arg(call, 0, nr);
1059                         param_type = get_method_param_type(method_type, nr);
1060
1061                         if(arg->in_reg) {
1062                                 args_repl[i] = new_r_Proj(irg, reg_params_bl, env->reg_params, get_irn_mode(arg_proj), reg_params_nr);
1063                                 be_set_constr_single_reg(env->reg_params, -(reg_params_nr + 1), arg->reg);
1064                                 reg_params_nr++;
1065                         }
1066
1067                         /* when the (stack) parameter is primitive, we insert a StackParam
1068                         node representing the load of that parameter */
1069                         else {
1070
1071                                 /* For atomic parameters which are actually used, we create a StackParam node. */
1072                                 if(is_atomic_type(param_type) && get_irn_n_edges(args[i]) > 0) {
1073                                         ir_mode *mode                    = get_type_mode(param_type);
1074                                         const arch_register_class_t *cls = arch_isa_get_reg_class_for_mode(isa, mode);
1075                                         args_repl[i] = be_new_StackParam(cls, isa->bp->reg_class, irg, reg_params_bl, mode, frame_pointer, arg->stack_ent);
1076                                 }
1077
1078                                 /* The stack parameter is not primitive (it is a struct or array),
1079                                 we thus will create a node representing the parameter's address
1080                                 on the stack. */
1081                                 else {
1082                                         args_repl[i] = be_new_FrameAddr(sp->reg_class, irg, reg_params_bl, frame_pointer, arg->stack_ent);
1083                                 }
1084                         }
1085                 }
1086         }
1087
1088         /* reroute the edges from the original argument projs to the RegParam ones. */
1089         for(i = 0; i < max_arg; ++i) {
1090                 if(args[i] != NULL) {
1091                         assert(args_repl[i] != NULL);
1092                         edges_reroute(args[i], args_repl[i], irg);
1093                 }
1094         }
1095
1096         /* All Return nodes hang on the End node, so look for them there. */
1097         for(i = 0, n = get_irn_arity(end); i < n; ++i) {
1098                 ir_node *irn = get_irn_n(end, i);
1099
1100                 if(get_irn_opcode(irn) == iro_Return) {
1101                         ir_node *bl   = get_nodes_block(irn);
1102                         int n_res     = get_Return_n_ress(irn);
1103                         pmap *reg_map = pmap_create_ex(n_res);
1104                         ir_node *ret;
1105                         int i, n;
1106                         ir_node **in;
1107
1108                         /* collect all arguments of the return */
1109                         obstack_ptr_grow(&env->obst, get_Return_mem(irn));
1110                         for(i = 0; i < n_res; ++i) {
1111                                 ir_node *res           = get_Return_res(irn, i);
1112                                 be_abi_call_arg_t *arg = get_call_arg(call, 1, i);
1113
1114                                 assert(arg->in_reg && "return value must be passed in register");
1115                                 pmap_insert(reg_map, res, (void *) arg->reg);
1116                                 obstack_ptr_grow(&env->obst, res);
1117                         }
1118
1119                         /* generate the clean up code and add additional parameters to the return. */
1120                         clearup_frame(env, irn, &env->obst);
1121
1122                         /* The in array for the new back end return is now ready. */
1123                         n   = obstack_object_size(&env->obst) / sizeof(in[0]);
1124                         in  = obstack_finish(&env->obst);
1125                         ret = be_new_Return(irg, bl, n, in);
1126
1127                         /* Set the constraints for some arguments of the return. */
1128                         for(i = 0; i < n; i++) {
1129                                 const arch_register_t *reg = pmap_get(reg_map, in[i]);
1130                                 if(reg != NULL)
1131                                         be_set_constr_single_reg(ret, i, reg);
1132                         }
1133                         exchange(irn, ret);
1134                         obstack_free(&env->obst, in);
1135                         pmap_destroy(reg_map);
1136                 }
1137         }
1138
1139         obstack_free(&env->obst, args);
1140         be_abi_call_free(call);
1141 }
1142
1143 /**
1144  * Walker: puts all Alloc(stack_alloc) on a obstack
1145  */
1146 static void collect_alloca_walker(ir_node *irn, void *data)
1147 {
1148         be_abi_irg_t *env = data;
1149         if(get_irn_opcode(irn) == iro_Alloc && get_Alloc_where(irn) == stack_alloc)
1150                 obstack_ptr_grow(&env->obst, irn);
1151 }
1152
1153 be_abi_irg_t *be_abi_introduce(be_irg_t *birg)
1154 {
1155         pmap_entry *ent;
1156         be_abi_irg_t *env = malloc(sizeof(env[0]));
1157
1158         ir_node *dummy;
1159
1160         env->isa           = birg->main_env->arch_env->isa;
1161         env->method_type   = get_entity_type(get_irg_entity(birg->irg));
1162         env->call          = be_abi_call_new();
1163         arch_isa_get_call_abi(env->isa, env->method_type, env->call);
1164
1165         env->dce_survivor     = new_survive_dce();
1166         env->birg             = birg;
1167         env->dbg              = firm_dbg_register("firm.be.abi");
1168         env->stack_phis       = pset_new_ptr(16);
1169         env->init_sp = dummy  = new_r_Unknown(birg->irg, env->isa->sp->reg_class->mode);
1170
1171         obstack_init(&env->obst);
1172
1173         memcpy(&env->irn_handler, &abi_irn_handler, sizeof(abi_irn_handler));
1174         env->irn_ops.impl = &abi_irn_ops;
1175
1176         /* Lower all call nodes in the IRG. */
1177         process_calls(env);
1178
1179         /* Process the IRG */
1180         modify_irg(env);
1181
1182         /* reroute the stack origin of the calls to the true stack origin. */
1183         exchange(dummy, env->init_sp);
1184
1185         /* Make some important node pointers survive the dead node elimination. */
1186         survive_dce_register_irn(env->dce_survivor, &env->init_sp);
1187
1188         pmap_foreach(env->regs, ent)
1189                 survive_dce_register_irn(env->dce_survivor, (ir_node **) &ent->value);
1190
1191         arch_env_push_irn_handler(env->birg->main_env->arch_env, &env->irn_handler);
1192
1193         return env;
1194 }
1195
1196 void be_abi_free(be_abi_irg_t *env)
1197 {
1198         free_survive_dce(env->dce_survivor);
1199         del_pset(env->stack_phis);
1200         pmap_destroy(env->regs);
1201         obstack_free(&env->obst, NULL);
1202         arch_env_pop_irn_handler(env->birg->main_env->arch_env);
1203         free(env);
1204 }
1205
1206
1207 /*
1208
1209   _____ _        ____  _             _
1210  |  ___(_)_  __ / ___|| |_ __ _  ___| | __
1211  | |_  | \ \/ / \___ \| __/ _` |/ __| |/ /
1212  |  _| | |>  <   ___) | || (_| | (__|   <
1213  |_|   |_/_/\_\ |____/ \__\__,_|\___|_|\_\
1214
1215 */
1216
1217 static void collect_stack_nodes_walker(ir_node *irn, void *data)
1218 {
1219         pset *s = data;
1220
1221         if((is_Proj(irn) && be_is_Alloca(get_Proj_pred(irn)) && get_Proj_proj(irn) == pn_Alloc_res)
1222                 || be_is_IncSP(irn)
1223                 || be_is_SetSP(irn))
1224                 pset_insert_ptr(s, irn);
1225 }
1226
1227 void be_abi_fix_stack_nodes(be_abi_irg_t *env)
1228 {
1229         dom_front_info_t *df;
1230         pset *stack_nodes;
1231
1232         /* We need dominance frontiers for fix up */
1233         df = be_compute_dominance_frontiers(env->birg->irg);
1234         stack_nodes = pset_new_ptr(16);
1235         pset_insert_ptr(stack_nodes, env->init_sp);
1236         irg_walk_graph(env->birg->irg, collect_stack_nodes_walker, NULL, stack_nodes);
1237         be_ssa_constr_set_phis(df, stack_nodes, env->stack_phis);
1238         del_pset(stack_nodes);
1239
1240         /* free these dominance frontiers */
1241         be_free_dominance_frontiers(df);
1242 }
1243
1244 /**
1245  * Translates a direction of an IncSP node (either be_stack_dir_against, or ...along)
1246  * into -1 or 1, respectively.
1247  * @param irn The node.
1248  * @return 1, if the direction of the IncSP was along, -1 if against.
1249  */
1250 static int get_dir(ir_node *irn)
1251 {
1252         return 1 - 2 * (be_get_IncSP_direction(irn) == be_stack_dir_against);
1253 }
1254
1255 static int process_stack_bias(be_abi_irg_t *env, ir_node *bl, int bias)
1256 {
1257         const arch_env_t *aenv = env->birg->main_env->arch_env;
1258         ir_node *irn;
1259         int start_bias = bias;
1260         int omit_fp    = env->call->flags.bits.try_omit_fp;
1261
1262         sched_foreach(bl, irn) {
1263
1264                 /*
1265                         If the node modifies the stack pointer by a constant offset,
1266                         record that in the bias.
1267                 */
1268                 if(be_is_IncSP(irn)) {
1269                         int ofs = be_get_IncSP_offset(irn);
1270                         int dir = get_dir(irn);
1271
1272                         if(ofs == BE_STACK_FRAME_SIZE) {
1273                                 ofs = get_type_size_bytes(get_irg_frame_type(env->birg->irg));
1274                                 be_set_IncSP_offset(irn, ofs);
1275                         }
1276
1277                         if(omit_fp)
1278                                 bias += dir * ofs;
1279                 }
1280
1281                 /*
1282                         Else check, if the node relates to an entity on the stack frame.
1283                         If so, set the true offset (including the bias) for that
1284                         node.
1285                 */
1286                 else {
1287                         entity *ent = arch_get_frame_entity(aenv, irn);
1288                         if(ent) {
1289                                 int offset = get_stack_entity_offset(env->frame, ent, bias);
1290                                 arch_set_frame_offset(aenv, irn, offset);
1291                                 DBG((env->dbg, LEVEL_2, "%F has offset %d\n", ent, offset));
1292                         }
1293                 }
1294         }
1295
1296         return bias;
1297 }
1298
1299 /**
1300  * A helper struct for the bias walker.
1301  */
1302 struct bias_walk {
1303         be_abi_irg_t *env;     /**< The ABI irg environment. */
1304         int start_block_bias;  /**< The bias at the end of the start block. */
1305 };
1306
1307 static void stack_bias_walker(ir_node *bl, void *data)
1308 {
1309         if(bl != get_irg_start_block(get_irn_irg(bl))) {
1310                 struct bias_walk *bw = data;
1311                 process_stack_bias(bw->env, bl, bw->start_block_bias);
1312         }
1313 }
1314
1315 void be_abi_fix_stack_bias(be_abi_irg_t *env)
1316 {
1317         ir_graph *irg  = env->birg->irg;
1318         struct bias_walk bw;
1319
1320         stack_frame_compute_initial_offset(env->frame);
1321         // stack_frame_dump(stdout, env->frame);
1322
1323         /* Determine the stack bias at the and of the start block. */
1324         bw.start_block_bias = process_stack_bias(env, get_irg_start_block(irg), 0);
1325
1326         /* fix the bias is all other blocks */
1327         bw.env = env;
1328         irg_block_walk_graph(irg, stack_bias_walker, NULL, &bw);
1329 }
1330
1331 ir_node *be_abi_get_callee_save_irn(be_abi_irg_t *abi, const arch_register_t *reg)
1332 {
1333         assert(arch_register_type_is(reg, callee_save));
1334         assert(pmap_contains(abi->regs, (void *) reg));
1335         return pmap_get(abi->regs, (void *) reg);
1336 }
1337
1338 /*
1339   _____ _____  _   _   _    _                 _ _
1340  |_   _|  __ \| \ | | | |  | |               | | |
1341    | | | |__) |  \| | | |__| | __ _ _ __   __| | | ___ _ __
1342    | | |  _  /| . ` | |  __  |/ _` | '_ \ / _` | |/ _ \ '__|
1343   _| |_| | \ \| |\  | | |  | | (_| | | | | (_| | |  __/ |
1344  |_____|_|  \_\_| \_| |_|  |_|\__,_|_| |_|\__,_|_|\___|_|
1345
1346   for Phi nodes which are created due to stack modifying nodes
1347   such as IncSP, AddSP and SetSP.
1348
1349   These Phis are always to be ignored by the reg alloc and are
1350   fixed on the SP register of the ISA.
1351 */
1352
1353 static const void *abi_get_irn_ops(const arch_irn_handler_t *handler, const ir_node *irn)
1354 {
1355         const be_abi_irg_t *abi = get_abi_from_handler(handler);
1356         return is_Phi(irn) && pset_find_ptr(abi->stack_phis, (void *) irn) != NULL ? &abi->irn_ops : NULL;
1357 }
1358
1359 static void be_abi_limited(void *data, bitset_t *bs)
1360 {
1361         be_abi_irg_t *abi = data;
1362         bitset_clear_all(bs);
1363         bitset_set(bs, abi->isa->sp->index);
1364 }
1365
1366 static const arch_register_req_t *abi_get_irn_reg_req(const void *self, arch_register_req_t *req, const ir_node *irn, int pos)
1367 {
1368         be_abi_irg_t *abi          = get_abi_from_ops(self);
1369         const arch_register_t *reg = abi->isa->sp;
1370
1371         req->cls         = reg->reg_class;
1372         req->type        = arch_register_req_type_limited;
1373         req->limited     = be_abi_limited;
1374         req->limited_env = abi;
1375         return req;
1376 }
1377
1378 static void abi_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg)
1379 {
1380 }
1381
1382 static const arch_register_t *abi_get_irn_reg(const void *self, const ir_node *irn)
1383 {
1384         const be_abi_irg_t *abi = get_abi_from_ops(self);
1385         return abi->isa->sp;
1386 }
1387
1388 static arch_irn_class_t abi_classify(const void *_self, const ir_node *irn)
1389 {
1390         return arch_irn_class_normal;
1391 }
1392
1393 static arch_irn_flags_t abi_get_flags(const void *_self, const ir_node *irn)
1394 {
1395         return arch_irn_flags_ignore;
1396 }
1397
1398 static entity *abi_get_frame_entity(const void *_self, const ir_node *irn)
1399 {
1400         return NULL;
1401 }
1402
1403 static void abi_set_stack_bias(const void *_self, ir_node *irn, int bias)
1404 {
1405 }
1406
1407 static const arch_irn_ops_if_t abi_irn_ops = {
1408         abi_get_irn_reg_req,
1409         abi_set_irn_reg,
1410         abi_get_irn_reg,
1411         abi_classify,
1412         abi_get_flags,
1413         abi_get_frame_entity,
1414         abi_set_stack_bias
1415 };
1416
1417 static const arch_irn_handler_t abi_irn_handler = {
1418         abi_get_irn_ops
1419 };