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