benode.c 43.8 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
typedef struct {
	arch_register_req_t req;
67
	arch_register_req_t in_req;
68
69
} be_reg_data_t;

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	return a;
}
291

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

Michael Beck's avatar
Michael Beck committed
304
305
306
307
308
309
310
311
312
313
/**
 * 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
314
315
316
317
{
	const ir_node *n = *node;

	if(is_Proj(n)) {
318
319
320
321
322
323
324
		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
325
326
327
328
329
330
		return get_Proj_proj(n);
	}

	return 0;
}

331
332
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)
333
{
Matthias Braun's avatar
Matthias Braun committed
334
	be_frame_attr_t *a;
335
	ir_node         *in[2];
336
	ir_node         *res;
337

338
339
340
	in[0]     = frame;
	in[1]     = to_spill;
	res       = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
341
342
	a         = init_node_attr(res, 2);
	a->ent    = NULL;
Matthias Braun's avatar
Matthias Braun committed
343
	a->offset = 0;
Sebastian Hack's avatar
Sebastian Hack committed
344

345
346
	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
347
348
	return res;
}
349

350
351
352
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)
353
{
354
355
356
357
358
	ir_node *in[2];
	ir_node *res;

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

Sebastian Hack's avatar
Sebastian Hack committed
361
	init_node_attr(res, 2);
362
363
364
	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
365
366
	return res;
}
367

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

374
375
376
377
378
379
ir_node *be_get_Reload_frame(const ir_node *irn)
{
	assert(be_is_Reload(irn));
	return get_irn_n(irn, be_pos_Reload_frame);
}

380
381
382
383
384
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
385

386
387
388
389
390
ir_node *be_get_Spill_frame(const ir_node *irn)
{
	assert(be_is_Spill(irn));
	return get_irn_n(irn, be_pos_Spill_frame);
}
391

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

Sebastian Hack's avatar
Sebastian Hack committed
403
404
	return irn;
}
405

406
407
void be_Perm_reduce(ir_node *perm, int new_size, int *map)
{
408
409
410
411
412
	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);
413
	ir_node        **new_in;
414
415
416
417

	int i;

	assert(be_is_Perm(perm));
418
	assert(new_size <= arity);
419

420
	new_in = alloca(new_size * sizeof(*new_in));
421

422
	/* save the old register data */
423
	memcpy(old_data, attr->reg_data, arity * sizeof(old_data[0]));
424
	memcpy(old_infos, info->out_infos, arity * sizeof(old_infos[0]));
425
426

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

	set_irn_in(perm, new_size, new_in);
}

Matthias Braun's avatar
Matthias Braun committed
437
438
ir_node *be_new_MemPerm(const arch_env_t *arch_env, ir_graph *irg, ir_node *bl, int n, ir_node *in[])
{
439
440
441
442
443
444
445
	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
446

447
	real_in = ALLOCAN(ir_node*, n + 1);
448
449
450
451
452
453
	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);
454
	be_node_set_reg_class_in(irn, 0, sp->reg_class);
Michael Beck's avatar
Michael Beck committed
455
	for (i = 0; i < n; ++i) {
456
457
		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
458
459
460
461
	}

	attr = get_irn_attr(irn);

462
463
	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
464
465
466
467
468
469
470
	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
471
472
473
474
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
475
	arch_register_req_t *req;
476

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

483
	req = get_be_req(res, BE_OUT_POS(0));
Matthias Braun's avatar
Matthias Braun committed
484
485
	req->cls = cls;
	req->type = arch_register_req_type_should_be_same;
486
	req->other_same = 1U << 0;
Matthias Braun's avatar
Matthias Braun committed
487

Sebastian Hack's avatar
Sebastian Hack committed
488
	return res;
489
490
}

491
492
493
494
ir_node *be_get_Copy_op(const ir_node *cpy) {
	return get_irn_n(cpy, be_pos_Copy_op);
}

495
496
497
498
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
499
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
500
{
Sebastian Hack's avatar
Sebastian Hack committed
501
	int i;
502
503
504
505
	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
506

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

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

536
537
538
539
540
	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
541

542
	irn = new_ir_node(dbg, irg, bl, op_be_Call, mode_T, real_n, real_in);
543
544
545
	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
546
	a->pop     = 0;
Sebastian Hack's avatar
Sebastian Hack committed
547
	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
548
549
}

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

557
/* Sets the call entity. */
558
void be_Call_set_entity(ir_node *call, ir_entity *ent) {
559
560
561
562
563
	be_call_attr_t *a = get_irn_attr(call);
	assert(be_is_Call(call));
	a->ent = ent;
}

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

588
/* Construct a new be_Return. */
589
ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *block, int n_res,
590
                       unsigned pop, int n, ir_node *in[])
Sebastian Hack's avatar
Sebastian Hack committed
591
{
592
	be_return_attr_t *a;
593
594
595
596
597
598
599
600
601
602
603
	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);
604
	a->num_ret_vals = n_res;
605
	a->pop          = pop;
606
	a->emit_pop     = 0;
Sebastian Hack's avatar
Sebastian Hack committed
607

608
	return res;
Sebastian Hack's avatar
Sebastian Hack committed
609
610
}

611
/* Returns the number of real returns values */
612
int be_Return_get_n_rets(const ir_node *ret) {
613
	const be_return_attr_t *a = get_irn_generic_attr_const(ret);
614
615
616
	return a->num_ret_vals;
}

617
618
/* return the number of bytes that should be popped from stack when executing the Return. */
unsigned be_Return_get_pop(const ir_node *ret) {
619
620
621
622
	const be_return_attr_t *a = get_irn_generic_attr_const(ret);
	return a->pop;
}

623
624
625
626
627
628
629
630
631
632
633
634
635
/* 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) {
636
637
638
639
640
641
642
643
	int pos;

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

	return pos;
}

644
645
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
646
{
Michael Beck's avatar
Michael Beck committed
647
	be_incsp_attr_t *a;
Sebastian Hack's avatar
Sebastian Hack committed
648
	ir_node *irn;
Sebastian Hack's avatar
Sebastian Hack committed
649
	ir_node *in[1];
Sebastian Hack's avatar
Sebastian Hack committed
650
651

	in[0]     = old_sp;
652
653
	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
654
	a         = init_node_attr(irn, 1);
Sebastian Hack's avatar
Sebastian Hack committed
655
	a->offset = offset;
656
	a->align  = align;
Sebastian Hack's avatar
Sebastian Hack committed
657
658

	/* Set output constraint to stack register. */
659
660
	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
661
662

	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
663
}
Sebastian Hack's avatar
Sebastian Hack committed
664

Sebastian Hack's avatar
Sebastian Hack committed
665
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
666
{
Sebastian Hack's avatar
Sebastian Hack committed
667
	be_node_attr_t *a;
Sebastian Hack's avatar
Sebastian Hack committed
668
	ir_node *irn;
Sebastian Hack's avatar
Sebastian Hack committed
669
	ir_node *in[be_pos_AddSP_last];
670
	const arch_register_class_t *cls;
Sebastian Hack's avatar
Sebastian Hack committed
671

Sebastian Hack's avatar
Sebastian Hack committed
672
673
	in[be_pos_AddSP_old_sp] = old_sp;
	in[be_pos_AddSP_size]   = sz;
Sebastian Hack's avatar
Sebastian Hack committed
674

675
676
	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
677

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

683
	cls = arch_register_get_class(sp);
Sebastian Hack's avatar
Sebastian Hack committed
684
685
686
687

	return irn;
}

Michael Beck's avatar
Michael Beck committed
688
689
690
691
692
693
694
695
696
697
698
699
700
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. */
701
702
703
	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
704
705
706
707

	return irn;
}

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

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

	return res;
}

721
ir_node *be_new_FrameAddr(const arch_register_class_t *cls_frame, ir_graph *irg, ir_node *bl, ir_node *frame, ir_entity *ent)
Sebastian Hack's avatar
Sebastian Hack committed
722
{
723
	be_frame_attr_t *a;
Sebastian Hack's avatar
Sebastian Hack committed
724
725
726
	ir_node *irn;
	ir_node *in[1];

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

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

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

Matthias Braun's avatar
Matthias Braun committed
743
744
745
746
747
748
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
749
750
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)
{
751
752
	ir_node  *irn;
	ir_node **in = ALLOCAN(ir_node*, n + 1);
Sebastian Hack's avatar
Sebastian Hack committed
753
754
755
756

	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
757
	init_node_attr(irn, n + 1);
758
759
	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
760
761
762
763
764
765

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

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

777
ir_node *be_new_Barrier(ir_graph *irg, ir_node *bl, int n, ir_node *in[])
Sebastian Hack's avatar
Sebastian Hack committed
778
{
779
780
	ir_node *res;
	int i;
Sebastian Hack's avatar
Sebastian Hack committed
781

782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
	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);
798

799
800
801
802
	ir_node *proj = new_r_Proj(irg, block, barrier, mode, n);
	add_register_req(barrier);

	return proj;
Sebastian Hack's avatar
Sebastian Hack committed
803
804
}

Michael Beck's avatar
Michael Beck committed
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
/* 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;
}

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

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

841
842
843
844
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
845
		const be_frame_attr_t *a = get_irn_attr_const(irn);
846
847
848
849
850
		return a->offset;
	}
	return 0;
}

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

	assert(be_is_MemPerm(irn));
856
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
857
858
859
860

	attr->in_entities[n] = ent;
}

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

	assert(be_is_MemPerm(irn));
866
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
867
868
869
870

	return attr->in_entities[n];
}

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

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

	attr->out_entities[n] = ent;
}

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

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

	return attr->out_entities[n];
}

891
892
893
894
895
int be_get_MemPerm_entity_arity(const ir_node *irn)
{
	return get_irn_arity(irn) - 1;
}

896
897
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
898
{
Matthias Braun's avatar
Matthias Braun committed
899
	const arch_register_class_t *cls = arch_register_get_class(reg);
900
	unsigned                    *limited_bitset;
Sebastian Hack's avatar
Sebastian Hack committed
901

Matthias Braun's avatar
Matthias Braun committed
902
903
	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
904

Matthias Braun's avatar
Matthias Braun committed
905
	req->cls = cls;
906
	req->type |= arch_register_req_type_limited | additional_types;
Matthias Braun's avatar
Matthias Braun committed
907
	req->limited = limited_bitset;
908
909
910
911
912
913

}

void be_set_constr_single_reg_in(ir_node *node, int pos,
		const arch_register_t *reg, arch_register_req_type_t additional_types)
{
914
	arch_register_req_t *req = get_be_req(node, pos);
915
916
917
918
919
920
921
922
923
	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)
{
924
	arch_register_req_t *req = get_be_req(node, BE_OUT_POS(pos));
925
926
927
928
929
930
931
932
933
934
	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
935
936
}

Matthias Braun's avatar
Matthias Braun committed
937
void be_set_constr_