ia32_optimize.c 50.6 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
/**
Christian Würdig's avatar
Christian Würdig committed
2
3
4
5
6
7
8
9
10
 * Project:     libFIRM
 * File name:   ir/be/ia32/ia32_optimize.c
 * Purpose:     Implements several optimizations for IA32
 * Author:      Christian Wuerdig
 * CVS-ID:      $Id$
 * Copyright:   (c) 2006 Universitt Karlsruhe
 * Licence:     This file protected by GPL -  GNU GENERAL PUBLIC LICENSE.
 */

11
12
13
14
15
16
17
18
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#include "irnode.h"
#include "irprog_t.h"
#include "ircons.h"
#include "firm_types.h"
Christian Würdig's avatar
Christian Würdig committed
19
20
21
#include "iredges.h"
#include "tv.h"
#include "irgmod.h"
22
#include "irgwalk.h"
Christian Würdig's avatar
Christian Würdig committed
23
#include "height.h"
24
#include "irbitset.h"
Christian Würdig's avatar
Christian Würdig committed
25

Christian Würdig's avatar
Christian Würdig committed
26
27
#include "../be_t.h"
#include "../beabi.h"
Christian Würdig's avatar
Christian Würdig committed
28
29
#include "../benode_t.h"
#include "../besched_t.h"
30
31
32

#include "ia32_new_nodes.h"
#include "bearch_ia32_t.h"
Christian Würdig's avatar
Christian Würdig committed
33
#include "gen_ia32_regalloc_if.h"     /* the generated interface (register type and class defenitions) */
Christian Würdig's avatar
Christian Würdig committed
34
#include "ia32_transform.h"
Christian Würdig's avatar
Christian Würdig committed
35
#include "ia32_dbg_stat.h"
Christian Würdig's avatar
Christian Würdig committed
36
#include "ia32_util.h"
37

38
39
40
41
42
typedef struct _ia32_place_env_t {
	ia32_code_gen_t *cg;
	bitset_t        *visited;
} ia32_place_env_t;

Christian Würdig's avatar
Christian Würdig committed
43
44
45
46
47
48
49
typedef enum {
	IA32_AM_CAND_NONE  = 0,
	IA32_AM_CAND_LEFT  = 1,
	IA32_AM_CAND_RIGHT = 2,
	IA32_AM_CAND_BOTH  = 3
} ia32_am_cand_t;

Christian Würdig's avatar
Christian Würdig committed
50
51
52
#undef is_NoMem
#define is_NoMem(irn) (get_irn_op(irn) == op_NoMem)

53
typedef int is_op_func_t(const ir_node *n);
54
typedef ir_node *load_func_t(dbg_info *db, ir_graph *irg, ir_node *block, ir_node *base, ir_node *index, ir_node *mem);
55

Michael Beck's avatar
Michael Beck committed
56
57
58
59
60
61
62
/**
 * checks if a node represents the NOREG value
 */
static int be_is_NoReg(ia32_code_gen_t *cg, const ir_node *irn) {
  be_abi_irg_t *babi = cg->birg->abi;
	const arch_register_t *fp_noreg = USE_SSE2(cg) ?
		&ia32_xmm_regs[REG_XMM_NOREG] : &ia32_vfp_regs[REG_VFP_NOREG];
Christian Würdig's avatar
Christian Würdig committed
63

Michael Beck's avatar
Michael Beck committed
64
65
	return (be_abi_get_callee_save_irn(babi, &ia32_gp_regs[REG_GP_NOREG]) == irn) ||
	       (be_abi_get_callee_save_irn(babi, fp_noreg) == irn);
Christian Würdig's avatar
Christian Würdig committed
66
67
}

68
69
70
71
72
73
74
75
76
77
78
79


/*************************************************
 *   _____                _              _
 *  / ____|              | |            | |
 * | |     ___  _ __  ___| |_ __ _ _ __ | |_ ___
 * | |    / _ \| '_ \/ __| __/ _` | '_ \| __/ __|
 * | |___| (_) | | | \__ \ || (_| | | | | |_\__ \
 *  \_____\___/|_| |_|___/\__\__,_|_| |_|\__|___/
 *
 *************************************************/

80
81
82
83
84
85
86
87
/**
 * creates a unique ident by adding a number to a tag
 *
 * @param tag   the tag string, must contain a %d if a number
 *              should be added
 */
static ident *unique_id(const char *tag)
{
88
89
	static unsigned id = 0;
	char str[256];
90

91
92
	snprintf(str, sizeof(str), tag, ++id);
	return new_id_from_str(str);
93
94
95
96
97
98
99
100
101
102
103
104
}

/**
 * Transforms a SymConst.
 *
 * @param mod     the debug module
 * @param block   the block the new node should belong to
 * @param node    the ir SymConst node
 * @param mode    mode of the SymConst
 * @return the created ia32 Const node
 */
static ir_node *gen_SymConst(ia32_transform_env_t *env) {
Christian Würdig's avatar
Christian Würdig committed
105
106
107
108
	dbg_info *dbg   = env->dbg;
	ir_mode  *mode  = env->mode;
	ir_graph *irg   = env->irg;
	ir_node  *block = env->block;
109
	ir_node  *cnst;
110

Christian Würdig's avatar
Christian Würdig committed
111
	if (mode_is_float(mode)) {
Michael Beck's avatar
Michael Beck committed
112
		FP_USED(env->cg);
Michael Beck's avatar
Michael Beck committed
113
		if (USE_SSE2(env->cg))
114
			cnst = new_rd_ia32_xConst(dbg, irg, block, get_irg_no_mem(irg), mode);
Michael Beck's avatar
Michael Beck committed
115
		else
116
			cnst = new_rd_ia32_vfConst(dbg, irg, block, get_irg_no_mem(irg), mode);
Christian Würdig's avatar
Christian Würdig committed
117
	}
118
119
	else
		cnst = new_rd_ia32_Const(dbg, irg, block, get_irg_no_mem(irg), mode);
Christian Würdig's avatar
Christian Würdig committed
120

121
	set_ia32_Const_attr(cnst, env->irn);
Christian Würdig's avatar
Christian Würdig committed
122

123
124
	return cnst;
}
125

126
127
128
129
130
131
132
133
134
135
136
137
138
/**
 * Get a primitive type for a mode.
 */
static ir_type *get_prim_type(pmap *types, ir_mode *mode)
{
	pmap_entry *e = pmap_find(types, mode);
	ir_type *res;

	if (! e) {
		char buf[64];
		snprintf(buf, sizeof(buf), "prim_type_%s", get_mode_name(mode));
		res = new_type_primitive(new_id_from_str(buf), mode);
		pmap_insert(types, mode, res);
139
	}
140
141
142
143
	else
		res = e->value;
	return res;
}
144

145
146
147
148
149
150
/**
 * Get an entity that is initialized with a tarval
 */
static entity *get_entity_for_tv(ia32_code_gen_t *cg, ir_node *cnst)
{
	tarval *tv    = get_Const_tarval(cnst);
151
	pmap_entry *e = pmap_find(cg->isa->tv_ent, tv);
152
153
154
155
156
157
158
	entity *res;
	ir_graph *rem;

	if (! e) {
		ir_mode *mode = get_irn_mode(cnst);
		ir_type *tp = get_Const_type(cnst);
		if (tp == firm_unknown_type)
159
			tp = get_prim_type(cg->isa->types, mode);
160

161
		res = new_entity(get_glob_type(), unique_id(".LC%u"), tp);
162
163
164
165
166
167
168
169
170
171
172
173

		set_entity_ld_ident(res, get_entity_ident(res));
		set_entity_visibility(res, visibility_local);
		set_entity_variability(res, variability_constant);
		set_entity_allocation(res, allocation_static);

		 /* we create a new entity here: It's initialization must resist on the
		    const code irg */
		rem = current_ir_graph;
		current_ir_graph = get_const_code_irg();
		set_atomic_ent_value(res, new_Const_type(tv, tp));
		current_ir_graph = rem;
174
175

		pmap_insert(cg->isa->tv_ent, tv, res);
176
177
178
179
	}
	else
		res = e->value;
	return res;
180
181
182
183
184
185
186
187
188
189
190
191
}

/**
 * Transforms a Const.
 *
 * @param mod     the debug module
 * @param block   the block the new node should belong to
 * @param node    the ir Const node
 * @param mode    mode of the Const
 * @return the created ia32 Const node
 */
static ir_node *gen_Const(ia32_transform_env_t *env) {
192
	ir_node *cnst, *load;
193
	symconst_symbol sym;
Christian Würdig's avatar
Christian Würdig committed
194
195
196
	ir_graph *irg   = env->irg;
	ir_node  *block = env->block;
	ir_node  *node  = env->irn;
197
198
	dbg_info *dbg   = env->dbg;
	ir_mode  *mode  = env->mode;
199
200

	if (mode_is_float(mode)) {
Michael Beck's avatar
Michael Beck committed
201
		FP_USED(env->cg);
Michael Beck's avatar
Michael Beck committed
202
203
204
205
206
207
208
209
		if (! USE_SSE2(env->cg)) {
			cnst_classify_t clss = classify_Const(node);

			if (clss == CNST_NULL)
				return new_rd_ia32_vfldz(dbg, irg, block, mode);
			else if (clss == CNST_ONE)
				return new_rd_ia32_vfld1(dbg, irg, block, mode);
		}
210
		sym.entity_p = get_entity_for_tv(env->cg, node);
211

212
213
214
215
216
217
218
219
220

		cnst      = new_rd_SymConst(dbg, irg, block, sym, symconst_addr_ent);
		load      = new_r_Load(irg, block, get_irg_no_mem(irg), cnst, mode);
		load      = new_r_Proj(irg, block, load, mode, pn_Load_res);
		env->irn  = cnst;
		env->mode = mode_P;
		cnst      = gen_SymConst(env);
		set_Load_ptr(get_Proj_pred(load), cnst);
		cnst      = load;
221
222
	}
	else {
223
		cnst = new_rd_ia32_Const(dbg, irg, block, get_irg_no_mem(irg), get_irn_mode(node));
224
225
226
227
228
229
230
		set_ia32_Const_attr(cnst, node);
	}
	return cnst;
}

/**
 * Transforms (all) Const's into ia32_Const and places them in the
Christian Würdig's avatar
Christian Würdig committed
231
 * block where they are used (or in the cfg-pred Block in case of Phi's).
232
 * Additionally all reference nodes are changed into mode_Is nodes.
233
 * NOTE: irn must be a firm constant!
234
 */
235
236
237
238
239
240
241
242
243
244
245
static void ia32_transform_const(ir_node *irn, void *env) {
	ia32_code_gen_t      *cg   = env;
	ir_node              *cnst = NULL;
	ia32_transform_env_t tenv;

	tenv.cg   = cg;
	tenv.irg  = cg->irg;
	tenv.mode = get_irn_mode(irn);
	tenv.dbg  = get_irn_dbg_info(irn);
	tenv.irn  = irn;
	DEBUG_ONLY(tenv.mod = cg->mod;)
246

247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
	/* place const either in the smallest dominator of all its users or the original block */
	if (cg->opt & IA32_OPT_PLACECNST)
		tenv.block = node_users_smallest_common_dominator(irn, 1);
	else
		tenv.block = get_nodes_block(irn);

	switch (get_irn_opcode(irn)) {
		case iro_Const:
			cnst = gen_Const(&tenv);
			break;
		case iro_SymConst:
			cnst = gen_SymConst(&tenv);
			break;
		default:
			assert(0 && "Wrong usage of ia32_transform_const!");
	}

	assert(cnst && "Could not create ia32 Const");

	/* set the new ia32 const */
	exchange(irn, cnst);
}

/**
 * Transform all firm consts and assure, we visit each const only once.
 */
static void ia32_place_consts_walker(ir_node *irn, void *env) {
	ia32_place_env_t *penv = env;
	opcode           opc   = get_irn_opcode(irn);

	/* transform only firm consts which are not already visited */
	if ((opc != iro_Const && opc != iro_SymConst) || bitset_is_set(penv->visited, get_irn_idx(irn)))
279
280
		return;

281
282
	/* mark const visited */
	bitset_set(penv->visited, get_irn_idx(irn));
283

284
285
286
287
288
289
290
291
292
293
294
295
296
	ia32_transform_const(irn, penv->cg);
}

/**
 * Replace reference modes with mode_Iu and preserve store value modes.
 */
static void ia32_set_modes(ir_node *irn, void *env) {
	if (is_Block(irn))
		return;

	/* transform all reference nodes into mode_Iu nodes */
	if (mode_is_reference(get_irn_mode(irn))) {
		set_irn_mode(irn, mode_Iu);
Christian Würdig's avatar
Christian Würdig committed
297
	}
298
}
299

300
301
302
303
304
305
306
/**
 * Walks over the graph, transforms all firm consts into ia32 consts
 * and places them into the "best" block.
 * @param cg  The ia32 codegenerator object
 */
static void ia32_transform_all_firm_consts(ia32_code_gen_t *cg) {
	ia32_place_env_t penv;
307

308
309
310
311
	penv.cg      = cg;
	penv.visited = bitset_irg_malloc(cg->irg);
	irg_walk_graph(cg->irg, NULL, ia32_place_consts_walker, &penv);
	bitset_free(penv.visited);
312
}
Christian Würdig's avatar
Christian Würdig committed
313

314
315
316
317
318
319
320
321
322
323
324
/* Place all consts and change pointer arithmetics into unsigned integer arithmetics. */
void ia32_pre_transform_phase(ia32_code_gen_t *cg) {
	/*
		We need to transform the consts twice:
		- the psi condition tree transformer needs existing constants to be ia32 constants
		- the psi condition tree transformer inserts new firm constants which need to be transformed
	*/
	ia32_transform_all_firm_consts(cg);
	irg_walk_graph(cg->irg, ia32_set_modes, ia32_transform_psi_cond_tree, cg);
	ia32_transform_all_firm_consts(cg);
}
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341

/********************************************************************************************************
 *  _____                _           _         ____        _   _           _          _   _
 * |  __ \              | |         | |       / __ \      | | (_)         (_)        | | (_)
 * | |__) |__  ___ _ __ | |__   ___ | | ___  | |  | |_ __ | |_ _ _ __ ___  _ ______ _| |_ _  ___  _ __
 * |  ___/ _ \/ _ \ '_ \| '_ \ / _ \| |/ _ \ | |  | | '_ \| __| | '_ ` _ \| |_  / _` | __| |/ _ \| '_ \
 * | |  |  __/  __/ |_) | | | | (_) | |  __/ | |__| | |_) | |_| | | | | | | |/ / (_| | |_| | (_) | | | |
 * |_|   \___|\___| .__/|_| |_|\___/|_|\___|  \____/| .__/ \__|_|_| |_| |_|_/___\__,_|\__|_|\___/|_| |_|
 *                | |                               | |
 *                |_|                               |_|
 ********************************************************************************************************/

/**
 * NOTE: THESE PEEPHOLE OPTIMIZATIONS MUST BE CALLED AFTER SCHEDULING AND REGISTER ALLOCATION.
 */

static int ia32_cnst_compare(ir_node *n1, ir_node *n2) {
342
	return get_ia32_id_cnst(n1) == get_ia32_id_cnst(n2);
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
}

/**
 * Checks for potential CJmp/CJmpAM optimization candidates.
 */
static ir_node *ia32_determine_cjmp_cand(ir_node *irn, is_op_func_t *is_op_func) {
	ir_node *cand = NULL;
	ir_node *prev = sched_prev(irn);

	if (is_Block(prev)) {
		if (get_Block_n_cfgpreds(prev) == 1)
			prev = get_Block_cfgpred(prev, 0);
		else
			prev = NULL;
	}

	/* The predecessor must be a ProjX. */
	if (prev && is_Proj(prev) && get_irn_mode(prev) == mode_X) {
		prev = get_Proj_pred(prev);

		if (is_op_func(prev))
			cand = prev;
	}

	return cand;
}

static int is_TestJmp_cand(const ir_node *irn) {
	return is_ia32_TestJmp(irn) || is_ia32_And(irn);
}

/**
 * Checks if two consecutive arguments of cand matches
 * the two arguments of irn (TestJmp).
 */
static int is_TestJmp_replacement(ir_node *cand, ir_node *irn) {
	ir_node *in1       = get_irn_n(irn, 0);
	ir_node *in2       = get_irn_n(irn, 1);
	int      i, n      = get_irn_arity(cand);
	int      same_args = 0;

	for (i = 0; i < n - 1; i++) {
		if (get_irn_n(cand, i)     == in1 &&
			get_irn_n(cand, i + 1) == in2)
		{
			same_args = 1;
			break;
		}
	}

	if (same_args)
		return ia32_cnst_compare(cand, irn);

	return 0;
}

/**
 * Tries to replace a TestJmp by a CJmp or CJmpAM (in case of And)
 */
static void ia32_optimize_TestJmp(ir_node *irn, ia32_code_gen_t *cg) {
	ir_node *cand    = ia32_determine_cjmp_cand(irn, is_TestJmp_cand);
	int      replace = 0;

	/* we found a possible candidate */
	replace = cand ? is_TestJmp_replacement(cand, irn) : 0;

	if (replace) {
		DBG((cg->mod, LEVEL_1, "replacing %+F by ", irn));

		if (is_ia32_And(cand))
			set_irn_op(irn, op_ia32_CJmpAM);
		else
			set_irn_op(irn, op_ia32_CJmp);

		DB((cg->mod, LEVEL_1, "%+F\n", irn));
	}
}

static int is_CondJmp_cand(const ir_node *irn) {
	return is_ia32_CondJmp(irn) || is_ia32_Sub(irn);
}

/**
 * Checks if the arguments of cand are the same of irn.
 */
static int is_CondJmp_replacement(ir_node *cand, ir_node *irn) {
429
	int i, n      = get_irn_arity(cand);
Christian Würdig's avatar
Christian Würdig committed
430
	int same_args = 1;
431
432

	for (i = 0; i < n; i++) {
Michael Beck's avatar
Michael Beck committed
433
		if (get_irn_n(cand, i) != get_irn_n(irn, i)) {
Christian Würdig's avatar
Christian Würdig committed
434
			same_args = 0;
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
			break;
		}
	}

	if (same_args)
		return ia32_cnst_compare(cand, irn);

	return 0;
}

/**
 * Tries to replace a CondJmp by a CJmpAM
 */
static void ia32_optimize_CondJmp(ir_node *irn, ia32_code_gen_t *cg) {
	ir_node *cand    = ia32_determine_cjmp_cand(irn, is_CondJmp_cand);
	int      replace = 0;

	/* we found a possible candidate */
	replace = cand ? is_CondJmp_replacement(cand, irn) : 0;

	if (replace) {
		DBG((cg->mod, LEVEL_1, "replacing %+F by ", irn));
Christian Würdig's avatar
Christian Würdig committed
457
		DBG_OPT_CJMP(irn);
458

Michael Beck's avatar
Michael Beck committed
459
		set_irn_op(irn, op_ia32_CJmpAM);
460
461
462
463
464

		DB((cg->mod, LEVEL_1, "%+F\n", irn));
	}
}

465
466
467
468
/**
 * Creates a Push from Store(IncSP(gp_reg_size))
 */
static void ia32_create_Push(ir_node *irn, ia32_code_gen_t *cg) {
469
470
471
	ir_node  *sp  = get_irn_n(irn, 0);
	ir_graph *irg = cg->irg;
	ir_node *val, *next, *push, *bl, *proj_M, *proj_res, *old_proj_M, *mem;
472
	const ir_edge_t *edge;
Christian Würdig's avatar
Christian Würdig committed
473
	heights_t *h;
474

Christian Würdig's avatar
Christian Würdig committed
475
476
	/* do not create push if store has already an offset assigned or base is not a IncSP */
	if (get_ia32_am_offs(irn) || ! be_is_IncSP(sp))
477
478
		return;

Christian Würdig's avatar
Christian Würdig committed
479
	/* do not create push if index is not NOREG */
480
	if (arch_get_irn_register(cg->arch_env, get_irn_n(irn, 1)) !=
Christian Würdig's avatar
Christian Würdig committed
481
		&ia32_gp_regs[REG_GP_NOREG])
482
483
		return;

Christian Würdig's avatar
Christian Würdig committed
484
	/* do not create push for floating point */
485
486
487
488
	val = get_irn_n(irn, 2);
	if (mode_is_float(get_irn_mode(val)))
		return;

Christian Würdig's avatar
Christian Würdig committed
489
	/* do not create push if IncSp doesn't expand stack or expand size is different from register size */
490
	if (be_get_IncSP_direction(sp) != be_stack_dir_expand ||
Matthias Braun's avatar
Matthias Braun committed
491
		be_get_IncSP_offset(sp) != (unsigned) get_mode_size_bytes(ia32_reg_classes[CLASS_ia32_gp].mode))
492
493
		return;

Christian Würdig's avatar
Christian Würdig committed
494
495
496
497
498
499
500
501
502
503
	/* do not create push, if there is a path (inside the block) from the push value to IncSP */
	h = heights_new(cg->irg);
	if (get_nodes_block(val) == get_nodes_block(sp) &&
		heights_reachable_in_block(h, val, sp))
	{
		heights_free(h);
		return;
	}
	heights_free(h);

504
	/* ok, translate into Push */
Christian Würdig's avatar
Christian Würdig committed
505
	edge       = get_irn_out_edge_first(irn);
506
	old_proj_M = get_edge_src_irn(edge);
507
	bl         = get_nodes_block(irn);
508
509
510
511
512

	next = sched_next(irn);
	sched_remove(irn);
	sched_remove(sp);

513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
	/*
		build memory input:
		if the IncSP points to NoMem -> just use the memory input from store
		if IncSP points to somewhere else -> sync memory of IncSP and Store
	*/
	mem = be_get_IncSP_mem(sp);
	if (mem == get_irg_no_mem(irg))
		mem = get_irn_n(irn, 3);
	else {
		ir_node *in[2];

		in[0] = mem;
		in[1] = get_irn_n(irn, 3);
		mem   = new_r_Sync(irg, bl, 2, in);
	}

	push = new_rd_ia32_Push(NULL, irg, bl, be_get_IncSP_pred(sp), val, mem);
	proj_res = new_r_Proj(irg, bl, push, get_irn_mode(sp), pn_ia32_Push_stack);
	proj_M   = new_r_Proj(irg, bl, push, mode_M, pn_ia32_Push_M);
532

533
534
535
536
	/* copy a possible constant from the store */
	set_ia32_id_cnst(push, get_ia32_id_cnst(irn));
	set_ia32_immop_type(push, get_ia32_immop_type(irn));

537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
	/* the push must have SP out register */
	arch_set_irn_register(cg->arch_env, push, arch_get_irn_register(cg->arch_env, sp));

	exchange(old_proj_M, proj_M);
	exchange(sp, proj_res);
	sched_add_before(next, push);
	sched_add_after(push, proj_res);
}

/**
 * Creates a Pop from IncSP(Load(sp))
 */
static void ia32_create_Pop(ir_node *irn, ia32_code_gen_t *cg) {
	ir_node *old_proj_M = be_get_IncSP_mem(irn);
	ir_node *load = skip_Proj(old_proj_M);
	ir_node *old_proj_res = NULL;
	ir_node *bl, *pop, *next, *proj_res, *proj_sp, *proj_M;
	const ir_edge_t *edge;
	const arch_register_t *reg, *sp;

	if (! is_ia32_Load(load) || get_ia32_am_offs(load))
		return;

	if (arch_get_irn_register(cg->arch_env, get_irn_n(load, 1)) !=
Christian Würdig's avatar
Christian Würdig committed
561
		&ia32_gp_regs[REG_GP_NOREG])
562
		return;
Michael Beck's avatar
Michael Beck committed
563
	if (arch_get_irn_register(cg->arch_env, get_irn_n(load, 0)) != cg->isa->arch_isa.sp)
564
565
566
567
568
569
570
571
572
573
574
575
		return;

	/* ok, translate into pop */
	foreach_out_edge(load, edge) {
		ir_node *succ = get_edge_src_irn(edge);
		if (succ != old_proj_M) {
			old_proj_res = succ;
			break;
		}
	}
	if (! old_proj_res) {
		assert(0);
Christian Würdig's avatar
Christian Würdig committed
576
		return; /* should not happen */
577
578
579
580
581
582
583
584
585
586
587
588
589
	}

	bl = get_nodes_block(load);

	/* IncSP is typically scheduled after the load, so remove it first */
	sched_remove(irn);
	next = sched_next(old_proj_res);
	sched_remove(old_proj_res);
	sched_remove(load);

	reg = arch_get_irn_register(cg->arch_env, load);
	sp  = arch_get_irn_register(cg->arch_env, irn);

Michael Beck's avatar
Michael Beck committed
590
591
592
593
	pop      = new_rd_ia32_Pop(NULL, current_ir_graph, bl, get_irn_n(irn, 0), get_irn_n(load, 2));
	proj_res = new_r_Proj(current_ir_graph, bl, pop, get_irn_mode(old_proj_res), pn_ia32_Pop_res);
	proj_sp  = new_r_Proj(current_ir_graph, bl, pop, get_irn_mode(irn), pn_ia32_Pop_stack);
	proj_M   = new_r_Proj(current_ir_graph, bl, pop, mode_M, pn_ia32_Pop_M);
594
595
596
597
598
599
600
601
602
603
604
605
606

	exchange(old_proj_M, proj_M);
	exchange(old_proj_res, proj_res);
	exchange(irn, proj_sp);

	arch_set_irn_register(cg->arch_env, proj_res, reg);
	arch_set_irn_register(cg->arch_env, proj_sp, sp);

	sched_add_before(next, proj_sp);
	sched_add_before(proj_sp, proj_res);
	sched_add_before(proj_res,pop);
}

607
/**
608
 * Tries to optimize two following IncSP.
609
 */
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
static void ia32_optimize_IncSP(ir_node *irn, ia32_code_gen_t *cg) {
	ir_node *prev = be_get_IncSP_pred(irn);
	int real_uses = get_irn_n_edges(prev);

	if (be_is_IncSP(prev) && real_uses == 1) {
		/* first IncSP has only one IncSP user, kill the first one */
		unsigned       prev_offs = be_get_IncSP_offset(prev);
		be_stack_dir_t prev_dir  = be_get_IncSP_direction(prev);
		unsigned       curr_offs = be_get_IncSP_offset(irn);
		be_stack_dir_t curr_dir  = be_get_IncSP_direction(irn);

		int new_ofs = prev_offs * (prev_dir == be_stack_dir_expand ? -1 : +1) +
			            curr_offs * (curr_dir == be_stack_dir_expand ? -1 : +1);

		if (new_ofs < 0) {
			new_ofs  = -new_ofs;
			curr_dir = be_stack_dir_expand;
627
		}
628
629
630
631
632
		else
			curr_dir = be_stack_dir_shrink;
		be_set_IncSP_offset(prev, 0);
		be_set_IncSP_offset(irn, (unsigned)new_ofs);
		be_set_IncSP_direction(irn, curr_dir);
Christian Würdig's avatar
Christian Würdig committed
633
634
635

		/* Omit the optimized IncSP */
		be_set_IncSP_pred(irn, be_get_IncSP_pred(prev));
636
	}
637
638
639
640
641
642
643
644
}

/**
 * Performs Peephole Optimizations.
 */
void ia32_peephole_optimization(ir_node *irn, void *env) {
	ia32_code_gen_t *cg = env;

645
646
647
648
649
650
651
	/* AMD CPUs want explicit compare before conditional jump  */
	if (! ARCH_AMD(cg->opt_arch)) {
		if (is_ia32_TestJmp(irn))
			ia32_optimize_TestJmp(irn, cg);
		else if (is_ia32_CondJmp(irn))
			ia32_optimize_CondJmp(irn, cg);
	}
652
	/* seems to be buggy when using Pushes */
653
654
	else if (be_is_IncSP(irn))
		ia32_optimize_IncSP(irn, cg);
655
656
	else if (is_ia32_Store(irn))
		ia32_create_Push(irn, cg);
657
658
659
660
}



Christian Würdig's avatar
Christian Würdig committed
661
662
663
664
665
666
667
668
669
670
/******************************************************************
 *              _     _                   __  __           _
 *     /\      | |   | |                 |  \/  |         | |
 *    /  \   __| | __| |_ __ ___  ___ ___| \  / | ___   __| | ___
 *   / /\ \ / _` |/ _` | '__/ _ \/ __/ __| |\/| |/ _ \ / _` |/ _ \
 *  / ____ \ (_| | (_| | | |  __/\__ \__ \ |  | | (_) | (_| |  __/
 * /_/    \_\__,_|\__,_|_|  \___||___/___/_|  |_|\___/ \__,_|\___|
 *
 ******************************************************************/

Christian Würdig's avatar
Christian Würdig committed
671
672
673
674
675
typedef struct {
	ia32_code_gen_t *cg;
	heights_t       *h;
} ia32_am_opt_env_t;

676
static int node_is_ia32_comm(const ir_node *irn) {
Christian Würdig's avatar
Christian Würdig committed
677
	return is_ia32_irn(irn) ? is_ia32_commutative(irn) : 0;
Christian Würdig's avatar
Christian Würdig committed
678
679
}

680
681
682
683
684
685
686
687
688
689
690
static int ia32_get_irn_n_edges(const ir_node *irn) {
	const ir_edge_t *edge;
	int cnt = 0;

	foreach_out_edge(irn, edge) {
		cnt++;
	}

	return cnt;
}

Christian Würdig's avatar
Christian Würdig committed
691
/**
Christian Würdig's avatar
Christian Würdig committed
692
693
694
695
696
 * Determines if pred is a Proj and if is_op_func returns true for it's predecessor.
 *
 * @param pred       The node to be checked
 * @param is_op_func The check-function
 * @return 1 if conditions are fulfilled, 0 otherwise
Christian Würdig's avatar
Christian Würdig committed
697
 */
698
static int pred_is_specific_node(const ir_node *pred, is_op_func_t *is_op_func) {
Christian Würdig's avatar
Christian Würdig committed
699
	if (is_Proj(pred) && is_op_func(get_Proj_pred(pred))) {
Christian Würdig's avatar
Christian Würdig committed
700
701
702
703
704
705
		return 1;
	}

	return 0;
}

Christian Würdig's avatar
Christian Würdig committed
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
/**
 * Determines if pred is a Proj and if is_op_func returns true for it's predecessor
 * and if the predecessor is in block bl.
 *
 * @param bl         The block
 * @param pred       The node to be checked
 * @param is_op_func The check-function
 * @return 1 if conditions are fulfilled, 0 otherwise
 */
static int pred_is_specific_nodeblock(const ir_node *bl, const ir_node *pred,
	int (*is_op_func)(const ir_node *n))
{
	if (is_Proj(pred)) {
		pred = get_Proj_pred(pred);
		if ((bl == get_nodes_block(pred)) && is_op_func(pred)) {
			return 1;
		}
	}

	return 0;
}

728
/**
Christian Würdig's avatar
Christian Würdig committed
729
 * Checks if irn is a candidate for address calculation.
730
731
732
733
734
 *
 * - none of the operand must be a Load  within the same block OR
 * - all Loads must have more than one user                    OR
 * - the irn has a frame entity (it's a former FrameAddr)
 *
Christian Würdig's avatar
Christian Würdig committed
735
736
737
738
739
 * @param block   The block the Loads must/mustnot be in
 * @param irn     The irn to check
 * return 1 if irn is a candidate, 0 otherwise
 */
static int is_addr_candidate(const ir_node *block, const ir_node *irn) {
740
	ir_node *in, *left, *right;
Christian Würdig's avatar
Christian Würdig committed
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
	int      n, is_cand = 1;

	left  = get_irn_n(irn, 2);
	right = get_irn_n(irn, 3);

	in = left;

	if (pred_is_specific_nodeblock(block, in, is_ia32_Ld)) {
		n         = ia32_get_irn_n_edges(in);
		is_cand   = (n == 1) ? 0 : is_cand;  /* load with only one user: don't create LEA */
	}

	in = right;

	if (pred_is_specific_nodeblock(block, in, is_ia32_Ld)) {
		n         = ia32_get_irn_n_edges(in);
		is_cand   = (n == 1) ? 0 : is_cand;  /* load with only one user: don't create LEA */
	}

	is_cand = get_ia32_frame_ent(irn) ? 1 : is_cand;

	return is_cand;
}

/**
 * Checks if irn is a candidate for address mode.
 *
768
769
770
771
772
 * address mode (AM):
 * - at least one operand has to be a Load within the same block AND
 * - the load must not have other users than the irn             AND
 * - the irn must not have a frame entity set
 *
773
 * @param cg          The ia32 code generator
Christian Würdig's avatar
Christian Würdig committed
774
775
 * @param h           The height information of the irg
 * @param block       The block the Loads must/mustnot be in
776
 * @param irn         The irn to check
Christian Würdig's avatar
Christian Würdig committed
777
 * return 0 if irn is no candidate, 1 if left load can be used, 2 if right one, 3 for both
778
 */
779
static ia32_am_cand_t is_am_candidate(ia32_code_gen_t *cg, heights_t *h, const ir_node *block, ir_node *irn) {
Christian Würdig's avatar
Christian Würdig committed
780
	ir_node *in, *load, *other, *left, *right;
Christian Würdig's avatar
Christian Würdig committed
781
	int      n, is_cand = 0, cand;
782

783
	if (is_ia32_Ld(irn) || is_ia32_St(irn) || is_ia32_Store8Bit(irn) || is_ia32_vfild(irn) || is_ia32_vfist(irn) ||
784
		is_ia32_GetST0(irn) || is_ia32_SetST0(irn) || is_ia32_xStoreSimple(irn))
Christian Würdig's avatar
Christian Würdig committed
785
786
787
788
789
790
		return 0;

	left  = get_irn_n(irn, 2);
	right = get_irn_n(irn, 3);

	in = left;
Christian Würdig's avatar
Christian Würdig committed
791

792
	if (pred_is_specific_nodeblock(block, in, is_ia32_Ld)) {
Christian Würdig's avatar
Christian Würdig committed
793
		n         = ia32_get_irn_n_edges(in);
Christian Würdig's avatar
Christian Würdig committed
794
795
796
797
798
		is_cand   = (n == 1) ? 1 : is_cand;  /* load with more than one user: no AM */

		load  = get_Proj_pred(in);
		other = right;

799
800
801
802
		/* 8bit Loads are not supported, they cannot be used with every register */
		if (get_mode_size_bits(get_ia32_ls_mode(load)) < 16)
			is_cand = 0;

Christian Würdig's avatar
Christian Würdig committed
803
		/* If there is a data dependency of other irn from load: cannot use AM */
804
		if (is_cand && get_nodes_block(other) == block) {
805
806
			other   = skip_Proj(other);
			is_cand = heights_reachable_in_block(h, other, load) ? 0 : is_cand;
807
808
			/* this could happen in loops */
			is_cand = heights_reachable_in_block(h, load, irn) ? 0 : is_cand;
809
		}
810
811
	}

Christian Würdig's avatar
Christian Würdig committed
812
813
814
	cand    = is_cand ? IA32_AM_CAND_LEFT : IA32_AM_CAND_NONE;
	in      = right;
	is_cand = 0;
Christian Würdig's avatar
Christian Würdig committed
815

816
	if (pred_is_specific_nodeblock(block, in, is_ia32_Ld)) {
Christian Würdig's avatar
Christian Würdig committed
817
		n         = ia32_get_irn_n_edges(in);
Christian Würdig's avatar
Christian Würdig committed
818
819
820
821
822
		is_cand   = (n == 1) ? 1 : is_cand;  /* load with more than one user: no AM */

		load  = get_Proj_pred(in);
		other = left;

823
824
825
826
		/* 8bit Loads are not supported, they cannot be used with every register */
		if (get_mode_size_bits(get_ia32_ls_mode(load)) < 16)
			is_cand = 0;

Christian Würdig's avatar
Christian Würdig committed
827
		/* If there is a data dependency of other irn from load: cannot use load */
828
		if (is_cand && get_nodes_block(other) == block) {
829
830
			other   = skip_Proj(other);
			is_cand = heights_reachable_in_block(h, other, load) ? 0 : is_cand;
831
832
			/* this could happen in loops */
			is_cand = heights_reachable_in_block(h, load, irn) ? 0 : is_cand;
833
		}
834
835
	}

Christian Würdig's avatar
Christian Würdig committed
836
	cand = is_cand ? (cand | IA32_AM_CAND_RIGHT) : cand;
837

838
839
840
841
842
843
844
845
846
847
848
849
	/* check some special cases */
	if (USE_SSE2(cg) && is_ia32_Conv_I2FP(irn)) {
		/* SSE Conv I -> FP cvtsi2s(s|d) can only load 32 bit values */
		if (get_mode_size_bits(get_ia32_tgt_mode(irn)) != 32)
			cand = IA32_AM_CAND_NONE;
	}
	else if (is_ia32_Conv_I2I(irn)) {
		/* we cannot load an N bit value and implicitly convert it into an M bit value if N > M */
		if (get_mode_size_bits(get_ia32_src_mode(irn)) > get_mode_size_bits(get_ia32_tgt_mode(irn)))
			cand = IA32_AM_CAND_NONE;
	}

Christian Würdig's avatar
Christian Würdig committed
850
851
	/* if the irn has a frame entity: we do not use address mode */
	return get_ia32_frame_ent(irn) ? IA32_AM_CAND_NONE : cand;
852
853
854
855
856
857
858
859
860
861
862
863
}

/**
 * Compares the base and index addr and the load/store entities
 * and returns 1 if they are equal.
 */
static int load_store_addr_is_equal(const ir_node *load, const ir_node *store,
									const ir_node *addr_b, const ir_node *addr_i)
{
	int     is_equal = (addr_b == get_irn_n(load, 0)) && (addr_i == get_irn_n(load, 1));
	entity *lent     = get_ia32_frame_ent(load);
	entity *sent     = get_ia32_frame_ent(store);
864
865
	ident  *lid      = get_ia32_am_sc(load);
	ident  *sid      = get_ia32_am_sc(store);
866
867
	char   *loffs    = get_ia32_am_offs(load);
	char   *soffs    = get_ia32_am_offs(store);
868
869

	/* are both entities set and equal? */
870
	if (is_equal && (lent || sent))
871
		is_equal = lent && sent && (lent == sent);
Christian Würdig's avatar
Christian Würdig committed
872

873
874
875
	/* are address mode idents set and equal? */
	if (is_equal && (lid || sid))
		is_equal = lid && sid && (lid == sid);
876

877
	/* are offsets set and equal */
878
	if (is_equal && (loffs || soffs))
879
880
		is_equal = loffs && soffs && strcmp(loffs, soffs) == 0;

881
882
883
	/* are the load and the store of the same mode? */
	is_equal = is_equal ? get_ia32_ls_mode(load) == get_ia32_ls_mode(store) : 0;

884
885
886
	return is_equal;
}

Christian Würdig's avatar
Christian Würdig committed
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
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
typedef enum _ia32_take_lea_attr {
	IA32_LEA_ATTR_NONE  = 0,
	IA32_LEA_ATTR_BASE  = (1 << 0),
	IA32_LEA_ATTR_INDEX = (1 << 1),
	IA32_LEA_ATTR_OFFS  = (1 << 2),
	IA32_LEA_ATTR_SCALE = (1 << 3),
	IA32_LEA_ATTR_AMSC  = (1 << 4),
	IA32_LEA_ATTR_FENT  = (1 << 5)
} ia32_take_lea_attr;

/**
 * Decides if we have to keep the LEA operand or if we can assimilate it.
 */
static int do_new_lea(ir_node *irn, ir_node *base, ir_node *index, ir_node *lea,
		int have_am_sc, ia32_code_gen_t *cg)
{
	entity  *irn_ent  = get_ia32_frame_ent(irn);
	entity  *lea_ent  = get_ia32_frame_ent(lea);
	int      ret_val  = 0;
	int      is_noreg_base  = be_is_NoReg(cg, base);
	int      is_noreg_index = be_is_NoReg(cg, index);
	ia32_am_flavour_t am_flav = get_ia32_am_flavour(lea);

	/* If the Add and the LEA both have a different frame entity set: keep */
	if (irn_ent && lea_ent && (irn_ent != lea_ent))
		return IA32_LEA_ATTR_NONE;
	else if (! irn_ent && lea_ent)
		ret_val |= IA32_LEA_ATTR_FENT;

	/* If the Add and the LEA both have already an address mode symconst: keep */
	if (have_am_sc && get_ia32_am_sc(lea))
		return IA32_LEA_ATTR_NONE;
	else if (get_ia32_am_sc(lea))
		ret_val |= IA32_LEA_ATTR_AMSC;

	/* Check the different base-index combinations */

	if (! is_noreg_base && ! is_noreg_index) {
		/* Assimilate if base is the lea and the LEA is just a Base + Offset calculation */
		if ((base == lea) && ! (am_flav & ia32_I ? 1 : 0)) {
			if (am_flav & ia32_O)
				ret_val |= IA32_LEA_ATTR_OFFS;

			ret_val |= IA32_LEA_ATTR_BASE;
		}
		else
			return IA32_LEA_ATTR_NONE;
	}
	else if (! is_noreg_base && is_noreg_index) {
		/* Base is set but index not */
		if (base == lea) {
			/* Base points to LEA: assimilate everything */
			if (am_flav & ia32_O)
				ret_val |= IA32_LEA_ATTR_OFFS;
			if (am_flav & ia32_S)
				ret_val |= IA32_LEA_ATTR_SCALE;
			if (am_flav & ia32_I)
				ret_val |= IA32_LEA_ATTR_INDEX;

			ret_val |= IA32_LEA_ATTR_BASE;
		}
		else if (am_flav & ia32_B ? 0 : 1) {
			/* Base is not the LEA but the LEA is an index only calculation: assimilate */
			if (am_flav & ia32_O)
				ret_val |= IA32_LEA_ATTR_OFFS;
			if (am_flav & ia32_S)
				ret_val |= IA32_LEA_ATTR_SCALE;

			ret_val |= IA32_LEA_ATTR_INDEX;
		}
		else
			return IA32_LEA_ATTR_NONE;
	}
	else if (is_noreg_base && ! is_noreg_index) {
		/* Index is set but not base */
		if (index == lea) {
			/* Index points to LEA: assimilate everything */
			if (am_flav & ia32_O)
				ret_val |= IA32_LEA_ATTR_OFFS;
			if (am_flav & ia32_S)
				ret_val |= IA32_LEA_ATTR_SCALE;
			if (am_flav & ia32_B)
				ret_val |= IA32_LEA_ATTR_BASE;

			ret_val |= IA32_LEA_ATTR_INDEX;
		}
		else if (am_flav & ia32_I ? 0 : 1) {
			/* Index is not the LEA but the LEA is a base only calculation: assimilate */
			if (am_flav & ia32_O)
				ret_val |= IA32_LEA_ATTR_OFFS;
			if (am_flav & ia32_S)
				ret_val |= IA32_LEA_ATTR_SCALE;

			ret_val |= IA32_LEA_ATTR_BASE;
		}
		else
			return IA32_LEA_ATTR_NONE;
	}
	else {
		assert(0 && "There must have been set base or index");
	}

	return ret_val;
}
Christian Würdig's avatar
Christian Würdig committed
991

992
993
994
995
996
997
998
999
1000
/**
 * Adds res before irn into schedule if irn was scheduled.
 * @param irn  The schedule point
 * @param res  The node to be scheduled
 */
static INLINE void try_add_to_sched(ir_node *irn, ir_node *res) {
	if (sched_is_scheduled(irn))
		sched_add_before(irn, res);
}
For faster browsing, not all history is shown. View entire blame