4 * @author Sebastian Hack
6 * Backend node support.
8 * This file provides Perm, Copy, Spill and Reload nodes.
10 * Copyright (C) 2005-2006 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;
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, F, 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_SubSP = new_ir_op(beo_base + beo_SubSP, "be_SubSP", op_pin_state_pinned, N, oparity_unary, 0, sizeof(be_node_attr_t), &be_node_op_ops);
208 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);
209 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);
210 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);
211 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);
212 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);
213 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);
214 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);
215 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);
217 set_op_tag(op_be_Spill, &be_node_tag);
218 set_op_tag(op_be_Reload, &be_node_tag);
219 set_op_tag(op_be_Perm, &be_node_tag);
220 set_op_tag(op_be_MemPerm, &be_node_tag);
221 set_op_tag(op_be_Copy, &be_node_tag);
222 set_op_tag(op_be_Keep, &be_node_tag);
223 set_op_tag(op_be_CopyKeep, &be_node_tag);
224 set_op_tag(op_be_Call, &be_node_tag);
225 set_op_tag(op_be_Return, &be_node_tag);
226 set_op_tag(op_be_AddSP, &be_node_tag);
227 set_op_tag(op_be_SubSP, &be_node_tag);
228 set_op_tag(op_be_SetSP, &be_node_tag);
229 set_op_tag(op_be_IncSP, &be_node_tag);
230 set_op_tag(op_be_RegParams, &be_node_tag);
231 set_op_tag(op_be_StackParam, &be_node_tag);
232 set_op_tag(op_be_FrameLoad, &be_node_tag);
233 set_op_tag(op_be_FrameStore, &be_node_tag);
234 set_op_tag(op_be_FrameAddr, &be_node_tag);
235 set_op_tag(op_be_Barrier, &be_node_tag);
237 op_be_FrameAddr->ops.node_cmp_attr = FrameAddr_cmp_attr;
241 * Initializes the generic attribute of all be nodes and return ir.
243 static void *init_node_attr(ir_node* irn, int max_reg_data)
245 ir_graph *irg = get_irn_irg(irn);
246 be_node_attr_t *a = get_irn_attr(irn);
248 memset(a, 0, sizeof(get_op_attr_size(get_irn_op(irn))));
249 a->max_reg_data = max_reg_data;
252 if(max_reg_data > 0) {
255 a->reg_data = NEW_ARR_D(be_reg_data_t, get_irg_obstack(irg), max_reg_data);
256 memset(a->reg_data, 0, max_reg_data * sizeof(a->reg_data[0]));
257 for(i = 0; i < max_reg_data; ++i) {
258 a->reg_data[i].req.req.cls = NULL;
259 a->reg_data[i].req.req.type = arch_register_req_type_none;
266 int is_be_node(const ir_node *irn)
268 return get_op_tag(get_irn_op(irn)) == &be_node_tag;
271 be_opcode_t be_get_irn_opcode(const ir_node *irn)
273 return is_be_node(irn) ? get_irn_opcode(irn) - beo_base : beo_NoBeOp;
276 static int redir_proj(const ir_node **node, int pos)
278 const ir_node *n = *node;
283 *node = irn = get_Proj_pred(n);
285 assert(get_irn_mode(irn) == mode_T);
286 *node = get_Proj_pred(irn);
288 return get_Proj_proj(n);
294 static be_node_attr_t *retrieve_irn_attr(const ir_node *irn, int *the_pos)
297 be_node_attr_t *res = NULL;
298 int *pos = the_pos ? the_pos : &dummy;
302 ir_node *pred = get_Proj_pred(irn);
303 int p = get_Proj_proj(irn);
305 if(is_be_node(pred)) {
306 assert(get_irn_mode(pred) == mode_T);
308 res = get_irn_attr(pred);
309 assert(p >= 0 && p < res->max_reg_data && "illegal proj number");
313 else if(is_be_node(irn) && get_irn_mode(irn) != mode_T) {
314 be_node_attr_t *a = get_irn_attr(irn);
315 if(a->max_reg_data > 0) {
324 static be_reg_data_t *retrieve_reg_data(const ir_node *irn)
327 be_node_attr_t *a = retrieve_irn_attr(irn, &pos);
328 return a ? &a->reg_data[pos] : NULL;
332 be_node_set_irn_reg(const void *_self, ir_node *irn, const arch_register_t *reg)
334 be_reg_data_t *r = retrieve_reg_data(irn);
341 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)
349 res = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
350 a = init_node_attr(res, 2);
354 be_node_set_reg_class(res, 0, cls_frame);
355 be_node_set_reg_class(res, 1, cls);
359 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)
366 res = new_ir_node(NULL, irg, bl, op_be_Reload, mode, 2, in);
367 init_node_attr(res, 2);
368 be_node_set_reg_class(res, 0, cls_frame);
369 be_node_set_reg_class(res, -1, cls);
370 be_node_set_flags(res, -1, arch_irn_flags_rematerializable);
374 ir_node *be_get_Reload_mem(const ir_node *irn)
376 assert(be_is_Reload(irn));
377 return get_irn_n(irn, be_pos_Reload_mem);
380 ir_node *be_get_Reload_frame(const ir_node *irn)
382 assert(be_is_Reload(irn));
383 return get_irn_n(irn, be_pos_Reload_frame);
386 ir_node *be_get_Spill_val(const ir_node *irn)
388 assert(be_is_Spill(irn));
389 return get_irn_n(irn, be_pos_Spill_val);
392 ir_node *be_get_Spill_frame(const ir_node *irn)
394 assert(be_is_Spill(irn));
395 return get_irn_n(irn, be_pos_Spill_frame);
398 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
401 ir_node *irn = new_ir_node(NULL, irg, bl, op_be_Perm, mode_T, n, in);
402 init_node_attr(irn, n);
403 for(i = 0; i < n; ++i) {
404 be_node_set_reg_class(irn, i, cls);
405 be_node_set_reg_class(irn, OUT_POS(i), cls);
411 ir_node *be_new_MemPerm(const arch_env_t *arch_env, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
414 ir_node *frame = get_irg_frame(irg);
415 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
417 const arch_register_t *sp = arch_env->isa->sp;
418 be_memperm_attr_t *attr;
421 real_in = alloca((n+1) * sizeof(real_in[0]));
423 memcpy(&real_in[1], in, n * sizeof(real_in[0]));
425 irn = new_ir_node(NULL, irg, bl, op_be_MemPerm, mode_T, n+1, real_in);
427 init_node_attr(irn, n + 1);
428 be_node_set_reg_class(irn, 0, sp->reg_class);
429 for(i = 0; i < n; ++i) {
430 be_node_set_reg_class(irn, i + 1, cls_frame);
431 be_node_set_reg_class(irn, OUT_POS(i), cls_frame);
434 attr = get_irn_attr(irn);
436 attr->in_entities = obstack_alloc(irg->obst, n * sizeof(attr->in_entities[0]));
437 memset(attr->in_entities, 0, n * sizeof(attr->in_entities[0]));
438 attr->out_entities = obstack_alloc(irg->obst, n*sizeof(attr->out_entities[0]));
439 memset(attr->out_entities, 0, n*sizeof(attr->out_entities[0]));
445 ir_node *be_new_Copy(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *op)
451 res = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
452 init_node_attr(res, 1);
453 be_node_set_reg_class(res, 0, cls);
454 be_node_set_reg_class(res, OUT_POS(0), cls);
458 ir_node *be_get_Copy_op(const ir_node *cpy) {
459 return get_irn_n(cpy, be_pos_Copy_op);
462 void be_set_Copy_op(ir_node *cpy, ir_node *op) {
463 set_irn_n(cpy, be_pos_Copy_op, op);
466 ir_node *be_new_Keep(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
471 irn = new_ir_node(NULL, irg, bl, op_be_Keep, mode_ANY, n, in);
472 init_node_attr(irn, n);
473 for(i = 0; i < n; ++i) {
474 be_node_set_reg_class(irn, i, cls);
480 ir_node *be_new_Call(dbg_info *dbg, ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *sp, ir_node *ptr,
481 int n_outs, int n, ir_node *in[], ir_type *call_tp)
484 int real_n = be_pos_Call_first_arg + n;
488 NEW_ARR_A(ir_node *, real_in, real_n);
489 real_in[be_pos_Call_mem] = mem;
490 real_in[be_pos_Call_sp] = sp;
491 real_in[be_pos_Call_ptr] = ptr;
492 memcpy(&real_in[be_pos_Call_first_arg], in, n * sizeof(in[0]));
494 irn = new_ir_node(dbg, irg, bl, op_be_Call, mode_T, real_n, real_in);
495 a = init_node_attr(irn, (n_outs > real_n ? n_outs : real_n));
497 a->call_tp = call_tp;
501 /* Gets the call entity or NULL if this is no static call. */
502 entity *be_Call_get_entity(const ir_node *call) {
503 be_call_attr_t *a = get_irn_attr(call);
504 assert(be_is_Call(call));
508 /* Sets the call entity. */
509 void be_Call_set_entity(ir_node *call, entity *ent) {
510 be_call_attr_t *a = get_irn_attr(call);
511 assert(be_is_Call(call));
515 /* Gets the call type. */
516 ir_type *be_Call_get_type(ir_node *call) {
517 be_call_attr_t *a = get_irn_attr(call);
518 assert(be_is_Call(call));
522 /* Sets the call type. */
523 void be_Call_set_type(ir_node *call, ir_type *call_tp) {
524 be_call_attr_t *a = get_irn_attr(call);
525 assert(be_is_Call(call));
526 a->call_tp = call_tp;
529 /* Construct a new be_Return. */
530 ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *bl, int n_res, int n, ir_node *in[])
533 ir_node *irn = new_ir_node(dbg, irg, bl, op_be_Return, mode_X, n, in);
534 init_node_attr(irn, n);
535 a = get_irn_attr(irn);
536 a->num_ret_vals = n_res;
541 /* Returns the number of real returns values */
542 int be_Return_get_n_rets(ir_node *ret)
544 be_return_attr_t *a = get_irn_attr(ret);
545 return a->num_ret_vals;
548 ir_node *be_new_IncSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, int offset)
555 irn = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode, sizeof(in) / sizeof(in[0]), in);
556 a = init_node_attr(irn, 1);
559 be_node_set_flags(irn, -1, arch_irn_flags_ignore | arch_irn_flags_modify_sp);
561 /* Set output constraint to stack register. */
562 be_node_set_reg_class(irn, 0, sp->reg_class);
563 be_set_constr_single_reg(irn, BE_OUT_POS(0), sp);
564 be_node_set_irn_reg(NULL, irn, sp);
569 ir_node *be_new_AddSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, ir_node *sz)
573 ir_node *in[be_pos_AddSP_last];
575 in[be_pos_AddSP_old_sp] = old_sp;
576 in[be_pos_AddSP_size] = sz;
578 irn = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T, be_pos_AddSP_last, in);
579 a = init_node_attr(irn, be_pos_AddSP_last);
581 be_node_set_flags(irn, OUT_POS(pn_be_AddSP_res), arch_irn_flags_ignore | arch_irn_flags_modify_sp);
583 /* Set output constraint to stack register. */
584 be_set_constr_single_reg(irn, be_pos_AddSP_old_sp, sp);
585 be_node_set_reg_class(irn, be_pos_AddSP_size, arch_register_get_class(sp));
586 be_set_constr_single_reg(irn, OUT_POS(pn_be_AddSP_res), sp);
587 a->reg_data[pn_be_AddSP_res].reg = sp;
592 ir_node *be_new_SubSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, ir_node *sz)
596 ir_node *in[be_pos_SubSP_last];
598 in[be_pos_SubSP_old_sp] = old_sp;
599 in[be_pos_SubSP_size] = sz;
601 irn = new_ir_node(NULL, irg, bl, op_be_SubSP, mode_T, be_pos_SubSP_last, in);
602 a = init_node_attr(irn, be_pos_SubSP_last);
604 be_node_set_flags(irn, OUT_POS(pn_be_SubSP_res), arch_irn_flags_ignore | arch_irn_flags_modify_sp);
606 /* Set output constraint to stack register. */
607 be_set_constr_single_reg(irn, be_pos_SubSP_old_sp, sp);
608 be_node_set_reg_class(irn, be_pos_SubSP_size, arch_register_get_class(sp));
609 be_set_constr_single_reg(irn, OUT_POS(pn_be_SubSP_res), sp);
610 a->reg_data[pn_be_SubSP_res].reg = sp;
615 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)
624 irn = new_ir_node(NULL, irg, bl, op_be_SetSP, get_irn_mode(old_sp), 3, in);
625 a = init_node_attr(irn, 3);
627 be_node_set_flags(irn, OUT_POS(0), arch_irn_flags_ignore | arch_irn_flags_modify_sp);
629 /* Set output constraint to stack register. */
630 be_set_constr_single_reg(irn, OUT_POS(0), sp);
631 be_node_set_reg_class(irn, be_pos_AddSP_size, sp->reg_class);
632 be_node_set_reg_class(irn, be_pos_AddSP_old_sp, sp->reg_class);
637 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)
643 in[0] = frame_pointer;
644 irn = new_ir_node(NULL, irg, bl, op_be_StackParam, mode, 1, in);
645 a = init_node_attr(irn, 1);
648 be_node_set_reg_class(irn, 0, cls_frame);
649 be_node_set_reg_class(irn, OUT_POS(0), cls);
653 ir_node *be_new_RegParams(ir_graph *irg, ir_node *bl, int n_outs)
658 irn = new_ir_node(NULL, irg, bl, op_be_RegParams, mode_T, 0, in);
659 init_node_attr(irn, n_outs);
663 ir_node *be_new_FrameLoad(const arch_register_class_t *cls_frame, const arch_register_class_t *cls_data,
664 ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *frame, entity *ent)
672 irn = new_ir_node(NULL, irg, bl, op_be_FrameLoad, mode_T, 2, in);
673 a = init_node_attr(irn, 3);
676 be_node_set_reg_class(irn, 1, cls_frame);
677 be_node_set_reg_class(irn, OUT_POS(pn_Load_res), cls_data);
681 ir_node *be_new_FrameStore(const arch_register_class_t *cls_frame, const arch_register_class_t *cls_data,
682 ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *frame, ir_node *data, entity *ent)
691 irn = new_ir_node(NULL, irg, bl, op_be_FrameStore, mode_T, 3, in);
692 a = init_node_attr(irn, 3);
695 be_node_set_reg_class(irn, 1, cls_frame);
696 be_node_set_reg_class(irn, 2, cls_data);
700 ir_node *be_new_FrameAddr(const arch_register_class_t *cls_frame, ir_graph *irg, ir_node *bl, ir_node *frame, entity *ent)
707 irn = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
708 a = init_node_attr(irn, 1);
711 be_node_set_reg_class(irn, 0, cls_frame);
712 be_node_set_reg_class(irn, OUT_POS(0), cls_frame);
714 return optimize_node(irn);
717 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)
720 ir_node **in = (ir_node **) alloca((n + 1) * sizeof(in[0]));
723 memcpy(&in[1], in_keep, n * sizeof(in[0]));
724 irn = new_ir_node(NULL, irg, bl, op_be_CopyKeep, mode, n + 1, in);
725 init_node_attr(irn, n + 1);
726 be_node_set_reg_class(irn, OUT_POS(0), cls);
727 be_node_set_reg_class(irn, 0, cls);
732 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)
737 return be_new_CopyKeep(cls, irg, bl, src, 1, in, mode);
740 ir_node *be_get_CopyKeep_op(const ir_node *cpy) {
741 return get_irn_n(cpy, be_pos_CopyKeep_op);
744 void be_set_CopyKeep_op(ir_node *cpy, ir_node *op) {
745 set_irn_n(cpy, be_pos_CopyKeep_op, op);
748 ir_node *be_new_Barrier(ir_graph *irg, ir_node *bl, int n, ir_node *in[])
752 irn = new_ir_node(NULL, irg, bl, op_be_Barrier, mode_T, n, in);
753 init_node_attr(irn, n);
757 int be_is_Spill (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Spill ; }
758 int be_is_Reload (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Reload ; }
759 int be_is_Copy (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Copy ; }
760 int be_is_CopyKeep (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_CopyKeep ; }
761 int be_is_Perm (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Perm ; }
762 int be_is_MemPerm (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_MemPerm ; }
763 int be_is_Keep (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Keep ; }
764 int be_is_Call (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Call ; }
765 int be_is_Return (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Return ; }
766 int be_is_IncSP (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_IncSP ; }
767 int be_is_SetSP (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_SetSP ; }
768 int be_is_AddSP (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_AddSP ; }
769 int be_is_RegParams (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_RegParams ; }
770 int be_is_StackParam (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_StackParam ; }
771 int be_is_FrameAddr (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameAddr ; }
772 int be_is_FrameLoad (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameLoad ; }
773 int be_is_FrameStore (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameStore ; }
774 int be_is_Barrier (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Barrier ; }
776 int be_has_frame_entity(const ir_node *irn)
778 switch(be_get_irn_opcode(irn)) {
791 entity* be_get_frame_entity(const ir_node *irn)
793 if(be_has_frame_entity(irn)) {
794 be_frame_attr_t *a = get_irn_attr(irn);
800 void be_set_MemPerm_in_entity(const ir_node *irn, int n, entity *ent)
802 be_memperm_attr_t *attr = get_irn_attr(irn);
804 assert(be_is_MemPerm(irn));
805 assert(n < be_get_MemPerm_entity_arity(irn));
807 attr->in_entities[n] = ent;
810 entity* be_get_MemPerm_in_entity(const ir_node* irn, int n)
812 be_memperm_attr_t *attr = get_irn_attr(irn);
814 assert(be_is_MemPerm(irn));
815 assert(n < be_get_MemPerm_entity_arity(irn));
817 return attr->in_entities[n];
820 void be_set_MemPerm_out_entity(const ir_node *irn, int n, entity *ent)
822 be_memperm_attr_t *attr = get_irn_attr(irn);
824 assert(be_is_MemPerm(irn));
825 assert(n < be_get_MemPerm_entity_arity(irn));
827 attr->out_entities[n] = ent;
830 entity* be_get_MemPerm_out_entity(const ir_node* irn, int n)
832 be_memperm_attr_t *attr = get_irn_attr(irn);
834 assert(be_is_MemPerm(irn));
835 assert(n < be_get_MemPerm_entity_arity(irn));
837 return attr->out_entities[n];
840 int be_get_MemPerm_entity_arity(const ir_node *irn)
842 return get_irn_arity(irn) - 1;
845 static void be_limited(void *data, bitset_t *bs)
847 be_req_t *req = data;
850 case be_req_kind_negate_old_limited:
851 case be_req_kind_old_limited:
852 req->x.old_limited.old_limited(req->x.old_limited.old_limited_env, bs);
853 if(req->kind == be_req_kind_negate_old_limited)
856 case be_req_kind_single_reg:
857 bitset_clear_all(bs);
858 bitset_set(bs, req->x.single_reg->index);
863 static INLINE be_req_t *get_req(ir_node *irn, int pos)
865 int idx = pos < 0 ? -(pos + 1) : pos;
866 be_node_attr_t *a = get_irn_attr(irn);
867 be_reg_data_t *rd = &a->reg_data[idx];
868 be_req_t *r = pos < 0 ? &rd->req : &rd->in_req;
870 assert(is_be_node(irn));
871 assert(!(pos >= 0) || pos < get_irn_arity(irn));
872 assert(!(pos < 0) || -(pos + 1) <= a->max_reg_data);
877 void be_set_constr_single_reg(ir_node *irn, int pos, const arch_register_t *reg)
879 be_req_t *r = get_req(irn, pos);
881 r->kind = be_req_kind_single_reg;
882 r->x.single_reg = reg;
883 r->req.limited = be_limited;
884 r->req.limited_env = r;
885 r->req.type = arch_register_req_type_limited;
886 r->req.cls = reg->reg_class;
889 void be_set_constr_limited(ir_node *irn, int pos, const arch_register_req_t *req)
891 be_req_t *r = get_req(irn, pos);
893 assert(arch_register_req_is(req, limited));
895 r->kind = be_req_kind_old_limited;
896 r->req.limited = be_limited;
897 r->req.limited_env = r;
898 r->req.type = arch_register_req_type_limited;
899 r->req.cls = req->cls;
901 r->x.old_limited.old_limited = req->limited;
902 r->x.old_limited.old_limited_env = req->limited_env;
905 void be_node_set_flags(ir_node *irn, int pos, arch_irn_flags_t flags)
907 be_req_t *r = get_req(irn, pos);
911 void be_node_set_reg_class(ir_node *irn, int pos, const arch_register_class_t *cls)
913 be_req_t *r = get_req(irn, pos);
915 if(r->req.type == arch_register_req_type_none)
916 r->req.type = arch_register_req_type_normal;
919 void be_node_set_req_type(ir_node *irn, int pos, arch_register_req_type_t type)
921 be_req_t *r = get_req(irn, pos);
925 ir_node *be_get_IncSP_pred(ir_node *irn) {
926 assert(be_is_IncSP(irn));
927 return get_irn_n(irn, 0);
930 void be_set_IncSP_pred(ir_node *incsp, ir_node *pred) {
931 assert(be_is_IncSP(incsp));
932 set_irn_n(incsp, 0, pred);
935 ir_node *be_get_IncSP_mem(ir_node *irn) {
936 assert(be_is_IncSP(irn));
937 return get_irn_n(irn, 1);
940 void be_set_IncSP_offset(ir_node *irn, int offset)
942 be_stack_attr_t *a = get_irn_attr(irn);
943 assert(be_is_IncSP(irn));
947 int be_get_IncSP_offset(const ir_node *irn)
949 be_stack_attr_t *a = get_irn_attr(irn);
950 assert(be_is_IncSP(irn));
954 ir_node *be_spill(const arch_env_t *arch_env, ir_node *irn)
956 ir_node *bl = get_nodes_block(irn);
957 ir_graph *irg = get_irn_irg(bl);
958 ir_node *frame = get_irg_frame(irg);
961 const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, irn, -1);
962 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
964 spill = be_new_Spill(cls, cls_frame, irg, bl, frame, irn);
968 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)
972 ir_node *bl = is_Block(insert) ? insert : get_nodes_block(insert);
973 ir_graph *irg = get_irn_irg(bl);
974 ir_node *frame = get_irg_frame(irg);
975 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
977 assert(be_is_Spill(spill) || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
979 reload = be_new_Reload(cls, cls_frame, irg, bl, frame, spill, mode);
981 if(is_Block(insert)) {
982 insert = sched_skip(insert, 0, sched_skip_cf_predicator, (void *) arch_env);
983 sched_add_after(insert, reload);
987 sched_add_before(insert, reload);
994 | _ \ ___ __ _ | _ \ ___ __ _ ___
995 | |_) / _ \/ _` | | |_) / _ \/ _` / __|
996 | _ < __/ (_| | | _ < __/ (_| \__ \
997 |_| \_\___|\__, | |_| \_\___|\__, |___/
1003 static void *put_out_reg_req(arch_register_req_t *req, const ir_node *irn, int out_pos)
1005 const be_node_attr_t *a = get_irn_attr(irn);
1007 if(out_pos < a->max_reg_data) {
1008 memcpy(req, &a->reg_data[out_pos].req, sizeof(req[0]));
1010 if(be_is_Copy(irn)) {
1011 req->type |= arch_register_req_type_should_be_same;
1012 req->other_same = be_get_Copy_op(irn);
1016 req->type = arch_register_req_type_none;
1023 static void *put_in_reg_req(arch_register_req_t *req, const ir_node *irn, int pos)
1025 const be_node_attr_t *a = get_irn_attr(irn);
1027 if(pos < get_irn_arity(irn) && pos < a->max_reg_data)
1028 memcpy(req, &a->reg_data[pos].in_req, sizeof(req[0]));
1030 req->type = arch_register_req_type_none;
1037 static const arch_register_req_t *
1038 be_node_get_irn_reg_req(const void *self, arch_register_req_t *req, const ir_node *irn, int pos)
1043 if(get_irn_mode(irn) == mode_T)
1046 out_pos = redir_proj((const ir_node **) &irn, pos);
1047 assert(is_be_node(irn));
1048 return put_out_reg_req(req, irn, out_pos);
1052 return is_be_node(irn) ? put_in_reg_req(req, irn, pos) : NULL;
1058 const arch_register_t *
1059 be_node_get_irn_reg(const void *_self, const ir_node *irn)
1061 be_reg_data_t *r = retrieve_reg_data(irn);
1062 return r ? r->reg : NULL;
1065 static arch_irn_class_t be_node_classify(const void *_self, const ir_node *irn)
1067 redir_proj((const ir_node **) &irn, -1);
1069 switch(be_get_irn_opcode(irn)) {
1070 #define XXX(a,b) case beo_ ## a: return arch_irn_class_ ## b
1072 XXX(Reload, reload);
1075 XXX(Return, branch);
1076 XXX(StackParam, stackparam);
1079 return arch_irn_class_normal;
1085 static arch_irn_flags_t be_node_get_flags(const void *_self, const ir_node *irn)
1087 be_reg_data_t *r = retrieve_reg_data(irn);
1088 return r ? r->req.flags : 0;
1091 static entity *be_node_get_frame_entity(const void *self, const ir_node *irn)
1093 return be_get_frame_entity(irn);
1096 static void be_node_set_frame_entity(const void *self, ir_node *irn, entity *ent)
1100 assert(be_has_frame_entity(irn));
1102 a = get_irn_attr(irn);
1106 static void be_node_set_frame_offset(const void *self, ir_node *irn, int offset)
1108 if(be_has_frame_entity(irn)) {
1109 be_frame_attr_t *a = get_irn_attr(irn);
1114 static int be_node_get_sp_bias(const void *self, const ir_node *irn)
1118 if(be_is_IncSP(irn)) {
1119 result = be_get_IncSP_offset(irn);
1126 ___ ____ _ _ _ _ _ _
1127 |_ _| _ \| \ | | | | | | __ _ _ __ __| | | ___ _ __
1128 | || |_) | \| | | |_| |/ _` | '_ \ / _` | |/ _ \ '__|
1129 | || _ <| |\ | | _ | (_| | | | | (_| | | __/ |
1130 |___|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
1134 static const arch_irn_ops_if_t be_node_irn_ops_if = {
1135 be_node_get_irn_reg_req,
1136 be_node_set_irn_reg,
1137 be_node_get_irn_reg,
1140 be_node_get_frame_entity,
1141 be_node_set_frame_entity,
1142 be_node_set_frame_offset,
1143 be_node_get_sp_bias,
1144 NULL, /* get_inverse */
1145 NULL, /* get_op_estimated_cost */
1146 NULL, /* possible_memory_operand */
1147 NULL, /* perform_memory_operand */
1150 static const arch_irn_ops_t be_node_irn_ops = {
1154 const void *be_node_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn)
1156 redir_proj((const ir_node **) &irn, -1);
1157 return is_be_node(irn) ? &be_node_irn_ops : NULL;
1160 const arch_irn_handler_t be_node_irn_handler = {
1165 ____ _ _ ___ ____ _ _ _ _ _ _
1166 | _ \| |__ (_) |_ _| _ \| \ | | | | | | __ _ _ __ __| | | ___ _ __
1167 | |_) | '_ \| | | || |_) | \| | | |_| |/ _` | '_ \ / _` | |/ _ \ '__|
1168 | __/| | | | | | || _ <| |\ | | _ | (_| | | | | (_| | | __/ |
1169 |_| |_| |_|_| |___|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
1174 arch_irn_handler_t irn_handler;
1175 arch_irn_ops_t irn_ops;
1176 const arch_env_t *arch_env;
1180 #define get_phi_handler_from_handler(h) container_of(h, phi_handler_t, irn_handler)
1181 #define get_phi_handler_from_ops(h) container_of(h, phi_handler_t, irn_ops)
1183 static const void *phi_get_irn_ops(const arch_irn_handler_t *handler, const ir_node *irn)
1185 const phi_handler_t *h = get_phi_handler_from_handler(handler);
1186 return is_Phi(irn) && mode_is_datab(get_irn_mode(irn)) ? &h->irn_ops : NULL;
1190 * Get register class of a Phi.
1193 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)
1195 int n = get_irn_arity(phi);
1199 if(*visited && pset_find_ptr(*visited, phi))
1202 for(i = 0; i < n; ++i) {
1203 op = get_irn_n(phi, i);
1205 return arch_get_register_req(h->arch_env, req, op, BE_OUT_POS(0));
1209 The operands of that Phi were all Phis themselves.
1210 We have to start a DFS for a non-Phi argument now.
1213 *visited = pset_new_ptr(16);
1215 pset_insert_ptr(*visited, phi);
1217 for(i = 0; i < n; ++i) {
1218 op = get_irn_n(phi, i);
1219 if(get_Phi_reg_req_recursive(h, req, op, visited))
1226 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)
1228 phi_handler_t *phi_handler = get_phi_handler_from_ops(self);
1229 pset *visited = NULL;
1231 get_Phi_reg_req_recursive(phi_handler, req, irn, &visited);
1232 /* Set the requirements type to normal, since an operand of the Phi could have had constraints. */
1233 req->type = arch_register_req_type_normal;
1240 static void phi_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg)
1242 phi_handler_t *h = get_phi_handler_from_ops(self);
1243 pmap_insert(h->regs, irn, (void *) reg);
1246 static const arch_register_t *phi_get_irn_reg(const void *self, const ir_node *irn)
1248 phi_handler_t *h = get_phi_handler_from_ops(self);
1249 return pmap_get(h->regs, (void *) irn);
1252 static arch_irn_class_t phi_classify(const void *_self, const ir_node *irn)
1254 return arch_irn_class_normal;
1257 static arch_irn_flags_t phi_get_flags(const void *_self, const ir_node *irn)
1259 return arch_irn_flags_none;
1262 static entity *phi_get_frame_entity(const void *_self, const ir_node *irn)
1267 static void phi_set_frame_entity(const void *_self, ir_node *irn, entity *ent)
1271 static void phi_set_frame_offset(const void *_self, ir_node *irn, int bias)
1275 static int phi_get_sp_bias(const void* self, const ir_node *irn)
1280 static const arch_irn_ops_if_t phi_irn_ops = {
1281 phi_get_irn_reg_req,
1286 phi_get_frame_entity,
1287 phi_set_frame_entity,
1288 phi_set_frame_offset,
1290 NULL, /* get_inverse */
1291 NULL, /* get_op_estimated_cost */
1292 NULL, /* possible_memory_operand */
1293 NULL, /* perform_memory_operand */
1296 static const arch_irn_handler_t phi_irn_handler = {
1300 arch_irn_handler_t *be_phi_handler_new(const arch_env_t *arch_env)
1302 phi_handler_t *h = xmalloc(sizeof(h[0]));
1303 h->irn_handler.get_irn_ops = phi_get_irn_ops;
1304 h->irn_ops.impl = &phi_irn_ops;
1305 h->arch_env = arch_env;
1306 h->regs = pmap_create();
1307 return (arch_irn_handler_t *) h;
1310 void be_phi_handler_free(arch_irn_handler_t *handler)
1312 phi_handler_t *h = (void *) handler;
1313 pmap_destroy(h->regs);
1317 const void *be_phi_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn)
1319 phi_handler_t *phi_handler = get_phi_handler_from_handler(self);
1320 return is_Phi(irn) ? &phi_handler->irn_ops : NULL;
1323 void be_phi_handler_reset(arch_irn_handler_t *handler)
1325 phi_handler_t *h = get_phi_handler_from_handler(handler);
1327 pmap_destroy(h->regs);
1328 h->regs = pmap_create();
1333 | \ | | ___ __| | ___ | _ \ _ _ _ __ ___ _ __ (_)_ __ __ _
1334 | \| |/ _ \ / _` |/ _ \ | | | | | | | '_ ` _ \| '_ \| | '_ \ / _` |
1335 | |\ | (_) | (_| | __/ | |_| | |_| | | | | | | |_) | | | | | (_| |
1336 |_| \_|\___/ \__,_|\___| |____/ \__,_|_| |_| |_| .__/|_|_| |_|\__, |
1341 * Dumps a register requirement to a file.
1343 static void dump_node_req(FILE *f, int idx, be_req_t *req)
1346 int did_something = 0;
1348 const char *prefix = buf;
1350 snprintf(buf, sizeof(buf), "#%d ", idx);
1351 buf[sizeof(buf) - 1] = '\0';
1353 if(req->flags != arch_irn_flags_none) {
1354 fprintf(f, "%sflags: ", prefix);
1356 for(i = arch_irn_flags_none; i <= log2_ceil(arch_irn_flags_last); ++i) {
1357 if(req->flags & (1 << i)) {
1358 fprintf(f, "%s%s", prefix, arch_irn_flag_str(1 << i));
1366 if(req->req.cls != 0) {
1369 arch_register_req_format(tmp, sizeof(tmp), &req->req);
1370 fprintf(f, "%s", tmp);
1379 * Dumps node register requirements to a file.
1381 static void dump_node_reqs(FILE *f, ir_node *irn)
1384 be_node_attr_t *a = get_irn_attr(irn);
1386 fprintf(f, "registers: \n");
1387 for(i = 0; i < a->max_reg_data; ++i) {
1388 be_reg_data_t *rd = &a->reg_data[i];
1390 fprintf(f, "#%d: %s\n", i, rd->reg->name);
1393 fprintf(f, "in requirements\n");
1394 for(i = 0; i < a->max_reg_data; ++i) {
1395 dump_node_req(f, i, &a->reg_data[i].in_req);
1398 fprintf(f, "\nout requirements\n");
1399 for(i = 0; i < a->max_reg_data; ++i) {
1400 dump_node_req(f, i, &a->reg_data[i].req);
1405 * ir_op-Operation: dump a be node to file
1407 static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason)
1409 be_node_attr_t *at = get_irn_attr(irn);
1411 assert(is_be_node(irn));
1414 case dump_node_opcode_txt:
1415 fprintf(f, get_op_name(get_irn_op(irn)));
1417 case dump_node_mode_txt:
1418 fprintf(f, get_mode_name(get_irn_mode(irn)));
1420 case dump_node_nodeattr_txt:
1422 case dump_node_info_txt:
1423 dump_node_reqs(f, irn);
1425 if(be_has_frame_entity(irn)) {
1426 be_frame_attr_t *a = (be_frame_attr_t *) at;
1428 int bits = get_type_size_bits(get_entity_type(a->ent));
1429 ir_fprintf(f, "frame entity: %+F, offset 0x%x (%d), size 0x%x (%d) bits\n",
1430 a->ent, a->offset, a->offset, bits, bits);
1435 switch(be_get_irn_opcode(irn)) {
1438 be_stack_attr_t *a = (be_stack_attr_t *) at;
1439 if (a->offset == BE_STACK_FRAME_SIZE_EXPAND)
1440 fprintf(f, "offset: FRAME_SIZE\n");
1441 else if(a->offset == BE_STACK_FRAME_SIZE_SHRINK)
1442 fprintf(f, "offset: -FRAME SIZE\n");
1444 fprintf(f, "offset: %u\n", a->offset);
1449 be_call_attr_t *a = (be_call_attr_t *) at;
1452 fprintf(f, "\ncalling: %s\n", get_entity_name(a->ent));
1458 for(i = 0; i < be_get_MemPerm_entity_arity(irn); ++i) {
1460 in = be_get_MemPerm_in_entity(irn, i);
1461 out = be_get_MemPerm_out_entity(irn, i);
1463 fprintf(f, "\nin[%d]: %s\n", i, get_entity_name(in));
1466 fprintf(f, "\nout[%d]: %s\n", i, get_entity_name(out));
1482 * Copies the backend specific attributes from old node to new node.
1484 static void copy_attr(const ir_node *old_node, ir_node *new_node)
1486 be_node_attr_t *old_attr = get_irn_attr(old_node);
1487 be_node_attr_t *new_attr = get_irn_attr(new_node);
1490 assert(is_be_node(old_node));
1491 assert(is_be_node(new_node));
1493 memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
1494 new_attr->reg_data = NULL;
1496 if(new_attr->max_reg_data > 0) {
1497 new_attr->reg_data = NEW_ARR_D(be_reg_data_t, get_irg_obstack(get_irn_irg(new_node)), new_attr->max_reg_data);
1498 memcpy(new_attr->reg_data, old_attr->reg_data, new_attr->max_reg_data * sizeof(be_reg_data_t));
1500 for(i = 0; i < old_attr->max_reg_data; ++i) {
1503 r = &new_attr->reg_data[i].req;
1504 r->req.limited_env = r;
1506 r = &new_attr->reg_data[i].in_req;
1507 r->req.limited_env = r;
1512 static const ir_op_ops be_node_op_ops = {