benode.c 44.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"
Sebastian Hack's avatar
Sebastian Hack committed
57
#include "benode_t.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
66
typedef struct {
	arch_register_req_t req;
67
	arch_register_req_t in_req;
68
69
} be_reg_data_t;

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

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

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

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

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

106
typedef struct {
107
108
109
	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
110
} be_memperm_attr_t;
111

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

Sebastian Hack's avatar
Sebastian Hack committed
129
static const ir_op_ops be_node_op_ops;
Sebastian Hack's avatar
Sebastian Hack committed
130

131
132
133
134
135
136
137
138
139
140
#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
141
#define M   irop_flag_uses_memory
142

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

151
	if (len != ARR_LEN(b->reg_data))
152
		return 1;
Michael Beck's avatar
Michael Beck committed
153

154
155
156
	for (i = len - 1; i >= 0; --i) {
		if (!reg_reqs_equal(&a->reg_data[i].in_req, &b->reg_data[i].in_req) ||
		    !reg_reqs_equal(&a->reg_data[i].req,    &b->reg_data[i].req))
157
			return 1;
Michael Beck's avatar
Michael Beck committed
158
	}
159
160

	return 0;
Michael Beck's avatar
Michael Beck committed
161
162
}

Michael Beck's avatar
Michael Beck committed
163
164
165
166
167
/**
 * Compare the node attributes of two be_node's.
 *
 * @return zero if both nodes have identically attributes
 */
168
static int node_cmp_attr(ir_node *a, ir_node *b) {
Michael Beck's avatar
Michael Beck committed
169
170
	const be_node_attr_t *a_attr = get_irn_attr_const(a);
	const be_node_attr_t *b_attr = get_irn_attr_const(b);
171

172
	if (_node_cmp_attr(a_attr, b_attr) != 0)
173
174
175
		return 1;

	return !be_info_equal(a, b);
176
177
}

Michael Beck's avatar
Michael Beck committed
178
/**
Michael Beck's avatar
Michael Beck committed
179
 * Compare the attributes of two be_FrameAddr nodes.
Michael Beck's avatar
Michael Beck committed
180
 *
Michael Beck's avatar
Michael Beck committed
181
 * @return zero if both nodes have identically attributes
Michael Beck's avatar
Michael Beck committed
182
 */
183
static int FrameAddr_cmp_attr(ir_node *a, ir_node *b) {
Michael Beck's avatar
Michael Beck committed
184
185
	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
186

187
188
189
	if (a_attr->ent != b_attr->ent || a_attr->offset != b_attr->offset)
		return 1;

Michael Beck's avatar
Michael Beck committed
190
	return _node_cmp_attr(&a_attr->node_attr, &b_attr->node_attr);
191
192
}

Michael Beck's avatar
Michael Beck committed
193
194
195
196
197
/**
 * Compare the attributes of two be_Return nodes.
 *
 * @return zero if both nodes have identically attributes
 */
198
static int Return_cmp_attr(ir_node *a, ir_node *b) {
Michael Beck's avatar
Michael Beck committed
199
200
	const be_return_attr_t *a_attr = get_irn_attr_const(a);
	const be_return_attr_t *b_attr = get_irn_attr_const(b);
201
202
203

	if (a_attr->num_ret_vals != b_attr->num_ret_vals)
		return 1;
204
205
	if (a_attr->pop != b_attr->pop)
		return 1;
206
207
	if (a_attr->emit_pop != b_attr->emit_pop)
		return 1;
208

Michael Beck's avatar
Michael Beck committed
209
	return _node_cmp_attr(&a_attr->node_attr, &b_attr->node_attr);
210
211
}

Michael Beck's avatar
Michael Beck committed
212
213
214
215
216
/**
 * Compare the attributes of two be_IncSP nodes.
 *
 * @return zero if both nodes have identically attributes
 */
217
static int IncSP_cmp_attr(ir_node *a, ir_node *b) {
Michael Beck's avatar
Michael Beck committed
218
219
	const be_incsp_attr_t *a_attr = get_irn_attr_const(a);
	const be_incsp_attr_t *b_attr = get_irn_attr_const(b);
220
221
222
223

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

Michael Beck's avatar
Michael Beck committed
224
	return _node_cmp_attr(&a_attr->node_attr, &b_attr->node_attr);
225
226
}

Michael Beck's avatar
Michael Beck committed
227
228
229
230
231
/**
 * Compare the attributes of two be_Call nodes.
 *
 * @return zero if both nodes have identically attributes
 */
232
static int Call_cmp_attr(ir_node *a, ir_node *b) {
Michael Beck's avatar
Michael Beck committed
233
234
	const be_call_attr_t *a_attr = get_irn_attr_const(a);
	const be_call_attr_t *b_attr = get_irn_attr_const(b);
235
236

	if (a_attr->ent != b_attr->ent ||
237
		a_attr->call_tp != b_attr->call_tp)
238
239
		return 1;

Michael Beck's avatar
Michael Beck committed
240
	return _node_cmp_attr(&a_attr->node_attr, &b_attr->node_attr);
Michael Beck's avatar
Michael Beck committed
241
242
}

243
static inline arch_register_req_t *get_be_req(const ir_node *node, int pos)
Matthias Braun's avatar
Matthias Braun committed
244
245
{
	int idx;
Matthias Braun's avatar
Matthias Braun committed
246
	const be_node_attr_t *attr;
Matthias Braun's avatar
Matthias Braun committed
247
248
249
	be_reg_data_t *rd;

	assert(is_be_node(node));
Matthias Braun's avatar
Matthias Braun committed
250
	attr = get_irn_attr_const(node);
Matthias Braun's avatar
Matthias Braun committed
251

252
	if (pos < 0) {
Matthias Braun's avatar
Matthias Braun committed
253
254
255
256
257
258
259
260
261
262
263
		idx = -(pos + 1);
	} else {
		idx = pos;
		assert(idx < get_irn_arity(node));
	}
	assert(idx < ARR_LEN(attr->reg_data));
	rd = &attr->reg_data[idx];

	return pos < 0 ? &rd->req : &rd->in_req;
}

264
/**
265
 * Initializes the generic attribute of all be nodes and return it.
266
 */
267
static void *init_node_attr(ir_node *node, int max_reg_data)
Sebastian Hack's avatar
Sebastian Hack committed
268
{
269
	ir_graph *irg = get_irn_irg(node);
270
	struct obstack *obst = get_irg_obstack(irg);
271
	be_node_attr_t *a = get_irn_attr(node);
Sebastian Hack's avatar
Sebastian Hack committed
272

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

275
	if(max_reg_data >= 0) {
276
277
278
279
		backend_info_t *info = be_get_info(node);
		info->out_infos = NEW_ARR_D(reg_out_info_t, obst, max_reg_data);
		memset(info->out_infos, 0, max_reg_data * sizeof(info->out_infos[0]));

280
		a->reg_data = NEW_ARR_D(be_reg_data_t, obst, max_reg_data);
Sebastian Hack's avatar
Sebastian Hack committed
281
		memset(a->reg_data, 0, max_reg_data * sizeof(a->reg_data[0]));
282
	} else {
283
284
285
		backend_info_t *info = be_get_info(node);
		info->out_infos = NEW_ARR_F(reg_out_info_t, 0);

286
		a->reg_data = NEW_ARR_F(be_reg_data_t, 0);
Sebastian Hack's avatar
Sebastian Hack committed
287
288
289
290
	}

	return a;
}
291

292
293
static void add_register_req(ir_node *node)
{
294
295
296
297
	backend_info_t *info = be_get_info(node);
	be_node_attr_t *a    = get_irn_attr(node);
	be_reg_data_t   regreq;
	reg_out_info_t  out_info;
298
	memset(&regreq, 0, sizeof(regreq));
299
	memset(&out_info, 0, sizeof(out_info));
300
	ARR_APP1(be_reg_data_t, a->reg_data, regreq);
301
	ARR_APP1(reg_out_info_t, info->out_infos, out_info);
302
303
}

304
ir_node *be_new_Spill(const arch_register_class_t *cls, const arch_register_class_t *cls_frame,
305
	ir_node *bl, ir_node *frame, ir_node *to_spill)
306
{
Matthias Braun's avatar
Matthias Braun committed
307
	be_frame_attr_t *a;
308
	ir_node         *in[2];
309
	ir_node         *res;
310
	ir_graph        *irg = get_Block_irg(bl);
311

312
313
314
	in[0]     = frame;
	in[1]     = to_spill;
	res       = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
315
316
	a         = init_node_attr(res, 2);
	a->ent    = NULL;
Matthias Braun's avatar
Matthias Braun committed
317
	a->offset = 0;
Sebastian Hack's avatar
Sebastian Hack committed
318

319
320
	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);
321
322
323
324
325
326
327
328
329

	/*
	 * 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
	 */
	be_node_set_reg_class_in(res, be_pos_Spill_frame, NULL);

Sebastian Hack's avatar
Sebastian Hack committed
330
331
	return res;
}
332

333
ir_node *be_new_Reload(const arch_register_class_t *cls,
334
		const arch_register_class_t *cls_frame, ir_node *block,
335
		ir_node *frame, ir_node *mem, ir_mode *mode)
336
{
337
338
339
	ir_node  *in[2];
	ir_node  *res;
	ir_graph *irg = get_Block_irg(block);
340
341
342

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

Sebastian Hack's avatar
Sebastian Hack committed
345
	init_node_attr(res, 2);
346
347
348
	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);
349
350
351
352
353
354
355
356
357

	/*
	 * 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
	 */
	be_node_set_reg_class_in(res, be_pos_Reload_frame, NULL);

Sebastian Hack's avatar
Sebastian Hack committed
358
359
	return res;
}
360

361
ir_node *be_get_Reload_mem(const ir_node *irn)
Sebastian Hack's avatar
Sebastian Hack committed
362
363
{
	assert(be_is_Reload(irn));
Daniel Grund's avatar
Daniel Grund committed
364
	return get_irn_n(irn, be_pos_Reload_mem);
Sebastian Hack's avatar
Sebastian Hack committed
365
366
}

367
368
369
370
371
372
ir_node *be_get_Reload_frame(const ir_node *irn)
{
	assert(be_is_Reload(irn));
	return get_irn_n(irn, be_pos_Reload_frame);
}

373
374
375
376
377
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
378

379
380
381
382
383
ir_node *be_get_Spill_frame(const ir_node *irn)
{
	assert(be_is_Spill(irn));
	return get_irn_n(irn, be_pos_Spill_frame);
}
384

385
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
386
{
387
388
389
	int      i;
	ir_graph *irg = get_Block_irg(block);

390
	ir_node *irn = new_ir_node(NULL, irg, block, op_be_Perm, mode_T, n, in);
Sebastian Hack's avatar
Sebastian Hack committed
391
	init_node_attr(irn, n);
392
	for (i = 0; i < n; ++i) {
393
394
		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
395
396
	}

Sebastian Hack's avatar
Sebastian Hack committed
397
398
	return irn;
}
399

400
401
void be_Perm_reduce(ir_node *perm, int new_size, int *map)
{
402
403
404
405
406
	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);
407
	ir_node        **new_in;
408
409
410
411

	int i;

	assert(be_is_Perm(perm));
412
	assert(new_size <= arity);
413

414
	new_in = alloca(new_size * sizeof(*new_in));
415

416
	/* save the old register data */
417
	memcpy(old_data, attr->reg_data, arity * sizeof(old_data[0]));
418
	memcpy(old_infos, info->out_infos, arity * sizeof(old_infos[0]));
419
420

	/* compose the new in array and set the new register data directly in place */
421
	for (i = 0; i < new_size; ++i) {
422
		int idx = map[i];
423
424
425
		new_in[i]          = get_irn_n(perm, idx);
		attr->reg_data[i]  = old_data[idx];
		info->out_infos[i] = old_infos[idx];
426
427
428
429
430
	}

	set_irn_in(perm, new_size, new_in);
}

431
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
432
{
433
	ir_graph                     *irg       = get_Block_irg(bl);
434
435
436
437
438
439
440
	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
441

442
	real_in = ALLOCAN(ir_node*, n + 1);
443
444
445
446
447
448
	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);

	init_node_attr(irn, n + 1);
449
	be_node_set_reg_class_in(irn, 0, sp->reg_class);
Michael Beck's avatar
Michael Beck committed
450
	for (i = 0; i < n; ++i) {
451
452
		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
453
454
455
	}

	attr = get_irn_attr(irn);
456
457
	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
458
459
460
461
462

	return irn;
}


463
ir_node *be_new_Copy(const arch_register_class_t *cls, ir_node *bl, ir_node *op)
Sebastian Hack's avatar
Sebastian Hack committed
464
465
466
{
	ir_node *in[1];
	ir_node *res;
Matthias Braun's avatar
Matthias Braun committed
467
	arch_register_req_t *req;
468
	ir_graph *irg = get_Block_irg(bl);
469

Sebastian Hack's avatar
Sebastian Hack committed
470
	in[0] = op;
471
	res   = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
Sebastian Hack's avatar
Sebastian Hack committed
472
	init_node_attr(res, 1);
473
474
	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
475

476
	req = get_be_req(res, BE_OUT_POS(0));
Matthias Braun's avatar
Matthias Braun committed
477
478
	req->cls = cls;
	req->type = arch_register_req_type_should_be_same;
479
	req->other_same = 1U << 0;
Matthias Braun's avatar
Matthias Braun committed
480

Sebastian Hack's avatar
Sebastian Hack committed
481
	return res;
482
483
}

484
485
486
487
ir_node *be_get_Copy_op(const ir_node *cpy) {
	return get_irn_n(cpy, be_pos_Copy_op);
}

488
489
490
491
void be_set_Copy_op(ir_node *cpy, ir_node *op) {
	set_irn_n(cpy, be_pos_Copy_op, op);
}

492
ir_node *be_new_Keep(const arch_register_class_t *cls, ir_node *bl, int n, ir_node *in[])
Sebastian Hack's avatar
Sebastian Hack committed
493
{
Sebastian Hack's avatar
Sebastian Hack committed
494
	int i;
495
	ir_node *res;
496
	ir_graph *irg = get_Block_irg(bl);
497
498
499

	res = new_ir_node(NULL, irg, bl, op_be_Keep, mode_ANY, -1, NULL);
	init_node_attr(res, -1);
Sebastian Hack's avatar
Sebastian Hack committed
500

Sebastian Hack's avatar
Sebastian Hack committed
501
	for(i = 0; i < n; ++i) {
502
503
		add_irn_n(res, in[i]);
		add_register_req(res);
504
		be_node_set_reg_class_in(res, i, cls);
Sebastian Hack's avatar
Sebastian Hack committed
505
	}
506
507
508
509
510
511
512
513
514
515
516
517
	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);
	add_register_req(keep);
518
	be_node_set_reg_class_in(keep, n, cls);
Sebastian Hack's avatar
Sebastian Hack committed
519
520
}

Michael Beck's avatar
Michael Beck committed
521
/* creates a be_Call */
522
523
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
524
{
525
526
	be_call_attr_t *a;
	int real_n = be_pos_Call_first_arg + n;
Sebastian Hack's avatar
Sebastian Hack committed
527
528
	ir_node *irn;
	ir_node **real_in;
Sebastian Hack's avatar
Sebastian Hack committed
529

530
531
532
533
534
	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
535

536
	irn = new_ir_node(dbg, irg, bl, op_be_Call, mode_T, real_n, real_in);
537
538
539
	a = init_node_attr(irn, (n_outs > real_n ? n_outs : real_n));
	a->ent     = NULL;
	a->call_tp = call_tp;
Matthias Braun's avatar
Matthias Braun committed
540
	a->pop     = 0;
Sebastian Hack's avatar
Sebastian Hack committed
541
	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
542
543
}

544
/* Gets the call entity or NULL if this is no static call. */
545
ir_entity *be_Call_get_entity(const ir_node *call) {
Matthias Braun's avatar
Matthias Braun committed
546
	const be_call_attr_t *a = get_irn_attr_const(call);
547
548
549
550
	assert(be_is_Call(call));
	return a->ent;
}

551
/* Sets the call entity. */
552
void be_Call_set_entity(ir_node *call, ir_entity *ent) {
553
554
555
556
557
	be_call_attr_t *a = get_irn_attr(call);
	assert(be_is_Call(call));
	a->ent = ent;
}

558
559
/* Gets the call type. */
ir_type *be_Call_get_type(ir_node *call) {
Matthias Braun's avatar
Matthias Braun committed
560
	const be_call_attr_t *a = get_irn_attr_const(call);
561
562
563
564
565
566
567
568
569
570
571
	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
572
573
574
575
576
577
578
579
580
581
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;
}

582
/* Construct a new be_Return. */
583
ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *block, int n_res,
584
                       unsigned pop, int n, ir_node *in[])
Sebastian Hack's avatar
Sebastian Hack committed
585
{
586
	be_return_attr_t *a;
587
588
589
590
591
592
593
594
595
596
597
	ir_node *res;
	int i;

	res = new_ir_node(dbg, irg, block, op_be_Return, mode_X, -1, NULL);
	init_node_attr(res, -1);
	for(i = 0; i < n; ++i) {
		add_irn_n(res, in[i]);
		add_register_req(res);
	}

	a = get_irn_attr(res);
598
	a->num_ret_vals = n_res;
599
	a->pop          = pop;
600
	a->emit_pop     = 0;
Sebastian Hack's avatar
Sebastian Hack committed
601

602
	return res;
Sebastian Hack's avatar
Sebastian Hack committed
603
604
}

605
/* Returns the number of real returns values */
606
int be_Return_get_n_rets(const ir_node *ret) {
607
	const be_return_attr_t *a = get_irn_generic_attr_const(ret);
608
609
610
	return a->num_ret_vals;
}

611
612
/* return the number of bytes that should be popped from stack when executing the Return. */
unsigned be_Return_get_pop(const ir_node *ret) {
613
614
615
616
	const be_return_attr_t *a = get_irn_generic_attr_const(ret);
	return a->pop;
}

617
618
619
620
621
622
623
624
625
626
627
628
629
/* 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) {
630
631
632
633
634
635
636
637
	int pos;

	pos = add_irn_n(ret, node);
	add_register_req(ret);

	return pos;
}

638
ir_node *be_new_IncSP(const arch_register_t *sp, ir_node *bl,
639
                      ir_node *old_sp, int offset, int align)
Sebastian Hack's avatar
Sebastian Hack committed
640
{
Michael Beck's avatar
Michael Beck committed
641
	be_incsp_attr_t *a;
Sebastian Hack's avatar
Sebastian Hack committed
642
	ir_node *irn;
Sebastian Hack's avatar
Sebastian Hack committed
643
	ir_node *in[1];
644
	ir_graph *irg = get_Block_irg(bl);
Sebastian Hack's avatar
Sebastian Hack committed
645
646

	in[0]     = old_sp;
647
648
	irn       = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode,
	                        sizeof(in) / sizeof(in[0]), in);
Sebastian Hack's avatar
Sebastian Hack committed
649
	a         = init_node_attr(irn, 1);
Sebastian Hack's avatar
Sebastian Hack committed
650
	a->offset = offset;
651
	a->align  = align;
Sebastian Hack's avatar
Sebastian Hack committed
652
653

	/* Set output constraint to stack register. */
654
655
	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
656
657

	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
658
}
Sebastian Hack's avatar
Sebastian Hack committed
659

660
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
661
{
Sebastian Hack's avatar
Sebastian Hack committed
662
	be_node_attr_t *a;
Sebastian Hack's avatar
Sebastian Hack committed
663
	ir_node *irn;
Sebastian Hack's avatar
Sebastian Hack committed
664
	ir_node *in[be_pos_AddSP_last];
665
	const arch_register_class_t *cls;
666
	ir_graph *irg;
Sebastian Hack's avatar
Sebastian Hack committed
667

Sebastian Hack's avatar
Sebastian Hack committed
668
669
	in[be_pos_AddSP_old_sp] = old_sp;
	in[be_pos_AddSP_size]   = sz;
Sebastian Hack's avatar
Sebastian Hack committed
670

671
	irg = get_Block_irg(bl);
672
673
	irn = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T, be_pos_AddSP_last, in);
	a   = init_node_attr(irn, be_pos_AddSP_last);
Sebastian Hack's avatar
Sebastian Hack committed
674

Sebastian Hack's avatar
Sebastian Hack committed
675
	/* Set output constraint to stack register. */
676
677
678
	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);
679

680
	cls = arch_register_get_class(sp);
Sebastian Hack's avatar
Sebastian Hack committed
681
682
683
684

	return irn;
}

685
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
686
687
688
689
{
	be_node_attr_t *a;
	ir_node *irn;
	ir_node *in[be_pos_SubSP_last];
690
	ir_graph *irg;
Michael Beck's avatar
Michael Beck committed
691
692
693
694

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

695
	irg = get_Block_irg(bl);
Michael Beck's avatar
Michael Beck committed
696
697
698
699
	irn = new_ir_node(NULL, irg, bl, op_be_SubSP, mode_T, be_pos_SubSP_last, in);
	a   = init_node_attr(irn, be_pos_SubSP_last);

	/* Set output constraint to stack register. */
700
701
702
	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
703
704
705
706

	return irn;
}

707
ir_node *be_new_RegParams(ir_node *bl, int n_outs)
Sebastian Hack's avatar
Sebastian Hack committed
708
{
709
710
	ir_node *res;
	int i;
711
	ir_graph *irg = get_Block_irg(bl);
Sebastian Hack's avatar
Sebastian Hack committed
712

713
714
715
716
717
718
719
720
	res = new_ir_node(NULL, irg, bl, op_be_RegParams, mode_T, 0, NULL);
	init_node_attr(res, -1);
	for(i = 0; i < n_outs; ++i)
		add_register_req(res);

	return res;
}

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

728
	in[0]  = frame;
729
	irn    = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
Sebastian Hack's avatar
Sebastian Hack committed
730
	a      = init_node_attr(irn, 1);
731
732
	a->ent = ent;
	a->offset = 0;
733
734
	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
735
736

	return optimize_node(irn);
Sebastian Hack's avatar
Sebastian Hack committed
737
738
}

Matthias Braun's avatar
Matthias Braun committed
739
ir_node *be_get_FrameAddr_frame(const ir_node *node) {
Michael Beck's avatar
Michael Beck committed
740
741
742
743
	assert(be_is_FrameAddr(node));
	return get_irn_n(node, be_pos_FrameAddr_ptr);
}

Matthias Braun's avatar
Matthias Braun committed
744
745
746
747
748
749
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;
}

750
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
751
{
752
753
	ir_node  *irn;
	ir_node **in = ALLOCAN(ir_node*, n + 1);
754
	ir_graph *irg = get_Block_irg(bl);
Sebastian Hack's avatar
Sebastian Hack committed
755
756
757
758

	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);
Sebastian Hack's avatar
Sebastian Hack committed
759
	init_node_attr(irn, n + 1);
760
761
	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
762
763
764
765

	return irn;
}

766
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
767
{
768
	return be_new_CopyKeep(cls, bl, src, 1, &keep, mode);
Sebastian Hack's avatar
Sebastian Hack committed
769
770
}

771
772
773
774
775
776
777
778
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);
}

779
ir_node *be_new_Barrier(ir_node *bl, int n, ir_node *in[])
Sebastian Hack's avatar
Sebastian Hack committed
780
{
781
782
	ir_node *res;
	int i;
783
	ir_graph *irg = get_Block_irg(bl);
Sebastian Hack's avatar
Sebastian Hack committed
784

785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
	res = new_ir_node(NULL, irg, bl, op_be_Barrier, mode_T, -1, NULL);
	init_node_attr(res, -1);
	for(i = 0; i < n; ++i) {
		add_irn_n(res, in[i]);
		add_register_req(res);
	}

	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);
800

801
	ir_node *proj = new_r_Proj(block, barrier, mode, n);
802
803
804
	add_register_req(barrier);

	return proj;
Sebastian Hack's avatar
Sebastian Hack committed
805
806
}

Michael Beck's avatar
Michael Beck committed
807
/* Construct a new be_Unwind. */
808
ir_node *be_new_Unwind(dbg_info *dbg, ir_node *block,
Michael Beck's avatar
Michael Beck committed
809
810
811
812
					   ir_node *mem, ir_node *sp)
{
	ir_node *res;
	ir_node *in[2];
813
	ir_graph *irg = get_Block_irg(block);
Michael Beck's avatar
Michael Beck committed
814
815
816
817
818
819
820
821
822

	in[be_pos_Unwind_mem] = mem;
	in[be_pos_Unwind_sp]  = sp;
	res = new_ir_node(dbg, irg, block, op_be_Unwind, mode_X, 2, in);
	init_node_attr(res, -1);

	return res;
}

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];
}

874
void be_set_MemPerm_out_entity(const ir_node *irn, int n, ir_entity *ent)
Matthias Braun's avatar
Matthias Braun committed
875
{
Matthias Braun's avatar
Matthias Braun committed
876
	const be_memperm_attr_t *attr = get_irn_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
877
878

	assert(be_is_MemPerm(irn));
879
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
880
881
882
883

	attr->out_entities[n] = ent;
}

884
ir_entity* be_get_MemPerm_out_entity(const ir_node* irn, int n)
Matthias Braun's avatar
Matthias Braun committed
885
{
Matthias Braun's avatar
Matthias Braun committed