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