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 const arch_register_t *reg;
64 arch_register_req_t req;
69 const arch_register_class_t *cls;
70 be_reg_data_t *reg_data;
74 be_node_attr_t node_attr;
75 ir_node *spill_ctx; /**< The node in whose context this spill was introduced. */
76 entity *ent; /**< The entity in the stack frame the spill writes to. */
79 static ir_op *op_Spill;
80 static ir_op *op_Reload;
81 static ir_op *op_Perm;
82 static ir_op *op_Copy;
83 static ir_op *op_Keep;
85 static int beo_base = -1;
87 static const ir_op_ops be_node_op_ops;
89 void be_node_init(void) {
90 static int inited = 0;
98 beo_base = get_next_ir_opcode();
100 /* Acquire all needed opcodes. We assume that they are consecutive! */
101 for(i = beo_Spill; i < beo_Last; ++i)
102 get_next_ir_opcode();
104 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);
105 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);
106 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);
107 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);
108 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);
110 set_op_tag(op_Spill, &be_node_tag);
111 set_op_tag(op_Reload, &be_node_tag);
112 set_op_tag(op_Perm, &be_node_tag);
113 set_op_tag(op_Copy, &be_node_tag);
114 set_op_tag(op_Keep, &be_node_tag);
117 static void *init_node_attr(ir_node* irn, const arch_register_class_t *cls, ir_graph *irg, int n_outs)
119 be_node_attr_t *a = get_irn_attr(irn);
128 a->reg_data = NEW_ARR_D(be_reg_data_t, get_irg_obstack(irg), n_outs);
129 memset(a->reg_data, 0, n_outs * sizeof(a->reg_data[0]));
130 for(i = 0; i < n_outs; ++i) {
131 a->reg_data[i].req.cls = cls;
132 a->reg_data[i].req.type = arch_register_req_type_normal;
139 static INLINE int is_be_node(const ir_node *irn)
141 return get_op_tag(get_irn_op(irn)) == &be_node_tag;
144 be_opcode_t get_irn_be_opcode(const ir_node *irn)
146 return is_be_node(irn) ? get_irn_opcode(irn) - beo_base : beo_NoBeOp;
149 ir_node *be_new_Spill(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *to_spill, ir_node *ctx)
156 res = new_ir_node(NULL, irg, bl, op_Spill, mode_M, 1, in);
157 a = init_node_attr(res, cls, irg, 0);
163 ir_node *be_new_Reload(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_mode *mode, ir_node *mem)
169 res = new_ir_node(NULL, irg, bl, op_Reload, mode, 1, in);
170 init_node_attr(res, cls, irg, 1);
174 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
176 ir_node *irn = new_ir_node(NULL, irg, bl, op_Perm, mode_T, n, in);
177 init_node_attr(irn, cls, irg, n);
181 ir_node *be_new_Copy(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *op)
187 res = new_ir_node(NULL, irg, bl, op_Copy, get_irn_mode(op), 1, in);
188 init_node_attr(res, cls, irg, 1);
192 ir_node *be_new_Keep(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
196 irn = new_ir_node(NULL, irg, bl, op_Keep, mode_ANY, n, in);
197 init_node_attr(irn, cls, irg, 0);
202 int be_is_Spill(const ir_node *irn)
204 return get_irn_be_opcode(irn) == beo_Spill;
207 int be_is_Reload(const ir_node *irn)
209 return get_irn_be_opcode(irn) == beo_Reload;
212 int be_is_Copy(const ir_node *irn)
214 return get_irn_be_opcode(irn) == beo_Copy;
217 int be_is_Perm(const ir_node *irn)
219 return get_irn_be_opcode(irn) == beo_Perm;
222 int be_is_Keep(const ir_node *irn)
224 return get_irn_be_opcode(irn) == beo_Keep;
227 void be_set_Perm_out_req(ir_node *irn, int pos, const arch_register_req_t *req)
229 be_node_attr_t *a = get_irn_attr(irn);
231 assert(be_is_Perm(irn));
232 assert(pos >= 0 && pos < get_irn_arity(irn));
233 memcpy(&a->reg_data[pos].req, req, sizeof(req[0]));
236 void be_set_Spill_entity(ir_node *irn, entity *ent)
238 be_spill_attr_t *a = get_irn_attr(irn);
239 assert(be_is_Spill(irn));
243 static ir_node *find_a_spill_walker(ir_node *irn, unsigned visited_nr)
245 if(get_irn_visited(irn) < visited_nr) {
246 set_irn_visited(irn, visited_nr);
250 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
251 ir_node *n = find_a_spill_walker(get_irn_n(irn, i), visited_nr);
257 else if(get_irn_be_opcode(irn) == beo_Spill)
264 ir_node *be_get_Spill_context(const ir_node *irn) {
265 const be_spill_attr_t *a = get_irn_attr(irn);
266 assert(be_is_Spill(irn));
271 * Finds a spill for a reload.
272 * If the reload is directly using the spill, this is simple,
273 * else we perform DFS from the reload (over all PhiMs) and return
274 * the first spill node we find.
276 static INLINE ir_node *find_a_spill(ir_node *irn)
278 ir_graph *irg = get_irn_irg(irn);
279 unsigned visited_nr = get_irg_visited(irg) + 1;
281 assert(be_is_Reload(irn));
282 set_irg_visited(irg, visited_nr);
283 return find_a_spill_walker(irn, visited_nr);
287 entity *be_get_spill_entity(ir_node *irn)
289 int opc = get_irn_opcode(irn);
291 switch(get_irn_be_opcode(irn)) {
293 return be_get_spill_entity(find_a_spill(irn));
296 be_spill_attr_t *a = get_irn_attr(irn);
300 assert(0 && "Must give spill/reload node");
306 ir_node *be_spill(const arch_env_t *arch_env, ir_node *irn, ir_node *ctx)
308 const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, irn, -1);
310 ir_node *bl = get_nodes_block(irn);
311 ir_graph *irg = get_irn_irg(bl);
312 ir_node *spill = be_new_Spill(cls, irg, bl, irn, ctx);
316 * search the right insertion point. a spill of a phi cannot be put
317 * directly after the phi, if there are some phis behind the one which
320 insert = sched_next(irn);
321 while(is_Phi(insert) && !sched_is_end(insert))
322 insert = sched_next(insert);
324 sched_add_before(insert, spill);
328 ir_node *be_reload(const arch_env_t *arch_env,
329 const arch_register_class_t *cls,
330 ir_node *irn, int pos, ir_mode *mode, ir_node *spill)
334 ir_node *bl = get_nodes_block(irn);
335 ir_graph *irg = get_irn_irg(bl);
337 assert(be_is_Spill(spill) || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
339 reload = be_new_Reload(cls, irg, bl, mode, spill);
341 set_irn_n(irn, pos, reload);
342 sched_add_before(irn, reload);
346 static int redir_proj(const ir_node **node, int pos)
348 const ir_node *n = *node;
351 assert(pos == -1 && "Illegal pos for a Proj");
352 *node = get_Proj_pred(n);
353 return get_Proj_proj(n);
359 static void *put_out_reg_req(arch_register_req_t *req, const ir_node *irn, int out_pos)
361 const be_node_attr_t *a = get_irn_attr(irn);
364 if(out_pos < a->n_outs)
365 memcpy(req, &a->reg_data[out_pos].req, sizeof(req[0]));
367 req->type = arch_register_req_type_none;
374 static void *put_in_reg_req(arch_register_req_t *req, const ir_node *irn, int pos)
376 const be_node_attr_t *a = get_irn_attr(irn);
377 int n = get_irn_arity(irn);
379 req->type = arch_register_req_type_none;
382 switch(get_irn_be_opcode(irn)) {
388 req->type = arch_register_req_type_normal;
400 static const arch_register_req_t *
401 be_node_get_irn_reg_req(const void *self, arch_register_req_t *req, const ir_node *irn, int pos)
406 if(get_irn_mode(irn) == mode_T)
409 out_pos = redir_proj((const ir_node **) &irn, pos);
410 assert(is_be_node(irn));
411 return put_out_reg_req(req, irn, out_pos);
415 return is_be_node(irn) ? put_in_reg_req(req, irn, pos) : NULL;
422 be_node_set_irn_reg(const void *_self, ir_node *irn, const arch_register_t *reg)
427 out_pos = redir_proj((const ir_node **) &irn, -1);
428 a = get_irn_attr(irn);
430 assert(is_be_node(irn));
431 assert(out_pos < a->n_outs && "position too high");
432 a->reg_data[out_pos].reg = reg;
435 const arch_register_t *
436 be_node_get_irn_reg(const void *_self, const ir_node *irn)
441 out_pos = redir_proj((const ir_node **) &irn, -1);
442 a = get_irn_attr(irn);
444 assert(is_be_node(irn));
445 assert(out_pos < a->n_outs && "position too high");
447 return a->reg_data[out_pos].reg;
450 arch_irn_class_t be_node_classify(const void *_self, const ir_node *irn)
452 redir_proj((const ir_node **) &irn, -1);
454 switch(get_irn_be_opcode(irn)) {
455 #define XXX(a,b) case beo_ ## a: return arch_irn_class_ ## b;
468 arch_irn_class_t be_node_get_flags(const void *_self, const ir_node *irn)
473 static const arch_irn_ops_if_t be_node_irn_ops_if = {
474 be_node_get_irn_reg_req,
481 static const arch_irn_ops_t be_node_irn_ops = {
485 const void *be_node_get_arch_ops(const arch_irn_handler_t *self, const ir_node *irn)
487 redir_proj((const ir_node **) &irn, -1);
488 return is_be_node(irn) ? &be_node_irn_ops : NULL;
491 const arch_irn_handler_t be_node_irn_handler = {
495 static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason)
497 be_node_attr_t *at = get_irn_attr(irn);
500 assert(is_be_node(irn));
503 case dump_node_opcode_txt:
504 fprintf(f, get_op_name(get_irn_op(irn)));
506 case dump_node_mode_txt:
507 fprintf(f, get_mode_name(get_irn_mode(irn)));
509 case dump_node_nodeattr_txt:
511 case dump_node_info_txt:
512 fprintf(f, "reg class: %s\n", at->cls->name);
513 for(i = 0; i < at->n_outs; ++i) {
514 const arch_register_t *reg = at->reg_data[i].reg;
515 fprintf(f, "reg #%d: %s\n", i, reg ? reg->name : "n/a");
518 if(get_irn_be_opcode(irn) == beo_Spill) {
519 be_spill_attr_t *a = (be_spill_attr_t *) at;
520 ir_fprintf(f, "spill context: %+F\n", a->spill_ctx);
521 //TODO ir_fprintf(f, "spill offset: %04x (%u)\n", a->offset, a->offset);
529 static const ir_op_ops be_node_op_ops = {
545 pset *nodes_live_at(const arch_env_t *arch_env, const arch_register_class_t *cls, const ir_node *pos, pset *live)
547 firm_dbg_module_t *dbg = firm_dbg_register("firm.be.node");
548 ir_node *bl = get_nodes_block(pos);
552 live_foreach(bl, li) {
553 ir_node *irn = (ir_node *) li->irn;
554 if(live_is_end(li) && arch_irn_consider_in_reg_alloc(arch_env, cls, irn))
555 pset_insert_ptr(live, irn);
558 sched_foreach_reverse(bl, irn) {
563 * If we encounter the node we want to insert the Perm after,
564 * exit immediately, so that this node is still live
569 DBG((dbg, LEVEL_1, "%+F\n", irn));
570 for(x = pset_first(live); x; x = pset_next(live))
571 DBG((dbg, LEVEL_1, "\tlive: %+F\n", x));
573 if(arch_irn_has_reg_class(arch_env, irn, -1, cls))
574 pset_remove_ptr(live, irn);
576 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
577 ir_node *op = get_irn_n(irn, i);
579 if(arch_irn_consider_in_reg_alloc(arch_env, cls, op))
580 pset_insert_ptr(live, op);
587 ir_node *insert_Perm_after(const arch_env_t *arch_env,
588 const arch_register_class_t *cls,
589 dom_front_info_t *dom_front,
592 ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
593 ir_graph *irg = get_irn_irg(bl);
594 pset *live = pset_new_ptr_default();
595 firm_dbg_module_t *dbg = firm_dbg_register("be.node");
597 ir_node *curr, *irn, *perm, **nodes;
600 DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
602 if(!nodes_live_at(arch_env, cls, pos, live))
603 assert(0 && "position not found");
605 n = pset_count(live);
610 nodes = malloc(n * sizeof(nodes[0]));
612 DBG((dbg, LEVEL_1, "live:\n"));
613 for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++) {
614 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
618 perm = be_new_Perm(cls, irg, bl, n, nodes);
619 sched_add_after(pos, perm);
623 for(i = 0; i < n; ++i) {
625 ir_node *perm_op = get_irn_n(perm, i);
626 const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op);
628 ir_mode *mode = get_irn_mode(perm_op);
629 ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
630 arch_set_irn_register(arch_env, proj, reg);
632 sched_add_after(curr, proj);
636 be_introduce_copies(dom_front, perm_op, 1, copies);