benode.c 20.6 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;
71
ir_op *op_be_Copy;
Sebastian Hack's avatar
Sebastian Hack committed
72
ir_op *op_be_CopyKeep;
73
ir_op *op_be_IncSP;
Matthias Braun's avatar
Matthias Braun committed
74
75
76
ir_op *op_be_Keep;
ir_op *op_be_MemPerm;
ir_op *op_be_Perm;
Sebastian Hack's avatar
Sebastian Hack committed
77

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

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

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

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

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

Matthias Braun's avatar
Matthias Braun committed
117
	const arch_register_req_t **in_reqs;
118
	if (n_inputs >= 0) {
119
120
		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
121
		for (int i = 0; i < n_inputs; ++i) {
122
			in_reqs[i] = arch_no_register_req;
123
124
		}
	} else {
125
		in_reqs = NEW_ARR_F(const arch_register_req_t*, 0);
126
	}
127
	info->in_reqs = in_reqs;
128

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

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

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

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

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

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

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

195
ir_node *be_new_MemPerm(ir_node *const block, int n, ir_node *const *const in)
Matthias Braun's avatar
Matthias Braun committed
196
{
197
198
	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
199

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

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

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

	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
221
222
	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
223

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

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

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

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

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

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

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

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

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

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

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

327
void be_set_MemPerm_in_entity(const ir_node *irn, unsigned n, ir_entity *ent)
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
	attr->in_entities[n] = ent;
}

335
ir_entity* be_get_MemPerm_in_entity(const ir_node* irn, unsigned n)
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
	return attr->in_entities[n];
}

343
void be_set_MemPerm_out_entity(const ir_node *irn, unsigned n, ir_entity *ent)
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
	attr->out_entities[n] = ent;
}

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

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

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

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

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

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
398
399
	ir_graph *irg  = get_irn_irg(node);
	be_irg_t *birg = be_birg_from_irg(irg);
400
401

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

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

413
	arch_set_irn_register_out(node, pos, reg);
Matthias Braun's avatar
Matthias Braun committed
414
	arch_set_irn_register_req_out(node, pos, req);
Sebastian Hack's avatar
Sebastian Hack committed
415
416
}

417
418
ir_node *be_get_IncSP_pred(ir_node *irn)
{
Michael Beck's avatar
BugFix:    
Michael Beck committed
419
420
421
422
	assert(be_is_IncSP(irn));
	return get_irn_n(irn, 0);
}

423
424
void be_set_IncSP_pred(ir_node *incsp, ir_node *pred)
{
425
426
427
428
	assert(be_is_IncSP(incsp));
	set_irn_n(incsp, 0, pred);
}

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

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

444
unsigned be_get_IncSP_align(const ir_node *irn)
445
446
{
	assert(be_is_IncSP(irn));
Matthias Braun's avatar
Matthias Braun committed
447
448
	const be_incsp_attr_t *a
		= (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
449
450
451
	return a->align;
}

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

ir_node *be_get_initial_reg_value(ir_graph *irg, const arch_register_t *reg)
{
472
473
474
475
	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);
476
477
}

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

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

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

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

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

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

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

Matthias Braun's avatar
Matthias Braun committed
574
575
576
		default:
			break;
		}
577
578
579
	}
}

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

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

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

615
616
617
618
619
620
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
621
{
622
623
	assert(is_be_node(node));
	return (be_opcode) (get_irn_opcode(node) - be_opcode_start);
Matthias Braun's avatar
Matthias Braun committed
624
625
}

Matthias Braun's avatar
Matthias Braun committed
626
627
628
629
630
static const arch_irn_ops_t null_ops = {
	.get_op_estimated_cost  = NULL,
	.perform_memory_operand = NULL,
};

631
632
633
634
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
635
636
	set_op_dump(res, dump_node);
	set_op_copy_attr(res, copy_attr);
Matthias Braun's avatar
Matthias Braun committed
637
	res->ops.be_ops = &null_ops;
638
	set_op_tag(res, be_op_tag);
639
640
641
	return res;
}

642
643
void be_init_op(void)
{
644
	assert(op_be_Perm == NULL);
Matthias Braun's avatar
Matthias Braun committed
645

646
647
	be_opcode_start = get_next_ir_opcodes(beo_last+1);

Matthias Braun's avatar
Matthias Braun committed
648
	/* Acquire all needed opcodes. */
649
	unsigned o = be_opcode_start;
Matthias Braun's avatar
Matthias Braun committed
650
651
652
653
654
655
	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));
Matthias Braun's avatar
Matthias Braun committed
656

657
658
659
	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
660
661
662
	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);
663
664

	/* attach out dummy_ops to middle end nodes */
665
	for (unsigned opc = iro_first; opc <= iro_last; ++opc) {
Matthias Braun's avatar
Matthias Braun committed
666
		ir_op *op = ir_get_opcode(opc);
667
		assert(op->ops.be_ops == NULL);
Matthias Braun's avatar
Matthias Braun committed
668
		op->ops.be_ops = &null_ops;
669
	}
Matthias Braun's avatar
Matthias Braun committed
670
}
Matthias Braun's avatar
Matthias Braun committed
671
672
673

void be_finish_op(void)
{
Matthias Braun's avatar
Matthias Braun committed
674
675
676
	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
677
678
679
	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
680
}