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"
39 #include "besched_t.h"
44 #define OUT_POS(x) (-((x) + 1))
46 /* Sometimes we want to put const nodes into get_irn_generic_attr ... */
47 #define get_irn_attr(irn) get_irn_generic_attr((ir_node *) (irn))
49 static unsigned be_node_tag = FOURCC('B', 'E', 'N', 'O');
52 be_req_kind_old_limited,
53 be_req_kind_negate_old_limited,
54 be_req_kind_single_reg
58 arch_register_req_t req;
60 arch_irn_flags_t flags;
63 void (*old_limited)(void *ptr, bitset_t *bs);
64 void *old_limited_env;
67 const arch_register_t *single_reg;
72 const arch_register_t *reg;
77 /** The generic be nodes attribute type. */
80 be_reg_data_t *reg_data;
83 /** The be_Return nodes attribute type. */
85 be_node_attr_t node_attr;
86 int num_ret_vals; /**< number of return values */
89 /** The be_Stack attribute type. */
91 be_node_attr_t node_attr;
92 int offset; /**< The offset by which the stack shall be expanded/shrinked. */
95 /** The be_Frame attribute type. */
97 be_node_attr_t node_attr;
102 /** The be_Call attribute type. */
104 be_node_attr_t node_attr;
105 entity *ent; /**< The called entity if this is a static call. */
106 ir_type *call_tp; /**< The call type, copied from the original Call node. */
110 be_node_attr_t node_attr;
111 entity **in_entities;
112 entity **out_entities;
118 ir_op *op_be_MemPerm;
121 ir_op *op_be_CopyKeep;
127 ir_op *op_be_RegParams;
128 ir_op *op_be_StackParam;
129 ir_op *op_be_FrameAddr;
130 ir_op *op_be_FrameLoad;
131 ir_op *op_be_FrameStore;
132 ir_op *op_be_Barrier;
134 static int beo_base = -1;
136 static const ir_op_ops be_node_op_ops;
138 #define N irop_flag_none
139 #define L irop_flag_labeled
140 #define C irop_flag_commutative
141 #define X irop_flag_cfopcode
142 #define I irop_flag_ip_cfopcode
143 #define F irop_flag_fragile
144 #define Y irop_flag_forking
145 #define H irop_flag_highlevel
146 #define c irop_flag_constlike
147 #define K irop_flag_keep
148 #define M irop_flag_machine
152 * Compare two node attributes.
154 * @return zero if both attributes are identically
156 static int cmp_node_attr(be_node_attr_t *a, be_node_attr_t *b) {
157 if (a->max_reg_data == b->max_reg_data) {
160 for (i = 0; i < a->max_reg_data; ++i) {
161 if (a->reg_data[i].reg != b->reg_data[i].reg ||
162 memcmp(&a->reg_data[i].in_req, &b->reg_data[i].in_req, sizeof(b->reg_data[i].in_req)) ||
163 memcmp(&a->reg_data[i].req, &b->reg_data[i].req, sizeof(a->reg_data[i].req)))
172 * Compare the attributes of two FrameAddr nodes.
174 * @return zero if both attributes are identically
176 static int FrameAddr_cmp_attr(ir_node *a, ir_node *b) {
177 be_frame_attr_t *a_attr = get_irn_attr(a);
178 be_frame_attr_t *b_attr = get_irn_attr(b);
180 if (a_attr->ent == b_attr->ent && a_attr->offset == b_attr->offset)
181 return cmp_node_attr(&a_attr->node_attr, &b_attr->node_attr);
185 void be_node_init(void) {
186 static int inited = 0;
193 /* Acquire all needed opcodes. */
194 beo_base = get_next_ir_opcodes(beo_Last - 1);
196 op_be_Spill = new_ir_op(beo_base + beo_Spill, "be_Spill", op_pin_state_mem_pinned, N, oparity_unary, 0, sizeof(be_frame_attr_t), &be_node_op_ops);
197 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);
198 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);
199 op_be_MemPerm = new_ir_op(beo_base + beo_MemPerm, "be_MemPerm", op_pin_state_mem_pinned, N, oparity_variable, 0, sizeof(be_memperm_attr_t), &be_node_op_ops);
200 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);
201 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);
202 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);
203 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);
204 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);
205 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);
206 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);
207 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);
208 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);
209 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);
210 op_be_FrameAddr = new_ir_op(beo_base + beo_FrameAddr, "be_FrameAddr", op_pin_state_pinned, N, oparity_unary, 0, sizeof(be_frame_attr_t), &be_node_op_ops);
211 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);
212 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);
213 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);
215 set_op_tag(op_be_Spill, &be_node_tag);
216 set_op_tag(op_be_Reload, &be_node_tag);
217 set_op_tag(op_be_Perm, &be_node_tag);
218 set_op_tag(op_be_MemPerm, &be_node_tag);
219 set_op_tag(op_be_Copy, &be_node_tag);
220 set_op_tag(op_be_Keep, &be_node_tag);
221 set_op_tag(op_be_CopyKeep, &be_node_tag);
222 set_op_tag(op_be_Call, &be_node_tag);
223 set_op_tag(op_be_Return, &be_node_tag);
224 set_op_tag(op_be_AddSP, &be_node_tag);
225 set_op_tag(op_be_SetSP, &be_node_tag);
226 set_op_tag(op_be_IncSP, &be_node_tag);
227 set_op_tag(op_be_RegParams, &be_node_tag);
228 set_op_tag(op_be_StackParam, &be_node_tag);
229 set_op_tag(op_be_FrameLoad, &be_node_tag);
230 set_op_tag(op_be_FrameStore, &be_node_tag);
231 set_op_tag(op_be_FrameAddr, &be_node_tag);
232 set_op_tag(op_be_Barrier, &be_node_tag);
234 op_be_FrameAddr->ops.node_cmp_attr = FrameAddr_cmp_attr;
238 * Initializes the generic attribute of all be nodes and return ir.
240 static void *init_node_attr(ir_node* irn, int max_reg_data)
242 ir_graph *irg = get_irn_irg(irn);
243 be_node_attr_t *a = get_irn_attr(irn);
245 memset(a, 0, sizeof(get_op_attr_size(get_irn_op(irn))));
246 a->max_reg_data = max_reg_data;
249 if(max_reg_data > 0) {
252 a->reg_data = NEW_ARR_D(be_reg_data_t, get_irg_obstack(irg), max_reg_data);
253 memset(a->reg_data, 0, max_reg_data * sizeof(a->reg_data[0]));
254 for(i = 0; i < max_reg_data; ++i) {
255 a->reg_data[i].req.req.cls = NULL;
256 a->reg_data[i].req.req.type = arch_register_req_type_none;
263 int is_be_node(const ir_node *irn)
265 return get_op_tag(get_irn_op(irn)) == &be_node_tag;
268 be_opcode_t be_get_irn_opcode(const ir_node *irn)
270 return is_be_node(irn) ? get_irn_opcode(irn) - beo_base : beo_NoBeOp;
273 static int redir_proj(const ir_node **node, int pos)
275 const ir_node *n = *node;
280 *node = irn = get_Proj_pred(n);
282 assert(get_irn_mode(irn) == mode_T);
283 *node = get_Proj_pred(irn);
285 return get_Proj_proj(n);
291 static be_node_attr_t *retrieve_irn_attr(const ir_node *irn, int *the_pos)
294 be_node_attr_t *res = NULL;
295 int *pos = the_pos ? the_pos : &dummy;
299 ir_node *pred = get_Proj_pred(irn);
300 int p = get_Proj_proj(irn);
302 if(is_be_node(pred)) {
303 assert(get_irn_mode(pred) == mode_T);
305 res = get_irn_attr(pred);
306 assert(p >= 0 && p < res->max_reg_data && "illegal proj number");
310 else if(is_be_node(irn) && get_irn_mode(irn) != mode_T) {
311 be_node_attr_t *a = get_irn_attr(irn);
312 if(a->max_reg_data > 0) {
321 static be_reg_data_t *retrieve_reg_data(const ir_node *irn)
324 be_node_attr_t *a = retrieve_irn_attr(irn, &pos);
325 return a ? &a->reg_data[pos] : NULL;
329 be_node_set_irn_reg(const void *_self, ir_node *irn, const arch_register_t *reg)
331 be_reg_data_t *r = retrieve_reg_data(irn);
338 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)
346 res = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
347 a = init_node_attr(res, 2);
351 be_node_set_reg_class(res, 0, cls_frame);
352 be_node_set_reg_class(res, 1, cls);
356 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)
363 res = new_ir_node(NULL, irg, bl, op_be_Reload, mode, 2, in);
364 init_node_attr(res, 2);
365 be_node_set_reg_class(res, 0, cls_frame);
366 be_node_set_reg_class(res, -1, cls);
367 be_node_set_flags(res, -1, arch_irn_flags_rematerializable);
371 ir_node *be_get_Reload_mem(const ir_node *irn)
373 assert(be_is_Reload(irn));
374 return get_irn_n(irn, be_pos_Reload_mem);
377 ir_node *be_get_Reload_frame(const ir_node *irn)
379 assert(be_is_Reload(irn));
380 return get_irn_n(irn, be_pos_Reload_frame);
383 ir_node *be_get_Spill_val(const ir_node *irn)
385 assert(be_is_Spill(irn));
386 return get_irn_n(irn, be_pos_Spill_val);
389 ir_node *be_get_Spill_frame(const ir_node *irn)
391 assert(be_is_Spill(irn));
392 return get_irn_n(irn, be_pos_Spill_frame);
395 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
398 ir_node *irn = new_ir_node(NULL, irg, bl, op_be_Perm, mode_T, n, in);
399 init_node_attr(irn, n);
400 for(i = 0; i < n; ++i) {
401 be_node_set_reg_class(irn, i, cls);
402 be_node_set_reg_class(irn, OUT_POS(i), cls);
408 ir_node *be_new_MemPerm(const arch_env_t *arch_env, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
411 ir_node *frame = get_irg_frame(irg);
412 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
414 const arch_register_t *sp = arch_env->isa->sp;
415 be_memperm_attr_t *attr;
418 real_in = alloca((n+1) * sizeof(real_in[0]));
420 memcpy(&real_in[1], in, n * sizeof(real_in[0]));
422 irn = new_ir_node(NULL, irg, bl, op_be_MemPerm, mode_T, n+1, real_in);
424 init_node_attr(irn, n + 1);
425 be_node_set_reg_class(irn, 0, sp->reg_class);
426 for(i = 0; i < n; ++i) {
427 be_node_set_reg_class(irn, i + 1, cls_frame);
428 be_node_set_reg_class(irn, OUT_POS(i), cls_frame);
431 attr = get_irn_attr(irn);
433 attr->in_entities = obstack_alloc(irg->obst, n * sizeof(attr->in_entities[0]));
434 memset(attr->in_entities, 0, n * sizeof(attr->in_entities[0]));
435 attr->out_entities = obstack_alloc(irg->obst, n*sizeof(attr->out_entities[0]));
436 memset(attr->out_entities, 0, n*sizeof(attr->out_entities[0]));
442 ir_node *be_new_Copy(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *op)
448 res = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
449 init_node_attr(res, 1);
450 be_node_set_reg_class(res, 0, cls);
451 be_node_set_reg_class(res, OUT_POS(0), cls);
455 ir_node *be_get_Copy_op(const ir_node *cpy) {
456 return get_irn_n(cpy, be_pos_Copy_op);
459 void be_set_Copy_op(ir_node *cpy, ir_node *op) {
460 set_irn_n(cpy, be_pos_Copy_op, op);
463 ir_node *be_new_Keep(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
468 irn = new_ir_node(NULL, irg, bl, op_be_Keep, mode_ANY, n, in);
469 init_node_attr(irn, n);
470 for(i = 0; i < n; ++i) {
471 be_node_set_reg_class(irn, i, cls);
477 ir_node *be_new_Call(dbg_info *dbg, ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *sp, ir_node *ptr,
478 int n_outs, int n, ir_node *in[], ir_type *call_tp)
481 int real_n = be_pos_Call_first_arg + n;
485 NEW_ARR_A(ir_node *, real_in, real_n);
486 real_in[be_pos_Call_mem] = mem;
487 real_in[be_pos_Call_sp] = sp;
488 real_in[be_pos_Call_ptr] = ptr;
489 memcpy(&real_in[be_pos_Call_first_arg], in, n * sizeof(in[0]));
491 irn = new_ir_node(dbg, irg, bl, op_be_Call, mode_T, real_n, real_in);
492 a = init_node_attr(irn, (n_outs > real_n ? n_outs : real_n));
494 a->call_tp = call_tp;
498 /* Gets the call entity or NULL if this is no static call. */
499 entity *be_Call_get_entity(const ir_node *call) {
500 be_call_attr_t *a = get_irn_attr(call);
501 assert(be_is_Call(call));
505 /* Sets the call entity. */
506 void be_Call_set_entity(ir_node *call, entity *ent) {
507 be_call_attr_t *a = get_irn_attr(call);
508 assert(be_is_Call(call));
512 /* Gets the call type. */
513 ir_type *be_Call_get_type(ir_node *call) {
514 be_call_attr_t *a = get_irn_attr(call);
515 assert(be_is_Call(call));
519 /* Sets the call type. */
520 void be_Call_set_type(ir_node *call, ir_type *call_tp) {
521 be_call_attr_t *a = get_irn_attr(call);
522 assert(be_is_Call(call));
523 a->call_tp = call_tp;
526 /* Construct a new be_Return. */
527 ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *bl, int n_res, int n, ir_node *in[])
530 ir_node *irn = new_ir_node(dbg, irg, bl, op_be_Return, mode_X, n, in);
531 init_node_attr(irn, n);
532 a = get_irn_attr(irn);
533 a->num_ret_vals = n_res;
538 /* Returns the number of real returns values */
539 int be_Return_get_n_rets(ir_node *ret)
541 be_return_attr_t *a = get_irn_attr(ret);
542 return a->num_ret_vals;
545 ir_node *be_new_IncSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, ir_node *mem, int offset)
553 irn = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode, 2, in);
554 a = init_node_attr(irn, 1);
557 be_node_set_flags(irn, -1, arch_irn_flags_ignore | arch_irn_flags_modify_sp);
559 /* Set output constraint to stack register. */
560 be_node_set_reg_class(irn, 0, sp->reg_class);
561 be_set_constr_single_reg(irn, BE_OUT_POS(0), sp);
562 be_node_set_irn_reg(NULL, irn, sp);
567 ir_node *be_new_AddSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, ir_node *sz)
571 ir_node *in[be_pos_AddSP_last];
573 in[be_pos_AddSP_old_sp] = old_sp;
574 in[be_pos_AddSP_size] = sz;
576 irn = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T, be_pos_AddSP_last, in);
577 a = init_node_attr(irn, be_pos_AddSP_last);
579 be_node_set_flags(irn, OUT_POS(pn_be_AddSP_res), arch_irn_flags_ignore | arch_irn_flags_modify_sp);
581 /* Set output constraint to stack register. */
582 be_set_constr_single_reg(irn, be_pos_AddSP_old_sp, sp);
583 be_node_set_reg_class(irn, be_pos_AddSP_size, arch_register_get_class(sp));
584 be_set_constr_single_reg(irn, OUT_POS(pn_be_AddSP_res), sp);
585 a->reg_data[pn_be_AddSP_res].reg = sp;
590 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)
599 irn = new_ir_node(NULL, irg, bl, op_be_SetSP, get_irn_mode(old_sp), 3, in);
600 a = init_node_attr(irn, 3);
602 be_node_set_flags(irn, OUT_POS(0), arch_irn_flags_ignore | arch_irn_flags_modify_sp);
604 /* Set output constraint to stack register. */
605 be_set_constr_single_reg(irn, OUT_POS(0), sp);
606 be_node_set_reg_class(irn, 1, sp->reg_class);
607 be_node_set_reg_class(irn, 2, sp->reg_class);
608 be_node_set_irn_reg(NULL, irn, sp);
613 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)
619 in[0] = frame_pointer;
620 irn = new_ir_node(NULL, irg, bl, op_be_StackParam, mode, 1, in);
621 a = init_node_attr(irn, 1);
624 be_node_set_reg_class(irn, 0, cls_frame);
625 be_node_set_reg_class(irn, OUT_POS(0), cls);
629 ir_node *be_new_RegParams(ir_graph *irg, ir_node *bl, int n_outs)
634 irn = new_ir_node(NULL, irg, bl, op_be_RegParams, mode_T, 0, in);
635 init_node_attr(irn, n_outs);
639 ir_node *be_new_FrameLoad(const arch_register_class_t *cls_frame, const arch_register_class_t *cls_data,
640 ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *frame, entity *ent)
648 irn = new_ir_node(NULL, irg, bl, op_be_FrameLoad, mode_T, 2, in);
649 a = init_node_attr(irn, 3);
652 be_node_set_reg_class(irn, 1, cls_frame);
653 be_node_set_reg_class(irn, OUT_POS(pn_Load_res), cls_data);
657 ir_node *be_new_FrameStore(const arch_register_class_t *cls_frame, const arch_register_class_t *cls_data,
658 ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *frame, ir_node *data, entity *ent)
667 irn = new_ir_node(NULL, irg, bl, op_be_FrameStore, mode_T, 3, in);
668 a = init_node_attr(irn, 3);
671 be_node_set_reg_class(irn, 1, cls_frame);
672 be_node_set_reg_class(irn, 2, cls_data);
676 ir_node *be_new_FrameAddr(const arch_register_class_t *cls_frame, ir_graph *irg, ir_node *bl, ir_node *frame, entity *ent)
683 irn = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
684 a = init_node_attr(irn, 1);
687 be_node_set_reg_class(irn, 0, cls_frame);
688 be_node_set_reg_class(irn, OUT_POS(0), cls_frame);
690 return optimize_node(irn);
693 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)
696 ir_node **in = (ir_node **) alloca((n + 1) * sizeof(in[0]));
699 memcpy(&in[1], in_keep, n * sizeof(in[0]));
700 irn = new_ir_node(NULL, irg, bl, op_be_CopyKeep, mode, n + 1, in);
701 init_node_attr(irn, n + 1);
702 be_node_set_reg_class(irn, OUT_POS(0), cls);
703 be_node_set_reg_class(irn, 0, cls);
708 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)
713 return be_new_CopyKeep(cls, irg, bl, src, 1, in, mode);
716 ir_node *be_get_CopyKeep_op(const ir_node *cpy) {
717 return get_irn_n(cpy, be_pos_CopyKeep_op);
720 void be_set_CopyKeep_op(ir_node *cpy, ir_node *op) {
721 set_irn_n(cpy, be_pos_CopyKeep_op, op);
724 ir_node *be_new_Barrier(ir_graph *irg, ir_node *bl, int n, ir_node *in[])
728 irn = new_ir_node(NULL, irg, bl, op_be_Barrier, mode_T, n, in);
729 init_node_attr(irn, n);
733 int be_is_Spill (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Spill ; }
734 int be_is_Reload (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Reload ; }
735 int be_is_Copy (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Copy ; }
736 int be_is_CopyKeep (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_CopyKeep ; }
737 int be_is_Perm (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Perm ; }
738 int be_is_MemPerm (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_MemPerm ; }
739 int be_is_Keep (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Keep ; }
740 int be_is_Call (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Call ; }
741 int be_is_Return (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Return ; }
742 int be_is_IncSP (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_IncSP ; }
743 int be_is_SetSP (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_SetSP ; }
744 int be_is_AddSP (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_AddSP ; }
745 int be_is_RegParams (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_RegParams ; }
746 int be_is_StackParam (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_StackParam ; }
747 int be_is_FrameAddr (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameAddr ; }
748 int be_is_FrameLoad (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameLoad ; }
749 int be_is_FrameStore (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameStore ; }
750 int be_is_Barrier (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Barrier ; }
752 int be_has_frame_entity(const ir_node *irn)
754 switch(be_get_irn_opcode(irn)) {
767 entity* be_get_frame_entity(const ir_node *irn)
769 if(be_has_frame_entity(irn)) {
770 be_frame_attr_t *a = get_irn_attr(irn);
776 void be_set_MemPerm_in_entity(const ir_node *irn, int n, entity *ent)
778 be_memperm_attr_t *attr = get_irn_attr(irn);
780 assert(be_is_MemPerm(irn));
781 assert(n < be_get_MemPerm_entity_arity(irn));
783 attr->in_entities[n] = ent;
786 entity* be_get_MemPerm_in_entity(const ir_node* irn, int n)
788 be_memperm_attr_t *attr = get_irn_attr(irn);
790 assert(be_is_MemPerm(irn));
791 assert(n < be_get_MemPerm_entity_arity(irn));
793 return attr->in_entities[n];
796 void be_set_MemPerm_out_entity(const ir_node *irn, int n, entity *ent)
798 be_memperm_attr_t *attr = get_irn_attr(irn);
800 assert(be_is_MemPerm(irn));
801 assert(n < be_get_MemPerm_entity_arity(irn));
803 attr->out_entities[n] = ent;
806 entity* be_get_MemPerm_out_entity(const ir_node* irn, int n)
808 be_memperm_attr_t *attr = get_irn_attr(irn);
810 assert(be_is_MemPerm(irn));
811 assert(n < be_get_MemPerm_entity_arity(irn));
813 return attr->out_entities[n];
816 int be_get_MemPerm_entity_arity(const ir_node *irn)
818 return get_irn_arity(irn) - 1;
821 static void be_limited(void *data, bitset_t *bs)
823 be_req_t *req = data;
826 case be_req_kind_negate_old_limited:
827 case be_req_kind_old_limited:
828 req->x.old_limited.old_limited(req->x.old_limited.old_limited_env, bs);
829 if(req->kind == be_req_kind_negate_old_limited)
832 case be_req_kind_single_reg:
833 bitset_clear_all(bs);
834 bitset_set(bs, req->x.single_reg->index);
839 static INLINE be_req_t *get_req(ir_node *irn, int pos)
841 int idx = pos < 0 ? -(pos + 1) : pos;
842 be_node_attr_t *a = get_irn_attr(irn);
843 be_reg_data_t *rd = &a->reg_data[idx];
844 be_req_t *r = pos < 0 ? &rd->req : &rd->in_req;
846 assert(is_be_node(irn));
847 assert(!(pos >= 0) || pos < get_irn_arity(irn));
848 assert(!(pos < 0) || -(pos + 1) <= a->max_reg_data);
853 void be_set_constr_single_reg(ir_node *irn, int pos, const arch_register_t *reg)
855 be_req_t *r = get_req(irn, pos);
857 r->kind = be_req_kind_single_reg;
858 r->x.single_reg = reg;
859 r->req.limited = be_limited;
860 r->req.limited_env = r;
861 r->req.type = arch_register_req_type_limited;
862 r->req.cls = reg->reg_class;
865 void be_set_constr_limited(ir_node *irn, int pos, const arch_register_req_t *req)
867 be_req_t *r = get_req(irn, pos);
869 assert(arch_register_req_is(req, limited));
871 r->kind = be_req_kind_old_limited;
872 r->req.limited = be_limited;
873 r->req.limited_env = r;
874 r->req.type = arch_register_req_type_limited;
875 r->req.cls = req->cls;
877 r->x.old_limited.old_limited = req->limited;
878 r->x.old_limited.old_limited_env = req->limited_env;
881 void be_node_set_flags(ir_node *irn, int pos, arch_irn_flags_t flags)
883 be_req_t *r = get_req(irn, pos);
887 void be_node_set_reg_class(ir_node *irn, int pos, const arch_register_class_t *cls)
889 be_req_t *r = get_req(irn, pos);
891 if(r->req.type == arch_register_req_type_none)
892 r->req.type = arch_register_req_type_normal;
895 void be_node_set_req_type(ir_node *irn, int pos, arch_register_req_type_t type)
897 be_req_t *r = get_req(irn, pos);
901 ir_node *be_get_IncSP_pred(ir_node *irn) {
902 assert(be_is_IncSP(irn));
903 return get_irn_n(irn, 0);
906 void be_set_IncSP_pred(ir_node *incsp, ir_node *pred) {
907 assert(be_is_IncSP(incsp));
908 set_irn_n(incsp, 0, pred);
911 ir_node *be_get_IncSP_mem(ir_node *irn) {
912 assert(be_is_IncSP(irn));
913 return get_irn_n(irn, 1);
916 void be_set_IncSP_offset(ir_node *irn, int offset)
918 be_stack_attr_t *a = get_irn_attr(irn);
919 assert(be_is_IncSP(irn));
923 int be_get_IncSP_offset(const ir_node *irn)
925 be_stack_attr_t *a = get_irn_attr(irn);
926 assert(be_is_IncSP(irn));
930 ir_node *be_spill(const arch_env_t *arch_env, ir_node *irn)
932 ir_node *bl = get_nodes_block(irn);
933 ir_graph *irg = get_irn_irg(bl);
934 ir_node *frame = get_irg_frame(irg);
937 const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, irn, -1);
938 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
940 spill = be_new_Spill(cls, cls_frame, irg, bl, frame, irn);
944 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)
948 ir_node *block = get_nodes_block(insert);
949 ir_graph *irg = get_irn_irg(block);
950 ir_node *frame = get_irg_frame(irg);
951 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
953 assert(be_is_Spill(spill) || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
955 reload = be_new_Reload(cls, cls_frame, irg, block, frame, spill, mode);
957 assert(!is_Block(insert));
959 sched_add_before(insert, reload);
966 | _ \ ___ __ _ | _ \ ___ __ _ ___
967 | |_) / _ \/ _` | | |_) / _ \/ _` / __|
968 | _ < __/ (_| | | _ < __/ (_| \__ \
969 |_| \_\___|\__, | |_| \_\___|\__, |___/
975 static void *put_out_reg_req(arch_register_req_t *req, const ir_node *irn, int out_pos)
977 const be_node_attr_t *a = get_irn_attr(irn);
979 if(out_pos < a->max_reg_data) {
980 memcpy(req, &a->reg_data[out_pos].req, sizeof(req[0]));
982 if(be_is_Copy(irn)) {
983 req->type |= arch_register_req_type_should_be_same;
984 req->other_same = be_get_Copy_op(irn);
988 req->type = arch_register_req_type_none;
995 static void *put_in_reg_req(arch_register_req_t *req, const ir_node *irn, int pos)
997 const be_node_attr_t *a = get_irn_attr(irn);
999 if(pos < get_irn_arity(irn) && pos < a->max_reg_data)
1000 memcpy(req, &a->reg_data[pos].in_req, sizeof(req[0]));
1002 req->type = arch_register_req_type_none;
1009 static const arch_register_req_t *
1010 be_node_get_irn_reg_req(const void *self, arch_register_req_t *req, const ir_node *irn, int pos)
1015 if(get_irn_mode(irn) == mode_T)
1018 out_pos = redir_proj((const ir_node **) &irn, pos);
1019 assert(is_be_node(irn));
1020 return put_out_reg_req(req, irn, out_pos);
1024 return is_be_node(irn) ? put_in_reg_req(req, irn, pos) : NULL;
1030 const arch_register_t *
1031 be_node_get_irn_reg(const void *_self, const ir_node *irn)
1033 be_reg_data_t *r = retrieve_reg_data(irn);
1034 return r ? r->reg : NULL;
1037 static arch_irn_class_t be_node_classify(const void *_self, const ir_node *irn)
1039 redir_proj((const ir_node **) &irn, -1);
1041 switch(be_get_irn_opcode(irn)) {
1042 #define XXX(a,b) case beo_ ## a: return arch_irn_class_ ## b
1044 XXX(Reload, reload);
1047 XXX(Return, branch);
1048 XXX(StackParam, stackparam);
1051 return arch_irn_class_normal;
1057 static arch_irn_flags_t be_node_get_flags(const void *_self, const ir_node *irn)
1059 be_reg_data_t *r = retrieve_reg_data(irn);
1060 return r ? r->req.flags : 0;
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_entity(const void *self, ir_node *irn, entity *ent)
1072 assert(be_has_frame_entity(irn));
1074 a = get_irn_attr(irn);
1078 static void be_node_set_frame_offset(const void *self, ir_node *irn, int offset)
1080 if(be_has_frame_entity(irn)) {
1081 be_frame_attr_t *a = get_irn_attr(irn);
1086 static int be_node_get_sp_bias(const void *self, const ir_node *irn)
1090 if(be_is_IncSP(irn)) {
1091 result = be_get_IncSP_offset(irn);
1098 ___ ____ _ _ _ _ _ _
1099 |_ _| _ \| \ | | | | | | __ _ _ __ __| | | ___ _ __
1100 | || |_) | \| | | |_| |/ _` | '_ \ / _` | |/ _ \ '__|
1101 | || _ <| |\ | | _ | (_| | | | | (_| | | __/ |
1102 |___|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
1106 static const arch_irn_ops_if_t be_node_irn_ops_if = {
1107 be_node_get_irn_reg_req,
1108 be_node_set_irn_reg,
1109 be_node_get_irn_reg,
1112 be_node_get_frame_entity,
1113 be_node_set_frame_entity,
1114 be_node_set_frame_offset,
1115 be_node_get_sp_bias,
1116 NULL, /* get_inverse */
1117 NULL, /* get_op_estimated_cost */
1118 NULL, /* possible_memory_operand */
1119 NULL, /* perform_memory_operand */
1122 static const arch_irn_ops_t be_node_irn_ops = {
1126 const void *be_node_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn)
1128 redir_proj((const ir_node **) &irn, -1);
1129 return is_be_node(irn) ? &be_node_irn_ops : NULL;
1132 const arch_irn_handler_t be_node_irn_handler = {
1137 ____ _ _ ___ ____ _ _ _ _ _ _
1138 | _ \| |__ (_) |_ _| _ \| \ | | | | | | __ _ _ __ __| | | ___ _ __
1139 | |_) | '_ \| | | || |_) | \| | | |_| |/ _` | '_ \ / _` | |/ _ \ '__|
1140 | __/| | | | | | || _ <| |\ | | _ | (_| | | | | (_| | | __/ |
1141 |_| |_| |_|_| |___|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
1146 arch_irn_handler_t irn_handler;
1147 arch_irn_ops_t irn_ops;
1148 const arch_env_t *arch_env;
1152 #define get_phi_handler_from_handler(h) container_of(h, phi_handler_t, irn_handler)
1153 #define get_phi_handler_from_ops(h) container_of(h, phi_handler_t, irn_ops)
1155 static const void *phi_get_irn_ops(const arch_irn_handler_t *handler, const ir_node *irn)
1157 const phi_handler_t *h = get_phi_handler_from_handler(handler);
1158 return is_Phi(irn) && mode_is_datab(get_irn_mode(irn)) ? &h->irn_ops : NULL;
1162 * Get register class of a Phi.
1165 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)
1167 int n = get_irn_arity(phi);
1171 if(*visited && pset_find_ptr(*visited, phi))
1174 for(i = 0; i < n; ++i) {
1175 op = get_irn_n(phi, i);
1177 return arch_get_register_req(h->arch_env, req, op, BE_OUT_POS(0));
1181 The operands of that Phi were all Phis themselves.
1182 We have to start a DFS for a non-Phi argument now.
1185 *visited = pset_new_ptr(16);
1187 pset_insert_ptr(*visited, phi);
1189 for(i = 0; i < n; ++i) {
1190 op = get_irn_n(phi, i);
1191 if(get_Phi_reg_req_recursive(h, req, op, visited))
1198 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)
1200 phi_handler_t *phi_handler = get_phi_handler_from_ops(self);
1201 pset *visited = NULL;
1203 get_Phi_reg_req_recursive(phi_handler, req, irn, &visited);
1204 /* Set the requirements type to normal, since an operand of the Phi could have had constraints. */
1205 req->type = arch_register_req_type_normal;
1212 static void phi_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg)
1214 phi_handler_t *h = get_phi_handler_from_ops(self);
1215 pmap_insert(h->regs, irn, (void *) reg);
1218 static const arch_register_t *phi_get_irn_reg(const void *self, const ir_node *irn)
1220 phi_handler_t *h = get_phi_handler_from_ops(self);
1221 return pmap_get(h->regs, (void *) irn);
1224 static arch_irn_class_t phi_classify(const void *_self, const ir_node *irn)
1226 return arch_irn_class_normal;
1229 static arch_irn_flags_t phi_get_flags(const void *_self, const ir_node *irn)
1231 return arch_irn_flags_none;
1234 static entity *phi_get_frame_entity(const void *_self, const ir_node *irn)
1239 static void phi_set_frame_entity(const void *_self, ir_node *irn, entity *ent)
1243 static void phi_set_frame_offset(const void *_self, ir_node *irn, int bias)
1247 static int phi_get_sp_bias(const void* self, const ir_node *irn)
1252 static const arch_irn_ops_if_t phi_irn_ops = {
1253 phi_get_irn_reg_req,
1258 phi_get_frame_entity,
1259 phi_set_frame_entity,
1260 phi_set_frame_offset,
1262 NULL, /* get_inverse */
1263 NULL, /* get_op_estimated_cost */
1264 NULL, /* possible_memory_operand */
1265 NULL, /* perform_memory_operand */
1268 static const arch_irn_handler_t phi_irn_handler = {
1272 arch_irn_handler_t *be_phi_handler_new(const arch_env_t *arch_env)
1274 phi_handler_t *h = xmalloc(sizeof(h[0]));
1275 h->irn_handler.get_irn_ops = phi_get_irn_ops;
1276 h->irn_ops.impl = &phi_irn_ops;
1277 h->arch_env = arch_env;
1278 h->regs = pmap_create();
1279 return (arch_irn_handler_t *) h;
1282 void be_phi_handler_free(arch_irn_handler_t *handler)
1284 phi_handler_t *h = (void *) handler;
1285 pmap_destroy(h->regs);
1289 const void *be_phi_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn)
1291 phi_handler_t *phi_handler = get_phi_handler_from_handler(self);
1292 return is_Phi(irn) ? &phi_handler->irn_ops : NULL;
1295 void be_phi_handler_reset(arch_irn_handler_t *handler)
1297 phi_handler_t *h = get_phi_handler_from_handler(handler);
1299 pmap_destroy(h->regs);
1300 h->regs = pmap_create();
1305 | \ | | ___ __| | ___ | _ \ _ _ _ __ ___ _ __ (_)_ __ __ _
1306 | \| |/ _ \ / _` |/ _ \ | | | | | | | '_ ` _ \| '_ \| | '_ \ / _` |
1307 | |\ | (_) | (_| | __/ | |_| | |_| | | | | | | |_) | | | | | (_| |
1308 |_| \_|\___/ \__,_|\___| |____/ \__,_|_| |_| |_| .__/|_|_| |_|\__, |
1313 * Dumps a register requirement to a file.
1315 static void dump_node_req(FILE *f, int idx, be_req_t *req)
1318 int did_something = 0;
1320 const char *prefix = buf;
1322 snprintf(buf, sizeof(buf), "#%d ", idx);
1323 buf[sizeof(buf) - 1] = '\0';
1325 if(req->flags != arch_irn_flags_none) {
1326 fprintf(f, "%sflags: ", prefix);
1328 for(i = arch_irn_flags_none; i <= log2_ceil(arch_irn_flags_last); ++i) {
1329 if(req->flags & (1 << i)) {
1330 fprintf(f, "%s%s", prefix, arch_irn_flag_str(1 << i));
1338 if(req->req.cls != 0) {
1341 arch_register_req_format(tmp, sizeof(tmp), &req->req);
1342 fprintf(f, "%s", tmp);
1351 * Dumps node register requirements to a file.
1353 static void dump_node_reqs(FILE *f, ir_node *irn)
1356 be_node_attr_t *a = get_irn_attr(irn);
1358 fprintf(f, "registers: \n");
1359 for(i = 0; i < a->max_reg_data; ++i) {
1360 be_reg_data_t *rd = &a->reg_data[i];
1362 fprintf(f, "#%d: %s\n", i, rd->reg->name);
1365 fprintf(f, "in requirements\n");
1366 for(i = 0; i < a->max_reg_data; ++i) {
1367 dump_node_req(f, i, &a->reg_data[i].in_req);
1370 fprintf(f, "\nout requirements\n");
1371 for(i = 0; i < a->max_reg_data; ++i) {
1372 dump_node_req(f, i, &a->reg_data[i].req);
1377 * ir_op-Operation: dump a be node to file
1379 static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason)
1381 be_node_attr_t *at = get_irn_attr(irn);
1383 assert(is_be_node(irn));
1386 case dump_node_opcode_txt:
1387 fprintf(f, get_op_name(get_irn_op(irn)));
1389 case dump_node_mode_txt:
1390 fprintf(f, get_mode_name(get_irn_mode(irn)));
1392 case dump_node_nodeattr_txt:
1394 case dump_node_info_txt:
1395 dump_node_reqs(f, irn);
1397 if(be_has_frame_entity(irn)) {
1398 be_frame_attr_t *a = (be_frame_attr_t *) at;
1400 int bits = get_type_size_bits(get_entity_type(a->ent));
1401 ir_fprintf(f, "frame entity: %+F, offset 0x%x (%d), size 0x%x (%d) bits\n",
1402 a->ent, a->offset, a->offset, bits, bits);
1407 switch(be_get_irn_opcode(irn)) {
1410 be_stack_attr_t *a = (be_stack_attr_t *) at;
1411 if (a->offset == BE_STACK_FRAME_SIZE_EXPAND)
1412 fprintf(f, "offset: FRAME_SIZE\n");
1413 else if(a->offset == BE_STACK_FRAME_SIZE_SHRINK)
1414 fprintf(f, "offset: -FRAME SIZE\n");
1416 fprintf(f, "offset: %u\n", a->offset);
1421 be_call_attr_t *a = (be_call_attr_t *) at;
1424 fprintf(f, "\ncalling: %s\n", get_entity_name(a->ent));
1430 for(i = 0; i < be_get_MemPerm_entity_arity(irn); ++i) {
1432 in = be_get_MemPerm_in_entity(irn, i);
1433 out = be_get_MemPerm_out_entity(irn, i);
1435 fprintf(f, "\nin[%d]: %s\n", i, get_entity_name(in));
1438 fprintf(f, "\nout[%d]: %s\n", i, get_entity_name(out));
1454 * Copies the backend specific attributes from old node to new node.
1456 static void copy_attr(const ir_node *old_node, ir_node *new_node)
1458 be_node_attr_t *old_attr = get_irn_attr(old_node);
1459 be_node_attr_t *new_attr = get_irn_attr(new_node);
1462 assert(is_be_node(old_node));
1463 assert(is_be_node(new_node));
1465 memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
1466 new_attr->reg_data = NULL;
1468 if(new_attr->max_reg_data > 0) {
1469 new_attr->reg_data = NEW_ARR_D(be_reg_data_t, get_irg_obstack(get_irn_irg(new_node)), new_attr->max_reg_data);
1470 memcpy(new_attr->reg_data, old_attr->reg_data, new_attr->max_reg_data * sizeof(be_reg_data_t));
1472 for(i = 0; i < old_attr->max_reg_data; ++i) {
1475 r = &new_attr->reg_data[i].req;
1476 r->req.limited_env = r;
1478 r = &new_attr->reg_data[i].in_req;
1479 r->req.limited_env = r;
1484 static const ir_op_ops be_node_op_ops = {