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_Return nodes attribute type. */
95 be_node_attr_t node_attr;
96 int num_ret_vals; /**< number of return values */
99 /** The be_Stack attribute type. */
101 be_node_attr_t node_attr;
102 int offset; /**< The offset by which the stack shall be increased/decreased. */
103 be_stack_dir_t dir; /**< The direction in which the stack shall be modified (expand or shrink). */
106 /** The be_Frame attribute type. */
108 be_node_attr_t node_attr;
113 /** The be_Call attribute type. */
115 be_node_attr_t node_attr;
116 entity *ent; /**< The called entity if this is a static call. */
117 ir_type *call_tp; /**< The call type, copied from the original Call node. */
120 /** The be_Spill attribute type. */
122 be_frame_attr_t frame_attr;
123 ir_node *spill_ctx; /**< The node in whose context this spill was introduced. */
131 ir_op *op_be_CopyKeep;
137 ir_op *op_be_RegParams;
138 ir_op *op_be_StackParam;
139 ir_op *op_be_FrameAddr;
140 ir_op *op_be_FrameLoad;
141 ir_op *op_be_FrameStore;
142 ir_op *op_be_Barrier;
144 static int beo_base = -1;
146 static const ir_op_ops be_node_op_ops;
148 #define N irop_flag_none
149 #define L irop_flag_labeled
150 #define C irop_flag_commutative
151 #define X irop_flag_cfopcode
152 #define I irop_flag_ip_cfopcode
153 #define F irop_flag_fragile
154 #define Y irop_flag_forking
155 #define H irop_flag_highlevel
156 #define c irop_flag_constlike
157 #define K irop_flag_keep
158 #define M irop_flag_machine
161 void be_node_init(void) {
162 static int inited = 0;
169 /* Acquire all needed opcodes. */
170 beo_base = get_next_ir_opcodes(beo_Last - 1);
172 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);
173 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);
174 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);
175 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);
176 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);
177 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);
178 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);
179 op_be_Return = new_ir_op(beo_base + beo_Return, "be_Return", op_pin_state_pinned, X, oparity_variable, 0, sizeof(be_return_attr_t), &be_node_op_ops);
180 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);
181 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);
182 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);
183 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);
184 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);
185 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);
186 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);
187 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);
188 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);
190 set_op_tag(op_be_Spill, &be_node_tag);
191 set_op_tag(op_be_Reload, &be_node_tag);
192 set_op_tag(op_be_Perm, &be_node_tag);
193 set_op_tag(op_be_Copy, &be_node_tag);
194 set_op_tag(op_be_Keep, &be_node_tag);
195 set_op_tag(op_be_CopyKeep, &be_node_tag);
196 set_op_tag(op_be_Call, &be_node_tag);
197 set_op_tag(op_be_Return, &be_node_tag);
198 set_op_tag(op_be_AddSP, &be_node_tag);
199 set_op_tag(op_be_SetSP, &be_node_tag);
200 set_op_tag(op_be_IncSP, &be_node_tag);
201 set_op_tag(op_be_RegParams, &be_node_tag);
202 set_op_tag(op_be_StackParam, &be_node_tag);
203 set_op_tag(op_be_FrameLoad, &be_node_tag);
204 set_op_tag(op_be_FrameStore, &be_node_tag);
205 set_op_tag(op_be_FrameAddr, &be_node_tag);
206 set_op_tag(op_be_Barrier, &be_node_tag);
210 * Initializes the generic attribute of all be nodes and return ir.
212 static void *init_node_attr(ir_node* irn, int max_reg_data)
214 ir_graph *irg = get_irn_irg(irn);
215 be_node_attr_t *a = get_irn_attr(irn);
217 memset(a, 0, sizeof(get_op_attr_size(get_irn_op(irn))));
218 a->max_reg_data = max_reg_data;
221 if(max_reg_data > 0) {
224 a->reg_data = NEW_ARR_D(be_reg_data_t, get_irg_obstack(irg), max_reg_data);
225 memset(a->reg_data, 0, max_reg_data * sizeof(a->reg_data[0]));
226 for(i = 0; i < max_reg_data; ++i) {
227 a->reg_data[i].req.req.cls = NULL;
228 a->reg_data[i].req.req.type = arch_register_req_type_none;
235 int is_be_node(const ir_node *irn)
237 return get_op_tag(get_irn_op(irn)) == &be_node_tag;
240 be_opcode_t be_get_irn_opcode(const ir_node *irn)
242 return is_be_node(irn) ? get_irn_opcode(irn) - beo_base : beo_NoBeOp;
245 static int redir_proj(const ir_node **node, int pos)
247 const ir_node *n = *node;
252 assert(pos == -1 && "Illegal pos for a Proj");
253 *node = irn = get_Proj_pred(n);
255 assert(get_irn_mode(irn) == mode_T);
256 *node = get_Proj_pred(irn);
258 return get_Proj_proj(n);
265 be_node_set_irn_reg(const void *_self, ir_node *irn, const arch_register_t *reg)
270 out_pos = redir_proj((const ir_node **) &irn, -1);
271 a = get_irn_attr(irn);
273 assert(is_be_node(irn));
274 assert(out_pos < a->max_reg_data && "position too high");
275 a->reg_data[out_pos].reg = reg;
279 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)
287 res = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
288 a = init_node_attr(res, 2);
289 a->frame_attr.ent = NULL;
290 a->frame_attr.offset = 0;
293 be_node_set_reg_class(res, 0, cls_frame);
294 be_node_set_reg_class(res, 1, cls);
298 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)
305 res = new_ir_node(NULL, irg, bl, op_be_Reload, mode, 2, in);
306 init_node_attr(res, 2);
307 be_node_set_reg_class(res, 0, cls_frame);
308 be_node_set_reg_class(res, -1, cls);
312 ir_node *be_get_Reload_mem(const ir_node *irn)
314 assert(be_is_Reload(irn));
315 return get_irn_n(irn, be_pos_Reload_mem);
318 ir_node *be_get_Reload_frame(const ir_node *irn)
320 assert(be_is_Reload(irn));
321 return get_irn_n(irn, be_pos_Reload_frame);
324 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
327 ir_node *irn = new_ir_node(NULL, irg, bl, op_be_Perm, mode_T, n, in);
328 init_node_attr(irn, n);
329 for(i = 0; i < n; ++i) {
330 be_node_set_reg_class(irn, i, cls);
331 be_node_set_reg_class(irn, OUT_POS(i), cls);
337 ir_node *be_new_Copy(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *op)
343 res = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
344 init_node_attr(res, 1);
345 be_node_set_reg_class(res, 0, cls);
346 be_node_set_reg_class(res, OUT_POS(0), cls);
350 ir_node *be_get_Copy_op(const ir_node *cpy) {
351 return get_irn_n(cpy, be_pos_Copy_op);
354 void be_set_Copy_op(ir_node *cpy, ir_node *op) {
355 set_irn_n(cpy, be_pos_Copy_op, op);
358 ir_node *be_new_Keep(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
363 irn = new_ir_node(NULL, irg, bl, op_be_Keep, mode_ANY, n, in);
364 init_node_attr(irn, n);
365 for(i = 0; i < n; ++i) {
366 be_node_set_reg_class(irn, i, cls);
372 ir_node *be_new_Call(dbg_info *dbg, ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *sp, ir_node *ptr,
373 int n_outs, int n, ir_node *in[], ir_type *call_tp)
376 int real_n = be_pos_Call_first_arg + n;
380 NEW_ARR_A(ir_node *, real_in, real_n);
381 real_in[be_pos_Call_mem] = mem;
382 real_in[be_pos_Call_sp] = sp;
383 real_in[be_pos_Call_ptr] = ptr;
384 memcpy(&real_in[be_pos_Call_first_arg], in, n * sizeof(in[0]));
386 irn = new_ir_node(NULL, irg, bl, op_be_Call, mode_T, real_n, real_in);
387 a = init_node_attr(irn, (n_outs > real_n ? n_outs : real_n));
389 a->call_tp = call_tp;
393 /* Gets the call entity or NULL if this is no static call. */
394 entity *be_Call_get_entity(const ir_node *call) {
395 be_call_attr_t *a = get_irn_attr(call);
396 assert(be_is_Call(call));
400 /* Sets the call entity. */
401 void be_Call_set_entity(ir_node *call, entity *ent) {
402 be_call_attr_t *a = get_irn_attr(call);
403 assert(be_is_Call(call));
407 /* Gets the call type. */
408 ir_type *be_Call_get_type(ir_node *call) {
409 be_call_attr_t *a = get_irn_attr(call);
410 assert(be_is_Call(call));
414 /* Sets the call type. */
415 void be_Call_set_type(ir_node *call, ir_type *call_tp) {
416 be_call_attr_t *a = get_irn_attr(call);
417 assert(be_is_Call(call));
418 a->call_tp = call_tp;
421 /* Construct a new be_Return. */
422 ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *bl, int n_res, int n, ir_node *in[])
425 ir_node *irn = new_ir_node(dbg, irg, bl, op_be_Return, mode_X, n, in);
426 init_node_attr(irn, n);
427 a = get_irn_attr(irn);
428 a->num_ret_vals = n_res;
433 /* Returns the number of real returns values */
434 int be_Return_get_n_rets(ir_node *ret)
436 be_return_attr_t *a = get_irn_attr(ret);
437 return a->num_ret_vals;
440 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)
448 irn = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode, 2, in);
449 a = init_node_attr(irn, 1);
453 be_node_set_flags(irn, -1, arch_irn_flags_ignore | arch_irn_flags_modify_sp);
455 /* Set output constraint to stack register. */
456 be_node_set_reg_class(irn, 0, sp->reg_class);
457 be_set_constr_single_reg(irn, -1, sp);
458 be_node_set_irn_reg(NULL, irn, sp);
463 ir_node *be_new_AddSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, ir_node *sz)
467 ir_node *in[be_pos_AddSP_last];
469 in[be_pos_AddSP_old_sp] = old_sp;
470 in[be_pos_AddSP_size] = sz;
472 irn = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T, be_pos_AddSP_last, in);
473 a = init_node_attr(irn, be_pos_AddSP_last);
475 be_node_set_flags(irn, OUT_POS(0), arch_irn_flags_ignore | arch_irn_flags_modify_sp);
477 /* Set output constraint to stack register. */
478 be_set_constr_single_reg(irn, OUT_POS(0), sp);
483 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)
492 irn = new_ir_node(NULL, irg, bl, op_be_SetSP, get_irn_mode(old_sp), 3, in);
493 a = init_node_attr(irn, 3);
495 be_node_set_flags(irn, OUT_POS(0), arch_irn_flags_ignore | arch_irn_flags_modify_sp);
497 /* Set output constraint to stack register. */
498 be_set_constr_single_reg(irn, OUT_POS(0), sp);
499 be_node_set_reg_class(irn, 1, sp->reg_class);
500 be_node_set_reg_class(irn, 2, sp->reg_class);
501 be_node_set_irn_reg(NULL, irn, sp);
506 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)
512 in[0] = frame_pointer;
513 irn = new_ir_node(NULL, irg, bl, op_be_StackParam, mode, 1, in);
514 a = init_node_attr(irn, 1);
517 be_node_set_reg_class(irn, 0, cls_frame);
518 be_node_set_reg_class(irn, OUT_POS(0), cls);
522 ir_node *be_new_RegParams(ir_graph *irg, ir_node *bl, int n_outs)
527 irn = new_ir_node(NULL, irg, bl, op_be_RegParams, mode_T, 0, in);
528 init_node_attr(irn, n_outs);
532 ir_node *be_new_FrameLoad(const arch_register_class_t *cls_frame, const arch_register_class_t *cls_data,
533 ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *frame, entity *ent)
541 irn = new_ir_node(NULL, irg, bl, op_be_FrameLoad, mode_T, 2, in);
542 a = init_node_attr(irn, 3);
545 be_node_set_reg_class(irn, 1, cls_frame);
546 be_node_set_reg_class(irn, OUT_POS(pn_Load_res), cls_data);
550 ir_node *be_new_FrameStore(const arch_register_class_t *cls_frame, const arch_register_class_t *cls_data,
551 ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *frame, ir_node *data, entity *ent)
560 irn = new_ir_node(NULL, irg, bl, op_be_FrameStore, mode_T, 3, in);
561 a = init_node_attr(irn, 3);
564 be_node_set_reg_class(irn, 1, cls_frame);
565 be_node_set_reg_class(irn, 2, cls_data);
569 ir_node *be_new_FrameAddr(const arch_register_class_t *cls_frame, ir_graph *irg, ir_node *bl, ir_node *frame, entity *ent)
576 irn = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
577 a = init_node_attr(irn, 1);
580 be_node_set_reg_class(irn, 0, cls_frame);
581 be_node_set_reg_class(irn, OUT_POS(0), cls_frame);
585 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)
588 ir_node **in = (ir_node **) alloca((n + 1) * sizeof(in[0]));
591 memcpy(&in[1], in_keep, n * sizeof(in[0]));
592 irn = new_ir_node(NULL, irg, bl, op_be_CopyKeep, mode, n + 1, in);
593 init_node_attr(irn, n + 1);
594 be_node_set_reg_class(irn, OUT_POS(0), cls);
595 be_node_set_reg_class(irn, 0, cls);
600 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)
605 return be_new_CopyKeep(cls, irg, bl, src, 1, in, mode);
608 ir_node *be_get_CopyKeep_op(const ir_node *cpy) {
609 return get_irn_n(cpy, be_pos_CopyKeep_op);
612 void be_set_CopyKeep_op(ir_node *cpy, ir_node *op) {
613 set_irn_n(cpy, be_pos_CopyKeep_op, op);
616 ir_node *be_new_Barrier(ir_graph *irg, ir_node *bl, int n, ir_node *in[])
620 irn = new_ir_node(NULL, irg, bl, op_be_Barrier, mode_T, n, in);
621 init_node_attr(irn, n);
625 int be_is_Spill (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Spill ; }
626 int be_is_Reload (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Reload ; }
627 int be_is_Copy (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Copy ; }
628 int be_is_CopyKeep (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_CopyKeep ; }
629 int be_is_Perm (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Perm ; }
630 int be_is_Keep (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Keep ; }
631 int be_is_Call (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Call ; }
632 int be_is_Return (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Return ; }
633 int be_is_IncSP (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_IncSP ; }
634 int be_is_SetSP (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_SetSP ; }
635 int be_is_AddSP (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_AddSP ; }
636 int be_is_RegParams (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_RegParams ; }
637 int be_is_StackParam (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_StackParam ; }
638 int be_is_FrameAddr (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameAddr ; }
639 int be_is_FrameLoad (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameLoad ; }
640 int be_is_FrameStore (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameStore ; }
641 int be_is_Barrier (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Barrier ; }
643 int be_has_frame_entity(const ir_node *irn)
645 switch(be_get_irn_opcode(irn)) {
658 entity *be_get_frame_entity(const ir_node *irn)
660 if(be_has_frame_entity(irn)) {
661 be_frame_attr_t *a = get_irn_attr(irn);
667 static void be_limited(void *data, bitset_t *bs)
669 be_req_t *req = data;
672 case be_req_kind_negate_old_limited:
673 case be_req_kind_old_limited:
674 req->x.old_limited.old_limited(req->x.old_limited.old_limited_env, bs);
675 if(req->kind == be_req_kind_negate_old_limited)
678 case be_req_kind_single_reg:
679 bitset_clear_all(bs);
680 bitset_set(bs, req->x.single_reg->index);
685 static INLINE be_req_t *get_req(ir_node *irn, int pos)
687 int idx = pos < 0 ? -(pos + 1) : pos;
688 be_node_attr_t *a = get_irn_attr(irn);
689 be_reg_data_t *rd = &a->reg_data[idx];
690 be_req_t *r = pos < 0 ? &rd->req : &rd->in_req;
692 assert(is_be_node(irn));
693 assert(!(pos >= 0) || pos < get_irn_arity(irn));
694 assert(!(pos < 0) || -(pos + 1) <= a->max_reg_data);
699 void be_set_constr_single_reg(ir_node *irn, int pos, const arch_register_t *reg)
701 be_req_t *r = get_req(irn, pos);
703 r->kind = be_req_kind_single_reg;
704 r->x.single_reg = reg;
705 r->req.limited = be_limited;
706 r->req.limited_env = r;
707 r->req.type = arch_register_req_type_limited;
708 r->req.cls = reg->reg_class;
711 void be_set_constr_limited(ir_node *irn, int pos, const arch_register_req_t *req)
713 be_req_t *r = get_req(irn, pos);
715 assert(arch_register_req_is(req, limited));
717 r->kind = be_req_kind_old_limited;
718 r->req.limited = be_limited;
719 r->req.limited_env = r;
720 r->req.type = arch_register_req_type_limited;
721 r->req.cls = req->cls;
723 r->x.old_limited.old_limited = req->limited;
724 r->x.old_limited.old_limited_env = req->limited_env;
727 void be_node_set_flags(ir_node *irn, int pos, arch_irn_flags_t flags)
729 be_req_t *r = get_req(irn, pos);
733 void be_node_set_reg_class(ir_node *irn, int pos, const arch_register_class_t *cls)
735 be_req_t *r = get_req(irn, pos);
737 if(r->req.type == arch_register_req_type_none)
738 r->req.type = arch_register_req_type_normal;
741 void be_node_set_req_type(ir_node *irn, int pos, arch_register_req_type_t type)
743 be_req_t *r = get_req(irn, pos);
747 ir_node *be_get_IncSP_pred(ir_node *irn) {
748 assert(be_is_IncSP(irn));
749 return get_irn_n(irn, 0);
752 void be_set_IncSP_pred(ir_node *incsp, ir_node *pred) {
753 assert(be_is_IncSP(incsp));
754 set_irn_n(incsp, 0, pred);
757 ir_node *be_get_IncSP_mem(ir_node *irn) {
758 assert(be_is_IncSP(irn));
759 return get_irn_n(irn, 1);
762 void be_set_IncSP_offset(ir_node *irn, unsigned offset)
764 be_stack_attr_t *a = get_irn_attr(irn);
765 assert(be_is_IncSP(irn));
769 unsigned be_get_IncSP_offset(const ir_node *irn)
771 be_stack_attr_t *a = get_irn_attr(irn);
772 assert(be_is_IncSP(irn));
776 void be_set_IncSP_direction(ir_node *irn, be_stack_dir_t dir)
778 be_stack_attr_t *a = get_irn_attr(irn);
779 assert(be_is_IncSP(irn));
783 be_stack_dir_t be_get_IncSP_direction(const ir_node *irn)
785 be_stack_attr_t *a = get_irn_attr(irn);
786 assert(be_is_IncSP(irn));
790 void be_set_Spill_entity(ir_node *irn, entity *ent)
792 be_spill_attr_t *a = get_irn_attr(irn);
793 assert(be_is_Spill(irn));
794 a->frame_attr.ent = ent;
797 static ir_node *find_a_spill_walker(ir_node *irn, unsigned visited_nr)
799 unsigned nr = get_irn_visited(irn);
801 set_irn_visited(irn, visited_nr);
805 if(nr < visited_nr) {
806 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
807 ir_node *n = find_a_spill_walker(get_irn_n(irn, i), visited_nr);
814 else if(be_get_irn_opcode(irn) == beo_Spill)
820 ir_node *be_get_Spill_context(const ir_node *irn) {
821 const be_spill_attr_t *a = get_irn_attr(irn);
822 assert(be_is_Spill(irn));
827 * Finds a spill for a reload.
828 * If the reload is directly using the spill, this is simple,
829 * else we perform DFS from the reload (over all PhiMs) and return
830 * the first spill node we find.
832 static INLINE ir_node *find_a_spill(const ir_node *irn)
834 ir_graph *irg = get_irn_irg(irn);
835 unsigned visited_nr = get_irg_visited(irg) + 1;
837 assert(be_is_Reload(irn));
838 set_irg_visited(irg, visited_nr);
839 return find_a_spill_walker(be_get_Reload_mem(irn), visited_nr);
842 entity *be_get_spill_entity(const ir_node *irn)
844 switch(be_get_irn_opcode(irn)) {
847 ir_node *spill = find_a_spill(irn);
848 return be_get_spill_entity(spill);
852 be_spill_attr_t *a = get_irn_attr(irn);
853 return a->frame_attr.ent;
856 assert(0 && "Must give spill/reload node");
863 static void link_reload_walker(ir_node *irn, void *data)
865 ir_node **root = (ir_node **) data;
866 if(be_is_Reload(irn)) {
867 set_irn_link(irn, *root);
872 void be_copy_entities_to_reloads(ir_graph *irg)
875 irg_walk_graph(irg, link_reload_walker, NULL, (void *) &irn);
878 be_frame_attr_t *a = get_irn_attr(irn);
879 entity *ent = be_get_spill_entity(irn);
881 irn = get_irn_link(irn);
885 ir_node *be_spill(const arch_env_t *arch_env, ir_node *irn, ir_node *ctx)
887 ir_node *bl = get_nodes_block(irn);
888 ir_graph *irg = get_irn_irg(bl);
889 ir_node *frame = get_irg_frame(irg);
890 ir_node *insert = bl;
893 const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, irn, -1);
894 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
896 spill = be_new_Spill(cls, cls_frame, irg, bl, frame, irn, ctx);
901 * search the right insertion point. a spill of a phi cannot be put
902 * directly after the phi, if there are some phis behind the one which
903 * is spilled. Also, a spill of a Proj must be after all Projs of the
906 * Here's one special case:
907 * If the spill is in the start block, the spill must be after the frame
908 * pointer is set up. This is done by setting insert to the end of the block
909 * which is its default initialization (see above).
912 insert = sched_next(irn);
913 if(insert != bl && bl == get_irg_start_block(irg) && sched_get_time_step(frame) >= sched_get_time_step(insert))
914 insert = sched_next(frame);
916 while((is_Phi(insert) || is_Proj(insert)) && !sched_is_end(insert))
917 insert = sched_next(insert);
919 sched_add_before(insert, spill);
924 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)
928 ir_node *bl = is_Block(insert) ? insert : get_nodes_block(insert);
929 ir_graph *irg = get_irn_irg(bl);
930 ir_node *frame = get_irg_frame(irg);
931 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
933 assert(be_is_Spill(spill) || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
935 reload = be_new_Reload(cls, cls_frame, irg, bl, frame, spill, mode);
937 if(is_Block(insert)) {
938 insert = sched_skip(insert, 0, sched_skip_cf_predicator, (void *) arch_env);
939 sched_add_after(insert, reload);
943 sched_add_before(insert, reload);
950 | _ \ ___ __ _ | _ \ ___ __ _ ___
951 | |_) / _ \/ _` | | |_) / _ \/ _` / __|
952 | _ < __/ (_| | | _ < __/ (_| \__ \
953 |_| \_\___|\__, | |_| \_\___|\__, |___/
959 static void *put_out_reg_req(arch_register_req_t *req, const ir_node *irn, int out_pos)
961 const be_node_attr_t *a = get_irn_attr(irn);
963 if(out_pos < a->max_reg_data) {
964 memcpy(req, &a->reg_data[out_pos].req, sizeof(req[0]));
966 if(be_is_Copy(irn)) {
967 req->type |= arch_register_req_type_should_be_same;
968 req->other_same = be_get_Copy_op(irn);
972 req->type = arch_register_req_type_none;
979 static void *put_in_reg_req(arch_register_req_t *req, const ir_node *irn, int pos)
981 const be_node_attr_t *a = get_irn_attr(irn);
983 if(pos < get_irn_arity(irn) && pos < a->max_reg_data)
984 memcpy(req, &a->reg_data[pos].in_req, sizeof(req[0]));
986 req->type = arch_register_req_type_none;
993 static const arch_register_req_t *
994 be_node_get_irn_reg_req(const void *self, arch_register_req_t *req, const ir_node *irn, int pos)
999 if(get_irn_mode(irn) == mode_T)
1002 out_pos = redir_proj((const ir_node **) &irn, pos);
1003 assert(is_be_node(irn));
1004 return put_out_reg_req(req, irn, out_pos);
1008 return is_be_node(irn) ? put_in_reg_req(req, irn, pos) : NULL;
1014 const arch_register_t *
1015 be_node_get_irn_reg(const void *_self, const ir_node *irn)
1020 out_pos = redir_proj((const ir_node **) &irn, -1);
1021 a = get_irn_attr(irn);
1023 assert(is_be_node(irn));
1024 assert(out_pos < a->max_reg_data && "position too high");
1026 return a->reg_data[out_pos].reg;
1029 static arch_irn_class_t be_node_classify(const void *_self, const ir_node *irn)
1031 redir_proj((const ir_node **) &irn, -1);
1033 switch(be_get_irn_opcode(irn)) {
1034 #define XXX(a,b) case beo_ ## a: return arch_irn_class_ ## b
1036 XXX(Reload, reload);
1039 XXX(Return, branch);
1040 XXX(StackParam, stackparam);
1049 static arch_irn_flags_t be_node_get_flags(const void *_self, const ir_node *irn)
1054 out_pos = redir_proj((const ir_node **) &irn, -1);
1055 a = get_irn_attr(irn);
1057 assert(is_be_node(irn));
1058 assert(out_pos < a->max_reg_data && "position too high");
1060 return a->reg_data[out_pos].req.flags;
1063 static entity *be_node_get_frame_entity(const void *self, const ir_node *irn)
1065 return be_get_frame_entity(irn);
1068 static void be_node_set_frame_offset(const void *self, ir_node *irn, int offset)
1070 if(be_has_frame_entity(irn)) {
1071 be_frame_attr_t *a = get_irn_attr(irn);
1077 ___ ____ _ _ _ _ _ _
1078 |_ _| _ \| \ | | | | | | __ _ _ __ __| | | ___ _ __
1079 | || |_) | \| | | |_| |/ _` | '_ \ / _` | |/ _ \ '__|
1080 | || _ <| |\ | | _ | (_| | | | | (_| | | __/ |
1081 |___|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
1085 static const arch_irn_ops_if_t be_node_irn_ops_if = {
1086 be_node_get_irn_reg_req,
1087 be_node_set_irn_reg,
1088 be_node_get_irn_reg,
1091 be_node_get_frame_entity,
1092 be_node_set_frame_offset
1095 static const arch_irn_ops_t be_node_irn_ops = {
1099 const void *be_node_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn)
1101 redir_proj((const ir_node **) &irn, -1);
1102 return is_be_node(irn) ? &be_node_irn_ops : NULL;
1105 const arch_irn_handler_t be_node_irn_handler = {
1110 ____ _ _ ___ ____ _ _ _ _ _ _
1111 | _ \| |__ (_) |_ _| _ \| \ | | | | | | __ _ _ __ __| | | ___ _ __
1112 | |_) | '_ \| | | || |_) | \| | | |_| |/ _` | '_ \ / _` | |/ _ \ '__|
1113 | __/| | | | | | || _ <| |\ | | _ | (_| | | | | (_| | | __/ |
1114 |_| |_| |_|_| |___|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
1119 arch_irn_handler_t irn_handler;
1120 arch_irn_ops_t irn_ops;
1121 const arch_env_t *arch_env;
1125 #define get_phi_handler_from_handler(h) container_of(h, phi_handler_t, irn_handler)
1126 #define get_phi_handler_from_ops(h) container_of(h, phi_handler_t, irn_ops)
1128 static const void *phi_get_irn_ops(const arch_irn_handler_t *handler, const ir_node *irn)
1130 const phi_handler_t *h = get_phi_handler_from_handler(handler);
1131 return is_Phi(irn) && mode_is_datab(get_irn_mode(irn)) ? &h->irn_ops : NULL;
1135 * Get register class of a Phi.
1138 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)
1140 int n = get_irn_arity(phi);
1144 if(*visited && pset_find_ptr(*visited, phi))
1147 for(i = 0; i < n; ++i) {
1148 op = get_irn_n(phi, i);
1150 return arch_get_register_req(h->arch_env, req, op, BE_OUT_POS(0));
1154 The operands of that Phi were all Phis themselves.
1155 We have to start a DFS for a non-Phi argument now.
1158 *visited = pset_new_ptr(16);
1160 pset_insert_ptr(*visited, phi);
1162 for(i = 0; i < n; ++i) {
1163 op = get_irn_n(phi, i);
1164 if(get_Phi_reg_req_recursive(h, req, op, visited))
1171 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)
1173 phi_handler_t *phi_handler = get_phi_handler_from_ops(self);
1174 pset *visited = NULL;
1176 get_Phi_reg_req_recursive(phi_handler, req, irn, &visited);
1177 /* Set the requirements type to normal, since an operand of the Phi could have had constraints. */
1178 req->type = arch_register_req_type_normal;
1185 static void phi_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg)
1187 phi_handler_t *h = get_phi_handler_from_ops(self);
1188 pmap_insert(h->regs, irn, (void *) reg);
1191 static const arch_register_t *phi_get_irn_reg(const void *self, const ir_node *irn)
1193 phi_handler_t *h = get_phi_handler_from_ops(self);
1194 return pmap_get(h->regs, (void *) irn);
1197 static arch_irn_class_t phi_classify(const void *_self, const ir_node *irn)
1199 return arch_irn_class_normal;
1202 static arch_irn_flags_t phi_get_flags(const void *_self, const ir_node *irn)
1204 return arch_irn_flags_none;
1207 static entity *phi_get_frame_entity(const void *_self, const ir_node *irn)
1212 static void phi_set_frame_offset(const void *_self, ir_node *irn, int bias)
1216 static const arch_irn_ops_if_t phi_irn_ops = {
1217 phi_get_irn_reg_req,
1222 phi_get_frame_entity,
1223 phi_set_frame_offset
1226 static const arch_irn_handler_t phi_irn_handler = {
1230 arch_irn_handler_t *be_phi_handler_new(const arch_env_t *arch_env)
1232 phi_handler_t *h = xmalloc(sizeof(h[0]));
1233 h->irn_handler.get_irn_ops = phi_get_irn_ops;
1234 h->irn_ops.impl = &phi_irn_ops;
1235 h->arch_env = arch_env;
1236 h->regs = pmap_create();
1237 return (arch_irn_handler_t *) h;
1240 void be_phi_handler_free(arch_irn_handler_t *handler)
1242 phi_handler_t *h = (void *) handler;
1243 pmap_destroy(h->regs);
1247 const void *be_phi_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn)
1249 phi_handler_t *phi_handler = get_phi_handler_from_handler(self);
1250 return is_Phi(irn) ? &phi_handler->irn_ops : NULL;
1253 void be_phi_handler_reset(arch_irn_handler_t *handler)
1255 phi_handler_t *h = get_phi_handler_from_handler(handler);
1257 pmap_destroy(h->regs);
1258 h->regs = pmap_create();
1264 | \ | | ___ __| | ___ | _ \ _ _ _ __ ___ _ __ (_)_ __ __ _
1265 | \| |/ _ \ / _` |/ _ \ | | | | | | | '_ ` _ \| '_ \| | '_ \ / _` |
1266 | |\ | (_) | (_| | __/ | |_| | |_| | | | | | | |_) | | | | | (_| |
1267 |_| \_|\___/ \__,_|\___| |____/ \__,_|_| |_| |_| .__/|_|_| |_|\__, |
1272 * Dumps a register requirement to a file.
1274 static void dump_node_req(FILE *f, int idx, be_req_t *req)
1277 int did_something = 0;
1279 const char *prefix = buf;
1281 snprintf(buf, sizeof(buf), "#%d ", idx);
1283 if(req->flags != arch_irn_flags_none) {
1284 fprintf(f, "%sflags: ", prefix);
1286 for(i = arch_irn_flags_none; i <= log2_ceil(arch_irn_flags_last); ++i) {
1287 if(req->flags & (1 << i)) {
1288 fprintf(f, "%s%s", prefix, arch_irn_flag_str(1 << i));
1296 if(req->req.cls != 0) {
1299 arch_register_req_format(tmp, sizeof(tmp), &req->req);
1300 fprintf(f, "%s", tmp);
1309 * Dumps node register requirements to a file.
1311 static void dump_node_reqs(FILE *f, ir_node *irn)
1314 be_node_attr_t *a = get_irn_attr(irn);
1316 fprintf(f, "registers: \n");
1317 for(i = 0; i < a->max_reg_data; ++i) {
1318 be_reg_data_t *rd = &a->reg_data[i];
1320 fprintf(f, "#%d: %s\n", i, rd->reg->name);
1323 fprintf(f, "in requirements\n");
1324 for(i = 0; i < a->max_reg_data; ++i) {
1325 dump_node_req(f, i, &a->reg_data[i].in_req);
1328 fprintf(f, "\nout requirements\n");
1329 for(i = 0; i < a->max_reg_data; ++i) {
1330 dump_node_req(f, i, &a->reg_data[i].req);
1335 * ir_op-Operation: dump a be node to file
1337 static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason)
1339 be_node_attr_t *at = get_irn_attr(irn);
1341 assert(is_be_node(irn));
1344 case dump_node_opcode_txt:
1345 fprintf(f, get_op_name(get_irn_op(irn)));
1347 case dump_node_mode_txt:
1348 fprintf(f, get_mode_name(get_irn_mode(irn)));
1350 case dump_node_nodeattr_txt:
1352 case dump_node_info_txt:
1353 dump_node_reqs(f, irn);
1355 if(be_has_frame_entity(irn)) {
1356 be_frame_attr_t *a = (be_frame_attr_t *) at;
1358 int bits = get_type_size_bits(get_entity_type(a->ent));
1359 ir_fprintf(f, "frame entity: %+F, offset 0x%x (%d), size 0x%x (%d) bits\n",
1360 a->ent, a->offset, a->offset, bits, bits);
1365 switch(be_get_irn_opcode(irn)) {
1368 be_spill_attr_t *a = (be_spill_attr_t *) at;
1369 ir_fprintf(f, "spill context: %+F\n", a->spill_ctx);
1375 be_stack_attr_t *a = (be_stack_attr_t *) at;
1376 if (a->offset == BE_STACK_FRAME_SIZE)
1377 fprintf(f, "offset: FRAME_SIZE\n");
1379 fprintf(f, "offset: %u\n", a->offset);
1380 fprintf(f, "direction: %s\n", a->dir == be_stack_dir_expand ? "expand" : "shrink");
1385 be_call_attr_t *a = (be_call_attr_t *) at;
1388 fprintf(f, "\ncalling: %s\n", get_entity_name(a->ent));
1400 * Copies the backend specific attributes from old node to new node.
1402 static void copy_attr(const ir_node *old_node, ir_node *new_node)
1404 be_node_attr_t *old_attr = get_irn_attr(old_node);
1405 be_node_attr_t *new_attr = get_irn_attr(new_node);
1408 assert(is_be_node(old_node));
1409 assert(is_be_node(new_node));
1411 memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
1412 new_attr->reg_data = NULL;
1414 if(new_attr->max_reg_data > 0) {
1415 new_attr->reg_data = NEW_ARR_D(be_reg_data_t, get_irg_obstack(get_irn_irg(new_node)), new_attr->max_reg_data);
1416 memcpy(new_attr->reg_data, old_attr->reg_data, new_attr->max_reg_data * sizeof(be_reg_data_t));
1418 for(i = 0; i < old_attr->max_reg_data; ++i) {
1421 r = &new_attr->reg_data[i].req;
1422 r->req.limited_env = r;
1424 r = &new_attr->reg_data[i].in_req;
1425 r->req.limited_env = r;
1430 static const ir_op_ops be_node_op_ops = {