benode.c 38.5 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
/*
Michael Beck's avatar
Michael Beck committed
2
 * Copyright (C) 1995-2011 University of Karlsruhe.  All right reserved.
Christian Würdig's avatar
Christian Würdig committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * This file is part of libFirm.
 *
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * Licensees holding valid libFirm Professional Edition licenses may use
 * this file in accordance with the libFirm Commercial License.
 * Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

Sebastian Hack's avatar
Sebastian Hack committed
20
/**
Christian Würdig's avatar
Christian Würdig committed
21
22
23
24
 * @file
 * @brief       Backend node support for generic backend nodes.
 * @author      Sebastian Hack
 * @date        17.05.2005
Sebastian Hack's avatar
Sebastian Hack committed
25
 *
26
 * Backend node support for generic backend nodes.
Michael Beck's avatar
Michael Beck committed
27
 * This file provides Perm, Copy, Spill and Reload nodes.
Sebastian Hack's avatar
Sebastian Hack committed
28
 */
29
#include "config.h"
Sebastian Hack's avatar
Sebastian Hack committed
30
31
32

#include <stdlib.h>

33
#include "beirg.h"
Sebastian Hack's avatar
Sebastian Hack committed
34
35
36
#include "obst.h"
#include "set.h"
#include "pmap.h"
37
#include "util.h"
Sebastian Hack's avatar
Sebastian Hack committed
38
#include "debug.h"
39
#include "fourcc.h"
Sebastian Hack's avatar
Sebastian Hack committed
40
#include "bitfiddle.h"
Matthias Braun's avatar
Matthias Braun committed
41
#include "raw_bitset.h"
42
#include "error.h"
43
#include "array_t.h"
Sebastian Hack's avatar
Sebastian Hack committed
44
45
46
47

#include "irop_t.h"
#include "irmode_t.h"
#include "irnode_t.h"
48
#include "ircons_t.h"
49
#include "irprintf.h"
Sebastian Hack's avatar
Sebastian Hack committed
50
#include "irgwalk.h"
Michael Beck's avatar
Michael Beck committed
51
#include "iropt_t.h"
52
53
#include "irbackedge_t.h"
#include "irverify_t.h"
Sebastian Hack's avatar
Sebastian Hack committed
54

55
56
#include "be_t.h"
#include "belive_t.h"
57
#include "besched.h"
58
#include "benode.h"
59
#include "bearch.h"
Sebastian Hack's avatar
Sebastian Hack committed
60

61
62
#include "beirgmod.h"

63
64
65
66
typedef struct be_node_attr_t {
	except_attr  exc;
} be_node_attr_t;

67
68
/** The be_Return nodes attribute type. */
typedef struct {
69
	be_node_attr_t base;
Matthias Braun's avatar
Matthias Braun committed
70
71
72
	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 */
73
74
} be_return_attr_t;

Michael Beck's avatar
Michael Beck committed
75
/** The be_IncSP attribute type. */
Sebastian Hack's avatar
Sebastian Hack committed
76
typedef struct {
77
	be_node_attr_t base;
Matthias Braun's avatar
Matthias Braun committed
78
79
80
81
	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
82
} be_incsp_attr_t;
Sebastian Hack's avatar
Sebastian Hack committed
83

84
/** The be_Frame attribute type. */
85
typedef struct {
86
	be_node_attr_t  base;
87
88
	ir_entity      *ent;
	int             offset;
89
90
} be_frame_attr_t;

91
/** The be_Call attribute type. */
92
typedef struct {
93
	be_node_attr_t  base;
Matthias Braun's avatar
Matthias Braun committed
94
	ir_entity      *ent;        /**< called entity if this is a static call. */
95
	unsigned        pop;
Matthias Braun's avatar
Matthias Braun committed
96
	ir_type        *call_tp;    /**< call type, copied from the original Call */
97
98
} be_call_attr_t;

99
typedef struct {
100
101
102
	be_node_attr_t base;
	ir_entity    **in_entities;
	ir_entity    **out_entities;
Matthias Braun's avatar
Matthias Braun committed
103
} be_memperm_attr_t;
104

105
106
107
ir_op *op_be_Spill;
ir_op *op_be_Reload;
ir_op *op_be_Perm;
Matthias Braun's avatar
Matthias Braun committed
108
ir_op *op_be_MemPerm;
109
110
ir_op *op_be_Copy;
ir_op *op_be_Keep;
Sebastian Hack's avatar
Sebastian Hack committed
111
ir_op *op_be_CopyKeep;
112
113
114
ir_op *op_be_Call;
ir_op *op_be_Return;
ir_op *op_be_IncSP;
Sebastian Hack's avatar
Sebastian Hack committed
115
ir_op *op_be_AddSP;
Michael Beck's avatar
Michael Beck committed
116
ir_op *op_be_SubSP;
117
ir_op *op_be_Start;
118
ir_op *op_be_FrameAddr;
Sebastian Hack's avatar
Sebastian Hack committed
119

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

130
131
132
	if (a_attr->ent != b_attr->ent || a_attr->offset != b_attr->offset)
		return 1;

133
	return be_nodes_equal(a, b);
134
135
}

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

	if (a_attr->num_ret_vals != b_attr->num_ret_vals)
		return 1;
148
149
	if (a_attr->pop != b_attr->pop)
		return 1;
150
151
	if (a_attr->emit_pop != b_attr->emit_pop)
		return 1;
152

153
	return be_nodes_equal(a, b);
154
155
}

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

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

169
	return be_nodes_equal(a, b);
170
171
}

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

	if (a_attr->ent != b_attr->ent ||
183
		a_attr->call_tp != b_attr->call_tp)
184
185
		return 1;

186
	return be_nodes_equal(a, b);
Michael Beck's avatar
Michael Beck committed
187
188
}

189
static arch_register_req_t *allocate_reg_req(const ir_node *node)
Matthias Braun's avatar
Matthias Braun committed
190
{
191
	ir_graph       *irg  = get_irn_irg(node);
192
	struct obstack *obst = be_get_be_obst(irg);
Matthias Braun's avatar
Matthias Braun committed
193

194
	arch_register_req_t *req = OALLOCZ(obst, arch_register_req_t);
195
196
	return req;
}
Matthias Braun's avatar
Matthias Braun committed
197

198
199
void be_set_constr_in(ir_node *node, int pos, const arch_register_req_t *req)
{
200
201
202
	backend_info_t *info = be_get_info(node);
	assert(pos < get_irn_arity(node));
	info->in_reqs[pos] = req;
203
}
Matthias Braun's avatar
Matthias Braun committed
204

205
206
207
208
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
209
210
}

211
/**
212
 * Initializes the generic attribute of all be nodes and return it.
213
 */
214
static void init_node_attr(ir_node *node, int n_inputs, int n_outputs)
Sebastian Hack's avatar
Sebastian Hack committed
215
{
216
	ir_graph       *irg  = get_irn_irg(node);
217
	struct obstack *obst = be_get_be_obst(irg);
218
	backend_info_t *info = be_get_info(node);
219
	const arch_register_req_t **in_reqs;
Sebastian Hack's avatar
Sebastian Hack committed
220

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

233
234
235
236
237
238
239
	if (n_outputs >= 0) {
		int i;
		info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_outputs);
		memset(info->out_infos, 0, n_outputs * sizeof(info->out_infos[0]));
		for (i = 0; i < n_outputs; ++i) {
			info->out_infos[i].req = arch_no_register_req;
		}
240
	} else {
241
		info->out_infos = NEW_ARR_F(reg_out_info_t, 0);
Sebastian Hack's avatar
Sebastian Hack committed
242
243
	}
}
244

245
static void add_register_req_in(ir_node *node, const arch_register_req_t *req)
246
{
247
	backend_info_t *info = be_get_info(node);
248
	ARR_APP1(const arch_register_req_t*, info->in_reqs, req);
249
250
}

251
252
253
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)
254
{
Matthias Braun's avatar
Matthias Braun committed
255
	be_frame_attr_t *a;
256
	ir_node         *in[2];
257
	ir_node         *res;
258
	ir_graph        *irg = get_Block_irg(bl);
259

260
261
262
	in[0]     = frame;
	in[1]     = to_spill;
	res       = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
263
264
	init_node_attr(res, 2, 1);
	a         = (be_frame_attr_t*) get_irn_generic_attr(res);
265
	a->ent    = NULL;
Matthias Braun's avatar
Matthias Braun committed
266
	a->offset = 0;
267
	a->base.exc.pin_state = op_pin_state_pinned;
Sebastian Hack's avatar
Sebastian Hack committed
268

269
270
	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);
271
	arch_set_irn_register_req_out(res, 0, arch_no_register_req);
272

Sebastian Hack's avatar
Sebastian Hack committed
273
274
	return res;
}
275

276
ir_node *be_new_Reload(const arch_register_class_t *cls,
277
		const arch_register_class_t *cls_frame, ir_node *block,
278
		ir_node *frame, ir_node *mem, ir_mode *mode)
279
{
280
281
282
	ir_node  *in[2];
	ir_node  *res;
	ir_graph *irg = get_Block_irg(block);
283
	be_frame_attr_t *a;
284
285
286

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

289
	init_node_attr(res, 2, 1);
290
	be_node_set_reg_class_out(res, 0, cls);
291

292
	be_node_set_reg_class_in(res, n_be_Reload_frame, cls_frame);
293
	arch_set_irn_flags(res, arch_irn_flags_rematerializable);
294

295
296
297
298
299
	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
300
301
	return res;
}
302

303
ir_node *be_get_Reload_mem(const ir_node *irn)
Sebastian Hack's avatar
Sebastian Hack committed
304
305
{
	assert(be_is_Reload(irn));
306
	return get_irn_n(irn, n_be_Reload_mem);
Sebastian Hack's avatar
Sebastian Hack committed
307
308
}

309
310
311
ir_node *be_get_Reload_frame(const ir_node *irn)
{
	assert(be_is_Reload(irn));
312
	return get_irn_n(irn, n_be_Reload_frame);
313
314
}

315
316
317
ir_node *be_get_Spill_val(const ir_node *irn)
{
	assert(be_is_Spill(irn));
318
	return get_irn_n(irn, n_be_Spill_val);
319
}
Matthias Braun's avatar
Matthias Braun committed
320

321
322
323
ir_node *be_get_Spill_frame(const ir_node *irn)
{
	assert(be_is_Spill(irn));
324
	return get_irn_n(irn, n_be_Spill_frame);
325
}
326

327
328
ir_node *be_new_Perm(const arch_register_class_t *cls, ir_node *block,
                     int n, ir_node *in[])
Sebastian Hack's avatar
Sebastian Hack committed
329
{
330
331
332
	int            i;
	ir_graph       *irg = get_Block_irg(block);
	be_node_attr_t *attr;
333

334
	ir_node *irn = new_ir_node(NULL, irg, block, op_be_Perm, mode_T, n, in);
335
	init_node_attr(irn, n, n);
336
337
	attr                = (be_node_attr_t*) get_irn_generic_attr(irn);
	attr->exc.pin_state = op_pin_state_pinned;
338
	for (i = 0; i < n; ++i) {
339
340
341
342
343
344
345
346
347
348
349
350
351
		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 {
			arch_register_req_t *new_req = allocate_reg_req(irn);
			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
352
353
	}

Sebastian Hack's avatar
Sebastian Hack committed
354
355
	return irn;
}
356

357
358
void be_Perm_reduce(ir_node *perm, int new_size, int *map)
{
359
360
361
362
363
	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);
364
	ir_node        **new_in;
365
	int              i;
366
367

	assert(be_is_Perm(perm));
368
	assert(new_size <= arity);
369

370
	new_in = ALLOCAN(ir_node*, new_size);
371

372
	/* save the old register data */
373
	memcpy(old_in_reqs, info->in_reqs, arity * sizeof(old_in_reqs[0]));
374
	memcpy(old_infos, info->out_infos, arity * sizeof(old_infos[0]));
375

Matthias Braun's avatar
Matthias Braun committed
376
	/* compose the new in array and set the new register data directly */
377
	for (i = 0; i < new_size; ++i) {
378
		int idx = map[i];
379
		new_in[i]          = get_irn_n(perm, idx);
380
		info->in_reqs[i]   = old_in_reqs[idx];
381
		info->out_infos[i] = old_infos[idx];
382
383
384
385
386
	}

	set_irn_in(perm, new_size, new_in);
}

387
ir_node *be_new_MemPerm(ir_node *block, int n, ir_node *in[])
Matthias Braun's avatar
Matthias Braun committed
388
{
389
390
	ir_graph                     *irg       = get_Block_irg(block);
	const arch_env_t             *arch_env  = be_get_irg_arch_env(irg);
391
392
393
394
395
	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
396

397
	real_in = ALLOCAN(ir_node*, n + 1);
398
399
400
	real_in[0] = frame;
	memcpy(&real_in[1], in, n * sizeof(real_in[0]));

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

403
	init_node_attr(irn, n + 1, n);
404
	be_node_set_reg_class_in(irn, 0, sp->reg_class);
Matthias Braun's avatar
Matthias Braun committed
405

406
	attr               = (be_memperm_attr_t*)get_irn_generic_attr(irn);
407
408
	attr->in_entities  = OALLOCNZ(irg->obst, ir_entity*, n);
	attr->out_entities = OALLOCNZ(irg->obst, ir_entity*, n);
Matthias Braun's avatar
Matthias Braun committed
409
410
411
412

	return irn;
}

413
ir_node *be_new_Copy(ir_node *bl, ir_node *op)
Sebastian Hack's avatar
Sebastian Hack committed
414
415
416
{
	ir_node *in[1];
	ir_node *res;
Matthias Braun's avatar
Matthias Braun committed
417
	arch_register_req_t *req;
418
	be_node_attr_t *attr;
419
	ir_graph *irg = get_Block_irg(bl);
420
421
	const arch_register_req_t   *in_req = arch_get_irn_register_req(op);
	const arch_register_class_t *cls    = in_req->cls;
422

Sebastian Hack's avatar
Sebastian Hack committed
423
	in[0] = op;
424
	res   = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
425
	init_node_attr(res, 1, 1);
426
427
	attr = (be_node_attr_t*) get_irn_generic_attr(res);
	attr->exc.pin_state = op_pin_state_floats;
428
429
	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
430

431
432
	req = allocate_reg_req(res);
	req->cls        = cls;
433
434
	req->type       = arch_register_req_type_should_be_same
		| (in_req->type & arch_register_req_type_aligned);
435
	req->other_same = 1U << 0;
436
	req->width      = in_req->width;
437
	be_set_constr_out(res, 0, req);
Matthias Braun's avatar
Matthias Braun committed
438

Sebastian Hack's avatar
Sebastian Hack committed
439
	return res;
440
441
}

442
443
ir_node *be_get_Copy_op(const ir_node *cpy)
{
444
	return get_irn_n(cpy, n_be_Copy_op);
445
446
}

447
ir_node *be_new_Keep(ir_node *block, int n, ir_node *in[])
Sebastian Hack's avatar
Sebastian Hack committed
448
{
Sebastian Hack's avatar
Sebastian Hack committed
449
	int i;
450
	ir_node *res;
451
	ir_graph *irg = get_Block_irg(block);
452
	be_node_attr_t *attr;
453

454
455
	res = new_ir_node(NULL, irg, block, op_be_Keep, mode_ANY, -1, NULL);
	init_node_attr(res, -1, 1);
456
457
	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
458

459
	for (i = 0; i < n; ++i) {
460
461
462
463
464
		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
465
	}
466
467
468
469
470
471
472
473
	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));
474
475
	add_irn_n(keep, node);
	add_register_req_in(keep, cls->class_req);
Sebastian Hack's avatar
Sebastian Hack committed
476
477
}

478
ir_node *be_new_Call(dbg_info *dbg, ir_graph *irg, ir_node *bl, ir_node *mem,
479
480
481
		const arch_register_req_t *sp_req, ir_node *sp,
		const arch_register_req_t *ptr_req, ir_node *ptr,
		int n_outs, int n, ir_node *in[], ir_type *call_tp)
Sebastian Hack's avatar
Sebastian Hack committed
482
{
483
	be_call_attr_t *a;
484
	int real_n = n_be_Call_first_arg + n;
Sebastian Hack's avatar
Sebastian Hack committed
485
486
	ir_node *irn;
	ir_node **real_in;
Sebastian Hack's avatar
Sebastian Hack committed
487

488
	NEW_ARR_A(ir_node *, real_in, real_n);
489
490
491
492
	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
493

494
	irn = new_ir_node(dbg, irg, bl, op_be_Call, mode_T, real_n, real_in);
495
	init_node_attr(irn, real_n, n_outs);
496
497
498
499
500
	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;
501
502
	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
503
	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
504
505
}

506
507
ir_entity *be_Call_get_entity(const ir_node *call)
{
508
	const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
509
510
511
512
	assert(be_is_Call(call));
	return a->ent;
}

513
514
void be_Call_set_entity(ir_node *call, ir_entity *ent)
{
515
	be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
516
517
518
519
	assert(be_is_Call(call));
	a->ent = ent;
}

520
521
ir_type *be_Call_get_type(ir_node *call)
{
522
	const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
523
524
525
526
	assert(be_is_Call(call));
	return a->call_tp;
}

527
528
void be_Call_set_type(ir_node *call, ir_type *call_tp)
{
529
	be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
530
531
532
533
	assert(be_is_Call(call));
	a->call_tp = call_tp;
}

534
535
void be_Call_set_pop(ir_node *call, unsigned pop)
{
536
	be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
Matthias Braun's avatar
Matthias Braun committed
537
538
539
	a->pop = pop;
}

540
541
unsigned be_Call_get_pop(const ir_node *call)
{
542
	const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
Matthias Braun's avatar
Matthias Braun committed
543
544
545
	return a->pop;
}

546
ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *block, int n_res,
547
                       unsigned pop, int n, ir_node *in[])
Sebastian Hack's avatar
Sebastian Hack committed
548
{
549
	be_return_attr_t *a;
550
551
	ir_node *res;

552
553
	res = new_ir_node(dbg, irg, block, op_be_Return, mode_X, n, in);
	init_node_attr(res, n, 1);
554
	be_set_constr_out(res, 0, arch_no_register_req);
555

556
	a = (be_return_attr_t*)get_irn_generic_attr(res);
557
558
559
560
	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
561

562
	return res;
Sebastian Hack's avatar
Sebastian Hack committed
563
564
}

565
566
int be_Return_get_n_rets(const ir_node *ret)
{
567
	const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
568
569
570
	return a->num_ret_vals;
}

571
572
unsigned be_Return_get_pop(const ir_node *ret)
{
573
	const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
574
575
576
	return a->pop;
}

577
578
int be_Return_get_emit_pop(const ir_node *ret)
{
579
	const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
580
581
582
	return a->emit_pop;
}

583
584
void be_Return_set_emit_pop(ir_node *ret, int emit_pop)
{
585
	be_return_attr_t *a = (be_return_attr_t*)get_irn_generic_attr(ret);
586
587
588
	a->emit_pop = emit_pop;
}

589
ir_node *be_new_IncSP(const arch_register_t *sp, ir_node *bl,
590
                      ir_node *old_sp, int offset, int align)
Sebastian Hack's avatar
Sebastian Hack committed
591
{
Michael Beck's avatar
Michael Beck committed
592
	be_incsp_attr_t *a;
Sebastian Hack's avatar
Sebastian Hack committed
593
	ir_node *irn;
Sebastian Hack's avatar
Sebastian Hack committed
594
	ir_node *in[1];
595
	ir_graph *irg = get_Block_irg(bl);
Sebastian Hack's avatar
Sebastian Hack committed
596
597

	in[0]     = old_sp;
598
	irn       = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode,
599
	                        ARRAY_SIZE(in), in);
600
	init_node_attr(irn, 1, 1);
601
602
603
604
	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
605
606

	/* Set output constraint to stack register. */
607
608
	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
609
610

	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
611
}
Sebastian Hack's avatar
Sebastian Hack committed
612

613
614
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
615
{
Sebastian Hack's avatar
Sebastian Hack committed
616
	ir_node *irn;
617
	ir_node *in[n_be_AddSP_last];
618
	ir_graph *irg;
619
	be_node_attr_t *attr;
Sebastian Hack's avatar
Sebastian Hack committed
620

621
622
	in[n_be_AddSP_old_sp] = old_sp;
	in[n_be_AddSP_size]   = sz;
Sebastian Hack's avatar
Sebastian Hack committed
623

624
	irg = get_Block_irg(bl);
625
626
	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);
627
628
	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
629

Sebastian Hack's avatar
Sebastian Hack committed
630
	/* Set output constraint to stack register. */
631
	be_set_constr_single_reg_in(irn, n_be_AddSP_old_sp, sp,
632
	                            arch_register_req_type_none);
633
	be_node_set_reg_class_in(irn, n_be_AddSP_size, sp->reg_class);
Matthias Braun's avatar
Matthias Braun committed
634
635
	be_set_constr_single_reg_out(irn, pn_be_AddSP_sp, sp,
	                             arch_register_req_type_produces_sp);
636

Sebastian Hack's avatar
Sebastian Hack committed
637
638
639
	return irn;
}

640
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
641
642
{
	ir_node *irn;
643
	ir_node *in[n_be_SubSP_last];
644
	ir_graph *irg;
645
	be_node_attr_t *attr;
Michael Beck's avatar
Michael Beck committed
646

647
648
	in[n_be_SubSP_old_sp] = old_sp;
	in[n_be_SubSP_size]   = sz;
Michael Beck's avatar
Michael Beck committed
649

650
	irg = get_Block_irg(bl);
651
652
	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);
653
654
	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
655
656

	/* Set output constraint to stack register. */
657
	be_set_constr_single_reg_in(irn, n_be_SubSP_old_sp, sp,
658
	                            arch_register_req_type_none);
659
	be_node_set_reg_class_in(irn, n_be_SubSP_size, sp->reg_class);
660
	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
661
662
663
664

	return irn;
}

665
ir_node *be_new_Start(dbg_info *dbgi, ir_node *bl, int n_outs)
Sebastian Hack's avatar
Sebastian Hack committed
666
{
667
	ir_node *res;
668
	ir_graph *irg = get_Block_irg(bl);
669
	be_node_attr_t *attr;
Sebastian Hack's avatar
Sebastian Hack committed
670

671
	res = new_ir_node(dbgi, irg, bl, op_be_Start, mode_T, 0, NULL);
672
	init_node_attr(res, 0, n_outs);
673
674
	attr = (be_node_attr_t*) get_irn_generic_attr(res);
	attr->exc.pin_state = op_pin_state_pinned;
675
676
677
678

	return res;
}

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

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

	return optimize_node(irn);
Sebastian Hack's avatar
Sebastian Hack committed
697
698
}

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

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

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

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

	return irn;
}

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

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

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

754
static bool be_has_frame_entity(const ir_node *irn)
755
{
756
	switch (get_irn_opcode(irn)) {
757
	case beo_Spill:
758
	case beo_Reload:
759
	case beo_FrameAddr:
760
		return true;
761
	default:
762
		return false;
763
764
765
	}
}

766
ir_entity *be_get_frame_entity(const ir_node *irn)
767
{
768
	if (be_has_frame_entity(irn)) {
769
		const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
770
771
772
773
		return a->ent;
	}
	return NULL;
}
Sebastian Hack's avatar
Sebastian Hack committed
774

775
776
777
778
int be_get_frame_offset(const ir_node *irn)
{
	assert(is_be_node(irn));
	if (be_has_frame_entity(irn)) {
779
		const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
780
781
782
783
784
		return a->offset;
	}
	return 0;
}