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