4 * @author Sebastian Hack
6 * Backend node support.
8 * Copyright (C) 2005 Universitaet Karlsruhe
9 * Released under the GPL
26 #include "besched_t.h"
31 typedef enum _node_kind_t {
41 const arch_register_class_t *cls;
47 static int templ_pos_Spill[] = {
51 static int templ_pos_Reload[] = {
55 static int templ_pos_Copy[] = {
60 #define ARRSIZE(x) (sizeof(x) / sizeof(x[0]))
62 static int cmp_op_map(const void *a, const void *b, size_t size)
67 return !(x->kind == y->kind && x->cls == y->cls);
70 static be_op_t *get_op(const be_node_factory_t *fact,
71 const arch_register_class_t *cls, node_kind_t kind)
78 return set_insert(fact->ops, &templ, sizeof(templ),
79 HASH_PTR(cls) + 7 * kind);
82 ir_node *new_Spill(const be_node_factory_t *factory,
83 const arch_register_class_t *cls,
84 ir_graph *irg, ir_node *bl, ir_node *node_to_spill)
87 ir_op *op = get_op(factory, cls, node_kind_spill)->op;
89 assert(op && "Spill opcode must be present for this register class");
90 in[0] = node_to_spill;
92 return new_ir_node(NULL, irg, bl, op, mode_M, 1, in);
95 ir_node *new_Reload(const be_node_factory_t *factory,
96 const arch_register_class_t *cls,
97 ir_graph *irg, ir_node *bl, ir_node *spill_node)
101 ir_op *op = get_op(factory, cls, node_kind_reload)->op;
103 assert(op && "Reload opcode must be present for this register class");
104 assert(is_Spill(factory, spill_node) && "Operand of Reload must be a Spill");
106 mode = get_irn_mode(get_irn_n(spill_node, 0));
108 return new_ir_node(NULL, irg, bl, op, mode, 1, in);
111 ir_node *new_Perm(const be_node_factory_t *factory,
112 const arch_register_class_t *cls,
113 ir_graph *irg, ir_node *bl, int arity, ir_node **in)
115 ir_op *op = get_op(factory, cls, node_kind_perm)->op;
117 return new_ir_node(NULL, irg, bl, op, mode_T, arity, in);
120 ir_node *new_Copy(const be_node_factory_t *factory,
121 const arch_register_class_t *cls,
122 ir_graph *irg, ir_node *bl, ir_node *in)
125 ir_op *op = get_op(factory, cls, node_kind_perm)->op;
129 return new_ir_node(NULL, irg, bl, op, get_irn_mode(in), 1, ins);
133 * If the node is a proj, reset the node to the proj's target and return
135 * @param node The address of a node pointer.
136 * @param def A default value.
137 * @return If *node is a Proj, *node is set to the Proj's target and
138 * the Proj number is returned. Else *node remains the same and @p def
141 static int redir_proj(const ir_node **node, int def)
143 const ir_node *n = *node;
146 *node = get_Proj_pred(n);
147 def = get_Proj_proj(n);
153 static const arch_register_req_t *
154 be_node_get_irn_reg_req(const arch_irn_ops_t *_self,
155 arch_register_req_t *req, const ir_node *irn, int pos)
158 const be_node_factory_t *factory =
159 container_of(_self, const be_node_factory_t, irn_ops);
161 pos = arch_pos_make_out(redir_proj(&irn, arch_pos_get_index(pos)));
163 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
168 req->type = arch_register_req_type_normal;
171 for(i = 0; i < bo->n_pos; ++pos)
172 if(pos == bo->pos[i])
180 be_node_get_n_operands(const arch_irn_ops_t *_self, const ir_node *irn, int in_out)
184 const be_node_factory_t *factory =
185 container_of(_self, const be_node_factory_t, irn_ops);
188 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
190 for(i = 0; i < bo->n_pos; ++i)
191 res += (bo->pos[i] ^ in_out) > 0;
197 be_node_set_irn_reg(const arch_irn_ops_t *_self, ir_node *irn,
198 int idx, const arch_register_t *reg)
200 const arch_register_t **regs;
202 idx = redir_proj((const ir_node **) &irn, idx);
203 regs = (const arch_register_t **) &irn->attr;
207 const arch_register_t *
208 be_node_get_irn_reg(const arch_irn_ops_t *_self, const ir_node *irn, int idx)
210 const arch_register_t **regs;
212 idx = redir_proj(&irn, idx);
213 regs = (const arch_register_t **) &irn->attr;
217 arch_irn_class_t be_node_classify(const arch_irn_ops_t *_self, const ir_node *irn)
219 const be_node_factory_t *factory =
220 container_of(_self, const be_node_factory_t, irn_ops);
225 idx = redir_proj(&irn, 0);
226 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
231 static const arch_irn_ops_t *
232 be_node_get_irn_ops(const arch_irn_handler_t *_self, const ir_node *irn)
235 const be_node_factory_t *factory =
236 container_of(_self, const be_node_factory_t, handler);
239 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
241 return bo ? &factory->irn_ops : NULL;
244 const arch_irn_handler_t *be_node_get_irn_handler(const be_node_factory_t *f)
249 int is_Spill(const be_node_factory_t *f, const ir_node *irn)
252 bo = pmap_get(f->irn_op_map, get_irn_op(irn));
253 return bo->kind == node_kind_spill;
256 be_node_factory_t *be_node_factory_init(be_node_factory_t *factory,
257 const arch_isa_if_t *isa)
262 factory->ops = new_set(cmp_op_map, 64);
263 factory->irn_op_map = pmap_create();
264 obstack_init(&factory->obst);
266 factory->handler.get_irn_ops = be_node_get_irn_ops;
268 factory->irn_ops.get_irn_reg_req = be_node_get_irn_reg_req;
269 factory->irn_ops.get_n_operands = be_node_get_n_operands;
270 factory->irn_ops.set_irn_reg = be_node_set_irn_reg;
271 factory->irn_ops.get_irn_reg = be_node_get_irn_reg;
272 factory->irn_ops.classify = be_node_classify;
274 for(i = 0, n = isa->get_n_reg_class(); i < n; ++i) {
275 const arch_register_class_t *cls = isa->get_reg_class(i);
278 ent = get_op(factory, cls, node_kind_spill);
279 snprintf(buf, sizeof(buf), "Spill_%s", cls->name);
280 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned,
281 0, 0, oparity_unary, 0);
282 ent->n_pos = ARRSIZE(templ_pos_Spill);
283 ent->pos = templ_pos_Spill;
284 pmap_insert(factory->irn_op_map, ent->op, ent);
286 ent = get_op(factory, cls, node_kind_reload);
287 snprintf(buf, sizeof(buf), "Reload_%s", cls->name);
288 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
289 oparity_unary, sizeof(const arch_register_t *));
290 ent->n_pos = ARRSIZE(templ_pos_Reload);
291 ent->pos = templ_pos_Reload;
292 pmap_insert(factory->irn_op_map, ent->op, ent);
294 ent = get_op(factory, cls, node_kind_copy);
295 snprintf(buf, sizeof(buf), "Copy_%s", cls->name);
296 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
297 oparity_unary, sizeof(const arch_register_t *));
298 ent->n_pos = ARRSIZE(templ_pos_Copy);
299 ent->pos = templ_pos_Copy;
300 pmap_insert(factory->irn_op_map, ent->op, ent);
302 ent = get_op(factory, cls, node_kind_perm);
303 snprintf(buf, sizeof(buf), "Perm_%s", cls->name);
304 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
305 oparity_variable, sizeof(const arch_register_t) * cls->n_regs);
306 ent->n_pos = 2 * cls->n_regs;
307 ent->pos = obstack_alloc(&factory->obst, sizeof(ent->pos[0]) * ent->n_pos);
308 for(j = 0; j < ent->n_pos; j += 2) {
309 ent->pos[j] = arch_pos_make_in(j);
310 ent->pos[j + 1] = arch_pos_make_out(j);
312 pmap_insert(factory->irn_op_map, ent->op, ent);
319 void insert_Perm_after(const be_main_session_env_t *env,
320 const arch_register_class_t *cls, ir_node *pos)
322 const arch_env_t *arch_env = env->main_env->arch_env;
323 ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
324 ir_graph *irg = get_irn_irg(bl);
325 pset *live_end = get_live_end(bl);
326 pset *live = pset_new_ptr_default();
327 ir_node *curr, *irn, *perm, **nodes;
330 /* put all live ends in the live set. */
331 for(irn = pset_first(live_end); irn; irn = pset_next(live_end))
332 pset_insert_ptr(live, irn);
334 sched_foreach_reverse(bl, irn) {
336 if(arch_irn_has_reg_class(arch_env, irn, arch_pos_make_out(0), cls))
337 pset_remove_ptr(live, irn);
339 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
340 ir_node *op = get_irn_n(irn, i);
342 if(arch_irn_has_reg_class(arch_env, op, arch_pos_make_out(0), cls))
343 pset_insert_ptr(live, op);
346 if(sched_prev(irn) == pos)
350 n = pset_count(live);
351 nodes = malloc(n * sizeof(nodes[0]));
353 for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++)
356 curr = perm = new_Perm(env->main_env->node_factory, cls, irg, bl, n, nodes);
357 sched_add_after(pos, perm);
360 for(i = 0; i < n; ++i) {
362 ir_node *perm_op = get_irn_n(perm, i);
364 ir_mode *mode = get_irn_mode(perm_op);
365 ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
366 sched_add_after(curr, proj);
370 be_introduce_copies(env->dom_front, perm_op, array_size(copies), copies);