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