Implement binary emitter for Perm.
[libfirm] / ir / be / benode.c
1 /*
2  * Copyright (C) 1995-2008 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  * @version     $Id$
26  *
27  * Backend node support for generic backend nodes.
28  * This file provides Perm, Copy, Spill and Reload nodes.
29  */
30 #include "config.h"
31
32 #include <stdlib.h>
33
34 #include "obst.h"
35 #include "set.h"
36 #include "pmap.h"
37 #include "util.h"
38 #include "debug.h"
39 #include "fourcc.h"
40 #include "offset.h"
41 #include "bitfiddle.h"
42 #include "raw_bitset.h"
43 #include "error.h"
44 #include "array_t.h"
45
46 #include "irop_t.h"
47 #include "irmode_t.h"
48 #include "irnode_t.h"
49 #include "ircons_t.h"
50 #include "irprintf.h"
51 #include "irgwalk.h"
52 #include "iropt_t.h"
53
54 #include "be_t.h"
55 #include "belive_t.h"
56 #include "besched.h"
57 #include "benode.h"
58 #include "bearch.h"
59
60 #include "beirgmod.h"
61
62 #define get_irn_attr(irn) get_irn_generic_attr(irn)
63 #define get_irn_attr_const(irn) get_irn_generic_attr_const(irn)
64
65 typedef struct {
66         const arch_register_req_t *in_req;
67 } be_reg_data_t;
68
69 /** The generic be nodes attribute type. */
70 typedef struct {
71         be_reg_data_t *reg_data;
72 } be_node_attr_t;
73
74 /** The be_Return nodes attribute type. */
75 typedef struct {
76         be_node_attr_t node_attr;     /**< base attributes of every be node. */
77         int            num_ret_vals;  /**< number of return values */
78         unsigned       pop;           /**< number of bytes that should be popped */
79         int            emit_pop;      /**< if set, emit pop bytes, even if pop = 0 */
80 } be_return_attr_t;
81
82 /** The be_IncSP attribute type. */
83 typedef struct {
84         be_node_attr_t node_attr;   /**< base attributes of every be node. */
85         int            offset;      /**< The offset by which the stack shall be expanded/shrinked. */
86         int            align;       /**< whether stack should be aligned after the
87                                          IncSP */
88 } be_incsp_attr_t;
89
90 /** The be_Frame attribute type. */
91 typedef struct {
92         be_node_attr_t  node_attr;   /**< base attributes of every be node. */
93         ir_entity      *ent;
94         int             offset;
95 } be_frame_attr_t;
96
97 /** The be_Call attribute type. */
98 typedef struct {
99         be_node_attr_t  node_attr;  /**< base attributes of every be node. */
100         ir_entity      *ent;        /**< The called entity if this is a static call. */
101         unsigned        pop;
102         ir_type        *call_tp;    /**< The call type, copied from the original Call node. */
103 } be_call_attr_t;
104
105 typedef struct {
106         be_node_attr_t   node_attr;  /**< base attributes of every be node. */
107         ir_entity      **in_entities;
108         ir_entity      **out_entities;
109 } be_memperm_attr_t;
110
111 ir_op *op_be_Spill;
112 ir_op *op_be_Reload;
113 ir_op *op_be_Perm;
114 ir_op *op_be_MemPerm;
115 ir_op *op_be_Copy;
116 ir_op *op_be_Keep;
117 ir_op *op_be_CopyKeep;
118 ir_op *op_be_Call;
119 ir_op *op_be_Return;
120 ir_op *op_be_IncSP;
121 ir_op *op_be_AddSP;
122 ir_op *op_be_SubSP;
123 ir_op *op_be_Start;
124 ir_op *op_be_FrameAddr;
125 ir_op *op_be_Barrier;
126
127 static const ir_op_ops be_node_op_ops;
128
129 #define N   irop_flag_none
130 #define L   irop_flag_labeled
131 #define C   irop_flag_commutative
132 #define X   irop_flag_cfopcode
133 #define I   irop_flag_ip_cfopcode
134 #define F   irop_flag_fragile
135 #define Y   irop_flag_forking
136 #define H   irop_flag_highlevel
137 #define c   irop_flag_constlike
138 #define K   irop_flag_keep
139 #define M   irop_flag_uses_memory
140
141 /**
142  * Compare two be node attributes.
143  *
144  * @return zero if both attributes are identically
145  */
146 static int node_cmp_attr(ir_node *a, ir_node *b)
147 {
148         const be_node_attr_t *a_attr = get_irn_attr_const(a);
149         const be_node_attr_t *b_attr = get_irn_attr_const(b);
150         int i, len = ARR_LEN(a_attr->reg_data);
151
152         if (len != ARR_LEN(b_attr->reg_data))
153                 return 1;
154
155         if (!be_nodes_equal(a, b))
156                 return 1;
157
158         for (i = len - 1; i >= 0; --i) {
159                 if (!reg_reqs_equal(a_attr->reg_data[i].in_req,
160                                     b_attr->reg_data[i].in_req))
161                         return 1;
162         }
163
164         return 0;
165 }
166
167 /**
168  * Compare the attributes of two be_FrameAddr nodes.
169  *
170  * @return zero if both nodes have identically attributes
171  */
172 static int FrameAddr_cmp_attr(ir_node *a, ir_node *b)
173 {
174         const be_frame_attr_t *a_attr = get_irn_attr_const(a);
175         const be_frame_attr_t *b_attr = get_irn_attr_const(b);
176
177         if (a_attr->ent != b_attr->ent || a_attr->offset != b_attr->offset)
178                 return 1;
179
180         return node_cmp_attr(a, b);
181 }
182
183 /**
184  * Compare the attributes of two be_Return nodes.
185  *
186  * @return zero if both nodes have identically attributes
187  */
188 static int Return_cmp_attr(ir_node *a, ir_node *b)
189 {
190         const be_return_attr_t *a_attr = get_irn_attr_const(a);
191         const be_return_attr_t *b_attr = get_irn_attr_const(b);
192
193         if (a_attr->num_ret_vals != b_attr->num_ret_vals)
194                 return 1;
195         if (a_attr->pop != b_attr->pop)
196                 return 1;
197         if (a_attr->emit_pop != b_attr->emit_pop)
198                 return 1;
199
200         return node_cmp_attr(a, b);
201 }
202
203 /**
204  * Compare the attributes of two be_IncSP nodes.
205  *
206  * @return zero if both nodes have identically attributes
207  */
208 static int IncSP_cmp_attr(ir_node *a, ir_node *b) {
209         const be_incsp_attr_t *a_attr = get_irn_attr_const(a);
210         const be_incsp_attr_t *b_attr = get_irn_attr_const(b);
211
212         if (a_attr->offset != b_attr->offset)
213                 return 1;
214
215         return node_cmp_attr(a, b);
216 }
217
218 /**
219  * Compare the attributes of two be_Call nodes.
220  *
221  * @return zero if both nodes have identically attributes
222  */
223 static int Call_cmp_attr(ir_node *a, ir_node *b)
224 {
225         const be_call_attr_t *a_attr = get_irn_attr_const(a);
226         const be_call_attr_t *b_attr = get_irn_attr_const(b);
227
228         if (a_attr->ent != b_attr->ent ||
229                 a_attr->call_tp != b_attr->call_tp)
230                 return 1;
231
232         return node_cmp_attr(a, b);
233 }
234
235 static arch_register_req_t *allocate_reg_req(const ir_node *node)
236 {
237         ir_graph       *irg  = get_irn_irg(node);
238         struct obstack *obst = be_get_birg_obst(irg);
239
240         arch_register_req_t *req = obstack_alloc(obst, sizeof(*req));
241         memset(req, 0, sizeof(*req));
242         return req;
243 }
244
245 void be_set_constr_in(ir_node *node, int pos, const arch_register_req_t *req)
246 {
247         const be_node_attr_t *attr = get_irn_attr_const(node);
248         be_reg_data_t *rd = &attr->reg_data[pos];
249         assert(pos < ARR_LEN(attr->reg_data));
250         rd->in_req = req;
251 }
252
253 void be_set_constr_out(ir_node *node, int pos, const arch_register_req_t *req)
254 {
255         backend_info_t *info = be_get_info(node);
256         info->out_infos[pos].req = req;
257 }
258
259 /**
260  * Initializes the generic attribute of all be nodes and return it.
261  */
262 static void *init_node_attr(ir_node *node, int n_inputs, int n_outputs)
263 {
264         ir_graph       *irg  = get_irn_irg(node);
265         struct obstack *obst = be_get_birg_obst(irg);
266         be_node_attr_t *a    = get_irn_attr(node);
267         backend_info_t *info = be_get_info(node);
268
269         memset(a, 0, sizeof(get_op_attr_size(get_irn_op(node))));
270
271         if (n_inputs >= 0) {
272                 int i;
273                 a->reg_data = NEW_ARR_D(be_reg_data_t, obst, n_inputs);
274                 for (i = 0; i < n_inputs; ++i) {
275                         a->reg_data[i].in_req = arch_no_register_req;
276                 }
277         } else {
278                 a->reg_data = NEW_ARR_F(be_reg_data_t, 0);
279         }
280
281         if (n_outputs >= 0) {
282                 int i;
283                 info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_outputs);
284                 memset(info->out_infos, 0, n_outputs * sizeof(info->out_infos[0]));
285                 for (i = 0; i < n_outputs; ++i) {
286                         info->out_infos[i].req = arch_no_register_req;
287                 }
288         } else {
289                 info->out_infos = NEW_ARR_F(reg_out_info_t, 0);
290         }
291
292         return a;
293 }
294
295 static void add_register_req_out(ir_node *node)
296 {
297         backend_info_t *info = be_get_info(node);
298         reg_out_info_t  out_info;
299         memset(&out_info, 0, sizeof(out_info));
300         out_info.req = arch_no_register_req;
301         ARR_APP1(reg_out_info_t, info->out_infos, out_info);
302 }
303
304 static void add_register_req_in(ir_node *node)
305 {
306         be_node_attr_t *a    = get_irn_attr(node);
307         be_reg_data_t   regreq;
308         memset(&regreq, 0, sizeof(regreq));
309         regreq.in_req = arch_no_register_req;
310         ARR_APP1(be_reg_data_t, a->reg_data, regreq);
311 }
312
313 ir_node *be_new_Spill(const arch_register_class_t *cls,
314                 const arch_register_class_t *cls_frame, ir_node *bl,
315                 ir_node *frame, ir_node *to_spill)
316 {
317         be_frame_attr_t *a;
318         ir_node         *in[2];
319         ir_node         *res;
320         ir_graph        *irg = get_Block_irg(bl);
321
322         in[0]     = frame;
323         in[1]     = to_spill;
324         res       = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
325         a         = init_node_attr(res, 2, 1);
326         a->ent    = NULL;
327         a->offset = 0;
328
329         be_node_set_reg_class_in(res, be_pos_Spill_frame, cls_frame);
330         be_node_set_reg_class_in(res, be_pos_Spill_val, cls);
331         /*
332          * For spills and reloads, we return "none" as requirement for frame
333          * pointer, so every input is ok. Some backends need this (STA).
334          * Matze: we should investigate if this is really needed, this solution
335          *        looks very hacky to me
336          */
337         be_set_constr_in(res, be_pos_Spill_frame, arch_no_register_req);
338
339         arch_set_out_register_req(res, 0, arch_no_register_req);
340
341         return res;
342 }
343
344 ir_node *be_new_Reload(const arch_register_class_t *cls,
345                 const arch_register_class_t *cls_frame, ir_node *block,
346                 ir_node *frame, ir_node *mem, ir_mode *mode)
347 {
348         ir_node  *in[2];
349         ir_node  *res;
350         ir_graph *irg = get_Block_irg(block);
351
352         in[0] = frame;
353         in[1] = mem;
354         res   = new_ir_node(NULL, irg, block, op_be_Reload, mode, 2, in);
355
356         init_node_attr(res, 2, 1);
357         be_node_set_reg_class_out(res, 0, cls);
358
359         be_node_set_reg_class_in(res, be_pos_Reload_frame, cls_frame);
360         arch_irn_set_flags(res, arch_irn_flags_rematerializable);
361
362         /*
363          * For spills and reloads, we return "none" as requirement for frame
364          * pointer, so every input is ok. Some backends need this (e.g. STA).
365          * Matze: we should investigate if this is really needed, this solution
366          *        looks very hacky to me
367          */
368         be_set_constr_in(res, be_pos_Reload_frame, arch_no_register_req);
369
370         return res;
371 }
372
373 ir_node *be_get_Reload_mem(const ir_node *irn)
374 {
375         assert(be_is_Reload(irn));
376         return get_irn_n(irn, be_pos_Reload_mem);
377 }
378
379 ir_node *be_get_Reload_frame(const ir_node *irn)
380 {
381         assert(be_is_Reload(irn));
382         return get_irn_n(irn, be_pos_Reload_frame);
383 }
384
385 ir_node *be_get_Spill_val(const ir_node *irn)
386 {
387         assert(be_is_Spill(irn));
388         return get_irn_n(irn, be_pos_Spill_val);
389 }
390
391 ir_node *be_get_Spill_frame(const ir_node *irn)
392 {
393         assert(be_is_Spill(irn));
394         return get_irn_n(irn, be_pos_Spill_frame);
395 }
396
397 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_node *block,
398                      int n, ir_node *in[])
399 {
400         int      i;
401         ir_graph *irg = get_Block_irg(block);
402
403         ir_node *irn = new_ir_node(NULL, irg, block, op_be_Perm, mode_T, n, in);
404         init_node_attr(irn, n, n);
405         for (i = 0; i < n; ++i) {
406                 be_node_set_reg_class_in(irn, i, cls);
407                 be_node_set_reg_class_out(irn, i, cls);
408         }
409
410         return irn;
411 }
412
413 void be_Perm_reduce(ir_node *perm, int new_size, int *map)
414 {
415         int            arity      = get_irn_arity(perm);
416         be_reg_data_t  *old_data  = ALLOCAN(be_reg_data_t, arity);
417         reg_out_info_t *old_infos = ALLOCAN(reg_out_info_t, arity);
418         be_node_attr_t *attr      = get_irn_attr(perm);
419         backend_info_t *info      = be_get_info(perm);
420         ir_node        **new_in;
421
422         int i;
423
424         assert(be_is_Perm(perm));
425         assert(new_size <= arity);
426
427         new_in = alloca(new_size * sizeof(*new_in));
428
429         /* save the old register data */
430         memcpy(old_data, attr->reg_data, arity * sizeof(old_data[0]));
431         memcpy(old_infos, info->out_infos, arity * sizeof(old_infos[0]));
432
433         /* compose the new in array and set the new register data directly in place */
434         for (i = 0; i < new_size; ++i) {
435                 int idx = map[i];
436                 new_in[i]          = get_irn_n(perm, idx);
437                 attr->reg_data[i]  = old_data[idx];
438                 info->out_infos[i] = old_infos[idx];
439         }
440
441         set_irn_in(perm, new_size, new_in);
442 }
443
444 ir_node *be_new_MemPerm(const arch_env_t *arch_env, ir_node *bl, int n, ir_node *in[])
445 {
446         ir_graph                     *irg       = get_Block_irg(bl);
447         ir_node                      *frame     = get_irg_frame(irg);
448         const arch_register_class_t  *cls_frame = arch_get_irn_reg_class_out(frame);
449         const arch_register_t        *sp        = arch_env->sp;
450         ir_node                      *irn;
451         be_memperm_attr_t            *attr;
452         ir_node                     **real_in;
453         int                           i;
454
455         real_in = ALLOCAN(ir_node*, n + 1);
456         real_in[0] = frame;
457         memcpy(&real_in[1], in, n * sizeof(real_in[0]));
458
459         irn =  new_ir_node(NULL, irg, bl, op_be_MemPerm, mode_T, n+1, real_in);
460
461         init_node_attr(irn, n + 1, n + 1);
462         be_node_set_reg_class_in(irn, 0, sp->reg_class);
463         for (i = 0; i < n; ++i) {
464                 be_node_set_reg_class_in(irn, i + 1, cls_frame);
465                 be_node_set_reg_class_out(irn, i, cls_frame);
466         }
467
468         attr = get_irn_attr(irn);
469         attr->in_entities  = OALLOCNZ(irg->obst, ir_entity*, n);
470         attr->out_entities = OALLOCNZ(irg->obst, ir_entity*, n);
471
472         return irn;
473 }
474
475 ir_node *be_new_Copy(const arch_register_class_t *cls, ir_node *bl, ir_node *op)
476 {
477         ir_node *in[1];
478         ir_node *res;
479         arch_register_req_t *req;
480         ir_graph *irg = get_Block_irg(bl);
481
482         in[0] = op;
483         res   = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
484         init_node_attr(res, 1, 1);
485         be_node_set_reg_class_in(res, 0, cls);
486         be_node_set_reg_class_out(res, 0, cls);
487
488         req = allocate_reg_req(res);
489         req->cls        = cls;
490         req->type       = arch_register_req_type_should_be_same;
491         req->other_same = 1U << 0;
492         be_set_constr_out(res, 0, req);
493
494         return res;
495 }
496
497 ir_node *be_get_Copy_op(const ir_node *cpy) {
498         return get_irn_n(cpy, be_pos_Copy_op);
499 }
500
501 void be_set_Copy_op(ir_node *cpy, ir_node *op) {
502         set_irn_n(cpy, be_pos_Copy_op, op);
503 }
504
505 ir_node *be_new_Keep(ir_node *block, int n, ir_node *in[])
506 {
507         int i;
508         ir_node *res;
509         ir_graph *irg = get_Block_irg(block);
510
511         res = new_ir_node(NULL, irg, block, op_be_Keep, mode_ANY, -1, NULL);
512         init_node_attr(res, -1, 1);
513
514         for (i = 0; i < n; ++i) {
515                 add_irn_n(res, in[i]);
516                 add_register_req_in(res);
517         }
518         keep_alive(res);
519
520         return res;
521 }
522
523 void be_Keep_add_node(ir_node *keep, const arch_register_class_t *cls, ir_node *node)
524 {
525         int n;
526
527         assert(be_is_Keep(keep));
528         n = add_irn_n(keep, node);
529         add_register_req_in(keep);
530         be_node_set_reg_class_in(keep, n, cls);
531 }
532
533 /* creates a be_Call */
534 ir_node *be_new_Call(dbg_info *dbg, ir_graph *irg, ir_node *bl, ir_node *mem,
535                 ir_node *sp, ir_node *ptr, int n_outs, int n, ir_node *in[],
536                 ir_type *call_tp)
537 {
538         be_call_attr_t *a;
539         int real_n = be_pos_Call_first_arg + n;
540         ir_node *irn;
541         ir_node **real_in;
542
543         NEW_ARR_A(ir_node *, real_in, real_n);
544         real_in[be_pos_Call_mem] = mem;
545         real_in[be_pos_Call_sp]  = sp;
546         real_in[be_pos_Call_ptr] = ptr;
547         memcpy(&real_in[be_pos_Call_first_arg], in, n * sizeof(in[0]));
548
549         irn = new_ir_node(dbg, irg, bl, op_be_Call, mode_T, real_n, real_in);
550         a = init_node_attr(irn, real_n, n_outs);
551         a->ent     = NULL;
552         a->call_tp = call_tp;
553         a->pop     = 0;
554         return irn;
555 }
556
557 /* Gets the call entity or NULL if this is no static call. */
558 ir_entity *be_Call_get_entity(const ir_node *call) {
559         const be_call_attr_t *a = get_irn_attr_const(call);
560         assert(be_is_Call(call));
561         return a->ent;
562 }
563
564 /* Sets the call entity. */
565 void be_Call_set_entity(ir_node *call, ir_entity *ent) {
566         be_call_attr_t *a = get_irn_attr(call);
567         assert(be_is_Call(call));
568         a->ent = ent;
569 }
570
571 /* Gets the call type. */
572 ir_type *be_Call_get_type(ir_node *call) {
573         const be_call_attr_t *a = get_irn_attr_const(call);
574         assert(be_is_Call(call));
575         return a->call_tp;
576 }
577
578 /* Sets the call type. */
579 void be_Call_set_type(ir_node *call, ir_type *call_tp) {
580         be_call_attr_t *a = get_irn_attr(call);
581         assert(be_is_Call(call));
582         a->call_tp = call_tp;
583 }
584
585 void be_Call_set_pop(ir_node *call, unsigned pop) {
586         be_call_attr_t *a = get_irn_attr(call);
587         a->pop = pop;
588 }
589
590 unsigned be_Call_get_pop(const ir_node *call) {
591         const be_call_attr_t *a = get_irn_attr_const(call);
592         return a->pop;
593 }
594
595 /* Construct a new be_Return. */
596 ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *block, int n_res,
597                        unsigned pop, int n, ir_node *in[])
598 {
599         be_return_attr_t *a;
600         ir_node *res;
601         int i;
602
603         res = new_ir_node(dbg, irg, block, op_be_Return, mode_X, -1, NULL);
604         init_node_attr(res, -1, 1);
605         for (i = 0; i < n; ++i) {
606                 add_irn_n(res, in[i]);
607                 add_register_req_in(res);
608         }
609         be_set_constr_out(res, 0, arch_no_register_req);
610
611         a = get_irn_attr(res);
612         a->num_ret_vals = n_res;
613         a->pop          = pop;
614         a->emit_pop     = 0;
615
616         return res;
617 }
618
619 /* Returns the number of real returns values */
620 int be_Return_get_n_rets(const ir_node *ret) {
621         const be_return_attr_t *a = get_irn_generic_attr_const(ret);
622         return a->num_ret_vals;
623 }
624
625 /* return the number of bytes that should be popped from stack when executing the Return. */
626 unsigned be_Return_get_pop(const ir_node *ret) {
627         const be_return_attr_t *a = get_irn_generic_attr_const(ret);
628         return a->pop;
629 }
630
631 /* return non-zero, if number of popped bytes must be always emitted */
632 int be_Return_get_emit_pop(const ir_node *ret) {
633         const be_return_attr_t *a = get_irn_generic_attr_const(ret);
634         return a->emit_pop;
635 }
636
637 /* return non-zero, if number of popped bytes must be always emitted */
638 void be_Return_set_emit_pop(ir_node *ret, int emit_pop) {
639         be_return_attr_t *a = get_irn_generic_attr(ret);
640         a->emit_pop = emit_pop;
641 }
642
643 int be_Return_append_node(ir_node *ret, ir_node *node) {
644         int pos;
645
646         pos = add_irn_n(ret, node);
647         add_register_req_in(ret);
648
649         return pos;
650 }
651
652 ir_node *be_new_IncSP(const arch_register_t *sp, ir_node *bl,
653                       ir_node *old_sp, int offset, int align)
654 {
655         be_incsp_attr_t *a;
656         ir_node *irn;
657         ir_node *in[1];
658         ir_graph *irg = get_Block_irg(bl);
659
660         in[0]     = old_sp;
661         irn       = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode,
662                                 sizeof(in) / sizeof(in[0]), in);
663         a         = init_node_attr(irn, 1, 1);
664         a->offset = offset;
665         a->align  = align;
666
667         /* Set output constraint to stack register. */
668         be_node_set_reg_class_in(irn, 0, sp->reg_class);
669         be_set_constr_single_reg_out(irn, 0, sp, arch_register_req_type_produces_sp);
670
671         return irn;
672 }
673
674 ir_node *be_new_AddSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp,
675                 ir_node *sz)
676 {
677         be_node_attr_t *a;
678         ir_node *irn;
679         ir_node *in[be_pos_AddSP_last];
680         const arch_register_class_t *cls;
681         ir_graph *irg;
682
683         in[be_pos_AddSP_old_sp] = old_sp;
684         in[be_pos_AddSP_size]   = sz;
685
686         irg = get_Block_irg(bl);
687         irn = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T, be_pos_AddSP_last, in);
688         a   = init_node_attr(irn, be_pos_AddSP_last, pn_be_AddSP_last);
689
690         /* Set output constraint to stack register. */
691         be_set_constr_single_reg_in(irn, be_pos_AddSP_old_sp, sp, 0);
692         be_node_set_reg_class_in(irn, be_pos_AddSP_size, arch_register_get_class(sp));
693         be_set_constr_single_reg_out(irn, pn_be_AddSP_sp, sp, arch_register_req_type_produces_sp);
694
695         cls = arch_register_get_class(sp);
696
697         return irn;
698 }
699
700 ir_node *be_new_SubSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp, ir_node *sz)
701 {
702         be_node_attr_t *a;
703         ir_node *irn;
704         ir_node *in[be_pos_SubSP_last];
705         ir_graph *irg;
706
707         in[be_pos_SubSP_old_sp] = old_sp;
708         in[be_pos_SubSP_size]   = sz;
709
710         irg = get_Block_irg(bl);
711         irn = new_ir_node(NULL, irg, bl, op_be_SubSP, mode_T, be_pos_SubSP_last, in);
712         a   = init_node_attr(irn, be_pos_SubSP_last, pn_be_SubSP_last);
713
714         /* Set output constraint to stack register. */
715         be_set_constr_single_reg_in(irn, be_pos_SubSP_old_sp, sp, 0);
716         be_node_set_reg_class_in(irn, be_pos_SubSP_size, arch_register_get_class(sp));
717         be_set_constr_single_reg_out(irn, pn_be_SubSP_sp, sp, arch_register_req_type_produces_sp);
718
719         return irn;
720 }
721
722 ir_node *be_new_Start(ir_node *bl, int n_outs)
723 {
724         ir_node *res;
725         int i;
726         ir_graph *irg = get_Block_irg(bl);
727
728         res = new_ir_node(NULL, irg, bl, op_be_Start, mode_T, 0, NULL);
729         init_node_attr(res, 0, -1);
730         for (i = 0; i < n_outs; ++i) {
731                 add_register_req_out(res);
732         }
733
734         return res;
735 }
736
737 ir_node *be_new_FrameAddr(const arch_register_class_t *cls_frame, ir_node *bl, ir_node *frame, ir_entity *ent)
738 {
739         be_frame_attr_t *a;
740         ir_node *irn;
741         ir_node *in[1];
742         ir_graph *irg = get_Block_irg(bl);
743
744         in[0]  = frame;
745         irn    = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
746         a      = init_node_attr(irn, 1, 1);
747         a->ent = ent;
748         a->offset = 0;
749         be_node_set_reg_class_in(irn, 0, cls_frame);
750         be_node_set_reg_class_out(irn, 0, cls_frame);
751
752         return optimize_node(irn);
753 }
754
755 ir_node *be_get_FrameAddr_frame(const ir_node *node) {
756         assert(be_is_FrameAddr(node));
757         return get_irn_n(node, be_pos_FrameAddr_ptr);
758 }
759
760 ir_entity *be_get_FrameAddr_entity(const ir_node *node)
761 {
762         const be_frame_attr_t *attr = get_irn_generic_attr_const(node);
763         return attr->ent;
764 }
765
766 ir_node *be_new_CopyKeep(const arch_register_class_t *cls, ir_node *bl, ir_node *src, int n, ir_node *in_keep[], ir_mode *mode)
767 {
768         ir_node  *irn;
769         ir_node **in = ALLOCAN(ir_node*, n + 1);
770         ir_graph *irg = get_Block_irg(bl);
771
772         in[0] = src;
773         memcpy(&in[1], in_keep, n * sizeof(in[0]));
774         irn   = new_ir_node(NULL, irg, bl, op_be_CopyKeep, mode, n + 1, in);
775         init_node_attr(irn, n + 1, 1);
776         be_node_set_reg_class_in(irn, 0, cls);
777         be_node_set_reg_class_out(irn, 0, cls);
778
779         return irn;
780 }
781
782 ir_node *be_new_CopyKeep_single(const arch_register_class_t *cls, ir_node *bl, ir_node *src, ir_node *keep, ir_mode *mode)
783 {
784         return be_new_CopyKeep(cls, bl, src, 1, &keep, mode);
785 }
786
787 ir_node *be_get_CopyKeep_op(const ir_node *cpy) {
788         return get_irn_n(cpy, be_pos_CopyKeep_op);
789 }
790
791 void be_set_CopyKeep_op(ir_node *cpy, ir_node *op) {
792         set_irn_n(cpy, be_pos_CopyKeep_op, op);
793 }
794
795 ir_node *be_new_Barrier(ir_node *bl, int n, ir_node *in[])
796 {
797         ir_node *res;
798         int i;
799         ir_graph *irg = get_Block_irg(bl);
800
801         res = new_ir_node(NULL, irg, bl, op_be_Barrier, mode_T, -1, NULL);
802         init_node_attr(res, -1, -1);
803         for (i = 0; i < n; ++i) {
804                 add_irn_n(res, in[i]);
805                 add_register_req_in(res);
806                 add_register_req_out(res);
807         }
808
809         return res;
810 }
811
812 ir_node *be_Barrier_append_node(ir_node *barrier, ir_node *node)
813 {
814         ir_node *block = get_nodes_block(barrier);
815         ir_mode *mode = get_irn_mode(node);
816         int n = add_irn_n(barrier, node);
817
818         ir_node *proj = new_r_Proj(block, barrier, mode, n);
819         add_register_req_in(barrier);
820         add_register_req_out(barrier);
821
822         return proj;
823 }
824
825 int be_has_frame_entity(const ir_node *irn)
826 {
827         switch (get_irn_opcode(irn)) {
828         case beo_Spill:
829         case beo_Reload:
830         case beo_FrameAddr:
831                 return 1;
832         default:
833                 return 0;
834         }
835 }
836
837 ir_entity *be_get_frame_entity(const ir_node *irn)
838 {
839         if (be_has_frame_entity(irn)) {
840                 const be_frame_attr_t *a = get_irn_attr_const(irn);
841                 return a->ent;
842         }
843         return NULL;
844 }
845
846 int be_get_frame_offset(const ir_node *irn)
847 {
848         assert(is_be_node(irn));
849         if (be_has_frame_entity(irn)) {
850                 const be_frame_attr_t *a = get_irn_attr_const(irn);
851                 return a->offset;
852         }
853         return 0;
854 }
855
856 void be_set_MemPerm_in_entity(const ir_node *irn, int n, ir_entity *ent)
857 {
858         const be_memperm_attr_t *attr = get_irn_attr_const(irn);
859
860         assert(be_is_MemPerm(irn));
861         assert(n < be_get_MemPerm_entity_arity(irn));
862
863         attr->in_entities[n] = ent;
864 }
865
866 ir_entity* be_get_MemPerm_in_entity(const ir_node* irn, int n)
867 {
868         const be_memperm_attr_t *attr = get_irn_attr_const(irn);
869
870         assert(be_is_MemPerm(irn));
871         assert(n < be_get_MemPerm_entity_arity(irn));
872
873         return attr->in_entities[n];
874 }
875
876 void be_set_MemPerm_out_entity(const ir_node *irn, int n, ir_entity *ent)
877 {
878         const be_memperm_attr_t *attr = get_irn_attr_const(irn);
879
880         assert(be_is_MemPerm(irn));
881         assert(n < be_get_MemPerm_entity_arity(irn));
882
883         attr->out_entities[n] = ent;
884 }
885
886 ir_entity* be_get_MemPerm_out_entity(const ir_node* irn, int n)
887 {
888         const be_memperm_attr_t *attr = get_irn_attr_const(irn);
889
890         assert(be_is_MemPerm(irn));
891         assert(n < be_get_MemPerm_entity_arity(irn));
892
893         return attr->out_entities[n];
894 }
895
896 int be_get_MemPerm_entity_arity(const ir_node *irn)
897 {
898         return get_irn_arity(irn) - 1;
899 }
900
901 static const arch_register_req_t *get_single_req(struct obstack *obst,
902                 const arch_register_t *reg, arch_register_req_type_t additional_types)
903 {
904         arch_register_req_t         *req = obstack_alloc(obst, sizeof(*req));
905         const arch_register_class_t *cls = arch_register_get_class(reg);
906         unsigned                    *limited_bitset;
907
908         limited_bitset = rbitset_obstack_alloc(obst, arch_register_class_n_regs(cls));
909         rbitset_set(limited_bitset, arch_register_get_index(reg));
910
911         req->type    = arch_register_req_type_limited | additional_types;
912         req->cls     = cls;
913         req->limited = limited_bitset;
914         return req;
915 }
916
917 void be_set_constr_single_reg_in(ir_node *node, int pos,
918                 const arch_register_t *reg, arch_register_req_type_t additional_types)
919 {
920         const arch_register_req_t *req;
921
922         if (additional_types == 0) {
923                 req = reg->single_req;
924         } else {
925                 ir_graph       *irg  = get_irn_irg(node);
926                 struct obstack *obst = be_get_birg_obst(irg);
927                 req = get_single_req(obst, reg, additional_types);
928         }
929         be_set_constr_in(node, pos, req);
930 }
931
932 void be_set_constr_single_reg_out(ir_node *node, int pos,
933                 const arch_register_t *reg, arch_register_req_type_t additional_types)
934 {
935         const arch_register_req_t *req;
936
937         /* if we have an ignore register, add ignore flag and just assign it */
938         if (reg->type & arch_register_type_ignore) {
939                 additional_types |= arch_register_req_type_ignore;
940         }
941
942         if (additional_types == 0) {
943                 req = reg->single_req;
944         } else {
945                 ir_graph       *irg  = get_irn_irg(node);
946                 struct obstack *obst = be_get_birg_obst(irg);
947                 req = get_single_req(obst, reg, additional_types);
948         }
949
950         arch_irn_set_register(node, pos, reg);
951         be_set_constr_out(node, pos, req);
952 }
953
954 void be_node_set_reg_class_in(ir_node *irn, int pos,
955                               const arch_register_class_t *cls)
956 {
957         be_set_constr_in(irn, pos, cls->class_req);
958 }
959
960 void be_node_set_reg_class_out(ir_node *irn, int pos,
961                                const arch_register_class_t *cls)
962 {
963         be_set_constr_out(irn, pos, cls->class_req);
964 }
965
966 ir_node *be_get_IncSP_pred(ir_node *irn) {
967         assert(be_is_IncSP(irn));
968         return get_irn_n(irn, 0);
969 }
970
971 void be_set_IncSP_pred(ir_node *incsp, ir_node *pred) {
972         assert(be_is_IncSP(incsp));
973         set_irn_n(incsp, 0, pred);
974 }
975
976 void be_set_IncSP_offset(ir_node *irn, int offset)
977 {
978         be_incsp_attr_t *a = get_irn_attr(irn);
979         assert(be_is_IncSP(irn));
980         a->offset = offset;
981 }
982
983 int be_get_IncSP_offset(const ir_node *irn)
984 {
985         const be_incsp_attr_t *a = get_irn_attr_const(irn);
986         assert(be_is_IncSP(irn));
987         return a->offset;
988 }
989
990 int be_get_IncSP_align(const ir_node *irn)
991 {
992         const be_incsp_attr_t *a = get_irn_attr_const(irn);
993         assert(be_is_IncSP(irn));
994         return a->align;
995 }
996
997 ir_node *be_spill(ir_node *block, ir_node *irn)
998 {
999         ir_graph                    *irg       = get_Block_irg(block);
1000         ir_node                     *frame     = get_irg_frame(irg);
1001         const arch_register_class_t *cls       = arch_get_irn_reg_class_out(irn);
1002         const arch_register_class_t *cls_frame = arch_get_irn_reg_class_out(frame);
1003         ir_node                     *spill;
1004
1005         spill = be_new_Spill(cls, cls_frame, block, frame, irn);
1006         return spill;
1007 }
1008
1009 ir_node *be_reload(const arch_register_class_t *cls, ir_node *insert, ir_mode *mode, ir_node *spill)
1010 {
1011         ir_node  *reload;
1012         ir_node  *bl    = is_Block(insert) ? insert : get_nodes_block(insert);
1013         ir_graph *irg   = get_Block_irg(bl);
1014         ir_node  *frame = get_irg_frame(irg);
1015         const arch_register_class_t *cls_frame = arch_get_irn_reg_class_out(frame);
1016
1017         assert(be_is_Spill(spill) || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
1018
1019         reload = be_new_Reload(cls, cls_frame, bl, frame, spill, mode);
1020
1021         if (is_Block(insert)) {
1022                 insert = sched_skip(insert, 0, sched_skip_cf_predicator, NULL);
1023                 sched_add_after(insert, reload);
1024         } else {
1025                 sched_add_before(insert, reload);
1026         }
1027
1028         return reload;
1029 }
1030
1031 /*
1032   ____              ____
1033  |  _ \ ___  __ _  |  _ \ ___  __ _ ___
1034  | |_) / _ \/ _` | | |_) / _ \/ _` / __|
1035  |  _ <  __/ (_| | |  _ <  __/ (_| \__ \
1036  |_| \_\___|\__, | |_| \_\___|\__, |___/
1037             |___/                |_|
1038
1039 */
1040
1041
1042 static const arch_register_req_t *be_node_get_in_reg_req(
1043                 const ir_node *irn, int pos)
1044 {
1045         const be_node_attr_t *a = get_irn_attr_const(irn);
1046
1047         assert(pos >= 0);
1048         if (pos >= get_irn_arity(irn) || pos >= ARR_LEN(a->reg_data))
1049                 return arch_no_register_req;
1050
1051         return a->reg_data[pos].in_req;
1052 }
1053
1054 static arch_irn_class_t be_node_classify(const ir_node *irn)
1055 {
1056         switch (get_irn_opcode(irn)) {
1057                 case beo_Spill:  return arch_irn_class_spill;
1058                 case beo_Reload: return arch_irn_class_reload;
1059                 case beo_Perm:   return arch_irn_class_perm;
1060                 case beo_Copy:   return arch_irn_class_copy;
1061                 default:         return 0;
1062         }
1063 }
1064
1065 static ir_entity *be_node_get_frame_entity(const ir_node *irn)
1066 {
1067         return be_get_frame_entity(irn);
1068 }
1069
1070 static void be_node_set_frame_entity(ir_node *irn, ir_entity *ent)
1071 {
1072         be_frame_attr_t *a;
1073
1074         assert(be_has_frame_entity(irn));
1075
1076         a = get_irn_attr(irn);
1077         a->ent = ent;
1078 }
1079
1080 static void be_node_set_frame_offset(ir_node *irn, int offset)
1081 {
1082         be_frame_attr_t *a;
1083
1084         if (!be_has_frame_entity(irn))
1085                 return;
1086
1087         a = get_irn_attr(irn);
1088         a->offset = offset;
1089 }
1090
1091 static int be_node_get_sp_bias(const ir_node *irn)
1092 {
1093         if (be_is_IncSP(irn))
1094                 return be_get_IncSP_offset(irn);
1095         if (be_is_Call(irn))
1096                 return -(int)be_Call_get_pop(irn);
1097
1098         return 0;
1099 }
1100
1101 /*
1102   ___ ____  _   _   _   _                 _ _
1103  |_ _|  _ \| \ | | | | | | __ _ _ __   __| | | ___ _ __
1104   | || |_) |  \| | | |_| |/ _` | '_ \ / _` | |/ _ \ '__|
1105   | ||  _ <| |\  | |  _  | (_| | | | | (_| | |  __/ |
1106  |___|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
1107
1108 */
1109
1110 /* for be nodes */
1111 static const arch_irn_ops_t be_node_irn_ops = {
1112         be_node_get_in_reg_req,
1113         be_node_classify,
1114         be_node_get_frame_entity,
1115         be_node_set_frame_entity,
1116         be_node_set_frame_offset,
1117         be_node_get_sp_bias,
1118         NULL,    /* get_inverse             */
1119         NULL,    /* get_op_estimated_cost   */
1120         NULL,    /* possible_memory_operand */
1121         NULL,    /* perform_memory_operand  */
1122 };
1123
1124 static const arch_register_req_t *dummy_reg_req(
1125                 const ir_node *node, int pos)
1126 {
1127         (void) node;
1128         (void) pos;
1129         return arch_no_register_req;
1130 }
1131
1132 static arch_irn_class_t dummy_classify(const ir_node *node)
1133 {
1134         (void) node;
1135         return 0;
1136 }
1137
1138 static ir_entity* dummy_get_frame_entity(const ir_node *node)
1139 {
1140         (void) node;
1141         return NULL;
1142 }
1143
1144 static void dummy_set_frame_entity(ir_node *node, ir_entity *entity)
1145 {
1146         (void) node;
1147         (void) entity;
1148         panic("dummy_set_frame_entity() should not be called");
1149 }
1150
1151 static void dummy_set_frame_offset(ir_node *node, int bias)
1152 {
1153         (void) node;
1154         (void) bias;
1155         panic("dummy_set_frame_offset() should not be called");
1156 }
1157
1158 static int dummy_get_sp_bias(const ir_node *node)
1159 {
1160         (void) node;
1161         return 0;
1162 }
1163
1164 /* for "middleend" nodes */
1165 static const arch_irn_ops_t dummy_be_irn_ops = {
1166         dummy_reg_req,
1167         dummy_classify,
1168         dummy_get_frame_entity,
1169         dummy_set_frame_entity,
1170         dummy_set_frame_offset,
1171         dummy_get_sp_bias,
1172         NULL,      /* get_inverse           */
1173         NULL,      /* get_op_estimated_cost */
1174         NULL,      /* possible_memory_operand */
1175         NULL,      /* perform_memory_operand */
1176 };
1177
1178 /*
1179   ____  _     _   ___ ____  _   _   _   _                 _ _
1180  |  _ \| |__ (_) |_ _|  _ \| \ | | | | | | __ _ _ __   __| | | ___ _ __
1181  | |_) | '_ \| |  | || |_) |  \| | | |_| |/ _` | '_ \ / _` | |/ _ \ '__|
1182  |  __/| | | | |  | ||  _ <| |\  | |  _  | (_| | | | | (_| | |  __/ |
1183  |_|   |_| |_|_| |___|_| \_\_| \_| |_| |_|\__,_|_| |_|\__,_|_|\___|_|
1184
1185 */
1186
1187 ir_node *be_new_Phi(ir_node *block, int n_ins, ir_node **ins, ir_mode *mode,
1188                     const arch_register_class_t *cls)
1189 {
1190         struct obstack *obst = be_get_birg_obst(get_irn_irg(block));
1191         backend_info_t *info;
1192
1193         ir_node *phi = new_r_Phi(block, n_ins, ins, mode);
1194         info = be_get_info(phi);
1195         info->out_infos = NEW_ARR_D(reg_out_info_t, obst, 1);
1196         memset(info->out_infos, 0, 1 * sizeof(info->out_infos[0]));
1197         if (cls == NULL) {
1198                 info->out_infos[0].req = arch_no_register_req;
1199         } else {
1200                 info->out_infos[0].req = cls->class_req;
1201         }
1202
1203         return phi;
1204 }
1205
1206 /**
1207  * Guess correct register class of a phi node by looking at its arguments
1208  */
1209 static const arch_register_req_t *get_Phi_reg_req_recursive(const ir_node *phi,
1210                                                             pset **visited)
1211 {
1212         int n = get_irn_arity(phi);
1213         ir_node *op;
1214         int i;
1215
1216         if (*visited && pset_find_ptr(*visited, phi))
1217                 return NULL;
1218
1219         for (i = 0; i < n; ++i) {
1220                 op = get_irn_n(phi, i);
1221                 /* Matze: don't we unnecessary constraint our phis with this?
1222                  * we only need to take the regclass IMO*/
1223                 if (!is_Phi(op))
1224                         return arch_get_register_req_out(op);
1225         }
1226
1227         /*
1228          * The operands of that Phi were all Phis themselves.
1229          * We have to start a DFS for a non-Phi argument now.
1230          */
1231         if (!*visited)
1232                 *visited = pset_new_ptr(16);
1233
1234         pset_insert_ptr(*visited, phi);
1235
1236         for (i = 0; i < n; ++i) {
1237                 const arch_register_req_t *req;
1238                 op = get_irn_n(phi, i);
1239                 req = get_Phi_reg_req_recursive(op, visited);
1240                 if (req != NULL)
1241                         return req;
1242         }
1243
1244         return NULL;
1245 }
1246
1247 static const arch_register_req_t *phi_get_irn_reg_req(const ir_node *node,
1248                                                       int pos)
1249 {
1250         backend_info_t            *info = be_get_info(node);
1251         const arch_register_req_t *req  = info->out_infos[0].req;
1252         (void) pos;
1253
1254         if (req == NULL) {
1255                 if (!mode_is_datab(get_irn_mode(node))) {
1256                         req = arch_no_register_req;
1257                 } else {
1258                         pset *visited = NULL;
1259
1260                         req = get_Phi_reg_req_recursive(node, &visited);
1261                         assert(req->cls != NULL);
1262                         req = req->cls->class_req;
1263
1264                         if (visited != NULL)
1265                                 del_pset(visited);
1266                 }
1267                 info->out_infos[0].req = req;
1268         }
1269
1270         return req;
1271 }
1272
1273 void be_set_phi_reg_req(ir_node *node, const arch_register_req_t *req)
1274 {
1275         backend_info_t *info = be_get_info(node);
1276         info->out_infos[0].req = req;
1277
1278         assert(mode_is_datab(get_irn_mode(node)));
1279 }
1280
1281 int be_dump_phi_reg_reqs(ir_node *node, FILE *F, dump_reason_t reason)
1282 {
1283         switch(reason) {
1284         case dump_node_opcode_txt:
1285                 fputs(get_op_name(get_irn_op(node)), F);
1286                 break;
1287         case dump_node_mode_txt:
1288                 fprintf(F, "%s", get_mode_name(get_irn_mode(node)));
1289                 break;
1290         case dump_node_nodeattr_txt:
1291                 break;
1292         case dump_node_info_txt:
1293         {
1294                 backend_info_t *info = be_get_info(node);
1295                 if (info != NULL && info->out_infos[0].req != NULL) {
1296                         arch_dump_reqs_and_registers(F, node);
1297                 }
1298                 break;
1299         }
1300
1301         default:
1302                 break;
1303         }
1304
1305         return 0;
1306 }
1307
1308 static const arch_irn_ops_t phi_irn_ops = {
1309         phi_get_irn_reg_req,
1310         dummy_classify,
1311         dummy_get_frame_entity,
1312         dummy_set_frame_entity,
1313         dummy_set_frame_offset,
1314         dummy_get_sp_bias,
1315         NULL,    /* get_inverse             */
1316         NULL,    /* get_op_estimated_cost   */
1317         NULL,    /* possible_memory_operand */
1318         NULL,    /* perform_memory_operand  */
1319 };
1320
1321 /*
1322   _   _           _        ____                        _
1323  | \ | | ___   __| | ___  |  _ \ _   _ _ __ ___  _ __ (_)_ __   __ _
1324  |  \| |/ _ \ / _` |/ _ \ | | | | | | | '_ ` _ \| '_ \| | '_ \ / _` |
1325  | |\  | (_) | (_| |  __/ | |_| | |_| | | | | | | |_) | | | | | (_| |
1326  |_| \_|\___/ \__,_|\___| |____/ \__,_|_| |_| |_| .__/|_|_| |_|\__, |
1327                                                 |_|            |___/
1328 */
1329
1330 /**
1331  * ir_op-Operation: dump a be node to file
1332  */
1333 static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason)
1334 {
1335         be_node_attr_t *at = get_irn_attr(irn);
1336
1337         assert(is_be_node(irn));
1338
1339         switch(reason) {
1340                 case dump_node_opcode_txt:
1341                         fputs(get_op_name(get_irn_op(irn)), f);
1342                         break;
1343                 case dump_node_mode_txt:
1344                         if (be_is_Perm(irn) || be_is_Copy(irn) || be_is_CopyKeep(irn)) {
1345                                 fprintf(f, " %s", get_mode_name(get_irn_mode(irn)));
1346                         }
1347                         break;
1348                 case dump_node_nodeattr_txt:
1349                         if (be_is_Call(irn)) {
1350                                 be_call_attr_t *a = (be_call_attr_t *) at;
1351                                 if (a->ent)
1352                                         fprintf(f, " [%s] ", get_entity_name(a->ent));
1353                         }
1354                         if (be_is_IncSP(irn)) {
1355                                 const be_incsp_attr_t *attr = get_irn_generic_attr_const(irn);
1356                                 if (attr->offset == BE_STACK_FRAME_SIZE_EXPAND) {
1357                                         fprintf(f, " [Setup Stackframe] ");
1358                                 } else if (attr->offset == BE_STACK_FRAME_SIZE_SHRINK) {
1359                                         fprintf(f, " [Destroy Stackframe] ");
1360                                 } else {
1361                                         fprintf(f, " [%d] ", attr->offset);
1362                                 }
1363                         }
1364                         break;
1365                 case dump_node_info_txt:
1366                         arch_dump_reqs_and_registers(f, irn);
1367
1368                         if (be_has_frame_entity(irn)) {
1369                                 be_frame_attr_t *a = (be_frame_attr_t *) at;
1370                                 if (a->ent) {
1371                                         unsigned size = get_type_size_bytes(get_entity_type(a->ent));
1372                                         ir_fprintf(f, "frame entity: %+F, offset 0x%x (%d), size 0x%x (%d) bytes\n",
1373                                           a->ent, a->offset, a->offset, size, size);
1374                                 }
1375
1376                         }
1377
1378                         switch (get_irn_opcode(irn)) {
1379                         case beo_IncSP:
1380                                 {
1381                                         be_incsp_attr_t *a = (be_incsp_attr_t *) at;
1382                                         if (a->offset == BE_STACK_FRAME_SIZE_EXPAND)
1383                                                 fprintf(f, "offset: FRAME_SIZE\n");
1384                                         else if (a->offset == BE_STACK_FRAME_SIZE_SHRINK)
1385                                                 fprintf(f, "offset: -FRAME SIZE\n");
1386                                         else
1387                                                 fprintf(f, "offset: %u\n", a->offset);
1388                                 }
1389                                 break;
1390                         case beo_Call:
1391                                 {
1392                                         be_call_attr_t *a = (be_call_attr_t *) at;
1393
1394                                         if (a->ent)
1395                                                 fprintf(f, "\ncalling: %s\n", get_entity_name(a->ent));
1396                                 }
1397                                 break;
1398                         case beo_MemPerm:
1399                                 {
1400                                         int i;
1401                                         for (i = 0; i < be_get_MemPerm_entity_arity(irn); ++i) {
1402                                                 ir_entity *in, *out;
1403                                                 in = be_get_MemPerm_in_entity(irn, i);
1404                                                 out = be_get_MemPerm_out_entity(irn, i);
1405                                                 if (in) {
1406                                                         fprintf(f, "\nin[%d]: %s\n", i, get_entity_name(in));
1407                                                 }
1408                                                 if (out) {
1409                                                         fprintf(f, "\nout[%d]: %s\n", i, get_entity_name(out));
1410                                                 }
1411                                         }
1412                                 }
1413                                 break;
1414
1415                         default:
1416                                 break;
1417                         }
1418         }
1419
1420         return 0;
1421 }
1422
1423 /**
1424  * ir_op-Operation:
1425  * Copies the backend specific attributes from old node to new node.
1426  */
1427 static void copy_attr(const ir_node *old_node, ir_node *new_node)
1428 {
1429         const be_node_attr_t *old_attr = get_irn_attr_const(old_node);
1430         be_node_attr_t *new_attr = get_irn_attr(new_node);
1431         ir_graph       *irg      = get_irn_irg(new_node);
1432         struct obstack *obst     = be_get_birg_obst(irg);
1433         backend_info_t *old_info = be_get_info(old_node);
1434         backend_info_t *new_info = be_get_info(new_node);
1435
1436         assert(is_be_node(old_node));
1437         assert(is_be_node(new_node));
1438
1439         memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
1440
1441         if (old_info->out_infos != NULL) {
1442                 unsigned n_outs = ARR_LEN(old_info->out_infos);
1443                 /* need dyanmic out infos? */
1444                 if (be_is_Barrier(new_node)     || be_is_Perm(new_node)) {
1445                         new_info->out_infos = NEW_ARR_F(reg_out_info_t, n_outs);
1446                 } else {
1447                         new_info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_outs);
1448                 }
1449                 memcpy(new_info->out_infos, old_info->out_infos,
1450                            n_outs * sizeof(new_info->out_infos[0]));
1451         } else {
1452                 new_info->out_infos = NULL;
1453         }
1454
1455         /* input infos */
1456         if (old_attr->reg_data != NULL) {
1457                 unsigned n_ins = ARR_LEN(old_attr->reg_data);
1458                 /* need dynamic in infos? */
1459                 if (get_irn_op(old_node)->opar == oparity_dynamic) {
1460                         new_attr->reg_data = NEW_ARR_F(be_reg_data_t, n_ins);
1461                 } else {
1462                         new_attr->reg_data = NEW_ARR_D(be_reg_data_t, obst, n_ins);
1463                 }
1464                 memcpy(new_attr->reg_data, old_attr->reg_data,
1465                        n_ins * sizeof(be_reg_data_t));
1466         } else {
1467                 new_attr->reg_data = NULL;
1468         }
1469 }
1470
1471 static const ir_op_ops be_node_op_ops = {
1472         firm_default_hash,
1473         NULL,
1474         NULL,
1475         NULL,
1476         NULL,
1477         NULL,
1478         NULL,
1479         NULL,
1480         NULL,
1481         copy_attr,
1482         NULL,
1483         NULL,
1484         NULL,
1485         NULL,
1486         NULL,
1487         dump_node,
1488         NULL,
1489         &be_node_irn_ops
1490 };
1491
1492 int is_be_node(const ir_node *irn)
1493 {
1494         return get_op_ops(get_irn_op(irn))->be_ops == &be_node_irn_ops;
1495 }
1496
1497 void be_init_op(void)
1498 {
1499         ir_opcode opc;
1500
1501         /* Acquire all needed opcodes. */
1502         op_be_Spill      = new_ir_op(beo_Spill,     "be_Spill",     op_pin_state_pinned, N,   oparity_unary,    0, sizeof(be_frame_attr_t),   &be_node_op_ops);
1503         op_be_Reload     = new_ir_op(beo_Reload,    "be_Reload",    op_pin_state_pinned, N,   oparity_zero,     0, sizeof(be_frame_attr_t),   &be_node_op_ops);
1504         op_be_Perm       = new_ir_op(beo_Perm,      "be_Perm",      op_pin_state_pinned, N,   oparity_variable, 0, sizeof(be_node_attr_t),    &be_node_op_ops);
1505         op_be_MemPerm    = new_ir_op(beo_MemPerm,   "be_MemPerm",   op_pin_state_pinned, N,   oparity_variable, 0, sizeof(be_memperm_attr_t), &be_node_op_ops);
1506         op_be_Copy       = new_ir_op(beo_Copy,      "be_Copy",      op_pin_state_floats, N,   oparity_unary,    0, sizeof(be_node_attr_t),    &be_node_op_ops);
1507         op_be_Keep       = new_ir_op(beo_Keep,      "be_Keep",      op_pin_state_floats, K,   oparity_dynamic,  0, sizeof(be_node_attr_t),    &be_node_op_ops);
1508         op_be_CopyKeep   = new_ir_op(beo_CopyKeep,  "be_CopyKeep",  op_pin_state_floats, K,   oparity_variable, 0, sizeof(be_node_attr_t),    &be_node_op_ops);
1509         op_be_Call       = new_ir_op(beo_Call,      "be_Call",      op_pin_state_pinned, F|M, oparity_variable, 0, sizeof(be_call_attr_t),    &be_node_op_ops);
1510         op_be_Return     = new_ir_op(beo_Return,    "be_Return",    op_pin_state_pinned, X,   oparity_dynamic,  0, sizeof(be_return_attr_t),  &be_node_op_ops);
1511         op_be_AddSP      = new_ir_op(beo_AddSP,     "be_AddSP",     op_pin_state_pinned, N,   oparity_unary,    0, sizeof(be_node_attr_t),    &be_node_op_ops);
1512         op_be_SubSP      = new_ir_op(beo_SubSP,     "be_SubSP",     op_pin_state_pinned, N,   oparity_unary,    0, sizeof(be_node_attr_t),    &be_node_op_ops);
1513         op_be_IncSP      = new_ir_op(beo_IncSP,     "be_IncSP",     op_pin_state_pinned, N,   oparity_unary,    0, sizeof(be_incsp_attr_t),   &be_node_op_ops);
1514         op_be_Start      = new_ir_op(beo_Start,     "be_Start",     op_pin_state_pinned, N,   oparity_zero,     0, sizeof(be_node_attr_t),    &be_node_op_ops);
1515         op_be_FrameAddr  = new_ir_op(beo_FrameAddr, "be_FrameAddr", op_pin_state_floats, N,   oparity_unary,    0, sizeof(be_frame_attr_t),   &be_node_op_ops);
1516         op_be_Barrier    = new_ir_op(beo_Barrier,   "be_Barrier",   op_pin_state_pinned, N,   oparity_dynamic,  0, sizeof(be_node_attr_t),    &be_node_op_ops);
1517
1518         op_be_Spill->ops.node_cmp_attr     = FrameAddr_cmp_attr;
1519         op_be_Reload->ops.node_cmp_attr    = FrameAddr_cmp_attr;
1520         op_be_Perm->ops.node_cmp_attr      = node_cmp_attr;
1521         op_be_MemPerm->ops.node_cmp_attr   = node_cmp_attr;
1522         op_be_Copy->ops.node_cmp_attr      = node_cmp_attr;
1523         op_be_Keep->ops.node_cmp_attr      = node_cmp_attr;
1524         op_be_CopyKeep->ops.node_cmp_attr  = node_cmp_attr;
1525         op_be_Call->ops.node_cmp_attr      = Call_cmp_attr;
1526         op_be_Return->ops.node_cmp_attr    = Return_cmp_attr;
1527         op_be_AddSP->ops.node_cmp_attr     = node_cmp_attr;
1528         op_be_SubSP->ops.node_cmp_attr     = node_cmp_attr;
1529         op_be_IncSP->ops.node_cmp_attr     = IncSP_cmp_attr;
1530         op_be_Start->ops.node_cmp_attr     = node_cmp_attr;
1531         op_be_FrameAddr->ops.node_cmp_attr = FrameAddr_cmp_attr;
1532         op_be_Barrier->ops.node_cmp_attr   = node_cmp_attr;
1533
1534         /* attach out dummy_ops to middle end nodes */
1535         for (opc = iro_First; opc <= iro_Last; ++opc) {
1536                 ir_op *op = get_irp_opcode(opc);
1537                 assert(op->ops.be_ops == NULL);
1538                 op->ops.be_ops = &dummy_be_irn_ops;
1539         }
1540
1541         op_Phi->ops.be_ops = &phi_irn_ops;
1542 }