benode.c 38.4 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 "bitfiddle.h"
Matthias Braun's avatar
Matthias Braun committed
41
#include "raw_bitset.h"
42
#include "error.h"
43
#include "array_t.h"
Sebastian Hack's avatar
Sebastian Hack committed
44
45
46
47

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

53
54
#include "be_t.h"
#include "belive_t.h"
55
#include "besched.h"
56
#include "benode.h"
57
#include "bearch.h"
Sebastian Hack's avatar
Sebastian Hack committed
58

59
60
#include "beirgmod.h"

Sebastian Hack's avatar
Sebastian Hack committed
61
typedef struct {
62
	const arch_register_req_t *in_req;
63
64
} be_reg_data_t;

65
/** The generic be nodes attribute type. */
66
typedef struct {
Michael Beck's avatar
Michael Beck committed
67
	be_reg_data_t *reg_data;
68
69
} be_node_attr_t;

70
71
/** The be_Return nodes attribute type. */
typedef struct {
Matthias Braun's avatar
Matthias Braun committed
72
73
74
75
	be_node_attr_t node_attr;    /**< base attributes of every be node. */
	int            num_ret_vals; /**< number of return values */
	unsigned       pop;          /**< number of bytes that should be popped */
	int            emit_pop;     /**< if set, emit pop bytes, even if pop = 0 */
76
77
} be_return_attr_t;

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

87
/** The be_Frame attribute type. */
88
typedef struct {
89
90
91
	be_node_attr_t  node_attr;   /**< base attributes of every be node. */
	ir_entity      *ent;
	int             offset;
92
93
} be_frame_attr_t;

94
/** The be_Call attribute type. */
95
typedef struct {
96
	be_node_attr_t  node_attr;  /**< base attributes of every be node. */
Matthias Braun's avatar
Matthias Braun committed
97
	ir_entity      *ent;        /**< called entity if this is a static call. */
98
	unsigned        pop;
Matthias Braun's avatar
Matthias Braun committed
99
	ir_type        *call_tp;    /**< call type, copied from the original Call */
100
101
} be_call_attr_t;

102
typedef struct {
103
104
105
	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
106
} be_memperm_attr_t;
107

108
109
110
ir_op *op_be_Spill;
ir_op *op_be_Reload;
ir_op *op_be_Perm;
Matthias Braun's avatar
Matthias Braun committed
111
ir_op *op_be_MemPerm;
112
113
ir_op *op_be_Copy;
ir_op *op_be_Keep;
Sebastian Hack's avatar
Sebastian Hack committed
114
ir_op *op_be_CopyKeep;
115
116
117
ir_op *op_be_Call;
ir_op *op_be_Return;
ir_op *op_be_IncSP;
Sebastian Hack's avatar
Sebastian Hack committed
118
ir_op *op_be_AddSP;
Michael Beck's avatar
Michael Beck committed
119
ir_op *op_be_SubSP;
120
ir_op *op_be_Start;
121
ir_op *op_be_FrameAddr;
122
ir_op *op_be_Barrier;
Sebastian Hack's avatar
Sebastian Hack committed
123

Sebastian Hack's avatar
Sebastian Hack committed
124
static const ir_op_ops be_node_op_ops;
Sebastian Hack's avatar
Sebastian Hack committed
125

Michael Beck's avatar
Michael Beck committed
126
/**
Michael Beck's avatar
Michael Beck committed
127
 * Compare two be node attributes.
Michael Beck's avatar
Michael Beck committed
128
129
130
 *
 * @return zero if both attributes are identically
 */
131
132
static int node_cmp_attr(ir_node *a, ir_node *b)
{
Matthias Braun's avatar
Matthias Braun committed
133
134
	const be_node_attr_t *a_attr = get_irn_generic_attr_const(a);
	const be_node_attr_t *b_attr = get_irn_generic_attr_const(b);
135
	int i, len = ARR_LEN(a_attr->reg_data);
136

137
138
139
140
	if (len != ARR_LEN(b_attr->reg_data))
		return 1;

	if (!be_nodes_equal(a, b))
141
		return 1;
Michael Beck's avatar
Michael Beck committed
142

143
	for (i = len - 1; i >= 0; --i) {
144
145
		if (!reg_reqs_equal(a_attr->reg_data[i].in_req,
		                    b_attr->reg_data[i].in_req))
146
			return 1;
Michael Beck's avatar
Michael Beck committed
147
	}
148
149

	return 0;
Michael Beck's avatar
Michael Beck committed
150
151
152
}

/**
Michael Beck's avatar
Michael Beck committed
153
 * Compare the attributes of two be_FrameAddr nodes.
Michael Beck's avatar
Michael Beck committed
154
 *
Michael Beck's avatar
Michael Beck committed
155
 * @return zero if both nodes have identically attributes
Michael Beck's avatar
Michael Beck committed
156
 */
157
158
static int FrameAddr_cmp_attr(ir_node *a, ir_node *b)
{
Matthias Braun's avatar
Matthias Braun committed
159
160
	const be_frame_attr_t *a_attr = get_irn_generic_attr_const(a);
	const be_frame_attr_t *b_attr = get_irn_generic_attr_const(b);
Michael Beck's avatar
Michael Beck committed
161

162
163
164
	if (a_attr->ent != b_attr->ent || a_attr->offset != b_attr->offset)
		return 1;

165
	return node_cmp_attr(a, b);
166
167
}

Michael Beck's avatar
Michael Beck committed
168
169
170
171
172
/**
 * Compare the attributes of two be_Return nodes.
 *
 * @return zero if both nodes have identically attributes
 */
173
174
static int Return_cmp_attr(ir_node *a, ir_node *b)
{
Matthias Braun's avatar
Matthias Braun committed
175
176
	const be_return_attr_t *a_attr = get_irn_generic_attr_const(a);
	const be_return_attr_t *b_attr = get_irn_generic_attr_const(b);
177
178
179

	if (a_attr->num_ret_vals != b_attr->num_ret_vals)
		return 1;
180
181
	if (a_attr->pop != b_attr->pop)
		return 1;
182
183
	if (a_attr->emit_pop != b_attr->emit_pop)
		return 1;
184

185
	return node_cmp_attr(a, b);
186
187
}

Michael Beck's avatar
Michael Beck committed
188
189
190
191
192
/**
 * Compare the attributes of two be_IncSP nodes.
 *
 * @return zero if both nodes have identically attributes
 */
193
194
static int IncSP_cmp_attr(ir_node *a, ir_node *b)
{
Matthias Braun's avatar
Matthias Braun committed
195
196
	const be_incsp_attr_t *a_attr = get_irn_generic_attr_const(a);
	const be_incsp_attr_t *b_attr = get_irn_generic_attr_const(b);
197
198
199
200

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

201
	return node_cmp_attr(a, b);
202
203
}

Michael Beck's avatar
Michael Beck committed
204
205
206
207
208
/**
 * Compare the attributes of two be_Call nodes.
 *
 * @return zero if both nodes have identically attributes
 */
209
210
static int Call_cmp_attr(ir_node *a, ir_node *b)
{
Matthias Braun's avatar
Matthias Braun committed
211
212
	const be_call_attr_t *a_attr = get_irn_generic_attr_const(a);
	const be_call_attr_t *b_attr = get_irn_generic_attr_const(b);
213
214

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

218
	return node_cmp_attr(a, b);
Michael Beck's avatar
Michael Beck committed
219
220
}

221
static arch_register_req_t *allocate_reg_req(const ir_node *node)
Matthias Braun's avatar
Matthias Braun committed
222
{
223
	ir_graph       *irg  = get_irn_irg(node);
224
	struct obstack *obst = be_get_be_obst(irg);
Matthias Braun's avatar
Matthias Braun committed
225

226
227
228
229
	arch_register_req_t *req = obstack_alloc(obst, sizeof(*req));
	memset(req, 0, sizeof(*req));
	return req;
}
Matthias Braun's avatar
Matthias Braun committed
230

231
232
void be_set_constr_in(ir_node *node, int pos, const arch_register_req_t *req)
{
Matthias Braun's avatar
Matthias Braun committed
233
	const be_node_attr_t *attr = get_irn_generic_attr_const(node);
234
235
236
237
	be_reg_data_t *rd = &attr->reg_data[pos];
	assert(pos < ARR_LEN(attr->reg_data));
	rd->in_req = req;
}
Matthias Braun's avatar
Matthias Braun committed
238

239
240
241
242
void be_set_constr_out(ir_node *node, int pos, const arch_register_req_t *req)
{
	backend_info_t *info = be_get_info(node);
	info->out_infos[pos].req = req;
Matthias Braun's avatar
Matthias Braun committed
243
244
}

245
/**
246
 * Initializes the generic attribute of all be nodes and return it.
247
 */
248
static void *init_node_attr(ir_node *node, int n_inputs, int n_outputs)
Sebastian Hack's avatar
Sebastian Hack committed
249
{
250
	ir_graph       *irg  = get_irn_irg(node);
251
	struct obstack *obst = be_get_be_obst(irg);
Matthias Braun's avatar
Matthias Braun committed
252
	be_node_attr_t *a    = get_irn_generic_attr(node);
253
	backend_info_t *info = be_get_info(node);
Sebastian Hack's avatar
Sebastian Hack committed
254

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

257
258
259
260
261
262
263
264
265
	if (n_inputs >= 0) {
		int i;
		a->reg_data = NEW_ARR_D(be_reg_data_t, obst, n_inputs);
		for (i = 0; i < n_inputs; ++i) {
			a->reg_data[i].in_req = arch_no_register_req;
		}
	} else {
		a->reg_data = NEW_ARR_F(be_reg_data_t, 0);
	}
266

267
268
269
270
271
272
273
	if (n_outputs >= 0) {
		int i;
		info->out_infos = NEW_ARR_D(reg_out_info_t, obst, n_outputs);
		memset(info->out_infos, 0, n_outputs * sizeof(info->out_infos[0]));
		for (i = 0; i < n_outputs; ++i) {
			info->out_infos[i].req = arch_no_register_req;
		}
274
	} else {
275
		info->out_infos = NEW_ARR_F(reg_out_info_t, 0);
Sebastian Hack's avatar
Sebastian Hack committed
276
277
278
279
	}

	return a;
}
280

281
static void add_register_req_out(ir_node *node)
282
{
283
	backend_info_t *info = be_get_info(node);
284
285
286
287
288
289
290
291
	reg_out_info_t  out_info;
	memset(&out_info, 0, sizeof(out_info));
	out_info.req = arch_no_register_req;
	ARR_APP1(reg_out_info_t, info->out_infos, out_info);
}

static void add_register_req_in(ir_node *node)
{
Matthias Braun's avatar
Matthias Braun committed
292
	be_node_attr_t *a    = get_irn_generic_attr(node);
293
	be_reg_data_t   regreq;
294
	memset(&regreq, 0, sizeof(regreq));
295
	regreq.in_req = arch_no_register_req;
296
297
298
	ARR_APP1(be_reg_data_t, a->reg_data, regreq);
}

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

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

315
316
	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);
317
318
	/*
	 * For spills and reloads, we return "none" as requirement for frame
319
	 * pointer, so every input is ok. Some backends need this (STA).
320
321
322
	 * Matze: we should investigate if this is really needed, this solution
	 *        looks very hacky to me
	 */
323
	be_set_constr_in(res, be_pos_Spill_frame, arch_no_register_req);
324

325
326
	arch_set_out_register_req(res, 0, arch_no_register_req);

Sebastian Hack's avatar
Sebastian Hack committed
327
328
	return res;
}
329

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

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

342
	init_node_attr(res, 2, 1);
343
	be_node_set_reg_class_out(res, 0, cls);
344

345
346
	be_node_set_reg_class_in(res, be_pos_Reload_frame, cls_frame);
	arch_irn_set_flags(res, arch_irn_flags_rematerializable);
347
348
349
350
351
352
353

	/*
	 * For spills and reloads, we return "none" as requirement for frame
	 * pointer, so every input is ok. Some backends need this (e.g. STA).
	 * Matze: we should investigate if this is really needed, this solution
	 *        looks very hacky to me
	 */
354
	be_set_constr_in(res, be_pos_Reload_frame, arch_no_register_req);
355

Sebastian Hack's avatar
Sebastian Hack committed
356
357
	return res;
}
358

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

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

371
372
373
374
375
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
376

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

383
384
ir_node *be_new_Perm(const arch_register_class_t *cls, ir_node *block,
                     int n, ir_node *in[])
Sebastian Hack's avatar
Sebastian Hack committed
385
{
386
387
388
	int      i;
	ir_graph *irg = get_Block_irg(block);

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

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

399
400
void be_Perm_reduce(ir_node *perm, int new_size, int *map)
{
401
402
403
	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);
Matthias Braun's avatar
Matthias Braun committed
404
	be_node_attr_t *attr      = get_irn_generic_attr(perm);
405
	backend_info_t *info      = be_get_info(perm);
406
	ir_node        **new_in;
407
408
409
410

	int i;

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

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

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

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

	set_irn_in(perm, new_size, new_in);
}

430
ir_node *be_new_MemPerm(ir_node *block, int n, ir_node *in[])
Matthias Braun's avatar
Matthias Braun committed
431
{
432
433
	ir_graph                     *irg       = get_Block_irg(block);
	const arch_env_t             *arch_env  = be_get_irg_arch_env(irg);
434
435
436
437
438
	ir_node                      *frame     = get_irg_frame(irg);
	const arch_register_t        *sp        = arch_env->sp;
	ir_node                      *irn;
	be_memperm_attr_t            *attr;
	ir_node                     **real_in;
Matthias Braun's avatar
Matthias Braun committed
439

440
	real_in = ALLOCAN(ir_node*, n + 1);
441
442
443
	real_in[0] = frame;
	memcpy(&real_in[1], in, n * sizeof(real_in[0]));

444
	irn = new_ir_node(NULL, irg, block, op_be_MemPerm, mode_T, n+1, real_in);
445

446
	init_node_attr(irn, n + 1, n);
447
	be_node_set_reg_class_in(irn, 0, sp->reg_class);
Matthias Braun's avatar
Matthias Braun committed
448

Matthias Braun's avatar
Matthias Braun committed
449
	attr               = get_irn_generic_attr(irn);
450
451
	attr->in_entities  = OALLOCNZ(irg->obst, ir_entity*, n);
	attr->out_entities = OALLOCNZ(irg->obst, ir_entity*, n);
Matthias Braun's avatar
Matthias Braun committed
452
453
454
455

	return irn;
}

456
ir_node *be_new_Copy(const arch_register_class_t *cls, ir_node *bl, ir_node *op)
Sebastian Hack's avatar
Sebastian Hack committed
457
458
459
{
	ir_node *in[1];
	ir_node *res;
Matthias Braun's avatar
Matthias Braun committed
460
	arch_register_req_t *req;
461
	ir_graph *irg = get_Block_irg(bl);
462

Sebastian Hack's avatar
Sebastian Hack committed
463
	in[0] = op;
464
	res   = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
465
	init_node_attr(res, 1, 1);
466
467
	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
468

469
470
471
	req = allocate_reg_req(res);
	req->cls        = cls;
	req->type       = arch_register_req_type_should_be_same;
472
	req->other_same = 1U << 0;
473
	req->width      = 1;
474
	be_set_constr_out(res, 0, req);
Matthias Braun's avatar
Matthias Braun committed
475

Sebastian Hack's avatar
Sebastian Hack committed
476
	return res;
477
478
}

479
480
ir_node *be_get_Copy_op(const ir_node *cpy)
{
481
482
483
	return get_irn_n(cpy, be_pos_Copy_op);
}

484
485
void be_set_Copy_op(ir_node *cpy, ir_node *op)
{
486
487
488
	set_irn_n(cpy, be_pos_Copy_op, op);
}

489
ir_node *be_new_Keep(ir_node *block, int n, ir_node *in[])
Sebastian Hack's avatar
Sebastian Hack committed
490
{
Sebastian Hack's avatar
Sebastian Hack committed
491
	int i;
492
	ir_node *res;
493
	ir_graph *irg = get_Block_irg(block);
494

495
496
	res = new_ir_node(NULL, irg, block, op_be_Keep, mode_ANY, -1, NULL);
	init_node_attr(res, -1, 1);
Sebastian Hack's avatar
Sebastian Hack committed
497

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

517
518
519
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
520
{
521
522
	be_call_attr_t *a;
	int real_n = be_pos_Call_first_arg + n;
Sebastian Hack's avatar
Sebastian Hack committed
523
524
	ir_node *irn;
	ir_node **real_in;
Sebastian Hack's avatar
Sebastian Hack committed
525

526
527
528
529
530
	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
531

532
	irn = new_ir_node(dbg, irg, bl, op_be_Call, mode_T, real_n, real_in);
533
	a = init_node_attr(irn, real_n, n_outs);
534
535
	a->ent     = NULL;
	a->call_tp = call_tp;
Matthias Braun's avatar
Matthias Braun committed
536
	a->pop     = 0;
Sebastian Hack's avatar
Sebastian Hack committed
537
	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
538
539
}

540
541
ir_entity *be_Call_get_entity(const ir_node *call)
{
Matthias Braun's avatar
Matthias Braun committed
542
	const be_call_attr_t *a = get_irn_generic_attr_const(call);
543
544
545
546
	assert(be_is_Call(call));
	return a->ent;
}

547
548
void be_Call_set_entity(ir_node *call, ir_entity *ent)
{
Matthias Braun's avatar
Matthias Braun committed
549
	be_call_attr_t *a = get_irn_generic_attr(call);
550
551
552
553
	assert(be_is_Call(call));
	a->ent = ent;
}

554
555
ir_type *be_Call_get_type(ir_node *call)
{
Matthias Braun's avatar
Matthias Braun committed
556
	const be_call_attr_t *a = get_irn_generic_attr_const(call);
557
558
559
560
	assert(be_is_Call(call));
	return a->call_tp;
}

561
562
void be_Call_set_type(ir_node *call, ir_type *call_tp)
{
Matthias Braun's avatar
Matthias Braun committed
563
	be_call_attr_t *a = get_irn_generic_attr(call);
564
565
566
567
	assert(be_is_Call(call));
	a->call_tp = call_tp;
}

568
569
void be_Call_set_pop(ir_node *call, unsigned pop)
{
Matthias Braun's avatar
Matthias Braun committed
570
	be_call_attr_t *a = get_irn_generic_attr(call);
Matthias Braun's avatar
Matthias Braun committed
571
572
573
	a->pop = pop;
}

574
575
unsigned be_Call_get_pop(const ir_node *call)
{
Matthias Braun's avatar
Matthias Braun committed
576
	const be_call_attr_t *a = get_irn_generic_attr_const(call);
Matthias Braun's avatar
Matthias Braun committed
577
578
579
	return a->pop;
}

580
ir_node *be_new_Return(dbg_info *dbg, ir_graph *irg, ir_node *block, int n_res,
581
                       unsigned pop, int n, ir_node *in[])
Sebastian Hack's avatar
Sebastian Hack committed
582
{
583
	be_return_attr_t *a;
584
585
586
587
	ir_node *res;
	int i;

	res = new_ir_node(dbg, irg, block, op_be_Return, mode_X, -1, NULL);
588
589
	init_node_attr(res, -1, 1);
	for (i = 0; i < n; ++i) {
590
		add_irn_n(res, in[i]);
591
		add_register_req_in(res);
592
	}
593
	be_set_constr_out(res, 0, arch_no_register_req);
594

Matthias Braun's avatar
Matthias Braun committed
595
	a = get_irn_generic_attr(res);
596
	a->num_ret_vals = n_res;
597
	a->pop          = pop;
598
	a->emit_pop     = 0;
Sebastian Hack's avatar
Sebastian Hack committed
599

600
	return res;
Sebastian Hack's avatar
Sebastian Hack committed
601
602
}

603
604
int be_Return_get_n_rets(const ir_node *ret)
{
605
	const be_return_attr_t *a = get_irn_generic_attr_const(ret);
606
607
608
	return a->num_ret_vals;
}

609
610
unsigned be_Return_get_pop(const ir_node *ret)
{
611
612
613
614
	const be_return_attr_t *a = get_irn_generic_attr_const(ret);
	return a->pop;
}

615
616
int be_Return_get_emit_pop(const ir_node *ret)
{
617
618
619
620
	const be_return_attr_t *a = get_irn_generic_attr_const(ret);
	return a->emit_pop;
}

621
622
void be_Return_set_emit_pop(ir_node *ret, int emit_pop)
{
623
624
625
626
	be_return_attr_t *a = get_irn_generic_attr(ret);
	a->emit_pop = emit_pop;
}

627
628
int be_Return_append_node(ir_node *ret, ir_node *node)
{
629
630
631
	int pos;

	pos = add_irn_n(ret, node);
632
	add_register_req_in(ret);
633
634
635
636

	return pos;
}

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

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

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

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

659
660
ir_node *be_new_AddSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp,
		ir_node *sz)
Sebastian Hack's avatar
Sebastian Hack committed
661
{
Sebastian Hack's avatar
Sebastian Hack committed
662
	be_node_attr_t *a;
Sebastian Hack's avatar
Sebastian Hack committed
663
	ir_node *irn;
Sebastian Hack's avatar
Sebastian Hack committed
664
	ir_node *in[be_pos_AddSP_last];
665
	const arch_register_class_t *cls;
666
	ir_graph *irg;
Sebastian Hack's avatar
Sebastian Hack committed
667

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

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

Sebastian Hack's avatar
Sebastian Hack committed
675
	/* Set output constraint to stack register. */
676
677
	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));
Matthias Braun's avatar
Matthias Braun committed
678
679
	be_set_constr_single_reg_out(irn, pn_be_AddSP_sp, sp,
	                             arch_register_req_type_produces_sp);
680

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

	return irn;
}

686
ir_node *be_new_SubSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp, ir_node *sz)
Michael Beck's avatar
Michael Beck committed
687
688
689
690
{
	be_node_attr_t *a;
	ir_node *irn;
	ir_node *in[be_pos_SubSP_last];
691
	ir_graph *irg;
Michael Beck's avatar
Michael Beck committed
692
693
694
695

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

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

	/* 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_Start(dbg_info *dbgi, ir_node *bl, int n_outs)
Sebastian Hack's avatar
Sebastian Hack committed
709
{
710
711
	ir_node *res;
	int i;
712
	ir_graph *irg = get_Block_irg(bl);
Sebastian Hack's avatar
Sebastian Hack committed
713

714
	res = new_ir_node(dbgi, irg, bl, op_be_Start, mode_T, 0, NULL);
715
716
717
718
	init_node_attr(res, 0, -1);
	for (i = 0; i < n_outs; ++i) {
		add_register_req_out(res);
	}
719
720
721
722

	return res;
}

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

730
	in[0]  = frame;
731
	irn    = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
732
	a      = init_node_attr(irn, 1, 1);
733
734
	a->ent = ent;
	a->offset = 0;
735
736
	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
737
738

	return optimize_node(irn);
Sebastian Hack's avatar
Sebastian Hack committed
739
740
}

741
742
ir_node *be_get_FrameAddr_frame(const ir_node *node)
{
Michael Beck's avatar
Michael Beck committed
743
744
745
746
	assert(be_is_FrameAddr(node));
	return get_irn_n(node, be_pos_FrameAddr_ptr);
}

Matthias Braun's avatar
Matthias Braun committed
747
748
749
750
751
752
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;
}

753
ir_node *be_new_CopyKeep(const arch_register_class_t *cls, ir_node *bl, ir_node *src, int n, ir_node *in_keep[], ir_mode *mode)
Sebastian Hack's avatar
Sebastian Hack committed
754
{
755
756
	ir_node  *irn;
	ir_node **in = ALLOCAN(ir_node*, n + 1);
757
	ir_graph *irg = get_Block_irg(bl);
Sebastian Hack's avatar
Sebastian Hack committed
758
759
760
761

	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);
762
	init_node_attr(irn, n + 1, 1);
763
764
	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
765
766
767
768

	return irn;
}

769
ir_node *be_new_CopyKeep_single(const arch_register_class_t *cls, ir_node *bl, ir_node *src, ir_node *keep, ir_mode *mode)
Sebastian Hack's avatar
Sebastian Hack committed
770
{
771
	return be_new_CopyKeep(cls, bl, src, 1, &keep, mode);
Sebastian Hack's avatar
Sebastian Hack committed
772
773
}

774
775
ir_node *be_get_CopyKeep_op(const ir_node *cpy)
{
776
777
778
	return get_irn_n(cpy, be_pos_CopyKeep_op);
}

779
780
void be_set_CopyKeep_op(ir_node *cpy, ir_node *op)
{
781
782
783
	set_irn_n(cpy, be_pos_CopyKeep_op, op);
}

784
ir_node *be_new_Barrier(ir_node *bl, int n, ir_node *in[])
Sebastian Hack's avatar
Sebastian Hack committed
785
{
786
787
	ir_node *res;
	int i;
788
	ir_graph *irg = get_Block_irg(bl);
Sebastian Hack's avatar
Sebastian Hack committed
789

790
	res = new_ir_node(NULL, irg, bl, op_be_Barrier, mode_T, -1, NULL);
791
792
	init_node_attr(res, -1, -1);
	for (i = 0; i < n; ++i) {
793
		add_irn_n(res, in[i]);
794
795
		add_register_req_in(res);
		add_register_req_out(res);
796
797
798
799
800
801
802
803
804
	}

	return res;
}

ir_node *be_Barrier_append_node(ir_node *barrier, ir_node *node)
{
	ir_mode *mode = get_irn_mode(node);
	int n = add_irn_n(barrier, node);
805

806
	ir_node *proj = new_r_Proj(barrier, mode, n);
807
808
	add_register_req_in(barrier);
	add_register_req_out(barrier);
809
810

	return proj;
Sebastian Hack's avatar
Sebastian Hack committed
811
812
}

813
static bool be_has_frame_entity(const ir_node *irn)
814
{
815
	switch (get_irn_opcode(irn)) {
816
	case beo_Spill:
817
	case beo_Reload:
818
	case beo_FrameAddr:
819
		return true;
820
	default:
821
		return false;
822
823
824
	}
}

825
ir_entity *be_get_frame_entity(const ir_node *irn)
826
{
827
	if (be_has_frame_entity(irn)) {
Matthias Braun's avatar
Matthias Braun committed
828
		const be_frame_attr_t *a = get_irn_generic_attr_const(irn);
829
830