5960b6fa38efbfb38235a964b323d269c718101a
[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 #ifdef HAVE_CONFIG_H
13 #include "config.h"
14 #endif
15
16 #include <stdlib.h>
17
18 #include "obst.h"
19 #include "set.h"
20 #include "pmap.h"
21 #include "util.h"
22 #include "debug.h"
23
24 #include "irop_t.h"
25 #include "irmode_t.h"
26 #include "irnode_t.h"
27 #include "ircons_t.h"
28 #include "irprintf.h"
29
30 #include "be_t.h"
31 #include "belive_t.h"
32 #include "besched_t.h"
33 #include "benode_t.h"
34
35 #include "beirgmod.h"
36
37 #define DBG_LEVEL 0
38
39 typedef enum _node_kind_t {
40         node_kind_spill,
41         node_kind_reload,
42         node_kind_perm,
43         node_kind_copy,
44         node_kind_last
45 } node_kind_t;
46
47 typedef struct {
48         node_kind_t kind;
49         const arch_register_class_t *cls;
50         ir_op *op;
51         int n_pos;
52         int *pos;
53 } be_op_t;
54
55 typedef struct {
56         const be_node_factory_t *factory;
57         int n_regs;
58         const arch_register_t *reg[1];
59 } be_node_attr_t;
60
61 typedef struct {
62         be_node_attr_t attr;
63         ir_node *spill_ctx;
64 } be_spill_attr_t;
65
66 static int templ_pos_Spill[] = {
67         0
68 };
69
70 static int templ_pos_Reload[] = {
71         -1
72 };
73
74 static int templ_pos_Copy[] = {
75         0, -1
76 };
77
78 static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason);
79
80 static const ir_op_ops be_node_ops = {
81         NULL,
82         NULL,
83         NULL,
84         NULL,
85         NULL,
86         NULL,
87         NULL,
88         NULL,
89         NULL,
90         NULL,
91         NULL,
92         dump_node,
93         NULL
94 };
95
96 static be_node_attr_t *init_node_attr(ir_node *irn,
97                 const be_node_factory_t *fact, int n_regs)
98 {
99         be_node_attr_t *attr = (be_node_attr_t *) &irn->attr;
100         int i;
101
102         attr->n_regs  = n_regs;
103         attr->factory = fact;
104
105         for(i = 0; i < n_regs; ++i)
106                 attr->reg[i] = NULL;
107
108         return attr;
109 }
110
111 #define ARRSIZE(x) (sizeof(x) / sizeof(x[0]))
112
113 static int cmp_op_map(const void *a, const void *b, size_t size)
114 {
115         const be_op_t *x = a;
116         const be_op_t *y = b;
117
118         return !(x->kind == y->kind && x->cls == y->cls);
119 }
120
121 static be_op_t *get_op(const be_node_factory_t *fact,
122                 const arch_register_class_t *cls, node_kind_t kind)
123 {
124         be_op_t templ;
125
126         templ.kind = kind;
127         templ.cls = cls;
128
129         return set_insert(fact->ops, &templ, sizeof(templ),
130                 HASH_PTR(cls) + 7 * kind);
131 }
132
133 ir_node *new_Spill(const be_node_factory_t *factory,
134                 const arch_register_class_t *cls,
135                 ir_graph *irg, ir_node *bl, ir_node *node_to_spill, ir_node *ctx)
136 {
137         be_spill_attr_t *attr;
138         ir_node *irn;
139         ir_node *in[1];
140         ir_op *op = get_op(factory, cls, node_kind_spill)->op;
141
142         assert(op && "Spill opcode must be present for this register class");
143         in[0] = node_to_spill;
144         irn  = new_ir_node(NULL, irg, bl, op, mode_M, 1, in);
145         attr = (be_spill_attr_t *) init_node_attr(irn, factory, 0);
146         attr->spill_ctx = ctx;
147
148         return irn;
149 }
150
151 ir_node *new_Reload(const be_node_factory_t *factory,
152                 const arch_register_class_t *cls, ir_graph *irg,
153                 ir_node *bl, ir_mode *mode, ir_node *spill_node)
154 {
155         ir_node *irn, *in[1];
156         ir_op *op = get_op(factory, cls, node_kind_reload)->op;
157
158         assert(op && "Reload opcode must be present for this register class");
159         // assert(is_Spill(factory, spill_node) && "Operand of Reload must be a Spill");
160         in[0] = spill_node;
161
162         irn  = new_ir_node(NULL, irg, bl, op, mode, 1, in);
163         init_node_attr(irn, factory, 1);
164
165         return irn;
166 }
167
168 ir_node *new_Perm(const be_node_factory_t *factory,
169                 const arch_register_class_t *cls,
170                 ir_graph *irg, ir_node *bl, int arity, ir_node **in)
171 {
172         ir_node *irn;
173         ir_op *op = get_op(factory, cls, node_kind_perm)->op;
174
175         irn  = new_ir_node(NULL, irg, bl, op, mode_T, arity, in);
176         init_node_attr(irn, factory, arity);
177
178         return irn;
179 }
180
181 ir_node *new_Copy(const be_node_factory_t *factory,
182                 const arch_register_class_t *cls,
183                 ir_graph *irg, ir_node *bl, ir_node *in)
184 {
185         ir_node *irn, *ins[1];
186         ir_op *op = get_op(factory, cls, node_kind_copy)->op;
187
188         ins[0] = in;
189
190         irn  = new_ir_node(NULL, irg, bl, op, get_irn_mode(in), 1, ins);
191         init_node_attr(irn, factory, 1);
192
193         return irn;
194 }
195
196 ir_node *be_spill(
197                 const be_node_factory_t *factory,
198                 const arch_env_t *arch_env,
199                 ir_node *irn, ir_node *ctx)
200 {
201         const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, irn, -1);
202
203         ir_node *bl    = get_nodes_block(irn);
204         ir_graph *irg  = get_irn_irg(bl);
205         ir_node *spill = new_Spill(factory, cls, irg, bl, irn, ctx);
206         ir_node *insert;
207
208         /*
209          * search the right insertion point. a spill of a phi cannot be put
210          * directly after the phi, if there are some phis behind the one which
211          * is spilled.
212          */
213         insert = sched_next(irn);
214         while(is_Phi(insert) && !sched_is_end(insert))
215                 insert = sched_next(insert);
216
217         sched_add_before(insert, spill);
218         return spill;
219 }
220
221 ir_node *be_reload(const be_node_factory_t *factory,
222                                          const arch_env_t *arch_env,
223                                          const arch_register_class_t *cls,
224                                          ir_node *irn, int pos, ir_mode *mode, ir_node *spill)
225 {
226         ir_node *reload;
227
228         ir_node *bl   = get_nodes_block(irn);
229         ir_graph *irg = get_irn_irg(bl);
230
231         assert(is_Spill(factory, spill)
232                         || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
233
234         reload = new_Reload(factory, cls, irg, bl, mode, spill);
235
236         set_irn_n(irn, pos, reload);
237         sched_add_before(irn, reload);
238         return reload;
239 }
240
241 /**
242  * If the node is a proj, reset the node to the proj's target and return
243  * the proj number.
244  * @param node The address of a node pointer.
245  * @param def  A default value.
246  * @return     If *node is a Proj, *node is set to the Proj's target and
247  *             the Proj number is returned. Else *node remains the same and @p def
248  *             is returned.
249  */
250 static int redir_proj(const ir_node **node, int def)
251 {
252         const ir_node *n = *node;
253
254         if(is_Proj(n)) {
255                 *node = get_Proj_pred(n);
256                 def = -(get_Proj_proj(n) + 1);
257         }
258
259         return def;
260 }
261
262 static const arch_register_req_t *
263 be_node_get_irn_reg_req(const arch_irn_ops_t *_self,
264                 arch_register_req_t *req, const ir_node *irn, int pos)
265 {
266         be_op_t *bo;
267         const be_node_factory_t *factory =
268                 container_of(_self, const be_node_factory_t, irn_ops);
269
270         if(get_irn_mode(irn) == mode_T && pos < 0)
271                 return NULL;
272
273         /*
274          * were interested in an output operand, so
275          * let's resolve projs.
276          */
277         if(pos < 0)
278                 pos = redir_proj((const ir_node **) &irn, pos);
279
280         bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
281
282         if(bo) {
283                 int i;
284
285                 req->type = arch_register_req_type_normal;
286                 req->cls  = bo->cls;
287
288                 for(i = 0; i < bo->n_pos; ++i)
289                         if(pos == bo->pos[i])
290                                 return req;
291         }
292
293         return NULL;
294 }
295
296 void
297 be_node_set_irn_reg(const arch_irn_ops_t *_self, ir_node *irn,
298                 const arch_register_t *reg)
299 {
300         int pos;
301         be_op_t *bo;
302         be_node_attr_t *attr;
303         const be_node_factory_t *factory =
304                 container_of(_self, const be_node_factory_t, irn_ops);
305
306         if(get_irn_mode(irn) == mode_T && pos < 0)
307                 return;
308
309         pos = redir_proj((const ir_node **) &irn, -1);
310         bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
311
312         if(!bo)
313                 return;
314
315         attr = (be_node_attr_t *) &irn->attr;
316         attr->reg[-pos - 1] = reg;
317 }
318
319 const arch_register_t *
320 be_node_get_irn_reg(const arch_irn_ops_t *_self, const ir_node *irn)
321 {
322         int i, pos;
323         be_op_t *bo;
324         const be_node_factory_t *factory =
325                 container_of(_self, const be_node_factory_t, irn_ops);
326
327         if(get_irn_mode(irn) == mode_T && pos < 0)
328                 return NULL;
329
330         pos = redir_proj((const ir_node **) &irn, -1);
331         bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
332
333         if(!bo)
334                 return NULL;
335
336         for(i = 0; i < bo->n_pos; ++i) {
337                 if(bo->pos[i] == pos) {
338                         be_node_attr_t *attr = (be_node_attr_t *) &irn->attr;
339                         return attr->reg[-pos - 1];
340                 }
341         }
342
343         return NULL;
344 }
345
346 arch_irn_class_t be_node_classify(const arch_irn_ops_t *_self, const ir_node *irn)
347 {
348         const be_node_factory_t *factory = container_of(_self, const be_node_factory_t, irn_ops);
349
350         be_op_t *bo;
351         int idx;
352
353         idx = redir_proj(&irn, 0);
354         bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
355
356         switch(bo->kind) {
357 #define XXX(a) case node_kind_ ## a: return arch_irn_class_ ## a;
358                 XXX(spill)
359                 XXX(reload)
360                 XXX(perm)
361                 XXX(copy)
362 #undef XXX
363                 default:
364                 return 0;
365         }
366
367         return 0;
368 }
369
370 arch_irn_class_t be_node_get_flags(const arch_irn_ops_t *_self, const ir_node *irn)
371 {
372         return 0;
373 }
374
375 static const arch_irn_ops_t *
376 be_node_get_irn_ops(const arch_irn_handler_t *_self, const ir_node *irn)
377 {
378         be_op_t *bo;
379         const be_node_factory_t *factory =
380                 container_of(_self, const be_node_factory_t, handler);
381
382         redir_proj(&irn, 0);
383         bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
384
385         return bo ? &factory->irn_ops : NULL;
386 }
387
388 const arch_irn_handler_t *be_node_get_irn_handler(const be_node_factory_t *f)
389 {
390         return &f->handler;
391 }
392
393 int is_Spill(const be_node_factory_t *f, const ir_node *irn)
394 {
395         be_op_t *bo;
396         bo = pmap_get(f->irn_op_map, get_irn_op(irn));
397         return bo != NULL && bo->kind == node_kind_spill;
398 }
399
400 be_node_factory_t *be_node_factory_init(be_node_factory_t *factory, const arch_isa_t *isa)
401 {
402         int i, j, n;
403
404         factory->ops = new_set(cmp_op_map, 64);
405         factory->irn_op_map = pmap_create();
406         obstack_init(&factory->obst);
407
408         factory->handler.get_irn_ops = be_node_get_irn_ops;
409
410         factory->irn_ops.get_irn_reg_req = be_node_get_irn_reg_req;
411         factory->irn_ops.set_irn_reg     = be_node_set_irn_reg;
412         factory->irn_ops.get_irn_reg     = be_node_get_irn_reg;
413         factory->irn_ops.classify        = be_node_classify;
414         factory->irn_ops.get_flags       = be_node_get_flags;
415
416         for(i = 0, n = arch_isa_get_n_reg_class(isa); i < n; ++i) {
417                 const arch_register_class_t *cls = arch_isa_get_reg_class(isa, i);
418                 be_op_t *ent;
419
420                 ent = get_op(factory, cls, node_kind_spill);
421                 ent->op = new_ir_op(get_next_ir_opcode(), "Spill", op_pin_state_pinned,
422                                 0, oparity_unary, 0, sizeof(be_spill_attr_t), &be_node_ops);
423                 ent->n_pos = ARRSIZE(templ_pos_Spill);
424                 ent->pos = templ_pos_Spill;
425                 pmap_insert(factory->irn_op_map, ent->op, ent);
426
427                 ent = get_op(factory, cls, node_kind_reload);
428                 ent->op = new_ir_op(get_next_ir_opcode(), "Reload", op_pin_state_pinned, 0,
429                                 oparity_unary, 0, sizeof(be_node_attr_t), &be_node_ops);
430                 ent->n_pos = ARRSIZE(templ_pos_Reload);
431                 ent->pos = templ_pos_Reload;
432                 pmap_insert(factory->irn_op_map, ent->op, ent);
433
434                 ent = get_op(factory, cls, node_kind_copy);
435                 ent->op = new_ir_op(get_next_ir_opcode(), "Copy", op_pin_state_pinned, 0,
436                                 oparity_unary, 0, sizeof(be_node_attr_t), &be_node_ops);
437                 ent->n_pos = ARRSIZE(templ_pos_Copy);
438                 ent->pos = templ_pos_Copy;
439                 pmap_insert(factory->irn_op_map, ent->op, ent);
440
441                 ent = get_op(factory, cls, node_kind_perm);
442                 ent->op = new_ir_op(get_next_ir_opcode(), "Perm", op_pin_state_pinned, 0,
443                                 oparity_variable, 0,
444                                 sizeof(be_node_attr_t) + sizeof(arch_register_t) * cls->n_regs, &be_node_ops);
445                 ent->n_pos = 2 * cls->n_regs;
446                 ent->pos = obstack_alloc(&factory->obst, sizeof(ent->pos[0]) * ent->n_pos);
447                 for(j = 0; j < ent->n_pos; j += 2) {
448                         int k = j / 2;
449                         ent->pos[j] = k;
450                         ent->pos[j + 1] = -(k + 1);
451                 }
452                 pmap_insert(factory->irn_op_map, ent->op, ent);
453
454         }
455
456         return factory;
457 }
458
459 static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason)
460 {
461         be_node_attr_t *attr = (be_node_attr_t *) &irn->attr;
462         be_op_t *bo          = pmap_get(attr->factory->irn_op_map, get_irn_op(irn));
463
464         int i;
465
466         switch(reason) {
467                 case dump_node_opcode_txt:
468                         fprintf(f, get_op_name(bo->op));
469                         break;
470                 case dump_node_mode_txt:
471                         fprintf(f, get_mode_name(get_irn_mode(irn)));
472                         break;
473                 case dump_node_nodeattr_txt:
474                         fprintf(f, "%s ", bo->cls->name);
475                         break;
476                 case dump_node_info_txt:
477                         for(i = 0; i < attr->n_regs; ++i) {
478                                 const arch_register_t *reg = attr->reg[i];
479                                 fprintf(f, "reg #%d: %s\n", i, reg ? reg->name : "n/a");
480                         }
481
482                         if(bo->kind == node_kind_spill) {
483                                 be_spill_attr_t *a = (be_spill_attr_t *) attr;
484                                 ir_fprintf(f, "spill context: %+F\n", a->spill_ctx);
485                         }
486                         break;
487         }
488
489         return 1;
490 }
491
492 ir_node *insert_Perm_after(const be_main_env_t *env,
493                                                          const arch_register_class_t *cls,
494                                                          dom_front_info_t *dom_front,
495                                                          ir_node *pos)
496 {
497         const arch_env_t *arch_env  = env->arch_env;
498         ir_node *bl                 = is_Block(pos) ? pos : get_nodes_block(pos);
499         ir_graph *irg               = get_irn_irg(bl);
500         pset *live                  = pset_new_ptr_default();
501         firm_dbg_module_t *dbg      = firm_dbg_register("firm.be.node");
502
503         irn_live_t *li;
504         ir_node *curr, *irn, *perm, **nodes;
505         int i, n;
506
507         firm_dbg_set_mask(dbg, DBG_LEVEL);
508         DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
509
510
511         live_foreach(bl, li) {
512                 ir_node *irn = (ir_node *) li->irn;
513                 if(live_is_end(li) && arch_irn_has_reg_class(arch_env, irn, -1, cls))
514                         pset_insert_ptr(live, irn);
515         }
516
517         sched_foreach_reverse(bl, irn) {
518                 ir_node *x;
519
520         /*
521          * If we encounter the node we want to insert the Perm after,
522          * exit immediately, so that this node is still live
523          */
524                 if(irn == pos)
525                         break;
526
527                 DBG((dbg, LEVEL_1, "%+F\n", irn));
528                 for(x = pset_first(live); x; x = pset_next(live))
529                         DBG((dbg, LEVEL_1, "\tlive: %+F\n", x));
530
531                 if(arch_irn_has_reg_class(arch_env, irn, -1, cls))
532                         pset_remove_ptr(live, irn);
533
534                 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
535                         ir_node *op = get_irn_n(irn, i);
536
537                         if(arch_irn_has_reg_class(arch_env, op, -1, cls))
538                                 pset_insert_ptr(live, op);
539                 }
540         }
541
542         n = pset_count(live);
543         nodes = malloc(n * sizeof(nodes[0]));
544
545         DBG((dbg, LEVEL_1, "live:\n"));
546         for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++) {
547                 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
548                 nodes[i] = irn;
549         }
550
551         perm = new_Perm(env->node_factory, cls, irg, bl, n, nodes);
552         sched_add_after(pos, perm);
553         free(nodes);
554
555         curr = perm;
556         for(i = 0; i < n; ++i) {
557                 ir_node *copies[1];
558                 ir_node *perm_op = get_irn_n(perm, i);
559         const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op);
560
561                 ir_mode *mode = get_irn_mode(perm_op);
562                 ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
563                 arch_set_irn_register(arch_env, proj, reg);
564
565                 sched_add_after(curr, proj);
566                 curr = proj;
567
568                 copies[0] = proj;
569                 be_introduce_copies(dom_front, perm_op, array_size(copies), copies);
570         }
571         return perm;
572 }