26cef9e3b1f145fbaccd8f3acdd2c9576e0af2c9
[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 #ifdef HAVE_CONFIG_H
12 #include "config.h"
13 #endif
14
15 #include <stdlib.h>
16
17 #include "obst.h"
18 #include "set.h"
19 #include "pmap.h"
20 #include "util.h"
21 #include "debug.h"
22
23 #include "irop_t.h"
24 #include "irmode_t.h"
25 #include "irnode_t.h"
26 #include "ircons_t.h"
27
28 #include "be_t.h"
29 #include "belive_t.h"
30 #include "besched_t.h"
31 #include "benode_t.h"
32
33 #include "beirgmod.h"
34
35 typedef enum _node_kind_t {
36   node_kind_spill,
37   node_kind_reload,
38   node_kind_perm,
39   node_kind_copy,
40   node_kind_last
41 } node_kind_t;
42
43 typedef struct {
44   node_kind_t kind;
45   const arch_register_class_t *cls;
46   ir_op *op;
47   int n_pos;
48   int *pos;
49 } be_op_t;
50
51 static int templ_pos_Spill[] = {
52   arch_pos_make_in(0)
53 };
54
55 static int templ_pos_Reload[] = {
56   arch_pos_make_out(0)
57 };
58
59 static int templ_pos_Copy[] = {
60   arch_pos_make_in(0),
61   arch_pos_make_out(0)
62 };
63
64 #define ARRSIZE(x) (sizeof(x) / sizeof(x[0]))
65
66 static int cmp_op_map(const void *a, const void *b, size_t size)
67 {
68   const be_op_t *x = a;
69   const be_op_t *y = b;
70
71   return !(x->kind == y->kind && x->cls == y->cls);
72 }
73
74 static be_op_t *get_op(const be_node_factory_t *fact,
75     const arch_register_class_t *cls, node_kind_t kind)
76 {
77   be_op_t templ;
78
79   templ.kind = kind;
80   templ.cls = cls;
81
82   return set_insert(fact->ops, &templ, sizeof(templ),
83       HASH_PTR(cls) + 7 * kind);
84 }
85
86 ir_node *new_Spill(const be_node_factory_t *factory,
87     const arch_register_class_t *cls,
88     ir_graph *irg, ir_node *bl, ir_node *node_to_spill)
89 {
90   ir_node *in[1];
91   ir_op *op = get_op(factory, cls, node_kind_spill)->op;
92
93   assert(op && "Spill opcode must be present for this register class");
94   in[0] = node_to_spill;
95
96   return new_ir_node(NULL, irg, bl, op, mode_M, 1, in);
97 }
98
99 ir_node *new_Reload(const be_node_factory_t *factory,
100     const arch_register_class_t *cls, ir_graph *irg,
101     ir_node *bl, ir_mode *mode, ir_node *spill_node)
102 {
103   ir_node *in[1];
104   ir_op *op = get_op(factory, cls, node_kind_reload)->op;
105
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");
108   in[0] = spill_node;
109
110   return new_ir_node(NULL, irg, bl, op, mode, 1, in);
111 }
112
113 ir_node *new_Perm(const be_node_factory_t *factory,
114     const arch_register_class_t *cls,
115     ir_graph *irg, ir_node *bl, int arity, ir_node **in)
116 {
117   ir_op *op = get_op(factory, cls, node_kind_perm)->op;
118
119   return new_ir_node(NULL, irg, bl, op, mode_T, arity, in);
120 }
121
122 ir_node *new_Copy(const be_node_factory_t *factory,
123     const arch_register_class_t *cls,
124     ir_graph *irg, ir_node *bl, ir_node *in)
125 {
126   ir_node *ins[1];
127   ir_op *op = get_op(factory, cls, node_kind_copy)->op;
128
129   ins[0] = in;
130
131   return new_ir_node(NULL, irg, bl, op, get_irn_mode(in), 1, ins);
132 }
133
134 /**
135  * If the node is a proj, reset the node to the proj's target and return
136  * the proj number.
137  * @param node The address of a node pointer.
138  * @param def  A default value.
139  * @return     If *node is a Proj, *node is set to the Proj's target and
140  *             the Proj number is returned. Else *node remains the same and @p def
141  *             is returned.
142  */
143 static int redir_proj(const ir_node **node, int def)
144 {
145   const ir_node *n = *node;
146
147   if(is_Proj(n)) {
148     *node = get_Proj_pred(n);
149     def = get_Proj_proj(n);
150   }
151
152   return def;
153 }
154
155 static const arch_register_req_t *
156 be_node_get_irn_reg_req(const arch_irn_ops_t *_self,
157     arch_register_req_t *req, const ir_node *irn, int pos)
158 {
159   be_op_t *bo;
160   const be_node_factory_t *factory =
161     container_of(_self, const be_node_factory_t, irn_ops);
162
163   pos = arch_pos_make_out(redir_proj(&irn, arch_pos_get_index(pos)));
164
165   bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
166
167   if(bo) {
168     int i;
169
170     req->type = arch_register_req_type_normal;
171     req->cls = bo->cls;
172
173     for(i = 0; i < bo->n_pos; ++pos)
174       if(pos == bo->pos[i])
175         return req;
176   }
177
178   return NULL;
179 }
180
181 static int
182 be_node_get_n_operands(const arch_irn_ops_t *_self, const ir_node *irn, int in_out)
183 {
184   be_op_t *bo;
185   int i, res = 0;
186   const be_node_factory_t *factory =
187     container_of(_self, const be_node_factory_t, irn_ops);
188
189   redir_proj(&irn, 0);
190   bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
191
192   for(i = 0; i < bo->n_pos; ++i)
193     res += (bo->pos[i] ^ in_out) > 0;
194
195   return res;
196 }
197
198 void
199 be_node_set_irn_reg(const arch_irn_ops_t *_self, ir_node *irn,
200     int idx, const arch_register_t *reg)
201 {
202   const arch_register_t **regs;
203
204   idx = redir_proj((const ir_node **) &irn, idx);
205   regs = (const arch_register_t **) &irn->attr;
206   regs[idx] = reg;
207 }
208
209 const arch_register_t *
210 be_node_get_irn_reg(const arch_irn_ops_t *_self, const ir_node *irn, int idx)
211 {
212   const arch_register_t **regs;
213
214   idx = redir_proj(&irn, idx);
215   regs = (const arch_register_t **) &irn->attr;
216   return regs[idx];
217 }
218
219 arch_irn_class_t be_node_classify(const arch_irn_ops_t *_self, const ir_node *irn)
220 {
221   const be_node_factory_t *factory =
222     container_of(_self, const be_node_factory_t, irn_ops);
223
224   be_op_t *bo;
225   int idx;
226
227   idx = redir_proj(&irn, 0);
228   bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
229
230   return 0;
231 }
232
233 static const arch_irn_ops_t *
234 be_node_get_irn_ops(const arch_irn_handler_t *_self, const ir_node *irn)
235 {
236   be_op_t *bo;
237   const be_node_factory_t *factory =
238     container_of(_self, const be_node_factory_t, handler);
239
240   redir_proj(&irn, 0);
241   bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
242
243   return bo ? &factory->irn_ops : NULL;
244 }
245
246 const arch_irn_handler_t *be_node_get_irn_handler(const be_node_factory_t *f)
247 {
248   return &f->handler;
249 }
250
251 int is_Spill(const be_node_factory_t *f, const ir_node *irn)
252 {
253   be_op_t *bo;
254   bo = pmap_get(f->irn_op_map, get_irn_op(irn));
255   return bo->kind == node_kind_spill;
256 }
257
258 be_node_factory_t *be_node_factory_init(be_node_factory_t *factory,
259     const arch_isa_if_t *isa)
260 {
261   char buf[256];
262   int i, j, n;
263
264   factory->ops = new_set(cmp_op_map, 64);
265   factory->irn_op_map = pmap_create();
266   obstack_init(&factory->obst);
267
268   factory->handler.get_irn_ops = be_node_get_irn_ops;
269
270   factory->irn_ops.get_irn_reg_req = be_node_get_irn_reg_req;
271   factory->irn_ops.get_n_operands  = be_node_get_n_operands;
272   factory->irn_ops.set_irn_reg     = be_node_set_irn_reg;
273   factory->irn_ops.get_irn_reg     = be_node_get_irn_reg;
274   factory->irn_ops.classify        = be_node_classify;
275
276   for(i = 0, n = isa->get_n_reg_class(); i < n; ++i) {
277     const arch_register_class_t *cls = isa->get_reg_class(i);
278     be_op_t *ent;
279
280     ent = get_op(factory, cls, node_kind_spill);
281     snprintf(buf, sizeof(buf), "Spill_%s", cls->name);
282     ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned,
283         0, 0, oparity_unary, 0);
284     ent->n_pos = ARRSIZE(templ_pos_Spill);
285     ent->pos = templ_pos_Spill;
286     pmap_insert(factory->irn_op_map, ent->op, ent);
287
288     ent = get_op(factory, cls, node_kind_reload);
289     snprintf(buf, sizeof(buf), "Reload_%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_Reload);
293     ent->pos = templ_pos_Reload;
294     pmap_insert(factory->irn_op_map, ent->op, ent);
295
296     ent = get_op(factory, cls, node_kind_copy);
297     snprintf(buf, sizeof(buf), "Copy_%s", cls->name);
298     ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
299         oparity_unary, sizeof(const arch_register_t *));
300     ent->n_pos = ARRSIZE(templ_pos_Copy);
301     ent->pos = templ_pos_Copy;
302     pmap_insert(factory->irn_op_map, ent->op, ent);
303
304     ent = get_op(factory, cls, node_kind_perm);
305     snprintf(buf, sizeof(buf), "Perm_%s", cls->name);
306     ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
307         oparity_variable, sizeof(const arch_register_t) * cls->n_regs);
308     ent->n_pos = 2 * cls->n_regs;
309     ent->pos = obstack_alloc(&factory->obst, sizeof(ent->pos[0]) * ent->n_pos);
310     for(j = 0; j < ent->n_pos; j += 2) {
311       ent->pos[j] = arch_pos_make_in(j);
312       ent->pos[j + 1] = arch_pos_make_out(j);
313     }
314     pmap_insert(factory->irn_op_map, ent->op, ent);
315
316   }
317
318   return factory;
319 }
320
321 ir_node *insert_Perm_after(const be_main_session_env_t *env,
322     const arch_register_class_t *cls, ir_node *pos)
323 {
324   const arch_env_t *arch_env = env->main_env->arch_env;
325   ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
326   ir_graph *irg = get_irn_irg(bl);
327   pset *live = put_live_end(bl, pset_new_ptr_default());
328   ir_node *curr, *irn, *perm, **nodes;
329   firm_dbg_module_t *dbg = firm_dbg_register("firm.be.node");
330   int i, n;
331
332   firm_dbg_set_mask(dbg, -1);
333   DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
334
335   sched_foreach_reverse(bl, irn) {
336     ir_node *x;
337
338         /*
339          * If we encounter the node we want to insert the Perm after,
340          * exit immediately, so that this node is still live
341          */
342     if(irn == pos)
343       break;
344
345     DBG((dbg, LEVEL_1, "%+F\n", irn));
346     for(x = pset_first(live); x; x = pset_next(live))
347       DBG((dbg, LEVEL_1, "\tlive: %+F\n", x));
348
349     if(arch_irn_has_reg_class(arch_env, irn, arch_pos_make_out(0), cls))
350       pset_remove_ptr(live, irn);
351
352     for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
353       ir_node *op = get_irn_n(irn, i);
354
355       if(arch_irn_has_reg_class(arch_env, op, arch_pos_make_out(0), cls))
356         pset_insert_ptr(live, op);
357     }
358   }
359
360   n = pset_count(live);
361   nodes = malloc(n * sizeof(nodes[0]));
362
363   DBG((dbg, LEVEL_1, "live:\n"));
364   for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++) {
365         DBG((dbg, LEVEL_1, "\t%+F\n", irn));
366     nodes[i] = irn;
367   }
368
369   perm = new_Perm(env->main_env->node_factory, cls, irg, bl, n, nodes);
370   sched_add_after(pos, perm);
371   free(nodes);
372
373   curr = perm;
374   for(i = 0; i < n; ++i) {
375     ir_node *copies[1];
376     ir_node *perm_op = get_irn_n(perm, i);
377         const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op, 0);
378
379     ir_mode *mode = get_irn_mode(perm_op);
380     ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
381     arch_set_irn_register(arch_env, proj, 0, reg);
382
383     sched_add_after(curr, proj);
384     curr = proj;
385
386     copies[0] = proj;
387     be_introduce_copies(env->dom_front, perm_op, array_size(copies), copies);
388   }
389   return perm;
390 }