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