benode.c 44.6 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"

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

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

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

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

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

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

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

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

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

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

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

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

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

151
152
153
154
155
156
157
158
159
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;
}
160

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

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

172
173
174
	len = ARR_LEN(a->reg_data);
	for (i = 0; i < len; ++i) {
		if (a->reg_data[i].reg != b->reg_data[i].reg ||
175
176
				!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))
177
			return 1;
Michael Beck's avatar
Michael Beck committed
178
	}
179
180

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

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

	return _node_cmp_attr(a_attr, b_attr);
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

298
299
	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
300
		memset(a->reg_data, 0, max_reg_data * sizeof(a->reg_data[0]));
301
302
	} else {
		a->reg_data = NEW_ARR_F(be_reg_data_t, 0);
Sebastian Hack's avatar
Sebastian Hack committed
303
304
305
306
	}

	return a;
}
307

308
309
310
311
312
313
314
315
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
316
317
318
319
320
321
322
323
324
325
/**
 * 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
326
327
328
329
{
	const ir_node *n = *node;

	if(is_Proj(n)) {
330
331
332
333
334
335
336
		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
337
338
339
340
341
342
		return get_Proj_proj(n);
	}

	return 0;
}

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

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

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

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

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

367
368
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)
369
{
Matthias Braun's avatar
Matthias Braun committed
370
	be_frame_attr_t *a;
371
	ir_node         *in[2];
372
	ir_node         *res;
373

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

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

386
387
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)
388
{
389
390
391
392
393
394
395
	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
396
397
	init_node_attr(res, 2);
	be_node_set_reg_class(res, -1, cls);
398
	be_node_set_reg_class(res, be_pos_Reload_frame, cls_frame);
399
	be_node_set_flags(res, -1, arch_irn_flags_rematerializable);
Sebastian Hack's avatar
Sebastian Hack committed
400
401
	return res;
}
402

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

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

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

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

Sebastian Hack's avatar
Sebastian Hack committed
427
428
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
429
	int i;
430
	ir_node *irn = new_ir_node(NULL, irg, bl, op_be_Perm, mode_T, n, in);
Sebastian Hack's avatar
Sebastian Hack committed
431
432
433
434
435
436
	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
437
438
	return irn;
}
439

440
441
void be_Perm_reduce(ir_node *perm, int new_size, int *map)
{
442
	int            arity     = get_irn_arity(perm);
443
	be_reg_data_t  *old_data = ALLOCAN(be_reg_data_t, arity);
444
445
	be_node_attr_t *attr     = get_irn_attr(perm);
	ir_node        **new_in;
446
447
448
449

	int i;

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

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

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

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

	set_irn_in(perm, new_size, new_in);
}

Matthias Braun's avatar
Matthias Braun committed
467
468
ir_node *be_new_MemPerm(const arch_env_t *arch_env, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
{
469
470
471
472
473
474
475
	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
476

477
	real_in = ALLOCAN(ir_node*, n + 1);
478
479
480
481
482
483
484
	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
485
	for (i = 0; i < n; ++i) {
486
		be_node_set_reg_class(irn, i + 1, cls_frame);
Matthias Braun's avatar
Matthias Braun committed
487
488
489
490
491
		be_node_set_reg_class(irn, OUT_POS(i), cls_frame);
	}

	attr = get_irn_attr(irn);

492
493
	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
494
495
496
497
498
499
500
	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
501
502
503
504
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
505
	arch_register_req_t *req;
506

Sebastian Hack's avatar
Sebastian Hack committed
507
	in[0] = op;
508
	res   = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
Sebastian Hack's avatar
Sebastian Hack committed
509
510
511
	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
512
513
514
515

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

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

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

525
526
527
528
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
529
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
530
{
Sebastian Hack's avatar
Sebastian Hack committed
531
	int i;
532
533
534
535
	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
536

Sebastian Hack's avatar
Sebastian Hack committed
537
	for(i = 0; i < n; ++i) {
538
539
540
		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
541
	}
542
543
544
545
546
547
548
549
550
551
552
553
554
	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
555
556
}

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

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

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

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

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

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

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

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

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

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

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

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

	return pos;
}

674
675
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
676
{
Michael Beck's avatar
Michael Beck committed
677
	be_incsp_attr_t *a;
Sebastian Hack's avatar
Sebastian Hack committed
678
	ir_node *irn;
Sebastian Hack's avatar
Sebastian Hack committed
679
	ir_node *in[1];
Sebastian Hack's avatar
Sebastian Hack committed
680
681

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

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

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

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

Sebastian Hack's avatar
Sebastian Hack committed
698
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
699
{
Sebastian Hack's avatar
Sebastian Hack committed
700
	be_node_attr_t *a;
Sebastian Hack's avatar
Sebastian Hack committed
701
	ir_node *irn;
Sebastian Hack's avatar
Sebastian Hack committed
702
	ir_node *in[be_pos_AddSP_last];
703
	const arch_register_class_t *cls;
Sebastian Hack's avatar
Sebastian Hack committed
704

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

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

711
712
	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
713
714

	/* Set output constraint to stack register. */
715
716
	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));
717
718
719
	be_set_constr_single_reg(irn, OUT_POS(pn_be_AddSP_sp), sp);
	a->reg_data[pn_be_AddSP_sp].reg = sp;

720
721
	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
722
723
724
725

	return irn;
}

Michael Beck's avatar
Michael Beck committed
726
727
728
729
730
731
732
733
734
735
736
737
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);

738
739
	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
740
741
742
743

	/* 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));
744
745
	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
746
747
748
749

	return irn;
}

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

755
756
757
758
759
760
761
762
	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;
}

763
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
764
{
765
	be_frame_attr_t *a;
Sebastian Hack's avatar
Sebastian Hack committed
766
767
768
	ir_node *irn;
	ir_node *in[1];

769
	in[0]  = frame;
770
	irn    = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
Sebastian Hack's avatar
Sebastian Hack committed
771
	a      = init_node_attr(irn, 1);
772
773
774
	a->ent = ent;
	a->offset = 0;
	be_node_set_reg_class(irn, 0, cls_frame);
Sebastian Hack's avatar
Sebastian Hack committed
775
	be_node_set_reg_class(irn, OUT_POS(0), cls_frame);
Michael Beck's avatar
Michael Beck committed
776
777

	return optimize_node(irn);
Sebastian Hack's avatar
Sebastian Hack committed
778
779
}

Matthias Braun's avatar
Matthias Braun committed
780
ir_node *be_get_FrameAddr_frame(const ir_node *node) {
Michael Beck's avatar
Michael Beck committed
781
782
783
784
	assert(be_is_FrameAddr(node));
	return get_irn_n(node, be_pos_FrameAddr_ptr);
}

Matthias Braun's avatar
Matthias Braun committed
785
786
787
788
789
790
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
791
792
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)
{
793
794
	ir_node  *irn;
	ir_node **in = ALLOCAN(ir_node*, n + 1);
Sebastian Hack's avatar
Sebastian Hack committed
795
796
797
798

	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
799
	init_node_attr(irn, n + 1);
Sebastian Hack's avatar
Sebastian Hack committed
800
801
802
803
804
805
806
807
	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
808
	return be_new_CopyKeep(cls, irg, bl, src, 1, &keep, mode);
Sebastian Hack's avatar
Sebastian Hack committed
809
810
}

811
812
813
814
815
816
817
818
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);
}

819
ir_node *be_new_Barrier(ir_graph *irg, ir_node *bl, int n, ir_node *in[])
Sebastian Hack's avatar
Sebastian Hack committed
820
{
821
822
	ir_node *res;
	int i;
Sebastian Hack's avatar
Sebastian Hack committed
823

824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
	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);
840

841
842
843
844
	ir_node *proj = new_r_Proj(irg, block, barrier, mode, n);
	add_register_req(barrier);

	return proj;
Sebastian Hack's avatar
Sebastian Hack committed
845
846
}

Michael Beck's avatar
Michael Beck committed
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
/* 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;
}

862
863
int be_has_frame_entity(const ir_node *irn)
{
864
	switch (get_irn_opcode(irn)) {
865
	case beo_Spill:
866
	case beo_Reload:
867
868
	case beo_FrameAddr:
		return 1;
869
870
	default:
		return 0;
871
872
873
	}
}

874
ir_entity *be_get_frame_entity(const ir_node *irn)
875
{
876
	if (be_has_frame_entity(irn)) {
Matthias Braun's avatar
Matthias Braun committed
877
		const be_frame_attr_t *a = get_irn_attr_const(irn);
878
879
880
881
		return a->ent;
	}
	return NULL;
}
Sebastian Hack's avatar
Sebastian Hack committed
882

883
884
885
886
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
887
		const be_frame_attr_t *a = get_irn_attr_const(irn);
888
889
890
891
892
		return a->offset;
	}
	return 0;
}

893
void be_set_MemPerm_in_entity(const ir_node *irn, int n, ir_entity *ent)
Matthias Braun's avatar
Matthias Braun committed
894
{
Matthias Braun's avatar
Matthias Braun committed
895
	const be_memperm_attr_t *attr = get_irn_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
896
897

	assert(be_is_MemPerm(irn));
898
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
899
900
901
902

	attr->in_entities[n] = ent;
}

903
ir_entity* be_get_MemPerm_in_entity(const ir_node* irn, int n)
Matthias Braun's avatar
Matthias Braun committed
904
{
Matthias Braun's avatar
Matthias Braun committed
905
	const be_memperm_attr_t *attr = get_irn_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
906
907

	assert(be_is_MemPerm(irn));
908
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
909
910
911
912

	return attr->in_entities[n];
}

913
void be_set_MemPerm_out_entity(const ir_node *irn, int n, ir_entity *ent)
Matthias Braun's avatar
Matthias Braun committed
914
{
Matthias Braun's avatar
Matthias Braun committed
915
	const be_memperm_attr_t *attr = get_irn_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
916
917

	assert(be_is_MemPerm(irn));
918
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
919
920
921
922

	attr->out_entities[n] = ent;
}

923
ir_entity* be_get_MemPerm_out_entity(const ir_node* irn, int n)
Matthias Braun's avatar
Matthias Braun committed
924
{
Matthias Braun's avatar
Matthias Braun committed
925
	const be_memperm_attr_t *attr = get_irn_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
926
927

	assert(be_is_MemPerm(irn));
928
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
929
930
931
932

	return attr->out_entities[n];
}

933
934
935
936
937
int be_get_MemPerm_entity_arity(const ir_node *irn)
{
	return get_irn_arity(irn) - 1;
}

Matthias Braun's avatar
Matthias Braun committed
938
void be_set_constr_single_reg(ir_node *node, int pos, const arch_register_t *reg)
Sebastian Hack's avatar
Sebastian Hack committed
939
{
Matthias Braun's avatar
Matthias Braun committed
940
941
942
943
944
	arch_register_req_t *req = get_req(node, pos);
	const arch_register_class_t *cls = arch_register_get_class(reg);
	ir_graph *irg = get_irn_irg(node);
	struct obstack *obst = get_irg_obstack(irg);
	unsigned *limited_bitset;
Sebastian Hack's avatar
Sebastian Hack committed
945

Matthias Braun's avatar
Matthias Braun committed
946
947
948
	assert(req->cls == NULL || req->cls == cls);
	assert(! (req->type & arch_register_req_type_limited));
	assert(req->limited == NULL);
Sebastian Hack's avatar
Sebastian Hack committed
949

Matthias Braun's avatar
Matthias Braun committed
950
951
	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
952

Matthias Braun's avatar
Matthias Braun committed