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"
42 #define BENODE_MAGIC FOURCC('B', 'E', 'N', 'O')
44 typedef enum _node_kind_t {
55 const arch_register_class_t *cls;
62 const arch_register_t *reg;
63 arch_register_req_t req;
70 be_reg_data_t reg_data[1];
75 ir_node *spill_ctx; /**< The node in whose context this spill was introduced. */
76 unsigned offset; /**< The offset of the memory location the spill writes to
81 ir_node *new_Keep(ir_graph *irg, ir_node *bl, int n, ir_node *in[])
83 static ir_op *keep_op = NULL;
87 keep_op = new_ir_op(get_next_ir_opcode(), "Keep", op_pin_state_pinned,
88 irop_flag_keep, oparity_variable, 0, 0, NULL);
90 irn = new_ir_node(NULL, irg, bl, keep_op, mode_ANY, n, in);
96 static int templ_pos_Spill[] = {
100 static int templ_pos_Reload[] = {
104 static int templ_pos_Copy[] = {
108 static int templ_pos_Kill[] = {
112 static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason);
114 static const ir_op_ops be_node_ops = {
130 static INLINE int is_be_node(const ir_node *irn)
132 const be_node_attr_t *attr = (const be_node_attr_t *) &irn->attr;
133 return attr->magic == BENODE_MAGIC;
136 static INLINE int is_be_kind(const ir_node *irn, node_kind_t kind)
138 const be_node_attr_t *a = (const be_node_attr_t *) &irn->attr;
139 return a->magic == BENODE_MAGIC && a->op && a->op->kind == kind;
142 static INLINE void *get_attr_and_check(ir_node *irn, node_kind_t kind)
144 is_be_kind(irn, kind);
148 static be_node_attr_t *init_node_attr(ir_node *irn,
150 const arch_register_class_t *cls,
154 be_node_attr_t *attr = (be_node_attr_t *) &irn->attr;
157 attr->magic = BENODE_MAGIC;
158 attr->n_regs = n_regs;
161 for(i = 0; i < n_regs; ++i) {
162 be_reg_data_t *rd = attr->reg_data + i;
164 memset(&rd->req, 0, sizeof(rd->req));
167 rd->req.type = arch_register_req_type_normal;
173 #define ARRSIZE(x) (sizeof(x) / sizeof(x[0]))
175 static int cmp_op_map(const void *a, const void *b, size_t size)
177 const be_op_t *x = a;
178 const be_op_t *y = b;
180 return !(x->kind == y->kind && x->cls == y->cls);
183 static be_op_t *get_op(const be_node_factory_t *fact,
184 const arch_register_class_t *cls, node_kind_t kind)
191 return set_insert(fact->ops, &templ, sizeof(templ),
192 HASH_PTR(cls) + 7 * kind);
197 ir_node *new_Spill(const be_node_factory_t *factory,
198 const arch_register_class_t *cls,
199 ir_graph *irg, ir_node *bl, ir_node *node_to_spill, ir_node *ctx)
204 be_op_t *bop = get_op(factory, cls, node_kind_spill);
207 assert(op && "Spill opcode must be present for this register class");
208 in[0] = node_to_spill;
209 irn = new_ir_node(NULL, irg, bl, op, mode_M, 1, in);
210 a = (be_spill_attr_t *) init_node_attr(irn, bop, cls, 0);
217 void set_Spill_offset(ir_node *irn, unsigned offset)
219 be_spill_attr_t *a = (be_spill_attr_t *) &irn->attr;
220 assert(is_be_kind(irn, node_kind_spill));
224 static ir_node *find_a_spill_walker(ir_node *irn, unsigned visited_nr)
226 if(get_irn_visited(irn) < visited_nr) {
227 set_irn_visited(irn, visited_nr);
231 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
232 ir_node *n = find_a_spill_walker(get_irn_n(irn, i), visited_nr);
238 else if(is_be_kind(irn, node_kind_spill))
245 ir_node *get_Spill_context(const ir_node *irn) {
246 be_spill_attr_t *a = (be_spill_attr_t *) &irn->attr;
247 assert(is_be_kind(irn, node_kind_spill));
252 * Finds a spill for a reload.
253 * If the reload is directly using the spill, this is simple,
254 * else we perform DFS from the reload (over all PhiMs) and return
255 * the first spill node we find.
257 static INLINE ir_node *find_a_spill(ir_node *irn)
259 ir_graph *irg = get_irn_irg(irn);
260 unsigned visited_nr = get_irg_visited(irg) + 1;
262 assert(is_be_kind(irn, node_kind_reload));
263 set_irg_visited(irg, visited_nr);
264 return find_a_spill_walker(irn, visited_nr);
268 unsigned get_Spill_offset(ir_node *irn)
270 be_node_attr_t *a = (be_node_attr_t *) &irn->attr;
271 assert(is_be_node(irn));
273 switch(a->op->kind) {
274 case node_kind_reload:
275 assert(0 && "not yet implemented");
276 return get_Spill_offset(find_a_spill(irn));
277 case node_kind_spill:
278 return ((be_spill_attr_t *) a)->offset;
280 assert(0 && "Illegal node kind (spill/reload required)");
286 ir_node *new_Reload(const be_node_factory_t *factory,
287 const arch_register_class_t *cls, ir_graph *irg,
288 ir_node *bl, ir_mode *mode, ir_node *spill_node)
290 ir_node *irn, *in[1];
291 be_op_t *bop = get_op(factory, cls, node_kind_reload);
294 assert(op && "Reload opcode must be present for this register class");
297 irn = new_ir_node(NULL, irg, bl, op, mode, 1, in);
298 init_node_attr(irn, bop, cls, 1);
303 ir_node *new_Perm(const be_node_factory_t *factory,
304 const arch_register_class_t *cls,
305 ir_graph *irg, ir_node *bl, int arity, ir_node **in)
308 be_op_t *bop = get_op(factory, cls, node_kind_perm);
311 irn = new_ir_node(NULL, irg, bl, op, mode_T, arity, in);
312 init_node_attr(irn, bop, cls, arity);
317 ir_node *new_Copy(const be_node_factory_t *factory,
318 const arch_register_class_t *cls,
319 ir_graph *irg, ir_node *bl, ir_node *in)
321 ir_node *irn, *ins[1];
322 be_op_t *bop = get_op(factory, cls, node_kind_copy);
327 irn = new_ir_node(NULL, irg, bl, op, get_irn_mode(in), 1, ins);
328 init_node_attr(irn, bop, cls, 1);
334 const be_node_factory_t *factory,
335 const arch_env_t *arch_env,
336 ir_node *irn, ir_node *ctx)
338 const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, irn, -1);
340 ir_node *bl = get_nodes_block(irn);
341 ir_graph *irg = get_irn_irg(bl);
342 ir_node *spill = new_Spill(factory, cls, irg, bl, irn, ctx);
346 * search the right insertion point. a spill of a phi cannot be put
347 * directly after the phi, if there are some phis behind the one which
350 insert = sched_next(irn);
351 while(is_Phi(insert) && !sched_is_end(insert))
352 insert = sched_next(insert);
354 sched_add_before(insert, spill);
358 ir_node *be_reload(const be_node_factory_t *factory,
359 const arch_env_t *arch_env,
360 const arch_register_class_t *cls,
361 ir_node *irn, int pos, ir_mode *mode, ir_node *spill)
365 ir_node *bl = get_nodes_block(irn);
366 ir_graph *irg = get_irn_irg(bl);
368 assert(be_is_Spill(spill)
369 || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
371 reload = new_Reload(factory, cls, irg, bl, mode, spill);
373 set_irn_n(irn, pos, reload);
374 sched_add_before(irn, reload);
379 * If the node is a proj, reset the node to the proj's target and return
381 * @param node The address of a node pointer.
382 * @param def A default value.
383 * @return If *node is a Proj, *node is set to the Proj's target and
384 * the Proj number is returned. Else *node remains the same and @p def
387 static int redir_proj(const ir_node **node, int def)
389 const ir_node *n = *node;
392 *node = get_Proj_pred(n);
393 def = -(get_Proj_proj(n) + 1);
399 static const arch_register_req_t *
400 be_node_get_irn_reg_req(const arch_irn_ops_t *_self,
401 arch_register_req_t *req, const ir_node *irn, int pos)
403 /* We cannot get output requirements for tuple nodes. */
404 if(get_irn_mode(irn) == mode_T && pos < 0)
408 * if we're interested in an output operand (pos < 0), so let's resolve projs.
411 pos = redir_proj((const ir_node **) &irn, pos);
413 if(is_be_node(irn)) {
414 const be_node_attr_t *a = (const be_node_attr_t *) &irn->attr;
415 const be_op_t *bo = a->op;
418 for(i = 0; i < bo->n_pos; ++i) {
419 if(pos == bo->pos[i]) {
421 /* be nodes have no input constraints.
422 so return normal register requirements. */
424 memset(req, 0, sizeof(req[0]));
426 req->type = arch_register_req_type_normal;
430 * if an output requirement is requested,
431 * return the one stored in the node.
434 *req = a->reg_data[-pos - 1].req;
444 void be_set_Perm_out_req(ir_node *irn, int pos, const arch_register_req_t *req)
446 be_node_attr_t *a = get_attr_and_check(irn, node_kind_perm);
447 assert(pos >= 0 && pos < get_irn_arity(irn) && "position out of range");
448 a->reg_data[pos].req = *req;
452 be_node_set_irn_reg(const arch_irn_ops_t *_self, ir_node *irn,
453 const arch_register_t *reg)
457 be_node_attr_t *attr;
458 const be_node_factory_t *factory =
459 container_of(_self, const be_node_factory_t, irn_ops);
461 if(get_irn_mode(irn) == mode_T)
464 pos = redir_proj((const ir_node **) &irn, -1);
465 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
470 attr = (be_node_attr_t *) &irn->attr;
471 attr->reg_data[-pos - 1].reg = reg;
474 const arch_register_t *
475 be_node_get_irn_reg(const arch_irn_ops_t *_self, const ir_node *irn)
479 const be_node_factory_t *factory =
480 container_of(_self, const be_node_factory_t, irn_ops);
482 if(get_irn_mode(irn) == mode_T)
485 pos = redir_proj((const ir_node **) &irn, -1);
486 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
491 for(i = 0; i < bo->n_pos; ++i) {
492 if(bo->pos[i] == pos) {
493 be_node_attr_t *attr = (be_node_attr_t *) &irn->attr;
494 return attr->reg_data[-pos - 1].reg;
501 arch_irn_class_t be_node_classify(const arch_irn_ops_t *_self, const ir_node *irn)
503 const be_node_factory_t *factory = container_of(_self, const be_node_factory_t, irn_ops);
508 idx = redir_proj(&irn, 0);
509 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
512 #define XXX(a) case node_kind_ ## a: return arch_irn_class_ ## a;
525 arch_irn_class_t be_node_get_flags(const arch_irn_ops_t *_self, const ir_node *irn)
530 static const arch_irn_ops_t *
531 be_node_get_irn_ops(const arch_irn_handler_t *_self, const ir_node *irn)
534 const be_node_factory_t *factory =
535 container_of(_self, const be_node_factory_t, handler);
538 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
540 return bo ? &factory->irn_ops : NULL;
543 const arch_irn_handler_t *be_node_get_irn_handler(const be_node_factory_t *f)
548 int be_is_Spill(const ir_node *irn)
550 return is_be_kind(irn, node_kind_spill);
553 int be_is_Reload(const ir_node *irn)
555 return is_be_kind(irn, node_kind_reload);
558 int be_is_Copy(const ir_node *irn)
560 return is_be_kind(irn, node_kind_copy);
563 int be_is_Perm(const ir_node *irn)
565 return is_be_kind(irn, node_kind_perm);
569 be_node_factory_t *be_node_factory_init(be_node_factory_t *factory, const arch_isa_t *isa)
573 factory->ops = new_set(cmp_op_map, 64);
574 factory->irn_op_map = pmap_create();
575 obstack_init(&factory->obst);
577 factory->handler.get_irn_ops = be_node_get_irn_ops;
579 factory->irn_ops.get_irn_reg_req = be_node_get_irn_reg_req;
580 factory->irn_ops.set_irn_reg = be_node_set_irn_reg;
581 factory->irn_ops.get_irn_reg = be_node_get_irn_reg;
582 factory->irn_ops.classify = be_node_classify;
583 factory->irn_ops.get_flags = be_node_get_flags;
585 for(i = 0, n = arch_isa_get_n_reg_class(isa); i < n; ++i) {
586 const arch_register_class_t *cls = arch_isa_get_reg_class(isa, i);
589 ent = get_op(factory, cls, node_kind_spill);
590 ent->op = new_ir_op(get_next_ir_opcode(), "Spill", op_pin_state_pinned,
591 0, oparity_unary, 0, sizeof(be_spill_attr_t), &be_node_ops);
592 ent->n_pos = ARRSIZE(templ_pos_Spill);
593 ent->pos = templ_pos_Spill;
594 pmap_insert(factory->irn_op_map, ent->op, ent);
596 ent = get_op(factory, cls, node_kind_reload);
597 ent->op = new_ir_op(get_next_ir_opcode(), "Reload", op_pin_state_pinned, 0,
598 oparity_unary, 0, sizeof(be_node_attr_t), &be_node_ops);
599 ent->n_pos = ARRSIZE(templ_pos_Reload);
600 ent->pos = templ_pos_Reload;
601 pmap_insert(factory->irn_op_map, ent->op, ent);
603 ent = get_op(factory, cls, node_kind_copy);
604 ent->op = new_ir_op(get_next_ir_opcode(), "Copy", op_pin_state_pinned, 0,
605 oparity_unary, 0, sizeof(be_node_attr_t), &be_node_ops);
606 ent->n_pos = ARRSIZE(templ_pos_Copy);
607 ent->pos = templ_pos_Copy;
608 pmap_insert(factory->irn_op_map, ent->op, ent);
610 ent = get_op(factory, cls, node_kind_perm);
611 ent->op = new_ir_op(get_next_ir_opcode(), "Perm", op_pin_state_pinned, 0,
613 sizeof(be_node_attr_t)
614 + sizeof(be_reg_data_t) * cls->n_regs, &be_node_ops);
615 ent->n_pos = 2 * cls->n_regs;
616 ent->pos = obstack_alloc(&factory->obst, sizeof(ent->pos[0]) * ent->n_pos);
617 for(j = 0; j < ent->n_pos; j += 2) {
620 ent->pos[j + 1] = -(k + 1);
622 pmap_insert(factory->irn_op_map, ent->op, ent);
629 static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason)
631 be_node_attr_t *at = (be_node_attr_t *) &irn->attr;
635 assert(is_be_node(irn));
639 case dump_node_opcode_txt:
640 fprintf(f, get_op_name(bo->op));
642 case dump_node_mode_txt:
643 fprintf(f, get_mode_name(get_irn_mode(irn)));
645 case dump_node_nodeattr_txt:
646 fprintf(f, "%s ", bo->cls->name);
648 case dump_node_info_txt:
649 for(i = 0; i < at->n_regs; ++i) {
650 const arch_register_t *reg = at->reg_data[i].reg;
651 fprintf(f, "reg #%d: %s\n", i, reg ? reg->name : "n/a");
654 if(bo->kind == node_kind_spill) {
655 be_spill_attr_t *a = (be_spill_attr_t *) at;
656 ir_fprintf(f, "spill context: %+F\n", a->spill_ctx);
657 ir_fprintf(f, "spill offset: %u\n", a->offset);
665 ir_node *insert_Perm_after(const be_main_env_t *env,
666 const arch_register_class_t *cls,
667 dom_front_info_t *dom_front,
670 const arch_env_t *arch_env = env->arch_env;
671 ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
672 ir_graph *irg = get_irn_irg(bl);
673 pset *live = pset_new_ptr_default();
674 firm_dbg_module_t *dbg = firm_dbg_register("be.node");
677 ir_node *curr, *irn, *perm, **nodes;
680 DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
683 live_foreach(bl, li) {
684 ir_node *irn = (ir_node *) li->irn;
685 if(live_is_end(li) && arch_irn_has_reg_class(arch_env, irn, -1, cls))
686 pset_insert_ptr(live, irn);
689 sched_foreach_reverse(bl, irn) {
693 * If we encounter the node we want to insert the Perm after,
694 * exit immediately, so that this node is still live
699 DBG((dbg, LEVEL_1, "%+F\n", irn));
700 for(x = pset_first(live); x; x = pset_next(live))
701 DBG((dbg, LEVEL_1, "\tlive: %+F\n", x));
703 if(arch_irn_has_reg_class(arch_env, irn, -1, cls))
704 pset_remove_ptr(live, irn);
706 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
707 ir_node *op = get_irn_n(irn, i);
709 if(arch_irn_has_reg_class(arch_env, op, -1, cls))
710 pset_insert_ptr(live, op);
714 n = pset_count(live);
715 nodes = malloc(n * sizeof(nodes[0]));
717 DBG((dbg, LEVEL_1, "live:\n"));
718 for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++) {
719 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
723 perm = new_Perm(env->node_factory, cls, irg, bl, n, nodes);
724 sched_add_after(pos, perm);
728 for(i = 0; i < n; ++i) {
730 ir_node *perm_op = get_irn_n(perm, i);
731 const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op);
733 ir_mode *mode = get_irn_mode(perm_op);
734 ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
735 arch_set_irn_register(arch_env, proj, reg);
737 sched_add_after(curr, proj);
741 be_introduce_copies(dom_front, perm_op, array_size(copies), copies);