4 * @author Sebastian Hack
6 * Backend node support.
8 * Copyright (C) 2005 Universitaet Karlsruhe
9 * Released under the GPL
32 #include "besched_t.h"
39 typedef enum _node_kind_t {
49 const arch_register_class_t *cls;
56 const be_node_factory_t *factory;
58 const arch_register_t *reg[1];
66 static int templ_pos_Spill[] = {
70 static int templ_pos_Reload[] = {
74 static int templ_pos_Copy[] = {
78 static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason);
80 static const ir_op_ops be_node_ops = {
96 static be_node_attr_t *init_node_attr(ir_node *irn,
97 const be_node_factory_t *fact, int n_regs)
99 be_node_attr_t *attr = (be_node_attr_t *) &irn->attr;
102 attr->n_regs = n_regs;
103 attr->factory = fact;
105 for(i = 0; i < n_regs; ++i)
111 #define ARRSIZE(x) (sizeof(x) / sizeof(x[0]))
113 static int cmp_op_map(const void *a, const void *b, size_t size)
115 const be_op_t *x = a;
116 const be_op_t *y = b;
118 return !(x->kind == y->kind && x->cls == y->cls);
121 static be_op_t *get_op(const be_node_factory_t *fact,
122 const arch_register_class_t *cls, node_kind_t kind)
129 return set_insert(fact->ops, &templ, sizeof(templ),
130 HASH_PTR(cls) + 7 * kind);
133 ir_node *new_Spill(const be_node_factory_t *factory,
134 const arch_register_class_t *cls,
135 ir_graph *irg, ir_node *bl, ir_node *node_to_spill, ir_node *ctx)
137 be_spill_attr_t *attr;
140 ir_op *op = get_op(factory, cls, node_kind_spill)->op;
142 assert(op && "Spill opcode must be present for this register class");
143 in[0] = node_to_spill;
144 irn = new_ir_node(NULL, irg, bl, op, mode_M, 1, in);
145 attr = (be_spill_attr_t *) init_node_attr(irn, factory, 0);
146 attr->spill_ctx = ctx;
151 ir_node *new_Reload(const be_node_factory_t *factory,
152 const arch_register_class_t *cls, ir_graph *irg,
153 ir_node *bl, ir_mode *mode, ir_node *spill_node)
155 ir_node *irn, *in[1];
156 ir_op *op = get_op(factory, cls, node_kind_reload)->op;
158 assert(op && "Reload opcode must be present for this register class");
159 // assert(is_Spill(factory, spill_node) && "Operand of Reload must be a Spill");
162 irn = new_ir_node(NULL, irg, bl, op, mode, 1, in);
163 init_node_attr(irn, factory, 1);
168 ir_node *new_Perm(const be_node_factory_t *factory,
169 const arch_register_class_t *cls,
170 ir_graph *irg, ir_node *bl, int arity, ir_node **in)
173 ir_op *op = get_op(factory, cls, node_kind_perm)->op;
175 irn = new_ir_node(NULL, irg, bl, op, mode_T, arity, in);
176 init_node_attr(irn, factory, arity);
181 ir_node *new_Copy(const be_node_factory_t *factory,
182 const arch_register_class_t *cls,
183 ir_graph *irg, ir_node *bl, ir_node *in)
185 ir_node *irn, *ins[1];
186 ir_op *op = get_op(factory, cls, node_kind_copy)->op;
190 irn = new_ir_node(NULL, irg, bl, op, get_irn_mode(in), 1, ins);
191 init_node_attr(irn, factory, 1);
197 const be_node_factory_t *factory,
198 const arch_env_t *arch_env,
199 ir_node *irn, ir_node *ctx)
201 const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, irn, -1);
203 ir_node *bl = get_nodes_block(irn);
204 ir_graph *irg = get_irn_irg(bl);
205 ir_node *spill = new_Spill(factory, cls, irg, bl, irn, ctx);
209 * search the right insertion point. a spill of a phi cannot be put
210 * directly after the phi, if there are some phis behind the one which
213 insert = sched_next(irn);
214 while(is_Phi(insert) && !sched_is_end(insert))
215 insert = sched_next(insert);
217 sched_add_before(insert, spill);
221 ir_node *be_reload(const be_node_factory_t *factory,
222 const arch_env_t *arch_env,
223 const arch_register_class_t *cls,
224 ir_node *irn, int pos, ir_mode *mode, ir_node *spill)
228 ir_node *bl = get_nodes_block(irn);
229 ir_graph *irg = get_irn_irg(bl);
231 assert(is_Spill(factory, spill)
232 || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
234 reload = new_Reload(factory, cls, irg, bl, mode, spill);
236 set_irn_n(irn, pos, reload);
237 sched_add_before(irn, reload);
242 * If the node is a proj, reset the node to the proj's target and return
244 * @param node The address of a node pointer.
245 * @param def A default value.
246 * @return If *node is a Proj, *node is set to the Proj's target and
247 * the Proj number is returned. Else *node remains the same and @p def
250 static int redir_proj(const ir_node **node, int def)
252 const ir_node *n = *node;
255 *node = get_Proj_pred(n);
256 def = -(get_Proj_proj(n) + 1);
262 static const arch_register_req_t *
263 be_node_get_irn_reg_req(const arch_irn_ops_t *_self,
264 arch_register_req_t *req, const ir_node *irn, int pos)
267 const be_node_factory_t *factory =
268 container_of(_self, const be_node_factory_t, irn_ops);
270 if(get_irn_mode(irn) == mode_T && pos < 0)
274 * were interested in an output operand, so
275 * let's resolve projs.
278 pos = redir_proj((const ir_node **) &irn, pos);
280 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
285 req->type = arch_register_req_type_normal;
288 for(i = 0; i < bo->n_pos; ++i)
289 if(pos == bo->pos[i])
297 be_node_set_irn_reg(const arch_irn_ops_t *_self, ir_node *irn,
298 const arch_register_t *reg)
302 be_node_attr_t *attr;
303 const be_node_factory_t *factory =
304 container_of(_self, const be_node_factory_t, irn_ops);
306 if(get_irn_mode(irn) == mode_T && pos < 0)
309 pos = redir_proj((const ir_node **) &irn, -1);
310 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
315 attr = (be_node_attr_t *) &irn->attr;
316 attr->reg[-pos - 1] = reg;
319 const arch_register_t *
320 be_node_get_irn_reg(const arch_irn_ops_t *_self, const ir_node *irn)
324 const be_node_factory_t *factory =
325 container_of(_self, const be_node_factory_t, irn_ops);
327 if(get_irn_mode(irn) == mode_T && pos < 0)
330 pos = redir_proj((const ir_node **) &irn, -1);
331 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
336 for(i = 0; i < bo->n_pos; ++i) {
337 if(bo->pos[i] == pos) {
338 be_node_attr_t *attr = (be_node_attr_t *) &irn->attr;
339 return attr->reg[-pos - 1];
346 arch_irn_class_t be_node_classify(const arch_irn_ops_t *_self, const ir_node *irn)
348 const be_node_factory_t *factory = container_of(_self, const be_node_factory_t, irn_ops);
353 idx = redir_proj(&irn, 0);
354 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
357 #define XXX(a) case node_kind_ ## a: return arch_irn_class_ ## a;
370 arch_irn_class_t be_node_get_flags(const arch_irn_ops_t *_self, const ir_node *irn)
375 static const arch_irn_ops_t *
376 be_node_get_irn_ops(const arch_irn_handler_t *_self, const ir_node *irn)
379 const be_node_factory_t *factory =
380 container_of(_self, const be_node_factory_t, handler);
383 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
385 return bo ? &factory->irn_ops : NULL;
388 const arch_irn_handler_t *be_node_get_irn_handler(const be_node_factory_t *f)
393 int is_Spill(const be_node_factory_t *f, const ir_node *irn)
396 bo = pmap_get(f->irn_op_map, get_irn_op(irn));
397 return bo != NULL && bo->kind == node_kind_spill;
400 be_node_factory_t *be_node_factory_init(be_node_factory_t *factory, const arch_isa_t *isa)
404 factory->ops = new_set(cmp_op_map, 64);
405 factory->irn_op_map = pmap_create();
406 obstack_init(&factory->obst);
408 factory->handler.get_irn_ops = be_node_get_irn_ops;
410 factory->irn_ops.get_irn_reg_req = be_node_get_irn_reg_req;
411 factory->irn_ops.set_irn_reg = be_node_set_irn_reg;
412 factory->irn_ops.get_irn_reg = be_node_get_irn_reg;
413 factory->irn_ops.classify = be_node_classify;
414 factory->irn_ops.get_flags = be_node_get_flags;
416 for(i = 0, n = arch_isa_get_n_reg_class(isa); i < n; ++i) {
417 const arch_register_class_t *cls = arch_isa_get_reg_class(isa, i);
420 ent = get_op(factory, cls, node_kind_spill);
421 ent->op = new_ir_op(get_next_ir_opcode(), "Spill", op_pin_state_pinned,
422 0, oparity_unary, 0, sizeof(be_spill_attr_t), &be_node_ops);
423 ent->n_pos = ARRSIZE(templ_pos_Spill);
424 ent->pos = templ_pos_Spill;
425 pmap_insert(factory->irn_op_map, ent->op, ent);
427 ent = get_op(factory, cls, node_kind_reload);
428 ent->op = new_ir_op(get_next_ir_opcode(), "Reload", op_pin_state_pinned, 0,
429 oparity_unary, 0, sizeof(be_node_attr_t), &be_node_ops);
430 ent->n_pos = ARRSIZE(templ_pos_Reload);
431 ent->pos = templ_pos_Reload;
432 pmap_insert(factory->irn_op_map, ent->op, ent);
434 ent = get_op(factory, cls, node_kind_copy);
435 ent->op = new_ir_op(get_next_ir_opcode(), "Copy", op_pin_state_pinned, 0,
436 oparity_unary, 0, sizeof(be_node_attr_t), &be_node_ops);
437 ent->n_pos = ARRSIZE(templ_pos_Copy);
438 ent->pos = templ_pos_Copy;
439 pmap_insert(factory->irn_op_map, ent->op, ent);
441 ent = get_op(factory, cls, node_kind_perm);
442 ent->op = new_ir_op(get_next_ir_opcode(), "Perm", op_pin_state_pinned, 0,
444 sizeof(be_node_attr_t) + sizeof(arch_register_t) * cls->n_regs, &be_node_ops);
445 ent->n_pos = 2 * cls->n_regs;
446 ent->pos = obstack_alloc(&factory->obst, sizeof(ent->pos[0]) * ent->n_pos);
447 for(j = 0; j < ent->n_pos; j += 2) {
450 ent->pos[j + 1] = -(k + 1);
452 pmap_insert(factory->irn_op_map, ent->op, ent);
459 static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason)
461 be_node_attr_t *attr = (be_node_attr_t *) &irn->attr;
462 be_op_t *bo = pmap_get(attr->factory->irn_op_map, get_irn_op(irn));
467 case dump_node_opcode_txt:
468 fprintf(f, get_op_name(bo->op));
470 case dump_node_mode_txt:
471 fprintf(f, get_mode_name(get_irn_mode(irn)));
473 case dump_node_nodeattr_txt:
474 fprintf(f, "%s ", bo->cls->name);
476 case dump_node_info_txt:
477 for(i = 0; i < attr->n_regs; ++i) {
478 const arch_register_t *reg = attr->reg[i];
479 fprintf(f, "reg #%d: %s\n", i, reg ? reg->name : "n/a");
482 if(bo->kind == node_kind_spill) {
483 be_spill_attr_t *a = (be_spill_attr_t *) attr;
484 ir_fprintf(f, "spill context: %+F\n", a->spill_ctx);
492 ir_node *insert_Perm_after(const be_main_env_t *env,
493 const arch_register_class_t *cls,
494 dom_front_info_t *dom_front,
497 const arch_env_t *arch_env = env->arch_env;
498 ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
499 ir_graph *irg = get_irn_irg(bl);
500 pset *live = pset_new_ptr_default();
501 firm_dbg_module_t *dbg = firm_dbg_register("firm.be.node");
504 ir_node *curr, *irn, *perm, **nodes;
507 firm_dbg_set_mask(dbg, DBG_LEVEL);
508 DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
511 live_foreach(bl, li) {
512 ir_node *irn = (ir_node *) li->irn;
513 if(live_is_end(li) && arch_irn_has_reg_class(arch_env, irn, -1, cls))
514 pset_insert_ptr(live, irn);
517 sched_foreach_reverse(bl, irn) {
521 * If we encounter the node we want to insert the Perm after,
522 * exit immediately, so that this node is still live
527 DBG((dbg, LEVEL_1, "%+F\n", irn));
528 for(x = pset_first(live); x; x = pset_next(live))
529 DBG((dbg, LEVEL_1, "\tlive: %+F\n", x));
531 if(arch_irn_has_reg_class(arch_env, irn, -1, cls))
532 pset_remove_ptr(live, irn);
534 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
535 ir_node *op = get_irn_n(irn, i);
537 if(arch_irn_has_reg_class(arch_env, op, -1, cls))
538 pset_insert_ptr(live, op);
542 n = pset_count(live);
543 nodes = malloc(n * sizeof(nodes[0]));
545 DBG((dbg, LEVEL_1, "live:\n"));
546 for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++) {
547 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
551 perm = new_Perm(env->node_factory, cls, irg, bl, n, nodes);
552 sched_add_after(pos, perm);
556 for(i = 0; i < n; ++i) {
558 ir_node *perm_op = get_irn_n(perm, i);
559 const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op);
561 ir_mode *mode = get_irn_mode(perm_op);
562 ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
563 arch_set_irn_register(arch_env, proj, reg);
565 sched_add_after(curr, proj);
569 be_introduce_copies(dom_front, perm_op, array_size(copies), copies);