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 typedef enum _node_kind_t {
63 be_req_kind_old_limited,
64 be_req_kind_negate_old_limited,
65 be_req_kind_single_reg
69 arch_register_req_t req;
71 arch_irn_flags_t flags;
74 void (*old_limited)(void *ptr, bitset_t *bs);
75 void *old_limited_env;
78 const arch_register_t *single_reg;
83 const arch_register_t *reg;
88 /** The generic be nodes attribute type. */
91 be_reg_data_t *reg_data;
94 /** The be_Return nodes attribute type. */
96 be_node_attr_t node_attr;
97 int num_ret_vals; /**< number of return values */
100 /** The be_Stack attribute type. */
102 be_node_attr_t node_attr;
103 int offset; /**< The offset by which the stack shall be increased/decreased. */
104 be_stack_dir_t dir; /**< The direction in which the stack shall be modified (expand or shrink). */
107 /** The be_Frame attribute type. */
109 be_node_attr_t node_attr;
114 /** The be_Call attribute type. */
116 be_node_attr_t node_attr;
117 entity *ent; /**< The called entity if this is a static call. */
118 ir_type *call_tp; /**< The call type, copied from the original Call node. */
121 /** The be_Spill attribute type. */
123 be_frame_attr_t frame_attr;
124 ir_node *spill_ctx; /**< The node in whose context this spill was introduced. */
132 ir_op *op_be_CopyKeep;
138 ir_op *op_be_RegParams;
139 ir_op *op_be_StackParam;
140 ir_op *op_be_FrameAddr;
141 ir_op *op_be_FrameLoad;
142 ir_op *op_be_FrameStore;
143 ir_op *op_be_Barrier;
145 static int beo_base = -1;
147 static const ir_op_ops be_node_op_ops;
149 #define N irop_flag_none
150 #define L irop_flag_labeled
151 #define C irop_flag_commutative
152 #define X irop_flag_cfopcode
153 #define I irop_flag_ip_cfopcode
154 #define F irop_flag_fragile
155 #define Y irop_flag_forking
156 #define H irop_flag_highlevel
157 #define c irop_flag_constlike
158 #define K irop_flag_keep
159 #define M irop_flag_machine
163 * Compare two node attributes.
165 * @return zero if both attributes are identically
167 static int cmp_node_attr(be_node_attr_t *a, be_node_attr_t *b) {
168 if (a->max_reg_data == b->max_reg_data) {
171 for (i = 0; i < a->max_reg_data; ++i) {
172 if (a->reg_data[i].reg != b->reg_data[i].reg ||
173 memcmp(&a->reg_data[i].in_req, &b->reg_data[i].in_req, sizeof(b->reg_data[i].in_req)) ||
174 memcmp(&a->reg_data[i].req, &b->reg_data[i].req, sizeof(a->reg_data[i].req)))
183 * Compare the attributes of two FrameAddr nodes.
185 * @return zero if both attributes are identically
187 static int FrameAddr_cmp_attr(ir_node *a, ir_node *b) {
188 be_frame_attr_t *a_attr = get_irn_attr(a);
189 be_frame_attr_t *b_attr = get_irn_attr(b);
191 if (a_attr->ent == b_attr->ent && a_attr->offset == b_attr->offset)
192 return cmp_node_attr(&a_attr->node_attr, &b_attr->node_attr);
196 void be_node_init(void) {
197 static int inited = 0;
204 /* Acquire all needed opcodes. */
205 beo_base = get_next_ir_opcodes(beo_Last - 1);
207 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);
208 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);
209 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);
210 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);
211 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);
212 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);
213 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);
214 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);
215 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);
216 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);
217 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);
218 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);
219 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);
220 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);
221 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);
222 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);
223 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);
225 set_op_tag(op_be_Spill, &be_node_tag);
226 set_op_tag(op_be_Reload, &be_node_tag);
227 set_op_tag(op_be_Perm, &be_node_tag);
228 set_op_tag(op_be_Copy, &be_node_tag);
229 set_op_tag(op_be_Keep, &be_node_tag);
230 set_op_tag(op_be_CopyKeep, &be_node_tag);
231 set_op_tag(op_be_Call, &be_node_tag);
232 set_op_tag(op_be_Return, &be_node_tag);
233 set_op_tag(op_be_AddSP, &be_node_tag);
234 set_op_tag(op_be_SetSP, &be_node_tag);
235 set_op_tag(op_be_IncSP, &be_node_tag);
236 set_op_tag(op_be_RegParams, &be_node_tag);
237 set_op_tag(op_be_StackParam, &be_node_tag);
238 set_op_tag(op_be_FrameLoad, &be_node_tag);
239 set_op_tag(op_be_FrameStore, &be_node_tag);
240 set_op_tag(op_be_FrameAddr, &be_node_tag);
241 set_op_tag(op_be_Barrier, &be_node_tag);
243 op_be_FrameAddr->ops.node_cmp_attr = FrameAddr_cmp_attr;
247 * Initializes the generic attribute of all be nodes and return ir.
249 static void *init_node_attr(ir_node* irn, int max_reg_data)
251 ir_graph *irg = get_irn_irg(irn);
252 be_node_attr_t *a = get_irn_attr(irn);
254 memset(a, 0, sizeof(get_op_attr_size(get_irn_op(irn))));
255 a->max_reg_data = max_reg_data;
258 if(max_reg_data > 0) {
261 a->reg_data = NEW_ARR_D(be_reg_data_t, get_irg_obstack(irg), max_reg_data);
262 memset(a->reg_data, 0, max_reg_data * sizeof(a->reg_data[0]));
263 for(i = 0; i < max_reg_data; ++i) {
264 a->reg_data[i].req.req.cls = NULL;
265 a->reg_data[i].req.req.type = arch_register_req_type_none;
272 int is_be_node(const ir_node *irn)
274 return get_op_tag(get_irn_op(irn)) == &be_node_tag;
277 be_opcode_t be_get_irn_opcode(const ir_node *irn)
279 return is_be_node(irn) ? get_irn_opcode(irn) - beo_base : beo_NoBeOp;
282 static int redir_proj(const ir_node **node, int pos)
284 const ir_node *n = *node;
289 assert(pos == -1 && "Illegal pos for a Proj");
290 *node = irn = get_Proj_pred(n);
292 assert(get_irn_mode(irn) == mode_T);
293 *node = get_Proj_pred(irn);
295 return get_Proj_proj(n);
302 be_node_set_irn_reg(const void *_self, ir_node *irn, const arch_register_t *reg)
307 out_pos = redir_proj((const ir_node **) &irn, -1);
308 a = get_irn_attr(irn);
310 assert(is_be_node(irn));
311 assert(out_pos < a->max_reg_data && "position too high");
312 a->reg_data[out_pos].reg = reg;
316 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)
324 res = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
325 a = init_node_attr(res, 2);
326 a->frame_attr.ent = NULL;
327 a->frame_attr.offset = 0;
330 be_node_set_reg_class(res, 0, cls_frame);
331 be_node_set_reg_class(res, 1, cls);
335 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)
342 res = new_ir_node(NULL, irg, bl, op_be_Reload, mode, 2, in);
343 init_node_attr(res, 2);
344 be_node_set_reg_class(res, 0, cls_frame);
345 be_node_set_reg_class(res, -1, cls);
349 ir_node *be_get_Reload_mem(const ir_node *irn)
351 assert(be_is_Reload(irn));
352 return get_irn_n(irn, be_pos_Reload_mem);
355 ir_node *be_get_Reload_frame(const ir_node *irn)
357 assert(be_is_Reload(irn));
358 return get_irn_n(irn, be_pos_Reload_frame);
361 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
364 ir_node *irn = new_ir_node(NULL, irg, bl, op_be_Perm, mode_T, n, in);
365 init_node_attr(irn, n);
366 for(i = 0; i < n; ++i) {
367 be_node_set_reg_class(irn, i, cls);
368 be_node_set_reg_class(irn, OUT_POS(i), cls);
374 ir_node *be_new_Copy(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *op)
380 res = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
381 init_node_attr(res, 1);
382 be_node_set_reg_class(res, 0, cls);
383 be_node_set_reg_class(res, OUT_POS(0), cls);
387 ir_node *be_get_Copy_op(const ir_node *cpy) {
388 return get_irn_n(cpy, be_pos_Copy_op);
391 void be_set_Copy_op(ir_node *cpy, ir_node *op) {
392 set_irn_n(cpy, be_pos_Copy_op, op);
395 ir_node *be_new_Keep(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
400 irn = new_ir_node(NULL, irg, bl, op_be_Keep, mode_ANY, n, in);
401 init_node_attr(irn, n);
402 for(i = 0; i < n; ++i) {
403 be_node_set_reg_class(irn, i, cls);
409 ir_node *be_new_Call(dbg_info *dbg, ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *sp, ir_node *ptr,
410 int n_outs, int n, ir_node *in[], ir_type *call_tp)
413 int real_n = be_pos_Call_first_arg + n;
417 NEW_ARR_A(ir_node *, real_in, real_n);
418 real_in[be_pos_Call_mem] = mem;
419 real_in[be_pos_Call_sp] = sp;
420 real_in[be_pos_Call_ptr] = ptr;
421 memcpy(&real_in[be_pos_Call_first_arg], in, n * sizeof(in[0]));
423 irn = new_ir_node(dbg, irg, bl, op_be_Call, mode_T, real_n, real_in);
424 a = init_node_attr(irn, (n_outs > real_n ? n_outs : real_n));
426 a->call_tp = call_tp;
430 /* Gets the call entity or NULL if this is no static call. */
431 entity *be_Call_get_entity(const ir_node *call) {
432 be_call_attr_t *a = get_irn_attr(call);
433 assert(be_is_Call(call));
437 /* Sets the call entity. */
438 void be_Call_set_entity(ir_node *call, entity *ent) {
439 be_call_attr_t *a = get_irn_attr(call);
440 assert(be_is_Call(call));
444 /* Gets the call type. */
445 ir_type *be_Call_get_type(ir_node *call) {
446 be_call_attr_t *a = get_irn_attr(call);
447 assert(be_is_Call(call));
451 /* Sets the call type. */
452 void be_Call_set_type(ir_node *call, ir_type *call_tp) {
453 be_call_attr_t *a = get_irn_attr(call);
454 assert(be_is_Call(call));
455 a->call_tp = call_tp;
458 /* Construct a new be_Return. */
459 ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *bl, int n_res, int n, ir_node *in[])
462 ir_node *irn = new_ir_node(dbg, irg, bl, op_be_Return, mode_X, n, in);
463 init_node_attr(irn, n);
464 a = get_irn_attr(irn);
465 a->num_ret_vals = n_res;
470 /* Returns the number of real returns values */
471 int be_Return_get_n_rets(ir_node *ret)
473 be_return_attr_t *a = get_irn_attr(ret);
474 return a->num_ret_vals;
477 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)
485 irn = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode, 2, in);
486 a = init_node_attr(irn, 1);
490 be_node_set_flags(irn, -1, arch_irn_flags_ignore | arch_irn_flags_modify_sp);
492 /* Set output constraint to stack register. */
493 be_node_set_reg_class(irn, 0, sp->reg_class);
494 be_set_constr_single_reg(irn, -1, sp);
495 be_node_set_irn_reg(NULL, irn, sp);
500 ir_node *be_new_AddSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, ir_node *sz)
504 ir_node *in[be_pos_AddSP_last];
506 in[be_pos_AddSP_old_sp] = old_sp;
507 in[be_pos_AddSP_size] = sz;
509 irn = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T, be_pos_AddSP_last, in);
510 a = init_node_attr(irn, be_pos_AddSP_last);
512 be_node_set_flags(irn, OUT_POS(0), arch_irn_flags_ignore | arch_irn_flags_modify_sp);
514 /* Set output constraint to stack register. */
515 be_set_constr_single_reg(irn, OUT_POS(0), sp);
520 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)
529 irn = new_ir_node(NULL, irg, bl, op_be_SetSP, get_irn_mode(old_sp), 3, in);
530 a = init_node_attr(irn, 3);
532 be_node_set_flags(irn, OUT_POS(0), arch_irn_flags_ignore | arch_irn_flags_modify_sp);
534 /* Set output constraint to stack register. */
535 be_set_constr_single_reg(irn, OUT_POS(0), sp);
536 be_node_set_reg_class(irn, 1, sp->reg_class);
537 be_node_set_reg_class(irn, 2, sp->reg_class);
538 be_node_set_irn_reg(NULL, irn, sp);
543 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)
549 in[0] = frame_pointer;
550 irn = new_ir_node(NULL, irg, bl, op_be_StackParam, mode, 1, in);
551 a = init_node_attr(irn, 1);
554 be_node_set_reg_class(irn, 0, cls_frame);
555 be_node_set_reg_class(irn, OUT_POS(0), cls);
559 ir_node *be_new_RegParams(ir_graph *irg, ir_node *bl, int n_outs)
564 irn = new_ir_node(NULL, irg, bl, op_be_RegParams, mode_T, 0, in);
565 init_node_attr(irn, n_outs);
569 ir_node *be_new_FrameLoad(const arch_register_class_t *cls_frame, const arch_register_class_t *cls_data,
570 ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *frame, entity *ent)
578 irn = new_ir_node(NULL, irg, bl, op_be_FrameLoad, mode_T, 2, in);
579 a = init_node_attr(irn, 3);
582 be_node_set_reg_class(irn, 1, cls_frame);
583 be_node_set_reg_class(irn, OUT_POS(pn_Load_res), cls_data);
587 ir_node *be_new_FrameStore(const arch_register_class_t *cls_frame, const arch_register_class_t *cls_data,
588 ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *frame, ir_node *data, entity *ent)
597 irn = new_ir_node(NULL, irg, bl, op_be_FrameStore, mode_T, 3, 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, 2, cls_data);
606 ir_node *be_new_FrameAddr(const arch_register_class_t *cls_frame, ir_graph *irg, ir_node *bl, ir_node *frame, entity *ent)
613 irn = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
614 a = init_node_attr(irn, 1);
617 be_node_set_reg_class(irn, 0, cls_frame);
618 be_node_set_reg_class(irn, OUT_POS(0), cls_frame);
620 return optimize_node(irn);
623 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)
626 ir_node **in = (ir_node **) alloca((n + 1) * sizeof(in[0]));
629 memcpy(&in[1], in_keep, n * sizeof(in[0]));
630 irn = new_ir_node(NULL, irg, bl, op_be_CopyKeep, mode, n + 1, in);
631 init_node_attr(irn, n + 1);
632 be_node_set_reg_class(irn, OUT_POS(0), cls);
633 be_node_set_reg_class(irn, 0, cls);
638 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)
643 return be_new_CopyKeep(cls, irg, bl, src, 1, in, mode);
646 ir_node *be_get_CopyKeep_op(const ir_node *cpy) {
647 return get_irn_n(cpy, be_pos_CopyKeep_op);
650 void be_set_CopyKeep_op(ir_node *cpy, ir_node *op) {
651 set_irn_n(cpy, be_pos_CopyKeep_op, op);
654 ir_node *be_new_Barrier(ir_graph *irg, ir_node *bl, int n, ir_node *in[])
658 irn = new_ir_node(NULL, irg, bl, op_be_Barrier, mode_T, n, in);
659 init_node_attr(irn, n);
663 int be_is_Spill (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Spill ; }
664 int be_is_Reload (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Reload ; }
665 int be_is_Copy (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Copy ; }
666 int be_is_CopyKeep (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_CopyKeep ; }
667 int be_is_Perm (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Perm ; }
668 int be_is_Keep (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Keep ; }
669 int be_is_Call (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Call ; }
670 int be_is_Return (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Return ; }
671 int be_is_IncSP (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_IncSP ; }
672 int be_is_SetSP (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_SetSP ; }
673 int be_is_AddSP (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_AddSP ; }
674 int be_is_RegParams (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_RegParams ; }
675 int be_is_StackParam (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_StackParam ; }
676 int be_is_FrameAddr (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameAddr ; }
677 int be_is_FrameLoad (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameLoad ; }
678 int be_is_FrameStore (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameStore ; }
679 int be_is_Barrier (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Barrier ; }
681 int be_has_frame_entity(const ir_node *irn)
683 switch(be_get_irn_opcode(irn)) {
696 entity *be_get_frame_entity(const ir_node *irn)
698 if(be_has_frame_entity(irn)) {
699 be_frame_attr_t *a = get_irn_attr(irn);
705 static void be_limited(void *data, bitset_t *bs)
707 be_req_t *req = data;
710 case be_req_kind_negate_old_limited:
711 case be_req_kind_old_limited:
712 req->x.old_limited.old_limited(req->x.old_limited.old_limited_env, bs);
713 if(req->kind == be_req_kind_negate_old_limited)
716 case be_req_kind_single_reg:
717 bitset_clear_all(bs);
718 bitset_set(bs, req->x.single_reg->index);
723 static INLINE be_req_t *get_req(ir_node *irn, int pos)
725 int idx = pos < 0 ? -(pos + 1) : pos;
726 be_node_attr_t *a = get_irn_attr(irn);
727 be_reg_data_t *rd = &a->reg_data[idx];
728 be_req_t *r = pos < 0 ? &rd->req : &rd->in_req;
730 assert(is_be_node(irn));
731 assert(!(pos >= 0) || pos < get_irn_arity(irn));
732 assert(!(pos < 0) || -(pos + 1) <= a->max_reg_data);
737 void be_set_constr_single_reg(ir_node *irn, int pos, const arch_register_t *reg)
739 be_req_t *r = get_req(irn, pos);
741 r->kind = be_req_kind_single_reg;
742 r->x.single_reg = reg;
743 r->req.limited = be_limited;
744 r->req.limited_env = r;
745 r->req.type = arch_register_req_type_limited;
746 r->req.cls = reg->reg_class;
749 void be_set_constr_limited(ir_node *irn, int pos, const arch_register_req_t *req)
751 be_req_t *r = get_req(irn, pos);
753 assert(arch_register_req_is(req, limited));
755 r->kind = be_req_kind_old_limited;
756 r->req.limited = be_limited;
757 r->req.limited_env = r;
758 r->req.type = arch_register_req_type_limited;
759 r->req.cls = req->cls;
761 r->x.old_limited.old_limited = req->limited;
762 r->x.old_limited.old_limited_env = req->limited_env;
765 void be_node_set_flags(ir_node *irn, int pos, arch_irn_flags_t flags)
767 be_req_t *r = get_req(irn, pos);
771 void be_node_set_reg_class(ir_node *irn, int pos, const arch_register_class_t *cls)
773 be_req_t *r = get_req(irn, pos);
775 if(r->req.type == arch_register_req_type_none)
776 r->req.type = arch_register_req_type_normal;
779 void be_node_set_req_type(ir_node *irn, int pos, arch_register_req_type_t type)
781 be_req_t *r = get_req(irn, pos);
785 ir_node *be_get_IncSP_pred(ir_node *irn) {
786 assert(be_is_IncSP(irn));
787 return get_irn_n(irn, 0);
790 void be_set_IncSP_pred(ir_node *incsp, ir_node *pred) {
791 assert(be_is_IncSP(incsp));
792 set_irn_n(incsp, 0, pred);
795 ir_node *be_get_IncSP_mem(ir_node *irn) {
796 assert(be_is_IncSP(irn));
797 return get_irn_n(irn, 1);
800 void be_set_IncSP_offset(ir_node *irn, unsigned offset)
802 be_stack_attr_t *a = get_irn_attr(irn);
803 assert(be_is_IncSP(irn));
807 unsigned be_get_IncSP_offset(const ir_node *irn)
809 be_stack_attr_t *a = get_irn_attr(irn);
810 assert(be_is_IncSP(irn));
814 void be_set_IncSP_direction(ir_node *irn, be_stack_dir_t dir)
816 be_stack_attr_t *a = get_irn_attr(irn);
817 assert(be_is_IncSP(irn));
821 be_stack_dir_t be_get_IncSP_direction(const ir_node *irn)
823 be_stack_attr_t *a = get_irn_attr(irn);
824 assert(be_is_IncSP(irn));
828 void be_set_Spill_entity(ir_node *irn, entity *ent)
830 be_spill_attr_t *a = get_irn_attr(irn);
831 assert(be_is_Spill(irn));
832 a->frame_attr.ent = ent;
835 void be_set_Spill_context(ir_node *irn, ir_node *ctx)
837 be_spill_attr_t *a = get_irn_attr(irn);
838 assert(be_is_Spill(irn));
842 static ir_node *find_a_spill_walker(ir_node *irn, unsigned visited_nr)
844 unsigned nr = get_irn_visited(irn);
846 set_irn_visited(irn, visited_nr);
850 if(nr < visited_nr) {
851 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
852 ir_node *n = find_a_spill_walker(get_irn_n(irn, i), visited_nr);
859 else if(be_get_irn_opcode(irn) == beo_Spill)
865 ir_node *be_get_Spill_context(const ir_node *irn) {
866 const be_spill_attr_t *a = get_irn_attr(irn);
867 assert(be_is_Spill(irn));
872 * Finds a spill for a reload.
873 * If the reload is directly using the spill, this is simple,
874 * else we perform DFS from the reload (over all PhiMs) and return
875 * the first spill node we find.
877 static INLINE ir_node *find_a_spill(const ir_node *irn)
879 ir_graph *irg = get_irn_irg(irn);
880 unsigned visited_nr = get_irg_visited(irg) + 1;
882 assert(be_is_Reload(irn));
883 set_irg_visited(irg, visited_nr);
884 return find_a_spill_walker(be_get_Reload_mem(irn), visited_nr);
887 entity *be_get_spill_entity(const ir_node *irn)
889 switch(be_get_irn_opcode(irn)) {
892 ir_node *spill = find_a_spill(irn);
893 return be_get_spill_entity(spill);
897 be_spill_attr_t *a = get_irn_attr(irn);
898 return a->frame_attr.ent;
901 assert(0 && "Must give spill/reload node");
908 static void link_reload_walker(ir_node *irn, void *data)
910 ir_node **root = (ir_node **) data;
911 if(be_is_Reload(irn)) {
912 set_irn_link(irn, *root);
917 void be_copy_entities_to_reloads(ir_graph *irg)
920 irg_walk_graph(irg, link_reload_walker, NULL, (void *) &irn);
923 be_frame_attr_t *a = get_irn_attr(irn);
924 entity *ent = be_get_spill_entity(irn);
926 irn = get_irn_link(irn);
930 ir_node *be_spill(const arch_env_t *arch_env, ir_node *irn, ir_node *ctx)
932 ir_node *bl = get_nodes_block(irn);
933 ir_graph *irg = get_irn_irg(bl);
934 ir_node *frame = get_irg_frame(irg);
935 ir_node *insert = bl;
938 const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, irn, -1);
939 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
941 spill = be_new_Spill(cls, cls_frame, irg, bl, frame, irn, ctx);
946 * search the right insertion point. a spill of a phi cannot be put
947 * directly after the phi, if there are some phis behind the one which
948 * is spilled. Also, a spill of a Proj must be after all Projs of the
951 * Here's one special case:
952 * If the spill is in the start block, the spill must be after the frame
953 * pointer is set up. This is done by setting insert to the end of the block
954 * which is its default initialization (see above).
957 insert = sched_next(irn);
958 if(insert != bl && bl == get_irg_start_block(irg) && sched_get_time_step(frame) >= sched_get_time_step(insert))
959 insert = sched_next(frame);
961 while((is_Phi(insert) || is_Proj(insert)) && !sched_is_end(insert))
962 insert = sched_next(insert);
964 sched_add_before(insert, spill);
969 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)
973 ir_node *bl = is_Block(insert) ? insert : get_nodes_block(insert);
974 ir_graph *irg = get_irn_irg(bl);
975 ir_node *frame = get_irg_frame(irg);
976 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
978 assert(be_is_Spill(spill) || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
980 reload = be_new_Reload(cls, cls_frame, irg, bl, frame, spill, mode);
982 if(is_Block(insert)) {
983 insert = sched_skip(insert, 0, sched_skip_cf_predicator, (void *) arch_env);
984 sched_add_after(insert, reload);
988 sched_add_before(insert, reload);
995 | _ \ ___ __ _ | _ \ ___ __ _ ___
996 | |_) / _ \/ _` | | |_) / _ \/ _` / __|
997 | _ < __/ (_| | | _ < __/ (_| \__ \
998 |_| \_\___|\__, | |_| \_\___|\__, |___/
1004 static void *put_out_reg_req(arch_register_req_t *req, const ir_node *irn, int out_pos)
1006 const be_node_attr_t *a = get_irn_attr(irn);
1008 if(out_pos < a->max_reg_data) {
1009 memcpy(req, &a->reg_data[out_pos].req, sizeof(req[0]));
1011 if(be_is_Copy(irn)) {
1012 req->type |= arch_register_req_type_should_be_same;
1013 req->other_same = be_get_Copy_op(irn);
1017 req->type = arch_register_req_type_none;
1024 static void *put_in_reg_req(arch_register_req_t *req, const ir_node *irn, int pos)
1026 const be_node_attr_t *a = get_irn_attr(irn);
1028 if(pos < get_irn_arity(irn) && pos < a->max_reg_data)
1029 memcpy(req, &a->reg_data[pos].in_req, sizeof(req[0]));
1031 req->type = arch_register_req_type_none;
1038 static const arch_register_req_t *
1039 be_node_get_irn_reg_req(const void *self, arch_register_req_t *req, const ir_node *irn, int pos)
1044 if(get_irn_mode(irn) == mode_T)
1047 out_pos = redir_proj((const ir_node **) &irn, pos);
1048 assert(is_be_node(irn));
1049 return put_out_reg_req(req, irn, out_pos);
1053 return is_be_node(irn) ? put_in_reg_req(req, irn, pos) : NULL;
1059 const arch_register_t *
1060 be_node_get_irn_reg(const void *_self, const ir_node *irn)
1065 out_pos = redir_proj((const ir_node **) &irn, -1);
1066 a = get_irn_attr(irn);
1068 assert(is_be_node(irn));
1069 assert(out_pos < a->max_reg_data && "position too high");
1071 return a->reg_data[out_pos].reg;
1074 static arch_irn_class_t be_node_classify(const void *_self, const ir_node *irn)
1076 redir_proj((const ir_node **) &irn, -1);
1078 switch(be_get_irn_opcode(irn)) {
1079 #define XXX(a,b) case beo_ ## a: return arch_irn_class_ ## b
1081 XXX(Reload, reload);
1084 XXX(Return, branch);
1085 XXX(StackParam, stackparam);
1094 static arch_irn_flags_t be_node_get_flags(const void *_self, const ir_node *irn)
1099 out_pos = redir_proj((const ir_node **) &irn, -1);
1100 a = get_irn_attr(irn);
1102 assert(is_be_node(irn));
1103 assert(out_pos < a->max_reg_data && "position too high");
1105 return a->reg_data[out_pos].req.flags;
1108 static entity *be_node_get_frame_entity(const void *self, const ir_node *irn)
1110 return be_get_frame_entity(irn);
1113 static void be_node_set_frame_offset(const void *self, ir_node *irn, int offset)
1115 if(be_has_frame_entity(irn)) {
1116 be_frame_attr_t *a = get_irn_attr(irn);
1122 ___ ____ _ _ _ _ _ _
1123 |_ _| _ \| \ | | | | | | __ _ _ __ __| | | ___ _ __
1124 | || |_) | \| | | |_| |/ _` | '_ \ / _` | |/ _ \ '__|
1125 | || _ <| |\ | | _ | (_| | | | | (_| | | __/ |
1126 |___|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
1130 static const arch_irn_ops_if_t be_node_irn_ops_if = {
1131 be_node_get_irn_reg_req,
1132 be_node_set_irn_reg,
1133 be_node_get_irn_reg,
1136 be_node_get_frame_entity,
1137 be_node_set_frame_offset
1140 static const arch_irn_ops_t be_node_irn_ops = {
1144 const void *be_node_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn)
1146 redir_proj((const ir_node **) &irn, -1);
1147 return is_be_node(irn) ? &be_node_irn_ops : NULL;
1150 const arch_irn_handler_t be_node_irn_handler = {
1155 ____ _ _ ___ ____ _ _ _ _ _ _
1156 | _ \| |__ (_) |_ _| _ \| \ | | | | | | __ _ _ __ __| | | ___ _ __
1157 | |_) | '_ \| | | || |_) | \| | | |_| |/ _` | '_ \ / _` | |/ _ \ '__|
1158 | __/| | | | | | || _ <| |\ | | _ | (_| | | | | (_| | | __/ |
1159 |_| |_| |_|_| |___|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
1164 arch_irn_handler_t irn_handler;
1165 arch_irn_ops_t irn_ops;
1166 const arch_env_t *arch_env;
1170 #define get_phi_handler_from_handler(h) container_of(h, phi_handler_t, irn_handler)
1171 #define get_phi_handler_from_ops(h) container_of(h, phi_handler_t, irn_ops)
1173 static const void *phi_get_irn_ops(const arch_irn_handler_t *handler, const ir_node *irn)
1175 const phi_handler_t *h = get_phi_handler_from_handler(handler);
1176 return is_Phi(irn) && mode_is_datab(get_irn_mode(irn)) ? &h->irn_ops : NULL;
1180 * Get register class of a Phi.
1183 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)
1185 int n = get_irn_arity(phi);
1189 if(*visited && pset_find_ptr(*visited, phi))
1192 for(i = 0; i < n; ++i) {
1193 op = get_irn_n(phi, i);
1195 return arch_get_register_req(h->arch_env, req, op, BE_OUT_POS(0));
1199 The operands of that Phi were all Phis themselves.
1200 We have to start a DFS for a non-Phi argument now.
1203 *visited = pset_new_ptr(16);
1205 pset_insert_ptr(*visited, phi);
1207 for(i = 0; i < n; ++i) {
1208 op = get_irn_n(phi, i);
1209 if(get_Phi_reg_req_recursive(h, req, op, visited))
1216 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)
1218 phi_handler_t *phi_handler = get_phi_handler_from_ops(self);
1219 pset *visited = NULL;
1221 get_Phi_reg_req_recursive(phi_handler, req, irn, &visited);
1222 /* Set the requirements type to normal, since an operand of the Phi could have had constraints. */
1223 req->type = arch_register_req_type_normal;
1230 static void phi_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg)
1232 phi_handler_t *h = get_phi_handler_from_ops(self);
1233 pmap_insert(h->regs, irn, (void *) reg);
1236 static const arch_register_t *phi_get_irn_reg(const void *self, const ir_node *irn)
1238 phi_handler_t *h = get_phi_handler_from_ops(self);
1239 return pmap_get(h->regs, (void *) irn);
1242 static arch_irn_class_t phi_classify(const void *_self, const ir_node *irn)
1244 return arch_irn_class_normal;
1247 static arch_irn_flags_t phi_get_flags(const void *_self, const ir_node *irn)
1249 return arch_irn_flags_none;
1252 static entity *phi_get_frame_entity(const void *_self, const ir_node *irn)
1257 static void phi_set_frame_offset(const void *_self, ir_node *irn, int bias)
1261 static const arch_irn_ops_if_t phi_irn_ops = {
1262 phi_get_irn_reg_req,
1267 phi_get_frame_entity,
1268 phi_set_frame_offset
1271 static const arch_irn_handler_t phi_irn_handler = {
1275 arch_irn_handler_t *be_phi_handler_new(const arch_env_t *arch_env)
1277 phi_handler_t *h = xmalloc(sizeof(h[0]));
1278 h->irn_handler.get_irn_ops = phi_get_irn_ops;
1279 h->irn_ops.impl = &phi_irn_ops;
1280 h->arch_env = arch_env;
1281 h->regs = pmap_create();
1282 return (arch_irn_handler_t *) h;
1285 void be_phi_handler_free(arch_irn_handler_t *handler)
1287 phi_handler_t *h = (void *) handler;
1288 pmap_destroy(h->regs);
1292 const void *be_phi_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn)
1294 phi_handler_t *phi_handler = get_phi_handler_from_handler(self);
1295 return is_Phi(irn) ? &phi_handler->irn_ops : NULL;
1298 void be_phi_handler_reset(arch_irn_handler_t *handler)
1300 phi_handler_t *h = get_phi_handler_from_handler(handler);
1302 pmap_destroy(h->regs);
1303 h->regs = pmap_create();
1309 | \ | | ___ __| | ___ | _ \ _ _ _ __ ___ _ __ (_)_ __ __ _
1310 | \| |/ _ \ / _` |/ _ \ | | | | | | | '_ ` _ \| '_ \| | '_ \ / _` |
1311 | |\ | (_) | (_| | __/ | |_| | |_| | | | | | | |_) | | | | | (_| |
1312 |_| \_|\___/ \__,_|\___| |____/ \__,_|_| |_| |_| .__/|_|_| |_|\__, |
1317 * Dumps a register requirement to a file.
1319 static void dump_node_req(FILE *f, int idx, be_req_t *req)
1322 int did_something = 0;
1324 const char *prefix = buf;
1326 snprintf(buf, sizeof(buf), "#%d ", idx);
1327 buf[sizeof(buf) - 1] = '\0';
1329 if(req->flags != arch_irn_flags_none) {
1330 fprintf(f, "%sflags: ", prefix);
1332 for(i = arch_irn_flags_none; i <= log2_ceil(arch_irn_flags_last); ++i) {
1333 if(req->flags & (1 << i)) {
1334 fprintf(f, "%s%s", prefix, arch_irn_flag_str(1 << i));
1342 if(req->req.cls != 0) {
1345 arch_register_req_format(tmp, sizeof(tmp), &req->req);
1346 fprintf(f, "%s", tmp);
1355 * Dumps node register requirements to a file.
1357 static void dump_node_reqs(FILE *f, ir_node *irn)
1360 be_node_attr_t *a = get_irn_attr(irn);
1362 fprintf(f, "registers: \n");
1363 for(i = 0; i < a->max_reg_data; ++i) {
1364 be_reg_data_t *rd = &a->reg_data[i];
1366 fprintf(f, "#%d: %s\n", i, rd->reg->name);
1369 fprintf(f, "in requirements\n");
1370 for(i = 0; i < a->max_reg_data; ++i) {
1371 dump_node_req(f, i, &a->reg_data[i].in_req);
1374 fprintf(f, "\nout requirements\n");
1375 for(i = 0; i < a->max_reg_data; ++i) {
1376 dump_node_req(f, i, &a->reg_data[i].req);
1381 * ir_op-Operation: dump a be node to file
1383 static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason)
1385 be_node_attr_t *at = get_irn_attr(irn);
1387 assert(is_be_node(irn));
1390 case dump_node_opcode_txt:
1391 fprintf(f, get_op_name(get_irn_op(irn)));
1393 case dump_node_mode_txt:
1394 fprintf(f, get_mode_name(get_irn_mode(irn)));
1396 case dump_node_nodeattr_txt:
1398 case dump_node_info_txt:
1399 dump_node_reqs(f, irn);
1401 if(be_has_frame_entity(irn)) {
1402 be_frame_attr_t *a = (be_frame_attr_t *) at;
1404 int bits = get_type_size_bits(get_entity_type(a->ent));
1405 ir_fprintf(f, "frame entity: %+F, offset 0x%x (%d), size 0x%x (%d) bits\n",
1406 a->ent, a->offset, a->offset, bits, bits);
1411 switch(be_get_irn_opcode(irn)) {
1414 be_spill_attr_t *a = (be_spill_attr_t *) at;
1415 ir_fprintf(f, "spill context: %+F\n", a->spill_ctx);
1421 be_stack_attr_t *a = (be_stack_attr_t *) at;
1422 if (a->offset == BE_STACK_FRAME_SIZE)
1423 fprintf(f, "offset: FRAME_SIZE\n");
1425 fprintf(f, "offset: %u\n", a->offset);
1426 fprintf(f, "direction: %s\n", a->dir == be_stack_dir_expand ? "expand" : "shrink");
1431 be_call_attr_t *a = (be_call_attr_t *) at;
1434 fprintf(f, "\ncalling: %s\n", get_entity_name(a->ent));
1446 * Copies the backend specific attributes from old node to new node.
1448 static void copy_attr(const ir_node *old_node, ir_node *new_node)
1450 be_node_attr_t *old_attr = get_irn_attr(old_node);
1451 be_node_attr_t *new_attr = get_irn_attr(new_node);
1454 assert(is_be_node(old_node));
1455 assert(is_be_node(new_node));
1457 memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
1458 new_attr->reg_data = NULL;
1460 if(new_attr->max_reg_data > 0) {
1461 new_attr->reg_data = NEW_ARR_D(be_reg_data_t, get_irg_obstack(get_irn_irg(new_node)), new_attr->max_reg_data);
1462 memcpy(new_attr->reg_data, old_attr->reg_data, new_attr->max_reg_data * sizeof(be_reg_data_t));
1464 for(i = 0; i < old_attr->max_reg_data; ++i) {
1467 r = &new_attr->reg_data[i].req;
1468 r->req.limited_env = r;
1470 r = &new_attr->reg_data[i].in_req;
1471 r->req.limited_env = r;
1476 static const ir_op_ops be_node_op_ops = {