4 * @author Sebastian Hack
6 * Backend node support.
8 * Copyright (C) 2005 Universitaet Karlsruhe
9 * Released under the GPL
25 typedef enum _node_kind_t {
35 const arch_register_class_t *cls;
41 static int templ_pos_Spill[] = {
45 static int templ_pos_Reload[] = {
49 static int templ_pos_Copy[] = {
54 #define ARRSIZE(x) (sizeof(x) / sizeof(x[0]))
56 static int cmp_op_map(const void *a, const void *b, size_t size)
61 return !(x->kind == y->kind && x->cls == y->cls);
64 static be_op_t *get_op(const be_node_factory_t *fact,
65 const arch_register_class_t *cls, node_kind_t kind)
72 return set_insert(fact->ops, &templ, sizeof(templ),
73 HASH_PTR(cls) + 7 * kind);
76 ir_node *new_Spill(const be_node_factory_t *factory,
77 const arch_register_class_t *cls,
78 ir_graph *irg, ir_node *bl, ir_node *node_to_spill)
81 ir_op *op = get_op(factory, cls, node_kind_spill)->op;
83 assert(op && "Spill opcode must be present for this register class");
84 in[0] = node_to_spill;
86 return new_ir_node(NULL, irg, bl, op, mode_M, 1, in);
89 ir_node *new_Reload(const be_node_factory_t *factory,
90 const arch_register_class_t *cls,
91 ir_graph *irg, ir_node *bl, ir_node *spill_node)
95 ir_op *op = get_op(factory, cls, node_kind_reload)->op;
97 assert(op && "Reload opcode must be present for this register class");
98 assert(is_Spill(factory, spill_node) && "Operand of Reload must be a Spill");
100 mode = get_irn_mode(get_irn_n(spill_node, 0));
102 return new_ir_node(NULL, irg, bl, op, mode, 1, in);
105 ir_node *new_Perm(const be_node_factory_t *factory,
106 const arch_register_class_t *cls,
107 ir_graph *irg, ir_node *bl, int arity, ir_node **in)
109 ir_op *op = get_op(factory, cls, node_kind_perm)->op;
111 return new_ir_node(NULL, irg, bl, op, mode_T, arity, in);
114 ir_node *new_Copy(const be_node_factory_t *factory,
115 const arch_register_class_t *cls,
116 ir_graph *irg, ir_node *bl, ir_node *in)
119 ir_op *op = get_op(factory, cls, node_kind_perm)->op;
123 return new_ir_node(NULL, irg, bl, op, get_irn_mode(in), 1, ins);
127 * If the node is a proj, reset the node to the proj's target and return
129 * @param node The address of a node pointer.
130 * @param def A default value.
131 * @return If *node is a Proj, *node is set to the Proj's target and
132 * the Proj number is returned. Else *node remains the same and @p def
135 static int redir_proj(const ir_node **node, int def)
137 const ir_node *n = *node;
140 *node = get_Proj_pred(n);
141 def = get_Proj_proj(n);
147 static const arch_register_req_t *
148 be_node_get_irn_reg_req(const arch_irn_ops_t *_self,
149 arch_register_req_t *req, const ir_node *irn, int pos)
152 const be_node_factory_t *factory =
153 container_of(_self, const be_node_factory_t, irn_ops);
155 pos = arch_pos_make_out(redir_proj(&irn, arch_pos_get_index(pos)));
157 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
162 req->type = arch_register_req_type_normal;
165 for(i = 0; i < bo->n_pos; ++pos)
166 if(pos == bo->pos[i])
174 be_node_get_n_operands(const arch_irn_ops_t *_self, const ir_node *irn, int in_out)
178 const be_node_factory_t *factory =
179 container_of(_self, const be_node_factory_t, irn_ops);
182 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
184 for(i = 0; i < bo->n_pos; ++i)
185 res += (bo->pos[i] ^ in_out) > 0;
191 be_node_set_irn_reg(const arch_irn_ops_t *_self, ir_node *irn,
192 int idx, const arch_register_t *reg)
194 const arch_register_t **regs;
196 idx = redir_proj((const ir_node **) &irn, idx);
197 regs = (const arch_register_t **) &irn->attr;
201 const arch_register_t *
202 be_node_get_irn_reg(const arch_irn_ops_t *_self, const ir_node *irn, int idx)
204 const arch_register_t **regs;
206 idx = redir_proj(&irn, idx);
207 regs = (const arch_register_t **) &irn->attr;
211 arch_irn_class_t be_node_classify(const arch_irn_ops_t *_self, const ir_node *irn)
213 const be_node_factory_t *factory =
214 container_of(_self, const be_node_factory_t, irn_ops);
219 idx = redir_proj(&irn, 0);
220 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
225 static const arch_irn_ops_t *
226 be_node_get_irn_ops(const arch_irn_handler_t *_self, const ir_node *irn)
229 const be_node_factory_t *factory =
230 container_of(_self, const be_node_factory_t, handler);
233 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
235 return bo ? &factory->irn_ops : NULL;
238 const arch_irn_handler_t *be_node_get_irn_handler(const be_node_factory_t *f)
243 int is_Spill(const be_node_factory_t *f, const ir_node *irn)
246 bo = pmap_get(f->irn_op_map, get_irn_op(irn));
247 return bo->kind == node_kind_spill;
250 be_node_factory_t *be_node_factory_init(be_node_factory_t *factory,
251 const arch_isa_if_t *isa)
256 factory->ops = new_set(cmp_op_map, 64);
257 factory->irn_op_map = pmap_create();
258 obstack_init(&factory->obst);
260 factory->handler.get_irn_ops = be_node_get_irn_ops;
262 factory->irn_ops.get_irn_reg_req = be_node_get_irn_reg_req;
263 factory->irn_ops.get_n_operands = be_node_get_n_operands;
264 factory->irn_ops.set_irn_reg = be_node_set_irn_reg;
265 factory->irn_ops.get_irn_reg = be_node_get_irn_reg;
266 factory->irn_ops.classify = be_node_classify;
268 for(i = 0, n = isa->get_n_reg_class(); i < n; ++i) {
269 const arch_register_class_t *cls = isa->get_reg_class(i);
272 ent = get_op(factory, cls, node_kind_spill);
273 snprintf(buf, sizeof(buf), "Spill_%s", cls->name);
274 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned,
275 0, 0, oparity_unary, 0);
276 ent->n_pos = ARRSIZE(templ_pos_Spill);
277 ent->pos = templ_pos_Spill;
278 pmap_insert(factory->irn_op_map, ent->op, ent);
280 ent = get_op(factory, cls, node_kind_reload);
281 snprintf(buf, sizeof(buf), "Reload_%s", cls->name);
282 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
283 oparity_unary, sizeof(const arch_register_t *));
284 ent->n_pos = ARRSIZE(templ_pos_Reload);
285 ent->pos = templ_pos_Reload;
286 pmap_insert(factory->irn_op_map, ent->op, ent);
288 ent = get_op(factory, cls, node_kind_copy);
289 snprintf(buf, sizeof(buf), "Copy_%s", cls->name);
290 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
291 oparity_unary, sizeof(const arch_register_t *));
292 ent->n_pos = ARRSIZE(templ_pos_Copy);
293 ent->pos = templ_pos_Copy;
294 pmap_insert(factory->irn_op_map, ent->op, ent);
296 ent = get_op(factory, cls, node_kind_perm);
297 snprintf(buf, sizeof(buf), "Perm_%s", cls->name);
298 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
299 oparity_variable, sizeof(const arch_register_t) * cls->n_regs);
300 ent->n_pos = 2 * cls->n_regs;
301 ent->pos = obstack_alloc(&factory->obst, sizeof(ent->pos[0]) * ent->n_pos);
302 for(j = 0; j < ent->n_pos; j += 2) {
303 ent->pos[j] = arch_pos_make_in(j);
304 ent->pos[j + 1] = arch_pos_make_out(j);
306 pmap_insert(factory->irn_op_map, ent->op, ent);
314 ir_node *be_spill(const be_node_factory_t *factory,
315 const arch_env_t *env, ir_node *node_to_spill)
318 if(is_Reload(node_to_spill))
319 res = get_irn_n(node_to_spill, 0);
321 ir_node *bl = get_nodes_block(node_to_spill);
322 ir_graph *irg = get_irn_irg(bl);
324 res = new_Spill(factory, cls, irg, bl, node_to_spill);
330 ir_node *be_reload(const be_node_factory_t *factory,
331 const arch_env_t *env, ir_node *spill)