benode.c 45.7 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
 */
Michael Beck's avatar
Michael Beck committed
30
#ifdef HAVE_CONFIG_H
31
#include "config.h"
Michael Beck's avatar
Michael Beck committed
32
#endif
Sebastian Hack's avatar
Sebastian Hack committed
33
34
35
36
37
38

#include <stdlib.h>

#include "obst.h"
#include "set.h"
#include "pmap.h"
39
#include "util.h"
Sebastian Hack's avatar
Sebastian Hack committed
40
#include "debug.h"
41
#include "fourcc.h"
Sebastian Hack's avatar
Sebastian Hack committed
42
#include "offset.h"
Sebastian Hack's avatar
Sebastian Hack committed
43
#include "bitfiddle.h"
Matthias Braun's avatar
Matthias Braun committed
44
#include "raw_bitset.h"
45
#include "error.h"
Sebastian Hack's avatar
Sebastian Hack committed
46
47
48
49

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

55
56
57
#include "be_t.h"
#include "belive_t.h"
#include "besched_t.h"
Sebastian Hack's avatar
Sebastian Hack committed
58
#include "benode_t.h"
59
#include "bearch_t.h"
Sebastian Hack's avatar
Sebastian Hack committed
60

61
62
#include "beirgmod.h"

Sebastian Hack's avatar
Sebastian Hack committed
63
64
#define OUT_POS(x) (-((x) + 1))

Matthias Braun's avatar
Matthias Braun committed
65
66
#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
67

Sebastian Hack's avatar
Sebastian Hack committed
68
69
typedef struct {
	arch_register_req_t req;
Sebastian Hack's avatar
Sebastian Hack committed
70
	arch_irn_flags_t    flags;
Sebastian Hack's avatar
Sebastian Hack committed
71
72
} be_req_t;

73
typedef struct {
74
	const arch_register_t *reg;
75
76
	be_req_t               req;
	be_req_t               in_req;
77
78
} be_reg_data_t;

79
/** The generic be nodes attribute type. */
80
typedef struct {
Michael Beck's avatar
Michael Beck committed
81
	be_reg_data_t *reg_data;
82
83
} be_node_attr_t;

84
85
/** The be_Return nodes attribute type. */
typedef struct {
Michael Beck's avatar
Michael Beck committed
86
	be_node_attr_t node_attr;     /**< base attributes of every be node. */
87
	int            num_ret_vals;  /**< number of return values */
88
	unsigned       pop;           /**< number of bytes that should be popped */
89
	int            emit_pop;      /**< if set, emit pop bytes, even if pop = 0 */
90
91
} be_return_attr_t;

Michael Beck's avatar
Michael Beck committed
92
/** The be_IncSP attribute type. */
Sebastian Hack's avatar
Sebastian Hack committed
93
typedef struct {
Michael Beck's avatar
Michael Beck committed
94
	be_node_attr_t node_attr;   /**< base attributes of every be node. */
95
	int            offset;      /**< The offset by which the stack shall be expanded/shrinked. */
96
97
	int            align;       /**< whether stack should be aligned after the
	                                 IncSP */
Michael Beck's avatar
Michael Beck committed
98
} be_incsp_attr_t;
Sebastian Hack's avatar
Sebastian Hack committed
99

100
/** The be_Frame attribute type. */
101
typedef struct {
102
103
104
	be_node_attr_t  node_attr;   /**< base attributes of every be node. */
	ir_entity      *ent;
	int             offset;
105
106
} be_frame_attr_t;

107
/** The be_Call attribute type. */
108
typedef struct {
109
	be_node_attr_t  node_attr;  /**< base attributes of every be node. */
110
	ir_entity      *ent;        /**< The called entity if this is a static call. */
111
	unsigned        pop;
112
	ir_type        *call_tp;    /**< The call type, copied from the original Call node. */
113
114
} be_call_attr_t;

115
typedef struct {
116
117
118
	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
119
} be_memperm_attr_t;
120

121
122
123
ir_op *op_be_Spill;
ir_op *op_be_Reload;
ir_op *op_be_Perm;
Matthias Braun's avatar
Matthias Braun committed
124
ir_op *op_be_MemPerm;
125
126
ir_op *op_be_Copy;
ir_op *op_be_Keep;
Sebastian Hack's avatar
Sebastian Hack committed
127
ir_op *op_be_CopyKeep;
128
129
130
ir_op *op_be_Call;
ir_op *op_be_Return;
ir_op *op_be_IncSP;
Sebastian Hack's avatar
Sebastian Hack committed
131
ir_op *op_be_AddSP;
Michael Beck's avatar
Michael Beck committed
132
ir_op *op_be_SubSP;
133
134
ir_op *op_be_RegParams;
ir_op *op_be_FrameAddr;
135
ir_op *op_be_Barrier;
Michael Beck's avatar
Michael Beck committed
136
ir_op *op_be_Unwind;
Sebastian Hack's avatar
Sebastian Hack committed
137

Sebastian Hack's avatar
Sebastian Hack committed
138
static const ir_op_ops be_node_op_ops;
Sebastian Hack's avatar
Sebastian Hack committed
139

140
141
142
143
144
145
146
147
148
149
#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
150
#define M   irop_flag_uses_memory
151

152
153
154
155
156
157
158
159
160
static int be_reqs_equal(const be_req_t *req1, const be_req_t *req2)
{
	if(!reg_reqs_equal(&req1->req, &req2->req))
		return 0;
	if(req1->flags != req2->flags)
		return 0;

	return 1;
}
161

Michael Beck's avatar
Michael Beck committed
162
/**
Michael Beck's avatar
Michael Beck committed
163
 * Compare two be node attributes.
Michael Beck's avatar
Michael Beck committed
164
165
166
 *
 * @return zero if both attributes are identically
 */
Michael Beck's avatar
Michael Beck committed
167
static int _node_cmp_attr(const be_node_attr_t *a, const be_node_attr_t *b) {
168
169
	int i, len;

Michael Beck's avatar
Michael Beck committed
170
	if (ARR_LEN(a->reg_data) != ARR_LEN(b->reg_data))
171
		return 1;
Michael Beck's avatar
Michael Beck committed
172

173
174
175
	len = ARR_LEN(a->reg_data);
	for (i = 0; i < len; ++i) {
		if (a->reg_data[i].reg != b->reg_data[i].reg ||
176
177
				!be_reqs_equal(&a->reg_data[i].in_req, &b->reg_data[i].in_req) ||
			    !be_reqs_equal(&a->reg_data[i].req,    &b->reg_data[i].req))
178
			return 1;
Michael Beck's avatar
Michael Beck committed
179
	}
180
181

	return 0;
Michael Beck's avatar
Michael Beck committed
182
183
}

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

	return _node_cmp_attr(a_attr, b_attr);
}

Michael Beck's avatar
Michael Beck committed
196
/**
Michael Beck's avatar
Michael Beck committed
197
 * Compare the attributes of two be_FrameAddr nodes.
Michael Beck's avatar
Michael Beck committed
198
 *
Michael Beck's avatar
Michael Beck committed
199
 * @return zero if both nodes have identically attributes
Michael Beck's avatar
Michael Beck committed
200
 */
201
static int FrameAddr_cmp_attr(ir_node *a, ir_node *b) {
Michael Beck's avatar
Michael Beck committed
202
203
	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
204

205
206
207
	if (a_attr->ent != b_attr->ent || a_attr->offset != b_attr->offset)
		return 1;

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

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

	if (a_attr->num_ret_vals != b_attr->num_ret_vals)
		return 1;
222
223
	if (a_attr->pop != b_attr->pop)
		return 1;
224
225
	if (a_attr->emit_pop != b_attr->emit_pop)
		return 1;
226

Michael Beck's avatar
Michael Beck committed
227
	return _node_cmp_attr(&a_attr->node_attr, &b_attr->node_attr);
228
229
}

Michael Beck's avatar
Michael Beck committed
230
231
232
233
234
/**
 * Compare the attributes of two be_IncSP nodes.
 *
 * @return zero if both nodes have identically attributes
 */
235
static int IncSP_cmp_attr(ir_node *a, ir_node *b) {
Michael Beck's avatar
Michael Beck committed
236
237
	const be_incsp_attr_t *a_attr = get_irn_attr_const(a);
	const be_incsp_attr_t *b_attr = get_irn_attr_const(b);
238
239
240
241

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

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

Michael Beck's avatar
Michael Beck committed
245
246
247
248
249
/**
 * Compare the attributes of two be_Call nodes.
 *
 * @return zero if both nodes have identically attributes
 */
250
static int Call_cmp_attr(ir_node *a, ir_node *b) {
Michael Beck's avatar
Michael Beck committed
251
252
	const be_call_attr_t *a_attr = get_irn_attr_const(a);
	const be_call_attr_t *b_attr = get_irn_attr_const(b);
253
254
255
256
257

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

Michael Beck's avatar
Michael Beck committed
258
	return _node_cmp_attr(&a_attr->node_attr, &b_attr->node_attr);
Michael Beck's avatar
Michael Beck committed
259
260
}

Matthias Braun's avatar
Matthias Braun committed
261
262
263
static INLINE be_req_t *get_be_req(const ir_node *node, int pos)
{
	int idx;
Matthias Braun's avatar
Matthias Braun committed
264
	const be_node_attr_t *attr;
Matthias Braun's avatar
Matthias Braun committed
265
266
267
	be_reg_data_t *rd;

	assert(is_be_node(node));
Matthias Braun's avatar
Matthias Braun committed
268
	attr = get_irn_attr_const(node);
Matthias Braun's avatar
Matthias Braun committed
269
270
271
272
273
274
275
276
277
278
279
280
281

	if(pos < 0) {
		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;
}

282
static INLINE arch_register_req_t *get_req(const ir_node *node, int pos)
Matthias Braun's avatar
Matthias Braun committed
283
284
285
286
287
{
	be_req_t *bereq = get_be_req(node, pos);
	return &bereq->req;
}

288
289
290
/**
 * Initializes the generic attribute of all be nodes and return ir.
 */
291
static void *init_node_attr(ir_node *node, int max_reg_data)
Sebastian Hack's avatar
Sebastian Hack committed
292
{
293
	ir_graph *irg = get_irn_irg(node);
294
	struct obstack *obst = get_irg_obstack(irg);
295
	be_node_attr_t *a = get_irn_attr(node);
Sebastian Hack's avatar
Sebastian Hack committed
296

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

299
300
	if(max_reg_data >= 0) {
		a->reg_data = NEW_ARR_D(be_reg_data_t, obst, max_reg_data);
Sebastian Hack's avatar
Sebastian Hack committed
301
		memset(a->reg_data, 0, max_reg_data * sizeof(a->reg_data[0]));
302
303
	} else {
		a->reg_data = NEW_ARR_F(be_reg_data_t, 0);
Sebastian Hack's avatar
Sebastian Hack committed
304
305
306
307
	}

	return a;
}
308

309
310
311
312
313
314
315
316
static void add_register_req(ir_node *node)
{
	be_node_attr_t *a = get_irn_attr(node);
	be_reg_data_t regreq;
	memset(&regreq, 0, sizeof(regreq));
	ARR_APP1(be_reg_data_t, a->reg_data, regreq);
}

Michael Beck's avatar
Michael Beck committed
317
318
319
320
321
322
323
324
325
326
/**
 * Skip Proj nodes and return their Proj numbers.
 *
 * If *node is a Proj or Proj(Proj) node, skip it.
 *
 * @param node  points to the node to be skipped
 *
 * @return 0 if *node was no Proj node, its Proj number else.
 */
static int redir_proj(const ir_node **node)
Sebastian Hack's avatar
Sebastian Hack committed
327
328
329
330
{
	const ir_node *n = *node;

	if(is_Proj(n)) {
331
332
333
334
335
336
337
		ir_node *irn;

		*node = irn = get_Proj_pred(n);
		if(is_Proj(irn)) {
			assert(get_irn_mode(irn) == mode_T);
			*node = get_Proj_pred(irn);
		}
Sebastian Hack's avatar
Sebastian Hack committed
338
339
340
341
342
343
		return get_Proj_proj(n);
	}

	return 0;
}

Matthias Braun's avatar
Matthias Braun committed
344
static be_reg_data_t *retrieve_reg_data(const ir_node *node)
Sebastian Hack's avatar
Sebastian Hack committed
345
{
Matthias Braun's avatar
Matthias Braun committed
346
	const be_node_attr_t *attr;
Matthias Braun's avatar
Matthias Braun committed
347
	int pos = 0;
Sebastian Hack's avatar
Sebastian Hack committed
348

Matthias Braun's avatar
Matthias Braun committed
349
350
351
	if(is_Proj(node)) {
		pos = get_Proj_proj(node);
		node = get_Proj_pred(node);
Sebastian Hack's avatar
Sebastian Hack committed
352
353
	}

Matthias Braun's avatar
Matthias Braun committed
354
	assert(is_be_node(node));
Matthias Braun's avatar
Matthias Braun committed
355
	attr = get_irn_attr_const(node);
Matthias Braun's avatar
Matthias Braun committed
356
	assert(pos >= 0 && pos < ARR_LEN(attr->reg_data) && "illegal proj number");
Sebastian Hack's avatar
Sebastian Hack committed
357

Matthias Braun's avatar
Matthias Braun committed
358
	return &attr->reg_data[pos];
Sebastian Hack's avatar
Sebastian Hack committed
359
360
}

Sebastian Hack's avatar
Sebastian Hack committed
361
static void
362
be_node_set_irn_reg(ir_node *irn, const arch_register_t *reg)
Sebastian Hack's avatar
Sebastian Hack committed
363
{
Sebastian Hack's avatar
Sebastian Hack committed
364
	be_reg_data_t *r = retrieve_reg_data(irn);
Matthias Braun's avatar
Matthias Braun committed
365
	r->reg = reg;
Sebastian Hack's avatar
Sebastian Hack committed
366
367
}

368
369
ir_node *be_new_Spill(const arch_register_class_t *cls, const arch_register_class_t *cls_frame,
	ir_graph *irg, ir_node *bl, ir_node *frame, ir_node *to_spill)
370
{
Matthias Braun's avatar
Matthias Braun committed
371
	be_frame_attr_t *a;
372
	ir_node         *in[2];
373
	ir_node         *res;
374

375
376
377
	in[0]     = frame;
	in[1]     = to_spill;
	res       = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
378
379
	a         = init_node_attr(res, 2);
	a->ent    = NULL;
Matthias Braun's avatar
Matthias Braun committed
380
	a->offset = 0;
Sebastian Hack's avatar
Sebastian Hack committed
381

382
	be_node_set_reg_class(res, be_pos_Spill_frame, cls_frame);
383
	be_node_set_reg_class(res, be_pos_Spill_val, cls);
Sebastian Hack's avatar
Sebastian Hack committed
384
385
	return res;
}
386

387
388
ir_node *be_new_Reload(const arch_register_class_t *cls, const arch_register_class_t *cls_frame,
	ir_graph *irg, ir_node *bl, ir_node *frame, ir_node *mem, ir_mode *mode)
389
{
390
391
392
393
394
395
396
	ir_node *in[2];
	ir_node *res;

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

Sebastian Hack's avatar
Sebastian Hack committed
397
398
	init_node_attr(res, 2);
	be_node_set_reg_class(res, -1, cls);
399
	be_node_set_reg_class(res, be_pos_Reload_frame, cls_frame);
400
	be_node_set_flags(res, -1, arch_irn_flags_rematerializable);
Sebastian Hack's avatar
Sebastian Hack committed
401
402
	return res;
}
403

404
ir_node *be_get_Reload_mem(const ir_node *irn)
Sebastian Hack's avatar
Sebastian Hack committed
405
406
{
	assert(be_is_Reload(irn));
Daniel Grund's avatar
Daniel Grund committed
407
	return get_irn_n(irn, be_pos_Reload_mem);
Sebastian Hack's avatar
Sebastian Hack committed
408
409
}

410
411
412
413
414
415
ir_node *be_get_Reload_frame(const ir_node *irn)
{
	assert(be_is_Reload(irn));
	return get_irn_n(irn, be_pos_Reload_frame);
}

416
417
418
419
420
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
421

422
423
424
425
426
ir_node *be_get_Spill_frame(const ir_node *irn)
{
	assert(be_is_Spill(irn));
	return get_irn_n(irn, be_pos_Spill_frame);
}
427

Sebastian Hack's avatar
Sebastian Hack committed
428
429
ir_node *be_new_Perm(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
{
Sebastian Hack's avatar
Sebastian Hack committed
430
	int i;
431
	ir_node *irn = new_ir_node(NULL, irg, bl, op_be_Perm, mode_T, n, in);
Sebastian Hack's avatar
Sebastian Hack committed
432
433
434
435
436
437
	init_node_attr(irn, n);
	for(i = 0; i < n; ++i) {
		be_node_set_reg_class(irn, i, cls);
		be_node_set_reg_class(irn, OUT_POS(i), cls);
	}

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

441
442
void be_Perm_reduce(ir_node *perm, int new_size, int *map)
{
443
444
445
446
	int            arity     = get_irn_arity(perm);
	be_reg_data_t  *old_data = alloca(arity * sizeof(old_data[0]));
	be_node_attr_t *attr     = get_irn_attr(perm);
	ir_node        **new_in;
447
448
449
450

	int i;

	assert(be_is_Perm(perm));
451
	assert(new_size <= arity);
452

453
454
	NEW_ARR_A(ir_node *, new_in, new_size);

455
	/* save the old register data */
456
	memcpy(old_data, attr->reg_data, arity * sizeof(old_data[0]));
457
458

	/* compose the new in array and set the new register data directly in place */
459
	for (i = 0; i < new_size; ++i) {
460
		int idx = map[i];
461
462
		new_in[i]         = get_irn_n(perm, idx);
		attr->reg_data[i] = old_data[idx];
463
464
465
466
467
	}

	set_irn_in(perm, new_size, new_in);
}

Matthias Braun's avatar
Matthias Braun committed
468
469
470
471
472
ir_node *be_new_MemPerm(const arch_env_t *arch_env, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
{
	int i;
	ir_node *frame = get_irg_frame(irg);
	const arch_register_class_t *cls_frame = arch_get_irn_reg_class(arch_env, frame, -1);
473
	ir_node *irn;
474
	const arch_register_t *sp = arch_env->sp;
Matthias Braun's avatar
Matthias Braun committed
475
	be_memperm_attr_t *attr;
476
	ir_node **real_in;
Matthias Braun's avatar
Matthias Braun committed
477

478
479
480
481
482
483
484
485
	real_in = alloca((n+1) * sizeof(real_in[0]));
	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);
	be_node_set_reg_class(irn, 0, sp->reg_class);
Michael Beck's avatar
Michael Beck committed
486
	for (i = 0; i < n; ++i) {
487
		be_node_set_reg_class(irn, i + 1, cls_frame);
Matthias Braun's avatar
Matthias Braun committed
488
489
490
491
492
		be_node_set_reg_class(irn, OUT_POS(i), cls_frame);
	}

	attr = get_irn_attr(irn);

493
494
	attr->in_entities = obstack_alloc(irg->obst, n * sizeof(attr->in_entities[0]));
	memset(attr->in_entities, 0, n * sizeof(attr->in_entities[0]));
Matthias Braun's avatar
Matthias Braun committed
495
496
497
498
499
500
501
	attr->out_entities = obstack_alloc(irg->obst, n*sizeof(attr->out_entities[0]));
	memset(attr->out_entities, 0, n*sizeof(attr->out_entities[0]));

	return irn;
}


Sebastian Hack's avatar
Sebastian Hack committed
502
503
504
505
ir_node *be_new_Copy(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *op)
{
	ir_node *in[1];
	ir_node *res;
Matthias Braun's avatar
Matthias Braun committed
506
	arch_register_req_t *req;
507

Sebastian Hack's avatar
Sebastian Hack committed
508
	in[0] = op;
509
	res   = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
Sebastian Hack's avatar
Sebastian Hack committed
510
511
512
	init_node_attr(res, 1);
	be_node_set_reg_class(res, 0, cls);
	be_node_set_reg_class(res, OUT_POS(0), cls);
Matthias Braun's avatar
Matthias Braun committed
513
514
515
516

	req = get_req(res, OUT_POS(0));
	req->cls = cls;
	req->type = arch_register_req_type_should_be_same;
517
	req->other_same = 1U << 0;
Matthias Braun's avatar
Matthias Braun committed
518

Sebastian Hack's avatar
Sebastian Hack committed
519
	return res;
520
521
}

522
523
524
525
ir_node *be_get_Copy_op(const ir_node *cpy) {
	return get_irn_n(cpy, be_pos_Copy_op);
}

526
527
528
529
void be_set_Copy_op(ir_node *cpy, ir_node *op) {
	set_irn_n(cpy, be_pos_Copy_op, op);
}

Sebastian Hack's avatar
Sebastian Hack committed
530
ir_node *be_new_Keep(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
Sebastian Hack's avatar
Sebastian Hack committed
531
{
Sebastian Hack's avatar
Sebastian Hack committed
532
	int i;
533
534
535
536
	ir_node *res;

	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
537

Sebastian Hack's avatar
Sebastian Hack committed
538
	for(i = 0; i < n; ++i) {
539
540
541
		add_irn_n(res, in[i]);
		add_register_req(res);
		be_node_set_reg_class(res, i, cls);
Sebastian Hack's avatar
Sebastian Hack committed
542
	}
543
544
545
546
547
548
549
550
551
552
553
554
555
	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);
	be_node_set_reg_class(keep, n, cls);
Sebastian Hack's avatar
Sebastian Hack committed
556
557
}

Michael Beck's avatar
Michael Beck committed
558
/* creates a be_Call */
559
560
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
561
{
562
563
	be_call_attr_t *a;
	int real_n = be_pos_Call_first_arg + n;
Sebastian Hack's avatar
Sebastian Hack committed
564
565
	ir_node *irn;
	ir_node **real_in;
Sebastian Hack's avatar
Sebastian Hack committed
566

567
568
569
570
571
	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
572

573
	irn = new_ir_node(dbg, irg, bl, op_be_Call, mode_T, real_n, real_in);
574
575
576
	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
577
	a->pop     = 0;
Sebastian Hack's avatar
Sebastian Hack committed
578
	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
579
580
}

581
/* Gets the call entity or NULL if this is no static call. */
582
ir_entity *be_Call_get_entity(const ir_node *call) {
Matthias Braun's avatar
Matthias Braun committed
583
	const be_call_attr_t *a = get_irn_attr_const(call);
584
585
586
587
	assert(be_is_Call(call));
	return a->ent;
}

588
/* Sets the call entity. */
589
void be_Call_set_entity(ir_node *call, ir_entity *ent) {
590
591
592
593
594
	be_call_attr_t *a = get_irn_attr(call);
	assert(be_is_Call(call));
	a->ent = ent;
}

595
596
/* Gets the call type. */
ir_type *be_Call_get_type(ir_node *call) {
Matthias Braun's avatar
Matthias Braun committed
597
	const be_call_attr_t *a = get_irn_attr_const(call);
598
599
600
601
602
603
604
605
606
607
608
	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
609
610
611
612
613
614
615
616
617
618
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;
}

619
/* Construct a new be_Return. */
620
ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *block, int n_res,
621
                       unsigned pop, int n, ir_node *in[])
Sebastian Hack's avatar
Sebastian Hack committed
622
{
623
	be_return_attr_t *a;
624
625
626
627
628
629
630
631
632
633
634
	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);
635
	a->num_ret_vals = n_res;
636
	a->pop          = pop;
637
	a->emit_pop     = 0;
Sebastian Hack's avatar
Sebastian Hack committed
638

639
	return res;
Sebastian Hack's avatar
Sebastian Hack committed
640
641
}

642
/* Returns the number of real returns values */
643
int be_Return_get_n_rets(const ir_node *ret) {
644
	const be_return_attr_t *a = get_irn_generic_attr_const(ret);
645
646
647
	return a->num_ret_vals;
}

648
649
/* return the number of bytes that should be popped from stack when executing the Return. */
unsigned be_Return_get_pop(const ir_node *ret) {
650
651
652
653
	const be_return_attr_t *a = get_irn_generic_attr_const(ret);
	return a->pop;
}

654
655
656
657
658
659
660
661
662
663
664
665
666
/* 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) {
667
668
669
670
671
672
673
674
	int pos;

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

	return pos;
}

675
676
ir_node *be_new_IncSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl,
                      ir_node *old_sp, int offset, int align)
Sebastian Hack's avatar
Sebastian Hack committed
677
{
Michael Beck's avatar
Michael Beck committed
678
	be_incsp_attr_t *a;
Sebastian Hack's avatar
Sebastian Hack committed
679
	ir_node *irn;
Sebastian Hack's avatar
Sebastian Hack committed
680
	ir_node *in[1];
Sebastian Hack's avatar
Sebastian Hack committed
681
682

	in[0]     = old_sp;
683
684
	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
685
	a         = init_node_attr(irn, 1);
Sebastian Hack's avatar
Sebastian Hack committed
686
	a->offset = offset;
687
	a->align  = align;
Sebastian Hack's avatar
Sebastian Hack committed
688

689
	be_node_set_flags(irn, -1, arch_irn_flags_ignore | arch_irn_flags_modify_sp);
Sebastian Hack's avatar
Sebastian Hack committed
690

Sebastian Hack's avatar
Sebastian Hack committed
691
	/* Set output constraint to stack register. */
Sebastian Hack's avatar
Sebastian Hack committed
692
	be_node_set_reg_class(irn, 0, sp->reg_class);
693
	be_set_constr_single_reg(irn, BE_OUT_POS(0), sp);
694
	be_node_set_irn_reg(irn, sp);
Sebastian Hack's avatar
Sebastian Hack committed
695
696

	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
697
}
Sebastian Hack's avatar
Sebastian Hack committed
698

Sebastian Hack's avatar
Sebastian Hack committed
699
ir_node *be_new_AddSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, ir_node *sz)
Sebastian Hack's avatar
Sebastian Hack committed
700
{
Sebastian Hack's avatar
Sebastian Hack committed
701
	be_node_attr_t *a;
Sebastian Hack's avatar
Sebastian Hack committed
702
	ir_node *irn;
Sebastian Hack's avatar
Sebastian Hack committed
703
	ir_node *in[be_pos_AddSP_last];
704
	const arch_register_class_t *cls;
Sebastian Hack's avatar
Sebastian Hack committed
705

Sebastian Hack's avatar
Sebastian Hack committed
706
707
	in[be_pos_AddSP_old_sp] = old_sp;
	in[be_pos_AddSP_size]   = sz;
Sebastian Hack's avatar
Sebastian Hack committed
708

709
710
	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
711

712
713
	be_node_set_flags(irn, OUT_POS(pn_be_AddSP_sp),
	                  arch_irn_flags_ignore | arch_irn_flags_modify_sp);
Sebastian Hack's avatar
Sebastian Hack committed
714
715

	/* Set output constraint to stack register. */
716
717
	be_set_constr_single_reg(irn, be_pos_AddSP_old_sp, sp);
	be_node_set_reg_class(irn, be_pos_AddSP_size, arch_register_get_class(sp));
718
719
720
	be_set_constr_single_reg(irn, OUT_POS(pn_be_AddSP_sp), sp);
	a->reg_data[pn_be_AddSP_sp].reg = sp;

721
722
	cls = arch_register_get_class(sp);
	be_node_set_reg_class(irn, OUT_POS(pn_be_AddSP_res), cls);
Sebastian Hack's avatar
Sebastian Hack committed
723
724
725
726

	return irn;
}

Michael Beck's avatar
Michael Beck committed
727
728
729
730
731
732
733
734
735
736
737
738
ir_node *be_new_SubSP(const arch_register_t *sp, ir_graph *irg, ir_node *bl, ir_node *old_sp, ir_node *sz)
{
	be_node_attr_t *a;
	ir_node *irn;
	ir_node *in[be_pos_SubSP_last];

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

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

739
740
	be_node_set_flags(irn, OUT_POS(pn_be_SubSP_sp),
	                  arch_irn_flags_ignore | arch_irn_flags_modify_sp);
Michael Beck's avatar
Michael Beck committed
741
742
743
744

	/* Set output constraint to stack register. */
	be_set_constr_single_reg(irn, be_pos_SubSP_old_sp, sp);
	be_node_set_reg_class(irn, be_pos_SubSP_size, arch_register_get_class(sp));
745
746
	be_set_constr_single_reg(irn, OUT_POS(pn_be_SubSP_sp), sp);
	a->reg_data[pn_be_SubSP_sp].reg = sp;
Michael Beck's avatar
Michael Beck committed
747
748
749
750

	return irn;
}

751
ir_node *be_new_RegParams(ir_graph *irg, ir_node *bl, int n_outs)
Sebastian Hack's avatar
Sebastian Hack committed
752
{
753
754
	ir_node *res;
	int i;
Sebastian Hack's avatar
Sebastian Hack committed
755

756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
	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;
}

ir_node *be_RegParams_append_out_reg(ir_node *regparams,
                                     const arch_env_t *arch_env,
                                     const arch_register_t *reg)
{
	ir_graph *irg = get_irn_irg(regparams);
	ir_node *block = get_nodes_block(regparams);
	be_node_attr_t *attr = get_irn_attr(regparams);
	const arch_register_class_t *cls = arch_register_get_class(reg);
	ir_mode *mode = arch_register_class_mode(cls);
	int n = ARR_LEN(attr->reg_data);
Michael Beck's avatar
Michael Beck committed
774
	ir_node *proj;
775
776

	assert(be_is_RegParams(regparams));
Michael Beck's avatar
Michael Beck committed
777
	proj = new_r_Proj(irg, block, regparams, mode, n);
778
	add_register_req(regparams);
779
	be_set_constr_single_reg(regparams, BE_OUT_POS(n), reg);
780
781
	arch_set_irn_register(arch_env, proj, reg);

Michael Beck's avatar
Michael Beck committed
782
	/* TODO decide, whether we need to set ignore/modify sp flags here? */
783
784

	return proj;
Sebastian Hack's avatar
Sebastian Hack committed
785
786
}

787
ir_node *be_new_FrameAddr(const arch_register_class_t *cls_frame, ir_graph *irg, ir_node *bl, ir_node *frame, ir_entity *ent)
Sebastian Hack's avatar
Sebastian Hack committed
788
{
789
	be_frame_attr_t *a;
Sebastian Hack's avatar
Sebastian Hack committed
790
791
792
	ir_node *irn;
	ir_node *in[1];

793
	in[0]  = frame;
794
	irn    = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
Sebastian Hack's avatar
Sebastian Hack committed
795
	a      = init_node_attr(irn, 1);
796
797
798
	a->ent = ent;
	a->offset = 0;
	be_node_set_reg_class(irn, 0, cls_frame);
Sebastian Hack's avatar
Sebastian Hack committed
799
	be_node_set_reg_class(irn, OUT_POS(0), cls_frame);
Michael Beck's avatar
Michael Beck committed
800
801

	return optimize_node(irn);
Sebastian Hack's avatar
Sebastian Hack committed
802
803
}

Matthias Braun's avatar
Matthias Braun committed
804
ir_node *be_get_FrameAddr_frame(const ir_node *node) {
Michael Beck's avatar
Michael Beck committed
805
806
807
808
	assert(be_is_FrameAddr(node));
	return get_irn_n(node, be_pos_FrameAddr_ptr);
}

Matthias Braun's avatar
Matthias Braun committed
809
810
811
812
813
814
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;
}

Sebastian Hack's avatar
Sebastian Hack committed
815
816
817
818
819
820
821
822
ir_node *be_new_CopyKeep(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *src, int n, ir_node *in_keep[], ir_mode *mode)
{
	ir_node *irn;
	ir_node **in = (ir_node **) alloca((n + 1) * sizeof(in[0]));

	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
823
	init_node_attr(irn, n + 1);
Sebastian Hack's avatar
Sebastian Hack committed
824
825
826
827
828
829
830
831
	be_node_set_reg_class(irn, OUT_POS(0), cls);
	be_node_set_reg_class(irn, 0, cls);

	return irn;
}

ir_node *be_new_CopyKeep_single(const arch_register_class_t *cls, ir_graph *irg, ir_node *bl, ir_node *src, ir_node *keep, ir_mode *mode)
{
Michael Beck's avatar
Michael Beck committed
832
	return be_new_CopyKeep(cls, irg, bl, src, 1, &keep, mode);
Sebastian Hack's avatar
Sebastian Hack committed
833
834
}

835
836
837
838
839
840
841
842
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);
}

843
ir_node *be_new_Barrier(ir_graph *irg, ir_node *bl, int n, ir_node *in[])
Sebastian Hack's avatar
Sebastian Hack committed
844
{
845
846
	ir_node *res;
	int i;
Sebastian Hack's avatar
Sebastian Hack committed
847

848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
	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_graph *irg = get_irn_irg(barrier);
	ir_node *block = get_nodes_block(barrier);
	ir_mode *mode = get_irn_mode(node);
	int n = add_irn_n(barrier, node);
864

865
866
867
868
	ir_node *proj = new_r_Proj(irg, block, barrier, mode, n);
	add_register_req(barrier);

	return proj;
Sebastian Hack's avatar
Sebastian Hack committed
869
870
}

Michael Beck's avatar
Michael Beck committed
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
/* Construct a new be_Unwind. */
ir_node *be_new_Unwind(dbg_info *dbg, ir_graph *irg, ir_node *block,
					   ir_node *mem, ir_node *sp)
{
	ir_node *res;
	ir_node *in[2];

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

886
887
int be_has_frame_entity(const ir_node *irn)
{
888
	switch (get_irn_opcode(irn)) {
889
	case beo_Spill:
890
	case beo_Reload:
891
892
	case beo_FrameAddr:
		return 1;
893
894
	default:
		return 0;
895
896
897
	}
}

898
ir_entity *be_get_frame_entity(const ir_node *irn)
899
{
900
	if (be_has_frame_entity(irn)) {
Matthias Braun's avatar
Matthias Braun committed
901
		const be_frame_attr_t *a = get_irn_attr_const(irn);
902
903
904
905
		return a->ent;
	}
	return NULL;
}
Sebastian Hack's avatar
Sebastian Hack committed
906

907
908
909
910
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
911
		const be_frame_attr_t *a = get_irn_attr_const(irn);
912
913
914
915
916
		return a->offset;
	}
	return 0;
}

917
void be_set_MemPerm_in_entity(const ir_node *irn, int n, ir_entity *ent)
Matthias Braun's avatar
Matthias Braun committed
918
{
Matthias Braun's avatar
Matthias Braun committed
919
	const be_memperm_attr_t *attr = get_irn_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
920
921

	assert(be_is_MemPerm(irn));
922
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
923
924
925
926

	attr->in_entities[n] = ent;
}

927
ir_entity* be_get_MemPerm_in_entity(const ir_node* irn, int n)
Matthias Braun's avatar
Matthias Braun committed
928
{
Matthias Braun's avatar
Matthias Braun committed
929
	const be_memperm_attr_t *attr = get_irn_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
930
931

	assert(be_is_MemPerm(irn));
932
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
933
934
935
936

	return attr->in_entities[n];
}

937
void be_set_MemPerm_out_entity(const ir_node *irn, int n, ir_entity *ent)
Matthias Braun's avatar
Matthias Braun committed
938
{
Matthias Braun's avatar
Matthias Braun committed
939
	const be_memperm_attr_t *attr = get_irn_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
940
941

	assert(be_is_MemPerm(irn));
942
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
943
944
945
946

	attr->out_entities[n] = ent;
}

947
ir_entity* be_get_MemPerm_out_entity(const ir_node* irn, int n)
Matthias Braun's avatar
Matthias Braun committed
948
{
Matthias Braun's avatar
Matthias Braun committed
949
	const be_memperm_attr_t *attr = get_irn_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
950
951

	assert(be_is_MemPerm(irn));
952
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
953
954
955
956

	return attr->out_entities[n];
}

Matthias Braun's avatar