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