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