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