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