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 const be_node_factory_t *factory,
138 const arch_env_t *arch_env,
141 const arch_register_class_t *cls
142 = arch_get_irn_reg_class(arch_env, irn, arch_pos_make_out(0));
143 ir_node *bl = get_nodes_block(irn);
144 ir_graph *irg = get_irn_irg(bl);
145 ir_node *spill = new_Spill(factory, cls, irg, bl, irn);
150 * search the right insertion point. a spill of a phi cannot be put
151 * directly after the phi, if there are some phis behind the one which
154 insert = sched_next(irn);
155 while(is_Phi(insert) && !sched_is_end(insert))
156 insert = sched_next(insert);
158 sched_add_before(insert, spill);
163 const be_node_factory_t *factory,
164 const arch_env_t *arch_env,
165 const arch_register_class_t *cls,
166 ir_node *irn, int pos, ir_mode *mode, ir_node *spill)
170 ir_node *bl = get_nodes_block(irn);
171 ir_graph *irg = get_irn_irg(bl);
173 assert(is_Spill(factory, spill)
174 || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
176 reload = new_Reload(factory, cls, irg, bl, mode, spill);
178 set_irn_n(irn, pos, reload);
179 sched_add_before(irn, reload);
184 * If the node is a proj, reset the node to the proj's target and return
186 * @param node The address of a node pointer.
187 * @param def A default value.
188 * @return If *node is a Proj, *node is set to the Proj's target and
189 * the Proj number is returned. Else *node remains the same and @p def
192 static int redir_proj(const ir_node **node, int def)
194 const ir_node *n = *node;
197 *node = get_Proj_pred(n);
198 def = get_Proj_proj(n);
204 static const arch_register_req_t *
205 be_node_get_irn_reg_req(const arch_irn_ops_t *_self,
206 arch_register_req_t *req, const ir_node *irn, int pos)
209 int index = arch_pos_get_index(pos);
210 const be_node_factory_t *factory =
211 container_of(_self, const be_node_factory_t, irn_ops);
213 if(arch_pos_is_out(pos))
214 pos = arch_pos_make_out(redir_proj(&irn, index));
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_get_n_operands(const arch_irn_ops_t *_self, const ir_node *irn, int in_out)
237 const be_node_factory_t *factory =
238 container_of(_self, const be_node_factory_t, irn_ops);
241 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
243 for(i = 0; i < bo->n_pos; ++i)
244 res += (bo->pos[i] ^ in_out) > 0;
250 be_node_set_irn_reg(const arch_irn_ops_t *_self, ir_node *irn,
251 int idx, const arch_register_t *reg)
253 const arch_register_t **regs;
255 idx = redir_proj((const ir_node **) &irn, idx);
256 regs = (const arch_register_t **) &irn->attr;
260 const arch_register_t *
261 be_node_get_irn_reg(const arch_irn_ops_t *_self, const ir_node *irn, int idx)
263 const arch_register_t **regs;
265 idx = redir_proj(&irn, idx);
266 regs = (const arch_register_t **) &irn->attr;
270 arch_irn_class_t be_node_classify(const arch_irn_ops_t *_self, const ir_node *irn)
272 const be_node_factory_t *factory =
273 container_of(_self, const be_node_factory_t, irn_ops);
278 idx = redir_proj(&irn, 0);
279 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
282 #define XXX(a) case node_kind_ ## a: return arch_irn_class_ ## a;
293 arch_irn_class_t be_node_get_flags(const arch_irn_ops_t *_self, const ir_node *irn)
298 static const arch_irn_ops_t *
299 be_node_get_irn_ops(const arch_irn_handler_t *_self, const ir_node *irn)
302 const be_node_factory_t *factory =
303 container_of(_self, const be_node_factory_t, handler);
306 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
308 return bo ? &factory->irn_ops : NULL;
311 const arch_irn_handler_t *be_node_get_irn_handler(const be_node_factory_t *f)
316 int is_Spill(const be_node_factory_t *f, const ir_node *irn)
319 bo = pmap_get(f->irn_op_map, get_irn_op(irn));
320 return bo != NULL && bo->kind == node_kind_spill;
323 be_node_factory_t *be_node_factory_init(be_node_factory_t *factory,
324 const arch_isa_if_t *isa)
329 factory->ops = new_set(cmp_op_map, 64);
330 factory->irn_op_map = pmap_create();
331 obstack_init(&factory->obst);
333 factory->handler.get_irn_ops = be_node_get_irn_ops;
335 factory->irn_ops.get_irn_reg_req = be_node_get_irn_reg_req;
336 factory->irn_ops.get_n_operands = be_node_get_n_operands;
337 factory->irn_ops.set_irn_reg = be_node_set_irn_reg;
338 factory->irn_ops.get_irn_reg = be_node_get_irn_reg;
339 factory->irn_ops.classify = be_node_classify;
340 factory->irn_ops.get_flags = be_node_get_flags;
342 for(i = 0, n = isa->get_n_reg_class(); i < n; ++i) {
343 const arch_register_class_t *cls = isa->get_reg_class(i);
346 ent = get_op(factory, cls, node_kind_spill);
347 snprintf(buf, sizeof(buf), "Spill_%s", cls->name);
348 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned,
349 0, oparity_unary, 0, 0);
350 ent->n_pos = ARRSIZE(templ_pos_Spill);
351 ent->pos = templ_pos_Spill;
352 pmap_insert(factory->irn_op_map, ent->op, ent);
354 ent = get_op(factory, cls, node_kind_reload);
355 snprintf(buf, sizeof(buf), "Reload_%s", cls->name);
356 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0,
357 oparity_unary, 0, sizeof(const arch_register_t *));
358 ent->n_pos = ARRSIZE(templ_pos_Reload);
359 ent->pos = templ_pos_Reload;
360 pmap_insert(factory->irn_op_map, ent->op, ent);
362 ent = get_op(factory, cls, node_kind_copy);
363 snprintf(buf, sizeof(buf), "Copy_%s", cls->name);
364 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0,
365 oparity_unary, 0, sizeof(const arch_register_t *));
366 ent->n_pos = ARRSIZE(templ_pos_Copy);
367 ent->pos = templ_pos_Copy;
368 pmap_insert(factory->irn_op_map, ent->op, ent);
370 ent = get_op(factory, cls, node_kind_perm);
371 snprintf(buf, sizeof(buf), "Perm_%s", cls->name);
372 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0,
373 oparity_variable, 0, sizeof(const arch_register_t) * cls->n_regs);
374 ent->n_pos = 2 * cls->n_regs;
375 ent->pos = obstack_alloc(&factory->obst, sizeof(ent->pos[0]) * ent->n_pos);
376 for(j = 0; j < ent->n_pos; j += 2) {
377 ent->pos[j] = arch_pos_make_in(j);
378 ent->pos[j + 1] = arch_pos_make_out(j);
380 pmap_insert(factory->irn_op_map, ent->op, ent);
387 ir_node *insert_Perm_after(const be_main_session_env_t *env,
388 const arch_register_class_t *cls, ir_node *pos)
390 const arch_env_t *arch_env = env->main_env->arch_env;
391 ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
392 ir_graph *irg = get_irn_irg(bl);
393 pset *live = put_live_end(bl, pset_new_ptr_default());
394 ir_node *curr, *irn, *perm, **nodes;
395 firm_dbg_module_t *dbg = firm_dbg_register("firm.be.node");
398 firm_dbg_set_mask(dbg, DBG_LEVEL);
399 DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
401 sched_foreach_reverse(bl, irn) {
405 * If we encounter the node we want to insert the Perm after,
406 * exit immediately, so that this node is still live
411 DBG((dbg, LEVEL_1, "%+F\n", irn));
412 for(x = pset_first(live); x; x = pset_next(live))
413 DBG((dbg, LEVEL_1, "\tlive: %+F\n", x));
415 if(arch_irn_has_reg_class(arch_env, irn, arch_pos_make_out(0), cls))
416 pset_remove_ptr(live, irn);
418 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
419 ir_node *op = get_irn_n(irn, i);
421 if(arch_irn_has_reg_class(arch_env, op, arch_pos_make_out(0), cls))
422 pset_insert_ptr(live, op);
426 n = pset_count(live);
427 nodes = malloc(n * sizeof(nodes[0]));
429 DBG((dbg, LEVEL_1, "live:\n"));
430 for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++) {
431 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
435 perm = new_Perm(env->main_env->node_factory, cls, irg, bl, n, nodes);
436 sched_add_after(pos, perm);
440 for(i = 0; i < n; ++i) {
442 ir_node *perm_op = get_irn_n(perm, i);
443 const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op, 0);
445 ir_mode *mode = get_irn_mode(perm_op);
446 ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
447 arch_set_irn_register(arch_env, proj, 0, reg);
449 sched_add_after(curr, proj);
453 be_introduce_copies(env->dom_front, perm_op, array_size(copies), copies);