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;
111 * Compare the attributes of two be_FrameAddr nodes.
113 * @return zero if both nodes have identically attributes
115 static int FrameAddr_cmp_attr(ir_node *a, ir_node *b)
117 const be_frame_attr_t *a_attr = (const be_frame_attr_t*)get_irn_generic_attr_const(a);
118 const be_frame_attr_t *b_attr = (const be_frame_attr_t*)get_irn_generic_attr_const(b);
120 if (a_attr->ent != b_attr->ent || a_attr->offset != b_attr->offset)
123 return be_nodes_equal(a, b);
127 * Compare the attributes of two be_Return nodes.
129 * @return zero if both nodes have identically attributes
131 static int Return_cmp_attr(ir_node *a, ir_node *b)
133 const be_return_attr_t *a_attr = (const be_return_attr_t*)get_irn_generic_attr_const(a);
134 const be_return_attr_t *b_attr = (const be_return_attr_t*)get_irn_generic_attr_const(b);
136 if (a_attr->num_ret_vals != b_attr->num_ret_vals)
138 if (a_attr->pop != b_attr->pop)
140 if (a_attr->emit_pop != b_attr->emit_pop)
143 return be_nodes_equal(a, b);
147 * Compare the attributes of two be_IncSP nodes.
149 * @return zero if both nodes have identically attributes
151 static int IncSP_cmp_attr(ir_node *a, ir_node *b)
153 const be_incsp_attr_t *a_attr = (const be_incsp_attr_t*)get_irn_generic_attr_const(a);
154 const be_incsp_attr_t *b_attr = (const be_incsp_attr_t*)get_irn_generic_attr_const(b);
156 if (a_attr->offset != b_attr->offset)
159 return be_nodes_equal(a, b);
163 * Compare the attributes of two be_Call nodes.
165 * @return zero if both nodes have identically attributes
167 static int Call_cmp_attr(ir_node *a, ir_node *b)
169 const be_call_attr_t *a_attr = (const be_call_attr_t*)get_irn_generic_attr_const(a);
170 const be_call_attr_t *b_attr = (const be_call_attr_t*)get_irn_generic_attr_const(b);
172 if (a_attr->ent != b_attr->ent ||
173 a_attr->call_tp != b_attr->call_tp)
176 return be_nodes_equal(a, b);
179 static arch_register_req_t *allocate_reg_req(const ir_node *node)
181 ir_graph *irg = get_irn_irg(node);
182 struct obstack *obst = be_get_be_obst(irg);
184 arch_register_req_t *req = OALLOCZ(obst, arch_register_req_t);
188 void be_set_constr_in(ir_node *node, int pos, const arch_register_req_t *req)
190 backend_info_t *info = be_get_info(node);
191 assert(pos < get_irn_arity(node));
192 info->in_reqs[pos] = req;
195 void be_set_constr_out(ir_node *node, int pos, const arch_register_req_t *req)
197 backend_info_t *info = be_get_info(node);
198 info->out_infos[pos].req = req;
202 * Initializes the generic attribute of all be nodes and return it.
204 static void init_node_attr(ir_node *node, int n_inputs, int n_outputs)
206 ir_graph *irg = get_irn_irg(node);
207 struct obstack *obst = be_get_be_obst(irg);
208 backend_info_t *info = be_get_info(node);
209 const arch_register_req_t **in_reqs;
213 assert(n_inputs == get_irn_arity(node));
214 in_reqs = OALLOCN(obst, const arch_register_req_t*, n_inputs);
215 for (i = 0; i < n_inputs; ++i) {
216 in_reqs[i] = arch_no_register_req;
219 in_reqs = NEW_ARR_F(const arch_register_req_t*, 0);
221 info->in_reqs = in_reqs;
223 if (n_outputs >= 0) {
225 info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_outputs);
226 memset(info->out_infos, 0, n_outputs * sizeof(info->out_infos[0]));
227 for (i = 0; i < n_outputs; ++i) {
228 info->out_infos[i].req = arch_no_register_req;
231 info->out_infos = NEW_ARR_F(reg_out_info_t, 0);
235 static void add_register_req_out(ir_node *node)
237 backend_info_t *info = be_get_info(node);
238 reg_out_info_t out_info;
239 memset(&out_info, 0, sizeof(out_info));
240 out_info.req = arch_no_register_req;
241 ARR_APP1(reg_out_info_t, info->out_infos, out_info);
244 static void add_register_req_in(ir_node *node)
246 backend_info_t *info = be_get_info(node);
247 ARR_APP1(const arch_register_req_t*, info->in_reqs, arch_no_register_req);
250 ir_node *be_new_Spill(const arch_register_class_t *cls,
251 const arch_register_class_t *cls_frame, ir_node *bl,
252 ir_node *frame, ir_node *to_spill)
257 ir_graph *irg = get_Block_irg(bl);
261 res = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
262 init_node_attr(res, 2, 1);
263 a = (be_frame_attr_t*) get_irn_generic_attr(res);
267 be_node_set_reg_class_in(res, be_pos_Spill_frame, cls_frame);
268 be_node_set_reg_class_in(res, be_pos_Spill_val, cls);
270 * For spills and reloads, we return "none" as requirement for frame
271 * pointer, so every input is ok. Some backends need this (STA).
272 * Matze: we should investigate if this is really needed, this solution
273 * looks very hacky to me
275 be_set_constr_in(res, be_pos_Spill_frame, arch_no_register_req);
277 arch_set_out_register_req(res, 0, arch_no_register_req);
282 ir_node *be_new_Reload(const arch_register_class_t *cls,
283 const arch_register_class_t *cls_frame, ir_node *block,
284 ir_node *frame, ir_node *mem, ir_mode *mode)
288 ir_graph *irg = get_Block_irg(block);
292 res = new_ir_node(NULL, irg, block, op_be_Reload, mode, 2, in);
294 init_node_attr(res, 2, 1);
295 be_node_set_reg_class_out(res, 0, cls);
297 be_node_set_reg_class_in(res, be_pos_Reload_frame, cls_frame);
298 arch_irn_set_flags(res, arch_irn_flags_rematerializable);
301 * For spills and reloads, we return "none" as requirement for frame
302 * pointer, so every input is ok. Some backends need this (e.g. STA).
303 * Matze: we should investigate if this is really needed, this solution
304 * looks very hacky to me
306 be_set_constr_in(res, be_pos_Reload_frame, arch_no_register_req);
311 ir_node *be_get_Reload_mem(const ir_node *irn)
313 assert(be_is_Reload(irn));
314 return get_irn_n(irn, be_pos_Reload_mem);
317 ir_node *be_get_Reload_frame(const ir_node *irn)
319 assert(be_is_Reload(irn));
320 return get_irn_n(irn, be_pos_Reload_frame);
323 ir_node *be_get_Spill_val(const ir_node *irn)
325 assert(be_is_Spill(irn));
326 return get_irn_n(irn, be_pos_Spill_val);
329 ir_node *be_get_Spill_frame(const ir_node *irn)
331 assert(be_is_Spill(irn));
332 return get_irn_n(irn, be_pos_Spill_frame);
335 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_node *block,
336 int n, ir_node *in[])
339 ir_graph *irg = get_Block_irg(block);
341 ir_node *irn = new_ir_node(NULL, irg, block, op_be_Perm, mode_T, n, in);
342 init_node_attr(irn, n, n);
343 for (i = 0; i < n; ++i) {
344 be_node_set_reg_class_in(irn, i, cls);
345 be_node_set_reg_class_out(irn, i, cls);
351 void be_Perm_reduce(ir_node *perm, int new_size, int *map)
353 int arity = get_irn_arity(perm);
354 const arch_register_req_t **old_in_reqs
355 = ALLOCAN(const arch_register_req_t*, arity);
356 reg_out_info_t *old_infos = ALLOCAN(reg_out_info_t, arity);
357 backend_info_t *info = be_get_info(perm);
361 assert(be_is_Perm(perm));
362 assert(new_size <= arity);
364 new_in = ALLOCAN(ir_node*, new_size);
366 /* save the old register data */
367 memcpy(old_in_reqs, info->in_reqs, arity * sizeof(old_in_reqs[0]));
368 memcpy(old_infos, info->out_infos, arity * sizeof(old_infos[0]));
370 /* compose the new in array and set the new register data directly */
371 for (i = 0; i < new_size; ++i) {
373 new_in[i] = get_irn_n(perm, idx);
374 info->in_reqs[i] = old_in_reqs[idx];
375 info->out_infos[i] = old_infos[idx];
378 set_irn_in(perm, new_size, new_in);
381 ir_node *be_new_MemPerm(ir_node *block, int n, ir_node *in[])
383 ir_graph *irg = get_Block_irg(block);
384 const arch_env_t *arch_env = be_get_irg_arch_env(irg);
385 ir_node *frame = get_irg_frame(irg);
386 const arch_register_t *sp = arch_env->sp;
388 be_memperm_attr_t *attr;
391 real_in = ALLOCAN(ir_node*, n + 1);
393 memcpy(&real_in[1], in, n * sizeof(real_in[0]));
395 irn = new_ir_node(NULL, irg, block, op_be_MemPerm, mode_T, n+1, real_in);
397 init_node_attr(irn, n + 1, n);
398 be_node_set_reg_class_in(irn, 0, sp->reg_class);
400 attr = (be_memperm_attr_t*)get_irn_generic_attr(irn);
401 attr->in_entities = OALLOCNZ(irg->obst, ir_entity*, n);
402 attr->out_entities = OALLOCNZ(irg->obst, ir_entity*, n);
407 ir_node *be_new_Copy(const arch_register_class_t *cls, ir_node *bl, ir_node *op)
411 arch_register_req_t *req;
412 ir_graph *irg = get_Block_irg(bl);
415 res = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
416 init_node_attr(res, 1, 1);
417 be_node_set_reg_class_in(res, 0, cls);
418 be_node_set_reg_class_out(res, 0, cls);
420 req = allocate_reg_req(res);
422 req->type = arch_register_req_type_should_be_same;
423 req->other_same = 1U << 0;
425 be_set_constr_out(res, 0, req);
430 ir_node *be_get_Copy_op(const ir_node *cpy)
432 return get_irn_n(cpy, be_pos_Copy_op);
435 void be_set_Copy_op(ir_node *cpy, ir_node *op)
437 set_irn_n(cpy, be_pos_Copy_op, op);
440 ir_node *be_new_Keep(ir_node *block, int n, ir_node *in[])
444 ir_graph *irg = get_Block_irg(block);
446 res = new_ir_node(NULL, irg, block, op_be_Keep, mode_ANY, -1, NULL);
447 init_node_attr(res, -1, 1);
449 for (i = 0; i < n; ++i) {
450 add_irn_n(res, in[i]);
451 add_register_req_in(res);
458 void be_Keep_add_node(ir_node *keep, const arch_register_class_t *cls, ir_node *node)
462 assert(be_is_Keep(keep));
463 n = add_irn_n(keep, node);
464 add_register_req_in(keep);
465 be_node_set_reg_class_in(keep, n, cls);
468 ir_node *be_new_Call(dbg_info *dbg, ir_graph *irg, ir_node *bl, ir_node *mem,
469 ir_node *sp, ir_node *ptr, int n_outs, int n, ir_node *in[],
473 int real_n = be_pos_Call_first_arg + n;
477 NEW_ARR_A(ir_node *, real_in, real_n);
478 real_in[be_pos_Call_mem] = mem;
479 real_in[be_pos_Call_sp] = sp;
480 real_in[be_pos_Call_ptr] = ptr;
481 memcpy(&real_in[be_pos_Call_first_arg], in, n * sizeof(in[0]));
483 irn = new_ir_node(dbg, irg, bl, op_be_Call, mode_T, real_n, real_in);
484 init_node_attr(irn, real_n, n_outs);
485 a = (be_call_attr_t*)get_irn_generic_attr(irn);
487 a->call_tp = call_tp;
492 ir_entity *be_Call_get_entity(const ir_node *call)
494 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
495 assert(be_is_Call(call));
499 void be_Call_set_entity(ir_node *call, ir_entity *ent)
501 be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
502 assert(be_is_Call(call));
506 ir_type *be_Call_get_type(ir_node *call)
508 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
509 assert(be_is_Call(call));
513 void be_Call_set_type(ir_node *call, ir_type *call_tp)
515 be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
516 assert(be_is_Call(call));
517 a->call_tp = call_tp;
520 void be_Call_set_pop(ir_node *call, unsigned pop)
522 be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
526 unsigned be_Call_get_pop(const ir_node *call)
528 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
532 ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *block, int n_res,
533 unsigned pop, int n, ir_node *in[])
539 res = new_ir_node(dbg, irg, block, op_be_Return, mode_X, -1, NULL);
540 init_node_attr(res, -1, 1);
541 for (i = 0; i < n; ++i) {
542 add_irn_n(res, in[i]);
543 add_register_req_in(res);
545 be_set_constr_out(res, 0, arch_no_register_req);
547 a = (be_return_attr_t*)get_irn_generic_attr(res);
548 a->num_ret_vals = n_res;
555 int be_Return_get_n_rets(const ir_node *ret)
557 const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
558 return a->num_ret_vals;
561 unsigned be_Return_get_pop(const ir_node *ret)
563 const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
567 int be_Return_get_emit_pop(const ir_node *ret)
569 const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
573 void be_Return_set_emit_pop(ir_node *ret, int emit_pop)
575 be_return_attr_t *a = (be_return_attr_t*)get_irn_generic_attr(ret);
576 a->emit_pop = emit_pop;
579 int be_Return_append_node(ir_node *ret, ir_node *node)
583 pos = add_irn_n(ret, node);
584 add_register_req_in(ret);
589 ir_node *be_new_IncSP(const arch_register_t *sp, ir_node *bl,
590 ir_node *old_sp, int offset, int align)
595 ir_graph *irg = get_Block_irg(bl);
598 irn = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode,
599 sizeof(in) / sizeof(in[0]), in);
600 init_node_attr(irn, 1, 1);
601 a = (be_incsp_attr_t*)get_irn_generic_attr(irn);
605 /* Set output constraint to stack register. */
606 be_node_set_reg_class_in(irn, 0, sp->reg_class);
607 be_set_constr_single_reg_out(irn, 0, sp, arch_register_req_type_produces_sp);
612 ir_node *be_new_AddSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp,
616 ir_node *in[be_pos_AddSP_last];
617 const arch_register_class_t *cls;
620 in[be_pos_AddSP_old_sp] = old_sp;
621 in[be_pos_AddSP_size] = sz;
623 irg = get_Block_irg(bl);
624 irn = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T, be_pos_AddSP_last, in);
625 init_node_attr(irn, be_pos_AddSP_last, pn_be_AddSP_last);
627 /* Set output constraint to stack register. */
628 be_set_constr_single_reg_in(irn, be_pos_AddSP_old_sp, sp,
629 arch_register_req_type_none);
630 be_node_set_reg_class_in(irn, be_pos_AddSP_size, arch_register_get_class(sp));
631 be_set_constr_single_reg_out(irn, pn_be_AddSP_sp, sp,
632 arch_register_req_type_produces_sp);
634 cls = arch_register_get_class(sp);
639 ir_node *be_new_SubSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp, ir_node *sz)
642 ir_node *in[be_pos_SubSP_last];
645 in[be_pos_SubSP_old_sp] = old_sp;
646 in[be_pos_SubSP_size] = sz;
648 irg = get_Block_irg(bl);
649 irn = new_ir_node(NULL, irg, bl, op_be_SubSP, mode_T, be_pos_SubSP_last, in);
650 init_node_attr(irn, be_pos_SubSP_last, pn_be_SubSP_last);
652 /* Set output constraint to stack register. */
653 be_set_constr_single_reg_in(irn, be_pos_SubSP_old_sp, sp,
654 arch_register_req_type_none);
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 init_node_attr(irn, 1, 1);
686 a = (be_frame_attr_t*)get_irn_generic_attr(irn);
689 be_node_set_reg_class_in(irn, 0, cls_frame);
690 be_node_set_reg_class_out(irn, 0, cls_frame);
692 return optimize_node(irn);
695 ir_node *be_get_FrameAddr_frame(const ir_node *node)
697 assert(be_is_FrameAddr(node));
698 return get_irn_n(node, be_pos_FrameAddr_ptr);
701 ir_entity *be_get_FrameAddr_entity(const ir_node *node)
703 const be_frame_attr_t *attr = (const be_frame_attr_t*)get_irn_generic_attr_const(node);
707 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)
710 ir_node **in = ALLOCAN(ir_node*, n + 1);
711 ir_graph *irg = get_Block_irg(bl);
714 memcpy(&in[1], in_keep, n * sizeof(in[0]));
715 irn = new_ir_node(NULL, irg, bl, op_be_CopyKeep, mode, n + 1, in);
716 init_node_attr(irn, n + 1, 1);
717 be_node_set_reg_class_in(irn, 0, cls);
718 be_node_set_reg_class_out(irn, 0, cls);
723 ir_node *be_new_CopyKeep_single(const arch_register_class_t *cls, ir_node *bl, ir_node *src, ir_node *keep, ir_mode *mode)
725 return be_new_CopyKeep(cls, bl, src, 1, &keep, mode);
728 ir_node *be_get_CopyKeep_op(const ir_node *cpy)
730 return get_irn_n(cpy, be_pos_CopyKeep_op);
733 void be_set_CopyKeep_op(ir_node *cpy, ir_node *op)
735 set_irn_n(cpy, be_pos_CopyKeep_op, op);
738 ir_node *be_new_Barrier(ir_node *bl, int n, ir_node *in[])
742 ir_graph *irg = get_Block_irg(bl);
744 res = new_ir_node(NULL, irg, bl, op_be_Barrier, mode_T, -1, NULL);
745 init_node_attr(res, -1, -1);
746 for (i = 0; i < n; ++i) {
747 add_irn_n(res, in[i]);
748 add_register_req_in(res);
749 add_register_req_out(res);
755 ir_node *be_Barrier_append_node(ir_node *barrier, ir_node *node)
757 ir_mode *mode = get_irn_mode(node);
758 int n = add_irn_n(barrier, node);
760 ir_node *proj = new_r_Proj(barrier, mode, n);
761 add_register_req_in(barrier);
762 add_register_req_out(barrier);
767 static bool be_has_frame_entity(const ir_node *irn)
769 switch (get_irn_opcode(irn)) {
779 ir_entity *be_get_frame_entity(const ir_node *irn)
781 if (be_has_frame_entity(irn)) {
782 const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
788 int be_get_frame_offset(const ir_node *irn)
790 assert(is_be_node(irn));
791 if (be_has_frame_entity(irn)) {
792 const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
798 void be_set_MemPerm_in_entity(const ir_node *irn, int n, ir_entity *ent)
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 attr->in_entities[n] = ent;
808 ir_entity* be_get_MemPerm_in_entity(const ir_node* irn, int n)
810 const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
812 assert(be_is_MemPerm(irn));
813 assert(n < be_get_MemPerm_entity_arity(irn));
815 return attr->in_entities[n];
818 void be_set_MemPerm_out_entity(const ir_node *irn, int n, ir_entity *ent)
820 const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
822 assert(be_is_MemPerm(irn));
823 assert(n < be_get_MemPerm_entity_arity(irn));
825 attr->out_entities[n] = ent;
828 ir_entity* be_get_MemPerm_out_entity(const ir_node* irn, int n)
830 const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
832 assert(be_is_MemPerm(irn));
833 assert(n < be_get_MemPerm_entity_arity(irn));
835 return attr->out_entities[n];
838 int be_get_MemPerm_entity_arity(const ir_node *irn)
840 return get_irn_arity(irn) - 1;
843 const arch_register_req_t *be_create_reg_req(struct obstack *obst,
844 const arch_register_t *reg, arch_register_req_type_t additional_types)
846 arch_register_req_t *req = OALLOC(obst, arch_register_req_t);
847 const arch_register_class_t *cls = arch_register_get_class(reg);
848 unsigned *limited_bitset;
850 limited_bitset = rbitset_obstack_alloc(obst, arch_register_class_n_regs(cls));
851 rbitset_set(limited_bitset, arch_register_get_index(reg));
853 req->type = arch_register_req_type_limited | additional_types;
855 req->limited = limited_bitset;
860 void be_set_constr_single_reg_in(ir_node *node, int pos,
861 const arch_register_t *reg, arch_register_req_type_t additional_types)
863 const arch_register_req_t *req;
865 if (additional_types == 0) {
866 req = reg->single_req;
868 ir_graph *irg = get_irn_irg(node);
869 struct obstack *obst = be_get_be_obst(irg);
870 req = be_create_reg_req(obst, reg, additional_types);
872 be_set_constr_in(node, pos, req);
875 void be_set_constr_single_reg_out(ir_node *node, int pos,
876 const arch_register_t *reg, arch_register_req_type_t additional_types)
878 ir_graph *irg = get_irn_irg(node);
879 be_irg_t *birg = be_birg_from_irg(irg);
880 const arch_register_req_t *req;
882 /* if we have an ignore register, add ignore flag and just assign it */
883 if (!rbitset_is_set(birg->allocatable_regs, reg->global_index)) {
884 additional_types |= arch_register_req_type_ignore;
887 if (additional_types == 0) {
888 req = reg->single_req;
890 ir_graph *irg = get_irn_irg(node);
891 struct obstack *obst = be_get_be_obst(irg);
892 req = be_create_reg_req(obst, reg, additional_types);
895 arch_irn_set_register(node, pos, reg);
896 be_set_constr_out(node, pos, req);
899 void be_node_set_reg_class_in(ir_node *irn, int pos,
900 const arch_register_class_t *cls)
902 be_set_constr_in(irn, pos, cls->class_req);
905 void be_node_set_reg_class_out(ir_node *irn, int pos,
906 const arch_register_class_t *cls)
908 be_set_constr_out(irn, pos, cls->class_req);
911 ir_node *be_get_IncSP_pred(ir_node *irn)
913 assert(be_is_IncSP(irn));
914 return get_irn_n(irn, 0);
917 void be_set_IncSP_pred(ir_node *incsp, ir_node *pred)
919 assert(be_is_IncSP(incsp));
920 set_irn_n(incsp, 0, pred);
923 void be_set_IncSP_offset(ir_node *irn, int offset)
925 be_incsp_attr_t *a = (be_incsp_attr_t*)get_irn_generic_attr(irn);
926 assert(be_is_IncSP(irn));
930 int be_get_IncSP_offset(const ir_node *irn)
932 const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
933 assert(be_is_IncSP(irn));
937 int be_get_IncSP_align(const ir_node *irn)
939 const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
940 assert(be_is_IncSP(irn));
944 ir_node *be_spill(ir_node *block, ir_node *irn)
946 ir_graph *irg = get_Block_irg(block);
947 ir_node *frame = get_irg_frame(irg);
948 const arch_register_class_t *cls = arch_get_irn_reg_class_out(irn);
949 const arch_register_class_t *cls_frame = arch_get_irn_reg_class_out(frame);
952 spill = be_new_Spill(cls, cls_frame, block, frame, irn);
956 ir_node *be_reload(const arch_register_class_t *cls, ir_node *insert, ir_mode *mode, ir_node *spill)
959 ir_node *bl = is_Block(insert) ? insert : get_nodes_block(insert);
960 ir_graph *irg = get_Block_irg(bl);
961 ir_node *frame = get_irg_frame(irg);
962 const arch_register_class_t *cls_frame = arch_get_irn_reg_class_out(frame);
964 assert(be_is_Spill(spill) || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
966 reload = be_new_Reload(cls, cls_frame, bl, frame, spill, mode);
968 if (is_Block(insert)) {
969 insert = sched_skip(insert, 0, sched_skip_cf_predicator, NULL);
970 sched_add_after(insert, reload);
972 sched_add_before(insert, reload);
979 static arch_irn_class_t be_node_classify(const ir_node *irn)
981 switch (get_irn_opcode(irn)) {
982 case beo_Spill: return arch_irn_class_spill;
983 case beo_Reload: return arch_irn_class_reload;
984 case beo_Perm: return arch_irn_class_perm;
985 case beo_Copy: return arch_irn_class_copy;
986 default: return arch_irn_class_none;
990 static ir_entity *be_node_get_frame_entity(const ir_node *irn)
992 return be_get_frame_entity(irn);
995 void be_node_set_frame_entity(ir_node *irn, ir_entity *ent)
999 assert(be_has_frame_entity(irn));
1001 a = (be_frame_attr_t*)get_irn_generic_attr(irn);
1005 static void be_node_set_frame_offset(ir_node *irn, int offset)
1009 if (!be_has_frame_entity(irn))
1012 a = (be_frame_attr_t*)get_irn_generic_attr(irn);
1016 static int be_node_get_sp_bias(const ir_node *irn)
1018 if (be_is_IncSP(irn))
1019 return be_get_IncSP_offset(irn);
1020 if (be_is_Call(irn))
1021 return -(int)be_Call_get_pop(irn);
1029 static const arch_irn_ops_t be_node_irn_ops = {
1031 be_node_get_frame_entity,
1032 be_node_set_frame_offset,
1033 be_node_get_sp_bias,
1034 NULL, /* get_inverse */
1035 NULL, /* get_op_estimated_cost */
1036 NULL, /* possible_memory_operand */
1037 NULL, /* perform_memory_operand */
1040 static arch_irn_class_t dummy_classify(const ir_node *node)
1043 return arch_irn_class_none;
1046 static ir_entity* dummy_get_frame_entity(const ir_node *node)
1052 static void dummy_set_frame_offset(ir_node *node, int bias)
1056 panic("dummy_set_frame_offset() should not be called");
1059 static int dummy_get_sp_bias(const ir_node *node)
1065 /* for "middleend" nodes */
1066 static const arch_irn_ops_t dummy_be_irn_ops = {
1068 dummy_get_frame_entity,
1069 dummy_set_frame_offset,
1071 NULL, /* get_inverse */
1072 NULL, /* get_op_estimated_cost */
1073 NULL, /* possible_memory_operand */
1074 NULL, /* perform_memory_operand */
1079 ir_node *be_new_Phi(ir_node *block, int n_ins, ir_node **ins, ir_mode *mode,
1080 const arch_register_class_t *cls)
1082 const arch_register_req_t *req;
1083 ir_graph *irg = get_irn_irg(block);
1084 struct obstack *obst = be_get_be_obst(irg);
1085 backend_info_t *info;
1088 ir_node *phi = new_r_Phi(block, n_ins, ins, mode);
1089 info = be_get_info(phi);
1090 info->out_infos = NEW_ARR_D(reg_out_info_t, obst, 1);
1091 memset(info->out_infos, 0, 1 * sizeof(info->out_infos[0]));
1092 info->in_reqs = OALLOCN(obst, const arch_register_req_t*, n_ins);
1095 req = arch_no_register_req;
1097 req = cls->class_req;
1099 info->out_infos[0].req = req;
1100 for (i = 0; i < n_ins; ++i) {
1101 info->in_reqs[i] = req;
1107 void be_set_phi_reg_req(ir_node *node, const arch_register_req_t *req)
1109 int arity = get_irn_arity(node);
1112 backend_info_t *info = be_get_info(node);
1113 info->out_infos[0].req = req;
1114 for (i = 0; i < arity; ++i) {
1115 info->in_reqs[i] = req;
1118 assert(mode_is_datab(get_irn_mode(node)));
1121 void be_dump_phi_reg_reqs(FILE *F, ir_node *node, dump_reason_t reason)
1124 case dump_node_opcode_txt:
1125 fputs(get_op_name(get_irn_op(node)), F);
1127 case dump_node_mode_txt:
1128 fprintf(F, "%s", get_mode_name(get_irn_mode(node)));
1130 case dump_node_nodeattr_txt:
1132 case dump_node_info_txt:
1134 backend_info_t *info = be_get_info(node);
1135 if (info != NULL && info->out_infos[0].req != NULL) {
1136 arch_dump_reqs_and_registers(F, node);
1146 static const arch_irn_ops_t phi_irn_ops = {
1148 dummy_get_frame_entity,
1149 dummy_set_frame_offset,
1151 NULL, /* get_inverse */
1152 NULL, /* get_op_estimated_cost */
1153 NULL, /* possible_memory_operand */
1154 NULL, /* perform_memory_operand */
1160 * ir_op-Operation: dump a be node to file
1162 static void dump_node(FILE *f, ir_node *irn, dump_reason_t reason)
1164 assert(is_be_node(irn));
1167 case dump_node_opcode_txt:
1168 fputs(get_op_name(get_irn_op(irn)), f);
1170 case dump_node_mode_txt:
1171 if (be_is_Copy(irn) || be_is_CopyKeep(irn)) {
1172 fprintf(f, "%s", get_mode_name(get_irn_mode(irn)));
1175 case dump_node_nodeattr_txt:
1176 if (be_is_Call(irn)) {
1177 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(irn);
1179 fprintf(f, " [%s] ", get_entity_name(a->ent));
1181 if (be_is_IncSP(irn)) {
1182 const be_incsp_attr_t *attr = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
1183 if (attr->offset == BE_STACK_FRAME_SIZE_EXPAND) {
1184 fprintf(f, " [Setup Stackframe] ");
1185 } else if (attr->offset == BE_STACK_FRAME_SIZE_SHRINK) {
1186 fprintf(f, " [Destroy Stackframe] ");
1188 fprintf(f, " [%d] ", attr->offset);
1192 case dump_node_info_txt:
1193 arch_dump_reqs_and_registers(f, irn);
1195 if (be_has_frame_entity(irn)) {
1196 const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
1198 unsigned size = get_type_size_bytes(get_entity_type(a->ent));
1199 ir_fprintf(f, "frame entity: %+F, offset 0x%x (%d), size 0x%x (%d) bytes\n",
1200 a->ent, a->offset, a->offset, size, size);
1205 switch (get_irn_opcode(irn)) {
1207 const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
1208 fprintf(f, "align: %d\n", a->align);
1209 if (a->offset == BE_STACK_FRAME_SIZE_EXPAND)
1210 fprintf(f, "offset: FRAME_SIZE\n");
1211 else if (a->offset == BE_STACK_FRAME_SIZE_SHRINK)
1212 fprintf(f, "offset: -FRAME SIZE\n");
1214 fprintf(f, "offset: %d\n", a->offset);
1218 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(irn);
1221 fprintf(f, "\ncalling: %s\n", get_entity_name(a->ent));
1226 for (i = 0; i < be_get_MemPerm_entity_arity(irn); ++i) {
1227 ir_entity *in, *out;
1228 in = be_get_MemPerm_in_entity(irn, i);
1229 out = be_get_MemPerm_out_entity(irn, i);
1231 fprintf(f, "\nin[%d]: %s\n", i, get_entity_name(in));
1234 fprintf(f, "\nout[%d]: %s\n", i, get_entity_name(out));
1248 * Copies the backend specific attributes from old node to new node.
1250 static void copy_attr(ir_graph *irg, const ir_node *old_node, ir_node *new_node)
1252 const void *old_attr = get_irn_generic_attr_const(old_node);
1253 void *new_attr = get_irn_generic_attr(new_node);
1254 struct obstack *obst = be_get_be_obst(irg);
1255 backend_info_t *old_info = be_get_info(old_node);
1256 backend_info_t *new_info = be_get_info(new_node);
1258 assert(is_be_node(old_node));
1259 assert(is_be_node(new_node));
1261 memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
1263 if (old_info->out_infos != NULL) {
1264 unsigned n_outs = ARR_LEN(old_info->out_infos);
1265 /* need dyanmic out infos? */
1266 if (be_is_Barrier(new_node) || be_is_Perm(new_node)) {
1267 new_info->out_infos = NEW_ARR_F(reg_out_info_t, n_outs);
1269 new_info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_outs);
1271 memcpy(new_info->out_infos, old_info->out_infos,
1272 n_outs * sizeof(new_info->out_infos[0]));
1274 new_info->out_infos = NULL;
1278 if (old_info->in_reqs != NULL) {
1279 unsigned n_ins = get_irn_arity(old_node);
1280 /* need dynamic in infos? */
1281 if (get_irn_op(old_node)->opar == oparity_dynamic) {
1282 new_info->in_reqs = NEW_ARR_F(const arch_register_req_t*, n_ins);
1284 new_info->in_reqs = OALLOCN(obst,const arch_register_req_t*, n_ins);
1286 memcpy(new_info->in_reqs, old_info->in_reqs,
1287 n_ins * sizeof(new_info->in_reqs[0]));
1289 new_info->in_reqs = NULL;
1293 static const ir_op_ops be_node_op_ops = {
1313 int is_be_node(const ir_node *irn)
1315 return get_op_ops(get_irn_op(irn))->be_ops == &be_node_irn_ops;
1318 void be_init_op(void)
1322 /* Acquire all needed opcodes. */
1323 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);
1324 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);
1325 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);
1326 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);
1327 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);
1328 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);
1329 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);
1330 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);
1331 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);
1332 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);
1333 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);
1334 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);
1335 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);
1336 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);
1337 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);
1339 op_be_Spill->ops.node_cmp_attr = FrameAddr_cmp_attr;
1340 op_be_Reload->ops.node_cmp_attr = FrameAddr_cmp_attr;
1341 op_be_Perm->ops.node_cmp_attr = be_nodes_equal;
1342 op_be_MemPerm->ops.node_cmp_attr = be_nodes_equal;
1343 op_be_Copy->ops.node_cmp_attr = be_nodes_equal;
1344 op_be_Keep->ops.node_cmp_attr = be_nodes_equal;
1345 op_be_CopyKeep->ops.node_cmp_attr = be_nodes_equal;
1346 op_be_Call->ops.node_cmp_attr = Call_cmp_attr;
1347 op_be_Return->ops.node_cmp_attr = Return_cmp_attr;
1348 op_be_AddSP->ops.node_cmp_attr = be_nodes_equal;
1349 op_be_SubSP->ops.node_cmp_attr = be_nodes_equal;
1350 op_be_IncSP->ops.node_cmp_attr = IncSP_cmp_attr;
1351 op_be_Start->ops.node_cmp_attr = be_nodes_equal;
1352 op_be_FrameAddr->ops.node_cmp_attr = FrameAddr_cmp_attr;
1353 op_be_Barrier->ops.node_cmp_attr = be_nodes_equal;
1355 /* attach out dummy_ops to middle end nodes */
1356 for (opc = iro_First; opc <= iro_Last; ++opc) {
1357 ir_op *op = get_irp_opcode(opc);
1358 assert(op->ops.be_ops == NULL);
1359 op->ops.be_ops = &dummy_be_irn_ops;
1362 op_Phi->ops.be_ops = &phi_irn_ops;