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