iropt.c 183 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

134
135
136
137
138
139
	/* x+~x => -1 */
	if ((is_Not(a) && get_Not_op(a) == b)
	    || (is_Not(b) && get_Not_op(b) == a)) {
		return get_mode_all_one(get_irn_mode(n));
	}

Michael Beck's avatar
Michael Beck committed
140
	return tarval_bad;
141
}  /* computed_value_Add */
Christian Schäfer's avatar
Christian Schäfer committed
142

Michael Beck's avatar
Michael Beck committed
143
/**
144
 * Return the value of a Sub.
Michael Beck's avatar
Michael Beck committed
145
146
 * Special case: a - a
 */
Matthias Braun's avatar
Matthias Braun committed
147
static ir_tarval *computed_value_Sub(const ir_node *n)
148
{
Matthias Braun's avatar
Matthias Braun committed
149
150
151
152
153
	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
154

155
156
157
158
159
160
	/* 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
161

Michael Beck's avatar
Michael Beck committed
162
163
	ta = value_of(a);
	tb = value_of(b);
164

165
166
	if ((ta != tarval_bad) && (tb != tarval_bad))
		return tarval_sub(ta, tb, mode);
167

Michael Beck's avatar
Michael Beck committed
168
	return tarval_bad;
169
}  /* computed_value_Sub */
170

171
172
173
174
/**
 * Return the value of a Carry.
 * Special : a op 0, 0 op b
 */
Matthias Braun's avatar
Matthias Braun committed
175
static ir_tarval *computed_value_Carry(const ir_node *n)
176
{
Matthias Braun's avatar
Matthias Braun committed
177
178
179
180
181
	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);
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196

	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
197
static ir_tarval *computed_value_Borrow(const ir_node *n)
198
{
Matthias Braun's avatar
Matthias Braun committed
199
200
201
202
203
	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);
204
205

	if ((ta != tarval_bad) && (tb != tarval_bad)) {
206
		return tarval_cmp(ta, tb) == ir_relation_less ? get_mode_one(m) : get_mode_null(m);
207
208
209
210
211
212
	} else if (tarval_is_null(ta)) {
		return get_mode_null(m);
	}
	return tarval_bad;
}  /* computed_value_Borrow */

Michael Beck's avatar
Michael Beck committed
213
/**
214
 * Return the value of an unary Minus.
Michael Beck's avatar
Michael Beck committed
215
 */
Matthias Braun's avatar
Matthias Braun committed
216
static ir_tarval *computed_value_Minus(const ir_node *n)
217
{
Matthias Braun's avatar
Matthias Braun committed
218
219
	ir_node   *a  = get_Minus_op(n);
	ir_tarval *ta = value_of(a);
220

Michael Beck's avatar
Michael Beck committed
221
	if (ta != tarval_bad)
Michael Beck's avatar
Michael Beck committed
222
		return tarval_neg(ta);
223

Michael Beck's avatar
Michael Beck committed
224
	return tarval_bad;
225
}  /* computed_value_Minus */
226

Michael Beck's avatar
Michael Beck committed
227
/**
228
 * Return the value of a Mul.
Michael Beck's avatar
Michael Beck committed
229
 */
Matthias Braun's avatar
Matthias Braun committed
230
static ir_tarval *computed_value_Mul(const ir_node *n)
231
{
Matthias Braun's avatar
Matthias Braun committed
232
233
234
235
236
	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
237

Michael Beck's avatar
Michael Beck committed
238
239
240
241
242
243
244
245
	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
246
247
		return tarval_mul(ta, tb);
	} else {
248
249
250
251
252
253
254
255
		/* 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
256
257
	}
	return tarval_bad;
258
}  /* computed_value_Mul */
259

Michael Beck's avatar
Michael Beck committed
260
/**
261
 * Return the value of an And.
Michael Beck's avatar
Michael Beck committed
262
263
 * Special case: a & 0, 0 & b
 */
Matthias Braun's avatar
Matthias Braun committed
264
static ir_tarval *computed_value_And(const ir_node *n)
265
{
Matthias Braun's avatar
Matthias Braun committed
266
267
268
269
	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
270
271
272
273

	if ((ta != tarval_bad) && (tb != tarval_bad)) {
		return tarval_and (ta, tb);
	}
274
275
276
277
278
279
280
281
282
283

	if (tarval_is_null(ta)) return ta;
	if (tarval_is_null(tb)) return tb;

	/* x&~x => 0 */
	if ((is_Not(a) && get_Not_op(a) == b)
	    || (is_Not(b) && get_Not_op(b) == a)) {
		return get_mode_null(get_irn_mode(n));
	}

Michael Beck's avatar
Michael Beck committed
284
	return tarval_bad;
285
}  /* computed_value_And */
286

Michael Beck's avatar
Michael Beck committed
287
/**
288
 * Return the value of an Or.
Michael Beck's avatar
Michael Beck committed
289
290
 * Special case: a | 1...1, 1...1 | b
 */
Matthias Braun's avatar
Matthias Braun committed
291
static ir_tarval *computed_value_Or(const ir_node *n)
292
{
Matthias Braun's avatar
Matthias Braun committed
293
294
295
296
	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
297
298
299

	if ((ta != tarval_bad) && (tb != tarval_bad)) {
		return tarval_or (ta, tb);
300
301
302
303
304
305
306
307
308
	}

	if (tarval_is_all_one(ta)) return ta;
	if (tarval_is_all_one(tb)) return tb;

	/* x|~x => -1 */
	if ((is_Not(a) && get_Not_op(a) == b)
	    || (is_Not(b) && get_Not_op(b) == a)) {
		return get_mode_all_one(get_irn_mode(n));
Michael Beck's avatar
Michael Beck committed
309
310
	}
	return tarval_bad;
311
}  /* computed_value_Or */
312

Michael Beck's avatar
Michael Beck committed
313
/**
314
 * Return the value of an Eor.
Michael Beck's avatar
Michael Beck committed
315
 */
Matthias Braun's avatar
Matthias Braun committed
316
static ir_tarval *computed_value_Eor(const ir_node *n)
317
{
Michael Beck's avatar
Michael Beck committed
318
319
	ir_node *a = get_Eor_left(n);
	ir_node *b = get_Eor_right(n);
320

Matthias Braun's avatar
Matthias Braun committed
321
	ir_tarval *ta, *tb;
Michael Beck's avatar
Michael Beck committed
322

Michael Beck's avatar
Michael Beck committed
323
324
	if (a == b)
		return get_mode_null(get_irn_mode(n));
325
326
327
328
329
	/* x^~x => -1 */
	if ((is_Not(a) && get_Not_op(a) == b)
	    || (is_Not(b) && get_Not_op(b) == a)) {
		return get_mode_all_one(get_irn_mode(n));
	}
Michael Beck's avatar
Michael Beck committed
330

Michael Beck's avatar
Michael Beck committed
331
332
	ta = value_of(a);
	tb = value_of(b);
333

Michael Beck's avatar
Michael Beck committed
334
	if ((ta != tarval_bad) && (tb != tarval_bad)) {
335
		return tarval_eor(ta, tb);
Michael Beck's avatar
Michael Beck committed
336
337
	}
	return tarval_bad;
338
}  /* computed_value_Eor */
339

Michael Beck's avatar
Michael Beck committed
340
/**
341
 * Return the value of a Not.
Michael Beck's avatar
Michael Beck committed
342
 */
Matthias Braun's avatar
Matthias Braun committed
343
static ir_tarval *computed_value_Not(const ir_node *n)
344
{
Matthias Braun's avatar
Matthias Braun committed
345
346
	ir_node   *a  = get_Not_op(n);
	ir_tarval *ta = value_of(a);
347

Michael Beck's avatar
Michael Beck committed
348
349
	if (ta != tarval_bad)
		return tarval_not(ta);
350

Michael Beck's avatar
Michael Beck committed
351
	return tarval_bad;
352
}  /* computed_value_Not */
353

354
/**
yb9976's avatar
yb9976 committed
355
 * Tests whether a shift shifts more bits than available in the mode
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
 */
static bool is_oversize_shift(const ir_node *n)
{
	ir_node   *count = get_binop_right(n);
	ir_mode   *mode  = get_irn_mode(n);
	ir_tarval *tv    = value_of(count);
	long       modulo_shift;
	long       shiftval;
	if (tv == tarval_bad)
		return false;
	if (!tarval_is_long(tv))
		return false;
	shiftval     = get_tarval_long(tv);
	modulo_shift = get_mode_modulo_shift(mode);
	if (shiftval < 0 || (modulo_shift > 0 && shiftval >= modulo_shift))
		return false;

	return shiftval >= (long)get_mode_size_bits(mode);
}

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

Matthias Braun's avatar
Matthias Braun committed
384
385
	ir_tarval *ta = value_of(a);
	ir_tarval *tb = value_of(b);
386

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

	if (is_oversize_shift(n))
		return get_mode_null(get_irn_mode(n));

Michael Beck's avatar
Michael Beck committed
394
	return tarval_bad;
395
}  /* computed_value_Shl */
396

Michael Beck's avatar
Michael Beck committed
397
/**
398
 * Return the value of a Shr.
Michael Beck's avatar
Michael Beck committed
399
 */
Matthias Braun's avatar
Matthias Braun committed
400
static ir_tarval *computed_value_Shr(const ir_node *n)
401
{
Michael Beck's avatar
Michael Beck committed
402
403
	ir_node *a = get_Shr_left(n);
	ir_node *b = get_Shr_right(n);
404

Matthias Braun's avatar
Matthias Braun committed
405
406
	ir_tarval *ta = value_of(a);
	ir_tarval *tb = value_of(b);
407

Michael Beck's avatar
Michael Beck committed
408
	if ((ta != tarval_bad) && (tb != tarval_bad)) {
409
		return tarval_shr(ta, tb);
Michael Beck's avatar
Michael Beck committed
410
	}
411
412
413
	if (is_oversize_shift(n))
		return get_mode_null(get_irn_mode(n));

Michael Beck's avatar
Michael Beck committed
414
	return tarval_bad;
415
}  /* computed_value_Shr */
416

Michael Beck's avatar
Michael Beck committed
417
/**
418
 * Return the value of a Shrs.
Michael Beck's avatar
Michael Beck committed
419
 */
Matthias Braun's avatar
Matthias Braun committed
420
static ir_tarval *computed_value_Shrs(const ir_node *n)
421
{
Michael Beck's avatar
Michael Beck committed
422
423
	ir_node *a = get_Shrs_left(n);
	ir_node *b = get_Shrs_right(n);
424

Matthias Braun's avatar
Matthias Braun committed
425
426
	ir_tarval *ta = value_of(a);
	ir_tarval *tb = value_of(b);
427

Michael Beck's avatar
Michael Beck committed
428
	if ((ta != tarval_bad) && (tb != tarval_bad)) {
429
		return tarval_shrs(ta, tb);
Michael Beck's avatar
Michael Beck committed
430
431
	}
	return tarval_bad;
432
}  /* computed_value_Shrs */
433

Michael Beck's avatar
Michael Beck committed
434
/**
435
 * Return the value of a Rotl.
Michael Beck's avatar
Michael Beck committed
436
 */
Matthias Braun's avatar
Matthias Braun committed
437
static ir_tarval *computed_value_Rotl(const ir_node *n)
438
{
439
440
	ir_node *a = get_Rotl_left(n);
	ir_node *b = get_Rotl_right(n);
441

Matthias Braun's avatar
Matthias Braun committed
442
443
	ir_tarval *ta = value_of(a);
	ir_tarval *tb = value_of(b);
444

Michael Beck's avatar
Michael Beck committed
445
	if ((ta != tarval_bad) && (tb != tarval_bad)) {
446
		return tarval_rotl(ta, tb);
Michael Beck's avatar
Michael Beck committed
447
448
	}
	return tarval_bad;
449
}  /* computed_value_Rotl */
450

451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
bool ir_zero_when_converted(const ir_node *node, ir_mode *dest_mode)
{
	ir_mode *mode = get_irn_mode(node);
	if (get_mode_arithmetic(mode) != irma_twos_complement
	    || get_mode_arithmetic(dest_mode) != irma_twos_complement)
	    return false;

	if (is_Shl(node)) {
		ir_node *count = get_Shl_right(node);
		if (is_Const(count)) {
			ir_tarval *tv = get_Const_tarval(count);
			if (tarval_is_long(tv)) {
				long shiftval = get_tarval_long(tv);
				long destbits = get_mode_size_bits(dest_mode);
				if (shiftval >= destbits
				    && shiftval < (long)get_mode_modulo_shift(mode))
					return true;
			}
		}
	}
	if (is_And(node)) {
		ir_node *right = get_And_right(node);
		if (is_Const(right)) {
			ir_tarval *tv     = get_Const_tarval(right);
			ir_tarval *conved = tarval_convert_to(tv, dest_mode);
			return tarval_is_null(conved);
		}
	}
	return false;
}

Michael Beck's avatar
Michael Beck committed
482
/**
483
 * Return the value of a Conv.
Michael Beck's avatar
Michael Beck committed
484
 */
Matthias Braun's avatar
Matthias Braun committed
485
static ir_tarval *computed_value_Conv(const ir_node *n)
486
{
487
488
489
	ir_node   *a    = get_Conv_op(n);
	ir_tarval *ta   = value_of(a);
	ir_mode   *mode = get_irn_mode(n);
490

Michael Beck's avatar
Michael Beck committed
491
492
	if (ta != tarval_bad)
		return tarval_convert_to(ta, get_irn_mode(n));
493

494
495
496
	if (ir_zero_when_converted(a, mode))
		return get_mode_null(mode);

Michael Beck's avatar
Michael Beck committed
497
	return tarval_bad;
498
}  /* computed_value_Conv */
499

Michael Beck's avatar
Michael Beck committed
500
501
502
503
/**
 * 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
504
static ir_tarval *computed_value_Mux(const ir_node *n)
505
{
Michael Beck's avatar
Michael Beck committed
506
	ir_node *sel = get_Mux_sel(n);
Matthias Braun's avatar
Matthias Braun committed
507
	ir_tarval *ts = value_of(sel);
Michael Beck's avatar
Michael Beck committed
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523

	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
524
static ir_tarval *computed_value_Confirm(const ir_node *n)
525
{
526
	if (get_Confirm_relation(n) == ir_relation_equal) {
Matthias Braun's avatar
Matthias Braun committed
527
		ir_tarval *tv = value_of(get_Confirm_bound(n));
528
529
		if (tv != tarval_bad)
			return tv;
Michael Beck's avatar
Michael Beck committed
530
531
532
533
	}
	return value_of(get_Confirm_value(n));
}  /* computed_value_Confirm */

534
/**
535
536
 * gives a (conservative) estimation of possible relation when comparing
 * left+right
537
 */
538
539
ir_relation ir_get_possible_cmp_relations(const ir_node *left,
                                          const ir_node *right)
540
{
541
542
543
544
545
546
	ir_relation possible = ir_relation_true;
	ir_tarval  *tv_l     = value_of(left);
	ir_tarval  *tv_r     = value_of(right);
	ir_mode    *mode     = get_irn_mode(left);
	ir_tarval  *min      = mode == mode_b ? tarval_b_false : get_mode_min(mode);
	ir_tarval  *max      = mode == mode_b ? tarval_b_true  : get_mode_max(mode);
547

548
	/* both values known - evaluate them */
549
	if ((tv_l != tarval_bad) && (tv_r != tarval_bad)) {
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
		possible = tarval_cmp(tv_l, tv_r);
		/* we can return now, won't get any better */
		return possible;
	}
	/* a == a is never less or greater (but might be equal or unordered) */
	if (left == right)
		possible &= ~ir_relation_less_greater;
	/* unordered results only happen for float compares */
	if (!mode_is_float(mode))
		possible &= ~ir_relation_unordered;
	/* values can never be less than the least representable number or
	 * greater than the greatest representable number */
	if (tv_l == min)
		possible &= ~ir_relation_greater;
	if (tv_l == max)
		possible &= ~ir_relation_less;
	if (tv_r == max)
		possible &= ~ir_relation_greater;
	if (tv_r == min)
		possible &= ~ir_relation_less;
	/* maybe vrp can tell us more */
	possible &= vrp_cmp(left, right);
	/* Alloc nodes never return null (but throw an exception) */
	if (is_Alloc(left) && tarval_is_null(tv_r))
		possible &= ~ir_relation_equal;

	return possible;
}
578

579
static ir_tarval *compute_cmp(const ir_node *cmp)
580
581
582
{
	ir_node    *left     = get_Cmp_left(cmp);
	ir_node    *right    = get_Cmp_right(cmp);
583
	ir_relation possible = ir_get_possible_cmp_relations(left, right);
584
585
586
587
588
589
590
591
592
593
594
	ir_relation relation = get_Cmp_relation(cmp);

	/* if none of the requested relations is possible, return false */
	if ((possible & relation) == ir_relation_false)
		return tarval_b_false;
	/* if possible relations are a subset of the requested ones return true */
	if ((possible & ~relation) == ir_relation_false)
		return tarval_b_true;

	return computed_value_Cmp_Confirm(cmp, left, right, relation);
}
595

596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/**
 * Return the value of a Cmp.
 *
 * The basic idea here is to determine which relations are possible and which
 * one are definitely impossible.
 */
static ir_tarval *computed_value_Cmp(const ir_node *cmp)
{
	/* we can't construct Constb after lowering mode_b nodes */
	if (is_irg_state(get_irn_irg(cmp), IR_GRAPH_STATE_MODEB_LOWERED))
		return tarval_bad;

	return compute_cmp(cmp);
}

611
/**
Michael Beck's avatar
Michael Beck committed
612
 * Calculate the value of an integer Div.
613
614
 * Special case: 0 / b
 */
615
static ir_tarval *do_computed_value_Div(const ir_node *div)
616
{
617
618
619
620
	const ir_node *a    = get_Div_left(div);
	const ir_node *b    = get_Div_right(div);
	const ir_mode *mode = get_Div_resmode(div);
	ir_tarval     *ta   = value_of(a);
Matthias Braun's avatar
Matthias Braun committed
621
	ir_tarval     *tb;
Michael Beck's avatar
Michael Beck committed
622
	const ir_node *dummy;
623

Michael Beck's avatar
Michael Beck committed
624
	/* cannot optimize 0 / b = 0 because of NaN */
625
626
	if (!mode_is_float(mode)) {
		if (tarval_is_null(ta) && value_not_zero(b, &dummy))
Michael Beck's avatar
Michael Beck committed
627
			return ta;  /* 0 / b == 0 if b != 0 */
628
	}
629
630
631
632
	tb = value_of(b);
	if (ta != tarval_bad && tb != tarval_bad)
		return tarval_div(ta, tb);
	return tarval_bad;
Michael Beck's avatar
Michael Beck committed
633
}  /* do_computed_value_Div */
Michael Beck's avatar
Michael Beck committed
634

635
636
637
638
/**
 * Calculate the value of an integer Mod of two nodes.
 * Special case: a % 1
 */
Matthias Braun's avatar
Matthias Braun committed
639
static ir_tarval *do_computed_value_Mod(const ir_node *a, const ir_node *b)
640
{
Matthias Braun's avatar
Matthias Braun committed
641
642
	ir_tarval *ta = value_of(a);
	ir_tarval *tb = value_of(b);
643
644
645
646

	/* 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
647
	if (ta != tarval_bad && tb != tarval_bad)
648
649
650
651
		return tarval_mod(ta, tb);
	return tarval_bad;
}  /* do_computed_value_Mod */

Michael Beck's avatar
Michael Beck committed
652
653
654
/**
 * Return the value of a Proj(Div).
 */
Matthias Braun's avatar
Matthias Braun committed
655
static ir_tarval *computed_value_Proj_Div(const ir_node *n)
656
{
Michael Beck's avatar
Michael Beck committed
657
	long proj_nr = get_Proj_proj(n);
658
659
	if (proj_nr != pn_Div_res)
		return tarval_bad;
Michael Beck's avatar
Michael Beck committed
660

661
	return do_computed_value_Div(get_Proj_pred(n));
Michael Beck's avatar
Michael Beck committed
662
}  /* computed_value_Proj_Div */
663

664
/**
Michael Beck's avatar
Michael Beck committed
665
 * Return the value of a Proj(Mod).
666
 */
Matthias Braun's avatar
Matthias Braun committed
667
static ir_tarval *computed_value_Proj_Mod(const ir_node *n)
668
{
Michael Beck's avatar
Michael Beck committed
669
	long proj_nr = get_Proj_proj(n);
Michael Beck's avatar
Michael Beck committed
670

Michael Beck's avatar
Michael Beck committed
671
	if (proj_nr == pn_Mod_res) {
Michael Beck's avatar
Michael Beck committed
672
673
		const ir_node *mod = get_Proj_pred(n);
		return do_computed_value_Mod(get_Mod_left(mod), get_Mod_right(mod));
Michael Beck's avatar
Michael Beck committed
674
675
	}
	return tarval_bad;
Michael Beck's avatar
Michael Beck committed
676
}  /* computed_value_Proj_Mod */
677

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

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

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

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

	return ops;
Michael Beck's avatar
Michael Beck committed
757
#undef CASE_PROJ
758
#undef CASE
759
}  /* firm_set_default_computed_value */
Christian Schäfer's avatar
Christian Schäfer committed
760

761
/**
762
 * Optimize operations that are commutative and have neutral 0,
763
 * so a op 0 = 0 op a = a.
764
 */
765
766
static ir_node *equivalent_node_neutral_zero(ir_node *n)
{
Michael Beck's avatar
Michael Beck committed
767
768
769
770
771
	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
772
	ir_tarval *tv;
Michael Beck's avatar
Michael Beck committed
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
	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.
	 */
791
792
	if (tarval_is_null(tv) && get_irn_mode(on) == get_irn_mode(n)) {
		n = on;
Michael Beck's avatar
Michael Beck committed
793

794
		DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
Michael Beck's avatar
Michael Beck committed
795
796
797
	}

	return n;
798
}  /* equivalent_node_neutral_zero */
799

800
801
802
/**
 * Eor is commutative and has neutral 0.
 */
803
804
static ir_node *equivalent_node_Eor(ir_node *n)
{
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
	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
822
			DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
Michael Beck's avatar
Michael Beck committed
823
			return n;
824
825
826
		} else if (ab == b) {
			/* (a ^ b) ^ b -> a */
			n = aa;
Michael Beck's avatar
Michael Beck committed
827
			DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
Michael Beck's avatar
Michael Beck committed
828
			return n;
829
		}
830
831
	}
	if (is_Eor(b)) {
832
833
834
835
836
837
		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
838
			DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
Michael Beck's avatar
Michael Beck committed
839
			return n;
840
841
842
		} else if (bb == a) {
			/* a ^ (b ^ a) -> b */
			n = ba;
Michael Beck's avatar
Michael Beck committed
843
			DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_EOR_A_B_A);
Michael Beck's avatar
Michael Beck committed
844
			return n;
845
846
847
848
		}
	}
	return n;
}
849

850
851
852
853
854
/*
 * 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 :-).
855
856
857
 *
 * 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.
858
 */
859
860
static ir_node *equivalent_node_Add(ir_node *n)
{
Michael Beck's avatar
Michael Beck committed
861
862
863
864
865
866
867
868
	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;

869
	/* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
870
871
872
873
874
	if (mode_is_float(mode)) {
		ir_graph *irg = get_irn_irg(n);
		if (get_irg_fp_model(irg) & fp_strict_algebraic)
			return n;
	}
875

Michael Beck's avatar
Michael Beck committed
876
877
878
	left  = get_Add_left(n);
	right = get_Add_right(n);

879
	if (is_Sub(left)) {
Michael Beck's avatar
Michael Beck committed
880
881
882
883
884
885
886
887
888
889
		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;
			}
		}
	}
890
	if (is_Sub(right)) {
Michael Beck's avatar
Michael Beck committed
891
892
893
894
895
896
897
898
899
900
901
		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;
902
}  /* equivalent_node_Add */
903

904
/**
905
906
 * optimize operations that are not commutative but have neutral 0 on left,
 * so a op 0 = a.
907
 */
908
909
static ir_node *equivalent_node_left_zero(ir_node *n)
{
Michael Beck's avatar
Michael Beck committed
910
	ir_node *oldn = n;
911

Matthias Braun's avatar
Matthias Braun committed
912
913
914
	ir_node   *a  = get_binop_left(n);
	ir_node   *b  = get_binop_right(n);
	ir_tarval *tb = value_of(b);
915

916
	if (tarval_is_null(tb)) {
Michael Beck's avatar
Michael Beck committed
917
		n = a;
918

Michael Beck's avatar
Michael Beck committed
919
920
921
		DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
	}
	return n;
922
}  /* equivalent_node_left_zero */
923

924
925
926
#define equivalent_node_Shl   equivalent_node_left_zero
#define equivalent_node_Shr   equivalent_node_left_zero
#define equivalent_node_Shrs  equivalent_node_left_zero
927
#define equivalent_node_Rotl  equivalent_node_left_zero
928

929
930
931
932
933
/**
 * 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 :-).
934
935
936
 *
 * 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.
937
 */
938
939
static ir_node *equivalent_node_Sub(ir_node *n)
{
Matthias Braun's avatar
Matthias Braun committed
940
941
942
943
	ir_node   *oldn = n;
	ir_node   *b;
	ir_mode   *mode = get_irn_mode(n);
	ir_tarval *tb;
Michael Beck's avatar
Michael Beck committed
944
945

	/* for FP these optimizations are only allowed if fp_strict_algebraic is disabled */
946
947
948
949
950
	if (mode_is_float(mode)) {
		ir_graph *irg = get_irn_irg(n);
		if (get_irg_fp_model(irg) & fp_strict_algebraic)
			return n;
	}
Michael Beck's avatar
Michael Beck committed
951

952
953
	b  = get_Sub_right(n);
	tb = value_of(b);
Michael Beck's avatar
Michael Beck committed
954
955

	/* Beware: modes might be different */
956
	if (tarval_is_null(tb)) {
957
		ir_node *a = get_Sub_left(n);
Michael Beck's avatar
Michael Beck committed
958
959
960
961
962
963
964
		if (mode == get_irn_mode(a)) {
			n = a;

			DBG_OPT_ALGSIM1(oldn, a, b, n, FS_OPT_NEUTRAL_0);
		}
	}
	return n;
965
}  /* equivalent_node_Sub */
966
967


968
/**
yb9976's avatar
yb9976 committed
969
 * Optimize an "self-inverse unary op", i.e. op(op(n)) = n.
970
 *
971
972
973
974
 * @todo
 *   -(-a) == a, but might overflow two times.
 *   We handle it anyway here but the better way would be a
 *   flag. This would be needed for Pascal for instance.
975
 */
976
977
static ir_node *equivalent_node_idempotent_unop(ir_node *n)
{
Michael Beck's avatar
Michael Beck committed
978
979
	ir_node *oldn = n;
	ir_node *pred = get_unop_op(n);
980