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