benode.c 41.2 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
/*
Michael Beck's avatar
Michael Beck committed
2
 * Copyright (C) 1995-2008 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 "offset.h"
Sebastian Hack's avatar
Sebastian Hack committed
41
#include "bitfiddle.h"
Matthias Braun's avatar
Matthias Braun committed
42
#include "raw_bitset.h"
43
#include "error.h"
44
#include "array_t.h"
Sebastian Hack's avatar
Sebastian Hack committed
45
46
47
48

#include "irop_t.h"
#include "irmode_t.h"
#include "irnode_t.h"
49
#include "ircons_t.h"
50
#include "irprintf.h"
Sebastian Hack's avatar
Sebastian Hack committed
51
#include "irgwalk.h"
Michael Beck's avatar
Michael Beck committed
52
#include "iropt_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"

Matthias Braun's avatar
Matthias Braun committed
62
63
#define get_irn_attr(irn) get_irn_generic_attr(irn)
#define get_irn_attr_const(irn) get_irn_generic_attr_const(irn)
Daniel Grund's avatar
Daniel Grund committed
64

Sebastian Hack's avatar
Sebastian Hack committed
65
typedef struct {
66
	const arch_register_req_t *in_req;
67
68
} be_reg_data_t;

69
/** The generic be nodes attribute type. */
70
typedef struct {
Michael Beck's avatar
Michael Beck committed
71
	be_reg_data_t *reg_data;
72
73
} be_node_attr_t;

74
75
/** The be_Return nodes attribute type. */
typedef struct {
Michael Beck's avatar
Michael Beck committed
76
	be_node_attr_t node_attr;     /**< base attributes of every be node. */
77
	int            num_ret_vals;  /**< number of return values */
78
	unsigned       pop;           /**< number of bytes that should be popped */
79
	int            emit_pop;      /**< if set, emit pop bytes, even if pop = 0 */
80
81
} be_return_attr_t;

Michael Beck's avatar
Michael Beck committed
82
/** The be_IncSP attribute type. */
Sebastian Hack's avatar
Sebastian Hack committed
83
typedef struct {
Michael Beck's avatar
Michael Beck committed
84
	be_node_attr_t node_attr;   /**< base attributes of every be node. */
85
	int            offset;      /**< The offset by which the stack shall be expanded/shrinked. */
86
87
	int            align;       /**< whether stack should be aligned after the
	                                 IncSP */
Michael Beck's avatar
Michael Beck committed
88
} be_incsp_attr_t;
Sebastian Hack's avatar
Sebastian Hack committed
89

90
/** The be_Frame attribute type. */
91
typedef struct {
92
93
94
	be_node_attr_t  node_attr;   /**< base attributes of every be node. */
	ir_entity      *ent;
	int             offset;
95
96
} be_frame_attr_t;

97
/** The be_Call attribute type. */
98
typedef struct {
99
	be_node_attr_t  node_attr;  /**< base attributes of every be node. */
100
	ir_entity      *ent;        /**< The called entity if this is a static call. */
101
	unsigned        pop;
102
	ir_type        *call_tp;    /**< The call type, copied from the original Call node. */
103
104
} be_call_attr_t;

105
typedef struct {
106
107
108
	be_node_attr_t   node_attr;  /**< base attributes of every be node. */
	ir_entity      **in_entities;
	ir_entity      **out_entities;
Matthias Braun's avatar
Matthias Braun committed
109
} be_memperm_attr_t;
110

111
112
113
ir_op *op_be_Spill;
ir_op *op_be_Reload;
ir_op *op_be_Perm;
Matthias Braun's avatar
Matthias Braun committed
114
ir_op *op_be_MemPerm;
115
116
ir_op *op_be_Copy;
ir_op *op_be_Keep;
Sebastian Hack's avatar
Sebastian Hack committed
117
ir_op *op_be_CopyKeep;
118
119
120
ir_op *op_be_Call;
ir_op *op_be_Return;
ir_op *op_be_IncSP;
Sebastian Hack's avatar
Sebastian Hack committed
121
ir_op *op_be_AddSP;
Michael Beck's avatar
Michael Beck committed
122
ir_op *op_be_SubSP;
123
124
ir_op *op_be_RegParams;
ir_op *op_be_FrameAddr;
125
ir_op *op_be_Barrier;
Sebastian Hack's avatar
Sebastian Hack committed
126

Sebastian Hack's avatar
Sebastian Hack committed
127
static const ir_op_ops be_node_op_ops;
Sebastian Hack's avatar
Sebastian Hack committed
128

129
130
131
132
133
134
135
136
137
138
#define N   irop_flag_none
#define L   irop_flag_labeled
#define C   irop_flag_commutative
#define X   irop_flag_cfopcode
#define I   irop_flag_ip_cfopcode
#define F   irop_flag_fragile
#define Y   irop_flag_forking
#define H   irop_flag_highlevel
#define c   irop_flag_constlike
#define K   irop_flag_keep
139
#define M   irop_flag_uses_memory
140

Michael Beck's avatar
Michael Beck committed
141
/**
Michael Beck's avatar
Michael Beck committed
142
 * Compare two be node attributes.
Michael Beck's avatar
Michael Beck committed
143
144
145
 *
 * @return zero if both attributes are identically
 */
146
147
148
149
150
static int node_cmp_attr(ir_node *a, ir_node *b)
{
	const be_node_attr_t *a_attr = get_irn_attr_const(a);
	const be_node_attr_t *b_attr = get_irn_attr_const(b);
	int i, len = ARR_LEN(a_attr->reg_data);
151

152
153
154
155
	if (len != ARR_LEN(b_attr->reg_data))
		return 1;

	if (!be_nodes_equal(a, b))
156
		return 1;
Michael Beck's avatar
Michael Beck committed
157

158
	for (i = len - 1; i >= 0; --i) {
159
160
		if (!reg_reqs_equal(a_attr->reg_data[i].in_req,
		                    b_attr->reg_data[i].in_req))
161
			return 1;
Michael Beck's avatar
Michael Beck committed
162
	}
163
164

	return 0;
Michael Beck's avatar
Michael Beck committed
165
166
167
}

/**
Michael Beck's avatar
Michael Beck committed
168
 * Compare the attributes of two be_FrameAddr nodes.
Michael Beck's avatar
Michael Beck committed
169
 *
Michael Beck's avatar
Michael Beck committed
170
 * @return zero if both nodes have identically attributes
Michael Beck's avatar
Michael Beck committed
171
 */
172
173
static int FrameAddr_cmp_attr(ir_node *a, ir_node *b)
{
Michael Beck's avatar
Michael Beck committed
174
175
	const be_frame_attr_t *a_attr = get_irn_attr_const(a);
	const be_frame_attr_t *b_attr = get_irn_attr_const(b);
Michael Beck's avatar
Michael Beck committed
176

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

180
	return node_cmp_attr(a, b);
181
182
}

Michael Beck's avatar
Michael Beck committed
183
184
185
186
187
/**
 * Compare the attributes of two be_Return nodes.
 *
 * @return zero if both nodes have identically attributes
 */
188
189
static int Return_cmp_attr(ir_node *a, ir_node *b)
{
Michael Beck's avatar
Michael Beck committed
190
191
	const be_return_attr_t *a_attr = get_irn_attr_const(a);
	const be_return_attr_t *b_attr = get_irn_attr_const(b);
192
193
194

	if (a_attr->num_ret_vals != b_attr->num_ret_vals)
		return 1;
195
196
	if (a_attr->pop != b_attr->pop)
		return 1;
197
198
	if (a_attr->emit_pop != b_attr->emit_pop)
		return 1;
199

200
	return node_cmp_attr(a, b);
201
202
}

Michael Beck's avatar
Michael Beck committed
203
204
205
206
207
/**
 * Compare the attributes of two be_IncSP nodes.
 *
 * @return zero if both nodes have identically attributes
 */
208
static int IncSP_cmp_attr(ir_node *a, ir_node *b) {
Michael Beck's avatar
Michael Beck committed
209
210
	const be_incsp_attr_t *a_attr = get_irn_attr_const(a);
	const be_incsp_attr_t *b_attr = get_irn_attr_const(b);
211
212
213
214

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

215
	return node_cmp_attr(a, b);
216
217
}

Michael Beck's avatar
Michael Beck committed
218
219
220
221
222
/**
 * Compare the attributes of two be_Call nodes.
 *
 * @return zero if both nodes have identically attributes
 */
223
224
static int Call_cmp_attr(ir_node *a, ir_node *b)
{
Michael Beck's avatar
Michael Beck committed
225
226
	const be_call_attr_t *a_attr = get_irn_attr_const(a);
	const be_call_attr_t *b_attr = get_irn_attr_const(b);
227
228

	if (a_attr->ent != b_attr->ent ||
229
		a_attr->call_tp != b_attr->call_tp)
230
231
		return 1;

232
	return node_cmp_attr(a, b);
Michael Beck's avatar
Michael Beck committed
233
234
}

235
static arch_register_req_t *allocate_reg_req(const ir_node *node)
Matthias Braun's avatar
Matthias Braun committed
236
{
237
238
	ir_graph       *irg  = get_irn_irg(node);
	struct obstack *obst = be_get_birg_obst(irg);
Matthias Braun's avatar
Matthias Braun committed
239

240
241
242
243
	arch_register_req_t *req = obstack_alloc(obst, sizeof(*req));
	memset(req, 0, sizeof(*req));
	return req;
}
Matthias Braun's avatar
Matthias Braun committed
244

245
246
247
248
249
250
251
void be_set_constr_in(ir_node *node, int pos, const arch_register_req_t *req)
{
	const be_node_attr_t *attr = get_irn_attr_const(node);
	be_reg_data_t *rd = &attr->reg_data[pos];
	assert(pos < ARR_LEN(attr->reg_data));
	rd->in_req = req;
}
Matthias Braun's avatar
Matthias Braun committed
252

253
254
255
256
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
257
258
}

259
/**
260
 * Initializes the generic attribute of all be nodes and return it.
261
 */
262
static void *init_node_attr(ir_node *node, int n_inputs, int n_outputs)
Sebastian Hack's avatar
Sebastian Hack committed
263
{
264
265
266
267
	ir_graph       *irg  = get_irn_irg(node);
	struct obstack *obst = be_get_birg_obst(irg);
	be_node_attr_t *a    = get_irn_attr(node);
	backend_info_t *info = be_get_info(node);
Sebastian Hack's avatar
Sebastian Hack committed
268

269
	memset(a, 0, sizeof(get_op_attr_size(get_irn_op(node))));
Sebastian Hack's avatar
Sebastian Hack committed
270

271
272
273
274
275
276
277
278
279
	if (n_inputs >= 0) {
		int i;
		a->reg_data = NEW_ARR_D(be_reg_data_t, obst, n_inputs);
		for (i = 0; i < n_inputs; ++i) {
			a->reg_data[i].in_req = arch_no_register_req;
		}
	} else {
		a->reg_data = NEW_ARR_F(be_reg_data_t, 0);
	}
280

281
282
283
284
285
286
287
	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;
		}
288
	} else {
289
		info->out_infos = NEW_ARR_F(reg_out_info_t, 0);
Sebastian Hack's avatar
Sebastian Hack committed
290
291
292
293
	}

	return a;
}
294

295
static void add_register_req_out(ir_node *node)
296
{
297
	backend_info_t *info = be_get_info(node);
298
299
300
301
302
303
304
305
	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)
{
306
307
	be_node_attr_t *a    = get_irn_attr(node);
	be_reg_data_t   regreq;
308
	memset(&regreq, 0, sizeof(regreq));
309
	regreq.in_req = arch_no_register_req;
310
311
312
	ARR_APP1(be_reg_data_t, a->reg_data, regreq);
}

313
314
315
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)
316
{
Matthias Braun's avatar
Matthias Braun committed
317
	be_frame_attr_t *a;
318
	ir_node         *in[2];
319
	ir_node         *res;
320
	ir_graph        *irg = get_Block_irg(bl);
321

322
323
324
	in[0]     = frame;
	in[1]     = to_spill;
	res       = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
325
	a         = init_node_attr(res, 2, 1);
326
	a->ent    = NULL;
Matthias Braun's avatar
Matthias Braun committed
327
	a->offset = 0;
Sebastian Hack's avatar
Sebastian Hack committed
328

329
330
	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);
331
332
333
334
335
336
337

	/*
	 * 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
	 */
338
	be_set_constr_in(res, be_pos_Spill_frame, arch_no_register_req);
339

Sebastian Hack's avatar
Sebastian Hack committed
340
341
	return res;
}
342

343
ir_node *be_new_Reload(const arch_register_class_t *cls,
344
		const arch_register_class_t *cls_frame, ir_node *block,
345
		ir_node *frame, ir_node *mem, ir_mode *mode)
346
{
347
348
349
	ir_node  *in[2];
	ir_node  *res;
	ir_graph *irg = get_Block_irg(block);
350
351
352

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

355
	init_node_attr(res, 2, 2);
356
357
358
	be_node_set_reg_class_out(res, 0, cls);
	be_node_set_reg_class_in(res, be_pos_Reload_frame, cls_frame);
	arch_irn_set_flags(res, arch_irn_flags_rematerializable);
359
360
361
362
363
364
365

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

Sebastian Hack's avatar
Sebastian Hack committed
368
369
	return res;
}
370

371
ir_node *be_get_Reload_mem(const ir_node *irn)
Sebastian Hack's avatar
Sebastian Hack committed
372
373
{
	assert(be_is_Reload(irn));
Daniel Grund's avatar
Daniel Grund committed
374
	return get_irn_n(irn, be_pos_Reload_mem);
Sebastian Hack's avatar
Sebastian Hack committed
375
376
}

377
378
379
380
381
382
ir_node *be_get_Reload_frame(const ir_node *irn)
{
	assert(be_is_Reload(irn));
	return get_irn_n(irn, be_pos_Reload_frame);
}

383
384
385
386
387
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
388

389
390
391
392
393
ir_node *be_get_Spill_frame(const ir_node *irn)
{
	assert(be_is_Spill(irn));
	return get_irn_n(irn, be_pos_Spill_frame);
}
394

395
396
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
397
{
398
399
400
	int      i;
	ir_graph *irg = get_Block_irg(block);

401
	ir_node *irn = new_ir_node(NULL, irg, block, op_be_Perm, mode_T, n, in);
402
	init_node_attr(irn, n, n);
403
	for (i = 0; i < n; ++i) {
404
405
		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
406
407
	}

Sebastian Hack's avatar
Sebastian Hack committed
408
409
	return irn;
}
410

411
412
void be_Perm_reduce(ir_node *perm, int new_size, int *map)
{
413
414
415
416
417
	int            arity      = get_irn_arity(perm);
	be_reg_data_t  *old_data  = ALLOCAN(be_reg_data_t, arity);
	reg_out_info_t *old_infos = ALLOCAN(reg_out_info_t, arity);
	be_node_attr_t *attr      = get_irn_attr(perm);
	backend_info_t *info      = be_get_info(perm);
418
	ir_node        **new_in;
419
420
421
422

	int i;

	assert(be_is_Perm(perm));
423
	assert(new_size <= arity);
424

425
	new_in = alloca(new_size * sizeof(*new_in));
426

427
	/* save the old register data */
428
	memcpy(old_data, attr->reg_data, arity * sizeof(old_data[0]));
429
	memcpy(old_infos, info->out_infos, arity * sizeof(old_infos[0]));
430
431

	/* compose the new in array and set the new register data directly in place */
432
	for (i = 0; i < new_size; ++i) {
433
		int idx = map[i];
434
435
436
		new_in[i]          = get_irn_n(perm, idx);
		attr->reg_data[i]  = old_data[idx];
		info->out_infos[i] = old_infos[idx];
437
438
439
440
441
	}

	set_irn_in(perm, new_size, new_in);
}

442
ir_node *be_new_MemPerm(const arch_env_t *arch_env, ir_node *bl, int n, ir_node *in[])
Matthias Braun's avatar
Matthias Braun committed
443
{
444
	ir_graph                     *irg       = get_Block_irg(bl);
445
446
447
448
449
450
451
	ir_node                      *frame     = get_irg_frame(irg);
	const arch_register_class_t  *cls_frame = arch_get_irn_reg_class_out(frame);
	const arch_register_t        *sp        = arch_env->sp;
	ir_node                      *irn;
	be_memperm_attr_t            *attr;
	ir_node                     **real_in;
	int                           i;
Matthias Braun's avatar
Matthias Braun committed
452

453
	real_in = ALLOCAN(ir_node*, n + 1);
454
455
456
457
458
	real_in[0] = frame;
	memcpy(&real_in[1], in, n * sizeof(real_in[0]));

	irn =  new_ir_node(NULL, irg, bl, op_be_MemPerm, mode_T, n+1, real_in);

459
	init_node_attr(irn, n + 1, n + 1);
460
	be_node_set_reg_class_in(irn, 0, sp->reg_class);
Michael Beck's avatar
Michael Beck committed
461
	for (i = 0; i < n; ++i) {
462
463
		be_node_set_reg_class_in(irn, i + 1, cls_frame);
		be_node_set_reg_class_out(irn, i, cls_frame);
Matthias Braun's avatar
Matthias Braun committed
464
465
466
	}

	attr = get_irn_attr(irn);
467
468
	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
469
470
471
472

	return irn;
}

473
ir_node *be_new_Copy(const arch_register_class_t *cls, ir_node *bl, ir_node *op)
Sebastian Hack's avatar
Sebastian Hack committed
474
475
476
{
	ir_node *in[1];
	ir_node *res;
Matthias Braun's avatar
Matthias Braun committed
477
	arch_register_req_t *req;
478
	ir_graph *irg = get_Block_irg(bl);
479

Sebastian Hack's avatar
Sebastian Hack committed
480
	in[0] = op;
481
	res   = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
482
	init_node_attr(res, 1, 1);
483
484
	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
485

486
487
488
	req = allocate_reg_req(res);
	req->cls        = cls;
	req->type       = arch_register_req_type_should_be_same;
489
	req->other_same = 1U << 0;
490
	be_set_constr_out(res, 0, req);
Matthias Braun's avatar
Matthias Braun committed
491

Sebastian Hack's avatar
Sebastian Hack committed
492
	return res;
493
494
}

495
496
497
498
ir_node *be_get_Copy_op(const ir_node *cpy) {
	return get_irn_n(cpy, be_pos_Copy_op);
}

499
500
501
502
void be_set_Copy_op(ir_node *cpy, ir_node *op) {
	set_irn_n(cpy, be_pos_Copy_op, op);
}

503
ir_node *be_new_Keep(ir_node *block, int n, ir_node *in[])
Sebastian Hack's avatar
Sebastian Hack committed
504
{
Sebastian Hack's avatar
Sebastian Hack committed
505
	int i;
506
	ir_node *res;
507
	ir_graph *irg = get_Block_irg(block);
508

509
510
	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
511

512
	for (i = 0; i < n; ++i) {
513
		add_irn_n(res, in[i]);
514
		add_register_req_in(res);
Sebastian Hack's avatar
Sebastian Hack committed
515
	}
516
517
518
519
520
521
522
523
524
525
526
	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);
527
	add_register_req_in(keep);
528
	be_node_set_reg_class_in(keep, n, cls);
Sebastian Hack's avatar
Sebastian Hack committed
529
530
}

Michael Beck's avatar
Michael Beck committed
531
/* creates a be_Call */
532
533
534
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
535
{
536
537
	be_call_attr_t *a;
	int real_n = be_pos_Call_first_arg + n;
Sebastian Hack's avatar
Sebastian Hack committed
538
539
	ir_node *irn;
	ir_node **real_in;
Sebastian Hack's avatar
Sebastian Hack committed
540

541
542
543
544
545
	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
546

547
	irn = new_ir_node(dbg, irg, bl, op_be_Call, mode_T, real_n, real_in);
548
	a = init_node_attr(irn, real_n, n_outs);
549
550
	a->ent     = NULL;
	a->call_tp = call_tp;
Matthias Braun's avatar
Matthias Braun committed
551
	a->pop     = 0;
Sebastian Hack's avatar
Sebastian Hack committed
552
	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
553
554
}

555
/* Gets the call entity or NULL if this is no static call. */
556
ir_entity *be_Call_get_entity(const ir_node *call) {
Matthias Braun's avatar
Matthias Braun committed
557
	const be_call_attr_t *a = get_irn_attr_const(call);
558
559
560
561
	assert(be_is_Call(call));
	return a->ent;
}

562
/* Sets the call entity. */
563
void be_Call_set_entity(ir_node *call, ir_entity *ent) {
564
565
566
567
568
	be_call_attr_t *a = get_irn_attr(call);
	assert(be_is_Call(call));
	a->ent = ent;
}

569
570
/* Gets the call type. */
ir_type *be_Call_get_type(ir_node *call) {
Matthias Braun's avatar
Matthias Braun committed
571
	const be_call_attr_t *a = get_irn_attr_const(call);
572
573
574
575
576
577
578
579
580
581
582
	assert(be_is_Call(call));
	return a->call_tp;
}

/* Sets the call type. */
void be_Call_set_type(ir_node *call, ir_type *call_tp) {
	be_call_attr_t *a = get_irn_attr(call);
	assert(be_is_Call(call));
	a->call_tp = call_tp;
}

Matthias Braun's avatar
Matthias Braun committed
583
584
585
586
587
588
589
590
591
592
void be_Call_set_pop(ir_node *call, unsigned pop) {
	be_call_attr_t *a = get_irn_attr(call);
	a->pop = pop;
}

unsigned be_Call_get_pop(const ir_node *call) {
	const be_call_attr_t *a = get_irn_attr_const(call);
	return a->pop;
}

593
/* Construct a new be_Return. */
594
ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *block, int n_res,
595
                       unsigned pop, int n, ir_node *in[])
Sebastian Hack's avatar
Sebastian Hack committed
596
{
597
	be_return_attr_t *a;
598
599
600
601
	ir_node *res;
	int i;

	res = new_ir_node(dbg, irg, block, op_be_Return, mode_X, -1, NULL);
602
603
	init_node_attr(res, -1, 1);
	for (i = 0; i < n; ++i) {
604
		add_irn_n(res, in[i]);
605
		add_register_req_in(res);
606
	}
607
	be_set_constr_out(res, 0, arch_no_register_req);
608
609

	a = get_irn_attr(res);
610
	a->num_ret_vals = n_res;
611
	a->pop          = pop;
612
	a->emit_pop     = 0;
Sebastian Hack's avatar
Sebastian Hack committed
613

614
	return res;
Sebastian Hack's avatar
Sebastian Hack committed
615
616
}

617
/* Returns the number of real returns values */
618
int be_Return_get_n_rets(const ir_node *ret) {
619
	const be_return_attr_t *a = get_irn_generic_attr_const(ret);
620
621
622
	return a->num_ret_vals;
}

623
624
/* return the number of bytes that should be popped from stack when executing the Return. */
unsigned be_Return_get_pop(const ir_node *ret) {
625
626
627
628
	const be_return_attr_t *a = get_irn_generic_attr_const(ret);
	return a->pop;
}

629
630
631
632
633
634
635
636
637
638
639
640
641
/* return non-zero, if number of popped bytes must be always emitted */
int be_Return_get_emit_pop(const ir_node *ret) {
	const be_return_attr_t *a = get_irn_generic_attr_const(ret);
	return a->emit_pop;
}

/* return non-zero, if number of popped bytes must be always emitted */
void be_Return_set_emit_pop(ir_node *ret, int emit_pop) {
	be_return_attr_t *a = get_irn_generic_attr(ret);
	a->emit_pop = emit_pop;
}

int be_Return_append_node(ir_node *ret, ir_node *node) {
642
643
644
	int pos;

	pos = add_irn_n(ret, node);
645
	add_register_req_in(ret);
646
647
648
649

	return pos;
}

650
ir_node *be_new_IncSP(const arch_register_t *sp, ir_node *bl,
651
                      ir_node *old_sp, int offset, int align)
Sebastian Hack's avatar
Sebastian Hack committed
652
{
Michael Beck's avatar
Michael Beck committed
653
	be_incsp_attr_t *a;
Sebastian Hack's avatar
Sebastian Hack committed
654
	ir_node *irn;
Sebastian Hack's avatar
Sebastian Hack committed
655
	ir_node *in[1];
656
	ir_graph *irg = get_Block_irg(bl);
Sebastian Hack's avatar
Sebastian Hack committed
657
658

	in[0]     = old_sp;
659
660
	irn       = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode,
	                        sizeof(in) / sizeof(in[0]), in);
661
	a         = init_node_attr(irn, 1, 1);
Sebastian Hack's avatar
Sebastian Hack committed
662
	a->offset = offset;
663
	a->align  = align;
Sebastian Hack's avatar
Sebastian Hack committed
664
665

	/* Set output constraint to stack register. */
666
667
	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
668
669

	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
670
}
Sebastian Hack's avatar
Sebastian Hack committed
671

672
673
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
674
{
Sebastian Hack's avatar
Sebastian Hack committed
675
	be_node_attr_t *a;
Sebastian Hack's avatar
Sebastian Hack committed
676
	ir_node *irn;
Sebastian Hack's avatar
Sebastian Hack committed
677
	ir_node *in[be_pos_AddSP_last];
678
	const arch_register_class_t *cls;
679
	ir_graph *irg;
Sebastian Hack's avatar
Sebastian Hack committed
680

Sebastian Hack's avatar
Sebastian Hack committed
681
682
	in[be_pos_AddSP_old_sp] = old_sp;
	in[be_pos_AddSP_size]   = sz;
Sebastian Hack's avatar
Sebastian Hack committed
683

684
	irg = get_Block_irg(bl);
685
	irn = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T, be_pos_AddSP_last, in);
686
	a   = init_node_attr(irn, be_pos_AddSP_last, pn_be_AddSP_last);
Sebastian Hack's avatar
Sebastian Hack committed
687

Sebastian Hack's avatar
Sebastian Hack committed
688
	/* Set output constraint to stack register. */
689
690
691
	be_set_constr_single_reg_in(irn, be_pos_AddSP_old_sp, sp, 0);
	be_node_set_reg_class_in(irn, be_pos_AddSP_size, arch_register_get_class(sp));
	be_set_constr_single_reg_out(irn, pn_be_AddSP_sp, sp, arch_register_req_type_produces_sp);
692

693
	cls = arch_register_get_class(sp);
Sebastian Hack's avatar
Sebastian Hack committed
694
695
696
697

	return irn;
}

698
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
699
700
701
702
{
	be_node_attr_t *a;
	ir_node *irn;
	ir_node *in[be_pos_SubSP_last];
703
	ir_graph *irg;
Michael Beck's avatar
Michael Beck committed
704
705
706
707

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

708
	irg = get_Block_irg(bl);
Michael Beck's avatar
Michael Beck committed
709
	irn = new_ir_node(NULL, irg, bl, op_be_SubSP, mode_T, be_pos_SubSP_last, in);
710
	a   = init_node_attr(irn, be_pos_SubSP_last, pn_be_SubSP_last);
Michael Beck's avatar
Michael Beck committed
711
712

	/* Set output constraint to stack register. */
713
714
715
	be_set_constr_single_reg_in(irn, be_pos_SubSP_old_sp, sp, 0);
	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
716
717
718
719

	return irn;
}

720
ir_node *be_new_RegParams(ir_node *bl, int n_outs)
Sebastian Hack's avatar
Sebastian Hack committed
721
{
722
723
	ir_node *res;
	int i;
724
	ir_graph *irg = get_Block_irg(bl);
Sebastian Hack's avatar
Sebastian Hack committed
725

726
	res = new_ir_node(NULL, irg, bl, op_be_RegParams, mode_T, 0, NULL);
727
728
729
730
	init_node_attr(res, 0, -1);
	for (i = 0; i < n_outs; ++i) {
		add_register_req_out(res);
	}
731
732
733
734

	return res;
}

735
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
736
{
737
	be_frame_attr_t *a;
Sebastian Hack's avatar
Sebastian Hack committed
738
739
	ir_node *irn;
	ir_node *in[1];
740
	ir_graph *irg = get_Block_irg(bl);
Sebastian Hack's avatar
Sebastian Hack committed
741

742
	in[0]  = frame;
743
	irn    = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
744
	a      = init_node_attr(irn, 1, 1);
745
746
	a->ent = ent;
	a->offset = 0;
747
748
	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
749
750

	return optimize_node(irn);
Sebastian Hack's avatar
Sebastian Hack committed
751
752
}

Matthias Braun's avatar
Matthias Braun committed
753
ir_node *be_get_FrameAddr_frame(const ir_node *node) {
Michael Beck's avatar
Michael Beck committed
754
755
756
757
	assert(be_is_FrameAddr(node));
	return get_irn_n(node, be_pos_FrameAddr_ptr);
}

Matthias Braun's avatar
Matthias Braun committed
758
759
760
761
762
763
ir_entity *be_get_FrameAddr_entity(const ir_node *node)
{
	const be_frame_attr_t *attr = get_irn_generic_attr_const(node);
	return attr->ent;
}

764
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
765
{
766
767
	ir_node  *irn;
	ir_node **in = ALLOCAN(ir_node*, n + 1);
768
	ir_graph *irg = get_Block_irg(bl);
Sebastian Hack's avatar
Sebastian Hack committed
769
770
771
772

	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);
773
	init_node_attr(irn, n + 1, 1);
774
775
	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
776
777
778
779

	return irn;
}

780
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
781
{
782
	return be_new_CopyKeep(cls, bl, src, 1, &keep, mode);
Sebastian Hack's avatar
Sebastian Hack committed
783
784
}

785
786
787
788
789
790
791
792
ir_node *be_get_CopyKeep_op(const ir_node *cpy) {
	return get_irn_n(cpy, be_pos_CopyKeep_op);
}

void be_set_CopyKeep_op(ir_node *cpy, ir_node *op) {
	set_irn_n(cpy, be_pos_CopyKeep_op, op);
}

793
ir_node *be_new_Barrier(ir_node *bl, int n, ir_node *in[])
Sebastian Hack's avatar
Sebastian Hack committed
794
{
795
796
	ir_node *res;
	int i;
797
	ir_graph *irg = get_Block_irg(bl);
Sebastian Hack's avatar
Sebastian Hack committed
798

799
	res = new_ir_node(NULL, irg, bl, op_be_Barrier, mode_T, -1, NULL);
800
801
	init_node_attr(res, -1, -1);
	for (i = 0; i < n; ++i) {
802
		add_irn_n(res, in[i]);
803
804
		add_register_req_in(res);
		add_register_req_out(res);
805
806
807
808
809
810
811
812
813
814
	}

	return res;
}

ir_node *be_Barrier_append_node(ir_node *barrier, ir_node *node)
{
	ir_node *block = get_nodes_block(barrier);
	ir_mode *mode = get_irn_mode(node);
	int n = add_irn_n(barrier, node);
815

816
	ir_node *proj = new_r_Proj(block, barrier, mode, n);
817
818
	add_register_req_in(barrier);
	add_register_req_out(barrier);
819
820

	return proj;
Sebastian Hack's avatar
Sebastian Hack committed
821
822
}

823
824
int be_has_frame_entity(const ir_node *irn)
{
825
	switch (get_irn_opcode(irn)) {
826
	case beo_Spill:
827
	case beo_Reload:
828
829
	case beo_FrameAddr:
		return 1;
830
831
	default:
		return 0;
832
833
834
	}
}

835
ir_entity *be_get_frame_entity(const ir_node *irn)
836
{
837
	if (be_has_frame_entity(irn)) {
Matthias Braun's avatar
Matthias Braun committed
838
		const be_frame_attr_t *a = get_irn_attr_const(irn);
839
840
841
842
		return a->ent;
	}
	return NULL;
}
Sebastian Hack's avatar
Sebastian Hack committed
843

844
845
846
847
int be_get_frame_offset(const ir_node *irn)
{
	assert(is_be_node(irn));
	if (be_has_frame_entity(irn)) {
Matthias Braun's avatar
Matthias Braun committed
848
		const be_frame_attr_t *a = get_irn_attr_const(irn);
849
850
851
852
853
		return a->offset;
	}
	return 0;
}

854
void be_set_MemPerm_in_entity(const ir_node *irn, int n, ir_entity *ent)
Matthias Braun's avatar
Matthias Braun committed
855
{
Matthias Braun's avatar
Matthias Braun committed
856
	const be_memperm_attr_t *attr = get_irn_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
857
858

	assert(be_is_MemPerm(irn));
859
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
860
861
862
863

	attr->in_entities[n] = ent;
}

864
ir_entity* be_get_MemPerm_in_entity(const ir_node* irn, int n)
Matthias Braun's avatar
Matthias Braun committed
865
{
Matthias Braun's avatar
Matthias Braun committed
866
	const be_memperm_attr_t *attr = get_irn_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
867
868

	assert(be_is_MemPerm(irn));
869
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
870
871
872
873

	return attr->in_entities[n];
}