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