benode.c 24.2 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.
 *
8
9
 * This file provdies Perm, Copy, Spill and Reload nodes.
 *
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
27
28
29

#include "irop_t.h"
#include "irmode_t.h"
#include "irnode_t.h"
30
#include "ircons_t.h"
31
#include "irprintf.h"
Sebastian Hack's avatar
Sebastian Hack committed
32

33
34
35
#include "be_t.h"
#include "belive_t.h"
#include "besched_t.h"
Sebastian Hack's avatar
Sebastian Hack committed
36
37
#include "benode_t.h"

38
39
#include "beirgmod.h"

Sebastian Hack's avatar
Sebastian Hack committed
40
41
/* 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
42

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

Sebastian Hack's avatar
Sebastian Hack committed
45
typedef enum _node_kind_t {
Christian Würdig's avatar
Christian Würdig committed
46
47
48
49
	node_kind_spill,
	node_kind_reload,
	node_kind_perm,
	node_kind_copy,
Sebastian Hack's avatar
Sebastian Hack committed
50
	node_kind_kill,
Christian Würdig's avatar
Christian Würdig committed
51
	node_kind_last
Sebastian Hack's avatar
Sebastian Hack committed
52
53
54
} node_kind_t;

typedef struct {
Christian Würdig's avatar
Christian Würdig committed
55
56
57
58
59
	node_kind_t kind;
	const arch_register_class_t *cls;
	ir_op *op;
	int n_pos;
	int *pos;
Sebastian Hack's avatar
Sebastian Hack committed
60
61
} be_op_t;

Sebastian Hack's avatar
Sebastian Hack committed
62
63
64
65
66
67
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
68
69
typedef struct {
	arch_register_req_t req;
Sebastian Hack's avatar
Sebastian Hack committed
70
	be_req_kind_t       kind;
Sebastian Hack's avatar
Sebastian Hack committed
71
	arch_irn_flags_t    flags;
Sebastian Hack's avatar
Sebastian Hack committed
72
73
74
75
76
77
78
79
	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
80
81
} be_req_t;

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

typedef struct {
Sebastian Hack's avatar
Sebastian Hack committed
89
	int                         max_reg_data;
Sebastian Hack's avatar
Sebastian Hack committed
90
91
	const arch_register_class_t *cls;
	be_reg_data_t               *reg_data;
92
93
94
} be_node_attr_t;

typedef struct {
Sebastian Hack's avatar
Sebastian Hack committed
95
	be_node_attr_t node_attr;
96
	ir_node *spill_ctx;  /**< The node in whose context this spill was introduced. */
97
	entity *ent;     /**< The entity in the stack frame the spill writes to. */
98
99
} be_spill_attr_t;

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

Sebastian Hack's avatar
Sebastian Hack committed
106
107
108
109
110
static ir_op *op_Spill;
static ir_op *op_Reload;
static ir_op *op_Perm;
static ir_op *op_Copy;
static ir_op *op_Keep;
Sebastian Hack's avatar
Sebastian Hack committed
111
static ir_op *op_Call;
Sebastian Hack's avatar
Sebastian Hack committed
112
static ir_op *op_Return;
Sebastian Hack's avatar
Sebastian Hack committed
113
114
static ir_op *op_IncSP;
static ir_op *op_AddSP;
Sebastian Hack's avatar
Sebastian Hack committed
115
116
117
static ir_op *op_RegParams;
static ir_op *op_StackParam;
static ir_op *op_NoReg;
Sebastian Hack's avatar
Sebastian Hack committed
118

Sebastian Hack's avatar
Sebastian Hack committed
119
static int beo_base = -1;
Sebastian Hack's avatar
Sebastian Hack committed
120

Sebastian Hack's avatar
Sebastian Hack committed
121
static const ir_op_ops be_node_op_ops;
Sebastian Hack's avatar
Sebastian Hack committed
122

123
124
125
126
127
128
129
130
131
132
133
#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

Sebastian Hack's avatar
Sebastian Hack committed
134
135
void be_node_init(void) {
	static int inited = 0;
Sebastian Hack's avatar
Sebastian Hack committed
136

Sebastian Hack's avatar
Sebastian Hack committed
137
138
	if(inited)
		return;
Sebastian Hack's avatar
Sebastian Hack committed
139

Sebastian Hack's avatar
Sebastian Hack committed
140
	inited = 1;
Sebastian Hack's avatar
Sebastian Hack committed
141

142
143
	/* Acquire all needed opcodes. */
	beo_base = get_next_ir_opcodes(beo_Last - 1);
Sebastian Hack's avatar
Sebastian Hack committed
144

Sebastian Hack's avatar
Sebastian Hack committed
145
146
147
148
149
150
151
	op_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);
	op_Reload     = new_ir_op(beo_base + beo_Reload,     "Reload",     op_pin_state_mem_pinned, N, oparity_zero,     0, sizeof(be_node_attr_t),  &be_node_op_ops);
	op_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);
	op_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);
	op_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);
	op_NoReg      = new_ir_op(beo_base + beo_NoReg,      "NoReg",      op_pin_state_floats,     N, oparity_zero,     0, sizeof(be_node_attr_t),  &be_node_op_ops);
	op_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);
Sebastian Hack's avatar
Sebastian Hack committed
152
	op_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);
Sebastian Hack's avatar
Sebastian Hack committed
153
154
155
156
	op_AddSP      = new_ir_op(beo_base + beo_AddSP,      "AddSP",      op_pin_state_pinned,     N, oparity_unary,    0, sizeof(be_stack_attr_t), &be_node_op_ops);
	op_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);
	op_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);
	op_StackParam = new_ir_op(beo_base + beo_StackParam, "StackParam", op_pin_state_pinned,     N, oparity_unary,    0, sizeof(be_stack_attr_t), &be_node_op_ops);
Sebastian Hack's avatar
Sebastian Hack committed
157
158
159
160
161
162
163
164

	set_op_tag(op_Spill,      &be_node_tag);
	set_op_tag(op_Reload,     &be_node_tag);
	set_op_tag(op_Perm,       &be_node_tag);
	set_op_tag(op_Copy,       &be_node_tag);
	set_op_tag(op_Keep,       &be_node_tag);
	set_op_tag(op_NoReg,      &be_node_tag);
	set_op_tag(op_Call,       &be_node_tag);
Sebastian Hack's avatar
Sebastian Hack committed
165
	set_op_tag(op_Return,     &be_node_tag);
Sebastian Hack's avatar
Sebastian Hack committed
166
167
168
169
	set_op_tag(op_AddSP,      &be_node_tag);
	set_op_tag(op_IncSP,      &be_node_tag);
	set_op_tag(op_RegParams,  &be_node_tag);
	set_op_tag(op_StackParam, &be_node_tag);
Sebastian Hack's avatar
Sebastian Hack committed
170
}
171

Sebastian Hack's avatar
Sebastian Hack committed
172
static void *init_node_attr(ir_node* irn, const arch_register_class_t *cls, ir_graph *irg, int max_reg_data)
Sebastian Hack's avatar
Sebastian Hack committed
173
174
175
{
	be_node_attr_t *a = get_irn_attr(irn);

Sebastian Hack's avatar
Sebastian Hack committed
176
177
178
	a->max_reg_data = max_reg_data;
	a->cls          = cls;
	a->reg_data     = NULL;
Sebastian Hack's avatar
Sebastian Hack committed
179

Sebastian Hack's avatar
Sebastian Hack committed
180
	if(max_reg_data > 0) {
Sebastian Hack's avatar
Sebastian Hack committed
181
182
		int i;

Sebastian Hack's avatar
Sebastian Hack committed
183
184
185
		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
186
187
			a->reg_data[i].req.req.cls  = cls;
			a->reg_data[i].req.req.type = arch_register_req_type_normal;
Sebastian Hack's avatar
Sebastian Hack committed
188
189
190
191
192
		}
	}

	return a;
}
193

194
195
static INLINE int is_be_node(const ir_node *irn)
{
Sebastian Hack's avatar
Sebastian Hack committed
196
	return get_op_tag(get_irn_op(irn)) == &be_node_tag;
197
198
}

Sebastian Hack's avatar
Sebastian Hack committed
199
be_opcode_t be_get_irn_opcode(const ir_node *irn)
200
{
Sebastian Hack's avatar
Sebastian Hack committed
201
	return is_be_node(irn) ? get_irn_opcode(irn) - beo_base : beo_NoBeOp;
202
203
}

Sebastian Hack's avatar
Sebastian Hack committed
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
static int redir_proj(const ir_node **node, int pos)
{
	const ir_node *n = *node;

	if(is_Proj(n)) {
		assert(pos == -1 && "Illegal pos for a Proj");
		*node = get_Proj_pred(n);
		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
227
	assert(out_pos < a->max_reg_data && "position too high");
Sebastian Hack's avatar
Sebastian Hack committed
228
229
230
231
	a->reg_data[out_pos].reg = reg;
}


Sebastian Hack's avatar
Sebastian Hack committed
232
ir_node *be_new_Spill(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *to_spill, ir_node *ctx)
233
{
Sebastian Hack's avatar
Sebastian Hack committed
234
235
236
	be_spill_attr_t *a;
	ir_node *in[1];
	ir_node *res;
237

Sebastian Hack's avatar
Sebastian Hack committed
238
239
240
	in[0] = to_spill;
	res   = new_ir_node(NULL, irg, bl, op_Spill, mode_M, 1, in);
	a     = init_node_attr(res, cls, irg, 0);
Sebastian Hack's avatar
Sebastian Hack committed
241
	a->ent       = NULL;
Sebastian Hack's avatar
Sebastian Hack committed
242
243
244
	a->spill_ctx = ctx;
	return res;
}
245

Sebastian Hack's avatar
Sebastian Hack committed
246
ir_node *be_new_Reload(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_mode *mode, ir_node *mem)
247
{
Sebastian Hack's avatar
Sebastian Hack committed
248
249
	ir_node *in[1];
	ir_node *res;
250

Sebastian Hack's avatar
Sebastian Hack committed
251
252
253
254
255
	in[0] = mem;
	res   = new_ir_node(NULL, irg, bl, op_Reload, mode, 1, in);
	init_node_attr(res, cls, irg, 1);
	return res;
}
256

Sebastian Hack's avatar
Sebastian Hack committed
257
258
259
260
261
262
ir_node *be_new_Perm(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
{
	ir_node *irn = new_ir_node(NULL, irg, bl, op_Perm, mode_T, n, in);
	init_node_attr(irn, cls, irg, n);
	return irn;
}
263

Sebastian Hack's avatar
Sebastian Hack committed
264
265
266
267
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;
268

Sebastian Hack's avatar
Sebastian Hack committed
269
270
271
272
	in[0] = op;
	res   = new_ir_node(NULL, irg, bl, op_Copy, get_irn_mode(op), 1, in);
	init_node_attr(res, cls, irg, 1);
	return res;
273
274
}

Sebastian Hack's avatar
Sebastian Hack committed
275
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
276
{
Sebastian Hack's avatar
Sebastian Hack committed
277
	ir_node *irn;
Sebastian Hack's avatar
Sebastian Hack committed
278

Sebastian Hack's avatar
Sebastian Hack committed
279
280
281
282
	irn = new_ir_node(NULL, irg, bl, op_Keep, mode_ANY, n, in);
	init_node_attr(irn, cls, irg, 0);
	keep_alive(irn);
	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
283
284
}

Sebastian Hack's avatar
Sebastian Hack committed
285
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[])
Sebastian Hack's avatar
Sebastian Hack committed
286
{
Sebastian Hack's avatar
Sebastian Hack committed
287
288
289
	int real_n = 3 + n;
	ir_node *irn;
	ir_node **real_in;
Sebastian Hack's avatar
Sebastian Hack committed
290

Sebastian Hack's avatar
Sebastian Hack committed
291
292
293
294
295
296
297
298
	real_in = malloc(sizeof(real_in[0]) * (real_n));

	real_in[0] = mem;
	real_in[1] = sp;
	real_in[2] = ptr;
	memcpy(&real_in[3], in, n * sizeof(in[0]));

	irn = new_ir_node(NULL, irg, bl, op_Call, mode_T, real_n, real_in);
Sebastian Hack's avatar
Sebastian Hack committed
299
	init_node_attr(irn, NULL, irg, (n_outs > real_n ? n_outs : real_n));
Sebastian Hack's avatar
Sebastian Hack committed
300
	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
301
302
}

Sebastian Hack's avatar
Sebastian Hack committed
303
304
305
306
307
308
309
310
ir_node *be_new_Return(ir_graph *irg, ir_node *bl, int n, ir_node *in[])
{
	ir_node *irn = new_ir_node(NULL, irg, bl, op_Return, mode_X, n, in);
	init_node_attr(irn, NULL, irg, n);

	return irn;
}

311
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
312
{
Sebastian Hack's avatar
Sebastian Hack committed
313
314
315
316
317
	be_stack_attr_t *a;
	ir_node *irn;
	ir_node *in[1];

	in[0]     = old_sp;
318
319
	in[1]     = mem;
	irn       = new_ir_node(NULL, irg, bl, op_IncSP, sp->reg_class->mode, 2, in);
Sebastian Hack's avatar
Sebastian Hack committed
320
321
322
323
	a         = init_node_attr(irn, sp->reg_class, irg, 1);
	a->dir    = dir;
	a->offset = offset;

Sebastian Hack's avatar
Sebastian Hack committed
324
	be_node_set_flags(irn, -1, arch_irn_flags_ignore);
Sebastian Hack's avatar
Sebastian Hack committed
325

Sebastian Hack's avatar
Sebastian Hack committed
326
327
	/* Set output constraint to stack register. */
	be_set_constr_single_reg(irn, -1, sp);
Sebastian Hack's avatar
Sebastian Hack committed
328
	be_node_set_irn_reg(NULL, irn, sp);
Sebastian Hack's avatar
Sebastian Hack committed
329
330

	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
331
}
Sebastian Hack's avatar
Sebastian Hack committed
332

Sebastian Hack's avatar
Sebastian Hack committed
333
ir_node *be_new_AddSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, ir_node *op)
Sebastian Hack's avatar
Sebastian Hack committed
334
{
Sebastian Hack's avatar
Sebastian Hack committed
335
	be_node_attr_t *a;
Sebastian Hack's avatar
Sebastian Hack committed
336
337
338
339
340
341
	ir_node *irn;
	ir_node *in[2];

	in[0]    = old_sp;
	in[1]    = op;
	irn      = new_ir_node(NULL, irg, bl, op_AddSP, sp->reg_class->mode, 2, in);
Sebastian Hack's avatar
Sebastian Hack committed
342
	a        = init_node_attr(irn, sp->reg_class, irg, 1);
Sebastian Hack's avatar
Sebastian Hack committed
343
344

	be_node_set_flags(irn, -1, arch_irn_flags_ignore);
Sebastian Hack's avatar
Sebastian Hack committed
345
346
347

	/* Set output constraint to stack register. */
	be_set_constr_single_reg(irn, -1, sp);
Sebastian Hack's avatar
Sebastian Hack committed
348
	be_node_set_irn_reg(NULL, irn, sp);
Sebastian Hack's avatar
Sebastian Hack committed
349
350

	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
351
}
Sebastian Hack's avatar
Sebastian Hack committed
352

Sebastian Hack's avatar
Sebastian Hack committed
353
ir_node *be_new_NoReg(const arch_register_t *reg, ir_graph *irg, ir_node *bl)
Sebastian Hack's avatar
Sebastian Hack committed
354
{
Sebastian Hack's avatar
Sebastian Hack committed
355
	be_node_attr_t *a;
Sebastian Hack's avatar
Sebastian Hack committed
356
357
358
359
	ir_node *irn;
	ir_node *in[1];

	irn = new_ir_node(NULL, irg, bl, op_NoReg, reg->reg_class->mode, 0, in);
Sebastian Hack's avatar
Sebastian Hack committed
360
	a   = init_node_attr(irn, reg->reg_class, irg, 1);
Sebastian Hack's avatar
Sebastian Hack committed
361
	be_node_set_flags(irn, -1, arch_irn_flags_ignore);
Sebastian Hack's avatar
Sebastian Hack committed
362
	be_set_constr_single_reg(irn, -1, reg);
Sebastian Hack's avatar
Sebastian Hack committed
363
	be_node_set_irn_reg(NULL, irn, reg);
Sebastian Hack's avatar
Sebastian Hack committed
364
	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
365
}
Sebastian Hack's avatar
Sebastian Hack committed
366

Sebastian Hack's avatar
Sebastian Hack committed
367
368
369
370
371
372
373
374
375
376
377
378
379
ir_node *be_new_StackParam(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_mode *mode, ir_node *frame_pointer, unsigned offset)
{
	be_stack_attr_t *a;
	ir_node *irn;
	ir_node *in[1];

	in[0] = frame_pointer;
	irn = new_ir_node(NULL, irg, bl, op_StackParam, mode, 1, in);
	a = init_node_attr(irn, cls, irg, 1);
	a->offset = offset;
	return irn;
}

Sebastian Hack's avatar
Sebastian Hack committed
380
381
382
383
384
385
386
387
388
389
ir_node *be_new_RegParams(ir_graph *irg, ir_node *bl, int n_outs)
{
	ir_node *irn;
	ir_node *in[1];

	irn = new_ir_node(NULL, irg, bl, op_RegParams, mode_T, 0, in);
	init_node_attr(irn, NULL, irg, n_outs);
	return irn;
}

Sebastian Hack's avatar
Sebastian Hack committed
390
391
392
393
394
395
396
397
398
399
400
401
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           ; }
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          ; }
int be_is_AddSP         (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_AddSP          ; }
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     ; }
int be_is_NoReg         (const ir_node *irn) { return be_get_irn_opcode(irn) == beo_NoReg          ; }
Sebastian Hack's avatar
Sebastian Hack committed
402

Sebastian Hack's avatar
Sebastian Hack committed
403
static void be_limited(void *data, bitset_t *bs)
Sebastian Hack's avatar
Sebastian Hack committed
404
{
Sebastian Hack's avatar
Sebastian Hack committed
405
406
	be_req_t *req = data;

Sebastian Hack's avatar
Sebastian Hack committed
407
408
409
410
411
412
413
414
415
416
417
418
	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
419
420
}

Sebastian Hack's avatar
Sebastian Hack committed
421
static INLINE be_req_t *get_req(ir_node *irn, int pos)
Sebastian Hack's avatar
Sebastian Hack committed
422
{
Michael Beck's avatar
Michael Beck committed
423
	int idx           = pos < 0 ? -(pos + 1) : pos;
Sebastian Hack's avatar
Sebastian Hack committed
424
425
426
427
428
429
	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
430
	assert(!(pos < 0)  || -(pos + 1) <= a->max_reg_data);
Sebastian Hack's avatar
Sebastian Hack committed
431

Sebastian Hack's avatar
Sebastian Hack committed
432
433
434
435
436
437
438
	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
439
440
441
442
443
444
445
446
	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;
}

447
448
void be_set_constr_limited(ir_node *irn, int pos, const arch_register_req_t *req)
{
Sebastian Hack's avatar
Sebastian Hack committed
449
	be_req_t *r = get_req(irn, pos);
450
451
452
453
454
455
456
457
458
459
460
461
462

	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
463
464
465
466
467
468
469
470
471
472
473
474
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
475
476
477
478
479
480
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;
}
481

Sebastian Hack's avatar
Sebastian Hack committed
482
unsigned be_get_IncSP_offset(ir_node *irn)
Sebastian Hack's avatar
Sebastian Hack committed
483
{
Sebastian Hack's avatar
Sebastian Hack committed
484
485
486
487
	be_stack_attr_t *a = get_irn_attr(irn);
	assert(be_is_IncSP(irn));
	return a->offset;
}
Sebastian Hack's avatar
Sebastian Hack committed
488

Sebastian Hack's avatar
Sebastian Hack committed
489
490
491
492
493
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
494
495
}

Sebastian Hack's avatar
Sebastian Hack committed
496
be_stack_dir_t be_get_IncSP_direction(ir_node *irn)
Sebastian Hack's avatar
Sebastian Hack committed
497
{
Sebastian Hack's avatar
Sebastian Hack committed
498
499
500
	be_stack_attr_t *a = get_irn_attr(irn);
	assert(be_is_IncSP(irn));
	return a->dir;
Sebastian Hack's avatar
Sebastian Hack committed
501
502
}

503
void be_set_Spill_entity(ir_node *irn, entity *ent)
504
{
Sebastian Hack's avatar
Sebastian Hack committed
505
506
	be_spill_attr_t *a = get_irn_attr(irn);
	assert(be_is_Spill(irn));
507
	a->ent = ent;
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
}

static ir_node *find_a_spill_walker(ir_node *irn, unsigned visited_nr)
{
	if(get_irn_visited(irn) < visited_nr) {
		set_irn_visited(irn, visited_nr);

		if(is_Phi(irn)) {
			int i, n;
			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;
			}
		}

Sebastian Hack's avatar
Sebastian Hack committed
524
		else if(be_get_irn_opcode(irn) == beo_Spill)
525
526
527
528
529
530
			return irn;
	}

	return NULL;
}

Sebastian Hack's avatar
Sebastian Hack committed
531
532
533
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));
534
535
536
	return a->spill_ctx;
}

537
538
539
540
541
542
543
544
545
546
547
/**
 * 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.
 */
static INLINE ir_node *find_a_spill(ir_node *irn)
{
	ir_graph *irg       = get_irn_irg(irn);
	unsigned visited_nr = get_irg_visited(irg) + 1;

Sebastian Hack's avatar
Sebastian Hack committed
548
	assert(be_is_Reload(irn));
549
550
551
552
	set_irg_visited(irg, visited_nr);
	return find_a_spill_walker(irn, visited_nr);
}

553
entity *be_get_spill_entity(ir_node *irn)
554
{
Sebastian Hack's avatar
Sebastian Hack committed
555
	int opc           = get_irn_opcode(irn);
556

Sebastian Hack's avatar
Sebastian Hack committed
557
	switch(be_get_irn_opcode(irn)) {
Sebastian Hack's avatar
Sebastian Hack committed
558
	case beo_Reload:
559
		return be_get_spill_entity(find_a_spill(irn));
Sebastian Hack's avatar
Sebastian Hack committed
560
561
562
	case beo_Spill:
		{
			be_spill_attr_t *a = get_irn_attr(irn);
563
			return a->ent;
Sebastian Hack's avatar
Sebastian Hack committed
564
		}
565
	default:
Sebastian Hack's avatar
Sebastian Hack committed
566
		assert(0 && "Must give spill/reload node");
567
568
	}

569
	return NULL;
Sebastian Hack's avatar
Sebastian Hack committed
570
571
}

Sebastian Hack's avatar
Sebastian Hack committed
572
ir_node *be_spill(const arch_env_t *arch_env, ir_node *irn, ir_node *ctx)
Sebastian Hack's avatar
Sebastian Hack committed
573
{
Christian Würdig's avatar
Christian Würdig committed
574
	const arch_register_class_t *cls = arch_get_irn_reg_class(arch_env, irn, -1);
575

Christian Würdig's avatar
Christian Würdig committed
576
577
	ir_node *bl    = get_nodes_block(irn);
	ir_graph *irg  = get_irn_irg(bl);
Sebastian Hack's avatar
Sebastian Hack committed
578
	ir_node *spill = be_new_Spill(cls, irg, bl, irn, ctx);
Sebastian Hack's avatar
Sebastian Hack committed
579
580
581
582
583
584
585
586
587
588
589
590
	ir_node *insert;

	/*
	 * 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
	 * is spilled.
	 */
	insert = sched_next(irn);
	while(is_Phi(insert) && !sched_is_end(insert))
		insert = sched_next(insert);

	sched_add_before(insert, spill);
Christian Würdig's avatar
Christian Würdig committed
591
	return spill;
Sebastian Hack's avatar
Sebastian Hack committed
592
593
}

Sebastian Hack's avatar
Sebastian Hack committed
594
595
596
ir_node *be_reload(const arch_env_t *arch_env,
				   const arch_register_class_t *cls,
				   ir_node *irn, int pos, ir_mode *mode, ir_node *spill)
Sebastian Hack's avatar
Sebastian Hack committed
597
598
599
{
	ir_node *reload;

Christian Würdig's avatar
Christian Würdig committed
600
601
	ir_node *bl   = get_nodes_block(irn);
	ir_graph *irg = get_irn_irg(bl);
Sebastian Hack's avatar
Sebastian Hack committed
602

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

Sebastian Hack's avatar
Sebastian Hack committed
605
	reload = be_new_Reload(cls, irg, bl, mode, spill);
Sebastian Hack's avatar
Sebastian Hack committed
606

Sebastian Hack's avatar
Sebastian Hack committed
607
	set_irn_n(irn, pos, reload);
Christian Würdig's avatar
Christian Würdig committed
608
609
	sched_add_before(irn, reload);
	return reload;
Sebastian Hack's avatar
Sebastian Hack committed
610
}
Sebastian Hack's avatar
Sebastian Hack committed
611

Sebastian Hack's avatar
Sebastian Hack committed
612
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
613
{
Sebastian Hack's avatar
Sebastian Hack committed
614
	const be_node_attr_t *a = get_irn_attr(irn);
Daniel Grund's avatar
Daniel Grund committed
615

Sebastian Hack's avatar
Sebastian Hack committed
616
	if(out_pos < a->max_reg_data)
Sebastian Hack's avatar
Sebastian Hack committed
617
618
619
620
		memcpy(req, &a->reg_data[out_pos].req, sizeof(req[0]));
	else {
		req->type = arch_register_req_type_none;
		req->cls  = NULL;
Sebastian Hack's avatar
Sebastian Hack committed
621
	}
Sebastian Hack's avatar
Sebastian Hack committed
622

Sebastian Hack's avatar
Sebastian Hack committed
623
	return req;
Sebastian Hack's avatar
Sebastian Hack committed
624
625
}

Sebastian Hack's avatar
Sebastian Hack committed
626
static void *put_in_reg_req(arch_register_req_t *req, const ir_node *irn, int pos)
627
{
Sebastian Hack's avatar
Sebastian Hack committed
628
629
630
	const be_node_attr_t *a = get_irn_attr(irn);
	int n                   = get_irn_arity(irn);

Sebastian Hack's avatar
Sebastian Hack committed
631
	if(pos < get_irn_arity(irn) && pos < a->max_reg_data)
Sebastian Hack's avatar
Sebastian Hack committed
632
633
634
635
		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
636
637
638
	}

	return req;
639
640
}

Sebastian Hack's avatar
Sebastian Hack committed
641
static const arch_register_req_t *
642
be_node_get_irn_reg_req(const void *self, arch_register_req_t *req, const ir_node *irn, int pos)
Sebastian Hack's avatar
Sebastian Hack committed
643
{
Sebastian Hack's avatar
Sebastian Hack committed
644
	int out_pos = pos;
Sebastian Hack's avatar
Sebastian Hack committed
645

Sebastian Hack's avatar
Sebastian Hack committed
646
647
648
	if(pos < 0) {
		if(get_irn_mode(irn) == mode_T)
			return NULL;
Daniel Grund's avatar
Daniel Grund committed
649

Sebastian Hack's avatar
Sebastian Hack committed
650
651
652
653
		out_pos = redir_proj((const ir_node **) &irn, pos);
		assert(is_be_node(irn));
		return put_out_reg_req(req, irn, out_pos);
	}
Daniel Grund's avatar
Daniel Grund committed
654

Sebastian Hack's avatar
Sebastian Hack committed
655
656
657
	else {
		return is_be_node(irn) ? put_in_reg_req(req, irn, pos) : NULL;
	}
Daniel Grund's avatar
Daniel Grund committed
658

Sebastian Hack's avatar
Sebastian Hack committed
659
	return req;
Sebastian Hack's avatar
Sebastian Hack committed
660
661
}

Sebastian Hack's avatar
Sebastian Hack committed
662
const arch_register_t *
663
be_node_get_irn_reg(const void *_self, const ir_node *irn)
Sebastian Hack's avatar
Sebastian Hack committed
664
665
666
{
	int out_pos;
	be_node_attr_t *a;
Daniel Grund's avatar
Daniel Grund committed
667

Christian Würdig's avatar
Christian Würdig committed
668
	out_pos = redir_proj((const ir_node **) &irn, -1);
Sebastian Hack's avatar
Sebastian Hack committed
669
	a       = get_irn_attr(irn);
Daniel Grund's avatar
Daniel Grund committed
670

Sebastian Hack's avatar
Sebastian Hack committed
671
	assert(is_be_node(irn));
Sebastian Hack's avatar
Sebastian Hack committed
672
	assert(out_pos < a->max_reg_data && "position too high");
Daniel Grund's avatar
Daniel Grund committed
673

Sebastian Hack's avatar
Sebastian Hack committed
674
	return a->reg_data[out_pos].reg;
Sebastian Hack's avatar
Sebastian Hack committed
675
676
}

Sebastian Hack's avatar
Sebastian Hack committed
677
static arch_irn_class_t be_node_classify(const void *_self, const ir_node *irn)
Sebastian Hack's avatar
Sebastian Hack committed
678
{
Christian Würdig's avatar
Christian Würdig committed
679
	redir_proj((const ir_node **) &irn, -1);
Sebastian Hack's avatar
Sebastian Hack committed
680

Sebastian Hack's avatar
Sebastian Hack committed
681
	switch(be_get_irn_opcode(irn)) {
Sebastian Hack's avatar
Sebastian Hack committed
682
683
684
685
686
#define XXX(a,b) case beo_ ## a: return arch_irn_class_ ## b;
		XXX(Spill, spill)
		XXX(Reload, reload)
		XXX(Perm, perm)
		XXX(Copy, copy)
Sebastian Hack's avatar
Sebastian Hack committed
687
#undef XXX
Sebastian Hack's avatar
Sebastian Hack committed
688
689
		default:
		return 0;
Sebastian Hack's avatar
Sebastian Hack committed
690
691
	}

Christian Würdig's avatar
Christian Würdig committed
692
	return 0;
Sebastian Hack's avatar
Sebastian Hack committed
693
694
}

Sebastian Hack's avatar
Sebastian Hack committed
695
static arch_irn_flags_t be_node_get_flags(const void *_self, const ir_node *irn)
Sebastian Hack's avatar
Sebastian Hack committed
696
{
Sebastian Hack's avatar
Sebastian Hack committed
697
698
699
700
701
702
	be_node_attr_t *a;

	redir_proj((const ir_node **) &irn, -1);
	assert(is_be_node(irn));
	a = get_irn_attr(irn);
	return a->max_reg_data > 0 ? a->reg_data[0].req.flags : arch_irn_flags_none;
Sebastian Hack's avatar
Sebastian Hack committed
703
704
}

705
static const arch_irn_ops_if_t be_node_irn_ops_if = {
Sebastian Hack's avatar
Sebastian Hack committed
706
707
708
709
710
711
	be_node_get_irn_reg_req,
	be_node_set_irn_reg,
	be_node_get_irn_reg,
	be_node_classify,
	be_node_get_flags,
};
712

713
714
715
716
717
static const arch_irn_ops_t be_node_irn_ops = {
	&be_node_irn_ops_if
};

const void *be_node_get_arch_ops(const arch_irn_handler_t *self, const ir_node *irn)
718
{
Sebastian Hack's avatar
Sebastian Hack committed
719
720
	redir_proj((const ir_node **) &irn, -1);
	return is_be_node(irn) ? &be_node_irn_ops : NULL;
Sebastian Hack's avatar
Sebastian Hack committed
721
722
}

Sebastian Hack's avatar
Sebastian Hack committed
723
724
725
const arch_irn_handler_t be_node_irn_handler = {
	be_node_get_arch_ops
};
Sebastian Hack's avatar
Sebastian Hack committed
726

727
728
static int dump_node(ir_node *irn, FILE *f, dump_reason_t reason)
{
Sebastian Hack's avatar
Sebastian Hack committed
729
	be_node_attr_t *at = get_irn_attr(irn);
730
731
	int i;

732
733
	assert(is_be_node(irn));

734
735
	switch(reason) {
		case dump_node_opcode_txt:
Sebastian Hack's avatar
Sebastian Hack committed
736
			fprintf(f, get_op_name(get_irn_op(irn)));
737
738
739
740
741
742
743
			break;
		case dump_node_mode_txt:
			fprintf(f, get_mode_name(get_irn_mode(irn)));
			break;
		case dump_node_nodeattr_txt:
			break;
		case dump_node_info_txt:
Sebastian Hack's avatar
Sebastian Hack committed
744
			fprintf(f, "reg class: %s\n", at->cls ? at->cls->name : "n/a");
Sebastian Hack's avatar
Sebastian Hack committed
745
			for(i = 0; i < at->max_reg_data; ++i) {
746
				const arch_register_t *reg = at->reg_data[i].reg;
747
748
749
				fprintf(f, "reg #%d: %s\n", i, reg ? reg->name : "n/a");
			}

Sebastian Hack's avatar
Sebastian Hack committed
750
			switch(be_get_irn_opcode(irn)) {
Sebastian Hack's avatar
Sebastian Hack committed
751
752
753
754
755
756
757
758
759
760
761
762
			case beo_Spill:
				{
					be_spill_attr_t *a = (be_spill_attr_t *) at;

					ir_fprintf(f, "spill context: %+F\n", a->spill_ctx);
					if (a->ent) {
						unsigned ofs = get_entity_offset_bytes(a->ent);
						ir_fprintf(f, "spill entity: %+F offset %x (%d)\n", a->ent, ofs, ofs);
					}
					else {
						ir_fprintf(f, "spill entity: n/a\n");
					}
Christian Würdig's avatar
Christian Würdig committed
763
				}
Sebastian Hack's avatar
Sebastian Hack committed
764
765
766
767
768
769
770
				break;

			case beo_IncSP:
				{
					be_stack_attr_t *a = (be_stack_attr_t *) at;
					fprintf(f, "offset: %u\n", a->offset);
					fprintf(f, "direction: %s\n", a->dir == be_stack_dir_along ? "along" : "against");
Christian Würdig's avatar
Christian Würdig committed
771
				}
Sebastian Hack's avatar
Sebastian Hack committed
772
				break;
773
			}
Sebastian Hack's avatar
Sebastian Hack committed
774

775
776
	}

777
	return 0;
778
779
}

Sebastian Hack's avatar
Sebastian Hack committed
780
781
782
783
/**
 * Copies the backend specific attributes from old node to new node.
 */
static void copy_attr(const ir_node *old_node, ir_node *new_node)
Sebastian Hack's avatar
Sebastian Hack committed
784
{
Sebastian Hack's avatar
Sebastian Hack committed
785
	be_node_attr_t *old_attr = get_irn_attr(old_node);
Sebastian Hack's avatar
Sebastian Hack committed
786
	be_node_attr_t *new_attr = get_irn_attr(new_node);
Sebastian Hack's avatar
Sebastian Hack committed
787
	int i;
Sebastian Hack's avatar
Sebastian Hack committed
788
789
790
791

	assert(is_be_node(old_node));
	assert(is_be_node(new_node));

Sebastian Hack's avatar
Sebastian Hack committed
792
793
	memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
	new_attr->reg_data = NULL;
Sebastian Hack's avatar
Sebastian Hack committed
794

Sebastian Hack's avatar
Sebastian Hack committed
795
796
797
	if(new_attr->max_reg_data > 0) {
		new_attr->reg_data = NEW_ARR_D(be_reg_data_t, get_irg_obstack(get_irn_irg(new_node)), new_attr->max_reg_data);
		memcpy(new_attr->reg_data, old_attr->reg_data, new_attr->max_reg_data * sizeof(be_reg_data_t));
Sebastian Hack's avatar
Sebastian Hack committed
798
799
800
801
802
803
804
805
806
807

		for(i = 0; i < old_attr->max_reg_data; ++i) {
			be_req_t *r;

			r = &new_attr->reg_data[i].req;
			r->req.limited_env = r;

			r = &new_attr->reg_data[i].in_req;
			r->req.limited_env = r;
		}
Sebastian Hack's avatar
Sebastian Hack committed
808
	}
Sebastian Hack's avatar
Sebastian Hack committed
809
810
}

Sebastian Hack's avatar
Sebastian Hack committed
811
812
813
814
815
816
static const ir_op_ops be_node_op_ops = {
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
Sebastian Hack's avatar
Sebastian Hack committed
817
	copy_attr,
Sebastian Hack's avatar
Sebastian Hack committed
818
819
820
821
822
823
824
825
826
	NULL,
	NULL,
	NULL,
	NULL,
	NULL,
	dump_node,
	NULL
};

Sebastian Hack's avatar
Sebastian Hack committed
827
pset *nodes_live_at(const arch_env_t *arch_env, const arch_register_class_t *cls, const ir_node *pos, pset *live)
Sebastian Hack's avatar
Sebastian Hack committed
828
{
Sebastian Hack's avatar
Sebastian Hack committed
829
	firm_dbg_module_t *dbg = firm_dbg_register("firm.be.node");
Christian Würdig's avatar
Christian Würdig committed
830
	const ir_node *bl      = is_Block(pos) ? pos : get_nodes_block(pos);
Sebastian Hack's avatar
Sebastian Hack committed
831
	ir_node *irn;
Christian Würdig's avatar
Christian Würdig committed
832
	irn_live_t *li;
Daniel Grund's avatar
Daniel Grund committed
833
834
835

	live_foreach(bl, li) {
		ir_node *irn = (ir_node *) li->irn;
Sebastian Hack's avatar
Sebastian Hack committed
836
		if(live_is_end(li) && arch_irn_consider_in_reg_alloc(arch_env, cls, irn))
Daniel Grund's avatar
Daniel Grund committed
837
838
839
			pset_insert_ptr(live, irn);
	}

Christian Würdig's avatar
Christian Würdig committed
840
	sched_foreach_reverse(bl, irn) {
Sebastian Hack's avatar
Sebastian Hack committed
841
		int i, n;
Christian Würdig's avatar
Christian Würdig committed
842
		ir_node *x;
Sebastian Hack's avatar
Sebastian Hack committed
843

Sebastian Hack's avatar
Sebastian Hack committed
844
845
846
847
		/*
		 * If we encounter the node we want to insert the Perm after,
		* exit immediately, so that this node is still live
		*/
Christian Würdig's avatar
Christian Würdig committed
848
		if(irn == pos)
Sebastian Hack's avatar
Sebastian Hack committed
849
			return live;
850

Christian Würdig's avatar
Christian Würdig committed
851
852
853
		DBG((dbg, LEVEL_1, "%+F\n", irn));
		for(x = pset_first(live); x; x = pset_next(live))
			DBG((dbg, LEVEL_1, "\tlive: %+F\n", x));
854

855
		if(arch_irn_consider_in_reg_alloc(arch_env, cls, irn))
Christian Würdig's avatar
Christian Würdig committed
856
			pset_remove_ptr(live, irn);
857

Christian Würdig's avatar
Christian Würdig committed
858
859
		for(i = 0, n = get_irn_arity(irn); i < n; ++i) {
			ir_node *op = get_irn_n(irn, i);
860

Sebastian Hack's avatar
Sebastian Hack committed
861
			if(arch_irn_consider_in_reg_alloc(arch_env, cls, op))
Christian Würdig's avatar
Christian Würdig committed
862
863
864
				pset_insert_ptr(live, op);
		}
	}
Sebastian Hack's avatar
Sebastian Hack committed
865

Christian Würdig's avatar
Christian Würdig committed
866
	return live;
Sebastian Hack's avatar
Sebastian Hack committed
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
}

ir_node *insert_Perm_after(const arch_env_t *arch_env,
						   const arch_register_class_t *cls,
						   dom_front_info_t *dom_front,
						   ir_node *pos)
{
	ir_node *bl                 = is_Block(pos) ? pos : get_nodes_block(pos);
	ir_graph *irg               = get_irn_irg(bl);
	pset *live                  = pset_new_ptr_default();
	firm_dbg_module_t *dbg      = firm_dbg_register("be.node");

	ir_node *curr, *irn, *perm, **nodes;
	int i, n;

	DBG((dbg, LEVEL_1, "Insert Perm after: %+F\n", pos));

Christian Würdig's avatar
Christian Würdig committed
884
	if(!nodes_live_at(arch_env, cls, pos, live));
Sebastian Hack's avatar
Sebastian Hack committed
885

Christian Würdig's avatar
Christian Würdig committed
886
	n = pset_count(live);
887
888
889
890

	if(n == 0)
		return NULL;

Christian Würdig's avatar
Christian Würdig committed
891
	nodes = malloc(n * sizeof(nodes[0]));
Sebastian Hack's avatar
Sebastian Hack committed
892

Christian Würdig's avatar
Christian Würdig committed
893
894
895
896
897
	DBG((dbg, LEVEL_1, "live:\n"));
	for(irn = pset_first(live), i = 0; irn; irn = pset_next(live), i++) {
		DBG((dbg, LEVEL_1, "\t%+F\n", irn));
		nodes[i] = irn;
	}
898

Sebastian Hack's avatar
Sebastian Hack committed
899
	perm = be_new_Perm(cls, irg, bl, n, nodes);
Christian Würdig's avatar
Christian Würdig committed
900
901
	sched_add_after(pos, perm);
	free(nodes);
902

Christian Würdig's avatar
Christian Würdig committed
903
904
	curr = perm;
	for(i = 0; i < n; ++i) {
Sebastian Hack's avatar
Sebastian Hack committed
905
		ir_node *copies[2];
Christian Würdig's avatar
Christian Würdig committed
906
		ir_node *perm_op = get_irn_n(perm, i);
907
		const arch_register_t *reg = arch_get_irn_register(arch_env, perm_op);
908

Christian Würdig's avatar
Christian Würdig committed
909
910
911
		ir_mode *mode = get_irn_mode(perm_op);
		ir_node *proj = new_r_Proj(irg, bl, perm, mode, i);
		arch_set_irn_register(arch_env, proj, reg);
912

Christian Würdig's avatar
Christian Würdig committed
913
914
		sched_add_after(curr, proj);
		curr = proj;
915

Sebastian Hack's avatar
Sebastian Hack committed
916
917
918
		copies[0] = perm_op;
		copies[1] = proj;
		be_ssa_constr(dom_front, 2, copies);
Christian Würdig's avatar
Christian Würdig committed
919
920
	}
	return perm;
Sebastian Hack's avatar
Sebastian Hack committed
921
}