benode.c 38.8 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"
Sebastian Hack's avatar
Sebastian Hack committed
51

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

58
59
#include "beirgmod.h"

60
61
62
63
typedef struct be_node_attr_t {
	except_attr  exc;
} be_node_attr_t;

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

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

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

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

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

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

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

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

130
	return be_nodes_equal(a, b);
131
132
}

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

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

150
	return be_nodes_equal(a, b);
151
152
}

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

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

166
	return be_nodes_equal(a, b);
167
168
}

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

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

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

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

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

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

202
203
204
205
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
206
207
}

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

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

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

242
static void add_register_req_out(ir_node *node)
243
{
244
	backend_info_t *info = be_get_info(node);
245
246
247
248
249
250
251
252
	reg_out_info_t  out_info;
	memset(&out_info, 0, sizeof(out_info));
	out_info.req = arch_no_register_req;
	ARR_APP1(reg_out_info_t, info->out_infos, out_info);
}

static void add_register_req_in(ir_node *node)
{
253
254
	backend_info_t *info = be_get_info(node);
	ARR_APP1(const arch_register_req_t*, info->in_reqs, arch_no_register_req);
255
256
}

257
258
259
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)
260
{
Matthias Braun's avatar
Matthias Braun committed
261
	be_frame_attr_t *a;
262
	ir_node         *in[2];
263
	ir_node         *res;
264
	ir_graph        *irg = get_Block_irg(bl);
265

266
267
268
	in[0]     = frame;
	in[1]     = to_spill;
	res       = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
269
270
	init_node_attr(res, 2, 1);
	a         = (be_frame_attr_t*) get_irn_generic_attr(res);
271
	a->ent    = NULL;
Matthias Braun's avatar
Matthias Braun committed
272
	a->offset = 0;
273
	a->base.exc.pin_state = op_pin_state_pinned;
Sebastian Hack's avatar
Sebastian Hack committed
274

275
276
	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);
277
278
	/*
	 * For spills and reloads, we return "none" as requirement for frame
279
	 * pointer, so every input is ok. Some backends need this (STA).
280
281
282
	 * Matze: we should investigate if this is really needed, this solution
	 *        looks very hacky to me
	 */
283
	be_set_constr_in(res, n_be_Spill_frame, arch_no_register_req);
284

285
	arch_set_irn_register_req_out(res, 0, arch_no_register_req);
286

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

290
ir_node *be_new_Reload(const arch_register_class_t *cls,
291
		const arch_register_class_t *cls_frame, ir_node *block,
292
		ir_node *frame, ir_node *mem, ir_mode *mode)
293
{
294
295
296
	ir_node  *in[2];
	ir_node  *res;
	ir_graph *irg = get_Block_irg(block);
297
	be_frame_attr_t *a;
298
299
300

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

303
	init_node_attr(res, 2, 1);
304
	be_node_set_reg_class_out(res, 0, cls);
305

306
	be_node_set_reg_class_in(res, n_be_Reload_frame, cls_frame);
307
	arch_set_irn_flags(res, arch_irn_flags_rematerializable);
308

309
310
311
312
313
	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;

314
315
316
317
318
319
	/*
	 * For spills and reloads, we return "none" as requirement for frame
	 * pointer, so every input is ok. Some backends need this (e.g. STA).
	 * Matze: we should investigate if this is really needed, this solution
	 *        looks very hacky to me
	 */
320
	be_set_constr_in(res, n_be_Reload_frame, arch_no_register_req);
321

Sebastian Hack's avatar
Sebastian Hack committed
322
323
	return res;
}
324

325
ir_node *be_get_Reload_mem(const ir_node *irn)
Sebastian Hack's avatar
Sebastian Hack committed
326
327
{
	assert(be_is_Reload(irn));
328
	return get_irn_n(irn, n_be_Reload_mem);
Sebastian Hack's avatar
Sebastian Hack committed
329
330
}

331
332
333
ir_node *be_get_Reload_frame(const ir_node *irn)
{
	assert(be_is_Reload(irn));
334
	return get_irn_n(irn, n_be_Reload_frame);
335
336
}

337
338
339
ir_node *be_get_Spill_val(const ir_node *irn)
{
	assert(be_is_Spill(irn));
340
	return get_irn_n(irn, n_be_Spill_val);
341
}
Matthias Braun's avatar
Matthias Braun committed
342

343
344
345
ir_node *be_get_Spill_frame(const ir_node *irn)
{
	assert(be_is_Spill(irn));
346
	return get_irn_n(irn, n_be_Spill_frame);
347
}
348

349
350
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
351
{
352
353
354
	int            i;
	ir_graph       *irg = get_Block_irg(block);
	be_node_attr_t *attr;
355

356
	ir_node *irn = new_ir_node(NULL, irg, block, op_be_Perm, mode_T, n, in);
357
	init_node_attr(irn, n, n);
358
359
	attr                = (be_node_attr_t*) get_irn_generic_attr(irn);
	attr->exc.pin_state = op_pin_state_pinned;
360
	for (i = 0; i < n; ++i) {
361
362
363
364
365
366
367
368
369
370
371
372
373
		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
374
375
	}

Sebastian Hack's avatar
Sebastian Hack committed
376
377
	return irn;
}
378

379
380
void be_Perm_reduce(ir_node *perm, int new_size, int *map)
{
381
382
383
384
385
	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);
386
	ir_node        **new_in;
387
	int              i;
388
389

	assert(be_is_Perm(perm));
390
	assert(new_size <= arity);
391

392
	new_in = ALLOCAN(ir_node*, new_size);
393

394
	/* save the old register data */
395
	memcpy(old_in_reqs, info->in_reqs, arity * sizeof(old_in_reqs[0]));
396
	memcpy(old_infos, info->out_infos, arity * sizeof(old_infos[0]));
397

Matthias Braun's avatar
Matthias Braun committed
398
	/* compose the new in array and set the new register data directly */
399
	for (i = 0; i < new_size; ++i) {
400
		int idx = map[i];
401
		new_in[i]          = get_irn_n(perm, idx);
402
		info->in_reqs[i]   = old_in_reqs[idx];
403
		info->out_infos[i] = old_infos[idx];
404
405
406
407
408
	}

	set_irn_in(perm, new_size, new_in);
}

409
ir_node *be_new_MemPerm(ir_node *block, int n, ir_node *in[])
Matthias Braun's avatar
Matthias Braun committed
410
{
411
412
	ir_graph                     *irg       = get_Block_irg(block);
	const arch_env_t             *arch_env  = be_get_irg_arch_env(irg);
413
414
415
416
417
	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
418

419
	real_in = ALLOCAN(ir_node*, n + 1);
420
421
422
	real_in[0] = frame;
	memcpy(&real_in[1], in, n * sizeof(real_in[0]));

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

425
	init_node_attr(irn, n + 1, n);
426
	be_node_set_reg_class_in(irn, 0, sp->reg_class);
Matthias Braun's avatar
Matthias Braun committed
427

428
	attr               = (be_memperm_attr_t*)get_irn_generic_attr(irn);
429
430
	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
431
432
433
434

	return irn;
}

435
ir_node *be_new_Copy(ir_node *bl, ir_node *op)
Sebastian Hack's avatar
Sebastian Hack committed
436
437
438
{
	ir_node *in[1];
	ir_node *res;
Matthias Braun's avatar
Matthias Braun committed
439
	arch_register_req_t *req;
440
	be_node_attr_t *attr;
441
	ir_graph *irg = get_Block_irg(bl);
442
443
	const arch_register_req_t   *in_req = arch_get_irn_register_req(op);
	const arch_register_class_t *cls    = in_req->cls;
444

Sebastian Hack's avatar
Sebastian Hack committed
445
	in[0] = op;
446
	res   = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
447
	init_node_attr(res, 1, 1);
448
449
	attr = (be_node_attr_t*) get_irn_generic_attr(res);
	attr->exc.pin_state = op_pin_state_floats;
450
451
	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
452

453
454
	req = allocate_reg_req(res);
	req->cls        = cls;
455
456
	req->type       = arch_register_req_type_should_be_same
		| (in_req->type & arch_register_req_type_aligned);
457
	req->other_same = 1U << 0;
458
	req->width      = in_req->width;
459
	be_set_constr_out(res, 0, req);
Matthias Braun's avatar
Matthias Braun committed
460

Sebastian Hack's avatar
Sebastian Hack committed
461
	return res;
462
463
}

464
465
ir_node *be_get_Copy_op(const ir_node *cpy)
{
466
	return get_irn_n(cpy, n_be_Copy_op);
467
468
}

469
470
void be_set_Copy_op(ir_node *cpy, ir_node *op)
{
471
	set_irn_n(cpy, n_be_Copy_op, op);
472
473
}

474
ir_node *be_new_Keep(ir_node *block, int n, ir_node *in[])
Sebastian Hack's avatar
Sebastian Hack committed
475
{
Sebastian Hack's avatar
Sebastian Hack committed
476
	int i;
477
	ir_node *res;
478
	ir_graph *irg = get_Block_irg(block);
479
	be_node_attr_t *attr;
480

481
482
	res = new_ir_node(NULL, irg, block, op_be_Keep, mode_ANY, -1, NULL);
	init_node_attr(res, -1, 1);
483
484
	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
485

486
	for (i = 0; i < n; ++i) {
487
		add_irn_n(res, in[i]);
488
		add_register_req_in(res);
Sebastian Hack's avatar
Sebastian Hack committed
489
	}
490
491
492
493
494
495
496
497
498
499
500
	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);
501
	add_register_req_in(keep);
502
	be_node_set_reg_class_in(keep, n, cls);
Sebastian Hack's avatar
Sebastian Hack committed
503
504
}

505
506
507
ir_node *be_new_Call(dbg_info *dbg, ir_graph *irg, ir_node *bl, ir_node *mem,
		ir_node *sp, ir_node *ptr, int n_outs, int n, ir_node *in[],
		ir_type *call_tp)
Sebastian Hack's avatar
Sebastian Hack committed
508
{
509
	be_call_attr_t *a;
510
	int real_n = n_be_Call_first_arg + n;
Sebastian Hack's avatar
Sebastian Hack committed
511
512
	ir_node *irn;
	ir_node **real_in;
Sebastian Hack's avatar
Sebastian Hack committed
513

514
	NEW_ARR_A(ir_node *, real_in, real_n);
515
516
517
518
	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
519

520
	irn = new_ir_node(dbg, irg, bl, op_be_Call, mode_T, real_n, real_in);
521
	init_node_attr(irn, real_n, n_outs);
522
523
524
525
526
	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;
Sebastian Hack's avatar
Sebastian Hack committed
527
	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
528
529
}

530
531
ir_entity *be_Call_get_entity(const ir_node *call)
{
532
	const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
533
534
535
536
	assert(be_is_Call(call));
	return a->ent;
}

537
538
void be_Call_set_entity(ir_node *call, ir_entity *ent)
{
539
	be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
540
541
542
543
	assert(be_is_Call(call));
	a->ent = ent;
}

544
545
ir_type *be_Call_get_type(ir_node *call)
{
546
	const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
547
548
549
550
	assert(be_is_Call(call));
	return a->call_tp;
}

551
552
void be_Call_set_type(ir_node *call, ir_type *call_tp)
{
553
	be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
554
555
556
557
	assert(be_is_Call(call));
	a->call_tp = call_tp;
}

558
559
void be_Call_set_pop(ir_node *call, unsigned pop)
{
560
	be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
Matthias Braun's avatar
Matthias Braun committed
561
562
563
	a->pop = pop;
}

564
565
unsigned be_Call_get_pop(const ir_node *call)
{
566
	const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
Matthias Braun's avatar
Matthias Braun committed
567
568
569
	return a->pop;
}

570
ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *block, int n_res,
571
                       unsigned pop, int n, ir_node *in[])
Sebastian Hack's avatar
Sebastian Hack committed
572
{
573
	be_return_attr_t *a;
574
575
576
577
	ir_node *res;
	int i;

	res = new_ir_node(dbg, irg, block, op_be_Return, mode_X, -1, NULL);
578
579
	init_node_attr(res, -1, 1);
	for (i = 0; i < n; ++i) {
580
		add_irn_n(res, in[i]);
581
		add_register_req_in(res);
582
	}
583
	be_set_constr_out(res, 0, arch_no_register_req);
584

585
	a = (be_return_attr_t*)get_irn_generic_attr(res);
586
587
588
589
	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
590

591
	return res;
Sebastian Hack's avatar
Sebastian Hack committed
592
593
}

594
595
int be_Return_get_n_rets(const ir_node *ret)
{
596
	const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
597
598
599
	return a->num_ret_vals;
}

600
601
unsigned be_Return_get_pop(const ir_node *ret)
{
602
	const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
603
604
605
	return a->pop;
}

606
607
int be_Return_get_emit_pop(const ir_node *ret)
{
608
	const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
609
610
611
	return a->emit_pop;
}

612
613
void be_Return_set_emit_pop(ir_node *ret, int emit_pop)
{
614
	be_return_attr_t *a = (be_return_attr_t*)get_irn_generic_attr(ret);
615
616
617
	a->emit_pop = emit_pop;
}

618
619
int be_Return_append_node(ir_node *ret, ir_node *node)
{
620
621
622
	int pos;

	pos = add_irn_n(ret, node);
623
	add_register_req_in(ret);
624
625
626
627

	return pos;
}

628
ir_node *be_new_IncSP(const arch_register_t *sp, ir_node *bl,
629
                      ir_node *old_sp, int offset, int align)
Sebastian Hack's avatar
Sebastian Hack committed
630
{
Michael Beck's avatar
Michael Beck committed
631
	be_incsp_attr_t *a;
Sebastian Hack's avatar
Sebastian Hack committed
632
	ir_node *irn;
Sebastian Hack's avatar
Sebastian Hack committed
633
	ir_node *in[1];
634
	ir_graph *irg = get_Block_irg(bl);
Sebastian Hack's avatar
Sebastian Hack committed
635
636

	in[0]     = old_sp;
637
	irn       = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode,
638
	                        ARRAY_SIZE(in), in);
639
	init_node_attr(irn, 1, 1);
640
641
642
643
	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
644
645

	/* Set output constraint to stack register. */
646
647
	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
648
649

	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
650
}
Sebastian Hack's avatar
Sebastian Hack committed
651

652
653
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
654
{
Sebastian Hack's avatar
Sebastian Hack committed
655
	ir_node *irn;
656
	ir_node *in[n_be_AddSP_last];
657
	ir_graph *irg;
658
	be_node_attr_t *attr;
Sebastian Hack's avatar
Sebastian Hack committed
659

660
661
	in[n_be_AddSP_old_sp] = old_sp;
	in[n_be_AddSP_size]   = sz;
Sebastian Hack's avatar
Sebastian Hack committed
662

663
	irg = get_Block_irg(bl);
664
665
	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);
666
667
	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
668

Sebastian Hack's avatar
Sebastian Hack committed
669
	/* Set output constraint to stack register. */
670
	be_set_constr_single_reg_in(irn, n_be_AddSP_old_sp, sp,
671
	                            arch_register_req_type_none);
672
	be_node_set_reg_class_in(irn, n_be_AddSP_size, sp->reg_class);
Matthias Braun's avatar
Matthias Braun committed
673
674
	be_set_constr_single_reg_out(irn, pn_be_AddSP_sp, sp,
	                             arch_register_req_type_produces_sp);
675

Sebastian Hack's avatar
Sebastian Hack committed
676
677
678
	return irn;
}

679
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
680
681
{
	ir_node *irn;
682
	ir_node *in[n_be_SubSP_last];
683
	ir_graph *irg;
684
	be_node_attr_t *attr;
Michael Beck's avatar
Michael Beck committed
685

686
687
	in[n_be_SubSP_old_sp] = old_sp;
	in[n_be_SubSP_size]   = sz;
Michael Beck's avatar
Michael Beck committed
688

689
	irg = get_Block_irg(bl);
690
691
	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);
692
693
	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
694
695

	/* Set output constraint to stack register. */
696
	be_set_constr_single_reg_in(irn, n_be_SubSP_old_sp, sp,
697
	                            arch_register_req_type_none);
698
	be_node_set_reg_class_in(irn, n_be_SubSP_size, sp->reg_class);
699
	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
700
701
702
703

	return irn;
}

704
ir_node *be_new_Start(dbg_info *dbgi, ir_node *bl, int n_outs)
Sebastian Hack's avatar
Sebastian Hack committed
705
{
706
707
	ir_node *res;
	int i;
708
	ir_graph *irg = get_Block_irg(bl);
709
	be_node_attr_t *attr;
Sebastian Hack's avatar
Sebastian Hack committed
710

711
	res = new_ir_node(dbgi, irg, bl, op_be_Start, mode_T, 0, NULL);
712
	init_node_attr(res, 0, -1);
713
714
	attr = (be_node_attr_t*) get_irn_generic_attr(res);
	attr->exc.pin_state = op_pin_state_pinned;
715
716
717
	for (i = 0; i < n_outs; ++i) {
		add_register_req_out(res);
	}
718
719
720
721

	return res;
}

722
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
723
{
724
	be_frame_attr_t *a;
Sebastian Hack's avatar
Sebastian Hack committed
725
726
	ir_node *irn;
	ir_node *in[1];
727
	ir_graph *irg = get_Block_irg(bl);
Sebastian Hack's avatar
Sebastian Hack committed
728

729
	in[0]  = frame;
730
	irn    = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
731
	init_node_attr(irn, 1, 1);
732
733
734
735
	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;
736
737
	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
738
739

	return optimize_node(irn);
Sebastian Hack's avatar
Sebastian Hack committed
740
741
}

742
743
ir_node *be_get_FrameAddr_frame(const ir_node *node)
{
Michael Beck's avatar
Michael Beck committed
744
	assert(be_is_FrameAddr(node));
745
	return get_irn_n(node, n_be_FrameAddr_ptr);
Michael Beck's avatar
Michael Beck committed
746
747
}

Matthias Braun's avatar
Matthias Braun committed
748
749
ir_entity *be_get_FrameAddr_entity(const ir_node *node)
{
750
	const be_frame_attr_t *attr = (const be_frame_attr_t*)get_irn_generic_attr_const(node);
Matthias Braun's avatar
Matthias Braun committed
751
752
753
	return attr->ent;
}

754
ir_node *be_new_CopyKeep(ir_node *bl, ir_node *src, int n, ir_node *in_keep[])
Sebastian Hack's avatar
Sebastian Hack committed
755
{
756
757
	ir_node  *irn;
	ir_node **in = ALLOCAN(ir_node*, n + 1);
758
	ir_graph *irg = get_Block_irg(bl);
759
760
761
	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);
762
	be_node_attr_t *attr;
Sebastian Hack's avatar
Sebastian Hack committed
763
764
765
766

	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);
767
	init_node_attr(irn, n + 1, 1);
768
769
	attr = (be_node_attr_t*) get_irn_generic_attr(irn);
	attr->exc.pin_state = op_pin_state_floats;
770
771
	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
772
773
774
775

	return irn;
}

776
ir_node *be_new_CopyKeep_single(ir_node *bl, ir_node *src, ir_node *keep)
Sebastian Hack's avatar
Sebastian Hack committed
777
{
778
	return be_new_CopyKeep(bl, src, 1, &keep);
Sebastian Hack's avatar
Sebastian Hack committed
779
780
}

781
782
ir_node *be_get_CopyKeep_op(const ir_node *cpy)
{
783
	return get_irn_n(cpy, n_be_CopyKeep_op);
784
785
}

786
787
void be_set_CopyKeep_op(ir_node *cpy, ir_node *op)
{
788
	set_irn_n(cpy, n_be_CopyKeep_op, op);
789
790
}