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