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