benode.c 38.6 KB
Newer Older
Sebastian Hack's avatar
Sebastian Hack committed
1
2
3
4
5
6
7
/**
 * @file   benode.c
 * @date   17.05.2005
 * @author Sebastian Hack
 *
 * Backend node support.
 *
Michael Beck's avatar
Michael Beck committed
8
 * This file provides Perm, Copy, Spill and Reload nodes.
9
 *
Sebastian Hack's avatar
Sebastian Hack committed
10
11
12
 * Copyright (C) 2005 Universitaet Karlsruhe
 * Released under the GPL
 */
13

Michael Beck's avatar
Michael Beck committed
14
15
16
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
Sebastian Hack's avatar
Sebastian Hack committed
17
18
19
20
21
22

#include <stdlib.h>

#include "obst.h"
#include "set.h"
#include "pmap.h"
23
#include "util.h"
Sebastian Hack's avatar
Sebastian Hack committed
24
#include "debug.h"
25
#include "fourcc.h"
Sebastian Hack's avatar
Sebastian Hack committed
26
#include "offset.h"
Sebastian Hack's avatar
Sebastian Hack committed
27
#include "bitfiddle.h"
Sebastian Hack's avatar
Sebastian Hack committed
28
29
30
31

#include "irop_t.h"
#include "irmode_t.h"
#include "irnode_t.h"
32
#include "ircons_t.h"
33
#include "irprintf.h"
Sebastian Hack's avatar
Sebastian Hack committed
34
#include "irgwalk.h"
Sebastian Hack's avatar
Sebastian Hack committed
35

36
37
38
#include "be_t.h"
#include "belive_t.h"
#include "besched_t.h"
Sebastian Hack's avatar
Sebastian Hack committed
39
40
#include "benode_t.h"

41
42
#include "beirgmod.h"

Sebastian Hack's avatar
Sebastian Hack committed
43
44
#define OUT_POS(x) (-((x) + 1))

Sebastian Hack's avatar
Sebastian Hack committed
45
46
/* Sometimes we want to put const nodes into get_irn_generic_attr ... */
#define get_irn_attr(irn) get_irn_generic_attr((ir_node *) (irn))
Daniel Grund's avatar
Daniel Grund committed
47

Sebastian Hack's avatar
Sebastian Hack committed
48
static unsigned be_node_tag = FOURCC('B', 'E', 'N', 'O');
49

Sebastian Hack's avatar
Sebastian Hack committed
50
#if 0
Sebastian Hack's avatar
Sebastian Hack committed
51
typedef enum _node_kind_t {
Christian Würdig's avatar
Christian Würdig committed
52
53
54
55
	node_kind_spill,
	node_kind_reload,
	node_kind_perm,
	node_kind_copy,
Sebastian Hack's avatar
Sebastian Hack committed
56
	node_kind_kill,
Christian Würdig's avatar
Christian Würdig committed
57
	node_kind_last
Sebastian Hack's avatar
Sebastian Hack committed
58
} node_kind_t;
Sebastian Hack's avatar
Sebastian Hack committed
59
#endif
Sebastian Hack's avatar
Sebastian Hack committed
60

Sebastian Hack's avatar
Sebastian Hack committed
61
62
63
64
65
66
typedef enum {
	be_req_kind_old_limited,
	be_req_kind_negate_old_limited,
	be_req_kind_single_reg
} be_req_kind_t;

Sebastian Hack's avatar
Sebastian Hack committed
67
68
typedef struct {
	arch_register_req_t req;
Sebastian Hack's avatar
Sebastian Hack committed
69
	be_req_kind_t       kind;
Sebastian Hack's avatar
Sebastian Hack committed
70
	arch_irn_flags_t    flags;
Sebastian Hack's avatar
Sebastian Hack committed
71
72
73
74
75
76
77
78
	union {
		struct {
			void (*old_limited)(void *ptr, bitset_t *bs);
			void *old_limited_env;
		} old_limited;

		const arch_register_t *single_reg;
	} x;
Sebastian Hack's avatar
Sebastian Hack committed
79
80
} be_req_t;

81
typedef struct {
82
	const arch_register_t *reg;
Sebastian Hack's avatar
Sebastian Hack committed
83
	be_req_t              req;
Sebastian Hack's avatar
Sebastian Hack committed
84
	be_req_t              in_req;
85
86
} be_reg_data_t;

87
/** The generic be nodes attribute type. */
88
typedef struct {
89
90
	int                   max_reg_data;
	be_reg_data_t         *reg_data;
91
92
} be_node_attr_t;

93
94
95
96
97
98
/** The be_Return nodes attribute type. */
typedef struct {
	be_node_attr_t node_attr;
	int            num_ret_vals;  /**< number of return values */
} be_return_attr_t;

99
/** The be_Stack attribute type. */
Sebastian Hack's avatar
Sebastian Hack committed
100
101
102
typedef struct {
	be_node_attr_t node_attr;
	int offset;           /**< The offset by which the stack shall be increased/decreased. */
103
	be_stack_dir_t dir;   /**< The direction in which the stack shall be modified (expand or shrink). */
Sebastian Hack's avatar
Sebastian Hack committed
104
105
} be_stack_attr_t;

106
/** The be_Frame attribute type. */
107
108
109
110
111
112
typedef struct {
	be_node_attr_t node_attr;
	entity *ent;
	int offset;
} be_frame_attr_t;

113
/** The be_Call attribute type. */
114
115
typedef struct {
	be_node_attr_t node_attr;
116
117
	entity *ent;         /**< The called entity if this is a static call. */
	ir_type *call_tp;    /**< The call type, copied from the original Call node. */
118
119
} be_call_attr_t;

120
/** The be_Spill attribute type. */
121
122
123
124
125
typedef struct {
	be_frame_attr_t frame_attr;
	ir_node *spill_ctx;  /**< The node in whose context this spill was introduced. */
} be_spill_attr_t;

126
127
128
129
130
ir_op *op_be_Spill;
ir_op *op_be_Reload;
ir_op *op_be_Perm;
ir_op *op_be_Copy;
ir_op *op_be_Keep;
Sebastian Hack's avatar
Sebastian Hack committed
131
ir_op *op_be_CopyKeep;
132
133
134
ir_op *op_be_Call;
ir_op *op_be_Return;
ir_op *op_be_IncSP;
Sebastian Hack's avatar
Sebastian Hack committed
135
ir_op *op_be_AddSP;
136
137
138
139
140
141
ir_op *op_be_SetSP;
ir_op *op_be_RegParams;
ir_op *op_be_StackParam;
ir_op *op_be_FrameAddr;
ir_op *op_be_FrameLoad;
ir_op *op_be_FrameStore;
142
ir_op *op_be_Barrier;
Sebastian Hack's avatar
Sebastian Hack committed
143

Sebastian Hack's avatar
Sebastian Hack committed
144
static int beo_base = -1;
Sebastian Hack's avatar
Sebastian Hack committed
145

Sebastian Hack's avatar
Sebastian Hack committed
146
static const ir_op_ops be_node_op_ops;
Sebastian Hack's avatar
Sebastian Hack committed
147

148
149
150
151
152
153
154
155
156
157
#define N   irop_flag_none
#define L   irop_flag_labeled
#define C   irop_flag_commutative
#define X   irop_flag_cfopcode
#define I   irop_flag_ip_cfopcode
#define F   irop_flag_fragile
#define Y   irop_flag_forking
#define H   irop_flag_highlevel
#define c   irop_flag_constlike
#define K   irop_flag_keep
158
159
#define M   irop_flag_machine

160

Sebastian Hack's avatar
Sebastian Hack committed
161
162
void be_node_init(void) {
	static int inited = 0;
Sebastian Hack's avatar
Sebastian Hack committed
163

Sebastian Hack's avatar
Sebastian Hack committed
164
165
	if(inited)
		return;
Sebastian Hack's avatar
Sebastian Hack committed
166

Sebastian Hack's avatar
Sebastian Hack committed
167
	inited = 1;
Sebastian Hack's avatar
Sebastian Hack committed
168

169
170
	/* Acquire all needed opcodes. */
	beo_base = get_next_ir_opcodes(beo_Last - 1);
Sebastian Hack's avatar
Sebastian Hack committed
171

172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
	op_be_Spill      = new_ir_op(beo_base + beo_Spill,      "be_Spill",      op_pin_state_mem_pinned, N, oparity_unary,    0, sizeof(be_spill_attr_t),  &be_node_op_ops);
	op_be_Reload     = new_ir_op(beo_base + beo_Reload,     "be_Reload",     op_pin_state_mem_pinned, N, oparity_zero,     0, sizeof(be_frame_attr_t),  &be_node_op_ops);
	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);
	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);
	op_be_Keep       = new_ir_op(beo_base + beo_Keep,       "be_Keep",       op_pin_state_pinned,     K, oparity_variable, 0, sizeof(be_node_attr_t),   &be_node_op_ops);
	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);
	op_be_Call       = new_ir_op(beo_base + beo_Call,       "be_Call",       op_pin_state_pinned,     N, oparity_variable, 0, sizeof(be_call_attr_t),   &be_node_op_ops);
	op_be_Return     = new_ir_op(beo_base + beo_Return,     "be_Return",     op_pin_state_pinned,     X, oparity_variable, 0, sizeof(be_return_attr_t), &be_node_op_ops);
	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);
	op_be_SetSP      = new_ir_op(beo_base + beo_SetSP,      "be_SetSP",      op_pin_state_pinned,     N, oparity_binary,   0, sizeof(be_stack_attr_t),  &be_node_op_ops);
	op_be_IncSP      = new_ir_op(beo_base + beo_IncSP,      "be_IncSP",      op_pin_state_pinned,     N, oparity_binary,   0, sizeof(be_stack_attr_t),  &be_node_op_ops);
	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);
	op_be_StackParam = new_ir_op(beo_base + beo_StackParam, "be_StackParam", op_pin_state_pinned,     N, oparity_unary,    0, sizeof(be_frame_attr_t),  &be_node_op_ops);
	op_be_FrameAddr  = new_ir_op(beo_base + beo_FrameAddr,  "be_FrameAddr",  op_pin_state_pinned,     N, oparity_binary,   0, sizeof(be_frame_attr_t),  &be_node_op_ops);
	op_be_FrameLoad  = new_ir_op(beo_base + beo_FrameLoad,  "be_FrameLoad",  op_pin_state_pinned,     N, oparity_any,      0, sizeof(be_frame_attr_t),  &be_node_op_ops);
	op_be_FrameStore = new_ir_op(beo_base + beo_FrameStore, "be_FrameStore", op_pin_state_pinned,     N, oparity_any,      0, sizeof(be_frame_attr_t),  &be_node_op_ops);
	op_be_Barrier    = new_ir_op(beo_base + beo_Barrier,    "be_Barrier",    op_pin_state_pinned,     N, oparity_any,      0, sizeof(be_node_attr_t),   &be_node_op_ops);
189
190
191
192
193
194

	set_op_tag(op_be_Spill,      &be_node_tag);
	set_op_tag(op_be_Reload,     &be_node_tag);
	set_op_tag(op_be_Perm,       &be_node_tag);
	set_op_tag(op_be_Copy,       &be_node_tag);
	set_op_tag(op_be_Keep,       &be_node_tag);
Sebastian Hack's avatar
Sebastian Hack committed
195
	set_op_tag(op_be_CopyKeep,   &be_node_tag);
196
197
	set_op_tag(op_be_Call,       &be_node_tag);
	set_op_tag(op_be_Return,     &be_node_tag);
Sebastian Hack's avatar
Sebastian Hack committed
198
	set_op_tag(op_be_AddSP,      &be_node_tag);
199
200
201
202
203
204
205
	set_op_tag(op_be_SetSP,      &be_node_tag);
	set_op_tag(op_be_IncSP,      &be_node_tag);
	set_op_tag(op_be_RegParams,  &be_node_tag);
	set_op_tag(op_be_StackParam, &be_node_tag);
	set_op_tag(op_be_FrameLoad,  &be_node_tag);
	set_op_tag(op_be_FrameStore, &be_node_tag);
	set_op_tag(op_be_FrameAddr,  &be_node_tag);
206
	set_op_tag(op_be_Barrier,    &be_node_tag);
Sebastian Hack's avatar
Sebastian Hack committed
207
}
208

209
210
211
/**
 * Initializes the generic attribute of all be nodes and return ir.
 */
Sebastian Hack's avatar
Sebastian Hack committed
212
static void *init_node_attr(ir_node* irn, int max_reg_data)
Sebastian Hack's avatar
Sebastian Hack committed
213
{
Sebastian Hack's avatar
Sebastian Hack committed
214
	ir_graph *irg     = get_irn_irg(irn);
Sebastian Hack's avatar
Sebastian Hack committed
215
216
	be_node_attr_t *a = get_irn_attr(irn);

217
	memset(a, 0, sizeof(get_op_attr_size(get_irn_op(irn))));
Sebastian Hack's avatar
Sebastian Hack committed
218
219
	a->max_reg_data = max_reg_data;
	a->reg_data     = NULL;
Sebastian Hack's avatar
Sebastian Hack committed
220

Sebastian Hack's avatar
Sebastian Hack committed
221
	if(max_reg_data > 0) {
Sebastian Hack's avatar
Sebastian Hack committed
222
223
		int i;

Sebastian Hack's avatar
Sebastian Hack committed
224
225
226
		a->reg_data = NEW_ARR_D(be_reg_data_t, get_irg_obstack(irg), max_reg_data);
		memset(a->reg_data, 0, max_reg_data * sizeof(a->reg_data[0]));
		for(i = 0; i < max_reg_data; ++i) {
Sebastian Hack's avatar
Sebastian Hack committed
227
228
			a->reg_data[i].req.req.cls  = NULL;
			a->reg_data[i].req.req.type = arch_register_req_type_none;
Sebastian Hack's avatar
Sebastian Hack committed
229
230
231
232
233
		}
	}

	return a;
}
234

235
int is_be_node(const ir_node *irn)
236
{
Sebastian Hack's avatar
Sebastian Hack committed
237
	return get_op_tag(get_irn_op(irn)) == &be_node_tag;
238
239
}

Sebastian Hack's avatar
Sebastian Hack committed
240
be_opcode_t be_get_irn_opcode(const ir_node *irn)
241
{
Sebastian Hack's avatar
Sebastian Hack committed
242
	return is_be_node(irn) ? get_irn_opcode(irn) - beo_base : beo_NoBeOp;
243
244
}

Sebastian Hack's avatar
Sebastian Hack committed
245
246
247
248
249
static int redir_proj(const ir_node **node, int pos)
{
	const ir_node *n = *node;

	if(is_Proj(n)) {
250
251
		ir_node *irn;

Sebastian Hack's avatar
Sebastian Hack committed
252
		assert(pos == -1 && "Illegal pos for a Proj");
253
254
255
256
257
		*node = irn = get_Proj_pred(n);
		if(is_Proj(irn)) {
			assert(get_irn_mode(irn) == mode_T);
			*node = get_Proj_pred(irn);
		}
Sebastian Hack's avatar
Sebastian Hack committed
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
		return get_Proj_proj(n);
	}

	return 0;
}

static void
be_node_set_irn_reg(const void *_self, ir_node *irn, const arch_register_t *reg)
{
	int out_pos;
	be_node_attr_t *a;

	out_pos = redir_proj((const ir_node **) &irn, -1);
	a       = get_irn_attr(irn);

	assert(is_be_node(irn));
Sebastian Hack's avatar
Sebastian Hack committed
274
	assert(out_pos < a->max_reg_data && "position too high");
Sebastian Hack's avatar
Sebastian Hack committed
275
276
277
278
	a->reg_data[out_pos].reg = reg;
}


Sebastian Hack's avatar
Sebastian Hack committed
279
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)
280
{
Sebastian Hack's avatar
Sebastian Hack committed
281
	be_spill_attr_t *a;
Sebastian Hack's avatar
Sebastian Hack committed
282
	ir_node *in[2];
Sebastian Hack's avatar
Sebastian Hack committed
283
	ir_node *res;
284

Sebastian Hack's avatar
Sebastian Hack committed
285
286
	in[0] = frame;
	in[1] = to_spill;
287
	res   = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
Sebastian Hack's avatar
Sebastian Hack committed
288
	a     = init_node_attr(res, 2);
289
290
	a->frame_attr.ent = NULL;
	a->frame_attr.offset = 0;
Sebastian Hack's avatar
Sebastian Hack committed
291
	a->spill_ctx = ctx;
Sebastian Hack's avatar
Sebastian Hack committed
292
293
294

	be_node_set_reg_class(res, 0, cls_frame);
	be_node_set_reg_class(res, 1, cls);
Sebastian Hack's avatar
Sebastian Hack committed
295
296
	return res;
}
297

Sebastian Hack's avatar
Sebastian Hack committed
298
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)
299
{
Sebastian Hack's avatar
Sebastian Hack committed
300
	ir_node *in[2];
Sebastian Hack's avatar
Sebastian Hack committed
301
	ir_node *res;
302

Sebastian Hack's avatar
Sebastian Hack committed
303
304
	in[0] = frame;
	in[1] = mem;
305
	res   = new_ir_node(NULL, irg, bl, op_be_Reload, mode, 2, in);
Sebastian Hack's avatar
Sebastian Hack committed
306
307
308
	init_node_attr(res, 2);
	be_node_set_reg_class(res, 0, cls_frame);
	be_node_set_reg_class(res, -1, cls);
Sebastian Hack's avatar
Sebastian Hack committed
309
310
	return res;
}
311

312
ir_node *be_get_Reload_mem(const ir_node *irn)
Sebastian Hack's avatar
Sebastian Hack committed
313
314
{
	assert(be_is_Reload(irn));
Daniel Grund's avatar
Daniel Grund committed
315
	return get_irn_n(irn, be_pos_Reload_mem);
Sebastian Hack's avatar
Sebastian Hack committed
316
317
}

318
319
320
321
322
323
ir_node *be_get_Reload_frame(const ir_node *irn)
{
	assert(be_is_Reload(irn));
	return get_irn_n(irn, be_pos_Reload_frame);
}

Sebastian Hack's avatar
Sebastian Hack committed
324
325
ir_node *be_new_Perm(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
{
Sebastian Hack's avatar
Sebastian Hack committed
326
	int i;
327
	ir_node *irn = new_ir_node(NULL, irg, bl, op_be_Perm, mode_T, n, in);
Sebastian Hack's avatar
Sebastian Hack committed
328
329
330
331
332
333
	init_node_attr(irn, n);
	for(i = 0; i < n; ++i) {
		be_node_set_reg_class(irn, i, cls);
		be_node_set_reg_class(irn, OUT_POS(i), cls);
	}

Sebastian Hack's avatar
Sebastian Hack committed
334
335
	return irn;
}
336

Sebastian Hack's avatar
Sebastian Hack committed
337
338
339
340
ir_node *be_new_Copy(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *op)
{
	ir_node *in[1];
	ir_node *res;
341

Sebastian Hack's avatar
Sebastian Hack committed
342
	in[0] = op;
343
	res   = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
Sebastian Hack's avatar
Sebastian Hack committed
344
345
346
	init_node_attr(res, 1);
	be_node_set_reg_class(res, 0, cls);
	be_node_set_reg_class(res, OUT_POS(0), cls);
Sebastian Hack's avatar
Sebastian Hack committed
347
	return res;
348
349
}

350
351
352
353
ir_node *be_get_Copy_op(const ir_node *cpy) {
	return get_irn_n(cpy, be_pos_Copy_op);
}

354
355
356
357
void be_set_Copy_op(ir_node *cpy, ir_node *op) {
	set_irn_n(cpy, be_pos_Copy_op, op);
}

Sebastian Hack's avatar
Sebastian Hack committed
358
ir_node *be_new_Keep(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
Sebastian Hack's avatar
Sebastian Hack committed
359
{
Sebastian Hack's avatar
Sebastian Hack committed
360
	int i;
Sebastian Hack's avatar
Sebastian Hack committed
361
	ir_node *irn;
Sebastian Hack's avatar
Sebastian Hack committed
362

363
	irn = new_ir_node(NULL, irg, bl, op_be_Keep, mode_ANY, n, in);
Sebastian Hack's avatar
Sebastian Hack committed
364
365
366
367
	init_node_attr(irn, n);
	for(i = 0; i < n; ++i) {
		be_node_set_reg_class(irn, i, cls);
	}
Sebastian Hack's avatar
Sebastian Hack committed
368
369
	keep_alive(irn);
	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
370
371
}

372
373
ir_node *be_new_Call(dbg_info *dbg, ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *sp, ir_node *ptr,
                     int n_outs, int n, ir_node *in[], ir_type *call_tp)
Sebastian Hack's avatar
Sebastian Hack committed
374
{
375
376
	be_call_attr_t *a;
	int real_n = be_pos_Call_first_arg + n;
Sebastian Hack's avatar
Sebastian Hack committed
377
378
	ir_node *irn;
	ir_node **real_in;
Sebastian Hack's avatar
Sebastian Hack committed
379

380
381
382
383
384
	NEW_ARR_A(ir_node *, real_in, real_n);
	real_in[be_pos_Call_mem] = mem;
	real_in[be_pos_Call_sp]  = sp;
	real_in[be_pos_Call_ptr] = ptr;
	memcpy(&real_in[be_pos_Call_first_arg], in, n * sizeof(in[0]));
Sebastian Hack's avatar
Sebastian Hack committed
385

386
	irn = new_ir_node(NULL, irg, bl, op_be_Call, mode_T, real_n, real_in);
387
388
389
	a = init_node_attr(irn, (n_outs > real_n ? n_outs : real_n));
	a->ent     = NULL;
	a->call_tp = call_tp;
Sebastian Hack's avatar
Sebastian Hack committed
390
	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
391
392
}

393
394
/* Gets the call entity or NULL if this is no static call. */
entity *be_Call_get_entity(const ir_node *call) {
395
396
397
398
399
	be_call_attr_t *a = get_irn_attr(call);
	assert(be_is_Call(call));
	return a->ent;
}

400
401
/* Sets the call entity. */
void be_Call_set_entity(ir_node *call, entity *ent) {
402
403
404
405
406
	be_call_attr_t *a = get_irn_attr(call);
	assert(be_is_Call(call));
	a->ent = ent;
}

407
408
409
410
411
412
413
414
415
416
417
418
419
420
/* Gets the call type. */
ir_type *be_Call_get_type(ir_node *call) {
	be_call_attr_t *a = get_irn_attr(call);
	assert(be_is_Call(call));
	return a->call_tp;
}

/* Sets the call type. */
void be_Call_set_type(ir_node *call, ir_type *call_tp) {
	be_call_attr_t *a = get_irn_attr(call);
	assert(be_is_Call(call));
	a->call_tp = call_tp;
}

421
422
/* Construct a new be_Return. */
ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *bl, int n_res, int n, ir_node *in[])
Sebastian Hack's avatar
Sebastian Hack committed
423
{
424
	be_return_attr_t *a;
425
	ir_node *irn = new_ir_node(dbg, irg, bl, op_be_Return, mode_X, n, in);
Sebastian Hack's avatar
Sebastian Hack committed
426
	init_node_attr(irn, n);
427
428
	a = get_irn_attr(irn);
	a->num_ret_vals = n_res;
Sebastian Hack's avatar
Sebastian Hack committed
429
430
431
432

	return irn;
}

433
434
435
436
437
438
439
/* Returns the number of real returns values */
int be_Return_get_n_rets(ir_node *ret)
{
	be_return_attr_t *a = get_irn_attr(ret);
	return a->num_ret_vals;
}

440
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)
Sebastian Hack's avatar
Sebastian Hack committed
441
{
Sebastian Hack's avatar
Sebastian Hack committed
442
443
	be_stack_attr_t *a;
	ir_node *irn;
444
	ir_node *in[2];
Sebastian Hack's avatar
Sebastian Hack committed
445
446

	in[0]     = old_sp;
447
	in[1]     = mem;
448
	irn       = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode, 2, in);
Sebastian Hack's avatar
Sebastian Hack committed
449
	a         = init_node_attr(irn, 1);
Sebastian Hack's avatar
Sebastian Hack committed
450
451
452
	a->dir    = dir;
	a->offset = offset;

453
	be_node_set_flags(irn, -1, arch_irn_flags_ignore | arch_irn_flags_modify_sp);
Sebastian Hack's avatar
Sebastian Hack committed
454

Sebastian Hack's avatar
Sebastian Hack committed
455
	/* Set output constraint to stack register. */
Sebastian Hack's avatar
Sebastian Hack committed
456
	be_node_set_reg_class(irn, 0, sp->reg_class);
Sebastian Hack's avatar
Sebastian Hack committed
457
	be_set_constr_single_reg(irn, -1, sp);
Sebastian Hack's avatar
Sebastian Hack committed
458
	be_node_set_irn_reg(NULL, irn, sp);
Sebastian Hack's avatar
Sebastian Hack committed
459
460

	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
461
}
Sebastian Hack's avatar
Sebastian Hack committed
462

Sebastian Hack's avatar
Sebastian Hack committed
463
ir_node *be_new_AddSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, ir_node *sz)
Sebastian Hack's avatar
Sebastian Hack committed
464
{
Sebastian Hack's avatar
Sebastian Hack committed
465
	be_node_attr_t *a;
Sebastian Hack's avatar
Sebastian Hack committed
466
	ir_node *irn;
Sebastian Hack's avatar
Sebastian Hack committed
467
	ir_node *in[be_pos_AddSP_last];
Sebastian Hack's avatar
Sebastian Hack committed
468

Sebastian Hack's avatar
Sebastian Hack committed
469
470
	in[be_pos_AddSP_old_sp] = old_sp;
	in[be_pos_AddSP_size]   = sz;
Sebastian Hack's avatar
Sebastian Hack committed
471

Sebastian Hack's avatar
Sebastian Hack committed
472
473
474
	irn      = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T, be_pos_AddSP_last, in);
	a        = init_node_attr(irn, be_pos_AddSP_last);

475
	be_node_set_flags(irn, OUT_POS(0), arch_irn_flags_ignore | arch_irn_flags_modify_sp);
Sebastian Hack's avatar
Sebastian Hack committed
476
477

	/* Set output constraint to stack register. */
Sebastian Hack's avatar
Sebastian Hack committed
478
	be_set_constr_single_reg(irn, OUT_POS(0), sp);
Sebastian Hack's avatar
Sebastian Hack committed
479
480
481
482
483
484
485
486
487
488
489
490
491

	return irn;
}

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)
{
	be_node_attr_t *a;
	ir_node *irn;
	ir_node *in[3];

	in[0]    = mem;
	in[1]    = old_sp;
	in[2]    = op;
492
	irn      = new_ir_node(NULL, irg, bl, op_be_SetSP, get_irn_mode(old_sp), 3, in);
Sebastian Hack's avatar
Sebastian Hack committed
493
494
	a        = init_node_attr(irn, 3);

495
	be_node_set_flags(irn, OUT_POS(0), arch_irn_flags_ignore | arch_irn_flags_modify_sp);
Sebastian Hack's avatar
Sebastian Hack committed
496
497
498
499
500

	/* Set output constraint to stack register. */
	be_set_constr_single_reg(irn, OUT_POS(0), sp);
	be_node_set_reg_class(irn, 1, sp->reg_class);
	be_node_set_reg_class(irn, 2, sp->reg_class);
Sebastian Hack's avatar
Sebastian Hack committed
501
	be_node_set_irn_reg(NULL, irn, sp);
Sebastian Hack's avatar
Sebastian Hack committed
502
503

	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
504
}
Sebastian Hack's avatar
Sebastian Hack committed
505

Sebastian Hack's avatar
Sebastian Hack committed
506
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)
Sebastian Hack's avatar
Sebastian Hack committed
507
{
508
	be_frame_attr_t *a;
Sebastian Hack's avatar
Sebastian Hack committed
509
510
511
	ir_node *irn;
	ir_node *in[1];

512
	in[0] = frame_pointer;
513
	irn = new_ir_node(NULL, irg, bl, op_be_StackParam, mode, 1, in);
Sebastian Hack's avatar
Sebastian Hack committed
514
	a = init_node_attr(irn, 1);
515
	a->ent = ent;
Sebastian Hack's avatar
Sebastian Hack committed
516
517
518

	be_node_set_reg_class(irn, 0, cls_frame);
	be_node_set_reg_class(irn, OUT_POS(0), cls);
Sebastian Hack's avatar
Sebastian Hack committed
519
	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
520
}
Sebastian Hack's avatar
Sebastian Hack committed
521

522
ir_node *be_new_RegParams(ir_graph *irg, ir_node *bl, int n_outs)
Sebastian Hack's avatar
Sebastian Hack committed
523
524
525
526
{
	ir_node *irn;
	ir_node *in[1];

527
	irn = new_ir_node(NULL, irg, bl, op_be_RegParams, mode_T, 0, in);
Sebastian Hack's avatar
Sebastian Hack committed
528
	init_node_attr(irn, n_outs);
Sebastian Hack's avatar
Sebastian Hack committed
529
530
531
	return irn;
}

532
533
534
535
536
537
538
539
540
ir_node *be_new_FrameLoad(const arch_register_class_t *cls_frame, const arch_register_class_t *cls_data,
						  ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *frame, entity *ent)
{
	be_frame_attr_t *a;
	ir_node *irn;
	ir_node *in[2];

	in[0]  = mem;
	in[1]  = frame;
541
	irn    = new_ir_node(NULL, irg, bl, op_be_FrameLoad, mode_T, 2, in);
Sebastian Hack's avatar
Sebastian Hack committed
542
	a      = init_node_attr(irn, 3);
543
544
545
	a->ent = ent;
	a->offset = 0;
	be_node_set_reg_class(irn, 1, cls_frame);
Sebastian Hack's avatar
Sebastian Hack committed
546
	be_node_set_reg_class(irn, OUT_POS(pn_Load_res), cls_data);
547
548
549
550
551
552
553
554
555
556
557
558
559
	return irn;
}

ir_node *be_new_FrameStore(const arch_register_class_t *cls_frame, const arch_register_class_t *cls_data,
						   ir_graph *irg, ir_node *bl, ir_node *mem, ir_node *frame, ir_node *data, entity *ent)
{
	be_frame_attr_t *a;
	ir_node *irn;
	ir_node *in[3];

	in[0]  = mem;
	in[1]  = frame;
	in[2]  = data;
560
	irn    = new_ir_node(NULL, irg, bl, op_be_FrameStore, mode_T, 3, in);
Sebastian Hack's avatar
Sebastian Hack committed
561
	a      = init_node_attr(irn, 3);
562
563
564
565
566
567
568
569
	a->ent = ent;
	a->offset = 0;
	be_node_set_reg_class(irn, 1, cls_frame);
	be_node_set_reg_class(irn, 2, cls_data);
	return irn;
}

ir_node *be_new_FrameAddr(const arch_register_class_t *cls_frame, ir_graph *irg, ir_node *bl, ir_node *frame, entity *ent)
Sebastian Hack's avatar
Sebastian Hack committed
570
{
571
	be_frame_attr_t *a;
Sebastian Hack's avatar
Sebastian Hack committed
572
573
574
	ir_node *irn;
	ir_node *in[1];

575
	in[0]  = frame;
576
	irn    = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
Sebastian Hack's avatar
Sebastian Hack committed
577
	a      = init_node_attr(irn, 1);
578
579
580
	a->ent = ent;
	a->offset = 0;
	be_node_set_reg_class(irn, 0, cls_frame);
Sebastian Hack's avatar
Sebastian Hack committed
581
	be_node_set_reg_class(irn, OUT_POS(0), cls_frame);
Sebastian Hack's avatar
Sebastian Hack committed
582
583
584
	return irn;
}

Sebastian Hack's avatar
Sebastian Hack committed
585
586
587
588
589
590
591
592
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)
{
	ir_node *irn;
	ir_node **in = (ir_node **) alloca((n + 1) * sizeof(in[0]));

	in[0] = src;
	memcpy(&in[1], in_keep, n * sizeof(in[0]));
	irn   = new_ir_node(NULL, irg, bl, op_be_CopyKeep, mode, n + 1, in);
Sebastian Hack's avatar
Sebastian Hack committed
593
	init_node_attr(irn, n + 1);
Sebastian Hack's avatar
Sebastian Hack committed
594
595
596
597
598
599
600
601
602
603
604
605
606
607
	be_node_set_reg_class(irn, OUT_POS(0), cls);
	be_node_set_reg_class(irn, 0, cls);

	return irn;
}

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)
{
	ir_node *in[1];

	in[0] = keep;
	return be_new_CopyKeep(cls, irg, bl, src, 1, in, mode);
}

608
ir_node *be_new_Barrier(ir_graph *irg, ir_node *bl, int n, ir_node *in[])
Sebastian Hack's avatar
Sebastian Hack committed
609
610
611
{
	ir_node *irn;

612
	irn = new_ir_node(NULL, irg, bl, op_be_Barrier, mode_T, n, in);
Sebastian Hack's avatar
Sebastian Hack committed
613
614
615
616
	init_node_attr(irn, n);
	return irn;
}

Sebastian Hack's avatar
Sebastian Hack committed
617
618
619
int be_is_Spill         (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Spill          ; }
int be_is_Reload        (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Reload         ; }
int be_is_Copy          (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Copy           ; }
Sebastian Hack's avatar
Sebastian Hack committed
620
int be_is_CopyKeep      (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_CopyKeep       ; }
Sebastian Hack's avatar
Sebastian Hack committed
621
622
623
624
625
int be_is_Perm          (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Perm           ; }
int be_is_Keep          (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Keep           ; }
int be_is_Call          (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Call           ; }
int be_is_Return        (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Return         ; }
int be_is_IncSP         (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_IncSP          ; }
Sebastian Hack's avatar
Sebastian Hack committed
626
int be_is_SetSP         (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_SetSP          ; }
Sebastian Hack's avatar
Sebastian Hack committed
627
int be_is_AddSP         (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_AddSP          ; }
Sebastian Hack's avatar
Sebastian Hack committed
628
629
int be_is_RegParams     (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_RegParams      ; }
int be_is_StackParam    (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_StackParam     ; }
630
631
632
int be_is_FrameAddr     (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameAddr      ; }
int be_is_FrameLoad     (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameLoad      ; }
int be_is_FrameStore    (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_FrameStore     ; }
633
int be_is_Barrier       (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_Barrier        ; }
634
635
636
637
638
639

int be_has_frame_entity(const ir_node *irn)
{
	switch(be_get_irn_opcode(irn)) {
	case beo_StackParam:
	case beo_Spill:
640
	case beo_Reload:
641
642
643
644
	case beo_FrameStore:
	case beo_FrameLoad:
	case beo_FrameAddr:
		return 1;
645
646
	default:
		return 0;
647
648
649
	}
}

650
entity *be_get_frame_entity(const ir_node *irn)
651
652
653
654
655
656
657
{
	if(be_has_frame_entity(irn)) {
		be_frame_attr_t *a = get_irn_attr(irn);
		return a->ent;
	}
	return NULL;
}
Sebastian Hack's avatar
Sebastian Hack committed
658

Sebastian Hack's avatar
Sebastian Hack committed
659
static void be_limited(void *data, bitset_t *bs)
Sebastian Hack's avatar
Sebastian Hack committed
660
{
Sebastian Hack's avatar
Sebastian Hack committed
661
662
	be_req_t *req = data;

Sebastian Hack's avatar
Sebastian Hack committed
663
664
665
666
667
668
669
670
671
672
673
674
	switch(req->kind) {
	case be_req_kind_negate_old_limited:
	case be_req_kind_old_limited:
		req->x.old_limited.old_limited(req->x.old_limited.old_limited_env, bs);
		if(req->kind == be_req_kind_negate_old_limited)
			bitset_flip_all(bs);
		break;
	case be_req_kind_single_reg:
		bitset_clear_all(bs);
		bitset_set(bs, req->x.single_reg->index);
		break;
	}
Sebastian Hack's avatar
Sebastian Hack committed
675
676
}

Sebastian Hack's avatar
Sebastian Hack committed
677
static INLINE be_req_t *get_req(ir_node *irn, int pos)
Sebastian Hack's avatar
Sebastian Hack committed
678
{
Michael Beck's avatar
Michael Beck committed
679
	int idx           = pos < 0 ? -(pos + 1) : pos;
Sebastian Hack's avatar
Sebastian Hack committed
680
681
682
683
684
685
	be_node_attr_t *a = get_irn_attr(irn);
	be_reg_data_t *rd = &a->reg_data[idx];
	be_req_t       *r = pos < 0 ? &rd->req : &rd->in_req;

	assert(is_be_node(irn));
	assert(!(pos >= 0) || pos < get_irn_arity(irn));
Sebastian Hack's avatar
Sebastian Hack committed
686
	assert(!(pos < 0)  || -(pos + 1) <= a->max_reg_data);
Sebastian Hack's avatar
Sebastian Hack committed
687

Sebastian Hack's avatar
Sebastian Hack committed
688
689
690
691
692
693
694
	return r;
}

void be_set_constr_single_reg(ir_node *irn, int pos, const arch_register_t *reg)
{
	be_req_t *r = get_req(irn, pos);

Sebastian Hack's avatar
Sebastian Hack committed
695
696
697
698
699
700
701
702
	r->kind            = be_req_kind_single_reg;
	r->x.single_reg    = reg;
	r->req.limited     = be_limited;
	r->req.limited_env = r;
	r->req.type        = arch_register_req_type_limited;
	r->req.cls         = reg->reg_class;
}

703
704
void be_set_constr_limited(ir_node *irn, int pos, const arch_register_req_t *req)
{
Sebastian Hack's avatar
Sebastian Hack committed
705
	be_req_t *r = get_req(irn, pos);
706
707
708
709
710
711
712
713
714
715
716
717
718

	assert(arch_register_req_is(req, limited));

	r->kind            = be_req_kind_old_limited;
	r->req.limited     = be_limited;
	r->req.limited_env = r;
	r->req.type        = arch_register_req_type_limited;
	r->req.cls         = req->cls;

	r->x.old_limited.old_limited     = req->limited;
	r->x.old_limited.old_limited_env = req->limited_env;
}

Sebastian Hack's avatar
Sebastian Hack committed
719
720
721
722
723
724
725
726
727
728
void be_node_set_flags(ir_node *irn, int pos, arch_irn_flags_t flags)
{
	be_req_t *r = get_req(irn, pos);
	r->flags = flags;
}

void be_node_set_reg_class(ir_node *irn, int pos, const arch_register_class_t *cls)
{
	be_req_t *r = get_req(irn, pos);
	r->req.cls = cls;
Sebastian Hack's avatar
Sebastian Hack committed
729
730
	if(r->req.type == arch_register_req_type_none)
		r->req.type = arch_register_req_type_normal;
Sebastian Hack's avatar
Sebastian Hack committed
731
732
}

733
734
735
736
737
738
void be_node_set_req_type(ir_node *irn, int pos, arch_register_req_type_t type)
{
	be_req_t *r = get_req(irn, pos);
	r->req.type = type;
}

Michael Beck's avatar
BugFix:    
Michael Beck committed
739
740
741
742
743
ir_node *be_get_IncSP_pred(ir_node *irn) {
	assert(be_is_IncSP(irn));
	return get_irn_n(irn, 0);
}

744
void be_set_IncSP_pred(ir_node *incsp, ir_node *pred) {
745
746
747
748
	assert(be_is_IncSP(incsp));
	set_irn_n(incsp, 0, pred);
}

Michael Beck's avatar
Michael Beck committed
749
750
751
752
753
ir_node *be_get_IncSP_mem(ir_node *irn) {
	assert(be_is_IncSP(irn));
	return get_irn_n(irn, 1);
}

Sebastian Hack's avatar
Sebastian Hack committed
754
755
756
757
758
759
void be_set_IncSP_offset(ir_node *irn, unsigned offset)
{
	be_stack_attr_t *a = get_irn_attr(irn);
	assert(be_is_IncSP(irn));
	a->offset = offset;
}
760

761
unsigned be_get_IncSP_offset(const ir_node *irn)
Sebastian Hack's avatar
Sebastian Hack committed
762
{
Sebastian Hack's avatar
Sebastian Hack committed
763
764
765
766
	be_stack_attr_t *a = get_irn_attr(irn);
	assert(be_is_IncSP(irn));
	return a->offset;
}
Sebastian Hack's avatar
Sebastian Hack committed
767

Sebastian Hack's avatar
Sebastian Hack committed
768
769
770
771
772
void be_set_IncSP_direction(ir_node *irn, be_stack_dir_t dir)
{
	be_stack_attr_t *a = get_irn_attr(irn);
	assert(be_is_IncSP(irn));
	a->dir = dir;
Sebastian Hack's avatar
Sebastian Hack committed
773
774
}

775
be_stack_dir_t be_get_IncSP_direction(const ir_node *irn)
Sebastian Hack's avatar
Sebastian Hack committed
776
{
Sebastian Hack's avatar
Sebastian Hack committed
777
778
779
	be_stack_attr_t *a = get_irn_attr(irn);
	assert(be_is_IncSP(irn));
	return a->dir;
Sebastian Hack's avatar
Sebastian Hack committed
780
781
}

782
void be_set_Spill_entity(ir_node *irn, entity *ent)
783
{
Sebastian Hack's avatar
Sebastian Hack committed
784
785
	be_spill_attr_t *a = get_irn_attr(irn);
	assert(be_is_Spill(irn));
786
	a->frame_attr.ent = ent;
787
788
789
790
}

static ir_node *find_a_spill_walker(ir_node *irn, unsigned visited_nr)
{
791
792
793
	unsigned nr = get_irn_visited(irn);

	set_irn_visited(irn, visited_nr);
794

795
796
797
	if(is_Phi(irn)) {
		int i, n;
		if(nr < visited_nr) {
798
799
800
801
802
803
804
805
			for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
				ir_node *n = find_a_spill_walker(get_irn_n(irn, i), visited_nr);
				if(n != NULL)
					return n;
			}
		}
	}

806
807
808
	else if(be_get_irn_opcode(irn) == beo_Spill)
		return irn;

809
810
811
	return NULL;
}

Sebastian Hack's avatar
Sebastian Hack committed
812
813
814
ir_node *be_get_Spill_context(const ir_node *irn) {
	const be_spill_attr_t *a = get_irn_attr(irn);
	assert(be_is_Spill(irn));
815
816
817
	return a->spill_ctx;
}

818
819
820
821
822
823
/**
 * Finds a spill for a reload.
 * If the reload is directly using the spill, this is simple,
 * else we perform DFS from the reload (over all PhiMs) and return
 * the first spill node we find.
 */
824
static INLINE ir_node *find_a_spill(const ir_node *irn)
825
826
827
828
{
	ir_graph *irg       = get_irn_irg(irn);
	unsigned visited_nr = get_irg_visited(irg) + 1;

Sebastian Hack's avatar
Sebastian Hack committed
829
	assert(be_is_Reload(irn));
830
	set_irg_visited(irg, visited_nr);
Sebastian Hack's avatar
Sebastian Hack committed
831
	return find_a_spill_walker(be_get_Reload_mem(irn), visited_nr);
832
833
}

834
entity *be_get_spill_entity(const ir_node *irn)
835
{
Sebastian Hack's avatar
Sebastian Hack committed
836
	switch(be_get_irn_opcode(irn)) {
Sebastian Hack's avatar
Sebastian Hack committed
837
	case beo_Reload:
838
839
840
841
		{
			ir_node *spill = find_a_spill(irn);
			return be_get_spill_entity(spill);
		}
Sebastian Hack's avatar
Sebastian Hack committed
842
843
844
	case beo_Spill:
		{
			be_spill_attr_t *a = get_irn_attr(irn);
845
			return a->frame_attr.ent;
Sebastian Hack's avatar
Sebastian Hack committed
846
		}
847
	default:
Sebastian Hack's avatar
Sebastian Hack committed
848
		assert(0 && "Must give spill/reload node");
849
		break;
850
851
	}

852
	return NULL;
Sebastian Hack's avatar
Sebastian Hack committed
853
854
}

Sebastian Hack's avatar
Sebastian Hack committed
855
static void link_reload_walker(ir_node *irn, void *data)
856
{
Sebastian Hack's avatar
Sebastian Hack committed
857
	ir_node **root = (ir_node **) data;
858
	if(be_is_Reload(irn)) {
Sebastian Hack's avatar
Sebastian Hack committed
859
860
		set_irn_link(irn, *root);
		*root = irn;
861
862
863
864
865
	}
}

void be_copy_entities_to_reloads(ir_graph *irg)
{
Sebastian Hack's avatar
Sebastian Hack committed
866
867
868
869
870
871
872
873
874
	ir_node *irn = NULL;
	irg_walk_graph(irg, link_reload_walker, NULL, (void *) &irn);

	while(irn) {
		be_frame_attr_t *a = get_irn_attr(irn);
		entity *ent        = be_get_spill_entity(irn);
		a->ent = ent;
		irn    = get_irn_link(irn);
	}
875
876
}

Sebastian Hack's avatar
Sebastian Hack committed
877
ir_node *be_spill(const arch_env_t *arch_env, ir_node *irn, ir_node *ctx)
Sebastian Hack's avatar
Sebastian Hack committed
878
{
879
880
881
882
	ir_node *bl     = get_nodes_block(irn);
	ir_graph *irg   = get_irn_irg(bl);
	ir_node *frame  = get_irg_frame(irg);
	ir_node *insert = bl;
Sebastian Hack's avatar
Sebastian Hack committed
883
	ir_node *spill;
Sebastian Hack's avatar
Sebastian Hack committed
884

Sebastian Hack's avatar
Sebastian Hack committed
885
886
887
888
889
	const arch_register_class_t *cls       = arch_get_irn_reg_class(arch_env, irn, -1);
	const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);

	spill = be_new_Spill(cls, cls_frame, irg, bl, frame, irn, ctx);

Sebastian Hack's avatar
Sebastian Hack committed
890
891
892
	/*
	 * search the right insertion point. a spill of a phi cannot be put
	 * directly after the phi, if there are some phis behind the one which
Sebastian Hack's avatar
Sebastian Hack committed
893
894
	 * is spilled. Also, a spill of a Proj must be after all Projs of the
	 * same tuple node.
895
	 *
Sebastian Hack's avatar
Sebastian Hack committed
896
897
	 * Here's one special case:
	 * If the spill is in the start block, the spill must be after the frame
898
899
	 * pointer is set up. This is done by setting insert to the end of the block
	 * which is its default initialization (see above).
Sebastian Hack's avatar
Sebastian Hack committed
900
	 */
901
902

	insert = sched_next(irn);
903
	if(insert != bl && bl == get_irg_start_block(irg) && sched_get_time_step(frame) >= sched_get_time_step(insert))
904
905
906
907
		insert = sched_next(frame);

	while((is_Phi(insert) || is_Proj(insert)) && !sched_is_end(insert))
		insert = sched_next(insert);
Sebastian Hack's avatar
Sebastian Hack committed
908

Sebastian Hack's avatar
Sebastian Hack committed
909
	sched_add_before(insert, spill);
Christian Würdig's avatar
Christian Würdig committed
910
	return spill;
Sebastian Hack's avatar
Sebastian Hack committed
911
912
}

913
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)
Sebastian Hack's avatar
Sebastian Hack committed
914
915
916
{
	ir_node *reload;

917
	ir_node *bl    = is_Block(insert) ? insert : get_nodes_block(insert);
Sebastian Hack's avatar
Sebastian Hack committed
918
919
920
	ir_graph *irg  = get_irn_irg(bl);
	ir_node *frame = get_irg_frame(irg);
	const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
Sebastian Hack's avatar
Sebastian Hack committed
921

Sebastian Hack's avatar
Sebastian Hack committed
922
	assert(be_is_Spill(spill) || (is_Phi(spill) && get_irn_mode(spill) == mode_M));
Sebastian Hack's avatar
Sebastian Hack committed
923

Sebastian Hack's avatar
Sebastian Hack committed
924
	reload = be_new_Reload(cls, cls_frame, irg, bl, frame, spill, mode);
Sebastian Hack's avatar
Sebastian Hack committed
925

926
927
928
929
930
931
932
933
	if(is_Block(insert)) {
		insert = sched_skip(insert, 0, sched_skip_cf_predicator, (void *) arch_env);
		sched_add_after(insert, reload);
	}

	else
		sched_add_before(insert, reload);

Christian Würdig's avatar
Christian Würdig committed
934
	return reload;
Sebastian Hack's avatar
Sebastian Hack committed
935
}
Sebastian Hack's avatar
Sebastian Hack committed
936

Sebastian Hack's avatar
Sebastian Hack committed
937
938
939
940
941
942
943
944
945
946
947
/*
  ____              ____
 |  _ \ ___  __ _  |  _ \ ___  __ _ ___
 | |_) / _ \/ _` | | |_) / _ \/ _` / __|
 |  _ <  __/ (_| | |  _ <  __/ (_| \__ \
 |_| \_\___|\__, | |_| \_\___|\__, |___/
            |___/                |_|

*/


Sebastian Hack's avatar
Sebastian Hack committed
948
static void *put_out_reg_req(arch_register_req_t *req, const ir_node *irn, int out_pos)
Sebastian Hack's avatar
Sebastian Hack committed
949
{
Sebastian Hack's avatar
Sebastian Hack committed
950
	const be_node_attr_t *a = get_irn_attr(irn);
Daniel Grund's avatar
Daniel Grund committed
951

Sebastian Hack's avatar
Sebastian Hack committed
952
	if(out_pos < a->max_reg_data) {
Sebastian Hack's avatar
Sebastian Hack committed
953
		memcpy(req, &a->reg_data[out_pos].req, sizeof(req[0]));
Sebastian Hack's avatar
Sebastian Hack committed
954
955
956

		if(be_is_Copy(irn)) {
			req->type |= arch_register_req_type_should_be_same;
957
			req->other_same = be_get_Copy_op(irn);
Sebastian Hack's avatar
Sebastian Hack committed
958
959
		}
	}
Sebastian Hack's avatar
Sebastian Hack committed
960
961
962
	else {
		req->type = arch_register_req_type_none;
		req->cls  = NULL;
Sebastian Hack's avatar
Sebastian Hack committed
963
	}
Sebastian Hack's avatar
Sebastian Hack committed
964

Sebastian Hack's avatar
Sebastian Hack committed
965
	return req;
Sebastian Hack's avatar
Sebastian Hack committed
966
967
}

Sebastian Hack's avatar
Sebastian Hack committed
968
static void *put_in_reg_req(arch_register_req_t *req, const ir_node *irn, int pos)
969
{
Sebastian Hack's avatar
Sebastian Hack committed
970
971
	const be_node_attr_t *a = get_irn_attr(irn);

Sebastian Hack's avatar
Sebastian Hack committed
972
	if(pos < get_irn_arity(irn) && pos < a->max_reg_data)
Sebastian Hack's avatar
Sebastian Hack committed
973
974
975
976
		memcpy(req, &a->reg_data[pos].in_req, sizeof(req[0]));
	else {
		req->type = arch_register_req_type_none;
		req->cls  = NULL;
Sebastian Hack's avatar
Sebastian Hack committed
977
978
979
	}

	return req;