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
56
#include "be_t.h"
#include "belive_t.h"
#include "besched_t.h"
Sebastian Hack's avatar
Sebastian Hack committed
57
#include "benode_t.h"
58
#include "bearch_t.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
67
68
typedef struct {
	arch_register_req_t req;
} be_req_t;

69
typedef struct {
70
71
	be_req_t req;
	be_req_t in_req;
72
73
} be_reg_data_t;

74
/** The generic be nodes attribute type. */
75
typedef struct {
Michael Beck's avatar
Michael Beck committed
76
	be_reg_data_t *reg_data;
77
78
} be_node_attr_t;

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

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

95
/** The be_Frame attribute type. */
96
typedef struct {
97
98
99
	be_node_attr_t  node_attr;   /**< base attributes of every be node. */
	ir_entity      *ent;
	int             offset;
100
101
} be_frame_attr_t;

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

110
typedef struct {
111
112
113
	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
114
} be_memperm_attr_t;
115

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

Sebastian Hack's avatar
Sebastian Hack committed
133
static const ir_op_ops be_node_op_ops;
Sebastian Hack's avatar
Sebastian Hack committed
134

135
136
137
138
139
140
141
142
143
144
#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
145
#define M   irop_flag_uses_memory
146

147
148
149
150
151
152
153
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;

	return 1;
}
154

Michael Beck's avatar
Michael Beck committed
155
/**
Michael Beck's avatar
Michael Beck committed
156
 * Compare two be node attributes.
Michael Beck's avatar
Michael Beck committed
157
158
159
 *
 * @return zero if both attributes are identically
 */
Michael Beck's avatar
Michael Beck committed
160
static int _node_cmp_attr(const be_node_attr_t *a, const be_node_attr_t *b) {
161
162
	int i, len;

Michael Beck's avatar
Michael Beck committed
163
	if (ARR_LEN(a->reg_data) != ARR_LEN(b->reg_data))
164
		return 1;
Michael Beck's avatar
Michael Beck committed
165

166
167
	len = ARR_LEN(a->reg_data);
	for (i = 0; i < len; ++i) {
168
		if (!be_reqs_equal(&a->reg_data[i].in_req, &b->reg_data[i].in_req) ||
169
			    !be_reqs_equal(&a->reg_data[i].req,    &b->reg_data[i].req))
170
			return 1;
Michael Beck's avatar
Michael Beck committed
171
	}
172
173

	return 0;
Michael Beck's avatar
Michael Beck committed
174
175
}

Michael Beck's avatar
Michael Beck committed
176
177
178
179
180
/**
 * Compare the node attributes of two be_node's.
 *
 * @return zero if both nodes have identically attributes
 */
181
static int node_cmp_attr(ir_node *a, ir_node *b) {
Michael Beck's avatar
Michael Beck committed
182
183
	const be_node_attr_t *a_attr = get_irn_attr_const(a);
	const be_node_attr_t *b_attr = get_irn_attr_const(b);
184

185
186
187
188
	if(_node_cmp_attr(a_attr, b_attr) != 0)
		return 1;

	return !be_info_equal(a, b);
189
190
}

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

200
201
202
	if (a_attr->ent != b_attr->ent || a_attr->offset != b_attr->offset)
		return 1;

Michael Beck's avatar
Michael Beck committed
203
	return _node_cmp_attr(&a_attr->node_attr, &b_attr->node_attr);
204
205
}

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

	if (a_attr->num_ret_vals != b_attr->num_ret_vals)
		return 1;
217
218
	if (a_attr->pop != b_attr->pop)
		return 1;
219
220
	if (a_attr->emit_pop != b_attr->emit_pop)
		return 1;
221

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

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

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

Michael Beck's avatar
Michael Beck committed
237
	return _node_cmp_attr(&a_attr->node_attr, &b_attr->node_attr);
238
239
}

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

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

Michael Beck's avatar
Michael Beck committed
253
	return _node_cmp_attr(&a_attr->node_attr, &b_attr->node_attr);
Michael Beck's avatar
Michael Beck committed
254
255
}

256
static inline be_req_t *get_be_req(const ir_node *node, int pos)
Matthias Braun's avatar
Matthias Braun committed
257
258
{
	int idx;
Matthias Braun's avatar
Matthias Braun committed
259
	const be_node_attr_t *attr;
Matthias Braun's avatar
Matthias Braun committed
260
261
262
	be_reg_data_t *rd;

	assert(is_be_node(node));
Matthias Braun's avatar
Matthias Braun committed
263
	attr = get_irn_attr_const(node);
Matthias Braun's avatar
Matthias Braun committed
264
265
266
267
268
269
270
271
272
273
274
275
276

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

277
static inline arch_register_req_t *get_req(const ir_node *node, int pos)
Matthias Braun's avatar
Matthias Braun committed
278
279
280
281
282
{
	be_req_t *bereq = get_be_req(node, pos);
	return &bereq->req;
}

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

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

294
	if(max_reg_data >= 0) {
295
296
297
298
		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]));

299
		a->reg_data = NEW_ARR_D(be_reg_data_t, obst, max_reg_data);
Sebastian Hack's avatar
Sebastian Hack committed
300
		memset(a->reg_data, 0, max_reg_data * sizeof(a->reg_data[0]));
301
	} else {
302
303
304
		backend_info_t *info = be_get_info(node);
		info->out_infos = NEW_ARR_F(reg_out_info_t, 0);

305
		a->reg_data = NEW_ARR_F(be_reg_data_t, 0);
Sebastian Hack's avatar
Sebastian Hack committed
306
307
308
309
	}

	return a;
}
310

311
312
static void add_register_req(ir_node *node)
{
313
314
315
316
	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;
317
	memset(&regreq, 0, sizeof(regreq));
318
	memset(&out_info, 0, sizeof(out_info));
319
	ARR_APP1(be_reg_data_t, a->reg_data, regreq);
320
	ARR_APP1(reg_out_info_t, info->out_infos, out_info);
321
322
}

Michael Beck's avatar
Michael Beck committed
323
324
325
326
327
328
329
330
331
332
/**
 * 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
333
334
335
336
{
	const ir_node *n = *node;

	if(is_Proj(n)) {
337
338
339
340
341
342
343
		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
344
345
346
347
348
349
		return get_Proj_proj(n);
	}

	return 0;
}

350
351
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)
352
{
Matthias Braun's avatar
Matthias Braun committed
353
	be_frame_attr_t *a;
354
	ir_node         *in[2];
355
	ir_node         *res;
356

357
358
359
	in[0]     = frame;
	in[1]     = to_spill;
	res       = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
360
361
	a         = init_node_attr(res, 2);
	a->ent    = NULL;
Matthias Braun's avatar
Matthias Braun committed
362
	a->offset = 0;
Sebastian Hack's avatar
Sebastian Hack committed
363

364
365
	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);
Sebastian Hack's avatar
Sebastian Hack committed
366
367
	return res;
}
368

369
370
371
ir_node *be_new_Reload(const arch_register_class_t *cls,
		const arch_register_class_t *cls_frame, ir_graph *irg, ir_node *block,
		ir_node *frame, ir_node *mem, ir_mode *mode)
372
{
373
374
375
376
377
	ir_node *in[2];
	ir_node *res;

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

Sebastian Hack's avatar
Sebastian Hack committed
380
	init_node_attr(res, 2);
381
382
383
	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);
Sebastian Hack's avatar
Sebastian Hack committed
384
385
	return res;
}
386

387
ir_node *be_get_Reload_mem(const ir_node *irn)
Sebastian Hack's avatar
Sebastian Hack committed
388
389
{
	assert(be_is_Reload(irn));
Daniel Grund's avatar
Daniel Grund committed
390
	return get_irn_n(irn, be_pos_Reload_mem);
Sebastian Hack's avatar
Sebastian Hack committed
391
392
}

393
394
395
396
397
398
ir_node *be_get_Reload_frame(const ir_node *irn)
{
	assert(be_is_Reload(irn));
	return get_irn_n(irn, be_pos_Reload_frame);
}

399
400
401
402
403
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
404

405
406
407
408
409
ir_node *be_get_Spill_frame(const ir_node *irn)
{
	assert(be_is_Spill(irn));
	return get_irn_n(irn, be_pos_Spill_frame);
}
410

411
412
ir_node *be_new_Perm(const arch_register_class_t *cls, ir_graph *irg,
		ir_node *block, int n, ir_node *in[])
Sebastian Hack's avatar
Sebastian Hack committed
413
{
Sebastian Hack's avatar
Sebastian Hack committed
414
	int i;
415
	ir_node *irn = new_ir_node(NULL, irg, block, op_be_Perm, mode_T, n, in);
Sebastian Hack's avatar
Sebastian Hack committed
416
417
	init_node_attr(irn, n);
	for(i = 0; i < n; ++i) {
418
419
		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
420
421
	}

Sebastian Hack's avatar
Sebastian Hack committed
422
423
	return irn;
}
424

425
426
void be_Perm_reduce(ir_node *perm, int new_size, int *map)
{
427
428
429
430
431
	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);
432
	ir_node        **new_in;
433
434
435
436

	int i;

	assert(be_is_Perm(perm));
437
	assert(new_size <= arity);
438

439
	new_in = alloca(new_size * sizeof(*new_in));
440

441
	/* save the old register data */
442
	memcpy(old_data, attr->reg_data, arity * sizeof(old_data[0]));
443
	memcpy(old_infos, info->out_infos, arity * sizeof(old_infos[0]));
444
445

	/* compose the new in array and set the new register data directly in place */
446
	for (i = 0; i < new_size; ++i) {
447
		int idx = map[i];
448
449
450
		new_in[i]          = get_irn_n(perm, idx);
		attr->reg_data[i]  = old_data[idx];
		info->out_infos[i] = old_infos[idx];
451
452
453
454
455
	}

	set_irn_in(perm, new_size, new_in);
}

Matthias Braun's avatar
Matthias Braun committed
456
457
ir_node *be_new_MemPerm(const arch_env_t *arch_env, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
{
458
459
460
461
462
463
464
	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
465

466
	real_in = ALLOCAN(ir_node*, n + 1);
467
468
469
470
471
472
	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);
473
	be_node_set_reg_class_in(irn, 0, sp->reg_class);
Michael Beck's avatar
Michael Beck committed
474
	for (i = 0; i < n; ++i) {
475
476
		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
477
478
479
480
	}

	attr = get_irn_attr(irn);

481
482
	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
483
484
485
486
487
488
489
	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
490
491
492
493
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
494
	arch_register_req_t *req;
495

Sebastian Hack's avatar
Sebastian Hack committed
496
	in[0] = op;
497
	res   = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
Sebastian Hack's avatar
Sebastian Hack committed
498
	init_node_attr(res, 1);
499
500
	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
501

502
	req = get_req(res, BE_OUT_POS(0));
Matthias Braun's avatar
Matthias Braun committed
503
504
	req->cls = cls;
	req->type = arch_register_req_type_should_be_same;
505
	req->other_same = 1U << 0;
Matthias Braun's avatar
Matthias Braun committed
506

Sebastian Hack's avatar
Sebastian Hack committed
507
	return res;
508
509
}

510
511
512
513
ir_node *be_get_Copy_op(const ir_node *cpy) {
	return get_irn_n(cpy, be_pos_Copy_op);
}

514
515
516
517
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
518
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
519
{
Sebastian Hack's avatar
Sebastian Hack committed
520
	int i;
521
522
523
524
	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
525

Sebastian Hack's avatar
Sebastian Hack committed
526
	for(i = 0; i < n; ++i) {
527
528
		add_irn_n(res, in[i]);
		add_register_req(res);
529
		be_node_set_reg_class_in(res, i, cls);
Sebastian Hack's avatar
Sebastian Hack committed
530
	}
531
532
533
534
535
536
537
538
539
540
541
542
	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);
543
	be_node_set_reg_class_in(keep, n, cls);
Sebastian Hack's avatar
Sebastian Hack committed
544
545
}

Michael Beck's avatar
Michael Beck committed
546
/* creates a be_Call */
547
548
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
549
{
550
551
	be_call_attr_t *a;
	int real_n = be_pos_Call_first_arg + n;
Sebastian Hack's avatar
Sebastian Hack committed
552
553
	ir_node *irn;
	ir_node **real_in;
Sebastian Hack's avatar
Sebastian Hack committed
554

555
556
557
558
559
	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
560

561
	irn = new_ir_node(dbg, irg, bl, op_be_Call, mode_T, real_n, real_in);
562
563
564
	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
565
	a->pop     = 0;
Sebastian Hack's avatar
Sebastian Hack committed
566
	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
567
568
}

569
/* Gets the call entity or NULL if this is no static call. */
570
ir_entity *be_Call_get_entity(const 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
	assert(be_is_Call(call));
	return a->ent;
}

576
/* Sets the call entity. */
577
void be_Call_set_entity(ir_node *call, ir_entity *ent) {
578
579
580
581
582
	be_call_attr_t *a = get_irn_attr(call);
	assert(be_is_Call(call));
	a->ent = ent;
}

583
584
/* Gets the call type. */
ir_type *be_Call_get_type(ir_node *call) {
Matthias Braun's avatar
Matthias Braun committed
585
	const be_call_attr_t *a = get_irn_attr_const(call);
586
587
588
589
590
591
592
593
594
595
596
	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
597
598
599
600
601
602
603
604
605
606
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;
}

607
/* Construct a new be_Return. */
608
ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *block, int n_res,
609
                       unsigned pop, int n, ir_node *in[])
Sebastian Hack's avatar
Sebastian Hack committed
610
{
611
	be_return_attr_t *a;
612
613
614
615
616
617
618
619
620
621
622
	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);
623
	a->num_ret_vals = n_res;
624
	a->pop          = pop;
625
	a->emit_pop     = 0;
Sebastian Hack's avatar
Sebastian Hack committed
626

627
	return res;
Sebastian Hack's avatar
Sebastian Hack committed
628
629
}

630
/* Returns the number of real returns values */
631
int be_Return_get_n_rets(const ir_node *ret) {
632
	const be_return_attr_t *a = get_irn_generic_attr_const(ret);
633
634
635
	return a->num_ret_vals;
}

636
637
/* return the number of bytes that should be popped from stack when executing the Return. */
unsigned be_Return_get_pop(const ir_node *ret) {
638
639
640
641
	const be_return_attr_t *a = get_irn_generic_attr_const(ret);
	return a->pop;
}

642
643
644
645
646
647
648
649
650
651
652
653
654
/* 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) {
655
656
657
658
659
660
661
662
	int pos;

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

	return pos;
}

663
664
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
665
{
Michael Beck's avatar
Michael Beck committed
666
	be_incsp_attr_t *a;
Sebastian Hack's avatar
Sebastian Hack committed
667
	ir_node *irn;
Sebastian Hack's avatar
Sebastian Hack committed
668
	ir_node *in[1];
Sebastian Hack's avatar
Sebastian Hack committed
669
670

	in[0]     = old_sp;
671
672
	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
673
	a         = init_node_attr(irn, 1);
Sebastian Hack's avatar
Sebastian Hack committed
674
	a->offset = offset;
675
	a->align  = align;
Sebastian Hack's avatar
Sebastian Hack committed
676
677

	/* Set output constraint to stack register. */
678
679
	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
680
681

	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
682
}
Sebastian Hack's avatar
Sebastian Hack committed
683

Sebastian Hack's avatar
Sebastian Hack committed
684
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
685
{
Sebastian Hack's avatar
Sebastian Hack committed
686
	be_node_attr_t *a;
Sebastian Hack's avatar
Sebastian Hack committed
687
	ir_node *irn;
Sebastian Hack's avatar
Sebastian Hack committed
688
	ir_node *in[be_pos_AddSP_last];
689
	const arch_register_class_t *cls;
Sebastian Hack's avatar
Sebastian Hack committed
690

Sebastian Hack's avatar
Sebastian Hack committed
691
692
	in[be_pos_AddSP_old_sp] = old_sp;
	in[be_pos_AddSP_size]   = sz;
Sebastian Hack's avatar
Sebastian Hack committed
693

694
695
	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
696

Sebastian Hack's avatar
Sebastian Hack committed
697
	/* Set output constraint to stack register. */
698
699
700
	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);
701

702
	cls = arch_register_get_class(sp);
Sebastian Hack's avatar
Sebastian Hack committed
703
704
705
706

	return irn;
}

Michael Beck's avatar
Michael Beck committed
707
708
709
710
711
712
713
714
715
716
717
718
719
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);

	/* Set output constraint to stack register. */
720
721
722
	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
723
724
725
726

	return irn;
}

727
ir_node *be_new_RegParams(ir_graph *irg, ir_node *bl, int n_outs)
Sebastian Hack's avatar
Sebastian Hack committed
728
{
729
730
	ir_node *res;
	int i;
Sebastian Hack's avatar
Sebastian Hack committed
731

732
733
734
735
736
737
738
739
	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;
}

740
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
741
{
742
	be_frame_attr_t *a;
Sebastian Hack's avatar
Sebastian Hack committed
743
744
745
	ir_node *irn;
	ir_node *in[1];

746
	in[0]  = frame;
747
	irn    = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
Sebastian Hack's avatar
Sebastian Hack committed
748
	a      = init_node_attr(irn, 1);
749
750
	a->ent = ent;
	a->offset = 0;
751
752
	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
753
754

	return optimize_node(irn);
Sebastian Hack's avatar
Sebastian Hack committed
755
756
}

Matthias Braun's avatar
Matthias Braun committed
757
ir_node *be_get_FrameAddr_frame(const ir_node *node) {
Michael Beck's avatar
Michael Beck committed
758
759
760
761
	assert(be_is_FrameAddr(node));
	return get_irn_n(node, be_pos_FrameAddr_ptr);
}

Matthias Braun's avatar
Matthias Braun committed
762
763
764
765
766
767
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
768
769
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)
{
770
771
	ir_node  *irn;
	ir_node **in = ALLOCAN(ir_node*, n + 1);
Sebastian Hack's avatar
Sebastian Hack committed
772
773
774
775

	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
776
	init_node_attr(irn, n + 1);
777
778
	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
779
780
781
782
783
784

	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
785
	return be_new_CopyKeep(cls, irg, bl, src, 1, &keep, mode);
Sebastian Hack's avatar
Sebastian Hack committed
786
787
}

788
789
790
791
792
793
794
795
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);
}

796
ir_node *be_new_Barrier(ir_graph *irg, ir_node *bl, int n, ir_node *in[])
Sebastian Hack's avatar
Sebastian Hack committed
797
{
798
799
	ir_node *res;
	int i;
Sebastian Hack's avatar
Sebastian Hack committed
800

801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
	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);
817

818
819
820
821
	ir_node *proj = new_r_Proj(irg, block, barrier, mode, n);
	add_register_req(barrier);

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

Michael Beck's avatar
Michael Beck committed
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
/* 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;
}

839
840
int be_has_frame_entity(const ir_node *irn)
{
841
	switch (get_irn_opcode(irn)) {
842
	case beo_Spill:
843
	case beo_Reload:
844
845
	case beo_FrameAddr:
		return 1;
846
847
	default:
		return 0;
848
849
850
	}
}

851
ir_entity *be_get_frame_entity(const ir_node *irn)
852
{
853
	if (be_has_frame_entity(irn)) {
Matthias Braun's avatar
Matthias Braun committed
854
		const be_frame_attr_t *a = get_irn_attr_const(irn);
855
856
857
858
		return a->ent;
	}
	return NULL;
}
Sebastian Hack's avatar
Sebastian Hack committed
859

860
861
862
863
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
864
		const be_frame_attr_t *a = get_irn_attr_const(irn);
865
866
867
868
869
		return a->offset;
	}
	return 0;
}

870
void be_set_MemPerm_in_entity(const ir_node *irn, int n, ir_entity *ent)
Matthias Braun's avatar
Matthias Braun committed
871
{
Matthias Braun's avatar
Matthias Braun committed
872
	const be_memperm_attr_t *attr = get_irn_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
873
874

	assert(be_is_MemPerm(irn));
875
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
876
877
878
879

	attr->in_entities[n] = ent;
}

880
ir_entity* be_get_MemPerm_in_entity(const ir_node* irn, int n)
Matthias Braun's avatar
Matthias Braun committed
881
{
Matthias Braun's avatar
Matthias Braun committed
882
	const be_memperm_attr_t *attr = get_irn_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
883
884

	assert(be_is_MemPerm(irn));
885
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
886
887
888
889

	return attr->in_entities[n];
}

890
void be_set_MemPerm_out_entity(const ir_node *irn, int n, ir_entity *ent)
Matthias Braun's avatar
Matthias Braun committed
891
{
Matthias Braun's avatar
Matthias Braun committed
892
	const be_memperm_attr_t *attr = get_irn_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
893
894

	assert(be_is_MemPerm(irn));
895
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
896
897
898
899

	attr->out_entities[n] = ent;
}

900
ir_entity* be_get_MemPerm_out_entity(const ir_node* irn, int n)
Matthias Braun's avatar
Matthias Braun committed
901
{
Matthias Braun's avatar
Matthias Braun committed
902
	const be_memperm_attr_t *attr = get_irn_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
903
904

	assert(be_is_MemPerm(irn));
905
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
906
907
908
909

	return attr->out_entities[n];
}

910
911
912
913
914
int be_get_MemPerm_entity_arity(const ir_node *irn)
{
	return get_irn_arity(irn) - 1;
}

915
916
static void set_req_single(struct obstack *obst, arch_register_req_t *req,
		const arch_register_t *reg, arch_register_req_type_t additional_types)
Sebastian Hack's avatar
Sebastian Hack committed
917
{
Matthias Braun's avatar
Matthias Braun committed
918
	const arch_register_class_t *cls = arch_register_get_class(reg);
919
	unsigned                    *limited_bitset;
Sebastian Hack's avatar
Sebastian Hack committed
920

Matthias Braun's avatar
Matthias Braun committed
921
922
	limited_bitset = rbitset_obstack_alloc(obst, arch_register_class_n_regs(cls));
	rbitset_set(limited_bitset, arch_register_get_index(reg));
Sebastian Hack's avatar
Sebastian Hack committed
923

Matthias Braun's avatar
Matthias Braun committed
924
	req->cls = cls;
925
	req->type |= arch_register_req_type_limited | additional_types;
Matthias Braun's avatar
Matthias Braun committed
926
	req->limited = limited_bitset;
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953

}

void be_set_constr_single_reg_in(ir_node *node, int pos,
		const arch_register_t *reg, arch_register_req_type_t additional_types)
{
	arch_register_req_t *req = get_req(node, pos);
	ir_graph *irg = get_irn_irg(node);
	struct obstack *obst = get_irg_obstack(irg);

	set_req_single(obst, req, reg, additional_types);
}

void be_set_constr_single_reg_out(ir_node *node, int pos,
		const arch_register_t *reg, arch_register_req_type_t additional_types)
{
	arch_register_req_t *req = get_req(node, BE_OUT_POS(pos));
	ir_graph *irg = get_irn_irg(node);
	struct obstack *obst = get_irg_obstack(irg);

	/* if we have an ignore register, add ignore flag and just assign it */
	if (reg->type & arch_register_type_ignore) {
		additional_types |= arch_register_req_type_ignore;
	}

	arch_irn_set_register(node, pos, reg);
	set_req_single(obst, req, reg, additional_types);
Sebastian Hack's avatar
Sebastian Hack committed
954
955
}

Matthias Braun's avatar
Matthias Braun committed
956
void be_set_constr_limited(ir_node *node, int pos, const arch_register_req_t *req)
957
{