benode.c 36.9 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
2
/*
 * This file is part of libFirm.
3
 * Copyright (C) 2012 University of Karlsruhe.
Christian Würdig's avatar
Christian Würdig committed
4
5
 */

Sebastian Hack's avatar
Sebastian Hack committed
6
/**
Christian Würdig's avatar
Christian Würdig committed
7
8
9
10
 * @file
 * @brief       Backend node support for generic backend nodes.
 * @author      Sebastian Hack
 * @date        17.05.2005
Sebastian Hack's avatar
Sebastian Hack committed
11
 *
12
 * Backend node support for generic backend nodes.
Michael Beck's avatar
Michael Beck committed
13
 * This file provides Perm, Copy, Spill and Reload nodes.
Sebastian Hack's avatar
Sebastian Hack committed
14
 */
15
#include "config.h"
Sebastian Hack's avatar
Sebastian Hack committed
16
17
18

#include <stdlib.h>

19
#include "beirg.h"
Sebastian Hack's avatar
Sebastian Hack committed
20
21
22
#include "obst.h"
#include "set.h"
#include "pmap.h"
23
#include "util.h"
Sebastian Hack's avatar
Sebastian Hack committed
24
#include "debug.h"
25
#include "fourcc.h"
Sebastian Hack's avatar
Sebastian Hack committed
26
#include "bitfiddle.h"
Matthias Braun's avatar
Matthias Braun committed
27
#include "raw_bitset.h"
28
#include "error.h"
29
#include "array_t.h"
Sebastian Hack's avatar
Sebastian Hack committed
30
31
32
33

#include "irop_t.h"
#include "irmode_t.h"
#include "irnode_t.h"
34
#include "ircons_t.h"
35
#include "irprintf.h"
Sebastian Hack's avatar
Sebastian Hack committed
36
#include "irgwalk.h"
Michael Beck's avatar
Michael Beck committed
37
#include "iropt_t.h"
38
39
#include "irbackedge_t.h"
#include "irverify_t.h"
Sebastian Hack's avatar
Sebastian Hack committed
40

41
42
#include "be_t.h"
#include "belive_t.h"
43
#include "besched.h"
44
#include "benode.h"
45
#include "bearch.h"
Sebastian Hack's avatar
Sebastian Hack committed
46

47
48
#include "beirgmod.h"

49
50
51
52
typedef struct be_node_attr_t {
	except_attr  exc;
} be_node_attr_t;

53
54
/** The be_Return nodes attribute type. */
typedef struct {
55
	be_node_attr_t base;
Matthias Braun's avatar
Matthias Braun committed
56
57
58
	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 */
59
60
} be_return_attr_t;

Michael Beck's avatar
Michael Beck committed
61
/** The be_IncSP attribute type. */
Sebastian Hack's avatar
Sebastian Hack committed
62
typedef struct {
63
	be_node_attr_t base;
Matthias Braun's avatar
Matthias Braun committed
64
65
66
67
	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
68
} be_incsp_attr_t;
Sebastian Hack's avatar
Sebastian Hack committed
69

70
/** The be_Frame attribute type. */
71
typedef struct {
72
	be_node_attr_t  base;
73
74
	ir_entity      *ent;
	int             offset;
75
76
} be_frame_attr_t;

77
/** The be_Call attribute type. */
78
typedef struct {
79
	be_node_attr_t  base;
Matthias Braun's avatar
Matthias Braun committed
80
	ir_entity      *ent;        /**< called entity if this is a static call. */
81
	unsigned        pop;
Matthias Braun's avatar
Matthias Braun committed
82
	ir_type        *call_tp;    /**< call type, copied from the original Call */
83
84
} be_call_attr_t;

85
typedef struct {
86
87
88
	be_node_attr_t base;
	ir_entity    **in_entities;
	ir_entity    **out_entities;
Matthias Braun's avatar
Matthias Braun committed
89
} be_memperm_attr_t;
90

91
92
93
ir_op *op_be_Spill;
ir_op *op_be_Reload;
ir_op *op_be_Perm;
Matthias Braun's avatar
Matthias Braun committed
94
ir_op *op_be_MemPerm;
95
96
ir_op *op_be_Copy;
ir_op *op_be_Keep;
Sebastian Hack's avatar
Sebastian Hack committed
97
ir_op *op_be_CopyKeep;
98
99
100
ir_op *op_be_Call;
ir_op *op_be_Return;
ir_op *op_be_IncSP;
Sebastian Hack's avatar
Sebastian Hack committed
101
ir_op *op_be_AddSP;
Michael Beck's avatar
Michael Beck committed
102
ir_op *op_be_SubSP;
103
ir_op *op_be_Start;
104
ir_op *op_be_FrameAddr;
Sebastian Hack's avatar
Sebastian Hack committed
105

Michael Beck's avatar
Michael Beck committed
106
/**
Michael Beck's avatar
Michael Beck committed
107
 * Compare the attributes of two be_FrameAddr nodes.
Michael Beck's avatar
Michael Beck committed
108
 *
Michael Beck's avatar
Michael Beck committed
109
 * @return zero if both nodes have identically attributes
Michael Beck's avatar
Michael Beck committed
110
 */
Michael Beck's avatar
Michael Beck committed
111
static int FrameAddr_cmp_attr(const ir_node *a, const ir_node *b)
112
{
113
114
	const be_frame_attr_t *a_attr = (const be_frame_attr_t*)get_irn_generic_attr_const(a);
	const be_frame_attr_t *b_attr = (const be_frame_attr_t*)get_irn_generic_attr_const(b);
Michael Beck's avatar
Michael Beck committed
115

116
117
118
	if (a_attr->ent != b_attr->ent || a_attr->offset != b_attr->offset)
		return 1;

119
	return be_nodes_equal(a, b);
120
121
}

Michael Beck's avatar
Michael Beck committed
122
123
124
125
126
/**
 * Compare the attributes of two be_Return nodes.
 *
 * @return zero if both nodes have identically attributes
 */
Michael Beck's avatar
Michael Beck committed
127
static int Return_cmp_attr(const ir_node *a, const ir_node *b)
128
{
129
130
	const be_return_attr_t *a_attr = (const be_return_attr_t*)get_irn_generic_attr_const(a);
	const be_return_attr_t *b_attr = (const be_return_attr_t*)get_irn_generic_attr_const(b);
131
132
133

	if (a_attr->num_ret_vals != b_attr->num_ret_vals)
		return 1;
134
135
	if (a_attr->pop != b_attr->pop)
		return 1;
136
137
	if (a_attr->emit_pop != b_attr->emit_pop)
		return 1;
138

139
	return be_nodes_equal(a, b);
140
141
}

Michael Beck's avatar
Michael Beck committed
142
143
144
145
146
/**
 * Compare the attributes of two be_IncSP nodes.
 *
 * @return zero if both nodes have identically attributes
 */
Michael Beck's avatar
Michael Beck committed
147
static int IncSP_cmp_attr(const ir_node *a, const ir_node *b)
148
{
149
150
	const be_incsp_attr_t *a_attr = (const be_incsp_attr_t*)get_irn_generic_attr_const(a);
	const be_incsp_attr_t *b_attr = (const be_incsp_attr_t*)get_irn_generic_attr_const(b);
151
152
153
154

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

155
	return be_nodes_equal(a, b);
156
157
}

Michael Beck's avatar
Michael Beck committed
158
159
160
161
162
/**
 * Compare the attributes of two be_Call nodes.
 *
 * @return zero if both nodes have identically attributes
 */
Michael Beck's avatar
Michael Beck committed
163
static int Call_cmp_attr(const ir_node *a, const ir_node *b)
164
{
165
166
	const be_call_attr_t *a_attr = (const be_call_attr_t*)get_irn_generic_attr_const(a);
	const be_call_attr_t *b_attr = (const be_call_attr_t*)get_irn_generic_attr_const(b);
167
168

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

172
	return be_nodes_equal(a, b);
Michael Beck's avatar
Michael Beck committed
173
174
}

175
static arch_register_req_t *allocate_reg_req(ir_graph *const irg)
Matthias Braun's avatar
Matthias Braun committed
176
{
177
	struct obstack *obst = be_get_be_obst(irg);
Matthias Braun's avatar
Matthias Braun committed
178

179
	arch_register_req_t *req = OALLOCZ(obst, arch_register_req_t);
180
181
	return req;
}
Matthias Braun's avatar
Matthias Braun committed
182

183
184
void be_set_constr_in(ir_node *node, int pos, const arch_register_req_t *req)
{
185
186
187
	backend_info_t *info = be_get_info(node);
	assert(pos < get_irn_arity(node));
	info->in_reqs[pos] = req;
188
}
Matthias Braun's avatar
Matthias Braun committed
189

190
191
192
193
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
194
195
}

196
/**
197
 * Initializes the generic attribute of all be nodes and return it.
198
 */
199
static void init_node_attr(ir_node *node, int n_inputs, int n_outputs)
Sebastian Hack's avatar
Sebastian Hack committed
200
{
201
202
	assert(n_outputs >= 0);

203
	ir_graph       *irg  = get_irn_irg(node);
204
	struct obstack *obst = be_get_be_obst(irg);
205
	backend_info_t *info = be_get_info(node);
206
	const arch_register_req_t **in_reqs;
Sebastian Hack's avatar
Sebastian Hack committed
207

208
209
	if (n_inputs >= 0) {
		int i;
210
211
		assert(n_inputs == get_irn_arity(node));
		in_reqs = OALLOCN(obst, const arch_register_req_t*, n_inputs);
212
		for (i = 0; i < n_inputs; ++i) {
213
			in_reqs[i] = arch_no_register_req;
214
215
		}
	} else {
216
		in_reqs = NEW_ARR_F(const arch_register_req_t*, 0);
217
	}
218
	info->in_reqs = in_reqs;
219

220
	info->out_infos = NEW_ARR_DZ(reg_out_info_t, obst, n_outputs);
221
222
	for (int i = 0; i < n_outputs; ++i) {
		info->out_infos[i].req = arch_no_register_req;
Sebastian Hack's avatar
Sebastian Hack committed
223
224
	}
}
225

226
static void add_register_req_in(ir_node *node, const arch_register_req_t *req)
227
{
228
	backend_info_t *info = be_get_info(node);
229
	ARR_APP1(const arch_register_req_t*, info->in_reqs, req);
230
231
}

232
233
234
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)
235
{
Matthias Braun's avatar
Matthias Braun committed
236
	be_frame_attr_t *a;
237
	ir_node         *in[2];
238
	ir_node         *res;
239
	ir_graph        *irg = get_Block_irg(bl);
240

241
242
243
	in[0]     = frame;
	in[1]     = to_spill;
	res       = new_ir_node(NULL, irg, bl, op_be_Spill, mode_M, 2, in);
244
245
	init_node_attr(res, 2, 1);
	a         = (be_frame_attr_t*) get_irn_generic_attr(res);
246
	a->ent    = NULL;
Matthias Braun's avatar
Matthias Braun committed
247
	a->offset = 0;
248
	a->base.exc.pin_state = op_pin_state_pinned;
Sebastian Hack's avatar
Sebastian Hack committed
249

250
251
	be_node_set_reg_class_in(res, n_be_Spill_frame, cls_frame);
	be_node_set_reg_class_in(res, n_be_Spill_val, cls);
252
	arch_set_irn_register_req_out(res, 0, arch_no_register_req);
253
	arch_add_irn_flags(res, arch_irn_flags_spill);
254

Sebastian Hack's avatar
Sebastian Hack committed
255
256
	return res;
}
257

258
ir_node *be_new_Reload(const arch_register_class_t *cls,
259
		const arch_register_class_t *cls_frame, ir_node *block,
260
		ir_node *frame, ir_node *mem, ir_mode *mode)
261
{
262
263
264
	ir_node  *in[2];
	ir_node  *res;
	ir_graph *irg = get_Block_irg(block);
265
	be_frame_attr_t *a;
266
267
268

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

271
	init_node_attr(res, 2, 1);
272
	be_node_set_reg_class_out(res, 0, cls);
273

274
	be_node_set_reg_class_in(res, n_be_Reload_frame, cls_frame);
275
	arch_set_irn_flags(res, arch_irn_flags_rematerializable);
276

277
278
279
280
281
	a         = (be_frame_attr_t*) get_irn_generic_attr(res);
	a->ent    = NULL;
	a->offset = 0;
	a->base.exc.pin_state = op_pin_state_pinned;

Sebastian Hack's avatar
Sebastian Hack committed
282
283
	return res;
}
284

285
ir_node *be_get_Reload_mem(const ir_node *irn)
Sebastian Hack's avatar
Sebastian Hack committed
286
287
{
	assert(be_is_Reload(irn));
288
	return get_irn_n(irn, n_be_Reload_mem);
Sebastian Hack's avatar
Sebastian Hack committed
289
290
}

291
292
293
ir_node *be_get_Reload_frame(const ir_node *irn)
{
	assert(be_is_Reload(irn));
294
	return get_irn_n(irn, n_be_Reload_frame);
295
296
}

297
298
299
ir_node *be_get_Spill_val(const ir_node *irn)
{
	assert(be_is_Spill(irn));
300
	return get_irn_n(irn, n_be_Spill_val);
301
}
Matthias Braun's avatar
Matthias Braun committed
302

303
304
305
ir_node *be_get_Spill_frame(const ir_node *irn)
{
	assert(be_is_Spill(irn));
306
	return get_irn_n(irn, n_be_Spill_frame);
307
}
308

309
310
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
311
{
312
313
314
	int            i;
	ir_graph       *irg = get_Block_irg(block);
	be_node_attr_t *attr;
315

316
	ir_node *irn = new_ir_node(NULL, irg, block, op_be_Perm, mode_T, n, in);
317
	init_node_attr(irn, n, n);
318
319
	attr                = (be_node_attr_t*) get_irn_generic_attr(irn);
	attr->exc.pin_state = op_pin_state_pinned;
320
	for (i = 0; i < n; ++i) {
321
322
323
324
325
326
		const ir_node             *input = in[i];
		const arch_register_req_t *req   = arch_get_irn_register_req(input);
		if (req->width == 1) {
			be_set_constr_in(irn, i, cls->class_req);
			be_set_constr_out(irn, i, cls->class_req);
		} else {
327
			arch_register_req_t *const new_req = allocate_reg_req(irg);
328
329
330
331
332
333
			new_req->cls   = cls;
			new_req->type  = (req->type & arch_register_req_type_aligned);
			new_req->width = req->width;
			be_set_constr_in(irn, i, new_req);
			be_set_constr_out(irn, i, new_req);
		}
Sebastian Hack's avatar
Sebastian Hack committed
334
335
	}

Sebastian Hack's avatar
Sebastian Hack committed
336
337
	return irn;
}
338

339
340
void be_Perm_reduce(ir_node *perm, int new_size, int *map)
{
341
342
343
344
345
	int             arity      = get_irn_arity(perm);
	const arch_register_req_t **old_in_reqs
		= ALLOCAN(const arch_register_req_t*, arity);
	reg_out_info_t  *old_infos = ALLOCAN(reg_out_info_t, arity);
	backend_info_t  *info      = be_get_info(perm);
346
	ir_node        **new_in;
347
	int              i;
348
349

	assert(be_is_Perm(perm));
350
	assert(new_size <= arity);
351

352
	new_in = ALLOCAN(ir_node*, new_size);
353

354
	/* save the old register data */
355
	memcpy(old_in_reqs, info->in_reqs, arity * sizeof(old_in_reqs[0]));
356
	memcpy(old_infos, info->out_infos, arity * sizeof(old_infos[0]));
357

Matthias Braun's avatar
Matthias Braun committed
358
	/* compose the new in array and set the new register data directly */
359
	for (i = 0; i < new_size; ++i) {
360
		int idx = map[i];
361
		new_in[i]          = get_irn_n(perm, idx);
362
		info->in_reqs[i]   = old_in_reqs[idx];
363
		info->out_infos[i] = old_infos[idx];
364
365
366
367
368
	}

	set_irn_in(perm, new_size, new_in);
}

369
ir_node *be_new_MemPerm(ir_node *block, int n, ir_node *in[])
Matthias Braun's avatar
Matthias Braun committed
370
{
371
372
	ir_graph                     *irg       = get_Block_irg(block);
	const arch_env_t             *arch_env  = be_get_irg_arch_env(irg);
373
374
375
376
377
	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
378

379
	real_in = ALLOCAN(ir_node*, n + 1);
380
381
382
	real_in[0] = frame;
	memcpy(&real_in[1], in, n * sizeof(real_in[0]));

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

385
	init_node_attr(irn, n + 1, n);
386
	be_node_set_reg_class_in(irn, 0, sp->reg_class);
Matthias Braun's avatar
Matthias Braun committed
387

388
	attr               = (be_memperm_attr_t*)get_irn_generic_attr(irn);
389
390
	attr->in_entities  = OALLOCNZ(get_irg_obstack(irg), ir_entity*, n);
	attr->out_entities = OALLOCNZ(get_irg_obstack(irg), ir_entity*, n);
Matthias Braun's avatar
Matthias Braun committed
391
392
393
394

	return irn;
}

395
ir_node *be_new_Copy(ir_node *bl, ir_node *op)
Sebastian Hack's avatar
Sebastian Hack committed
396
397
398
{
	ir_node *in[1];
	ir_node *res;
399
	be_node_attr_t *attr;
400
	ir_graph *irg = get_Block_irg(bl);
401
402
	const arch_register_req_t   *in_req = arch_get_irn_register_req(op);
	const arch_register_class_t *cls    = in_req->cls;
403

Sebastian Hack's avatar
Sebastian Hack committed
404
	in[0] = op;
405
	res   = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
406
	init_node_attr(res, 1, 1);
407
408
	attr = (be_node_attr_t*) get_irn_generic_attr(res);
	attr->exc.pin_state = op_pin_state_floats;
409
410
	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
411

412
	arch_register_req_t *const req = allocate_reg_req(irg);
413
	req->cls        = cls;
414
415
	req->type       = arch_register_req_type_should_be_same
		| (in_req->type & arch_register_req_type_aligned);
416
	req->other_same = 1U << 0;
417
	req->width      = in_req->width;
418
	be_set_constr_out(res, 0, req);
Matthias Braun's avatar
Matthias Braun committed
419

Sebastian Hack's avatar
Sebastian Hack committed
420
	return res;
421
422
}

423
424
ir_node *be_get_Copy_op(const ir_node *cpy)
{
425
	return get_irn_n(cpy, n_be_Copy_op);
426
427
}

428
ir_node *be_new_Keep(ir_node *block, int n, ir_node *in[])
Sebastian Hack's avatar
Sebastian Hack committed
429
{
Sebastian Hack's avatar
Sebastian Hack committed
430
	int i;
431
	ir_node *res;
432
	ir_graph *irg = get_Block_irg(block);
433
	be_node_attr_t *attr;
434

435
436
	res = new_ir_node(NULL, irg, block, op_be_Keep, mode_ANY, -1, NULL);
	init_node_attr(res, -1, 1);
437
438
	attr = (be_node_attr_t*) get_irn_generic_attr(res);
	attr->exc.pin_state = op_pin_state_pinned;
Sebastian Hack's avatar
Sebastian Hack committed
439

440
	for (i = 0; i < n; ++i) {
441
442
443
444
445
		ir_node *pred = in[i];
		add_irn_n(res, pred);
		const arch_register_req_t *req = arch_get_irn_register_req(pred);
		req = req->cls != NULL ? req->cls->class_req : arch_no_register_req;
		add_register_req_in(res, req);
Sebastian Hack's avatar
Sebastian Hack committed
446
	}
447
448
449
450
451
452
453
454
	keep_alive(res);

	return res;
}

void be_Keep_add_node(ir_node *keep, const arch_register_class_t *cls, ir_node *node)
{
	assert(be_is_Keep(keep));
455
456
	add_irn_n(keep, node);
	add_register_req_in(keep, cls->class_req);
Sebastian Hack's avatar
Sebastian Hack committed
457
458
}

459
ir_node *be_new_Call(dbg_info *dbg, ir_node *bl, ir_node *mem,
460
461
462
		const arch_register_req_t *sp_req, ir_node *sp,
		const arch_register_req_t *ptr_req, ir_node *ptr,
		int n_outs, int n, ir_node *in[], ir_type *call_tp)
Sebastian Hack's avatar
Sebastian Hack committed
463
{
464
	be_call_attr_t *a;
465
	int real_n = n_be_Call_first_arg + n;
Sebastian Hack's avatar
Sebastian Hack committed
466
	ir_node **real_in;
Sebastian Hack's avatar
Sebastian Hack committed
467

468
	NEW_ARR_A(ir_node *, real_in, real_n);
469
470
471
472
	real_in[n_be_Call_mem] = mem;
	real_in[n_be_Call_sp]  = sp;
	real_in[n_be_Call_ptr] = ptr;
	memcpy(&real_in[n_be_Call_first_arg], in, n * sizeof(in[0]));
Sebastian Hack's avatar
Sebastian Hack committed
473

474
475
	ir_graph *const irg = get_Block_irg(bl);
	ir_node  *const irn = new_ir_node(dbg, irg, bl, op_be_Call, mode_T, real_n, real_in);
476
	init_node_attr(irn, real_n, n_outs);
477
478
479
480
481
	a                     = (be_call_attr_t*)get_irn_generic_attr(irn);
	a->ent                = NULL;
	a->call_tp            = call_tp;
	a->pop                = 0;
	a->base.exc.pin_state = op_pin_state_pinned;
482
483
	be_set_constr_in(irn, n_be_Call_sp, sp_req);
	be_set_constr_in(irn, n_be_Call_ptr, ptr_req);
Sebastian Hack's avatar
Sebastian Hack committed
484
	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
485
486
}

487
488
ir_entity *be_Call_get_entity(const ir_node *call)
{
489
	const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
490
491
492
493
	assert(be_is_Call(call));
	return a->ent;
}

494
495
void be_Call_set_entity(ir_node *call, ir_entity *ent)
{
496
	be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
497
498
499
500
	assert(be_is_Call(call));
	a->ent = ent;
}

501
502
ir_type *be_Call_get_type(ir_node *call)
{
503
	const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
504
505
506
507
	assert(be_is_Call(call));
	return a->call_tp;
}

508
509
void be_Call_set_type(ir_node *call, ir_type *call_tp)
{
510
	be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
511
512
513
514
	assert(be_is_Call(call));
	a->call_tp = call_tp;
}

515
516
void be_Call_set_pop(ir_node *call, unsigned pop)
{
517
	be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
Matthias Braun's avatar
Matthias Braun committed
518
519
520
	a->pop = pop;
}

521
522
unsigned be_Call_get_pop(const ir_node *call)
{
523
	const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
Matthias Braun's avatar
Matthias Braun committed
524
525
526
	return a->pop;
}

527
ir_node *be_new_Return(dbg_info *const dbg, ir_node *const block, int const n_res, unsigned const pop, int const n, ir_node **const in)
Sebastian Hack's avatar
Sebastian Hack committed
528
{
529
530
	ir_graph *const irg = get_Block_irg(block);
	ir_node  *const res = new_ir_node(dbg, irg, block, op_be_Return, mode_X, n, in);
531
	init_node_attr(res, n, 1);
532
	be_set_constr_out(res, 0, arch_no_register_req);
533

534
	be_return_attr_t *const a = (be_return_attr_t*)get_irn_generic_attr(res);
535
536
537
538
	a->num_ret_vals       = n_res;
	a->pop                = pop;
	a->emit_pop           = 0;
	a->base.exc.pin_state = op_pin_state_pinned;
Sebastian Hack's avatar
Sebastian Hack committed
539

540
	return res;
Sebastian Hack's avatar
Sebastian Hack committed
541
542
}

543
544
int be_Return_get_n_rets(const ir_node *ret)
{
545
	const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
546
547
548
	return a->num_ret_vals;
}

549
550
unsigned be_Return_get_pop(const ir_node *ret)
{
551
	const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
552
553
554
	return a->pop;
}

555
556
int be_Return_get_emit_pop(const ir_node *ret)
{
557
	const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
558
559
560
	return a->emit_pop;
}

561
562
void be_Return_set_emit_pop(ir_node *ret, int emit_pop)
{
563
	be_return_attr_t *a = (be_return_attr_t*)get_irn_generic_attr(ret);
564
565
566
	a->emit_pop = emit_pop;
}

567
ir_node *be_new_IncSP(const arch_register_t *sp, ir_node *bl,
568
                      ir_node *old_sp, int offset, int align)
Sebastian Hack's avatar
Sebastian Hack committed
569
{
Michael Beck's avatar
Michael Beck committed
570
	be_incsp_attr_t *a;
Sebastian Hack's avatar
Sebastian Hack committed
571
	ir_node *irn;
Sebastian Hack's avatar
Sebastian Hack committed
572
	ir_node *in[1];
573
	ir_graph *irg = get_Block_irg(bl);
Sebastian Hack's avatar
Sebastian Hack committed
574
575

	in[0]     = old_sp;
576
	irn       = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode,
577
	                        ARRAY_SIZE(in), in);
578
	init_node_attr(irn, 1, 1);
579
580
581
582
	a                     = (be_incsp_attr_t*)get_irn_generic_attr(irn);
	a->offset             = offset;
	a->align              = align;
	a->base.exc.pin_state = op_pin_state_pinned;
Sebastian Hack's avatar
Sebastian Hack committed
583
584

	/* Set output constraint to stack register. */
585
586
	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
587
588

	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
589
}
Sebastian Hack's avatar
Sebastian Hack committed
590

591
592
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
593
{
Sebastian Hack's avatar
Sebastian Hack committed
594
	ir_node *irn;
595
	ir_node *in[n_be_AddSP_last];
596
	ir_graph *irg;
597
	be_node_attr_t *attr;
Sebastian Hack's avatar
Sebastian Hack committed
598

599
600
	in[n_be_AddSP_old_sp] = old_sp;
	in[n_be_AddSP_size]   = sz;
Sebastian Hack's avatar
Sebastian Hack committed
601

602
	irg = get_Block_irg(bl);
603
604
	irn = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T, n_be_AddSP_last, in);
	init_node_attr(irn, n_be_AddSP_last, pn_be_AddSP_last);
605
606
	attr = (be_node_attr_t*) get_irn_generic_attr(irn);
	attr->exc.pin_state = op_pin_state_pinned;
Sebastian Hack's avatar
Sebastian Hack committed
607

Sebastian Hack's avatar
Sebastian Hack committed
608
	/* Set output constraint to stack register. */
609
	be_set_constr_single_reg_in(irn, n_be_AddSP_old_sp, sp,
610
	                            arch_register_req_type_none);
611
	be_node_set_reg_class_in(irn, n_be_AddSP_size, sp->reg_class);
Matthias Braun's avatar
Matthias Braun committed
612
613
	be_set_constr_single_reg_out(irn, pn_be_AddSP_sp, sp,
	                             arch_register_req_type_produces_sp);
614

Sebastian Hack's avatar
Sebastian Hack committed
615
616
617
	return irn;
}

618
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
619
620
{
	ir_node *irn;
621
	ir_node *in[n_be_SubSP_last];
622
	ir_graph *irg;
623
	be_node_attr_t *attr;
Michael Beck's avatar
Michael Beck committed
624

625
626
	in[n_be_SubSP_old_sp] = old_sp;
	in[n_be_SubSP_size]   = sz;
Michael Beck's avatar
Michael Beck committed
627

628
	irg = get_Block_irg(bl);
629
630
	irn = new_ir_node(NULL, irg, bl, op_be_SubSP, mode_T, n_be_SubSP_last, in);
	init_node_attr(irn, n_be_SubSP_last, pn_be_SubSP_last);
631
632
	attr = (be_node_attr_t*) get_irn_generic_attr(irn);
	attr->exc.pin_state = op_pin_state_pinned;
Michael Beck's avatar
Michael Beck committed
633
634

	/* Set output constraint to stack register. */
635
	be_set_constr_single_reg_in(irn, n_be_SubSP_old_sp, sp,
636
	                            arch_register_req_type_none);
637
	be_node_set_reg_class_in(irn, n_be_SubSP_size, sp->reg_class);
638
	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
639
640
641
642

	return irn;
}

643
ir_node *be_new_Start(dbg_info *dbgi, ir_node *bl, int n_outs)
Sebastian Hack's avatar
Sebastian Hack committed
644
{
645
	ir_node *res;
646
	ir_graph *irg = get_Block_irg(bl);
647
	be_node_attr_t *attr;
Sebastian Hack's avatar
Sebastian Hack committed
648

649
	res = new_ir_node(dbgi, irg, bl, op_be_Start, mode_T, 0, NULL);
650
	init_node_attr(res, 0, n_outs);
651
652
	attr = (be_node_attr_t*) get_irn_generic_attr(res);
	attr->exc.pin_state = op_pin_state_pinned;
653
654
655
656

	return res;
}

657
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
658
{
659
	be_frame_attr_t *a;
Sebastian Hack's avatar
Sebastian Hack committed
660
661
	ir_node *irn;
	ir_node *in[1];
662
	ir_graph *irg = get_Block_irg(bl);
Sebastian Hack's avatar
Sebastian Hack committed
663

664
	in[0]  = frame;
665
	irn    = new_ir_node(NULL, irg, bl, op_be_FrameAddr, get_irn_mode(frame), 1, in);
666
	init_node_attr(irn, 1, 1);
667
668
669
670
	a                     = (be_frame_attr_t*)get_irn_generic_attr(irn);
	a->ent                = ent;
	a->offset             = 0;
	a->base.exc.pin_state = op_pin_state_floats;
671
672
	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
673
674

	return optimize_node(irn);
Sebastian Hack's avatar
Sebastian Hack committed
675
676
}

677
678
ir_node *be_get_FrameAddr_frame(const ir_node *node)
{
Michael Beck's avatar
Michael Beck committed
679
	assert(be_is_FrameAddr(node));
680
	return get_irn_n(node, n_be_FrameAddr_ptr);
Michael Beck's avatar
Michael Beck committed
681
682
}

Matthias Braun's avatar
Matthias Braun committed
683
684
ir_entity *be_get_FrameAddr_entity(const ir_node *node)
{
685
	const be_frame_attr_t *attr = (const be_frame_attr_t*)get_irn_generic_attr_const(node);
Matthias Braun's avatar
Matthias Braun committed
686
687
688
	return attr->ent;
}

689
ir_node *be_new_CopyKeep(ir_node *bl, ir_node *src, int n, ir_node *in_keep[])
Sebastian Hack's avatar
Sebastian Hack committed
690
{
691
692
	ir_node  *irn;
	ir_node **in = ALLOCAN(ir_node*, n + 1);
693
	ir_graph *irg = get_Block_irg(bl);
694
695
696
	const arch_register_req_t   *req  = arch_get_irn_register_req(src);
	const arch_register_class_t *cls  = req->cls;
	ir_mode                     *mode = get_irn_mode(src);
697
	be_node_attr_t *attr;
Sebastian Hack's avatar
Sebastian Hack committed
698
699
700
701

	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);
702
	init_node_attr(irn, n + 1, 1);
703
704
	attr = (be_node_attr_t*) get_irn_generic_attr(irn);
	attr->exc.pin_state = op_pin_state_floats;
705
706
	be_node_set_reg_class_in(irn, 0, cls);
	be_node_set_reg_class_out(irn, 0, cls);
707
708
709
710
711
712
	for (int i = 0; i < n; ++i) {
		ir_node *pred = in_keep[i];
		const arch_register_req_t *req = arch_get_irn_register_req(pred);
		req = req->cls != NULL ? req->cls->class_req : arch_no_register_req;
		be_set_constr_in(irn, i+1, req);
	}
Sebastian Hack's avatar
Sebastian Hack committed
713
714
715
716

	return irn;
}

717
ir_node *be_new_CopyKeep_single(ir_node *bl, ir_node *src, ir_node *keep)
Sebastian Hack's avatar
Sebastian Hack committed
718
{
719
	return be_new_CopyKeep(bl, src, 1, &keep);
Sebastian Hack's avatar
Sebastian Hack committed
720
721
}

722
723
ir_node *be_get_CopyKeep_op(const ir_node *cpy)
{
724
	return get_irn_n(cpy, n_be_CopyKeep_op);
725
726
}

727
728
void be_set_CopyKeep_op(ir_node *cpy, ir_node *op)
{
729
	set_irn_n(cpy, n_be_CopyKeep_op, op);
730
731
}

732
static bool be_has_frame_entity(const ir_node *irn)
733
{
734
	switch (get_irn_opcode(irn)) {
735
	case beo_Spill:
736
	case beo_Reload:
737
	case beo_FrameAddr:
738
		return true;
739
	default:
740
		return false;
741
742
743
	}
}

744
ir_entity *be_get_frame_entity(const ir_node *irn)
745
{
746
	if (be_has_frame_entity(irn)) {
747
		const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
748
749
750
751
		return a->ent;
	}
	return NULL;
}
Sebastian Hack's avatar
Sebastian Hack committed
752

753
754
755
756
int be_get_frame_offset(const ir_node *irn)
{
	assert(is_be_node(irn));
	if (be_has_frame_entity(irn)) {
757
		const be_frame_attr_t *a = (const be_frame_attr_t*)get_irn_generic_attr_const(irn);
758
759
760
761
762
		return a->offset;
	}
	return 0;
}

763
void be_set_MemPerm_in_entity(const ir_node *irn, int n, ir_entity *ent)
Matthias Braun's avatar
Matthias Braun committed
764
{
765
	const be_memperm_attr_t *attr = (const be_memperm_attr_t*)