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 arch_register_req_t req;
64 unsigned negate_limited : 1;
65 void (*old_limited)(void *ptr, bitset_t *bs);
66 void *old_limited_env;
70 const arch_register_t *reg;
76 const arch_register_class_t *cls;
77 be_reg_data_t *reg_data;
81 be_node_attr_t node_attr;
82 ir_node *spill_ctx; /**< The node in whose context this spill was introduced. */
83 entity *ent; /**< The entity in the stack frame the spill writes to. */
86 static ir_op *op_Spill;
87 static ir_op *op_Reload;
88 static ir_op *op_Perm;
89 static ir_op *op_Copy;
90 static ir_op *op_Keep;
92 static int beo_base = -1;
94 static const ir_op_ops be_node_op_ops;
96 void be_node_init(void) {
97 static int inited = 0;
105 beo_base = get_next_ir_opcode();
107 /* Acquire all needed opcodes. We assume that they are consecutive! */
108 for(i = beo_Spill; i < beo_Last; ++i)
109 get_next_ir_opcode();
111 op_Spill = new_ir_op(beo_base + beo_Spill, "Spill", op_pin_state_mem_pinned, 0, oparity_unary, 0, sizeof(be_spill_attr_t), &be_node_op_ops);
112 op_Reload = new_ir_op(beo_base + beo_Reload, "Reload", op_pin_state_mem_pinned, 0, oparity_zero, 0, sizeof(be_node_attr_t), &be_node_op_ops);
113 op_Perm = new_ir_op(beo_base + beo_Perm, "Perm", op_pin_state_pinned, 0, oparity_variable, 0, sizeof(be_node_attr_t), &be_node_op_ops);
114 op_Copy = new_ir_op(beo_base + beo_Copy, "Copy", op_pin_state_pinned, 0, oparity_unary, 0, sizeof(be_node_attr_t), &be_node_op_ops);
115 op_Keep = new_ir_op(beo_base + beo_Keep, "Keep", op_pin_state_pinned, 0, oparity_variable, 0, sizeof(be_node_attr_t), &be_node_op_ops);
117 set_op_tag(op_Spill, &be_node_tag);
118 set_op_tag(op_Reload, &be_node_tag);
119 set_op_tag(op_Perm, &be_node_tag);
120 set_op_tag(op_Copy, &be_node_tag);
121 set_op_tag(op_Keep, &be_node_tag);
124 static void *init_node_attr(ir_node* irn, const arch_register_class_t *cls, ir_graph *irg, int n_outs)
126 be_node_attr_t *a = get_irn_attr(irn);
135 a->reg_data = NEW_ARR_D(be_reg_data_t, get_irg_obstack(irg), n_outs);
136 memset(a->reg_data, 0, n_outs * sizeof(a->reg_data[0]));
137 for(i = 0; i < n_outs; ++i) {
138 a->reg_data[i].req.req.cls = cls;
139 a->reg_data[i].req.req.type = arch_register_req_type_normal;
146 static INLINE int is_be_node(const ir_node *irn)
148 return get_op_tag(get_irn_op(irn)) == &be_node_tag;
151 be_opcode_t get_irn_be_opcode(const ir_node *irn)
153 return is_be_node(irn) ? get_irn_opcode(irn) - beo_base : beo_NoBeOp;
156 ir_node *be_new_Spill(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *to_spill, ir_node *ctx)
163 res = new_ir_node(NULL, irg, bl, op_Spill, mode_M, 1, in);
164 a = init_node_attr(res, cls, irg, 0);
170 ir_node *be_new_Reload(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_mode *mode, ir_node *mem)
176 res = new_ir_node(NULL, irg, bl, op_Reload, mode, 1, in);
177 init_node_attr(res, cls, irg, 1);
181 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
183 ir_node *irn = new_ir_node(NULL, irg, bl, op_Perm, mode_T, n, in);
184 init_node_attr(irn, cls, irg, n);
188 ir_node *be_new_Copy(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *op)
194 res = new_ir_node(NULL, irg, bl, op_Copy, get_irn_mode(op), 1, in);
195 init_node_attr(res, cls, irg, 1);
199 ir_node *be_new_Keep(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
203 irn = new_ir_node(NULL, irg, bl, op_Keep, mode_ANY, n, in);
204 init_node_attr(irn, cls, irg, 0);
209 int be_is_Spill(const ir_node *irn)
211 return get_irn_be_opcode(irn) == beo_Spill;
214 int be_is_Reload(const ir_node *irn)
216 return get_irn_be_opcode(irn) == beo_Reload;
219 int be_is_Copy(const ir_node *irn)
221 return get_irn_be_opcode(irn) == beo_Copy;
224 int be_is_Perm(const ir_node *irn)
226 return get_irn_be_opcode(irn) == beo_Perm;
229 int be_is_Keep(const ir_node *irn)
231 return get_irn_be_opcode(irn) == beo_Keep;
234 static void be_limited(void *data, bitset_t *bs)
236 be_req_t *req = data;
238 req->old_limited(req->old_limited_env, bs);
239 if(req->negate_limited)
243 void be_set_Perm_out_req(ir_node *irn, int pos, const arch_register_req_t *req, unsigned negate_limited)
245 be_node_attr_t *a = get_irn_attr(irn);
246 be_req_t *r = &a->reg_data[pos].req;
248 assert(be_is_Perm(irn));
249 assert(pos >= 0 && pos < get_irn_arity(irn));
250 memcpy(&r->req, req, sizeof(req[0]));
252 if(arch_register_req_is(req, limited)) {
253 r->old_limited = r->req.limited;
254 r->old_limited_env = r->req.limited_env;
255 r->req.limited = be_limited;
256 r->req.limited_env = r;
257 r->negate_limited = negate_limited;
261 void be_set_Spill_entity(ir_node *irn, entity *ent)
263 be_spill_attr_t *a = get_irn_attr(irn);
264 assert(be_is_Spill(irn));
268 static ir_node *find_a_spill_walker(ir_node *irn, unsigned visited_nr)
270 if(get_irn_visited(irn) < visited_nr) {
271 set_irn_visited(irn, visited_nr);
275 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
276 ir_node *n = find_a_spill_walker(get_irn_n(irn, i), visited_nr);
282 else if(get_irn_be_opcode(irn) == beo_Spill)
289 ir_node *be_get_Spill_context(const ir_node *irn) {
290 const be_spill_attr_t *a = get_irn_attr(irn);
291 assert(be_is_Spill(irn));
296 * Finds a spill for a reload.
297 * If the reload is directly using the spill, this is simple,
298 * else we perform DFS from the reload (over all PhiMs) and return
299 * the first spill node we find.
301 static INLINE ir_node *find_a_spill(ir_node *irn)
303 ir_graph *irg = get_irn_irg(irn);
304 unsigned visited_nr = get_irg_visited(irg) + 1;
306 assert(be_is_Reload(irn));
307 set_irg_visited(irg, visited_nr);
308 return find_a_spill_walker(irn, visited_nr);
311 entity *be_get_spill_entity(ir_node *irn)
313 int opc = get_irn_opcode(irn);
315 switch(get_irn_be_opcode(irn)) {
317 return be_get_spill_entity(find_a_spill(irn));
320 be_spill_attr_t *a = get_irn_attr(irn);
324 assert(0 && "Must give spill/reload node");
330 ir_node *be_spill(const arch_env_t *arch_env, ir_node *irn, ir_node *ctx)
332 const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, irn, -1);
334 ir_node *bl = get_nodes_block(irn);
335 ir_graph *irg = get_irn_irg(bl);
336 ir_node *spill = be_new_Spill(cls, irg, bl, irn, ctx);
340 * search the right insertion point. a spill of a phi cannot be put
341 * directly after the phi, if there are some phis behind the one which
344 insert = sched_next(irn);
345 while(is_Phi(insert) && !sched_is_end(insert))
346 insert = sched_next(insert);
348 sched_add_before(insert, spill);
352 ir_node *be_reload(const arch_env_t *arch_env,
353 const arch_register_class_t *cls,
354 ir_node *irn, int pos, ir_mode *mode, ir_node *spill)
358 ir_node *bl = get_nodes_block(irn);
359 ir_graph *irg = get_irn_irg(bl);
361 assert(be_is_Spill(spill) || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
363 reload = be_new_Reload(cls, irg, bl, mode, spill);
365 set_irn_n(irn, pos, reload);
366 sched_add_before(irn, reload);
370 static int redir_proj(const ir_node **node, int pos)
372 const ir_node *n = *node;
375 assert(pos == -1 && "Illegal pos for a Proj");
376 *node = get_Proj_pred(n);
377 return get_Proj_proj(n);
383 static void *put_out_reg_req(arch_register_req_t *req, const ir_node *irn, int out_pos)
385 const be_node_attr_t *a = get_irn_attr(irn);
388 if(out_pos < a->n_outs)
389 memcpy(req, &a->reg_data[out_pos].req, sizeof(req[0]));
391 req->type = arch_register_req_type_none;
398 static void *put_in_reg_req(arch_register_req_t *req, const ir_node *irn, int pos)
400 const be_node_attr_t *a = get_irn_attr(irn);
401 int n = get_irn_arity(irn);
403 req->type = arch_register_req_type_none;
406 switch(get_irn_be_opcode(irn)) {
412 req->type = arch_register_req_type_normal;
424 static const arch_register_req_t *
425 be_node_get_irn_reg_req(const void *self, arch_register_req_t *req, const ir_node *irn, int pos)
430 if(get_irn_mode(irn) == mode_T)
433 out_pos = redir_proj((const ir_node **) &irn, pos);
434 assert(is_be_node(irn));
435 return put_out_reg_req(req, irn, out_pos);
439 return is_be_node(irn) ? put_in_reg_req(req, irn, pos) : NULL;
446 be_node_set_irn_reg(const void *_self, ir_node *irn, const arch_register_t *reg)
451 out_pos = redir_proj((const ir_node **) &irn, -1);
452 a = get_irn_attr(irn);
454 assert(is_be_node(irn));
455 assert(out_pos < a->n_outs && "position too high");
456 a->reg_data[out_pos].reg = reg;
459 const arch_register_t *
460 be_node_get_irn_reg(const void *_self, const ir_node *irn)
465 out_pos = redir_proj((const ir_node **) &irn, -1);
466 a = get_irn_attr(irn);
468 assert(is_be_node(irn));
469 assert(out_pos < a->n_outs && "position too high");
471 return a->reg_data[out_pos].reg;
474 arch_irn_class_t be_node_classify(const void *_self, const ir_node *irn)
476 redir_proj((const ir_node **) &irn, -1);
478 switch(get_irn_be_opcode(irn)) {
479 #define XXX(a,b) case beo_ ## a: return arch_irn_class_ ## b;
492 arch_irn_class_t be_node_get_flags(const void *_self, const ir_node *irn)
497 static const arch_irn_ops_if_t be_node_irn_ops_if = {
498 be_node_get_irn_reg_req,
505 static const arch_irn_ops_t be_node_irn_ops = {
509 const void *be_node_get_arch_ops(const arch_irn_handler_t *self, const ir_node *irn)
511 redir_proj((const ir_node **) &irn, -1);
512 return is_be_node(irn) ? &be_node_irn_ops : NULL;
515 const arch_irn_handler_t be_node_irn_handler = {
519 static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason)
521 be_node_attr_t *at = get_irn_attr(irn);
524 assert(is_be_node(irn));
527 case dump_node_opcode_txt:
528 fprintf(f, get_op_name(get_irn_op(irn)));
530 case dump_node_mode_txt:
531 fprintf(f, get_mode_name(get_irn_mode(irn)));
533 case dump_node_nodeattr_txt:
535 case dump_node_info_txt:
536 fprintf(f, "reg class: %s\n", at->cls->name);
537 for(i = 0; i < at->n_outs; ++i) {
538 const arch_register_t *reg = at->reg_data[i].reg;
539 fprintf(f, "reg #%d: %s\n", i, reg ? reg->name : "n/a");
542 if(get_irn_be_opcode(irn) == beo_Spill) {
543 be_spill_attr_t *a = (be_spill_attr_t *) at;
544 unsigned ofs = get_entity_offset_bytes(a->ent);
545 ir_fprintf(f, "spill context: %+F\n", a->spill_ctx);
546 ir_fprintf(f, "spill entity: %+F offset %x (%d)\n", a->ent, ofs, ofs);
554 static const ir_op_ops be_node_op_ops = {
570 pset *nodes_live_at(const arch_env_t *arch_env, const arch_register_class_t *cls, const ir_node *pos, pset *live)
572 firm_dbg_module_t *dbg = firm_dbg_register("firm.be.node");
573 ir_node *bl = get_nodes_block(pos);
577 live_foreach(bl, li) {
578 ir_node *irn = (ir_node *) li->irn;
579 if(live_is_end(li) && arch_irn_consider_in_reg_alloc(arch_env, cls, irn))
580 pset_insert_ptr(live, irn);
583 sched_foreach_reverse(bl, irn) {
588 * If we encounter the node we want to insert the Perm after,
589 * exit immediately, so that this node is still live
594 DBG((dbg, LEVEL_1, "%+F\n", irn));
595 for(x = pset_first(live); x; x = pset_next(live))
596 DBG((dbg, LEVEL_1, "\tlive: %+F\n", x));
598 if(arch_irn_has_reg_class(arch_env, irn, -1, cls))
599 pset_remove_ptr(live, irn);
601 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
602 ir_node *op = get_irn_n(irn, i);
604 if(arch_irn_consider_in_reg_alloc(arch_env, cls, op))
605 pset_insert_ptr(live, op);
612 ir_node *insert_Perm_after(const arch_env_t *arch_env,
613 const arch_register_class_t *cls,
614 dom_front_info_t *dom_front,
617 ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
618 ir_graph *irg = get_irn_irg(bl);
619 pset *live = pset_new_ptr_default();
620 firm_dbg_module_t *dbg = firm_dbg_register("be.node");
622 ir_node *curr, *irn, *perm, **nodes;
625 DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
627 if(!nodes_live_at(arch_env, cls, pos, live))
628 assert(0 && "position not found");
630 n = pset_count(live);
635 nodes = malloc(n * sizeof(nodes[0]));
637 DBG((dbg, LEVEL_1, "live:\n"));
638 for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++) {
639 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
643 perm = be_new_Perm(cls, irg, bl, n, nodes);
644 sched_add_after(pos, perm);
648 for(i = 0; i < n; ++i) {
650 ir_node *perm_op = get_irn_n(perm, i);
651 const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op);
653 ir_mode *mode = get_irn_mode(perm_op);
654 ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
655 arch_set_irn_register(arch_env, proj, reg);
657 sched_add_after(curr, proj);
661 be_introduce_copies(dom_front, perm_op, 1, copies);