4 * @author Sebastian Hack
6 * Backend node support.
8 * Copyright (C) 2005 Universitaet Karlsruhe
9 * Released under the GPL
30 #include "besched_t.h"
37 typedef enum _node_kind_t {
47 const arch_register_class_t *cls;
53 static int templ_pos_Spill[] = {
57 static int templ_pos_Reload[] = {
61 static int templ_pos_Copy[] = {
65 #define ARRSIZE(x) (sizeof(x) / sizeof(x[0]))
67 static int cmp_op_map(const void *a, const void *b, size_t size)
72 return !(x->kind == y->kind && x->cls == y->cls);
75 static be_op_t *get_op(const be_node_factory_t *fact,
76 const arch_register_class_t *cls, node_kind_t kind)
83 return set_insert(fact->ops, &templ, sizeof(templ),
84 HASH_PTR(cls) + 7 * kind);
87 ir_node *new_Spill(const be_node_factory_t *factory,
88 const arch_register_class_t *cls,
89 ir_graph *irg, ir_node *bl, ir_node *node_to_spill)
92 ir_op *op = get_op(factory, cls, node_kind_spill)->op;
94 assert(op && "Spill opcode must be present for this register class");
95 in[0] = node_to_spill;
97 return new_ir_node(NULL, irg, bl, op, mode_M, 1, in);
100 ir_node *new_Reload(const be_node_factory_t *factory,
101 const arch_register_class_t *cls, ir_graph *irg,
102 ir_node *bl, ir_mode *mode, ir_node *spill_node)
105 ir_op *op = get_op(factory, cls, node_kind_reload)->op;
107 assert(op && "Reload opcode must be present for this register class");
108 // assert(is_Spill(factory, spill_node) && "Operand of Reload must be a Spill");
111 return new_ir_node(NULL, irg, bl, op, mode, 1, in);
114 ir_node *new_Perm(const be_node_factory_t *factory,
115 const arch_register_class_t *cls,
116 ir_graph *irg, ir_node *bl, int arity, ir_node **in)
118 ir_op *op = get_op(factory, cls, node_kind_perm)->op;
120 return new_ir_node(NULL, irg, bl, op, mode_T, arity, in);
123 ir_node *new_Copy(const be_node_factory_t *factory,
124 const arch_register_class_t *cls,
125 ir_graph *irg, ir_node *bl, ir_node *in)
128 ir_op *op = get_op(factory, cls, node_kind_copy)->op;
132 return new_ir_node(NULL, irg, bl, op, get_irn_mode(in), 1, ins);
136 const be_node_factory_t *factory,
137 const arch_env_t *arch_env,
140 const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, irn, -1);
141 ir_node *bl = get_nodes_block(irn);
142 ir_graph *irg = get_irn_irg(bl);
143 ir_node *spill = new_Spill(factory, cls, irg, bl, irn);
148 * search the right insertion point. a spill of a phi cannot be put
149 * directly after the phi, if there are some phis behind the one which
152 insert = sched_next(irn);
153 while(is_Phi(insert) && !sched_is_end(insert))
154 insert = sched_next(insert);
156 sched_add_before(insert, spill);
160 ir_node *be_reload(const be_node_factory_t *factory,
161 const arch_env_t *arch_env,
162 const arch_register_class_t *cls,
163 ir_node *irn, int pos, ir_mode *mode, ir_node *spill)
167 ir_node *bl = get_nodes_block(irn);
168 ir_graph *irg = get_irn_irg(bl);
170 assert(is_Spill(factory, spill)
171 || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
173 reload = new_Reload(factory, cls, irg, bl, mode, spill);
175 set_irn_n(irn, pos, reload);
176 sched_add_before(irn, reload);
181 * If the node is a proj, reset the node to the proj's target and return
183 * @param node The address of a node pointer.
184 * @param def A default value.
185 * @return If *node is a Proj, *node is set to the Proj's target and
186 * the Proj number is returned. Else *node remains the same and @p def
189 static int redir_proj(const ir_node **node, int def)
191 const ir_node *n = *node;
194 *node = get_Proj_pred(n);
195 def = -(get_Proj_proj(n) + 1);
201 static const arch_register_req_t *
202 be_node_get_irn_reg_req(const arch_irn_ops_t *_self,
203 arch_register_req_t *req, const ir_node *irn, int pos)
206 const be_node_factory_t *factory =
207 container_of(_self, const be_node_factory_t, irn_ops);
210 * were interested in an output operand, so
211 * let's resolve projs.
214 pos = redir_proj((const ir_node **) &irn, pos);
216 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
221 req->type = arch_register_req_type_normal;
224 for(i = 0; i < bo->n_pos; ++i)
225 if(pos == bo->pos[i])
233 be_node_set_irn_reg(const arch_irn_ops_t *_self, ir_node *irn,
234 const arch_register_t *reg)
237 const arch_register_t **regs;
239 const be_node_factory_t *factory =
240 container_of(_self, const be_node_factory_t, irn_ops);
242 pos = redir_proj((const ir_node **) &irn, -1);
243 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
248 regs = (const arch_register_t **) &irn->attr;
249 regs[-pos - 1] = reg;
252 const arch_register_t *
253 be_node_get_irn_reg(const arch_irn_ops_t *_self, const ir_node *irn)
256 const arch_register_t **regs;
258 const be_node_factory_t *factory =
259 container_of(_self, const be_node_factory_t, irn_ops);
261 pos = redir_proj((const ir_node **) &irn, -1);
262 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
267 for(i = 0; i < bo->n_pos; ++i) {
268 if(bo->pos[i] == pos) {
269 regs = (const arch_register_t **) &irn->attr;
270 return regs[-pos - 1];
277 arch_irn_class_t be_node_classify(const arch_irn_ops_t *_self, const ir_node *irn)
279 const be_node_factory_t *factory =
280 container_of(_self, const be_node_factory_t, irn_ops);
285 idx = redir_proj(&irn, 0);
286 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
289 #define XXX(a) case node_kind_ ## a: return arch_irn_class_ ## a;
302 arch_irn_class_t be_node_get_flags(const arch_irn_ops_t *_self, const ir_node *irn)
307 static const arch_irn_ops_t *
308 be_node_get_irn_ops(const arch_irn_handler_t *_self, const ir_node *irn)
311 const be_node_factory_t *factory =
312 container_of(_self, const be_node_factory_t, handler);
315 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
317 return bo ? &factory->irn_ops : NULL;
320 const arch_irn_handler_t *be_node_get_irn_handler(const be_node_factory_t *f)
325 int is_Spill(const be_node_factory_t *f, const ir_node *irn)
328 bo = pmap_get(f->irn_op_map, get_irn_op(irn));
329 return bo != NULL && bo->kind == node_kind_spill;
332 be_node_factory_t *be_node_factory_init(be_node_factory_t *factory, const arch_isa_t *isa)
337 factory->ops = new_set(cmp_op_map, 64);
338 factory->irn_op_map = pmap_create();
339 obstack_init(&factory->obst);
341 factory->handler.get_irn_ops = be_node_get_irn_ops;
343 factory->irn_ops.get_irn_reg_req = be_node_get_irn_reg_req;
344 factory->irn_ops.set_irn_reg = be_node_set_irn_reg;
345 factory->irn_ops.get_irn_reg = be_node_get_irn_reg;
346 factory->irn_ops.classify = be_node_classify;
347 factory->irn_ops.get_flags = be_node_get_flags;
349 for(i = 0, n = arch_isa_get_n_reg_class(isa); i < n; ++i) {
350 const arch_register_class_t *cls = arch_isa_get_reg_class(isa, i);
353 ent = get_op(factory, cls, node_kind_spill);
354 snprintf(buf, sizeof(buf), "Spill_%s", cls->name);
355 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned,
356 0, oparity_unary, 0, 0, NULL);
357 ent->n_pos = ARRSIZE(templ_pos_Spill);
358 ent->pos = templ_pos_Spill;
359 pmap_insert(factory->irn_op_map, ent->op, ent);
361 ent = get_op(factory, cls, node_kind_reload);
362 snprintf(buf, sizeof(buf), "Reload_%s", cls->name);
363 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0,
364 oparity_unary, 0, sizeof(const arch_register_t *), NULL);
365 ent->n_pos = ARRSIZE(templ_pos_Reload);
366 ent->pos = templ_pos_Reload;
367 pmap_insert(factory->irn_op_map, ent->op, ent);
369 ent = get_op(factory, cls, node_kind_copy);
370 snprintf(buf, sizeof(buf), "Copy_%s", cls->name);
371 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0,
372 oparity_unary, 0, sizeof(const arch_register_t *), NULL);
373 ent->n_pos = ARRSIZE(templ_pos_Copy);
374 ent->pos = templ_pos_Copy;
375 pmap_insert(factory->irn_op_map, ent->op, ent);
377 ent = get_op(factory, cls, node_kind_perm);
378 snprintf(buf, sizeof(buf), "Perm_%s", cls->name);
379 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0,
380 oparity_variable, 0, sizeof(const arch_register_t) * cls->n_regs, NULL);
381 ent->n_pos = 2 * cls->n_regs;
382 ent->pos = obstack_alloc(&factory->obst, sizeof(ent->pos[0]) * ent->n_pos);
383 for(j = 0; j < ent->n_pos; j += 2) {
385 ent->pos[j + 1] = -(j + 1);
387 pmap_insert(factory->irn_op_map, ent->op, ent);
394 ir_node *insert_Perm_after(const be_main_env_t *env,
395 const arch_register_class_t *cls,
396 dom_front_info_t *dom_front,
399 const arch_env_t *arch_env = env->arch_env;
400 ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
401 ir_graph *irg = get_irn_irg(bl);
402 pset *live = put_live_end(bl, pset_new_ptr_default());
403 firm_dbg_module_t *dbg = firm_dbg_register("firm.be.node");
405 ir_node *curr, *irn, *perm, **nodes;
408 firm_dbg_set_mask(dbg, DBG_LEVEL);
409 DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
411 sched_foreach_reverse(bl, irn) {
415 * If we encounter the node we want to insert the Perm after,
416 * exit immediately, so that this node is still live
421 DBG((dbg, LEVEL_1, "%+F\n", irn));
422 for(x = pset_first(live); x; x = pset_next(live))
423 DBG((dbg, LEVEL_1, "\tlive: %+F\n", x));
425 if(arch_irn_has_reg_class(arch_env, irn, -1, cls))
426 pset_remove_ptr(live, irn);
428 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
429 ir_node *op = get_irn_n(irn, i);
431 if(arch_irn_has_reg_class(arch_env, op, -1, cls))
432 pset_insert_ptr(live, op);
436 n = pset_count(live);
437 nodes = malloc(n * sizeof(nodes[0]));
439 DBG((dbg, LEVEL_1, "live:\n"));
440 for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++) {
441 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
445 perm = new_Perm(env->node_factory, cls, irg, bl, n, nodes);
446 sched_add_after(pos, perm);
450 for(i = 0; i < n; ++i) {
452 ir_node *perm_op = get_irn_n(perm, i);
453 const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op);
455 ir_mode *mode = get_irn_mode(perm_op);
456 ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
457 arch_set_irn_register(arch_env, proj, reg);
459 sched_add_after(curr, proj);
463 be_introduce_copies(dom_front, perm_op, array_size(copies), copies);