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