benode.c 22.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
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
	ir_graph              *irg      = get_irn_irg(block);
Matthias Braun's avatar
Matthias Braun committed
206
207
208
	const arch_env_t      *arch_env = be_get_irg_arch_env(irg);
	ir_node               *frame    = get_irg_frame(irg);
	const arch_register_t *sp       = arch_env->sp;
Matthias Braun's avatar
Matthias Braun committed
209

Matthias Braun's avatar
Matthias Braun committed
210
	ir_node **real_in = ALLOCAN(ir_node*, n + 1);
211
	real_in[0] = frame;
Christoph Mallon's avatar
Christoph Mallon committed
212
	MEMCPY(&real_in[1], in, n);
213

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

Matthias Braun's avatar
Matthias Braun committed
216
	init_node_attr(irn, n + 1, n, arch_irn_flags_none);
217
	be_node_set_reg_class_in(irn, 0, sp->cls);
Matthias Braun's avatar
Matthias Braun committed
218

Matthias Braun's avatar
Matthias Braun committed
219
	be_memperm_attr_t *attr = (be_memperm_attr_t*)get_irn_generic_attr(irn);
220
221
	attr->in_entities  = OALLOCNZ(get_irg_obstack(irg), ir_entity*, n);
	attr->out_entities = OALLOCNZ(get_irg_obstack(irg), ir_entity*, n);
222
	attr->offset       = 0;
Matthias Braun's avatar
Matthias Braun committed
223
224
225
	return irn;
}

226
ir_node *be_new_Copy(ir_node *bl, ir_node *op)
Sebastian Hack's avatar
Sebastian Hack committed
227
{
228
	ir_graph *irg  = get_irn_irg(bl);
Matthias Braun's avatar
Matthias Braun committed
229
230
231
	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
232
	init_node_attr(res, 1, 1, arch_irn_flags_none);
Matthias Braun's avatar
Matthias Braun committed
233
	be_node_attr_t *attr = (be_node_attr_t*) get_irn_generic_attr(res);
234
	attr->exc.pin_state = op_pin_state_floats;
Matthias Braun's avatar
Matthias Braun committed
235
236
237

	const arch_register_req_t   *in_req = arch_get_irn_register_req(op);
	const arch_register_class_t *cls    = in_req->cls;
238
239
	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
240

241
	arch_register_req_t *const req = allocate_reg_req(irg);
Matthias Braun's avatar
Matthias Braun committed
242
243
244
	req->cls  = cls;
	req->type = arch_register_req_type_should_be_same
	            | (in_req->type & arch_register_req_type_aligned);
245
	req->other_same = 1U << 0;
246
	req->width      = in_req->width;
247
	be_set_constr_out(res, 0, req);
Sebastian Hack's avatar
Sebastian Hack committed
248
	return res;
249
250
}

251
252
ir_node *be_get_Copy_op(const ir_node *cpy)
{
253
	return get_irn_n(cpy, n_be_Copy_op);
254
255
}

256
ir_node *be_new_Keep(ir_node *const block, int const n, ir_node *const *const in)
Sebastian Hack's avatar
Sebastian Hack committed
257
{
258
	ir_graph *irg = get_irn_irg(block);
Matthias Braun's avatar
Matthias Braun committed
259
	ir_node  *res = new_ir_node(NULL, irg, block, op_be_Keep, mode_ANY, -1, NULL);
Matthias Braun's avatar
Matthias Braun committed
260
	init_node_attr(res, -1, 1, arch_irn_flag_schedule_first);
Matthias Braun's avatar
Matthias Braun committed
261
	be_node_attr_t *attr = (be_node_attr_t*) get_irn_generic_attr(res);
262
	attr->exc.pin_state = op_pin_state_pinned;
Sebastian Hack's avatar
Sebastian Hack committed
263

Matthias Braun's avatar
Matthias Braun committed
264
	for (int i = 0; i < n; ++i) {
265
266
267
268
269
		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
270
	}
271
272
273
274
275
276
277
	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));
278
279
	add_irn_n(keep, node);
	add_register_req_in(keep, cls->class_req);
Sebastian Hack's avatar
Sebastian Hack committed
280
281
}

282
ir_node *be_new_IncSP(const arch_register_t *sp, ir_node *bl,
283
                      ir_node *old_sp, int offset, unsigned align)
Sebastian Hack's avatar
Sebastian Hack committed
284
{
285
	ir_graph *irg = get_irn_irg(bl);
Matthias Braun's avatar
Matthias Braun committed
286
	ir_node  *in[] = { old_sp };
287
	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
288
	init_node_attr(irn, 1, 1, arch_irn_flags_none);
Matthias Braun's avatar
Matthias Braun committed
289
	be_incsp_attr_t *a    = (be_incsp_attr_t*)get_irn_generic_attr(irn);
290
291
292
	a->offset             = offset;
	a->align              = align;
	a->base.exc.pin_state = op_pin_state_pinned;
Sebastian Hack's avatar
Sebastian Hack committed
293
294

	/* Set output constraint to stack register. */
295
	be_node_set_reg_class_in(irn, 0, sp->cls);
296
	be_set_constr_single_reg_out(irn, 0, sp, arch_register_req_type_produces_sp);
Sebastian Hack's avatar
Sebastian Hack committed
297
	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
298
}
Sebastian Hack's avatar
Sebastian Hack committed
299

300
ir_node *be_new_CopyKeep(ir_node *bl, ir_node *src, int n, ir_node *in_keep[])
Sebastian Hack's avatar
Sebastian Hack committed
301
{
Matthias Braun's avatar
Matthias Braun committed
302
	ir_mode  *mode  = get_irn_mode(src);
303
	ir_graph *irg   = get_irn_irg(bl);
Matthias Braun's avatar
Matthias Braun committed
304
305
	int       arity = n+1;
	ir_node **in    = ALLOCAN(ir_node*, arity);
Sebastian Hack's avatar
Sebastian Hack committed
306
	in[0] = src;
Christoph Mallon's avatar
Christoph Mallon committed
307
	MEMCPY(&in[1], in_keep, n);
Matthias Braun's avatar
Matthias Braun committed
308
	ir_node *irn = new_ir_node(NULL, irg, bl, op_be_CopyKeep, mode, arity, in);
Matthias Braun's avatar
Matthias Braun committed
309
	init_node_attr(irn, arity, 1, arch_irn_flag_schedule_first);
Matthias Braun's avatar
Matthias Braun committed
310
	be_node_attr_t *attr = (be_node_attr_t*)get_irn_generic_attr(irn);
311
	attr->exc.pin_state = op_pin_state_floats;
Matthias Braun's avatar
Matthias Braun committed
312
313
	const arch_register_req_t   *req  = arch_get_irn_register_req(src);
	const arch_register_class_t *cls  = req->cls;
314
315
	be_node_set_reg_class_in(irn, 0, cls);
	be_node_set_reg_class_out(irn, 0, cls);
316
317
318
319
320
321
	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
322
323
324
	return irn;
}

325
ir_node *be_new_CopyKeep_single(ir_node *bl, ir_node *src, ir_node *keep)
Sebastian Hack's avatar
Sebastian Hack committed
326
{
327
	return be_new_CopyKeep(bl, src, 1, &keep);
Sebastian Hack's avatar
Sebastian Hack committed
328
329
}

330
331
ir_node *be_get_CopyKeep_op(const ir_node *cpy)
{
332
	return get_irn_n(cpy, n_be_CopyKeep_op);
333
334
}

335
336
void be_set_CopyKeep_op(ir_node *cpy, ir_node *op)
{
337
	set_irn_n(cpy, n_be_CopyKeep_op, op);
338
339
}

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

347
ir_entity* be_get_MemPerm_in_entity(const ir_node* irn, unsigned n)
Matthias Braun's avatar
Matthias Braun committed
348
{
349
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
350
	const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
351
352
353
	return attr->in_entities[n];
}

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

361
ir_entity* be_get_MemPerm_out_entity(const ir_node* irn, unsigned n)
Matthias Braun's avatar
Matthias Braun committed
362
{
363
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
364
	const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
365
366
367
	return attr->out_entities[n];
}

368
369
370
void be_set_MemPerm_offset(ir_node *irn, int offset)
{
	assert(be_is_MemPerm(irn));
Matthias Braun's avatar
Matthias Braun committed
371
	be_memperm_attr_t *attr = (be_memperm_attr_t*)get_irn_generic_attr(irn);
372
373
374
375
376
377
	attr->offset = offset;
}

int be_get_MemPerm_offset(const ir_node *irn)
{
	assert(be_is_MemPerm(irn));
Matthias Braun's avatar
Matthias Braun committed
378
	const be_memperm_attr_t *attr = (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
379
380
381
	return attr->offset;
}

382
unsigned be_get_MemPerm_entity_arity(const ir_node *irn)
383
{
Matthias Braun's avatar
Matthias Braun committed
384
	assert(be_is_MemPerm(irn));
385
386
387
	return get_irn_arity(irn) - 1;
}

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

void be_set_constr_single_reg_in(ir_node *node, int pos,
		const arch_register_t *reg, arch_register_req_type_t additional_types)
{
406
407
408
409
410
	const arch_register_req_t *req;
	if (additional_types == 0) {
		req = reg->single_req;
	} else {
		ir_graph       *irg  = get_irn_irg(node);
411
		struct obstack *obst = be_get_be_obst(irg);
412
		req = be_create_reg_req(obst, reg, additional_types);
413
414
	}
	be_set_constr_in(node, pos, req);
415
416
417
418
419
}

void be_set_constr_single_reg_out(ir_node *node, int pos,
		const arch_register_t *reg, arch_register_req_type_t additional_types)
{
420
421
	ir_graph                  *irg  = get_irn_irg(node);
	be_irg_t                  *birg = be_birg_from_irg(irg);
422
423

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

Matthias Braun's avatar
Matthias Braun committed
427
	const arch_register_req_t *req;
428
429
430
	if (additional_types == 0) {
		req = reg->single_req;
	} else {
431
		struct obstack *obst = be_get_be_obst(irg);
432
		req = be_create_reg_req(obst, reg, additional_types);
433
434
	}

435
	arch_set_irn_register_out(node, pos, reg);
436
	be_set_constr_out(node, pos, req);
Sebastian Hack's avatar
Sebastian Hack committed
437
438
}

439
440
void be_node_set_reg_class_in(ir_node *irn, int pos,
                              const arch_register_class_t *cls)
441
{
442
	be_set_constr_in(irn, pos, cls->class_req);
443
444
}

445
446
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
447
{
448
	be_set_constr_out(irn, pos, cls->class_req);
Sebastian Hack's avatar
Sebastian Hack committed
449
450
}

451
452
ir_node *be_get_IncSP_pred(ir_node *irn)
{
Michael Beck's avatar
BugFix:    
Michael Beck committed
453
454
455
456
	assert(be_is_IncSP(irn));
	return get_irn_n(irn, 0);
}

457
458
void be_set_IncSP_pred(ir_node *incsp, ir_node *pred)
{
459
460
461
462
	assert(be_is_IncSP(incsp));
	set_irn_n(incsp, 0, pred);
}

463
void be_set_IncSP_offset(ir_node *irn, int offset)
Sebastian Hack's avatar
Sebastian Hack committed
464
465
{
	assert(be_is_IncSP(irn));
Matthias Braun's avatar
Matthias Braun committed
466
	be_incsp_attr_t *a = (be_incsp_attr_t*)get_irn_generic_attr(irn);
Sebastian Hack's avatar
Sebastian Hack committed
467
468
	a->offset = offset;
}
469

470
int be_get_IncSP_offset(const ir_node *irn)
Sebastian Hack's avatar
Sebastian Hack committed
471
{
Sebastian Hack's avatar
Sebastian Hack committed
472
	assert(be_is_IncSP(irn));
Matthias Braun's avatar
Matthias Braun committed
473
	const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
Sebastian Hack's avatar
Sebastian Hack committed
474
475
	return a->offset;
}
Sebastian Hack's avatar
Sebastian Hack committed
476

477
unsigned be_get_IncSP_align(const ir_node *irn)
478
479
{
	assert(be_is_IncSP(irn));
Matthias Braun's avatar
Matthias Braun committed
480
	const be_incsp_attr_t *a = (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
481
482
483
	return a->align;
}

484
static ir_entity *be_node_get_frame_entity(const ir_node *irn)
485
{
Matthias Braun's avatar
Matthias Braun committed
486
	if (be_is_MemPerm(irn))
487
488
		return be_get_MemPerm_in_entity(irn, 0);
	return NULL;
489
490
}

491
static void be_node_set_frame_offset(ir_node *irn, int offset)
492
{
Matthias Braun's avatar
Matthias Braun committed
493
	if (be_is_MemPerm(irn))
494
		be_set_MemPerm_offset(irn, offset);
495
496
}

497
static int be_node_get_sp_bias(const ir_node *irn)
498
{
499
	if (be_is_IncSP(irn))
Matthias Braun's avatar
Matthias Braun committed
500
501
		return be_get_IncSP_offset(irn);
	return 0;
502
503
}

504
/* for be nodes */
Matthias Braun's avatar
Matthias Braun committed
505
static const arch_irn_ops_t be_node_irn_ops = {
506
507
508
	.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
509
};
510

511
static unsigned get_start_reg_index(ir_graph *irg, const arch_register_t *reg)
512
513
{
	/* do a naive linear search... */
Matthias Braun's avatar
Matthias Braun committed
514
	ir_node *start  = get_irg_start(irg);
515
	be_foreach_out(start, i) {
516
517
		arch_register_req_t const *const out_req = arch_get_irn_register_req_out(start, i);
		if (!arch_register_req_is(out_req, limited))
518
			continue;
519
		if (out_req->cls != reg->cls)
520
521
522
523
524
525
526
527
528
529
			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)
{
530
	unsigned i     = get_start_reg_index(irg, reg);
531
	ir_node *start = get_irg_start(irg);
532
	ir_mode *mode  = arch_register_class_mode(reg->cls);
533

Christoph Mallon's avatar
Christoph Mallon committed
534
535
	ir_node *const proj = get_Proj_for_pn(start, i);
	return proj ? proj : new_r_Proj(start, mode, i);
536
537
}

538
539
static ir_entity* dummy_get_frame_entity(const ir_node *node)
{
Matthias Braun's avatar
Matthias Braun committed
540
	(void)node;
541
542
543
544
545
	return NULL;
}

static void dummy_set_frame_offset(ir_node *node, int bias)
{
Matthias Braun's avatar
Matthias Braun committed
546
547
	(void)node;
	(void)bias;
548
	panic("should not be called");
549
550
551
552
}

static int dummy_get_sp_bias(const ir_node *node)
{
Matthias Braun's avatar
Matthias Braun committed
553
	(void)node;
554
555
556
557
558
	return 0;
}

/* for "middleend" nodes */
static const arch_irn_ops_t dummy_be_irn_ops = {
559
560
561
	.get_frame_entity = dummy_get_frame_entity,
	.set_frame_offset = dummy_set_frame_offset,
	.get_sp_bias      = dummy_get_sp_bias,
562
563
};

564
ir_node *be_new_Phi(ir_node *block, int n_ins, ir_node **ins, ir_mode *mode,
565
                    const arch_register_req_t *req)
566
{
Matthias Braun's avatar
Matthias Braun committed
567
568
	ir_graph *irg  = get_irn_irg(block);
	ir_node  *phi = new_ir_node(NULL, irg, block, op_Phi, mode, n_ins, ins);
569
	phi->attr.phi.u.backedge = new_backedge_arr(get_irg_obstack(irg), n_ins);
Matthias Braun's avatar
Matthias Braun committed
570
571
	struct obstack *obst = be_get_be_obst(irg);
	backend_info_t *info = be_get_info(phi);
572
573
	info->out_infos = NEW_ARR_DZ(reg_out_info_t, obst, 1);
	info->in_reqs   = OALLOCN(obst, const arch_register_req_t*, n_ins);
574
575

	info->out_infos[0].req = req;
Matthias Braun's avatar
Matthias Braun committed
576
	for (int i = 0; i < n_ins; ++i) {
577
		info->in_reqs[i] = req;
578
	}
Matthias Braun's avatar
Matthias Braun committed
579
	verify_new_node(irg, phi);
580
	phi = optimize_node(phi);
581
582
583
	return phi;
}

584
void be_set_phi_reg_req(ir_node *node, const arch_register_req_t *req)
585
{
Matthias Braun's avatar
Matthias Braun committed
586
	assert(mode_is_data(get_irn_mode(node)));
587
588
	backend_info_t *info = be_get_info(node);
	info->out_infos[0].req = req;
Matthias Braun's avatar
Matthias Braun committed
589
	for (int i = 0, arity = get_irn_arity(node); i < arity; ++i) {
590
591
		info->in_reqs[i] = req;
	}
592
593
}

594
void be_dump_phi_reg_reqs(FILE *F, const ir_node *node, dump_reason_t reason)
595
{
596
	switch (reason) {
597
	case dump_node_opcode_txt:
598
		fputs(get_irn_opname(node), F);
599
600
601
602
603
604
		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
605
	case dump_node_info_txt: {
606
607
		ir_graph *irg = get_irn_irg(node);
		if (irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_BACKEND)) {
608
			be_dump_reqs_and_registers(F, node);
609
		}
610
		break;
611
	}
612
	}
613
614
}

Matthias Braun's avatar
Matthias Braun committed
615
static const arch_irn_ops_t phi_irn_ops = {
616
617
618
	dummy_get_frame_entity,
	dummy_set_frame_offset,
	dummy_get_sp_bias,
619
620
	NULL,    /* get_op_estimated_cost   */
	NULL,    /* perform_memory_operand  */
Sebastian Hack's avatar
Sebastian Hack committed
621
622
};

623
624
625
/**
 * ir_op-Operation: dump a be node to file
 */
626
static void dump_node(FILE *f, const ir_node *irn, dump_reason_t reason)
627
{
628
629
	assert(is_be_node(irn));

630
	switch (reason) {
Matthias Braun's avatar
Matthias Braun committed
631
632
633
634
635
636
637
638
	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:
639
		if (be_is_IncSP(irn)) {
Matthias Braun's avatar
Matthias Braun committed
640
641
642
643
644
			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:
645
		be_dump_reqs_and_registers(f, irn);
Matthias Braun's avatar
Matthias Braun committed
646
647
648
649

		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);
650
			fprintf(f, "align: %u\n", a->align);
Matthias Braun's avatar
Matthias Braun committed
651
			fprintf(f, "offset: %d\n", a->offset);
652
			break;
Matthias Braun's avatar
Matthias Braun committed
653
654
		}
		case beo_MemPerm: {
655
			for (unsigned i = 0; i < be_get_MemPerm_entity_arity(irn); ++i) {
Matthias Braun's avatar
Matthias Braun committed
656
657
658
				ir_entity *in  = be_get_MemPerm_in_entity(irn, i);
				ir_entity *out = be_get_MemPerm_out_entity(irn, i);
				if (in != NULL)
659
					fprintf(f, "\nin[%u]: %s\n", i, get_entity_name(in));
Matthias Braun's avatar
Matthias Braun committed
660
				if (out != NULL)
661
					fprintf(f, "\nout[%u]: %s\n", i, get_entity_name(out));
662
			}
663
			break;
Matthias Braun's avatar
Matthias Braun committed
664
		}
Matthias Braun's avatar
Matthias Braun committed
665

Matthias Braun's avatar
Matthias Braun committed
666
667
668
		default:
			break;
		}
669
670
671
	}
}

Sebastian Hack's avatar
Sebastian Hack committed
672
/**
673
 * ir_op-Operation:
Sebastian Hack's avatar
Sebastian Hack committed
674
675
 * Copies the backend specific attributes from old node to new node.
 */
676
static void copy_attr(ir_graph *irg, const ir_node *old_node, ir_node *new_node)
Sebastian Hack's avatar
Sebastian Hack committed
677
{
Matthias Braun's avatar
Matthias Braun committed
678
679
680
	assert(is_be_node(old_node));
	assert(is_be_node(new_node));

681
682
	const void     *old_attr = get_irn_generic_attr_const(old_node);
	void           *new_attr = get_irn_generic_attr(new_node);
683
	struct obstack *obst     = be_get_be_obst(irg);
684
685
	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
686
	memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
687
688
	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;
689

690
	/* input infos */
691
692
	if (old_info->in_reqs != NULL) {
		unsigned n_ins = get_irn_arity(old_node);
693
		/* need dynamic in infos? */
694
		if (is_irn_dynamic(old_node)) {
695
			new_info->in_reqs = NEW_ARR_F(const arch_register_req_t*, n_ins);
696
		} else {
697
			new_info->in_reqs = OALLOCN(obst,const arch_register_req_t*, n_ins);
Sebastian Hack's avatar
Sebastian Hack committed
698
		}
Christoph Mallon's avatar
Christoph Mallon committed
699
		MEMCPY(new_info->in_reqs, old_info->in_reqs, n_ins);
700
	} else {
701
		new_info->in_reqs = NULL;
Sebastian Hack's avatar
Sebastian Hack committed
702
	}
Sebastian Hack's avatar
Sebastian Hack committed
703
704
}

705
706
707
708
709
710
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
711
{
712
713
	assert(is_be_node(node));
	return (be_opcode) (get_irn_opcode(node) - be_opcode_start);
Matthias Braun's avatar
Matthias Braun committed
714
715
}

716
717
718
719
720
721
722
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;
723
	set_op_tag(res, be_op_tag);
724
725
726
	return res;
}

727
728
void be_init_op(void)
{
729
	assert(op_be_Perm == NULL);
Matthias Braun's avatar
Matthias Braun committed
730

731
732
	be_opcode_start = get_next_ir_opcodes(beo_last+1);

Matthias Braun's avatar
Matthias Braun committed
733
	/* Acquire all needed opcodes. */
734
	unsigned o = be_opcode_start;
Matthias Braun's avatar
Matthias Braun committed
735
736
737
738
739
740
	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
741

742
743
744
745
746
747
	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);
748
749

	/* attach out dummy_ops to middle end nodes */
750
	for (unsigned opc = iro_first; opc <= iro_last; ++opc) {
Matthias Braun's avatar
Matthias Braun committed
751
		ir_op *op = ir_get_opcode(opc);
752
753
754
		assert(op->ops.be_ops == NULL);
		op->ops.be_ops = &dummy_be_irn_ops;
	}
755
	op_Phi->ops.be_ops = &phi_irn_ops;
Matthias Braun's avatar
Matthias Braun committed
756
}
Matthias Braun's avatar
Matthias Braun committed
757
758
759

void be_finish_op(void)
{
Matthias Braun's avatar
Matthias Braun committed
760
761
762
763
764
765
	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
766
}