4 * @author Sebastian Hack
6 * Backend node support.
8 * This file provides Perm, Copy, Spill and Reload nodes.
10 * Copyright (C) 2005 Universitaet Karlsruhe
11 * Released under the GPL
27 #include "bitfiddle.h"
38 #include "besched_t.h"
43 #define OUT_POS(x) (-((x) + 1))
45 /* Sometimes we want to put const nodes into get_irn_generic_attr ... */
46 #define get_irn_attr(irn) get_irn_generic_attr((ir_node *) (irn))
48 static unsigned be_node_tag = FOURCC('B', 'E', 'N', 'O');
51 typedef enum _node_kind_t {
62 be_req_kind_old_limited,
63 be_req_kind_negate_old_limited,
64 be_req_kind_single_reg
68 arch_register_req_t req;
70 arch_irn_flags_t flags;
73 void (*old_limited)(void *ptr, bitset_t *bs);
74 void *old_limited_env;
77 const arch_register_t *single_reg;
82 const arch_register_t *reg;
89 be_reg_data_t *reg_data;
93 be_node_attr_t node_attr;
94 int offset; /**< The offset by which the stack shall be increased/decreased. */
95 be_stack_dir_t dir; /**< The direction in which the stack shall be modified (along or in the other direction). */
99 be_node_attr_t node_attr;
105 be_node_attr_t node_attr;
110 be_frame_attr_t frame_attr;
111 ir_node *spill_ctx; /**< The node in whose context this spill was introduced. */
119 ir_op *op_be_CopyKeep;
125 ir_op *op_be_RegParams;
126 ir_op *op_be_StackParam;
127 ir_op *op_be_FrameAddr;
128 ir_op *op_be_FrameLoad;
129 ir_op *op_be_FrameStore;
130 ir_op *op_be_Barrier;
132 static int beo_base = -1;
134 static const ir_op_ops be_node_op_ops;
136 #define N irop_flag_none
137 #define L irop_flag_labeled
138 #define C irop_flag_commutative
139 #define X irop_flag_cfopcode
140 #define I irop_flag_ip_cfopcode
141 #define F irop_flag_fragile
142 #define Y irop_flag_forking
143 #define H irop_flag_highlevel
144 #define c irop_flag_constlike
145 #define K irop_flag_keep
147 void be_node_init(void) {
148 static int inited = 0;
155 /* Acquire all needed opcodes. */
156 beo_base = get_next_ir_opcodes(beo_Last - 1);
158 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);
159 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);
160 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);
161 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);
162 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);
163 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);
164 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);
165 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);
166 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);
167 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);
168 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);
169 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);
170 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);
171 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);
172 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);
173 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);
174 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);
176 set_op_tag(op_be_Spill, &be_node_tag);
177 set_op_tag(op_be_Reload, &be_node_tag);
178 set_op_tag(op_be_Perm, &be_node_tag);
179 set_op_tag(op_be_Copy, &be_node_tag);
180 set_op_tag(op_be_Keep, &be_node_tag);
181 set_op_tag(op_be_CopyKeep, &be_node_tag);
182 set_op_tag(op_be_Call, &be_node_tag);
183 set_op_tag(op_be_Return, &be_node_tag);
184 set_op_tag(op_be_AddSP, &be_node_tag);
185 set_op_tag(op_be_SetSP, &be_node_tag);
186 set_op_tag(op_be_IncSP, &be_node_tag);
187 set_op_tag(op_be_RegParams, &be_node_tag);
188 set_op_tag(op_be_StackParam, &be_node_tag);
189 set_op_tag(op_be_FrameLoad, &be_node_tag);
190 set_op_tag(op_be_FrameStore, &be_node_tag);
191 set_op_tag(op_be_FrameAddr, &be_node_tag);
192 set_op_tag(op_be_Barrier, &be_node_tag);
195 static void *init_node_attr(ir_node* irn, int max_reg_data)
197 ir_graph *irg = get_irn_irg(irn);
198 be_node_attr_t *a = get_irn_attr(irn);
200 memset(a, 0, sizeof(get_op_attr_size(get_irn_op(irn))));
201 a->max_reg_data = max_reg_data;
204 if(max_reg_data > 0) {
207 a->reg_data = NEW_ARR_D(be_reg_data_t, get_irg_obstack(irg), max_reg_data);
208 memset(a->reg_data, 0, max_reg_data * sizeof(a->reg_data[0]));
209 for(i = 0; i < max_reg_data; ++i) {
210 a->reg_data[i].req.req.cls = NULL;
211 a->reg_data[i].req.req.type = arch_register_req_type_none;
218 static INLINE int is_be_node(const ir_node *irn)
220 return get_op_tag(get_irn_op(irn)) == &be_node_tag;
223 be_opcode_t be_get_irn_opcode(const ir_node *irn)
225 return is_be_node(irn) ? get_irn_opcode(irn) - beo_base : beo_NoBeOp;
228 static int redir_proj(const ir_node **node, int pos)
230 const ir_node *n = *node;
235 assert(pos == -1 && "Illegal pos for a Proj");
236 *node = irn = get_Proj_pred(n);
238 assert(get_irn_mode(irn) == mode_T);
239 *node = get_Proj_pred(irn);
241 return get_Proj_proj(n);
248 be_node_set_irn_reg(const void *_self, ir_node *irn, const arch_register_t *reg)
253 out_pos = redir_proj((const ir_node **) &irn, -1);
254 a = get_irn_attr(irn);
256 assert(is_be_node(irn));
257 assert(out_pos < a->max_reg_data && "position too high");
258 a->reg_data[out_pos].reg = reg;
262 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)
270 res = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
271 a = init_node_attr(res, 2);
272 a->frame_attr.ent = NULL;
273 a->frame_attr.offset = 0;
276 be_node_set_reg_class(res, 0, cls_frame);
277 be_node_set_reg_class(res, 1, cls);
281 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)
288 res = new_ir_node(NULL, irg, bl, op_be_Reload, mode, 2, in);
289 init_node_attr(res, 2);
290 be_node_set_reg_class(res, 0, cls_frame);
291 be_node_set_reg_class(res, -1, cls);
295 ir_node *(be_get_Reload_mem)(const ir_node *irn)
297 assert(be_is_Reload(irn));
298 return get_irn_n(irn, be_pos_Reload_mem);
301 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
304 ir_node *irn = new_ir_node(NULL, irg, bl, op_be_Perm, mode_T, n, in);
305 init_node_attr(irn, n);
306 for(i = 0; i < n; ++i) {
307 be_node_set_reg_class(irn, i, cls);
308 be_node_set_reg_class(irn, OUT_POS(i), cls);
314 ir_node *be_new_Copy(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *op)
320 res = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
321 init_node_attr(res, 1);
322 be_node_set_reg_class(res, 0, cls);
323 be_node_set_reg_class(res, OUT_POS(0), cls);
327 ir_node *be_new_Keep(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
332 irn = new_ir_node(NULL, irg, bl, op_be_Keep, mode_ANY, n, in);
333 init_node_attr(irn, n);
334 for(i = 0; i < n; ++i) {
335 be_node_set_reg_class(irn, i, cls);
341 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[])
347 real_in = xmalloc(sizeof(real_in[0]) * (real_n));
352 memcpy(&real_in[3], in, n * sizeof(in[0]));
354 irn = new_ir_node(NULL, irg, bl, op_be_Call, mode_T, real_n, real_in);
355 init_node_attr(irn, (n_outs > real_n ? n_outs : real_n));
359 entity *be_Call_get_entity(const ir_node *call)
361 be_call_attr_t *a = get_irn_attr(call);
362 assert(be_is_Call(call));
366 void be_Call_set_entity(ir_node *call, entity *ent)
368 be_call_attr_t *a = get_irn_attr(call);
369 assert(be_is_Call(call));
373 ir_node *be_new_Return(ir_graph *irg, ir_node *bl, int n, ir_node *in[])
375 ir_node *irn = new_ir_node(NULL, irg, bl, op_be_Return, mode_X, n, in);
376 init_node_attr(irn, n);
381 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)
389 irn = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode, 2, in);
390 a = init_node_attr(irn, 1);
394 be_node_set_flags(irn, -1, arch_irn_flags_ignore);
396 /* Set output constraint to stack register. */
397 be_node_set_reg_class(irn, 0, sp->reg_class);
398 be_set_constr_single_reg(irn, -1, sp);
399 be_node_set_irn_reg(NULL, irn, sp);
404 ir_node *be_new_AddSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, ir_node *sz)
408 ir_node *in[be_pos_AddSP_last];
410 in[be_pos_AddSP_old_sp] = old_sp;
411 in[be_pos_AddSP_size] = sz;
413 irn = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T, be_pos_AddSP_last, in);
414 a = init_node_attr(irn, be_pos_AddSP_last);
416 be_node_set_flags(irn, OUT_POS(0), arch_irn_flags_ignore);
418 /* Set output constraint to stack register. */
419 be_set_constr_single_reg(irn, OUT_POS(0), sp);
424 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)
433 irn = new_ir_node(NULL, irg, bl, op_be_SetSP, get_irn_mode(old_sp), 3, in);
434 a = init_node_attr(irn, 3);
436 be_node_set_flags(irn, OUT_POS(0), arch_irn_flags_ignore);
438 /* Set output constraint to stack register. */
439 be_set_constr_single_reg(irn, OUT_POS(0), sp);
440 be_node_set_reg_class(irn, 1, sp->reg_class);
441 be_node_set_reg_class(irn, 2, sp->reg_class);
442 be_node_set_irn_reg(NULL, irn, sp);
447 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)
453 in[0] = frame_pointer;
454 irn = new_ir_node(NULL, irg, bl, op_be_StackParam, mode, 1, in);
455 a = init_node_attr(irn, 1);
458 be_node_set_reg_class(irn, 0, cls_frame);
459 be_node_set_reg_class(irn, OUT_POS(0), cls);
463 ir_node *be_new_RegParams(ir_graph *irg, ir_node *bl, int n_outs)
468 irn = new_ir_node(NULL, irg, bl, op_be_RegParams, mode_T, 0, in);
469 init_node_attr(irn, n_outs);
473 ir_node *be_new_FrameLoad(const arch_register_class_t *cls_frame, const arch_register_class_t *cls_data,
474 ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *frame, entity *ent)
482 irn = new_ir_node(NULL, irg, bl, op_be_FrameLoad, mode_T, 2, in);
483 a = init_node_attr(irn, 3);
486 be_node_set_reg_class(irn, 1, cls_frame);
487 be_node_set_reg_class(irn, OUT_POS(pn_Load_res), cls_data);
491 ir_node *be_new_FrameStore(const arch_register_class_t *cls_frame, const arch_register_class_t *cls_data,
492 ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *frame, ir_node *data, entity *ent)
501 irn = new_ir_node(NULL, irg, bl, op_be_FrameStore, mode_T, 3, in);
502 a = init_node_attr(irn, 3);
505 be_node_set_reg_class(irn, 1, cls_frame);
506 be_node_set_reg_class(irn, 2, cls_data);
510 ir_node *be_new_FrameAddr(const arch_register_class_t *cls_frame, ir_graph *irg, ir_node *bl, ir_node *frame, entity *ent)
517 irn = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
518 a = init_node_attr(irn, 1);
521 be_node_set_reg_class(irn, 0, cls_frame);
522 be_node_set_reg_class(irn, OUT_POS(0), cls_frame);
526 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)
529 ir_node **in = (ir_node **) alloca((n + 1) * sizeof(in[0]));
532 memcpy(&in[1], in_keep, n * sizeof(in[0]));
533 irn = new_ir_node(NULL, irg, bl, op_be_CopyKeep, mode, n + 1, in);
534 init_node_attr(irn, n + 1);
535 be_node_set_reg_class(irn, OUT_POS(0), cls);
536 be_node_set_reg_class(irn, 0, cls);
541 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)
546 return be_new_CopyKeep(cls, irg, bl, src, 1, in, mode);
549 ir_node *be_new_Barrier(ir_graph *irg, ir_node *bl, int n, ir_node *in[])
553 irn = new_ir_node(NULL, irg, bl, op_be_Barrier, mode_T, n, in);
554 init_node_attr(irn, n);
558 int be_is_Spill (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Spill ; }
559 int be_is_Reload (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Reload ; }
560 int be_is_Copy (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Copy ; }
561 int be_is_Perm (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Perm ; }
562 int be_is_Keep (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Keep ; }
563 int be_is_Call (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Call ; }
564 int be_is_Return (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Return ; }
565 int be_is_IncSP (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_IncSP ; }
566 int be_is_SetSP (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_SetSP ; }
567 int be_is_AddSP (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_AddSP ; }
568 int be_is_RegParams (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_RegParams ; }
569 int be_is_StackParam (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_StackParam ; }
570 int be_is_FrameAddr (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameAddr ; }
571 int be_is_FrameLoad (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameLoad ; }
572 int be_is_FrameStore (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameStore ; }
573 int be_is_Barrier (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Barrier ; }
575 int be_has_frame_entity(const ir_node *irn)
577 switch(be_get_irn_opcode(irn)) {
590 entity *be_get_frame_entity(const ir_node *irn)
592 if(be_has_frame_entity(irn)) {
593 be_frame_attr_t *a = get_irn_attr(irn);
599 static void be_limited(void *data, bitset_t *bs)
601 be_req_t *req = data;
604 case be_req_kind_negate_old_limited:
605 case be_req_kind_old_limited:
606 req->x.old_limited.old_limited(req->x.old_limited.old_limited_env, bs);
607 if(req->kind == be_req_kind_negate_old_limited)
610 case be_req_kind_single_reg:
611 bitset_clear_all(bs);
612 bitset_set(bs, req->x.single_reg->index);
617 static INLINE be_req_t *get_req(ir_node *irn, int pos)
619 int idx = pos < 0 ? -(pos + 1) : pos;
620 be_node_attr_t *a = get_irn_attr(irn);
621 be_reg_data_t *rd = &a->reg_data[idx];
622 be_req_t *r = pos < 0 ? &rd->req : &rd->in_req;
624 assert(is_be_node(irn));
625 assert(!(pos >= 0) || pos < get_irn_arity(irn));
626 assert(!(pos < 0) || -(pos + 1) <= a->max_reg_data);
631 void be_set_constr_single_reg(ir_node *irn, int pos, const arch_register_t *reg)
633 be_req_t *r = get_req(irn, pos);
635 r->kind = be_req_kind_single_reg;
636 r->x.single_reg = reg;
637 r->req.limited = be_limited;
638 r->req.limited_env = r;
639 r->req.type = arch_register_req_type_limited;
640 r->req.cls = reg->reg_class;
643 void be_set_constr_limited(ir_node *irn, int pos, const arch_register_req_t *req)
645 be_req_t *r = get_req(irn, pos);
647 assert(arch_register_req_is(req, limited));
649 r->kind = be_req_kind_old_limited;
650 r->req.limited = be_limited;
651 r->req.limited_env = r;
652 r->req.type = arch_register_req_type_limited;
653 r->req.cls = req->cls;
655 r->x.old_limited.old_limited = req->limited;
656 r->x.old_limited.old_limited_env = req->limited_env;
659 void be_node_set_flags(ir_node *irn, int pos, arch_irn_flags_t flags)
661 be_req_t *r = get_req(irn, pos);
665 void be_node_set_reg_class(ir_node *irn, int pos, const arch_register_class_t *cls)
667 be_req_t *r = get_req(irn, pos);
669 if(r->req.type == arch_register_req_type_none)
670 r->req.type = arch_register_req_type_normal;
673 void be_node_set_req_type(ir_node *irn, int pos, arch_register_req_type_t type)
675 be_req_t *r = get_req(irn, pos);
679 void be_set_IncSP_offset(ir_node *irn, unsigned offset)
681 be_stack_attr_t *a = get_irn_attr(irn);
682 assert(be_is_IncSP(irn));
686 unsigned be_get_IncSP_offset(const ir_node *irn)
688 be_stack_attr_t *a = get_irn_attr(irn);
689 assert(be_is_IncSP(irn));
693 void be_set_IncSP_direction(ir_node *irn, be_stack_dir_t dir)
695 be_stack_attr_t *a = get_irn_attr(irn);
696 assert(be_is_IncSP(irn));
700 be_stack_dir_t be_get_IncSP_direction(const ir_node *irn)
702 be_stack_attr_t *a = get_irn_attr(irn);
703 assert(be_is_IncSP(irn));
707 void be_set_Spill_entity(ir_node *irn, entity *ent)
709 be_spill_attr_t *a = get_irn_attr(irn);
710 assert(be_is_Spill(irn));
711 a->frame_attr.ent = ent;
714 static ir_node *find_a_spill_walker(ir_node *irn, unsigned visited_nr)
716 unsigned nr = get_irn_visited(irn);
718 set_irn_visited(irn, visited_nr);
722 if(nr < visited_nr) {
723 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
724 ir_node *n = find_a_spill_walker(get_irn_n(irn, i), visited_nr);
731 else if(be_get_irn_opcode(irn) == beo_Spill)
737 ir_node *be_get_Spill_context(const ir_node *irn) {
738 const be_spill_attr_t *a = get_irn_attr(irn);
739 assert(be_is_Spill(irn));
744 * Finds a spill for a reload.
745 * If the reload is directly using the spill, this is simple,
746 * else we perform DFS from the reload (over all PhiMs) and return
747 * the first spill node we find.
749 static INLINE ir_node *find_a_spill(const ir_node *irn)
751 ir_graph *irg = get_irn_irg(irn);
752 unsigned visited_nr = get_irg_visited(irg) + 1;
754 assert(be_is_Reload(irn));
755 set_irg_visited(irg, visited_nr);
756 return find_a_spill_walker(be_get_Reload_mem(irn), visited_nr);
759 entity *be_get_spill_entity(const ir_node *irn)
761 int opc = get_irn_opcode(irn);
763 switch(be_get_irn_opcode(irn)) {
766 ir_node *spill = find_a_spill(irn);
767 return be_get_spill_entity(spill);
771 be_spill_attr_t *a = get_irn_attr(irn);
772 return a->frame_attr.ent;
775 assert(0 && "Must give spill/reload node");
782 static void link_reload_walker(ir_node *irn, void *data)
784 ir_node **root = (ir_node **) data;
785 if(be_is_Reload(irn)) {
786 set_irn_link(irn, *root);
791 void be_copy_entities_to_reloads(ir_graph *irg)
794 irg_walk_graph(irg, link_reload_walker, NULL, (void *) &irn);
797 be_frame_attr_t *a = get_irn_attr(irn);
798 entity *ent = be_get_spill_entity(irn);
800 irn = get_irn_link(irn);
804 ir_node *be_spill(const arch_env_t *arch_env, ir_node *irn, ir_node *ctx)
806 ir_node *bl = get_nodes_block(irn);
807 ir_graph *irg = get_irn_irg(bl);
808 ir_node *frame = get_irg_frame(irg);
809 ir_node *insert = bl;
812 const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, irn, -1);
813 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
815 spill = be_new_Spill(cls, cls_frame, irg, bl, frame, irn, ctx);
818 * search the right insertion point. a spill of a phi cannot be put
819 * directly after the phi, if there are some phis behind the one which
820 * is spilled. Also, a spill of a Proj must be after all Projs of the
823 * Here's one special case:
824 * If the spill is in the start block, the spill must be after the frame
825 * pointer is set up. This is done by setting insert to the end of the block
826 * which is its default initialization (see above).
829 insert = sched_next(irn);
830 if(bl == get_irg_start_block(irg) && insert != bl && sched_get_time_step(frame) >= sched_get_time_step(insert))
831 insert = sched_next(frame);
833 while((is_Phi(insert) || is_Proj(insert)) && !sched_is_end(insert))
834 insert = sched_next(insert);
836 sched_add_before(insert, spill);
840 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)
844 ir_node *bl = is_Block(insert) ? insert : get_nodes_block(insert);
845 ir_graph *irg = get_irn_irg(bl);
846 ir_node *frame = get_irg_frame(irg);
847 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
849 assert(be_is_Spill(spill) || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
851 reload = be_new_Reload(cls, cls_frame, irg, bl, frame, spill, mode);
853 if(is_Block(insert)) {
854 insert = sched_skip(insert, 0, sched_skip_cf_predicator, (void *) arch_env);
855 sched_add_after(insert, reload);
859 sched_add_before(insert, reload);
866 | _ \ ___ __ _ | _ \ ___ __ _ ___
867 | |_) / _ \/ _` | | |_) / _ \/ _` / __|
868 | _ < __/ (_| | | _ < __/ (_| \__ \
869 |_| \_\___|\__, | |_| \_\___|\__, |___/
875 static void *put_out_reg_req(arch_register_req_t *req, const ir_node *irn, int out_pos)
877 const be_node_attr_t *a = get_irn_attr(irn);
879 if(out_pos < a->max_reg_data) {
880 memcpy(req, &a->reg_data[out_pos].req, sizeof(req[0]));
882 if(be_is_Copy(irn)) {
883 req->type |= arch_register_req_type_should_be_same;
884 req->other_same = get_irn_n(irn, be_pos_Copy_orig);
888 req->type = arch_register_req_type_none;
895 static void *put_in_reg_req(arch_register_req_t *req, const ir_node *irn, int pos)
897 const be_node_attr_t *a = get_irn_attr(irn);
898 int n = get_irn_arity(irn);
900 if(pos < get_irn_arity(irn) && pos < a->max_reg_data)
901 memcpy(req, &a->reg_data[pos].in_req, sizeof(req[0]));
903 req->type = arch_register_req_type_none;
910 static const arch_register_req_t *
911 be_node_get_irn_reg_req(const void *self, arch_register_req_t *req, const ir_node *irn, int pos)
916 if(get_irn_mode(irn) == mode_T)
919 out_pos = redir_proj((const ir_node **) &irn, pos);
920 assert(is_be_node(irn));
921 return put_out_reg_req(req, irn, out_pos);
925 return is_be_node(irn) ? put_in_reg_req(req, irn, pos) : NULL;
931 const arch_register_t *
932 be_node_get_irn_reg(const void *_self, const ir_node *irn)
937 out_pos = redir_proj((const ir_node **) &irn, -1);
938 a = get_irn_attr(irn);
940 assert(is_be_node(irn));
941 assert(out_pos < a->max_reg_data && "position too high");
943 return a->reg_data[out_pos].reg;
946 static arch_irn_class_t be_node_classify(const void *_self, const ir_node *irn)
948 redir_proj((const ir_node **) &irn, -1);
950 switch(be_get_irn_opcode(irn)) {
951 #define XXX(a,b) case beo_ ## a: return arch_irn_class_ ## b;
964 static arch_irn_flags_t be_node_get_flags(const void *_self, const ir_node *irn)
969 out_pos = redir_proj((const ir_node **) &irn, -1);
970 a = get_irn_attr(irn);
972 assert(is_be_node(irn));
973 assert(out_pos < a->max_reg_data && "position too high");
975 return a->reg_data[out_pos].req.flags;
978 static entity *be_node_get_frame_entity(const void *self, const ir_node *irn)
980 return be_get_frame_entity(irn);
983 static void be_node_set_frame_offset(const void *self, ir_node *irn, int offset)
985 if(be_has_frame_entity(irn)) {
986 be_frame_attr_t *a = get_irn_attr(irn);
993 |_ _| _ \| \ | | | | | | __ _ _ __ __| | | ___ _ __
994 | || |_) | \| | | |_| |/ _` | '_ \ / _` | |/ _ \ '__|
995 | || _ <| |\ | | _ | (_| | | | | (_| | | __/ |
996 |___|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
1000 static const arch_irn_ops_if_t be_node_irn_ops_if = {
1001 be_node_get_irn_reg_req,
1002 be_node_set_irn_reg,
1003 be_node_get_irn_reg,
1006 be_node_get_frame_entity,
1007 be_node_set_frame_offset
1010 static const arch_irn_ops_t be_node_irn_ops = {
1014 const void *be_node_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn)
1016 redir_proj((const ir_node **) &irn, -1);
1017 return is_be_node(irn) ? &be_node_irn_ops : NULL;
1020 const arch_irn_handler_t be_node_irn_handler = {
1025 ____ _ _ ___ ____ _ _ _ _ _ _
1026 | _ \| |__ (_) |_ _| _ \| \ | | | | | | __ _ _ __ __| | | ___ _ __
1027 | |_) | '_ \| | | || |_) | \| | | |_| |/ _` | '_ \ / _` | |/ _ \ '__|
1028 | __/| | | | | | || _ <| |\ | | _ | (_| | | | | (_| | | __/ |
1029 |_| |_| |_|_| |___|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
1034 arch_irn_handler_t irn_handler;
1035 arch_irn_ops_t irn_ops;
1036 const arch_env_t *arch_env;
1040 #define get_phi_handler_from_handler(h) container_of(h, phi_handler_t, irn_handler)
1041 #define get_phi_handler_from_ops(h) container_of(h, phi_handler_t, irn_ops)
1043 static const void *phi_get_irn_ops(const arch_irn_handler_t *handler, const ir_node *irn)
1045 const phi_handler_t *h = get_phi_handler_from_handler(handler);
1046 return is_Phi(irn) && mode_is_datab(get_irn_mode(irn)) ? &h->irn_ops : NULL;
1050 * Get register class of a Phi.
1053 static const arch_register_req_t *get_Phi_reg_req_recursive(const phi_handler_t *h, arch_register_req_t *req, const ir_node *phi, pset **visited)
1055 int n = get_irn_arity(phi);
1060 if(*visited && pset_find_ptr(*visited, phi))
1063 for(i = 0; i < n; ++i) {
1064 op = get_irn_n(phi, i);
1066 return arch_get_register_req(h->arch_env, req, op, BE_OUT_POS(0));
1070 The operands of that Phi were all Phis themselves.
1071 We have to start a DFS for a non-Phi argument now.
1074 *visited = pset_new_ptr(16);
1076 pset_insert_ptr(*visited, phi);
1078 for(i = 0; i < n; ++i) {
1079 op = get_irn_n(phi, i);
1080 if(get_Phi_reg_req_recursive(h, req, op, visited))
1087 static const arch_register_req_t *phi_get_irn_reg_req(const void *self, arch_register_req_t *req, const ir_node *irn, int pos)
1089 phi_handler_t *phi_handler = get_phi_handler_from_ops(self);
1090 pset *visited = NULL;
1092 get_Phi_reg_req_recursive(phi_handler, req, irn, &visited);
1093 /* Set the requirements type to normal, since an operand of the Phi could have had constraints. */
1094 req->type = arch_register_req_type_normal;
1101 static void phi_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg)
1103 phi_handler_t *h = get_phi_handler_from_ops(self);
1104 pmap_insert(h->regs, irn, (void *) reg);
1107 static const arch_register_t *phi_get_irn_reg(const void *self, const ir_node *irn)
1109 phi_handler_t *h = get_phi_handler_from_ops(self);
1110 return pmap_get(h->regs, (void *) irn);
1113 static arch_irn_class_t phi_classify(const void *_self, const ir_node *irn)
1115 return arch_irn_class_normal;
1118 static arch_irn_flags_t phi_get_flags(const void *_self, const ir_node *irn)
1120 return arch_irn_flags_none;
1123 static entity *phi_get_frame_entity(const void *_self, const ir_node *irn)
1128 static void phi_set_frame_offset(const void *_self, ir_node *irn, int bias)
1132 static const arch_irn_ops_if_t phi_irn_ops = {
1133 phi_get_irn_reg_req,
1138 phi_get_frame_entity,
1139 phi_set_frame_offset
1142 static const arch_irn_handler_t phi_irn_handler = {
1146 arch_irn_handler_t *be_phi_handler_new(const arch_env_t *arch_env)
1148 phi_handler_t *h = xmalloc(sizeof(h[0]));
1149 h->irn_handler.get_irn_ops = phi_get_irn_ops;
1150 h->irn_ops.impl = &phi_irn_ops;
1151 h->arch_env = arch_env;
1152 h->regs = pmap_create();
1153 return (arch_irn_handler_t *) h;
1156 void be_phi_handler_free(arch_irn_handler_t *handler)
1158 phi_handler_t *h = (void *) handler;
1159 pmap_destroy(h->regs);
1163 const void *be_phi_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn)
1165 phi_handler_t *phi_handler = get_phi_handler_from_handler(self);
1166 return is_Phi(irn) ? &phi_handler->irn_ops : NULL;
1169 void be_phi_handler_reset(arch_irn_handler_t *handler)
1171 phi_handler_t *h = get_phi_handler_from_handler(handler);
1173 pmap_destroy(h->regs);
1174 h->regs = pmap_create();
1180 | \ | | ___ __| | ___ | _ \ _ _ _ __ ___ _ __ (_)_ __ __ _
1181 | \| |/ _ \ / _` |/ _ \ | | | | | | | '_ ` _ \| '_ \| | '_ \ / _` |
1182 | |\ | (_) | (_| | __/ | |_| | |_| | | | | | | |_) | | | | | (_| |
1183 |_| \_|\___/ \__,_|\___| |____/ \__,_|_| |_| |_| .__/|_|_| |_|\__, |
1187 static void dump_node_req(FILE *f, int idx, be_req_t *req)
1190 int did_something = 0;
1192 const char *prefix = buf;
1194 snprintf(buf, sizeof(buf), "#%d ", idx);
1196 if(req->flags != arch_irn_flags_none) {
1197 fprintf(f, "%sflags: ", prefix);
1199 for(i = arch_irn_flags_none; i <= log2_ceil(arch_irn_flags_last); ++i) {
1200 if(req->flags & (1 << i)) {
1201 fprintf(f, "%s%s", prefix, arch_irn_flag_str(1 << i));
1209 if(req->req.cls != 0) {
1212 arch_register_req_format(tmp, sizeof(tmp), &req->req);
1213 fprintf(f, "%s", tmp);
1221 static void dump_node_reqs(FILE *f, ir_node *irn)
1224 be_node_attr_t *a = get_irn_attr(irn);
1226 fprintf(f, "registers: \n");
1227 for(i = 0; i < a->max_reg_data; ++i) {
1228 be_reg_data_t *rd = &a->reg_data[i];
1230 fprintf(f, "#%d: %s\n", i, rd->reg->name);
1233 fprintf(f, "in requirements\n");
1234 for(i = 0; i < a->max_reg_data; ++i) {
1235 dump_node_req(f, i, &a->reg_data[i].in_req);
1238 fprintf(f, "\nout requirements\n");
1239 for(i = 0; i < a->max_reg_data; ++i) {
1240 dump_node_req(f, i, &a->reg_data[i].req);
1244 static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason)
1246 be_node_attr_t *at = get_irn_attr(irn);
1248 assert(is_be_node(irn));
1251 case dump_node_opcode_txt:
1252 fprintf(f, get_op_name(get_irn_op(irn)));
1254 case dump_node_mode_txt:
1255 fprintf(f, get_mode_name(get_irn_mode(irn)));
1257 case dump_node_nodeattr_txt:
1259 case dump_node_info_txt:
1260 dump_node_reqs(f, irn);
1262 if(be_has_frame_entity(irn)) {
1263 be_frame_attr_t *a = (be_frame_attr_t *) at;
1265 ir_fprintf(f, "frame entity: %+F offset %x (%d)\n", a->ent, a->offset, a->offset);
1269 switch(be_get_irn_opcode(irn)) {
1272 be_spill_attr_t *a = (be_spill_attr_t *) at;
1273 ir_fprintf(f, "spill context: %+F\n", a->spill_ctx);
1279 be_stack_attr_t *a = (be_stack_attr_t *) at;
1280 fprintf(f, "offset: %u\n", a->offset);
1281 fprintf(f, "direction: %s\n", a->dir == be_stack_dir_expand ? "expand" : "shrink");
1292 * Copies the backend specific attributes from old node to new node.
1294 static void copy_attr(const ir_node *old_node, ir_node *new_node)
1296 be_node_attr_t *old_attr = get_irn_attr(old_node);
1297 be_node_attr_t *new_attr = get_irn_attr(new_node);
1300 assert(is_be_node(old_node));
1301 assert(is_be_node(new_node));
1303 memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
1304 new_attr->reg_data = NULL;
1306 if(new_attr->max_reg_data > 0) {
1307 new_attr->reg_data = NEW_ARR_D(be_reg_data_t, get_irg_obstack(get_irn_irg(new_node)), new_attr->max_reg_data);
1308 memcpy(new_attr->reg_data, old_attr->reg_data, new_attr->max_reg_data * sizeof(be_reg_data_t));
1310 for(i = 0; i < old_attr->max_reg_data; ++i) {
1313 r = &new_attr->reg_data[i].req;
1314 r->req.limited_env = r;
1316 r = &new_attr->reg_data[i].in_req;
1317 r->req.limited_env = r;
1322 static const ir_op_ops be_node_op_ops = {