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
155 static int be_reqs_equal(const be_req_t *req1, const be_req_t *req2)
157 if(!reg_reqs_equal(&req1->req, &req2->req))
159 if(req1->flags != req2->flags)
166 * Compare two be node attributes.
168 * @return zero if both attributes are identically
170 static int _node_cmp_attr(const be_node_attr_t *a, const be_node_attr_t *b) {
173 if (ARR_LEN(a->reg_data) != ARR_LEN(b->reg_data))
176 len = ARR_LEN(a->reg_data);
177 for (i = 0; i < len; ++i) {
178 if (a->reg_data[i].reg != b->reg_data[i].reg ||
179 !be_reqs_equal(&a->reg_data[i].in_req, &b->reg_data[i].in_req) ||
180 !be_reqs_equal(&a->reg_data[i].req, &b->reg_data[i].req))
188 * Compare the node attributes of two be_node's.
190 * @return zero if both nodes have identically attributes
192 static int node_cmp_attr(ir_node *a, ir_node *b) {
193 const be_node_attr_t *a_attr = get_irn_attr_const(a);
194 const be_node_attr_t *b_attr = get_irn_attr_const(b);
196 return _node_cmp_attr(a_attr, b_attr);
200 * Compare the attributes of two be_FrameAddr nodes.
202 * @return zero if both nodes have identically attributes
204 static int FrameAddr_cmp_attr(ir_node *a, ir_node *b) {
205 const be_frame_attr_t *a_attr = get_irn_attr_const(a);
206 const be_frame_attr_t *b_attr = get_irn_attr_const(b);
208 if (a_attr->ent != b_attr->ent || a_attr->offset != b_attr->offset)
211 return _node_cmp_attr(&a_attr->node_attr, &b_attr->node_attr);
215 * Compare the attributes of two be_Return nodes.
217 * @return zero if both nodes have identically attributes
219 static int Return_cmp_attr(ir_node *a, ir_node *b) {
220 const be_return_attr_t *a_attr = get_irn_attr_const(a);
221 const be_return_attr_t *b_attr = get_irn_attr_const(b);
223 if (a_attr->num_ret_vals != b_attr->num_ret_vals)
225 if (a_attr->pop != b_attr->pop)
227 if (a_attr->emit_pop != b_attr->emit_pop)
230 return _node_cmp_attr(&a_attr->node_attr, &b_attr->node_attr);
234 * Compare the attributes of two be_IncSP nodes.
236 * @return zero if both nodes have identically attributes
238 static int IncSP_cmp_attr(ir_node *a, ir_node *b) {
239 const be_incsp_attr_t *a_attr = get_irn_attr_const(a);
240 const be_incsp_attr_t *b_attr = get_irn_attr_const(b);
242 if (a_attr->offset != b_attr->offset)
245 return _node_cmp_attr(&a_attr->node_attr, &b_attr->node_attr);
249 * Compare the attributes of two be_Call nodes.
251 * @return zero if both nodes have identically attributes
253 static int Call_cmp_attr(ir_node *a, ir_node *b) {
254 const be_call_attr_t *a_attr = get_irn_attr_const(a);
255 const be_call_attr_t *b_attr = get_irn_attr_const(b);
257 if (a_attr->ent != b_attr->ent ||
258 a_attr->call_tp != b_attr->call_tp)
261 return _node_cmp_attr(&a_attr->node_attr, &b_attr->node_attr);
264 static INLINE be_req_t *get_be_req(const ir_node *node, int pos)
267 const be_node_attr_t *attr;
270 assert(is_be_node(node));
271 attr = get_irn_attr_const(node);
277 assert(idx < get_irn_arity(node));
279 assert(idx < ARR_LEN(attr->reg_data));
280 rd = &attr->reg_data[idx];
282 return pos < 0 ? &rd->req : &rd->in_req;
285 static INLINE arch_register_req_t *get_req(const ir_node *node, int pos)
287 be_req_t *bereq = get_be_req(node, pos);
291 void be_node_init(void) {
292 static int inited = 0;
299 /* Acquire all needed opcodes. */
300 beo_base = get_next_ir_opcodes(beo_Last);
302 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);
303 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);
304 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);
305 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);
306 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);
307 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);
308 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);
309 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);
310 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);
311 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);
312 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);
313 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);
314 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);
315 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);
316 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);
317 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);
319 set_op_tag(op_be_Spill, &be_node_tag);
320 op_be_Spill->ops.node_cmp_attr = FrameAddr_cmp_attr;
321 set_op_tag(op_be_Reload, &be_node_tag);
322 op_be_Reload->ops.node_cmp_attr = FrameAddr_cmp_attr;
323 set_op_tag(op_be_Perm, &be_node_tag);
324 op_be_Perm->ops.node_cmp_attr = node_cmp_attr;
325 set_op_tag(op_be_MemPerm, &be_node_tag);
326 op_be_MemPerm->ops.node_cmp_attr = node_cmp_attr;
327 set_op_tag(op_be_Copy, &be_node_tag);
328 op_be_Copy->ops.node_cmp_attr = node_cmp_attr;
329 set_op_tag(op_be_Keep, &be_node_tag);
330 op_be_Keep->ops.node_cmp_attr = node_cmp_attr;
331 set_op_tag(op_be_CopyKeep, &be_node_tag);
332 op_be_CopyKeep->ops.node_cmp_attr = node_cmp_attr;
333 set_op_tag(op_be_Call, &be_node_tag);
334 op_be_Call->ops.node_cmp_attr = Call_cmp_attr;
335 set_op_tag(op_be_Return, &be_node_tag);
336 op_be_Return->ops.node_cmp_attr = Return_cmp_attr;
337 set_op_tag(op_be_AddSP, &be_node_tag);
338 op_be_AddSP->ops.node_cmp_attr = node_cmp_attr;
339 set_op_tag(op_be_SubSP, &be_node_tag);
340 op_be_SubSP->ops.node_cmp_attr = node_cmp_attr;
341 set_op_tag(op_be_IncSP, &be_node_tag);
342 op_be_IncSP->ops.node_cmp_attr = IncSP_cmp_attr;
343 set_op_tag(op_be_RegParams, &be_node_tag);
344 op_be_RegParams->ops.node_cmp_attr = node_cmp_attr;
345 set_op_tag(op_be_FrameAddr, &be_node_tag);
346 op_be_FrameAddr->ops.node_cmp_attr = FrameAddr_cmp_attr;
347 set_op_tag(op_be_Barrier, &be_node_tag);
348 op_be_Barrier->ops.node_cmp_attr = node_cmp_attr;
349 set_op_tag(op_be_Unwind, &be_node_tag);
350 op_be_Unwind->ops.node_cmp_attr = node_cmp_attr;
354 * Initializes the generic attribute of all be nodes and return ir.
356 static void *init_node_attr(ir_node *node, int max_reg_data)
358 ir_graph *irg = get_irn_irg(node);
359 struct obstack *obst = get_irg_obstack(irg);
360 be_node_attr_t *a = get_irn_attr(node);
362 memset(a, 0, sizeof(get_op_attr_size(get_irn_op(node))));
364 if(max_reg_data >= 0) {
365 a->reg_data = NEW_ARR_D(be_reg_data_t, obst, max_reg_data);
366 memset(a->reg_data, 0, max_reg_data * sizeof(a->reg_data[0]));
368 a->reg_data = NEW_ARR_F(be_reg_data_t, 0);
374 static void add_register_req(ir_node *node)
376 be_node_attr_t *a = get_irn_attr(node);
377 be_reg_data_t regreq;
378 memset(®req, 0, sizeof(regreq));
379 ARR_APP1(be_reg_data_t, a->reg_data, regreq);
382 int is_be_node(const ir_node *irn)
384 return get_op_tag(get_irn_op(irn)) == &be_node_tag;
387 be_opcode_t be_get_irn_opcode(const ir_node *irn)
389 return is_be_node(irn) ? (be_opcode_t) get_irn_opcode(irn) - beo_base : beo_NoBeOp;
393 * Skip Proj nodes and return their Proj numbers.
395 * If *node is a Proj or Proj(Proj) node, skip it.
397 * @param node points to the node to be skipped
399 * @return 0 if *node was no Proj node, its Proj number else.
401 static int redir_proj(const ir_node **node)
403 const ir_node *n = *node;
408 *node = irn = get_Proj_pred(n);
410 assert(get_irn_mode(irn) == mode_T);
411 *node = get_Proj_pred(irn);
413 return get_Proj_proj(n);
419 static be_reg_data_t *retrieve_reg_data(const ir_node *node)
421 const be_node_attr_t *attr;
425 pos = get_Proj_proj(node);
426 node = get_Proj_pred(node);
429 assert(is_be_node(node));
430 attr = get_irn_attr_const(node);
431 assert(pos >= 0 && pos < ARR_LEN(attr->reg_data) && "illegal proj number");
433 return &attr->reg_data[pos];
437 be_node_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg)
439 be_reg_data_t *r = retrieve_reg_data(irn);
444 ir_node *be_new_Spill(const arch_register_class_t *cls, const arch_register_class_t *cls_frame,
445 ir_graph *irg, ir_node *bl, ir_node *frame, ir_node *to_spill)
453 res = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
454 a = init_node_attr(res, 2);
458 be_node_set_reg_class(res, be_pos_Spill_frame, cls_frame);
459 be_node_set_reg_class(res, be_pos_Spill_val, cls);
463 ir_node *be_new_Reload(const arch_register_class_t *cls, const arch_register_class_t *cls_frame,
464 ir_graph *irg, ir_node *bl, ir_node *frame, ir_node *mem, ir_mode *mode)
471 res = new_ir_node(NULL, irg, bl, op_be_Reload, mode, 2, in);
473 init_node_attr(res, 2);
474 be_node_set_reg_class(res, -1, cls);
475 be_node_set_reg_class(res, be_pos_Reload_frame, cls_frame);
476 be_node_set_flags(res, -1, arch_irn_flags_rematerializable);
480 ir_node *be_get_Reload_mem(const ir_node *irn)
482 assert(be_is_Reload(irn));
483 return get_irn_n(irn, be_pos_Reload_mem);
486 ir_node *be_get_Reload_frame(const ir_node *irn)
488 assert(be_is_Reload(irn));
489 return get_irn_n(irn, be_pos_Reload_frame);
492 ir_node *be_get_Spill_val(const ir_node *irn)
494 assert(be_is_Spill(irn));
495 return get_irn_n(irn, be_pos_Spill_val);
498 ir_node *be_get_Spill_frame(const ir_node *irn)
500 assert(be_is_Spill(irn));
501 return get_irn_n(irn, be_pos_Spill_frame);
504 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
507 ir_node *irn = new_ir_node(NULL, irg, bl, op_be_Perm, mode_T, n, in);
508 init_node_attr(irn, n);
509 for(i = 0; i < n; ++i) {
510 be_node_set_reg_class(irn, i, cls);
511 be_node_set_reg_class(irn, OUT_POS(i), cls);
517 void be_Perm_reduce(ir_node *perm, int new_size, int *map)
519 ir_graph *irg = get_irn_irg(perm);
520 int arity = get_irn_arity(perm);
521 be_reg_data_t *old_data = alloca(arity * sizeof(old_data[0]));
522 be_node_attr_t *attr = get_irn_attr(perm);
523 ir_node **new_in = NEW_ARR_D(ir_node *, irg->obst, new_size);
527 assert(be_is_Perm(perm));
528 assert(new_size <= arity);
530 /* save the old register data */
531 memcpy(old_data, attr->reg_data, arity * sizeof(old_data[0]));
533 /* compose the new in array and set the new register data directly in place */
534 for (i = 0; i < new_size; ++i) {
536 new_in[i] = get_irn_n(perm, idx);
537 attr->reg_data[i] = old_data[idx];
540 set_irn_in(perm, new_size, new_in);
543 ir_node *be_new_MemPerm(const arch_env_t *arch_env, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
546 ir_node *frame = get_irg_frame(irg);
547 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
549 const arch_register_t *sp = arch_env->isa->sp;
550 be_memperm_attr_t *attr;
553 real_in = alloca((n+1) * sizeof(real_in[0]));
555 memcpy(&real_in[1], in, n * sizeof(real_in[0]));
557 irn = new_ir_node(NULL, irg, bl, op_be_MemPerm, mode_T, n+1, real_in);
559 init_node_attr(irn, n + 1);
560 be_node_set_reg_class(irn, 0, sp->reg_class);
561 for (i = 0; i < n; ++i) {
562 be_node_set_reg_class(irn, i + 1, cls_frame);
563 be_node_set_reg_class(irn, OUT_POS(i), cls_frame);
566 attr = get_irn_attr(irn);
568 attr->in_entities = obstack_alloc(irg->obst, n * sizeof(attr->in_entities[0]));
569 memset(attr->in_entities, 0, n * sizeof(attr->in_entities[0]));
570 attr->out_entities = obstack_alloc(irg->obst, n*sizeof(attr->out_entities[0]));
571 memset(attr->out_entities, 0, n*sizeof(attr->out_entities[0]));
577 ir_node *be_new_Copy(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *op)
581 arch_register_req_t *req;
584 res = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
585 init_node_attr(res, 1);
586 be_node_set_reg_class(res, 0, cls);
587 be_node_set_reg_class(res, OUT_POS(0), cls);
589 req = get_req(res, OUT_POS(0));
591 req->type = arch_register_req_type_should_be_same;
592 req->other_same = 1U << 0;
597 ir_node *be_get_Copy_op(const ir_node *cpy) {
598 return get_irn_n(cpy, be_pos_Copy_op);
601 void be_set_Copy_op(ir_node *cpy, ir_node *op) {
602 set_irn_n(cpy, be_pos_Copy_op, op);
605 ir_node *be_new_Keep(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
610 res = new_ir_node(NULL, irg, bl, op_be_Keep, mode_ANY, -1, NULL);
611 init_node_attr(res, -1);
613 for(i = 0; i < n; ++i) {
614 add_irn_n(res, in[i]);
615 add_register_req(res);
616 be_node_set_reg_class(res, i, cls);
623 void be_Keep_add_node(ir_node *keep, const arch_register_class_t *cls, ir_node *node)
627 assert(be_is_Keep(keep));
628 n = add_irn_n(keep, node);
629 add_register_req(keep);
630 be_node_set_reg_class(keep, n, cls);
633 /* creates a be_Call */
634 ir_node *be_new_Call(dbg_info *dbg, ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *sp, ir_node *ptr,
635 int n_outs, int n, ir_node *in[], ir_type *call_tp)
638 int real_n = be_pos_Call_first_arg + n;
642 NEW_ARR_A(ir_node *, real_in, real_n);
643 real_in[be_pos_Call_mem] = mem;
644 real_in[be_pos_Call_sp] = sp;
645 real_in[be_pos_Call_ptr] = ptr;
646 memcpy(&real_in[be_pos_Call_first_arg], in, n * sizeof(in[0]));
648 irn = new_ir_node(dbg, irg, bl, op_be_Call, mode_T, real_n, real_in);
649 a = init_node_attr(irn, (n_outs > real_n ? n_outs : real_n));
651 a->call_tp = call_tp;
656 /* Gets the call entity or NULL if this is no static call. */
657 ir_entity *be_Call_get_entity(const ir_node *call) {
658 const be_call_attr_t *a = get_irn_attr_const(call);
659 assert(be_is_Call(call));
663 /* Sets the call entity. */
664 void be_Call_set_entity(ir_node *call, ir_entity *ent) {
665 be_call_attr_t *a = get_irn_attr(call);
666 assert(be_is_Call(call));
670 /* Gets the call type. */
671 ir_type *be_Call_get_type(ir_node *call) {
672 const be_call_attr_t *a = get_irn_attr_const(call);
673 assert(be_is_Call(call));
677 /* Sets the call type. */
678 void be_Call_set_type(ir_node *call, ir_type *call_tp) {
679 be_call_attr_t *a = get_irn_attr(call);
680 assert(be_is_Call(call));
681 a->call_tp = call_tp;
684 void be_Call_set_pop(ir_node *call, unsigned pop) {
685 be_call_attr_t *a = get_irn_attr(call);
689 unsigned be_Call_get_pop(const ir_node *call) {
690 const be_call_attr_t *a = get_irn_attr_const(call);
694 /* Construct a new be_Return. */
695 ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *block, int n_res,
696 unsigned pop, int n, ir_node *in[])
702 res = new_ir_node(dbg, irg, block, op_be_Return, mode_X, -1, NULL);
703 init_node_attr(res, -1);
704 for(i = 0; i < n; ++i) {
705 add_irn_n(res, in[i]);
706 add_register_req(res);
709 a = get_irn_attr(res);
710 a->num_ret_vals = n_res;
717 /* Returns the number of real returns values */
718 int be_Return_get_n_rets(const ir_node *ret) {
719 const be_return_attr_t *a = get_irn_generic_attr_const(ret);
720 return a->num_ret_vals;
723 /* return the number of bytes that should be popped from stack when executing the Return. */
724 unsigned be_Return_get_pop(const ir_node *ret) {
725 const be_return_attr_t *a = get_irn_generic_attr_const(ret);
729 /* return non-zero, if number of popped bytes must be always emitted */
730 int be_Return_get_emit_pop(const ir_node *ret) {
731 const be_return_attr_t *a = get_irn_generic_attr_const(ret);
735 /* return non-zero, if number of popped bytes must be always emitted */
736 void be_Return_set_emit_pop(ir_node *ret, int emit_pop) {
737 be_return_attr_t *a = get_irn_generic_attr(ret);
738 a->emit_pop = emit_pop;
741 int be_Return_append_node(ir_node *ret, ir_node *node) {
744 pos = add_irn_n(ret, node);
745 add_register_req(ret);
750 ir_node *be_new_IncSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl,
751 ir_node *old_sp, int offset, int align)
758 irn = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode,
759 sizeof(in) / sizeof(in[0]), in);
760 a = init_node_attr(irn, 1);
764 be_node_set_flags(irn, -1, arch_irn_flags_ignore | arch_irn_flags_modify_sp);
766 /* Set output constraint to stack register. */
767 be_node_set_reg_class(irn, 0, sp->reg_class);
768 be_set_constr_single_reg(irn, BE_OUT_POS(0), sp);
769 be_node_set_irn_reg(NULL, irn, sp);
774 ir_node *be_new_AddSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, ir_node *sz)
778 ir_node *in[be_pos_AddSP_last];
779 const arch_register_class_t *class;
781 in[be_pos_AddSP_old_sp] = old_sp;
782 in[be_pos_AddSP_size] = sz;
784 irn = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T, be_pos_AddSP_last, in);
785 a = init_node_attr(irn, be_pos_AddSP_last);
787 be_node_set_flags(irn, OUT_POS(pn_be_AddSP_sp),
788 arch_irn_flags_ignore | arch_irn_flags_modify_sp);
790 /* Set output constraint to stack register. */
791 be_set_constr_single_reg(irn, be_pos_AddSP_old_sp, sp);
792 be_node_set_reg_class(irn, be_pos_AddSP_size, arch_register_get_class(sp));
793 be_set_constr_single_reg(irn, OUT_POS(pn_be_AddSP_sp), sp);
794 a->reg_data[pn_be_AddSP_sp].reg = sp;
796 class = arch_register_get_class(sp);
797 be_node_set_reg_class(irn, OUT_POS(pn_be_AddSP_res), class);
802 ir_node *be_new_SubSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, ir_node *sz)
806 ir_node *in[be_pos_SubSP_last];
808 in[be_pos_SubSP_old_sp] = old_sp;
809 in[be_pos_SubSP_size] = sz;
811 irn = new_ir_node(NULL, irg, bl, op_be_SubSP, mode_T, be_pos_SubSP_last, in);
812 a = init_node_attr(irn, be_pos_SubSP_last);
814 be_node_set_flags(irn, OUT_POS(pn_be_SubSP_sp),
815 arch_irn_flags_ignore | arch_irn_flags_modify_sp);
817 /* Set output constraint to stack register. */
818 be_set_constr_single_reg(irn, be_pos_SubSP_old_sp, sp);
819 be_node_set_reg_class(irn, be_pos_SubSP_size, arch_register_get_class(sp));
820 be_set_constr_single_reg(irn, OUT_POS(pn_be_SubSP_sp), sp);
821 a->reg_data[pn_be_SubSP_sp].reg = sp;
826 ir_node *be_new_RegParams(ir_graph *irg, ir_node *bl, int n_outs)
831 res = new_ir_node(NULL, irg, bl, op_be_RegParams, mode_T, 0, NULL);
832 init_node_attr(res, -1);
833 for(i = 0; i < n_outs; ++i)
834 add_register_req(res);
839 ir_node *be_RegParams_append_out_reg(ir_node *regparams,
840 const arch_env_t *arch_env,
841 const arch_register_t *reg)
843 ir_graph *irg = get_irn_irg(regparams);
844 ir_node *block = get_nodes_block(regparams);
845 be_node_attr_t *attr = get_irn_attr(regparams);
846 const arch_register_class_t *cls = arch_register_get_class(reg);
847 ir_mode *mode = arch_register_class_mode(cls);
848 int n = ARR_LEN(attr->reg_data);
851 assert(be_is_RegParams(regparams));
852 proj = new_r_Proj(irg, block, regparams, mode, n);
853 add_register_req(regparams);
854 be_set_constr_single_reg(regparams, BE_OUT_POS(n), reg);
855 arch_set_irn_register(arch_env, proj, reg);
857 /* TODO decide, whether we need to set ignore/modify sp flags here? */
862 ir_node *be_new_FrameAddr(const arch_register_class_t *cls_frame, ir_graph *irg, ir_node *bl, ir_node *frame, ir_entity *ent)
869 irn = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
870 a = init_node_attr(irn, 1);
873 be_node_set_reg_class(irn, 0, cls_frame);
874 be_node_set_reg_class(irn, OUT_POS(0), cls_frame);
876 return optimize_node(irn);
879 ir_node *be_get_FrameAddr_frame(const ir_node *node) {
880 assert(be_is_FrameAddr(node));
881 return get_irn_n(node, be_pos_FrameAddr_ptr);
884 ir_entity *be_get_FrameAddr_entity(const ir_node *node)
886 const be_frame_attr_t *attr = get_irn_generic_attr_const(node);
890 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)
893 ir_node **in = (ir_node **) alloca((n + 1) * sizeof(in[0]));
896 memcpy(&in[1], in_keep, n * sizeof(in[0]));
897 irn = new_ir_node(NULL, irg, bl, op_be_CopyKeep, mode, n + 1, in);
898 init_node_attr(irn, n + 1);
899 be_node_set_reg_class(irn, OUT_POS(0), cls);
900 be_node_set_reg_class(irn, 0, cls);
905 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)
907 return be_new_CopyKeep(cls, irg, bl, src, 1, &keep, mode);
910 ir_node *be_get_CopyKeep_op(const ir_node *cpy) {
911 return get_irn_n(cpy, be_pos_CopyKeep_op);
914 void be_set_CopyKeep_op(ir_node *cpy, ir_node *op) {
915 set_irn_n(cpy, be_pos_CopyKeep_op, op);
918 ir_node *be_new_Barrier(ir_graph *irg, ir_node *bl, int n, ir_node *in[])
923 res = new_ir_node(NULL, irg, bl, op_be_Barrier, mode_T, -1, NULL);
924 init_node_attr(res, -1);
925 for(i = 0; i < n; ++i) {
926 add_irn_n(res, in[i]);
927 add_register_req(res);
933 ir_node *be_Barrier_append_node(ir_node *barrier, ir_node *node)
935 ir_graph *irg = get_irn_irg(barrier);
936 ir_node *block = get_nodes_block(barrier);
937 ir_mode *mode = get_irn_mode(node);
938 int n = add_irn_n(barrier, node);
940 ir_node *proj = new_r_Proj(irg, block, barrier, mode, n);
941 add_register_req(barrier);
946 /* Construct a new be_Unwind. */
947 ir_node *be_new_Unwind(dbg_info *dbg, ir_graph *irg, ir_node *block,
948 ir_node *mem, ir_node *sp)
953 in[be_pos_Unwind_mem] = mem;
954 in[be_pos_Unwind_sp] = sp;
955 res = new_ir_node(dbg, irg, block, op_be_Unwind, mode_X, 2, in);
956 init_node_attr(res, -1);
961 int be_is_Spill (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Spill ; }
962 int be_is_Reload (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Reload ; }
963 int be_is_Copy (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Copy ; }
964 int be_is_CopyKeep (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_CopyKeep ; }
965 int be_is_Perm (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Perm ; }
966 int be_is_MemPerm (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_MemPerm ; }
967 int be_is_Keep (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Keep ; }
968 int be_is_Call (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Call ; }
969 int be_is_Return (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Return ; }
970 int be_is_IncSP (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_IncSP ; }
971 int be_is_AddSP (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_AddSP ; }
972 int be_is_SubSP (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_SubSP ; }
973 int be_is_RegParams (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_RegParams ; }
974 int be_is_FrameAddr (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameAddr ; }
975 int be_is_Barrier (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Barrier ; }
976 int be_is_Unwind (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Unwind ; }
978 int be_has_frame_entity(const ir_node *irn)
980 switch (be_get_irn_opcode(irn)) {
990 ir_entity *be_get_frame_entity(const ir_node *irn)
992 if (be_has_frame_entity(irn)) {
993 const be_frame_attr_t *a = get_irn_attr_const(irn);
999 int be_get_frame_offset(const ir_node *irn)
1001 assert(is_be_node(irn));
1002 if (be_has_frame_entity(irn)) {
1003 const be_frame_attr_t *a = get_irn_attr_const(irn);
1009 void be_set_MemPerm_in_entity(const ir_node *irn, int n, ir_entity *ent)
1011 const be_memperm_attr_t *attr = get_irn_attr_const(irn);
1013 assert(be_is_MemPerm(irn));
1014 assert(n < be_get_MemPerm_entity_arity(irn));
1016 attr->in_entities[n] = ent;
1019 ir_entity* be_get_MemPerm_in_entity(const ir_node* irn, int n)
1021 const be_memperm_attr_t *attr = get_irn_attr_const(irn);
1023 assert(be_is_MemPerm(irn));
1024 assert(n < be_get_MemPerm_entity_arity(irn));
1026 return attr->in_entities[n];
1029 void be_set_MemPerm_out_entity(const ir_node *irn, int n, ir_entity *ent)
1031 const be_memperm_attr_t *attr = get_irn_attr_const(irn);
1033 assert(be_is_MemPerm(irn));
1034 assert(n < be_get_MemPerm_entity_arity(irn));
1036 attr->out_entities[n] = ent;
1039 ir_entity* be_get_MemPerm_out_entity(const ir_node* irn, int n)
1041 const be_memperm_attr_t *attr = get_irn_attr_const(irn);
1043 assert(be_is_MemPerm(irn));
1044 assert(n < be_get_MemPerm_entity_arity(irn));
1046 return attr->out_entities[n];
1049 int be_get_MemPerm_entity_arity(const ir_node *irn)
1051 return get_irn_arity(irn) - 1;
1054 void be_set_constr_single_reg(ir_node *node, int pos, const arch_register_t *reg)
1056 arch_register_req_t *req = get_req(node, pos);
1057 const arch_register_class_t *cls = arch_register_get_class(reg);
1058 ir_graph *irg = get_irn_irg(node);
1059 struct obstack *obst = get_irg_obstack(irg);
1060 unsigned *limited_bitset;
1062 assert(req->cls == NULL || req->cls == cls);
1063 assert(! (req->type & arch_register_req_type_limited));
1064 assert(req->limited == NULL);
1066 limited_bitset = rbitset_obstack_alloc(obst, arch_register_class_n_regs(cls));
1067 rbitset_set(limited_bitset, arch_register_get_index(reg));
1070 req->type |= arch_register_req_type_limited;
1071 req->limited = limited_bitset;
1074 void be_set_constr_limited(ir_node *node, int pos, const arch_register_req_t *req)
1076 ir_graph *irg = get_irn_irg(node);
1077 struct obstack *obst = get_irg_obstack(irg);
1078 arch_register_req_t *r = get_req(node, pos);
1080 assert(arch_register_req_is(req, limited));
1081 assert(! (req->type & (arch_register_req_type_should_be_same | arch_register_req_type_should_be_different)));
1082 memcpy(r, req, sizeof(r[0]));
1083 r->limited = rbitset_duplicate_obstack_alloc(obst, req->limited, req->cls->n_regs);
1086 void be_node_set_flags(ir_node *irn, int pos, arch_irn_flags_t flags)
1088 be_req_t *bereq = get_be_req(irn, pos);
1089 bereq->flags = flags;
1092 void be_node_add_flags(ir_node *irn, int pos, arch_irn_flags_t flags)
1094 be_req_t *bereq = get_be_req(irn, pos);
1095 bereq->flags |= flags;
1098 void be_node_set_reg_class(ir_node *irn, int pos, const arch_register_class_t *cls)
1100 arch_register_req_t *req = get_req(irn, pos);
1105 req->type = arch_register_req_type_none;
1106 } else if (req->type == arch_register_req_type_none) {
1107 req->type = arch_register_req_type_normal;
1111 void be_node_set_req_type(ir_node *irn, int pos, arch_register_req_type_t type)
1113 arch_register_req_t *req = get_req(irn, pos);
1117 ir_node *be_get_IncSP_pred(ir_node *irn) {
1118 assert(be_is_IncSP(irn));
1119 return get_irn_n(irn, 0);
1122 void be_set_IncSP_pred(ir_node *incsp, ir_node *pred) {
1123 assert(be_is_IncSP(incsp));
1124 set_irn_n(incsp, 0, pred);
1127 void be_set_IncSP_offset(ir_node *irn, int offset)
1129 be_incsp_attr_t *a = get_irn_attr(irn);
1130 assert(be_is_IncSP(irn));
1134 int be_get_IncSP_offset(const ir_node *irn)
1136 const be_incsp_attr_t *a = get_irn_attr_const(irn);
1137 assert(be_is_IncSP(irn));
1141 int be_get_IncSP_align(const ir_node *irn)
1143 const be_incsp_attr_t *a = get_irn_attr_const(irn);
1144 assert(be_is_IncSP(irn));
1148 ir_node *be_spill(const arch_env_t *arch_env, ir_node *block, ir_node *irn)
1150 ir_graph *irg = get_irn_irg(block);
1151 ir_node *frame = get_irg_frame(irg);
1152 const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, irn, -1);
1153 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
1156 spill = be_new_Spill(cls, cls_frame, irg, block, frame, irn);
1160 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)
1163 ir_node *bl = is_Block(insert) ? insert : get_nodes_block(insert);
1164 ir_graph *irg = get_irn_irg(bl);
1165 ir_node *frame = get_irg_frame(irg);
1166 const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
1168 assert(be_is_Spill(spill) || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
1170 reload = be_new_Reload(cls, cls_frame, irg, bl, frame, spill, mode);
1172 if (is_Block(insert)) {
1173 insert = sched_skip(insert, 0, sched_skip_cf_predicator, (void *) arch_env);
1174 sched_add_after(insert, reload);
1176 sched_add_before(insert, reload);
1184 | _ \ ___ __ _ | _ \ ___ __ _ ___
1185 | |_) / _ \/ _` | | |_) / _ \/ _` / __|
1186 | _ < __/ (_| | | _ < __/ (_| \__ \
1187 |_| \_\___|\__, | |_| \_\___|\__, |___/
1194 arch_register_req_t *get_out_reg_req(const ir_node *irn, int out_pos)
1196 const be_node_attr_t *a = get_irn_attr_const(irn);
1198 if(out_pos >= ARR_LEN(a->reg_data)) {
1199 return arch_no_register_req;
1202 return &a->reg_data[out_pos].req.req;
1206 arch_register_req_t *get_in_reg_req(const ir_node *irn, int pos)
1208 const be_node_attr_t *a = get_irn_attr_const(irn);
1210 if(pos >= get_irn_arity(irn) || pos >= ARR_LEN(a->reg_data))
1211 return arch_no_register_req;
1213 return &a->reg_data[pos].in_req.req;
1216 static const arch_register_req_t *
1217 be_node_get_irn_reg_req(const void *self, const ir_node *irn, int pos)
1223 if (get_irn_mode(irn) == mode_T)
1224 return arch_no_register_req;
1226 out_pos = redir_proj((const ir_node **)&irn);
1227 assert(is_be_node(irn));
1228 return get_out_reg_req(irn, out_pos);
1229 } else if (is_be_node(irn)) {
1231 * For spills and reloads, we return "none" as requirement for frame
1232 * pointer, so every input is ok. Some backends need this (e.g. STA).
1234 if ((be_is_Spill(irn) && pos == be_pos_Spill_frame) ||
1235 (be_is_Reload(irn) && pos == be_pos_Reload_frame))
1236 return arch_no_register_req;
1238 return get_in_reg_req(irn, pos);
1241 return arch_no_register_req;
1244 const arch_register_t *
1245 be_node_get_irn_reg(const void *self, const ir_node *irn)
1250 if (get_irn_mode(irn) == mode_T)
1252 r = retrieve_reg_data(irn);
1256 static arch_irn_class_t be_node_classify(const void *self, const ir_node *irn)
1258 redir_proj((const ir_node **) &irn);
1261 switch(be_get_irn_opcode(irn)) {
1262 #define XXX(a,b) case beo_ ## a: return arch_irn_class_ ## b
1264 XXX(Reload, reload);
1267 XXX(Return, branch);
1270 return arch_irn_class_normal;
1276 static arch_irn_flags_t be_node_get_flags(const void *self, const ir_node *node)
1283 pos = OUT_POS(get_Proj_proj(node));
1284 node = skip_Proj_const(node);
1287 bereq = get_be_req(node, pos);
1289 return bereq->flags;
1292 static ir_entity *be_node_get_frame_entity(const void *self, const ir_node *irn)
1295 return be_get_frame_entity(irn);
1298 static void be_node_set_frame_entity(const void *self, ir_node *irn, ir_entity *ent)
1303 assert(be_has_frame_entity(irn));
1305 a = get_irn_attr(irn);
1309 static void be_node_set_frame_offset(const void *self, ir_node *irn, int offset)
1312 if(be_has_frame_entity(irn)) {
1313 be_frame_attr_t *a = get_irn_attr(irn);
1318 static int be_node_get_sp_bias(const void *self, const ir_node *irn)
1321 if(be_is_IncSP(irn))
1322 return be_get_IncSP_offset(irn);
1324 return -(int)be_Call_get_pop(irn);
1330 ___ ____ _ _ _ _ _ _
1331 |_ _| _ \| \ | | | | | | __ _ _ __ __| | | ___ _ __
1332 | || |_) | \| | | |_| |/ _` | '_ \ / _` | |/ _ \ '__|
1333 | || _ <| |\ | | _ | (_| | | | | (_| | | __/ |
1334 |___|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
1338 static const arch_irn_ops_if_t be_node_irn_ops_if = {
1339 be_node_get_irn_reg_req,
1340 be_node_set_irn_reg,
1341 be_node_get_irn_reg,
1344 be_node_get_frame_entity,
1345 be_node_set_frame_entity,
1346 be_node_set_frame_offset,
1347 be_node_get_sp_bias,
1348 NULL, /* get_inverse */
1349 NULL, /* get_op_estimated_cost */
1350 NULL, /* possible_memory_operand */
1351 NULL, /* perform_memory_operand */
1354 static const arch_irn_ops_t be_node_irn_ops = {
1358 const void *be_node_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn)
1360 redir_proj((const ir_node **) &irn);
1362 return is_be_node(irn) ? &be_node_irn_ops : NULL;
1365 const arch_irn_handler_t be_node_irn_handler = {
1370 ____ _ _ ___ ____ _ _ _ _ _ _
1371 | _ \| |__ (_) |_ _| _ \| \ | | | | | | __ _ _ __ __| | | ___ _ __
1372 | |_) | '_ \| | | || |_) | \| | | |_| |/ _` | '_ \ / _` | |/ _ \ '__|
1373 | __/| | | | | | || _ <| |\ | | _ | (_| | | | | (_| | | __/ |
1374 |_| |_| |_|_| |___|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
1379 const arch_register_t *reg;
1380 arch_register_req_t req;
1381 arch_irn_flags_t flags;
1385 arch_irn_handler_t irn_handler;
1386 arch_irn_ops_t irn_ops;
1387 const arch_env_t *arch_env;
1391 #define get_phi_handler_from_handler(h) container_of(h, phi_handler_t, irn_handler)
1392 #define get_phi_handler_from_ops(h) container_of(h, phi_handler_t, irn_ops)
1395 const void *phi_get_irn_ops(const arch_irn_handler_t *handler,
1398 const phi_handler_t *h;
1399 if(!is_Phi(irn) || !mode_is_datab(get_irn_mode(irn)))
1402 h = get_phi_handler_from_handler(handler);
1407 phi_attr_t *get_Phi_attr(const phi_handler_t *handler, const ir_node *phi)
1409 phi_attr_t *attr = pmap_get(handler->phi_attrs, (void*) phi);
1411 ir_graph *irg = get_irn_irg(phi);
1412 struct obstack *obst = get_irg_obstack(irg);
1413 attr = obstack_alloc(obst, sizeof(attr[0]));
1414 memset(attr, 0, sizeof(attr[0]));
1415 pmap_insert(handler->phi_attrs, phi, attr);
1422 * Get register class of a Phi.
1425 const arch_register_req_t *get_Phi_reg_req_recursive(const phi_handler_t *h,
1429 int n = get_irn_arity(phi);
1433 if(*visited && pset_find_ptr(*visited, phi))
1436 for(i = 0; i < n; ++i) {
1437 op = get_irn_n(phi, i);
1438 /* Matze: don't we unnecessary constraint our phis with this?
1439 * we only need to take the regclass IMO*/
1441 return arch_get_register_req(h->arch_env, op, BE_OUT_POS(0));
1445 * The operands of that Phi were all Phis themselves.
1446 * We have to start a DFS for a non-Phi argument now.
1449 *visited = pset_new_ptr(16);
1451 pset_insert_ptr(*visited, phi);
1453 for(i = 0; i < n; ++i) {
1454 const arch_register_req_t *req;
1455 op = get_irn_n(phi, i);
1456 req = get_Phi_reg_req_recursive(h, op, visited);
1465 const arch_register_req_t *phi_get_irn_reg_req(const void *self,
1466 const ir_node *irn, int pos)
1468 phi_handler_t *phi_handler = get_phi_handler_from_ops(self);
1473 if(!mode_is_datab(get_irn_mode(irn)))
1474 return arch_no_register_req;
1476 attr = get_Phi_attr(phi_handler, irn);
1478 if(attr->req.type == arch_register_req_type_none) {
1479 pset *visited = NULL;
1480 const arch_register_req_t *req;
1481 req = get_Phi_reg_req_recursive(phi_handler, irn, &visited);
1483 memcpy(&attr->req, req, sizeof(req[0]));
1484 assert(attr->req.cls != NULL);
1485 attr->req.type = arch_register_req_type_normal;
1494 void be_set_phi_reg_req(const arch_env_t *arch_env, ir_node *node,
1495 const arch_register_req_t *req)
1497 const arch_irn_ops_t *ops = arch_get_irn_ops(arch_env, node);
1498 const phi_handler_t *handler = get_phi_handler_from_ops(ops);
1501 assert(mode_is_datab(get_irn_mode(node)));
1503 attr = get_Phi_attr(handler, node);
1504 memcpy(&attr->req, req, sizeof(req[0]));
1507 void be_set_phi_flags(const arch_env_t *arch_env, ir_node *node,
1508 arch_irn_flags_t flags)
1510 const arch_irn_ops_t *ops = arch_get_irn_ops(arch_env, node);
1511 const phi_handler_t *handler = get_phi_handler_from_ops(ops);
1514 assert(mode_is_datab(get_irn_mode(node)));
1516 attr = get_Phi_attr(handler, node);
1517 attr->flags = flags;
1521 void phi_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg)
1523 phi_handler_t *h = get_phi_handler_from_ops(self);
1524 phi_attr_t *attr = get_Phi_attr(h, irn);
1529 const arch_register_t *phi_get_irn_reg(const void *self, const ir_node *irn)
1531 phi_handler_t *h = get_phi_handler_from_ops(self);
1532 phi_attr_t *attr = get_Phi_attr(h, irn);
1537 arch_irn_class_t phi_classify(const void *self, const ir_node *irn)
1541 return arch_irn_class_normal;
1545 arch_irn_flags_t phi_get_flags(const void *self, const ir_node *irn)
1547 phi_handler_t *h = get_phi_handler_from_ops(self);
1548 phi_attr_t *attr = get_Phi_attr(h, irn);
1554 ir_entity *phi_get_frame_entity(const void *self, const ir_node *irn)
1562 void phi_set_frame_entity(const void *self, ir_node *irn, ir_entity *ent)
1571 void phi_set_frame_offset(const void *self, ir_node *irn, int bias)
1580 int phi_get_sp_bias(const void* self, const ir_node *irn)
1588 const arch_irn_ops_if_t phi_irn_ops = {
1589 phi_get_irn_reg_req,
1594 phi_get_frame_entity,
1595 phi_set_frame_entity,
1596 phi_set_frame_offset,
1598 NULL, /* get_inverse */
1599 NULL, /* get_op_estimated_cost */
1600 NULL, /* possible_memory_operand */
1601 NULL, /* perform_memory_operand */
1604 arch_irn_handler_t *be_phi_handler_new(const arch_env_t *arch_env)
1606 phi_handler_t *h = xmalloc(sizeof(h[0]));
1607 h->irn_handler.get_irn_ops = phi_get_irn_ops;
1608 h->irn_ops.impl = &phi_irn_ops;
1609 h->arch_env = arch_env;
1610 h->phi_attrs = pmap_create();
1611 return (arch_irn_handler_t *) h;
1614 void be_phi_handler_free(arch_irn_handler_t *handler)
1616 phi_handler_t *h = get_phi_handler_from_handler(handler);
1617 pmap_destroy(h->phi_attrs);
1618 h->phi_attrs = NULL;
1622 void be_phi_handler_reset(arch_irn_handler_t *handler)
1624 phi_handler_t *h = get_phi_handler_from_handler(handler);
1626 pmap_destroy(h->phi_attrs);
1627 h->phi_attrs = pmap_create();
1632 | \ | | ___ __| | ___ | _ \ _ _ _ __ ___ _ __ (_)_ __ __ _
1633 | \| |/ _ \ / _` |/ _ \ | | | | | | | '_ ` _ \| '_ \| | '_ \ / _` |
1634 | |\ | (_) | (_| | __/ | |_| | |_| | | | | | | |_) | | | | | (_| |
1635 |_| \_|\___/ \__,_|\___| |____/ \__,_|_| |_| |_| .__/|_|_| |_|\__, |
1640 * Dumps a register requirement to a file.
1642 static void dump_node_req(FILE *f, int idx, const arch_register_req_t *req,
1643 const ir_node *node)
1645 int did_something = 0;
1647 const char *prefix = buf;
1649 snprintf(buf, sizeof(buf), "#%d ", idx);
1650 buf[sizeof(buf) - 1] = '\0';
1655 arch_register_req_format(tmp, sizeof(tmp), req, node);
1656 fprintf(f, "%s", tmp);
1665 * Dumps node register requirements to a file.
1667 static void dump_node_reqs(FILE *f, ir_node *node)
1670 be_node_attr_t *a = get_irn_attr(node);
1671 int len = ARR_LEN(a->reg_data);
1673 fprintf(f, "registers: \n");
1674 for(i = 0; i < len; ++i) {
1675 be_reg_data_t *rd = &a->reg_data[i];
1677 fprintf(f, "#%d: %s\n", i, rd->reg->name);
1680 fprintf(f, "in requirements:\n");
1681 for(i = 0; i < len; ++i) {
1682 dump_node_req(f, i, &a->reg_data[i].in_req.req, node);
1685 fprintf(f, "\nout requirements:\n");
1686 for(i = 0; i < len; ++i) {
1687 dump_node_req(f, i, &a->reg_data[i].req.req, node);
1692 * ir_op-Operation: dump a be node to file
1694 static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason)
1696 be_node_attr_t *at = get_irn_attr(irn);
1698 assert(is_be_node(irn));
1701 case dump_node_opcode_txt:
1702 fprintf(f, get_op_name(get_irn_op(irn)));
1704 case dump_node_mode_txt:
1705 if(be_is_Perm(irn) || be_is_Copy(irn) || be_is_CopyKeep(irn)) {
1706 fprintf(f, " %s", get_mode_name(get_irn_mode(irn)));
1709 case dump_node_nodeattr_txt:
1710 if(be_is_Call(irn)) {
1711 be_call_attr_t *a = (be_call_attr_t *) at;
1713 fprintf(f, " [%s] ", get_entity_name(a->ent));
1715 if(be_is_IncSP(irn)) {
1716 const be_incsp_attr_t *attr = get_irn_generic_attr_const(irn);
1717 if(attr->offset == BE_STACK_FRAME_SIZE_EXPAND) {
1718 fprintf(f, " [Setup Stackframe] ");
1719 } else if(attr->offset == BE_STACK_FRAME_SIZE_SHRINK) {
1720 fprintf(f, " [Destroy Stackframe] ");
1722 fprintf(f, " [%d] ", attr->offset);
1726 case dump_node_info_txt:
1727 dump_node_reqs(f, irn);
1729 if(be_has_frame_entity(irn)) {
1730 be_frame_attr_t *a = (be_frame_attr_t *) at;
1732 unsigned size = get_type_size_bytes(get_entity_type(a->ent));
1733 ir_fprintf(f, "frame entity: %+F, offset 0x%x (%d), size 0x%x (%d) bytes\n",
1734 a->ent, a->offset, a->offset, size, size);
1739 switch(be_get_irn_opcode(irn)) {
1742 be_incsp_attr_t *a = (be_incsp_attr_t *) at;
1743 if (a->offset == BE_STACK_FRAME_SIZE_EXPAND)
1744 fprintf(f, "offset: FRAME_SIZE\n");
1745 else if(a->offset == BE_STACK_FRAME_SIZE_SHRINK)
1746 fprintf(f, "offset: -FRAME SIZE\n");
1748 fprintf(f, "offset: %u\n", a->offset);
1753 be_call_attr_t *a = (be_call_attr_t *) at;
1756 fprintf(f, "\ncalling: %s\n", get_entity_name(a->ent));
1762 for(i = 0; i < be_get_MemPerm_entity_arity(irn); ++i) {
1763 ir_entity *in, *out;
1764 in = be_get_MemPerm_in_entity(irn, i);
1765 out = be_get_MemPerm_out_entity(irn, i);
1767 fprintf(f, "\nin[%d]: %s\n", i, get_entity_name(in));
1770 fprintf(f, "\nout[%d]: %s\n", i, get_entity_name(out));
1786 * Copies the backend specific attributes from old node to new node.
1788 static void copy_attr(const ir_node *old_node, ir_node *new_node)
1790 const be_node_attr_t *old_attr = get_irn_attr_const(old_node);
1791 be_node_attr_t *new_attr = get_irn_attr(new_node);
1792 struct obstack *obst = get_irg_obstack(get_irn_irg(new_node));
1795 assert(is_be_node(old_node));
1796 assert(is_be_node(new_node));
1798 memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
1799 new_attr->reg_data = NULL;
1801 if(old_attr->reg_data != NULL)
1802 len = ARR_LEN(old_attr->reg_data);
1806 if(get_irn_op(old_node)->opar == oparity_dynamic
1807 || be_is_RegParams(old_node)) {
1808 new_attr->reg_data = NEW_ARR_F(be_reg_data_t, len);
1810 new_attr->reg_data = NEW_ARR_D(be_reg_data_t, obst, len);
1814 memcpy(new_attr->reg_data, old_attr->reg_data, len * sizeof(be_reg_data_t));
1815 for(i = 0; i < len; ++i) {
1816 const be_reg_data_t *rd = &old_attr->reg_data[i];
1817 be_reg_data_t *newrd = &new_attr->reg_data[i];
1818 if(arch_register_req_is(&rd->req.req, limited)) {
1819 const arch_register_req_t *req = &rd->req.req;
1820 arch_register_req_t *new_req = &newrd->req.req;
1822 = rbitset_duplicate_obstack_alloc(obst, req->limited, req->cls->n_regs);
1824 if(arch_register_req_is(&rd->in_req.req, limited)) {
1825 const arch_register_req_t *req = &rd->in_req.req;
1826 arch_register_req_t *new_req = &newrd->in_req.req;
1828 = rbitset_duplicate_obstack_alloc(obst, req->limited, req->cls->n_regs);
1834 static const ir_op_ops be_node_op_ops = {