4 * @author Sebastian Hack
6 * Backend node support.
8 * Copyright (C) 2005 Universitaet Karlsruhe
9 * Released under the GPL
30 #include "besched_t.h"
35 typedef enum _node_kind_t {
45 const arch_register_class_t *cls;
51 static int templ_pos_Spill[] = {
55 static int templ_pos_Reload[] = {
59 static int templ_pos_Copy[] = {
64 #define ARRSIZE(x) (sizeof(x) / sizeof(x[0]))
66 static int cmp_op_map(const void *a, const void *b, size_t size)
71 return !(x->kind == y->kind && x->cls == y->cls);
74 static be_op_t *get_op(const be_node_factory_t *fact,
75 const arch_register_class_t *cls, node_kind_t kind)
82 return set_insert(fact->ops, &templ, sizeof(templ),
83 HASH_PTR(cls) + 7 * kind);
86 ir_node *new_Spill(const be_node_factory_t *factory,
87 const arch_register_class_t *cls,
88 ir_graph *irg, ir_node *bl, ir_node *node_to_spill)
91 ir_op *op = get_op(factory, cls, node_kind_spill)->op;
93 assert(op && "Spill opcode must be present for this register class");
94 in[0] = node_to_spill;
96 return new_ir_node(NULL, irg, bl, op, mode_M, 1, in);
99 ir_node *new_Reload(const be_node_factory_t *factory,
100 const arch_register_class_t *cls, ir_graph *irg,
101 ir_node *bl, ir_mode *mode, ir_node *spill_node)
104 ir_op *op = get_op(factory, cls, node_kind_reload)->op;
106 assert(op && "Reload opcode must be present for this register class");
107 // assert(is_Spill(factory, spill_node) && "Operand of Reload must be a Spill");
110 return new_ir_node(NULL, irg, bl, op, mode, 1, in);
113 ir_node *new_Perm(const be_node_factory_t *factory,
114 const arch_register_class_t *cls,
115 ir_graph *irg, ir_node *bl, int arity, ir_node **in)
117 ir_op *op = get_op(factory, cls, node_kind_perm)->op;
119 return new_ir_node(NULL, irg, bl, op, mode_T, arity, in);
122 ir_node *new_Copy(const be_node_factory_t *factory,
123 const arch_register_class_t *cls,
124 ir_graph *irg, ir_node *bl, ir_node *in)
127 ir_op *op = get_op(factory, cls, node_kind_perm)->op;
131 return new_ir_node(NULL, irg, bl, op, get_irn_mode(in), 1, ins);
135 * If the node is a proj, reset the node to the proj's target and return
137 * @param node The address of a node pointer.
138 * @param def A default value.
139 * @return If *node is a Proj, *node is set to the Proj's target and
140 * the Proj number is returned. Else *node remains the same and @p def
143 static int redir_proj(const ir_node **node, int def)
145 const ir_node *n = *node;
148 *node = get_Proj_pred(n);
149 def = get_Proj_proj(n);
155 static const arch_register_req_t *
156 be_node_get_irn_reg_req(const arch_irn_ops_t *_self,
157 arch_register_req_t *req, const ir_node *irn, int pos)
160 const be_node_factory_t *factory =
161 container_of(_self, const be_node_factory_t, irn_ops);
163 pos = arch_pos_make_out(redir_proj(&irn, arch_pos_get_index(pos)));
165 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
170 req->type = arch_register_req_type_normal;
173 for(i = 0; i < bo->n_pos; ++pos)
174 if(pos == bo->pos[i])
182 be_node_get_n_operands(const arch_irn_ops_t *_self, const ir_node *irn, int in_out)
186 const be_node_factory_t *factory =
187 container_of(_self, const be_node_factory_t, irn_ops);
190 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
192 for(i = 0; i < bo->n_pos; ++i)
193 res += (bo->pos[i] ^ in_out) > 0;
199 be_node_set_irn_reg(const arch_irn_ops_t *_self, ir_node *irn,
200 int idx, const arch_register_t *reg)
202 const arch_register_t **regs;
204 idx = redir_proj((const ir_node **) &irn, idx);
205 regs = (const arch_register_t **) &irn->attr;
209 const arch_register_t *
210 be_node_get_irn_reg(const arch_irn_ops_t *_self, const ir_node *irn, int idx)
212 const arch_register_t **regs;
214 idx = redir_proj(&irn, idx);
215 regs = (const arch_register_t **) &irn->attr;
219 arch_irn_class_t be_node_classify(const arch_irn_ops_t *_self, const ir_node *irn)
221 const be_node_factory_t *factory =
222 container_of(_self, const be_node_factory_t, irn_ops);
227 idx = redir_proj(&irn, 0);
228 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
233 static const arch_irn_ops_t *
234 be_node_get_irn_ops(const arch_irn_handler_t *_self, const ir_node *irn)
237 const be_node_factory_t *factory =
238 container_of(_self, const be_node_factory_t, handler);
241 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
243 return bo ? &factory->irn_ops : NULL;
246 const arch_irn_handler_t *be_node_get_irn_handler(const be_node_factory_t *f)
251 int is_Spill(const be_node_factory_t *f, const ir_node *irn)
254 bo = pmap_get(f->irn_op_map, get_irn_op(irn));
255 return bo->kind == node_kind_spill;
258 be_node_factory_t *be_node_factory_init(be_node_factory_t *factory,
259 const arch_isa_if_t *isa)
264 factory->ops = new_set(cmp_op_map, 64);
265 factory->irn_op_map = pmap_create();
266 obstack_init(&factory->obst);
268 factory->handler.get_irn_ops = be_node_get_irn_ops;
270 factory->irn_ops.get_irn_reg_req = be_node_get_irn_reg_req;
271 factory->irn_ops.get_n_operands = be_node_get_n_operands;
272 factory->irn_ops.set_irn_reg = be_node_set_irn_reg;
273 factory->irn_ops.get_irn_reg = be_node_get_irn_reg;
274 factory->irn_ops.classify = be_node_classify;
276 for(i = 0, n = isa->get_n_reg_class(); i < n; ++i) {
277 const arch_register_class_t *cls = isa->get_reg_class(i);
280 ent = get_op(factory, cls, node_kind_spill);
281 snprintf(buf, sizeof(buf), "Spill_%s", cls->name);
282 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned,
283 0, 0, oparity_unary, 0);
284 ent->n_pos = ARRSIZE(templ_pos_Spill);
285 ent->pos = templ_pos_Spill;
286 pmap_insert(factory->irn_op_map, ent->op, ent);
288 ent = get_op(factory, cls, node_kind_reload);
289 snprintf(buf, sizeof(buf), "Reload_%s", cls->name);
290 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
291 oparity_unary, sizeof(const arch_register_t *));
292 ent->n_pos = ARRSIZE(templ_pos_Reload);
293 ent->pos = templ_pos_Reload;
294 pmap_insert(factory->irn_op_map, ent->op, ent);
296 ent = get_op(factory, cls, node_kind_copy);
297 snprintf(buf, sizeof(buf), "Copy_%s", cls->name);
298 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
299 oparity_unary, sizeof(const arch_register_t *));
300 ent->n_pos = ARRSIZE(templ_pos_Copy);
301 ent->pos = templ_pos_Copy;
302 pmap_insert(factory->irn_op_map, ent->op, ent);
304 ent = get_op(factory, cls, node_kind_perm);
305 snprintf(buf, sizeof(buf), "Perm_%s", cls->name);
306 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
307 oparity_variable, sizeof(const arch_register_t) * cls->n_regs);
308 ent->n_pos = 2 * cls->n_regs;
309 ent->pos = obstack_alloc(&factory->obst, sizeof(ent->pos[0]) * ent->n_pos);
310 for(j = 0; j < ent->n_pos; j += 2) {
311 ent->pos[j] = arch_pos_make_in(j);
312 ent->pos[j + 1] = arch_pos_make_out(j);
314 pmap_insert(factory->irn_op_map, ent->op, ent);
321 ir_node *insert_Perm_after(const be_main_session_env_t *env,
322 const arch_register_class_t *cls, ir_node *pos)
324 const arch_env_t *arch_env = env->main_env->arch_env;
325 ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
326 ir_graph *irg = get_irn_irg(bl);
327 pset *live = put_live_end(bl, pset_new_ptr_default());
328 ir_node *curr, *irn, *perm, **nodes;
329 firm_dbg_module_t *dbg = firm_dbg_register("firm.be.node");
332 DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
334 sched_foreach_reverse(bl, irn) {
337 DBG((dbg, LEVEL_1, "%+F\n", irn));
338 for(x = pset_first(live); x; x = pset_next(live))
339 DBG((dbg, LEVEL_1, "\tlive: %+F\n", x));
341 if(arch_irn_has_reg_class(arch_env, irn, arch_pos_make_out(0), cls))
342 pset_remove_ptr(live, irn);
345 * Consider the definition of the node, but not the uses, since
346 * newly created liveranges by the node after which the perm is
347 * located are not of interest for the perm.
352 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
353 ir_node *op = get_irn_n(irn, i);
355 if(arch_irn_has_reg_class(arch_env, op, arch_pos_make_out(0), cls))
356 pset_insert_ptr(live, op);
360 n = pset_count(live);
361 nodes = malloc(n * sizeof(nodes[0]));
363 for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++)
366 perm = new_Perm(env->main_env->node_factory, cls, irg, bl, n, nodes);
367 sched_add_after(pos, perm);
371 for(i = 0; i < n; ++i) {
373 ir_node *perm_op = get_irn_n(perm, i);
375 ir_mode *mode = get_irn_mode(perm_op);
376 ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
377 sched_add_after(curr, proj);
381 be_introduce_copies(env->dom_front, perm_op, array_size(copies), copies);