be: make several "variable" inputs/outputs dynamic
[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_class_t *cls)
1079 {
1080         const arch_register_req_t *req;
1081         ir_graph       *irg  = get_irn_irg(block);
1082         struct obstack *obst = be_get_be_obst(irg);
1083         backend_info_t *info;
1084         int             i;
1085
1086         ir_node *phi = new_r_Phi(block, n_ins, ins, mode);
1087         info = be_get_info(phi);
1088         info->out_infos = NEW_ARR_D(reg_out_info_t, obst, 1);
1089         memset(info->out_infos, 0, 1 * sizeof(info->out_infos[0]));
1090         info->in_reqs = OALLOCN(obst, const arch_register_req_t*, n_ins);
1091
1092         if (cls == NULL) {
1093                 req = arch_no_register_req;
1094         } else {
1095                 req = cls->class_req;
1096         }
1097         info->out_infos[0].req = req;
1098         for (i = 0; i < n_ins; ++i) {
1099                 info->in_reqs[i] = req;
1100         }
1101
1102         return phi;
1103 }
1104
1105 void be_set_phi_reg_req(ir_node *node, const arch_register_req_t *req)
1106 {
1107         int arity = get_irn_arity(node);
1108         int i;
1109
1110         backend_info_t *info = be_get_info(node);
1111         info->out_infos[0].req = req;
1112         for (i = 0; i < arity; ++i) {
1113                 info->in_reqs[i] = req;
1114         }
1115
1116         assert(mode_is_datab(get_irn_mode(node)));
1117 }
1118
1119 void be_dump_phi_reg_reqs(FILE *F, const ir_node *node, dump_reason_t reason)
1120 {
1121         switch (reason) {
1122         case dump_node_opcode_txt:
1123                 fputs(get_op_name(get_irn_op(node)), F);
1124                 break;
1125         case dump_node_mode_txt:
1126                 fprintf(F, "%s", get_mode_name(get_irn_mode(node)));
1127                 break;
1128         case dump_node_nodeattr_txt:
1129                 break;
1130         case dump_node_info_txt:
1131         {
1132                 backend_info_t *info = be_get_info(node);
1133                 if (info != NULL && info->out_infos[0].req != NULL) {
1134                         arch_dump_reqs_and_registers(F, node);
1135                 }
1136                 break;
1137         }
1138
1139         default:
1140                 break;
1141         }
1142 }
1143
1144 static const arch_irn_ops_t phi_irn_ops = {
1145         dummy_get_frame_entity,
1146         dummy_set_frame_offset,
1147         dummy_get_sp_bias,
1148         NULL,    /* get_inverse             */
1149         NULL,    /* get_op_estimated_cost   */
1150         NULL,    /* possible_memory_operand */
1151         NULL,    /* perform_memory_operand  */
1152 };
1153
1154
1155
1156 /**
1157  * ir_op-Operation: dump a be node to file
1158  */
1159 static void dump_node(FILE *f, const ir_node *irn, dump_reason_t reason)
1160 {
1161         assert(is_be_node(irn));
1162
1163         switch (reason) {
1164                 case dump_node_opcode_txt:
1165                         fputs(get_op_name(get_irn_op(irn)), f);
1166                         break;
1167                 case dump_node_mode_txt:
1168                         if (be_is_Copy(irn) || be_is_CopyKeep(irn)) {
1169                                 fprintf(f, "%s", get_mode_name(get_irn_mode(irn)));
1170                         }
1171                         break;
1172                 case dump_node_nodeattr_txt:
1173                         if (be_is_Call(irn)) {
1174                                 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(irn);
1175                                 if (a->ent)
1176                                         fprintf(f, " [%s] ", get_entity_name(a->ent));
1177                         }
1178                         if (be_is_IncSP(irn)) {
1179                                 const be_incsp_attr_t *attr = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
1180                                 fprintf(f, " [%d] ", attr->offset);
1181                         }
1182                         break;
1183                 case dump_node_info_txt:
1184                         arch_dump_reqs_and_registers(f, irn);
1185
1186                         if (be_has_frame_entity(irn)) {
1187                                 const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
1188                                 if (a->ent) {
1189                                         unsigned size = get_type_size_bytes(get_entity_type(a->ent));
1190                                         ir_fprintf(f, "frame entity: %+F, offset 0x%x (%d), size 0x%x (%d) bytes\n",
1191                                           a->ent, a->offset, a->offset, size, size);
1192                                 }
1193
1194                         }
1195
1196                         switch (get_irn_opcode(irn)) {
1197                         case beo_IncSP: {
1198                                 const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
1199                                 fprintf(f, "align: %d\n", a->align);
1200                                 fprintf(f, "offset: %d\n", a->offset);
1201                                 break;
1202                         }
1203                         case beo_Call: {
1204                                 const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(irn);
1205
1206                                 if (a->ent)
1207                                         fprintf(f, "\ncalling: %s\n", get_entity_name(a->ent));
1208                                 break;
1209                         }
1210                         case beo_MemPerm: {
1211                                 int i;
1212                                 for (i = 0; i < be_get_MemPerm_entity_arity(irn); ++i) {
1213                                         ir_entity *in, *out;
1214                                         in = be_get_MemPerm_in_entity(irn, i);
1215                                         out = be_get_MemPerm_out_entity(irn, i);
1216                                         if (in) {
1217                                                 fprintf(f, "\nin[%d]: %s\n", i, get_entity_name(in));
1218                                         }
1219                                         if (out) {
1220                                                 fprintf(f, "\nout[%d]: %s\n", i, get_entity_name(out));
1221                                         }
1222                                 }
1223                                 break;
1224                         }
1225
1226                         default:
1227                                 break;
1228                         }
1229         }
1230 }
1231
1232 /**
1233  * ir_op-Operation:
1234  * Copies the backend specific attributes from old node to new node.
1235  */
1236 static void copy_attr(ir_graph *irg, const ir_node *old_node, ir_node *new_node)
1237 {
1238         const void     *old_attr = get_irn_generic_attr_const(old_node);
1239         void           *new_attr = get_irn_generic_attr(new_node);
1240         struct obstack *obst     = be_get_be_obst(irg);
1241         backend_info_t *old_info = be_get_info(old_node);
1242         backend_info_t *new_info = be_get_info(new_node);
1243
1244         assert(is_be_node(old_node));
1245         assert(is_be_node(new_node));
1246
1247         memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
1248
1249         new_info->flags = old_info->flags;
1250         if (old_info->out_infos != NULL) {
1251                 size_t n_outs = ARR_LEN(old_info->out_infos);
1252                 /* need dyanmic out infos? */
1253                 if (be_is_Perm(new_node)) {
1254                         new_info->out_infos = NEW_ARR_F(reg_out_info_t, n_outs);
1255                 } else {
1256                         new_info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_outs);
1257                 }
1258                 memcpy(new_info->out_infos, old_info->out_infos,
1259                            n_outs * sizeof(new_info->out_infos[0]));
1260         } else {
1261                 new_info->out_infos = NULL;
1262         }
1263
1264         /* input infos */
1265         if (old_info->in_reqs != NULL) {
1266                 unsigned n_ins = get_irn_arity(old_node);
1267                 /* need dynamic in infos? */
1268                 if (get_irn_op(old_node)->opar == oparity_dynamic) {
1269                         new_info->in_reqs = NEW_ARR_F(const arch_register_req_t*, n_ins);
1270                 } else {
1271                         new_info->in_reqs = OALLOCN(obst,const arch_register_req_t*, n_ins);
1272                 }
1273                 memcpy(new_info->in_reqs, old_info->in_reqs,
1274                        n_ins * sizeof(new_info->in_reqs[0]));
1275         } else {
1276                 new_info->in_reqs = NULL;
1277         }
1278 }
1279
1280 static const ir_op_ops be_node_op_ops = {
1281         firm_default_hash,
1282         NULL,
1283         NULL,
1284         NULL,
1285         NULL,
1286         NULL,
1287         NULL,
1288         NULL,
1289         NULL,
1290         copy_attr,
1291         NULL,
1292         NULL,
1293         NULL,
1294         NULL,
1295         dump_node,
1296         NULL,
1297         &be_node_irn_ops
1298 };
1299
1300 int is_be_node(const ir_node *irn)
1301 {
1302         return get_op_ops(get_irn_op(irn))->be_ops == &be_node_irn_ops;
1303 }
1304
1305 void be_init_op(void)
1306 {
1307         unsigned opc;
1308
1309         assert(op_be_Spill == NULL);
1310
1311         /* Acquire all needed opcodes. */
1312         op_be_Spill     = new_ir_op(beo_Spill,     "be_Spill",     op_pin_state_exc_pinned, irop_flag_none,                          oparity_unary,    0, sizeof(be_frame_attr_t),   &be_node_op_ops);
1313         op_be_Reload    = new_ir_op(beo_Reload,    "be_Reload",    op_pin_state_exc_pinned, irop_flag_none,                          oparity_zero,     0, sizeof(be_frame_attr_t),   &be_node_op_ops);
1314         op_be_Perm      = new_ir_op(beo_Perm,      "be_Perm",      op_pin_state_exc_pinned, irop_flag_none,                          oparity_variable, 0, sizeof(be_node_attr_t),    &be_node_op_ops);
1315         op_be_MemPerm   = new_ir_op(beo_MemPerm,   "be_MemPerm",   op_pin_state_exc_pinned, irop_flag_none,                          oparity_variable, 0, sizeof(be_memperm_attr_t), &be_node_op_ops);
1316         op_be_Copy      = new_ir_op(beo_Copy,      "be_Copy",      op_pin_state_exc_pinned, irop_flag_none,                          oparity_unary,    0, sizeof(be_node_attr_t),    &be_node_op_ops);
1317         op_be_Keep      = new_ir_op(beo_Keep,      "be_Keep",      op_pin_state_exc_pinned, irop_flag_keep,                          oparity_dynamic,  0, sizeof(be_node_attr_t),    &be_node_op_ops);
1318         op_be_CopyKeep  = new_ir_op(beo_CopyKeep,  "be_CopyKeep",  op_pin_state_exc_pinned, irop_flag_keep,                          oparity_variable, 0, sizeof(be_node_attr_t),    &be_node_op_ops);
1319         op_be_Call      = new_ir_op(beo_Call,      "be_Call",      op_pin_state_exc_pinned, irop_flag_fragile|irop_flag_uses_memory, oparity_variable, 0, sizeof(be_call_attr_t),    &be_node_op_ops);
1320         ir_op_set_memory_index(op_be_Call, n_be_Call_mem);
1321         ir_op_set_fragile_indices(op_be_Call, pn_be_Call_X_regular, pn_be_Call_X_except);
1322         op_be_Return    = new_ir_op(beo_Return,    "be_Return",    op_pin_state_exc_pinned, irop_flag_cfopcode,                      oparity_variable, 0, sizeof(be_return_attr_t),  &be_node_op_ops);
1323         op_be_AddSP     = new_ir_op(beo_AddSP,     "be_AddSP",     op_pin_state_exc_pinned, irop_flag_none,                          oparity_unary,    0, sizeof(be_node_attr_t),    &be_node_op_ops);
1324         op_be_SubSP     = new_ir_op(beo_SubSP,     "be_SubSP",     op_pin_state_exc_pinned, irop_flag_none,                          oparity_unary,    0, sizeof(be_node_attr_t),    &be_node_op_ops);
1325         op_be_IncSP     = new_ir_op(beo_IncSP,     "be_IncSP",     op_pin_state_exc_pinned, irop_flag_none,                          oparity_unary,    0, sizeof(be_incsp_attr_t),   &be_node_op_ops);
1326         op_be_Start     = new_ir_op(beo_Start,     "be_Start",     op_pin_state_exc_pinned, irop_flag_none,                          oparity_zero,     0, sizeof(be_node_attr_t),    &be_node_op_ops);
1327         op_be_FrameAddr = new_ir_op(beo_FrameAddr, "be_FrameAddr", op_pin_state_exc_pinned, irop_flag_none,                          oparity_unary,    0, sizeof(be_frame_attr_t),   &be_node_op_ops);
1328
1329         op_be_Spill->ops.node_cmp_attr     = FrameAddr_cmp_attr;
1330         op_be_Reload->ops.node_cmp_attr    = FrameAddr_cmp_attr;
1331         op_be_Perm->ops.node_cmp_attr      = be_nodes_equal;
1332         op_be_MemPerm->ops.node_cmp_attr   = be_nodes_equal;
1333         op_be_Copy->ops.node_cmp_attr      = be_nodes_equal;
1334         op_be_Keep->ops.node_cmp_attr      = be_nodes_equal;
1335         op_be_CopyKeep->ops.node_cmp_attr  = be_nodes_equal;
1336         op_be_Call->ops.node_cmp_attr      = Call_cmp_attr;
1337         op_be_Return->ops.node_cmp_attr    = Return_cmp_attr;
1338         op_be_AddSP->ops.node_cmp_attr     = be_nodes_equal;
1339         op_be_SubSP->ops.node_cmp_attr     = be_nodes_equal;
1340         op_be_IncSP->ops.node_cmp_attr     = IncSP_cmp_attr;
1341         op_be_Start->ops.node_cmp_attr     = be_nodes_equal;
1342         op_be_FrameAddr->ops.node_cmp_attr = FrameAddr_cmp_attr;
1343
1344         /* attach out dummy_ops to middle end nodes */
1345         for (opc = iro_First; opc <= iro_Last; ++opc) {
1346                 ir_op *op = ir_get_opcode(opc);
1347                 assert(op->ops.be_ops == NULL);
1348                 op->ops.be_ops = &dummy_be_irn_ops;
1349         }
1350
1351         op_Phi->ops.be_ops = &phi_irn_ops;
1352 }
1353
1354 void be_finish_op(void)
1355 {
1356         free_ir_op(op_be_Spill);     op_be_Spill     = NULL;
1357         free_ir_op(op_be_Reload);    op_be_Reload    = NULL;
1358         free_ir_op(op_be_Perm);      op_be_Perm      = NULL;
1359         free_ir_op(op_be_MemPerm);   op_be_MemPerm   = NULL;
1360         free_ir_op(op_be_Copy);      op_be_Copy      = NULL;
1361         free_ir_op(op_be_Keep);      op_be_Keep      = NULL;
1362         free_ir_op(op_be_CopyKeep);  op_be_CopyKeep  = NULL;
1363         free_ir_op(op_be_Call);      op_be_Call      = NULL;
1364         free_ir_op(op_be_Return);    op_be_Return    = NULL;
1365         free_ir_op(op_be_IncSP);     op_be_IncSP     = NULL;
1366         free_ir_op(op_be_AddSP);     op_be_AddSP     = NULL;
1367         free_ir_op(op_be_SubSP);     op_be_SubSP     = NULL;
1368         free_ir_op(op_be_Start);     op_be_Start     = NULL;
1369         free_ir_op(op_be_FrameAddr); op_be_FrameAddr = NULL;
1370 }