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