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