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