ia32_optimize.c 37 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
/**
Christian Würdig's avatar
Christian Würdig committed
21
22
 * @file
 * @brief       Implements several optimizations for IA32.
23
 * @author      Matthias Braun, Christian Wuerdig
Christian Würdig's avatar
Christian Würdig committed
24
 * @version     $Id$
Christian Würdig's avatar
Christian Würdig committed
25
 */
26
#ifdef HAVE_CONFIG_H
27
#include "config.h"
28
29
30
31
32
#endif

#include "irnode.h"
#include "irprog_t.h"
#include "ircons.h"
33
#include "irtools.h"
34
#include "firm_types.h"
Christian Würdig's avatar
Christian Würdig committed
35
36
37
#include "iredges.h"
#include "tv.h"
#include "irgmod.h"
38
#include "irgwalk.h"
Christian Würdig's avatar
Christian Würdig committed
39
#include "height.h"
40
#include "irbitset.h"
41
#include "irprintf.h"
Matthias Braun's avatar
Matthias Braun committed
42
#include "error.h"
Christian Würdig's avatar
Christian Würdig committed
43

Christian Würdig's avatar
Christian Würdig committed
44
45
#include "../be_t.h"
#include "../beabi.h"
Christian Würdig's avatar
Christian Würdig committed
46
47
#include "../benode_t.h"
#include "../besched_t.h"
48
#include "../bepeephole.h"
49
50

#include "ia32_new_nodes.h"
51
#include "ia32_optimize.h"
52
#include "bearch_ia32_t.h"
Matthias Braun's avatar
Matthias Braun committed
53
#include "gen_ia32_regalloc_if.h"
54
#include "ia32_common_transform.h"
Christian Würdig's avatar
Christian Würdig committed
55
#include "ia32_transform.h"
Christian Würdig's avatar
Christian Würdig committed
56
#include "ia32_dbg_stat.h"
Christian Würdig's avatar
Christian Würdig committed
57
#include "ia32_util.h"
58
#include "ia32_architecture.h"
59

60
61
DEBUG_ONLY(static firm_dbg_module_t *dbg = NULL;)

62
63
64
static const arch_env_t *arch_env;
static ia32_code_gen_t  *cg;

65
66
67
68
69
70
71
72
73
74
static void copy_mark(const ir_node *old, ir_node *new)
{
	if (is_ia32_is_reload(old))
		set_ia32_is_reload(new);
	if (is_ia32_is_spill(old))
		set_ia32_is_spill(new);
	if (is_ia32_is_remat(old))
		set_ia32_is_remat(new);
}

Michael Beck's avatar
Michael Beck committed
75
76
77
78
79
80
81
/**
 * Returns non-zero if the given node produces
 * a zero flag.
 *
 * @param node  the node to check
 * @param pn    if >= 0, the projection number of the used result
 */
82
83
84
85
86
static int produces_zero_flag(ir_node *node, int pn)
{
	ir_node                     *count;
	const ia32_immediate_attr_t *imm_attr;

Michael Beck's avatar
Michael Beck committed
87
	if (!is_ia32_irn(node))
88
89
		return 0;

Michael Beck's avatar
Michael Beck committed
90
91
	if (pn >= 0) {
		if (pn != pn_ia32_res)
92
93
94
			return 0;
	}

Michael Beck's avatar
Michael Beck committed
95
	switch (get_ia32_irn_opcode(node)) {
96
97
98
99
100
101
102
103
104
105
106
107
108
109
	case iro_ia32_Add:
	case iro_ia32_Adc:
	case iro_ia32_And:
	case iro_ia32_Or:
	case iro_ia32_Xor:
	case iro_ia32_Sub:
	case iro_ia32_Sbb:
	case iro_ia32_Neg:
	case iro_ia32_Inc:
	case iro_ia32_Dec:
		return 1;

	case iro_ia32_ShlD:
	case iro_ia32_ShrD:
Christoph Mallon's avatar
Christoph Mallon committed
110
111
112
113
		assert(n_ia32_ShlD_count == n_ia32_ShrD_count);
		count = get_irn_n(node, n_ia32_ShlD_count);
		goto check_shift_amount;

114
115
116
117
118
	case iro_ia32_Shl:
	case iro_ia32_Shr:
	case iro_ia32_Sar:
		assert(n_ia32_Shl_count == n_ia32_Shr_count
				&& n_ia32_Shl_count == n_ia32_Sar_count);
Christoph Mallon's avatar
Christoph Mallon committed
119
120
		count = get_irn_n(node, n_ia32_Shl_count);
check_shift_amount:
121
122
		/* when shift count is zero the flags are not affected, so we can only
		 * do this for constants != 0 */
Michael Beck's avatar
Michael Beck committed
123
		if (!is_ia32_Immediate(count))
124
125
126
			return 0;

		imm_attr = get_ia32_immediate_attr_const(count);
Michael Beck's avatar
Michael Beck committed
127
		if (imm_attr->symconst != NULL)
128
			return 0;
Michael Beck's avatar
Michael Beck committed
129
		if ((imm_attr->offset & 0x1f) == 0)
130
131
132
133
134
135
136
137
138
			return 0;
		return 1;

	default:
		break;
	}
	return 0;
}

Michael Beck's avatar
Michael Beck committed
139
140
141
142
143
144
145
/**
 * If the given node has not mode_T, creates a mode_T version (with a result Proj).
 *
 * @param node  the node to change
 *
 * @return the new mode_T node (if the mode was changed) or node itself
 */
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
static ir_node *turn_into_mode_t(ir_node *node)
{
	ir_node               *block;
	ir_node               *res_proj;
	ir_node               *new_node;
	const arch_register_t *reg;

	if(get_irn_mode(node) == mode_T)
		return node;

	assert(get_irn_mode(node) == mode_Iu);

	new_node = exact_copy(node);
	set_irn_mode(new_node, mode_T);

	block    = get_nodes_block(new_node);
	res_proj = new_r_Proj(current_ir_graph, block, new_node, mode_Iu,
	                      pn_ia32_res);

	reg = arch_get_irn_register(arch_env, node);
	arch_set_irn_register(arch_env, res_proj, reg);

	sched_add_before(node, new_node);
Christoph Mallon's avatar
Christoph Mallon committed
169
	be_peephole_exchange(node, res_proj);
170
171
172
	return new_node;
}

173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
/**
 * Replace Cmp(x, 0) by a Test(x, x)
 */
static void peephole_ia32_Cmp(ir_node *const node)
{
	ir_node                     *right;
	ia32_immediate_attr_t const *imm;
	dbg_info                    *dbgi;
	ir_graph                    *irg;
	ir_node                     *block;
	ir_node                     *noreg;
	ir_node                     *nomem;
	ir_node                     *op;
	ia32_attr_t           const *attr;
	int                          ins_permuted;
	int                          cmp_unsigned;
	ir_node                     *test;
	arch_register_t       const *reg;
191
192
	ir_edge_t             const *edge;
	ir_edge_t             const *tmp;
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226

	if (get_ia32_op_type(node) != ia32_Normal)
		return;

	right = get_irn_n(node, n_ia32_Cmp_right);
	if (!is_ia32_Immediate(right))
		return;

	imm = get_ia32_immediate_attr_const(right);
	if (imm->symconst != NULL || imm->offset != 0)
		return;

	dbgi         = get_irn_dbg_info(node);
	irg          = current_ir_graph;
	block        = get_nodes_block(node);
	noreg        = ia32_new_NoReg_gp(cg);
	nomem        = get_irg_no_mem(irg);
	op           = get_irn_n(node, n_ia32_Cmp_left);
	attr         = get_irn_generic_attr(node);
	ins_permuted = attr->data.ins_permuted;
	cmp_unsigned = attr->data.cmp_unsigned;

	if (is_ia32_Cmp(node)) {
		test = new_rd_ia32_Test(dbgi, irg, block, noreg, noreg, nomem,
		                        op, op, ins_permuted, cmp_unsigned);
	} else {
		test = new_rd_ia32_Test8Bit(dbgi, irg, block, noreg, noreg, nomem,
		                            op, op, ins_permuted, cmp_unsigned);
	}
	set_ia32_ls_mode(test, get_ia32_ls_mode(node));

	reg = arch_get_irn_register(arch_env, node);
	arch_set_irn_register(arch_env, test, reg);

227
228
229
230
231
232
233
	foreach_out_edge_safe(node, edge, tmp) {
		ir_node *const user = get_edge_src_irn(edge);

		if (is_Proj(user))
			exchange(user, test);
	}

234
	sched_add_before(node, test);
235
	copy_mark(node, test);
236
237
238
	be_peephole_exchange(node, test);
}

Michael Beck's avatar
Michael Beck committed
239
240
241
242
243
/**
 * Peephole optimization for Test instructions.
 * We can remove the Test, if a zero flags was produced which is still
 * live.
 */
244
245
static void peephole_ia32_Test(ir_node *node)
{
Michael Beck's avatar
Michael Beck committed
246
247
248
249
250
251
252
253
	ir_node         *left  = get_irn_n(node, n_ia32_Test_left);
	ir_node         *right = get_irn_n(node, n_ia32_Test_right);
	ir_node         *flags_proj;
	ir_node         *block;
	ir_mode         *flags_mode;
	int              pn    = -1;
	ir_node         *schedpoint;
	const ir_edge_t *edge;
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270

	assert(n_ia32_Test_left == n_ia32_Test8Bit_left
			&& n_ia32_Test_right == n_ia32_Test8Bit_right);

	/* we need a test for 0 */
	if(left != right)
		return;

	block = get_nodes_block(node);
	if(get_nodes_block(left) != block)
		return;

	if(is_Proj(left)) {
		pn   = get_Proj_proj(left);
		left = get_Proj_pred(left);
	}

271
272
273
274
	/* happens rarely, but if it does code will panic' */
	if (is_ia32_Unknown_GP(left))
		return;

275
276
	/* walk schedule up and abort when we find left or some other node destroys
	   the flags */
277
278
	schedpoint = node;
	for (;;) {
279
		schedpoint = sched_prev(schedpoint);
280
281
282
		if (schedpoint == left)
			break;
		if (arch_irn_is(arch_env, schedpoint, modify_flags))
283
			return;
284
		if (schedpoint == block)
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
			panic("couldn't find left");
	}

	/* make sure only Lg/Eq tests are used */
	foreach_out_edge(node, edge) {
		ir_node *user = get_edge_src_irn(edge);
		int      pnc  = get_ia32_condcode(user);

		if(pnc != pn_Cmp_Eq && pnc != pn_Cmp_Lg) {
			return;
		}
	}

	if(!produces_zero_flag(left, pn))
		return;

	left = turn_into_mode_t(left);

	flags_mode = ia32_reg_classes[CLASS_ia32_flags].mode;
	flags_proj = new_r_Proj(current_ir_graph, block, left, flags_mode,
	                        pn_ia32_flags);
	arch_set_irn_register(arch_env, flags_proj, &ia32_flags_regs[REG_EFLAGS]);

	assert(get_irn_mode(node) != mode_T);

Christoph Mallon's avatar
Christoph Mallon committed
310
	be_peephole_exchange(node, flags_proj);
311
312
}

313
314
315
316
317
318
/**
 * AMD Athlon works faster when RET is not destination of
 * conditional jump or directly preceded by other jump instruction.
 * Can be avoided by placing a Rep prefix before the return.
 */
static void peephole_ia32_Return(ir_node *node) {
Michael Beck's avatar
Michael Beck committed
319
	ir_node *block, *irn;
320
321
322
323
324
325
326
327

	if (!ia32_cg_config.use_pad_return)
		return;

	block = get_nodes_block(node);

	/* check if this return is the first on the block */
	sched_foreach_reverse_from(node, irn) {
328
		switch (get_irn_opcode(irn)) {
329
330
331
		case beo_Return:
			/* the return node itself, ignore */
			continue;
332
333
		case iro_Start:
		case beo_RegParams:
334
		case beo_Barrier:
335
			/* ignore no code generated */
336
337
338
339
340
341
			continue;
		case beo_IncSP:
			/* arg, IncSP 0 nodes might occur, ignore these */
			if (be_get_IncSP_offset(irn) == 0)
				continue;
			return;
342
343
		case iro_Phi:
			continue;
344
345
346
347
		default:
			return;
		}
	}
348

349
	/* ensure, that the 3 byte return is generated */
350
	be_Return_set_emit_pop(node, 1);
351
352
353
}

/* only optimize up to 48 stores behind IncSPs */
354
#define MAXPUSH_OPTIMIZE	48
355
356

/**
Michael Beck's avatar
Michael Beck committed
357
 * Tries to create Push's from IncSP, Store combinations.
358
359
 * The Stores are replaced by Push's, the IncSP is modified
 * (possibly into IncSP 0, but not removed).
360
 */
361
static void peephole_IncSP_Store_to_push(ir_node *irn)
362
{
363
364
365
366
367
368
369
370
371
372
373
374
	int              i;
	int              maxslot;
	int              inc_ofs;
	ir_node         *node;
	ir_node         *stores[MAXPUSH_OPTIMIZE];
	ir_node         *block;
	ir_graph        *irg;
	ir_node         *curr_sp;
	ir_mode         *spmode;
	ir_node         *first_push = NULL;
	ir_edge_t const *edge;
	ir_edge_t const *next;
375
376
377
378
379

	memset(stores, 0, sizeof(stores));

	assert(be_is_IncSP(irn));

380
381
	inc_ofs = be_get_IncSP_offset(irn);
	if (inc_ofs < 4)
382
383
384
385
		return;

	/*
	 * We first walk the schedule after the IncSP node as long as we find
386
	 * suitable Stores that could be transformed to a Push.
387
388
389
	 * We save them into the stores array which is sorted by the frame offset/4
	 * attached to the node
	 */
390
391
	maxslot = -1;
	for (node = sched_next(irn); !sched_is_end(node); node = sched_next(node)) {
392
		ir_node *mem;
393
		int offset;
394
395
		int storeslot;

396
397
		/* it has to be a Store */
		if (!is_ia32_Store(node))
398
399
			break;

400
401
		/* it has to use our sp value */
		if (get_irn_n(node, n_ia32_base) != irn)
402
			continue;
403
		/* Store has to be attached to NoMem */
404
		mem = get_irn_n(node, n_ia32_mem);
405
		if (!is_NoMem(mem))
406
407
			continue;

408
409
		/* unfortunately we can't support the full AMs possible for push at the
		 * moment. TODO: fix this */
410
		if (!is_ia32_NoReg_GP(get_irn_n(node, n_ia32_index)))
411
412
			break;

413
		offset = get_ia32_am_offs_int(node);
414
415
		/* we should NEVER access uninitialized stack BELOW the current SP */
		assert(offset >= 0);
416

417
418
		/* storing at half-slots is bad */
		if ((offset & 3) != 0)
419
420
			break;

421
		if (inc_ofs - 4 < offset || offset >= MAXPUSH_OPTIMIZE * 4)
422
423
424
425
426
			continue;
		storeslot = offset >> 2;

		/* storing into the same slot twice is bad (and shouldn't happen...) */
		if (stores[storeslot] != NULL)
427
428
429
			break;

		stores[storeslot] = node;
430
431
		if (storeslot > maxslot)
			maxslot = storeslot;
432
433
	}

434
435
436
437
438
439
	curr_sp = irn;

	for (i = -1; i < maxslot; ++i) {
		if (stores[i + 1] == NULL)
			break;
	}
440

441
442
443
444
	/* walk through the Stores and create Pushs for them */
	block  = get_nodes_block(irn);
	spmode = get_irn_mode(irn);
	irg    = cg->irg;
445
	for (; i >= 0; --i) {
446
447
		const arch_register_t *spreg;
		ir_node *push;
448
		ir_node *val, *mem, *mem_proj;
449
		ir_node *store = stores[i];
450
		ir_node *noreg = ia32_new_NoReg_gp(cg);
451

452
453
		val = get_irn_n(store, n_ia32_unary_op);
		mem = get_irn_n(store, n_ia32_mem);
454
455
		spreg = arch_get_irn_register(cg->arch_env, curr_sp);

456
		push = new_rd_ia32_Push(get_irn_dbg_info(store), irg, block, noreg, noreg, mem, val, curr_sp);
457
		copy_mark(store, push);
458

459
460
461
462
		if (first_push == NULL)
			first_push = push;

		sched_add_after(curr_sp, push);
463

464
		/* create stackpointer Proj */
465
466
		curr_sp = new_r_Proj(irg, block, push, spmode, pn_ia32_Push_stack);
		arch_set_irn_register(cg->arch_env, curr_sp, spreg);
Michael Beck's avatar
Michael Beck committed
467

468
		/* create memory Proj */
469
470
		mem_proj = new_r_Proj(irg, block, push, mode_M, pn_ia32_Push_M);

471
		/* use the memproj now */
472
		be_peephole_exchange(store, mem_proj);
Matthias Braun's avatar
bugfix    
Matthias Braun committed
473

474
		inc_ofs -= 4;
475
476
	}

477
478
479
480
481
482
483
484
485
486
	foreach_out_edge_safe(irn, edge, next) {
		ir_node *const src = get_edge_src_irn(edge);
		int      const pos = get_edge_src_pos(edge);

		if (src == first_push)
			continue;

		set_irn_n(src, pos, curr_sp);
	}

487
	be_set_IncSP_offset(irn, inc_ofs);
488
489
}

490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
#if 0
static void peephole_store_incsp(ir_node *store)
{
	dbg_info *dbgi;
	ir_node  *node;
	ir_node  *block;
	ir_node  *noref;
	ir_node  *mem;
	ir_node  *push;
	ir_node  *val;
	ir_node  *am_base = get_irn_n(store, n_ia32_Store_base);
	if (!be_is_IncSP(am_base)
			|| get_nodes_block(am_base) != get_nodes_block(store))
		return;
	mem = get_irn_n(store, n_ia32_Store_mem);
	if (!is_ia32_NoReg_GP(get_irn_n(store, n_ia32_Store_index))
			|| !is_NoMem(mem))
		return;

	int incsp_offset = be_get_IncSP_offset(am_base);
	if (incsp_offset <= 0)
		return;

	/* we have to be at offset 0 */
	int my_offset = get_ia32_am_offs_int(store);
	if (my_offset != 0) {
		/* TODO here: find out wether there is a store with offset 0 before
		 * us and wether we can move it down to our place */
		return;
	}
	ir_mode *ls_mode = get_ia32_ls_mode(store);
	int my_store_size = get_mode_size_bytes(ls_mode);

	if (my_offset + my_store_size > incsp_offset)
		return;

	/* correctness checking:
		- noone else must write to that stackslot
		    (because after translation incsp won't allocate it anymore)
	*/
	sched_foreach_reverse_from(store, node) {
		int i, arity;

		if (node == am_base)
			break;

		/* make sure noone else can use the space on the stack */
		arity = get_irn_arity(node);
		for (i = 0; i < arity; ++i) {
			ir_node *pred = get_irn_n(node, i);
			if (pred != am_base)
				continue;

			if (i == n_ia32_base &&
					(get_ia32_op_type(node) == ia32_AddrModeS
					 || get_ia32_op_type(node) == ia32_AddrModeD)) {
				int      node_offset  = get_ia32_am_offs_int(node);
				ir_mode *node_ls_mode = get_ia32_ls_mode(node);
				int      node_size    = get_mode_size_bytes(node);
				/* overlapping with our position? abort */
				if (node_offset < my_offset + my_store_size
						&& node_offset + node_size >= my_offset)
					return;
				/* otherwise it's fine */
				continue;
			}

			/* strange use of esp: abort */
			return;
		}
	}

	/* all ok, change to push */
	dbgi  = get_irn_dbg_info(store);
	block = get_nodes_block(store);
	noreg = ia32_new_NoReg_gp(cg);
	val   = get_ia32_

	push  = new_rd_ia32_Push(dbgi, irg, block, noreg, noreg, mem,

	create_push(dbgi, current_ir_graph, block, am_base, store);
}
#endif

574
575
576
577
578
579
580
581
582
583
584
/**
 * Return true if a mode can be stored in the GP register set
 */
static INLINE int mode_needs_gp_reg(ir_mode *mode) {
        if (mode == mode_fpcw)
                return 0;
        if (get_mode_size_bits(mode) > 32)
                return 0;
        return mode_is_int(mode) || mode_is_reference(mode) || mode == mode_b;
}

585
586
587
588
589
590
591
592
/**
 * Tries to create Pops from Load, IncSP combinations.
 * The Loads are replaced by Pops, the IncSP is modified
 * (possibly into IncSP 0, but not removed).
 */
static void peephole_Load_IncSP_to_pop(ir_node *irn)
{
	const arch_register_t *esp = &ia32_gp_regs[REG_ESP];
593
	int      i, maxslot, inc_ofs, ofs;
594
595
596
597
	ir_node  *node, *pred_sp, *block;
	ir_node  *loads[MAXPUSH_OPTIMIZE];
	ir_graph *irg;
	unsigned regmask = 0;
598
	unsigned copymask = ~0;
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617

	memset(loads, 0, sizeof(loads));
	assert(be_is_IncSP(irn));

	inc_ofs = -be_get_IncSP_offset(irn);
	if (inc_ofs < 4)
		return;

	/*
	 * We first walk the schedule before the IncSP node as long as we find
	 * suitable Loads that could be transformed to a Pop.
	 * We save them into the stores array which is sorted by the frame offset/4
	 * attached to the node
	 */
	maxslot = -1;
	pred_sp = be_get_IncSP_pred(irn);
	for (node = sched_prev(irn); !sched_is_end(node); node = sched_prev(node)) {
		int offset;
		int loadslot;
618
		const arch_register_t *sreg, *dreg;
619
620
621
622

		/* it has to be a Load */
		if (!is_ia32_Load(node)) {
			if (be_is_Copy(node)) {
623
				if (!mode_needs_gp_reg(get_irn_mode(node))) {
624
625
626
627
					/* not a GP copy, ignore */
					continue;
				}
				dreg = arch_get_irn_register(arch_env, node);
628
629
630
631
632
				sreg = arch_get_irn_register(arch_env, be_get_Copy_op(node));
				if (regmask & copymask & (1 << sreg->index)) {
					break;
				}
				if (regmask & copymask & (1 << dreg->index)) {
633
634
					break;
				}
635
636
637
638
				/* we CAN skip Copies if neither the destination nor the source
				 * is not in our regmask, ie none of our future Pop will overwrite it */
				regmask |= (1 << dreg->index) | (1 << sreg->index);
				copymask &= ~((1 << dreg->index) | (1 << sreg->index));
639
640
641
642
643
644
				continue;
			}
			break;
		}

		/* we can handle only GP loads */
645
		if (!mode_needs_gp_reg(get_ia32_ls_mode(node)))
646
647
648
			continue;

		/* it has to use our predecessor sp value */
649
650
651
652
653
		if (get_irn_n(node, n_ia32_base) != pred_sp) {
			/* it would be ok if this load does not use a Pop result,
			 * but we do not check this */
			break;
		}
654
655

		/* should have NO index */
656
		if (!is_ia32_NoReg_GP(get_irn_n(node, n_ia32_index)))
657
658
659
660
661
662
663
664
665
666
667
668
			break;

		offset = get_ia32_am_offs_int(node);
		/* we should NEVER access uninitialized stack BELOW the current SP */
		assert(offset >= 0);

		/* storing at half-slots is bad */
		if ((offset & 3) != 0)
			break;

		if (offset < 0 || offset >= MAXPUSH_OPTIMIZE * 4)
			continue;
669
670
671
		/* ignore those outside the possible windows */
		if (offset > inc_ofs - 4)
			continue;
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
		loadslot = offset >> 2;

		/* loading from the same slot twice is bad (and shouldn't happen...) */
		if (loads[loadslot] != NULL)
			break;

		dreg = arch_get_irn_register(arch_env, node);
		if (regmask & (1 << dreg->index)) {
			/* this register is already used */
			break;
		}
		regmask |= 1 << dreg->index;

		loads[loadslot] = node;
		if (loadslot > maxslot)
			maxslot = loadslot;
	}

	if (maxslot < 0)
		return;

693
	/* find the first slot */
694
695
696
697
698
699
	for (i = maxslot; i >= 0; --i) {
		ir_node *load = loads[i];

		if (load == NULL)
			break;
	}
700

701
702
703
	ofs = inc_ofs - (maxslot + 1) * 4;
	inc_ofs = (i+1) * 4;

704
705
706
	/* create a new IncSP if needed */
	block = get_nodes_block(irn);
	irg   = cg->irg;
707
	if (inc_ofs > 0) {
708
709
710
711
		pred_sp = be_new_IncSP(esp, irg, block, pred_sp, -inc_ofs, be_get_IncSP_align(irn));
		sched_add_before(irn, pred_sp);
	}

712
	/* walk through the Loads and create Pops for them */
713
714
715
716
717
718
719
720
721
722
723
724
	for (++i; i <= maxslot; ++i) {
		ir_node *load = loads[i];
		ir_node *mem, *pop;
		const ir_edge_t *edge, *tmp;
		const arch_register_t *reg;

		mem = get_irn_n(load, n_ia32_mem);
		reg = arch_get_irn_register(arch_env, load);

		pop = new_rd_ia32_Pop(get_irn_dbg_info(load), irg, block, mem, pred_sp);
		arch_set_irn_register(arch_env, pop, reg);

725
726
		copy_mark(load, pop);

727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
		/* create stackpointer Proj */
		pred_sp = new_r_Proj(irg, block, pop, mode_Iu, pn_ia32_Pop_stack);
		arch_set_irn_register(arch_env, pred_sp, esp);

		sched_add_before(irn, pop);

		/* rewire now */
		foreach_out_edge_safe(load, edge, tmp) {
			ir_node *proj = get_edge_src_irn(edge);

			set_Proj_pred(proj, pop);
		}

		/* we can remove the Load now */
		sched_remove(load);
		kill_node(load);
	}
744

745
	be_set_IncSP_offset(irn, -ofs);
746
747
748
749
	be_set_IncSP_pred(irn, pred_sp);
}


750
751
752
/**
 * Find a free GP register if possible, else return NULL.
 */
753
static const arch_register_t *get_free_gp_reg(void)
754
{
755
756
757
758
759
760
761
762
763
	int i;

	for(i = 0; i < N_ia32_gp_REGS; ++i) {
		const arch_register_t *reg = &ia32_gp_regs[i];
		if(arch_register_type_is(reg, ignore))
			continue;

		if(be_peephole_get_value(CLASS_ia32_gp, i) == NULL)
			return &ia32_gp_regs[i];
764
	}
765
766
767
768

	return NULL;
}

769
770
771
772
773
774
775
776
777
778
779
780
/**
 * Creates a Pop instruction before the given schedule point.
 *
 * @param dbgi        debug info
 * @param irg         the graph
 * @param block       the block
 * @param stack       the previous stack value
 * @param schedpoint  the new node is added before this node
 * @param reg         the register to pop
 *
 * @return the new stack value
 */
781
782
static ir_node *create_pop(dbg_info *dbgi, ir_graph *irg, ir_node *block,
                           ir_node *stack, ir_node *schedpoint,
783
                           const arch_register_t *reg)
784
785
786
787
788
789
790
791
792
793
794
795
{
	const arch_register_t *esp = &ia32_gp_regs[REG_ESP];
	ir_node *pop;
	ir_node *keep;
	ir_node *val;
	ir_node *in[1];

	pop   = new_rd_ia32_Pop(dbgi, irg, block, new_NoMem(), stack);

	stack = new_r_Proj(irg, block, pop, mode_Iu, pn_ia32_Pop_stack);
	arch_set_irn_register(arch_env, stack, esp);
	val   = new_r_Proj(irg, block, pop, mode_Iu, pn_ia32_Pop_res);
796
	arch_set_irn_register(arch_env, val, reg);
797
798
799
800
801
802
803
804
805
806

	sched_add_before(schedpoint, pop);

	in[0] = val;
	keep = be_new_Keep(&ia32_reg_classes[CLASS_ia32_gp], irg, block, 1, in);
	sched_add_before(schedpoint, keep);

	return stack;
}

807
808
809
810
811
812
813
814
815
816
817
818
819
/**
 * Creates a Push instruction before the given schedule point.
 *
 * @param dbgi        debug info
 * @param irg         the graph
 * @param block       the block
 * @param stack       the previous stack value
 * @param schedpoint  the new node is added before this node
 * @param reg         the register to pop
 *
 * @return the new stack value
 */
static ir_node *create_push(dbg_info *dbgi, ir_graph *irg, ir_node *block,
820
                            ir_node *stack, ir_node *schedpoint)
821
822
823
{
	const arch_register_t *esp = &ia32_gp_regs[REG_ESP];

824
825
826
827
	ir_node *val   = ia32_new_Unknown_gp(cg);
	ir_node *noreg = ia32_new_NoReg_gp(cg);
	ir_node *nomem = get_irg_no_mem(irg);
	ir_node *push  = new_rd_ia32_Push(dbgi, irg, block, noreg, noreg, nomem, val, stack);
828
829
830
831
832
833
834
835
836
	sched_add_before(schedpoint, push);

	stack = new_r_Proj(irg, block, push, mode_Iu, pn_ia32_Push_stack);
	arch_set_irn_register(arch_env, stack, esp);

	return stack;
}

/**
Michael Beck's avatar
Michael Beck committed
837
 * Optimize an IncSp by replacing it with Push/Pop.
838
 */
839
840
841
842
static void peephole_be_IncSP(ir_node *node)
{
	const arch_register_t *esp = &ia32_gp_regs[REG_ESP];
	const arch_register_t *reg;
843
	ir_graph              *irg = current_ir_graph;
844
845
846
847
848
849
	dbg_info              *dbgi;
	ir_node               *block;
	ir_node               *stack;
	int                    offset;

	/* first optimize incsp->incsp combinations */
850
	node = be_peephole_IncSP_IncSP(node);
851

852
853
854
	/* transform IncSP->Store combinations to Push where possible */
	peephole_IncSP_Store_to_push(node);

855
856
857
	/* transform Load->IncSP combinations to Pop where possible */
	peephole_Load_IncSP_to_pop(node);

858
859
860
	if (arch_get_irn_register(arch_env, node) != esp)
		return;

861
862
	/* replace IncSP -4 by Pop freereg when possible */
	offset = be_get_IncSP_offset(node);
863
864
865
866
	if ((offset != -8 || ia32_cg_config.use_add_esp_8) &&
	    (offset != -4 || ia32_cg_config.use_add_esp_4) &&
	    (offset != +4 || ia32_cg_config.use_sub_esp_4) &&
	    (offset != +8 || ia32_cg_config.use_sub_esp_8))
867
868
		return;

869
870
871
	if (offset < 0) {
		/* we need a free register for pop */
		reg = get_free_gp_reg();
872
		if (reg == NULL)
873
			return;
874

875
876
877
878
		dbgi  = get_irn_dbg_info(node);
		block = get_nodes_block(node);
		stack = be_get_IncSP_pred(node);

879
		stack = create_pop(dbgi, irg, block, stack, node, reg);
880

881
		if (offset == -8) {
882
			stack = create_pop(dbgi, irg, block, stack, node, reg);
883
		}
884
	} else {
885
886
887
		dbgi  = get_irn_dbg_info(node);
		block = get_nodes_block(node);
		stack = be_get_IncSP_pred(node);
888
		stack = create_push(dbgi, irg, block, stack, node);
889
890

		if (offset == +8) {
891
			stack = create_push(dbgi, irg, block, stack, node);
892
		}
893
894
	}

Christoph Mallon's avatar
Christoph Mallon committed
895
	be_peephole_exchange(node, stack);
896
897
}

Michael Beck's avatar
Michael Beck committed
898
899
900
/**
 * Peephole optimisation for ia32_Const's
 */
901
static void peephole_ia32_Const(ir_node *node)
902
{
903
904
905
906
907
908
909
910
911
912
	const ia32_immediate_attr_t *attr = get_ia32_immediate_attr_const(node);
	const arch_register_t       *reg;
	ir_graph                    *irg = current_ir_graph;
	ir_node                     *block;
	dbg_info                    *dbgi;
	ir_node                     *produceval;
	ir_node                     *xor;
	ir_node                     *noreg;

	/* try to transform a mov 0, reg to xor reg reg */
913
914
915
	if (attr->offset != 0 || attr->symconst != NULL)
		return;
	if (ia32_cg_config.use_mov_0)
916
		return;
Michael Beck's avatar
Michael Beck committed
917
	/* xor destroys the flags, so no-one must be using them */
918
	if (be_peephole_get_value(CLASS_ia32_flags, REG_EFLAGS) != NULL)
919
		return;
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936

	reg = arch_get_irn_register(arch_env, node);
	assert(be_peephole_get_reg_value(reg) == NULL);

	/* create xor(produceval, produceval) */
	block      = get_nodes_block(node);
	dbgi       = get_irn_dbg_info(node);
	produceval = new_rd_ia32_ProduceVal(dbgi, irg, block);
	arch_set_irn_register(arch_env, produceval, reg);

	noreg = ia32_new_NoReg_gp(cg);
	xor   = new_rd_ia32_Xor(dbgi, irg, block, noreg, noreg, new_NoMem(),
	                        produceval, produceval);
	arch_set_irn_register(arch_env, xor, reg);

	sched_add_before(node, produceval);
	sched_add_before(node, xor);
937

938
	copy_mark(node, xor);
Christoph Mallon's avatar
Christoph Mallon committed
939
	be_peephole_exchange(node, xor);
940
}
941

942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
static INLINE int is_noreg(ia32_code_gen_t *cg, const ir_node *node)
{
	return node == cg->noreg_gp;
}

static ir_node *create_immediate_from_int(ia32_code_gen_t *cg, int val)
{
	ir_graph *irg         = current_ir_graph;
	ir_node  *start_block = get_irg_start_block(irg);
	ir_node  *immediate   = new_rd_ia32_Immediate(NULL, irg, start_block, NULL,
	                                              0, val);
	arch_set_irn_register(cg->arch_env, immediate, &ia32_gp_regs[REG_GP_NOREG]);

	return immediate;
}

static ir_node *create_immediate_from_am(ia32_code_gen_t *cg,
                                         const ir_node *node)
{
	ir_graph  *irg     = get_irn_irg(node);
	ir_node   *block   = get_nodes_block(node);
	int        offset  = get_ia32_am_offs_int(node);
	int        sc_sign = is_ia32_am_sc_sign(node);
	ir_entity *entity  = get_ia32_am_sc(node);
	ir_node   *res;

	res = new_rd_ia32_Immediate(NULL, irg, block, entity, sc_sign, offset);
	arch_set_irn_register(cg->arch_env, res, &ia32_gp_regs[REG_GP_NOREG]);
	return res;
}

static int is_am_one(const ir_node *node)
{
	int        offset  = get_ia32_am_offs_int(node);
	ir_entity *entity  = get_ia32_am_sc(node);

	return offset == 1 && entity == NULL;
}

static int is_am_minus_one(const ir_node *node)
{
	int        offset  = get_ia32_am_offs_int(node);
	ir_entity *entity  = get_ia32_am_sc(node);

	return offset == -1 && entity == NULL;
}

/**
 * Transforms a LEA into an Add or SHL if possible.
 */
static void peephole_ia32_Lea(ir_node *node)
{
	const arch_env_t      *arch_env = cg->arch_env;
	ir_graph              *irg      = current_ir_graph;
	ir_node               *base;
	ir_node               *index;
	const arch_register_t *base_reg;
	const arch_register_t *index_reg;
	const arch_register_t *out_reg;