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