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_out(ir_node *node)
244 backend_info_t *info = be_get_info(node);
245 reg_out_info_t out_info;
246 memset(&out_info, 0, sizeof(out_info));
247 out_info.req = arch_no_register_req;
248 ARR_APP1(reg_out_info_t, info->out_infos, out_info);
251 static void add_register_req_in(ir_node *node)
253 backend_info_t *info = be_get_info(node);
254 ARR_APP1(const arch_register_req_t*, info->in_reqs, arch_no_register_req);
257 ir_node *be_new_Spill(const arch_register_class_t *cls,
258 const arch_register_class_t *cls_frame, ir_node *bl,
259 ir_node *frame, ir_node *to_spill)
264 ir_graph *irg = get_Block_irg(bl);
268 res = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
269 init_node_attr(res, 2, 1);
270 a = (be_frame_attr_t*) get_irn_generic_attr(res);
273 a->base.exc.pin_state = op_pin_state_pinned;
275 be_node_set_reg_class_in(res, n_be_Spill_frame, cls_frame);
276 be_node_set_reg_class_in(res, n_be_Spill_val, cls);
278 * For spills and reloads, we return "none" as requirement for frame
279 * pointer, so every input is ok. Some backends need this (STA).
280 * Matze: we should investigate if this is really needed, this solution
281 * looks very hacky to me
283 be_set_constr_in(res, n_be_Spill_frame, arch_no_register_req);
285 arch_set_irn_register_req_out(res, 0, arch_no_register_req);
290 ir_node *be_new_Reload(const arch_register_class_t *cls,
291 const arch_register_class_t *cls_frame, ir_node *block,
292 ir_node *frame, ir_node *mem, ir_mode *mode)
296 ir_graph *irg = get_Block_irg(block);
301 res = new_ir_node(NULL, irg, block, op_be_Reload, mode, 2, in);
303 init_node_attr(res, 2, 1);
304 be_node_set_reg_class_out(res, 0, cls);
306 be_node_set_reg_class_in(res, n_be_Reload_frame, cls_frame);
307 arch_set_irn_flags(res, arch_irn_flags_rematerializable);
309 a = (be_frame_attr_t*) get_irn_generic_attr(res);
312 a->base.exc.pin_state = op_pin_state_pinned;
315 * For spills and reloads, we return "none" as requirement for frame
316 * pointer, so every input is ok. Some backends need this (e.g. STA).
317 * Matze: we should investigate if this is really needed, this solution
318 * looks very hacky to me
320 be_set_constr_in(res, n_be_Reload_frame, arch_no_register_req);
325 ir_node *be_get_Reload_mem(const ir_node *irn)
327 assert(be_is_Reload(irn));
328 return get_irn_n(irn, n_be_Reload_mem);
331 ir_node *be_get_Reload_frame(const ir_node *irn)
333 assert(be_is_Reload(irn));
334 return get_irn_n(irn, n_be_Reload_frame);
337 ir_node *be_get_Spill_val(const ir_node *irn)
339 assert(be_is_Spill(irn));
340 return get_irn_n(irn, n_be_Spill_val);
343 ir_node *be_get_Spill_frame(const ir_node *irn)
345 assert(be_is_Spill(irn));
346 return get_irn_n(irn, n_be_Spill_frame);
349 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_node *block,
350 int n, ir_node *in[])
353 ir_graph *irg = get_Block_irg(block);
354 be_node_attr_t *attr;
356 ir_node *irn = new_ir_node(NULL, irg, block, op_be_Perm, mode_T, n, in);
357 init_node_attr(irn, n, n);
358 attr = (be_node_attr_t*) get_irn_generic_attr(irn);
359 attr->exc.pin_state = op_pin_state_pinned;
360 for (i = 0; i < n; ++i) {
361 const ir_node *input = in[i];
362 const arch_register_req_t *req = arch_get_irn_register_req(input);
363 if (req->width == 1) {
364 be_set_constr_in(irn, i, cls->class_req);
365 be_set_constr_out(irn, i, cls->class_req);
367 arch_register_req_t *new_req = allocate_reg_req(irn);
369 new_req->type = (req->type & arch_register_req_type_aligned);
370 new_req->width = req->width;
371 be_set_constr_in(irn, i, new_req);
372 be_set_constr_out(irn, i, new_req);
379 void be_Perm_reduce(ir_node *perm, int new_size, int *map)
381 int arity = get_irn_arity(perm);
382 const arch_register_req_t **old_in_reqs
383 = ALLOCAN(const arch_register_req_t*, arity);
384 reg_out_info_t *old_infos = ALLOCAN(reg_out_info_t, arity);
385 backend_info_t *info = be_get_info(perm);
389 assert(be_is_Perm(perm));
390 assert(new_size <= arity);
392 new_in = ALLOCAN(ir_node*, new_size);
394 /* save the old register data */
395 memcpy(old_in_reqs, info->in_reqs, arity * sizeof(old_in_reqs[0]));
396 memcpy(old_infos, info->out_infos, arity * sizeof(old_infos[0]));
398 /* compose the new in array and set the new register data directly */
399 for (i = 0; i < new_size; ++i) {
401 new_in[i] = get_irn_n(perm, idx);
402 info->in_reqs[i] = old_in_reqs[idx];
403 info->out_infos[i] = old_infos[idx];
406 set_irn_in(perm, new_size, new_in);
409 ir_node *be_new_MemPerm(ir_node *block, int n, ir_node *in[])
411 ir_graph *irg = get_Block_irg(block);
412 const arch_env_t *arch_env = be_get_irg_arch_env(irg);
413 ir_node *frame = get_irg_frame(irg);
414 const arch_register_t *sp = arch_env->sp;
416 be_memperm_attr_t *attr;
419 real_in = ALLOCAN(ir_node*, n + 1);
421 memcpy(&real_in[1], in, n * sizeof(real_in[0]));
423 irn = new_ir_node(NULL, irg, block, op_be_MemPerm, mode_T, n+1, real_in);
425 init_node_attr(irn, n + 1, n);
426 be_node_set_reg_class_in(irn, 0, sp->reg_class);
428 attr = (be_memperm_attr_t*)get_irn_generic_attr(irn);
429 attr->in_entities = OALLOCNZ(irg->obst, ir_entity*, n);
430 attr->out_entities = OALLOCNZ(irg->obst, ir_entity*, n);
435 ir_node *be_new_Copy(ir_node *bl, ir_node *op)
439 arch_register_req_t *req;
440 be_node_attr_t *attr;
441 ir_graph *irg = get_Block_irg(bl);
442 const arch_register_req_t *in_req = arch_get_irn_register_req(op);
443 const arch_register_class_t *cls = in_req->cls;
446 res = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
447 init_node_attr(res, 1, 1);
448 attr = (be_node_attr_t*) get_irn_generic_attr(res);
449 attr->exc.pin_state = op_pin_state_floats;
450 be_node_set_reg_class_in(res, 0, cls);
451 be_node_set_reg_class_out(res, 0, cls);
453 req = allocate_reg_req(res);
455 req->type = arch_register_req_type_should_be_same
456 | (in_req->type & arch_register_req_type_aligned);
457 req->other_same = 1U << 0;
458 req->width = in_req->width;
459 be_set_constr_out(res, 0, req);
464 ir_node *be_get_Copy_op(const ir_node *cpy)
466 return get_irn_n(cpy, n_be_Copy_op);
469 void be_set_Copy_op(ir_node *cpy, ir_node *op)
471 set_irn_n(cpy, n_be_Copy_op, op);
474 ir_node *be_new_Keep(ir_node *block, int n, ir_node *in[])
478 ir_graph *irg = get_Block_irg(block);
479 be_node_attr_t *attr;
481 res = new_ir_node(NULL, irg, block, op_be_Keep, mode_ANY, -1, NULL);
482 init_node_attr(res, -1, 1);
483 attr = (be_node_attr_t*) get_irn_generic_attr(res);
484 attr->exc.pin_state = op_pin_state_pinned;
486 for (i = 0; i < n; ++i) {
487 add_irn_n(res, in[i]);
488 add_register_req_in(res);
495 void be_Keep_add_node(ir_node *keep, const arch_register_class_t *cls, ir_node *node)
499 assert(be_is_Keep(keep));
500 n = add_irn_n(keep, node);
501 add_register_req_in(keep);
502 be_node_set_reg_class_in(keep, n, cls);
505 ir_node *be_new_Call(dbg_info *dbg, ir_graph *irg, ir_node *bl, ir_node *mem,
506 ir_node *sp, ir_node *ptr, int n_outs, int n, ir_node *in[],
510 int real_n = n_be_Call_first_arg + n;
514 NEW_ARR_A(ir_node *, real_in, real_n);
515 real_in[n_be_Call_mem] = mem;
516 real_in[n_be_Call_sp] = sp;
517 real_in[n_be_Call_ptr] = ptr;
518 memcpy(&real_in[n_be_Call_first_arg], in, n * sizeof(in[0]));
520 irn = new_ir_node(dbg, irg, bl, op_be_Call, mode_T, real_n, real_in);
521 init_node_attr(irn, real_n, n_outs);
522 a = (be_call_attr_t*)get_irn_generic_attr(irn);
524 a->call_tp = call_tp;
526 a->base.exc.pin_state = op_pin_state_pinned;
530 ir_entity *be_Call_get_entity(const ir_node *call)
532 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
533 assert(be_is_Call(call));
537 void be_Call_set_entity(ir_node *call, ir_entity *ent)
539 be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
540 assert(be_is_Call(call));
544 ir_type *be_Call_get_type(ir_node *call)
546 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
547 assert(be_is_Call(call));
551 void be_Call_set_type(ir_node *call, ir_type *call_tp)
553 be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
554 assert(be_is_Call(call));
555 a->call_tp = call_tp;
558 void be_Call_set_pop(ir_node *call, unsigned pop)
560 be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
564 unsigned be_Call_get_pop(const ir_node *call)
566 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
570 ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *block, int n_res,
571 unsigned pop, int n, ir_node *in[])
577 res = new_ir_node(dbg, irg, block, op_be_Return, mode_X, -1, NULL);
578 init_node_attr(res, -1, 1);
579 for (i = 0; i < n; ++i) {
580 add_irn_n(res, in[i]);
581 add_register_req_in(res);
583 be_set_constr_out(res, 0, arch_no_register_req);
585 a = (be_return_attr_t*)get_irn_generic_attr(res);
586 a->num_ret_vals = n_res;
589 a->base.exc.pin_state = op_pin_state_pinned;
594 int be_Return_get_n_rets(const ir_node *ret)
596 const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
597 return a->num_ret_vals;
600 unsigned be_Return_get_pop(const ir_node *ret)
602 const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
606 int be_Return_get_emit_pop(const ir_node *ret)
608 const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
612 void be_Return_set_emit_pop(ir_node *ret, int emit_pop)
614 be_return_attr_t *a = (be_return_attr_t*)get_irn_generic_attr(ret);
615 a->emit_pop = emit_pop;
618 int be_Return_append_node(ir_node *ret, ir_node *node)
622 pos = add_irn_n(ret, node);
623 add_register_req_in(ret);
628 ir_node *be_new_IncSP(const arch_register_t *sp, ir_node *bl,
629 ir_node *old_sp, int offset, int align)
634 ir_graph *irg = get_Block_irg(bl);
637 irn = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode,
639 init_node_attr(irn, 1, 1);
640 a = (be_incsp_attr_t*)get_irn_generic_attr(irn);
643 a->base.exc.pin_state = op_pin_state_pinned;
645 /* Set output constraint to stack register. */
646 be_node_set_reg_class_in(irn, 0, sp->reg_class);
647 be_set_constr_single_reg_out(irn, 0, sp, arch_register_req_type_produces_sp);
652 ir_node *be_new_AddSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp,
656 ir_node *in[n_be_AddSP_last];
658 be_node_attr_t *attr;
660 in[n_be_AddSP_old_sp] = old_sp;
661 in[n_be_AddSP_size] = sz;
663 irg = get_Block_irg(bl);
664 irn = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T, n_be_AddSP_last, in);
665 init_node_attr(irn, n_be_AddSP_last, pn_be_AddSP_last);
666 attr = (be_node_attr_t*) get_irn_generic_attr(irn);
667 attr->exc.pin_state = op_pin_state_pinned;
669 /* Set output constraint to stack register. */
670 be_set_constr_single_reg_in(irn, n_be_AddSP_old_sp, sp,
671 arch_register_req_type_none);
672 be_node_set_reg_class_in(irn, n_be_AddSP_size, sp->reg_class);
673 be_set_constr_single_reg_out(irn, pn_be_AddSP_sp, sp,
674 arch_register_req_type_produces_sp);
679 ir_node *be_new_SubSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp, ir_node *sz)
682 ir_node *in[n_be_SubSP_last];
684 be_node_attr_t *attr;
686 in[n_be_SubSP_old_sp] = old_sp;
687 in[n_be_SubSP_size] = sz;
689 irg = get_Block_irg(bl);
690 irn = new_ir_node(NULL, irg, bl, op_be_SubSP, mode_T, n_be_SubSP_last, in);
691 init_node_attr(irn, n_be_SubSP_last, pn_be_SubSP_last);
692 attr = (be_node_attr_t*) get_irn_generic_attr(irn);
693 attr->exc.pin_state = op_pin_state_pinned;
695 /* Set output constraint to stack register. */
696 be_set_constr_single_reg_in(irn, n_be_SubSP_old_sp, sp,
697 arch_register_req_type_none);
698 be_node_set_reg_class_in(irn, n_be_SubSP_size, sp->reg_class);
699 be_set_constr_single_reg_out(irn, pn_be_SubSP_sp, sp, arch_register_req_type_produces_sp);
704 ir_node *be_new_Start(dbg_info *dbgi, ir_node *bl, int n_outs)
708 ir_graph *irg = get_Block_irg(bl);
709 be_node_attr_t *attr;
711 res = new_ir_node(dbgi, irg, bl, op_be_Start, mode_T, 0, NULL);
712 init_node_attr(res, 0, -1);
713 attr = (be_node_attr_t*) get_irn_generic_attr(res);
714 attr->exc.pin_state = op_pin_state_pinned;
715 for (i = 0; i < n_outs; ++i) {
716 add_register_req_out(res);
722 ir_node *be_new_FrameAddr(const arch_register_class_t *cls_frame, ir_node *bl, ir_node *frame, ir_entity *ent)
727 ir_graph *irg = get_Block_irg(bl);
730 irn = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
731 init_node_attr(irn, 1, 1);
732 a = (be_frame_attr_t*)get_irn_generic_attr(irn);
735 a->base.exc.pin_state = op_pin_state_floats;
736 be_node_set_reg_class_in(irn, 0, cls_frame);
737 be_node_set_reg_class_out(irn, 0, cls_frame);
739 return optimize_node(irn);
742 ir_node *be_get_FrameAddr_frame(const ir_node *node)
744 assert(be_is_FrameAddr(node));
745 return get_irn_n(node, n_be_FrameAddr_ptr);
748 ir_entity *be_get_FrameAddr_entity(const ir_node *node)
750 const be_frame_attr_t *attr = (const be_frame_attr_t*)get_irn_generic_attr_const(node);
754 ir_node *be_new_CopyKeep(ir_node *bl, ir_node *src, int n, ir_node *in_keep[])
757 ir_node **in = ALLOCAN(ir_node*, n + 1);
758 ir_graph *irg = get_Block_irg(bl);
759 const arch_register_req_t *req = arch_get_irn_register_req(src);
760 const arch_register_class_t *cls = req->cls;
761 ir_mode *mode = get_irn_mode(src);
762 be_node_attr_t *attr;
765 memcpy(&in[1], in_keep, n * sizeof(in[0]));
766 irn = new_ir_node(NULL, irg, bl, op_be_CopyKeep, mode, n + 1, in);
767 init_node_attr(irn, n + 1, 1);
768 attr = (be_node_attr_t*) get_irn_generic_attr(irn);
769 attr->exc.pin_state = op_pin_state_floats;
770 be_node_set_reg_class_in(irn, 0, cls);
771 be_node_set_reg_class_out(irn, 0, cls);
776 ir_node *be_new_CopyKeep_single(ir_node *bl, ir_node *src, ir_node *keep)
778 return be_new_CopyKeep(bl, src, 1, &keep);
781 ir_node *be_get_CopyKeep_op(const ir_node *cpy)
783 return get_irn_n(cpy, n_be_CopyKeep_op);
786 void be_set_CopyKeep_op(ir_node *cpy, ir_node *op)
788 set_irn_n(cpy, n_be_CopyKeep_op, op);
791 static bool be_has_frame_entity(const ir_node *irn)
793 switch (get_irn_opcode(irn)) {
803 ir_entity *be_get_frame_entity(const ir_node *irn)
805 if (be_has_frame_entity(irn)) {
806 const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
812 int be_get_frame_offset(const ir_node *irn)
814 assert(is_be_node(irn));
815 if (be_has_frame_entity(irn)) {
816 const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
822 void be_set_MemPerm_in_entity(const ir_node *irn, int n, ir_entity *ent)
824 const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
826 assert(be_is_MemPerm(irn));
827 assert(n < be_get_MemPerm_entity_arity(irn));
829 attr->in_entities[n] = ent;
832 ir_entity* be_get_MemPerm_in_entity(const ir_node* irn, int n)
834 const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
836 assert(be_is_MemPerm(irn));
837 assert(n < be_get_MemPerm_entity_arity(irn));
839 return attr->in_entities[n];
842 void be_set_MemPerm_out_entity(const ir_node *irn, int n, ir_entity *ent)
844 const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
846 assert(be_is_MemPerm(irn));
847 assert(n < be_get_MemPerm_entity_arity(irn));
849 attr->out_entities[n] = ent;
852 ir_entity* be_get_MemPerm_out_entity(const ir_node* irn, int n)
854 const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
856 assert(be_is_MemPerm(irn));
857 assert(n < be_get_MemPerm_entity_arity(irn));
859 return attr->out_entities[n];
862 int be_get_MemPerm_entity_arity(const ir_node *irn)
864 return get_irn_arity(irn) - 1;
867 const arch_register_req_t *be_create_reg_req(struct obstack *obst,
868 const arch_register_t *reg, arch_register_req_type_t additional_types)
870 arch_register_req_t *req = OALLOC(obst, arch_register_req_t);
871 const arch_register_class_t *cls = arch_register_get_class(reg);
872 unsigned *limited_bitset;
874 limited_bitset = rbitset_obstack_alloc(obst, arch_register_class_n_regs(cls));
875 rbitset_set(limited_bitset, arch_register_get_index(reg));
877 req->type = arch_register_req_type_limited | additional_types;
879 req->limited = limited_bitset;
884 void be_set_constr_single_reg_in(ir_node *node, int pos,
885 const arch_register_t *reg, arch_register_req_type_t additional_types)
887 const arch_register_req_t *req;
889 if (additional_types == 0) {
890 req = reg->single_req;
892 ir_graph *irg = get_irn_irg(node);
893 struct obstack *obst = be_get_be_obst(irg);
894 req = be_create_reg_req(obst, reg, additional_types);
896 be_set_constr_in(node, pos, req);
899 void be_set_constr_single_reg_out(ir_node *node, int pos,
900 const arch_register_t *reg, arch_register_req_type_t additional_types)
902 ir_graph *irg = get_irn_irg(node);
903 be_irg_t *birg = be_birg_from_irg(irg);
904 const arch_register_req_t *req;
906 /* if we have an ignore register, add ignore flag and just assign it */
907 if (!rbitset_is_set(birg->allocatable_regs, reg->global_index)) {
908 additional_types |= arch_register_req_type_ignore;
911 if (additional_types == 0) {
912 req = reg->single_req;
914 struct obstack *obst = be_get_be_obst(irg);
915 req = be_create_reg_req(obst, reg, additional_types);
918 arch_set_irn_register_out(node, pos, reg);
919 be_set_constr_out(node, pos, req);
922 void be_node_set_reg_class_in(ir_node *irn, int pos,
923 const arch_register_class_t *cls)
925 be_set_constr_in(irn, pos, cls->class_req);
928 void be_node_set_reg_class_out(ir_node *irn, int pos,
929 const arch_register_class_t *cls)
931 be_set_constr_out(irn, pos, cls->class_req);
934 ir_node *be_get_IncSP_pred(ir_node *irn)
936 assert(be_is_IncSP(irn));
937 return get_irn_n(irn, 0);
940 void be_set_IncSP_pred(ir_node *incsp, ir_node *pred)
942 assert(be_is_IncSP(incsp));
943 set_irn_n(incsp, 0, pred);
946 void be_set_IncSP_offset(ir_node *irn, int offset)
948 be_incsp_attr_t *a = (be_incsp_attr_t*)get_irn_generic_attr(irn);
949 assert(be_is_IncSP(irn));
953 int be_get_IncSP_offset(const ir_node *irn)
955 const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
956 assert(be_is_IncSP(irn));
960 int be_get_IncSP_align(const ir_node *irn)
962 const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
963 assert(be_is_IncSP(irn));
967 static ir_entity *be_node_get_frame_entity(const ir_node *irn)
969 return be_get_frame_entity(irn);
972 void be_node_set_frame_entity(ir_node *irn, ir_entity *ent)
976 assert(be_has_frame_entity(irn));
978 a = (be_frame_attr_t*)get_irn_generic_attr(irn);
982 static void be_node_set_frame_offset(ir_node *irn, int offset)
986 if (!be_has_frame_entity(irn))
989 a = (be_frame_attr_t*)get_irn_generic_attr(irn);
993 static int be_node_get_sp_bias(const ir_node *irn)
995 if (be_is_IncSP(irn))
996 return be_get_IncSP_offset(irn);
998 return -(int)be_Call_get_pop(irn);
1006 static const arch_irn_ops_t be_node_irn_ops = {
1007 be_node_get_frame_entity,
1008 be_node_set_frame_offset,
1009 be_node_get_sp_bias,
1010 NULL, /* get_inverse */
1011 NULL, /* get_op_estimated_cost */
1012 NULL, /* possible_memory_operand */
1013 NULL, /* perform_memory_operand */
1016 static int get_start_reg_index(ir_graph *irg, const arch_register_t *reg)
1018 ir_node *start = get_irg_start(irg);
1019 unsigned n_outs = arch_get_irn_n_outs(start);
1022 /* do a naive linear search... */
1023 for (i = 0; i < (int)n_outs; ++i) {
1024 const arch_register_req_t *out_req
1025 = arch_get_irn_register_req_out(start, i);
1026 if (! (out_req->type & arch_register_req_type_limited))
1028 if (out_req->cls != arch_register_get_class(reg))
1030 if (!rbitset_is_set(out_req->limited, reg->index))
1034 panic("Tried querying undefined register '%s' at Start", reg->name);
1037 ir_node *be_get_initial_reg_value(ir_graph *irg, const arch_register_t *reg)
1039 int i = get_start_reg_index(irg, reg);
1040 ir_node *start = get_irg_start(irg);
1041 ir_mode *mode = arch_register_class_mode(arch_register_get_class(reg));
1042 const ir_edge_t *edge;
1044 foreach_out_edge(start, edge) {
1045 ir_node *proj = get_edge_src_irn(edge);
1046 if (!is_Proj(proj)) // maybe End/Anchor
1048 if (get_Proj_proj(proj) == i) {
1052 return new_r_Proj(start, mode, i);
1055 int be_find_return_reg_input(ir_node *ret, const arch_register_t *reg)
1057 int arity = get_irn_arity(ret);
1059 /* do a naive linear search... */
1060 for (i = 0; i < arity; ++i) {
1061 const arch_register_req_t *req = arch_get_irn_register_req_in(ret, i);
1062 if (! (req->type & arch_register_req_type_limited))
1064 if (req->cls != arch_register_get_class(reg))
1066 if (!rbitset_is_set(req->limited, reg->index))
1070 panic("Tried querying undefined register '%s' at Return", reg->name);
1073 static ir_entity* dummy_get_frame_entity(const ir_node *node)
1079 static void dummy_set_frame_offset(ir_node *node, int bias)
1083 panic("dummy_set_frame_offset() should not be called");
1086 static int dummy_get_sp_bias(const ir_node *node)
1092 /* for "middleend" nodes */
1093 static const arch_irn_ops_t dummy_be_irn_ops = {
1094 dummy_get_frame_entity,
1095 dummy_set_frame_offset,
1097 NULL, /* get_inverse */
1098 NULL, /* get_op_estimated_cost */
1099 NULL, /* possible_memory_operand */
1100 NULL, /* perform_memory_operand */
1105 ir_node *be_new_Phi(ir_node *block, int n_ins, ir_node **ins, ir_mode *mode,
1106 const arch_register_class_t *cls)
1108 const arch_register_req_t *req;
1109 ir_graph *irg = get_irn_irg(block);
1110 struct obstack *obst = be_get_be_obst(irg);
1111 backend_info_t *info;
1114 ir_node *phi = new_r_Phi(block, n_ins, ins, mode);
1115 info = be_get_info(phi);
1116 info->out_infos = NEW_ARR_D(reg_out_info_t, obst, 1);
1117 memset(info->out_infos, 0, 1 * sizeof(info->out_infos[0]));
1118 info->in_reqs = OALLOCN(obst, const arch_register_req_t*, n_ins);
1121 req = arch_no_register_req;
1123 req = cls->class_req;
1125 info->out_infos[0].req = req;
1126 for (i = 0; i < n_ins; ++i) {
1127 info->in_reqs[i] = req;
1133 void be_set_phi_reg_req(ir_node *node, const arch_register_req_t *req)
1135 int arity = get_irn_arity(node);
1138 backend_info_t *info = be_get_info(node);
1139 info->out_infos[0].req = req;
1140 for (i = 0; i < arity; ++i) {
1141 info->in_reqs[i] = req;
1144 assert(mode_is_datab(get_irn_mode(node)));
1147 void be_dump_phi_reg_reqs(FILE *F, const ir_node *node, dump_reason_t reason)
1150 case dump_node_opcode_txt:
1151 fputs(get_op_name(get_irn_op(node)), F);
1153 case dump_node_mode_txt:
1154 fprintf(F, "%s", get_mode_name(get_irn_mode(node)));
1156 case dump_node_nodeattr_txt:
1158 case dump_node_info_txt:
1160 backend_info_t *info = be_get_info(node);
1161 if (info != NULL && info->out_infos[0].req != NULL) {
1162 arch_dump_reqs_and_registers(F, node);
1172 static const arch_irn_ops_t phi_irn_ops = {
1173 dummy_get_frame_entity,
1174 dummy_set_frame_offset,
1176 NULL, /* get_inverse */
1177 NULL, /* get_op_estimated_cost */
1178 NULL, /* possible_memory_operand */
1179 NULL, /* perform_memory_operand */
1185 * ir_op-Operation: dump a be node to file
1187 static void dump_node(FILE *f, const ir_node *irn, dump_reason_t reason)
1189 assert(is_be_node(irn));
1192 case dump_node_opcode_txt:
1193 fputs(get_op_name(get_irn_op(irn)), f);
1195 case dump_node_mode_txt:
1196 if (be_is_Copy(irn) || be_is_CopyKeep(irn)) {
1197 fprintf(f, "%s", get_mode_name(get_irn_mode(irn)));
1200 case dump_node_nodeattr_txt:
1201 if (be_is_Call(irn)) {
1202 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(irn);
1204 fprintf(f, " [%s] ", get_entity_name(a->ent));
1206 if (be_is_IncSP(irn)) {
1207 const be_incsp_attr_t *attr = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
1208 fprintf(f, " [%d] ", attr->offset);
1211 case dump_node_info_txt:
1212 arch_dump_reqs_and_registers(f, irn);
1214 if (be_has_frame_entity(irn)) {
1215 const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
1217 unsigned size = get_type_size_bytes(get_entity_type(a->ent));
1218 ir_fprintf(f, "frame entity: %+F, offset 0x%x (%d), size 0x%x (%d) bytes\n",
1219 a->ent, a->offset, a->offset, size, size);
1224 switch (get_irn_opcode(irn)) {
1226 const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
1227 fprintf(f, "align: %d\n", a->align);
1228 fprintf(f, "offset: %d\n", a->offset);
1232 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(irn);
1235 fprintf(f, "\ncalling: %s\n", get_entity_name(a->ent));
1240 for (i = 0; i < be_get_MemPerm_entity_arity(irn); ++i) {
1241 ir_entity *in, *out;
1242 in = be_get_MemPerm_in_entity(irn, i);
1243 out = be_get_MemPerm_out_entity(irn, i);
1245 fprintf(f, "\nin[%d]: %s\n", i, get_entity_name(in));
1248 fprintf(f, "\nout[%d]: %s\n", i, get_entity_name(out));
1262 * Copies the backend specific attributes from old node to new node.
1264 static void copy_attr(ir_graph *irg, const ir_node *old_node, ir_node *new_node)
1266 const void *old_attr = get_irn_generic_attr_const(old_node);
1267 void *new_attr = get_irn_generic_attr(new_node);
1268 struct obstack *obst = be_get_be_obst(irg);
1269 backend_info_t *old_info = be_get_info(old_node);
1270 backend_info_t *new_info = be_get_info(new_node);
1272 assert(is_be_node(old_node));
1273 assert(is_be_node(new_node));
1275 memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
1277 new_info->flags = old_info->flags;
1278 if (old_info->out_infos != NULL) {
1279 size_t n_outs = ARR_LEN(old_info->out_infos);
1280 /* need dyanmic out infos? */
1281 if (be_is_Perm(new_node)) {
1282 new_info->out_infos = NEW_ARR_F(reg_out_info_t, n_outs);
1284 new_info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_outs);
1286 memcpy(new_info->out_infos, old_info->out_infos,
1287 n_outs * sizeof(new_info->out_infos[0]));
1289 new_info->out_infos = NULL;
1293 if (old_info->in_reqs != NULL) {
1294 unsigned n_ins = get_irn_arity(old_node);
1295 /* need dynamic in infos? */
1296 if (get_irn_op(old_node)->opar == oparity_dynamic) {
1297 new_info->in_reqs = NEW_ARR_F(const arch_register_req_t*, n_ins);
1299 new_info->in_reqs = OALLOCN(obst,const arch_register_req_t*, n_ins);
1301 memcpy(new_info->in_reqs, old_info->in_reqs,
1302 n_ins * sizeof(new_info->in_reqs[0]));
1304 new_info->in_reqs = NULL;
1308 static const ir_op_ops be_node_op_ops = {
1328 int is_be_node(const ir_node *irn)
1330 return get_op_ops(get_irn_op(irn))->be_ops == &be_node_irn_ops;
1333 void be_init_op(void)
1337 /* Acquire all needed opcodes. */
1338 op_be_Spill = new_ir_op(beo_Spill, "be_Spill", op_pin_state_exc_pinned, irop_flag_none, oparity_unary, 0, sizeof(be_frame_attr_t), &be_node_op_ops);
1339 op_be_Reload = new_ir_op(beo_Reload, "be_Reload", op_pin_state_exc_pinned, irop_flag_none, oparity_zero, 0, sizeof(be_frame_attr_t), &be_node_op_ops);
1340 op_be_Perm = new_ir_op(beo_Perm, "be_Perm", op_pin_state_exc_pinned, irop_flag_none, oparity_variable, 0, sizeof(be_node_attr_t), &be_node_op_ops);
1341 op_be_MemPerm = new_ir_op(beo_MemPerm, "be_MemPerm", op_pin_state_exc_pinned, irop_flag_none, oparity_variable, 0, sizeof(be_memperm_attr_t), &be_node_op_ops);
1342 op_be_Copy = new_ir_op(beo_Copy, "be_Copy", op_pin_state_exc_pinned, irop_flag_none, oparity_unary, 0, sizeof(be_node_attr_t), &be_node_op_ops);
1343 op_be_Keep = new_ir_op(beo_Keep, "be_Keep", op_pin_state_exc_pinned, irop_flag_keep, oparity_dynamic, 0, sizeof(be_node_attr_t), &be_node_op_ops);
1344 op_be_CopyKeep = new_ir_op(beo_CopyKeep, "be_CopyKeep", op_pin_state_exc_pinned, irop_flag_keep, oparity_variable, 0, sizeof(be_node_attr_t), &be_node_op_ops);
1345 op_be_Call = new_ir_op(beo_Call, "be_Call", op_pin_state_exc_pinned, irop_flag_fragile|irop_flag_uses_memory, oparity_variable, 0, sizeof(be_call_attr_t), &be_node_op_ops);
1346 ir_op_set_memory_index(op_be_Call, n_be_Call_mem);
1347 ir_op_set_fragile_indices(op_be_Call, pn_be_Call_X_regular, pn_be_Call_X_except);
1348 op_be_Return = new_ir_op(beo_Return, "be_Return", op_pin_state_exc_pinned, irop_flag_cfopcode, oparity_dynamic, 0, sizeof(be_return_attr_t), &be_node_op_ops);
1349 op_be_AddSP = new_ir_op(beo_AddSP, "be_AddSP", op_pin_state_exc_pinned, irop_flag_none, oparity_unary, 0, sizeof(be_node_attr_t), &be_node_op_ops);
1350 op_be_SubSP = new_ir_op(beo_SubSP, "be_SubSP", op_pin_state_exc_pinned, irop_flag_none, oparity_unary, 0, sizeof(be_node_attr_t), &be_node_op_ops);
1351 op_be_IncSP = new_ir_op(beo_IncSP, "be_IncSP", op_pin_state_exc_pinned, irop_flag_none, oparity_unary, 0, sizeof(be_incsp_attr_t), &be_node_op_ops);
1352 op_be_Start = new_ir_op(beo_Start, "be_Start", op_pin_state_exc_pinned, irop_flag_none, oparity_zero, 0, sizeof(be_node_attr_t), &be_node_op_ops);
1353 op_be_FrameAddr = new_ir_op(beo_FrameAddr, "be_FrameAddr", op_pin_state_exc_pinned, irop_flag_none, oparity_unary, 0, sizeof(be_frame_attr_t), &be_node_op_ops);
1355 op_be_Spill->ops.node_cmp_attr = FrameAddr_cmp_attr;
1356 op_be_Reload->ops.node_cmp_attr = FrameAddr_cmp_attr;
1357 op_be_Perm->ops.node_cmp_attr = be_nodes_equal;
1358 op_be_MemPerm->ops.node_cmp_attr = be_nodes_equal;
1359 op_be_Copy->ops.node_cmp_attr = be_nodes_equal;
1360 op_be_Keep->ops.node_cmp_attr = be_nodes_equal;
1361 op_be_CopyKeep->ops.node_cmp_attr = be_nodes_equal;
1362 op_be_Call->ops.node_cmp_attr = Call_cmp_attr;
1363 op_be_Return->ops.node_cmp_attr = Return_cmp_attr;
1364 op_be_AddSP->ops.node_cmp_attr = be_nodes_equal;
1365 op_be_SubSP->ops.node_cmp_attr = be_nodes_equal;
1366 op_be_IncSP->ops.node_cmp_attr = IncSP_cmp_attr;
1367 op_be_Start->ops.node_cmp_attr = be_nodes_equal;
1368 op_be_FrameAddr->ops.node_cmp_attr = FrameAddr_cmp_attr;
1370 /* attach out dummy_ops to middle end nodes */
1371 for (opc = iro_First; opc <= iro_Last; ++opc) {
1372 ir_op *op = get_irp_opcode(opc);
1373 assert(op->ops.be_ops == NULL);
1374 op->ops.be_ops = &dummy_be_irn_ops;
1377 op_Phi->ops.be_ops = &phi_irn_ops;