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