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 = {
94 static be_node_attr_t *init_node_attr(ir_node *irn,
95 const be_node_factory_t *fact, int n_regs)
97 be_node_attr_t *attr = (be_node_attr_t *) &irn->attr;
100 attr->n_regs = n_regs;
101 attr->factory = fact;
103 for(i = 0; i < n_regs; ++i)
109 #define ARRSIZE(x) (sizeof(x) / sizeof(x[0]))
111 static int cmp_op_map(const void *a, const void *b, size_t size)
113 const be_op_t *x = a;
114 const be_op_t *y = b;
116 return !(x->kind == y->kind && x->cls == y->cls);
119 static be_op_t *get_op(const be_node_factory_t *fact,
120 const arch_register_class_t *cls, node_kind_t kind)
127 return set_insert(fact->ops, &templ, sizeof(templ),
128 HASH_PTR(cls) + 7 * kind);
131 ir_node *new_Spill(const be_node_factory_t *factory,
132 const arch_register_class_t *cls,
133 ir_graph *irg, ir_node *bl, ir_node *node_to_spill, ir_node *ctx)
135 be_spill_attr_t *attr;
138 ir_op *op = get_op(factory, cls, node_kind_spill)->op;
140 assert(op && "Spill opcode must be present for this register class");
141 in[0] = node_to_spill;
142 irn = new_ir_node(NULL, irg, bl, op, mode_M, 1, in);
143 attr = (be_spill_attr_t *) init_node_attr(irn, factory, 0);
144 attr->spill_ctx = ctx;
149 ir_node *new_Reload(const be_node_factory_t *factory,
150 const arch_register_class_t *cls, ir_graph *irg,
151 ir_node *bl, ir_mode *mode, ir_node *spill_node)
153 ir_node *irn, *in[1];
154 ir_op *op = get_op(factory, cls, node_kind_reload)->op;
156 assert(op && "Reload opcode must be present for this register class");
157 // assert(is_Spill(factory, spill_node) && "Operand of Reload must be a Spill");
160 irn = new_ir_node(NULL, irg, bl, op, mode, 1, in);
161 init_node_attr(irn, factory, 1);
166 ir_node *new_Perm(const be_node_factory_t *factory,
167 const arch_register_class_t *cls,
168 ir_graph *irg, ir_node *bl, int arity, ir_node **in)
171 ir_op *op = get_op(factory, cls, node_kind_perm)->op;
173 irn = new_ir_node(NULL, irg, bl, op, mode_T, arity, in);
174 init_node_attr(irn, factory, arity);
179 ir_node *new_Copy(const be_node_factory_t *factory,
180 const arch_register_class_t *cls,
181 ir_graph *irg, ir_node *bl, ir_node *in)
183 ir_node *irn, *ins[1];
184 ir_op *op = get_op(factory, cls, node_kind_copy)->op;
188 irn = new_ir_node(NULL, irg, bl, op, get_irn_mode(in), 1, ins);
189 init_node_attr(irn, factory, 1);
195 const be_node_factory_t *factory,
196 const arch_env_t *arch_env,
197 ir_node *irn, ir_node *ctx)
199 const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, irn, -1);
201 ir_node *bl = get_nodes_block(irn);
202 ir_graph *irg = get_irn_irg(bl);
203 ir_node *spill = new_Spill(factory, cls, irg, bl, irn, ctx);
207 * search the right insertion point. a spill of a phi cannot be put
208 * directly after the phi, if there are some phis behind the one which
211 insert = sched_next(irn);
212 while(is_Phi(insert) && !sched_is_end(insert))
213 insert = sched_next(insert);
215 sched_add_before(insert, spill);
219 ir_node *be_reload(const be_node_factory_t *factory,
220 const arch_env_t *arch_env,
221 const arch_register_class_t *cls,
222 ir_node *irn, int pos, ir_mode *mode, ir_node *spill)
226 ir_node *bl = get_nodes_block(irn);
227 ir_graph *irg = get_irn_irg(bl);
229 assert(is_Spill(factory, spill)
230 || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
232 reload = new_Reload(factory, cls, irg, bl, mode, spill);
234 set_irn_n(irn, pos, reload);
235 sched_add_before(irn, reload);
240 * If the node is a proj, reset the node to the proj's target and return
242 * @param node The address of a node pointer.
243 * @param def A default value.
244 * @return If *node is a Proj, *node is set to the Proj's target and
245 * the Proj number is returned. Else *node remains the same and @p def
248 static int redir_proj(const ir_node **node, int def)
250 const ir_node *n = *node;
253 *node = get_Proj_pred(n);
254 def = -(get_Proj_proj(n) + 1);
260 static const arch_register_req_t *
261 be_node_get_irn_reg_req(const arch_irn_ops_t *_self,
262 arch_register_req_t *req, const ir_node *irn, int pos)
265 const be_node_factory_t *factory =
266 container_of(_self, const be_node_factory_t, irn_ops);
269 * were interested in an output operand, so
270 * let's resolve projs.
273 pos = redir_proj((const ir_node **) &irn, pos);
275 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
280 req->type = arch_register_req_type_normal;
283 for(i = 0; i < bo->n_pos; ++i)
284 if(pos == bo->pos[i])
292 be_node_set_irn_reg(const arch_irn_ops_t *_self, ir_node *irn,
293 const arch_register_t *reg)
297 be_node_attr_t *attr;
298 const be_node_factory_t *factory =
299 container_of(_self, const be_node_factory_t, irn_ops);
301 pos = redir_proj((const ir_node **) &irn, -1);
302 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
307 attr = (be_node_attr_t *) &irn->attr;
308 attr->reg[-pos - 1] = reg;
311 const arch_register_t *
312 be_node_get_irn_reg(const arch_irn_ops_t *_self, const ir_node *irn)
316 const be_node_factory_t *factory =
317 container_of(_self, const be_node_factory_t, irn_ops);
319 pos = redir_proj((const ir_node **) &irn, -1);
320 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
325 for(i = 0; i < bo->n_pos; ++i) {
326 if(bo->pos[i] == pos) {
327 be_node_attr_t *attr = (be_node_attr_t *) &irn->attr;
328 return attr->reg[-pos - 1];
335 arch_irn_class_t be_node_classify(const arch_irn_ops_t *_self, const ir_node *irn)
337 const be_node_factory_t *factory =
338 container_of(_self, const be_node_factory_t, irn_ops);
343 idx = redir_proj(&irn, 0);
344 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
347 #define XXX(a) case node_kind_ ## a: return arch_irn_class_ ## a;
360 arch_irn_class_t be_node_get_flags(const arch_irn_ops_t *_self, const ir_node *irn)
365 static const arch_irn_ops_t *
366 be_node_get_irn_ops(const arch_irn_handler_t *_self, const ir_node *irn)
369 const be_node_factory_t *factory =
370 container_of(_self, const be_node_factory_t, handler);
373 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
375 return bo ? &factory->irn_ops : NULL;
378 const arch_irn_handler_t *be_node_get_irn_handler(const be_node_factory_t *f)
383 int is_Spill(const be_node_factory_t *f, const ir_node *irn)
386 bo = pmap_get(f->irn_op_map, get_irn_op(irn));
387 return bo != NULL && bo->kind == node_kind_spill;
390 be_node_factory_t *be_node_factory_init(be_node_factory_t *factory, const arch_isa_t *isa)
394 factory->ops = new_set(cmp_op_map, 64);
395 factory->irn_op_map = pmap_create();
396 obstack_init(&factory->obst);
398 factory->handler.get_irn_ops = be_node_get_irn_ops;
400 factory->irn_ops.get_irn_reg_req = be_node_get_irn_reg_req;
401 factory->irn_ops.set_irn_reg = be_node_set_irn_reg;
402 factory->irn_ops.get_irn_reg = be_node_get_irn_reg;
403 factory->irn_ops.classify = be_node_classify;
404 factory->irn_ops.get_flags = be_node_get_flags;
406 for(i = 0, n = arch_isa_get_n_reg_class(isa); i < n; ++i) {
407 const arch_register_class_t *cls = arch_isa_get_reg_class(isa, i);
410 ent = get_op(factory, cls, node_kind_spill);
411 ent->op = new_ir_op(get_next_ir_opcode(), "Spill", op_pin_state_pinned,
412 0, oparity_unary, 0, sizeof(be_spill_attr_t), &be_node_ops);
413 ent->n_pos = ARRSIZE(templ_pos_Spill);
414 ent->pos = templ_pos_Spill;
415 pmap_insert(factory->irn_op_map, ent->op, ent);
417 ent = get_op(factory, cls, node_kind_reload);
418 ent->op = new_ir_op(get_next_ir_opcode(), "Reload", op_pin_state_pinned, 0,
419 oparity_unary, 0, sizeof(be_node_attr_t), &be_node_ops);
420 ent->n_pos = ARRSIZE(templ_pos_Reload);
421 ent->pos = templ_pos_Reload;
422 pmap_insert(factory->irn_op_map, ent->op, ent);
424 ent = get_op(factory, cls, node_kind_copy);
425 ent->op = new_ir_op(get_next_ir_opcode(), "Copy", op_pin_state_pinned, 0,
426 oparity_unary, 0, sizeof(be_node_attr_t), &be_node_ops);
427 ent->n_pos = ARRSIZE(templ_pos_Copy);
428 ent->pos = templ_pos_Copy;
429 pmap_insert(factory->irn_op_map, ent->op, ent);
431 ent = get_op(factory, cls, node_kind_perm);
432 ent->op = new_ir_op(get_next_ir_opcode(), "Perm", op_pin_state_pinned, 0,
434 sizeof(be_node_attr_t) + sizeof(arch_register_t) * cls->n_regs, &be_node_ops);
435 ent->n_pos = 2 * cls->n_regs;
436 ent->pos = obstack_alloc(&factory->obst, sizeof(ent->pos[0]) * ent->n_pos);
437 for(j = 0; j < ent->n_pos; j += 2) {
440 ent->pos[j + 1] = -(k + 1);
442 pmap_insert(factory->irn_op_map, ent->op, ent);
449 static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason)
451 be_node_attr_t *attr = (be_node_attr_t *) &irn->attr;
452 be_op_t *bo = pmap_get(attr->factory->irn_op_map, get_irn_op(irn));
457 case dump_node_opcode_txt:
458 fprintf(f, get_op_name(bo->op));
460 case dump_node_mode_txt:
461 fprintf(f, get_mode_name(get_irn_mode(irn)));
463 case dump_node_nodeattr_txt:
464 fprintf(f, bo->cls->name);
466 case dump_node_info_txt:
467 for(i = 0; i < attr->n_regs; ++i) {
468 const arch_register_t *reg = attr->reg[i];
469 fprintf(f, "reg #%d: %s\n", i, reg ? reg->name : "n/a");
472 if(bo->kind == node_kind_spill) {
473 be_spill_attr_t *a = (be_spill_attr_t *) attr;
474 ir_fprintf(f, "spill context: %+F\n", a->spill_ctx);
482 ir_node *insert_Perm_after(const be_main_env_t *env,
483 const arch_register_class_t *cls,
484 dom_front_info_t *dom_front,
487 const arch_env_t *arch_env = env->arch_env;
488 ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
489 ir_graph *irg = get_irn_irg(bl);
490 pset *live = put_live_end(bl, pset_new_ptr_default());
491 firm_dbg_module_t *dbg = firm_dbg_register("firm.be.node");
493 ir_node *curr, *irn, *perm, **nodes;
496 firm_dbg_set_mask(dbg, DBG_LEVEL);
497 DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
499 sched_foreach_reverse(bl, irn) {
503 * If we encounter the node we want to insert the Perm after,
504 * exit immediately, so that this node is still live
509 DBG((dbg, LEVEL_1, "%+F\n", irn));
510 for(x = pset_first(live); x; x = pset_next(live))
511 DBG((dbg, LEVEL_1, "\tlive: %+F\n", x));
513 if(arch_irn_has_reg_class(arch_env, irn, -1, cls))
514 pset_remove_ptr(live, irn);
516 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
517 ir_node *op = get_irn_n(irn, i);
519 if(arch_irn_has_reg_class(arch_env, op, -1, cls))
520 pset_insert_ptr(live, op);
524 n = pset_count(live);
525 nodes = malloc(n * sizeof(nodes[0]));
527 DBG((dbg, LEVEL_1, "live:\n"));
528 for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++) {
529 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
533 perm = new_Perm(env->node_factory, cls, irg, bl, n, nodes);
534 sched_add_after(pos, perm);
538 for(i = 0; i < n; ++i) {
540 ir_node *perm_op = get_irn_n(perm, i);
541 const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op);
543 ir_mode *mode = get_irn_mode(perm_op);
544 ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
545 arch_set_irn_register(arch_env, proj, reg);
547 sched_add_after(curr, proj);
551 be_introduce_copies(dom_front, perm_op, array_size(copies), copies);