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