4 * @author Sebastian Hack
14 #include "survive_dce.h"
16 #include "irgraph_t.h"
19 #include "iredges_t.h"
22 #include "irprintf_t.h"
28 #include "besched_t.h"
30 #define MAX(x, y) ((x) > (y) ? (x) : (y))
31 #define MIN(x, y) ((x) < (y) ? (x) : (y))
33 typedef struct _be_abi_call_arg_t {
38 const arch_register_t *reg;
42 struct _be_abi_call_t {
43 be_abi_call_flags_t flags;
48 #define N_FRAME_TYPES 3
50 typedef struct _be_stack_frame_t {
55 type *order[N_FRAME_TYPES]; /**< arg, between and frame types ordered. */
61 struct _be_stack_slot_t {
62 struct _be_stack_frame_t *frame;
66 struct _be_abi_irg_t {
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;
74 be_abi_call_t *call; /**< The ABI call information. */
75 type *method_type; /**< The type of the method of the IRG. */
77 ir_node *init_sp; /**< The node representing the stack pointer
78 at the start of the function. */
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. */
84 pset *stack_phis; /**< The set of all Phi nodes inserted due to
85 stack pointer modifying nodes. */
87 int start_block_bias; /**< The stack bias at the end of the start block. */
89 arch_irn_handler_t irn_handler;
90 arch_irn_ops_t irn_ops;
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)
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;
103 _ ____ ___ ____ _ _ _ _
104 / \ | __ )_ _| / ___|__ _| | | |__ __ _ ___| | _____
105 / _ \ | _ \| | | | / _` | | | '_ \ / _` |/ __| |/ / __|
106 / ___ \| |_) | | | |__| (_| | | | |_) | (_| | (__| <\__ \
107 /_/ \_\____/___| \____\__,_|_|_|_.__/ \__,_|\___|_|\_\___/
109 These callbacks are used by the backend to set the parameters
110 for a specific call type.
113 static int cmp_call_arg(const void *a, const void *b, size_t n)
115 const be_abi_call_arg_t *p = a, *q = b;
116 return !(p->is_res == q->is_res && p->pos == q->pos);
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)
121 be_abi_call_arg_t arg;
127 hash = is_res * 100 + pos;
130 ? set_insert(call->params, &arg, sizeof(arg), hash)
131 : set_find(call->params, &arg, sizeof(arg), hash);
134 static INLINE be_abi_call_arg_t *get_call_arg(be_abi_call_t *call, int is_res, int pos)
136 return get_or_set_call_arg(call, is_res, pos, 0);
139 void be_abi_call_set_flags(be_abi_call_t *call, be_abi_call_flags_t flags, ir_type *between_type)
142 call->between_type = between_type;
145 void be_abi_call_param_stack(be_abi_call_t *call, int arg_pos)
147 be_abi_call_arg_t *arg = get_or_set_call_arg(call, 0, arg_pos, 1);
150 void be_abi_call_param_reg(be_abi_call_t *call, int arg_pos, const arch_register_t *reg)
152 be_abi_call_arg_t *arg = get_or_set_call_arg(call, 0, arg_pos, 1);
157 void be_abi_call_res_reg(be_abi_call_t *call, int arg_pos, const arch_register_t *reg)
159 be_abi_call_arg_t *arg = get_or_set_call_arg(call, 1, arg_pos, 1);
164 be_abi_call_t *be_abi_call_new(void)
166 be_abi_call_t *call = malloc(sizeof(call[0]));
168 call->params = new_set(cmp_call_arg, 16);
172 void be_abi_call_free(be_abi_call_t *call)
174 del_set(call->params);
180 | ___| __ __ _ _ __ ___ ___ | | | | __ _ _ __ __| | (_)_ __ __ _
181 | |_ | '__/ _` | '_ ` _ \ / _ \ | |_| |/ _` | '_ \ / _` | | | '_ \ / _` |
182 | _|| | | (_| | | | | | | __/ | _ | (_| | | | | (_| | | | | | | (_| |
183 |_| |_| \__,_|_| |_| |_|\___| |_| |_|\__,_|_| |_|\__,_|_|_|_| |_|\__, |
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
195 static int get_stack_entity_offset(be_stack_frame_t *frame, entity *ent, int bias)
197 type *t = get_entity_owner(ent);
198 int ofs = get_entity_offset_bytes(ent);
202 /* Find the type the entity is contained in. */
203 for(index = 0; index < N_FRAME_TYPES; ++index) {
204 if(frame->order[index] == t)
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]);
212 /* correct the offset by the initial position of the frame pointer */
213 ofs -= frame->initial_offset;
215 /* correct the offset with the current bias. */
221 static entity *search_ent_with_offset(type *t, int offset)
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)
234 static int stack_frame_compute_initial_offset(be_stack_frame_t *frame)
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;
243 static be_stack_frame_t *stack_frame_init(be_stack_frame_t *frame, type *args, type *between, type *locals, int stack_dir)
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;
253 frame->order[0] = args;
254 frame->order[2] = locals;
258 frame->order[0] = locals;
259 frame->order[2] = args;
265 static void stack_frame_dump(FILE *file, be_stack_frame_t *frame)
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];
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));
282 * If irn is a Sel node computing the address of an entity
283 * on the frame type return the entity, else NULL.
285 static INLINE entity *get_sel_ent(ir_node *irn)
287 if(get_irn_opcode(irn) == iro_Sel
288 && get_Sel_ptr(irn) == get_irg_frame(get_irn_irg(irn))) {
290 return get_Sel_entity(irn);
297 * Walker: Replaces Loads, Stores and Sels of frame type entities
298 * by FrameLoad, FrameStore and FrameAdress.
300 static void lower_frame_sels_walker(ir_node *irn, void *data)
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);
308 opcode opc = get_irn_opcode(irn);
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));
316 nw = be_new_FrameLoad(isa->sp->reg_class, cls, irg, bl, get_Load_mem(irn), frame, ent);
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));
326 nw = be_new_FrameStore(isa->sp->reg_class, cls, irg, bl, get_Store_mem(irn), frame, val, ent);
330 entity *ent = get_sel_ent(irn);
332 ir_node *bl = get_nodes_block(irn);
333 nw = be_new_FrameAddr(isa->sp->reg_class, irg, bl, frame, ent);
341 static INLINE int is_on_stack(be_abi_call_t *call, int pos)
343 be_abi_call_arg_t *arg = get_call_arg(call, 0, pos);
344 return arg && !arg->in_reg;
354 Adjustment of the calls inside a graph.
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.
365 static ir_node *adjust_call(be_abi_irg_t *env, ir_node *irn, ir_node *curr_sp)
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);
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);
384 ir_node *res_proj = NULL;
385 int curr_res_proj = pn_Call_max;
392 const ir_edge_t *edge;
397 /* Let the isa fill out the abi description for that call node. */
398 arch_isa_get_call_abi(isa, mt, call);
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);
406 stack_size += get_type_size_bytes(get_method_param_type(mt, i));
407 obstack_int_grow(obst, i);
411 pos = obstack_finish(obst);
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);
421 low_args = obstack_finish(obst);
423 /* If there are some parameters which shall be passed on the stack. */
426 int do_seq = call->flags.bits.store_args_sequential;
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;
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.
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);
447 assert(mode_is_reference(mach_mode) && "machine mode must be pointer");
448 for(i = 0; i < n_pos; ++i) {
450 ir_node *param = get_Call_param(irn, p);
451 ir_node *addr = curr_sp;
453 type *param_type = get_method_param_type(mt, p);
454 int param_size = get_type_size_bytes(param_type);
456 /* Make the expression to compute the argument's offset. */
458 addr = new_r_Const_long(irg, bl, mode_Is, curr_ofs);
459 addr = new_r_Add(irg, bl, curr_sp, addr, mach_mode);
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);
468 /* Make a memcopy for compound arguments. */
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);
475 obstack_ptr_grow(obst, mem);
477 curr_ofs += param_size;
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.
486 curr_sp = be_new_IncSP(sp, irg, bl, curr_sp, no_mem, param_size, be_stack_dir_along);
491 in = (ir_node **) obstack_finish(obst);
493 /* We need the sync only, if we didn't build the stores sequentially. */
495 curr_mem = new_r_Sync(irg, bl, n_pos, in);
496 obstack_free(obst, in);
499 /* Collect caller save registers */
500 for(i = 0, n = arch_isa_get_n_reg_class(isa); i < n; ++i) {
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);
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);
515 if(is_Proj(irn) && get_irn_mode(irn) == mode_T) {
517 foreach_out_edge(irn, res_edge) {
519 be_abi_call_arg_t *arg;
520 ir_node *res = get_edge_src_irn(res_edge);
522 assert(is_Proj(res));
524 proj = get_Proj_proj(res);
525 arg = get_call_arg(call, 1, proj);
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
533 set_Proj_proj(res, proj);
534 obstack_ptr_grow(obst, res);
536 if(proj > curr_res_proj)
537 curr_res_proj = proj;
539 pset_remove_ptr(caller_save, arg->reg);
544 obstack_ptr_grow(obst, NULL);
545 res_projs = obstack_finish(obst);
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]));
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));
558 low_call = be_new_Call(irg, bl, curr_mem, curr_sp, call_ptr, curr_res_proj, n_low_args, in);
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);
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);
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);
577 be_set_constr_single_reg(low_call, BE_OUT_POS(proj), arg->reg);
579 obstack_free(obst, in);
580 exchange(irn, low_call);
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);
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;
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++);
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);
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);
607 obstack_free(obst, in);
610 /* Clean up the stack. */
612 ir_node *mem_proj = NULL;
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) {
623 mem_proj = new_r_Proj(irg, bl, low_call, mode_M, pn_Call_M);
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);
629 be_abi_call_free(call);
630 obstack_free(obst, pos);
632 del_pset(caller_save);
639 * The alloca is transformed into a back end alloca node and connected to the stack nodes.
641 static ir_node *adjust_alloc(be_abi_irg_t *env, ir_node *alloc, ir_node *curr_sp)
643 if(get_Alloc_where(alloc) == stack_alloc) {
646 ir_node *bl = get_nodes_block(alloc);
647 ir_graph *irg = get_irn_irg(bl);
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);
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.
663 static int check_dependence(ir_node *curr, ir_node *tgt, ir_node *bl, unsigned long visited_nr)
667 if(get_irn_visited(curr) >= visited_nr)
670 set_irn_visited(curr, visited_nr);
671 if(get_nodes_block(curr) != bl)
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))
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.
692 static int dependent_on(ir_node *n1, ir_node *n2)
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;
698 assert(bl == get_nodes_block(n2));
699 set_irg_visited(irg, vis_nr);
700 return check_dependence(n1, n2, bl, vis_nr);
703 static int cmp_call_dependecy(const void *c1, const void *c2)
705 ir_node *n1 = *(ir_node **) c1;
706 ir_node *n2 = *(ir_node **) c2;
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
714 return n1 == n2 ? 0 : (dependent_on(n1, n2) ? -1 : 1);
717 static void link_calls_in_block_walker(ir_node *irn, void *data)
720 ir_node *bl = get_nodes_block(irn);
721 void *save = get_irn_link(bl);
723 set_irn_link(irn, save);
724 set_irn_link(bl, irn);
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.
734 static void process_calls_in_block(ir_node *bl, void *data)
736 be_abi_irg_t *env = data;
737 ir_node *curr_sp = env->init_sp;
741 for(irn = get_irn_link(bl), n = 0; irn; irn = get_irn_link(irn), ++n)
742 obstack_ptr_grow(&env->obst, irn);
744 /* If there were call nodes in the block. */
749 nodes = obstack_finish(&env->obst);
751 /* order the call nodes according to data dependency */
752 qsort(nodes, n, sizeof(nodes[0]), cmp_call_dependecy);
754 for(i = n - 1; i >= 0; --i) {
755 ir_node *irn = nodes[i];
757 switch(get_irn_opcode(irn)) {
759 curr_sp = adjust_call(env, irn, curr_sp);
762 curr_sp = adjust_alloc(env, irn, curr_sp);
769 obstack_free(&env->obst, nodes);
771 /* Keep the last stack state in the block by tying it to Keep node */
773 be_new_Keep(env->isa->sp->reg_class, get_irn_irg(bl), bl, 1, nodes);
776 set_irn_link(bl, curr_sp);
780 * Adjust all call nodes in the graph to the ABI conventions.
782 static void process_calls(be_abi_irg_t *env)
784 ir_graph *irg = env->birg->irg;
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);
792 * Walker to implement alloca-style allocations.
793 * They are implemented using an add to the stack pointer
794 * and a copy instruction.
796 static void implement_stack_alloc(be_abi_irg_t *env, ir_node *irn)
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;
803 assert(get_irn_opcode(irn) == iro_Alloc && get_Alloc_where(irn) == stack_alloc);
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);
809 res = be_new_AddSP(isa->sp, env->birg->irg, bl, res, size);
811 if(isa->stack_dir < 0)
812 res = be_new_Copy(isa->sp->reg_class, env->birg->irg, bl, res);
817 static void collect_return_walker(ir_node *irn, void *data)
819 if(get_irn_opcode(irn) == iro_Return) {
820 struct obstack *obst = data;
821 obstack_ptr_grow(obst, irn);
825 static ir_node *setup_frame(be_abi_irg_t *env)
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);
838 int stack_nr = get_Proj_proj(stack);
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);
846 frame = be_new_Copy(bp->reg_class, irg, bl, stack);
848 be_node_set_flags(frame, -1, arch_irn_flags_dont_spill);
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);
855 stack = be_new_IncSP(sp, irg, bl, stack, frame, BE_STACK_FRAME_SIZE, be_stack_dir_along);
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);
866 static void clearup_frame(be_abi_irg_t *env, ir_node *ret, struct obstack *obst)
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);
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);
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);
889 pmap_foreach(env->regs, ent) {
890 const arch_register_t *reg = ent->key;
891 ir_node *irn = ent->value;
898 obstack_ptr_grow(obst, irn);
902 static ir_type *compute_arg_type(be_abi_irg_t *env, be_abi_call_t *call, ir_type *method_type)
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;
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));
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);
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);
929 set_type_size_bytes(res, ofs);
934 * Modify the irg itself and the frame type.
936 static void modify_irg(be_abi_irg_t *env)
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);
950 int reg_params_nr = 0;
955 ir_node *frame_pointer;
956 ir_node *reg_params_bl;
957 ir_node **args, **args_repl;
958 const ir_edge_t *edge;
963 DBG((dbg, LEVEL_1, "introducing abi on %+F\n", irg));
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);
968 env->frame = obstack_alloc(&env->obst, sizeof(env->frame[0]));
969 env->regs = pmap_create();
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);
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]));
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);
988 DBG((dbg, LEVEL_2, "\treading arg: %d -> %+F\n", nr, irn));
991 /* Get the ABI constraints from the ISA */
992 arch_isa_get_call_abi(isa, method_type, call);
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);
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);
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));
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);
1017 pmap_insert(env->regs, (void *) sp, NULL);
1018 pmap_insert(env->regs, (void *) isa->bp, NULL);
1020 reg_params_bl = get_irg_start_block(irg);
1021 env->reg_params = be_new_RegParams(irg, reg_params_bl, pmap_count(env->regs));
1024 * make proj nodes for the callee save registers.
1025 * memorize them, since Return nodes get those as inputs.
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);
1035 * If the register is an ignore register,
1036 * The Proj for that register shall also be ignored during register allocation.
1038 if(arch_register_type_is(reg, ignore))
1039 be_node_set_flags(env->reg_params, pos, arch_irn_flags_ignore);
1043 DBG((dbg, LEVEL_2, "\tregister save proj #%d -> reg %s\n", reg_params_nr - 1, reg->name));
1046 /* Insert the code to set up the stack frame */
1047 frame_pointer = setup_frame(env);
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);
1057 nr = MIN(nr, n_params);
1058 arg = get_call_arg(call, 0, nr);
1059 param_type = get_method_param_type(method_type, nr);
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);
1067 /* when the (stack) parameter is primitive, we insert a StackParam
1068 node representing the load of that parameter */
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);
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
1082 args_repl[i] = be_new_FrameAddr(sp->reg_class, irg, reg_params_bl, frame_pointer, arg->stack_ent);
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);
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);
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);
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);
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);
1119 /* generate the clean up code and add additional parameters to the return. */
1120 clearup_frame(env, irn, &env->obst);
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);
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]);
1131 be_set_constr_single_reg(ret, i, reg);
1134 obstack_free(&env->obst, in);
1135 pmap_destroy(reg_map);
1139 obstack_free(&env->obst, args);
1140 be_abi_call_free(call);
1144 * Walker: puts all Alloc(stack_alloc) on a obstack
1146 static void collect_alloca_walker(ir_node *irn, void *data)
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);
1153 be_abi_irg_t *be_abi_introduce(be_irg_t *birg)
1155 be_abi_irg_t *env = malloc(sizeof(env[0]));
1159 env->isa = birg->main_env->arch_env->isa;
1160 env->method_type = get_entity_type(get_irg_entity(birg->irg));
1161 env->call = be_abi_call_new();
1162 arch_isa_get_call_abi(env->isa, env->method_type, env->call);
1164 env->dce_survivor = new_survive_dce();
1166 env->dbg = firm_dbg_register("firm.be.abi");
1167 env->stack_phis = pset_new_ptr(16);
1168 env->init_sp = dummy = new_r_Unknown(birg->irg, env->isa->sp->reg_class->mode);
1170 obstack_init(&env->obst);
1172 memcpy(&env->irn_handler, &abi_irn_handler, sizeof(abi_irn_handler));
1173 env->irn_ops.impl = &abi_irn_ops;
1175 /* Lower all call nodes in the IRG. */
1178 /* Process the IRG */
1181 /* reroute the stack origin of the calls to the true stack origin. */
1182 exchange(dummy, env->init_sp);
1184 /* Make some important node pointers survive the dead node elimination. */
1185 survive_dce_register_irn(env->dce_survivor, &env->init_sp);
1186 survive_dce_register_pmap(env->dce_survivor, env->regs);
1188 arch_env_push_irn_handler(env->birg->main_env->arch_env, &env->irn_handler);
1193 void be_abi_free(be_abi_irg_t *env)
1195 free_survive_dce(env->dce_survivor);
1196 del_pset(env->stack_phis);
1197 pmap_destroy(env->regs);
1198 obstack_free(&env->obst, NULL);
1199 arch_env_pop_irn_handler(env->birg->main_env->arch_env);
1207 | ___(_)_ __ / ___|| |_ __ _ ___| | __
1208 | |_ | \ \/ / \___ \| __/ _` |/ __| |/ /
1209 | _| | |> < ___) | || (_| | (__| <
1210 |_| |_/_/\_\ |____/ \__\__,_|\___|_|\_\
1214 static void collect_stack_nodes_walker(ir_node *irn, void *data)
1218 if((is_Proj(irn) && be_is_Alloca(get_Proj_pred(irn)) && get_Proj_proj(irn) == pn_Alloc_res)
1220 || be_is_SetSP(irn))
1221 pset_insert_ptr(s, irn);
1224 void be_abi_fix_stack_nodes(be_abi_irg_t *env)
1226 dom_front_info_t *df;
1229 /* We need dominance frontiers for fix up */
1230 df = be_compute_dominance_frontiers(env->birg->irg);
1231 stack_nodes = pset_new_ptr(16);
1232 pset_insert_ptr(stack_nodes, env->init_sp);
1233 irg_walk_graph(env->birg->irg, collect_stack_nodes_walker, NULL, stack_nodes);
1234 be_ssa_constr_set_phis(df, stack_nodes, env->stack_phis);
1235 del_pset(stack_nodes);
1237 /* free these dominance frontiers */
1238 be_free_dominance_frontiers(df);
1242 * Translates a direction of an IncSP node (either be_stack_dir_against, or ...along)
1243 * into -1 or 1, respectively.
1244 * @param irn The node.
1245 * @return 1, if the direction of the IncSP was along, -1 if against.
1247 static int get_dir(ir_node *irn)
1249 return 1 - 2 * (be_get_IncSP_direction(irn) == be_stack_dir_against);
1252 static int process_stack_bias(be_abi_irg_t *env, ir_node *bl, int bias)
1254 const arch_env_t *aenv = env->birg->main_env->arch_env;
1256 int start_bias = bias;
1257 int omit_fp = env->call->flags.bits.try_omit_fp;
1259 sched_foreach(bl, irn) {
1262 If the node modifies the stack pointer by a constant offset,
1263 record that in the bias.
1265 if(be_is_IncSP(irn)) {
1266 int ofs = be_get_IncSP_offset(irn);
1267 int dir = get_dir(irn);
1269 if(ofs == BE_STACK_FRAME_SIZE) {
1270 ofs = get_type_size_bytes(get_irg_frame_type(env->birg->irg));
1271 be_set_IncSP_offset(irn, ofs);
1279 Else check, if the node relates to an entity on the stack frame.
1280 If so, set the true offset (including the bias) for that
1284 entity *ent = arch_get_frame_entity(aenv, irn);
1286 int offset = get_stack_entity_offset(env->frame, ent, bias);
1287 arch_set_frame_offset(aenv, irn, offset);
1288 DBG((env->dbg, LEVEL_2, "%F has offset %d\n", ent, offset));
1297 * A helper struct for the bias walker.
1300 be_abi_irg_t *env; /**< The ABI irg environment. */
1301 int start_block_bias; /**< The bias at the end of the start block. */
1304 static void stack_bias_walker(ir_node *bl, void *data)
1306 if(bl != get_irg_start_block(get_irn_irg(bl))) {
1307 struct bias_walk *bw = data;
1308 process_stack_bias(bw->env, bl, bw->start_block_bias);
1312 void be_abi_fix_stack_bias(be_abi_irg_t *env)
1314 ir_graph *irg = env->birg->irg;
1315 struct bias_walk bw;
1317 stack_frame_compute_initial_offset(env->frame);
1318 // stack_frame_dump(stdout, env->frame);
1320 /* Determine the stack bias at the and of the start block. */
1321 bw.start_block_bias = process_stack_bias(env, get_irg_start_block(irg), 0);
1323 /* fix the bias is all other blocks */
1325 irg_block_walk_graph(irg, stack_bias_walker, NULL, &bw);
1328 ir_node *be_abi_get_callee_save_irn(be_abi_irg_t *abi, const arch_register_t *reg)
1330 assert(arch_register_type_is(reg, callee_save));
1331 assert(pmap_contains(abi->regs, (void *) reg));
1332 return pmap_get(abi->regs, (void *) reg);
1336 _____ _____ _ _ _ _ _ _
1337 |_ _| __ \| \ | | | | | | | | |
1338 | | | |__) | \| | | |__| | __ _ _ __ __| | | ___ _ __
1339 | | | _ /| . ` | | __ |/ _` | '_ \ / _` | |/ _ \ '__|
1340 _| |_| | \ \| |\ | | | | | (_| | | | | (_| | | __/ |
1341 |_____|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
1343 for Phi nodes which are created due to stack modifying nodes
1344 such as IncSP, AddSP and SetSP.
1346 These Phis are always to be ignored by the reg alloc and are
1347 fixed on the SP register of the ISA.
1350 static const void *abi_get_irn_ops(const arch_irn_handler_t *handler, const ir_node *irn)
1352 const be_abi_irg_t *abi = get_abi_from_handler(handler);
1353 return is_Phi(irn) && pset_find_ptr(abi->stack_phis, (void *) irn) != NULL ? &abi->irn_ops : NULL;
1356 static void be_abi_limited(void *data, bitset_t *bs)
1358 be_abi_irg_t *abi = data;
1359 bitset_clear_all(bs);
1360 bitset_set(bs, abi->isa->sp->index);
1363 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)
1365 be_abi_irg_t *abi = get_abi_from_ops(self);
1366 const arch_register_t *reg = abi->isa->sp;
1368 req->cls = reg->reg_class;
1369 req->type = arch_register_req_type_limited;
1370 req->limited = be_abi_limited;
1371 req->limited_env = abi;
1375 static void abi_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg)
1379 static const arch_register_t *abi_get_irn_reg(const void *self, const ir_node *irn)
1381 const be_abi_irg_t *abi = get_abi_from_ops(self);
1382 return abi->isa->sp;
1385 static arch_irn_class_t abi_classify(const void *_self, const ir_node *irn)
1387 return arch_irn_class_normal;
1390 static arch_irn_flags_t abi_get_flags(const void *_self, const ir_node *irn)
1392 return arch_irn_flags_ignore;
1395 static entity *abi_get_frame_entity(const void *_self, const ir_node *irn)
1400 static void abi_set_stack_bias(const void *_self, ir_node *irn, int bias)
1404 static const arch_irn_ops_if_t abi_irn_ops = {
1405 abi_get_irn_reg_req,
1410 abi_get_frame_entity,
1414 static const arch_irn_handler_t abi_irn_handler = {