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