iropt.c 185 KB
Newer Older
Christian Würdig's avatar
Christian Würdig committed
1
/*
Michael Beck's avatar
Michael Beck committed
2
 * Copyright (C) 1995-2008 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>
Boris Boesler's avatar
added    
Boris Boesler committed
29

Michael Beck's avatar
Michael Beck committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#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 "opt_confirms.h"
46
#include "opt_polymorphy.h"
Michael Beck's avatar
Michael Beck committed
47
#include "irtools.h"
48
#include "irhooks.h"
49
#include "array_t.h"
50
51
#include "vrp.h"
#include "firm_types.h"
Christian Schäfer's avatar
Christian Schäfer committed
52

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

56
57
58
/**
 * Returns the tarval of a Const node or tarval_bad for all other nodes.
 */
59
60
static tarval *default_value_of(const ir_node *n)
{
61
62
63
64
65
66
67
68
	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
69
/* * Set a new value_of function. */
70
71
void set_value_of_func(value_of_func func)
{
72
73
74
75
76
77
	if (func != NULL)
		value_of_ptr = func;
	else
		value_of_ptr = default_value_of;
}

78
/**
79
 * Return the value of a Constant.
80
 */
81
82
static tarval *computed_value_Const(const ir_node *n)
{
Michael Beck's avatar
Michael Beck committed
83
	return get_Const_tarval(n);
84
}  /* computed_value_Const */
Christian Schäfer's avatar
Christian Schäfer committed
85

86
/**
Michael Beck's avatar
Michael Beck committed
87
 * Return the value of a 'sizeof', 'alignof' or 'offsetof' SymConst.
88
 */
89
90
static tarval *computed_value_SymConst(const ir_node *n)
{
Michael Beck's avatar
Michael Beck committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
	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;
115
}  /* computed_value_SymConst */
Christian Schäfer's avatar
Christian Schäfer committed
116

Michael Beck's avatar
Michael Beck committed
117
/**
118
 * Return the value of an Add.
Michael Beck's avatar
Michael Beck committed
119
 */
120
121
static tarval *computed_value_Add(const ir_node *n)
{
Michael Beck's avatar
Michael Beck committed
122
123
	ir_node *a = get_Add_left(n);
	ir_node *b = get_Add_right(n);
Christian Schäfer's avatar
Christian Schäfer committed
124

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

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

Michael Beck's avatar
Michael Beck committed
131
	return tarval_bad;
132
}  /* computed_value_Add */
Christian Schäfer's avatar
Christian Schäfer committed
133

Michael Beck's avatar
Michael Beck committed
134
/**
135
 * Return the value of a Sub.
Michael Beck's avatar
Michael Beck committed
136
137
 * Special case: a - a
 */
138
139
static tarval *computed_value_Sub(const ir_node *n)
{
140
141
142
143
144
	ir_mode *mode = get_irn_mode(n);
	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
145

146
147
148
149
150
151
	/* 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
152

Michael Beck's avatar
Michael Beck committed
153
154
	ta = value_of(a);
	tb = value_of(b);
155

156
157
	if ((ta != tarval_bad) && (tb != tarval_bad))
		return tarval_sub(ta, tb, mode);
158

Michael Beck's avatar
Michael Beck committed
159
	return tarval_bad;
160
}  /* computed_value_Sub */
161

162
/**
163
 * Return the value of a Carry.
164
165
 * Special : a op 0, 0 op b
 */
166
167
static tarval *computed_value_Carry(const ir_node *n)
{
Michael Beck's avatar
Michael Beck committed
168
169
170
171
172
173
174
175
176
177
178
	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 {
179
		if (tarval_is_null(ta) || tarval_is_null(tb))
Michael Beck's avatar
Michael Beck committed
180
181
182
			return get_mode_null(m);
	}
	return tarval_bad;
183
}  /* computed_value_Carry */
184
185

/**
186
 * Return the value of a Borrow.
187
188
 * Special : a op 0
 */
189
190
static tarval *computed_value_Borrow(const ir_node *n)
{
Michael Beck's avatar
Michael Beck committed
191
192
193
194
195
196
197
198
199
	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);
200
	} else if (tarval_is_null(ta)) {
Michael Beck's avatar
Michael Beck committed
201
202
203
		return get_mode_null(m);
	}
	return tarval_bad;
204
}  /* computed_value_Borrow */
205

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

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

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

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

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

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 Abs.
Michael Beck's avatar
Michael Beck committed
256
 */
257
258
static tarval *computed_value_Abs(const ir_node *n)
{
Michael Beck's avatar
Michael Beck committed
259
260
	ir_node *a = get_Abs_op(n);
	tarval *ta = value_of(a);
261

Michael Beck's avatar
Michael Beck committed
262
263
	if (ta != tarval_bad)
		return tarval_abs(ta);
264

Michael Beck's avatar
Michael Beck committed
265
	return tarval_bad;
266
}  /* computed_value_Abs */
267

Michael Beck's avatar
Michael Beck committed
268
/**
269
 * Return the value of an And.
Michael Beck's avatar
Michael Beck committed
270
271
 * Special case: a & 0, 0 & b
 */
272
273
static tarval *computed_value_And(const ir_node *n)
{
Michael Beck's avatar
Michael Beck committed
274
275
276
277
278
279
280
281
282
	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 {
283
284
		if (tarval_is_null(ta)) return ta;
		if (tarval_is_null(tb)) return tb;
Michael Beck's avatar
Michael Beck committed
285
286
	}
	return tarval_bad;
287
}  /* computed_value_And */
288

Michael Beck's avatar
Michael Beck committed
289
/**
290
 * Return the value of an Or.
Michael Beck's avatar
Michael Beck committed
291
292
 * Special case: a | 1...1, 1...1 | b
 */
293
294
static tarval *computed_value_Or(const ir_node *n)
{
Michael Beck's avatar
Michael Beck committed
295
296
297
298
299
300
301
302
303
	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 {
304
305
		if (tarval_is_all_one(ta)) return ta;
		if (tarval_is_all_one(tb)) return tb;
Michael Beck's avatar
Michael Beck committed
306
307
	}
	return tarval_bad;
308
}  /* computed_value_Or */
309

Michael Beck's avatar
Michael Beck committed
310
/**
311
 * Return the value of an Eor.
Michael Beck's avatar
Michael Beck committed
312
 */
313
314
static tarval *computed_value_Eor(const ir_node *n)
{
Michael Beck's avatar
Michael Beck committed
315
316
	ir_node *a = get_Eor_left(n);
	ir_node *b = get_Eor_right(n);
317

Michael Beck's avatar
Michael Beck committed
318
	tarval *ta, *tb;
Michael Beck's avatar
Michael Beck committed
319

Michael Beck's avatar
Michael Beck committed
320
321
	if (a == b)
		return get_mode_null(get_irn_mode(n));
Michael Beck's avatar
Michael Beck committed
322

Michael Beck's avatar
Michael Beck committed
323
324
	ta = value_of(a);
	tb = value_of(b);
325

Michael Beck's avatar
Michael Beck committed
326
	if ((ta != tarval_bad) && (tb != tarval_bad)) {
327
		return tarval_eor(ta, tb);
Michael Beck's avatar
Michael Beck committed
328
329
	}
	return tarval_bad;
330
}  /* computed_value_Eor */
331

Michael Beck's avatar
Michael Beck committed
332
/**
333
 * Return the value of a Not.
Michael Beck's avatar
Michael Beck committed
334
 */
335
336
static tarval *computed_value_Not(const ir_node *n)
{
Michael Beck's avatar
Michael Beck committed
337
338
	ir_node *a = get_Not_op(n);
	tarval *ta = value_of(a);
339

Michael Beck's avatar
Michael Beck committed
340
341
	if (ta != tarval_bad)
		return tarval_not(ta);
342

Michael Beck's avatar
Michael Beck committed
343
	return tarval_bad;
344
}  /* computed_value_Not */
345

Michael Beck's avatar
Michael Beck committed
346
/**
347
 * Return the value of a Shl.
Michael Beck's avatar
Michael Beck committed
348
 */
349
350
static tarval *computed_value_Shl(const ir_node *n)
{
Michael Beck's avatar
Michael Beck committed
351
352
	ir_node *a = get_Shl_left(n);
	ir_node *b = get_Shl_right(n);
353

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

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

Michael Beck's avatar
Michael Beck committed
363
/**
364
 * Return the value of a Shr.
Michael Beck's avatar
Michael Beck committed
365
 */
366
367
static tarval *computed_value_Shr(const ir_node *n)
{
Michael Beck's avatar
Michael Beck committed
368
369
	ir_node *a = get_Shr_left(n);
	ir_node *b = get_Shr_right(n);
370

Michael Beck's avatar
Michael Beck committed
371
372
	tarval *ta = value_of(a);
	tarval *tb = value_of(b);
373

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

Michael Beck's avatar
Michael Beck committed
380
/**
381
 * Return the value of a Shrs.
Michael Beck's avatar
Michael Beck committed
382
 */
383
384
static tarval *computed_value_Shrs(const ir_node *n)
{
Michael Beck's avatar
Michael Beck committed
385
386
	ir_node *a = get_Shrs_left(n);
	ir_node *b = get_Shrs_right(n);
387

Michael Beck's avatar
Michael Beck committed
388
389
	tarval *ta = value_of(a);
	tarval *tb = value_of(b);
390

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

Michael Beck's avatar
Michael Beck committed
397
/**
398
 * Return the value of a Rotl.
Michael Beck's avatar
Michael Beck committed
399
 */
400
401
static tarval *computed_value_Rotl(const ir_node *n)
{
402
403
	ir_node *a = get_Rotl_left(n);
	ir_node *b = get_Rotl_right(n);
404

Michael Beck's avatar
Michael Beck committed
405
406
	tarval *ta = value_of(a);
	tarval *tb = value_of(b);
407

Michael Beck's avatar
Michael Beck committed
408
	if ((ta != tarval_bad) && (tb != tarval_bad)) {
409
		return tarval_rotl(ta, tb);
Michael Beck's avatar
Michael Beck committed
410
411
	}
	return tarval_bad;
412
}  /* computed_value_Rotl */
413

Michael Beck's avatar
Michael Beck committed
414
/**
415
 * Return the value of a Conv.
Michael Beck's avatar
Michael Beck committed
416
 */
417
418
static tarval *computed_value_Conv(const ir_node *n)
{
Michael Beck's avatar
Michael Beck committed
419
420
	ir_node *a = get_Conv_op(n);
	tarval *ta = value_of(a);
421

Michael Beck's avatar
Michael Beck committed
422
423
	if (ta != tarval_bad)
		return tarval_convert_to(ta, get_irn_mode(n));
424

Michael Beck's avatar
Michael Beck committed
425
	return tarval_bad;
426
}  /* computed_value_Conv */
427

Michael Beck's avatar
Michael Beck committed
428
429
430
431
/**
 * Calculate the value of a Mux: can be evaluated, if the
 * sel and the right input are known.
 */
432
433
static tarval *computed_value_Mux(const ir_node *n)
{
Michael Beck's avatar
Michael Beck committed
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
	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;
}  /* computed_value_Mux */

/**
 * Calculate the value of a Confirm: can be evaluated,
 * if it has the form Confirm(x, '=', Const).
 */
452
453
static tarval *computed_value_Confirm(const ir_node *n)
{
Michael Beck's avatar
Michael Beck committed
454
455
	/*
	 * Beware: we might produce Phi(Confirm(x == true), Confirm(x == false)).
yb9976's avatar
yb9976 committed
456
	 * Do NOT optimize them away (jump threading wants them), so wait until
Michael Beck's avatar
Michael Beck committed
457
458
459
460
461
462
463
464
465
466
467
468
	 * remove_confirm is activated.
	 */
	if (get_opt_remove_confirm()) {
		if (get_Confirm_cmp(n) == pn_Cmp_Eq) {
			tarval *tv = value_of(get_Confirm_bound(n));
			if (tv != tarval_bad)
				return tv;
		}
	}
	return value_of(get_Confirm_value(n));
}  /* computed_value_Confirm */

469
/**
470
 * Return the value of a Proj(Cmp).
471
472
473
474
475
476
477
 *
 * 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.
 */
478
479
static tarval *computed_value_Proj_Cmp(const ir_node *n)
{
480
481
482
483
484
485
	ir_node *cmp   = get_Proj_pred(n);
	ir_node *left  = get_Cmp_left(cmp);
	ir_node *right = get_Cmp_right(cmp);
	long pn_cmp    = get_Proj_proj(n);
	ir_mode *mode  = get_irn_mode(left);
	tarval *tv_l, *tv_r;
Michael Beck's avatar
Michael Beck committed
486
487
488
489
490

	/*
	 * BEWARE: a == a is NOT always True for floating Point values, as
	 * NaN != NaN is defined, so we must check this here.
	 */
491
	if (left == right && (!mode_is_float(mode) || pn_cmp == pn_Cmp_Lt ||  pn_cmp == pn_Cmp_Gt)) {
Michael Beck's avatar
Michael Beck committed
492
493
		/* This is a trick with the bits used for encoding the Cmp
		   Proj numbers, the following statement is not the same:
494
495
		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
496
	}
497
498
499
500
501
502
503
504
505
	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);
506
		if (flags != pn_Cmp_False) {
507
			return new_tarval_from_long (pn_cmp & flags, mode_b);
Michael Beck's avatar
Michael Beck committed
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
	} 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;
537
538
		}

539
		cmp_result = vrp_cmp(left, right);
540
		if (cmp_result != pn_Cmp_False) {
541
			if (cmp_result == pn_Cmp_Lg) {
542
543
544
545
				if (pn_cmp == pn_Cmp_Eq) {
					return tarval_b_false;
				} else if (pn_cmp == pn_Cmp_Lg) {
					return tarval_b_true;
546
547
				}
			} else {
548
				return new_tarval_from_long(cmp_result & pn_cmp, mode_b);
549
			}
Michael Beck's avatar
Michael Beck committed
550
		}
551
552
553
554
555
556
557
558
559
560
561
562
563
564
	} 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);
		}
565
	}
566
	return computed_value_Cmp_Confirm(cmp, left, right, pn_cmp);
567
}  /* computed_value_Proj_Cmp */
568

569
570
571
/**
 * Return the value of a floating point Quot.
 */
572
573
static tarval *do_computed_value_Quot(const ir_node *a, const ir_node *b)
{
574
575
576
577
578
579
580
581
582
583
584
585
586
	tarval  *ta = value_of(a);
	tarval  *tb = value_of(b);

	/* cannot optimize 0 / b = 0 because of NaN */
	if (ta != tarval_bad && tb != tarval_bad)
		return tarval_quo(ta, tb);
	return tarval_bad;
}  /* do_computed_value_Quot */

/**
 * Calculate the value of an integer Div of two nodes.
 * Special case: 0 / b
 */
587
588
static tarval *do_computed_value_Div(const ir_node *a, const ir_node *b)
{
Michael Beck's avatar
Michael Beck committed
589
590
591
	tarval        *ta = value_of(a);
	tarval        *tb;
	const ir_node *dummy;
592
593
594
595
596
597
598
599
600
601
602
603
604
605

	/* 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
 */
606
607
static tarval *do_computed_value_Mod(const ir_node *a, const ir_node *b)
{
608
609
610
611
612
613
	tarval *ta = value_of(a);
	tarval *tb = value_of(b);

	/* 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
614
	if (ta != tarval_bad && tb != tarval_bad)
615
616
617
618
		return tarval_mod(ta, tb);
	return tarval_bad;
}  /* do_computed_value_Mod */

Michael Beck's avatar
Michael Beck committed
619
/**
Michael Beck's avatar
Michael Beck committed
620
 * Return the value of a Proj(DivMod).
Michael Beck's avatar
Michael Beck committed
621
 */
622
623
static tarval *computed_value_Proj_DivMod(const ir_node *n)
{
Michael Beck's avatar
Michael Beck committed
624
	long proj_nr = get_Proj_proj(n);
Michael Beck's avatar
Michael Beck committed
625

Michael Beck's avatar
Michael Beck committed
626
627
628
629
630
631
632
633
634
635
	/* compute either the Div or the Mod part */
	if (proj_nr == pn_DivMod_res_div) {
		const ir_node *a = get_Proj_pred(n);
		return do_computed_value_Div(get_DivMod_left(a), get_DivMod_right(a));
	} else if (proj_nr == pn_DivMod_res_mod) {
		const ir_node *a = get_Proj_pred(n);
		return do_computed_value_Mod(get_DivMod_left(a), get_DivMod_right(a));
	}
	return tarval_bad;
}  /* computed_value_Proj_DivMod */
Michael Beck's avatar
Michael Beck committed
636

Michael Beck's avatar
Michael Beck committed
637
638
639
/**
 * Return the value of a Proj(Div).
 */
640
641
static tarval *computed_value_Proj_Div(const ir_node *n)
{
Michael Beck's avatar
Michael Beck committed
642
	long proj_nr = get_Proj_proj(n);
Michael Beck's avatar
Michael Beck committed
643

Michael Beck's avatar
Michael Beck committed
644
645
646
	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
647
648
	}
	return tarval_bad;
Michael Beck's avatar
Michael Beck committed
649
}  /* computed_value_Proj_Div */
650

651
/**
Michael Beck's avatar
Michael Beck committed
652
 * Return the value of a Proj(Mod).
653
 */
654
655
static tarval *computed_value_Proj_Mod(const ir_node *n)
{
Michael Beck's avatar
Michael Beck committed
656
	long proj_nr = get_Proj_proj(n);
Michael Beck's avatar
Michael Beck committed
657

Michael Beck's avatar
Michael Beck committed
658
659
660
	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
661
662
	}
	return tarval_bad;
Michael Beck's avatar
Michael Beck committed
663
}  /* computed_value_Proj_Mod */
664

Michael Beck's avatar
BugFix:    
Michael Beck committed
665
/**
Michael Beck's avatar
Michael Beck committed
666
 * Return the value of a Proj(Quot).
Michael Beck's avatar
BugFix:    
Michael Beck committed
667
 */
668
669
static tarval *computed_value_Proj_Quot(const ir_node *n)
{
Michael Beck's avatar
Michael Beck committed
670
671
672
673
674
675
	long proj_nr = get_Proj_proj(n);

	if (proj_nr == pn_Quot_res) {
		const ir_node *a = get_Proj_pred(n);
		return do_computed_value_Quot(get_Quot_left(a), get_Quot_right(a));
	}
Michael Beck's avatar
Michael Beck committed
676
	return tarval_bad;
Michael Beck's avatar
Michael Beck committed
677
}  /* computed_value_Proj_Quot */
Michael Beck's avatar
BugFix:    
Michael Beck committed
678

Michael Beck's avatar
Michael Beck committed
679
/**
Michael Beck's avatar
Michael Beck committed
680
 * Return the value of a Proj.
Michael Beck's avatar
Michael Beck committed
681
 */
682
683
static tarval *computed_value_Proj(const ir_node *proj)
{
Michael Beck's avatar
Michael Beck committed
684
685
686
687
688
689
	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
690

691
692
693
/**
 * If the parameter n can be computed, return its value, else tarval_bad.
 * Performs constant folding.
694
695
 *
 * @param n  The node this should be evaluated
696
 */
697
698
tarval *computed_value(const ir_node *n)
{
699
	vrp_attr *vrp = vrp_get_info(n);
700
	if (vrp && vrp->valid && tarval_cmp(vrp->bits_set, vrp->bits_not_set) == pn_Cmp_Eq) {
701
		return vrp->bits_set;
702
	}
Michael Beck's avatar
Michael Beck committed
703
704
705
	if (n->op->ops.computed_value)
		return n->op->ops.computed_value(n);
	return tarval_bad;
706
}  /* computed_value */
Christian Schäfer's avatar
Christian Schäfer committed
707

708
/**
709
 * Set the default computed_value evaluator in an ir_op_ops.
Michael Beck's avatar
Michael Beck committed
710
711
712
713
714
715
 *
 * @param code   the opcode for the default operation
 * @param ops    the operations initialized
 *
 * @return
 *    The operations.
716
 */
717
718
static ir_op_ops *firm_set_default_computed_value(ir_opcode code, ir_op_ops *ops)
{
Michael Beck's avatar
Michael Beck committed
719
720
721
722
723
724
725
#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
726
727
728
729
730
731
732
		break

	switch (code) {
	CASE(Const);
	CASE(SymConst);
	CASE(Add);
	CASE(Sub);
Michael Beck's avatar
Michael Beck committed
733
734
	CASE(Carry);
	CASE(Borrow);
Michael Beck's avatar
Michael Beck committed
735
736
737
738
739
740
741
742
743
744
	CASE(Minus);
	CASE(Mul);
	CASE(Abs);
	CASE(And);
	CASE(Or);
	CASE(Eor);
	CASE(Not);
	CASE(Shl);
	CASE(Shr);
	CASE(Shrs);
745
	CASE(Rotl);
Michael Beck's avatar
Michael Beck committed
746
747
748
	CASE(Conv);
	CASE(Mux);
	CASE(Confirm);
Michael Beck's avatar
Michael Beck committed
749
750
751
752
753
754
	CASE_PROJ(Cmp);
	CASE_PROJ(DivMod);
	CASE_PROJ(Div);
	CASE_PROJ(Mod);
	CASE_PROJ(Quot);
	CASE(Proj);
Michael Beck's avatar
Michael Beck committed
755
	default:
756
757
		/* leave NULL */
		break;
Michael Beck's avatar
Michael Beck committed
758
759
760
	}

	return ops;
Michael Beck's avatar
Michael Beck committed
761
#undef CASE_PROJ
762
#undef CASE
763
}  /* firm_set_default_computed_value */
Christian Schäfer's avatar
Christian Schäfer committed
764

Michael Beck's avatar
Michael Beck committed
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
/**
 * 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).
 */
781
782
static ir_node *equivalent_node_Block(ir_node *n)
{
Michael Beck's avatar
Michael Beck committed
783
	ir_node *oldn = n;
784
	int     n_preds;
Michael Beck's avatar
Michael Beck committed
785

786
	/* don't optimize dead or labeled blocks */
787
	if (is_Block_dead(n) || has_Block_entity(n))
788
789
790
791
792
793
		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
794
795
796
797
798
799
800
801
	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. */
Michael Beck's avatar
Michael Beck committed
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
	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);
			} else if (get_opt_control_flow_straightening()) {
				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
822
823
824
825
826
827
828
829
830
831
		}
	} 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);

Michael Beck's avatar
Michael Beck committed
832
833
834
835
836
837
838
839
840
841
		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
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
		}
	} 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;
872
}  /* equivalent_node_Block */
873

874
875
/**
 * Returns a equivalent node for a Jmp, a Bad :-)
876
 * Of course this only happens if the Block of the Jmp is dead.
877
 */
878
879
static ir_node *equivalent_node_Jmp(ir_node *n)
{
Michael Beck's avatar
Michael Beck committed
880
	ir_node *oldn = n;
881

Michael Beck's avatar
Michael Beck committed
882
883
884
885
886
	/* unreachable code elimination */
	if (is_Block_dead(get_nodes_block(n))) {
		n = get_irg_bad(current_ir_graph);
		DBG_OPT_DEAD_BLOCK(oldn, n);
	}
Michael Beck's avatar
Michael Beck committed
887
	return n;
888
}  /* equivalent_node_Jmp */
889

890
/** Raise is handled in the same way as Jmp. */
891
892
893
#define equivalent_node_Raise   equivalent_node_Jmp


894
895
/* We do not evaluate Cond here as we replace it by a new node, a Jmp.
   See transform_node_Proj_Cond(). */
896

897
/**
898
 * Optimize operations that are commutative and have neutral 0,
899
 * so a op 0 = 0 op a = a.
900
 */
901
902
static ir_node *equivalent_node_neutral_zero(ir_node *n)
{
Michael Beck's avatar
Michael Beck committed
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
	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.
	 */
927
928
	if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
		n = on;
Michael Beck's avatar
Michael Beck committed
929

930
		DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
Michael Beck's avatar
Michael Beck committed
931
932
933
	}

	return n;
934
}  /* equivalent_node_neutral_zero */
935

936
937
938
/**
 * Eor is commutative and has neutral 0.
 */
939
940
static ir_node *equivalent_node_Eor(ir_node *n)
{
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
	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
958
			DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
Michael Beck's avatar
Michael Beck committed
959
			return n;
960
961
962
		} else if (ab == b) {
			/* (a ^ b) ^ b -> a */
			n = aa;
Michael Beck's avatar
Michael Beck committed
963
			DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
Michael Beck's avatar
Michael Beck committed
964
			return n;
965
		}
966
967
	}
	if (is_Eor(b)) {
968
969
970
971
972
973
		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
974
			DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
Michael Beck's avatar
Michael Beck committed
975
			return n;
976
977
978
		} else if (bb == a) {
			/* a ^ (b ^ a) -> b */
			n = ba;
Michael Beck's avatar
Michael Beck committed
979
			DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
Michael Beck's avatar
Michael Beck committed
980
			return n;
981
982
983
984
		}
	}
	return n;
}
985

986
987
988
989
990
/*
 * 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 :-).
991
992
993
 *
 * 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.
994
 */
995
996
static ir_node *equivalent_node_Add(ir_node *n)
{
Michael Beck's avatar
Michael Beck committed
997
998
999
1000
	ir_node *oldn = n;
	ir_node *left, *right;
	ir_mode *mode = get_irn_mode(n);

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