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