27c359b9abb28457e23a7473691d56015a37ca49
[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 "util.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 typedef enum _node_kind_t {
26   node_kind_spill,
27   node_kind_reload,
28   node_kind_perm,
29   node_kind_copy,
30   node_kind_last
31 } node_kind_t;
32
33 typedef struct {
34   node_kind_t kind;
35   const arch_register_class_t *cls;
36   ir_op *op;
37   int n_pos;
38   int *pos;
39 } be_op_t;
40
41 static int templ_pos_Spill[] = {
42   arch_pos_make_in(0)
43 };
44
45 static int templ_pos_Reload[] = {
46   arch_pos_make_out(0)
47 };
48
49 static int templ_pos_Copy[] = {
50   arch_pos_make_in(0),
51   arch_pos_make_out(0)
52 };
53
54 #define ARRSIZE(x) (sizeof(x) / sizeof(x[0]))
55
56 static int cmp_op_map(const void *a, const void *b, size_t size)
57 {
58   const be_op_t *x = a;
59   const be_op_t *y = b;
60
61   return !(x->kind == y->kind && x->cls == y->cls);
62 }
63
64 static be_op_t *get_op(const be_node_factory_t *fact,
65     const arch_register_class_t *cls, node_kind_t kind)
66 {
67   be_op_t templ;
68
69   templ.kind = kind;
70   templ.cls = cls;
71
72   return set_insert(fact->ops, &templ, sizeof(templ),
73       HASH_PTR(cls) + 7 * kind);
74 }
75
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)
79 {
80   ir_node *in[1];
81   ir_op *op = get_op(factory, cls, node_kind_spill)->op;
82
83   assert(op && "Spill opcode must be present for this register class");
84   in[0] = node_to_spill;
85
86   return new_ir_node(NULL, irg, bl, op, mode_M, 1, in);
87 }
88
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)
92 {
93   ir_mode *mode;
94   ir_node *in[1];
95   ir_op *op = get_op(factory, cls, node_kind_reload)->op;
96
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");
99   in[0] = spill_node;
100   mode = get_irn_mode(get_irn_n(spill_node, 0));
101
102   return new_ir_node(NULL, irg, bl, op, mode, 1, in);
103 }
104
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)
108 {
109   ir_op *op = get_op(factory, cls, node_kind_perm)->op;
110
111   return new_ir_node(NULL, irg, bl, op, mode_T, arity, in);
112 }
113
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)
117 {
118   ir_node *ins[1];
119   ir_op *op = get_op(factory, cls, node_kind_perm)->op;
120
121   ins[0] = in;
122
123   return new_ir_node(NULL, irg, bl, op, get_irn_mode(in), 1, ins);
124 }
125
126 /**
127  * If the node is a proj, reset the node to the proj's target and return
128  * the proj number.
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
133  *             is returned.
134  */
135 static int redir_proj(const ir_node **node, int def)
136 {
137   const ir_node *n = *node;
138
139   if(is_Proj(n)) {
140     *node = get_Proj_pred(n);
141     def = get_Proj_proj(n);
142   }
143
144   return def;
145 }
146
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)
150 {
151   be_op_t *bo;
152   const be_node_factory_t *factory =
153     container_of(_self, const be_node_factory_t, irn_ops);
154
155   pos = arch_pos_make_out(redir_proj(&irn, arch_pos_get_index(pos)));
156
157   bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
158
159   if(bo) {
160     int i;
161
162     req->type = arch_register_req_type_normal;
163     req->cls = bo->cls;
164
165     for(i = 0; i < bo->n_pos; ++pos)
166       if(pos == bo->pos[i])
167         return req;
168   }
169
170   return NULL;
171 }
172
173 static int
174 be_node_get_n_operands(const arch_irn_ops_t *_self, const ir_node *irn, int in_out)
175 {
176   be_op_t *bo;
177   int i, res = 0;
178   const be_node_factory_t *factory =
179     container_of(_self, const be_node_factory_t, irn_ops);
180
181   redir_proj(&irn, 0);
182   bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
183
184   for(i = 0; i < bo->n_pos; ++i)
185     res += (bo->pos[i] ^ in_out) > 0;
186
187   return res;
188 }
189
190 void
191 be_node_set_irn_reg(const arch_irn_ops_t *_self, ir_node *irn,
192     int idx, const arch_register_t *reg)
193 {
194   const arch_register_t **regs;
195
196   idx = redir_proj((const ir_node **) &irn, idx);
197   regs = (const arch_register_t **) &irn->attr;
198   regs[idx] = reg;
199 }
200
201 const arch_register_t *
202 be_node_get_irn_reg(const arch_irn_ops_t *_self, const ir_node *irn, int idx)
203 {
204   const arch_register_t **regs;
205
206   idx = redir_proj(&irn, idx);
207   regs = (const arch_register_t **) &irn->attr;
208   return regs[idx];
209 }
210
211 arch_irn_class_t be_node_classify(const arch_irn_ops_t *_self, const ir_node *irn)
212 {
213   const be_node_factory_t *factory =
214     container_of(_self, const be_node_factory_t, irn_ops);
215
216   be_op_t *bo;
217   int idx;
218
219   idx = redir_proj(&irn, 0);
220   bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
221
222   return 0;
223 }
224
225 static const arch_irn_ops_t *
226 be_node_get_irn_ops(const arch_irn_handler_t *_self, const ir_node *irn)
227 {
228   be_op_t *bo;
229   const be_node_factory_t *factory =
230     container_of(_self, const be_node_factory_t, handler);
231
232   redir_proj(&irn, 0);
233   bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
234
235   return bo ? &factory->irn_ops : NULL;
236 }
237
238 const arch_irn_handler_t *be_node_get_irn_handler(const be_node_factory_t *f)
239 {
240   return &f->handler;
241 }
242
243 int is_Spill(const be_node_factory_t *f, const ir_node *irn)
244 {
245   be_op_t *bo;
246   bo = pmap_get(f->irn_op_map, get_irn_op(irn));
247   return bo->kind == node_kind_spill;
248 }
249
250 be_node_factory_t *be_node_factory_init(be_node_factory_t *factory,
251     const arch_isa_if_t *isa)
252 {
253   char buf[256];
254   int i, j, n;
255
256   factory->ops = new_set(cmp_op_map, 64);
257   factory->irn_op_map = pmap_create();
258   obstack_init(&factory->obst);
259
260   factory->handler.get_irn_ops = be_node_get_irn_ops;
261
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;
267
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);
270     be_op_t *ent;
271
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);
279
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);
287
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);
295
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);
305     }
306     pmap_insert(factory->irn_op_map, ent->op, ent);
307
308   }
309
310   return factory;
311 }
312
313 #if 0
314 ir_node *be_spill(const be_node_factory_t *factory,
315     const arch_env_t *env, ir_node *node_to_spill)
316 {
317   ir_node *res;
318   if(is_Reload(node_to_spill))
319     res = get_irn_n(node_to_spill, 0);
320   else {
321     ir_node *bl = get_nodes_block(node_to_spill);
322     ir_graph *irg = get_irn_irg(bl);
323
324     res = new_Spill(factory, cls, irg, bl, node_to_spill);
325   }
326
327   return res;
328 }
329
330 ir_node *be_reload(const be_node_factory_t *factory,
331     const arch_env_t *env, ir_node *spill)
332 {
333 }
334 #endif