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