benode.c 38.5 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;
Matthias Braun's avatar
Matthias Braun committed
94
} be_memperm_attr_t;
95

96
static unsigned be_opcode_start;
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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	set_irn_in(perm, new_size, new_in);
}

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

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

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

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

395
	attr               = (be_memperm_attr_t*)get_irn_generic_attr(irn);
396
397
	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
398
399
400
401

	return irn;
}

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

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

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

Sebastian Hack's avatar
Sebastian Hack committed
427
	return res;
428
429
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

544
	return res;
Sebastian Hack's avatar
Sebastian Hack committed
545
546
}

547
548
549
550
551
552
553
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;
}

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

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

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

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

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

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

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

	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
600
}
Sebastian Hack's avatar
Sebastian Hack committed
601

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

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

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

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

Sebastian Hack's avatar
Sebastian Hack committed
626
627
628
	return irn;
}

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

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

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

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

	return irn;
}

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

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

	return res;
}

668
669
670
671
672
673
674
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;
}

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

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

	return optimize_node(irn);
Sebastian Hack's avatar
Sebastian Hack committed
693
694
}

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

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

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

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

	return irn;
}

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

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

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

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

755
ir_entity *be_get_frame_entity(const ir_node *irn)
756
{
757
	if (be_has_frame_entity(irn)) {
758
		const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
759
760
761
762
		return a->ent;
	}
	return NULL;
}
Sebastian Hack's avatar
Sebastian Hack committed
763

764
765
766
767
int be_get_frame_offset(const ir_node *irn)
{
	assert(is_be_node(irn));
	if (be_has_frame_entity(irn)) {
768
		const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
769
770
771
772
773
		return a->offset;
	}
	return 0;
}

774
void be_set_MemPerm_in_entity(const ir_node *irn, int n, ir_entity *ent)