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 increased/decreased. */
93 be_stack_dir_t dir; /**< The direction in which the stack shall be modified (expand or shrink). */
96 /** The be_Frame attribute type. */
98 be_node_attr_t node_attr;
103 /** The be_Call attribute type. */
105 be_node_attr_t node_attr;
106 entity *ent; /**< The called entity if this is a static call. */
107 ir_type *call_tp; /**< The call type, copied from the original Call node. */
111 be_node_attr_t node_attr;
112 entity **in_entities;
113 entity **out_entities;
119 ir_op *op_be_MemPerm;
122 ir_op *op_be_CopyKeep;
128 ir_op *op_be_RegParams;
129 ir_op *op_be_StackParam;
130 ir_op *op_be_FrameAddr;
131 ir_op *op_be_FrameLoad;
132 ir_op *op_be_FrameStore;
133 ir_op *op_be_Barrier;
135 static int beo_base = -1;
137 static const ir_op_ops be_node_op_ops;
139 #define N irop_flag_none
140 #define L irop_flag_labeled
141 #define C irop_flag_commutative
142 #define X irop_flag_cfopcode
143 #define I irop_flag_ip_cfopcode
144 #define F irop_flag_fragile
145 #define Y irop_flag_forking
146 #define H irop_flag_highlevel
147 #define c irop_flag_constlike
148 #define K irop_flag_keep
149 #define M irop_flag_machine
153 * Compare two node attributes.
155 * @return zero if both attributes are identically
157 static int cmp_node_attr(be_node_attr_t *a, be_node_attr_t *b) {
158 if (a->max_reg_data == b->max_reg_data) {
161 for (i = 0; i < a->max_reg_data; ++i) {
162 if (a->reg_data[i].reg != b->reg_data[i].reg ||
163 memcmp(&a->reg_data[i].in_req, &b->reg_data[i].in_req, sizeof(b->reg_data[i].in_req)) ||
164 memcmp(&a->reg_data[i].req, &b->reg_data[i].req, sizeof(a->reg_data[i].req)))
173 * Compare the attributes of two FrameAddr nodes.
175 * @return zero if both attributes are identically
177 static int FrameAddr_cmp_attr(ir_node *a, ir_node *b) {
178 be_frame_attr_t *a_attr = get_irn_attr(a);
179 be_frame_attr_t *b_attr = get_irn_attr(b);
181 if (a_attr->ent == b_attr->ent && a_attr->offset == b_attr->offset)
182 return cmp_node_attr(&a_attr->node_attr, &b_attr->node_attr);
186 void be_node_init(void) {
187 static int inited = 0;
194 /* Acquire all needed opcodes. */
195 beo_base = get_next_ir_opcodes(beo_Last - 1);
197 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);
198 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);
199 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);
200 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);
201 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);
202 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);
203 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);
204 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);
205 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);
206 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);
207 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);
208 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);
209 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);
210 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);
211 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);
212 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);
213 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);
214 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);
216 set_op_tag(op_be_Spill, &be_node_tag);
217 set_op_tag(op_be_Reload, &be_node_tag);
218 set_op_tag(op_be_Perm, &be_node_tag);
219 set_op_tag(op_be_MemPerm, &be_node_tag);
220 set_op_tag(op_be_Copy, &be_node_tag);
221 set_op_tag(op_be_Keep, &be_node_tag);
222 set_op_tag(op_be_CopyKeep, &be_node_tag);
223 set_op_tag(op_be_Call, &be_node_tag);
224 set_op_tag(op_be_Return, &be_node_tag);
225 set_op_tag(op_be_AddSP, &be_node_tag);
226 set_op_tag(op_be_SetSP, &be_node_tag);
227 set_op_tag(op_be_IncSP, &be_node_tag);
228 set_op_tag(op_be_RegParams, &be_node_tag);
229 set_op_tag(op_be_StackParam, &be_node_tag);
230 set_op_tag(op_be_FrameLoad, &be_node_tag);
231 set_op_tag(op_be_FrameStore, &be_node_tag);
232 set_op_tag(op_be_FrameAddr, &be_node_tag);
233 set_op_tag(op_be_Barrier, &be_node_tag);
235 op_be_FrameAddr->ops.node_cmp_attr = FrameAddr_cmp_attr;
239 * Initializes the generic attribute of all be nodes and return ir.
241 static void *init_node_attr(ir_node* irn, int max_reg_data)
243 ir_graph *irg = get_irn_irg(irn);
244 be_node_attr_t *a = get_irn_attr(irn);
246 memset(a, 0, sizeof(get_op_attr_size(get_irn_op(irn))));
247 a->max_reg_data = max_reg_data;
250 if(max_reg_data > 0) {
253 a->reg_data = NEW_ARR_D(be_reg_data_t, get_irg_obstack(irg), max_reg_data);
254 memset(a->reg_data, 0, max_reg_data * sizeof(a->reg_data[0]));
255 for(i = 0; i < max_reg_data; ++i) {
256 a->reg_data[i].req.req.cls = NULL;
257 a->reg_data[i].req.req.type = arch_register_req_type_none;
264 int is_be_node(const ir_node *irn)
266 return get_op_tag(get_irn_op(irn)) == &be_node_tag;
269 be_opcode_t be_get_irn_opcode(const ir_node *irn)
271 return is_be_node(irn) ? get_irn_opcode(irn) - beo_base : beo_NoBeOp;
274 static int redir_proj(const ir_node **node, int pos)
276 const ir_node *n = *node;
281 assert(pos == -1 && "Illegal pos for a Proj");
282 *node = irn = get_Proj_pred(n);
284 assert(get_irn_mode(irn) == mode_T);
285 *node = get_Proj_pred(irn);
287 return get_Proj_proj(n);
294 be_node_set_irn_reg(const void *_self, ir_node *irn, const arch_register_t *reg)
299 out_pos = redir_proj((const ir_node **) &irn, -1);
300 a = get_irn_attr(irn);
302 assert(is_be_node(irn));
303 assert(out_pos < a->max_reg_data && "position too high");
304 a->reg_data[out_pos].reg = reg;
308 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)
316 res = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
317 a = init_node_attr(res, 2);
321 be_node_set_reg_class(res, 0, cls_frame);
322 be_node_set_reg_class(res, 1, cls);
326 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)
333 res = new_ir_node(NULL, irg, bl, op_be_Reload, mode, 2, in);
334 init_node_attr(res, 2);
335 be_node_set_reg_class(res, 0, cls_frame);
336 be_node_set_reg_class(res, -1, cls);
340 ir_node *be_get_Reload_mem(const ir_node *irn)
342 assert(be_is_Reload(irn));
343 return get_irn_n(irn, be_pos_Reload_mem);
346 ir_node *be_get_Reload_frame(const ir_node *irn)
348 assert(be_is_Reload(irn));
349 return get_irn_n(irn, be_pos_Reload_frame);
352 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
355 ir_node *irn = new_ir_node(NULL, irg, bl, op_be_Perm, mode_T, n, in);
356 init_node_attr(irn, n);
357 for(i = 0; i < n; ++i) {
358 be_node_set_reg_class(irn, i, cls);
359 be_node_set_reg_class(irn, OUT_POS(i), cls);
365 ir_node *be_new_MemPerm(const arch_env_t *arch_env, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
368 ir_node *frame = get_irg_frame(irg);
369 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
370 ir_node *irn = new_ir_node(NULL, irg, bl, op_be_MemPerm, mode_T, n, in);
371 be_memperm_attr_t *attr;
373 init_node_attr(irn, n);
374 for(i = 0; i < n; ++i) {
375 be_node_set_reg_class(irn, i, cls_frame);
376 be_node_set_reg_class(irn, OUT_POS(i), cls_frame);
379 attr = get_irn_attr(irn);
381 attr->in_entities = obstack_alloc(irg->obst, n*sizeof(attr->in_entities[0]));
382 memset(attr->in_entities, 0, n*sizeof(attr->in_entities[0]));
383 attr->out_entities = obstack_alloc(irg->obst, n*sizeof(attr->out_entities[0]));
384 memset(attr->out_entities, 0, n*sizeof(attr->out_entities[0]));
390 ir_node *be_new_Copy(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *op)
396 res = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
397 init_node_attr(res, 1);
398 be_node_set_reg_class(res, 0, cls);
399 be_node_set_reg_class(res, OUT_POS(0), cls);
403 ir_node *be_get_Copy_op(const ir_node *cpy) {
404 return get_irn_n(cpy, be_pos_Copy_op);
407 void be_set_Copy_op(ir_node *cpy, ir_node *op) {
408 set_irn_n(cpy, be_pos_Copy_op, op);
411 ir_node *be_new_Keep(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
416 irn = new_ir_node(NULL, irg, bl, op_be_Keep, mode_ANY, n, in);
417 init_node_attr(irn, n);
418 for(i = 0; i < n; ++i) {
419 be_node_set_reg_class(irn, i, cls);
425 ir_node *be_new_Call(dbg_info *dbg, ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *sp, ir_node *ptr,
426 int n_outs, int n, ir_node *in[], ir_type *call_tp)
429 int real_n = be_pos_Call_first_arg + n;
433 NEW_ARR_A(ir_node *, real_in, real_n);
434 real_in[be_pos_Call_mem] = mem;
435 real_in[be_pos_Call_sp] = sp;
436 real_in[be_pos_Call_ptr] = ptr;
437 memcpy(&real_in[be_pos_Call_first_arg], in, n * sizeof(in[0]));
439 irn = new_ir_node(dbg, irg, bl, op_be_Call, mode_T, real_n, real_in);
440 a = init_node_attr(irn, (n_outs > real_n ? n_outs : real_n));
442 a->call_tp = call_tp;
446 /* Gets the call entity or NULL if this is no static call. */
447 entity *be_Call_get_entity(const ir_node *call) {
448 be_call_attr_t *a = get_irn_attr(call);
449 assert(be_is_Call(call));
453 /* Sets the call entity. */
454 void be_Call_set_entity(ir_node *call, entity *ent) {
455 be_call_attr_t *a = get_irn_attr(call);
456 assert(be_is_Call(call));
460 /* Gets the call type. */
461 ir_type *be_Call_get_type(ir_node *call) {
462 be_call_attr_t *a = get_irn_attr(call);
463 assert(be_is_Call(call));
467 /* Sets the call type. */
468 void be_Call_set_type(ir_node *call, ir_type *call_tp) {
469 be_call_attr_t *a = get_irn_attr(call);
470 assert(be_is_Call(call));
471 a->call_tp = call_tp;
474 /* Construct a new be_Return. */
475 ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *bl, int n_res, int n, ir_node *in[])
478 ir_node *irn = new_ir_node(dbg, irg, bl, op_be_Return, mode_X, n, in);
479 init_node_attr(irn, n);
480 a = get_irn_attr(irn);
481 a->num_ret_vals = n_res;
486 /* Returns the number of real returns values */
487 int be_Return_get_n_rets(ir_node *ret)
489 be_return_attr_t *a = get_irn_attr(ret);
490 return a->num_ret_vals;
493 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)
501 irn = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode, 2, in);
502 a = init_node_attr(irn, 1);
506 be_node_set_flags(irn, -1, arch_irn_flags_ignore | arch_irn_flags_modify_sp);
508 /* Set output constraint to stack register. */
509 be_node_set_reg_class(irn, 0, sp->reg_class);
510 be_set_constr_single_reg(irn, BE_OUT_POS(0), sp);
511 be_node_set_irn_reg(NULL, irn, sp);
516 ir_node *be_new_AddSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, ir_node *sz)
520 ir_node *in[be_pos_AddSP_last];
522 in[be_pos_AddSP_old_sp] = old_sp;
523 in[be_pos_AddSP_size] = sz;
525 irn = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T, be_pos_AddSP_last, in);
526 a = init_node_attr(irn, be_pos_AddSP_last);
528 be_node_set_flags(irn, OUT_POS(0), arch_irn_flags_ignore | arch_irn_flags_modify_sp);
530 /* Set output constraint to stack register. */
531 be_set_constr_single_reg(irn, be_pos_AddSP_old_sp, sp);
532 be_node_set_reg_class(irn, be_pos_AddSP_size, arch_register_get_class(sp));
533 be_set_constr_single_reg(irn, OUT_POS(0), sp);
534 a->reg_data[pn_be_AddSP_res].reg = sp;
539 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)
548 irn = new_ir_node(NULL, irg, bl, op_be_SetSP, get_irn_mode(old_sp), 3, in);
549 a = init_node_attr(irn, 3);
551 be_node_set_flags(irn, OUT_POS(0), arch_irn_flags_ignore | arch_irn_flags_modify_sp);
553 /* Set output constraint to stack register. */
554 be_set_constr_single_reg(irn, OUT_POS(0), sp);
555 be_node_set_reg_class(irn, 1, sp->reg_class);
556 be_node_set_reg_class(irn, 2, sp->reg_class);
557 be_node_set_irn_reg(NULL, irn, sp);
562 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)
568 in[0] = frame_pointer;
569 irn = new_ir_node(NULL, irg, bl, op_be_StackParam, mode, 1, in);
570 a = init_node_attr(irn, 1);
573 be_node_set_reg_class(irn, 0, cls_frame);
574 be_node_set_reg_class(irn, OUT_POS(0), cls);
578 ir_node *be_new_RegParams(ir_graph *irg, ir_node *bl, int n_outs)
583 irn = new_ir_node(NULL, irg, bl, op_be_RegParams, mode_T, 0, in);
584 init_node_attr(irn, n_outs);
588 ir_node *be_new_FrameLoad(const arch_register_class_t *cls_frame, const arch_register_class_t *cls_data,
589 ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *frame, entity *ent)
597 irn = new_ir_node(NULL, irg, bl, op_be_FrameLoad, mode_T, 2, in);
598 a = init_node_attr(irn, 3);
601 be_node_set_reg_class(irn, 1, cls_frame);
602 be_node_set_reg_class(irn, OUT_POS(pn_Load_res), cls_data);
606 ir_node *be_new_FrameStore(const arch_register_class_t *cls_frame, const arch_register_class_t *cls_data,
607 ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *frame, ir_node *data, entity *ent)
616 irn = new_ir_node(NULL, irg, bl, op_be_FrameStore, mode_T, 3, in);
617 a = init_node_attr(irn, 3);
620 be_node_set_reg_class(irn, 1, cls_frame);
621 be_node_set_reg_class(irn, 2, cls_data);
625 ir_node *be_new_FrameAddr(const arch_register_class_t *cls_frame, ir_graph *irg, ir_node *bl, ir_node *frame, entity *ent)
632 irn = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
633 a = init_node_attr(irn, 1);
636 be_node_set_reg_class(irn, 0, cls_frame);
637 be_node_set_reg_class(irn, OUT_POS(0), cls_frame);
639 return optimize_node(irn);
642 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)
645 ir_node **in = (ir_node **) alloca((n + 1) * sizeof(in[0]));
648 memcpy(&in[1], in_keep, n * sizeof(in[0]));
649 irn = new_ir_node(NULL, irg, bl, op_be_CopyKeep, mode, n + 1, in);
650 init_node_attr(irn, n + 1);
651 be_node_set_reg_class(irn, OUT_POS(0), cls);
652 be_node_set_reg_class(irn, 0, cls);
657 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)
662 return be_new_CopyKeep(cls, irg, bl, src, 1, in, mode);
665 ir_node *be_get_CopyKeep_op(const ir_node *cpy) {
666 return get_irn_n(cpy, be_pos_CopyKeep_op);
669 void be_set_CopyKeep_op(ir_node *cpy, ir_node *op) {
670 set_irn_n(cpy, be_pos_CopyKeep_op, op);
673 ir_node *be_new_Barrier(ir_graph *irg, ir_node *bl, int n, ir_node *in[])
677 irn = new_ir_node(NULL, irg, bl, op_be_Barrier, mode_T, n, in);
678 init_node_attr(irn, n);
682 int be_is_Spill (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Spill ; }
683 int be_is_Reload (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Reload ; }
684 int be_is_Copy (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Copy ; }
685 int be_is_CopyKeep (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_CopyKeep ; }
686 int be_is_Perm (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Perm ; }
687 int be_is_MemPerm (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_MemPerm ; }
688 int be_is_Keep (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Keep ; }
689 int be_is_Call (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Call ; }
690 int be_is_Return (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Return ; }
691 int be_is_IncSP (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_IncSP ; }
692 int be_is_SetSP (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_SetSP ; }
693 int be_is_AddSP (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_AddSP ; }
694 int be_is_RegParams (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_RegParams ; }
695 int be_is_StackParam (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_StackParam ; }
696 int be_is_FrameAddr (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameAddr ; }
697 int be_is_FrameLoad (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameLoad ; }
698 int be_is_FrameStore (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameStore ; }
699 int be_is_Barrier (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Barrier ; }
701 int be_has_frame_entity(const ir_node *irn)
703 switch(be_get_irn_opcode(irn)) {
716 entity *be_get_frame_entity(const ir_node *irn)
718 if(be_has_frame_entity(irn)) {
719 be_frame_attr_t *a = get_irn_attr(irn);
725 void be_set_frame_entity(const ir_node *irn, entity* ent)
729 assert(be_has_frame_entity(irn));
731 a = get_irn_attr(irn);
735 void be_set_MemPerm_in_entity(const ir_node *irn, int n, entity *ent)
737 be_memperm_attr_t *attr = get_irn_attr(irn);
739 assert(be_is_MemPerm(irn));
740 assert(n < get_irn_arity(irn));
742 attr->in_entities[n] = ent;
745 entity* be_get_MemPerm_in_entity(const ir_node* irn, int n)
747 be_memperm_attr_t *attr = get_irn_attr(irn);
749 assert(be_is_MemPerm(irn));
750 assert(n < get_irn_arity(irn));
752 return attr->in_entities[n];
755 void be_set_MemPerm_out_entity(const ir_node *irn, int n, entity *ent)
757 be_memperm_attr_t *attr = get_irn_attr(irn);
759 assert(be_is_MemPerm(irn));
760 assert(n < get_irn_arity(irn));
762 attr->out_entities[n] = ent;
765 entity* be_get_MemPerm_out_entity(const ir_node* irn, int n)
767 be_memperm_attr_t *attr = get_irn_attr(irn);
769 assert(be_is_MemPerm(irn));
770 assert(n < get_irn_arity(irn));
772 return attr->out_entities[n];
775 static void be_limited(void *data, bitset_t *bs)
777 be_req_t *req = data;
780 case be_req_kind_negate_old_limited:
781 case be_req_kind_old_limited:
782 req->x.old_limited.old_limited(req->x.old_limited.old_limited_env, bs);
783 if(req->kind == be_req_kind_negate_old_limited)
786 case be_req_kind_single_reg:
787 bitset_clear_all(bs);
788 bitset_set(bs, req->x.single_reg->index);
793 static INLINE be_req_t *get_req(ir_node *irn, int pos)
795 int idx = pos < 0 ? -(pos + 1) : pos;
796 be_node_attr_t *a = get_irn_attr(irn);
797 be_reg_data_t *rd = &a->reg_data[idx];
798 be_req_t *r = pos < 0 ? &rd->req : &rd->in_req;
800 assert(is_be_node(irn));
801 assert(!(pos >= 0) || pos < get_irn_arity(irn));
802 assert(!(pos < 0) || -(pos + 1) <= a->max_reg_data);
807 void be_set_constr_single_reg(ir_node *irn, int pos, const arch_register_t *reg)
809 be_req_t *r = get_req(irn, pos);
811 r->kind = be_req_kind_single_reg;
812 r->x.single_reg = reg;
813 r->req.limited = be_limited;
814 r->req.limited_env = r;
815 r->req.type = arch_register_req_type_limited;
816 r->req.cls = reg->reg_class;
819 void be_set_constr_limited(ir_node *irn, int pos, const arch_register_req_t *req)
821 be_req_t *r = get_req(irn, pos);
823 assert(arch_register_req_is(req, limited));
825 r->kind = be_req_kind_old_limited;
826 r->req.limited = be_limited;
827 r->req.limited_env = r;
828 r->req.type = arch_register_req_type_limited;
829 r->req.cls = req->cls;
831 r->x.old_limited.old_limited = req->limited;
832 r->x.old_limited.old_limited_env = req->limited_env;
835 void be_node_set_flags(ir_node *irn, int pos, arch_irn_flags_t flags)
837 be_req_t *r = get_req(irn, pos);
841 void be_node_set_reg_class(ir_node *irn, int pos, const arch_register_class_t *cls)
843 be_req_t *r = get_req(irn, pos);
845 if(r->req.type == arch_register_req_type_none)
846 r->req.type = arch_register_req_type_normal;
849 void be_node_set_req_type(ir_node *irn, int pos, arch_register_req_type_t type)
851 be_req_t *r = get_req(irn, pos);
855 ir_node *be_get_IncSP_pred(ir_node *irn) {
856 assert(be_is_IncSP(irn));
857 return get_irn_n(irn, 0);
860 void be_set_IncSP_pred(ir_node *incsp, ir_node *pred) {
861 assert(be_is_IncSP(incsp));
862 set_irn_n(incsp, 0, pred);
865 ir_node *be_get_IncSP_mem(ir_node *irn) {
866 assert(be_is_IncSP(irn));
867 return get_irn_n(irn, 1);
870 void be_set_IncSP_offset(ir_node *irn, unsigned offset)
872 be_stack_attr_t *a = get_irn_attr(irn);
873 assert(be_is_IncSP(irn));
877 unsigned be_get_IncSP_offset(const ir_node *irn)
879 be_stack_attr_t *a = get_irn_attr(irn);
880 assert(be_is_IncSP(irn));
884 void be_set_IncSP_direction(ir_node *irn, be_stack_dir_t dir)
886 be_stack_attr_t *a = get_irn_attr(irn);
887 assert(be_is_IncSP(irn));
891 be_stack_dir_t be_get_IncSP_direction(const ir_node *irn)
893 be_stack_attr_t *a = get_irn_attr(irn);
894 assert(be_is_IncSP(irn));
898 ir_node *be_spill(const arch_env_t *arch_env, ir_node *irn)
900 ir_node *bl = get_nodes_block(irn);
901 ir_graph *irg = get_irn_irg(bl);
902 ir_node *frame = get_irg_frame(irg);
905 const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, irn, -1);
906 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
908 spill = be_new_Spill(cls, cls_frame, irg, bl, frame, irn);
912 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)
916 ir_node *bl = is_Block(insert) ? insert : get_nodes_block(insert);
917 ir_graph *irg = get_irn_irg(bl);
918 ir_node *frame = get_irg_frame(irg);
919 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
921 assert(be_is_Spill(spill) || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
923 reload = be_new_Reload(cls, cls_frame, irg, bl, frame, spill, mode);
925 if(is_Block(insert)) {
926 insert = sched_skip(insert, 0, sched_skip_cf_predicator, (void *) arch_env);
927 sched_add_after(insert, reload);
931 sched_add_before(insert, reload);
938 | _ \ ___ __ _ | _ \ ___ __ _ ___
939 | |_) / _ \/ _` | | |_) / _ \/ _` / __|
940 | _ < __/ (_| | | _ < __/ (_| \__ \
941 |_| \_\___|\__, | |_| \_\___|\__, |___/
947 static void *put_out_reg_req(arch_register_req_t *req, const ir_node *irn, int out_pos)
949 const be_node_attr_t *a = get_irn_attr(irn);
951 if(out_pos < a->max_reg_data) {
952 memcpy(req, &a->reg_data[out_pos].req, sizeof(req[0]));
954 if(be_is_Copy(irn)) {
955 req->type |= arch_register_req_type_should_be_same;
956 req->other_same = be_get_Copy_op(irn);
960 req->type = arch_register_req_type_none;
967 static void *put_in_reg_req(arch_register_req_t *req, const ir_node *irn, int pos)
969 const be_node_attr_t *a = get_irn_attr(irn);
971 if(pos < get_irn_arity(irn) && pos < a->max_reg_data)
972 memcpy(req, &a->reg_data[pos].in_req, sizeof(req[0]));
974 req->type = arch_register_req_type_none;
981 static const arch_register_req_t *
982 be_node_get_irn_reg_req(const void *self, arch_register_req_t *req, const ir_node *irn, int pos)
987 if(get_irn_mode(irn) == mode_T)
990 out_pos = redir_proj((const ir_node **) &irn, pos);
991 assert(is_be_node(irn));
992 return put_out_reg_req(req, irn, out_pos);
996 return is_be_node(irn) ? put_in_reg_req(req, irn, pos) : NULL;
1002 const arch_register_t *
1003 be_node_get_irn_reg(const void *_self, const ir_node *irn)
1008 out_pos = redir_proj((const ir_node **) &irn, -1);
1009 a = get_irn_attr(irn);
1011 assert(is_be_node(irn));
1012 assert(out_pos < a->max_reg_data && "position too high");
1014 return a->reg_data[out_pos].reg;
1017 static arch_irn_class_t be_node_classify(const void *_self, const ir_node *irn)
1019 redir_proj((const ir_node **) &irn, -1);
1021 switch(be_get_irn_opcode(irn)) {
1022 #define XXX(a,b) case beo_ ## a: return arch_irn_class_ ## b
1024 XXX(Reload, reload);
1027 XXX(Return, branch);
1028 XXX(StackParam, stackparam);
1037 static arch_irn_flags_t be_node_get_flags(const void *_self, const ir_node *irn)
1042 out_pos = redir_proj((const ir_node **) &irn, -1);
1043 a = get_irn_attr(irn);
1045 assert(is_be_node(irn));
1046 assert(out_pos < a->max_reg_data && "position too high");
1048 return a->reg_data[out_pos].req.flags;
1051 static entity *be_node_get_frame_entity(const void *self, const ir_node *irn)
1053 return be_get_frame_entity(irn);
1056 static void be_node_set_frame_offset(const void *self, ir_node *irn, int offset)
1058 if(be_has_frame_entity(irn)) {
1059 be_frame_attr_t *a = get_irn_attr(irn);
1065 ___ ____ _ _ _ _ _ _
1066 |_ _| _ \| \ | | | | | | __ _ _ __ __| | | ___ _ __
1067 | || |_) | \| | | |_| |/ _` | '_ \ / _` | |/ _ \ '__|
1068 | || _ <| |\ | | _ | (_| | | | | (_| | | __/ |
1069 |___|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
1073 static const arch_irn_ops_if_t be_node_irn_ops_if = {
1074 be_node_get_irn_reg_req,
1075 be_node_set_irn_reg,
1076 be_node_get_irn_reg,
1079 be_node_get_frame_entity,
1080 be_node_set_frame_offset
1083 static const arch_irn_ops_t be_node_irn_ops = {
1087 const void *be_node_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn)
1089 redir_proj((const ir_node **) &irn, -1);
1090 return is_be_node(irn) ? &be_node_irn_ops : NULL;
1093 const arch_irn_handler_t be_node_irn_handler = {
1098 ____ _ _ ___ ____ _ _ _ _ _ _
1099 | _ \| |__ (_) |_ _| _ \| \ | | | | | | __ _ _ __ __| | | ___ _ __
1100 | |_) | '_ \| | | || |_) | \| | | |_| |/ _` | '_ \ / _` | |/ _ \ '__|
1101 | __/| | | | | | || _ <| |\ | | _ | (_| | | | | (_| | | __/ |
1102 |_| |_| |_|_| |___|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
1107 arch_irn_handler_t irn_handler;
1108 arch_irn_ops_t irn_ops;
1109 const arch_env_t *arch_env;
1113 #define get_phi_handler_from_handler(h) container_of(h, phi_handler_t, irn_handler)
1114 #define get_phi_handler_from_ops(h) container_of(h, phi_handler_t, irn_ops)
1116 static const void *phi_get_irn_ops(const arch_irn_handler_t *handler, const ir_node *irn)
1118 const phi_handler_t *h = get_phi_handler_from_handler(handler);
1119 return is_Phi(irn) && mode_is_datab(get_irn_mode(irn)) ? &h->irn_ops : NULL;
1123 * Get register class of a Phi.
1126 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)
1128 int n = get_irn_arity(phi);
1132 if(*visited && pset_find_ptr(*visited, phi))
1135 for(i = 0; i < n; ++i) {
1136 op = get_irn_n(phi, i);
1138 return arch_get_register_req(h->arch_env, req, op, BE_OUT_POS(0));
1142 The operands of that Phi were all Phis themselves.
1143 We have to start a DFS for a non-Phi argument now.
1146 *visited = pset_new_ptr(16);
1148 pset_insert_ptr(*visited, phi);
1150 for(i = 0; i < n; ++i) {
1151 op = get_irn_n(phi, i);
1152 if(get_Phi_reg_req_recursive(h, req, op, visited))
1159 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)
1161 phi_handler_t *phi_handler = get_phi_handler_from_ops(self);
1162 pset *visited = NULL;
1164 get_Phi_reg_req_recursive(phi_handler, req, irn, &visited);
1165 /* Set the requirements type to normal, since an operand of the Phi could have had constraints. */
1166 req->type = arch_register_req_type_normal;
1173 static void phi_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg)
1175 phi_handler_t *h = get_phi_handler_from_ops(self);
1176 pmap_insert(h->regs, irn, (void *) reg);
1179 static const arch_register_t *phi_get_irn_reg(const void *self, const ir_node *irn)
1181 phi_handler_t *h = get_phi_handler_from_ops(self);
1182 return pmap_get(h->regs, (void *) irn);
1185 static arch_irn_class_t phi_classify(const void *_self, const ir_node *irn)
1187 return arch_irn_class_normal;
1190 static arch_irn_flags_t phi_get_flags(const void *_self, const ir_node *irn)
1192 return arch_irn_flags_none;
1195 static entity *phi_get_frame_entity(const void *_self, const ir_node *irn)
1200 static void phi_set_frame_offset(const void *_self, ir_node *irn, int bias)
1204 static const arch_irn_ops_if_t phi_irn_ops = {
1205 phi_get_irn_reg_req,
1210 phi_get_frame_entity,
1211 phi_set_frame_offset
1214 static const arch_irn_handler_t phi_irn_handler = {
1218 arch_irn_handler_t *be_phi_handler_new(const arch_env_t *arch_env)
1220 phi_handler_t *h = xmalloc(sizeof(h[0]));
1221 h->irn_handler.get_irn_ops = phi_get_irn_ops;
1222 h->irn_ops.impl = &phi_irn_ops;
1223 h->arch_env = arch_env;
1224 h->regs = pmap_create();
1225 return (arch_irn_handler_t *) h;
1228 void be_phi_handler_free(arch_irn_handler_t *handler)
1230 phi_handler_t *h = (void *) handler;
1231 pmap_destroy(h->regs);
1235 const void *be_phi_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn)
1237 phi_handler_t *phi_handler = get_phi_handler_from_handler(self);
1238 return is_Phi(irn) ? &phi_handler->irn_ops : NULL;
1241 void be_phi_handler_reset(arch_irn_handler_t *handler)
1243 phi_handler_t *h = get_phi_handler_from_handler(handler);
1245 pmap_destroy(h->regs);
1246 h->regs = pmap_create();
1251 | \ | | ___ __| | ___ | _ \ _ _ _ __ ___ _ __ (_)_ __ __ _
1252 | \| |/ _ \ / _` |/ _ \ | | | | | | | '_ ` _ \| '_ \| | '_ \ / _` |
1253 | |\ | (_) | (_| | __/ | |_| | |_| | | | | | | |_) | | | | | (_| |
1254 |_| \_|\___/ \__,_|\___| |____/ \__,_|_| |_| |_| .__/|_|_| |_|\__, |
1259 * Dumps a register requirement to a file.
1261 static void dump_node_req(FILE *f, int idx, be_req_t *req)
1264 int did_something = 0;
1266 const char *prefix = buf;
1268 snprintf(buf, sizeof(buf), "#%d ", idx);
1269 buf[sizeof(buf) - 1] = '\0';
1271 if(req->flags != arch_irn_flags_none) {
1272 fprintf(f, "%sflags: ", prefix);
1274 for(i = arch_irn_flags_none; i <= log2_ceil(arch_irn_flags_last); ++i) {
1275 if(req->flags & (1 << i)) {
1276 fprintf(f, "%s%s", prefix, arch_irn_flag_str(1 << i));
1284 if(req->req.cls != 0) {
1287 arch_register_req_format(tmp, sizeof(tmp), &req->req);
1288 fprintf(f, "%s", tmp);
1297 * Dumps node register requirements to a file.
1299 static void dump_node_reqs(FILE *f, ir_node *irn)
1302 be_node_attr_t *a = get_irn_attr(irn);
1304 fprintf(f, "registers: \n");
1305 for(i = 0; i < a->max_reg_data; ++i) {
1306 be_reg_data_t *rd = &a->reg_data[i];
1308 fprintf(f, "#%d: %s\n", i, rd->reg->name);
1311 fprintf(f, "in requirements\n");
1312 for(i = 0; i < a->max_reg_data; ++i) {
1313 dump_node_req(f, i, &a->reg_data[i].in_req);
1316 fprintf(f, "\nout requirements\n");
1317 for(i = 0; i < a->max_reg_data; ++i) {
1318 dump_node_req(f, i, &a->reg_data[i].req);
1323 * ir_op-Operation: dump a be node to file
1325 static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason)
1327 be_node_attr_t *at = get_irn_attr(irn);
1329 assert(is_be_node(irn));
1332 case dump_node_opcode_txt:
1333 fprintf(f, get_op_name(get_irn_op(irn)));
1335 case dump_node_mode_txt:
1336 fprintf(f, get_mode_name(get_irn_mode(irn)));
1338 case dump_node_nodeattr_txt:
1340 case dump_node_info_txt:
1341 dump_node_reqs(f, irn);
1343 if(be_has_frame_entity(irn)) {
1344 be_frame_attr_t *a = (be_frame_attr_t *) at;
1346 int bits = get_type_size_bits(get_entity_type(a->ent));
1347 ir_fprintf(f, "frame entity: %+F, offset 0x%x (%d), size 0x%x (%d) bits\n",
1348 a->ent, a->offset, a->offset, bits, bits);
1353 switch(be_get_irn_opcode(irn)) {
1356 be_stack_attr_t *a = (be_stack_attr_t *) at;
1357 if (a->offset == BE_STACK_FRAME_SIZE)
1358 fprintf(f, "offset: FRAME_SIZE\n");
1360 fprintf(f, "offset: %u\n", a->offset);
1361 fprintf(f, "direction: %s\n", a->dir == be_stack_dir_expand ? "expand" : "shrink");
1366 be_call_attr_t *a = (be_call_attr_t *) at;
1369 fprintf(f, "\ncalling: %s\n", get_entity_name(a->ent));
1375 for(i = 0; i < get_irn_arity(irn); ++i) {
1377 in = be_get_MemPerm_in_entity(irn, i);
1378 out = be_get_MemPerm_out_entity(irn, i);
1380 fprintf(f, "\nin[%d]: %s\n", i, get_entity_name(in));
1383 fprintf(f, "\nout[%d]: %s\n", i, get_entity_name(out));
1399 * Copies the backend specific attributes from old node to new node.
1401 static void copy_attr(const ir_node *old_node, ir_node *new_node)
1403 be_node_attr_t *old_attr = get_irn_attr(old_node);
1404 be_node_attr_t *new_attr = get_irn_attr(new_node);
1407 assert(is_be_node(old_node));
1408 assert(is_be_node(new_node));
1410 memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
1411 new_attr->reg_data = NULL;
1413 if(new_attr->max_reg_data > 0) {
1414 new_attr->reg_data = NEW_ARR_D(be_reg_data_t, get_irg_obstack(get_irn_irg(new_node)), new_attr->max_reg_data);
1415 memcpy(new_attr->reg_data, old_attr->reg_data, new_attr->max_reg_data * sizeof(be_reg_data_t));
1417 for(i = 0; i < old_attr->max_reg_data; ++i) {
1420 r = &new_attr->reg_data[i].req;
1421 r->req.limited_env = r;
1423 r = &new_attr->reg_data[i].in_req;
1424 r->req.limited_env = r;
1429 static const ir_op_ops be_node_op_ops = {