4 * @author Sebastian Hack
6 * Backend node support.
8 * Copyright (C) 2005 Universitaet Karlsruhe
9 * Released under the GPL
29 #include "besched_t.h"
34 typedef enum _node_kind_t {
44 const arch_register_class_t *cls;
50 static int templ_pos_Spill[] = {
54 static int templ_pos_Reload[] = {
58 static int templ_pos_Copy[] = {
63 #define ARRSIZE(x) (sizeof(x) / sizeof(x[0]))
65 static int cmp_op_map(const void *a, const void *b, size_t size)
70 return !(x->kind == y->kind && x->cls == y->cls);
73 static be_op_t *get_op(const be_node_factory_t *fact,
74 const arch_register_class_t *cls, node_kind_t kind)
81 return set_insert(fact->ops, &templ, sizeof(templ),
82 HASH_PTR(cls) + 7 * kind);
85 ir_node *new_Spill(const be_node_factory_t *factory,
86 const arch_register_class_t *cls,
87 ir_graph *irg, ir_node *bl, ir_node *node_to_spill)
90 ir_op *op = get_op(factory, cls, node_kind_spill)->op;
92 assert(op && "Spill opcode must be present for this register class");
93 in[0] = node_to_spill;
95 return new_ir_node(NULL, irg, bl, op, mode_M, 1, in);
98 ir_node *new_Reload(const be_node_factory_t *factory,
99 const arch_register_class_t *cls,
100 ir_graph *irg, ir_node *bl, ir_node *spill_node)
104 ir_op *op = get_op(factory, cls, node_kind_reload)->op;
106 assert(op && "Reload opcode must be present for this register class");
107 assert(is_Spill(factory, spill_node) && "Operand of Reload must be a Spill");
109 mode = get_irn_mode(get_irn_n(spill_node, 0));
111 return new_ir_node(NULL, irg, bl, op, mode, 1, in);
114 ir_node *new_Perm(const be_node_factory_t *factory,
115 const arch_register_class_t *cls,
116 ir_graph *irg, ir_node *bl, int arity, ir_node **in)
118 ir_op *op = get_op(factory, cls, node_kind_perm)->op;
120 return new_ir_node(NULL, irg, bl, op, mode_T, arity, in);
123 ir_node *new_Copy(const be_node_factory_t *factory,
124 const arch_register_class_t *cls,
125 ir_graph *irg, ir_node *bl, ir_node *in)
128 ir_op *op = get_op(factory, cls, node_kind_perm)->op;
132 return new_ir_node(NULL, irg, bl, op, get_irn_mode(in), 1, ins);
136 * If the node is a proj, reset the node to the proj's target and return
138 * @param node The address of a node pointer.
139 * @param def A default value.
140 * @return If *node is a Proj, *node is set to the Proj's target and
141 * the Proj number is returned. Else *node remains the same and @p def
144 static int redir_proj(const ir_node **node, int def)
146 const ir_node *n = *node;
149 *node = get_Proj_pred(n);
150 def = get_Proj_proj(n);
156 static const arch_register_req_t *
157 be_node_get_irn_reg_req(const arch_irn_ops_t *_self,
158 arch_register_req_t *req, const ir_node *irn, int pos)
161 const be_node_factory_t *factory =
162 container_of(_self, const be_node_factory_t, irn_ops);
164 pos = arch_pos_make_out(redir_proj(&irn, arch_pos_get_index(pos)));
166 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
171 req->type = arch_register_req_type_normal;
174 for(i = 0; i < bo->n_pos; ++pos)
175 if(pos == bo->pos[i])
183 be_node_get_n_operands(const arch_irn_ops_t *_self, const ir_node *irn, int in_out)
187 const be_node_factory_t *factory =
188 container_of(_self, const be_node_factory_t, irn_ops);
191 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
193 for(i = 0; i < bo->n_pos; ++i)
194 res += (bo->pos[i] ^ in_out) > 0;
200 be_node_set_irn_reg(const arch_irn_ops_t *_self, ir_node *irn,
201 int idx, const arch_register_t *reg)
203 const arch_register_t **regs;
205 idx = redir_proj((const ir_node **) &irn, idx);
206 regs = (const arch_register_t **) &irn->attr;
210 const arch_register_t *
211 be_node_get_irn_reg(const arch_irn_ops_t *_self, const ir_node *irn, int idx)
213 const arch_register_t **regs;
215 idx = redir_proj(&irn, idx);
216 regs = (const arch_register_t **) &irn->attr;
220 arch_irn_class_t be_node_classify(const arch_irn_ops_t *_self, const ir_node *irn)
222 const be_node_factory_t *factory =
223 container_of(_self, const be_node_factory_t, irn_ops);
228 idx = redir_proj(&irn, 0);
229 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
234 static const arch_irn_ops_t *
235 be_node_get_irn_ops(const arch_irn_handler_t *_self, const ir_node *irn)
238 const be_node_factory_t *factory =
239 container_of(_self, const be_node_factory_t, handler);
242 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
244 return bo ? &factory->irn_ops : NULL;
247 const arch_irn_handler_t *be_node_get_irn_handler(const be_node_factory_t *f)
252 int is_Spill(const be_node_factory_t *f, const ir_node *irn)
255 bo = pmap_get(f->irn_op_map, get_irn_op(irn));
256 return bo->kind == node_kind_spill;
259 be_node_factory_t *be_node_factory_init(be_node_factory_t *factory,
260 const arch_isa_if_t *isa)
265 factory->ops = new_set(cmp_op_map, 64);
266 factory->irn_op_map = pmap_create();
267 obstack_init(&factory->obst);
269 factory->handler.get_irn_ops = be_node_get_irn_ops;
271 factory->irn_ops.get_irn_reg_req = be_node_get_irn_reg_req;
272 factory->irn_ops.get_n_operands = be_node_get_n_operands;
273 factory->irn_ops.set_irn_reg = be_node_set_irn_reg;
274 factory->irn_ops.get_irn_reg = be_node_get_irn_reg;
275 factory->irn_ops.classify = be_node_classify;
277 for(i = 0, n = isa->get_n_reg_class(); i < n; ++i) {
278 const arch_register_class_t *cls = isa->get_reg_class(i);
281 ent = get_op(factory, cls, node_kind_spill);
282 snprintf(buf, sizeof(buf), "Spill_%s", cls->name);
283 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned,
284 0, 0, oparity_unary, 0);
285 ent->n_pos = ARRSIZE(templ_pos_Spill);
286 ent->pos = templ_pos_Spill;
287 pmap_insert(factory->irn_op_map, ent->op, ent);
289 ent = get_op(factory, cls, node_kind_reload);
290 snprintf(buf, sizeof(buf), "Reload_%s", cls->name);
291 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
292 oparity_unary, sizeof(const arch_register_t *));
293 ent->n_pos = ARRSIZE(templ_pos_Reload);
294 ent->pos = templ_pos_Reload;
295 pmap_insert(factory->irn_op_map, ent->op, ent);
297 ent = get_op(factory, cls, node_kind_copy);
298 snprintf(buf, sizeof(buf), "Copy_%s", cls->name);
299 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
300 oparity_unary, sizeof(const arch_register_t *));
301 ent->n_pos = ARRSIZE(templ_pos_Copy);
302 ent->pos = templ_pos_Copy;
303 pmap_insert(factory->irn_op_map, ent->op, ent);
305 ent = get_op(factory, cls, node_kind_perm);
306 snprintf(buf, sizeof(buf), "Perm_%s", cls->name);
307 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
308 oparity_variable, sizeof(const arch_register_t) * cls->n_regs);
309 ent->n_pos = 2 * cls->n_regs;
310 ent->pos = obstack_alloc(&factory->obst, sizeof(ent->pos[0]) * ent->n_pos);
311 for(j = 0; j < ent->n_pos; j += 2) {
312 ent->pos[j] = arch_pos_make_in(j);
313 ent->pos[j + 1] = arch_pos_make_out(j);
315 pmap_insert(factory->irn_op_map, ent->op, ent);
322 ir_node *insert_Perm_after(const be_main_session_env_t *env,
323 const arch_register_class_t *cls, ir_node *pos)
325 const arch_env_t *arch_env = env->main_env->arch_env;
326 ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
327 ir_graph *irg = get_irn_irg(bl);
328 pset *live_end = get_live_end(bl);
329 pset *live = pset_new_ptr_default();
330 ir_node *curr, *irn, *perm, **nodes;
333 /* put all live ends in the live set. */
334 for(irn = pset_first(live_end); irn; irn = pset_next(live_end))
335 pset_insert_ptr(live, irn);
337 sched_foreach_reverse(bl, irn) {
339 if(arch_irn_has_reg_class(arch_env, irn, arch_pos_make_out(0), cls))
340 pset_remove_ptr(live, irn);
342 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
343 ir_node *op = get_irn_n(irn, i);
345 if(arch_irn_has_reg_class(arch_env, op, arch_pos_make_out(0), cls))
346 pset_insert_ptr(live, op);
349 if(sched_prev(irn) == pos)
353 n = pset_count(live);
354 nodes = malloc(n * sizeof(nodes[0]));
356 for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++)
359 curr = perm = new_Perm(env->main_env->node_factory, cls, irg, bl, n, nodes);
360 sched_add_after(pos, perm);
363 for(i = 0; i < n; ++i) {
365 ir_node *perm_op = get_irn_n(perm, i);
367 ir_mode *mode = get_irn_mode(perm_op);
368 ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
369 sched_add_after(curr, proj);
373 be_introduce_copies(env->dom_front, perm_op, array_size(copies), copies);