Modified everything
[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 #if 0
137 ir_node *be_spill(const be_node_factory_t *factory, ir_node *irn)
138 {
139   ir_node *bl = get_nodes_block(irn);
140   ir_graph *irg = get_irn_irg(bl);
141   ir_node *spill =
142     new_Spill(factory, arch_get_irn_reg_class(irn), irg, bl, irn);
143   sched_add_after(irn, spill);
144   return spill;
145 }
146 #endif
147
148
149
150
151 /**
152  * If the node is a proj, reset the node to the proj's target and return
153  * the proj number.
154  * @param node The address of a node pointer.
155  * @param def  A default value.
156  * @return     If *node is a Proj, *node is set to the Proj's target and
157  *             the Proj number is returned. Else *node remains the same and @p def
158  *             is returned.
159  */
160 static int redir_proj(const ir_node **node, int def)
161 {
162   const ir_node *n = *node;
163
164   if(is_Proj(n)) {
165     *node = get_Proj_pred(n);
166     def = get_Proj_proj(n);
167   }
168
169   return def;
170 }
171
172 static const arch_register_req_t *
173 be_node_get_irn_reg_req(const arch_irn_ops_t *_self,
174     arch_register_req_t *req, const ir_node *irn, int pos)
175 {
176   be_op_t *bo;
177   const be_node_factory_t *factory =
178     container_of(_self, const be_node_factory_t, irn_ops);
179
180   pos = arch_pos_make_out(redir_proj(&irn, arch_pos_get_index(pos)));
181
182   bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
183
184   if(bo) {
185     int i;
186
187     req->type = arch_register_req_type_normal;
188     req->cls = bo->cls;
189
190     for(i = 0; i < bo->n_pos; ++pos)
191       if(pos == bo->pos[i])
192         return req;
193   }
194
195   return NULL;
196 }
197
198 static int
199 be_node_get_n_operands(const arch_irn_ops_t *_self, const ir_node *irn, int in_out)
200 {
201   be_op_t *bo;
202   int i, res = 0;
203   const be_node_factory_t *factory =
204     container_of(_self, const be_node_factory_t, irn_ops);
205
206   redir_proj(&irn, 0);
207   bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
208
209   for(i = 0; i < bo->n_pos; ++i)
210     res += (bo->pos[i] ^ in_out) > 0;
211
212   return res;
213 }
214
215 void
216 be_node_set_irn_reg(const arch_irn_ops_t *_self, ir_node *irn,
217     int idx, const arch_register_t *reg)
218 {
219   const arch_register_t **regs;
220
221   idx = redir_proj((const ir_node **) &irn, idx);
222   regs = (const arch_register_t **) &irn->attr;
223   regs[idx] = reg;
224 }
225
226 const arch_register_t *
227 be_node_get_irn_reg(const arch_irn_ops_t *_self, const ir_node *irn, int idx)
228 {
229   const arch_register_t **regs;
230
231   idx = redir_proj(&irn, idx);
232   regs = (const arch_register_t **) &irn->attr;
233   return regs[idx];
234 }
235
236 arch_irn_class_t be_node_classify(const arch_irn_ops_t *_self, const ir_node *irn)
237 {
238   const be_node_factory_t *factory =
239     container_of(_self, const be_node_factory_t, irn_ops);
240
241   be_op_t *bo;
242   int idx;
243
244   idx = redir_proj(&irn, 0);
245   bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
246
247   /* TODO Implement */
248   return 0;
249 }
250
251 static const arch_irn_ops_t *
252 be_node_get_irn_ops(const arch_irn_handler_t *_self, const ir_node *irn)
253 {
254   be_op_t *bo;
255   const be_node_factory_t *factory =
256     container_of(_self, const be_node_factory_t, handler);
257
258   redir_proj(&irn, 0);
259   bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
260
261   return bo ? &factory->irn_ops : NULL;
262 }
263
264 const arch_irn_handler_t *be_node_get_irn_handler(const be_node_factory_t *f)
265 {
266   return &f->handler;
267 }
268
269 int is_Spill(const be_node_factory_t *f, const ir_node *irn)
270 {
271   be_op_t *bo;
272   bo = pmap_get(f->irn_op_map, get_irn_op(irn));
273   return bo->kind == node_kind_spill;
274 }
275
276 be_node_factory_t *be_node_factory_init(be_node_factory_t *factory,
277     const arch_isa_if_t *isa)
278 {
279   char buf[256];
280   int i, j, n;
281
282   factory->ops = new_set(cmp_op_map, 64);
283   factory->irn_op_map = pmap_create();
284   obstack_init(&factory->obst);
285
286   factory->handler.get_irn_ops = be_node_get_irn_ops;
287
288   factory->irn_ops.get_irn_reg_req = be_node_get_irn_reg_req;
289   factory->irn_ops.get_n_operands  = be_node_get_n_operands;
290   factory->irn_ops.set_irn_reg     = be_node_set_irn_reg;
291   factory->irn_ops.get_irn_reg     = be_node_get_irn_reg;
292   factory->irn_ops.classify        = be_node_classify;
293
294   for(i = 0, n = isa->get_n_reg_class(); i < n; ++i) {
295     const arch_register_class_t *cls = isa->get_reg_class(i);
296     be_op_t *ent;
297
298     ent = get_op(factory, cls, node_kind_spill);
299     snprintf(buf, sizeof(buf), "Spill_%s", cls->name);
300     ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned,
301         0, 0, oparity_unary, 0);
302     ent->n_pos = ARRSIZE(templ_pos_Spill);
303     ent->pos = templ_pos_Spill;
304     pmap_insert(factory->irn_op_map, ent->op, ent);
305
306     ent = get_op(factory, cls, node_kind_reload);
307     snprintf(buf, sizeof(buf), "Reload_%s", cls->name);
308     ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
309         oparity_unary, sizeof(const arch_register_t *));
310     ent->n_pos = ARRSIZE(templ_pos_Reload);
311     ent->pos = templ_pos_Reload;
312     pmap_insert(factory->irn_op_map, ent->op, ent);
313
314     ent = get_op(factory, cls, node_kind_copy);
315     snprintf(buf, sizeof(buf), "Copy_%s", cls->name);
316     ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
317         oparity_unary, sizeof(const arch_register_t *));
318     ent->n_pos = ARRSIZE(templ_pos_Copy);
319     ent->pos = templ_pos_Copy;
320     pmap_insert(factory->irn_op_map, ent->op, ent);
321
322     ent = get_op(factory, cls, node_kind_perm);
323     snprintf(buf, sizeof(buf), "Perm_%s", cls->name);
324     ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
325         oparity_variable, sizeof(const arch_register_t) * cls->n_regs);
326     ent->n_pos = 2 * cls->n_regs;
327     ent->pos = obstack_alloc(&factory->obst, sizeof(ent->pos[0]) * ent->n_pos);
328     for(j = 0; j < ent->n_pos; j += 2) {
329       ent->pos[j] = arch_pos_make_in(j);
330       ent->pos[j + 1] = arch_pos_make_out(j);
331     }
332     pmap_insert(factory->irn_op_map, ent->op, ent);
333
334   }
335
336   return factory;
337 }
338
339 ir_node *insert_Perm_after(const be_main_session_env_t *env,
340     const arch_register_class_t *cls, ir_node *pos)
341 {
342   const arch_env_t *arch_env = env->main_env->arch_env;
343   ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
344   ir_graph *irg = get_irn_irg(bl);
345   pset *live = put_live_end(bl, pset_new_ptr_default());
346   ir_node *curr, *irn, *perm, **nodes;
347   firm_dbg_module_t *dbg = firm_dbg_register("firm.be.node");
348   int i, n;
349
350   firm_dbg_set_mask(dbg, DBG_LEVEL);
351   DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
352
353   sched_foreach_reverse(bl, irn) {
354     ir_node *x;
355
356         /*
357          * If we encounter the node we want to insert the Perm after,
358          * exit immediately, so that this node is still live
359          */
360     if(irn == pos)
361       break;
362
363     DBG((dbg, LEVEL_1, "%+F\n", irn));
364     for(x = pset_first(live); x; x = pset_next(live))
365       DBG((dbg, LEVEL_1, "\tlive: %+F\n", x));
366
367     if(arch_irn_has_reg_class(arch_env, irn, arch_pos_make_out(0), cls))
368       pset_remove_ptr(live, irn);
369
370     for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
371       ir_node *op = get_irn_n(irn, i);
372
373       if(arch_irn_has_reg_class(arch_env, op, arch_pos_make_out(0), cls))
374         pset_insert_ptr(live, op);
375     }
376   }
377
378   n = pset_count(live);
379   nodes = malloc(n * sizeof(nodes[0]));
380
381   DBG((dbg, LEVEL_1, "live:\n"));
382   for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++) {
383         DBG((dbg, LEVEL_1, "\t%+F\n", irn));
384     nodes[i] = irn;
385   }
386
387   perm = new_Perm(env->main_env->node_factory, cls, irg, bl, n, nodes);
388   sched_add_after(pos, perm);
389   free(nodes);
390
391   curr = perm;
392   for(i = 0; i < n; ++i) {
393     ir_node *copies[1];
394     ir_node *perm_op = get_irn_n(perm, i);
395         const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op, 0);
396
397     ir_mode *mode = get_irn_mode(perm_op);
398     ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
399     arch_set_irn_register(arch_env, proj, 0, reg);
400
401     sched_add_after(curr, proj);
402     curr = proj;
403
404     copies[0] = proj;
405     be_introduce_copies(env->dom_front, perm_op, array_size(copies), copies);
406   }
407   return perm;
408 }
409
410 #if 0
411 typedef struct _phi_perm_info_t {
412   const be_main_session_env_t *env;
413   const arch_register_class_t *cls;
414   pmap *perm_map;
415 } phi_perm_info_t;
416
417 static void clear_link(ir_node *irn, void *data)
418 {
419   set_irn_link(irn, NULL);
420 }
421
422 static void collect_phis(ir_node *irn, void *data)
423 {
424   phi_perm_info_t *pi = data;
425   if(is_Phi(irn) && arch_irn_has_reg_class(pi->env->main_env->arch_env,
426         irn, arch_pos_make_out(0), pi->cls)) {
427
428     ir_node *bl = get_nodes_block(irn);
429     set_irn_link(irn, get_irn_link(bl));
430     set_irn_link(bl, irn);
431   }
432 }
433
434 /* This is only semi-ugly :-) */
435 #define INT_TO_PTR(i) ((void *) ((char *) 0 + (i)))
436 #define PTR_TO_INT(p) ((int) ((char *) (p) - (char *) 0))
437
438 static void insert_phi_perms(ir_node *bl, void *data)
439 {
440   phi_perm_info_t *pi = data;
441   ir_graph *irg = pi->env->irg;
442
443   assert(is_Block(bl));
444
445   /* If the link flag is NULL, this block has no phis. */
446   if(get_irn_link(bl)) {
447     int i, n;
448
449     /* Look at all predecessors of the phi block */
450     for(i = 0, n = get_irn_arity(bl); i < n; ++i) {
451       ir_node *pred_bl = get_Block_cfgpred_block(bl, i);
452       ir_node *phi, *perm;
453       ir_node **in;
454       int n_phis = 0;
455       int j;
456
457       for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi))
458         n_phis++;
459
460       in = malloc(n_phis * sizeof(in[0]));
461
462       /* Look at all phis in this block */
463       j = 0;
464       for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi))
465         in[j++] = get_irn_n(phi, i);
466
467       perm = new_Perm(pi->env->main_env->node_factory, pi->cls, irg, pred_bl, n_phis, in);
468
469       j = 0;
470       for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
471         ir_node *proj = new_r_Proj(irg, pred_bl, perm, get_irn_mode(phi), j++);
472         set_irn_n(phi, i, proj);
473       }
474
475       free(in);
476     }
477   }
478 }
479
480 void be_insert_phi_perms(const be_main_session_env_t *env,
481     const arch_register_class_t *cls)
482 {
483   phi_perm_info_t pi;
484
485   pi.env = env;
486   pi.cls = cls;
487   pi.perm_map = pmap_create();
488
489   irg_walk_graph(env->irg, clear_link, collect_phis, &pi);
490   irg_block_walk_graph(env->irg, insert_phi_perms, NULL, &pi);
491   pmap_destroy(pi.perm_map);
492 }
493 #endif