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