b87dcfd6ca50a9b56e06958bae345148c3bc960f
[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                         max_reg_data;
89         arch_irn_flags_t            flags;
90         const arch_register_class_t *cls;
91         be_reg_data_t               *reg_data;
92 } be_node_attr_t;
93
94 typedef struct {
95         be_node_attr_t node_attr;
96         ir_node *spill_ctx;  /**< The node in whose context this spill was introduced. */
97         entity *ent;     /**< The entity in the stack frame the spill writes to. */
98 } be_spill_attr_t;
99
100 typedef struct {
101         be_node_attr_t node_attr;
102         int offset;           /**< The offset by which the stack shall be increased/decreased. */
103         be_stack_dir_t dir;   /**< The direction in which the stack shall be modified (along or in the other direction). */
104 } be_stack_attr_t;
105
106 static ir_op *op_Spill;
107 static ir_op *op_Reload;
108 static ir_op *op_Perm;
109 static ir_op *op_Copy;
110 static ir_op *op_Keep;
111 static ir_op *op_Call;
112 static ir_op *op_IncSP;
113 static ir_op *op_AddSP;
114 static ir_op *op_RegParams;
115 static ir_op *op_StackParam;
116 static ir_op *op_NoReg;
117
118 static int beo_base = -1;
119
120 static const ir_op_ops be_node_op_ops;
121
122 #define N   irop_flag_none
123 #define L   irop_flag_labeled
124 #define C   irop_flag_commutative
125 #define X   irop_flag_cfopcode
126 #define I   irop_flag_ip_cfopcode
127 #define F   irop_flag_fragile
128 #define Y   irop_flag_forking
129 #define H   irop_flag_highlevel
130 #define c   irop_flag_constlike
131 #define K   irop_flag_keep
132
133 void be_node_init(void) {
134         static int inited = 0;
135
136         if(inited)
137                 return;
138
139         inited = 1;
140
141         /* Acquire all needed opcodes. */
142         beo_base = get_next_ir_opcodes(beo_Last - 1);
143
144         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);
145         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);
146         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);
147         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);
148         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);
149         op_NoReg      = new_ir_op(beo_base + beo_Keep,   "NoReg",      op_pin_state_pinned,     K, oparity_variable, 0, sizeof(be_node_attr_t),  &be_node_op_ops);
150         op_Call       = new_ir_op(beo_base + beo_Keep,   "Call",       op_pin_state_pinned,     K, oparity_variable, 0, sizeof(be_node_attr_t),  &be_node_op_ops);
151         op_AddSP      = new_ir_op(beo_base + beo_Keep,   "AddSP",      op_pin_state_pinned,     K, oparity_variable, 0, sizeof(be_stack_attr_t), &be_node_op_ops);
152         op_IncSP      = new_ir_op(beo_base + beo_Keep,   "IncSP",      op_pin_state_pinned,     K, oparity_variable, 0, sizeof(be_stack_attr_t), &be_node_op_ops);
153         op_RegParams  = new_ir_op(beo_base + beo_Keep,   "RegParams",  op_pin_state_pinned,     K, oparity_variable, 0, sizeof(be_node_attr_t),  &be_node_op_ops);
154         op_StackParam = new_ir_op(beo_base + beo_Keep,   "StackParam", op_pin_state_pinned,     K, oparity_variable, 0, sizeof(be_stack_attr_t), &be_node_op_ops);
155
156         set_op_tag(op_Spill,      &be_node_tag);
157         set_op_tag(op_Reload,     &be_node_tag);
158         set_op_tag(op_Perm,       &be_node_tag);
159         set_op_tag(op_Copy,       &be_node_tag);
160         set_op_tag(op_Keep,       &be_node_tag);
161         set_op_tag(op_NoReg,      &be_node_tag);
162         set_op_tag(op_Call,       &be_node_tag);
163         set_op_tag(op_AddSP,      &be_node_tag);
164         set_op_tag(op_IncSP,      &be_node_tag);
165         set_op_tag(op_RegParams,  &be_node_tag);
166         set_op_tag(op_StackParam, &be_node_tag);
167 }
168
169 static void *init_node_attr(ir_node* irn, const arch_register_class_t *cls, ir_graph *irg, int max_reg_data)
170 {
171         be_node_attr_t *a = get_irn_attr(irn);
172
173         a->max_reg_data = max_reg_data;
174         a->flags        = arch_irn_flags_none;
175         a->cls          = cls;
176         a->reg_data     = NULL;
177
178         if(max_reg_data > 0) {
179                 int i;
180
181                 a->reg_data = NEW_ARR_D(be_reg_data_t, get_irg_obstack(irg), max_reg_data);
182                 memset(a->reg_data, 0, max_reg_data * sizeof(a->reg_data[0]));
183                 for(i = 0; i < max_reg_data; ++i) {
184                         a->reg_data[i].req.req.cls  = cls;
185                         a->reg_data[i].req.req.type = arch_register_req_type_normal;
186                 }
187         }
188
189         return a;
190 }
191
192 static INLINE int is_be_node(const ir_node *irn)
193 {
194         return get_op_tag(get_irn_op(irn)) == &be_node_tag;
195 }
196
197 be_opcode_t get_irn_be_opcode(const ir_node *irn)
198 {
199         return is_be_node(irn) ? get_irn_opcode(irn) - beo_base : beo_NoBeOp;
200 }
201
202 static int redir_proj(const ir_node **node, int pos)
203 {
204         const ir_node *n = *node;
205
206         if(is_Proj(n)) {
207                 assert(pos == -1 && "Illegal pos for a Proj");
208                 *node = get_Proj_pred(n);
209                 return get_Proj_proj(n);
210         }
211
212         return 0;
213 }
214
215 static void
216 be_node_set_irn_reg(const void *_self, ir_node *irn, const arch_register_t *reg)
217 {
218         int out_pos;
219         be_node_attr_t *a;
220
221         out_pos = redir_proj((const ir_node **) &irn, -1);
222         a       = get_irn_attr(irn);
223
224         assert(is_be_node(irn));
225         assert(out_pos < a->max_reg_data && "position too high");
226         a->reg_data[out_pos].reg = reg;
227 }
228
229
230 ir_node *be_new_Spill(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *to_spill, ir_node *ctx)
231 {
232         be_spill_attr_t *a;
233         ir_node *in[1];
234         ir_node *res;
235
236         in[0] = to_spill;
237         res   = new_ir_node(NULL, irg, bl, op_Spill, mode_M, 1, in);
238         a     = init_node_attr(res, cls, irg, 0);
239         a->ent       = NULL;
240         a->spill_ctx = ctx;
241         return res;
242 }
243
244 ir_node *be_new_Reload(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_mode *mode, ir_node *mem)
245 {
246         ir_node *in[1];
247         ir_node *res;
248
249         in[0] = mem;
250         res   = new_ir_node(NULL, irg, bl, op_Reload, mode, 1, in);
251         init_node_attr(res, cls, irg, 1);
252         return res;
253 }
254
255 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
256 {
257         ir_node *irn = new_ir_node(NULL, irg, bl, op_Perm, mode_T, n, in);
258         init_node_attr(irn, cls, irg, n);
259         return irn;
260 }
261
262 ir_node *be_new_Copy(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *op)
263 {
264         ir_node *in[1];
265         ir_node *res;
266
267         in[0] = op;
268         res   = new_ir_node(NULL, irg, bl, op_Copy, get_irn_mode(op), 1, in);
269         init_node_attr(res, cls, irg, 1);
270         return res;
271 }
272
273 ir_node *be_new_Keep(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
274 {
275         ir_node *irn;
276
277         irn = new_ir_node(NULL, irg, bl, op_Keep, mode_ANY, n, in);
278         init_node_attr(irn, cls, irg, 0);
279         keep_alive(irn);
280         return irn;
281 }
282
283 ir_node *be_new_Call(ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *sp, ir_node *ptr, int n_outs, int n, ir_node *in[])
284 {
285         int real_n = 3 + n;
286         ir_node *irn;
287         ir_node **real_in;
288
289         real_in = malloc(sizeof(real_in[0]) * (real_n));
290
291         real_in[0] = mem;
292         real_in[1] = sp;
293         real_in[2] = ptr;
294         memcpy(&real_in[3], in, n * sizeof(in[0]));
295
296         irn = new_ir_node(NULL, irg, bl, op_Call, mode_T, real_n, real_in);
297         init_node_attr(irn, NULL, irg, (n_outs > real_n ? n_outs : real_n));
298         return irn;
299 }
300
301 ir_node *be_new_IncSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, unsigned offset, be_stack_dir_t dir)
302 {
303         be_stack_attr_t *a;
304         ir_node *irn;
305         ir_node *in[1];
306
307         in[0]     = old_sp;
308         irn       = new_ir_node(NULL, irg, bl, op_IncSP, sp->reg_class->mode, 1, in);
309         a         = init_node_attr(irn, sp->reg_class, irg, 1);
310         a->dir    = dir;
311         a->offset = offset;
312
313         a->node_attr.flags |= arch_irn_flags_ignore;
314
315         /* Set output constraint to stack register. */
316         be_set_constr_single_reg(irn, -1, sp);
317         be_node_set_irn_reg(NULL, irn, sp);
318
319         return irn;
320 }
321
322 ir_node *be_new_AddSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, ir_node *op)
323 {
324         be_node_attr_t *a;
325         ir_node *irn;
326         ir_node *in[2];
327
328         in[0]    = old_sp;
329         in[1]    = op;
330         irn      = new_ir_node(NULL, irg, bl, op_AddSP, sp->reg_class->mode, 2, in);
331         a        = init_node_attr(irn, sp->reg_class, irg, 1);
332         a->flags |= arch_irn_flags_ignore;
333
334         /* Set output constraint to stack register. */
335         be_set_constr_single_reg(irn, -1, sp);
336         be_node_set_irn_reg(NULL, irn, sp);
337
338         return irn;
339 }
340
341 ir_node *be_new_NoReg(const arch_register_t *reg, ir_graph *irg, ir_node *bl)
342 {
343         be_node_attr_t *a;
344         ir_node *irn;
345         ir_node *in[1];
346
347         irn = new_ir_node(NULL, irg, bl, op_NoReg, reg->reg_class->mode, 0, in);
348         a   = init_node_attr(irn, reg->reg_class, irg, 1);
349         a->flags |= arch_irn_flags_ignore;
350         be_set_constr_single_reg(irn, -1, reg);
351         be_node_set_irn_reg(NULL, irn, reg);
352         return irn;
353 }
354
355 ir_node *be_new_StackParam(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_mode *mode, ir_node *frame_pointer, unsigned offset)
356 {
357         be_stack_attr_t *a;
358         ir_node *irn;
359         ir_node *in[1];
360
361         in[0] = frame_pointer;
362         irn = new_ir_node(NULL, irg, bl, op_StackParam, mode, 1, in);
363         a = init_node_attr(irn, cls, irg, 1);
364         a->offset = offset;
365         return irn;
366 }
367
368 int be_is_Spill         (const ir_node *irn) { return get_irn_be_opcode(irn) == beo_Spill          ; }
369 int be_is_Reload        (const ir_node *irn) { return get_irn_be_opcode(irn) == beo_Reload         ; }
370 int be_is_Copy          (const ir_node *irn) { return get_irn_be_opcode(irn) == beo_Copy           ; }
371 int be_is_Perm          (const ir_node *irn) { return get_irn_be_opcode(irn) == beo_Perm           ; }
372 int be_is_Keep          (const ir_node *irn) { return get_irn_be_opcode(irn) == beo_Keep           ; }
373 int be_is_Call          (const ir_node *irn) { return get_irn_be_opcode(irn) == beo_Call           ; }
374 int be_is_IncSP         (const ir_node *irn) { return get_irn_be_opcode(irn) == beo_IncSP          ; }
375 int be_is_AddSP         (const ir_node *irn) { return get_irn_be_opcode(irn) == beo_AddSP          ; }
376 int be_is_RegParams     (const ir_node *irn) { return get_irn_be_opcode(irn) == beo_RegParams      ; }
377 int be_is_StackParam    (const ir_node *irn) { return get_irn_be_opcode(irn) == beo_StackParam     ; }
378 int be_is_NoReg         (const ir_node *irn) { return get_irn_be_opcode(irn) == beo_NoReg          ; }
379
380 static void be_limited(void *data, bitset_t *bs)
381 {
382         be_req_t *req = data;
383
384         switch(req->kind) {
385         case be_req_kind_negate_old_limited:
386         case be_req_kind_old_limited:
387                 req->x.old_limited.old_limited(req->x.old_limited.old_limited_env, bs);
388                 if(req->kind == be_req_kind_negate_old_limited)
389                         bitset_flip_all(bs);
390                 break;
391         case be_req_kind_single_reg:
392                 bitset_clear_all(bs);
393                 bitset_set(bs, req->x.single_reg->index);
394                 break;
395         }
396 }
397
398 void be_set_constr_single_reg(ir_node *irn, int pos, const arch_register_t *reg)
399 {
400         int idx           = pos < 0 ? -(pos - 1) : pos;
401         be_node_attr_t *a = get_irn_attr(irn);
402         be_reg_data_t *rd = &a->reg_data[idx];
403         be_req_t       *r = pos < 0 ? &rd->req : &rd->in_req;
404
405         assert(is_be_node(irn));
406         assert(!(pos >= 0) || pos < get_irn_arity(irn));
407         assert(!(pos < 0)  || -(pos + 1) <= a->max_reg_data);
408
409         r->kind            = be_req_kind_single_reg;
410         r->x.single_reg    = reg;
411         r->req.limited     = be_limited;
412         r->req.limited_env = r;
413         r->req.type        = arch_register_req_type_limited;
414         r->req.cls         = reg->reg_class;
415 }
416
417 void be_set_constr_limited(ir_node *irn, int pos, const arch_register_req_t *req)
418 {
419         int idx           = pos < 0 ? -(pos - 1) : pos;
420         be_node_attr_t *a = get_irn_attr(irn);
421         be_reg_data_t *rd = &a->reg_data[idx];
422         be_req_t       *r = pos < 0 ? &rd->req : &rd->in_req;
423
424         assert(is_be_node(irn));
425         assert(!(pos >= 0) || pos < get_irn_arity(irn));
426         assert(!(pos < 0)  || -(pos + 1) <= a->max_reg_data);
427         assert(arch_register_req_is(req, limited));
428
429         r->kind            = be_req_kind_old_limited;
430         r->req.limited     = be_limited;
431         r->req.limited_env = r;
432         r->req.type        = arch_register_req_type_limited;
433         r->req.cls         = req->cls;
434
435         r->x.old_limited.old_limited     = req->limited;
436         r->x.old_limited.old_limited_env = req->limited_env;
437 }
438
439 void be_set_IncSP_offset(ir_node *irn, unsigned offset)
440 {
441         be_stack_attr_t *a = get_irn_attr(irn);
442         assert(be_is_IncSP(irn));
443         a->offset = offset;
444 }
445
446 unsigned be_get_IncSP_offset(ir_node *irn)
447 {
448         be_stack_attr_t *a = get_irn_attr(irn);
449         assert(be_is_IncSP(irn));
450         return a->offset;
451 }
452
453 void be_set_IncSP_direction(ir_node *irn, be_stack_dir_t dir)
454 {
455         be_stack_attr_t *a = get_irn_attr(irn);
456         assert(be_is_IncSP(irn));
457         a->dir = dir;
458 }
459
460 be_stack_dir_t be_get_IncSP_direction(ir_node *irn)
461 {
462         be_stack_attr_t *a = get_irn_attr(irn);
463         assert(be_is_IncSP(irn));
464         return a->dir;
465 }
466
467 void be_set_Spill_entity(ir_node *irn, entity *ent)
468 {
469         be_spill_attr_t *a = get_irn_attr(irn);
470         assert(be_is_Spill(irn));
471         a->ent = ent;
472 }
473
474 static ir_node *find_a_spill_walker(ir_node *irn, unsigned visited_nr)
475 {
476         if(get_irn_visited(irn) < visited_nr) {
477                 set_irn_visited(irn, visited_nr);
478
479                 if(is_Phi(irn)) {
480                         int i, n;
481                         for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
482                                 ir_node *n = find_a_spill_walker(get_irn_n(irn, i), visited_nr);
483                                 if(n != NULL)
484                                         return n;
485                         }
486                 }
487
488                 else if(get_irn_be_opcode(irn) == beo_Spill)
489                         return irn;
490         }
491
492         return NULL;
493 }
494
495 ir_node *be_get_Spill_context(const ir_node *irn) {
496         const be_spill_attr_t *a = get_irn_attr(irn);
497         assert(be_is_Spill(irn));
498         return a->spill_ctx;
499 }
500
501 /**
502  * Finds a spill for a reload.
503  * If the reload is directly using the spill, this is simple,
504  * else we perform DFS from the reload (over all PhiMs) and return
505  * the first spill node we find.
506  */
507 static INLINE ir_node *find_a_spill(ir_node *irn)
508 {
509         ir_graph *irg       = get_irn_irg(irn);
510         unsigned visited_nr = get_irg_visited(irg) + 1;
511
512         assert(be_is_Reload(irn));
513         set_irg_visited(irg, visited_nr);
514         return find_a_spill_walker(irn, visited_nr);
515 }
516
517 entity *be_get_spill_entity(ir_node *irn)
518 {
519         int opc           = get_irn_opcode(irn);
520
521         switch(get_irn_be_opcode(irn)) {
522         case beo_Reload:
523                 return be_get_spill_entity(find_a_spill(irn));
524         case beo_Spill:
525                 {
526                         be_spill_attr_t *a = get_irn_attr(irn);
527                         return a->ent;
528                 }
529         default:
530                 assert(0 && "Must give spill/reload node");
531         }
532
533         return NULL;
534 }
535
536 ir_node *be_spill(const arch_env_t *arch_env, ir_node *irn, ir_node *ctx)
537 {
538         const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, irn, -1);
539
540         ir_node *bl    = get_nodes_block(irn);
541         ir_graph *irg  = get_irn_irg(bl);
542         ir_node *spill = be_new_Spill(cls, irg, bl, irn, ctx);
543         ir_node *insert;
544
545         /*
546          * search the right insertion point. a spill of a phi cannot be put
547          * directly after the phi, if there are some phis behind the one which
548          * is spilled.
549          */
550         insert = sched_next(irn);
551         while(is_Phi(insert) && !sched_is_end(insert))
552                 insert = sched_next(insert);
553
554         sched_add_before(insert, spill);
555         return spill;
556 }
557
558 ir_node *be_reload(const arch_env_t *arch_env,
559                                    const arch_register_class_t *cls,
560                                    ir_node *irn, int pos, ir_mode *mode, ir_node *spill)
561 {
562         ir_node *reload;
563
564         ir_node *bl   = get_nodes_block(irn);
565         ir_graph *irg = get_irn_irg(bl);
566
567         assert(be_is_Spill(spill) || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
568
569         reload = be_new_Reload(cls, irg, bl, mode, spill);
570
571         set_irn_n(irn, pos, reload);
572         sched_add_before(irn, reload);
573         return reload;
574 }
575
576 static void *put_out_reg_req(arch_register_req_t *req, const ir_node *irn, int out_pos)
577 {
578         const be_node_attr_t *a = get_irn_attr(irn);
579
580         if(out_pos < a->max_reg_data)
581                 memcpy(req, &a->reg_data[out_pos].req, sizeof(req[0]));
582         else {
583                 req->type = arch_register_req_type_none;
584                 req->cls  = NULL;
585         }
586
587         return req;
588 }
589
590 static void *put_in_reg_req(arch_register_req_t *req, const ir_node *irn, int pos)
591 {
592         const be_node_attr_t *a = get_irn_attr(irn);
593         int n                   = get_irn_arity(irn);
594
595         if(pos < get_irn_arity(irn))
596                 memcpy(req, &a->reg_data[pos].in_req, sizeof(req[0]));
597         else {
598                 req->type = arch_register_req_type_none;
599                 req->cls  = NULL;
600         }
601
602         return req;
603 }
604
605 static const arch_register_req_t *
606 be_node_get_irn_reg_req(const void *self, arch_register_req_t *req, const ir_node *irn, int pos)
607 {
608         int out_pos = pos;
609
610         if(pos < 0) {
611                 if(get_irn_mode(irn) == mode_T)
612                         return NULL;
613
614                 out_pos = redir_proj((const ir_node **) &irn, pos);
615                 assert(is_be_node(irn));
616                 return put_out_reg_req(req, irn, out_pos);
617         }
618
619         else {
620                 return is_be_node(irn) ? put_in_reg_req(req, irn, pos) : NULL;
621         }
622
623         return req;
624 }
625
626 const arch_register_t *
627 be_node_get_irn_reg(const void *_self, const ir_node *irn)
628 {
629         int out_pos;
630         be_node_attr_t *a;
631
632         out_pos = redir_proj((const ir_node **) &irn, -1);
633         a       = get_irn_attr(irn);
634
635         assert(is_be_node(irn));
636         assert(out_pos < a->max_reg_data && "position too high");
637
638         return a->reg_data[out_pos].reg;
639 }
640
641 arch_irn_class_t be_node_classify(const void *_self, const ir_node *irn)
642 {
643         redir_proj((const ir_node **) &irn, -1);
644
645         switch(get_irn_be_opcode(irn)) {
646 #define XXX(a,b) case beo_ ## a: return arch_irn_class_ ## b;
647                 XXX(Spill, spill)
648                 XXX(Reload, reload)
649                 XXX(Perm, perm)
650                 XXX(Copy, copy)
651 #undef XXX
652                 default:
653                 return 0;
654         }
655
656         return 0;
657 }
658
659 arch_irn_flags_t be_node_get_flags(const void *_self, const ir_node *irn)
660 {
661         be_node_attr_t *a = get_irn_attr(irn);
662         return a->flags;
663 }
664
665 static const arch_irn_ops_if_t be_node_irn_ops_if = {
666         be_node_get_irn_reg_req,
667         be_node_set_irn_reg,
668         be_node_get_irn_reg,
669         be_node_classify,
670         be_node_get_flags,
671 };
672
673 static const arch_irn_ops_t be_node_irn_ops = {
674         &be_node_irn_ops_if
675 };
676
677 const void *be_node_get_arch_ops(const arch_irn_handler_t *self, const ir_node *irn)
678 {
679         redir_proj((const ir_node **) &irn, -1);
680         return is_be_node(irn) ? &be_node_irn_ops : NULL;
681 }
682
683 const arch_irn_handler_t be_node_irn_handler = {
684         be_node_get_arch_ops
685 };
686
687 static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason)
688 {
689         be_node_attr_t *at = get_irn_attr(irn);
690         int i;
691
692         assert(is_be_node(irn));
693
694         switch(reason) {
695                 case dump_node_opcode_txt:
696                         fprintf(f, get_op_name(get_irn_op(irn)));
697                         break;
698                 case dump_node_mode_txt:
699                         fprintf(f, get_mode_name(get_irn_mode(irn)));
700                         break;
701                 case dump_node_nodeattr_txt:
702                         break;
703                 case dump_node_info_txt:
704                         fprintf(f, "reg class: %s\n", at->cls->name);
705                         for(i = 0; i < at->max_reg_data; ++i) {
706                                 const arch_register_t *reg = at->reg_data[i].reg;
707                                 fprintf(f, "reg #%d: %s\n", i, reg ? reg->name : "n/a");
708                         }
709
710                         switch(get_irn_be_opcode(irn)) {
711                         case beo_Spill:
712                                 {
713                                         be_spill_attr_t *a = (be_spill_attr_t *) at;
714
715                                         ir_fprintf(f, "spill context: %+F\n", a->spill_ctx);
716                                         if (a->ent) {
717                                                 unsigned ofs = get_entity_offset_bytes(a->ent);
718                                                 ir_fprintf(f, "spill entity: %+F offset %x (%d)\n", a->ent, ofs, ofs);
719                                         }
720                                         else {
721                                                 ir_fprintf(f, "spill entity: n/a\n");
722                                         }
723                                 }
724                                 break;
725
726                         case beo_IncSP:
727                                 {
728                                         be_stack_attr_t *a = (be_stack_attr_t *) at;
729                                         fprintf(f, "offset: %u\n", a->offset);
730                                         fprintf(f, "direction: %s\n", a->dir == be_stack_dir_along ? "along" : "against");
731                                 }
732                                 break;
733                         }
734
735         }
736
737         return 0;
738 }
739
740 void copy_attr(const ir_node *old_node, ir_node *new_node)
741 {
742         be_node_attr_t *old_attr = get_irn_attr(old_attr);
743         be_node_attr_t *new_attr = get_irn_attr(new_node);
744
745         assert(is_be_node(old_node));
746         assert(is_be_node(new_node));
747
748         memcpy(new_attr, old_attr, old_node->op->attr_size);
749
750         new_attr->reg_data = NEW_ARR_D(be_reg_data_t, get_irg_obstack(get_irn_irg(new_node)), new_attr->max_reg_data);
751         memcpy(new_attr->reg_data, old_attr->reg_data, new_attr->max_reg_data * sizeof(be_reg_data_t));
752 }
753
754 static const ir_op_ops be_node_op_ops = {
755         NULL,
756         NULL,
757         NULL,
758         NULL,
759         NULL,
760         copy_attr,
761         NULL,
762         NULL,
763         NULL,
764         NULL,
765         NULL,
766         dump_node,
767         NULL
768 };
769
770 pset *nodes_live_at(const arch_env_t *arch_env, const arch_register_class_t *cls, const ir_node *pos, pset *live)
771 {
772         firm_dbg_module_t *dbg = firm_dbg_register("firm.be.node");
773         const ir_node *bl      = is_Block(pos) ? pos : get_nodes_block(pos);
774         ir_node *irn;
775         irn_live_t *li;
776
777         live_foreach(bl, li) {
778                 ir_node *irn = (ir_node *) li->irn;
779                 if(live_is_end(li) && arch_irn_consider_in_reg_alloc(arch_env, cls, irn))
780                         pset_insert_ptr(live, irn);
781         }
782
783         sched_foreach_reverse(bl, irn) {
784                 int i, n;
785                 ir_node *x;
786
787                 /*
788                  * If we encounter the node we want to insert the Perm after,
789                 * exit immediately, so that this node is still live
790                 */
791                 if(irn == pos)
792                         return live;
793
794                 DBG((dbg, LEVEL_1, "%+F\n", irn));
795                 for(x = pset_first(live); x; x = pset_next(live))
796                         DBG((dbg, LEVEL_1, "\tlive: %+F\n", x));
797
798                 if(arch_irn_has_reg_class(arch_env, irn, -1, cls))
799                         pset_remove_ptr(live, irn);
800
801                 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
802                         ir_node *op = get_irn_n(irn, i);
803
804                         if(arch_irn_consider_in_reg_alloc(arch_env, cls, op))
805                                 pset_insert_ptr(live, op);
806                 }
807         }
808
809         return live;
810 }
811
812 ir_node *insert_Perm_after(const arch_env_t *arch_env,
813                                                    const arch_register_class_t *cls,
814                                                    dom_front_info_t *dom_front,
815                                                    ir_node *pos)
816 {
817         ir_node *bl                 = is_Block(pos) ? pos : get_nodes_block(pos);
818         ir_graph *irg               = get_irn_irg(bl);
819         pset *live                  = pset_new_ptr_default();
820         firm_dbg_module_t *dbg      = firm_dbg_register("be.node");
821
822         ir_node *curr, *irn, *perm, **nodes;
823         int i, n;
824
825         DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
826
827         if(!nodes_live_at(arch_env, cls, pos, live));
828
829         n = pset_count(live);
830
831         if(n == 0)
832                 return NULL;
833
834         nodes = malloc(n * sizeof(nodes[0]));
835
836         DBG((dbg, LEVEL_1, "live:\n"));
837         for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++) {
838                 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
839                 nodes[i] = irn;
840         }
841
842         perm = be_new_Perm(cls, irg, bl, n, nodes);
843         sched_add_after(pos, perm);
844         free(nodes);
845
846         curr = perm;
847         for(i = 0; i < n; ++i) {
848                 ir_node *copies[1];
849                 ir_node *perm_op = get_irn_n(perm, i);
850                 const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op);
851
852                 ir_mode *mode = get_irn_mode(perm_op);
853                 ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
854                 arch_set_irn_register(arch_env, proj, reg);
855
856                 sched_add_after(curr, proj);
857                 curr = proj;
858
859                 copies[0] = proj;
860                 be_ssa_constr_single(dom_front, perm_op, 1, copies);
861         }
862         return perm;
863 }