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 #define get_irn_attr(irn) get_irn_generic_attr(irn)
62 #define get_irn_attr_const(irn) get_irn_generic_attr_const(irn)
65 const arch_register_req_t *in_req;
68 /** The generic be nodes attribute type. */
70 be_reg_data_t *reg_data;
73 /** The be_Return nodes attribute type. */
75 be_node_attr_t node_attr; /**< base attributes of every be node. */
76 int num_ret_vals; /**< number of return values */
77 unsigned pop; /**< number of bytes that should be popped */
78 int emit_pop; /**< if set, emit pop bytes, even if pop = 0 */
81 /** The be_IncSP attribute type. */
83 be_node_attr_t node_attr; /**< base attributes of every be node. */
84 int offset; /**< The offset by which the stack shall be expanded/shrinked. */
85 int align; /**< whether stack should be aligned after the
89 /** The be_Frame attribute type. */
91 be_node_attr_t node_attr; /**< base attributes of every be node. */
96 /** The be_Call attribute type. */
98 be_node_attr_t node_attr; /**< base attributes of every be node. */
99 ir_entity *ent; /**< The called entity if this is a static call. */
101 ir_type *call_tp; /**< The call type, copied from the original Call node. */
105 be_node_attr_t node_attr; /**< base attributes of every be node. */
106 ir_entity **in_entities;
107 ir_entity **out_entities;
113 ir_op *op_be_MemPerm;
116 ir_op *op_be_CopyKeep;
123 ir_op *op_be_FrameAddr;
124 ir_op *op_be_Barrier;
126 static const ir_op_ops be_node_op_ops;
128 #define N irop_flag_none
129 #define L irop_flag_labeled
130 #define C irop_flag_commutative
131 #define X irop_flag_cfopcode
132 #define I irop_flag_ip_cfopcode
133 #define F irop_flag_fragile
134 #define Y irop_flag_forking
135 #define H irop_flag_highlevel
136 #define c irop_flag_constlike
137 #define K irop_flag_keep
138 #define M irop_flag_uses_memory
141 * Compare two be node attributes.
143 * @return zero if both attributes are identically
145 static int node_cmp_attr(ir_node *a, ir_node *b)
147 const be_node_attr_t *a_attr = get_irn_attr_const(a);
148 const be_node_attr_t *b_attr = get_irn_attr_const(b);
149 int i, len = ARR_LEN(a_attr->reg_data);
151 if (len != ARR_LEN(b_attr->reg_data))
154 if (!be_nodes_equal(a, b))
157 for (i = len - 1; i >= 0; --i) {
158 if (!reg_reqs_equal(a_attr->reg_data[i].in_req,
159 b_attr->reg_data[i].in_req))
167 * Compare the attributes of two be_FrameAddr nodes.
169 * @return zero if both nodes have identically attributes
171 static int FrameAddr_cmp_attr(ir_node *a, ir_node *b)
173 const be_frame_attr_t *a_attr = get_irn_attr_const(a);
174 const be_frame_attr_t *b_attr = get_irn_attr_const(b);
176 if (a_attr->ent != b_attr->ent || a_attr->offset != b_attr->offset)
179 return node_cmp_attr(a, b);
183 * Compare the attributes of two be_Return nodes.
185 * @return zero if both nodes have identically attributes
187 static int Return_cmp_attr(ir_node *a, ir_node *b)
189 const be_return_attr_t *a_attr = get_irn_attr_const(a);
190 const be_return_attr_t *b_attr = get_irn_attr_const(b);
192 if (a_attr->num_ret_vals != b_attr->num_ret_vals)
194 if (a_attr->pop != b_attr->pop)
196 if (a_attr->emit_pop != b_attr->emit_pop)
199 return node_cmp_attr(a, b);
203 * Compare the attributes of two be_IncSP nodes.
205 * @return zero if both nodes have identically attributes
207 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_be_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_be_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(ir_node *block, int n, ir_node *in[])
446 ir_graph *irg = get_Block_irg(block);
447 const arch_env_t *arch_env = be_get_irg_arch_env(irg);
448 ir_node *frame = get_irg_frame(irg);
449 const arch_register_t *sp = arch_env->sp;
451 be_memperm_attr_t *attr;
454 real_in = ALLOCAN(ir_node*, n + 1);
456 memcpy(&real_in[1], in, n * sizeof(real_in[0]));
458 irn = new_ir_node(NULL, irg, block, op_be_MemPerm, mode_T, n+1, real_in);
460 init_node_attr(irn, n + 1, n);
461 be_node_set_reg_class_in(irn, 0, sp->reg_class);
463 attr = get_irn_attr(irn);
464 attr->in_entities = OALLOCNZ(irg->obst, ir_entity*, n);
465 attr->out_entities = OALLOCNZ(irg->obst, ir_entity*, n);
470 ir_node *be_new_Copy(const arch_register_class_t *cls, ir_node *bl, ir_node *op)
474 arch_register_req_t *req;
475 ir_graph *irg = get_Block_irg(bl);
478 res = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
479 init_node_attr(res, 1, 1);
480 be_node_set_reg_class_in(res, 0, cls);
481 be_node_set_reg_class_out(res, 0, cls);
483 req = allocate_reg_req(res);
485 req->type = arch_register_req_type_should_be_same;
486 req->other_same = 1U << 0;
487 be_set_constr_out(res, 0, req);
492 ir_node *be_get_Copy_op(const ir_node *cpy)
494 return get_irn_n(cpy, be_pos_Copy_op);
497 void be_set_Copy_op(ir_node *cpy, ir_node *op)
499 set_irn_n(cpy, be_pos_Copy_op, op);
502 ir_node *be_new_Keep(ir_node *block, int n, ir_node *in[])
506 ir_graph *irg = get_Block_irg(block);
508 res = new_ir_node(NULL, irg, block, op_be_Keep, mode_ANY, -1, NULL);
509 init_node_attr(res, -1, 1);
511 for (i = 0; i < n; ++i) {
512 add_irn_n(res, in[i]);
513 add_register_req_in(res);
520 void be_Keep_add_node(ir_node *keep, const arch_register_class_t *cls, ir_node *node)
524 assert(be_is_Keep(keep));
525 n = add_irn_n(keep, node);
526 add_register_req_in(keep);
527 be_node_set_reg_class_in(keep, n, cls);
530 /* creates a be_Call */
531 ir_node *be_new_Call(dbg_info *dbg, ir_graph *irg, ir_node *bl, ir_node *mem,
532 ir_node *sp, ir_node *ptr, int n_outs, int n, ir_node *in[],
536 int real_n = be_pos_Call_first_arg + n;
540 NEW_ARR_A(ir_node *, real_in, real_n);
541 real_in[be_pos_Call_mem] = mem;
542 real_in[be_pos_Call_sp] = sp;
543 real_in[be_pos_Call_ptr] = ptr;
544 memcpy(&real_in[be_pos_Call_first_arg], in, n * sizeof(in[0]));
546 irn = new_ir_node(dbg, irg, bl, op_be_Call, mode_T, real_n, real_in);
547 a = init_node_attr(irn, real_n, n_outs);
549 a->call_tp = call_tp;
554 /* Gets the call entity or NULL if this is no static call. */
555 ir_entity *be_Call_get_entity(const ir_node *call)
557 const be_call_attr_t *a = get_irn_attr_const(call);
558 assert(be_is_Call(call));
562 /* Sets the call entity. */
563 void be_Call_set_entity(ir_node *call, ir_entity *ent)
565 be_call_attr_t *a = get_irn_attr(call);
566 assert(be_is_Call(call));
570 /* Gets the call type. */
571 ir_type *be_Call_get_type(ir_node *call)
573 const be_call_attr_t *a = get_irn_attr_const(call);
574 assert(be_is_Call(call));
578 /* Sets the call type. */
579 void be_Call_set_type(ir_node *call, ir_type *call_tp)
581 be_call_attr_t *a = get_irn_attr(call);
582 assert(be_is_Call(call));
583 a->call_tp = call_tp;
586 void be_Call_set_pop(ir_node *call, unsigned pop)
588 be_call_attr_t *a = get_irn_attr(call);
592 unsigned be_Call_get_pop(const ir_node *call)
594 const be_call_attr_t *a = get_irn_attr_const(call);
598 /* Construct a new be_Return. */
599 ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *block, int n_res,
600 unsigned pop, int n, ir_node *in[])
606 res = new_ir_node(dbg, irg, block, op_be_Return, mode_X, -1, NULL);
607 init_node_attr(res, -1, 1);
608 for (i = 0; i < n; ++i) {
609 add_irn_n(res, in[i]);
610 add_register_req_in(res);
612 be_set_constr_out(res, 0, arch_no_register_req);
614 a = get_irn_attr(res);
615 a->num_ret_vals = n_res;
622 /* Returns the number of real returns values */
623 int be_Return_get_n_rets(const ir_node *ret)
625 const be_return_attr_t *a = get_irn_generic_attr_const(ret);
626 return a->num_ret_vals;
629 /* return the number of bytes that should be popped from stack when executing the Return. */
630 unsigned be_Return_get_pop(const ir_node *ret)
632 const be_return_attr_t *a = get_irn_generic_attr_const(ret);
636 /* return non-zero, if number of popped bytes must be always emitted */
637 int be_Return_get_emit_pop(const ir_node *ret)
639 const be_return_attr_t *a = get_irn_generic_attr_const(ret);
643 /* return non-zero, if number of popped bytes must be always emitted */
644 void be_Return_set_emit_pop(ir_node *ret, int emit_pop)
646 be_return_attr_t *a = get_irn_generic_attr(ret);
647 a->emit_pop = emit_pop;
650 int be_Return_append_node(ir_node *ret, ir_node *node)
654 pos = add_irn_n(ret, node);
655 add_register_req_in(ret);
660 ir_node *be_new_IncSP(const arch_register_t *sp, ir_node *bl,
661 ir_node *old_sp, int offset, int align)
666 ir_graph *irg = get_Block_irg(bl);
669 irn = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode,
670 sizeof(in) / sizeof(in[0]), in);
671 a = init_node_attr(irn, 1, 1);
675 /* Set output constraint to stack register. */
676 be_node_set_reg_class_in(irn, 0, sp->reg_class);
677 be_set_constr_single_reg_out(irn, 0, sp, arch_register_req_type_produces_sp);
682 ir_node *be_new_AddSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp,
687 ir_node *in[be_pos_AddSP_last];
688 const arch_register_class_t *cls;
691 in[be_pos_AddSP_old_sp] = old_sp;
692 in[be_pos_AddSP_size] = sz;
694 irg = get_Block_irg(bl);
695 irn = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T, be_pos_AddSP_last, in);
696 a = init_node_attr(irn, be_pos_AddSP_last, pn_be_AddSP_last);
698 /* Set output constraint to stack register. */
699 be_set_constr_single_reg_in(irn, be_pos_AddSP_old_sp, sp, 0);
700 be_node_set_reg_class_in(irn, be_pos_AddSP_size, arch_register_get_class(sp));
701 be_set_constr_single_reg_out(irn, pn_be_AddSP_sp, sp, arch_register_req_type_produces_sp);
703 cls = arch_register_get_class(sp);
708 ir_node *be_new_SubSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp, ir_node *sz)
712 ir_node *in[be_pos_SubSP_last];
715 in[be_pos_SubSP_old_sp] = old_sp;
716 in[be_pos_SubSP_size] = sz;
718 irg = get_Block_irg(bl);
719 irn = new_ir_node(NULL, irg, bl, op_be_SubSP, mode_T, be_pos_SubSP_last, in);
720 a = init_node_attr(irn, be_pos_SubSP_last, pn_be_SubSP_last);
722 /* Set output constraint to stack register. */
723 be_set_constr_single_reg_in(irn, be_pos_SubSP_old_sp, sp, 0);
724 be_node_set_reg_class_in(irn, be_pos_SubSP_size, arch_register_get_class(sp));
725 be_set_constr_single_reg_out(irn, pn_be_SubSP_sp, sp, arch_register_req_type_produces_sp);
730 ir_node *be_new_Start(dbg_info *dbgi, ir_node *bl, int n_outs)
734 ir_graph *irg = get_Block_irg(bl);
736 res = new_ir_node(dbgi, irg, bl, op_be_Start, mode_T, 0, NULL);
737 init_node_attr(res, 0, -1);
738 for (i = 0; i < n_outs; ++i) {
739 add_register_req_out(res);
745 ir_node *be_new_FrameAddr(const arch_register_class_t *cls_frame, ir_node *bl, ir_node *frame, ir_entity *ent)
750 ir_graph *irg = get_Block_irg(bl);
753 irn = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
754 a = init_node_attr(irn, 1, 1);
757 be_node_set_reg_class_in(irn, 0, cls_frame);
758 be_node_set_reg_class_out(irn, 0, cls_frame);
760 return optimize_node(irn);
763 ir_node *be_get_FrameAddr_frame(const ir_node *node)
765 assert(be_is_FrameAddr(node));
766 return get_irn_n(node, be_pos_FrameAddr_ptr);
769 ir_entity *be_get_FrameAddr_entity(const ir_node *node)
771 const be_frame_attr_t *attr = get_irn_generic_attr_const(node);
775 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)
778 ir_node **in = ALLOCAN(ir_node*, n + 1);
779 ir_graph *irg = get_Block_irg(bl);
782 memcpy(&in[1], in_keep, n * sizeof(in[0]));
783 irn = new_ir_node(NULL, irg, bl, op_be_CopyKeep, mode, n + 1, in);
784 init_node_attr(irn, n + 1, 1);
785 be_node_set_reg_class_in(irn, 0, cls);
786 be_node_set_reg_class_out(irn, 0, cls);
791 ir_node *be_new_CopyKeep_single(const arch_register_class_t *cls, ir_node *bl, ir_node *src, ir_node *keep, ir_mode *mode)
793 return be_new_CopyKeep(cls, bl, src, 1, &keep, mode);
796 ir_node *be_get_CopyKeep_op(const ir_node *cpy)
798 return get_irn_n(cpy, be_pos_CopyKeep_op);
801 void be_set_CopyKeep_op(ir_node *cpy, ir_node *op)
803 set_irn_n(cpy, be_pos_CopyKeep_op, op);
806 ir_node *be_new_Barrier(ir_node *bl, int n, ir_node *in[])
810 ir_graph *irg = get_Block_irg(bl);
812 res = new_ir_node(NULL, irg, bl, op_be_Barrier, mode_T, -1, NULL);
813 init_node_attr(res, -1, -1);
814 for (i = 0; i < n; ++i) {
815 add_irn_n(res, in[i]);
816 add_register_req_in(res);
817 add_register_req_out(res);
823 ir_node *be_Barrier_append_node(ir_node *barrier, ir_node *node)
825 ir_mode *mode = get_irn_mode(node);
826 int n = add_irn_n(barrier, node);
828 ir_node *proj = new_r_Proj(barrier, mode, n);
829 add_register_req_in(barrier);
830 add_register_req_out(barrier);
835 static bool be_has_frame_entity(const ir_node *irn)
837 switch (get_irn_opcode(irn)) {
847 ir_entity *be_get_frame_entity(const ir_node *irn)
849 if (be_has_frame_entity(irn)) {
850 const be_frame_attr_t *a = get_irn_attr_const(irn);
856 int be_get_frame_offset(const ir_node *irn)
858 assert(is_be_node(irn));
859 if (be_has_frame_entity(irn)) {
860 const be_frame_attr_t *a = get_irn_attr_const(irn);
866 void be_set_MemPerm_in_entity(const ir_node *irn, int n, ir_entity *ent)
868 const be_memperm_attr_t *attr = get_irn_attr_const(irn);
870 assert(be_is_MemPerm(irn));
871 assert(n < be_get_MemPerm_entity_arity(irn));
873 attr->in_entities[n] = ent;
876 ir_entity* be_get_MemPerm_in_entity(const ir_node* irn, int n)
878 const be_memperm_attr_t *attr = get_irn_attr_const(irn);
880 assert(be_is_MemPerm(irn));
881 assert(n < be_get_MemPerm_entity_arity(irn));
883 return attr->in_entities[n];
886 void be_set_MemPerm_out_entity(const ir_node *irn, int n, ir_entity *ent)
888 const be_memperm_attr_t *attr = get_irn_attr_const(irn);
890 assert(be_is_MemPerm(irn));
891 assert(n < be_get_MemPerm_entity_arity(irn));
893 attr->out_entities[n] = ent;
896 ir_entity* be_get_MemPerm_out_entity(const ir_node* irn, int n)
898 const be_memperm_attr_t *attr = get_irn_attr_const(irn);
900 assert(be_is_MemPerm(irn));
901 assert(n < be_get_MemPerm_entity_arity(irn));
903 return attr->out_entities[n];
906 int be_get_MemPerm_entity_arity(const ir_node *irn)
908 return get_irn_arity(irn) - 1;
911 const arch_register_req_t *be_create_reg_req(struct obstack *obst,
912 const arch_register_t *reg, arch_register_req_type_t additional_types)
914 arch_register_req_t *req = obstack_alloc(obst, sizeof(*req));
915 const arch_register_class_t *cls = arch_register_get_class(reg);
916 unsigned *limited_bitset;
918 limited_bitset = rbitset_obstack_alloc(obst, arch_register_class_n_regs(cls));
919 rbitset_set(limited_bitset, arch_register_get_index(reg));
921 req->type = arch_register_req_type_limited | additional_types;
923 req->limited = limited_bitset;
927 void be_set_constr_single_reg_in(ir_node *node, int pos,
928 const arch_register_t *reg, arch_register_req_type_t additional_types)
930 const arch_register_req_t *req;
932 if (additional_types == 0) {
933 req = reg->single_req;
935 ir_graph *irg = get_irn_irg(node);
936 struct obstack *obst = be_get_be_obst(irg);
937 req = be_create_reg_req(obst, reg, additional_types);
939 be_set_constr_in(node, pos, req);
942 void be_set_constr_single_reg_out(ir_node *node, int pos,
943 const arch_register_t *reg, arch_register_req_type_t additional_types)
945 const arch_register_req_t *req;
947 /* if we have an ignore register, add ignore flag and just assign it */
948 if (reg->type & arch_register_type_ignore) {
949 additional_types |= arch_register_req_type_ignore;
952 if (additional_types == 0) {
953 req = reg->single_req;
955 ir_graph *irg = get_irn_irg(node);
956 struct obstack *obst = be_get_be_obst(irg);
957 req = be_create_reg_req(obst, reg, additional_types);
960 arch_irn_set_register(node, pos, reg);
961 be_set_constr_out(node, pos, req);
964 void be_node_set_reg_class_in(ir_node *irn, int pos,
965 const arch_register_class_t *cls)
967 be_set_constr_in(irn, pos, cls->class_req);
970 void be_node_set_reg_class_out(ir_node *irn, int pos,
971 const arch_register_class_t *cls)
973 be_set_constr_out(irn, pos, cls->class_req);
976 ir_node *be_get_IncSP_pred(ir_node *irn)
978 assert(be_is_IncSP(irn));
979 return get_irn_n(irn, 0);
982 void be_set_IncSP_pred(ir_node *incsp, ir_node *pred)
984 assert(be_is_IncSP(incsp));
985 set_irn_n(incsp, 0, pred);
988 void be_set_IncSP_offset(ir_node *irn, int offset)
990 be_incsp_attr_t *a = get_irn_attr(irn);
991 assert(be_is_IncSP(irn));
995 int be_get_IncSP_offset(const ir_node *irn)
997 const be_incsp_attr_t *a = get_irn_attr_const(irn);
998 assert(be_is_IncSP(irn));
1002 int be_get_IncSP_align(const ir_node *irn)
1004 const be_incsp_attr_t *a = get_irn_attr_const(irn);
1005 assert(be_is_IncSP(irn));
1009 ir_node *be_spill(ir_node *block, ir_node *irn)
1011 ir_graph *irg = get_Block_irg(block);
1012 ir_node *frame = get_irg_frame(irg);
1013 const arch_register_class_t *cls = arch_get_irn_reg_class_out(irn);
1014 const arch_register_class_t *cls_frame = arch_get_irn_reg_class_out(frame);
1017 spill = be_new_Spill(cls, cls_frame, block, frame, irn);
1021 ir_node *be_reload(const arch_register_class_t *cls, ir_node *insert, ir_mode *mode, ir_node *spill)
1024 ir_node *bl = is_Block(insert) ? insert : get_nodes_block(insert);
1025 ir_graph *irg = get_Block_irg(bl);
1026 ir_node *frame = get_irg_frame(irg);
1027 const arch_register_class_t *cls_frame = arch_get_irn_reg_class_out(frame);
1029 assert(be_is_Spill(spill) || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
1031 reload = be_new_Reload(cls, cls_frame, bl, frame, spill, mode);
1033 if (is_Block(insert)) {
1034 insert = sched_skip(insert, 0, sched_skip_cf_predicator, NULL);
1035 sched_add_after(insert, reload);
1037 sched_add_before(insert, reload);
1045 | _ \ ___ __ _ | _ \ ___ __ _ ___
1046 | |_) / _ \/ _` | | |_) / _ \/ _` / __|
1047 | _ < __/ (_| | | _ < __/ (_| \__ \
1048 |_| \_\___|\__, | |_| \_\___|\__, |___/
1054 static const arch_register_req_t *be_node_get_in_reg_req(
1055 const ir_node *irn, int pos)
1057 const be_node_attr_t *a = get_irn_attr_const(irn);
1060 if (pos >= get_irn_arity(irn) || pos >= ARR_LEN(a->reg_data))
1061 return arch_no_register_req;
1063 return a->reg_data[pos].in_req;
1066 static arch_irn_class_t be_node_classify(const ir_node *irn)
1068 switch (get_irn_opcode(irn)) {
1069 case beo_Spill: return arch_irn_class_spill;
1070 case beo_Reload: return arch_irn_class_reload;
1071 case beo_Perm: return arch_irn_class_perm;
1072 case beo_Copy: return arch_irn_class_copy;
1077 static ir_entity *be_node_get_frame_entity(const ir_node *irn)
1079 return be_get_frame_entity(irn);
1082 void be_node_set_frame_entity(ir_node *irn, ir_entity *ent)
1086 assert(be_has_frame_entity(irn));
1088 a = get_irn_attr(irn);
1092 static void be_node_set_frame_offset(ir_node *irn, int offset)
1096 if (!be_has_frame_entity(irn))
1099 a = get_irn_attr(irn);
1103 static int be_node_get_sp_bias(const ir_node *irn)
1105 if (be_is_IncSP(irn))
1106 return be_get_IncSP_offset(irn);
1107 if (be_is_Call(irn))
1108 return -(int)be_Call_get_pop(irn);
1114 ___ ____ _ _ _ _ _ _
1115 |_ _| _ \| \ | | | | | | __ _ _ __ __| | | ___ _ __
1116 | || |_) | \| | | |_| |/ _` | '_ \ / _` | |/ _ \ '__|
1117 | || _ <| |\ | | _ | (_| | | | | (_| | | __/ |
1118 |___|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
1123 static const arch_irn_ops_t be_node_irn_ops = {
1124 be_node_get_in_reg_req,
1126 be_node_get_frame_entity,
1127 be_node_set_frame_offset,
1128 be_node_get_sp_bias,
1129 NULL, /* get_inverse */
1130 NULL, /* get_op_estimated_cost */
1131 NULL, /* possible_memory_operand */
1132 NULL, /* perform_memory_operand */
1135 static const arch_register_req_t *dummy_reg_req(
1136 const ir_node *node, int pos)
1140 return arch_no_register_req;
1143 static arch_irn_class_t dummy_classify(const ir_node *node)
1149 static ir_entity* dummy_get_frame_entity(const ir_node *node)
1155 static void dummy_set_frame_offset(ir_node *node, int bias)
1159 panic("dummy_set_frame_offset() should not be called");
1162 static int dummy_get_sp_bias(const ir_node *node)
1168 /* for "middleend" nodes */
1169 static const arch_irn_ops_t dummy_be_irn_ops = {
1172 dummy_get_frame_entity,
1173 dummy_set_frame_offset,
1175 NULL, /* get_inverse */
1176 NULL, /* get_op_estimated_cost */
1177 NULL, /* possible_memory_operand */
1178 NULL, /* perform_memory_operand */
1182 ____ _ _ ___ ____ _ _ _ _ _ _
1183 | _ \| |__ (_) |_ _| _ \| \ | | | | | | __ _ _ __ __| | | ___ _ __
1184 | |_) | '_ \| | | || |_) | \| | | |_| |/ _` | '_ \ / _` | |/ _ \ '__|
1185 | __/| | | | | | || _ <| |\ | | _ | (_| | | | | (_| | | __/ |
1186 |_| |_| |_|_| |___|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
1190 ir_node *be_new_Phi(ir_node *block, int n_ins, ir_node **ins, ir_mode *mode,
1191 const arch_register_class_t *cls)
1193 struct obstack *obst = be_get_be_obst(get_irn_irg(block));
1194 backend_info_t *info;
1196 ir_node *phi = new_r_Phi(block, n_ins, ins, mode);
1197 info = be_get_info(phi);
1198 info->out_infos = NEW_ARR_D(reg_out_info_t, obst, 1);
1199 memset(info->out_infos, 0, 1 * sizeof(info->out_infos[0]));
1201 info->out_infos[0].req = arch_no_register_req;
1203 info->out_infos[0].req = cls->class_req;
1210 * Guess correct register class of a phi node by looking at its arguments
1212 static const arch_register_req_t *get_Phi_reg_req_recursive(const ir_node *phi,
1215 int n = get_irn_arity(phi);
1219 if (*visited && pset_find_ptr(*visited, phi))
1222 for (i = 0; i < n; ++i) {
1223 op = get_irn_n(phi, i);
1224 /* Matze: don't we unnecessary constraint our phis with this?
1225 * we only need to take the regclass IMO*/
1227 return arch_get_register_req_out(op);
1231 * The operands of that Phi were all Phis themselves.
1232 * We have to start a DFS for a non-Phi argument now.
1235 *visited = pset_new_ptr(16);
1237 pset_insert_ptr(*visited, phi);
1239 for (i = 0; i < n; ++i) {
1240 const arch_register_req_t *req;
1241 op = get_irn_n(phi, i);
1242 req = get_Phi_reg_req_recursive(op, visited);
1250 static const arch_register_req_t *phi_get_irn_reg_req(const ir_node *node,
1253 backend_info_t *info = be_get_info(node);
1254 const arch_register_req_t *req = info->out_infos[0].req;
1258 if (!mode_is_datab(get_irn_mode(node))) {
1259 req = arch_no_register_req;
1261 pset *visited = NULL;
1263 req = get_Phi_reg_req_recursive(node, &visited);
1264 assert(req->cls != NULL);
1265 req = req->cls->class_req;
1267 if (visited != NULL)
1270 info->out_infos[0].req = req;
1276 void be_set_phi_reg_req(ir_node *node, const arch_register_req_t *req)
1278 backend_info_t *info = be_get_info(node);
1279 info->out_infos[0].req = req;
1281 assert(mode_is_datab(get_irn_mode(node)));
1284 void be_dump_phi_reg_reqs(FILE *F, ir_node *node, dump_reason_t reason)
1287 case dump_node_opcode_txt:
1288 fputs(get_op_name(get_irn_op(node)), F);
1290 case dump_node_mode_txt:
1291 fprintf(F, "%s", get_mode_name(get_irn_mode(node)));
1293 case dump_node_nodeattr_txt:
1295 case dump_node_info_txt:
1297 backend_info_t *info = be_get_info(node);
1298 if (info != NULL && info->out_infos[0].req != NULL) {
1299 arch_dump_reqs_and_registers(F, node);
1309 static const arch_irn_ops_t phi_irn_ops = {
1310 phi_get_irn_reg_req,
1312 dummy_get_frame_entity,
1313 dummy_set_frame_offset,
1315 NULL, /* get_inverse */
1316 NULL, /* get_op_estimated_cost */
1317 NULL, /* possible_memory_operand */
1318 NULL, /* perform_memory_operand */
1323 | \ | | ___ __| | ___ | _ \ _ _ _ __ ___ _ __ (_)_ __ __ _
1324 | \| |/ _ \ / _` |/ _ \ | | | | | | | '_ ` _ \| '_ \| | '_ \ / _` |
1325 | |\ | (_) | (_| | __/ | |_| | |_| | | | | | | |_) | | | | | (_| |
1326 |_| \_|\___/ \__,_|\___| |____/ \__,_|_| |_| |_| .__/|_|_| |_|\__, |
1331 * ir_op-Operation: dump a be node to file
1333 static void dump_node(FILE *f, ir_node *irn, dump_reason_t reason)
1335 assert(is_be_node(irn));
1338 case dump_node_opcode_txt:
1339 fputs(get_op_name(get_irn_op(irn)), f);
1341 case dump_node_mode_txt:
1342 if (be_is_Copy(irn) || be_is_CopyKeep(irn)) {
1343 fprintf(f, "%s", get_mode_name(get_irn_mode(irn)));
1346 case dump_node_nodeattr_txt:
1347 if (be_is_Call(irn)) {
1348 const be_call_attr_t *a = get_irn_generic_attr_const(irn);
1350 fprintf(f, " [%s] ", get_entity_name(a->ent));
1352 if (be_is_IncSP(irn)) {
1353 const be_incsp_attr_t *attr = get_irn_generic_attr_const(irn);
1354 if (attr->offset == BE_STACK_FRAME_SIZE_EXPAND) {
1355 fprintf(f, " [Setup Stackframe] ");
1356 } else if (attr->offset == BE_STACK_FRAME_SIZE_SHRINK) {
1357 fprintf(f, " [Destroy Stackframe] ");
1359 fprintf(f, " [%d] ", attr->offset);
1363 case dump_node_info_txt:
1364 arch_dump_reqs_and_registers(f, irn);
1366 if (be_has_frame_entity(irn)) {
1367 const be_frame_attr_t *a = get_irn_generic_attr_const(irn);
1369 unsigned size = get_type_size_bytes(get_entity_type(a->ent));
1370 ir_fprintf(f, "frame entity: %+F, offset 0x%x (%d), size 0x%x (%d) bytes\n",
1371 a->ent, a->offset, a->offset, size, size);
1376 switch (get_irn_opcode(irn)) {
1378 const be_incsp_attr_t *a = get_irn_generic_attr_const(irn);
1379 fprintf(f, "align: %d\n", a->align);
1380 if (a->offset == BE_STACK_FRAME_SIZE_EXPAND)
1381 fprintf(f, "offset: FRAME_SIZE\n");
1382 else if (a->offset == BE_STACK_FRAME_SIZE_SHRINK)
1383 fprintf(f, "offset: -FRAME SIZE\n");
1385 fprintf(f, "offset: %d\n", a->offset);
1389 const be_call_attr_t *a = get_irn_generic_attr_const(irn);
1392 fprintf(f, "\ncalling: %s\n", get_entity_name(a->ent));
1397 for (i = 0; i < be_get_MemPerm_entity_arity(irn); ++i) {
1398 ir_entity *in, *out;
1399 in = be_get_MemPerm_in_entity(irn, i);
1400 out = be_get_MemPerm_out_entity(irn, i);
1402 fprintf(f, "\nin[%d]: %s\n", i, get_entity_name(in));
1405 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(ir_graph *irg, 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 struct obstack *obst = be_get_be_obst(irg);
1426 backend_info_t *old_info = be_get_info(old_node);
1427 backend_info_t *new_info = be_get_info(new_node);
1429 assert(is_be_node(old_node));
1430 assert(is_be_node(new_node));
1432 memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
1434 if (old_info->out_infos != NULL) {
1435 unsigned n_outs = ARR_LEN(old_info->out_infos);
1436 /* need dyanmic out infos? */
1437 if (be_is_Barrier(new_node) || be_is_Perm(new_node)) {
1438 new_info->out_infos = NEW_ARR_F(reg_out_info_t, n_outs);
1440 new_info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_outs);
1442 memcpy(new_info->out_infos, old_info->out_infos,
1443 n_outs * sizeof(new_info->out_infos[0]));
1445 new_info->out_infos = NULL;
1449 if (old_attr->reg_data != NULL) {
1450 unsigned n_ins = ARR_LEN(old_attr->reg_data);
1451 /* need dynamic in infos? */
1452 if (get_irn_op(old_node)->opar == oparity_dynamic) {
1453 new_attr->reg_data = NEW_ARR_F(be_reg_data_t, n_ins);
1455 new_attr->reg_data = NEW_ARR_D(be_reg_data_t, obst, n_ins);
1457 memcpy(new_attr->reg_data, old_attr->reg_data,
1458 n_ins * sizeof(be_reg_data_t));
1460 new_attr->reg_data = NULL;
1464 static const ir_op_ops be_node_op_ops = {
1485 int is_be_node(const ir_node *irn)
1487 return get_op_ops(get_irn_op(irn))->be_ops == &be_node_irn_ops;
1490 void be_init_op(void)
1494 /* Acquire all needed opcodes. */
1495 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);
1496 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);
1497 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);
1498 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);
1499 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);
1500 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);
1501 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);
1502 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);
1503 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);
1504 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);
1505 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);
1506 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);
1507 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);
1508 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);
1509 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);
1511 op_be_Spill->ops.node_cmp_attr = FrameAddr_cmp_attr;
1512 op_be_Reload->ops.node_cmp_attr = FrameAddr_cmp_attr;
1513 op_be_Perm->ops.node_cmp_attr = node_cmp_attr;
1514 op_be_MemPerm->ops.node_cmp_attr = node_cmp_attr;
1515 op_be_Copy->ops.node_cmp_attr = node_cmp_attr;
1516 op_be_Keep->ops.node_cmp_attr = node_cmp_attr;
1517 op_be_CopyKeep->ops.node_cmp_attr = node_cmp_attr;
1518 op_be_Call->ops.node_cmp_attr = Call_cmp_attr;
1519 op_be_Return->ops.node_cmp_attr = Return_cmp_attr;
1520 op_be_AddSP->ops.node_cmp_attr = node_cmp_attr;
1521 op_be_SubSP->ops.node_cmp_attr = node_cmp_attr;
1522 op_be_IncSP->ops.node_cmp_attr = IncSP_cmp_attr;
1523 op_be_Start->ops.node_cmp_attr = node_cmp_attr;
1524 op_be_FrameAddr->ops.node_cmp_attr = FrameAddr_cmp_attr;
1525 op_be_Barrier->ops.node_cmp_attr = node_cmp_attr;
1527 /* attach out dummy_ops to middle end nodes */
1528 for (opc = iro_First; opc <= iro_Last; ++opc) {
1529 ir_op *op = get_irp_opcode(opc);
1530 assert(op->ops.be_ops == NULL);
1531 op->ops.be_ops = &dummy_be_irn_ops;
1534 op_Phi->ops.be_ops = &phi_irn_ops;