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 ir_node *be_spill(const be_node_factory_t *factory, ir_node *irn)
139 ir_node *bl = get_nodes_block(irn);
140 ir_graph *irg = get_irn_irg(bl);
142 new_Spill(factory, arch_get_irn_reg_class(irn), irg, bl, irn);
143 sched_add_after(irn, spill);
152 * If the node is a proj, reset the node to the proj's target and return
154 * @param node The address of a node pointer.
155 * @param def A default value.
156 * @return If *node is a Proj, *node is set to the Proj's target and
157 * the Proj number is returned. Else *node remains the same and @p def
160 static int redir_proj(const ir_node **node, int def)
162 const ir_node *n = *node;
165 *node = get_Proj_pred(n);
166 def = get_Proj_proj(n);
172 static const arch_register_req_t *
173 be_node_get_irn_reg_req(const arch_irn_ops_t *_self,
174 arch_register_req_t *req, const ir_node *irn, int pos)
177 const be_node_factory_t *factory =
178 container_of(_self, const be_node_factory_t, irn_ops);
180 pos = arch_pos_make_out(redir_proj(&irn, arch_pos_get_index(pos)));
182 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
187 req->type = arch_register_req_type_normal;
190 for(i = 0; i < bo->n_pos; ++pos)
191 if(pos == bo->pos[i])
199 be_node_get_n_operands(const arch_irn_ops_t *_self, const ir_node *irn, int in_out)
203 const be_node_factory_t *factory =
204 container_of(_self, const be_node_factory_t, irn_ops);
207 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
209 for(i = 0; i < bo->n_pos; ++i)
210 res += (bo->pos[i] ^ in_out) > 0;
216 be_node_set_irn_reg(const arch_irn_ops_t *_self, ir_node *irn,
217 int idx, const arch_register_t *reg)
219 const arch_register_t **regs;
221 idx = redir_proj((const ir_node **) &irn, idx);
222 regs = (const arch_register_t **) &irn->attr;
226 const arch_register_t *
227 be_node_get_irn_reg(const arch_irn_ops_t *_self, const ir_node *irn, int idx)
229 const arch_register_t **regs;
231 idx = redir_proj(&irn, idx);
232 regs = (const arch_register_t **) &irn->attr;
236 arch_irn_class_t be_node_classify(const arch_irn_ops_t *_self, const ir_node *irn)
238 const be_node_factory_t *factory =
239 container_of(_self, const be_node_factory_t, irn_ops);
244 idx = redir_proj(&irn, 0);
245 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
250 static const arch_irn_ops_t *
251 be_node_get_irn_ops(const arch_irn_handler_t *_self, const ir_node *irn)
254 const be_node_factory_t *factory =
255 container_of(_self, const be_node_factory_t, handler);
258 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
260 return bo ? &factory->irn_ops : NULL;
263 const arch_irn_handler_t *be_node_get_irn_handler(const be_node_factory_t *f)
268 int is_Spill(const be_node_factory_t *f, const ir_node *irn)
271 bo = pmap_get(f->irn_op_map, get_irn_op(irn));
272 return bo->kind == node_kind_spill;
275 be_node_factory_t *be_node_factory_init(be_node_factory_t *factory,
276 const arch_isa_if_t *isa)
281 factory->ops = new_set(cmp_op_map, 64);
282 factory->irn_op_map = pmap_create();
283 obstack_init(&factory->obst);
285 factory->handler.get_irn_ops = be_node_get_irn_ops;
287 factory->irn_ops.get_irn_reg_req = be_node_get_irn_reg_req;
288 factory->irn_ops.get_n_operands = be_node_get_n_operands;
289 factory->irn_ops.set_irn_reg = be_node_set_irn_reg;
290 factory->irn_ops.get_irn_reg = be_node_get_irn_reg;
291 factory->irn_ops.classify = be_node_classify;
293 for(i = 0, n = isa->get_n_reg_class(); i < n; ++i) {
294 const arch_register_class_t *cls = isa->get_reg_class(i);
297 ent = get_op(factory, cls, node_kind_spill);
298 snprintf(buf, sizeof(buf), "Spill_%s", cls->name);
299 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned,
300 0, 0, oparity_unary, 0);
301 ent->n_pos = ARRSIZE(templ_pos_Spill);
302 ent->pos = templ_pos_Spill;
303 pmap_insert(factory->irn_op_map, ent->op, ent);
305 ent = get_op(factory, cls, node_kind_reload);
306 snprintf(buf, sizeof(buf), "Reload_%s", cls->name);
307 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
308 oparity_unary, sizeof(const arch_register_t *));
309 ent->n_pos = ARRSIZE(templ_pos_Reload);
310 ent->pos = templ_pos_Reload;
311 pmap_insert(factory->irn_op_map, ent->op, ent);
313 ent = get_op(factory, cls, node_kind_copy);
314 snprintf(buf, sizeof(buf), "Copy_%s", cls->name);
315 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
316 oparity_unary, sizeof(const arch_register_t *));
317 ent->n_pos = ARRSIZE(templ_pos_Copy);
318 ent->pos = templ_pos_Copy;
319 pmap_insert(factory->irn_op_map, ent->op, ent);
321 ent = get_op(factory, cls, node_kind_perm);
322 snprintf(buf, sizeof(buf), "Perm_%s", cls->name);
323 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
324 oparity_variable, sizeof(const arch_register_t) * cls->n_regs);
325 ent->n_pos = 2 * cls->n_regs;
326 ent->pos = obstack_alloc(&factory->obst, sizeof(ent->pos[0]) * ent->n_pos);
327 for(j = 0; j < ent->n_pos; j += 2) {
328 ent->pos[j] = arch_pos_make_in(j);
329 ent->pos[j + 1] = arch_pos_make_out(j);
331 pmap_insert(factory->irn_op_map, ent->op, ent);
338 ir_node *insert_Perm_after(const be_main_session_env_t *env,
339 const arch_register_class_t *cls, ir_node *pos)
341 const arch_env_t *arch_env = env->main_env->arch_env;
342 ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
343 ir_graph *irg = get_irn_irg(bl);
344 pset *live = put_live_end(bl, pset_new_ptr_default());
345 ir_node *curr, *irn, *perm, **nodes;
346 firm_dbg_module_t *dbg = firm_dbg_register("firm.be.node");
349 firm_dbg_set_mask(dbg, DBG_LEVEL);
350 DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
352 sched_foreach_reverse(bl, irn) {
356 * If we encounter the node we want to insert the Perm after,
357 * exit immediately, so that this node is still live
362 DBG((dbg, LEVEL_1, "%+F\n", irn));
363 for(x = pset_first(live); x; x = pset_next(live))
364 DBG((dbg, LEVEL_1, "\tlive: %+F\n", x));
366 if(arch_irn_has_reg_class(arch_env, irn, arch_pos_make_out(0), cls))
367 pset_remove_ptr(live, irn);
369 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
370 ir_node *op = get_irn_n(irn, i);
372 if(arch_irn_has_reg_class(arch_env, op, arch_pos_make_out(0), cls))
373 pset_insert_ptr(live, op);
377 n = pset_count(live);
378 nodes = malloc(n * sizeof(nodes[0]));
380 DBG((dbg, LEVEL_1, "live:\n"));
381 for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++) {
382 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
386 perm = new_Perm(env->main_env->node_factory, cls, irg, bl, n, nodes);
387 sched_add_after(pos, perm);
391 for(i = 0; i < n; ++i) {
393 ir_node *perm_op = get_irn_n(perm, i);
394 const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op, 0);
396 ir_mode *mode = get_irn_mode(perm_op);
397 ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
398 arch_set_irn_register(arch_env, proj, 0, reg);
400 sched_add_after(curr, proj);
404 be_introduce_copies(env->dom_front, perm_op, array_size(copies), copies);
409 typedef struct _phi_perm_info_t {
410 const be_main_session_env_t *env;
411 const arch_register_class_t *cls;
415 static void clear_link(ir_node *irn, void *data)
417 set_irn_link(irn, NULL);
420 static void collect_phis(ir_node *irn, void *data)
422 phi_perm_info_t *pi = data;
423 if(is_Phi(irn) && arch_irn_has_reg_class(pi->env->main_env->arch_env,
424 irn, arch_pos_make_out(0), pi->cls)) {
426 ir_node *bl = get_nodes_block(irn);
427 set_irn_link(irn, get_irn_link(bl));
428 set_irn_link(bl, irn);
432 /* This is only semi-ugly :-) */
433 #define INT_TO_PTR(i) ((void *) ((char *) 0 + (i)))
434 #define PTR_TO_INT(p) ((int) ((char *) (p) - (char *) 0))
436 static void insert_phi_perms(ir_node *bl, void *data)
438 phi_perm_info_t *pi = data;
439 ir_graph *irg = pi->env->irg;
441 assert(is_Block(bl));
443 /* If the link flag is NULL, this block has no phis. */
444 if(get_irn_link(bl)) {
447 /* Look at all predecessors of the phi block */
448 for(i = 0, n = get_irn_arity(bl); i < n; ++i) {
449 ir_node *pred_bl = get_Block_cfgpred_block(bl, i);
455 for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi))
458 in = malloc(n_phis * sizeof(in[0]));
460 /* Look at all phis in this block */
462 for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi))
463 in[j++] = get_irn_n(phi, i);
465 perm = new_Perm(pi->env->main_env->node_factory, pi->cls, irg, pred_bl, n_phis, in);
468 for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
469 ir_node *proj = new_r_Proj(irg, pred_bl, perm, get_irn_mode(phi), j++);
470 set_irn_n(phi, i, proj);
478 void be_insert_phi_perms(const be_main_session_env_t *env,
479 const arch_register_class_t *cls)
485 pi.perm_map = pmap_create();
487 irg_walk_graph(env->irg, clear_link, collect_phis, &pi);
488 irg_block_walk_graph(env->irg, insert_phi_perms, NULL, &pi);
489 pmap_destroy(pi.perm_map);