2 * Copyright (C) 1995-2007 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Backend node support for generic backend nodes.
23 * @author Sebastian Hack
27 * Backend node support for generic backend nodes.
28 * This file provides Perm, Copy, Spill and Reload nodes.
43 #include "bitfiddle.h"
44 #include "raw_bitset.h"
56 #include "besched_t.h"
61 #define OUT_POS(x) (-((x) + 1))
63 /* Sometimes we want to put const nodes into get_irn_generic_attr ... */
64 #define get_irn_attr(irn) get_irn_generic_attr((ir_node *) (irn))
66 static unsigned be_node_tag = FOURCC('B', 'E', 'N', 'O');
69 arch_register_req_t req;
70 arch_irn_flags_t flags;
74 const arch_register_t *reg;
79 /** The generic be nodes attribute type. */
81 be_reg_data_t *reg_data;
84 /** The be_Return nodes attribute type. */
86 be_node_attr_t node_attr;
87 int num_ret_vals; /**< number of return values */
90 /** The be_Stack attribute type. */
92 be_node_attr_t node_attr;
93 int offset; /**< The offset by which the stack shall be expanded/shrinked. */
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 ir_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 ir_entity **in_entities;
113 ir_entity **out_entities;
119 ir_op *op_be_MemPerm;
122 ir_op *op_be_CopyKeep;
129 ir_op *op_be_RegParams;
130 ir_op *op_be_StackParam;
131 ir_op *op_be_FrameAddr;
132 ir_op *op_be_FrameLoad;
133 ir_op *op_be_FrameStore;
134 ir_op *op_be_Barrier;
136 static int beo_base = -1;
138 static const ir_op_ops be_node_op_ops;
140 #define N irop_flag_none
141 #define L irop_flag_labeled
142 #define C irop_flag_commutative
143 #define X irop_flag_cfopcode
144 #define I irop_flag_ip_cfopcode
145 #define F irop_flag_fragile
146 #define Y irop_flag_forking
147 #define H irop_flag_highlevel
148 #define c irop_flag_constlike
149 #define K irop_flag_keep
150 #define M irop_flag_machine
154 * Compare two node attributes.
156 * @return zero if both attributes are identically
158 static int _node_cmp_attr(be_node_attr_t *a, be_node_attr_t *b) {
161 if(ARR_LEN(a->reg_data) != ARR_LEN(b->reg_data))
164 len = ARR_LEN(a->reg_data);
165 for (i = 0; i < len; ++i) {
166 if (a->reg_data[i].reg != b->reg_data[i].reg ||
167 memcmp(&a->reg_data[i].in_req, &b->reg_data[i].in_req, sizeof(b->reg_data[i].in_req)) ||
168 memcmp(&a->reg_data[i].req, &b->reg_data[i].req, sizeof(a->reg_data[i].req)))
175 static int node_cmp_attr(ir_node *a, ir_node *b) {
176 be_node_attr_t *a_attr = get_irn_attr(a);
177 be_node_attr_t *b_attr = get_irn_attr(b);
179 return _node_cmp_attr(a_attr, b_attr);
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)
194 return _node_cmp_attr((be_node_attr_t*) a_attr, (be_node_attr_t*) b_attr);
197 static int Return_cmp_attr(ir_node *a, ir_node *b) {
198 be_return_attr_t *a_attr = get_irn_attr(a);
199 be_return_attr_t *b_attr = get_irn_attr(b);
201 if (a_attr->num_ret_vals != b_attr->num_ret_vals)
204 return _node_cmp_attr((be_node_attr_t*) a_attr, (be_node_attr_t*) b_attr);
207 static int Stack_cmp_attr(ir_node *a, ir_node *b) {
208 be_stack_attr_t *a_attr = get_irn_attr(a);
209 be_stack_attr_t *b_attr = get_irn_attr(b);
211 if (a_attr->offset != b_attr->offset)
214 return _node_cmp_attr((be_node_attr_t*) a_attr, (be_node_attr_t*) b_attr);
217 static int Call_cmp_attr(ir_node *a, ir_node *b) {
218 be_call_attr_t *a_attr = get_irn_attr(a);
219 be_call_attr_t *b_attr = get_irn_attr(b);
221 if (a_attr->ent != b_attr->ent ||
222 a_attr->call_tp != b_attr->call_tp)
225 return _node_cmp_attr((be_node_attr_t*) a_attr, (be_node_attr_t*) b_attr);
228 static INLINE be_req_t *get_be_req(const ir_node *node, int pos)
231 be_node_attr_t *attr;
234 assert(is_be_node(node));
235 attr = get_irn_attr(node);
241 assert(idx < get_irn_arity(node));
243 assert(idx < ARR_LEN(attr->reg_data));
244 rd = &attr->reg_data[idx];
246 return pos < 0 ? &rd->req : &rd->in_req;
249 static INLINE arch_register_req_t *get_req(const ir_node *node, int pos)
251 be_req_t *bereq = get_be_req(node, pos);
255 void be_node_init(void) {
256 static int inited = 0;
263 /* Acquire all needed opcodes. */
264 beo_base = get_next_ir_opcodes(beo_Last);
266 op_be_Spill = new_ir_op(beo_base + beo_Spill, "be_Spill", op_pin_state_pinned, N, oparity_unary, 0, sizeof(be_frame_attr_t), &be_node_op_ops);
267 op_be_Reload = new_ir_op(beo_base + beo_Reload, "be_Reload", op_pin_state_pinned, N, oparity_zero, 0, sizeof(be_frame_attr_t), &be_node_op_ops);
268 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);
269 op_be_MemPerm = new_ir_op(beo_base + beo_MemPerm, "be_MemPerm", op_pin_state_pinned, N, oparity_variable, 0, sizeof(be_memperm_attr_t), &be_node_op_ops);
270 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);
271 op_be_Keep = new_ir_op(beo_base + beo_Keep, "be_Keep", op_pin_state_pinned, K, oparity_dynamic, 0, sizeof(be_node_attr_t), &be_node_op_ops);
272 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);
273 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);
274 op_be_Return = new_ir_op(beo_base + beo_Return, "be_Return", op_pin_state_pinned, X, oparity_dynamic, 0, sizeof(be_return_attr_t), &be_node_op_ops);
275 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);
276 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);
277 op_be_IncSP = new_ir_op(beo_base + beo_IncSP, "be_IncSP", op_pin_state_pinned, N, oparity_unary, 0, sizeof(be_stack_attr_t), &be_node_op_ops);
278 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);
279 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);
280 op_be_StackParam = new_ir_op(beo_base + beo_StackParam, "be_StackParam", op_pin_state_floats, N, oparity_unary, 0, sizeof(be_frame_attr_t), &be_node_op_ops);
281 op_be_FrameLoad = new_ir_op(beo_base + beo_FrameLoad, "be_FrameLoad", op_pin_state_floats, N, oparity_any, 0, sizeof(be_frame_attr_t), &be_node_op_ops);
282 op_be_FrameStore = new_ir_op(beo_base + beo_FrameStore, "be_FrameStore", op_pin_state_floats, N, oparity_any, 0, sizeof(be_frame_attr_t), &be_node_op_ops);
283 op_be_FrameAddr = new_ir_op(beo_base + beo_FrameAddr, "be_FrameAddr", op_pin_state_floats, N, oparity_unary, 0, sizeof(be_frame_attr_t), &be_node_op_ops);
284 op_be_Barrier = new_ir_op(beo_base + beo_Barrier, "be_Barrier", op_pin_state_pinned, N, oparity_dynamic, 0, sizeof(be_node_attr_t), &be_node_op_ops);
286 set_op_tag(op_be_Spill, &be_node_tag);
287 op_be_Spill->ops.node_cmp_attr = FrameAddr_cmp_attr;
288 set_op_tag(op_be_Reload, &be_node_tag);
289 op_be_Reload->ops.node_cmp_attr = FrameAddr_cmp_attr;
290 set_op_tag(op_be_Perm, &be_node_tag);
291 op_be_Perm->ops.node_cmp_attr = node_cmp_attr;
292 set_op_tag(op_be_MemPerm, &be_node_tag);
293 op_be_MemPerm->ops.node_cmp_attr = node_cmp_attr;
294 set_op_tag(op_be_Copy, &be_node_tag);
295 op_be_Copy->ops.node_cmp_attr = node_cmp_attr;
296 set_op_tag(op_be_Keep, &be_node_tag);
297 op_be_Keep->ops.node_cmp_attr = node_cmp_attr;
298 set_op_tag(op_be_CopyKeep, &be_node_tag);
299 op_be_CopyKeep->ops.node_cmp_attr = node_cmp_attr;
300 set_op_tag(op_be_Call, &be_node_tag);
301 op_be_Call->ops.node_cmp_attr = Call_cmp_attr;
302 set_op_tag(op_be_Return, &be_node_tag);
303 op_be_Return->ops.node_cmp_attr = Return_cmp_attr;
304 set_op_tag(op_be_AddSP, &be_node_tag);
305 op_be_AddSP->ops.node_cmp_attr = node_cmp_attr;
306 set_op_tag(op_be_SubSP, &be_node_tag);
307 op_be_SubSP->ops.node_cmp_attr = node_cmp_attr;
308 set_op_tag(op_be_SetSP, &be_node_tag);
309 op_be_SetSP->ops.node_cmp_attr = Stack_cmp_attr;
310 set_op_tag(op_be_IncSP, &be_node_tag);
311 op_be_IncSP->ops.node_cmp_attr = Stack_cmp_attr;
312 set_op_tag(op_be_RegParams, &be_node_tag);
313 op_be_RegParams->ops.node_cmp_attr = node_cmp_attr;
314 set_op_tag(op_be_StackParam, &be_node_tag);
315 op_be_StackParam->ops.node_cmp_attr = FrameAddr_cmp_attr;
316 set_op_tag(op_be_FrameLoad, &be_node_tag);
317 op_be_FrameLoad->ops.node_cmp_attr = FrameAddr_cmp_attr;
318 set_op_tag(op_be_FrameStore, &be_node_tag);
319 op_be_FrameStore->ops.node_cmp_attr = FrameAddr_cmp_attr;
320 set_op_tag(op_be_FrameAddr, &be_node_tag);
321 op_be_FrameAddr->ops.node_cmp_attr = FrameAddr_cmp_attr;
322 set_op_tag(op_be_Barrier, &be_node_tag);
323 op_be_Barrier->ops.node_cmp_attr = node_cmp_attr;
327 * Initializes the generic attribute of all be nodes and return ir.
329 static void *init_node_attr(ir_node *node, int max_reg_data)
331 ir_graph *irg = get_irn_irg(node);
332 struct obstack *obst = get_irg_obstack(irg);
333 be_node_attr_t *a = get_irn_attr(node);
335 memset(a, 0, sizeof(get_op_attr_size(get_irn_op(node))));
337 if(max_reg_data >= 0) {
338 a->reg_data = NEW_ARR_D(be_reg_data_t, obst, max_reg_data);
339 memset(a->reg_data, 0, max_reg_data * sizeof(a->reg_data[0]));
341 a->reg_data = NEW_ARR_F(be_reg_data_t, 0);
347 static void add_register_req(ir_node *node)
349 be_node_attr_t *a = get_irn_attr(node);
350 be_reg_data_t regreq;
351 memset(®req, 0, sizeof(regreq));
352 ARR_APP1(be_reg_data_t, a->reg_data, regreq);
355 int is_be_node(const ir_node *irn)
357 return get_op_tag(get_irn_op(irn)) == &be_node_tag;
360 be_opcode_t be_get_irn_opcode(const ir_node *irn)
362 return is_be_node(irn) ? get_irn_opcode(irn) - beo_base : beo_NoBeOp;
366 * Skip Proj nodes and return their Proj numbers.
368 * If *node is a Proj or Proj(Proj) node, skip it.
370 * @param node points to the node to be skipped
372 * @return 0 if *node was no Proj node, its Proj number else.
374 static int redir_proj(const ir_node **node)
376 const ir_node *n = *node;
381 *node = irn = get_Proj_pred(n);
383 assert(get_irn_mode(irn) == mode_T);
384 *node = get_Proj_pred(irn);
386 return get_Proj_proj(n);
392 static be_reg_data_t *retrieve_reg_data(const ir_node *node)
394 be_node_attr_t *attr;
398 pos = get_Proj_proj(node);
399 node = get_Proj_pred(node);
402 assert(is_be_node(node));
403 attr = get_irn_attr(node);
404 assert(pos >= 0 && pos < ARR_LEN(attr->reg_data) && "illegal proj number");
406 return &attr->reg_data[pos];
410 be_node_set_irn_reg(const void *_self, ir_node *irn, const arch_register_t *reg)
412 be_reg_data_t *r = retrieve_reg_data(irn);
416 ir_node *be_new_Spill(const arch_register_class_t *cls, const arch_register_class_t *cls_frame,
417 ir_graph *irg, ir_node *bl, ir_node *frame, ir_node *to_spill)
425 res = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
426 a = init_node_attr(res, 2);
430 be_node_set_reg_class(res, be_pos_Spill_frame, cls_frame);
431 be_node_set_reg_class(res, be_pos_Spill_val, cls);
435 ir_node *be_new_Reload(const arch_register_class_t *cls, const arch_register_class_t *cls_frame,
436 ir_graph *irg, ir_node *bl, ir_node *frame, ir_node *mem, ir_mode *mode)
443 res = new_ir_node(NULL, irg, bl, op_be_Reload, mode, 2, in);
445 init_node_attr(res, 2);
446 be_node_set_reg_class(res, -1, cls);
447 be_node_set_reg_class(res, be_pos_Reload_frame, cls_frame);
448 be_node_set_flags(res, -1, arch_irn_flags_rematerializable);
452 ir_node *be_get_Reload_mem(const ir_node *irn)
454 assert(be_is_Reload(irn));
455 return get_irn_n(irn, be_pos_Reload_mem);
458 ir_node *be_get_Reload_frame(const ir_node *irn)
460 assert(be_is_Reload(irn));
461 return get_irn_n(irn, be_pos_Reload_frame);
464 ir_node *be_get_Spill_val(const ir_node *irn)
466 assert(be_is_Spill(irn));
467 return get_irn_n(irn, be_pos_Spill_val);
470 ir_node *be_get_Spill_frame(const ir_node *irn)
472 assert(be_is_Spill(irn));
473 return get_irn_n(irn, be_pos_Spill_frame);
476 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
479 ir_node *irn = new_ir_node(NULL, irg, bl, op_be_Perm, mode_T, n, in);
480 init_node_attr(irn, n);
481 for(i = 0; i < n; ++i) {
482 be_node_set_reg_class(irn, i, cls);
483 be_node_set_reg_class(irn, OUT_POS(i), cls);
489 void be_Perm_reduce(ir_node *perm, int new_size, int *map)
491 ir_graph *irg = get_irn_irg(perm);
492 int n = get_irn_arity(perm);
493 be_reg_data_t *old_data = malloc(n * sizeof(old_data[0]));
494 be_node_attr_t *attr = get_irn_attr(perm);
495 ir_node **new_in = NEW_ARR_D(ir_node *, irg->obst, new_size + 1);
499 assert(be_is_Perm(perm));
500 assert(new_size <= n);
502 /* save the old register data */
503 memcpy(old_data, attr->reg_data, n * sizeof(old_data[0]));
505 /* compose the new in array and set the new register data directly in place */
506 for (i = 0; i < n; ++i) {
509 new_in[idx] = get_irn_n(perm, i);
510 attr->reg_data[idx] = old_data[i];
515 set_irn_in(perm, new_size, new_in);
518 ir_node *be_new_MemPerm(const arch_env_t *arch_env, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
521 ir_node *frame = get_irg_frame(irg);
522 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
524 const arch_register_t *sp = arch_env->isa->sp;
525 be_memperm_attr_t *attr;
528 real_in = alloca((n+1) * sizeof(real_in[0]));
530 memcpy(&real_in[1], in, n * sizeof(real_in[0]));
532 irn = new_ir_node(NULL, irg, bl, op_be_MemPerm, mode_T, n+1, real_in);
534 init_node_attr(irn, n + 1);
535 be_node_set_reg_class(irn, 0, sp->reg_class);
536 for(i = 0; i < n; ++i) {
537 be_node_set_reg_class(irn, i + 1, cls_frame);
538 be_node_set_reg_class(irn, OUT_POS(i), cls_frame);
541 attr = get_irn_attr(irn);
543 attr->in_entities = obstack_alloc(irg->obst, n * sizeof(attr->in_entities[0]));
544 memset(attr->in_entities, 0, n * sizeof(attr->in_entities[0]));
545 attr->out_entities = obstack_alloc(irg->obst, n*sizeof(attr->out_entities[0]));
546 memset(attr->out_entities, 0, n*sizeof(attr->out_entities[0]));
552 ir_node *be_new_Copy(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *op)
556 arch_register_req_t *req;
559 res = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
560 init_node_attr(res, 1);
561 be_node_set_reg_class(res, 0, cls);
562 be_node_set_reg_class(res, OUT_POS(0), cls);
564 req = get_req(res, OUT_POS(0));
566 req->type = arch_register_req_type_should_be_same;
572 ir_node *be_get_Copy_op(const ir_node *cpy) {
573 return get_irn_n(cpy, be_pos_Copy_op);
576 void be_set_Copy_op(ir_node *cpy, ir_node *op) {
577 set_irn_n(cpy, be_pos_Copy_op, op);
580 ir_node *be_new_Keep(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
585 res = new_ir_node(NULL, irg, bl, op_be_Keep, mode_ANY, -1, NULL);
586 init_node_attr(res, -1);
588 for(i = 0; i < n; ++i) {
589 add_irn_n(res, in[i]);
590 add_register_req(res);
591 be_node_set_reg_class(res, i, cls);
598 void be_Keep_add_node(ir_node *keep, const arch_register_class_t *cls, ir_node *node)
602 assert(be_is_Keep(keep));
603 n = add_irn_n(keep, node);
604 add_register_req(keep);
605 be_node_set_reg_class(keep, n, cls);
608 /* creates a be_Call */
609 ir_node *be_new_Call(dbg_info *dbg, ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *sp, ir_node *ptr,
610 int n_outs, int n, ir_node *in[], ir_type *call_tp)
613 int real_n = be_pos_Call_first_arg + n;
617 NEW_ARR_A(ir_node *, real_in, real_n);
618 real_in[be_pos_Call_mem] = mem;
619 real_in[be_pos_Call_sp] = sp;
620 real_in[be_pos_Call_ptr] = ptr;
621 memcpy(&real_in[be_pos_Call_first_arg], in, n * sizeof(in[0]));
623 irn = new_ir_node(dbg, irg, bl, op_be_Call, mode_T, real_n, real_in);
624 a = init_node_attr(irn, (n_outs > real_n ? n_outs : real_n));
626 a->call_tp = call_tp;
630 /* Gets the call entity or NULL if this is no static call. */
631 ir_entity *be_Call_get_entity(const ir_node *call) {
632 be_call_attr_t *a = get_irn_attr(call);
633 assert(be_is_Call(call));
637 /* Sets the call entity. */
638 void be_Call_set_entity(ir_node *call, ir_entity *ent) {
639 be_call_attr_t *a = get_irn_attr(call);
640 assert(be_is_Call(call));
644 /* Gets the call type. */
645 ir_type *be_Call_get_type(ir_node *call) {
646 be_call_attr_t *a = get_irn_attr(call);
647 assert(be_is_Call(call));
651 /* Sets the call type. */
652 void be_Call_set_type(ir_node *call, ir_type *call_tp) {
653 be_call_attr_t *a = get_irn_attr(call);
654 assert(be_is_Call(call));
655 a->call_tp = call_tp;
658 /* Construct a new be_Return. */
659 ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *block, int n_res,
660 int n, ir_node *in[])
666 res = new_ir_node(dbg, irg, block, op_be_Return, mode_X, -1, NULL);
667 init_node_attr(res, -1);
668 for(i = 0; i < n; ++i) {
669 add_irn_n(res, in[i]);
670 add_register_req(res);
673 a = get_irn_attr(res);
674 a->num_ret_vals = n_res;
679 /* Returns the number of real returns values */
680 int be_Return_get_n_rets(ir_node *ret)
682 be_return_attr_t *a = get_irn_attr(ret);
683 return a->num_ret_vals;
686 int be_Return_append_node(ir_node *ret, ir_node *node)
690 pos = add_irn_n(ret, node);
691 add_register_req(ret);
696 ir_node *be_new_IncSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, int offset)
703 irn = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode, sizeof(in) / sizeof(in[0]), in);
704 a = init_node_attr(irn, 1);
707 be_node_set_flags(irn, -1, arch_irn_flags_ignore | arch_irn_flags_modify_sp);
709 /* Set output constraint to stack register. */
710 be_node_set_reg_class(irn, 0, sp->reg_class);
711 be_set_constr_single_reg(irn, BE_OUT_POS(0), sp);
712 be_node_set_irn_reg(NULL, irn, sp);
717 ir_node *be_new_AddSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, ir_node *sz)
721 ir_node *in[be_pos_AddSP_last];
723 in[be_pos_AddSP_old_sp] = old_sp;
724 in[be_pos_AddSP_size] = sz;
726 irn = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T, be_pos_AddSP_last, in);
727 a = init_node_attr(irn, be_pos_AddSP_last);
729 be_node_set_flags(irn, OUT_POS(pn_be_AddSP_res), arch_irn_flags_ignore | arch_irn_flags_modify_sp);
731 /* Set output constraint to stack register. */
732 be_set_constr_single_reg(irn, be_pos_AddSP_old_sp, sp);
733 be_node_set_reg_class(irn, be_pos_AddSP_size, arch_register_get_class(sp));
734 be_set_constr_single_reg(irn, OUT_POS(pn_be_AddSP_res), sp);
735 a->reg_data[pn_be_AddSP_res].reg = sp;
740 ir_node *be_new_SubSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, ir_node *sz)
744 ir_node *in[be_pos_SubSP_last];
746 in[be_pos_SubSP_old_sp] = old_sp;
747 in[be_pos_SubSP_size] = sz;
749 irn = new_ir_node(NULL, irg, bl, op_be_SubSP, mode_T, be_pos_SubSP_last, in);
750 a = init_node_attr(irn, be_pos_SubSP_last);
752 be_node_set_flags(irn, OUT_POS(pn_be_SubSP_res), arch_irn_flags_ignore | arch_irn_flags_modify_sp);
754 /* Set output constraint to stack register. */
755 be_set_constr_single_reg(irn, be_pos_SubSP_old_sp, sp);
756 be_node_set_reg_class(irn, be_pos_SubSP_size, arch_register_get_class(sp));
757 be_set_constr_single_reg(irn, OUT_POS(pn_be_SubSP_res), sp);
758 a->reg_data[pn_be_SubSP_res].reg = sp;
763 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)
772 irn = new_ir_node(NULL, irg, bl, op_be_SetSP, get_irn_mode(old_sp), 3, in);
773 a = init_node_attr(irn, 3);
775 be_node_set_flags(irn, OUT_POS(0), arch_irn_flags_ignore | arch_irn_flags_modify_sp);
777 /* Set output constraint to stack register. */
778 be_set_constr_single_reg(irn, OUT_POS(0), sp);
779 be_node_set_reg_class(irn, be_pos_AddSP_size, sp->reg_class);
780 be_node_set_reg_class(irn, be_pos_AddSP_old_sp, sp->reg_class);
785 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, ir_entity *ent)
791 in[0] = frame_pointer;
792 irn = new_ir_node(NULL, irg, bl, op_be_StackParam, mode, 1, in);
793 a = init_node_attr(irn, 1);
796 be_node_set_reg_class(irn, 0, cls_frame);
797 be_node_set_reg_class(irn, OUT_POS(0), cls);
798 be_node_set_flags(irn, OUT_POS(0), arch_irn_flags_rematerializable);
803 ir_node *be_new_RegParams(ir_graph *irg, ir_node *bl, int n_outs)
808 res = new_ir_node(NULL, irg, bl, op_be_RegParams, mode_T, 0, NULL);
809 init_node_attr(res, -1);
810 for(i = 0; i < n_outs; ++i)
811 add_register_req(res);
816 ir_node *be_RegParams_append_out_reg(ir_node *regparams,
817 const arch_env_t *arch_env,
818 const arch_register_t *reg)
820 ir_graph *irg = get_irn_irg(regparams);
821 ir_node *block = get_nodes_block(regparams);
822 be_node_attr_t *attr = get_irn_attr(regparams);
823 const arch_register_class_t *cls = arch_register_get_class(reg);
824 ir_mode *mode = arch_register_class_mode(cls);
825 int n = ARR_LEN(attr->reg_data);
828 assert(be_is_RegParams(regparams));
829 proj = new_r_Proj(irg, block, regparams, mode, n);
830 add_register_req(regparams);
831 be_set_constr_single_reg(regparams, BE_OUT_POS(n), reg);
832 arch_set_irn_register(arch_env, proj, reg);
834 /* TODO decide, whether we need to set ignore/modify sp flags here? */
839 ir_node *be_new_FrameLoad(const arch_register_class_t *cls_frame, const arch_register_class_t *cls_data,
840 ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *frame, ir_entity *ent)
848 irn = new_ir_node(NULL, irg, bl, op_be_FrameLoad, mode_T, 2, in);
849 a = init_node_attr(irn, 3);
852 be_node_set_reg_class(irn, 1, cls_frame);
853 be_node_set_reg_class(irn, OUT_POS(pn_Load_res), cls_data);
857 ir_node *be_new_FrameStore(const arch_register_class_t *cls_frame, const arch_register_class_t *cls_data,
858 ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *frame, ir_node *data, ir_entity *ent)
867 irn = new_ir_node(NULL, irg, bl, op_be_FrameStore, mode_T, 3, in);
868 a = init_node_attr(irn, 3);
871 be_node_set_reg_class(irn, 1, cls_frame);
872 be_node_set_reg_class(irn, 2, cls_data);
876 ir_node *be_new_FrameAddr(const arch_register_class_t *cls_frame, ir_graph *irg, ir_node *bl, ir_node *frame, ir_entity *ent)
883 irn = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
884 a = init_node_attr(irn, 1);
887 be_node_set_reg_class(irn, 0, cls_frame);
888 be_node_set_reg_class(irn, OUT_POS(0), cls_frame);
890 return optimize_node(irn);
893 ir_node *be_get_FrameAddr_frame(ir_node *node) {
894 assert(be_is_FrameAddr(node));
895 return get_irn_n(node, be_pos_FrameAddr_ptr);
898 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)
901 ir_node **in = (ir_node **) alloca((n + 1) * sizeof(in[0]));
904 memcpy(&in[1], in_keep, n * sizeof(in[0]));
905 irn = new_ir_node(NULL, irg, bl, op_be_CopyKeep, mode, n + 1, in);
906 init_node_attr(irn, n + 1);
907 be_node_set_reg_class(irn, OUT_POS(0), cls);
908 be_node_set_reg_class(irn, 0, cls);
913 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)
918 return be_new_CopyKeep(cls, irg, bl, src, 1, in, mode);
921 ir_node *be_get_CopyKeep_op(const ir_node *cpy) {
922 return get_irn_n(cpy, be_pos_CopyKeep_op);
925 void be_set_CopyKeep_op(ir_node *cpy, ir_node *op) {
926 set_irn_n(cpy, be_pos_CopyKeep_op, op);
929 ir_node *be_new_Barrier(ir_graph *irg, ir_node *bl, int n, ir_node *in[])
934 res = new_ir_node(NULL, irg, bl, op_be_Barrier, mode_T, -1, NULL);
935 init_node_attr(res, -1);
936 for(i = 0; i < n; ++i) {
937 add_irn_n(res, in[i]);
938 add_register_req(res);
944 ir_node *be_Barrier_append_node(ir_node *barrier, ir_node *node)
946 ir_graph *irg = get_irn_irg(barrier);
947 ir_node *block = get_nodes_block(barrier);
948 ir_mode *mode = get_irn_mode(node);
949 int n = add_irn_n(barrier, node);
951 ir_node *proj = new_r_Proj(irg, block, barrier, mode, n);
952 add_register_req(barrier);
957 int be_is_Spill (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Spill ; }
958 int be_is_Reload (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Reload ; }
959 int be_is_Copy (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Copy ; }
960 int be_is_CopyKeep (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_CopyKeep ; }
961 int be_is_Perm (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Perm ; }
962 int be_is_MemPerm (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_MemPerm ; }
963 int be_is_Keep (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Keep ; }
964 int be_is_Call (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Call ; }
965 int be_is_Return (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Return ; }
966 int be_is_IncSP (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_IncSP ; }
967 int be_is_SetSP (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_SetSP ; }
968 int be_is_AddSP (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_AddSP ; }
969 int be_is_SubSP (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_SubSP ; }
970 int be_is_RegParams (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_RegParams ; }
971 int be_is_StackParam (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_StackParam ; }
972 int be_is_FrameAddr (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameAddr ; }
973 int be_is_FrameLoad (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameLoad ; }
974 int be_is_FrameStore (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameStore ; }
975 int be_is_Barrier (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Barrier ; }
977 int be_has_frame_entity(const ir_node *irn)
979 switch(be_get_irn_opcode(irn)) {
992 ir_entity *be_get_frame_entity(const ir_node *irn)
994 if (be_has_frame_entity(irn)) {
995 be_frame_attr_t *a = get_irn_attr(irn);
1001 int be_get_frame_offset(const ir_node *irn)
1003 assert(is_be_node(irn));
1004 if (be_has_frame_entity(irn)) {
1005 be_frame_attr_t *a = get_irn_attr(irn);
1011 void be_set_MemPerm_in_entity(const ir_node *irn, int n, ir_entity *ent)
1013 be_memperm_attr_t *attr = get_irn_attr(irn);
1015 assert(be_is_MemPerm(irn));
1016 assert(n < be_get_MemPerm_entity_arity(irn));
1018 attr->in_entities[n] = ent;
1021 ir_entity* be_get_MemPerm_in_entity(const ir_node* irn, int n)
1023 be_memperm_attr_t *attr = get_irn_attr(irn);
1025 assert(be_is_MemPerm(irn));
1026 assert(n < be_get_MemPerm_entity_arity(irn));
1028 return attr->in_entities[n];
1031 void be_set_MemPerm_out_entity(const ir_node *irn, int n, ir_entity *ent)
1033 be_memperm_attr_t *attr = get_irn_attr(irn);
1035 assert(be_is_MemPerm(irn));
1036 assert(n < be_get_MemPerm_entity_arity(irn));
1038 attr->out_entities[n] = ent;
1041 ir_entity* be_get_MemPerm_out_entity(const ir_node* irn, int n)
1043 be_memperm_attr_t *attr = get_irn_attr(irn);
1045 assert(be_is_MemPerm(irn));
1046 assert(n < be_get_MemPerm_entity_arity(irn));
1048 return attr->out_entities[n];
1051 int be_get_MemPerm_entity_arity(const ir_node *irn)
1053 return get_irn_arity(irn) - 1;
1056 void be_set_constr_single_reg(ir_node *node, int pos, const arch_register_t *reg)
1058 arch_register_req_t *req = get_req(node, pos);
1059 const arch_register_class_t *cls = arch_register_get_class(reg);
1060 ir_graph *irg = get_irn_irg(node);
1061 struct obstack *obst = get_irg_obstack(irg);
1062 unsigned *limited_bitset;
1064 assert(req->cls == NULL || req->cls == cls);
1065 assert(! (req->type & arch_register_req_type_limited));
1066 assert(req->limited == NULL);
1068 limited_bitset = rbitset_obstack_alloc(obst, arch_register_class_n_regs(cls));
1069 rbitset_set(limited_bitset, arch_register_get_index(reg));
1072 req->type |= arch_register_req_type_limited;
1073 req->limited = limited_bitset;
1076 void be_set_constr_limited(ir_node *node, int pos, const arch_register_req_t *req)
1078 ir_graph *irg = get_irn_irg(node);
1079 struct obstack *obst = get_irg_obstack(irg);
1080 arch_register_req_t *r = get_req(node, pos);
1082 assert(arch_register_req_is(req, limited));
1083 assert(! (req->type & (arch_register_req_type_should_be_same | arch_register_req_type_should_be_different)));
1084 memcpy(r, req, sizeof(r[0]));
1085 r->limited = rbitset_duplicate_obstack_alloc(obst, req->limited, req->cls->n_regs);
1088 void be_node_set_flags(ir_node *irn, int pos, arch_irn_flags_t flags)
1090 be_req_t *bereq = get_be_req(irn, pos);
1091 bereq->flags = flags;
1094 void be_node_set_reg_class(ir_node *irn, int pos, const arch_register_class_t *cls)
1096 arch_register_req_t *req = get_req(irn, pos);
1101 req->type = arch_register_req_type_none;
1102 } else if (req->type == arch_register_req_type_none) {
1103 req->type = arch_register_req_type_normal;
1107 void be_node_set_req_type(ir_node *irn, int pos, arch_register_req_type_t type)
1109 arch_register_req_t *req = get_req(irn, pos);
1113 ir_node *be_get_IncSP_pred(ir_node *irn) {
1114 assert(be_is_IncSP(irn));
1115 return get_irn_n(irn, 0);
1118 void be_set_IncSP_pred(ir_node *incsp, ir_node *pred) {
1119 assert(be_is_IncSP(incsp));
1120 set_irn_n(incsp, 0, pred);
1123 ir_node *be_get_IncSP_mem(ir_node *irn) {
1124 assert(be_is_IncSP(irn));
1125 return get_irn_n(irn, 1);
1128 void be_set_IncSP_offset(ir_node *irn, int offset)
1130 be_stack_attr_t *a = get_irn_attr(irn);
1131 assert(be_is_IncSP(irn));
1135 int be_get_IncSP_offset(const ir_node *irn)
1137 be_stack_attr_t *a = get_irn_attr(irn);
1138 assert(be_is_IncSP(irn));
1142 ir_node *be_spill(const arch_env_t *arch_env, ir_node *irn)
1144 ir_node *bl = get_nodes_block(irn);
1145 ir_graph *irg = get_irn_irg(bl);
1146 ir_node *frame = get_irg_frame(irg);
1147 const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, irn, -1);
1148 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
1151 spill = be_new_Spill(cls, cls_frame, irg, bl, frame, irn);
1155 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)
1158 ir_node *bl = is_Block(insert) ? insert : get_nodes_block(insert);
1159 ir_graph *irg = get_irn_irg(bl);
1160 ir_node *frame = get_irg_frame(irg);
1161 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
1163 assert(be_is_Spill(spill) || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
1165 reload = be_new_Reload(cls, cls_frame, irg, bl, frame, spill, mode);
1167 if (is_Block(insert)) {
1168 insert = sched_skip(insert, 0, sched_skip_cf_predicator, (void *) arch_env);
1169 sched_add_after(insert, reload);
1171 sched_add_before(insert, reload);
1179 | _ \ ___ __ _ | _ \ ___ __ _ ___
1180 | |_) / _ \/ _` | | |_) / _ \/ _` / __|
1181 | _ < __/ (_| | | _ < __/ (_| \__ \
1182 |_| \_\___|\__, | |_| \_\___|\__, |___/
1189 arch_register_req_t *get_out_reg_req(const ir_node *irn, int out_pos)
1191 const be_node_attr_t *a = get_irn_attr(irn);
1193 if(out_pos >= ARR_LEN(a->reg_data)) {
1194 return arch_no_register_req;
1197 return &a->reg_data[out_pos].req.req;
1201 arch_register_req_t *get_in_reg_req(const ir_node *irn, int pos)
1203 const be_node_attr_t *a = get_irn_attr(irn);
1205 if(pos >= get_irn_arity(irn) || pos >= ARR_LEN(a->reg_data))
1206 return arch_no_register_req;
1208 return &a->reg_data[pos].in_req.req;
1211 static const arch_register_req_t *
1212 be_node_get_irn_reg_req(const void *self, const ir_node *irn, int pos)
1217 if (get_irn_mode(irn) == mode_T)
1218 return arch_no_register_req;
1220 out_pos = redir_proj((const ir_node **)&irn);
1221 assert(is_be_node(irn));
1222 return get_out_reg_req(irn, out_pos);
1223 } else if (is_be_node(irn)) {
1225 * For spills and reloads, we return "none" as requirement for frame
1226 * pointer, so every input is ok. Some backends need this (e.g. STA).
1228 if ((be_is_Spill(irn) && pos == be_pos_Spill_frame) ||
1229 (be_is_Reload(irn) && pos == be_pos_Reload_frame))
1230 return arch_no_register_req;
1232 return get_in_reg_req(irn, pos);
1235 return arch_no_register_req;
1238 const arch_register_t *
1239 be_node_get_irn_reg(const void *_self, const ir_node *irn)
1243 if (get_irn_mode(irn) == mode_T)
1245 r = retrieve_reg_data(irn);
1249 static arch_irn_class_t be_node_classify(const void *_self, const ir_node *irn)
1251 redir_proj((const ir_node **) &irn);
1253 switch(be_get_irn_opcode(irn)) {
1254 #define XXX(a,b) case beo_ ## a: return arch_irn_class_ ## b
1256 XXX(Reload, reload);
1259 XXX(Return, branch);
1260 XXX(StackParam, stackparam);
1263 return arch_irn_class_normal;
1269 static arch_irn_flags_t be_node_get_flags(const void *_self, const ir_node *node)
1275 pos = OUT_POS(get_Proj_proj(node));
1276 node = skip_Proj_const(node);
1279 bereq = get_be_req(node, pos);
1281 return bereq->flags;
1284 static ir_entity *be_node_get_frame_entity(const void *self, const ir_node *irn)
1286 return be_get_frame_entity(irn);
1289 static void be_node_set_frame_entity(const void *self, ir_node *irn, ir_entity *ent)
1293 assert(be_has_frame_entity(irn));
1295 a = get_irn_attr(irn);
1299 static void be_node_set_frame_offset(const void *self, ir_node *irn, int offset)
1301 if(be_has_frame_entity(irn)) {
1302 be_frame_attr_t *a = get_irn_attr(irn);
1307 static int be_node_get_sp_bias(const void *self, const ir_node *irn)
1309 return be_is_IncSP(irn) ? be_get_IncSP_offset(irn) : 0;
1313 ___ ____ _ _ _ _ _ _
1314 |_ _| _ \| \ | | | | | | __ _ _ __ __| | | ___ _ __
1315 | || |_) | \| | | |_| |/ _` | '_ \ / _` | |/ _ \ '__|
1316 | || _ <| |\ | | _ | (_| | | | | (_| | | __/ |
1317 |___|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
1321 static const arch_irn_ops_if_t be_node_irn_ops_if = {
1322 be_node_get_irn_reg_req,
1323 be_node_set_irn_reg,
1324 be_node_get_irn_reg,
1327 be_node_get_frame_entity,
1328 be_node_set_frame_entity,
1329 be_node_set_frame_offset,
1330 be_node_get_sp_bias,
1331 NULL, /* get_inverse */
1332 NULL, /* get_op_estimated_cost */
1333 NULL, /* possible_memory_operand */
1334 NULL, /* perform_memory_operand */
1337 static const arch_irn_ops_t be_node_irn_ops = {
1341 const void *be_node_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn)
1343 redir_proj((const ir_node **) &irn);
1344 return is_be_node(irn) ? &be_node_irn_ops : NULL;
1347 const arch_irn_handler_t be_node_irn_handler = {
1352 ____ _ _ ___ ____ _ _ _ _ _ _
1353 | _ \| |__ (_) |_ _| _ \| \ | | | | | | __ _ _ __ __| | | ___ _ __
1354 | |_) | '_ \| | | || |_) | \| | | |_| |/ _` | '_ \ / _` | |/ _ \ '__|
1355 | __/| | | | | | || _ <| |\ | | _ | (_| | | | | (_| | | __/ |
1356 |_| |_| |_|_| |___|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
1361 const arch_register_t *reg;
1362 arch_register_req_t req;
1363 arch_irn_flags_t flags;
1367 arch_irn_handler_t irn_handler;
1368 arch_irn_ops_t irn_ops;
1369 const arch_env_t *arch_env;
1373 #define get_phi_handler_from_handler(h) container_of(h, phi_handler_t, irn_handler)
1374 #define get_phi_handler_from_ops(h) container_of(h, phi_handler_t, irn_ops)
1377 const void *phi_get_irn_ops(const arch_irn_handler_t *handler,
1380 const phi_handler_t *h;
1381 if(!is_Phi(irn) || !mode_is_datab(get_irn_mode(irn)))
1384 h = get_phi_handler_from_handler(handler);
1389 phi_attr_t *get_Phi_attr(const phi_handler_t *handler, const ir_node *phi)
1391 phi_attr_t *attr = pmap_get(handler->phi_attrs, (void*) phi);
1393 ir_graph *irg = get_irn_irg(phi);
1394 struct obstack *obst = get_irg_obstack(irg);
1395 attr = obstack_alloc(obst, sizeof(attr[0]));
1396 memset(attr, 0, sizeof(attr[0]));
1397 pmap_insert(handler->phi_attrs, phi, attr);
1404 * Get register class of a Phi.
1407 const arch_register_req_t *get_Phi_reg_req_recursive(const phi_handler_t *h,
1411 int n = get_irn_arity(phi);
1415 if(*visited && pset_find_ptr(*visited, phi))
1418 for(i = 0; i < n; ++i) {
1419 op = get_irn_n(phi, i);
1420 /* Matze: don't we unnecessary constraint our phis with this?
1421 * we only need to take the regclass IMO*/
1423 return arch_get_register_req(h->arch_env, op, BE_OUT_POS(0));
1427 * The operands of that Phi were all Phis themselves.
1428 * We have to start a DFS for a non-Phi argument now.
1431 *visited = pset_new_ptr(16);
1433 pset_insert_ptr(*visited, phi);
1435 for(i = 0; i < n; ++i) {
1436 const arch_register_req_t *req;
1437 op = get_irn_n(phi, i);
1438 req = get_Phi_reg_req_recursive(h, op, visited);
1447 const arch_register_req_t *phi_get_irn_reg_req(const void *self,
1448 const ir_node *irn, int pos)
1450 phi_handler_t *phi_handler = get_phi_handler_from_ops(self);
1453 if(!mode_is_datab(get_irn_mode(irn)))
1454 return arch_no_register_req;
1456 attr = get_Phi_attr(phi_handler, irn);
1458 if(attr->req.type == arch_register_req_type_none) {
1459 pset *visited = NULL;
1460 const arch_register_req_t *req;
1461 req = get_Phi_reg_req_recursive(phi_handler, irn, &visited);
1463 memcpy(&attr->req, req, sizeof(req[0]));
1464 assert(attr->req.cls != NULL);
1465 attr->req.type = arch_register_req_type_normal;
1474 void be_set_phi_reg_req(const arch_env_t *arch_env, ir_node *node,
1475 const arch_register_req_t *req)
1477 const arch_irn_ops_t *ops = arch_get_irn_ops(arch_env, node);
1478 const phi_handler_t *handler = get_phi_handler_from_ops(ops);
1481 assert(mode_is_datab(get_irn_mode(node)));
1483 attr = get_Phi_attr(handler, node);
1484 memcpy(&attr->req, req, sizeof(req[0]));
1487 void be_set_phi_flags(const arch_env_t *arch_env, ir_node *node,
1488 arch_irn_flags_t flags)
1490 const arch_irn_ops_t *ops = arch_get_irn_ops(arch_env, node);
1491 const phi_handler_t *handler = get_phi_handler_from_ops(ops);
1494 assert(mode_is_datab(get_irn_mode(node)));
1496 attr = get_Phi_attr(handler, node);
1497 attr->flags = flags;
1501 void phi_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg)
1503 phi_handler_t *h = get_phi_handler_from_ops(self);
1504 phi_attr_t *attr = get_Phi_attr(h, irn);
1509 const arch_register_t *phi_get_irn_reg(const void *self, const ir_node *irn)
1511 phi_handler_t *h = get_phi_handler_from_ops(self);
1512 phi_attr_t *attr = get_Phi_attr(h, irn);
1517 arch_irn_class_t phi_classify(const void *self, const ir_node *irn)
1519 return arch_irn_class_normal;
1523 arch_irn_flags_t phi_get_flags(const void *self, const ir_node *irn)
1525 phi_handler_t *h = get_phi_handler_from_ops(self);
1526 phi_attr_t *attr = get_Phi_attr(h, irn);
1531 ir_entity *phi_get_frame_entity(const void *_self, const ir_node *irn)
1537 void phi_set_frame_entity(const void *_self, ir_node *irn, ir_entity *ent)
1543 void phi_set_frame_offset(const void *_self, ir_node *irn, int bias)
1549 int phi_get_sp_bias(const void* self, const ir_node *irn)
1555 const arch_irn_ops_if_t phi_irn_ops = {
1556 phi_get_irn_reg_req,
1561 phi_get_frame_entity,
1562 phi_set_frame_entity,
1563 phi_set_frame_offset,
1565 NULL, /* get_inverse */
1566 NULL, /* get_op_estimated_cost */
1567 NULL, /* possible_memory_operand */
1568 NULL, /* perform_memory_operand */
1571 arch_irn_handler_t *be_phi_handler_new(const arch_env_t *arch_env)
1573 phi_handler_t *h = xmalloc(sizeof(h[0]));
1574 h->irn_handler.get_irn_ops = phi_get_irn_ops;
1575 h->irn_ops.impl = &phi_irn_ops;
1576 h->arch_env = arch_env;
1577 h->phi_attrs = pmap_create();
1578 return (arch_irn_handler_t *) h;
1581 void be_phi_handler_free(arch_irn_handler_t *handler)
1583 phi_handler_t *h = (void *) handler;
1584 pmap_destroy(h->phi_attrs);
1588 void be_phi_handler_reset(arch_irn_handler_t *handler)
1590 phi_handler_t *h = get_phi_handler_from_handler(handler);
1592 pmap_destroy(h->phi_attrs);
1593 h->phi_attrs = pmap_create();
1598 | \ | | ___ __| | ___ | _ \ _ _ _ __ ___ _ __ (_)_ __ __ _
1599 | \| |/ _ \ / _` |/ _ \ | | | | | | | '_ ` _ \| '_ \| | '_ \ / _` |
1600 | |\ | (_) | (_| | __/ | |_| | |_| | | | | | | |_) | | | | | (_| |
1601 |_| \_|\___/ \__,_|\___| |____/ \__,_|_| |_| |_| .__/|_|_| |_|\__, |
1606 * Dumps a register requirement to a file.
1608 static void dump_node_req(FILE *f, int idx, const arch_register_req_t *req,
1609 const ir_node *node)
1611 int did_something = 0;
1613 const char *prefix = buf;
1615 snprintf(buf, sizeof(buf), "#%d ", idx);
1616 buf[sizeof(buf) - 1] = '\0';
1621 arch_register_req_format(tmp, sizeof(tmp), req, node);
1622 fprintf(f, "%s", tmp);
1631 * Dumps node register requirements to a file.
1633 static void dump_node_reqs(FILE *f, ir_node *node)
1636 be_node_attr_t *a = get_irn_attr(node);
1637 int len = ARR_LEN(a->reg_data);
1639 fprintf(f, "registers: \n");
1640 for(i = 0; i < len; ++i) {
1641 be_reg_data_t *rd = &a->reg_data[i];
1643 fprintf(f, "#%d: %s\n", i, rd->reg->name);
1646 fprintf(f, "in requirements:\n");
1647 for(i = 0; i < len; ++i) {
1648 dump_node_req(f, i, &a->reg_data[i].in_req.req, node);
1651 fprintf(f, "\nout requirements:\n");
1652 for(i = 0; i < len; ++i) {
1653 dump_node_req(f, i, &a->reg_data[i].req.req, node);
1658 * ir_op-Operation: dump a be node to file
1660 static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason)
1662 be_node_attr_t *at = get_irn_attr(irn);
1664 assert(is_be_node(irn));
1667 case dump_node_opcode_txt:
1668 fprintf(f, get_op_name(get_irn_op(irn)));
1670 case dump_node_mode_txt:
1671 fprintf(f, get_mode_name(get_irn_mode(irn)));
1673 case dump_node_nodeattr_txt:
1675 case dump_node_info_txt:
1676 dump_node_reqs(f, irn);
1678 if(be_has_frame_entity(irn)) {
1679 be_frame_attr_t *a = (be_frame_attr_t *) at;
1681 int bits = get_type_size_bits(get_entity_type(a->ent));
1682 ir_fprintf(f, "frame entity: %+F, offset 0x%x (%d), size 0x%x (%d) bits\n",
1683 a->ent, a->offset, a->offset, bits, bits);
1688 switch(be_get_irn_opcode(irn)) {
1691 be_stack_attr_t *a = (be_stack_attr_t *) at;
1692 if (a->offset == BE_STACK_FRAME_SIZE_EXPAND)
1693 fprintf(f, "offset: FRAME_SIZE\n");
1694 else if(a->offset == BE_STACK_FRAME_SIZE_SHRINK)
1695 fprintf(f, "offset: -FRAME SIZE\n");
1697 fprintf(f, "offset: %u\n", a->offset);
1702 be_call_attr_t *a = (be_call_attr_t *) at;
1705 fprintf(f, "\ncalling: %s\n", get_entity_name(a->ent));
1711 for(i = 0; i < be_get_MemPerm_entity_arity(irn); ++i) {
1712 ir_entity *in, *out;
1713 in = be_get_MemPerm_in_entity(irn, i);
1714 out = be_get_MemPerm_out_entity(irn, i);
1716 fprintf(f, "\nin[%d]: %s\n", i, get_entity_name(in));
1719 fprintf(f, "\nout[%d]: %s\n", i, get_entity_name(out));
1735 * Copies the backend specific attributes from old node to new node.
1737 static void copy_attr(const ir_node *old_node, ir_node *new_node)
1739 be_node_attr_t *old_attr = get_irn_attr(old_node);
1740 be_node_attr_t *new_attr = get_irn_attr(new_node);
1741 struct obstack *obst = get_irg_obstack(get_irn_irg(new_node));
1744 assert(is_be_node(old_node));
1745 assert(is_be_node(new_node));
1747 memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
1748 new_attr->reg_data = NULL;
1750 if(old_attr->reg_data != NULL)
1751 len = ARR_LEN(old_attr->reg_data);
1755 if(get_irn_op(old_node)->opar == oparity_dynamic
1756 || be_is_RegParams(old_node)) {
1757 new_attr->reg_data = NEW_ARR_F(be_reg_data_t, len);
1759 new_attr->reg_data = NEW_ARR_D(be_reg_data_t, obst, len);
1763 memcpy(new_attr->reg_data, old_attr->reg_data, len * sizeof(be_reg_data_t));
1764 for(i = 0; i < len; ++i) {
1765 const be_reg_data_t *rd = &old_attr->reg_data[i];
1766 be_reg_data_t *newrd = &new_attr->reg_data[i];
1767 if(arch_register_req_is(&rd->req.req, limited)) {
1768 const arch_register_req_t *req = &rd->req.req;
1769 arch_register_req_t *new_req = &newrd->req.req;
1771 = rbitset_duplicate_obstack_alloc(obst, req->limited, req->cls->n_regs);
1773 if(arch_register_req_is(&rd->in_req.req, limited)) {
1774 const arch_register_req_t *req = &rd->in_req.req;
1775 arch_register_req_t *new_req = &newrd->in_req.req;
1777 = rbitset_duplicate_obstack_alloc(obst, req->limited, req->cls->n_regs);
1783 static const ir_op_ops be_node_op_ops = {