irop: refactor node_ops registration code
[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         /*
269          * For spills and reloads, we return "none" as requirement for frame
270          * pointer, so every input is ok. Some backends need this (STA).
271          * Matze: we should investigate if this is really needed, this solution
272          *        looks very hacky to me
273          */
274         be_set_constr_in(res, n_be_Spill_frame, arch_no_register_req);
275
276         arch_set_irn_register_req_out(res, 0, arch_no_register_req);
277
278         return res;
279 }
280
281 ir_node *be_new_Reload(const arch_register_class_t *cls,
282                 const arch_register_class_t *cls_frame, ir_node *block,
283                 ir_node *frame, ir_node *mem, ir_mode *mode)
284 {
285         ir_node  *in[2];
286         ir_node  *res;
287         ir_graph *irg = get_Block_irg(block);
288         be_frame_attr_t *a;
289
290         in[0] = frame;
291         in[1] = mem;
292         res   = new_ir_node(NULL, irg, block, op_be_Reload, mode, 2, in);
293
294         init_node_attr(res, 2, 1);
295         be_node_set_reg_class_out(res, 0, cls);
296
297         be_node_set_reg_class_in(res, n_be_Reload_frame, cls_frame);
298         arch_set_irn_flags(res, arch_irn_flags_rematerializable);
299
300         a         = (be_frame_attr_t*) get_irn_generic_attr(res);
301         a->ent    = NULL;
302         a->offset = 0;
303         a->base.exc.pin_state = op_pin_state_pinned;
304
305         /*
306          * For spills and reloads, we return "none" as requirement for frame
307          * pointer, so every input is ok. Some backends need this (e.g. STA).
308          * Matze: we should investigate if this is really needed, this solution
309          *        looks very hacky to me
310          */
311         be_set_constr_in(res, n_be_Reload_frame, arch_no_register_req);
312
313         return res;
314 }
315
316 ir_node *be_get_Reload_mem(const ir_node *irn)
317 {
318         assert(be_is_Reload(irn));
319         return get_irn_n(irn, n_be_Reload_mem);
320 }
321
322 ir_node *be_get_Reload_frame(const ir_node *irn)
323 {
324         assert(be_is_Reload(irn));
325         return get_irn_n(irn, n_be_Reload_frame);
326 }
327
328 ir_node *be_get_Spill_val(const ir_node *irn)
329 {
330         assert(be_is_Spill(irn));
331         return get_irn_n(irn, n_be_Spill_val);
332 }
333
334 ir_node *be_get_Spill_frame(const ir_node *irn)
335 {
336         assert(be_is_Spill(irn));
337         return get_irn_n(irn, n_be_Spill_frame);
338 }
339
340 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_node *block,
341                      int n, ir_node *in[])
342 {
343         int            i;
344         ir_graph       *irg = get_Block_irg(block);
345         be_node_attr_t *attr;
346
347         ir_node *irn = new_ir_node(NULL, irg, block, op_be_Perm, mode_T, n, in);
348         init_node_attr(irn, n, n);
349         attr                = (be_node_attr_t*) get_irn_generic_attr(irn);
350         attr->exc.pin_state = op_pin_state_pinned;
351         for (i = 0; i < n; ++i) {
352                 const ir_node             *input = in[i];
353                 const arch_register_req_t *req   = arch_get_irn_register_req(input);
354                 if (req->width == 1) {
355                         be_set_constr_in(irn, i, cls->class_req);
356                         be_set_constr_out(irn, i, cls->class_req);
357                 } else {
358                         arch_register_req_t *new_req = allocate_reg_req(irn);
359                         new_req->cls   = cls;
360                         new_req->type  = (req->type & arch_register_req_type_aligned);
361                         new_req->width = req->width;
362                         be_set_constr_in(irn, i, new_req);
363                         be_set_constr_out(irn, i, new_req);
364                 }
365         }
366
367         return irn;
368 }
369
370 void be_Perm_reduce(ir_node *perm, int new_size, int *map)
371 {
372         int             arity      = get_irn_arity(perm);
373         const arch_register_req_t **old_in_reqs
374                 = ALLOCAN(const arch_register_req_t*, arity);
375         reg_out_info_t  *old_infos = ALLOCAN(reg_out_info_t, arity);
376         backend_info_t  *info      = be_get_info(perm);
377         ir_node        **new_in;
378         int              i;
379
380         assert(be_is_Perm(perm));
381         assert(new_size <= arity);
382
383         new_in = ALLOCAN(ir_node*, new_size);
384
385         /* save the old register data */
386         memcpy(old_in_reqs, info->in_reqs, arity * sizeof(old_in_reqs[0]));
387         memcpy(old_infos, info->out_infos, arity * sizeof(old_infos[0]));
388
389         /* compose the new in array and set the new register data directly */
390         for (i = 0; i < new_size; ++i) {
391                 int idx = map[i];
392                 new_in[i]          = get_irn_n(perm, idx);
393                 info->in_reqs[i]   = old_in_reqs[idx];
394                 info->out_infos[i] = old_infos[idx];
395         }
396
397         set_irn_in(perm, new_size, new_in);
398 }
399
400 ir_node *be_new_MemPerm(ir_node *block, int n, ir_node *in[])
401 {
402         ir_graph                     *irg       = get_Block_irg(block);
403         const arch_env_t             *arch_env  = be_get_irg_arch_env(irg);
404         ir_node                      *frame     = get_irg_frame(irg);
405         const arch_register_t        *sp        = arch_env->sp;
406         ir_node                      *irn;
407         be_memperm_attr_t            *attr;
408         ir_node                     **real_in;
409
410         real_in = ALLOCAN(ir_node*, n + 1);
411         real_in[0] = frame;
412         memcpy(&real_in[1], in, n * sizeof(real_in[0]));
413
414         irn = new_ir_node(NULL, irg, block, op_be_MemPerm, mode_T, n+1, real_in);
415
416         init_node_attr(irn, n + 1, n);
417         be_node_set_reg_class_in(irn, 0, sp->reg_class);
418
419         attr               = (be_memperm_attr_t*)get_irn_generic_attr(irn);
420         attr->in_entities  = OALLOCNZ(irg->obst, ir_entity*, n);
421         attr->out_entities = OALLOCNZ(irg->obst, ir_entity*, n);
422
423         return irn;
424 }
425
426 ir_node *be_new_Copy(ir_node *bl, ir_node *op)
427 {
428         ir_node *in[1];
429         ir_node *res;
430         arch_register_req_t *req;
431         be_node_attr_t *attr;
432         ir_graph *irg = get_Block_irg(bl);
433         const arch_register_req_t   *in_req = arch_get_irn_register_req(op);
434         const arch_register_class_t *cls    = in_req->cls;
435
436         in[0] = op;
437         res   = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
438         init_node_attr(res, 1, 1);
439         attr = (be_node_attr_t*) get_irn_generic_attr(res);
440         attr->exc.pin_state = op_pin_state_floats;
441         be_node_set_reg_class_in(res, 0, cls);
442         be_node_set_reg_class_out(res, 0, cls);
443
444         req = allocate_reg_req(res);
445         req->cls        = cls;
446         req->type       = arch_register_req_type_should_be_same
447                 | (in_req->type & arch_register_req_type_aligned);
448         req->other_same = 1U << 0;
449         req->width      = in_req->width;
450         be_set_constr_out(res, 0, req);
451
452         return res;
453 }
454
455 ir_node *be_get_Copy_op(const ir_node *cpy)
456 {
457         return get_irn_n(cpy, n_be_Copy_op);
458 }
459
460 void be_set_Copy_op(ir_node *cpy, ir_node *op)
461 {
462         set_irn_n(cpy, n_be_Copy_op, op);
463 }
464
465 ir_node *be_new_Keep(ir_node *block, int n, ir_node *in[])
466 {
467         int i;
468         ir_node *res;
469         ir_graph *irg = get_Block_irg(block);
470         be_node_attr_t *attr;
471
472         res = new_ir_node(NULL, irg, block, op_be_Keep, mode_ANY, -1, NULL);
473         init_node_attr(res, -1, 1);
474         attr = (be_node_attr_t*) get_irn_generic_attr(res);
475         attr->exc.pin_state = op_pin_state_pinned;
476
477         for (i = 0; i < n; ++i) {
478                 add_irn_n(res, in[i]);
479                 add_register_req_in(res);
480         }
481         keep_alive(res);
482
483         return res;
484 }
485
486 void be_Keep_add_node(ir_node *keep, const arch_register_class_t *cls, ir_node *node)
487 {
488         int n;
489
490         assert(be_is_Keep(keep));
491         n = add_irn_n(keep, node);
492         add_register_req_in(keep);
493         be_node_set_reg_class_in(keep, n, cls);
494 }
495
496 ir_node *be_new_Call(dbg_info *dbg, ir_graph *irg, ir_node *bl, ir_node *mem,
497                 ir_node *sp, ir_node *ptr, int n_outs, int n, ir_node *in[],
498                 ir_type *call_tp)
499 {
500         be_call_attr_t *a;
501         int real_n = n_be_Call_first_arg + n;
502         ir_node *irn;
503         ir_node **real_in;
504
505         NEW_ARR_A(ir_node *, real_in, real_n);
506         real_in[n_be_Call_mem] = mem;
507         real_in[n_be_Call_sp]  = sp;
508         real_in[n_be_Call_ptr] = ptr;
509         memcpy(&real_in[n_be_Call_first_arg], in, n * sizeof(in[0]));
510
511         irn = new_ir_node(dbg, irg, bl, op_be_Call, mode_T, real_n, real_in);
512         init_node_attr(irn, real_n, n_outs);
513         a                     = (be_call_attr_t*)get_irn_generic_attr(irn);
514         a->ent                = NULL;
515         a->call_tp            = call_tp;
516         a->pop                = 0;
517         a->base.exc.pin_state = op_pin_state_pinned;
518         return irn;
519 }
520
521 ir_entity *be_Call_get_entity(const ir_node *call)
522 {
523         const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
524         assert(be_is_Call(call));
525         return a->ent;
526 }
527
528 void be_Call_set_entity(ir_node *call, ir_entity *ent)
529 {
530         be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
531         assert(be_is_Call(call));
532         a->ent = ent;
533 }
534
535 ir_type *be_Call_get_type(ir_node *call)
536 {
537         const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
538         assert(be_is_Call(call));
539         return a->call_tp;
540 }
541
542 void be_Call_set_type(ir_node *call, ir_type *call_tp)
543 {
544         be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
545         assert(be_is_Call(call));
546         a->call_tp = call_tp;
547 }
548
549 void be_Call_set_pop(ir_node *call, unsigned pop)
550 {
551         be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
552         a->pop = pop;
553 }
554
555 unsigned be_Call_get_pop(const ir_node *call)
556 {
557         const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
558         return a->pop;
559 }
560
561 ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *block, int n_res,
562                        unsigned pop, int n, ir_node *in[])
563 {
564         be_return_attr_t *a;
565         ir_node *res;
566
567         res = new_ir_node(dbg, irg, block, op_be_Return, mode_X, n, in);
568         init_node_attr(res, n, 1);
569         be_set_constr_out(res, 0, arch_no_register_req);
570
571         a = (be_return_attr_t*)get_irn_generic_attr(res);
572         a->num_ret_vals       = n_res;
573         a->pop                = pop;
574         a->emit_pop           = 0;
575         a->base.exc.pin_state = op_pin_state_pinned;
576
577         return res;
578 }
579
580 int be_Return_get_n_rets(const ir_node *ret)
581 {
582         const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
583         return a->num_ret_vals;
584 }
585
586 unsigned be_Return_get_pop(const ir_node *ret)
587 {
588         const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
589         return a->pop;
590 }
591
592 int be_Return_get_emit_pop(const ir_node *ret)
593 {
594         const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
595         return a->emit_pop;
596 }
597
598 void be_Return_set_emit_pop(ir_node *ret, int emit_pop)
599 {
600         be_return_attr_t *a = (be_return_attr_t*)get_irn_generic_attr(ret);
601         a->emit_pop = emit_pop;
602 }
603
604 ir_node *be_new_IncSP(const arch_register_t *sp, ir_node *bl,
605                       ir_node *old_sp, int offset, int align)
606 {
607         be_incsp_attr_t *a;
608         ir_node *irn;
609         ir_node *in[1];
610         ir_graph *irg = get_Block_irg(bl);
611
612         in[0]     = old_sp;
613         irn       = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode,
614                                 ARRAY_SIZE(in), in);
615         init_node_attr(irn, 1, 1);
616         a                     = (be_incsp_attr_t*)get_irn_generic_attr(irn);
617         a->offset             = offset;
618         a->align              = align;
619         a->base.exc.pin_state = op_pin_state_pinned;
620
621         /* Set output constraint to stack register. */
622         be_node_set_reg_class_in(irn, 0, sp->reg_class);
623         be_set_constr_single_reg_out(irn, 0, sp, arch_register_req_type_produces_sp);
624
625         return irn;
626 }
627
628 ir_node *be_new_AddSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp,
629                 ir_node *sz)
630 {
631         ir_node *irn;
632         ir_node *in[n_be_AddSP_last];
633         ir_graph *irg;
634         be_node_attr_t *attr;
635
636         in[n_be_AddSP_old_sp] = old_sp;
637         in[n_be_AddSP_size]   = sz;
638
639         irg = get_Block_irg(bl);
640         irn = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T, n_be_AddSP_last, in);
641         init_node_attr(irn, n_be_AddSP_last, pn_be_AddSP_last);
642         attr = (be_node_attr_t*) get_irn_generic_attr(irn);
643         attr->exc.pin_state = op_pin_state_pinned;
644
645         /* Set output constraint to stack register. */
646         be_set_constr_single_reg_in(irn, n_be_AddSP_old_sp, sp,
647                                     arch_register_req_type_none);
648         be_node_set_reg_class_in(irn, n_be_AddSP_size, sp->reg_class);
649         be_set_constr_single_reg_out(irn, pn_be_AddSP_sp, sp,
650                                      arch_register_req_type_produces_sp);
651
652         return irn;
653 }
654
655 ir_node *be_new_SubSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp, ir_node *sz)
656 {
657         ir_node *irn;
658         ir_node *in[n_be_SubSP_last];
659         ir_graph *irg;
660         be_node_attr_t *attr;
661
662         in[n_be_SubSP_old_sp] = old_sp;
663         in[n_be_SubSP_size]   = sz;
664
665         irg = get_Block_irg(bl);
666         irn = new_ir_node(NULL, irg, bl, op_be_SubSP, mode_T, n_be_SubSP_last, in);
667         init_node_attr(irn, n_be_SubSP_last, pn_be_SubSP_last);
668         attr = (be_node_attr_t*) get_irn_generic_attr(irn);
669         attr->exc.pin_state = op_pin_state_pinned;
670
671         /* Set output constraint to stack register. */
672         be_set_constr_single_reg_in(irn, n_be_SubSP_old_sp, sp,
673                                     arch_register_req_type_none);
674         be_node_set_reg_class_in(irn, n_be_SubSP_size, sp->reg_class);
675         be_set_constr_single_reg_out(irn, pn_be_SubSP_sp, sp, arch_register_req_type_produces_sp);
676
677         return irn;
678 }
679
680 ir_node *be_new_Start(dbg_info *dbgi, ir_node *bl, int n_outs)
681 {
682         ir_node *res;
683         ir_graph *irg = get_Block_irg(bl);
684         be_node_attr_t *attr;
685
686         res = new_ir_node(dbgi, irg, bl, op_be_Start, mode_T, 0, NULL);
687         init_node_attr(res, 0, n_outs);
688         attr = (be_node_attr_t*) get_irn_generic_attr(res);
689         attr->exc.pin_state = op_pin_state_pinned;
690
691         return res;
692 }
693
694 ir_node *be_new_FrameAddr(const arch_register_class_t *cls_frame, ir_node *bl, ir_node *frame, ir_entity *ent)
695 {
696         be_frame_attr_t *a;
697         ir_node *irn;
698         ir_node *in[1];
699         ir_graph *irg = get_Block_irg(bl);
700
701         in[0]  = frame;
702         irn    = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
703         init_node_attr(irn, 1, 1);
704         a                     = (be_frame_attr_t*)get_irn_generic_attr(irn);
705         a->ent                = ent;
706         a->offset             = 0;
707         a->base.exc.pin_state = op_pin_state_floats;
708         be_node_set_reg_class_in(irn, 0, cls_frame);
709         be_node_set_reg_class_out(irn, 0, cls_frame);
710
711         return optimize_node(irn);
712 }
713
714 ir_node *be_get_FrameAddr_frame(const ir_node *node)
715 {
716         assert(be_is_FrameAddr(node));
717         return get_irn_n(node, n_be_FrameAddr_ptr);
718 }
719
720 ir_entity *be_get_FrameAddr_entity(const ir_node *node)
721 {
722         const be_frame_attr_t *attr = (const be_frame_attr_t*)get_irn_generic_attr_const(node);
723         return attr->ent;
724 }
725
726 ir_node *be_new_CopyKeep(ir_node *bl, ir_node *src, int n, ir_node *in_keep[])
727 {
728         ir_node  *irn;
729         ir_node **in = ALLOCAN(ir_node*, n + 1);
730         ir_graph *irg = get_Block_irg(bl);
731         const arch_register_req_t   *req  = arch_get_irn_register_req(src);
732         const arch_register_class_t *cls  = req->cls;
733         ir_mode                     *mode = get_irn_mode(src);
734         be_node_attr_t *attr;
735
736         in[0] = src;
737         memcpy(&in[1], in_keep, n * sizeof(in[0]));
738         irn   = new_ir_node(NULL, irg, bl, op_be_CopyKeep, mode, n + 1, in);
739         init_node_attr(irn, n + 1, 1);
740         attr = (be_node_attr_t*) get_irn_generic_attr(irn);
741         attr->exc.pin_state = op_pin_state_floats;
742         be_node_set_reg_class_in(irn, 0, cls);
743         be_node_set_reg_class_out(irn, 0, cls);
744
745         return irn;
746 }
747
748 ir_node *be_new_CopyKeep_single(ir_node *bl, ir_node *src, ir_node *keep)
749 {
750         return be_new_CopyKeep(bl, src, 1, &keep);
751 }
752
753 ir_node *be_get_CopyKeep_op(const ir_node *cpy)
754 {
755         return get_irn_n(cpy, n_be_CopyKeep_op);
756 }
757
758 void be_set_CopyKeep_op(ir_node *cpy, ir_node *op)
759 {
760         set_irn_n(cpy, n_be_CopyKeep_op, op);
761 }
762
763 static bool be_has_frame_entity(const ir_node *irn)
764 {
765         switch (get_irn_opcode(irn)) {
766         case beo_Spill:
767         case beo_Reload:
768         case beo_FrameAddr:
769                 return true;
770         default:
771                 return false;
772         }
773 }
774
775 ir_entity *be_get_frame_entity(const ir_node *irn)
776 {
777         if (be_has_frame_entity(irn)) {
778                 const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
779                 return a->ent;
780         }
781         return NULL;
782 }
783
784 int be_get_frame_offset(const ir_node *irn)
785 {
786         assert(is_be_node(irn));
787         if (be_has_frame_entity(irn)) {
788                 const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
789                 return a->offset;
790         }
791         return 0;
792 }
793
794 void be_set_MemPerm_in_entity(const ir_node *irn, int n, ir_entity *ent)
795 {
796         const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
797
798         assert(be_is_MemPerm(irn));
799         assert(n < be_get_MemPerm_entity_arity(irn));
800
801         attr->in_entities[n] = ent;
802 }
803
804 ir_entity* be_get_MemPerm_in_entity(const ir_node* irn, int n)
805 {
806         const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
807
808         assert(be_is_MemPerm(irn));
809         assert(n < be_get_MemPerm_entity_arity(irn));
810
811         return attr->in_entities[n];
812 }
813
814 void be_set_MemPerm_out_entity(const ir_node *irn, int n, ir_entity *ent)
815 {
816         const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
817
818         assert(be_is_MemPerm(irn));
819         assert(n < be_get_MemPerm_entity_arity(irn));
820
821         attr->out_entities[n] = ent;
822 }
823
824 ir_entity* be_get_MemPerm_out_entity(const ir_node* irn, int n)
825 {
826         const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
827
828         assert(be_is_MemPerm(irn));
829         assert(n < be_get_MemPerm_entity_arity(irn));
830
831         return attr->out_entities[n];
832 }
833
834 int be_get_MemPerm_entity_arity(const ir_node *irn)
835 {
836         return get_irn_arity(irn) - 1;
837 }
838
839 const arch_register_req_t *be_create_reg_req(struct obstack *obst,
840                 const arch_register_t *reg, arch_register_req_type_t additional_types)
841 {
842         arch_register_req_t         *req = OALLOC(obst, arch_register_req_t);
843         const arch_register_class_t *cls = arch_register_get_class(reg);
844         unsigned                    *limited_bitset;
845
846         limited_bitset = rbitset_obstack_alloc(obst, arch_register_class_n_regs(cls));
847         rbitset_set(limited_bitset, arch_register_get_index(reg));
848
849         req->type    = arch_register_req_type_limited | additional_types;
850         req->cls     = cls;
851         req->limited = limited_bitset;
852         req->width   = 1;
853         return req;
854 }
855
856 void be_set_constr_single_reg_in(ir_node *node, int pos,
857                 const arch_register_t *reg, arch_register_req_type_t additional_types)
858 {
859         const arch_register_req_t *req;
860
861         if (additional_types == 0) {
862                 req = reg->single_req;
863         } else {
864                 ir_graph       *irg  = get_irn_irg(node);
865                 struct obstack *obst = be_get_be_obst(irg);
866                 req = be_create_reg_req(obst, reg, additional_types);
867         }
868         be_set_constr_in(node, pos, req);
869 }
870
871 void be_set_constr_single_reg_out(ir_node *node, int pos,
872                 const arch_register_t *reg, arch_register_req_type_t additional_types)
873 {
874         ir_graph                  *irg  = get_irn_irg(node);
875         be_irg_t                  *birg = be_birg_from_irg(irg);
876         const arch_register_req_t *req;
877
878         /* if we have an ignore register, add ignore flag and just assign it */
879         if (!rbitset_is_set(birg->allocatable_regs, reg->global_index)) {
880                 additional_types |= arch_register_req_type_ignore;
881         }
882
883         if (additional_types == 0) {
884                 req = reg->single_req;
885         } else {
886                 struct obstack *obst = be_get_be_obst(irg);
887                 req = be_create_reg_req(obst, reg, additional_types);
888         }
889
890         arch_set_irn_register_out(node, pos, reg);
891         be_set_constr_out(node, pos, req);
892 }
893
894 void be_node_set_reg_class_in(ir_node *irn, int pos,
895                               const arch_register_class_t *cls)
896 {
897         be_set_constr_in(irn, pos, cls->class_req);
898 }
899
900 void be_node_set_reg_class_out(ir_node *irn, int pos,
901                                const arch_register_class_t *cls)
902 {
903         be_set_constr_out(irn, pos, cls->class_req);
904 }
905
906 ir_node *be_get_IncSP_pred(ir_node *irn)
907 {
908         assert(be_is_IncSP(irn));
909         return get_irn_n(irn, 0);
910 }
911
912 void be_set_IncSP_pred(ir_node *incsp, ir_node *pred)
913 {
914         assert(be_is_IncSP(incsp));
915         set_irn_n(incsp, 0, pred);
916 }
917
918 void be_set_IncSP_offset(ir_node *irn, int offset)
919 {
920         be_incsp_attr_t *a = (be_incsp_attr_t*)get_irn_generic_attr(irn);
921         assert(be_is_IncSP(irn));
922         a->offset = offset;
923 }
924
925 int be_get_IncSP_offset(const ir_node *irn)
926 {
927         const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
928         assert(be_is_IncSP(irn));
929         return a->offset;
930 }
931
932 int be_get_IncSP_align(const ir_node *irn)
933 {
934         const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
935         assert(be_is_IncSP(irn));
936         return a->align;
937 }
938
939 static ir_entity *be_node_get_frame_entity(const ir_node *irn)
940 {
941         return be_get_frame_entity(irn);
942 }
943
944 void be_node_set_frame_entity(ir_node *irn, ir_entity *ent)
945 {
946         be_frame_attr_t *a;
947
948         assert(be_has_frame_entity(irn));
949
950         a = (be_frame_attr_t*)get_irn_generic_attr(irn);
951         a->ent = ent;
952 }
953
954 static void be_node_set_frame_offset(ir_node *irn, int offset)
955 {
956         be_frame_attr_t *a;
957
958         if (!be_has_frame_entity(irn))
959                 return;
960
961         a = (be_frame_attr_t*)get_irn_generic_attr(irn);
962         a->offset = offset;
963 }
964
965 static int be_node_get_sp_bias(const ir_node *irn)
966 {
967         if (be_is_IncSP(irn))
968                 return be_get_IncSP_offset(irn);
969         if (be_is_Call(irn))
970                 return -(int)be_Call_get_pop(irn);
971
972         return 0;
973 }
974
975
976
977 /* for be nodes */
978 static const arch_irn_ops_t be_node_irn_ops = {
979         be_node_get_frame_entity,
980         be_node_set_frame_offset,
981         be_node_get_sp_bias,
982         NULL,    /* get_inverse             */
983         NULL,    /* get_op_estimated_cost   */
984         NULL,    /* possible_memory_operand */
985         NULL,    /* perform_memory_operand  */
986 };
987
988 static int get_start_reg_index(ir_graph *irg, const arch_register_t *reg)
989 {
990         ir_node *start  = get_irg_start(irg);
991         unsigned n_outs = arch_get_irn_n_outs(start);
992         int      i;
993
994         /* do a naive linear search... */
995         for (i = 0; i < (int)n_outs; ++i) {
996                 const arch_register_req_t *out_req
997                         = arch_get_irn_register_req_out(start, i);
998                 if (! (out_req->type & arch_register_req_type_limited))
999                         continue;
1000                 if (out_req->cls != arch_register_get_class(reg))
1001                         continue;
1002                 if (!rbitset_is_set(out_req->limited, reg->index))
1003                         continue;
1004                 return i;
1005         }
1006         panic("Tried querying undefined register '%s' at Start", reg->name);
1007 }
1008
1009 ir_node *be_get_initial_reg_value(ir_graph *irg, const arch_register_t *reg)
1010 {
1011         int      i     = get_start_reg_index(irg, reg);
1012         ir_node *start = get_irg_start(irg);
1013         ir_mode *mode  = arch_register_class_mode(arch_register_get_class(reg));
1014         const ir_edge_t *edge;
1015
1016         foreach_out_edge(start, edge) {
1017                 ir_node *proj = get_edge_src_irn(edge);
1018                 if (!is_Proj(proj)) // maybe End/Anchor
1019                         continue;
1020                 if (get_Proj_proj(proj) == i) {
1021                         return proj;
1022                 }
1023         }
1024         return new_r_Proj(start, mode, i);
1025 }
1026
1027 int be_find_return_reg_input(ir_node *ret, const arch_register_t *reg)
1028 {
1029         int arity = get_irn_arity(ret);
1030         int i;
1031         /* do a naive linear search... */
1032         for (i = 0; i < arity; ++i) {
1033                 const arch_register_req_t *req = arch_get_irn_register_req_in(ret, i);
1034                 if (! (req->type & arch_register_req_type_limited))
1035                         continue;
1036                 if (req->cls != arch_register_get_class(reg))
1037                         continue;
1038                 if (!rbitset_is_set(req->limited, reg->index))
1039                         continue;
1040                 return i;
1041         }
1042         panic("Tried querying undefined register '%s' at Return", reg->name);
1043 }
1044
1045 static ir_entity* dummy_get_frame_entity(const ir_node *node)
1046 {
1047         (void) node;
1048         return NULL;
1049 }
1050
1051 static void dummy_set_frame_offset(ir_node *node, int bias)
1052 {
1053         (void) node;
1054         (void) bias;
1055         panic("dummy_set_frame_offset() should not be called");
1056 }
1057
1058 static int dummy_get_sp_bias(const ir_node *node)
1059 {
1060         (void) node;
1061         return 0;
1062 }
1063
1064 /* for "middleend" nodes */
1065 static const arch_irn_ops_t dummy_be_irn_ops = {
1066         dummy_get_frame_entity,
1067         dummy_set_frame_offset,
1068         dummy_get_sp_bias,
1069         NULL,      /* get_inverse           */
1070         NULL,      /* get_op_estimated_cost */
1071         NULL,      /* possible_memory_operand */
1072         NULL,      /* perform_memory_operand */
1073 };
1074
1075
1076
1077 ir_node *be_new_Phi(ir_node *block, int n_ins, ir_node **ins, ir_mode *mode,
1078                     const arch_register_req_t *req)
1079 {
1080         ir_graph       *irg  = get_irn_irg(block);
1081         struct obstack *obst = be_get_be_obst(irg);
1082         backend_info_t *info;
1083         int             i;
1084
1085         ir_node *phi = new_r_Phi(block, n_ins, ins, mode);
1086         info = be_get_info(phi);
1087         info->out_infos = NEW_ARR_D(reg_out_info_t, obst, 1);
1088         memset(info->out_infos, 0, 1 * sizeof(info->out_infos[0]));
1089         info->in_reqs = OALLOCN(obst, const arch_register_req_t*, n_ins);
1090
1091         info->out_infos[0].req = req;
1092         for (i = 0; i < n_ins; ++i) {
1093                 info->in_reqs[i] = req;
1094         }
1095
1096         return phi;
1097 }
1098
1099 void be_set_phi_reg_req(ir_node *node, const arch_register_req_t *req)
1100 {
1101         int arity = get_irn_arity(node);
1102         int i;
1103
1104         backend_info_t *info = be_get_info(node);
1105         info->out_infos[0].req = req;
1106         for (i = 0; i < arity; ++i) {
1107                 info->in_reqs[i] = req;
1108         }
1109
1110         assert(mode_is_datab(get_irn_mode(node)));
1111 }
1112
1113 void be_dump_phi_reg_reqs(FILE *F, const ir_node *node, dump_reason_t reason)
1114 {
1115         switch (reason) {
1116         case dump_node_opcode_txt:
1117                 fputs(get_op_name(get_irn_op(node)), F);
1118                 break;
1119         case dump_node_mode_txt:
1120                 fprintf(F, "%s", get_mode_name(get_irn_mode(node)));
1121                 break;
1122         case dump_node_nodeattr_txt:
1123                 break;
1124         case dump_node_info_txt:
1125         {
1126                 backend_info_t *info = be_get_info(node);
1127                 if (info != NULL && info->out_infos[0].req != NULL) {
1128                         arch_dump_reqs_and_registers(F, node);
1129                 }
1130                 break;
1131         }
1132
1133         default:
1134                 break;
1135         }
1136 }
1137
1138 static const arch_irn_ops_t phi_irn_ops = {
1139         dummy_get_frame_entity,
1140         dummy_set_frame_offset,
1141         dummy_get_sp_bias,
1142         NULL,    /* get_inverse             */
1143         NULL,    /* get_op_estimated_cost   */
1144         NULL,    /* possible_memory_operand */
1145         NULL,    /* perform_memory_operand  */
1146 };
1147
1148
1149
1150 /**
1151  * ir_op-Operation: dump a be node to file
1152  */
1153 static void dump_node(FILE *f, const ir_node *irn, dump_reason_t reason)
1154 {
1155         assert(is_be_node(irn));
1156
1157         switch (reason) {
1158                 case dump_node_opcode_txt:
1159                         fputs(get_op_name(get_irn_op(irn)), f);
1160                         break;
1161                 case dump_node_mode_txt:
1162                         if (be_is_Copy(irn) || be_is_CopyKeep(irn)) {
1163                                 fprintf(f, "%s", get_mode_name(get_irn_mode(irn)));
1164                         }
1165                         break;
1166                 case dump_node_nodeattr_txt:
1167                         if (be_is_Call(irn)) {
1168                                 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(irn);
1169                                 if (a->ent)
1170                                         fprintf(f, " [%s] ", get_entity_name(a->ent));
1171                         }
1172                         if (be_is_IncSP(irn)) {
1173                                 const be_incsp_attr_t *attr = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
1174                                 fprintf(f, " [%d] ", attr->offset);
1175                         }
1176                         break;
1177                 case dump_node_info_txt:
1178                         arch_dump_reqs_and_registers(f, irn);
1179
1180                         if (be_has_frame_entity(irn)) {
1181                                 const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
1182                                 if (a->ent) {
1183                                         unsigned size = get_type_size_bytes(get_entity_type(a->ent));
1184                                         ir_fprintf(f, "frame entity: %+F, offset 0x%x (%d), size 0x%x (%d) bytes\n",
1185                                           a->ent, a->offset, a->offset, size, size);
1186                                 }
1187
1188                         }
1189
1190                         switch (get_irn_opcode(irn)) {
1191                         case beo_IncSP: {
1192                                 const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
1193                                 fprintf(f, "align: %d\n", a->align);
1194                                 fprintf(f, "offset: %d\n", a->offset);
1195                                 break;
1196                         }
1197                         case beo_Call: {
1198                                 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(irn);
1199
1200                                 if (a->ent)
1201                                         fprintf(f, "\ncalling: %s\n", get_entity_name(a->ent));
1202                                 break;
1203                         }
1204                         case beo_MemPerm: {
1205                                 int i;
1206                                 for (i = 0; i < be_get_MemPerm_entity_arity(irn); ++i) {
1207                                         ir_entity *in, *out;
1208                                         in = be_get_MemPerm_in_entity(irn, i);
1209                                         out = be_get_MemPerm_out_entity(irn, i);
1210                                         if (in) {
1211                                                 fprintf(f, "\nin[%d]: %s\n", i, get_entity_name(in));
1212                                         }
1213                                         if (out) {
1214                                                 fprintf(f, "\nout[%d]: %s\n", i, get_entity_name(out));
1215                                         }
1216                                 }
1217                                 break;
1218                         }
1219
1220                         default:
1221                                 break;
1222                         }
1223         }
1224 }
1225
1226 /**
1227  * ir_op-Operation:
1228  * Copies the backend specific attributes from old node to new node.
1229  */
1230 static void copy_attr(ir_graph *irg, const ir_node *old_node, ir_node *new_node)
1231 {
1232         const void     *old_attr = get_irn_generic_attr_const(old_node);
1233         void           *new_attr = get_irn_generic_attr(new_node);
1234         struct obstack *obst     = be_get_be_obst(irg);
1235         backend_info_t *old_info = be_get_info(old_node);
1236         backend_info_t *new_info = be_get_info(new_node);
1237
1238         assert(is_be_node(old_node));
1239         assert(is_be_node(new_node));
1240
1241         memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
1242
1243         new_info->flags = old_info->flags;
1244         if (old_info->out_infos != NULL) {
1245                 size_t n_outs = ARR_LEN(old_info->out_infos);
1246                 /* need dyanmic out infos? */
1247                 if (be_is_Perm(new_node)) {
1248                         new_info->out_infos = NEW_ARR_F(reg_out_info_t, n_outs);
1249                 } else {
1250                         new_info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_outs);
1251                 }
1252                 memcpy(new_info->out_infos, old_info->out_infos,
1253                            n_outs * sizeof(new_info->out_infos[0]));
1254         } else {
1255                 new_info->out_infos = NULL;
1256         }
1257
1258         /* input infos */
1259         if (old_info->in_reqs != NULL) {
1260                 unsigned n_ins = get_irn_arity(old_node);
1261                 /* need dynamic in infos? */
1262                 if (get_irn_op(old_node)->opar == oparity_dynamic) {
1263                         new_info->in_reqs = NEW_ARR_F(const arch_register_req_t*, n_ins);
1264                 } else {
1265                         new_info->in_reqs = OALLOCN(obst,const arch_register_req_t*, n_ins);
1266                 }
1267                 memcpy(new_info->in_reqs, old_info->in_reqs,
1268                        n_ins * sizeof(new_info->in_reqs[0]));
1269         } else {
1270                 new_info->in_reqs = NULL;
1271         }
1272 }
1273
1274 int is_be_node(const ir_node *irn)
1275 {
1276         return get_op_ops(get_irn_op(irn))->be_ops == &be_node_irn_ops;
1277 }
1278
1279 static ir_op *new_be_op(unsigned code, const char *name, op_pin_state p,
1280                         irop_flags flags, op_arity opar, size_t attr_size)
1281 {
1282         ir_op *res = new_ir_op(code, name, p, flags, opar, 0, attr_size);
1283         res->ops.dump_node = dump_node;
1284         res->ops.copy_attr = copy_attr;
1285         res->ops.be_ops    = &be_node_irn_ops;
1286         return res;
1287 }
1288
1289 void be_init_op(void)
1290 {
1291         unsigned opc;
1292
1293         assert(op_be_Spill == NULL);
1294
1295         /* Acquire all needed opcodes. */
1296         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));
1297         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));
1298         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));
1299         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));
1300         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));
1301         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));
1302         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));
1303         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));
1304         ir_op_set_memory_index(op_be_Call, n_be_Call_mem);
1305         ir_op_set_fragile_indices(op_be_Call, pn_be_Call_X_regular, pn_be_Call_X_except);
1306         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));
1307         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));
1308         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));
1309         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));
1310         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));
1311         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));
1312
1313         op_be_Spill->ops.node_cmp_attr     = FrameAddr_cmp_attr;
1314         op_be_Reload->ops.node_cmp_attr    = FrameAddr_cmp_attr;
1315         op_be_Perm->ops.node_cmp_attr      = be_nodes_equal;
1316         op_be_MemPerm->ops.node_cmp_attr   = be_nodes_equal;
1317         op_be_Copy->ops.node_cmp_attr      = be_nodes_equal;
1318         op_be_Keep->ops.node_cmp_attr      = be_nodes_equal;
1319         op_be_CopyKeep->ops.node_cmp_attr  = be_nodes_equal;
1320         op_be_Call->ops.node_cmp_attr      = Call_cmp_attr;
1321         op_be_Return->ops.node_cmp_attr    = Return_cmp_attr;
1322         op_be_AddSP->ops.node_cmp_attr     = be_nodes_equal;
1323         op_be_SubSP->ops.node_cmp_attr     = be_nodes_equal;
1324         op_be_IncSP->ops.node_cmp_attr     = IncSP_cmp_attr;
1325         op_be_Start->ops.node_cmp_attr     = be_nodes_equal;
1326         op_be_FrameAddr->ops.node_cmp_attr = FrameAddr_cmp_attr;
1327
1328         /* attach out dummy_ops to middle end nodes */
1329         for (opc = iro_First; opc <= iro_Last; ++opc) {
1330                 ir_op *op = ir_get_opcode(opc);
1331                 assert(op->ops.be_ops == NULL);
1332                 op->ops.be_ops = &dummy_be_irn_ops;
1333         }
1334
1335         op_Phi->ops.be_ops = &phi_irn_ops;
1336 }
1337
1338 void be_finish_op(void)
1339 {
1340         free_ir_op(op_be_Spill);     op_be_Spill     = NULL;
1341         free_ir_op(op_be_Reload);    op_be_Reload    = NULL;
1342         free_ir_op(op_be_Perm);      op_be_Perm      = NULL;
1343         free_ir_op(op_be_MemPerm);   op_be_MemPerm   = NULL;
1344         free_ir_op(op_be_Copy);      op_be_Copy      = NULL;
1345         free_ir_op(op_be_Keep);      op_be_Keep      = NULL;
1346         free_ir_op(op_be_CopyKeep);  op_be_CopyKeep  = NULL;
1347         free_ir_op(op_be_Call);      op_be_Call      = NULL;
1348         free_ir_op(op_be_Return);    op_be_Return    = NULL;
1349         free_ir_op(op_be_IncSP);     op_be_IncSP     = NULL;
1350         free_ir_op(op_be_AddSP);     op_be_AddSP     = NULL;
1351         free_ir_op(op_be_SubSP);     op_be_SubSP     = NULL;
1352         free_ir_op(op_be_Start);     op_be_Start     = NULL;
1353         free_ir_op(op_be_FrameAddr); op_be_FrameAddr = NULL;
1354 }