ia32_optimize.c 47.8 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
	*/
502
	mem  = get_irn_n(irn, 3);
503
504
505
	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);
506

507
508
509
510
	/* 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));

511
512
513
514
515
516
517
518
519
520
521
522
523
	/* 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) {
524
	/* TODO */
525
526
}

527
/**
528
 * Tries to optimize two following IncSP.
529
 */
530
531
532
533
534
535
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 */
536
537
		int prev_offs = be_get_IncSP_offset(prev);
		int curr_offs = be_get_IncSP_offset(irn);
538

539
		be_set_IncSP_offset(prev, prev_offs + curr_offs);
Christian Würdig's avatar
Christian Würdig committed
540
541
542

		/* Omit the optimized IncSP */
		be_set_IncSP_pred(irn, be_get_IncSP_pred(prev));
543
	}
544
545
546
547
548
549
550
551
}

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

552
553
554
555
556
557
558
	/* 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);
	}
559
	/* seems to be buggy when using Pushes */
560
561
	else if (be_is_IncSP(irn))
		ia32_optimize_IncSP(irn, cg);
562
563
	else if (is_ia32_Store(irn))
		ia32_create_Push(irn, cg);
564
565
566
567
}



Christian Würdig's avatar
Christian Würdig committed
568
569
570
571
572
573
574
575
576
577
/******************************************************************
 *              _     _                   __  __           _
 *     /\      | |   | |                 |  \/  |         | |
 *    /  \   __| | __| |_ __ ___  ___ ___| \  / | ___   __| | ___
 *   / /\ \ / _` |/ _` | '__/ _ \/ __/ __| |\/| |/ _ \ / _` |/ _ \
 *  / ____ \ (_| | (_| | | |  __/\__ \__ \ |  | | (_) | (_| |  __/
 * /_/    \_\__,_|\__,_|_|  \___||___/___/_|  |_|\___/ \__,_|\___|
 *
 ******************************************************************/

Christian Würdig's avatar
Christian Würdig committed
578
579
580
581
582
typedef struct {
	ia32_code_gen_t *cg;
	heights_t       *h;
} ia32_am_opt_env_t;

583
static int node_is_ia32_comm(const ir_node *irn) {
Christian Würdig's avatar
Christian Würdig committed
584
	return is_ia32_irn(irn) ? is_ia32_commutative(irn) : 0;
Christian Würdig's avatar
Christian Würdig committed
585
586
}

587
588
589
590
591
592
593
594
595
596
597
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
598
/**
Christian Würdig's avatar
Christian Würdig committed
599
600
601
602
603
 * 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
604
 */
605
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
606
	if (is_Proj(pred) && is_op_func(get_Proj_pred(pred))) {
Christian Würdig's avatar
Christian Würdig committed
607
608
609
610
611
612
		return 1;
	}

	return 0;
}

Christian Würdig's avatar
Christian Würdig committed
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
/**
 * 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;
}

635
/**
Christian Würdig's avatar
Christian Würdig committed
636
 * Checks if irn is a candidate for address calculation.
637
638
639
640
641
 *
 * - 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
642
643
644
645
646
 * @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) {
647
	ir_node *in, *left, *right;
Christian Würdig's avatar
Christian Würdig committed
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
	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.
 *
675
676
677
678
679
 * 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
 *
680
 * @param cg          The ia32 code generator
Christian Würdig's avatar
Christian Würdig committed
681
682
 * @param h           The height information of the irg
 * @param block       The block the Loads must/mustnot be in
683
 * @param irn         The irn to check
Christian Würdig's avatar
Christian Würdig committed
684
 * return 0 if irn is no candidate, 1 if left load can be used, 2 if right one, 3 for both
685
 */
686
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
687
	ir_node *in, *load, *other, *left, *right;
Christian Würdig's avatar
Christian Würdig committed
688
	int      n, is_cand = 0, cand;
689

690
	if (is_ia32_Ld(irn) || is_ia32_St(irn) || is_ia32_Store8Bit(irn) || is_ia32_vfild(irn) || is_ia32_vfist(irn) ||
691
		is_ia32_GetST0(irn) || is_ia32_SetST0(irn) || is_ia32_xStoreSimple(irn))
Christian Würdig's avatar
Christian Würdig committed
692
693
694
695
696
697
		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
698

699
	if (pred_is_specific_nodeblock(block, in, is_ia32_Ld)) {
Christian Würdig's avatar
Christian Würdig committed
700
		n         = ia32_get_irn_n_edges(in);
Christian Würdig's avatar
Christian Würdig committed
701
702
703
704
705
		is_cand   = (n == 1) ? 1 : is_cand;  /* load with more than one user: no AM */

		load  = get_Proj_pred(in);
		other = right;

706
707
708
709
		/* 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
710
		/* If there is a data dependency of other irn from load: cannot use AM */
711
		if (is_cand && get_nodes_block(other) == block) {
712
713
			other   = skip_Proj(other);
			is_cand = heights_reachable_in_block(h, other, load) ? 0 : is_cand;
714
715
			/* this could happen in loops */
			is_cand = heights_reachable_in_block(h, load, irn) ? 0 : is_cand;
716
		}
717
718
	}

Christian Würdig's avatar
Christian Würdig committed
719
720
721
	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
722

723
	if (pred_is_specific_nodeblock(block, in, is_ia32_Ld)) {
Christian Würdig's avatar
Christian Würdig committed
724
		n         = ia32_get_irn_n_edges(in);
Christian Würdig's avatar
Christian Würdig committed
725
726
727
728
729
		is_cand   = (n == 1) ? 1 : is_cand;  /* load with more than one user: no AM */

		load  = get_Proj_pred(in);
		other = left;

730
731
732
733
		/* 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
734
		/* If there is a data dependency of other irn from load: cannot use load */
735
		if (is_cand && get_nodes_block(other) == block) {
736
737
			other   = skip_Proj(other);
			is_cand = heights_reachable_in_block(h, other, load) ? 0 : is_cand;
738
739
			/* this could happen in loops */
			is_cand = heights_reachable_in_block(h, load, irn) ? 0 : is_cand;
740
		}
741
742
	}

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

745
746
747
748
749
750
751
752
753
754
755
756
	/* 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
757
758
	/* if the irn has a frame entity: we do not use address mode */
	return get_ia32_frame_ent(irn) ? IA32_AM_CAND_NONE : cand;
759
760
761
762
763
764
765
766
767
768
769
770
}

/**
 * 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);
771
772
	ident  *lid      = get_ia32_am_sc(load);
	ident  *sid      = get_ia32_am_sc(store);
773
774
	char   *loffs    = get_ia32_am_offs(load);
	char   *soffs    = get_ia32_am_offs(store);
775
776

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

780
781
782
	/* are address mode idents set and equal? */
	if (is_equal && (lid || sid))
		is_equal = lid && sid && (lid == sid);
783

784
	/* are offsets set and equal */
785
	if (is_equal && (loffs || soffs))
786
787
		is_equal = loffs && soffs && strcmp(loffs, soffs) == 0;

788
789
790
	/* 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;

791
792
793
	return is_equal;
}

Christian Würdig's avatar
Christian Würdig committed
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
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
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
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
/**
 * 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
927

Christian Würdig's avatar
Christian Würdig committed
928
929
930
/**
 * Folds Add or Sub to LEA if possible
 */
931
static ir_node *fold_addr(ia32_code_gen_t *cg, ir_node *irn, ir_node *noreg) {
932
933
934
935
	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
936
937
938
	ir_node    *shift      = NULL;
	ir_node    *lea_o      = NULL;
	ir_node    *lea        = NULL;
939
940
941
942
943
944
945
946
947
	char       *offs       = NULL;
	const char *offs_cnst  = NULL;
	char       *offs_lea   = NULL;
	int         scale      = 0;
	int         isadd      = 0;
	int         dolea      = 0;
	int         have_am_sc = 0;
	int         am_sc_sign = 0;
	ident      *am_sc      = NULL;
Christian Würdig's avatar
Christian Würdig committed
948
	entity     *lea_ent    = NULL;
949
	ir_node    *left, *right, *temp;
Christian Würdig's avatar
Christian Würdig committed
950
	ir_node    *base, *index;
Christian Würdig's avatar
Christian Würdig committed
951
	int consumed_left_shift;
Christian Würdig's avatar
Christian Würdig committed
952
	ia32_am_flavour_t am_flav;
953
	DEBUG_ONLY(firm_dbg_module_t *mod = cg->mod;)
Christian Würdig's avatar
Christian Würdig committed
954
955
956
957
958
959
960

	if (is_ia32_Add(irn))
		isadd = 1;

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

Christian Würdig's avatar
Christian Würdig committed
961
	/* "normalize" arguments in case of add with two operands */
Michael Beck's avatar
Michael Beck committed
962
	if  (isadd && ! be_is_NoReg(cg, right)) {
Christian Würdig's avatar
Christian Würdig committed
963
964
		/* put LEA == ia32_am_O as right operand */
		if (is_ia32_Lea(left) && get_ia32_am_flavour(left) == ia32_am_O) {
Christian Würdig's avatar
Christian Würdig committed
965
966
			set_irn_n(irn, 2, right);
			set_irn_n(irn, 3, left);
Christian Würdig's avatar
Christian Würdig committed
967
968
969
970
971
972
973
			temp  = left;
			left  = right;
			right = temp;
		}

		/* put LEA != ia32_am_O as left operand */
		if (is_ia32_Lea(right) && get_ia32_am_flavour(right) != ia32_am_O) {
Christian Würdig's avatar
Christian Würdig committed
974
975
			set_irn_n(irn, 2, right);
			set_irn_n(irn, 3, left);
Christian Würdig's avatar
Christian Würdig committed
976
977
978
979
980
			temp  = left;
			left  = right;
			right = temp;
		}

Christian Würdig's avatar
Christian Würdig committed
981
982
		/* put SHL as left operand iff left is NOT a LEA */
		if (! is_ia32_Lea(left) && pred_is_specific_node(right, is_ia32_Shl)) {
Christian Würdig's avatar
Christian Würdig committed
983
984
			set_irn_n(irn, 2, right);
			set_irn_n(irn, 3, left);
Christian Würdig's avatar
Christian Würdig committed
985
986
987
988
989
990
			temp  = left;
			left  = right;
			right = temp;
		}
	}

Christian Würdig's avatar
Christian Würdig committed
991
992
993
994
995
996
	base    = left;
	index   = noreg;
	offs    = NULL;
	scale   = 0;
	am_flav = 0;

997
998
999
	/* check for operation with immediate */
	if (is_ia32_ImmConst(irn)) {
		DBG((mod, LEVEL_1, "\tfound op with imm const"));
Christian Würdig's avatar
Christian Würdig committed
1000

For faster browsing, not all history is shown. View entire blame