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));
251 static const arch_irn_ops_t *
252 be_node_get_irn_ops(const arch_irn_handler_t *_self, const ir_node *irn)
255 const be_node_factory_t *factory =
256 container_of(_self, const be_node_factory_t, handler);
259 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
261 return bo ? &factory->irn_ops : NULL;
264 const arch_irn_handler_t *be_node_get_irn_handler(const be_node_factory_t *f)
269 int is_Spill(const be_node_factory_t *f, const ir_node *irn)
272 bo = pmap_get(f->irn_op_map, get_irn_op(irn));
273 return bo->kind == node_kind_spill;
276 be_node_factory_t *be_node_factory_init(be_node_factory_t *factory,
277 const arch_isa_if_t *isa)
282 factory->ops = new_set(cmp_op_map, 64);
283 factory->irn_op_map = pmap_create();
284 obstack_init(&factory->obst);
286 factory->handler.get_irn_ops = be_node_get_irn_ops;
288 factory->irn_ops.get_irn_reg_req = be_node_get_irn_reg_req;
289 factory->irn_ops.get_n_operands = be_node_get_n_operands;
290 factory->irn_ops.set_irn_reg = be_node_set_irn_reg;
291 factory->irn_ops.get_irn_reg = be_node_get_irn_reg;
292 factory->irn_ops.classify = be_node_classify;
294 for(i = 0, n = isa->get_n_reg_class(); i < n; ++i) {
295 const arch_register_class_t *cls = isa->get_reg_class(i);
298 ent = get_op(factory, cls, node_kind_spill);
299 snprintf(buf, sizeof(buf), "Spill_%s", cls->name);
300 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned,
301 0, 0, oparity_unary, 0);
302 ent->n_pos = ARRSIZE(templ_pos_Spill);
303 ent->pos = templ_pos_Spill;
304 pmap_insert(factory->irn_op_map, ent->op, ent);
306 ent = get_op(factory, cls, node_kind_reload);
307 snprintf(buf, sizeof(buf), "Reload_%s", cls->name);
308 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
309 oparity_unary, sizeof(const arch_register_t *));
310 ent->n_pos = ARRSIZE(templ_pos_Reload);
311 ent->pos = templ_pos_Reload;
312 pmap_insert(factory->irn_op_map, ent->op, ent);
314 ent = get_op(factory, cls, node_kind_copy);
315 snprintf(buf, sizeof(buf), "Copy_%s", cls->name);
316 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
317 oparity_unary, sizeof(const arch_register_t *));
318 ent->n_pos = ARRSIZE(templ_pos_Copy);
319 ent->pos = templ_pos_Copy;
320 pmap_insert(factory->irn_op_map, ent->op, ent);
322 ent = get_op(factory, cls, node_kind_perm);
323 snprintf(buf, sizeof(buf), "Perm_%s", cls->name);
324 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
325 oparity_variable, sizeof(const arch_register_t) * cls->n_regs);
326 ent->n_pos = 2 * cls->n_regs;
327 ent->pos = obstack_alloc(&factory->obst, sizeof(ent->pos[0]) * ent->n_pos);
328 for(j = 0; j < ent->n_pos; j += 2) {
329 ent->pos[j] = arch_pos_make_in(j);
330 ent->pos[j + 1] = arch_pos_make_out(j);
332 pmap_insert(factory->irn_op_map, ent->op, ent);
339 ir_node *insert_Perm_after(const be_main_session_env_t *env,
340 const arch_register_class_t *cls, ir_node *pos)
342 const arch_env_t *arch_env = env->main_env->arch_env;
343 ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
344 ir_graph *irg = get_irn_irg(bl);
345 pset *live = put_live_end(bl, pset_new_ptr_default());
346 ir_node *curr, *irn, *perm, **nodes;
347 firm_dbg_module_t *dbg = firm_dbg_register("firm.be.node");
350 firm_dbg_set_mask(dbg, DBG_LEVEL);
351 DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
353 sched_foreach_reverse(bl, irn) {
357 * If we encounter the node we want to insert the Perm after,
358 * exit immediately, so that this node is still live
363 DBG((dbg, LEVEL_1, "%+F\n", irn));
364 for(x = pset_first(live); x; x = pset_next(live))
365 DBG((dbg, LEVEL_1, "\tlive: %+F\n", x));
367 if(arch_irn_has_reg_class(arch_env, irn, arch_pos_make_out(0), cls))
368 pset_remove_ptr(live, irn);
370 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
371 ir_node *op = get_irn_n(irn, i);
373 if(arch_irn_has_reg_class(arch_env, op, arch_pos_make_out(0), cls))
374 pset_insert_ptr(live, op);
378 n = pset_count(live);
379 nodes = malloc(n * sizeof(nodes[0]));
381 DBG((dbg, LEVEL_1, "live:\n"));
382 for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++) {
383 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
387 perm = new_Perm(env->main_env->node_factory, cls, irg, bl, n, nodes);
388 sched_add_after(pos, perm);
392 for(i = 0; i < n; ++i) {
394 ir_node *perm_op = get_irn_n(perm, i);
395 const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op, 0);
397 ir_mode *mode = get_irn_mode(perm_op);
398 ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
399 arch_set_irn_register(arch_env, proj, 0, reg);
401 sched_add_after(curr, proj);
405 be_introduce_copies(env->dom_front, perm_op, array_size(copies), copies);
411 typedef struct _phi_perm_info_t {
412 const be_main_session_env_t *env;
413 const arch_register_class_t *cls;
417 static void clear_link(ir_node *irn, void *data)
419 set_irn_link(irn, NULL);
422 static void collect_phis(ir_node *irn, void *data)
424 phi_perm_info_t *pi = data;
425 if(is_Phi(irn) && arch_irn_has_reg_class(pi->env->main_env->arch_env,
426 irn, arch_pos_make_out(0), pi->cls)) {
428 ir_node *bl = get_nodes_block(irn);
429 set_irn_link(irn, get_irn_link(bl));
430 set_irn_link(bl, irn);
434 /* This is only semi-ugly :-) */
435 #define INT_TO_PTR(i) ((void *) ((char *) 0 + (i)))
436 #define PTR_TO_INT(p) ((int) ((char *) (p) - (char *) 0))
438 static void insert_phi_perms(ir_node *bl, void *data)
440 phi_perm_info_t *pi = data;
441 ir_graph *irg = pi->env->irg;
443 assert(is_Block(bl));
445 /* If the link flag is NULL, this block has no phis. */
446 if(get_irn_link(bl)) {
449 /* Look at all predecessors of the phi block */
450 for(i = 0, n = get_irn_arity(bl); i < n; ++i) {
451 ir_node *pred_bl = get_Block_cfgpred_block(bl, i);
457 for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi))
460 in = malloc(n_phis * sizeof(in[0]));
462 /* Look at all phis in this block */
464 for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi))
465 in[j++] = get_irn_n(phi, i);
467 perm = new_Perm(pi->env->main_env->node_factory, pi->cls, irg, pred_bl, n_phis, in);
470 for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
471 ir_node *proj = new_r_Proj(irg, pred_bl, perm, get_irn_mode(phi), j++);
472 set_irn_n(phi, i, proj);
480 void be_insert_phi_perms(const be_main_session_env_t *env,
481 const arch_register_class_t *cls)
487 pi.perm_map = pmap_create();
489 irg_walk_graph(env->irg, clear_link, collect_phis, &pi);
490 irg_block_walk_graph(env->irg, insert_phi_perms, NULL, &pi);
491 pmap_destroy(pi.perm_map);