benode.c 32.2 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.
13
 * This file provides Perm, and Copy nodes.
Sebastian Hack's avatar
Sebastian Hack committed
14
15
16
 */
#include <stdlib.h>

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

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

40
#include "be_t.h"
Matthias Braun's avatar
Matthias Braun committed
41
#include "belive.h"
42
#include "besched.h"
43
#include "benode.h"
44
#include "bearch.h"
Sebastian Hack's avatar
Sebastian Hack committed
45

46
47
48
49
typedef struct be_node_attr_t {
	except_attr  exc;
} be_node_attr_t;

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

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

67
/** The be_Call attribute type. */
68
typedef struct {
69
	be_node_attr_t  base;
Matthias Braun's avatar
Matthias Braun committed
70
	ir_entity      *ent;        /**< called entity if this is a static call. */
71
	unsigned        pop;
Matthias Braun's avatar
Matthias Braun committed
72
	ir_type        *call_tp;    /**< call type, copied from the original Call */
73
74
} be_call_attr_t;

75
typedef struct {
76
77
78
	be_node_attr_t base;
	ir_entity    **in_entities;
	ir_entity    **out_entities;
79
	int            offset;
Matthias Braun's avatar
Matthias Braun committed
80
} be_memperm_attr_t;
81

82
static unsigned be_opcode_start;
83
ir_op *op_be_Perm;
Matthias Braun's avatar
Matthias Braun committed
84
ir_op *op_be_MemPerm;
85
86
ir_op *op_be_Copy;
ir_op *op_be_Keep;
Sebastian Hack's avatar
Sebastian Hack committed
87
ir_op *op_be_CopyKeep;
88
89
90
ir_op *op_be_Call;
ir_op *op_be_Return;
ir_op *op_be_IncSP;
Sebastian Hack's avatar
Sebastian Hack committed
91
ir_op *op_be_AddSP;
Michael Beck's avatar
Michael Beck committed
92
ir_op *op_be_SubSP;
93
ir_op *op_be_Start;
Sebastian Hack's avatar
Sebastian Hack committed
94

95
96
#define be_op_tag FOURCC('B', 'E', '\0', '\0')

Michael Beck's avatar
Michael Beck committed
97
98
99
100
101
/**
 * Compare the attributes of two be_Return nodes.
 *
 * @return zero if both nodes have identically attributes
 */
Michael Beck's avatar
Michael Beck committed
102
static int Return_cmp_attr(const ir_node *a, const ir_node *b)
103
{
104
105
	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);
106
107
108

	if (a_attr->num_ret_vals != b_attr->num_ret_vals)
		return 1;
109
110
	if (a_attr->pop != b_attr->pop)
		return 1;
111
112
	if (a_attr->emit_pop != b_attr->emit_pop)
		return 1;
113

114
	return be_nodes_equal(a, b);
115
116
}

Michael Beck's avatar
Michael Beck committed
117
118
119
120
121
/**
 * Compare the attributes of two be_IncSP nodes.
 *
 * @return zero if both nodes have identically attributes
 */
Michael Beck's avatar
Michael Beck committed
122
static int IncSP_cmp_attr(const ir_node *a, const ir_node *b)
123
{
124
125
	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);
126
127
128
129

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

130
	return be_nodes_equal(a, b);
131
132
}

Michael Beck's avatar
Michael Beck committed
133
134
135
136
137
/**
 * Compare the attributes of two be_Call nodes.
 *
 * @return zero if both nodes have identically attributes
 */
Michael Beck's avatar
Michael Beck committed
138
static int Call_cmp_attr(const ir_node *a, const ir_node *b)
139
{
140
141
	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);
142
143

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

147
	return be_nodes_equal(a, b);
Michael Beck's avatar
Michael Beck committed
148
149
}

150
static arch_register_req_t *allocate_reg_req(ir_graph *const irg)
Matthias Braun's avatar
Matthias Braun committed
151
{
152
	struct obstack *obst = be_get_be_obst(irg);
Matthias Braun's avatar
Matthias Braun committed
153

154
	arch_register_req_t *req = OALLOCZ(obst, arch_register_req_t);
155
156
	return req;
}
Matthias Braun's avatar
Matthias Braun committed
157

158
159
void be_set_constr_in(ir_node *node, int pos, const arch_register_req_t *req)
{
160
161
162
	backend_info_t *info = be_get_info(node);
	assert(pos < get_irn_arity(node));
	info->in_reqs[pos] = req;
163
}
Matthias Braun's avatar
Matthias Braun committed
164

165
166
167
168
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
169
170
}

171
/**
172
 * Initializes the generic attribute of all be nodes and return it.
173
 */
174
static void init_node_attr(ir_node *node, int n_inputs, int n_outputs)
Sebastian Hack's avatar
Sebastian Hack committed
175
{
176
177
	assert(n_outputs >= 0);

178
	ir_graph       *irg  = get_irn_irg(node);
179
	struct obstack *obst = be_get_be_obst(irg);
180
	backend_info_t *info = be_get_info(node);
181
	const arch_register_req_t **in_reqs;
Sebastian Hack's avatar
Sebastian Hack committed
182

183
184
	if (n_inputs >= 0) {
		int i;
185
186
		assert(n_inputs == get_irn_arity(node));
		in_reqs = OALLOCN(obst, const arch_register_req_t*, n_inputs);
187
		for (i = 0; i < n_inputs; ++i) {
188
			in_reqs[i] = arch_no_register_req;
189
190
		}
	} else {
191
		in_reqs = NEW_ARR_F(const arch_register_req_t*, 0);
192
	}
193
	info->in_reqs = in_reqs;
194

195
	info->out_infos = NEW_ARR_DZ(reg_out_info_t, obst, n_outputs);
196
197
	for (int i = 0; i < n_outputs; ++i) {
		info->out_infos[i].req = arch_no_register_req;
Sebastian Hack's avatar
Sebastian Hack committed
198
199
	}
}
200

201
static void add_register_req_in(ir_node *node, const arch_register_req_t *req)
202
{
203
	backend_info_t *info = be_get_info(node);
204
	ARR_APP1(const arch_register_req_t*, info->in_reqs, req);
205
206
}

207
ir_node *be_new_Perm(arch_register_class_t const *const cls, ir_node *const block, int const n, ir_node *const *const in)
Sebastian Hack's avatar
Sebastian Hack committed
208
{
209
210
211
	int            i;
	ir_graph       *irg = get_Block_irg(block);
	be_node_attr_t *attr;
212

213
	ir_node *irn = new_ir_node(NULL, irg, block, op_be_Perm, mode_T, n, in);
214
	init_node_attr(irn, n, n);
215
216
	attr                = (be_node_attr_t*) get_irn_generic_attr(irn);
	attr->exc.pin_state = op_pin_state_pinned;
217
	for (i = 0; i < n; ++i) {
218
219
220
221
222
223
		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 {
224
			arch_register_req_t *const new_req = allocate_reg_req(irg);
225
226
227
228
229
230
			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
231
232
	}

Sebastian Hack's avatar
Sebastian Hack committed
233
234
	return irn;
}
235

236
237
void be_Perm_reduce(ir_node *perm, int new_size, int *map)
{
238
239
240
241
242
	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);
243
	ir_node        **new_in;
244
	int              i;
245
246

	assert(be_is_Perm(perm));
247
	assert(new_size <= arity);
248

249
	new_in = ALLOCAN(ir_node*, new_size);
250

251
	/* save the old register data */
252
	memcpy(old_in_reqs, info->in_reqs, arity * sizeof(old_in_reqs[0]));
253
	memcpy(old_infos, info->out_infos, arity * sizeof(old_infos[0]));
254

Matthias Braun's avatar
Matthias Braun committed
255
	/* compose the new in array and set the new register data directly */
256
	for (i = 0; i < new_size; ++i) {
257
		int idx = map[i];
258
		new_in[i]          = get_irn_n(perm, idx);
259
		info->in_reqs[i]   = old_in_reqs[idx];
260
		info->out_infos[i] = old_infos[idx];
261
262
263
264
265
	}

	set_irn_in(perm, new_size, new_in);
}

266
ir_node *be_new_MemPerm(ir_node *const block, int n, ir_node *const *const in)
Matthias Braun's avatar
Matthias Braun committed
267
{
268
269
	ir_graph                     *irg       = get_Block_irg(block);
	const arch_env_t             *arch_env  = be_get_irg_arch_env(irg);
270
271
272
273
274
	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
275

276
	real_in = ALLOCAN(ir_node*, n + 1);
277
278
279
	real_in[0] = frame;
	memcpy(&real_in[1], in, n * sizeof(real_in[0]));

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

282
	init_node_attr(irn, n + 1, n);
283
	be_node_set_reg_class_in(irn, 0, sp->reg_class);
Matthias Braun's avatar
Matthias Braun committed
284

285
	attr               = (be_memperm_attr_t*)get_irn_generic_attr(irn);
286
287
	attr->in_entities  = OALLOCNZ(get_irg_obstack(irg), ir_entity*, n);
	attr->out_entities = OALLOCNZ(get_irg_obstack(irg), ir_entity*, n);
288
	attr->offset       = 0;
Matthias Braun's avatar
Matthias Braun committed
289
290
291
292

	return irn;
}

293
ir_node *be_new_Copy(ir_node *bl, ir_node *op)
Sebastian Hack's avatar
Sebastian Hack committed
294
295
296
{
	ir_node *in[1];
	ir_node *res;
297
	be_node_attr_t *attr;
298
	ir_graph *irg = get_Block_irg(bl);
299
300
	const arch_register_req_t   *in_req = arch_get_irn_register_req(op);
	const arch_register_class_t *cls    = in_req->cls;
301

Sebastian Hack's avatar
Sebastian Hack committed
302
	in[0] = op;
303
	res   = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op), 1, in);
304
	init_node_attr(res, 1, 1);
305
306
	attr = (be_node_attr_t*) get_irn_generic_attr(res);
	attr->exc.pin_state = op_pin_state_floats;
307
308
	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
309

310
	arch_register_req_t *const req = allocate_reg_req(irg);
311
	req->cls        = cls;
312
313
	req->type       = arch_register_req_type_should_be_same
		| (in_req->type & arch_register_req_type_aligned);
314
	req->other_same = 1U << 0;
315
	req->width      = in_req->width;
316
	be_set_constr_out(res, 0, req);
Matthias Braun's avatar
Matthias Braun committed
317

Sebastian Hack's avatar
Sebastian Hack committed
318
	return res;
319
320
}

321
322
ir_node *be_get_Copy_op(const ir_node *cpy)
{
323
	return get_irn_n(cpy, n_be_Copy_op);
324
325
}

326
ir_node *be_new_Keep(ir_node *const block, int const n, ir_node *const *const in)
Sebastian Hack's avatar
Sebastian Hack committed
327
{
Sebastian Hack's avatar
Sebastian Hack committed
328
	int i;
329
	ir_node *res;
330
	ir_graph *irg = get_Block_irg(block);
331
	be_node_attr_t *attr;
332

333
334
	res = new_ir_node(NULL, irg, block, op_be_Keep, mode_ANY, -1, NULL);
	init_node_attr(res, -1, 1);
335
336
	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
337

338
	for (i = 0; i < n; ++i) {
339
340
341
342
343
		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
344
	}
345
346
347
348
349
350
351
352
	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));
353
354
	add_irn_n(keep, node);
	add_register_req_in(keep, cls->class_req);
Sebastian Hack's avatar
Sebastian Hack committed
355
356
}

357
ir_node *be_new_Call(dbg_info *const dbg, ir_node *const bl, ir_node *const mem, arch_register_req_t const *const sp_req, ir_node *const sp, arch_register_req_t const *const ptr_req, ir_node *const ptr, int const n_outs, int const n, ir_node *const *const in, ir_type *const call_tp)
Sebastian Hack's avatar
Sebastian Hack committed
358
{
359
	be_call_attr_t *a;
360
	int real_n = n_be_Call_first_arg + n;
361
	ir_node **real_in = ALLOCAN(ir_node*, real_n);
Sebastian Hack's avatar
Sebastian Hack committed
362

363
364
365
366
	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
367

368
369
	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);
370
	init_node_attr(irn, real_n, n_outs);
371
372
373
374
375
	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;
376
377
	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
378
	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
379
380
}

381
382
ir_entity *be_Call_get_entity(const ir_node *call)
{
383
	const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
384
385
386
387
	assert(be_is_Call(call));
	return a->ent;
}

388
389
void be_Call_set_entity(ir_node *call, ir_entity *ent)
{
390
	be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
391
392
393
394
	assert(be_is_Call(call));
	a->ent = ent;
}

395
396
ir_type *be_Call_get_type(ir_node *call)
{
397
	const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
398
399
400
401
	assert(be_is_Call(call));
	return a->call_tp;
}

402
403
void be_Call_set_type(ir_node *call, ir_type *call_tp)
{
404
	be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
405
406
407
408
	assert(be_is_Call(call));
	a->call_tp = call_tp;
}

409
410
void be_Call_set_pop(ir_node *call, unsigned pop)
{
411
	be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
Matthias Braun's avatar
Matthias Braun committed
412
413
414
	a->pop = pop;
}

415
416
unsigned be_Call_get_pop(const ir_node *call)
{
417
	const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
Matthias Braun's avatar
Matthias Braun committed
418
419
420
	return a->pop;
}

421
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 *const in)
Sebastian Hack's avatar
Sebastian Hack committed
422
{
423
424
	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);
425
	init_node_attr(res, n, 1);
426
	be_set_constr_out(res, 0, arch_no_register_req);
427

428
	be_return_attr_t *const a = (be_return_attr_t*)get_irn_generic_attr(res);
429
430
431
432
	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
433

434
	return res;
Sebastian Hack's avatar
Sebastian Hack committed
435
436
}

437
438
int be_Return_get_n_rets(const ir_node *ret)
{
439
	const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
440
441
442
	return a->num_ret_vals;
}

443
444
unsigned be_Return_get_pop(const ir_node *ret)
{
445
	const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
446
447
448
	return a->pop;
}

449
450
int be_Return_get_emit_pop(const ir_node *ret)
{
451
	const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
452
453
454
	return a->emit_pop;
}

455
456
void be_Return_set_emit_pop(ir_node *ret, int emit_pop)
{
457
	be_return_attr_t *a = (be_return_attr_t*)get_irn_generic_attr(ret);
458
459
460
	a->emit_pop = emit_pop;
}

461
ir_node *be_new_IncSP(const arch_register_t *sp, ir_node *bl,
462
                      ir_node *old_sp, int offset, int align)
Sebastian Hack's avatar
Sebastian Hack committed
463
{
Michael Beck's avatar
Michael Beck committed
464
	be_incsp_attr_t *a;
Sebastian Hack's avatar
Sebastian Hack committed
465
	ir_node *irn;
Sebastian Hack's avatar
Sebastian Hack committed
466
	ir_node *in[1];
467
	ir_graph *irg = get_Block_irg(bl);
Sebastian Hack's avatar
Sebastian Hack committed
468
469

	in[0]     = old_sp;
470
	irn       = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode,
471
	                        ARRAY_SIZE(in), in);
472
	init_node_attr(irn, 1, 1);
473
474
475
476
	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
477
478

	/* Set output constraint to stack register. */
479
480
	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
481
482

	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
483
}
Sebastian Hack's avatar
Sebastian Hack committed
484

485
486
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
487
{
Sebastian Hack's avatar
Sebastian Hack committed
488
	ir_node *irn;
489
	ir_node *in[n_be_AddSP_last];
490
	ir_graph *irg;
491
	be_node_attr_t *attr;
Sebastian Hack's avatar
Sebastian Hack committed
492

493
494
	in[n_be_AddSP_old_sp] = old_sp;
	in[n_be_AddSP_size]   = sz;
Sebastian Hack's avatar
Sebastian Hack committed
495

496
	irg = get_Block_irg(bl);
497
498
	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);
499
500
	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
501

Sebastian Hack's avatar
Sebastian Hack committed
502
	/* Set output constraint to stack register. */
503
	be_set_constr_single_reg_in(irn, n_be_AddSP_old_sp, sp,
504
	                            arch_register_req_type_none);
505
	be_node_set_reg_class_in(irn, n_be_AddSP_size, sp->reg_class);
Matthias Braun's avatar
Matthias Braun committed
506
507
	be_set_constr_single_reg_out(irn, pn_be_AddSP_sp, sp,
	                             arch_register_req_type_produces_sp);
508

Sebastian Hack's avatar
Sebastian Hack committed
509
510
511
	return irn;
}

512
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
513
514
{
	ir_node *irn;
515
	ir_node *in[n_be_SubSP_last];
516
	ir_graph *irg;
517
	be_node_attr_t *attr;
Michael Beck's avatar
Michael Beck committed
518

519
520
	in[n_be_SubSP_old_sp] = old_sp;
	in[n_be_SubSP_size]   = sz;
Michael Beck's avatar
Michael Beck committed
521

522
	irg = get_Block_irg(bl);
523
524
	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);
525
526
	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
527
528

	/* Set output constraint to stack register. */
529
	be_set_constr_single_reg_in(irn, n_be_SubSP_old_sp, sp,
530
	                            arch_register_req_type_none);
531
	be_node_set_reg_class_in(irn, n_be_SubSP_size, sp->reg_class);
532
	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
533
534
535
536

	return irn;
}

537
ir_node *be_new_Start(dbg_info *dbgi, ir_node *bl, int n_outs)
Sebastian Hack's avatar
Sebastian Hack committed
538
{
539
	ir_node *res;
540
	ir_graph *irg = get_Block_irg(bl);
541
	be_node_attr_t *attr;
Sebastian Hack's avatar
Sebastian Hack committed
542

543
	res = new_ir_node(dbgi, irg, bl, op_be_Start, mode_T, 0, NULL);
544
	init_node_attr(res, 0, n_outs);
545
546
	attr = (be_node_attr_t*) get_irn_generic_attr(res);
	attr->exc.pin_state = op_pin_state_pinned;
547
548
549
550

	return res;
}

551
ir_node *be_new_CopyKeep(ir_node *bl, ir_node *src, int n, ir_node *in_keep[])
Sebastian Hack's avatar
Sebastian Hack committed
552
{
553
554
	ir_node  *irn;
	ir_node **in = ALLOCAN(ir_node*, n + 1);
555
	ir_graph *irg = get_Block_irg(bl);
556
557
558
	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);
559
	be_node_attr_t *attr;
Sebastian Hack's avatar
Sebastian Hack committed
560
561
562
563

	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);
564
	init_node_attr(irn, n + 1, 1);
565
566
	attr = (be_node_attr_t*) get_irn_generic_attr(irn);
	attr->exc.pin_state = op_pin_state_floats;
567
568
	be_node_set_reg_class_in(irn, 0, cls);
	be_node_set_reg_class_out(irn, 0, cls);
569
570
571
572
573
574
	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
575
576
577
578

	return irn;
}

579
ir_node *be_new_CopyKeep_single(ir_node *bl, ir_node *src, ir_node *keep)
Sebastian Hack's avatar
Sebastian Hack committed
580
{
581
	return be_new_CopyKeep(bl, src, 1, &keep);
Sebastian Hack's avatar
Sebastian Hack committed
582
583
}

584
585
ir_node *be_get_CopyKeep_op(const ir_node *cpy)
{
586
	return get_irn_n(cpy, n_be_CopyKeep_op);
587
588
}

589
590
void be_set_CopyKeep_op(ir_node *cpy, ir_node *op)
{
591
	set_irn_n(cpy, n_be_CopyKeep_op, op);
592
593
}

594
void be_set_MemPerm_in_entity(const ir_node *irn, int n, ir_entity *ent)
Matthias Braun's avatar
Matthias Braun committed
595
{
596
	const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
597
598

	assert(be_is_MemPerm(irn));
599
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
600
601
602
603

	attr->in_entities[n] = ent;
}

604
ir_entity* be_get_MemPerm_in_entity(const ir_node* irn, int n)
Matthias Braun's avatar
Matthias Braun committed
605
{
606
	const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
607
608

	assert(be_is_MemPerm(irn));
609
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
610
611
612
613

	return attr->in_entities[n];
}

614
void be_set_MemPerm_out_entity(const ir_node *irn, int n, ir_entity *ent)
Matthias Braun's avatar
Matthias Braun committed
615
{
616
	const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
617
618

	assert(be_is_MemPerm(irn));
619
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
620
621
622
623

	attr->out_entities[n] = ent;
}

624
ir_entity* be_get_MemPerm_out_entity(const ir_node* irn, int n)
Matthias Braun's avatar
Matthias Braun committed
625
{
626
	const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
627
628

	assert(be_is_MemPerm(irn));
629
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
630
631
632
633

	return attr->out_entities[n];
}

634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
void be_set_MemPerm_offset(ir_node *irn, int offset)
{
	be_memperm_attr_t *attr = (be_memperm_attr_t*)get_irn_generic_attr(irn);

	assert(be_is_MemPerm(irn));
	attr->offset = offset;
}

int be_get_MemPerm_offset(const ir_node *irn)
{
	const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);

	assert(be_is_MemPerm(irn));
	return attr->offset;
}

650
651
652
653
654
int be_get_MemPerm_entity_arity(const ir_node *irn)
{
	return get_irn_arity(irn) - 1;
}

655
const arch_register_req_t *be_create_reg_req(struct obstack *obst,
656
		const arch_register_t *reg, arch_register_req_type_t additional_types)
Sebastian Hack's avatar
Sebastian Hack committed
657
{
658
	arch_register_req_t         *req = OALLOC(obst, arch_register_req_t);
659
	const arch_register_class_t *cls = reg->reg_class;
660
	unsigned                    *limited_bitset;
Sebastian Hack's avatar
Sebastian Hack committed
661

Matthias Braun's avatar
Matthias Braun committed
662
	limited_bitset = rbitset_obstack_alloc(obst, arch_register_class_n_regs(cls));
663
	rbitset_set(limited_bitset, reg->index);
Sebastian Hack's avatar
Sebastian Hack committed
664

665
666
	req->type    = arch_register_req_type_limited | additional_types;
	req->cls     = cls;
Matthias Braun's avatar
Matthias Braun committed
667
	req->limited = limited_bitset;
668
	req->width   = 1;
669
	return req;
670
671
672
673
674
}

void be_set_constr_single_reg_in(ir_node *node, int pos,
		const arch_register_t *reg, arch_register_req_type_t additional_types)
{
675
	const arch_register_req_t *req;
676

677
678
679
680
	if (additional_types == 0) {
		req = reg->single_req;
	} else {
		ir_graph       *irg  = get_irn_irg(node);
681
		struct obstack *obst = be_get_be_obst(irg);
682
		req = be_create_reg_req(obst, reg, additional_types);
683
684
	}
	be_set_constr_in(node, pos, req);
685
686
687
688
689
}

void be_set_constr_single_reg_out(ir_node *node, int pos,
		const arch_register_t *reg, arch_register_req_type_t additional_types)
{
690
691
	ir_graph                  *irg  = get_irn_irg(node);
	be_irg_t                  *birg = be_birg_from_irg(irg);
692
	const arch_register_req_t *req;
693
694

	/* if we have an ignore register, add ignore flag and just assign it */
695
	if (!rbitset_is_set(birg->allocatable_regs, reg->global_index)) {
696
697
698
		additional_types |= arch_register_req_type_ignore;
	}

699
700
701
	if (additional_types == 0) {
		req = reg->single_req;
	} else {
702
		struct obstack *obst = be_get_be_obst(irg);
703
		req = be_create_reg_req(obst, reg, additional_types);
704
705
	}

706
	arch_set_irn_register_out(node, pos, reg);
707
	be_set_constr_out(node, pos, req);
Sebastian Hack's avatar
Sebastian Hack committed
708
709
}

710
711
void be_node_set_reg_class_in(ir_node *irn, int pos,
                              const arch_register_class_t *cls)
712
{
713
	be_set_constr_in(irn, pos, cls->class_req);
714
715
}

716
717
void be_node_set_reg_class_out(ir_node *irn, int pos,
                               const arch_register_class_t *cls)
Sebastian Hack's avatar
Sebastian Hack committed
718
{
719
	be_set_constr_out(irn, pos, cls->class_req);
Sebastian Hack's avatar
Sebastian Hack committed
720
721
}

722
723
ir_node *be_get_IncSP_pred(ir_node *irn)
{
Michael Beck's avatar
BugFix:    
Michael Beck committed
724
725
726
727
	assert(be_is_IncSP(irn));
	return get_irn_n(irn, 0);
}

728
729
void be_set_IncSP_pred(ir_node *incsp, ir_node *pred)
{
730
731
732
733
	assert(be_is_IncSP(incsp));
	set_irn_n(incsp, 0, pred);
}

734
void be_set_IncSP_offset(ir_node *irn, int offset)
Sebastian Hack's avatar
Sebastian Hack committed
735
{
736
	be_incsp_attr_t *a = (be_incsp_attr_t*)get_irn_generic_attr(irn);
Sebastian Hack's avatar
Sebastian Hack committed
737
738
739
	assert(be_is_IncSP(irn));
	a->offset = offset;
}
740

741
int be_get_IncSP_offset(const ir_node *irn)
Sebastian Hack's avatar
Sebastian Hack committed
742
{
743
	const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
Sebastian Hack's avatar
Sebastian Hack committed
744
745
746
	assert(be_is_IncSP(irn));
	return a->offset;
}
Sebastian Hack's avatar
Sebastian Hack committed
747

748
749
int be_get_IncSP_align(const ir_node *irn)
{
750
	const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
751
752
753
754
	assert(be_is_IncSP(irn));
	return a->align;
}

755
static ir_entity *be_node_get_frame_entity(const ir_node *irn)
756
{
757
	if (be_is_MemPerm(irn)) {
758
759
760
		return be_get_MemPerm_in_entity(irn, 0);
	}
	return NULL;
761
762
}

763
static void be_node_set_frame_offset(ir_node *irn, int offset)
764
{
765
766
767
	if (be_is_MemPerm(irn)) {
		be_set_MemPerm_offset(irn, offset);
	}
768
769
}

770
static int be_node_get_sp_bias(const ir_node *irn)
771
{
772
	assert(!be_is_Call(irn));
773
	if (be_is_IncSP(irn))
Matthias Braun's avatar
Matthias Braun committed
774
775
		return be_get_IncSP_offset(irn);
	return 0;
776
777
}

778
/* for be nodes */
Matthias Braun's avatar
Matthias Braun committed
779
static const arch_irn_ops_t be_node_irn_ops = {
780
	be_node_get_frame_entity,
781
	be_node_set_frame_offset,
782
	be_node_get_sp_bias,
783
784
785
	NULL,    /* get_op_estimated_cost   */
	NULL,    /* possible_memory_operand */
	NULL,    /* perform_memory_operand  */
Sebastian Hack's avatar
Sebastian Hack committed
786
};