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