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