Changed API a little
[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  * This file provdies Perm, Copy, Spill and Reload nodes.
9  *
10  * Copyright (C) 2005 Universitaet Karlsruhe
11  * Released under the GPL
12  */
13
14 #ifdef HAVE_CONFIG_H
15 #include "config.h"
16 #endif
17
18 #include <stdlib.h>
19
20 #include "obst.h"
21 #include "set.h"
22 #include "pmap.h"
23 #include "util.h"
24 #include "debug.h"
25 #include "fourcc.h"
26
27 #include "irop_t.h"
28 #include "irmode_t.h"
29 #include "irnode_t.h"
30 #include "ircons_t.h"
31 #include "irprintf.h"
32
33 #include "be_t.h"
34 #include "belive_t.h"
35 #include "besched_t.h"
36 #include "benode_t.h"
37
38 #include "beirgmod.h"
39
40 /* Sometimes we want to put const nodes into get_irn_generic_attr ... */
41 #define get_irn_attr(irn) get_irn_generic_attr((ir_node *) (irn))
42
43 static unsigned be_node_tag = FOURCC('B', 'E', 'N', 'O');
44
45 typedef enum _node_kind_t {
46         node_kind_spill,
47         node_kind_reload,
48         node_kind_perm,
49         node_kind_copy,
50         node_kind_kill,
51         node_kind_last
52 } node_kind_t;
53
54 typedef struct {
55         node_kind_t kind;
56         const arch_register_class_t *cls;
57         ir_op *op;
58         int n_pos;
59         int *pos;
60 } be_op_t;
61
62 typedef struct {
63         const arch_register_t *reg;
64         arch_register_req_t   req;
65 } be_reg_data_t;
66
67 typedef struct {
68         int                         n_outs;
69         const arch_register_class_t *cls;
70         be_reg_data_t               *reg_data;
71 } be_node_attr_t;
72
73 typedef struct {
74         be_node_attr_t node_attr;
75         ir_node *spill_ctx;  /**< The node in whose context this spill was introduced. */
76         entity *ent;     /**< The entity in the stack frame the spill writes to. */
77 } be_spill_attr_t;
78
79 static ir_op *op_Spill;
80 static ir_op *op_Reload;
81 static ir_op *op_Perm;
82 static ir_op *op_Copy;
83 static ir_op *op_Keep;
84
85 static int beo_base = -1;
86
87 static const ir_op_ops be_node_op_ops;
88
89 void be_node_init(void) {
90         static int inited = 0;
91         int i;
92
93         if(inited)
94                 return;
95
96         inited = 1;
97
98         beo_base = get_next_ir_opcode();
99
100         /* Acquire all needed opcodes. We assume that they are consecutive! */
101         for(i = beo_Spill; i < beo_Last; ++i)
102                 get_next_ir_opcode();
103
104         op_Spill  = new_ir_op(beo_base + beo_Spill,  "Spill",  op_pin_state_mem_pinned, 0, oparity_unary,    0, sizeof(be_spill_attr_t), &be_node_op_ops);
105         op_Reload = new_ir_op(beo_base + beo_Reload, "Reload", op_pin_state_mem_pinned, 0, oparity_zero,     0, sizeof(be_node_attr_t),  &be_node_op_ops);
106         op_Perm   = new_ir_op(beo_base + beo_Perm,   "Perm",   op_pin_state_pinned,     0, oparity_variable, 0, sizeof(be_node_attr_t),  &be_node_op_ops);
107         op_Copy   = new_ir_op(beo_base + beo_Copy,   "Copy",   op_pin_state_pinned,     0, oparity_unary,    0, sizeof(be_node_attr_t),  &be_node_op_ops);
108         op_Keep   = new_ir_op(beo_base + beo_Keep,   "Keep",   op_pin_state_pinned,     0, oparity_variable, 0, sizeof(be_node_attr_t),  &be_node_op_ops);
109
110         set_op_tag(op_Spill,  &be_node_tag);
111         set_op_tag(op_Reload, &be_node_tag);
112         set_op_tag(op_Perm,   &be_node_tag);
113         set_op_tag(op_Copy,   &be_node_tag);
114         set_op_tag(op_Keep,   &be_node_tag);
115 }
116
117 static void *init_node_attr(ir_node* irn, const arch_register_class_t *cls, ir_graph *irg, int n_outs)
118 {
119         be_node_attr_t *a = get_irn_attr(irn);
120
121         a->n_outs   = n_outs;
122         a->cls      = cls;
123         a->reg_data = NULL;
124
125         if(n_outs > 0) {
126                 int i;
127
128                 a->reg_data = NEW_ARR_D(be_reg_data_t, get_irg_obstack(irg), n_outs);
129                 memset(a->reg_data, 0, n_outs * sizeof(a->reg_data[0]));
130                 for(i = 0; i < n_outs; ++i) {
131                         a->reg_data[i].req.cls  = cls;
132                         a->reg_data[i].req.type = arch_register_req_type_normal;
133                 }
134         }
135
136         return a;
137 }
138
139 static INLINE int is_be_node(const ir_node *irn)
140 {
141         return get_op_tag(get_irn_op(irn)) == &be_node_tag;
142 }
143
144 be_opcode_t get_irn_be_opcode(const ir_node *irn)
145 {
146         return is_be_node(irn) ? get_irn_opcode(irn) - beo_base : beo_NoBeOp;
147 }
148
149 ir_node *be_new_Spill(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *to_spill, ir_node *ctx)
150 {
151         be_spill_attr_t *a;
152         ir_node *in[1];
153         ir_node *res;
154
155         in[0] = to_spill;
156         res   = new_ir_node(NULL, irg, bl, op_Spill, mode_M, 1, in);
157         a     = init_node_attr(res, cls, irg, 0);
158         a->ent    = NULL;
159         a->spill_ctx = ctx;
160         return res;
161 }
162
163 ir_node *be_new_Reload(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_mode *mode, ir_node *mem)
164 {
165         ir_node *in[1];
166         ir_node *res;
167
168         in[0] = mem;
169         res   = new_ir_node(NULL, irg, bl, op_Reload, mode, 1, in);
170         init_node_attr(res, cls, irg, 1);
171         return res;
172 }
173
174 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
175 {
176         ir_node *irn = new_ir_node(NULL, irg, bl, op_Perm, mode_T, n, in);
177         init_node_attr(irn, cls, irg, n);
178         return irn;
179 }
180
181 ir_node *be_new_Copy(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *op)
182 {
183         ir_node *in[1];
184         ir_node *res;
185
186         in[0] = op;
187         res   = new_ir_node(NULL, irg, bl, op_Copy, get_irn_mode(op), 1, in);
188         init_node_attr(res, cls, irg, 1);
189         return res;
190 }
191
192 ir_node *be_new_Keep(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
193 {
194         ir_node *irn;
195
196         irn = new_ir_node(NULL, irg, bl, op_Keep, mode_ANY, n, in);
197         init_node_attr(irn, cls, irg, 0);
198         keep_alive(irn);
199         return irn;
200 }
201
202 int be_is_Spill(const ir_node *irn)
203 {
204         return get_irn_be_opcode(irn) == beo_Spill;
205 }
206
207 int be_is_Reload(const ir_node *irn)
208 {
209         return get_irn_be_opcode(irn) == beo_Reload;
210 }
211
212 int be_is_Copy(const ir_node *irn)
213 {
214         return get_irn_be_opcode(irn) == beo_Copy;
215 }
216
217 int be_is_Perm(const ir_node *irn)
218 {
219         return get_irn_be_opcode(irn) == beo_Perm;
220 }
221
222 int be_is_Keep(const ir_node *irn)
223 {
224         return get_irn_be_opcode(irn) == beo_Keep;
225 }
226
227 void be_set_Perm_out_req(ir_node *irn, int pos, const arch_register_req_t *req)
228 {
229         be_node_attr_t *a = get_irn_attr(irn);
230
231         assert(be_is_Perm(irn));
232         assert(pos >= 0 && pos < get_irn_arity(irn));
233         memcpy(&a->reg_data[pos].req, req, sizeof(req[0]));
234 }
235
236 void be_set_Spill_entity(ir_node *irn, entity *ent)
237 {
238         be_spill_attr_t *a = get_irn_attr(irn);
239         assert(be_is_Spill(irn));
240         a->ent = ent;
241 }
242
243 static ir_node *find_a_spill_walker(ir_node *irn, unsigned visited_nr)
244 {
245         if(get_irn_visited(irn) < visited_nr) {
246                 set_irn_visited(irn, visited_nr);
247
248                 if(is_Phi(irn)) {
249                         int i, n;
250                         for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
251                                 ir_node *n = find_a_spill_walker(get_irn_n(irn, i), visited_nr);
252                                 if(n != NULL)
253                                         return n;
254                         }
255                 }
256
257                 else if(get_irn_be_opcode(irn) == beo_Spill)
258                         return irn;
259         }
260
261         return NULL;
262 }
263
264 ir_node *be_get_Spill_context(const ir_node *irn) {
265         const be_spill_attr_t *a = get_irn_attr(irn);
266         assert(be_is_Spill(irn));
267         return a->spill_ctx;
268 }
269
270 /**
271  * Finds a spill for a reload.
272  * If the reload is directly using the spill, this is simple,
273  * else we perform DFS from the reload (over all PhiMs) and return
274  * the first spill node we find.
275  */
276 static INLINE ir_node *find_a_spill(ir_node *irn)
277 {
278         ir_graph *irg       = get_irn_irg(irn);
279         unsigned visited_nr = get_irg_visited(irg) + 1;
280
281         assert(be_is_Reload(irn));
282         set_irg_visited(irg, visited_nr);
283         return find_a_spill_walker(irn, visited_nr);
284 }
285
286
287 entity *be_get_spill_entity(ir_node *irn)
288 {
289         int opc           = get_irn_opcode(irn);
290
291         switch(get_irn_be_opcode(irn)) {
292         case beo_Reload:
293                 return be_get_spill_entity(find_a_spill(irn));
294         case beo_Spill:
295                 {
296                         be_spill_attr_t *a = get_irn_attr(irn);
297                         return a->ent;
298                 }
299         default:
300                 assert(0 && "Must give spill/reload node");
301         }
302
303         return NULL;
304 }
305
306 ir_node *be_spill(const arch_env_t *arch_env, ir_node *irn, ir_node *ctx)
307 {
308         const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, irn, -1);
309
310         ir_node *bl    = get_nodes_block(irn);
311         ir_graph *irg  = get_irn_irg(bl);
312         ir_node *spill = be_new_Spill(cls, irg, bl, irn, ctx);
313         ir_node *insert;
314
315         /*
316          * search the right insertion point. a spill of a phi cannot be put
317          * directly after the phi, if there are some phis behind the one which
318          * is spilled.
319          */
320         insert = sched_next(irn);
321         while(is_Phi(insert) && !sched_is_end(insert))
322                 insert = sched_next(insert);
323
324         sched_add_before(insert, spill);
325         return spill;
326 }
327
328 ir_node *be_reload(const arch_env_t *arch_env,
329                                    const arch_register_class_t *cls,
330                                    ir_node *irn, int pos, ir_mode *mode, ir_node *spill)
331 {
332         ir_node *reload;
333
334         ir_node *bl   = get_nodes_block(irn);
335         ir_graph *irg = get_irn_irg(bl);
336
337         assert(be_is_Spill(spill) || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
338
339         reload = be_new_Reload(cls, irg, bl, mode, spill);
340
341         set_irn_n(irn, pos, reload);
342         sched_add_before(irn, reload);
343         return reload;
344 }
345
346 static int redir_proj(const ir_node **node, int pos)
347 {
348         const ir_node *n = *node;
349
350         if(is_Proj(n)) {
351                 assert(pos == -1 && "Illegal pos for a Proj");
352                 *node = get_Proj_pred(n);
353                 return get_Proj_proj(n);
354         }
355
356         return 0;
357 }
358
359 static void *put_out_reg_req(arch_register_req_t *req, const ir_node *irn, int out_pos)
360 {
361         const be_node_attr_t *a = get_irn_attr(irn);
362
363
364         if(out_pos < a->n_outs)
365                 memcpy(req, &a->reg_data[out_pos].req, sizeof(req[0]));
366         else {
367                 req->type = arch_register_req_type_none;
368                 req->cls  = NULL;
369         }
370
371         return req;
372 }
373
374 static void *put_in_reg_req(arch_register_req_t *req, const ir_node *irn, int pos)
375 {
376         const be_node_attr_t *a = get_irn_attr(irn);
377         int n                   = get_irn_arity(irn);
378
379         req->type = arch_register_req_type_none;
380         req->cls  = NULL;
381
382         switch(get_irn_be_opcode(irn)) {
383         case beo_Spill:
384         case beo_Copy:
385         case beo_Keep:
386         case beo_Perm:
387                 if(pos < n) {
388                         req->type = arch_register_req_type_normal;
389                         req->cls  = a->cls;
390                 }
391                 break;
392         case beo_Reload:
393         default:
394                 req = NULL;
395         }
396
397         return req;
398 }
399
400 static const arch_register_req_t *
401 be_node_get_irn_reg_req(const void *self, arch_register_req_t *req, const ir_node *irn, int pos)
402 {
403         int out_pos = pos;
404
405         if(pos < 0) {
406                 if(get_irn_mode(irn) == mode_T)
407                         return NULL;
408
409                 out_pos = redir_proj((const ir_node **) &irn, pos);
410                 assert(is_be_node(irn));
411                 return put_out_reg_req(req, irn, out_pos);
412         }
413
414         else {
415                 return is_be_node(irn) ? put_in_reg_req(req, irn, pos) : NULL;
416         }
417
418         return req;
419 }
420
421 static void
422 be_node_set_irn_reg(const void *_self, ir_node *irn, const arch_register_t *reg)
423 {
424         int out_pos;
425         be_node_attr_t *a;
426
427         out_pos = redir_proj((const ir_node **) &irn, -1);
428         a       = get_irn_attr(irn);
429
430         assert(is_be_node(irn));
431         assert(out_pos < a->n_outs && "position too high");
432         a->reg_data[out_pos].reg = reg;
433 }
434
435 const arch_register_t *
436 be_node_get_irn_reg(const void *_self, const ir_node *irn)
437 {
438         int out_pos;
439         be_node_attr_t *a;
440
441         out_pos = redir_proj((const ir_node **) &irn, -1);
442         a       = get_irn_attr(irn);
443
444         assert(is_be_node(irn));
445         assert(out_pos < a->n_outs && "position too high");
446
447         return a->reg_data[out_pos].reg;
448 }
449
450 arch_irn_class_t be_node_classify(const void *_self, const ir_node *irn)
451 {
452         redir_proj((const ir_node **) &irn, -1);
453
454         switch(get_irn_be_opcode(irn)) {
455 #define XXX(a,b) case beo_ ## a: return arch_irn_class_ ## b;
456                 XXX(Spill, spill)
457                 XXX(Reload, reload)
458                 XXX(Perm, perm)
459                 XXX(Copy, copy)
460 #undef XXX
461                 default:
462                 return 0;
463         }
464
465         return 0;
466 }
467
468 arch_irn_class_t be_node_get_flags(const void *_self, const ir_node *irn)
469 {
470         return 0;
471 }
472
473 static const arch_irn_ops_if_t be_node_irn_ops_if = {
474         be_node_get_irn_reg_req,
475         be_node_set_irn_reg,
476         be_node_get_irn_reg,
477         be_node_classify,
478         be_node_get_flags,
479 };
480
481 static const arch_irn_ops_t be_node_irn_ops = {
482         &be_node_irn_ops_if
483 };
484
485 const void *be_node_get_arch_ops(const arch_irn_handler_t *self, const ir_node *irn)
486 {
487         redir_proj((const ir_node **) &irn, -1);
488         return is_be_node(irn) ? &be_node_irn_ops : NULL;
489 }
490
491 const arch_irn_handler_t be_node_irn_handler = {
492         be_node_get_arch_ops
493 };
494
495 static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason)
496 {
497         be_node_attr_t *at = get_irn_attr(irn);
498         int i;
499
500         assert(is_be_node(irn));
501
502         switch(reason) {
503                 case dump_node_opcode_txt:
504                         fprintf(f, get_op_name(get_irn_op(irn)));
505                         break;
506                 case dump_node_mode_txt:
507                         fprintf(f, get_mode_name(get_irn_mode(irn)));
508                         break;
509                 case dump_node_nodeattr_txt:
510                         break;
511                 case dump_node_info_txt:
512                         fprintf(f, "reg class: %s\n", at->cls->name);
513                         for(i = 0; i < at->n_outs; ++i) {
514                                 const arch_register_t *reg = at->reg_data[i].reg;
515                                 fprintf(f, "reg #%d: %s\n", i, reg ? reg->name : "n/a");
516                         }
517
518                         if(get_irn_be_opcode(irn) == beo_Spill) {
519                                 be_spill_attr_t *a = (be_spill_attr_t *) at;
520                                 ir_fprintf(f, "spill context: %+F\n", a->spill_ctx);
521 //TODO                          ir_fprintf(f, "spill offset: %04x (%u)\n", a->offset, a->offset);
522                         }
523                         break;
524         }
525
526         return 0;
527 }
528
529 static const ir_op_ops be_node_op_ops = {
530         NULL,
531         NULL,
532         NULL,
533         NULL,
534         NULL,
535         NULL,
536         NULL,
537         NULL,
538         NULL,
539         NULL,
540         NULL,
541         dump_node,
542         NULL
543 };
544
545 pset *nodes_live_at(const arch_env_t *arch_env, const arch_register_class_t *cls, const ir_node *pos, pset *live)
546 {
547         firm_dbg_module_t *dbg = firm_dbg_register("firm.be.node");
548         ir_node *bl            = get_nodes_block(pos);
549         ir_node *irn;
550         irn_live_t *li;
551
552         live_foreach(bl, li) {
553                 ir_node *irn = (ir_node *) li->irn;
554                 if(live_is_end(li) && arch_irn_consider_in_reg_alloc(arch_env, cls, irn))
555                         pset_insert_ptr(live, irn);
556         }
557
558         sched_foreach_reverse(bl, irn) {
559                 int i, n;
560                 ir_node *x;
561
562                 /*
563                  * If we encounter the node we want to insert the Perm after,
564                 * exit immediately, so that this node is still live
565                 */
566                 if(irn == pos)
567                         return live;
568
569                 DBG((dbg, LEVEL_1, "%+F\n", irn));
570                 for(x = pset_first(live); x; x = pset_next(live))
571                         DBG((dbg, LEVEL_1, "\tlive: %+F\n", x));
572
573                 if(arch_irn_has_reg_class(arch_env, irn, -1, cls))
574                         pset_remove_ptr(live, irn);
575
576                 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
577                         ir_node *op = get_irn_n(irn, i);
578
579                         if(arch_irn_consider_in_reg_alloc(arch_env, cls, op))
580                                 pset_insert_ptr(live, op);
581                 }
582         }
583
584         return NULL;
585 }
586
587 ir_node *insert_Perm_after(const arch_env_t *arch_env,
588                                                    const arch_register_class_t *cls,
589                                                    dom_front_info_t *dom_front,
590                                                    ir_node *pos)
591 {
592         ir_node *bl                 = is_Block(pos) ? pos : get_nodes_block(pos);
593         ir_graph *irg               = get_irn_irg(bl);
594         pset *live                  = pset_new_ptr_default();
595         firm_dbg_module_t *dbg      = firm_dbg_register("be.node");
596
597         ir_node *curr, *irn, *perm, **nodes;
598         int i, n;
599
600         DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
601
602         if(!nodes_live_at(arch_env, cls, pos, live))
603                 assert(0 && "position not found");
604
605         n = pset_count(live);
606
607         if(n == 0)
608                 return NULL;
609
610         nodes = malloc(n * sizeof(nodes[0]));
611
612         DBG((dbg, LEVEL_1, "live:\n"));
613         for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++) {
614                 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
615                 nodes[i] = irn;
616         }
617
618         perm = be_new_Perm(cls, irg, bl, n, nodes);
619         sched_add_after(pos, perm);
620         free(nodes);
621
622         curr = perm;
623         for(i = 0; i < n; ++i) {
624                 ir_node *copies[1];
625                 ir_node *perm_op = get_irn_n(perm, i);
626                 const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op);
627
628                 ir_mode *mode = get_irn_mode(perm_op);
629                 ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
630                 arch_set_irn_register(arch_env, proj, reg);
631
632                 sched_add_after(curr, proj);
633                 curr = proj;
634
635                 copies[0] = proj;
636                 be_introduce_copies(dom_front, perm_op, 1, copies);
637         }
638         return perm;
639 }