2 * Copyright (C) 1995-2011 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
26 * Backend node support for generic backend nodes.
27 * This file provides Perm, Copy, Spill and Reload nodes.
40 #include "bitfiddle.h"
41 #include "raw_bitset.h"
52 #include "irbackedge_t.h"
53 #include "irverify_t.h"
63 typedef struct be_node_attr_t {
67 /** The be_Return nodes attribute type. */
70 int num_ret_vals; /**< number of return values */
71 unsigned pop; /**< number of bytes that should be popped */
72 int emit_pop; /**< if set, emit pop bytes, even if pop = 0 */
75 /** The be_IncSP attribute type. */
78 int offset; /**< The offset by which the stack shall be
80 int align; /**< whether stack should be aligned after the
84 /** The be_Frame attribute type. */
91 /** The be_Call attribute type. */
94 ir_entity *ent; /**< called entity if this is a static call. */
96 ir_type *call_tp; /**< call type, copied from the original Call */
101 ir_entity **in_entities;
102 ir_entity **out_entities;
108 ir_op *op_be_MemPerm;
111 ir_op *op_be_CopyKeep;
118 ir_op *op_be_FrameAddr;
121 * Compare the attributes of two be_FrameAddr nodes.
123 * @return zero if both nodes have identically attributes
125 static int FrameAddr_cmp_attr(const ir_node *a, const ir_node *b)
127 const be_frame_attr_t *a_attr = (const be_frame_attr_t*)get_irn_generic_attr_const(a);
128 const be_frame_attr_t *b_attr = (const be_frame_attr_t*)get_irn_generic_attr_const(b);
130 if (a_attr->ent != b_attr->ent || a_attr->offset != b_attr->offset)
133 return be_nodes_equal(a, b);
137 * Compare the attributes of two be_Return nodes.
139 * @return zero if both nodes have identically attributes
141 static int Return_cmp_attr(const ir_node *a, const ir_node *b)
143 const be_return_attr_t *a_attr = (const be_return_attr_t*)get_irn_generic_attr_const(a);
144 const be_return_attr_t *b_attr = (const be_return_attr_t*)get_irn_generic_attr_const(b);
146 if (a_attr->num_ret_vals != b_attr->num_ret_vals)
148 if (a_attr->pop != b_attr->pop)
150 if (a_attr->emit_pop != b_attr->emit_pop)
153 return be_nodes_equal(a, b);
157 * Compare the attributes of two be_IncSP nodes.
159 * @return zero if both nodes have identically attributes
161 static int IncSP_cmp_attr(const ir_node *a, const ir_node *b)
163 const be_incsp_attr_t *a_attr = (const be_incsp_attr_t*)get_irn_generic_attr_const(a);
164 const be_incsp_attr_t *b_attr = (const be_incsp_attr_t*)get_irn_generic_attr_const(b);
166 if (a_attr->offset != b_attr->offset)
169 return be_nodes_equal(a, b);
173 * Compare the attributes of two be_Call nodes.
175 * @return zero if both nodes have identically attributes
177 static int Call_cmp_attr(const ir_node *a, const ir_node *b)
179 const be_call_attr_t *a_attr = (const be_call_attr_t*)get_irn_generic_attr_const(a);
180 const be_call_attr_t *b_attr = (const be_call_attr_t*)get_irn_generic_attr_const(b);
182 if (a_attr->ent != b_attr->ent ||
183 a_attr->call_tp != b_attr->call_tp)
186 return be_nodes_equal(a, b);
189 static arch_register_req_t *allocate_reg_req(const ir_node *node)
191 ir_graph *irg = get_irn_irg(node);
192 struct obstack *obst = be_get_be_obst(irg);
194 arch_register_req_t *req = OALLOCZ(obst, arch_register_req_t);
198 void be_set_constr_in(ir_node *node, int pos, const arch_register_req_t *req)
200 backend_info_t *info = be_get_info(node);
201 assert(pos < get_irn_arity(node));
202 info->in_reqs[pos] = req;
205 void be_set_constr_out(ir_node *node, int pos, const arch_register_req_t *req)
207 backend_info_t *info = be_get_info(node);
208 info->out_infos[pos].req = req;
212 * Initializes the generic attribute of all be nodes and return it.
214 static void init_node_attr(ir_node *node, int n_inputs, int n_outputs)
216 assert(n_outputs >= 0);
218 ir_graph *irg = get_irn_irg(node);
219 struct obstack *obst = be_get_be_obst(irg);
220 backend_info_t *info = be_get_info(node);
221 const arch_register_req_t **in_reqs;
225 assert(n_inputs == get_irn_arity(node));
226 in_reqs = OALLOCN(obst, const arch_register_req_t*, n_inputs);
227 for (i = 0; i < n_inputs; ++i) {
228 in_reqs[i] = arch_no_register_req;
231 in_reqs = NEW_ARR_F(const arch_register_req_t*, 0);
233 info->in_reqs = in_reqs;
235 info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_outputs);
236 memset(info->out_infos, 0, n_outputs * sizeof(info->out_infos[0]));
237 for (int i = 0; i < n_outputs; ++i) {
238 info->out_infos[i].req = arch_no_register_req;
242 static void add_register_req_in(ir_node *node, const arch_register_req_t *req)
244 backend_info_t *info = be_get_info(node);
245 ARR_APP1(const arch_register_req_t*, info->in_reqs, req);
248 ir_node *be_new_Spill(const arch_register_class_t *cls,
249 const arch_register_class_t *cls_frame, ir_node *bl,
250 ir_node *frame, ir_node *to_spill)
255 ir_graph *irg = get_Block_irg(bl);
259 res = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
260 init_node_attr(res, 2, 1);
261 a = (be_frame_attr_t*) get_irn_generic_attr(res);
264 a->base.exc.pin_state = op_pin_state_pinned;
266 be_node_set_reg_class_in(res, n_be_Spill_frame, cls_frame);
267 be_node_set_reg_class_in(res, n_be_Spill_val, cls);
268 arch_set_irn_register_req_out(res, 0, arch_no_register_req);
273 ir_node *be_new_Reload(const arch_register_class_t *cls,
274 const arch_register_class_t *cls_frame, ir_node *block,
275 ir_node *frame, ir_node *mem, ir_mode *mode)
279 ir_graph *irg = get_Block_irg(block);
284 res = new_ir_node(NULL, irg, block, op_be_Reload, mode, 2, in);
286 init_node_attr(res, 2, 1);
287 be_node_set_reg_class_out(res, 0, cls);
289 be_node_set_reg_class_in(res, n_be_Reload_frame, cls_frame);
290 arch_set_irn_flags(res, arch_irn_flags_rematerializable);
292 a = (be_frame_attr_t*) get_irn_generic_attr(res);
295 a->base.exc.pin_state = op_pin_state_pinned;
300 ir_node *be_get_Reload_mem(const ir_node *irn)
302 assert(be_is_Reload(irn));
303 return get_irn_n(irn, n_be_Reload_mem);
306 ir_node *be_get_Reload_frame(const ir_node *irn)
308 assert(be_is_Reload(irn));
309 return get_irn_n(irn, n_be_Reload_frame);
312 ir_node *be_get_Spill_val(const ir_node *irn)
314 assert(be_is_Spill(irn));
315 return get_irn_n(irn, n_be_Spill_val);
318 ir_node *be_get_Spill_frame(const ir_node *irn)
320 assert(be_is_Spill(irn));
321 return get_irn_n(irn, n_be_Spill_frame);
324 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_node *block,
325 int n, ir_node *in[])
328 ir_graph *irg = get_Block_irg(block);
329 be_node_attr_t *attr;
331 ir_node *irn = new_ir_node(NULL, irg, block, op_be_Perm, mode_T, n, in);
332 init_node_attr(irn, n, n);
333 attr = (be_node_attr_t*) get_irn_generic_attr(irn);
334 attr->exc.pin_state = op_pin_state_pinned;
335 for (i = 0; i < n; ++i) {
336 const ir_node *input = in[i];
337 const arch_register_req_t *req = arch_get_irn_register_req(input);
338 if (req->width == 1) {
339 be_set_constr_in(irn, i, cls->class_req);
340 be_set_constr_out(irn, i, cls->class_req);
342 arch_register_req_t *new_req = allocate_reg_req(irn);
344 new_req->type = (req->type & arch_register_req_type_aligned);
345 new_req->width = req->width;
346 be_set_constr_in(irn, i, new_req);
347 be_set_constr_out(irn, i, new_req);
354 void be_Perm_reduce(ir_node *perm, int new_size, int *map)
356 int arity = get_irn_arity(perm);
357 const arch_register_req_t **old_in_reqs
358 = ALLOCAN(const arch_register_req_t*, arity);
359 reg_out_info_t *old_infos = ALLOCAN(reg_out_info_t, arity);
360 backend_info_t *info = be_get_info(perm);
364 assert(be_is_Perm(perm));
365 assert(new_size <= arity);
367 new_in = ALLOCAN(ir_node*, new_size);
369 /* save the old register data */
370 memcpy(old_in_reqs, info->in_reqs, arity * sizeof(old_in_reqs[0]));
371 memcpy(old_infos, info->out_infos, arity * sizeof(old_infos[0]));
373 /* compose the new in array and set the new register data directly */
374 for (i = 0; i < new_size; ++i) {
376 new_in[i] = get_irn_n(perm, idx);
377 info->in_reqs[i] = old_in_reqs[idx];
378 info->out_infos[i] = old_infos[idx];
381 set_irn_in(perm, new_size, new_in);
384 ir_node *be_new_MemPerm(ir_node *block, int n, ir_node *in[])
386 ir_graph *irg = get_Block_irg(block);
387 const arch_env_t *arch_env = be_get_irg_arch_env(irg);
388 ir_node *frame = get_irg_frame(irg);
389 const arch_register_t *sp = arch_env->sp;
391 be_memperm_attr_t *attr;
394 real_in = ALLOCAN(ir_node*, n + 1);
396 memcpy(&real_in[1], in, n * sizeof(real_in[0]));
398 irn = new_ir_node(NULL, irg, block, op_be_MemPerm, mode_T, n+1, real_in);
400 init_node_attr(irn, n + 1, n);
401 be_node_set_reg_class_in(irn, 0, sp->reg_class);
403 attr = (be_memperm_attr_t*)get_irn_generic_attr(irn);
404 attr->in_entities = OALLOCNZ(irg->obst, ir_entity*, n);
405 attr->out_entities = OALLOCNZ(irg->obst, ir_entity*, n);
410 ir_node *be_new_Copy(ir_node *bl, ir_node *op)
414 arch_register_req_t *req;
415 be_node_attr_t *attr;
416 ir_graph *irg = get_Block_irg(bl);
417 const arch_register_req_t *in_req = arch_get_irn_register_req(op);
418 const arch_register_class_t *cls = in_req->cls;
421 res = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
422 init_node_attr(res, 1, 1);
423 attr = (be_node_attr_t*) get_irn_generic_attr(res);
424 attr->exc.pin_state = op_pin_state_floats;
425 be_node_set_reg_class_in(res, 0, cls);
426 be_node_set_reg_class_out(res, 0, cls);
428 req = allocate_reg_req(res);
430 req->type = arch_register_req_type_should_be_same
431 | (in_req->type & arch_register_req_type_aligned);
432 req->other_same = 1U << 0;
433 req->width = in_req->width;
434 be_set_constr_out(res, 0, req);
439 ir_node *be_get_Copy_op(const ir_node *cpy)
441 return get_irn_n(cpy, n_be_Copy_op);
444 ir_node *be_new_Keep(ir_node *block, int n, ir_node *in[])
448 ir_graph *irg = get_Block_irg(block);
449 be_node_attr_t *attr;
451 res = new_ir_node(NULL, irg, block, op_be_Keep, mode_ANY, -1, NULL);
452 init_node_attr(res, -1, 1);
453 attr = (be_node_attr_t*) get_irn_generic_attr(res);
454 attr->exc.pin_state = op_pin_state_pinned;
456 for (i = 0; i < n; ++i) {
457 ir_node *pred = in[i];
458 add_irn_n(res, pred);
459 const arch_register_req_t *req = arch_get_irn_register_req(pred);
460 req = req->cls != NULL ? req->cls->class_req : arch_no_register_req;
461 add_register_req_in(res, req);
468 void be_Keep_add_node(ir_node *keep, const arch_register_class_t *cls, ir_node *node)
470 assert(be_is_Keep(keep));
471 add_irn_n(keep, node);
472 add_register_req_in(keep, cls->class_req);
475 ir_node *be_new_Call(dbg_info *dbg, ir_graph *irg, ir_node *bl, ir_node *mem,
476 const arch_register_req_t *sp_req, ir_node *sp,
477 const arch_register_req_t *ptr_req, ir_node *ptr,
478 int n_outs, int n, ir_node *in[], ir_type *call_tp)
481 int real_n = n_be_Call_first_arg + n;
485 NEW_ARR_A(ir_node *, real_in, real_n);
486 real_in[n_be_Call_mem] = mem;
487 real_in[n_be_Call_sp] = sp;
488 real_in[n_be_Call_ptr] = ptr;
489 memcpy(&real_in[n_be_Call_first_arg], in, n * sizeof(in[0]));
491 irn = new_ir_node(dbg, irg, bl, op_be_Call, mode_T, real_n, real_in);
492 init_node_attr(irn, real_n, n_outs);
493 a = (be_call_attr_t*)get_irn_generic_attr(irn);
495 a->call_tp = call_tp;
497 a->base.exc.pin_state = op_pin_state_pinned;
498 be_set_constr_in(irn, n_be_Call_sp, sp_req);
499 be_set_constr_in(irn, n_be_Call_ptr, ptr_req);
503 ir_entity *be_Call_get_entity(const ir_node *call)
505 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
506 assert(be_is_Call(call));
510 void be_Call_set_entity(ir_node *call, ir_entity *ent)
512 be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
513 assert(be_is_Call(call));
517 ir_type *be_Call_get_type(ir_node *call)
519 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
520 assert(be_is_Call(call));
524 void be_Call_set_type(ir_node *call, ir_type *call_tp)
526 be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
527 assert(be_is_Call(call));
528 a->call_tp = call_tp;
531 void be_Call_set_pop(ir_node *call, unsigned pop)
533 be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
537 unsigned be_Call_get_pop(const ir_node *call)
539 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
543 ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *block, int n_res,
544 unsigned pop, int n, ir_node *in[])
549 res = new_ir_node(dbg, irg, block, op_be_Return, mode_X, n, in);
550 init_node_attr(res, n, 1);
551 be_set_constr_out(res, 0, arch_no_register_req);
553 a = (be_return_attr_t*)get_irn_generic_attr(res);
554 a->num_ret_vals = n_res;
557 a->base.exc.pin_state = op_pin_state_pinned;
562 int be_Return_get_n_rets(const ir_node *ret)
564 const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
565 return a->num_ret_vals;
568 unsigned be_Return_get_pop(const ir_node *ret)
570 const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
574 int be_Return_get_emit_pop(const ir_node *ret)
576 const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
580 void be_Return_set_emit_pop(ir_node *ret, int emit_pop)
582 be_return_attr_t *a = (be_return_attr_t*)get_irn_generic_attr(ret);
583 a->emit_pop = emit_pop;
586 ir_node *be_new_IncSP(const arch_register_t *sp, ir_node *bl,
587 ir_node *old_sp, int offset, int align)
592 ir_graph *irg = get_Block_irg(bl);
595 irn = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode,
597 init_node_attr(irn, 1, 1);
598 a = (be_incsp_attr_t*)get_irn_generic_attr(irn);
601 a->base.exc.pin_state = op_pin_state_pinned;
603 /* Set output constraint to stack register. */
604 be_node_set_reg_class_in(irn, 0, sp->reg_class);
605 be_set_constr_single_reg_out(irn, 0, sp, arch_register_req_type_produces_sp);
610 ir_node *be_new_AddSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp,
614 ir_node *in[n_be_AddSP_last];
616 be_node_attr_t *attr;
618 in[n_be_AddSP_old_sp] = old_sp;
619 in[n_be_AddSP_size] = sz;
621 irg = get_Block_irg(bl);
622 irn = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T, n_be_AddSP_last, in);
623 init_node_attr(irn, n_be_AddSP_last, pn_be_AddSP_last);
624 attr = (be_node_attr_t*) get_irn_generic_attr(irn);
625 attr->exc.pin_state = op_pin_state_pinned;
627 /* Set output constraint to stack register. */
628 be_set_constr_single_reg_in(irn, n_be_AddSP_old_sp, sp,
629 arch_register_req_type_none);
630 be_node_set_reg_class_in(irn, n_be_AddSP_size, sp->reg_class);
631 be_set_constr_single_reg_out(irn, pn_be_AddSP_sp, sp,
632 arch_register_req_type_produces_sp);
637 ir_node *be_new_SubSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp, ir_node *sz)
640 ir_node *in[n_be_SubSP_last];
642 be_node_attr_t *attr;
644 in[n_be_SubSP_old_sp] = old_sp;
645 in[n_be_SubSP_size] = sz;
647 irg = get_Block_irg(bl);
648 irn = new_ir_node(NULL, irg, bl, op_be_SubSP, mode_T, n_be_SubSP_last, in);
649 init_node_attr(irn, n_be_SubSP_last, pn_be_SubSP_last);
650 attr = (be_node_attr_t*) get_irn_generic_attr(irn);
651 attr->exc.pin_state = op_pin_state_pinned;
653 /* Set output constraint to stack register. */
654 be_set_constr_single_reg_in(irn, n_be_SubSP_old_sp, sp,
655 arch_register_req_type_none);
656 be_node_set_reg_class_in(irn, n_be_SubSP_size, sp->reg_class);
657 be_set_constr_single_reg_out(irn, pn_be_SubSP_sp, sp, arch_register_req_type_produces_sp);
662 ir_node *be_new_Start(dbg_info *dbgi, ir_node *bl, int n_outs)
665 ir_graph *irg = get_Block_irg(bl);
666 be_node_attr_t *attr;
668 res = new_ir_node(dbgi, irg, bl, op_be_Start, mode_T, 0, NULL);
669 init_node_attr(res, 0, n_outs);
670 attr = (be_node_attr_t*) get_irn_generic_attr(res);
671 attr->exc.pin_state = op_pin_state_pinned;
676 ir_node *be_new_FrameAddr(const arch_register_class_t *cls_frame, ir_node *bl, ir_node *frame, ir_entity *ent)
681 ir_graph *irg = get_Block_irg(bl);
684 irn = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
685 init_node_attr(irn, 1, 1);
686 a = (be_frame_attr_t*)get_irn_generic_attr(irn);
689 a->base.exc.pin_state = op_pin_state_floats;
690 be_node_set_reg_class_in(irn, 0, cls_frame);
691 be_node_set_reg_class_out(irn, 0, cls_frame);
693 return optimize_node(irn);
696 ir_node *be_get_FrameAddr_frame(const ir_node *node)
698 assert(be_is_FrameAddr(node));
699 return get_irn_n(node, n_be_FrameAddr_ptr);
702 ir_entity *be_get_FrameAddr_entity(const ir_node *node)
704 const be_frame_attr_t *attr = (const be_frame_attr_t*)get_irn_generic_attr_const(node);
708 ir_node *be_new_CopyKeep(ir_node *bl, ir_node *src, int n, ir_node *in_keep[])
711 ir_node **in = ALLOCAN(ir_node*, n + 1);
712 ir_graph *irg = get_Block_irg(bl);
713 const arch_register_req_t *req = arch_get_irn_register_req(src);
714 const arch_register_class_t *cls = req->cls;
715 ir_mode *mode = get_irn_mode(src);
716 be_node_attr_t *attr;
719 memcpy(&in[1], in_keep, n * sizeof(in[0]));
720 irn = new_ir_node(NULL, irg, bl, op_be_CopyKeep, mode, n + 1, in);
721 init_node_attr(irn, n + 1, 1);
722 attr = (be_node_attr_t*) get_irn_generic_attr(irn);
723 attr->exc.pin_state = op_pin_state_floats;
724 be_node_set_reg_class_in(irn, 0, cls);
725 be_node_set_reg_class_out(irn, 0, cls);
726 for (int i = 0; i < n; ++i) {
727 ir_node *pred = in_keep[i];
728 const arch_register_req_t *req = arch_get_irn_register_req(pred);
729 req = req->cls != NULL ? req->cls->class_req : arch_no_register_req;
730 be_set_constr_in(irn, i+1, req);
736 ir_node *be_new_CopyKeep_single(ir_node *bl, ir_node *src, ir_node *keep)
738 return be_new_CopyKeep(bl, src, 1, &keep);
741 ir_node *be_get_CopyKeep_op(const ir_node *cpy)
743 return get_irn_n(cpy, n_be_CopyKeep_op);
746 void be_set_CopyKeep_op(ir_node *cpy, ir_node *op)
748 set_irn_n(cpy, n_be_CopyKeep_op, op);
751 static bool be_has_frame_entity(const ir_node *irn)
753 switch (get_irn_opcode(irn)) {
763 ir_entity *be_get_frame_entity(const ir_node *irn)
765 if (be_has_frame_entity(irn)) {
766 const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
772 int be_get_frame_offset(const ir_node *irn)
774 assert(is_be_node(irn));
775 if (be_has_frame_entity(irn)) {
776 const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
782 void be_set_MemPerm_in_entity(const ir_node *irn, int n, ir_entity *ent)
784 const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
786 assert(be_is_MemPerm(irn));
787 assert(n < be_get_MemPerm_entity_arity(irn));
789 attr->in_entities[n] = ent;
792 ir_entity* be_get_MemPerm_in_entity(const ir_node* irn, int n)
794 const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
796 assert(be_is_MemPerm(irn));
797 assert(n < be_get_MemPerm_entity_arity(irn));
799 return attr->in_entities[n];
802 void be_set_MemPerm_out_entity(const ir_node *irn, int n, ir_entity *ent)
804 const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
806 assert(be_is_MemPerm(irn));
807 assert(n < be_get_MemPerm_entity_arity(irn));
809 attr->out_entities[n] = ent;
812 ir_entity* be_get_MemPerm_out_entity(const ir_node* irn, int n)
814 const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
816 assert(be_is_MemPerm(irn));
817 assert(n < be_get_MemPerm_entity_arity(irn));
819 return attr->out_entities[n];
822 int be_get_MemPerm_entity_arity(const ir_node *irn)
824 return get_irn_arity(irn) - 1;
827 const arch_register_req_t *be_create_reg_req(struct obstack *obst,
828 const arch_register_t *reg, arch_register_req_type_t additional_types)
830 arch_register_req_t *req = OALLOC(obst, arch_register_req_t);
831 const arch_register_class_t *cls = reg->reg_class;
832 unsigned *limited_bitset;
834 limited_bitset = rbitset_obstack_alloc(obst, arch_register_class_n_regs(cls));
835 rbitset_set(limited_bitset, reg->index);
837 req->type = arch_register_req_type_limited | additional_types;
839 req->limited = limited_bitset;
844 void be_set_constr_single_reg_in(ir_node *node, int pos,
845 const arch_register_t *reg, arch_register_req_type_t additional_types)
847 const arch_register_req_t *req;
849 if (additional_types == 0) {
850 req = reg->single_req;
852 ir_graph *irg = get_irn_irg(node);
853 struct obstack *obst = be_get_be_obst(irg);
854 req = be_create_reg_req(obst, reg, additional_types);
856 be_set_constr_in(node, pos, req);
859 void be_set_constr_single_reg_out(ir_node *node, int pos,
860 const arch_register_t *reg, arch_register_req_type_t additional_types)
862 ir_graph *irg = get_irn_irg(node);
863 be_irg_t *birg = be_birg_from_irg(irg);
864 const arch_register_req_t *req;
866 /* if we have an ignore register, add ignore flag and just assign it */
867 if (!rbitset_is_set(birg->allocatable_regs, reg->global_index)) {
868 additional_types |= arch_register_req_type_ignore;
871 if (additional_types == 0) {
872 req = reg->single_req;
874 struct obstack *obst = be_get_be_obst(irg);
875 req = be_create_reg_req(obst, reg, additional_types);
878 arch_set_irn_register_out(node, pos, reg);
879 be_set_constr_out(node, pos, req);
882 void be_node_set_reg_class_in(ir_node *irn, int pos,
883 const arch_register_class_t *cls)
885 be_set_constr_in(irn, pos, cls->class_req);
888 void be_node_set_reg_class_out(ir_node *irn, int pos,
889 const arch_register_class_t *cls)
891 be_set_constr_out(irn, pos, cls->class_req);
894 ir_node *be_get_IncSP_pred(ir_node *irn)
896 assert(be_is_IncSP(irn));
897 return get_irn_n(irn, 0);
900 void be_set_IncSP_pred(ir_node *incsp, ir_node *pred)
902 assert(be_is_IncSP(incsp));
903 set_irn_n(incsp, 0, pred);
906 void be_set_IncSP_offset(ir_node *irn, int offset)
908 be_incsp_attr_t *a = (be_incsp_attr_t*)get_irn_generic_attr(irn);
909 assert(be_is_IncSP(irn));
913 int be_get_IncSP_offset(const ir_node *irn)
915 const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
916 assert(be_is_IncSP(irn));
920 int be_get_IncSP_align(const ir_node *irn)
922 const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
923 assert(be_is_IncSP(irn));
927 static ir_entity *be_node_get_frame_entity(const ir_node *irn)
929 return be_get_frame_entity(irn);
932 void be_node_set_frame_entity(ir_node *irn, ir_entity *ent)
936 assert(be_has_frame_entity(irn));
938 a = (be_frame_attr_t*)get_irn_generic_attr(irn);
942 static void be_node_set_frame_offset(ir_node *irn, int offset)
946 if (!be_has_frame_entity(irn))
949 a = (be_frame_attr_t*)get_irn_generic_attr(irn);
953 static int be_node_get_sp_bias(const ir_node *irn)
955 if (be_is_IncSP(irn))
956 return be_get_IncSP_offset(irn);
958 return -(int)be_Call_get_pop(irn);
966 static const arch_irn_ops_t be_node_irn_ops = {
967 be_node_get_frame_entity,
968 be_node_set_frame_offset,
970 NULL, /* get_op_estimated_cost */
971 NULL, /* possible_memory_operand */
972 NULL, /* perform_memory_operand */
975 static int get_start_reg_index(ir_graph *irg, const arch_register_t *reg)
977 ir_node *start = get_irg_start(irg);
978 unsigned n_outs = arch_get_irn_n_outs(start);
981 /* do a naive linear search... */
982 for (i = 0; i < (int)n_outs; ++i) {
983 arch_register_req_t const *const out_req = arch_get_irn_register_req_out(start, i);
984 if (!arch_register_req_is(out_req, limited))
986 if (out_req->cls != reg->reg_class)
988 if (!rbitset_is_set(out_req->limited, reg->index))
992 panic("Tried querying undefined register '%s' at Start", reg->name);
995 ir_node *be_get_initial_reg_value(ir_graph *irg, const arch_register_t *reg)
997 int i = get_start_reg_index(irg, reg);
998 ir_node *start = get_irg_start(irg);
999 ir_mode *mode = arch_register_class_mode(reg->reg_class);
1001 foreach_out_edge(start, edge) {
1002 ir_node *proj = get_edge_src_irn(edge);
1003 if (!is_Proj(proj)) // maybe End/Anchor
1005 if (get_Proj_proj(proj) == i) {
1009 return new_r_Proj(start, mode, i);
1012 int be_find_return_reg_input(ir_node *ret, const arch_register_t *reg)
1014 int arity = get_irn_arity(ret);
1016 /* do a naive linear search... */
1017 for (i = 0; i < arity; ++i) {
1018 const arch_register_req_t *req = arch_get_irn_register_req_in(ret, i);
1019 if (!arch_register_req_is(req, limited))
1021 if (req->cls != reg->reg_class)
1023 if (!rbitset_is_set(req->limited, reg->index))
1027 panic("Tried querying undefined register '%s' at Return", reg->name);
1030 static ir_entity* dummy_get_frame_entity(const ir_node *node)
1036 static void dummy_set_frame_offset(ir_node *node, int bias)
1040 panic("should not be called");
1043 static int dummy_get_sp_bias(const ir_node *node)
1049 /* for "middleend" nodes */
1050 static const arch_irn_ops_t dummy_be_irn_ops = {
1051 dummy_get_frame_entity,
1052 dummy_set_frame_offset,
1054 NULL, /* get_op_estimated_cost */
1055 NULL, /* possible_memory_operand */
1056 NULL, /* perform_memory_operand */
1061 ir_node *be_new_Phi(ir_node *block, int n_ins, ir_node **ins, ir_mode *mode,
1062 const arch_register_req_t *req)
1064 ir_graph *irg = get_irn_irg(block);
1065 struct obstack *obst = be_get_be_obst(irg);
1066 backend_info_t *info;
1069 ir_node *phi = new_ir_node(NULL, irg, block, op_Phi, mode, n_ins, ins);
1070 phi->attr.phi.u.backedge = new_backedge_arr(irg->obst, n_ins);
1071 info = be_get_info(phi);
1072 info->out_infos = NEW_ARR_D(reg_out_info_t, obst, 1);
1073 memset(info->out_infos, 0, 1 * sizeof(info->out_infos[0]));
1074 info->in_reqs = OALLOCN(obst, const arch_register_req_t*, n_ins);
1076 info->out_infos[0].req = req;
1077 for (i = 0; i < n_ins; ++i) {
1078 info->in_reqs[i] = req;
1080 irn_verify_irg(phi, irg);
1081 phi = optimize_node(phi);
1085 void be_set_phi_reg_req(ir_node *node, const arch_register_req_t *req)
1087 int arity = get_irn_arity(node);
1090 backend_info_t *info = be_get_info(node);
1091 info->out_infos[0].req = req;
1092 for (i = 0; i < arity; ++i) {
1093 info->in_reqs[i] = req;
1096 assert(mode_is_datab(get_irn_mode(node)));
1099 void be_dump_phi_reg_reqs(FILE *F, const ir_node *node, dump_reason_t reason)
1102 case dump_node_opcode_txt:
1103 fputs(get_op_name(get_irn_op(node)), F);
1105 case dump_node_mode_txt:
1106 fprintf(F, "%s", get_mode_name(get_irn_mode(node)));
1108 case dump_node_nodeattr_txt:
1110 case dump_node_info_txt:
1112 backend_info_t *info = be_get_info(node);
1113 if (info != NULL && info->out_infos[0].req != NULL) {
1114 arch_dump_reqs_and_registers(F, node);
1124 static const arch_irn_ops_t phi_irn_ops = {
1125 dummy_get_frame_entity,
1126 dummy_set_frame_offset,
1128 NULL, /* get_op_estimated_cost */
1129 NULL, /* possible_memory_operand */
1130 NULL, /* perform_memory_operand */
1136 * ir_op-Operation: dump a be node to file
1138 static void dump_node(FILE *f, const ir_node *irn, dump_reason_t reason)
1140 assert(is_be_node(irn));
1143 case dump_node_opcode_txt:
1144 fputs(get_op_name(get_irn_op(irn)), f);
1146 case dump_node_mode_txt:
1147 if (be_is_Copy(irn) || be_is_CopyKeep(irn)) {
1148 fprintf(f, "%s", get_mode_name(get_irn_mode(irn)));
1151 case dump_node_nodeattr_txt:
1152 if (be_is_Call(irn)) {
1153 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(irn);
1155 fprintf(f, " [%s] ", get_entity_name(a->ent));
1157 if (be_is_IncSP(irn)) {
1158 const be_incsp_attr_t *attr = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
1159 fprintf(f, " [%d] ", attr->offset);
1162 case dump_node_info_txt:
1163 arch_dump_reqs_and_registers(f, irn);
1165 if (be_has_frame_entity(irn)) {
1166 const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
1168 unsigned size = get_type_size_bytes(get_entity_type(a->ent));
1169 ir_fprintf(f, "frame entity: %+F, offset 0x%x (%d), size 0x%x (%d) bytes\n",
1170 a->ent, a->offset, a->offset, size, size);
1175 switch (get_irn_opcode(irn)) {
1177 const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
1178 fprintf(f, "align: %d\n", a->align);
1179 fprintf(f, "offset: %d\n", a->offset);
1183 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(irn);
1186 fprintf(f, "\ncalling: %s\n", get_entity_name(a->ent));
1191 for (i = 0; i < be_get_MemPerm_entity_arity(irn); ++i) {
1192 ir_entity *in, *out;
1193 in = be_get_MemPerm_in_entity(irn, i);
1194 out = be_get_MemPerm_out_entity(irn, i);
1196 fprintf(f, "\nin[%d]: %s\n", i, get_entity_name(in));
1199 fprintf(f, "\nout[%d]: %s\n", i, get_entity_name(out));
1213 * Copies the backend specific attributes from old node to new node.
1215 static void copy_attr(ir_graph *irg, const ir_node *old_node, ir_node *new_node)
1217 const void *old_attr = get_irn_generic_attr_const(old_node);
1218 void *new_attr = get_irn_generic_attr(new_node);
1219 struct obstack *obst = be_get_be_obst(irg);
1220 backend_info_t *old_info = be_get_info(old_node);
1221 backend_info_t *new_info = be_get_info(new_node);
1223 assert(is_be_node(old_node));
1224 assert(is_be_node(new_node));
1226 memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
1228 new_info->flags = old_info->flags;
1229 new_info->out_infos = old_info->out_infos ? DUP_ARR_D(reg_out_info_t, obst, old_info->out_infos) : NULL;
1232 if (old_info->in_reqs != NULL) {
1233 unsigned n_ins = get_irn_arity(old_node);
1234 /* need dynamic in infos? */
1235 if (get_irn_op(old_node)->opar == oparity_dynamic) {
1236 new_info->in_reqs = NEW_ARR_F(const arch_register_req_t*, n_ins);
1238 new_info->in_reqs = OALLOCN(obst,const arch_register_req_t*, n_ins);
1240 memcpy(new_info->in_reqs, old_info->in_reqs,
1241 n_ins * sizeof(new_info->in_reqs[0]));
1243 new_info->in_reqs = NULL;
1247 int is_be_node(const ir_node *irn)
1249 return get_op_ops(get_irn_op(irn))->be_ops == &be_node_irn_ops;
1252 static ir_op *new_be_op(unsigned code, const char *name, op_pin_state p,
1253 irop_flags flags, op_arity opar, size_t attr_size)
1255 ir_op *res = new_ir_op(code, name, p, flags, opar, 0, attr_size);
1256 res->ops.dump_node = dump_node;
1257 res->ops.copy_attr = copy_attr;
1258 res->ops.be_ops = &be_node_irn_ops;
1262 void be_init_op(void)
1266 assert(op_be_Spill == NULL);
1268 /* Acquire all needed opcodes. */
1269 op_be_Spill = new_be_op(beo_Spill, "be_Spill", op_pin_state_exc_pinned, irop_flag_none, oparity_unary, sizeof(be_frame_attr_t));
1270 op_be_Reload = new_be_op(beo_Reload, "be_Reload", op_pin_state_exc_pinned, irop_flag_none, oparity_zero, sizeof(be_frame_attr_t));
1271 op_be_Perm = new_be_op(beo_Perm, "be_Perm", op_pin_state_exc_pinned, irop_flag_none, oparity_variable, sizeof(be_node_attr_t));
1272 op_be_MemPerm = new_be_op(beo_MemPerm, "be_MemPerm", op_pin_state_exc_pinned, irop_flag_none, oparity_variable, sizeof(be_memperm_attr_t));
1273 op_be_Copy = new_be_op(beo_Copy, "be_Copy", op_pin_state_exc_pinned, irop_flag_none, oparity_unary, sizeof(be_node_attr_t));
1274 op_be_Keep = new_be_op(beo_Keep, "be_Keep", op_pin_state_exc_pinned, irop_flag_keep, oparity_dynamic, sizeof(be_node_attr_t));
1275 op_be_CopyKeep = new_be_op(beo_CopyKeep, "be_CopyKeep", op_pin_state_exc_pinned, irop_flag_keep, oparity_variable, sizeof(be_node_attr_t));
1276 op_be_Call = new_be_op(beo_Call, "be_Call", op_pin_state_exc_pinned, irop_flag_fragile|irop_flag_uses_memory, oparity_variable, sizeof(be_call_attr_t));
1277 ir_op_set_memory_index(op_be_Call, n_be_Call_mem);
1278 ir_op_set_fragile_indices(op_be_Call, pn_be_Call_X_regular, pn_be_Call_X_except);
1279 op_be_Return = new_be_op(beo_Return, "be_Return", op_pin_state_exc_pinned, irop_flag_cfopcode, oparity_variable, sizeof(be_return_attr_t));
1280 op_be_AddSP = new_be_op(beo_AddSP, "be_AddSP", op_pin_state_exc_pinned, irop_flag_none, oparity_unary, sizeof(be_node_attr_t));
1281 op_be_SubSP = new_be_op(beo_SubSP, "be_SubSP", op_pin_state_exc_pinned, irop_flag_none, oparity_unary, sizeof(be_node_attr_t));
1282 op_be_IncSP = new_be_op(beo_IncSP, "be_IncSP", op_pin_state_exc_pinned, irop_flag_none, oparity_unary, sizeof(be_incsp_attr_t));
1283 op_be_Start = new_be_op(beo_Start, "be_Start", op_pin_state_exc_pinned, irop_flag_none, oparity_zero, sizeof(be_node_attr_t));
1284 op_be_FrameAddr = new_be_op(beo_FrameAddr, "be_FrameAddr", op_pin_state_exc_pinned, irop_flag_none, oparity_unary, sizeof(be_frame_attr_t));
1286 op_be_Spill->ops.node_cmp_attr = FrameAddr_cmp_attr;
1287 op_be_Reload->ops.node_cmp_attr = FrameAddr_cmp_attr;
1288 op_be_Perm->ops.node_cmp_attr = be_nodes_equal;
1289 op_be_MemPerm->ops.node_cmp_attr = be_nodes_equal;
1290 op_be_Copy->ops.node_cmp_attr = be_nodes_equal;
1291 op_be_Keep->ops.node_cmp_attr = be_nodes_equal;
1292 op_be_CopyKeep->ops.node_cmp_attr = be_nodes_equal;
1293 op_be_Call->ops.node_cmp_attr = Call_cmp_attr;
1294 op_be_Return->ops.node_cmp_attr = Return_cmp_attr;
1295 op_be_AddSP->ops.node_cmp_attr = be_nodes_equal;
1296 op_be_SubSP->ops.node_cmp_attr = be_nodes_equal;
1297 op_be_IncSP->ops.node_cmp_attr = IncSP_cmp_attr;
1298 op_be_Start->ops.node_cmp_attr = be_nodes_equal;
1299 op_be_FrameAddr->ops.node_cmp_attr = FrameAddr_cmp_attr;
1301 /* attach out dummy_ops to middle end nodes */
1302 for (opc = iro_First; opc <= iro_Last; ++opc) {
1303 ir_op *op = ir_get_opcode(opc);
1304 assert(op->ops.be_ops == NULL);
1305 op->ops.be_ops = &dummy_be_irn_ops;
1308 op_Phi->ops.be_ops = &phi_irn_ops;
1311 void be_finish_op(void)
1313 free_ir_op(op_be_Spill); op_be_Spill = NULL;
1314 free_ir_op(op_be_Reload); op_be_Reload = NULL;
1315 free_ir_op(op_be_Perm); op_be_Perm = NULL;
1316 free_ir_op(op_be_MemPerm); op_be_MemPerm = NULL;
1317 free_ir_op(op_be_Copy); op_be_Copy = NULL;
1318 free_ir_op(op_be_Keep); op_be_Keep = NULL;
1319 free_ir_op(op_be_CopyKeep); op_be_CopyKeep = NULL;
1320 free_ir_op(op_be_Call); op_be_Call = NULL;
1321 free_ir_op(op_be_Return); op_be_Return = NULL;
1322 free_ir_op(op_be_IncSP); op_be_IncSP = NULL;
1323 free_ir_op(op_be_AddSP); op_be_AddSP = NULL;
1324 free_ir_op(op_be_SubSP); op_be_SubSP = NULL;
1325 free_ir_op(op_be_Start); op_be_Start = NULL;
1326 free_ir_op(op_be_FrameAddr); op_be_FrameAddr = NULL;