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