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