iropt.c 111 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
/*
 * Copyright (C) 1995-2007 University of Karlsruhe.  All right reserved.
 *
 * 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
 */
Boris Boesler's avatar
added    
Boris Boesler committed
26
#ifdef HAVE_CONFIG_H
Michael Beck's avatar
Michael Beck committed
27
28
29
30
31
# include "config.h"
#endif

#ifdef HAVE_STRING_H
#include <string.h>
Boris Boesler's avatar
added    
Boris Boesler committed
32
33
#endif

Michael Beck's avatar
Michael Beck committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#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"
#include "irvrfy.h"
#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"
#include "archop.h"
#include "opt_polymorphy.h"
#include "opt_confirms.h"
Michael Beck's avatar
Michael Beck committed
52
#include "irtools.h"
53
#include "xmalloc.h"
Christian Schäfer's avatar
Christian Schäfer committed
54

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

58
/**
59
 * Return the value of a Constant.
60
 */
61
static tarval *computed_value_Const(ir_node *n) {
Michael Beck's avatar
Michael Beck committed
62
	return get_Const_tarval(n);
63
}  /* computed_value_Const */
Christian Schäfer's avatar
Christian Schäfer committed
64

65
/**
Michael Beck's avatar
Michael Beck committed
66
 * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
67
 */
68
static tarval *computed_value_SymConst(ir_node *n) {
Michael Beck's avatar
Michael Beck committed
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
	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;
93
}  /* computed_value_SymConst */
Christian Schäfer's avatar
Christian Schäfer committed
94

Michael Beck's avatar
Michael Beck committed
95
/**
96
 * Return the value of an Add.
Michael Beck's avatar
Michael Beck committed
97
 */
98
static tarval *computed_value_Add(ir_node *n) {
Michael Beck's avatar
Michael Beck committed
99
100
	ir_node *a = get_Add_left(n);
	ir_node *b = get_Add_right(n);
Christian Schäfer's avatar
Christian Schäfer committed
101

Michael Beck's avatar
Michael Beck committed
102
103
	tarval *ta = value_of(a);
	tarval *tb = value_of(b);
104

Michael Beck's avatar
Michael Beck committed
105
106
	if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
		return tarval_add(ta, tb);
107

Michael Beck's avatar
Michael Beck committed
108
	return tarval_bad;
109
}  /* computed_value_Add */
Christian Schäfer's avatar
Christian Schäfer committed
110

Michael Beck's avatar
Michael Beck committed
111
/**
112
 * Return the value of a Sub.
Michael Beck's avatar
Michael Beck committed
113
114
 * Special case: a - a
 */
115
static tarval *computed_value_Sub(ir_node *n) {
Michael Beck's avatar
Michael Beck committed
116
117
118
119
	ir_node *a = get_Sub_left(n);
	ir_node *b = get_Sub_right(n);
	tarval *ta;
	tarval *tb;
Christian Schäfer's avatar
Christian Schäfer committed
120

Michael Beck's avatar
Michael Beck committed
121
122
123
	/* a - a */
	if (a == b && !is_Bad(a))
		return get_mode_null(get_irn_mode(n));
Michael Beck's avatar
Michael Beck committed
124

Michael Beck's avatar
Michael Beck committed
125
126
	ta = value_of(a);
	tb = value_of(b);
127

Michael Beck's avatar
Michael Beck committed
128
129
	if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b)))
		return tarval_sub(ta, tb);
130

Michael Beck's avatar
Michael Beck committed
131
	return tarval_bad;
132
}  /* computed_value_Sub */
133

134
/**
135
 * Return the value of a Carry.
136
137
 * Special : a op 0, 0 op b
 */
138
static tarval *computed_value_Carry(ir_node *n) {
Michael Beck's avatar
Michael Beck committed
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
	ir_node *a = get_binop_left(n);
	ir_node *b = get_binop_right(n);
	ir_mode *m = get_irn_mode(n);

	tarval *ta = value_of(a);
	tarval *tb = value_of(b);

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

/**
158
 * Return the value of a Borrow.
159
160
 * Special : a op 0
 */
161
static tarval *computed_value_Borrow(ir_node *n) {
Michael Beck's avatar
Michael Beck committed
162
163
164
165
166
167
168
169
170
171
172
173
174
	ir_node *a = get_binop_left(n);
	ir_node *b = get_binop_right(n);
	ir_mode *m = get_irn_mode(n);

	tarval *ta = value_of(a);
	tarval *tb = value_of(b);

	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 (classify_tarval(ta) == TV_CLASSIFY_NULL) {
		return get_mode_null(m);
	}
	return tarval_bad;
175
}  /* computed_value_Borrow */
176

Michael Beck's avatar
Michael Beck committed
177
/**
178
 * Return the value of an unary Minus.
Michael Beck's avatar
Michael Beck committed
179
 */
180
static tarval *computed_value_Minus(ir_node *n) {
Michael Beck's avatar
Michael Beck committed
181
182
	ir_node *a = get_Minus_op(n);
	tarval *ta = value_of(a);
183

Michael Beck's avatar
Michael Beck committed
184
185
	if ((ta != tarval_bad) && mode_is_signed(get_irn_mode(a)))
		return tarval_neg(ta);
186

Michael Beck's avatar
Michael Beck committed
187
	return tarval_bad;
188
}  /* computed_value_Minus */
189

Michael Beck's avatar
Michael Beck committed
190
/**
191
 * Return the value of a Mul.
Michael Beck's avatar
Michael Beck committed
192
 */
193
static tarval *computed_value_Mul(ir_node *n) {
Michael Beck's avatar
Michael Beck committed
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
	ir_node *a = get_Mul_left(n);
	ir_node *b = get_Mul_right(n);

	tarval *ta = value_of(a);
	tarval *tb = value_of(b);

	if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
		return tarval_mul(ta, tb);
	} else {
	/* a*0 = 0 or 0*b = 0:
	   calls computed_value recursive and returns the 0 with proper
	   mode. */
		if ((ta != tarval_bad) && (ta == get_mode_null(get_tarval_mode(ta))))
			return ta;
		if ((tb != tarval_bad) && (tb == get_mode_null(get_tarval_mode(tb))))
			return tb;
	}
	return tarval_bad;
212
}  /* computed_value_Mul */
213

Michael Beck's avatar
Michael Beck committed
214
/**
215
 * Return the value of a floating point Quot.
Michael Beck's avatar
Michael Beck committed
216
 */
217
static tarval *computed_value_Quot(ir_node *n) {
Michael Beck's avatar
Michael Beck committed
218
219
220
221
222
223
224
225
226
227
228
229
	ir_node *a = get_Quot_left(n);
	ir_node *b = get_Quot_right(n);

	tarval *ta = value_of(a);
	tarval *tb = value_of(b);

	/* This was missing in original implementation. Why? */
	if ((ta != tarval_bad) && (tb != tarval_bad) && (get_irn_mode(a) == get_irn_mode(b))) {
		if (tb != get_mode_null(get_tarval_mode(tb)))   /* div by zero: return tarval_bad */
			return tarval_quo(ta, tb);
	}
	return tarval_bad;
230
}  /* computed_value_Quot */
231

Michael Beck's avatar
Michael Beck committed
232
/**
233
 * Calculate the value of an integer Div of two nodes.
Michael Beck's avatar
Michael Beck committed
234
235
 * Special case: 0 / b
 */
236
static tarval *do_computed_value_Div(ir_node *a, ir_node *b) {
Michael Beck's avatar
Michael Beck committed
237
238
239
240
241
242
243
244
245
246
247
	tarval *ta = value_of(a);
	tarval *tb = value_of(b);

	/* Compute c1 / c2 or 0 / a, a != 0 */
	if (ta != tarval_bad) {
		if ((tb != tarval_bad) && (tb != get_mode_null(get_irn_mode(b))))   /* div by zero: return tarval_bad */
			return tarval_div(ta, tb);
		else if (ta == get_mode_null(get_tarval_mode(ta)))  /* 0 / b == 0 */
			return ta;
	}
	return tarval_bad;
248
}  /* do_computed_value_Div */
249

Michael Beck's avatar
Michael Beck committed
250
/**
251
 * Return the value of an integer Div.
Michael Beck's avatar
Michael Beck committed
252
 */
253
static tarval *computed_value_Div(ir_node *n) {
Michael Beck's avatar
Michael Beck committed
254
	return do_computed_value_Div(get_Div_left(n), get_Div_right(n));
255
}  /* computed_value_Div */
256

Michael Beck's avatar
Michael Beck committed
257
/**
258
 * Calculate the value of an integer Mod of two nodes.
Michael Beck's avatar
Michael Beck committed
259
260
 * Special case: a % 1
 */
261
static tarval *do_computed_value_Mod(ir_node *a, ir_node *b) {
Michael Beck's avatar
Michael Beck committed
262
263
264
265
266
267
268
269
270
271
272
	tarval *ta = value_of(a);
	tarval *tb = value_of(b);

	/* Compute c1 % c2 or a % 1 */
	if (tb != tarval_bad) {
		if ((ta != tarval_bad) && (tb != get_mode_null(get_tarval_mode(tb))))   /* div by zero: return tarval_bad */
			return tarval_mod(ta, tb);
		else if (tb == get_mode_one(get_tarval_mode(tb)))    /* x mod 1 == 0 */
			return get_mode_null(get_irn_mode(a));
	}
	return tarval_bad;
273
}  /* do_computed_value_Mod */
274

Michael Beck's avatar
Michael Beck committed
275
/**
276
 * Return the value of an integer Mod.
Michael Beck's avatar
Michael Beck committed
277
 */
278
static tarval *computed_value_Mod(ir_node *n) {
Michael Beck's avatar
Michael Beck committed
279
	return do_computed_value_Mod(get_Mod_left(n), get_Mod_right(n));
280
}  /* computed_value_Mod */
Michael Beck's avatar
Michael Beck committed
281
282

/**
283
 * Return the value of an Abs.
Michael Beck's avatar
Michael Beck committed
284
 */
285
static tarval *computed_value_Abs(ir_node *n) {
Michael Beck's avatar
Michael Beck committed
286
287
	ir_node *a = get_Abs_op(n);
	tarval *ta = value_of(a);
288

Michael Beck's avatar
Michael Beck committed
289
290
	if (ta != tarval_bad)
		return tarval_abs(ta);
291

Michael Beck's avatar
Michael Beck committed
292
	return tarval_bad;
293
}  /* computed_value_Abs */
294

Michael Beck's avatar
Michael Beck committed
295
/**
296
 * Return the value of an And.
Michael Beck's avatar
Michael Beck committed
297
298
 * Special case: a & 0, 0 & b
 */
299
static tarval *computed_value_And(ir_node *n) {
Michael Beck's avatar
Michael Beck committed
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
	ir_node *a = get_And_left(n);
	ir_node *b = get_And_right(n);

	tarval *ta = value_of(a);
	tarval *tb = value_of(b);

	if ((ta != tarval_bad) && (tb != tarval_bad)) {
		return tarval_and (ta, tb);
	} else {
		tarval *v;

		if (   (classify_tarval ((v = ta)) == TV_CLASSIFY_NULL)
			|| (classify_tarval ((v = tb)) == TV_CLASSIFY_NULL)) {
			return v;
		}
	}
	return tarval_bad;
317
}  /* computed_value_And */
318

Michael Beck's avatar
Michael Beck committed
319
/**
320
 * Return the value of an Or.
Michael Beck's avatar
Michael Beck committed
321
322
 * Special case: a | 1...1, 1...1 | b
 */
323
static tarval *computed_value_Or(ir_node *n) {
Michael Beck's avatar
Michael Beck committed
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
	ir_node *a = get_Or_left(n);
	ir_node *b = get_Or_right(n);

	tarval *ta = value_of(a);
	tarval *tb = value_of(b);

	if ((ta != tarval_bad) && (tb != tarval_bad)) {
		return tarval_or (ta, tb);
	} else {
		tarval *v;
		if (   (classify_tarval ((v = ta)) == TV_CLASSIFY_ALL_ONE)
			|| (classify_tarval ((v = tb)) == TV_CLASSIFY_ALL_ONE)) {
			return v;
		}
	}
	return tarval_bad;
340
}  /* computed_value_Or */
341

Michael Beck's avatar
Michael Beck committed
342
/**
343
 * Return the value of an Eor.
Michael Beck's avatar
Michael Beck committed
344
 */
345
static tarval *computed_value_Eor(ir_node *n) {
Michael Beck's avatar
Michael Beck committed
346
347
	ir_node *a = get_Eor_left(n);
	ir_node *b = get_Eor_right(n);
348

Michael Beck's avatar
Michael Beck committed
349
	tarval *ta, *tb;
Michael Beck's avatar
Michael Beck committed
350

Michael Beck's avatar
Michael Beck committed
351
352
	if (a == b)
		return get_mode_null(get_irn_mode(n));
Michael Beck's avatar
Michael Beck committed
353

Michael Beck's avatar
Michael Beck committed
354
355
	ta = value_of(a);
	tb = value_of(b);
356

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

Michael Beck's avatar
Michael Beck committed
363
/**
364
 * Return the value of a Not.
Michael Beck's avatar
Michael Beck committed
365
 */
366
static tarval *computed_value_Not(ir_node *n) {
Michael Beck's avatar
Michael Beck committed
367
368
	ir_node *a = get_Not_op(n);
	tarval *ta = value_of(a);
369

Michael Beck's avatar
Michael Beck committed
370
371
	if (ta != tarval_bad)
		return tarval_not(ta);
372

Michael Beck's avatar
Michael Beck committed
373
	return tarval_bad;
374
}  /* computed_value_Not */
375

Michael Beck's avatar
Michael Beck committed
376
/**
377
 * Return the value of a Shl.
Michael Beck's avatar
Michael Beck committed
378
 */
379
static tarval *computed_value_Shl(ir_node *n) {
Michael Beck's avatar
Michael Beck committed
380
381
	ir_node *a = get_Shl_left(n);
	ir_node *b = get_Shl_right(n);
382

Michael Beck's avatar
Michael Beck committed
383
384
	tarval *ta = value_of(a);
	tarval *tb = value_of(b);
385

Michael Beck's avatar
Michael Beck committed
386
387
388
389
	if ((ta != tarval_bad) && (tb != tarval_bad)) {
		return tarval_shl (ta, tb);
	}
	return tarval_bad;
390
}  /* computed_value_Shl */
391

Michael Beck's avatar
Michael Beck committed
392
/**
393
 * Return the value of a Shr.
Michael Beck's avatar
Michael Beck committed
394
 */
395
static tarval *computed_value_Shr(ir_node *n) {
Michael Beck's avatar
Michael Beck committed
396
397
	ir_node *a = get_Shr_left(n);
	ir_node *b = get_Shr_right(n);
398

Michael Beck's avatar
Michael Beck committed
399
400
	tarval *ta = value_of(a);
	tarval *tb = value_of(b);
401

Michael Beck's avatar
Michael Beck committed
402
403
404
405
	if ((ta != tarval_bad) && (tb != tarval_bad)) {
		return tarval_shr (ta, tb);
	}
	return tarval_bad;
406
}  /* computed_value_Shr */
407

Michael Beck's avatar
Michael Beck committed
408
/**
409
 * Return the value of a Shrs.
Michael Beck's avatar
Michael Beck committed
410
 */
411
static tarval *computed_value_Shrs(ir_node *n) {
Michael Beck's avatar
Michael Beck committed
412
413
	ir_node *a = get_Shrs_left(n);
	ir_node *b = get_Shrs_right(n);
414

Michael Beck's avatar
Michael Beck committed
415
416
	tarval *ta = value_of(a);
	tarval *tb = value_of(b);
417

Michael Beck's avatar
Michael Beck committed
418
419
420
421
	if ((ta != tarval_bad) && (tb != tarval_bad)) {
		return tarval_shrs (ta, tb);
	}
	return tarval_bad;
422
}  /* computed_value_Shrs */
423

Michael Beck's avatar
Michael Beck committed
424
/**
425
 * Return the value of a Rot.
Michael Beck's avatar
Michael Beck committed
426
 */
Michael Beck's avatar
Michael Beck committed
427
428
429
static tarval *computed_value_Rot(ir_node *n) {
	ir_node *a = get_Rot_left(n);
	ir_node *b = get_Rot_right(n);
430

Michael Beck's avatar
Michael Beck committed
431
432
	tarval *ta = value_of(a);
	tarval *tb = value_of(b);
433

Michael Beck's avatar
Michael Beck committed
434
435
436
437
	if ((ta != tarval_bad) && (tb != tarval_bad)) {
		return tarval_rot (ta, tb);
	}
	return tarval_bad;
438
}  /* computed_value_Rot */
439

Michael Beck's avatar
Michael Beck committed
440
/**
441
 * Return the value of a Conv.
Michael Beck's avatar
Michael Beck committed
442
 */
Michael Beck's avatar
Michael Beck committed
443
444
445
static tarval *computed_value_Conv(ir_node *n) {
	ir_node *a = get_Conv_op(n);
	tarval *ta = value_of(a);
446

Michael Beck's avatar
Michael Beck committed
447
448
	if (ta != tarval_bad)
		return tarval_convert_to(ta, get_irn_mode(n));
449

Michael Beck's avatar
Michael Beck committed
450
	return tarval_bad;
451
}  /* computed_value_Conv */
452

453
/**
454
 * Return the value of a Proj(Cmp).
455
456
457
458
459
460
461
 *
 * 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.
 */
Michael Beck's avatar
Michael Beck committed
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
static tarval *computed_value_Proj_Cmp(ir_node *n) {
	ir_node *a   = get_Proj_pred(n);
	ir_node *aa  = get_Cmp_left(a);
	ir_node *ab  = get_Cmp_right(a);
	long proj_nr = get_Proj_proj(n);

	/*
	 * BEWARE: a == a is NOT always True for floating Point values, as
	 * NaN != NaN is defined, so we must check this here.
	 */
	if (aa == ab && (
		!mode_is_float(get_irn_mode(aa)) || proj_nr == pn_Cmp_Lt ||  proj_nr == pn_Cmp_Gt)
		) { /* 1.: */

		/* This is a trick with the bits used for encoding the Cmp
		   Proj numbers, the following statement is not the same:
		return new_tarval_from_long (proj_nr == pn_Cmp_Eq, mode_b) */
		return new_tarval_from_long (proj_nr & pn_Cmp_Eq, mode_b);
	}
	else {
		tarval *taa = value_of(aa);
		tarval *tab = value_of(ab);
		ir_mode *mode = get_irn_mode(aa);

		/*
		 * The predecessors of Cmp are target values.  We can evaluate
		 * the Cmp.
		 */
		if ((taa != tarval_bad) && (tab != tarval_bad)) {
			/* strange checks... */
			pn_Cmp flags = tarval_cmp(taa, tab);
			if (flags != pn_Cmp_False) {
				return new_tarval_from_long (proj_nr & flags, mode_b);
			}
		}
		/* for integer values, we can check against MIN/MAX */
		else if (mode_is_int(mode)) {
			/* MIN <=/> x.  This results in true/false. */
			if (taa == get_mode_min(mode)) {
				/* a compare with the MIN value */
				if (proj_nr == pn_Cmp_Le)
					return get_tarval_b_true();
				else if (proj_nr == pn_Cmp_Gt)
					return get_tarval_b_false();
			}
			/* x >=/< MIN.  This results in true/false. */
			else
				if (tab == get_mode_min(mode)) {
					/* a compare with the MIN value */
					if (proj_nr == pn_Cmp_Ge)
						return get_tarval_b_true();
					else if (proj_nr == pn_Cmp_Lt)
						return get_tarval_b_false();
				}
				/* MAX >=/< x.  This results in true/false. */
				else if (taa == get_mode_max(mode)) {
					if (proj_nr == pn_Cmp_Ge)
						return get_tarval_b_true();
					else if (proj_nr == pn_Cmp_Lt)
						return get_tarval_b_false();
				}
				/* x <=/> MAX.  This results in true/false. */
				else if (tab == get_mode_max(mode)) {
					if (proj_nr == pn_Cmp_Le)
						return get_tarval_b_true();
					else if (proj_nr == pn_Cmp_Gt)
						return get_tarval_b_false();
				}
		}
		/*
		 * The predecessors are Allocs or (void*)(0) constants.  Allocs never
		 * return NULL, they raise an exception.   Therefore we can predict
		 * the Cmp result.
		 */
		else {
			ir_node *aaa = skip_Id(skip_Proj(aa));
			ir_node *aba = skip_Id(skip_Proj(ab));

			if (   (   (/* aa is ProjP and aaa is Alloc */
			               (get_irn_op(aa) == op_Proj)
			            && (mode_is_reference(get_irn_mode(aa)))
			            && (get_irn_op(aaa) == op_Alloc))
			        && (   (/* ab is NULL */
			                   (get_irn_op(ab) == op_Const)
			                && (mode_is_reference(get_irn_mode(ab)))
			                && (get_Const_tarval(ab) == get_mode_null(get_irn_mode(ab))))
			            || (/* ab is other Alloc */
			                   (get_irn_op(ab) == op_Proj)
			                && (mode_is_reference(get_irn_mode(ab)))
			                && (get_irn_op(aba) == op_Alloc)
			                && (aaa != aba))))
			    || (/* aa is NULL and aba is Alloc */
			           (get_irn_op(aa) == op_Const)
			        && (mode_is_reference(get_irn_mode(aa)))
			        && (get_Const_tarval(aa) == get_mode_null(get_irn_mode(aa)))
			        && (get_irn_op(ab) == op_Proj)
			        && (mode_is_reference(get_irn_mode(ab)))
			        && (get_irn_op(aba) == op_Alloc)))
				/* 3.: */
			return new_tarval_from_long(proj_nr & pn_Cmp_Ne, mode_b);
		}
	}
	return computed_value_Cmp_Confirm(a, aa, ab, proj_nr);
565
}  /* computed_value_Proj_Cmp */
566

Michael Beck's avatar
Michael Beck committed
567
/**
568
569
 * Return the value of a Proj, handle Proj(Cmp), Proj(Div), Proj(Mod),
 * Proj(DivMod) and Proj(Quot).
Michael Beck's avatar
Michael Beck committed
570
 */
571
static tarval *computed_value_Proj(ir_node *n) {
Michael Beck's avatar
Michael Beck committed
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
	ir_node *a = get_Proj_pred(n);
	long proj_nr;

	switch (get_irn_opcode(a)) {
	case iro_Cmp:
		return computed_value_Proj_Cmp(n);

	case iro_DivMod:
		/* compute either the Div or the Mod part */
		proj_nr = get_Proj_proj(n);
		if (proj_nr == pn_DivMod_res_div)
			return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
		else if (proj_nr == pn_DivMod_res_mod)
			return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
		break;

	case iro_Div:
		if (get_Proj_proj(n) == pn_Div_res)
			return computed_value(a);
		break;

	case iro_Mod:
		if (get_Proj_proj(n) == pn_Mod_res)
			return computed_value(a);
		break;

	case iro_Quot:
		if (get_Proj_proj(n) == pn_Quot_res)
			return computed_value(a);
		break;

	default:
		return tarval_bad;
	}
	return tarval_bad;
607
}  /* computed_value_Proj */
608

609
/**
610
611
 * Calculate the value of a Mux: can be evaluated, if the
 * sel and the right input are known.
612
 */
613
static tarval *computed_value_Mux(ir_node *n) {
Michael Beck's avatar
Michael Beck committed
614
615
616
617
618
619
620
621
622
623
624
625
	ir_node *sel = get_Mux_sel(n);
	tarval *ts = value_of(sel);

	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;
626
}  /* computed_value_Mux */
627

Michael Beck's avatar
BugFix:    
Michael Beck committed
628
629
630
631
632
/**
 * Calculate the value of a Psi: can be evaluated, if a condition is true
 * and all previous conditions are false. If all conditions are false
 * we evaluate to the default one.
 */
633
static tarval *computed_value_Psi(ir_node *n) {
Michael Beck's avatar
Michael Beck committed
634
635
636
	if (is_Mux(n))
		return computed_value_Mux(n);
	return tarval_bad;
637
}  /* computed_value_Psi */
Michael Beck's avatar
BugFix:    
Michael Beck committed
638

Michael Beck's avatar
Michael Beck committed
639
/**
640
 * Calculate the value of a Confirm: can be evaluated,
Michael Beck's avatar
Michael Beck committed
641
642
 * if it has the form Confirm(x, '=', Const).
 */
643
static tarval *computed_value_Confirm(ir_node *n) {
Michael Beck's avatar
Michael Beck committed
644
645
	return get_Confirm_cmp(n) == pn_Cmp_Eq ?
		value_of(get_Confirm_bound(n)) : tarval_bad;
646
}  /* computed_value_Confirm */
Michael Beck's avatar
Michael Beck committed
647

648
649
650
/**
 * If the parameter n can be computed, return its value, else tarval_bad.
 * Performs constant folding.
651
652
 *
 * @param n  The node this should be evaluated
653
 */
654
tarval *computed_value(ir_node *n) {
Michael Beck's avatar
Michael Beck committed
655
656
657
	if (n->op->ops.computed_value)
		return n->op->ops.computed_value(n);
	return tarval_bad;
658
}  /* computed_value */
Christian Schäfer's avatar
Christian Schäfer committed
659

660
/**
661
 * Set the default computed_value evaluator in an ir_op_ops.
Michael Beck's avatar
Michael Beck committed
662
663
664
665
666
667
 *
 * @param code   the opcode for the default operation
 * @param ops    the operations initialized
 *
 * @return
 *    The operations.
668
 */
669
static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
670
{
Michael Beck's avatar
Michael Beck committed
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
#define CASE(a)                                    \
	case iro_##a:                                  \
		ops->computed_value  = computed_value_##a; \
		break

	switch (code) {
	CASE(Const);
	CASE(SymConst);
	CASE(Add);
	CASE(Sub);
	CASE(Minus);
	CASE(Mul);
	CASE(Quot);
	CASE(Div);
	CASE(Mod);
	CASE(Abs);
	CASE(And);
	CASE(Or);
	CASE(Eor);
	CASE(Not);
	CASE(Shl);
	CASE(Shr);
	CASE(Shrs);
	CASE(Rot);
	CASE(Carry);
	CASE(Borrow);
	CASE(Conv);
	CASE(Proj);
	CASE(Mux);
	CASE(Psi);
	CASE(Confirm);
	default:
		/* leave NULL */;
	}

	return ops;
707
#undef CASE
708
}  /* firm_set_default_computed_value */
Christian Schäfer's avatar
Christian Schäfer committed
709

Michael Beck's avatar
Michael Beck committed
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
/**
 * 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).
 */
726
727
static ir_node *equivalent_node_Block(ir_node *n)
{
Michael Beck's avatar
Michael Beck committed
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
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
802
803
804
805
806
	ir_node *oldn = n;
	int n_preds   = get_Block_n_cfgpreds(n);

	/* The Block constructor does not call optimize, but mature_immBlock
	calls the optimization. */
	assert(get_Block_matured(n));

	/* 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. */
	if ((n_preds == 1) && (get_irn_op(get_Block_cfgpred(n, 0)) == op_Jmp)) {
		ir_node *predblock = get_nodes_block(get_Block_cfgpred(n, 0));
		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);
		} else if (get_opt_control_flow_straightening()) {
			n = predblock;
			DBG_OPT_STG(oldn, n);
		}
	} else if ((n_preds == 1) &&
	           (get_irn_op(skip_Proj(get_Block_cfgpred(n, 0))) == op_Cond)) {
		ir_node *predblock = get_Block_cfgpred_block(n, 0);
		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);
		}
	} else if ((n_preds == 2) &&
	           (get_opt_control_flow_weak_simplification())) {
		/* 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);

		if ((get_irn_op(a) == op_Proj) &&
		    (get_irn_op(b) == op_Proj) &&
		    (get_Proj_pred(a) == get_Proj_pred(b)) &&
		    (get_irn_op(get_Proj_pred(a)) == op_Cond) &&
		    (get_irn_mode(get_Cond_selector(get_Proj_pred(a))) == 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(get_Proj_pred(a));
			DBG_OPT_IFSIM1(oldn, a, b, n);
		}
	} else if (get_opt_unreachable_code() &&
	           (n != get_irg_start_block(current_ir_graph)) &&
	           (n != get_irg_end_block(current_ir_graph))    ) {
		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;
807
}  /* equivalent_node_Block */
808

809
810
/**
 * Returns a equivalent node for a Jmp, a Bad :-)
811
 * Of course this only happens if the Block of the Jmp is dead.
812
 */
813
static ir_node *equivalent_node_Jmp(ir_node *n) {
Michael Beck's avatar
Michael Beck committed
814
815
816
	/* unreachable code elimination */
	if (is_Block_dead(get_nodes_block(n)))
		n = new_Bad();
817

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
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
	ir_node *oldn = n;

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

	tarval *tv;
	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.
	 */
	if (classify_tarval (tv) == TV_CLASSIFY_NULL) {
		if (get_irn_mode(on) == get_irn_mode(n)) {
			n = on;

			DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
		}
	}

	return n;
867
}  /* equivalent_node_neutral_zero */
868

869
870
871
/**
 * Eor is commutative and has neutral 0.
 */
872
#define equivalent_node_Eor  equivalent_node_neutral_zero
873

874
875
876
877
878
/*
 * 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 :-).
879
880
881
 *
 * 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.
882
 */
Michael Beck's avatar
Michael Beck committed
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
static ir_node *equivalent_node_Add(ir_node *n) {
	ir_node *oldn = n;
	ir_node *left, *right;
	ir_mode *mode = get_irn_mode(n);

	/* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
	if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
		return n;

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

	left  = get_Add_left(n);
	right = get_Add_right(n);

	if (get_irn_op(left) == op_Sub) {
		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;
			}
		}
	}
	if (get_irn_op(right) == op_Sub) {
		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;
922
}  /* equivalent_node_Add */
923

924
/**
925
926
 * optimize operations that are not commutative but have neutral 0 on left,
 * so a op 0 = a.
927
 */
928
static ir_node *equivalent_node_left_zero(ir_node *n) {
Michael Beck's avatar
Michael Beck committed
929
	ir_node *oldn = n;
930

Michael Beck's avatar
Michael Beck committed
931
932
	ir_node *a = get_binop_left(n);
	ir_node *b = get_binop_right(n);
933

Michael Beck's avatar
Michael Beck committed
934
935
	if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
		n = a;
936

Michael Beck's avatar
Michael Beck committed
937
938
939
		DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
	}
	return n;
940
}  /* equivalent_node_left_zero */
941

942
943
944
945
#define equivalent_node_Shl   equivalent_node_left_zero
#define equivalent_node_Shr   equivalent_node_left_zero
#define equivalent_node_Shrs  equivalent_node_left_zero
#define equivalent_node_Rot   equivalent_node_left_zero
946

947
948
949
950
951
/**
 * 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 :-).
952
953
954
 *
 * Beware: The Mode of a Sub may be different than the mode of its
 * predecessors, so we could not return a predecessors in all cases.
955
 */
Michael Beck's avatar
Michael Beck committed
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
static ir_node *equivalent_node_Sub(ir_node *n) {
	ir_node *oldn = n;
	ir_node *a, *b;
	ir_mode *mode = get_irn_mode(n);

	/* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
	if (mode_is_float(mode) && (get_irg_fp_model(current_ir_graph) & fp_strict_algebraic))
		return n;

	a = get_Sub_left(n);
	b = get_Sub_right(n);

	/* Beware: modes might be different */
	if (classify_tarval(value_of(b)) == TV_CLASSIFY_NULL) {
		if (mode == get_irn_mode(a)) {
			n = a;

			DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
		}
	} else if (get_irn_op(a) == op_Add) {
		if (mode_wrap_around(mode)) {
			ir_node *left  = get_Add_left(a);
			ir_node *right = get_Add_right(a);

			if (left == b) {
				if (mode == get_irn_mode(right)) {
					n = right;
					DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
				}
			} else if (right == b) {
				if (mode == get_irn_mode(left)) {
					n = left;
					DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_ADD_SUB);
				}
			}
		}
	}
	return n;
994
}  /* equivalent_node_Sub */
995
996


997
/**
Michael Beck's avatar
Michael Beck committed
998
 * Optimize an "idempotent unary op", ie op(op(n)) = n.
999
 *
1000
 * @todo
For faster browsing, not all history is shown. View entire blame