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
27 * Backend node support for generic backend nodes.
28 * This file provides Perm, Copy, Spill and Reload nodes.
40 #include "bitfiddle.h"
41 #include "raw_bitset.h"
61 typedef struct be_node_attr_t {
65 /** The be_Return nodes attribute type. */
68 int num_ret_vals; /**< number of return values */
69 unsigned pop; /**< number of bytes that should be popped */
70 int emit_pop; /**< if set, emit pop bytes, even if pop = 0 */
73 /** The be_IncSP attribute type. */
76 int offset; /**< The offset by which the stack shall be
78 int align; /**< whether stack should be aligned after the
82 /** The be_Frame attribute type. */
89 /** The be_Call attribute type. */
92 ir_entity *ent; /**< called entity if this is a static call. */
94 ir_type *call_tp; /**< call type, copied from the original Call */
99 ir_entity **in_entities;
100 ir_entity **out_entities;
106 ir_op *op_be_MemPerm;
109 ir_op *op_be_CopyKeep;
116 ir_op *op_be_FrameAddr;
119 * Compare the attributes of two be_FrameAddr nodes.
121 * @return zero if both nodes have identically attributes
123 static int FrameAddr_cmp_attr(const ir_node *a, const ir_node *b)
125 const be_frame_attr_t *a_attr = (const be_frame_attr_t*)get_irn_generic_attr_const(a);
126 const be_frame_attr_t *b_attr = (const be_frame_attr_t*)get_irn_generic_attr_const(b);
128 if (a_attr->ent != b_attr->ent || a_attr->offset != b_attr->offset)
131 return be_nodes_equal(a, b);
135 * Compare the attributes of two be_Return nodes.
137 * @return zero if both nodes have identically attributes
139 static int Return_cmp_attr(const ir_node *a, const ir_node *b)
141 const be_return_attr_t *a_attr = (const be_return_attr_t*)get_irn_generic_attr_const(a);
142 const be_return_attr_t *b_attr = (const be_return_attr_t*)get_irn_generic_attr_const(b);
144 if (a_attr->num_ret_vals != b_attr->num_ret_vals)
146 if (a_attr->pop != b_attr->pop)
148 if (a_attr->emit_pop != b_attr->emit_pop)
151 return be_nodes_equal(a, b);
155 * Compare the attributes of two be_IncSP nodes.
157 * @return zero if both nodes have identically attributes
159 static int IncSP_cmp_attr(const ir_node *a, const ir_node *b)
161 const be_incsp_attr_t *a_attr = (const be_incsp_attr_t*)get_irn_generic_attr_const(a);
162 const be_incsp_attr_t *b_attr = (const be_incsp_attr_t*)get_irn_generic_attr_const(b);
164 if (a_attr->offset != b_attr->offset)
167 return be_nodes_equal(a, b);
171 * Compare the attributes of two be_Call nodes.
173 * @return zero if both nodes have identically attributes
175 static int Call_cmp_attr(const ir_node *a, const ir_node *b)
177 const be_call_attr_t *a_attr = (const be_call_attr_t*)get_irn_generic_attr_const(a);
178 const be_call_attr_t *b_attr = (const be_call_attr_t*)get_irn_generic_attr_const(b);
180 if (a_attr->ent != b_attr->ent ||
181 a_attr->call_tp != b_attr->call_tp)
184 return be_nodes_equal(a, b);
187 static arch_register_req_t *allocate_reg_req(const ir_node *node)
189 ir_graph *irg = get_irn_irg(node);
190 struct obstack *obst = be_get_be_obst(irg);
192 arch_register_req_t *req = OALLOCZ(obst, arch_register_req_t);
196 void be_set_constr_in(ir_node *node, int pos, const arch_register_req_t *req)
198 backend_info_t *info = be_get_info(node);
199 assert(pos < get_irn_arity(node));
200 info->in_reqs[pos] = req;
203 void be_set_constr_out(ir_node *node, int pos, const arch_register_req_t *req)
205 backend_info_t *info = be_get_info(node);
206 info->out_infos[pos].req = req;
210 * Initializes the generic attribute of all be nodes and return it.
212 static void init_node_attr(ir_node *node, int n_inputs, int n_outputs)
214 ir_graph *irg = get_irn_irg(node);
215 struct obstack *obst = be_get_be_obst(irg);
216 backend_info_t *info = be_get_info(node);
217 const arch_register_req_t **in_reqs;
221 assert(n_inputs == get_irn_arity(node));
222 in_reqs = OALLOCN(obst, const arch_register_req_t*, n_inputs);
223 for (i = 0; i < n_inputs; ++i) {
224 in_reqs[i] = arch_no_register_req;
227 in_reqs = NEW_ARR_F(const arch_register_req_t*, 0);
229 info->in_reqs = in_reqs;
231 if (n_outputs >= 0) {
233 info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_outputs);
234 memset(info->out_infos, 0, n_outputs * sizeof(info->out_infos[0]));
235 for (i = 0; i < n_outputs; ++i) {
236 info->out_infos[i].req = arch_no_register_req;
239 info->out_infos = NEW_ARR_F(reg_out_info_t, 0);
243 static void add_register_req_out(ir_node *node)
245 backend_info_t *info = be_get_info(node);
246 reg_out_info_t out_info;
247 memset(&out_info, 0, sizeof(out_info));
248 out_info.req = arch_no_register_req;
249 ARR_APP1(reg_out_info_t, info->out_infos, out_info);
252 static void add_register_req_in(ir_node *node)
254 backend_info_t *info = be_get_info(node);
255 ARR_APP1(const arch_register_req_t*, info->in_reqs, arch_no_register_req);
258 ir_node *be_new_Spill(const arch_register_class_t *cls,
259 const arch_register_class_t *cls_frame, ir_node *bl,
260 ir_node *frame, ir_node *to_spill)
265 ir_graph *irg = get_Block_irg(bl);
269 res = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
270 init_node_attr(res, 2, 1);
271 a = (be_frame_attr_t*) get_irn_generic_attr(res);
274 a->base.exc.pin_state = op_pin_state_pinned;
276 be_node_set_reg_class_in(res, n_be_Spill_frame, cls_frame);
277 be_node_set_reg_class_in(res, n_be_Spill_val, cls);
279 * For spills and reloads, we return "none" as requirement for frame
280 * pointer, so every input is ok. Some backends need this (STA).
281 * Matze: we should investigate if this is really needed, this solution
282 * looks very hacky to me
284 be_set_constr_in(res, n_be_Spill_frame, arch_no_register_req);
286 arch_set_out_register_req(res, 0, arch_no_register_req);
291 ir_node *be_new_Reload(const arch_register_class_t *cls,
292 const arch_register_class_t *cls_frame, ir_node *block,
293 ir_node *frame, ir_node *mem, ir_mode *mode)
297 ir_graph *irg = get_Block_irg(block);
302 res = new_ir_node(NULL, irg, block, op_be_Reload, mode, 2, in);
304 init_node_attr(res, 2, 1);
305 be_node_set_reg_class_out(res, 0, cls);
307 be_node_set_reg_class_in(res, n_be_Reload_frame, cls_frame);
308 arch_irn_set_flags(res, arch_irn_flags_rematerializable);
310 a = (be_frame_attr_t*) get_irn_generic_attr(res);
313 a->base.exc.pin_state = op_pin_state_pinned;
316 * For spills and reloads, we return "none" as requirement for frame
317 * pointer, so every input is ok. Some backends need this (e.g. STA).
318 * Matze: we should investigate if this is really needed, this solution
319 * looks very hacky to me
321 be_set_constr_in(res, n_be_Reload_frame, arch_no_register_req);
326 ir_node *be_get_Reload_mem(const ir_node *irn)
328 assert(be_is_Reload(irn));
329 return get_irn_n(irn, n_be_Reload_mem);
332 ir_node *be_get_Reload_frame(const ir_node *irn)
334 assert(be_is_Reload(irn));
335 return get_irn_n(irn, n_be_Reload_frame);
338 ir_node *be_get_Spill_val(const ir_node *irn)
340 assert(be_is_Spill(irn));
341 return get_irn_n(irn, n_be_Spill_val);
344 ir_node *be_get_Spill_frame(const ir_node *irn)
346 assert(be_is_Spill(irn));
347 return get_irn_n(irn, n_be_Spill_frame);
350 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_node *block,
351 int n, ir_node *in[])
354 ir_graph *irg = get_Block_irg(block);
355 be_node_attr_t *attr;
357 ir_node *irn = new_ir_node(NULL, irg, block, op_be_Perm, mode_T, n, in);
358 init_node_attr(irn, n, n);
359 attr = (be_node_attr_t*) get_irn_generic_attr(irn);
360 attr->exc.pin_state = op_pin_state_pinned;
361 for (i = 0; i < n; ++i) {
362 be_node_set_reg_class_in(irn, i, cls);
363 be_node_set_reg_class_out(irn, i, cls);
369 void be_Perm_reduce(ir_node *perm, int new_size, int *map)
371 int arity = get_irn_arity(perm);
372 const arch_register_req_t **old_in_reqs
373 = ALLOCAN(const arch_register_req_t*, arity);
374 reg_out_info_t *old_infos = ALLOCAN(reg_out_info_t, arity);
375 backend_info_t *info = be_get_info(perm);
379 assert(be_is_Perm(perm));
380 assert(new_size <= arity);
382 new_in = ALLOCAN(ir_node*, new_size);
384 /* save the old register data */
385 memcpy(old_in_reqs, info->in_reqs, arity * sizeof(old_in_reqs[0]));
386 memcpy(old_infos, info->out_infos, arity * sizeof(old_infos[0]));
388 /* compose the new in array and set the new register data directly */
389 for (i = 0; i < new_size; ++i) {
391 new_in[i] = get_irn_n(perm, idx);
392 info->in_reqs[i] = old_in_reqs[idx];
393 info->out_infos[i] = old_infos[idx];
396 set_irn_in(perm, new_size, new_in);
399 ir_node *be_new_MemPerm(ir_node *block, int n, ir_node *in[])
401 ir_graph *irg = get_Block_irg(block);
402 const arch_env_t *arch_env = be_get_irg_arch_env(irg);
403 ir_node *frame = get_irg_frame(irg);
404 const arch_register_t *sp = arch_env->sp;
406 be_memperm_attr_t *attr;
409 real_in = ALLOCAN(ir_node*, n + 1);
411 memcpy(&real_in[1], in, n * sizeof(real_in[0]));
413 irn = new_ir_node(NULL, irg, block, op_be_MemPerm, mode_T, n+1, real_in);
415 init_node_attr(irn, n + 1, n);
416 be_node_set_reg_class_in(irn, 0, sp->reg_class);
418 attr = (be_memperm_attr_t*)get_irn_generic_attr(irn);
419 attr->in_entities = OALLOCNZ(irg->obst, ir_entity*, n);
420 attr->out_entities = OALLOCNZ(irg->obst, ir_entity*, n);
425 ir_node *be_new_Copy(const arch_register_class_t *cls, ir_node *bl, ir_node *op)
429 arch_register_req_t *req;
430 be_node_attr_t *attr;
431 ir_graph *irg = get_Block_irg(bl);
434 res = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
435 init_node_attr(res, 1, 1);
436 attr = (be_node_attr_t*) get_irn_generic_attr(res);
437 attr->exc.pin_state = op_pin_state_floats;
438 be_node_set_reg_class_in(res, 0, cls);
439 be_node_set_reg_class_out(res, 0, cls);
441 req = allocate_reg_req(res);
443 req->type = arch_register_req_type_should_be_same;
444 req->other_same = 1U << 0;
446 be_set_constr_out(res, 0, req);
451 ir_node *be_get_Copy_op(const ir_node *cpy)
453 return get_irn_n(cpy, n_be_Copy_op);
456 void be_set_Copy_op(ir_node *cpy, ir_node *op)
458 set_irn_n(cpy, n_be_Copy_op, op);
461 ir_node *be_new_Keep(ir_node *block, int n, ir_node *in[])
465 ir_graph *irg = get_Block_irg(block);
466 be_node_attr_t *attr;
468 res = new_ir_node(NULL, irg, block, op_be_Keep, mode_ANY, -1, NULL);
469 init_node_attr(res, -1, 1);
470 attr = (be_node_attr_t*) get_irn_generic_attr(res);
471 attr->exc.pin_state = op_pin_state_pinned;
473 for (i = 0; i < n; ++i) {
474 add_irn_n(res, in[i]);
475 add_register_req_in(res);
482 void be_Keep_add_node(ir_node *keep, const arch_register_class_t *cls, ir_node *node)
486 assert(be_is_Keep(keep));
487 n = add_irn_n(keep, node);
488 add_register_req_in(keep);
489 be_node_set_reg_class_in(keep, n, cls);
492 ir_node *be_new_Call(dbg_info *dbg, ir_graph *irg, ir_node *bl, ir_node *mem,
493 ir_node *sp, ir_node *ptr, int n_outs, int n, ir_node *in[],
497 int real_n = n_be_Call_first_arg + n;
501 NEW_ARR_A(ir_node *, real_in, real_n);
502 real_in[n_be_Call_mem] = mem;
503 real_in[n_be_Call_sp] = sp;
504 real_in[n_be_Call_ptr] = ptr;
505 memcpy(&real_in[n_be_Call_first_arg], in, n * sizeof(in[0]));
507 irn = new_ir_node(dbg, irg, bl, op_be_Call, mode_T, real_n, real_in);
508 init_node_attr(irn, real_n, n_outs);
509 a = (be_call_attr_t*)get_irn_generic_attr(irn);
511 a->call_tp = call_tp;
513 a->base.exc.pin_state = op_pin_state_pinned;
517 ir_entity *be_Call_get_entity(const 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_entity(ir_node *call, ir_entity *ent)
526 be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
527 assert(be_is_Call(call));
531 ir_type *be_Call_get_type(ir_node *call)
533 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
534 assert(be_is_Call(call));
538 void be_Call_set_type(ir_node *call, ir_type *call_tp)
540 be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
541 assert(be_is_Call(call));
542 a->call_tp = call_tp;
545 void be_Call_set_pop(ir_node *call, unsigned pop)
547 be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
551 unsigned be_Call_get_pop(const ir_node *call)
553 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
557 ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *block, int n_res,
558 unsigned pop, int n, ir_node *in[])
564 res = new_ir_node(dbg, irg, block, op_be_Return, mode_X, -1, NULL);
565 init_node_attr(res, -1, 1);
566 for (i = 0; i < n; ++i) {
567 add_irn_n(res, in[i]);
568 add_register_req_in(res);
570 be_set_constr_out(res, 0, arch_no_register_req);
572 a = (be_return_attr_t*)get_irn_generic_attr(res);
573 a->num_ret_vals = n_res;
576 a->base.exc.pin_state = op_pin_state_pinned;
581 int be_Return_get_n_rets(const ir_node *ret)
583 const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
584 return a->num_ret_vals;
587 unsigned be_Return_get_pop(const ir_node *ret)
589 const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
593 int be_Return_get_emit_pop(const ir_node *ret)
595 const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
599 void be_Return_set_emit_pop(ir_node *ret, int emit_pop)
601 be_return_attr_t *a = (be_return_attr_t*)get_irn_generic_attr(ret);
602 a->emit_pop = emit_pop;
605 int be_Return_append_node(ir_node *ret, ir_node *node)
609 pos = add_irn_n(ret, node);
610 add_register_req_in(ret);
615 ir_node *be_new_IncSP(const arch_register_t *sp, ir_node *bl,
616 ir_node *old_sp, int offset, int align)
621 ir_graph *irg = get_Block_irg(bl);
624 irn = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode,
625 sizeof(in) / sizeof(in[0]), in);
626 init_node_attr(irn, 1, 1);
627 a = (be_incsp_attr_t*)get_irn_generic_attr(irn);
630 a->base.exc.pin_state = op_pin_state_pinned;
632 /* Set output constraint to stack register. */
633 be_node_set_reg_class_in(irn, 0, sp->reg_class);
634 be_set_constr_single_reg_out(irn, 0, sp, arch_register_req_type_produces_sp);
639 ir_node *be_new_AddSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp,
643 ir_node *in[n_be_AddSP_last];
644 const arch_register_class_t *cls;
646 be_node_attr_t *attr;
648 in[n_be_AddSP_old_sp] = old_sp;
649 in[n_be_AddSP_size] = sz;
651 irg = get_Block_irg(bl);
652 irn = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T, n_be_AddSP_last, in);
653 init_node_attr(irn, n_be_AddSP_last, pn_be_AddSP_last);
654 attr = (be_node_attr_t*) get_irn_generic_attr(irn);
655 attr->exc.pin_state = op_pin_state_pinned;
657 /* Set output constraint to stack register. */
658 be_set_constr_single_reg_in(irn, n_be_AddSP_old_sp, sp,
659 arch_register_req_type_none);
660 be_node_set_reg_class_in(irn, n_be_AddSP_size, arch_register_get_class(sp));
661 be_set_constr_single_reg_out(irn, pn_be_AddSP_sp, sp,
662 arch_register_req_type_produces_sp);
664 cls = arch_register_get_class(sp);
669 ir_node *be_new_SubSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp, ir_node *sz)
672 ir_node *in[n_be_SubSP_last];
674 be_node_attr_t *attr;
676 in[n_be_SubSP_old_sp] = old_sp;
677 in[n_be_SubSP_size] = sz;
679 irg = get_Block_irg(bl);
680 irn = new_ir_node(NULL, irg, bl, op_be_SubSP, mode_T, n_be_SubSP_last, in);
681 init_node_attr(irn, n_be_SubSP_last, pn_be_SubSP_last);
682 attr = (be_node_attr_t*) get_irn_generic_attr(irn);
683 attr->exc.pin_state = op_pin_state_pinned;
685 /* Set output constraint to stack register. */
686 be_set_constr_single_reg_in(irn, n_be_SubSP_old_sp, sp,
687 arch_register_req_type_none);
688 be_node_set_reg_class_in(irn, n_be_SubSP_size, arch_register_get_class(sp));
689 be_set_constr_single_reg_out(irn, pn_be_SubSP_sp, sp, arch_register_req_type_produces_sp);
694 ir_node *be_new_Start(dbg_info *dbgi, ir_node *bl, int n_outs)
698 ir_graph *irg = get_Block_irg(bl);
699 be_node_attr_t *attr;
701 res = new_ir_node(dbgi, irg, bl, op_be_Start, mode_T, 0, NULL);
702 init_node_attr(res, 0, -1);
703 attr = (be_node_attr_t*) get_irn_generic_attr(res);
704 attr->exc.pin_state = op_pin_state_pinned;
705 for (i = 0; i < n_outs; ++i) {
706 add_register_req_out(res);
712 ir_node *be_new_FrameAddr(const arch_register_class_t *cls_frame, ir_node *bl, ir_node *frame, ir_entity *ent)
717 ir_graph *irg = get_Block_irg(bl);
720 irn = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
721 init_node_attr(irn, 1, 1);
722 a = (be_frame_attr_t*)get_irn_generic_attr(irn);
725 a->base.exc.pin_state = op_pin_state_floats;
726 be_node_set_reg_class_in(irn, 0, cls_frame);
727 be_node_set_reg_class_out(irn, 0, cls_frame);
729 return optimize_node(irn);
732 ir_node *be_get_FrameAddr_frame(const ir_node *node)
734 assert(be_is_FrameAddr(node));
735 return get_irn_n(node, n_be_FrameAddr_ptr);
738 ir_entity *be_get_FrameAddr_entity(const ir_node *node)
740 const be_frame_attr_t *attr = (const be_frame_attr_t*)get_irn_generic_attr_const(node);
744 ir_node *be_new_CopyKeep(const arch_register_class_t *cls, ir_node *bl, ir_node *src, int n, ir_node *in_keep[], ir_mode *mode)
747 ir_node **in = ALLOCAN(ir_node*, n + 1);
748 ir_graph *irg = get_Block_irg(bl);
749 be_node_attr_t *attr;
752 memcpy(&in[1], in_keep, n * sizeof(in[0]));
753 irn = new_ir_node(NULL, irg, bl, op_be_CopyKeep, mode, n + 1, in);
754 init_node_attr(irn, n + 1, 1);
755 attr = (be_node_attr_t*) get_irn_generic_attr(irn);
756 attr->exc.pin_state = op_pin_state_floats;
757 be_node_set_reg_class_in(irn, 0, cls);
758 be_node_set_reg_class_out(irn, 0, cls);
763 ir_node *be_new_CopyKeep_single(const arch_register_class_t *cls, ir_node *bl, ir_node *src, ir_node *keep, ir_mode *mode)
765 return be_new_CopyKeep(cls, bl, src, 1, &keep, mode);
768 ir_node *be_get_CopyKeep_op(const ir_node *cpy)
770 return get_irn_n(cpy, n_be_CopyKeep_op);
773 void be_set_CopyKeep_op(ir_node *cpy, ir_node *op)
775 set_irn_n(cpy, n_be_CopyKeep_op, op);
778 static bool be_has_frame_entity(const ir_node *irn)
780 switch (get_irn_opcode(irn)) {
790 ir_entity *be_get_frame_entity(const ir_node *irn)
792 if (be_has_frame_entity(irn)) {
793 const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
799 int be_get_frame_offset(const ir_node *irn)
801 assert(is_be_node(irn));
802 if (be_has_frame_entity(irn)) {
803 const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
809 void be_set_MemPerm_in_entity(const ir_node *irn, int n, ir_entity *ent)
811 const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
813 assert(be_is_MemPerm(irn));
814 assert(n < be_get_MemPerm_entity_arity(irn));
816 attr->in_entities[n] = ent;
819 ir_entity* be_get_MemPerm_in_entity(const ir_node* irn, int n)
821 const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
823 assert(be_is_MemPerm(irn));
824 assert(n < be_get_MemPerm_entity_arity(irn));
826 return attr->in_entities[n];
829 void be_set_MemPerm_out_entity(const ir_node *irn, int n, ir_entity *ent)
831 const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
833 assert(be_is_MemPerm(irn));
834 assert(n < be_get_MemPerm_entity_arity(irn));
836 attr->out_entities[n] = ent;
839 ir_entity* be_get_MemPerm_out_entity(const ir_node* irn, int n)
841 const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
843 assert(be_is_MemPerm(irn));
844 assert(n < be_get_MemPerm_entity_arity(irn));
846 return attr->out_entities[n];
849 int be_get_MemPerm_entity_arity(const ir_node *irn)
851 return get_irn_arity(irn) - 1;
854 const arch_register_req_t *be_create_reg_req(struct obstack *obst,
855 const arch_register_t *reg, arch_register_req_type_t additional_types)
857 arch_register_req_t *req = OALLOC(obst, arch_register_req_t);
858 const arch_register_class_t *cls = arch_register_get_class(reg);
859 unsigned *limited_bitset;
861 limited_bitset = rbitset_obstack_alloc(obst, arch_register_class_n_regs(cls));
862 rbitset_set(limited_bitset, arch_register_get_index(reg));
864 req->type = arch_register_req_type_limited | additional_types;
866 req->limited = limited_bitset;
871 void be_set_constr_single_reg_in(ir_node *node, int pos,
872 const arch_register_t *reg, arch_register_req_type_t additional_types)
874 const arch_register_req_t *req;
876 if (additional_types == 0) {
877 req = reg->single_req;
879 ir_graph *irg = get_irn_irg(node);
880 struct obstack *obst = be_get_be_obst(irg);
881 req = be_create_reg_req(obst, reg, additional_types);
883 be_set_constr_in(node, pos, req);
886 void be_set_constr_single_reg_out(ir_node *node, int pos,
887 const arch_register_t *reg, arch_register_req_type_t additional_types)
889 ir_graph *irg = get_irn_irg(node);
890 be_irg_t *birg = be_birg_from_irg(irg);
891 const arch_register_req_t *req;
893 /* if we have an ignore register, add ignore flag and just assign it */
894 if (!rbitset_is_set(birg->allocatable_regs, reg->global_index)) {
895 additional_types |= arch_register_req_type_ignore;
898 if (additional_types == 0) {
899 req = reg->single_req;
901 ir_graph *irg = get_irn_irg(node);
902 struct obstack *obst = be_get_be_obst(irg);
903 req = be_create_reg_req(obst, reg, additional_types);
906 arch_irn_set_register(node, pos, reg);
907 be_set_constr_out(node, pos, req);
910 void be_node_set_reg_class_in(ir_node *irn, int pos,
911 const arch_register_class_t *cls)
913 be_set_constr_in(irn, pos, cls->class_req);
916 void be_node_set_reg_class_out(ir_node *irn, int pos,
917 const arch_register_class_t *cls)
919 be_set_constr_out(irn, pos, cls->class_req);
922 ir_node *be_get_IncSP_pred(ir_node *irn)
924 assert(be_is_IncSP(irn));
925 return get_irn_n(irn, 0);
928 void be_set_IncSP_pred(ir_node *incsp, ir_node *pred)
930 assert(be_is_IncSP(incsp));
931 set_irn_n(incsp, 0, pred);
934 void be_set_IncSP_offset(ir_node *irn, int offset)
936 be_incsp_attr_t *a = (be_incsp_attr_t*)get_irn_generic_attr(irn);
937 assert(be_is_IncSP(irn));
941 int be_get_IncSP_offset(const ir_node *irn)
943 const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
944 assert(be_is_IncSP(irn));
948 int be_get_IncSP_align(const ir_node *irn)
950 const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
951 assert(be_is_IncSP(irn));
955 ir_node *be_spill(ir_node *block, ir_node *irn)
957 ir_graph *irg = get_Block_irg(block);
958 ir_node *frame = get_irg_frame(irg);
959 const arch_register_class_t *cls = arch_get_irn_reg_class_out(irn);
960 const arch_register_class_t *cls_frame = arch_get_irn_reg_class_out(frame);
963 spill = be_new_Spill(cls, cls_frame, block, frame, irn);
967 ir_node *be_reload(const arch_register_class_t *cls, ir_node *insert, ir_mode *mode, ir_node *spill)
970 ir_node *bl = is_Block(insert) ? insert : get_nodes_block(insert);
971 ir_graph *irg = get_Block_irg(bl);
972 ir_node *frame = get_irg_frame(irg);
973 const arch_register_class_t *cls_frame = arch_get_irn_reg_class_out(frame);
975 assert(be_is_Spill(spill) || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
977 reload = be_new_Reload(cls, cls_frame, bl, frame, spill, mode);
979 if (is_Block(insert)) {
981 insert = sched_prev(insert);
982 } while (is_cfop(insert));
983 sched_add_after(insert, reload);
985 sched_add_before(insert, reload);
992 static arch_irn_class_t be_node_classify(const ir_node *irn)
994 switch (get_irn_opcode(irn)) {
995 case beo_Spill: return arch_irn_class_spill;
996 case beo_Reload: return arch_irn_class_reload;
997 case beo_Perm: return arch_irn_class_perm;
998 case beo_Copy: return arch_irn_class_copy;
999 default: return arch_irn_class_none;
1003 static ir_entity *be_node_get_frame_entity(const ir_node *irn)
1005 return be_get_frame_entity(irn);
1008 void be_node_set_frame_entity(ir_node *irn, ir_entity *ent)
1012 assert(be_has_frame_entity(irn));
1014 a = (be_frame_attr_t*)get_irn_generic_attr(irn);
1018 static void be_node_set_frame_offset(ir_node *irn, int offset)
1022 if (!be_has_frame_entity(irn))
1025 a = (be_frame_attr_t*)get_irn_generic_attr(irn);
1029 static int be_node_get_sp_bias(const ir_node *irn)
1031 if (be_is_IncSP(irn))
1032 return be_get_IncSP_offset(irn);
1033 if (be_is_Call(irn))
1034 return -(int)be_Call_get_pop(irn);
1042 static const arch_irn_ops_t be_node_irn_ops = {
1044 be_node_get_frame_entity,
1045 be_node_set_frame_offset,
1046 be_node_get_sp_bias,
1047 NULL, /* get_inverse */
1048 NULL, /* get_op_estimated_cost */
1049 NULL, /* possible_memory_operand */
1050 NULL, /* perform_memory_operand */
1053 static int get_start_reg_index(ir_graph *irg, const arch_register_t *reg)
1055 ir_node *start = get_irg_start(irg);
1056 unsigned n_outs = arch_irn_get_n_outs(start);
1059 /* do a naive linear search... */
1060 for (i = 0; i < (int)n_outs; ++i) {
1061 const arch_register_req_t *out_req
1062 = arch_get_out_register_req(start, i);
1063 if (! (out_req->type & arch_register_req_type_limited))
1065 if (out_req->cls != arch_register_get_class(reg))
1067 if (!rbitset_is_set(out_req->limited, reg->index))
1071 panic("Tried querying undefined register '%s' at Start", reg->name);
1074 ir_node *be_get_initial_reg_value(ir_graph *irg, const arch_register_t *reg)
1076 int i = get_start_reg_index(irg, reg);
1077 ir_node *start = get_irg_start(irg);
1078 ir_mode *mode = arch_register_class_mode(arch_register_get_class(reg));
1079 const ir_edge_t *edge;
1081 foreach_out_edge(start, edge) {
1082 ir_node *proj = get_edge_src_irn(edge);
1083 if (!is_Proj(proj)) // maybe End/Anchor
1085 if (get_Proj_proj(proj) == i) {
1089 return new_r_Proj(start, mode, i);
1092 int be_find_return_reg_input(ir_node *ret, const arch_register_t *reg)
1094 int arity = get_irn_arity(ret);
1096 /* do a naive linear search... */
1097 for (i = 0; i < arity; ++i) {
1098 const arch_register_req_t *req = arch_get_in_register_req(ret, i);
1099 if (! (req->type & arch_register_req_type_limited))
1101 if (req->cls != arch_register_get_class(reg))
1103 if (!rbitset_is_set(req->limited, reg->index))
1107 panic("Tried querying undefined register '%s' at Return", reg->name);
1110 static arch_irn_class_t dummy_classify(const ir_node *node)
1113 return arch_irn_class_none;
1116 static ir_entity* dummy_get_frame_entity(const ir_node *node)
1122 static void dummy_set_frame_offset(ir_node *node, int bias)
1126 panic("dummy_set_frame_offset() should not be called");
1129 static int dummy_get_sp_bias(const ir_node *node)
1135 /* for "middleend" nodes */
1136 static const arch_irn_ops_t dummy_be_irn_ops = {
1138 dummy_get_frame_entity,
1139 dummy_set_frame_offset,
1141 NULL, /* get_inverse */
1142 NULL, /* get_op_estimated_cost */
1143 NULL, /* possible_memory_operand */
1144 NULL, /* perform_memory_operand */
1149 ir_node *be_new_Phi(ir_node *block, int n_ins, ir_node **ins, ir_mode *mode,
1150 const arch_register_class_t *cls)
1152 const arch_register_req_t *req;
1153 ir_graph *irg = get_irn_irg(block);
1154 struct obstack *obst = be_get_be_obst(irg);
1155 backend_info_t *info;
1158 ir_node *phi = new_r_Phi(block, n_ins, ins, mode);
1159 info = be_get_info(phi);
1160 info->out_infos = NEW_ARR_D(reg_out_info_t, obst, 1);
1161 memset(info->out_infos, 0, 1 * sizeof(info->out_infos[0]));
1162 info->in_reqs = OALLOCN(obst, const arch_register_req_t*, n_ins);
1165 req = arch_no_register_req;
1167 req = cls->class_req;
1169 info->out_infos[0].req = req;
1170 for (i = 0; i < n_ins; ++i) {
1171 info->in_reqs[i] = req;
1177 void be_set_phi_reg_req(ir_node *node, const arch_register_req_t *req)
1179 int arity = get_irn_arity(node);
1182 backend_info_t *info = be_get_info(node);
1183 info->out_infos[0].req = req;
1184 for (i = 0; i < arity; ++i) {
1185 info->in_reqs[i] = req;
1188 assert(mode_is_datab(get_irn_mode(node)));
1191 void be_dump_phi_reg_reqs(FILE *F, ir_node *node, dump_reason_t reason)
1194 case dump_node_opcode_txt:
1195 fputs(get_op_name(get_irn_op(node)), F);
1197 case dump_node_mode_txt:
1198 fprintf(F, "%s", get_mode_name(get_irn_mode(node)));
1200 case dump_node_nodeattr_txt:
1202 case dump_node_info_txt:
1204 backend_info_t *info = be_get_info(node);
1205 if (info != NULL && info->out_infos[0].req != NULL) {
1206 arch_dump_reqs_and_registers(F, node);
1216 static const arch_irn_ops_t phi_irn_ops = {
1218 dummy_get_frame_entity,
1219 dummy_set_frame_offset,
1221 NULL, /* get_inverse */
1222 NULL, /* get_op_estimated_cost */
1223 NULL, /* possible_memory_operand */
1224 NULL, /* perform_memory_operand */
1230 * ir_op-Operation: dump a be node to file
1232 static void dump_node(FILE *f, ir_node *irn, dump_reason_t reason)
1234 assert(is_be_node(irn));
1237 case dump_node_opcode_txt:
1238 fputs(get_op_name(get_irn_op(irn)), f);
1240 case dump_node_mode_txt:
1241 if (be_is_Copy(irn) || be_is_CopyKeep(irn)) {
1242 fprintf(f, "%s", get_mode_name(get_irn_mode(irn)));
1245 case dump_node_nodeattr_txt:
1246 if (be_is_Call(irn)) {
1247 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(irn);
1249 fprintf(f, " [%s] ", get_entity_name(a->ent));
1251 if (be_is_IncSP(irn)) {
1252 const be_incsp_attr_t *attr = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
1253 fprintf(f, " [%d] ", attr->offset);
1256 case dump_node_info_txt:
1257 arch_dump_reqs_and_registers(f, irn);
1259 if (be_has_frame_entity(irn)) {
1260 const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
1262 unsigned size = get_type_size_bytes(get_entity_type(a->ent));
1263 ir_fprintf(f, "frame entity: %+F, offset 0x%x (%d), size 0x%x (%d) bytes\n",
1264 a->ent, a->offset, a->offset, size, size);
1269 switch (get_irn_opcode(irn)) {
1271 const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
1272 fprintf(f, "align: %d\n", a->align);
1273 fprintf(f, "offset: %d\n", a->offset);
1277 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(irn);
1280 fprintf(f, "\ncalling: %s\n", get_entity_name(a->ent));
1285 for (i = 0; i < be_get_MemPerm_entity_arity(irn); ++i) {
1286 ir_entity *in, *out;
1287 in = be_get_MemPerm_in_entity(irn, i);
1288 out = be_get_MemPerm_out_entity(irn, i);
1290 fprintf(f, "\nin[%d]: %s\n", i, get_entity_name(in));
1293 fprintf(f, "\nout[%d]: %s\n", i, get_entity_name(out));
1307 * Copies the backend specific attributes from old node to new node.
1309 static void copy_attr(ir_graph *irg, const ir_node *old_node, ir_node *new_node)
1311 const void *old_attr = get_irn_generic_attr_const(old_node);
1312 void *new_attr = get_irn_generic_attr(new_node);
1313 struct obstack *obst = be_get_be_obst(irg);
1314 backend_info_t *old_info = be_get_info(old_node);
1315 backend_info_t *new_info = be_get_info(new_node);
1317 assert(is_be_node(old_node));
1318 assert(is_be_node(new_node));
1320 memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
1322 new_info->flags = old_info->flags;
1323 if (old_info->out_infos != NULL) {
1324 size_t n_outs = ARR_LEN(old_info->out_infos);
1325 /* need dyanmic out infos? */
1326 if (be_is_Perm(new_node)) {
1327 new_info->out_infos = NEW_ARR_F(reg_out_info_t, n_outs);
1329 new_info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_outs);
1331 memcpy(new_info->out_infos, old_info->out_infos,
1332 n_outs * sizeof(new_info->out_infos[0]));
1334 new_info->out_infos = NULL;
1338 if (old_info->in_reqs != NULL) {
1339 unsigned n_ins = get_irn_arity(old_node);
1340 /* need dynamic in infos? */
1341 if (get_irn_op(old_node)->opar == oparity_dynamic) {
1342 new_info->in_reqs = NEW_ARR_F(const arch_register_req_t*, n_ins);
1344 new_info->in_reqs = OALLOCN(obst,const arch_register_req_t*, n_ins);
1346 memcpy(new_info->in_reqs, old_info->in_reqs,
1347 n_ins * sizeof(new_info->in_reqs[0]));
1349 new_info->in_reqs = NULL;
1353 static const ir_op_ops be_node_op_ops = {
1373 int is_be_node(const ir_node *irn)
1375 return get_op_ops(get_irn_op(irn))->be_ops == &be_node_irn_ops;
1378 void be_init_op(void)
1382 /* Acquire all needed opcodes. */
1383 op_be_Spill = new_ir_op(beo_Spill, "be_Spill", op_pin_state_exc_pinned, irop_flag_none, oparity_unary, 0, sizeof(be_frame_attr_t), &be_node_op_ops);
1384 op_be_Reload = new_ir_op(beo_Reload, "be_Reload", op_pin_state_exc_pinned, irop_flag_none, oparity_zero, 0, sizeof(be_frame_attr_t), &be_node_op_ops);
1385 op_be_Perm = new_ir_op(beo_Perm, "be_Perm", op_pin_state_exc_pinned, irop_flag_none, oparity_variable, 0, sizeof(be_node_attr_t), &be_node_op_ops);
1386 op_be_MemPerm = new_ir_op(beo_MemPerm, "be_MemPerm", op_pin_state_exc_pinned, irop_flag_none, oparity_variable, 0, sizeof(be_memperm_attr_t), &be_node_op_ops);
1387 op_be_Copy = new_ir_op(beo_Copy, "be_Copy", op_pin_state_exc_pinned, irop_flag_none, oparity_unary, 0, sizeof(be_node_attr_t), &be_node_op_ops);
1388 op_be_Keep = new_ir_op(beo_Keep, "be_Keep", op_pin_state_exc_pinned, irop_flag_keep, oparity_dynamic, 0, sizeof(be_node_attr_t), &be_node_op_ops);
1389 op_be_CopyKeep = new_ir_op(beo_CopyKeep, "be_CopyKeep", op_pin_state_exc_pinned, irop_flag_keep, oparity_variable, 0, sizeof(be_node_attr_t), &be_node_op_ops);
1390 op_be_Call = new_ir_op(beo_Call, "be_Call", op_pin_state_exc_pinned, irop_flag_fragile|irop_flag_uses_memory, oparity_variable, 0, sizeof(be_call_attr_t), &be_node_op_ops);
1391 op_be_Return = new_ir_op(beo_Return, "be_Return", op_pin_state_exc_pinned, irop_flag_cfopcode, oparity_dynamic, 0, sizeof(be_return_attr_t), &be_node_op_ops);
1392 op_be_AddSP = new_ir_op(beo_AddSP, "be_AddSP", op_pin_state_exc_pinned, irop_flag_none, oparity_unary, 0, sizeof(be_node_attr_t), &be_node_op_ops);
1393 op_be_SubSP = new_ir_op(beo_SubSP, "be_SubSP", op_pin_state_exc_pinned, irop_flag_none, oparity_unary, 0, sizeof(be_node_attr_t), &be_node_op_ops);
1394 op_be_IncSP = new_ir_op(beo_IncSP, "be_IncSP", op_pin_state_exc_pinned, irop_flag_none, oparity_unary, 0, sizeof(be_incsp_attr_t), &be_node_op_ops);
1395 op_be_Start = new_ir_op(beo_Start, "be_Start", op_pin_state_exc_pinned, irop_flag_none, oparity_zero, 0, sizeof(be_node_attr_t), &be_node_op_ops);
1396 op_be_FrameAddr = new_ir_op(beo_FrameAddr, "be_FrameAddr", op_pin_state_exc_pinned, irop_flag_none, oparity_unary, 0, sizeof(be_frame_attr_t), &be_node_op_ops);
1398 op_be_Spill->ops.node_cmp_attr = FrameAddr_cmp_attr;
1399 op_be_Reload->ops.node_cmp_attr = FrameAddr_cmp_attr;
1400 op_be_Perm->ops.node_cmp_attr = be_nodes_equal;
1401 op_be_MemPerm->ops.node_cmp_attr = be_nodes_equal;
1402 op_be_Copy->ops.node_cmp_attr = be_nodes_equal;
1403 op_be_Keep->ops.node_cmp_attr = be_nodes_equal;
1404 op_be_CopyKeep->ops.node_cmp_attr = be_nodes_equal;
1405 op_be_Call->ops.node_cmp_attr = Call_cmp_attr;
1406 op_be_Return->ops.node_cmp_attr = Return_cmp_attr;
1407 op_be_AddSP->ops.node_cmp_attr = be_nodes_equal;
1408 op_be_SubSP->ops.node_cmp_attr = be_nodes_equal;
1409 op_be_IncSP->ops.node_cmp_attr = IncSP_cmp_attr;
1410 op_be_Start->ops.node_cmp_attr = be_nodes_equal;
1411 op_be_FrameAddr->ops.node_cmp_attr = FrameAddr_cmp_attr;
1413 /* attach out dummy_ops to middle end nodes */
1414 for (opc = iro_First; opc <= iro_Last; ++opc) {
1415 ir_op *op = get_irp_opcode(opc);
1416 assert(op->ops.be_ops == NULL);
1417 op->ops.be_ops = &dummy_be_irn_ops;
1420 op_Phi->ops.be_ops = &phi_irn_ops;