ia32_optimize.c 49.5 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

Christian Würdig's avatar
Christian Würdig committed
38
39
40
41
42
43
44
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
45
46
47
#undef is_NoMem
#define is_NoMem(irn) (get_irn_op(irn) == op_NoMem)

48
typedef int is_op_func_t(const ir_node *n);
49
typedef ir_node *load_func_t(dbg_info *db, ir_graph *irg, ir_node *block, ir_node *base, ir_node *index, ir_node *mem);
50

Michael Beck's avatar
Michael Beck committed
51
52
53
54
55
56
57
/**
 * 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
58

Michael Beck's avatar
Michael Beck committed
59
60
	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
61
62
}

63
64
65
66
67
68
69
70
71
72
73
74


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

75
76
77
78
79
80
81
82
/**
 * 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)
{
83
84
	static unsigned id = 0;
	char str[256];
85

86
87
	snprintf(str, sizeof(str), tag, ++id);
	return new_id_from_str(str);
88
89
90
91
92
93
94
95
96
97
98
99
}

/**
 * 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
100
101
102
103
	dbg_info *dbg   = env->dbg;
	ir_mode  *mode  = env->mode;
	ir_graph *irg   = env->irg;
	ir_node  *block = env->block;
104
	ir_node  *cnst;
105

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

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

118
119
	return cnst;
}
120

121
122
123
124
125
126
127
128
129
130
131
132
133
/**
 * 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);
134
	}
135
136
137
138
	else
		res = e->value;
	return res;
}
139

140
141
142
143
144
145
/**
 * 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);
146
	pmap_entry *e = pmap_find(cg->isa->tv_ent, tv);
147
148
149
150
151
152
153
	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)
154
			tp = get_prim_type(cg->isa->types, mode);
155

156
		res = new_entity(get_glob_type(), unique_id(".LC%u"), tp);
157
158
159
160
161
162
163
164
165
166
167
168

		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;
169
170

		pmap_insert(cg->isa->tv_ent, tv, res);
171
172
173
174
	}
	else
		res = e->value;
	return res;
175
176
177
178
179
180
181
182
183
184
185
186
}

/**
 * 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) {
187
	ir_node *cnst, *load;
188
	symconst_symbol sym;
Christian Würdig's avatar
Christian Würdig committed
189
190
191
	ir_graph *irg   = env->irg;
	ir_node  *block = env->block;
	ir_node  *node  = env->irn;
192
193
	dbg_info *dbg   = env->dbg;
	ir_mode  *mode  = env->mode;
194
195

	if (mode_is_float(mode)) {
Michael Beck's avatar
Michael Beck committed
196
		FP_USED(env->cg);
Michael Beck's avatar
Michael Beck committed
197
198
199
200
201
202
203
204
		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);
		}
205
		sym.entity_p = get_entity_for_tv(env->cg, node);
206

207
208
209
210
211
212
213
214
215

		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;
216
217
	}
	else {
218
		cnst = new_rd_ia32_Const(dbg, irg, block, get_irg_no_mem(irg), get_irn_mode(node));
219
220
221
222
223
224
225
		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
226
 * block where they are used (or in the cfg-pred Block in case of Phi's).
227
 * Additionally all reference nodes are changed into mode_Is nodes.
228
 * NOTE: irn must be a firm constant!
229
 */
230
231
232
233
234
235
236
237
238
239
240
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;)
241

242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
	/* 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) {
269
	ia32_code_gen_t *cg = env;
270

271
	if(!is_Const(irn) && !is_SymConst(irn))
272
273
		return;

274
	ia32_transform_const(irn, cg);
275
276
277
278
279
280
281
282
283
284
285
286
}

/**
 * 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
287
	}
288
}
289

290
291
292
293
294
295
/**
 * 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) {
296
	irg_walk_graph(cg->irg, NULL, ia32_place_consts_walker, cg);
297
}
Christian Würdig's avatar
Christian Würdig committed
298

299
300
301
302
303
304
305
306
307
308
309
/* 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);
}
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326

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

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

static int ia32_cnst_compare(ir_node *n1, ir_node *n2) {
327
	return get_ia32_id_cnst(n1) == get_ia32_id_cnst(n2);
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
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
}

/**
 * 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) {
414
	int i, n      = get_irn_arity(cand);
Christian Würdig's avatar
Christian Würdig committed
415
	int same_args = 1;
416
417

	for (i = 0; i < n; i++) {
Michael Beck's avatar
Michael Beck committed
418
		if (get_irn_n(cand, i) != get_irn_n(irn, i)) {
Christian Würdig's avatar
Christian Würdig committed
419
			same_args = 0;
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
			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
442
		DBG_OPT_CJMP(irn);
443

Michael Beck's avatar
Michael Beck committed
444
		set_irn_op(irn, op_ia32_CJmpAM);
445
446
447
448
449

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

450
451
452
453
/**
 * Creates a Push from Store(IncSP(gp_reg_size))
 */
static void ia32_create_Push(ir_node *irn, ia32_code_gen_t *cg) {
454
455
456
	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;
457
	const ir_edge_t *edge;
Christian Würdig's avatar
Christian Würdig committed
458
	heights_t *h;
459

Christian Würdig's avatar
Christian Würdig committed
460
461
	/* 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))
462
463
		return;

Christian Würdig's avatar
Christian Würdig committed
464
	/* do not create push if index is not NOREG */
465
	if (arch_get_irn_register(cg->arch_env, get_irn_n(irn, 1)) !=
Christian Würdig's avatar
Christian Würdig committed
466
		&ia32_gp_regs[REG_GP_NOREG])
467
468
		return;

Christian Würdig's avatar
Christian Würdig committed
469
	/* do not create push for floating point */
470
471
472
473
	val = get_irn_n(irn, 2);
	if (mode_is_float(get_irn_mode(val)))
		return;

Christian Würdig's avatar
Christian Würdig committed
474
	/* do not create push if IncSp doesn't expand stack or expand size is different from register size */
475
	if (be_get_IncSP_offset(sp) != get_mode_size_bytes(ia32_reg_classes[CLASS_ia32_gp].mode))
476
477
		return;

Christian Würdig's avatar
Christian Würdig committed
478
479
480
481
482
483
484
485
486
487
	/* 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);

488
	/* ok, translate into Push */
Christian Würdig's avatar
Christian Würdig committed
489
	edge       = get_irn_out_edge_first(irn);
490
	old_proj_M = get_edge_src_irn(edge);
491
	bl         = get_nodes_block(irn);
492
493
494
495
496

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

497
498
499
500
501
	/*
		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
	*/
Sebastian Hack's avatar
Sebastian Hack committed
502
	mem = get_irn_n(irn, 3);
503
504
505
506

	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);
Sebastian Hack's avatar
Sebastian Hack committed
507
	add_irn_deps(push, sp);
508

509
510
511
512
	/* 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));

513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
	/* 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
537
		&ia32_gp_regs[REG_GP_NOREG])
538
		return;
Michael Beck's avatar
Michael Beck committed
539
	if (arch_get_irn_register(cg->arch_env, get_irn_n(load, 0)) != cg->isa->arch_isa.sp)
540
541
542
543
544
545
546
547
548
549
550
551
		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
552
		return; /* should not happen */
553
554
555
556
557
558
559
560
561
562
563
564
565
	}

	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
566
567
568
569
	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);
570
571
572
573
574
575
576
577
578
579
580
581
582

	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);
}

583
/**
584
 * Tries to optimize two following IncSP.
585
 */
586
587
588
589
590
591
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 */
592
593
		int prev_offs = be_get_IncSP_offset(prev);
		int curr_offs = be_get_IncSP_offset(irn);
594

595
		be_set_IncSP_offset(prev, prev_offs + curr_offs);
Christian Würdig's avatar
Christian Würdig committed
596
597
598

		/* Omit the optimized IncSP */
		be_set_IncSP_pred(irn, be_get_IncSP_pred(prev));
599
	}
600
601
602
603
604
605
606
607
}

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

608
609
610
611
612
613
614
	/* 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);
	}
615
	/* seems to be buggy when using Pushes */
616
617
	else if (be_is_IncSP(irn))
		ia32_optimize_IncSP(irn, cg);
618
619
	else if (is_ia32_Store(irn))
		ia32_create_Push(irn, cg);
620
621
622
623
}



Christian Würdig's avatar
Christian Würdig committed
624
625
626
627
628
629
630
631
632
633
/******************************************************************
 *              _     _                   __  __           _
 *     /\      | |   | |                 |  \/  |         | |
 *    /  \   __| | __| |_ __ ___  ___ ___| \  / | ___   __| | ___
 *   / /\ \ / _` |/ _` | '__/ _ \/ __/ __| |\/| |/ _ \ / _` |/ _ \
 *  / ____ \ (_| | (_| | | |  __/\__ \__ \ |  | | (_) | (_| |  __/
 * /_/    \_\__,_|\__,_|_|  \___||___/___/_|  |_|\___/ \__,_|\___|
 *
 ******************************************************************/

Christian Würdig's avatar
Christian Würdig committed
634
635
636
637
638
typedef struct {
	ia32_code_gen_t *cg;
	heights_t       *h;
} ia32_am_opt_env_t;

639
static int node_is_ia32_comm(const ir_node *irn) {
Christian Würdig's avatar
Christian Würdig committed
640
	return is_ia32_irn(irn) ? is_ia32_commutative(irn) : 0;
Christian Würdig's avatar
Christian Würdig committed
641
642
}

643
644
645
646
647
648
649
650
651
652
653
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
654
/**
Christian Würdig's avatar
Christian Würdig committed
655
656
657
658
659
 * 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
660
 */
661
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
662
	if (is_Proj(pred) && is_op_func(get_Proj_pred(pred))) {
Christian Würdig's avatar
Christian Würdig committed
663
664
665
666
667
668
		return 1;
	}

	return 0;
}

Christian Würdig's avatar
Christian Würdig committed
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/**
 * 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;
}

691
/**
Christian Würdig's avatar
Christian Würdig committed
692
 * Checks if irn is a candidate for address calculation.
693
694
695
696
697
 *
 * - 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
698
699
700
701
702
 * @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) {
703
	ir_node *in, *left, *right;
Christian Würdig's avatar
Christian Würdig committed
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
	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.
 *
731
732
733
734
735
 * 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
 *
736
 * @param cg          The ia32 code generator
Christian Würdig's avatar
Christian Würdig committed
737
738
 * @param h           The height information of the irg
 * @param block       The block the Loads must/mustnot be in
739
 * @param irn         The irn to check
Christian Würdig's avatar
Christian Würdig committed
740
 * return 0 if irn is no candidate, 1 if left load can be used, 2 if right one, 3 for both
741
 */
742
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
743
	ir_node *in, *load, *other, *left, *right;
Christian Würdig's avatar
Christian Würdig committed
744
	int      n, is_cand = 0, cand;
745

746
	if (is_ia32_Ld(irn) || is_ia32_St(irn) || is_ia32_Store8Bit(irn) || is_ia32_vfild(irn) || is_ia32_vfist(irn) ||
747
		is_ia32_GetST0(irn) || is_ia32_SetST0(irn) || is_ia32_xStoreSimple(irn))
Christian Würdig's avatar
Christian Würdig committed
748
749
750
751
752
753
		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
754

755
	if (pred_is_specific_nodeblock(block, in, is_ia32_Ld)) {
Christian Würdig's avatar
Christian Würdig committed
756
		n         = ia32_get_irn_n_edges(in);
Christian Würdig's avatar
Christian Würdig committed
757
758
759
760
761
		is_cand   = (n == 1) ? 1 : is_cand;  /* load with more than one user: no AM */

		load  = get_Proj_pred(in);
		other = right;

762
763
764
765
		/* 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
766
		/* If there is a data dependency of other irn from load: cannot use AM */
767
		if (is_cand && get_nodes_block(other) == block) {
768
769
			other   = skip_Proj(other);
			is_cand = heights_reachable_in_block(h, other, load) ? 0 : is_cand;
770
771
			/* this could happen in loops */
			is_cand = heights_reachable_in_block(h, load, irn) ? 0 : is_cand;
772
		}
773
774
	}

Christian Würdig's avatar
Christian Würdig committed
775
776
777
	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
778

779
	if (pred_is_specific_nodeblock(block, in, is_ia32_Ld)) {
Christian Würdig's avatar
Christian Würdig committed
780
		n         = ia32_get_irn_n_edges(in);
Christian Würdig's avatar
Christian Würdig committed
781
782
783
784
785
		is_cand   = (n == 1) ? 1 : is_cand;  /* load with more than one user: no AM */

		load  = get_Proj_pred(in);
		other = left;

786
787
788
789
		/* 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
790
		/* If there is a data dependency of other irn from load: cannot use load */
791
		if (is_cand && get_nodes_block(other) == block) {
792
793
			other   = skip_Proj(other);
			is_cand = heights_reachable_in_block(h, other, load) ? 0 : is_cand;
794
795
			/* this could happen in loops */
			is_cand = heights_reachable_in_block(h, load, irn) ? 0 : is_cand;
796
		}
797
798
	}

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

801
802
803
804
805
806
807
808
809
810
811
812
	/* 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
813
814
	/* if the irn has a frame entity: we do not use address mode */
	return get_ia32_frame_ent(irn) ? IA32_AM_CAND_NONE : cand;
815
816
817
818
819
820
821
822
823
824
825
826
}

/**
 * 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);
827
828
	ident  *lid      = get_ia32_am_sc(load);
	ident  *sid      = get_ia32_am_sc(store);
829
830
	char   *loffs    = get_ia32_am_offs(load);
	char   *soffs    = get_ia32_am_offs(store);
831
832

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

836
837
838
	/* are address mode idents set and equal? */
	if (is_equal && (lid || sid))
		is_equal = lid && sid && (lid == sid);
839

840
	/* are offsets set and equal */
841
	if (is_equal && (loffs || soffs))
842
843
		is_equal = loffs && soffs && strcmp(loffs, soffs) == 0;

844
845
846
	/* 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;

847
848
849
	return is_equal;
}

Christian Würdig's avatar
Christian Würdig committed
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
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
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
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
/**
 * 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);
}

/**
 * Removes irn from schedule if it was scheduled. If irn is a mode_T node
 * all it's Projs are removed as well.
 * @param irn  The irn to be removed from schedule
 */
static INLINE void try_remove_from_sched(ir_node *irn) {
	if (sched_is_scheduled(irn)) {
		if (get_irn_mode(irn) == mode_T) {
			const ir_edge_t *edge;
			foreach_out_edge(irn, edge) {
				ir_node *proj = get_edge_src_irn(edge);
				if (sched_is_scheduled(proj))
					sched_remove(proj);
			}
		}
		sched_remove(irn);
	}
}
Christian Würdig's avatar
Christian Würdig committed
983

Christian Würdig's avatar
Christian Würdig committed
984
985
986
/**
 * Folds Add or Sub to LEA if possible
 */
987
static ir_node *fold_addr(ia32_code_gen_t *cg, ir_node *irn, ir_node *noreg) {
988
989
990
991
	ir_graph   *irg        = get_irn_irg(irn);
	dbg_info   *dbg        = get_irn_dbg_info(irn);
	ir_node    *block      = get_nodes_block(irn);
	ir_node    *res        = irn;
Christian Würdig's avatar
Christian Würdig committed
992
993
994
	ir_node    *shift      = NULL;
	ir_node    *lea_o      = NULL;
	ir_node    *lea        = NULL;
995
996
997
998
999
1000
	char       *offs       = NULL;
	const char *offs_cnst  = NULL;
	char       *offs_lea   = NULL;
	int         scale      = 0;
	int         isadd      = 0;
	int         dolea      = 0;
For faster browsing, not all history is shown. View entire blame