remove remaining WITH_ILP uses, fix bug introduced when adapting lpp
[libfirm] / ir / be / benode.c
1 /*
2  * Copyright (C) 1995-2011 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 "bitfiddle.h"
41 #include "raw_bitset.h"
42 #include "error.h"
43 #include "array_t.h"
44
45 #include "irop_t.h"
46 #include "irmode_t.h"
47 #include "irnode_t.h"
48 #include "ircons_t.h"
49 #include "irprintf.h"
50 #include "irgwalk.h"
51 #include "iropt_t.h"
52
53 #include "be_t.h"
54 #include "belive_t.h"
55 #include "besched.h"
56 #include "benode.h"
57 #include "bearch.h"
58
59 #include "beirgmod.h"
60
61 typedef struct be_node_attr_t {
62         except_attr  exc;
63 } be_node_attr_t;
64
65 /** The be_Return nodes attribute type. */
66 typedef struct {
67         be_node_attr_t base;
68         int            num_ret_vals; /**< number of return values */
69         unsigned       pop;          /**< number of bytes that should be popped */
70         int            emit_pop;     /**< if set, emit pop bytes, even if pop = 0 */
71 } be_return_attr_t;
72
73 /** The be_IncSP attribute type. */
74 typedef struct {
75         be_node_attr_t base;
76         int            offset;    /**< The offset by which the stack shall be
77                                        expanded/shrinked. */
78         int            align;     /**< whether stack should be aligned after the
79                                        IncSP */
80 } be_incsp_attr_t;
81
82 /** The be_Frame attribute type. */
83 typedef struct {
84         be_node_attr_t  base;
85         ir_entity      *ent;
86         int             offset;
87 } be_frame_attr_t;
88
89 /** The be_Call attribute type. */
90 typedef struct {
91         be_node_attr_t  base;
92         ir_entity      *ent;        /**< called entity if this is a static call. */
93         unsigned        pop;
94         ir_type        *call_tp;    /**< call type, copied from the original Call */
95 } be_call_attr_t;
96
97 typedef struct {
98         be_node_attr_t base;
99         ir_entity    **in_entities;
100         ir_entity    **out_entities;
101 } be_memperm_attr_t;
102
103 ir_op *op_be_Spill;
104 ir_op *op_be_Reload;
105 ir_op *op_be_Perm;
106 ir_op *op_be_MemPerm;
107 ir_op *op_be_Copy;
108 ir_op *op_be_Keep;
109 ir_op *op_be_CopyKeep;
110 ir_op *op_be_Call;
111 ir_op *op_be_Return;
112 ir_op *op_be_IncSP;
113 ir_op *op_be_AddSP;
114 ir_op *op_be_SubSP;
115 ir_op *op_be_Start;
116 ir_op *op_be_FrameAddr;
117
118 /**
119  * Compare the attributes of two be_FrameAddr nodes.
120  *
121  * @return zero if both nodes have identically attributes
122  */
123 static int FrameAddr_cmp_attr(const ir_node *a, const ir_node *b)
124 {
125         const be_frame_attr_t *a_attr = (const be_frame_attr_t*)get_irn_generic_attr_const(a);
126         const be_frame_attr_t *b_attr = (const be_frame_attr_t*)get_irn_generic_attr_const(b);
127
128         if (a_attr->ent != b_attr->ent || a_attr->offset != b_attr->offset)
129                 return 1;
130
131         return be_nodes_equal(a, b);
132 }
133
134 /**
135  * Compare the attributes of two be_Return nodes.
136  *
137  * @return zero if both nodes have identically attributes
138  */
139 static int Return_cmp_attr(const ir_node *a, const ir_node *b)
140 {
141         const be_return_attr_t *a_attr = (const be_return_attr_t*)get_irn_generic_attr_const(a);
142         const be_return_attr_t *b_attr = (const be_return_attr_t*)get_irn_generic_attr_const(b);
143
144         if (a_attr->num_ret_vals != b_attr->num_ret_vals)
145                 return 1;
146         if (a_attr->pop != b_attr->pop)
147                 return 1;
148         if (a_attr->emit_pop != b_attr->emit_pop)
149                 return 1;
150
151         return be_nodes_equal(a, b);
152 }
153
154 /**
155  * Compare the attributes of two be_IncSP nodes.
156  *
157  * @return zero if both nodes have identically attributes
158  */
159 static int IncSP_cmp_attr(const ir_node *a, const ir_node *b)
160 {
161         const be_incsp_attr_t *a_attr = (const be_incsp_attr_t*)get_irn_generic_attr_const(a);
162         const be_incsp_attr_t *b_attr = (const be_incsp_attr_t*)get_irn_generic_attr_const(b);
163
164         if (a_attr->offset != b_attr->offset)
165                 return 1;
166
167         return be_nodes_equal(a, b);
168 }
169
170 /**
171  * Compare the attributes of two be_Call nodes.
172  *
173  * @return zero if both nodes have identically attributes
174  */
175 static int Call_cmp_attr(const ir_node *a, const ir_node *b)
176 {
177         const be_call_attr_t *a_attr = (const be_call_attr_t*)get_irn_generic_attr_const(a);
178         const be_call_attr_t *b_attr = (const be_call_attr_t*)get_irn_generic_attr_const(b);
179
180         if (a_attr->ent != b_attr->ent ||
181                 a_attr->call_tp != b_attr->call_tp)
182                 return 1;
183
184         return be_nodes_equal(a, b);
185 }
186
187 static arch_register_req_t *allocate_reg_req(const ir_node *node)
188 {
189         ir_graph       *irg  = get_irn_irg(node);
190         struct obstack *obst = be_get_be_obst(irg);
191
192         arch_register_req_t *req = OALLOCZ(obst, arch_register_req_t);
193         return req;
194 }
195
196 void be_set_constr_in(ir_node *node, int pos, const arch_register_req_t *req)
197 {
198         backend_info_t *info = be_get_info(node);
199         assert(pos < get_irn_arity(node));
200         info->in_reqs[pos] = req;
201 }
202
203 void be_set_constr_out(ir_node *node, int pos, const arch_register_req_t *req)
204 {
205         backend_info_t *info = be_get_info(node);
206         info->out_infos[pos].req = req;
207 }
208
209 /**
210  * Initializes the generic attribute of all be nodes and return it.
211  */
212 static void init_node_attr(ir_node *node, int n_inputs, int n_outputs)
213 {
214         ir_graph       *irg  = get_irn_irg(node);
215         struct obstack *obst = be_get_be_obst(irg);
216         backend_info_t *info = be_get_info(node);
217         const arch_register_req_t **in_reqs;
218
219         if (n_inputs >= 0) {
220                 int i;
221                 assert(n_inputs == get_irn_arity(node));
222                 in_reqs = OALLOCN(obst, const arch_register_req_t*, n_inputs);
223                 for (i = 0; i < n_inputs; ++i) {
224                         in_reqs[i] = arch_no_register_req;
225                 }
226         } else {
227                 in_reqs = NEW_ARR_F(const arch_register_req_t*, 0);
228         }
229         info->in_reqs = in_reqs;
230
231         if (n_outputs >= 0) {
232                 int i;
233                 info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_outputs);
234                 memset(info->out_infos, 0, n_outputs * sizeof(info->out_infos[0]));
235                 for (i = 0; i < n_outputs; ++i) {
236                         info->out_infos[i].req = arch_no_register_req;
237                 }
238         } else {
239                 info->out_infos = NEW_ARR_F(reg_out_info_t, 0);
240         }
241 }
242
243 static void add_register_req_out(ir_node *node)
244 {
245         backend_info_t *info = be_get_info(node);
246         reg_out_info_t  out_info;
247         memset(&out_info, 0, sizeof(out_info));
248         out_info.req = arch_no_register_req;
249         ARR_APP1(reg_out_info_t, info->out_infos, out_info);
250 }
251
252 static void add_register_req_in(ir_node *node)
253 {
254         backend_info_t *info = be_get_info(node);
255         ARR_APP1(const arch_register_req_t*, info->in_reqs, arch_no_register_req);
256 }
257
258 ir_node *be_new_Spill(const arch_register_class_t *cls,
259                 const arch_register_class_t *cls_frame, ir_node *bl,
260                 ir_node *frame, ir_node *to_spill)
261 {
262         be_frame_attr_t *a;
263         ir_node         *in[2];
264         ir_node         *res;
265         ir_graph        *irg = get_Block_irg(bl);
266
267         in[0]     = frame;
268         in[1]     = to_spill;
269         res       = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
270         init_node_attr(res, 2, 1);
271         a         = (be_frame_attr_t*) get_irn_generic_attr(res);
272         a->ent    = NULL;
273         a->offset = 0;
274         a->base.exc.pin_state = op_pin_state_pinned;
275
276         be_node_set_reg_class_in(res, n_be_Spill_frame, cls_frame);
277         be_node_set_reg_class_in(res, n_be_Spill_val, cls);
278         /*
279          * For spills and reloads, we return "none" as requirement for frame
280          * pointer, so every input is ok. Some backends need this (STA).
281          * Matze: we should investigate if this is really needed, this solution
282          *        looks very hacky to me
283          */
284         be_set_constr_in(res, n_be_Spill_frame, arch_no_register_req);
285
286         arch_set_out_register_req(res, 0, arch_no_register_req);
287
288         return res;
289 }
290
291 ir_node *be_new_Reload(const arch_register_class_t *cls,
292                 const arch_register_class_t *cls_frame, ir_node *block,
293                 ir_node *frame, ir_node *mem, ir_mode *mode)
294 {
295         ir_node  *in[2];
296         ir_node  *res;
297         ir_graph *irg = get_Block_irg(block);
298         be_frame_attr_t *a;
299
300         in[0] = frame;
301         in[1] = mem;
302         res   = new_ir_node(NULL, irg, block, op_be_Reload, mode, 2, in);
303
304         init_node_attr(res, 2, 1);
305         be_node_set_reg_class_out(res, 0, cls);
306
307         be_node_set_reg_class_in(res, n_be_Reload_frame, cls_frame);
308         arch_irn_set_flags(res, arch_irn_flags_rematerializable);
309
310         a         = (be_frame_attr_t*) get_irn_generic_attr(res);
311         a->ent    = NULL;
312         a->offset = 0;
313         a->base.exc.pin_state = op_pin_state_pinned;
314
315         /*
316          * For spills and reloads, we return "none" as requirement for frame
317          * pointer, so every input is ok. Some backends need this (e.g. STA).
318          * Matze: we should investigate if this is really needed, this solution
319          *        looks very hacky to me
320          */
321         be_set_constr_in(res, n_be_Reload_frame, arch_no_register_req);
322
323         return res;
324 }
325
326 ir_node *be_get_Reload_mem(const ir_node *irn)
327 {
328         assert(be_is_Reload(irn));
329         return get_irn_n(irn, n_be_Reload_mem);
330 }
331
332 ir_node *be_get_Reload_frame(const ir_node *irn)
333 {
334         assert(be_is_Reload(irn));
335         return get_irn_n(irn, n_be_Reload_frame);
336 }
337
338 ir_node *be_get_Spill_val(const ir_node *irn)
339 {
340         assert(be_is_Spill(irn));
341         return get_irn_n(irn, n_be_Spill_val);
342 }
343
344 ir_node *be_get_Spill_frame(const ir_node *irn)
345 {
346         assert(be_is_Spill(irn));
347         return get_irn_n(irn, n_be_Spill_frame);
348 }
349
350 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_node *block,
351                      int n, ir_node *in[])
352 {
353         int            i;
354         ir_graph       *irg = get_Block_irg(block);
355         be_node_attr_t *attr;
356
357         ir_node *irn = new_ir_node(NULL, irg, block, op_be_Perm, mode_T, n, in);
358         init_node_attr(irn, n, n);
359         attr                = (be_node_attr_t*) get_irn_generic_attr(irn);
360         attr->exc.pin_state = op_pin_state_pinned;
361         for (i = 0; i < n; ++i) {
362                 be_node_set_reg_class_in(irn, i, cls);
363                 be_node_set_reg_class_out(irn, i, cls);
364         }
365
366         return irn;
367 }
368
369 void be_Perm_reduce(ir_node *perm, int new_size, int *map)
370 {
371         int             arity      = get_irn_arity(perm);
372         const arch_register_req_t **old_in_reqs
373                 = ALLOCAN(const arch_register_req_t*, arity);
374         reg_out_info_t  *old_infos = ALLOCAN(reg_out_info_t, arity);
375         backend_info_t  *info      = be_get_info(perm);
376         ir_node        **new_in;
377         int              i;
378
379         assert(be_is_Perm(perm));
380         assert(new_size <= arity);
381
382         new_in = ALLOCAN(ir_node*, new_size);
383
384         /* save the old register data */
385         memcpy(old_in_reqs, info->in_reqs, arity * sizeof(old_in_reqs[0]));
386         memcpy(old_infos, info->out_infos, arity * sizeof(old_infos[0]));
387
388         /* compose the new in array and set the new register data directly */
389         for (i = 0; i < new_size; ++i) {
390                 int idx = map[i];
391                 new_in[i]          = get_irn_n(perm, idx);
392                 info->in_reqs[i]   = old_in_reqs[idx];
393                 info->out_infos[i] = old_infos[idx];
394         }
395
396         set_irn_in(perm, new_size, new_in);
397 }
398
399 ir_node *be_new_MemPerm(ir_node *block, int n, ir_node *in[])
400 {
401         ir_graph                     *irg       = get_Block_irg(block);
402         const arch_env_t             *arch_env  = be_get_irg_arch_env(irg);
403         ir_node                      *frame     = get_irg_frame(irg);
404         const arch_register_t        *sp        = arch_env->sp;
405         ir_node                      *irn;
406         be_memperm_attr_t            *attr;
407         ir_node                     **real_in;
408
409         real_in = ALLOCAN(ir_node*, n + 1);
410         real_in[0] = frame;
411         memcpy(&real_in[1], in, n * sizeof(real_in[0]));
412
413         irn = new_ir_node(NULL, irg, block, op_be_MemPerm, mode_T, n+1, real_in);
414
415         init_node_attr(irn, n + 1, n);
416         be_node_set_reg_class_in(irn, 0, sp->reg_class);
417
418         attr               = (be_memperm_attr_t*)get_irn_generic_attr(irn);
419         attr->in_entities  = OALLOCNZ(irg->obst, ir_entity*, n);
420         attr->out_entities = OALLOCNZ(irg->obst, ir_entity*, n);
421
422         return irn;
423 }
424
425 ir_node *be_new_Copy(const arch_register_class_t *cls, ir_node *bl, ir_node *op)
426 {
427         ir_node *in[1];
428         ir_node *res;
429         arch_register_req_t *req;
430         be_node_attr_t *attr;
431         ir_graph *irg = get_Block_irg(bl);
432
433         in[0] = op;
434         res   = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
435         init_node_attr(res, 1, 1);
436         attr = (be_node_attr_t*) get_irn_generic_attr(res);
437         attr->exc.pin_state = op_pin_state_floats;
438         be_node_set_reg_class_in(res, 0, cls);
439         be_node_set_reg_class_out(res, 0, cls);
440
441         req = allocate_reg_req(res);
442         req->cls        = cls;
443         req->type       = arch_register_req_type_should_be_same;
444         req->other_same = 1U << 0;
445         req->width      = 1;
446         be_set_constr_out(res, 0, req);
447
448         return res;
449 }
450
451 ir_node *be_get_Copy_op(const ir_node *cpy)
452 {
453         return get_irn_n(cpy, n_be_Copy_op);
454 }
455
456 void be_set_Copy_op(ir_node *cpy, ir_node *op)
457 {
458         set_irn_n(cpy, n_be_Copy_op, op);
459 }
460
461 ir_node *be_new_Keep(ir_node *block, int n, ir_node *in[])
462 {
463         int i;
464         ir_node *res;
465         ir_graph *irg = get_Block_irg(block);
466         be_node_attr_t *attr;
467
468         res = new_ir_node(NULL, irg, block, op_be_Keep, mode_ANY, -1, NULL);
469         init_node_attr(res, -1, 1);
470         attr = (be_node_attr_t*) get_irn_generic_attr(res);
471         attr->exc.pin_state = op_pin_state_pinned;
472
473         for (i = 0; i < n; ++i) {
474                 add_irn_n(res, in[i]);
475                 add_register_req_in(res);
476         }
477         keep_alive(res);
478
479         return res;
480 }
481
482 void be_Keep_add_node(ir_node *keep, const arch_register_class_t *cls, ir_node *node)
483 {
484         int n;
485
486         assert(be_is_Keep(keep));
487         n = add_irn_n(keep, node);
488         add_register_req_in(keep);
489         be_node_set_reg_class_in(keep, n, cls);
490 }
491
492 ir_node *be_new_Call(dbg_info *dbg, ir_graph *irg, ir_node *bl, ir_node *mem,
493                 ir_node *sp, ir_node *ptr, int n_outs, int n, ir_node *in[],
494                 ir_type *call_tp)
495 {
496         be_call_attr_t *a;
497         int real_n = n_be_Call_first_arg + n;
498         ir_node *irn;
499         ir_node **real_in;
500
501         NEW_ARR_A(ir_node *, real_in, real_n);
502         real_in[n_be_Call_mem] = mem;
503         real_in[n_be_Call_sp]  = sp;
504         real_in[n_be_Call_ptr] = ptr;
505         memcpy(&real_in[n_be_Call_first_arg], in, n * sizeof(in[0]));
506
507         irn = new_ir_node(dbg, irg, bl, op_be_Call, mode_T, real_n, real_in);
508         init_node_attr(irn, real_n, n_outs);
509         a                     = (be_call_attr_t*)get_irn_generic_attr(irn);
510         a->ent                = NULL;
511         a->call_tp            = call_tp;
512         a->pop                = 0;
513         a->base.exc.pin_state = op_pin_state_pinned;
514         return irn;
515 }
516
517 ir_entity *be_Call_get_entity(const ir_node *call)
518 {
519         const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
520         assert(be_is_Call(call));
521         return a->ent;
522 }
523
524 void be_Call_set_entity(ir_node *call, ir_entity *ent)
525 {
526         be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
527         assert(be_is_Call(call));
528         a->ent = ent;
529 }
530
531 ir_type *be_Call_get_type(ir_node *call)
532 {
533         const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
534         assert(be_is_Call(call));
535         return a->call_tp;
536 }
537
538 void be_Call_set_type(ir_node *call, ir_type *call_tp)
539 {
540         be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
541         assert(be_is_Call(call));
542         a->call_tp = call_tp;
543 }
544
545 void be_Call_set_pop(ir_node *call, unsigned pop)
546 {
547         be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
548         a->pop = pop;
549 }
550
551 unsigned be_Call_get_pop(const ir_node *call)
552 {
553         const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
554         return a->pop;
555 }
556
557 ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *block, int n_res,
558                        unsigned pop, int n, ir_node *in[])
559 {
560         be_return_attr_t *a;
561         ir_node *res;
562         int i;
563
564         res = new_ir_node(dbg, irg, block, op_be_Return, mode_X, -1, NULL);
565         init_node_attr(res, -1, 1);
566         for (i = 0; i < n; ++i) {
567                 add_irn_n(res, in[i]);
568                 add_register_req_in(res);
569         }
570         be_set_constr_out(res, 0, arch_no_register_req);
571
572         a = (be_return_attr_t*)get_irn_generic_attr(res);
573         a->num_ret_vals       = n_res;
574         a->pop                = pop;
575         a->emit_pop           = 0;
576         a->base.exc.pin_state = op_pin_state_pinned;
577
578         return res;
579 }
580
581 int be_Return_get_n_rets(const ir_node *ret)
582 {
583         const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
584         return a->num_ret_vals;
585 }
586
587 unsigned be_Return_get_pop(const ir_node *ret)
588 {
589         const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
590         return a->pop;
591 }
592
593 int be_Return_get_emit_pop(const ir_node *ret)
594 {
595         const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
596         return a->emit_pop;
597 }
598
599 void be_Return_set_emit_pop(ir_node *ret, int emit_pop)
600 {
601         be_return_attr_t *a = (be_return_attr_t*)get_irn_generic_attr(ret);
602         a->emit_pop = emit_pop;
603 }
604
605 int be_Return_append_node(ir_node *ret, ir_node *node)
606 {
607         int pos;
608
609         pos = add_irn_n(ret, node);
610         add_register_req_in(ret);
611
612         return pos;
613 }
614
615 ir_node *be_new_IncSP(const arch_register_t *sp, ir_node *bl,
616                       ir_node *old_sp, int offset, int align)
617 {
618         be_incsp_attr_t *a;
619         ir_node *irn;
620         ir_node *in[1];
621         ir_graph *irg = get_Block_irg(bl);
622
623         in[0]     = old_sp;
624         irn       = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode,
625                                 sizeof(in) / sizeof(in[0]), in);
626         init_node_attr(irn, 1, 1);
627         a                     = (be_incsp_attr_t*)get_irn_generic_attr(irn);
628         a->offset             = offset;
629         a->align              = align;
630         a->base.exc.pin_state = op_pin_state_pinned;
631
632         /* Set output constraint to stack register. */
633         be_node_set_reg_class_in(irn, 0, sp->reg_class);
634         be_set_constr_single_reg_out(irn, 0, sp, arch_register_req_type_produces_sp);
635
636         return irn;
637 }
638
639 ir_node *be_new_AddSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp,
640                 ir_node *sz)
641 {
642         ir_node *irn;
643         ir_node *in[n_be_AddSP_last];
644         ir_graph *irg;
645         be_node_attr_t *attr;
646
647         in[n_be_AddSP_old_sp] = old_sp;
648         in[n_be_AddSP_size]   = sz;
649
650         irg = get_Block_irg(bl);
651         irn = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T, n_be_AddSP_last, in);
652         init_node_attr(irn, n_be_AddSP_last, pn_be_AddSP_last);
653         attr = (be_node_attr_t*) get_irn_generic_attr(irn);
654         attr->exc.pin_state = op_pin_state_pinned;
655
656         /* Set output constraint to stack register. */
657         be_set_constr_single_reg_in(irn, n_be_AddSP_old_sp, sp,
658                                     arch_register_req_type_none);
659         be_node_set_reg_class_in(irn, n_be_AddSP_size, arch_register_get_class(sp));
660         be_set_constr_single_reg_out(irn, pn_be_AddSP_sp, sp,
661                                      arch_register_req_type_produces_sp);
662
663         return irn;
664 }
665
666 ir_node *be_new_SubSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp, ir_node *sz)
667 {
668         ir_node *irn;
669         ir_node *in[n_be_SubSP_last];
670         ir_graph *irg;
671         be_node_attr_t *attr;
672
673         in[n_be_SubSP_old_sp] = old_sp;
674         in[n_be_SubSP_size]   = sz;
675
676         irg = get_Block_irg(bl);
677         irn = new_ir_node(NULL, irg, bl, op_be_SubSP, mode_T, n_be_SubSP_last, in);
678         init_node_attr(irn, n_be_SubSP_last, pn_be_SubSP_last);
679         attr = (be_node_attr_t*) get_irn_generic_attr(irn);
680         attr->exc.pin_state = op_pin_state_pinned;
681
682         /* Set output constraint to stack register. */
683         be_set_constr_single_reg_in(irn, n_be_SubSP_old_sp, sp,
684                                     arch_register_req_type_none);
685         be_node_set_reg_class_in(irn, n_be_SubSP_size, arch_register_get_class(sp));
686         be_set_constr_single_reg_out(irn, pn_be_SubSP_sp, sp, arch_register_req_type_produces_sp);
687
688         return irn;
689 }
690
691 ir_node *be_new_Start(dbg_info *dbgi, ir_node *bl, int n_outs)
692 {
693         ir_node *res;
694         int i;
695         ir_graph *irg = get_Block_irg(bl);
696         be_node_attr_t *attr;
697
698         res = new_ir_node(dbgi, irg, bl, op_be_Start, mode_T, 0, NULL);
699         init_node_attr(res, 0, -1);
700         attr = (be_node_attr_t*) get_irn_generic_attr(res);
701         attr->exc.pin_state = op_pin_state_pinned;
702         for (i = 0; i < n_outs; ++i) {
703                 add_register_req_out(res);
704         }
705
706         return res;
707 }
708
709 ir_node *be_new_FrameAddr(const arch_register_class_t *cls_frame, ir_node *bl, ir_node *frame, ir_entity *ent)
710 {
711         be_frame_attr_t *a;
712         ir_node *irn;
713         ir_node *in[1];
714         ir_graph *irg = get_Block_irg(bl);
715
716         in[0]  = frame;
717         irn    = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
718         init_node_attr(irn, 1, 1);
719         a                     = (be_frame_attr_t*)get_irn_generic_attr(irn);
720         a->ent                = ent;
721         a->offset             = 0;
722         a->base.exc.pin_state = op_pin_state_floats;
723         be_node_set_reg_class_in(irn, 0, cls_frame);
724         be_node_set_reg_class_out(irn, 0, cls_frame);
725
726         return optimize_node(irn);
727 }
728
729 ir_node *be_get_FrameAddr_frame(const ir_node *node)
730 {
731         assert(be_is_FrameAddr(node));
732         return get_irn_n(node, n_be_FrameAddr_ptr);
733 }
734
735 ir_entity *be_get_FrameAddr_entity(const ir_node *node)
736 {
737         const be_frame_attr_t *attr = (const be_frame_attr_t*)get_irn_generic_attr_const(node);
738         return attr->ent;
739 }
740
741 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)
742 {
743         ir_node  *irn;
744         ir_node **in = ALLOCAN(ir_node*, n + 1);
745         ir_graph *irg = get_Block_irg(bl);
746         be_node_attr_t *attr;
747
748         in[0] = src;
749         memcpy(&in[1], in_keep, n * sizeof(in[0]));
750         irn   = new_ir_node(NULL, irg, bl, op_be_CopyKeep, mode, n + 1, in);
751         init_node_attr(irn, n + 1, 1);
752         attr = (be_node_attr_t*) get_irn_generic_attr(irn);
753         attr->exc.pin_state = op_pin_state_floats;
754         be_node_set_reg_class_in(irn, 0, cls);
755         be_node_set_reg_class_out(irn, 0, cls);
756
757         return irn;
758 }
759
760 ir_node *be_new_CopyKeep_single(const arch_register_class_t *cls, ir_node *bl, ir_node *src, ir_node *keep, ir_mode *mode)
761 {
762         return be_new_CopyKeep(cls, bl, src, 1, &keep, mode);
763 }
764
765 ir_node *be_get_CopyKeep_op(const ir_node *cpy)
766 {
767         return get_irn_n(cpy, n_be_CopyKeep_op);
768 }
769
770 void be_set_CopyKeep_op(ir_node *cpy, ir_node *op)
771 {
772         set_irn_n(cpy, n_be_CopyKeep_op, op);
773 }
774
775 static bool be_has_frame_entity(const ir_node *irn)
776 {
777         switch (get_irn_opcode(irn)) {
778         case beo_Spill:
779         case beo_Reload:
780         case beo_FrameAddr:
781                 return true;
782         default:
783                 return false;
784         }
785 }
786
787 ir_entity *be_get_frame_entity(const ir_node *irn)
788 {
789         if (be_has_frame_entity(irn)) {
790                 const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
791                 return a->ent;
792         }
793         return NULL;
794 }
795
796 int be_get_frame_offset(const ir_node *irn)
797 {
798         assert(is_be_node(irn));
799         if (be_has_frame_entity(irn)) {
800                 const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
801                 return a->offset;
802         }
803         return 0;
804 }
805
806 void be_set_MemPerm_in_entity(const ir_node *irn, int n, ir_entity *ent)
807 {
808         const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
809
810         assert(be_is_MemPerm(irn));
811         assert(n < be_get_MemPerm_entity_arity(irn));
812
813         attr->in_entities[n] = ent;
814 }
815
816 ir_entity* be_get_MemPerm_in_entity(const ir_node* irn, int n)
817 {
818         const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
819
820         assert(be_is_MemPerm(irn));
821         assert(n < be_get_MemPerm_entity_arity(irn));
822
823         return attr->in_entities[n];
824 }
825
826 void be_set_MemPerm_out_entity(const ir_node *irn, int n, ir_entity *ent)
827 {
828         const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
829
830         assert(be_is_MemPerm(irn));
831         assert(n < be_get_MemPerm_entity_arity(irn));
832
833         attr->out_entities[n] = ent;
834 }
835
836 ir_entity* be_get_MemPerm_out_entity(const ir_node* irn, int n)
837 {
838         const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
839
840         assert(be_is_MemPerm(irn));
841         assert(n < be_get_MemPerm_entity_arity(irn));
842
843         return attr->out_entities[n];
844 }
845
846 int be_get_MemPerm_entity_arity(const ir_node *irn)
847 {
848         return get_irn_arity(irn) - 1;
849 }
850
851 const arch_register_req_t *be_create_reg_req(struct obstack *obst,
852                 const arch_register_t *reg, arch_register_req_type_t additional_types)
853 {
854         arch_register_req_t         *req = OALLOC(obst, arch_register_req_t);
855         const arch_register_class_t *cls = arch_register_get_class(reg);
856         unsigned                    *limited_bitset;
857
858         limited_bitset = rbitset_obstack_alloc(obst, arch_register_class_n_regs(cls));
859         rbitset_set(limited_bitset, arch_register_get_index(reg));
860
861         req->type    = arch_register_req_type_limited | additional_types;
862         req->cls     = cls;
863         req->limited = limited_bitset;
864         req->width   = 1;
865         return req;
866 }
867
868 void be_set_constr_single_reg_in(ir_node *node, int pos,
869                 const arch_register_t *reg, arch_register_req_type_t additional_types)
870 {
871         const arch_register_req_t *req;
872
873         if (additional_types == 0) {
874                 req = reg->single_req;
875         } else {
876                 ir_graph       *irg  = get_irn_irg(node);
877                 struct obstack *obst = be_get_be_obst(irg);
878                 req = be_create_reg_req(obst, reg, additional_types);
879         }
880         be_set_constr_in(node, pos, req);
881 }
882
883 void be_set_constr_single_reg_out(ir_node *node, int pos,
884                 const arch_register_t *reg, arch_register_req_type_t additional_types)
885 {
886         ir_graph                  *irg  = get_irn_irg(node);
887         be_irg_t                  *birg = be_birg_from_irg(irg);
888         const arch_register_req_t *req;
889
890         /* if we have an ignore register, add ignore flag and just assign it */
891         if (!rbitset_is_set(birg->allocatable_regs, reg->global_index)) {
892                 additional_types |= arch_register_req_type_ignore;
893         }
894
895         if (additional_types == 0) {
896                 req = reg->single_req;
897         } else {
898                 ir_graph       *irg  = get_irn_irg(node);
899                 struct obstack *obst = be_get_be_obst(irg);
900                 req = be_create_reg_req(obst, reg, additional_types);
901         }
902
903         arch_irn_set_register(node, pos, reg);
904         be_set_constr_out(node, pos, req);
905 }
906
907 void be_node_set_reg_class_in(ir_node *irn, int pos,
908                               const arch_register_class_t *cls)
909 {
910         be_set_constr_in(irn, pos, cls->class_req);
911 }
912
913 void be_node_set_reg_class_out(ir_node *irn, int pos,
914                                const arch_register_class_t *cls)
915 {
916         be_set_constr_out(irn, pos, cls->class_req);
917 }
918
919 ir_node *be_get_IncSP_pred(ir_node *irn)
920 {
921         assert(be_is_IncSP(irn));
922         return get_irn_n(irn, 0);
923 }
924
925 void be_set_IncSP_pred(ir_node *incsp, ir_node *pred)
926 {
927         assert(be_is_IncSP(incsp));
928         set_irn_n(incsp, 0, pred);
929 }
930
931 void be_set_IncSP_offset(ir_node *irn, int offset)
932 {
933         be_incsp_attr_t *a = (be_incsp_attr_t*)get_irn_generic_attr(irn);
934         assert(be_is_IncSP(irn));
935         a->offset = offset;
936 }
937
938 int be_get_IncSP_offset(const ir_node *irn)
939 {
940         const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
941         assert(be_is_IncSP(irn));
942         return a->offset;
943 }
944
945 int be_get_IncSP_align(const ir_node *irn)
946 {
947         const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
948         assert(be_is_IncSP(irn));
949         return a->align;
950 }
951
952 ir_node *be_spill(ir_node *block, ir_node *irn)
953 {
954         ir_graph                    *irg       = get_Block_irg(block);
955         ir_node                     *frame     = get_irg_frame(irg);
956         const arch_register_class_t *cls       = arch_get_irn_reg_class_out(irn);
957         const arch_register_class_t *cls_frame = arch_get_irn_reg_class_out(frame);
958         ir_node                     *spill;
959
960         spill = be_new_Spill(cls, cls_frame, block, frame, irn);
961         return spill;
962 }
963
964 ir_node *be_reload(const arch_register_class_t *cls, ir_node *insert, ir_mode *mode, ir_node *spill)
965 {
966         ir_node  *reload;
967         ir_node  *bl    = is_Block(insert) ? insert : get_nodes_block(insert);
968         ir_graph *irg   = get_Block_irg(bl);
969         ir_node  *frame = get_irg_frame(irg);
970         const arch_register_class_t *cls_frame = arch_get_irn_reg_class_out(frame);
971
972         assert(be_is_Spill(spill) || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
973
974         reload = be_new_Reload(cls, cls_frame, bl, frame, spill, mode);
975
976         if (is_Block(insert)) {
977                 do {
978                         insert = sched_prev(insert);
979                 } while (is_cfop(insert));
980                 sched_add_after(insert, reload);
981         } else {
982                 sched_add_before(insert, reload);
983         }
984
985         return reload;
986 }
987
988
989 static arch_irn_class_t be_node_classify(const ir_node *irn)
990 {
991         switch (get_irn_opcode(irn)) {
992                 case beo_Spill:  return arch_irn_class_spill;
993                 case beo_Reload: return arch_irn_class_reload;
994                 case beo_Perm:   return arch_irn_class_perm;
995                 case beo_Copy:   return arch_irn_class_copy;
996                 default:         return arch_irn_class_none;
997         }
998 }
999
1000 static ir_entity *be_node_get_frame_entity(const ir_node *irn)
1001 {
1002         return be_get_frame_entity(irn);
1003 }
1004
1005 void be_node_set_frame_entity(ir_node *irn, ir_entity *ent)
1006 {
1007         be_frame_attr_t *a;
1008
1009         assert(be_has_frame_entity(irn));
1010
1011         a = (be_frame_attr_t*)get_irn_generic_attr(irn);
1012         a->ent = ent;
1013 }
1014
1015 static void be_node_set_frame_offset(ir_node *irn, int offset)
1016 {
1017         be_frame_attr_t *a;
1018
1019         if (!be_has_frame_entity(irn))
1020                 return;
1021
1022         a = (be_frame_attr_t*)get_irn_generic_attr(irn);
1023         a->offset = offset;
1024 }
1025
1026 static int be_node_get_sp_bias(const ir_node *irn)
1027 {
1028         if (be_is_IncSP(irn))
1029                 return be_get_IncSP_offset(irn);
1030         if (be_is_Call(irn))
1031                 return -(int)be_Call_get_pop(irn);
1032
1033         return 0;
1034 }
1035
1036
1037
1038 /* for be nodes */
1039 static const arch_irn_ops_t be_node_irn_ops = {
1040         be_node_classify,
1041         be_node_get_frame_entity,
1042         be_node_set_frame_offset,
1043         be_node_get_sp_bias,
1044         NULL,    /* get_inverse             */
1045         NULL,    /* get_op_estimated_cost   */
1046         NULL,    /* possible_memory_operand */
1047         NULL,    /* perform_memory_operand  */
1048 };
1049
1050 static int get_start_reg_index(ir_graph *irg, const arch_register_t *reg)
1051 {
1052         ir_node *start  = get_irg_start(irg);
1053         unsigned n_outs = arch_irn_get_n_outs(start);
1054         int      i;
1055
1056         /* do a naive linear search... */
1057         for (i = 0; i < (int)n_outs; ++i) {
1058                 const arch_register_req_t *out_req
1059                         = arch_get_out_register_req(start, i);
1060                 if (! (out_req->type & arch_register_req_type_limited))
1061                         continue;
1062                 if (out_req->cls != arch_register_get_class(reg))
1063                         continue;
1064                 if (!rbitset_is_set(out_req->limited, reg->index))
1065                         continue;
1066                 return i;
1067         }
1068         panic("Tried querying undefined register '%s' at Start", reg->name);
1069 }
1070
1071 ir_node *be_get_initial_reg_value(ir_graph *irg, const arch_register_t *reg)
1072 {
1073         int      i     = get_start_reg_index(irg, reg);
1074         ir_node *start = get_irg_start(irg);
1075         ir_mode *mode  = arch_register_class_mode(arch_register_get_class(reg));
1076         const ir_edge_t *edge;
1077
1078         foreach_out_edge(start, edge) {
1079                 ir_node *proj = get_edge_src_irn(edge);
1080                 if (!is_Proj(proj)) // maybe End/Anchor
1081                         continue;
1082                 if (get_Proj_proj(proj) == i) {
1083                         return proj;
1084                 }
1085         }
1086         return new_r_Proj(start, mode, i);
1087 }
1088
1089 int be_find_return_reg_input(ir_node *ret, const arch_register_t *reg)
1090 {
1091         int arity = get_irn_arity(ret);
1092         int i;
1093         /* do a naive linear search... */
1094         for (i = 0; i < arity; ++i) {
1095                 const arch_register_req_t *req = arch_get_in_register_req(ret, i);
1096                 if (! (req->type & arch_register_req_type_limited))
1097                         continue;
1098                 if (req->cls != arch_register_get_class(reg))
1099                         continue;
1100                 if (!rbitset_is_set(req->limited, reg->index))
1101                         continue;
1102                 return i;
1103         }
1104         panic("Tried querying undefined register '%s' at Return", reg->name);
1105 }
1106
1107 static arch_irn_class_t dummy_classify(const ir_node *node)
1108 {
1109         (void) node;
1110         return arch_irn_class_none;
1111 }
1112
1113 static ir_entity* dummy_get_frame_entity(const ir_node *node)
1114 {
1115         (void) node;
1116         return NULL;
1117 }
1118
1119 static void dummy_set_frame_offset(ir_node *node, int bias)
1120 {
1121         (void) node;
1122         (void) bias;
1123         panic("dummy_set_frame_offset() should not be called");
1124 }
1125
1126 static int dummy_get_sp_bias(const ir_node *node)
1127 {
1128         (void) node;
1129         return 0;
1130 }
1131
1132 /* for "middleend" nodes */
1133 static const arch_irn_ops_t dummy_be_irn_ops = {
1134         dummy_classify,
1135         dummy_get_frame_entity,
1136         dummy_set_frame_offset,
1137         dummy_get_sp_bias,
1138         NULL,      /* get_inverse           */
1139         NULL,      /* get_op_estimated_cost */
1140         NULL,      /* possible_memory_operand */
1141         NULL,      /* perform_memory_operand */
1142 };
1143
1144
1145
1146 ir_node *be_new_Phi(ir_node *block, int n_ins, ir_node **ins, ir_mode *mode,
1147                     const arch_register_class_t *cls)
1148 {
1149         const arch_register_req_t *req;
1150         ir_graph       *irg  = get_irn_irg(block);
1151         struct obstack *obst = be_get_be_obst(irg);
1152         backend_info_t *info;
1153         int             i;
1154
1155         ir_node *phi = new_r_Phi(block, n_ins, ins, mode);
1156         info = be_get_info(phi);
1157         info->out_infos = NEW_ARR_D(reg_out_info_t, obst, 1);
1158         memset(info->out_infos, 0, 1 * sizeof(info->out_infos[0]));
1159         info->in_reqs = OALLOCN(obst, const arch_register_req_t*, n_ins);
1160
1161         if (cls == NULL) {
1162                 req = arch_no_register_req;
1163         } else {
1164                 req = cls->class_req;
1165         }
1166         info->out_infos[0].req = req;
1167         for (i = 0; i < n_ins; ++i) {
1168                 info->in_reqs[i] = req;
1169         }
1170
1171         return phi;
1172 }
1173
1174 void be_set_phi_reg_req(ir_node *node, const arch_register_req_t *req)
1175 {
1176         int arity = get_irn_arity(node);
1177         int i;
1178
1179         backend_info_t *info = be_get_info(node);
1180         info->out_infos[0].req = req;
1181         for (i = 0; i < arity; ++i) {
1182                 info->in_reqs[i] = req;
1183         }
1184
1185         assert(mode_is_datab(get_irn_mode(node)));
1186 }
1187
1188 void be_dump_phi_reg_reqs(FILE *F, ir_node *node, dump_reason_t reason)
1189 {
1190         switch (reason) {
1191         case dump_node_opcode_txt:
1192                 fputs(get_op_name(get_irn_op(node)), F);
1193                 break;
1194         case dump_node_mode_txt:
1195                 fprintf(F, "%s", get_mode_name(get_irn_mode(node)));
1196                 break;
1197         case dump_node_nodeattr_txt:
1198                 break;
1199         case dump_node_info_txt:
1200         {
1201                 backend_info_t *info = be_get_info(node);
1202                 if (info != NULL && info->out_infos[0].req != NULL) {
1203                         arch_dump_reqs_and_registers(F, node);
1204                 }
1205                 break;
1206         }
1207
1208         default:
1209                 break;
1210         }
1211 }
1212
1213 static const arch_irn_ops_t phi_irn_ops = {
1214         dummy_classify,
1215         dummy_get_frame_entity,
1216         dummy_set_frame_offset,
1217         dummy_get_sp_bias,
1218         NULL,    /* get_inverse             */
1219         NULL,    /* get_op_estimated_cost   */
1220         NULL,    /* possible_memory_operand */
1221         NULL,    /* perform_memory_operand  */
1222 };
1223
1224
1225
1226 /**
1227  * ir_op-Operation: dump a be node to file
1228  */
1229 static void dump_node(FILE *f, ir_node *irn, dump_reason_t reason)
1230 {
1231         assert(is_be_node(irn));
1232
1233         switch (reason) {
1234                 case dump_node_opcode_txt:
1235                         fputs(get_op_name(get_irn_op(irn)), f);
1236                         break;
1237                 case dump_node_mode_txt:
1238                         if (be_is_Copy(irn) || be_is_CopyKeep(irn)) {
1239                                 fprintf(f, "%s", get_mode_name(get_irn_mode(irn)));
1240                         }
1241                         break;
1242                 case dump_node_nodeattr_txt:
1243                         if (be_is_Call(irn)) {
1244                                 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(irn);
1245                                 if (a->ent)
1246                                         fprintf(f, " [%s] ", get_entity_name(a->ent));
1247                         }
1248                         if (be_is_IncSP(irn)) {
1249                                 const be_incsp_attr_t *attr = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
1250                                 fprintf(f, " [%d] ", attr->offset);
1251                         }
1252                         break;
1253                 case dump_node_info_txt:
1254                         arch_dump_reqs_and_registers(f, irn);
1255
1256                         if (be_has_frame_entity(irn)) {
1257                                 const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
1258                                 if (a->ent) {
1259                                         unsigned size = get_type_size_bytes(get_entity_type(a->ent));
1260                                         ir_fprintf(f, "frame entity: %+F, offset 0x%x (%d), size 0x%x (%d) bytes\n",
1261                                           a->ent, a->offset, a->offset, size, size);
1262                                 }
1263
1264                         }
1265
1266                         switch (get_irn_opcode(irn)) {
1267                         case beo_IncSP: {
1268                                 const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
1269                                 fprintf(f, "align: %d\n", a->align);
1270                                 fprintf(f, "offset: %d\n", a->offset);
1271                                 break;
1272                         }
1273                         case beo_Call: {
1274                                 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(irn);
1275
1276                                 if (a->ent)
1277                                         fprintf(f, "\ncalling: %s\n", get_entity_name(a->ent));
1278                                 break;
1279                         }
1280                         case beo_MemPerm: {
1281                                 int i;
1282                                 for (i = 0; i < be_get_MemPerm_entity_arity(irn); ++i) {
1283                                         ir_entity *in, *out;
1284                                         in = be_get_MemPerm_in_entity(irn, i);
1285                                         out = be_get_MemPerm_out_entity(irn, i);
1286                                         if (in) {
1287                                                 fprintf(f, "\nin[%d]: %s\n", i, get_entity_name(in));
1288                                         }
1289                                         if (out) {
1290                                                 fprintf(f, "\nout[%d]: %s\n", i, get_entity_name(out));
1291                                         }
1292                                 }
1293                                 break;
1294                         }
1295
1296                         default:
1297                                 break;
1298                         }
1299         }
1300 }
1301
1302 /**
1303  * ir_op-Operation:
1304  * Copies the backend specific attributes from old node to new node.
1305  */
1306 static void copy_attr(ir_graph *irg, const ir_node *old_node, ir_node *new_node)
1307 {
1308         const void     *old_attr = get_irn_generic_attr_const(old_node);
1309         void           *new_attr = get_irn_generic_attr(new_node);
1310         struct obstack *obst     = be_get_be_obst(irg);
1311         backend_info_t *old_info = be_get_info(old_node);
1312         backend_info_t *new_info = be_get_info(new_node);
1313
1314         assert(is_be_node(old_node));
1315         assert(is_be_node(new_node));
1316
1317         memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
1318
1319         new_info->flags = old_info->flags;
1320         if (old_info->out_infos != NULL) {
1321                 size_t n_outs = ARR_LEN(old_info->out_infos);
1322                 /* need dyanmic out infos? */
1323                 if (be_is_Perm(new_node)) {
1324                         new_info->out_infos = NEW_ARR_F(reg_out_info_t, n_outs);
1325                 } else {
1326                         new_info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_outs);
1327                 }
1328                 memcpy(new_info->out_infos, old_info->out_infos,
1329                            n_outs * sizeof(new_info->out_infos[0]));
1330         } else {
1331                 new_info->out_infos = NULL;
1332         }
1333
1334         /* input infos */
1335         if (old_info->in_reqs != NULL) {
1336                 unsigned n_ins = get_irn_arity(old_node);
1337                 /* need dynamic in infos? */
1338                 if (get_irn_op(old_node)->opar == oparity_dynamic) {
1339                         new_info->in_reqs = NEW_ARR_F(const arch_register_req_t*, n_ins);
1340                 } else {
1341                         new_info->in_reqs = OALLOCN(obst,const arch_register_req_t*, n_ins);
1342                 }
1343                 memcpy(new_info->in_reqs, old_info->in_reqs,
1344                        n_ins * sizeof(new_info->in_reqs[0]));
1345         } else {
1346                 new_info->in_reqs = NULL;
1347         }
1348 }
1349
1350 static const ir_op_ops be_node_op_ops = {
1351         firm_default_hash,
1352         NULL,
1353         NULL,
1354         NULL,
1355         NULL,
1356         NULL,
1357         NULL,
1358         NULL,
1359         NULL,
1360         copy_attr,
1361         NULL,
1362         NULL,
1363         NULL,
1364         NULL,
1365         dump_node,
1366         NULL,
1367         &be_node_irn_ops
1368 };
1369
1370 int is_be_node(const ir_node *irn)
1371 {
1372         return get_op_ops(get_irn_op(irn))->be_ops == &be_node_irn_ops;
1373 }
1374
1375 void be_init_op(void)
1376 {
1377         unsigned opc;
1378
1379         /* Acquire all needed opcodes. */
1380         op_be_Spill     = new_ir_op(beo_Spill,     "be_Spill",     op_pin_state_exc_pinned, irop_flag_none,                          oparity_unary,    0, sizeof(be_frame_attr_t),   &be_node_op_ops);
1381         op_be_Reload    = new_ir_op(beo_Reload,    "be_Reload",    op_pin_state_exc_pinned, irop_flag_none,                          oparity_zero,     0, sizeof(be_frame_attr_t),   &be_node_op_ops);
1382         op_be_Perm      = new_ir_op(beo_Perm,      "be_Perm",      op_pin_state_exc_pinned, irop_flag_none,                          oparity_variable, 0, sizeof(be_node_attr_t),    &be_node_op_ops);
1383         op_be_MemPerm   = new_ir_op(beo_MemPerm,   "be_MemPerm",   op_pin_state_exc_pinned, irop_flag_none,                          oparity_variable, 0, sizeof(be_memperm_attr_t), &be_node_op_ops);
1384         op_be_Copy      = new_ir_op(beo_Copy,      "be_Copy",      op_pin_state_exc_pinned, irop_flag_none,                          oparity_unary,    0, sizeof(be_node_attr_t),    &be_node_op_ops);
1385         op_be_Keep      = new_ir_op(beo_Keep,      "be_Keep",      op_pin_state_exc_pinned, irop_flag_keep,                          oparity_dynamic,  0, sizeof(be_node_attr_t),    &be_node_op_ops);
1386         op_be_CopyKeep  = new_ir_op(beo_CopyKeep,  "be_CopyKeep",  op_pin_state_exc_pinned, irop_flag_keep,                          oparity_variable, 0, sizeof(be_node_attr_t),    &be_node_op_ops);
1387         op_be_Call      = new_ir_op(beo_Call,      "be_Call",      op_pin_state_exc_pinned, irop_flag_fragile|irop_flag_uses_memory, oparity_variable, 0, sizeof(be_call_attr_t),    &be_node_op_ops);
1388         ir_op_set_fragile_indices(op_be_Call, n_be_Call_mem, pn_be_Call_X_regular, pn_be_Call_X_except);
1389         op_be_Return    = new_ir_op(beo_Return,    "be_Return",    op_pin_state_exc_pinned, irop_flag_cfopcode,                      oparity_dynamic,  0, sizeof(be_return_attr_t),  &be_node_op_ops);
1390         op_be_AddSP     = new_ir_op(beo_AddSP,     "be_AddSP",     op_pin_state_exc_pinned, irop_flag_none,                          oparity_unary,    0, sizeof(be_node_attr_t),    &be_node_op_ops);
1391         op_be_SubSP     = new_ir_op(beo_SubSP,     "be_SubSP",     op_pin_state_exc_pinned, irop_flag_none,                          oparity_unary,    0, sizeof(be_node_attr_t),    &be_node_op_ops);
1392         op_be_IncSP     = new_ir_op(beo_IncSP,     "be_IncSP",     op_pin_state_exc_pinned, irop_flag_none,                          oparity_unary,    0, sizeof(be_incsp_attr_t),   &be_node_op_ops);
1393         op_be_Start     = new_ir_op(beo_Start,     "be_Start",     op_pin_state_exc_pinned, irop_flag_none,                          oparity_zero,     0, sizeof(be_node_attr_t),    &be_node_op_ops);
1394         op_be_FrameAddr = new_ir_op(beo_FrameAddr, "be_FrameAddr", op_pin_state_exc_pinned, irop_flag_none,                          oparity_unary,    0, sizeof(be_frame_attr_t),   &be_node_op_ops);
1395
1396         op_be_Spill->ops.node_cmp_attr     = FrameAddr_cmp_attr;
1397         op_be_Reload->ops.node_cmp_attr    = FrameAddr_cmp_attr;
1398         op_be_Perm->ops.node_cmp_attr      = be_nodes_equal;
1399         op_be_MemPerm->ops.node_cmp_attr   = be_nodes_equal;
1400         op_be_Copy->ops.node_cmp_attr      = be_nodes_equal;
1401         op_be_Keep->ops.node_cmp_attr      = be_nodes_equal;
1402         op_be_CopyKeep->ops.node_cmp_attr  = be_nodes_equal;
1403         op_be_Call->ops.node_cmp_attr      = Call_cmp_attr;
1404         op_be_Return->ops.node_cmp_attr    = Return_cmp_attr;
1405         op_be_AddSP->ops.node_cmp_attr     = be_nodes_equal;
1406         op_be_SubSP->ops.node_cmp_attr     = be_nodes_equal;
1407         op_be_IncSP->ops.node_cmp_attr     = IncSP_cmp_attr;
1408         op_be_Start->ops.node_cmp_attr     = be_nodes_equal;
1409         op_be_FrameAddr->ops.node_cmp_attr = FrameAddr_cmp_attr;
1410
1411         /* attach out dummy_ops to middle end nodes */
1412         for (opc = iro_First; opc <= iro_Last; ++opc) {
1413                 ir_op *op = get_irp_opcode(opc);
1414                 assert(op->ops.be_ops == NULL);
1415                 op->ops.be_ops = &dummy_be_irn_ops;
1416         }
1417
1418         op_Phi->ops.be_ops = &phi_irn_ops;
1419 }