4 * @author Sebastian Hack
6 * Backend node support.
8 * Copyright (C) 2005 Universitaet Karlsruhe
9 * Released under the GPL
25 struct _be_node_factory_t {
26 const arch_isa_if_t *isa;
33 arch_irn_handler_t handler;
34 arch_irn_ops_t irn_ops;
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,
103 ir_graph *irg, ir_node *bl, ir_node *spill_node)
107 ir_op *op = get_op(factory, cls, node_kind_reload)->op;
109 assert(op && "Reload opcode must be present for this register class");
110 assert(is_Spill(factory, spill_node) && "Operand of Reload must be a Spill");
112 mode = get_irn_mode(get_irn_n(spill_node, 0));
114 return new_ir_node(NULL, irg, bl, op, mode, 1, in);
117 ir_node *new_Perm(const be_node_factory_t *factory,
118 const arch_register_class_t *cls,
119 ir_graph *irg, ir_node *bl, int arity, ir_node **in)
121 ir_op *op = get_op(factory, cls, node_kind_perm)->op;
123 return new_ir_node(NULL, irg, bl, op, mode_T, arity, in);
126 ir_node *new_Copy(const be_node_factory_t *factory,
127 const arch_register_class_t *cls,
128 ir_graph *irg, ir_node *bl, ir_node *in)
131 ir_op *op = get_op(factory, cls, node_kind_perm)->op;
135 return new_ir_node(NULL, irg, bl, op, get_irn_mode(in), 1, ins);
139 * If the node is a proj, reset the node to the proj's target and return
141 * @param node The address of a node pointer.
142 * @param def A default value.
143 * @return If *node is a Proj, *node is set to the Proj's target and
144 * the Proj number is returned. Else *node remains the same and @p def
147 static int redir_proj(const ir_node **node, int def)
149 const ir_node *n = *node;
152 *node = get_Proj_pred(n);
153 def = get_Proj_proj(n);
159 static const arch_register_req_t *
160 be_node_get_irn_reg_req(const arch_irn_ops_t *_self,
161 arch_register_req_t *req, const ir_node *irn, int pos)
164 const be_node_factory_t *factory =
165 container_of(_self, const be_node_factory_t, irn_ops);
167 pos = arch_pos_make_out(redir_proj(&irn, arch_pos_get_index(pos)));
169 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
174 req->type = arch_register_req_type_normal;
177 for(i = 0; i < bo->n_pos; ++pos)
178 if(pos == bo->pos[i])
186 be_node_get_n_operands(const arch_irn_ops_t *_self, const ir_node *irn, int in_out)
190 const be_node_factory_t *factory =
191 container_of(_self, const be_node_factory_t, irn_ops);
194 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
196 for(i = 0; i < bo->n_pos; ++i)
197 res += (bo->pos[i] ^ in_out) > 0;
203 be_node_set_irn_reg(const arch_irn_ops_t *_self, ir_node *irn,
204 int idx, const arch_register_t *reg)
206 const arch_register_t **regs;
208 idx = redir_proj((const ir_node **) &irn, idx);
209 regs = (const arch_register_t **) &irn->attr;
213 const arch_register_t *
214 be_node_get_irn_reg(const arch_irn_ops_t *_self, const ir_node *irn, int idx)
216 const arch_register_t **regs;
218 idx = redir_proj(&irn, idx);
219 regs = (const arch_register_t **) &irn->attr;
223 arch_irn_class_t be_node_classify(const arch_irn_ops_t *_self, const ir_node *irn)
225 const be_node_factory_t *factory =
226 container_of(_self, const be_node_factory_t, irn_ops);
231 idx = redir_proj(&irn, 0);
232 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
237 static const arch_irn_ops_t *
238 be_node_get_irn_ops(const arch_irn_handler_t *_self, const ir_node *irn)
241 const be_node_factory_t *factory =
242 container_of(_self, const be_node_factory_t, handler);
245 bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
247 return bo ? &factory->irn_ops : NULL;
250 const arch_irn_handler_t *be_node_get_irn_handler(const be_node_factory_t *f)
255 int is_Spill(const be_node_factory_t *f, const ir_node *irn)
258 bo = pmap_get(f->irn_op_map, get_irn_op(irn));
259 return bo->kind == node_kind_spill;
262 be_node_factory_t *be_new_node_factory(const arch_isa_if_t *isa)
264 be_node_factory_t *factory;
268 factory = malloc(sizeof(*factory));
269 factory->ops = new_set(cmp_op_map, 64);
270 factory->irn_op_map = pmap_create();
271 obstack_init(&factory->obst);
273 factory->handler.get_irn_ops = be_node_get_irn_ops;
275 factory->irn_ops.get_irn_reg_req = be_node_get_irn_reg_req;
276 factory->irn_ops.get_n_operands = be_node_get_n_operands;
277 factory->irn_ops.set_irn_reg = be_node_set_irn_reg;
278 factory->irn_ops.get_irn_reg = be_node_get_irn_reg;
279 factory->irn_ops.classify = be_node_classify;
281 for(i = 0, n = isa->get_n_reg_class(); i < n; ++i) {
282 const arch_register_class_t *cls = isa->get_reg_class(i);
285 ent = get_op(factory, cls, node_kind_spill);
286 snprintf(buf, sizeof(buf), "Spill_%s", cls->name);
287 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned,
288 0, 0, oparity_unary, 0);
289 ent->n_pos = ARRSIZE(templ_pos_Spill);
290 ent->pos = templ_pos_Spill;
291 pmap_insert(factory->irn_op_map, ent->op, ent);
293 ent = get_op(factory, cls, node_kind_reload);
294 snprintf(buf, sizeof(buf), "Reload_%s", cls->name);
295 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
296 oparity_unary, sizeof(const arch_register_t *));
297 ent->n_pos = ARRSIZE(templ_pos_Reload);
298 ent->pos = templ_pos_Reload;
299 pmap_insert(factory->irn_op_map, ent->op, ent);
301 ent = get_op(factory, cls, node_kind_copy);
302 snprintf(buf, sizeof(buf), "Copy_%s", cls->name);
303 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
304 oparity_unary, sizeof(const arch_register_t *));
305 ent->n_pos = ARRSIZE(templ_pos_Copy);
306 ent->pos = templ_pos_Copy;
307 pmap_insert(factory->irn_op_map, ent->op, ent);
309 ent = get_op(factory, cls, node_kind_perm);
310 snprintf(buf, sizeof(buf), "Perm_%s", cls->name);
311 ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
312 oparity_variable, sizeof(const arch_register_t) * cls->n_regs);
313 ent->n_pos = 2 * cls->n_regs;
314 ent->pos = obstack_alloc(&factory->obst, sizeof(ent->pos[0]) * ent->n_pos);
315 for(j = 0; j < ent->n_pos; j += 2) {
316 ent->pos[j] = arch_pos_make_in(j);
317 ent->pos[j + 1] = arch_pos_make_out(j);
319 pmap_insert(factory->irn_op_map, ent->op, ent);
327 ir_node *be_spill(const be_node_factory_t *factory,
328 const arch_env_t *env, ir_node *node_to_spill)
331 if(is_Reload(node_to_spill))
332 res = get_irn_n(node_to_spill, 0);
334 ir_node *bl = get_nodes_block(node_to_spill);
335 ir_graph *irg = get_irn_irg(bl);
337 res = new_Spill(factory, cls, irg, bl, node_to_spill);
343 ir_node *be_reload(const be_node_factory_t *factory,
344 const arch_env_t *env, ir_node *spill)