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