Removed AddSP in favor of Alloca node
[libfirm] / ir / be / benode.c
1 /**
2  * @file   benode.c
3  * @date   17.05.2005
4  * @author Sebastian Hack
5  *
6  * Backend node support.
7  *
8  * This file provdies Perm, Copy, Spill and Reload nodes.
9  *
10  * Copyright (C) 2005 Universitaet Karlsruhe
11  * Released under the GPL
12  */
13
14 #ifdef HAVE_CONFIG_H
15 #include "config.h"
16 #endif
17
18 #include <stdlib.h>
19
20 #include "obst.h"
21 #include "set.h"
22 #include "pmap.h"
23 #include "util.h"
24 #include "debug.h"
25 #include "fourcc.h"
26 #include "bitfiddle.h"
27
28 #include "irop_t.h"
29 #include "irmode_t.h"
30 #include "irnode_t.h"
31 #include "ircons_t.h"
32 #include "irprintf.h"
33
34 #include "be_t.h"
35 #include "belive_t.h"
36 #include "besched_t.h"
37 #include "benode_t.h"
38
39 #include "beirgmod.h"
40
41 #define OUT_POS(x) (-((x) + 1))
42
43 /* Sometimes we want to put const nodes into get_irn_generic_attr ... */
44 #define get_irn_attr(irn) get_irn_generic_attr((ir_node *) (irn))
45
46 static unsigned be_node_tag = FOURCC('B', 'E', 'N', 'O');
47
48 #if 0
49 typedef enum _node_kind_t {
50         node_kind_spill,
51         node_kind_reload,
52         node_kind_perm,
53         node_kind_copy,
54         node_kind_kill,
55         node_kind_last
56 } node_kind_t;
57 #endif
58
59 typedef enum {
60         be_req_kind_old_limited,
61         be_req_kind_negate_old_limited,
62         be_req_kind_single_reg
63 } be_req_kind_t;
64
65 typedef struct {
66         arch_register_req_t req;
67         be_req_kind_t       kind;
68         arch_irn_flags_t    flags;
69         union {
70                 struct {
71                         void (*old_limited)(void *ptr, bitset_t *bs);
72                         void *old_limited_env;
73                 } old_limited;
74
75                 const arch_register_t *single_reg;
76         } x;
77 } be_req_t;
78
79 typedef struct {
80         const arch_register_t *reg;
81         be_req_t              req;
82         be_req_t              in_req;
83 } be_reg_data_t;
84
85 typedef struct {
86         int                         max_reg_data;
87         be_reg_data_t               *reg_data;
88 } be_node_attr_t;
89
90 typedef struct {
91         be_node_attr_t node_attr;
92         int offset;           /**< The offset by which the stack shall be increased/decreased. */
93         be_stack_dir_t dir;   /**< The direction in which the stack shall be modified (along or in the other direction). */
94 } be_stack_attr_t;
95
96 typedef struct {
97         be_node_attr_t node_attr;
98         entity *ent;
99         int offset;
100 } be_frame_attr_t;
101
102 typedef struct {
103         be_node_attr_t node_attr;
104         entity *ent;
105 } be_call_attr_t;
106
107 typedef struct {
108         be_frame_attr_t frame_attr;
109         ir_node *spill_ctx;  /**< The node in whose context this spill was introduced. */
110 } be_spill_attr_t;
111
112 ir_op *op_be_Spill;
113 ir_op *op_be_Reload;
114 ir_op *op_be_Perm;
115 ir_op *op_be_Copy;
116 ir_op *op_be_Keep;
117 ir_op *op_be_Call;
118 ir_op *op_be_Return;
119 ir_op *op_be_IncSP;
120 ir_op *op_be_Alloca;
121 ir_op *op_be_SetSP;
122 ir_op *op_be_RegParams;
123 ir_op *op_be_StackParam;
124 ir_op *op_be_FrameAddr;
125 ir_op *op_be_FrameLoad;
126 ir_op *op_be_FrameStore;
127
128 static int beo_base = -1;
129
130 static const ir_op_ops be_node_op_ops;
131
132 #define N   irop_flag_none
133 #define L   irop_flag_labeled
134 #define C   irop_flag_commutative
135 #define X   irop_flag_cfopcode
136 #define I   irop_flag_ip_cfopcode
137 #define F   irop_flag_fragile
138 #define Y   irop_flag_forking
139 #define H   irop_flag_highlevel
140 #define c   irop_flag_constlike
141 #define K   irop_flag_keep
142
143 void be_node_init(void) {
144         static int inited = 0;
145
146         if(inited)
147                 return;
148
149         inited = 1;
150
151         /* Acquire all needed opcodes. */
152         beo_base = get_next_ir_opcodes(beo_Last - 1);
153
154         op_be_Spill      = new_ir_op(beo_base + beo_Spill,      "Spill",      op_pin_state_mem_pinned, N, oparity_unary,    0, sizeof(be_spill_attr_t), &be_node_op_ops);
155         op_be_Reload     = new_ir_op(beo_base + beo_Reload,     "Reload",     op_pin_state_mem_pinned, N, oparity_zero,     0, sizeof(be_frame_attr_t), &be_node_op_ops);
156         op_be_Perm       = new_ir_op(beo_base + beo_Perm,       "Perm",       op_pin_state_pinned,     N, oparity_variable, 0, sizeof(be_node_attr_t),  &be_node_op_ops);
157         op_be_Copy       = new_ir_op(beo_base + beo_Copy,       "Copy",       op_pin_state_floats,     N, oparity_unary,    0, sizeof(be_node_attr_t),  &be_node_op_ops);
158         op_be_Keep       = new_ir_op(beo_base + beo_Keep,       "Keep",       op_pin_state_pinned,     K, oparity_variable, 0, sizeof(be_node_attr_t),  &be_node_op_ops);
159         op_be_Call       = new_ir_op(beo_base + beo_Call,       "Call",       op_pin_state_pinned,     N, oparity_variable, 0, sizeof(be_node_attr_t),  &be_node_op_ops);
160         op_be_Return     = new_ir_op(beo_base + beo_Return,     "Return",     op_pin_state_pinned,     X, oparity_variable, 0, sizeof(be_node_attr_t),  &be_node_op_ops);
161         op_be_Alloca     = new_ir_op(beo_base + beo_Alloca,     "Alloca",     op_pin_state_pinned,     N, oparity_unary,    0, sizeof(be_node_attr_t),  &be_node_op_ops);
162         op_be_SetSP      = new_ir_op(beo_base + beo_SetSP,      "SetSP",      op_pin_state_pinned,     N, oparity_binary,   0, sizeof(be_stack_attr_t), &be_node_op_ops);
163         op_be_IncSP      = new_ir_op(beo_base + beo_IncSP,      "IncSP",      op_pin_state_pinned,     N, oparity_binary,   0, sizeof(be_stack_attr_t), &be_node_op_ops);
164         op_be_RegParams  = new_ir_op(beo_base + beo_RegParams,  "RegParams",  op_pin_state_pinned,     N, oparity_zero,     0, sizeof(be_node_attr_t),  &be_node_op_ops);
165         op_be_StackParam = new_ir_op(beo_base + beo_StackParam, "StackParam", op_pin_state_pinned,     N, oparity_unary,    0, sizeof(be_frame_attr_t), &be_node_op_ops);
166         op_be_FrameAddr  = new_ir_op(beo_base + beo_FrameAddr,  "FrameAddr",  op_pin_state_pinned,     N, oparity_binary,   0, sizeof(be_frame_attr_t), &be_node_op_ops);
167         op_be_FrameLoad  = new_ir_op(beo_base + beo_FrameLoad,  "FrameLoad",  op_pin_state_pinned,     N, oparity_any,      0, sizeof(be_frame_attr_t), &be_node_op_ops);
168         op_be_FrameStore = new_ir_op(beo_base + beo_FrameStore, "FrameStore", op_pin_state_pinned,     N, oparity_any,      0, sizeof(be_frame_attr_t), &be_node_op_ops);
169
170         set_op_tag(op_be_Spill,      &be_node_tag);
171         set_op_tag(op_be_Reload,     &be_node_tag);
172         set_op_tag(op_be_Perm,       &be_node_tag);
173         set_op_tag(op_be_Copy,       &be_node_tag);
174         set_op_tag(op_be_Keep,       &be_node_tag);
175         set_op_tag(op_be_Call,       &be_node_tag);
176         set_op_tag(op_be_Return,     &be_node_tag);
177         set_op_tag(op_be_Alloca,     &be_node_tag);
178         set_op_tag(op_be_SetSP,      &be_node_tag);
179         set_op_tag(op_be_IncSP,      &be_node_tag);
180         set_op_tag(op_be_RegParams,  &be_node_tag);
181         set_op_tag(op_be_StackParam, &be_node_tag);
182         set_op_tag(op_be_FrameLoad,  &be_node_tag);
183         set_op_tag(op_be_FrameStore, &be_node_tag);
184         set_op_tag(op_be_FrameAddr,  &be_node_tag);
185 }
186
187 static void *init_node_attr(ir_node* irn, int max_reg_data)
188 {
189         ir_graph *irg     = get_irn_irg(irn);
190         be_node_attr_t *a = get_irn_attr(irn);
191
192         memset(a, 0, sizeof(get_op_attr_size(get_irn_op(irn))));
193         a->max_reg_data = max_reg_data;
194         a->reg_data     = NULL;
195
196         if(max_reg_data > 0) {
197                 int i;
198
199                 a->reg_data = NEW_ARR_D(be_reg_data_t, get_irg_obstack(irg), max_reg_data);
200                 memset(a->reg_data, 0, max_reg_data * sizeof(a->reg_data[0]));
201                 for(i = 0; i < max_reg_data; ++i) {
202                         a->reg_data[i].req.req.cls  = NULL;
203                         a->reg_data[i].req.req.type = arch_register_req_type_none;
204                 }
205         }
206
207         return a;
208 }
209
210 static INLINE int is_be_node(const ir_node *irn)
211 {
212         return get_op_tag(get_irn_op(irn)) == &be_node_tag;
213 }
214
215 be_opcode_t be_get_irn_opcode(const ir_node *irn)
216 {
217         return is_be_node(irn) ? get_irn_opcode(irn) - beo_base : beo_NoBeOp;
218 }
219
220 static int redir_proj(const ir_node **node, int pos)
221 {
222         const ir_node *n = *node;
223
224         if(is_Proj(n)) {
225                 ir_node *irn;
226
227                 assert(pos == -1 && "Illegal pos for a Proj");
228                 *node = irn = get_Proj_pred(n);
229                 if(is_Proj(irn)) {
230                         assert(get_irn_mode(irn) == mode_T);
231                         *node = get_Proj_pred(irn);
232                 }
233                 return get_Proj_proj(n);
234         }
235
236         return 0;
237 }
238
239 static void
240 be_node_set_irn_reg(const void *_self, ir_node *irn, const arch_register_t *reg)
241 {
242         int out_pos;
243         be_node_attr_t *a;
244
245         out_pos = redir_proj((const ir_node **) &irn, -1);
246         a       = get_irn_attr(irn);
247
248         assert(is_be_node(irn));
249         assert(out_pos < a->max_reg_data && "position too high");
250         a->reg_data[out_pos].reg = reg;
251 }
252
253
254 ir_node *be_new_Spill(const arch_register_class_t *cls, const arch_register_class_t *cls_frame, ir_graph *irg, ir_node *bl, ir_node *frame, ir_node *to_spill, ir_node *ctx)
255 {
256         be_spill_attr_t *a;
257         ir_node *in[2];
258         ir_node *res;
259
260         in[0] = frame;
261         in[1] = to_spill;
262         res   = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
263         a     = init_node_attr(res, 2);
264         a->frame_attr.ent = NULL;
265         a->frame_attr.offset = 0;
266         a->spill_ctx = ctx;
267
268         be_node_set_reg_class(res, 0, cls_frame);
269         be_node_set_reg_class(res, 1, cls);
270         return res;
271 }
272
273 ir_node *be_new_Reload(const arch_register_class_t *cls, const arch_register_class_t *cls_frame, ir_graph *irg, ir_node *bl, ir_node *frame, ir_node *mem, ir_mode *mode)
274 {
275         ir_node *in[2];
276         ir_node *res;
277
278         in[0] = frame;
279         in[1] = mem;
280         res   = new_ir_node(NULL, irg, bl, op_be_Reload, mode, 2, in);
281         init_node_attr(res, 2);
282         be_node_set_reg_class(res, 0, cls_frame);
283         be_node_set_reg_class(res, -1, cls);
284         return res;
285 }
286
287 ir_node *(be_get_Reload_mem)(const ir_node *irn)
288 {
289         assert(be_is_Reload(irn));
290         return get_irn_n(irn, 1);
291 }
292
293 ir_node *be_new_Perm(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
294 {
295         int i;
296         ir_node *irn = new_ir_node(NULL, irg, bl, op_be_Perm, mode_T, n, in);
297         init_node_attr(irn, n);
298         for(i = 0; i < n; ++i) {
299                 be_node_set_reg_class(irn, i, cls);
300                 be_node_set_reg_class(irn, OUT_POS(i), cls);
301         }
302
303         return irn;
304 }
305
306 ir_node *be_new_Copy(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *op)
307 {
308         ir_node *in[1];
309         ir_node *res;
310
311         in[0] = op;
312         res   = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
313         init_node_attr(res, 1);
314         be_node_set_reg_class(res, 0, cls);
315         be_node_set_reg_class(res, OUT_POS(0), cls);
316         return res;
317 }
318
319 ir_node *be_new_Keep(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
320 {
321         int i;
322         ir_node *irn;
323
324         irn = new_ir_node(NULL, irg, bl, op_be_Keep, mode_ANY, n, in);
325         init_node_attr(irn, n);
326         for(i = 0; i < n; ++i) {
327                 be_node_set_reg_class(irn, i, cls);
328         }
329         keep_alive(irn);
330         return irn;
331 }
332
333 ir_node *be_new_Call(ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *sp, ir_node *ptr, int n_outs, int n, ir_node *in[])
334 {
335         int real_n = 3 + n;
336         ir_node *irn;
337         ir_node **real_in;
338
339         real_in = malloc(sizeof(real_in[0]) * (real_n));
340
341         real_in[0] = mem;
342         real_in[1] = sp;
343         real_in[2] = ptr;
344         memcpy(&real_in[3], in, n * sizeof(in[0]));
345
346         irn = new_ir_node(NULL, irg, bl, op_be_Call, mode_T, real_n, real_in);
347         init_node_attr(irn, (n_outs > real_n ? n_outs : real_n));
348         return irn;
349 }
350
351 entity *be_Call_get_entity(const ir_node *call)
352 {
353         be_call_attr_t *a = get_irn_attr(call);
354         assert(be_is_Call(call));
355         return a->ent;
356 }
357
358 void    be_Call_set_entity(ir_node *call, entity *ent)
359 {
360         be_call_attr_t *a = get_irn_attr(call);
361         assert(be_is_Call(call));
362         a->ent = ent;
363 }
364
365 ir_node *be_new_Return(ir_graph *irg, ir_node *bl, int n, ir_node *in[])
366 {
367         ir_node *irn = new_ir_node(NULL, irg, bl, op_be_Return, mode_X, n, in);
368         init_node_attr(irn, n);
369
370         return irn;
371 }
372
373 ir_node *be_new_IncSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, ir_node *mem, unsigned offset, be_stack_dir_t dir)
374 {
375         be_stack_attr_t *a;
376         ir_node *irn;
377         ir_node *in[1];
378
379         in[0]     = old_sp;
380         in[1]     = mem;
381         irn       = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode, 2, in);
382         a         = init_node_attr(irn, 1);
383         a->dir    = dir;
384         a->offset = offset;
385
386         be_node_set_flags(irn, -1, arch_irn_flags_ignore);
387
388         /* Set output constraint to stack register. */
389         be_node_set_reg_class(irn, 0, sp->reg_class);
390         be_set_constr_single_reg(irn, -1, sp);
391         be_node_set_irn_reg(NULL, irn, sp);
392
393         return irn;
394 }
395
396 ir_node *be_new_Alloca(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *old_sp, ir_node *sz)
397 {
398         be_node_attr_t *a;
399         ir_node *irn;
400         ir_node *in[3];
401
402         in[0]    = mem;
403         in[1]    = old_sp;
404         in[2]    = sz;
405         irn      = new_ir_node(NULL, irg, bl, op_be_Alloca, mode_T, 3, in);
406         a        = init_node_attr(irn, 3);
407
408         be_node_set_flags(irn, OUT_POS(pn_Alloc_res), arch_irn_flags_ignore);
409
410         /* Set output constraint to stack register. */
411         be_set_constr_single_reg(irn, OUT_POS(pn_Alloc_res), sp);
412
413         return irn;
414 }
415
416 ir_node *be_new_SetSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, ir_node *op, ir_node *mem)
417 {
418         be_node_attr_t *a;
419         ir_node *irn;
420         ir_node *in[3];
421
422         in[0]    = mem;
423         in[1]    = old_sp;
424         in[2]    = op;
425         irn      = new_ir_node(NULL, irg, bl, op_be_SetSP, get_irn_mode(old_sp), 3, in);
426         a        = init_node_attr(irn, 3);
427
428         be_node_set_flags(irn, OUT_POS(0), arch_irn_flags_ignore);
429
430         /* Set output constraint to stack register. */
431         be_set_constr_single_reg(irn, OUT_POS(0), sp);
432         be_node_set_reg_class(irn, 1, sp->reg_class);
433         be_node_set_reg_class(irn, 2, sp->reg_class);
434         be_node_set_irn_reg(NULL, irn, sp);
435
436         return irn;
437 }
438
439 ir_node *be_new_StackParam(const arch_register_class_t *cls, const arch_register_class_t *cls_frame, ir_graph *irg, ir_node *bl, ir_mode *mode, ir_node *frame_pointer, entity *ent)
440 {
441         be_frame_attr_t *a;
442         ir_node *irn;
443         ir_node *in[1];
444
445         in[0] = frame_pointer;
446         irn = new_ir_node(NULL, irg, bl, op_be_StackParam, mode, 1, in);
447         a = init_node_attr(irn, 1);
448         a->ent = ent;
449
450         be_node_set_reg_class(irn, 0, cls_frame);
451         be_node_set_reg_class(irn, OUT_POS(0), cls);
452         return irn;
453 }
454
455 ir_node *be_new_RegParams(ir_graph *irg, ir_node *bl, int n_outs)
456 {
457         ir_node *irn;
458         ir_node *in[1];
459
460         irn = new_ir_node(NULL, irg, bl, op_be_RegParams, mode_T, 0, in);
461         init_node_attr(irn, n_outs);
462         return irn;
463 }
464
465 ir_node *be_new_FrameLoad(const arch_register_class_t *cls_frame, const arch_register_class_t *cls_data,
466                                                   ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *frame, entity *ent)
467 {
468         be_frame_attr_t *a;
469         ir_node *irn;
470         ir_node *in[2];
471
472         in[0]  = mem;
473         in[1]  = frame;
474         irn    = new_ir_node(NULL, irg, bl, op_be_FrameLoad, mode_T, 2, in);
475         a      = init_node_attr(irn, 3);
476         a->ent = ent;
477         a->offset = 0;
478         be_node_set_reg_class(irn, 1, cls_frame);
479         be_node_set_reg_class(irn, OUT_POS(pn_Load_res), cls_data);
480         return irn;
481 }
482
483 ir_node *be_new_FrameStore(const arch_register_class_t *cls_frame, const arch_register_class_t *cls_data,
484                                                    ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *frame, ir_node *data, entity *ent)
485 {
486         be_frame_attr_t *a;
487         ir_node *irn;
488         ir_node *in[3];
489
490         in[0]  = mem;
491         in[1]  = frame;
492         in[2]  = data;
493         irn    = new_ir_node(NULL, irg, bl, op_be_FrameStore, mode_T, 3, in);
494         a      = init_node_attr(irn, 3);
495         a->ent = ent;
496         a->offset = 0;
497         be_node_set_reg_class(irn, 1, cls_frame);
498         be_node_set_reg_class(irn, 2, cls_data);
499         return irn;
500 }
501
502 ir_node *be_new_FrameAddr(const arch_register_class_t *cls_frame, ir_graph *irg, ir_node *bl, ir_node *frame, entity *ent)
503 {
504         be_frame_attr_t *a;
505         ir_node *irn;
506         ir_node *in[1];
507
508         in[0]  = frame;
509         irn    = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
510         a      = init_node_attr(irn, 1);
511         a->ent = ent;
512         a->offset = 0;
513         be_node_set_reg_class(irn, 0, cls_frame);
514         be_node_set_reg_class(irn, OUT_POS(0), cls_frame);
515         return irn;
516 }
517
518 int be_is_Spill         (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Spill          ; }
519 int be_is_Reload        (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Reload         ; }
520 int be_is_Copy          (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Copy           ; }
521 int be_is_Perm          (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Perm           ; }
522 int be_is_Keep          (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Keep           ; }
523 int be_is_Call          (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Call           ; }
524 int be_is_Return        (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Return         ; }
525 int be_is_IncSP         (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_IncSP          ; }
526 int be_is_SetSP         (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_SetSP          ; }
527 int be_is_Alloca        (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Alloca         ; }
528 int be_is_RegParams     (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_RegParams      ; }
529 int be_is_StackParam    (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_StackParam     ; }
530 int be_is_FrameAddr     (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameAddr      ; }
531 int be_is_FrameLoad     (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameLoad      ; }
532 int be_is_FrameStore    (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameStore     ; }
533
534 int be_has_frame_entity(const ir_node *irn)
535 {
536         switch(be_get_irn_opcode(irn)) {
537         case beo_StackParam:
538         case beo_Spill:
539         case beo_Reload:
540         case beo_FrameStore:
541         case beo_FrameLoad:
542         case beo_FrameAddr:
543                 return 1;
544         }
545
546         return 0;
547 }
548
549 entity *be_get_frame_entity(const ir_node *irn)
550 {
551         if(be_has_frame_entity(irn)) {
552                 be_frame_attr_t *a = get_irn_attr(irn);
553                 return a->ent;
554         }
555         return NULL;
556 }
557
558 static void be_limited(void *data, bitset_t *bs)
559 {
560         be_req_t *req = data;
561
562         switch(req->kind) {
563         case be_req_kind_negate_old_limited:
564         case be_req_kind_old_limited:
565                 req->x.old_limited.old_limited(req->x.old_limited.old_limited_env, bs);
566                 if(req->kind == be_req_kind_negate_old_limited)
567                         bitset_flip_all(bs);
568                 break;
569         case be_req_kind_single_reg:
570                 bitset_clear_all(bs);
571                 bitset_set(bs, req->x.single_reg->index);
572                 break;
573         }
574 }
575
576 static INLINE be_req_t *get_req(ir_node *irn, int pos)
577 {
578         int idx           = pos < 0 ? -(pos + 1) : pos;
579         be_node_attr_t *a = get_irn_attr(irn);
580         be_reg_data_t *rd = &a->reg_data[idx];
581         be_req_t       *r = pos < 0 ? &rd->req : &rd->in_req;
582
583         assert(is_be_node(irn));
584         assert(!(pos >= 0) || pos < get_irn_arity(irn));
585         assert(!(pos < 0)  || -(pos + 1) <= a->max_reg_data);
586
587         return r;
588 }
589
590 void be_set_constr_single_reg(ir_node *irn, int pos, const arch_register_t *reg)
591 {
592         be_req_t *r = get_req(irn, pos);
593
594         r->kind            = be_req_kind_single_reg;
595         r->x.single_reg    = reg;
596         r->req.limited     = be_limited;
597         r->req.limited_env = r;
598         r->req.type        = arch_register_req_type_limited;
599         r->req.cls         = reg->reg_class;
600 }
601
602 void be_set_constr_limited(ir_node *irn, int pos, const arch_register_req_t *req)
603 {
604         be_req_t *r = get_req(irn, pos);
605
606         assert(arch_register_req_is(req, limited));
607
608         r->kind            = be_req_kind_old_limited;
609         r->req.limited     = be_limited;
610         r->req.limited_env = r;
611         r->req.type        = arch_register_req_type_limited;
612         r->req.cls         = req->cls;
613
614         r->x.old_limited.old_limited     = req->limited;
615         r->x.old_limited.old_limited_env = req->limited_env;
616 }
617
618 void be_node_set_flags(ir_node *irn, int pos, arch_irn_flags_t flags)
619 {
620         be_req_t *r = get_req(irn, pos);
621         r->flags = flags;
622 }
623
624 void be_node_set_reg_class(ir_node *irn, int pos, const arch_register_class_t *cls)
625 {
626         be_req_t *r = get_req(irn, pos);
627         r->req.cls = cls;
628         if(r->req.type == arch_register_req_type_none)
629                 r->req.type = arch_register_req_type_normal;
630 }
631
632 void be_set_IncSP_offset(ir_node *irn, unsigned offset)
633 {
634         be_stack_attr_t *a = get_irn_attr(irn);
635         assert(be_is_IncSP(irn));
636         a->offset = offset;
637 }
638
639 unsigned be_get_IncSP_offset(const ir_node *irn)
640 {
641         be_stack_attr_t *a = get_irn_attr(irn);
642         assert(be_is_IncSP(irn));
643         return a->offset;
644 }
645
646 void be_set_IncSP_direction(ir_node *irn, be_stack_dir_t dir)
647 {
648         be_stack_attr_t *a = get_irn_attr(irn);
649         assert(be_is_IncSP(irn));
650         a->dir = dir;
651 }
652
653 be_stack_dir_t be_get_IncSP_direction(const ir_node *irn)
654 {
655         be_stack_attr_t *a = get_irn_attr(irn);
656         assert(be_is_IncSP(irn));
657         return a->dir;
658 }
659
660 void be_set_Spill_entity(ir_node *irn, entity *ent)
661 {
662         be_spill_attr_t *a = get_irn_attr(irn);
663         assert(be_is_Spill(irn));
664         a->frame_attr.ent = ent;
665 }
666
667 static ir_node *find_a_spill_walker(ir_node *irn, unsigned visited_nr)
668 {
669         unsigned nr = get_irn_visited(irn);
670
671         set_irn_visited(irn, visited_nr);
672
673         if(is_Phi(irn)) {
674                 int i, n;
675                 if(nr < visited_nr) {
676                         for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
677                                 ir_node *n = find_a_spill_walker(get_irn_n(irn, i), visited_nr);
678                                 if(n != NULL)
679                                         return n;
680                         }
681                 }
682         }
683
684         else if(be_get_irn_opcode(irn) == beo_Spill)
685                 return irn;
686
687         return NULL;
688 }
689
690 ir_node *be_get_Spill_context(const ir_node *irn) {
691         const be_spill_attr_t *a = get_irn_attr(irn);
692         assert(be_is_Spill(irn));
693         return a->spill_ctx;
694 }
695
696 /**
697  * Finds a spill for a reload.
698  * If the reload is directly using the spill, this is simple,
699  * else we perform DFS from the reload (over all PhiMs) and return
700  * the first spill node we find.
701  */
702 static INLINE ir_node *find_a_spill(const ir_node *irn)
703 {
704         ir_graph *irg       = get_irn_irg(irn);
705         unsigned visited_nr = get_irg_visited(irg) + 1;
706
707         assert(be_is_Reload(irn));
708         set_irg_visited(irg, visited_nr);
709         return find_a_spill_walker(be_get_Reload_mem(irn), visited_nr);
710 }
711
712 entity *be_get_spill_entity(const ir_node *irn)
713 {
714         int opc           = get_irn_opcode(irn);
715
716         switch(be_get_irn_opcode(irn)) {
717         case beo_Reload:
718                 {
719                         ir_node *spill = find_a_spill(irn);
720                         return be_get_spill_entity(spill);
721                 }
722         case beo_Spill:
723                 {
724                         be_spill_attr_t *a = get_irn_attr(irn);
725                         return a->frame_attr.ent;
726                 }
727         default:
728                 assert(0 && "Must give spill/reload node");
729                 break;
730         }
731
732         return NULL;
733 }
734
735 static void entity_copy_walker(ir_node *irn, void *data)
736 {
737         if(be_is_Reload(irn)) {
738                 be_frame_attr_t *a = get_irn_attr(irn);
739                 entity *ent        = be_get_spill_entity(irn);
740
741                 a->ent = ent;
742         }
743 }
744
745 void be_copy_entities_to_reloads(ir_graph *irg)
746 {
747         irg_walk_graph(irg, entity_copy_walker, NULL, NULL);
748 }
749
750 ir_node *be_spill(const arch_env_t *arch_env, ir_node *irn, ir_node *ctx)
751 {
752         ir_node *bl    = get_nodes_block(irn);
753         ir_graph *irg  = get_irn_irg(bl);
754         ir_node *frame = get_irg_frame(irg);
755         ir_node *spill;
756         ir_node *insert;
757
758         const arch_register_class_t *cls       = arch_get_irn_reg_class(arch_env, irn, -1);
759         const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
760
761         spill = be_new_Spill(cls, cls_frame, irg, bl, frame, irn, ctx);
762
763         /*
764          * search the right insertion point. a spill of a phi cannot be put
765          * directly after the phi, if there are some phis behind the one which
766          * is spilled. Also, a spill of a Proj must be after all Projs of the
767          * same tuple node.
768          */
769         insert = sched_next(irn);
770         while((is_Phi(insert) || is_Proj(insert)) && !sched_is_end(insert))
771                 insert = sched_next(insert);
772
773         /*
774          * Here's one special case:
775          * If the spill is in the start block, the spill must be after the frame
776          * pointer is set up. This is checked here and fixed.
777          */
778         if(bl == get_irg_start_block(irg))
779                 insert = sched_next(frame);
780
781         sched_add_before(insert, spill);
782         return spill;
783 }
784
785 ir_node *be_reload(const arch_env_t *arch_env, const arch_register_class_t *cls, ir_node *reloader, ir_mode *mode, ir_node *spill)
786 {
787         ir_node *reload;
788
789         ir_node *bl    = is_Block(reloader) ? reloader : get_nodes_block(reloader);
790         ir_graph *irg  = get_irn_irg(bl);
791         ir_node *frame = get_irg_frame(irg);
792         const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
793
794         assert(be_is_Spill(spill) || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
795
796         reload = be_new_Reload(cls, cls_frame, irg, bl, frame, spill, mode);
797
798         sched_add_before(reloader, reload);
799         return reload;
800 }
801
802 static void *put_out_reg_req(arch_register_req_t *req, const ir_node *irn, int out_pos)
803 {
804         const be_node_attr_t *a = get_irn_attr(irn);
805
806         if(out_pos < a->max_reg_data)
807                 memcpy(req, &a->reg_data[out_pos].req, sizeof(req[0]));
808         else {
809                 req->type = arch_register_req_type_none;
810                 req->cls  = NULL;
811         }
812
813         return req;
814 }
815
816 static void *put_in_reg_req(arch_register_req_t *req, const ir_node *irn, int pos)
817 {
818         const be_node_attr_t *a = get_irn_attr(irn);
819         int n                   = get_irn_arity(irn);
820
821         if(pos < get_irn_arity(irn) && pos < a->max_reg_data)
822                 memcpy(req, &a->reg_data[pos].in_req, sizeof(req[0]));
823         else {
824                 req->type = arch_register_req_type_none;
825                 req->cls  = NULL;
826         }
827
828         return req;
829 }
830
831 static const arch_register_req_t *
832 be_node_get_irn_reg_req(const void *self, arch_register_req_t *req, const ir_node *irn, int pos)
833 {
834         int out_pos = pos;
835
836         if(pos < 0) {
837                 if(get_irn_mode(irn) == mode_T)
838                         return NULL;
839
840                 out_pos = redir_proj((const ir_node **) &irn, pos);
841                 assert(is_be_node(irn));
842                 return put_out_reg_req(req, irn, out_pos);
843         }
844
845         else {
846                 return is_be_node(irn) ? put_in_reg_req(req, irn, pos) : NULL;
847         }
848
849         return req;
850 }
851
852 const arch_register_t *
853 be_node_get_irn_reg(const void *_self, const ir_node *irn)
854 {
855         int out_pos;
856         be_node_attr_t *a;
857
858         out_pos = redir_proj((const ir_node **) &irn, -1);
859         a       = get_irn_attr(irn);
860
861         assert(is_be_node(irn));
862         assert(out_pos < a->max_reg_data && "position too high");
863
864         return a->reg_data[out_pos].reg;
865 }
866
867 static arch_irn_class_t be_node_classify(const void *_self, const ir_node *irn)
868 {
869         redir_proj((const ir_node **) &irn, -1);
870
871         switch(be_get_irn_opcode(irn)) {
872 #define XXX(a,b) case beo_ ## a: return arch_irn_class_ ## b;
873                 XXX(Spill, spill)
874                 XXX(Reload, reload)
875                 XXX(Perm, perm)
876                 XXX(Copy, copy)
877 #undef XXX
878                 default:
879                 return 0;
880         }
881
882         return 0;
883 }
884
885 static arch_irn_flags_t be_node_get_flags(const void *_self, const ir_node *irn)
886 {
887         int out_pos;
888         be_node_attr_t *a;
889
890         out_pos = redir_proj((const ir_node **) &irn, -1);
891         a       = get_irn_attr(irn);
892
893         assert(is_be_node(irn));
894         assert(out_pos < a->max_reg_data && "position too high");
895
896         return a->reg_data[out_pos].req.flags;
897 }
898
899 static entity *be_node_get_frame_entity(const void *self, const ir_node *irn)
900 {
901         return be_get_frame_entity(irn);
902 }
903
904 static void be_node_set_frame_offset(const void *self, ir_node *irn, int offset)
905 {
906         if(be_has_frame_entity(irn)) {
907                 be_frame_attr_t *a = get_irn_attr(irn);
908                 a->offset = offset;
909         }
910 }
911
912
913 static const arch_irn_ops_if_t be_node_irn_ops_if = {
914         be_node_get_irn_reg_req,
915         be_node_set_irn_reg,
916         be_node_get_irn_reg,
917         be_node_classify,
918         be_node_get_flags,
919         be_node_get_frame_entity,
920         be_node_set_frame_offset
921 };
922
923 static const arch_irn_ops_t be_node_irn_ops = {
924         &be_node_irn_ops_if
925 };
926
927 const void *be_node_get_arch_ops(const arch_irn_handler_t *self, const ir_node *irn)
928 {
929         redir_proj((const ir_node **) &irn, -1);
930         return is_be_node(irn) ? &be_node_irn_ops : NULL;
931 }
932
933 const arch_irn_handler_t be_node_irn_handler = {
934         be_node_get_arch_ops
935 };
936
937
938 static void dump_node_req(FILE *f, be_req_t *req)
939 {
940         unsigned i;
941         int did_something = 0;
942         const char *suffix = "";
943
944         if(req->flags != arch_irn_flags_none) {
945                 fprintf(f, "flags: ");
946                 for(i = arch_irn_flags_none; i <= log2_ceil(arch_irn_flags_last); ++i) {
947                         if(req->flags & (1 << i)) {
948                                 fprintf(f, "%s%s", suffix, arch_irn_flag_str(1 << i));
949                                 suffix = "|";
950                         }
951                 }
952                 suffix = ", ";
953                 did_something = 1;
954         }
955
956         if(req->req.cls != 0) {
957                 char tmp[256];
958                 fprintf(f, suffix);
959                 arch_register_req_format(tmp, sizeof(tmp), &req->req);
960                 fprintf(f, "%s", tmp);
961                 did_something = 1;
962         }
963
964         if(did_something)
965                 fprintf(f, "\n");
966 }
967
968 static void dump_node_reqs(FILE *f, ir_node *irn)
969 {
970         int i;
971         be_node_attr_t *a = get_irn_attr(irn);
972
973         fprintf(f, "registers: \n");
974         for(i = 0; i < a->max_reg_data; ++i) {
975                 be_reg_data_t *rd = &a->reg_data[i];
976                 if(rd->reg)
977                         fprintf(f, "#%d: %s\n", i, rd->reg->name);
978         }
979
980         fprintf(f, "in requirements\n");
981         for(i = 0; i < a->max_reg_data; ++i) {
982                 dump_node_req(f, &a->reg_data[i].in_req);
983         }
984
985         fprintf(f, "\nout requirements\n");
986         for(i = 0; i < a->max_reg_data; ++i) {
987                 dump_node_req(f, &a->reg_data[i].req);
988         }
989 }
990
991 static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason)
992 {
993         be_node_attr_t *at = get_irn_attr(irn);
994
995         assert(is_be_node(irn));
996
997         switch(reason) {
998                 case dump_node_opcode_txt:
999                         fprintf(f, get_op_name(get_irn_op(irn)));
1000                         break;
1001                 case dump_node_mode_txt:
1002                         fprintf(f, get_mode_name(get_irn_mode(irn)));
1003                         break;
1004                 case dump_node_nodeattr_txt:
1005                         break;
1006                 case dump_node_info_txt:
1007                         dump_node_reqs(f, irn);
1008
1009                         if(be_has_frame_entity(irn)) {
1010                                 be_frame_attr_t *a = (be_frame_attr_t *) at;
1011                                 if (a->ent)
1012                                         ir_fprintf(f, "frame entity: %+F offset %x (%d)\n", a->ent, a->offset, a->offset);
1013
1014                         }
1015
1016                         switch(be_get_irn_opcode(irn)) {
1017                         case beo_Spill:
1018                                 {
1019                                         be_spill_attr_t *a = (be_spill_attr_t *) at;
1020                                         ir_fprintf(f, "spill context: %+F\n", a->spill_ctx);
1021                                 }
1022                                 break;
1023
1024                         case beo_IncSP:
1025                                 {
1026                                         be_stack_attr_t *a = (be_stack_attr_t *) at;
1027                                         fprintf(f, "offset: %u\n", a->offset);
1028                                         fprintf(f, "direction: %s\n", a->dir == be_stack_dir_along ? "along" : "against");
1029                                 }
1030                                 break;
1031                         }
1032
1033         }
1034
1035         return 0;
1036 }
1037
1038 /**
1039  * Copies the backend specific attributes from old node to new node.
1040  */
1041 static void copy_attr(const ir_node *old_node, ir_node *new_node)
1042 {
1043         be_node_attr_t *old_attr = get_irn_attr(old_node);
1044         be_node_attr_t *new_attr = get_irn_attr(new_node);
1045         int i;
1046
1047         assert(is_be_node(old_node));
1048         assert(is_be_node(new_node));
1049
1050         memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
1051         new_attr->reg_data = NULL;
1052
1053         if(new_attr->max_reg_data > 0) {
1054                 new_attr->reg_data = NEW_ARR_D(be_reg_data_t, get_irg_obstack(get_irn_irg(new_node)), new_attr->max_reg_data);
1055                 memcpy(new_attr->reg_data, old_attr->reg_data, new_attr->max_reg_data * sizeof(be_reg_data_t));
1056
1057                 for(i = 0; i < old_attr->max_reg_data; ++i) {
1058                         be_req_t *r;
1059
1060                         r = &new_attr->reg_data[i].req;
1061                         r->req.limited_env = r;
1062
1063                         r = &new_attr->reg_data[i].in_req;
1064                         r->req.limited_env = r;
1065                 }
1066         }
1067 }
1068
1069 static const ir_op_ops be_node_op_ops = {
1070         NULL,
1071         NULL,
1072         NULL,
1073         NULL,
1074         NULL,
1075         copy_attr,
1076         NULL,
1077         NULL,
1078         NULL,
1079         NULL,
1080         NULL,
1081         dump_node,
1082         NULL
1083 };
1084
1085 pset *nodes_live_at(const arch_env_t *arch_env, const arch_register_class_t *cls, const ir_node *pos, pset *live)
1086 {
1087         firm_dbg_module_t *dbg = firm_dbg_register("firm.be.node");
1088         const ir_node *bl      = is_Block(pos) ? pos : get_nodes_block(pos);
1089         ir_node *irn;
1090         irn_live_t *li;
1091
1092         live_foreach(bl, li) {
1093                 ir_node *irn = (ir_node *) li->irn;
1094                 if(live_is_end(li) && arch_irn_consider_in_reg_alloc(arch_env, cls, irn))
1095                         pset_insert_ptr(live, irn);
1096         }
1097
1098         sched_foreach_reverse(bl, irn) {
1099                 int i, n;
1100                 ir_node *x;
1101
1102                 /*
1103                  * If we encounter the node we want to insert the Perm after,
1104                 * exit immediately, so that this node is still live
1105                 */
1106                 if(irn == pos)
1107                         return live;
1108
1109                 DBG((dbg, LEVEL_1, "%+F\n", irn));
1110                 for(x = pset_first(live); x; x = pset_next(live))
1111                         DBG((dbg, LEVEL_1, "\tlive: %+F\n", x));
1112
1113                 if(arch_irn_consider_in_reg_alloc(arch_env, cls, irn))
1114                         pset_remove_ptr(live, irn);
1115
1116                 for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
1117                         ir_node *op = get_irn_n(irn, i);
1118
1119                         if(arch_irn_consider_in_reg_alloc(arch_env, cls, op))
1120                                 pset_insert_ptr(live, op);
1121                 }
1122         }
1123
1124         return live;
1125 }
1126
1127 ir_node *insert_Perm_after(const arch_env_t *arch_env,
1128                                                    const arch_register_class_t *cls,
1129                                                    dom_front_info_t *dom_front,
1130                                                    ir_node *pos)
1131 {
1132         ir_node *bl                 = is_Block(pos) ? pos : get_nodes_block(pos);
1133         ir_graph *irg               = get_irn_irg(bl);
1134         pset *live                  = pset_new_ptr_default();
1135         firm_dbg_module_t *dbg      = firm_dbg_register("be.node");
1136
1137         ir_node *curr, *irn, *perm, **nodes;
1138         int i, n;
1139
1140         DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));
1141
1142         if(!nodes_live_at(arch_env, cls, pos, live));
1143
1144         n = pset_count(live);
1145
1146         if(n == 0)
1147                 return NULL;
1148
1149         nodes = malloc(n * sizeof(nodes[0]));
1150
1151         DBG((dbg, LEVEL_1, "live:\n"));
1152         for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++) {
1153                 DBG((dbg, LEVEL_1, "\t%+F\n", irn));
1154                 nodes[i] = irn;
1155         }
1156
1157         perm = be_new_Perm(cls, irg, bl, n, nodes);
1158         sched_add_after(pos, perm);
1159         free(nodes);
1160
1161         curr = perm;
1162         for(i = 0; i < n; ++i) {
1163                 ir_node *copies[2];
1164                 ir_node *perm_op = get_irn_n(perm, i);
1165                 const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op);
1166
1167                 ir_mode *mode = get_irn_mode(perm_op);
1168                 ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
1169                 arch_set_irn_register(arch_env, proj, reg);
1170
1171                 sched_add_after(curr, proj);
1172                 curr = proj;
1173
1174                 copies[0] = perm_op;
1175                 copies[1] = proj;
1176                 be_ssa_constr(dom_front, 2, copies);
1177         }
1178         return perm;
1179 }