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