b3b7b88787d9ffcf8bf91e009329c8abd14b1485
[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, int offset)
546 {
547         be_stack_attr_t *a;
548         ir_node *irn;
549         ir_node *in[1];
550
551         in[0]     = old_sp;
552         irn       = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode, sizeof(in) / sizeof(in[0]), in);
553         a         = init_node_attr(irn, 1);
554         a->offset = offset;
555
556         be_node_set_flags(irn, -1, arch_irn_flags_ignore | arch_irn_flags_modify_sp);
557
558         /* Set output constraint to stack register. */
559         be_node_set_reg_class(irn, 0, sp->reg_class);
560         be_set_constr_single_reg(irn, BE_OUT_POS(0), sp);
561         be_node_set_irn_reg(NULL, irn, sp);
562
563         return irn;
564 }
565
566 ir_node *be_new_AddSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, ir_node *sz)
567 {
568         be_node_attr_t *a;
569         ir_node *irn;
570         ir_node *in[be_pos_AddSP_last];
571
572         in[be_pos_AddSP_old_sp] = old_sp;
573         in[be_pos_AddSP_size]   = sz;
574
575         irn = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T, be_pos_AddSP_last, in);
576         a   = init_node_attr(irn, be_pos_AddSP_last);
577
578         be_node_set_flags(irn, OUT_POS(pn_be_AddSP_res), arch_irn_flags_ignore | arch_irn_flags_modify_sp);
579
580         /* Set output constraint to stack register. */
581         be_set_constr_single_reg(irn, be_pos_AddSP_old_sp, sp);
582         be_node_set_reg_class(irn, be_pos_AddSP_size, arch_register_get_class(sp));
583         be_set_constr_single_reg(irn, OUT_POS(pn_be_AddSP_res), sp);
584         a->reg_data[pn_be_AddSP_res].reg = sp;
585
586         return irn;
587 }
588
589 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)
590 {
591         be_node_attr_t *a;
592         ir_node *irn;
593         ir_node *in[3];
594
595         in[0]    = mem;
596         in[1]    = old_sp;
597         in[2]    = op;
598         irn      = new_ir_node(NULL, irg, bl, op_be_SetSP, get_irn_mode(old_sp), 3, in);
599         a        = init_node_attr(irn, 3);
600
601         be_node_set_flags(irn, OUT_POS(0), arch_irn_flags_ignore | arch_irn_flags_modify_sp);
602
603         /* Set output constraint to stack register. */
604         be_set_constr_single_reg(irn, OUT_POS(0), sp);
605         be_node_set_reg_class(irn, 1, sp->reg_class);
606         be_node_set_reg_class(irn, 2, sp->reg_class);
607         be_node_set_irn_reg(NULL, irn, sp);
608
609         return irn;
610 }
611
612 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)
613 {
614         be_frame_attr_t *a;
615         ir_node *irn;
616         ir_node *in[1];
617
618         in[0] = frame_pointer;
619         irn = new_ir_node(NULL, irg, bl, op_be_StackParam, mode, 1, in);
620         a = init_node_attr(irn, 1);
621         a->ent = ent;
622
623         be_node_set_reg_class(irn, 0, cls_frame);
624         be_node_set_reg_class(irn, OUT_POS(0), cls);
625         return irn;
626 }
627
628 ir_node *be_new_RegParams(ir_graph *irg, ir_node *bl, int n_outs)
629 {
630         ir_node *irn;
631         ir_node *in[1];
632
633         irn = new_ir_node(NULL, irg, bl, op_be_RegParams, mode_T, 0, in);
634         init_node_attr(irn, n_outs);
635         return irn;
636 }
637
638 ir_node *be_new_FrameLoad(const arch_register_class_t *cls_frame, const arch_register_class_t *cls_data,
639                                                   ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *frame, entity *ent)
640 {
641         be_frame_attr_t *a;
642         ir_node *irn;
643         ir_node *in[2];
644
645         in[0]  = mem;
646         in[1]  = frame;
647         irn    = new_ir_node(NULL, irg, bl, op_be_FrameLoad, mode_T, 2, in);
648         a      = init_node_attr(irn, 3);
649         a->ent = ent;
650         a->offset = 0;
651         be_node_set_reg_class(irn, 1, cls_frame);
652         be_node_set_reg_class(irn, OUT_POS(pn_Load_res), cls_data);
653         return irn;
654 }
655
656 ir_node *be_new_FrameStore(const arch_register_class_t *cls_frame, const arch_register_class_t *cls_data,
657                                                    ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *frame, ir_node *data, entity *ent)
658 {
659         be_frame_attr_t *a;
660         ir_node *irn;
661         ir_node *in[3];
662
663         in[0]  = mem;
664         in[1]  = frame;
665         in[2]  = data;
666         irn    = new_ir_node(NULL, irg, bl, op_be_FrameStore, mode_T, 3, in);
667         a      = init_node_attr(irn, 3);
668         a->ent = ent;
669         a->offset = 0;
670         be_node_set_reg_class(irn, 1, cls_frame);
671         be_node_set_reg_class(irn, 2, cls_data);
672         return irn;
673 }
674
675 ir_node *be_new_FrameAddr(const arch_register_class_t *cls_frame, ir_graph *irg, ir_node *bl, ir_node *frame, entity *ent)
676 {
677         be_frame_attr_t *a;
678         ir_node *irn;
679         ir_node *in[1];
680
681         in[0]  = frame;
682         irn    = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
683         a      = init_node_attr(irn, 1);
684         a->ent = ent;
685         a->offset = 0;
686         be_node_set_reg_class(irn, 0, cls_frame);
687         be_node_set_reg_class(irn, OUT_POS(0), cls_frame);
688
689         return optimize_node(irn);
690 }
691
692 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)
693 {
694         ir_node *irn;
695         ir_node **in = (ir_node **) alloca((n + 1) * sizeof(in[0]));
696
697         in[0] = src;
698         memcpy(&in[1], in_keep, n * sizeof(in[0]));
699         irn   = new_ir_node(NULL, irg, bl, op_be_CopyKeep, mode, n + 1, in);
700         init_node_attr(irn, n + 1);
701         be_node_set_reg_class(irn, OUT_POS(0), cls);
702         be_node_set_reg_class(irn, 0, cls);
703
704         return irn;
705 }
706
707 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)
708 {
709         ir_node *in[1];
710
711         in[0] = keep;
712         return be_new_CopyKeep(cls, irg, bl, src, 1, in, mode);
713 }
714
715 ir_node *be_get_CopyKeep_op(const ir_node *cpy) {
716         return get_irn_n(cpy, be_pos_CopyKeep_op);
717 }
718
719 void be_set_CopyKeep_op(ir_node *cpy, ir_node *op) {
720         set_irn_n(cpy, be_pos_CopyKeep_op, op);
721 }
722
723 ir_node *be_new_Barrier(ir_graph *irg, ir_node *bl, int n, ir_node *in[])
724 {
725         ir_node *irn;
726
727         irn = new_ir_node(NULL, irg, bl, op_be_Barrier, mode_T, n, in);
728         init_node_attr(irn, n);
729         return irn;
730 }
731
732 int be_is_Spill         (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Spill          ; }
733 int be_is_Reload        (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Reload         ; }
734 int be_is_Copy          (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Copy           ; }
735 int be_is_CopyKeep      (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_CopyKeep       ; }
736 int be_is_Perm          (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Perm           ; }
737 int be_is_MemPerm       (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_MemPerm        ; }
738 int be_is_Keep          (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Keep           ; }
739 int be_is_Call          (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Call           ; }
740 int be_is_Return        (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Return         ; }
741 int be_is_IncSP         (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_IncSP          ; }
742 int be_is_SetSP         (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_SetSP          ; }
743 int be_is_AddSP         (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_AddSP          ; }
744 int be_is_RegParams     (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_RegParams      ; }
745 int be_is_StackParam    (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_StackParam     ; }
746 int be_is_FrameAddr     (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameAddr      ; }
747 int be_is_FrameLoad     (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameLoad      ; }
748 int be_is_FrameStore    (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameStore     ; }
749 int be_is_Barrier       (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Barrier        ; }
750
751 int be_has_frame_entity(const ir_node *irn)
752 {
753         switch(be_get_irn_opcode(irn)) {
754         case beo_StackParam:
755         case beo_Spill:
756         case beo_Reload:
757         case beo_FrameStore:
758         case beo_FrameLoad:
759         case beo_FrameAddr:
760                 return 1;
761         default:
762                 return 0;
763         }
764 }
765
766 entity* be_get_frame_entity(const ir_node *irn)
767 {
768         if(be_has_frame_entity(irn)) {
769                 be_frame_attr_t *a = get_irn_attr(irn);
770                 return a->ent;
771         }
772         return NULL;
773 }
774
775 void be_set_MemPerm_in_entity(const ir_node *irn, int n, entity *ent)
776 {
777         be_memperm_attr_t *attr = get_irn_attr(irn);
778
779         assert(be_is_MemPerm(irn));
780         assert(n < be_get_MemPerm_entity_arity(irn));
781
782         attr->in_entities[n] = ent;
783 }
784
785 entity* be_get_MemPerm_in_entity(const ir_node* irn, int n)
786 {
787         be_memperm_attr_t *attr = get_irn_attr(irn);
788
789         assert(be_is_MemPerm(irn));
790         assert(n < be_get_MemPerm_entity_arity(irn));
791
792         return attr->in_entities[n];
793 }
794
795 void be_set_MemPerm_out_entity(const ir_node *irn, int n, entity *ent)
796 {
797         be_memperm_attr_t *attr = get_irn_attr(irn);
798
799         assert(be_is_MemPerm(irn));
800         assert(n < be_get_MemPerm_entity_arity(irn));
801
802         attr->out_entities[n] = ent;
803 }
804
805 entity* be_get_MemPerm_out_entity(const ir_node* irn, int n)
806 {
807         be_memperm_attr_t *attr = get_irn_attr(irn);
808
809         assert(be_is_MemPerm(irn));
810         assert(n < be_get_MemPerm_entity_arity(irn));
811
812         return attr->out_entities[n];
813 }
814
815 int be_get_MemPerm_entity_arity(const ir_node *irn)
816 {
817         return get_irn_arity(irn) - 1;
818 }
819
820 static void be_limited(void *data, bitset_t *bs)
821 {
822         be_req_t *req = data;
823
824         switch(req->kind) {
825         case be_req_kind_negate_old_limited:
826         case be_req_kind_old_limited:
827                 req->x.old_limited.old_limited(req->x.old_limited.old_limited_env, bs);
828                 if(req->kind == be_req_kind_negate_old_limited)
829                         bitset_flip_all(bs);
830                 break;
831         case be_req_kind_single_reg:
832                 bitset_clear_all(bs);
833                 bitset_set(bs, req->x.single_reg->index);
834                 break;
835         }
836 }
837
838 static INLINE be_req_t *get_req(ir_node *irn, int pos)
839 {
840         int idx           = pos < 0 ? -(pos + 1) : pos;
841         be_node_attr_t *a = get_irn_attr(irn);
842         be_reg_data_t *rd = &a->reg_data[idx];
843         be_req_t       *r = pos < 0 ? &rd->req : &rd->in_req;
844
845         assert(is_be_node(irn));
846         assert(!(pos >= 0) || pos < get_irn_arity(irn));
847         assert(!(pos < 0)  || -(pos + 1) <= a->max_reg_data);
848
849         return r;
850 }
851
852 void be_set_constr_single_reg(ir_node *irn, int pos, const arch_register_t *reg)
853 {
854         be_req_t *r = get_req(irn, pos);
855
856         r->kind            = be_req_kind_single_reg;
857         r->x.single_reg    = reg;
858         r->req.limited     = be_limited;
859         r->req.limited_env = r;
860         r->req.type        = arch_register_req_type_limited;
861         r->req.cls         = reg->reg_class;
862 }
863
864 void be_set_constr_limited(ir_node *irn, int pos, const arch_register_req_t *req)
865 {
866         be_req_t *r = get_req(irn, pos);
867
868         assert(arch_register_req_is(req, limited));
869
870         r->kind            = be_req_kind_old_limited;
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         = req->cls;
875
876         r->x.old_limited.old_limited     = req->limited;
877         r->x.old_limited.old_limited_env = req->limited_env;
878 }
879
880 void be_node_set_flags(ir_node *irn, int pos, arch_irn_flags_t flags)
881 {
882         be_req_t *r = get_req(irn, pos);
883         r->flags = flags;
884 }
885
886 void be_node_set_reg_class(ir_node *irn, int pos, const arch_register_class_t *cls)
887 {
888         be_req_t *r = get_req(irn, pos);
889         r->req.cls = cls;
890         if(r->req.type == arch_register_req_type_none)
891                 r->req.type = arch_register_req_type_normal;
892 }
893
894 void be_node_set_req_type(ir_node *irn, int pos, arch_register_req_type_t type)
895 {
896         be_req_t *r = get_req(irn, pos);
897         r->req.type = type;
898 }
899
900 ir_node *be_get_IncSP_pred(ir_node *irn) {
901         assert(be_is_IncSP(irn));
902         return get_irn_n(irn, 0);
903 }
904
905 void be_set_IncSP_pred(ir_node *incsp, ir_node *pred) {
906         assert(be_is_IncSP(incsp));
907         set_irn_n(incsp, 0, pred);
908 }
909
910 ir_node *be_get_IncSP_mem(ir_node *irn) {
911         assert(be_is_IncSP(irn));
912         return get_irn_n(irn, 1);
913 }
914
915 void be_set_IncSP_offset(ir_node *irn, int offset)
916 {
917         be_stack_attr_t *a = get_irn_attr(irn);
918         assert(be_is_IncSP(irn));
919         a->offset = offset;
920 }
921
922 int be_get_IncSP_offset(const ir_node *irn)
923 {
924         be_stack_attr_t *a = get_irn_attr(irn);
925         assert(be_is_IncSP(irn));
926         return a->offset;
927 }
928
929 ir_node *be_spill(const arch_env_t *arch_env, ir_node *irn)
930 {
931         ir_node *bl     = get_nodes_block(irn);
932         ir_graph *irg   = get_irn_irg(bl);
933         ir_node *frame  = get_irg_frame(irg);
934         ir_node *spill;
935
936         const arch_register_class_t *cls       = arch_get_irn_reg_class(arch_env, irn, -1);
937         const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
938
939         spill = be_new_Spill(cls, cls_frame, irg, bl, frame, irn);
940         return spill;
941 }
942
943 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)
944 {
945         ir_node *reload;
946
947         ir_node *bl    = is_Block(insert) ? insert : get_nodes_block(insert);
948         ir_graph *irg  = get_irn_irg(bl);
949         ir_node *frame = get_irg_frame(irg);
950         const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
951
952         assert(be_is_Spill(spill) || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
953
954         reload = be_new_Reload(cls, cls_frame, irg, bl, frame, spill, mode);
955
956         if(is_Block(insert)) {
957                 insert = sched_skip(insert, 0, sched_skip_cf_predicator, (void *) arch_env);
958                 sched_add_after(insert, reload);
959         }
960
961         else
962                 sched_add_before(insert, reload);
963
964         return reload;
965 }
966
967 /*
968   ____              ____
969  |  _ \ ___  __ _  |  _ \ ___  __ _ ___
970  | |_) / _ \/ _` | | |_) / _ \/ _` / __|
971  |  _ <  __/ (_| | |  _ <  __/ (_| \__ \
972  |_| \_\___|\__, | |_| \_\___|\__, |___/
973             |___/                |_|
974
975 */
976
977
978 static void *put_out_reg_req(arch_register_req_t *req, const ir_node *irn, int out_pos)
979 {
980         const be_node_attr_t *a = get_irn_attr(irn);
981
982         if(out_pos < a->max_reg_data) {
983                 memcpy(req, &a->reg_data[out_pos].req, sizeof(req[0]));
984
985                 if(be_is_Copy(irn)) {
986                         req->type |= arch_register_req_type_should_be_same;
987                         req->other_same = be_get_Copy_op(irn);
988                 }
989         }
990         else {
991                 req->type = arch_register_req_type_none;
992                 req->cls  = NULL;
993         }
994
995         return req;
996 }
997
998 static void *put_in_reg_req(arch_register_req_t *req, const ir_node *irn, int pos)
999 {
1000         const be_node_attr_t *a = get_irn_attr(irn);
1001
1002         if(pos < get_irn_arity(irn) && pos < a->max_reg_data)
1003                 memcpy(req, &a->reg_data[pos].in_req, sizeof(req[0]));
1004         else {
1005                 req->type = arch_register_req_type_none;
1006                 req->cls  = NULL;
1007         }
1008
1009         return req;
1010 }
1011
1012 static const arch_register_req_t *
1013 be_node_get_irn_reg_req(const void *self, arch_register_req_t *req, const ir_node *irn, int pos)
1014 {
1015         int out_pos = pos;
1016
1017         if(pos < 0) {
1018                 if(get_irn_mode(irn) == mode_T)
1019                         return NULL;
1020
1021                 out_pos = redir_proj((const ir_node **) &irn, pos);
1022                 assert(is_be_node(irn));
1023                 return put_out_reg_req(req, irn, out_pos);
1024         }
1025
1026         else {
1027                 return is_be_node(irn) ? put_in_reg_req(req, irn, pos) : NULL;
1028         }
1029
1030         return req;
1031 }
1032
1033 const arch_register_t *
1034 be_node_get_irn_reg(const void *_self, const ir_node *irn)
1035 {
1036         be_reg_data_t *r = retrieve_reg_data(irn);
1037         return r ? r->reg : NULL;
1038 }
1039
1040 static arch_irn_class_t be_node_classify(const void *_self, const ir_node *irn)
1041 {
1042         redir_proj((const ir_node **) &irn, -1);
1043
1044         switch(be_get_irn_opcode(irn)) {
1045 #define XXX(a,b) case beo_ ## a: return arch_irn_class_ ## b
1046                 XXX(Spill, spill);
1047                 XXX(Reload, reload);
1048                 XXX(Perm, perm);
1049                 XXX(Copy, copy);
1050                 XXX(Return, branch);
1051                 XXX(StackParam, stackparam);
1052 #undef XXX
1053                 default:
1054                 return arch_irn_class_normal;
1055         }
1056
1057         return 0;
1058 }
1059
1060 static arch_irn_flags_t be_node_get_flags(const void *_self, const ir_node *irn)
1061 {
1062         be_reg_data_t *r = retrieve_reg_data(irn);
1063         return r ? r->req.flags : 0;
1064 }
1065
1066 static entity *be_node_get_frame_entity(const void *self, const ir_node *irn)
1067 {
1068         return be_get_frame_entity(irn);
1069 }
1070
1071 static void be_node_set_frame_entity(const void *self, ir_node *irn, entity *ent)
1072 {
1073         be_frame_attr_t *a;
1074
1075         assert(be_has_frame_entity(irn));
1076
1077         a = get_irn_attr(irn);
1078         a->ent = ent;
1079 }
1080
1081 static void be_node_set_frame_offset(const void *self, ir_node *irn, int offset)
1082 {
1083         if(be_has_frame_entity(irn)) {
1084                 be_frame_attr_t *a = get_irn_attr(irn);
1085                 a->offset = offset;
1086         }
1087 }
1088
1089 static int be_node_get_sp_bias(const void *self, const ir_node *irn)
1090 {
1091         int result = 0;
1092
1093         if(be_is_IncSP(irn)) {
1094                 result = be_get_IncSP_offset(irn);
1095         }
1096
1097         return result;
1098 }
1099
1100 /*
1101   ___ ____  _   _   _   _                 _ _
1102  |_ _|  _ \| \ | | | | | | __ _ _ __   __| | | ___ _ __
1103   | || |_) |  \| | | |_| |/ _` | '_ \ / _` | |/ _ \ '__|
1104   | ||  _ <| |\  | |  _  | (_| | | | | (_| | |  __/ |
1105  |___|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
1106
1107 */
1108
1109 static const arch_irn_ops_if_t be_node_irn_ops_if = {
1110         be_node_get_irn_reg_req,
1111         be_node_set_irn_reg,
1112         be_node_get_irn_reg,
1113         be_node_classify,
1114         be_node_get_flags,
1115         be_node_get_frame_entity,
1116         be_node_set_frame_entity,
1117         be_node_set_frame_offset,
1118         be_node_get_sp_bias,
1119         NULL,    /* get_inverse             */
1120         NULL,    /* get_op_estimated_cost   */
1121         NULL,    /* possible_memory_operand */
1122         NULL,    /* perform_memory_operand  */
1123 };
1124
1125 static const arch_irn_ops_t be_node_irn_ops = {
1126         &be_node_irn_ops_if
1127 };
1128
1129 const void *be_node_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn)
1130 {
1131         redir_proj((const ir_node **) &irn, -1);
1132         return is_be_node(irn) ? &be_node_irn_ops : NULL;
1133 }
1134
1135 const arch_irn_handler_t be_node_irn_handler = {
1136         be_node_get_irn_ops
1137 };
1138
1139 /*
1140   ____  _     _   ___ ____  _   _   _   _                 _ _
1141  |  _ \| |__ (_) |_ _|  _ \| \ | | | | | | __ _ _ __   __| | | ___ _ __
1142  | |_) | '_ \| |  | || |_) |  \| | | |_| |/ _` | '_ \ / _` | |/ _ \ '__|
1143  |  __/| | | | |  | ||  _ <| |\  | |  _  | (_| | | | | (_| | |  __/ |
1144  |_|   |_| |_|_| |___|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
1145
1146 */
1147
1148 typedef struct {
1149         arch_irn_handler_t irn_handler;
1150         arch_irn_ops_t     irn_ops;
1151         const arch_env_t   *arch_env;
1152         pmap               *regs;
1153 } phi_handler_t;
1154
1155 #define get_phi_handler_from_handler(h)  container_of(h, phi_handler_t, irn_handler)
1156 #define get_phi_handler_from_ops(h)      container_of(h, phi_handler_t, irn_ops)
1157
1158 static const void *phi_get_irn_ops(const arch_irn_handler_t *handler, const ir_node *irn)
1159 {
1160         const phi_handler_t *h = get_phi_handler_from_handler(handler);
1161         return is_Phi(irn) && mode_is_datab(get_irn_mode(irn)) ? &h->irn_ops : NULL;
1162 }
1163
1164 /**
1165  * Get register class of a Phi.
1166  *
1167  */
1168 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)
1169 {
1170         int n = get_irn_arity(phi);
1171         ir_node *op;
1172         int i;
1173
1174         if(*visited && pset_find_ptr(*visited, phi))
1175                 return NULL;
1176
1177         for(i = 0; i < n; ++i) {
1178                 op = get_irn_n(phi, i);
1179                 if(!is_Phi(op))
1180                         return arch_get_register_req(h->arch_env, req, op, BE_OUT_POS(0));
1181         }
1182
1183         /*
1184         The operands of that Phi were all Phis themselves.
1185         We have to start a DFS for a non-Phi argument now.
1186         */
1187         if(!*visited)
1188                 *visited = pset_new_ptr(16);
1189
1190         pset_insert_ptr(*visited, phi);
1191
1192         for(i = 0; i < n; ++i) {
1193                 op = get_irn_n(phi, i);
1194                 if(get_Phi_reg_req_recursive(h, req, op, visited))
1195                         return req;
1196         }
1197
1198         return NULL;
1199 }
1200
1201 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)
1202 {
1203         phi_handler_t *phi_handler = get_phi_handler_from_ops(self);
1204         pset *visited              = NULL;
1205
1206         get_Phi_reg_req_recursive(phi_handler, req, irn, &visited);
1207         /* Set the requirements type to normal, since an operand of the Phi could have had constraints. */
1208         req->type = arch_register_req_type_normal;
1209         if(visited)
1210                 del_pset(visited);
1211
1212         return req;
1213 }
1214
1215 static void phi_set_irn_reg(const void *self, ir_node *irn, const arch_register_t *reg)
1216 {
1217         phi_handler_t *h = get_phi_handler_from_ops(self);
1218         pmap_insert(h->regs, irn, (void *) reg);
1219 }
1220
1221 static const arch_register_t *phi_get_irn_reg(const void *self, const ir_node *irn)
1222 {
1223         phi_handler_t *h = get_phi_handler_from_ops(self);
1224         return pmap_get(h->regs, (void *) irn);
1225 }
1226
1227 static arch_irn_class_t phi_classify(const void *_self, const ir_node *irn)
1228 {
1229         return arch_irn_class_normal;
1230 }
1231
1232 static arch_irn_flags_t phi_get_flags(const void *_self, const ir_node *irn)
1233 {
1234         return arch_irn_flags_none;
1235 }
1236
1237 static entity *phi_get_frame_entity(const void *_self, const ir_node *irn)
1238 {
1239         return NULL;
1240 }
1241
1242 static void phi_set_frame_entity(const void *_self, ir_node *irn, entity *ent)
1243 {
1244 }
1245
1246 static void phi_set_frame_offset(const void *_self, ir_node *irn, int bias)
1247 {
1248 }
1249
1250 static int phi_get_sp_bias(const void* self, const ir_node *irn)
1251 {
1252         return 0;
1253 }
1254
1255 static const arch_irn_ops_if_t phi_irn_ops = {
1256         phi_get_irn_reg_req,
1257         phi_set_irn_reg,
1258         phi_get_irn_reg,
1259         phi_classify,
1260         phi_get_flags,
1261         phi_get_frame_entity,
1262         phi_set_frame_entity,
1263         phi_set_frame_offset,
1264         phi_get_sp_bias,
1265         NULL,    /* get_inverse             */
1266         NULL,    /* get_op_estimated_cost   */
1267         NULL,    /* possible_memory_operand */
1268         NULL,    /* perform_memory_operand  */
1269 };
1270
1271 static const arch_irn_handler_t phi_irn_handler = {
1272         phi_get_irn_ops
1273 };
1274
1275 arch_irn_handler_t *be_phi_handler_new(const arch_env_t *arch_env)
1276 {
1277         phi_handler_t *h           = xmalloc(sizeof(h[0]));
1278         h->irn_handler.get_irn_ops = phi_get_irn_ops;
1279         h->irn_ops.impl            = &phi_irn_ops;
1280         h->arch_env                = arch_env;
1281         h->regs                    = pmap_create();
1282         return (arch_irn_handler_t *) h;
1283 }
1284
1285 void be_phi_handler_free(arch_irn_handler_t *handler)
1286 {
1287         phi_handler_t *h = (void *) handler;
1288         pmap_destroy(h->regs);
1289         free(handler);
1290 }
1291
1292 const void *be_phi_get_irn_ops(const arch_irn_handler_t *self, const ir_node *irn)
1293 {
1294         phi_handler_t *phi_handler = get_phi_handler_from_handler(self);
1295         return is_Phi(irn) ? &phi_handler->irn_ops : NULL;
1296 }
1297
1298 void be_phi_handler_reset(arch_irn_handler_t *handler)
1299 {
1300         phi_handler_t *h = get_phi_handler_from_handler(handler);
1301         if(h->regs)
1302                 pmap_destroy(h->regs);
1303         h->regs = pmap_create();
1304 }
1305
1306 /*
1307   _   _           _        ____                        _
1308  | \ | | ___   __| | ___  |  _ \ _   _ _ __ ___  _ __ (_)_ __   __ _
1309  |  \| |/ _ \ / _` |/ _ \ | | | | | | | '_ ` _ \| '_ \| | '_ \ / _` |
1310  | |\  | (_) | (_| |  __/ | |_| | |_| | | | | | | |_) | | | | | (_| |
1311  |_| \_|\___/ \__,_|\___| |____/ \__,_|_| |_| |_| .__/|_|_| |_|\__, |
1312                                                 |_|            |___/
1313 */
1314
1315 /**
1316  * Dumps a register requirement to a file.
1317  */
1318 static void dump_node_req(FILE *f, int idx, be_req_t *req)
1319 {
1320         unsigned i;
1321         int did_something = 0;
1322         char buf[16];
1323         const char *prefix = buf;
1324
1325         snprintf(buf, sizeof(buf), "#%d ", idx);
1326         buf[sizeof(buf) - 1] = '\0';
1327
1328         if(req->flags != arch_irn_flags_none) {
1329                 fprintf(f, "%sflags: ", prefix);
1330                 prefix = "";
1331                 for(i = arch_irn_flags_none; i <= log2_ceil(arch_irn_flags_last); ++i) {
1332                         if(req->flags & (1 << i)) {
1333                                 fprintf(f, "%s%s", prefix, arch_irn_flag_str(1 << i));
1334                                 prefix = "|";
1335                         }
1336                 }
1337                 prefix = ", ";
1338                 did_something = 1;
1339         }
1340
1341         if(req->req.cls != 0) {
1342                 char tmp[256];
1343                 fprintf(f, prefix);
1344                 arch_register_req_format(tmp, sizeof(tmp), &req->req);
1345                 fprintf(f, "%s", tmp);
1346                 did_something = 1;
1347         }
1348
1349         if(did_something)
1350                 fprintf(f, "\n");
1351 }
1352
1353 /**
1354  * Dumps node register requirements to a file.
1355  */
1356 static void dump_node_reqs(FILE *f, ir_node *irn)
1357 {
1358         int i;
1359         be_node_attr_t *a = get_irn_attr(irn);
1360
1361         fprintf(f, "registers: \n");
1362         for(i = 0; i < a->max_reg_data; ++i) {
1363                 be_reg_data_t *rd = &a->reg_data[i];
1364                 if(rd->reg)
1365                         fprintf(f, "#%d: %s\n", i, rd->reg->name);
1366         }
1367
1368         fprintf(f, "in requirements\n");
1369         for(i = 0; i < a->max_reg_data; ++i) {
1370                 dump_node_req(f, i, &a->reg_data[i].in_req);
1371         }
1372
1373         fprintf(f, "\nout requirements\n");
1374         for(i = 0; i < a->max_reg_data; ++i) {
1375                 dump_node_req(f, i, &a->reg_data[i].req);
1376         }
1377 }
1378
1379 /**
1380  * ir_op-Operation: dump a be node to file
1381  */
1382 static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason)
1383 {
1384         be_node_attr_t *at = get_irn_attr(irn);
1385
1386         assert(is_be_node(irn));
1387
1388         switch(reason) {
1389                 case dump_node_opcode_txt:
1390                         fprintf(f, get_op_name(get_irn_op(irn)));
1391                         break;
1392                 case dump_node_mode_txt:
1393                         fprintf(f, get_mode_name(get_irn_mode(irn)));
1394                         break;
1395                 case dump_node_nodeattr_txt:
1396                         break;
1397                 case dump_node_info_txt:
1398                         dump_node_reqs(f, irn);
1399
1400                         if(be_has_frame_entity(irn)) {
1401                                 be_frame_attr_t *a = (be_frame_attr_t *) at;
1402                                 if (a->ent) {
1403                                         int bits = get_type_size_bits(get_entity_type(a->ent));
1404                                         ir_fprintf(f, "frame entity: %+F, offset 0x%x (%d), size 0x%x (%d) bits\n",
1405                                           a->ent, a->offset, a->offset, bits, bits);
1406                                 }
1407
1408                         }
1409
1410                         switch(be_get_irn_opcode(irn)) {
1411                         case beo_IncSP:
1412                                 {
1413                                         be_stack_attr_t *a = (be_stack_attr_t *) at;
1414                                         if (a->offset == BE_STACK_FRAME_SIZE_EXPAND)
1415                                                 fprintf(f, "offset: FRAME_SIZE\n");
1416                                         else if(a->offset == BE_STACK_FRAME_SIZE_SHRINK)
1417                                                 fprintf(f, "offset: -FRAME SIZE\n");
1418                                         else
1419                                                 fprintf(f, "offset: %u\n", a->offset);
1420                                 }
1421                                 break;
1422                         case beo_Call:
1423                                 {
1424                                         be_call_attr_t *a = (be_call_attr_t *) at;
1425
1426                                         if (a->ent)
1427                                                 fprintf(f, "\ncalling: %s\n", get_entity_name(a->ent));
1428                                 }
1429                                 break;
1430                         case beo_MemPerm:
1431                                 {
1432                                         int i;
1433                                         for(i = 0; i < be_get_MemPerm_entity_arity(irn); ++i) {
1434                                                 entity *in, *out;
1435                                                 in = be_get_MemPerm_in_entity(irn, i);
1436                                                 out = be_get_MemPerm_out_entity(irn, i);
1437                                                 if(in) {
1438                                                         fprintf(f, "\nin[%d]: %s\n", i, get_entity_name(in));
1439                                                 }
1440                                                 if(out) {
1441                                                         fprintf(f, "\nout[%d]: %s\n", i, get_entity_name(out));
1442                                                 }
1443                                         }
1444                                 }
1445                                 break;
1446
1447                         default:
1448                                 break;
1449                         }
1450         }
1451
1452         return 0;
1453 }
1454
1455 /**
1456  * ir_op-Operation:
1457  * Copies the backend specific attributes from old node to new node.
1458  */
1459 static void copy_attr(const ir_node *old_node, ir_node *new_node)
1460 {
1461         be_node_attr_t *old_attr = get_irn_attr(old_node);
1462         be_node_attr_t *new_attr = get_irn_attr(new_node);
1463         int i;
1464
1465         assert(is_be_node(old_node));
1466         assert(is_be_node(new_node));
1467
1468         memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
1469         new_attr->reg_data = NULL;
1470
1471         if(new_attr->max_reg_data > 0) {
1472                 new_attr->reg_data = NEW_ARR_D(be_reg_data_t, get_irg_obstack(get_irn_irg(new_node)), new_attr->max_reg_data);
1473                 memcpy(new_attr->reg_data, old_attr->reg_data, new_attr->max_reg_data * sizeof(be_reg_data_t));
1474
1475                 for(i = 0; i < old_attr->max_reg_data; ++i) {
1476                         be_req_t *r;
1477
1478                         r = &new_attr->reg_data[i].req;
1479                         r->req.limited_env = r;
1480
1481                         r = &new_attr->reg_data[i].in_req;
1482                         r->req.limited_env = r;
1483                 }
1484         }
1485 }
1486
1487 static const ir_op_ops be_node_op_ops = {
1488         NULL,
1489         NULL,
1490         NULL,
1491         NULL,
1492         NULL,
1493         copy_attr,
1494         NULL,
1495         NULL,
1496         NULL,
1497         NULL,
1498         NULL,
1499         dump_node,
1500         NULL
1501 };