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