benode.c 38.2 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.
Michael Beck's avatar
Michael Beck committed
13
 * This file provides Perm, Copy, Spill and Reload nodes.
Sebastian Hack's avatar
Sebastian Hack committed
14
 */
15
#include "config.h"
Sebastian Hack's avatar
Sebastian Hack committed
16
17
18

#include <stdlib.h>

19
#include "beirg.h"
Sebastian Hack's avatar
Sebastian Hack committed
20
21
22
#include "obst.h"
#include "set.h"
#include "pmap.h"
23
#include "util.h"
Sebastian Hack's avatar
Sebastian Hack committed
24
#include "debug.h"
25
#include "fourcc.h"
Sebastian Hack's avatar
Sebastian Hack committed
26
#include "bitfiddle.h"
Matthias Braun's avatar
Matthias Braun committed
27
#include "raw_bitset.h"
28
#include "error.h"
29
#include "array_t.h"
Sebastian Hack's avatar
Sebastian Hack committed
30
31
32
33

#include "irop_t.h"
#include "irmode_t.h"
#include "irnode_t.h"
34
#include "ircons_t.h"
35
#include "irprintf.h"
Sebastian Hack's avatar
Sebastian Hack committed
36
#include "irgwalk.h"
Michael Beck's avatar
Michael Beck committed
37
#include "iropt_t.h"
38
39
#include "irbackedge_t.h"
#include "irverify_t.h"
Sebastian Hack's avatar
Sebastian Hack committed
40

41
42
#include "be_t.h"
#include "belive_t.h"
43
#include "besched.h"
44
#include "benode.h"
45
#include "bearch.h"
Sebastian Hack's avatar
Sebastian Hack committed
46

47
48
#include "beirgmod.h"

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

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

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

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

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

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

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

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

Michael Beck's avatar
Michael Beck committed
112
/**
Michael Beck's avatar
Michael Beck committed
113
 * Compare the attributes of two be_FrameAddr nodes.
Michael Beck's avatar
Michael Beck committed
114
 *
Michael Beck's avatar
Michael Beck committed
115
 * @return zero if both nodes have identically attributes
Michael Beck's avatar
Michael Beck committed
116
 */
Michael Beck's avatar
Michael Beck committed
117
static int FrameAddr_cmp_attr(const ir_node *a, const ir_node *b)
118
{
119
120
	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
121

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

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

Michael Beck's avatar
Michael Beck committed
128
129
130
131
132
/**
 * Compare the attributes of two be_Return nodes.
 *
 * @return zero if both nodes have identically attributes
 */
Michael Beck's avatar
Michael Beck committed
133
static int Return_cmp_attr(const ir_node *a, const ir_node *b)
134
{
135
136
	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);
137
138
139

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

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

Michael Beck's avatar
Michael Beck committed
148
149
150
151
152
/**
 * Compare the attributes of two be_IncSP nodes.
 *
 * @return zero if both nodes have identically attributes
 */
Michael Beck's avatar
Michael Beck committed
153
static int IncSP_cmp_attr(const ir_node *a, const ir_node *b)
154
{
155
156
	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);
157
158
159
160

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

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

Michael Beck's avatar
Michael Beck committed
164
165
166
167
168
/**
 * Compare the attributes of two be_Call nodes.
 *
 * @return zero if both nodes have identically attributes
 */
Michael Beck's avatar
Michael Beck committed
169
static int Call_cmp_attr(const ir_node *a, const ir_node *b)
170
{
171
172
	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);
173
174

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

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

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

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

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

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

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

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

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

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

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

238
239
240
ir_node *be_new_Spill(const arch_register_class_t *cls,
		const arch_register_class_t *cls_frame, ir_node *bl,
		ir_node *frame, ir_node *to_spill)
241
{
Matthias Braun's avatar
Matthias Braun committed
242
	be_frame_attr_t *a;
243
	ir_node         *in[2];
244
	ir_node         *res;
245
	ir_graph        *irg = get_Block_irg(bl);
246

247
248
249
	in[0]     = frame;
	in[1]     = to_spill;
	res       = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
250
251
	init_node_attr(res, 2, 1);
	a         = (be_frame_attr_t*) get_irn_generic_attr(res);
252
	a->ent    = NULL;
Matthias Braun's avatar
Matthias Braun committed
253
	a->offset = 0;
254
	a->base.exc.pin_state = op_pin_state_pinned;
Sebastian Hack's avatar
Sebastian Hack committed
255

256
257
	be_node_set_reg_class_in(res, n_be_Spill_frame, cls_frame);
	be_node_set_reg_class_in(res, n_be_Spill_val, cls);
258
	arch_set_irn_register_req_out(res, 0, arch_no_register_req);
259
	arch_add_irn_flags(res, arch_irn_flags_spill);
260

Sebastian Hack's avatar
Sebastian Hack committed
261
262
	return res;
}
263

264
ir_node *be_new_Reload(const arch_register_class_t *cls,
265
		const arch_register_class_t *cls_frame, ir_node *block,
266
		ir_node *frame, ir_node *mem, ir_mode *mode)
267
{
268
269
270
	ir_node  *in[2];
	ir_node  *res;
	ir_graph *irg = get_Block_irg(block);
271
	be_frame_attr_t *a;
272
273
274

	in[0] = frame;
	in[1] = mem;
275
	res   = new_ir_node(NULL, irg, block, op_be_Reload, mode, 2, in);
276

277
	init_node_attr(res, 2, 1);
278
	be_node_set_reg_class_out(res, 0, cls);
279

280
	be_node_set_reg_class_in(res, n_be_Reload_frame, cls_frame);
281
	arch_set_irn_flags(res, arch_irn_flags_rematerializable);
282

283
284
285
286
287
	a         = (be_frame_attr_t*) get_irn_generic_attr(res);
	a->ent    = NULL;
	a->offset = 0;
	a->base.exc.pin_state = op_pin_state_pinned;

Sebastian Hack's avatar
Sebastian Hack committed
288
289
	return res;
}
290

291
ir_node *be_get_Reload_mem(const ir_node *irn)
Sebastian Hack's avatar
Sebastian Hack committed
292
293
{
	assert(be_is_Reload(irn));
294
	return get_irn_n(irn, n_be_Reload_mem);
Sebastian Hack's avatar
Sebastian Hack committed
295
296
}

297
298
299
ir_node *be_get_Reload_frame(const ir_node *irn)
{
	assert(be_is_Reload(irn));
300
	return get_irn_n(irn, n_be_Reload_frame);
301
302
}

303
304
305
ir_node *be_get_Spill_val(const ir_node *irn)
{
	assert(be_is_Spill(irn));
306
	return get_irn_n(irn, n_be_Spill_val);
307
}
Matthias Braun's avatar
Matthias Braun committed
308

309
310
311
ir_node *be_get_Spill_frame(const ir_node *irn)
{
	assert(be_is_Spill(irn));
312
	return get_irn_n(irn, n_be_Spill_frame);
313
}
314

315
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
316
{
317
318
319
	int            i;
	ir_graph       *irg = get_Block_irg(block);
	be_node_attr_t *attr;
320

321
	ir_node *irn = new_ir_node(NULL, irg, block, op_be_Perm, mode_T, n, in);
322
	init_node_attr(irn, n, n);
323
324
	attr                = (be_node_attr_t*) get_irn_generic_attr(irn);
	attr->exc.pin_state = op_pin_state_pinned;
325
	for (i = 0; i < n; ++i) {
326
327
328
329
330
331
		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 {
332
			arch_register_req_t *const new_req = allocate_reg_req(irg);
333
334
335
336
337
338
			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
339
340
	}

Sebastian Hack's avatar
Sebastian Hack committed
341
342
	return irn;
}
343

344
345
void be_Perm_reduce(ir_node *perm, int new_size, int *map)
{
346
347
348
349
350
	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);
351
	ir_node        **new_in;
352
	int              i;
353
354

	assert(be_is_Perm(perm));
355
	assert(new_size <= arity);
356

357
	new_in = ALLOCAN(ir_node*, new_size);
358

359
	/* save the old register data */
360
	memcpy(old_in_reqs, info->in_reqs, arity * sizeof(old_in_reqs[0]));
361
	memcpy(old_infos, info->out_infos, arity * sizeof(old_infos[0]));
362

Matthias Braun's avatar
Matthias Braun committed
363
	/* compose the new in array and set the new register data directly */
364
	for (i = 0; i < new_size; ++i) {
365
		int idx = map[i];
366
		new_in[i]          = get_irn_n(perm, idx);
367
		info->in_reqs[i]   = old_in_reqs[idx];
368
		info->out_infos[i] = old_infos[idx];
369
370
371
372
373
	}

	set_irn_in(perm, new_size, new_in);
}

374
ir_node *be_new_MemPerm(ir_node *const block, int n, ir_node *const *const in)
Matthias Braun's avatar
Matthias Braun committed
375
{
376
377
	ir_graph                     *irg       = get_Block_irg(block);
	const arch_env_t             *arch_env  = be_get_irg_arch_env(irg);
378
379
380
381
382
	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
383

384
	real_in = ALLOCAN(ir_node*, n + 1);
385
386
387
	real_in[0] = frame;
	memcpy(&real_in[1], in, n * sizeof(real_in[0]));

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

390
	init_node_attr(irn, n + 1, n);
391
	be_node_set_reg_class_in(irn, 0, sp->reg_class);
Matthias Braun's avatar
Matthias Braun committed
392

393
	attr               = (be_memperm_attr_t*)get_irn_generic_attr(irn);
394
395
	attr->in_entities  = OALLOCNZ(get_irg_obstack(irg), ir_entity*, n);
	attr->out_entities = OALLOCNZ(get_irg_obstack(irg), ir_entity*, n);
Matthias Braun's avatar
Matthias Braun committed
396
397
398
399

	return irn;
}

400
ir_node *be_new_Copy(ir_node *bl, ir_node *op)
Sebastian Hack's avatar
Sebastian Hack committed
401
402
403
{
	ir_node *in[1];
	ir_node *res;
404
	be_node_attr_t *attr;
405
	ir_graph *irg = get_Block_irg(bl);
406
407
	const arch_register_req_t   *in_req = arch_get_irn_register_req(op);
	const arch_register_class_t *cls    = in_req->cls;
408

Sebastian Hack's avatar
Sebastian Hack committed
409
	in[0] = op;
410
	res   = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
411
	init_node_attr(res, 1, 1);
412
413
	attr = (be_node_attr_t*) get_irn_generic_attr(res);
	attr->exc.pin_state = op_pin_state_floats;
414
415
	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
416

417
	arch_register_req_t *const req = allocate_reg_req(irg);
418
	req->cls        = cls;
419
420
	req->type       = arch_register_req_type_should_be_same
		| (in_req->type & arch_register_req_type_aligned);
421
	req->other_same = 1U << 0;
422
	req->width      = in_req->width;
423
	be_set_constr_out(res, 0, req);
Matthias Braun's avatar
Matthias Braun committed
424

Sebastian Hack's avatar
Sebastian Hack committed
425
	return res;
426
427
}

428
429
ir_node *be_get_Copy_op(const ir_node *cpy)
{
430
	return get_irn_n(cpy, n_be_Copy_op);
431
432
}

433
ir_node *be_new_Keep(ir_node *const block, int const n, ir_node *const *const in)
Sebastian Hack's avatar
Sebastian Hack committed
434
{
Sebastian Hack's avatar
Sebastian Hack committed
435
	int i;
436
	ir_node *res;
437
	ir_graph *irg = get_Block_irg(block);
438
	be_node_attr_t *attr;
439

440
441
	res = new_ir_node(NULL, irg, block, op_be_Keep, mode_ANY, -1, NULL);
	init_node_attr(res, -1, 1);
442
443
	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
444

445
	for (i = 0; i < n; ++i) {
446
447
448
449
450
		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
451
	}
452
453
454
455
456
457
458
459
	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));
460
461
	add_irn_n(keep, node);
	add_register_req_in(keep, cls->class_req);
Sebastian Hack's avatar
Sebastian Hack committed
462
463
}

464
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
465
{
466
	be_call_attr_t *a;
467
	int real_n = n_be_Call_first_arg + n;
Sebastian Hack's avatar
Sebastian Hack committed
468
	ir_node **real_in;
Sebastian Hack's avatar
Sebastian Hack committed
469

470
	NEW_ARR_A(ir_node *, real_in, real_n);
471
472
473
474
	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
475

476
477
	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);
478
	init_node_attr(irn, real_n, n_outs);
479
480
481
482
483
	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;
484
485
	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
486
	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
487
488
}

489
490
ir_entity *be_Call_get_entity(const ir_node *call)
{
491
	const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
492
493
494
495
	assert(be_is_Call(call));
	return a->ent;
}

496
497
void be_Call_set_entity(ir_node *call, ir_entity *ent)
{
498
	be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
499
500
501
502
	assert(be_is_Call(call));
	a->ent = ent;
}

503
504
ir_type *be_Call_get_type(ir_node *call)
{
505
	const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
506
507
508
509
	assert(be_is_Call(call));
	return a->call_tp;
}

510
511
void be_Call_set_type(ir_node *call, ir_type *call_tp)
{
512
	be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
513
514
515
516
	assert(be_is_Call(call));
	a->call_tp = call_tp;
}

517
518
void be_Call_set_pop(ir_node *call, unsigned pop)
{
519
	be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
Matthias Braun's avatar
Matthias Braun committed
520
521
522
	a->pop = pop;
}

523
524
unsigned be_Call_get_pop(const ir_node *call)
{
525
	const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
Matthias Braun's avatar
Matthias Braun committed
526
527
528
	return a->pop;
}

529
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
530
{
531
532
	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);
533
	init_node_attr(res, n, 1);
534
	be_set_constr_out(res, 0, arch_no_register_req);
535

536
	be_return_attr_t *const a = (be_return_attr_t*)get_irn_generic_attr(res);
537
538
539
540
	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
541

542
	return res;
Sebastian Hack's avatar
Sebastian Hack committed
543
544
}

545
546
547
548
549
550
551
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;
}

552
553
int be_Return_get_n_rets(const ir_node *ret)
{
554
	const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
555
556
557
	return a->num_ret_vals;
}

558
559
unsigned be_Return_get_pop(const ir_node *ret)
{
560
	const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
561
562
563
	return a->pop;
}

564
565
int be_Return_get_emit_pop(const ir_node *ret)
{
566
	const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
567
568
569
	return a->emit_pop;
}

570
571
void be_Return_set_emit_pop(ir_node *ret, int emit_pop)
{
572
	be_return_attr_t *a = (be_return_attr_t*)get_irn_generic_attr(ret);
573
574
575
	a->emit_pop = emit_pop;
}

576
ir_node *be_new_IncSP(const arch_register_t *sp, ir_node *bl,
577
                      ir_node *old_sp, int offset, int align)
Sebastian Hack's avatar
Sebastian Hack committed
578
{
Michael Beck's avatar
Michael Beck committed
579
	be_incsp_attr_t *a;
Sebastian Hack's avatar
Sebastian Hack committed
580
	ir_node *irn;
Sebastian Hack's avatar
Sebastian Hack committed
581
	ir_node *in[1];
582
	ir_graph *irg = get_Block_irg(bl);
Sebastian Hack's avatar
Sebastian Hack committed
583
584

	in[0]     = old_sp;
585
	irn       = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode,
586
	                        ARRAY_SIZE(in), in);
587
	init_node_attr(irn, 1, 1);
588
589
590
591
	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
592
593

	/* Set output constraint to stack register. */
594
595
	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
596
597

	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
598
}
Sebastian Hack's avatar
Sebastian Hack committed
599

600
601
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
602
{
Sebastian Hack's avatar
Sebastian Hack committed
603
	ir_node *irn;
604
	ir_node *in[n_be_AddSP_last];
605
	ir_graph *irg;
606
	be_node_attr_t *attr;
Sebastian Hack's avatar
Sebastian Hack committed
607

608
609
	in[n_be_AddSP_old_sp] = old_sp;
	in[n_be_AddSP_size]   = sz;
Sebastian Hack's avatar
Sebastian Hack committed
610

611
	irg = get_Block_irg(bl);
612
613
	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);
614
615
	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
616

Sebastian Hack's avatar
Sebastian Hack committed
617
	/* Set output constraint to stack register. */
618
	be_set_constr_single_reg_in(irn, n_be_AddSP_old_sp, sp,
619
	                            arch_register_req_type_none);
620
	be_node_set_reg_class_in(irn, n_be_AddSP_size, sp->reg_class);
Matthias Braun's avatar
Matthias Braun committed
621
622
	be_set_constr_single_reg_out(irn, pn_be_AddSP_sp, sp,
	                             arch_register_req_type_produces_sp);
623

Sebastian Hack's avatar
Sebastian Hack committed
624
625
626
	return irn;
}

627
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
628
629
{
	ir_node *irn;
630
	ir_node *in[n_be_SubSP_last];
631
	ir_graph *irg;
632
	be_node_attr_t *attr;
Michael Beck's avatar
Michael Beck committed
633

634
635
	in[n_be_SubSP_old_sp] = old_sp;
	in[n_be_SubSP_size]   = sz;
Michael Beck's avatar
Michael Beck committed
636

637
	irg = get_Block_irg(bl);
638
639
	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);
640
641
	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
642
643

	/* Set output constraint to stack register. */
644
	be_set_constr_single_reg_in(irn, n_be_SubSP_old_sp, sp,
645
	                            arch_register_req_type_none);
646
	be_node_set_reg_class_in(irn, n_be_SubSP_size, sp->reg_class);
647
	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
648
649
650
651

	return irn;
}

652
ir_node *be_new_Start(dbg_info *dbgi, ir_node *bl, int n_outs)
Sebastian Hack's avatar
Sebastian Hack committed
653
{
654
	ir_node *res;
655
	ir_graph *irg = get_Block_irg(bl);
656
	be_node_attr_t *attr;
Sebastian Hack's avatar
Sebastian Hack committed
657

658
	res = new_ir_node(dbgi, irg, bl, op_be_Start, mode_T, 0, NULL);
659
	init_node_attr(res, 0, n_outs);
660
661
	attr = (be_node_attr_t*) get_irn_generic_attr(res);
	attr->exc.pin_state = op_pin_state_pinned;
662
663
664
665

	return res;
}

666
667
668
669
670
671
672
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;
}

673
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
674
{
675
	be_frame_attr_t *a;
Sebastian Hack's avatar
Sebastian Hack committed
676
677
	ir_node *irn;
	ir_node *in[1];
678
	ir_graph *irg = get_Block_irg(bl);
Sebastian Hack's avatar
Sebastian Hack committed
679

680
	in[0]  = frame;
681
	irn    = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
682
	init_node_attr(irn, 1, 1);
683
684
685
686
	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;
687
688
	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
689
690

	return optimize_node(irn);
Sebastian Hack's avatar
Sebastian Hack committed
691
692
}

693
694
ir_node *be_get_FrameAddr_frame(const ir_node *node)
{
Michael Beck's avatar
Michael Beck committed
695
	assert(be_is_FrameAddr(node));
696
	return get_irn_n(node, n_be_FrameAddr_ptr);
Michael Beck's avatar
Michael Beck committed
697
698
}

Matthias Braun's avatar
Matthias Braun committed
699
700
ir_entity *be_get_FrameAddr_entity(const ir_node *node)
{
701
	const be_frame_attr_t *attr = (const be_frame_attr_t*)get_irn_generic_attr_const(node);
Matthias Braun's avatar
Matthias Braun committed
702
703
704
	return attr->ent;
}

705
ir_node *be_new_CopyKeep(ir_node *bl, ir_node *src, int n, ir_node *in_keep[])
Sebastian Hack's avatar
Sebastian Hack committed
706
{
707
708
	ir_node  *irn;
	ir_node **in = ALLOCAN(ir_node*, n + 1);
709
	ir_graph *irg = get_Block_irg(bl);
710
711
712
	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);
713
	be_node_attr_t *attr;
Sebastian Hack's avatar
Sebastian Hack committed
714
715
716
717

	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);
718
	init_node_attr(irn, n + 1, 1);
719
720
	attr = (be_node_attr_t*) get_irn_generic_attr(irn);
	attr->exc.pin_state = op_pin_state_floats;
721
722
	be_node_set_reg_class_in(irn, 0, cls);
	be_node_set_reg_class_out(irn, 0, cls);
723
724
725
726
727
728
	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
729
730
731
732

	return irn;
}

733
ir_node *be_new_CopyKeep_single(ir_node *bl, ir_node *src, ir_node *keep)
Sebastian Hack's avatar
Sebastian Hack committed
734
{
735
	return be_new_CopyKeep(bl, src, 1, &keep);
Sebastian Hack's avatar
Sebastian Hack committed
736
737
}

738
739
ir_node *be_get_CopyKeep_op(const ir_node *cpy)
{
740
	return get_irn_n(cpy, n_be_CopyKeep_op);
741
742
}

743
744
void be_set_CopyKeep_op(ir_node *cpy, ir_node *op)
{
745
	set_irn_n(cpy, n_be_CopyKeep_op, op);
746
747
}

748
static bool be_has_frame_entity(const ir_node *irn)
749
{
750
	switch (get_irn_opcode(irn)) {
751
	case beo_Spill:
752
	case beo_Reload:
753
	case beo_FrameAddr:
754
		return true;
755
	default:
756
		return false;
757
758
759
	}
}

760
ir_entity *be_get_frame_entity(const ir_node *irn)
761
{
762
	if (be_has_frame_entity(irn)) {
763
		const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
764
765
766
767
		return a->ent;
	}
	return NULL;
}
Sebastian Hack's avatar
Sebastian Hack committed
768

769
770
771
772
int be_get_frame_offset(const ir_node *irn)
{
	assert(is_be_node(irn));
	if (be_has_frame_entity(irn)) {
Matthias Braun's avatar