benode.c 37 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
25
 * @file
 * @brief       Backend node support for generic backend nodes.
 * @author      Sebastian Hack
 * @date        17.05.2005
 * @version     $Id$
Sebastian Hack's avatar
Sebastian Hack committed
26
 *
27
 * Backend node support for generic backend nodes.
Michael Beck's avatar
Michael Beck committed
28
 * This file provides Perm, Copy, Spill and Reload nodes.
Sebastian Hack's avatar
Sebastian Hack committed
29
 */
30
#include "config.h"
Sebastian Hack's avatar
Sebastian Hack committed
31
32
33
34
35
36

#include <stdlib.h>

#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"
Sebastian Hack's avatar
Sebastian Hack committed
52

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

59
60
#include "beirgmod.h"

61
62
/** The be_Return nodes attribute type. */
typedef struct {
Matthias Braun's avatar
Matthias Braun committed
63
64
65
	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 */
66
67
} be_return_attr_t;

Michael Beck's avatar
Michael Beck committed
68
/** The be_IncSP attribute type. */
Sebastian Hack's avatar
Sebastian Hack committed
69
typedef struct {
Matthias Braun's avatar
Matthias Braun committed
70
71
72
73
	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
74
} be_incsp_attr_t;
Sebastian Hack's avatar
Sebastian Hack committed
75

76
/** The be_Frame attribute type. */
77
typedef struct {
78
79
	ir_entity      *ent;
	int             offset;
80
81
} be_frame_attr_t;

82
/** The be_Call attribute type. */
83
typedef struct {
Matthias Braun's avatar
Matthias Braun committed
84
	ir_entity      *ent;        /**< called entity if this is a static call. */
85
	unsigned        pop;
Matthias Braun's avatar
Matthias Braun committed
86
	ir_type        *call_tp;    /**< call type, copied from the original Call */
87
88
} be_call_attr_t;

89
typedef struct {
90
91
	ir_entity **in_entities;
	ir_entity **out_entities;
Matthias Braun's avatar
Matthias Braun committed
92
} be_memperm_attr_t;
93

94
95
96
ir_op *op_be_Spill;
ir_op *op_be_Reload;
ir_op *op_be_Perm;
Matthias Braun's avatar
Matthias Braun committed
97
ir_op *op_be_MemPerm;
98
99
ir_op *op_be_Copy;
ir_op *op_be_Keep;
Sebastian Hack's avatar
Sebastian Hack committed
100
ir_op *op_be_CopyKeep;
101
102
103
ir_op *op_be_Call;
ir_op *op_be_Return;
ir_op *op_be_IncSP;
Sebastian Hack's avatar
Sebastian Hack committed
104
ir_op *op_be_AddSP;
Michael Beck's avatar
Michael Beck committed
105
ir_op *op_be_SubSP;
106
ir_op *op_be_Start;
107
ir_op *op_be_FrameAddr;
Sebastian Hack's avatar
Sebastian Hack committed
108

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

119
120
121
	if (a_attr->ent != b_attr->ent || a_attr->offset != b_attr->offset)
		return 1;

122
	return be_nodes_equal(a, b);
123
124
}

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

	if (a_attr->num_ret_vals != b_attr->num_ret_vals)
		return 1;
137
138
	if (a_attr->pop != b_attr->pop)
		return 1;
139
140
	if (a_attr->emit_pop != b_attr->emit_pop)
		return 1;
141

142
	return be_nodes_equal(a, b);
143
144
}

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

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

158
	return be_nodes_equal(a, b);
159
160
}

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

	if (a_attr->ent != b_attr->ent ||
172
		a_attr->call_tp != b_attr->call_tp)
173
174
		return 1;

175
	return be_nodes_equal(a, b);
Michael Beck's avatar
Michael Beck committed
176
177
}

178
static arch_register_req_t *allocate_reg_req(const ir_node *node)
Matthias Braun's avatar
Matthias Braun committed
179
{
180
	ir_graph       *irg  = get_irn_irg(node);
181
	struct obstack *obst = be_get_be_obst(irg);
Matthias Braun's avatar
Matthias Braun committed
182

183
	arch_register_req_t *req = OALLOCZ(obst, arch_register_req_t);
184
185
	return req;
}
Matthias Braun's avatar
Matthias Braun committed
186

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

194
195
196
197
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
198
199
}

200
/**
201
 * Initializes the generic attribute of all be nodes and return it.
202
 */
203
static void init_node_attr(ir_node *node, int n_inputs, int n_outputs)
Sebastian Hack's avatar
Sebastian Hack committed
204
{
205
	ir_graph       *irg  = get_irn_irg(node);
206
	struct obstack *obst = be_get_be_obst(irg);
207
	backend_info_t *info = be_get_info(node);
208
	const arch_register_req_t **in_reqs;
Sebastian Hack's avatar
Sebastian Hack committed
209

210
211
	if (n_inputs >= 0) {
		int i;
212
213
		assert(n_inputs == get_irn_arity(node));
		in_reqs = OALLOCN(obst, const arch_register_req_t*, n_inputs);
214
		for (i = 0; i < n_inputs; ++i) {
215
			in_reqs[i] = arch_no_register_req;
216
217
		}
	} else {
218
		in_reqs = NEW_ARR_F(const arch_register_req_t*, 0);
219
	}
220
	info->in_reqs = in_reqs;
221

222
223
224
225
226
227
228
	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;
		}
229
	} else {
230
		info->out_infos = NEW_ARR_F(reg_out_info_t, 0);
Sebastian Hack's avatar
Sebastian Hack committed
231
232
	}
}
233

234
static void add_register_req_out(ir_node *node)
235
{
236
	backend_info_t *info = be_get_info(node);
237
238
239
240
241
242
243
244
	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)
{
245
246
	backend_info_t *info = be_get_info(node);
	ARR_APP1(const arch_register_req_t*, info->in_reqs, arch_no_register_req);
247
248
}

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

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

266
267
	be_node_set_reg_class_in(res, be_pos_Spill_frame, cls_frame);
	be_node_set_reg_class_in(res, be_pos_Spill_val, cls);
268
269
	/*
	 * For spills and reloads, we return "none" as requirement for frame
270
	 * pointer, so every input is ok. Some backends need this (STA).
271
272
273
	 * Matze: we should investigate if this is really needed, this solution
	 *        looks very hacky to me
	 */
274
	be_set_constr_in(res, be_pos_Spill_frame, arch_no_register_req);
275

276
277
	arch_set_out_register_req(res, 0, arch_no_register_req);

Sebastian Hack's avatar
Sebastian Hack committed
278
279
	return res;
}
280

281
ir_node *be_new_Reload(const arch_register_class_t *cls,
282
		const arch_register_class_t *cls_frame, ir_node *block,
283
		ir_node *frame, ir_node *mem, ir_mode *mode)
284
{
285
286
287
	ir_node  *in[2];
	ir_node  *res;
	ir_graph *irg = get_Block_irg(block);
288
289
290

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

293
	init_node_attr(res, 2, 1);
294
	be_node_set_reg_class_out(res, 0, cls);
295

296
297
	be_node_set_reg_class_in(res, be_pos_Reload_frame, cls_frame);
	arch_irn_set_flags(res, arch_irn_flags_rematerializable);
298
299
300
301
302
303
304

	/*
	 * 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
	 */
305
	be_set_constr_in(res, be_pos_Reload_frame, arch_no_register_req);
306

Sebastian Hack's avatar
Sebastian Hack committed
307
308
	return res;
}
309

310
ir_node *be_get_Reload_mem(const ir_node *irn)
Sebastian Hack's avatar
Sebastian Hack committed
311
312
{
	assert(be_is_Reload(irn));
Daniel Grund's avatar
Daniel Grund committed
313
	return get_irn_n(irn, be_pos_Reload_mem);
Sebastian Hack's avatar
Sebastian Hack committed
314
315
}

316
317
318
319
320
321
ir_node *be_get_Reload_frame(const ir_node *irn)
{
	assert(be_is_Reload(irn));
	return get_irn_n(irn, be_pos_Reload_frame);
}

322
323
324
325
326
ir_node *be_get_Spill_val(const ir_node *irn)
{
	assert(be_is_Spill(irn));
	return get_irn_n(irn, be_pos_Spill_val);
}
Matthias Braun's avatar
Matthias Braun committed
327

328
329
330
331
332
ir_node *be_get_Spill_frame(const ir_node *irn)
{
	assert(be_is_Spill(irn));
	return get_irn_n(irn, be_pos_Spill_frame);
}
333

334
335
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
336
{
337
338
339
	int      i;
	ir_graph *irg = get_Block_irg(block);

340
	ir_node *irn = new_ir_node(NULL, irg, block, op_be_Perm, mode_T, n, in);
341
	init_node_attr(irn, n, n);
342
	for (i = 0; i < n; ++i) {
343
344
		be_node_set_reg_class_in(irn, i, cls);
		be_node_set_reg_class_out(irn, i, cls);
Sebastian Hack's avatar
Sebastian Hack committed
345
346
	}

Sebastian Hack's avatar
Sebastian Hack committed
347
348
	return irn;
}
349

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

	assert(be_is_Perm(perm));
361
	assert(new_size <= arity);
362

363
	new_in = ALLOCAN(ir_node*, new_size);
364

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

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

	set_irn_in(perm, new_size, new_in);
}

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

390
	real_in = ALLOCAN(ir_node*, n + 1);
391
392
393
	real_in[0] = frame;
	memcpy(&real_in[1], in, n * sizeof(real_in[0]));

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

396
	init_node_attr(irn, n + 1, n);
397
	be_node_set_reg_class_in(irn, 0, sp->reg_class);
Matthias Braun's avatar
Matthias Braun committed
398

399
	attr               = (be_memperm_attr_t*)get_irn_generic_attr(irn);
400
401
	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
402
403
404
405

	return irn;
}

406
ir_node *be_new_Copy(const arch_register_class_t *cls, ir_node *bl, ir_node *op)
Sebastian Hack's avatar
Sebastian Hack committed
407
408
409
{
	ir_node *in[1];
	ir_node *res;
Matthias Braun's avatar
Matthias Braun committed
410
	arch_register_req_t *req;
411
	ir_graph *irg = get_Block_irg(bl);
412

Sebastian Hack's avatar
Sebastian Hack committed
413
	in[0] = op;
414
	res   = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
415
	init_node_attr(res, 1, 1);
416
417
	be_node_set_reg_class_in(res, 0, cls);
	be_node_set_reg_class_out(res, 0, cls);
Matthias Braun's avatar
Matthias Braun committed
418

419
420
421
	req = allocate_reg_req(res);
	req->cls        = cls;
	req->type       = arch_register_req_type_should_be_same;
422
	req->other_same = 1U << 0;
423
	req->width      = 1;
424
	be_set_constr_out(res, 0, req);
Matthias Braun's avatar
Matthias Braun committed
425

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

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

434
435
void be_set_Copy_op(ir_node *cpy, ir_node *op)
{
436
437
438
	set_irn_n(cpy, be_pos_Copy_op, op);
}

439
ir_node *be_new_Keep(ir_node *block, int n, ir_node *in[])
Sebastian Hack's avatar
Sebastian Hack committed
440
{
Sebastian Hack's avatar
Sebastian Hack committed
441
	int i;
442
	ir_node *res;
443
	ir_graph *irg = get_Block_irg(block);
444

445
446
	res = new_ir_node(NULL, irg, block, op_be_Keep, mode_ANY, -1, NULL);
	init_node_attr(res, -1, 1);
Sebastian Hack's avatar
Sebastian Hack committed
447

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

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

476
477
478
479
480
	NEW_ARR_A(ir_node *, real_in, real_n);
	real_in[be_pos_Call_mem] = mem;
	real_in[be_pos_Call_sp]  = sp;
	real_in[be_pos_Call_ptr] = ptr;
	memcpy(&real_in[be_pos_Call_first_arg], in, n * sizeof(in[0]));
Sebastian Hack's avatar
Sebastian Hack committed
481

482
	irn = new_ir_node(dbg, irg, bl, op_be_Call, mode_T, real_n, real_in);
483
484
	init_node_attr(irn, real_n, n_outs);
	a          = (be_call_attr_t*)get_irn_generic_attr(irn);
485
486
	a->ent     = NULL;
	a->call_tp = call_tp;
Matthias Braun's avatar
Matthias Braun committed
487
	a->pop     = 0;
Sebastian Hack's avatar
Sebastian Hack committed
488
	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
489
490
}

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

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

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

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

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

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

531
ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *block, int n_res,
532
                       unsigned pop, int n, ir_node *in[])
Sebastian Hack's avatar
Sebastian Hack committed
533
{
534
	be_return_attr_t *a;
535
536
537
538
	ir_node *res;
	int i;

	res = new_ir_node(dbg, irg, block, op_be_Return, mode_X, -1, NULL);
539
540
	init_node_attr(res, -1, 1);
	for (i = 0; i < n; ++i) {
541
		add_irn_n(res, in[i]);
542
		add_register_req_in(res);
543
	}
544
	be_set_constr_out(res, 0, arch_no_register_req);
545

546
	a = (be_return_attr_t*)get_irn_generic_attr(res);
547
	a->num_ret_vals = n_res;
548
	a->pop          = pop;
549
	a->emit_pop     = 0;
Sebastian Hack's avatar
Sebastian Hack committed
550

551
	return res;
Sebastian Hack's avatar
Sebastian Hack committed
552
553
}

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

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

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

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

578
579
int be_Return_append_node(ir_node *ret, ir_node *node)
{
580
581
582
	int pos;

	pos = add_irn_n(ret, node);
583
	add_register_req_in(ret);
584
585
586
587

	return pos;
}

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

	in[0]     = old_sp;
597
598
	irn       = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode,
	                        sizeof(in) / sizeof(in[0]), in);
599
600
	init_node_attr(irn, 1, 1);
	a         = (be_incsp_attr_t*)get_irn_generic_attr(irn);
Sebastian Hack's avatar
Sebastian Hack committed
601
	a->offset = offset;
602
	a->align  = align;
Sebastian Hack's avatar
Sebastian Hack committed
603
604

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

	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
609
}
Sebastian Hack's avatar
Sebastian Hack committed
610

611
612
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
613
{
Sebastian Hack's avatar
Sebastian Hack committed
614
	ir_node *irn;
Sebastian Hack's avatar
Sebastian Hack committed
615
	ir_node *in[be_pos_AddSP_last];
616
	const arch_register_class_t *cls;
617
	ir_graph *irg;
Sebastian Hack's avatar
Sebastian Hack committed
618

Sebastian Hack's avatar
Sebastian Hack committed
619
620
	in[be_pos_AddSP_old_sp] = old_sp;
	in[be_pos_AddSP_size]   = sz;
Sebastian Hack's avatar
Sebastian Hack committed
621

622
	irg = get_Block_irg(bl);
623
	irn = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T, be_pos_AddSP_last, in);
624
	init_node_attr(irn, be_pos_AddSP_last, pn_be_AddSP_last);
Sebastian Hack's avatar
Sebastian Hack committed
625

Sebastian Hack's avatar
Sebastian Hack committed
626
	/* Set output constraint to stack register. */
627
628
	be_set_constr_single_reg_in(irn, be_pos_AddSP_old_sp, sp,
	                            arch_register_req_type_none);
629
	be_node_set_reg_class_in(irn, be_pos_AddSP_size, arch_register_get_class(sp));
Matthias Braun's avatar
Matthias Braun committed
630
631
	be_set_constr_single_reg_out(irn, pn_be_AddSP_sp, sp,
	                             arch_register_req_type_produces_sp);
632

633
	cls = arch_register_get_class(sp);
Sebastian Hack's avatar
Sebastian Hack committed
634
635
636
637

	return irn;
}

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

	in[be_pos_SubSP_old_sp] = old_sp;
	in[be_pos_SubSP_size]   = sz;

647
	irg = get_Block_irg(bl);
Michael Beck's avatar
Michael Beck committed
648
	irn = new_ir_node(NULL, irg, bl, op_be_SubSP, mode_T, be_pos_SubSP_last, in);
649
	init_node_attr(irn, be_pos_SubSP_last, pn_be_SubSP_last);
Michael Beck's avatar
Michael Beck committed
650
651

	/* Set output constraint to stack register. */
652
653
	be_set_constr_single_reg_in(irn, be_pos_SubSP_old_sp, sp,
	                            arch_register_req_type_none);
654
655
	be_node_set_reg_class_in(irn, be_pos_SubSP_size, arch_register_get_class(sp));
	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
656
657
658
659

	return irn;
}

660
ir_node *be_new_Start(dbg_info *dbgi, ir_node *bl, int n_outs)
Sebastian Hack's avatar
Sebastian Hack committed
661
{
662
663
	ir_node *res;
	int i;
664
	ir_graph *irg = get_Block_irg(bl);
Sebastian Hack's avatar
Sebastian Hack committed
665

666
	res = new_ir_node(dbgi, irg, bl, op_be_Start, mode_T, 0, NULL);
667
668
669
670
	init_node_attr(res, 0, -1);
	for (i = 0; i < n_outs; ++i) {
		add_register_req_out(res);
	}
671
672
673
674

	return res;
}

675
ir_node *be_new_FrameAddr(const arch_register_class_t *cls_frame, ir_node *bl, ir_node *frame, ir_entity *ent)
Sebastian Hack's avatar
Sebastian Hack committed
676
{
677
	be_frame_attr_t *a;
Sebastian Hack's avatar
Sebastian Hack committed
678
679
	ir_node *irn;
	ir_node *in[1];
680
	ir_graph *irg = get_Block_irg(bl);
Sebastian Hack's avatar
Sebastian Hack committed
681

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

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

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

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

706
ir_node *be_new_CopyKeep(const arch_register_class_t *cls, ir_node *bl, ir_node *src, int n, ir_node *in_keep[], ir_mode *mode)
Sebastian Hack's avatar
Sebastian Hack committed
707
{
708
709
	ir_node  *irn;
	ir_node **in = ALLOCAN(ir_node*, n + 1);
710
	ir_graph *irg = get_Block_irg(bl);
Sebastian Hack's avatar
Sebastian Hack committed
711
712
713
714

	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);
715
	init_node_attr(irn, n + 1, 1);
716
717
	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
718
719
720
721

	return irn;
}

722
ir_node *be_new_CopyKeep_single(const arch_register_class_t *cls, ir_node *bl, ir_node *src, ir_node *keep, ir_mode *mode)
Sebastian Hack's avatar
Sebastian Hack committed
723
{
724
	return be_new_CopyKeep(cls, bl, src, 1, &keep, mode);
Sebastian Hack's avatar
Sebastian Hack committed
725
726
}

727
728
ir_node *be_get_CopyKeep_op(const ir_node *cpy)
{
729
730
731
	return get_irn_n(cpy, be_pos_CopyKeep_op);
}

732
733
void be_set_CopyKeep_op(ir_node *cpy, ir_node *op)
{
734
735
736
	set_irn_n(cpy, be_pos_CopyKeep_op, op);
}

737
static bool be_has_frame_entity(const ir_node *irn)
738
{
739
	switch (get_irn_opcode(irn)) {
740
	case beo_Spill:
741
	case beo_Reload:
742
	case beo_FrameAddr:
743
		return true;
744
	default:
745
		return false;
746
747
748
	}
}

749
ir_entity *be_get_frame_entity(const ir_node *irn)
750
{
751
	if (be_has_frame_entity(irn)) {
752
		const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
753
754
755
756
		return a->ent;
	}
	return NULL;
}
Sebastian Hack's avatar
Sebastian Hack committed
757

758
759
760
761
int be_get_frame_offset(const ir_node *irn)
{
	assert(is_be_node(irn));
	if (be_has_frame_entity(irn)) {
762
		const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
763
764
765
766
767
		return a->offset;
	}
	return 0;
}

768
void be_set_MemPerm_in_entity(const ir_node *irn, int n, ir_entity *ent)
Matthias Braun's avatar
Matthias Braun committed
769
{
770
	const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
771
772

	assert(be_is_MemPerm(irn));
773
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
774
775
776
777

	attr->in_entities[n] = ent;
}

778
ir_entity* be_get_MemPerm_in_entity(const ir_node* irn, int n)
Matthias Braun's avatar
Matthias Braun committed
779
{
780
	const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
781
782

	assert(be_is_MemPerm(irn));
783
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
784
785
786
787

	return attr->in_entities[n];
}

788
void be_set_MemPerm_out_entity(const ir_node *irn, int n, ir_entity *ent)
Matthias Braun's avatar
Matthias Braun committed
789
{
790
	const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
791
792

	assert(be_is_MemPerm(irn));
793
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
794
795
796
797

	attr->out_entities[n] = ent;
}

798
ir_entity* be_get_MemPerm_out_entity(const ir_node* irn, int n)
Matthias Braun's avatar
Matthias Braun committed
799
{
800
	const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
801
802

	assert(be_is_MemPerm(irn));
803
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
804
805
806
807

	return attr->out_entities[n];
}