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;
87 /** The generic be nodes attribute type. */
90 be_reg_data_t *reg_data;
93 /** The be_Stack attribute type. */
95 be_node_attr_t node_attr;
96 int offset; /**< The offset by which the stack shall be increased/decreased. */
97 be_stack_dir_t dir; /**< The direction in which the stack shall be modified (expand or shrink). */
100 /** The be_Frame attribute type. */
102 be_node_attr_t node_attr;
107 /** The be_Call attribute type. */
109 be_node_attr_t node_attr;
110 entity *ent; /**< The called entity if this is a static call. */
111 ir_type *call_tp; /**< The call type, copied from the original Call node. */
114 /** The be_Spill attribute type. */
116 be_frame_attr_t frame_attr;
117 ir_node *spill_ctx; /**< The node in whose context this spill was introduced. */
125 ir_op *op_be_CopyKeep;
131 ir_op *op_be_RegParams;
132 ir_op *op_be_StackParam;
133 ir_op *op_be_FrameAddr;
134 ir_op *op_be_FrameLoad;
135 ir_op *op_be_FrameStore;
136 ir_op *op_be_Barrier;
138 static int beo_base = -1;
140 static const ir_op_ops be_node_op_ops;
142 #define N irop_flag_none
143 #define L irop_flag_labeled
144 #define C irop_flag_commutative
145 #define X irop_flag_cfopcode
146 #define I irop_flag_ip_cfopcode
147 #define F irop_flag_fragile
148 #define Y irop_flag_forking
149 #define H irop_flag_highlevel
150 #define c irop_flag_constlike
151 #define K irop_flag_keep
152 #define M irop_flag_machine
155 void be_node_init(void) {
156 static int inited = 0;
163 /* Acquire all needed opcodes. */
164 beo_base = get_next_ir_opcodes(beo_Last - 1);
166 op_be_Spill = new_ir_op(beo_base + beo_Spill, "be_Spill", op_pin_state_mem_pinned, N, oparity_unary, 0, sizeof(be_spill_attr_t), &be_node_op_ops);
167 op_be_Reload = new_ir_op(beo_base + beo_Reload, "be_Reload", op_pin_state_mem_pinned, N, oparity_zero, 0, sizeof(be_frame_attr_t), &be_node_op_ops);
168 op_be_Perm = new_ir_op(beo_base + beo_Perm, "be_Perm", op_pin_state_pinned, N, oparity_variable, 0, sizeof(be_node_attr_t), &be_node_op_ops);
169 op_be_Copy = new_ir_op(beo_base + beo_Copy, "be_Copy", op_pin_state_floats, N, oparity_unary, 0, sizeof(be_node_attr_t), &be_node_op_ops);
170 op_be_Keep = new_ir_op(beo_base + beo_Keep, "be_Keep", op_pin_state_pinned, K, oparity_variable, 0, sizeof(be_node_attr_t), &be_node_op_ops);
171 op_be_CopyKeep = new_ir_op(beo_base + beo_CopyKeep, "be_CopyKeep", op_pin_state_pinned, K, oparity_variable, 0, sizeof(be_node_attr_t), &be_node_op_ops);
172 op_be_Call = new_ir_op(beo_base + beo_Call, "be_Call", op_pin_state_pinned, N, oparity_variable, 0, sizeof(be_call_attr_t), &be_node_op_ops);
173 op_be_Return = new_ir_op(beo_base + beo_Return, "be_Return", op_pin_state_pinned, X, oparity_variable, 0, sizeof(be_node_attr_t), &be_node_op_ops);
174 op_be_AddSP = new_ir_op(beo_base + beo_AddSP, "be_AddSP", op_pin_state_pinned, N, oparity_unary, 0, sizeof(be_node_attr_t), &be_node_op_ops);
175 op_be_SetSP = new_ir_op(beo_base + beo_SetSP, "be_SetSP", op_pin_state_pinned, N, oparity_binary, 0, sizeof(be_stack_attr_t), &be_node_op_ops);
176 op_be_IncSP = new_ir_op(beo_base + beo_IncSP, "be_IncSP", op_pin_state_pinned, N, oparity_binary, 0, sizeof(be_stack_attr_t), &be_node_op_ops);
177 op_be_RegParams = new_ir_op(beo_base + beo_RegParams, "be_RegParams", op_pin_state_pinned, N, oparity_zero, 0, sizeof(be_node_attr_t), &be_node_op_ops);
178 op_be_StackParam = new_ir_op(beo_base + beo_StackParam, "be_StackParam", op_pin_state_pinned, N, oparity_unary, 0, sizeof(be_frame_attr_t), &be_node_op_ops);
179 op_be_FrameAddr = new_ir_op(beo_base + beo_FrameAddr, "be_FrameAddr", op_pin_state_pinned, N, oparity_binary, 0, sizeof(be_frame_attr_t), &be_node_op_ops);
180 op_be_FrameLoad = new_ir_op(beo_base + beo_FrameLoad, "be_FrameLoad", op_pin_state_pinned, N, oparity_any, 0, sizeof(be_frame_attr_t), &be_node_op_ops);
181 op_be_FrameStore = new_ir_op(beo_base + beo_FrameStore, "be_FrameStore", op_pin_state_pinned, N, oparity_any, 0, sizeof(be_frame_attr_t), &be_node_op_ops);
182 op_be_Barrier = new_ir_op(beo_base + beo_Barrier, "be_Barrier", op_pin_state_pinned, N, oparity_any, 0, sizeof(be_node_attr_t), &be_node_op_ops);
184 set_op_tag(op_be_Spill, &be_node_tag);
185 set_op_tag(op_be_Reload, &be_node_tag);
186 set_op_tag(op_be_Perm, &be_node_tag);
187 set_op_tag(op_be_Copy, &be_node_tag);
188 set_op_tag(op_be_Keep, &be_node_tag);
189 set_op_tag(op_be_CopyKeep, &be_node_tag);
190 set_op_tag(op_be_Call, &be_node_tag);
191 set_op_tag(op_be_Return, &be_node_tag);
192 set_op_tag(op_be_AddSP, &be_node_tag);
193 set_op_tag(op_be_SetSP, &be_node_tag);
194 set_op_tag(op_be_IncSP, &be_node_tag);
195 set_op_tag(op_be_RegParams, &be_node_tag);
196 set_op_tag(op_be_StackParam, &be_node_tag);
197 set_op_tag(op_be_FrameLoad, &be_node_tag);
198 set_op_tag(op_be_FrameStore, &be_node_tag);
199 set_op_tag(op_be_FrameAddr, &be_node_tag);
200 set_op_tag(op_be_Barrier, &be_node_tag);
204 * Initializes the generic attribute of all be nodes and return ir.
206 static void *init_node_attr(ir_node* irn, int max_reg_data)
208 ir_graph *irg = get_irn_irg(irn);
209 be_node_attr_t *a = get_irn_attr(irn);
211 memset(a, 0, sizeof(get_op_attr_size(get_irn_op(irn))));
212 a->max_reg_data = max_reg_data;
215 if(max_reg_data > 0) {
218 a->reg_data = NEW_ARR_D(be_reg_data_t, get_irg_obstack(irg), max_reg_data);
219 memset(a->reg_data, 0, max_reg_data * sizeof(a->reg_data[0]));
220 for(i = 0; i < max_reg_data; ++i) {
221 a->reg_data[i].req.req.cls = NULL;
222 a->reg_data[i].req.req.type = arch_register_req_type_none;
229 static INLINE int is_be_node(const ir_node *irn)
231 return get_op_tag(get_irn_op(irn)) == &be_node_tag;
234 be_opcode_t be_get_irn_opcode(const ir_node *irn)
236 return is_be_node(irn) ? get_irn_opcode(irn) - beo_base : beo_NoBeOp;
239 static int redir_proj(const ir_node **node, int pos)
241 const ir_node *n = *node;
246 assert(pos == -1 && "Illegal pos for a Proj");
247 *node = irn = get_Proj_pred(n);
249 assert(get_irn_mode(irn) == mode_T);
250 *node = get_Proj_pred(irn);
252 return get_Proj_proj(n);
259 be_node_set_irn_reg(const void *_self, ir_node *irn, const arch_register_t *reg)
264 out_pos = redir_proj((const ir_node **) &irn, -1);
265 a = get_irn_attr(irn);
267 assert(is_be_node(irn));
268 assert(out_pos < a->max_reg_data && "position too high");
269 a->reg_data[out_pos].reg = reg;
273 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)
281 res = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
282 a = init_node_attr(res, 2);
283 a->frame_attr.ent = NULL;
284 a->frame_attr.offset = 0;
287 be_node_set_reg_class(res, 0, cls_frame);
288 be_node_set_reg_class(res, 1, cls);
292 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)
299 res = new_ir_node(NULL, irg, bl, op_be_Reload, mode, 2, in);
300 init_node_attr(res, 2);
301 be_node_set_reg_class(res, 0, cls_frame);
302 be_node_set_reg_class(res, -1, cls);
306 ir_node *(be_get_Reload_mem)(const ir_node *irn)
308 assert(be_is_Reload(irn));
309 return get_irn_n(irn, be_pos_Reload_mem);
312 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
315 ir_node *irn = new_ir_node(NULL, irg, bl, op_be_Perm, mode_T, n, in);
316 init_node_attr(irn, n);
317 for(i = 0; i < n; ++i) {
318 be_node_set_reg_class(irn, i, cls);
319 be_node_set_reg_class(irn, OUT_POS(i), cls);
325 ir_node *be_new_Copy(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *op)
331 res = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
332 init_node_attr(res, 1);
333 be_node_set_reg_class(res, 0, cls);
334 be_node_set_reg_class(res, OUT_POS(0), cls);
338 ir_node *be_get_Copy_op(const ir_node *cpy) {
339 return get_irn_n(cpy, be_pos_Copy_op);
342 ir_node *be_new_Keep(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
347 irn = new_ir_node(NULL, irg, bl, op_be_Keep, mode_ANY, n, in);
348 init_node_attr(irn, n);
349 for(i = 0; i < n; ++i) {
350 be_node_set_reg_class(irn, i, cls);
356 ir_node *be_new_Call(dbg_info *dbg, ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *sp, ir_node *ptr,
357 int n_outs, int n, ir_node *in[], ir_type *call_tp)
360 int real_n = be_pos_Call_first_arg + n;
364 NEW_ARR_A(ir_node *, real_in, real_n);
365 real_in[be_pos_Call_mem] = mem;
366 real_in[be_pos_Call_sp] = sp;
367 real_in[be_pos_Call_ptr] = ptr;
368 memcpy(&real_in[be_pos_Call_first_arg], in, n * sizeof(in[0]));
370 irn = new_ir_node(NULL, irg, bl, op_be_Call, mode_T, real_n, real_in);
371 a = init_node_attr(irn, (n_outs > real_n ? n_outs : real_n));
373 a->call_tp = call_tp;
377 /* Gets the call entity or NULL if this is no static call. */
378 entity *be_Call_get_entity(const ir_node *call) {
379 be_call_attr_t *a = get_irn_attr(call);
380 assert(be_is_Call(call));
384 /* Sets the call entity. */
385 void be_Call_set_entity(ir_node *call, entity *ent) {
386 be_call_attr_t *a = get_irn_attr(call);
387 assert(be_is_Call(call));
391 /* Gets the call type. */
392 ir_type *be_Call_get_type(ir_node *call) {
393 be_call_attr_t *a = get_irn_attr(call);
394 assert(be_is_Call(call));
398 /* Sets the call type. */
399 void be_Call_set_type(ir_node *call, ir_type *call_tp) {
400 be_call_attr_t *a = get_irn_attr(call);
401 assert(be_is_Call(call));
402 a->call_tp = call_tp;
406 ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
408 ir_node *irn = new_ir_node(dbg, irg, bl, op_be_Return, mode_X, n, in);
409 init_node_attr(irn, n);
414 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)
422 irn = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode, 2, in);
423 a = init_node_attr(irn, 1);
427 be_node_set_flags(irn, -1, arch_irn_flags_ignore);
429 /* Set output constraint to stack register. */
430 be_node_set_reg_class(irn, 0, sp->reg_class);
431 be_set_constr_single_reg(irn, -1, sp);
432 be_node_set_irn_reg(NULL, irn, sp);
437 ir_node *be_new_AddSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, ir_node *sz)
441 ir_node *in[be_pos_AddSP_last];
443 in[be_pos_AddSP_old_sp] = old_sp;
444 in[be_pos_AddSP_size] = sz;
446 irn = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T, be_pos_AddSP_last, in);
447 a = init_node_attr(irn, be_pos_AddSP_last);
449 be_node_set_flags(irn, OUT_POS(0), arch_irn_flags_ignore);
451 /* Set output constraint to stack register. */
452 be_set_constr_single_reg(irn, OUT_POS(0), sp);
457 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)
466 irn = new_ir_node(NULL, irg, bl, op_be_SetSP, get_irn_mode(old_sp), 3, in);
467 a = init_node_attr(irn, 3);
469 be_node_set_flags(irn, OUT_POS(0), arch_irn_flags_ignore);
471 /* Set output constraint to stack register. */
472 be_set_constr_single_reg(irn, OUT_POS(0), sp);
473 be_node_set_reg_class(irn, 1, sp->reg_class);
474 be_node_set_reg_class(irn, 2, sp->reg_class);
475 be_node_set_irn_reg(NULL, irn, sp);
480 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)
486 in[0] = frame_pointer;
487 irn = new_ir_node(NULL, irg, bl, op_be_StackParam, mode, 1, in);
488 a = init_node_attr(irn, 1);
491 be_node_set_reg_class(irn, 0, cls_frame);
492 be_node_set_reg_class(irn, OUT_POS(0), cls);
496 ir_node *be_new_RegParams(ir_graph *irg, ir_node *bl, int n_outs)
501 irn = new_ir_node(NULL, irg, bl, op_be_RegParams, mode_T, 0, in);
502 init_node_attr(irn, n_outs);
506 ir_node *be_new_FrameLoad(const arch_register_class_t *cls_frame, const arch_register_class_t *cls_data,
507 ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *frame, entity *ent)
515 irn = new_ir_node(NULL, irg, bl, op_be_FrameLoad, mode_T, 2, in);
516 a = init_node_attr(irn, 3);
519 be_node_set_reg_class(irn, 1, cls_frame);
520 be_node_set_reg_class(irn, OUT_POS(pn_Load_res), cls_data);
524 ir_node *be_new_FrameStore(const arch_register_class_t *cls_frame, const arch_register_class_t *cls_data,
525 ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *frame, ir_node *data, entity *ent)
534 irn = new_ir_node(NULL, irg, bl, op_be_FrameStore, mode_T, 3, in);
535 a = init_node_attr(irn, 3);
538 be_node_set_reg_class(irn, 1, cls_frame);
539 be_node_set_reg_class(irn, 2, cls_data);
543 ir_node *be_new_FrameAddr(const arch_register_class_t *cls_frame, ir_graph *irg, ir_node *bl, ir_node *frame, entity *ent)
550 irn = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
551 a = init_node_attr(irn, 1);
554 be_node_set_reg_class(irn, 0, cls_frame);
555 be_node_set_reg_class(irn, OUT_POS(0), cls_frame);
559 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)
562 ir_node **in = (ir_node **) alloca((n + 1) * sizeof(in[0]));
565 memcpy(&in[1], in_keep, n * sizeof(in[0]));
566 irn = new_ir_node(NULL, irg, bl, op_be_CopyKeep, mode, n + 1, in);
567 init_node_attr(irn, n + 1);
568 be_node_set_reg_class(irn, OUT_POS(0), cls);
569 be_node_set_reg_class(irn, 0, cls);
574 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)
579 return be_new_CopyKeep(cls, irg, bl, src, 1, in, mode);
582 ir_node *be_new_Barrier(ir_graph *irg, ir_node *bl, int n, ir_node *in[])
586 irn = new_ir_node(NULL, irg, bl, op_be_Barrier, mode_T, n, in);
587 init_node_attr(irn, n);
591 int be_is_Spill (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Spill ; }
592 int be_is_Reload (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Reload ; }
593 int be_is_Copy (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Copy ; }
594 int be_is_Perm (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Perm ; }
595 int be_is_Keep (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Keep ; }
596 int be_is_Call (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Call ; }
597 int be_is_Return (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Return ; }
598 int be_is_IncSP (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_IncSP ; }
599 int be_is_SetSP (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_SetSP ; }
600 int be_is_AddSP (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_AddSP ; }
601 int be_is_RegParams (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_RegParams ; }
602 int be_is_StackParam (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_StackParam ; }
603 int be_is_FrameAddr (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameAddr ; }
604 int be_is_FrameLoad (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameLoad ; }
605 int be_is_FrameStore (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameStore ; }
606 int be_is_Barrier (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Barrier ; }
608 int be_has_frame_entity(const ir_node *irn)
610 switch(be_get_irn_opcode(irn)) {
623 entity *be_get_frame_entity(const ir_node *irn)
625 if(be_has_frame_entity(irn)) {
626 be_frame_attr_t *a = get_irn_attr(irn);
632 static void be_limited(void *data, bitset_t *bs)
634 be_req_t *req = data;
637 case be_req_kind_negate_old_limited:
638 case be_req_kind_old_limited:
639 req->x.old_limited.old_limited(req->x.old_limited.old_limited_env, bs);
640 if(req->kind == be_req_kind_negate_old_limited)
643 case be_req_kind_single_reg:
644 bitset_clear_all(bs);
645 bitset_set(bs, req->x.single_reg->index);
650 static INLINE be_req_t *get_req(ir_node *irn, int pos)
652 int idx = pos < 0 ? -(pos + 1) : pos;
653 be_node_attr_t *a = get_irn_attr(irn);
654 be_reg_data_t *rd = &a->reg_data[idx];
655 be_req_t *r = pos < 0 ? &rd->req : &rd->in_req;
657 assert(is_be_node(irn));
658 assert(!(pos >= 0) || pos < get_irn_arity(irn));
659 assert(!(pos < 0) || -(pos + 1) <= a->max_reg_data);
664 void be_set_constr_single_reg(ir_node *irn, int pos, const arch_register_t *reg)
666 be_req_t *r = get_req(irn, pos);
668 r->kind = be_req_kind_single_reg;
669 r->x.single_reg = reg;
670 r->req.limited = be_limited;
671 r->req.limited_env = r;
672 r->req.type = arch_register_req_type_limited;
673 r->req.cls = reg->reg_class;
676 void be_set_constr_limited(ir_node *irn, int pos, const arch_register_req_t *req)
678 be_req_t *r = get_req(irn, pos);
680 assert(arch_register_req_is(req, limited));
682 r->kind = be_req_kind_old_limited;
683 r->req.limited = be_limited;
684 r->req.limited_env = r;
685 r->req.type = arch_register_req_type_limited;
686 r->req.cls = req->cls;
688 r->x.old_limited.old_limited = req->limited;
689 r->x.old_limited.old_limited_env = req->limited_env;
692 void be_node_set_flags(ir_node *irn, int pos, arch_irn_flags_t flags)
694 be_req_t *r = get_req(irn, pos);
698 void be_node_set_reg_class(ir_node *irn, int pos, const arch_register_class_t *cls)
700 be_req_t *r = get_req(irn, pos);
702 if(r->req.type == arch_register_req_type_none)
703 r->req.type = arch_register_req_type_normal;
706 void be_node_set_req_type(ir_node *irn, int pos, arch_register_req_type_t type)
708 be_req_t *r = get_req(irn, pos);
712 ir_node *be_get_IncSP_pred(ir_node *irn) {
713 assert(be_is_IncSP(irn));
714 return get_irn_n(irn, 0);
717 ir_node *be_get_IncSP_mem(ir_node *irn) {
718 assert(be_is_IncSP(irn));
719 return get_irn_n(irn, 1);
722 void be_set_IncSP_offset(ir_node *irn, unsigned offset)
724 be_stack_attr_t *a = get_irn_attr(irn);
725 assert(be_is_IncSP(irn));
729 unsigned be_get_IncSP_offset(const ir_node *irn)
731 be_stack_attr_t *a = get_irn_attr(irn);
732 assert(be_is_IncSP(irn));
736 void be_set_IncSP_direction(ir_node *irn, be_stack_dir_t dir)
738 be_stack_attr_t *a = get_irn_attr(irn);
739 assert(be_is_IncSP(irn));
743 be_stack_dir_t be_get_IncSP_direction(const ir_node *irn)
745 be_stack_attr_t *a = get_irn_attr(irn);
746 assert(be_is_IncSP(irn));
750 void be_set_Spill_entity(ir_node *irn, entity *ent)
752 be_spill_attr_t *a = get_irn_attr(irn);
753 assert(be_is_Spill(irn));
754 a->frame_attr.ent = ent;
757 static ir_node *find_a_spill_walker(ir_node *irn, unsigned visited_nr)
759 unsigned nr = get_irn_visited(irn);
761 set_irn_visited(irn, visited_nr);
765 if(nr < visited_nr) {
766 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
767 ir_node *n = find_a_spill_walker(get_irn_n(irn, i), visited_nr);
774 else if(be_get_irn_opcode(irn) == beo_Spill)
780 ir_node *be_get_Spill_context(const ir_node *irn) {
781 const be_spill_attr_t *a = get_irn_attr(irn);
782 assert(be_is_Spill(irn));
787 * Finds a spill for a reload.
788 * If the reload is directly using the spill, this is simple,
789 * else we perform DFS from the reload (over all PhiMs) and return
790 * the first spill node we find.
792 static INLINE ir_node *find_a_spill(const ir_node *irn)
794 ir_graph *irg = get_irn_irg(irn);
795 unsigned visited_nr = get_irg_visited(irg) + 1;
797 assert(be_is_Reload(irn));
798 set_irg_visited(irg, visited_nr);
799 return find_a_spill_walker(be_get_Reload_mem(irn), visited_nr);
802 entity *be_get_spill_entity(const ir_node *irn)
804 switch(be_get_irn_opcode(irn)) {
807 ir_node *spill = find_a_spill(irn);
808 return be_get_spill_entity(spill);
812 be_spill_attr_t *a = get_irn_attr(irn);
813 return a->frame_attr.ent;
816 assert(0 && "Must give spill/reload node");
823 static void link_reload_walker(ir_node *irn, void *data)
825 ir_node **root = (ir_node **) data;
826 if(be_is_Reload(irn)) {
827 set_irn_link(irn, *root);
832 void be_copy_entities_to_reloads(ir_graph *irg)
835 irg_walk_graph(irg, link_reload_walker, NULL, (void *) &irn);
838 be_frame_attr_t *a = get_irn_attr(irn);
839 entity *ent = be_get_spill_entity(irn);
841 irn = get_irn_link(irn);
845 ir_node *be_spill(const arch_env_t *arch_env, ir_node *irn, ir_node *ctx)
847 ir_node *bl = get_nodes_block(irn);
848 ir_graph *irg = get_irn_irg(bl);
849 ir_node *frame = get_irg_frame(irg);
850 ir_node *insert = bl;
853 const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, irn, -1);
854 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
856 spill = be_new_Spill(cls, cls_frame, irg, bl, frame, irn, ctx);
859 * search the right insertion point. a spill of a phi cannot be put
860 * directly after the phi, if there are some phis behind the one which
861 * is spilled. Also, a spill of a Proj must be after all Projs of the
864 * Here's one special case:
865 * If the spill is in the start block, the spill must be after the frame
866 * pointer is set up. This is done by setting insert to the end of the block
867 * which is its default initialization (see above).
870 insert = sched_next(irn);
871 if(insert != bl && bl == get_irg_start_block(irg) && sched_get_time_step(frame) >= sched_get_time_step(insert))
872 insert = sched_next(frame);
874 while((is_Phi(insert) || is_Proj(insert)) && !sched_is_end(insert))
875 insert = sched_next(insert);
877 sched_add_before(insert, spill);
881 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)
885 ir_node *bl = is_Block(insert) ? insert : get_nodes_block(insert);
886 ir_graph *irg = get_irn_irg(bl);
887 ir_node *frame = get_irg_frame(irg);
888 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
890 assert(be_is_Spill(spill) || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
892 reload = be_new_Reload(cls, cls_frame, irg, bl, frame, spill, mode);
894 if(is_Block(insert)) {
895 insert = sched_skip(insert, 0, sched_skip_cf_predicator, (void *) arch_env);
896 sched_add_after(insert, reload);
900 sched_add_before(insert, reload);
907 | _ \ ___ __ _ | _ \ ___ __ _ ___
908 | |_) / _ \/ _` | | |_) / _ \/ _` / __|
909 | _ < __/ (_| | | _ < __/ (_| \__ \
910 |_| \_\___|\__, | |_| \_\___|\__, |___/
916 static void *put_out_reg_req(arch_register_req_t *req, const ir_node *irn, int out_pos)
918 const be_node_attr_t *a = get_irn_attr(irn);
920 if(out_pos < a->max_reg_data) {
921 memcpy(req, &a->reg_data[out_pos].req, sizeof(req[0]));
923 if(be_is_Copy(irn)) {
924 req->type |= arch_register_req_type_should_be_same;
925 req->other_same = be_get_Copy_op(irn);
929 req->type = arch_register_req_type_none;
936 static void *put_in_reg_req(arch_register_req_t *req, const ir_node *irn, int pos)
938 const be_node_attr_t *a = get_irn_attr(irn);
940 if(pos < get_irn_arity(irn) && pos < a->max_reg_data)
941 memcpy(req, &a->reg_data[pos].in_req, sizeof(req[0]));
943 req->type = arch_register_req_type_none;
950 static const arch_register_req_t *
951 be_node_get_irn_reg_req(const void *self, arch_register_req_t *req, const ir_node *irn, int pos)
956 if(get_irn_mode(irn) == mode_T)
959 out_pos = redir_proj((const ir_node **) &irn, pos);
960 assert(is_be_node(irn));
961 return put_out_reg_req(req, irn, out_pos);
965 return is_be_node(irn) ? put_in_reg_req(req, irn, pos) : NULL;
971 const arch_register_t *
972 be_node_get_irn_reg(const void *_self, const ir_node *irn)
977 out_pos = redir_proj((const ir_node **) &irn, -1);
978 a = get_irn_attr(irn);
980 assert(is_be_node(irn));
981 assert(out_pos < a->max_reg_data && "position too high");
983 return a->reg_data[out_pos].reg;
986 static arch_irn_class_t be_node_classify(const void *_self, const ir_node *irn)
988 redir_proj((const ir_node **) &irn, -1);
990 switch(be_get_irn_opcode(irn)) {
991 #define XXX(a,b) case beo_ ## a: return arch_irn_class_ ## b
1005 static arch_irn_flags_t be_node_get_flags(const void *_self, const ir_node *irn)
1010 out_pos = redir_proj((const ir_node **) &irn, -1);
1011 a = get_irn_attr(irn);
1013 assert(is_be_node(irn));
1014 assert(out_pos < a->max_reg_data && "position too high");
1016 return a->reg_data[out_pos].req.flags;
1019 static entity *be_node_get_frame_entity(const void *self, const ir_node *irn)
1021 return be_get_frame_entity(irn);
1024 static void be_node_set_frame_offset(const void *self, ir_node *irn, int offset)
1026 if(be_has_frame_entity(irn)) {
1027 be_frame_attr_t *a = get_irn_attr(irn);
1033 ___ ____ _ _ _ _ _ _
1034 |_ _| _ \| \ | | | | | | __ _ _ __ __| | | ___ _ __
1035 | || |_) | \| | | |_| |/ _` | '_ \ / _` | |/ _ \ '__|
1036 | || _ <| |\ | | _ | (_| | | | | (_| | | __/ |
1037 |___|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
1041 static const arch_irn_ops_if_t be_node_irn_ops_if = {
1042 be_node_get_irn_reg_req,
1043 be_node_set_irn_reg,
1044 be_node_get_irn_reg,
1047 be_node_get_frame_entity,
1048 be_node_set_frame_offset
1051 static const arch_irn_ops_t be_node_irn_ops = {
1055 const void *be_node_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn)
1057 redir_proj((const ir_node **) &irn, -1);
1058 return is_be_node(irn) ? &be_node_irn_ops : NULL;
1061 const arch_irn_handler_t be_node_irn_handler = {
1066 ____ _ _ ___ ____ _ _ _ _ _ _
1067 | _ \| |__ (_) |_ _| _ \| \ | | | | | | __ _ _ __ __| | | ___ _ __
1068 | |_) | '_ \| | | || |_) | \| | | |_| |/ _` | '_ \ / _` | |/ _ \ '__|
1069 | __/| | | | | | || _ <| |\ | | _ | (_| | | | | (_| | | __/ |
1070 |_| |_| |_|_| |___|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
1075 arch_irn_handler_t irn_handler;
1076 arch_irn_ops_t irn_ops;
1077 const arch_env_t *arch_env;
1081 #define get_phi_handler_from_handler(h) container_of(h, phi_handler_t, irn_handler)
1082 #define get_phi_handler_from_ops(h) container_of(h, phi_handler_t, irn_ops)
1084 static const void *phi_get_irn_ops(const arch_irn_handler_t *handler, const ir_node *irn)
1086 const phi_handler_t *h = get_phi_handler_from_handler(handler);
1087 return is_Phi(irn) && mode_is_datab(get_irn_mode(irn)) ? &h->irn_ops : NULL;
1091 * Get register class of a Phi.
1094 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)
1096 int n = get_irn_arity(phi);
1100 if(*visited && pset_find_ptr(*visited, phi))
1103 for(i = 0; i < n; ++i) {
1104 op = get_irn_n(phi, i);
1106 return arch_get_register_req(h->arch_env, req, op, BE_OUT_POS(0));
1110 The operands of that Phi were all Phis themselves.
1111 We have to start a DFS for a non-Phi argument now.
1114 *visited = pset_new_ptr(16);
1116 pset_insert_ptr(*visited, phi);
1118 for(i = 0; i < n; ++i) {
1119 op = get_irn_n(phi, i);
1120 if(get_Phi_reg_req_recursive(h, req, op, visited))
1127 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)
1129 phi_handler_t *phi_handler = get_phi_handler_from_ops(self);
1130 pset *visited = NULL;
1132 get_Phi_reg_req_recursive(phi_handler, req, irn, &visited);
1133 /* Set the requirements type to normal, since an operand of the Phi could have had constraints. */
1134 req->type = arch_register_req_type_normal;
1141 static void phi_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg)
1143 phi_handler_t *h = get_phi_handler_from_ops(self);
1144 pmap_insert(h->regs, irn, (void *) reg);
1147 static const arch_register_t *phi_get_irn_reg(const void *self, const ir_node *irn)
1149 phi_handler_t *h = get_phi_handler_from_ops(self);
1150 return pmap_get(h->regs, (void *) irn);
1153 static arch_irn_class_t phi_classify(const void *_self, const ir_node *irn)
1155 return arch_irn_class_normal;
1158 static arch_irn_flags_t phi_get_flags(const void *_self, const ir_node *irn)
1160 return arch_irn_flags_none;
1163 static entity *phi_get_frame_entity(const void *_self, const ir_node *irn)
1168 static void phi_set_frame_offset(const void *_self, ir_node *irn, int bias)
1172 static const arch_irn_ops_if_t phi_irn_ops = {
1173 phi_get_irn_reg_req,
1178 phi_get_frame_entity,
1179 phi_set_frame_offset
1182 static const arch_irn_handler_t phi_irn_handler = {
1186 arch_irn_handler_t *be_phi_handler_new(const arch_env_t *arch_env)
1188 phi_handler_t *h = xmalloc(sizeof(h[0]));
1189 h->irn_handler.get_irn_ops = phi_get_irn_ops;
1190 h->irn_ops.impl = &phi_irn_ops;
1191 h->arch_env = arch_env;
1192 h->regs = pmap_create();
1193 return (arch_irn_handler_t *) h;
1196 void be_phi_handler_free(arch_irn_handler_t *handler)
1198 phi_handler_t *h = (void *) handler;
1199 pmap_destroy(h->regs);
1203 const void *be_phi_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn)
1205 phi_handler_t *phi_handler = get_phi_handler_from_handler(self);
1206 return is_Phi(irn) ? &phi_handler->irn_ops : NULL;
1209 void be_phi_handler_reset(arch_irn_handler_t *handler)
1211 phi_handler_t *h = get_phi_handler_from_handler(handler);
1213 pmap_destroy(h->regs);
1214 h->regs = pmap_create();
1220 | \ | | ___ __| | ___ | _ \ _ _ _ __ ___ _ __ (_)_ __ __ _
1221 | \| |/ _ \ / _` |/ _ \ | | | | | | | '_ ` _ \| '_ \| | '_ \ / _` |
1222 | |\ | (_) | (_| | __/ | |_| | |_| | | | | | | |_) | | | | | (_| |
1223 |_| \_|\___/ \__,_|\___| |____/ \__,_|_| |_| |_| .__/|_|_| |_|\__, |
1228 * Dumps a register requirement to a file.
1230 static void dump_node_req(FILE *f, int idx, be_req_t *req)
1233 int did_something = 0;
1235 const char *prefix = buf;
1237 snprintf(buf, sizeof(buf), "#%d ", idx);
1239 if(req->flags != arch_irn_flags_none) {
1240 fprintf(f, "%sflags: ", prefix);
1242 for(i = arch_irn_flags_none; i <= log2_ceil(arch_irn_flags_last); ++i) {
1243 if(req->flags & (1 << i)) {
1244 fprintf(f, "%s%s", prefix, arch_irn_flag_str(1 << i));
1252 if(req->req.cls != 0) {
1255 arch_register_req_format(tmp, sizeof(tmp), &req->req);
1256 fprintf(f, "%s", tmp);
1265 * Dumps node register requirements to a file.
1267 static void dump_node_reqs(FILE *f, ir_node *irn)
1270 be_node_attr_t *a = get_irn_attr(irn);
1272 fprintf(f, "registers: \n");
1273 for(i = 0; i < a->max_reg_data; ++i) {
1274 be_reg_data_t *rd = &a->reg_data[i];
1276 fprintf(f, "#%d: %s\n", i, rd->reg->name);
1279 fprintf(f, "in requirements\n");
1280 for(i = 0; i < a->max_reg_data; ++i) {
1281 dump_node_req(f, i, &a->reg_data[i].in_req);
1284 fprintf(f, "\nout requirements\n");
1285 for(i = 0; i < a->max_reg_data; ++i) {
1286 dump_node_req(f, i, &a->reg_data[i].req);
1291 * ir_op-Operation: dump a be node to file
1293 static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason)
1295 be_node_attr_t *at = get_irn_attr(irn);
1297 assert(is_be_node(irn));
1300 case dump_node_opcode_txt:
1301 fprintf(f, get_op_name(get_irn_op(irn)));
1303 case dump_node_mode_txt:
1304 fprintf(f, get_mode_name(get_irn_mode(irn)));
1306 case dump_node_nodeattr_txt:
1308 case dump_node_info_txt:
1309 dump_node_reqs(f, irn);
1311 if(be_has_frame_entity(irn)) {
1312 be_frame_attr_t *a = (be_frame_attr_t *) at;
1314 int bits = get_type_size_bits(get_entity_type(a->ent));
1315 ir_fprintf(f, "frame entity: %+F offset 0x%x (%d) size 0x%x %d\n",
1316 a->ent, a->offset, a->offset, bits, bits);
1321 switch(be_get_irn_opcode(irn)) {
1324 be_spill_attr_t *a = (be_spill_attr_t *) at;
1325 ir_fprintf(f, "spill context: %+F\n", a->spill_ctx);
1331 be_stack_attr_t *a = (be_stack_attr_t *) at;
1332 if (a->offset == BE_STACK_FRAME_SIZE)
1333 fprintf(f, "offset: FRAME_SIZE\n");
1335 fprintf(f, "offset: %u\n", a->offset);
1336 fprintf(f, "direction: %s\n", a->dir == be_stack_dir_expand ? "expand" : "shrink");
1341 be_call_attr_t *a = (be_call_attr_t *) at;
1344 fprintf(f, "\ncalling: %s\n", get_entity_name(a->ent));
1356 * Copies the backend specific attributes from old node to new node.
1358 static void copy_attr(const ir_node *old_node, ir_node *new_node)
1360 be_node_attr_t *old_attr = get_irn_attr(old_node);
1361 be_node_attr_t *new_attr = get_irn_attr(new_node);
1364 assert(is_be_node(old_node));
1365 assert(is_be_node(new_node));
1367 memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
1368 new_attr->reg_data = NULL;
1370 if(new_attr->max_reg_data > 0) {
1371 new_attr->reg_data = NEW_ARR_D(be_reg_data_t, get_irg_obstack(get_irn_irg(new_node)), new_attr->max_reg_data);
1372 memcpy(new_attr->reg_data, old_attr->reg_data, new_attr->max_reg_data * sizeof(be_reg_data_t));
1374 for(i = 0; i < old_attr->max_reg_data; ++i) {
1377 r = &new_attr->reg_data[i].req;
1378 r->req.limited_env = r;
1380 r = &new_attr->reg_data[i].in_req;
1381 r->req.limited_env = r;
1386 static const ir_op_ops be_node_op_ops = {