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 F irop_flag_fragile
133 #define Y irop_flag_forking
134 #define H irop_flag_highlevel
135 #define c irop_flag_constlike
136 #define K irop_flag_keep
137 #define M irop_flag_uses_memory
140 * Compare two be node attributes.
142 * @return zero if both attributes are identically
144 static int node_cmp_attr(ir_node *a, ir_node *b)
146 const be_node_attr_t *a_attr = get_irn_attr_const(a);
147 const be_node_attr_t *b_attr = get_irn_attr_const(b);
148 int i, len = ARR_LEN(a_attr->reg_data);
150 if (len != ARR_LEN(b_attr->reg_data))
153 if (!be_nodes_equal(a, b))
156 for (i = len - 1; i >= 0; --i) {
157 if (!reg_reqs_equal(a_attr->reg_data[i].in_req,
158 b_attr->reg_data[i].in_req))
166 * Compare the attributes of two be_FrameAddr nodes.
168 * @return zero if both nodes have identically attributes
170 static int FrameAddr_cmp_attr(ir_node *a, ir_node *b)
172 const be_frame_attr_t *a_attr = get_irn_attr_const(a);
173 const be_frame_attr_t *b_attr = get_irn_attr_const(b);
175 if (a_attr->ent != b_attr->ent || a_attr->offset != b_attr->offset)
178 return node_cmp_attr(a, b);
182 * Compare the attributes of two be_Return nodes.
184 * @return zero if both nodes have identically attributes
186 static int Return_cmp_attr(ir_node *a, ir_node *b)
188 const be_return_attr_t *a_attr = get_irn_attr_const(a);
189 const be_return_attr_t *b_attr = get_irn_attr_const(b);
191 if (a_attr->num_ret_vals != b_attr->num_ret_vals)
193 if (a_attr->pop != b_attr->pop)
195 if (a_attr->emit_pop != b_attr->emit_pop)
198 return node_cmp_attr(a, b);
202 * Compare the attributes of two be_IncSP nodes.
204 * @return zero if both nodes have identically attributes
206 static int IncSP_cmp_attr(ir_node *a, ir_node *b)
208 const be_incsp_attr_t *a_attr = get_irn_attr_const(a);
209 const be_incsp_attr_t *b_attr = get_irn_attr_const(b);
211 if (a_attr->offset != b_attr->offset)
214 return node_cmp_attr(a, b);
218 * Compare the attributes of two be_Call nodes.
220 * @return zero if both nodes have identically attributes
222 static int Call_cmp_attr(ir_node *a, ir_node *b)
224 const be_call_attr_t *a_attr = get_irn_attr_const(a);
225 const be_call_attr_t *b_attr = get_irn_attr_const(b);
227 if (a_attr->ent != b_attr->ent ||
228 a_attr->call_tp != b_attr->call_tp)
231 return node_cmp_attr(a, b);
234 static arch_register_req_t *allocate_reg_req(const ir_node *node)
236 ir_graph *irg = get_irn_irg(node);
237 struct obstack *obst = be_get_be_obst(irg);
239 arch_register_req_t *req = obstack_alloc(obst, sizeof(*req));
240 memset(req, 0, sizeof(*req));
244 void be_set_constr_in(ir_node *node, int pos, const arch_register_req_t *req)
246 const be_node_attr_t *attr = get_irn_attr_const(node);
247 be_reg_data_t *rd = &attr->reg_data[pos];
248 assert(pos < ARR_LEN(attr->reg_data));
252 void be_set_constr_out(ir_node *node, int pos, const arch_register_req_t *req)
254 backend_info_t *info = be_get_info(node);
255 info->out_infos[pos].req = req;
259 * Initializes the generic attribute of all be nodes and return it.
261 static void *init_node_attr(ir_node *node, int n_inputs, int n_outputs)
263 ir_graph *irg = get_irn_irg(node);
264 struct obstack *obst = be_get_be_obst(irg);
265 be_node_attr_t *a = get_irn_attr(node);
266 backend_info_t *info = be_get_info(node);
268 memset(a, 0, sizeof(get_op_attr_size(get_irn_op(node))));
272 a->reg_data = NEW_ARR_D(be_reg_data_t, obst, n_inputs);
273 for (i = 0; i < n_inputs; ++i) {
274 a->reg_data[i].in_req = arch_no_register_req;
277 a->reg_data = NEW_ARR_F(be_reg_data_t, 0);
280 if (n_outputs >= 0) {
282 info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_outputs);
283 memset(info->out_infos, 0, n_outputs * sizeof(info->out_infos[0]));
284 for (i = 0; i < n_outputs; ++i) {
285 info->out_infos[i].req = arch_no_register_req;
288 info->out_infos = NEW_ARR_F(reg_out_info_t, 0);
294 static void add_register_req_out(ir_node *node)
296 backend_info_t *info = be_get_info(node);
297 reg_out_info_t out_info;
298 memset(&out_info, 0, sizeof(out_info));
299 out_info.req = arch_no_register_req;
300 ARR_APP1(reg_out_info_t, info->out_infos, out_info);
303 static void add_register_req_in(ir_node *node)
305 be_node_attr_t *a = get_irn_attr(node);
306 be_reg_data_t regreq;
307 memset(®req, 0, sizeof(regreq));
308 regreq.in_req = arch_no_register_req;
309 ARR_APP1(be_reg_data_t, a->reg_data, regreq);
312 ir_node *be_new_Spill(const arch_register_class_t *cls,
313 const arch_register_class_t *cls_frame, ir_node *bl,
314 ir_node *frame, ir_node *to_spill)
319 ir_graph *irg = get_Block_irg(bl);
323 res = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
324 a = init_node_attr(res, 2, 1);
328 be_node_set_reg_class_in(res, be_pos_Spill_frame, cls_frame);
329 be_node_set_reg_class_in(res, be_pos_Spill_val, cls);
331 * For spills and reloads, we return "none" as requirement for frame
332 * pointer, so every input is ok. Some backends need this (STA).
333 * Matze: we should investigate if this is really needed, this solution
334 * looks very hacky to me
336 be_set_constr_in(res, be_pos_Spill_frame, arch_no_register_req);
338 arch_set_out_register_req(res, 0, arch_no_register_req);
343 ir_node *be_new_Reload(const arch_register_class_t *cls,
344 const arch_register_class_t *cls_frame, ir_node *block,
345 ir_node *frame, ir_node *mem, ir_mode *mode)
349 ir_graph *irg = get_Block_irg(block);
353 res = new_ir_node(NULL, irg, block, op_be_Reload, mode, 2, in);
355 init_node_attr(res, 2, 1);
356 be_node_set_reg_class_out(res, 0, cls);
358 be_node_set_reg_class_in(res, be_pos_Reload_frame, cls_frame);
359 arch_irn_set_flags(res, arch_irn_flags_rematerializable);
362 * For spills and reloads, we return "none" as requirement for frame
363 * pointer, so every input is ok. Some backends need this (e.g. STA).
364 * Matze: we should investigate if this is really needed, this solution
365 * looks very hacky to me
367 be_set_constr_in(res, be_pos_Reload_frame, arch_no_register_req);
372 ir_node *be_get_Reload_mem(const ir_node *irn)
374 assert(be_is_Reload(irn));
375 return get_irn_n(irn, be_pos_Reload_mem);
378 ir_node *be_get_Reload_frame(const ir_node *irn)
380 assert(be_is_Reload(irn));
381 return get_irn_n(irn, be_pos_Reload_frame);
384 ir_node *be_get_Spill_val(const ir_node *irn)
386 assert(be_is_Spill(irn));
387 return get_irn_n(irn, be_pos_Spill_val);
390 ir_node *be_get_Spill_frame(const ir_node *irn)
392 assert(be_is_Spill(irn));
393 return get_irn_n(irn, be_pos_Spill_frame);
396 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_node *block,
397 int n, ir_node *in[])
400 ir_graph *irg = get_Block_irg(block);
402 ir_node *irn = new_ir_node(NULL, irg, block, op_be_Perm, mode_T, n, in);
403 init_node_attr(irn, n, n);
404 for (i = 0; i < n; ++i) {
405 be_node_set_reg_class_in(irn, i, cls);
406 be_node_set_reg_class_out(irn, i, cls);
412 void be_Perm_reduce(ir_node *perm, int new_size, int *map)
414 int arity = get_irn_arity(perm);
415 be_reg_data_t *old_data = ALLOCAN(be_reg_data_t, arity);
416 reg_out_info_t *old_infos = ALLOCAN(reg_out_info_t, arity);
417 be_node_attr_t *attr = get_irn_attr(perm);
418 backend_info_t *info = be_get_info(perm);
423 assert(be_is_Perm(perm));
424 assert(new_size <= arity);
426 new_in = alloca(new_size * sizeof(*new_in));
428 /* save the old register data */
429 memcpy(old_data, attr->reg_data, arity * sizeof(old_data[0]));
430 memcpy(old_infos, info->out_infos, arity * sizeof(old_infos[0]));
432 /* compose the new in array and set the new register data directly in place */
433 for (i = 0; i < new_size; ++i) {
435 new_in[i] = get_irn_n(perm, idx);
436 attr->reg_data[i] = old_data[idx];
437 info->out_infos[i] = old_infos[idx];
440 set_irn_in(perm, new_size, new_in);
443 ir_node *be_new_MemPerm(ir_node *block, int n, ir_node *in[])
445 ir_graph *irg = get_Block_irg(block);
446 const arch_env_t *arch_env = be_get_irg_arch_env(irg);
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, block, 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)
493 return get_irn_n(cpy, be_pos_Copy_op);
496 void be_set_Copy_op(ir_node *cpy, ir_node *op)
498 set_irn_n(cpy, be_pos_Copy_op, op);
501 ir_node *be_new_Keep(ir_node *block, int n, ir_node *in[])
505 ir_graph *irg = get_Block_irg(block);
507 res = new_ir_node(NULL, irg, block, op_be_Keep, mode_ANY, -1, NULL);
508 init_node_attr(res, -1, 1);
510 for (i = 0; i < n; ++i) {
511 add_irn_n(res, in[i]);
512 add_register_req_in(res);
519 void be_Keep_add_node(ir_node *keep, const arch_register_class_t *cls, ir_node *node)
523 assert(be_is_Keep(keep));
524 n = add_irn_n(keep, node);
525 add_register_req_in(keep);
526 be_node_set_reg_class_in(keep, n, cls);
529 /* creates a be_Call */
530 ir_node *be_new_Call(dbg_info *dbg, ir_graph *irg, ir_node *bl, ir_node *mem,
531 ir_node *sp, ir_node *ptr, int n_outs, int n, ir_node *in[],
535 int real_n = be_pos_Call_first_arg + n;
539 NEW_ARR_A(ir_node *, real_in, real_n);
540 real_in[be_pos_Call_mem] = mem;
541 real_in[be_pos_Call_sp] = sp;
542 real_in[be_pos_Call_ptr] = ptr;
543 memcpy(&real_in[be_pos_Call_first_arg], in, n * sizeof(in[0]));
545 irn = new_ir_node(dbg, irg, bl, op_be_Call, mode_T, real_n, real_in);
546 a = init_node_attr(irn, real_n, n_outs);
548 a->call_tp = call_tp;
553 /* Gets the call entity or NULL if this is no static call. */
554 ir_entity *be_Call_get_entity(const ir_node *call)
556 const be_call_attr_t *a = get_irn_attr_const(call);
557 assert(be_is_Call(call));
561 /* Sets the call entity. */
562 void be_Call_set_entity(ir_node *call, ir_entity *ent)
564 be_call_attr_t *a = get_irn_attr(call);
565 assert(be_is_Call(call));
569 /* Gets the call type. */
570 ir_type *be_Call_get_type(ir_node *call)
572 const be_call_attr_t *a = get_irn_attr_const(call);
573 assert(be_is_Call(call));
577 /* Sets the call type. */
578 void be_Call_set_type(ir_node *call, ir_type *call_tp)
580 be_call_attr_t *a = get_irn_attr(call);
581 assert(be_is_Call(call));
582 a->call_tp = call_tp;
585 void be_Call_set_pop(ir_node *call, unsigned pop)
587 be_call_attr_t *a = get_irn_attr(call);
591 unsigned be_Call_get_pop(const ir_node *call)
593 const be_call_attr_t *a = get_irn_attr_const(call);
597 /* Construct a new be_Return. */
598 ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *block, int n_res,
599 unsigned pop, int n, ir_node *in[])
605 res = new_ir_node(dbg, irg, block, op_be_Return, mode_X, -1, NULL);
606 init_node_attr(res, -1, 1);
607 for (i = 0; i < n; ++i) {
608 add_irn_n(res, in[i]);
609 add_register_req_in(res);
611 be_set_constr_out(res, 0, arch_no_register_req);
613 a = get_irn_attr(res);
614 a->num_ret_vals = n_res;
621 /* Returns the number of real returns values */
622 int be_Return_get_n_rets(const ir_node *ret)
624 const be_return_attr_t *a = get_irn_generic_attr_const(ret);
625 return a->num_ret_vals;
628 /* return the number of bytes that should be popped from stack when executing the Return. */
629 unsigned be_Return_get_pop(const ir_node *ret)
631 const be_return_attr_t *a = get_irn_generic_attr_const(ret);
635 /* return non-zero, if number of popped bytes must be always emitted */
636 int be_Return_get_emit_pop(const ir_node *ret)
638 const be_return_attr_t *a = get_irn_generic_attr_const(ret);
642 /* return non-zero, if number of popped bytes must be always emitted */
643 void be_Return_set_emit_pop(ir_node *ret, int emit_pop)
645 be_return_attr_t *a = get_irn_generic_attr(ret);
646 a->emit_pop = emit_pop;
649 int be_Return_append_node(ir_node *ret, ir_node *node)
653 pos = add_irn_n(ret, node);
654 add_register_req_in(ret);
659 ir_node *be_new_IncSP(const arch_register_t *sp, ir_node *bl,
660 ir_node *old_sp, int offset, int align)
665 ir_graph *irg = get_Block_irg(bl);
668 irn = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode,
669 sizeof(in) / sizeof(in[0]), in);
670 a = init_node_attr(irn, 1, 1);
674 /* Set output constraint to stack register. */
675 be_node_set_reg_class_in(irn, 0, sp->reg_class);
676 be_set_constr_single_reg_out(irn, 0, sp, arch_register_req_type_produces_sp);
681 ir_node *be_new_AddSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp,
686 ir_node *in[be_pos_AddSP_last];
687 const arch_register_class_t *cls;
690 in[be_pos_AddSP_old_sp] = old_sp;
691 in[be_pos_AddSP_size] = sz;
693 irg = get_Block_irg(bl);
694 irn = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T, be_pos_AddSP_last, in);
695 a = init_node_attr(irn, be_pos_AddSP_last, pn_be_AddSP_last);
697 /* Set output constraint to stack register. */
698 be_set_constr_single_reg_in(irn, be_pos_AddSP_old_sp, sp, 0);
699 be_node_set_reg_class_in(irn, be_pos_AddSP_size, arch_register_get_class(sp));
700 be_set_constr_single_reg_out(irn, pn_be_AddSP_sp, sp, arch_register_req_type_produces_sp);
702 cls = arch_register_get_class(sp);
707 ir_node *be_new_SubSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp, ir_node *sz)
711 ir_node *in[be_pos_SubSP_last];
714 in[be_pos_SubSP_old_sp] = old_sp;
715 in[be_pos_SubSP_size] = sz;
717 irg = get_Block_irg(bl);
718 irn = new_ir_node(NULL, irg, bl, op_be_SubSP, mode_T, be_pos_SubSP_last, in);
719 a = init_node_attr(irn, be_pos_SubSP_last, pn_be_SubSP_last);
721 /* Set output constraint to stack register. */
722 be_set_constr_single_reg_in(irn, be_pos_SubSP_old_sp, sp, 0);
723 be_node_set_reg_class_in(irn, be_pos_SubSP_size, arch_register_get_class(sp));
724 be_set_constr_single_reg_out(irn, pn_be_SubSP_sp, sp, arch_register_req_type_produces_sp);
729 ir_node *be_new_Start(dbg_info *dbgi, ir_node *bl, int n_outs)
733 ir_graph *irg = get_Block_irg(bl);
735 res = new_ir_node(dbgi, irg, bl, op_be_Start, mode_T, 0, NULL);
736 init_node_attr(res, 0, -1);
737 for (i = 0; i < n_outs; ++i) {
738 add_register_req_out(res);
744 ir_node *be_new_FrameAddr(const arch_register_class_t *cls_frame, ir_node *bl, ir_node *frame, ir_entity *ent)
749 ir_graph *irg = get_Block_irg(bl);
752 irn = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
753 a = init_node_attr(irn, 1, 1);
756 be_node_set_reg_class_in(irn, 0, cls_frame);
757 be_node_set_reg_class_out(irn, 0, cls_frame);
759 return optimize_node(irn);
762 ir_node *be_get_FrameAddr_frame(const ir_node *node)
764 assert(be_is_FrameAddr(node));
765 return get_irn_n(node, be_pos_FrameAddr_ptr);
768 ir_entity *be_get_FrameAddr_entity(const ir_node *node)
770 const be_frame_attr_t *attr = get_irn_generic_attr_const(node);
774 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)
777 ir_node **in = ALLOCAN(ir_node*, n + 1);
778 ir_graph *irg = get_Block_irg(bl);
781 memcpy(&in[1], in_keep, n * sizeof(in[0]));
782 irn = new_ir_node(NULL, irg, bl, op_be_CopyKeep, mode, n + 1, in);
783 init_node_attr(irn, n + 1, 1);
784 be_node_set_reg_class_in(irn, 0, cls);
785 be_node_set_reg_class_out(irn, 0, cls);
790 ir_node *be_new_CopyKeep_single(const arch_register_class_t *cls, ir_node *bl, ir_node *src, ir_node *keep, ir_mode *mode)
792 return be_new_CopyKeep(cls, bl, src, 1, &keep, mode);
795 ir_node *be_get_CopyKeep_op(const ir_node *cpy)
797 return get_irn_n(cpy, be_pos_CopyKeep_op);
800 void be_set_CopyKeep_op(ir_node *cpy, ir_node *op)
802 set_irn_n(cpy, be_pos_CopyKeep_op, op);
805 ir_node *be_new_Barrier(ir_node *bl, int n, ir_node *in[])
809 ir_graph *irg = get_Block_irg(bl);
811 res = new_ir_node(NULL, irg, bl, op_be_Barrier, mode_T, -1, NULL);
812 init_node_attr(res, -1, -1);
813 for (i = 0; i < n; ++i) {
814 add_irn_n(res, in[i]);
815 add_register_req_in(res);
816 add_register_req_out(res);
822 ir_node *be_Barrier_append_node(ir_node *barrier, ir_node *node)
824 ir_mode *mode = get_irn_mode(node);
825 int n = add_irn_n(barrier, node);
827 ir_node *proj = new_r_Proj(barrier, mode, n);
828 add_register_req_in(barrier);
829 add_register_req_out(barrier);
834 static bool be_has_frame_entity(const ir_node *irn)
836 switch (get_irn_opcode(irn)) {
846 ir_entity *be_get_frame_entity(const ir_node *irn)
848 if (be_has_frame_entity(irn)) {
849 const be_frame_attr_t *a = get_irn_attr_const(irn);
855 int be_get_frame_offset(const ir_node *irn)
857 assert(is_be_node(irn));
858 if (be_has_frame_entity(irn)) {
859 const be_frame_attr_t *a = get_irn_attr_const(irn);
865 void be_set_MemPerm_in_entity(const ir_node *irn, int n, ir_entity *ent)
867 const be_memperm_attr_t *attr = get_irn_attr_const(irn);
869 assert(be_is_MemPerm(irn));
870 assert(n < be_get_MemPerm_entity_arity(irn));
872 attr->in_entities[n] = ent;
875 ir_entity* be_get_MemPerm_in_entity(const ir_node* irn, int n)
877 const be_memperm_attr_t *attr = get_irn_attr_const(irn);
879 assert(be_is_MemPerm(irn));
880 assert(n < be_get_MemPerm_entity_arity(irn));
882 return attr->in_entities[n];
885 void be_set_MemPerm_out_entity(const ir_node *irn, int n, ir_entity *ent)
887 const be_memperm_attr_t *attr = get_irn_attr_const(irn);
889 assert(be_is_MemPerm(irn));
890 assert(n < be_get_MemPerm_entity_arity(irn));
892 attr->out_entities[n] = ent;
895 ir_entity* be_get_MemPerm_out_entity(const ir_node* irn, int n)
897 const be_memperm_attr_t *attr = get_irn_attr_const(irn);
899 assert(be_is_MemPerm(irn));
900 assert(n < be_get_MemPerm_entity_arity(irn));
902 return attr->out_entities[n];
905 int be_get_MemPerm_entity_arity(const ir_node *irn)
907 return get_irn_arity(irn) - 1;
910 const arch_register_req_t *be_create_reg_req(struct obstack *obst,
911 const arch_register_t *reg, arch_register_req_type_t additional_types)
913 arch_register_req_t *req = obstack_alloc(obst, sizeof(*req));
914 const arch_register_class_t *cls = arch_register_get_class(reg);
915 unsigned *limited_bitset;
917 limited_bitset = rbitset_obstack_alloc(obst, arch_register_class_n_regs(cls));
918 rbitset_set(limited_bitset, arch_register_get_index(reg));
920 req->type = arch_register_req_type_limited | additional_types;
922 req->limited = limited_bitset;
926 void be_set_constr_single_reg_in(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 (additional_types == 0) {
932 req = reg->single_req;
934 ir_graph *irg = get_irn_irg(node);
935 struct obstack *obst = be_get_be_obst(irg);
936 req = be_create_reg_req(obst, reg, additional_types);
938 be_set_constr_in(node, pos, req);
941 void be_set_constr_single_reg_out(ir_node *node, int pos,
942 const arch_register_t *reg, arch_register_req_type_t additional_types)
944 const arch_register_req_t *req;
946 /* if we have an ignore register, add ignore flag and just assign it */
947 if (reg->type & arch_register_type_ignore) {
948 additional_types |= arch_register_req_type_ignore;
951 if (additional_types == 0) {
952 req = reg->single_req;
954 ir_graph *irg = get_irn_irg(node);
955 struct obstack *obst = be_get_be_obst(irg);
956 req = be_create_reg_req(obst, reg, additional_types);
959 arch_irn_set_register(node, pos, reg);
960 be_set_constr_out(node, pos, req);
963 void be_node_set_reg_class_in(ir_node *irn, int pos,
964 const arch_register_class_t *cls)
966 be_set_constr_in(irn, pos, cls->class_req);
969 void be_node_set_reg_class_out(ir_node *irn, int pos,
970 const arch_register_class_t *cls)
972 be_set_constr_out(irn, pos, cls->class_req);
975 ir_node *be_get_IncSP_pred(ir_node *irn)
977 assert(be_is_IncSP(irn));
978 return get_irn_n(irn, 0);
981 void be_set_IncSP_pred(ir_node *incsp, ir_node *pred)
983 assert(be_is_IncSP(incsp));
984 set_irn_n(incsp, 0, pred);
987 void be_set_IncSP_offset(ir_node *irn, int offset)
989 be_incsp_attr_t *a = get_irn_attr(irn);
990 assert(be_is_IncSP(irn));
994 int be_get_IncSP_offset(const ir_node *irn)
996 const be_incsp_attr_t *a = get_irn_attr_const(irn);
997 assert(be_is_IncSP(irn));
1001 int be_get_IncSP_align(const ir_node *irn)
1003 const be_incsp_attr_t *a = get_irn_attr_const(irn);
1004 assert(be_is_IncSP(irn));
1008 ir_node *be_spill(ir_node *block, ir_node *irn)
1010 ir_graph *irg = get_Block_irg(block);
1011 ir_node *frame = get_irg_frame(irg);
1012 const arch_register_class_t *cls = arch_get_irn_reg_class_out(irn);
1013 const arch_register_class_t *cls_frame = arch_get_irn_reg_class_out(frame);
1016 spill = be_new_Spill(cls, cls_frame, block, frame, irn);
1020 ir_node *be_reload(const arch_register_class_t *cls, ir_node *insert, ir_mode *mode, ir_node *spill)
1023 ir_node *bl = is_Block(insert) ? insert : get_nodes_block(insert);
1024 ir_graph *irg = get_Block_irg(bl);
1025 ir_node *frame = get_irg_frame(irg);
1026 const arch_register_class_t *cls_frame = arch_get_irn_reg_class_out(frame);
1028 assert(be_is_Spill(spill) || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
1030 reload = be_new_Reload(cls, cls_frame, bl, frame, spill, mode);
1032 if (is_Block(insert)) {
1033 insert = sched_skip(insert, 0, sched_skip_cf_predicator, NULL);
1034 sched_add_after(insert, reload);
1036 sched_add_before(insert, reload);
1044 | _ \ ___ __ _ | _ \ ___ __ _ ___
1045 | |_) / _ \/ _` | | |_) / _ \/ _` / __|
1046 | _ < __/ (_| | | _ < __/ (_| \__ \
1047 |_| \_\___|\__, | |_| \_\___|\__, |___/
1053 static const arch_register_req_t *be_node_get_in_reg_req(
1054 const ir_node *irn, int pos)
1056 const be_node_attr_t *a = get_irn_attr_const(irn);
1059 if (pos >= get_irn_arity(irn) || pos >= ARR_LEN(a->reg_data))
1060 return arch_no_register_req;
1062 return a->reg_data[pos].in_req;
1065 static arch_irn_class_t be_node_classify(const ir_node *irn)
1067 switch (get_irn_opcode(irn)) {
1068 case beo_Spill: return arch_irn_class_spill;
1069 case beo_Reload: return arch_irn_class_reload;
1070 case beo_Perm: return arch_irn_class_perm;
1071 case beo_Copy: return arch_irn_class_copy;
1076 static ir_entity *be_node_get_frame_entity(const ir_node *irn)
1078 return be_get_frame_entity(irn);
1081 void be_node_set_frame_entity(ir_node *irn, ir_entity *ent)
1085 assert(be_has_frame_entity(irn));
1087 a = get_irn_attr(irn);
1091 static void be_node_set_frame_offset(ir_node *irn, int offset)
1095 if (!be_has_frame_entity(irn))
1098 a = get_irn_attr(irn);
1102 static int be_node_get_sp_bias(const ir_node *irn)
1104 if (be_is_IncSP(irn))
1105 return be_get_IncSP_offset(irn);
1106 if (be_is_Call(irn))
1107 return -(int)be_Call_get_pop(irn);
1113 ___ ____ _ _ _ _ _ _
1114 |_ _| _ \| \ | | | | | | __ _ _ __ __| | | ___ _ __
1115 | || |_) | \| | | |_| |/ _` | '_ \ / _` | |/ _ \ '__|
1116 | || _ <| |\ | | _ | (_| | | | | (_| | | __/ |
1117 |___|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
1122 static const arch_irn_ops_t be_node_irn_ops = {
1123 be_node_get_in_reg_req,
1125 be_node_get_frame_entity,
1126 be_node_set_frame_offset,
1127 be_node_get_sp_bias,
1128 NULL, /* get_inverse */
1129 NULL, /* get_op_estimated_cost */
1130 NULL, /* possible_memory_operand */
1131 NULL, /* perform_memory_operand */
1134 static const arch_register_req_t *dummy_reg_req(
1135 const ir_node *node, int pos)
1139 return arch_no_register_req;
1142 static arch_irn_class_t dummy_classify(const ir_node *node)
1148 static ir_entity* dummy_get_frame_entity(const ir_node *node)
1154 static void dummy_set_frame_offset(ir_node *node, int bias)
1158 panic("dummy_set_frame_offset() should not be called");
1161 static int dummy_get_sp_bias(const ir_node *node)
1167 /* for "middleend" nodes */
1168 static const arch_irn_ops_t dummy_be_irn_ops = {
1171 dummy_get_frame_entity,
1172 dummy_set_frame_offset,
1174 NULL, /* get_inverse */
1175 NULL, /* get_op_estimated_cost */
1176 NULL, /* possible_memory_operand */
1177 NULL, /* perform_memory_operand */
1181 ____ _ _ ___ ____ _ _ _ _ _ _
1182 | _ \| |__ (_) |_ _| _ \| \ | | | | | | __ _ _ __ __| | | ___ _ __
1183 | |_) | '_ \| | | || |_) | \| | | |_| |/ _` | '_ \ / _` | |/ _ \ '__|
1184 | __/| | | | | | || _ <| |\ | | _ | (_| | | | | (_| | | __/ |
1185 |_| |_| |_|_| |___|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
1189 ir_node *be_new_Phi(ir_node *block, int n_ins, ir_node **ins, ir_mode *mode,
1190 const arch_register_class_t *cls)
1192 struct obstack *obst = be_get_be_obst(get_irn_irg(block));
1193 backend_info_t *info;
1195 ir_node *phi = new_r_Phi(block, n_ins, ins, mode);
1196 info = be_get_info(phi);
1197 info->out_infos = NEW_ARR_D(reg_out_info_t, obst, 1);
1198 memset(info->out_infos, 0, 1 * sizeof(info->out_infos[0]));
1200 info->out_infos[0].req = arch_no_register_req;
1202 info->out_infos[0].req = cls->class_req;
1209 * Guess correct register class of a phi node by looking at its arguments
1211 static const arch_register_req_t *get_Phi_reg_req_recursive(const ir_node *phi,
1214 int n = get_irn_arity(phi);
1218 if (*visited && pset_find_ptr(*visited, phi))
1221 for (i = 0; i < n; ++i) {
1222 op = get_irn_n(phi, i);
1223 /* Matze: don't we unnecessary constraint our phis with this?
1224 * we only need to take the regclass IMO*/
1226 return arch_get_register_req_out(op);
1230 * The operands of that Phi were all Phis themselves.
1231 * We have to start a DFS for a non-Phi argument now.
1234 *visited = pset_new_ptr(16);
1236 pset_insert_ptr(*visited, phi);
1238 for (i = 0; i < n; ++i) {
1239 const arch_register_req_t *req;
1240 op = get_irn_n(phi, i);
1241 req = get_Phi_reg_req_recursive(op, visited);
1249 static const arch_register_req_t *phi_get_irn_reg_req(const ir_node *node,
1252 backend_info_t *info = be_get_info(node);
1253 const arch_register_req_t *req = info->out_infos[0].req;
1257 if (!mode_is_datab(get_irn_mode(node))) {
1258 req = arch_no_register_req;
1260 pset *visited = NULL;
1262 req = get_Phi_reg_req_recursive(node, &visited);
1263 assert(req->cls != NULL);
1264 req = req->cls->class_req;
1266 if (visited != NULL)
1269 info->out_infos[0].req = req;
1275 void be_set_phi_reg_req(ir_node *node, const arch_register_req_t *req)
1277 backend_info_t *info = be_get_info(node);
1278 info->out_infos[0].req = req;
1280 assert(mode_is_datab(get_irn_mode(node)));
1283 void be_dump_phi_reg_reqs(FILE *F, ir_node *node, dump_reason_t reason)
1286 case dump_node_opcode_txt:
1287 fputs(get_op_name(get_irn_op(node)), F);
1289 case dump_node_mode_txt:
1290 fprintf(F, "%s", get_mode_name(get_irn_mode(node)));
1292 case dump_node_nodeattr_txt:
1294 case dump_node_info_txt:
1296 backend_info_t *info = be_get_info(node);
1297 if (info != NULL && info->out_infos[0].req != NULL) {
1298 arch_dump_reqs_and_registers(F, node);
1308 static const arch_irn_ops_t phi_irn_ops = {
1309 phi_get_irn_reg_req,
1311 dummy_get_frame_entity,
1312 dummy_set_frame_offset,
1314 NULL, /* get_inverse */
1315 NULL, /* get_op_estimated_cost */
1316 NULL, /* possible_memory_operand */
1317 NULL, /* perform_memory_operand */
1322 | \ | | ___ __| | ___ | _ \ _ _ _ __ ___ _ __ (_)_ __ __ _
1323 | \| |/ _ \ / _` |/ _ \ | | | | | | | '_ ` _ \| '_ \| | '_ \ / _` |
1324 | |\ | (_) | (_| | __/ | |_| | |_| | | | | | | |_) | | | | | (_| |
1325 |_| \_|\___/ \__,_|\___| |____/ \__,_|_| |_| |_| .__/|_|_| |_|\__, |
1330 * ir_op-Operation: dump a be node to file
1332 static void dump_node(FILE *f, ir_node *irn, dump_reason_t reason)
1334 assert(is_be_node(irn));
1337 case dump_node_opcode_txt:
1338 fputs(get_op_name(get_irn_op(irn)), f);
1340 case dump_node_mode_txt:
1341 if (be_is_Copy(irn) || be_is_CopyKeep(irn)) {
1342 fprintf(f, "%s", get_mode_name(get_irn_mode(irn)));
1345 case dump_node_nodeattr_txt:
1346 if (be_is_Call(irn)) {
1347 const be_call_attr_t *a = get_irn_generic_attr_const(irn);
1349 fprintf(f, " [%s] ", get_entity_name(a->ent));
1351 if (be_is_IncSP(irn)) {
1352 const be_incsp_attr_t *attr = get_irn_generic_attr_const(irn);
1353 if (attr->offset == BE_STACK_FRAME_SIZE_EXPAND) {
1354 fprintf(f, " [Setup Stackframe] ");
1355 } else if (attr->offset == BE_STACK_FRAME_SIZE_SHRINK) {
1356 fprintf(f, " [Destroy Stackframe] ");
1358 fprintf(f, " [%d] ", attr->offset);
1362 case dump_node_info_txt:
1363 arch_dump_reqs_and_registers(f, irn);
1365 if (be_has_frame_entity(irn)) {
1366 const be_frame_attr_t *a = get_irn_generic_attr_const(irn);
1368 unsigned size = get_type_size_bytes(get_entity_type(a->ent));
1369 ir_fprintf(f, "frame entity: %+F, offset 0x%x (%d), size 0x%x (%d) bytes\n",
1370 a->ent, a->offset, a->offset, size, size);
1375 switch (get_irn_opcode(irn)) {
1377 const be_incsp_attr_t *a = get_irn_generic_attr_const(irn);
1378 fprintf(f, "align: %d\n", a->align);
1379 if (a->offset == BE_STACK_FRAME_SIZE_EXPAND)
1380 fprintf(f, "offset: FRAME_SIZE\n");
1381 else if (a->offset == BE_STACK_FRAME_SIZE_SHRINK)
1382 fprintf(f, "offset: -FRAME SIZE\n");
1384 fprintf(f, "offset: %d\n", a->offset);
1388 const be_call_attr_t *a = get_irn_generic_attr_const(irn);
1391 fprintf(f, "\ncalling: %s\n", get_entity_name(a->ent));
1396 for (i = 0; i < be_get_MemPerm_entity_arity(irn); ++i) {
1397 ir_entity *in, *out;
1398 in = be_get_MemPerm_in_entity(irn, i);
1399 out = be_get_MemPerm_out_entity(irn, i);
1401 fprintf(f, "\nin[%d]: %s\n", i, get_entity_name(in));
1404 fprintf(f, "\nout[%d]: %s\n", i, get_entity_name(out));
1418 * Copies the backend specific attributes from old node to new node.
1420 static void copy_attr(ir_graph *irg, const ir_node *old_node, ir_node *new_node)
1422 const be_node_attr_t *old_attr = get_irn_attr_const(old_node);
1423 be_node_attr_t *new_attr = get_irn_attr(new_node);
1424 struct obstack *obst = be_get_be_obst(irg);
1425 backend_info_t *old_info = be_get_info(old_node);
1426 backend_info_t *new_info = be_get_info(new_node);
1428 assert(is_be_node(old_node));
1429 assert(is_be_node(new_node));
1431 memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
1433 if (old_info->out_infos != NULL) {
1434 unsigned n_outs = ARR_LEN(old_info->out_infos);
1435 /* need dyanmic out infos? */
1436 if (be_is_Barrier(new_node) || be_is_Perm(new_node)) {
1437 new_info->out_infos = NEW_ARR_F(reg_out_info_t, n_outs);
1439 new_info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_outs);
1441 memcpy(new_info->out_infos, old_info->out_infos,
1442 n_outs * sizeof(new_info->out_infos[0]));
1444 new_info->out_infos = NULL;
1448 if (old_attr->reg_data != NULL) {
1449 unsigned n_ins = ARR_LEN(old_attr->reg_data);
1450 /* need dynamic in infos? */
1451 if (get_irn_op(old_node)->opar == oparity_dynamic) {
1452 new_attr->reg_data = NEW_ARR_F(be_reg_data_t, n_ins);
1454 new_attr->reg_data = NEW_ARR_D(be_reg_data_t, obst, n_ins);
1456 memcpy(new_attr->reg_data, old_attr->reg_data,
1457 n_ins * sizeof(be_reg_data_t));
1459 new_attr->reg_data = NULL;
1463 static const ir_op_ops be_node_op_ops = {
1484 int is_be_node(const ir_node *irn)
1486 return get_op_ops(get_irn_op(irn))->be_ops == &be_node_irn_ops;
1489 void be_init_op(void)
1493 /* Acquire all needed opcodes. */
1494 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);
1495 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);
1496 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);
1497 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);
1498 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);
1499 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);
1500 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);
1501 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);
1502 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);
1503 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);
1504 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);
1505 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);
1506 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);
1507 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);
1508 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);
1510 op_be_Spill->ops.node_cmp_attr = FrameAddr_cmp_attr;
1511 op_be_Reload->ops.node_cmp_attr = FrameAddr_cmp_attr;
1512 op_be_Perm->ops.node_cmp_attr = node_cmp_attr;
1513 op_be_MemPerm->ops.node_cmp_attr = node_cmp_attr;
1514 op_be_Copy->ops.node_cmp_attr = node_cmp_attr;
1515 op_be_Keep->ops.node_cmp_attr = node_cmp_attr;
1516 op_be_CopyKeep->ops.node_cmp_attr = node_cmp_attr;
1517 op_be_Call->ops.node_cmp_attr = Call_cmp_attr;
1518 op_be_Return->ops.node_cmp_attr = Return_cmp_attr;
1519 op_be_AddSP->ops.node_cmp_attr = node_cmp_attr;
1520 op_be_SubSP->ops.node_cmp_attr = node_cmp_attr;
1521 op_be_IncSP->ops.node_cmp_attr = IncSP_cmp_attr;
1522 op_be_Start->ops.node_cmp_attr = node_cmp_attr;
1523 op_be_FrameAddr->ops.node_cmp_attr = FrameAddr_cmp_attr;
1524 op_be_Barrier->ops.node_cmp_attr = node_cmp_attr;
1526 /* attach out dummy_ops to middle end nodes */
1527 for (opc = iro_First; opc <= iro_Last; ++opc) {
1528 ir_op *op = get_irp_opcode(opc);
1529 assert(op->ops.be_ops == NULL);
1530 op->ops.be_ops = &dummy_be_irn_ops;
1533 op_Phi->ops.be_ops = &phi_irn_ops;