2 * Copyright (C) 1995-2008 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Backend node support for generic backend nodes.
23 * @author Sebastian Hack
27 * Backend node support for generic backend nodes.
28 * This file provides Perm, Copy, Spill and Reload nodes.
40 #include "bitfiddle.h"
41 #include "raw_bitset.h"
61 /** The be_Return nodes attribute type. */
63 int num_ret_vals; /**< number of return values */
64 unsigned pop; /**< number of bytes that should be popped */
65 int emit_pop; /**< if set, emit pop bytes, even if pop = 0 */
68 /** The be_IncSP attribute type. */
70 int offset; /**< The offset by which the stack shall be
72 int align; /**< whether stack should be aligned after the
76 /** The be_Frame attribute type. */
82 /** The be_Call attribute type. */
84 ir_entity *ent; /**< called entity if this is a static call. */
86 ir_type *call_tp; /**< call type, copied from the original Call */
90 ir_entity **in_entities;
91 ir_entity **out_entities;
100 ir_op *op_be_CopyKeep;
107 ir_op *op_be_FrameAddr;
108 ir_op *op_be_Barrier;
110 static const ir_op_ops be_node_op_ops;
113 * Compare the attributes of two be_FrameAddr nodes.
115 * @return zero if both nodes have identically attributes
117 static int FrameAddr_cmp_attr(ir_node *a, ir_node *b)
119 const be_frame_attr_t *a_attr = get_irn_generic_attr_const(a);
120 const be_frame_attr_t *b_attr = get_irn_generic_attr_const(b);
122 if (a_attr->ent != b_attr->ent || a_attr->offset != b_attr->offset)
125 return be_nodes_equal(a, b);
129 * Compare the attributes of two be_Return nodes.
131 * @return zero if both nodes have identically attributes
133 static int Return_cmp_attr(ir_node *a, ir_node *b)
135 const be_return_attr_t *a_attr = get_irn_generic_attr_const(a);
136 const be_return_attr_t *b_attr = get_irn_generic_attr_const(b);
138 if (a_attr->num_ret_vals != b_attr->num_ret_vals)
140 if (a_attr->pop != b_attr->pop)
142 if (a_attr->emit_pop != b_attr->emit_pop)
145 return be_nodes_equal(a, b);
149 * Compare the attributes of two be_IncSP nodes.
151 * @return zero if both nodes have identically attributes
153 static int IncSP_cmp_attr(ir_node *a, ir_node *b)
155 const be_incsp_attr_t *a_attr = get_irn_generic_attr_const(a);
156 const be_incsp_attr_t *b_attr = get_irn_generic_attr_const(b);
158 if (a_attr->offset != b_attr->offset)
161 return be_nodes_equal(a, b);
165 * Compare the attributes of two be_Call nodes.
167 * @return zero if both nodes have identically attributes
169 static int Call_cmp_attr(ir_node *a, ir_node *b)
171 const be_call_attr_t *a_attr = get_irn_generic_attr_const(a);
172 const be_call_attr_t *b_attr = get_irn_generic_attr_const(b);
174 if (a_attr->ent != b_attr->ent ||
175 a_attr->call_tp != b_attr->call_tp)
178 return be_nodes_equal(a, b);
181 static arch_register_req_t *allocate_reg_req(const ir_node *node)
183 ir_graph *irg = get_irn_irg(node);
184 struct obstack *obst = be_get_be_obst(irg);
186 arch_register_req_t *req = obstack_alloc(obst, sizeof(*req));
187 memset(req, 0, sizeof(*req));
191 void be_set_constr_in(ir_node *node, int pos, const arch_register_req_t *req)
193 backend_info_t *info = be_get_info(node);
194 assert(pos < get_irn_arity(node));
195 info->in_reqs[pos] = req;
198 void be_set_constr_out(ir_node *node, int pos, const arch_register_req_t *req)
200 backend_info_t *info = be_get_info(node);
201 info->out_infos[pos].req = req;
205 * Initializes the generic attribute of all be nodes and return it.
207 static void *init_node_attr(ir_node *node, int n_inputs, int n_outputs)
209 ir_graph *irg = get_irn_irg(node);
210 struct obstack *obst = be_get_be_obst(irg);
211 backend_info_t *info = be_get_info(node);
212 const arch_register_req_t **in_reqs;
216 assert(n_inputs == get_irn_arity(node));
217 in_reqs = OALLOCN(obst, const arch_register_req_t*, n_inputs);
218 for (i = 0; i < n_inputs; ++i) {
219 in_reqs[i] = arch_no_register_req;
222 in_reqs = NEW_ARR_F(const arch_register_req_t*, 0);
224 info->in_reqs = in_reqs;
226 if (n_outputs >= 0) {
228 info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_outputs);
229 memset(info->out_infos, 0, n_outputs * sizeof(info->out_infos[0]));
230 for (i = 0; i < n_outputs; ++i) {
231 info->out_infos[i].req = arch_no_register_req;
234 info->out_infos = NEW_ARR_F(reg_out_info_t, 0);
237 return get_irn_generic_attr(node);
240 static void add_register_req_out(ir_node *node)
242 backend_info_t *info = be_get_info(node);
243 reg_out_info_t out_info;
244 memset(&out_info, 0, sizeof(out_info));
245 out_info.req = arch_no_register_req;
246 ARR_APP1(reg_out_info_t, info->out_infos, out_info);
249 static void add_register_req_in(ir_node *node)
251 backend_info_t *info = be_get_info(node);
252 ARR_APP1(const arch_register_req_t*, info->in_reqs, arch_no_register_req);
255 ir_node *be_new_Spill(const arch_register_class_t *cls,
256 const arch_register_class_t *cls_frame, ir_node *bl,
257 ir_node *frame, ir_node *to_spill)
262 ir_graph *irg = get_Block_irg(bl);
266 res = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
267 a = init_node_attr(res, 2, 1);
271 be_node_set_reg_class_in(res, be_pos_Spill_frame, cls_frame);
272 be_node_set_reg_class_in(res, be_pos_Spill_val, cls);
274 * For spills and reloads, we return "none" as requirement for frame
275 * pointer, so every input is ok. Some backends need this (STA).
276 * Matze: we should investigate if this is really needed, this solution
277 * looks very hacky to me
279 be_set_constr_in(res, be_pos_Spill_frame, arch_no_register_req);
281 arch_set_out_register_req(res, 0, arch_no_register_req);
286 ir_node *be_new_Reload(const arch_register_class_t *cls,
287 const arch_register_class_t *cls_frame, ir_node *block,
288 ir_node *frame, ir_node *mem, ir_mode *mode)
292 ir_graph *irg = get_Block_irg(block);
296 res = new_ir_node(NULL, irg, block, op_be_Reload, mode, 2, in);
298 init_node_attr(res, 2, 1);
299 be_node_set_reg_class_out(res, 0, cls);
301 be_node_set_reg_class_in(res, be_pos_Reload_frame, cls_frame);
302 arch_irn_set_flags(res, arch_irn_flags_rematerializable);
305 * For spills and reloads, we return "none" as requirement for frame
306 * pointer, so every input is ok. Some backends need this (e.g. STA).
307 * Matze: we should investigate if this is really needed, this solution
308 * looks very hacky to me
310 be_set_constr_in(res, be_pos_Reload_frame, arch_no_register_req);
315 ir_node *be_get_Reload_mem(const ir_node *irn)
317 assert(be_is_Reload(irn));
318 return get_irn_n(irn, be_pos_Reload_mem);
321 ir_node *be_get_Reload_frame(const ir_node *irn)
323 assert(be_is_Reload(irn));
324 return get_irn_n(irn, be_pos_Reload_frame);
327 ir_node *be_get_Spill_val(const ir_node *irn)
329 assert(be_is_Spill(irn));
330 return get_irn_n(irn, be_pos_Spill_val);
333 ir_node *be_get_Spill_frame(const ir_node *irn)
335 assert(be_is_Spill(irn));
336 return get_irn_n(irn, be_pos_Spill_frame);
339 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_node *block,
340 int n, ir_node *in[])
343 ir_graph *irg = get_Block_irg(block);
345 ir_node *irn = new_ir_node(NULL, irg, block, op_be_Perm, mode_T, n, in);
346 init_node_attr(irn, n, n);
347 for (i = 0; i < n; ++i) {
348 be_node_set_reg_class_in(irn, i, cls);
349 be_node_set_reg_class_out(irn, i, cls);
355 void be_Perm_reduce(ir_node *perm, int new_size, int *map)
357 int arity = get_irn_arity(perm);
358 const arch_register_req_t **old_in_reqs
359 = ALLOCAN(const arch_register_req_t*, arity);
360 reg_out_info_t *old_infos = ALLOCAN(reg_out_info_t, arity);
361 backend_info_t *info = be_get_info(perm);
365 assert(be_is_Perm(perm));
366 assert(new_size <= arity);
368 new_in = alloca(new_size * sizeof(*new_in));
370 /* save the old register data */
371 memcpy(old_in_reqs, info->in_reqs, arity * sizeof(old_in_reqs[0]));
372 memcpy(old_infos, info->out_infos, arity * sizeof(old_infos[0]));
374 /* compose the new in array and set the new register data directly */
375 for (i = 0; i < new_size; ++i) {
377 new_in[i] = get_irn_n(perm, idx);
378 info->in_reqs[i] = old_in_reqs[idx];
379 info->out_infos[i] = old_infos[idx];
382 set_irn_in(perm, new_size, new_in);
385 ir_node *be_new_MemPerm(ir_node *block, int n, ir_node *in[])
387 ir_graph *irg = get_Block_irg(block);
388 const arch_env_t *arch_env = be_get_irg_arch_env(irg);
389 ir_node *frame = get_irg_frame(irg);
390 const arch_register_t *sp = arch_env->sp;
392 be_memperm_attr_t *attr;
395 real_in = ALLOCAN(ir_node*, n + 1);
397 memcpy(&real_in[1], in, n * sizeof(real_in[0]));
399 irn = new_ir_node(NULL, irg, block, op_be_MemPerm, mode_T, n+1, real_in);
401 init_node_attr(irn, n + 1, n);
402 be_node_set_reg_class_in(irn, 0, sp->reg_class);
404 attr = get_irn_generic_attr(irn);
405 attr->in_entities = OALLOCNZ(irg->obst, ir_entity*, n);
406 attr->out_entities = OALLOCNZ(irg->obst, ir_entity*, n);
411 ir_node *be_new_Copy(const arch_register_class_t *cls, ir_node *bl, ir_node *op)
415 arch_register_req_t *req;
416 ir_graph *irg = get_Block_irg(bl);
419 res = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
420 init_node_attr(res, 1, 1);
421 be_node_set_reg_class_in(res, 0, cls);
422 be_node_set_reg_class_out(res, 0, cls);
424 req = allocate_reg_req(res);
426 req->type = arch_register_req_type_should_be_same;
427 req->other_same = 1U << 0;
429 be_set_constr_out(res, 0, req);
434 ir_node *be_get_Copy_op(const ir_node *cpy)
436 return get_irn_n(cpy, be_pos_Copy_op);
439 void be_set_Copy_op(ir_node *cpy, ir_node *op)
441 set_irn_n(cpy, be_pos_Copy_op, op);
444 ir_node *be_new_Keep(ir_node *block, int n, ir_node *in[])
448 ir_graph *irg = get_Block_irg(block);
450 res = new_ir_node(NULL, irg, block, op_be_Keep, mode_ANY, -1, NULL);
451 init_node_attr(res, -1, 1);
453 for (i = 0; i < n; ++i) {
454 add_irn_n(res, in[i]);
455 add_register_req_in(res);
462 void be_Keep_add_node(ir_node *keep, const arch_register_class_t *cls, ir_node *node)
466 assert(be_is_Keep(keep));
467 n = add_irn_n(keep, node);
468 add_register_req_in(keep);
469 be_node_set_reg_class_in(keep, n, cls);
472 ir_node *be_new_Call(dbg_info *dbg, ir_graph *irg, ir_node *bl, ir_node *mem,
473 ir_node *sp, ir_node *ptr, int n_outs, int n, ir_node *in[],
477 int real_n = be_pos_Call_first_arg + n;
481 NEW_ARR_A(ir_node *, real_in, real_n);
482 real_in[be_pos_Call_mem] = mem;
483 real_in[be_pos_Call_sp] = sp;
484 real_in[be_pos_Call_ptr] = ptr;
485 memcpy(&real_in[be_pos_Call_first_arg], in, n * sizeof(in[0]));
487 irn = new_ir_node(dbg, irg, bl, op_be_Call, mode_T, real_n, real_in);
488 a = init_node_attr(irn, real_n, n_outs);
490 a->call_tp = call_tp;
495 ir_entity *be_Call_get_entity(const ir_node *call)
497 const be_call_attr_t *a = get_irn_generic_attr_const(call);
498 assert(be_is_Call(call));
502 void be_Call_set_entity(ir_node *call, ir_entity *ent)
504 be_call_attr_t *a = get_irn_generic_attr(call);
505 assert(be_is_Call(call));
509 ir_type *be_Call_get_type(ir_node *call)
511 const be_call_attr_t *a = get_irn_generic_attr_const(call);
512 assert(be_is_Call(call));
516 void be_Call_set_type(ir_node *call, ir_type *call_tp)
518 be_call_attr_t *a = get_irn_generic_attr(call);
519 assert(be_is_Call(call));
520 a->call_tp = call_tp;
523 void be_Call_set_pop(ir_node *call, unsigned pop)
525 be_call_attr_t *a = get_irn_generic_attr(call);
529 unsigned be_Call_get_pop(const ir_node *call)
531 const be_call_attr_t *a = get_irn_generic_attr_const(call);
535 ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *block, int n_res,
536 unsigned pop, int n, ir_node *in[])
542 res = new_ir_node(dbg, irg, block, op_be_Return, mode_X, -1, NULL);
543 init_node_attr(res, -1, 1);
544 for (i = 0; i < n; ++i) {
545 add_irn_n(res, in[i]);
546 add_register_req_in(res);
548 be_set_constr_out(res, 0, arch_no_register_req);
550 a = get_irn_generic_attr(res);
551 a->num_ret_vals = n_res;
558 int be_Return_get_n_rets(const ir_node *ret)
560 const be_return_attr_t *a = get_irn_generic_attr_const(ret);
561 return a->num_ret_vals;
564 unsigned be_Return_get_pop(const ir_node *ret)
566 const be_return_attr_t *a = get_irn_generic_attr_const(ret);
570 int be_Return_get_emit_pop(const ir_node *ret)
572 const be_return_attr_t *a = get_irn_generic_attr_const(ret);
576 void be_Return_set_emit_pop(ir_node *ret, int emit_pop)
578 be_return_attr_t *a = get_irn_generic_attr(ret);
579 a->emit_pop = emit_pop;
582 int be_Return_append_node(ir_node *ret, ir_node *node)
586 pos = add_irn_n(ret, node);
587 add_register_req_in(ret);
592 ir_node *be_new_IncSP(const arch_register_t *sp, ir_node *bl,
593 ir_node *old_sp, int offset, int align)
598 ir_graph *irg = get_Block_irg(bl);
601 irn = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode,
602 sizeof(in) / sizeof(in[0]), in);
603 a = init_node_attr(irn, 1, 1);
607 /* Set output constraint to stack register. */
608 be_node_set_reg_class_in(irn, 0, sp->reg_class);
609 be_set_constr_single_reg_out(irn, 0, sp, arch_register_req_type_produces_sp);
614 ir_node *be_new_AddSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp,
618 ir_node *in[be_pos_AddSP_last];
619 const arch_register_class_t *cls;
622 in[be_pos_AddSP_old_sp] = old_sp;
623 in[be_pos_AddSP_size] = sz;
625 irg = get_Block_irg(bl);
626 irn = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T, be_pos_AddSP_last, in);
627 init_node_attr(irn, be_pos_AddSP_last, pn_be_AddSP_last);
629 /* Set output constraint to stack register. */
630 be_set_constr_single_reg_in(irn, be_pos_AddSP_old_sp, sp, 0);
631 be_node_set_reg_class_in(irn, be_pos_AddSP_size, arch_register_get_class(sp));
632 be_set_constr_single_reg_out(irn, pn_be_AddSP_sp, sp,
633 arch_register_req_type_produces_sp);
635 cls = arch_register_get_class(sp);
640 ir_node *be_new_SubSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp, ir_node *sz)
643 ir_node *in[be_pos_SubSP_last];
646 in[be_pos_SubSP_old_sp] = old_sp;
647 in[be_pos_SubSP_size] = sz;
649 irg = get_Block_irg(bl);
650 irn = new_ir_node(NULL, irg, bl, op_be_SubSP, mode_T, be_pos_SubSP_last, in);
651 init_node_attr(irn, be_pos_SubSP_last, pn_be_SubSP_last);
653 /* Set output constraint to stack register. */
654 be_set_constr_single_reg_in(irn, be_pos_SubSP_old_sp, sp, 0);
655 be_node_set_reg_class_in(irn, be_pos_SubSP_size, arch_register_get_class(sp));
656 be_set_constr_single_reg_out(irn, pn_be_SubSP_sp, sp, arch_register_req_type_produces_sp);
661 ir_node *be_new_Start(dbg_info *dbgi, ir_node *bl, int n_outs)
665 ir_graph *irg = get_Block_irg(bl);
667 res = new_ir_node(dbgi, irg, bl, op_be_Start, mode_T, 0, NULL);
668 init_node_attr(res, 0, -1);
669 for (i = 0; i < n_outs; ++i) {
670 add_register_req_out(res);
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 a = init_node_attr(irn, 1, 1);
688 be_node_set_reg_class_in(irn, 0, cls_frame);
689 be_node_set_reg_class_out(irn, 0, cls_frame);
691 return optimize_node(irn);
694 ir_node *be_get_FrameAddr_frame(const ir_node *node)
696 assert(be_is_FrameAddr(node));
697 return get_irn_n(node, be_pos_FrameAddr_ptr);
700 ir_entity *be_get_FrameAddr_entity(const ir_node *node)
702 const be_frame_attr_t *attr = get_irn_generic_attr_const(node);
706 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)
709 ir_node **in = ALLOCAN(ir_node*, n + 1);
710 ir_graph *irg = get_Block_irg(bl);
713 memcpy(&in[1], in_keep, n * sizeof(in[0]));
714 irn = new_ir_node(NULL, irg, bl, op_be_CopyKeep, mode, n + 1, in);
715 init_node_attr(irn, n + 1, 1);
716 be_node_set_reg_class_in(irn, 0, cls);
717 be_node_set_reg_class_out(irn, 0, cls);
722 ir_node *be_new_CopyKeep_single(const arch_register_class_t *cls, ir_node *bl, ir_node *src, ir_node *keep, ir_mode *mode)
724 return be_new_CopyKeep(cls, bl, src, 1, &keep, mode);
727 ir_node *be_get_CopyKeep_op(const ir_node *cpy)
729 return get_irn_n(cpy, be_pos_CopyKeep_op);
732 void be_set_CopyKeep_op(ir_node *cpy, ir_node *op)
734 set_irn_n(cpy, be_pos_CopyKeep_op, op);
737 ir_node *be_new_Barrier(ir_node *bl, int n, ir_node *in[])
741 ir_graph *irg = get_Block_irg(bl);
743 res = new_ir_node(NULL, irg, bl, op_be_Barrier, mode_T, -1, NULL);
744 init_node_attr(res, -1, -1);
745 for (i = 0; i < n; ++i) {
746 add_irn_n(res, in[i]);
747 add_register_req_in(res);
748 add_register_req_out(res);
754 ir_node *be_Barrier_append_node(ir_node *barrier, ir_node *node)
756 ir_mode *mode = get_irn_mode(node);
757 int n = add_irn_n(barrier, node);
759 ir_node *proj = new_r_Proj(barrier, mode, n);
760 add_register_req_in(barrier);
761 add_register_req_out(barrier);
766 static bool be_has_frame_entity(const ir_node *irn)
768 switch (get_irn_opcode(irn)) {
778 ir_entity *be_get_frame_entity(const ir_node *irn)
780 if (be_has_frame_entity(irn)) {
781 const be_frame_attr_t *a = get_irn_generic_attr_const(irn);
787 int be_get_frame_offset(const ir_node *irn)
789 assert(is_be_node(irn));
790 if (be_has_frame_entity(irn)) {
791 const be_frame_attr_t *a = get_irn_generic_attr_const(irn);
797 void be_set_MemPerm_in_entity(const ir_node *irn, int n, ir_entity *ent)
799 const be_memperm_attr_t *attr = get_irn_generic_attr_const(irn);
801 assert(be_is_MemPerm(irn));
802 assert(n < be_get_MemPerm_entity_arity(irn));
804 attr->in_entities[n] = ent;
807 ir_entity* be_get_MemPerm_in_entity(const ir_node* irn, int n)
809 const be_memperm_attr_t *attr = get_irn_generic_attr_const(irn);
811 assert(be_is_MemPerm(irn));
812 assert(n < be_get_MemPerm_entity_arity(irn));
814 return attr->in_entities[n];
817 void be_set_MemPerm_out_entity(const ir_node *irn, int n, ir_entity *ent)
819 const be_memperm_attr_t *attr = get_irn_generic_attr_const(irn);
821 assert(be_is_MemPerm(irn));
822 assert(n < be_get_MemPerm_entity_arity(irn));
824 attr->out_entities[n] = ent;
827 ir_entity* be_get_MemPerm_out_entity(const ir_node* irn, int n)
829 const be_memperm_attr_t *attr = get_irn_generic_attr_const(irn);
831 assert(be_is_MemPerm(irn));
832 assert(n < be_get_MemPerm_entity_arity(irn));
834 return attr->out_entities[n];
837 int be_get_MemPerm_entity_arity(const ir_node *irn)
839 return get_irn_arity(irn) - 1;
842 const arch_register_req_t *be_create_reg_req(struct obstack *obst,
843 const arch_register_t *reg, arch_register_req_type_t additional_types)
845 arch_register_req_t *req = obstack_alloc(obst, sizeof(*req));
846 const arch_register_class_t *cls = arch_register_get_class(reg);
847 unsigned *limited_bitset;
849 limited_bitset = rbitset_obstack_alloc(obst, arch_register_class_n_regs(cls));
850 rbitset_set(limited_bitset, arch_register_get_index(reg));
852 req->type = arch_register_req_type_limited | additional_types;
854 req->limited = limited_bitset;
859 void be_set_constr_single_reg_in(ir_node *node, int pos,
860 const arch_register_t *reg, arch_register_req_type_t additional_types)
862 const arch_register_req_t *req;
864 if (additional_types == 0) {
865 req = reg->single_req;
867 ir_graph *irg = get_irn_irg(node);
868 struct obstack *obst = be_get_be_obst(irg);
869 req = be_create_reg_req(obst, reg, additional_types);
871 be_set_constr_in(node, pos, req);
874 void be_set_constr_single_reg_out(ir_node *node, int pos,
875 const arch_register_t *reg, arch_register_req_type_t additional_types)
877 const arch_register_req_t *req;
879 /* if we have an ignore register, add ignore flag and just assign it */
880 if (reg->type & arch_register_type_ignore) {
881 additional_types |= arch_register_req_type_ignore;
884 if (additional_types == 0) {
885 req = reg->single_req;
887 ir_graph *irg = get_irn_irg(node);
888 struct obstack *obst = be_get_be_obst(irg);
889 req = be_create_reg_req(obst, reg, additional_types);
892 arch_irn_set_register(node, pos, reg);
893 be_set_constr_out(node, pos, req);
896 void be_node_set_reg_class_in(ir_node *irn, int pos,
897 const arch_register_class_t *cls)
899 be_set_constr_in(irn, pos, cls->class_req);
902 void be_node_set_reg_class_out(ir_node *irn, int pos,
903 const arch_register_class_t *cls)
905 be_set_constr_out(irn, pos, cls->class_req);
908 ir_node *be_get_IncSP_pred(ir_node *irn)
910 assert(be_is_IncSP(irn));
911 return get_irn_n(irn, 0);
914 void be_set_IncSP_pred(ir_node *incsp, ir_node *pred)
916 assert(be_is_IncSP(incsp));
917 set_irn_n(incsp, 0, pred);
920 void be_set_IncSP_offset(ir_node *irn, int offset)
922 be_incsp_attr_t *a = get_irn_generic_attr(irn);
923 assert(be_is_IncSP(irn));
927 int be_get_IncSP_offset(const ir_node *irn)
929 const be_incsp_attr_t *a = get_irn_generic_attr_const(irn);
930 assert(be_is_IncSP(irn));
934 int be_get_IncSP_align(const ir_node *irn)
936 const be_incsp_attr_t *a = get_irn_generic_attr_const(irn);
937 assert(be_is_IncSP(irn));
941 ir_node *be_spill(ir_node *block, ir_node *irn)
943 ir_graph *irg = get_Block_irg(block);
944 ir_node *frame = get_irg_frame(irg);
945 const arch_register_class_t *cls = arch_get_irn_reg_class_out(irn);
946 const arch_register_class_t *cls_frame = arch_get_irn_reg_class_out(frame);
949 spill = be_new_Spill(cls, cls_frame, block, frame, irn);
953 ir_node *be_reload(const arch_register_class_t *cls, ir_node *insert, ir_mode *mode, ir_node *spill)
956 ir_node *bl = is_Block(insert) ? insert : get_nodes_block(insert);
957 ir_graph *irg = get_Block_irg(bl);
958 ir_node *frame = get_irg_frame(irg);
959 const arch_register_class_t *cls_frame = arch_get_irn_reg_class_out(frame);
961 assert(be_is_Spill(spill) || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
963 reload = be_new_Reload(cls, cls_frame, bl, frame, spill, mode);
965 if (is_Block(insert)) {
966 insert = sched_skip(insert, 0, sched_skip_cf_predicator, NULL);
967 sched_add_after(insert, reload);
969 sched_add_before(insert, reload);
976 static arch_irn_class_t be_node_classify(const ir_node *irn)
978 switch (get_irn_opcode(irn)) {
979 case beo_Spill: return arch_irn_class_spill;
980 case beo_Reload: return arch_irn_class_reload;
981 case beo_Perm: return arch_irn_class_perm;
982 case beo_Copy: return arch_irn_class_copy;
987 static ir_entity *be_node_get_frame_entity(const ir_node *irn)
989 return be_get_frame_entity(irn);
992 void be_node_set_frame_entity(ir_node *irn, ir_entity *ent)
996 assert(be_has_frame_entity(irn));
998 a = get_irn_generic_attr(irn);
1002 static void be_node_set_frame_offset(ir_node *irn, int offset)
1006 if (!be_has_frame_entity(irn))
1009 a = get_irn_generic_attr(irn);
1013 static int be_node_get_sp_bias(const ir_node *irn)
1015 if (be_is_IncSP(irn))
1016 return be_get_IncSP_offset(irn);
1017 if (be_is_Call(irn))
1018 return -(int)be_Call_get_pop(irn);
1026 static const arch_irn_ops_t be_node_irn_ops = {
1028 be_node_get_frame_entity,
1029 be_node_set_frame_offset,
1030 be_node_get_sp_bias,
1031 NULL, /* get_inverse */
1032 NULL, /* get_op_estimated_cost */
1033 NULL, /* possible_memory_operand */
1034 NULL, /* perform_memory_operand */
1037 static arch_irn_class_t dummy_classify(const ir_node *node)
1043 static ir_entity* dummy_get_frame_entity(const ir_node *node)
1049 static void dummy_set_frame_offset(ir_node *node, int bias)
1053 panic("dummy_set_frame_offset() should not be called");
1056 static int dummy_get_sp_bias(const ir_node *node)
1062 /* for "middleend" nodes */
1063 static const arch_irn_ops_t dummy_be_irn_ops = {
1065 dummy_get_frame_entity,
1066 dummy_set_frame_offset,
1068 NULL, /* get_inverse */
1069 NULL, /* get_op_estimated_cost */
1070 NULL, /* possible_memory_operand */
1071 NULL, /* perform_memory_operand */
1076 ir_node *be_new_Phi(ir_node *block, int n_ins, ir_node **ins, ir_mode *mode,
1077 const arch_register_class_t *cls)
1079 const arch_register_req_t *req;
1080 ir_graph *irg = get_irn_irg(block);
1081 struct obstack *obst = be_get_be_obst(irg);
1082 backend_info_t *info;
1085 ir_node *phi = new_r_Phi(block, n_ins, ins, mode);
1086 info = be_get_info(phi);
1087 info->out_infos = NEW_ARR_D(reg_out_info_t, obst, 1);
1088 memset(info->out_infos, 0, 1 * sizeof(info->out_infos[0]));
1089 info->in_reqs = OALLOCN(obst, const arch_register_req_t*, n_ins);
1092 req = arch_no_register_req;
1094 req = cls->class_req;
1096 info->out_infos[0].req = req;
1097 for (i = 0; i < n_ins; ++i) {
1098 info->in_reqs[i] = req;
1104 void be_set_phi_reg_req(ir_node *node, const arch_register_req_t *req)
1106 int arity = get_irn_arity(node);
1109 backend_info_t *info = be_get_info(node);
1110 info->out_infos[0].req = req;
1111 for (i = 0; i < arity; ++i) {
1112 info->in_reqs[i] = req;
1115 assert(mode_is_datab(get_irn_mode(node)));
1118 void be_dump_phi_reg_reqs(FILE *F, ir_node *node, dump_reason_t reason)
1121 case dump_node_opcode_txt:
1122 fputs(get_op_name(get_irn_op(node)), F);
1124 case dump_node_mode_txt:
1125 fprintf(F, "%s", get_mode_name(get_irn_mode(node)));
1127 case dump_node_nodeattr_txt:
1129 case dump_node_info_txt:
1131 backend_info_t *info = be_get_info(node);
1132 if (info != NULL && info->out_infos[0].req != NULL) {
1133 arch_dump_reqs_and_registers(F, node);
1143 static const arch_irn_ops_t phi_irn_ops = {
1145 dummy_get_frame_entity,
1146 dummy_set_frame_offset,
1148 NULL, /* get_inverse */
1149 NULL, /* get_op_estimated_cost */
1150 NULL, /* possible_memory_operand */
1151 NULL, /* perform_memory_operand */
1157 * ir_op-Operation: dump a be node to file
1159 static void dump_node(FILE *f, ir_node *irn, dump_reason_t reason)
1161 assert(is_be_node(irn));
1164 case dump_node_opcode_txt:
1165 fputs(get_op_name(get_irn_op(irn)), f);
1167 case dump_node_mode_txt:
1168 if (be_is_Copy(irn) || be_is_CopyKeep(irn)) {
1169 fprintf(f, "%s", get_mode_name(get_irn_mode(irn)));
1172 case dump_node_nodeattr_txt:
1173 if (be_is_Call(irn)) {
1174 const be_call_attr_t *a = get_irn_generic_attr_const(irn);
1176 fprintf(f, " [%s] ", get_entity_name(a->ent));
1178 if (be_is_IncSP(irn)) {
1179 const be_incsp_attr_t *attr = get_irn_generic_attr_const(irn);
1180 if (attr->offset == BE_STACK_FRAME_SIZE_EXPAND) {
1181 fprintf(f, " [Setup Stackframe] ");
1182 } else if (attr->offset == BE_STACK_FRAME_SIZE_SHRINK) {
1183 fprintf(f, " [Destroy Stackframe] ");
1185 fprintf(f, " [%d] ", attr->offset);
1189 case dump_node_info_txt:
1190 arch_dump_reqs_and_registers(f, irn);
1192 if (be_has_frame_entity(irn)) {
1193 const be_frame_attr_t *a = get_irn_generic_attr_const(irn);
1195 unsigned size = get_type_size_bytes(get_entity_type(a->ent));
1196 ir_fprintf(f, "frame entity: %+F, offset 0x%x (%d), size 0x%x (%d) bytes\n",
1197 a->ent, a->offset, a->offset, size, size);
1202 switch (get_irn_opcode(irn)) {
1204 const be_incsp_attr_t *a = get_irn_generic_attr_const(irn);
1205 fprintf(f, "align: %d\n", a->align);
1206 if (a->offset == BE_STACK_FRAME_SIZE_EXPAND)
1207 fprintf(f, "offset: FRAME_SIZE\n");
1208 else if (a->offset == BE_STACK_FRAME_SIZE_SHRINK)
1209 fprintf(f, "offset: -FRAME SIZE\n");
1211 fprintf(f, "offset: %d\n", a->offset);
1215 const be_call_attr_t *a = get_irn_generic_attr_const(irn);
1218 fprintf(f, "\ncalling: %s\n", get_entity_name(a->ent));
1223 for (i = 0; i < be_get_MemPerm_entity_arity(irn); ++i) {
1224 ir_entity *in, *out;
1225 in = be_get_MemPerm_in_entity(irn, i);
1226 out = be_get_MemPerm_out_entity(irn, i);
1228 fprintf(f, "\nin[%d]: %s\n", i, get_entity_name(in));
1231 fprintf(f, "\nout[%d]: %s\n", i, get_entity_name(out));
1245 * Copies the backend specific attributes from old node to new node.
1247 static void copy_attr(ir_graph *irg, const ir_node *old_node, ir_node *new_node)
1249 const void *old_attr = get_irn_generic_attr_const(old_node);
1250 void *new_attr = get_irn_generic_attr(new_node);
1251 struct obstack *obst = be_get_be_obst(irg);
1252 backend_info_t *old_info = be_get_info(old_node);
1253 backend_info_t *new_info = be_get_info(new_node);
1255 assert(is_be_node(old_node));
1256 assert(is_be_node(new_node));
1258 memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
1260 if (old_info->out_infos != NULL) {
1261 unsigned n_outs = ARR_LEN(old_info->out_infos);
1262 /* need dyanmic out infos? */
1263 if (be_is_Barrier(new_node) || be_is_Perm(new_node)) {
1264 new_info->out_infos = NEW_ARR_F(reg_out_info_t, n_outs);
1266 new_info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_outs);
1268 memcpy(new_info->out_infos, old_info->out_infos,
1269 n_outs * sizeof(new_info->out_infos[0]));
1271 new_info->out_infos = NULL;
1275 if (old_info->in_reqs != NULL) {
1276 unsigned n_ins = get_irn_arity(old_node);
1277 /* need dynamic in infos? */
1278 if (get_irn_op(old_node)->opar == oparity_dynamic) {
1279 new_info->in_reqs = NEW_ARR_F(const arch_register_req_t*, n_ins);
1281 new_info->in_reqs = OALLOCN(obst,const arch_register_req_t*, n_ins);
1283 memcpy(new_info->in_reqs, old_info->in_reqs,
1284 n_ins * sizeof(new_info->in_reqs[0]));
1286 new_info->in_reqs = NULL;
1290 static const ir_op_ops be_node_op_ops = {
1310 int is_be_node(const ir_node *irn)
1312 return get_op_ops(get_irn_op(irn))->be_ops == &be_node_irn_ops;
1315 void be_init_op(void)
1319 /* Acquire all needed opcodes. */
1320 op_be_Spill = new_ir_op(beo_Spill, "be_Spill", op_pin_state_pinned, irop_flag_none, oparity_unary, 0, sizeof(be_frame_attr_t), &be_node_op_ops);
1321 op_be_Reload = new_ir_op(beo_Reload, "be_Reload", op_pin_state_pinned, irop_flag_none, oparity_zero, 0, sizeof(be_frame_attr_t), &be_node_op_ops);
1322 op_be_Perm = new_ir_op(beo_Perm, "be_Perm", op_pin_state_pinned, irop_flag_none, oparity_variable, 0, 0, &be_node_op_ops);
1323 op_be_MemPerm = new_ir_op(beo_MemPerm, "be_MemPerm", op_pin_state_pinned, irop_flag_none, oparity_variable, 0, sizeof(be_memperm_attr_t), &be_node_op_ops);
1324 op_be_Copy = new_ir_op(beo_Copy, "be_Copy", op_pin_state_floats, irop_flag_none, oparity_unary, 0, 0, &be_node_op_ops);
1325 op_be_Keep = new_ir_op(beo_Keep, "be_Keep", op_pin_state_floats, irop_flag_keep, oparity_dynamic, 0, 0, &be_node_op_ops);
1326 op_be_CopyKeep = new_ir_op(beo_CopyKeep, "be_CopyKeep", op_pin_state_floats, irop_flag_keep, oparity_variable, 0, 0, &be_node_op_ops);
1327 op_be_Call = new_ir_op(beo_Call, "be_Call", op_pin_state_pinned, irop_flag_fragile|irop_flag_uses_memory, oparity_variable, 0, sizeof(be_call_attr_t), &be_node_op_ops);
1328 op_be_Return = new_ir_op(beo_Return, "be_Return", op_pin_state_pinned, irop_flag_cfopcode, oparity_dynamic, 0, sizeof(be_return_attr_t), &be_node_op_ops);
1329 op_be_AddSP = new_ir_op(beo_AddSP, "be_AddSP", op_pin_state_pinned, irop_flag_none, oparity_unary, 0, 0, &be_node_op_ops);
1330 op_be_SubSP = new_ir_op(beo_SubSP, "be_SubSP", op_pin_state_pinned, irop_flag_none, oparity_unary, 0, 0, &be_node_op_ops);
1331 op_be_IncSP = new_ir_op(beo_IncSP, "be_IncSP", op_pin_state_pinned, irop_flag_none, oparity_unary, 0, sizeof(be_incsp_attr_t), &be_node_op_ops);
1332 op_be_Start = new_ir_op(beo_Start, "be_Start", op_pin_state_pinned, irop_flag_none, oparity_zero, 0, 0, &be_node_op_ops);
1333 op_be_FrameAddr = new_ir_op(beo_FrameAddr, "be_FrameAddr", op_pin_state_floats, irop_flag_none, oparity_unary, 0, sizeof(be_frame_attr_t), &be_node_op_ops);
1334 op_be_Barrier = new_ir_op(beo_Barrier, "be_Barrier", op_pin_state_pinned, irop_flag_none, oparity_dynamic, 0, 0, &be_node_op_ops);
1336 op_be_Spill->ops.node_cmp_attr = FrameAddr_cmp_attr;
1337 op_be_Reload->ops.node_cmp_attr = FrameAddr_cmp_attr;
1338 op_be_Perm->ops.node_cmp_attr = be_nodes_equal;
1339 op_be_MemPerm->ops.node_cmp_attr = be_nodes_equal;
1340 op_be_Copy->ops.node_cmp_attr = be_nodes_equal;
1341 op_be_Keep->ops.node_cmp_attr = be_nodes_equal;
1342 op_be_CopyKeep->ops.node_cmp_attr = be_nodes_equal;
1343 op_be_Call->ops.node_cmp_attr = Call_cmp_attr;
1344 op_be_Return->ops.node_cmp_attr = Return_cmp_attr;
1345 op_be_AddSP->ops.node_cmp_attr = be_nodes_equal;
1346 op_be_SubSP->ops.node_cmp_attr = be_nodes_equal;
1347 op_be_IncSP->ops.node_cmp_attr = IncSP_cmp_attr;
1348 op_be_Start->ops.node_cmp_attr = be_nodes_equal;
1349 op_be_FrameAddr->ops.node_cmp_attr = FrameAddr_cmp_attr;
1350 op_be_Barrier->ops.node_cmp_attr = be_nodes_equal;
1352 /* attach out dummy_ops to middle end nodes */
1353 for (opc = iro_First; opc <= iro_Last; ++opc) {
1354 ir_op *op = get_irp_opcode(opc);
1355 assert(op->ops.be_ops == NULL);
1356 op->ops.be_ops = &dummy_be_irn_ops;
1359 op_Phi->ops.be_ops = &phi_irn_ops;