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