benode.c 23.1 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->reg_class);
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
287
288
	ir_node  *in[] = { old_sp };
	ir_node  *irn  = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->reg_class->mode,
	                             ARRAY_SIZE(in), in);
Matthias Braun's avatar
Matthias Braun committed
289
	init_node_attr(irn, 1, 1, arch_irn_flags_none);
Matthias Braun's avatar
Matthias Braun committed
290
	be_incsp_attr_t *a    = (be_incsp_attr_t*)get_irn_generic_attr(irn);
291
292
293
	a->offset             = offset;
	a->align              = align;
	a->base.exc.pin_state = op_pin_state_pinned;
Sebastian Hack's avatar
Sebastian Hack committed
294
295

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

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

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

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

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

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

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

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

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

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

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

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

389
const arch_register_req_t *be_create_reg_req(struct obstack *obst,
390
		const arch_register_t *reg, arch_register_req_type_t additional_types)
Sebastian Hack's avatar
Sebastian Hack committed
391
{
Matthias Braun's avatar
Matthias Braun committed
392
393
394
395
396
	const arch_register_class_t *cls     = reg->reg_class;
	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);
397
398
	req->type    = arch_register_req_type_limited | additional_types;
	req->cls     = cls;
Matthias Braun's avatar
Matthias Braun committed
399
	req->limited = limited;
400
	req->width   = 1;
401
	return req;
402
403
404
405
406
}

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

	foreach_out_edge(start, edge) {
		ir_node *proj = get_edge_src_irn(edge);
		if (!is_Proj(proj)) // maybe End/Anchor
			continue;
539
		if (get_Proj_num(proj) == i) {
540
541
542
543
544
545
			return proj;
		}
	}
	return new_r_Proj(start, mode, i);
}

546
547
static ir_entity* dummy_get_frame_entity(const ir_node *node)
{
Matthias Braun's avatar
Matthias Braun committed
548
	(void)node;
549
550
551
552
553
	return NULL;
}

static void dummy_set_frame_offset(ir_node *node, int bias)
{
Matthias Braun's avatar
Matthias Braun committed
554
555
	(void)node;
	(void)bias;
556
	panic("should not be called");
557
558
559
560
}

static int dummy_get_sp_bias(const ir_node *node)
{
Matthias Braun's avatar
Matthias Braun committed
561
	(void)node;
562
563
564
565
566
	return 0;
}

/* for "middleend" nodes */
static const arch_irn_ops_t dummy_be_irn_ops = {
567
568
569
	.get_frame_entity = dummy_get_frame_entity,
	.set_frame_offset = dummy_set_frame_offset,
	.get_sp_bias      = dummy_get_sp_bias,
570
571
};

572
ir_node *be_new_Phi(ir_node *block, int n_ins, ir_node **ins, ir_mode *mode,
573
                    const arch_register_req_t *req)
574
{
Matthias Braun's avatar
Matthias Braun committed
575
576
	ir_graph *irg  = get_irn_irg(block);
	ir_node  *phi = new_ir_node(NULL, irg, block, op_Phi, mode, n_ins, ins);
577
	phi->attr.phi.u.backedge = new_backedge_arr(get_irg_obstack(irg), n_ins);
Matthias Braun's avatar
Matthias Braun committed
578
579
	struct obstack *obst = be_get_be_obst(irg);
	backend_info_t *info = be_get_info(phi);
580
581
	info->out_infos = NEW_ARR_DZ(reg_out_info_t, obst, 1);
	info->in_reqs   = OALLOCN(obst, const arch_register_req_t*, n_ins);
582
583

	info->out_infos[0].req = req;
Matthias Braun's avatar
Matthias Braun committed
584
	for (int i = 0; i < n_ins; ++i) {
585
		info->in_reqs[i] = req;
586
	}
Matthias Braun's avatar
Matthias Braun committed
587
	verify_new_node(irg, phi);
588
	phi = optimize_node(phi);
589
590
591
	return phi;
}

592
void be_set_phi_reg_req(ir_node *node, const arch_register_req_t *req)
593
{
Matthias Braun's avatar
Matthias Braun committed
594
	assert(mode_is_data(get_irn_mode(node)));
595
596
	backend_info_t *info = be_get_info(node);
	info->out_infos[0].req = req;
Matthias Braun's avatar
Matthias Braun committed
597
	for (int i = 0, arity = get_irn_arity(node); i < arity; ++i) {
598
599
		info->in_reqs[i] = req;
	}
600
601
}

602
void be_dump_phi_reg_reqs(FILE *F, const ir_node *node, dump_reason_t reason)
603
{
604
	switch (reason) {
605
	case dump_node_opcode_txt:
606
		fputs(get_irn_opname(node), F);
607
608
609
610
611
612
		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
613
	case dump_node_info_txt: {
614
615
		ir_graph *irg = get_irn_irg(node);
		if (irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_BACKEND)) {
616
			be_dump_reqs_and_registers(F, node);
617
		}
618
		break;
619
	}
620
	}
621
622
}

Matthias Braun's avatar
Matthias Braun committed
623
static const arch_irn_ops_t phi_irn_ops = {
624
625
626
	dummy_get_frame_entity,
	dummy_set_frame_offset,
	dummy_get_sp_bias,
627
628
	NULL,    /* get_op_estimated_cost   */
	NULL,    /* perform_memory_operand  */
Sebastian Hack's avatar
Sebastian Hack committed
629
630
};

631
632
633
/**
 * ir_op-Operation: dump a be node to file
 */
634
static void dump_node(FILE *f, const ir_node *irn, dump_reason_t reason)
635
{
636
637
	assert(is_be_node(irn));

638
	switch (reason) {
Matthias Braun's avatar
Matthias Braun committed
639
640
641
642
643
644
645
646
	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:
647
		if (be_is_IncSP(irn)) {
Matthias Braun's avatar
Matthias Braun committed
648
649
650
651
652
			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:
653
		be_dump_reqs_and_registers(f, irn);
Matthias Braun's avatar
Matthias Braun committed
654
655
656
657

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

Matthias Braun's avatar
Matthias Braun committed
674
675
676
		default:
			break;
		}
677
678
679
	}
}

Sebastian Hack's avatar
Sebastian Hack committed
680
/**
681
 * ir_op-Operation:
Sebastian Hack's avatar
Sebastian Hack committed
682
683
 * Copies the backend specific attributes from old node to new node.
 */
684
static void copy_attr(ir_graph *irg, const ir_node *old_node, ir_node *new_node)
Sebastian Hack's avatar
Sebastian Hack committed
685
{
Matthias Braun's avatar
Matthias Braun committed
686
687
688
	assert(is_be_node(old_node));
	assert(is_be_node(new_node));

689
690
	const void     *old_attr = get_irn_generic_attr_const(old_node);
	void           *new_attr = get_irn_generic_attr(new_node);
691
	struct obstack *obst     = be_get_be_obst(irg);
692
693
	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
694
	memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
695
696
	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;
697

698
	/* input infos */
699
700
	if (old_info->in_reqs != NULL) {
		unsigned n_ins = get_irn_arity(old_node);
701
702
		/* need dynamic in infos? */
		if (get_irn_op(old_node)->opar == oparity_dynamic) {
703
			new_info->in_reqs = NEW_ARR_F(const arch_register_req_t*, n_ins);
704
		} else {
705
			new_info->in_reqs = OALLOCN(obst,const arch_register_req_t*, n_ins);
Sebastian Hack's avatar
Sebastian Hack committed
706
		}
Christoph Mallon's avatar
Christoph Mallon committed
707
		MEMCPY(new_info->in_reqs, old_info->in_reqs, n_ins);
708
	} else {
709
		new_info->in_reqs = NULL;
Sebastian Hack's avatar
Sebastian Hack committed
710
	}
Sebastian Hack's avatar
Sebastian Hack committed
711
712
}

713
714
715
716
717
718
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
719
{
720
721
	assert(is_be_node(node));
	return (be_opcode) (get_irn_opcode(node) - be_opcode_start);
Matthias Braun's avatar
Matthias Braun committed
722
723
}

724
725
726
727
728
729
730
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;
731
	set_op_tag(res, be_op_tag);
732
733
734
	return res;
}

735
736
void be_init_op(void)
{
737
	assert(op_be_Perm == NULL);
Matthias Braun's avatar
Matthias Braun committed
738

739
740
	be_opcode_start = get_next_ir_opcodes(beo_last+1);

Matthias Braun's avatar
Matthias Braun committed
741
	/* Acquire all needed opcodes. */
742
	unsigned o = be_opcode_start;
Matthias Braun's avatar
Matthias Braun committed
743
744
745
746
747
748
	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
749

750
751
752
753
754
755
	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);
756
757

	/* attach out dummy_ops to middle end nodes */
758
	for (unsigned opc = iro_first; opc <= iro_last; ++opc) {
Matthias Braun's avatar
Matthias Braun committed
759
		ir_op *op = ir_get_opcode(opc);
760
761
762
		assert(op->ops.be_ops == NULL);
		op->ops.be_ops = &dummy_be_irn_ops;
	}
763
	op_Phi->ops.be_ops = &phi_irn_ops;
Matthias Braun's avatar
Matthias Braun committed
764
}
Matthias Braun's avatar
Matthias Braun committed
765
766
767

void be_finish_op(void)
{
Matthias Braun's avatar
Matthias Braun committed
768
769
770
771
772
773
	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
774
}