debug stuff and bugfixes
[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 #define DBG_LEVEL 0
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, ir_graph *irg,
103     ir_node *bl, ir_mode *mode, ir_node *spill_node)
104 {
105   ir_node *in[1];
106   ir_op *op = get_op(factory, cls, node_kind_reload)->op;
107
108   assert(op && "Reload opcode must be present for this register class");
109   // assert(is_Spill(factory, spill_node) && "Operand of Reload must be a Spill");
110   in[0] = spill_node;
111
112   return new_ir_node(NULL, irg, bl, op, mode, 1, in);
113 }
114
115 ir_node *new_Perm(const be_node_factory_t *factory,
116     const arch_register_class_t *cls,
117     ir_graph *irg, ir_node *bl, int arity, ir_node **in)
118 {
119   ir_op *op = get_op(factory, cls, node_kind_perm)->op;
120
121   return new_ir_node(NULL, irg, bl, op, mode_T, arity, in);
122 }
123
124 ir_node *new_Copy(const be_node_factory_t *factory,
125     const arch_register_class_t *cls,
126     ir_graph *irg, ir_node *bl, ir_node *in)
127 {
128   ir_node *ins[1];
129   ir_op *op = get_op(factory, cls, node_kind_copy)->op;
130
131   ins[0] = in;
132
133   return new_ir_node(NULL, irg, bl, op, get_irn_mode(in), 1, ins);
134 }
135
136 /**
137  * If the node is a proj, reset the node to the proj's target and return
138  * the proj number.
139  * @param node The address of a node pointer.
140  * @param def  A default value.
141  * @return     If *node is a Proj, *node is set to the Proj's target and
142  *             the Proj number is returned. Else *node remains the same and @p def
143  *             is returned.
144  */
145 static int redir_proj(const ir_node **node, int def)
146 {
147   const ir_node *n = *node;
148
149   if(is_Proj(n)) {
150     *node = get_Proj_pred(n);
151     def = get_Proj_proj(n);
152   }
153
154   return def;
155 }
156
157 static const arch_register_req_t *
158 be_node_get_irn_reg_req(const arch_irn_ops_t *_self,
159     arch_register_req_t *req, const ir_node *irn, int pos)
160 {
161   be_op_t *bo;
162   const be_node_factory_t *factory =
163     container_of(_self, const be_node_factory_t, irn_ops);
164
165   pos = arch_pos_make_out(redir_proj(&irn, arch_pos_get_index(pos)));
166
167   bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
168
169   if(bo) {
170     int i;
171
172     req->type = arch_register_req_type_normal;
173     req->cls = bo->cls;
174
175     for(i = 0; i < bo->n_pos; ++pos)
176       if(pos == bo->pos[i])
177         return req;
178   }
179
180   return NULL;
181 }
182
183 static int
184 be_node_get_n_operands(const arch_irn_ops_t *_self, const ir_node *irn, int in_out)
185 {
186   be_op_t *bo;
187   int i, res = 0;
188   const be_node_factory_t *factory =
189     container_of(_self, const be_node_factory_t, irn_ops);
190
191   redir_proj(&irn, 0);
192   bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
193
194   for(i = 0; i < bo->n_pos; ++i)
195     res += (bo->pos[i] ^ in_out) > 0;
196
197   return res;
198 }
199
200 void
201 be_node_set_irn_reg(const arch_irn_ops_t *_self, ir_node *irn,
202     int idx, const arch_register_t *reg)
203 {
204   const arch_register_t **regs;
205
206   idx = redir_proj((const ir_node **) &irn, idx);
207   regs = (const arch_register_t **) &irn->attr;
208   regs[idx] = reg;
209 }
210
211 const arch_register_t *
212 be_node_get_irn_reg(const arch_irn_ops_t *_self, const ir_node *irn, int idx)
213 {
214   const arch_register_t **regs;
215
216   idx = redir_proj(&irn, idx);
217   regs = (const arch_register_t **) &irn->attr;
218   return regs[idx];
219 }
220
221 arch_irn_class_t be_node_classify(const arch_irn_ops_t *_self, const ir_node *irn)
222 {
223   const be_node_factory_t *factory =
224     container_of(_self, const be_node_factory_t, irn_ops);
225
226   be_op_t *bo;
227   int idx;
228
229   idx = redir_proj(&irn, 0);
230   bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
231
232   return 0;
233 }
234
235 static const arch_irn_ops_t *
236 be_node_get_irn_ops(const arch_irn_handler_t *_self, const ir_node *irn)
237 {
238   be_op_t *bo;
239   const be_node_factory_t *factory =
240     container_of(_self, const be_node_factory_t, handler);
241
242   redir_proj(&irn, 0);
243   bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
244
245   return bo ? &factory->irn_ops : NULL;
246 }
247
248 const arch_irn_handler_t *be_node_get_irn_handler(const be_node_factory_t *f)
249 {
250   return &f->handler;
251 }
252
253 int is_Spill(const be_node_factory_t *f, const ir_node *irn)
254 {
255   be_op_t *bo;
256   bo = pmap_get(f->irn_op_map, get_irn_op(irn));
257   return bo->kind == node_kind_spill;
258 }
259
260 be_node_factory_t *be_node_factory_init(be_node_factory_t *factory,
261     const arch_isa_if_t *isa)
262 {
263   char buf[256];
264   int i, j, n;
265
266   factory->ops = new_set(cmp_op_map, 64);
267   factory->irn_op_map = pmap_create();
268   obstack_init(&factory->obst);
269
270   factory->handler.get_irn_ops = be_node_get_irn_ops;
271
272   factory->irn_ops.get_irn_reg_req = be_node_get_irn_reg_req;
273   factory->irn_ops.get_n_operands  = be_node_get_n_operands;
274   factory->irn_ops.set_irn_reg     = be_node_set_irn_reg;
275   factory->irn_ops.get_irn_reg     = be_node_get_irn_reg;
276   factory->irn_ops.classify        = be_node_classify;
277
278   for(i = 0, n = isa->get_n_reg_class(); i < n; ++i) {
279     const arch_register_class_t *cls = isa->get_reg_class(i);
280     be_op_t *ent;
281
282     ent = get_op(factory, cls, node_kind_spill);
283     snprintf(buf, sizeof(buf), "Spill_%s", cls->name);
284     ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned,
285         0, 0, oparity_unary, 0);
286     ent->n_pos = ARRSIZE(templ_pos_Spill);
287     ent->pos = templ_pos_Spill;
288     pmap_insert(factory->irn_op_map, ent->op, ent);
289
290     ent = get_op(factory, cls, node_kind_reload);
291     snprintf(buf, sizeof(buf), "Reload_%s", cls->name);
292     ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
293         oparity_unary, sizeof(const arch_register_t *));
294     ent->n_pos = ARRSIZE(templ_pos_Reload);
295     ent->pos = templ_pos_Reload;
296     pmap_insert(factory->irn_op_map, ent->op, ent);
297
298     ent = get_op(factory, cls, node_kind_copy);
299     snprintf(buf, sizeof(buf), "Copy_%s", cls->name);
300     ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
301         oparity_unary, sizeof(const arch_register_t *));
302     ent->n_pos = ARRSIZE(templ_pos_Copy);
303     ent->pos = templ_pos_Copy;
304     pmap_insert(factory->irn_op_map, ent->op, ent);
305
306     ent = get_op(factory, cls, node_kind_perm);
307     snprintf(buf, sizeof(buf), "Perm_%s", cls->name);
308     ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
309         oparity_variable, sizeof(const arch_register_t) * cls->n_regs);
310     ent->n_pos = 2 * cls->n_regs;
311     ent->pos = obstack_alloc(&factory->obst, sizeof(ent->pos[0]) * ent->n_pos);
312     for(j = 0; j < ent->n_pos; j += 2) {
313       ent->pos[j] = arch_pos_make_in(j);
314       ent->pos[j + 1] = arch_pos_make_out(j);
315     }
316     pmap_insert(factory->irn_op_map, ent->op, ent);
317
318   }
319
320   return factory;
321 }
322
323 ir_node *insert_Perm_after(const be_main_session_env_t *env,
324     const arch_register_class_t *cls, ir_node *pos)
325 {
326   const arch_env_t *arch_env = env->main_env->arch_env;
327   ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
328   ir_graph *irg = get_irn_irg(bl);
329   pset *live = put_live_end(bl, pset_new_ptr_default());
330   ir_node *curr, *irn, *perm, **nodes;
331   firm_dbg_module_t *dbg = firm_dbg_register("firm.be.node");
332   int i, n;
333
334   firm_dbg_set_mask(dbg, DBG_LEVEL);
335   DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
336
337   sched_foreach_reverse(bl, irn) {
338     ir_node *x;
339
340         /*
341          * If we encounter the node we want to insert the Perm after,
342          * exit immediately, so that this node is still live
343          */
344     if(irn == pos)
345       break;
346
347     DBG((dbg, LEVEL_1, "%+F\n", irn));
348     for(x = pset_first(live); x; x = pset_next(live))
349       DBG((dbg, LEVEL_1, "\tlive: %+F\n", x));
350
351     if(arch_irn_has_reg_class(arch_env, irn, arch_pos_make_out(0), cls))
352       pset_remove_ptr(live, irn);
353
354     for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
355       ir_node *op = get_irn_n(irn, i);
356
357       if(arch_irn_has_reg_class(arch_env, op, arch_pos_make_out(0), cls))
358         pset_insert_ptr(live, op);
359     }
360   }
361
362   n = pset_count(live);
363   nodes = malloc(n * sizeof(nodes[0]));
364
365   DBG((dbg, LEVEL_1, "live:\n"));
366   for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++) {
367         DBG((dbg, LEVEL_1, "\t%+F\n", irn));
368     nodes[i] = irn;
369   }
370
371   perm = new_Perm(env->main_env->node_factory, cls, irg, bl, n, nodes);
372   sched_add_after(pos, perm);
373   free(nodes);
374
375   curr = perm;
376   for(i = 0; i < n; ++i) {
377     ir_node *copies[1];
378     ir_node *perm_op = get_irn_n(perm, i);
379         const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op, 0);
380
381     ir_mode *mode = get_irn_mode(perm_op);
382     ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
383     arch_set_irn_register(arch_env, proj, 0, reg);
384
385     sched_add_after(curr, proj);
386     curr = proj;
387
388     copies[0] = proj;
389     be_introduce_copies(env->dom_front, perm_op, array_size(copies), copies);
390   }
391   return perm;
392 }