added dumping option after regalloc
[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         0
55 };
56
57 static int templ_pos_Reload[] = {
58         -1
59 };
60
61 static int templ_pos_Copy[] = {
62         0, -1
63 };
64
65 #define ARRSIZE(x) (sizeof(x) / sizeof(x[0]))
66
67 static int cmp_op_map(const void *a, const void *b, size_t size)
68 {
69   const be_op_t *x = a;
70   const be_op_t *y = b;
71
72   return !(x->kind == y->kind && x->cls == y->cls);
73 }
74
75 static be_op_t *get_op(const be_node_factory_t *fact,
76     const arch_register_class_t *cls, node_kind_t kind)
77 {
78   be_op_t templ;
79
80   templ.kind = kind;
81   templ.cls = cls;
82
83   return set_insert(fact->ops, &templ, sizeof(templ),
84       HASH_PTR(cls) + 7 * kind);
85 }
86
87 ir_node *new_Spill(const be_node_factory_t *factory,
88     const arch_register_class_t *cls,
89     ir_graph *irg, ir_node *bl, ir_node *node_to_spill)
90 {
91   ir_node *in[1];
92   ir_op *op = get_op(factory, cls, node_kind_spill)->op;
93
94   assert(op && "Spill opcode must be present for this register class");
95   in[0] = node_to_spill;
96
97   return new_ir_node(NULL, irg, bl, op, mode_M, 1, in);
98 }
99
100 ir_node *new_Reload(const be_node_factory_t *factory,
101     const arch_register_class_t *cls, ir_graph *irg,
102     ir_node *bl, ir_mode *mode, ir_node *spill_node)
103 {
104   ir_node *in[1];
105   ir_op *op = get_op(factory, cls, node_kind_reload)->op;
106
107   assert(op && "Reload opcode must be present for this register class");
108   // assert(is_Spill(factory, spill_node) && "Operand of Reload must be a Spill");
109   in[0] = spill_node;
110
111   return new_ir_node(NULL, irg, bl, op, mode, 1, in);
112 }
113
114 ir_node *new_Perm(const be_node_factory_t *factory,
115     const arch_register_class_t *cls,
116     ir_graph *irg, ir_node *bl, int arity, ir_node **in)
117 {
118   ir_op *op = get_op(factory, cls, node_kind_perm)->op;
119
120   return new_ir_node(NULL, irg, bl, op, mode_T, arity, in);
121 }
122
123 ir_node *new_Copy(const be_node_factory_t *factory,
124     const arch_register_class_t *cls,
125     ir_graph *irg, ir_node *bl, ir_node *in)
126 {
127   ir_node *ins[1];
128   ir_op *op = get_op(factory, cls, node_kind_copy)->op;
129
130   ins[0] = in;
131
132   return new_ir_node(NULL, irg, bl, op, get_irn_mode(in), 1, ins);
133 }
134
135 ir_node *be_spill(
136                 const be_node_factory_t *factory,
137                 const arch_env_t *arch_env,
138                 ir_node *irn)
139 {
140   const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, irn, -1);
141   ir_node *bl    = get_nodes_block(irn);
142   ir_graph *irg  = get_irn_irg(bl);
143   ir_node *spill = new_Spill(factory, cls, irg, bl, irn);
144
145         ir_node *insert;
146
147         /*
148          * search the right insertion point. a spill of a phi cannot be put
149          * directly after the phi, if there are some phis behind the one which
150          * is spilled.
151          */
152         insert = sched_next(irn);
153         while(is_Phi(insert) && !sched_is_end(insert))
154                 insert = sched_next(insert);
155
156         sched_add_before(insert, spill);
157   return spill;
158 }
159
160 ir_node *be_reload(const be_node_factory_t *factory,
161                                    const arch_env_t *arch_env,
162                                    const arch_register_class_t *cls,
163                                    ir_node *irn, int pos, ir_mode *mode, ir_node *spill)
164 {
165         ir_node *reload;
166
167   ir_node *bl   = get_nodes_block(irn);
168   ir_graph *irg = get_irn_irg(bl);
169
170         assert(is_Spill(factory, spill)
171                         || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
172
173   reload = new_Reload(factory, cls, irg, bl, mode, spill);
174
175         set_irn_n(irn, pos, reload);
176   sched_add_before(irn, reload);
177   return reload;
178 }
179
180 /**
181  * If the node is a proj, reset the node to the proj's target and return
182  * the proj number.
183  * @param node The address of a node pointer.
184  * @param def  A default value.
185  * @return     If *node is a Proj, *node is set to the Proj's target and
186  *             the Proj number is returned. Else *node remains the same and @p def
187  *             is returned.
188  */
189 static int redir_proj(const ir_node **node, int def)
190 {
191   const ir_node *n = *node;
192
193   if(is_Proj(n)) {
194     *node = get_Proj_pred(n);
195     def = -(get_Proj_proj(n) + 1);
196   }
197
198   return def;
199 }
200
201 static const arch_register_req_t *
202 be_node_get_irn_reg_req(const arch_irn_ops_t *_self,
203     arch_register_req_t *req, const ir_node *irn, int pos)
204 {
205         be_op_t *bo;
206         const be_node_factory_t *factory =
207                 container_of(_self, const be_node_factory_t, irn_ops);
208
209         /*
210          * were interested in an output operand, so
211          * let's resolve projs.
212          */
213         if(pos < 0)
214                 pos = redir_proj((const ir_node **) &irn, pos);
215
216         bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
217
218         if(bo) {
219                 int i;
220
221                 req->type = arch_register_req_type_normal;
222                 req->cls  = bo->cls;
223
224                 for(i = 0; i < bo->n_pos; ++i)
225                         if(pos == bo->pos[i])
226                                 return req;
227         }
228
229         return NULL;
230 }
231
232 void
233 be_node_set_irn_reg(const arch_irn_ops_t *_self, ir_node *irn,
234     const arch_register_t *reg)
235 {
236         int pos;
237         const arch_register_t **regs;
238         be_op_t *bo;
239         const be_node_factory_t *factory =
240                 container_of(_self, const be_node_factory_t, irn_ops);
241
242         pos = redir_proj((const ir_node **) &irn, -1);
243         bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
244
245         if(!bo)
246                 return;
247
248         regs = (const arch_register_t **) &irn->attr;
249         regs[-pos - 1] = reg;
250 }
251
252 const arch_register_t *
253 be_node_get_irn_reg(const arch_irn_ops_t *_self, const ir_node *irn)
254 {
255         int i, pos;
256         const arch_register_t **regs;
257         be_op_t *bo;
258         const be_node_factory_t *factory =
259                 container_of(_self, const be_node_factory_t, irn_ops);
260
261         pos = redir_proj((const ir_node **) &irn, -1);
262         bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
263
264         if(!bo)
265                 return NULL;
266
267         for(i = 0; i < bo->n_pos; ++i) {
268                 if(bo->pos[i] == pos) {
269                         regs = (const arch_register_t **) &irn->attr;
270                         return regs[-pos - 1];
271                 }
272         }
273
274         return NULL;
275 }
276
277 arch_irn_class_t be_node_classify(const arch_irn_ops_t *_self, const ir_node *irn)
278 {
279   const be_node_factory_t *factory =
280     container_of(_self, const be_node_factory_t, irn_ops);
281
282   be_op_t *bo;
283   int idx;
284
285   idx = redir_proj(&irn, 0);
286   bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
287
288         switch(bo->kind) {
289 #define XXX(a) case node_kind_ ## a: return arch_irn_class_ ## a;
290                 XXX(spill)
291                 XXX(reload)
292                 XXX(perm)
293                 XXX(copy)
294 #undef XXX
295                 default:
296                 return 0;
297         }
298
299   return 0;
300 }
301
302 arch_irn_class_t be_node_get_flags(const arch_irn_ops_t *_self, const ir_node *irn)
303 {
304         return 0;
305 }
306
307 static const arch_irn_ops_t *
308 be_node_get_irn_ops(const arch_irn_handler_t *_self, const ir_node *irn)
309 {
310   be_op_t *bo;
311   const be_node_factory_t *factory =
312     container_of(_self, const be_node_factory_t, handler);
313
314   redir_proj(&irn, 0);
315   bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
316
317   return bo ? &factory->irn_ops : NULL;
318 }
319
320 const arch_irn_handler_t *be_node_get_irn_handler(const be_node_factory_t *f)
321 {
322   return &f->handler;
323 }
324
325 int is_Spill(const be_node_factory_t *f, const ir_node *irn)
326 {
327   be_op_t *bo;
328   bo = pmap_get(f->irn_op_map, get_irn_op(irn));
329   return bo != NULL && bo->kind == node_kind_spill;
330 }
331
332 be_node_factory_t *be_node_factory_init(be_node_factory_t *factory, const arch_isa_t *isa)
333 {
334   char buf[256];
335   int i, j, n;
336
337   factory->ops = new_set(cmp_op_map, 64);
338   factory->irn_op_map = pmap_create();
339   obstack_init(&factory->obst);
340
341   factory->handler.get_irn_ops = be_node_get_irn_ops;
342
343   factory->irn_ops.get_irn_reg_req = be_node_get_irn_reg_req;
344   factory->irn_ops.set_irn_reg     = be_node_set_irn_reg;
345   factory->irn_ops.get_irn_reg     = be_node_get_irn_reg;
346   factory->irn_ops.classify        = be_node_classify;
347   factory->irn_ops.get_flags       = be_node_get_flags;
348
349   for(i = 0, n = arch_isa_get_n_reg_class(isa); i < n; ++i) {
350     const arch_register_class_t *cls = arch_isa_get_reg_class(isa, i);
351     be_op_t *ent;
352
353     ent = get_op(factory, cls, node_kind_spill);
354     snprintf(buf, sizeof(buf), "Spill_%s", cls->name);
355     ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned,
356         0, oparity_unary, 0, 0, NULL);
357     ent->n_pos = ARRSIZE(templ_pos_Spill);
358     ent->pos = templ_pos_Spill;
359     pmap_insert(factory->irn_op_map, ent->op, ent);
360
361     ent = get_op(factory, cls, node_kind_reload);
362     snprintf(buf, sizeof(buf), "Reload_%s", cls->name);
363     ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0,
364         oparity_unary, 0, sizeof(const arch_register_t *), NULL);
365     ent->n_pos = ARRSIZE(templ_pos_Reload);
366     ent->pos = templ_pos_Reload;
367     pmap_insert(factory->irn_op_map, ent->op, ent);
368
369     ent = get_op(factory, cls, node_kind_copy);
370     snprintf(buf, sizeof(buf), "Copy_%s", cls->name);
371     ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0,
372         oparity_unary, 0, sizeof(const arch_register_t *), NULL);
373     ent->n_pos = ARRSIZE(templ_pos_Copy);
374     ent->pos = templ_pos_Copy;
375     pmap_insert(factory->irn_op_map, ent->op, ent);
376
377     ent = get_op(factory, cls, node_kind_perm);
378     snprintf(buf, sizeof(buf), "Perm_%s", cls->name);
379     ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0,
380         oparity_variable, 0, sizeof(const arch_register_t) * cls->n_regs, NULL);
381     ent->n_pos = 2 * cls->n_regs;
382     ent->pos = obstack_alloc(&factory->obst, sizeof(ent->pos[0]) * ent->n_pos);
383     for(j = 0; j < ent->n_pos; j += 2) {
384       ent->pos[j] = j;
385       ent->pos[j + 1] = -(j + 1);
386     }
387     pmap_insert(factory->irn_op_map, ent->op, ent);
388
389   }
390
391   return factory;
392 }
393
394 ir_node *insert_Perm_after(const be_main_env_t *env,
395                                                    const arch_register_class_t *cls,
396                                                    dom_front_info_t *dom_front,
397                                                    ir_node *pos)
398 {
399   const arch_env_t *arch_env  = env->arch_env;
400   ir_node *bl                 = is_Block(pos) ? pos : get_nodes_block(pos);
401   ir_graph *irg               = get_irn_irg(bl);
402   pset *live                  = put_live_end(bl, pset_new_ptr_default());
403   firm_dbg_module_t *dbg      = firm_dbg_register("firm.be.node");
404
405   ir_node *curr, *irn, *perm, **nodes;
406   int i, n;
407
408   firm_dbg_set_mask(dbg, DBG_LEVEL);
409   DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
410
411   sched_foreach_reverse(bl, irn) {
412     ir_node *x;
413
414         /*
415          * If we encounter the node we want to insert the Perm after,
416          * exit immediately, so that this node is still live
417          */
418     if(irn == pos)
419       break;
420
421     DBG((dbg, LEVEL_1, "%+F\n", irn));
422     for(x = pset_first(live); x; x = pset_next(live))
423       DBG((dbg, LEVEL_1, "\tlive: %+F\n", x));
424
425     if(arch_irn_has_reg_class(arch_env, irn, -1, cls))
426       pset_remove_ptr(live, irn);
427
428     for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
429       ir_node *op = get_irn_n(irn, i);
430
431       if(arch_irn_has_reg_class(arch_env, op, -1, cls))
432         pset_insert_ptr(live, op);
433     }
434   }
435
436   n = pset_count(live);
437   nodes = malloc(n * sizeof(nodes[0]));
438
439   DBG((dbg, LEVEL_1, "live:\n"));
440   for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++) {
441         DBG((dbg, LEVEL_1, "\t%+F\n", irn));
442     nodes[i] = irn;
443   }
444
445   perm = new_Perm(env->node_factory, cls, irg, bl, n, nodes);
446   sched_add_after(pos, perm);
447   free(nodes);
448
449   curr = perm;
450   for(i = 0; i < n; ++i) {
451     ir_node *copies[1];
452     ir_node *perm_op = get_irn_n(perm, i);
453         const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op);
454
455     ir_mode *mode = get_irn_mode(perm_op);
456     ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
457     arch_set_irn_register(arch_env, proj, reg);
458
459     sched_add_after(curr, proj);
460     curr = proj;
461
462     copies[0] = proj;
463     be_introduce_copies(dom_front, perm_op, array_size(copies), copies);
464   }
465   return perm;
466 }