Added phi handler
[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 #include "offset.h"
27 #include "bitfiddle.h"
28
29 #include "irop_t.h"
30 #include "irmode_t.h"
31 #include "irnode_t.h"
32 #include "ircons_t.h"
33 #include "irprintf.h"
34 #include "irgwalk.h"
35
36 #include "be_t.h"
37 #include "belive_t.h"
38 #include "besched_t.h"
39 #include "benode_t.h"
40
41 #include "beirgmod.h"
42
43 #define OUT_POS(x) (-((x) + 1))
44
45 /* Sometimes we want to put const nodes into get_irn_generic_attr ... */
46 #define get_irn_attr(irn) get_irn_generic_attr((ir_node *) (irn))
47
48 static unsigned be_node_tag = FOURCC('B', 'E', 'N', 'O');
49
50 #if 0
51 typedef enum _node_kind_t {
52         node_kind_spill,
53         node_kind_reload,
54         node_kind_perm,
55         node_kind_copy,
56         node_kind_kill,
57         node_kind_last
58 } node_kind_t;
59 #endif
60
61 typedef enum {
62         be_req_kind_old_limited,
63         be_req_kind_negate_old_limited,
64         be_req_kind_single_reg
65 } be_req_kind_t;
66
67 typedef struct {
68         arch_register_req_t req;
69         be_req_kind_t       kind;
70         arch_irn_flags_t    flags;
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         be_reg_data_t               *reg_data;
90 } be_node_attr_t;
91
92 typedef struct {
93         be_node_attr_t node_attr;
94         int offset;           /**< The offset by which the stack shall be increased/decreased. */
95         be_stack_dir_t dir;   /**< The direction in which the stack shall be modified (along or in the other direction). */
96 } be_stack_attr_t;
97
98 typedef struct {
99         be_node_attr_t node_attr;
100         entity *ent;
101         int offset;
102 } be_frame_attr_t;
103
104 typedef struct {
105         be_node_attr_t node_attr;
106         entity *ent;
107 } be_call_attr_t;
108
109 typedef struct {
110         be_frame_attr_t frame_attr;
111         ir_node *spill_ctx;  /**< The node in whose context this spill was introduced. */
112 } be_spill_attr_t;
113
114 ir_op *op_be_Spill;
115 ir_op *op_be_Reload;
116 ir_op *op_be_Perm;
117 ir_op *op_be_Copy;
118 ir_op *op_be_Keep;
119 ir_op *op_be_CopyKeep;
120 ir_op *op_be_Call;
121 ir_op *op_be_Return;
122 ir_op *op_be_IncSP;
123 ir_op *op_be_AddSP;
124 ir_op *op_be_SetSP;
125 ir_op *op_be_RegParams;
126 ir_op *op_be_StackParam;
127 ir_op *op_be_FrameAddr;
128 ir_op *op_be_FrameLoad;
129 ir_op *op_be_FrameStore;
130 ir_op *op_be_Barrier;
131
132 static int beo_base = -1;
133
134 static const ir_op_ops be_node_op_ops;
135
136 #define N   irop_flag_none
137 #define L   irop_flag_labeled
138 #define C   irop_flag_commutative
139 #define X   irop_flag_cfopcode
140 #define I   irop_flag_ip_cfopcode
141 #define F   irop_flag_fragile
142 #define Y   irop_flag_forking
143 #define H   irop_flag_highlevel
144 #define c   irop_flag_constlike
145 #define K   irop_flag_keep
146
147 void be_node_init(void) {
148         static int inited = 0;
149
150         if(inited)
151                 return;
152
153         inited = 1;
154
155         /* Acquire all needed opcodes. */
156         beo_base = get_next_ir_opcodes(beo_Last - 1);
157
158         op_be_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);
159         op_be_Reload     = new_ir_op(beo_base + beo_Reload,     "Reload",     op_pin_state_mem_pinned, N, oparity_zero,     0, sizeof(be_frame_attr_t), &be_node_op_ops);
160         op_be_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);
161         op_be_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);
162         op_be_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);
163         op_be_CopyKeep   = new_ir_op(beo_base + beo_CopyKeep,   "CopyKeep",   op_pin_state_pinned,     K, oparity_variable, 0, sizeof(be_node_attr_t),  &be_node_op_ops);
164         op_be_Call       = new_ir_op(beo_base + beo_Call,       "Call",       op_pin_state_pinned,     N, oparity_variable, 0, sizeof(be_call_attr_t),  &be_node_op_ops);
165         op_be_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);
166         op_be_AddSP      = new_ir_op(beo_base + beo_AddSP,      "AddSP",      op_pin_state_pinned,     N, oparity_unary,    0, sizeof(be_node_attr_t),  &be_node_op_ops);
167         op_be_SetSP      = new_ir_op(beo_base + beo_SetSP,      "SetSP",      op_pin_state_pinned,     N, oparity_binary,   0, sizeof(be_stack_attr_t), &be_node_op_ops);
168         op_be_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);
169         op_be_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);
170         op_be_StackParam = new_ir_op(beo_base + beo_StackParam, "StackParam", op_pin_state_pinned,     N, oparity_unary,    0, sizeof(be_frame_attr_t), &be_node_op_ops);
171         op_be_FrameAddr  = new_ir_op(beo_base + beo_FrameAddr,  "FrameAddr",  op_pin_state_pinned,     N, oparity_binary,   0, sizeof(be_frame_attr_t), &be_node_op_ops);
172         op_be_FrameLoad  = new_ir_op(beo_base + beo_FrameLoad,  "FrameLoad",  op_pin_state_pinned,     N, oparity_any,      0, sizeof(be_frame_attr_t), &be_node_op_ops);
173         op_be_FrameStore = new_ir_op(beo_base + beo_FrameStore, "FrameStore", op_pin_state_pinned,     N, oparity_any,      0, sizeof(be_frame_attr_t), &be_node_op_ops);
174         op_be_Barrier    = new_ir_op(beo_base + beo_Barrier,    "Barrier",    op_pin_state_pinned,     N, oparity_any,      0, sizeof(be_node_attr_t),  &be_node_op_ops);
175
176         set_op_tag(op_be_Spill,      &be_node_tag);
177         set_op_tag(op_be_Reload,     &be_node_tag);
178         set_op_tag(op_be_Perm,       &be_node_tag);
179         set_op_tag(op_be_Copy,       &be_node_tag);
180         set_op_tag(op_be_Keep,       &be_node_tag);
181         set_op_tag(op_be_CopyKeep,   &be_node_tag);
182         set_op_tag(op_be_Call,       &be_node_tag);
183         set_op_tag(op_be_Return,     &be_node_tag);
184         set_op_tag(op_be_AddSP,      &be_node_tag);
185         set_op_tag(op_be_SetSP,      &be_node_tag);
186         set_op_tag(op_be_IncSP,      &be_node_tag);
187         set_op_tag(op_be_RegParams,  &be_node_tag);
188         set_op_tag(op_be_StackParam, &be_node_tag);
189         set_op_tag(op_be_FrameLoad,  &be_node_tag);
190         set_op_tag(op_be_FrameStore, &be_node_tag);
191         set_op_tag(op_be_FrameAddr,  &be_node_tag);
192         set_op_tag(op_be_Barrier,   &be_node_tag);
193 }
194
195 static void *init_node_attr(ir_node* irn, int max_reg_data)
196 {
197         ir_graph *irg     = get_irn_irg(irn);
198         be_node_attr_t *a = get_irn_attr(irn);
199
200         memset(a, 0, sizeof(get_op_attr_size(get_irn_op(irn))));
201         a->max_reg_data = max_reg_data;
202         a->reg_data     = NULL;
203
204         if(max_reg_data > 0) {
205                 int i;
206
207                 a->reg_data = NEW_ARR_D(be_reg_data_t, get_irg_obstack(irg), max_reg_data);
208                 memset(a->reg_data, 0, max_reg_data * sizeof(a->reg_data[0]));
209                 for(i = 0; i < max_reg_data; ++i) {
210                         a->reg_data[i].req.req.cls  = NULL;
211                         a->reg_data[i].req.req.type = arch_register_req_type_none;
212                 }
213         }
214
215         return a;
216 }
217
218 static INLINE int is_be_node(const ir_node *irn)
219 {
220         return get_op_tag(get_irn_op(irn)) == &be_node_tag;
221 }
222
223 be_opcode_t be_get_irn_opcode(const ir_node *irn)
224 {
225         return is_be_node(irn) ? get_irn_opcode(irn) - beo_base : beo_NoBeOp;
226 }
227
228 static int redir_proj(const ir_node **node, int pos)
229 {
230         const ir_node *n = *node;
231
232         if(is_Proj(n)) {
233                 ir_node *irn;
234
235                 assert(pos == -1 && "Illegal pos for a Proj");
236                 *node = irn = get_Proj_pred(n);
237                 if(is_Proj(irn)) {
238                         assert(get_irn_mode(irn) == mode_T);
239                         *node = get_Proj_pred(irn);
240                 }
241                 return get_Proj_proj(n);
242         }
243
244         return 0;
245 }
246
247 static void
248 be_node_set_irn_reg(const void *_self, ir_node *irn, const arch_register_t *reg)
249 {
250         int out_pos;
251         be_node_attr_t *a;
252
253         out_pos = redir_proj((const ir_node **) &irn, -1);
254         a       = get_irn_attr(irn);
255
256         assert(is_be_node(irn));
257         assert(out_pos < a->max_reg_data && "position too high");
258         a->reg_data[out_pos].reg = reg;
259 }
260
261
262 ir_node *be_new_Spill(const arch_register_class_t *cls, const arch_register_class_t *cls_frame, ir_graph *irg, ir_node *bl, ir_node *frame, ir_node *to_spill, ir_node *ctx)
263 {
264         be_spill_attr_t *a;
265         ir_node *in[2];
266         ir_node *res;
267
268         in[0] = frame;
269         in[1] = to_spill;
270         res   = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
271         a     = init_node_attr(res, 2);
272         a->frame_attr.ent = NULL;
273         a->frame_attr.offset = 0;
274         a->spill_ctx = ctx;
275
276         be_node_set_reg_class(res, 0, cls_frame);
277         be_node_set_reg_class(res, 1, cls);
278         return res;
279 }
280
281 ir_node *be_new_Reload(const arch_register_class_t *cls, const arch_register_class_t *cls_frame, ir_graph *irg, ir_node *bl, ir_node *frame, ir_node *mem, ir_mode *mode)
282 {
283         ir_node *in[2];
284         ir_node *res;
285
286         in[0] = frame;
287         in[1] = mem;
288         res   = new_ir_node(NULL, irg, bl, op_be_Reload, mode, 2, in);
289         init_node_attr(res, 2);
290         be_node_set_reg_class(res, 0, cls_frame);
291         be_node_set_reg_class(res, -1, cls);
292         return res;
293 }
294
295 ir_node *(be_get_Reload_mem)(const ir_node *irn)
296 {
297         assert(be_is_Reload(irn));
298         return get_irn_n(irn, be_pos_Reload_mem);
299 }
300
301 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
302 {
303         int i;
304         ir_node *irn = new_ir_node(NULL, irg, bl, op_be_Perm, mode_T, n, in);
305         init_node_attr(irn, n);
306         for(i = 0; i < n; ++i) {
307                 be_node_set_reg_class(irn, i, cls);
308                 be_node_set_reg_class(irn, OUT_POS(i), cls);
309         }
310
311         return irn;
312 }
313
314 ir_node *be_new_Copy(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *op)
315 {
316         ir_node *in[1];
317         ir_node *res;
318
319         in[0] = op;
320         res   = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
321         init_node_attr(res, 1);
322         be_node_set_reg_class(res, 0, cls);
323         be_node_set_reg_class(res, OUT_POS(0), cls);
324         return res;
325 }
326
327 ir_node *be_new_Keep(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
328 {
329         int i;
330         ir_node *irn;
331
332         irn = new_ir_node(NULL, irg, bl, op_be_Keep, mode_ANY, n, in);
333         init_node_attr(irn, n);
334         for(i = 0; i < n; ++i) {
335                 be_node_set_reg_class(irn, i, cls);
336         }
337         keep_alive(irn);
338         return irn;
339 }
340
341 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[])
342 {
343         int real_n = 3 + n;
344         ir_node *irn;
345         ir_node **real_in;
346
347         real_in = malloc(sizeof(real_in[0]) * (real_n));
348
349         real_in[0] = mem;
350         real_in[1] = sp;
351         real_in[2] = ptr;
352         memcpy(&real_in[3], in, n * sizeof(in[0]));
353
354         irn = new_ir_node(NULL, irg, bl, op_be_Call, mode_T, real_n, real_in);
355         init_node_attr(irn, (n_outs > real_n ? n_outs : real_n));
356         return irn;
357 }
358
359 entity *be_Call_get_entity(const ir_node *call)
360 {
361         be_call_attr_t *a = get_irn_attr(call);
362         assert(be_is_Call(call));
363         return a->ent;
364 }
365
366 void    be_Call_set_entity(ir_node *call, entity *ent)
367 {
368         be_call_attr_t *a = get_irn_attr(call);
369         assert(be_is_Call(call));
370         a->ent = ent;
371 }
372
373 ir_node *be_new_Return(ir_graph *irg, ir_node *bl, int n, ir_node *in[])
374 {
375         ir_node *irn = new_ir_node(NULL, irg, bl, op_be_Return, mode_X, n, in);
376         init_node_attr(irn, n);
377
378         return irn;
379 }
380
381 ir_node *be_new_IncSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, ir_node *mem, unsigned offset, be_stack_dir_t dir)
382 {
383         be_stack_attr_t *a;
384         ir_node *irn;
385         ir_node *in[1];
386
387         in[0]     = old_sp;
388         in[1]     = mem;
389         irn       = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode, 2, in);
390         a         = init_node_attr(irn, 1);
391         a->dir    = dir;
392         a->offset = offset;
393
394         be_node_set_flags(irn, -1, arch_irn_flags_ignore);
395
396         /* Set output constraint to stack register. */
397         be_node_set_reg_class(irn, 0, sp->reg_class);
398         be_set_constr_single_reg(irn, -1, sp);
399         be_node_set_irn_reg(NULL, irn, sp);
400
401         return irn;
402 }
403
404 ir_node *be_new_AddSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, ir_node *sz)
405 {
406         be_node_attr_t *a;
407         ir_node *irn;
408         ir_node *in[be_pos_AddSP_last];
409
410         in[be_pos_AddSP_old_sp] = old_sp;
411         in[be_pos_AddSP_size]   = sz;
412
413         irn      = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T, be_pos_AddSP_last, in);
414         a        = init_node_attr(irn, be_pos_AddSP_last);
415
416         be_node_set_flags(irn, OUT_POS(0), arch_irn_flags_ignore);
417
418         /* Set output constraint to stack register. */
419         be_set_constr_single_reg(irn, OUT_POS(0), sp);
420
421         return irn;
422 }
423
424 ir_node *be_new_SetSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, ir_node *op, ir_node *mem)
425 {
426         be_node_attr_t *a;
427         ir_node *irn;
428         ir_node *in[3];
429
430         in[0]    = mem;
431         in[1]    = old_sp;
432         in[2]    = op;
433         irn      = new_ir_node(NULL, irg, bl, op_be_SetSP, get_irn_mode(old_sp), 3, in);
434         a        = init_node_attr(irn, 3);
435
436         be_node_set_flags(irn, OUT_POS(0), arch_irn_flags_ignore);
437
438         /* Set output constraint to stack register. */
439         be_set_constr_single_reg(irn, OUT_POS(0), sp);
440         be_node_set_reg_class(irn, 1, sp->reg_class);
441         be_node_set_reg_class(irn, 2, sp->reg_class);
442         be_node_set_irn_reg(NULL, irn, sp);
443
444         return irn;
445 }
446
447 ir_node *be_new_StackParam(const arch_register_class_t *cls, const arch_register_class_t *cls_frame, ir_graph *irg, ir_node *bl, ir_mode *mode, ir_node *frame_pointer, entity *ent)
448 {
449         be_frame_attr_t *a;
450         ir_node *irn;
451         ir_node *in[1];
452
453         in[0] = frame_pointer;
454         irn = new_ir_node(NULL, irg, bl, op_be_StackParam, mode, 1, in);
455         a = init_node_attr(irn, 1);
456         a->ent = ent;
457
458         be_node_set_reg_class(irn, 0, cls_frame);
459         be_node_set_reg_class(irn, OUT_POS(0), cls);
460         return irn;
461 }
462
463 ir_node *be_new_RegParams(ir_graph *irg, ir_node *bl, int n_outs)
464 {
465         ir_node *irn;
466         ir_node *in[1];
467
468         irn = new_ir_node(NULL, irg, bl, op_be_RegParams, mode_T, 0, in);
469         init_node_attr(irn, n_outs);
470         return irn;
471 }
472
473 ir_node *be_new_FrameLoad(const arch_register_class_t *cls_frame, const arch_register_class_t *cls_data,
474                                                   ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *frame, entity *ent)
475 {
476         be_frame_attr_t *a;
477         ir_node *irn;
478         ir_node *in[2];
479
480         in[0]  = mem;
481         in[1]  = frame;
482         irn    = new_ir_node(NULL, irg, bl, op_be_FrameLoad, mode_T, 2, in);
483         a      = init_node_attr(irn, 3);
484         a->ent = ent;
485         a->offset = 0;
486         be_node_set_reg_class(irn, 1, cls_frame);
487         be_node_set_reg_class(irn, OUT_POS(pn_Load_res), cls_data);
488         return irn;
489 }
490
491 ir_node *be_new_FrameStore(const arch_register_class_t *cls_frame, const arch_register_class_t *cls_data,
492                                                    ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *frame, ir_node *data, entity *ent)
493 {
494         be_frame_attr_t *a;
495         ir_node *irn;
496         ir_node *in[3];
497
498         in[0]  = mem;
499         in[1]  = frame;
500         in[2]  = data;
501         irn    = new_ir_node(NULL, irg, bl, op_be_FrameStore, mode_T, 3, in);
502         a      = init_node_attr(irn, 3);
503         a->ent = ent;
504         a->offset = 0;
505         be_node_set_reg_class(irn, 1, cls_frame);
506         be_node_set_reg_class(irn, 2, cls_data);
507         return irn;
508 }
509
510 ir_node *be_new_FrameAddr(const arch_register_class_t *cls_frame, ir_graph *irg, ir_node *bl, ir_node *frame, entity *ent)
511 {
512         be_frame_attr_t *a;
513         ir_node *irn;
514         ir_node *in[1];
515
516         in[0]  = frame;
517         irn    = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
518         a      = init_node_attr(irn, 1);
519         a->ent = ent;
520         a->offset = 0;
521         be_node_set_reg_class(irn, 0, cls_frame);
522         be_node_set_reg_class(irn, OUT_POS(0), cls_frame);
523         return irn;
524 }
525
526 ir_node *be_new_CopyKeep(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *src, int n, ir_node *in_keep[], ir_mode *mode)
527 {
528         ir_node *irn;
529         ir_node **in = (ir_node **) alloca((n + 1) * sizeof(in[0]));
530
531         in[0] = src;
532         memcpy(&in[1], in_keep, n * sizeof(in[0]));
533         irn   = new_ir_node(NULL, irg, bl, op_be_CopyKeep, mode, n + 1, in);
534         init_node_attr(irn, n);
535         be_node_set_reg_class(irn, OUT_POS(0), cls);
536         be_node_set_reg_class(irn, 0, cls);
537
538         return irn;
539 }
540
541 ir_node *be_new_CopyKeep_single(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *src, ir_node *keep, ir_mode *mode)
542 {
543         ir_node *in[1];
544
545         in[0] = keep;
546         return be_new_CopyKeep(cls, irg, bl, src, 1, in, mode);
547 }
548
549 ir_node *be_new_Barrier(ir_graph *irg, ir_node *bl, int n, ir_node *in[])
550 {
551         ir_node *irn;
552
553         irn = new_ir_node(NULL, irg, bl, op_be_Barrier, mode_T, n, in);
554         init_node_attr(irn, n);
555         return irn;
556 }
557
558 int be_is_Spill         (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Spill          ; }
559 int be_is_Reload        (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Reload         ; }
560 int be_is_Copy          (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Copy           ; }
561 int be_is_Perm          (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Perm           ; }
562 int be_is_Keep          (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Keep           ; }
563 int be_is_Call          (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Call           ; }
564 int be_is_Return        (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Return         ; }
565 int be_is_IncSP         (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_IncSP          ; }
566 int be_is_SetSP         (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_SetSP          ; }
567 int be_is_AddSP         (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_AddSP          ; }
568 int be_is_RegParams     (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_RegParams      ; }
569 int be_is_StackParam    (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_StackParam     ; }
570 int be_is_FrameAddr     (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameAddr      ; }
571 int be_is_FrameLoad     (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameLoad      ; }
572 int be_is_FrameStore    (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameStore     ; }
573 int be_is_Barrier       (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Barrier        ; }
574
575 int be_has_frame_entity(const ir_node *irn)
576 {
577         switch(be_get_irn_opcode(irn)) {
578         case beo_StackParam:
579         case beo_Spill:
580         case beo_Reload:
581         case beo_FrameStore:
582         case beo_FrameLoad:
583         case beo_FrameAddr:
584                 return 1;
585         }
586
587         return 0;
588 }
589
590 entity *be_get_frame_entity(const ir_node *irn)
591 {
592         if(be_has_frame_entity(irn)) {
593                 be_frame_attr_t *a = get_irn_attr(irn);
594                 return a->ent;
595         }
596         return NULL;
597 }
598
599 static void be_limited(void *data, bitset_t *bs)
600 {
601         be_req_t *req = data;
602
603         switch(req->kind) {
604         case be_req_kind_negate_old_limited:
605         case be_req_kind_old_limited:
606                 req->x.old_limited.old_limited(req->x.old_limited.old_limited_env, bs);
607                 if(req->kind == be_req_kind_negate_old_limited)
608                         bitset_flip_all(bs);
609                 break;
610         case be_req_kind_single_reg:
611                 bitset_clear_all(bs);
612                 bitset_set(bs, req->x.single_reg->index);
613                 break;
614         }
615 }
616
617 static INLINE be_req_t *get_req(ir_node *irn, int pos)
618 {
619         int idx           = pos < 0 ? -(pos + 1) : pos;
620         be_node_attr_t *a = get_irn_attr(irn);
621         be_reg_data_t *rd = &a->reg_data[idx];
622         be_req_t       *r = pos < 0 ? &rd->req : &rd->in_req;
623
624         assert(is_be_node(irn));
625         assert(!(pos >= 0) || pos < get_irn_arity(irn));
626         assert(!(pos < 0)  || -(pos + 1) <= a->max_reg_data);
627
628         return r;
629 }
630
631 void be_set_constr_single_reg(ir_node *irn, int pos, const arch_register_t *reg)
632 {
633         be_req_t *r = get_req(irn, pos);
634
635         r->kind            = be_req_kind_single_reg;
636         r->x.single_reg    = reg;
637         r->req.limited     = be_limited;
638         r->req.limited_env = r;
639         r->req.type        = arch_register_req_type_limited;
640         r->req.cls         = reg->reg_class;
641 }
642
643 void be_set_constr_limited(ir_node *irn, int pos, const arch_register_req_t *req)
644 {
645         be_req_t *r = get_req(irn, pos);
646
647         assert(arch_register_req_is(req, limited));
648
649         r->kind            = be_req_kind_old_limited;
650         r->req.limited     = be_limited;
651         r->req.limited_env = r;
652         r->req.type        = arch_register_req_type_limited;
653         r->req.cls         = req->cls;
654
655         r->x.old_limited.old_limited     = req->limited;
656         r->x.old_limited.old_limited_env = req->limited_env;
657 }
658
659 void be_node_set_flags(ir_node *irn, int pos, arch_irn_flags_t flags)
660 {
661         be_req_t *r = get_req(irn, pos);
662         r->flags = flags;
663 }
664
665 void be_node_set_reg_class(ir_node *irn, int pos, const arch_register_class_t *cls)
666 {
667         be_req_t *r = get_req(irn, pos);
668         r->req.cls = cls;
669         if(r->req.type == arch_register_req_type_none)
670                 r->req.type = arch_register_req_type_normal;
671 }
672
673 void be_node_set_req_type(ir_node *irn, int pos, arch_register_req_type_t type)
674 {
675         be_req_t *r = get_req(irn, pos);
676         r->req.type = type;
677 }
678
679 void be_set_IncSP_offset(ir_node *irn, unsigned offset)
680 {
681         be_stack_attr_t *a = get_irn_attr(irn);
682         assert(be_is_IncSP(irn));
683         a->offset = offset;
684 }
685
686 unsigned be_get_IncSP_offset(const ir_node *irn)
687 {
688         be_stack_attr_t *a = get_irn_attr(irn);
689         assert(be_is_IncSP(irn));
690         return a->offset;
691 }
692
693 void be_set_IncSP_direction(ir_node *irn, be_stack_dir_t dir)
694 {
695         be_stack_attr_t *a = get_irn_attr(irn);
696         assert(be_is_IncSP(irn));
697         a->dir = dir;
698 }
699
700 be_stack_dir_t be_get_IncSP_direction(const ir_node *irn)
701 {
702         be_stack_attr_t *a = get_irn_attr(irn);
703         assert(be_is_IncSP(irn));
704         return a->dir;
705 }
706
707 void be_set_Spill_entity(ir_node *irn, entity *ent)
708 {
709         be_spill_attr_t *a = get_irn_attr(irn);
710         assert(be_is_Spill(irn));
711         a->frame_attr.ent = ent;
712 }
713
714 static ir_node *find_a_spill_walker(ir_node *irn, unsigned visited_nr)
715 {
716         unsigned nr = get_irn_visited(irn);
717
718         set_irn_visited(irn, visited_nr);
719
720         if(is_Phi(irn)) {
721                 int i, n;
722                 if(nr < visited_nr) {
723                         for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
724                                 ir_node *n = find_a_spill_walker(get_irn_n(irn, i), visited_nr);
725                                 if(n != NULL)
726                                         return n;
727                         }
728                 }
729         }
730
731         else if(be_get_irn_opcode(irn) == beo_Spill)
732                 return irn;
733
734         return NULL;
735 }
736
737 ir_node *be_get_Spill_context(const ir_node *irn) {
738         const be_spill_attr_t *a = get_irn_attr(irn);
739         assert(be_is_Spill(irn));
740         return a->spill_ctx;
741 }
742
743 /**
744  * Finds a spill for a reload.
745  * If the reload is directly using the spill, this is simple,
746  * else we perform DFS from the reload (over all PhiMs) and return
747  * the first spill node we find.
748  */
749 static INLINE ir_node *find_a_spill(const ir_node *irn)
750 {
751         ir_graph *irg       = get_irn_irg(irn);
752         unsigned visited_nr = get_irg_visited(irg) + 1;
753
754         assert(be_is_Reload(irn));
755         set_irg_visited(irg, visited_nr);
756         return find_a_spill_walker(be_get_Reload_mem(irn), visited_nr);
757 }
758
759 entity *be_get_spill_entity(const ir_node *irn)
760 {
761         int opc           = get_irn_opcode(irn);
762
763         switch(be_get_irn_opcode(irn)) {
764         case beo_Reload:
765                 {
766                         ir_node *spill = find_a_spill(irn);
767                         return be_get_spill_entity(spill);
768                 }
769         case beo_Spill:
770                 {
771                         be_spill_attr_t *a = get_irn_attr(irn);
772                         return a->frame_attr.ent;
773                 }
774         default:
775                 assert(0 && "Must give spill/reload node");
776                 break;
777         }
778
779         return NULL;
780 }
781
782 static void link_reload_walker(ir_node *irn, void *data)
783 {
784         ir_node **root = (ir_node **) data;
785         if(be_is_Reload(irn)) {
786                 set_irn_link(irn, *root);
787                 *root = irn;
788         }
789 }
790
791 void be_copy_entities_to_reloads(ir_graph *irg)
792 {
793         ir_node *irn = NULL;
794         irg_walk_graph(irg, link_reload_walker, NULL, (void *) &irn);
795
796         while(irn) {
797                 be_frame_attr_t *a = get_irn_attr(irn);
798                 entity *ent        = be_get_spill_entity(irn);
799                 a->ent = ent;
800                 irn    = get_irn_link(irn);
801         }
802 }
803
804 ir_node *be_spill(const arch_env_t *arch_env, ir_node *irn, ir_node *ctx)
805 {
806         ir_node *bl     = get_nodes_block(irn);
807         ir_graph *irg   = get_irn_irg(bl);
808         ir_node *frame  = get_irg_frame(irg);
809         ir_node *insert = bl;
810         ir_node *spill;
811
812         const arch_register_class_t *cls       = arch_get_irn_reg_class(arch_env, irn, -1);
813         const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
814
815         spill = be_new_Spill(cls, cls_frame, irg, bl, frame, irn, ctx);
816
817         /*
818          * search the right insertion point. a spill of a phi cannot be put
819          * directly after the phi, if there are some phis behind the one which
820          * is spilled. Also, a spill of a Proj must be after all Projs of the
821          * same tuple node.
822          *
823          * Here's one special case:
824          * If the spill is in the start block, the spill must be after the frame
825          * pointer is set up. This is done by setting insert to the end of the block
826          * which is its default initialization (see above).
827          */
828
829         insert = sched_next(irn);
830         if(bl == get_irg_start_block(irg) && insert != bl && sched_get_time_step(frame) >= sched_get_time_step(insert))
831                 insert = sched_next(frame);
832
833         while((is_Phi(insert) || is_Proj(insert)) && !sched_is_end(insert))
834                 insert = sched_next(insert);
835
836         sched_add_before(insert, spill);
837         return spill;
838 }
839
840 ir_node *be_reload(const arch_env_t *arch_env, const arch_register_class_t *cls, ir_node *insert, ir_mode *mode, ir_node *spill)
841 {
842         ir_node *reload;
843
844         ir_node *bl    = is_Block(insert) ? insert : get_nodes_block(insert);
845         ir_graph *irg  = get_irn_irg(bl);
846         ir_node *frame = get_irg_frame(irg);
847         const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
848
849         assert(be_is_Spill(spill) || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
850
851         reload = be_new_Reload(cls, cls_frame, irg, bl, frame, spill, mode);
852
853         if(is_Block(insert)) {
854                 insert = sched_skip(insert, 0, sched_skip_cf_predicator, (void *) arch_env);
855                 sched_add_after(insert, reload);
856         }
857
858         else
859                 sched_add_before(insert, reload);
860
861         return reload;
862 }
863
864 /*
865   ____              ____
866  |  _ \ ___  __ _  |  _ \ ___  __ _ ___
867  | |_) / _ \/ _` | | |_) / _ \/ _` / __|
868  |  _ <  __/ (_| | |  _ <  __/ (_| \__ \
869  |_| \_\___|\__, | |_| \_\___|\__, |___/
870             |___/                |_|
871
872 */
873
874
875 static void *put_out_reg_req(arch_register_req_t *req, const ir_node *irn, int out_pos)
876 {
877         const be_node_attr_t *a = get_irn_attr(irn);
878
879         if(out_pos < a->max_reg_data) {
880                 memcpy(req, &a->reg_data[out_pos].req, sizeof(req[0]));
881
882                 if(be_is_Copy(irn)) {
883                         req->type |= arch_register_req_type_should_be_same;
884                         req->other_same = get_irn_n(irn, be_pos_Copy_orig);
885                 }
886         }
887         else {
888                 req->type = arch_register_req_type_none;
889                 req->cls  = NULL;
890         }
891
892         return req;
893 }
894
895 static void *put_in_reg_req(arch_register_req_t *req, const ir_node *irn, int pos)
896 {
897         const be_node_attr_t *a = get_irn_attr(irn);
898         int n                   = get_irn_arity(irn);
899
900         if(pos < get_irn_arity(irn) && pos < a->max_reg_data)
901                 memcpy(req, &a->reg_data[pos].in_req, sizeof(req[0]));
902         else {
903                 req->type = arch_register_req_type_none;
904                 req->cls  = NULL;
905         }
906
907         return req;
908 }
909
910 static const arch_register_req_t *
911 be_node_get_irn_reg_req(const void *self, arch_register_req_t *req, const ir_node *irn, int pos)
912 {
913         int out_pos = pos;
914
915         if(pos < 0) {
916                 if(get_irn_mode(irn) == mode_T)
917                         return NULL;
918
919                 out_pos = redir_proj((const ir_node **) &irn, pos);
920                 assert(is_be_node(irn));
921                 return put_out_reg_req(req, irn, out_pos);
922         }
923
924         else {
925                 return is_be_node(irn) ? put_in_reg_req(req, irn, pos) : NULL;
926         }
927
928         return req;
929 }
930
931 const arch_register_t *
932 be_node_get_irn_reg(const void *_self, const ir_node *irn)
933 {
934         int out_pos;
935         be_node_attr_t *a;
936
937         out_pos = redir_proj((const ir_node **) &irn, -1);
938         a       = get_irn_attr(irn);
939
940         assert(is_be_node(irn));
941         assert(out_pos < a->max_reg_data && "position too high");
942
943         return a->reg_data[out_pos].reg;
944 }
945
946 static arch_irn_class_t be_node_classify(const void *_self, const ir_node *irn)
947 {
948         redir_proj((const ir_node **) &irn, -1);
949
950         switch(be_get_irn_opcode(irn)) {
951 #define XXX(a,b) case beo_ ## a: return arch_irn_class_ ## b;
952                 XXX(Spill, spill)
953                 XXX(Reload, reload)
954                 XXX(Perm, perm)
955                 XXX(Copy, copy)
956 #undef XXX
957                 default:
958                 return 0;
959         }
960
961         return 0;
962 }
963
964 static arch_irn_flags_t be_node_get_flags(const void *_self, const ir_node *irn)
965 {
966         int out_pos;
967         be_node_attr_t *a;
968
969         out_pos = redir_proj((const ir_node **) &irn, -1);
970         a       = get_irn_attr(irn);
971
972         assert(is_be_node(irn));
973         assert(out_pos < a->max_reg_data && "position too high");
974
975         return a->reg_data[out_pos].req.flags;
976 }
977
978 static entity *be_node_get_frame_entity(const void *self, const ir_node *irn)
979 {
980         return be_get_frame_entity(irn);
981 }
982
983 static void be_node_set_frame_offset(const void *self, ir_node *irn, int offset)
984 {
985         if(be_has_frame_entity(irn)) {
986                 be_frame_attr_t *a = get_irn_attr(irn);
987                 a->offset = offset;
988         }
989 }
990
991 /*
992   ___ ____  _   _   _   _                 _ _
993  |_ _|  _ \| \ | | | | | | __ _ _ __   __| | | ___ _ __
994   | || |_) |  \| | | |_| |/ _` | '_ \ / _` | |/ _ \ '__|
995   | ||  _ <| |\  | |  _  | (_| | | | | (_| | |  __/ |
996  |___|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
997
998 */
999
1000 static const arch_irn_ops_if_t be_node_irn_ops_if = {
1001         be_node_get_irn_reg_req,
1002         be_node_set_irn_reg,
1003         be_node_get_irn_reg,
1004         be_node_classify,
1005         be_node_get_flags,
1006         be_node_get_frame_entity,
1007         be_node_set_frame_offset
1008 };
1009
1010 static const arch_irn_ops_t be_node_irn_ops = {
1011         &be_node_irn_ops_if
1012 };
1013
1014 const void *be_node_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn)
1015 {
1016         redir_proj((const ir_node **) &irn, -1);
1017         return is_be_node(irn) ? &be_node_irn_ops : NULL;
1018 }
1019
1020 const arch_irn_handler_t be_node_irn_handler = {
1021         be_node_get_irn_ops
1022 };
1023
1024 /*
1025   ____  _     _   ___ ____  _   _   _   _                 _ _
1026  |  _ \| |__ (_) |_ _|  _ \| \ | | | | | | __ _ _ __   __| | | ___ _ __
1027  | |_) | '_ \| |  | || |_) |  \| | | |_| |/ _` | '_ \ / _` | |/ _ \ '__|
1028  |  __/| | | | |  | ||  _ <| |\  | |  _  | (_| | | | | (_| | |  __/ |
1029  |_|   |_| |_|_| |___|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
1030
1031 */
1032
1033 typedef struct {
1034         arch_irn_handler_t irn_handler;
1035         arch_irn_ops_t     irn_ops;
1036         const arch_env_t   *arch_env;
1037         pmap               *regs;
1038 } phi_handler_t;
1039
1040 #define get_phi_handler_from_handler(h)  container_of(h, phi_handler_t, irn_handler)
1041 #define get_phi_handler_from_ops(h)      container_of(h, phi_handler_t, irn_ops)
1042
1043 static const void *phi_get_irn_ops(const arch_irn_handler_t *handler, const ir_node *irn)
1044 {
1045         const phi_handler_t *h = get_phi_handler_from_handler(handler);
1046         return is_Phi(irn) && mode_is_datab(get_irn_mode(irn)) ? &h->irn_ops : NULL;
1047 }
1048
1049 /**
1050  * Get register class of a Phi.
1051  *
1052  */
1053 static const arch_register_req_t *get_Phi_reg_req_recursive(const phi_handler_t *h, arch_register_req_t *req, const ir_node *phi, pset **visited)
1054 {
1055         int n = get_irn_arity(phi);
1056         ir_node *op;
1057         int done = 0;
1058         int i;
1059
1060         if(*visited && pset_find_ptr(*visited, phi))
1061                 return NULL;
1062
1063         for(i = 0; i < n; ++i) {
1064                 op = get_irn_n(phi, i);
1065                 if(!is_Phi(op))
1066                         return arch_get_register_req(h->arch_env, req, op, BE_OUT_POS(0));
1067         }
1068
1069         /*
1070         The operands of that Phi were all Phis themselves.
1071         We have to start a DFS for a non-Phi argument now.
1072         */
1073         if(!*visited)
1074                 *visited = pset_new_ptr(16);
1075
1076         pset_insert_ptr(*visited, phi);
1077
1078         for(i = 0; i < n; ++i) {
1079                 op = get_irn_n(phi, i);
1080                 if(get_Phi_reg_req_recursive(h, req, op, visited))
1081                         return req;
1082         }
1083
1084         return NULL;
1085 }
1086
1087 static const arch_register_req_t *phi_get_irn_reg_req(const void *self, arch_register_req_t *req, const ir_node *irn, int pos)
1088 {
1089         phi_handler_t *phi_handler = get_phi_handler_from_ops(self);
1090         pset *visited              = NULL;
1091
1092         get_Phi_reg_req_recursive(phi_handler, req, irn, &visited);
1093         /* Set the requirements type to normal, since an operand of the Phi could have had constraints. */
1094         req->type = arch_register_req_type_normal;
1095         if(visited)
1096                 del_pset(visited);
1097
1098         return req;
1099 }
1100
1101 static void phi_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg)
1102 {
1103         phi_handler_t *h = get_phi_handler_from_ops(self);
1104         pmap_insert(h->regs, irn, (void *) reg);
1105 }
1106
1107 static const arch_register_t *phi_get_irn_reg(const void *self, const ir_node *irn)
1108 {
1109         phi_handler_t *h = get_phi_handler_from_ops(self);
1110         return pmap_get(h->regs, (void *) irn);
1111 }
1112
1113 static arch_irn_class_t phi_classify(const void *_self, const ir_node *irn)
1114 {
1115         return arch_irn_class_normal;
1116 }
1117
1118 static arch_irn_flags_t phi_get_flags(const void *_self, const ir_node *irn)
1119 {
1120         return arch_irn_flags_none;
1121 }
1122
1123 static entity *phi_get_frame_entity(const void *_self, const ir_node *irn)
1124 {
1125         return NULL;
1126 }
1127
1128 static void phi_set_frame_offset(const void *_self, ir_node *irn, int bias)
1129 {
1130 }
1131
1132 static const arch_irn_ops_if_t phi_irn_ops = {
1133         phi_get_irn_reg_req,
1134         phi_set_irn_reg,
1135         phi_get_irn_reg,
1136         phi_classify,
1137         phi_get_flags,
1138         phi_get_frame_entity,
1139         phi_set_frame_offset
1140 };
1141
1142 static const arch_irn_handler_t phi_irn_handler = {
1143         phi_get_irn_ops
1144 };
1145
1146 arch_irn_handler_t *be_phi_handler_new(const arch_env_t *arch_env)
1147 {
1148         phi_handler_t *h           = xmalloc(sizeof(h[0]));
1149         h->irn_handler.get_irn_ops = phi_get_irn_ops;
1150         h->irn_ops.impl            = &phi_irn_ops;
1151         h->arch_env                = arch_env;
1152         h->regs                    = pmap_create();
1153         return (arch_irn_handler_t *) h;
1154 }
1155
1156 void be_phi_handler_free(arch_irn_handler_t *handler)
1157 {
1158         phi_handler_t *h = (void *) handler;
1159         pmap_destroy(h->regs);
1160         free(handler);
1161 }
1162
1163 const void *be_phi_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn)
1164 {
1165         phi_handler_t *phi_handler = get_phi_handler_from_handler(self);
1166         return is_Phi(irn) ? &phi_handler->irn_ops : NULL;
1167 }
1168
1169 void be_phi_handler_reset(arch_irn_handler_t *handler)
1170 {
1171         phi_handler_t *h = get_phi_handler_from_handler(handler);
1172         if(h->regs)
1173                 pmap_destroy(h->regs);
1174         h->regs = pmap_create();
1175 }
1176
1177
1178 /*
1179   _   _           _        ____                        _
1180  | \ | | ___   __| | ___  |  _ \ _   _ _ __ ___  _ __ (_)_ __   __ _
1181  |  \| |/ _ \ / _` |/ _ \ | | | | | | | '_ ` _ \| '_ \| | '_ \ / _` |
1182  | |\  | (_) | (_| |  __/ | |_| | |_| | | | | | | |_) | | | | | (_| |
1183  |_| \_|\___/ \__,_|\___| |____/ \__,_|_| |_| |_| .__/|_|_| |_|\__, |
1184                                                 |_|            |___/
1185 */
1186
1187 static void dump_node_req(FILE *f, int idx, be_req_t *req)
1188 {
1189         unsigned i;
1190         int did_something = 0;
1191         char buf[16];
1192         const char *prefix = buf;
1193
1194         snprintf(buf, sizeof(buf), "#%d ", idx);
1195
1196         if(req->flags != arch_irn_flags_none) {
1197                 fprintf(f, "%sflags: ", prefix);
1198                 prefix = "";
1199                 for(i = arch_irn_flags_none; i <= log2_ceil(arch_irn_flags_last); ++i) {
1200                         if(req->flags & (1 << i)) {
1201                                 fprintf(f, "%s%s", prefix, arch_irn_flag_str(1 << i));
1202                                 prefix = "|";
1203                         }
1204                 }
1205                 prefix = ", ";
1206                 did_something = 1;
1207         }
1208
1209         if(req->req.cls != 0) {
1210                 char tmp[256];
1211                 fprintf(f, prefix);
1212                 arch_register_req_format(tmp, sizeof(tmp), &req->req);
1213                 fprintf(f, "%s", tmp);
1214                 did_something = 1;
1215         }
1216
1217         if(did_something)
1218                 fprintf(f, "\n");
1219 }
1220
1221 static void dump_node_reqs(FILE *f, ir_node *irn)
1222 {
1223         int i;
1224         be_node_attr_t *a = get_irn_attr(irn);
1225
1226         fprintf(f, "registers: \n");
1227         for(i = 0; i < a->max_reg_data; ++i) {
1228                 be_reg_data_t *rd = &a->reg_data[i];
1229                 if(rd->reg)
1230                         fprintf(f, "#%d: %s\n", i, rd->reg->name);
1231         }
1232
1233         fprintf(f, "in requirements\n");
1234         for(i = 0; i < a->max_reg_data; ++i) {
1235                 dump_node_req(f, i, &a->reg_data[i].in_req);
1236         }
1237
1238         fprintf(f, "\nout requirements\n");
1239         for(i = 0; i < a->max_reg_data; ++i) {
1240                 dump_node_req(f, i, &a->reg_data[i].req);
1241         }
1242 }
1243
1244 static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason)
1245 {
1246         be_node_attr_t *at = get_irn_attr(irn);
1247
1248         assert(is_be_node(irn));
1249
1250         switch(reason) {
1251                 case dump_node_opcode_txt:
1252                         fprintf(f, get_op_name(get_irn_op(irn)));
1253                         break;
1254                 case dump_node_mode_txt:
1255                         fprintf(f, get_mode_name(get_irn_mode(irn)));
1256                         break;
1257                 case dump_node_nodeattr_txt:
1258                         break;
1259                 case dump_node_info_txt:
1260                         dump_node_reqs(f, irn);
1261
1262                         if(be_has_frame_entity(irn)) {
1263                                 be_frame_attr_t *a = (be_frame_attr_t *) at;
1264                                 if (a->ent)
1265                                         ir_fprintf(f, "frame entity: %+F offset %x (%d)\n", a->ent, a->offset, a->offset);
1266
1267                         }
1268
1269                         switch(be_get_irn_opcode(irn)) {
1270                         case beo_Spill:
1271                                 {
1272                                         be_spill_attr_t *a = (be_spill_attr_t *) at;
1273                                         ir_fprintf(f, "spill context: %+F\n", a->spill_ctx);
1274                                 }
1275                                 break;
1276
1277                         case beo_IncSP:
1278                                 {
1279                                         be_stack_attr_t *a = (be_stack_attr_t *) at;
1280                                         fprintf(f, "offset: %u\n", a->offset);
1281                                         fprintf(f, "direction: %s\n", a->dir == be_stack_dir_along ? "along" : "against");
1282                                 }
1283                                 break;
1284                         }
1285
1286         }
1287
1288         return 0;
1289 }
1290
1291 /**
1292  * Copies the backend specific attributes from old node to new node.
1293  */
1294 static void copy_attr(const ir_node *old_node, ir_node *new_node)
1295 {
1296         be_node_attr_t *old_attr = get_irn_attr(old_node);
1297         be_node_attr_t *new_attr = get_irn_attr(new_node);
1298         int i;
1299
1300         assert(is_be_node(old_node));
1301         assert(is_be_node(new_node));
1302
1303         memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
1304         new_attr->reg_data = NULL;
1305
1306         if(new_attr->max_reg_data > 0) {
1307                 new_attr->reg_data = NEW_ARR_D(be_reg_data_t, get_irg_obstack(get_irn_irg(new_node)), new_attr->max_reg_data);
1308                 memcpy(new_attr->reg_data, old_attr->reg_data, new_attr->max_reg_data * sizeof(be_reg_data_t));
1309
1310                 for(i = 0; i < old_attr->max_reg_data; ++i) {
1311                         be_req_t *r;
1312
1313                         r = &new_attr->reg_data[i].req;
1314                         r->req.limited_env = r;
1315
1316                         r = &new_attr->reg_data[i].in_req;
1317                         r->req.limited_env = r;
1318                 }
1319         }
1320 }
1321
1322 static const ir_op_ops be_node_op_ops = {
1323         NULL,
1324         NULL,
1325         NULL,
1326         NULL,
1327         NULL,
1328         copy_attr,
1329         NULL,
1330         NULL,
1331         NULL,
1332         NULL,
1333         NULL,
1334         dump_node,
1335         NULL
1336 };