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 /** 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;
110 * Compare the attributes of two be_FrameAddr nodes.
112 * @return zero if both nodes have identically attributes
114 static int FrameAddr_cmp_attr(const ir_node *a, const ir_node *b)
116 const be_frame_attr_t *a_attr = (const be_frame_attr_t*)get_irn_generic_attr_const(a);
117 const be_frame_attr_t *b_attr = (const be_frame_attr_t*)get_irn_generic_attr_const(b);
119 if (a_attr->ent != b_attr->ent || a_attr->offset != b_attr->offset)
122 return be_nodes_equal(a, b);
126 * Compare the attributes of two be_Return nodes.
128 * @return zero if both nodes have identically attributes
130 static int Return_cmp_attr(const ir_node *a, const ir_node *b)
132 const be_return_attr_t *a_attr = (const be_return_attr_t*)get_irn_generic_attr_const(a);
133 const be_return_attr_t *b_attr = (const be_return_attr_t*)get_irn_generic_attr_const(b);
135 if (a_attr->num_ret_vals != b_attr->num_ret_vals)
137 if (a_attr->pop != b_attr->pop)
139 if (a_attr->emit_pop != b_attr->emit_pop)
142 return be_nodes_equal(a, b);
146 * Compare the attributes of two be_IncSP nodes.
148 * @return zero if both nodes have identically attributes
150 static int IncSP_cmp_attr(const ir_node *a, const ir_node *b)
152 const be_incsp_attr_t *a_attr = (const be_incsp_attr_t*)get_irn_generic_attr_const(a);
153 const be_incsp_attr_t *b_attr = (const be_incsp_attr_t*)get_irn_generic_attr_const(b);
155 if (a_attr->offset != b_attr->offset)
158 return be_nodes_equal(a, b);
162 * Compare the attributes of two be_Call nodes.
164 * @return zero if both nodes have identically attributes
166 static int Call_cmp_attr(const ir_node *a, const ir_node *b)
168 const be_call_attr_t *a_attr = (const be_call_attr_t*)get_irn_generic_attr_const(a);
169 const be_call_attr_t *b_attr = (const be_call_attr_t*)get_irn_generic_attr_const(b);
171 if (a_attr->ent != b_attr->ent ||
172 a_attr->call_tp != b_attr->call_tp)
175 return be_nodes_equal(a, b);
178 static arch_register_req_t *allocate_reg_req(const ir_node *node)
180 ir_graph *irg = get_irn_irg(node);
181 struct obstack *obst = be_get_be_obst(irg);
183 arch_register_req_t *req = OALLOCZ(obst, arch_register_req_t);
187 void be_set_constr_in(ir_node *node, int pos, const arch_register_req_t *req)
189 backend_info_t *info = be_get_info(node);
190 assert(pos < get_irn_arity(node));
191 info->in_reqs[pos] = req;
194 void be_set_constr_out(ir_node *node, int pos, const arch_register_req_t *req)
196 backend_info_t *info = be_get_info(node);
197 info->out_infos[pos].req = req;
201 * Initializes the generic attribute of all be nodes and return it.
203 static void init_node_attr(ir_node *node, int n_inputs, int n_outputs)
205 ir_graph *irg = get_irn_irg(node);
206 struct obstack *obst = be_get_be_obst(irg);
207 backend_info_t *info = be_get_info(node);
208 const arch_register_req_t **in_reqs;
212 assert(n_inputs == get_irn_arity(node));
213 in_reqs = OALLOCN(obst, const arch_register_req_t*, n_inputs);
214 for (i = 0; i < n_inputs; ++i) {
215 in_reqs[i] = arch_no_register_req;
218 in_reqs = NEW_ARR_F(const arch_register_req_t*, 0);
220 info->in_reqs = in_reqs;
222 if (n_outputs >= 0) {
224 info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_outputs);
225 memset(info->out_infos, 0, n_outputs * sizeof(info->out_infos[0]));
226 for (i = 0; i < n_outputs; ++i) {
227 info->out_infos[i].req = arch_no_register_req;
230 info->out_infos = NEW_ARR_F(reg_out_info_t, 0);
234 static void add_register_req_out(ir_node *node)
236 backend_info_t *info = be_get_info(node);
237 reg_out_info_t out_info;
238 memset(&out_info, 0, sizeof(out_info));
239 out_info.req = arch_no_register_req;
240 ARR_APP1(reg_out_info_t, info->out_infos, out_info);
243 static void add_register_req_in(ir_node *node)
245 backend_info_t *info = be_get_info(node);
246 ARR_APP1(const arch_register_req_t*, info->in_reqs, arch_no_register_req);
249 ir_node *be_new_Spill(const arch_register_class_t *cls,
250 const arch_register_class_t *cls_frame, ir_node *bl,
251 ir_node *frame, ir_node *to_spill)
256 ir_graph *irg = get_Block_irg(bl);
260 res = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
261 init_node_attr(res, 2, 1);
262 a = (be_frame_attr_t*) get_irn_generic_attr(res);
266 be_node_set_reg_class_in(res, be_pos_Spill_frame, cls_frame);
267 be_node_set_reg_class_in(res, be_pos_Spill_val, cls);
269 * For spills and reloads, we return "none" as requirement for frame
270 * pointer, so every input is ok. Some backends need this (STA).
271 * Matze: we should investigate if this is really needed, this solution
272 * looks very hacky to me
274 be_set_constr_in(res, be_pos_Spill_frame, arch_no_register_req);
276 arch_set_out_register_req(res, 0, arch_no_register_req);
281 ir_node *be_new_Reload(const arch_register_class_t *cls,
282 const arch_register_class_t *cls_frame, ir_node *block,
283 ir_node *frame, ir_node *mem, ir_mode *mode)
287 ir_graph *irg = get_Block_irg(block);
291 res = new_ir_node(NULL, irg, block, op_be_Reload, mode, 2, in);
293 init_node_attr(res, 2, 1);
294 be_node_set_reg_class_out(res, 0, cls);
296 be_node_set_reg_class_in(res, be_pos_Reload_frame, cls_frame);
297 arch_irn_set_flags(res, arch_irn_flags_rematerializable);
300 * For spills and reloads, we return "none" as requirement for frame
301 * pointer, so every input is ok. Some backends need this (e.g. STA).
302 * Matze: we should investigate if this is really needed, this solution
303 * looks very hacky to me
305 be_set_constr_in(res, be_pos_Reload_frame, arch_no_register_req);
310 ir_node *be_get_Reload_mem(const ir_node *irn)
312 assert(be_is_Reload(irn));
313 return get_irn_n(irn, be_pos_Reload_mem);
316 ir_node *be_get_Reload_frame(const ir_node *irn)
318 assert(be_is_Reload(irn));
319 return get_irn_n(irn, be_pos_Reload_frame);
322 ir_node *be_get_Spill_val(const ir_node *irn)
324 assert(be_is_Spill(irn));
325 return get_irn_n(irn, be_pos_Spill_val);
328 ir_node *be_get_Spill_frame(const ir_node *irn)
330 assert(be_is_Spill(irn));
331 return get_irn_n(irn, be_pos_Spill_frame);
334 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_node *block,
335 int n, ir_node *in[])
338 ir_graph *irg = get_Block_irg(block);
340 ir_node *irn = new_ir_node(NULL, irg, block, op_be_Perm, mode_T, n, in);
341 init_node_attr(irn, n, n);
342 for (i = 0; i < n; ++i) {
343 be_node_set_reg_class_in(irn, i, cls);
344 be_node_set_reg_class_out(irn, i, cls);
350 void be_Perm_reduce(ir_node *perm, int new_size, int *map)
352 int arity = get_irn_arity(perm);
353 const arch_register_req_t **old_in_reqs
354 = ALLOCAN(const arch_register_req_t*, arity);
355 reg_out_info_t *old_infos = ALLOCAN(reg_out_info_t, arity);
356 backend_info_t *info = be_get_info(perm);
360 assert(be_is_Perm(perm));
361 assert(new_size <= arity);
363 new_in = ALLOCAN(ir_node*, new_size);
365 /* save the old register data */
366 memcpy(old_in_reqs, info->in_reqs, arity * sizeof(old_in_reqs[0]));
367 memcpy(old_infos, info->out_infos, arity * sizeof(old_infos[0]));
369 /* compose the new in array and set the new register data directly */
370 for (i = 0; i < new_size; ++i) {
372 new_in[i] = get_irn_n(perm, idx);
373 info->in_reqs[i] = old_in_reqs[idx];
374 info->out_infos[i] = old_infos[idx];
377 set_irn_in(perm, new_size, new_in);
380 ir_node *be_new_MemPerm(ir_node *block, int n, ir_node *in[])
382 ir_graph *irg = get_Block_irg(block);
383 const arch_env_t *arch_env = be_get_irg_arch_env(irg);
384 ir_node *frame = get_irg_frame(irg);
385 const arch_register_t *sp = arch_env->sp;
387 be_memperm_attr_t *attr;
390 real_in = ALLOCAN(ir_node*, n + 1);
392 memcpy(&real_in[1], in, n * sizeof(real_in[0]));
394 irn = new_ir_node(NULL, irg, block, op_be_MemPerm, mode_T, n+1, real_in);
396 init_node_attr(irn, n + 1, n);
397 be_node_set_reg_class_in(irn, 0, sp->reg_class);
399 attr = (be_memperm_attr_t*)get_irn_generic_attr(irn);
400 attr->in_entities = OALLOCNZ(irg->obst, ir_entity*, n);
401 attr->out_entities = OALLOCNZ(irg->obst, ir_entity*, n);
406 ir_node *be_new_Copy(const arch_register_class_t *cls, ir_node *bl, ir_node *op)
410 arch_register_req_t *req;
411 ir_graph *irg = get_Block_irg(bl);
414 res = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
415 init_node_attr(res, 1, 1);
416 be_node_set_reg_class_in(res, 0, cls);
417 be_node_set_reg_class_out(res, 0, cls);
419 req = allocate_reg_req(res);
421 req->type = arch_register_req_type_should_be_same;
422 req->other_same = 1U << 0;
424 be_set_constr_out(res, 0, req);
429 ir_node *be_get_Copy_op(const ir_node *cpy)
431 return get_irn_n(cpy, be_pos_Copy_op);
434 void be_set_Copy_op(ir_node *cpy, ir_node *op)
436 set_irn_n(cpy, be_pos_Copy_op, op);
439 ir_node *be_new_Keep(ir_node *block, int n, ir_node *in[])
443 ir_graph *irg = get_Block_irg(block);
445 res = new_ir_node(NULL, irg, block, op_be_Keep, mode_ANY, -1, NULL);
446 init_node_attr(res, -1, 1);
448 for (i = 0; i < n; ++i) {
449 add_irn_n(res, in[i]);
450 add_register_req_in(res);
457 void be_Keep_add_node(ir_node *keep, const arch_register_class_t *cls, ir_node *node)
461 assert(be_is_Keep(keep));
462 n = add_irn_n(keep, node);
463 add_register_req_in(keep);
464 be_node_set_reg_class_in(keep, n, cls);
467 ir_node *be_new_Call(dbg_info *dbg, ir_graph *irg, ir_node *bl, ir_node *mem,
468 ir_node *sp, ir_node *ptr, int n_outs, int n, ir_node *in[],
472 int real_n = be_pos_Call_first_arg + n;
476 NEW_ARR_A(ir_node *, real_in, real_n);
477 real_in[be_pos_Call_mem] = mem;
478 real_in[be_pos_Call_sp] = sp;
479 real_in[be_pos_Call_ptr] = ptr;
480 memcpy(&real_in[be_pos_Call_first_arg], in, n * sizeof(in[0]));
482 irn = new_ir_node(dbg, irg, bl, op_be_Call, mode_T, real_n, real_in);
483 init_node_attr(irn, real_n, n_outs);
484 a = (be_call_attr_t*)get_irn_generic_attr(irn);
486 a->call_tp = call_tp;
491 ir_entity *be_Call_get_entity(const ir_node *call)
493 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
494 assert(be_is_Call(call));
498 void be_Call_set_entity(ir_node *call, ir_entity *ent)
500 be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
501 assert(be_is_Call(call));
505 ir_type *be_Call_get_type(ir_node *call)
507 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
508 assert(be_is_Call(call));
512 void be_Call_set_type(ir_node *call, ir_type *call_tp)
514 be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
515 assert(be_is_Call(call));
516 a->call_tp = call_tp;
519 void be_Call_set_pop(ir_node *call, unsigned pop)
521 be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
525 unsigned be_Call_get_pop(const ir_node *call)
527 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
531 ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *block, int n_res,
532 unsigned pop, int n, ir_node *in[])
538 res = new_ir_node(dbg, irg, block, op_be_Return, mode_X, -1, NULL);
539 init_node_attr(res, -1, 1);
540 for (i = 0; i < n; ++i) {
541 add_irn_n(res, in[i]);
542 add_register_req_in(res);
544 be_set_constr_out(res, 0, arch_no_register_req);
546 a = (be_return_attr_t*)get_irn_generic_attr(res);
547 a->num_ret_vals = n_res;
554 int be_Return_get_n_rets(const ir_node *ret)
556 const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
557 return a->num_ret_vals;
560 unsigned be_Return_get_pop(const ir_node *ret)
562 const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
566 int be_Return_get_emit_pop(const ir_node *ret)
568 const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
572 void be_Return_set_emit_pop(ir_node *ret, int emit_pop)
574 be_return_attr_t *a = (be_return_attr_t*)get_irn_generic_attr(ret);
575 a->emit_pop = emit_pop;
578 int be_Return_append_node(ir_node *ret, ir_node *node)
582 pos = add_irn_n(ret, node);
583 add_register_req_in(ret);
588 ir_node *be_new_IncSP(const arch_register_t *sp, ir_node *bl,
589 ir_node *old_sp, int offset, int align)
594 ir_graph *irg = get_Block_irg(bl);
597 irn = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode,
598 sizeof(in) / sizeof(in[0]), in);
599 init_node_attr(irn, 1, 1);
600 a = (be_incsp_attr_t*)get_irn_generic_attr(irn);
604 /* Set output constraint to stack register. */
605 be_node_set_reg_class_in(irn, 0, sp->reg_class);
606 be_set_constr_single_reg_out(irn, 0, sp, arch_register_req_type_produces_sp);
611 ir_node *be_new_AddSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp,
615 ir_node *in[be_pos_AddSP_last];
616 const arch_register_class_t *cls;
619 in[be_pos_AddSP_old_sp] = old_sp;
620 in[be_pos_AddSP_size] = sz;
622 irg = get_Block_irg(bl);
623 irn = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T, be_pos_AddSP_last, in);
624 init_node_attr(irn, be_pos_AddSP_last, pn_be_AddSP_last);
626 /* Set output constraint to stack register. */
627 be_set_constr_single_reg_in(irn, be_pos_AddSP_old_sp, sp,
628 arch_register_req_type_none);
629 be_node_set_reg_class_in(irn, be_pos_AddSP_size, arch_register_get_class(sp));
630 be_set_constr_single_reg_out(irn, pn_be_AddSP_sp, sp,
631 arch_register_req_type_produces_sp);
633 cls = arch_register_get_class(sp);
638 ir_node *be_new_SubSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp, ir_node *sz)
641 ir_node *in[be_pos_SubSP_last];
644 in[be_pos_SubSP_old_sp] = old_sp;
645 in[be_pos_SubSP_size] = sz;
647 irg = get_Block_irg(bl);
648 irn = new_ir_node(NULL, irg, bl, op_be_SubSP, mode_T, be_pos_SubSP_last, in);
649 init_node_attr(irn, be_pos_SubSP_last, pn_be_SubSP_last);
651 /* Set output constraint to stack register. */
652 be_set_constr_single_reg_in(irn, be_pos_SubSP_old_sp, sp,
653 arch_register_req_type_none);
654 be_node_set_reg_class_in(irn, be_pos_SubSP_size, arch_register_get_class(sp));
655 be_set_constr_single_reg_out(irn, pn_be_SubSP_sp, sp, arch_register_req_type_produces_sp);
660 ir_node *be_new_Start(dbg_info *dbgi, ir_node *bl, int n_outs)
664 ir_graph *irg = get_Block_irg(bl);
666 res = new_ir_node(dbgi, irg, bl, op_be_Start, mode_T, 0, NULL);
667 init_node_attr(res, 0, -1);
668 for (i = 0; i < n_outs; ++i) {
669 add_register_req_out(res);
675 ir_node *be_new_FrameAddr(const arch_register_class_t *cls_frame, ir_node *bl, ir_node *frame, ir_entity *ent)
680 ir_graph *irg = get_Block_irg(bl);
683 irn = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
684 init_node_attr(irn, 1, 1);
685 a = (be_frame_attr_t*)get_irn_generic_attr(irn);
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 = (const be_frame_attr_t*)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 static bool be_has_frame_entity(const ir_node *irn)
739 switch (get_irn_opcode(irn)) {
749 ir_entity *be_get_frame_entity(const ir_node *irn)
751 if (be_has_frame_entity(irn)) {
752 const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
758 int be_get_frame_offset(const ir_node *irn)
760 assert(is_be_node(irn));
761 if (be_has_frame_entity(irn)) {
762 const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
768 void be_set_MemPerm_in_entity(const ir_node *irn, int n, ir_entity *ent)
770 const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
772 assert(be_is_MemPerm(irn));
773 assert(n < be_get_MemPerm_entity_arity(irn));
775 attr->in_entities[n] = ent;
778 ir_entity* be_get_MemPerm_in_entity(const ir_node* irn, int n)
780 const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
782 assert(be_is_MemPerm(irn));
783 assert(n < be_get_MemPerm_entity_arity(irn));
785 return attr->in_entities[n];
788 void be_set_MemPerm_out_entity(const ir_node *irn, int n, ir_entity *ent)
790 const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
792 assert(be_is_MemPerm(irn));
793 assert(n < be_get_MemPerm_entity_arity(irn));
795 attr->out_entities[n] = ent;
798 ir_entity* be_get_MemPerm_out_entity(const ir_node* irn, int n)
800 const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
802 assert(be_is_MemPerm(irn));
803 assert(n < be_get_MemPerm_entity_arity(irn));
805 return attr->out_entities[n];
808 int be_get_MemPerm_entity_arity(const ir_node *irn)
810 return get_irn_arity(irn) - 1;
813 const arch_register_req_t *be_create_reg_req(struct obstack *obst,
814 const arch_register_t *reg, arch_register_req_type_t additional_types)
816 arch_register_req_t *req = OALLOC(obst, arch_register_req_t);
817 const arch_register_class_t *cls = arch_register_get_class(reg);
818 unsigned *limited_bitset;
820 limited_bitset = rbitset_obstack_alloc(obst, arch_register_class_n_regs(cls));
821 rbitset_set(limited_bitset, arch_register_get_index(reg));
823 req->type = arch_register_req_type_limited | additional_types;
825 req->limited = limited_bitset;
830 void be_set_constr_single_reg_in(ir_node *node, int pos,
831 const arch_register_t *reg, arch_register_req_type_t additional_types)
833 const arch_register_req_t *req;
835 if (additional_types == 0) {
836 req = reg->single_req;
838 ir_graph *irg = get_irn_irg(node);
839 struct obstack *obst = be_get_be_obst(irg);
840 req = be_create_reg_req(obst, reg, additional_types);
842 be_set_constr_in(node, pos, req);
845 void be_set_constr_single_reg_out(ir_node *node, int pos,
846 const arch_register_t *reg, arch_register_req_type_t additional_types)
848 ir_graph *irg = get_irn_irg(node);
849 be_irg_t *birg = be_birg_from_irg(irg);
850 const arch_register_req_t *req;
852 /* if we have an ignore register, add ignore flag and just assign it */
853 if (!rbitset_is_set(birg->allocatable_regs, reg->global_index)) {
854 additional_types |= arch_register_req_type_ignore;
857 if (additional_types == 0) {
858 req = reg->single_req;
860 ir_graph *irg = get_irn_irg(node);
861 struct obstack *obst = be_get_be_obst(irg);
862 req = be_create_reg_req(obst, reg, additional_types);
865 arch_irn_set_register(node, pos, reg);
866 be_set_constr_out(node, pos, req);
869 void be_node_set_reg_class_in(ir_node *irn, int pos,
870 const arch_register_class_t *cls)
872 be_set_constr_in(irn, pos, cls->class_req);
875 void be_node_set_reg_class_out(ir_node *irn, int pos,
876 const arch_register_class_t *cls)
878 be_set_constr_out(irn, pos, cls->class_req);
881 ir_node *be_get_IncSP_pred(ir_node *irn)
883 assert(be_is_IncSP(irn));
884 return get_irn_n(irn, 0);
887 void be_set_IncSP_pred(ir_node *incsp, ir_node *pred)
889 assert(be_is_IncSP(incsp));
890 set_irn_n(incsp, 0, pred);
893 void be_set_IncSP_offset(ir_node *irn, int offset)
895 be_incsp_attr_t *a = (be_incsp_attr_t*)get_irn_generic_attr(irn);
896 assert(be_is_IncSP(irn));
900 int be_get_IncSP_offset(const ir_node *irn)
902 const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
903 assert(be_is_IncSP(irn));
907 int be_get_IncSP_align(const ir_node *irn)
909 const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
910 assert(be_is_IncSP(irn));
914 ir_node *be_spill(ir_node *block, ir_node *irn)
916 ir_graph *irg = get_Block_irg(block);
917 ir_node *frame = get_irg_frame(irg);
918 const arch_register_class_t *cls = arch_get_irn_reg_class_out(irn);
919 const arch_register_class_t *cls_frame = arch_get_irn_reg_class_out(frame);
922 spill = be_new_Spill(cls, cls_frame, block, frame, irn);
926 ir_node *be_reload(const arch_register_class_t *cls, ir_node *insert, ir_mode *mode, ir_node *spill)
929 ir_node *bl = is_Block(insert) ? insert : get_nodes_block(insert);
930 ir_graph *irg = get_Block_irg(bl);
931 ir_node *frame = get_irg_frame(irg);
932 const arch_register_class_t *cls_frame = arch_get_irn_reg_class_out(frame);
934 assert(be_is_Spill(spill) || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
936 reload = be_new_Reload(cls, cls_frame, bl, frame, spill, mode);
938 if (is_Block(insert)) {
940 insert = sched_prev(insert);
941 } while (is_cfop(insert));
942 sched_add_after(insert, reload);
944 sched_add_before(insert, reload);
951 static arch_irn_class_t be_node_classify(const ir_node *irn)
953 switch (get_irn_opcode(irn)) {
954 case beo_Spill: return arch_irn_class_spill;
955 case beo_Reload: return arch_irn_class_reload;
956 case beo_Perm: return arch_irn_class_perm;
957 case beo_Copy: return arch_irn_class_copy;
958 default: return arch_irn_class_none;
962 static ir_entity *be_node_get_frame_entity(const ir_node *irn)
964 return be_get_frame_entity(irn);
967 void be_node_set_frame_entity(ir_node *irn, ir_entity *ent)
971 assert(be_has_frame_entity(irn));
973 a = (be_frame_attr_t*)get_irn_generic_attr(irn);
977 static void be_node_set_frame_offset(ir_node *irn, int offset)
981 if (!be_has_frame_entity(irn))
984 a = (be_frame_attr_t*)get_irn_generic_attr(irn);
988 static int be_node_get_sp_bias(const ir_node *irn)
990 if (be_is_IncSP(irn))
991 return be_get_IncSP_offset(irn);
993 return -(int)be_Call_get_pop(irn);
1001 static const arch_irn_ops_t be_node_irn_ops = {
1003 be_node_get_frame_entity,
1004 be_node_set_frame_offset,
1005 be_node_get_sp_bias,
1006 NULL, /* get_inverse */
1007 NULL, /* get_op_estimated_cost */
1008 NULL, /* possible_memory_operand */
1009 NULL, /* perform_memory_operand */
1012 static arch_irn_class_t dummy_classify(const ir_node *node)
1015 return arch_irn_class_none;
1018 static ir_entity* dummy_get_frame_entity(const ir_node *node)
1024 static void dummy_set_frame_offset(ir_node *node, int bias)
1028 panic("dummy_set_frame_offset() should not be called");
1031 static int dummy_get_sp_bias(const ir_node *node)
1037 /* for "middleend" nodes */
1038 static const arch_irn_ops_t dummy_be_irn_ops = {
1040 dummy_get_frame_entity,
1041 dummy_set_frame_offset,
1043 NULL, /* get_inverse */
1044 NULL, /* get_op_estimated_cost */
1045 NULL, /* possible_memory_operand */
1046 NULL, /* perform_memory_operand */
1051 ir_node *be_new_Phi(ir_node *block, int n_ins, ir_node **ins, ir_mode *mode,
1052 const arch_register_class_t *cls)
1054 const arch_register_req_t *req;
1055 ir_graph *irg = get_irn_irg(block);
1056 struct obstack *obst = be_get_be_obst(irg);
1057 backend_info_t *info;
1060 ir_node *phi = new_r_Phi(block, n_ins, ins, mode);
1061 info = be_get_info(phi);
1062 info->out_infos = NEW_ARR_D(reg_out_info_t, obst, 1);
1063 memset(info->out_infos, 0, 1 * sizeof(info->out_infos[0]));
1064 info->in_reqs = OALLOCN(obst, const arch_register_req_t*, n_ins);
1067 req = arch_no_register_req;
1069 req = cls->class_req;
1071 info->out_infos[0].req = req;
1072 for (i = 0; i < n_ins; ++i) {
1073 info->in_reqs[i] = req;
1079 void be_set_phi_reg_req(ir_node *node, const arch_register_req_t *req)
1081 int arity = get_irn_arity(node);
1084 backend_info_t *info = be_get_info(node);
1085 info->out_infos[0].req = req;
1086 for (i = 0; i < arity; ++i) {
1087 info->in_reqs[i] = req;
1090 assert(mode_is_datab(get_irn_mode(node)));
1093 void be_dump_phi_reg_reqs(FILE *F, ir_node *node, dump_reason_t reason)
1096 case dump_node_opcode_txt:
1097 fputs(get_op_name(get_irn_op(node)), F);
1099 case dump_node_mode_txt:
1100 fprintf(F, "%s", get_mode_name(get_irn_mode(node)));
1102 case dump_node_nodeattr_txt:
1104 case dump_node_info_txt:
1106 backend_info_t *info = be_get_info(node);
1107 if (info != NULL && info->out_infos[0].req != NULL) {
1108 arch_dump_reqs_and_registers(F, node);
1118 static const arch_irn_ops_t phi_irn_ops = {
1120 dummy_get_frame_entity,
1121 dummy_set_frame_offset,
1123 NULL, /* get_inverse */
1124 NULL, /* get_op_estimated_cost */
1125 NULL, /* possible_memory_operand */
1126 NULL, /* perform_memory_operand */
1132 * ir_op-Operation: dump a be node to file
1134 static void dump_node(FILE *f, ir_node *irn, dump_reason_t reason)
1136 assert(is_be_node(irn));
1139 case dump_node_opcode_txt:
1140 fputs(get_op_name(get_irn_op(irn)), f);
1142 case dump_node_mode_txt:
1143 if (be_is_Copy(irn) || be_is_CopyKeep(irn)) {
1144 fprintf(f, "%s", get_mode_name(get_irn_mode(irn)));
1147 case dump_node_nodeattr_txt:
1148 if (be_is_Call(irn)) {
1149 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(irn);
1151 fprintf(f, " [%s] ", get_entity_name(a->ent));
1153 if (be_is_IncSP(irn)) {
1154 const be_incsp_attr_t *attr = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
1155 if (attr->offset == BE_STACK_FRAME_SIZE_EXPAND) {
1156 fprintf(f, " [Setup Stackframe] ");
1157 } else if (attr->offset == BE_STACK_FRAME_SIZE_SHRINK) {
1158 fprintf(f, " [Destroy Stackframe] ");
1160 fprintf(f, " [%d] ", attr->offset);
1164 case dump_node_info_txt:
1165 arch_dump_reqs_and_registers(f, irn);
1167 if (be_has_frame_entity(irn)) {
1168 const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
1170 unsigned size = get_type_size_bytes(get_entity_type(a->ent));
1171 ir_fprintf(f, "frame entity: %+F, offset 0x%x (%d), size 0x%x (%d) bytes\n",
1172 a->ent, a->offset, a->offset, size, size);
1177 switch (get_irn_opcode(irn)) {
1179 const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
1180 fprintf(f, "align: %d\n", a->align);
1181 if (a->offset == BE_STACK_FRAME_SIZE_EXPAND)
1182 fprintf(f, "offset: FRAME_SIZE\n");
1183 else if (a->offset == BE_STACK_FRAME_SIZE_SHRINK)
1184 fprintf(f, "offset: -FRAME SIZE\n");
1186 fprintf(f, "offset: %d\n", a->offset);
1190 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(irn);
1193 fprintf(f, "\ncalling: %s\n", get_entity_name(a->ent));
1198 for (i = 0; i < be_get_MemPerm_entity_arity(irn); ++i) {
1199 ir_entity *in, *out;
1200 in = be_get_MemPerm_in_entity(irn, i);
1201 out = be_get_MemPerm_out_entity(irn, i);
1203 fprintf(f, "\nin[%d]: %s\n", i, get_entity_name(in));
1206 fprintf(f, "\nout[%d]: %s\n", i, get_entity_name(out));
1220 * Copies the backend specific attributes from old node to new node.
1222 static void copy_attr(ir_graph *irg, const ir_node *old_node, ir_node *new_node)
1224 const void *old_attr = get_irn_generic_attr_const(old_node);
1225 void *new_attr = get_irn_generic_attr(new_node);
1226 struct obstack *obst = be_get_be_obst(irg);
1227 backend_info_t *old_info = be_get_info(old_node);
1228 backend_info_t *new_info = be_get_info(new_node);
1230 assert(is_be_node(old_node));
1231 assert(is_be_node(new_node));
1233 memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
1235 new_info->flags = old_info->flags;
1236 if (old_info->out_infos != NULL) {
1237 size_t n_outs = ARR_LEN(old_info->out_infos);
1238 /* need dyanmic out infos? */
1239 if (be_is_Perm(new_node)) {
1240 new_info->out_infos = NEW_ARR_F(reg_out_info_t, n_outs);
1242 new_info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_outs);
1244 memcpy(new_info->out_infos, old_info->out_infos,
1245 n_outs * sizeof(new_info->out_infos[0]));
1247 new_info->out_infos = NULL;
1251 if (old_info->in_reqs != NULL) {
1252 unsigned n_ins = get_irn_arity(old_node);
1253 /* need dynamic in infos? */
1254 if (get_irn_op(old_node)->opar == oparity_dynamic) {
1255 new_info->in_reqs = NEW_ARR_F(const arch_register_req_t*, n_ins);
1257 new_info->in_reqs = OALLOCN(obst,const arch_register_req_t*, n_ins);
1259 memcpy(new_info->in_reqs, old_info->in_reqs,
1260 n_ins * sizeof(new_info->in_reqs[0]));
1262 new_info->in_reqs = NULL;
1266 static const ir_op_ops be_node_op_ops = {
1286 int is_be_node(const ir_node *irn)
1288 return get_op_ops(get_irn_op(irn))->be_ops == &be_node_irn_ops;
1291 void be_init_op(void)
1295 /* Acquire all needed opcodes. */
1296 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);
1297 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);
1298 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);
1299 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);
1300 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);
1301 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);
1302 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);
1303 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);
1304 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);
1305 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);
1306 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);
1307 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);
1308 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);
1309 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);
1311 op_be_Spill->ops.node_cmp_attr = FrameAddr_cmp_attr;
1312 op_be_Reload->ops.node_cmp_attr = FrameAddr_cmp_attr;
1313 op_be_Perm->ops.node_cmp_attr = be_nodes_equal;
1314 op_be_MemPerm->ops.node_cmp_attr = be_nodes_equal;
1315 op_be_Copy->ops.node_cmp_attr = be_nodes_equal;
1316 op_be_Keep->ops.node_cmp_attr = be_nodes_equal;
1317 op_be_CopyKeep->ops.node_cmp_attr = be_nodes_equal;
1318 op_be_Call->ops.node_cmp_attr = Call_cmp_attr;
1319 op_be_Return->ops.node_cmp_attr = Return_cmp_attr;
1320 op_be_AddSP->ops.node_cmp_attr = be_nodes_equal;
1321 op_be_SubSP->ops.node_cmp_attr = be_nodes_equal;
1322 op_be_IncSP->ops.node_cmp_attr = IncSP_cmp_attr;
1323 op_be_Start->ops.node_cmp_attr = be_nodes_equal;
1324 op_be_FrameAddr->ops.node_cmp_attr = FrameAddr_cmp_attr;
1326 /* attach out dummy_ops to middle end nodes */
1327 for (opc = iro_First; opc <= iro_Last; ++opc) {
1328 ir_op *op = get_irp_opcode(opc);
1329 assert(op->ops.be_ops == NULL);
1330 op->ops.be_ops = &dummy_be_irn_ops;
1333 op_Phi->ops.be_ops = &phi_irn_ops;