072c6edcd27e21fc19af291a01de6ef022d432de
[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
12 #include <stdlib.h>
13
14 #include "obst.h"
15 #include "set.h"
16 #include "pmap.h"
17 #include "util.h"
18
19 #include "irop_t.h"
20 #include "irmode_t.h"
21 #include "irnode_t.h"
22 #include "ircons_t.h"
23
24 #include "be_t.h"
25 #include "belive_t.h"
26 #include "besched_t.h"
27 #include "benode_t.h"
28
29 #include "beirgmod.h"
30
31 typedef enum _node_kind_t {
32   node_kind_spill,
33   node_kind_reload,
34   node_kind_perm,
35   node_kind_copy,
36   node_kind_last
37 } node_kind_t;
38
39 typedef struct {
40   node_kind_t kind;
41   const arch_register_class_t *cls;
42   ir_op *op;
43   int n_pos;
44   int *pos;
45 } be_op_t;
46
47 static int templ_pos_Spill[] = {
48   arch_pos_make_in(0)
49 };
50
51 static int templ_pos_Reload[] = {
52   arch_pos_make_out(0)
53 };
54
55 static int templ_pos_Copy[] = {
56   arch_pos_make_in(0),
57   arch_pos_make_out(0)
58 };
59
60 #define ARRSIZE(x) (sizeof(x) / sizeof(x[0]))
61
62 static int cmp_op_map(const void *a, const void *b, size_t size)
63 {
64   const be_op_t *x = a;
65   const be_op_t *y = b;
66
67   return !(x->kind == y->kind && x->cls == y->cls);
68 }
69
70 static be_op_t *get_op(const be_node_factory_t *fact,
71     const arch_register_class_t *cls, node_kind_t kind)
72 {
73   be_op_t templ;
74
75   templ.kind = kind;
76   templ.cls = cls;
77
78   return set_insert(fact->ops, &templ, sizeof(templ),
79       HASH_PTR(cls) + 7 * kind);
80 }
81
82 ir_node *new_Spill(const be_node_factory_t *factory,
83     const arch_register_class_t *cls,
84     ir_graph *irg, ir_node *bl, ir_node *node_to_spill)
85 {
86   ir_node *in[1];
87   ir_op *op = get_op(factory, cls, node_kind_spill)->op;
88
89   assert(op && "Spill opcode must be present for this register class");
90   in[0] = node_to_spill;
91
92   return new_ir_node(NULL, irg, bl, op, mode_M, 1, in);
93 }
94
95 ir_node *new_Reload(const be_node_factory_t *factory,
96     const arch_register_class_t *cls,
97     ir_graph *irg, ir_node *bl, ir_node *spill_node)
98 {
99   ir_mode *mode;
100   ir_node *in[1];
101   ir_op *op = get_op(factory, cls, node_kind_reload)->op;
102
103   assert(op && "Reload opcode must be present for this register class");
104   assert(is_Spill(factory, spill_node) && "Operand of Reload must be a Spill");
105   in[0] = spill_node;
106   mode = get_irn_mode(get_irn_n(spill_node, 0));
107
108   return new_ir_node(NULL, irg, bl, op, mode, 1, in);
109 }
110
111 ir_node *new_Perm(const be_node_factory_t *factory,
112     const arch_register_class_t *cls,
113     ir_graph *irg, ir_node *bl, int arity, ir_node **in)
114 {
115   ir_op *op = get_op(factory, cls, node_kind_perm)->op;
116
117   return new_ir_node(NULL, irg, bl, op, mode_T, arity, in);
118 }
119
120 ir_node *new_Copy(const be_node_factory_t *factory,
121     const arch_register_class_t *cls,
122     ir_graph *irg, ir_node *bl, ir_node *in)
123 {
124   ir_node *ins[1];
125   ir_op *op = get_op(factory, cls, node_kind_perm)->op;
126
127   ins[0] = in;
128
129   return new_ir_node(NULL, irg, bl, op, get_irn_mode(in), 1, ins);
130 }
131
132 /**
133  * If the node is a proj, reset the node to the proj's target and return
134  * the proj number.
135  * @param node The address of a node pointer.
136  * @param def  A default value.
137  * @return     If *node is a Proj, *node is set to the Proj's target and
138  *             the Proj number is returned. Else *node remains the same and @p def
139  *             is returned.
140  */
141 static int redir_proj(const ir_node **node, int def)
142 {
143   const ir_node *n = *node;
144
145   if(is_Proj(n)) {
146     *node = get_Proj_pred(n);
147     def = get_Proj_proj(n);
148   }
149
150   return def;
151 }
152
153 static const arch_register_req_t *
154 be_node_get_irn_reg_req(const arch_irn_ops_t *_self,
155     arch_register_req_t *req, const ir_node *irn, int pos)
156 {
157   be_op_t *bo;
158   const be_node_factory_t *factory =
159     container_of(_self, const be_node_factory_t, irn_ops);
160
161   pos = arch_pos_make_out(redir_proj(&irn, arch_pos_get_index(pos)));
162
163   bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
164
165   if(bo) {
166     int i;
167
168     req->type = arch_register_req_type_normal;
169     req->cls = bo->cls;
170
171     for(i = 0; i < bo->n_pos; ++pos)
172       if(pos == bo->pos[i])
173         return req;
174   }
175
176   return NULL;
177 }
178
179 static int
180 be_node_get_n_operands(const arch_irn_ops_t *_self, const ir_node *irn, int in_out)
181 {
182   be_op_t *bo;
183   int i, res = 0;
184   const be_node_factory_t *factory =
185     container_of(_self, const be_node_factory_t, irn_ops);
186
187   redir_proj(&irn, 0);
188   bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
189
190   for(i = 0; i < bo->n_pos; ++i)
191     res += (bo->pos[i] ^ in_out) > 0;
192
193   return res;
194 }
195
196 void
197 be_node_set_irn_reg(const arch_irn_ops_t *_self, ir_node *irn,
198     int idx, const arch_register_t *reg)
199 {
200   const arch_register_t **regs;
201
202   idx = redir_proj((const ir_node **) &irn, idx);
203   regs = (const arch_register_t **) &irn->attr;
204   regs[idx] = reg;
205 }
206
207 const arch_register_t *
208 be_node_get_irn_reg(const arch_irn_ops_t *_self, const ir_node *irn, int idx)
209 {
210   const arch_register_t **regs;
211
212   idx = redir_proj(&irn, idx);
213   regs = (const arch_register_t **) &irn->attr;
214   return regs[idx];
215 }
216
217 arch_irn_class_t be_node_classify(const arch_irn_ops_t *_self, const ir_node *irn)
218 {
219   const be_node_factory_t *factory =
220     container_of(_self, const be_node_factory_t, irn_ops);
221
222   be_op_t *bo;
223   int idx;
224
225   idx = redir_proj(&irn, 0);
226   bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
227
228   return 0;
229 }
230
231 static const arch_irn_ops_t *
232 be_node_get_irn_ops(const arch_irn_handler_t *_self, const ir_node *irn)
233 {
234   be_op_t *bo;
235   const be_node_factory_t *factory =
236     container_of(_self, const be_node_factory_t, handler);
237
238   redir_proj(&irn, 0);
239   bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
240
241   return bo ? &factory->irn_ops : NULL;
242 }
243
244 const arch_irn_handler_t *be_node_get_irn_handler(const be_node_factory_t *f)
245 {
246   return &f->handler;
247 }
248
249 int is_Spill(const be_node_factory_t *f, const ir_node *irn)
250 {
251   be_op_t *bo;
252   bo = pmap_get(f->irn_op_map, get_irn_op(irn));
253   return bo->kind == node_kind_spill;
254 }
255
256 be_node_factory_t *be_node_factory_init(be_node_factory_t *factory,
257     const arch_isa_if_t *isa)
258 {
259   char buf[256];
260   int i, j, n;
261
262   factory->ops = new_set(cmp_op_map, 64);
263   factory->irn_op_map = pmap_create();
264   obstack_init(&factory->obst);
265
266   factory->handler.get_irn_ops = be_node_get_irn_ops;
267
268   factory->irn_ops.get_irn_reg_req = be_node_get_irn_reg_req;
269   factory->irn_ops.get_n_operands  = be_node_get_n_operands;
270   factory->irn_ops.set_irn_reg     = be_node_set_irn_reg;
271   factory->irn_ops.get_irn_reg     = be_node_get_irn_reg;
272   factory->irn_ops.classify        = be_node_classify;
273
274   for(i = 0, n = isa->get_n_reg_class(); i < n; ++i) {
275     const arch_register_class_t *cls = isa->get_reg_class(i);
276     be_op_t *ent;
277
278     ent = get_op(factory, cls, node_kind_spill);
279     snprintf(buf, sizeof(buf), "Spill_%s", cls->name);
280     ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned,
281         0, 0, oparity_unary, 0);
282     ent->n_pos = ARRSIZE(templ_pos_Spill);
283     ent->pos = templ_pos_Spill;
284     pmap_insert(factory->irn_op_map, ent->op, ent);
285
286     ent = get_op(factory, cls, node_kind_reload);
287     snprintf(buf, sizeof(buf), "Reload_%s", cls->name);
288     ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
289         oparity_unary, sizeof(const arch_register_t *));
290     ent->n_pos = ARRSIZE(templ_pos_Reload);
291     ent->pos = templ_pos_Reload;
292     pmap_insert(factory->irn_op_map, ent->op, ent);
293
294     ent = get_op(factory, cls, node_kind_copy);
295     snprintf(buf, sizeof(buf), "Copy_%s", cls->name);
296     ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
297         oparity_unary, sizeof(const arch_register_t *));
298     ent->n_pos = ARRSIZE(templ_pos_Copy);
299     ent->pos = templ_pos_Copy;
300     pmap_insert(factory->irn_op_map, ent->op, ent);
301
302     ent = get_op(factory, cls, node_kind_perm);
303     snprintf(buf, sizeof(buf), "Perm_%s", cls->name);
304     ent->op = new_ir_op(get_next_ir_opcode(), buf, op_pin_state_pinned, 0, 0,
305         oparity_variable, sizeof(const arch_register_t) * cls->n_regs);
306     ent->n_pos = 2 * cls->n_regs;
307     ent->pos = obstack_alloc(&factory->obst, sizeof(ent->pos[0]) * ent->n_pos);
308     for(j = 0; j < ent->n_pos; j += 2) {
309       ent->pos[j] = arch_pos_make_in(j);
310       ent->pos[j + 1] = arch_pos_make_out(j);
311     }
312     pmap_insert(factory->irn_op_map, ent->op, ent);
313
314   }
315
316   return factory;
317 }
318
319 void insert_Perm_after(const be_main_session_env_t *env,
320     const arch_register_class_t *cls, ir_node *pos)
321 {
322   const arch_env_t *arch_env = env->main_env->arch_env;
323   ir_node *bl = is_Block(pos) ? pos : get_nodes_block(pos);
324   ir_graph *irg = get_irn_irg(bl);
325   pset *live_end = get_live_end(bl);
326   pset *live = pset_new_ptr_default();
327   ir_node *curr, *irn, *perm, **nodes;
328   int i, n;
329
330   /* put all live ends in the live set. */
331   for(irn = pset_first(live_end); irn; irn = pset_next(live_end))
332     pset_insert_ptr(live, irn);
333
334   sched_foreach_reverse(bl, irn) {
335
336     if(arch_irn_has_reg_class(arch_env, irn, arch_pos_make_out(0), cls))
337       pset_remove_ptr(live, irn);
338
339     for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
340       ir_node *op = get_irn_n(irn, i);
341
342       if(arch_irn_has_reg_class(arch_env, op, arch_pos_make_out(0), cls))
343         pset_insert_ptr(live, op);
344     }
345
346     if(sched_prev(irn) == pos)
347       break;
348   }
349
350   n = pset_count(live);
351   nodes = malloc(n * sizeof(nodes[0]));
352
353   for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++)
354     nodes[i] = irn;
355
356   curr = perm = new_Perm(env->main_env->node_factory, cls, irg, bl, n, nodes);
357   sched_add_after(pos, perm);
358   free(nodes);
359
360   for(i = 0; i < n; ++i) {
361     ir_node *copies[1];
362     ir_node *perm_op = get_irn_n(perm, i);
363
364     ir_mode *mode = get_irn_mode(perm_op);
365     ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
366     sched_add_after(curr, proj);
367     curr = proj;
368
369     copies[0] = proj;
370     be_introduce_copies(env->dom_front, perm_op, array_size(copies), copies);
371   }
372 }