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[] = {
66 #define ARRSIZE(x) (sizeof(x) / sizeof(x[0]))
68 static int cmp_op_map(const void *a, const void *b, size_t size)
73 return !(x->kind == y->kind && x->cls == y->cls);
76 static be_op_t *get_op(const be_node_factory_t *fact,
77 const arch_register_class_t *cls, node_kind_t kind)
84 return set_insert(fact->ops, &templ, sizeof(templ),
85 HASH_PTR(cls) + 7 * kind);
88 ir_node *new_Spill(const be_node_factory_t *factory,
89 const arch_register_class_t *cls,
90 ir_graph *irg, ir_node *bl, ir_node *node_to_spill)
93 ir_op *op = get_op(factory, cls, node_kind_spill)->op;
95 assert(op && "Spill opcode must be present for this register class");
96 in[0] = node_to_spill;
98 return new_ir_node(NULL, irg, bl, op, mode_M, 1, in);
101 ir_node *new_Reload(const be_node_factory_t *factory,
102 const arch_register_class_t *cls, ir_graph *irg,
103 ir_node *bl, ir_mode *mode, ir_node *spill_node)
106 ir_op *op = get_op(factory, cls, node_kind_reload)->op;
108 assert(op && "Reload opcode must be present for this register class");
109 // assert(is_Spill(factory, spill_node) && "Operand of Reload must be a Spill");
112 return new_ir_node(NULL, irg, bl, op, mode, 1, in);
115 ir_node *new_Perm(const be_node_factory_t *factory,
116 const arch_register_class_t *cls,
117 ir_graph *irg, ir_node *bl, int arity, ir_node **in)
119 ir_op *op = get_op(factory, cls, node_kind_perm)->op;
121 return new_ir_node(NULL, irg, bl, op, mode_T, arity, in);
124 ir_node *new_Copy(const be_node_factory_t *factory,
125 const arch_register_class_t *cls,
126 ir_graph *irg, ir_node *bl, ir_node *in)
129 ir_op *op = get_op(factory, cls, node_kind_copy)->op;
133 return new_ir_node(NULL, irg, bl, op, get_irn_mode(in), 1, ins);
137 * If the node is a proj, reset the node to the proj's target and return
139 * @param node The address of a node pointer.
140 * @param def A default value.
141 * @return If *node is a Proj, *node is set to the Proj's target and
142 * the Proj number is returned. Else *node remains the same and @p def
145 static int redir_proj(const ir_node **node, int def)
147 const ir_node *n = *node;
150 *node = get_Proj_pred(n);
151 def = get_Proj_proj(n);
157 static const arch_register_req_t *
158 be_node_get_irn_reg_req(const arch_irn_ops_t *_self,
159 arch_register_req_t *req, const ir_node *irn, int pos)
162 const be_node_factory_t *factory =
163 container_of(_self, const be_node_factory_t, irn_ops);
165 pos = arch_pos_make_out(redir_proj(&irn, arch_pos_get_index(pos)));
167 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
172 req->type = arch_register_req_type_normal;
175 for(i = 0; i < bo->n_pos; ++pos)
176 if(pos == bo->pos[i])
184 be_node_get_n_operands(const arch_irn_ops_t *_self, const ir_node *irn, int in_out)
188 const be_node_factory_t *factory =
189 container_of(_self, const be_node_factory_t, irn_ops);
192 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
194 for(i = 0; i < bo->n_pos; ++i)
195 res += (bo->pos[i] ^ in_out) > 0;
201 be_node_set_irn_reg(const arch_irn_ops_t *_self, ir_node *irn,
202 int idx, const arch_register_t *reg)
204 const arch_register_t **regs;
206 idx = redir_proj((const ir_node **) &irn, idx);
207 regs = (const arch_register_t **) &irn->attr;
211 const arch_register_t *
212 be_node_get_irn_reg(const arch_irn_ops_t *_self, const ir_node *irn, int idx)
214 const arch_register_t **regs;
216 idx = redir_proj(&irn, idx);
217 regs = (const arch_register_t **) &irn->attr;
221 arch_irn_class_t be_node_classify(const arch_irn_ops_t *_self, const ir_node *irn)
223 const be_node_factory_t *factory =
224 container_of(_self, const be_node_factory_t, irn_ops);
229 idx = redir_proj(&irn, 0);
230 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
235 static const arch_irn_ops_t *
236 be_node_get_irn_ops(const arch_irn_handler_t *_self, const ir_node *irn)
239 const be_node_factory_t *factory =
240 container_of(_self, const be_node_factory_t, handler);
243 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
245 return bo ? &factory->irn_ops : NULL;
248 const arch_irn_handler_t *be_node_get_irn_handler(const be_node_factory_t *f)
253 int is_Spill(const be_node_factory_t *f, const ir_node *irn)
256 bo = pmap_get(f->irn_op_map, get_irn_op(irn));
257 return bo->kind == node_kind_spill;
260 be_node_factory_t *be_node_factory_init(be_node_factory_t *factory,
261 const arch_isa_if_t *isa)
266 factory->ops = new_set(cmp_op_map, 64);
267 factory->irn_op_map = pmap_create();
268 obstack_init(&factory->obst);
270 factory->handler.get_irn_ops = be_node_get_irn_ops;
272 factory->irn_ops.get_irn_reg_req = be_node_get_irn_reg_req;
273 factory->irn_ops.get_n_operands = be_node_get_n_operands;
274 factory->irn_ops.set_irn_reg = be_node_set_irn_reg;
275 factory->irn_ops.get_irn_reg = be_node_get_irn_reg;
276 factory->irn_ops.classify = be_node_classify;
278 for(i = 0, n = isa->get_n_reg_class(); i < n; ++i) {
279 const arch_register_class_t *cls = isa->get_reg_class(i);
282 ent = get_op(factory, cls, node_kind_spill);
283 snprintf(buf, sizeof(buf), "Spill_%s", cls->name);
284 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned,
285 0, 0, oparity_unary, 0);
286 ent->n_pos = ARRSIZE(templ_pos_Spill);
287 ent->pos = templ_pos_Spill;
288 pmap_insert(factory->irn_op_map, ent->op, ent);
290 ent = get_op(factory, cls, node_kind_reload);
291 snprintf(buf, sizeof(buf), "Reload_%s", cls->name);
292 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
293 oparity_unary, sizeof(const arch_register_t *));
294 ent->n_pos = ARRSIZE(templ_pos_Reload);
295 ent->pos = templ_pos_Reload;
296 pmap_insert(factory->irn_op_map, ent->op, ent);
298 ent = get_op(factory, cls, node_kind_copy);
299 snprintf(buf, sizeof(buf), "Copy_%s", cls->name);
300 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
301 oparity_unary, sizeof(const arch_register_t *));
302 ent->n_pos = ARRSIZE(templ_pos_Copy);
303 ent->pos = templ_pos_Copy;
304 pmap_insert(factory->irn_op_map, ent->op, ent);
306 ent = get_op(factory, cls, node_kind_perm);
307 snprintf(buf, sizeof(buf), "Perm_%s", cls->name);
308 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
309 oparity_variable, sizeof(const arch_register_t) * cls->n_regs);
310 ent->n_pos = 2 * cls->n_regs;
311 ent->pos = obstack_alloc(&factory->obst, sizeof(ent->pos[0]) * ent->n_pos);
312 for(j = 0; j < ent->n_pos; j += 2) {
313 ent->pos[j] = arch_pos_make_in(j);
314 ent->pos[j + 1] = arch_pos_make_out(j);
316 pmap_insert(factory->irn_op_map, ent->op, ent);
323 ir_node *insert_Perm_after(const be_main_session_env_t *env,
324 const arch_register_class_t *cls, ir_node *pos)
326 const arch_env_t *arch_env = env->main_env->arch_env;
327 ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
328 ir_graph *irg = get_irn_irg(bl);
329 pset *live = put_live_end(bl, pset_new_ptr_default());
330 ir_node *curr, *irn, *perm, **nodes;
331 firm_dbg_module_t *dbg = firm_dbg_register("firm.be.node");
334 firm_dbg_set_mask(dbg, DBG_LEVEL);
335 DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
337 sched_foreach_reverse(bl, irn) {
341 * If we encounter the node we want to insert the Perm after,
342 * exit immediately, so that this node is still live
347 DBG((dbg, LEVEL_1, "%+F\n", irn));
348 for(x = pset_first(live); x; x = pset_next(live))
349 DBG((dbg, LEVEL_1, "\tlive: %+F\n", x));
351 if(arch_irn_has_reg_class(arch_env, irn, arch_pos_make_out(0), cls))
352 pset_remove_ptr(live, irn);
354 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
355 ir_node *op = get_irn_n(irn, i);
357 if(arch_irn_has_reg_class(arch_env, op, arch_pos_make_out(0), cls))
358 pset_insert_ptr(live, op);
362 n = pset_count(live);
363 nodes = malloc(n * sizeof(nodes[0]));
365 DBG((dbg, LEVEL_1, "live:\n"));
366 for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++) {
367 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
371 perm = new_Perm(env->main_env->node_factory, cls, irg, bl, n, nodes);
372 sched_add_after(pos, perm);
376 for(i = 0; i < n; ++i) {
378 ir_node *perm_op = get_irn_n(perm, i);
379 const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op, 0);
381 ir_mode *mode = get_irn_mode(perm_op);
382 ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
383 arch_set_irn_register(arch_env, proj, 0, reg);
385 sched_add_after(curr, proj);
389 be_introduce_copies(env->dom_front, perm_op, array_size(copies), copies);