benode.c 21.5 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
 * @file
 * @brief       Backend node support for generic backend nodes.
 * @author      Sebastian Hack
Sebastian Hack's avatar
Sebastian Hack committed
10
 *
11
 * Backend node support for generic backend nodes.
Sebastian Hack's avatar
Sebastian Hack committed
12
13
14
 */
#include <stdlib.h>

Matthias Braun's avatar
Matthias Braun committed
15
16
17
18
#include "array.h"
#include "be_t.h"
#include "bearch.h"
#include "bedump.h"
19
#include "beirg.h"
Matthias Braun's avatar
Matthias Braun committed
20
21
22
23
#include "belive.h"
#include "benode.h"
#include "besched.h"
#include "bitfiddle.h"
Sebastian Hack's avatar
Sebastian Hack committed
24
#include "debug.h"
25
#include "fourcc.h"
Matthias Braun's avatar
Matthias Braun committed
26
#include "irbackedge_t.h"
27
#include "ircons_t.h"
Matthias Braun's avatar
Matthias Braun committed
28
#include "irgopt.h"
Sebastian Hack's avatar
Sebastian Hack committed
29
#include "irgwalk.h"
Matthias Braun's avatar
Matthias Braun committed
30
31
32
#include "irmode_t.h"
#include "irnode_t.h"
#include "irop_t.h"
Michael Beck's avatar
Michael Beck committed
33
#include "iropt_t.h"
Matthias Braun's avatar
Matthias Braun committed
34
#include "irprintf.h"
35
#include "irverify_t.h"
Matthias Braun's avatar
Matthias Braun committed
36
37
38
39
40
41
#include "obst.h"
#include "panic.h"
#include "pmap.h"
#include "raw_bitset.h"
#include "set.h"
#include "util.h"
Sebastian Hack's avatar
Sebastian Hack committed
42

43
typedef struct be_node_attr_t {
Matthias Braun's avatar
Matthias Braun committed
44
	except_attr exc;
45
46
} be_node_attr_t;

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

Michael Beck's avatar
Michael Beck committed
55
/** The be_IncSP attribute type. */
Sebastian Hack's avatar
Sebastian Hack committed
56
typedef struct {
57
	be_node_attr_t base;
Matthias Braun's avatar
Matthias Braun committed
58
59
	int            offset; /**< The offset by which the stack shall be
	                            expanded/shrinked. */
60
	unsigned       align;  /**< alignment after the IncSP (0=no alignment) */
Michael Beck's avatar
Michael Beck committed
61
} be_incsp_attr_t;
Sebastian Hack's avatar
Sebastian Hack committed
62

63
typedef struct {
64
65
66
	be_node_attr_t base;
	ir_entity    **in_entities;
	ir_entity    **out_entities;
67
	int            offset;
Matthias Braun's avatar
Matthias Braun committed
68
} be_memperm_attr_t;
69

70
static unsigned be_opcode_start;
Matthias Braun's avatar
Matthias Braun committed
71
ir_op *op_be_AnyVal;
72
ir_op *op_be_Copy;
Sebastian Hack's avatar
Sebastian Hack committed
73
ir_op *op_be_CopyKeep;
74
ir_op *op_be_IncSP;
Matthias Braun's avatar
Matthias Braun committed
75
76
77
ir_op *op_be_Keep;
ir_op *op_be_MemPerm;
ir_op *op_be_Perm;
Sebastian Hack's avatar
Sebastian Hack committed
78

79
80
#define be_op_tag FOURCC('B', 'E', '\0', '\0')

Michael Beck's avatar
Michael Beck committed
81
82
83
/**
 * Compare the attributes of two be_IncSP nodes.
 */
84
static int be_incsp_attrs_equal(const ir_node *a, const ir_node *b)
85
{
Matthias Braun's avatar
Matthias Braun committed
86
87
88
89
	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);
90
	return attr_a->offset == attr_b->offset && attrs_equal_be_node(a, b);
91
92
}

93
static arch_register_req_t *allocate_reg_req(ir_graph *const irg)
Matthias Braun's avatar
Matthias Braun committed
94
{
95
	struct obstack *obst = be_get_be_obst(irg);
96
	arch_register_req_t *req = OALLOCZ(obst, arch_register_req_t);
97
98
	return req;
}
Matthias Braun's avatar
Matthias Braun committed
99

Matthias Braun's avatar
Matthias Braun committed
100
101
static void be_node_set_register_req_in(ir_node *const node, int const pos,
                                        arch_register_req_t const *const req)
102
{
103
104
105
	backend_info_t *info = be_get_info(node);
	assert(pos < get_irn_arity(node));
	info->in_reqs[pos] = req;
106
}
Matthias Braun's avatar
Matthias Braun committed
107

108
/**
109
 * Initializes the generic attribute of all be nodes and return it.
110
 */
Matthias Braun's avatar
Matthias Braun committed
111
static void init_node_attr(ir_node *node, int n_inputs, unsigned n_outputs,
Matthias Braun's avatar
Matthias Braun committed
112
                           arch_irn_flags_t flags)
Sebastian Hack's avatar
Sebastian Hack committed
113
{
114
	ir_graph       *irg  = get_irn_irg(node);
115
	struct obstack *obst = be_get_be_obst(irg);
116
	backend_info_t *info = be_get_info(node);
Sebastian Hack's avatar
Sebastian Hack committed
117

Matthias Braun's avatar
Matthias Braun committed
118
	const arch_register_req_t **in_reqs;
119
	if (n_inputs >= 0) {
120
121
		assert(n_inputs == get_irn_arity(node));
		in_reqs = OALLOCN(obst, const arch_register_req_t*, n_inputs);
Matthias Braun's avatar
Matthias Braun committed
122
		for (int i = 0; i < n_inputs; ++i) {
123
			in_reqs[i] = arch_no_register_req;
124
125
		}
	} else {
126
		in_reqs = NEW_ARR_F(const arch_register_req_t*, 0);
127
	}
128
	info->in_reqs = in_reqs;
129

130
	info->out_infos = NEW_ARR_DZ(reg_out_info_t, obst, n_outputs);
Matthias Braun's avatar
Matthias Braun committed
131
	for (unsigned i = 0; i < n_outputs; ++i) {
132
		info->out_infos[i].req = arch_no_register_req;
Sebastian Hack's avatar
Sebastian Hack committed
133
	}
Matthias Braun's avatar
Matthias Braun committed
134
	info->flags = flags;
Sebastian Hack's avatar
Sebastian Hack committed
135
}
136

137
static void add_register_req_in(ir_node *node, const arch_register_req_t *req)
138
{
139
	backend_info_t *info = be_get_info(node);
140
	ARR_APP1(const arch_register_req_t*, info->in_reqs, req);
141
142
}

Matthias Braun's avatar
Matthias Braun committed
143
144
145
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
146
{
147
	ir_graph *irg = get_irn_irg(block);
Matthias Braun's avatar
Matthias Braun committed
148
	ir_node  *irn = new_ir_node(NULL, irg, block, op_be_Perm, mode_T, n, in);
Matthias Braun's avatar
Matthias Braun committed
149
	init_node_attr(irn, n, n, arch_irn_flags_none);
Matthias Braun's avatar
Matthias Braun committed
150
	be_node_attr_t *attr = (be_node_attr_t*)get_irn_generic_attr(irn);
151
	attr->exc.pin_state = op_pin_state_pinned;
Matthias Braun's avatar
Matthias Braun committed
152
	for (int i = 0; i < n; ++i) {
153
154
155
		const ir_node             *input = in[i];
		const arch_register_req_t *req   = arch_get_irn_register_req(input);
		if (req->width == 1) {
Matthias Braun's avatar
Matthias Braun committed
156
157
			be_node_set_register_req_in(irn, i, cls->class_req);
			arch_set_irn_register_req_out(irn, i, cls->class_req);
158
		} else {
159
			arch_register_req_t *const new_req = allocate_reg_req(irg);
160
161
162
			new_req->cls   = cls;
			new_req->type  = (req->type & arch_register_req_type_aligned);
			new_req->width = req->width;
Matthias Braun's avatar
Matthias Braun committed
163
164
			be_node_set_register_req_in(irn, i, new_req);
			arch_set_irn_register_req_out(irn, i, new_req);
165
		}
Sebastian Hack's avatar
Sebastian Hack committed
166
167
	}

Sebastian Hack's avatar
Sebastian Hack committed
168
169
	return irn;
}
170

171
172
173
void be_Perm_reduce(ir_node *perm, int new_size, int *map)
{
	assert(be_is_Perm(perm));
Matthias Braun's avatar
Matthias Braun committed
174
	int arity = get_irn_arity(perm);
175
	assert(new_size <= arity);
Matthias Braun's avatar
Matthias Braun committed
176
177
178
179
180
	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);
181

182
	/* save the old register data */
Christoph Mallon's avatar
Christoph Mallon committed
183
184
	MEMCPY(old_in_reqs, info->in_reqs, arity);
	MEMCPY(old_infos, info->out_infos, arity);
185

Matthias Braun's avatar
Matthias Braun committed
186
	/* compose the new in array and set the new register data directly */
Matthias Braun's avatar
Matthias Braun committed
187
	for (int i = 0; i < new_size; ++i) {
188
		int idx = map[i];
189
		new_in[i]          = get_irn_n(perm, idx);
190
		info->in_reqs[i]   = old_in_reqs[idx];
191
		info->out_infos[i] = old_infos[idx];
192
193
194
195
	}
	set_irn_in(perm, new_size, new_in);
}

196
ir_node *be_new_MemPerm(ir_node *const block, int n, ir_node *const *const in)
Matthias Braun's avatar
Matthias Braun committed
197
{
198
199
	ir_graph *const irg = get_irn_irg(block);
	ir_node  *const irn = new_ir_node(NULL, irg, block, op_be_MemPerm, mode_T, n, in);
Matthias Braun's avatar
Matthias Braun committed
200

201
	init_node_attr(irn, n, n, arch_irn_flags_none);
Matthias Braun's avatar
Matthias Braun committed
202

Matthias Braun's avatar
Matthias Braun committed
203
	be_memperm_attr_t *attr = (be_memperm_attr_t*)get_irn_generic_attr(irn);
204
205
	attr->in_entities  = OALLOCNZ(get_irg_obstack(irg), ir_entity*, n);
	attr->out_entities = OALLOCNZ(get_irg_obstack(irg), ir_entity*, n);
206
	attr->offset       = 0;
Matthias Braun's avatar
Matthias Braun committed
207
208
209
	return irn;
}

210
ir_node *be_new_Copy(ir_node *bl, ir_node *op)
Sebastian Hack's avatar
Sebastian Hack committed
211
{
212
	ir_graph *irg  = get_irn_irg(bl);
Matthias Braun's avatar
Matthias Braun committed
213
214
215
	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
216
	init_node_attr(res, 1, 1, arch_irn_flags_none);
Matthias Braun's avatar
Matthias Braun committed
217
	be_node_attr_t *attr = (be_node_attr_t*) get_irn_generic_attr(res);
218
	attr->exc.pin_state = op_pin_state_floats;
Matthias Braun's avatar
Matthias Braun committed
219
220
221

	const arch_register_req_t   *in_req = arch_get_irn_register_req(op);
	const arch_register_class_t *cls    = in_req->cls;
Matthias Braun's avatar
Matthias Braun committed
222
223
	be_node_set_register_req_in(res, 0, cls->class_req);
	arch_set_irn_register_req_out(res, 0, cls->class_req);
Matthias Braun's avatar
Matthias Braun committed
224

225
	arch_register_req_t *const req = allocate_reg_req(irg);
Matthias Braun's avatar
Matthias Braun committed
226
227
228
	req->cls  = cls;
	req->type = arch_register_req_type_should_be_same
	            | (in_req->type & arch_register_req_type_aligned);
229
	req->other_same = 1U << 0;
230
	req->width      = in_req->width;
Matthias Braun's avatar
Matthias Braun committed
231
	arch_set_irn_register_req_out(res, 0, req);
Sebastian Hack's avatar
Sebastian Hack committed
232
	return res;
233
234
}

235
236
ir_node *be_get_Copy_op(const ir_node *cpy)
{
237
	return get_irn_n(cpy, n_be_Copy_op);
238
239
}

Matthias Braun's avatar
Matthias Braun committed
240
241
ir_node *be_new_Keep(ir_node *const block, int const n,
                     ir_node *const *const in)
Sebastian Hack's avatar
Sebastian Hack committed
242
{
243
	ir_graph *irg = get_irn_irg(block);
Matthias Braun's avatar
Matthias Braun committed
244
	ir_node  *res = new_ir_node(NULL, irg, block, op_be_Keep, mode_ANY, -1, NULL);
Matthias Braun's avatar
Matthias Braun committed
245
	init_node_attr(res, -1, 1, arch_irn_flag_schedule_first);
Matthias Braun's avatar
Matthias Braun committed
246
	be_node_attr_t *attr = (be_node_attr_t*) get_irn_generic_attr(res);
247
	attr->exc.pin_state = op_pin_state_pinned;
Sebastian Hack's avatar
Sebastian Hack committed
248

Matthias Braun's avatar
Matthias Braun committed
249
	for (int i = 0; i < n; ++i) {
250
251
252
253
254
		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
255
	}
256
257
258
259
	keep_alive(res);
	return res;
}

Matthias Braun's avatar
Matthias Braun committed
260
261
void be_Keep_add_node(ir_node *keep, const arch_register_class_t *cls,
                      ir_node *node)
262
263
{
	assert(be_is_Keep(keep));
264
265
	add_irn_n(keep, node);
	add_register_req_in(keep, cls->class_req);
Sebastian Hack's avatar
Sebastian Hack committed
266
267
}

268
ir_node *be_new_IncSP(const arch_register_t *sp, ir_node *bl,
269
                      ir_node *old_sp, int offset, unsigned align)
Sebastian Hack's avatar
Sebastian Hack committed
270
{
271
	ir_graph *irg = get_irn_irg(bl);
Matthias Braun's avatar
Matthias Braun committed
272
	ir_node  *in[] = { old_sp };
Matthias Braun's avatar
Matthias Braun committed
273
274
	ir_node  *irn  = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->cls->mode,
	                             ARRAY_SIZE(in), in);
Matthias Braun's avatar
Matthias Braun committed
275
	init_node_attr(irn, 1, 1, arch_irn_flags_none);
Matthias Braun's avatar
Matthias Braun committed
276
	be_incsp_attr_t *a    = (be_incsp_attr_t*)get_irn_generic_attr(irn);
277
278
279
	a->offset             = offset;
	a->align              = align;
	a->base.exc.pin_state = op_pin_state_pinned;
Sebastian Hack's avatar
Sebastian Hack committed
280
281

	/* Set output constraint to stack register. */
Matthias Braun's avatar
Matthias Braun committed
282
	be_node_set_register_req_in(irn, 0, sp->cls->class_req);
283
	be_set_constr_single_reg_out(irn, 0, sp, arch_register_req_type_produces_sp);
Sebastian Hack's avatar
Sebastian Hack committed
284
	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
285
}
Sebastian Hack's avatar
Sebastian Hack committed
286

287
ir_node *be_new_CopyKeep(ir_node *const bl, ir_node *const src, int const n, ir_node *const *const in_keep)
Sebastian Hack's avatar
Sebastian Hack committed
288
{
Matthias Braun's avatar
Matthias Braun committed
289
	ir_mode  *mode  = get_irn_mode(src);
290
	ir_graph *irg   = get_irn_irg(bl);
Matthias Braun's avatar
Matthias Braun committed
291
292
	int       arity = n+1;
	ir_node **in    = ALLOCAN(ir_node*, arity);
Sebastian Hack's avatar
Sebastian Hack committed
293
	in[0] = src;
Christoph Mallon's avatar
Christoph Mallon committed
294
	MEMCPY(&in[1], in_keep, n);
Matthias Braun's avatar
Matthias Braun committed
295
	ir_node *irn = new_ir_node(NULL, irg, bl, op_be_CopyKeep, mode, arity, in);
Matthias Braun's avatar
Matthias Braun committed
296
	init_node_attr(irn, arity, 1, arch_irn_flag_schedule_first);
Matthias Braun's avatar
Matthias Braun committed
297
	be_node_attr_t *attr = (be_node_attr_t*)get_irn_generic_attr(irn);
298
	attr->exc.pin_state = op_pin_state_floats;
Matthias Braun's avatar
Matthias Braun committed
299
300
	const arch_register_req_t   *req  = arch_get_irn_register_req(src);
	const arch_register_class_t *cls  = req->cls;
Matthias Braun's avatar
Matthias Braun committed
301
302
	be_node_set_register_req_in(irn, 0, cls->class_req);
	arch_set_irn_register_req_out(irn, 0, cls->class_req);
303
304
305
306
	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;
Matthias Braun's avatar
Matthias Braun committed
307
		be_node_set_register_req_in(irn, i+1, req);
308
	}
Sebastian Hack's avatar
Sebastian Hack committed
309
310
311
	return irn;
}

312
ir_node *be_new_CopyKeep_single(ir_node *bl, ir_node *src, ir_node *keep)
Sebastian Hack's avatar
Sebastian Hack committed
313
{
314
315
	ir_node *const in[] = { keep };
	return be_new_CopyKeep(bl, src, ARRAY_SIZE(in), in);
Sebastian Hack's avatar
Sebastian Hack committed
316
317
}

318
319
ir_node *be_get_CopyKeep_op(const ir_node *cpy)
{
320
	return get_irn_n(cpy, n_be_CopyKeep_op);
321
322
}

323
324
void be_set_CopyKeep_op(ir_node *cpy, ir_node *op)
{
325
	set_irn_n(cpy, n_be_CopyKeep_op, op);
326
327
}

328
void be_set_MemPerm_in_entity(const ir_node *irn, unsigned n, ir_entity *ent)
Matthias Braun's avatar
Matthias Braun committed
329
{
330
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
331
332
	const be_memperm_attr_t *attr
		= (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
333
334
335
	attr->in_entities[n] = ent;
}

336
ir_entity* be_get_MemPerm_in_entity(const ir_node* irn, unsigned n)
Matthias Braun's avatar
Matthias Braun committed
337
{
338
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
339
340
	const be_memperm_attr_t *attr
		= (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
341
342
343
	return attr->in_entities[n];
}

344
void be_set_MemPerm_out_entity(const ir_node *irn, unsigned n, ir_entity *ent)
Matthias Braun's avatar
Matthias Braun committed
345
{
346
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
347
348
	const be_memperm_attr_t *attr
		= (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
349
350
351
	attr->out_entities[n] = ent;
}

352
ir_entity* be_get_MemPerm_out_entity(const ir_node* irn, unsigned n)
Matthias Braun's avatar
Matthias Braun committed
353
{
354
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
355
356
	const be_memperm_attr_t *attr
		= (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
357
358
359
	return attr->out_entities[n];
}

360
361
362
void be_set_MemPerm_offset(ir_node *irn, int offset)
{
	assert(be_is_MemPerm(irn));
Matthias Braun's avatar
Matthias Braun committed
363
	be_memperm_attr_t *attr = (be_memperm_attr_t*)get_irn_generic_attr(irn);
364
365
366
367
368
369
	attr->offset = offset;
}

int be_get_MemPerm_offset(const ir_node *irn)
{
	assert(be_is_MemPerm(irn));
Matthias Braun's avatar
Matthias Braun committed
370
371
	const be_memperm_attr_t *attr
		= (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
372
373
374
	return attr->offset;
}

375
unsigned be_get_MemPerm_entity_arity(const ir_node *irn)
376
{
Matthias Braun's avatar
Matthias Braun committed
377
	assert(be_is_MemPerm(irn));
378
	return get_irn_arity(irn);
379
380
}

Matthias Braun's avatar
Matthias Braun committed
381
382
383
384
385
386
387
388
389
390
391
392
ir_node *be_new_AnyVal(ir_node *block, const arch_register_class_t *cls)
{
	ir_graph *irg  = get_irn_irg(block);
	ir_mode  *mode = cls->mode;
	ir_node  *res  = new_ir_node(NULL, irg, block, op_be_AnyVal, mode, 0, NULL);
	init_node_attr(res, 0, 1, arch_irn_flags_none);
	arch_set_irn_register_req_out(res, 0, cls->class_req);
	be_node_attr_t *attr = (be_node_attr_t*)get_irn_generic_attr(res);
	attr->exc.pin_state = op_pin_state_floats;
	return res;
}

393
const arch_register_req_t *be_create_reg_req(struct obstack *obst,
394
		const arch_register_t *reg, arch_register_req_type_t additional_types)
Sebastian Hack's avatar
Sebastian Hack committed
395
{
396
	arch_register_class_t const *cls     = reg->cls;
Matthias Braun's avatar
Matthias Braun committed
397
398
	unsigned                    *limited
		= rbitset_obstack_alloc(obst, cls->n_regs);
Matthias Braun's avatar
Matthias Braun committed
399
400
	rbitset_set(limited, reg->index);
	arch_register_req_t *req = OALLOC(obst, arch_register_req_t);
401
402
	req->type    = arch_register_req_type_limited | additional_types;
	req->cls     = cls;
Matthias Braun's avatar
Matthias Braun committed
403
	req->limited = limited;
404
	req->width   = 1;
405
	return req;
406
407
408
409
410
}

void be_set_constr_single_reg_out(ir_node *node, int pos,
		const arch_register_t *reg, arch_register_req_type_t additional_types)
{
Matthias Braun's avatar
Matthias Braun committed
411
412
	ir_graph *irg  = get_irn_irg(node);
	be_irg_t *birg = be_birg_from_irg(irg);
413
414

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

Matthias Braun's avatar
Matthias Braun committed
418
	const arch_register_req_t *req;
419
420
421
	if (additional_types == 0) {
		req = reg->single_req;
	} else {
422
		struct obstack *obst = be_get_be_obst(irg);
423
		req = be_create_reg_req(obst, reg, additional_types);
424
425
	}

426
	arch_set_irn_register_out(node, pos, reg);
Matthias Braun's avatar
Matthias Braun committed
427
	arch_set_irn_register_req_out(node, pos, req);
Sebastian Hack's avatar
Sebastian Hack committed
428
429
}

430
431
ir_node *be_get_IncSP_pred(ir_node *irn)
{
Michael Beck's avatar
BugFix:    
Michael Beck committed
432
433
434
435
	assert(be_is_IncSP(irn));
	return get_irn_n(irn, 0);
}

436
437
void be_set_IncSP_pred(ir_node *incsp, ir_node *pred)
{
438
439
440
441
	assert(be_is_IncSP(incsp));
	set_irn_n(incsp, 0, pred);
}

442
void be_set_IncSP_offset(ir_node *irn, int offset)
Sebastian Hack's avatar
Sebastian Hack committed
443
444
{
	assert(be_is_IncSP(irn));
Matthias Braun's avatar
Matthias Braun committed
445
	be_incsp_attr_t *a = (be_incsp_attr_t*)get_irn_generic_attr(irn);
Sebastian Hack's avatar
Sebastian Hack committed
446
447
	a->offset = offset;
}
448

449
int be_get_IncSP_offset(const ir_node *irn)
Sebastian Hack's avatar
Sebastian Hack committed
450
{
Sebastian Hack's avatar
Sebastian Hack committed
451
	assert(be_is_IncSP(irn));
Matthias Braun's avatar
Matthias Braun committed
452
453
	const be_incsp_attr_t *a
		= (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
Sebastian Hack's avatar
Sebastian Hack committed
454
455
	return a->offset;
}
Sebastian Hack's avatar
Sebastian Hack committed
456

457
unsigned be_get_IncSP_align(const ir_node *irn)
458
459
{
	assert(be_is_IncSP(irn));
Matthias Braun's avatar
Matthias Braun committed
460
461
	const be_incsp_attr_t *a
		= (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
462
463
464
	return a->align;
}

465
static unsigned get_start_reg_index(ir_graph *irg, const arch_register_t *reg)
466
467
{
	/* do a naive linear search... */
Matthias Braun's avatar
Matthias Braun committed
468
	ir_node *start  = get_irg_start(irg);
469
	be_foreach_out(start, i) {
Matthias Braun's avatar
Matthias Braun committed
470
471
		arch_register_req_t const *const out_req
			= arch_get_irn_register_req_out(start, i);
472
		if (!arch_register_req_is(out_req, limited))
473
			continue;
474
		if (out_req->cls != reg->cls)
475
476
477
478
479
			continue;
		if (!rbitset_is_set(out_req->limited, reg->index))
			continue;
		return i;
	}
480
	panic("tried querying undefined register '%s' at Start", reg->name);
481
482
483
484
}

ir_node *be_get_initial_reg_value(ir_graph *irg, const arch_register_t *reg)
{
485
486
487
488
	unsigned const i     = get_start_reg_index(irg, reg);
	ir_node *const start = get_irg_start(irg);
	ir_node *const proj  = get_Proj_for_pn(start, i);
	return proj ? proj : new_r_Proj(start, reg->cls->mode, i);
489
490
}

491
ir_node *be_new_Phi(ir_node *block, int n_ins, ir_node **ins, ir_mode *mode,
492
                    const arch_register_req_t *req)
493
{
Matthias Braun's avatar
Matthias Braun committed
494
495
	ir_graph *irg  = get_irn_irg(block);
	ir_node  *phi = new_ir_node(NULL, irg, block, op_Phi, mode, n_ins, ins);
496
	phi->attr.phi.u.backedge = new_backedge_arr(get_irg_obstack(irg), n_ins);
Matthias Braun's avatar
Matthias Braun committed
497
498
	struct obstack *obst = be_get_be_obst(irg);
	backend_info_t *info = be_get_info(phi);
499
500
	info->out_infos = NEW_ARR_DZ(reg_out_info_t, obst, 1);
	info->in_reqs   = OALLOCN(obst, const arch_register_req_t*, n_ins);
501
502

	info->out_infos[0].req = req;
Matthias Braun's avatar
Matthias Braun committed
503
	for (int i = 0; i < n_ins; ++i) {
504
		info->in_reqs[i] = req;
505
	}
Matthias Braun's avatar
Matthias Braun committed
506
	verify_new_node(irg, phi);
507
	phi = optimize_node(phi);
508
509
510
	return phi;
}

511
void be_set_phi_reg_req(ir_node *node, const arch_register_req_t *req)
512
{
Matthias Braun's avatar
Matthias Braun committed
513
	assert(mode_is_data(get_irn_mode(node)));
514
515
	backend_info_t *info = be_get_info(node);
	info->out_infos[0].req = req;
Matthias Braun's avatar
Matthias Braun committed
516
	for (int i = 0, arity = get_irn_arity(node); i < arity; ++i) {
517
518
		info->in_reqs[i] = req;
	}
519
520
}

521
void be_dump_phi_reg_reqs(FILE *F, const ir_node *node, dump_reason_t reason)
522
{
523
	switch (reason) {
524
	case dump_node_opcode_txt:
525
		fputs(get_irn_opname(node), F);
526
527
528
529
530
531
		break;
	case dump_node_mode_txt:
		fprintf(F, "%s", get_mode_name(get_irn_mode(node)));
		break;
	case dump_node_nodeattr_txt:
		break;
Matthias Braun's avatar
Matthias Braun committed
532
	case dump_node_info_txt: {
533
534
		ir_graph *irg = get_irn_irg(node);
		if (irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_BACKEND)) {
535
			be_dump_reqs_and_registers(F, node);
536
		}
537
		break;
538
	}
539
	}
540
541
}

542
543
544
/**
 * ir_op-Operation: dump a be node to file
 */
545
static void dump_node(FILE *f, const ir_node *irn, dump_reason_t reason)
546
{
547
548
	assert(is_be_node(irn));

549
	switch (reason) {
Matthias Braun's avatar
Matthias Braun committed
550
551
552
553
554
555
556
557
	case dump_node_opcode_txt:
		fputs(get_irn_opname(irn), f);
		break;
	case dump_node_mode_txt:
		if (be_is_Copy(irn) || be_is_CopyKeep(irn))
			fprintf(f, "%s", get_mode_name(get_irn_mode(irn)));
		break;
	case dump_node_nodeattr_txt:
558
		if (be_is_IncSP(irn)) {
Matthias Braun's avatar
Matthias Braun committed
559
560
			const be_incsp_attr_t *attr
				= (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
561
562
563
564
			fprintf(f, " [%d] ", attr->offset);
		}
		break;
	case dump_node_info_txt:
565
		be_dump_reqs_and_registers(f, irn);
Matthias Braun's avatar
Matthias Braun committed
566
567
568

		switch (get_be_irn_opcode(irn)) {
		case beo_IncSP: {
Matthias Braun's avatar
Matthias Braun committed
569
570
			const be_incsp_attr_t *a
				= (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
571
			fprintf(f, "align: %u\n", a->align);
Matthias Braun's avatar
Matthias Braun committed
572
			fprintf(f, "offset: %d\n", a->offset);
573
			break;
Matthias Braun's avatar
Matthias Braun committed
574
575
		}
		case beo_MemPerm: {
576
			for (unsigned i = 0; i < be_get_MemPerm_entity_arity(irn); ++i) {
Matthias Braun's avatar
Matthias Braun committed
577
578
579
				ir_entity *in  = be_get_MemPerm_in_entity(irn, i);
				ir_entity *out = be_get_MemPerm_out_entity(irn, i);
				if (in != NULL)
580
					fprintf(f, "\nin[%u]: %s\n", i, get_entity_name(in));
Matthias Braun's avatar
Matthias Braun committed
581
				if (out != NULL)
582
					fprintf(f, "\nout[%u]: %s\n", i, get_entity_name(out));
583
			}
584
			break;
Matthias Braun's avatar
Matthias Braun committed
585
		}
Matthias Braun's avatar
Matthias Braun committed
586

Matthias Braun's avatar
Matthias Braun committed
587
588
589
		default:
			break;
		}
590
591
592
	}
}

Sebastian Hack's avatar
Sebastian Hack committed
593
/**
594
 * ir_op-Operation:
Sebastian Hack's avatar
Sebastian Hack committed
595
596
 * Copies the backend specific attributes from old node to new node.
 */
597
static void copy_attr(ir_graph *irg, const ir_node *old_node, ir_node *new_node)
Sebastian Hack's avatar
Sebastian Hack committed
598
{
Matthias Braun's avatar
Matthias Braun committed
599
600
601
	assert(is_be_node(old_node));
	assert(is_be_node(new_node));

602
603
	const void     *old_attr = get_irn_generic_attr_const(old_node);
	void           *new_attr = get_irn_generic_attr(new_node);
604
	struct obstack *obst     = be_get_be_obst(irg);
605
606
	backend_info_t *old_info = be_get_info(old_node);
	backend_info_t *new_info = be_get_info(new_node);
Sebastian Hack's avatar
Sebastian Hack committed
607
	memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
608
	new_info->flags     = old_info->flags;
Matthias Braun's avatar
Matthias Braun committed
609
610
611
	new_info->out_infos = old_info->out_infos
	                    ? DUP_ARR_D(reg_out_info_t, obst, old_info->out_infos)
	                    : NULL;
612

613
	/* input infos */
614
615
	if (old_info->in_reqs != NULL) {
		unsigned n_ins = get_irn_arity(old_node);
616
		/* need dynamic in infos? */
617
		if (is_irn_dynamic(old_node)) {
618
			new_info->in_reqs = NEW_ARR_F(const arch_register_req_t*, n_ins);
619
		} else {
620
			new_info->in_reqs = OALLOCN(obst,const arch_register_req_t*, n_ins);
Sebastian Hack's avatar
Sebastian Hack committed
621
		}
Christoph Mallon's avatar
Christoph Mallon committed
622
		MEMCPY(new_info->in_reqs, old_info->in_reqs, n_ins);
623
	} else {
624
		new_info->in_reqs = NULL;
Sebastian Hack's avatar
Sebastian Hack committed
625
	}
Sebastian Hack's avatar
Sebastian Hack committed
626
627
}

628
629
630
631
632
633
bool is_be_node(const ir_node *irn)
{
	return get_op_tag(get_irn_op(irn)) == be_op_tag;
}

be_opcode get_be_irn_opcode(const ir_node *node)
Matthias Braun's avatar
Matthias Braun committed
634
{
635
636
	assert(is_be_node(node));
	return (be_opcode) (get_irn_opcode(node) - be_opcode_start);
Matthias Braun's avatar
Matthias Braun committed
637
638
}

Matthias Braun's avatar
Matthias Braun committed
639
640
641
642
643
static const arch_irn_ops_t null_ops = {
	.get_op_estimated_cost  = NULL,
	.perform_memory_operand = NULL,
};

644
645
646
647
static ir_op *new_be_op(unsigned code, const char *name, op_pin_state p,
                        irop_flags flags, op_arity opar, size_t attr_size)
{
	ir_op *res = new_ir_op(code, name, p, flags, opar, 0, attr_size);
Matthias Braun's avatar
Matthias Braun committed
648
649
	set_op_dump(res, dump_node);
	set_op_copy_attr(res, copy_attr);
Matthias Braun's avatar
Matthias Braun committed
650
	res->ops.be_ops = &null_ops;
651
	set_op_tag(res, be_op_tag);
652
653
654
	return res;
}

655
656
void be_init_op(void)
{
657
	assert(op_be_Perm == NULL);
Matthias Braun's avatar
Matthias Braun committed
658

659
660
	be_opcode_start = get_next_ir_opcodes(beo_last+1);

Matthias Braun's avatar
Matthias Braun committed
661
	/* Acquire all needed opcodes. */
662
	unsigned o = be_opcode_start;
Matthias Braun's avatar
Matthias Braun committed
663
664
665
666
667
668
669
670
671
	op_be_AnyVal   = new_be_op(o+beo_AnyVal,   "be_AnyVal",   op_pin_state_exc_pinned, irop_flag_constlike|irop_flag_cse_neutral, oparity_any,      sizeof(be_node_attr_t));
	op_be_Copy     = new_be_op(o+beo_Copy,     "be_Copy",     op_pin_state_exc_pinned, irop_flag_none,                            oparity_any,      sizeof(be_node_attr_t));
	op_be_CopyKeep = new_be_op(o+beo_CopyKeep, "be_CopyKeep", op_pin_state_exc_pinned, irop_flag_keep,                            oparity_variable, sizeof(be_node_attr_t));
	op_be_IncSP    = new_be_op(o+beo_IncSP,    "be_IncSP",    op_pin_state_exc_pinned, irop_flag_none,                            oparity_any,      sizeof(be_incsp_attr_t));
	op_be_Keep     = new_be_op(o+beo_Keep,     "be_Keep",     op_pin_state_exc_pinned, irop_flag_keep,                            oparity_dynamic,  sizeof(be_node_attr_t));
	op_be_MemPerm  = new_be_op(o+beo_MemPerm,  "be_MemPerm",  op_pin_state_exc_pinned, irop_flag_none,                            oparity_variable, sizeof(be_memperm_attr_t));
	op_be_Perm     = new_be_op(o+beo_Perm,     "be_Perm",     op_pin_state_exc_pinned, irop_flag_none,                            oparity_variable, sizeof(be_node_attr_t));

	set_op_attrs_equal(op_be_AnyVal,   attrs_equal_be_node);
672
673
674
	set_op_attrs_equal(op_be_Copy,     attrs_equal_be_node);
	set_op_attrs_equal(op_be_CopyKeep, attrs_equal_be_node);
	set_op_attrs_equal(op_be_IncSP,    be_incsp_attrs_equal);
Matthias Braun's avatar
Matthias Braun committed
675
676
677
	set_op_attrs_equal(op_be_Keep,     attrs_equal_be_node);
	set_op_attrs_equal(op_be_MemPerm,  attrs_equal_be_node);
	set_op_attrs_equal(op_be_Perm,     attrs_equal_be_node);
678
679

	/* attach out dummy_ops to middle end nodes */
680
	for (unsigned opc = iro_first; opc <= iro_last; ++opc) {
Matthias Braun's avatar
Matthias Braun committed
681
		ir_op *op = ir_get_opcode(opc);
682
		assert(op->ops.be_ops == NULL);
Matthias Braun's avatar
Matthias Braun committed
683
		op->ops.be_ops = &null_ops;
684
	}
Matthias Braun's avatar
Matthias Braun committed
685
}
Matthias Braun's avatar
Matthias Braun committed
686
687
688

void be_finish_op(void)
{
Matthias Braun's avatar
Matthias Braun committed
689
	free_ir_op(op_be_AnyVal);   op_be_AnyVal   = NULL;
Matthias Braun's avatar
Matthias Braun committed
690
691
692
	free_ir_op(op_be_Copy);     op_be_Copy     = NULL;
	free_ir_op(op_be_CopyKeep); op_be_CopyKeep = NULL;
	free_ir_op(op_be_IncSP);    op_be_IncSP    = NULL;
Matthias Braun's avatar
Matthias Braun committed
693
694
695
	free_ir_op(op_be_Keep);     op_be_Keep     = NULL;
	free_ir_op(op_be_MemPerm);  op_be_MemPerm  = NULL;
	free_ir_op(op_be_Perm);     op_be_Perm     = NULL;
Matthias Braun's avatar
Matthias Braun committed
696
}