benode.c 39.1 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
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_t.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
99
100
ir_op *op_be_Spill;
ir_op *op_be_Reload;
ir_op *op_be_Perm;
Matthias Braun's avatar
Matthias Braun committed
101
ir_op *op_be_MemPerm;
102
103
ir_op *op_be_Copy;
ir_op *op_be_Keep;
Sebastian Hack's avatar
Sebastian Hack committed
104
ir_op *op_be_CopyKeep;
105
106
107
ir_op *op_be_Call;
ir_op *op_be_Return;
ir_op *op_be_IncSP;
Sebastian Hack's avatar
Sebastian Hack committed
108
ir_op *op_be_AddSP;
Michael Beck's avatar
Michael Beck committed
109
ir_op *op_be_SubSP;
110
ir_op *op_be_Start;
111
ir_op *op_be_FrameAddr;
Sebastian Hack's avatar
Sebastian Hack committed
112

113
114
#define be_op_tag FOURCC('B', 'E', '\0', '\0')

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

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

128
	return be_nodes_equal(a, b);
129
130
}

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

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

148
	return be_nodes_equal(a, b);
149
150
}

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

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

164
	return be_nodes_equal(a, b);
165
166
}

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

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

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

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

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

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

199
200
201
202
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
203
204
}

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

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

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

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

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

241
242
243
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)
244
{
Matthias Braun's avatar
Matthias Braun committed
245
	be_frame_attr_t *a;
246
	ir_node         *in[2];
247
	ir_node         *res;
248
	ir_graph        *irg = get_Block_irg(bl);
249

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

259
260
	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);
261
	arch_set_irn_register_req_out(res, 0, arch_no_register_req);
262
	arch_add_irn_flags(res, arch_irn_flags_spill);
263

Sebastian Hack's avatar
Sebastian Hack committed
264
265
	return res;
}
266

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

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

280
	init_node_attr(res, 2, 1);
281
	be_node_set_reg_class_out(res, 0, cls);
282

283
	be_node_set_reg_class_in(res, n_be_Reload_frame, cls_frame);
284
	arch_set_irn_flags(res, arch_irn_flags_rematerializable);
285

286
287
288
289
290
	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
291
292
	return res;
}
293

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

300
301
302
ir_node *be_get_Reload_frame(const ir_node *irn)
{
	assert(be_is_Reload(irn));
303
	return get_irn_n(irn, n_be_Reload_frame);
304
305
}

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

312
313
314
ir_node *be_get_Spill_frame(const ir_node *irn)
{
	assert(be_is_Spill(irn));
315
	return get_irn_n(irn, n_be_Spill_frame);
316
}
317

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

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

Sebastian Hack's avatar
Sebastian Hack committed
344
345
	return irn;
}
346

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

	assert(be_is_Perm(perm));
358
	assert(new_size <= arity);
359

360
	new_in = ALLOCAN(ir_node*, new_size);
361

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

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

	set_irn_in(perm, new_size, new_in);
}

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

387
	real_in = ALLOCAN(ir_node*, n + 1);
388
389
390
	real_in[0] = frame;
	memcpy(&real_in[1], in, n * sizeof(real_in[0]));

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

393
	init_node_attr(irn, n + 1, n);
394
	be_node_set_reg_class_in(irn, 0, sp->reg_class);
Matthias Braun's avatar
Matthias Braun committed
395

396
	attr               = (be_memperm_attr_t*)get_irn_generic_attr(irn);
397
398
	attr->in_entities  = OALLOCNZ(get_irg_obstack(irg), ir_entity*, n);
	attr->out_entities = OALLOCNZ(get_irg_obstack(irg), ir_entity*, n);
399
	attr->offset       = 0;
Matthias Braun's avatar
Matthias Braun committed
400
401
402
403

	return irn;
}

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

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

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

Sebastian Hack's avatar
Sebastian Hack committed
429
	return res;
430
431
}

432
433
ir_node *be_get_Copy_op(const ir_node *cpy)
{
434
	return get_irn_n(cpy, n_be_Copy_op);
435
436
}

437
ir_node *be_new_Keep(ir_node *const block, int const n, ir_node *const *const in)
Sebastian Hack's avatar
Sebastian Hack committed
438
{
Sebastian Hack's avatar
Sebastian Hack committed
439
	int i;
440
	ir_node *res;
441
	ir_graph *irg = get_Block_irg(block);
442
	be_node_attr_t *attr;
443

444
445
	res = new_ir_node(NULL, irg, block, op_be_Keep, mode_ANY, -1, NULL);
	init_node_attr(res, -1, 1);
446
447
	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
448

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

468
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
469
{
470
	be_call_attr_t *a;
471
	int real_n = n_be_Call_first_arg + n;
Sebastian Hack's avatar
Sebastian Hack committed
472
	ir_node **real_in;
Sebastian Hack's avatar
Sebastian Hack committed
473

474
	NEW_ARR_A(ir_node *, real_in, real_n);
475
476
477
478
	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
479

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

493
494
ir_entity *be_Call_get_entity(const ir_node *call)
{
495
	const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
496
497
498
499
	assert(be_is_Call(call));
	return a->ent;
}

500
501
void be_Call_set_entity(ir_node *call, ir_entity *ent)
{
502
	be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
503
504
505
506
	assert(be_is_Call(call));
	a->ent = ent;
}

507
508
ir_type *be_Call_get_type(ir_node *call)
{
509
	const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
510
511
512
513
	assert(be_is_Call(call));
	return a->call_tp;
}

514
515
void be_Call_set_type(ir_node *call, ir_type *call_tp)
{
516
	be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
517
518
519
520
	assert(be_is_Call(call));
	a->call_tp = call_tp;
}

521
522
void be_Call_set_pop(ir_node *call, unsigned pop)
{
523
	be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
Matthias Braun's avatar
Matthias Braun committed
524
525
526
	a->pop = pop;
}

527
528
unsigned be_Call_get_pop(const ir_node *call)
{
529
	const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
Matthias Braun's avatar
Matthias Braun committed
530
531
532
	return a->pop;
}

533
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
534
{
535
536
	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);
537
	init_node_attr(res, n, 1);
538
	be_set_constr_out(res, 0, arch_no_register_req);
539

540
	be_return_attr_t *const a = (be_return_attr_t*)get_irn_generic_attr(res);
541
542
543
544
	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
545

546
	return res;
Sebastian Hack's avatar
Sebastian Hack committed
547
548
}

549
550
551
552
553
554
555
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;
}

556
557
int be_Return_get_n_rets(const ir_node *ret)
{
558
	const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
559
560
561
	return a->num_ret_vals;
}

562
563
unsigned be_Return_get_pop(const ir_node *ret)
{
564
	const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
565
566
567
	return a->pop;
}

568
569
int be_Return_get_emit_pop(const ir_node *ret)
{
570
	const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
571
572
573
	return a->emit_pop;
}

574
575
void be_Return_set_emit_pop(ir_node *ret, int emit_pop)
{
576
	be_return_attr_t *a = (be_return_attr_t*)get_irn_generic_attr(ret);
577
578
579
	a->emit_pop = emit_pop;
}

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

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

	/* Set output constraint to stack register. */
598
599
	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
600
601

	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
602
}
Sebastian Hack's avatar
Sebastian Hack committed
603

604
605
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
606
{
Sebastian Hack's avatar
Sebastian Hack committed
607
	ir_node *irn;
608
	ir_node *in[n_be_AddSP_last];
609
	ir_graph *irg;
610
	be_node_attr_t *attr;
Sebastian Hack's avatar
Sebastian Hack committed
611

612
613
	in[n_be_AddSP_old_sp] = old_sp;
	in[n_be_AddSP_size]   = sz;
Sebastian Hack's avatar
Sebastian Hack committed
614

615
	irg = get_Block_irg(bl);
616
617
	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);
618
619
	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
620

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

Sebastian Hack's avatar
Sebastian Hack committed
628
629
630
	return irn;
}

631
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
632
633
{
	ir_node *irn;
634
	ir_node *in[n_be_SubSP_last];
635
	ir_graph *irg;
636
	be_node_attr_t *attr;
Michael Beck's avatar
Michael Beck committed
637

638
639
	in[n_be_SubSP_old_sp] = old_sp;
	in[n_be_SubSP_size]   = sz;
Michael Beck's avatar
Michael Beck committed
640

641
	irg = get_Block_irg(bl);
642
643
	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);
644
645
	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
646
647

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

	return irn;
}

656
ir_node *be_new_Start(dbg_info *dbgi, ir_node *bl, int n_outs)
Sebastian Hack's avatar
Sebastian Hack committed
657
{
658
	ir_node *res;
659
	ir_graph *irg = get_Block_irg(bl);
660
	be_node_attr_t *attr;
Sebastian Hack's avatar
Sebastian Hack committed
661

662
	res = new_ir_node(dbgi, irg, bl, op_be_Start, mode_T, 0, NULL);
663
	init_node_attr(res, 0, n_outs);
664
665
	attr = (be_node_attr_t*) get_irn_generic_attr(res);
	attr->exc.pin_state = op_pin_state_pinned;
666
667
668
669

	return res;
}

670
671
672
673
674
675
676
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;
}

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

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

	return optimize_node(irn);
Sebastian Hack's avatar
Sebastian Hack committed
695
696
}

697
698
ir_node *be_get_FrameAddr_frame(const ir_node *node)
{
Michael Beck's avatar
Michael Beck committed
699
	assert(be_is_FrameAddr(node));
700
	return get_irn_n(node, n_be_FrameAddr_ptr);
Michael Beck's avatar
Michael Beck committed
701
702
}

Matthias Braun's avatar
Matthias Braun committed
703
704
ir_entity *be_get_FrameAddr_entity(const ir_node *node)
{
705
	const be_frame_attr_t *attr = (const be_frame_attr_t*)get_irn_generic_attr_const(node);
Matthias Braun's avatar
Matthias Braun committed
706
707
708
	return attr->ent;
}

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

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

	return irn;
}

737
ir_node *be_new_CopyKeep_single(ir_node *bl, ir_node *src, ir_node *keep)
Sebastian Hack's avatar
Sebastian Hack committed
738
{
739
	return be_new_CopyKeep(bl, src, 1, &keep);
Sebastian Hack's avatar
Sebastian Hack committed
740
741
}

742
743
ir_node *be_get_CopyKeep_op(const ir_node *cpy)
{
744
	return get_irn_n(cpy, n_be_CopyKeep_op);
745
746
}

747
748
void be_set_CopyKeep_op(ir_node *cpy, ir_node *op)
{
749
	set_irn_n(cpy, n_be_CopyKeep_op, op);
750
751
}

752
static bool be_has_frame_entity(const ir_node *irn)
753
{
754
	return be_is_Spill(irn) || be_is_Reload(irn) || be_is_FrameAddr(irn);
755
756
}

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

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