benode.c 35.9 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"
26
#include "error.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
41
#include "be_t.h"
#include "belive_t.h"
42
#include "besched.h"
43
#include "benode.h"
44
#include "bearch.h"
Sebastian Hack's avatar
Sebastian Hack committed
45

46
47
#include "beirgmod.h"

48
49
50
51
typedef struct be_node_attr_t {
	except_attr  exc;
} be_node_attr_t;

52
53
54
55
56
typedef struct {
	be_node_attr_t base;
	bool           setup_stackframe; /**< set stackframe up */
} be_start_attr_t;

57
58
/** The be_Return nodes attribute type. */
typedef struct {
59
	be_node_attr_t base;
Matthias Braun's avatar
Matthias Braun committed
60
61
62
	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 */
63
	bool           destroy_stackframe; /**< if set destroys the stackframe */
64
65
} be_return_attr_t;

Michael Beck's avatar
Michael Beck committed
66
/** The be_IncSP attribute type. */
Sebastian Hack's avatar
Sebastian Hack committed
67
typedef struct {
68
	be_node_attr_t base;
Matthias Braun's avatar
Matthias Braun committed
69
70
71
72
	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
73
} be_incsp_attr_t;
Sebastian Hack's avatar
Sebastian Hack committed
74

75
/** The be_Frame attribute type. */
76
typedef struct {
77
	be_node_attr_t  base;
78
79
	ir_entity      *ent;
	int             offset;
80
81
} be_frame_attr_t;

82
/** The be_Call attribute type. */
83
typedef struct {
84
	be_node_attr_t  base;
Matthias Braun's avatar
Matthias Braun committed
85
	ir_entity      *ent;        /**< called entity if this is a static call. */
86
	unsigned        pop;
Matthias Braun's avatar
Matthias Braun committed
87
	ir_type        *call_tp;    /**< call type, copied from the original Call */
88
89
} be_call_attr_t;

90
typedef struct {
91
92
93
	be_node_attr_t base;
	ir_entity    **in_entities;
	ir_entity    **out_entities;
94
	int            offset;
Matthias Braun's avatar
Matthias Braun committed
95
} be_memperm_attr_t;
96

97
static unsigned be_opcode_start;
98
ir_op *op_be_Perm;
Matthias Braun's avatar
Matthias Braun committed
99
ir_op *op_be_MemPerm;
100
101
ir_op *op_be_Copy;
ir_op *op_be_Keep;
Sebastian Hack's avatar
Sebastian Hack committed
102
ir_op *op_be_CopyKeep;
103
104
105
ir_op *op_be_Call;
ir_op *op_be_Return;
ir_op *op_be_IncSP;
Sebastian Hack's avatar
Sebastian Hack committed
106
ir_op *op_be_AddSP;
Michael Beck's avatar
Michael Beck committed
107
ir_op *op_be_SubSP;
108
ir_op *op_be_Start;
109
ir_op *op_be_FrameAddr;
Sebastian Hack's avatar
Sebastian Hack committed
110

111
112
#define be_op_tag FOURCC('B', 'E', '\0', '\0')

Michael Beck's avatar
Michael Beck committed
113
/**
Michael Beck's avatar
Michael Beck committed
114
 * Compare the attributes of two be_FrameAddr nodes.
Michael Beck's avatar
Michael Beck committed
115
 *
Michael Beck's avatar
Michael Beck committed
116
 * @return zero if both nodes have identically attributes
Michael Beck's avatar
Michael Beck committed
117
 */
Michael Beck's avatar
Michael Beck committed
118
static int FrameAddr_cmp_attr(const ir_node *a, const ir_node *b)
119
{
120
121
	const be_frame_attr_t *a_attr = (const be_frame_attr_t*)get_irn_generic_attr_const(a);
	const be_frame_attr_t *b_attr = (const be_frame_attr_t*)get_irn_generic_attr_const(b);
Michael Beck's avatar
Michael Beck committed
122

123
124
125
	if (a_attr->ent != b_attr->ent || a_attr->offset != b_attr->offset)
		return 1;

126
	return be_nodes_equal(a, b);
127
128
}

Michael Beck's avatar
Michael Beck committed
129
130
131
132
133
/**
 * Compare the attributes of two be_Return nodes.
 *
 * @return zero if both nodes have identically attributes
 */
Michael Beck's avatar
Michael Beck committed
134
static int Return_cmp_attr(const ir_node *a, const ir_node *b)
135
{
136
137
	const be_return_attr_t *a_attr = (const be_return_attr_t*)get_irn_generic_attr_const(a);
	const be_return_attr_t *b_attr = (const be_return_attr_t*)get_irn_generic_attr_const(b);
138
139
140

	if (a_attr->num_ret_vals != b_attr->num_ret_vals)
		return 1;
141
142
	if (a_attr->pop != b_attr->pop)
		return 1;
143
144
	if (a_attr->emit_pop != b_attr->emit_pop)
		return 1;
145

146
	return be_nodes_equal(a, b);
147
148
}

Michael Beck's avatar
Michael Beck committed
149
150
151
152
153
/**
 * Compare the attributes of two be_IncSP nodes.
 *
 * @return zero if both nodes have identically attributes
 */
Michael Beck's avatar
Michael Beck committed
154
static int IncSP_cmp_attr(const ir_node *a, const ir_node *b)
155
{
156
157
	const be_incsp_attr_t *a_attr = (const be_incsp_attr_t*)get_irn_generic_attr_const(a);
	const be_incsp_attr_t *b_attr = (const be_incsp_attr_t*)get_irn_generic_attr_const(b);
158
159
160
161

	if (a_attr->offset != b_attr->offset)
		return 1;

162
	return be_nodes_equal(a, b);
163
164
}

Michael Beck's avatar
Michael Beck committed
165
166
167
168
169
/**
 * Compare the attributes of two be_Call nodes.
 *
 * @return zero if both nodes have identically attributes
 */
Michael Beck's avatar
Michael Beck committed
170
static int Call_cmp_attr(const ir_node *a, const ir_node *b)
171
{
172
173
	const be_call_attr_t *a_attr = (const be_call_attr_t*)get_irn_generic_attr_const(a);
	const be_call_attr_t *b_attr = (const be_call_attr_t*)get_irn_generic_attr_const(b);
174
175

	if (a_attr->ent != b_attr->ent ||
176
		a_attr->call_tp != b_attr->call_tp)
177
178
		return 1;

179
	return be_nodes_equal(a, b);
Michael Beck's avatar
Michael Beck committed
180
181
}

182
static arch_register_req_t *allocate_reg_req(ir_graph *const irg)
Matthias Braun's avatar
Matthias Braun committed
183
{
184
	struct obstack *obst = be_get_be_obst(irg);
Matthias Braun's avatar
Matthias Braun committed
185

186
	arch_register_req_t *req = OALLOCZ(obst, arch_register_req_t);
187
188
	return req;
}
Matthias Braun's avatar
Matthias Braun committed
189

190
191
void be_set_constr_in(ir_node *node, int pos, const arch_register_req_t *req)
{
192
193
194
	backend_info_t *info = be_get_info(node);
	assert(pos < get_irn_arity(node));
	info->in_reqs[pos] = req;
195
}
Matthias Braun's avatar
Matthias Braun committed
196

197
198
199
200
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
201
202
}

203
/**
204
 * Initializes the generic attribute of all be nodes and return it.
205
 */
206
static void init_node_attr(ir_node *node, int n_inputs, int n_outputs)
Sebastian Hack's avatar
Sebastian Hack committed
207
{
208
209
	assert(n_outputs >= 0);

210
	ir_graph       *irg  = get_irn_irg(node);
211
	struct obstack *obst = be_get_be_obst(irg);
212
	backend_info_t *info = be_get_info(node);
213
	const arch_register_req_t **in_reqs;
Sebastian Hack's avatar
Sebastian Hack committed
214

215
216
	if (n_inputs >= 0) {
		int i;
217
218
		assert(n_inputs == get_irn_arity(node));
		in_reqs = OALLOCN(obst, const arch_register_req_t*, n_inputs);
219
		for (i = 0; i < n_inputs; ++i) {
220
			in_reqs[i] = arch_no_register_req;
221
222
		}
	} else {
223
		in_reqs = NEW_ARR_F(const arch_register_req_t*, 0);
224
	}
225
	info->in_reqs = in_reqs;
226

227
	info->out_infos = NEW_ARR_DZ(reg_out_info_t, obst, n_outputs);
228
229
	for (int i = 0; i < n_outputs; ++i) {
		info->out_infos[i].req = arch_no_register_req;
Sebastian Hack's avatar
Sebastian Hack committed
230
231
	}
}
232

233
static void add_register_req_in(ir_node *node, const arch_register_req_t *req)
234
{
235
	backend_info_t *info = be_get_info(node);
236
	ARR_APP1(const arch_register_req_t*, info->in_reqs, req);
237
238
}

239
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
240
{
241
242
243
	int            i;
	ir_graph       *irg = get_Block_irg(block);
	be_node_attr_t *attr;
244

245
	ir_node *irn = new_ir_node(NULL, irg, block, op_be_Perm, mode_T, n, in);
246
	init_node_attr(irn, n, n);
247
248
	attr                = (be_node_attr_t*) get_irn_generic_attr(irn);
	attr->exc.pin_state = op_pin_state_pinned;
249
	for (i = 0; i < n; ++i) {
250
251
252
253
254
255
		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 {
256
			arch_register_req_t *const new_req = allocate_reg_req(irg);
257
258
259
260
261
262
			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
263
264
	}

Sebastian Hack's avatar
Sebastian Hack committed
265
266
	return irn;
}
267

268
269
void be_Perm_reduce(ir_node *perm, int new_size, int *map)
{
270
271
272
273
274
	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);
275
	ir_node        **new_in;
276
	int              i;
277
278

	assert(be_is_Perm(perm));
279
	assert(new_size <= arity);
280

281
	new_in = ALLOCAN(ir_node*, new_size);
282

283
	/* save the old register data */
284
	memcpy(old_in_reqs, info->in_reqs, arity * sizeof(old_in_reqs[0]));
285
	memcpy(old_infos, info->out_infos, arity * sizeof(old_infos[0]));
286

Matthias Braun's avatar
Matthias Braun committed
287
	/* compose the new in array and set the new register data directly */
288
	for (i = 0; i < new_size; ++i) {
289
		int idx = map[i];
290
		new_in[i]          = get_irn_n(perm, idx);
291
		info->in_reqs[i]   = old_in_reqs[idx];
292
		info->out_infos[i] = old_infos[idx];
293
294
295
296
297
	}

	set_irn_in(perm, new_size, new_in);
}

298
ir_node *be_new_MemPerm(ir_node *const block, int n, ir_node *const *const in)
Matthias Braun's avatar
Matthias Braun committed
299
{
300
301
	ir_graph                     *irg       = get_Block_irg(block);
	const arch_env_t             *arch_env  = be_get_irg_arch_env(irg);
302
303
304
305
306
	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
307

308
	real_in = ALLOCAN(ir_node*, n + 1);
309
310
311
	real_in[0] = frame;
	memcpy(&real_in[1], in, n * sizeof(real_in[0]));

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

314
	init_node_attr(irn, n + 1, n);
315
	be_node_set_reg_class_in(irn, 0, sp->reg_class);
Matthias Braun's avatar
Matthias Braun committed
316

317
	attr               = (be_memperm_attr_t*)get_irn_generic_attr(irn);
318
319
	attr->in_entities  = OALLOCNZ(get_irg_obstack(irg), ir_entity*, n);
	attr->out_entities = OALLOCNZ(get_irg_obstack(irg), ir_entity*, n);
320
	attr->offset       = 0;
Matthias Braun's avatar
Matthias Braun committed
321
322
323
324

	return irn;
}

325
ir_node *be_new_Copy(ir_node *bl, ir_node *op)
Sebastian Hack's avatar
Sebastian Hack committed
326
327
328
{
	ir_node *in[1];
	ir_node *res;
329
	be_node_attr_t *attr;
330
	ir_graph *irg = get_Block_irg(bl);
331
332
	const arch_register_req_t   *in_req = arch_get_irn_register_req(op);
	const arch_register_class_t *cls    = in_req->cls;
333

Sebastian Hack's avatar
Sebastian Hack committed
334
	in[0] = op;
335
	res   = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
336
	init_node_attr(res, 1, 1);
337
338
	attr = (be_node_attr_t*) get_irn_generic_attr(res);
	attr->exc.pin_state = op_pin_state_floats;
339
340
	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
341

342
	arch_register_req_t *const req = allocate_reg_req(irg);
343
	req->cls        = cls;
344
345
	req->type       = arch_register_req_type_should_be_same
		| (in_req->type & arch_register_req_type_aligned);
346
	req->other_same = 1U << 0;
347
	req->width      = in_req->width;
348
	be_set_constr_out(res, 0, req);
Matthias Braun's avatar
Matthias Braun committed
349

Sebastian Hack's avatar
Sebastian Hack committed
350
	return res;
351
352
}

353
354
ir_node *be_get_Copy_op(const ir_node *cpy)
{
355
	return get_irn_n(cpy, n_be_Copy_op);
356
357
}

358
ir_node *be_new_Keep(ir_node *const block, int const n, ir_node *const *const in)
Sebastian Hack's avatar
Sebastian Hack committed
359
{
Sebastian Hack's avatar
Sebastian Hack committed
360
	int i;
361
	ir_node *res;
362
	ir_graph *irg = get_Block_irg(block);
363
	be_node_attr_t *attr;
364

365
366
	res = new_ir_node(NULL, irg, block, op_be_Keep, mode_ANY, -1, NULL);
	init_node_attr(res, -1, 1);
367
368
	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
369

370
	for (i = 0; i < n; ++i) {
371
372
373
374
375
		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
376
	}
377
378
379
380
381
382
383
384
	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));
385
386
	add_irn_n(keep, node);
	add_register_req_in(keep, cls->class_req);
Sebastian Hack's avatar
Sebastian Hack committed
387
388
}

389
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
390
{
391
	be_call_attr_t *a;
392
	int real_n = n_be_Call_first_arg + n;
393
	ir_node **real_in = ALLOCAN(ir_node*, real_n);
Sebastian Hack's avatar
Sebastian Hack committed
394

395
396
397
398
	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
399

400
401
	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);
402
	init_node_attr(irn, real_n, n_outs);
403
404
405
406
407
	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;
408
409
	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
410
	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
411
412
}

413
414
ir_entity *be_Call_get_entity(const ir_node *call)
{
415
	const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
416
417
418
419
	assert(be_is_Call(call));
	return a->ent;
}

420
421
void be_Call_set_entity(ir_node *call, ir_entity *ent)
{
422
	be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
423
424
425
426
	assert(be_is_Call(call));
	a->ent = ent;
}

427
428
ir_type *be_Call_get_type(ir_node *call)
{
429
	const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
430
431
432
433
	assert(be_is_Call(call));
	return a->call_tp;
}

434
435
void be_Call_set_type(ir_node *call, ir_type *call_tp)
{
436
	be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
437
438
439
440
	assert(be_is_Call(call));
	a->call_tp = call_tp;
}

441
442
void be_Call_set_pop(ir_node *call, unsigned pop)
{
443
	be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
Matthias Braun's avatar
Matthias Braun committed
444
445
446
	a->pop = pop;
}

447
448
unsigned be_Call_get_pop(const ir_node *call)
{
449
	const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
Matthias Braun's avatar
Matthias Braun committed
450
451
452
	return a->pop;
}

453
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
454
{
455
456
	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);
457
	init_node_attr(res, n, 1);
458
	be_set_constr_out(res, 0, arch_no_register_req);
459

460
	be_return_attr_t *const a = (be_return_attr_t*)get_irn_generic_attr(res);
461
462
463
464
	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
465

466
	return res;
Sebastian Hack's avatar
Sebastian Hack committed
467
468
}

469
470
471
472
473
474
475
void be_return_set_destroy_stackframe(ir_node *node, bool value)
{
	be_return_attr_t *attr = (be_return_attr_t*) get_irn_generic_attr(node);
	assert(be_is_Return(node));
	attr->destroy_stackframe = value;
}

476
477
int be_Return_get_n_rets(const ir_node *ret)
{
478
	const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
479
480
481
	return a->num_ret_vals;
}

482
483
unsigned be_Return_get_pop(const ir_node *ret)
{
484
	const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
485
486
487
	return a->pop;
}

488
489
int be_Return_get_emit_pop(const ir_node *ret)
{
490
	const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
491
492
493
	return a->emit_pop;
}

494
495
void be_Return_set_emit_pop(ir_node *ret, int emit_pop)
{
496
	be_return_attr_t *a = (be_return_attr_t*)get_irn_generic_attr(ret);
497
498
499
	a->emit_pop = emit_pop;
}

500
ir_node *be_new_IncSP(const arch_register_t *sp, ir_node *bl,
501
                      ir_node *old_sp, int offset, int align)
Sebastian Hack's avatar
Sebastian Hack committed
502
{
Michael Beck's avatar
Michael Beck committed
503
	be_incsp_attr_t *a;
Sebastian Hack's avatar
Sebastian Hack committed
504
	ir_node *irn;
Sebastian Hack's avatar
Sebastian Hack committed
505
	ir_node *in[1];
506
	ir_graph *irg = get_Block_irg(bl);
Sebastian Hack's avatar
Sebastian Hack committed
507
508

	in[0]     = old_sp;
509
	irn       = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode,
510
	                        ARRAY_SIZE(in), in);
511
	init_node_attr(irn, 1, 1);
512
513
514
515
	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
516
517

	/* Set output constraint to stack register. */
518
519
	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
520
521

	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
522
}
Sebastian Hack's avatar
Sebastian Hack committed
523

524
525
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
526
{
Sebastian Hack's avatar
Sebastian Hack committed
527
	ir_node *irn;
528
	ir_node *in[n_be_AddSP_last];
529
	ir_graph *irg;
530
	be_node_attr_t *attr;
Sebastian Hack's avatar
Sebastian Hack committed
531

532
533
	in[n_be_AddSP_old_sp] = old_sp;
	in[n_be_AddSP_size]   = sz;
Sebastian Hack's avatar
Sebastian Hack committed
534

535
	irg = get_Block_irg(bl);
536
537
	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);
538
539
	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
540

Sebastian Hack's avatar
Sebastian Hack committed
541
	/* Set output constraint to stack register. */
542
	be_set_constr_single_reg_in(irn, n_be_AddSP_old_sp, sp,
543
	                            arch_register_req_type_none);
544
	be_node_set_reg_class_in(irn, n_be_AddSP_size, sp->reg_class);
Matthias Braun's avatar
Matthias Braun committed
545
546
	be_set_constr_single_reg_out(irn, pn_be_AddSP_sp, sp,
	                             arch_register_req_type_produces_sp);
547

Sebastian Hack's avatar
Sebastian Hack committed
548
549
550
	return irn;
}

551
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
552
553
{
	ir_node *irn;
554
	ir_node *in[n_be_SubSP_last];
555
	ir_graph *irg;
556
	be_node_attr_t *attr;
Michael Beck's avatar
Michael Beck committed
557

558
559
	in[n_be_SubSP_old_sp] = old_sp;
	in[n_be_SubSP_size]   = sz;
Michael Beck's avatar
Michael Beck committed
560

561
	irg = get_Block_irg(bl);
562
563
	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);
564
565
	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
566
567

	/* Set output constraint to stack register. */
568
	be_set_constr_single_reg_in(irn, n_be_SubSP_old_sp, sp,
569
	                            arch_register_req_type_none);
570
	be_node_set_reg_class_in(irn, n_be_SubSP_size, sp->reg_class);
571
	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
572
573
574
575

	return irn;
}

576
ir_node *be_new_Start(dbg_info *dbgi, ir_node *bl, int n_outs)
Sebastian Hack's avatar
Sebastian Hack committed
577
{
578
	ir_node *res;
579
	ir_graph *irg = get_Block_irg(bl);
580
	be_node_attr_t *attr;
Sebastian Hack's avatar
Sebastian Hack committed
581

582
	res = new_ir_node(dbgi, irg, bl, op_be_Start, mode_T, 0, NULL);
583
	init_node_attr(res, 0, n_outs);
584
585
	attr = (be_node_attr_t*) get_irn_generic_attr(res);
	attr->exc.pin_state = op_pin_state_pinned;
586
587
588
589

	return res;
}

590
591
592
593
594
595
596
void be_start_set_setup_stackframe(ir_node *node, bool value)
{
	be_start_attr_t *attr = (be_start_attr_t*) get_irn_generic_attr(node);
	assert(be_is_Start(node));
	attr->setup_stackframe = value;
}

597
ir_node *be_new_FrameAddr(const arch_register_class_t *cls_frame, ir_node *bl, ir_node *frame, ir_entity *ent)
Sebastian Hack's avatar
Sebastian Hack committed
598
{
599
	be_frame_attr_t *a;
Sebastian Hack's avatar
Sebastian Hack committed
600
601
	ir_node *irn;
	ir_node *in[1];
602
	ir_graph *irg = get_Block_irg(bl);
Sebastian Hack's avatar
Sebastian Hack committed
603

604
	in[0]  = frame;
605
	irn    = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
606
	init_node_attr(irn, 1, 1);
607
608
609
610
	a                     = (be_frame_attr_t*)get_irn_generic_attr(irn);
	a->ent                = ent;
	a->offset             = 0;
	a->base.exc.pin_state = op_pin_state_floats;
611
612
	be_node_set_reg_class_in(irn, 0, cls_frame);
	be_node_set_reg_class_out(irn, 0, cls_frame);
Michael Beck's avatar
Michael Beck committed
613
614

	return optimize_node(irn);
Sebastian Hack's avatar
Sebastian Hack committed
615
616
}

617
618
ir_node *be_get_FrameAddr_frame(const ir_node *node)
{
Michael Beck's avatar
Michael Beck committed
619
	assert(be_is_FrameAddr(node));
620
	return get_irn_n(node, n_be_FrameAddr_ptr);
Michael Beck's avatar
Michael Beck committed
621
622
}

Matthias Braun's avatar
Matthias Braun committed
623
624
ir_entity *be_get_FrameAddr_entity(const ir_node *node)
{
625
	const be_frame_attr_t *attr = (const be_frame_attr_t*)get_irn_generic_attr_const(node);
Matthias Braun's avatar
Matthias Braun committed
626
627
628
	return attr->ent;
}

629
ir_node *be_new_CopyKeep(ir_node *bl, ir_node *src, int n, ir_node *in_keep[])
Sebastian Hack's avatar
Sebastian Hack committed
630
{
631
632
	ir_node  *irn;
	ir_node **in = ALLOCAN(ir_node*, n + 1);
633
	ir_graph *irg = get_Block_irg(bl);
634
635
636
	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);
637
	be_node_attr_t *attr;
Sebastian Hack's avatar
Sebastian Hack committed
638
639
640
641

	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);
642
	init_node_attr(irn, n + 1, 1);
643
644
	attr = (be_node_attr_t*) get_irn_generic_attr(irn);
	attr->exc.pin_state = op_pin_state_floats;
645
646
	be_node_set_reg_class_in(irn, 0, cls);
	be_node_set_reg_class_out(irn, 0, cls);
647
648
649
650
651
652
	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
653
654
655
656

	return irn;
}

657
ir_node *be_new_CopyKeep_single(ir_node *bl, ir_node *src, ir_node *keep)
Sebastian Hack's avatar
Sebastian Hack committed
658
{
659
	return be_new_CopyKeep(bl, src, 1, &keep);
Sebastian Hack's avatar
Sebastian Hack committed
660
661
}

662
663
ir_node *be_get_CopyKeep_op(const ir_node *cpy)
{
664
	return get_irn_n(cpy, n_be_CopyKeep_op);
665
666
}

667
668
void be_set_CopyKeep_op(ir_node *cpy, ir_node *op)
{
669
	set_irn_n(cpy, n_be_CopyKeep_op, op);
670
671
}

672
static bool be_has_frame_entity(const ir_node *irn)
673
{
674
	return be_is_FrameAddr(irn);
675
676
}

677
void be_set_MemPerm_in_entity(const ir_node *irn, int n, ir_entity *ent)
Matthias Braun's avatar
Matthias Braun committed
678
{
679
	const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
680
681

	assert(be_is_MemPerm(irn));
682
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
683
684
685
686

	attr->in_entities[n] = ent;
}

687
ir_entity* be_get_MemPerm_in_entity(const ir_node* irn, int n)
Matthias Braun's avatar
Matthias Braun committed
688
{
689
	const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
690
691

	assert(be_is_MemPerm(irn));
692
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
693
694
695
696

	return attr->in_entities[n];
}

697
void be_set_MemPerm_out_entity(const ir_node *irn, int n, ir_entity *ent)
Matthias Braun's avatar
Matthias Braun committed
698
{
699
	const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
700
701

	assert(be_is_MemPerm(irn));
702
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
703
704
705
706

	attr->out_entities[n] = ent;
}

707
ir_entity* be_get_MemPerm_out_entity(const ir_node* irn, int n)
Matthias Braun's avatar
Matthias Braun committed
708
{
709
	const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
710
711

	assert(be_is_MemPerm(irn));
712
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
713
714
715
716

	return attr->out_entities[n];
}

717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
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;
}

733
734
735
736
737
int be_get_MemPerm_entity_arity(const ir_node *irn)
{
	return get_irn_arity(irn) - 1;
}

738
const arch_register_req_t *be_create_reg_req(struct obstack *obst,
739
		const arch_register_t *reg, arch_register_req_type_t additional_types)
Sebastian Hack's avatar
Sebastian Hack committed
740
{
741
	arch_register_req_t         *req = OALLOC(obst, arch_register_req_t);
742
	const arch_register_class_t *cls = reg->reg_class;
743
	unsigned                    *limited_bitset;
Sebastian Hack's avatar
Sebastian Hack committed
744

Matthias Braun's avatar
Matthias Braun committed
745
	limited_bitset = rbitset_obstack_alloc(obst, arch_register_class_n_regs(cls));
746
	rbitset_set(limited_bitset, reg->index);
Sebastian Hack's avatar
Sebastian Hack committed
747

748
749
	req->type    = arch_register_req_type_limited | additional_types;
	req->cls     = cls;
Matthias Braun's avatar
Matthias Braun committed
750
	req->limited = limited_bitset;
751
	req->width   = 1;
752
	return req;
753
754
755
756
757
}

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

760
761
762
763
	if (additional_types == 0) {
		req = reg->single_req;
	} else {
		ir_graph       *irg  = get_irn_irg(node);
764
		struct obstack *obst = be_get_be_obst(irg);
765
		req = be_create_reg_req(obst, reg, additional_types);
766
767
	}
	be_set_constr_in(node, pos, req);
768
769
770
771
772
}

void be_set_constr_single_reg_out(ir_node *node, int pos,
		const arch_register_t *reg, arch_register_req_type_t additional_types)
{
773
774
	ir_graph                  *irg  = get_irn_irg(node);
	be_irg_t                  *birg = be_birg_from_irg(irg);
775
	const arch_register_req_t *req;
776
777

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

782
783
784
	if (additional_types == 0) {
		req = reg->single_req;
	} else {
785
		struct obstack *obst = be_get_be_obst(irg);
786
		req = be_create_reg_req(obst, reg, additional_types);
787
788
	}

789
	arch_set_irn_register_out(node, pos, reg);
790
	be_set_constr_out(node, pos, req);