4 * @author Sebastian Hack
6 * Backend node support.
8 * This file provdies Perm, Copy, Spill and Reload nodes.
10 * Copyright (C) 2005 Universitaet Karlsruhe
11 * Released under the GPL
35 #include "besched_t.h"
40 /* Sometimes we want to put const nodes into get_irn_generic_attr ... */
41 #define get_irn_attr(irn) get_irn_generic_attr((ir_node *) (irn))
43 static unsigned be_node_tag = FOURCC('B', 'E', 'N', 'O');
45 typedef enum _node_kind_t {
56 const arch_register_class_t *cls;
63 be_req_kind_old_limited,
64 be_req_kind_negate_old_limited,
65 be_req_kind_single_reg
69 arch_register_req_t req;
73 void (*old_limited)(void *ptr, bitset_t *bs);
74 void *old_limited_env;
77 const arch_register_t *single_reg;
82 const arch_register_t *reg;
89 const arch_register_class_t *cls;
90 be_reg_data_t *reg_data;
94 be_node_attr_t node_attr;
95 ir_node *spill_ctx; /**< The node in whose context this spill was introduced. */
96 entity *ent; /**< The entity in the stack frame the spill writes to. */
99 static ir_op *op_Spill;
100 static ir_op *op_Reload;
101 static ir_op *op_Perm;
102 static ir_op *op_Copy;
103 static ir_op *op_Keep;
104 static ir_op *op_Call;
105 static ir_op *op_IncSP;
106 static ir_op *op_AddSP;
108 static int beo_base = -1;
110 static const ir_op_ops be_node_op_ops;
112 #define N irop_flag_none
113 #define L irop_flag_labeled
114 #define C irop_flag_commutative
115 #define X irop_flag_cfopcode
116 #define I irop_flag_ip_cfopcode
117 #define F irop_flag_fragile
118 #define Y irop_flag_forking
119 #define H irop_flag_highlevel
120 #define c irop_flag_constlike
121 #define K irop_flag_keep
123 void be_node_init(void) {
124 static int inited = 0;
131 /* Acquire all needed opcodes. */
132 beo_base = get_next_ir_opcodes(beo_Last - 1);
134 op_Spill = new_ir_op(beo_base + beo_Spill, "Spill", op_pin_state_mem_pinned, N, oparity_unary, 0, sizeof(be_spill_attr_t), &be_node_op_ops);
135 op_Reload = new_ir_op(beo_base + beo_Reload, "Reload", op_pin_state_mem_pinned, N, oparity_zero, 0, sizeof(be_node_attr_t), &be_node_op_ops);
136 op_Perm = new_ir_op(beo_base + beo_Perm, "Perm", op_pin_state_pinned, N, oparity_variable, 0, sizeof(be_node_attr_t), &be_node_op_ops);
137 op_Copy = new_ir_op(beo_base + beo_Copy, "Copy", op_pin_state_pinned, N, oparity_unary, 0, sizeof(be_node_attr_t), &be_node_op_ops);
138 op_Keep = new_ir_op(beo_base + beo_Keep, "Keep", op_pin_state_pinned, K, oparity_variable, 0, sizeof(be_node_attr_t), &be_node_op_ops);
140 set_op_tag(op_Spill, &be_node_tag);
141 set_op_tag(op_Reload, &be_node_tag);
142 set_op_tag(op_Perm, &be_node_tag);
143 set_op_tag(op_Copy, &be_node_tag);
144 set_op_tag(op_Keep, &be_node_tag);
147 static void *init_node_attr(ir_node* irn, const arch_register_class_t *cls, ir_graph *irg, int n_outs)
149 be_node_attr_t *a = get_irn_attr(irn);
158 a->reg_data = NEW_ARR_D(be_reg_data_t, get_irg_obstack(irg), n_outs);
159 memset(a->reg_data, 0, n_outs * sizeof(a->reg_data[0]));
160 for(i = 0; i < n_outs; ++i) {
161 a->reg_data[i].req.req.cls = cls;
162 a->reg_data[i].req.req.type = arch_register_req_type_normal;
169 static INLINE int is_be_node(const ir_node *irn)
171 return get_op_tag(get_irn_op(irn)) == &be_node_tag;
174 be_opcode_t get_irn_be_opcode(const ir_node *irn)
176 return is_be_node(irn) ? get_irn_opcode(irn) - beo_base : beo_NoBeOp;
179 ir_node *be_new_Spill(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *to_spill, ir_node *ctx)
186 res = new_ir_node(NULL, irg, bl, op_Spill, mode_M, 1, in);
187 a = init_node_attr(res, cls, irg, 0);
193 ir_node *be_new_Reload(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_mode *mode, ir_node *mem)
199 res = new_ir_node(NULL, irg, bl, op_Reload, mode, 1, in);
200 init_node_attr(res, cls, irg, 1);
204 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
206 ir_node *irn = new_ir_node(NULL, irg, bl, op_Perm, mode_T, n, in);
207 init_node_attr(irn, cls, irg, n);
211 ir_node *be_new_Copy(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *op)
217 res = new_ir_node(NULL, irg, bl, op_Copy, get_irn_mode(op), 1, in);
218 init_node_attr(res, cls, irg, 1);
222 ir_node *be_new_Keep(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
226 irn = new_ir_node(NULL, irg, bl, op_Keep, mode_ANY, n, in);
227 init_node_attr(irn, cls, irg, 0);
232 int be_is_Spill(const ir_node *irn)
234 return get_irn_be_opcode(irn) == beo_Spill;
237 int be_is_Reload(const ir_node *irn)
239 return get_irn_be_opcode(irn) == beo_Reload;
242 int be_is_Copy(const ir_node *irn)
244 return get_irn_be_opcode(irn) == beo_Copy;
247 int be_is_Perm(const ir_node *irn)
249 return get_irn_be_opcode(irn) == beo_Perm;
252 int be_is_Keep(const ir_node *irn)
254 return get_irn_be_opcode(irn) == beo_Keep;
257 static void be_limited(void *data, bitset_t *bs)
259 be_req_t *req = data;
262 case be_req_kind_negate_old_limited:
263 case be_req_kind_old_limited:
264 req->x.old_limited.old_limited(req->x.old_limited.old_limited_env, bs);
265 if(req->kind == be_req_kind_negate_old_limited)
268 case be_req_kind_single_reg:
269 bitset_clear_all(bs);
270 bitset_set(bs, req->x.single_reg->index);
275 void be_set_constr_single_reg(ir_node *irn, int pos, const arch_register_t *reg)
277 int idx = pos < 0 ? -(pos - 1) : pos;
278 be_node_attr_t *a = get_irn_attr(irn);
279 be_reg_data_t *rd = &a->reg_data[idx];
280 be_req_t *r = pos < 0 ? &rd->req : &rd->in_req;
282 assert(is_be_node(irn));
283 assert(!(pos >= 0) || pos < get_irn_arity(irn));
284 assert(!(pos < 0) || -(pos + 1) <= a->n_outs);
286 r->kind = be_req_kind_single_reg;
287 r->x.single_reg = reg;
288 r->req.limited = be_limited;
289 r->req.limited_env = r;
290 r->req.type = arch_register_req_type_limited;
291 r->req.cls = reg->reg_class;
294 void be_set_constr_limited(ir_node *irn, int pos, const arch_register_req_t *req)
296 int idx = pos < 0 ? -(pos - 1) : pos;
297 be_node_attr_t *a = get_irn_attr(irn);
298 be_reg_data_t *rd = &a->reg_data[idx];
299 be_req_t *r = pos < 0 ? &rd->req : &rd->in_req;
301 assert(is_be_node(irn));
302 assert(!(pos >= 0) || pos < get_irn_arity(irn));
303 assert(!(pos < 0) || -(pos + 1) <= a->n_outs);
304 assert(arch_register_req_is(req, limited));
306 r->kind = be_req_kind_old_limited;
307 r->req.limited = be_limited;
308 r->req.limited_env = r;
309 r->req.type = arch_register_req_type_limited;
310 r->req.cls = req->cls;
312 r->x.old_limited.old_limited = req->limited;
313 r->x.old_limited.old_limited_env = req->limited_env;
317 void be_set_IncSP_offset(ir_node *irn, int offset)
322 int be_get_IncSP_offset(ir_node *irn)
327 void be_set_Spill_entity(ir_node *irn, entity *ent)
329 be_spill_attr_t *a = get_irn_attr(irn);
330 assert(be_is_Spill(irn));
334 static ir_node *find_a_spill_walker(ir_node *irn, unsigned visited_nr)
336 if(get_irn_visited(irn) < visited_nr) {
337 set_irn_visited(irn, visited_nr);
341 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
342 ir_node *n = find_a_spill_walker(get_irn_n(irn, i), visited_nr);
348 else if(get_irn_be_opcode(irn) == beo_Spill)
355 ir_node *be_get_Spill_context(const ir_node *irn) {
356 const be_spill_attr_t *a = get_irn_attr(irn);
357 assert(be_is_Spill(irn));
362 * Finds a spill for a reload.
363 * If the reload is directly using the spill, this is simple,
364 * else we perform DFS from the reload (over all PhiMs) and return
365 * the first spill node we find.
367 static INLINE ir_node *find_a_spill(ir_node *irn)
369 ir_graph *irg = get_irn_irg(irn);
370 unsigned visited_nr = get_irg_visited(irg) + 1;
372 assert(be_is_Reload(irn));
373 set_irg_visited(irg, visited_nr);
374 return find_a_spill_walker(irn, visited_nr);
377 entity *be_get_spill_entity(ir_node *irn)
379 int opc = get_irn_opcode(irn);
381 switch(get_irn_be_opcode(irn)) {
383 return be_get_spill_entity(find_a_spill(irn));
386 be_spill_attr_t *a = get_irn_attr(irn);
390 assert(0 && "Must give spill/reload node");
396 ir_node *be_spill(const arch_env_t *arch_env, ir_node *irn, ir_node *ctx)
398 const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, irn, -1);
400 ir_node *bl = get_nodes_block(irn);
401 ir_graph *irg = get_irn_irg(bl);
402 ir_node *spill = be_new_Spill(cls, irg, bl, irn, ctx);
406 * search the right insertion point. a spill of a phi cannot be put
407 * directly after the phi, if there are some phis behind the one which
410 insert = sched_next(irn);
411 while(is_Phi(insert) && !sched_is_end(insert))
412 insert = sched_next(insert);
414 sched_add_before(insert, spill);
418 ir_node *be_reload(const arch_env_t *arch_env,
419 const arch_register_class_t *cls,
420 ir_node *irn, int pos, ir_mode *mode, ir_node *spill)
424 ir_node *bl = get_nodes_block(irn);
425 ir_graph *irg = get_irn_irg(bl);
427 assert(be_is_Spill(spill) || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
429 reload = be_new_Reload(cls, irg, bl, mode, spill);
431 set_irn_n(irn, pos, reload);
432 sched_add_before(irn, reload);
436 static int redir_proj(const ir_node **node, int pos)
438 const ir_node *n = *node;
441 assert(pos == -1 && "Illegal pos for a Proj");
442 *node = get_Proj_pred(n);
443 return get_Proj_proj(n);
449 static void *put_out_reg_req(arch_register_req_t *req, const ir_node *irn, int out_pos)
451 const be_node_attr_t *a = get_irn_attr(irn);
453 if(out_pos < a->n_outs)
454 memcpy(req, &a->reg_data[out_pos].req, sizeof(req[0]));
456 req->type = arch_register_req_type_none;
463 static void *put_in_reg_req(arch_register_req_t *req, const ir_node *irn, int pos)
465 const be_node_attr_t *a = get_irn_attr(irn);
466 int n = get_irn_arity(irn);
468 if(pos < get_irn_arity(irn))
469 memcpy(req, &a->reg_data[pos].in_req, sizeof(req[0]));
471 req->type = arch_register_req_type_none;
478 static const arch_register_req_t *
479 be_node_get_irn_reg_req(const void *self, arch_register_req_t *req, const ir_node *irn, int pos)
484 if(get_irn_mode(irn) == mode_T)
487 out_pos = redir_proj((const ir_node **) &irn, pos);
488 assert(is_be_node(irn));
489 return put_out_reg_req(req, irn, out_pos);
493 return is_be_node(irn) ? put_in_reg_req(req, irn, pos) : NULL;
500 be_node_set_irn_reg(const void *_self, ir_node *irn, const arch_register_t *reg)
505 out_pos = redir_proj((const ir_node **) &irn, -1);
506 a = get_irn_attr(irn);
508 assert(is_be_node(irn));
509 assert(out_pos < a->n_outs && "position too high");
510 a->reg_data[out_pos].reg = reg;
513 const arch_register_t *
514 be_node_get_irn_reg(const void *_self, const ir_node *irn)
519 out_pos = redir_proj((const ir_node **) &irn, -1);
520 a = get_irn_attr(irn);
522 assert(is_be_node(irn));
523 assert(out_pos < a->n_outs && "position too high");
525 return a->reg_data[out_pos].reg;
528 arch_irn_class_t be_node_classify(const void *_self, const ir_node *irn)
530 redir_proj((const ir_node **) &irn, -1);
532 switch(get_irn_be_opcode(irn)) {
533 #define XXX(a,b) case beo_ ## a: return arch_irn_class_ ## b;
546 arch_irn_class_t be_node_get_flags(const void *_self, const ir_node *irn)
551 static const arch_irn_ops_if_t be_node_irn_ops_if = {
552 be_node_get_irn_reg_req,
559 static const arch_irn_ops_t be_node_irn_ops = {
563 const void *be_node_get_arch_ops(const arch_irn_handler_t *self, const ir_node *irn)
565 redir_proj((const ir_node **) &irn, -1);
566 return is_be_node(irn) ? &be_node_irn_ops : NULL;
569 const arch_irn_handler_t be_node_irn_handler = {
573 static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason)
575 be_node_attr_t *at = get_irn_attr(irn);
578 assert(is_be_node(irn));
581 case dump_node_opcode_txt:
582 fprintf(f, get_op_name(get_irn_op(irn)));
584 case dump_node_mode_txt:
585 fprintf(f, get_mode_name(get_irn_mode(irn)));
587 case dump_node_nodeattr_txt:
589 case dump_node_info_txt:
590 fprintf(f, "reg class: %s\n", at->cls->name);
591 for(i = 0; i < at->n_outs; ++i) {
592 const arch_register_t *reg = at->reg_data[i].reg;
593 fprintf(f, "reg #%d: %s\n", i, reg ? reg->name : "n/a");
596 if(get_irn_be_opcode(irn) == beo_Spill) {
597 be_spill_attr_t *a = (be_spill_attr_t *) at;
599 ir_fprintf(f, "spill context: %+F\n", a->spill_ctx);
601 unsigned ofs = get_entity_offset_bytes(a->ent);
602 ir_fprintf(f, "spill entity: %+F offset %x (%d)\n", a->ent, ofs, ofs);
605 ir_fprintf(f, "spill entity: n/a\n");
614 static const ir_op_ops be_node_op_ops = {
630 pset *nodes_live_at(const arch_env_t *arch_env, const arch_register_class_t *cls, const ir_node *pos, pset *live)
632 firm_dbg_module_t *dbg = firm_dbg_register("firm.be.node");
633 const ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
637 live_foreach(bl, li) {
638 ir_node *irn = (ir_node *) li->irn;
639 if(live_is_end(li) && arch_irn_consider_in_reg_alloc(arch_env, cls, irn))
640 pset_insert_ptr(live, irn);
643 sched_foreach_reverse(bl, irn) {
648 * If we encounter the node we want to insert the Perm after,
649 * exit immediately, so that this node is still live
654 DBG((dbg, LEVEL_1, "%+F\n", irn));
655 for(x = pset_first(live); x; x = pset_next(live))
656 DBG((dbg, LEVEL_1, "\tlive: %+F\n", x));
658 if(arch_irn_has_reg_class(arch_env, irn, -1, cls))
659 pset_remove_ptr(live, irn);
661 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
662 ir_node *op = get_irn_n(irn, i);
664 if(arch_irn_consider_in_reg_alloc(arch_env, cls, op))
665 pset_insert_ptr(live, op);
672 ir_node *insert_Perm_after(const arch_env_t *arch_env,
673 const arch_register_class_t *cls,
674 dom_front_info_t *dom_front,
677 ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
678 ir_graph *irg = get_irn_irg(bl);
679 pset *live = pset_new_ptr_default();
680 firm_dbg_module_t *dbg = firm_dbg_register("be.node");
682 ir_node *curr, *irn, *perm, **nodes;
685 DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
687 if(!nodes_live_at(arch_env, cls, pos, live));
689 n = pset_count(live);
694 nodes = malloc(n * sizeof(nodes[0]));
696 DBG((dbg, LEVEL_1, "live:\n"));
697 for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++) {
698 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
702 perm = be_new_Perm(cls, irg, bl, n, nodes);
703 sched_add_after(pos, perm);
707 for(i = 0; i < n; ++i) {
709 ir_node *perm_op = get_irn_n(perm, i);
710 const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op);
712 ir_mode *mode = get_irn_mode(perm_op);
713 ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
714 arch_set_irn_register(arch_env, proj, reg);
716 sched_add_after(curr, proj);
720 be_ssa_constr_single(dom_front, perm_op, 1, copies);