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