benode.c 32 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
2
/*
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
Christian Würdig's avatar
Christian Würdig committed
4
5
 */

Sebastian Hack's avatar
Sebastian Hack committed
6
/**
Christian Würdig's avatar
Christian Würdig committed
7
8
9
10
 * @file
 * @brief       Backend node support for generic backend nodes.
 * @author      Sebastian Hack
 * @date        17.05.2005
Sebastian Hack's avatar
Sebastian Hack committed
11
 *
12
 * Backend node support for generic backend nodes.
13
 * This file provides Perm, and Copy nodes.
Sebastian Hack's avatar
Sebastian Hack committed
14
15
16
 */
#include <stdlib.h>

17
#include "beirg.h"
Sebastian Hack's avatar
Sebastian Hack committed
18
19
20
#include "obst.h"
#include "set.h"
#include "pmap.h"
21
#include "util.h"
Sebastian Hack's avatar
Sebastian Hack committed
22
#include "debug.h"
23
#include "fourcc.h"
Sebastian Hack's avatar
Sebastian Hack committed
24
#include "bitfiddle.h"
Matthias Braun's avatar
Matthias Braun committed
25
#include "raw_bitset.h"
Matthias Braun's avatar
Matthias Braun committed
26
#include "panic.h"
27
#include "array.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"
Michael Beck's avatar
Michael Beck committed
35
#include "iropt_t.h"
36
37
#include "irbackedge_t.h"
#include "irverify_t.h"
38
#include "irgopt.h"
Sebastian Hack's avatar
Sebastian Hack committed
39

40
#include "be_t.h"
Matthias Braun's avatar
Matthias Braun committed
41
#include "belive.h"
42
#include "besched.h"
43
#include "benode.h"
44
#include "bearch.h"
Sebastian Hack's avatar
Sebastian Hack committed
45

46
47
48
49
typedef struct be_node_attr_t {
	except_attr  exc;
} be_node_attr_t;

50
51
/** The be_Return nodes attribute type. */
typedef struct {
52
	be_node_attr_t base;
Matthias Braun's avatar
Matthias Braun committed
53
54
55
	int            num_ret_vals; /**< number of return values */
	unsigned       pop;          /**< number of bytes that should be popped */
	int            emit_pop;     /**< if set, emit pop bytes, even if pop = 0 */
56
57
} be_return_attr_t;

Michael Beck's avatar
Michael Beck committed
58
/** The be_IncSP attribute type. */
Sebastian Hack's avatar
Sebastian Hack committed
59
typedef struct {
60
	be_node_attr_t base;
Matthias Braun's avatar
Matthias Braun committed
61
62
63
64
	int            offset;    /**< The offset by which the stack shall be
	                               expanded/shrinked. */
	int            align;     /**< whether stack should be aligned after the
	                               IncSP */
Michael Beck's avatar
Michael Beck committed
65
} be_incsp_attr_t;
Sebastian Hack's avatar
Sebastian Hack committed
66

67
/** The be_Call attribute type. */
68
typedef struct {
69
	be_node_attr_t  base;
Matthias Braun's avatar
Matthias Braun committed
70
	ir_entity      *ent;        /**< called entity if this is a static call. */
71
	unsigned        pop;
Matthias Braun's avatar
Matthias Braun committed
72
	ir_type        *call_tp;    /**< call type, copied from the original Call */
73
74
} be_call_attr_t;

75
typedef struct {
76
77
78
	be_node_attr_t base;
	ir_entity    **in_entities;
	ir_entity    **out_entities;
79
	int            offset;
Matthias Braun's avatar
Matthias Braun committed
80
} be_memperm_attr_t;
81

82
static unsigned be_opcode_start;
83
ir_op *op_be_Perm;
Matthias Braun's avatar
Matthias Braun committed
84
ir_op *op_be_MemPerm;
85
86
ir_op *op_be_Copy;
ir_op *op_be_Keep;
Sebastian Hack's avatar
Sebastian Hack committed
87
ir_op *op_be_CopyKeep;
88
89
90
ir_op *op_be_Call;
ir_op *op_be_Return;
ir_op *op_be_IncSP;
Sebastian Hack's avatar
Sebastian Hack committed
91
ir_op *op_be_AddSP;
Michael Beck's avatar
Michael Beck committed
92
ir_op *op_be_SubSP;
93
ir_op *op_be_Start;
Sebastian Hack's avatar
Sebastian Hack committed
94

95
96
#define be_op_tag FOURCC('B', 'E', '\0', '\0')

Michael Beck's avatar
Michael Beck committed
97
98
99
/**
 * Compare the attributes of two be_Return nodes.
 */
100
static int be_return_attrs_equal(const ir_node *a, const ir_node *b)
101
{
102
103
104
105
106
	const be_return_attr_t *attr_a = (const be_return_attr_t*)get_irn_generic_attr_const(a);
	const be_return_attr_t *attr_b = (const be_return_attr_t*)get_irn_generic_attr_const(b);
	return attr_a->num_ret_vals == attr_b->num_ret_vals
	    && attr_a->pop == attr_b->pop && attr_a->emit_pop == attr_b->emit_pop
	    && attrs_equal_be_node(a, b);
107
108
}

Michael Beck's avatar
Michael Beck committed
109
110
111
/**
 * Compare the attributes of two be_IncSP nodes.
 */
112
static int be_incsp_attrs_equal(const ir_node *a, const ir_node *b)
113
{
114
115
116
	const be_incsp_attr_t *attr_a = (const be_incsp_attr_t*)get_irn_generic_attr_const(a);
	const be_incsp_attr_t *attr_b = (const be_incsp_attr_t*)get_irn_generic_attr_const(b);
	return attr_a->offset == attr_b->offset && attrs_equal_be_node(a, b);
117
118
}

Michael Beck's avatar
Michael Beck committed
119
120
121
/**
 * Compare the attributes of two be_Call nodes.
 */
122
static int be_call_attrs_equal(const ir_node *a, const ir_node *b)
123
{
124
125
126
127
	const be_call_attr_t *attr_a = (const be_call_attr_t*)get_irn_generic_attr_const(a);
	const be_call_attr_t *attr_b = (const be_call_attr_t*)get_irn_generic_attr_const(b);
	return attr_a->ent == attr_b->ent && attr_a->call_tp == attr_b->call_tp
	    && attrs_equal_be_node(a, b);
Michael Beck's avatar
Michael Beck committed
128
129
}

130
static arch_register_req_t *allocate_reg_req(ir_graph *const irg)
Matthias Braun's avatar
Matthias Braun committed
131
{
132
	struct obstack *obst = be_get_be_obst(irg);
Matthias Braun's avatar
Matthias Braun committed
133

134
	arch_register_req_t *req = OALLOCZ(obst, arch_register_req_t);
135
136
	return req;
}
Matthias Braun's avatar
Matthias Braun committed
137

138
139
void be_set_constr_in(ir_node *node, int pos, const arch_register_req_t *req)
{
140
141
142
	backend_info_t *info = be_get_info(node);
	assert(pos < get_irn_arity(node));
	info->in_reqs[pos] = req;
143
}
Matthias Braun's avatar
Matthias Braun committed
144

145
146
147
148
void be_set_constr_out(ir_node *node, int pos, const arch_register_req_t *req)
{
	backend_info_t *info = be_get_info(node);
	info->out_infos[pos].req = req;
Matthias Braun's avatar
Matthias Braun committed
149
150
}

151
/**
152
 * Initializes the generic attribute of all be nodes and return it.
153
 */
154
static void init_node_attr(ir_node *node, int n_inputs, int n_outputs)
Sebastian Hack's avatar
Sebastian Hack committed
155
{
156
157
	assert(n_outputs >= 0);

158
	ir_graph       *irg  = get_irn_irg(node);
159
	struct obstack *obst = be_get_be_obst(irg);
160
	backend_info_t *info = be_get_info(node);
161
	const arch_register_req_t **in_reqs;
Sebastian Hack's avatar
Sebastian Hack committed
162

163
164
	if (n_inputs >= 0) {
		int i;
165
166
		assert(n_inputs == get_irn_arity(node));
		in_reqs = OALLOCN(obst, const arch_register_req_t*, n_inputs);
167
		for (i = 0; i < n_inputs; ++i) {
168
			in_reqs[i] = arch_no_register_req;
169
170
		}
	} else {
171
		in_reqs = NEW_ARR_F(const arch_register_req_t*, 0);
172
	}
173
	info->in_reqs = in_reqs;
174

175
	info->out_infos = NEW_ARR_DZ(reg_out_info_t, obst, n_outputs);
176
177
	for (int i = 0; i < n_outputs; ++i) {
		info->out_infos[i].req = arch_no_register_req;
Sebastian Hack's avatar
Sebastian Hack committed
178
179
	}
}
180

181
static void add_register_req_in(ir_node *node, const arch_register_req_t *req)
182
{
183
	backend_info_t *info = be_get_info(node);
184
	ARR_APP1(const arch_register_req_t*, info->in_reqs, req);
185
186
}

187
ir_node *be_new_Perm(arch_register_class_t const *const cls, ir_node *const block, int const n, ir_node *const *const in)
Sebastian Hack's avatar
Sebastian Hack committed
188
{
189
190
191
	int            i;
	ir_graph       *irg = get_Block_irg(block);
	be_node_attr_t *attr;
192

193
	ir_node *irn = new_ir_node(NULL, irg, block, op_be_Perm, mode_T, n, in);
194
	init_node_attr(irn, n, n);
195
196
	attr                = (be_node_attr_t*) get_irn_generic_attr(irn);
	attr->exc.pin_state = op_pin_state_pinned;
197
	for (i = 0; i < n; ++i) {
198
199
200
201
202
203
		const ir_node             *input = in[i];
		const arch_register_req_t *req   = arch_get_irn_register_req(input);
		if (req->width == 1) {
			be_set_constr_in(irn, i, cls->class_req);
			be_set_constr_out(irn, i, cls->class_req);
		} else {
204
			arch_register_req_t *const new_req = allocate_reg_req(irg);
205
206
207
208
209
210
			new_req->cls   = cls;
			new_req->type  = (req->type & arch_register_req_type_aligned);
			new_req->width = req->width;
			be_set_constr_in(irn, i, new_req);
			be_set_constr_out(irn, i, new_req);
		}
Sebastian Hack's avatar
Sebastian Hack committed
211
212
	}

Sebastian Hack's avatar
Sebastian Hack committed
213
214
	return irn;
}
215

216
217
void be_Perm_reduce(ir_node *perm, int new_size, int *map)
{
218
219
220
221
222
	int             arity      = get_irn_arity(perm);
	const arch_register_req_t **old_in_reqs
		= ALLOCAN(const arch_register_req_t*, arity);
	reg_out_info_t  *old_infos = ALLOCAN(reg_out_info_t, arity);
	backend_info_t  *info      = be_get_info(perm);
223
	ir_node        **new_in;
224
	int              i;
225
226

	assert(be_is_Perm(perm));
227
	assert(new_size <= arity);
228

229
	new_in = ALLOCAN(ir_node*, new_size);
230

231
	/* save the old register data */
232
	memcpy(old_in_reqs, info->in_reqs, arity * sizeof(old_in_reqs[0]));
233
	memcpy(old_infos, info->out_infos, arity * sizeof(old_infos[0]));
234

Matthias Braun's avatar
Matthias Braun committed
235
	/* compose the new in array and set the new register data directly */
236
	for (i = 0; i < new_size; ++i) {
237
		int idx = map[i];
238
		new_in[i]          = get_irn_n(perm, idx);
239
		info->in_reqs[i]   = old_in_reqs[idx];
240
		info->out_infos[i] = old_infos[idx];
241
242
243
244
245
	}

	set_irn_in(perm, new_size, new_in);
}

246
ir_node *be_new_MemPerm(ir_node *const block, int n, ir_node *const *const in)
Matthias Braun's avatar
Matthias Braun committed
247
{
248
249
	ir_graph                     *irg       = get_Block_irg(block);
	const arch_env_t             *arch_env  = be_get_irg_arch_env(irg);
250
251
252
253
254
	ir_node                      *frame     = get_irg_frame(irg);
	const arch_register_t        *sp        = arch_env->sp;
	ir_node                      *irn;
	be_memperm_attr_t            *attr;
	ir_node                     **real_in;
Matthias Braun's avatar
Matthias Braun committed
255

256
	real_in = ALLOCAN(ir_node*, n + 1);
257
258
259
	real_in[0] = frame;
	memcpy(&real_in[1], in, n * sizeof(real_in[0]));

260
	irn = new_ir_node(NULL, irg, block, op_be_MemPerm, mode_T, n+1, real_in);
261

262
	init_node_attr(irn, n + 1, n);
263
	be_node_set_reg_class_in(irn, 0, sp->reg_class);
Matthias Braun's avatar
Matthias Braun committed
264

265
	attr               = (be_memperm_attr_t*)get_irn_generic_attr(irn);
266
267
	attr->in_entities  = OALLOCNZ(get_irg_obstack(irg), ir_entity*, n);
	attr->out_entities = OALLOCNZ(get_irg_obstack(irg), ir_entity*, n);
268
	attr->offset       = 0;
Matthias Braun's avatar
Matthias Braun committed
269
270
271
272

	return irn;
}

273
ir_node *be_new_Copy(ir_node *bl, ir_node *op)
Sebastian Hack's avatar
Sebastian Hack committed
274
275
276
{
	ir_node *in[1];
	ir_node *res;
277
	be_node_attr_t *attr;
278
	ir_graph *irg = get_Block_irg(bl);
279
280
	const arch_register_req_t   *in_req = arch_get_irn_register_req(op);
	const arch_register_class_t *cls    = in_req->cls;
281

Sebastian Hack's avatar
Sebastian Hack committed
282
	in[0] = op;
283
	res   = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
284
	init_node_attr(res, 1, 1);
285
286
	attr = (be_node_attr_t*) get_irn_generic_attr(res);
	attr->exc.pin_state = op_pin_state_floats;
287
288
	be_node_set_reg_class_in(res, 0, cls);
	be_node_set_reg_class_out(res, 0, cls);
Matthias Braun's avatar
Matthias Braun committed
289

290
	arch_register_req_t *const req = allocate_reg_req(irg);
291
	req->cls        = cls;
292
293
	req->type       = arch_register_req_type_should_be_same
		| (in_req->type & arch_register_req_type_aligned);
294
	req->other_same = 1U << 0;
295
	req->width      = in_req->width;
296
	be_set_constr_out(res, 0, req);
Matthias Braun's avatar
Matthias Braun committed
297

Sebastian Hack's avatar
Sebastian Hack committed
298
	return res;
299
300
}

301
302
ir_node *be_get_Copy_op(const ir_node *cpy)
{
303
	return get_irn_n(cpy, n_be_Copy_op);
304
305
}

306
ir_node *be_new_Keep(ir_node *const block, int const n, ir_node *const *const in)
Sebastian Hack's avatar
Sebastian Hack committed
307
{
Sebastian Hack's avatar
Sebastian Hack committed
308
	int i;
309
	ir_node *res;
310
	ir_graph *irg = get_Block_irg(block);
311
	be_node_attr_t *attr;
312

313
314
	res = new_ir_node(NULL, irg, block, op_be_Keep, mode_ANY, -1, NULL);
	init_node_attr(res, -1, 1);
315
316
	attr = (be_node_attr_t*) get_irn_generic_attr(res);
	attr->exc.pin_state = op_pin_state_pinned;
Sebastian Hack's avatar
Sebastian Hack committed
317

318
	for (i = 0; i < n; ++i) {
319
320
321
322
323
		ir_node *pred = in[i];
		add_irn_n(res, pred);
		const arch_register_req_t *req = arch_get_irn_register_req(pred);
		req = req->cls != NULL ? req->cls->class_req : arch_no_register_req;
		add_register_req_in(res, req);
Sebastian Hack's avatar
Sebastian Hack committed
324
	}
325
326
327
328
329
330
331
332
	keep_alive(res);

	return res;
}

void be_Keep_add_node(ir_node *keep, const arch_register_class_t *cls, ir_node *node)
{
	assert(be_is_Keep(keep));
333
334
	add_irn_n(keep, node);
	add_register_req_in(keep, cls->class_req);
Sebastian Hack's avatar
Sebastian Hack committed
335
336
}

337
ir_node *be_new_Call(dbg_info *const dbg, ir_node *const bl, ir_node *const mem, arch_register_req_t const *const sp_req, ir_node *const sp, arch_register_req_t const *const ptr_req, ir_node *const ptr, int const n_outs, int const n, ir_node *const *const in, ir_type *const call_tp)
Sebastian Hack's avatar
Sebastian Hack committed
338
{
339
	be_call_attr_t *a;
340
	int real_n = n_be_Call_first_arg + n;
341
	ir_node **real_in = ALLOCAN(ir_node*, real_n);
Sebastian Hack's avatar
Sebastian Hack committed
342

343
344
345
346
	real_in[n_be_Call_mem] = mem;
	real_in[n_be_Call_sp]  = sp;
	real_in[n_be_Call_ptr] = ptr;
	memcpy(&real_in[n_be_Call_first_arg], in, n * sizeof(in[0]));
Sebastian Hack's avatar
Sebastian Hack committed
347

348
349
	ir_graph *const irg = get_Block_irg(bl);
	ir_node  *const irn = new_ir_node(dbg, irg, bl, op_be_Call, mode_T, real_n, real_in);
350
	init_node_attr(irn, real_n, n_outs);
351
352
353
354
355
	a                     = (be_call_attr_t*)get_irn_generic_attr(irn);
	a->ent                = NULL;
	a->call_tp            = call_tp;
	a->pop                = 0;
	a->base.exc.pin_state = op_pin_state_pinned;
356
357
	be_set_constr_in(irn, n_be_Call_sp, sp_req);
	be_set_constr_in(irn, n_be_Call_ptr, ptr_req);
Sebastian Hack's avatar
Sebastian Hack committed
358
	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
359
360
}

361
362
ir_entity *be_Call_get_entity(const ir_node *call)
{
363
	const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
364
365
366
367
	assert(be_is_Call(call));
	return a->ent;
}

368
369
void be_Call_set_entity(ir_node *call, ir_entity *ent)
{
370
	be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
371
372
373
374
	assert(be_is_Call(call));
	a->ent = ent;
}

375
376
ir_type *be_Call_get_type(ir_node *call)
{
377
	const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
378
379
380
381
	assert(be_is_Call(call));
	return a->call_tp;
}

382
383
void be_Call_set_type(ir_node *call, ir_type *call_tp)
{
384
	be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
385
386
387
388
	assert(be_is_Call(call));
	a->call_tp = call_tp;
}

389
390
void be_Call_set_pop(ir_node *call, unsigned pop)
{
391
	be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
Matthias Braun's avatar
Matthias Braun committed
392
393
394
	a->pop = pop;
}

395
396
unsigned be_Call_get_pop(const ir_node *call)
{
397
	const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
Matthias Braun's avatar
Matthias Braun committed
398
399
400
	return a->pop;
}

401
ir_node *be_new_Return(dbg_info *const dbg, ir_node *const block, int const n_res, unsigned const pop, int const n, ir_node *const *const in)
Sebastian Hack's avatar
Sebastian Hack committed
402
{
403
404
	ir_graph *const irg = get_Block_irg(block);
	ir_node  *const res = new_ir_node(dbg, irg, block, op_be_Return, mode_X, n, in);
405
	init_node_attr(res, n, 1);
406
	be_set_constr_out(res, 0, arch_no_register_req);
407

408
	be_return_attr_t *const a = (be_return_attr_t*)get_irn_generic_attr(res);
409
410
411
412
	a->num_ret_vals       = n_res;
	a->pop                = pop;
	a->emit_pop           = 0;
	a->base.exc.pin_state = op_pin_state_pinned;
Sebastian Hack's avatar
Sebastian Hack committed
413

414
	return res;
Sebastian Hack's avatar
Sebastian Hack committed
415
416
}

417
418
int be_Return_get_n_rets(const ir_node *ret)
{
419
	const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
420
421
422
	return a->num_ret_vals;
}

423
424
unsigned be_Return_get_pop(const ir_node *ret)
{
425
	const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
426
427
428
	return a->pop;
}

429
430
int be_Return_get_emit_pop(const ir_node *ret)
{
431
	const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
432
433
434
	return a->emit_pop;
}

435
436
void be_Return_set_emit_pop(ir_node *ret, int emit_pop)
{
437
	be_return_attr_t *a = (be_return_attr_t*)get_irn_generic_attr(ret);
438
439
440
	a->emit_pop = emit_pop;
}

441
ir_node *be_new_IncSP(const arch_register_t *sp, ir_node *bl,
442
                      ir_node *old_sp, int offset, int align)
Sebastian Hack's avatar
Sebastian Hack committed
443
{
Michael Beck's avatar
Michael Beck committed
444
	be_incsp_attr_t *a;
Sebastian Hack's avatar
Sebastian Hack committed
445
	ir_node *irn;
Sebastian Hack's avatar
Sebastian Hack committed
446
	ir_node *in[1];
447
	ir_graph *irg = get_Block_irg(bl);
Sebastian Hack's avatar
Sebastian Hack committed
448
449

	in[0]     = old_sp;
450
	irn       = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode,
451
	                        ARRAY_SIZE(in), in);
452
	init_node_attr(irn, 1, 1);
453
454
455
456
	a                     = (be_incsp_attr_t*)get_irn_generic_attr(irn);
	a->offset             = offset;
	a->align              = align;
	a->base.exc.pin_state = op_pin_state_pinned;
Sebastian Hack's avatar
Sebastian Hack committed
457
458

	/* Set output constraint to stack register. */
459
460
	be_node_set_reg_class_in(irn, 0, sp->reg_class);
	be_set_constr_single_reg_out(irn, 0, sp, arch_register_req_type_produces_sp);
Sebastian Hack's avatar
Sebastian Hack committed
461
462

	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
463
}
Sebastian Hack's avatar
Sebastian Hack committed
464

465
466
ir_node *be_new_AddSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp,
		ir_node *sz)
Sebastian Hack's avatar
Sebastian Hack committed
467
{
Sebastian Hack's avatar
Sebastian Hack committed
468
	ir_node *irn;
469
	ir_node *in[n_be_AddSP_last];
470
	ir_graph *irg;
471
	be_node_attr_t *attr;
Sebastian Hack's avatar
Sebastian Hack committed
472

473
474
	in[n_be_AddSP_old_sp] = old_sp;
	in[n_be_AddSP_size]   = sz;
Sebastian Hack's avatar
Sebastian Hack committed
475

476
	irg = get_Block_irg(bl);
477
478
	irn = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T, n_be_AddSP_last, in);
	init_node_attr(irn, n_be_AddSP_last, pn_be_AddSP_last);
479
480
	attr = (be_node_attr_t*) get_irn_generic_attr(irn);
	attr->exc.pin_state = op_pin_state_pinned;
Sebastian Hack's avatar
Sebastian Hack committed
481

Sebastian Hack's avatar
Sebastian Hack committed
482
	/* Set output constraint to stack register. */
483
	be_set_constr_single_reg_in(irn, n_be_AddSP_old_sp, sp,
484
	                            arch_register_req_type_none);
485
	be_node_set_reg_class_in(irn, n_be_AddSP_size, sp->reg_class);
Matthias Braun's avatar
Matthias Braun committed
486
487
	be_set_constr_single_reg_out(irn, pn_be_AddSP_sp, sp,
	                             arch_register_req_type_produces_sp);
488

Sebastian Hack's avatar
Sebastian Hack committed
489
490
491
	return irn;
}

492
ir_node *be_new_SubSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp, ir_node *sz)
Michael Beck's avatar
Michael Beck committed
493
494
{
	ir_node *irn;
495
	ir_node *in[n_be_SubSP_last];
496
	ir_graph *irg;
497
	be_node_attr_t *attr;
Michael Beck's avatar
Michael Beck committed
498

499
500
	in[n_be_SubSP_old_sp] = old_sp;
	in[n_be_SubSP_size]   = sz;
Michael Beck's avatar
Michael Beck committed
501

502
	irg = get_Block_irg(bl);
503
504
	irn = new_ir_node(NULL, irg, bl, op_be_SubSP, mode_T, n_be_SubSP_last, in);
	init_node_attr(irn, n_be_SubSP_last, pn_be_SubSP_last);
505
506
	attr = (be_node_attr_t*) get_irn_generic_attr(irn);
	attr->exc.pin_state = op_pin_state_pinned;
Michael Beck's avatar
Michael Beck committed
507
508

	/* Set output constraint to stack register. */
509
	be_set_constr_single_reg_in(irn, n_be_SubSP_old_sp, sp,
510
	                            arch_register_req_type_none);
511
	be_node_set_reg_class_in(irn, n_be_SubSP_size, sp->reg_class);
512
	be_set_constr_single_reg_out(irn, pn_be_SubSP_sp, sp, arch_register_req_type_produces_sp);
Michael Beck's avatar
Michael Beck committed
513
514
515
516

	return irn;
}

517
ir_node *be_new_Start(dbg_info *dbgi, ir_node *bl, int n_outs)
Sebastian Hack's avatar
Sebastian Hack committed
518
{
519
	ir_node *res;
520
	ir_graph *irg = get_Block_irg(bl);
521
	be_node_attr_t *attr;
Sebastian Hack's avatar
Sebastian Hack committed
522

523
	res = new_ir_node(dbgi, irg, bl, op_be_Start, mode_T, 0, NULL);
524
	init_node_attr(res, 0, n_outs);
525
526
	attr = (be_node_attr_t*) get_irn_generic_attr(res);
	attr->exc.pin_state = op_pin_state_pinned;
527
528
529
530

	return res;
}

531
ir_node *be_new_CopyKeep(ir_node *bl, ir_node *src, int n, ir_node *in_keep[])
Sebastian Hack's avatar
Sebastian Hack committed
532
{
533
534
	ir_node  *irn;
	ir_node **in = ALLOCAN(ir_node*, n + 1);
535
	ir_graph *irg = get_Block_irg(bl);
536
537
538
	const arch_register_req_t   *req  = arch_get_irn_register_req(src);
	const arch_register_class_t *cls  = req->cls;
	ir_mode                     *mode = get_irn_mode(src);
539
	be_node_attr_t *attr;
Sebastian Hack's avatar
Sebastian Hack committed
540
541
542
543

	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);
544
	init_node_attr(irn, n + 1, 1);
545
546
	attr = (be_node_attr_t*) get_irn_generic_attr(irn);
	attr->exc.pin_state = op_pin_state_floats;
547
548
	be_node_set_reg_class_in(irn, 0, cls);
	be_node_set_reg_class_out(irn, 0, cls);
549
550
551
552
553
554
	for (int i = 0; i < n; ++i) {
		ir_node *pred = in_keep[i];
		const arch_register_req_t *req = arch_get_irn_register_req(pred);
		req = req->cls != NULL ? req->cls->class_req : arch_no_register_req;
		be_set_constr_in(irn, i+1, req);
	}
Sebastian Hack's avatar
Sebastian Hack committed
555
556
557
558

	return irn;
}

559
ir_node *be_new_CopyKeep_single(ir_node *bl, ir_node *src, ir_node *keep)
Sebastian Hack's avatar
Sebastian Hack committed
560
{
561
	return be_new_CopyKeep(bl, src, 1, &keep);
Sebastian Hack's avatar
Sebastian Hack committed
562
563
}

564
565
ir_node *be_get_CopyKeep_op(const ir_node *cpy)
{
566
	return get_irn_n(cpy, n_be_CopyKeep_op);
567
568
}

569
570
void be_set_CopyKeep_op(ir_node *cpy, ir_node *op)
{
571
	set_irn_n(cpy, n_be_CopyKeep_op, op);
572
573
}

574
void be_set_MemPerm_in_entity(const ir_node *irn, int n, ir_entity *ent)
Matthias Braun's avatar
Matthias Braun committed
575
{
576
	const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
577
578

	assert(be_is_MemPerm(irn));
579
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
580
581
582
583

	attr->in_entities[n] = ent;
}

584
ir_entity* be_get_MemPerm_in_entity(const ir_node* irn, int n)
Matthias Braun's avatar
Matthias Braun committed
585
{
586
	const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
587
588

	assert(be_is_MemPerm(irn));
589
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
590
591
592
593

	return attr->in_entities[n];
}

594
void be_set_MemPerm_out_entity(const ir_node *irn, int n, ir_entity *ent)
Matthias Braun's avatar
Matthias Braun committed
595
{
596
	const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
597
598

	assert(be_is_MemPerm(irn));
599
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
600
601
602
603

	attr->out_entities[n] = ent;
}

604
ir_entity* be_get_MemPerm_out_entity(const ir_node* irn, int n)
Matthias Braun's avatar
Matthias Braun committed
605
{
606
	const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
607
608

	assert(be_is_MemPerm(irn));
609
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
610
611
612
613

	return attr->out_entities[n];
}

614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
void be_set_MemPerm_offset(ir_node *irn, int offset)
{
	be_memperm_attr_t *attr = (be_memperm_attr_t*)get_irn_generic_attr(irn);

	assert(be_is_MemPerm(irn));
	attr->offset = offset;
}

int be_get_MemPerm_offset(const ir_node *irn)
{
	const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);

	assert(be_is_MemPerm(irn));
	return attr->offset;
}

630
631
632
633
634
int be_get_MemPerm_entity_arity(const ir_node *irn)
{
	return get_irn_arity(irn) - 1;
}

635
const arch_register_req_t *be_create_reg_req(struct obstack *obst,
636
		const arch_register_t *reg, arch_register_req_type_t additional_types)
Sebastian Hack's avatar
Sebastian Hack committed
637
{
638
	arch_register_req_t         *req = OALLOC(obst, arch_register_req_t);
639
	const arch_register_class_t *cls = reg->reg_class;
640
	unsigned                    *limited_bitset;
Sebastian Hack's avatar
Sebastian Hack committed
641

Matthias Braun's avatar
Matthias Braun committed
642
	limited_bitset = rbitset_obstack_alloc(obst, arch_register_class_n_regs(cls));
643
	rbitset_set(limited_bitset, reg->index);
Sebastian Hack's avatar
Sebastian Hack committed
644

645
646
	req->type    = arch_register_req_type_limited | additional_types;
	req->cls     = cls;
Matthias Braun's avatar
Matthias Braun committed
647
	req->limited = limited_bitset;
648
	req->width   = 1;
649
	return req;
650
651
652
653
654
}

void be_set_constr_single_reg_in(ir_node *node, int pos,
		const arch_register_t *reg, arch_register_req_type_t additional_types)
{
655
	const arch_register_req_t *req;
656

657
658
659
660
	if (additional_types == 0) {
		req = reg->single_req;
	} else {
		ir_graph       *irg  = get_irn_irg(node);
661
		struct obstack *obst = be_get_be_obst(irg);
662
		req = be_create_reg_req(obst, reg, additional_types);
663
664
	}
	be_set_constr_in(node, pos, req);
665
666
667
668
669
}

void be_set_constr_single_reg_out(ir_node *node, int pos,
		const arch_register_t *reg, arch_register_req_type_t additional_types)
{
670
671
	ir_graph                  *irg  = get_irn_irg(node);
	be_irg_t                  *birg = be_birg_from_irg(irg);
672
	const arch_register_req_t *req;
673
674

	/* if we have an ignore register, add ignore flag and just assign it */
675
	if (!rbitset_is_set(birg->allocatable_regs, reg->global_index)) {
676
677
678
		additional_types |= arch_register_req_type_ignore;
	}

679
680
681
	if (additional_types == 0) {
		req = reg->single_req;
	} else {
682
		struct obstack *obst = be_get_be_obst(irg);
683
		req = be_create_reg_req(obst, reg, additional_types);
684
685
	}

686
	arch_set_irn_register_out(node, pos, reg);
687
	be_set_constr_out(node, pos, req);
Sebastian Hack's avatar
Sebastian Hack committed
688
689
}

690
691
void be_node_set_reg_class_in(ir_node *irn, int pos,
                              const arch_register_class_t *cls)
692
{
693
	be_set_constr_in(irn, pos, cls->class_req);
694
695
}

696
697
void be_node_set_reg_class_out(ir_node *irn, int pos,
                               const arch_register_class_t *cls)
Sebastian Hack's avatar
Sebastian Hack committed
698
{
699
	be_set_constr_out(irn, pos, cls->class_req);
Sebastian Hack's avatar
Sebastian Hack committed
700
701
}

702
703
ir_node *be_get_IncSP_pred(ir_node *irn)
{
Michael Beck's avatar
BugFix:    
Michael Beck committed
704
705
706
707
	assert(be_is_IncSP(irn));
	return get_irn_n(irn, 0);
}

708
709
void be_set_IncSP_pred(ir_node *incsp, ir_node *pred)
{
710
711
712
713
	assert(be_is_IncSP(incsp));
	set_irn_n(incsp, 0, pred);
}

714
void be_set_IncSP_offset(ir_node *irn, int offset)
Sebastian Hack's avatar
Sebastian Hack committed
715
{
716
	be_incsp_attr_t *a = (be_incsp_attr_t*)get_irn_generic_attr(irn);
Sebastian Hack's avatar
Sebastian Hack committed
717
718
719
	assert(be_is_IncSP(irn));
	a->offset = offset;
}
720

721
int be_get_IncSP_offset(const ir_node *irn)
Sebastian Hack's avatar
Sebastian Hack committed
722
{
723
	const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
Sebastian Hack's avatar
Sebastian Hack committed
724
725
726
	assert(be_is_IncSP(irn));
	return a->offset;
}
Sebastian Hack's avatar
Sebastian Hack committed
727

728
729
int be_get_IncSP_align(const ir_node *irn)
{
730
	const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
731
732
733
734
	assert(be_is_IncSP(irn));
	return a->align;
}

735
static ir_entity *be_node_get_frame_entity(const ir_node *irn)
736
{
737
	if (be_is_MemPerm(irn)) {
738
739
740
		return be_get_MemPerm_in_entity(irn, 0);
	}
	return NULL;
741
742
}

743
static void be_node_set_frame_offset(ir_node *irn, int offset)
744
{
745
746
747
	if (be_is_MemPerm(irn)) {
		be_set_MemPerm_offset(irn, offset);
	}
748
749
}

750
static int be_node_get_sp_bias(const ir_node *irn)
751
{
752
	assert(!be_is_Call(irn));
753
	if (be_is_IncSP(irn))
Matthias Braun's avatar
Matthias Braun committed
754
755
		return be_get_IncSP_offset(irn);
	return 0;
756
757
}

758
/* for be nodes */
Matthias Braun's avatar
Matthias Braun committed
759
static const arch_irn_ops_t be_node_irn_ops = {
760
	be_node_get_frame_entity,
761
	be_node_set_frame_offset,
762
	be_node_get_sp_bias,
763
764
765
	NULL,    /* get_op_estimated_cost   */
	NULL,    /* possible_memory_operand */
	NULL,    /* perform_memory_operand  */
Sebastian Hack's avatar
Sebastian Hack committed
766
};
767

768
769
770
771
772
static int get_start_reg_index(ir_graph *irg, const arch_register_t *reg)
{
	ir_node *start  = get_irg_start(irg);

	/* do a naive linear search... */
773
	be_foreach_out(start, i) {
774
775
		arch_register_req_t const *const out_req = arch_get_irn_register_req_out(start, i);
		if (!arch_register_req_is(out_req, limited))
776
			continue;
777
		if (out_req->cls != reg->reg_class)
778
779
780
781
782
783
784
785
786
787
788
789
			continue;
		if (!rbitset_is_set(out_req->limited, reg->index))
			continue;
		return i;
	}
	panic("Tried querying undefined register '%s' at Start", reg->name);
}

ir_node *be_get_initial_reg_value(ir_graph *irg, const arch_register_t *reg)
{
	int      i     = get_start_reg_index(irg, reg);
	ir_node *start = get_irg_start(irg);
790
	ir_mode *mode  = arch_register_class_mode(reg->reg_class);