benode.c 31.7 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
typedef struct be_node_attr_t {
Matthias Braun's avatar
Matthias Braun committed
47
	except_attr exc;
48
49
} 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
	int            offset; /**< The offset by which the stack shall be
	                            expanded/shrinked. */
	int            align;  /**< alignment after the IncSP (0=no alignment) */
Michael Beck's avatar
Michael Beck committed
64
} be_incsp_attr_t;
Sebastian Hack's avatar
Sebastian Hack committed
65

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

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

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

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

Michael Beck's avatar
Michael Beck committed
96
97
98
/**
 * Compare the attributes of two be_Return nodes.
 */
99
static int be_return_attrs_equal(const ir_node *a, const ir_node *b)
100
{
101
102
103
104
105
	const be_return_attr_t *attr_a = (const be_return_attr_t*)get_irn_generic_attr_const(a);
	const be_return_attr_t *attr_b = (const be_return_attr_t*)get_irn_generic_attr_const(b);
	return attr_a->num_ret_vals == attr_b->num_ret_vals
	    && attr_a->pop == attr_b->pop && attr_a->emit_pop == attr_b->emit_pop
	    && attrs_equal_be_node(a, b);
106
107
}

Michael Beck's avatar
Michael Beck committed
108
109
110
/**
 * Compare the attributes of two be_IncSP nodes.
 */
111
static int be_incsp_attrs_equal(const ir_node *a, const ir_node *b)
112
{
113
114
115
	const be_incsp_attr_t *attr_a = (const be_incsp_attr_t*)get_irn_generic_attr_const(a);
	const be_incsp_attr_t *attr_b = (const be_incsp_attr_t*)get_irn_generic_attr_const(b);
	return attr_a->offset == attr_b->offset && attrs_equal_be_node(a, b);
116
117
}

Michael Beck's avatar
Michael Beck committed
118
119
120
/**
 * Compare the attributes of two be_Call nodes.
 */
121
static int be_call_attrs_equal(const ir_node *a, const ir_node *b)
122
{
123
124
125
126
	const be_call_attr_t *attr_a = (const be_call_attr_t*)get_irn_generic_attr_const(a);
	const be_call_attr_t *attr_b = (const be_call_attr_t*)get_irn_generic_attr_const(b);
	return attr_a->ent == attr_b->ent && attr_a->call_tp == attr_b->call_tp
	    && attrs_equal_be_node(a, b);
Michael Beck's avatar
Michael Beck committed
127
128
}

129
static arch_register_req_t *allocate_reg_req(ir_graph *const irg)
Matthias Braun's avatar
Matthias Braun committed
130
{
131
	struct obstack *obst = be_get_be_obst(irg);
Matthias Braun's avatar
Matthias Braun committed
132

133
	arch_register_req_t *req = OALLOCZ(obst, arch_register_req_t);
134
135
	return req;
}
Matthias Braun's avatar
Matthias Braun committed
136

137
138
void be_set_constr_in(ir_node *node, int pos, const arch_register_req_t *req)
{
139
140
141
	backend_info_t *info = be_get_info(node);
	assert(pos < get_irn_arity(node));
	info->in_reqs[pos] = req;
142
}
Matthias Braun's avatar
Matthias Braun committed
143

144
145
146
147
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
148
149
}

150
/**
151
 * Initializes the generic attribute of all be nodes and return it.
152
 */
Matthias Braun's avatar
Matthias Braun committed
153
154
static void init_node_attr(ir_node *node, int n_inputs, int n_outputs,
                           arch_irn_flags_t flags)
Sebastian Hack's avatar
Sebastian Hack committed
155
{
156
	assert(n_outputs >= 0);
157
	ir_graph       *irg  = get_irn_irg(node);
158
	struct obstack *obst = be_get_be_obst(irg);
159
	backend_info_t *info = be_get_info(node);
Sebastian Hack's avatar
Sebastian Hack committed
160

Matthias Braun's avatar
Matthias Braun committed
161
	const arch_register_req_t **in_reqs;
162
163
	if (n_inputs >= 0) {
		int i;
164
165
		assert(n_inputs == get_irn_arity(node));
		in_reqs = OALLOCN(obst, const arch_register_req_t*, n_inputs);
166
		for (i = 0; i < n_inputs; ++i) {
167
			in_reqs[i] = arch_no_register_req;
168
169
		}
	} else {
170
		in_reqs = NEW_ARR_F(const arch_register_req_t*, 0);
171
	}
172
	info->in_reqs = in_reqs;
173

174
	info->out_infos = NEW_ARR_DZ(reg_out_info_t, obst, n_outputs);
175
176
	for (int i = 0; i < n_outputs; ++i) {
		info->out_infos[i].req = arch_no_register_req;
Sebastian Hack's avatar
Sebastian Hack committed
177
	}
Matthias Braun's avatar
Matthias Braun committed
178
	info->flags = flags;
Sebastian Hack's avatar
Sebastian Hack committed
179
}
180

181
static void add_register_req_in(ir_node *node, const arch_register_req_t *req)
182
{
183
	backend_info_t *info = be_get_info(node);
184
	ARR_APP1(const arch_register_req_t*, info->in_reqs, req);
185
186
}

187
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
188
{
189
	ir_graph *irg = get_irn_irg(block);
Matthias Braun's avatar
Matthias Braun committed
190
	ir_node  *irn = new_ir_node(NULL, irg, block, op_be_Perm, mode_T, n, in);
Matthias Braun's avatar
Matthias Braun committed
191
	init_node_attr(irn, n, n, arch_irn_flags_none);
Matthias Braun's avatar
Matthias Braun committed
192
	be_node_attr_t *attr = (be_node_attr_t*)get_irn_generic_attr(irn);
193
	attr->exc.pin_state = op_pin_state_pinned;
Matthias Braun's avatar
Matthias Braun committed
194
	for (int i = 0; i < n; ++i) {
195
196
197
198
199
200
		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 {
201
			arch_register_req_t *const new_req = allocate_reg_req(irg);
202
203
204
205
206
207
			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
208
209
	}

Sebastian Hack's avatar
Sebastian Hack committed
210
211
	return irn;
}
212

213
214
215
void be_Perm_reduce(ir_node *perm, int new_size, int *map)
{
	assert(be_is_Perm(perm));
Matthias Braun's avatar
Matthias Braun committed
216
	int arity = get_irn_arity(perm);
217
	assert(new_size <= arity);
Matthias Braun's avatar
Matthias Braun committed
218
219
220
221
222
	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);
	ir_node       **new_in    = ALLOCAN(ir_node*, new_size);
223

224
	/* save the old register data */
225
	memcpy(old_in_reqs, info->in_reqs, arity * sizeof(old_in_reqs[0]));
226
	memcpy(old_infos, info->out_infos, arity * sizeof(old_infos[0]));
227

Matthias Braun's avatar
Matthias Braun committed
228
	/* compose the new in array and set the new register data directly */
Matthias Braun's avatar
Matthias Braun committed
229
	for (int i = 0; i < new_size; ++i) {
230
		int idx = map[i];
231
		new_in[i]          = get_irn_n(perm, idx);
232
		info->in_reqs[i]   = old_in_reqs[idx];
233
		info->out_infos[i] = old_infos[idx];
234
235
236
237
	}
	set_irn_in(perm, new_size, new_in);
}

238
ir_node *be_new_MemPerm(ir_node *const block, int n, ir_node *const *const in)
Matthias Braun's avatar
Matthias Braun committed
239
{
240
	ir_graph              *irg      = get_irn_irg(block);
Matthias Braun's avatar
Matthias Braun committed
241
242
243
	const arch_env_t      *arch_env = be_get_irg_arch_env(irg);
	ir_node               *frame    = get_irg_frame(irg);
	const arch_register_t *sp       = arch_env->sp;
Matthias Braun's avatar
Matthias Braun committed
244

Matthias Braun's avatar
Matthias Braun committed
245
	ir_node **real_in = ALLOCAN(ir_node*, n + 1);
246
247
248
	real_in[0] = frame;
	memcpy(&real_in[1], in, n * sizeof(real_in[0]));

Matthias Braun's avatar
Matthias Braun committed
249
	ir_node *irn = new_ir_node(NULL, irg, block, op_be_MemPerm, mode_T, n+1, real_in);
250

Matthias Braun's avatar
Matthias Braun committed
251
	init_node_attr(irn, n + 1, n, arch_irn_flags_none);
252
	be_node_set_reg_class_in(irn, 0, sp->reg_class);
Matthias Braun's avatar
Matthias Braun committed
253

Matthias Braun's avatar
Matthias Braun committed
254
	be_memperm_attr_t *attr = (be_memperm_attr_t*)get_irn_generic_attr(irn);
255
256
	attr->in_entities  = OALLOCNZ(get_irg_obstack(irg), ir_entity*, n);
	attr->out_entities = OALLOCNZ(get_irg_obstack(irg), ir_entity*, n);
257
	attr->offset       = 0;
Matthias Braun's avatar
Matthias Braun committed
258
259
260
	return irn;
}

261
ir_node *be_new_Copy(ir_node *bl, ir_node *op)
Sebastian Hack's avatar
Sebastian Hack committed
262
{
263
	ir_graph *irg  = get_irn_irg(bl);
Matthias Braun's avatar
Matthias Braun committed
264
265
266
	ir_node  *in[] = { op };
	ir_node  *res  = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op),
	                             ARRAY_SIZE(in), in);
Matthias Braun's avatar
Matthias Braun committed
267
	init_node_attr(res, 1, 1, arch_irn_flags_none);
Matthias Braun's avatar
Matthias Braun committed
268
	be_node_attr_t *attr = (be_node_attr_t*) get_irn_generic_attr(res);
269
	attr->exc.pin_state = op_pin_state_floats;
Matthias Braun's avatar
Matthias Braun committed
270
271
272

	const arch_register_req_t   *in_req = arch_get_irn_register_req(op);
	const arch_register_class_t *cls    = in_req->cls;
273
274
	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
275

276
	arch_register_req_t *const req = allocate_reg_req(irg);
Matthias Braun's avatar
Matthias Braun committed
277
278
279
	req->cls  = cls;
	req->type = arch_register_req_type_should_be_same
	            | (in_req->type & arch_register_req_type_aligned);
280
	req->other_same = 1U << 0;
281
	req->width      = in_req->width;
282
	be_set_constr_out(res, 0, req);
Sebastian Hack's avatar
Sebastian Hack committed
283
	return res;
284
285
}

286
287
ir_node *be_get_Copy_op(const ir_node *cpy)
{
288
	return get_irn_n(cpy, n_be_Copy_op);
289
290
}

291
ir_node *be_new_Keep(ir_node *const block, int const n, ir_node *const *const in)
Sebastian Hack's avatar
Sebastian Hack committed
292
{
293
	ir_graph *irg = get_irn_irg(block);
Matthias Braun's avatar
Matthias Braun committed
294
	ir_node  *res = new_ir_node(NULL, irg, block, op_be_Keep, mode_ANY, -1, NULL);
Matthias Braun's avatar
Matthias Braun committed
295
	init_node_attr(res, -1, 1, arch_irn_flag_schedule_first);
Matthias Braun's avatar
Matthias Braun committed
296
	be_node_attr_t *attr = (be_node_attr_t*) get_irn_generic_attr(res);
297
	attr->exc.pin_state = op_pin_state_pinned;
Sebastian Hack's avatar
Sebastian Hack committed
298

Matthias Braun's avatar
Matthias Braun committed
299
	for (int i = 0; i < n; ++i) {
300
301
302
303
304
		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
305
	}
306
307
308
309
310
311
312
	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));
313
314
	add_irn_n(keep, node);
	add_register_req_in(keep, cls->class_req);
Sebastian Hack's avatar
Sebastian Hack committed
315
316
}

Matthias Braun's avatar
Matthias Braun committed
317
318
319
320
321
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
322
{
Matthias Braun's avatar
Matthias Braun committed
323
	int       real_n  = n_be_Call_first_arg + n;
324
	ir_node **real_in = ALLOCAN(ir_node*, real_n);
Sebastian Hack's avatar
Sebastian Hack committed
325

326
327
328
329
	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
330

331
	ir_graph *const irg = get_irn_irg(bl);
332
	ir_node  *const irn = new_ir_node(dbg, irg, bl, op_be_Call, mode_T, real_n, real_in);
Matthias Braun's avatar
Matthias Braun committed
333
	init_node_attr(irn, real_n, n_outs, arch_irn_flags_none);
Matthias Braun's avatar
Matthias Braun committed
334
	be_call_attr_t *a     = (be_call_attr_t*)get_irn_generic_attr(irn);
335
336
337
338
	a->ent                = NULL;
	a->call_tp            = call_tp;
	a->pop                = 0;
	a->base.exc.pin_state = op_pin_state_pinned;
339
340
	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
341
	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
342
343
}

344
345
ir_entity *be_Call_get_entity(const ir_node *call)
{
346
	const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
347
348
349
350
	assert(be_is_Call(call));
	return a->ent;
}

351
352
void be_Call_set_entity(ir_node *call, ir_entity *ent)
{
353
	be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
354
355
356
357
	assert(be_is_Call(call));
	a->ent = ent;
}

358
359
ir_type *be_Call_get_type(ir_node *call)
{
360
	const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
361
362
363
364
	assert(be_is_Call(call));
	return a->call_tp;
}

365
366
void be_Call_set_type(ir_node *call, ir_type *call_tp)
{
367
	be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
368
369
370
371
	assert(be_is_Call(call));
	a->call_tp = call_tp;
}

372
373
void be_Call_set_pop(ir_node *call, unsigned pop)
{
374
	be_call_attr_t *a = (be_call_attr_t*)get_irn_generic_attr(call);
Matthias Braun's avatar
Matthias Braun committed
375
376
377
	a->pop = pop;
}

378
379
unsigned be_Call_get_pop(const ir_node *call)
{
380
	const be_call_attr_t *a = (const be_call_attr_t*)get_irn_generic_attr_const(call);
Matthias Braun's avatar
Matthias Braun committed
381
382
383
	return a->pop;
}

Matthias Braun's avatar
Matthias Braun committed
384
385
386
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
387
{
388
	ir_graph *const irg = get_irn_irg(block);
389
	ir_node  *const res = new_ir_node(dbg, irg, block, op_be_Return, mode_X, n, in);
Matthias Braun's avatar
Matthias Braun committed
390
	init_node_attr(res, n, 1, arch_irn_flags_none);
391
	be_set_constr_out(res, 0, arch_no_register_req);
392

393
	be_return_attr_t *const a = (be_return_attr_t*)get_irn_generic_attr(res);
394
395
396
397
	a->num_ret_vals       = n_res;
	a->pop                = pop;
	a->emit_pop           = 0;
	a->base.exc.pin_state = op_pin_state_pinned;
398
	return res;
Sebastian Hack's avatar
Sebastian Hack committed
399
400
}

401
402
int be_Return_get_n_rets(const ir_node *ret)
{
403
	const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
404
405
406
	return a->num_ret_vals;
}

407
408
unsigned be_Return_get_pop(const ir_node *ret)
{
409
	const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
410
411
412
	return a->pop;
}

413
414
int be_Return_get_emit_pop(const ir_node *ret)
{
415
	const be_return_attr_t *a = (const be_return_attr_t*)get_irn_generic_attr_const(ret);
416
417
418
	return a->emit_pop;
}

419
420
void be_Return_set_emit_pop(ir_node *ret, int emit_pop)
{
421
	be_return_attr_t *a = (be_return_attr_t*)get_irn_generic_attr(ret);
422
423
424
	a->emit_pop = emit_pop;
}

425
ir_node *be_new_IncSP(const arch_register_t *sp, ir_node *bl,
426
                      ir_node *old_sp, int offset, int align)
Sebastian Hack's avatar
Sebastian Hack committed
427
{
428
	ir_graph *irg = get_irn_irg(bl);
Matthias Braun's avatar
Matthias Braun committed
429
430
431
	ir_node  *in[] = { old_sp };
	ir_node  *irn  = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode,
	                             ARRAY_SIZE(in), in);
Matthias Braun's avatar
Matthias Braun committed
432
	init_node_attr(irn, 1, 1, arch_irn_flags_none);
Matthias Braun's avatar
Matthias Braun committed
433
	be_incsp_attr_t *a    = (be_incsp_attr_t*)get_irn_generic_attr(irn);
434
435
436
	a->offset             = offset;
	a->align              = align;
	a->base.exc.pin_state = op_pin_state_pinned;
Sebastian Hack's avatar
Sebastian Hack committed
437
438

	/* Set output constraint to stack register. */
439
440
	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
441
	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
442
}
Sebastian Hack's avatar
Sebastian Hack committed
443

444
ir_node *be_new_AddSP(const arch_register_t *sp, ir_node *bl, ir_node *old_sp,
Matthias Braun's avatar
Matthias Braun committed
445
446
                      ir_node *sz)
{
447
	ir_graph *irg  = get_irn_irg(bl);
Matthias Braun's avatar
Matthias Braun committed
448
449
450
451
	ir_node  *in[] = { old_sp, sz };
	assert(ARRAY_SIZE(in) == n_be_AddSP_max+1);
	ir_node  *irn  = new_ir_node(NULL, irg, bl, op_be_AddSP, mode_T,
	                             ARRAY_SIZE(in), in);
Matthias Braun's avatar
Matthias Braun committed
452
	init_node_attr(irn, ARRAY_SIZE(in), pn_be_AddSP_max+1, arch_irn_flags_none);
Matthias Braun's avatar
Matthias Braun committed
453
	be_node_attr_t *attr = (be_node_attr_t*)get_irn_generic_attr(irn);
454
	attr->exc.pin_state = op_pin_state_pinned;
Sebastian Hack's avatar
Sebastian Hack committed
455

Sebastian Hack's avatar
Sebastian Hack committed
456
	/* Set output constraint to stack register. */
457
	be_set_constr_single_reg_in(irn, n_be_AddSP_old_sp, sp,
458
	                            arch_register_req_type_none);
459
	be_node_set_reg_class_in(irn, n_be_AddSP_size, sp->reg_class);
Matthias Braun's avatar
Matthias Braun committed
460
461
	be_set_constr_single_reg_out(irn, pn_be_AddSP_sp, sp,
	                             arch_register_req_type_produces_sp);
Sebastian Hack's avatar
Sebastian Hack committed
462
463
464
	return irn;
}

465
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
466
{
467
	ir_graph *irg  = get_irn_irg(bl);
Matthias Braun's avatar
Matthias Braun committed
468
469
470
471
	ir_node  *in[] = { old_sp, sz };
	assert(ARRAY_SIZE(in) == n_be_SubSP_max+1);
	ir_node  *irn = new_ir_node(NULL, irg, bl, op_be_SubSP, mode_T,
	                            ARRAY_SIZE(in), in);
Matthias Braun's avatar
Matthias Braun committed
472
	init_node_attr(irn, ARRAY_SIZE(in), pn_be_SubSP_max+1, arch_irn_flags_none);
Matthias Braun's avatar
Matthias Braun committed
473
	be_node_attr_t *attr = (be_node_attr_t*)get_irn_generic_attr(irn);
474
	attr->exc.pin_state = op_pin_state_pinned;
Michael Beck's avatar
Michael Beck committed
475
476

	/* Set output constraint to stack register. */
477
	be_set_constr_single_reg_in(irn, n_be_SubSP_old_sp, sp,
478
	                            arch_register_req_type_none);
479
	be_node_set_reg_class_in(irn, n_be_SubSP_size, sp->reg_class);
480
	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
481
482
483
	return irn;
}

484
ir_node *be_new_Start(dbg_info *dbgi, ir_node *bl, int n_outs)
Sebastian Hack's avatar
Sebastian Hack committed
485
{
486
	ir_graph *irg = get_irn_irg(bl);
Matthias Braun's avatar
Matthias Braun committed
487
	ir_node  *res = new_ir_node(dbgi, irg, bl, op_be_Start, mode_T, 0, NULL);
Matthias Braun's avatar
Matthias Braun committed
488
	init_node_attr(res, 0, n_outs, arch_irn_flag_schedule_first);
Matthias Braun's avatar
Matthias Braun committed
489
	be_node_attr_t *attr = (be_node_attr_t*) get_irn_generic_attr(res);
490
	attr->exc.pin_state = op_pin_state_pinned;
491
492
493
	return res;
}

494
ir_node *be_new_CopyKeep(ir_node *bl, ir_node *src, int n, ir_node *in_keep[])
Sebastian Hack's avatar
Sebastian Hack committed
495
{
Matthias Braun's avatar
Matthias Braun committed
496
	ir_mode  *mode  = get_irn_mode(src);
497
	ir_graph *irg   = get_irn_irg(bl);
Matthias Braun's avatar
Matthias Braun committed
498
499
	int       arity = n+1;
	ir_node **in    = ALLOCAN(ir_node*, arity);
Sebastian Hack's avatar
Sebastian Hack committed
500
501
	in[0] = src;
	memcpy(&in[1], in_keep, n * sizeof(in[0]));
Matthias Braun's avatar
Matthias Braun committed
502
	ir_node *irn = new_ir_node(NULL, irg, bl, op_be_CopyKeep, mode, arity, in);
Matthias Braun's avatar
Matthias Braun committed
503
	init_node_attr(irn, arity, 1, arch_irn_flag_schedule_first);
Matthias Braun's avatar
Matthias Braun committed
504
	be_node_attr_t *attr = (be_node_attr_t*)get_irn_generic_attr(irn);
505
	attr->exc.pin_state = op_pin_state_floats;
Matthias Braun's avatar
Matthias Braun committed
506
507
	const arch_register_req_t   *req  = arch_get_irn_register_req(src);
	const arch_register_class_t *cls  = req->cls;
508
509
	be_node_set_reg_class_in(irn, 0, cls);
	be_node_set_reg_class_out(irn, 0, cls);
510
511
512
513
514
515
	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
516
517
518
	return irn;
}

519
ir_node *be_new_CopyKeep_single(ir_node *bl, ir_node *src, ir_node *keep)
Sebastian Hack's avatar
Sebastian Hack committed
520
{
521
	return be_new_CopyKeep(bl, src, 1, &keep);
Sebastian Hack's avatar
Sebastian Hack committed
522
523
}

524
525
ir_node *be_get_CopyKeep_op(const ir_node *cpy)
{
526
	return get_irn_n(cpy, n_be_CopyKeep_op);
527
528
}

529
530
void be_set_CopyKeep_op(ir_node *cpy, ir_node *op)
{
531
	set_irn_n(cpy, n_be_CopyKeep_op, op);
532
533
}

534
void be_set_MemPerm_in_entity(const ir_node *irn, int n, ir_entity *ent)
Matthias Braun's avatar
Matthias Braun committed
535
{
536
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
537
	const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
538
539
540
	attr->in_entities[n] = ent;
}

541
ir_entity* be_get_MemPerm_in_entity(const ir_node* irn, int n)
Matthias Braun's avatar
Matthias Braun committed
542
{
543
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
544
	const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
545
546
547
	return attr->in_entities[n];
}

548
void be_set_MemPerm_out_entity(const ir_node *irn, int n, ir_entity *ent)
Matthias Braun's avatar
Matthias Braun committed
549
{
550
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
551
	const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
552
553
554
	attr->out_entities[n] = ent;
}

555
ir_entity* be_get_MemPerm_out_entity(const ir_node* irn, int n)
Matthias Braun's avatar
Matthias Braun committed
556
{
557
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
558
	const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
559
560
561
	return attr->out_entities[n];
}

562
563
564
void be_set_MemPerm_offset(ir_node *irn, int offset)
{
	assert(be_is_MemPerm(irn));
Matthias Braun's avatar
Matthias Braun committed
565
	be_memperm_attr_t *attr = (be_memperm_attr_t*)get_irn_generic_attr(irn);
566
567
568
569
570
571
	attr->offset = offset;
}

int be_get_MemPerm_offset(const ir_node *irn)
{
	assert(be_is_MemPerm(irn));
Matthias Braun's avatar
Matthias Braun committed
572
	const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
573
574
575
	return attr->offset;
}

576
577
int be_get_MemPerm_entity_arity(const ir_node *irn)
{
Matthias Braun's avatar
Matthias Braun committed
578
	assert(be_is_MemPerm(irn));
579
580
581
	return get_irn_arity(irn) - 1;
}

582
const arch_register_req_t *be_create_reg_req(struct obstack *obst,
583
		const arch_register_t *reg, arch_register_req_type_t additional_types)
Sebastian Hack's avatar
Sebastian Hack committed
584
{
Matthias Braun's avatar
Matthias Braun committed
585
586
587
588
589
	const arch_register_class_t *cls     = reg->reg_class;
	unsigned                     n_regs  = arch_register_class_n_regs(cls);
	unsigned                    *limited = rbitset_obstack_alloc(obst, n_regs);
	rbitset_set(limited, reg->index);
	arch_register_req_t *req = OALLOC(obst, arch_register_req_t);
590
591
	req->type    = arch_register_req_type_limited | additional_types;
	req->cls     = cls;
Matthias Braun's avatar
Matthias Braun committed
592
	req->limited = limited;
593
	req->width   = 1;
594
	return req;
595
596
597
598
599
}

void be_set_constr_single_reg_in(ir_node *node, int pos,
		const arch_register_t *reg, arch_register_req_type_t additional_types)
{
600
601
602
603
604
	const arch_register_req_t *req;
	if (additional_types == 0) {
		req = reg->single_req;
	} else {
		ir_graph       *irg  = get_irn_irg(node);
605
		struct obstack *obst = be_get_be_obst(irg);
606
		req = be_create_reg_req(obst, reg, additional_types);
607
608
	}
	be_set_constr_in(node, pos, req);
609
610
611
612
613
}

void be_set_constr_single_reg_out(ir_node *node, int pos,
		const arch_register_t *reg, arch_register_req_type_t additional_types)
{
614
615
	ir_graph                  *irg  = get_irn_irg(node);
	be_irg_t                  *birg = be_birg_from_irg(irg);
616
617

	/* if we have an ignore register, add ignore flag and just assign it */
Matthias Braun's avatar
Matthias Braun committed
618
	if (!rbitset_is_set(birg->allocatable_regs, reg->global_index))
619
620
		additional_types |= arch_register_req_type_ignore;

Matthias Braun's avatar
Matthias Braun committed
621
	const arch_register_req_t *req;
622
623
624
	if (additional_types == 0) {
		req = reg->single_req;
	} else {
625
		struct obstack *obst = be_get_be_obst(irg);
626
		req = be_create_reg_req(obst, reg, additional_types);
627
628
	}

629
	arch_set_irn_register_out(node, pos, reg);
630
	be_set_constr_out(node, pos, req);
Sebastian Hack's avatar
Sebastian Hack committed
631
632
}

633
634
void be_node_set_reg_class_in(ir_node *irn, int pos,
                              const arch_register_class_t *cls)
635
{
636
	be_set_constr_in(irn, pos, cls->class_req);
637
638
}

639
640
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
641
{
642
	be_set_constr_out(irn, pos, cls->class_req);
Sebastian Hack's avatar
Sebastian Hack committed
643
644
}

645
646
ir_node *be_get_IncSP_pred(ir_node *irn)
{
Michael Beck's avatar
BugFix:    
Michael Beck committed
647
648
649
650
	assert(be_is_IncSP(irn));
	return get_irn_n(irn, 0);
}

651
652
void be_set_IncSP_pred(ir_node *incsp, ir_node *pred)
{
653
654
655
656
	assert(be_is_IncSP(incsp));
	set_irn_n(incsp, 0, pred);
}

657
void be_set_IncSP_offset(ir_node *irn, int offset)
Sebastian Hack's avatar
Sebastian Hack committed
658
659
{
	assert(be_is_IncSP(irn));
Matthias Braun's avatar
Matthias Braun committed
660
	be_incsp_attr_t *a = (be_incsp_attr_t*)get_irn_generic_attr(irn);
Sebastian Hack's avatar
Sebastian Hack committed
661
662
	a->offset = offset;
}
663

664
int be_get_IncSP_offset(const ir_node *irn)
Sebastian Hack's avatar
Sebastian Hack committed
665
{
Sebastian Hack's avatar
Sebastian Hack committed
666
	assert(be_is_IncSP(irn));
Matthias Braun's avatar
Matthias Braun committed
667
	const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
Sebastian Hack's avatar
Sebastian Hack committed
668
669
	return a->offset;
}
Sebastian Hack's avatar
Sebastian Hack committed
670

671
672
673
int be_get_IncSP_align(const ir_node *irn)
{
	assert(be_is_IncSP(irn));
Matthias Braun's avatar
Matthias Braun committed
674
	const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
675
676
677
	return a->align;
}

678
static ir_entity *be_node_get_frame_entity(const ir_node *irn)
679
{
Matthias Braun's avatar
Matthias Braun committed
680
	if (be_is_MemPerm(irn))
681
682
		return be_get_MemPerm_in_entity(irn, 0);
	return NULL;
683
684
}

685
static void be_node_set_frame_offset(ir_node *irn, int offset)
686
{
Matthias Braun's avatar
Matthias Braun committed
687
	if (be_is_MemPerm(irn))
688
		be_set_MemPerm_offset(irn, offset);
689
690
}

691
static int be_node_get_sp_bias(const ir_node *irn)
692
{
693
	if (be_is_IncSP(irn))
Matthias Braun's avatar
Matthias Braun committed
694
695
		return be_get_IncSP_offset(irn);
	return 0;
696
697
}

698
/* for be nodes */
Matthias Braun's avatar
Matthias Braun committed
699
static const arch_irn_ops_t be_node_irn_ops = {
700
701
702
	.get_frame_entity = be_node_get_frame_entity,
	.set_frame_offset = be_node_set_frame_offset,
	.get_sp_bias      = be_node_get_sp_bias,
Sebastian Hack's avatar
Sebastian Hack committed
703
};
704

705
706
707
static int get_start_reg_index(ir_graph *irg, const arch_register_t *reg)
{
	/* do a naive linear search... */
Matthias Braun's avatar
Matthias Braun committed
708
	ir_node *start  = get_irg_start(irg);
709
	be_foreach_out(start, i) {
710
711
		arch_register_req_t const *const out_req = arch_get_irn_register_req_out(start, i);
		if (!arch_register_req_is(out_req, limited))
712
			continue;
713
		if (out_req->cls != reg->reg_class)
714
715
716
717
718
719
720
721
722
723
724
725
			continue;
		if (!rbitset_is_set(out_req->limited, reg->index))
			continue;
		return i;
	}
	panic("Tried querying undefined register '%s' at Start", reg->name);
}

ir_node *be_get_initial_reg_value(ir_graph *irg, const arch_register_t *reg)
{
	int      i     = get_start_reg_index(irg, reg);
	ir_node *start = get_irg_start(irg);
726
	ir_mode *mode  = arch_register_class_mode(reg->reg_class);
727
728
729
730
731
732
733
734
735
736
737
738

	foreach_out_edge(start, edge) {
		ir_node *proj = get_edge_src_irn(edge);
		if (!is_Proj(proj)) // maybe End/Anchor
			continue;
		if (get_Proj_proj(proj) == i) {
			return proj;
		}
	}
	return new_r_Proj(start, mode, i);
}

739
740
static ir_entity* dummy_get_frame_entity(const ir_node *node)
{
Matthias Braun's avatar
Matthias Braun committed
741
	(void)node;
742
743
744
745
746
	return NULL;
}

static void dummy_set_frame_offset(ir_node *node, int bias)
{
Matthias Braun's avatar
Matthias Braun committed
747
748
	(void)node;
	(void)bias;
749
	panic("should not be called");
750
751
752
753
}

static int dummy_get_sp_bias(const ir_node *node)
{
Matthias Braun's avatar
Matthias Braun committed
754
	(void)node;
755
756
757
758
759
	return 0;
}

/* for "middleend" nodes */
static const arch_irn_ops_t dummy_be_irn_ops = {
760
761
762
	.get_frame_entity = dummy_get_frame_entity,
	.set_frame_offset = dummy_set_frame_offset,
	.get_sp_bias      = dummy_get_sp_bias,
763
764
};

765
ir_node *be_new_Phi(ir_node *block, int n_ins, ir_node **ins, ir_mode *mode,
766
                    const arch_register_req_t *req)
767
{
Matthias Braun's avatar
Matthias Braun committed
768
769
	ir_graph *irg  = get_irn_irg(block);
	ir_node  *phi = new_ir_node(NULL, irg, block, op_Phi, mode, n_ins, ins);
770
	phi->attr.phi.u.backedge = new_backedge_arr(get_irg_obstack(irg), n_ins);
Matthias Braun's avatar
Matthias Braun committed
771
772
	struct obstack *obst = be_get_be_obst(irg);
	backend_info_t *info = be_get_info(phi);
773
774
	info->out_infos = NEW_ARR_DZ(reg_out_info_t, obst, 1);
	info->in_reqs   = OALLOCN(obst, const arch_register_req_t*, n_ins);
775
776

	info->out_infos[0].req = req;
Matthias Braun's avatar
Matthias Braun committed
777
	for (int i = 0; i < n_ins; ++i) {
778
		info->in_reqs[i] = req;
779
	}
Matthias Braun's avatar
Matthias Braun committed
780
	verify_new_node(irg, phi);
781
	phi = optimize_node(phi);
782
783
784
	return phi;
}

785
void be_set_phi_reg_req(ir_node *node</