ia32_finish.c 13.8 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
/*
Michael Beck's avatar
Michael Beck committed
2
 * Copyright (C) 1995-2008 University of Karlsruhe.  All right reserved.
Christian Würdig's avatar
Christian Würdig committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
 *
 * This file is part of libFirm.
 *
 * This file may be distributed and/or modified under the terms of the
 * GNU General Public License version 2 as published by the Free Software
 * Foundation and appearing in the file LICENSE.GPL included in the
 * packaging of this file.
 *
 * Licensees holding valid libFirm Professional Edition licenses may use
 * this file in accordance with the libFirm Commercial License.
 * Agreement provided with the Software.
 *
 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE.
 */

Christian Würdig's avatar
Christian Würdig committed
20
/**
21
22
23
24
 * @file
 * @brief   This file implements functions to finalize the irg for emit.
 * @author  Christian Wuerdig
 * @version $Id$
Christian Würdig's avatar
Christian Würdig committed
25
 */
Matthias Braun's avatar
Matthias Braun committed
26
27
#include "config.h"

Christian Würdig's avatar
Christian Würdig committed
28
29
30
31
#include "irnode.h"
#include "ircons.h"
#include "irgmod.h"
#include "irgwalk.h"
32
#include "iredges.h"
33
#include "irprintf.h"
Christian Würdig's avatar
Christian Würdig committed
34
#include "pdeq.h"
35
#include "error.h"
Christian Würdig's avatar
Christian Würdig committed
36

37
38
#include "../bearch.h"
#include "../besched.h"
Christian Würdig's avatar
Christian Würdig committed
39
40
41
42
43
44
#include "../benode_t.h"

#include "bearch_ia32_t.h"
#include "ia32_finish.h"
#include "ia32_new_nodes.h"
#include "ia32_map_regs.h"
45
#include "ia32_common_transform.h"
Christian Würdig's avatar
Christian Würdig committed
46
47
48
#include "ia32_transform.h"
#include "ia32_dbg_stat.h"
#include "ia32_optimize.h"
49
#include "gen_ia32_regalloc_if.h"
Christian Würdig's avatar
Christian Würdig committed
50

51
52
DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)

Christian Würdig's avatar
Christian Würdig committed
53
54
55
56
/**
 * Transforms a Sub or xSub into Neg--Add iff OUT_REG == SRC2_REG.
 * THIS FUNCTIONS MUST BE CALLED AFTER REGISTER ALLOCATION.
 */
Christoph Mallon's avatar
Christoph Mallon committed
57
58
static void ia32_transform_sub_to_neg_add(ir_node *irn, ia32_code_gen_t *cg)
{
59
	ir_graph *irg;
Christian Würdig's avatar
Christian Würdig committed
60
	ir_node *in1, *in2, *noreg, *nomem, *res;
61
	ir_node *noreg_fp, *block;
62
63
	dbg_info *dbg;
	const arch_register_t *in1_reg, *in2_reg, *out_reg;
Christian Würdig's avatar
Christian Würdig committed
64

65
	/* fix_am will solve this for AddressMode variants */
66
	if (get_ia32_op_type(irn) != ia32_Normal)
Christian Würdig's avatar
Christian Würdig committed
67
68
		return;

69
70
	noreg    = ia32_new_NoReg_gp(cg);
	noreg_fp = ia32_new_NoReg_xmm(cg);
71
	nomem    = new_NoMem();
72
73
	in1      = get_irn_n(irn, n_ia32_binary_left);
	in2      = get_irn_n(irn, n_ia32_binary_right);
74
75
	in1_reg  = arch_get_irn_register(in1);
	in2_reg  = arch_get_irn_register(in2);
76
	out_reg  = arch_irn_get_register(irn, 0);
Christian Würdig's avatar
Christian Würdig committed
77

78
79
	irg     = cg->irg;
	block   = get_nodes_block(irn);
Christian Würdig's avatar
Christian Würdig committed
80
81

	/* in case of sub and OUT == SRC2 we can transform the sequence into neg src2 -- add */
82
	if (out_reg != in2_reg)
83
84
		return;

85
86
	dbg = get_irn_dbg_info(irn);

87
	/* generate the neg src2 */
88
	if (is_ia32_xSub(irn)) {
89
		int size;
90
		ir_entity *entity;
91
		ir_mode *op_mode = get_ia32_ls_mode(irn);
92

93
94
		assert(get_irn_mode(irn) != mode_T);

95
		res = new_bd_ia32_xXor(dbg, block, noreg, noreg, nomem, in2, noreg_fp);
96
		size = get_mode_size_bits(op_mode);
97
98
		entity = ia32_gen_fp_known_const(size == 32 ? ia32_SSIGN : ia32_DSIGN);
		set_ia32_am_sc(res, entity);
99
		set_ia32_op_type(res, ia32_AddrModeS);
100
		set_ia32_ls_mode(res, op_mode);
Christian Würdig's avatar
Christian Würdig committed
101

102
		arch_set_irn_register(res, in2_reg);
Christian Würdig's avatar
Christian Würdig committed
103

104
105
106
107
		/* add to schedule */
		sched_add_before(irn, res);

		/* generate the add */
108
		res = new_bd_ia32_xAdd(dbg, block, noreg, noreg, nomem, res, in1);
109
		set_ia32_ls_mode(res, get_ia32_ls_mode(irn));
110
111
112
113
114
115

		/* exchange the add and the sub */
		edges_reroute(irn, res, irg);

		/* add to schedule */
		sched_add_before(irn, res);
116
	} else {
117
118
119
120
		ir_node         *res_proj   = NULL;
		ir_node         *flags_proj = NULL;
		const ir_edge_t *edge;

121
		if (get_irn_mode(irn) == mode_T) {
Michael Beck's avatar
Michael Beck committed
122
			/* collect the Proj uses */
123
124
125
			foreach_out_edge(irn, edge) {
				ir_node *proj = get_edge_src_irn(edge);
				long     pn   = get_Proj_proj(proj);
Christoph Mallon's avatar
Christoph Mallon committed
126
				if (pn == pn_ia32_Sub_res) {
127
128
129
130
131
132
133
134
135
					assert(res_proj == NULL);
					res_proj = proj;
				} else {
					assert(pn == pn_ia32_Sub_flags);
					assert(flags_proj == NULL);
					flags_proj = proj;
				}
			}
		}
Christian Würdig's avatar
Christian Würdig committed
136

137
		if (flags_proj == NULL) {
138
			res = new_bd_ia32_Neg(dbg, block, in2);
139
			arch_set_irn_register(res, in2_reg);
140
141
142

			/* add to schedule */
			sched_add_before(irn, res);
Christian Würdig's avatar
Christian Würdig committed
143

144
			/* generate the add */
145
			res = new_bd_ia32_Add(dbg, block, noreg, noreg, nomem, res, in1);
146
			arch_set_irn_register(res, out_reg);
147
			set_ia32_commutative(res);
Christian Würdig's avatar
Christian Würdig committed
148

149
150
151
152
153
154
155
156
157
			/* exchange the add and the sub */
			edges_reroute(irn, res, irg);

			/* add to schedule */
			sched_add_before(irn, res);
		} else {
			ir_node *stc, *cmc, *not, *adc;
			ir_node *adc_flags;

Michael Beck's avatar
Michael Beck committed
158
159
160
161
162
163
164
			/*
			 * ARG, the above technique does NOT set the flags right.
			 * So, we must produce the following code:
			 * t1 = ~b
			 * t2 = a + ~b + Carry
			 * Complement Carry
			 *
165
			 * a + -b = a + (~b + 1)  would set the carry flag IF a == b ...
Michael Beck's avatar
Michael Beck committed
166
			 */
167
			not = new_bd_ia32_Not(dbg, block, in2);
168
			arch_set_irn_register(not, in2_reg);
169
170
			sched_add_before(irn, not);

171
			stc = new_bd_ia32_Stc(dbg, block);
172
			arch_set_irn_register(stc, &ia32_flags_regs[REG_EFLAGS]);
173
			sched_add_before(irn, stc);
174

175
			adc = new_bd_ia32_Adc(dbg, block, noreg, noreg, nomem, not, in1, stc);
176
			arch_set_irn_register(adc, out_reg);
177
178
			sched_add_before(irn, adc);

179
			set_irn_mode(adc, mode_T);
180
			adc_flags = new_r_Proj(block, adc, mode_Iu, pn_ia32_Adc_flags);
181
			arch_set_irn_register(adc_flags, &ia32_flags_regs[REG_EFLAGS]);
182

183
			cmc = new_bd_ia32_Cmc(dbg, block, adc_flags);
184
			arch_set_irn_register(cmc, &ia32_flags_regs[REG_EFLAGS]);
185
186
187
			sched_add_before(irn, cmc);

			exchange(flags_proj, cmc);
188
			if (res_proj != NULL) {
189
190
191
192
193
194
195
196
				set_Proj_pred(res_proj, adc);
				set_Proj_proj(res_proj, pn_ia32_Adc_res);
			}

			res = adc;
		}
	}

197
198
	set_irn_mode(res, get_irn_mode(irn));

199
	SET_IA32_ORIG_NODE(res, irn);
200
201
202

	/* remove the old sub */
	sched_remove(irn);
203
	kill_node(irn);
204
205

	DBG_OPT_SUB2NEGADD(irn, res);
Christian Würdig's avatar
Christian Würdig committed
206
207
}

208
static inline int need_constraint_copy(ir_node *irn)
209
210
211
212
213
214
215
216
217
218
219
220
221
{
	/* TODO this should be determined from the node specification */
	switch (get_ia32_irn_opcode(irn)) {
		case iro_ia32_IMul: {
			/* the 3 operand form of IMul needs no constraint copy */
			ir_node *right = get_irn_n(irn, n_ia32_IMul_right);
			return !is_ia32_Immediate(right);
		}

		case iro_ia32_Lea:
		case iro_ia32_Conv_I2I:
		case iro_ia32_Conv_I2I8Bit:
		case iro_ia32_CMov:
Matthias Braun's avatar
Matthias Braun committed
222
223
			return 0;

224
225
226
		default:
			return 1;
	}
Christian Würdig's avatar
Christian Würdig committed
227
228
}

Michael Beck's avatar
Michael Beck committed
229
230
231
232
/**
 * Returns the index of the "same" register.
 * On the x86, we should have only one.
 */
233
234
235
236
237
static int get_first_same(const arch_register_req_t* req)
{
	const unsigned other = req->other_same;
	int i;

Michael Beck's avatar
Michael Beck committed
238
	for (i = 0; i < 32; ++i) {
239
240
		if (other & (1U << i)) return i;
	}
Michael Beck's avatar
Michael Beck committed
241
242
	assert(! "same position not found");
	return 32;
243
244
}

Christian Würdig's avatar
Christian Würdig committed
245
246
247
248
249
/**
 * Insert copies for all ia32 nodes where the should_be_same requirement
 * is not fulfilled.
 * Transform Sub into Neg -- Add if IN2 == OUT
 */
250
static void assure_should_be_same_requirements(ir_node *node)
251
{
Matthias Braun's avatar
Matthias Braun committed
252
	const arch_register_req_t **reqs;
253
	const arch_register_t      *out_reg, *in_reg;
Christian Würdig's avatar
Christian Würdig committed
254
	int                         n_res, i;
255
	ir_node                    *in_node, *block;
Christian Würdig's avatar
Christian Würdig committed
256

257
	reqs  = get_ia32_out_req_all(node);
258
	n_res = arch_irn_get_n_outs(node);
259
260
261
262
263
264
265
266
267
268
	block = get_nodes_block(node);

	/* check all OUT requirements, if there is a should_be_same */
	for (i = 0; i < n_res; i++) {
		int                          i2, arity;
		int                          same_pos;
		ir_node                     *perm;
		ir_node                     *in[2];
		ir_node                     *perm_proj0;
		ir_node                     *perm_proj1;
Michael Beck's avatar
Michael Beck committed
269
		ir_node                     *uses_out_reg;
270
		const arch_register_req_t   *req = reqs[i];
271
		const arch_register_class_t *cls;
Michael Beck's avatar
Michael Beck committed
272
		int                         uses_out_reg_pos;
273
274
275
276

		if (!arch_register_req_is(req, should_be_same))
			continue;

277
		same_pos = get_first_same(req);
278
279

		/* get in and out register */
280
		out_reg  = arch_irn_get_register(node, i);
281
		in_node  = get_irn_n(node, same_pos);
282
		in_reg   = arch_get_irn_register(in_node);
283
284
285
286
287
288
289

		/* requirement already fulfilled? */
		if (in_reg == out_reg)
			continue;
		/* unknowns can be changed to any register we want on emitting */
		if (is_unknown_reg(in_reg))
			continue;
290
291
		cls = arch_register_get_class(in_reg);
		assert(cls == arch_register_get_class(out_reg));
292
293
294

		/* check if any other input operands uses the out register */
		arity = get_irn_arity(node);
Michael Beck's avatar
Michael Beck committed
295
296
		uses_out_reg     = NULL;
		uses_out_reg_pos = -1;
Christoph Mallon's avatar
Christoph Mallon committed
297
		for (i2 = 0; i2 < arity; ++i2) {
298
			ir_node               *in     = get_irn_n(node, i2);
Matthias Braun's avatar
Matthias Braun committed
299
300
			const arch_register_t *in_reg;

Christoph Mallon's avatar
Christoph Mallon committed
301
			if (!mode_is_data(get_irn_mode(in)))
Matthias Braun's avatar
Matthias Braun committed
302
303
				continue;

304
			in_reg = arch_get_irn_register(in);
305

Christoph Mallon's avatar
Christoph Mallon committed
306
			if (in_reg != out_reg)
307
308
				continue;

Christoph Mallon's avatar
Christoph Mallon committed
309
			if (uses_out_reg != NULL && in != uses_out_reg) {
310
				panic("invalid register allocation");
Christian Würdig's avatar
Christian Würdig committed
311
			}
312
			uses_out_reg = in;
Christoph Mallon's avatar
Christoph Mallon committed
313
			if (uses_out_reg_pos >= 0)
314
315
316
				uses_out_reg_pos = -1; /* multiple inputs... */
			else
				uses_out_reg_pos = i2;
Christian Würdig's avatar
Christian Würdig committed
317
318
		}

319
		/* no-one else is using the out reg, we can simply copy it
320
321
		 * (the register can't be live since the operation will override it
		 *  anyway) */
Christoph Mallon's avatar
Christoph Mallon committed
322
		if (uses_out_reg == NULL) {
323
			ir_node *copy = be_new_Copy(cls, block, in_node);
324
			DBG_OPT_2ADDRCPY(copy);
325

326
			/* destination is the out register */
327
			arch_set_irn_register(copy, out_reg);
328

329
330
331
332
333
334
			/* insert copy before the node into the schedule */
			sched_add_before(node, copy);

			/* set copy as in */
			set_irn_n(node, same_pos, copy);

335
336
337
			DBG((dbg, LEVEL_1,
				"created copy %+F for should be same argument at input %d of %+F\n",
				copy, same_pos, node));
338
339
340
341
			continue;
		}

		/* for commutative nodes we can simply swap the left/right */
Michael Beck's avatar
Michael Beck committed
342
		if (uses_out_reg_pos == n_ia32_binary_right && is_ia32_commutative(node)) {
343
			ia32_swap_left_right(node);
344
345
346
			DBG((dbg, LEVEL_1,
				"swapped left/right input of %+F to resolve should be same constraint\n",
				node));
347
348
349
350
351
352
353
354
355
356
357
			continue;
		}

#ifdef DEBUG_libfirm
		ir_fprintf(stderr, "Note: need perm to resolve should_be_same constraint at %+F (this is unsafe and should not happen in theory...)\n", node);
#endif
		/* the out reg is used as node input: we need to permutate our input
		 * and the other (this is allowed, since the other node can't be live
		 * after! the operation as we will override the register. */
		in[0] = in_node;
		in[1] = uses_out_reg;
358
		perm  = be_new_Perm(cls, block, 2, in);
359

360
361
		perm_proj0 = new_r_Proj(block, perm, get_irn_mode(in[0]), 0);
		perm_proj1 = new_r_Proj(block, perm, get_irn_mode(in[1]), 1);
362

363
364
		arch_set_irn_register(perm_proj0, out_reg);
		arch_set_irn_register(perm_proj1, in_reg);
365
366
367

		sched_add_before(node, perm);

368
369
370
		DBG((dbg, LEVEL_1,
			"created perm %+F for should be same argument at input %d of %+F (need permutate with %+F)\n",
			perm, same_pos, node, uses_out_reg));
371
372

		/* use the perm results */
Christoph Mallon's avatar
Christoph Mallon committed
373
		for (i2 = 0; i2 < arity; ++i2) {
374
375
			ir_node *in = get_irn_n(node, i2);

Christoph Mallon's avatar
Christoph Mallon committed
376
			if (in == in_node) {
377
				set_irn_n(node, i2, perm_proj0);
Christoph Mallon's avatar
Christoph Mallon committed
378
			} else if (in == uses_out_reg) {
379
380
381
382
				set_irn_n(node, i2, perm_proj1);
			}
		}
	}
Christian Würdig's avatar
Christian Würdig committed
383
384
}

385
386
387
/**
 * Following Problem:
 * We have a source address mode node with base or index register equal to
388
389
390
 * result register and unfulfilled should_be_same requirement. The constraint
 * handler will insert a copy from the remaining input operand to the result
 * register -> base or index is broken then.
391
392
 * Solution: Turn back this address mode into explicit Load + Operation.
 */
393
static void fix_am_source(ir_node *irn)
394
{
Matthias Braun's avatar
Matthias Braun committed
395
	const arch_register_req_t **reqs;
396
	int                         n_res, i;
397
398

	/* check only ia32 nodes with source address mode */
399
	if (!is_ia32_irn(irn) || get_ia32_op_type(irn) != ia32_AddrModeS)
400
		return;
401
	/* only need to fix binary operations */
402
	if (get_ia32_am_support(irn) != ia32_am_binary)
403
		return;
404

405
	reqs  = get_ia32_out_req_all(irn);
406
	n_res = arch_irn_get_n_outs(irn);
407
408

	for (i = 0; i < n_res; i++) {
409
410
411
412
413
		const arch_register_t *out_reg;
		int                    same_pos;
		ir_node               *same_node;
		const arch_register_t *same_reg;
		ir_node               *load_res;
414

415
416
		if (!arch_register_req_is(reqs[i], should_be_same))
			continue;
417

418
		/* get in and out register */
419
		out_reg   = arch_irn_get_register(irn, i);
420
421
		same_pos  = get_first_same(reqs[i]);
		same_node = get_irn_n(irn, same_pos);
422
		same_reg  = arch_get_irn_register(same_node);
423

424
425
426
		/* should_be same constraint is fullfilled, nothing to do */
		if (out_reg == same_reg)
			continue;
427

428
429
		/* we only need to do something if the out reg is the same as base
			 or index register */
430
431
		if (out_reg != arch_get_irn_register(get_irn_n(irn, n_ia32_base)) &&
				out_reg != arch_get_irn_register(get_irn_n(irn, n_ia32_index)))
432
			continue;
433

434
		load_res = turn_back_am(irn);
435
		arch_set_irn_register(load_res, out_reg);
436

437
438
		DBG((dbg, LEVEL_3,
			"irg %+F: build back AM source for node %+F, inserted load %+F\n",
439
			get_irn_irg(irn), irn, get_Proj_pred(load_res)));
440
		break;
441
442
443
	}
}

444
445
446
/**
 * Block walker: finishes a block
 */
Christoph Mallon's avatar
Christoph Mallon committed
447
448
static void ia32_finish_irg_walker(ir_node *block, void *env)
{
449
	ia32_code_gen_t *cg = env;
Christian Würdig's avatar
Christian Würdig committed
450
451
	ir_node *irn, *next;

452
453
454
	/* first: turn back AM source if necessary */
	for (irn = sched_first(block); ! sched_is_end(irn); irn = next) {
		next = sched_next(irn);
455
		fix_am_source(irn);
456
457
	}

458
	for (irn = sched_first(block); ! sched_is_end(irn); irn = next) {
459
		next = sched_next(irn);
460

461
		/* check if there is a sub which need to be transformed */
462
463
464
		if (is_ia32_Sub(irn) || is_ia32_xSub(irn)) {
			ia32_transform_sub_to_neg_add(irn, cg);
		}
465
466
	}

467
	/* second: insert copies and finish irg */
Christian Würdig's avatar
Christian Würdig committed
468
469
	for (irn = sched_first(block); ! sched_is_end(irn); irn = next) {
		next = sched_next(irn);
Michael Beck's avatar
Michael Beck committed
470
471
472
473
		if (is_ia32_irn(irn)) {
			/* some nodes are just a bit less efficient, but need no fixing if the
			 * should be same requirement is not fulfilled */
			if (need_constraint_copy(irn))
474
				assure_should_be_same_requirements(irn);
Michael Beck's avatar
Michael Beck committed
475
		}
Christian Würdig's avatar
Christian Würdig committed
476
477
478
	}
}

479
480
481
/**
 * Block walker: pushes all blocks on a wait queue
 */
Christoph Mallon's avatar
Christoph Mallon committed
482
483
static void ia32_push_on_queue_walker(ir_node *block, void *env)
{
Christian Würdig's avatar
Christian Würdig committed
484
485
486
487
488
489
490
491
	waitq *wq = env;
	waitq_put(wq, block);
}


/**
 * Add Copy nodes for not fulfilled should_be_equal constraints
 */
Christoph Mallon's avatar
Christoph Mallon committed
492
493
void ia32_finish_irg(ir_graph *irg, ia32_code_gen_t *cg)
{
Christian Würdig's avatar
Christian Würdig committed
494
495
496
497
498
499
500
501
502
503
504
	waitq *wq = new_waitq();

	/* Push the blocks on the waitq because ia32_finish_irg_walker starts more walks ... */
	irg_block_walk_graph(irg, NULL, ia32_push_on_queue_walker, wq);

	while (! waitq_empty(wq)) {
		ir_node *block = waitq_get(wq);
		ia32_finish_irg_walker(block, cg);
	}
	del_waitq(wq);
}
505
506
507
508
509

void ia32_init_finish(void)
{
	FIRM_DBG_REGISTER(dbg, "firm.be.ia32.finish");
}