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.
40 #include "bitfiddle.h"
41 #include "raw_bitset.h"
52 #include "irbackedge_t.h"
53 #include "irverify_t.h"
63 typedef struct be_node_attr_t {
67 /** The be_Return nodes attribute type. */
70 int num_ret_vals; /**< number of return values */
71 unsigned pop; /**< number of bytes that should be popped */
72 int emit_pop; /**< if set, emit pop bytes, even if pop = 0 */
75 /** The be_IncSP attribute type. */
78 int offset; /**< The offset by which the stack shall be
80 int align; /**< whether stack should be aligned after the
84 /** The be_Frame attribute type. */
91 /** The be_Call attribute type. */
94 ir_entity *ent; /**< called entity if this is a static call. */
96 ir_type *call_tp; /**< call type, copied from the original Call */
101 ir_entity **in_entities;
102 ir_entity **out_entities;
108 ir_op *op_be_MemPerm;
111 ir_op *op_be_CopyKeep;
118 ir_op *op_be_FrameAddr;
121 * Compare the attributes of two be_FrameAddr nodes.
123 * @return zero if both nodes have identically attributes
125 static int FrameAddr_cmp_attr(const ir_node *a, const ir_node *b)
127 const be_frame_attr_t *a_attr = (const be_frame_attr_t*)get_irn_generic_attr_const(a);
128 const be_frame_attr_t *b_attr = (const be_frame_attr_t*)get_irn_generic_attr_const(b);
130 if (a_attr->ent != b_attr->ent || a_attr->offset != b_attr->offset)
133 return be_nodes_equal(a, b);
137 * Compare the attributes of two be_Return nodes.
139 * @return zero if both nodes have identically attributes
141 static int Return_cmp_attr(const ir_node *a, const ir_node *b)
143 const be_return_attr_t *a_attr = (const be_return_attr_t*)get_irn_generic_attr_const(a);
144 const be_return_attr_t *b_attr = (const be_return_attr_t*)get_irn_generic_attr_const(b);
146 if (a_attr->num_ret_vals != b_attr->num_ret_vals)
148 if (a_attr->pop != b_attr->pop)
150 if (a_attr->emit_pop != b_attr->emit_pop)
153 return be_nodes_equal(a, b);
157 * Compare the attributes of two be_IncSP nodes.
159 * @return zero if both nodes have identically attributes
161 static int IncSP_cmp_attr(const ir_node *a, const ir_node *b)
163 const be_incsp_attr_t *a_attr = (const be_incsp_attr_t*)get_irn_generic_attr_const(a);
164 const be_incsp_attr_t *b_attr = (const be_incsp_attr_t*)get_irn_generic_attr_const(b);
166 if (a_attr->offset != b_attr->offset)
169 return be_nodes_equal(a, b);
173 * Compare the attributes of two be_Call nodes.
175 * @return zero if both nodes have identically attributes
177 static int Call_cmp_attr(const ir_node *a, const ir_node *b)
179 const be_call_attr_t *a_attr = (const be_call_attr_t*)get_irn_generic_attr_const(a);
180 const be_call_attr_t *b_attr = (const be_call_attr_t*)get_irn_generic_attr_const(b);
182 if (a_attr->ent != b_attr->ent ||
183 a_attr->call_tp != b_attr->call_tp)
186 return be_nodes_equal(a, b);
189 static arch_register_req_t *allocate_reg_req(const ir_node *node)
191 ir_graph *irg = get_irn_irg(node);
192 struct obstack *obst = be_get_be_obst(irg);
194 arch_register_req_t *req = OALLOCZ(obst, arch_register_req_t);
198 void be_set_constr_in(ir_node *node, int pos, const arch_register_req_t *req)
200 backend_info_t *info = be_get_info(node);
201 assert(pos < get_irn_arity(node));
202 info->in_reqs[pos] = req;
205 void be_set_constr_out(ir_node *node, int pos, const arch_register_req_t *req)
207 backend_info_t *info = be_get_info(node);
208 info->out_infos[pos].req = req;
212 * Initializes the generic attribute of all be nodes and return it.
214 static void init_node_attr(ir_node *node, int n_inputs, int n_outputs)
216 ir_graph *irg = get_irn_irg(node);
217 struct obstack *obst = be_get_be_obst(irg);
218 backend_info_t *info = be_get_info(node);
219 const arch_register_req_t **in_reqs;
223 assert(n_inputs == get_irn_arity(node));
224 in_reqs = OALLOCN(obst, const arch_register_req_t*, n_inputs);
225 for (i = 0; i < n_inputs; ++i) {
226 in_reqs[i] = arch_no_register_req;
229 in_reqs = NEW_ARR_F(const arch_register_req_t*, 0);
231 info->in_reqs = in_reqs;
233 if (n_outputs >= 0) {
235 info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_outputs);
236 memset(info->out_infos, 0, n_outputs * sizeof(info->out_infos[0]));
237 for (i = 0; i < n_outputs; ++i) {
238 info->out_infos[i].req = arch_no_register_req;
241 info->out_infos = NEW_ARR_F(reg_out_info_t, 0);
245 static void add_register_req_in(ir_node *node, const arch_register_req_t *req)
247 backend_info_t *info = be_get_info(node);
248 ARR_APP1(const arch_register_req_t*, info->in_reqs, req);
251 ir_node *be_new_Spill(const arch_register_class_t *cls,
252 const arch_register_class_t *cls_frame, ir_node *bl,
253 ir_node *frame, ir_node *to_spill)
258 ir_graph *irg = get_Block_irg(bl);
262 res = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
263 init_node_attr(res, 2, 1);
264 a = (be_frame_attr_t*) get_irn_generic_attr(res);
267 a->base.exc.pin_state = op_pin_state_pinned;
269 be_node_set_reg_class_in(res, n_be_Spill_frame, cls_frame);
270 be_node_set_reg_class_in(res, n_be_Spill_val, cls);
271 arch_set_irn_register_req_out(res, 0, arch_no_register_req);
276 ir_node *be_new_Reload(const arch_register_class_t *cls,
277 const arch_register_class_t *cls_frame, ir_node *block,
278 ir_node *frame, ir_node *mem, ir_mode *mode)
282 ir_graph *irg = get_Block_irg(block);
287 res = new_ir_node(NULL, irg, block, op_be_Reload, mode, 2, in);
289 init_node_attr(res, 2, 1);
290 be_node_set_reg_class_out(res, 0, cls);
292 be_node_set_reg_class_in(res, n_be_Reload_frame, cls_frame);
293 arch_set_irn_flags(res, arch_irn_flags_rematerializable);
295 a = (be_frame_attr_t*) get_irn_generic_attr(res);
298 a->base.exc.pin_state = op_pin_state_pinned;
303 ir_node *be_get_Reload_mem(const ir_node *irn)
305 assert(be_is_Reload(irn));
306 return get_irn_n(irn, n_be_Reload_mem);
309 ir_node *be_get_Reload_frame(const ir_node *irn)
311 assert(be_is_Reload(irn));
312 return get_irn_n(irn, n_be_Reload_frame);
315 ir_node *be_get_Spill_val(const ir_node *irn)
317 assert(be_is_Spill(irn));
318 return get_irn_n(irn, n_be_Spill_val);
321 ir_node *be_get_Spill_frame(const ir_node *irn)
323 assert(be_is_Spill(irn));
324 return get_irn_n(irn, n_be_Spill_frame);
327 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_node *block,
328 int n, ir_node *in[])
331 ir_graph *irg = get_Block_irg(block);
332 be_node_attr_t *attr;
334 ir_node *irn = new_ir_node(NULL, irg, block, op_be_Perm, mode_T, n, in);
335 init_node_attr(irn, n, n);
336 attr = (be_node_attr_t*) get_irn_generic_attr(irn);
337 attr->exc.pin_state = op_pin_state_pinned;
338 for (i = 0; i < n; ++i) {
339 const ir_node *input = in[i];
340 const arch_register_req_t *req = arch_get_irn_register_req(input);
341 if (req->width == 1) {
342 be_set_constr_in(irn, i, cls->class_req);
343 be_set_constr_out(irn, i, cls->class_req);
345 arch_register_req_t *new_req = allocate_reg_req(irn);
347 new_req->type = (req->type & arch_register_req_type_aligned);
348 new_req->width = req->width;
349 be_set_constr_in(irn, i, new_req);
350 be_set_constr_out(irn, i, new_req);
357 void be_Perm_reduce(ir_node *perm, int new_size, int *map)
359 int arity = get_irn_arity(perm);
360 const arch_register_req_t **old_in_reqs
361 = ALLOCAN(const arch_register_req_t*, arity);
362 reg_out_info_t *old_infos = ALLOCAN(reg_out_info_t, arity);
363 backend_info_t *info = be_get_info(perm);
367 assert(be_is_Perm(perm));
368 assert(new_size <= arity);
370 new_in = ALLOCAN(ir_node*, new_size);
372 /* save the old register data */
373 memcpy(old_in_reqs, info->in_reqs, arity * sizeof(old_in_reqs[0]));
374 memcpy(old_infos, info->out_infos, arity * sizeof(old_infos[0]));
376 /* compose the new in array and set the new register data directly */
377 for (i = 0; i < new_size; ++i) {
379 new_in[i] = get_irn_n(perm, idx);
380 info->in_reqs[i] = old_in_reqs[idx];
381 info->out_infos[i] = old_infos[idx];
384 set_irn_in(perm, new_size, new_in);
387 ir_node *be_new_MemPerm(ir_node *block, int n, ir_node *in[])
389 ir_graph *irg = get_Block_irg(block);
390 const arch_env_t *arch_env = be_get_irg_arch_env(irg);
391 ir_node *frame = get_irg_frame(irg);
392 const arch_register_t *sp = arch_env->sp;
394 be_memperm_attr_t *attr;
397 real_in = ALLOCAN(ir_node*, n + 1);
399 memcpy(&real_in[1], in, n * sizeof(real_in[0]));
401 irn = new_ir_node(NULL, irg, block, op_be_MemPerm, mode_T, n+1, real_in);
403 init_node_attr(irn, n + 1, n);
404 be_node_set_reg_class_in(irn, 0, sp->reg_class);
406 attr = (be_memperm_attr_t*)get_irn_generic_attr(irn);
407 attr->in_entities = OALLOCNZ(irg->obst, ir_entity*, n);
408 attr->out_entities = OALLOCNZ(irg->obst, ir_entity*, n);
413 ir_node *be_new_Copy(ir_node *bl, ir_node *op)
417 arch_register_req_t *req;
418 be_node_attr_t *attr;
419 ir_graph *irg = get_Block_irg(bl);
420 const arch_register_req_t *in_req = arch_get_irn_register_req(op);
421 const arch_register_class_t *cls = in_req->cls;
424 res = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
425 init_node_attr(res, 1, 1);
426 attr = (be_node_attr_t*) get_irn_generic_attr(res);
427 attr->exc.pin_state = op_pin_state_floats;
428 be_node_set_reg_class_in(res, 0, cls);
429 be_node_set_reg_class_out(res, 0, cls);
431 req = allocate_reg_req(res);
433 req->type = arch_register_req_type_should_be_same
434 | (in_req->type & arch_register_req_type_aligned);
435 req->other_same = 1U << 0;
436 req->width = in_req->width;
437 be_set_constr_out(res, 0, req);
442 ir_node *be_get_Copy_op(const ir_node *cpy)
444 return get_irn_n(cpy, n_be_Copy_op);
447 ir_node *be_new_Keep(ir_node *block, int n, ir_node *in[])
451 ir_graph *irg = get_Block_irg(block);
452 be_node_attr_t *attr;
454 res = new_ir_node(NULL, irg, block, op_be_Keep, mode_ANY, -1, NULL);
455 init_node_attr(res, -1, 1);
456 attr = (be_node_attr_t*) get_irn_generic_attr(res);
457 attr->exc.pin_state = op_pin_state_pinned;
459 for (i = 0; i < n; ++i) {
460 ir_node *pred = in[i];
461 add_irn_n(res, pred);
462 const arch_register_req_t *req = arch_get_irn_register_req(pred);
463 req = req->cls != NULL ? req->cls->class_req : arch_no_register_req;
464 add_register_req_in(res, req);
471 void be_Keep_add_node(ir_node *keep, const arch_register_class_t *cls, ir_node *node)
473 assert(be_is_Keep(keep));
474 add_irn_n(keep, node);
475 add_register_req_in(keep, cls->class_req);
478 ir_node *be_new_Call(dbg_info *dbg, ir_graph *irg, ir_node *bl, ir_node *mem,
479 const arch_register_req_t *sp_req, ir_node *sp,
480 const arch_register_req_t *ptr_req, ir_node *ptr,
481 int n_outs, int n, ir_node *in[], ir_type *call_tp)
484 int real_n = n_be_Call_first_arg + n;
488 NEW_ARR_A(ir_node *, real_in, real_n);
489 real_in[n_be_Call_mem] = mem;
490 real_in[n_be_Call_sp] = sp;
491 real_in[n_be_Call_ptr] = ptr;
492 memcpy(&real_in[n_be_Call_first_arg], in, n * sizeof(in[0]));
494 irn = new_ir_node(dbg, irg, bl, op_be_Call, mode_T, real_n, real_in);
495 init_node_attr(irn, real_n, n_outs);
496 a = (be_call_attr_t*)get_irn_generic_attr(irn);
498 a->call_tp = call_tp;
500 a->base.exc.pin_state = op_pin_state_pinned;
501 be_set_constr_in(irn, n_be_Call_sp, sp_req);
502 be_set_constr_in(irn, n_be_Call_ptr, ptr_req);
506 ir_entity *be_Call_get_entity(const ir_node *call)
508 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
509 assert(be_is_Call(call));
513 void be_Call_set_entity(ir_node *call, ir_entity *ent)
515 be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
516 assert(be_is_Call(call));
520 ir_type *be_Call_get_type(ir_node *call)
522 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
523 assert(be_is_Call(call));
527 void be_Call_set_type(ir_node *call, ir_type *call_tp)
529 be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
530 assert(be_is_Call(call));
531 a->call_tp = call_tp;
534 void be_Call_set_pop(ir_node *call, unsigned pop)
536 be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
540 unsigned be_Call_get_pop(const ir_node *call)
542 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
546 ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *block, int n_res,
547 unsigned pop, int n, ir_node *in[])
552 res = new_ir_node(dbg, irg, block, op_be_Return, mode_X, n, in);
553 init_node_attr(res, n, 1);
554 be_set_constr_out(res, 0, arch_no_register_req);
556 a = (be_return_attr_t*)get_irn_generic_attr(res);
557 a->num_ret_vals = n_res;
560 a->base.exc.pin_state = op_pin_state_pinned;
565 int be_Return_get_n_rets(const ir_node *ret)
567 const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
568 return a->num_ret_vals;
571 unsigned be_Return_get_pop(const ir_node *ret)
573 const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
577 int be_Return_get_emit_pop(const ir_node *ret)
579 const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
583 void be_Return_set_emit_pop(ir_node *ret, int emit_pop)
585 be_return_attr_t *a = (be_return_attr_t*)get_irn_generic_attr(ret);
586 a->emit_pop = emit_pop;
589 ir_node *be_new_IncSP(const arch_register_t *sp, ir_node *bl,
590 ir_node *old_sp, int offset, int align)
595 ir_graph *irg = get_Block_irg(bl);
598 irn = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode,
600 init_node_attr(irn, 1, 1);
601 a = (be_incsp_attr_t*)get_irn_generic_attr(irn);
604 a->base.exc.pin_state = op_pin_state_pinned;
606 /* Set output constraint to stack register. */
607 be_node_set_reg_class_in(irn, 0, sp->reg_class);
608 be_set_constr_single_reg_out(irn, 0, sp, arch_register_req_type_produces_sp);
613 ir_node *be_new_AddSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp,
617 ir_node *in[n_be_AddSP_last];
619 be_node_attr_t *attr;
621 in[n_be_AddSP_old_sp] = old_sp;
622 in[n_be_AddSP_size] = sz;
624 irg = get_Block_irg(bl);
625 irn = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T, n_be_AddSP_last, in);
626 init_node_attr(irn, n_be_AddSP_last, pn_be_AddSP_last);
627 attr = (be_node_attr_t*) get_irn_generic_attr(irn);
628 attr->exc.pin_state = op_pin_state_pinned;
630 /* Set output constraint to stack register. */
631 be_set_constr_single_reg_in(irn, n_be_AddSP_old_sp, sp,
632 arch_register_req_type_none);
633 be_node_set_reg_class_in(irn, n_be_AddSP_size, sp->reg_class);
634 be_set_constr_single_reg_out(irn, pn_be_AddSP_sp, sp,
635 arch_register_req_type_produces_sp);
640 ir_node *be_new_SubSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp, ir_node *sz)
643 ir_node *in[n_be_SubSP_last];
645 be_node_attr_t *attr;
647 in[n_be_SubSP_old_sp] = old_sp;
648 in[n_be_SubSP_size] = sz;
650 irg = get_Block_irg(bl);
651 irn = new_ir_node(NULL, irg, bl, op_be_SubSP, mode_T, n_be_SubSP_last, in);
652 init_node_attr(irn, n_be_SubSP_last, pn_be_SubSP_last);
653 attr = (be_node_attr_t*) get_irn_generic_attr(irn);
654 attr->exc.pin_state = op_pin_state_pinned;
656 /* Set output constraint to stack register. */
657 be_set_constr_single_reg_in(irn, n_be_SubSP_old_sp, sp,
658 arch_register_req_type_none);
659 be_node_set_reg_class_in(irn, n_be_SubSP_size, sp->reg_class);
660 be_set_constr_single_reg_out(irn, pn_be_SubSP_sp, sp, arch_register_req_type_produces_sp);
665 ir_node *be_new_Start(dbg_info *dbgi, ir_node *bl, int n_outs)
668 ir_graph *irg = get_Block_irg(bl);
669 be_node_attr_t *attr;
671 res = new_ir_node(dbgi, irg, bl, op_be_Start, mode_T, 0, NULL);
672 init_node_attr(res, 0, n_outs);
673 attr = (be_node_attr_t*) get_irn_generic_attr(res);
674 attr->exc.pin_state = op_pin_state_pinned;
679 ir_node *be_new_FrameAddr(const arch_register_class_t *cls_frame, ir_node *bl, ir_node *frame, ir_entity *ent)
684 ir_graph *irg = get_Block_irg(bl);
687 irn = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
688 init_node_attr(irn, 1, 1);
689 a = (be_frame_attr_t*)get_irn_generic_attr(irn);
692 a->base.exc.pin_state = op_pin_state_floats;
693 be_node_set_reg_class_in(irn, 0, cls_frame);
694 be_node_set_reg_class_out(irn, 0, cls_frame);
696 return optimize_node(irn);
699 ir_node *be_get_FrameAddr_frame(const ir_node *node)
701 assert(be_is_FrameAddr(node));
702 return get_irn_n(node, n_be_FrameAddr_ptr);
705 ir_entity *be_get_FrameAddr_entity(const ir_node *node)
707 const be_frame_attr_t *attr = (const be_frame_attr_t*)get_irn_generic_attr_const(node);
711 ir_node *be_new_CopyKeep(ir_node *bl, ir_node *src, int n, ir_node *in_keep[])
714 ir_node **in = ALLOCAN(ir_node*, n + 1);
715 ir_graph *irg = get_Block_irg(bl);
716 const arch_register_req_t *req = arch_get_irn_register_req(src);
717 const arch_register_class_t *cls = req->cls;
718 ir_mode *mode = get_irn_mode(src);
719 be_node_attr_t *attr;
722 memcpy(&in[1], in_keep, n * sizeof(in[0]));
723 irn = new_ir_node(NULL, irg, bl, op_be_CopyKeep, mode, n + 1, in);
724 init_node_attr(irn, n + 1, 1);
725 attr = (be_node_attr_t*) get_irn_generic_attr(irn);
726 attr->exc.pin_state = op_pin_state_floats;
727 be_node_set_reg_class_in(irn, 0, cls);
728 be_node_set_reg_class_out(irn, 0, cls);
729 for (int i = 0; i < n; ++i) {
730 ir_node *pred = in_keep[i];
731 const arch_register_req_t *req = arch_get_irn_register_req(pred);
732 req = req->cls != NULL ? req->cls->class_req : arch_no_register_req;
733 be_set_constr_in(irn, i+1, req);
739 ir_node *be_new_CopyKeep_single(ir_node *bl, ir_node *src, ir_node *keep)
741 return be_new_CopyKeep(bl, src, 1, &keep);
744 ir_node *be_get_CopyKeep_op(const ir_node *cpy)
746 return get_irn_n(cpy, n_be_CopyKeep_op);
749 void be_set_CopyKeep_op(ir_node *cpy, ir_node *op)
751 set_irn_n(cpy, n_be_CopyKeep_op, op);
754 static bool be_has_frame_entity(const ir_node *irn)
756 switch (get_irn_opcode(irn)) {
766 ir_entity *be_get_frame_entity(const ir_node *irn)
768 if (be_has_frame_entity(irn)) {
769 const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
775 int be_get_frame_offset(const ir_node *irn)
777 assert(is_be_node(irn));
778 if (be_has_frame_entity(irn)) {
779 const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
785 void be_set_MemPerm_in_entity(const ir_node *irn, int n, ir_entity *ent)
787 const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
789 assert(be_is_MemPerm(irn));
790 assert(n < be_get_MemPerm_entity_arity(irn));
792 attr->in_entities[n] = ent;
795 ir_entity* be_get_MemPerm_in_entity(const ir_node* irn, int n)
797 const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
799 assert(be_is_MemPerm(irn));
800 assert(n < be_get_MemPerm_entity_arity(irn));
802 return attr->in_entities[n];
805 void be_set_MemPerm_out_entity(const ir_node *irn, int n, ir_entity *ent)
807 const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
809 assert(be_is_MemPerm(irn));
810 assert(n < be_get_MemPerm_entity_arity(irn));
812 attr->out_entities[n] = ent;
815 ir_entity* be_get_MemPerm_out_entity(const ir_node* irn, int n)
817 const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
819 assert(be_is_MemPerm(irn));
820 assert(n < be_get_MemPerm_entity_arity(irn));
822 return attr->out_entities[n];
825 int be_get_MemPerm_entity_arity(const ir_node *irn)
827 return get_irn_arity(irn) - 1;
830 const arch_register_req_t *be_create_reg_req(struct obstack *obst,
831 const arch_register_t *reg, arch_register_req_type_t additional_types)
833 arch_register_req_t *req = OALLOC(obst, arch_register_req_t);
834 const arch_register_class_t *cls = reg->reg_class;
835 unsigned *limited_bitset;
837 limited_bitset = rbitset_obstack_alloc(obst, arch_register_class_n_regs(cls));
838 rbitset_set(limited_bitset, reg->index);
840 req->type = arch_register_req_type_limited | additional_types;
842 req->limited = limited_bitset;
847 void be_set_constr_single_reg_in(ir_node *node, int pos,
848 const arch_register_t *reg, arch_register_req_type_t additional_types)
850 const arch_register_req_t *req;
852 if (additional_types == 0) {
853 req = reg->single_req;
855 ir_graph *irg = get_irn_irg(node);
856 struct obstack *obst = be_get_be_obst(irg);
857 req = be_create_reg_req(obst, reg, additional_types);
859 be_set_constr_in(node, pos, req);
862 void be_set_constr_single_reg_out(ir_node *node, int pos,
863 const arch_register_t *reg, arch_register_req_type_t additional_types)
865 ir_graph *irg = get_irn_irg(node);
866 be_irg_t *birg = be_birg_from_irg(irg);
867 const arch_register_req_t *req;
869 /* if we have an ignore register, add ignore flag and just assign it */
870 if (!rbitset_is_set(birg->allocatable_regs, reg->global_index)) {
871 additional_types |= arch_register_req_type_ignore;
874 if (additional_types == 0) {
875 req = reg->single_req;
877 struct obstack *obst = be_get_be_obst(irg);
878 req = be_create_reg_req(obst, reg, additional_types);
881 arch_set_irn_register_out(node, pos, reg);
882 be_set_constr_out(node, pos, req);
885 void be_node_set_reg_class_in(ir_node *irn, int pos,
886 const arch_register_class_t *cls)
888 be_set_constr_in(irn, pos, cls->class_req);
891 void be_node_set_reg_class_out(ir_node *irn, int pos,
892 const arch_register_class_t *cls)
894 be_set_constr_out(irn, pos, cls->class_req);
897 ir_node *be_get_IncSP_pred(ir_node *irn)
899 assert(be_is_IncSP(irn));
900 return get_irn_n(irn, 0);
903 void be_set_IncSP_pred(ir_node *incsp, ir_node *pred)
905 assert(be_is_IncSP(incsp));
906 set_irn_n(incsp, 0, pred);
909 void be_set_IncSP_offset(ir_node *irn, int offset)
911 be_incsp_attr_t *a = (be_incsp_attr_t*)get_irn_generic_attr(irn);
912 assert(be_is_IncSP(irn));
916 int be_get_IncSP_offset(const ir_node *irn)
918 const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
919 assert(be_is_IncSP(irn));
923 int be_get_IncSP_align(const ir_node *irn)
925 const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
926 assert(be_is_IncSP(irn));
930 static ir_entity *be_node_get_frame_entity(const ir_node *irn)
932 return be_get_frame_entity(irn);
935 void be_node_set_frame_entity(ir_node *irn, ir_entity *ent)
939 assert(be_has_frame_entity(irn));
941 a = (be_frame_attr_t*)get_irn_generic_attr(irn);
945 static void be_node_set_frame_offset(ir_node *irn, int offset)
949 if (!be_has_frame_entity(irn))
952 a = (be_frame_attr_t*)get_irn_generic_attr(irn);
956 static int be_node_get_sp_bias(const ir_node *irn)
958 if (be_is_IncSP(irn))
959 return be_get_IncSP_offset(irn);
961 return -(int)be_Call_get_pop(irn);
969 static const arch_irn_ops_t be_node_irn_ops = {
970 be_node_get_frame_entity,
971 be_node_set_frame_offset,
973 NULL, /* get_op_estimated_cost */
974 NULL, /* possible_memory_operand */
975 NULL, /* perform_memory_operand */
978 static int get_start_reg_index(ir_graph *irg, const arch_register_t *reg)
980 ir_node *start = get_irg_start(irg);
981 unsigned n_outs = arch_get_irn_n_outs(start);
984 /* do a naive linear search... */
985 for (i = 0; i < (int)n_outs; ++i) {
986 const arch_register_req_t *out_req
987 = arch_get_irn_register_req_out(start, i);
988 if (! (out_req->type & arch_register_req_type_limited))
990 if (out_req->cls != reg->reg_class)
992 if (!rbitset_is_set(out_req->limited, reg->index))
996 panic("Tried querying undefined register '%s' at Start", reg->name);
999 ir_node *be_get_initial_reg_value(ir_graph *irg, const arch_register_t *reg)
1001 int i = get_start_reg_index(irg, reg);
1002 ir_node *start = get_irg_start(irg);
1003 ir_mode *mode = arch_register_class_mode(reg->reg_class);
1005 foreach_out_edge(start, edge) {
1006 ir_node *proj = get_edge_src_irn(edge);
1007 if (!is_Proj(proj)) // maybe End/Anchor
1009 if (get_Proj_proj(proj) == i) {
1013 return new_r_Proj(start, mode, i);
1016 int be_find_return_reg_input(ir_node *ret, const arch_register_t *reg)
1018 int arity = get_irn_arity(ret);
1020 /* do a naive linear search... */
1021 for (i = 0; i < arity; ++i) {
1022 const arch_register_req_t *req = arch_get_irn_register_req_in(ret, i);
1023 if (! (req->type & arch_register_req_type_limited))
1025 if (req->cls != reg->reg_class)
1027 if (!rbitset_is_set(req->limited, reg->index))
1031 panic("Tried querying undefined register '%s' at Return", reg->name);
1034 static ir_entity* dummy_get_frame_entity(const ir_node *node)
1040 static void dummy_set_frame_offset(ir_node *node, int bias)
1044 panic("should not be called");
1047 static int dummy_get_sp_bias(const ir_node *node)
1053 /* for "middleend" nodes */
1054 static const arch_irn_ops_t dummy_be_irn_ops = {
1055 dummy_get_frame_entity,
1056 dummy_set_frame_offset,
1058 NULL, /* get_op_estimated_cost */
1059 NULL, /* possible_memory_operand */
1060 NULL, /* perform_memory_operand */
1065 ir_node *be_new_Phi(ir_node *block, int n_ins, ir_node **ins, ir_mode *mode,
1066 const arch_register_req_t *req)
1068 ir_graph *irg = get_irn_irg(block);
1069 struct obstack *obst = be_get_be_obst(irg);
1070 backend_info_t *info;
1073 ir_node *phi = new_ir_node(NULL, irg, block, op_Phi, mode, n_ins, ins);
1074 phi->attr.phi.u.backedge = new_backedge_arr(irg->obst, n_ins);
1075 info = be_get_info(phi);
1076 info->out_infos = NEW_ARR_D(reg_out_info_t, obst, 1);
1077 memset(info->out_infos, 0, 1 * sizeof(info->out_infos[0]));
1078 info->in_reqs = OALLOCN(obst, const arch_register_req_t*, n_ins);
1080 info->out_infos[0].req = req;
1081 for (i = 0; i < n_ins; ++i) {
1082 info->in_reqs[i] = req;
1084 irn_verify_irg(phi, irg);
1085 phi = optimize_node(phi);
1089 void be_set_phi_reg_req(ir_node *node, const arch_register_req_t *req)
1091 int arity = get_irn_arity(node);
1094 backend_info_t *info = be_get_info(node);
1095 info->out_infos[0].req = req;
1096 for (i = 0; i < arity; ++i) {
1097 info->in_reqs[i] = req;
1100 assert(mode_is_datab(get_irn_mode(node)));
1103 void be_dump_phi_reg_reqs(FILE *F, const ir_node *node, dump_reason_t reason)
1106 case dump_node_opcode_txt:
1107 fputs(get_op_name(get_irn_op(node)), F);
1109 case dump_node_mode_txt:
1110 fprintf(F, "%s", get_mode_name(get_irn_mode(node)));
1112 case dump_node_nodeattr_txt:
1114 case dump_node_info_txt:
1116 backend_info_t *info = be_get_info(node);
1117 if (info != NULL && info->out_infos[0].req != NULL) {
1118 arch_dump_reqs_and_registers(F, node);
1128 static const arch_irn_ops_t phi_irn_ops = {
1129 dummy_get_frame_entity,
1130 dummy_set_frame_offset,
1132 NULL, /* get_op_estimated_cost */
1133 NULL, /* possible_memory_operand */
1134 NULL, /* perform_memory_operand */
1140 * ir_op-Operation: dump a be node to file
1142 static void dump_node(FILE *f, const ir_node *irn, dump_reason_t reason)
1144 assert(is_be_node(irn));
1147 case dump_node_opcode_txt:
1148 fputs(get_op_name(get_irn_op(irn)), f);
1150 case dump_node_mode_txt:
1151 if (be_is_Copy(irn) || be_is_CopyKeep(irn)) {
1152 fprintf(f, "%s", get_mode_name(get_irn_mode(irn)));
1155 case dump_node_nodeattr_txt:
1156 if (be_is_Call(irn)) {
1157 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(irn);
1159 fprintf(f, " [%s] ", get_entity_name(a->ent));
1161 if (be_is_IncSP(irn)) {
1162 const be_incsp_attr_t *attr = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
1163 fprintf(f, " [%d] ", attr->offset);
1166 case dump_node_info_txt:
1167 arch_dump_reqs_and_registers(f, irn);
1169 if (be_has_frame_entity(irn)) {
1170 const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
1172 unsigned size = get_type_size_bytes(get_entity_type(a->ent));
1173 ir_fprintf(f, "frame entity: %+F, offset 0x%x (%d), size 0x%x (%d) bytes\n",
1174 a->ent, a->offset, a->offset, size, size);
1179 switch (get_irn_opcode(irn)) {
1181 const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
1182 fprintf(f, "align: %d\n", a->align);
1183 fprintf(f, "offset: %d\n", a->offset);
1187 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(irn);
1190 fprintf(f, "\ncalling: %s\n", get_entity_name(a->ent));
1195 for (i = 0; i < be_get_MemPerm_entity_arity(irn); ++i) {
1196 ir_entity *in, *out;
1197 in = be_get_MemPerm_in_entity(irn, i);
1198 out = be_get_MemPerm_out_entity(irn, i);
1200 fprintf(f, "\nin[%d]: %s\n", i, get_entity_name(in));
1203 fprintf(f, "\nout[%d]: %s\n", i, get_entity_name(out));
1217 * Copies the backend specific attributes from old node to new node.
1219 static void copy_attr(ir_graph *irg, const ir_node *old_node, ir_node *new_node)
1221 const void *old_attr = get_irn_generic_attr_const(old_node);
1222 void *new_attr = get_irn_generic_attr(new_node);
1223 struct obstack *obst = be_get_be_obst(irg);
1224 backend_info_t *old_info = be_get_info(old_node);
1225 backend_info_t *new_info = be_get_info(new_node);
1227 assert(is_be_node(old_node));
1228 assert(is_be_node(new_node));
1230 memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
1232 new_info->flags = old_info->flags;
1233 if (old_info->out_infos != NULL) {
1234 size_t n_outs = ARR_LEN(old_info->out_infos);
1235 /* need dyanmic out infos? */
1236 if (be_is_Perm(new_node)) {
1237 new_info->out_infos = NEW_ARR_F(reg_out_info_t, n_outs);
1239 new_info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_outs);
1241 memcpy(new_info->out_infos, old_info->out_infos,
1242 n_outs * sizeof(new_info->out_infos[0]));
1244 new_info->out_infos = NULL;
1248 if (old_info->in_reqs != NULL) {
1249 unsigned n_ins = get_irn_arity(old_node);
1250 /* need dynamic in infos? */
1251 if (get_irn_op(old_node)->opar == oparity_dynamic) {
1252 new_info->in_reqs = NEW_ARR_F(const arch_register_req_t*, n_ins);
1254 new_info->in_reqs = OALLOCN(obst,const arch_register_req_t*, n_ins);
1256 memcpy(new_info->in_reqs, old_info->in_reqs,
1257 n_ins * sizeof(new_info->in_reqs[0]));
1259 new_info->in_reqs = NULL;
1263 int is_be_node(const ir_node *irn)
1265 return get_op_ops(get_irn_op(irn))->be_ops == &be_node_irn_ops;
1268 static ir_op *new_be_op(unsigned code, const char *name, op_pin_state p,
1269 irop_flags flags, op_arity opar, size_t attr_size)
1271 ir_op *res = new_ir_op(code, name, p, flags, opar, 0, attr_size);
1272 res->ops.dump_node = dump_node;
1273 res->ops.copy_attr = copy_attr;
1274 res->ops.be_ops = &be_node_irn_ops;
1278 void be_init_op(void)
1282 assert(op_be_Spill == NULL);
1284 /* Acquire all needed opcodes. */
1285 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));
1286 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));
1287 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));
1288 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));
1289 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));
1290 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));
1291 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));
1292 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));
1293 ir_op_set_memory_index(op_be_Call, n_be_Call_mem);
1294 ir_op_set_fragile_indices(op_be_Call, pn_be_Call_X_regular, pn_be_Call_X_except);
1295 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));
1296 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));
1297 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));
1298 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));
1299 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));
1300 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));
1302 op_be_Spill->ops.node_cmp_attr = FrameAddr_cmp_attr;
1303 op_be_Reload->ops.node_cmp_attr = FrameAddr_cmp_attr;
1304 op_be_Perm->ops.node_cmp_attr = be_nodes_equal;
1305 op_be_MemPerm->ops.node_cmp_attr = be_nodes_equal;
1306 op_be_Copy->ops.node_cmp_attr = be_nodes_equal;
1307 op_be_Keep->ops.node_cmp_attr = be_nodes_equal;
1308 op_be_CopyKeep->ops.node_cmp_attr = be_nodes_equal;
1309 op_be_Call->ops.node_cmp_attr = Call_cmp_attr;
1310 op_be_Return->ops.node_cmp_attr = Return_cmp_attr;
1311 op_be_AddSP->ops.node_cmp_attr = be_nodes_equal;
1312 op_be_SubSP->ops.node_cmp_attr = be_nodes_equal;
1313 op_be_IncSP->ops.node_cmp_attr = IncSP_cmp_attr;
1314 op_be_Start->ops.node_cmp_attr = be_nodes_equal;
1315 op_be_FrameAddr->ops.node_cmp_attr = FrameAddr_cmp_attr;
1317 /* attach out dummy_ops to middle end nodes */
1318 for (opc = iro_First; opc <= iro_Last; ++opc) {
1319 ir_op *op = ir_get_opcode(opc);
1320 assert(op->ops.be_ops == NULL);
1321 op->ops.be_ops = &dummy_be_irn_ops;
1324 op_Phi->ops.be_ops = &phi_irn_ops;
1327 void be_finish_op(void)
1329 free_ir_op(op_be_Spill); op_be_Spill = NULL;
1330 free_ir_op(op_be_Reload); op_be_Reload = NULL;
1331 free_ir_op(op_be_Perm); op_be_Perm = NULL;
1332 free_ir_op(op_be_MemPerm); op_be_MemPerm = NULL;
1333 free_ir_op(op_be_Copy); op_be_Copy = NULL;
1334 free_ir_op(op_be_Keep); op_be_Keep = NULL;
1335 free_ir_op(op_be_CopyKeep); op_be_CopyKeep = NULL;
1336 free_ir_op(op_be_Call); op_be_Call = NULL;
1337 free_ir_op(op_be_Return); op_be_Return = NULL;
1338 free_ir_op(op_be_IncSP); op_be_IncSP = NULL;
1339 free_ir_op(op_be_AddSP); op_be_AddSP = NULL;
1340 free_ir_op(op_be_SubSP); op_be_SubSP = NULL;
1341 free_ir_op(op_be_Start); op_be_Start = NULL;
1342 free_ir_op(op_be_FrameAddr); op_be_FrameAddr = NULL;