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