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 const be_node_factory_t *factory =
256 container_of(_self, const be_node_factory_t, irn_ops);
258 idx = redir_proj((const ir_node **) &irn, idx);
259 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
264 regs = (const arch_register_t **) &irn->attr;
268 const arch_register_t *
269 be_node_get_irn_reg(const arch_irn_ops_t *_self, const ir_node *irn, int idx)
271 const arch_register_t **regs;
273 int i, pos = arch_pos_make_out(idx);
274 const be_node_factory_t *factory =
275 container_of(_self, const be_node_factory_t, irn_ops);
277 idx = redir_proj(&irn, idx);
278 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
283 for(i = 0; i < bo->n_pos; ++i) {
284 if(bo->pos[i] == pos) {
285 regs = (const arch_register_t **) &irn->attr;
293 arch_irn_class_t be_node_classify(const arch_irn_ops_t *_self, const ir_node *irn)
295 const be_node_factory_t *factory =
296 container_of(_self, const be_node_factory_t, irn_ops);
301 idx = redir_proj(&irn, 0);
302 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
305 #define XXX(a) case node_kind_ ## a: return arch_irn_class_ ## a;
316 arch_irn_class_t be_node_get_flags(const arch_irn_ops_t *_self, const ir_node *irn)
321 static const arch_irn_ops_t *
322 be_node_get_irn_ops(const arch_irn_handler_t *_self, const ir_node *irn)
325 const be_node_factory_t *factory =
326 container_of(_self, const be_node_factory_t, handler);
329 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
331 return bo ? &factory->irn_ops : NULL;
334 const arch_irn_handler_t *be_node_get_irn_handler(const be_node_factory_t *f)
339 int is_Spill(const be_node_factory_t *f, const ir_node *irn)
342 bo = pmap_get(f->irn_op_map, get_irn_op(irn));
343 return bo != NULL && bo->kind == node_kind_spill;
346 be_node_factory_t *be_node_factory_init(be_node_factory_t *factory,
347 const arch_isa_if_t *isa)
352 factory->ops = new_set(cmp_op_map, 64);
353 factory->irn_op_map = pmap_create();
354 obstack_init(&factory->obst);
356 factory->handler.get_irn_ops = be_node_get_irn_ops;
358 factory->irn_ops.get_irn_reg_req = be_node_get_irn_reg_req;
359 factory->irn_ops.get_n_operands = be_node_get_n_operands;
360 factory->irn_ops.set_irn_reg = be_node_set_irn_reg;
361 factory->irn_ops.get_irn_reg = be_node_get_irn_reg;
362 factory->irn_ops.classify = be_node_classify;
363 factory->irn_ops.get_flags = be_node_get_flags;
365 for(i = 0, n = isa->get_n_reg_class(); i < n; ++i) {
366 const arch_register_class_t *cls = isa->get_reg_class(i);
369 ent = get_op(factory, cls, node_kind_spill);
370 snprintf(buf, sizeof(buf), "Spill_%s", cls->name);
371 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned,
372 0, oparity_unary, 0, 0, NULL);
373 ent->n_pos = ARRSIZE(templ_pos_Spill);
374 ent->pos = templ_pos_Spill;
375 pmap_insert(factory->irn_op_map, ent->op, ent);
377 ent = get_op(factory, cls, node_kind_reload);
378 snprintf(buf, sizeof(buf), "Reload_%s", cls->name);
379 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0,
380 oparity_unary, 0, sizeof(const arch_register_t *), NULL);
381 ent->n_pos = ARRSIZE(templ_pos_Reload);
382 ent->pos = templ_pos_Reload;
383 pmap_insert(factory->irn_op_map, ent->op, ent);
385 ent = get_op(factory, cls, node_kind_copy);
386 snprintf(buf, sizeof(buf), "Copy_%s", cls->name);
387 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0,
388 oparity_unary, 0, sizeof(const arch_register_t *), NULL);
389 ent->n_pos = ARRSIZE(templ_pos_Copy);
390 ent->pos = templ_pos_Copy;
391 pmap_insert(factory->irn_op_map, ent->op, ent);
393 ent = get_op(factory, cls, node_kind_perm);
394 snprintf(buf, sizeof(buf), "Perm_%s", cls->name);
395 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0,
396 oparity_variable, 0, sizeof(const arch_register_t) * cls->n_regs, NULL);
397 ent->n_pos = 2 * cls->n_regs;
398 ent->pos = obstack_alloc(&factory->obst, sizeof(ent->pos[0]) * ent->n_pos);
399 for(j = 0; j < ent->n_pos; j += 2) {
400 ent->pos[j] = arch_pos_make_in(j);
401 ent->pos[j + 1] = arch_pos_make_out(j);
403 pmap_insert(factory->irn_op_map, ent->op, ent);
410 ir_node *insert_Perm_after(const be_main_session_env_t *env,
411 const arch_register_class_t *cls, ir_node *pos)
413 const arch_env_t *arch_env = env->main_env->arch_env;
414 ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
415 ir_graph *irg = get_irn_irg(bl);
416 pset *live = put_live_end(bl, pset_new_ptr_default());
417 ir_node *curr, *irn, *perm, **nodes;
418 firm_dbg_module_t *dbg = firm_dbg_register("firm.be.node");
421 firm_dbg_set_mask(dbg, DBG_LEVEL);
422 DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
424 sched_foreach_reverse(bl, irn) {
428 * If we encounter the node we want to insert the Perm after,
429 * exit immediately, so that this node is still live
434 DBG((dbg, LEVEL_1, "%+F\n", irn));
435 for(x = pset_first(live); x; x = pset_next(live))
436 DBG((dbg, LEVEL_1, "\tlive: %+F\n", x));
438 if(arch_irn_has_reg_class(arch_env, irn, arch_pos_make_out(0), cls))
439 pset_remove_ptr(live, irn);
441 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
442 ir_node *op = get_irn_n(irn, i);
444 if(arch_irn_has_reg_class(arch_env, op, arch_pos_make_out(0), cls))
445 pset_insert_ptr(live, op);
449 n = pset_count(live);
450 nodes = malloc(n * sizeof(nodes[0]));
452 DBG((dbg, LEVEL_1, "live:\n"));
453 for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++) {
454 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
458 perm = new_Perm(env->main_env->node_factory, cls, irg, bl, n, nodes);
459 sched_add_after(pos, perm);
463 for(i = 0; i < n; ++i) {
465 ir_node *perm_op = get_irn_n(perm, i);
466 const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op, 0);
468 ir_mode *mode = get_irn_mode(perm_op);
469 ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
470 arch_set_irn_register(arch_env, proj, 0, reg);
472 sched_add_after(curr, proj);
476 be_introduce_copies(env->dom_front, perm_op, array_size(copies), copies);