Not yet finished
[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 enum {
63         be_req_kind_old_limited,
64         be_req_kind_negate_old_limited,
65         be_req_kind_single_reg
66 } be_req_kind_t;
67
68 typedef struct {
69         arch_register_req_t req;
70         be_req_kind_t       kind;
71         union {
72                 struct {
73                         void (*old_limited)(void *ptr, bitset_t *bs);
74                         void *old_limited_env;
75                 } old_limited;
76
77                 const arch_register_t *single_reg;
78         } x;
79 } be_req_t;
80
81 typedef struct {
82         const arch_register_t *reg;
83         be_req_t              req;
84         be_req_t              in_req;
85 } be_reg_data_t;
86
87 typedef struct {
88         int                         n_outs;
89         const arch_register_class_t *cls;
90         be_reg_data_t               *reg_data;
91 } be_node_attr_t;
92
93 typedef struct {
94         be_node_attr_t node_attr;
95         ir_node *spill_ctx;  /**< The node in whose context this spill was introduced. */
96         entity *ent;     /**< The entity in the stack frame the spill writes to. */
97 } be_spill_attr_t;
98
99 static ir_op *op_Spill;
100 static ir_op *op_Reload;
101 static ir_op *op_Perm;
102 static ir_op *op_Copy;
103 static ir_op *op_Keep;
104 static ir_op *op_Call;
105 static ir_op *op_IncSP;
106 static ir_op *op_AddSP;
107
108 static int beo_base = -1;
109
110 static const ir_op_ops be_node_op_ops;
111
112 #define N   irop_flag_none
113 #define L   irop_flag_labeled
114 #define C   irop_flag_commutative
115 #define X   irop_flag_cfopcode
116 #define I   irop_flag_ip_cfopcode
117 #define F   irop_flag_fragile
118 #define Y   irop_flag_forking
119 #define H   irop_flag_highlevel
120 #define c   irop_flag_constlike
121 #define K   irop_flag_keep
122
123 void be_node_init(void) {
124         static int inited = 0;
125
126         if(inited)
127                 return;
128
129         inited = 1;
130
131         /* Acquire all needed opcodes. */
132         beo_base = get_next_ir_opcodes(beo_Last - 1);
133
134         op_Spill  = new_ir_op(beo_base + beo_Spill,  "Spill",  op_pin_state_mem_pinned, N, oparity_unary,    0, sizeof(be_spill_attr_t), &be_node_op_ops);
135         op_Reload = new_ir_op(beo_base + beo_Reload, "Reload", op_pin_state_mem_pinned, N, oparity_zero,     0, sizeof(be_node_attr_t),  &be_node_op_ops);
136         op_Perm   = new_ir_op(beo_base + beo_Perm,   "Perm",   op_pin_state_pinned,     N, oparity_variable, 0, sizeof(be_node_attr_t),  &be_node_op_ops);
137         op_Copy   = new_ir_op(beo_base + beo_Copy,   "Copy",   op_pin_state_pinned,     N, oparity_unary,    0, sizeof(be_node_attr_t),  &be_node_op_ops);
138         op_Keep   = new_ir_op(beo_base + beo_Keep,   "Keep",   op_pin_state_pinned,     K, oparity_variable, 0, sizeof(be_node_attr_t),  &be_node_op_ops);
139
140         set_op_tag(op_Spill,  &be_node_tag);
141         set_op_tag(op_Reload, &be_node_tag);
142         set_op_tag(op_Perm,   &be_node_tag);
143         set_op_tag(op_Copy,   &be_node_tag);
144         set_op_tag(op_Keep,   &be_node_tag);
145 }
146
147 static void *init_node_attr(ir_node* irn, const arch_register_class_t *cls, ir_graph *irg, int n_outs)
148 {
149         be_node_attr_t *a = get_irn_attr(irn);
150
151         a->n_outs   = n_outs;
152         a->cls      = cls;
153         a->reg_data = NULL;
154
155         if(n_outs > 0) {
156                 int i;
157
158                 a->reg_data = NEW_ARR_D(be_reg_data_t, get_irg_obstack(irg), n_outs);
159                 memset(a->reg_data, 0, n_outs * sizeof(a->reg_data[0]));
160                 for(i = 0; i < n_outs; ++i) {
161                         a->reg_data[i].req.req.cls  = cls;
162                         a->reg_data[i].req.req.type = arch_register_req_type_normal;
163                 }
164         }
165
166         return a;
167 }
168
169 static INLINE int is_be_node(const ir_node *irn)
170 {
171         return get_op_tag(get_irn_op(irn)) == &be_node_tag;
172 }
173
174 be_opcode_t get_irn_be_opcode(const ir_node *irn)
175 {
176         return is_be_node(irn) ? get_irn_opcode(irn) - beo_base : beo_NoBeOp;
177 }
178
179 ir_node *be_new_Spill(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *to_spill, ir_node *ctx)
180 {
181         be_spill_attr_t *a;
182         ir_node *in[1];
183         ir_node *res;
184
185         in[0] = to_spill;
186         res   = new_ir_node(NULL, irg, bl, op_Spill, mode_M, 1, in);
187         a     = init_node_attr(res, cls, irg, 0);
188         a->ent       = NULL;
189         a->spill_ctx = ctx;
190         return res;
191 }
192
193 ir_node *be_new_Reload(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_mode *mode, ir_node *mem)
194 {
195         ir_node *in[1];
196         ir_node *res;
197
198         in[0] = mem;
199         res   = new_ir_node(NULL, irg, bl, op_Reload, mode, 1, in);
200         init_node_attr(res, cls, irg, 1);
201         return res;
202 }
203
204 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
205 {
206         ir_node *irn = new_ir_node(NULL, irg, bl, op_Perm, mode_T, n, in);
207         init_node_attr(irn, cls, irg, n);
208         return irn;
209 }
210
211 ir_node *be_new_Copy(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *op)
212 {
213         ir_node *in[1];
214         ir_node *res;
215
216         in[0] = op;
217         res   = new_ir_node(NULL, irg, bl, op_Copy, get_irn_mode(op), 1, in);
218         init_node_attr(res, cls, irg, 1);
219         return res;
220 }
221
222 ir_node *be_new_Keep(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
223 {
224         ir_node *irn;
225
226         irn = new_ir_node(NULL, irg, bl, op_Keep, mode_ANY, n, in);
227         init_node_attr(irn, cls, irg, 0);
228         keep_alive(irn);
229         return irn;
230 }
231
232 int be_is_Spill(const ir_node *irn)
233 {
234         return get_irn_be_opcode(irn) == beo_Spill;
235 }
236
237 int be_is_Reload(const ir_node *irn)
238 {
239         return get_irn_be_opcode(irn) == beo_Reload;
240 }
241
242 int be_is_Copy(const ir_node *irn)
243 {
244         return get_irn_be_opcode(irn) == beo_Copy;
245 }
246
247 int be_is_Perm(const ir_node *irn)
248 {
249         return get_irn_be_opcode(irn) == beo_Perm;
250 }
251
252 int be_is_Keep(const ir_node *irn)
253 {
254         return get_irn_be_opcode(irn) == beo_Keep;
255 }
256
257 static void be_limited(void *data, bitset_t *bs)
258 {
259         be_req_t *req = data;
260
261         switch(req->kind) {
262         case be_req_kind_negate_old_limited:
263         case be_req_kind_old_limited:
264                 req->x.old_limited.old_limited(req->x.old_limited.old_limited_env, bs);
265                 if(req->kind == be_req_kind_negate_old_limited)
266                         bitset_flip_all(bs);
267                 break;
268         case be_req_kind_single_reg:
269                 bitset_clear_all(bs);
270                 bitset_set(bs, req->x.single_reg->index);
271                 break;
272         }
273 }
274
275 void be_set_constr_single_reg(ir_node *irn, int pos, const arch_register_t *reg)
276 {
277         int idx           = pos < 0 ? -(pos - 1) : pos;
278         be_node_attr_t *a = get_irn_attr(irn);
279         be_reg_data_t *rd = &a->reg_data[idx];
280         be_req_t       *r = pos < 0 ? &rd->req : &rd->in_req;
281
282         assert(is_be_node(irn));
283         assert(!(pos >= 0) || pos < get_irn_arity(irn));
284         assert(!(pos < 0)  || -(pos + 1) <= a->n_outs);
285
286         r->kind            = be_req_kind_single_reg;
287         r->x.single_reg    = reg;
288         r->req.limited     = be_limited;
289         r->req.limited_env = r;
290         r->req.type        = arch_register_req_type_limited;
291         r->req.cls         = reg->reg_class;
292 }
293
294 void be_set_IncSP_offset(ir_node *irn, int offset)
295 {
296
297 }
298
299 int be_get_IncSP_offset(ir_node *irn)
300 {
301         return -1;
302 }
303
304 void be_set_Spill_entity(ir_node *irn, entity *ent)
305 {
306         be_spill_attr_t *a = get_irn_attr(irn);
307         assert(be_is_Spill(irn));
308         a->ent = ent;
309 }
310
311 static ir_node *find_a_spill_walker(ir_node *irn, unsigned visited_nr)
312 {
313         if(get_irn_visited(irn) < visited_nr) {
314                 set_irn_visited(irn, visited_nr);
315
316                 if(is_Phi(irn)) {
317                         int i, n;
318                         for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
319                                 ir_node *n = find_a_spill_walker(get_irn_n(irn, i), visited_nr);
320                                 if(n != NULL)
321                                         return n;
322                         }
323                 }
324
325                 else if(get_irn_be_opcode(irn) == beo_Spill)
326                         return irn;
327         }
328
329         return NULL;
330 }
331
332 ir_node *be_get_Spill_context(const ir_node *irn) {
333         const be_spill_attr_t *a = get_irn_attr(irn);
334         assert(be_is_Spill(irn));
335         return a->spill_ctx;
336 }
337
338 /**
339  * Finds a spill for a reload.
340  * If the reload is directly using the spill, this is simple,
341  * else we perform DFS from the reload (over all PhiMs) and return
342  * the first spill node we find.
343  */
344 static INLINE ir_node *find_a_spill(ir_node *irn)
345 {
346         ir_graph *irg       = get_irn_irg(irn);
347         unsigned visited_nr = get_irg_visited(irg) + 1;
348
349         assert(be_is_Reload(irn));
350         set_irg_visited(irg, visited_nr);
351         return find_a_spill_walker(irn, visited_nr);
352 }
353
354 entity *be_get_spill_entity(ir_node *irn)
355 {
356         int opc           = get_irn_opcode(irn);
357
358         switch(get_irn_be_opcode(irn)) {
359         case beo_Reload:
360                 return be_get_spill_entity(find_a_spill(irn));
361         case beo_Spill:
362                 {
363                         be_spill_attr_t *a = get_irn_attr(irn);
364                         return a->ent;
365                 }
366         default:
367                 assert(0 && "Must give spill/reload node");
368         }
369
370         return NULL;
371 }
372
373 ir_node *be_spill(const arch_env_t *arch_env, ir_node *irn, ir_node *ctx)
374 {
375         const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, irn, -1);
376
377         ir_node *bl    = get_nodes_block(irn);
378         ir_graph *irg  = get_irn_irg(bl);
379         ir_node *spill = be_new_Spill(cls, irg, bl, irn, ctx);
380         ir_node *insert;
381
382         /*
383          * search the right insertion point. a spill of a phi cannot be put
384          * directly after the phi, if there are some phis behind the one which
385          * is spilled.
386          */
387         insert = sched_next(irn);
388         while(is_Phi(insert) && !sched_is_end(insert))
389                 insert = sched_next(insert);
390
391         sched_add_before(insert, spill);
392         return spill;
393 }
394
395 ir_node *be_reload(const arch_env_t *arch_env,
396                                    const arch_register_class_t *cls,
397                                    ir_node *irn, int pos, ir_mode *mode, ir_node *spill)
398 {
399         ir_node *reload;
400
401         ir_node *bl   = get_nodes_block(irn);
402         ir_graph *irg = get_irn_irg(bl);
403
404         assert(be_is_Spill(spill) || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
405
406         reload = be_new_Reload(cls, irg, bl, mode, spill);
407
408         set_irn_n(irn, pos, reload);
409         sched_add_before(irn, reload);
410         return reload;
411 }
412
413 static int redir_proj(const ir_node **node, int pos)
414 {
415         const ir_node *n = *node;
416
417         if(is_Proj(n)) {
418                 assert(pos == -1 && "Illegal pos for a Proj");
419                 *node = get_Proj_pred(n);
420                 return get_Proj_proj(n);
421         }
422
423         return 0;
424 }
425
426 static void *put_out_reg_req(arch_register_req_t *req, const ir_node *irn, int out_pos)
427 {
428         const be_node_attr_t *a = get_irn_attr(irn);
429
430         if(out_pos < a->n_outs)
431                 memcpy(req, &a->reg_data[out_pos].req, sizeof(req[0]));
432         else {
433                 req->type = arch_register_req_type_none;
434                 req->cls  = NULL;
435         }
436
437         return req;
438 }
439
440 static void *put_in_reg_req(arch_register_req_t *req, const ir_node *irn, int pos)
441 {
442         const be_node_attr_t *a = get_irn_attr(irn);
443         int n                   = get_irn_arity(irn);
444
445         if(pos < get_irn_arity(irn))
446                 memcpy(req, &a->reg_data[pos].in_req, sizeof(req[0]));
447         else {
448                 req->type = arch_register_req_type_none;
449                 req->cls  = NULL;
450         }
451
452         return req;
453 }
454
455 static const arch_register_req_t *
456 be_node_get_irn_reg_req(const void *self, arch_register_req_t *req, const ir_node *irn, int pos)
457 {
458         int out_pos = pos;
459
460         if(pos < 0) {
461                 if(get_irn_mode(irn) == mode_T)
462                         return NULL;
463
464                 out_pos = redir_proj((const ir_node **) &irn, pos);
465                 assert(is_be_node(irn));
466                 return put_out_reg_req(req, irn, out_pos);
467         }
468
469         else {
470                 return is_be_node(irn) ? put_in_reg_req(req, irn, pos) : NULL;
471         }
472
473         return req;
474 }
475
476 static void
477 be_node_set_irn_reg(const void *_self, ir_node *irn, const arch_register_t *reg)
478 {
479         int out_pos;
480         be_node_attr_t *a;
481
482         out_pos = redir_proj((const ir_node **) &irn, -1);
483         a       = get_irn_attr(irn);
484
485         assert(is_be_node(irn));
486         assert(out_pos < a->n_outs && "position too high");
487         a->reg_data[out_pos].reg = reg;
488 }
489
490 const arch_register_t *
491 be_node_get_irn_reg(const void *_self, const ir_node *irn)
492 {
493         int out_pos;
494         be_node_attr_t *a;
495
496         out_pos = redir_proj((const ir_node **) &irn, -1);
497         a       = get_irn_attr(irn);
498
499         assert(is_be_node(irn));
500         assert(out_pos < a->n_outs && "position too high");
501
502         return a->reg_data[out_pos].reg;
503 }
504
505 arch_irn_class_t be_node_classify(const void *_self, const ir_node *irn)
506 {
507         redir_proj((const ir_node **) &irn, -1);
508
509         switch(get_irn_be_opcode(irn)) {
510 #define XXX(a,b) case beo_ ## a: return arch_irn_class_ ## b;
511                 XXX(Spill, spill)
512                 XXX(Reload, reload)
513                 XXX(Perm, perm)
514                 XXX(Copy, copy)
515 #undef XXX
516                 default:
517                 return 0;
518         }
519
520         return 0;
521 }
522
523 arch_irn_class_t be_node_get_flags(const void *_self, const ir_node *irn)
524 {
525         return 0;
526 }
527
528 static const arch_irn_ops_if_t be_node_irn_ops_if = {
529         be_node_get_irn_reg_req,
530         be_node_set_irn_reg,
531         be_node_get_irn_reg,
532         be_node_classify,
533         be_node_get_flags,
534 };
535
536 static const arch_irn_ops_t be_node_irn_ops = {
537         &be_node_irn_ops_if
538 };
539
540 const void *be_node_get_arch_ops(const arch_irn_handler_t *self, const ir_node *irn)
541 {
542         redir_proj((const ir_node **) &irn, -1);
543         return is_be_node(irn) ? &be_node_irn_ops : NULL;
544 }
545
546 const arch_irn_handler_t be_node_irn_handler = {
547         be_node_get_arch_ops
548 };
549
550 static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason)
551 {
552         be_node_attr_t *at = get_irn_attr(irn);
553         int i;
554
555         assert(is_be_node(irn));
556
557         switch(reason) {
558                 case dump_node_opcode_txt:
559                         fprintf(f, get_op_name(get_irn_op(irn)));
560                         break;
561                 case dump_node_mode_txt:
562                         fprintf(f, get_mode_name(get_irn_mode(irn)));
563                         break;
564                 case dump_node_nodeattr_txt:
565                         break;
566                 case dump_node_info_txt:
567                         fprintf(f, "reg class: %s\n", at->cls->name);
568                         for(i = 0; i < at->n_outs; ++i) {
569                                 const arch_register_t *reg = at->reg_data[i].reg;
570                                 fprintf(f, "reg #%d: %s\n", i, reg ? reg->name : "n/a");
571                         }
572
573                         if(get_irn_be_opcode(irn) == beo_Spill) {
574                                 be_spill_attr_t *a = (be_spill_attr_t *) at;
575
576                                 ir_fprintf(f, "spill context: %+F\n", a->spill_ctx);
577                                 if (a->ent) {
578                                         unsigned ofs = get_entity_offset_bytes(a->ent);
579                                         ir_fprintf(f, "spill entity: %+F offset %x (%d)\n", a->ent, ofs, ofs);
580                                 }
581                                 else {
582                                         ir_fprintf(f, "spill entity: n/a\n");
583                                 }
584                         }
585                         break;
586         }
587
588         return 0;
589 }
590
591 static const ir_op_ops be_node_op_ops = {
592         NULL,
593         NULL,
594         NULL,
595         NULL,
596         NULL,
597         NULL,
598         NULL,
599         NULL,
600         NULL,
601         NULL,
602         NULL,
603         dump_node,
604         NULL
605 };
606
607 pset *nodes_live_at(const arch_env_t *arch_env, const arch_register_class_t *cls, const ir_node *pos, pset *live)
608 {
609         firm_dbg_module_t *dbg = firm_dbg_register("firm.be.node");
610         const ir_node *bl      = is_Block(pos) ? pos : get_nodes_block(pos);
611         ir_node *irn;
612         irn_live_t *li;
613
614         live_foreach(bl, li) {
615                 ir_node *irn = (ir_node *) li->irn;
616                 if(live_is_end(li) && arch_irn_consider_in_reg_alloc(arch_env, cls, irn))
617                         pset_insert_ptr(live, irn);
618         }
619
620         sched_foreach_reverse(bl, irn) {
621                 int i, n;
622                 ir_node *x;
623
624                 /*
625                  * If we encounter the node we want to insert the Perm after,
626                 * exit immediately, so that this node is still live
627                 */
628                 if(irn == pos)
629                         return live;
630
631                 DBG((dbg, LEVEL_1, "%+F\n", irn));
632                 for(x = pset_first(live); x; x = pset_next(live))
633                         DBG((dbg, LEVEL_1, "\tlive: %+F\n", x));
634
635                 if(arch_irn_has_reg_class(arch_env, irn, -1, cls))
636                         pset_remove_ptr(live, irn);
637
638                 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
639                         ir_node *op = get_irn_n(irn, i);
640
641                         if(arch_irn_consider_in_reg_alloc(arch_env, cls, op))
642                                 pset_insert_ptr(live, op);
643                 }
644         }
645
646         return live;
647 }
648
649 ir_node *insert_Perm_after(const arch_env_t *arch_env,
650                                                    const arch_register_class_t *cls,
651                                                    dom_front_info_t *dom_front,
652                                                    ir_node *pos)
653 {
654         ir_node *bl                 = is_Block(pos) ? pos : get_nodes_block(pos);
655         ir_graph *irg               = get_irn_irg(bl);
656         pset *live                  = pset_new_ptr_default();
657         firm_dbg_module_t *dbg      = firm_dbg_register("be.node");
658
659         ir_node *curr, *irn, *perm, **nodes;
660         int i, n;
661
662         DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
663
664         if(!nodes_live_at(arch_env, cls, pos, live));
665
666         n = pset_count(live);
667
668         if(n == 0)
669                 return NULL;
670
671         nodes = malloc(n * sizeof(nodes[0]));
672
673         DBG((dbg, LEVEL_1, "live:\n"));
674         for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++) {
675                 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
676                 nodes[i] = irn;
677         }
678
679         perm = be_new_Perm(cls, irg, bl, n, nodes);
680         sched_add_after(pos, perm);
681         free(nodes);
682
683         curr = perm;
684         for(i = 0; i < n; ++i) {
685                 ir_node *copies[1];
686                 ir_node *perm_op = get_irn_n(perm, i);
687                 const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op);
688
689                 ir_mode *mode = get_irn_mode(perm_op);
690                 ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
691                 arch_set_irn_register(arch_env, proj, reg);
692
693                 sched_add_after(curr, proj);
694                 curr = proj;
695
696                 copies[0] = proj;
697                 be_introduce_copies(dom_front, perm_op, 1, copies);
698         }
699         return perm;
700 }