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