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