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 =
349 container_of(_self, const be_node_factory_t, irn_ops);
354 idx = redir_proj(&irn, 0);
355 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
358 #define XXX(a) case node_kind_ ## a: return arch_irn_class_ ## a;
371 arch_irn_class_t be_node_get_flags(const arch_irn_ops_t *_self, const ir_node *irn)
376 static const arch_irn_ops_t *
377 be_node_get_irn_ops(const arch_irn_handler_t *_self, const ir_node *irn)
380 const be_node_factory_t *factory =
381 container_of(_self, const be_node_factory_t, handler);
384 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
386 return bo ? &factory->irn_ops : NULL;
389 const arch_irn_handler_t *be_node_get_irn_handler(const be_node_factory_t *f)
394 int is_Spill(const be_node_factory_t *f, const ir_node *irn)
397 bo = pmap_get(f->irn_op_map, get_irn_op(irn));
398 return bo != NULL && bo->kind == node_kind_spill;
401 be_node_factory_t *be_node_factory_init(be_node_factory_t *factory, const arch_isa_t *isa)
405 factory->ops = new_set(cmp_op_map, 64);
406 factory->irn_op_map = pmap_create();
407 obstack_init(&factory->obst);
409 factory->handler.get_irn_ops = be_node_get_irn_ops;
411 factory->irn_ops.get_irn_reg_req = be_node_get_irn_reg_req;
412 factory->irn_ops.set_irn_reg = be_node_set_irn_reg;
413 factory->irn_ops.get_irn_reg = be_node_get_irn_reg;
414 factory->irn_ops.classify = be_node_classify;
415 factory->irn_ops.get_flags = be_node_get_flags;
417 for(i = 0, n = arch_isa_get_n_reg_class(isa); i < n; ++i) {
418 const arch_register_class_t *cls = arch_isa_get_reg_class(isa, i);
421 ent = get_op(factory, cls, node_kind_spill);
422 ent->op = new_ir_op(get_next_ir_opcode(), "Spill", op_pin_state_pinned,
423 0, oparity_unary, 0, sizeof(be_spill_attr_t), &be_node_ops);
424 ent->n_pos = ARRSIZE(templ_pos_Spill);
425 ent->pos = templ_pos_Spill;
426 pmap_insert(factory->irn_op_map, ent->op, ent);
428 ent = get_op(factory, cls, node_kind_reload);
429 ent->op = new_ir_op(get_next_ir_opcode(), "Reload", op_pin_state_pinned, 0,
430 oparity_unary, 0, sizeof(be_node_attr_t), &be_node_ops);
431 ent->n_pos = ARRSIZE(templ_pos_Reload);
432 ent->pos = templ_pos_Reload;
433 pmap_insert(factory->irn_op_map, ent->op, ent);
435 ent = get_op(factory, cls, node_kind_copy);
436 ent->op = new_ir_op(get_next_ir_opcode(), "Copy", op_pin_state_pinned, 0,
437 oparity_unary, 0, sizeof(be_node_attr_t), &be_node_ops);
438 ent->n_pos = ARRSIZE(templ_pos_Copy);
439 ent->pos = templ_pos_Copy;
440 pmap_insert(factory->irn_op_map, ent->op, ent);
442 ent = get_op(factory, cls, node_kind_perm);
443 ent->op = new_ir_op(get_next_ir_opcode(), "Perm", op_pin_state_pinned, 0,
445 sizeof(be_node_attr_t) + sizeof(arch_register_t) * cls->n_regs, &be_node_ops);
446 ent->n_pos = 2 * cls->n_regs;
447 ent->pos = obstack_alloc(&factory->obst, sizeof(ent->pos[0]) * ent->n_pos);
448 for(j = 0; j < ent->n_pos; j += 2) {
451 ent->pos[j + 1] = -(k + 1);
453 pmap_insert(factory->irn_op_map, ent->op, ent);
460 static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason)
462 be_node_attr_t *attr = (be_node_attr_t *) &irn->attr;
463 be_op_t *bo = pmap_get(attr->factory->irn_op_map, get_irn_op(irn));
468 case dump_node_opcode_txt:
469 fprintf(f, get_op_name(bo->op));
471 case dump_node_mode_txt:
472 fprintf(f, get_mode_name(get_irn_mode(irn)));
474 case dump_node_nodeattr_txt:
475 fprintf(f, "%s ", bo->cls->name);
477 case dump_node_info_txt:
478 for(i = 0; i < attr->n_regs; ++i) {
479 const arch_register_t *reg = attr->reg[i];
480 fprintf(f, "reg #%d: %s\n", i, reg ? reg->name : "n/a");
483 if(bo->kind == node_kind_spill) {
484 be_spill_attr_t *a = (be_spill_attr_t *) attr;
485 ir_fprintf(f, "spill context: %+F\n", a->spill_ctx);
493 ir_node *insert_Perm_after(const be_main_env_t *env,
494 const arch_register_class_t *cls,
495 dom_front_info_t *dom_front,
498 const arch_env_t *arch_env = env->arch_env;
499 ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
500 ir_graph *irg = get_irn_irg(bl);
501 pset *live = pset_new_ptr_default();
502 firm_dbg_module_t *dbg = firm_dbg_register("firm.be.node");
505 ir_node *curr, *irn, *perm, **nodes;
508 firm_dbg_set_mask(dbg, DBG_LEVEL);
509 DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
512 live_foreach(bl, li) {
513 ir_node *irn = (ir_node *) li->irn;
514 if(live_is_end(li) && arch_irn_has_reg_class(arch_env, irn, -1, cls))
515 pset_insert_ptr(live, irn);
518 sched_foreach_reverse(bl, irn) {
522 * If we encounter the node we want to insert the Perm after,
523 * exit immediately, so that this node is still live
528 DBG((dbg, LEVEL_1, "%+F\n", irn));
529 for(x = pset_first(live); x; x = pset_next(live))
530 DBG((dbg, LEVEL_1, "\tlive: %+F\n", x));
532 if(arch_irn_has_reg_class(arch_env, irn, -1, cls))
533 pset_remove_ptr(live, irn);
535 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
536 ir_node *op = get_irn_n(irn, i);
538 if(arch_irn_has_reg_class(arch_env, op, -1, cls))
539 pset_insert_ptr(live, op);
543 n = pset_count(live);
544 nodes = malloc(n * sizeof(nodes[0]));
546 DBG((dbg, LEVEL_1, "live:\n"));
547 for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++) {
548 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
552 perm = new_Perm(env->node_factory, cls, irg, bl, n, nodes);
553 sched_add_after(pos, perm);
557 for(i = 0; i < n; ++i) {
559 ir_node *perm_op = get_irn_n(perm, i);
560 const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op);
562 ir_mode *mode = get_irn_mode(perm_op);
563 ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
564 arch_set_irn_register(arch_env, proj, reg);
566 sched_add_after(curr, proj);
570 be_introduce_copies(dom_front, perm_op, array_size(copies), copies);