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