Added phi perm inserter
[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   return 0;
248 }
249
250 static const arch_irn_ops_t *
251 be_node_get_irn_ops(const arch_irn_handler_t *_self, const ir_node *irn)
252 {
253   be_op_t *bo;
254   const be_node_factory_t *factory =
255     container_of(_self, const be_node_factory_t, handler);
256
257   redir_proj(&irn, 0);
258   bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
259
260   return bo ? &factory->irn_ops : NULL;
261 }
262
263 const arch_irn_handler_t *be_node_get_irn_handler(const be_node_factory_t *f)
264 {
265   return &f->handler;
266 }
267
268 int is_Spill(const be_node_factory_t *f, const ir_node *irn)
269 {
270   be_op_t *bo;
271   bo = pmap_get(f->irn_op_map, get_irn_op(irn));
272   return bo->kind == node_kind_spill;
273 }
274
275 be_node_factory_t *be_node_factory_init(be_node_factory_t *factory,
276     const arch_isa_if_t *isa)
277 {
278   char buf[256];
279   int i, j, n;
280
281   factory->ops = new_set(cmp_op_map, 64);
282   factory->irn_op_map = pmap_create();
283   obstack_init(&factory->obst);
284
285   factory->handler.get_irn_ops = be_node_get_irn_ops;
286
287   factory->irn_ops.get_irn_reg_req = be_node_get_irn_reg_req;
288   factory->irn_ops.get_n_operands  = be_node_get_n_operands;
289   factory->irn_ops.set_irn_reg     = be_node_set_irn_reg;
290   factory->irn_ops.get_irn_reg     = be_node_get_irn_reg;
291   factory->irn_ops.classify        = be_node_classify;
292
293   for(i = 0, n = isa->get_n_reg_class(); i < n; ++i) {
294     const arch_register_class_t *cls = isa->get_reg_class(i);
295     be_op_t *ent;
296
297     ent = get_op(factory, cls, node_kind_spill);
298     snprintf(buf, sizeof(buf), "Spill_%s", cls->name);
299     ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned,
300         0, 0, oparity_unary, 0);
301     ent->n_pos = ARRSIZE(templ_pos_Spill);
302     ent->pos = templ_pos_Spill;
303     pmap_insert(factory->irn_op_map, ent->op, ent);
304
305     ent = get_op(factory, cls, node_kind_reload);
306     snprintf(buf, sizeof(buf), "Reload_%s", cls->name);
307     ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
308         oparity_unary, sizeof(const arch_register_t *));
309     ent->n_pos = ARRSIZE(templ_pos_Reload);
310     ent->pos = templ_pos_Reload;
311     pmap_insert(factory->irn_op_map, ent->op, ent);
312
313     ent = get_op(factory, cls, node_kind_copy);
314     snprintf(buf, sizeof(buf), "Copy_%s", cls->name);
315     ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
316         oparity_unary, sizeof(const arch_register_t *));
317     ent->n_pos = ARRSIZE(templ_pos_Copy);
318     ent->pos = templ_pos_Copy;
319     pmap_insert(factory->irn_op_map, ent->op, ent);
320
321     ent = get_op(factory, cls, node_kind_perm);
322     snprintf(buf, sizeof(buf), "Perm_%s", cls->name);
323     ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
324         oparity_variable, sizeof(const arch_register_t) * cls->n_regs);
325     ent->n_pos = 2 * cls->n_regs;
326     ent->pos = obstack_alloc(&factory->obst, sizeof(ent->pos[0]) * ent->n_pos);
327     for(j = 0; j < ent->n_pos; j += 2) {
328       ent->pos[j] = arch_pos_make_in(j);
329       ent->pos[j + 1] = arch_pos_make_out(j);
330     }
331     pmap_insert(factory->irn_op_map, ent->op, ent);
332
333   }
334
335   return factory;
336 }
337
338 ir_node *insert_Perm_after(const be_main_session_env_t *env,
339     const arch_register_class_t *cls, ir_node *pos)
340 {
341   const arch_env_t *arch_env = env->main_env->arch_env;
342   ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
343   ir_graph *irg = get_irn_irg(bl);
344   pset *live = put_live_end(bl, pset_new_ptr_default());
345   ir_node *curr, *irn, *perm, **nodes;
346   firm_dbg_module_t *dbg = firm_dbg_register("firm.be.node");
347   int i, n;
348
349   firm_dbg_set_mask(dbg, DBG_LEVEL);
350   DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
351
352   sched_foreach_reverse(bl, irn) {
353     ir_node *x;
354
355         /*
356          * If we encounter the node we want to insert the Perm after,
357          * exit immediately, so that this node is still live
358          */
359     if(irn == pos)
360       break;
361
362     DBG((dbg, LEVEL_1, "%+F\n", irn));
363     for(x = pset_first(live); x; x = pset_next(live))
364       DBG((dbg, LEVEL_1, "\tlive: %+F\n", x));
365
366     if(arch_irn_has_reg_class(arch_env, irn, arch_pos_make_out(0), cls))
367       pset_remove_ptr(live, irn);
368
369     for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
370       ir_node *op = get_irn_n(irn, i);
371
372       if(arch_irn_has_reg_class(arch_env, op, arch_pos_make_out(0), cls))
373         pset_insert_ptr(live, op);
374     }
375   }
376
377   n = pset_count(live);
378   nodes = malloc(n * sizeof(nodes[0]));
379
380   DBG((dbg, LEVEL_1, "live:\n"));
381   for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++) {
382         DBG((dbg, LEVEL_1, "\t%+F\n", irn));
383     nodes[i] = irn;
384   }
385
386   perm = new_Perm(env->main_env->node_factory, cls, irg, bl, n, nodes);
387   sched_add_after(pos, perm);
388   free(nodes);
389
390   curr = perm;
391   for(i = 0; i < n; ++i) {
392     ir_node *copies[1];
393     ir_node *perm_op = get_irn_n(perm, i);
394         const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op, 0);
395
396     ir_mode *mode = get_irn_mode(perm_op);
397     ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
398     arch_set_irn_register(arch_env, proj, 0, reg);
399
400     sched_add_after(curr, proj);
401     curr = proj;
402
403     copies[0] = proj;
404     be_introduce_copies(env->dom_front, perm_op, array_size(copies), copies);
405   }
406   return perm;
407 }
408
409 typedef struct _phi_perm_info_t {
410   const be_main_session_env_t *env;
411   const arch_register_class_t *cls;
412   pmap *perm_map;
413 } phi_perm_info_t;
414
415 static void clear_link(ir_node *irn, void *data)
416 {
417   set_irn_link(irn, NULL);
418 }
419
420 static void collect_phis(ir_node *irn, void *data)
421 {
422   phi_perm_info_t *pi = data;
423   if(is_Phi(irn) && arch_irn_has_reg_class(pi->env->main_env->arch_env,
424         irn, arch_pos_make_out(0), pi->cls)) {
425
426     ir_node *bl = get_nodes_block(irn);
427     set_irn_link(irn, get_irn_link(bl));
428     set_irn_link(bl, irn);
429   }
430 }
431
432 /* This is only semi-ugly :-) */
433 #define INT_TO_PTR(i) ((void *) ((char *) 0 + (i)))
434 #define PTR_TO_INT(p) ((int) ((char *) (p) - (char *) 0))
435
436 static void insert_phi_perms(ir_node *bl, void *data)
437 {
438   phi_perm_info_t *pi = data;
439   ir_graph *irg = pi->env->irg;
440
441   assert(is_Block(bl));
442
443   /* If the link flag is NULL, this block has no phis. */
444   if(get_irn_link(bl)) {
445     int i, n;
446
447     /* Look at all predecessors of the phi block */
448     for(i = 0, n = get_irn_arity(bl); i < n; ++i) {
449       ir_node *pred_bl = get_Block_cfgpred_block(bl, i);
450       ir_node *phi, *perm;
451       ir_node **in;
452       int n_phis = 0;
453       int j;
454
455       for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi))
456         n_phis++;
457
458       in = malloc(n_phis * sizeof(in[0]));
459
460       /* Look at all phis in this block */
461       j = 0;
462       for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi))
463         in[j++] = get_irn_n(phi, i);
464
465       perm = new_Perm(pi->env->main_env->node_factory, pi->cls, irg, pred_bl, n_phis, in);
466
467       j = 0;
468       for(phi = get_irn_link(bl); phi; phi = get_irn_link(phi)) {
469         ir_node *proj = new_r_Proj(irg, pred_bl, perm, get_irn_mode(phi), j++);
470         set_irn_n(phi, i, proj);
471       }
472
473       free(in);
474     }
475   }
476 }
477
478 void be_insert_phi_perms(const be_main_session_env_t *env,
479     const arch_register_class_t *cls)
480 {
481   phi_perm_info_t pi;
482
483   pi.env = env;
484   pi.cls = cls;
485   pi.perm_map = pmap_create();
486
487   irg_walk_graph(env->irg, clear_link, collect_phis, &pi);
488   irg_block_walk_graph(env->irg, insert_phi_perms, NULL, &pi);
489   pmap_destroy(pi.perm_map);
490 }