2 * Copyright (C) 1995-2011 University of Karlsruhe. All right reserved.
4 * This file is part of libFirm.
6 * This file may be distributed and/or modified under the terms of the
7 * GNU General Public License version 2 as published by the Free Software
8 * Foundation and appearing in the file LICENSE.GPL included in the
9 * packaging of this file.
11 * Licensees holding valid libFirm Professional Edition licenses may use
12 * this file in accordance with the libFirm Commercial License.
13 * Agreement provided with the Software.
15 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
16 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * @brief Backend node support for generic backend nodes.
23 * @author Sebastian Hack
26 * Backend node support for generic backend nodes.
27 * This file provides Perm, Copy, Spill and Reload nodes.
39 #include "bitfiddle.h"
40 #include "raw_bitset.h"
60 typedef struct be_node_attr_t {
64 /** The be_Return nodes attribute type. */
67 int num_ret_vals; /**< number of return values */
68 unsigned pop; /**< number of bytes that should be popped */
69 int emit_pop; /**< if set, emit pop bytes, even if pop = 0 */
72 /** The be_IncSP attribute type. */
75 int offset; /**< The offset by which the stack shall be
77 int align; /**< whether stack should be aligned after the
81 /** The be_Frame attribute type. */
88 /** The be_Call attribute type. */
91 ir_entity *ent; /**< called entity if this is a static call. */
93 ir_type *call_tp; /**< call type, copied from the original Call */
98 ir_entity **in_entities;
99 ir_entity **out_entities;
105 ir_op *op_be_MemPerm;
108 ir_op *op_be_CopyKeep;
115 ir_op *op_be_FrameAddr;
118 * Compare the attributes of two be_FrameAddr nodes.
120 * @return zero if both nodes have identically attributes
122 static int FrameAddr_cmp_attr(const ir_node *a, const ir_node *b)
124 const be_frame_attr_t *a_attr = (const be_frame_attr_t*)get_irn_generic_attr_const(a);
125 const be_frame_attr_t *b_attr = (const be_frame_attr_t*)get_irn_generic_attr_const(b);
127 if (a_attr->ent != b_attr->ent || a_attr->offset != b_attr->offset)
130 return be_nodes_equal(a, b);
134 * Compare the attributes of two be_Return nodes.
136 * @return zero if both nodes have identically attributes
138 static int Return_cmp_attr(const ir_node *a, const ir_node *b)
140 const be_return_attr_t *a_attr = (const be_return_attr_t*)get_irn_generic_attr_const(a);
141 const be_return_attr_t *b_attr = (const be_return_attr_t*)get_irn_generic_attr_const(b);
143 if (a_attr->num_ret_vals != b_attr->num_ret_vals)
145 if (a_attr->pop != b_attr->pop)
147 if (a_attr->emit_pop != b_attr->emit_pop)
150 return be_nodes_equal(a, b);
154 * Compare the attributes of two be_IncSP nodes.
156 * @return zero if both nodes have identically attributes
158 static int IncSP_cmp_attr(const ir_node *a, const ir_node *b)
160 const be_incsp_attr_t *a_attr = (const be_incsp_attr_t*)get_irn_generic_attr_const(a);
161 const be_incsp_attr_t *b_attr = (const be_incsp_attr_t*)get_irn_generic_attr_const(b);
163 if (a_attr->offset != b_attr->offset)
166 return be_nodes_equal(a, b);
170 * Compare the attributes of two be_Call nodes.
172 * @return zero if both nodes have identically attributes
174 static int Call_cmp_attr(const ir_node *a, const ir_node *b)
176 const be_call_attr_t *a_attr = (const be_call_attr_t*)get_irn_generic_attr_const(a);
177 const be_call_attr_t *b_attr = (const be_call_attr_t*)get_irn_generic_attr_const(b);
179 if (a_attr->ent != b_attr->ent ||
180 a_attr->call_tp != b_attr->call_tp)
183 return be_nodes_equal(a, b);
186 static arch_register_req_t *allocate_reg_req(const ir_node *node)
188 ir_graph *irg = get_irn_irg(node);
189 struct obstack *obst = be_get_be_obst(irg);
191 arch_register_req_t *req = OALLOCZ(obst, arch_register_req_t);
195 void be_set_constr_in(ir_node *node, int pos, const arch_register_req_t *req)
197 backend_info_t *info = be_get_info(node);
198 assert(pos < get_irn_arity(node));
199 info->in_reqs[pos] = req;
202 void be_set_constr_out(ir_node *node, int pos, const arch_register_req_t *req)
204 backend_info_t *info = be_get_info(node);
205 info->out_infos[pos].req = req;
209 * Initializes the generic attribute of all be nodes and return it.
211 static void init_node_attr(ir_node *node, int n_inputs, int n_outputs)
213 ir_graph *irg = get_irn_irg(node);
214 struct obstack *obst = be_get_be_obst(irg);
215 backend_info_t *info = be_get_info(node);
216 const arch_register_req_t **in_reqs;
220 assert(n_inputs == get_irn_arity(node));
221 in_reqs = OALLOCN(obst, const arch_register_req_t*, n_inputs);
222 for (i = 0; i < n_inputs; ++i) {
223 in_reqs[i] = arch_no_register_req;
226 in_reqs = NEW_ARR_F(const arch_register_req_t*, 0);
228 info->in_reqs = in_reqs;
230 if (n_outputs >= 0) {
232 info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_outputs);
233 memset(info->out_infos, 0, n_outputs * sizeof(info->out_infos[0]));
234 for (i = 0; i < n_outputs; ++i) {
235 info->out_infos[i].req = arch_no_register_req;
238 info->out_infos = NEW_ARR_F(reg_out_info_t, 0);
242 static void add_register_req_in(ir_node *node)
244 backend_info_t *info = be_get_info(node);
245 ARR_APP1(const arch_register_req_t*, info->in_reqs, arch_no_register_req);
248 ir_node *be_new_Spill(const arch_register_class_t *cls,
249 const arch_register_class_t *cls_frame, ir_node *bl,
250 ir_node *frame, ir_node *to_spill)
255 ir_graph *irg = get_Block_irg(bl);
259 res = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
260 init_node_attr(res, 2, 1);
261 a = (be_frame_attr_t*) get_irn_generic_attr(res);
264 a->base.exc.pin_state = op_pin_state_pinned;
266 be_node_set_reg_class_in(res, n_be_Spill_frame, cls_frame);
267 be_node_set_reg_class_in(res, n_be_Spill_val, cls);
269 * For spills and reloads, we return "none" as requirement for frame
270 * pointer, so every input is ok. Some backends need this (STA).
271 * Matze: we should investigate if this is really needed, this solution
272 * looks very hacky to me
274 be_set_constr_in(res, n_be_Spill_frame, arch_no_register_req);
276 arch_set_irn_register_req_out(res, 0, arch_no_register_req);
281 ir_node *be_new_Reload(const arch_register_class_t *cls,
282 const arch_register_class_t *cls_frame, ir_node *block,
283 ir_node *frame, ir_node *mem, ir_mode *mode)
287 ir_graph *irg = get_Block_irg(block);
292 res = new_ir_node(NULL, irg, block, op_be_Reload, mode, 2, in);
294 init_node_attr(res, 2, 1);
295 be_node_set_reg_class_out(res, 0, cls);
297 be_node_set_reg_class_in(res, n_be_Reload_frame, cls_frame);
298 arch_set_irn_flags(res, arch_irn_flags_rematerializable);
300 a = (be_frame_attr_t*) get_irn_generic_attr(res);
303 a->base.exc.pin_state = op_pin_state_pinned;
306 * For spills and reloads, we return "none" as requirement for frame
307 * pointer, so every input is ok. Some backends need this (e.g. STA).
308 * Matze: we should investigate if this is really needed, this solution
309 * looks very hacky to me
311 be_set_constr_in(res, n_be_Reload_frame, arch_no_register_req);
316 ir_node *be_get_Reload_mem(const ir_node *irn)
318 assert(be_is_Reload(irn));
319 return get_irn_n(irn, n_be_Reload_mem);
322 ir_node *be_get_Reload_frame(const ir_node *irn)
324 assert(be_is_Reload(irn));
325 return get_irn_n(irn, n_be_Reload_frame);
328 ir_node *be_get_Spill_val(const ir_node *irn)
330 assert(be_is_Spill(irn));
331 return get_irn_n(irn, n_be_Spill_val);
334 ir_node *be_get_Spill_frame(const ir_node *irn)
336 assert(be_is_Spill(irn));
337 return get_irn_n(irn, n_be_Spill_frame);
340 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_node *block,
341 int n, ir_node *in[])
344 ir_graph *irg = get_Block_irg(block);
345 be_node_attr_t *attr;
347 ir_node *irn = new_ir_node(NULL, irg, block, op_be_Perm, mode_T, n, in);
348 init_node_attr(irn, n, n);
349 attr = (be_node_attr_t*) get_irn_generic_attr(irn);
350 attr->exc.pin_state = op_pin_state_pinned;
351 for (i = 0; i < n; ++i) {
352 const ir_node *input = in[i];
353 const arch_register_req_t *req = arch_get_irn_register_req(input);
354 if (req->width == 1) {
355 be_set_constr_in(irn, i, cls->class_req);
356 be_set_constr_out(irn, i, cls->class_req);
358 arch_register_req_t *new_req = allocate_reg_req(irn);
360 new_req->type = (req->type & arch_register_req_type_aligned);
361 new_req->width = req->width;
362 be_set_constr_in(irn, i, new_req);
363 be_set_constr_out(irn, i, new_req);
370 void be_Perm_reduce(ir_node *perm, int new_size, int *map)
372 int arity = get_irn_arity(perm);
373 const arch_register_req_t **old_in_reqs
374 = ALLOCAN(const arch_register_req_t*, arity);
375 reg_out_info_t *old_infos = ALLOCAN(reg_out_info_t, arity);
376 backend_info_t *info = be_get_info(perm);
380 assert(be_is_Perm(perm));
381 assert(new_size <= arity);
383 new_in = ALLOCAN(ir_node*, new_size);
385 /* save the old register data */
386 memcpy(old_in_reqs, info->in_reqs, arity * sizeof(old_in_reqs[0]));
387 memcpy(old_infos, info->out_infos, arity * sizeof(old_infos[0]));
389 /* compose the new in array and set the new register data directly */
390 for (i = 0; i < new_size; ++i) {
392 new_in[i] = get_irn_n(perm, idx);
393 info->in_reqs[i] = old_in_reqs[idx];
394 info->out_infos[i] = old_infos[idx];
397 set_irn_in(perm, new_size, new_in);
400 ir_node *be_new_MemPerm(ir_node *block, int n, ir_node *in[])
402 ir_graph *irg = get_Block_irg(block);
403 const arch_env_t *arch_env = be_get_irg_arch_env(irg);
404 ir_node *frame = get_irg_frame(irg);
405 const arch_register_t *sp = arch_env->sp;
407 be_memperm_attr_t *attr;
410 real_in = ALLOCAN(ir_node*, n + 1);
412 memcpy(&real_in[1], in, n * sizeof(real_in[0]));
414 irn = new_ir_node(NULL, irg, block, op_be_MemPerm, mode_T, n+1, real_in);
416 init_node_attr(irn, n + 1, n);
417 be_node_set_reg_class_in(irn, 0, sp->reg_class);
419 attr = (be_memperm_attr_t*)get_irn_generic_attr(irn);
420 attr->in_entities = OALLOCNZ(irg->obst, ir_entity*, n);
421 attr->out_entities = OALLOCNZ(irg->obst, ir_entity*, n);
426 ir_node *be_new_Copy(ir_node *bl, ir_node *op)
430 arch_register_req_t *req;
431 be_node_attr_t *attr;
432 ir_graph *irg = get_Block_irg(bl);
433 const arch_register_req_t *in_req = arch_get_irn_register_req(op);
434 const arch_register_class_t *cls = in_req->cls;
437 res = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
438 init_node_attr(res, 1, 1);
439 attr = (be_node_attr_t*) get_irn_generic_attr(res);
440 attr->exc.pin_state = op_pin_state_floats;
441 be_node_set_reg_class_in(res, 0, cls);
442 be_node_set_reg_class_out(res, 0, cls);
444 req = allocate_reg_req(res);
446 req->type = arch_register_req_type_should_be_same
447 | (in_req->type & arch_register_req_type_aligned);
448 req->other_same = 1U << 0;
449 req->width = in_req->width;
450 be_set_constr_out(res, 0, req);
455 ir_node *be_get_Copy_op(const ir_node *cpy)
457 return get_irn_n(cpy, n_be_Copy_op);
460 void be_set_Copy_op(ir_node *cpy, ir_node *op)
462 set_irn_n(cpy, n_be_Copy_op, op);
465 ir_node *be_new_Keep(ir_node *block, int n, ir_node *in[])
469 ir_graph *irg = get_Block_irg(block);
470 be_node_attr_t *attr;
472 res = new_ir_node(NULL, irg, block, op_be_Keep, mode_ANY, -1, NULL);
473 init_node_attr(res, -1, 1);
474 attr = (be_node_attr_t*) get_irn_generic_attr(res);
475 attr->exc.pin_state = op_pin_state_pinned;
477 for (i = 0; i < n; ++i) {
478 add_irn_n(res, in[i]);
479 add_register_req_in(res);
486 void be_Keep_add_node(ir_node *keep, const arch_register_class_t *cls, ir_node *node)
490 assert(be_is_Keep(keep));
491 n = add_irn_n(keep, node);
492 add_register_req_in(keep);
493 be_node_set_reg_class_in(keep, n, cls);
496 ir_node *be_new_Call(dbg_info *dbg, ir_graph *irg, ir_node *bl, ir_node *mem,
497 ir_node *sp, ir_node *ptr, int n_outs, int n, ir_node *in[],
501 int real_n = n_be_Call_first_arg + n;
505 NEW_ARR_A(ir_node *, real_in, real_n);
506 real_in[n_be_Call_mem] = mem;
507 real_in[n_be_Call_sp] = sp;
508 real_in[n_be_Call_ptr] = ptr;
509 memcpy(&real_in[n_be_Call_first_arg], in, n * sizeof(in[0]));
511 irn = new_ir_node(dbg, irg, bl, op_be_Call, mode_T, real_n, real_in);
512 init_node_attr(irn, real_n, n_outs);
513 a = (be_call_attr_t*)get_irn_generic_attr(irn);
515 a->call_tp = call_tp;
517 a->base.exc.pin_state = op_pin_state_pinned;
521 ir_entity *be_Call_get_entity(const ir_node *call)
523 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
524 assert(be_is_Call(call));
528 void be_Call_set_entity(ir_node *call, ir_entity *ent)
530 be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
531 assert(be_is_Call(call));
535 ir_type *be_Call_get_type(ir_node *call)
537 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
538 assert(be_is_Call(call));
542 void be_Call_set_type(ir_node *call, ir_type *call_tp)
544 be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
545 assert(be_is_Call(call));
546 a->call_tp = call_tp;
549 void be_Call_set_pop(ir_node *call, unsigned pop)
551 be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
555 unsigned be_Call_get_pop(const ir_node *call)
557 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
561 ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *block, int n_res,
562 unsigned pop, int n, ir_node *in[])
567 res = new_ir_node(dbg, irg, block, op_be_Return, mode_X, n, in);
568 init_node_attr(res, n, 1);
569 be_set_constr_out(res, 0, arch_no_register_req);
571 a = (be_return_attr_t*)get_irn_generic_attr(res);
572 a->num_ret_vals = n_res;
575 a->base.exc.pin_state = op_pin_state_pinned;
580 int be_Return_get_n_rets(const ir_node *ret)
582 const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
583 return a->num_ret_vals;
586 unsigned be_Return_get_pop(const ir_node *ret)
588 const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
592 int be_Return_get_emit_pop(const ir_node *ret)
594 const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
598 void be_Return_set_emit_pop(ir_node *ret, int emit_pop)
600 be_return_attr_t *a = (be_return_attr_t*)get_irn_generic_attr(ret);
601 a->emit_pop = emit_pop;
604 ir_node *be_new_IncSP(const arch_register_t *sp, ir_node *bl,
605 ir_node *old_sp, int offset, int align)
610 ir_graph *irg = get_Block_irg(bl);
613 irn = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode,
615 init_node_attr(irn, 1, 1);
616 a = (be_incsp_attr_t*)get_irn_generic_attr(irn);
619 a->base.exc.pin_state = op_pin_state_pinned;
621 /* Set output constraint to stack register. */
622 be_node_set_reg_class_in(irn, 0, sp->reg_class);
623 be_set_constr_single_reg_out(irn, 0, sp, arch_register_req_type_produces_sp);
628 ir_node *be_new_AddSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp,
632 ir_node *in[n_be_AddSP_last];
634 be_node_attr_t *attr;
636 in[n_be_AddSP_old_sp] = old_sp;
637 in[n_be_AddSP_size] = sz;
639 irg = get_Block_irg(bl);
640 irn = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T, n_be_AddSP_last, in);
641 init_node_attr(irn, n_be_AddSP_last, pn_be_AddSP_last);
642 attr = (be_node_attr_t*) get_irn_generic_attr(irn);
643 attr->exc.pin_state = op_pin_state_pinned;
645 /* Set output constraint to stack register. */
646 be_set_constr_single_reg_in(irn, n_be_AddSP_old_sp, sp,
647 arch_register_req_type_none);
648 be_node_set_reg_class_in(irn, n_be_AddSP_size, sp->reg_class);
649 be_set_constr_single_reg_out(irn, pn_be_AddSP_sp, sp,
650 arch_register_req_type_produces_sp);
655 ir_node *be_new_SubSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp, ir_node *sz)
658 ir_node *in[n_be_SubSP_last];
660 be_node_attr_t *attr;
662 in[n_be_SubSP_old_sp] = old_sp;
663 in[n_be_SubSP_size] = sz;
665 irg = get_Block_irg(bl);
666 irn = new_ir_node(NULL, irg, bl, op_be_SubSP, mode_T, n_be_SubSP_last, in);
667 init_node_attr(irn, n_be_SubSP_last, pn_be_SubSP_last);
668 attr = (be_node_attr_t*) get_irn_generic_attr(irn);
669 attr->exc.pin_state = op_pin_state_pinned;
671 /* Set output constraint to stack register. */
672 be_set_constr_single_reg_in(irn, n_be_SubSP_old_sp, sp,
673 arch_register_req_type_none);
674 be_node_set_reg_class_in(irn, n_be_SubSP_size, sp->reg_class);
675 be_set_constr_single_reg_out(irn, pn_be_SubSP_sp, sp, arch_register_req_type_produces_sp);
680 ir_node *be_new_Start(dbg_info *dbgi, ir_node *bl, int n_outs)
683 ir_graph *irg = get_Block_irg(bl);
684 be_node_attr_t *attr;
686 res = new_ir_node(dbgi, irg, bl, op_be_Start, mode_T, 0, NULL);
687 init_node_attr(res, 0, n_outs);
688 attr = (be_node_attr_t*) get_irn_generic_attr(res);
689 attr->exc.pin_state = op_pin_state_pinned;
694 ir_node *be_new_FrameAddr(const arch_register_class_t *cls_frame, ir_node *bl, ir_node *frame, ir_entity *ent)
699 ir_graph *irg = get_Block_irg(bl);
702 irn = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
703 init_node_attr(irn, 1, 1);
704 a = (be_frame_attr_t*)get_irn_generic_attr(irn);
707 a->base.exc.pin_state = op_pin_state_floats;
708 be_node_set_reg_class_in(irn, 0, cls_frame);
709 be_node_set_reg_class_out(irn, 0, cls_frame);
711 return optimize_node(irn);
714 ir_node *be_get_FrameAddr_frame(const ir_node *node)
716 assert(be_is_FrameAddr(node));
717 return get_irn_n(node, n_be_FrameAddr_ptr);
720 ir_entity *be_get_FrameAddr_entity(const ir_node *node)
722 const be_frame_attr_t *attr = (const be_frame_attr_t*)get_irn_generic_attr_const(node);
726 ir_node *be_new_CopyKeep(ir_node *bl, ir_node *src, int n, ir_node *in_keep[])
729 ir_node **in = ALLOCAN(ir_node*, n + 1);
730 ir_graph *irg = get_Block_irg(bl);
731 const arch_register_req_t *req = arch_get_irn_register_req(src);
732 const arch_register_class_t *cls = req->cls;
733 ir_mode *mode = get_irn_mode(src);
734 be_node_attr_t *attr;
737 memcpy(&in[1], in_keep, n * sizeof(in[0]));
738 irn = new_ir_node(NULL, irg, bl, op_be_CopyKeep, mode, n + 1, in);
739 init_node_attr(irn, n + 1, 1);
740 attr = (be_node_attr_t*) get_irn_generic_attr(irn);
741 attr->exc.pin_state = op_pin_state_floats;
742 be_node_set_reg_class_in(irn, 0, cls);
743 be_node_set_reg_class_out(irn, 0, cls);
748 ir_node *be_new_CopyKeep_single(ir_node *bl, ir_node *src, ir_node *keep)
750 return be_new_CopyKeep(bl, src, 1, &keep);
753 ir_node *be_get_CopyKeep_op(const ir_node *cpy)
755 return get_irn_n(cpy, n_be_CopyKeep_op);
758 void be_set_CopyKeep_op(ir_node *cpy, ir_node *op)
760 set_irn_n(cpy, n_be_CopyKeep_op, op);
763 static bool be_has_frame_entity(const ir_node *irn)
765 switch (get_irn_opcode(irn)) {
775 ir_entity *be_get_frame_entity(const ir_node *irn)
777 if (be_has_frame_entity(irn)) {
778 const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
784 int be_get_frame_offset(const ir_node *irn)
786 assert(is_be_node(irn));
787 if (be_has_frame_entity(irn)) {
788 const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
794 void be_set_MemPerm_in_entity(const ir_node *irn, int n, ir_entity *ent)
796 const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
798 assert(be_is_MemPerm(irn));
799 assert(n < be_get_MemPerm_entity_arity(irn));
801 attr->in_entities[n] = ent;
804 ir_entity* be_get_MemPerm_in_entity(const ir_node* irn, int n)
806 const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
808 assert(be_is_MemPerm(irn));
809 assert(n < be_get_MemPerm_entity_arity(irn));
811 return attr->in_entities[n];
814 void be_set_MemPerm_out_entity(const ir_node *irn, int n, ir_entity *ent)
816 const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
818 assert(be_is_MemPerm(irn));
819 assert(n < be_get_MemPerm_entity_arity(irn));
821 attr->out_entities[n] = ent;
824 ir_entity* be_get_MemPerm_out_entity(const ir_node* irn, int n)
826 const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
828 assert(be_is_MemPerm(irn));
829 assert(n < be_get_MemPerm_entity_arity(irn));
831 return attr->out_entities[n];
834 int be_get_MemPerm_entity_arity(const ir_node *irn)
836 return get_irn_arity(irn) - 1;
839 const arch_register_req_t *be_create_reg_req(struct obstack *obst,
840 const arch_register_t *reg, arch_register_req_type_t additional_types)
842 arch_register_req_t *req = OALLOC(obst, arch_register_req_t);
843 const arch_register_class_t *cls = arch_register_get_class(reg);
844 unsigned *limited_bitset;
846 limited_bitset = rbitset_obstack_alloc(obst, arch_register_class_n_regs(cls));
847 rbitset_set(limited_bitset, arch_register_get_index(reg));
849 req->type = arch_register_req_type_limited | additional_types;
851 req->limited = limited_bitset;
856 void be_set_constr_single_reg_in(ir_node *node, int pos,
857 const arch_register_t *reg, arch_register_req_type_t additional_types)
859 const arch_register_req_t *req;
861 if (additional_types == 0) {
862 req = reg->single_req;
864 ir_graph *irg = get_irn_irg(node);
865 struct obstack *obst = be_get_be_obst(irg);
866 req = be_create_reg_req(obst, reg, additional_types);
868 be_set_constr_in(node, pos, req);
871 void be_set_constr_single_reg_out(ir_node *node, int pos,
872 const arch_register_t *reg, arch_register_req_type_t additional_types)
874 ir_graph *irg = get_irn_irg(node);
875 be_irg_t *birg = be_birg_from_irg(irg);
876 const arch_register_req_t *req;
878 /* if we have an ignore register, add ignore flag and just assign it */
879 if (!rbitset_is_set(birg->allocatable_regs, reg->global_index)) {
880 additional_types |= arch_register_req_type_ignore;
883 if (additional_types == 0) {
884 req = reg->single_req;
886 struct obstack *obst = be_get_be_obst(irg);
887 req = be_create_reg_req(obst, reg, additional_types);
890 arch_set_irn_register_out(node, pos, reg);
891 be_set_constr_out(node, pos, req);
894 void be_node_set_reg_class_in(ir_node *irn, int pos,
895 const arch_register_class_t *cls)
897 be_set_constr_in(irn, pos, cls->class_req);
900 void be_node_set_reg_class_out(ir_node *irn, int pos,
901 const arch_register_class_t *cls)
903 be_set_constr_out(irn, pos, cls->class_req);
906 ir_node *be_get_IncSP_pred(ir_node *irn)
908 assert(be_is_IncSP(irn));
909 return get_irn_n(irn, 0);
912 void be_set_IncSP_pred(ir_node *incsp, ir_node *pred)
914 assert(be_is_IncSP(incsp));
915 set_irn_n(incsp, 0, pred);
918 void be_set_IncSP_offset(ir_node *irn, int offset)
920 be_incsp_attr_t *a = (be_incsp_attr_t*)get_irn_generic_attr(irn);
921 assert(be_is_IncSP(irn));
925 int be_get_IncSP_offset(const ir_node *irn)
927 const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
928 assert(be_is_IncSP(irn));
932 int be_get_IncSP_align(const ir_node *irn)
934 const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
935 assert(be_is_IncSP(irn));
939 static ir_entity *be_node_get_frame_entity(const ir_node *irn)
941 return be_get_frame_entity(irn);
944 void be_node_set_frame_entity(ir_node *irn, ir_entity *ent)
948 assert(be_has_frame_entity(irn));
950 a = (be_frame_attr_t*)get_irn_generic_attr(irn);
954 static void be_node_set_frame_offset(ir_node *irn, int offset)
958 if (!be_has_frame_entity(irn))
961 a = (be_frame_attr_t*)get_irn_generic_attr(irn);
965 static int be_node_get_sp_bias(const ir_node *irn)
967 if (be_is_IncSP(irn))
968 return be_get_IncSP_offset(irn);
970 return -(int)be_Call_get_pop(irn);
978 static const arch_irn_ops_t be_node_irn_ops = {
979 be_node_get_frame_entity,
980 be_node_set_frame_offset,
982 NULL, /* get_inverse */
983 NULL, /* get_op_estimated_cost */
984 NULL, /* possible_memory_operand */
985 NULL, /* perform_memory_operand */
988 static int get_start_reg_index(ir_graph *irg, const arch_register_t *reg)
990 ir_node *start = get_irg_start(irg);
991 unsigned n_outs = arch_get_irn_n_outs(start);
994 /* do a naive linear search... */
995 for (i = 0; i < (int)n_outs; ++i) {
996 const arch_register_req_t *out_req
997 = arch_get_irn_register_req_out(start, i);
998 if (! (out_req->type & arch_register_req_type_limited))
1000 if (out_req->cls != arch_register_get_class(reg))
1002 if (!rbitset_is_set(out_req->limited, reg->index))
1006 panic("Tried querying undefined register '%s' at Start", reg->name);
1009 ir_node *be_get_initial_reg_value(ir_graph *irg, const arch_register_t *reg)
1011 int i = get_start_reg_index(irg, reg);
1012 ir_node *start = get_irg_start(irg);
1013 ir_mode *mode = arch_register_class_mode(arch_register_get_class(reg));
1015 foreach_out_edge(start, edge) {
1016 ir_node *proj = get_edge_src_irn(edge);
1017 if (!is_Proj(proj)) // maybe End/Anchor
1019 if (get_Proj_proj(proj) == i) {
1023 return new_r_Proj(start, mode, i);
1026 int be_find_return_reg_input(ir_node *ret, const arch_register_t *reg)
1028 int arity = get_irn_arity(ret);
1030 /* do a naive linear search... */
1031 for (i = 0; i < arity; ++i) {
1032 const arch_register_req_t *req = arch_get_irn_register_req_in(ret, i);
1033 if (! (req->type & arch_register_req_type_limited))
1035 if (req->cls != arch_register_get_class(reg))
1037 if (!rbitset_is_set(req->limited, reg->index))
1041 panic("Tried querying undefined register '%s' at Return", reg->name);
1044 static ir_entity* dummy_get_frame_entity(const ir_node *node)
1050 static void dummy_set_frame_offset(ir_node *node, int bias)
1054 panic("should not be called");
1057 static int dummy_get_sp_bias(const ir_node *node)
1063 /* for "middleend" nodes */
1064 static const arch_irn_ops_t dummy_be_irn_ops = {
1065 dummy_get_frame_entity,
1066 dummy_set_frame_offset,
1068 NULL, /* get_inverse */
1069 NULL, /* get_op_estimated_cost */
1070 NULL, /* possible_memory_operand */
1071 NULL, /* perform_memory_operand */
1076 ir_node *be_new_Phi(ir_node *block, int n_ins, ir_node **ins, ir_mode *mode,
1077 const arch_register_req_t *req)
1079 ir_graph *irg = get_irn_irg(block);
1080 struct obstack *obst = be_get_be_obst(irg);
1081 backend_info_t *info;
1084 ir_node *phi = new_r_Phi(block, n_ins, ins, mode);
1085 info = be_get_info(phi);
1086 info->out_infos = NEW_ARR_D(reg_out_info_t, obst, 1);
1087 memset(info->out_infos, 0, 1 * sizeof(info->out_infos[0]));
1088 info->in_reqs = OALLOCN(obst, const arch_register_req_t*, n_ins);
1090 info->out_infos[0].req = req;
1091 for (i = 0; i < n_ins; ++i) {
1092 info->in_reqs[i] = req;
1098 void be_set_phi_reg_req(ir_node *node, const arch_register_req_t *req)
1100 int arity = get_irn_arity(node);
1103 backend_info_t *info = be_get_info(node);
1104 info->out_infos[0].req = req;
1105 for (i = 0; i < arity; ++i) {
1106 info->in_reqs[i] = req;
1109 assert(mode_is_datab(get_irn_mode(node)));
1112 void be_dump_phi_reg_reqs(FILE *F, const ir_node *node, dump_reason_t reason)
1115 case dump_node_opcode_txt:
1116 fputs(get_op_name(get_irn_op(node)), F);
1118 case dump_node_mode_txt:
1119 fprintf(F, "%s", get_mode_name(get_irn_mode(node)));
1121 case dump_node_nodeattr_txt:
1123 case dump_node_info_txt:
1125 backend_info_t *info = be_get_info(node);
1126 if (info != NULL && info->out_infos[0].req != NULL) {
1127 arch_dump_reqs_and_registers(F, node);
1137 static const arch_irn_ops_t phi_irn_ops = {
1138 dummy_get_frame_entity,
1139 dummy_set_frame_offset,
1141 NULL, /* get_inverse */
1142 NULL, /* get_op_estimated_cost */
1143 NULL, /* possible_memory_operand */
1144 NULL, /* perform_memory_operand */
1150 * ir_op-Operation: dump a be node to file
1152 static void dump_node(FILE *f, const ir_node *irn, dump_reason_t reason)
1154 assert(is_be_node(irn));
1157 case dump_node_opcode_txt:
1158 fputs(get_op_name(get_irn_op(irn)), f);
1160 case dump_node_mode_txt:
1161 if (be_is_Copy(irn) || be_is_CopyKeep(irn)) {
1162 fprintf(f, "%s", get_mode_name(get_irn_mode(irn)));
1165 case dump_node_nodeattr_txt:
1166 if (be_is_Call(irn)) {
1167 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(irn);
1169 fprintf(f, " [%s] ", get_entity_name(a->ent));
1171 if (be_is_IncSP(irn)) {
1172 const be_incsp_attr_t *attr = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
1173 fprintf(f, " [%d] ", attr->offset);
1176 case dump_node_info_txt:
1177 arch_dump_reqs_and_registers(f, irn);
1179 if (be_has_frame_entity(irn)) {
1180 const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
1182 unsigned size = get_type_size_bytes(get_entity_type(a->ent));
1183 ir_fprintf(f, "frame entity: %+F, offset 0x%x (%d), size 0x%x (%d) bytes\n",
1184 a->ent, a->offset, a->offset, size, size);
1189 switch (get_irn_opcode(irn)) {
1191 const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
1192 fprintf(f, "align: %d\n", a->align);
1193 fprintf(f, "offset: %d\n", a->offset);
1197 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(irn);
1200 fprintf(f, "\ncalling: %s\n", get_entity_name(a->ent));
1205 for (i = 0; i < be_get_MemPerm_entity_arity(irn); ++i) {
1206 ir_entity *in, *out;
1207 in = be_get_MemPerm_in_entity(irn, i);
1208 out = be_get_MemPerm_out_entity(irn, i);
1210 fprintf(f, "\nin[%d]: %s\n", i, get_entity_name(in));
1213 fprintf(f, "\nout[%d]: %s\n", i, get_entity_name(out));
1227 * Copies the backend specific attributes from old node to new node.
1229 static void copy_attr(ir_graph *irg, const ir_node *old_node, ir_node *new_node)
1231 const void *old_attr = get_irn_generic_attr_const(old_node);
1232 void *new_attr = get_irn_generic_attr(new_node);
1233 struct obstack *obst = be_get_be_obst(irg);
1234 backend_info_t *old_info = be_get_info(old_node);
1235 backend_info_t *new_info = be_get_info(new_node);
1237 assert(is_be_node(old_node));
1238 assert(is_be_node(new_node));
1240 memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
1242 new_info->flags = old_info->flags;
1243 if (old_info->out_infos != NULL) {
1244 size_t n_outs = ARR_LEN(old_info->out_infos);
1245 /* need dyanmic out infos? */
1246 if (be_is_Perm(new_node)) {
1247 new_info->out_infos = NEW_ARR_F(reg_out_info_t, n_outs);
1249 new_info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_outs);
1251 memcpy(new_info->out_infos, old_info->out_infos,
1252 n_outs * sizeof(new_info->out_infos[0]));
1254 new_info->out_infos = NULL;
1258 if (old_info->in_reqs != NULL) {
1259 unsigned n_ins = get_irn_arity(old_node);
1260 /* need dynamic in infos? */
1261 if (get_irn_op(old_node)->opar == oparity_dynamic) {
1262 new_info->in_reqs = NEW_ARR_F(const arch_register_req_t*, n_ins);
1264 new_info->in_reqs = OALLOCN(obst,const arch_register_req_t*, n_ins);
1266 memcpy(new_info->in_reqs, old_info->in_reqs,
1267 n_ins * sizeof(new_info->in_reqs[0]));
1269 new_info->in_reqs = NULL;
1273 int is_be_node(const ir_node *irn)
1275 return get_op_ops(get_irn_op(irn))->be_ops == &be_node_irn_ops;
1278 static ir_op *new_be_op(unsigned code, const char *name, op_pin_state p,
1279 irop_flags flags, op_arity opar, size_t attr_size)
1281 ir_op *res = new_ir_op(code, name, p, flags, opar, 0, attr_size);
1282 res->ops.dump_node = dump_node;
1283 res->ops.copy_attr = copy_attr;
1284 res->ops.be_ops = &be_node_irn_ops;
1288 void be_init_op(void)
1292 assert(op_be_Spill == NULL);
1294 /* Acquire all needed opcodes. */
1295 op_be_Spill = new_be_op(beo_Spill, "be_Spill", op_pin_state_exc_pinned, irop_flag_none, oparity_unary, sizeof(be_frame_attr_t));
1296 op_be_Reload = new_be_op(beo_Reload, "be_Reload", op_pin_state_exc_pinned, irop_flag_none, oparity_zero, sizeof(be_frame_attr_t));
1297 op_be_Perm = new_be_op(beo_Perm, "be_Perm", op_pin_state_exc_pinned, irop_flag_none, oparity_variable, sizeof(be_node_attr_t));
1298 op_be_MemPerm = new_be_op(beo_MemPerm, "be_MemPerm", op_pin_state_exc_pinned, irop_flag_none, oparity_variable, sizeof(be_memperm_attr_t));
1299 op_be_Copy = new_be_op(beo_Copy, "be_Copy", op_pin_state_exc_pinned, irop_flag_none, oparity_unary, sizeof(be_node_attr_t));
1300 op_be_Keep = new_be_op(beo_Keep, "be_Keep", op_pin_state_exc_pinned, irop_flag_keep, oparity_dynamic, sizeof(be_node_attr_t));
1301 op_be_CopyKeep = new_be_op(beo_CopyKeep, "be_CopyKeep", op_pin_state_exc_pinned, irop_flag_keep, oparity_variable, sizeof(be_node_attr_t));
1302 op_be_Call = new_be_op(beo_Call, "be_Call", op_pin_state_exc_pinned, irop_flag_fragile|irop_flag_uses_memory, oparity_variable, sizeof(be_call_attr_t));
1303 ir_op_set_memory_index(op_be_Call, n_be_Call_mem);
1304 ir_op_set_fragile_indices(op_be_Call, pn_be_Call_X_regular, pn_be_Call_X_except);
1305 op_be_Return = new_be_op(beo_Return, "be_Return", op_pin_state_exc_pinned, irop_flag_cfopcode, oparity_variable, sizeof(be_return_attr_t));
1306 op_be_AddSP = new_be_op(beo_AddSP, "be_AddSP", op_pin_state_exc_pinned, irop_flag_none, oparity_unary, sizeof(be_node_attr_t));
1307 op_be_SubSP = new_be_op(beo_SubSP, "be_SubSP", op_pin_state_exc_pinned, irop_flag_none, oparity_unary, sizeof(be_node_attr_t));
1308 op_be_IncSP = new_be_op(beo_IncSP, "be_IncSP", op_pin_state_exc_pinned, irop_flag_none, oparity_unary, sizeof(be_incsp_attr_t));
1309 op_be_Start = new_be_op(beo_Start, "be_Start", op_pin_state_exc_pinned, irop_flag_none, oparity_zero, sizeof(be_node_attr_t));
1310 op_be_FrameAddr = new_be_op(beo_FrameAddr, "be_FrameAddr", op_pin_state_exc_pinned, irop_flag_none, oparity_unary, sizeof(be_frame_attr_t));
1312 op_be_Spill->ops.node_cmp_attr = FrameAddr_cmp_attr;
1313 op_be_Reload->ops.node_cmp_attr = FrameAddr_cmp_attr;
1314 op_be_Perm->ops.node_cmp_attr = be_nodes_equal;
1315 op_be_MemPerm->ops.node_cmp_attr = be_nodes_equal;
1316 op_be_Copy->ops.node_cmp_attr = be_nodes_equal;
1317 op_be_Keep->ops.node_cmp_attr = be_nodes_equal;
1318 op_be_CopyKeep->ops.node_cmp_attr = be_nodes_equal;
1319 op_be_Call->ops.node_cmp_attr = Call_cmp_attr;
1320 op_be_Return->ops.node_cmp_attr = Return_cmp_attr;
1321 op_be_AddSP->ops.node_cmp_attr = be_nodes_equal;
1322 op_be_SubSP->ops.node_cmp_attr = be_nodes_equal;
1323 op_be_IncSP->ops.node_cmp_attr = IncSP_cmp_attr;
1324 op_be_Start->ops.node_cmp_attr = be_nodes_equal;
1325 op_be_FrameAddr->ops.node_cmp_attr = FrameAddr_cmp_attr;
1327 /* attach out dummy_ops to middle end nodes */
1328 for (opc = iro_First; opc <= iro_Last; ++opc) {
1329 ir_op *op = ir_get_opcode(opc);
1330 assert(op->ops.be_ops == NULL);
1331 op->ops.be_ops = &dummy_be_irn_ops;
1334 op_Phi->ops.be_ops = &phi_irn_ops;
1337 void be_finish_op(void)
1339 free_ir_op(op_be_Spill); op_be_Spill = NULL;
1340 free_ir_op(op_be_Reload); op_be_Reload = NULL;
1341 free_ir_op(op_be_Perm); op_be_Perm = NULL;
1342 free_ir_op(op_be_MemPerm); op_be_MemPerm = NULL;
1343 free_ir_op(op_be_Copy); op_be_Copy = NULL;
1344 free_ir_op(op_be_Keep); op_be_Keep = NULL;
1345 free_ir_op(op_be_CopyKeep); op_be_CopyKeep = NULL;
1346 free_ir_op(op_be_Call); op_be_Call = NULL;
1347 free_ir_op(op_be_Return); op_be_Return = NULL;
1348 free_ir_op(op_be_IncSP); op_be_IncSP = NULL;
1349 free_ir_op(op_be_AddSP); op_be_AddSP = NULL;
1350 free_ir_op(op_be_SubSP); op_be_SubSP = NULL;
1351 free_ir_op(op_be_Start); op_be_Start = NULL;
1352 free_ir_op(op_be_FrameAddr); op_be_FrameAddr = NULL;