ecb11e40f575d702d9386fc5cb2e7c473dd4b435
[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.h"
57 #include "benode_t.h"
58 #include "bearch.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 ir_node *be_new_Spill(const arch_register_class_t *cls, const arch_register_class_t *cls_frame,
305         ir_node *bl, ir_node *frame, ir_node *to_spill)
306 {
307         be_frame_attr_t *a;
308         ir_node         *in[2];
309         ir_node         *res;
310         ir_graph        *irg = get_Block_irg(bl);
311
312         in[0]     = frame;
313         in[1]     = to_spill;
314         res       = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
315         a         = init_node_attr(res, 2);
316         a->ent    = NULL;
317         a->offset = 0;
318
319         be_node_set_reg_class_in(res, be_pos_Spill_frame, cls_frame);
320         be_node_set_reg_class_in(res, be_pos_Spill_val, cls);
321
322         /*
323          * For spills and reloads, we return "none" as requirement for frame
324          * pointer, so every input is ok. Some backends need this (e.g. STA).
325          * Matze: we should investigate if this is really needed, this solution
326          *        looks very hacky to me
327          */
328         be_node_set_reg_class_in(res, be_pos_Spill_frame, NULL);
329
330         return res;
331 }
332
333 ir_node *be_new_Reload(const arch_register_class_t *cls,
334                 const arch_register_class_t *cls_frame, ir_node *block,
335                 ir_node *frame, ir_node *mem, ir_mode *mode)
336 {
337         ir_node  *in[2];
338         ir_node  *res;
339         ir_graph *irg = get_Block_irg(block);
340
341         in[0] = frame;
342         in[1] = mem;
343         res   = new_ir_node(NULL, irg, block, op_be_Reload, mode, 2, in);
344
345         init_node_attr(res, 2);
346         be_node_set_reg_class_out(res, 0, cls);
347         be_node_set_reg_class_in(res, be_pos_Reload_frame, cls_frame);
348         arch_irn_set_flags(res, arch_irn_flags_rematerializable);
349
350         /*
351          * For spills and reloads, we return "none" as requirement for frame
352          * pointer, so every input is ok. Some backends need this (e.g. STA).
353          * Matze: we should investigate if this is really needed, this solution
354          *        looks very hacky to me
355          */
356         be_node_set_reg_class_in(res, be_pos_Reload_frame, NULL);
357
358         return res;
359 }
360
361 ir_node *be_get_Reload_mem(const ir_node *irn)
362 {
363         assert(be_is_Reload(irn));
364         return get_irn_n(irn, be_pos_Reload_mem);
365 }
366
367 ir_node *be_get_Reload_frame(const ir_node *irn)
368 {
369         assert(be_is_Reload(irn));
370         return get_irn_n(irn, be_pos_Reload_frame);
371 }
372
373 ir_node *be_get_Spill_val(const ir_node *irn)
374 {
375         assert(be_is_Spill(irn));
376         return get_irn_n(irn, be_pos_Spill_val);
377 }
378
379 ir_node *be_get_Spill_frame(const ir_node *irn)
380 {
381         assert(be_is_Spill(irn));
382         return get_irn_n(irn, be_pos_Spill_frame);
383 }
384
385 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_node *block, int n, ir_node *in[])
386 {
387         int      i;
388         ir_graph *irg = get_Block_irg(block);
389
390         ir_node *irn = new_ir_node(NULL, irg, block, op_be_Perm, mode_T, n, in);
391         init_node_attr(irn, n);
392         for (i = 0; i < n; ++i) {
393                 be_node_set_reg_class_in(irn, i, cls);
394                 be_node_set_reg_class_out(irn, i, cls);
395         }
396
397         return irn;
398 }
399
400 void be_Perm_reduce(ir_node *perm, int new_size, int *map)
401 {
402         int            arity      = get_irn_arity(perm);
403         be_reg_data_t  *old_data  = ALLOCAN(be_reg_data_t, arity);
404         reg_out_info_t *old_infos = ALLOCAN(reg_out_info_t, arity);
405         be_node_attr_t *attr      = get_irn_attr(perm);
406         backend_info_t *info      = be_get_info(perm);
407         ir_node        **new_in;
408
409         int i;
410
411         assert(be_is_Perm(perm));
412         assert(new_size <= arity);
413
414         new_in = alloca(new_size * sizeof(*new_in));
415
416         /* save the old register data */
417         memcpy(old_data, attr->reg_data, arity * sizeof(old_data[0]));
418         memcpy(old_infos, info->out_infos, arity * sizeof(old_infos[0]));
419
420         /* compose the new in array and set the new register data directly in place */
421         for (i = 0; i < new_size; ++i) {
422                 int idx = map[i];
423                 new_in[i]          = get_irn_n(perm, idx);
424                 attr->reg_data[i]  = old_data[idx];
425                 info->out_infos[i] = old_infos[idx];
426         }
427
428         set_irn_in(perm, new_size, new_in);
429 }
430
431 ir_node *be_new_MemPerm(const arch_env_t *arch_env, ir_node *bl, int n, ir_node *in[])
432 {
433         ir_graph                     *irg       = get_Block_irg(bl);
434         ir_node                      *frame     = get_irg_frame(irg);
435         const arch_register_class_t  *cls_frame = arch_get_irn_reg_class_out(frame);
436         const arch_register_t        *sp        = arch_env->sp;
437         ir_node                      *irn;
438         be_memperm_attr_t            *attr;
439         ir_node                     **real_in;
440         int                           i;
441
442         real_in = ALLOCAN(ir_node*, n + 1);
443         real_in[0] = frame;
444         memcpy(&real_in[1], in, n * sizeof(real_in[0]));
445
446         irn =  new_ir_node(NULL, irg, bl, op_be_MemPerm, mode_T, n+1, real_in);
447
448         init_node_attr(irn, n + 1);
449         be_node_set_reg_class_in(irn, 0, sp->reg_class);
450         for (i = 0; i < n; ++i) {
451                 be_node_set_reg_class_in(irn, i + 1, cls_frame);
452                 be_node_set_reg_class_out(irn, i, cls_frame);
453         }
454
455         attr = get_irn_attr(irn);
456         attr->in_entities  = OALLOCNZ(irg->obst, ir_entity*, n);
457         attr->out_entities = OALLOCNZ(irg->obst, ir_entity*, n);
458
459         return irn;
460 }
461
462
463 ir_node *be_new_Copy(const arch_register_class_t *cls, ir_node *bl, ir_node *op)
464 {
465         ir_node *in[1];
466         ir_node *res;
467         arch_register_req_t *req;
468         ir_graph *irg = get_Block_irg(bl);
469
470         in[0] = op;
471         res   = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
472         init_node_attr(res, 1);
473         be_node_set_reg_class_in(res, 0, cls);
474         be_node_set_reg_class_out(res, 0, cls);
475
476         req = get_be_req(res, BE_OUT_POS(0));
477         req->cls = cls;
478         req->type = arch_register_req_type_should_be_same;
479         req->other_same = 1U << 0;
480
481         return res;
482 }
483
484 ir_node *be_get_Copy_op(const ir_node *cpy) {
485         return get_irn_n(cpy, be_pos_Copy_op);
486 }
487
488 void be_set_Copy_op(ir_node *cpy, ir_node *op) {
489         set_irn_n(cpy, be_pos_Copy_op, op);
490 }
491
492 ir_node *be_new_Keep(const arch_register_class_t *cls, ir_node *bl, int n, ir_node *in[])
493 {
494         int i;
495         ir_node *res;
496         ir_graph *irg = get_Block_irg(bl);
497
498         res = new_ir_node(NULL, irg, bl, op_be_Keep, mode_ANY, -1, NULL);
499         init_node_attr(res, -1);
500
501         for(i = 0; i < n; ++i) {
502                 add_irn_n(res, in[i]);
503                 add_register_req(res);
504                 be_node_set_reg_class_in(res, i, cls);
505         }
506         keep_alive(res);
507
508         return res;
509 }
510
511 void be_Keep_add_node(ir_node *keep, const arch_register_class_t *cls, ir_node *node)
512 {
513         int n;
514
515         assert(be_is_Keep(keep));
516         n = add_irn_n(keep, node);
517         add_register_req(keep);
518         be_node_set_reg_class_in(keep, n, cls);
519 }
520
521 /* creates a be_Call */
522 ir_node *be_new_Call(dbg_info *dbg, ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *sp, ir_node *ptr,
523                      int n_outs, int n, ir_node *in[], ir_type *call_tp)
524 {
525         be_call_attr_t *a;
526         int real_n = be_pos_Call_first_arg + n;
527         ir_node *irn;
528         ir_node **real_in;
529
530         NEW_ARR_A(ir_node *, real_in, real_n);
531         real_in[be_pos_Call_mem] = mem;
532         real_in[be_pos_Call_sp]  = sp;
533         real_in[be_pos_Call_ptr] = ptr;
534         memcpy(&real_in[be_pos_Call_first_arg], in, n * sizeof(in[0]));
535
536         irn = new_ir_node(dbg, irg, bl, op_be_Call, mode_T, real_n, real_in);
537         a = init_node_attr(irn, (n_outs > real_n ? n_outs : real_n));
538         a->ent     = NULL;
539         a->call_tp = call_tp;
540         a->pop     = 0;
541         return irn;
542 }
543
544 /* Gets the call entity or NULL if this is no static call. */
545 ir_entity *be_Call_get_entity(const ir_node *call) {
546         const be_call_attr_t *a = get_irn_attr_const(call);
547         assert(be_is_Call(call));
548         return a->ent;
549 }
550
551 /* Sets the call entity. */
552 void be_Call_set_entity(ir_node *call, ir_entity *ent) {
553         be_call_attr_t *a = get_irn_attr(call);
554         assert(be_is_Call(call));
555         a->ent = ent;
556 }
557
558 /* Gets the call type. */
559 ir_type *be_Call_get_type(ir_node *call) {
560         const be_call_attr_t *a = get_irn_attr_const(call);
561         assert(be_is_Call(call));
562         return a->call_tp;
563 }
564
565 /* Sets the call type. */
566 void be_Call_set_type(ir_node *call, ir_type *call_tp) {
567         be_call_attr_t *a = get_irn_attr(call);
568         assert(be_is_Call(call));
569         a->call_tp = call_tp;
570 }
571
572 void be_Call_set_pop(ir_node *call, unsigned pop) {
573         be_call_attr_t *a = get_irn_attr(call);
574         a->pop = pop;
575 }
576
577 unsigned be_Call_get_pop(const ir_node *call) {
578         const be_call_attr_t *a = get_irn_attr_const(call);
579         return a->pop;
580 }
581
582 /* Construct a new be_Return. */
583 ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *block, int n_res,
584                        unsigned pop, int n, ir_node *in[])
585 {
586         be_return_attr_t *a;
587         ir_node *res;
588         int i;
589
590         res = new_ir_node(dbg, irg, block, op_be_Return, mode_X, -1, NULL);
591         init_node_attr(res, -1);
592         for(i = 0; i < n; ++i) {
593                 add_irn_n(res, in[i]);
594                 add_register_req(res);
595         }
596
597         a = get_irn_attr(res);
598         a->num_ret_vals = n_res;
599         a->pop          = pop;
600         a->emit_pop     = 0;
601
602         return res;
603 }
604
605 /* Returns the number of real returns values */
606 int be_Return_get_n_rets(const ir_node *ret) {
607         const be_return_attr_t *a = get_irn_generic_attr_const(ret);
608         return a->num_ret_vals;
609 }
610
611 /* return the number of bytes that should be popped from stack when executing the Return. */
612 unsigned be_Return_get_pop(const ir_node *ret) {
613         const be_return_attr_t *a = get_irn_generic_attr_const(ret);
614         return a->pop;
615 }
616
617 /* return non-zero, if number of popped bytes must be always emitted */
618 int be_Return_get_emit_pop(const ir_node *ret) {
619         const be_return_attr_t *a = get_irn_generic_attr_const(ret);
620         return a->emit_pop;
621 }
622
623 /* return non-zero, if number of popped bytes must be always emitted */
624 void be_Return_set_emit_pop(ir_node *ret, int emit_pop) {
625         be_return_attr_t *a = get_irn_generic_attr(ret);
626         a->emit_pop = emit_pop;
627 }
628
629 int be_Return_append_node(ir_node *ret, ir_node *node) {
630         int pos;
631
632         pos = add_irn_n(ret, node);
633         add_register_req(ret);
634
635         return pos;
636 }
637
638 ir_node *be_new_IncSP(const arch_register_t *sp, ir_node *bl,
639                       ir_node *old_sp, int offset, int align)
640 {
641         be_incsp_attr_t *a;
642         ir_node *irn;
643         ir_node *in[1];
644         ir_graph *irg = get_Block_irg(bl);
645
646         in[0]     = old_sp;
647         irn       = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode,
648                                 sizeof(in) / sizeof(in[0]), in);
649         a         = init_node_attr(irn, 1);
650         a->offset = offset;
651         a->align  = align;
652
653         /* Set output constraint to stack register. */
654         be_node_set_reg_class_in(irn, 0, sp->reg_class);
655         be_set_constr_single_reg_out(irn, 0, sp, arch_register_req_type_produces_sp);
656
657         return irn;
658 }
659
660 ir_node *be_new_AddSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp, ir_node *sz)
661 {
662         be_node_attr_t *a;
663         ir_node *irn;
664         ir_node *in[be_pos_AddSP_last];
665         const arch_register_class_t *cls;
666         ir_graph *irg;
667
668         in[be_pos_AddSP_old_sp] = old_sp;
669         in[be_pos_AddSP_size]   = sz;
670
671         irg = get_Block_irg(bl);
672         irn = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T, be_pos_AddSP_last, in);
673         a   = init_node_attr(irn, be_pos_AddSP_last);
674
675         /* Set output constraint to stack register. */
676         be_set_constr_single_reg_in(irn, be_pos_AddSP_old_sp, sp, 0);
677         be_node_set_reg_class_in(irn, be_pos_AddSP_size, arch_register_get_class(sp));
678         be_set_constr_single_reg_out(irn, pn_be_AddSP_sp, sp, arch_register_req_type_produces_sp);
679
680         cls = arch_register_get_class(sp);
681
682         return irn;
683 }
684
685 ir_node *be_new_SubSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp, ir_node *sz)
686 {
687         be_node_attr_t *a;
688         ir_node *irn;
689         ir_node *in[be_pos_SubSP_last];
690         ir_graph *irg;
691
692         in[be_pos_SubSP_old_sp] = old_sp;
693         in[be_pos_SubSP_size]   = sz;
694
695         irg = get_Block_irg(bl);
696         irn = new_ir_node(NULL, irg, bl, op_be_SubSP, mode_T, be_pos_SubSP_last, in);
697         a   = init_node_attr(irn, be_pos_SubSP_last);
698
699         /* Set output constraint to stack register. */
700         be_set_constr_single_reg_in(irn, be_pos_SubSP_old_sp, sp, 0);
701         be_node_set_reg_class_in(irn, be_pos_SubSP_size, arch_register_get_class(sp));
702         be_set_constr_single_reg_out(irn, pn_be_SubSP_sp, sp, arch_register_req_type_produces_sp);
703
704         return irn;
705 }
706
707 ir_node *be_new_RegParams(ir_node *bl, int n_outs)
708 {
709         ir_node *res;
710         int i;
711         ir_graph *irg = get_Block_irg(bl);
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_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         ir_graph *irg = get_Block_irg(bl);
727
728         in[0]  = frame;
729         irn    = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
730         a      = init_node_attr(irn, 1);
731         a->ent = ent;
732         a->offset = 0;
733         be_node_set_reg_class_in(irn, 0, cls_frame);
734         be_node_set_reg_class_out(irn, 0, cls_frame);
735
736         return optimize_node(irn);
737 }
738
739 ir_node *be_get_FrameAddr_frame(const ir_node *node) {
740         assert(be_is_FrameAddr(node));
741         return get_irn_n(node, be_pos_FrameAddr_ptr);
742 }
743
744 ir_entity *be_get_FrameAddr_entity(const ir_node *node)
745 {
746         const be_frame_attr_t *attr = get_irn_generic_attr_const(node);
747         return attr->ent;
748 }
749
750 ir_node *be_new_CopyKeep(const arch_register_class_t *cls, ir_node *bl, ir_node *src, int n, ir_node *in_keep[], ir_mode *mode)
751 {
752         ir_node  *irn;
753         ir_node **in = ALLOCAN(ir_node*, n + 1);
754         ir_graph *irg = get_Block_irg(bl);
755
756         in[0] = src;
757         memcpy(&in[1], in_keep, n * sizeof(in[0]));
758         irn   = new_ir_node(NULL, irg, bl, op_be_CopyKeep, mode, n + 1, in);
759         init_node_attr(irn, n + 1);
760         be_node_set_reg_class_in(irn, 0, cls);
761         be_node_set_reg_class_out(irn, 0, cls);
762
763         return irn;
764 }
765
766 ir_node *be_new_CopyKeep_single(const arch_register_class_t *cls, ir_node *bl, ir_node *src, ir_node *keep, ir_mode *mode)
767 {
768         return be_new_CopyKeep(cls, bl, src, 1, &keep, mode);
769 }
770
771 ir_node *be_get_CopyKeep_op(const ir_node *cpy) {
772         return get_irn_n(cpy, be_pos_CopyKeep_op);
773 }
774
775 void be_set_CopyKeep_op(ir_node *cpy, ir_node *op) {
776         set_irn_n(cpy, be_pos_CopyKeep_op, op);
777 }
778
779 ir_node *be_new_Barrier(ir_node *bl, int n, ir_node *in[])
780 {
781         ir_node *res;
782         int i;
783         ir_graph *irg = get_Block_irg(bl);
784
785         res = new_ir_node(NULL, irg, bl, op_be_Barrier, mode_T, -1, NULL);
786         init_node_attr(res, -1);
787         for(i = 0; i < n; ++i) {
788                 add_irn_n(res, in[i]);
789                 add_register_req(res);
790         }
791
792         return res;
793 }
794
795 ir_node *be_Barrier_append_node(ir_node *barrier, ir_node *node)
796 {
797         ir_node *block = get_nodes_block(barrier);
798         ir_mode *mode = get_irn_mode(node);
799         int n = add_irn_n(barrier, node);
800
801         ir_node *proj = new_r_Proj(block, barrier, mode, n);
802         add_register_req(barrier);
803
804         return proj;
805 }
806
807 /* Construct a new be_Unwind. */
808 ir_node *be_new_Unwind(dbg_info *dbg, ir_node *block,
809                                            ir_node *mem, ir_node *sp)
810 {
811         ir_node *res;
812         ir_node *in[2];
813         ir_graph *irg = get_Block_irg(block);
814
815         in[be_pos_Unwind_mem] = mem;
816         in[be_pos_Unwind_sp]  = sp;
817         res = new_ir_node(dbg, irg, block, op_be_Unwind, mode_X, 2, in);
818         init_node_attr(res, -1);
819
820         return res;
821 }
822
823 int be_has_frame_entity(const ir_node *irn)
824 {
825         switch (get_irn_opcode(irn)) {
826         case beo_Spill:
827         case beo_Reload:
828         case beo_FrameAddr:
829                 return 1;
830         default:
831                 return 0;
832         }
833 }
834
835 ir_entity *be_get_frame_entity(const ir_node *irn)
836 {
837         if (be_has_frame_entity(irn)) {
838                 const be_frame_attr_t *a = get_irn_attr_const(irn);
839                 return a->ent;
840         }
841         return NULL;
842 }
843
844 int be_get_frame_offset(const ir_node *irn)
845 {
846         assert(is_be_node(irn));
847         if (be_has_frame_entity(irn)) {
848                 const be_frame_attr_t *a = get_irn_attr_const(irn);
849                 return a->offset;
850         }
851         return 0;
852 }
853
854 void be_set_MemPerm_in_entity(const ir_node *irn, int n, ir_entity *ent)
855 {
856         const be_memperm_attr_t *attr = get_irn_attr_const(irn);
857
858         assert(be_is_MemPerm(irn));
859         assert(n < be_get_MemPerm_entity_arity(irn));
860
861         attr->in_entities[n] = ent;
862 }
863
864 ir_entity* be_get_MemPerm_in_entity(const ir_node* irn, int n)
865 {
866         const be_memperm_attr_t *attr = get_irn_attr_const(irn);
867
868         assert(be_is_MemPerm(irn));
869         assert(n < be_get_MemPerm_entity_arity(irn));
870
871         return attr->in_entities[n];
872 }
873
874 void be_set_MemPerm_out_entity(const ir_node *irn, int n, ir_entity *ent)
875 {
876         const be_memperm_attr_t *attr = get_irn_attr_const(irn);
877
878         assert(be_is_MemPerm(irn));
879         assert(n < be_get_MemPerm_entity_arity(irn));
880
881         attr->out_entities[n] = ent;
882 }
883
884 ir_entity* be_get_MemPerm_out_entity(const ir_node* irn, int n)
885 {
886         const be_memperm_attr_t *attr = get_irn_attr_const(irn);
887
888         assert(be_is_MemPerm(irn));
889         assert(n < be_get_MemPerm_entity_arity(irn));
890
891         return attr->out_entities[n];
892 }
893
894 int be_get_MemPerm_entity_arity(const ir_node *irn)
895 {
896         return get_irn_arity(irn) - 1;
897 }
898
899 static void set_req_single(struct obstack *obst, arch_register_req_t *req,
900                 const arch_register_t *reg, arch_register_req_type_t additional_types)
901 {
902         const arch_register_class_t *cls = arch_register_get_class(reg);
903         unsigned                    *limited_bitset;
904
905         limited_bitset = rbitset_obstack_alloc(obst, arch_register_class_n_regs(cls));
906         rbitset_set(limited_bitset, arch_register_get_index(reg));
907
908         req->cls = cls;
909         req->type |= arch_register_req_type_limited | additional_types;
910         req->limited = limited_bitset;
911
912 }
913
914 void be_set_constr_single_reg_in(ir_node *node, int pos,
915                 const arch_register_t *reg, arch_register_req_type_t additional_types)
916 {
917         arch_register_req_t *req = get_be_req(node, pos);
918         ir_graph *irg = get_irn_irg(node);
919         struct obstack *obst = get_irg_obstack(irg);
920
921         set_req_single(obst, req, reg, additional_types);
922 }
923
924 void be_set_constr_single_reg_out(ir_node *node, int pos,
925                 const arch_register_t *reg, arch_register_req_type_t additional_types)
926 {
927         arch_register_req_t *req = get_be_req(node, BE_OUT_POS(pos));
928         ir_graph *irg = get_irn_irg(node);
929         struct obstack *obst = get_irg_obstack(irg);
930
931         /* if we have an ignore register, add ignore flag and just assign it */
932         if (reg->type & arch_register_type_ignore) {
933                 additional_types |= arch_register_req_type_ignore;
934         }
935
936         arch_irn_set_register(node, pos, reg);
937         set_req_single(obst, req, reg, additional_types);
938 }
939
940 void be_set_constr_limited(ir_node *node, int pos, const arch_register_req_t *req)
941 {
942         ir_graph *irg = get_irn_irg(node);
943         struct obstack *obst = get_irg_obstack(irg);
944         arch_register_req_t *r = get_be_req(node, pos);
945
946         assert(arch_register_req_is(req, limited));
947         assert(!(req->type & (arch_register_req_type_should_be_same | arch_register_req_type_must_be_different)));
948         memcpy(r, req, sizeof(r[0]));
949         r->limited = rbitset_duplicate_obstack_alloc(obst, req->limited, req->cls->n_regs);
950 }
951
952 void be_node_set_reg_class_in(ir_node *irn, int pos, const arch_register_class_t *cls)
953 {
954         arch_register_req_t *req = get_be_req(irn, pos);
955
956         req->cls = cls;
957
958         if (cls == NULL) {
959                 req->type = arch_register_req_type_none;
960         } else if (req->type == arch_register_req_type_none) {
961                 req->type = arch_register_req_type_normal;
962         }
963 }
964
965 void be_node_set_reg_class_out(ir_node *irn, int pos, const arch_register_class_t *cls)
966 {
967         arch_register_req_t *req = get_be_req(irn, BE_OUT_POS(pos));
968
969         req->cls = cls;
970
971         if (cls == NULL) {
972                 req->type = arch_register_req_type_none;
973         } else if (req->type == arch_register_req_type_none) {
974                 req->type = arch_register_req_type_normal;
975         }
976 }
977
978 void be_node_set_req_type(ir_node *irn, int pos, arch_register_req_type_t type)
979 {
980         arch_register_req_t *req = get_be_req(irn, pos);
981         req->type = type;
982 }
983
984 ir_node *be_get_IncSP_pred(ir_node *irn) {
985         assert(be_is_IncSP(irn));
986         return get_irn_n(irn, 0);
987 }
988
989 void be_set_IncSP_pred(ir_node *incsp, ir_node *pred) {
990         assert(be_is_IncSP(incsp));
991         set_irn_n(incsp, 0, pred);
992 }
993
994 void be_set_IncSP_offset(ir_node *irn, int offset)
995 {
996         be_incsp_attr_t *a = get_irn_attr(irn);
997         assert(be_is_IncSP(irn));
998         a->offset = offset;
999 }
1000
1001 int be_get_IncSP_offset(const ir_node *irn)
1002 {
1003         const be_incsp_attr_t *a = get_irn_attr_const(irn);
1004         assert(be_is_IncSP(irn));
1005         return a->offset;
1006 }
1007
1008 int be_get_IncSP_align(const ir_node *irn)
1009 {
1010         const be_incsp_attr_t *a = get_irn_attr_const(irn);
1011         assert(be_is_IncSP(irn));
1012         return a->align;
1013 }
1014
1015 ir_node *be_spill(ir_node *block, ir_node *irn)
1016 {
1017         ir_graph                    *irg       = get_Block_irg(block);
1018         ir_node                     *frame     = get_irg_frame(irg);
1019         const arch_register_class_t *cls       = arch_get_irn_reg_class_out(irn);
1020         const arch_register_class_t *cls_frame = arch_get_irn_reg_class_out(frame);
1021         ir_node                     *spill;
1022
1023         spill = be_new_Spill(cls, cls_frame, block, frame, irn);
1024         return spill;
1025 }
1026
1027 ir_node *be_reload(const arch_register_class_t *cls, ir_node *insert, ir_mode *mode, ir_node *spill)
1028 {
1029         ir_node  *reload;
1030         ir_node  *bl    = is_Block(insert) ? insert : get_nodes_block(insert);
1031         ir_graph *irg   = get_Block_irg(bl);
1032         ir_node  *frame = get_irg_frame(irg);
1033         const arch_register_class_t *cls_frame = arch_get_irn_reg_class_out(frame);
1034
1035         assert(be_is_Spill(spill) || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
1036
1037         reload = be_new_Reload(cls, cls_frame, bl, frame, spill, mode);
1038
1039         if (is_Block(insert)) {
1040                 insert = sched_skip(insert, 0, sched_skip_cf_predicator, NULL);
1041                 sched_add_after(insert, reload);
1042         } else {
1043                 sched_add_before(insert, reload);
1044         }
1045
1046         return reload;
1047 }
1048
1049 /*
1050   ____              ____
1051  |  _ \ ___  __ _  |  _ \ ___  __ _ ___
1052  | |_) / _ \/ _` | | |_) / _ \/ _` / __|
1053  |  _ <  __/ (_| | |  _ <  __/ (_| \__ \
1054  |_| \_\___|\__, | |_| \_\___|\__, |___/
1055             |___/                |_|
1056
1057 */
1058
1059
1060 static const arch_register_req_t *be_node_get_out_reg_req(
1061                 const ir_node *irn, int pos)
1062 {
1063         const be_node_attr_t *a = get_irn_attr_const(irn);
1064
1065         assert(pos >= 0);
1066         if (pos >= ARR_LEN(a->reg_data)) {
1067                 return arch_no_register_req;
1068         }
1069
1070         return &a->reg_data[pos].req;
1071 }
1072
1073 static const arch_register_req_t *be_node_get_in_reg_req(
1074                 const ir_node *irn, int pos)
1075 {
1076         const be_node_attr_t *a = get_irn_attr_const(irn);
1077
1078         assert(pos >= 0);
1079         if (pos >= get_irn_arity(irn) || pos >= ARR_LEN(a->reg_data))
1080                 return arch_no_register_req;
1081
1082         return &a->reg_data[pos].in_req;
1083 }
1084
1085 static arch_irn_class_t be_node_classify(const ir_node *irn)
1086 {
1087         switch (get_irn_opcode(irn)) {
1088                 case beo_Spill:  return arch_irn_class_spill;
1089                 case beo_Reload: return arch_irn_class_reload;
1090                 case beo_Perm:   return arch_irn_class_perm;
1091                 case beo_Copy:   return arch_irn_class_copy;
1092                 default:         return 0;
1093         }
1094 }
1095
1096 static ir_entity *be_node_get_frame_entity(const ir_node *irn)
1097 {
1098         return be_get_frame_entity(irn);
1099 }
1100
1101 static void be_node_set_frame_entity(ir_node *irn, ir_entity *ent)
1102 {
1103         be_frame_attr_t *a;
1104
1105         assert(be_has_frame_entity(irn));
1106
1107         a = get_irn_attr(irn);
1108         a->ent = ent;
1109 }
1110
1111 static void be_node_set_frame_offset(ir_node *irn, int offset)
1112 {
1113         be_frame_attr_t *a;
1114
1115         if(!be_has_frame_entity(irn))
1116                 return;
1117
1118         a = get_irn_attr(irn);
1119         a->offset = offset;
1120 }
1121
1122 static int be_node_get_sp_bias(const ir_node *irn)
1123 {
1124         if(be_is_IncSP(irn))
1125                 return be_get_IncSP_offset(irn);
1126         if(be_is_Call(irn))
1127                 return -(int)be_Call_get_pop(irn);
1128
1129         return 0;
1130 }
1131
1132 /*
1133   ___ ____  _   _   _   _                 _ _
1134  |_ _|  _ \| \ | | | | | | __ _ _ __   __| | | ___ _ __
1135   | || |_) |  \| | | |_| |/ _` | '_ \ / _` | |/ _ \ '__|
1136   | ||  _ <| |\  | |  _  | (_| | | | | (_| | |  __/ |
1137  |___|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
1138
1139 */
1140
1141 /* for be nodes */
1142 static const arch_irn_ops_t be_node_irn_ops = {
1143         be_node_get_in_reg_req,
1144         be_node_get_out_reg_req,
1145         be_node_classify,
1146         be_node_get_frame_entity,
1147         be_node_set_frame_entity,
1148         be_node_set_frame_offset,
1149         be_node_get_sp_bias,
1150         NULL,    /* get_inverse             */
1151         NULL,    /* get_op_estimated_cost   */
1152         NULL,    /* possible_memory_operand */
1153         NULL,    /* perform_memory_operand  */
1154 };
1155
1156 static const arch_register_req_t *dummy_reg_req(
1157                 const ir_node *node, int pos)
1158 {
1159         (void) node;
1160         (void) pos;
1161         return arch_no_register_req;
1162 }
1163
1164 static arch_irn_class_t dummy_classify(const ir_node *node)
1165 {
1166         (void) node;
1167         return 0;
1168 }
1169
1170 static ir_entity* dummy_get_frame_entity(const ir_node *node)
1171 {
1172         (void) node;
1173         return NULL;
1174 }
1175
1176 static void dummy_set_frame_entity(ir_node *node, ir_entity *entity)
1177 {
1178         (void) node;
1179         (void) entity;
1180         panic("dummy_set_frame_entity() should not be called");
1181 }
1182
1183 static void dummy_set_frame_offset(ir_node *node, int bias)
1184 {
1185         (void) node;
1186         (void) bias;
1187         panic("dummy_set_frame_offset() should not be called");
1188 }
1189
1190 static int dummy_get_sp_bias(const ir_node *node)
1191 {
1192         (void) node;
1193         return 0;
1194 }
1195
1196 /* for "middleend" nodes */
1197 static const arch_irn_ops_t dummy_be_irn_ops = {
1198         dummy_reg_req,
1199         dummy_reg_req,
1200         dummy_classify,
1201         dummy_get_frame_entity,
1202         dummy_set_frame_entity,
1203         dummy_set_frame_offset,
1204         dummy_get_sp_bias,
1205         NULL,      /* get_inverse           */
1206         NULL,      /* get_op_estimated_cost */
1207         NULL,      /* possible_memory_operand */
1208         NULL,      /* perform_memory_operand */
1209 };
1210
1211 /*
1212   ____  _     _   ___ ____  _   _   _   _                 _ _
1213  |  _ \| |__ (_) |_ _|  _ \| \ | | | | | | __ _ _ __   __| | | ___ _ __
1214  | |_) | '_ \| |  | || |_) |  \| | | |_| |/ _` | '_ \ / _` | |/ _ \ '__|
1215  |  __/| | | | |  | ||  _ <| |\  | |  _  | (_| | | | | (_| | |  __/ |
1216  |_|   |_| |_|_| |___|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
1217
1218 */
1219
1220 /**
1221  * Guess correct register class of a phi node by looking at its arguments
1222  */
1223 static const arch_register_req_t *get_Phi_reg_req_recursive(const ir_node *phi,
1224                                                             pset **visited)
1225 {
1226         int n = get_irn_arity(phi);
1227         ir_node *op;
1228         int i;
1229
1230         if(*visited && pset_find_ptr(*visited, phi))
1231                 return NULL;
1232
1233         for(i = 0; i < n; ++i) {
1234                 op = get_irn_n(phi, i);
1235                 /* Matze: don't we unnecessary constraint our phis with this?
1236                  * we only need to take the regclass IMO*/
1237                 if(!is_Phi(op))
1238                         return arch_get_register_req_out(op);
1239         }
1240
1241         /*
1242          * The operands of that Phi were all Phis themselves.
1243          * We have to start a DFS for a non-Phi argument now.
1244          */
1245         if(!*visited)
1246                 *visited = pset_new_ptr(16);
1247
1248         pset_insert_ptr(*visited, phi);
1249
1250         for(i = 0; i < n; ++i) {
1251                 const arch_register_req_t *req;
1252                 op = get_irn_n(phi, i);
1253                 req = get_Phi_reg_req_recursive(op, visited);
1254                 if(req != NULL)
1255                         return req;
1256         }
1257
1258         return NULL;
1259 }
1260
1261 static const arch_register_req_t *phi_get_irn_reg_req(const ir_node *node,
1262                                                       int pos)
1263 {
1264         backend_info_t            *info = be_get_info(node);
1265         const arch_register_req_t *req  = info->out_infos[0].req;
1266         (void) pos;
1267
1268         if (req == NULL) {
1269                 if (!mode_is_datab(get_irn_mode(node))) {
1270                         req = arch_no_register_req;
1271                 } else {
1272                         pset           *visited = NULL;
1273                         ir_graph       *irg     = get_irn_irg(node);
1274                         struct obstack *obst    = get_irg_obstack(irg);
1275
1276                         req = get_Phi_reg_req_recursive(node, &visited);
1277                         assert(req->cls != NULL);
1278
1279                         if (req->type != arch_register_req_type_normal) {
1280                                 arch_register_req_t *nreq = OALLOCZ(obst, arch_register_req_t);
1281                                 nreq->type = arch_register_req_type_normal;
1282                                 nreq->cls  = req->cls;
1283                                 req        = nreq;
1284                         }
1285
1286                         if (visited != NULL)
1287                                 del_pset(visited);
1288                 }
1289                 info->out_infos[0].req = req;
1290         }
1291
1292         return req;
1293 }
1294
1295 void be_set_phi_reg_req(ir_node *node, const arch_register_req_t *req)
1296 {
1297         backend_info_t *info = be_get_info(node);
1298         info->out_infos[0].req = req;
1299
1300         assert(mode_is_datab(get_irn_mode(node)));
1301 }
1302
1303 int be_dump_phi_reg_reqs(ir_node *node, FILE *F, dump_reason_t reason)
1304 {
1305         backend_info_t *info;
1306         int i;
1307         int arity;
1308
1309         switch(reason) {
1310         case dump_node_opcode_txt:
1311                 fputs(get_op_name(get_irn_op(node)), F);
1312                 break;
1313         case dump_node_mode_txt:
1314                 fprintf(F, "%s", get_mode_name(get_irn_mode(node)));
1315                 break;
1316         case dump_node_nodeattr_txt:
1317                 break;
1318         case dump_node_info_txt:
1319                 info = be_get_info(node);
1320
1321                 /* we still have a little problem with the initialisation order. This
1322                    dump function is attached to the Phi ops before we can be sure
1323                    that all backend infos have been constructed... */
1324                 if (info != NULL) {
1325                         const arch_register_req_t *req = info->out_infos[0].req;
1326                         const arch_register_t     *reg = arch_irn_get_register(node, 0);
1327
1328                         arity = get_irn_arity(node);
1329                         for (i = 0; i < arity; ++i) {
1330                                 fprintf(F, "inreq #%d = ", i);
1331                                 arch_dump_register_req(F, req, node);
1332                                 fputs("\n", F);
1333                         }
1334                         fprintf(F, "outreq #0 = ");
1335                         arch_dump_register_req(F, req, node);
1336                         fputs("\n", F);
1337
1338                         fputs("\n", F);
1339
1340                         fprintf(F, "reg #0 = %s\n", reg != NULL ? reg->name : "n/a");
1341                 }
1342
1343                 break;
1344
1345         default:
1346                 break;
1347         }
1348
1349         return 0;
1350 }
1351
1352 static const arch_irn_ops_t phi_irn_ops = {
1353         phi_get_irn_reg_req,
1354         phi_get_irn_reg_req,
1355         dummy_classify,
1356         dummy_get_frame_entity,
1357         dummy_set_frame_entity,
1358         dummy_set_frame_offset,
1359         dummy_get_sp_bias,
1360         NULL,    /* get_inverse             */
1361         NULL,    /* get_op_estimated_cost   */
1362         NULL,    /* possible_memory_operand */
1363         NULL,    /* perform_memory_operand  */
1364 };
1365
1366 /*
1367   _   _           _        ____                        _
1368  | \ | | ___   __| | ___  |  _ \ _   _ _ __ ___  _ __ (_)_ __   __ _
1369  |  \| |/ _ \ / _` |/ _ \ | | | | | | | '_ ` _ \| '_ \| | '_ \ / _` |
1370  | |\  | (_) | (_| |  __/ | |_| | |_| | | | | | | |_) | | | | | (_| |
1371  |_| \_|\___/ \__,_|\___| |____/ \__,_|_| |_| |_| .__/|_|_| |_|\__, |
1372                                                 |_|            |___/
1373 */
1374
1375 /**
1376  * Dumps node register requirements to a file.
1377  */
1378 static void dump_node_reqs(FILE *F, ir_node *node)
1379 {
1380         int i;
1381         be_node_attr_t *a = get_irn_attr(node);
1382         int len = ARR_LEN(a->reg_data);
1383
1384         for (i = 0; i < len; ++i) {
1385                 const arch_register_req_t *req = &a->reg_data[i].in_req;
1386                 if (req->cls == NULL)
1387                         continue;
1388                 fprintf(F, "inreq #%d = ", i);
1389                 arch_dump_register_req(F, req, node);
1390                 fputs("\n", F);
1391         }
1392
1393         for (i = 0; i < len; ++i) {
1394                 const arch_register_req_t *req = &a->reg_data[i].req;
1395                 if (req->cls == NULL)
1396                         continue;
1397                 fprintf(F, "outreq #%d = ", i);
1398                 arch_dump_register_req(F, req, node);
1399                 fputs("\n", F);
1400         }
1401
1402         fputs("\n", F);
1403
1404         for (i = 0; i < len; ++i) {
1405                 const arch_register_t *reg = arch_irn_get_register(node, i);
1406                 fprintf(F, "reg #%d = %s\n", i, reg != NULL ? reg->name : "n/a");
1407         }
1408 }
1409
1410 /**
1411  * ir_op-Operation: dump a be node to file
1412  */
1413 static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason)
1414 {
1415         be_node_attr_t *at = get_irn_attr(irn);
1416
1417         assert(is_be_node(irn));
1418
1419         switch(reason) {
1420                 case dump_node_opcode_txt:
1421                         fputs(get_op_name(get_irn_op(irn)), f);
1422                         break;
1423                 case dump_node_mode_txt:
1424                         if(be_is_Perm(irn) || be_is_Copy(irn) || be_is_CopyKeep(irn)) {
1425                                 fprintf(f, " %s", get_mode_name(get_irn_mode(irn)));
1426                         }
1427                         break;
1428                 case dump_node_nodeattr_txt:
1429                         if(be_is_Call(irn)) {
1430                                 be_call_attr_t *a = (be_call_attr_t *) at;
1431                                 if (a->ent)
1432                                         fprintf(f, " [%s] ", get_entity_name(a->ent));
1433                         }
1434                         if(be_is_IncSP(irn)) {
1435                                 const be_incsp_attr_t *attr = get_irn_generic_attr_const(irn);
1436                                 if(attr->offset == BE_STACK_FRAME_SIZE_EXPAND) {
1437                                         fprintf(f, " [Setup Stackframe] ");
1438                                 } else if(attr->offset == BE_STACK_FRAME_SIZE_SHRINK) {
1439                                         fprintf(f, " [Destroy Stackframe] ");
1440                                 } else {
1441                                         fprintf(f, " [%d] ", attr->offset);
1442                                 }
1443                         }
1444                         break;
1445                 case dump_node_info_txt:
1446                         dump_node_reqs(f, irn);
1447
1448                         if(be_has_frame_entity(irn)) {
1449                                 be_frame_attr_t *a = (be_frame_attr_t *) at;
1450                                 if (a->ent) {
1451                                         unsigned size = get_type_size_bytes(get_entity_type(a->ent));
1452                                         ir_fprintf(f, "frame entity: %+F, offset 0x%x (%d), size 0x%x (%d) bytes\n",
1453                                           a->ent, a->offset, a->offset, size, size);
1454                                 }
1455
1456                         }
1457
1458                         switch (get_irn_opcode(irn)) {
1459                         case beo_IncSP:
1460                                 {
1461                                         be_incsp_attr_t *a = (be_incsp_attr_t *) at;
1462                                         if (a->offset == BE_STACK_FRAME_SIZE_EXPAND)
1463                                                 fprintf(f, "offset: FRAME_SIZE\n");
1464                                         else if(a->offset == BE_STACK_FRAME_SIZE_SHRINK)
1465                                                 fprintf(f, "offset: -FRAME SIZE\n");
1466                                         else
1467                                                 fprintf(f, "offset: %u\n", a->offset);
1468                                 }
1469                                 break;
1470                         case beo_Call:
1471                                 {
1472                                         be_call_attr_t *a = (be_call_attr_t *) at;
1473
1474                                         if (a->ent)
1475                                                 fprintf(f, "\ncalling: %s\n", get_entity_name(a->ent));
1476                                 }
1477                                 break;
1478                         case beo_MemPerm:
1479                                 {
1480                                         int i;
1481                                         for(i = 0; i < be_get_MemPerm_entity_arity(irn); ++i) {
1482                                                 ir_entity *in, *out;
1483                                                 in = be_get_MemPerm_in_entity(irn, i);
1484                                                 out = be_get_MemPerm_out_entity(irn, i);
1485                                                 if(in) {
1486                                                         fprintf(f, "\nin[%d]: %s\n", i, get_entity_name(in));
1487                                                 }
1488                                                 if(out) {
1489                                                         fprintf(f, "\nout[%d]: %s\n", i, get_entity_name(out));
1490                                                 }
1491                                         }
1492                                 }
1493                                 break;
1494
1495                         default:
1496                                 break;
1497                         }
1498         }
1499
1500         return 0;
1501 }
1502
1503 /**
1504  * ir_op-Operation:
1505  * Copies the backend specific attributes from old node to new node.
1506  */
1507 static void copy_attr(const ir_node *old_node, ir_node *new_node)
1508 {
1509         const be_node_attr_t *old_attr = get_irn_attr_const(old_node);
1510         be_node_attr_t *new_attr = get_irn_attr(new_node);
1511         struct obstack *obst = get_irg_obstack(get_irn_irg(new_node));
1512         backend_info_t *old_info = be_get_info(old_node);
1513         backend_info_t *new_info = be_get_info(new_node);
1514         unsigned i, len;
1515
1516         assert(is_be_node(old_node));
1517         assert(is_be_node(new_node));
1518
1519         memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
1520         new_attr->reg_data = NULL;
1521
1522         if(old_attr->reg_data != NULL)
1523                 len = ARR_LEN(old_attr->reg_data);
1524         else
1525                 len = 0;
1526
1527         if(get_irn_op(old_node)->opar == oparity_dynamic
1528                         || be_is_RegParams(old_node)) {
1529                 new_attr->reg_data = NEW_ARR_F(be_reg_data_t, len);
1530                 new_info->out_infos = NEW_ARR_F(reg_out_info_t, len);
1531         } else {
1532                 new_attr->reg_data = NEW_ARR_D(be_reg_data_t, obst, len);
1533                 new_info->out_infos = NEW_ARR_D(reg_out_info_t, obst, len);
1534         }
1535
1536         if(len > 0) {
1537                 memcpy(new_attr->reg_data, old_attr->reg_data, len * sizeof(be_reg_data_t));
1538                 memcpy(new_info->out_infos, old_info->out_infos, len * sizeof(new_info->out_infos[0]));
1539                 for(i = 0; i < len; ++i) {
1540                         const be_reg_data_t *rd = &old_attr->reg_data[i];
1541                         be_reg_data_t *newrd = &new_attr->reg_data[i];
1542                         if (arch_register_req_is(&rd->req, limited)) {
1543                                 const arch_register_req_t *req = &rd->req;
1544                                 arch_register_req_t *new_req = &newrd->req;
1545                                 new_req->limited
1546                                         = rbitset_duplicate_obstack_alloc(obst, req->limited, req->cls->n_regs);
1547                         }
1548                         if(arch_register_req_is(&rd->in_req, limited)) {
1549                                 const arch_register_req_t *req = &rd->in_req;
1550                                 arch_register_req_t *new_req = &newrd->in_req;
1551                                 new_req->limited
1552                                         = rbitset_duplicate_obstack_alloc(obst, req->limited, req->cls->n_regs);
1553                         }
1554                 }
1555         }
1556 }
1557
1558 static const ir_op_ops be_node_op_ops = {
1559         firm_default_hash,
1560         NULL,
1561         NULL,
1562         NULL,
1563         NULL,
1564         NULL,
1565         NULL,
1566         NULL,
1567         NULL,
1568         copy_attr,
1569         NULL,
1570         NULL,
1571         NULL,
1572         NULL,
1573         NULL,
1574         dump_node,
1575         NULL,
1576         &be_node_irn_ops
1577 };
1578
1579 int is_be_node(const ir_node *irn)
1580 {
1581         return get_op_ops(get_irn_op(irn))->be_ops == &be_node_irn_ops;
1582 }
1583
1584 void be_init_op(void)
1585 {
1586         ir_opcode opc;
1587
1588         /* Acquire all needed opcodes. */
1589         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);
1590         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);
1591         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);
1592         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);
1593         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);
1594         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);
1595         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);
1596         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);
1597         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);
1598         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);
1599         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);
1600         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);
1601         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);
1602         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);
1603         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);
1604         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);
1605
1606         op_be_Spill->ops.node_cmp_attr     = FrameAddr_cmp_attr;
1607         op_be_Reload->ops.node_cmp_attr    = FrameAddr_cmp_attr;
1608         op_be_Perm->ops.node_cmp_attr      = node_cmp_attr;
1609         op_be_MemPerm->ops.node_cmp_attr   = node_cmp_attr;
1610         op_be_Copy->ops.node_cmp_attr      = node_cmp_attr;
1611         op_be_Keep->ops.node_cmp_attr      = node_cmp_attr;
1612         op_be_CopyKeep->ops.node_cmp_attr  = node_cmp_attr;
1613         op_be_Call->ops.node_cmp_attr      = Call_cmp_attr;
1614         op_be_Return->ops.node_cmp_attr    = Return_cmp_attr;
1615         op_be_AddSP->ops.node_cmp_attr     = node_cmp_attr;
1616         op_be_SubSP->ops.node_cmp_attr     = node_cmp_attr;
1617         op_be_IncSP->ops.node_cmp_attr     = IncSP_cmp_attr;
1618         op_be_RegParams->ops.node_cmp_attr = node_cmp_attr;
1619         op_be_FrameAddr->ops.node_cmp_attr = FrameAddr_cmp_attr;
1620         op_be_Barrier->ops.node_cmp_attr   = node_cmp_attr;
1621         op_be_Unwind->ops.node_cmp_attr    = node_cmp_attr;
1622
1623         /* attach out dummy_ops to middle end nodes */
1624         for (opc = iro_First; opc <= iro_Last; ++opc) {
1625                 ir_op *op = get_irp_opcode(opc);
1626                 assert(op->ops.be_ops == NULL);
1627                 op->ops.be_ops = &dummy_be_irn_ops;
1628         }
1629
1630         op_Phi->ops.be_ops = &phi_irn_ops;
1631 }