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

Sebastian Hack's avatar
Sebastian Hack committed
6
/**
Christian Würdig's avatar
Christian Würdig committed
7
8
9
 * @file
 * @brief       Backend node support for generic backend nodes.
 * @author      Sebastian Hack
Sebastian Hack's avatar
Sebastian Hack committed
10
 *
11
 * Backend node support for generic backend nodes.
Sebastian Hack's avatar
Sebastian Hack committed
12
13
14
 */
#include <stdlib.h>

Matthias Braun's avatar
Matthias Braun committed
15
16
17
18
#include "array.h"
#include "be_t.h"
#include "bearch.h"
#include "bedump.h"
19
#include "beirg.h"
Matthias Braun's avatar
Matthias Braun committed
20
21
22
23
#include "belive.h"
#include "benode.h"
#include "besched.h"
#include "bitfiddle.h"
Sebastian Hack's avatar
Sebastian Hack committed
24
#include "debug.h"
25
#include "fourcc.h"
Matthias Braun's avatar
Matthias Braun committed
26
#include "irbackedge_t.h"
27
#include "ircons_t.h"
Matthias Braun's avatar
Matthias Braun committed
28
#include "irgopt.h"
Sebastian Hack's avatar
Sebastian Hack committed
29
#include "irgwalk.h"
Matthias Braun's avatar
Matthias Braun committed
30
31
32
#include "irmode_t.h"
#include "irnode_t.h"
#include "irop_t.h"
Michael Beck's avatar
Michael Beck committed
33
#include "iropt_t.h"
Matthias Braun's avatar
Matthias Braun committed
34
#include "irprintf.h"
35
#include "irverify_t.h"
Matthias Braun's avatar
Matthias Braun committed
36
37
38
39
40
41
#include "obst.h"
#include "panic.h"
#include "pmap.h"
#include "raw_bitset.h"
#include "set.h"
#include "util.h"
Sebastian Hack's avatar
Sebastian Hack committed
42

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

51
typedef struct {
52
53
54
	be_node_attr_t base;
	ir_entity    **in_entities;
	ir_entity    **out_entities;
55
	int            offset;
Matthias Braun's avatar
Matthias Braun committed
56
} be_memperm_attr_t;
57

Matthias Braun's avatar
Matthias Braun committed
58
ir_op *op_be_AnyVal;
59
ir_op *op_be_Asm;
60
ir_op *op_be_Copy;
Sebastian Hack's avatar
Sebastian Hack committed
61
ir_op *op_be_CopyKeep;
62
ir_op *op_be_IncSP;
Matthias Braun's avatar
Matthias Braun committed
63
64
65
ir_op *op_be_Keep;
ir_op *op_be_MemPerm;
ir_op *op_be_Perm;
Sebastian Hack's avatar
Sebastian Hack committed
66

67
68
#define be_op_tag FOURCC('B', 'E', '\0', '\0')

69
70
71
72
73
74
75
static int be_asm_attr_equal(ir_node const *const a, ir_node const *const b)
{
	be_asm_attr_t const *const attr_a = get_be_asm_attr_const(a);
	be_asm_attr_t const *const attr_b = get_be_asm_attr_const(b);
	return attr_a->text == attr_b->text && attr_a->operands == attr_b->operands;
}

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

88
89
90
91
92
93
arch_register_req_t const **be_allocate_in_reqs(ir_graph *const irg, unsigned const n)
{
	struct obstack *const obst = be_get_be_obst(irg);
	return OALLOCN(obst, arch_register_req_t const*, n);
}

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);
97
	arch_register_req_t *req = OALLOCZ(obst, arch_register_req_t);
98
99
	return req;
}
Matthias Braun's avatar
Matthias Braun committed
100

Matthias Braun's avatar
Matthias Braun committed
101
102
static void be_node_set_register_req_in(ir_node *const node, int const pos,
                                        arch_register_req_t const *const req)
103
{
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
 * Initializes the generic attribute of all be nodes and return it.
111
 */
112
static void init_node_attr(ir_node *const node, unsigned const n_outputs, arch_irn_flags_t const flags)
Sebastian Hack's avatar
Sebastian Hack committed
113
{
114
115
	ir_graph       *irg  = get_irn_irg(node);
	backend_info_t *info = be_get_info(node);
Sebastian Hack's avatar
Sebastian Hack committed
116

117
	unsigned                    const arity   = get_irn_arity(node);
118
119
	arch_register_req_t const **const in_reqs =
		is_irn_dynamic(node) ? NEW_ARR_F(arch_register_req_t const*, arity) :
120
		arity != 0           ? be_allocate_in_reqs(irg, arity) :
121
122
123
		NULL;
	for (unsigned i = 0; i < arity; ++i) {
		in_reqs[i] = arch_no_register_req;
124
	}
125
	info->in_reqs = in_reqs;
126

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

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

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

163
ir_node *be_new_MemPerm(ir_node *const block, int n, ir_node *const *const in)
Matthias Braun's avatar
Matthias Braun committed
164
{
165
166
	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
167

168
	init_node_attr(irn, n, arch_irn_flags_none);
Matthias Braun's avatar
Matthias Braun committed
169

Matthias Braun's avatar
Matthias Braun committed
170
	be_memperm_attr_t *attr = (be_memperm_attr_t*)get_irn_generic_attr(irn);
171
172
	attr->in_entities  = OALLOCNZ(get_irg_obstack(irg), ir_entity*, n);
	attr->out_entities = OALLOCNZ(get_irg_obstack(irg), ir_entity*, n);
173
	attr->offset       = 0;
Matthias Braun's avatar
Matthias Braun committed
174
175
176
	return irn;
}

177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
static void set_copy_info(ir_node *const irn, ir_graph *const irg, ir_node *const op, arch_irn_flags_t const flags)
{
	init_node_attr(irn, 1, flags);
	be_node_attr_t *const attr = (be_node_attr_t*)get_irn_generic_attr(irn);
	attr->exc.pin_state = op_pin_state_floats;

	arch_register_req_t   const *const op_req = arch_get_irn_register_req(op);
	arch_register_class_t const *const cls    = op_req->cls;

	be_node_set_register_req_in(irn, 0, cls->class_req);

	arch_register_req_t *const out_req = allocate_reg_req(irg);
	out_req->cls            = cls;
	out_req->should_be_same = 1U << 0;
	out_req->aligned        = op_req->aligned;
	out_req->width          = op_req->width;
	arch_set_irn_register_req_out(irn, 0, out_req);
}

196
ir_node *be_new_Copy(ir_node *bl, ir_node *op)
Sebastian Hack's avatar
Sebastian Hack committed
197
{
198
	ir_graph *irg  = get_irn_irg(bl);
Matthias Braun's avatar
Matthias Braun committed
199
200
201
	ir_node  *in[] = { op };
	ir_node  *res  = new_ir_node(NULL, irg, bl, op_be_Copy, get_irn_mode(op),
	                             ARRAY_SIZE(in), in);
202
	set_copy_info(res, irg, op, arch_irn_flags_none);
Sebastian Hack's avatar
Sebastian Hack committed
203
	return res;
204
205
}

206
207
ir_node *be_get_Copy_op(const ir_node *cpy)
{
208
	return get_irn_n(cpy, n_be_Copy_op);
209
210
}

Matthias Braun's avatar
Matthias Braun committed
211
212
ir_node *be_new_Keep(ir_node *const block, int const n,
                     ir_node *const *const in)
Sebastian Hack's avatar
Sebastian Hack committed
213
{
214
	ir_graph *irg = get_irn_irg(block);
215
	ir_node  *res = new_ir_node(NULL, irg, block, op_be_Keep, mode_ANY, n, in);
216
	init_node_attr(res, 1, arch_irn_flag_schedule_first);
Matthias Braun's avatar
Matthias Braun committed
217
	be_node_attr_t *attr = (be_node_attr_t*) get_irn_generic_attr(res);
218
	attr->exc.pin_state = op_pin_state_pinned;
Sebastian Hack's avatar
Sebastian Hack committed
219

Matthias Braun's avatar
Matthias Braun committed
220
	for (int i = 0; i < n; ++i) {
221
		arch_register_req_t const *req = arch_get_irn_register_req(in[i]);
222
		req = req->cls != NULL ? req->cls->class_req : arch_no_register_req;
223
		be_node_set_register_req_in(res, i, req);
Sebastian Hack's avatar
Sebastian Hack committed
224
	}
225
226
227
228
	keep_alive(res);
	return res;
}

229
230
231
232
233
234
235
ir_node *be_new_Keep_one(ir_node *const kept)
{
	ir_node *const in[]  = { kept };
	ir_node *const block = get_nodes_block(kept);
	return be_new_Keep(block, ARRAY_SIZE(in), in);
}

236
ir_node *be_new_IncSP(const arch_register_t *sp, ir_node *bl,
237
                      ir_node *old_sp, int offset, unsigned align)
Sebastian Hack's avatar
Sebastian Hack committed
238
{
239
	ir_graph *irg = get_irn_irg(bl);
Matthias Braun's avatar
Matthias Braun committed
240
	ir_node  *in[] = { old_sp };
Matthias Braun's avatar
Matthias Braun committed
241
242
	ir_node  *irn  = new_ir_node(NULL, irg, bl, op_be_IncSP, sp->cls->mode,
	                             ARRAY_SIZE(in), in);
243
	init_node_attr(irn, 1, arch_irn_flags_none);
Matthias Braun's avatar
Matthias Braun committed
244
	be_incsp_attr_t *a    = (be_incsp_attr_t*)get_irn_generic_attr(irn);
245
246
247
	a->offset             = offset;
	a->align              = align;
	a->base.exc.pin_state = op_pin_state_pinned;
Sebastian Hack's avatar
Sebastian Hack committed
248
249

	/* Set output constraint to stack register. */
Matthias Braun's avatar
Matthias Braun committed
250
	be_node_set_register_req_in(irn, 0, sp->cls->class_req);
251
	arch_copy_irn_out_info(irn, 0, old_sp);
Sebastian Hack's avatar
Sebastian Hack committed
252
	return irn;
Sebastian Hack's avatar
Sebastian Hack committed
253
}
Sebastian Hack's avatar
Sebastian Hack committed
254

255
ir_node *be_new_CopyKeep(ir_node *const bl, ir_node *const src, int const n, ir_node *const *const in_keep)
Sebastian Hack's avatar
Sebastian Hack committed
256
{
Matthias Braun's avatar
Matthias Braun committed
257
	ir_mode  *mode  = get_irn_mode(src);
258
	ir_graph *irg   = get_irn_irg(bl);
Matthias Braun's avatar
Matthias Braun committed
259
260
	int       arity = n+1;
	ir_node **in    = ALLOCAN(ir_node*, arity);
Sebastian Hack's avatar
Sebastian Hack committed
261
	in[0] = src;
Christoph Mallon's avatar
Christoph Mallon committed
262
	MEMCPY(&in[1], in_keep, n);
Matthias Braun's avatar
Matthias Braun committed
263
	ir_node *irn = new_ir_node(NULL, irg, bl, op_be_CopyKeep, mode, arity, in);
264
	set_copy_info(irn, irg, src, arch_irn_flag_schedule_first);
265
266
267
268
	for (int i = 0; i < n; ++i) {
		ir_node *pred = in_keep[i];
		const arch_register_req_t *req = arch_get_irn_register_req(pred);
		req = req->cls != NULL ? req->cls->class_req : arch_no_register_req;
Matthias Braun's avatar
Matthias Braun committed
269
		be_node_set_register_req_in(irn, i+1, req);
270
	}
Sebastian Hack's avatar
Sebastian Hack committed
271
272
273
	return irn;
}

274
275
ir_node *be_get_CopyKeep_op(const ir_node *cpy)
{
276
	return get_irn_n(cpy, n_be_CopyKeep_op);
277
278
}

279
void be_set_MemPerm_in_entity(const ir_node *irn, unsigned n, ir_entity *ent)
Matthias Braun's avatar
Matthias Braun committed
280
{
281
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
282
283
	const be_memperm_attr_t *attr
		= (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
284
285
286
	attr->in_entities[n] = ent;
}

287
ir_entity* be_get_MemPerm_in_entity(const ir_node* irn, unsigned n)
Matthias Braun's avatar
Matthias Braun committed
288
{
289
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
290
291
	const be_memperm_attr_t *attr
		= (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
292
293
294
	return attr->in_entities[n];
}

295
void be_set_MemPerm_out_entity(const ir_node *irn, unsigned n, ir_entity *ent)
Matthias Braun's avatar
Matthias Braun committed
296
{
297
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
298
299
	const be_memperm_attr_t *attr
		= (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
300
301
302
	attr->out_entities[n] = ent;
}

303
ir_entity* be_get_MemPerm_out_entity(const ir_node* irn, unsigned n)
Matthias Braun's avatar
Matthias Braun committed
304
{
305
	assert(n < be_get_MemPerm_entity_arity(irn));
Matthias Braun's avatar
Matthias Braun committed
306
307
	const be_memperm_attr_t *attr
		= (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
308
309
310
	return attr->out_entities[n];
}

311
312
313
void be_set_MemPerm_offset(ir_node *irn, int offset)
{
	assert(be_is_MemPerm(irn));
Matthias Braun's avatar
Matthias Braun committed
314
	be_memperm_attr_t *attr = (be_memperm_attr_t*)get_irn_generic_attr(irn);
315
316
317
318
319
320
	attr->offset = offset;
}

int be_get_MemPerm_offset(const ir_node *irn)
{
	assert(be_is_MemPerm(irn));
Matthias Braun's avatar
Matthias Braun committed
321
322
	const be_memperm_attr_t *attr
		= (const be_memperm_attr_t*)get_irn_generic_attr_const(irn);
323
324
325
	return attr->offset;
}

326
unsigned be_get_MemPerm_entity_arity(const ir_node *irn)
327
{
Matthias Braun's avatar
Matthias Braun committed
328
	assert(be_is_MemPerm(irn));
329
	return get_irn_arity(irn);
330
331
}

Matthias Braun's avatar
Matthias Braun committed
332
333
334
335
336
ir_node *be_new_AnyVal(ir_node *block, const arch_register_class_t *cls)
{
	ir_graph *irg  = get_irn_irg(block);
	ir_mode  *mode = cls->mode;
	ir_node  *res  = new_ir_node(NULL, irg, block, op_be_AnyVal, mode, 0, NULL);
337
	init_node_attr(res, 1, arch_irn_flags_none);
Matthias Braun's avatar
Matthias Braun committed
338
339
340
341
342
343
	arch_set_irn_register_req_out(res, 0, cls->class_req);
	be_node_attr_t *attr = (be_node_attr_t*)get_irn_generic_attr(res);
	attr->exc.pin_state = op_pin_state_floats;
	return res;
}

344
const arch_register_req_t *be_create_reg_req(struct obstack *obst,
345
346
                                             const arch_register_t *reg,
                                             bool ignore)
Sebastian Hack's avatar
Sebastian Hack committed
347
{
348
	arch_register_class_t const *cls     = reg->cls;
Matthias Braun's avatar
Matthias Braun committed
349
350
	unsigned                    *limited
		= rbitset_obstack_alloc(obst, cls->n_regs);
Matthias Braun's avatar
Matthias Braun committed
351
	rbitset_set(limited, reg->index);
352
	arch_register_req_t *req = OALLOCZ(obst, arch_register_req_t);
353
	req->cls     = cls;
Matthias Braun's avatar
Matthias Braun committed
354
	req->limited = limited;
355
	req->width   = 1;
356
	req->ignore  = ignore;
357
	return req;
358
359
}

360
361
ir_node *be_get_IncSP_pred(ir_node *irn)
{
Michael Beck's avatar
BugFix:    
Michael Beck committed
362
363
364
365
	assert(be_is_IncSP(irn));
	return get_irn_n(irn, 0);
}

366
367
void be_set_IncSP_pred(ir_node *incsp, ir_node *pred)
{
368
369
370
371
	assert(be_is_IncSP(incsp));
	set_irn_n(incsp, 0, pred);
}

372
void be_set_IncSP_offset(ir_node *irn, int offset)
Sebastian Hack's avatar
Sebastian Hack committed
373
374
{
	assert(be_is_IncSP(irn));
Matthias Braun's avatar
Matthias Braun committed
375
	be_incsp_attr_t *a = (be_incsp_attr_t*)get_irn_generic_attr(irn);
Sebastian Hack's avatar
Sebastian Hack committed
376
377
	a->offset = offset;
}
378

379
int be_get_IncSP_offset(const ir_node *irn)
Sebastian Hack's avatar
Sebastian Hack committed
380
{
Sebastian Hack's avatar
Sebastian Hack committed
381
	assert(be_is_IncSP(irn));
Matthias Braun's avatar
Matthias Braun committed
382
383
	const be_incsp_attr_t *a
		= (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
Sebastian Hack's avatar
Sebastian Hack committed
384
385
	return a->offset;
}
Sebastian Hack's avatar
Sebastian Hack committed
386

387
unsigned be_get_IncSP_align(const ir_node *irn)
388
389
{
	assert(be_is_IncSP(irn));
Matthias Braun's avatar
Matthias Braun committed
390
391
	const be_incsp_attr_t *a
		= (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
392
393
394
	return a->align;
}

395
static unsigned get_start_reg_index(ir_graph *irg, const arch_register_t *reg)
396
397
{
	/* do a naive linear search... */
Matthias Braun's avatar
Matthias Braun committed
398
	ir_node *start  = get_irg_start(irg);
399
	be_foreach_out(start, i) {
Matthias Braun's avatar
Matthias Braun committed
400
401
		arch_register_req_t const *const out_req
			= arch_get_irn_register_req_out(start, i);
402
		if (out_req->limited == NULL)
403
			continue;
404
		if (out_req->cls != reg->cls)
405
406
407
408
409
			continue;
		if (!rbitset_is_set(out_req->limited, reg->index))
			continue;
		return i;
	}
410
	panic("tried querying undefined register '%s' at Start", reg->name);
411
412
413
414
}

ir_node *be_get_initial_reg_value(ir_graph *irg, const arch_register_t *reg)
{
415
416
417
418
	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);
419
420
}

421
ir_node *be_new_Phi(ir_node *block, int n_ins, ir_node **ins, ir_mode *mode,
422
                    const arch_register_req_t *req)
423
{
Matthias Braun's avatar
Matthias Braun committed
424
425
	ir_graph *irg  = get_irn_irg(block);
	ir_node  *phi = new_ir_node(NULL, irg, block, op_Phi, mode, n_ins, ins);
426
	phi->attr.phi.u.backedge = new_backedge_arr(get_irg_obstack(irg), n_ins);
Matthias Braun's avatar
Matthias Braun committed
427
428
	struct obstack *obst = be_get_be_obst(irg);
	backend_info_t *info = be_get_info(phi);
429
	info->out_infos = NEW_ARR_DZ(reg_out_info_t, obst, 1);
430
	info->in_reqs   = be_allocate_in_reqs(irg, n_ins);
431
432

	info->out_infos[0].req = req;
Matthias Braun's avatar
Matthias Braun committed
433
	for (int i = 0; i < n_ins; ++i) {
434
		info->in_reqs[i] = req;
435
	}
Matthias Braun's avatar
Matthias Braun committed
436
	verify_new_node(irg, phi);
437
	phi = optimize_node(phi);
438
439
440
	return phi;
}

441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
ir_node *be_new_Phi0(ir_node *const block, ir_mode *const mode, arch_register_req_t const *const req)
{
	ir_graph *const irg = get_irn_irg(block);
	ir_node  *const phi = new_ir_node(NULL, irg, block, op_Phi, mode, 0, NULL);
	struct obstack *const obst = be_get_be_obst(irg);
	backend_info_t *const info = be_get_info(phi);
	info->out_infos = NEW_ARR_DZ(reg_out_info_t, obst, 1);
	info->out_infos[0].req = req;
	return phi;
}

ir_node *be_complete_Phi(ir_node *const phi, unsigned const n_ins, ir_node **const ins)
{
	assert(is_Phi(phi) && get_Phi_n_preds(phi) == 0);

	ir_graph *const irg = get_irn_irg(phi);
	phi->attr.phi.u.backedge = new_backedge_arr(get_irg_obstack(irg), n_ins);
	set_irn_in(phi, n_ins, ins);

460
	arch_register_req_t const **const in_reqs = be_allocate_in_reqs(irg, n_ins);
461
462
463
464
465
466
467
468
469
470
471
	arch_register_req_t const  *const req     = arch_get_irn_register_req(phi);
	for (unsigned i = 0; i < n_ins; ++i) {
		in_reqs[i] = req;
	}
	backend_info_t *const info = be_get_info(phi);
	info->in_reqs = in_reqs;

	verify_new_node(irg, phi);
	return optimize_node(phi);
}

472
void be_set_phi_reg_req(ir_node *node, const arch_register_req_t *req)
473
{
Matthias Braun's avatar
Matthias Braun committed
474
	assert(mode_is_data(get_irn_mode(node)));
475
476
	backend_info_t *info = be_get_info(node);
	info->out_infos[0].req = req;
Matthias Braun's avatar
Matthias Braun committed
477
	for (int i = 0, arity = get_irn_arity(node); i < arity; ++i) {
478
479
		info->in_reqs[i] = req;
	}
480
481
}

482
void be_dump_phi_reg_reqs(FILE *F, const ir_node *node, dump_reason_t reason)
483
{
484
	switch (reason) {
485
	case dump_node_opcode_txt:
486
		fputs(get_irn_opname(node), F);
487
488
489
490
491
492
		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
493
	case dump_node_info_txt: {
494
495
		ir_graph *irg = get_irn_irg(node);
		if (irg_is_constrained(irg, IR_GRAPH_CONSTRAINT_BACKEND)) {
496
			be_dump_reqs_and_registers(F, node);
497
		}
498
		break;
499
	}
500
	}
501
502
}

503
504
505
506
507
508
509
510
511
512
513
514
515
ir_node *be_new_Asm(dbg_info *const dbgi, ir_node *const block, int const n_ins, ir_node **const ins, int const n_outs, ident *const text, void *const operands)
{
	ir_graph *const irg  = get_irn_irg(block);
	ir_node  *const asmn = new_ir_node(dbgi, irg, block, op_be_Asm, mode_T, n_ins, ins);
	be_info_init_irn(asmn, arch_irn_flags_none, NULL, n_outs);

	be_asm_attr_t *const attr = (be_asm_attr_t*)get_irn_generic_attr(asmn);
	attr->text     = text;
	attr->operands = operands;

	return asmn;
}

516
517
518
/**
 * ir_op-Operation: dump a be node to file
 */
519
static void dump_node(FILE *f, const ir_node *irn, dump_reason_t reason)
520
{
521
522
	assert(is_be_node(irn));

523
	switch (reason) {
Matthias Braun's avatar
Matthias Braun committed
524
525
526
527
528
529
530
531
	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:
532
		if (be_is_IncSP(irn)) {
Matthias Braun's avatar
Matthias Braun committed
533
534
			const be_incsp_attr_t *attr
				= (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
Matthias Braun's avatar
Matthias Braun committed
535
536
537
538
			fprintf(f, " [%d] ", attr->offset);
		}
		break;
	case dump_node_info_txt:
539
		be_dump_reqs_and_registers(f, irn);
Matthias Braun's avatar
Matthias Braun committed
540

541
		if (be_is_IncSP(irn)) {
Matthias Braun's avatar
Matthias Braun committed
542
543
			const be_incsp_attr_t *a
				= (const be_incsp_attr_t*)get_irn_generic_attr_const(irn);
544
			fprintf(f, "align: %u\n", a->align);
Matthias Braun's avatar
Matthias Braun committed
545
			fprintf(f, "offset: %d\n", a->offset);
546
		} else if (be_is_MemPerm(irn)) {
547
			for (unsigned i = 0; i < be_get_MemPerm_entity_arity(irn); ++i) {
Matthias Braun's avatar
Matthias Braun committed
548
549
550
				ir_entity *in  = be_get_MemPerm_in_entity(irn, i);
				ir_entity *out = be_get_MemPerm_out_entity(irn, i);
				if (in != NULL)
551
					fprintf(f, "\nin[%u]: %s\n", i, get_entity_name(in));
Matthias Braun's avatar
Matthias Braun committed
552
				if (out != NULL)
553
					fprintf(f, "\nout[%u]: %s\n", i, get_entity_name(out));
554
			}
Matthias Braun's avatar
Matthias Braun committed
555
		}
556
		break;
557
558
559
	}
}

Sebastian Hack's avatar
Sebastian Hack committed
560
/**
561
 * ir_op-Operation:
Sebastian Hack's avatar
Sebastian Hack committed
562
563
 * Copies the backend specific attributes from old node to new node.
 */
564
static void copy_attr(ir_graph *irg, const ir_node *old_node, ir_node *new_node)
Sebastian Hack's avatar
Sebastian Hack committed
565
{
Matthias Braun's avatar
Matthias Braun committed
566
567
568
	assert(is_be_node(old_node));
	assert(is_be_node(new_node));

569
570
	const void     *old_attr = get_irn_generic_attr_const(old_node);
	void           *new_attr = get_irn_generic_attr(new_node);
571
	struct obstack *obst     = be_get_be_obst(irg);
572
573
	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
574
	memcpy(new_attr, old_attr, get_op_attr_size(get_irn_op(old_node)));
575
	new_info->flags     = old_info->flags;
Matthias Braun's avatar
Matthias Braun committed
576
577
578
	new_info->out_infos = old_info->out_infos
	                    ? DUP_ARR_D(reg_out_info_t, obst, old_info->out_infos)
	                    : NULL;
579

580
	/* input infos */
581
582
	if (old_info->in_reqs != NULL) {
		unsigned n_ins = get_irn_arity(old_node);
583
		/* need dynamic in infos? */
584
		if (is_irn_dynamic(old_node)) {
585
			new_info->in_reqs = NEW_ARR_F(const arch_register_req_t*, n_ins);
586
		} else {
587
			new_info->in_reqs = be_allocate_in_reqs(irg, n_ins);
Sebastian Hack's avatar
Sebastian Hack committed
588
		}
Christoph Mallon's avatar
Christoph Mallon committed
589
		MEMCPY(new_info->in_reqs, old_info->in_reqs, n_ins);
590
	} else {
591
		new_info->in_reqs = NULL;
Sebastian Hack's avatar
Sebastian Hack committed
592
	}
Sebastian Hack's avatar
Sebastian Hack committed
593
594
}

595
596
597
598
599
bool is_be_node(const ir_node *irn)
{
	return get_op_tag(get_irn_op(irn)) == be_op_tag;
}

600
601
602
603
static ir_op *new_be_op(unsigned code, const char *name, op_pin_state p,
                        irop_flags flags, op_arity opar, size_t attr_size)
{
	ir_op *res = new_ir_op(code, name, p, flags, opar, 0, attr_size);
Matthias Braun's avatar
Matthias Braun committed
604
605
	set_op_dump(res, dump_node);
	set_op_copy_attr(res, copy_attr);
606
	set_op_tag(res, be_op_tag);
607
608
609
	return res;
}

610
611
void be_init_op(void)
{
612
	assert(op_be_Perm == NULL);
Matthias Braun's avatar
Matthias Braun committed
613

Matthias Braun's avatar
Matthias Braun committed
614
	/* Acquire all needed opcodes. */
615
	unsigned const o = get_next_ir_opcodes(beo_last + 1);
Matthias Braun's avatar
Matthias Braun committed
616
	op_be_AnyVal   = new_be_op(o+beo_AnyVal,   "be_AnyVal",   op_pin_state_exc_pinned, irop_flag_constlike|irop_flag_cse_neutral, oparity_any,      sizeof(be_node_attr_t));
617
	op_be_Asm      = new_be_op(o+beo_Asm,      "be_Asm",      op_pin_state_exc_pinned, irop_flag_none,                            oparity_any,      sizeof(be_asm_attr_t));
Matthias Braun's avatar
Matthias Braun committed
618
619
620
	op_be_Copy     = new_be_op(o+beo_Copy,     "be_Copy",     op_pin_state_exc_pinned, irop_flag_none,                            oparity_any,      sizeof(be_node_attr_t));
	op_be_CopyKeep = new_be_op(o+beo_CopyKeep, "be_CopyKeep", op_pin_state_exc_pinned, irop_flag_keep,                            oparity_variable, sizeof(be_node_attr_t));
	op_be_IncSP    = new_be_op(o+beo_IncSP,    "be_IncSP",    op_pin_state_exc_pinned, irop_flag_none,                            oparity_any,      sizeof(be_incsp_attr_t));
621
	op_be_Keep     = new_be_op(o+beo_Keep,     "be_Keep",     op_pin_state_exc_pinned, irop_flag_keep,                            oparity_variable, sizeof(be_node_attr_t));
Matthias Braun's avatar
Matthias Braun committed
622
623
624
	op_be_MemPerm  = new_be_op(o+beo_MemPerm,  "be_MemPerm",  op_pin_state_exc_pinned, irop_flag_none,                            oparity_variable, sizeof(be_memperm_attr_t));
	op_be_Perm     = new_be_op(o+beo_Perm,     "be_Perm",     op_pin_state_exc_pinned, irop_flag_none,                            oparity_variable, sizeof(be_node_attr_t));

625
	set_op_attrs_equal(op_be_Asm,      be_asm_attr_equal);
Matthias Braun's avatar
Matthias Braun committed
626
	set_op_attrs_equal(op_be_AnyVal,   attrs_equal_be_node);
627
628
629
	set_op_attrs_equal(op_be_Copy,     attrs_equal_be_node);
	set_op_attrs_equal(op_be_CopyKeep, attrs_equal_be_node);
	set_op_attrs_equal(op_be_IncSP,    be_incsp_attrs_equal);
Matthias Braun's avatar
Matthias Braun committed
630
631
632
	set_op_attrs_equal(op_be_Keep,     attrs_equal_be_node);
	set_op_attrs_equal(op_be_MemPerm,  attrs_equal_be_node);
	set_op_attrs_equal(op_be_Perm,     attrs_equal_be_node);
Matthias Braun's avatar
Matthias Braun committed
633
}
Matthias Braun's avatar
Matthias Braun committed
634
635
636

void be_finish_op(void)
{
Matthias Braun's avatar
Matthias Braun committed
637
	free_ir_op(op_be_AnyVal);   op_be_AnyVal   = NULL;
Matthias Braun's avatar
Matthias Braun committed
638
639
640
	free_ir_op(op_be_Copy);     op_be_Copy     = NULL;
	free_ir_op(op_be_CopyKeep); op_be_CopyKeep = NULL;
	free_ir_op(op_be_IncSP);    op_be_IncSP    = NULL;
Matthias Braun's avatar
Matthias Braun committed
641
642
643
	free_ir_op(op_be_Keep);     op_be_Keep     = NULL;
	free_ir_op(op_be_MemPerm);  op_be_MemPerm  = NULL;
	free_ir_op(op_be_Perm);     op_be_Perm     = NULL;
Matthias Braun's avatar
Matthias Braun committed
644
}