benode.c 22.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
10
 * @file
 * @brief       Backend node support for generic backend nodes.
 * @author      Sebastian Hack
 * @date        17.05.2005
Sebastian Hack's avatar
Sebastian Hack committed
11
 *
12
 * Backend node support for generic backend nodes.
13
 * This file provides Perm, and Copy nodes.
Sebastian Hack's avatar
Sebastian Hack committed
14
15
16
 */
#include <stdlib.h>

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

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

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

47
typedef struct be_node_attr_t {
Matthias Braun's avatar
Matthias Braun committed
48
	except_attr exc;
49
50
} be_node_attr_t;

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

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

67
typedef struct {
68
69
70
	be_node_attr_t base;
	ir_entity    **in_entities;
	ir_entity    **out_entities;
71
	int            offset;
Matthias Braun's avatar
Matthias Braun committed
72
} be_memperm_attr_t;
73

74
static unsigned be_opcode_start;
75
ir_op *op_be_Perm;
Matthias Braun's avatar
Matthias Braun committed
76
ir_op *op_be_MemPerm;
77
78
ir_op *op_be_Copy;
ir_op *op_be_Keep;
Sebastian Hack's avatar
Sebastian Hack committed
79
ir_op *op_be_CopyKeep;
80
ir_op *op_be_IncSP;
Sebastian Hack's avatar
Sebastian Hack committed
81

82
83
#define be_op_tag FOURCC('B', 'E', '\0', '\0')

Michael Beck's avatar
Michael Beck committed
84
85
86
/**
 * Compare the attributes of two be_IncSP nodes.
 */
87
static int be_incsp_attrs_equal(const ir_node *a, const ir_node *b)
88
{
89
90
91
	const be_incsp_attr_t *attr_a = (const be_incsp_attr_t*)get_irn_generic_attr_const(a);
	const be_incsp_attr_t *attr_b = (const be_incsp_attr_t*)get_irn_generic_attr_const(b);
	return attr_a->offset == attr_b->offset && attrs_equal_be_node(a, b);
92
93
}

94
static arch_register_req_t *allocate_reg_req(ir_graph *const irg)
Matthias Braun's avatar
Matthias Braun committed
95
{
96
	struct obstack *obst = be_get_be_obst(irg);
Matthias Braun's avatar
Matthias Braun committed
97

98
	arch_register_req_t *req = OALLOCZ(obst, arch_register_req_t);
99
100
	return req;
}
Matthias Braun's avatar
Matthias Braun committed
101

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

109
110
111
112
void be_set_constr_out(ir_node *node, int pos, const arch_register_req_t *req)
{
	backend_info_t *info = be_get_info(node);
	info->out_infos[pos].req = req;
Matthias Braun's avatar
Matthias Braun committed
113
114
}

115
/**
116
 * Initializes the generic attribute of all be nodes and return it.
117
 */
Matthias Braun's avatar
Matthias Braun committed
118
119
static void init_node_attr(ir_node *node, int n_inputs, int n_outputs,
                           arch_irn_flags_t flags)
Sebastian Hack's avatar
Sebastian Hack committed
120
{
121
	assert(n_outputs >= 0);
122
	ir_graph       *irg  = get_irn_irg(node);
123
	struct obstack *obst = be_get_be_obst(irg);
124
	backend_info_t *info = be_get_info(node);
Sebastian Hack's avatar
Sebastian Hack committed
125

Matthias Braun's avatar
Matthias Braun committed
126
	const arch_register_req_t **in_reqs;
127
128
	if (n_inputs >= 0) {
		int i;
129
130
		assert(n_inputs == get_irn_arity(node));
		in_reqs = OALLOCN(obst, const arch_register_req_t*, n_inputs);
131
		for (i = 0; i < n_inputs; ++i) {
132
			in_reqs[i] = arch_no_register_req;
133
134
		}
	} else {
135
		in_reqs = NEW_ARR_F(const arch_register_req_t*, 0);
136
	}
137
	info->in_reqs = in_reqs;
138

139
	info->out_infos = NEW_ARR_DZ(reg_out_info_t, obst, n_outputs);
140
141
	for (int i = 0; i < n_outputs; ++i) {
		info->out_infos[i].req = arch_no_register_req;
Sebastian Hack's avatar
Sebastian Hack committed
142
	}
Matthias Braun's avatar
Matthias Braun committed
143
	info->flags = flags;
Sebastian Hack's avatar
Sebastian Hack committed
144
}
145

146
static void add_register_req_in(ir_node *node, const arch_register_req_t *req)
147
{
148
	backend_info_t *info = be_get_info(node);
149
	ARR_APP1(const arch_register_req_t*, info->in_reqs, req);
150
151
}

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

Sebastian Hack's avatar
Sebastian Hack committed
175
176
	return irn;
}
177

178
179
180
void be_Perm_reduce(ir_node *perm, int new_size, int *map)
{
	assert(be_is_Perm(perm));
Matthias Braun's avatar
Matthias Braun committed
181
	int arity = get_irn_arity(perm);
182
	assert(new_size <= arity);
Matthias Braun's avatar
Matthias Braun committed
183
184
185
186
187
	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);
188

189
	/* save the old register data */
Christoph Mallon's avatar
Christoph Mallon committed
190
191
	MEMCPY(old_in_reqs, info->in_reqs, arity);
	MEMCPY(old_infos, info->out_infos, arity);
192

Matthias Braun's avatar
Matthias Braun committed
193
	/* compose the new in array and set the new register data directly */
Matthias Braun's avatar
Matthias Braun committed
194
	for (int i = 0; i < new_size; ++i) {
195
		int idx = map[i];
196
		new_in[i]          = get_irn_n(perm, idx);
197
		info->in_reqs[i]   = old_in_reqs[idx];
198
		info->out_infos[i] = old_infos[idx];
199
200
201
202
	}
	set_irn_in(perm, new_size, new_in);
}

203
ir_node *be_new_MemPerm(ir_node *const block, int n, ir_node *const *const in)
Matthias Braun's avatar
Matthias Braun committed
204
{
205
206
	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
207

208
	init_node_attr(irn, n, n, arch_irn_flags_none);
Matthias Braun's avatar
Matthias Braun committed
209

Matthias Braun's avatar
Matthias Braun committed
210
	be_memperm_attr_t *attr = (be_memperm_attr_t*)get_irn_generic_attr(irn);
211
212
	attr->in_entities  = OALLOCNZ(get_irg_obstack(irg), ir_entity*, n);
	attr->out_entities = OALLOCNZ(get_irg_obstack(irg), ir_entity*, n);
213
	attr->offset       = 0;
Matthias Braun's avatar
Matthias Braun committed
214
215
216
	return irn;
}

217
ir_node *be_new_Copy(ir_node *bl, ir_node *op)
Sebastian Hack's avatar
Sebastian Hack committed
218
{
219
	ir_graph *irg  = get_irn_irg(bl);
Matthias Braun's avatar
Matthias Braun committed
220
221
222
	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
223
	init_node_attr(res, 1, 1, arch_irn_flags_none);
Matthias Braun's avatar
Matthias Braun committed
224
	be_node_attr_t *attr = (be_node_attr_t*) get_irn_generic_attr(res);
225
	attr->exc.pin_state = op_pin_state_floats;
Matthias Braun's avatar
Matthias Braun committed
226
227
228

	const arch_register_req_t   *in_req = arch_get_irn_register_req(op);
	const arch_register_class_t *cls    = in_req->cls;
229
230
	be_node_set_reg_class_in(res, 0, cls);
	be_node_set_reg_class_out(res, 0, cls);
Matthias Braun's avatar
Matthias Braun committed
231

232
	arch_register_req_t *const req = allocate_reg_req(irg);
Matthias Braun's avatar
Matthias Braun committed
233
234
235
	req->cls  = cls;
	req->type = arch_register_req_type_should_be_same
	            | (in_req->type & arch_register_req_type_aligned);
236
	req->other_same = 1U << 0;
237
	req->width      = in_req->width;
238
	be_set_constr_out(res, 0, req);
Sebastian Hack's avatar
Sebastian Hack committed
239
	return res;
240
241
}

242
243
ir_node *be_get_Copy_op(const ir_node *cpy)
{
244
	return get_irn_n(cpy, n_be_Copy_op);
245
246
}

247
ir_node *be_new_Keep(ir_node *const block, int const n, ir_node *const *const in)
Sebastian Hack's avatar
Sebastian Hack committed
248
{
249
	ir_graph *irg = get_irn_irg(block);
Matthias Braun's avatar
Matthias Braun committed
250
	ir_node  *res = new_ir_node(NULL, irg, block, op_be_Keep, mode_ANY, -1, NULL);
Matthias Braun's avatar
Matthias Braun committed
251
	init_node_attr(res, -1, 1, arch_irn_flag_schedule_first);
Matthias Braun's avatar
Matthias Braun committed
252
	be_node_attr_t *attr = (be_node_attr_t*) get_irn_generic_attr(res);
253
	attr->exc.pin_state = op_pin_state_pinned;
Sebastian Hack's avatar
Sebastian Hack committed
254

Matthias Braun's avatar
Matthias Braun committed
255
	for (int i = 0; i < n; ++i) {
256
257
258
259
260
		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
261
	}
262
263
264
265
266
267
268
	keep_alive(res);
	return res;
}

void be_Keep_add_node(ir_node *keep, const arch_register_class_t *cls, ir_node *node)
{
	assert(be_is_Keep(keep));
269
270
	add_irn_n(keep, node);
	add_register_req_in(keep, cls->class_req);
Sebastian Hack's avatar
Sebastian Hack committed
271
272
}

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

	/* Set output constraint to stack register. */
286
	be_node_set_reg_class_in(irn, 0, sp->cls);
287
	be_set_constr_single_reg_out(irn, 0, sp, arch_register_req_type_produces_sp);
Sebastian Hack's avatar
Sebastian Hack committed
288
	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
289
}
Sebastian Hack's avatar
Sebastian Hack committed
290

291
ir_node *be_new_CopyKeep(ir_node *bl, ir_node *src, int n, ir_node *in_keep[])
Sebastian Hack's avatar
Sebastian Hack committed
292
{
Matthias Braun's avatar
Matthias Braun committed
293
	ir_mode  *mode  = get_irn_mode(src);
294
	ir_graph *irg   = get_irn_irg(bl);
Matthias Braun's avatar
Matthias Braun committed
295
296
	int       arity = n+1;
	ir_node **in    = ALLOCAN(ir_node*, arity);
Sebastian Hack's avatar
Sebastian Hack committed
297
	in[0] = src;
Christoph Mallon's avatar
Christoph Mallon committed
298
	MEMCPY(&in[1], in_keep, n);
Matthias Braun's avatar
Matthias Braun committed
299
	ir_node *irn = new_ir_node(NULL, irg, bl, op_be_CopyKeep, mode, arity, in);
Matthias Braun's avatar
Matthias Braun committed
300
	init_node_attr(irn, arity, 1, arch_irn_flag_schedule_first);
Matthias Braun's avatar
Matthias Braun committed
301
	be_node_attr_t *attr = (be_node_attr_t*)get_irn_generic_attr(irn);
302
	attr->exc.pin_state = op_pin_state_floats;
Matthias Braun's avatar
Matthias Braun committed
303
304
	const arch_register_req_t   *req  = arch_get_irn_register_req(src);
	const arch_register_class_t *cls  = req->cls;
305
306
	be_node_set_reg_class_in(irn, 0, cls);
	be_node_set_reg_class_out(irn, 0, cls);
307
308
309
310
311
312
	for (int i = 0; i < n; ++i) {
		ir_node *pred = in_keep[i];
		const arch_register_req_t *req = arch_get_irn_register_req(pred);
		req = req->cls != NULL ? req->cls->class_req : arch_no_register_req;
		be_set_constr_in(irn, i+1, req);
	}
Sebastian Hack's avatar
Sebastian Hack committed
313
314
315
	return irn;
}

316
ir_node *be_new_CopyKeep_single(ir_node *bl, ir_node *src, ir_node *keep)
Sebastian Hack's avatar
Sebastian Hack committed
317
{
318
	return be_new_CopyKeep(bl, src, 1, &keep);
Sebastian Hack's avatar
Sebastian Hack committed
319
320
}

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

326
327
void be_set_CopyKeep_op(ir_node *cpy, ir_node *op)
{
328
	set_irn_n(cpy, n_be_CopyKeep_op, op);
329
330
}

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

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

345
void be_set_MemPerm_out_entity(const ir_node *irn, unsigned n, ir_entity *ent)
Matthias Braun's avatar
Matthias Braun committed
346
{
347
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
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
	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
	const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
370
371
372
	return attr->offset;
}

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

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

void be_set_constr_single_reg_in(ir_node *node, int pos,
		const arch_register_t *reg, arch_register_req_type_t additional_types)
{
396
397
398
399
400
	const arch_register_req_t *req;
	if (additional_types == 0) {
		req = reg->single_req;
	} else {
		ir_graph       *irg  = get_irn_irg(node);
401
		struct obstack *obst = be_get_be_obst(irg);
402
		req = be_create_reg_req(obst, reg, additional_types);
403
404
	}
	be_set_constr_in(node, pos, req);
405
406
407
408
409
}

void be_set_constr_single_reg_out(ir_node *node, int pos,
		const arch_register_t *reg, arch_register_req_type_t additional_types)
{
410
411
	ir_graph                  *irg  = get_irn_irg(node);
	be_irg_t                  *birg = be_birg_from_irg(irg);
412
413

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

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

425
	arch_set_irn_register_out(node, pos, reg);
426
	be_set_constr_out(node, pos, req);
Sebastian Hack's avatar
Sebastian Hack committed
427
428
}

429
430
void be_node_set_reg_class_in(ir_node *irn, int pos,
                              const arch_register_class_t *cls)
431
{
432
	be_set_constr_in(irn, pos, cls->class_req);
433
434
}

435
436
void be_node_set_reg_class_out(ir_node *irn, int pos,
                               const arch_register_class_t *cls)
Sebastian Hack's avatar
Sebastian Hack committed
437
{
438
	be_set_constr_out(irn, pos, cls->class_req);
Sebastian Hack's avatar
Sebastian Hack committed
439
440
}

441
442
ir_node *be_get_IncSP_pred(ir_node *irn)
{
Michael Beck's avatar
BugFix:    
Michael Beck committed
443
444
445
446
	assert(be_is_IncSP(irn));
	return get_irn_n(irn, 0);
}

447
448
void be_set_IncSP_pred(ir_node *incsp, ir_node *pred)
{
449
450
451
452
	assert(be_is_IncSP(incsp));
	set_irn_n(incsp, 0, pred);
}

453
void be_set_IncSP_offset(ir_node *irn, int offset)
Sebastian Hack's avatar
Sebastian Hack committed
454
455
{
	assert(be_is_IncSP(irn));
Matthias Braun's avatar
Matthias Braun committed
456
	be_incsp_attr_t *a = (be_incsp_attr_t*)get_irn_generic_attr(irn);
Sebastian Hack's avatar
Sebastian Hack committed
457
458
	a->offset = offset;
}
459

460
int be_get_IncSP_offset(const ir_node *irn)
Sebastian Hack's avatar
Sebastian Hack committed
461
{
Sebastian Hack's avatar
Sebastian Hack committed
462
	assert(be_is_IncSP(irn));
Matthias Braun's avatar
Matthias Braun committed
463
	const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
Sebastian Hack's avatar
Sebastian Hack committed
464
465
	return a->offset;
}
Sebastian Hack's avatar
Sebastian Hack committed
466

467
unsigned be_get_IncSP_align(const ir_node *irn)
468
469
{
	assert(be_is_IncSP(irn));
Matthias Braun's avatar
Matthias Braun committed
470
	const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
471
472
473
	return a->align;
}

474
static ir_entity *be_node_get_frame_entity(const ir_node *irn)
475
{
Matthias Braun's avatar
Matthias Braun committed
476
	if (be_is_MemPerm(irn))
477
478
		return be_get_MemPerm_in_entity(irn, 0);
	return NULL;
479
480
}

481
static void be_node_set_frame_offset(ir_node *irn, int offset)
482
{
Matthias Braun's avatar
Matthias Braun committed
483
	if (be_is_MemPerm(irn))
484
		be_set_MemPerm_offset(irn, offset);
485
486
}

487
static int be_node_get_sp_bias(const ir_node *irn)
488
{
489
	if (be_is_IncSP(irn))
Matthias Braun's avatar
Matthias Braun committed
490
491
		return be_get_IncSP_offset(irn);
	return 0;
492
493
}

494
/* for be nodes */
Matthias Braun's avatar
Matthias Braun committed
495
static const arch_irn_ops_t be_node_irn_ops = {
496
497
498
	.get_frame_entity = be_node_get_frame_entity,
	.set_frame_offset = be_node_set_frame_offset,
	.get_sp_bias      = be_node_get_sp_bias,
Sebastian Hack's avatar
Sebastian Hack committed
499
};
500

501
static unsigned get_start_reg_index(ir_graph *irg, const arch_register_t *reg)
502
503
{
	/* do a naive linear search... */
Matthias Braun's avatar
Matthias Braun committed
504
	ir_node *start  = get_irg_start(irg);
505
	be_foreach_out(start, i) {
506
507
		arch_register_req_t const *const out_req = arch_get_irn_register_req_out(start, i);
		if (!arch_register_req_is(out_req, limited))
508
			continue;
509
		if (out_req->cls != reg->cls)
510
511
512
513
514
515
516
517
518
519
			continue;
		if (!rbitset_is_set(out_req->limited, reg->index))
			continue;
		return i;
	}
	panic("Tried querying undefined register '%s' at Start", reg->name);
}

ir_node *be_get_initial_reg_value(ir_graph *irg, const arch_register_t *reg)
{
520
521
522
523
	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);
524
525
}

526
527
static ir_entity* dummy_get_frame_entity(const ir_node *node)
{
Matthias Braun's avatar
Matthias Braun committed
528
	(void)node;
529
530
531
532
533
	return NULL;
}

static void dummy_set_frame_offset(ir_node *node, int bias)
{
Matthias Braun's avatar
Matthias Braun committed
534
535
	(void)node;
	(void)bias;
536
	panic("should not be called");
537
538
539
540
}

static int dummy_get_sp_bias(const ir_node *node)
{
Matthias Braun's avatar
Matthias Braun committed
541
	(void)node;
542
543
544
545
546
	return 0;
}

/* for "middleend" nodes */
static const arch_irn_ops_t dummy_be_irn_ops = {
547
548
549
	.get_frame_entity = dummy_get_frame_entity,
	.set_frame_offset = dummy_set_frame_offset,
	.get_sp_bias      = dummy_get_sp_bias,
550
551
};

552
ir_node *be_new_Phi(ir_node *block, int n_ins, ir_node **ins, ir_mode *mode,
553
                    const arch_register_req_t *req)
554
{
Matthias Braun's avatar
Matthias Braun committed
555
556
	ir_graph *irg  = get_irn_irg(block);
	ir_node  *phi = new_ir_node(NULL, irg, block, op_Phi, mode, n_ins, ins);
557
	phi->attr.phi.u.backedge = new_backedge_arr(get_irg_obstack(irg), n_ins);
Matthias Braun's avatar
Matthias Braun committed
558
559
	struct obstack *obst = be_get_be_obst(irg);
	backend_info_t *info = be_get_info(phi);
560
561
	info->out_infos = NEW_ARR_DZ(reg_out_info_t, obst, 1);
	info->in_reqs   = OALLOCN(obst, const arch_register_req_t*, n_ins);
562
563

	info->out_infos[0].req = req;
Matthias Braun's avatar
Matthias Braun committed
564
	for (int i = 0; i < n_ins; ++i) {
565
		info->in_reqs[i] = req;
566
	}
Matthias Braun's avatar
Matthias Braun committed
567
	verify_new_node(irg, phi);
568
	phi = optimize_node(phi);
569
570
571
	return phi;
}

572
void be_set_phi_reg_req(ir_node *node, const arch_register_req_t *req)
573
{
Matthias Braun's avatar
Matthias Braun committed
574
	assert(mode_is_data(get_irn_mode(node)));
575
576
	backend_info_t *info = be_get_info(node);
	info->out_infos[0].req = req;
Matthias Braun's avatar
Matthias Braun committed
577
	for (int i = 0, arity = get_irn_arity(node); i < arity; ++i) {
578
579
		info->in_reqs[i] = req;
	}
580
581
}

582
void be_dump_phi_reg_reqs(FILE *F, const ir_node *node, dump_reason_t reason)
583
{
584
	switch (reason) {
585
	case dump_node_opcode_txt:
586
		fputs(get_irn_opname(node), F);
587
588
589
590
591
592
		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
593
	case dump_node_info_txt: {
594
595
		ir_graph *irg = get_irn_irg(node);
		if (irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_BACKEND)) {
596
			be_dump_reqs_and_registers(F, node);
597
		}
598
		break;
599
	}
600
	}
601
602
}

Matthias Braun's avatar
Matthias Braun committed
603
static const arch_irn_ops_t phi_irn_ops = {
604
605
606
	dummy_get_frame_entity,
	dummy_set_frame_offset,
	dummy_get_sp_bias,
607
608
	NULL,    /* get_op_estimated_cost   */
	NULL,    /* perform_memory_operand  */
Sebastian Hack's avatar
Sebastian Hack committed
609
610
};

611
612
613
/**
 * ir_op-Operation: dump a be node to file
 */
614
static void dump_node(FILE *f, const ir_node *irn, dump_reason_t reason)
615
{
616
617
	assert(is_be_node(irn));

618
	switch (reason) {
Matthias Braun's avatar
Matthias Braun committed
619
620
621
622
623
624
625
626
	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:
627
		if (be_is_IncSP(irn)) {
Matthias Braun's avatar
Matthias Braun committed
628
629
630
631
632
			const be_incsp_attr_t *attr = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
			fprintf(f, " [%d] ", attr->offset);
		}
		break;
	case dump_node_info_txt:
633
		be_dump_reqs_and_registers(f, irn);
Matthias Braun's avatar
Matthias Braun committed
634
635
636
637

		switch (get_be_irn_opcode(irn)) {
		case beo_IncSP: {
			const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
638
			fprintf(f, "align: %u\n", a->align);
Matthias Braun's avatar
Matthias Braun committed
639
			fprintf(f, "offset: %d\n", a->offset);
640
			break;
Matthias Braun's avatar
Matthias Braun committed
641
642
		}
		case beo_MemPerm: {
643
			for (unsigned i = 0; i < be_get_MemPerm_entity_arity(irn); ++i) {
Matthias Braun's avatar
Matthias Braun committed
644
645
646
				ir_entity *in  = be_get_MemPerm_in_entity(irn, i);
				ir_entity *out = be_get_MemPerm_out_entity(irn, i);
				if (in != NULL)
647
					fprintf(f, "\nin[%u]: %s\n", i, get_entity_name(in));
Matthias Braun's avatar
Matthias Braun committed
648
				if (out != NULL)
649
					fprintf(f, "\nout[%u]: %s\n", i, get_entity_name(out));
650
			}
651
			break;
Matthias Braun's avatar
Matthias Braun committed
652
		}
Matthias Braun's avatar
Matthias Braun committed
653

Matthias Braun's avatar
Matthias Braun committed
654
655
656
		default:
			break;
		}
657
658
659
	}
}

Sebastian Hack's avatar
Sebastian Hack committed
660
/**
661
 * ir_op-Operation:
Sebastian Hack's avatar
Sebastian Hack committed
662
663
 * Copies the backend specific attributes from old node to new node.
 */
664
static void copy_attr(ir_graph *irg, const ir_node *old_node, ir_node *new_node)
Sebastian Hack's avatar
Sebastian Hack committed
665
{
Matthias Braun's avatar
Matthias Braun committed
666
667
668
	assert(is_be_node(old_node));
	assert(is_be_node(new_node));

669
670
	const void     *old_attr = get_irn_generic_attr_const(old_node);
	void           *new_attr = get_irn_generic_attr(new_node);
671
	struct obstack *obst     = be_get_be_obst(irg);
672
673
	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
674
	memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
675
676
	new_info->flags     = old_info->flags;
	new_info->out_infos = old_info->out_infos ? DUP_ARR_D(reg_out_info_t, obst, old_info->out_infos) : NULL;
677

678
	/* input infos */
679
680
	if (old_info->in_reqs != NULL) {
		unsigned n_ins = get_irn_arity(old_node);
681
		/* need dynamic in infos? */
682
		if (is_irn_dynamic(old_node)) {
683
			new_info->in_reqs = NEW_ARR_F(const arch_register_req_t*, n_ins);
684
		} else {
685
			new_info->in_reqs = OALLOCN(obst,const arch_register_req_t*, n_ins);
Sebastian Hack's avatar
Sebastian Hack committed
686
		}
Christoph Mallon's avatar
Christoph Mallon committed
687
		MEMCPY(new_info->in_reqs, old_info->in_reqs, n_ins);
688
	} else {
689
		new_info->in_reqs = NULL;
Sebastian Hack's avatar
Sebastian Hack committed
690
	}
Sebastian Hack's avatar
Sebastian Hack committed
691
692
}

693
694
695
696
697
698
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
699
{
700
701
	assert(is_be_node(node));
	return (be_opcode) (get_irn_opcode(node) - be_opcode_start);
Matthias Braun's avatar
Matthias Braun committed
702
703
}

704
705
706
707
708
709
710
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);
	res->ops.dump_node = dump_node;
	res->ops.copy_attr = copy_attr;
	res->ops.be_ops    = &be_node_irn_ops;
711
	set_op_tag(res, be_op_tag);
712
713
714
	return res;
}

715
716
void be_init_op(void)
{
717
	assert(op_be_Perm == NULL);
Matthias Braun's avatar
Matthias Braun committed
718

719
720
	be_opcode_start = get_next_ir_opcodes(beo_last+1);

Matthias Braun's avatar
Matthias Braun committed
721
	/* Acquire all needed opcodes. */
722
	unsigned o = be_opcode_start;
Matthias Braun's avatar
Matthias Braun committed
723
724
725
726
727
728
	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));
	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_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_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_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));
Matthias Braun's avatar
Matthias Braun committed
729

730
731
732
733
734
735
	set_op_attrs_equal(op_be_Perm,     attrs_equal_be_node);
	set_op_attrs_equal(op_be_MemPerm,  attrs_equal_be_node);
	set_op_attrs_equal(op_be_Copy,     attrs_equal_be_node);
	set_op_attrs_equal(op_be_Keep,     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);
736
737

	/* attach out dummy_ops to middle end nodes */
738
	for (unsigned opc = iro_first; opc <= iro_last; ++opc) {
Matthias Braun's avatar
Matthias Braun committed
739
		ir_op *op = ir_get_opcode(opc);
740
741
742
		assert(op->ops.be_ops == NULL);
		op->ops.be_ops = &dummy_be_irn_ops;
	}
743
	op_Phi->ops.be_ops = &phi_irn_ops;
Matthias Braun's avatar
Matthias Braun committed
744
}
Matthias Braun's avatar
Matthias Braun committed
745
746
747

void be_finish_op(void)
{
Matthias Braun's avatar
Matthias Braun committed
748
749
750
751
752
753
	free_ir_op(op_be_Perm);     op_be_Perm     = NULL;
	free_ir_op(op_be_MemPerm);  op_be_MemPerm  = NULL;
	free_ir_op(op_be_Copy);     op_be_Copy     = NULL;
	free_ir_op(op_be_Keep);     op_be_Keep     = 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
754
}