b7a37494d18b4533ccd8d746c37d1ed58c76d1b1
[libfirm] / ir / be / benode.c
1 /**
2  * @file   benode.c
3  * @date   17.05.2005
4  * @author Sebastian Hack
5  *
6  * Backend node support.
7  *
8  * Copyright (C) 2005 Universitaet Karlsruhe
9  * Released under the GPL
10  */
11
12 #include <stdlib.h>
13
14 #include "obst.h"
15 #include "set.h"
16 #include "pmap.h"
17 #include "offset.h"
18
19 #include "irop_t.h"
20 #include "irmode_t.h"
21 #include "irnode_t.h"
22
23 #include "benode_t.h"
24
25 struct _be_node_factory_t {
26   const arch_isa_if_t *isa;
27
28   struct obstack      obst;
29   set                 *ops;
30   pmap                *irn_op_map;
31   pmap                *reg_req_map;
32
33   arch_irn_handler_t  handler;
34   arch_irn_ops_t      irn_ops;
35 };
36
37 typedef enum _node_kind_t {
38   node_kind_spill,
39   node_kind_reload,
40   node_kind_perm,
41   node_kind_copy,
42   node_kind_last
43 } node_kind_t;
44
45 typedef struct {
46   node_kind_t kind;
47   const arch_register_class_t *cls;
48   ir_op *op;
49   int n_pos;
50   int *pos;
51 } be_op_t;
52
53 static int templ_pos_Spill[] = {
54   arch_pos_make_in(0)
55 };
56
57 static int templ_pos_Reload[] = {
58   arch_pos_make_out(0)
59 };
60
61 static int templ_pos_Copy[] = {
62   arch_pos_make_in(0),
63   arch_pos_make_out(0)
64 };
65
66 #define ARRSIZE(x) (sizeof(x) / sizeof(x[0]))
67
68 static int cmp_op_map(const void *a, const void *b, size_t size)
69 {
70   const be_op_t *x = a;
71   const be_op_t *y = b;
72
73   return !(x->kind == y->kind && x->cls == y->cls);
74 }
75
76 static be_op_t *get_op(const be_node_factory_t *fact,
77     const arch_register_class_t *cls, node_kind_t kind)
78 {
79   be_op_t templ;
80
81   templ.kind = kind;
82   templ.cls = cls;
83
84   return set_insert(fact->ops, &templ, sizeof(templ),
85       HASH_PTR(cls) + 7 * kind);
86 }
87
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)
91 {
92   ir_node *in[1];
93   ir_op *op = get_op(factory, cls, node_kind_spill)->op;
94
95   assert(op && "Spill opcode must be present for this register class");
96   in[0] = node_to_spill;
97
98   return new_ir_node(NULL, irg, bl, op, mode_M, 1, in);
99 }
100
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)
104 {
105   ir_mode *mode;
106   ir_node *in[1];
107   ir_op *op = get_op(factory, cls, node_kind_reload)->op;
108
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");
111   in[0] = spill_node;
112   mode = get_irn_mode(get_irn_n(spill_node, 0));
113
114   return new_ir_node(NULL, irg, bl, op, mode, 1, in);
115 }
116
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)
120 {
121   ir_op *op = get_op(factory, cls, node_kind_perm)->op;
122
123   return new_ir_node(NULL, irg, bl, op, mode_T, arity, in);
124 }
125
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)
129 {
130   ir_node *ins[1];
131   ir_op *op = get_op(factory, cls, node_kind_perm)->op;
132
133   ins[0] = in;
134
135   return new_ir_node(NULL, irg, bl, op, get_irn_mode(in), 1, ins);
136 }
137
138 /**
139  * If the node is a proj, reset the node to the proj's target and return
140  * the proj number.
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
145  *             is returned.
146  */
147 static int redir_proj(const ir_node **node, int def)
148 {
149   const ir_node *n = *node;
150
151   if(is_Proj(n)) {
152     *node = get_Proj_pred(n);
153     def = get_Proj_proj(n);
154   }
155
156   return def;
157 }
158
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)
162 {
163   be_op_t *bo;
164   const be_node_factory_t *factory =
165     container_of(_self, const be_node_factory_t, irn_ops);
166
167   pos = arch_pos_make_out(redir_proj(&irn, arch_pos_get_index(pos)));
168
169   bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
170
171   if(bo) {
172     int i;
173
174     req->type = arch_register_req_type_normal;
175     req->cls = bo->cls;
176
177     for(i = 0; i < bo->n_pos; ++pos)
178       if(pos == bo->pos[i])
179         return req;
180   }
181
182   return NULL;
183 }
184
185 static int
186 be_node_get_n_operands(const arch_irn_ops_t *_self, const ir_node *irn, int in_out)
187 {
188   be_op_t *bo;
189   int i, res = 0;
190   const be_node_factory_t *factory =
191     container_of(_self, const be_node_factory_t, irn_ops);
192
193   redir_proj(&irn, 0);
194   bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
195
196   for(i = 0; i < bo->n_pos; ++i)
197     res += (bo->pos[i] ^ in_out) > 0;
198
199   return res;
200 }
201
202 void
203 be_node_set_irn_reg(const arch_irn_ops_t *_self, ir_node *irn,
204     int idx, const arch_register_t *reg)
205 {
206   const arch_register_t **regs;
207
208   idx = redir_proj((const ir_node **) &irn, idx);
209   regs = (const arch_register_t **) &irn->attr;
210   regs[idx] = reg;
211 }
212
213 const arch_register_t *
214 be_node_get_irn_reg(const arch_irn_ops_t *_self, const ir_node *irn, int idx)
215 {
216   const arch_register_t **regs;
217
218   idx = redir_proj(&irn, idx);
219   regs = (const arch_register_t **) &irn->attr;
220   return regs[idx];
221 }
222
223 arch_irn_class_t be_node_classify(const arch_irn_ops_t *_self, const ir_node *irn)
224 {
225   const be_node_factory_t *factory =
226     container_of(_self, const be_node_factory_t, irn_ops);
227
228   be_op_t *bo;
229   int idx;
230
231   idx = redir_proj(&irn, 0);
232   bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
233
234   return 0;
235 }
236
237 static const arch_irn_ops_t *
238 be_node_get_irn_ops(const arch_irn_handler_t *_self, const ir_node *irn)
239 {
240   be_op_t *bo;
241   const be_node_factory_t *factory =
242     container_of(_self, const be_node_factory_t, handler);
243
244   redir_proj(&irn, 0);
245   bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
246
247   return bo ? &factory->irn_ops : NULL;
248 }
249
250 const arch_irn_handler_t *be_node_get_irn_handler(const be_node_factory_t *f)
251 {
252   return &f->handler;
253 }
254
255 int is_Spill(const be_node_factory_t *f, const ir_node *irn)
256 {
257   be_op_t *bo;
258   bo = pmap_get(f->irn_op_map, get_irn_op(irn));
259   return bo->kind == node_kind_spill;
260 }
261
262 be_node_factory_t *be_new_node_factory(const arch_isa_if_t *isa)
263 {
264   be_node_factory_t *factory;
265   char buf[256];
266   int i, j, n;
267
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);
272
273   factory->handler.get_irn_ops = be_node_get_irn_ops;
274
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;
280
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);
283     be_op_t *ent;
284
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);
292
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);
300
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);
308
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);
318     }
319     pmap_insert(factory->irn_op_map, ent->op, ent);
320
321   }
322
323   return factory;
324 }
325
326 #if 0
327 ir_node *be_spill(const be_node_factory_t *factory,
328     const arch_env_t *env, ir_node *node_to_spill)
329 {
330   ir_node *res;
331   if(is_Reload(node_to_spill))
332     res = get_irn_n(node_to_spill, 0);
333   else {
334     ir_node *bl = get_nodes_block(node_to_spill);
335     ir_graph *irg = get_irn_irg(bl);
336
337     res = new_Spill(factory, cls, irg, bl, node_to_spill);
338   }
339
340   return res;
341 }
342
343 ir_node *be_reload(const be_node_factory_t *factory,
344     const arch_env_t *env, ir_node *spill)
345 {
346 }
347 #endif