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

Sebastian Hack's avatar
Sebastian Hack committed
6
/**
Christian Würdig's avatar
Christian Würdig committed
7
8
9
 * @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;

Michael Beck's avatar
Michael Beck committed
47
/** The be_IncSP attribute type. */
Sebastian Hack's avatar
Sebastian Hack committed
48
typedef struct {
49
	be_node_attr_t base;
Matthias Braun's avatar
Matthias Braun committed
50
51
	int            offset; /**< The offset by which the stack shall be
	                            expanded/shrinked. */
52
	unsigned       align;  /**< alignment after the IncSP (0=no alignment) */
Michael Beck's avatar
Michael Beck committed
53
} be_incsp_attr_t;
Sebastian Hack's avatar
Sebastian Hack committed
54

55
typedef struct {
56
57
58
	be_node_attr_t base;
	ir_entity    **in_entities;
	ir_entity    **out_entities;
59
	int            offset;
Matthias Braun's avatar
Matthias Braun committed
60
} be_memperm_attr_t;
61

Matthias Braun's avatar
Matthias Braun committed
62
ir_op *op_be_AnyVal;
63
ir_op *op_be_Copy;
Sebastian Hack's avatar
Sebastian Hack committed
64
ir_op *op_be_CopyKeep;
65
ir_op *op_be_IncSP;
Matthias Braun's avatar
Matthias Braun committed
66
67
68
ir_op *op_be_Keep;
ir_op *op_be_MemPerm;
ir_op *op_be_Perm;
Sebastian Hack's avatar
Sebastian Hack committed
69

70
71
#define be_op_tag FOURCC('B', 'E', '\0', '\0')

Michael Beck's avatar
Michael Beck committed
72
73
74
/**
 * Compare the attributes of two be_IncSP nodes.
 */
75
static int be_incsp_attrs_equal(const ir_node *a, const ir_node *b)
76
{
Matthias Braun's avatar
Matthias Braun committed
77
78
79
80
	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);
81
	return attr_a->offset == attr_b->offset && attrs_equal_be_node(a, b);
82
83
}

84
static arch_register_req_t *allocate_reg_req(ir_graph *const irg)
Matthias Braun's avatar
Matthias Braun committed
85
{
86
	struct obstack *obst = be_get_be_obst(irg);
87
	arch_register_req_t *req = OALLOCZ(obst, arch_register_req_t);
88
89
	return req;
}
Matthias Braun's avatar
Matthias Braun committed
90

Matthias Braun's avatar
Matthias Braun committed
91
92
static void be_node_set_register_req_in(ir_node *const node, int const pos,
                                        arch_register_req_t const *const req)
93
{
94
95
96
	backend_info_t *info = be_get_info(node);
	assert(pos < get_irn_arity(node));
	info->in_reqs[pos] = req;
97
}
Matthias Braun's avatar
Matthias Braun committed
98

99
/**
100
 * Initializes the generic attribute of all be nodes and return it.
101
 */
Matthias Braun's avatar
Matthias Braun committed
102
static void init_node_attr(ir_node *node, int n_inputs, unsigned n_outputs,
Matthias Braun's avatar
Matthias Braun committed
103
                           arch_irn_flags_t flags)
Sebastian Hack's avatar
Sebastian Hack committed
104
{
105
	ir_graph       *irg  = get_irn_irg(node);
106
	struct obstack *obst = be_get_be_obst(irg);
107
	backend_info_t *info = be_get_info(node);
Sebastian Hack's avatar
Sebastian Hack committed
108

Matthias Braun's avatar
Matthias Braun committed
109
	const arch_register_req_t **in_reqs;
110
	if (n_inputs >= 0) {
111
112
		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
113
		for (int i = 0; i < n_inputs; ++i) {
114
			in_reqs[i] = arch_no_register_req;
115
116
		}
	} else {
117
		in_reqs = NEW_ARR_F(const arch_register_req_t*, 0);
118
	}
119
	info->in_reqs = in_reqs;
120

121
	info->out_infos = NEW_ARR_DZ(reg_out_info_t, obst, n_outputs);
Matthias Braun's avatar
Matthias Braun committed
122
	for (unsigned i = 0; i < n_outputs; ++i) {
123
		info->out_infos[i].req = arch_no_register_req;
Sebastian Hack's avatar
Sebastian Hack committed
124
	}
Matthias Braun's avatar
Matthias Braun committed
125
	info->flags = flags;
Sebastian Hack's avatar
Sebastian Hack committed
126
}
127

128
static void add_register_req_in(ir_node *node, const arch_register_req_t *req)
129
{
130
	backend_info_t *info = be_get_info(node);
131
	ARR_APP1(const arch_register_req_t*, info->in_reqs, req);
132
133
}

Matthias Braun's avatar
Matthias Braun committed
134
135
136
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
137
{
138
	ir_graph *irg = get_irn_irg(block);
Matthias Braun's avatar
Matthias Braun committed
139
	ir_node  *irn = new_ir_node(NULL, irg, block, op_be_Perm, mode_T, n, in);
Matthias Braun's avatar
Matthias Braun committed
140
	init_node_attr(irn, n, n, arch_irn_flags_none);
Matthias Braun's avatar
Matthias Braun committed
141
	be_node_attr_t *attr = (be_node_attr_t*)get_irn_generic_attr(irn);
142
	attr->exc.pin_state = op_pin_state_pinned;
Matthias Braun's avatar
Matthias Braun committed
143
	for (int i = 0; i < n; ++i) {
144
145
146
		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
147
148
			be_node_set_register_req_in(irn, i, cls->class_req);
			arch_set_irn_register_req_out(irn, i, cls->class_req);
149
		} else {
150
			arch_register_req_t *const new_req = allocate_reg_req(irg);
151
152
153
			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
154
155
			be_node_set_register_req_in(irn, i, new_req);
			arch_set_irn_register_req_out(irn, i, new_req);
156
		}
Sebastian Hack's avatar
Sebastian Hack committed
157
158
	}

Sebastian Hack's avatar
Sebastian Hack committed
159
160
	return irn;
}
161

162
163
164
void be_Perm_reduce(ir_node *perm, int new_size, int *map)
{
	assert(be_is_Perm(perm));
Matthias Braun's avatar
Matthias Braun committed
165
	int arity = get_irn_arity(perm);
166
	assert(new_size <= arity);
Matthias Braun's avatar
Matthias Braun committed
167
168
169
170
171
	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);
172

173
	/* save the old register data */
Christoph Mallon's avatar
Christoph Mallon committed
174
175
	MEMCPY(old_in_reqs, info->in_reqs, arity);
	MEMCPY(old_infos, info->out_infos, arity);
176

Matthias Braun's avatar
Matthias Braun committed
177
	/* compose the new in array and set the new register data directly */
Matthias Braun's avatar
Matthias Braun committed
178
	for (int i = 0; i < new_size; ++i) {
179
		int idx = map[i];
180
		new_in[i]          = get_irn_n(perm, idx);
181
		info->in_reqs[i]   = old_in_reqs[idx];
182
		info->out_infos[i] = old_infos[idx];
183
184
185
186
	}
	set_irn_in(perm, new_size, new_in);
}

187
ir_node *be_new_MemPerm(ir_node *const block, int n, ir_node *const *const in)
Matthias Braun's avatar
Matthias Braun committed
188
{
189
190
	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
191

192
	init_node_attr(irn, n, n, arch_irn_flags_none);
Matthias Braun's avatar
Matthias Braun committed
193

Matthias Braun's avatar
Matthias Braun committed
194
	be_memperm_attr_t *attr = (be_memperm_attr_t*)get_irn_generic_attr(irn);
195
196
	attr->in_entities  = OALLOCNZ(get_irg_obstack(irg), ir_entity*, n);
	attr->out_entities = OALLOCNZ(get_irg_obstack(irg), ir_entity*, n);
197
	attr->offset       = 0;
Matthias Braun's avatar
Matthias Braun committed
198
199
200
	return irn;
}

201
ir_node *be_new_Copy(ir_node *bl, ir_node *op)
Sebastian Hack's avatar
Sebastian Hack committed
202
{
203
	ir_graph *irg  = get_irn_irg(bl);
Matthias Braun's avatar
Matthias Braun committed
204
205
206
	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
207
	init_node_attr(res, 1, 1, arch_irn_flags_none);
Matthias Braun's avatar
Matthias Braun committed
208
	be_node_attr_t *attr = (be_node_attr_t*) get_irn_generic_attr(res);
209
	attr->exc.pin_state = op_pin_state_floats;
Matthias Braun's avatar
Matthias Braun committed
210
211
212

	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
213
214
	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
215

216
	arch_register_req_t *const req = allocate_reg_req(irg);
Matthias Braun's avatar
Matthias Braun committed
217
218
219
	req->cls  = cls;
	req->type = arch_register_req_type_should_be_same
	            | (in_req->type & arch_register_req_type_aligned);
220
	req->other_same = 1U << 0;
221
	req->width      = in_req->width;
Matthias Braun's avatar
Matthias Braun committed
222
	arch_set_irn_register_req_out(res, 0, req);
Sebastian Hack's avatar
Sebastian Hack committed
223
	return res;
224
225
}

226
227
ir_node *be_get_Copy_op(const ir_node *cpy)
{
228
	return get_irn_n(cpy, n_be_Copy_op);
229
230
}

Matthias Braun's avatar
Matthias Braun committed
231
232
ir_node *be_new_Keep(ir_node *const block, int const n,
                     ir_node *const *const in)
Sebastian Hack's avatar
Sebastian Hack committed
233
{
234
	ir_graph *irg = get_irn_irg(block);
Matthias Braun's avatar
Matthias Braun committed
235
	ir_node  *res = new_ir_node(NULL, irg, block, op_be_Keep, mode_ANY, -1, NULL);
Matthias Braun's avatar
Matthias Braun committed
236
	init_node_attr(res, -1, 1, arch_irn_flag_schedule_first);
Matthias Braun's avatar
Matthias Braun committed
237
	be_node_attr_t *attr = (be_node_attr_t*) get_irn_generic_attr(res);
238
	attr->exc.pin_state = op_pin_state_pinned;
Sebastian Hack's avatar
Sebastian Hack committed
239

Matthias Braun's avatar
Matthias Braun committed
240
	for (int i = 0; i < n; ++i) {
241
242
243
244
245
		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
246
	}
247
248
249
250
	keep_alive(res);
	return res;
}

Matthias Braun's avatar
Matthias Braun committed
251
252
void be_Keep_add_node(ir_node *keep, const arch_register_class_t *cls,
                      ir_node *node)
253
254
{
	assert(be_is_Keep(keep));
255
256
	add_irn_n(keep, node);
	add_register_req_in(keep, cls->class_req);
Sebastian Hack's avatar
Sebastian Hack committed
257
258
}

259
ir_node *be_new_IncSP(const arch_register_t *sp, ir_node *bl,
260
                      ir_node *old_sp, int offset, unsigned align)
Sebastian Hack's avatar
Sebastian Hack committed
261
{
262
	ir_graph *irg = get_irn_irg(bl);
Matthias Braun's avatar
Matthias Braun committed
263
	ir_node  *in[] = { old_sp };
Matthias Braun's avatar
Matthias Braun committed
264
265
	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
266
	init_node_attr(irn, 1, 1, arch_irn_flags_none);
Matthias Braun's avatar
Matthias Braun committed
267
	be_incsp_attr_t *a    = (be_incsp_attr_t*)get_irn_generic_attr(irn);
268
269
270
	a->offset             = offset;
	a->align              = align;
	a->base.exc.pin_state = op_pin_state_pinned;
Sebastian Hack's avatar
Sebastian Hack committed
271
272

	/* Set output constraint to stack register. */
Matthias Braun's avatar
Matthias Braun committed
273
	be_node_set_register_req_in(irn, 0, sp->cls->class_req);
274
	be_set_constr_single_reg_out(irn, 0, sp, arch_register_req_type_produces_sp);
Sebastian Hack's avatar
Sebastian Hack committed
275
	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
276
}
Sebastian Hack's avatar
Sebastian Hack committed
277

278
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
279
{
Matthias Braun's avatar
Matthias Braun committed
280
	ir_mode  *mode  = get_irn_mode(src);
281
	ir_graph *irg   = get_irn_irg(bl);
Matthias Braun's avatar
Matthias Braun committed
282
283
	int       arity = n+1;
	ir_node **in    = ALLOCAN(ir_node*, arity);
Sebastian Hack's avatar
Sebastian Hack committed
284
	in[0] = src;
Christoph Mallon's avatar
Christoph Mallon committed
285
	MEMCPY(&in[1], in_keep, n);
Matthias Braun's avatar
Matthias Braun committed
286
	ir_node *irn = new_ir_node(NULL, irg, bl, op_be_CopyKeep, mode, arity, in);
Matthias Braun's avatar
Matthias Braun committed
287
	init_node_attr(irn, arity, 1, arch_irn_flag_schedule_first);
Matthias Braun's avatar
Matthias Braun committed
288
	be_node_attr_t *attr = (be_node_attr_t*)get_irn_generic_attr(irn);
289
	attr->exc.pin_state = op_pin_state_floats;
Matthias Braun's avatar
Matthias Braun committed
290
291
	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
292
293
	be_node_set_register_req_in(irn, 0, cls->class_req);
	arch_set_irn_register_req_out(irn, 0, cls->class_req);
294
295
296
297
	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
298
		be_node_set_register_req_in(irn, i+1, req);
299
	}
Sebastian Hack's avatar
Sebastian Hack committed
300
301
302
	return irn;
}

303
ir_node *be_new_CopyKeep_single(ir_node *bl, ir_node *src, ir_node *keep)
Sebastian Hack's avatar
Sebastian Hack committed
304
{
305
306
	ir_node *const in[] = { keep };
	return be_new_CopyKeep(bl, src, ARRAY_SIZE(in), in);
Sebastian Hack's avatar
Sebastian Hack committed
307
308
}

309
310
ir_node *be_get_CopyKeep_op(const ir_node *cpy)
{
311
	return get_irn_n(cpy, n_be_CopyKeep_op);
312
313
}

314
315
void be_set_CopyKeep_op(ir_node *cpy, ir_node *op)
{
316
	set_irn_n(cpy, n_be_CopyKeep_op, op);
317
318
}

319
void be_set_MemPerm_in_entity(const ir_node *irn, unsigned n, ir_entity *ent)
Matthias Braun's avatar
Matthias Braun committed
320
{
321
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
322
323
	const be_memperm_attr_t *attr
		= (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
324
325
326
	attr->in_entities[n] = ent;
}

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

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

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

351
352
353
void be_set_MemPerm_offset(ir_node *irn, int offset)
{
	assert(be_is_MemPerm(irn));
Matthias Braun's avatar
Matthias Braun committed
354
	be_memperm_attr_t *attr = (be_memperm_attr_t*)get_irn_generic_attr(irn);
355
356
357
358
359
360
	attr->offset = offset;
}

int be_get_MemPerm_offset(const ir_node *irn)
{
	assert(be_is_MemPerm(irn));
Matthias Braun's avatar
Matthias Braun committed
361
362
	const be_memperm_attr_t *attr
		= (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
363
364
365
	return attr->offset;
}

366
unsigned be_get_MemPerm_entity_arity(const ir_node *irn)
367
{
Matthias Braun's avatar
Matthias Braun committed
368
	assert(be_is_MemPerm(irn));
369
	return get_irn_arity(irn);
370
371
}

Matthias Braun's avatar
Matthias Braun committed
372
373
374
375
376
377
378
379
380
381
382
383
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;
}

384
const arch_register_req_t *be_create_reg_req(struct obstack *obst,
385
		const arch_register_t *reg, arch_register_req_type_t additional_types)
Sebastian Hack's avatar
Sebastian Hack committed
386
{
387
	arch_register_class_t const *cls     = reg->cls;
Matthias Braun's avatar
Matthias Braun committed
388
389
	unsigned                    *limited
		= rbitset_obstack_alloc(obst, cls->n_regs);
Matthias Braun's avatar
Matthias Braun committed
390
391
	rbitset_set(limited, reg->index);
	arch_register_req_t *req = OALLOC(obst, arch_register_req_t);
392
393
	req->type    = arch_register_req_type_limited | additional_types;
	req->cls     = cls;
Matthias Braun's avatar
Matthias Braun committed
394
	req->limited = limited;
395
	req->width   = 1;
396
	return req;
397
398
399
400
401
}

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
402
403
	ir_graph *irg  = get_irn_irg(node);
	be_irg_t *birg = be_birg_from_irg(irg);
404
405

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

Matthias Braun's avatar
Matthias Braun committed
409
	const arch_register_req_t *req;
410
411
412
	if (additional_types == 0) {
		req = reg->single_req;
	} else {
413
		struct obstack *obst = be_get_be_obst(irg);
414
		req = be_create_reg_req(obst, reg, additional_types);
415
416
	}

417
	arch_set_irn_register_out(node, pos, reg);
Matthias Braun's avatar
Matthias Braun committed
418
	arch_set_irn_register_req_out(node, pos, req);
Sebastian Hack's avatar
Sebastian Hack committed
419
420
}

421
422
ir_node *be_get_IncSP_pred(ir_node *irn)
{
Michael Beck's avatar
BugFix:    
Michael Beck committed
423
424
425
426
	assert(be_is_IncSP(irn));
	return get_irn_n(irn, 0);
}

427
428
void be_set_IncSP_pred(ir_node *incsp, ir_node *pred)
{
429
430
431
432
	assert(be_is_IncSP(incsp));
	set_irn_n(incsp, 0, pred);
}

433
void be_set_IncSP_offset(ir_node *irn, int offset)
Sebastian Hack's avatar
Sebastian Hack committed
434
435
{
	assert(be_is_IncSP(irn));
Matthias Braun's avatar
Matthias Braun committed
436
	be_incsp_attr_t *a = (be_incsp_attr_t*)get_irn_generic_attr(irn);
Sebastian Hack's avatar
Sebastian Hack committed
437
438
	a->offset = offset;
}
439

440
int be_get_IncSP_offset(const ir_node *irn)
Sebastian Hack's avatar
Sebastian Hack committed
441
{
Sebastian Hack's avatar
Sebastian Hack committed
442
	assert(be_is_IncSP(irn));
Matthias Braun's avatar
Matthias Braun committed
443
444
	const be_incsp_attr_t *a
		= (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
Sebastian Hack's avatar
Sebastian Hack committed
445
446
	return a->offset;
}
Sebastian Hack's avatar
Sebastian Hack committed
447

448
unsigned be_get_IncSP_align(const ir_node *irn)
449
450
{
	assert(be_is_IncSP(irn));
Matthias Braun's avatar
Matthias Braun committed
451
452
	const be_incsp_attr_t *a
		= (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
453
454
455
	return a->align;
}

456
static unsigned get_start_reg_index(ir_graph *irg, const arch_register_t *reg)
457
458
{
	/* do a naive linear search... */
Matthias Braun's avatar
Matthias Braun committed
459
	ir_node *start  = get_irg_start(irg);
460
	be_foreach_out(start, i) {
Matthias Braun's avatar
Matthias Braun committed
461
462
		arch_register_req_t const *const out_req
			= arch_get_irn_register_req_out(start, i);
463
		if (!arch_register_req_is(out_req, limited))
464
			continue;
465
		if (out_req->cls != reg->cls)
466
467
468
469
470
			continue;
		if (!rbitset_is_set(out_req->limited, reg->index))
			continue;
		return i;
	}
471
	panic("tried querying undefined register '%s' at Start", reg->name);
472
473
474
475
}

ir_node *be_get_initial_reg_value(ir_graph *irg, const arch_register_t *reg)
{
476
477
478
479
	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);
480
481
}

482
ir_node *be_new_Phi(ir_node *block, int n_ins, ir_node **ins, ir_mode *mode,
483
                    const arch_register_req_t *req)
484
{
Matthias Braun's avatar
Matthias Braun committed
485
486
	ir_graph *irg  = get_irn_irg(block);
	ir_node  *phi = new_ir_node(NULL, irg, block, op_Phi, mode, n_ins, ins);
487
	phi->attr.phi.u.backedge = new_backedge_arr(get_irg_obstack(irg), n_ins);
Matthias Braun's avatar
Matthias Braun committed
488
489
	struct obstack *obst = be_get_be_obst(irg);
	backend_info_t *info = be_get_info(phi);
490
491
	info->out_infos = NEW_ARR_DZ(reg_out_info_t, obst, 1);
	info->in_reqs   = OALLOCN(obst, const arch_register_req_t*, n_ins);
492
493

	info->out_infos[0].req = req;
Matthias Braun's avatar
Matthias Braun committed
494
	for (int i = 0; i < n_ins; ++i) {
495
		info->in_reqs[i] = req;
496
	}
Matthias Braun's avatar
Matthias Braun committed
497
	verify_new_node(irg, phi);
498
	phi = optimize_node(phi);
499
500
501
	return phi;
}

502
void be_set_phi_reg_req(ir_node *node, const arch_register_req_t *req)
503
{
Matthias Braun's avatar
Matthias Braun committed
504
	assert(mode_is_data(get_irn_mode(node)));
505
506
	backend_info_t *info = be_get_info(node);
	info->out_infos[0].req = req;
Matthias Braun's avatar
Matthias Braun committed
507
	for (int i = 0, arity = get_irn_arity(node); i < arity; ++i) {
508
509
		info->in_reqs[i] = req;
	}
510
511
}

512
void be_dump_phi_reg_reqs(FILE *F, const ir_node *node, dump_reason_t reason)
513
{
514
	switch (reason) {
515
	case dump_node_opcode_txt:
516
		fputs(get_irn_opname(node), F);
517
518
519
520
521
522
		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
523
	case dump_node_info_txt: {
524
525
		ir_graph *irg = get_irn_irg(node);
		if (irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_BACKEND)) {
526
			be_dump_reqs_and_registers(F, node);
527
		}
528
		break;
529
	}
530
	}
531
532
}

533
534
535
/**
 * ir_op-Operation: dump a be node to file
 */
536
static void dump_node(FILE *f, const ir_node *irn, dump_reason_t reason)
537
{
538
539
	assert(is_be_node(irn));

540
	switch (reason) {
Matthias Braun's avatar
Matthias Braun committed
541
542
543
544
545
546
547
548
	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:
549
		if (be_is_IncSP(irn)) {
Matthias Braun's avatar
Matthias Braun committed
550
551
			const be_incsp_attr_t *attr
				= (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
552
553
554
555
			fprintf(f, " [%d] ", attr->offset);
		}
		break;
	case dump_node_info_txt:
556
		be_dump_reqs_and_registers(f, irn);
Matthias Braun's avatar
Matthias Braun committed
557

558
		if (be_is_IncSP(irn)) {
Matthias Braun's avatar
Matthias Braun committed
559
560
			const be_incsp_attr_t *a
				= (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
561
			fprintf(f, "align: %u\n", a->align);
Matthias Braun's avatar
Matthias Braun committed
562
			fprintf(f, "offset: %d\n", a->offset);
563
		} else if (be_is_MemPerm(irn)) {
564
			for (unsigned i = 0; i < be_get_MemPerm_entity_arity(irn); ++i) {
Matthias Braun's avatar
Matthias Braun committed
565
566
567
				ir_entity *in  = be_get_MemPerm_in_entity(irn, i);
				ir_entity *out = be_get_MemPerm_out_entity(irn, i);
				if (in != NULL)
568
					fprintf(f, "\nin[%u]: %s\n", i, get_entity_name(in));
Matthias Braun's avatar
Matthias Braun committed
569
				if (out != NULL)
570
					fprintf(f, "\nout[%u]: %s\n", i, get_entity_name(out));
571
			}
Matthias Braun's avatar
Matthias Braun committed
572
		}
573
		break;
574
575
576
	}
}

Sebastian Hack's avatar
Sebastian Hack committed
577
/**
578
 * ir_op-Operation:
Sebastian Hack's avatar
Sebastian Hack committed
579
580
 * Copies the backend specific attributes from old node to new node.
 */
581
static void copy_attr(ir_graph *irg, const ir_node *old_node, ir_node *new_node)
Sebastian Hack's avatar
Sebastian Hack committed
582
{
Matthias Braun's avatar
Matthias Braun committed
583
584
585
	assert(is_be_node(old_node));
	assert(is_be_node(new_node));

586
587
	const void     *old_attr = get_irn_generic_attr_const(old_node);
	void           *new_attr = get_irn_generic_attr(new_node);
588
	struct obstack *obst     = be_get_be_obst(irg);
589
590
	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
591
	memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
592
	new_info->flags     = old_info->flags;
Matthias Braun's avatar
Matthias Braun committed
593
594
595
	new_info->out_infos = old_info->out_infos
	                    ? DUP_ARR_D(reg_out_info_t, obst, old_info->out_infos)
	                    : NULL;
596

597
	/* input infos */
598
599
	if (old_info->in_reqs != NULL) {
		unsigned n_ins = get_irn_arity(old_node);
600
		/* need dynamic in infos? */
601
		if (is_irn_dynamic(old_node)) {
602
			new_info->in_reqs = NEW_ARR_F(const arch_register_req_t*, n_ins);
603
		} else {
604
			new_info->in_reqs = OALLOCN(obst,const arch_register_req_t*, n_ins);
Sebastian Hack's avatar
Sebastian Hack committed
605
		}
Christoph Mallon's avatar
Christoph Mallon committed
606
		MEMCPY(new_info->in_reqs, old_info->in_reqs, n_ins);
607
	} else {
608
		new_info->in_reqs = NULL;
Sebastian Hack's avatar
Sebastian Hack committed
609
	}
Sebastian Hack's avatar
Sebastian Hack committed
610
611
}

612
613
614
615
616
bool is_be_node(const ir_node *irn)
{
	return get_op_tag(get_irn_op(irn)) == be_op_tag;
}

Matthias Braun's avatar
Matthias Braun committed
617
618
619
620
621
static const arch_irn_ops_t null_ops = {
	.get_op_estimated_cost  = NULL,
	.perform_memory_operand = NULL,
};

622
623
624
625
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
626
627
	set_op_dump(res, dump_node);
	set_op_copy_attr(res, copy_attr);
Matthias Braun's avatar
Matthias Braun committed
628
	res->ops.be_ops = &null_ops;
629
	set_op_tag(res, be_op_tag);
630
631
632
	return res;
}

633
634
void be_init_op(void)
{
635
	assert(op_be_Perm == NULL);
Matthias Braun's avatar
Matthias Braun committed
636

Matthias Braun's avatar
Matthias Braun committed
637
	/* Acquire all needed opcodes. */
638
	unsigned const o = get_next_ir_opcodes(beo_last + 1);
Matthias Braun's avatar
Matthias Braun committed
639
640
641
642
643
644
645
646
647
	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);
648
649
650
	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
651
652
653
	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);
654
655

	/* attach out dummy_ops to middle end nodes */
656
	for (unsigned opc = iro_first; opc <= iro_last; ++opc) {
Matthias Braun's avatar
Matthias Braun committed
657
		ir_op *op = ir_get_opcode(opc);
658
		assert(op->ops.be_ops == NULL);
Matthias Braun's avatar
Matthias Braun committed
659
		op->ops.be_ops = &null_ops;
660
	}
Matthias Braun's avatar
Matthias Braun committed
661
}
Matthias Braun's avatar
Matthias Braun committed
662
663
664

void be_finish_op(void)
{
Matthias Braun's avatar
Matthias Braun committed
665
	free_ir_op(op_be_AnyVal);   op_be_AnyVal   = NULL;
Matthias Braun's avatar
Matthias Braun committed
666
667
668
	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
669
670
671
	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
672
}