4 * @author Sebastian Hack
6 * Backend node support.
8 * This file provdies Perm, Copy, Spill and Reload nodes.
10 * Copyright (C) 2005 Universitaet Karlsruhe
11 * Released under the GPL
26 #include "bitfiddle.h"
37 #include "besched_t.h"
42 #define OUT_POS(x) (-((x) + 1))
44 /* Sometimes we want to put const nodes into get_irn_generic_attr ... */
45 #define get_irn_attr(irn) get_irn_generic_attr((ir_node *) (irn))
47 static unsigned be_node_tag = FOURCC('B', 'E', 'N', 'O');
50 typedef enum _node_kind_t {
61 be_req_kind_old_limited,
62 be_req_kind_negate_old_limited,
63 be_req_kind_single_reg
67 arch_register_req_t req;
69 arch_irn_flags_t flags;
72 void (*old_limited)(void *ptr, bitset_t *bs);
73 void *old_limited_env;
76 const arch_register_t *single_reg;
81 const arch_register_t *reg;
88 be_reg_data_t *reg_data;
92 be_node_attr_t node_attr;
93 int offset; /**< The offset by which the stack shall be increased/decreased. */
94 be_stack_dir_t dir; /**< The direction in which the stack shall be modified (along or in the other direction). */
98 be_node_attr_t node_attr;
104 be_node_attr_t node_attr;
109 be_frame_attr_t frame_attr;
110 ir_node *spill_ctx; /**< The node in whose context this spill was introduced. */
118 ir_op *op_be_CopyKeep;
124 ir_op *op_be_RegParams;
125 ir_op *op_be_StackParam;
126 ir_op *op_be_FrameAddr;
127 ir_op *op_be_FrameLoad;
128 ir_op *op_be_FrameStore;
129 ir_op *op_be_Barrier;
131 static int beo_base = -1;
133 static const ir_op_ops be_node_op_ops;
135 #define N irop_flag_none
136 #define L irop_flag_labeled
137 #define C irop_flag_commutative
138 #define X irop_flag_cfopcode
139 #define I irop_flag_ip_cfopcode
140 #define F irop_flag_fragile
141 #define Y irop_flag_forking
142 #define H irop_flag_highlevel
143 #define c irop_flag_constlike
144 #define K irop_flag_keep
146 void be_node_init(void) {
147 static int inited = 0;
154 /* Acquire all needed opcodes. */
155 beo_base = get_next_ir_opcodes(beo_Last - 1);
157 op_be_Spill = new_ir_op(beo_base + beo_Spill, "Spill", op_pin_state_mem_pinned, N, oparity_unary, 0, sizeof(be_spill_attr_t), &be_node_op_ops);
158 op_be_Reload = new_ir_op(beo_base + beo_Reload, "Reload", op_pin_state_mem_pinned, N, oparity_zero, 0, sizeof(be_frame_attr_t), &be_node_op_ops);
159 op_be_Perm = new_ir_op(beo_base + beo_Perm, "Perm", op_pin_state_pinned, N, oparity_variable, 0, sizeof(be_node_attr_t), &be_node_op_ops);
160 op_be_Copy = new_ir_op(beo_base + beo_Copy, "Copy", op_pin_state_floats, N, oparity_unary, 0, sizeof(be_node_attr_t), &be_node_op_ops);
161 op_be_Keep = new_ir_op(beo_base + beo_Keep, "Keep", op_pin_state_pinned, K, oparity_variable, 0, sizeof(be_node_attr_t), &be_node_op_ops);
162 op_be_CopyKeep = new_ir_op(beo_base + beo_CopyKeep, "CopyKeep", op_pin_state_pinned, K, oparity_variable, 0, sizeof(be_node_attr_t), &be_node_op_ops);
163 op_be_Call = new_ir_op(beo_base + beo_Call, "Call", op_pin_state_pinned, N, oparity_variable, 0, sizeof(be_call_attr_t), &be_node_op_ops);
164 op_be_Return = new_ir_op(beo_base + beo_Return, "Return", op_pin_state_pinned, X, oparity_variable, 0, sizeof(be_node_attr_t), &be_node_op_ops);
165 op_be_AddSP = new_ir_op(beo_base + beo_AddSP, "AddSP", op_pin_state_pinned, N, oparity_unary, 0, sizeof(be_node_attr_t), &be_node_op_ops);
166 op_be_SetSP = new_ir_op(beo_base + beo_SetSP, "SetSP", op_pin_state_pinned, N, oparity_binary, 0, sizeof(be_stack_attr_t), &be_node_op_ops);
167 op_be_IncSP = new_ir_op(beo_base + beo_IncSP, "IncSP", op_pin_state_pinned, N, oparity_binary, 0, sizeof(be_stack_attr_t), &be_node_op_ops);
168 op_be_RegParams = new_ir_op(beo_base + beo_RegParams, "RegParams", op_pin_state_pinned, N, oparity_zero, 0, sizeof(be_node_attr_t), &be_node_op_ops);
169 op_be_StackParam = new_ir_op(beo_base + beo_StackParam, "StackParam", op_pin_state_pinned, N, oparity_unary, 0, sizeof(be_frame_attr_t), &be_node_op_ops);
170 op_be_FrameAddr = new_ir_op(beo_base + beo_FrameAddr, "FrameAddr", op_pin_state_pinned, N, oparity_binary, 0, sizeof(be_frame_attr_t), &be_node_op_ops);
171 op_be_FrameLoad = new_ir_op(beo_base + beo_FrameLoad, "FrameLoad", op_pin_state_pinned, N, oparity_any, 0, sizeof(be_frame_attr_t), &be_node_op_ops);
172 op_be_FrameStore = new_ir_op(beo_base + beo_FrameStore, "FrameStore", op_pin_state_pinned, N, oparity_any, 0, sizeof(be_frame_attr_t), &be_node_op_ops);
173 op_be_Barrier = new_ir_op(beo_base + beo_Barrier, "Barrier", op_pin_state_pinned, N, oparity_any, 0, sizeof(be_node_attr_t), &be_node_op_ops);
175 set_op_tag(op_be_Spill, &be_node_tag);
176 set_op_tag(op_be_Reload, &be_node_tag);
177 set_op_tag(op_be_Perm, &be_node_tag);
178 set_op_tag(op_be_Copy, &be_node_tag);
179 set_op_tag(op_be_Keep, &be_node_tag);
180 set_op_tag(op_be_CopyKeep, &be_node_tag);
181 set_op_tag(op_be_Call, &be_node_tag);
182 set_op_tag(op_be_Return, &be_node_tag);
183 set_op_tag(op_be_AddSP, &be_node_tag);
184 set_op_tag(op_be_SetSP, &be_node_tag);
185 set_op_tag(op_be_IncSP, &be_node_tag);
186 set_op_tag(op_be_RegParams, &be_node_tag);
187 set_op_tag(op_be_StackParam, &be_node_tag);
188 set_op_tag(op_be_FrameLoad, &be_node_tag);
189 set_op_tag(op_be_FrameStore, &be_node_tag);
190 set_op_tag(op_be_FrameAddr, &be_node_tag);
191 set_op_tag(op_be_Barrier, &be_node_tag);
194 static void *init_node_attr(ir_node* irn, int max_reg_data)
196 ir_graph *irg = get_irn_irg(irn);
197 be_node_attr_t *a = get_irn_attr(irn);
199 memset(a, 0, sizeof(get_op_attr_size(get_irn_op(irn))));
200 a->max_reg_data = max_reg_data;
203 if(max_reg_data > 0) {
206 a->reg_data = NEW_ARR_D(be_reg_data_t, get_irg_obstack(irg), max_reg_data);
207 memset(a->reg_data, 0, max_reg_data * sizeof(a->reg_data[0]));
208 for(i = 0; i < max_reg_data; ++i) {
209 a->reg_data[i].req.req.cls = NULL;
210 a->reg_data[i].req.req.type = arch_register_req_type_none;
217 static INLINE int is_be_node(const ir_node *irn)
219 return get_op_tag(get_irn_op(irn)) == &be_node_tag;
222 be_opcode_t be_get_irn_opcode(const ir_node *irn)
224 return is_be_node(irn) ? get_irn_opcode(irn) - beo_base : beo_NoBeOp;
227 static int redir_proj(const ir_node **node, int pos)
229 const ir_node *n = *node;
234 assert(pos == -1 && "Illegal pos for a Proj");
235 *node = irn = get_Proj_pred(n);
237 assert(get_irn_mode(irn) == mode_T);
238 *node = get_Proj_pred(irn);
240 return get_Proj_proj(n);
247 be_node_set_irn_reg(const void *_self, ir_node *irn, const arch_register_t *reg)
252 out_pos = redir_proj((const ir_node **) &irn, -1);
253 a = get_irn_attr(irn);
255 assert(is_be_node(irn));
256 assert(out_pos < a->max_reg_data && "position too high");
257 a->reg_data[out_pos].reg = reg;
261 ir_node *be_new_Spill(const arch_register_class_t *cls, const arch_register_class_t *cls_frame, ir_graph *irg, ir_node *bl, ir_node *frame, ir_node *to_spill, ir_node *ctx)
269 res = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
270 a = init_node_attr(res, 2);
271 a->frame_attr.ent = NULL;
272 a->frame_attr.offset = 0;
275 be_node_set_reg_class(res, 0, cls_frame);
276 be_node_set_reg_class(res, 1, cls);
280 ir_node *be_new_Reload(const arch_register_class_t *cls, const arch_register_class_t *cls_frame, ir_graph *irg, ir_node *bl, ir_node *frame, ir_node *mem, ir_mode *mode)
287 res = new_ir_node(NULL, irg, bl, op_be_Reload, mode, 2, in);
288 init_node_attr(res, 2);
289 be_node_set_reg_class(res, 0, cls_frame);
290 be_node_set_reg_class(res, -1, cls);
294 ir_node *(be_get_Reload_mem)(const ir_node *irn)
296 assert(be_is_Reload(irn));
297 return get_irn_n(irn, be_pos_Reload_mem);
300 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
303 ir_node *irn = new_ir_node(NULL, irg, bl, op_be_Perm, mode_T, n, in);
304 init_node_attr(irn, n);
305 for(i = 0; i < n; ++i) {
306 be_node_set_reg_class(irn, i, cls);
307 be_node_set_reg_class(irn, OUT_POS(i), cls);
313 ir_node *be_new_Copy(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *op)
319 res = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
320 init_node_attr(res, 1);
321 be_node_set_reg_class(res, 0, cls);
322 be_node_set_reg_class(res, OUT_POS(0), cls);
326 ir_node *be_new_Keep(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
331 irn = new_ir_node(NULL, irg, bl, op_be_Keep, mode_ANY, n, in);
332 init_node_attr(irn, n);
333 for(i = 0; i < n; ++i) {
334 be_node_set_reg_class(irn, i, cls);
340 ir_node *be_new_Call(ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *sp, ir_node *ptr, int n_outs, int n, ir_node *in[])
346 real_in = malloc(sizeof(real_in[0]) * (real_n));
351 memcpy(&real_in[3], in, n * sizeof(in[0]));
353 irn = new_ir_node(NULL, irg, bl, op_be_Call, mode_T, real_n, real_in);
354 init_node_attr(irn, (n_outs > real_n ? n_outs : real_n));
358 entity *be_Call_get_entity(const ir_node *call)
360 be_call_attr_t *a = get_irn_attr(call);
361 assert(be_is_Call(call));
365 void be_Call_set_entity(ir_node *call, entity *ent)
367 be_call_attr_t *a = get_irn_attr(call);
368 assert(be_is_Call(call));
372 ir_node *be_new_Return(ir_graph *irg, ir_node *bl, int n, ir_node *in[])
374 ir_node *irn = new_ir_node(NULL, irg, bl, op_be_Return, mode_X, n, in);
375 init_node_attr(irn, n);
380 ir_node *be_new_IncSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, ir_node *mem, unsigned offset, be_stack_dir_t dir)
388 irn = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode, 2, in);
389 a = init_node_attr(irn, 1);
393 be_node_set_flags(irn, -1, arch_irn_flags_ignore);
395 /* Set output constraint to stack register. */
396 be_node_set_reg_class(irn, 0, sp->reg_class);
397 be_set_constr_single_reg(irn, -1, sp);
398 be_node_set_irn_reg(NULL, irn, sp);
403 ir_node *be_new_AddSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, ir_node *sz)
407 ir_node *in[be_pos_AddSP_last];
409 in[be_pos_AddSP_old_sp] = old_sp;
410 in[be_pos_AddSP_size] = sz;
412 irn = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T, be_pos_AddSP_last, in);
413 a = init_node_attr(irn, be_pos_AddSP_last);
415 be_node_set_flags(irn, OUT_POS(0), arch_irn_flags_ignore);
417 /* Set output constraint to stack register. */
418 be_set_constr_single_reg(irn, OUT_POS(0), sp);
423 ir_node *be_new_SetSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, ir_node *op, ir_node *mem)
432 irn = new_ir_node(NULL, irg, bl, op_be_SetSP, get_irn_mode(old_sp), 3, in);
433 a = init_node_attr(irn, 3);
435 be_node_set_flags(irn, OUT_POS(0), arch_irn_flags_ignore);
437 /* Set output constraint to stack register. */
438 be_set_constr_single_reg(irn, OUT_POS(0), sp);
439 be_node_set_reg_class(irn, 1, sp->reg_class);
440 be_node_set_reg_class(irn, 2, sp->reg_class);
441 be_node_set_irn_reg(NULL, irn, sp);
446 ir_node *be_new_StackParam(const arch_register_class_t *cls, const arch_register_class_t *cls_frame, ir_graph *irg, ir_node *bl, ir_mode *mode, ir_node *frame_pointer, entity *ent)
452 in[0] = frame_pointer;
453 irn = new_ir_node(NULL, irg, bl, op_be_StackParam, mode, 1, in);
454 a = init_node_attr(irn, 1);
457 be_node_set_reg_class(irn, 0, cls_frame);
458 be_node_set_reg_class(irn, OUT_POS(0), cls);
462 ir_node *be_new_RegParams(ir_graph *irg, ir_node *bl, int n_outs)
467 irn = new_ir_node(NULL, irg, bl, op_be_RegParams, mode_T, 0, in);
468 init_node_attr(irn, n_outs);
472 ir_node *be_new_FrameLoad(const arch_register_class_t *cls_frame, const arch_register_class_t *cls_data,
473 ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *frame, entity *ent)
481 irn = new_ir_node(NULL, irg, bl, op_be_FrameLoad, mode_T, 2, in);
482 a = init_node_attr(irn, 3);
485 be_node_set_reg_class(irn, 1, cls_frame);
486 be_node_set_reg_class(irn, OUT_POS(pn_Load_res), cls_data);
490 ir_node *be_new_FrameStore(const arch_register_class_t *cls_frame, const arch_register_class_t *cls_data,
491 ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *frame, ir_node *data, entity *ent)
500 irn = new_ir_node(NULL, irg, bl, op_be_FrameStore, mode_T, 3, in);
501 a = init_node_attr(irn, 3);
504 be_node_set_reg_class(irn, 1, cls_frame);
505 be_node_set_reg_class(irn, 2, cls_data);
509 ir_node *be_new_FrameAddr(const arch_register_class_t *cls_frame, ir_graph *irg, ir_node *bl, ir_node *frame, entity *ent)
516 irn = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
517 a = init_node_attr(irn, 1);
520 be_node_set_reg_class(irn, 0, cls_frame);
521 be_node_set_reg_class(irn, OUT_POS(0), cls_frame);
525 ir_node *be_new_CopyKeep(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *src, int n, ir_node *in_keep[], ir_mode *mode)
528 ir_node **in = (ir_node **) alloca((n + 1) * sizeof(in[0]));
531 memcpy(&in[1], in_keep, n * sizeof(in[0]));
532 irn = new_ir_node(NULL, irg, bl, op_be_CopyKeep, mode, n + 1, in);
533 init_node_attr(irn, n);
534 be_node_set_reg_class(irn, OUT_POS(0), cls);
535 be_node_set_reg_class(irn, 0, cls);
540 ir_node *be_new_CopyKeep_single(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *src, ir_node *keep, ir_mode *mode)
545 return be_new_CopyKeep(cls, irg, bl, src, 1, in, mode);
548 ir_node *be_new_Barrier(ir_graph *irg, ir_node *bl, int n, ir_node *in[])
552 irn = new_ir_node(NULL, irg, bl, op_be_Barrier, mode_T, n, in);
553 init_node_attr(irn, n);
557 int be_is_Spill (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Spill ; }
558 int be_is_Reload (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Reload ; }
559 int be_is_Copy (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Copy ; }
560 int be_is_Perm (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Perm ; }
561 int be_is_Keep (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Keep ; }
562 int be_is_Call (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Call ; }
563 int be_is_Return (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Return ; }
564 int be_is_IncSP (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_IncSP ; }
565 int be_is_SetSP (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_SetSP ; }
566 int be_is_AddSP (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_AddSP ; }
567 int be_is_RegParams (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_RegParams ; }
568 int be_is_StackParam (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_StackParam ; }
569 int be_is_FrameAddr (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameAddr ; }
570 int be_is_FrameLoad (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameLoad ; }
571 int be_is_FrameStore (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameStore ; }
572 int be_is_Barrier (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Barrier ; }
574 int be_has_frame_entity(const ir_node *irn)
576 switch(be_get_irn_opcode(irn)) {
589 entity *be_get_frame_entity(const ir_node *irn)
591 if(be_has_frame_entity(irn)) {
592 be_frame_attr_t *a = get_irn_attr(irn);
598 static void be_limited(void *data, bitset_t *bs)
600 be_req_t *req = data;
603 case be_req_kind_negate_old_limited:
604 case be_req_kind_old_limited:
605 req->x.old_limited.old_limited(req->x.old_limited.old_limited_env, bs);
606 if(req->kind == be_req_kind_negate_old_limited)
609 case be_req_kind_single_reg:
610 bitset_clear_all(bs);
611 bitset_set(bs, req->x.single_reg->index);
616 static INLINE be_req_t *get_req(ir_node *irn, int pos)
618 int idx = pos < 0 ? -(pos + 1) : pos;
619 be_node_attr_t *a = get_irn_attr(irn);
620 be_reg_data_t *rd = &a->reg_data[idx];
621 be_req_t *r = pos < 0 ? &rd->req : &rd->in_req;
623 assert(is_be_node(irn));
624 assert(!(pos >= 0) || pos < get_irn_arity(irn));
625 assert(!(pos < 0) || -(pos + 1) <= a->max_reg_data);
630 void be_set_constr_single_reg(ir_node *irn, int pos, const arch_register_t *reg)
632 be_req_t *r = get_req(irn, pos);
634 r->kind = be_req_kind_single_reg;
635 r->x.single_reg = reg;
636 r->req.limited = be_limited;
637 r->req.limited_env = r;
638 r->req.type = arch_register_req_type_limited;
639 r->req.cls = reg->reg_class;
642 void be_set_constr_limited(ir_node *irn, int pos, const arch_register_req_t *req)
644 be_req_t *r = get_req(irn, pos);
646 assert(arch_register_req_is(req, limited));
648 r->kind = be_req_kind_old_limited;
649 r->req.limited = be_limited;
650 r->req.limited_env = r;
651 r->req.type = arch_register_req_type_limited;
652 r->req.cls = req->cls;
654 r->x.old_limited.old_limited = req->limited;
655 r->x.old_limited.old_limited_env = req->limited_env;
658 void be_node_set_flags(ir_node *irn, int pos, arch_irn_flags_t flags)
660 be_req_t *r = get_req(irn, pos);
664 void be_node_set_reg_class(ir_node *irn, int pos, const arch_register_class_t *cls)
666 be_req_t *r = get_req(irn, pos);
668 if(r->req.type == arch_register_req_type_none)
669 r->req.type = arch_register_req_type_normal;
672 void be_node_set_req_type(ir_node *irn, int pos, arch_register_req_type_t type)
674 be_req_t *r = get_req(irn, pos);
678 void be_set_IncSP_offset(ir_node *irn, unsigned offset)
680 be_stack_attr_t *a = get_irn_attr(irn);
681 assert(be_is_IncSP(irn));
685 unsigned be_get_IncSP_offset(const ir_node *irn)
687 be_stack_attr_t *a = get_irn_attr(irn);
688 assert(be_is_IncSP(irn));
692 void be_set_IncSP_direction(ir_node *irn, be_stack_dir_t dir)
694 be_stack_attr_t *a = get_irn_attr(irn);
695 assert(be_is_IncSP(irn));
699 be_stack_dir_t be_get_IncSP_direction(const ir_node *irn)
701 be_stack_attr_t *a = get_irn_attr(irn);
702 assert(be_is_IncSP(irn));
706 void be_set_Spill_entity(ir_node *irn, entity *ent)
708 be_spill_attr_t *a = get_irn_attr(irn);
709 assert(be_is_Spill(irn));
710 a->frame_attr.ent = ent;
713 static ir_node *find_a_spill_walker(ir_node *irn, unsigned visited_nr)
715 unsigned nr = get_irn_visited(irn);
717 set_irn_visited(irn, visited_nr);
721 if(nr < visited_nr) {
722 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
723 ir_node *n = find_a_spill_walker(get_irn_n(irn, i), visited_nr);
730 else if(be_get_irn_opcode(irn) == beo_Spill)
736 ir_node *be_get_Spill_context(const ir_node *irn) {
737 const be_spill_attr_t *a = get_irn_attr(irn);
738 assert(be_is_Spill(irn));
743 * Finds a spill for a reload.
744 * If the reload is directly using the spill, this is simple,
745 * else we perform DFS from the reload (over all PhiMs) and return
746 * the first spill node we find.
748 static INLINE ir_node *find_a_spill(const ir_node *irn)
750 ir_graph *irg = get_irn_irg(irn);
751 unsigned visited_nr = get_irg_visited(irg) + 1;
753 assert(be_is_Reload(irn));
754 set_irg_visited(irg, visited_nr);
755 return find_a_spill_walker(be_get_Reload_mem(irn), visited_nr);
758 entity *be_get_spill_entity(const ir_node *irn)
760 int opc = get_irn_opcode(irn);
762 switch(be_get_irn_opcode(irn)) {
765 ir_node *spill = find_a_spill(irn);
766 return be_get_spill_entity(spill);
770 be_spill_attr_t *a = get_irn_attr(irn);
771 return a->frame_attr.ent;
774 assert(0 && "Must give spill/reload node");
781 static void link_reload_walker(ir_node *irn, void *data)
783 ir_node **root = (ir_node **) data;
784 if(be_is_Reload(irn)) {
785 set_irn_link(irn, *root);
790 void be_copy_entities_to_reloads(ir_graph *irg)
793 irg_walk_graph(irg, link_reload_walker, NULL, (void *) &irn);
796 be_frame_attr_t *a = get_irn_attr(irn);
797 entity *ent = be_get_spill_entity(irn);
799 irn = get_irn_link(irn);
803 ir_node *be_spill(const arch_env_t *arch_env, ir_node *irn, ir_node *ctx)
805 ir_node *bl = get_nodes_block(irn);
806 ir_graph *irg = get_irn_irg(bl);
807 ir_node *frame = get_irg_frame(irg);
808 ir_node *insert = bl;
811 const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, irn, -1);
812 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
814 spill = be_new_Spill(cls, cls_frame, irg, bl, frame, irn, ctx);
817 * search the right insertion point. a spill of a phi cannot be put
818 * directly after the phi, if there are some phis behind the one which
819 * is spilled. Also, a spill of a Proj must be after all Projs of the
822 * Here's one special case:
823 * If the spill is in the start block, the spill must be after the frame
824 * pointer is set up. This is done by setting insert to the end of the block
825 * which is its default initialization (see above).
828 insert = sched_next(irn);
829 if(bl == get_irg_start_block(irg) && insert != bl && sched_get_time_step(frame) >= sched_get_time_step(insert))
830 insert = sched_next(frame);
832 while((is_Phi(insert) || is_Proj(insert)) && !sched_is_end(insert))
833 insert = sched_next(insert);
835 sched_add_before(insert, spill);
839 ir_node *be_reload(const arch_env_t *arch_env, const arch_register_class_t *cls, ir_node *insert, ir_mode *mode, ir_node *spill)
843 ir_node *bl = is_Block(insert) ? insert : get_nodes_block(insert);
844 ir_graph *irg = get_irn_irg(bl);
845 ir_node *frame = get_irg_frame(irg);
846 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
848 assert(be_is_Spill(spill) || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
850 reload = be_new_Reload(cls, cls_frame, irg, bl, frame, spill, mode);
852 if(is_Block(insert)) {
853 insert = sched_skip(insert, 0, sched_skip_cf_predicator, (void *) arch_env);
854 sched_add_after(insert, reload);
858 sched_add_before(insert, reload);
863 static void *put_out_reg_req(arch_register_req_t *req, const ir_node *irn, int out_pos)
865 const be_node_attr_t *a = get_irn_attr(irn);
867 if(out_pos < a->max_reg_data)
868 memcpy(req, &a->reg_data[out_pos].req, sizeof(req[0]));
870 req->type = arch_register_req_type_none;
877 static void *put_in_reg_req(arch_register_req_t *req, const ir_node *irn, int pos)
879 const be_node_attr_t *a = get_irn_attr(irn);
880 int n = get_irn_arity(irn);
882 if(pos < get_irn_arity(irn) && pos < a->max_reg_data)
883 memcpy(req, &a->reg_data[pos].in_req, sizeof(req[0]));
885 req->type = arch_register_req_type_none;
892 static const arch_register_req_t *
893 be_node_get_irn_reg_req(const void *self, arch_register_req_t *req, const ir_node *irn, int pos)
898 if(get_irn_mode(irn) == mode_T)
901 out_pos = redir_proj((const ir_node **) &irn, pos);
902 assert(is_be_node(irn));
903 return put_out_reg_req(req, irn, out_pos);
907 return is_be_node(irn) ? put_in_reg_req(req, irn, pos) : NULL;
913 const arch_register_t *
914 be_node_get_irn_reg(const void *_self, const ir_node *irn)
919 out_pos = redir_proj((const ir_node **) &irn, -1);
920 a = get_irn_attr(irn);
922 assert(is_be_node(irn));
923 assert(out_pos < a->max_reg_data && "position too high");
925 return a->reg_data[out_pos].reg;
928 static arch_irn_class_t be_node_classify(const void *_self, const ir_node *irn)
930 redir_proj((const ir_node **) &irn, -1);
932 switch(be_get_irn_opcode(irn)) {
933 #define XXX(a,b) case beo_ ## a: return arch_irn_class_ ## b;
946 static arch_irn_flags_t be_node_get_flags(const void *_self, const ir_node *irn)
951 out_pos = redir_proj((const ir_node **) &irn, -1);
952 a = get_irn_attr(irn);
954 assert(is_be_node(irn));
955 assert(out_pos < a->max_reg_data && "position too high");
957 return a->reg_data[out_pos].req.flags;
960 static entity *be_node_get_frame_entity(const void *self, const ir_node *irn)
962 return be_get_frame_entity(irn);
965 static void be_node_set_frame_offset(const void *self, ir_node *irn, int offset)
967 if(be_has_frame_entity(irn)) {
968 be_frame_attr_t *a = get_irn_attr(irn);
974 static const arch_irn_ops_if_t be_node_irn_ops_if = {
975 be_node_get_irn_reg_req,
980 be_node_get_frame_entity,
981 be_node_set_frame_offset
984 static const arch_irn_ops_t be_node_irn_ops = {
988 const void *be_node_get_arch_ops(const arch_irn_handler_t *self, const ir_node *irn)
990 redir_proj((const ir_node **) &irn, -1);
991 return is_be_node(irn) ? &be_node_irn_ops : NULL;
994 const arch_irn_handler_t be_node_irn_handler = {
999 static void dump_node_req(FILE *f, int idx, be_req_t *req)
1002 int did_something = 0;
1004 const char *prefix = buf;
1006 snprintf(buf, sizeof(buf), "#%d ", idx);
1008 if(req->flags != arch_irn_flags_none) {
1009 fprintf(f, "%sflags: ", prefix);
1011 for(i = arch_irn_flags_none; i <= log2_ceil(arch_irn_flags_last); ++i) {
1012 if(req->flags & (1 << i)) {
1013 fprintf(f, "%s%s", prefix, arch_irn_flag_str(1 << i));
1021 if(req->req.cls != 0) {
1024 arch_register_req_format(tmp, sizeof(tmp), &req->req);
1025 fprintf(f, "%s", tmp);
1033 static void dump_node_reqs(FILE *f, ir_node *irn)
1036 be_node_attr_t *a = get_irn_attr(irn);
1038 fprintf(f, "registers: \n");
1039 for(i = 0; i < a->max_reg_data; ++i) {
1040 be_reg_data_t *rd = &a->reg_data[i];
1042 fprintf(f, "#%d: %s\n", i, rd->reg->name);
1045 fprintf(f, "in requirements\n");
1046 for(i = 0; i < a->max_reg_data; ++i) {
1047 dump_node_req(f, i, &a->reg_data[i].in_req);
1050 fprintf(f, "\nout requirements\n");
1051 for(i = 0; i < a->max_reg_data; ++i) {
1052 dump_node_req(f, i, &a->reg_data[i].req);
1056 static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason)
1058 be_node_attr_t *at = get_irn_attr(irn);
1060 assert(is_be_node(irn));
1063 case dump_node_opcode_txt:
1064 fprintf(f, get_op_name(get_irn_op(irn)));
1066 case dump_node_mode_txt:
1067 fprintf(f, get_mode_name(get_irn_mode(irn)));
1069 case dump_node_nodeattr_txt:
1071 case dump_node_info_txt:
1072 dump_node_reqs(f, irn);
1074 if(be_has_frame_entity(irn)) {
1075 be_frame_attr_t *a = (be_frame_attr_t *) at;
1077 ir_fprintf(f, "frame entity: %+F offset %x (%d)\n", a->ent, a->offset, a->offset);
1081 switch(be_get_irn_opcode(irn)) {
1084 be_spill_attr_t *a = (be_spill_attr_t *) at;
1085 ir_fprintf(f, "spill context: %+F\n", a->spill_ctx);
1091 be_stack_attr_t *a = (be_stack_attr_t *) at;
1092 fprintf(f, "offset: %u\n", a->offset);
1093 fprintf(f, "direction: %s\n", a->dir == be_stack_dir_along ? "along" : "against");
1104 * Copies the backend specific attributes from old node to new node.
1106 static void copy_attr(const ir_node *old_node, ir_node *new_node)
1108 be_node_attr_t *old_attr = get_irn_attr(old_node);
1109 be_node_attr_t *new_attr = get_irn_attr(new_node);
1112 assert(is_be_node(old_node));
1113 assert(is_be_node(new_node));
1115 memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
1116 new_attr->reg_data = NULL;
1118 if(new_attr->max_reg_data > 0) {
1119 new_attr->reg_data = NEW_ARR_D(be_reg_data_t, get_irg_obstack(get_irn_irg(new_node)), new_attr->max_reg_data);
1120 memcpy(new_attr->reg_data, old_attr->reg_data, new_attr->max_reg_data * sizeof(be_reg_data_t));
1122 for(i = 0; i < old_attr->max_reg_data; ++i) {
1125 r = &new_attr->reg_data[i].req;
1126 r->req.limited_env = r;
1128 r = &new_attr->reg_data[i].in_req;
1129 r->req.limited_env = r;
1134 static const ir_op_ops be_node_op_ops = {