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