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.
39 #include "bitfiddle.h"
40 #include "raw_bitset.h"
60 typedef struct be_node_attr_t {
64 /** The be_Return nodes attribute type. */
67 int num_ret_vals; /**< number of return values */
68 unsigned pop; /**< number of bytes that should be popped */
69 int emit_pop; /**< if set, emit pop bytes, even if pop = 0 */
72 /** The be_IncSP attribute type. */
75 int offset; /**< The offset by which the stack shall be
77 int align; /**< whether stack should be aligned after the
81 /** The be_Frame attribute type. */
88 /** The be_Call attribute type. */
91 ir_entity *ent; /**< called entity if this is a static call. */
93 ir_type *call_tp; /**< call type, copied from the original Call */
98 ir_entity **in_entities;
99 ir_entity **out_entities;
105 ir_op *op_be_MemPerm;
108 ir_op *op_be_CopyKeep;
115 ir_op *op_be_FrameAddr;
118 * Compare the attributes of two be_FrameAddr nodes.
120 * @return zero if both nodes have identically attributes
122 static int FrameAddr_cmp_attr(const ir_node *a, const ir_node *b)
124 const be_frame_attr_t *a_attr = (const be_frame_attr_t*)get_irn_generic_attr_const(a);
125 const be_frame_attr_t *b_attr = (const be_frame_attr_t*)get_irn_generic_attr_const(b);
127 if (a_attr->ent != b_attr->ent || a_attr->offset != b_attr->offset)
130 return be_nodes_equal(a, b);
134 * Compare the attributes of two be_Return nodes.
136 * @return zero if both nodes have identically attributes
138 static int Return_cmp_attr(const ir_node *a, const ir_node *b)
140 const be_return_attr_t *a_attr = (const be_return_attr_t*)get_irn_generic_attr_const(a);
141 const be_return_attr_t *b_attr = (const be_return_attr_t*)get_irn_generic_attr_const(b);
143 if (a_attr->num_ret_vals != b_attr->num_ret_vals)
145 if (a_attr->pop != b_attr->pop)
147 if (a_attr->emit_pop != b_attr->emit_pop)
150 return be_nodes_equal(a, b);
154 * Compare the attributes of two be_IncSP nodes.
156 * @return zero if both nodes have identically attributes
158 static int IncSP_cmp_attr(const ir_node *a, const ir_node *b)
160 const be_incsp_attr_t *a_attr = (const be_incsp_attr_t*)get_irn_generic_attr_const(a);
161 const be_incsp_attr_t *b_attr = (const be_incsp_attr_t*)get_irn_generic_attr_const(b);
163 if (a_attr->offset != b_attr->offset)
166 return be_nodes_equal(a, b);
170 * Compare the attributes of two be_Call nodes.
172 * @return zero if both nodes have identically attributes
174 static int Call_cmp_attr(const ir_node *a, const ir_node *b)
176 const be_call_attr_t *a_attr = (const be_call_attr_t*)get_irn_generic_attr_const(a);
177 const be_call_attr_t *b_attr = (const be_call_attr_t*)get_irn_generic_attr_const(b);
179 if (a_attr->ent != b_attr->ent ||
180 a_attr->call_tp != b_attr->call_tp)
183 return be_nodes_equal(a, b);
186 static arch_register_req_t *allocate_reg_req(const ir_node *node)
188 ir_graph *irg = get_irn_irg(node);
189 struct obstack *obst = be_get_be_obst(irg);
191 arch_register_req_t *req = OALLOCZ(obst, arch_register_req_t);
195 void be_set_constr_in(ir_node *node, int pos, const arch_register_req_t *req)
197 backend_info_t *info = be_get_info(node);
198 assert(pos < get_irn_arity(node));
199 info->in_reqs[pos] = req;
202 void be_set_constr_out(ir_node *node, int pos, const arch_register_req_t *req)
204 backend_info_t *info = be_get_info(node);
205 info->out_infos[pos].req = req;
209 * Initializes the generic attribute of all be nodes and return it.
211 static void init_node_attr(ir_node *node, int n_inputs, int n_outputs)
213 ir_graph *irg = get_irn_irg(node);
214 struct obstack *obst = be_get_be_obst(irg);
215 backend_info_t *info = be_get_info(node);
216 const arch_register_req_t **in_reqs;
220 assert(n_inputs == get_irn_arity(node));
221 in_reqs = OALLOCN(obst, const arch_register_req_t*, n_inputs);
222 for (i = 0; i < n_inputs; ++i) {
223 in_reqs[i] = arch_no_register_req;
226 in_reqs = NEW_ARR_F(const arch_register_req_t*, 0);
228 info->in_reqs = in_reqs;
230 if (n_outputs >= 0) {
232 info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_outputs);
233 memset(info->out_infos, 0, n_outputs * sizeof(info->out_infos[0]));
234 for (i = 0; i < n_outputs; ++i) {
235 info->out_infos[i].req = arch_no_register_req;
238 info->out_infos = NEW_ARR_F(reg_out_info_t, 0);
242 static void add_register_req_out(ir_node *node)
244 backend_info_t *info = be_get_info(node);
245 reg_out_info_t out_info;
246 memset(&out_info, 0, sizeof(out_info));
247 out_info.req = arch_no_register_req;
248 ARR_APP1(reg_out_info_t, info->out_infos, out_info);
251 static void add_register_req_in(ir_node *node)
253 backend_info_t *info = be_get_info(node);
254 ARR_APP1(const arch_register_req_t*, info->in_reqs, arch_no_register_req);
257 ir_node *be_new_Spill(const arch_register_class_t *cls,
258 const arch_register_class_t *cls_frame, ir_node *bl,
259 ir_node *frame, ir_node *to_spill)
264 ir_graph *irg = get_Block_irg(bl);
268 res = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
269 init_node_attr(res, 2, 1);
270 a = (be_frame_attr_t*) get_irn_generic_attr(res);
273 a->base.exc.pin_state = op_pin_state_pinned;
275 be_node_set_reg_class_in(res, n_be_Spill_frame, cls_frame);
276 be_node_set_reg_class_in(res, n_be_Spill_val, cls);
278 * For spills and reloads, we return "none" as requirement for frame
279 * pointer, so every input is ok. Some backends need this (STA).
280 * Matze: we should investigate if this is really needed, this solution
281 * looks very hacky to me
283 be_set_constr_in(res, n_be_Spill_frame, arch_no_register_req);
285 arch_set_irn_register_req_out(res, 0, arch_no_register_req);
290 ir_node *be_new_Reload(const arch_register_class_t *cls,
291 const arch_register_class_t *cls_frame, ir_node *block,
292 ir_node *frame, ir_node *mem, ir_mode *mode)
296 ir_graph *irg = get_Block_irg(block);
301 res = new_ir_node(NULL, irg, block, op_be_Reload, mode, 2, in);
303 init_node_attr(res, 2, 1);
304 be_node_set_reg_class_out(res, 0, cls);
306 be_node_set_reg_class_in(res, n_be_Reload_frame, cls_frame);
307 arch_set_irn_flags(res, arch_irn_flags_rematerializable);
309 a = (be_frame_attr_t*) get_irn_generic_attr(res);
312 a->base.exc.pin_state = op_pin_state_pinned;
315 * For spills and reloads, we return "none" as requirement for frame
316 * pointer, so every input is ok. Some backends need this (e.g. STA).
317 * Matze: we should investigate if this is really needed, this solution
318 * looks very hacky to me
320 be_set_constr_in(res, n_be_Reload_frame, arch_no_register_req);
325 ir_node *be_get_Reload_mem(const ir_node *irn)
327 assert(be_is_Reload(irn));
328 return get_irn_n(irn, n_be_Reload_mem);
331 ir_node *be_get_Reload_frame(const ir_node *irn)
333 assert(be_is_Reload(irn));
334 return get_irn_n(irn, n_be_Reload_frame);
337 ir_node *be_get_Spill_val(const ir_node *irn)
339 assert(be_is_Spill(irn));
340 return get_irn_n(irn, n_be_Spill_val);
343 ir_node *be_get_Spill_frame(const ir_node *irn)
345 assert(be_is_Spill(irn));
346 return get_irn_n(irn, n_be_Spill_frame);
349 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_node *block,
350 int n, ir_node *in[])
353 ir_graph *irg = get_Block_irg(block);
354 be_node_attr_t *attr;
356 ir_node *irn = new_ir_node(NULL, irg, block, op_be_Perm, mode_T, n, in);
357 init_node_attr(irn, n, n);
358 attr = (be_node_attr_t*) get_irn_generic_attr(irn);
359 attr->exc.pin_state = op_pin_state_pinned;
360 for (i = 0; i < n; ++i) {
361 be_node_set_reg_class_in(irn, i, cls);
362 be_node_set_reg_class_out(irn, i, cls);
368 void be_Perm_reduce(ir_node *perm, int new_size, int *map)
370 int arity = get_irn_arity(perm);
371 const arch_register_req_t **old_in_reqs
372 = ALLOCAN(const arch_register_req_t*, arity);
373 reg_out_info_t *old_infos = ALLOCAN(reg_out_info_t, arity);
374 backend_info_t *info = be_get_info(perm);
378 assert(be_is_Perm(perm));
379 assert(new_size <= arity);
381 new_in = ALLOCAN(ir_node*, new_size);
383 /* save the old register data */
384 memcpy(old_in_reqs, info->in_reqs, arity * sizeof(old_in_reqs[0]));
385 memcpy(old_infos, info->out_infos, arity * sizeof(old_infos[0]));
387 /* compose the new in array and set the new register data directly */
388 for (i = 0; i < new_size; ++i) {
390 new_in[i] = get_irn_n(perm, idx);
391 info->in_reqs[i] = old_in_reqs[idx];
392 info->out_infos[i] = old_infos[idx];
395 set_irn_in(perm, new_size, new_in);
398 ir_node *be_new_MemPerm(ir_node *block, int n, ir_node *in[])
400 ir_graph *irg = get_Block_irg(block);
401 const arch_env_t *arch_env = be_get_irg_arch_env(irg);
402 ir_node *frame = get_irg_frame(irg);
403 const arch_register_t *sp = arch_env->sp;
405 be_memperm_attr_t *attr;
408 real_in = ALLOCAN(ir_node*, n + 1);
410 memcpy(&real_in[1], in, n * sizeof(real_in[0]));
412 irn = new_ir_node(NULL, irg, block, op_be_MemPerm, mode_T, n+1, real_in);
414 init_node_attr(irn, n + 1, n);
415 be_node_set_reg_class_in(irn, 0, sp->reg_class);
417 attr = (be_memperm_attr_t*)get_irn_generic_attr(irn);
418 attr->in_entities = OALLOCNZ(irg->obst, ir_entity*, n);
419 attr->out_entities = OALLOCNZ(irg->obst, ir_entity*, n);
424 ir_node *be_new_Copy(ir_node *bl, ir_node *op)
428 arch_register_req_t *req;
429 be_node_attr_t *attr;
430 ir_graph *irg = get_Block_irg(bl);
431 const arch_register_req_t *in_req = arch_get_irn_register_req(op);
432 const arch_register_class_t *cls = in_req->cls;
435 res = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
436 init_node_attr(res, 1, 1);
437 attr = (be_node_attr_t*) get_irn_generic_attr(res);
438 attr->exc.pin_state = op_pin_state_floats;
439 be_node_set_reg_class_in(res, 0, cls);
440 be_node_set_reg_class_out(res, 0, cls);
442 req = allocate_reg_req(res);
444 req->type = arch_register_req_type_should_be_same
445 | (in_req->type & arch_register_req_type_aligned);
446 req->other_same = 1U << 0;
447 req->width = in_req->width;
448 be_set_constr_out(res, 0, req);
453 ir_node *be_get_Copy_op(const ir_node *cpy)
455 return get_irn_n(cpy, n_be_Copy_op);
458 void be_set_Copy_op(ir_node *cpy, ir_node *op)
460 set_irn_n(cpy, n_be_Copy_op, op);
463 ir_node *be_new_Keep(ir_node *block, int n, ir_node *in[])
467 ir_graph *irg = get_Block_irg(block);
468 be_node_attr_t *attr;
470 res = new_ir_node(NULL, irg, block, op_be_Keep, mode_ANY, -1, NULL);
471 init_node_attr(res, -1, 1);
472 attr = (be_node_attr_t*) get_irn_generic_attr(res);
473 attr->exc.pin_state = op_pin_state_pinned;
475 for (i = 0; i < n; ++i) {
476 add_irn_n(res, in[i]);
477 add_register_req_in(res);
484 void be_Keep_add_node(ir_node *keep, const arch_register_class_t *cls, ir_node *node)
488 assert(be_is_Keep(keep));
489 n = add_irn_n(keep, node);
490 add_register_req_in(keep);
491 be_node_set_reg_class_in(keep, n, cls);
494 ir_node *be_new_Call(dbg_info *dbg, ir_graph *irg, ir_node *bl, ir_node *mem,
495 ir_node *sp, ir_node *ptr, int n_outs, int n, ir_node *in[],
499 int real_n = n_be_Call_first_arg + n;
503 NEW_ARR_A(ir_node *, real_in, real_n);
504 real_in[n_be_Call_mem] = mem;
505 real_in[n_be_Call_sp] = sp;
506 real_in[n_be_Call_ptr] = ptr;
507 memcpy(&real_in[n_be_Call_first_arg], in, n * sizeof(in[0]));
509 irn = new_ir_node(dbg, irg, bl, op_be_Call, mode_T, real_n, real_in);
510 init_node_attr(irn, real_n, n_outs);
511 a = (be_call_attr_t*)get_irn_generic_attr(irn);
513 a->call_tp = call_tp;
515 a->base.exc.pin_state = op_pin_state_pinned;
519 ir_entity *be_Call_get_entity(const ir_node *call)
521 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
522 assert(be_is_Call(call));
526 void be_Call_set_entity(ir_node *call, ir_entity *ent)
528 be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
529 assert(be_is_Call(call));
533 ir_type *be_Call_get_type(ir_node *call)
535 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
536 assert(be_is_Call(call));
540 void be_Call_set_type(ir_node *call, ir_type *call_tp)
542 be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
543 assert(be_is_Call(call));
544 a->call_tp = call_tp;
547 void be_Call_set_pop(ir_node *call, unsigned pop)
549 be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
553 unsigned be_Call_get_pop(const ir_node *call)
555 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
559 ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *block, int n_res,
560 unsigned pop, int n, ir_node *in[])
566 res = new_ir_node(dbg, irg, block, op_be_Return, mode_X, -1, NULL);
567 init_node_attr(res, -1, 1);
568 for (i = 0; i < n; ++i) {
569 add_irn_n(res, in[i]);
570 add_register_req_in(res);
572 be_set_constr_out(res, 0, arch_no_register_req);
574 a = (be_return_attr_t*)get_irn_generic_attr(res);
575 a->num_ret_vals = n_res;
578 a->base.exc.pin_state = op_pin_state_pinned;
583 int be_Return_get_n_rets(const ir_node *ret)
585 const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
586 return a->num_ret_vals;
589 unsigned be_Return_get_pop(const ir_node *ret)
591 const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
595 int be_Return_get_emit_pop(const ir_node *ret)
597 const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
601 void be_Return_set_emit_pop(ir_node *ret, int emit_pop)
603 be_return_attr_t *a = (be_return_attr_t*)get_irn_generic_attr(ret);
604 a->emit_pop = emit_pop;
607 int be_Return_append_node(ir_node *ret, ir_node *node)
611 pos = add_irn_n(ret, node);
612 add_register_req_in(ret);
617 ir_node *be_new_IncSP(const arch_register_t *sp, ir_node *bl,
618 ir_node *old_sp, int offset, int align)
623 ir_graph *irg = get_Block_irg(bl);
626 irn = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode,
627 sizeof(in) / sizeof(in[0]), in);
628 init_node_attr(irn, 1, 1);
629 a = (be_incsp_attr_t*)get_irn_generic_attr(irn);
632 a->base.exc.pin_state = op_pin_state_pinned;
634 /* Set output constraint to stack register. */
635 be_node_set_reg_class_in(irn, 0, sp->reg_class);
636 be_set_constr_single_reg_out(irn, 0, sp, arch_register_req_type_produces_sp);
641 ir_node *be_new_AddSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp,
645 ir_node *in[n_be_AddSP_last];
647 be_node_attr_t *attr;
649 in[n_be_AddSP_old_sp] = old_sp;
650 in[n_be_AddSP_size] = sz;
652 irg = get_Block_irg(bl);
653 irn = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T, n_be_AddSP_last, in);
654 init_node_attr(irn, n_be_AddSP_last, pn_be_AddSP_last);
655 attr = (be_node_attr_t*) get_irn_generic_attr(irn);
656 attr->exc.pin_state = op_pin_state_pinned;
658 /* Set output constraint to stack register. */
659 be_set_constr_single_reg_in(irn, n_be_AddSP_old_sp, sp,
660 arch_register_req_type_none);
661 be_node_set_reg_class_in(irn, n_be_AddSP_size, sp->reg_class);
662 be_set_constr_single_reg_out(irn, pn_be_AddSP_sp, sp,
663 arch_register_req_type_produces_sp);
668 ir_node *be_new_SubSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp, ir_node *sz)
671 ir_node *in[n_be_SubSP_last];
673 be_node_attr_t *attr;
675 in[n_be_SubSP_old_sp] = old_sp;
676 in[n_be_SubSP_size] = sz;
678 irg = get_Block_irg(bl);
679 irn = new_ir_node(NULL, irg, bl, op_be_SubSP, mode_T, n_be_SubSP_last, in);
680 init_node_attr(irn, n_be_SubSP_last, pn_be_SubSP_last);
681 attr = (be_node_attr_t*) get_irn_generic_attr(irn);
682 attr->exc.pin_state = op_pin_state_pinned;
684 /* Set output constraint to stack register. */
685 be_set_constr_single_reg_in(irn, n_be_SubSP_old_sp, sp,
686 arch_register_req_type_none);
687 be_node_set_reg_class_in(irn, n_be_SubSP_size, sp->reg_class);
688 be_set_constr_single_reg_out(irn, pn_be_SubSP_sp, sp, arch_register_req_type_produces_sp);
693 ir_node *be_new_Start(dbg_info *dbgi, ir_node *bl, int n_outs)
697 ir_graph *irg = get_Block_irg(bl);
698 be_node_attr_t *attr;
700 res = new_ir_node(dbgi, irg, bl, op_be_Start, mode_T, 0, NULL);
701 init_node_attr(res, 0, -1);
702 attr = (be_node_attr_t*) get_irn_generic_attr(res);
703 attr->exc.pin_state = op_pin_state_pinned;
704 for (i = 0; i < n_outs; ++i) {
705 add_register_req_out(res);
711 ir_node *be_new_FrameAddr(const arch_register_class_t *cls_frame, ir_node *bl, ir_node *frame, ir_entity *ent)
716 ir_graph *irg = get_Block_irg(bl);
719 irn = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
720 init_node_attr(irn, 1, 1);
721 a = (be_frame_attr_t*)get_irn_generic_attr(irn);
724 a->base.exc.pin_state = op_pin_state_floats;
725 be_node_set_reg_class_in(irn, 0, cls_frame);
726 be_node_set_reg_class_out(irn, 0, cls_frame);
728 return optimize_node(irn);
731 ir_node *be_get_FrameAddr_frame(const ir_node *node)
733 assert(be_is_FrameAddr(node));
734 return get_irn_n(node, n_be_FrameAddr_ptr);
737 ir_entity *be_get_FrameAddr_entity(const ir_node *node)
739 const be_frame_attr_t *attr = (const be_frame_attr_t*)get_irn_generic_attr_const(node);
743 ir_node *be_new_CopyKeep(ir_node *bl, ir_node *src, int n, ir_node *in_keep[])
746 ir_node **in = ALLOCAN(ir_node*, n + 1);
747 ir_graph *irg = get_Block_irg(bl);
748 const arch_register_req_t *req = arch_get_irn_register_req(src);
749 const arch_register_class_t *cls = req->cls;
750 ir_mode *mode = get_irn_mode(src);
751 be_node_attr_t *attr;
754 memcpy(&in[1], in_keep, n * sizeof(in[0]));
755 irn = new_ir_node(NULL, irg, bl, op_be_CopyKeep, mode, n + 1, in);
756 init_node_attr(irn, n + 1, 1);
757 attr = (be_node_attr_t*) get_irn_generic_attr(irn);
758 attr->exc.pin_state = op_pin_state_floats;
759 be_node_set_reg_class_in(irn, 0, cls);
760 be_node_set_reg_class_out(irn, 0, cls);
765 ir_node *be_new_CopyKeep_single(ir_node *bl, ir_node *src, ir_node *keep)
767 return be_new_CopyKeep(bl, src, 1, &keep);
770 ir_node *be_get_CopyKeep_op(const ir_node *cpy)
772 return get_irn_n(cpy, n_be_CopyKeep_op);
775 void be_set_CopyKeep_op(ir_node *cpy, ir_node *op)
777 set_irn_n(cpy, n_be_CopyKeep_op, op);
780 static bool be_has_frame_entity(const ir_node *irn)
782 switch (get_irn_opcode(irn)) {
792 ir_entity *be_get_frame_entity(const ir_node *irn)
794 if (be_has_frame_entity(irn)) {
795 const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
801 int be_get_frame_offset(const ir_node *irn)
803 assert(is_be_node(irn));
804 if (be_has_frame_entity(irn)) {
805 const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
811 void be_set_MemPerm_in_entity(const ir_node *irn, int n, ir_entity *ent)
813 const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
815 assert(be_is_MemPerm(irn));
816 assert(n < be_get_MemPerm_entity_arity(irn));
818 attr->in_entities[n] = ent;
821 ir_entity* be_get_MemPerm_in_entity(const ir_node* irn, int n)
823 const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
825 assert(be_is_MemPerm(irn));
826 assert(n < be_get_MemPerm_entity_arity(irn));
828 return attr->in_entities[n];
831 void be_set_MemPerm_out_entity(const ir_node *irn, int n, ir_entity *ent)
833 const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
835 assert(be_is_MemPerm(irn));
836 assert(n < be_get_MemPerm_entity_arity(irn));
838 attr->out_entities[n] = ent;
841 ir_entity* be_get_MemPerm_out_entity(const ir_node* irn, int n)
843 const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
845 assert(be_is_MemPerm(irn));
846 assert(n < be_get_MemPerm_entity_arity(irn));
848 return attr->out_entities[n];
851 int be_get_MemPerm_entity_arity(const ir_node *irn)
853 return get_irn_arity(irn) - 1;
856 const arch_register_req_t *be_create_reg_req(struct obstack *obst,
857 const arch_register_t *reg, arch_register_req_type_t additional_types)
859 arch_register_req_t *req = OALLOC(obst, arch_register_req_t);
860 const arch_register_class_t *cls = arch_register_get_class(reg);
861 unsigned *limited_bitset;
863 limited_bitset = rbitset_obstack_alloc(obst, arch_register_class_n_regs(cls));
864 rbitset_set(limited_bitset, arch_register_get_index(reg));
866 req->type = arch_register_req_type_limited | additional_types;
868 req->limited = limited_bitset;
873 void be_set_constr_single_reg_in(ir_node *node, int pos,
874 const arch_register_t *reg, arch_register_req_type_t additional_types)
876 const arch_register_req_t *req;
878 if (additional_types == 0) {
879 req = reg->single_req;
881 ir_graph *irg = get_irn_irg(node);
882 struct obstack *obst = be_get_be_obst(irg);
883 req = be_create_reg_req(obst, reg, additional_types);
885 be_set_constr_in(node, pos, req);
888 void be_set_constr_single_reg_out(ir_node *node, int pos,
889 const arch_register_t *reg, arch_register_req_type_t additional_types)
891 ir_graph *irg = get_irn_irg(node);
892 be_irg_t *birg = be_birg_from_irg(irg);
893 const arch_register_req_t *req;
895 /* if we have an ignore register, add ignore flag and just assign it */
896 if (!rbitset_is_set(birg->allocatable_regs, reg->global_index)) {
897 additional_types |= arch_register_req_type_ignore;
900 if (additional_types == 0) {
901 req = reg->single_req;
903 struct obstack *obst = be_get_be_obst(irg);
904 req = be_create_reg_req(obst, reg, additional_types);
907 arch_set_irn_register_out(node, pos, reg);
908 be_set_constr_out(node, pos, req);
911 void be_node_set_reg_class_in(ir_node *irn, int pos,
912 const arch_register_class_t *cls)
914 be_set_constr_in(irn, pos, cls->class_req);
917 void be_node_set_reg_class_out(ir_node *irn, int pos,
918 const arch_register_class_t *cls)
920 be_set_constr_out(irn, pos, cls->class_req);
923 ir_node *be_get_IncSP_pred(ir_node *irn)
925 assert(be_is_IncSP(irn));
926 return get_irn_n(irn, 0);
929 void be_set_IncSP_pred(ir_node *incsp, ir_node *pred)
931 assert(be_is_IncSP(incsp));
932 set_irn_n(incsp, 0, pred);
935 void be_set_IncSP_offset(ir_node *irn, int offset)
937 be_incsp_attr_t *a = (be_incsp_attr_t*)get_irn_generic_attr(irn);
938 assert(be_is_IncSP(irn));
942 int be_get_IncSP_offset(const ir_node *irn)
944 const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
945 assert(be_is_IncSP(irn));
949 int be_get_IncSP_align(const ir_node *irn)
951 const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
952 assert(be_is_IncSP(irn));
956 static ir_entity *be_node_get_frame_entity(const ir_node *irn)
958 return be_get_frame_entity(irn);
961 void be_node_set_frame_entity(ir_node *irn, ir_entity *ent)
965 assert(be_has_frame_entity(irn));
967 a = (be_frame_attr_t*)get_irn_generic_attr(irn);
971 static void be_node_set_frame_offset(ir_node *irn, int offset)
975 if (!be_has_frame_entity(irn))
978 a = (be_frame_attr_t*)get_irn_generic_attr(irn);
982 static int be_node_get_sp_bias(const ir_node *irn)
984 if (be_is_IncSP(irn))
985 return be_get_IncSP_offset(irn);
987 return -(int)be_Call_get_pop(irn);
995 static const arch_irn_ops_t be_node_irn_ops = {
996 be_node_get_frame_entity,
997 be_node_set_frame_offset,
999 NULL, /* get_inverse */
1000 NULL, /* get_op_estimated_cost */
1001 NULL, /* possible_memory_operand */
1002 NULL, /* perform_memory_operand */
1005 static int get_start_reg_index(ir_graph *irg, const arch_register_t *reg)
1007 ir_node *start = get_irg_start(irg);
1008 unsigned n_outs = arch_get_irn_n_outs(start);
1011 /* do a naive linear search... */
1012 for (i = 0; i < (int)n_outs; ++i) {
1013 const arch_register_req_t *out_req
1014 = arch_get_irn_register_req_out(start, i);
1015 if (! (out_req->type & arch_register_req_type_limited))
1017 if (out_req->cls != arch_register_get_class(reg))
1019 if (!rbitset_is_set(out_req->limited, reg->index))
1023 panic("Tried querying undefined register '%s' at Start", reg->name);
1026 ir_node *be_get_initial_reg_value(ir_graph *irg, const arch_register_t *reg)
1028 int i = get_start_reg_index(irg, reg);
1029 ir_node *start = get_irg_start(irg);
1030 ir_mode *mode = arch_register_class_mode(arch_register_get_class(reg));
1031 const ir_edge_t *edge;
1033 foreach_out_edge(start, edge) {
1034 ir_node *proj = get_edge_src_irn(edge);
1035 if (!is_Proj(proj)) // maybe End/Anchor
1037 if (get_Proj_proj(proj) == i) {
1041 return new_r_Proj(start, mode, i);
1044 int be_find_return_reg_input(ir_node *ret, const arch_register_t *reg)
1046 int arity = get_irn_arity(ret);
1048 /* do a naive linear search... */
1049 for (i = 0; i < arity; ++i) {
1050 const arch_register_req_t *req = arch_get_irn_register_req_in(ret, i);
1051 if (! (req->type & arch_register_req_type_limited))
1053 if (req->cls != arch_register_get_class(reg))
1055 if (!rbitset_is_set(req->limited, reg->index))
1059 panic("Tried querying undefined register '%s' at Return", reg->name);
1062 static ir_entity* dummy_get_frame_entity(const ir_node *node)
1068 static void dummy_set_frame_offset(ir_node *node, int bias)
1072 panic("dummy_set_frame_offset() should not be called");
1075 static int dummy_get_sp_bias(const ir_node *node)
1081 /* for "middleend" nodes */
1082 static const arch_irn_ops_t dummy_be_irn_ops = {
1083 dummy_get_frame_entity,
1084 dummy_set_frame_offset,
1086 NULL, /* get_inverse */
1087 NULL, /* get_op_estimated_cost */
1088 NULL, /* possible_memory_operand */
1089 NULL, /* perform_memory_operand */
1094 ir_node *be_new_Phi(ir_node *block, int n_ins, ir_node **ins, ir_mode *mode,
1095 const arch_register_class_t *cls)
1097 const arch_register_req_t *req;
1098 ir_graph *irg = get_irn_irg(block);
1099 struct obstack *obst = be_get_be_obst(irg);
1100 backend_info_t *info;
1103 ir_node *phi = new_r_Phi(block, n_ins, ins, mode);
1104 info = be_get_info(phi);
1105 info->out_infos = NEW_ARR_D(reg_out_info_t, obst, 1);
1106 memset(info->out_infos, 0, 1 * sizeof(info->out_infos[0]));
1107 info->in_reqs = OALLOCN(obst, const arch_register_req_t*, n_ins);
1110 req = arch_no_register_req;
1112 req = cls->class_req;
1114 info->out_infos[0].req = req;
1115 for (i = 0; i < n_ins; ++i) {
1116 info->in_reqs[i] = req;
1122 void be_set_phi_reg_req(ir_node *node, const arch_register_req_t *req)
1124 int arity = get_irn_arity(node);
1127 backend_info_t *info = be_get_info(node);
1128 info->out_infos[0].req = req;
1129 for (i = 0; i < arity; ++i) {
1130 info->in_reqs[i] = req;
1133 assert(mode_is_datab(get_irn_mode(node)));
1136 void be_dump_phi_reg_reqs(FILE *F, ir_node *node, dump_reason_t reason)
1139 case dump_node_opcode_txt:
1140 fputs(get_op_name(get_irn_op(node)), F);
1142 case dump_node_mode_txt:
1143 fprintf(F, "%s", get_mode_name(get_irn_mode(node)));
1145 case dump_node_nodeattr_txt:
1147 case dump_node_info_txt:
1149 backend_info_t *info = be_get_info(node);
1150 if (info != NULL && info->out_infos[0].req != NULL) {
1151 arch_dump_reqs_and_registers(F, node);
1161 static const arch_irn_ops_t phi_irn_ops = {
1162 dummy_get_frame_entity,
1163 dummy_set_frame_offset,
1165 NULL, /* get_inverse */
1166 NULL, /* get_op_estimated_cost */
1167 NULL, /* possible_memory_operand */
1168 NULL, /* perform_memory_operand */
1174 * ir_op-Operation: dump a be node to file
1176 static void dump_node(FILE *f, ir_node *irn, dump_reason_t reason)
1178 assert(is_be_node(irn));
1181 case dump_node_opcode_txt:
1182 fputs(get_op_name(get_irn_op(irn)), f);
1184 case dump_node_mode_txt:
1185 if (be_is_Copy(irn) || be_is_CopyKeep(irn)) {
1186 fprintf(f, "%s", get_mode_name(get_irn_mode(irn)));
1189 case dump_node_nodeattr_txt:
1190 if (be_is_Call(irn)) {
1191 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(irn);
1193 fprintf(f, " [%s] ", get_entity_name(a->ent));
1195 if (be_is_IncSP(irn)) {
1196 const be_incsp_attr_t *attr = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
1197 fprintf(f, " [%d] ", attr->offset);
1200 case dump_node_info_txt:
1201 arch_dump_reqs_and_registers(f, irn);
1203 if (be_has_frame_entity(irn)) {
1204 const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
1206 unsigned size = get_type_size_bytes(get_entity_type(a->ent));
1207 ir_fprintf(f, "frame entity: %+F, offset 0x%x (%d), size 0x%x (%d) bytes\n",
1208 a->ent, a->offset, a->offset, size, size);
1213 switch (get_irn_opcode(irn)) {
1215 const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
1216 fprintf(f, "align: %d\n", a->align);
1217 fprintf(f, "offset: %d\n", a->offset);
1221 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(irn);
1224 fprintf(f, "\ncalling: %s\n", get_entity_name(a->ent));
1229 for (i = 0; i < be_get_MemPerm_entity_arity(irn); ++i) {
1230 ir_entity *in, *out;
1231 in = be_get_MemPerm_in_entity(irn, i);
1232 out = be_get_MemPerm_out_entity(irn, i);
1234 fprintf(f, "\nin[%d]: %s\n", i, get_entity_name(in));
1237 fprintf(f, "\nout[%d]: %s\n", i, get_entity_name(out));
1251 * Copies the backend specific attributes from old node to new node.
1253 static void copy_attr(ir_graph *irg, const ir_node *old_node, ir_node *new_node)
1255 const void *old_attr = get_irn_generic_attr_const(old_node);
1256 void *new_attr = get_irn_generic_attr(new_node);
1257 struct obstack *obst = be_get_be_obst(irg);
1258 backend_info_t *old_info = be_get_info(old_node);
1259 backend_info_t *new_info = be_get_info(new_node);
1261 assert(is_be_node(old_node));
1262 assert(is_be_node(new_node));
1264 memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
1266 new_info->flags = old_info->flags;
1267 if (old_info->out_infos != NULL) {
1268 size_t n_outs = ARR_LEN(old_info->out_infos);
1269 /* need dyanmic out infos? */
1270 if (be_is_Perm(new_node)) {
1271 new_info->out_infos = NEW_ARR_F(reg_out_info_t, n_outs);
1273 new_info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_outs);
1275 memcpy(new_info->out_infos, old_info->out_infos,
1276 n_outs * sizeof(new_info->out_infos[0]));
1278 new_info->out_infos = NULL;
1282 if (old_info->in_reqs != NULL) {
1283 unsigned n_ins = get_irn_arity(old_node);
1284 /* need dynamic in infos? */
1285 if (get_irn_op(old_node)->opar == oparity_dynamic) {
1286 new_info->in_reqs = NEW_ARR_F(const arch_register_req_t*, n_ins);
1288 new_info->in_reqs = OALLOCN(obst,const arch_register_req_t*, n_ins);
1290 memcpy(new_info->in_reqs, old_info->in_reqs,
1291 n_ins * sizeof(new_info->in_reqs[0]));
1293 new_info->in_reqs = NULL;
1297 static const ir_op_ops be_node_op_ops = {
1317 int is_be_node(const ir_node *irn)
1319 return get_op_ops(get_irn_op(irn))->be_ops == &be_node_irn_ops;
1322 void be_init_op(void)
1326 /* Acquire all needed opcodes. */
1327 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);
1328 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);
1329 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);
1330 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);
1331 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);
1332 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);
1333 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);
1334 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);
1335 ir_op_set_fragile_indices(op_be_Call, n_be_Call_mem, pn_be_Call_X_regular, pn_be_Call_X_except);
1336 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);
1337 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);
1338 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);
1339 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);
1340 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);
1341 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);
1343 op_be_Spill->ops.node_cmp_attr = FrameAddr_cmp_attr;
1344 op_be_Reload->ops.node_cmp_attr = FrameAddr_cmp_attr;
1345 op_be_Perm->ops.node_cmp_attr = be_nodes_equal;
1346 op_be_MemPerm->ops.node_cmp_attr = be_nodes_equal;
1347 op_be_Copy->ops.node_cmp_attr = be_nodes_equal;
1348 op_be_Keep->ops.node_cmp_attr = be_nodes_equal;
1349 op_be_CopyKeep->ops.node_cmp_attr = be_nodes_equal;
1350 op_be_Call->ops.node_cmp_attr = Call_cmp_attr;
1351 op_be_Return->ops.node_cmp_attr = Return_cmp_attr;
1352 op_be_AddSP->ops.node_cmp_attr = be_nodes_equal;
1353 op_be_SubSP->ops.node_cmp_attr = be_nodes_equal;
1354 op_be_IncSP->ops.node_cmp_attr = IncSP_cmp_attr;
1355 op_be_Start->ops.node_cmp_attr = be_nodes_equal;
1356 op_be_FrameAddr->ops.node_cmp_attr = FrameAddr_cmp_attr;
1358 /* attach out dummy_ops to middle end nodes */
1359 for (opc = iro_First; opc <= iro_Last; ++opc) {
1360 ir_op *op = get_irp_opcode(opc);
1361 assert(op->ops.be_ops == NULL);
1362 op->ops.be_ops = &dummy_be_irn_ops;
1365 op_Phi->ops.be_ops = &phi_irn_ops;