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