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

Matthias Braun's avatar
Matthias Braun committed
20
21
22
23
24
/**
 * @file
 * @brief   iropt --- optimizations intertwined with IR construction.
 * @author  Christian Schaefer, Goetz Lindenmaier, Michael Beck
 * @version $Id$
Götz Lindenmaier's avatar
Götz Lindenmaier committed
25
 */
Matthias Braun's avatar
Matthias Braun committed
26
#include "config.h"
Michael Beck's avatar
Michael Beck committed
27
28

#include <string.h>
29
#include <stdbool.h>
Boris Boesler's avatar
added    
Boris Boesler committed
30

Michael Beck's avatar
Michael Beck committed
31
32
33
34
35
36
37
#include "irnode_t.h"
#include "irgraph_t.h"
#include "iredges_t.h"
#include "irmode_t.h"
#include "iropt_t.h"
#include "ircons_t.h"
#include "irgmod.h"
38
#include "irverify.h"
39
#include "iroptimize.h"
Michael Beck's avatar
Michael Beck committed
40
41
42
43
44
45
46
#include "tv_t.h"
#include "dbginfo_t.h"
#include "iropt_dbg.h"
#include "irflag_t.h"
#include "irhooks.h"
#include "irarch.h"
#include "hashptr.h"
47
#include "opt_polymorphy.h"
Michael Beck's avatar
Michael Beck committed
48
#include "irtools.h"
49
#include "irhooks.h"
50
#include "array_t.h"
51
52
#include "vrp.h"
#include "firm_types.h"
53
#include "bitfiddle.h"
54
#include "be.h"
Christian Schäfer's avatar
Christian Schäfer committed
55

Götz Lindenmaier's avatar
Götz Lindenmaier committed
56
/* Make types visible to allow most efficient access */
57
#include "entity_t.h"
Götz Lindenmaier's avatar
Götz Lindenmaier committed
58

59
60
61
/**
 * Returns the tarval of a Const node or tarval_bad for all other nodes.
 */
Matthias Braun's avatar
Matthias Braun committed
62
static ir_tarval *default_value_of(const ir_node *n)
63
{
64
65
66
67
68
69
70
71
	if (is_Const(n))
		return get_Const_tarval(n); /* might return tarval_bad */
	else
		return tarval_bad;
}

value_of_func value_of_ptr = default_value_of;

Michael Beck's avatar
Michael Beck committed
72
/* * Set a new value_of function. */
73
74
void set_value_of_func(value_of_func func)
{
75
76
77
78
79
80
	if (func != NULL)
		value_of_ptr = func;
	else
		value_of_ptr = default_value_of;
}

81
/**
82
 * Return the value of a Constant.
83
 */
Matthias Braun's avatar
Matthias Braun committed
84
static ir_tarval *computed_value_Const(const ir_node *n)
85
{
Michael Beck's avatar
Michael Beck committed
86
	return get_Const_tarval(n);
87
}  /* computed_value_Const */
Christian Schäfer's avatar
Christian Schäfer committed
88

89
/**
Michael Beck's avatar
Michael Beck committed
90
 * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
91
 */
Matthias Braun's avatar
Matthias Braun committed
92
static ir_tarval *computed_value_SymConst(const ir_node *n)
93
{
Michael Beck's avatar
Michael Beck committed
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
	ir_type   *type;
	ir_entity *ent;

	switch (get_SymConst_kind(n)) {
	case symconst_type_size:
		type = get_SymConst_type(n);
		if (get_type_state(type) == layout_fixed)
			return new_tarval_from_long(get_type_size_bytes(type), get_irn_mode(n));
		break;
	case symconst_type_align:
		type = get_SymConst_type(n);
		if (get_type_state(type) == layout_fixed)
			return new_tarval_from_long(get_type_alignment_bytes(type), get_irn_mode(n));
		break;
	case symconst_ofs_ent:
		ent  = get_SymConst_entity(n);
		type = get_entity_owner(ent);
		if (get_type_state(type) == layout_fixed)
			return new_tarval_from_long(get_entity_offset(ent), get_irn_mode(n));
		break;
	default:
		break;
	}
	return tarval_bad;
118
}  /* computed_value_SymConst */
Christian Schäfer's avatar
Christian Schäfer committed
119

Michael Beck's avatar
Michael Beck committed
120
/**
121
 * Return the value of an Add.
Michael Beck's avatar
Michael Beck committed
122
 */
Matthias Braun's avatar
Matthias Braun committed
123
static ir_tarval *computed_value_Add(const ir_node *n)
124
{
Michael Beck's avatar
Michael Beck committed
125
126
	ir_node *a = get_Add_left(n);
	ir_node *b = get_Add_right(n);
Christian Schäfer's avatar
Christian Schäfer committed
127

Matthias Braun's avatar
Matthias Braun committed
128
129
	ir_tarval *ta = value_of(a);
	ir_tarval *tb = value_of(b);
130

131
	if ((ta != tarval_bad) && (tb != tarval_bad))
Michael Beck's avatar
Michael Beck committed
132
		return tarval_add(ta, tb);
133

Michael Beck's avatar
Michael Beck committed
134
	return tarval_bad;
135
}  /* computed_value_Add */
Christian Schäfer's avatar
Christian Schäfer committed
136

Michael Beck's avatar
Michael Beck committed
137
/**
138
 * Return the value of a Sub.
Michael Beck's avatar
Michael Beck committed
139
140
 * Special case: a - a
 */
Matthias Braun's avatar
Matthias Braun committed
141
static ir_tarval *computed_value_Sub(const ir_node *n)
142
{
Matthias Braun's avatar
Matthias Braun committed
143
144
145
146
147
	ir_mode   *mode = get_irn_mode(n);
	ir_node   *a    = get_Sub_left(n);
	ir_node   *b    = get_Sub_right(n);
	ir_tarval *ta;
	ir_tarval *tb;
Christian Schäfer's avatar
Christian Schäfer committed
148

149
150
151
152
153
154
	/* NaN - NaN != 0 */
	if (! mode_is_float(mode)) {
		/* a - a = 0 */
		if (a == b)
			return get_mode_null(mode);
	}
Michael Beck's avatar
Michael Beck committed
155

Michael Beck's avatar
Michael Beck committed
156
157
	ta = value_of(a);
	tb = value_of(b);
158

159
160
	if ((ta != tarval_bad) && (tb != tarval_bad))
		return tarval_sub(ta, tb, mode);
161

Michael Beck's avatar
Michael Beck committed
162
	return tarval_bad;
163
}  /* computed_value_Sub */
164

165
166
167
168
/**
 * Return the value of a Carry.
 * Special : a op 0, 0 op b
 */
Matthias Braun's avatar
Matthias Braun committed
169
static ir_tarval *computed_value_Carry(const ir_node *n)
170
{
Matthias Braun's avatar
Matthias Braun committed
171
172
173
174
175
	ir_node   *a  = get_binop_left(n);
	ir_node   *b  = get_binop_right(n);
	ir_mode   *m  = get_irn_mode(n);
	ir_tarval *ta = value_of(a);
	ir_tarval *tb = value_of(b);
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190

	if ((ta != tarval_bad) && (tb != tarval_bad)) {
		tarval_add(ta, tb);
		return tarval_carry() ? get_mode_one(m) : get_mode_null(m);
	} else {
		if (tarval_is_null(ta) || tarval_is_null(tb))
			return get_mode_null(m);
	}
	return tarval_bad;
}  /* computed_value_Carry */

/**
 * Return the value of a Borrow.
 * Special : a op 0
 */
Matthias Braun's avatar
Matthias Braun committed
191
static ir_tarval *computed_value_Borrow(const ir_node *n)
192
{
Matthias Braun's avatar
Matthias Braun committed
193
194
195
196
197
	ir_node   *a  = get_binop_left(n);
	ir_node   *b  = get_binop_right(n);
	ir_mode   *m  = get_irn_mode(n);
	ir_tarval *ta = value_of(a);
	ir_tarval *tb = value_of(b);
198
199
200
201
202
203
204
205
206

	if ((ta != tarval_bad) && (tb != tarval_bad)) {
		return tarval_cmp(ta, tb) == pn_Cmp_Lt ? get_mode_one(m) : get_mode_null(m);
	} else if (tarval_is_null(ta)) {
		return get_mode_null(m);
	}
	return tarval_bad;
}  /* computed_value_Borrow */

Michael Beck's avatar
Michael Beck committed
207
/**
208
 * Return the value of an unary Minus.
Michael Beck's avatar
Michael Beck committed
209
 */
Matthias Braun's avatar
Matthias Braun committed
210
static ir_tarval *computed_value_Minus(const ir_node *n)
211
{
Matthias Braun's avatar
Matthias Braun committed
212
213
	ir_node   *a  = get_Minus_op(n);
	ir_tarval *ta = value_of(a);
214

Michael Beck's avatar
Michael Beck committed
215
	if (ta != tarval_bad)
Michael Beck's avatar
Michael Beck committed
216
		return tarval_neg(ta);
217

Michael Beck's avatar
Michael Beck committed
218
	return tarval_bad;
219
}  /* computed_value_Minus */
220

Michael Beck's avatar
Michael Beck committed
221
/**
222
 * Return the value of a Mul.
Michael Beck's avatar
Michael Beck committed
223
 */
Matthias Braun's avatar
Matthias Braun committed
224
static ir_tarval *computed_value_Mul(const ir_node *n)
225
{
Matthias Braun's avatar
Matthias Braun committed
226
227
228
229
230
	ir_node   *a  = get_Mul_left(n);
	ir_node   *b  = get_Mul_right(n);
	ir_tarval *ta = value_of(a);
	ir_tarval *tb = value_of(b);
	ir_mode   *mode;
Michael Beck's avatar
Michael Beck committed
231

Michael Beck's avatar
Michael Beck committed
232
233
234
235
236
237
238
239
	mode = get_irn_mode(n);
	if (mode != get_irn_mode(a)) {
		/* n * n = 2n bit multiplication */
		ta = tarval_convert_to(ta, mode);
		tb = tarval_convert_to(tb, mode);
	}

	if (ta != tarval_bad && tb != tarval_bad) {
Michael Beck's avatar
Michael Beck committed
240
241
		return tarval_mul(ta, tb);
	} else {
242
243
244
245
246
247
248
249
		/* a * 0 != 0 if a == NaN or a == Inf */
		if (!mode_is_float(mode)) {
			/* a*0 = 0 or 0*b = 0 */
			if (ta == get_mode_null(mode))
				return ta;
			if (tb == get_mode_null(mode))
				return tb;
		}
Michael Beck's avatar
Michael Beck committed
250
251
	}
	return tarval_bad;
252
}  /* computed_value_Mul */
253

Michael Beck's avatar
Michael Beck committed
254
/**
255
 * Return the value of an And.
Michael Beck's avatar
Michael Beck committed
256
257
 * Special case: a & 0, 0 & b
 */
Matthias Braun's avatar
Matthias Braun committed
258
static ir_tarval *computed_value_And(const ir_node *n)
259
{
Matthias Braun's avatar
Matthias Braun committed
260
261
262
263
	ir_node   *a  = get_And_left(n);
	ir_node   *b  = get_And_right(n);
	ir_tarval *ta = value_of(a);
	ir_tarval *tb = value_of(b);
Michael Beck's avatar
Michael Beck committed
264
265
266
267

	if ((ta != tarval_bad) && (tb != tarval_bad)) {
		return tarval_and (ta, tb);
	} else {
268
269
		if (tarval_is_null(ta)) return ta;
		if (tarval_is_null(tb)) return tb;
Michael Beck's avatar
Michael Beck committed
270
271
	}
	return tarval_bad;
272
}  /* computed_value_And */
273

Michael Beck's avatar
Michael Beck committed
274
/**
275
 * Return the value of an Or.
Michael Beck's avatar
Michael Beck committed
276
277
 * Special case: a | 1...1, 1...1 | b
 */
Matthias Braun's avatar
Matthias Braun committed
278
static ir_tarval *computed_value_Or(const ir_node *n)
279
{
Matthias Braun's avatar
Matthias Braun committed
280
281
282
283
	ir_node   *a  = get_Or_left(n);
	ir_node   *b  = get_Or_right(n);
	ir_tarval *ta = value_of(a);
	ir_tarval *tb = value_of(b);
Michael Beck's avatar
Michael Beck committed
284
285
286
287

	if ((ta != tarval_bad) && (tb != tarval_bad)) {
		return tarval_or (ta, tb);
	} else {
288
289
		if (tarval_is_all_one(ta)) return ta;
		if (tarval_is_all_one(tb)) return tb;
Michael Beck's avatar
Michael Beck committed
290
291
	}
	return tarval_bad;
292
}  /* computed_value_Or */
293

Michael Beck's avatar
Michael Beck committed
294
/**
295
 * Return the value of an Eor.
Michael Beck's avatar
Michael Beck committed
296
 */
Matthias Braun's avatar
Matthias Braun committed
297
static ir_tarval *computed_value_Eor(const ir_node *n)
298
{
Michael Beck's avatar
Michael Beck committed
299
300
	ir_node *a = get_Eor_left(n);
	ir_node *b = get_Eor_right(n);
301

Matthias Braun's avatar
Matthias Braun committed
302
	ir_tarval *ta, *tb;
Michael Beck's avatar
Michael Beck committed
303

Michael Beck's avatar
Michael Beck committed
304
305
	if (a == b)
		return get_mode_null(get_irn_mode(n));
Michael Beck's avatar
Michael Beck committed
306

Michael Beck's avatar
Michael Beck committed
307
308
	ta = value_of(a);
	tb = value_of(b);
309

Michael Beck's avatar
Michael Beck committed
310
	if ((ta != tarval_bad) && (tb != tarval_bad)) {
311
		return tarval_eor(ta, tb);
Michael Beck's avatar
Michael Beck committed
312
313
	}
	return tarval_bad;
314
}  /* computed_value_Eor */
315

Michael Beck's avatar
Michael Beck committed
316
/**
317
 * Return the value of a Not.
Michael Beck's avatar
Michael Beck committed
318
 */
Matthias Braun's avatar
Matthias Braun committed
319
static ir_tarval *computed_value_Not(const ir_node *n)
320
{
Matthias Braun's avatar
Matthias Braun committed
321
322
	ir_node   *a  = get_Not_op(n);
	ir_tarval *ta = value_of(a);
323

Michael Beck's avatar
Michael Beck committed
324
325
	if (ta != tarval_bad)
		return tarval_not(ta);
326

Michael Beck's avatar
Michael Beck committed
327
	return tarval_bad;
328
}  /* computed_value_Not */
329

Michael Beck's avatar
Michael Beck committed
330
/**
331
 * Return the value of a Shl.
Michael Beck's avatar
Michael Beck committed
332
 */
Matthias Braun's avatar
Matthias Braun committed
333
static ir_tarval *computed_value_Shl(const ir_node *n)
334
{
Michael Beck's avatar
Michael Beck committed
335
336
	ir_node *a = get_Shl_left(n);
	ir_node *b = get_Shl_right(n);
337

Matthias Braun's avatar
Matthias Braun committed
338
339
	ir_tarval *ta = value_of(a);
	ir_tarval *tb = value_of(b);
340

Michael Beck's avatar
Michael Beck committed
341
	if ((ta != tarval_bad) && (tb != tarval_bad)) {
342
		return tarval_shl(ta, tb);
Michael Beck's avatar
Michael Beck committed
343
344
	}
	return tarval_bad;
345
}  /* computed_value_Shl */
346

Michael Beck's avatar
Michael Beck committed
347
/**
348
 * Return the value of a Shr.
Michael Beck's avatar
Michael Beck committed
349
 */
Matthias Braun's avatar
Matthias Braun committed
350
static ir_tarval *computed_value_Shr(const ir_node *n)
351
{
Michael Beck's avatar
Michael Beck committed
352
353
	ir_node *a = get_Shr_left(n);
	ir_node *b = get_Shr_right(n);
354

Matthias Braun's avatar
Matthias Braun committed
355
356
	ir_tarval *ta = value_of(a);
	ir_tarval *tb = value_of(b);
357

Michael Beck's avatar
Michael Beck committed
358
	if ((ta != tarval_bad) && (tb != tarval_bad)) {
359
		return tarval_shr(ta, tb);
Michael Beck's avatar
Michael Beck committed
360
361
	}
	return tarval_bad;
362
}  /* computed_value_Shr */
363

Michael Beck's avatar
Michael Beck committed
364
/**
365
 * Return the value of a Shrs.
Michael Beck's avatar
Michael Beck committed
366
 */
Matthias Braun's avatar
Matthias Braun committed
367
static ir_tarval *computed_value_Shrs(const ir_node *n)
368
{
Michael Beck's avatar
Michael Beck committed
369
370
	ir_node *a = get_Shrs_left(n);
	ir_node *b = get_Shrs_right(n);
371

Matthias Braun's avatar
Matthias Braun committed
372
373
	ir_tarval *ta = value_of(a);
	ir_tarval *tb = value_of(b);
374

Michael Beck's avatar
Michael Beck committed
375
	if ((ta != tarval_bad) && (tb != tarval_bad)) {
376
		return tarval_shrs(ta, tb);
Michael Beck's avatar
Michael Beck committed
377
378
	}
	return tarval_bad;
379
}  /* computed_value_Shrs */
380

Michael Beck's avatar
Michael Beck committed
381
/**
382
 * Return the value of a Rotl.
Michael Beck's avatar
Michael Beck committed
383
 */
Matthias Braun's avatar
Matthias Braun committed
384
static ir_tarval *computed_value_Rotl(const ir_node *n)
385
{
386
387
	ir_node *a = get_Rotl_left(n);
	ir_node *b = get_Rotl_right(n);
388

Matthias Braun's avatar
Matthias Braun committed
389
390
	ir_tarval *ta = value_of(a);
	ir_tarval *tb = value_of(b);
391

Michael Beck's avatar
Michael Beck committed
392
	if ((ta != tarval_bad) && (tb != tarval_bad)) {
393
		return tarval_rotl(ta, tb);
Michael Beck's avatar
Michael Beck committed
394
395
	}
	return tarval_bad;
396
}  /* computed_value_Rotl */
397

Michael Beck's avatar
Michael Beck committed
398
/**
399
 * Return the value of a Conv.
Michael Beck's avatar
Michael Beck committed
400
 */
Matthias Braun's avatar
Matthias Braun committed
401
static ir_tarval *computed_value_Conv(const ir_node *n)
402
{
Michael Beck's avatar
Michael Beck committed
403
	ir_node *a = get_Conv_op(n);
Matthias Braun's avatar
Matthias Braun committed
404
	ir_tarval *ta = value_of(a);
405

Michael Beck's avatar
Michael Beck committed
406
407
	if (ta != tarval_bad)
		return tarval_convert_to(ta, get_irn_mode(n));
408

Michael Beck's avatar
Michael Beck committed
409
	return tarval_bad;
410
}  /* computed_value_Conv */
411

Michael Beck's avatar
Michael Beck committed
412
413
414
415
/**
 * Calculate the value of a Mux: can be evaluated, if the
 * sel and the right input are known.
 */
Matthias Braun's avatar
Matthias Braun committed
416
static ir_tarval *computed_value_Mux(const ir_node *n)
417
{
Michael Beck's avatar
Michael Beck committed
418
	ir_node *sel = get_Mux_sel(n);
Matthias Braun's avatar
Matthias Braun committed
419
	ir_tarval *ts = value_of(sel);
Michael Beck's avatar
Michael Beck committed
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435

	if (ts == get_tarval_b_true()) {
		ir_node *v = get_Mux_true(n);
		return value_of(v);
	}
	else if (ts == get_tarval_b_false()) {
		ir_node *v = get_Mux_false(n);
		return value_of(v);
	}
	return tarval_bad;
}  /* computed_value_Mux */

/**
 * Calculate the value of a Confirm: can be evaluated,
 * if it has the form Confirm(x, '=', Const).
 */
Matthias Braun's avatar
Matthias Braun committed
436
static ir_tarval *computed_value_Confirm(const ir_node *n)
437
{
438
	if (get_Confirm_cmp(n) == pn_Cmp_Eq) {
Matthias Braun's avatar
Matthias Braun committed
439
		ir_tarval *tv = value_of(get_Confirm_bound(n));
440
441
		if (tv != tarval_bad)
			return tv;
Michael Beck's avatar
Michael Beck committed
442
443
444
445
	}
	return value_of(get_Confirm_value(n));
}  /* computed_value_Confirm */

446
/**
447
 * Return the value of a Proj(Cmp).
448
449
450
451
452
453
454
 *
 * This performs a first step of unreachable code elimination.
 * Proj can not be computed, but folding a Cmp above the Proj here is
 * not as wasteful as folding a Cmp into a Tuple of 16 Consts of which
 * only 1 is used.
 * There are several case where we can evaluate a Cmp node, see later.
 */
Matthias Braun's avatar
Matthias Braun committed
455
static ir_tarval *computed_value_Proj_Cmp(const ir_node *n)
456
{
457
458
459
460
461
	ir_node *cmp    = get_Proj_pred(n);
	ir_node *left   = get_Cmp_left(cmp);
	ir_node *right  = get_Cmp_right(cmp);
	pn_Cmp   pn_cmp = get_Proj_pn_cmp(n);
	ir_mode *mode   = get_irn_mode(left);
Matthias Braun's avatar
Matthias Braun committed
462
	ir_tarval *tv_l, *tv_r;
Michael Beck's avatar
Michael Beck committed
463
464
465
466
467

	/*
	 * BEWARE: a == a is NOT always True for floating Point values, as
	 * NaN != NaN is defined, so we must check this here.
	 */
468
	if (left == right && (!mode_is_float(mode) || pn_cmp == pn_Cmp_Lt ||  pn_cmp == pn_Cmp_Gt)) {
Michael Beck's avatar
Michael Beck committed
469
470
		/* This is a trick with the bits used for encoding the Cmp
		   Proj numbers, the following statement is not the same:
471
472
		return new_tarval_from_long(pn_cmp == pn_Cmp_Eq, mode_b) */
		return new_tarval_from_long(pn_cmp & pn_Cmp_Eq, mode_b);
Michael Beck's avatar
Michael Beck committed
473
	}
474
475
476
477
478
479
480
481
482
	tv_l = value_of(left);
	tv_r = value_of(right);

	if ((tv_l != tarval_bad) && (tv_r != tarval_bad)) {
		/*
		 * The predecessors of Cmp are target values.  We can evaluate
		 * the Cmp.
		 */
		pn_Cmp flags = tarval_cmp(tv_l, tv_r);
483
		if (flags != pn_Cmp_False) {
484
			return new_tarval_from_long (pn_cmp & flags, mode_b);
Michael Beck's avatar
Michael Beck committed
485
		}
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
	} else if (mode_is_int(mode)) {
		/* for integer values, we can check against MIN/MAX */
		pn_Cmp cmp_result;

		if (tv_l == get_mode_min(mode)) {
			/* MIN <=/> x.  This results in true/false. */
			if (pn_cmp == pn_Cmp_Le)
				return tarval_b_true;
			else if (pn_cmp == pn_Cmp_Gt)
				return tarval_b_false;
		} else if (tv_r == get_mode_min(mode)) {
			/* x >=/< MIN.  This results in true/false. */
			if (pn_cmp == pn_Cmp_Ge)
				return tarval_b_true;
			else if (pn_cmp == pn_Cmp_Lt)
				return tarval_b_false;
		} else if (tv_l == get_mode_max(mode)) {
			/* MAX >=/< x.  This results in true/false. */
			if (pn_cmp == pn_Cmp_Ge)
				return tarval_b_true;
			else if (pn_cmp == pn_Cmp_Lt)
				return tarval_b_false;
		} else if (tv_r == get_mode_max(mode)) {
			/* x <=/> MAX.  This results in true/false. */
			if (pn_cmp == pn_Cmp_Le)
				return tarval_b_true;
			else if (pn_cmp == pn_Cmp_Gt)
				return tarval_b_false;
514
515
		}

516
		cmp_result = vrp_cmp(left, right);
517
		if (cmp_result != pn_Cmp_False) {
518
			if (cmp_result == pn_Cmp_Lg) {
519
520
521
522
				if (pn_cmp == pn_Cmp_Eq) {
					return tarval_b_false;
				} else if (pn_cmp == pn_Cmp_Lg) {
					return tarval_b_true;
523
524
				}
			} else {
525
				return new_tarval_from_long(cmp_result & pn_cmp, mode_b);
526
			}
Michael Beck's avatar
Michael Beck committed
527
		}
528
529
530
531
532
533
534
535
536
537
538
539
540
541
	} else if (mode_is_reference(mode)) {
		/* pointer compare */
		ir_node *s_l = skip_Proj(left);
		ir_node *s_r = skip_Proj(right);

		if ((is_Alloc(s_l) && tarval_is_null(tv_r)) ||
			(tarval_is_null(tv_l) && is_Alloc(s_r))) {
			/*
			 * The predecessors are Allocs and (void*)(0) constants. In Firm Allocs never
			 * return NULL, they raise an exception. Therefore we can predict
			 * the Cmp result.
			 */
			return new_tarval_from_long(pn_cmp & pn_Cmp_Lg, mode_b);
		}
542
	}
543
	return computed_value_Cmp_Confirm(cmp, left, right, pn_cmp);
544
}  /* computed_value_Proj_Cmp */
545

546
547
548
549
/**
 * Calculate the value of an integer Div of two nodes.
 * Special case: 0 / b
 */
Matthias Braun's avatar
Matthias Braun committed
550
static ir_tarval *do_computed_value_Div(const ir_node *a, const ir_node *b)
551
{
Matthias Braun's avatar
Matthias Braun committed
552
553
	ir_tarval     *ta = value_of(a);
	ir_tarval     *tb;
Michael Beck's avatar
Michael Beck committed
554
	const ir_node *dummy;
555
556
557
558
559
560
561
562
563
564
565
566
567
568

	/* Compute c1 / c2 or 0 / a, a != 0 */
	if (tarval_is_null(ta) && value_not_zero(b, &dummy))
		return ta;  /* 0 / b == 0 */
	tb = value_of(b);
	if (ta != tarval_bad && tb != tarval_bad)
		return tarval_div(ta, tb);
	return tarval_bad;
}  /* do_computed_value_Div */

/**
 * Calculate the value of an integer Mod of two nodes.
 * Special case: a % 1
 */
Matthias Braun's avatar
Matthias Braun committed
569
static ir_tarval *do_computed_value_Mod(const ir_node *a, const ir_node *b)
570
{
Matthias Braun's avatar
Matthias Braun committed
571
572
	ir_tarval *ta = value_of(a);
	ir_tarval *tb = value_of(b);
573
574
575
576

	/* Compute a % 1 or c1 % c2 */
	if (tarval_is_one(tb))
		return get_mode_null(get_irn_mode(a));
Michael Beck's avatar
Michael Beck committed
577
	if (ta != tarval_bad && tb != tarval_bad)
578
579
580
581
		return tarval_mod(ta, tb);
	return tarval_bad;
}  /* do_computed_value_Mod */

Michael Beck's avatar
Michael Beck committed
582
583
584
/**
 * Return the value of a Proj(Div).
 */
Matthias Braun's avatar
Matthias Braun committed
585
static ir_tarval *computed_value_Proj_Div(const ir_node *n)
586
{
Michael Beck's avatar
Michael Beck committed
587
	long proj_nr = get_Proj_proj(n);
Michael Beck's avatar
Michael Beck committed
588

Michael Beck's avatar
Michael Beck committed
589
590
591
	if (proj_nr == pn_Div_res) {
		const ir_node *a = get_Proj_pred(n);
		return do_computed_value_Div(get_Div_left(a), get_Div_right(a));
Michael Beck's avatar
Michael Beck committed
592
593
	}
	return tarval_bad;
Michael Beck's avatar
Michael Beck committed
594
}  /* computed_value_Proj_Div */
595

596
/**
Michael Beck's avatar
Michael Beck committed
597
 * Return the value of a Proj(Mod).
598
 */
Matthias Braun's avatar
Matthias Braun committed
599
static ir_tarval *computed_value_Proj_Mod(const ir_node *n)
600
{
Michael Beck's avatar
Michael Beck committed
601
	long proj_nr = get_Proj_proj(n);
Michael Beck's avatar
Michael Beck committed
602

Michael Beck's avatar
Michael Beck committed
603
604
605
	if (proj_nr == pn_Mod_res) {
		const ir_node *a = get_Proj_pred(n);
		return do_computed_value_Mod(get_Mod_left(a), get_Mod_right(a));
Michael Beck's avatar
Michael Beck committed
606
607
	}
	return tarval_bad;
Michael Beck's avatar
Michael Beck committed
608
}  /* computed_value_Proj_Mod */
609

Michael Beck's avatar
Michael Beck committed
610
/**
Michael Beck's avatar
Michael Beck committed
611
 * Return the value of a Proj.
Michael Beck's avatar
Michael Beck committed
612
 */
Matthias Braun's avatar
Matthias Braun committed
613
static ir_tarval *computed_value_Proj(const ir_node *proj)
614
{
Michael Beck's avatar
Michael Beck committed
615
616
617
618
619
620
	ir_node *n = get_Proj_pred(proj);

	if (n->op->ops.computed_value_Proj != NULL)
		return n->op->ops.computed_value_Proj(proj);
	return tarval_bad;
}  /* computed_value_Proj */
Michael Beck's avatar
Michael Beck committed
621

622
623
624
/**
 * If the parameter n can be computed, return its value, else tarval_bad.
 * Performs constant folding.
625
626
 *
 * @param n  The node this should be evaluated
627
 */
Matthias Braun's avatar
Matthias Braun committed
628
ir_tarval *computed_value(const ir_node *n)
629
{
630
	vrp_attr *vrp = vrp_get_info(n);
631
	if (vrp && vrp->valid && tarval_cmp(vrp->bits_set, vrp->bits_not_set) == pn_Cmp_Eq) {
632
		return vrp->bits_set;
633
	}
Michael Beck's avatar
Michael Beck committed
634
635
636
	if (n->op->ops.computed_value)
		return n->op->ops.computed_value(n);
	return tarval_bad;
637
}  /* computed_value */
Christian Schäfer's avatar
Christian Schäfer committed
638

639
/**
640
 * Set the default computed_value evaluator in an ir_op_ops.
Michael Beck's avatar
Michael Beck committed
641
642
643
644
645
646
 *
 * @param code   the opcode for the default operation
 * @param ops    the operations initialized
 *
 * @return
 *    The operations.
647
 */
648
649
static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
{
Michael Beck's avatar
Michael Beck committed
650
651
652
653
654
655
656
#define CASE(a)                                        \
	case iro_##a:                                      \
		ops->computed_value      = computed_value_##a; \
		break
#define CASE_PROJ(a)                                        \
	case iro_##a:                                           \
		ops->computed_value_Proj = computed_value_Proj_##a; \
Michael Beck's avatar
Michael Beck committed
657
658
659
660
661
662
663
		break

	switch (code) {
	CASE(Const);
	CASE(SymConst);
	CASE(Add);
	CASE(Sub);
664
665
	CASE(Carry);
	CASE(Borrow);
Michael Beck's avatar
Michael Beck committed
666
667
668
669
670
671
672
673
674
	CASE(Minus);
	CASE(Mul);
	CASE(And);
	CASE(Or);
	CASE(Eor);
	CASE(Not);
	CASE(Shl);
	CASE(Shr);
	CASE(Shrs);
675
	CASE(Rotl);
Michael Beck's avatar
Michael Beck committed
676
677
678
	CASE(Conv);
	CASE(Mux);
	CASE(Confirm);
Michael Beck's avatar
Michael Beck committed
679
680
681
682
	CASE_PROJ(Cmp);
	CASE_PROJ(Div);
	CASE_PROJ(Mod);
	CASE(Proj);
Michael Beck's avatar
Michael Beck committed
683
	default:
684
685
		/* leave NULL */
		break;
Michael Beck's avatar
Michael Beck committed
686
687
688
	}

	return ops;
Michael Beck's avatar
Michael Beck committed
689
#undef CASE_PROJ
690
#undef CASE
691
}  /* firm_set_default_computed_value */
Christian Schäfer's avatar
Christian Schäfer committed
692

Michael Beck's avatar
Michael Beck committed
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
/**
 * Returns a equivalent block for another block.
 * If the block has only one predecessor, this is
 * the equivalent one. If the only predecessor of a block is
 * the block itself, this is a dead block.
 *
 * If both predecessors of a block are the branches of a binary
 * Cond, the equivalent block is Cond's block.
 *
 * If all predecessors of a block are bad or lies in a dead
 * block, the current block is dead as well.
 *
 * Note, that blocks are NEVER turned into Bad's, instead
 * the dead_block flag is set. So, never test for is_Bad(block),
 * always use is_dead_Block(block).
 */
709
710
static ir_node *equivalent_node_Block(ir_node *n)
{
Michael Beck's avatar
Michael Beck committed
711
	ir_node *oldn = n;
712
	int     n_preds;
713
	ir_graph *irg;
Michael Beck's avatar
Michael Beck committed
714

715
	/* don't optimize dead or labeled blocks */
716
	if (is_Block_dead(n) || has_Block_entity(n))
717
718
719
720
721
722
		return n;

	n_preds = get_Block_n_cfgpreds(n);

	/* The Block constructor does not call optimize, but mature_immBlock()
	   calls the optimization. */
Michael Beck's avatar
Michael Beck committed
723
724
	assert(get_Block_matured(n));

725
726
	irg = get_irn_irg(n);

Michael Beck's avatar
Michael Beck committed
727
728
729
730
731
732
	/* Straightening: a single entry Block following a single exit Block
	   can be merged, if it is not the Start block. */
	/* !!! Beware, all Phi-nodes of n must have been optimized away.
	   This should be true, as the block is matured before optimize is called.
	   But what about Phi-cycles with the Phi0/Id that could not be resolved?
	   Remaining Phi nodes are just Ids. */
Michael Beck's avatar
Michael Beck committed
733
734
735
736
737
738
739
740
741
	if (n_preds == 1) {
		ir_node *pred = skip_Proj(get_Block_cfgpred(n, 0));

		if (is_Jmp(pred)) {
			ir_node *predblock = get_nodes_block(pred);
			if (predblock == oldn) {
				/* Jmp jumps into the block it is in -- deal self cycle. */
				n = set_Block_dead(n);
				DBG_OPT_DEAD_BLOCK(oldn, n);
742
			} else {
Michael Beck's avatar
Michael Beck committed
743
744
745
746
747
748
749
750
751
752
				n = predblock;
				DBG_OPT_STG(oldn, n);
			}
		} else if (is_Cond(pred)) {
			ir_node *predblock = get_nodes_block(pred);
			if (predblock == oldn) {
				/* Jmp jumps into the block it is in -- deal self cycle. */
				n = set_Block_dead(n);
				DBG_OPT_DEAD_BLOCK(oldn, n);
			}
Michael Beck's avatar
Michael Beck committed
753
		}
754
	} else if (n_preds == 2) {
Michael Beck's avatar
Michael Beck committed
755
756
757
758
759
760
761
		/* Test whether Cond jumps twice to this block
		 * The more general case which more than 2 predecessors is handles
		 * in optimize_cf(), we handle only this special case for speed here.
		 */
		ir_node *a = get_Block_cfgpred(n, 0);
		ir_node *b = get_Block_cfgpred(n, 1);

Michael Beck's avatar
Michael Beck committed
762
763
764
765
766
767
768
769
770
771
		if (is_Proj(a) && is_Proj(b)) {
			ir_node *cond = get_Proj_pred(a);

		    if (cond == get_Proj_pred(b) && is_Cond(cond) &&
		        get_irn_mode(get_Cond_selector(cond)) == mode_b) {
				/* Also a single entry Block following a single exit Block.  Phis have
				   twice the same operand and will be optimized away. */
				n = get_nodes_block(cond);
				DBG_OPT_IFSIM1(oldn, a, b, n);
			}
Michael Beck's avatar
Michael Beck committed
772
773
		}
	} else if (get_opt_unreachable_code() &&
774
775
	           (n != get_irg_start_block(irg)) &&
	           (n != get_irg_end_block(irg))) {
Michael Beck's avatar
Michael Beck committed
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
		int i;

		/* If all inputs are dead, this block is dead too, except if it is
		   the start or end block.  This is one step of unreachable code
		   elimination */
		for (i = get_Block_n_cfgpreds(n) - 1; i >= 0; --i) {
			ir_node *pred = get_Block_cfgpred(n, i);
			ir_node *pred_blk;

			if (is_Bad(pred)) continue;
			pred_blk = get_nodes_block(skip_Proj(pred));

			if (is_Block_dead(pred_blk)) continue;

			if (pred_blk != n) {
				/* really found a living input */
				break;
			}
		}
		if (i < 0) {
			n = set_Block_dead(n);
			DBG_OPT_DEAD_BLOCK(oldn, n);
		}
	}

	return n;
802
}  /* equivalent_node_Block */
803

804
805
/**
 * Returns a equivalent node for a Jmp, a Bad :-)
806
 * Of course this only happens if the Block of the Jmp is dead.
807
 */
808
809
static ir_node *equivalent_node_Jmp(ir_node *n)
{
Michael Beck's avatar
Michael Beck committed
810
	ir_node *oldn = n;
811

Michael Beck's avatar
Michael Beck committed
812
813
	/* unreachable code elimination */
	if (is_Block_dead(get_nodes_block(n))) {
814
815
		ir_graph *irg = get_irn_irg(n);
		n = get_irg_bad(irg);
Michael Beck's avatar
Michael Beck committed
816
817
		DBG_OPT_DEAD_BLOCK(oldn, n);
	}
Michael Beck's avatar
Michael Beck committed
818
	return n;
819
}  /* equivalent_node_Jmp */
820

821
/** Raise is handled in the same way as Jmp. */
822
823
824
#define equivalent_node_Raise   equivalent_node_Jmp


825
826
/* We do not evaluate Cond here as we replace it by a new node, a Jmp.
   See transform_node_Proj_Cond(). */
827

828
/**
829
 * Optimize operations that are commutative and have neutral 0,
830
 * so a op 0 = 0 op a = a.
831
 */
832
833
static ir_node *equivalent_node_neutral_zero(ir_node *n)
{
Michael Beck's avatar
Michael Beck committed
834
835
836
837
838
	ir_node *oldn = n;

	ir_node *a = get_binop_left(n);
	ir_node *b = get_binop_right(n);

Matthias Braun's avatar
Matthias Braun committed
839
	ir_tarval *tv;
Michael Beck's avatar
Michael Beck committed
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
	ir_node *on;

	/* After running compute_node there is only one constant predecessor.
	   Find this predecessors value and remember the other node: */
	if ((tv = value_of(a)) != tarval_bad) {
		on = b;
	} else if ((tv = value_of(b)) != tarval_bad) {
		on = a;
	} else
		return n;

	/* If this predecessors constant value is zero, the operation is
	 * unnecessary. Remove it.
	 *
	 * Beware: If n is a Add, the mode of on and n might be different
	 * which happens in this rare construction: NULL + 3.
	 * Then, a Conv would be needed which we cannot include here.
	 */
858
859
	if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
		n = on;
Michael Beck's avatar
Michael Beck committed
860

861
		DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
Michael Beck's avatar
Michael Beck committed
862
863
864
	}

	return n;
865
}  /* equivalent_node_neutral_zero */
866

867
868
869
/**
 * Eor is commutative and has neutral 0.
 */
870
871
static ir_node *equivalent_node_Eor(ir_node *n)
{
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
	ir_node *oldn = n;
	ir_node *a;
	ir_node *b;

	n = equivalent_node_neutral_zero(n);
	if (n != oldn) return n;

	a = get_Eor_left(n);
	b = get_Eor_right(n);

	if (is_Eor(a)) {
		ir_node *aa = get_Eor_left(a);
		ir_node *ab = get_Eor_right(a);

		if (aa == b) {
			/* (a ^ b) ^ a -> b */
			n = ab;
Michael Beck's avatar
Michael Beck committed
889
			DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
Michael Beck's avatar
Michael Beck committed
890
			return n;
891
892
893
		} else if (ab == b) {
			/* (a ^ b) ^ b -> a */
			n = aa;
Michael Beck's avatar
Michael Beck committed
894
			DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
Michael Beck's avatar
Michael Beck committed
895
			return n;
896
		}
897
898
	}
	if (is_Eor(b)) {
899
900
901
902
903
904
		ir_node *ba = get_Eor_left(b);
		ir_node *bb = get_Eor_right(b);

		if (ba == a) {
			/* a ^ (a ^ b) -> b */
			n = bb;
Michael Beck's avatar
Michael Beck committed
905
			DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
Michael Beck's avatar
Michael Beck committed
906
			return n;
907
908
909
		} else if (bb == a) {
			/* a ^ (b ^ a) -> b */
			n = ba;
Michael Beck's avatar
Michael Beck committed
910
			DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
Michael Beck's avatar
Michael Beck committed
911
			return n;
912
913
914
915
		}
	}
	return n;
}
916

917
918
919
920
921
/*
 * Optimize a - 0 and (a - x) + x (for modes with wrap-around).
 *
 * The second one looks strange, but this construct
 * is used heavily in the LCC sources :-).
922
923
924
 *
 * Beware: The Mode of an Add may be different than the mode of its
 * predecessors, so we could not return a predecessors in all cases.
925
 */
926
927
static ir_node *equivalent_node_Add(ir_node *n)
{
Michael Beck's avatar
Michael Beck committed
928
929
930
931
932
933
934
935
	ir_node *oldn = n;
	ir_node *left, *right;
	ir_mode *mode = get_irn_mode(n);

	n = equivalent_node_neutral_zero(n);
	if (n != oldn)
		return n;

936
	/* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
937
938
939
940
941
	if (mode_is_float(mode)) {
		ir_graph *irg = get_irn_irg(n);
		if (get_irg_fp_model(irg) & fp_strict_algebraic)
			return n;
	}
942

Michael Beck's avatar
Michael Beck committed
943
944
945
	left  = get_Add_left(n);
	right = get_Add_right(n);

946
	if (is_Sub(left)) {
Michael Beck's avatar
Michael Beck committed
947
948
949
950
951
952
953
954
955
956
		if (get_Sub_right(left) == right) {
			/* (a - x) + x */

			n = get_Sub_left(left);
			if (mode == get_irn_mode(n)) {
				DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
				return n;
			}
		}
	}
957
	if (is_Sub(right)) {
Michael Beck's avatar
Michael Beck committed
958
959
960
961
962
963
964
965
966
967
968
		if (get_Sub_right(right) == left) {
			/* x + (a - x) */

			n = get_Sub_left(right);
			if (mode == get_irn_mode(n)) {
				DBG_OPT_ALGSIM1(oldn, left, right, n, FS_OPT_ADD_SUB);
				return n;
			}
		}
	}
	return n;
969
}  /* equivalent_node_Add */
970

971
/**
972
973
 * optimize operations that are not commutative but have neutral 0 on left,
 * so a op 0 = a.
974
 */
975
976
static ir_node *equivalent_node_left_zero(ir_node *n)
{
Michael Beck's avatar
Michael Beck committed
977
	ir_node *oldn = n;
978

Matthias Braun's avatar
Matthias Braun committed
979
980
981
	ir_node   *a  = get_binop_left(n);
	ir_node   *b  = get_binop_right(n);
	ir_tarval *tb = value_of(b);
982

983
	if (tarval_is_null(tb)) {
Michael Beck's avatar
Michael Beck committed
984
		n = a;
985

Michael Beck's avatar
Michael Beck committed
986
987
988
		DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
	}
	return n;
989
}  /* equivalent_node_left_zero */