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