function pointer at wrong position in static struct initialization
[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 =
349     container_of(_self, const be_node_factory_t, irn_ops);
350
351   be_op_t *bo;
352   int idx;
353
354   idx = redir_proj(&irn, 0);
355   bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
356
357         switch(bo->kind) {
358 #define XXX(a) case node_kind_ ## a: return arch_irn_class_ ## a;
359                 XXX(spill)
360                 XXX(reload)
361                 XXX(perm)
362                 XXX(copy)
363 #undef XXX
364                 default:
365                 return 0;
366         }
367
368   return 0;
369 }
370
371 arch_irn_class_t be_node_get_flags(const arch_irn_ops_t *_self, const ir_node *irn)
372 {
373         return 0;
374 }
375
376 static const arch_irn_ops_t *
377 be_node_get_irn_ops(const arch_irn_handler_t *_self, const ir_node *irn)
378 {
379   be_op_t *bo;
380   const be_node_factory_t *factory =
381     container_of(_self, const be_node_factory_t, handler);
382
383   redir_proj(&irn, 0);
384   bo = pmap_get(factory->irn_op_map, get_irn_op(irn));
385
386   return bo ? &factory->irn_ops : NULL;
387 }
388
389 const arch_irn_handler_t *be_node_get_irn_handler(const be_node_factory_t *f)
390 {
391   return &f->handler;
392 }
393
394 int is_Spill(const be_node_factory_t *f, const ir_node *irn)
395 {
396   be_op_t *bo;
397   bo = pmap_get(f->irn_op_map, get_irn_op(irn));
398   return bo != NULL && bo->kind == node_kind_spill;
399 }
400
401 be_node_factory_t *be_node_factory_init(be_node_factory_t *factory, const arch_isa_t *isa)
402 {
403   int i, j, n;
404
405   factory->ops = new_set(cmp_op_map, 64);
406   factory->irn_op_map = pmap_create();
407   obstack_init(&factory->obst);
408
409   factory->handler.get_irn_ops = be_node_get_irn_ops;
410
411   factory->irn_ops.get_irn_reg_req = be_node_get_irn_reg_req;
412   factory->irn_ops.set_irn_reg     = be_node_set_irn_reg;
413   factory->irn_ops.get_irn_reg     = be_node_get_irn_reg;
414   factory->irn_ops.classify        = be_node_classify;
415   factory->irn_ops.get_flags       = be_node_get_flags;
416
417   for(i = 0, n = arch_isa_get_n_reg_class(isa); i < n; ++i) {
418     const arch_register_class_t *cls = arch_isa_get_reg_class(isa, i);
419     be_op_t *ent;
420
421     ent = get_op(factory, cls, node_kind_spill);
422     ent->op = new_ir_op(get_next_ir_opcode(), "Spill", op_pin_state_pinned,
423         0, oparity_unary, 0, sizeof(be_spill_attr_t), &be_node_ops);
424     ent->n_pos = ARRSIZE(templ_pos_Spill);
425     ent->pos = templ_pos_Spill;
426     pmap_insert(factory->irn_op_map, ent->op, ent);
427
428     ent = get_op(factory, cls, node_kind_reload);
429     ent->op = new_ir_op(get_next_ir_opcode(), "Reload", op_pin_state_pinned, 0,
430         oparity_unary, 0, sizeof(be_node_attr_t), &be_node_ops);
431     ent->n_pos = ARRSIZE(templ_pos_Reload);
432     ent->pos = templ_pos_Reload;
433     pmap_insert(factory->irn_op_map, ent->op, ent);
434
435     ent = get_op(factory, cls, node_kind_copy);
436     ent->op = new_ir_op(get_next_ir_opcode(), "Copy", op_pin_state_pinned, 0,
437         oparity_unary, 0, sizeof(be_node_attr_t), &be_node_ops);
438     ent->n_pos = ARRSIZE(templ_pos_Copy);
439     ent->pos = templ_pos_Copy;
440     pmap_insert(factory->irn_op_map, ent->op, ent);
441
442     ent = get_op(factory, cls, node_kind_perm);
443     ent->op = new_ir_op(get_next_ir_opcode(), "Perm", op_pin_state_pinned, 0,
444         oparity_variable, 0,
445                                 sizeof(be_node_attr_t) + sizeof(arch_register_t) * cls->n_regs, &be_node_ops);
446     ent->n_pos = 2 * cls->n_regs;
447     ent->pos = obstack_alloc(&factory->obst, sizeof(ent->pos[0]) * ent->n_pos);
448     for(j = 0; j < ent->n_pos; j += 2) {
449         int k = j / 2;
450       ent->pos[j] = k;
451       ent->pos[j + 1] = -(k + 1);
452     }
453     pmap_insert(factory->irn_op_map, ent->op, ent);
454
455   }
456
457   return factory;
458 }
459
460 static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason)
461 {
462         be_node_attr_t *attr = (be_node_attr_t *) &irn->attr;
463   be_op_t *bo          = pmap_get(attr->factory->irn_op_map, get_irn_op(irn));
464
465         int i;
466
467         switch(reason) {
468                 case dump_node_opcode_txt:
469                         fprintf(f, get_op_name(bo->op));
470                         break;
471                 case dump_node_mode_txt:
472                         fprintf(f, get_mode_name(get_irn_mode(irn)));
473                         break;
474                 case dump_node_nodeattr_txt:
475                         fprintf(f, "%s ", bo->cls->name);
476                         break;
477                 case dump_node_info_txt:
478                         for(i = 0; i < attr->n_regs; ++i) {
479                                 const arch_register_t *reg = attr->reg[i];
480                                 fprintf(f, "reg #%d: %s\n", i, reg ? reg->name : "n/a");
481                         }
482
483                         if(bo->kind == node_kind_spill) {
484                                 be_spill_attr_t *a = (be_spill_attr_t *) attr;
485                                 ir_fprintf(f, "spill context: %+F\n", a->spill_ctx);
486                         }
487                         break;
488         }
489
490         return 1;
491 }
492
493 ir_node *insert_Perm_after(const be_main_env_t *env,
494                                                    const arch_register_class_t *cls,
495                                                    dom_front_info_t *dom_front,
496                                                    ir_node *pos)
497 {
498   const arch_env_t *arch_env  = env->arch_env;
499   ir_node *bl                 = is_Block(pos) ? pos : get_nodes_block(pos);
500   ir_graph *irg               = get_irn_irg(bl);
501   pset *live                  = pset_new_ptr_default();
502   firm_dbg_module_t *dbg      = firm_dbg_register("firm.be.node");
503
504   irn_live_t *li;
505   ir_node *curr, *irn, *perm, **nodes;
506   int i, n;
507
508   firm_dbg_set_mask(dbg, DBG_LEVEL);
509   DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
510
511
512         live_foreach(bl, li) {
513                 ir_node *irn = (ir_node *) li->irn;
514                 if(live_is_end(li) && arch_irn_has_reg_class(arch_env, irn, -1, cls))
515                         pset_insert_ptr(live, irn);
516         }
517
518   sched_foreach_reverse(bl, irn) {
519     ir_node *x;
520
521         /*
522          * If we encounter the node we want to insert the Perm after,
523          * exit immediately, so that this node is still live
524          */
525     if(irn == pos)
526       break;
527
528     DBG((dbg, LEVEL_1, "%+F\n", irn));
529     for(x = pset_first(live); x; x = pset_next(live))
530       DBG((dbg, LEVEL_1, "\tlive: %+F\n", x));
531
532     if(arch_irn_has_reg_class(arch_env, irn, -1, cls))
533       pset_remove_ptr(live, irn);
534
535     for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
536       ir_node *op = get_irn_n(irn, i);
537
538       if(arch_irn_has_reg_class(arch_env, op, -1, cls))
539         pset_insert_ptr(live, op);
540     }
541   }
542
543   n = pset_count(live);
544   nodes = malloc(n * sizeof(nodes[0]));
545
546   DBG((dbg, LEVEL_1, "live:\n"));
547   for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++) {
548         DBG((dbg, LEVEL_1, "\t%+F\n", irn));
549     nodes[i] = irn;
550   }
551
552   perm = new_Perm(env->node_factory, cls, irg, bl, n, nodes);
553   sched_add_after(pos, perm);
554   free(nodes);
555
556   curr = perm;
557   for(i = 0; i < n; ++i) {
558     ir_node *copies[1];
559     ir_node *perm_op = get_irn_n(perm, i);
560         const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op);
561
562     ir_mode *mode = get_irn_mode(perm_op);
563     ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
564     arch_set_irn_register(arch_env, proj, reg);
565
566     sched_add_after(curr, proj);
567     curr = proj;
568
569     copies[0] = proj;
570     be_introduce_copies(dom_front, perm_op, array_size(copies), copies);
571   }
572   return perm;
573 }