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