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.
41 #include "bitfiddle.h"
42 #include "raw_bitset.h"
62 #define get_irn_attr(irn) get_irn_generic_attr(irn)
63 #define get_irn_attr_const(irn) get_irn_generic_attr_const(irn)
66 const arch_register_req_t *in_req;
69 /** The generic be nodes attribute type. */
71 be_reg_data_t *reg_data;
74 /** The be_Return nodes attribute type. */
76 be_node_attr_t node_attr; /**< base attributes of every be node. */
77 int num_ret_vals; /**< number of return values */
78 unsigned pop; /**< number of bytes that should be popped */
79 int emit_pop; /**< if set, emit pop bytes, even if pop = 0 */
82 /** The be_IncSP attribute type. */
84 be_node_attr_t node_attr; /**< base attributes of every be node. */
85 int offset; /**< The offset by which the stack shall be expanded/shrinked. */
86 int align; /**< whether stack should be aligned after the
90 /** The be_Frame attribute type. */
92 be_node_attr_t node_attr; /**< base attributes of every be node. */
97 /** The be_Call attribute type. */
99 be_node_attr_t node_attr; /**< base attributes of every be node. */
100 ir_entity *ent; /**< The called entity if this is a static call. */
102 ir_type *call_tp; /**< The call type, copied from the original Call node. */
106 be_node_attr_t node_attr; /**< base attributes of every be node. */
107 ir_entity **in_entities;
108 ir_entity **out_entities;
114 ir_op *op_be_MemPerm;
117 ir_op *op_be_CopyKeep;
124 ir_op *op_be_FrameAddr;
125 ir_op *op_be_Barrier;
127 static const ir_op_ops be_node_op_ops;
129 #define N irop_flag_none
130 #define L irop_flag_labeled
131 #define C irop_flag_commutative
132 #define X irop_flag_cfopcode
133 #define I irop_flag_ip_cfopcode
134 #define F irop_flag_fragile
135 #define Y irop_flag_forking
136 #define H irop_flag_highlevel
137 #define c irop_flag_constlike
138 #define K irop_flag_keep
139 #define M irop_flag_uses_memory
142 * Compare two be node attributes.
144 * @return zero if both attributes are identically
146 static int node_cmp_attr(ir_node *a, ir_node *b)
148 const be_node_attr_t *a_attr = get_irn_attr_const(a);
149 const be_node_attr_t *b_attr = get_irn_attr_const(b);
150 int i, len = ARR_LEN(a_attr->reg_data);
152 if (len != ARR_LEN(b_attr->reg_data))
155 if (!be_nodes_equal(a, b))
158 for (i = len - 1; i >= 0; --i) {
159 if (!reg_reqs_equal(a_attr->reg_data[i].in_req,
160 b_attr->reg_data[i].in_req))
168 * Compare the attributes of two be_FrameAddr nodes.
170 * @return zero if both nodes have identically attributes
172 static int FrameAddr_cmp_attr(ir_node *a, ir_node *b)
174 const be_frame_attr_t *a_attr = get_irn_attr_const(a);
175 const be_frame_attr_t *b_attr = get_irn_attr_const(b);
177 if (a_attr->ent != b_attr->ent || a_attr->offset != b_attr->offset)
180 return node_cmp_attr(a, b);
184 * Compare the attributes of two be_Return nodes.
186 * @return zero if both nodes have identically attributes
188 static int Return_cmp_attr(ir_node *a, ir_node *b)
190 const be_return_attr_t *a_attr = get_irn_attr_const(a);
191 const be_return_attr_t *b_attr = get_irn_attr_const(b);
193 if (a_attr->num_ret_vals != b_attr->num_ret_vals)
195 if (a_attr->pop != b_attr->pop)
197 if (a_attr->emit_pop != b_attr->emit_pop)
200 return node_cmp_attr(a, b);
204 * Compare the attributes of two be_IncSP nodes.
206 * @return zero if both nodes have identically attributes
208 static int IncSP_cmp_attr(ir_node *a, ir_node *b) {
209 const be_incsp_attr_t *a_attr = get_irn_attr_const(a);
210 const be_incsp_attr_t *b_attr = get_irn_attr_const(b);
212 if (a_attr->offset != b_attr->offset)
215 return node_cmp_attr(a, b);
219 * Compare the attributes of two be_Call nodes.
221 * @return zero if both nodes have identically attributes
223 static int Call_cmp_attr(ir_node *a, ir_node *b)
225 const be_call_attr_t *a_attr = get_irn_attr_const(a);
226 const be_call_attr_t *b_attr = get_irn_attr_const(b);
228 if (a_attr->ent != b_attr->ent ||
229 a_attr->call_tp != b_attr->call_tp)
232 return node_cmp_attr(a, b);
235 static arch_register_req_t *allocate_reg_req(const ir_node *node)
237 ir_graph *irg = get_irn_irg(node);
238 struct obstack *obst = be_get_birg_obst(irg);
240 arch_register_req_t *req = obstack_alloc(obst, sizeof(*req));
241 memset(req, 0, sizeof(*req));
245 void be_set_constr_in(ir_node *node, int pos, const arch_register_req_t *req)
247 const be_node_attr_t *attr = get_irn_attr_const(node);
248 be_reg_data_t *rd = &attr->reg_data[pos];
249 assert(pos < ARR_LEN(attr->reg_data));
253 void be_set_constr_out(ir_node *node, int pos, const arch_register_req_t *req)
255 backend_info_t *info = be_get_info(node);
256 info->out_infos[pos].req = req;
260 * Initializes the generic attribute of all be nodes and return it.
262 static void *init_node_attr(ir_node *node, int n_inputs, int n_outputs)
264 ir_graph *irg = get_irn_irg(node);
265 struct obstack *obst = be_get_birg_obst(irg);
266 be_node_attr_t *a = get_irn_attr(node);
267 backend_info_t *info = be_get_info(node);
269 memset(a, 0, sizeof(get_op_attr_size(get_irn_op(node))));
273 a->reg_data = NEW_ARR_D(be_reg_data_t, obst, n_inputs);
274 for (i = 0; i < n_inputs; ++i) {
275 a->reg_data[i].in_req = arch_no_register_req;
278 a->reg_data = NEW_ARR_F(be_reg_data_t, 0);
281 if (n_outputs >= 0) {
283 info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_outputs);
284 memset(info->out_infos, 0, n_outputs * sizeof(info->out_infos[0]));
285 for (i = 0; i < n_outputs; ++i) {
286 info->out_infos[i].req = arch_no_register_req;
289 info->out_infos = NEW_ARR_F(reg_out_info_t, 0);
295 static void add_register_req_out(ir_node *node)
297 backend_info_t *info = be_get_info(node);
298 reg_out_info_t out_info;
299 memset(&out_info, 0, sizeof(out_info));
300 out_info.req = arch_no_register_req;
301 ARR_APP1(reg_out_info_t, info->out_infos, out_info);
304 static void add_register_req_in(ir_node *node)
306 be_node_attr_t *a = get_irn_attr(node);
307 be_reg_data_t regreq;
308 memset(®req, 0, sizeof(regreq));
309 regreq.in_req = arch_no_register_req;
310 ARR_APP1(be_reg_data_t, a->reg_data, regreq);
313 ir_node *be_new_Spill(const arch_register_class_t *cls,
314 const arch_register_class_t *cls_frame, ir_node *bl,
315 ir_node *frame, ir_node *to_spill)
320 ir_graph *irg = get_Block_irg(bl);
324 res = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
325 a = init_node_attr(res, 2, 1);
329 be_node_set_reg_class_in(res, be_pos_Spill_frame, cls_frame);
330 be_node_set_reg_class_in(res, be_pos_Spill_val, cls);
332 * For spills and reloads, we return "none" as requirement for frame
333 * pointer, so every input is ok. Some backends need this (STA).
334 * Matze: we should investigate if this is really needed, this solution
335 * looks very hacky to me
337 be_set_constr_in(res, be_pos_Spill_frame, arch_no_register_req);
339 arch_set_out_register_req(res, 0, arch_no_register_req);
344 ir_node *be_new_Reload(const arch_register_class_t *cls,
345 const arch_register_class_t *cls_frame, ir_node *block,
346 ir_node *frame, ir_node *mem, ir_mode *mode)
350 ir_graph *irg = get_Block_irg(block);
354 res = new_ir_node(NULL, irg, block, op_be_Reload, mode, 2, in);
356 init_node_attr(res, 2, 1);
357 be_node_set_reg_class_out(res, 0, cls);
359 be_node_set_reg_class_in(res, be_pos_Reload_frame, cls_frame);
360 arch_irn_set_flags(res, arch_irn_flags_rematerializable);
363 * For spills and reloads, we return "none" as requirement for frame
364 * pointer, so every input is ok. Some backends need this (e.g. STA).
365 * Matze: we should investigate if this is really needed, this solution
366 * looks very hacky to me
368 be_set_constr_in(res, be_pos_Reload_frame, arch_no_register_req);
373 ir_node *be_get_Reload_mem(const ir_node *irn)
375 assert(be_is_Reload(irn));
376 return get_irn_n(irn, be_pos_Reload_mem);
379 ir_node *be_get_Reload_frame(const ir_node *irn)
381 assert(be_is_Reload(irn));
382 return get_irn_n(irn, be_pos_Reload_frame);
385 ir_node *be_get_Spill_val(const ir_node *irn)
387 assert(be_is_Spill(irn));
388 return get_irn_n(irn, be_pos_Spill_val);
391 ir_node *be_get_Spill_frame(const ir_node *irn)
393 assert(be_is_Spill(irn));
394 return get_irn_n(irn, be_pos_Spill_frame);
397 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_node *block,
398 int n, ir_node *in[])
401 ir_graph *irg = get_Block_irg(block);
403 ir_node *irn = new_ir_node(NULL, irg, block, op_be_Perm, mode_T, n, in);
404 init_node_attr(irn, n, n);
405 for (i = 0; i < n; ++i) {
406 be_node_set_reg_class_in(irn, i, cls);
407 be_node_set_reg_class_out(irn, i, cls);
413 void be_Perm_reduce(ir_node *perm, int new_size, int *map)
415 int arity = get_irn_arity(perm);
416 be_reg_data_t *old_data = ALLOCAN(be_reg_data_t, arity);
417 reg_out_info_t *old_infos = ALLOCAN(reg_out_info_t, arity);
418 be_node_attr_t *attr = get_irn_attr(perm);
419 backend_info_t *info = be_get_info(perm);
424 assert(be_is_Perm(perm));
425 assert(new_size <= arity);
427 new_in = alloca(new_size * sizeof(*new_in));
429 /* save the old register data */
430 memcpy(old_data, attr->reg_data, arity * sizeof(old_data[0]));
431 memcpy(old_infos, info->out_infos, arity * sizeof(old_infos[0]));
433 /* compose the new in array and set the new register data directly in place */
434 for (i = 0; i < new_size; ++i) {
436 new_in[i] = get_irn_n(perm, idx);
437 attr->reg_data[i] = old_data[idx];
438 info->out_infos[i] = old_infos[idx];
441 set_irn_in(perm, new_size, new_in);
444 ir_node *be_new_MemPerm(const arch_env_t *arch_env, ir_node *bl, int n, ir_node *in[])
446 ir_graph *irg = get_Block_irg(bl);
447 ir_node *frame = get_irg_frame(irg);
448 const arch_register_t *sp = arch_env->sp;
450 be_memperm_attr_t *attr;
453 real_in = ALLOCAN(ir_node*, n + 1);
455 memcpy(&real_in[1], in, n * sizeof(real_in[0]));
457 irn = new_ir_node(NULL, irg, bl, op_be_MemPerm, mode_T, n+1, real_in);
459 init_node_attr(irn, n + 1, n);
460 be_node_set_reg_class_in(irn, 0, sp->reg_class);
462 attr = get_irn_attr(irn);
463 attr->in_entities = OALLOCNZ(irg->obst, ir_entity*, n);
464 attr->out_entities = OALLOCNZ(irg->obst, ir_entity*, n);
469 ir_node *be_new_Copy(const arch_register_class_t *cls, ir_node *bl, ir_node *op)
473 arch_register_req_t *req;
474 ir_graph *irg = get_Block_irg(bl);
477 res = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
478 init_node_attr(res, 1, 1);
479 be_node_set_reg_class_in(res, 0, cls);
480 be_node_set_reg_class_out(res, 0, cls);
482 req = allocate_reg_req(res);
484 req->type = arch_register_req_type_should_be_same;
485 req->other_same = 1U << 0;
486 be_set_constr_out(res, 0, req);
491 ir_node *be_get_Copy_op(const ir_node *cpy) {
492 return get_irn_n(cpy, be_pos_Copy_op);
495 void be_set_Copy_op(ir_node *cpy, ir_node *op) {
496 set_irn_n(cpy, be_pos_Copy_op, op);
499 ir_node *be_new_Keep(ir_node *block, int n, ir_node *in[])
503 ir_graph *irg = get_Block_irg(block);
505 res = new_ir_node(NULL, irg, block, op_be_Keep, mode_ANY, -1, NULL);
506 init_node_attr(res, -1, 1);
508 for (i = 0; i < n; ++i) {
509 add_irn_n(res, in[i]);
510 add_register_req_in(res);
517 void be_Keep_add_node(ir_node *keep, const arch_register_class_t *cls, ir_node *node)
521 assert(be_is_Keep(keep));
522 n = add_irn_n(keep, node);
523 add_register_req_in(keep);
524 be_node_set_reg_class_in(keep, n, cls);
527 /* creates a be_Call */
528 ir_node *be_new_Call(dbg_info *dbg, ir_graph *irg, ir_node *bl, ir_node *mem,
529 ir_node *sp, ir_node *ptr, int n_outs, int n, ir_node *in[],
533 int real_n = be_pos_Call_first_arg + n;
537 NEW_ARR_A(ir_node *, real_in, real_n);
538 real_in[be_pos_Call_mem] = mem;
539 real_in[be_pos_Call_sp] = sp;
540 real_in[be_pos_Call_ptr] = ptr;
541 memcpy(&real_in[be_pos_Call_first_arg], in, n * sizeof(in[0]));
543 irn = new_ir_node(dbg, irg, bl, op_be_Call, mode_T, real_n, real_in);
544 a = init_node_attr(irn, real_n, n_outs);
546 a->call_tp = call_tp;
551 /* Gets the call entity or NULL if this is no static call. */
552 ir_entity *be_Call_get_entity(const ir_node *call) {
553 const be_call_attr_t *a = get_irn_attr_const(call);
554 assert(be_is_Call(call));
558 /* Sets the call entity. */
559 void be_Call_set_entity(ir_node *call, ir_entity *ent) {
560 be_call_attr_t *a = get_irn_attr(call);
561 assert(be_is_Call(call));
565 /* Gets the call type. */
566 ir_type *be_Call_get_type(ir_node *call) {
567 const be_call_attr_t *a = get_irn_attr_const(call);
568 assert(be_is_Call(call));
572 /* Sets the call type. */
573 void be_Call_set_type(ir_node *call, ir_type *call_tp) {
574 be_call_attr_t *a = get_irn_attr(call);
575 assert(be_is_Call(call));
576 a->call_tp = call_tp;
579 void be_Call_set_pop(ir_node *call, unsigned pop) {
580 be_call_attr_t *a = get_irn_attr(call);
584 unsigned be_Call_get_pop(const ir_node *call) {
585 const be_call_attr_t *a = get_irn_attr_const(call);
589 /* Construct a new be_Return. */
590 ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *block, int n_res,
591 unsigned pop, int n, ir_node *in[])
597 res = new_ir_node(dbg, irg, block, op_be_Return, mode_X, -1, NULL);
598 init_node_attr(res, -1, 1);
599 for (i = 0; i < n; ++i) {
600 add_irn_n(res, in[i]);
601 add_register_req_in(res);
603 be_set_constr_out(res, 0, arch_no_register_req);
605 a = get_irn_attr(res);
606 a->num_ret_vals = n_res;
613 /* Returns the number of real returns values */
614 int be_Return_get_n_rets(const ir_node *ret) {
615 const be_return_attr_t *a = get_irn_generic_attr_const(ret);
616 return a->num_ret_vals;
619 /* return the number of bytes that should be popped from stack when executing the Return. */
620 unsigned be_Return_get_pop(const ir_node *ret) {
621 const be_return_attr_t *a = get_irn_generic_attr_const(ret);
625 /* return non-zero, if number of popped bytes must be always emitted */
626 int be_Return_get_emit_pop(const ir_node *ret) {
627 const be_return_attr_t *a = get_irn_generic_attr_const(ret);
631 /* return non-zero, if number of popped bytes must be always emitted */
632 void be_Return_set_emit_pop(ir_node *ret, int emit_pop) {
633 be_return_attr_t *a = get_irn_generic_attr(ret);
634 a->emit_pop = emit_pop;
637 int be_Return_append_node(ir_node *ret, ir_node *node) {
640 pos = add_irn_n(ret, node);
641 add_register_req_in(ret);
646 ir_node *be_new_IncSP(const arch_register_t *sp, ir_node *bl,
647 ir_node *old_sp, int offset, int align)
652 ir_graph *irg = get_Block_irg(bl);
655 irn = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode,
656 sizeof(in) / sizeof(in[0]), in);
657 a = init_node_attr(irn, 1, 1);
661 /* Set output constraint to stack register. */
662 be_node_set_reg_class_in(irn, 0, sp->reg_class);
663 be_set_constr_single_reg_out(irn, 0, sp, arch_register_req_type_produces_sp);
668 ir_node *be_new_AddSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp,
673 ir_node *in[be_pos_AddSP_last];
674 const arch_register_class_t *cls;
677 in[be_pos_AddSP_old_sp] = old_sp;
678 in[be_pos_AddSP_size] = sz;
680 irg = get_Block_irg(bl);
681 irn = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T, be_pos_AddSP_last, in);
682 a = init_node_attr(irn, be_pos_AddSP_last, pn_be_AddSP_last);
684 /* Set output constraint to stack register. */
685 be_set_constr_single_reg_in(irn, be_pos_AddSP_old_sp, sp, 0);
686 be_node_set_reg_class_in(irn, be_pos_AddSP_size, arch_register_get_class(sp));
687 be_set_constr_single_reg_out(irn, pn_be_AddSP_sp, sp, arch_register_req_type_produces_sp);
689 cls = arch_register_get_class(sp);
694 ir_node *be_new_SubSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp, ir_node *sz)
698 ir_node *in[be_pos_SubSP_last];
701 in[be_pos_SubSP_old_sp] = old_sp;
702 in[be_pos_SubSP_size] = sz;
704 irg = get_Block_irg(bl);
705 irn = new_ir_node(NULL, irg, bl, op_be_SubSP, mode_T, be_pos_SubSP_last, in);
706 a = init_node_attr(irn, be_pos_SubSP_last, pn_be_SubSP_last);
708 /* Set output constraint to stack register. */
709 be_set_constr_single_reg_in(irn, be_pos_SubSP_old_sp, sp, 0);
710 be_node_set_reg_class_in(irn, be_pos_SubSP_size, arch_register_get_class(sp));
711 be_set_constr_single_reg_out(irn, pn_be_SubSP_sp, sp, arch_register_req_type_produces_sp);
716 ir_node *be_new_Start(ir_node *bl, int n_outs)
720 ir_graph *irg = get_Block_irg(bl);
722 res = new_ir_node(NULL, irg, bl, op_be_Start, mode_T, 0, NULL);
723 init_node_attr(res, 0, -1);
724 for (i = 0; i < n_outs; ++i) {
725 add_register_req_out(res);
731 ir_node *be_new_FrameAddr(const arch_register_class_t *cls_frame, ir_node *bl, ir_node *frame, ir_entity *ent)
736 ir_graph *irg = get_Block_irg(bl);
739 irn = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
740 a = init_node_attr(irn, 1, 1);
743 be_node_set_reg_class_in(irn, 0, cls_frame);
744 be_node_set_reg_class_out(irn, 0, cls_frame);
746 return optimize_node(irn);
749 ir_node *be_get_FrameAddr_frame(const ir_node *node) {
750 assert(be_is_FrameAddr(node));
751 return get_irn_n(node, be_pos_FrameAddr_ptr);
754 ir_entity *be_get_FrameAddr_entity(const ir_node *node)
756 const be_frame_attr_t *attr = get_irn_generic_attr_const(node);
760 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)
763 ir_node **in = ALLOCAN(ir_node*, n + 1);
764 ir_graph *irg = get_Block_irg(bl);
767 memcpy(&in[1], in_keep, n * sizeof(in[0]));
768 irn = new_ir_node(NULL, irg, bl, op_be_CopyKeep, mode, n + 1, in);
769 init_node_attr(irn, n + 1, 1);
770 be_node_set_reg_class_in(irn, 0, cls);
771 be_node_set_reg_class_out(irn, 0, cls);
776 ir_node *be_new_CopyKeep_single(const arch_register_class_t *cls, ir_node *bl, ir_node *src, ir_node *keep, ir_mode *mode)
778 return be_new_CopyKeep(cls, bl, src, 1, &keep, mode);
781 ir_node *be_get_CopyKeep_op(const ir_node *cpy) {
782 return get_irn_n(cpy, be_pos_CopyKeep_op);
785 void be_set_CopyKeep_op(ir_node *cpy, ir_node *op) {
786 set_irn_n(cpy, be_pos_CopyKeep_op, op);
789 ir_node *be_new_Barrier(ir_node *bl, int n, ir_node *in[])
793 ir_graph *irg = get_Block_irg(bl);
795 res = new_ir_node(NULL, irg, bl, op_be_Barrier, mode_T, -1, NULL);
796 init_node_attr(res, -1, -1);
797 for (i = 0; i < n; ++i) {
798 add_irn_n(res, in[i]);
799 add_register_req_in(res);
800 add_register_req_out(res);
806 ir_node *be_Barrier_append_node(ir_node *barrier, ir_node *node)
808 ir_node *block = get_nodes_block(barrier);
809 ir_mode *mode = get_irn_mode(node);
810 int n = add_irn_n(barrier, node);
812 ir_node *proj = new_r_Proj(block, barrier, mode, n);
813 add_register_req_in(barrier);
814 add_register_req_out(barrier);
819 int be_has_frame_entity(const ir_node *irn)
821 switch (get_irn_opcode(irn)) {
831 ir_entity *be_get_frame_entity(const ir_node *irn)
833 if (be_has_frame_entity(irn)) {
834 const be_frame_attr_t *a = get_irn_attr_const(irn);
840 int be_get_frame_offset(const ir_node *irn)
842 assert(is_be_node(irn));
843 if (be_has_frame_entity(irn)) {
844 const be_frame_attr_t *a = get_irn_attr_const(irn);
850 void be_set_MemPerm_in_entity(const ir_node *irn, int n, ir_entity *ent)
852 const be_memperm_attr_t *attr = get_irn_attr_const(irn);
854 assert(be_is_MemPerm(irn));
855 assert(n < be_get_MemPerm_entity_arity(irn));
857 attr->in_entities[n] = ent;
860 ir_entity* be_get_MemPerm_in_entity(const ir_node* irn, int n)
862 const be_memperm_attr_t *attr = get_irn_attr_const(irn);
864 assert(be_is_MemPerm(irn));
865 assert(n < be_get_MemPerm_entity_arity(irn));
867 return attr->in_entities[n];
870 void be_set_MemPerm_out_entity(const ir_node *irn, int n, ir_entity *ent)
872 const be_memperm_attr_t *attr = get_irn_attr_const(irn);
874 assert(be_is_MemPerm(irn));
875 assert(n < be_get_MemPerm_entity_arity(irn));
877 attr->out_entities[n] = ent;
880 ir_entity* be_get_MemPerm_out_entity(const ir_node* irn, int n)
882 const be_memperm_attr_t *attr = get_irn_attr_const(irn);
884 assert(be_is_MemPerm(irn));
885 assert(n < be_get_MemPerm_entity_arity(irn));
887 return attr->out_entities[n];
890 int be_get_MemPerm_entity_arity(const ir_node *irn)
892 return get_irn_arity(irn) - 1;
895 static const arch_register_req_t *get_single_req(struct obstack *obst,
896 const arch_register_t *reg, arch_register_req_type_t additional_types)
898 arch_register_req_t *req = obstack_alloc(obst, sizeof(*req));
899 const arch_register_class_t *cls = arch_register_get_class(reg);
900 unsigned *limited_bitset;
902 limited_bitset = rbitset_obstack_alloc(obst, arch_register_class_n_regs(cls));
903 rbitset_set(limited_bitset, arch_register_get_index(reg));
905 req->type = arch_register_req_type_limited | additional_types;
907 req->limited = limited_bitset;
911 void be_set_constr_single_reg_in(ir_node *node, int pos,
912 const arch_register_t *reg, arch_register_req_type_t additional_types)
914 const arch_register_req_t *req;
916 if (additional_types == 0) {
917 req = reg->single_req;
919 ir_graph *irg = get_irn_irg(node);
920 struct obstack *obst = be_get_birg_obst(irg);
921 req = get_single_req(obst, reg, additional_types);
923 be_set_constr_in(node, pos, req);
926 void be_set_constr_single_reg_out(ir_node *node, int pos,
927 const arch_register_t *reg, arch_register_req_type_t additional_types)
929 const arch_register_req_t *req;
931 /* if we have an ignore register, add ignore flag and just assign it */
932 if (reg->type & arch_register_type_ignore) {
933 additional_types |= arch_register_req_type_ignore;
936 if (additional_types == 0) {
937 req = reg->single_req;
939 ir_graph *irg = get_irn_irg(node);
940 struct obstack *obst = be_get_birg_obst(irg);
941 req = get_single_req(obst, reg, additional_types);
944 arch_irn_set_register(node, pos, reg);
945 be_set_constr_out(node, pos, req);
948 void be_node_set_reg_class_in(ir_node *irn, int pos,
949 const arch_register_class_t *cls)
951 be_set_constr_in(irn, pos, cls->class_req);
954 void be_node_set_reg_class_out(ir_node *irn, int pos,
955 const arch_register_class_t *cls)
957 be_set_constr_out(irn, pos, cls->class_req);
960 ir_node *be_get_IncSP_pred(ir_node *irn) {
961 assert(be_is_IncSP(irn));
962 return get_irn_n(irn, 0);
965 void be_set_IncSP_pred(ir_node *incsp, ir_node *pred) {
966 assert(be_is_IncSP(incsp));
967 set_irn_n(incsp, 0, pred);
970 void be_set_IncSP_offset(ir_node *irn, int offset)
972 be_incsp_attr_t *a = get_irn_attr(irn);
973 assert(be_is_IncSP(irn));
977 int be_get_IncSP_offset(const ir_node *irn)
979 const be_incsp_attr_t *a = get_irn_attr_const(irn);
980 assert(be_is_IncSP(irn));
984 int be_get_IncSP_align(const ir_node *irn)
986 const be_incsp_attr_t *a = get_irn_attr_const(irn);
987 assert(be_is_IncSP(irn));
991 ir_node *be_spill(ir_node *block, ir_node *irn)
993 ir_graph *irg = get_Block_irg(block);
994 ir_node *frame = get_irg_frame(irg);
995 const arch_register_class_t *cls = arch_get_irn_reg_class_out(irn);
996 const arch_register_class_t *cls_frame = arch_get_irn_reg_class_out(frame);
999 spill = be_new_Spill(cls, cls_frame, block, frame, irn);
1003 ir_node *be_reload(const arch_register_class_t *cls, ir_node *insert, ir_mode *mode, ir_node *spill)
1006 ir_node *bl = is_Block(insert) ? insert : get_nodes_block(insert);
1007 ir_graph *irg = get_Block_irg(bl);
1008 ir_node *frame = get_irg_frame(irg);
1009 const arch_register_class_t *cls_frame = arch_get_irn_reg_class_out(frame);
1011 assert(be_is_Spill(spill) || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
1013 reload = be_new_Reload(cls, cls_frame, bl, frame, spill, mode);
1015 if (is_Block(insert)) {
1016 insert = sched_skip(insert, 0, sched_skip_cf_predicator, NULL);
1017 sched_add_after(insert, reload);
1019 sched_add_before(insert, reload);
1027 | _ \ ___ __ _ | _ \ ___ __ _ ___
1028 | |_) / _ \/ _` | | |_) / _ \/ _` / __|
1029 | _ < __/ (_| | | _ < __/ (_| \__ \
1030 |_| \_\___|\__, | |_| \_\___|\__, |___/
1036 static const arch_register_req_t *be_node_get_in_reg_req(
1037 const ir_node *irn, int pos)
1039 const be_node_attr_t *a = get_irn_attr_const(irn);
1042 if (pos >= get_irn_arity(irn) || pos >= ARR_LEN(a->reg_data))
1043 return arch_no_register_req;
1045 return a->reg_data[pos].in_req;
1048 static arch_irn_class_t be_node_classify(const ir_node *irn)
1050 switch (get_irn_opcode(irn)) {
1051 case beo_Spill: return arch_irn_class_spill;
1052 case beo_Reload: return arch_irn_class_reload;
1053 case beo_Perm: return arch_irn_class_perm;
1054 case beo_Copy: return arch_irn_class_copy;
1059 static ir_entity *be_node_get_frame_entity(const ir_node *irn)
1061 return be_get_frame_entity(irn);
1064 static void be_node_set_frame_entity(ir_node *irn, ir_entity *ent)
1068 assert(be_has_frame_entity(irn));
1070 a = get_irn_attr(irn);
1074 static void be_node_set_frame_offset(ir_node *irn, int offset)
1078 if (!be_has_frame_entity(irn))
1081 a = get_irn_attr(irn);
1085 static int be_node_get_sp_bias(const ir_node *irn)
1087 if (be_is_IncSP(irn))
1088 return be_get_IncSP_offset(irn);
1089 if (be_is_Call(irn))
1090 return -(int)be_Call_get_pop(irn);
1096 ___ ____ _ _ _ _ _ _
1097 |_ _| _ \| \ | | | | | | __ _ _ __ __| | | ___ _ __
1098 | || |_) | \| | | |_| |/ _` | '_ \ / _` | |/ _ \ '__|
1099 | || _ <| |\ | | _ | (_| | | | | (_| | | __/ |
1100 |___|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
1105 static const arch_irn_ops_t be_node_irn_ops = {
1106 be_node_get_in_reg_req,
1108 be_node_get_frame_entity,
1109 be_node_set_frame_entity,
1110 be_node_set_frame_offset,
1111 be_node_get_sp_bias,
1112 NULL, /* get_inverse */
1113 NULL, /* get_op_estimated_cost */
1114 NULL, /* possible_memory_operand */
1115 NULL, /* perform_memory_operand */
1118 static const arch_register_req_t *dummy_reg_req(
1119 const ir_node *node, int pos)
1123 return arch_no_register_req;
1126 static arch_irn_class_t dummy_classify(const ir_node *node)
1132 static ir_entity* dummy_get_frame_entity(const ir_node *node)
1138 static void dummy_set_frame_entity(ir_node *node, ir_entity *entity)
1142 panic("dummy_set_frame_entity() should not be called");
1145 static void dummy_set_frame_offset(ir_node *node, int bias)
1149 panic("dummy_set_frame_offset() should not be called");
1152 static int dummy_get_sp_bias(const ir_node *node)
1158 /* for "middleend" nodes */
1159 static const arch_irn_ops_t dummy_be_irn_ops = {
1162 dummy_get_frame_entity,
1163 dummy_set_frame_entity,
1164 dummy_set_frame_offset,
1166 NULL, /* get_inverse */
1167 NULL, /* get_op_estimated_cost */
1168 NULL, /* possible_memory_operand */
1169 NULL, /* perform_memory_operand */
1173 ____ _ _ ___ ____ _ _ _ _ _ _
1174 | _ \| |__ (_) |_ _| _ \| \ | | | | | | __ _ _ __ __| | | ___ _ __
1175 | |_) | '_ \| | | || |_) | \| | | |_| |/ _` | '_ \ / _` | |/ _ \ '__|
1176 | __/| | | | | | || _ <| |\ | | _ | (_| | | | | (_| | | __/ |
1177 |_| |_| |_|_| |___|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
1181 ir_node *be_new_Phi(ir_node *block, int n_ins, ir_node **ins, ir_mode *mode,
1182 const arch_register_class_t *cls)
1184 struct obstack *obst = be_get_birg_obst(get_irn_irg(block));
1185 backend_info_t *info;
1187 ir_node *phi = new_r_Phi(block, n_ins, ins, mode);
1188 info = be_get_info(phi);
1189 info->out_infos = NEW_ARR_D(reg_out_info_t, obst, 1);
1190 memset(info->out_infos, 0, 1 * sizeof(info->out_infos[0]));
1192 info->out_infos[0].req = arch_no_register_req;
1194 info->out_infos[0].req = cls->class_req;
1201 * Guess correct register class of a phi node by looking at its arguments
1203 static const arch_register_req_t *get_Phi_reg_req_recursive(const ir_node *phi,
1206 int n = get_irn_arity(phi);
1210 if (*visited && pset_find_ptr(*visited, phi))
1213 for (i = 0; i < n; ++i) {
1214 op = get_irn_n(phi, i);
1215 /* Matze: don't we unnecessary constraint our phis with this?
1216 * we only need to take the regclass IMO*/
1218 return arch_get_register_req_out(op);
1222 * The operands of that Phi were all Phis themselves.
1223 * We have to start a DFS for a non-Phi argument now.
1226 *visited = pset_new_ptr(16);
1228 pset_insert_ptr(*visited, phi);
1230 for (i = 0; i < n; ++i) {
1231 const arch_register_req_t *req;
1232 op = get_irn_n(phi, i);
1233 req = get_Phi_reg_req_recursive(op, visited);
1241 static const arch_register_req_t *phi_get_irn_reg_req(const ir_node *node,
1244 backend_info_t *info = be_get_info(node);
1245 const arch_register_req_t *req = info->out_infos[0].req;
1249 if (!mode_is_datab(get_irn_mode(node))) {
1250 req = arch_no_register_req;
1252 pset *visited = NULL;
1254 req = get_Phi_reg_req_recursive(node, &visited);
1255 assert(req->cls != NULL);
1256 req = req->cls->class_req;
1258 if (visited != NULL)
1261 info->out_infos[0].req = req;
1267 void be_set_phi_reg_req(ir_node *node, const arch_register_req_t *req)
1269 backend_info_t *info = be_get_info(node);
1270 info->out_infos[0].req = req;
1272 assert(mode_is_datab(get_irn_mode(node)));
1275 int be_dump_phi_reg_reqs(ir_node *node, FILE *F, dump_reason_t reason)
1278 case dump_node_opcode_txt:
1279 fputs(get_op_name(get_irn_op(node)), F);
1281 case dump_node_mode_txt:
1282 fprintf(F, "%s", get_mode_name(get_irn_mode(node)));
1284 case dump_node_nodeattr_txt:
1286 case dump_node_info_txt:
1288 backend_info_t *info = be_get_info(node);
1289 if (info != NULL && info->out_infos[0].req != NULL) {
1290 arch_dump_reqs_and_registers(F, node);
1302 static const arch_irn_ops_t phi_irn_ops = {
1303 phi_get_irn_reg_req,
1305 dummy_get_frame_entity,
1306 dummy_set_frame_entity,
1307 dummy_set_frame_offset,
1309 NULL, /* get_inverse */
1310 NULL, /* get_op_estimated_cost */
1311 NULL, /* possible_memory_operand */
1312 NULL, /* perform_memory_operand */
1317 | \ | | ___ __| | ___ | _ \ _ _ _ __ ___ _ __ (_)_ __ __ _
1318 | \| |/ _ \ / _` |/ _ \ | | | | | | | '_ ` _ \| '_ \| | '_ \ / _` |
1319 | |\ | (_) | (_| | __/ | |_| | |_| | | | | | | |_) | | | | | (_| |
1320 |_| \_|\___/ \__,_|\___| |____/ \__,_|_| |_| |_| .__/|_|_| |_|\__, |
1325 * ir_op-Operation: dump a be node to file
1327 static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason)
1329 be_node_attr_t *at = get_irn_attr(irn);
1331 assert(is_be_node(irn));
1334 case dump_node_opcode_txt:
1335 fputs(get_op_name(get_irn_op(irn)), f);
1337 case dump_node_mode_txt:
1338 if (be_is_Perm(irn) || be_is_Copy(irn) || be_is_CopyKeep(irn)) {
1339 fprintf(f, " %s", get_mode_name(get_irn_mode(irn)));
1342 case dump_node_nodeattr_txt:
1343 if (be_is_Call(irn)) {
1344 be_call_attr_t *a = (be_call_attr_t *) at;
1346 fprintf(f, " [%s] ", get_entity_name(a->ent));
1348 if (be_is_IncSP(irn)) {
1349 const be_incsp_attr_t *attr = get_irn_generic_attr_const(irn);
1350 if (attr->offset == BE_STACK_FRAME_SIZE_EXPAND) {
1351 fprintf(f, " [Setup Stackframe] ");
1352 } else if (attr->offset == BE_STACK_FRAME_SIZE_SHRINK) {
1353 fprintf(f, " [Destroy Stackframe] ");
1355 fprintf(f, " [%d] ", attr->offset);
1359 case dump_node_info_txt:
1360 arch_dump_reqs_and_registers(f, irn);
1362 if (be_has_frame_entity(irn)) {
1363 be_frame_attr_t *a = (be_frame_attr_t *) at;
1365 unsigned size = get_type_size_bytes(get_entity_type(a->ent));
1366 ir_fprintf(f, "frame entity: %+F, offset 0x%x (%d), size 0x%x (%d) bytes\n",
1367 a->ent, a->offset, a->offset, size, size);
1372 switch (get_irn_opcode(irn)) {
1375 be_incsp_attr_t *a = (be_incsp_attr_t *) at;
1376 if (a->offset == BE_STACK_FRAME_SIZE_EXPAND)
1377 fprintf(f, "offset: FRAME_SIZE\n");
1378 else if (a->offset == BE_STACK_FRAME_SIZE_SHRINK)
1379 fprintf(f, "offset: -FRAME SIZE\n");
1381 fprintf(f, "offset: %u\n", a->offset);
1386 be_call_attr_t *a = (be_call_attr_t *) at;
1389 fprintf(f, "\ncalling: %s\n", get_entity_name(a->ent));
1395 for (i = 0; i < be_get_MemPerm_entity_arity(irn); ++i) {
1396 ir_entity *in, *out;
1397 in = be_get_MemPerm_in_entity(irn, i);
1398 out = be_get_MemPerm_out_entity(irn, i);
1400 fprintf(f, "\nin[%d]: %s\n", i, get_entity_name(in));
1403 fprintf(f, "\nout[%d]: %s\n", i, get_entity_name(out));
1419 * Copies the backend specific attributes from old node to new node.
1421 static void copy_attr(const ir_node *old_node, ir_node *new_node)
1423 const be_node_attr_t *old_attr = get_irn_attr_const(old_node);
1424 be_node_attr_t *new_attr = get_irn_attr(new_node);
1425 ir_graph *irg = get_irn_irg(new_node);
1426 struct obstack *obst = be_get_birg_obst(irg);
1427 backend_info_t *old_info = be_get_info(old_node);
1428 backend_info_t *new_info = be_get_info(new_node);
1430 assert(is_be_node(old_node));
1431 assert(is_be_node(new_node));
1433 memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
1435 if (old_info->out_infos != NULL) {
1436 unsigned n_outs = ARR_LEN(old_info->out_infos);
1437 /* need dyanmic out infos? */
1438 if (be_is_Barrier(new_node) || be_is_Perm(new_node)) {
1439 new_info->out_infos = NEW_ARR_F(reg_out_info_t, n_outs);
1441 new_info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_outs);
1443 memcpy(new_info->out_infos, old_info->out_infos,
1444 n_outs * sizeof(new_info->out_infos[0]));
1446 new_info->out_infos = NULL;
1450 if (old_attr->reg_data != NULL) {
1451 unsigned n_ins = ARR_LEN(old_attr->reg_data);
1452 /* need dynamic in infos? */
1453 if (get_irn_op(old_node)->opar == oparity_dynamic) {
1454 new_attr->reg_data = NEW_ARR_F(be_reg_data_t, n_ins);
1456 new_attr->reg_data = NEW_ARR_D(be_reg_data_t, obst, n_ins);
1458 memcpy(new_attr->reg_data, old_attr->reg_data,
1459 n_ins * sizeof(be_reg_data_t));
1461 new_attr->reg_data = NULL;
1465 static const ir_op_ops be_node_op_ops = {
1486 int is_be_node(const ir_node *irn)
1488 return get_op_ops(get_irn_op(irn))->be_ops == &be_node_irn_ops;
1491 void be_init_op(void)
1495 /* Acquire all needed opcodes. */
1496 op_be_Spill = new_ir_op(beo_Spill, "be_Spill", op_pin_state_pinned, N, oparity_unary, 0, sizeof(be_frame_attr_t), &be_node_op_ops);
1497 op_be_Reload = new_ir_op(beo_Reload, "be_Reload", op_pin_state_pinned, N, oparity_zero, 0, sizeof(be_frame_attr_t), &be_node_op_ops);
1498 op_be_Perm = new_ir_op(beo_Perm, "be_Perm", op_pin_state_pinned, N, oparity_variable, 0, sizeof(be_node_attr_t), &be_node_op_ops);
1499 op_be_MemPerm = new_ir_op(beo_MemPerm, "be_MemPerm", op_pin_state_pinned, N, oparity_variable, 0, sizeof(be_memperm_attr_t), &be_node_op_ops);
1500 op_be_Copy = new_ir_op(beo_Copy, "be_Copy", op_pin_state_floats, N, oparity_unary, 0, sizeof(be_node_attr_t), &be_node_op_ops);
1501 op_be_Keep = new_ir_op(beo_Keep, "be_Keep", op_pin_state_floats, K, oparity_dynamic, 0, sizeof(be_node_attr_t), &be_node_op_ops);
1502 op_be_CopyKeep = new_ir_op(beo_CopyKeep, "be_CopyKeep", op_pin_state_floats, K, oparity_variable, 0, sizeof(be_node_attr_t), &be_node_op_ops);
1503 op_be_Call = new_ir_op(beo_Call, "be_Call", op_pin_state_pinned, F|M, oparity_variable, 0, sizeof(be_call_attr_t), &be_node_op_ops);
1504 op_be_Return = new_ir_op(beo_Return, "be_Return", op_pin_state_pinned, X, oparity_dynamic, 0, sizeof(be_return_attr_t), &be_node_op_ops);
1505 op_be_AddSP = new_ir_op(beo_AddSP, "be_AddSP", op_pin_state_pinned, N, oparity_unary, 0, sizeof(be_node_attr_t), &be_node_op_ops);
1506 op_be_SubSP = new_ir_op(beo_SubSP, "be_SubSP", op_pin_state_pinned, N, oparity_unary, 0, sizeof(be_node_attr_t), &be_node_op_ops);
1507 op_be_IncSP = new_ir_op(beo_IncSP, "be_IncSP", op_pin_state_pinned, N, oparity_unary, 0, sizeof(be_incsp_attr_t), &be_node_op_ops);
1508 op_be_Start = new_ir_op(beo_Start, "be_Start", op_pin_state_pinned, N, oparity_zero, 0, sizeof(be_node_attr_t), &be_node_op_ops);
1509 op_be_FrameAddr = new_ir_op(beo_FrameAddr, "be_FrameAddr", op_pin_state_floats, N, oparity_unary, 0, sizeof(be_frame_attr_t), &be_node_op_ops);
1510 op_be_Barrier = new_ir_op(beo_Barrier, "be_Barrier", op_pin_state_pinned, N, oparity_dynamic, 0, sizeof(be_node_attr_t), &be_node_op_ops);
1512 op_be_Spill->ops.node_cmp_attr = FrameAddr_cmp_attr;
1513 op_be_Reload->ops.node_cmp_attr = FrameAddr_cmp_attr;
1514 op_be_Perm->ops.node_cmp_attr = node_cmp_attr;
1515 op_be_MemPerm->ops.node_cmp_attr = node_cmp_attr;
1516 op_be_Copy->ops.node_cmp_attr = node_cmp_attr;
1517 op_be_Keep->ops.node_cmp_attr = node_cmp_attr;
1518 op_be_CopyKeep->ops.node_cmp_attr = node_cmp_attr;
1519 op_be_Call->ops.node_cmp_attr = Call_cmp_attr;
1520 op_be_Return->ops.node_cmp_attr = Return_cmp_attr;
1521 op_be_AddSP->ops.node_cmp_attr = node_cmp_attr;
1522 op_be_SubSP->ops.node_cmp_attr = node_cmp_attr;
1523 op_be_IncSP->ops.node_cmp_attr = IncSP_cmp_attr;
1524 op_be_Start->ops.node_cmp_attr = node_cmp_attr;
1525 op_be_FrameAddr->ops.node_cmp_attr = FrameAddr_cmp_attr;
1526 op_be_Barrier->ops.node_cmp_attr = node_cmp_attr;
1528 /* attach out dummy_ops to middle end nodes */
1529 for (opc = iro_First; opc <= iro_Last; ++opc) {
1530 ir_op *op = get_irp_opcode(opc);
1531 assert(op->ops.be_ops == NULL);
1532 op->ops.be_ops = &dummy_be_irn_ops;
1535 op_Phi->ops.be_ops = &phi_irn_ops;