benode.c 38.4 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
33
34
35

#include <stdlib.h>

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

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

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

60
61
#include "beirgmod.h"

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

232
233
234
235
236
237
238
	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;
		}
239
	} else {
240
		info->out_infos = NEW_ARR_F(reg_out_info_t, 0);
Sebastian Hack's avatar
Sebastian Hack committed
241
242
	}
}
243

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

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

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

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

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

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

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

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

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

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

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

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

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

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

326
327
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
328
{
329
330
331
	int            i;
	ir_graph       *irg = get_Block_irg(block);
	be_node_attr_t *attr;
332

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

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

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

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

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

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

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

	set_irn_in(perm, new_size, new_in);
}

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

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

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

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

405
	attr               = (be_memperm_attr_t*)get_irn_generic_attr(irn);
406
407
	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
408
409
410
411

	return irn;
}

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

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

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

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

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

446
447
void be_set_Copy_op(ir_node *cpy, ir_node *op)
{
448
	set_irn_n(cpy, n_be_Copy_op, op);
449
450
}

451
ir_node *be_new_Keep(ir_node *block, int n, ir_node *in[])
Sebastian Hack's avatar
Sebastian Hack committed
452
{
Sebastian Hack's avatar
Sebastian Hack committed
453
	int i;
454
	ir_node *res;
455
	ir_graph *irg = get_Block_irg(block);
456
	be_node_attr_t *attr;
457

458
459
	res = new_ir_node(NULL, irg, block, op_be_Keep, mode_ANY, -1, NULL);
	init_node_attr(res, -1, 1);
460
461
	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
462

463
	for (i = 0; i < n; ++i) {
464
		add_irn_n(res, in[i]);
465
		add_register_req_in(res);
Sebastian Hack's avatar
Sebastian Hack committed
466
	}
467
468
469
470
471
472
473
474
475
476
477
	keep_alive(res);

	return res;
}

void be_Keep_add_node(ir_node *keep, const arch_register_class_t *cls, ir_node *node)
{
	int n;

	assert(be_is_Keep(keep));
	n = add_irn_n(keep, node);
478
	add_register_req_in(keep);
479
	be_node_set_reg_class_in(keep, n, cls);
Sebastian Hack's avatar
Sebastian Hack committed
480
481
}

482
ir_node *be_new_Call(dbg_info *dbg, ir_graph *irg, ir_node *bl, ir_node *mem,
483
484
485
		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
486
{
487
	be_call_attr_t *a;
488
	int real_n = n_be_Call_first_arg + n;
Sebastian Hack's avatar
Sebastian Hack committed
489
490
	ir_node *irn;
	ir_node **real_in;
Sebastian Hack's avatar
Sebastian Hack committed
491

492
	NEW_ARR_A(ir_node *, real_in, real_n);
493
494
495
496
	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
497

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

510
511
ir_entity *be_Call_get_entity(const ir_node *call)
{
512
	const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
513
514
515
516
	assert(be_is_Call(call));
	return a->ent;
}

517
518
void be_Call_set_entity(ir_node *call, ir_entity *ent)
{
519
	be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
520
521
522
523
	assert(be_is_Call(call));
	a->ent = ent;
}

524
525
ir_type *be_Call_get_type(ir_node *call)
{
526
	const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
527
528
529
530
	assert(be_is_Call(call));
	return a->call_tp;
}

531
532
void be_Call_set_type(ir_node *call, ir_type *call_tp)
{
533
	be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
534
535
536
537
	assert(be_is_Call(call));
	a->call_tp = call_tp;
}

538
539
void be_Call_set_pop(ir_node *call, unsigned pop)
{
540
	be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
Matthias Braun's avatar
Matthias Braun committed
541
542
543
	a->pop = pop;
}

544
545
unsigned be_Call_get_pop(const ir_node *call)
{
546
	const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
Matthias Braun's avatar
Matthias Braun committed
547
548
549
	return a->pop;
}

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

556
557
	res = new_ir_node(dbg, irg, block, op_be_Return, mode_X, n, in);
	init_node_attr(res, n, 1);
558
	be_set_constr_out(res, 0, arch_no_register_req);
559

560
	a = (be_return_attr_t*)get_irn_generic_attr(res);
561
562
563
564
	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
565

566
	return res;
Sebastian Hack's avatar
Sebastian Hack committed
567
568
}

569
570
int be_Return_get_n_rets(const ir_node *ret)
{
571
	const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
572
573
574
	return a->num_ret_vals;
}

575
576
unsigned be_Return_get_pop(const ir_node *ret)
{
577
	const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
578
579
580
	return a->pop;
}

581
582
int be_Return_get_emit_pop(const ir_node *ret)
{
583
	const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
584
585
586
	return a->emit_pop;
}

587
588
void be_Return_set_emit_pop(ir_node *ret, int emit_pop)
{
589
	be_return_attr_t *a = (be_return_attr_t*)get_irn_generic_attr(ret);
590
591
592
	a->emit_pop = emit_pop;
}

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

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

	/* Set output constraint to stack register. */
611
612
	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
613
614

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

617
618
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
619
{
Sebastian Hack's avatar
Sebastian Hack committed
620
	ir_node *irn;
621
	ir_node *in[n_be_AddSP_last];
622
	ir_graph *irg;
623
	be_node_attr_t *attr;
Sebastian Hack's avatar
Sebastian Hack committed
624

625
626
	in[n_be_AddSP_old_sp] = old_sp;
	in[n_be_AddSP_size]   = sz;
Sebastian Hack's avatar
Sebastian Hack committed
627

628
	irg = get_Block_irg(bl);
629
630
	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);
631
632
	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
633

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

Sebastian Hack's avatar
Sebastian Hack committed
641
642
643
	return irn;
}

644
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
645
646
{
	ir_node *irn;
647
	ir_node *in[n_be_SubSP_last];
648
	ir_graph *irg;
649
	be_node_attr_t *attr;
Michael Beck's avatar
Michael Beck committed
650

651
652
	in[n_be_SubSP_old_sp] = old_sp;
	in[n_be_SubSP_size]   = sz;
Michael Beck's avatar
Michael Beck committed
653

654
	irg = get_Block_irg(bl);
655
656
	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);
657
658
	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
659
660

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

	return irn;
}

669
ir_node *be_new_Start(dbg_info *dbgi, ir_node *bl, int n_outs)
Sebastian Hack's avatar
Sebastian Hack committed
670
{
671
	ir_node *res;
672
	ir_graph *irg = get_Block_irg(bl);
673
	be_node_attr_t *attr;
Sebastian Hack's avatar
Sebastian Hack committed
674

675
	res = new_ir_node(dbgi, irg, bl, op_be_Start, mode_T, 0, NULL);
676
	init_node_attr(res, 0, n_outs);
677
678
	attr = (be_node_attr_t*) get_irn_generic_attr(res);
	attr->exc.pin_state = op_pin_state_pinned;
679
680
681
682

	return res;
}

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

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

	return optimize_node(irn);
Sebastian Hack's avatar
Sebastian Hack committed
701
702
}

703
704
ir_node *be_get_FrameAddr_frame(const ir_node *node)
{
Michael Beck's avatar
Michael Beck committed
705
	assert(be_is_FrameAddr(node));
706
	return get_irn_n(node, n_be_FrameAddr_ptr);
Michael Beck's avatar
Michael Beck committed
707
708
}

Matthias Braun's avatar
Matthias Braun committed
709
710
ir_entity *be_get_FrameAddr_entity(const ir_node *node)
{
711
	const be_frame_attr_t *attr = (const be_frame_attr_t*)get_irn_generic_attr_const(node);
Matthias Braun's avatar
Matthias Braun committed
712
713
714
	return attr->ent;
}

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

	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);
728
	init_node_attr(irn, n + 1, 1);
729
730
	attr = (be_node_attr_t*) get_irn_generic_attr(irn);
	attr->exc.pin_state = op_pin_state_floats;
731
732
	be_node_set_reg_class_in(irn, 0, cls);
	be_node_set_reg_class_out(irn, 0, cls);
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
	switch (get_irn_opcode(irn)) {
755
	case beo_Spill:
756
	case beo_Reload:
757
	case beo_FrameAddr:
758
		return true;
759
	default:
760
		return false;
761
762
763
	}
}

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

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

783
void be_set_MemPerm_in_entity(const