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