2 * Copyright (C) 1995-2008 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"
62 #define OUT_POS(x) (-((x) + 1))
64 #define get_irn_attr(irn) get_irn_generic_attr(irn)
65 #define get_irn_attr_const(irn) get_irn_generic_attr_const(irn)
67 static unsigned be_node_tag = FOURCC('B', 'E', 'N', 'O');
70 arch_register_req_t req;
71 arch_irn_flags_t flags;
75 const arch_register_t *reg;
80 /** The generic be nodes attribute type. */
82 be_reg_data_t *reg_data;
85 /** The be_Return nodes attribute type. */
87 be_node_attr_t node_attr; /**< base attributes of every be node. */
88 int num_ret_vals; /**< number of return values */
89 unsigned pop; /**< number of bytes that should be popped */
90 int emit_pop; /**< if set, emit pop bytes, even if pop = 0 */
93 /** The be_IncSP attribute type. */
95 be_node_attr_t node_attr; /**< base attributes of every be node. */
96 int offset; /**< The offset by which the stack shall be expanded/shrinked. */
97 int align; /**< whether stack should be aligned after the
101 /** The be_Frame attribute type. */
103 be_node_attr_t node_attr; /**< base attributes of every be node. */
108 /** The be_Call attribute type. */
110 be_node_attr_t node_attr; /**< base attributes of every be node. */
111 ir_entity *ent; /**< The called entity if this is a static call. */
113 ir_type *call_tp; /**< The call type, copied from the original Call node. */
117 be_node_attr_t node_attr; /**< base attributes of every be node. */
118 ir_entity **in_entities;
119 ir_entity **out_entities;
125 ir_op *op_be_MemPerm;
128 ir_op *op_be_CopyKeep;
134 ir_op *op_be_RegParams;
135 ir_op *op_be_FrameAddr;
136 ir_op *op_be_Barrier;
139 static int beo_base = -1;
141 static const ir_op_ops be_node_op_ops;
143 #define N irop_flag_none
144 #define L irop_flag_labeled
145 #define C irop_flag_commutative
146 #define X irop_flag_cfopcode
147 #define I irop_flag_ip_cfopcode
148 #define F irop_flag_fragile
149 #define Y irop_flag_forking
150 #define H irop_flag_highlevel
151 #define c irop_flag_constlike
152 #define K irop_flag_keep
153 #define M irop_flag_uses_memory
157 * Compare two be node attributes.
159 * @return zero if both attributes are identically
161 static int _node_cmp_attr(const be_node_attr_t *a, const be_node_attr_t *b) {
164 if (ARR_LEN(a->reg_data) != ARR_LEN(b->reg_data))
167 len = ARR_LEN(a->reg_data);
168 for (i = 0; i < len; ++i) {
169 if (a->reg_data[i].reg != b->reg_data[i].reg ||
170 memcmp(&a->reg_data[i].in_req, &b->reg_data[i].in_req, sizeof(b->reg_data[i].in_req)) ||
171 memcmp(&a->reg_data[i].req, &b->reg_data[i].req, sizeof(a->reg_data[i].req)))
179 * Compare the node attributes of two be_node's.
181 * @return zero if both nodes have identically attributes
183 static int node_cmp_attr(ir_node *a, ir_node *b) {
184 const be_node_attr_t *a_attr = get_irn_attr_const(a);
185 const be_node_attr_t *b_attr = get_irn_attr_const(b);
187 return _node_cmp_attr(a_attr, b_attr);
191 * Compare the attributes of two be_FrameAddr nodes.
193 * @return zero if both nodes have identically attributes
195 static int FrameAddr_cmp_attr(ir_node *a, ir_node *b) {
196 const be_frame_attr_t *a_attr = get_irn_attr_const(a);
197 const be_frame_attr_t *b_attr = get_irn_attr_const(b);
199 if (a_attr->ent != b_attr->ent || a_attr->offset != b_attr->offset)
202 return _node_cmp_attr(&a_attr->node_attr, &b_attr->node_attr);
206 * Compare the attributes of two be_Return nodes.
208 * @return zero if both nodes have identically attributes
210 static int Return_cmp_attr(ir_node *a, ir_node *b) {
211 const be_return_attr_t *a_attr = get_irn_attr_const(a);
212 const be_return_attr_t *b_attr = get_irn_attr_const(b);
214 if (a_attr->num_ret_vals != b_attr->num_ret_vals)
216 if (a_attr->pop != b_attr->pop)
218 if (a_attr->emit_pop != b_attr->emit_pop)
221 return _node_cmp_attr(&a_attr->node_attr, &b_attr->node_attr);
225 * Compare the attributes of two be_IncSP nodes.
227 * @return zero if both nodes have identically attributes
229 static int IncSP_cmp_attr(ir_node *a, ir_node *b) {
230 const be_incsp_attr_t *a_attr = get_irn_attr_const(a);
231 const be_incsp_attr_t *b_attr = get_irn_attr_const(b);
233 if (a_attr->offset != b_attr->offset)
236 return _node_cmp_attr(&a_attr->node_attr, &b_attr->node_attr);
240 * Compare the attributes of two be_Call nodes.
242 * @return zero if both nodes have identically attributes
244 static int Call_cmp_attr(ir_node *a, ir_node *b) {
245 const be_call_attr_t *a_attr = get_irn_attr_const(a);
246 const be_call_attr_t *b_attr = get_irn_attr_const(b);
248 if (a_attr->ent != b_attr->ent ||
249 a_attr->call_tp != b_attr->call_tp)
252 return _node_cmp_attr(&a_attr->node_attr, &b_attr->node_attr);
255 static INLINE be_req_t *get_be_req(const ir_node *node, int pos)
258 const be_node_attr_t *attr;
261 assert(is_be_node(node));
262 attr = get_irn_attr_const(node);
268 assert(idx < get_irn_arity(node));
270 assert(idx < ARR_LEN(attr->reg_data));
271 rd = &attr->reg_data[idx];
273 return pos < 0 ? &rd->req : &rd->in_req;
276 static INLINE arch_register_req_t *get_req(const ir_node *node, int pos)
278 be_req_t *bereq = get_be_req(node, pos);
282 void be_node_init(void) {
283 static int inited = 0;
290 /* Acquire all needed opcodes. */
291 beo_base = get_next_ir_opcodes(beo_Last);
293 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);
294 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);
295 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);
296 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);
297 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);
298 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);
299 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);
300 op_be_Call = new_ir_op(beo_base + beo_Call, "be_Call", op_pin_state_pinned, F|M, oparity_variable, 0, sizeof(be_call_attr_t), &be_node_op_ops);
301 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);
302 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);
303 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);
304 op_be_IncSP = new_ir_op(beo_base + beo_IncSP, "be_IncSP", op_pin_state_pinned, N, oparity_unary, 0, sizeof(be_incsp_attr_t), &be_node_op_ops);
305 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);
306 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);
307 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);
308 op_be_Unwind = new_ir_op(beo_base + beo_Unwind, "be_Unwind", op_pin_state_pinned, X, oparity_zero, 0, sizeof(be_node_attr_t), &be_node_op_ops);
310 set_op_tag(op_be_Spill, &be_node_tag);
311 op_be_Spill->ops.node_cmp_attr = FrameAddr_cmp_attr;
312 set_op_tag(op_be_Reload, &be_node_tag);
313 op_be_Reload->ops.node_cmp_attr = FrameAddr_cmp_attr;
314 set_op_tag(op_be_Perm, &be_node_tag);
315 op_be_Perm->ops.node_cmp_attr = node_cmp_attr;
316 set_op_tag(op_be_MemPerm, &be_node_tag);
317 op_be_MemPerm->ops.node_cmp_attr = node_cmp_attr;
318 set_op_tag(op_be_Copy, &be_node_tag);
319 op_be_Copy->ops.node_cmp_attr = node_cmp_attr;
320 set_op_tag(op_be_Keep, &be_node_tag);
321 op_be_Keep->ops.node_cmp_attr = node_cmp_attr;
322 set_op_tag(op_be_CopyKeep, &be_node_tag);
323 op_be_CopyKeep->ops.node_cmp_attr = node_cmp_attr;
324 set_op_tag(op_be_Call, &be_node_tag);
325 op_be_Call->ops.node_cmp_attr = Call_cmp_attr;
326 set_op_tag(op_be_Return, &be_node_tag);
327 op_be_Return->ops.node_cmp_attr = Return_cmp_attr;
328 set_op_tag(op_be_AddSP, &be_node_tag);
329 op_be_AddSP->ops.node_cmp_attr = node_cmp_attr;
330 set_op_tag(op_be_SubSP, &be_node_tag);
331 op_be_SubSP->ops.node_cmp_attr = node_cmp_attr;
332 set_op_tag(op_be_IncSP, &be_node_tag);
333 op_be_IncSP->ops.node_cmp_attr = IncSP_cmp_attr;
334 set_op_tag(op_be_RegParams, &be_node_tag);
335 op_be_RegParams->ops.node_cmp_attr = node_cmp_attr;
336 set_op_tag(op_be_FrameAddr, &be_node_tag);
337 op_be_FrameAddr->ops.node_cmp_attr = FrameAddr_cmp_attr;
338 set_op_tag(op_be_Barrier, &be_node_tag);
339 op_be_Barrier->ops.node_cmp_attr = node_cmp_attr;
340 set_op_tag(op_be_Unwind, &be_node_tag);
341 op_be_Unwind->ops.node_cmp_attr = node_cmp_attr;
345 * Initializes the generic attribute of all be nodes and return ir.
347 static void *init_node_attr(ir_node *node, int max_reg_data)
349 ir_graph *irg = get_irn_irg(node);
350 struct obstack *obst = get_irg_obstack(irg);
351 be_node_attr_t *a = get_irn_attr(node);
353 memset(a, 0, sizeof(get_op_attr_size(get_irn_op(node))));
355 if(max_reg_data >= 0) {
356 a->reg_data = NEW_ARR_D(be_reg_data_t, obst, max_reg_data);
357 memset(a->reg_data, 0, max_reg_data * sizeof(a->reg_data[0]));
359 a->reg_data = NEW_ARR_F(be_reg_data_t, 0);
365 static void add_register_req(ir_node *node)
367 be_node_attr_t *a = get_irn_attr(node);
368 be_reg_data_t regreq;
369 memset(®req, 0, sizeof(regreq));
370 ARR_APP1(be_reg_data_t, a->reg_data, regreq);
373 int is_be_node(const ir_node *irn)
375 return get_op_tag(get_irn_op(irn)) == &be_node_tag;
378 be_opcode_t be_get_irn_opcode(const ir_node *irn)
380 return is_be_node(irn) ? (be_opcode_t) get_irn_opcode(irn) - beo_base : beo_NoBeOp;
384 * Skip Proj nodes and return their Proj numbers.
386 * If *node is a Proj or Proj(Proj) node, skip it.
388 * @param node points to the node to be skipped
390 * @return 0 if *node was no Proj node, its Proj number else.
392 static int redir_proj(const ir_node **node)
394 const ir_node *n = *node;
399 *node = irn = get_Proj_pred(n);
401 assert(get_irn_mode(irn) == mode_T);
402 *node = get_Proj_pred(irn);
404 return get_Proj_proj(n);
410 static be_reg_data_t *retrieve_reg_data(const ir_node *node)
412 const be_node_attr_t *attr;
416 pos = get_Proj_proj(node);
417 node = get_Proj_pred(node);
420 assert(is_be_node(node));
421 attr = get_irn_attr_const(node);
422 assert(pos >= 0 && pos < ARR_LEN(attr->reg_data) && "illegal proj number");
424 return &attr->reg_data[pos];
428 be_node_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg)
430 be_reg_data_t *r = retrieve_reg_data(irn);
435 ir_node *be_new_Spill(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 *to_spill)
444 res = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
445 a = init_node_attr(res, 2);
449 be_node_set_reg_class(res, be_pos_Spill_frame, cls_frame);
450 be_node_set_reg_class(res, be_pos_Spill_val, cls);
454 ir_node *be_new_Reload(const arch_register_class_t *cls, const arch_register_class_t *cls_frame,
455 ir_graph *irg, ir_node *bl, ir_node *frame, ir_node *mem, ir_mode *mode)
462 res = new_ir_node(NULL, irg, bl, op_be_Reload, mode, 2, in);
464 init_node_attr(res, 2);
465 be_node_set_reg_class(res, -1, cls);
466 be_node_set_reg_class(res, be_pos_Reload_frame, cls_frame);
467 be_node_set_flags(res, -1, arch_irn_flags_rematerializable);
471 ir_node *be_get_Reload_mem(const ir_node *irn)
473 assert(be_is_Reload(irn));
474 return get_irn_n(irn, be_pos_Reload_mem);
477 ir_node *be_get_Reload_frame(const ir_node *irn)
479 assert(be_is_Reload(irn));
480 return get_irn_n(irn, be_pos_Reload_frame);
483 ir_node *be_get_Spill_val(const ir_node *irn)
485 assert(be_is_Spill(irn));
486 return get_irn_n(irn, be_pos_Spill_val);
489 ir_node *be_get_Spill_frame(const ir_node *irn)
491 assert(be_is_Spill(irn));
492 return get_irn_n(irn, be_pos_Spill_frame);
495 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
498 ir_node *irn = new_ir_node(NULL, irg, bl, op_be_Perm, mode_T, n, in);
499 init_node_attr(irn, n);
500 for(i = 0; i < n; ++i) {
501 be_node_set_reg_class(irn, i, cls);
502 be_node_set_reg_class(irn, OUT_POS(i), cls);
508 void be_Perm_reduce(ir_node *perm, int new_size, int *map)
510 ir_graph *irg = get_irn_irg(perm);
511 int arity = get_irn_arity(perm);
512 be_reg_data_t *old_data = alloca(arity * sizeof(old_data[0]));
513 be_node_attr_t *attr = get_irn_attr(perm);
514 ir_node **new_in = NEW_ARR_D(ir_node *, irg->obst, new_size);
518 assert(be_is_Perm(perm));
519 assert(new_size <= arity);
521 /* save the old register data */
522 memcpy(old_data, attr->reg_data, arity * sizeof(old_data[0]));
524 /* compose the new in array and set the new register data directly in place */
525 for (i = 0; i < new_size; ++i) {
527 new_in[i] = get_irn_n(perm, idx);
528 attr->reg_data[i] = old_data[idx];
531 set_irn_in(perm, new_size, new_in);
534 ir_node *be_new_MemPerm(const arch_env_t *arch_env, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
537 ir_node *frame = get_irg_frame(irg);
538 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
540 const arch_register_t *sp = arch_env->isa->sp;
541 be_memperm_attr_t *attr;
544 real_in = alloca((n+1) * sizeof(real_in[0]));
546 memcpy(&real_in[1], in, n * sizeof(real_in[0]));
548 irn = new_ir_node(NULL, irg, bl, op_be_MemPerm, mode_T, n+1, real_in);
550 init_node_attr(irn, n + 1);
551 be_node_set_reg_class(irn, 0, sp->reg_class);
552 for (i = 0; i < n; ++i) {
553 be_node_set_reg_class(irn, i + 1, cls_frame);
554 be_node_set_reg_class(irn, OUT_POS(i), cls_frame);
557 attr = get_irn_attr(irn);
559 attr->in_entities = obstack_alloc(irg->obst, n * sizeof(attr->in_entities[0]));
560 memset(attr->in_entities, 0, n * sizeof(attr->in_entities[0]));
561 attr->out_entities = obstack_alloc(irg->obst, n*sizeof(attr->out_entities[0]));
562 memset(attr->out_entities, 0, n*sizeof(attr->out_entities[0]));
568 ir_node *be_new_Copy(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *op)
572 arch_register_req_t *req;
575 res = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
576 init_node_attr(res, 1);
577 be_node_set_reg_class(res, 0, cls);
578 be_node_set_reg_class(res, OUT_POS(0), cls);
580 req = get_req(res, OUT_POS(0));
582 req->type = arch_register_req_type_should_be_same;
583 req->other_same = 1U << 0;
588 ir_node *be_get_Copy_op(const ir_node *cpy) {
589 return get_irn_n(cpy, be_pos_Copy_op);
592 void be_set_Copy_op(ir_node *cpy, ir_node *op) {
593 set_irn_n(cpy, be_pos_Copy_op, op);
596 ir_node *be_new_Keep(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
601 res = new_ir_node(NULL, irg, bl, op_be_Keep, mode_ANY, -1, NULL);
602 init_node_attr(res, -1);
604 for(i = 0; i < n; ++i) {
605 add_irn_n(res, in[i]);
606 add_register_req(res);
607 be_node_set_reg_class(res, i, cls);
614 void be_Keep_add_node(ir_node *keep, const arch_register_class_t *cls, ir_node *node)
618 assert(be_is_Keep(keep));
619 n = add_irn_n(keep, node);
620 add_register_req(keep);
621 be_node_set_reg_class(keep, n, cls);
624 /* creates a be_Call */
625 ir_node *be_new_Call(dbg_info *dbg, ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *sp, ir_node *ptr,
626 int n_outs, int n, ir_node *in[], ir_type *call_tp)
629 int real_n = be_pos_Call_first_arg + n;
633 NEW_ARR_A(ir_node *, real_in, real_n);
634 real_in[be_pos_Call_mem] = mem;
635 real_in[be_pos_Call_sp] = sp;
636 real_in[be_pos_Call_ptr] = ptr;
637 memcpy(&real_in[be_pos_Call_first_arg], in, n * sizeof(in[0]));
639 irn = new_ir_node(dbg, irg, bl, op_be_Call, mode_T, real_n, real_in);
640 a = init_node_attr(irn, (n_outs > real_n ? n_outs : real_n));
642 a->call_tp = call_tp;
647 /* Gets the call entity or NULL if this is no static call. */
648 ir_entity *be_Call_get_entity(const ir_node *call) {
649 const be_call_attr_t *a = get_irn_attr_const(call);
650 assert(be_is_Call(call));
654 /* Sets the call entity. */
655 void be_Call_set_entity(ir_node *call, ir_entity *ent) {
656 be_call_attr_t *a = get_irn_attr(call);
657 assert(be_is_Call(call));
661 /* Gets the call type. */
662 ir_type *be_Call_get_type(ir_node *call) {
663 const be_call_attr_t *a = get_irn_attr_const(call);
664 assert(be_is_Call(call));
668 /* Sets the call type. */
669 void be_Call_set_type(ir_node *call, ir_type *call_tp) {
670 be_call_attr_t *a = get_irn_attr(call);
671 assert(be_is_Call(call));
672 a->call_tp = call_tp;
675 void be_Call_set_pop(ir_node *call, unsigned pop) {
676 be_call_attr_t *a = get_irn_attr(call);
680 unsigned be_Call_get_pop(const ir_node *call) {
681 const be_call_attr_t *a = get_irn_attr_const(call);
685 /* Construct a new be_Return. */
686 ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *block, int n_res,
687 unsigned pop, int n, ir_node *in[])
693 res = new_ir_node(dbg, irg, block, op_be_Return, mode_X, -1, NULL);
694 init_node_attr(res, -1);
695 for(i = 0; i < n; ++i) {
696 add_irn_n(res, in[i]);
697 add_register_req(res);
700 a = get_irn_attr(res);
701 a->num_ret_vals = n_res;
708 /* Returns the number of real returns values */
709 int be_Return_get_n_rets(const ir_node *ret) {
710 const be_return_attr_t *a = get_irn_generic_attr_const(ret);
711 return a->num_ret_vals;
714 /* return the number of bytes that should be popped from stack when executing the Return. */
715 unsigned be_Return_get_pop(const ir_node *ret) {
716 const be_return_attr_t *a = get_irn_generic_attr_const(ret);
720 /* return non-zero, if number of popped bytes must be always emitted */
721 int be_Return_get_emit_pop(const ir_node *ret) {
722 const be_return_attr_t *a = get_irn_generic_attr_const(ret);
726 /* return non-zero, if number of popped bytes must be always emitted */
727 void be_Return_set_emit_pop(ir_node *ret, int emit_pop) {
728 be_return_attr_t *a = get_irn_generic_attr(ret);
729 a->emit_pop = emit_pop;
732 int be_Return_append_node(ir_node *ret, ir_node *node) {
735 pos = add_irn_n(ret, node);
736 add_register_req(ret);
741 ir_node *be_new_IncSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl,
742 ir_node *old_sp, int offset, int align)
749 irn = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode,
750 sizeof(in) / sizeof(in[0]), in);
751 a = init_node_attr(irn, 1);
755 be_node_set_flags(irn, -1, arch_irn_flags_ignore | arch_irn_flags_modify_sp);
757 /* Set output constraint to stack register. */
758 be_node_set_reg_class(irn, 0, sp->reg_class);
759 be_set_constr_single_reg(irn, BE_OUT_POS(0), sp);
760 be_node_set_irn_reg(NULL, irn, sp);
765 ir_node *be_new_AddSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, ir_node *sz)
769 ir_node *in[be_pos_AddSP_last];
770 const arch_register_class_t *class;
772 in[be_pos_AddSP_old_sp] = old_sp;
773 in[be_pos_AddSP_size] = sz;
775 irn = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T, be_pos_AddSP_last, in);
776 a = init_node_attr(irn, be_pos_AddSP_last);
778 be_node_set_flags(irn, OUT_POS(pn_be_AddSP_sp),
779 arch_irn_flags_ignore | arch_irn_flags_modify_sp);
781 /* Set output constraint to stack register. */
782 be_set_constr_single_reg(irn, be_pos_AddSP_old_sp, sp);
783 be_node_set_reg_class(irn, be_pos_AddSP_size, arch_register_get_class(sp));
784 be_set_constr_single_reg(irn, OUT_POS(pn_be_AddSP_sp), sp);
785 a->reg_data[pn_be_AddSP_sp].reg = sp;
787 class = arch_register_get_class(sp);
788 be_node_set_reg_class(irn, OUT_POS(pn_be_AddSP_res), class);
793 ir_node *be_new_SubSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, ir_node *sz)
797 ir_node *in[be_pos_SubSP_last];
799 in[be_pos_SubSP_old_sp] = old_sp;
800 in[be_pos_SubSP_size] = sz;
802 irn = new_ir_node(NULL, irg, bl, op_be_SubSP, mode_T, be_pos_SubSP_last, in);
803 a = init_node_attr(irn, be_pos_SubSP_last);
805 be_node_set_flags(irn, OUT_POS(pn_be_SubSP_sp),
806 arch_irn_flags_ignore | arch_irn_flags_modify_sp);
808 /* Set output constraint to stack register. */
809 be_set_constr_single_reg(irn, be_pos_SubSP_old_sp, sp);
810 be_node_set_reg_class(irn, be_pos_SubSP_size, arch_register_get_class(sp));
811 be_set_constr_single_reg(irn, OUT_POS(pn_be_SubSP_sp), sp);
812 a->reg_data[pn_be_SubSP_sp].reg = sp;
817 ir_node *be_new_RegParams(ir_graph *irg, ir_node *bl, int n_outs)
822 res = new_ir_node(NULL, irg, bl, op_be_RegParams, mode_T, 0, NULL);
823 init_node_attr(res, -1);
824 for(i = 0; i < n_outs; ++i)
825 add_register_req(res);
830 ir_node *be_RegParams_append_out_reg(ir_node *regparams,
831 const arch_env_t *arch_env,
832 const arch_register_t *reg)
834 ir_graph *irg = get_irn_irg(regparams);
835 ir_node *block = get_nodes_block(regparams);
836 be_node_attr_t *attr = get_irn_attr(regparams);
837 const arch_register_class_t *cls = arch_register_get_class(reg);
838 ir_mode *mode = arch_register_class_mode(cls);
839 int n = ARR_LEN(attr->reg_data);
842 assert(be_is_RegParams(regparams));
843 proj = new_r_Proj(irg, block, regparams, mode, n);
844 add_register_req(regparams);
845 be_set_constr_single_reg(regparams, BE_OUT_POS(n), reg);
846 arch_set_irn_register(arch_env, proj, reg);
848 /* TODO decide, whether we need to set ignore/modify sp flags here? */
853 ir_node *be_new_FrameAddr(const arch_register_class_t *cls_frame, ir_graph *irg, ir_node *bl, ir_node *frame, ir_entity *ent)
860 irn = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
861 a = init_node_attr(irn, 1);
864 be_node_set_reg_class(irn, 0, cls_frame);
865 be_node_set_reg_class(irn, OUT_POS(0), cls_frame);
867 return optimize_node(irn);
870 ir_node *be_get_FrameAddr_frame(const ir_node *node) {
871 assert(be_is_FrameAddr(node));
872 return get_irn_n(node, be_pos_FrameAddr_ptr);
875 ir_entity *be_get_FrameAddr_entity(const ir_node *node)
877 const be_frame_attr_t *attr = get_irn_generic_attr_const(node);
881 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)
884 ir_node **in = (ir_node **) alloca((n + 1) * sizeof(in[0]));
887 memcpy(&in[1], in_keep, n * sizeof(in[0]));
888 irn = new_ir_node(NULL, irg, bl, op_be_CopyKeep, mode, n + 1, in);
889 init_node_attr(irn, n + 1);
890 be_node_set_reg_class(irn, OUT_POS(0), cls);
891 be_node_set_reg_class(irn, 0, cls);
896 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)
898 return be_new_CopyKeep(cls, irg, bl, src, 1, &keep, mode);
901 ir_node *be_get_CopyKeep_op(const ir_node *cpy) {
902 return get_irn_n(cpy, be_pos_CopyKeep_op);
905 void be_set_CopyKeep_op(ir_node *cpy, ir_node *op) {
906 set_irn_n(cpy, be_pos_CopyKeep_op, op);
909 ir_node *be_new_Barrier(ir_graph *irg, ir_node *bl, int n, ir_node *in[])
914 res = new_ir_node(NULL, irg, bl, op_be_Barrier, mode_T, -1, NULL);
915 init_node_attr(res, -1);
916 for(i = 0; i < n; ++i) {
917 add_irn_n(res, in[i]);
918 add_register_req(res);
924 ir_node *be_Barrier_append_node(ir_node *barrier, ir_node *node)
926 ir_graph *irg = get_irn_irg(barrier);
927 ir_node *block = get_nodes_block(barrier);
928 ir_mode *mode = get_irn_mode(node);
929 int n = add_irn_n(barrier, node);
931 ir_node *proj = new_r_Proj(irg, block, barrier, mode, n);
932 add_register_req(barrier);
937 /* Construct a new be_Unwind. */
938 ir_node *be_new_Unwind(dbg_info *dbg, ir_graph *irg, ir_node *block,
939 ir_node *mem, ir_node *sp)
944 in[be_pos_Unwind_mem] = mem;
945 in[be_pos_Unwind_sp] = sp;
946 res = new_ir_node(dbg, irg, block, op_be_Unwind, mode_X, 2, in);
947 init_node_attr(res, -1);
952 int be_is_Spill (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Spill ; }
953 int be_is_Reload (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Reload ; }
954 int be_is_Copy (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Copy ; }
955 int be_is_CopyKeep (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_CopyKeep ; }
956 int be_is_Perm (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Perm ; }
957 int be_is_MemPerm (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_MemPerm ; }
958 int be_is_Keep (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Keep ; }
959 int be_is_Call (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Call ; }
960 int be_is_Return (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Return ; }
961 int be_is_IncSP (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_IncSP ; }
962 int be_is_AddSP (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_AddSP ; }
963 int be_is_SubSP (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_SubSP ; }
964 int be_is_RegParams (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_RegParams ; }
965 int be_is_FrameAddr (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameAddr ; }
966 int be_is_Barrier (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Barrier ; }
967 int be_is_Unwind (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Unwind ; }
969 int be_has_frame_entity(const ir_node *irn)
971 switch (be_get_irn_opcode(irn)) {
981 ir_entity *be_get_frame_entity(const ir_node *irn)
983 if (be_has_frame_entity(irn)) {
984 const be_frame_attr_t *a = get_irn_attr_const(irn);
990 int be_get_frame_offset(const ir_node *irn)
992 assert(is_be_node(irn));
993 if (be_has_frame_entity(irn)) {
994 const be_frame_attr_t *a = get_irn_attr_const(irn);
1000 void be_set_MemPerm_in_entity(const ir_node *irn, int n, ir_entity *ent)
1002 const be_memperm_attr_t *attr = get_irn_attr_const(irn);
1004 assert(be_is_MemPerm(irn));
1005 assert(n < be_get_MemPerm_entity_arity(irn));
1007 attr->in_entities[n] = ent;
1010 ir_entity* be_get_MemPerm_in_entity(const ir_node* irn, int n)
1012 const be_memperm_attr_t *attr = get_irn_attr_const(irn);
1014 assert(be_is_MemPerm(irn));
1015 assert(n < be_get_MemPerm_entity_arity(irn));
1017 return attr->in_entities[n];
1020 void be_set_MemPerm_out_entity(const ir_node *irn, int n, ir_entity *ent)
1022 const be_memperm_attr_t *attr = get_irn_attr_const(irn);
1024 assert(be_is_MemPerm(irn));
1025 assert(n < be_get_MemPerm_entity_arity(irn));
1027 attr->out_entities[n] = ent;
1030 ir_entity* be_get_MemPerm_out_entity(const ir_node* irn, int n)
1032 const be_memperm_attr_t *attr = get_irn_attr_const(irn);
1034 assert(be_is_MemPerm(irn));
1035 assert(n < be_get_MemPerm_entity_arity(irn));
1037 return attr->out_entities[n];
1040 int be_get_MemPerm_entity_arity(const ir_node *irn)
1042 return get_irn_arity(irn) - 1;
1045 void be_set_constr_single_reg(ir_node *node, int pos, const arch_register_t *reg)
1047 arch_register_req_t *req = get_req(node, pos);
1048 const arch_register_class_t *cls = arch_register_get_class(reg);
1049 ir_graph *irg = get_irn_irg(node);
1050 struct obstack *obst = get_irg_obstack(irg);
1051 unsigned *limited_bitset;
1053 assert(req->cls == NULL || req->cls == cls);
1054 assert(! (req->type & arch_register_req_type_limited));
1055 assert(req->limited == NULL);
1057 limited_bitset = rbitset_obstack_alloc(obst, arch_register_class_n_regs(cls));
1058 rbitset_set(limited_bitset, arch_register_get_index(reg));
1061 req->type |= arch_register_req_type_limited;
1062 req->limited = limited_bitset;
1065 void be_set_constr_limited(ir_node *node, int pos, const arch_register_req_t *req)
1067 ir_graph *irg = get_irn_irg(node);
1068 struct obstack *obst = get_irg_obstack(irg);
1069 arch_register_req_t *r = get_req(node, pos);
1071 assert(arch_register_req_is(req, limited));
1072 assert(! (req->type & (arch_register_req_type_should_be_same | arch_register_req_type_should_be_different)));
1073 memcpy(r, req, sizeof(r[0]));
1074 r->limited = rbitset_duplicate_obstack_alloc(obst, req->limited, req->cls->n_regs);
1077 void be_node_set_flags(ir_node *irn, int pos, arch_irn_flags_t flags)
1079 be_req_t *bereq = get_be_req(irn, pos);
1080 bereq->flags = flags;
1083 void be_node_add_flags(ir_node *irn, int pos, arch_irn_flags_t flags)
1085 be_req_t *bereq = get_be_req(irn, pos);
1086 bereq->flags |= flags;
1089 void be_node_set_reg_class(ir_node *irn, int pos, const arch_register_class_t *cls)
1091 arch_register_req_t *req = get_req(irn, pos);
1096 req->type = arch_register_req_type_none;
1097 } else if (req->type == arch_register_req_type_none) {
1098 req->type = arch_register_req_type_normal;
1102 void be_node_set_req_type(ir_node *irn, int pos, arch_register_req_type_t type)
1104 arch_register_req_t *req = get_req(irn, pos);
1108 ir_node *be_get_IncSP_pred(ir_node *irn) {
1109 assert(be_is_IncSP(irn));
1110 return get_irn_n(irn, 0);
1113 void be_set_IncSP_pred(ir_node *incsp, ir_node *pred) {
1114 assert(be_is_IncSP(incsp));
1115 set_irn_n(incsp, 0, pred);
1118 void be_set_IncSP_offset(ir_node *irn, int offset)
1120 be_incsp_attr_t *a = get_irn_attr(irn);
1121 assert(be_is_IncSP(irn));
1125 int be_get_IncSP_offset(const ir_node *irn)
1127 const be_incsp_attr_t *a = get_irn_attr_const(irn);
1128 assert(be_is_IncSP(irn));
1132 int be_get_IncSP_align(const ir_node *irn)
1134 const be_incsp_attr_t *a = get_irn_attr_const(irn);
1135 assert(be_is_IncSP(irn));
1139 ir_node *be_spill(const arch_env_t *arch_env, ir_node *block, ir_node *irn)
1141 ir_graph *irg = get_irn_irg(block);
1142 ir_node *frame = get_irg_frame(irg);
1143 const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, irn, -1);
1144 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
1147 spill = be_new_Spill(cls, cls_frame, irg, block, frame, irn);
1151 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)
1154 ir_node *bl = is_Block(insert) ? insert : get_nodes_block(insert);
1155 ir_graph *irg = get_irn_irg(bl);
1156 ir_node *frame = get_irg_frame(irg);
1157 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
1159 assert(be_is_Spill(spill) || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
1161 reload = be_new_Reload(cls, cls_frame, irg, bl, frame, spill, mode);
1163 if (is_Block(insert)) {
1164 insert = sched_skip(insert, 0, sched_skip_cf_predicator, (void *) arch_env);
1165 sched_add_after(insert, reload);
1167 sched_add_before(insert, reload);
1175 | _ \ ___ __ _ | _ \ ___ __ _ ___
1176 | |_) / _ \/ _` | | |_) / _ \/ _` / __|
1177 | _ < __/ (_| | | _ < __/ (_| \__ \
1178 |_| \_\___|\__, | |_| \_\___|\__, |___/
1185 arch_register_req_t *get_out_reg_req(const ir_node *irn, int out_pos)
1187 const be_node_attr_t *a = get_irn_attr_const(irn);
1189 if(out_pos >= ARR_LEN(a->reg_data)) {
1190 return arch_no_register_req;
1193 return &a->reg_data[out_pos].req.req;
1197 arch_register_req_t *get_in_reg_req(const ir_node *irn, int pos)
1199 const be_node_attr_t *a = get_irn_attr_const(irn);
1201 if(pos >= get_irn_arity(irn) || pos >= ARR_LEN(a->reg_data))
1202 return arch_no_register_req;
1204 return &a->reg_data[pos].in_req.req;
1207 static const arch_register_req_t *
1208 be_node_get_irn_reg_req(const void *self, const ir_node *irn, int pos)
1214 if (get_irn_mode(irn) == mode_T)
1215 return arch_no_register_req;
1217 out_pos = redir_proj((const ir_node **)&irn);
1218 assert(is_be_node(irn));
1219 return get_out_reg_req(irn, out_pos);
1220 } else if (is_be_node(irn)) {
1222 * For spills and reloads, we return "none" as requirement for frame
1223 * pointer, so every input is ok. Some backends need this (e.g. STA).
1225 if ((be_is_Spill(irn) && pos == be_pos_Spill_frame) ||
1226 (be_is_Reload(irn) && pos == be_pos_Reload_frame))
1227 return arch_no_register_req;
1229 return get_in_reg_req(irn, pos);
1232 return arch_no_register_req;
1235 const arch_register_t *
1236 be_node_get_irn_reg(const void *self, const ir_node *irn)
1241 if (get_irn_mode(irn) == mode_T)
1243 r = retrieve_reg_data(irn);
1247 static arch_irn_class_t be_node_classify(const void *self, const ir_node *irn)
1249 redir_proj((const ir_node **) &irn);
1252 switch(be_get_irn_opcode(irn)) {
1253 #define XXX(a,b) case beo_ ## a: return arch_irn_class_ ## b
1255 XXX(Reload, reload);
1258 XXX(Return, branch);
1261 return arch_irn_class_normal;
1267 static arch_irn_flags_t be_node_get_flags(const void *self, const ir_node *node)
1274 pos = OUT_POS(get_Proj_proj(node));
1275 node = skip_Proj_const(node);
1278 bereq = get_be_req(node, pos);
1280 return bereq->flags;
1283 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)
1294 assert(be_has_frame_entity(irn));
1296 a = get_irn_attr(irn);
1300 static void be_node_set_frame_offset(const void *self, ir_node *irn, int offset)
1303 if(be_has_frame_entity(irn)) {
1304 be_frame_attr_t *a = get_irn_attr(irn);
1309 static int be_node_get_sp_bias(const void *self, const ir_node *irn)
1312 if(be_is_IncSP(irn))
1313 return be_get_IncSP_offset(irn);
1315 return -(int)be_Call_get_pop(irn);
1321 ___ ____ _ _ _ _ _ _
1322 |_ _| _ \| \ | | | | | | __ _ _ __ __| | | ___ _ __
1323 | || |_) | \| | | |_| |/ _` | '_ \ / _` | |/ _ \ '__|
1324 | || _ <| |\ | | _ | (_| | | | | (_| | | __/ |
1325 |___|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
1329 static const arch_irn_ops_if_t be_node_irn_ops_if = {
1330 be_node_get_irn_reg_req,
1331 be_node_set_irn_reg,
1332 be_node_get_irn_reg,
1335 be_node_get_frame_entity,
1336 be_node_set_frame_entity,
1337 be_node_set_frame_offset,
1338 be_node_get_sp_bias,
1339 NULL, /* get_inverse */
1340 NULL, /* get_op_estimated_cost */
1341 NULL, /* possible_memory_operand */
1342 NULL, /* perform_memory_operand */
1345 static const arch_irn_ops_t be_node_irn_ops = {
1349 const void *be_node_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn)
1351 redir_proj((const ir_node **) &irn);
1353 return is_be_node(irn) ? &be_node_irn_ops : NULL;
1356 const arch_irn_handler_t be_node_irn_handler = {
1361 ____ _ _ ___ ____ _ _ _ _ _ _
1362 | _ \| |__ (_) |_ _| _ \| \ | | | | | | __ _ _ __ __| | | ___ _ __
1363 | |_) | '_ \| | | || |_) | \| | | |_| |/ _` | '_ \ / _` | |/ _ \ '__|
1364 | __/| | | | | | || _ <| |\ | | _ | (_| | | | | (_| | | __/ |
1365 |_| |_| |_|_| |___|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
1370 const arch_register_t *reg;
1371 arch_register_req_t req;
1372 arch_irn_flags_t flags;
1376 arch_irn_handler_t irn_handler;
1377 arch_irn_ops_t irn_ops;
1378 const arch_env_t *arch_env;
1382 #define get_phi_handler_from_handler(h) container_of(h, phi_handler_t, irn_handler)
1383 #define get_phi_handler_from_ops(h) container_of(h, phi_handler_t, irn_ops)
1386 const void *phi_get_irn_ops(const arch_irn_handler_t *handler,
1389 const phi_handler_t *h;
1390 if(!is_Phi(irn) || !mode_is_datab(get_irn_mode(irn)))
1393 h = get_phi_handler_from_handler(handler);
1398 phi_attr_t *get_Phi_attr(const phi_handler_t *handler, const ir_node *phi)
1400 phi_attr_t *attr = pmap_get(handler->phi_attrs, (void*) phi);
1402 ir_graph *irg = get_irn_irg(phi);
1403 struct obstack *obst = get_irg_obstack(irg);
1404 attr = obstack_alloc(obst, sizeof(attr[0]));
1405 memset(attr, 0, sizeof(attr[0]));
1406 pmap_insert(handler->phi_attrs, phi, attr);
1413 * Get register class of a Phi.
1416 const arch_register_req_t *get_Phi_reg_req_recursive(const phi_handler_t *h,
1420 int n = get_irn_arity(phi);
1424 if(*visited && pset_find_ptr(*visited, phi))
1427 for(i = 0; i < n; ++i) {
1428 op = get_irn_n(phi, i);
1429 /* Matze: don't we unnecessary constraint our phis with this?
1430 * we only need to take the regclass IMO*/
1432 return arch_get_register_req(h->arch_env, op, BE_OUT_POS(0));
1436 * The operands of that Phi were all Phis themselves.
1437 * We have to start a DFS for a non-Phi argument now.
1440 *visited = pset_new_ptr(16);
1442 pset_insert_ptr(*visited, phi);
1444 for(i = 0; i < n; ++i) {
1445 const arch_register_req_t *req;
1446 op = get_irn_n(phi, i);
1447 req = get_Phi_reg_req_recursive(h, op, visited);
1456 const arch_register_req_t *phi_get_irn_reg_req(const void *self,
1457 const ir_node *irn, int pos)
1459 phi_handler_t *phi_handler = get_phi_handler_from_ops(self);
1464 if(!mode_is_datab(get_irn_mode(irn)))
1465 return arch_no_register_req;
1467 attr = get_Phi_attr(phi_handler, irn);
1469 if(attr->req.type == arch_register_req_type_none) {
1470 pset *visited = NULL;
1471 const arch_register_req_t *req;
1472 req = get_Phi_reg_req_recursive(phi_handler, irn, &visited);
1474 memcpy(&attr->req, req, sizeof(req[0]));
1475 assert(attr->req.cls != NULL);
1476 attr->req.type = arch_register_req_type_normal;
1485 void be_set_phi_reg_req(const arch_env_t *arch_env, ir_node *node,
1486 const arch_register_req_t *req)
1488 const arch_irn_ops_t *ops = arch_get_irn_ops(arch_env, node);
1489 const phi_handler_t *handler = get_phi_handler_from_ops(ops);
1492 assert(mode_is_datab(get_irn_mode(node)));
1494 attr = get_Phi_attr(handler, node);
1495 memcpy(&attr->req, req, sizeof(req[0]));
1498 void be_set_phi_flags(const arch_env_t *arch_env, ir_node *node,
1499 arch_irn_flags_t flags)
1501 const arch_irn_ops_t *ops = arch_get_irn_ops(arch_env, node);
1502 const phi_handler_t *handler = get_phi_handler_from_ops(ops);
1505 assert(mode_is_datab(get_irn_mode(node)));
1507 attr = get_Phi_attr(handler, node);
1508 attr->flags = flags;
1512 void phi_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg)
1514 phi_handler_t *h = get_phi_handler_from_ops(self);
1515 phi_attr_t *attr = get_Phi_attr(h, irn);
1520 const arch_register_t *phi_get_irn_reg(const void *self, const ir_node *irn)
1522 phi_handler_t *h = get_phi_handler_from_ops(self);
1523 phi_attr_t *attr = get_Phi_attr(h, irn);
1528 arch_irn_class_t phi_classify(const void *self, const ir_node *irn)
1532 return arch_irn_class_normal;
1536 arch_irn_flags_t phi_get_flags(const void *self, const ir_node *irn)
1538 phi_handler_t *h = get_phi_handler_from_ops(self);
1539 phi_attr_t *attr = get_Phi_attr(h, irn);
1545 ir_entity *phi_get_frame_entity(const void *self, const ir_node *irn)
1553 void phi_set_frame_entity(const void *self, ir_node *irn, ir_entity *ent)
1562 void phi_set_frame_offset(const void *self, ir_node *irn, int bias)
1571 int phi_get_sp_bias(const void* self, const ir_node *irn)
1579 const arch_irn_ops_if_t phi_irn_ops = {
1580 phi_get_irn_reg_req,
1585 phi_get_frame_entity,
1586 phi_set_frame_entity,
1587 phi_set_frame_offset,
1589 NULL, /* get_inverse */
1590 NULL, /* get_op_estimated_cost */
1591 NULL, /* possible_memory_operand */
1592 NULL, /* perform_memory_operand */
1595 arch_irn_handler_t *be_phi_handler_new(const arch_env_t *arch_env)
1597 phi_handler_t *h = xmalloc(sizeof(h[0]));
1598 h->irn_handler.get_irn_ops = phi_get_irn_ops;
1599 h->irn_ops.impl = &phi_irn_ops;
1600 h->arch_env = arch_env;
1601 h->phi_attrs = pmap_create();
1602 return (arch_irn_handler_t *) h;
1605 void be_phi_handler_free(arch_irn_handler_t *handler)
1607 phi_handler_t *h = get_phi_handler_from_handler(handler);
1608 pmap_destroy(h->phi_attrs);
1609 h->phi_attrs = NULL;
1613 void be_phi_handler_reset(arch_irn_handler_t *handler)
1615 phi_handler_t *h = get_phi_handler_from_handler(handler);
1617 pmap_destroy(h->phi_attrs);
1618 h->phi_attrs = pmap_create();
1623 | \ | | ___ __| | ___ | _ \ _ _ _ __ ___ _ __ (_)_ __ __ _
1624 | \| |/ _ \ / _` |/ _ \ | | | | | | | '_ ` _ \| '_ \| | '_ \ / _` |
1625 | |\ | (_) | (_| | __/ | |_| | |_| | | | | | | |_) | | | | | (_| |
1626 |_| \_|\___/ \__,_|\___| |____/ \__,_|_| |_| |_| .__/|_|_| |_|\__, |
1631 * Dumps a register requirement to a file.
1633 static void dump_node_req(FILE *f, int idx, const arch_register_req_t *req,
1634 const ir_node *node)
1636 int did_something = 0;
1638 const char *prefix = buf;
1640 snprintf(buf, sizeof(buf), "#%d ", idx);
1641 buf[sizeof(buf) - 1] = '\0';
1646 arch_register_req_format(tmp, sizeof(tmp), req, node);
1647 fprintf(f, "%s", tmp);
1656 * Dumps node register requirements to a file.
1658 static void dump_node_reqs(FILE *f, ir_node *node)
1661 be_node_attr_t *a = get_irn_attr(node);
1662 int len = ARR_LEN(a->reg_data);
1664 fprintf(f, "registers: \n");
1665 for(i = 0; i < len; ++i) {
1666 be_reg_data_t *rd = &a->reg_data[i];
1668 fprintf(f, "#%d: %s\n", i, rd->reg->name);
1671 fprintf(f, "in requirements:\n");
1672 for(i = 0; i < len; ++i) {
1673 dump_node_req(f, i, &a->reg_data[i].in_req.req, node);
1676 fprintf(f, "\nout requirements:\n");
1677 for(i = 0; i < len; ++i) {
1678 dump_node_req(f, i, &a->reg_data[i].req.req, node);
1683 * ir_op-Operation: dump a be node to file
1685 static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason)
1687 be_node_attr_t *at = get_irn_attr(irn);
1689 assert(is_be_node(irn));
1692 case dump_node_opcode_txt:
1693 fprintf(f, get_op_name(get_irn_op(irn)));
1695 case dump_node_mode_txt:
1696 if(be_is_Perm(irn) || be_is_Copy(irn) || be_is_CopyKeep(irn)) {
1697 fprintf(f, " %s", get_mode_name(get_irn_mode(irn)));
1700 case dump_node_nodeattr_txt:
1701 if(be_is_Call(irn)) {
1702 be_call_attr_t *a = (be_call_attr_t *) at;
1704 fprintf(f, " [%s] ", get_entity_name(a->ent));
1706 if(be_is_IncSP(irn)) {
1707 const be_incsp_attr_t *attr = get_irn_generic_attr_const(irn);
1708 if(attr->offset == BE_STACK_FRAME_SIZE_EXPAND) {
1709 fprintf(f, " [Setup Stackframe] ");
1710 } else if(attr->offset == BE_STACK_FRAME_SIZE_SHRINK) {
1711 fprintf(f, " [Destroy Stackframe] ");
1713 fprintf(f, " [%d] ", attr->offset);
1717 case dump_node_info_txt:
1718 dump_node_reqs(f, irn);
1720 if(be_has_frame_entity(irn)) {
1721 be_frame_attr_t *a = (be_frame_attr_t *) at;
1723 unsigned size = get_type_size_bytes(get_entity_type(a->ent));
1724 ir_fprintf(f, "frame entity: %+F, offset 0x%x (%d), size 0x%x (%d) bytes\n",
1725 a->ent, a->offset, a->offset, size, size);
1730 switch(be_get_irn_opcode(irn)) {
1733 be_incsp_attr_t *a = (be_incsp_attr_t *) at;
1734 if (a->offset == BE_STACK_FRAME_SIZE_EXPAND)
1735 fprintf(f, "offset: FRAME_SIZE\n");
1736 else if(a->offset == BE_STACK_FRAME_SIZE_SHRINK)
1737 fprintf(f, "offset: -FRAME SIZE\n");
1739 fprintf(f, "offset: %u\n", a->offset);
1744 be_call_attr_t *a = (be_call_attr_t *) at;
1747 fprintf(f, "\ncalling: %s\n", get_entity_name(a->ent));
1753 for(i = 0; i < be_get_MemPerm_entity_arity(irn); ++i) {
1754 ir_entity *in, *out;
1755 in = be_get_MemPerm_in_entity(irn, i);
1756 out = be_get_MemPerm_out_entity(irn, i);
1758 fprintf(f, "\nin[%d]: %s\n", i, get_entity_name(in));
1761 fprintf(f, "\nout[%d]: %s\n", i, get_entity_name(out));
1777 * Copies the backend specific attributes from old node to new node.
1779 static void copy_attr(const ir_node *old_node, ir_node *new_node)
1781 const be_node_attr_t *old_attr = get_irn_attr_const(old_node);
1782 be_node_attr_t *new_attr = get_irn_attr(new_node);
1783 struct obstack *obst = get_irg_obstack(get_irn_irg(new_node));
1786 assert(is_be_node(old_node));
1787 assert(is_be_node(new_node));
1789 memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
1790 new_attr->reg_data = NULL;
1792 if(old_attr->reg_data != NULL)
1793 len = ARR_LEN(old_attr->reg_data);
1797 if(get_irn_op(old_node)->opar == oparity_dynamic
1798 || be_is_RegParams(old_node)) {
1799 new_attr->reg_data = NEW_ARR_F(be_reg_data_t, len);
1801 new_attr->reg_data = NEW_ARR_D(be_reg_data_t, obst, len);
1805 memcpy(new_attr->reg_data, old_attr->reg_data, len * sizeof(be_reg_data_t));
1806 for(i = 0; i < len; ++i) {
1807 const be_reg_data_t *rd = &old_attr->reg_data[i];
1808 be_reg_data_t *newrd = &new_attr->reg_data[i];
1809 if(arch_register_req_is(&rd->req.req, limited)) {
1810 const arch_register_req_t *req = &rd->req.req;
1811 arch_register_req_t *new_req = &newrd->req.req;
1813 = rbitset_duplicate_obstack_alloc(obst, req->limited, req->cls->n_regs);
1815 if(arch_register_req_is(&rd->in_req.req, limited)) {
1816 const arch_register_req_t *req = &rd->in_req.req;
1817 arch_register_req_t *new_req = &newrd->in_req.req;
1819 = rbitset_duplicate_obstack_alloc(obst, req->limited, req->cls->n_regs);
1825 static const ir_op_ops be_node_op_ops = {