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 {
54 const arch_register_class_t *cls;
61 const arch_register_t *reg;
62 arch_register_req_t req;
69 be_reg_data_t reg_data[1];
74 ir_node *spill_ctx; /**< The node in whose context this spill was introduced. */
75 unsigned offset; /**< The offset of the memory location the spill writes to
79 static int templ_pos_Spill[] = {
83 static int templ_pos_Reload[] = {
87 static int templ_pos_Copy[] = {
91 static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason);
93 static const ir_op_ops be_node_ops = {
109 static INLINE int is_be_node(const ir_node *irn)
111 const be_node_attr_t *attr = (const be_node_attr_t *) &irn->attr;
112 return attr->magic == BENODE_MAGIC;
115 static INLINE int is_be_kind(const ir_node *irn, node_kind_t kind)
117 const be_node_attr_t *a = (const be_node_attr_t *) &irn->attr;
118 return a->magic == BENODE_MAGIC && a->op && a->op->kind == kind;
121 static INLINE void *get_attr_and_check(ir_node *irn, node_kind_t kind)
123 is_be_kind(irn, kind);
127 static be_node_attr_t *init_node_attr(ir_node *irn,
129 const arch_register_class_t *cls,
133 be_node_attr_t *attr = (be_node_attr_t *) &irn->attr;
136 attr->magic = BENODE_MAGIC;
137 attr->n_regs = n_regs;
140 for(i = 0; i < n_regs; ++i) {
141 be_reg_data_t *rd = attr->reg_data + i;
145 rd->req.type = arch_register_req_type_normal;
151 #define ARRSIZE(x) (sizeof(x) / sizeof(x[0]))
153 static int cmp_op_map(const void *a, const void *b, size_t size)
155 const be_op_t *x = a;
156 const be_op_t *y = b;
158 return !(x->kind == y->kind && x->cls == y->cls);
161 static be_op_t *get_op(const be_node_factory_t *fact,
162 const arch_register_class_t *cls, node_kind_t kind)
169 return set_insert(fact->ops, &templ, sizeof(templ),
170 HASH_PTR(cls) + 7 * kind);
173 ir_node *new_Spill(const be_node_factory_t *factory,
174 const arch_register_class_t *cls,
175 ir_graph *irg, ir_node *bl, ir_node *node_to_spill, ir_node *ctx)
180 be_op_t *bop = get_op(factory, cls, node_kind_spill);
183 assert(op && "Spill opcode must be present for this register class");
184 in[0] = node_to_spill;
185 irn = new_ir_node(NULL, irg, bl, op, mode_M, 1, in);
186 a = (be_spill_attr_t *) init_node_attr(irn, bop, cls, 0);
193 void set_Spill_offset(ir_node *irn, unsigned offset)
195 be_spill_attr_t *a = (be_spill_attr_t *) &irn->attr;
196 assert(is_be_kind(irn, node_kind_spill));
200 static ir_node *find_a_spill_walker(ir_node *irn, unsigned visited_nr)
202 if(get_irn_visited(irn) < visited_nr) {
203 set_irn_visited(irn, visited_nr);
207 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
208 ir_node *n = find_a_spill_walker(get_irn_n(irn, i), visited_nr);
214 else if(is_be_kind(irn, node_kind_spill))
221 ir_node *get_Spill_context(const ir_node *irn) {
222 be_spill_attr_t *a = (be_spill_attr_t *) &irn->attr;
223 assert(is_be_kind(irn, node_kind_spill));
228 * Finds a spill for a reload.
229 * If the reload is directly using the spill, this is simple,
230 * else we perform DFS from the reload (over all PhiMs) and return
231 * the first spill node we find.
233 static INLINE ir_node *find_a_spill(ir_node *irn)
235 ir_graph *irg = get_irn_irg(irn);
236 unsigned visited_nr = get_irg_visited(irg) + 1;
238 assert(is_be_kind(irn, node_kind_reload));
239 set_irg_visited(irg, visited_nr);
240 return find_a_spill_walker(irn, visited_nr);
244 unsigned get_Spill_offset(ir_node *irn)
246 be_node_attr_t *a = (be_node_attr_t *) &irn->attr;
247 assert(is_be_node(irn));
249 switch(a->op->kind) {
250 case node_kind_reload:
251 assert(0 && "not yet implemented");
252 return get_Spill_offset(find_a_spill(irn));
253 case node_kind_spill:
254 return ((be_spill_attr_t *) a)->offset;
256 assert(0 && "Illegal node kind (spill/reload required)");
262 ir_node *new_Reload(const be_node_factory_t *factory,
263 const arch_register_class_t *cls, ir_graph *irg,
264 ir_node *bl, ir_mode *mode, ir_node *spill_node)
266 ir_node *irn, *in[1];
267 be_op_t *bop = get_op(factory, cls, node_kind_reload);
270 assert(op && "Reload opcode must be present for this register class");
273 irn = new_ir_node(NULL, irg, bl, op, mode, 1, in);
274 init_node_attr(irn, bop, cls, 1);
279 ir_node *new_Perm(const be_node_factory_t *factory,
280 const arch_register_class_t *cls,
281 ir_graph *irg, ir_node *bl, int arity, ir_node **in)
284 be_op_t *bop = get_op(factory, cls, node_kind_perm);
287 irn = new_ir_node(NULL, irg, bl, op, mode_T, arity, in);
288 init_node_attr(irn, bop, cls, arity);
293 ir_node *new_Copy(const be_node_factory_t *factory,
294 const arch_register_class_t *cls,
295 ir_graph *irg, ir_node *bl, ir_node *in)
297 ir_node *irn, *ins[1];
298 be_op_t *bop = get_op(factory, cls, node_kind_copy);
303 irn = new_ir_node(NULL, irg, bl, op, get_irn_mode(in), 1, ins);
304 init_node_attr(irn, bop, cls, 1);
310 const be_node_factory_t *factory,
311 const arch_env_t *arch_env,
312 ir_node *irn, ir_node *ctx)
314 const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, irn, -1);
316 ir_node *bl = get_nodes_block(irn);
317 ir_graph *irg = get_irn_irg(bl);
318 ir_node *spill = new_Spill(factory, cls, irg, bl, irn, ctx);
322 * search the right insertion point. a spill of a phi cannot be put
323 * directly after the phi, if there are some phis behind the one which
326 insert = sched_next(irn);
327 while(is_Phi(insert) && !sched_is_end(insert))
328 insert = sched_next(insert);
330 sched_add_before(insert, spill);
334 ir_node *be_reload(const be_node_factory_t *factory,
335 const arch_env_t *arch_env,
336 const arch_register_class_t *cls,
337 ir_node *irn, int pos, ir_mode *mode, ir_node *spill)
341 ir_node *bl = get_nodes_block(irn);
342 ir_graph *irg = get_irn_irg(bl);
344 assert(be_is_Spill(spill)
345 || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
347 reload = new_Reload(factory, cls, irg, bl, mode, spill);
349 set_irn_n(irn, pos, reload);
350 sched_add_before(irn, reload);
355 * If the node is a proj, reset the node to the proj's target and return
357 * @param node The address of a node pointer.
358 * @param def A default value.
359 * @return If *node is a Proj, *node is set to the Proj's target and
360 * the Proj number is returned. Else *node remains the same and @p def
363 static int redir_proj(const ir_node **node, int def)
365 const ir_node *n = *node;
368 *node = get_Proj_pred(n);
369 def = -(get_Proj_proj(n) + 1);
375 static const arch_register_req_t *
376 be_node_get_irn_reg_req(const arch_irn_ops_t *_self,
377 arch_register_req_t *req, const ir_node *irn, int pos)
379 /* We cannot get output requirements for tuple nodes. */
380 if(get_irn_mode(irn) == mode_T && pos < 0)
384 * if we're interested in an output operand (pos < 0), so let's resolve projs.
387 pos = redir_proj((const ir_node **) &irn, pos);
389 if(is_be_node(irn)) {
390 const be_node_attr_t *a = (const be_node_attr_t *) &irn->attr;
391 const be_op_t *bo = a->op;
394 for(i = 0; i < bo->n_pos; ++i) {
395 if(pos == bo->pos[i]) {
397 /* be nodes have no input constraints.
398 so return normal register requirements. */
401 req->type = arch_register_req_type_normal;
405 * if an output requirement is requested,
406 * return the one stored in the node.
409 *req = a->reg_data[-pos - 1].req;
419 void be_set_Perm_out_req(ir_node *irn, int pos, const arch_register_req_t *req)
421 be_node_attr_t *a = get_attr_and_check(irn, node_kind_perm);
422 assert(pos >= 0 && pos < get_irn_arity(irn) && "position out of range");
423 a->reg_data[pos].req = *req;
427 be_node_set_irn_reg(const arch_irn_ops_t *_self, ir_node *irn,
428 const arch_register_t *reg)
432 be_node_attr_t *attr;
433 const be_node_factory_t *factory =
434 container_of(_self, const be_node_factory_t, irn_ops);
436 if(get_irn_mode(irn) == mode_T)
439 pos = redir_proj((const ir_node **) &irn, -1);
440 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
445 attr = (be_node_attr_t *) &irn->attr;
446 attr->reg_data[-pos - 1].reg = reg;
449 const arch_register_t *
450 be_node_get_irn_reg(const arch_irn_ops_t *_self, const ir_node *irn)
454 const be_node_factory_t *factory =
455 container_of(_self, const be_node_factory_t, irn_ops);
457 if(get_irn_mode(irn) == mode_T)
460 pos = redir_proj((const ir_node **) &irn, -1);
461 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
466 for(i = 0; i < bo->n_pos; ++i) {
467 if(bo->pos[i] == pos) {
468 be_node_attr_t *attr = (be_node_attr_t *) &irn->attr;
469 return attr->reg_data[-pos - 1].reg;
476 arch_irn_class_t be_node_classify(const arch_irn_ops_t *_self, const ir_node *irn)
478 const be_node_factory_t *factory = container_of(_self, const be_node_factory_t, irn_ops);
483 idx = redir_proj(&irn, 0);
484 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
487 #define XXX(a) case node_kind_ ## a: return arch_irn_class_ ## a;
500 arch_irn_class_t be_node_get_flags(const arch_irn_ops_t *_self, const ir_node *irn)
505 static const arch_irn_ops_t *
506 be_node_get_irn_ops(const arch_irn_handler_t *_self, const ir_node *irn)
509 const be_node_factory_t *factory =
510 container_of(_self, const be_node_factory_t, handler);
513 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
515 return bo ? &factory->irn_ops : NULL;
518 const arch_irn_handler_t *be_node_get_irn_handler(const be_node_factory_t *f)
523 int be_is_Spill(const ir_node *irn)
525 return is_be_kind(irn, node_kind_spill);
528 int be_is_Reload(const ir_node *irn)
530 return is_be_kind(irn, node_kind_reload);
533 int be_is_Copy(const ir_node *irn)
535 return is_be_kind(irn, node_kind_copy);
538 int be_is_Perm(const ir_node *irn)
540 return is_be_kind(irn, node_kind_perm);
543 be_node_factory_t *be_node_factory_init(be_node_factory_t *factory, const arch_isa_t *isa)
547 factory->ops = new_set(cmp_op_map, 64);
548 factory->irn_op_map = pmap_create();
549 obstack_init(&factory->obst);
551 factory->handler.get_irn_ops = be_node_get_irn_ops;
553 factory->irn_ops.get_irn_reg_req = be_node_get_irn_reg_req;
554 factory->irn_ops.set_irn_reg = be_node_set_irn_reg;
555 factory->irn_ops.get_irn_reg = be_node_get_irn_reg;
556 factory->irn_ops.classify = be_node_classify;
557 factory->irn_ops.get_flags = be_node_get_flags;
559 for(i = 0, n = arch_isa_get_n_reg_class(isa); i < n; ++i) {
560 const arch_register_class_t *cls = arch_isa_get_reg_class(isa, i);
563 ent = get_op(factory, cls, node_kind_spill);
564 ent->op = new_ir_op(get_next_ir_opcode(), "Spill", op_pin_state_pinned,
565 0, oparity_unary, 0, sizeof(be_spill_attr_t), &be_node_ops);
566 ent->n_pos = ARRSIZE(templ_pos_Spill);
567 ent->pos = templ_pos_Spill;
568 pmap_insert(factory->irn_op_map, ent->op, ent);
570 ent = get_op(factory, cls, node_kind_reload);
571 ent->op = new_ir_op(get_next_ir_opcode(), "Reload", op_pin_state_pinned, 0,
572 oparity_unary, 0, sizeof(be_node_attr_t), &be_node_ops);
573 ent->n_pos = ARRSIZE(templ_pos_Reload);
574 ent->pos = templ_pos_Reload;
575 pmap_insert(factory->irn_op_map, ent->op, ent);
577 ent = get_op(factory, cls, node_kind_copy);
578 ent->op = new_ir_op(get_next_ir_opcode(), "Copy", op_pin_state_pinned, 0,
579 oparity_unary, 0, sizeof(be_node_attr_t), &be_node_ops);
580 ent->n_pos = ARRSIZE(templ_pos_Copy);
581 ent->pos = templ_pos_Copy;
582 pmap_insert(factory->irn_op_map, ent->op, ent);
584 ent = get_op(factory, cls, node_kind_perm);
585 ent->op = new_ir_op(get_next_ir_opcode(), "Perm", op_pin_state_pinned, 0,
587 sizeof(be_node_attr_t)
588 + sizeof(be_reg_data_t) * cls->n_regs, &be_node_ops);
589 ent->n_pos = 2 * cls->n_regs;
590 ent->pos = obstack_alloc(&factory->obst, sizeof(ent->pos[0]) * ent->n_pos);
591 for(j = 0; j < ent->n_pos; j += 2) {
594 ent->pos[j + 1] = -(k + 1);
596 pmap_insert(factory->irn_op_map, ent->op, ent);
603 static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason)
605 be_node_attr_t *at = (be_node_attr_t *) &irn->attr;
609 assert(is_be_node(irn));
613 case dump_node_opcode_txt:
614 fprintf(f, get_op_name(bo->op));
616 case dump_node_mode_txt:
617 fprintf(f, get_mode_name(get_irn_mode(irn)));
619 case dump_node_nodeattr_txt:
620 fprintf(f, "%s ", bo->cls->name);
622 case dump_node_info_txt:
623 for(i = 0; i < at->n_regs; ++i) {
624 const arch_register_t *reg = at->reg_data[i].reg;
625 fprintf(f, "reg #%d: %s\n", i, reg ? reg->name : "n/a");
628 if(bo->kind == node_kind_spill) {
629 be_spill_attr_t *a = (be_spill_attr_t *) at;
630 ir_fprintf(f, "spill context: %+F\n", a->spill_ctx);
638 ir_node *insert_Perm_after(const be_main_env_t *env,
639 const arch_register_class_t *cls,
640 dom_front_info_t *dom_front,
643 const arch_env_t *arch_env = env->arch_env;
644 ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
645 ir_graph *irg = get_irn_irg(bl);
646 pset *live = pset_new_ptr_default();
647 firm_dbg_module_t *dbg = firm_dbg_register("be.node");
650 ir_node *curr, *irn, *perm, **nodes;
653 DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
656 live_foreach(bl, li) {
657 ir_node *irn = (ir_node *) li->irn;
658 if(live_is_end(li) && arch_irn_has_reg_class(arch_env, irn, -1, cls))
659 pset_insert_ptr(live, irn);
662 sched_foreach_reverse(bl, irn) {
666 * If we encounter the node we want to insert the Perm after,
667 * exit immediately, so that this node is still live
672 DBG((dbg, LEVEL_1, "%+F\n", irn));
673 for(x = pset_first(live); x; x = pset_next(live))
674 DBG((dbg, LEVEL_1, "\tlive: %+F\n", x));
676 if(arch_irn_has_reg_class(arch_env, irn, -1, cls))
677 pset_remove_ptr(live, irn);
679 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
680 ir_node *op = get_irn_n(irn, i);
682 if(arch_irn_has_reg_class(arch_env, op, -1, cls))
683 pset_insert_ptr(live, op);
687 n = pset_count(live);
688 nodes = malloc(n * sizeof(nodes[0]));
690 DBG((dbg, LEVEL_1, "live:\n"));
691 for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++) {
692 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
696 perm = new_Perm(env->node_factory, cls, irg, bl, n, nodes);
697 sched_add_after(pos, perm);
701 for(i = 0; i < n; ++i) {
703 ir_node *perm_op = get_irn_n(perm, i);
704 const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op);
706 ir_mode *mode = get_irn_mode(perm_op);
707 ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
708 arch_set_irn_register(arch_env, proj, reg);
710 sched_add_after(curr, proj);
714 be_introduce_copies(dom_front, perm_op, array_size(copies), copies);