Ayee, missed a file.
[official-gcc.git] / gcc / fold-const.c
blob0b225c05e029d2d8b65fbebc2830f5396888d6be
1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
10 version.
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
20 02111-1307, USA. */
22 /*@@ This file should be rewritten to use an arbitrary precision
23 @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
24 @@ Perhaps the routines could also be used for bc/dc, and made a lib.
25 @@ The routines that translate from the ap rep should
26 @@ warn if precision et. al. is lost.
27 @@ This would also make life easier when this technology is used
28 @@ for cross-compilers. */
30 /* The entry points in this file are fold, size_int_wide, size_binop
31 and force_fit_type.
33 fold takes a tree as argument and returns a simplified tree.
35 size_binop takes a tree code for an arithmetic operation
36 and two operands that are trees, and produces a tree for the
37 result, assuming the type comes from `sizetype'.
39 size_int takes an integer value, and creates a tree constant
40 with type from `sizetype'.
42 force_fit_type takes a constant, an overflowable flag and prior
43 overflow indicators. It forces the value to fit the type and sets
44 TREE_OVERFLOW and TREE_CONSTANT_OVERFLOW as appropriate. */
46 #include "config.h"
47 #include "system.h"
48 #include "coretypes.h"
49 #include "tm.h"
50 #include "flags.h"
51 #include "tree.h"
52 #include "real.h"
53 #include "rtl.h"
54 #include "expr.h"
55 #include "tm_p.h"
56 #include "toplev.h"
57 #include "ggc.h"
58 #include "hashtab.h"
59 #include "langhooks.h"
60 #include "md5.h"
62 /* The following constants represent a bit based encoding of GCC's
63 comparison operators. This encoding simplifies transformations
64 on relational comparison operators, such as AND and OR. */
65 enum comparison_code {
66 COMPCODE_FALSE = 0,
67 COMPCODE_LT = 1,
68 COMPCODE_EQ = 2,
69 COMPCODE_LE = 3,
70 COMPCODE_GT = 4,
71 COMPCODE_LTGT = 5,
72 COMPCODE_GE = 6,
73 COMPCODE_ORD = 7,
74 COMPCODE_UNORD = 8,
75 COMPCODE_UNLT = 9,
76 COMPCODE_UNEQ = 10,
77 COMPCODE_UNLE = 11,
78 COMPCODE_UNGT = 12,
79 COMPCODE_NE = 13,
80 COMPCODE_UNGE = 14,
81 COMPCODE_TRUE = 15
84 static void encode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT, HOST_WIDE_INT);
85 static void decode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT *, HOST_WIDE_INT *);
86 static bool negate_mathfn_p (enum built_in_function);
87 static bool negate_expr_p (tree);
88 static tree negate_expr (tree);
89 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
90 static tree associate_trees (tree, tree, enum tree_code, tree);
91 static tree const_binop (enum tree_code, tree, tree, int);
92 static hashval_t size_htab_hash (const void *);
93 static int size_htab_eq (const void *, const void *);
94 static tree fold_convert_const (enum tree_code, tree, tree);
95 static enum tree_code invert_tree_comparison (enum tree_code, bool);
96 static enum comparison_code comparison_to_compcode (enum tree_code);
97 static enum tree_code compcode_to_comparison (enum comparison_code);
98 static tree combine_comparisons (enum tree_code, enum tree_code,
99 enum tree_code, tree, tree, tree);
100 static int truth_value_p (enum tree_code);
101 static int operand_equal_for_comparison_p (tree, tree, tree);
102 static int twoval_comparison_p (tree, tree *, tree *, int *);
103 static tree eval_subst (tree, tree, tree, tree, tree);
104 static tree pedantic_omit_one_operand (tree, tree, tree);
105 static tree distribute_bit_expr (enum tree_code, tree, tree, tree);
106 static tree make_bit_field_ref (tree, tree, int, int, int);
107 static tree optimize_bit_field_compare (enum tree_code, tree, tree, tree);
108 static tree decode_field_reference (tree, HOST_WIDE_INT *, HOST_WIDE_INT *,
109 enum machine_mode *, int *, int *,
110 tree *, tree *);
111 static int all_ones_mask_p (tree, int);
112 static tree sign_bit_p (tree, tree);
113 static int simple_operand_p (tree);
114 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
115 static tree make_range (tree, int *, tree *, tree *);
116 static tree build_range_check (tree, tree, int, tree, tree);
117 static int merge_ranges (int *, tree *, tree *, int, tree, tree, int, tree,
118 tree);
119 static tree fold_range_test (tree);
120 static tree fold_cond_expr_with_comparison (tree, tree, tree, tree);
121 static tree unextend (tree, int, int, tree);
122 static tree fold_truthop (enum tree_code, tree, tree, tree);
123 static tree optimize_minmax_comparison (tree);
124 static tree extract_muldiv (tree, tree, enum tree_code, tree);
125 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree);
126 static int multiple_of_p (tree, tree, tree);
127 static tree fold_binary_op_with_conditional_arg (enum tree_code, tree, tree,
128 tree, int);
129 static bool fold_real_zero_addition_p (tree, tree, int);
130 static tree fold_mathfn_compare (enum built_in_function, enum tree_code,
131 tree, tree, tree);
132 static tree fold_inf_compare (enum tree_code, tree, tree, tree);
133 static tree fold_div_compare (enum tree_code, tree, tree, tree);
134 static bool reorder_operands_p (tree, tree);
135 static tree fold_negate_const (tree, tree);
136 static tree fold_not_const (tree, tree);
137 static tree fold_relational_const (enum tree_code, tree, tree, tree);
138 static tree fold_relational_hi_lo (enum tree_code *, const tree,
139 tree *, tree *);
140 static bool tree_expr_nonzero_p (tree);
142 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
143 overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
144 and SUM1. Then this yields nonzero if overflow occurred during the
145 addition.
147 Overflow occurs if A and B have the same sign, but A and SUM differ in
148 sign. Use `^' to test whether signs differ, and `< 0' to isolate the
149 sign. */
150 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
152 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
153 We do that by representing the two-word integer in 4 words, with only
154 HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
155 number. The value of the word is LOWPART + HIGHPART * BASE. */
157 #define LOWPART(x) \
158 ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
159 #define HIGHPART(x) \
160 ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
161 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
163 /* Unpack a two-word integer into 4 words.
164 LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
165 WORDS points to the array of HOST_WIDE_INTs. */
167 static void
168 encode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
170 words[0] = LOWPART (low);
171 words[1] = HIGHPART (low);
172 words[2] = LOWPART (hi);
173 words[3] = HIGHPART (hi);
176 /* Pack an array of 4 words into a two-word integer.
177 WORDS points to the array of words.
178 The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */
180 static void
181 decode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT *low,
182 HOST_WIDE_INT *hi)
184 *low = words[0] + words[1] * BASE;
185 *hi = words[2] + words[3] * BASE;
188 /* T is an INT_CST node. OVERFLOWABLE indicates if we are interested
189 in overflow of the value, when >0 we are only interested in signed
190 overflow, for <0 we are interested in any overflow. OVERFLOWED
191 indicates whether overflow has already occurred. CONST_OVERFLOWED
192 indicates whether constant overflow has already occurred. We force
193 T's value to be within range of T's type (by setting to 0 or 1 all
194 the bits outside the type's range). We set TREE_OVERFLOWED if,
195 OVERFLOWED is non-zero,
196 or OVERFLOWABLE is >0 and signed overflow occurs
197 or OVERFLOWABLE is <0 and any overflow occurs
198 We set TREE_CONSTANT_OVERFLOWED if,
199 CONST_OVERFLOWED is non-zero
200 or we set TREE_OVERFLOWED.
201 We return either the original T, or a copy. */
203 tree
204 force_fit_type (tree t, int overflowable, bool overflowed, bool overflowed_const)
206 unsigned HOST_WIDE_INT low;
207 HOST_WIDE_INT high;
208 unsigned int prec;
209 int sign_extended_type;
211 if (TREE_CODE (t) != INTEGER_CST)
212 abort ();
214 low = TREE_INT_CST_LOW (t);
215 high = TREE_INT_CST_HIGH (t);
217 if (POINTER_TYPE_P (TREE_TYPE (t))
218 || TREE_CODE (TREE_TYPE (t)) == OFFSET_TYPE)
219 prec = POINTER_SIZE;
220 else
221 prec = TYPE_PRECISION (TREE_TYPE (t));
222 /* Size types *are* sign extended. */
223 sign_extended_type = (!TYPE_UNSIGNED (TREE_TYPE (t))
224 || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
225 && TYPE_IS_SIZETYPE (TREE_TYPE (t))));
227 /* First clear all bits that are beyond the type's precision. */
229 if (prec == 2 * HOST_BITS_PER_WIDE_INT)
231 else if (prec > HOST_BITS_PER_WIDE_INT)
232 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
233 else
235 high = 0;
236 if (prec < HOST_BITS_PER_WIDE_INT)
237 low &= ~((HOST_WIDE_INT) (-1) << prec);
240 if (!sign_extended_type)
241 /* No sign extension */;
242 else if (prec == 2 * HOST_BITS_PER_WIDE_INT)
243 /* Correct width already. */;
244 else if (prec > HOST_BITS_PER_WIDE_INT)
246 /* Sign extend top half? */
247 if (high & ((unsigned HOST_WIDE_INT)1
248 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
249 high |= (HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT);
251 else if (prec == HOST_BITS_PER_WIDE_INT)
253 if ((HOST_WIDE_INT)low < 0)
254 high = -1;
256 else
258 /* Sign extend bottom half? */
259 if (low & ((unsigned HOST_WIDE_INT)1 << (prec - 1)))
261 high = -1;
262 low |= (HOST_WIDE_INT)(-1) << prec;
266 /* If the value changed, return a new node. */
267 if (overflowed || overflowed_const
268 || low != TREE_INT_CST_LOW (t) || high != TREE_INT_CST_HIGH (t))
270 if (overflowed
271 || overflowable < 0
272 || (overflowable > 0 && sign_extended_type))
274 TREE_OVERFLOW (t) = 1;
275 TREE_CONSTANT_OVERFLOW (t) = 1;
277 else if (overflowed_const)
278 TREE_CONSTANT_OVERFLOW (t) = 1;
280 TREE_INT_CST_LOW (t) = low;
281 TREE_INT_CST_HIGH (t) = high;
284 return t;
287 /* Add two doubleword integers with doubleword result.
288 Each argument is given as two `HOST_WIDE_INT' pieces.
289 One argument is L1 and H1; the other, L2 and H2.
290 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
293 add_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
294 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
295 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
297 unsigned HOST_WIDE_INT l;
298 HOST_WIDE_INT h;
300 l = l1 + l2;
301 h = h1 + h2 + (l < l1);
303 *lv = l;
304 *hv = h;
305 return OVERFLOW_SUM_SIGN (h1, h2, h);
308 /* Negate a doubleword integer with doubleword result.
309 Return nonzero if the operation overflows, assuming it's signed.
310 The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
311 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
314 neg_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
315 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
317 if (l1 == 0)
319 *lv = 0;
320 *hv = - h1;
321 return (*hv & h1) < 0;
323 else
325 *lv = -l1;
326 *hv = ~h1;
327 return 0;
331 /* Multiply two doubleword integers with doubleword result.
332 Return nonzero if the operation overflows, assuming it's signed.
333 Each argument is given as two `HOST_WIDE_INT' pieces.
334 One argument is L1 and H1; the other, L2 and H2.
335 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
338 mul_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
339 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
340 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
342 HOST_WIDE_INT arg1[4];
343 HOST_WIDE_INT arg2[4];
344 HOST_WIDE_INT prod[4 * 2];
345 unsigned HOST_WIDE_INT carry;
346 int i, j, k;
347 unsigned HOST_WIDE_INT toplow, neglow;
348 HOST_WIDE_INT tophigh, neghigh;
350 encode (arg1, l1, h1);
351 encode (arg2, l2, h2);
353 memset (prod, 0, sizeof prod);
355 for (i = 0; i < 4; i++)
357 carry = 0;
358 for (j = 0; j < 4; j++)
360 k = i + j;
361 /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */
362 carry += arg1[i] * arg2[j];
363 /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */
364 carry += prod[k];
365 prod[k] = LOWPART (carry);
366 carry = HIGHPART (carry);
368 prod[i + 4] = carry;
371 decode (prod, lv, hv); /* This ignores prod[4] through prod[4*2-1] */
373 /* Check for overflow by calculating the top half of the answer in full;
374 it should agree with the low half's sign bit. */
375 decode (prod + 4, &toplow, &tophigh);
376 if (h1 < 0)
378 neg_double (l2, h2, &neglow, &neghigh);
379 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
381 if (h2 < 0)
383 neg_double (l1, h1, &neglow, &neghigh);
384 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
386 return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
389 /* Shift the doubleword integer in L1, H1 left by COUNT places
390 keeping only PREC bits of result.
391 Shift right if COUNT is negative.
392 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
393 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
395 void
396 lshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
397 HOST_WIDE_INT count, unsigned int prec,
398 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, int arith)
400 unsigned HOST_WIDE_INT signmask;
402 if (count < 0)
404 rshift_double (l1, h1, -count, prec, lv, hv, arith);
405 return;
408 if (SHIFT_COUNT_TRUNCATED)
409 count %= prec;
411 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
413 /* Shifting by the host word size is undefined according to the
414 ANSI standard, so we must handle this as a special case. */
415 *hv = 0;
416 *lv = 0;
418 else if (count >= HOST_BITS_PER_WIDE_INT)
420 *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
421 *lv = 0;
423 else
425 *hv = (((unsigned HOST_WIDE_INT) h1 << count)
426 | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
427 *lv = l1 << count;
430 /* Sign extend all bits that are beyond the precision. */
432 signmask = -((prec > HOST_BITS_PER_WIDE_INT
433 ? ((unsigned HOST_WIDE_INT) *hv
434 >> (prec - HOST_BITS_PER_WIDE_INT - 1))
435 : (*lv >> (prec - 1))) & 1);
437 if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
439 else if (prec >= HOST_BITS_PER_WIDE_INT)
441 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
442 *hv |= signmask << (prec - HOST_BITS_PER_WIDE_INT);
444 else
446 *hv = signmask;
447 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
448 *lv |= signmask << prec;
452 /* Shift the doubleword integer in L1, H1 right by COUNT places
453 keeping only PREC bits of result. COUNT must be positive.
454 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
455 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
457 void
458 rshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
459 HOST_WIDE_INT count, unsigned int prec,
460 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
461 int arith)
463 unsigned HOST_WIDE_INT signmask;
465 signmask = (arith
466 ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
467 : 0);
469 if (SHIFT_COUNT_TRUNCATED)
470 count %= prec;
472 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
474 /* Shifting by the host word size is undefined according to the
475 ANSI standard, so we must handle this as a special case. */
476 *hv = 0;
477 *lv = 0;
479 else if (count >= HOST_BITS_PER_WIDE_INT)
481 *hv = 0;
482 *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
484 else
486 *hv = (unsigned HOST_WIDE_INT) h1 >> count;
487 *lv = ((l1 >> count)
488 | ((unsigned HOST_WIDE_INT) h1 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
491 /* Zero / sign extend all bits that are beyond the precision. */
493 if (count >= (HOST_WIDE_INT)prec)
495 *hv = signmask;
496 *lv = signmask;
498 else if ((prec - count) >= 2 * HOST_BITS_PER_WIDE_INT)
500 else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
502 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - count - HOST_BITS_PER_WIDE_INT));
503 *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
505 else
507 *hv = signmask;
508 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << (prec - count));
509 *lv |= signmask << (prec - count);
513 /* Rotate the doubleword integer in L1, H1 left by COUNT places
514 keeping only PREC bits of result.
515 Rotate right if COUNT is negative.
516 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
518 void
519 lrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
520 HOST_WIDE_INT count, unsigned int prec,
521 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
523 unsigned HOST_WIDE_INT s1l, s2l;
524 HOST_WIDE_INT s1h, s2h;
526 count %= prec;
527 if (count < 0)
528 count += prec;
530 lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
531 rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
532 *lv = s1l | s2l;
533 *hv = s1h | s2h;
536 /* Rotate the doubleword integer in L1, H1 left by COUNT places
537 keeping only PREC bits of result. COUNT must be positive.
538 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
540 void
541 rrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
542 HOST_WIDE_INT count, unsigned int prec,
543 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
545 unsigned HOST_WIDE_INT s1l, s2l;
546 HOST_WIDE_INT s1h, s2h;
548 count %= prec;
549 if (count < 0)
550 count += prec;
552 rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
553 lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
554 *lv = s1l | s2l;
555 *hv = s1h | s2h;
558 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
559 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
560 CODE is a tree code for a kind of division, one of
561 TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
562 or EXACT_DIV_EXPR
563 It controls how the quotient is rounded to an integer.
564 Return nonzero if the operation overflows.
565 UNS nonzero says do unsigned division. */
568 div_and_round_double (enum tree_code code, int uns,
569 unsigned HOST_WIDE_INT lnum_orig, /* num == numerator == dividend */
570 HOST_WIDE_INT hnum_orig,
571 unsigned HOST_WIDE_INT lden_orig, /* den == denominator == divisor */
572 HOST_WIDE_INT hden_orig,
573 unsigned HOST_WIDE_INT *lquo,
574 HOST_WIDE_INT *hquo, unsigned HOST_WIDE_INT *lrem,
575 HOST_WIDE_INT *hrem)
577 int quo_neg = 0;
578 HOST_WIDE_INT num[4 + 1]; /* extra element for scaling. */
579 HOST_WIDE_INT den[4], quo[4];
580 int i, j;
581 unsigned HOST_WIDE_INT work;
582 unsigned HOST_WIDE_INT carry = 0;
583 unsigned HOST_WIDE_INT lnum = lnum_orig;
584 HOST_WIDE_INT hnum = hnum_orig;
585 unsigned HOST_WIDE_INT lden = lden_orig;
586 HOST_WIDE_INT hden = hden_orig;
587 int overflow = 0;
589 if (hden == 0 && lden == 0)
590 overflow = 1, lden = 1;
592 /* Calculate quotient sign and convert operands to unsigned. */
593 if (!uns)
595 if (hnum < 0)
597 quo_neg = ~ quo_neg;
598 /* (minimum integer) / (-1) is the only overflow case. */
599 if (neg_double (lnum, hnum, &lnum, &hnum)
600 && ((HOST_WIDE_INT) lden & hden) == -1)
601 overflow = 1;
603 if (hden < 0)
605 quo_neg = ~ quo_neg;
606 neg_double (lden, hden, &lden, &hden);
610 if (hnum == 0 && hden == 0)
611 { /* single precision */
612 *hquo = *hrem = 0;
613 /* This unsigned division rounds toward zero. */
614 *lquo = lnum / lden;
615 goto finish_up;
618 if (hnum == 0)
619 { /* trivial case: dividend < divisor */
620 /* hden != 0 already checked. */
621 *hquo = *lquo = 0;
622 *hrem = hnum;
623 *lrem = lnum;
624 goto finish_up;
627 memset (quo, 0, sizeof quo);
629 memset (num, 0, sizeof num); /* to zero 9th element */
630 memset (den, 0, sizeof den);
632 encode (num, lnum, hnum);
633 encode (den, lden, hden);
635 /* Special code for when the divisor < BASE. */
636 if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
638 /* hnum != 0 already checked. */
639 for (i = 4 - 1; i >= 0; i--)
641 work = num[i] + carry * BASE;
642 quo[i] = work / lden;
643 carry = work % lden;
646 else
648 /* Full double precision division,
649 with thanks to Don Knuth's "Seminumerical Algorithms". */
650 int num_hi_sig, den_hi_sig;
651 unsigned HOST_WIDE_INT quo_est, scale;
653 /* Find the highest nonzero divisor digit. */
654 for (i = 4 - 1;; i--)
655 if (den[i] != 0)
657 den_hi_sig = i;
658 break;
661 /* Insure that the first digit of the divisor is at least BASE/2.
662 This is required by the quotient digit estimation algorithm. */
664 scale = BASE / (den[den_hi_sig] + 1);
665 if (scale > 1)
666 { /* scale divisor and dividend */
667 carry = 0;
668 for (i = 0; i <= 4 - 1; i++)
670 work = (num[i] * scale) + carry;
671 num[i] = LOWPART (work);
672 carry = HIGHPART (work);
675 num[4] = carry;
676 carry = 0;
677 for (i = 0; i <= 4 - 1; i++)
679 work = (den[i] * scale) + carry;
680 den[i] = LOWPART (work);
681 carry = HIGHPART (work);
682 if (den[i] != 0) den_hi_sig = i;
686 num_hi_sig = 4;
688 /* Main loop */
689 for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
691 /* Guess the next quotient digit, quo_est, by dividing the first
692 two remaining dividend digits by the high order quotient digit.
693 quo_est is never low and is at most 2 high. */
694 unsigned HOST_WIDE_INT tmp;
696 num_hi_sig = i + den_hi_sig + 1;
697 work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
698 if (num[num_hi_sig] != den[den_hi_sig])
699 quo_est = work / den[den_hi_sig];
700 else
701 quo_est = BASE - 1;
703 /* Refine quo_est so it's usually correct, and at most one high. */
704 tmp = work - quo_est * den[den_hi_sig];
705 if (tmp < BASE
706 && (den[den_hi_sig - 1] * quo_est
707 > (tmp * BASE + num[num_hi_sig - 2])))
708 quo_est--;
710 /* Try QUO_EST as the quotient digit, by multiplying the
711 divisor by QUO_EST and subtracting from the remaining dividend.
712 Keep in mind that QUO_EST is the I - 1st digit. */
714 carry = 0;
715 for (j = 0; j <= den_hi_sig; j++)
717 work = quo_est * den[j] + carry;
718 carry = HIGHPART (work);
719 work = num[i + j] - LOWPART (work);
720 num[i + j] = LOWPART (work);
721 carry += HIGHPART (work) != 0;
724 /* If quo_est was high by one, then num[i] went negative and
725 we need to correct things. */
726 if (num[num_hi_sig] < (HOST_WIDE_INT) carry)
728 quo_est--;
729 carry = 0; /* add divisor back in */
730 for (j = 0; j <= den_hi_sig; j++)
732 work = num[i + j] + den[j] + carry;
733 carry = HIGHPART (work);
734 num[i + j] = LOWPART (work);
737 num [num_hi_sig] += carry;
740 /* Store the quotient digit. */
741 quo[i] = quo_est;
745 decode (quo, lquo, hquo);
747 finish_up:
748 /* If result is negative, make it so. */
749 if (quo_neg)
750 neg_double (*lquo, *hquo, lquo, hquo);
752 /* Compute trial remainder: rem = num - (quo * den) */
753 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
754 neg_double (*lrem, *hrem, lrem, hrem);
755 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
757 switch (code)
759 case TRUNC_DIV_EXPR:
760 case TRUNC_MOD_EXPR: /* round toward zero */
761 case EXACT_DIV_EXPR: /* for this one, it shouldn't matter */
762 return overflow;
764 case FLOOR_DIV_EXPR:
765 case FLOOR_MOD_EXPR: /* round toward negative infinity */
766 if (quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio < 0 && rem != 0 */
768 /* quo = quo - 1; */
769 add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1,
770 lquo, hquo);
772 else
773 return overflow;
774 break;
776 case CEIL_DIV_EXPR:
777 case CEIL_MOD_EXPR: /* round toward positive infinity */
778 if (!quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio > 0 && rem != 0 */
780 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
781 lquo, hquo);
783 else
784 return overflow;
785 break;
787 case ROUND_DIV_EXPR:
788 case ROUND_MOD_EXPR: /* round to closest integer */
790 unsigned HOST_WIDE_INT labs_rem = *lrem;
791 HOST_WIDE_INT habs_rem = *hrem;
792 unsigned HOST_WIDE_INT labs_den = lden, ltwice;
793 HOST_WIDE_INT habs_den = hden, htwice;
795 /* Get absolute values. */
796 if (*hrem < 0)
797 neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
798 if (hden < 0)
799 neg_double (lden, hden, &labs_den, &habs_den);
801 /* If (2 * abs (lrem) >= abs (lden)) */
802 mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
803 labs_rem, habs_rem, &ltwice, &htwice);
805 if (((unsigned HOST_WIDE_INT) habs_den
806 < (unsigned HOST_WIDE_INT) htwice)
807 || (((unsigned HOST_WIDE_INT) habs_den
808 == (unsigned HOST_WIDE_INT) htwice)
809 && (labs_den < ltwice)))
811 if (*hquo < 0)
812 /* quo = quo - 1; */
813 add_double (*lquo, *hquo,
814 (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
815 else
816 /* quo = quo + 1; */
817 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
818 lquo, hquo);
820 else
821 return overflow;
823 break;
825 default:
826 abort ();
829 /* Compute true remainder: rem = num - (quo * den) */
830 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
831 neg_double (*lrem, *hrem, lrem, hrem);
832 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
833 return overflow;
836 /* Return true if built-in mathematical function specified by CODE
837 preserves the sign of it argument, i.e. -f(x) == f(-x). */
839 static bool
840 negate_mathfn_p (enum built_in_function code)
842 switch (code)
844 case BUILT_IN_ASIN:
845 case BUILT_IN_ASINF:
846 case BUILT_IN_ASINL:
847 case BUILT_IN_ATAN:
848 case BUILT_IN_ATANF:
849 case BUILT_IN_ATANL:
850 case BUILT_IN_SIN:
851 case BUILT_IN_SINF:
852 case BUILT_IN_SINL:
853 case BUILT_IN_TAN:
854 case BUILT_IN_TANF:
855 case BUILT_IN_TANL:
856 return true;
858 default:
859 break;
861 return false;
864 /* Determine whether an expression T can be cheaply negated using
865 the function negate_expr. */
867 static bool
868 negate_expr_p (tree t)
870 unsigned HOST_WIDE_INT val;
871 unsigned int prec;
872 tree type;
874 if (t == 0)
875 return false;
877 type = TREE_TYPE (t);
879 STRIP_SIGN_NOPS (t);
880 switch (TREE_CODE (t))
882 case INTEGER_CST:
883 if (TYPE_UNSIGNED (type) || ! flag_trapv)
884 return true;
886 /* Check that -CST will not overflow type. */
887 prec = TYPE_PRECISION (type);
888 if (prec > HOST_BITS_PER_WIDE_INT)
890 if (TREE_INT_CST_LOW (t) != 0)
891 return true;
892 prec -= HOST_BITS_PER_WIDE_INT;
893 val = TREE_INT_CST_HIGH (t);
895 else
896 val = TREE_INT_CST_LOW (t);
897 if (prec < HOST_BITS_PER_WIDE_INT)
898 val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
899 return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
901 case REAL_CST:
902 case NEGATE_EXPR:
903 return true;
905 case COMPLEX_CST:
906 return negate_expr_p (TREE_REALPART (t))
907 && negate_expr_p (TREE_IMAGPART (t));
909 case PLUS_EXPR:
910 if (FLOAT_TYPE_P (type) && !flag_unsafe_math_optimizations)
911 return false;
912 /* -(A + B) -> (-B) - A. */
913 if (negate_expr_p (TREE_OPERAND (t, 1))
914 && reorder_operands_p (TREE_OPERAND (t, 0),
915 TREE_OPERAND (t, 1)))
916 return true;
917 /* -(A + B) -> (-A) - B. */
918 return negate_expr_p (TREE_OPERAND (t, 0));
920 case MINUS_EXPR:
921 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
922 return (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
923 && reorder_operands_p (TREE_OPERAND (t, 0),
924 TREE_OPERAND (t, 1));
926 case MULT_EXPR:
927 if (TYPE_UNSIGNED (TREE_TYPE (t)))
928 break;
930 /* Fall through. */
932 case RDIV_EXPR:
933 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
934 return negate_expr_p (TREE_OPERAND (t, 1))
935 || negate_expr_p (TREE_OPERAND (t, 0));
936 break;
938 case NOP_EXPR:
939 /* Negate -((double)float) as (double)(-float). */
940 if (TREE_CODE (type) == REAL_TYPE)
942 tree tem = strip_float_extensions (t);
943 if (tem != t)
944 return negate_expr_p (tem);
946 break;
948 case CALL_EXPR:
949 /* Negate -f(x) as f(-x). */
950 if (negate_mathfn_p (builtin_mathfn_code (t)))
951 return negate_expr_p (TREE_VALUE (TREE_OPERAND (t, 1)));
952 break;
954 case RSHIFT_EXPR:
955 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
956 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
958 tree op1 = TREE_OPERAND (t, 1);
959 if (TREE_INT_CST_HIGH (op1) == 0
960 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
961 == TREE_INT_CST_LOW (op1))
962 return true;
964 break;
966 default:
967 break;
969 return false;
972 /* Given T, an expression, return the negation of T. Allow for T to be
973 null, in which case return null. */
975 static tree
976 negate_expr (tree t)
978 tree type;
979 tree tem;
981 if (t == 0)
982 return 0;
984 type = TREE_TYPE (t);
985 STRIP_SIGN_NOPS (t);
987 switch (TREE_CODE (t))
989 case INTEGER_CST:
990 tem = fold_negate_const (t, type);
991 if (! TREE_OVERFLOW (tem)
992 || TYPE_UNSIGNED (type)
993 || ! flag_trapv)
994 return tem;
995 break;
997 case REAL_CST:
998 tem = fold_negate_const (t, type);
999 /* Two's complement FP formats, such as c4x, may overflow. */
1000 if (! TREE_OVERFLOW (tem) || ! flag_trapping_math)
1001 return fold_convert (type, tem);
1002 break;
1004 case COMPLEX_CST:
1006 tree rpart = negate_expr (TREE_REALPART (t));
1007 tree ipart = negate_expr (TREE_IMAGPART (t));
1009 if ((TREE_CODE (rpart) == REAL_CST
1010 && TREE_CODE (ipart) == REAL_CST)
1011 || (TREE_CODE (rpart) == INTEGER_CST
1012 && TREE_CODE (ipart) == INTEGER_CST))
1013 return build_complex (type, rpart, ipart);
1015 break;
1017 case NEGATE_EXPR:
1018 return fold_convert (type, TREE_OPERAND (t, 0));
1020 case PLUS_EXPR:
1021 if (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
1023 /* -(A + B) -> (-B) - A. */
1024 if (negate_expr_p (TREE_OPERAND (t, 1))
1025 && reorder_operands_p (TREE_OPERAND (t, 0),
1026 TREE_OPERAND (t, 1)))
1028 tem = negate_expr (TREE_OPERAND (t, 1));
1029 tem = fold (build2 (MINUS_EXPR, TREE_TYPE (t),
1030 tem, TREE_OPERAND (t, 0)));
1031 return fold_convert (type, tem);
1034 /* -(A + B) -> (-A) - B. */
1035 if (negate_expr_p (TREE_OPERAND (t, 0)))
1037 tem = negate_expr (TREE_OPERAND (t, 0));
1038 tem = fold (build2 (MINUS_EXPR, TREE_TYPE (t),
1039 tem, TREE_OPERAND (t, 1)));
1040 return fold_convert (type, tem);
1043 break;
1045 case MINUS_EXPR:
1046 /* - (A - B) -> B - A */
1047 if ((! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
1048 && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
1049 return fold_convert (type,
1050 fold (build2 (MINUS_EXPR, TREE_TYPE (t),
1051 TREE_OPERAND (t, 1),
1052 TREE_OPERAND (t, 0))));
1053 break;
1055 case MULT_EXPR:
1056 if (TYPE_UNSIGNED (TREE_TYPE (t)))
1057 break;
1059 /* Fall through. */
1061 case RDIV_EXPR:
1062 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
1064 tem = TREE_OPERAND (t, 1);
1065 if (negate_expr_p (tem))
1066 return fold_convert (type,
1067 fold (build2 (TREE_CODE (t), TREE_TYPE (t),
1068 TREE_OPERAND (t, 0),
1069 negate_expr (tem))));
1070 tem = TREE_OPERAND (t, 0);
1071 if (negate_expr_p (tem))
1072 return fold_convert (type,
1073 fold (build2 (TREE_CODE (t), TREE_TYPE (t),
1074 negate_expr (tem),
1075 TREE_OPERAND (t, 1))));
1077 break;
1079 case NOP_EXPR:
1080 /* Convert -((double)float) into (double)(-float). */
1081 if (TREE_CODE (type) == REAL_TYPE)
1083 tem = strip_float_extensions (t);
1084 if (tem != t && negate_expr_p (tem))
1085 return fold_convert (type, negate_expr (tem));
1087 break;
1089 case CALL_EXPR:
1090 /* Negate -f(x) as f(-x). */
1091 if (negate_mathfn_p (builtin_mathfn_code (t))
1092 && negate_expr_p (TREE_VALUE (TREE_OPERAND (t, 1))))
1094 tree fndecl, arg, arglist;
1096 fndecl = get_callee_fndecl (t);
1097 arg = negate_expr (TREE_VALUE (TREE_OPERAND (t, 1)));
1098 arglist = build_tree_list (NULL_TREE, arg);
1099 return build_function_call_expr (fndecl, arglist);
1101 break;
1103 case RSHIFT_EXPR:
1104 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
1105 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1107 tree op1 = TREE_OPERAND (t, 1);
1108 if (TREE_INT_CST_HIGH (op1) == 0
1109 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1110 == TREE_INT_CST_LOW (op1))
1112 tree ntype = TYPE_UNSIGNED (type)
1113 ? lang_hooks.types.signed_type (type)
1114 : lang_hooks.types.unsigned_type (type);
1115 tree temp = fold_convert (ntype, TREE_OPERAND (t, 0));
1116 temp = fold (build2 (RSHIFT_EXPR, ntype, temp, op1));
1117 return fold_convert (type, temp);
1120 break;
1122 default:
1123 break;
1126 tem = fold (build1 (NEGATE_EXPR, TREE_TYPE (t), t));
1127 return fold_convert (type, tem);
1130 /* Split a tree IN into a constant, literal and variable parts that could be
1131 combined with CODE to make IN. "constant" means an expression with
1132 TREE_CONSTANT but that isn't an actual constant. CODE must be a
1133 commutative arithmetic operation. Store the constant part into *CONP,
1134 the literal in *LITP and return the variable part. If a part isn't
1135 present, set it to null. If the tree does not decompose in this way,
1136 return the entire tree as the variable part and the other parts as null.
1138 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
1139 case, we negate an operand that was subtracted. Except if it is a
1140 literal for which we use *MINUS_LITP instead.
1142 If NEGATE_P is true, we are negating all of IN, again except a literal
1143 for which we use *MINUS_LITP instead.
1145 If IN is itself a literal or constant, return it as appropriate.
1147 Note that we do not guarantee that any of the three values will be the
1148 same type as IN, but they will have the same signedness and mode. */
1150 static tree
1151 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
1152 tree *minus_litp, int negate_p)
1154 tree var = 0;
1156 *conp = 0;
1157 *litp = 0;
1158 *minus_litp = 0;
1160 /* Strip any conversions that don't change the machine mode or signedness. */
1161 STRIP_SIGN_NOPS (in);
1163 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST)
1164 *litp = in;
1165 else if (TREE_CODE (in) == code
1166 || (! FLOAT_TYPE_P (TREE_TYPE (in))
1167 /* We can associate addition and subtraction together (even
1168 though the C standard doesn't say so) for integers because
1169 the value is not affected. For reals, the value might be
1170 affected, so we can't. */
1171 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1172 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1174 tree op0 = TREE_OPERAND (in, 0);
1175 tree op1 = TREE_OPERAND (in, 1);
1176 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1177 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1179 /* First see if either of the operands is a literal, then a constant. */
1180 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST)
1181 *litp = op0, op0 = 0;
1182 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST)
1183 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1185 if (op0 != 0 && TREE_CONSTANT (op0))
1186 *conp = op0, op0 = 0;
1187 else if (op1 != 0 && TREE_CONSTANT (op1))
1188 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1190 /* If we haven't dealt with either operand, this is not a case we can
1191 decompose. Otherwise, VAR is either of the ones remaining, if any. */
1192 if (op0 != 0 && op1 != 0)
1193 var = in;
1194 else if (op0 != 0)
1195 var = op0;
1196 else
1197 var = op1, neg_var_p = neg1_p;
1199 /* Now do any needed negations. */
1200 if (neg_litp_p)
1201 *minus_litp = *litp, *litp = 0;
1202 if (neg_conp_p)
1203 *conp = negate_expr (*conp);
1204 if (neg_var_p)
1205 var = negate_expr (var);
1207 else if (TREE_CONSTANT (in))
1208 *conp = in;
1209 else
1210 var = in;
1212 if (negate_p)
1214 if (*litp)
1215 *minus_litp = *litp, *litp = 0;
1216 else if (*minus_litp)
1217 *litp = *minus_litp, *minus_litp = 0;
1218 *conp = negate_expr (*conp);
1219 var = negate_expr (var);
1222 return var;
1225 /* Re-associate trees split by the above function. T1 and T2 are either
1226 expressions to associate or null. Return the new expression, if any. If
1227 we build an operation, do it in TYPE and with CODE. */
1229 static tree
1230 associate_trees (tree t1, tree t2, enum tree_code code, tree type)
1232 if (t1 == 0)
1233 return t2;
1234 else if (t2 == 0)
1235 return t1;
1237 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1238 try to fold this since we will have infinite recursion. But do
1239 deal with any NEGATE_EXPRs. */
1240 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1241 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1243 if (code == PLUS_EXPR)
1245 if (TREE_CODE (t1) == NEGATE_EXPR)
1246 return build2 (MINUS_EXPR, type, fold_convert (type, t2),
1247 fold_convert (type, TREE_OPERAND (t1, 0)));
1248 else if (TREE_CODE (t2) == NEGATE_EXPR)
1249 return build2 (MINUS_EXPR, type, fold_convert (type, t1),
1250 fold_convert (type, TREE_OPERAND (t2, 0)));
1252 return build2 (code, type, fold_convert (type, t1),
1253 fold_convert (type, t2));
1256 return fold (build2 (code, type, fold_convert (type, t1),
1257 fold_convert (type, t2)));
1260 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1261 to produce a new constant.
1263 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1265 tree
1266 int_const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1268 unsigned HOST_WIDE_INT int1l, int2l;
1269 HOST_WIDE_INT int1h, int2h;
1270 unsigned HOST_WIDE_INT low;
1271 HOST_WIDE_INT hi;
1272 unsigned HOST_WIDE_INT garbagel;
1273 HOST_WIDE_INT garbageh;
1274 tree t;
1275 tree type = TREE_TYPE (arg1);
1276 int uns = TYPE_UNSIGNED (type);
1277 int is_sizetype
1278 = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
1279 int overflow = 0;
1280 int no_overflow = 0;
1282 int1l = TREE_INT_CST_LOW (arg1);
1283 int1h = TREE_INT_CST_HIGH (arg1);
1284 int2l = TREE_INT_CST_LOW (arg2);
1285 int2h = TREE_INT_CST_HIGH (arg2);
1287 switch (code)
1289 case BIT_IOR_EXPR:
1290 low = int1l | int2l, hi = int1h | int2h;
1291 break;
1293 case BIT_XOR_EXPR:
1294 low = int1l ^ int2l, hi = int1h ^ int2h;
1295 break;
1297 case BIT_AND_EXPR:
1298 low = int1l & int2l, hi = int1h & int2h;
1299 break;
1301 case RSHIFT_EXPR:
1302 int2l = -int2l;
1303 case LSHIFT_EXPR:
1304 /* It's unclear from the C standard whether shifts can overflow.
1305 The following code ignores overflow; perhaps a C standard
1306 interpretation ruling is needed. */
1307 lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1308 &low, &hi, !uns);
1309 no_overflow = 1;
1310 break;
1312 case RROTATE_EXPR:
1313 int2l = - int2l;
1314 case LROTATE_EXPR:
1315 lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1316 &low, &hi);
1317 break;
1319 case PLUS_EXPR:
1320 overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1321 break;
1323 case MINUS_EXPR:
1324 neg_double (int2l, int2h, &low, &hi);
1325 add_double (int1l, int1h, low, hi, &low, &hi);
1326 overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
1327 break;
1329 case MULT_EXPR:
1330 overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1331 break;
1333 case TRUNC_DIV_EXPR:
1334 case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1335 case EXACT_DIV_EXPR:
1336 /* This is a shortcut for a common special case. */
1337 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1338 && ! TREE_CONSTANT_OVERFLOW (arg1)
1339 && ! TREE_CONSTANT_OVERFLOW (arg2)
1340 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1342 if (code == CEIL_DIV_EXPR)
1343 int1l += int2l - 1;
1345 low = int1l / int2l, hi = 0;
1346 break;
1349 /* ... fall through ... */
1351 case ROUND_DIV_EXPR:
1352 if (int2h == 0 && int2l == 1)
1354 low = int1l, hi = int1h;
1355 break;
1357 if (int1l == int2l && int1h == int2h
1358 && ! (int1l == 0 && int1h == 0))
1360 low = 1, hi = 0;
1361 break;
1363 overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
1364 &low, &hi, &garbagel, &garbageh);
1365 break;
1367 case TRUNC_MOD_EXPR:
1368 case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1369 /* This is a shortcut for a common special case. */
1370 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1371 && ! TREE_CONSTANT_OVERFLOW (arg1)
1372 && ! TREE_CONSTANT_OVERFLOW (arg2)
1373 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1375 if (code == CEIL_MOD_EXPR)
1376 int1l += int2l - 1;
1377 low = int1l % int2l, hi = 0;
1378 break;
1381 /* ... fall through ... */
1383 case ROUND_MOD_EXPR:
1384 overflow = div_and_round_double (code, uns,
1385 int1l, int1h, int2l, int2h,
1386 &garbagel, &garbageh, &low, &hi);
1387 break;
1389 case MIN_EXPR:
1390 case MAX_EXPR:
1391 if (uns)
1392 low = (((unsigned HOST_WIDE_INT) int1h
1393 < (unsigned HOST_WIDE_INT) int2h)
1394 || (((unsigned HOST_WIDE_INT) int1h
1395 == (unsigned HOST_WIDE_INT) int2h)
1396 && int1l < int2l));
1397 else
1398 low = (int1h < int2h
1399 || (int1h == int2h && int1l < int2l));
1401 if (low == (code == MIN_EXPR))
1402 low = int1l, hi = int1h;
1403 else
1404 low = int2l, hi = int2h;
1405 break;
1407 default:
1408 abort ();
1411 /* If this is for a sizetype, can be represented as one (signed)
1412 HOST_WIDE_INT word, and doesn't overflow, use size_int since it caches
1413 constants. */
1414 if (is_sizetype
1415 && ((hi == 0 && (HOST_WIDE_INT) low >= 0)
1416 || (hi == -1 && (HOST_WIDE_INT) low < 0))
1417 && overflow == 0 && ! TREE_OVERFLOW (arg1) && ! TREE_OVERFLOW (arg2))
1418 return size_int_type (low, type);
1419 else
1421 t = build_int_2 (low, hi);
1422 TREE_TYPE (t) = TREE_TYPE (arg1);
1425 if (notrunc)
1427 /* Propagate overflow flags ourselves. */
1428 if (((!uns || is_sizetype) && overflow)
1429 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1430 TREE_OVERFLOW (t) = 1;
1432 if (TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1)
1433 | TREE_CONSTANT_OVERFLOW (arg2))
1434 TREE_CONSTANT_OVERFLOW (t) = 1;
1436 else
1437 t = force_fit_type (t, 1,
1438 ((!uns || is_sizetype) && overflow)
1439 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2),
1440 TREE_CONSTANT_OVERFLOW (arg1)
1441 | TREE_CONSTANT_OVERFLOW (arg2));
1443 return t;
1446 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1447 constant. We assume ARG1 and ARG2 have the same data type, or at least
1448 are the same kind of constant and the same machine mode.
1450 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1452 static tree
1453 const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1455 STRIP_NOPS (arg1);
1456 STRIP_NOPS (arg2);
1458 if (TREE_CODE (arg1) == INTEGER_CST)
1459 return int_const_binop (code, arg1, arg2, notrunc);
1461 if (TREE_CODE (arg1) == REAL_CST)
1463 enum machine_mode mode;
1464 REAL_VALUE_TYPE d1;
1465 REAL_VALUE_TYPE d2;
1466 REAL_VALUE_TYPE value;
1467 tree t, type;
1469 d1 = TREE_REAL_CST (arg1);
1470 d2 = TREE_REAL_CST (arg2);
1472 type = TREE_TYPE (arg1);
1473 mode = TYPE_MODE (type);
1475 /* Don't perform operation if we honor signaling NaNs and
1476 either operand is a NaN. */
1477 if (HONOR_SNANS (mode)
1478 && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1479 return NULL_TREE;
1481 /* Don't perform operation if it would raise a division
1482 by zero exception. */
1483 if (code == RDIV_EXPR
1484 && REAL_VALUES_EQUAL (d2, dconst0)
1485 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1486 return NULL_TREE;
1488 /* If either operand is a NaN, just return it. Otherwise, set up
1489 for floating-point trap; we return an overflow. */
1490 if (REAL_VALUE_ISNAN (d1))
1491 return arg1;
1492 else if (REAL_VALUE_ISNAN (d2))
1493 return arg2;
1495 REAL_ARITHMETIC (value, code, d1, d2);
1497 t = build_real (type, real_value_truncate (mode, value));
1499 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1500 TREE_CONSTANT_OVERFLOW (t)
1501 = TREE_OVERFLOW (t)
1502 | TREE_CONSTANT_OVERFLOW (arg1)
1503 | TREE_CONSTANT_OVERFLOW (arg2);
1504 return t;
1506 if (TREE_CODE (arg1) == COMPLEX_CST)
1508 tree type = TREE_TYPE (arg1);
1509 tree r1 = TREE_REALPART (arg1);
1510 tree i1 = TREE_IMAGPART (arg1);
1511 tree r2 = TREE_REALPART (arg2);
1512 tree i2 = TREE_IMAGPART (arg2);
1513 tree t;
1515 switch (code)
1517 case PLUS_EXPR:
1518 t = build_complex (type,
1519 const_binop (PLUS_EXPR, r1, r2, notrunc),
1520 const_binop (PLUS_EXPR, i1, i2, notrunc));
1521 break;
1523 case MINUS_EXPR:
1524 t = build_complex (type,
1525 const_binop (MINUS_EXPR, r1, r2, notrunc),
1526 const_binop (MINUS_EXPR, i1, i2, notrunc));
1527 break;
1529 case MULT_EXPR:
1530 t = build_complex (type,
1531 const_binop (MINUS_EXPR,
1532 const_binop (MULT_EXPR,
1533 r1, r2, notrunc),
1534 const_binop (MULT_EXPR,
1535 i1, i2, notrunc),
1536 notrunc),
1537 const_binop (PLUS_EXPR,
1538 const_binop (MULT_EXPR,
1539 r1, i2, notrunc),
1540 const_binop (MULT_EXPR,
1541 i1, r2, notrunc),
1542 notrunc));
1543 break;
1545 case RDIV_EXPR:
1547 tree magsquared
1548 = const_binop (PLUS_EXPR,
1549 const_binop (MULT_EXPR, r2, r2, notrunc),
1550 const_binop (MULT_EXPR, i2, i2, notrunc),
1551 notrunc);
1553 t = build_complex (type,
1554 const_binop
1555 (INTEGRAL_TYPE_P (TREE_TYPE (r1))
1556 ? TRUNC_DIV_EXPR : RDIV_EXPR,
1557 const_binop (PLUS_EXPR,
1558 const_binop (MULT_EXPR, r1, r2,
1559 notrunc),
1560 const_binop (MULT_EXPR, i1, i2,
1561 notrunc),
1562 notrunc),
1563 magsquared, notrunc),
1564 const_binop
1565 (INTEGRAL_TYPE_P (TREE_TYPE (r1))
1566 ? TRUNC_DIV_EXPR : RDIV_EXPR,
1567 const_binop (MINUS_EXPR,
1568 const_binop (MULT_EXPR, i1, r2,
1569 notrunc),
1570 const_binop (MULT_EXPR, r1, i2,
1571 notrunc),
1572 notrunc),
1573 magsquared, notrunc));
1575 break;
1577 default:
1578 abort ();
1580 return t;
1582 return 0;
1585 /* These are the hash table functions for the hash table of INTEGER_CST
1586 nodes of a sizetype. */
1588 /* Return the hash code code X, an INTEGER_CST. */
1590 static hashval_t
1591 size_htab_hash (const void *x)
1593 tree t = (tree) x;
1595 return (TREE_INT_CST_HIGH (t) ^ TREE_INT_CST_LOW (t)
1596 ^ htab_hash_pointer (TREE_TYPE (t))
1597 ^ (TREE_OVERFLOW (t) << 20));
1600 /* Return nonzero if the value represented by *X (an INTEGER_CST tree node)
1601 is the same as that given by *Y, which is the same. */
1603 static int
1604 size_htab_eq (const void *x, const void *y)
1606 tree xt = (tree) x;
1607 tree yt = (tree) y;
1609 return (TREE_INT_CST_HIGH (xt) == TREE_INT_CST_HIGH (yt)
1610 && TREE_INT_CST_LOW (xt) == TREE_INT_CST_LOW (yt)
1611 && TREE_TYPE (xt) == TREE_TYPE (yt)
1612 && TREE_OVERFLOW (xt) == TREE_OVERFLOW (yt));
1615 /* Return an INTEGER_CST with value whose low-order HOST_BITS_PER_WIDE_INT
1616 bits are given by NUMBER and of the sizetype represented by KIND. */
1618 tree
1619 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1621 return size_int_type (number, sizetype_tab[(int) kind]);
1624 /* Likewise, but the desired type is specified explicitly. */
1626 static GTY (()) tree new_const;
1627 static GTY ((if_marked ("ggc_marked_p"), param_is (union tree_node)))
1628 htab_t size_htab;
1630 tree
1631 size_int_type (HOST_WIDE_INT number, tree type)
1633 void **slot;
1634 unsigned int prec;
1635 HOST_WIDE_INT high;
1636 unsigned HOST_WIDE_INT low;
1638 if (size_htab == 0)
1640 size_htab = htab_create_ggc (1024, size_htab_hash, size_htab_eq, NULL);
1641 new_const = make_node (INTEGER_CST);
1644 /* Adjust NEW_CONST to be the constant we want. If it's already in the
1645 hash table, we return the value from the hash table. Otherwise, we
1646 place that in the hash table and make a new node for the next time. */
1647 prec = TYPE_PRECISION (type);
1648 TREE_TYPE (new_const) = type;
1649 TREE_OVERFLOW (new_const) = TREE_CONSTANT_OVERFLOW (new_const) = 0;
1650 low = number;
1651 if (number >= 0)
1652 high = 0;
1653 else
1655 /* Sizetype IS sign extended. */
1656 high = -1;
1657 if (prec <= HOST_BITS_PER_WIDE_INT)
1658 low |= (HOST_WIDE_INT)(-1) << (prec - 1);
1660 TREE_INT_CST_LOW (new_const) = low;
1661 TREE_INT_CST_HIGH (new_const) = high;
1663 if (low != (unsigned HOST_WIDE_INT)number
1664 || high != (number < 0 ? -1 : 0))
1665 TREE_OVERFLOW (new_const) = TREE_CONSTANT_OVERFLOW (new_const) = 1;
1667 slot = htab_find_slot (size_htab, new_const, INSERT);
1668 if (*slot == 0)
1670 tree t = new_const;
1672 *slot = new_const;
1673 new_const = make_node (INTEGER_CST);
1674 return t;
1676 else
1677 return (tree) *slot;
1680 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1681 is a tree code. The type of the result is taken from the operands.
1682 Both must be the same type integer type and it must be a size type.
1683 If the operands are constant, so is the result. */
1685 tree
1686 size_binop (enum tree_code code, tree arg0, tree arg1)
1688 tree type = TREE_TYPE (arg0);
1690 if (TREE_CODE (type) != INTEGER_TYPE || ! TYPE_IS_SIZETYPE (type)
1691 || type != TREE_TYPE (arg1))
1692 abort ();
1694 /* Handle the special case of two integer constants faster. */
1695 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1697 /* And some specific cases even faster than that. */
1698 if (code == PLUS_EXPR && integer_zerop (arg0))
1699 return arg1;
1700 else if ((code == MINUS_EXPR || code == PLUS_EXPR)
1701 && integer_zerop (arg1))
1702 return arg0;
1703 else if (code == MULT_EXPR && integer_onep (arg0))
1704 return arg1;
1706 /* Handle general case of two integer constants. */
1707 return int_const_binop (code, arg0, arg1, 0);
1710 if (arg0 == error_mark_node || arg1 == error_mark_node)
1711 return error_mark_node;
1713 return fold (build2 (code, type, arg0, arg1));
1716 /* Given two values, either both of sizetype or both of bitsizetype,
1717 compute the difference between the two values. Return the value
1718 in signed type corresponding to the type of the operands. */
1720 tree
1721 size_diffop (tree arg0, tree arg1)
1723 tree type = TREE_TYPE (arg0);
1724 tree ctype;
1726 if (TREE_CODE (type) != INTEGER_TYPE || ! TYPE_IS_SIZETYPE (type)
1727 || type != TREE_TYPE (arg1))
1728 abort ();
1730 /* If the type is already signed, just do the simple thing. */
1731 if (!TYPE_UNSIGNED (type))
1732 return size_binop (MINUS_EXPR, arg0, arg1);
1734 ctype = (type == bitsizetype || type == ubitsizetype
1735 ? sbitsizetype : ssizetype);
1737 /* If either operand is not a constant, do the conversions to the signed
1738 type and subtract. The hardware will do the right thing with any
1739 overflow in the subtraction. */
1740 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1741 return size_binop (MINUS_EXPR, fold_convert (ctype, arg0),
1742 fold_convert (ctype, arg1));
1744 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1745 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1746 overflow) and negate (which can't either). Special-case a result
1747 of zero while we're here. */
1748 if (tree_int_cst_equal (arg0, arg1))
1749 return fold_convert (ctype, integer_zero_node);
1750 else if (tree_int_cst_lt (arg1, arg0))
1751 return fold_convert (ctype, size_binop (MINUS_EXPR, arg0, arg1));
1752 else
1753 return size_binop (MINUS_EXPR, fold_convert (ctype, integer_zero_node),
1754 fold_convert (ctype, size_binop (MINUS_EXPR,
1755 arg1, arg0)));
1759 /* Attempt to fold type conversion operation CODE of expression ARG1 to
1760 type TYPE. If no simplification can be done return NULL_TREE. */
1762 static tree
1763 fold_convert_const (enum tree_code code, tree type, tree arg1)
1765 int overflow = 0;
1766 tree t;
1768 if (TREE_TYPE (arg1) == type)
1769 return arg1;
1771 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
1773 if (TREE_CODE (arg1) == INTEGER_CST)
1775 /* If we would build a constant wider than GCC supports,
1776 leave the conversion unfolded. */
1777 if (TYPE_PRECISION (type) > 2 * HOST_BITS_PER_WIDE_INT)
1778 return NULL_TREE;
1780 /* If we are trying to make a sizetype for a small integer, use
1781 size_int to pick up cached types to reduce duplicate nodes. */
1782 if (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type)
1783 && !TREE_CONSTANT_OVERFLOW (arg1)
1784 && compare_tree_int (arg1, 10000) < 0)
1785 return size_int_type (TREE_INT_CST_LOW (arg1), type);
1787 /* Given an integer constant, make new constant with new type,
1788 appropriately sign-extended or truncated. */
1789 t = build_int_2 (TREE_INT_CST_LOW (arg1),
1790 TREE_INT_CST_HIGH (arg1));
1791 TREE_TYPE (t) = type;
1793 t = force_fit_type (t,
1794 /* Don't set the overflow when
1795 converting a pointer */
1796 !POINTER_TYPE_P (TREE_TYPE (arg1)),
1797 (TREE_INT_CST_HIGH (arg1) < 0
1798 && (TYPE_UNSIGNED (type)
1799 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1800 | TREE_OVERFLOW (arg1),
1801 TREE_CONSTANT_OVERFLOW (arg1));
1802 return t;
1804 else if (TREE_CODE (arg1) == REAL_CST)
1806 /* The following code implements the floating point to integer
1807 conversion rules required by the Java Language Specification,
1808 that IEEE NaNs are mapped to zero and values that overflow
1809 the target precision saturate, i.e. values greater than
1810 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1811 are mapped to INT_MIN. These semantics are allowed by the
1812 C and C++ standards that simply state that the behavior of
1813 FP-to-integer conversion is unspecified upon overflow. */
1815 HOST_WIDE_INT high, low;
1816 REAL_VALUE_TYPE r;
1817 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1819 switch (code)
1821 case FIX_TRUNC_EXPR:
1822 real_trunc (&r, VOIDmode, &x);
1823 break;
1825 case FIX_CEIL_EXPR:
1826 real_ceil (&r, VOIDmode, &x);
1827 break;
1829 case FIX_FLOOR_EXPR:
1830 real_floor (&r, VOIDmode, &x);
1831 break;
1833 case FIX_ROUND_EXPR:
1834 real_round (&r, VOIDmode, &x);
1835 break;
1837 default:
1838 abort ();
1841 /* If R is NaN, return zero and show we have an overflow. */
1842 if (REAL_VALUE_ISNAN (r))
1844 overflow = 1;
1845 high = 0;
1846 low = 0;
1849 /* See if R is less than the lower bound or greater than the
1850 upper bound. */
1852 if (! overflow)
1854 tree lt = TYPE_MIN_VALUE (type);
1855 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1856 if (REAL_VALUES_LESS (r, l))
1858 overflow = 1;
1859 high = TREE_INT_CST_HIGH (lt);
1860 low = TREE_INT_CST_LOW (lt);
1864 if (! overflow)
1866 tree ut = TYPE_MAX_VALUE (type);
1867 if (ut)
1869 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1870 if (REAL_VALUES_LESS (u, r))
1872 overflow = 1;
1873 high = TREE_INT_CST_HIGH (ut);
1874 low = TREE_INT_CST_LOW (ut);
1879 if (! overflow)
1880 REAL_VALUE_TO_INT (&low, &high, r);
1882 t = build_int_2 (low, high);
1883 TREE_TYPE (t) = type;
1885 t = force_fit_type (t, -1, overflow | TREE_OVERFLOW (arg1),
1886 TREE_CONSTANT_OVERFLOW (arg1));
1887 return t;
1890 else if (TREE_CODE (type) == REAL_TYPE)
1892 if (TREE_CODE (arg1) == INTEGER_CST)
1893 return build_real_from_int_cst (type, arg1);
1894 if (TREE_CODE (arg1) == REAL_CST)
1896 if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1)))
1898 /* We make a copy of ARG1 so that we don't modify an
1899 existing constant tree. */
1900 t = copy_node (arg1);
1901 TREE_TYPE (t) = type;
1902 return t;
1905 t = build_real (type,
1906 real_value_truncate (TYPE_MODE (type),
1907 TREE_REAL_CST (arg1)));
1909 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1910 TREE_CONSTANT_OVERFLOW (t)
1911 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
1912 return t;
1915 return NULL_TREE;
1918 /* Convert expression ARG to type TYPE. Used by the middle-end for
1919 simple conversions in preference to calling the front-end's convert. */
1921 tree
1922 fold_convert (tree type, tree arg)
1924 tree orig = TREE_TYPE (arg);
1925 tree tem;
1927 if (type == orig)
1928 return arg;
1930 if (TREE_CODE (arg) == ERROR_MARK
1931 || TREE_CODE (type) == ERROR_MARK
1932 || TREE_CODE (orig) == ERROR_MARK)
1933 return error_mark_node;
1935 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig)
1936 || lang_hooks.types_compatible_p (TYPE_MAIN_VARIANT (type),
1937 TYPE_MAIN_VARIANT (orig)))
1938 return fold (build1 (NOP_EXPR, type, arg));
1940 if (INTEGRAL_TYPE_P (type) || POINTER_TYPE_P (type)
1941 || TREE_CODE (type) == OFFSET_TYPE)
1943 if (TREE_CODE (arg) == INTEGER_CST)
1945 tem = fold_convert_const (NOP_EXPR, type, arg);
1946 if (tem != NULL_TREE)
1947 return tem;
1949 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1950 || TREE_CODE (orig) == OFFSET_TYPE)
1951 return fold (build1 (NOP_EXPR, type, arg));
1952 if (TREE_CODE (orig) == COMPLEX_TYPE)
1954 tem = fold (build1 (REALPART_EXPR, TREE_TYPE (orig), arg));
1955 return fold_convert (type, tem);
1957 if (TREE_CODE (orig) == VECTOR_TYPE
1958 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)))
1959 return fold (build1 (NOP_EXPR, type, arg));
1961 else if (TREE_CODE (type) == REAL_TYPE)
1963 if (TREE_CODE (arg) == INTEGER_CST)
1965 tem = fold_convert_const (FLOAT_EXPR, type, arg);
1966 if (tem != NULL_TREE)
1967 return tem;
1969 else if (TREE_CODE (arg) == REAL_CST)
1971 tem = fold_convert_const (NOP_EXPR, type, arg);
1972 if (tem != NULL_TREE)
1973 return tem;
1976 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig))
1977 return fold (build1 (FLOAT_EXPR, type, arg));
1978 if (TREE_CODE (orig) == REAL_TYPE)
1979 return fold (build1 (flag_float_store ? CONVERT_EXPR : NOP_EXPR,
1980 type, arg));
1981 if (TREE_CODE (orig) == COMPLEX_TYPE)
1983 tem = fold (build1 (REALPART_EXPR, TREE_TYPE (orig), arg));
1984 return fold_convert (type, tem);
1987 else if (TREE_CODE (type) == COMPLEX_TYPE)
1989 if (INTEGRAL_TYPE_P (orig)
1990 || POINTER_TYPE_P (orig)
1991 || TREE_CODE (orig) == REAL_TYPE)
1992 return build2 (COMPLEX_EXPR, type,
1993 fold_convert (TREE_TYPE (type), arg),
1994 fold_convert (TREE_TYPE (type), integer_zero_node));
1995 if (TREE_CODE (orig) == COMPLEX_TYPE)
1997 tree rpart, ipart;
1999 if (TREE_CODE (arg) == COMPLEX_EXPR)
2001 rpart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 0));
2002 ipart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 1));
2003 return fold (build2 (COMPLEX_EXPR, type, rpart, ipart));
2006 arg = save_expr (arg);
2007 rpart = fold (build1 (REALPART_EXPR, TREE_TYPE (orig), arg));
2008 ipart = fold (build1 (IMAGPART_EXPR, TREE_TYPE (orig), arg));
2009 rpart = fold_convert (TREE_TYPE (type), rpart);
2010 ipart = fold_convert (TREE_TYPE (type), ipart);
2011 return fold (build2 (COMPLEX_EXPR, type, rpart, ipart));
2014 else if (TREE_CODE (type) == VECTOR_TYPE)
2016 if ((INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig))
2017 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)))
2018 return fold (build1 (NOP_EXPR, type, arg));
2019 if (TREE_CODE (orig) == VECTOR_TYPE
2020 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)))
2021 return fold (build1 (NOP_EXPR, type, arg));
2023 else if (VOID_TYPE_P (type))
2024 return fold (build1 (CONVERT_EXPR, type, fold_ignored_result (arg)));
2025 abort ();
2028 /* Return an expr equal to X but certainly not valid as an lvalue. */
2030 tree
2031 non_lvalue (tree x)
2033 /* We only need to wrap lvalue tree codes. */
2034 switch (TREE_CODE (x))
2036 case VAR_DECL:
2037 case PARM_DECL:
2038 case RESULT_DECL:
2039 case LABEL_DECL:
2040 case FUNCTION_DECL:
2041 case SSA_NAME:
2043 case COMPONENT_REF:
2044 case INDIRECT_REF:
2045 case ARRAY_REF:
2046 case ARRAY_RANGE_REF:
2047 case BIT_FIELD_REF:
2048 case OBJ_TYPE_REF:
2050 case REALPART_EXPR:
2051 case IMAGPART_EXPR:
2052 case PREINCREMENT_EXPR:
2053 case PREDECREMENT_EXPR:
2054 case SAVE_EXPR:
2055 case TRY_CATCH_EXPR:
2056 case WITH_CLEANUP_EXPR:
2057 case COMPOUND_EXPR:
2058 case MODIFY_EXPR:
2059 case TARGET_EXPR:
2060 case COND_EXPR:
2061 case BIND_EXPR:
2062 case MIN_EXPR:
2063 case MAX_EXPR:
2064 break;
2066 default:
2067 /* Assume the worst for front-end tree codes. */
2068 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2069 break;
2070 return x;
2072 return build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2075 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2076 Zero means allow extended lvalues. */
2078 int pedantic_lvalues;
2080 /* When pedantic, return an expr equal to X but certainly not valid as a
2081 pedantic lvalue. Otherwise, return X. */
2083 tree
2084 pedantic_non_lvalue (tree x)
2086 if (pedantic_lvalues)
2087 return non_lvalue (x);
2088 else
2089 return x;
2092 /* Given a tree comparison code, return the code that is the logical inverse
2093 of the given code. It is not safe to do this for floating-point
2094 comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2095 as well: if reversing the comparison is unsafe, return ERROR_MARK. */
2097 static enum tree_code
2098 invert_tree_comparison (enum tree_code code, bool honor_nans)
2100 if (honor_nans && flag_trapping_math)
2101 return ERROR_MARK;
2103 switch (code)
2105 case EQ_EXPR:
2106 return NE_EXPR;
2107 case NE_EXPR:
2108 return EQ_EXPR;
2109 case GT_EXPR:
2110 return honor_nans ? UNLE_EXPR : LE_EXPR;
2111 case GE_EXPR:
2112 return honor_nans ? UNLT_EXPR : LT_EXPR;
2113 case LT_EXPR:
2114 return honor_nans ? UNGE_EXPR : GE_EXPR;
2115 case LE_EXPR:
2116 return honor_nans ? UNGT_EXPR : GT_EXPR;
2117 case LTGT_EXPR:
2118 return UNEQ_EXPR;
2119 case UNEQ_EXPR:
2120 return LTGT_EXPR;
2121 case UNGT_EXPR:
2122 return LE_EXPR;
2123 case UNGE_EXPR:
2124 return LT_EXPR;
2125 case UNLT_EXPR:
2126 return GE_EXPR;
2127 case UNLE_EXPR:
2128 return GT_EXPR;
2129 case ORDERED_EXPR:
2130 return UNORDERED_EXPR;
2131 case UNORDERED_EXPR:
2132 return ORDERED_EXPR;
2133 default:
2134 abort ();
2138 /* Similar, but return the comparison that results if the operands are
2139 swapped. This is safe for floating-point. */
2141 enum tree_code
2142 swap_tree_comparison (enum tree_code code)
2144 switch (code)
2146 case EQ_EXPR:
2147 case NE_EXPR:
2148 return code;
2149 case GT_EXPR:
2150 return LT_EXPR;
2151 case GE_EXPR:
2152 return LE_EXPR;
2153 case LT_EXPR:
2154 return GT_EXPR;
2155 case LE_EXPR:
2156 return GE_EXPR;
2157 default:
2158 abort ();
2163 /* Convert a comparison tree code from an enum tree_code representation
2164 into a compcode bit-based encoding. This function is the inverse of
2165 compcode_to_comparison. */
2167 static enum comparison_code
2168 comparison_to_compcode (enum tree_code code)
2170 switch (code)
2172 case LT_EXPR:
2173 return COMPCODE_LT;
2174 case EQ_EXPR:
2175 return COMPCODE_EQ;
2176 case LE_EXPR:
2177 return COMPCODE_LE;
2178 case GT_EXPR:
2179 return COMPCODE_GT;
2180 case NE_EXPR:
2181 return COMPCODE_NE;
2182 case GE_EXPR:
2183 return COMPCODE_GE;
2184 case ORDERED_EXPR:
2185 return COMPCODE_ORD;
2186 case UNORDERED_EXPR:
2187 return COMPCODE_UNORD;
2188 case UNLT_EXPR:
2189 return COMPCODE_UNLT;
2190 case UNEQ_EXPR:
2191 return COMPCODE_UNEQ;
2192 case UNLE_EXPR:
2193 return COMPCODE_UNLE;
2194 case UNGT_EXPR:
2195 return COMPCODE_UNGT;
2196 case LTGT_EXPR:
2197 return COMPCODE_LTGT;
2198 case UNGE_EXPR:
2199 return COMPCODE_UNGE;
2200 default:
2201 abort ();
2205 /* Convert a compcode bit-based encoding of a comparison operator back
2206 to GCC's enum tree_code representation. This function is the
2207 inverse of comparison_to_compcode. */
2209 static enum tree_code
2210 compcode_to_comparison (enum comparison_code code)
2212 switch (code)
2214 case COMPCODE_LT:
2215 return LT_EXPR;
2216 case COMPCODE_EQ:
2217 return EQ_EXPR;
2218 case COMPCODE_LE:
2219 return LE_EXPR;
2220 case COMPCODE_GT:
2221 return GT_EXPR;
2222 case COMPCODE_NE:
2223 return NE_EXPR;
2224 case COMPCODE_GE:
2225 return GE_EXPR;
2226 case COMPCODE_ORD:
2227 return ORDERED_EXPR;
2228 case COMPCODE_UNORD:
2229 return UNORDERED_EXPR;
2230 case COMPCODE_UNLT:
2231 return UNLT_EXPR;
2232 case COMPCODE_UNEQ:
2233 return UNEQ_EXPR;
2234 case COMPCODE_UNLE:
2235 return UNLE_EXPR;
2236 case COMPCODE_UNGT:
2237 return UNGT_EXPR;
2238 case COMPCODE_LTGT:
2239 return LTGT_EXPR;
2240 case COMPCODE_UNGE:
2241 return UNGE_EXPR;
2242 default:
2243 abort ();
2247 /* Return a tree for the comparison which is the combination of
2248 doing the AND or OR (depending on CODE) of the two operations LCODE
2249 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2250 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2251 if this makes the transformation invalid. */
2253 tree
2254 combine_comparisons (enum tree_code code, enum tree_code lcode,
2255 enum tree_code rcode, tree truth_type,
2256 tree ll_arg, tree lr_arg)
2258 bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2259 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2260 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2261 enum comparison_code compcode;
2263 switch (code)
2265 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2266 compcode = lcompcode & rcompcode;
2267 break;
2269 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2270 compcode = lcompcode | rcompcode;
2271 break;
2273 default:
2274 return NULL_TREE;
2277 if (!honor_nans)
2279 /* Eliminate unordered comparisons, as well as LTGT and ORD
2280 which are not used unless the mode has NaNs. */
2281 compcode &= ~COMPCODE_UNORD;
2282 if (compcode == COMPCODE_LTGT)
2283 compcode = COMPCODE_NE;
2284 else if (compcode == COMPCODE_ORD)
2285 compcode = COMPCODE_TRUE;
2287 else if (flag_trapping_math)
2289 /* Check that the original operation and the optimized ones will trap
2290 under the same condition. */
2291 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2292 && (lcompcode != COMPCODE_EQ)
2293 && (lcompcode != COMPCODE_ORD);
2294 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2295 && (rcompcode != COMPCODE_EQ)
2296 && (rcompcode != COMPCODE_ORD);
2297 bool trap = (compcode & COMPCODE_UNORD) == 0
2298 && (compcode != COMPCODE_EQ)
2299 && (compcode != COMPCODE_ORD);
2301 /* In a short-circuited boolean expression the LHS might be
2302 such that the RHS, if evaluated, will never trap. For
2303 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2304 if neither x nor y is NaN. (This is a mixed blessing: for
2305 example, the expression above will never trap, hence
2306 optimizing it to x < y would be invalid). */
2307 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2308 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2309 rtrap = false;
2311 /* If the comparison was short-circuited, and only the RHS
2312 trapped, we may now generate a spurious trap. */
2313 if (rtrap && !ltrap
2314 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2315 return NULL_TREE;
2317 /* If we changed the conditions that cause a trap, we lose. */
2318 if ((ltrap || rtrap) != trap)
2319 return NULL_TREE;
2322 if (compcode == COMPCODE_TRUE)
2323 return constant_boolean_node (true, truth_type);
2324 else if (compcode == COMPCODE_FALSE)
2325 return constant_boolean_node (false, truth_type);
2326 else
2327 return fold (build2 (compcode_to_comparison (compcode),
2328 truth_type, ll_arg, lr_arg));
2331 /* Return nonzero if CODE is a tree code that represents a truth value. */
2333 static int
2334 truth_value_p (enum tree_code code)
2336 return (TREE_CODE_CLASS (code) == '<'
2337 || code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
2338 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
2339 || code == TRUTH_XOR_EXPR || code == TRUTH_NOT_EXPR);
2342 /* Return nonzero if two operands (typically of the same tree node)
2343 are necessarily equal. If either argument has side-effects this
2344 function returns zero. FLAGS modifies behavior as follows:
2346 If OEP_ONLY_CONST is set, only return nonzero for constants.
2347 This function tests whether the operands are indistinguishable;
2348 it does not test whether they are equal using C's == operation.
2349 The distinction is important for IEEE floating point, because
2350 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2351 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2353 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2354 even though it may hold multiple values during a function.
2355 This is because a GCC tree node guarantees that nothing else is
2356 executed between the evaluation of its "operands" (which may often
2357 be evaluated in arbitrary order). Hence if the operands themselves
2358 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2359 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2360 unset means assuming isochronic (or instantaneous) tree equivalence.
2361 Unless comparing arbitrary expression trees, such as from different
2362 statements, this flag can usually be left unset.
2364 If OEP_PURE_SAME is set, then pure functions with identical arguments
2365 are considered the same. It is used when the caller has other ways
2366 to ensure that global memory is unchanged in between. */
2369 operand_equal_p (tree arg0, tree arg1, unsigned int flags)
2371 /* If one is specified and the other isn't, they aren't equal and if
2372 neither is specified, they are.
2374 ??? This is temporary and is meant only to handle the cases of the
2375 optional operands for COMPONENT_REF and ARRAY_REF. */
2376 if ((arg0 && !arg1) || (!arg0 && arg1))
2377 return 0;
2378 else if (!arg0 && !arg1)
2379 return 1;
2380 /* If either is ERROR_MARK, they aren't equal. */
2381 else if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK)
2382 return 0;
2384 /* If both types don't have the same signedness, then we can't consider
2385 them equal. We must check this before the STRIP_NOPS calls
2386 because they may change the signedness of the arguments. */
2387 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2388 return 0;
2390 STRIP_NOPS (arg0);
2391 STRIP_NOPS (arg1);
2393 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2394 /* This is needed for conversions and for COMPONENT_REF.
2395 Might as well play it safe and always test this. */
2396 || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2397 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2398 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2399 return 0;
2401 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2402 We don't care about side effects in that case because the SAVE_EXPR
2403 takes care of that for us. In all other cases, two expressions are
2404 equal if they have no side effects. If we have two identical
2405 expressions with side effects that should be treated the same due
2406 to the only side effects being identical SAVE_EXPR's, that will
2407 be detected in the recursive calls below. */
2408 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2409 && (TREE_CODE (arg0) == SAVE_EXPR
2410 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2411 return 1;
2413 /* Next handle constant cases, those for which we can return 1 even
2414 if ONLY_CONST is set. */
2415 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2416 switch (TREE_CODE (arg0))
2418 case INTEGER_CST:
2419 return (! TREE_CONSTANT_OVERFLOW (arg0)
2420 && ! TREE_CONSTANT_OVERFLOW (arg1)
2421 && tree_int_cst_equal (arg0, arg1));
2423 case REAL_CST:
2424 return (! TREE_CONSTANT_OVERFLOW (arg0)
2425 && ! TREE_CONSTANT_OVERFLOW (arg1)
2426 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2427 TREE_REAL_CST (arg1)));
2429 case VECTOR_CST:
2431 tree v1, v2;
2433 if (TREE_CONSTANT_OVERFLOW (arg0)
2434 || TREE_CONSTANT_OVERFLOW (arg1))
2435 return 0;
2437 v1 = TREE_VECTOR_CST_ELTS (arg0);
2438 v2 = TREE_VECTOR_CST_ELTS (arg1);
2439 while (v1 && v2)
2441 if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
2442 flags))
2443 return 0;
2444 v1 = TREE_CHAIN (v1);
2445 v2 = TREE_CHAIN (v2);
2448 return 1;
2451 case COMPLEX_CST:
2452 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2453 flags)
2454 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2455 flags));
2457 case STRING_CST:
2458 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2459 && ! memcmp (TREE_STRING_POINTER (arg0),
2460 TREE_STRING_POINTER (arg1),
2461 TREE_STRING_LENGTH (arg0)));
2463 case ADDR_EXPR:
2464 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2466 default:
2467 break;
2470 if (flags & OEP_ONLY_CONST)
2471 return 0;
2473 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2475 case '1':
2476 /* Two conversions are equal only if signedness and modes match. */
2477 if ((TREE_CODE (arg0) == NOP_EXPR || TREE_CODE (arg0) == CONVERT_EXPR)
2478 && (TYPE_UNSIGNED (TREE_TYPE (arg0))
2479 != TYPE_UNSIGNED (TREE_TYPE (arg1))))
2480 return 0;
2482 return operand_equal_p (TREE_OPERAND (arg0, 0),
2483 TREE_OPERAND (arg1, 0), flags);
2485 case '<':
2486 case '2':
2487 if (operand_equal_p (TREE_OPERAND (arg0, 0),
2488 TREE_OPERAND (arg1, 0), flags)
2489 && operand_equal_p (TREE_OPERAND (arg0, 1),
2490 TREE_OPERAND (arg1, 1), flags))
2491 return 1;
2493 /* For commutative ops, allow the other order. */
2494 return (commutative_tree_code (TREE_CODE (arg0))
2495 && operand_equal_p (TREE_OPERAND (arg0, 0),
2496 TREE_OPERAND (arg1, 1), flags)
2497 && operand_equal_p (TREE_OPERAND (arg0, 1),
2498 TREE_OPERAND (arg1, 0), flags));
2500 case 'r':
2501 /* If either of the pointer (or reference) expressions we are
2502 dereferencing contain a side effect, these cannot be equal. */
2503 if (TREE_SIDE_EFFECTS (arg0)
2504 || TREE_SIDE_EFFECTS (arg1))
2505 return 0;
2507 switch (TREE_CODE (arg0))
2509 case INDIRECT_REF:
2510 case REALPART_EXPR:
2511 case IMAGPART_EXPR:
2512 return operand_equal_p (TREE_OPERAND (arg0, 0),
2513 TREE_OPERAND (arg1, 0), flags);
2515 case ARRAY_REF:
2516 case ARRAY_RANGE_REF:
2517 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2518 TREE_OPERAND (arg1, 0), flags)
2519 && operand_equal_p (TREE_OPERAND (arg0, 1),
2520 TREE_OPERAND (arg1, 1), flags)
2521 && operand_equal_p (TREE_OPERAND (arg0, 2),
2522 TREE_OPERAND (arg1, 2), flags)
2523 && operand_equal_p (TREE_OPERAND (arg0, 3),
2524 TREE_OPERAND (arg1, 3), flags));
2527 case COMPONENT_REF:
2528 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2529 TREE_OPERAND (arg1, 0), flags)
2530 && operand_equal_p (TREE_OPERAND (arg0, 1),
2531 TREE_OPERAND (arg1, 1), flags)
2532 && operand_equal_p (TREE_OPERAND (arg0, 2),
2533 TREE_OPERAND (arg1, 2), flags));
2536 case BIT_FIELD_REF:
2537 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2538 TREE_OPERAND (arg1, 0), flags)
2539 && operand_equal_p (TREE_OPERAND (arg0, 1),
2540 TREE_OPERAND (arg1, 1), flags)
2541 && operand_equal_p (TREE_OPERAND (arg0, 2),
2542 TREE_OPERAND (arg1, 2), flags));
2543 default:
2544 return 0;
2547 case 'e':
2548 switch (TREE_CODE (arg0))
2550 case ADDR_EXPR:
2551 case TRUTH_NOT_EXPR:
2552 return operand_equal_p (TREE_OPERAND (arg0, 0),
2553 TREE_OPERAND (arg1, 0), flags);
2555 case TRUTH_ANDIF_EXPR:
2556 case TRUTH_ORIF_EXPR:
2557 return operand_equal_p (TREE_OPERAND (arg0, 0),
2558 TREE_OPERAND (arg1, 0), flags)
2559 && operand_equal_p (TREE_OPERAND (arg0, 1),
2560 TREE_OPERAND (arg1, 1), flags);
2562 case TRUTH_AND_EXPR:
2563 case TRUTH_OR_EXPR:
2564 case TRUTH_XOR_EXPR:
2565 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2566 TREE_OPERAND (arg1, 0), flags)
2567 && operand_equal_p (TREE_OPERAND (arg0, 1),
2568 TREE_OPERAND (arg1, 1), flags))
2569 || (operand_equal_p (TREE_OPERAND (arg0, 0),
2570 TREE_OPERAND (arg1, 1), flags)
2571 && operand_equal_p (TREE_OPERAND (arg0, 1),
2572 TREE_OPERAND (arg1, 0), flags));
2574 case CALL_EXPR:
2575 /* If the CALL_EXPRs call different functions, then they
2576 clearly can not be equal. */
2577 if (! operand_equal_p (TREE_OPERAND (arg0, 0),
2578 TREE_OPERAND (arg1, 0), flags))
2579 return 0;
2582 unsigned int cef = call_expr_flags (arg0);
2583 if (flags & OEP_PURE_SAME)
2584 cef &= ECF_CONST | ECF_PURE;
2585 else
2586 cef &= ECF_CONST;
2587 if (!cef)
2588 return 0;
2591 /* Now see if all the arguments are the same. operand_equal_p
2592 does not handle TREE_LIST, so we walk the operands here
2593 feeding them to operand_equal_p. */
2594 arg0 = TREE_OPERAND (arg0, 1);
2595 arg1 = TREE_OPERAND (arg1, 1);
2596 while (arg0 && arg1)
2598 if (! operand_equal_p (TREE_VALUE (arg0), TREE_VALUE (arg1),
2599 flags))
2600 return 0;
2602 arg0 = TREE_CHAIN (arg0);
2603 arg1 = TREE_CHAIN (arg1);
2606 /* If we get here and both argument lists are exhausted
2607 then the CALL_EXPRs are equal. */
2608 return ! (arg0 || arg1);
2610 default:
2611 return 0;
2614 case 'd':
2615 /* Consider __builtin_sqrt equal to sqrt. */
2616 return (TREE_CODE (arg0) == FUNCTION_DECL
2617 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
2618 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
2619 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
2621 default:
2622 return 0;
2626 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2627 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2629 When in doubt, return 0. */
2631 static int
2632 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
2634 int unsignedp1, unsignedpo;
2635 tree primarg0, primarg1, primother;
2636 unsigned int correct_width;
2638 if (operand_equal_p (arg0, arg1, 0))
2639 return 1;
2641 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
2642 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
2643 return 0;
2645 /* Discard any conversions that don't change the modes of ARG0 and ARG1
2646 and see if the inner values are the same. This removes any
2647 signedness comparison, which doesn't matter here. */
2648 primarg0 = arg0, primarg1 = arg1;
2649 STRIP_NOPS (primarg0);
2650 STRIP_NOPS (primarg1);
2651 if (operand_equal_p (primarg0, primarg1, 0))
2652 return 1;
2654 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2655 actual comparison operand, ARG0.
2657 First throw away any conversions to wider types
2658 already present in the operands. */
2660 primarg1 = get_narrower (arg1, &unsignedp1);
2661 primother = get_narrower (other, &unsignedpo);
2663 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
2664 if (unsignedp1 == unsignedpo
2665 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
2666 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
2668 tree type = TREE_TYPE (arg0);
2670 /* Make sure shorter operand is extended the right way
2671 to match the longer operand. */
2672 primarg1 = fold_convert (lang_hooks.types.signed_or_unsigned_type
2673 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
2675 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
2676 return 1;
2679 return 0;
2682 /* See if ARG is an expression that is either a comparison or is performing
2683 arithmetic on comparisons. The comparisons must only be comparing
2684 two different values, which will be stored in *CVAL1 and *CVAL2; if
2685 they are nonzero it means that some operands have already been found.
2686 No variables may be used anywhere else in the expression except in the
2687 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
2688 the expression and save_expr needs to be called with CVAL1 and CVAL2.
2690 If this is true, return 1. Otherwise, return zero. */
2692 static int
2693 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
2695 enum tree_code code = TREE_CODE (arg);
2696 char class = TREE_CODE_CLASS (code);
2698 /* We can handle some of the 'e' cases here. */
2699 if (class == 'e' && code == TRUTH_NOT_EXPR)
2700 class = '1';
2701 else if (class == 'e'
2702 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
2703 || code == COMPOUND_EXPR))
2704 class = '2';
2706 else if (class == 'e' && code == SAVE_EXPR
2707 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
2709 /* If we've already found a CVAL1 or CVAL2, this expression is
2710 two complex to handle. */
2711 if (*cval1 || *cval2)
2712 return 0;
2714 class = '1';
2715 *save_p = 1;
2718 switch (class)
2720 case '1':
2721 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
2723 case '2':
2724 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
2725 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2726 cval1, cval2, save_p));
2728 case 'c':
2729 return 1;
2731 case 'e':
2732 if (code == COND_EXPR)
2733 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
2734 cval1, cval2, save_p)
2735 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2736 cval1, cval2, save_p)
2737 && twoval_comparison_p (TREE_OPERAND (arg, 2),
2738 cval1, cval2, save_p));
2739 return 0;
2741 case '<':
2742 /* First see if we can handle the first operand, then the second. For
2743 the second operand, we know *CVAL1 can't be zero. It must be that
2744 one side of the comparison is each of the values; test for the
2745 case where this isn't true by failing if the two operands
2746 are the same. */
2748 if (operand_equal_p (TREE_OPERAND (arg, 0),
2749 TREE_OPERAND (arg, 1), 0))
2750 return 0;
2752 if (*cval1 == 0)
2753 *cval1 = TREE_OPERAND (arg, 0);
2754 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
2756 else if (*cval2 == 0)
2757 *cval2 = TREE_OPERAND (arg, 0);
2758 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
2760 else
2761 return 0;
2763 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
2765 else if (*cval2 == 0)
2766 *cval2 = TREE_OPERAND (arg, 1);
2767 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
2769 else
2770 return 0;
2772 return 1;
2774 default:
2775 return 0;
2779 /* ARG is a tree that is known to contain just arithmetic operations and
2780 comparisons. Evaluate the operations in the tree substituting NEW0 for
2781 any occurrence of OLD0 as an operand of a comparison and likewise for
2782 NEW1 and OLD1. */
2784 static tree
2785 eval_subst (tree arg, tree old0, tree new0, tree old1, tree new1)
2787 tree type = TREE_TYPE (arg);
2788 enum tree_code code = TREE_CODE (arg);
2789 char class = TREE_CODE_CLASS (code);
2791 /* We can handle some of the 'e' cases here. */
2792 if (class == 'e' && code == TRUTH_NOT_EXPR)
2793 class = '1';
2794 else if (class == 'e'
2795 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2796 class = '2';
2798 switch (class)
2800 case '1':
2801 return fold (build1 (code, type,
2802 eval_subst (TREE_OPERAND (arg, 0),
2803 old0, new0, old1, new1)));
2805 case '2':
2806 return fold (build2 (code, type,
2807 eval_subst (TREE_OPERAND (arg, 0),
2808 old0, new0, old1, new1),
2809 eval_subst (TREE_OPERAND (arg, 1),
2810 old0, new0, old1, new1)));
2812 case 'e':
2813 switch (code)
2815 case SAVE_EXPR:
2816 return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
2818 case COMPOUND_EXPR:
2819 return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
2821 case COND_EXPR:
2822 return fold (build3 (code, type,
2823 eval_subst (TREE_OPERAND (arg, 0),
2824 old0, new0, old1, new1),
2825 eval_subst (TREE_OPERAND (arg, 1),
2826 old0, new0, old1, new1),
2827 eval_subst (TREE_OPERAND (arg, 2),
2828 old0, new0, old1, new1)));
2829 default:
2830 break;
2832 /* Fall through - ??? */
2834 case '<':
2836 tree arg0 = TREE_OPERAND (arg, 0);
2837 tree arg1 = TREE_OPERAND (arg, 1);
2839 /* We need to check both for exact equality and tree equality. The
2840 former will be true if the operand has a side-effect. In that
2841 case, we know the operand occurred exactly once. */
2843 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
2844 arg0 = new0;
2845 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
2846 arg0 = new1;
2848 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
2849 arg1 = new0;
2850 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
2851 arg1 = new1;
2853 return fold (build2 (code, type, arg0, arg1));
2856 default:
2857 return arg;
2861 /* Return a tree for the case when the result of an expression is RESULT
2862 converted to TYPE and OMITTED was previously an operand of the expression
2863 but is now not needed (e.g., we folded OMITTED * 0).
2865 If OMITTED has side effects, we must evaluate it. Otherwise, just do
2866 the conversion of RESULT to TYPE. */
2868 tree
2869 omit_one_operand (tree type, tree result, tree omitted)
2871 tree t = fold_convert (type, result);
2873 if (TREE_SIDE_EFFECTS (omitted))
2874 return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
2876 return non_lvalue (t);
2879 /* Similar, but call pedantic_non_lvalue instead of non_lvalue. */
2881 static tree
2882 pedantic_omit_one_operand (tree type, tree result, tree omitted)
2884 tree t = fold_convert (type, result);
2886 if (TREE_SIDE_EFFECTS (omitted))
2887 return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
2889 return pedantic_non_lvalue (t);
2892 /* Return a tree for the case when the result of an expression is RESULT
2893 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
2894 of the expression but are now not needed.
2896 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
2897 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
2898 evaluated before OMITTED2. Otherwise, if neither has side effects,
2899 just do the conversion of RESULT to TYPE. */
2901 tree
2902 omit_two_operands (tree type, tree result, tree omitted1, tree omitted2)
2904 tree t = fold_convert (type, result);
2906 if (TREE_SIDE_EFFECTS (omitted2))
2907 t = build2 (COMPOUND_EXPR, type, omitted2, t);
2908 if (TREE_SIDE_EFFECTS (omitted1))
2909 t = build2 (COMPOUND_EXPR, type, omitted1, t);
2911 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue (t) : t;
2915 /* Return a simplified tree node for the truth-negation of ARG. This
2916 never alters ARG itself. We assume that ARG is an operation that
2917 returns a truth value (0 or 1).
2919 FIXME: one would think we would fold the result, but it causes
2920 problems with the dominator optimizer. */
2921 tree
2922 invert_truthvalue (tree arg)
2924 tree type = TREE_TYPE (arg);
2925 enum tree_code code = TREE_CODE (arg);
2927 if (code == ERROR_MARK)
2928 return arg;
2930 /* If this is a comparison, we can simply invert it, except for
2931 floating-point non-equality comparisons, in which case we just
2932 enclose a TRUTH_NOT_EXPR around what we have. */
2934 if (TREE_CODE_CLASS (code) == '<')
2936 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
2937 if (FLOAT_TYPE_P (op_type)
2938 && flag_trapping_math
2939 && code != ORDERED_EXPR && code != UNORDERED_EXPR
2940 && code != NE_EXPR && code != EQ_EXPR)
2941 return build1 (TRUTH_NOT_EXPR, type, arg);
2942 else
2944 code = invert_tree_comparison (code,
2945 HONOR_NANS (TYPE_MODE (op_type)));
2946 if (code == ERROR_MARK)
2947 return build1 (TRUTH_NOT_EXPR, type, arg);
2948 else
2949 return build2 (code, type,
2950 TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
2954 switch (code)
2956 case INTEGER_CST:
2957 return fold_convert (type, build_int_2 (integer_zerop (arg), 0));
2959 case TRUTH_AND_EXPR:
2960 return build2 (TRUTH_OR_EXPR, type,
2961 invert_truthvalue (TREE_OPERAND (arg, 0)),
2962 invert_truthvalue (TREE_OPERAND (arg, 1)));
2964 case TRUTH_OR_EXPR:
2965 return build2 (TRUTH_AND_EXPR, type,
2966 invert_truthvalue (TREE_OPERAND (arg, 0)),
2967 invert_truthvalue (TREE_OPERAND (arg, 1)));
2969 case TRUTH_XOR_EXPR:
2970 /* Here we can invert either operand. We invert the first operand
2971 unless the second operand is a TRUTH_NOT_EXPR in which case our
2972 result is the XOR of the first operand with the inside of the
2973 negation of the second operand. */
2975 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
2976 return build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
2977 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
2978 else
2979 return build2 (TRUTH_XOR_EXPR, type,
2980 invert_truthvalue (TREE_OPERAND (arg, 0)),
2981 TREE_OPERAND (arg, 1));
2983 case TRUTH_ANDIF_EXPR:
2984 return build2 (TRUTH_ORIF_EXPR, type,
2985 invert_truthvalue (TREE_OPERAND (arg, 0)),
2986 invert_truthvalue (TREE_OPERAND (arg, 1)));
2988 case TRUTH_ORIF_EXPR:
2989 return build2 (TRUTH_ANDIF_EXPR, type,
2990 invert_truthvalue (TREE_OPERAND (arg, 0)),
2991 invert_truthvalue (TREE_OPERAND (arg, 1)));
2993 case TRUTH_NOT_EXPR:
2994 return TREE_OPERAND (arg, 0);
2996 case COND_EXPR:
2997 return build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
2998 invert_truthvalue (TREE_OPERAND (arg, 1)),
2999 invert_truthvalue (TREE_OPERAND (arg, 2)));
3001 case COMPOUND_EXPR:
3002 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
3003 invert_truthvalue (TREE_OPERAND (arg, 1)));
3005 case NON_LVALUE_EXPR:
3006 return invert_truthvalue (TREE_OPERAND (arg, 0));
3008 case NOP_EXPR:
3009 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3010 break;
3012 case CONVERT_EXPR:
3013 case FLOAT_EXPR:
3014 return build1 (TREE_CODE (arg), type,
3015 invert_truthvalue (TREE_OPERAND (arg, 0)));
3017 case BIT_AND_EXPR:
3018 if (!integer_onep (TREE_OPERAND (arg, 1)))
3019 break;
3020 return build2 (EQ_EXPR, type, arg,
3021 fold_convert (type, integer_zero_node));
3023 case SAVE_EXPR:
3024 return build1 (TRUTH_NOT_EXPR, type, arg);
3026 case CLEANUP_POINT_EXPR:
3027 return build1 (CLEANUP_POINT_EXPR, type,
3028 invert_truthvalue (TREE_OPERAND (arg, 0)));
3030 default:
3031 break;
3033 if (TREE_CODE (TREE_TYPE (arg)) != BOOLEAN_TYPE)
3034 abort ();
3035 return build1 (TRUTH_NOT_EXPR, type, arg);
3038 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3039 operands are another bit-wise operation with a common input. If so,
3040 distribute the bit operations to save an operation and possibly two if
3041 constants are involved. For example, convert
3042 (A | B) & (A | C) into A | (B & C)
3043 Further simplification will occur if B and C are constants.
3045 If this optimization cannot be done, 0 will be returned. */
3047 static tree
3048 distribute_bit_expr (enum tree_code code, tree type, tree arg0, tree arg1)
3050 tree common;
3051 tree left, right;
3053 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3054 || TREE_CODE (arg0) == code
3055 || (TREE_CODE (arg0) != BIT_AND_EXPR
3056 && TREE_CODE (arg0) != BIT_IOR_EXPR))
3057 return 0;
3059 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3061 common = TREE_OPERAND (arg0, 0);
3062 left = TREE_OPERAND (arg0, 1);
3063 right = TREE_OPERAND (arg1, 1);
3065 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3067 common = TREE_OPERAND (arg0, 0);
3068 left = TREE_OPERAND (arg0, 1);
3069 right = TREE_OPERAND (arg1, 0);
3071 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3073 common = TREE_OPERAND (arg0, 1);
3074 left = TREE_OPERAND (arg0, 0);
3075 right = TREE_OPERAND (arg1, 1);
3077 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3079 common = TREE_OPERAND (arg0, 1);
3080 left = TREE_OPERAND (arg0, 0);
3081 right = TREE_OPERAND (arg1, 0);
3083 else
3084 return 0;
3086 return fold (build2 (TREE_CODE (arg0), type, common,
3087 fold (build2 (code, type, left, right))));
3090 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3091 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero. */
3093 static tree
3094 make_bit_field_ref (tree inner, tree type, int bitsize, int bitpos,
3095 int unsignedp)
3097 tree result = build3 (BIT_FIELD_REF, type, inner,
3098 size_int (bitsize), bitsize_int (bitpos));
3100 BIT_FIELD_REF_UNSIGNED (result) = unsignedp;
3102 return result;
3105 /* Optimize a bit-field compare.
3107 There are two cases: First is a compare against a constant and the
3108 second is a comparison of two items where the fields are at the same
3109 bit position relative to the start of a chunk (byte, halfword, word)
3110 large enough to contain it. In these cases we can avoid the shift
3111 implicit in bitfield extractions.
3113 For constants, we emit a compare of the shifted constant with the
3114 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3115 compared. For two fields at the same position, we do the ANDs with the
3116 similar mask and compare the result of the ANDs.
3118 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3119 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3120 are the left and right operands of the comparison, respectively.
3122 If the optimization described above can be done, we return the resulting
3123 tree. Otherwise we return zero. */
3125 static tree
3126 optimize_bit_field_compare (enum tree_code code, tree compare_type,
3127 tree lhs, tree rhs)
3129 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3130 tree type = TREE_TYPE (lhs);
3131 tree signed_type, unsigned_type;
3132 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3133 enum machine_mode lmode, rmode, nmode;
3134 int lunsignedp, runsignedp;
3135 int lvolatilep = 0, rvolatilep = 0;
3136 tree linner, rinner = NULL_TREE;
3137 tree mask;
3138 tree offset;
3140 /* Get all the information about the extractions being done. If the bit size
3141 if the same as the size of the underlying object, we aren't doing an
3142 extraction at all and so can do nothing. We also don't want to
3143 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3144 then will no longer be able to replace it. */
3145 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3146 &lunsignedp, &lvolatilep);
3147 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3148 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3149 return 0;
3151 if (!const_p)
3153 /* If this is not a constant, we can only do something if bit positions,
3154 sizes, and signedness are the same. */
3155 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3156 &runsignedp, &rvolatilep);
3158 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3159 || lunsignedp != runsignedp || offset != 0
3160 || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
3161 return 0;
3164 /* See if we can find a mode to refer to this field. We should be able to,
3165 but fail if we can't. */
3166 nmode = get_best_mode (lbitsize, lbitpos,
3167 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3168 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3169 TYPE_ALIGN (TREE_TYPE (rinner))),
3170 word_mode, lvolatilep || rvolatilep);
3171 if (nmode == VOIDmode)
3172 return 0;
3174 /* Set signed and unsigned types of the precision of this mode for the
3175 shifts below. */
3176 signed_type = lang_hooks.types.type_for_mode (nmode, 0);
3177 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3179 /* Compute the bit position and size for the new reference and our offset
3180 within it. If the new reference is the same size as the original, we
3181 won't optimize anything, so return zero. */
3182 nbitsize = GET_MODE_BITSIZE (nmode);
3183 nbitpos = lbitpos & ~ (nbitsize - 1);
3184 lbitpos -= nbitpos;
3185 if (nbitsize == lbitsize)
3186 return 0;
3188 if (BYTES_BIG_ENDIAN)
3189 lbitpos = nbitsize - lbitsize - lbitpos;
3191 /* Make the mask to be used against the extracted field. */
3192 mask = build_int_2 (~0, ~0);
3193 TREE_TYPE (mask) = unsigned_type;
3194 mask = force_fit_type (mask, 0, false, false);
3195 mask = fold_convert (unsigned_type, mask);
3196 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
3197 mask = const_binop (RSHIFT_EXPR, mask,
3198 size_int (nbitsize - lbitsize - lbitpos), 0);
3200 if (! const_p)
3201 /* If not comparing with constant, just rework the comparison
3202 and return. */
3203 return build2 (code, compare_type,
3204 build2 (BIT_AND_EXPR, unsigned_type,
3205 make_bit_field_ref (linner, unsigned_type,
3206 nbitsize, nbitpos, 1),
3207 mask),
3208 build2 (BIT_AND_EXPR, unsigned_type,
3209 make_bit_field_ref (rinner, unsigned_type,
3210 nbitsize, nbitpos, 1),
3211 mask));
3213 /* Otherwise, we are handling the constant case. See if the constant is too
3214 big for the field. Warn and return a tree of for 0 (false) if so. We do
3215 this not only for its own sake, but to avoid having to test for this
3216 error case below. If we didn't, we might generate wrong code.
3218 For unsigned fields, the constant shifted right by the field length should
3219 be all zero. For signed fields, the high-order bits should agree with
3220 the sign bit. */
3222 if (lunsignedp)
3224 if (! integer_zerop (const_binop (RSHIFT_EXPR,
3225 fold_convert (unsigned_type, rhs),
3226 size_int (lbitsize), 0)))
3228 warning ("comparison is always %d due to width of bit-field",
3229 code == NE_EXPR);
3230 return constant_boolean_node (code == NE_EXPR, compare_type);
3233 else
3235 tree tem = const_binop (RSHIFT_EXPR, fold_convert (signed_type, rhs),
3236 size_int (lbitsize - 1), 0);
3237 if (! integer_zerop (tem) && ! integer_all_onesp (tem))
3239 warning ("comparison is always %d due to width of bit-field",
3240 code == NE_EXPR);
3241 return constant_boolean_node (code == NE_EXPR, compare_type);
3245 /* Single-bit compares should always be against zero. */
3246 if (lbitsize == 1 && ! integer_zerop (rhs))
3248 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3249 rhs = fold_convert (type, integer_zero_node);
3252 /* Make a new bitfield reference, shift the constant over the
3253 appropriate number of bits and mask it with the computed mask
3254 (in case this was a signed field). If we changed it, make a new one. */
3255 lhs = make_bit_field_ref (linner, unsigned_type, nbitsize, nbitpos, 1);
3256 if (lvolatilep)
3258 TREE_SIDE_EFFECTS (lhs) = 1;
3259 TREE_THIS_VOLATILE (lhs) = 1;
3262 rhs = fold (const_binop (BIT_AND_EXPR,
3263 const_binop (LSHIFT_EXPR,
3264 fold_convert (unsigned_type, rhs),
3265 size_int (lbitpos), 0),
3266 mask, 0));
3268 return build2 (code, compare_type,
3269 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask),
3270 rhs);
3273 /* Subroutine for fold_truthop: decode a field reference.
3275 If EXP is a comparison reference, we return the innermost reference.
3277 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3278 set to the starting bit number.
3280 If the innermost field can be completely contained in a mode-sized
3281 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
3283 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3284 otherwise it is not changed.
3286 *PUNSIGNEDP is set to the signedness of the field.
3288 *PMASK is set to the mask used. This is either contained in a
3289 BIT_AND_EXPR or derived from the width of the field.
3291 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3293 Return 0 if this is not a component reference or is one that we can't
3294 do anything with. */
3296 static tree
3297 decode_field_reference (tree exp, HOST_WIDE_INT *pbitsize,
3298 HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
3299 int *punsignedp, int *pvolatilep,
3300 tree *pmask, tree *pand_mask)
3302 tree outer_type = 0;
3303 tree and_mask = 0;
3304 tree mask, inner, offset;
3305 tree unsigned_type;
3306 unsigned int precision;
3308 /* All the optimizations using this function assume integer fields.
3309 There are problems with FP fields since the type_for_size call
3310 below can fail for, e.g., XFmode. */
3311 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3312 return 0;
3314 /* We are interested in the bare arrangement of bits, so strip everything
3315 that doesn't affect the machine mode. However, record the type of the
3316 outermost expression if it may matter below. */
3317 if (TREE_CODE (exp) == NOP_EXPR
3318 || TREE_CODE (exp) == CONVERT_EXPR
3319 || TREE_CODE (exp) == NON_LVALUE_EXPR)
3320 outer_type = TREE_TYPE (exp);
3321 STRIP_NOPS (exp);
3323 if (TREE_CODE (exp) == BIT_AND_EXPR)
3325 and_mask = TREE_OPERAND (exp, 1);
3326 exp = TREE_OPERAND (exp, 0);
3327 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3328 if (TREE_CODE (and_mask) != INTEGER_CST)
3329 return 0;
3332 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3333 punsignedp, pvolatilep);
3334 if ((inner == exp && and_mask == 0)
3335 || *pbitsize < 0 || offset != 0
3336 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3337 return 0;
3339 /* If the number of bits in the reference is the same as the bitsize of
3340 the outer type, then the outer type gives the signedness. Otherwise
3341 (in case of a small bitfield) the signedness is unchanged. */
3342 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
3343 *punsignedp = TYPE_UNSIGNED (outer_type);
3345 /* Compute the mask to access the bitfield. */
3346 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
3347 precision = TYPE_PRECISION (unsigned_type);
3349 mask = build_int_2 (~0, ~0);
3350 TREE_TYPE (mask) = unsigned_type;
3351 mask = force_fit_type (mask, 0, false, false);
3353 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3354 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3356 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
3357 if (and_mask != 0)
3358 mask = fold (build2 (BIT_AND_EXPR, unsigned_type,
3359 fold_convert (unsigned_type, and_mask), mask));
3361 *pmask = mask;
3362 *pand_mask = and_mask;
3363 return inner;
3366 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
3367 bit positions. */
3369 static int
3370 all_ones_mask_p (tree mask, int size)
3372 tree type = TREE_TYPE (mask);
3373 unsigned int precision = TYPE_PRECISION (type);
3374 tree tmask;
3376 tmask = build_int_2 (~0, ~0);
3377 TREE_TYPE (tmask) = lang_hooks.types.signed_type (type);
3378 tmask = force_fit_type (tmask, 0, false, false);
3380 return
3381 tree_int_cst_equal (mask,
3382 const_binop (RSHIFT_EXPR,
3383 const_binop (LSHIFT_EXPR, tmask,
3384 size_int (precision - size),
3386 size_int (precision - size), 0));
3389 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3390 represents the sign bit of EXP's type. If EXP represents a sign
3391 or zero extension, also test VAL against the unextended type.
3392 The return value is the (sub)expression whose sign bit is VAL,
3393 or NULL_TREE otherwise. */
3395 static tree
3396 sign_bit_p (tree exp, tree val)
3398 unsigned HOST_WIDE_INT mask_lo, lo;
3399 HOST_WIDE_INT mask_hi, hi;
3400 int width;
3401 tree t;
3403 /* Tree EXP must have an integral type. */
3404 t = TREE_TYPE (exp);
3405 if (! INTEGRAL_TYPE_P (t))
3406 return NULL_TREE;
3408 /* Tree VAL must be an integer constant. */
3409 if (TREE_CODE (val) != INTEGER_CST
3410 || TREE_CONSTANT_OVERFLOW (val))
3411 return NULL_TREE;
3413 width = TYPE_PRECISION (t);
3414 if (width > HOST_BITS_PER_WIDE_INT)
3416 hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
3417 lo = 0;
3419 mask_hi = ((unsigned HOST_WIDE_INT) -1
3420 >> (2 * HOST_BITS_PER_WIDE_INT - width));
3421 mask_lo = -1;
3423 else
3425 hi = 0;
3426 lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
3428 mask_hi = 0;
3429 mask_lo = ((unsigned HOST_WIDE_INT) -1
3430 >> (HOST_BITS_PER_WIDE_INT - width));
3433 /* We mask off those bits beyond TREE_TYPE (exp) so that we can
3434 treat VAL as if it were unsigned. */
3435 if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
3436 && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
3437 return exp;
3439 /* Handle extension from a narrower type. */
3440 if (TREE_CODE (exp) == NOP_EXPR
3441 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
3442 return sign_bit_p (TREE_OPERAND (exp, 0), val);
3444 return NULL_TREE;
3447 /* Subroutine for fold_truthop: determine if an operand is simple enough
3448 to be evaluated unconditionally. */
3450 static int
3451 simple_operand_p (tree exp)
3453 /* Strip any conversions that don't change the machine mode. */
3454 while ((TREE_CODE (exp) == NOP_EXPR
3455 || TREE_CODE (exp) == CONVERT_EXPR)
3456 && (TYPE_MODE (TREE_TYPE (exp))
3457 == TYPE_MODE (TREE_TYPE (TREE_OPERAND (exp, 0)))))
3458 exp = TREE_OPERAND (exp, 0);
3460 return (TREE_CODE_CLASS (TREE_CODE (exp)) == 'c'
3461 || (DECL_P (exp)
3462 && ! TREE_ADDRESSABLE (exp)
3463 && ! TREE_THIS_VOLATILE (exp)
3464 && ! DECL_NONLOCAL (exp)
3465 /* Don't regard global variables as simple. They may be
3466 allocated in ways unknown to the compiler (shared memory,
3467 #pragma weak, etc). */
3468 && ! TREE_PUBLIC (exp)
3469 && ! DECL_EXTERNAL (exp)
3470 /* Loading a static variable is unduly expensive, but global
3471 registers aren't expensive. */
3472 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
3475 /* The following functions are subroutines to fold_range_test and allow it to
3476 try to change a logical combination of comparisons into a range test.
3478 For example, both
3479 X == 2 || X == 3 || X == 4 || X == 5
3481 X >= 2 && X <= 5
3482 are converted to
3483 (unsigned) (X - 2) <= 3
3485 We describe each set of comparisons as being either inside or outside
3486 a range, using a variable named like IN_P, and then describe the
3487 range with a lower and upper bound. If one of the bounds is omitted,
3488 it represents either the highest or lowest value of the type.
3490 In the comments below, we represent a range by two numbers in brackets
3491 preceded by a "+" to designate being inside that range, or a "-" to
3492 designate being outside that range, so the condition can be inverted by
3493 flipping the prefix. An omitted bound is represented by a "-". For
3494 example, "- [-, 10]" means being outside the range starting at the lowest
3495 possible value and ending at 10, in other words, being greater than 10.
3496 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3497 always false.
3499 We set up things so that the missing bounds are handled in a consistent
3500 manner so neither a missing bound nor "true" and "false" need to be
3501 handled using a special case. */
3503 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3504 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3505 and UPPER1_P are nonzero if the respective argument is an upper bound
3506 and zero for a lower. TYPE, if nonzero, is the type of the result; it
3507 must be specified for a comparison. ARG1 will be converted to ARG0's
3508 type if both are specified. */
3510 static tree
3511 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
3512 tree arg1, int upper1_p)
3514 tree tem;
3515 int result;
3516 int sgn0, sgn1;
3518 /* If neither arg represents infinity, do the normal operation.
3519 Else, if not a comparison, return infinity. Else handle the special
3520 comparison rules. Note that most of the cases below won't occur, but
3521 are handled for consistency. */
3523 if (arg0 != 0 && arg1 != 0)
3525 tem = fold (build2 (code, type != 0 ? type : TREE_TYPE (arg0),
3526 arg0, fold_convert (TREE_TYPE (arg0), arg1)));
3527 STRIP_NOPS (tem);
3528 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
3531 if (TREE_CODE_CLASS (code) != '<')
3532 return 0;
3534 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
3535 for neither. In real maths, we cannot assume open ended ranges are
3536 the same. But, this is computer arithmetic, where numbers are finite.
3537 We can therefore make the transformation of any unbounded range with
3538 the value Z, Z being greater than any representable number. This permits
3539 us to treat unbounded ranges as equal. */
3540 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
3541 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
3542 switch (code)
3544 case EQ_EXPR:
3545 result = sgn0 == sgn1;
3546 break;
3547 case NE_EXPR:
3548 result = sgn0 != sgn1;
3549 break;
3550 case LT_EXPR:
3551 result = sgn0 < sgn1;
3552 break;
3553 case LE_EXPR:
3554 result = sgn0 <= sgn1;
3555 break;
3556 case GT_EXPR:
3557 result = sgn0 > sgn1;
3558 break;
3559 case GE_EXPR:
3560 result = sgn0 >= sgn1;
3561 break;
3562 default:
3563 abort ();
3566 return constant_boolean_node (result, type);
3569 /* Given EXP, a logical expression, set the range it is testing into
3570 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
3571 actually being tested. *PLOW and *PHIGH will be made of the same type
3572 as the returned expression. If EXP is not a comparison, we will most
3573 likely not be returning a useful value and range. */
3575 static tree
3576 make_range (tree exp, int *pin_p, tree *plow, tree *phigh)
3578 enum tree_code code;
3579 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
3580 tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
3581 int in_p, n_in_p;
3582 tree low, high, n_low, n_high;
3584 /* Start with simply saying "EXP != 0" and then look at the code of EXP
3585 and see if we can refine the range. Some of the cases below may not
3586 happen, but it doesn't seem worth worrying about this. We "continue"
3587 the outer loop when we've changed something; otherwise we "break"
3588 the switch, which will "break" the while. */
3590 in_p = 0;
3591 low = high = fold_convert (TREE_TYPE (exp), integer_zero_node);
3593 while (1)
3595 code = TREE_CODE (exp);
3596 exp_type = TREE_TYPE (exp);
3598 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
3600 if (first_rtl_op (code) > 0)
3601 arg0 = TREE_OPERAND (exp, 0);
3602 if (TREE_CODE_CLASS (code) == '<'
3603 || TREE_CODE_CLASS (code) == '1'
3604 || TREE_CODE_CLASS (code) == '2')
3605 arg0_type = TREE_TYPE (arg0);
3606 if (TREE_CODE_CLASS (code) == '2'
3607 || TREE_CODE_CLASS (code) == '<'
3608 || (TREE_CODE_CLASS (code) == 'e'
3609 && TREE_CODE_LENGTH (code) > 1))
3610 arg1 = TREE_OPERAND (exp, 1);
3613 switch (code)
3615 case TRUTH_NOT_EXPR:
3616 in_p = ! in_p, exp = arg0;
3617 continue;
3619 case EQ_EXPR: case NE_EXPR:
3620 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
3621 /* We can only do something if the range is testing for zero
3622 and if the second operand is an integer constant. Note that
3623 saying something is "in" the range we make is done by
3624 complementing IN_P since it will set in the initial case of
3625 being not equal to zero; "out" is leaving it alone. */
3626 if (low == 0 || high == 0
3627 || ! integer_zerop (low) || ! integer_zerop (high)
3628 || TREE_CODE (arg1) != INTEGER_CST)
3629 break;
3631 switch (code)
3633 case NE_EXPR: /* - [c, c] */
3634 low = high = arg1;
3635 break;
3636 case EQ_EXPR: /* + [c, c] */
3637 in_p = ! in_p, low = high = arg1;
3638 break;
3639 case GT_EXPR: /* - [-, c] */
3640 low = 0, high = arg1;
3641 break;
3642 case GE_EXPR: /* + [c, -] */
3643 in_p = ! in_p, low = arg1, high = 0;
3644 break;
3645 case LT_EXPR: /* - [c, -] */
3646 low = arg1, high = 0;
3647 break;
3648 case LE_EXPR: /* + [-, c] */
3649 in_p = ! in_p, low = 0, high = arg1;
3650 break;
3651 default:
3652 abort ();
3655 /* If this is an unsigned comparison, we also know that EXP is
3656 greater than or equal to zero. We base the range tests we make
3657 on that fact, so we record it here so we can parse existing
3658 range tests. We test arg0_type since often the return type
3659 of, e.g. EQ_EXPR, is boolean. */
3660 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
3662 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3663 in_p, low, high, 1,
3664 fold_convert (arg0_type, integer_zero_node),
3665 NULL_TREE))
3666 break;
3668 in_p = n_in_p, low = n_low, high = n_high;
3670 /* If the high bound is missing, but we have a nonzero low
3671 bound, reverse the range so it goes from zero to the low bound
3672 minus 1. */
3673 if (high == 0 && low && ! integer_zerop (low))
3675 in_p = ! in_p;
3676 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
3677 integer_one_node, 0);
3678 low = fold_convert (arg0_type, integer_zero_node);
3682 exp = arg0;
3683 continue;
3685 case NEGATE_EXPR:
3686 /* (-x) IN [a,b] -> x in [-b, -a] */
3687 n_low = range_binop (MINUS_EXPR, exp_type,
3688 fold_convert (exp_type, integer_zero_node),
3689 0, high, 1);
3690 n_high = range_binop (MINUS_EXPR, exp_type,
3691 fold_convert (exp_type, integer_zero_node),
3692 0, low, 0);
3693 low = n_low, high = n_high;
3694 exp = arg0;
3695 continue;
3697 case BIT_NOT_EXPR:
3698 /* ~ X -> -X - 1 */
3699 exp = build2 (MINUS_EXPR, exp_type, negate_expr (arg0),
3700 fold_convert (exp_type, integer_one_node));
3701 continue;
3703 case PLUS_EXPR: case MINUS_EXPR:
3704 if (TREE_CODE (arg1) != INTEGER_CST)
3705 break;
3707 /* If EXP is signed, any overflow in the computation is undefined,
3708 so we don't worry about it so long as our computations on
3709 the bounds don't overflow. For unsigned, overflow is defined
3710 and this is exactly the right thing. */
3711 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3712 arg0_type, low, 0, arg1, 0);
3713 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3714 arg0_type, high, 1, arg1, 0);
3715 if ((n_low != 0 && TREE_OVERFLOW (n_low))
3716 || (n_high != 0 && TREE_OVERFLOW (n_high)))
3717 break;
3719 /* Check for an unsigned range which has wrapped around the maximum
3720 value thus making n_high < n_low, and normalize it. */
3721 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
3723 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
3724 integer_one_node, 0);
3725 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
3726 integer_one_node, 0);
3728 /* If the range is of the form +/- [ x+1, x ], we won't
3729 be able to normalize it. But then, it represents the
3730 whole range or the empty set, so make it
3731 +/- [ -, - ]. */
3732 if (tree_int_cst_equal (n_low, low)
3733 && tree_int_cst_equal (n_high, high))
3734 low = high = 0;
3735 else
3736 in_p = ! in_p;
3738 else
3739 low = n_low, high = n_high;
3741 exp = arg0;
3742 continue;
3744 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
3745 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
3746 break;
3748 if (! INTEGRAL_TYPE_P (arg0_type)
3749 || (low != 0 && ! int_fits_type_p (low, arg0_type))
3750 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
3751 break;
3753 n_low = low, n_high = high;
3755 if (n_low != 0)
3756 n_low = fold_convert (arg0_type, n_low);
3758 if (n_high != 0)
3759 n_high = fold_convert (arg0_type, n_high);
3762 /* If we're converting arg0 from an unsigned type, to exp,
3763 a signed type, we will be doing the comparison as unsigned.
3764 The tests above have already verified that LOW and HIGH
3765 are both positive.
3767 So we have to ensure that we will handle large unsigned
3768 values the same way that the current signed bounds treat
3769 negative values. */
3771 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
3773 tree high_positive;
3774 tree equiv_type = lang_hooks.types.type_for_mode
3775 (TYPE_MODE (arg0_type), 1);
3777 /* A range without an upper bound is, naturally, unbounded.
3778 Since convert would have cropped a very large value, use
3779 the max value for the destination type. */
3780 high_positive
3781 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
3782 : TYPE_MAX_VALUE (arg0_type);
3784 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
3785 high_positive = fold (build2 (RSHIFT_EXPR, arg0_type,
3786 fold_convert (arg0_type,
3787 high_positive),
3788 fold_convert (arg0_type,
3789 integer_one_node)));
3791 /* If the low bound is specified, "and" the range with the
3792 range for which the original unsigned value will be
3793 positive. */
3794 if (low != 0)
3796 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3797 1, n_low, n_high, 1,
3798 fold_convert (arg0_type,
3799 integer_zero_node),
3800 high_positive))
3801 break;
3803 in_p = (n_in_p == in_p);
3805 else
3807 /* Otherwise, "or" the range with the range of the input
3808 that will be interpreted as negative. */
3809 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3810 0, n_low, n_high, 1,
3811 fold_convert (arg0_type,
3812 integer_zero_node),
3813 high_positive))
3814 break;
3816 in_p = (in_p != n_in_p);
3820 exp = arg0;
3821 low = n_low, high = n_high;
3822 continue;
3824 default:
3825 break;
3828 break;
3831 /* If EXP is a constant, we can evaluate whether this is true or false. */
3832 if (TREE_CODE (exp) == INTEGER_CST)
3834 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
3835 exp, 0, low, 0))
3836 && integer_onep (range_binop (LE_EXPR, integer_type_node,
3837 exp, 1, high, 1)));
3838 low = high = 0;
3839 exp = 0;
3842 *pin_p = in_p, *plow = low, *phigh = high;
3843 return exp;
3846 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
3847 type, TYPE, return an expression to test if EXP is in (or out of, depending
3848 on IN_P) the range. Return 0 if the test couldn't be created. */
3850 static tree
3851 build_range_check (tree type, tree exp, int in_p, tree low, tree high)
3853 tree etype = TREE_TYPE (exp);
3854 tree value;
3856 if (! in_p)
3858 value = build_range_check (type, exp, 1, low, high);
3859 if (value != 0)
3860 return invert_truthvalue (value);
3862 return 0;
3865 if (low == 0 && high == 0)
3866 return fold_convert (type, integer_one_node);
3868 if (low == 0)
3869 return fold (build2 (LE_EXPR, type, exp, high));
3871 if (high == 0)
3872 return fold (build2 (GE_EXPR, type, exp, low));
3874 if (operand_equal_p (low, high, 0))
3875 return fold (build2 (EQ_EXPR, type, exp, low));
3877 if (integer_zerop (low))
3879 if (! TYPE_UNSIGNED (etype))
3881 etype = lang_hooks.types.unsigned_type (etype);
3882 high = fold_convert (etype, high);
3883 exp = fold_convert (etype, exp);
3885 return build_range_check (type, exp, 1, 0, high);
3888 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
3889 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
3891 unsigned HOST_WIDE_INT lo;
3892 HOST_WIDE_INT hi;
3893 int prec;
3895 prec = TYPE_PRECISION (etype);
3896 if (prec <= HOST_BITS_PER_WIDE_INT)
3898 hi = 0;
3899 lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
3901 else
3903 hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
3904 lo = (unsigned HOST_WIDE_INT) -1;
3907 if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
3909 if (TYPE_UNSIGNED (etype))
3911 etype = lang_hooks.types.signed_type (etype);
3912 exp = fold_convert (etype, exp);
3914 return fold (build2 (GT_EXPR, type, exp,
3915 fold_convert (etype, integer_zero_node)));
3919 value = const_binop (MINUS_EXPR, high, low, 0);
3920 if (value != 0 && TREE_OVERFLOW (value) && ! TYPE_UNSIGNED (etype))
3922 tree utype, minv, maxv;
3924 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
3925 for the type in question, as we rely on this here. */
3926 switch (TREE_CODE (etype))
3928 case INTEGER_TYPE:
3929 case ENUMERAL_TYPE:
3930 case CHAR_TYPE:
3931 utype = lang_hooks.types.unsigned_type (etype);
3932 maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
3933 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
3934 integer_one_node, 1);
3935 minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
3936 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
3937 minv, 1, maxv, 1)))
3939 etype = utype;
3940 high = fold_convert (etype, high);
3941 low = fold_convert (etype, low);
3942 exp = fold_convert (etype, exp);
3943 value = const_binop (MINUS_EXPR, high, low, 0);
3945 break;
3946 default:
3947 break;
3951 if (value != 0 && ! TREE_OVERFLOW (value))
3952 return build_range_check (type,
3953 fold (build2 (MINUS_EXPR, etype, exp, low)),
3954 1, fold_convert (etype, integer_zero_node),
3955 value);
3957 return 0;
3960 /* Given two ranges, see if we can merge them into one. Return 1 if we
3961 can, 0 if we can't. Set the output range into the specified parameters. */
3963 static int
3964 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
3965 tree high0, int in1_p, tree low1, tree high1)
3967 int no_overlap;
3968 int subset;
3969 int temp;
3970 tree tem;
3971 int in_p;
3972 tree low, high;
3973 int lowequal = ((low0 == 0 && low1 == 0)
3974 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
3975 low0, 0, low1, 0)));
3976 int highequal = ((high0 == 0 && high1 == 0)
3977 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
3978 high0, 1, high1, 1)));
3980 /* Make range 0 be the range that starts first, or ends last if they
3981 start at the same value. Swap them if it isn't. */
3982 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
3983 low0, 0, low1, 0))
3984 || (lowequal
3985 && integer_onep (range_binop (GT_EXPR, integer_type_node,
3986 high1, 1, high0, 1))))
3988 temp = in0_p, in0_p = in1_p, in1_p = temp;
3989 tem = low0, low0 = low1, low1 = tem;
3990 tem = high0, high0 = high1, high1 = tem;
3993 /* Now flag two cases, whether the ranges are disjoint or whether the
3994 second range is totally subsumed in the first. Note that the tests
3995 below are simplified by the ones above. */
3996 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
3997 high0, 1, low1, 0));
3998 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
3999 high1, 1, high0, 1));
4001 /* We now have four cases, depending on whether we are including or
4002 excluding the two ranges. */
4003 if (in0_p && in1_p)
4005 /* If they don't overlap, the result is false. If the second range
4006 is a subset it is the result. Otherwise, the range is from the start
4007 of the second to the end of the first. */
4008 if (no_overlap)
4009 in_p = 0, low = high = 0;
4010 else if (subset)
4011 in_p = 1, low = low1, high = high1;
4012 else
4013 in_p = 1, low = low1, high = high0;
4016 else if (in0_p && ! in1_p)
4018 /* If they don't overlap, the result is the first range. If they are
4019 equal, the result is false. If the second range is a subset of the
4020 first, and the ranges begin at the same place, we go from just after
4021 the end of the first range to the end of the second. If the second
4022 range is not a subset of the first, or if it is a subset and both
4023 ranges end at the same place, the range starts at the start of the
4024 first range and ends just before the second range.
4025 Otherwise, we can't describe this as a single range. */
4026 if (no_overlap)
4027 in_p = 1, low = low0, high = high0;
4028 else if (lowequal && highequal)
4029 in_p = 0, low = high = 0;
4030 else if (subset && lowequal)
4032 in_p = 1, high = high0;
4033 low = range_binop (PLUS_EXPR, NULL_TREE, high1, 0,
4034 integer_one_node, 0);
4036 else if (! subset || highequal)
4038 in_p = 1, low = low0;
4039 high = range_binop (MINUS_EXPR, NULL_TREE, low1, 0,
4040 integer_one_node, 0);
4042 else
4043 return 0;
4046 else if (! in0_p && in1_p)
4048 /* If they don't overlap, the result is the second range. If the second
4049 is a subset of the first, the result is false. Otherwise,
4050 the range starts just after the first range and ends at the
4051 end of the second. */
4052 if (no_overlap)
4053 in_p = 1, low = low1, high = high1;
4054 else if (subset || highequal)
4055 in_p = 0, low = high = 0;
4056 else
4058 in_p = 1, high = high1;
4059 low = range_binop (PLUS_EXPR, NULL_TREE, high0, 1,
4060 integer_one_node, 0);
4064 else
4066 /* The case where we are excluding both ranges. Here the complex case
4067 is if they don't overlap. In that case, the only time we have a
4068 range is if they are adjacent. If the second is a subset of the
4069 first, the result is the first. Otherwise, the range to exclude
4070 starts at the beginning of the first range and ends at the end of the
4071 second. */
4072 if (no_overlap)
4074 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4075 range_binop (PLUS_EXPR, NULL_TREE,
4076 high0, 1,
4077 integer_one_node, 1),
4078 1, low1, 0)))
4079 in_p = 0, low = low0, high = high1;
4080 else
4082 /* Canonicalize - [min, x] into - [-, x]. */
4083 if (low0 && TREE_CODE (low0) == INTEGER_CST)
4084 switch (TREE_CODE (TREE_TYPE (low0)))
4086 case ENUMERAL_TYPE:
4087 if (TYPE_PRECISION (TREE_TYPE (low0))
4088 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4089 break;
4090 /* FALLTHROUGH */
4091 case INTEGER_TYPE:
4092 case CHAR_TYPE:
4093 if (tree_int_cst_equal (low0,
4094 TYPE_MIN_VALUE (TREE_TYPE (low0))))
4095 low0 = 0;
4096 break;
4097 case POINTER_TYPE:
4098 if (TYPE_UNSIGNED (TREE_TYPE (low0))
4099 && integer_zerop (low0))
4100 low0 = 0;
4101 break;
4102 default:
4103 break;
4106 /* Canonicalize - [x, max] into - [x, -]. */
4107 if (high1 && TREE_CODE (high1) == INTEGER_CST)
4108 switch (TREE_CODE (TREE_TYPE (high1)))
4110 case ENUMERAL_TYPE:
4111 if (TYPE_PRECISION (TREE_TYPE (high1))
4112 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4113 break;
4114 /* FALLTHROUGH */
4115 case INTEGER_TYPE:
4116 case CHAR_TYPE:
4117 if (tree_int_cst_equal (high1,
4118 TYPE_MAX_VALUE (TREE_TYPE (high1))))
4119 high1 = 0;
4120 break;
4121 case POINTER_TYPE:
4122 if (TYPE_UNSIGNED (TREE_TYPE (high1))
4123 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4124 high1, 1,
4125 integer_one_node, 1)))
4126 high1 = 0;
4127 break;
4128 default:
4129 break;
4132 /* The ranges might be also adjacent between the maximum and
4133 minimum values of the given type. For
4134 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4135 return + [x + 1, y - 1]. */
4136 if (low0 == 0 && high1 == 0)
4138 low = range_binop (PLUS_EXPR, NULL_TREE, high0, 1,
4139 integer_one_node, 1);
4140 high = range_binop (MINUS_EXPR, NULL_TREE, low1, 0,
4141 integer_one_node, 0);
4142 if (low == 0 || high == 0)
4143 return 0;
4145 in_p = 1;
4147 else
4148 return 0;
4151 else if (subset)
4152 in_p = 0, low = low0, high = high0;
4153 else
4154 in_p = 0, low = low0, high = high1;
4157 *pin_p = in_p, *plow = low, *phigh = high;
4158 return 1;
4162 /* Subroutine of fold, looking inside expressions of the form
4163 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4164 of the COND_EXPR. This function is being used also to optimize
4165 A op B ? C : A, by reversing the comparison first.
4167 Return a folded expression whose code is not a COND_EXPR
4168 anymore, or NULL_TREE if no folding opportunity is found. */
4170 static tree
4171 fold_cond_expr_with_comparison (tree type, tree arg0, tree arg1, tree arg2)
4173 enum tree_code comp_code = TREE_CODE (arg0);
4174 tree arg00 = TREE_OPERAND (arg0, 0);
4175 tree arg01 = TREE_OPERAND (arg0, 1);
4176 tree arg1_type = TREE_TYPE (arg1);
4177 tree tem;
4179 STRIP_NOPS (arg1);
4180 STRIP_NOPS (arg2);
4182 /* If we have A op 0 ? A : -A, consider applying the following
4183 transformations:
4185 A == 0? A : -A same as -A
4186 A != 0? A : -A same as A
4187 A >= 0? A : -A same as abs (A)
4188 A > 0? A : -A same as abs (A)
4189 A <= 0? A : -A same as -abs (A)
4190 A < 0? A : -A same as -abs (A)
4192 None of these transformations work for modes with signed
4193 zeros. If A is +/-0, the first two transformations will
4194 change the sign of the result (from +0 to -0, or vice
4195 versa). The last four will fix the sign of the result,
4196 even though the original expressions could be positive or
4197 negative, depending on the sign of A.
4199 Note that all these transformations are correct if A is
4200 NaN, since the two alternatives (A and -A) are also NaNs. */
4201 if ((FLOAT_TYPE_P (TREE_TYPE (arg01))
4202 ? real_zerop (arg01)
4203 : integer_zerop (arg01))
4204 && TREE_CODE (arg2) == NEGATE_EXPR
4205 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4206 switch (comp_code)
4208 case EQ_EXPR:
4209 tem = fold_convert (arg1_type, arg1);
4210 return pedantic_non_lvalue (fold_convert (type, negate_expr (tem)));
4211 case NE_EXPR:
4212 return pedantic_non_lvalue (fold_convert (type, arg1));
4213 case GE_EXPR:
4214 case GT_EXPR:
4215 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4216 arg1 = fold_convert (lang_hooks.types.signed_type
4217 (TREE_TYPE (arg1)), arg1);
4218 tem = fold (build1 (ABS_EXPR, TREE_TYPE (arg1), arg1));
4219 return pedantic_non_lvalue (fold_convert (type, tem));
4220 case LE_EXPR:
4221 case LT_EXPR:
4222 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4223 arg1 = fold_convert (lang_hooks.types.signed_type
4224 (TREE_TYPE (arg1)), arg1);
4225 tem = fold (build1 (ABS_EXPR, TREE_TYPE (arg1), arg1));
4226 return negate_expr (fold_convert (type, tem));
4227 default:
4228 abort ();
4231 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
4232 A == 0 ? A : 0 is always 0 unless A is -0. Note that
4233 both transformations are correct when A is NaN: A != 0
4234 is then true, and A == 0 is false. */
4236 if (integer_zerop (arg01) && integer_zerop (arg2))
4238 if (comp_code == NE_EXPR)
4239 return pedantic_non_lvalue (fold_convert (type, arg1));
4240 else if (comp_code == EQ_EXPR)
4241 return fold_convert (type, integer_zero_node);
4244 /* Try some transformations of A op B ? A : B.
4246 A == B? A : B same as B
4247 A != B? A : B same as A
4248 A >= B? A : B same as max (A, B)
4249 A > B? A : B same as max (B, A)
4250 A <= B? A : B same as min (A, B)
4251 A < B? A : B same as min (B, A)
4253 As above, these transformations don't work in the presence
4254 of signed zeros. For example, if A and B are zeros of
4255 opposite sign, the first two transformations will change
4256 the sign of the result. In the last four, the original
4257 expressions give different results for (A=+0, B=-0) and
4258 (A=-0, B=+0), but the transformed expressions do not.
4260 The first two transformations are correct if either A or B
4261 is a NaN. In the first transformation, the condition will
4262 be false, and B will indeed be chosen. In the case of the
4263 second transformation, the condition A != B will be true,
4264 and A will be chosen.
4266 The conversions to max() and min() are not correct if B is
4267 a number and A is not. The conditions in the original
4268 expressions will be false, so all four give B. The min()
4269 and max() versions would give a NaN instead. */
4270 if (operand_equal_for_comparison_p (arg01, arg2, arg00))
4272 tree comp_op0 = arg00;
4273 tree comp_op1 = arg01;
4274 tree comp_type = TREE_TYPE (comp_op0);
4276 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
4277 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
4279 comp_type = type;
4280 comp_op0 = arg1;
4281 comp_op1 = arg2;
4284 switch (comp_code)
4286 case EQ_EXPR:
4287 return pedantic_non_lvalue (fold_convert (type, arg2));
4288 case NE_EXPR:
4289 return pedantic_non_lvalue (fold_convert (type, arg1));
4290 case LE_EXPR:
4291 case LT_EXPR:
4292 /* In C++ a ?: expression can be an lvalue, so put the
4293 operand which will be used if they are equal first
4294 so that we can convert this back to the
4295 corresponding COND_EXPR. */
4296 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4298 comp_op0 = fold_convert (comp_type, comp_op0);
4299 comp_op1 = fold_convert (comp_type, comp_op1);
4300 tem = fold (build2 (MIN_EXPR, comp_type,
4301 (comp_code == LE_EXPR
4302 ? comp_op0 : comp_op1),
4303 (comp_code == LE_EXPR
4304 ? comp_op1 : comp_op0)));
4305 return pedantic_non_lvalue (fold_convert (type, tem));
4307 break;
4308 case GE_EXPR:
4309 case GT_EXPR:
4310 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4312 comp_op0 = fold_convert (comp_type, comp_op0);
4313 comp_op1 = fold_convert (comp_type, comp_op1);
4314 tem = fold (build2 (MAX_EXPR, comp_type,
4315 (comp_code == GE_EXPR
4316 ? comp_op0 : comp_op1),
4317 (comp_code == GE_EXPR
4318 ? comp_op1 : comp_op0)));
4319 tem = fold (build2 (MAX_EXPR, comp_type, comp_op0, comp_op1));
4320 return pedantic_non_lvalue (fold_convert (type, tem));
4322 break;
4323 default:
4324 abort ();
4328 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
4329 we might still be able to simplify this. For example,
4330 if C1 is one less or one more than C2, this might have started
4331 out as a MIN or MAX and been transformed by this function.
4332 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
4334 if (INTEGRAL_TYPE_P (type)
4335 && TREE_CODE (arg01) == INTEGER_CST
4336 && TREE_CODE (arg2) == INTEGER_CST)
4337 switch (comp_code)
4339 case EQ_EXPR:
4340 /* We can replace A with C1 in this case. */
4341 arg1 = fold_convert (type, arg01);
4342 return fold (build3 (COND_EXPR, type, arg0, arg1, arg2));
4344 case LT_EXPR:
4345 /* If C1 is C2 + 1, this is min(A, C2). */
4346 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4347 OEP_ONLY_CONST)
4348 && operand_equal_p (arg01,
4349 const_binop (PLUS_EXPR, arg2,
4350 integer_one_node, 0),
4351 OEP_ONLY_CONST))
4352 return pedantic_non_lvalue (fold (build2 (MIN_EXPR,
4353 type, arg1, arg2)));
4354 break;
4356 case LE_EXPR:
4357 /* If C1 is C2 - 1, this is min(A, C2). */
4358 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4359 OEP_ONLY_CONST)
4360 && operand_equal_p (arg01,
4361 const_binop (MINUS_EXPR, arg2,
4362 integer_one_node, 0),
4363 OEP_ONLY_CONST))
4364 return pedantic_non_lvalue (fold (build2 (MIN_EXPR,
4365 type, arg1, arg2)));
4366 break;
4368 case GT_EXPR:
4369 /* If C1 is C2 - 1, this is max(A, C2). */
4370 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4371 OEP_ONLY_CONST)
4372 && operand_equal_p (arg01,
4373 const_binop (MINUS_EXPR, arg2,
4374 integer_one_node, 0),
4375 OEP_ONLY_CONST))
4376 return pedantic_non_lvalue (fold (build2 (MAX_EXPR,
4377 type, arg1, arg2)));
4378 break;
4380 case GE_EXPR:
4381 /* If C1 is C2 + 1, this is max(A, C2). */
4382 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4383 OEP_ONLY_CONST)
4384 && operand_equal_p (arg01,
4385 const_binop (PLUS_EXPR, arg2,
4386 integer_one_node, 0),
4387 OEP_ONLY_CONST))
4388 return pedantic_non_lvalue (fold (build2 (MAX_EXPR,
4389 type, arg1, arg2)));
4390 break;
4391 case NE_EXPR:
4392 break;
4393 default:
4394 abort ();
4397 return NULL_TREE;
4402 #ifndef RANGE_TEST_NON_SHORT_CIRCUIT
4403 #define RANGE_TEST_NON_SHORT_CIRCUIT (BRANCH_COST >= 2)
4404 #endif
4406 /* EXP is some logical combination of boolean tests. See if we can
4407 merge it into some range test. Return the new tree if so. */
4409 static tree
4410 fold_range_test (tree exp)
4412 int or_op = (TREE_CODE (exp) == TRUTH_ORIF_EXPR
4413 || TREE_CODE (exp) == TRUTH_OR_EXPR);
4414 int in0_p, in1_p, in_p;
4415 tree low0, low1, low, high0, high1, high;
4416 tree lhs = make_range (TREE_OPERAND (exp, 0), &in0_p, &low0, &high0);
4417 tree rhs = make_range (TREE_OPERAND (exp, 1), &in1_p, &low1, &high1);
4418 tree tem;
4420 /* If this is an OR operation, invert both sides; we will invert
4421 again at the end. */
4422 if (or_op)
4423 in0_p = ! in0_p, in1_p = ! in1_p;
4425 /* If both expressions are the same, if we can merge the ranges, and we
4426 can build the range test, return it or it inverted. If one of the
4427 ranges is always true or always false, consider it to be the same
4428 expression as the other. */
4429 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
4430 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
4431 in1_p, low1, high1)
4432 && 0 != (tem = (build_range_check (TREE_TYPE (exp),
4433 lhs != 0 ? lhs
4434 : rhs != 0 ? rhs : integer_zero_node,
4435 in_p, low, high))))
4436 return or_op ? invert_truthvalue (tem) : tem;
4438 /* On machines where the branch cost is expensive, if this is a
4439 short-circuited branch and the underlying object on both sides
4440 is the same, make a non-short-circuit operation. */
4441 else if (RANGE_TEST_NON_SHORT_CIRCUIT
4442 && lhs != 0 && rhs != 0
4443 && (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
4444 || TREE_CODE (exp) == TRUTH_ORIF_EXPR)
4445 && operand_equal_p (lhs, rhs, 0))
4447 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
4448 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
4449 which cases we can't do this. */
4450 if (simple_operand_p (lhs))
4451 return build2 (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
4452 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4453 TREE_TYPE (exp), TREE_OPERAND (exp, 0),
4454 TREE_OPERAND (exp, 1));
4456 else if (lang_hooks.decls.global_bindings_p () == 0
4457 && ! CONTAINS_PLACEHOLDER_P (lhs))
4459 tree common = save_expr (lhs);
4461 if (0 != (lhs = build_range_check (TREE_TYPE (exp), common,
4462 or_op ? ! in0_p : in0_p,
4463 low0, high0))
4464 && (0 != (rhs = build_range_check (TREE_TYPE (exp), common,
4465 or_op ? ! in1_p : in1_p,
4466 low1, high1))))
4467 return build2 (TREE_CODE (exp) == TRUTH_ANDIF_EXPR
4468 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4469 TREE_TYPE (exp), lhs, rhs);
4473 return 0;
4476 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
4477 bit value. Arrange things so the extra bits will be set to zero if and
4478 only if C is signed-extended to its full width. If MASK is nonzero,
4479 it is an INTEGER_CST that should be AND'ed with the extra bits. */
4481 static tree
4482 unextend (tree c, int p, int unsignedp, tree mask)
4484 tree type = TREE_TYPE (c);
4485 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
4486 tree temp;
4488 if (p == modesize || unsignedp)
4489 return c;
4491 /* We work by getting just the sign bit into the low-order bit, then
4492 into the high-order bit, then sign-extend. We then XOR that value
4493 with C. */
4494 temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
4495 temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
4497 /* We must use a signed type in order to get an arithmetic right shift.
4498 However, we must also avoid introducing accidental overflows, so that
4499 a subsequent call to integer_zerop will work. Hence we must
4500 do the type conversion here. At this point, the constant is either
4501 zero or one, and the conversion to a signed type can never overflow.
4502 We could get an overflow if this conversion is done anywhere else. */
4503 if (TYPE_UNSIGNED (type))
4504 temp = fold_convert (lang_hooks.types.signed_type (type), temp);
4506 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
4507 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
4508 if (mask != 0)
4509 temp = const_binop (BIT_AND_EXPR, temp,
4510 fold_convert (TREE_TYPE (c), mask), 0);
4511 /* If necessary, convert the type back to match the type of C. */
4512 if (TYPE_UNSIGNED (type))
4513 temp = fold_convert (type, temp);
4515 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
4518 /* Find ways of folding logical expressions of LHS and RHS:
4519 Try to merge two comparisons to the same innermost item.
4520 Look for range tests like "ch >= '0' && ch <= '9'".
4521 Look for combinations of simple terms on machines with expensive branches
4522 and evaluate the RHS unconditionally.
4524 For example, if we have p->a == 2 && p->b == 4 and we can make an
4525 object large enough to span both A and B, we can do this with a comparison
4526 against the object ANDed with the a mask.
4528 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
4529 operations to do this with one comparison.
4531 We check for both normal comparisons and the BIT_AND_EXPRs made this by
4532 function and the one above.
4534 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
4535 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
4537 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
4538 two operands.
4540 We return the simplified tree or 0 if no optimization is possible. */
4542 static tree
4543 fold_truthop (enum tree_code code, tree truth_type, tree lhs, tree rhs)
4545 /* If this is the "or" of two comparisons, we can do something if
4546 the comparisons are NE_EXPR. If this is the "and", we can do something
4547 if the comparisons are EQ_EXPR. I.e.,
4548 (a->b == 2 && a->c == 4) can become (a->new == NEW).
4550 WANTED_CODE is this operation code. For single bit fields, we can
4551 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
4552 comparison for one-bit fields. */
4554 enum tree_code wanted_code;
4555 enum tree_code lcode, rcode;
4556 tree ll_arg, lr_arg, rl_arg, rr_arg;
4557 tree ll_inner, lr_inner, rl_inner, rr_inner;
4558 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
4559 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
4560 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
4561 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
4562 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
4563 enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
4564 enum machine_mode lnmode, rnmode;
4565 tree ll_mask, lr_mask, rl_mask, rr_mask;
4566 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
4567 tree l_const, r_const;
4568 tree lntype, rntype, result;
4569 int first_bit, end_bit;
4570 int volatilep;
4572 /* Start by getting the comparison codes. Fail if anything is volatile.
4573 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
4574 it were surrounded with a NE_EXPR. */
4576 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
4577 return 0;
4579 lcode = TREE_CODE (lhs);
4580 rcode = TREE_CODE (rhs);
4582 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
4584 lhs = build2 (NE_EXPR, truth_type, lhs,
4585 fold_convert (TREE_TYPE (lhs), integer_zero_node));
4586 lcode = NE_EXPR;
4589 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
4591 rhs = build2 (NE_EXPR, truth_type, rhs,
4592 fold_convert (TREE_TYPE (rhs), integer_zero_node));
4593 rcode = NE_EXPR;
4596 if (TREE_CODE_CLASS (lcode) != '<' || TREE_CODE_CLASS (rcode) != '<')
4597 return 0;
4599 ll_arg = TREE_OPERAND (lhs, 0);
4600 lr_arg = TREE_OPERAND (lhs, 1);
4601 rl_arg = TREE_OPERAND (rhs, 0);
4602 rr_arg = TREE_OPERAND (rhs, 1);
4604 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
4605 if (simple_operand_p (ll_arg)
4606 && simple_operand_p (lr_arg))
4608 tree result;
4609 if (operand_equal_p (ll_arg, rl_arg, 0)
4610 && operand_equal_p (lr_arg, rr_arg, 0))
4612 result = combine_comparisons (code, lcode, rcode,
4613 truth_type, ll_arg, lr_arg);
4614 if (result)
4615 return result;
4617 else if (operand_equal_p (ll_arg, rr_arg, 0)
4618 && operand_equal_p (lr_arg, rl_arg, 0))
4620 result = combine_comparisons (code, lcode,
4621 swap_tree_comparison (rcode),
4622 truth_type, ll_arg, lr_arg);
4623 if (result)
4624 return result;
4628 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
4629 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
4631 /* If the RHS can be evaluated unconditionally and its operands are
4632 simple, it wins to evaluate the RHS unconditionally on machines
4633 with expensive branches. In this case, this isn't a comparison
4634 that can be merged. Avoid doing this if the RHS is a floating-point
4635 comparison since those can trap. */
4637 if (BRANCH_COST >= 2
4638 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
4639 && simple_operand_p (rl_arg)
4640 && simple_operand_p (rr_arg))
4642 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
4643 if (code == TRUTH_OR_EXPR
4644 && lcode == NE_EXPR && integer_zerop (lr_arg)
4645 && rcode == NE_EXPR && integer_zerop (rr_arg)
4646 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg))
4647 return build2 (NE_EXPR, truth_type,
4648 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
4649 ll_arg, rl_arg),
4650 fold_convert (TREE_TYPE (ll_arg), integer_zero_node));
4652 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
4653 if (code == TRUTH_AND_EXPR
4654 && lcode == EQ_EXPR && integer_zerop (lr_arg)
4655 && rcode == EQ_EXPR && integer_zerop (rr_arg)
4656 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg))
4657 return build2 (EQ_EXPR, truth_type,
4658 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
4659 ll_arg, rl_arg),
4660 fold_convert (TREE_TYPE (ll_arg), integer_zero_node));
4662 return build2 (code, truth_type, lhs, rhs);
4665 /* See if the comparisons can be merged. Then get all the parameters for
4666 each side. */
4668 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
4669 || (rcode != EQ_EXPR && rcode != NE_EXPR))
4670 return 0;
4672 volatilep = 0;
4673 ll_inner = decode_field_reference (ll_arg,
4674 &ll_bitsize, &ll_bitpos, &ll_mode,
4675 &ll_unsignedp, &volatilep, &ll_mask,
4676 &ll_and_mask);
4677 lr_inner = decode_field_reference (lr_arg,
4678 &lr_bitsize, &lr_bitpos, &lr_mode,
4679 &lr_unsignedp, &volatilep, &lr_mask,
4680 &lr_and_mask);
4681 rl_inner = decode_field_reference (rl_arg,
4682 &rl_bitsize, &rl_bitpos, &rl_mode,
4683 &rl_unsignedp, &volatilep, &rl_mask,
4684 &rl_and_mask);
4685 rr_inner = decode_field_reference (rr_arg,
4686 &rr_bitsize, &rr_bitpos, &rr_mode,
4687 &rr_unsignedp, &volatilep, &rr_mask,
4688 &rr_and_mask);
4690 /* It must be true that the inner operation on the lhs of each
4691 comparison must be the same if we are to be able to do anything.
4692 Then see if we have constants. If not, the same must be true for
4693 the rhs's. */
4694 if (volatilep || ll_inner == 0 || rl_inner == 0
4695 || ! operand_equal_p (ll_inner, rl_inner, 0))
4696 return 0;
4698 if (TREE_CODE (lr_arg) == INTEGER_CST
4699 && TREE_CODE (rr_arg) == INTEGER_CST)
4700 l_const = lr_arg, r_const = rr_arg;
4701 else if (lr_inner == 0 || rr_inner == 0
4702 || ! operand_equal_p (lr_inner, rr_inner, 0))
4703 return 0;
4704 else
4705 l_const = r_const = 0;
4707 /* If either comparison code is not correct for our logical operation,
4708 fail. However, we can convert a one-bit comparison against zero into
4709 the opposite comparison against that bit being set in the field. */
4711 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
4712 if (lcode != wanted_code)
4714 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
4716 /* Make the left operand unsigned, since we are only interested
4717 in the value of one bit. Otherwise we are doing the wrong
4718 thing below. */
4719 ll_unsignedp = 1;
4720 l_const = ll_mask;
4722 else
4723 return 0;
4726 /* This is analogous to the code for l_const above. */
4727 if (rcode != wanted_code)
4729 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
4731 rl_unsignedp = 1;
4732 r_const = rl_mask;
4734 else
4735 return 0;
4738 /* After this point all optimizations will generate bit-field
4739 references, which we might not want. */
4740 if (! lang_hooks.can_use_bit_fields_p ())
4741 return 0;
4743 /* See if we can find a mode that contains both fields being compared on
4744 the left. If we can't, fail. Otherwise, update all constants and masks
4745 to be relative to a field of that size. */
4746 first_bit = MIN (ll_bitpos, rl_bitpos);
4747 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
4748 lnmode = get_best_mode (end_bit - first_bit, first_bit,
4749 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
4750 volatilep);
4751 if (lnmode == VOIDmode)
4752 return 0;
4754 lnbitsize = GET_MODE_BITSIZE (lnmode);
4755 lnbitpos = first_bit & ~ (lnbitsize - 1);
4756 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
4757 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
4759 if (BYTES_BIG_ENDIAN)
4761 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
4762 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
4765 ll_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, ll_mask),
4766 size_int (xll_bitpos), 0);
4767 rl_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, rl_mask),
4768 size_int (xrl_bitpos), 0);
4770 if (l_const)
4772 l_const = fold_convert (lntype, l_const);
4773 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
4774 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
4775 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
4776 fold (build1 (BIT_NOT_EXPR,
4777 lntype, ll_mask)),
4778 0)))
4780 warning ("comparison is always %d", wanted_code == NE_EXPR);
4782 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
4785 if (r_const)
4787 r_const = fold_convert (lntype, r_const);
4788 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
4789 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
4790 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
4791 fold (build1 (BIT_NOT_EXPR,
4792 lntype, rl_mask)),
4793 0)))
4795 warning ("comparison is always %d", wanted_code == NE_EXPR);
4797 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
4801 /* If the right sides are not constant, do the same for it. Also,
4802 disallow this optimization if a size or signedness mismatch occurs
4803 between the left and right sides. */
4804 if (l_const == 0)
4806 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
4807 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
4808 /* Make sure the two fields on the right
4809 correspond to the left without being swapped. */
4810 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
4811 return 0;
4813 first_bit = MIN (lr_bitpos, rr_bitpos);
4814 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
4815 rnmode = get_best_mode (end_bit - first_bit, first_bit,
4816 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
4817 volatilep);
4818 if (rnmode == VOIDmode)
4819 return 0;
4821 rnbitsize = GET_MODE_BITSIZE (rnmode);
4822 rnbitpos = first_bit & ~ (rnbitsize - 1);
4823 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
4824 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
4826 if (BYTES_BIG_ENDIAN)
4828 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
4829 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
4832 lr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, lr_mask),
4833 size_int (xlr_bitpos), 0);
4834 rr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, rr_mask),
4835 size_int (xrr_bitpos), 0);
4837 /* Make a mask that corresponds to both fields being compared.
4838 Do this for both items being compared. If the operands are the
4839 same size and the bits being compared are in the same position
4840 then we can do this by masking both and comparing the masked
4841 results. */
4842 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
4843 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
4844 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
4846 lhs = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
4847 ll_unsignedp || rl_unsignedp);
4848 if (! all_ones_mask_p (ll_mask, lnbitsize))
4849 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
4851 rhs = make_bit_field_ref (lr_inner, rntype, rnbitsize, rnbitpos,
4852 lr_unsignedp || rr_unsignedp);
4853 if (! all_ones_mask_p (lr_mask, rnbitsize))
4854 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
4856 return build2 (wanted_code, truth_type, lhs, rhs);
4859 /* There is still another way we can do something: If both pairs of
4860 fields being compared are adjacent, we may be able to make a wider
4861 field containing them both.
4863 Note that we still must mask the lhs/rhs expressions. Furthermore,
4864 the mask must be shifted to account for the shift done by
4865 make_bit_field_ref. */
4866 if ((ll_bitsize + ll_bitpos == rl_bitpos
4867 && lr_bitsize + lr_bitpos == rr_bitpos)
4868 || (ll_bitpos == rl_bitpos + rl_bitsize
4869 && lr_bitpos == rr_bitpos + rr_bitsize))
4871 tree type;
4873 lhs = make_bit_field_ref (ll_inner, lntype, ll_bitsize + rl_bitsize,
4874 MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
4875 rhs = make_bit_field_ref (lr_inner, rntype, lr_bitsize + rr_bitsize,
4876 MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
4878 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
4879 size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
4880 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
4881 size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
4883 /* Convert to the smaller type before masking out unwanted bits. */
4884 type = lntype;
4885 if (lntype != rntype)
4887 if (lnbitsize > rnbitsize)
4889 lhs = fold_convert (rntype, lhs);
4890 ll_mask = fold_convert (rntype, ll_mask);
4891 type = rntype;
4893 else if (lnbitsize < rnbitsize)
4895 rhs = fold_convert (lntype, rhs);
4896 lr_mask = fold_convert (lntype, lr_mask);
4897 type = lntype;
4901 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
4902 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
4904 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
4905 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
4907 return build2 (wanted_code, truth_type, lhs, rhs);
4910 return 0;
4913 /* Handle the case of comparisons with constants. If there is something in
4914 common between the masks, those bits of the constants must be the same.
4915 If not, the condition is always false. Test for this to avoid generating
4916 incorrect code below. */
4917 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
4918 if (! integer_zerop (result)
4919 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
4920 const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
4922 if (wanted_code == NE_EXPR)
4924 warning ("`or' of unmatched not-equal tests is always 1");
4925 return constant_boolean_node (true, truth_type);
4927 else
4929 warning ("`and' of mutually exclusive equal-tests is always 0");
4930 return constant_boolean_node (false, truth_type);
4934 /* Construct the expression we will return. First get the component
4935 reference we will make. Unless the mask is all ones the width of
4936 that field, perform the mask operation. Then compare with the
4937 merged constant. */
4938 result = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
4939 ll_unsignedp || rl_unsignedp);
4941 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
4942 if (! all_ones_mask_p (ll_mask, lnbitsize))
4943 result = build2 (BIT_AND_EXPR, lntype, result, ll_mask);
4945 return build2 (wanted_code, truth_type, result,
4946 const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
4949 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
4950 constant. */
4952 static tree
4953 optimize_minmax_comparison (tree t)
4955 tree type = TREE_TYPE (t);
4956 tree arg0 = TREE_OPERAND (t, 0);
4957 enum tree_code op_code;
4958 tree comp_const = TREE_OPERAND (t, 1);
4959 tree minmax_const;
4960 int consts_equal, consts_lt;
4961 tree inner;
4963 STRIP_SIGN_NOPS (arg0);
4965 op_code = TREE_CODE (arg0);
4966 minmax_const = TREE_OPERAND (arg0, 1);
4967 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
4968 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
4969 inner = TREE_OPERAND (arg0, 0);
4971 /* If something does not permit us to optimize, return the original tree. */
4972 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
4973 || TREE_CODE (comp_const) != INTEGER_CST
4974 || TREE_CONSTANT_OVERFLOW (comp_const)
4975 || TREE_CODE (minmax_const) != INTEGER_CST
4976 || TREE_CONSTANT_OVERFLOW (minmax_const))
4977 return t;
4979 /* Now handle all the various comparison codes. We only handle EQ_EXPR
4980 and GT_EXPR, doing the rest with recursive calls using logical
4981 simplifications. */
4982 switch (TREE_CODE (t))
4984 case NE_EXPR: case LT_EXPR: case LE_EXPR:
4985 return
4986 invert_truthvalue (optimize_minmax_comparison (invert_truthvalue (t)));
4988 case GE_EXPR:
4989 return
4990 fold (build2 (TRUTH_ORIF_EXPR, type,
4991 optimize_minmax_comparison
4992 (build2 (EQ_EXPR, type, arg0, comp_const)),
4993 optimize_minmax_comparison
4994 (build2 (GT_EXPR, type, arg0, comp_const))));
4996 case EQ_EXPR:
4997 if (op_code == MAX_EXPR && consts_equal)
4998 /* MAX (X, 0) == 0 -> X <= 0 */
4999 return fold (build2 (LE_EXPR, type, inner, comp_const));
5001 else if (op_code == MAX_EXPR && consts_lt)
5002 /* MAX (X, 0) == 5 -> X == 5 */
5003 return fold (build2 (EQ_EXPR, type, inner, comp_const));
5005 else if (op_code == MAX_EXPR)
5006 /* MAX (X, 0) == -1 -> false */
5007 return omit_one_operand (type, integer_zero_node, inner);
5009 else if (consts_equal)
5010 /* MIN (X, 0) == 0 -> X >= 0 */
5011 return fold (build2 (GE_EXPR, type, inner, comp_const));
5013 else if (consts_lt)
5014 /* MIN (X, 0) == 5 -> false */
5015 return omit_one_operand (type, integer_zero_node, inner);
5017 else
5018 /* MIN (X, 0) == -1 -> X == -1 */
5019 return fold (build2 (EQ_EXPR, type, inner, comp_const));
5021 case GT_EXPR:
5022 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5023 /* MAX (X, 0) > 0 -> X > 0
5024 MAX (X, 0) > 5 -> X > 5 */
5025 return fold (build2 (GT_EXPR, type, inner, comp_const));
5027 else if (op_code == MAX_EXPR)
5028 /* MAX (X, 0) > -1 -> true */
5029 return omit_one_operand (type, integer_one_node, inner);
5031 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5032 /* MIN (X, 0) > 0 -> false
5033 MIN (X, 0) > 5 -> false */
5034 return omit_one_operand (type, integer_zero_node, inner);
5036 else
5037 /* MIN (X, 0) > -1 -> X > -1 */
5038 return fold (build2 (GT_EXPR, type, inner, comp_const));
5040 default:
5041 return t;
5045 /* T is an integer expression that is being multiplied, divided, or taken a
5046 modulus (CODE says which and what kind of divide or modulus) by a
5047 constant C. See if we can eliminate that operation by folding it with
5048 other operations already in T. WIDE_TYPE, if non-null, is a type that
5049 should be used for the computation if wider than our type.
5051 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5052 (X * 2) + (Y * 4). We must, however, be assured that either the original
5053 expression would not overflow or that overflow is undefined for the type
5054 in the language in question.
5056 We also canonicalize (X + 7) * 4 into X * 4 + 28 in the hope that either
5057 the machine has a multiply-accumulate insn or that this is part of an
5058 addressing calculation.
5060 If we return a non-null expression, it is an equivalent form of the
5061 original computation, but need not be in the original type. */
5063 static tree
5064 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type)
5066 /* To avoid exponential search depth, refuse to allow recursion past
5067 three levels. Beyond that (1) it's highly unlikely that we'll find
5068 something interesting and (2) we've probably processed it before
5069 when we built the inner expression. */
5071 static int depth;
5072 tree ret;
5074 if (depth > 3)
5075 return NULL;
5077 depth++;
5078 ret = extract_muldiv_1 (t, c, code, wide_type);
5079 depth--;
5081 return ret;
5084 static tree
5085 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type)
5087 tree type = TREE_TYPE (t);
5088 enum tree_code tcode = TREE_CODE (t);
5089 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
5090 > GET_MODE_SIZE (TYPE_MODE (type)))
5091 ? wide_type : type);
5092 tree t1, t2;
5093 int same_p = tcode == code;
5094 tree op0 = NULL_TREE, op1 = NULL_TREE;
5096 /* Don't deal with constants of zero here; they confuse the code below. */
5097 if (integer_zerop (c))
5098 return NULL_TREE;
5100 if (TREE_CODE_CLASS (tcode) == '1')
5101 op0 = TREE_OPERAND (t, 0);
5103 if (TREE_CODE_CLASS (tcode) == '2')
5104 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
5106 /* Note that we need not handle conditional operations here since fold
5107 already handles those cases. So just do arithmetic here. */
5108 switch (tcode)
5110 case INTEGER_CST:
5111 /* For a constant, we can always simplify if we are a multiply
5112 or (for divide and modulus) if it is a multiple of our constant. */
5113 if (code == MULT_EXPR
5114 || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
5115 return const_binop (code, fold_convert (ctype, t),
5116 fold_convert (ctype, c), 0);
5117 break;
5119 case CONVERT_EXPR: case NON_LVALUE_EXPR: case NOP_EXPR:
5120 /* If op0 is an expression ... */
5121 if ((TREE_CODE_CLASS (TREE_CODE (op0)) == '<'
5122 || TREE_CODE_CLASS (TREE_CODE (op0)) == '1'
5123 || TREE_CODE_CLASS (TREE_CODE (op0)) == '2'
5124 || TREE_CODE_CLASS (TREE_CODE (op0)) == 'e')
5125 /* ... and is unsigned, and its type is smaller than ctype,
5126 then we cannot pass through as widening. */
5127 && ((TYPE_UNSIGNED (TREE_TYPE (op0))
5128 && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
5129 && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
5130 && (GET_MODE_SIZE (TYPE_MODE (ctype))
5131 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))))
5132 /* ... or this is a truncation (t is narrower than op0),
5133 then we cannot pass through this narrowing. */
5134 || (GET_MODE_SIZE (TYPE_MODE (type))
5135 < GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0))))
5136 /* ... or signedness changes for division or modulus,
5137 then we cannot pass through this conversion. */
5138 || (code != MULT_EXPR
5139 && (TYPE_UNSIGNED (ctype)
5140 != TYPE_UNSIGNED (TREE_TYPE (op0))))))
5141 break;
5143 /* Pass the constant down and see if we can make a simplification. If
5144 we can, replace this expression with the inner simplification for
5145 possible later conversion to our or some other type. */
5146 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
5147 && TREE_CODE (t2) == INTEGER_CST
5148 && ! TREE_CONSTANT_OVERFLOW (t2)
5149 && (0 != (t1 = extract_muldiv (op0, t2, code,
5150 code == MULT_EXPR
5151 ? ctype : NULL_TREE))))
5152 return t1;
5153 break;
5155 case NEGATE_EXPR: case ABS_EXPR:
5156 if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
5157 return fold (build1 (tcode, ctype, fold_convert (ctype, t1)));
5158 break;
5160 case MIN_EXPR: case MAX_EXPR:
5161 /* If widening the type changes the signedness, then we can't perform
5162 this optimization as that changes the result. */
5163 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
5164 break;
5166 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
5167 if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0
5168 && (t2 = extract_muldiv (op1, c, code, wide_type)) != 0)
5170 if (tree_int_cst_sgn (c) < 0)
5171 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
5173 return fold (build2 (tcode, ctype, fold_convert (ctype, t1),
5174 fold_convert (ctype, t2)));
5176 break;
5178 case LSHIFT_EXPR: case RSHIFT_EXPR:
5179 /* If the second operand is constant, this is a multiplication
5180 or floor division, by a power of two, so we can treat it that
5181 way unless the multiplier or divisor overflows. Signed
5182 left-shift overflow is implementation-defined rather than
5183 undefined in C90, so do not convert signed left shift into
5184 multiplication. */
5185 if (TREE_CODE (op1) == INTEGER_CST
5186 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
5187 /* const_binop may not detect overflow correctly,
5188 so check for it explicitly here. */
5189 && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
5190 && TREE_INT_CST_HIGH (op1) == 0
5191 && 0 != (t1 = fold_convert (ctype,
5192 const_binop (LSHIFT_EXPR,
5193 size_one_node,
5194 op1, 0)))
5195 && ! TREE_OVERFLOW (t1))
5196 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
5197 ? MULT_EXPR : FLOOR_DIV_EXPR,
5198 ctype, fold_convert (ctype, op0), t1),
5199 c, code, wide_type);
5200 break;
5202 case PLUS_EXPR: case MINUS_EXPR:
5203 /* See if we can eliminate the operation on both sides. If we can, we
5204 can return a new PLUS or MINUS. If we can't, the only remaining
5205 cases where we can do anything are if the second operand is a
5206 constant. */
5207 t1 = extract_muldiv (op0, c, code, wide_type);
5208 t2 = extract_muldiv (op1, c, code, wide_type);
5209 if (t1 != 0 && t2 != 0
5210 && (code == MULT_EXPR
5211 /* If not multiplication, we can only do this if both operands
5212 are divisible by c. */
5213 || (multiple_of_p (ctype, op0, c)
5214 && multiple_of_p (ctype, op1, c))))
5215 return fold (build2 (tcode, ctype, fold_convert (ctype, t1),
5216 fold_convert (ctype, t2)));
5218 /* If this was a subtraction, negate OP1 and set it to be an addition.
5219 This simplifies the logic below. */
5220 if (tcode == MINUS_EXPR)
5221 tcode = PLUS_EXPR, op1 = negate_expr (op1);
5223 if (TREE_CODE (op1) != INTEGER_CST)
5224 break;
5226 /* If either OP1 or C are negative, this optimization is not safe for
5227 some of the division and remainder types while for others we need
5228 to change the code. */
5229 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
5231 if (code == CEIL_DIV_EXPR)
5232 code = FLOOR_DIV_EXPR;
5233 else if (code == FLOOR_DIV_EXPR)
5234 code = CEIL_DIV_EXPR;
5235 else if (code != MULT_EXPR
5236 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
5237 break;
5240 /* If it's a multiply or a division/modulus operation of a multiple
5241 of our constant, do the operation and verify it doesn't overflow. */
5242 if (code == MULT_EXPR
5243 || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5245 op1 = const_binop (code, fold_convert (ctype, op1),
5246 fold_convert (ctype, c), 0);
5247 /* We allow the constant to overflow with wrapping semantics. */
5248 if (op1 == 0
5249 || (TREE_OVERFLOW (op1) && ! flag_wrapv))
5250 break;
5252 else
5253 break;
5255 /* If we have an unsigned type is not a sizetype, we cannot widen
5256 the operation since it will change the result if the original
5257 computation overflowed. */
5258 if (TYPE_UNSIGNED (ctype)
5259 && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
5260 && ctype != type)
5261 break;
5263 /* If we were able to eliminate our operation from the first side,
5264 apply our operation to the second side and reform the PLUS. */
5265 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
5266 return fold (build2 (tcode, ctype, fold_convert (ctype, t1), op1));
5268 /* The last case is if we are a multiply. In that case, we can
5269 apply the distributive law to commute the multiply and addition
5270 if the multiplication of the constants doesn't overflow. */
5271 if (code == MULT_EXPR)
5272 return fold (build2 (tcode, ctype,
5273 fold (build2 (code, ctype,
5274 fold_convert (ctype, op0),
5275 fold_convert (ctype, c))),
5276 op1));
5278 break;
5280 case MULT_EXPR:
5281 /* We have a special case here if we are doing something like
5282 (C * 8) % 4 since we know that's zero. */
5283 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
5284 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
5285 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
5286 && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5287 return omit_one_operand (type, integer_zero_node, op0);
5289 /* ... fall through ... */
5291 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
5292 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
5293 /* If we can extract our operation from the LHS, do so and return a
5294 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
5295 do something only if the second operand is a constant. */
5296 if (same_p
5297 && (t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
5298 return fold (build2 (tcode, ctype, fold_convert (ctype, t1),
5299 fold_convert (ctype, op1)));
5300 else if (tcode == MULT_EXPR && code == MULT_EXPR
5301 && (t1 = extract_muldiv (op1, c, code, wide_type)) != 0)
5302 return fold (build2 (tcode, ctype, fold_convert (ctype, op0),
5303 fold_convert (ctype, t1)));
5304 else if (TREE_CODE (op1) != INTEGER_CST)
5305 return 0;
5307 /* If these are the same operation types, we can associate them
5308 assuming no overflow. */
5309 if (tcode == code
5310 && 0 != (t1 = const_binop (MULT_EXPR, fold_convert (ctype, op1),
5311 fold_convert (ctype, c), 0))
5312 && ! TREE_OVERFLOW (t1))
5313 return fold (build2 (tcode, ctype, fold_convert (ctype, op0), t1));
5315 /* If these operations "cancel" each other, we have the main
5316 optimizations of this pass, which occur when either constant is a
5317 multiple of the other, in which case we replace this with either an
5318 operation or CODE or TCODE.
5320 If we have an unsigned type that is not a sizetype, we cannot do
5321 this since it will change the result if the original computation
5322 overflowed. */
5323 if ((! TYPE_UNSIGNED (ctype)
5324 || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
5325 && ! flag_wrapv
5326 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
5327 || (tcode == MULT_EXPR
5328 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
5329 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR)))
5331 if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5332 return fold (build2 (tcode, ctype, fold_convert (ctype, op0),
5333 fold_convert (ctype,
5334 const_binop (TRUNC_DIV_EXPR,
5335 op1, c, 0))));
5336 else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
5337 return fold (build2 (code, ctype, fold_convert (ctype, op0),
5338 fold_convert (ctype,
5339 const_binop (TRUNC_DIV_EXPR,
5340 c, op1, 0))));
5342 break;
5344 default:
5345 break;
5348 return 0;
5351 /* Return a node which has the indicated constant VALUE (either 0 or
5352 1), and is of the indicated TYPE. */
5354 tree
5355 constant_boolean_node (int value, tree type)
5357 if (type == integer_type_node)
5358 return value ? integer_one_node : integer_zero_node;
5359 else if (type == boolean_type_node)
5360 return value ? boolean_true_node : boolean_false_node;
5361 else if (TREE_CODE (type) == BOOLEAN_TYPE)
5362 return lang_hooks.truthvalue_conversion (value ? integer_one_node
5363 : integer_zero_node);
5364 else
5366 tree t = build_int_2 (value, 0);
5368 TREE_TYPE (t) = type;
5369 return t;
5373 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
5374 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
5375 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
5376 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
5377 COND is the first argument to CODE; otherwise (as in the example
5378 given here), it is the second argument. TYPE is the type of the
5379 original expression. Return NULL_TREE if no simplification is
5380 possible. */
5382 static tree
5383 fold_binary_op_with_conditional_arg (enum tree_code code, tree type,
5384 tree cond, tree arg, int cond_first_p)
5386 tree test, true_value, false_value;
5387 tree lhs = NULL_TREE;
5388 tree rhs = NULL_TREE;
5390 /* This transformation is only worthwhile if we don't have to wrap
5391 arg in a SAVE_EXPR, and the operation can be simplified on atleast
5392 one of the branches once its pushed inside the COND_EXPR. */
5393 if (!TREE_CONSTANT (arg))
5394 return NULL_TREE;
5396 if (TREE_CODE (cond) == COND_EXPR)
5398 test = TREE_OPERAND (cond, 0);
5399 true_value = TREE_OPERAND (cond, 1);
5400 false_value = TREE_OPERAND (cond, 2);
5401 /* If this operand throws an expression, then it does not make
5402 sense to try to perform a logical or arithmetic operation
5403 involving it. */
5404 if (VOID_TYPE_P (TREE_TYPE (true_value)))
5405 lhs = true_value;
5406 if (VOID_TYPE_P (TREE_TYPE (false_value)))
5407 rhs = false_value;
5409 else
5411 tree testtype = TREE_TYPE (cond);
5412 test = cond;
5413 true_value = constant_boolean_node (true, testtype);
5414 false_value = constant_boolean_node (false, testtype);
5417 if (lhs == 0)
5418 lhs = fold (cond_first_p ? build2 (code, type, true_value, arg)
5419 : build2 (code, type, arg, true_value));
5420 if (rhs == 0)
5421 rhs = fold (cond_first_p ? build2 (code, type, false_value, arg)
5422 : build2 (code, type, arg, false_value));
5424 test = fold (build3 (COND_EXPR, type, test, lhs, rhs));
5425 return fold_convert (type, test);
5429 /* Subroutine of fold() that checks for the addition of +/- 0.0.
5431 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
5432 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
5433 ADDEND is the same as X.
5435 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
5436 and finite. The problematic cases are when X is zero, and its mode
5437 has signed zeros. In the case of rounding towards -infinity,
5438 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
5439 modes, X + 0 is not the same as X because -0 + 0 is 0. */
5441 static bool
5442 fold_real_zero_addition_p (tree type, tree addend, int negate)
5444 if (!real_zerop (addend))
5445 return false;
5447 /* Don't allow the fold with -fsignaling-nans. */
5448 if (HONOR_SNANS (TYPE_MODE (type)))
5449 return false;
5451 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
5452 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
5453 return true;
5455 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
5456 if (TREE_CODE (addend) == REAL_CST
5457 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
5458 negate = !negate;
5460 /* The mode has signed zeros, and we have to honor their sign.
5461 In this situation, there is only one case we can return true for.
5462 X - 0 is the same as X unless rounding towards -infinity is
5463 supported. */
5464 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
5467 /* Subroutine of fold() that checks comparisons of built-in math
5468 functions against real constants.
5470 FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
5471 operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR. TYPE
5472 is the type of the result and ARG0 and ARG1 are the operands of the
5473 comparison. ARG1 must be a TREE_REAL_CST.
5475 The function returns the constant folded tree if a simplification
5476 can be made, and NULL_TREE otherwise. */
5478 static tree
5479 fold_mathfn_compare (enum built_in_function fcode, enum tree_code code,
5480 tree type, tree arg0, tree arg1)
5482 REAL_VALUE_TYPE c;
5484 if (BUILTIN_SQRT_P (fcode))
5486 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
5487 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
5489 c = TREE_REAL_CST (arg1);
5490 if (REAL_VALUE_NEGATIVE (c))
5492 /* sqrt(x) < y is always false, if y is negative. */
5493 if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
5494 return omit_one_operand (type, integer_zero_node, arg);
5496 /* sqrt(x) > y is always true, if y is negative and we
5497 don't care about NaNs, i.e. negative values of x. */
5498 if (code == NE_EXPR || !HONOR_NANS (mode))
5499 return omit_one_operand (type, integer_one_node, arg);
5501 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
5502 return fold (build2 (GE_EXPR, type, arg,
5503 build_real (TREE_TYPE (arg), dconst0)));
5505 else if (code == GT_EXPR || code == GE_EXPR)
5507 REAL_VALUE_TYPE c2;
5509 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
5510 real_convert (&c2, mode, &c2);
5512 if (REAL_VALUE_ISINF (c2))
5514 /* sqrt(x) > y is x == +Inf, when y is very large. */
5515 if (HONOR_INFINITIES (mode))
5516 return fold (build2 (EQ_EXPR, type, arg,
5517 build_real (TREE_TYPE (arg), c2)));
5519 /* sqrt(x) > y is always false, when y is very large
5520 and we don't care about infinities. */
5521 return omit_one_operand (type, integer_zero_node, arg);
5524 /* sqrt(x) > c is the same as x > c*c. */
5525 return fold (build2 (code, type, arg,
5526 build_real (TREE_TYPE (arg), c2)));
5528 else if (code == LT_EXPR || code == LE_EXPR)
5530 REAL_VALUE_TYPE c2;
5532 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
5533 real_convert (&c2, mode, &c2);
5535 if (REAL_VALUE_ISINF (c2))
5537 /* sqrt(x) < y is always true, when y is a very large
5538 value and we don't care about NaNs or Infinities. */
5539 if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
5540 return omit_one_operand (type, integer_one_node, arg);
5542 /* sqrt(x) < y is x != +Inf when y is very large and we
5543 don't care about NaNs. */
5544 if (! HONOR_NANS (mode))
5545 return fold (build2 (NE_EXPR, type, arg,
5546 build_real (TREE_TYPE (arg), c2)));
5548 /* sqrt(x) < y is x >= 0 when y is very large and we
5549 don't care about Infinities. */
5550 if (! HONOR_INFINITIES (mode))
5551 return fold (build2 (GE_EXPR, type, arg,
5552 build_real (TREE_TYPE (arg), dconst0)));
5554 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
5555 if (lang_hooks.decls.global_bindings_p () != 0
5556 || CONTAINS_PLACEHOLDER_P (arg))
5557 return NULL_TREE;
5559 arg = save_expr (arg);
5560 return fold (build2 (TRUTH_ANDIF_EXPR, type,
5561 fold (build2 (GE_EXPR, type, arg,
5562 build_real (TREE_TYPE (arg),
5563 dconst0))),
5564 fold (build2 (NE_EXPR, type, arg,
5565 build_real (TREE_TYPE (arg),
5566 c2)))));
5569 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
5570 if (! HONOR_NANS (mode))
5571 return fold (build2 (code, type, arg,
5572 build_real (TREE_TYPE (arg), c2)));
5574 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
5575 if (lang_hooks.decls.global_bindings_p () == 0
5576 && ! CONTAINS_PLACEHOLDER_P (arg))
5578 arg = save_expr (arg);
5579 return fold (build2 (TRUTH_ANDIF_EXPR, type,
5580 fold (build2 (GE_EXPR, type, arg,
5581 build_real (TREE_TYPE (arg),
5582 dconst0))),
5583 fold (build2 (code, type, arg,
5584 build_real (TREE_TYPE (arg),
5585 c2)))));
5590 return NULL_TREE;
5593 /* Subroutine of fold() that optimizes comparisons against Infinities,
5594 either +Inf or -Inf.
5596 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
5597 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
5598 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
5600 The function returns the constant folded tree if a simplification
5601 can be made, and NULL_TREE otherwise. */
5603 static tree
5604 fold_inf_compare (enum tree_code code, tree type, tree arg0, tree arg1)
5606 enum machine_mode mode;
5607 REAL_VALUE_TYPE max;
5608 tree temp;
5609 bool neg;
5611 mode = TYPE_MODE (TREE_TYPE (arg0));
5613 /* For negative infinity swap the sense of the comparison. */
5614 neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
5615 if (neg)
5616 code = swap_tree_comparison (code);
5618 switch (code)
5620 case GT_EXPR:
5621 /* x > +Inf is always false, if with ignore sNANs. */
5622 if (HONOR_SNANS (mode))
5623 return NULL_TREE;
5624 return omit_one_operand (type, integer_zero_node, arg0);
5626 case LE_EXPR:
5627 /* x <= +Inf is always true, if we don't case about NaNs. */
5628 if (! HONOR_NANS (mode))
5629 return omit_one_operand (type, integer_one_node, arg0);
5631 /* x <= +Inf is the same as x == x, i.e. isfinite(x). */
5632 if (lang_hooks.decls.global_bindings_p () == 0
5633 && ! CONTAINS_PLACEHOLDER_P (arg0))
5635 arg0 = save_expr (arg0);
5636 return fold (build2 (EQ_EXPR, type, arg0, arg0));
5638 break;
5640 case EQ_EXPR:
5641 case GE_EXPR:
5642 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
5643 real_maxval (&max, neg, mode);
5644 return fold (build2 (neg ? LT_EXPR : GT_EXPR, type,
5645 arg0, build_real (TREE_TYPE (arg0), max)));
5647 case LT_EXPR:
5648 /* x < +Inf is always equal to x <= DBL_MAX. */
5649 real_maxval (&max, neg, mode);
5650 return fold (build2 (neg ? GE_EXPR : LE_EXPR, type,
5651 arg0, build_real (TREE_TYPE (arg0), max)));
5653 case NE_EXPR:
5654 /* x != +Inf is always equal to !(x > DBL_MAX). */
5655 real_maxval (&max, neg, mode);
5656 if (! HONOR_NANS (mode))
5657 return fold (build2 (neg ? GE_EXPR : LE_EXPR, type,
5658 arg0, build_real (TREE_TYPE (arg0), max)));
5660 /* The transformation below creates non-gimple code and thus is
5661 not appropriate if we are in gimple form. */
5662 if (in_gimple_form)
5663 return NULL_TREE;
5665 temp = fold (build2 (neg ? LT_EXPR : GT_EXPR, type,
5666 arg0, build_real (TREE_TYPE (arg0), max)));
5667 return fold (build1 (TRUTH_NOT_EXPR, type, temp));
5669 default:
5670 break;
5673 return NULL_TREE;
5676 /* Subroutine of fold() that optimizes comparisons of a division by
5677 a nonzero integer constant against an integer constant, i.e.
5678 X/C1 op C2.
5680 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
5681 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
5682 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
5684 The function returns the constant folded tree if a simplification
5685 can be made, and NULL_TREE otherwise. */
5687 static tree
5688 fold_div_compare (enum tree_code code, tree type, tree arg0, tree arg1)
5690 tree prod, tmp, hi, lo;
5691 tree arg00 = TREE_OPERAND (arg0, 0);
5692 tree arg01 = TREE_OPERAND (arg0, 1);
5693 unsigned HOST_WIDE_INT lpart;
5694 HOST_WIDE_INT hpart;
5695 int overflow;
5697 /* We have to do this the hard way to detect unsigned overflow.
5698 prod = int_const_binop (MULT_EXPR, arg01, arg1, 0); */
5699 overflow = mul_double (TREE_INT_CST_LOW (arg01),
5700 TREE_INT_CST_HIGH (arg01),
5701 TREE_INT_CST_LOW (arg1),
5702 TREE_INT_CST_HIGH (arg1), &lpart, &hpart);
5703 prod = build_int_2 (lpart, hpart);
5704 TREE_TYPE (prod) = TREE_TYPE (arg00);
5705 prod = force_fit_type (prod, -1, overflow, false);
5707 if (TYPE_UNSIGNED (TREE_TYPE (arg0)))
5709 tmp = int_const_binop (MINUS_EXPR, arg01, integer_one_node, 0);
5710 lo = prod;
5712 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0). */
5713 overflow = add_double (TREE_INT_CST_LOW (prod),
5714 TREE_INT_CST_HIGH (prod),
5715 TREE_INT_CST_LOW (tmp),
5716 TREE_INT_CST_HIGH (tmp),
5717 &lpart, &hpart);
5718 hi = build_int_2 (lpart, hpart);
5719 TREE_TYPE (hi) = TREE_TYPE (arg00);
5720 hi = force_fit_type (hi, -1, overflow | TREE_OVERFLOW (prod),
5721 TREE_CONSTANT_OVERFLOW (prod));
5723 else if (tree_int_cst_sgn (arg01) >= 0)
5725 tmp = int_const_binop (MINUS_EXPR, arg01, integer_one_node, 0);
5726 switch (tree_int_cst_sgn (arg1))
5728 case -1:
5729 lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
5730 hi = prod;
5731 break;
5733 case 0:
5734 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
5735 hi = tmp;
5736 break;
5738 case 1:
5739 hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
5740 lo = prod;
5741 break;
5743 default:
5744 abort ();
5747 else
5749 tmp = int_const_binop (PLUS_EXPR, arg01, integer_one_node, 0);
5750 switch (tree_int_cst_sgn (arg1))
5752 case -1:
5753 hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
5754 lo = prod;
5755 break;
5757 case 0:
5758 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
5759 lo = tmp;
5760 break;
5762 case 1:
5763 lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
5764 hi = prod;
5765 break;
5767 default:
5768 abort ();
5772 switch (code)
5774 case EQ_EXPR:
5775 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
5776 return omit_one_operand (type, integer_zero_node, arg00);
5777 if (TREE_OVERFLOW (hi))
5778 return fold (build2 (GE_EXPR, type, arg00, lo));
5779 if (TREE_OVERFLOW (lo))
5780 return fold (build2 (LE_EXPR, type, arg00, hi));
5781 return build_range_check (type, arg00, 1, lo, hi);
5783 case NE_EXPR:
5784 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
5785 return omit_one_operand (type, integer_one_node, arg00);
5786 if (TREE_OVERFLOW (hi))
5787 return fold (build2 (LT_EXPR, type, arg00, lo));
5788 if (TREE_OVERFLOW (lo))
5789 return fold (build2 (GT_EXPR, type, arg00, hi));
5790 return build_range_check (type, arg00, 0, lo, hi);
5792 case LT_EXPR:
5793 if (TREE_OVERFLOW (lo))
5794 return omit_one_operand (type, integer_zero_node, arg00);
5795 return fold (build2 (LT_EXPR, type, arg00, lo));
5797 case LE_EXPR:
5798 if (TREE_OVERFLOW (hi))
5799 return omit_one_operand (type, integer_one_node, arg00);
5800 return fold (build2 (LE_EXPR, type, arg00, hi));
5802 case GT_EXPR:
5803 if (TREE_OVERFLOW (hi))
5804 return omit_one_operand (type, integer_zero_node, arg00);
5805 return fold (build2 (GT_EXPR, type, arg00, hi));
5807 case GE_EXPR:
5808 if (TREE_OVERFLOW (lo))
5809 return omit_one_operand (type, integer_one_node, arg00);
5810 return fold (build2 (GE_EXPR, type, arg00, lo));
5812 default:
5813 break;
5816 return NULL_TREE;
5820 /* If CODE with arguments ARG0 and ARG1 represents a single bit
5821 equality/inequality test, then return a simplified form of
5822 the test using shifts and logical operations. Otherwise return
5823 NULL. TYPE is the desired result type. */
5825 tree
5826 fold_single_bit_test (enum tree_code code, tree arg0, tree arg1,
5827 tree result_type)
5829 /* If this is a TRUTH_NOT_EXPR, it may have a single bit test inside
5830 operand 0. */
5831 if (code == TRUTH_NOT_EXPR)
5833 code = TREE_CODE (arg0);
5834 if (code != NE_EXPR && code != EQ_EXPR)
5835 return NULL_TREE;
5837 /* Extract the arguments of the EQ/NE. */
5838 arg1 = TREE_OPERAND (arg0, 1);
5839 arg0 = TREE_OPERAND (arg0, 0);
5841 /* This requires us to invert the code. */
5842 code = (code == EQ_EXPR ? NE_EXPR : EQ_EXPR);
5845 /* If this is testing a single bit, we can optimize the test. */
5846 if ((code == NE_EXPR || code == EQ_EXPR)
5847 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
5848 && integer_pow2p (TREE_OPERAND (arg0, 1)))
5850 tree inner = TREE_OPERAND (arg0, 0);
5851 tree type = TREE_TYPE (arg0);
5852 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
5853 enum machine_mode operand_mode = TYPE_MODE (type);
5854 int ops_unsigned;
5855 tree signed_type, unsigned_type, intermediate_type;
5856 tree arg00;
5858 /* If we have (A & C) != 0 where C is the sign bit of A, convert
5859 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
5860 arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
5861 if (arg00 != NULL_TREE
5862 /* This is only a win if casting to a signed type is cheap,
5863 i.e. when arg00's type is not a partial mode. */
5864 && TYPE_PRECISION (TREE_TYPE (arg00))
5865 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
5867 tree stype = lang_hooks.types.signed_type (TREE_TYPE (arg00));
5868 return fold (build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
5869 result_type, fold_convert (stype, arg00),
5870 fold_convert (stype, integer_zero_node)));
5873 /* Otherwise we have (A & C) != 0 where C is a single bit,
5874 convert that into ((A >> C2) & 1). Where C2 = log2(C).
5875 Similarly for (A & C) == 0. */
5877 /* If INNER is a right shift of a constant and it plus BITNUM does
5878 not overflow, adjust BITNUM and INNER. */
5879 if (TREE_CODE (inner) == RSHIFT_EXPR
5880 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
5881 && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
5882 && bitnum < TYPE_PRECISION (type)
5883 && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
5884 bitnum - TYPE_PRECISION (type)))
5886 bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
5887 inner = TREE_OPERAND (inner, 0);
5890 /* If we are going to be able to omit the AND below, we must do our
5891 operations as unsigned. If we must use the AND, we have a choice.
5892 Normally unsigned is faster, but for some machines signed is. */
5893 #ifdef LOAD_EXTEND_OP
5894 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND ? 0 : 1);
5895 #else
5896 ops_unsigned = 1;
5897 #endif
5899 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
5900 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
5901 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
5902 inner = fold_convert (intermediate_type, inner);
5904 if (bitnum != 0)
5905 inner = build2 (RSHIFT_EXPR, intermediate_type,
5906 inner, size_int (bitnum));
5908 if (code == EQ_EXPR)
5909 inner = fold (build2 (BIT_XOR_EXPR, intermediate_type,
5910 inner, integer_one_node));
5912 /* Put the AND last so it can combine with more things. */
5913 inner = build2 (BIT_AND_EXPR, intermediate_type,
5914 inner, integer_one_node);
5916 /* Make sure to return the proper type. */
5917 inner = fold_convert (result_type, inner);
5919 return inner;
5921 return NULL_TREE;
5924 /* Check whether we are allowed to reorder operands arg0 and arg1,
5925 such that the evaluation of arg1 occurs before arg0. */
5927 static bool
5928 reorder_operands_p (tree arg0, tree arg1)
5930 if (! flag_evaluation_order)
5931 return true;
5932 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
5933 return true;
5934 return ! TREE_SIDE_EFFECTS (arg0)
5935 && ! TREE_SIDE_EFFECTS (arg1);
5938 /* Test whether it is preferable two swap two operands, ARG0 and
5939 ARG1, for example because ARG0 is an integer constant and ARG1
5940 isn't. If REORDER is true, only recommend swapping if we can
5941 evaluate the operands in reverse order. */
5943 bool
5944 tree_swap_operands_p (tree arg0, tree arg1, bool reorder)
5946 STRIP_SIGN_NOPS (arg0);
5947 STRIP_SIGN_NOPS (arg1);
5949 if (TREE_CODE (arg1) == INTEGER_CST)
5950 return 0;
5951 if (TREE_CODE (arg0) == INTEGER_CST)
5952 return 1;
5954 if (TREE_CODE (arg1) == REAL_CST)
5955 return 0;
5956 if (TREE_CODE (arg0) == REAL_CST)
5957 return 1;
5959 if (TREE_CODE (arg1) == COMPLEX_CST)
5960 return 0;
5961 if (TREE_CODE (arg0) == COMPLEX_CST)
5962 return 1;
5964 if (TREE_CONSTANT (arg1))
5965 return 0;
5966 if (TREE_CONSTANT (arg0))
5967 return 1;
5969 if (optimize_size)
5970 return 0;
5972 if (reorder && flag_evaluation_order
5973 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
5974 return 0;
5976 if (DECL_P (arg1))
5977 return 0;
5978 if (DECL_P (arg0))
5979 return 1;
5981 if (reorder && flag_evaluation_order
5982 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
5983 return 0;
5985 if (DECL_P (arg1))
5986 return 0;
5987 if (DECL_P (arg0))
5988 return 1;
5990 /* It is preferable to swap two SSA_NAME to ensure a canonical form
5991 for commutative and comparison operators. Ensuring a canonical
5992 form allows the optimizers to find additional redundancies without
5993 having to explicitly check for both orderings. */
5994 if (TREE_CODE (arg0) == SSA_NAME
5995 && TREE_CODE (arg1) == SSA_NAME
5996 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
5997 return 1;
5999 return 0;
6002 /* Perform constant folding and related simplification of EXPR.
6003 The related simplifications include x*1 => x, x*0 => 0, etc.,
6004 and application of the associative law.
6005 NOP_EXPR conversions may be removed freely (as long as we
6006 are careful not to change the type of the overall expression).
6007 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
6008 but we can constant-fold them if they have constant operands. */
6010 #ifdef ENABLE_FOLD_CHECKING
6011 # define fold(x) fold_1 (x)
6012 static tree fold_1 (tree);
6013 static
6014 #endif
6015 tree
6016 fold (tree expr)
6018 const tree t = expr;
6019 const tree type = TREE_TYPE (expr);
6020 tree t1 = NULL_TREE;
6021 tree tem;
6022 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
6023 enum tree_code code = TREE_CODE (t);
6024 int kind = TREE_CODE_CLASS (code);
6026 /* WINS will be nonzero when the switch is done
6027 if all operands are constant. */
6028 int wins = 1;
6030 /* Return right away if a constant. */
6031 if (kind == 'c')
6032 return t;
6034 if (code == NOP_EXPR || code == FLOAT_EXPR || code == CONVERT_EXPR)
6036 tree subop;
6038 /* Special case for conversion ops that can have fixed point args. */
6039 arg0 = TREE_OPERAND (t, 0);
6041 /* Don't use STRIP_NOPS, because signedness of argument type matters. */
6042 if (arg0 != 0)
6043 STRIP_SIGN_NOPS (arg0);
6045 if (arg0 != 0 && TREE_CODE (arg0) == COMPLEX_CST)
6046 subop = TREE_REALPART (arg0);
6047 else
6048 subop = arg0;
6050 if (subop != 0 && TREE_CODE (subop) != INTEGER_CST
6051 && TREE_CODE (subop) != REAL_CST)
6052 /* Note that TREE_CONSTANT isn't enough:
6053 static var addresses are constant but we can't
6054 do arithmetic on them. */
6055 wins = 0;
6057 else if (IS_EXPR_CODE_CLASS (kind))
6059 int len = first_rtl_op (code);
6060 int i;
6061 for (i = 0; i < len; i++)
6063 tree op = TREE_OPERAND (t, i);
6064 tree subop;
6066 if (op == 0)
6067 continue; /* Valid for CALL_EXPR, at least. */
6069 /* Strip any conversions that don't change the mode. This is
6070 safe for every expression, except for a comparison expression
6071 because its signedness is derived from its operands. So, in
6072 the latter case, only strip conversions that don't change the
6073 signedness.
6075 Note that this is done as an internal manipulation within the
6076 constant folder, in order to find the simplest representation
6077 of the arguments so that their form can be studied. In any
6078 cases, the appropriate type conversions should be put back in
6079 the tree that will get out of the constant folder. */
6080 if (kind == '<')
6081 STRIP_SIGN_NOPS (op);
6082 else
6083 STRIP_NOPS (op);
6085 if (TREE_CODE (op) == COMPLEX_CST)
6086 subop = TREE_REALPART (op);
6087 else
6088 subop = op;
6090 if (TREE_CODE (subop) != INTEGER_CST
6091 && TREE_CODE (subop) != REAL_CST)
6092 /* Note that TREE_CONSTANT isn't enough:
6093 static var addresses are constant but we can't
6094 do arithmetic on them. */
6095 wins = 0;
6097 if (i == 0)
6098 arg0 = op;
6099 else if (i == 1)
6100 arg1 = op;
6104 /* If this is a commutative operation, and ARG0 is a constant, move it
6105 to ARG1 to reduce the number of tests below. */
6106 if (commutative_tree_code (code)
6107 && tree_swap_operands_p (arg0, arg1, true))
6108 return fold (build2 (code, type, TREE_OPERAND (t, 1),
6109 TREE_OPERAND (t, 0)));
6111 /* Now WINS is set as described above,
6112 ARG0 is the first operand of EXPR,
6113 and ARG1 is the second operand (if it has more than one operand).
6115 First check for cases where an arithmetic operation is applied to a
6116 compound, conditional, or comparison operation. Push the arithmetic
6117 operation inside the compound or conditional to see if any folding
6118 can then be done. Convert comparison to conditional for this purpose.
6119 The also optimizes non-constant cases that used to be done in
6120 expand_expr.
6122 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
6123 one of the operands is a comparison and the other is a comparison, a
6124 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
6125 code below would make the expression more complex. Change it to a
6126 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
6127 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
6129 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
6130 || code == EQ_EXPR || code == NE_EXPR)
6131 && ((truth_value_p (TREE_CODE (arg0))
6132 && (truth_value_p (TREE_CODE (arg1))
6133 || (TREE_CODE (arg1) == BIT_AND_EXPR
6134 && integer_onep (TREE_OPERAND (arg1, 1)))))
6135 || (truth_value_p (TREE_CODE (arg1))
6136 && (truth_value_p (TREE_CODE (arg0))
6137 || (TREE_CODE (arg0) == BIT_AND_EXPR
6138 && integer_onep (TREE_OPERAND (arg0, 1)))))))
6140 tem = fold (build2 (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
6141 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
6142 : TRUTH_XOR_EXPR,
6143 type, fold_convert (boolean_type_node, arg0),
6144 fold_convert (boolean_type_node, arg1)));
6146 if (code == EQ_EXPR)
6147 tem = invert_truthvalue (tem);
6149 return tem;
6152 if (TREE_CODE_CLASS (code) == '1')
6154 if (TREE_CODE (arg0) == COMPOUND_EXPR)
6155 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
6156 fold (build1 (code, type, TREE_OPERAND (arg0, 1))));
6157 else if (TREE_CODE (arg0) == COND_EXPR)
6159 tree arg01 = TREE_OPERAND (arg0, 1);
6160 tree arg02 = TREE_OPERAND (arg0, 2);
6161 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
6162 arg01 = fold (build1 (code, type, arg01));
6163 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
6164 arg02 = fold (build1 (code, type, arg02));
6165 tem = fold (build3 (COND_EXPR, type, TREE_OPERAND (arg0, 0),
6166 arg01, arg02));
6168 /* If this was a conversion, and all we did was to move into
6169 inside the COND_EXPR, bring it back out. But leave it if
6170 it is a conversion from integer to integer and the
6171 result precision is no wider than a word since such a
6172 conversion is cheap and may be optimized away by combine,
6173 while it couldn't if it were outside the COND_EXPR. Then return
6174 so we don't get into an infinite recursion loop taking the
6175 conversion out and then back in. */
6177 if ((code == NOP_EXPR || code == CONVERT_EXPR
6178 || code == NON_LVALUE_EXPR)
6179 && TREE_CODE (tem) == COND_EXPR
6180 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
6181 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
6182 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
6183 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
6184 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
6185 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
6186 && ! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
6187 && (INTEGRAL_TYPE_P
6188 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
6189 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD))
6190 tem = build1 (code, type,
6191 build3 (COND_EXPR,
6192 TREE_TYPE (TREE_OPERAND
6193 (TREE_OPERAND (tem, 1), 0)),
6194 TREE_OPERAND (tem, 0),
6195 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
6196 TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
6197 return tem;
6199 else if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<')
6201 if (TREE_CODE (type) == BOOLEAN_TYPE)
6203 arg0 = copy_node (arg0);
6204 TREE_TYPE (arg0) = type;
6205 return arg0;
6207 else if (TREE_CODE (type) != INTEGER_TYPE)
6208 return fold (build3 (COND_EXPR, type, arg0,
6209 fold (build1 (code, type,
6210 integer_one_node)),
6211 fold (build1 (code, type,
6212 integer_zero_node))));
6215 else if (TREE_CODE_CLASS (code) == '<'
6216 && TREE_CODE (arg0) == COMPOUND_EXPR)
6217 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
6218 fold (build2 (code, type, TREE_OPERAND (arg0, 1), arg1)));
6219 else if (TREE_CODE_CLASS (code) == '<'
6220 && TREE_CODE (arg1) == COMPOUND_EXPR)
6221 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
6222 fold (build2 (code, type, arg0, TREE_OPERAND (arg1, 1))));
6223 else if (TREE_CODE_CLASS (code) == '2'
6224 || TREE_CODE_CLASS (code) == '<')
6226 if (TREE_CODE (arg0) == COMPOUND_EXPR)
6227 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
6228 fold (build2 (code, type, TREE_OPERAND (arg0, 1),
6229 arg1)));
6230 if (TREE_CODE (arg1) == COMPOUND_EXPR
6231 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
6232 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
6233 fold (build2 (code, type,
6234 arg0, TREE_OPERAND (arg1, 1))));
6236 if (TREE_CODE (arg0) == COND_EXPR
6237 || TREE_CODE_CLASS (TREE_CODE (arg0)) == '<')
6239 tem = fold_binary_op_with_conditional_arg (code, type, arg0, arg1,
6240 /*cond_first_p=*/1);
6241 if (tem != NULL_TREE)
6242 return tem;
6245 if (TREE_CODE (arg1) == COND_EXPR
6246 || TREE_CODE_CLASS (TREE_CODE (arg1)) == '<')
6248 tem = fold_binary_op_with_conditional_arg (code, type, arg1, arg0,
6249 /*cond_first_p=*/0);
6250 if (tem != NULL_TREE)
6251 return tem;
6255 switch (code)
6257 case CONST_DECL:
6258 return fold (DECL_INITIAL (t));
6260 case NOP_EXPR:
6261 case FLOAT_EXPR:
6262 case CONVERT_EXPR:
6263 case FIX_TRUNC_EXPR:
6264 case FIX_CEIL_EXPR:
6265 case FIX_FLOOR_EXPR:
6266 case FIX_ROUND_EXPR:
6267 if (TREE_TYPE (TREE_OPERAND (t, 0)) == type)
6268 return TREE_OPERAND (t, 0);
6270 /* Handle cases of two conversions in a row. */
6271 if (TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
6272 || TREE_CODE (TREE_OPERAND (t, 0)) == CONVERT_EXPR)
6274 tree inside_type = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
6275 tree inter_type = TREE_TYPE (TREE_OPERAND (t, 0));
6276 int inside_int = INTEGRAL_TYPE_P (inside_type);
6277 int inside_ptr = POINTER_TYPE_P (inside_type);
6278 int inside_float = FLOAT_TYPE_P (inside_type);
6279 unsigned int inside_prec = TYPE_PRECISION (inside_type);
6280 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
6281 int inter_int = INTEGRAL_TYPE_P (inter_type);
6282 int inter_ptr = POINTER_TYPE_P (inter_type);
6283 int inter_float = FLOAT_TYPE_P (inter_type);
6284 unsigned int inter_prec = TYPE_PRECISION (inter_type);
6285 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
6286 int final_int = INTEGRAL_TYPE_P (type);
6287 int final_ptr = POINTER_TYPE_P (type);
6288 int final_float = FLOAT_TYPE_P (type);
6289 unsigned int final_prec = TYPE_PRECISION (type);
6290 int final_unsignedp = TYPE_UNSIGNED (type);
6292 /* In addition to the cases of two conversions in a row
6293 handled below, if we are converting something to its own
6294 type via an object of identical or wider precision, neither
6295 conversion is needed. */
6296 if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
6297 && ((inter_int && final_int) || (inter_float && final_float))
6298 && inter_prec >= final_prec)
6299 return fold (build1 (code, type,
6300 TREE_OPERAND (TREE_OPERAND (t, 0), 0)));
6302 /* Likewise, if the intermediate and final types are either both
6303 float or both integer, we don't need the middle conversion if
6304 it is wider than the final type and doesn't change the signedness
6305 (for integers). Avoid this if the final type is a pointer
6306 since then we sometimes need the inner conversion. Likewise if
6307 the outer has a precision not equal to the size of its mode. */
6308 if ((((inter_int || inter_ptr) && (inside_int || inside_ptr))
6309 || (inter_float && inside_float))
6310 && inter_prec >= inside_prec
6311 && (inter_float || inter_unsignedp == inside_unsignedp)
6312 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
6313 && TYPE_MODE (type) == TYPE_MODE (inter_type))
6314 && ! final_ptr)
6315 return fold (build1 (code, type,
6316 TREE_OPERAND (TREE_OPERAND (t, 0), 0)));
6318 /* If we have a sign-extension of a zero-extended value, we can
6319 replace that by a single zero-extension. */
6320 if (inside_int && inter_int && final_int
6321 && inside_prec < inter_prec && inter_prec < final_prec
6322 && inside_unsignedp && !inter_unsignedp)
6323 return fold (build1 (code, type,
6324 TREE_OPERAND (TREE_OPERAND (t, 0), 0)));
6326 /* Two conversions in a row are not needed unless:
6327 - some conversion is floating-point (overstrict for now), or
6328 - the intermediate type is narrower than both initial and
6329 final, or
6330 - the intermediate type and innermost type differ in signedness,
6331 and the outermost type is wider than the intermediate, or
6332 - the initial type is a pointer type and the precisions of the
6333 intermediate and final types differ, or
6334 - the final type is a pointer type and the precisions of the
6335 initial and intermediate types differ. */
6336 if (! inside_float && ! inter_float && ! final_float
6337 && (inter_prec > inside_prec || inter_prec > final_prec)
6338 && ! (inside_int && inter_int
6339 && inter_unsignedp != inside_unsignedp
6340 && inter_prec < final_prec)
6341 && ((inter_unsignedp && inter_prec > inside_prec)
6342 == (final_unsignedp && final_prec > inter_prec))
6343 && ! (inside_ptr && inter_prec != final_prec)
6344 && ! (final_ptr && inside_prec != inter_prec)
6345 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
6346 && TYPE_MODE (type) == TYPE_MODE (inter_type))
6347 && ! final_ptr)
6348 return fold (build1 (code, type,
6349 TREE_OPERAND (TREE_OPERAND (t, 0), 0)));
6352 if (TREE_CODE (TREE_OPERAND (t, 0)) == MODIFY_EXPR
6353 && TREE_CONSTANT (TREE_OPERAND (TREE_OPERAND (t, 0), 1))
6354 /* Detect assigning a bitfield. */
6355 && !(TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0)) == COMPONENT_REF
6356 && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (TREE_OPERAND (t, 0), 0), 1))))
6358 /* Don't leave an assignment inside a conversion
6359 unless assigning a bitfield. */
6360 tree prev = TREE_OPERAND (t, 0);
6361 tem = copy_node (t);
6362 TREE_OPERAND (tem, 0) = TREE_OPERAND (prev, 1);
6363 /* First do the assignment, then return converted constant. */
6364 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), prev, fold (tem));
6365 TREE_NO_WARNING (tem) = 1;
6366 TREE_USED (tem) = 1;
6367 return tem;
6370 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
6371 constants (if x has signed type, the sign bit cannot be set
6372 in c). This folds extension into the BIT_AND_EXPR. */
6373 if (INTEGRAL_TYPE_P (type)
6374 && TREE_CODE (type) != BOOLEAN_TYPE
6375 && TREE_CODE (TREE_OPERAND (t, 0)) == BIT_AND_EXPR
6376 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 1)) == INTEGER_CST)
6378 tree and = TREE_OPERAND (t, 0);
6379 tree and0 = TREE_OPERAND (and, 0), and1 = TREE_OPERAND (and, 1);
6380 int change = 0;
6382 if (TYPE_UNSIGNED (TREE_TYPE (and))
6383 || (TYPE_PRECISION (type)
6384 <= TYPE_PRECISION (TREE_TYPE (and))))
6385 change = 1;
6386 else if (TYPE_PRECISION (TREE_TYPE (and1))
6387 <= HOST_BITS_PER_WIDE_INT
6388 && host_integerp (and1, 1))
6390 unsigned HOST_WIDE_INT cst;
6392 cst = tree_low_cst (and1, 1);
6393 cst &= (HOST_WIDE_INT) -1
6394 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
6395 change = (cst == 0);
6396 #ifdef LOAD_EXTEND_OP
6397 if (change
6398 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
6399 == ZERO_EXTEND))
6401 tree uns = lang_hooks.types.unsigned_type (TREE_TYPE (and0));
6402 and0 = fold_convert (uns, and0);
6403 and1 = fold_convert (uns, and1);
6405 #endif
6407 if (change)
6408 return fold (build2 (BIT_AND_EXPR, type,
6409 fold_convert (type, and0),
6410 fold_convert (type, and1)));
6413 /* Convert (T1)((T2)X op Y) into (T1)X op Y, for pointer types T1 and
6414 T2 being pointers to types of the same size. */
6415 if (POINTER_TYPE_P (TREE_TYPE (t))
6416 && TREE_CODE_CLASS (TREE_CODE (arg0)) == '2'
6417 && TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
6418 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0))))
6420 tree arg00 = TREE_OPERAND (arg0, 0);
6421 tree t0 = TREE_TYPE (t);
6422 tree t1 = TREE_TYPE (arg00);
6423 tree tt0 = TREE_TYPE (t0);
6424 tree tt1 = TREE_TYPE (t1);
6425 tree s0 = TYPE_SIZE (tt0);
6426 tree s1 = TYPE_SIZE (tt1);
6428 if (s0 && s1 && operand_equal_p (s0, s1, OEP_ONLY_CONST))
6429 return build2 (TREE_CODE (arg0), t0, fold_convert (t0, arg00),
6430 TREE_OPERAND (arg0, 1));
6433 tem = fold_convert_const (code, type, arg0);
6434 return tem ? tem : t;
6436 case VIEW_CONVERT_EXPR:
6437 if (TREE_CODE (TREE_OPERAND (t, 0)) == VIEW_CONVERT_EXPR)
6438 return build1 (VIEW_CONVERT_EXPR, type,
6439 TREE_OPERAND (TREE_OPERAND (t, 0), 0));
6440 return t;
6442 case COMPONENT_REF:
6443 if (TREE_CODE (arg0) == CONSTRUCTOR
6444 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
6446 tree m = purpose_member (arg1, CONSTRUCTOR_ELTS (arg0));
6447 if (m)
6448 return TREE_VALUE (m);
6450 return t;
6452 case RANGE_EXPR:
6453 if (TREE_CONSTANT (t) != wins)
6455 tem = copy_node (t);
6456 TREE_CONSTANT (tem) = wins;
6457 TREE_INVARIANT (tem) = wins;
6458 return tem;
6460 return t;
6462 case NEGATE_EXPR:
6463 if (negate_expr_p (arg0))
6464 return fold_convert (type, negate_expr (arg0));
6465 return t;
6467 case ABS_EXPR:
6468 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
6469 return fold_abs_const (arg0, type);
6470 else if (TREE_CODE (arg0) == NEGATE_EXPR)
6471 return fold (build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0)));
6472 /* Convert fabs((double)float) into (double)fabsf(float). */
6473 else if (TREE_CODE (arg0) == NOP_EXPR
6474 && TREE_CODE (type) == REAL_TYPE)
6476 tree targ0 = strip_float_extensions (arg0);
6477 if (targ0 != arg0)
6478 return fold_convert (type, fold (build1 (ABS_EXPR,
6479 TREE_TYPE (targ0),
6480 targ0)));
6482 else if (tree_expr_nonnegative_p (arg0))
6483 return arg0;
6484 return t;
6486 case CONJ_EXPR:
6487 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
6488 return fold_convert (type, arg0);
6489 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
6490 return build2 (COMPLEX_EXPR, type,
6491 TREE_OPERAND (arg0, 0),
6492 negate_expr (TREE_OPERAND (arg0, 1)));
6493 else if (TREE_CODE (arg0) == COMPLEX_CST)
6494 return build_complex (type, TREE_REALPART (arg0),
6495 negate_expr (TREE_IMAGPART (arg0)));
6496 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
6497 return fold (build2 (TREE_CODE (arg0), type,
6498 fold (build1 (CONJ_EXPR, type,
6499 TREE_OPERAND (arg0, 0))),
6500 fold (build1 (CONJ_EXPR, type,
6501 TREE_OPERAND (arg0, 1)))));
6502 else if (TREE_CODE (arg0) == CONJ_EXPR)
6503 return TREE_OPERAND (arg0, 0);
6504 return t;
6506 case BIT_NOT_EXPR:
6507 if (TREE_CODE (arg0) == INTEGER_CST)
6508 return fold_not_const (arg0, type);
6509 else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
6510 return TREE_OPERAND (arg0, 0);
6511 return t;
6513 case PLUS_EXPR:
6514 /* A + (-B) -> A - B */
6515 if (TREE_CODE (arg1) == NEGATE_EXPR)
6516 return fold (build2 (MINUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
6517 /* (-A) + B -> B - A */
6518 if (TREE_CODE (arg0) == NEGATE_EXPR
6519 && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
6520 return fold (build2 (MINUS_EXPR, type, arg1, TREE_OPERAND (arg0, 0)));
6521 if (! FLOAT_TYPE_P (type))
6523 if (integer_zerop (arg1))
6524 return non_lvalue (fold_convert (type, arg0));
6526 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
6527 with a constant, and the two constants have no bits in common,
6528 we should treat this as a BIT_IOR_EXPR since this may produce more
6529 simplifications. */
6530 if (TREE_CODE (arg0) == BIT_AND_EXPR
6531 && TREE_CODE (arg1) == BIT_AND_EXPR
6532 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
6533 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
6534 && integer_zerop (const_binop (BIT_AND_EXPR,
6535 TREE_OPERAND (arg0, 1),
6536 TREE_OPERAND (arg1, 1), 0)))
6538 code = BIT_IOR_EXPR;
6539 goto bit_ior;
6542 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
6543 (plus (plus (mult) (mult)) (foo)) so that we can
6544 take advantage of the factoring cases below. */
6545 if ((TREE_CODE (arg0) == PLUS_EXPR
6546 && TREE_CODE (arg1) == MULT_EXPR)
6547 || (TREE_CODE (arg1) == PLUS_EXPR
6548 && TREE_CODE (arg0) == MULT_EXPR))
6550 tree parg0, parg1, parg, marg;
6552 if (TREE_CODE (arg0) == PLUS_EXPR)
6553 parg = arg0, marg = arg1;
6554 else
6555 parg = arg1, marg = arg0;
6556 parg0 = TREE_OPERAND (parg, 0);
6557 parg1 = TREE_OPERAND (parg, 1);
6558 STRIP_NOPS (parg0);
6559 STRIP_NOPS (parg1);
6561 if (TREE_CODE (parg0) == MULT_EXPR
6562 && TREE_CODE (parg1) != MULT_EXPR)
6563 return fold (build2 (PLUS_EXPR, type,
6564 fold (build2 (PLUS_EXPR, type,
6565 fold_convert (type, parg0),
6566 fold_convert (type, marg))),
6567 fold_convert (type, parg1)));
6568 if (TREE_CODE (parg0) != MULT_EXPR
6569 && TREE_CODE (parg1) == MULT_EXPR)
6570 return fold (build2 (PLUS_EXPR, type,
6571 fold (build2 (PLUS_EXPR, type,
6572 fold_convert (type, parg1),
6573 fold_convert (type, marg))),
6574 fold_convert (type, parg0)));
6577 if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR)
6579 tree arg00, arg01, arg10, arg11;
6580 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
6582 /* (A * C) + (B * C) -> (A+B) * C.
6583 We are most concerned about the case where C is a constant,
6584 but other combinations show up during loop reduction. Since
6585 it is not difficult, try all four possibilities. */
6587 arg00 = TREE_OPERAND (arg0, 0);
6588 arg01 = TREE_OPERAND (arg0, 1);
6589 arg10 = TREE_OPERAND (arg1, 0);
6590 arg11 = TREE_OPERAND (arg1, 1);
6591 same = NULL_TREE;
6593 if (operand_equal_p (arg01, arg11, 0))
6594 same = arg01, alt0 = arg00, alt1 = arg10;
6595 else if (operand_equal_p (arg00, arg10, 0))
6596 same = arg00, alt0 = arg01, alt1 = arg11;
6597 else if (operand_equal_p (arg00, arg11, 0))
6598 same = arg00, alt0 = arg01, alt1 = arg10;
6599 else if (operand_equal_p (arg01, arg10, 0))
6600 same = arg01, alt0 = arg00, alt1 = arg11;
6602 /* No identical multiplicands; see if we can find a common
6603 power-of-two factor in non-power-of-two multiplies. This
6604 can help in multi-dimensional array access. */
6605 else if (TREE_CODE (arg01) == INTEGER_CST
6606 && TREE_CODE (arg11) == INTEGER_CST
6607 && TREE_INT_CST_HIGH (arg01) == 0
6608 && TREE_INT_CST_HIGH (arg11) == 0)
6610 HOST_WIDE_INT int01, int11, tmp;
6611 int01 = TREE_INT_CST_LOW (arg01);
6612 int11 = TREE_INT_CST_LOW (arg11);
6614 /* Move min of absolute values to int11. */
6615 if ((int01 >= 0 ? int01 : -int01)
6616 < (int11 >= 0 ? int11 : -int11))
6618 tmp = int01, int01 = int11, int11 = tmp;
6619 alt0 = arg00, arg00 = arg10, arg10 = alt0;
6620 alt0 = arg01, arg01 = arg11, arg11 = alt0;
6623 if (exact_log2 (int11) > 0 && int01 % int11 == 0)
6625 alt0 = fold (build2 (MULT_EXPR, type, arg00,
6626 build_int_2 (int01 / int11, 0)));
6627 alt1 = arg10;
6628 same = arg11;
6632 if (same)
6633 return fold (build2 (MULT_EXPR, type,
6634 fold (build2 (PLUS_EXPR, type,
6635 alt0, alt1)),
6636 same));
6639 else
6641 /* See if ARG1 is zero and X + ARG1 reduces to X. */
6642 if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
6643 return non_lvalue (fold_convert (type, arg0));
6645 /* Likewise if the operands are reversed. */
6646 if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
6647 return non_lvalue (fold_convert (type, arg1));
6649 /* Convert X + -C into X - C. */
6650 if (TREE_CODE (arg1) == REAL_CST
6651 && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
6653 tem = fold_negate_const (arg1, type);
6654 if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
6655 return fold (build2 (MINUS_EXPR, type,
6656 fold_convert (type, arg0),
6657 fold_convert (type, tem)));
6660 /* Convert x+x into x*2.0. */
6661 if (operand_equal_p (arg0, arg1, 0)
6662 && SCALAR_FLOAT_TYPE_P (type))
6663 return fold (build2 (MULT_EXPR, type, arg0,
6664 build_real (type, dconst2)));
6666 /* Convert x*c+x into x*(c+1). */
6667 if (flag_unsafe_math_optimizations
6668 && TREE_CODE (arg0) == MULT_EXPR
6669 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
6670 && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg0, 1))
6671 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
6673 REAL_VALUE_TYPE c;
6675 c = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
6676 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
6677 return fold (build2 (MULT_EXPR, type, arg1,
6678 build_real (type, c)));
6681 /* Convert x+x*c into x*(c+1). */
6682 if (flag_unsafe_math_optimizations
6683 && TREE_CODE (arg1) == MULT_EXPR
6684 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST
6685 && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg1, 1))
6686 && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
6688 REAL_VALUE_TYPE c;
6690 c = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
6691 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
6692 return fold (build2 (MULT_EXPR, type, arg0,
6693 build_real (type, c)));
6696 /* Convert x*c1+x*c2 into x*(c1+c2). */
6697 if (flag_unsafe_math_optimizations
6698 && TREE_CODE (arg0) == MULT_EXPR
6699 && TREE_CODE (arg1) == MULT_EXPR
6700 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
6701 && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg0, 1))
6702 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST
6703 && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg1, 1))
6704 && operand_equal_p (TREE_OPERAND (arg0, 0),
6705 TREE_OPERAND (arg1, 0), 0))
6707 REAL_VALUE_TYPE c1, c2;
6709 c1 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
6710 c2 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
6711 real_arithmetic (&c1, PLUS_EXPR, &c1, &c2);
6712 return fold (build2 (MULT_EXPR, type,
6713 TREE_OPERAND (arg0, 0),
6714 build_real (type, c1)));
6716 /* Convert a + (b*c + d*e) into (a + b*c) + d*e */
6717 if (flag_unsafe_math_optimizations
6718 && TREE_CODE (arg1) == PLUS_EXPR
6719 && TREE_CODE (arg0) != MULT_EXPR)
6721 tree tree10 = TREE_OPERAND (arg1, 0);
6722 tree tree11 = TREE_OPERAND (arg1, 1);
6723 if (TREE_CODE (tree11) == MULT_EXPR
6724 && TREE_CODE (tree10) == MULT_EXPR)
6726 tree tree0;
6727 tree0 = fold (build2 (PLUS_EXPR, type, arg0, tree10));
6728 return fold (build2 (PLUS_EXPR, type, tree0, tree11));
6731 /* Convert (b*c + d*e) + a into b*c + (d*e +a) */
6732 if (flag_unsafe_math_optimizations
6733 && TREE_CODE (arg0) == PLUS_EXPR
6734 && TREE_CODE (arg1) != MULT_EXPR)
6736 tree tree00 = TREE_OPERAND (arg0, 0);
6737 tree tree01 = TREE_OPERAND (arg0, 1);
6738 if (TREE_CODE (tree01) == MULT_EXPR
6739 && TREE_CODE (tree00) == MULT_EXPR)
6741 tree tree0;
6742 tree0 = fold (build2 (PLUS_EXPR, type, tree01, arg1));
6743 return fold (build2 (PLUS_EXPR, type, tree00, tree0));
6748 bit_rotate:
6749 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
6750 is a rotate of A by C1 bits. */
6751 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
6752 is a rotate of A by B bits. */
6754 enum tree_code code0, code1;
6755 code0 = TREE_CODE (arg0);
6756 code1 = TREE_CODE (arg1);
6757 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
6758 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
6759 && operand_equal_p (TREE_OPERAND (arg0, 0),
6760 TREE_OPERAND (arg1, 0), 0)
6761 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
6763 tree tree01, tree11;
6764 enum tree_code code01, code11;
6766 tree01 = TREE_OPERAND (arg0, 1);
6767 tree11 = TREE_OPERAND (arg1, 1);
6768 STRIP_NOPS (tree01);
6769 STRIP_NOPS (tree11);
6770 code01 = TREE_CODE (tree01);
6771 code11 = TREE_CODE (tree11);
6772 if (code01 == INTEGER_CST
6773 && code11 == INTEGER_CST
6774 && TREE_INT_CST_HIGH (tree01) == 0
6775 && TREE_INT_CST_HIGH (tree11) == 0
6776 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
6777 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
6778 return build2 (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
6779 code0 == LSHIFT_EXPR ? tree01 : tree11);
6780 else if (code11 == MINUS_EXPR)
6782 tree tree110, tree111;
6783 tree110 = TREE_OPERAND (tree11, 0);
6784 tree111 = TREE_OPERAND (tree11, 1);
6785 STRIP_NOPS (tree110);
6786 STRIP_NOPS (tree111);
6787 if (TREE_CODE (tree110) == INTEGER_CST
6788 && 0 == compare_tree_int (tree110,
6789 TYPE_PRECISION
6790 (TREE_TYPE (TREE_OPERAND
6791 (arg0, 0))))
6792 && operand_equal_p (tree01, tree111, 0))
6793 return build2 ((code0 == LSHIFT_EXPR
6794 ? LROTATE_EXPR
6795 : RROTATE_EXPR),
6796 type, TREE_OPERAND (arg0, 0), tree01);
6798 else if (code01 == MINUS_EXPR)
6800 tree tree010, tree011;
6801 tree010 = TREE_OPERAND (tree01, 0);
6802 tree011 = TREE_OPERAND (tree01, 1);
6803 STRIP_NOPS (tree010);
6804 STRIP_NOPS (tree011);
6805 if (TREE_CODE (tree010) == INTEGER_CST
6806 && 0 == compare_tree_int (tree010,
6807 TYPE_PRECISION
6808 (TREE_TYPE (TREE_OPERAND
6809 (arg0, 0))))
6810 && operand_equal_p (tree11, tree011, 0))
6811 return build2 ((code0 != LSHIFT_EXPR
6812 ? LROTATE_EXPR
6813 : RROTATE_EXPR),
6814 type, TREE_OPERAND (arg0, 0), tree11);
6819 associate:
6820 /* In most languages, can't associate operations on floats through
6821 parentheses. Rather than remember where the parentheses were, we
6822 don't associate floats at all, unless the user has specified
6823 -funsafe-math-optimizations. */
6825 if (! wins
6826 && (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations))
6828 tree var0, con0, lit0, minus_lit0;
6829 tree var1, con1, lit1, minus_lit1;
6831 /* Split both trees into variables, constants, and literals. Then
6832 associate each group together, the constants with literals,
6833 then the result with variables. This increases the chances of
6834 literals being recombined later and of generating relocatable
6835 expressions for the sum of a constant and literal. */
6836 var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
6837 var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
6838 code == MINUS_EXPR);
6840 /* Only do something if we found more than two objects. Otherwise,
6841 nothing has changed and we risk infinite recursion. */
6842 if (2 < ((var0 != 0) + (var1 != 0)
6843 + (con0 != 0) + (con1 != 0)
6844 + (lit0 != 0) + (lit1 != 0)
6845 + (minus_lit0 != 0) + (minus_lit1 != 0)))
6847 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
6848 if (code == MINUS_EXPR)
6849 code = PLUS_EXPR;
6851 var0 = associate_trees (var0, var1, code, type);
6852 con0 = associate_trees (con0, con1, code, type);
6853 lit0 = associate_trees (lit0, lit1, code, type);
6854 minus_lit0 = associate_trees (minus_lit0, minus_lit1, code, type);
6856 /* Preserve the MINUS_EXPR if the negative part of the literal is
6857 greater than the positive part. Otherwise, the multiplicative
6858 folding code (i.e extract_muldiv) may be fooled in case
6859 unsigned constants are subtracted, like in the following
6860 example: ((X*2 + 4) - 8U)/2. */
6861 if (minus_lit0 && lit0)
6863 if (TREE_CODE (lit0) == INTEGER_CST
6864 && TREE_CODE (minus_lit0) == INTEGER_CST
6865 && tree_int_cst_lt (lit0, minus_lit0))
6867 minus_lit0 = associate_trees (minus_lit0, lit0,
6868 MINUS_EXPR, type);
6869 lit0 = 0;
6871 else
6873 lit0 = associate_trees (lit0, minus_lit0,
6874 MINUS_EXPR, type);
6875 minus_lit0 = 0;
6878 if (minus_lit0)
6880 if (con0 == 0)
6881 return fold_convert (type,
6882 associate_trees (var0, minus_lit0,
6883 MINUS_EXPR, type));
6884 else
6886 con0 = associate_trees (con0, minus_lit0,
6887 MINUS_EXPR, type);
6888 return fold_convert (type,
6889 associate_trees (var0, con0,
6890 PLUS_EXPR, type));
6894 con0 = associate_trees (con0, lit0, code, type);
6895 return fold_convert (type, associate_trees (var0, con0,
6896 code, type));
6900 binary:
6901 if (wins)
6902 t1 = const_binop (code, arg0, arg1, 0);
6903 if (t1 != NULL_TREE)
6905 /* The return value should always have
6906 the same type as the original expression. */
6907 if (TREE_TYPE (t1) != type)
6908 t1 = fold_convert (type, t1);
6910 return t1;
6912 return t;
6914 case MINUS_EXPR:
6915 /* A - (-B) -> A + B */
6916 if (TREE_CODE (arg1) == NEGATE_EXPR)
6917 return fold (build2 (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
6918 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
6919 if (TREE_CODE (arg0) == NEGATE_EXPR
6920 && (FLOAT_TYPE_P (type)
6921 || (INTEGRAL_TYPE_P (type) && flag_wrapv && !flag_trapv))
6922 && negate_expr_p (arg1)
6923 && reorder_operands_p (arg0, arg1))
6924 return fold (build2 (MINUS_EXPR, type, negate_expr (arg1),
6925 TREE_OPERAND (arg0, 0)));
6927 if (! FLOAT_TYPE_P (type))
6929 if (! wins && integer_zerop (arg0))
6930 return negate_expr (fold_convert (type, arg1));
6931 if (integer_zerop (arg1))
6932 return non_lvalue (fold_convert (type, arg0));
6934 /* Fold A - (A & B) into ~B & A. */
6935 if (!TREE_SIDE_EFFECTS (arg0)
6936 && TREE_CODE (arg1) == BIT_AND_EXPR)
6938 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
6939 return fold (build2 (BIT_AND_EXPR, type,
6940 fold (build1 (BIT_NOT_EXPR, type,
6941 TREE_OPERAND (arg1, 0))),
6942 arg0));
6943 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
6944 return fold (build2 (BIT_AND_EXPR, type,
6945 fold (build1 (BIT_NOT_EXPR, type,
6946 TREE_OPERAND (arg1, 1))),
6947 arg0));
6950 /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
6951 any power of 2 minus 1. */
6952 if (TREE_CODE (arg0) == BIT_AND_EXPR
6953 && TREE_CODE (arg1) == BIT_AND_EXPR
6954 && operand_equal_p (TREE_OPERAND (arg0, 0),
6955 TREE_OPERAND (arg1, 0), 0))
6957 tree mask0 = TREE_OPERAND (arg0, 1);
6958 tree mask1 = TREE_OPERAND (arg1, 1);
6959 tree tem = fold (build1 (BIT_NOT_EXPR, type, mask0));
6961 if (operand_equal_p (tem, mask1, 0))
6963 tem = fold (build2 (BIT_XOR_EXPR, type,
6964 TREE_OPERAND (arg0, 0), mask1));
6965 return fold (build2 (MINUS_EXPR, type, tem, mask1));
6970 /* See if ARG1 is zero and X - ARG1 reduces to X. */
6971 else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
6972 return non_lvalue (fold_convert (type, arg0));
6974 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
6975 ARG0 is zero and X + ARG0 reduces to X, since that would mean
6976 (-ARG1 + ARG0) reduces to -ARG1. */
6977 else if (!wins && fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
6978 return negate_expr (fold_convert (type, arg1));
6980 /* Fold &x - &x. This can happen from &x.foo - &x.
6981 This is unsafe for certain floats even in non-IEEE formats.
6982 In IEEE, it is unsafe because it does wrong for NaNs.
6983 Also note that operand_equal_p is always false if an operand
6984 is volatile. */
6986 if ((! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
6987 && operand_equal_p (arg0, arg1, 0))
6988 return fold_convert (type, integer_zero_node);
6990 /* A - B -> A + (-B) if B is easily negatable. */
6991 if (!wins && negate_expr_p (arg1)
6992 && ((FLOAT_TYPE_P (type)
6993 /* Avoid this transformation if B is a positive REAL_CST. */
6994 && (TREE_CODE (arg1) != REAL_CST
6995 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
6996 || (INTEGRAL_TYPE_P (type) && flag_wrapv && !flag_trapv)))
6997 return fold (build2 (PLUS_EXPR, type, arg0, negate_expr (arg1)));
6999 if (TREE_CODE (arg0) == MULT_EXPR
7000 && TREE_CODE (arg1) == MULT_EXPR
7001 && (INTEGRAL_TYPE_P (type) || flag_unsafe_math_optimizations))
7003 /* (A * C) - (B * C) -> (A-B) * C. */
7004 if (operand_equal_p (TREE_OPERAND (arg0, 1),
7005 TREE_OPERAND (arg1, 1), 0))
7006 return fold (build2 (MULT_EXPR, type,
7007 fold (build2 (MINUS_EXPR, type,
7008 TREE_OPERAND (arg0, 0),
7009 TREE_OPERAND (arg1, 0))),
7010 TREE_OPERAND (arg0, 1)));
7011 /* (A * C1) - (A * C2) -> A * (C1-C2). */
7012 if (operand_equal_p (TREE_OPERAND (arg0, 0),
7013 TREE_OPERAND (arg1, 0), 0))
7014 return fold (build2 (MULT_EXPR, type,
7015 TREE_OPERAND (arg0, 0),
7016 fold (build2 (MINUS_EXPR, type,
7017 TREE_OPERAND (arg0, 1),
7018 TREE_OPERAND (arg1, 1)))));
7021 goto associate;
7023 case MULT_EXPR:
7024 /* (-A) * (-B) -> A * B */
7025 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
7026 return fold (build2 (MULT_EXPR, type,
7027 TREE_OPERAND (arg0, 0),
7028 negate_expr (arg1)));
7029 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
7030 return fold (build2 (MULT_EXPR, type,
7031 negate_expr (arg0),
7032 TREE_OPERAND (arg1, 0)));
7034 if (! FLOAT_TYPE_P (type))
7036 if (integer_zerop (arg1))
7037 return omit_one_operand (type, arg1, arg0);
7038 if (integer_onep (arg1))
7039 return non_lvalue (fold_convert (type, arg0));
7041 /* (a * (1 << b)) is (a << b) */
7042 if (TREE_CODE (arg1) == LSHIFT_EXPR
7043 && integer_onep (TREE_OPERAND (arg1, 0)))
7044 return fold (build2 (LSHIFT_EXPR, type, arg0,
7045 TREE_OPERAND (arg1, 1)));
7046 if (TREE_CODE (arg0) == LSHIFT_EXPR
7047 && integer_onep (TREE_OPERAND (arg0, 0)))
7048 return fold (build2 (LSHIFT_EXPR, type, arg1,
7049 TREE_OPERAND (arg0, 1)));
7051 if (TREE_CODE (arg1) == INTEGER_CST
7052 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0),
7053 fold_convert (type, arg1),
7054 code, NULL_TREE)))
7055 return fold_convert (type, tem);
7058 else
7060 /* Maybe fold x * 0 to 0. The expressions aren't the same
7061 when x is NaN, since x * 0 is also NaN. Nor are they the
7062 same in modes with signed zeros, since multiplying a
7063 negative value by 0 gives -0, not +0. */
7064 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
7065 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
7066 && real_zerop (arg1))
7067 return omit_one_operand (type, arg1, arg0);
7068 /* In IEEE floating point, x*1 is not equivalent to x for snans. */
7069 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
7070 && real_onep (arg1))
7071 return non_lvalue (fold_convert (type, arg0));
7073 /* Transform x * -1.0 into -x. */
7074 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
7075 && real_minus_onep (arg1))
7076 return fold_convert (type, negate_expr (arg0));
7078 /* Convert (C1/X)*C2 into (C1*C2)/X. */
7079 if (flag_unsafe_math_optimizations
7080 && TREE_CODE (arg0) == RDIV_EXPR
7081 && TREE_CODE (arg1) == REAL_CST
7082 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
7084 tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
7085 arg1, 0);
7086 if (tem)
7087 return fold (build2 (RDIV_EXPR, type, tem,
7088 TREE_OPERAND (arg0, 1)));
7091 if (flag_unsafe_math_optimizations)
7093 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
7094 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
7096 /* Optimizations of root(...)*root(...). */
7097 if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
7099 tree rootfn, arg, arglist;
7100 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7101 tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
7103 /* Optimize sqrt(x)*sqrt(x) as x. */
7104 if (BUILTIN_SQRT_P (fcode0)
7105 && operand_equal_p (arg00, arg10, 0)
7106 && ! HONOR_SNANS (TYPE_MODE (type)))
7107 return arg00;
7109 /* Optimize root(x)*root(y) as root(x*y). */
7110 rootfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
7111 arg = fold (build2 (MULT_EXPR, type, arg00, arg10));
7112 arglist = build_tree_list (NULL_TREE, arg);
7113 return build_function_call_expr (rootfn, arglist);
7116 /* Optimize expN(x)*expN(y) as expN(x+y). */
7117 if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
7119 tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
7120 tree arg = build2 (PLUS_EXPR, type,
7121 TREE_VALUE (TREE_OPERAND (arg0, 1)),
7122 TREE_VALUE (TREE_OPERAND (arg1, 1)));
7123 tree arglist = build_tree_list (NULL_TREE, fold (arg));
7124 return build_function_call_expr (expfn, arglist);
7127 /* Optimizations of pow(...)*pow(...). */
7128 if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
7129 || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
7130 || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
7132 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7133 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0,
7134 1)));
7135 tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
7136 tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1,
7137 1)));
7139 /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y). */
7140 if (operand_equal_p (arg01, arg11, 0))
7142 tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
7143 tree arg = build2 (MULT_EXPR, type, arg00, arg10);
7144 tree arglist = tree_cons (NULL_TREE, fold (arg),
7145 build_tree_list (NULL_TREE,
7146 arg01));
7147 return build_function_call_expr (powfn, arglist);
7150 /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z). */
7151 if (operand_equal_p (arg00, arg10, 0))
7153 tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
7154 tree arg = fold (build2 (PLUS_EXPR, type, arg01, arg11));
7155 tree arglist = tree_cons (NULL_TREE, arg00,
7156 build_tree_list (NULL_TREE,
7157 arg));
7158 return build_function_call_expr (powfn, arglist);
7162 /* Optimize tan(x)*cos(x) as sin(x). */
7163 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
7164 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
7165 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
7166 || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
7167 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
7168 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
7169 && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
7170 TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
7172 tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
7174 if (sinfn != NULL_TREE)
7175 return build_function_call_expr (sinfn,
7176 TREE_OPERAND (arg0, 1));
7179 /* Optimize x*pow(x,c) as pow(x,c+1). */
7180 if (fcode1 == BUILT_IN_POW
7181 || fcode1 == BUILT_IN_POWF
7182 || fcode1 == BUILT_IN_POWL)
7184 tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
7185 tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1,
7186 1)));
7187 if (TREE_CODE (arg11) == REAL_CST
7188 && ! TREE_CONSTANT_OVERFLOW (arg11)
7189 && operand_equal_p (arg0, arg10, 0))
7191 tree powfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
7192 REAL_VALUE_TYPE c;
7193 tree arg, arglist;
7195 c = TREE_REAL_CST (arg11);
7196 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
7197 arg = build_real (type, c);
7198 arglist = build_tree_list (NULL_TREE, arg);
7199 arglist = tree_cons (NULL_TREE, arg0, arglist);
7200 return build_function_call_expr (powfn, arglist);
7204 /* Optimize pow(x,c)*x as pow(x,c+1). */
7205 if (fcode0 == BUILT_IN_POW
7206 || fcode0 == BUILT_IN_POWF
7207 || fcode0 == BUILT_IN_POWL)
7209 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7210 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0,
7211 1)));
7212 if (TREE_CODE (arg01) == REAL_CST
7213 && ! TREE_CONSTANT_OVERFLOW (arg01)
7214 && operand_equal_p (arg1, arg00, 0))
7216 tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
7217 REAL_VALUE_TYPE c;
7218 tree arg, arglist;
7220 c = TREE_REAL_CST (arg01);
7221 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
7222 arg = build_real (type, c);
7223 arglist = build_tree_list (NULL_TREE, arg);
7224 arglist = tree_cons (NULL_TREE, arg1, arglist);
7225 return build_function_call_expr (powfn, arglist);
7229 /* Optimize x*x as pow(x,2.0), which is expanded as x*x. */
7230 if (! optimize_size
7231 && operand_equal_p (arg0, arg1, 0))
7233 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
7235 if (powfn)
7237 tree arg = build_real (type, dconst2);
7238 tree arglist = build_tree_list (NULL_TREE, arg);
7239 arglist = tree_cons (NULL_TREE, arg0, arglist);
7240 return build_function_call_expr (powfn, arglist);
7245 goto associate;
7247 case BIT_IOR_EXPR:
7248 bit_ior:
7249 if (integer_all_onesp (arg1))
7250 return omit_one_operand (type, arg1, arg0);
7251 if (integer_zerop (arg1))
7252 return non_lvalue (fold_convert (type, arg0));
7253 if (operand_equal_p (arg0, arg1, 0))
7254 return non_lvalue (fold_convert (type, arg0));
7256 /* ~X | X is -1. */
7257 if (TREE_CODE (arg0) == BIT_NOT_EXPR
7258 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
7260 t1 = build_int_2 (-1, -1);
7261 TREE_TYPE (t1) = type;
7262 t1 = force_fit_type (t1, 0, false, false);
7263 return omit_one_operand (type, t1, arg1);
7266 /* X | ~X is -1. */
7267 if (TREE_CODE (arg1) == BIT_NOT_EXPR
7268 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
7270 t1 = build_int_2 (-1, -1);
7271 TREE_TYPE (t1) = type;
7272 t1 = force_fit_type (t1, 0, false, false);
7273 return omit_one_operand (type, t1, arg0);
7276 t1 = distribute_bit_expr (code, type, arg0, arg1);
7277 if (t1 != NULL_TREE)
7278 return t1;
7280 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
7282 This results in more efficient code for machines without a NAND
7283 instruction. Combine will canonicalize to the first form
7284 which will allow use of NAND instructions provided by the
7285 backend if they exist. */
7286 if (TREE_CODE (arg0) == BIT_NOT_EXPR
7287 && TREE_CODE (arg1) == BIT_NOT_EXPR)
7289 return fold (build1 (BIT_NOT_EXPR, type,
7290 build2 (BIT_AND_EXPR, type,
7291 TREE_OPERAND (arg0, 0),
7292 TREE_OPERAND (arg1, 0))));
7295 /* See if this can be simplified into a rotate first. If that
7296 is unsuccessful continue in the association code. */
7297 goto bit_rotate;
7299 case BIT_XOR_EXPR:
7300 if (integer_zerop (arg1))
7301 return non_lvalue (fold_convert (type, arg0));
7302 if (integer_all_onesp (arg1))
7303 return fold (build1 (BIT_NOT_EXPR, type, arg0));
7304 if (operand_equal_p (arg0, arg1, 0))
7305 return omit_one_operand (type, integer_zero_node, arg0);
7307 /* ~X ^ X is -1. */
7308 if (TREE_CODE (arg0) == BIT_NOT_EXPR
7309 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
7311 t1 = build_int_2 (-1, -1);
7312 TREE_TYPE (t1) = type;
7313 t1 = force_fit_type (t1, 0, false, false);
7314 return omit_one_operand (type, t1, arg1);
7317 /* X ^ ~X is -1. */
7318 if (TREE_CODE (arg1) == BIT_NOT_EXPR
7319 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
7321 t1 = build_int_2 (-1, -1);
7322 TREE_TYPE (t1) = type;
7323 t1 = force_fit_type (t1, 0, false, false);
7324 return omit_one_operand (type, t1, arg0);
7327 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
7328 with a constant, and the two constants have no bits in common,
7329 we should treat this as a BIT_IOR_EXPR since this may produce more
7330 simplifications. */
7331 if (TREE_CODE (arg0) == BIT_AND_EXPR
7332 && TREE_CODE (arg1) == BIT_AND_EXPR
7333 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7334 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
7335 && integer_zerop (const_binop (BIT_AND_EXPR,
7336 TREE_OPERAND (arg0, 1),
7337 TREE_OPERAND (arg1, 1), 0)))
7339 code = BIT_IOR_EXPR;
7340 goto bit_ior;
7343 /* See if this can be simplified into a rotate first. If that
7344 is unsuccessful continue in the association code. */
7345 goto bit_rotate;
7347 case BIT_AND_EXPR:
7348 if (integer_all_onesp (arg1))
7349 return non_lvalue (fold_convert (type, arg0));
7350 if (integer_zerop (arg1))
7351 return omit_one_operand (type, arg1, arg0);
7352 if (operand_equal_p (arg0, arg1, 0))
7353 return non_lvalue (fold_convert (type, arg0));
7355 /* ~X & X is always zero. */
7356 if (TREE_CODE (arg0) == BIT_NOT_EXPR
7357 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
7358 return omit_one_operand (type, integer_zero_node, arg1);
7360 /* X & ~X is always zero. */
7361 if (TREE_CODE (arg1) == BIT_NOT_EXPR
7362 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
7363 return omit_one_operand (type, integer_zero_node, arg0);
7365 t1 = distribute_bit_expr (code, type, arg0, arg1);
7366 if (t1 != NULL_TREE)
7367 return t1;
7368 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
7369 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
7370 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
7372 unsigned int prec
7373 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
7375 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
7376 && (~TREE_INT_CST_LOW (arg1)
7377 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
7378 return fold_convert (type, TREE_OPERAND (arg0, 0));
7381 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
7383 This results in more efficient code for machines without a NOR
7384 instruction. Combine will canonicalize to the first form
7385 which will allow use of NOR instructions provided by the
7386 backend if they exist. */
7387 if (TREE_CODE (arg0) == BIT_NOT_EXPR
7388 && TREE_CODE (arg1) == BIT_NOT_EXPR)
7390 return fold (build1 (BIT_NOT_EXPR, type,
7391 build2 (BIT_IOR_EXPR, type,
7392 TREE_OPERAND (arg0, 0),
7393 TREE_OPERAND (arg1, 0))));
7396 goto associate;
7398 case RDIV_EXPR:
7399 /* Don't touch a floating-point divide by zero unless the mode
7400 of the constant can represent infinity. */
7401 if (TREE_CODE (arg1) == REAL_CST
7402 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
7403 && real_zerop (arg1))
7404 return t;
7406 /* (-A) / (-B) -> A / B */
7407 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
7408 return fold (build2 (RDIV_EXPR, type,
7409 TREE_OPERAND (arg0, 0),
7410 negate_expr (arg1)));
7411 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
7412 return fold (build2 (RDIV_EXPR, type,
7413 negate_expr (arg0),
7414 TREE_OPERAND (arg1, 0)));
7416 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
7417 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
7418 && real_onep (arg1))
7419 return non_lvalue (fold_convert (type, arg0));
7421 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
7422 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
7423 && real_minus_onep (arg1))
7424 return non_lvalue (fold_convert (type, negate_expr (arg0)));
7426 /* If ARG1 is a constant, we can convert this to a multiply by the
7427 reciprocal. This does not have the same rounding properties,
7428 so only do this if -funsafe-math-optimizations. We can actually
7429 always safely do it if ARG1 is a power of two, but it's hard to
7430 tell if it is or not in a portable manner. */
7431 if (TREE_CODE (arg1) == REAL_CST)
7433 if (flag_unsafe_math_optimizations
7434 && 0 != (tem = const_binop (code, build_real (type, dconst1),
7435 arg1, 0)))
7436 return fold (build2 (MULT_EXPR, type, arg0, tem));
7437 /* Find the reciprocal if optimizing and the result is exact. */
7438 if (optimize)
7440 REAL_VALUE_TYPE r;
7441 r = TREE_REAL_CST (arg1);
7442 if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
7444 tem = build_real (type, r);
7445 return fold (build2 (MULT_EXPR, type, arg0, tem));
7449 /* Convert A/B/C to A/(B*C). */
7450 if (flag_unsafe_math_optimizations
7451 && TREE_CODE (arg0) == RDIV_EXPR)
7452 return fold (build2 (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
7453 fold (build2 (MULT_EXPR, type,
7454 TREE_OPERAND (arg0, 1), arg1))));
7456 /* Convert A/(B/C) to (A/B)*C. */
7457 if (flag_unsafe_math_optimizations
7458 && TREE_CODE (arg1) == RDIV_EXPR)
7459 return fold (build2 (MULT_EXPR, type,
7460 fold (build2 (RDIV_EXPR, type, arg0,
7461 TREE_OPERAND (arg1, 0))),
7462 TREE_OPERAND (arg1, 1)));
7464 /* Convert C1/(X*C2) into (C1/C2)/X. */
7465 if (flag_unsafe_math_optimizations
7466 && TREE_CODE (arg1) == MULT_EXPR
7467 && TREE_CODE (arg0) == REAL_CST
7468 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
7470 tree tem = const_binop (RDIV_EXPR, arg0,
7471 TREE_OPERAND (arg1, 1), 0);
7472 if (tem)
7473 return fold (build2 (RDIV_EXPR, type, tem,
7474 TREE_OPERAND (arg1, 0)));
7477 if (flag_unsafe_math_optimizations)
7479 enum built_in_function fcode = builtin_mathfn_code (arg1);
7480 /* Optimize x/expN(y) into x*expN(-y). */
7481 if (BUILTIN_EXPONENT_P (fcode))
7483 tree expfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
7484 tree arg = negate_expr (TREE_VALUE (TREE_OPERAND (arg1, 1)));
7485 tree arglist = build_tree_list (NULL_TREE,
7486 fold_convert (type, arg));
7487 arg1 = build_function_call_expr (expfn, arglist);
7488 return fold (build2 (MULT_EXPR, type, arg0, arg1));
7491 /* Optimize x/pow(y,z) into x*pow(y,-z). */
7492 if (fcode == BUILT_IN_POW
7493 || fcode == BUILT_IN_POWF
7494 || fcode == BUILT_IN_POWL)
7496 tree powfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
7497 tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
7498 tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1, 1)));
7499 tree neg11 = fold_convert (type, negate_expr (arg11));
7500 tree arglist = tree_cons(NULL_TREE, arg10,
7501 build_tree_list (NULL_TREE, neg11));
7502 arg1 = build_function_call_expr (powfn, arglist);
7503 return fold (build2 (MULT_EXPR, type, arg0, arg1));
7507 if (flag_unsafe_math_optimizations)
7509 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
7510 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
7512 /* Optimize sin(x)/cos(x) as tan(x). */
7513 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
7514 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
7515 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
7516 && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
7517 TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
7519 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
7521 if (tanfn != NULL_TREE)
7522 return build_function_call_expr (tanfn,
7523 TREE_OPERAND (arg0, 1));
7526 /* Optimize cos(x)/sin(x) as 1.0/tan(x). */
7527 if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
7528 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
7529 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
7530 && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
7531 TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
7533 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
7535 if (tanfn != NULL_TREE)
7537 tree tmp = TREE_OPERAND (arg0, 1);
7538 tmp = build_function_call_expr (tanfn, tmp);
7539 return fold (build2 (RDIV_EXPR, type,
7540 build_real (type, dconst1), tmp));
7544 /* Optimize pow(x,c)/x as pow(x,c-1). */
7545 if (fcode0 == BUILT_IN_POW
7546 || fcode0 == BUILT_IN_POWF
7547 || fcode0 == BUILT_IN_POWL)
7549 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7550 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0, 1)));
7551 if (TREE_CODE (arg01) == REAL_CST
7552 && ! TREE_CONSTANT_OVERFLOW (arg01)
7553 && operand_equal_p (arg1, arg00, 0))
7555 tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
7556 REAL_VALUE_TYPE c;
7557 tree arg, arglist;
7559 c = TREE_REAL_CST (arg01);
7560 real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
7561 arg = build_real (type, c);
7562 arglist = build_tree_list (NULL_TREE, arg);
7563 arglist = tree_cons (NULL_TREE, arg1, arglist);
7564 return build_function_call_expr (powfn, arglist);
7568 goto binary;
7570 case TRUNC_DIV_EXPR:
7571 case ROUND_DIV_EXPR:
7572 case FLOOR_DIV_EXPR:
7573 case CEIL_DIV_EXPR:
7574 case EXACT_DIV_EXPR:
7575 if (integer_onep (arg1))
7576 return non_lvalue (fold_convert (type, arg0));
7577 if (integer_zerop (arg1))
7578 return t;
7579 /* X / -1 is -X. */
7580 if (!TYPE_UNSIGNED (type)
7581 && TREE_CODE (arg1) == INTEGER_CST
7582 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
7583 && TREE_INT_CST_HIGH (arg1) == -1)
7584 return fold_convert (type, negate_expr (arg0));
7586 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
7587 operation, EXACT_DIV_EXPR.
7589 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
7590 At one time others generated faster code, it's not clear if they do
7591 after the last round to changes to the DIV code in expmed.c. */
7592 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
7593 && multiple_of_p (type, arg0, arg1))
7594 return fold (build2 (EXACT_DIV_EXPR, type, arg0, arg1));
7596 if (TREE_CODE (arg1) == INTEGER_CST
7597 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
7598 code, NULL_TREE)))
7599 return fold_convert (type, tem);
7601 goto binary;
7603 case CEIL_MOD_EXPR:
7604 case FLOOR_MOD_EXPR:
7605 case ROUND_MOD_EXPR:
7606 case TRUNC_MOD_EXPR:
7607 if (integer_onep (arg1))
7608 return omit_one_operand (type, integer_zero_node, arg0);
7609 if (integer_zerop (arg1))
7610 return t;
7612 /* X % -1 is zero. */
7613 if (!TYPE_UNSIGNED (type)
7614 && TREE_CODE (arg1) == INTEGER_CST
7615 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
7616 && TREE_INT_CST_HIGH (arg1) == -1)
7617 return omit_one_operand (type, integer_zero_node, arg0);
7619 /* Optimize unsigned TRUNC_MOD_EXPR by a power of two into a
7620 BIT_AND_EXPR, i.e. "X % C" into "X & C2". */
7621 if (code == TRUNC_MOD_EXPR
7622 && TYPE_UNSIGNED (type)
7623 && integer_pow2p (arg1))
7625 unsigned HOST_WIDE_INT high, low;
7626 tree mask;
7627 int l;
7629 l = tree_log2 (arg1);
7630 if (l >= HOST_BITS_PER_WIDE_INT)
7632 high = ((unsigned HOST_WIDE_INT) 1
7633 << (l - HOST_BITS_PER_WIDE_INT)) - 1;
7634 low = -1;
7636 else
7638 high = 0;
7639 low = ((unsigned HOST_WIDE_INT) 1 << l) - 1;
7642 mask = build_int_2 (low, high);
7643 TREE_TYPE (mask) = type;
7644 return fold (build2 (BIT_AND_EXPR, type,
7645 fold_convert (type, arg0), mask));
7648 /* X % -C is the same as X % C. */
7649 if (code == TRUNC_MOD_EXPR
7650 && !TYPE_UNSIGNED (type)
7651 && TREE_CODE (arg1) == INTEGER_CST
7652 && TREE_INT_CST_HIGH (arg1) < 0
7653 && !flag_trapv
7654 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
7655 && !sign_bit_p (arg1, arg1))
7656 return fold (build2 (code, type, fold_convert (type, arg0),
7657 fold_convert (type, negate_expr (arg1))));
7659 /* X % -Y is the same as X % Y. */
7660 if (code == TRUNC_MOD_EXPR
7661 && !TYPE_UNSIGNED (type)
7662 && TREE_CODE (arg1) == NEGATE_EXPR
7663 && !flag_trapv)
7664 return fold (build2 (code, type, fold_convert (type, arg0),
7665 fold_convert (type, TREE_OPERAND (arg1, 0))));
7667 if (TREE_CODE (arg1) == INTEGER_CST
7668 && 0 != (tem = extract_muldiv (TREE_OPERAND (t, 0), arg1,
7669 code, NULL_TREE)))
7670 return fold_convert (type, tem);
7672 goto binary;
7674 case LROTATE_EXPR:
7675 case RROTATE_EXPR:
7676 if (integer_all_onesp (arg0))
7677 return omit_one_operand (type, arg0, arg1);
7678 goto shift;
7680 case RSHIFT_EXPR:
7681 /* Optimize -1 >> x for arithmetic right shifts. */
7682 if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type))
7683 return omit_one_operand (type, arg0, arg1);
7684 /* ... fall through ... */
7686 case LSHIFT_EXPR:
7687 shift:
7688 if (integer_zerop (arg1))
7689 return non_lvalue (fold_convert (type, arg0));
7690 if (integer_zerop (arg0))
7691 return omit_one_operand (type, arg0, arg1);
7693 /* Since negative shift count is not well-defined,
7694 don't try to compute it in the compiler. */
7695 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
7696 return t;
7697 /* Rewrite an LROTATE_EXPR by a constant into an
7698 RROTATE_EXPR by a new constant. */
7699 if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
7701 tree tem = build_int_2 (GET_MODE_BITSIZE (TYPE_MODE (type)), 0);
7702 tem = fold_convert (TREE_TYPE (arg1), tem);
7703 tem = const_binop (MINUS_EXPR, tem, arg1, 0);
7704 return fold (build2 (RROTATE_EXPR, type, arg0, tem));
7707 /* If we have a rotate of a bit operation with the rotate count and
7708 the second operand of the bit operation both constant,
7709 permute the two operations. */
7710 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
7711 && (TREE_CODE (arg0) == BIT_AND_EXPR
7712 || TREE_CODE (arg0) == BIT_IOR_EXPR
7713 || TREE_CODE (arg0) == BIT_XOR_EXPR)
7714 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
7715 return fold (build2 (TREE_CODE (arg0), type,
7716 fold (build2 (code, type,
7717 TREE_OPERAND (arg0, 0), arg1)),
7718 fold (build2 (code, type,
7719 TREE_OPERAND (arg0, 1), arg1))));
7721 /* Two consecutive rotates adding up to the width of the mode can
7722 be ignored. */
7723 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
7724 && TREE_CODE (arg0) == RROTATE_EXPR
7725 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7726 && TREE_INT_CST_HIGH (arg1) == 0
7727 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
7728 && ((TREE_INT_CST_LOW (arg1)
7729 + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
7730 == (unsigned int) GET_MODE_BITSIZE (TYPE_MODE (type))))
7731 return TREE_OPERAND (arg0, 0);
7733 goto binary;
7735 case MIN_EXPR:
7736 if (operand_equal_p (arg0, arg1, 0))
7737 return omit_one_operand (type, arg0, arg1);
7738 if (INTEGRAL_TYPE_P (type)
7739 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
7740 return omit_one_operand (type, arg1, arg0);
7741 goto associate;
7743 case MAX_EXPR:
7744 if (operand_equal_p (arg0, arg1, 0))
7745 return omit_one_operand (type, arg0, arg1);
7746 if (INTEGRAL_TYPE_P (type)
7747 && TYPE_MAX_VALUE (type)
7748 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
7749 return omit_one_operand (type, arg1, arg0);
7750 goto associate;
7752 case TRUTH_NOT_EXPR:
7753 /* The argument to invert_truthvalue must have Boolean type. */
7754 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
7755 arg0 = fold_convert (boolean_type_node, arg0);
7757 /* Note that the operand of this must be an int
7758 and its values must be 0 or 1.
7759 ("true" is a fixed value perhaps depending on the language,
7760 but we don't handle values other than 1 correctly yet.) */
7761 tem = invert_truthvalue (arg0);
7762 /* Avoid infinite recursion. */
7763 if (TREE_CODE (tem) == TRUTH_NOT_EXPR)
7765 tem = fold_single_bit_test (code, arg0, arg1, type);
7766 if (tem)
7767 return tem;
7768 return t;
7770 return fold_convert (type, tem);
7772 case TRUTH_ANDIF_EXPR:
7773 /* Note that the operands of this must be ints
7774 and their values must be 0 or 1.
7775 ("true" is a fixed value perhaps depending on the language.) */
7776 /* If first arg is constant zero, return it. */
7777 if (integer_zerop (arg0))
7778 return fold_convert (type, arg0);
7779 case TRUTH_AND_EXPR:
7780 /* If either arg is constant true, drop it. */
7781 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
7782 return non_lvalue (fold_convert (type, arg1));
7783 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
7784 /* Preserve sequence points. */
7785 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
7786 return non_lvalue (fold_convert (type, arg0));
7787 /* If second arg is constant zero, result is zero, but first arg
7788 must be evaluated. */
7789 if (integer_zerop (arg1))
7790 return omit_one_operand (type, arg1, arg0);
7791 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
7792 case will be handled here. */
7793 if (integer_zerop (arg0))
7794 return omit_one_operand (type, arg0, arg1);
7796 /* !X && X is always false. */
7797 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
7798 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
7799 return omit_one_operand (type, integer_zero_node, arg1);
7800 /* X && !X is always false. */
7801 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
7802 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
7803 return omit_one_operand (type, integer_zero_node, arg0);
7805 truth_andor:
7806 /* We only do these simplifications if we are optimizing. */
7807 if (!optimize)
7808 return t;
7810 /* Check for things like (A || B) && (A || C). We can convert this
7811 to A || (B && C). Note that either operator can be any of the four
7812 truth and/or operations and the transformation will still be
7813 valid. Also note that we only care about order for the
7814 ANDIF and ORIF operators. If B contains side effects, this
7815 might change the truth-value of A. */
7816 if (TREE_CODE (arg0) == TREE_CODE (arg1)
7817 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
7818 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
7819 || TREE_CODE (arg0) == TRUTH_AND_EXPR
7820 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
7821 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
7823 tree a00 = TREE_OPERAND (arg0, 0);
7824 tree a01 = TREE_OPERAND (arg0, 1);
7825 tree a10 = TREE_OPERAND (arg1, 0);
7826 tree a11 = TREE_OPERAND (arg1, 1);
7827 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
7828 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
7829 && (code == TRUTH_AND_EXPR
7830 || code == TRUTH_OR_EXPR));
7832 if (operand_equal_p (a00, a10, 0))
7833 return fold (build2 (TREE_CODE (arg0), type, a00,
7834 fold (build2 (code, type, a01, a11))));
7835 else if (commutative && operand_equal_p (a00, a11, 0))
7836 return fold (build2 (TREE_CODE (arg0), type, a00,
7837 fold (build2 (code, type, a01, a10))));
7838 else if (commutative && operand_equal_p (a01, a10, 0))
7839 return fold (build2 (TREE_CODE (arg0), type, a01,
7840 fold (build2 (code, type, a00, a11))));
7842 /* This case if tricky because we must either have commutative
7843 operators or else A10 must not have side-effects. */
7845 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
7846 && operand_equal_p (a01, a11, 0))
7847 return fold (build2 (TREE_CODE (arg0), type,
7848 fold (build2 (code, type, a00, a10)),
7849 a01));
7852 /* See if we can build a range comparison. */
7853 if (0 != (tem = fold_range_test (t)))
7854 return tem;
7856 /* Check for the possibility of merging component references. If our
7857 lhs is another similar operation, try to merge its rhs with our
7858 rhs. Then try to merge our lhs and rhs. */
7859 if (TREE_CODE (arg0) == code
7860 && 0 != (tem = fold_truthop (code, type,
7861 TREE_OPERAND (arg0, 1), arg1)))
7862 return fold (build2 (code, type, TREE_OPERAND (arg0, 0), tem));
7864 if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
7865 return tem;
7867 return t;
7869 case TRUTH_ORIF_EXPR:
7870 /* Note that the operands of this must be ints
7871 and their values must be 0 or true.
7872 ("true" is a fixed value perhaps depending on the language.) */
7873 /* If first arg is constant true, return it. */
7874 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
7875 return fold_convert (type, arg0);
7876 case TRUTH_OR_EXPR:
7877 /* If either arg is constant zero, drop it. */
7878 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
7879 return non_lvalue (fold_convert (type, arg1));
7880 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
7881 /* Preserve sequence points. */
7882 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
7883 return non_lvalue (fold_convert (type, arg0));
7884 /* If second arg is constant true, result is true, but we must
7885 evaluate first arg. */
7886 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
7887 return omit_one_operand (type, arg1, arg0);
7888 /* Likewise for first arg, but note this only occurs here for
7889 TRUTH_OR_EXPR. */
7890 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
7891 return omit_one_operand (type, arg0, arg1);
7893 /* !X || X is always true. */
7894 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
7895 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
7896 return omit_one_operand (type, integer_one_node, arg1);
7897 /* X || !X is always true. */
7898 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
7899 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
7900 return omit_one_operand (type, integer_one_node, arg0);
7902 goto truth_andor;
7904 case TRUTH_XOR_EXPR:
7905 /* If the second arg is constant zero, drop it. */
7906 if (integer_zerop (arg1))
7907 return non_lvalue (fold_convert (type, arg0));
7908 /* If the second arg is constant true, this is a logical inversion. */
7909 if (integer_onep (arg1))
7910 return non_lvalue (fold_convert (type, invert_truthvalue (arg0)));
7911 /* Identical arguments cancel to zero. */
7912 if (operand_equal_p (arg0, arg1, 0))
7913 return omit_one_operand (type, integer_zero_node, arg0);
7915 /* !X ^ X is always true. */
7916 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
7917 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
7918 return omit_one_operand (type, integer_one_node, arg1);
7920 /* X ^ !X is always true. */
7921 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
7922 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
7923 return omit_one_operand (type, integer_one_node, arg0);
7925 return t;
7927 case EQ_EXPR:
7928 case NE_EXPR:
7929 case LT_EXPR:
7930 case GT_EXPR:
7931 case LE_EXPR:
7932 case GE_EXPR:
7933 /* If one arg is a real or integer constant, put it last. */
7934 if (tree_swap_operands_p (arg0, arg1, true))
7935 return fold (build2 (swap_tree_comparison (code), type, arg1, arg0));
7937 /* If this is an equality comparison of the address of a non-weak
7938 object against zero, then we know the result. */
7939 if ((code == EQ_EXPR || code == NE_EXPR)
7940 && TREE_CODE (arg0) == ADDR_EXPR
7941 && DECL_P (TREE_OPERAND (arg0, 0))
7942 && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
7943 && integer_zerop (arg1))
7944 return constant_boolean_node (code != EQ_EXPR, type);
7946 /* If this is an equality comparison of the address of two non-weak,
7947 unaliased symbols neither of which are extern (since we do not
7948 have access to attributes for externs), then we know the result. */
7949 if ((code == EQ_EXPR || code == NE_EXPR)
7950 && TREE_CODE (arg0) == ADDR_EXPR
7951 && DECL_P (TREE_OPERAND (arg0, 0))
7952 && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
7953 && ! lookup_attribute ("alias",
7954 DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
7955 && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
7956 && TREE_CODE (arg1) == ADDR_EXPR
7957 && DECL_P (TREE_OPERAND (arg1, 0))
7958 && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
7959 && ! lookup_attribute ("alias",
7960 DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
7961 && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
7962 return constant_boolean_node (operand_equal_p (arg0, arg1, 0)
7963 ? code == EQ_EXPR : code != EQ_EXPR,
7964 type);
7966 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
7968 tree targ0 = strip_float_extensions (arg0);
7969 tree targ1 = strip_float_extensions (arg1);
7970 tree newtype = TREE_TYPE (targ0);
7972 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
7973 newtype = TREE_TYPE (targ1);
7975 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
7976 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
7977 return fold (build2 (code, type, fold_convert (newtype, targ0),
7978 fold_convert (newtype, targ1)));
7980 /* (-a) CMP (-b) -> b CMP a */
7981 if (TREE_CODE (arg0) == NEGATE_EXPR
7982 && TREE_CODE (arg1) == NEGATE_EXPR)
7983 return fold (build2 (code, type, TREE_OPERAND (arg1, 0),
7984 TREE_OPERAND (arg0, 0)));
7986 if (TREE_CODE (arg1) == REAL_CST)
7988 REAL_VALUE_TYPE cst;
7989 cst = TREE_REAL_CST (arg1);
7991 /* (-a) CMP CST -> a swap(CMP) (-CST) */
7992 if (TREE_CODE (arg0) == NEGATE_EXPR)
7993 return
7994 fold (build2 (swap_tree_comparison (code), type,
7995 TREE_OPERAND (arg0, 0),
7996 build_real (TREE_TYPE (arg1),
7997 REAL_VALUE_NEGATE (cst))));
7999 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
8000 /* a CMP (-0) -> a CMP 0 */
8001 if (REAL_VALUE_MINUS_ZERO (cst))
8002 return fold (build2 (code, type, arg0,
8003 build_real (TREE_TYPE (arg1), dconst0)));
8005 /* x != NaN is always true, other ops are always false. */
8006 if (REAL_VALUE_ISNAN (cst)
8007 && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
8009 tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
8010 return omit_one_operand (type, tem, arg0);
8013 /* Fold comparisons against infinity. */
8014 if (REAL_VALUE_ISINF (cst))
8016 tem = fold_inf_compare (code, type, arg0, arg1);
8017 if (tem != NULL_TREE)
8018 return tem;
8022 /* If this is a comparison of a real constant with a PLUS_EXPR
8023 or a MINUS_EXPR of a real constant, we can convert it into a
8024 comparison with a revised real constant as long as no overflow
8025 occurs when unsafe_math_optimizations are enabled. */
8026 if (flag_unsafe_math_optimizations
8027 && TREE_CODE (arg1) == REAL_CST
8028 && (TREE_CODE (arg0) == PLUS_EXPR
8029 || TREE_CODE (arg0) == MINUS_EXPR)
8030 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
8031 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
8032 ? MINUS_EXPR : PLUS_EXPR,
8033 arg1, TREE_OPERAND (arg0, 1), 0))
8034 && ! TREE_CONSTANT_OVERFLOW (tem))
8035 return fold (build2 (code, type, TREE_OPERAND (arg0, 0), tem));
8037 /* Likewise, we can simplify a comparison of a real constant with
8038 a MINUS_EXPR whose first operand is also a real constant, i.e.
8039 (c1 - x) < c2 becomes x > c1-c2. */
8040 if (flag_unsafe_math_optimizations
8041 && TREE_CODE (arg1) == REAL_CST
8042 && TREE_CODE (arg0) == MINUS_EXPR
8043 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
8044 && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
8045 arg1, 0))
8046 && ! TREE_CONSTANT_OVERFLOW (tem))
8047 return fold (build2 (swap_tree_comparison (code), type,
8048 TREE_OPERAND (arg0, 1), tem));
8050 /* Fold comparisons against built-in math functions. */
8051 if (TREE_CODE (arg1) == REAL_CST
8052 && flag_unsafe_math_optimizations
8053 && ! flag_errno_math)
8055 enum built_in_function fcode = builtin_mathfn_code (arg0);
8057 if (fcode != END_BUILTINS)
8059 tem = fold_mathfn_compare (fcode, code, type, arg0, arg1);
8060 if (tem != NULL_TREE)
8061 return tem;
8066 /* Convert foo++ == CONST into ++foo == CONST + INCR. */
8067 if (TREE_CONSTANT (arg1)
8068 && (TREE_CODE (arg0) == POSTINCREMENT_EXPR
8069 || TREE_CODE (arg0) == POSTDECREMENT_EXPR)
8070 /* This optimization is invalid for ordered comparisons
8071 if CONST+INCR overflows or if foo+incr might overflow.
8072 This optimization is invalid for floating point due to rounding.
8073 For pointer types we assume overflow doesn't happen. */
8074 && (POINTER_TYPE_P (TREE_TYPE (arg0))
8075 || (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8076 && (code == EQ_EXPR || code == NE_EXPR))))
8078 tree varop, newconst;
8080 if (TREE_CODE (arg0) == POSTINCREMENT_EXPR)
8082 newconst = fold (build2 (PLUS_EXPR, TREE_TYPE (arg0),
8083 arg1, TREE_OPERAND (arg0, 1)));
8084 varop = build2 (PREINCREMENT_EXPR, TREE_TYPE (arg0),
8085 TREE_OPERAND (arg0, 0),
8086 TREE_OPERAND (arg0, 1));
8088 else
8090 newconst = fold (build2 (MINUS_EXPR, TREE_TYPE (arg0),
8091 arg1, TREE_OPERAND (arg0, 1)));
8092 varop = build2 (PREDECREMENT_EXPR, TREE_TYPE (arg0),
8093 TREE_OPERAND (arg0, 0),
8094 TREE_OPERAND (arg0, 1));
8098 /* If VAROP is a reference to a bitfield, we must mask
8099 the constant by the width of the field. */
8100 if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF
8101 && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (varop, 0), 1))
8102 && host_integerp (DECL_SIZE (TREE_OPERAND
8103 (TREE_OPERAND (varop, 0), 1)), 1))
8105 tree fielddecl = TREE_OPERAND (TREE_OPERAND (varop, 0), 1);
8106 HOST_WIDE_INT size = tree_low_cst (DECL_SIZE (fielddecl), 1);
8107 tree folded_compare, shift;
8109 /* First check whether the comparison would come out
8110 always the same. If we don't do that we would
8111 change the meaning with the masking. */
8112 folded_compare = fold (build2 (code, type,
8113 TREE_OPERAND (varop, 0), arg1));
8114 if (integer_zerop (folded_compare)
8115 || integer_onep (folded_compare))
8116 return omit_one_operand (type, folded_compare, varop);
8118 shift = build_int_2 (TYPE_PRECISION (TREE_TYPE (varop)) - size,
8120 shift = fold_convert (TREE_TYPE (varop), shift);
8121 newconst = fold (build2 (LSHIFT_EXPR, TREE_TYPE (varop),
8122 newconst, shift));
8123 newconst = fold (build2 (RSHIFT_EXPR, TREE_TYPE (varop),
8124 newconst, shift));
8127 return fold (build2 (code, type, varop, newconst));
8130 /* Change X >= C to X > (C - 1) and X < C to X <= (C - 1) if C > 0.
8131 This transformation affects the cases which are handled in later
8132 optimizations involving comparisons with non-negative constants. */
8133 if (TREE_CODE (arg1) == INTEGER_CST
8134 && TREE_CODE (arg0) != INTEGER_CST
8135 && tree_int_cst_sgn (arg1) > 0)
8137 switch (code)
8139 case GE_EXPR:
8140 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
8141 return fold (build2 (GT_EXPR, type, arg0, arg1));
8143 case LT_EXPR:
8144 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
8145 return fold (build2 (LE_EXPR, type, arg0, arg1));
8147 default:
8148 break;
8152 /* Comparisons with the highest or lowest possible integer of
8153 the specified size will have known values.
8155 This is quite similar to fold_relational_hi_lo; however, my
8156 attempts to share the code have been nothing but trouble.
8157 I give up for now. */
8159 int width = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg1)));
8161 if (TREE_CODE (arg1) == INTEGER_CST
8162 && ! TREE_CONSTANT_OVERFLOW (arg1)
8163 && width <= HOST_BITS_PER_WIDE_INT
8164 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
8165 || POINTER_TYPE_P (TREE_TYPE (arg1))))
8167 unsigned HOST_WIDE_INT signed_max;
8168 unsigned HOST_WIDE_INT max, min;
8170 signed_max = ((unsigned HOST_WIDE_INT) 1 << (width - 1)) - 1;
8172 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
8174 max = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
8175 min = 0;
8177 else
8179 max = signed_max;
8180 min = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
8183 if (TREE_INT_CST_HIGH (arg1) == 0
8184 && TREE_INT_CST_LOW (arg1) == max)
8185 switch (code)
8187 case GT_EXPR:
8188 return omit_one_operand (type, integer_zero_node, arg0);
8190 case GE_EXPR:
8191 return fold (build2 (EQ_EXPR, type, arg0, arg1));
8193 case LE_EXPR:
8194 return omit_one_operand (type, integer_one_node, arg0);
8196 case LT_EXPR:
8197 return fold (build2 (NE_EXPR, type, arg0, arg1));
8199 /* The GE_EXPR and LT_EXPR cases above are not normally
8200 reached because of previous transformations. */
8202 default:
8203 break;
8205 else if (TREE_INT_CST_HIGH (arg1) == 0
8206 && TREE_INT_CST_LOW (arg1) == max - 1)
8207 switch (code)
8209 case GT_EXPR:
8210 arg1 = const_binop (PLUS_EXPR, arg1, integer_one_node, 0);
8211 return fold (build2 (EQ_EXPR, type, arg0, arg1));
8212 case LE_EXPR:
8213 arg1 = const_binop (PLUS_EXPR, arg1, integer_one_node, 0);
8214 return fold (build2 (NE_EXPR, type, arg0, arg1));
8215 default:
8216 break;
8218 else if (TREE_INT_CST_HIGH (arg1) == (min ? -1 : 0)
8219 && TREE_INT_CST_LOW (arg1) == min)
8220 switch (code)
8222 case LT_EXPR:
8223 return omit_one_operand (type, integer_zero_node, arg0);
8225 case LE_EXPR:
8226 return fold (build2 (EQ_EXPR, type, arg0, arg1));
8228 case GE_EXPR:
8229 return omit_one_operand (type, integer_one_node, arg0);
8231 case GT_EXPR:
8232 return fold (build2 (NE_EXPR, type, arg0, arg1));
8234 default:
8235 break;
8237 else if (TREE_INT_CST_HIGH (arg1) == (min ? -1 : 0)
8238 && TREE_INT_CST_LOW (arg1) == min + 1)
8239 switch (code)
8241 case GE_EXPR:
8242 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
8243 return fold (build2 (NE_EXPR, type, arg0, arg1));
8244 case LT_EXPR:
8245 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
8246 return fold (build2 (EQ_EXPR, type, arg0, arg1));
8247 default:
8248 break;
8251 else if (!in_gimple_form
8252 && TREE_INT_CST_HIGH (arg1) == 0
8253 && TREE_INT_CST_LOW (arg1) == signed_max
8254 && TYPE_UNSIGNED (TREE_TYPE (arg1))
8255 /* signed_type does not work on pointer types. */
8256 && INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
8258 /* The following case also applies to X < signed_max+1
8259 and X >= signed_max+1 because previous transformations. */
8260 if (code == LE_EXPR || code == GT_EXPR)
8262 tree st0, st1;
8263 st0 = lang_hooks.types.signed_type (TREE_TYPE (arg0));
8264 st1 = lang_hooks.types.signed_type (TREE_TYPE (arg1));
8265 return fold
8266 (build2 (code == LE_EXPR ? GE_EXPR: LT_EXPR,
8267 type, fold_convert (st0, arg0),
8268 fold_convert (st1, integer_zero_node)));
8274 /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
8275 a MINUS_EXPR of a constant, we can convert it into a comparison with
8276 a revised constant as long as no overflow occurs. */
8277 if ((code == EQ_EXPR || code == NE_EXPR)
8278 && TREE_CODE (arg1) == INTEGER_CST
8279 && (TREE_CODE (arg0) == PLUS_EXPR
8280 || TREE_CODE (arg0) == MINUS_EXPR)
8281 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8282 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
8283 ? MINUS_EXPR : PLUS_EXPR,
8284 arg1, TREE_OPERAND (arg0, 1), 0))
8285 && ! TREE_CONSTANT_OVERFLOW (tem))
8286 return fold (build2 (code, type, TREE_OPERAND (arg0, 0), tem));
8288 /* Similarly for a NEGATE_EXPR. */
8289 else if ((code == EQ_EXPR || code == NE_EXPR)
8290 && TREE_CODE (arg0) == NEGATE_EXPR
8291 && TREE_CODE (arg1) == INTEGER_CST
8292 && 0 != (tem = negate_expr (arg1))
8293 && TREE_CODE (tem) == INTEGER_CST
8294 && ! TREE_CONSTANT_OVERFLOW (tem))
8295 return fold (build2 (code, type, TREE_OPERAND (arg0, 0), tem));
8297 /* If we have X - Y == 0, we can convert that to X == Y and similarly
8298 for !=. Don't do this for ordered comparisons due to overflow. */
8299 else if ((code == NE_EXPR || code == EQ_EXPR)
8300 && integer_zerop (arg1) && TREE_CODE (arg0) == MINUS_EXPR)
8301 return fold (build2 (code, type,
8302 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1)));
8304 /* If we are widening one operand of an integer comparison,
8305 see if the other operand is similarly being widened. Perhaps we
8306 can do the comparison in the narrower type. */
8307 else if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
8308 && TREE_CODE (arg0) == NOP_EXPR
8309 && (tem = get_unwidened (arg0, NULL_TREE)) != arg0
8310 && (code == EQ_EXPR || code == NE_EXPR
8311 || TYPE_UNSIGNED (TREE_TYPE (arg0))
8312 == TYPE_UNSIGNED (TREE_TYPE (tem)))
8313 && (t1 = get_unwidened (arg1, TREE_TYPE (tem))) != 0
8314 && (TREE_TYPE (t1) == TREE_TYPE (tem)
8315 || (TREE_CODE (t1) == INTEGER_CST
8316 && int_fits_type_p (t1, TREE_TYPE (tem)))))
8317 return fold (build2 (code, type, tem,
8318 fold_convert (TREE_TYPE (tem), t1)));
8320 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
8321 constant, we can simplify it. */
8322 else if (TREE_CODE (arg1) == INTEGER_CST
8323 && (TREE_CODE (arg0) == MIN_EXPR
8324 || TREE_CODE (arg0) == MAX_EXPR)
8325 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8326 return optimize_minmax_comparison (t);
8328 /* If we are comparing an ABS_EXPR with a constant, we can
8329 convert all the cases into explicit comparisons, but they may
8330 well not be faster than doing the ABS and one comparison.
8331 But ABS (X) <= C is a range comparison, which becomes a subtraction
8332 and a comparison, and is probably faster. */
8333 else if (code == LE_EXPR && TREE_CODE (arg1) == INTEGER_CST
8334 && TREE_CODE (arg0) == ABS_EXPR
8335 && ! TREE_SIDE_EFFECTS (arg0)
8336 && (0 != (tem = negate_expr (arg1)))
8337 && TREE_CODE (tem) == INTEGER_CST
8338 && ! TREE_CONSTANT_OVERFLOW (tem))
8339 return fold (build2 (TRUTH_ANDIF_EXPR, type,
8340 build2 (GE_EXPR, type,
8341 TREE_OPERAND (arg0, 0), tem),
8342 build2 (LE_EXPR, type,
8343 TREE_OPERAND (arg0, 0), arg1)));
8345 /* If this is an EQ or NE comparison with zero and ARG0 is
8346 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
8347 two operations, but the latter can be done in one less insn
8348 on machines that have only two-operand insns or on which a
8349 constant cannot be the first operand. */
8350 if (integer_zerop (arg1) && (code == EQ_EXPR || code == NE_EXPR)
8351 && TREE_CODE (arg0) == BIT_AND_EXPR)
8353 tree arg00 = TREE_OPERAND (arg0, 0);
8354 tree arg01 = TREE_OPERAND (arg0, 1);
8355 if (TREE_CODE (arg00) == LSHIFT_EXPR
8356 && integer_onep (TREE_OPERAND (arg00, 0)))
8357 return
8358 fold (build2 (code, type,
8359 build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
8360 build2 (RSHIFT_EXPR, TREE_TYPE (arg00),
8361 arg01, TREE_OPERAND (arg00, 1)),
8362 fold_convert (TREE_TYPE (arg0),
8363 integer_one_node)),
8364 arg1));
8365 else if (TREE_CODE (TREE_OPERAND (arg0, 1)) == LSHIFT_EXPR
8366 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 1), 0)))
8367 return
8368 fold (build2 (code, type,
8369 build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
8370 build2 (RSHIFT_EXPR, TREE_TYPE (arg01),
8371 arg00, TREE_OPERAND (arg01, 1)),
8372 fold_convert (TREE_TYPE (arg0),
8373 integer_one_node)),
8374 arg1));
8377 /* If this is an NE or EQ comparison of zero against the result of a
8378 signed MOD operation whose second operand is a power of 2, make
8379 the MOD operation unsigned since it is simpler and equivalent. */
8380 if ((code == NE_EXPR || code == EQ_EXPR)
8381 && integer_zerop (arg1)
8382 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
8383 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
8384 || TREE_CODE (arg0) == CEIL_MOD_EXPR
8385 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
8386 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
8387 && integer_pow2p (TREE_OPERAND (arg0, 1)))
8389 tree newtype = lang_hooks.types.unsigned_type (TREE_TYPE (arg0));
8390 tree newmod = fold (build2 (TREE_CODE (arg0), newtype,
8391 fold_convert (newtype,
8392 TREE_OPERAND (arg0, 0)),
8393 fold_convert (newtype,
8394 TREE_OPERAND (arg0, 1))));
8396 return fold (build2 (code, type, newmod,
8397 fold_convert (newtype, arg1)));
8400 /* If this is an NE comparison of zero with an AND of one, remove the
8401 comparison since the AND will give the correct value. */
8402 if (code == NE_EXPR && integer_zerop (arg1)
8403 && TREE_CODE (arg0) == BIT_AND_EXPR
8404 && integer_onep (TREE_OPERAND (arg0, 1)))
8405 return fold_convert (type, arg0);
8407 /* If we have (A & C) == C where C is a power of 2, convert this into
8408 (A & C) != 0. Similarly for NE_EXPR. */
8409 if ((code == EQ_EXPR || code == NE_EXPR)
8410 && TREE_CODE (arg0) == BIT_AND_EXPR
8411 && integer_pow2p (TREE_OPERAND (arg0, 1))
8412 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
8413 return fold (build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
8414 arg0, fold_convert (TREE_TYPE (arg0),
8415 integer_zero_node)));
8417 /* If we have (A & C) != 0 or (A & C) == 0 and C is a power of
8418 2, then fold the expression into shifts and logical operations. */
8419 tem = fold_single_bit_test (code, arg0, arg1, type);
8420 if (tem)
8421 return tem;
8423 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
8424 Similarly for NE_EXPR. */
8425 if ((code == EQ_EXPR || code == NE_EXPR)
8426 && TREE_CODE (arg0) == BIT_AND_EXPR
8427 && TREE_CODE (arg1) == INTEGER_CST
8428 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8430 tree dandnotc
8431 = fold (build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
8432 arg1, build1 (BIT_NOT_EXPR,
8433 TREE_TYPE (TREE_OPERAND (arg0, 1)),
8434 TREE_OPERAND (arg0, 1))));
8435 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
8436 if (integer_nonzerop (dandnotc))
8437 return omit_one_operand (type, rslt, arg0);
8440 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
8441 Similarly for NE_EXPR. */
8442 if ((code == EQ_EXPR || code == NE_EXPR)
8443 && TREE_CODE (arg0) == BIT_IOR_EXPR
8444 && TREE_CODE (arg1) == INTEGER_CST
8445 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8447 tree candnotd
8448 = fold (build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
8449 TREE_OPERAND (arg0, 1),
8450 build1 (BIT_NOT_EXPR, TREE_TYPE (arg1), arg1)));
8451 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
8452 if (integer_nonzerop (candnotd))
8453 return omit_one_operand (type, rslt, arg0);
8456 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
8457 and similarly for >= into !=. */
8458 if ((code == LT_EXPR || code == GE_EXPR)
8459 && TYPE_UNSIGNED (TREE_TYPE (arg0))
8460 && TREE_CODE (arg1) == LSHIFT_EXPR
8461 && integer_onep (TREE_OPERAND (arg1, 0)))
8462 return build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
8463 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
8464 TREE_OPERAND (arg1, 1)),
8465 fold_convert (TREE_TYPE (arg0), integer_zero_node));
8467 else if ((code == LT_EXPR || code == GE_EXPR)
8468 && TYPE_UNSIGNED (TREE_TYPE (arg0))
8469 && (TREE_CODE (arg1) == NOP_EXPR
8470 || TREE_CODE (arg1) == CONVERT_EXPR)
8471 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
8472 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
8473 return
8474 build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
8475 fold_convert (TREE_TYPE (arg0),
8476 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
8477 TREE_OPERAND (TREE_OPERAND (arg1, 0),
8478 1))),
8479 fold_convert (TREE_TYPE (arg0), integer_zero_node));
8481 /* Simplify comparison of something with itself. (For IEEE
8482 floating-point, we can only do some of these simplifications.) */
8483 if (operand_equal_p (arg0, arg1, 0))
8485 switch (code)
8487 case EQ_EXPR:
8488 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
8489 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
8490 return constant_boolean_node (1, type);
8491 break;
8493 case GE_EXPR:
8494 case LE_EXPR:
8495 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
8496 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
8497 return constant_boolean_node (1, type);
8498 return fold (build2 (EQ_EXPR, type, arg0, arg1));
8500 case NE_EXPR:
8501 /* For NE, we can only do this simplification if integer
8502 or we don't honor IEEE floating point NaNs. */
8503 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
8504 && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
8505 break;
8506 /* ... fall through ... */
8507 case GT_EXPR:
8508 case LT_EXPR:
8509 return constant_boolean_node (0, type);
8510 default:
8511 abort ();
8515 /* If we are comparing an expression that just has comparisons
8516 of two integer values, arithmetic expressions of those comparisons,
8517 and constants, we can simplify it. There are only three cases
8518 to check: the two values can either be equal, the first can be
8519 greater, or the second can be greater. Fold the expression for
8520 those three values. Since each value must be 0 or 1, we have
8521 eight possibilities, each of which corresponds to the constant 0
8522 or 1 or one of the six possible comparisons.
8524 This handles common cases like (a > b) == 0 but also handles
8525 expressions like ((x > y) - (y > x)) > 0, which supposedly
8526 occur in macroized code. */
8528 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
8530 tree cval1 = 0, cval2 = 0;
8531 int save_p = 0;
8533 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
8534 /* Don't handle degenerate cases here; they should already
8535 have been handled anyway. */
8536 && cval1 != 0 && cval2 != 0
8537 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
8538 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
8539 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
8540 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
8541 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
8542 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
8543 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
8545 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
8546 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
8548 /* We can't just pass T to eval_subst in case cval1 or cval2
8549 was the same as ARG1. */
8551 tree high_result
8552 = fold (build2 (code, type,
8553 eval_subst (arg0, cval1, maxval,
8554 cval2, minval),
8555 arg1));
8556 tree equal_result
8557 = fold (build2 (code, type,
8558 eval_subst (arg0, cval1, maxval,
8559 cval2, maxval),
8560 arg1));
8561 tree low_result
8562 = fold (build2 (code, type,
8563 eval_subst (arg0, cval1, minval,
8564 cval2, maxval),
8565 arg1));
8567 /* All three of these results should be 0 or 1. Confirm they
8568 are. Then use those values to select the proper code
8569 to use. */
8571 if ((integer_zerop (high_result)
8572 || integer_onep (high_result))
8573 && (integer_zerop (equal_result)
8574 || integer_onep (equal_result))
8575 && (integer_zerop (low_result)
8576 || integer_onep (low_result)))
8578 /* Make a 3-bit mask with the high-order bit being the
8579 value for `>', the next for '=', and the low for '<'. */
8580 switch ((integer_onep (high_result) * 4)
8581 + (integer_onep (equal_result) * 2)
8582 + integer_onep (low_result))
8584 case 0:
8585 /* Always false. */
8586 return omit_one_operand (type, integer_zero_node, arg0);
8587 case 1:
8588 code = LT_EXPR;
8589 break;
8590 case 2:
8591 code = EQ_EXPR;
8592 break;
8593 case 3:
8594 code = LE_EXPR;
8595 break;
8596 case 4:
8597 code = GT_EXPR;
8598 break;
8599 case 5:
8600 code = NE_EXPR;
8601 break;
8602 case 6:
8603 code = GE_EXPR;
8604 break;
8605 case 7:
8606 /* Always true. */
8607 return omit_one_operand (type, integer_one_node, arg0);
8610 tem = build2 (code, type, cval1, cval2);
8611 if (save_p)
8612 return save_expr (tem);
8613 else
8614 return fold (tem);
8619 /* If this is a comparison of a field, we may be able to simplify it. */
8620 if (((TREE_CODE (arg0) == COMPONENT_REF
8621 && lang_hooks.can_use_bit_fields_p ())
8622 || TREE_CODE (arg0) == BIT_FIELD_REF)
8623 && (code == EQ_EXPR || code == NE_EXPR)
8624 /* Handle the constant case even without -O
8625 to make sure the warnings are given. */
8626 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
8628 t1 = optimize_bit_field_compare (code, type, arg0, arg1);
8629 if (t1)
8630 return t1;
8633 /* If this is a comparison of complex values and either or both sides
8634 are a COMPLEX_EXPR or COMPLEX_CST, it is best to split up the
8635 comparisons and join them with a TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR.
8636 This may prevent needless evaluations. */
8637 if ((code == EQ_EXPR || code == NE_EXPR)
8638 && TREE_CODE (TREE_TYPE (arg0)) == COMPLEX_TYPE
8639 && (TREE_CODE (arg0) == COMPLEX_EXPR
8640 || TREE_CODE (arg1) == COMPLEX_EXPR
8641 || TREE_CODE (arg0) == COMPLEX_CST
8642 || TREE_CODE (arg1) == COMPLEX_CST))
8644 tree subtype = TREE_TYPE (TREE_TYPE (arg0));
8645 tree real0, imag0, real1, imag1;
8647 arg0 = save_expr (arg0);
8648 arg1 = save_expr (arg1);
8649 real0 = fold (build1 (REALPART_EXPR, subtype, arg0));
8650 imag0 = fold (build1 (IMAGPART_EXPR, subtype, arg0));
8651 real1 = fold (build1 (REALPART_EXPR, subtype, arg1));
8652 imag1 = fold (build1 (IMAGPART_EXPR, subtype, arg1));
8654 return fold (build2 ((code == EQ_EXPR ? TRUTH_ANDIF_EXPR
8655 : TRUTH_ORIF_EXPR),
8656 type,
8657 fold (build2 (code, type, real0, real1)),
8658 fold (build2 (code, type, imag0, imag1))));
8661 /* Optimize comparisons of strlen vs zero to a compare of the
8662 first character of the string vs zero. To wit,
8663 strlen(ptr) == 0 => *ptr == 0
8664 strlen(ptr) != 0 => *ptr != 0
8665 Other cases should reduce to one of these two (or a constant)
8666 due to the return value of strlen being unsigned. */
8667 if ((code == EQ_EXPR || code == NE_EXPR)
8668 && integer_zerop (arg1)
8669 && TREE_CODE (arg0) == CALL_EXPR)
8671 tree fndecl = get_callee_fndecl (arg0);
8672 tree arglist;
8674 if (fndecl
8675 && DECL_BUILT_IN (fndecl)
8676 && DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_MD
8677 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
8678 && (arglist = TREE_OPERAND (arg0, 1))
8679 && TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) == POINTER_TYPE
8680 && ! TREE_CHAIN (arglist))
8681 return fold (build2 (code, type,
8682 build1 (INDIRECT_REF, char_type_node,
8683 TREE_VALUE (arglist)),
8684 fold_convert (char_type_node,
8685 integer_zero_node)));
8688 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
8689 into a single range test. */
8690 if (TREE_CODE (arg0) == TRUNC_DIV_EXPR
8691 && TREE_CODE (arg1) == INTEGER_CST
8692 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8693 && !integer_zerop (TREE_OPERAND (arg0, 1))
8694 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8695 && !TREE_OVERFLOW (arg1))
8697 t1 = fold_div_compare (code, type, arg0, arg1);
8698 if (t1 != NULL_TREE)
8699 return t1;
8702 if ((code == EQ_EXPR || code == NE_EXPR)
8703 && !TREE_SIDE_EFFECTS (arg0)
8704 && integer_zerop (arg1)
8705 && tree_expr_nonzero_p (arg0))
8706 return constant_boolean_node (code==NE_EXPR, type);
8708 t1 = fold_relational_const (code, type, arg0, arg1);
8709 return t1 == NULL_TREE ? t : t1;
8711 case UNORDERED_EXPR:
8712 case ORDERED_EXPR:
8713 case UNLT_EXPR:
8714 case UNLE_EXPR:
8715 case UNGT_EXPR:
8716 case UNGE_EXPR:
8717 case UNEQ_EXPR:
8718 case LTGT_EXPR:
8719 if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
8721 t1 = fold_relational_const (code, type, arg0, arg1);
8722 if (t1 != NULL_TREE)
8723 return t1;
8726 /* If the first operand is NaN, the result is constant. */
8727 if (TREE_CODE (arg0) == REAL_CST
8728 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
8729 && (code != LTGT_EXPR || ! flag_trapping_math))
8731 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
8732 ? integer_zero_node
8733 : integer_one_node;
8734 return omit_one_operand (type, t1, arg1);
8737 /* If the second operand is NaN, the result is constant. */
8738 if (TREE_CODE (arg1) == REAL_CST
8739 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
8740 && (code != LTGT_EXPR || ! flag_trapping_math))
8742 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
8743 ? integer_zero_node
8744 : integer_one_node;
8745 return omit_one_operand (type, t1, arg0);
8748 /* Simplify unordered comparison of something with itself. */
8749 if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
8750 && operand_equal_p (arg0, arg1, 0))
8751 return constant_boolean_node (1, type);
8753 if (code == LTGT_EXPR
8754 && !flag_trapping_math
8755 && operand_equal_p (arg0, arg1, 0))
8756 return constant_boolean_node (0, type);
8758 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
8760 tree targ0 = strip_float_extensions (arg0);
8761 tree targ1 = strip_float_extensions (arg1);
8762 tree newtype = TREE_TYPE (targ0);
8764 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
8765 newtype = TREE_TYPE (targ1);
8767 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
8768 return fold (build2 (code, type, fold_convert (newtype, targ0),
8769 fold_convert (newtype, targ1)));
8772 return t;
8774 case COND_EXPR:
8775 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
8776 so all simple results must be passed through pedantic_non_lvalue. */
8777 if (TREE_CODE (arg0) == INTEGER_CST)
8779 tem = TREE_OPERAND (t, (integer_zerop (arg0) ? 2 : 1));
8780 /* Only optimize constant conditions when the selected branch
8781 has the same type as the COND_EXPR. This avoids optimizing
8782 away "c ? x : throw", where the throw has a void type. */
8783 if (! VOID_TYPE_P (TREE_TYPE (tem))
8784 || VOID_TYPE_P (type))
8785 return pedantic_non_lvalue (tem);
8786 return t;
8788 if (operand_equal_p (arg1, TREE_OPERAND (t, 2), 0))
8789 return pedantic_omit_one_operand (type, arg1, arg0);
8791 /* If we have A op B ? A : C, we may be able to convert this to a
8792 simpler expression, depending on the operation and the values
8793 of B and C. Signed zeros prevent all of these transformations,
8794 for reasons given above each one.
8796 Also try swapping the arguments and inverting the conditional. */
8797 if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
8798 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
8799 arg1, TREE_OPERAND (arg0, 1))
8800 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
8802 tem = fold_cond_expr_with_comparison (type, arg0,
8803 TREE_OPERAND (t, 1),
8804 TREE_OPERAND (t, 2));
8805 if (tem)
8806 return tem;
8809 if (TREE_CODE_CLASS (TREE_CODE (arg0)) == '<'
8810 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
8811 TREE_OPERAND (t, 2),
8812 TREE_OPERAND (arg0, 1))
8813 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (TREE_OPERAND (t, 2)))))
8815 tem = invert_truthvalue (arg0);
8816 if (TREE_CODE_CLASS (TREE_CODE (tem)) == '<')
8818 tem = fold_cond_expr_with_comparison (type, tem,
8819 TREE_OPERAND (t, 2),
8820 TREE_OPERAND (t, 1));
8821 if (tem)
8822 return tem;
8826 /* If the second operand is simpler than the third, swap them
8827 since that produces better jump optimization results. */
8828 if (tree_swap_operands_p (TREE_OPERAND (t, 1),
8829 TREE_OPERAND (t, 2), false))
8831 /* See if this can be inverted. If it can't, possibly because
8832 it was a floating-point inequality comparison, don't do
8833 anything. */
8834 tem = invert_truthvalue (arg0);
8836 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
8837 return fold (build3 (code, type, tem,
8838 TREE_OPERAND (t, 2), TREE_OPERAND (t, 1)));
8841 /* Convert A ? 1 : 0 to simply A. */
8842 if (integer_onep (TREE_OPERAND (t, 1))
8843 && integer_zerop (TREE_OPERAND (t, 2))
8844 /* If we try to convert TREE_OPERAND (t, 0) to our type, the
8845 call to fold will try to move the conversion inside
8846 a COND, which will recurse. In that case, the COND_EXPR
8847 is probably the best choice, so leave it alone. */
8848 && type == TREE_TYPE (arg0))
8849 return pedantic_non_lvalue (arg0);
8851 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
8852 over COND_EXPR in cases such as floating point comparisons. */
8853 if (integer_zerop (TREE_OPERAND (t, 1))
8854 && integer_onep (TREE_OPERAND (t, 2))
8855 && truth_value_p (TREE_CODE (arg0)))
8856 return pedantic_non_lvalue (fold_convert (type,
8857 invert_truthvalue (arg0)));
8859 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
8860 if (TREE_CODE (arg0) == LT_EXPR
8861 && integer_zerop (TREE_OPERAND (arg0, 1))
8862 && integer_zerop (TREE_OPERAND (t, 2))
8863 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
8864 return fold_convert (type, fold (build2 (BIT_AND_EXPR,
8865 TREE_TYPE (tem), tem, arg1)));
8867 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
8868 already handled above. */
8869 if (TREE_CODE (arg0) == BIT_AND_EXPR
8870 && integer_onep (TREE_OPERAND (arg0, 1))
8871 && integer_zerop (TREE_OPERAND (t, 2))
8872 && integer_pow2p (arg1))
8874 tree tem = TREE_OPERAND (arg0, 0);
8875 STRIP_NOPS (tem);
8876 if (TREE_CODE (tem) == RSHIFT_EXPR
8877 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
8878 TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
8879 return fold (build2 (BIT_AND_EXPR, type,
8880 TREE_OPERAND (tem, 0), arg1));
8883 /* A & N ? N : 0 is simply A & N if N is a power of two. This
8884 is probably obsolete because the first operand should be a
8885 truth value (that's why we have the two cases above), but let's
8886 leave it in until we can confirm this for all front-ends. */
8887 if (integer_zerop (TREE_OPERAND (t, 2))
8888 && TREE_CODE (arg0) == NE_EXPR
8889 && integer_zerop (TREE_OPERAND (arg0, 1))
8890 && integer_pow2p (arg1)
8891 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
8892 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
8893 arg1, OEP_ONLY_CONST))
8894 return pedantic_non_lvalue (fold_convert (type,
8895 TREE_OPERAND (arg0, 0)));
8897 /* Convert A ? B : 0 into A && B if A and B are truth values. */
8898 if (integer_zerop (TREE_OPERAND (t, 2))
8899 && truth_value_p (TREE_CODE (arg0))
8900 && truth_value_p (TREE_CODE (arg1)))
8901 return fold (build2 (TRUTH_ANDIF_EXPR, type, arg0, arg1));
8903 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
8904 if (integer_onep (TREE_OPERAND (t, 2))
8905 && truth_value_p (TREE_CODE (arg0))
8906 && truth_value_p (TREE_CODE (arg1)))
8908 /* Only perform transformation if ARG0 is easily inverted. */
8909 tem = invert_truthvalue (arg0);
8910 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
8911 return fold (build2 (TRUTH_ORIF_EXPR, type, tem, arg1));
8914 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
8915 if (integer_zerop (arg1)
8916 && truth_value_p (TREE_CODE (arg0))
8917 && truth_value_p (TREE_CODE (TREE_OPERAND (t, 2))))
8919 /* Only perform transformation if ARG0 is easily inverted. */
8920 tem = invert_truthvalue (arg0);
8921 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
8922 return fold (build2 (TRUTH_ANDIF_EXPR, type, tem,
8923 TREE_OPERAND (t, 2)));
8926 /* Convert A ? 1 : B into A || B if A and B are truth values. */
8927 if (integer_onep (arg1)
8928 && truth_value_p (TREE_CODE (arg0))
8929 && truth_value_p (TREE_CODE (TREE_OPERAND (t, 2))))
8930 return fold (build2 (TRUTH_ORIF_EXPR, type, arg0,
8931 TREE_OPERAND (t, 2)));
8933 return t;
8935 case COMPOUND_EXPR:
8936 /* When pedantic, a compound expression can be neither an lvalue
8937 nor an integer constant expression. */
8938 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
8939 return t;
8940 /* Don't let (0, 0) be null pointer constant. */
8941 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
8942 : fold_convert (type, arg1);
8943 return pedantic_non_lvalue (tem);
8945 case COMPLEX_EXPR:
8946 if (wins)
8947 return build_complex (type, arg0, arg1);
8948 return t;
8950 case REALPART_EXPR:
8951 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8952 return t;
8953 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
8954 return omit_one_operand (type, TREE_OPERAND (arg0, 0),
8955 TREE_OPERAND (arg0, 1));
8956 else if (TREE_CODE (arg0) == COMPLEX_CST)
8957 return TREE_REALPART (arg0);
8958 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8959 return fold (build2 (TREE_CODE (arg0), type,
8960 fold (build1 (REALPART_EXPR, type,
8961 TREE_OPERAND (arg0, 0))),
8962 fold (build1 (REALPART_EXPR, type,
8963 TREE_OPERAND (arg0, 1)))));
8964 return t;
8966 case IMAGPART_EXPR:
8967 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8968 return fold_convert (type, integer_zero_node);
8969 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
8970 return omit_one_operand (type, TREE_OPERAND (arg0, 1),
8971 TREE_OPERAND (arg0, 0));
8972 else if (TREE_CODE (arg0) == COMPLEX_CST)
8973 return TREE_IMAGPART (arg0);
8974 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8975 return fold (build2 (TREE_CODE (arg0), type,
8976 fold (build1 (IMAGPART_EXPR, type,
8977 TREE_OPERAND (arg0, 0))),
8978 fold (build1 (IMAGPART_EXPR, type,
8979 TREE_OPERAND (arg0, 1)))));
8980 return t;
8982 /* Pull arithmetic ops out of the CLEANUP_POINT_EXPR where
8983 appropriate. */
8984 case CLEANUP_POINT_EXPR:
8985 if (! has_cleanups (arg0))
8986 return TREE_OPERAND (t, 0);
8989 enum tree_code code0 = TREE_CODE (arg0);
8990 int kind0 = TREE_CODE_CLASS (code0);
8991 tree arg00 = TREE_OPERAND (arg0, 0);
8992 tree arg01;
8994 if (kind0 == '1' || code0 == TRUTH_NOT_EXPR)
8995 return fold (build1 (code0, type,
8996 fold (build1 (CLEANUP_POINT_EXPR,
8997 TREE_TYPE (arg00), arg00))));
8999 if (kind0 == '<' || kind0 == '2'
9000 || code0 == TRUTH_ANDIF_EXPR || code0 == TRUTH_ORIF_EXPR
9001 || code0 == TRUTH_AND_EXPR || code0 == TRUTH_OR_EXPR
9002 || code0 == TRUTH_XOR_EXPR)
9004 arg01 = TREE_OPERAND (arg0, 1);
9006 if (TREE_CONSTANT (arg00)
9007 || ((code0 == TRUTH_ANDIF_EXPR || code0 == TRUTH_ORIF_EXPR)
9008 && ! has_cleanups (arg00)))
9009 return fold (build2 (code0, type, arg00,
9010 fold (build1 (CLEANUP_POINT_EXPR,
9011 TREE_TYPE (arg01), arg01))));
9013 if (TREE_CONSTANT (arg01))
9014 return fold (build2 (code0, type,
9015 fold (build1 (CLEANUP_POINT_EXPR,
9016 TREE_TYPE (arg00), arg00)),
9017 arg01));
9020 return t;
9023 case CALL_EXPR:
9024 /* Check for a built-in function. */
9025 if (TREE_CODE (TREE_OPERAND (t, 0)) == ADDR_EXPR
9026 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (t, 0), 0))
9027 == FUNCTION_DECL)
9028 && DECL_BUILT_IN (TREE_OPERAND (TREE_OPERAND (t, 0), 0)))
9030 tree tmp = fold_builtin (t, false);
9031 if (tmp)
9032 return tmp;
9034 return t;
9036 default:
9037 return t;
9038 } /* switch (code) */
9041 #ifdef ENABLE_FOLD_CHECKING
9042 #undef fold
9044 static void fold_checksum_tree (tree, struct md5_ctx *, htab_t);
9045 static void fold_check_failed (tree, tree);
9046 void print_fold_checksum (tree);
9048 /* When --enable-checking=fold, compute a digest of expr before
9049 and after actual fold call to see if fold did not accidentally
9050 change original expr. */
9052 tree
9053 fold (tree expr)
9055 tree ret;
9056 struct md5_ctx ctx;
9057 unsigned char checksum_before[16], checksum_after[16];
9058 htab_t ht;
9060 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
9061 md5_init_ctx (&ctx);
9062 fold_checksum_tree (expr, &ctx, ht);
9063 md5_finish_ctx (&ctx, checksum_before);
9064 htab_empty (ht);
9066 ret = fold_1 (expr);
9068 md5_init_ctx (&ctx);
9069 fold_checksum_tree (expr, &ctx, ht);
9070 md5_finish_ctx (&ctx, checksum_after);
9071 htab_delete (ht);
9073 if (memcmp (checksum_before, checksum_after, 16))
9074 fold_check_failed (expr, ret);
9076 return ret;
9079 void
9080 print_fold_checksum (tree expr)
9082 struct md5_ctx ctx;
9083 unsigned char checksum[16], cnt;
9084 htab_t ht;
9086 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
9087 md5_init_ctx (&ctx);
9088 fold_checksum_tree (expr, &ctx, ht);
9089 md5_finish_ctx (&ctx, checksum);
9090 htab_delete (ht);
9091 for (cnt = 0; cnt < 16; ++cnt)
9092 fprintf (stderr, "%02x", checksum[cnt]);
9093 putc ('\n', stderr);
9096 static void
9097 fold_check_failed (tree expr ATTRIBUTE_UNUSED, tree ret ATTRIBUTE_UNUSED)
9099 internal_error ("fold check: original tree changed by fold");
9102 static void
9103 fold_checksum_tree (tree expr, struct md5_ctx *ctx, htab_t ht)
9105 void **slot;
9106 enum tree_code code;
9107 char buf[sizeof (struct tree_decl)];
9108 int i, len;
9110 if (sizeof (struct tree_exp) + 5 * sizeof (tree)
9111 > sizeof (struct tree_decl)
9112 || sizeof (struct tree_type) > sizeof (struct tree_decl))
9113 abort ();
9114 if (expr == NULL)
9115 return;
9116 slot = htab_find_slot (ht, expr, INSERT);
9117 if (*slot != NULL)
9118 return;
9119 *slot = expr;
9120 code = TREE_CODE (expr);
9121 if (TREE_CODE_CLASS (code) == 'd' && DECL_ASSEMBLER_NAME_SET_P (expr))
9123 /* Allow DECL_ASSEMBLER_NAME to be modified. */
9124 memcpy (buf, expr, tree_size (expr));
9125 expr = (tree) buf;
9126 SET_DECL_ASSEMBLER_NAME (expr, NULL);
9128 else if (TREE_CODE_CLASS (code) == 't'
9129 && (TYPE_POINTER_TO (expr) || TYPE_REFERENCE_TO (expr)))
9131 /* Allow TYPE_POINTER_TO and TYPE_REFERENCE_TO to be modified. */
9132 memcpy (buf, expr, tree_size (expr));
9133 expr = (tree) buf;
9134 TYPE_POINTER_TO (expr) = NULL;
9135 TYPE_REFERENCE_TO (expr) = NULL;
9137 md5_process_bytes (expr, tree_size (expr), ctx);
9138 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
9139 if (TREE_CODE_CLASS (code) != 't' && TREE_CODE_CLASS (code) != 'd')
9140 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
9141 switch (TREE_CODE_CLASS (code))
9143 case 'c':
9144 switch (code)
9146 case STRING_CST:
9147 md5_process_bytes (TREE_STRING_POINTER (expr),
9148 TREE_STRING_LENGTH (expr), ctx);
9149 break;
9150 case COMPLEX_CST:
9151 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
9152 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
9153 break;
9154 case VECTOR_CST:
9155 fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
9156 break;
9157 default:
9158 break;
9160 break;
9161 case 'x':
9162 switch (code)
9164 case TREE_LIST:
9165 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
9166 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
9167 break;
9168 case TREE_VEC:
9169 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
9170 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
9171 break;
9172 default:
9173 break;
9175 break;
9176 case 'e':
9177 case 'r':
9178 case '<':
9179 case '1':
9180 case '2':
9181 case 's':
9182 len = first_rtl_op (code);
9183 for (i = 0; i < len; ++i)
9184 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
9185 break;
9186 case 'd':
9187 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
9188 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
9189 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
9190 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
9191 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
9192 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
9193 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
9194 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
9195 fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
9196 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
9197 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
9198 break;
9199 case 't':
9200 if (TREE_CODE (expr) == ENUMERAL_TYPE)
9201 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
9202 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
9203 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
9204 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
9205 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
9206 if (INTEGRAL_TYPE_P (expr)
9207 || SCALAR_FLOAT_TYPE_P (expr))
9209 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
9210 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
9212 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
9213 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
9214 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
9215 break;
9216 default:
9217 break;
9221 #endif
9223 /* Perform constant folding and related simplification of initializer
9224 expression EXPR. This behaves identically to "fold" but ignores
9225 potential run-time traps and exceptions that fold must preserve. */
9227 tree
9228 fold_initializer (tree expr)
9230 int saved_signaling_nans = flag_signaling_nans;
9231 int saved_trapping_math = flag_trapping_math;
9232 int saved_trapv = flag_trapv;
9233 tree result;
9235 flag_signaling_nans = 0;
9236 flag_trapping_math = 0;
9237 flag_trapv = 0;
9239 result = fold (expr);
9241 flag_signaling_nans = saved_signaling_nans;
9242 flag_trapping_math = saved_trapping_math;
9243 flag_trapv = saved_trapv;
9245 return result;
9248 /* Determine if first argument is a multiple of second argument. Return 0 if
9249 it is not, or we cannot easily determined it to be.
9251 An example of the sort of thing we care about (at this point; this routine
9252 could surely be made more general, and expanded to do what the *_DIV_EXPR's
9253 fold cases do now) is discovering that
9255 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
9257 is a multiple of
9259 SAVE_EXPR (J * 8)
9261 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
9263 This code also handles discovering that
9265 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
9267 is a multiple of 8 so we don't have to worry about dealing with a
9268 possible remainder.
9270 Note that we *look* inside a SAVE_EXPR only to determine how it was
9271 calculated; it is not safe for fold to do much of anything else with the
9272 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
9273 at run time. For example, the latter example above *cannot* be implemented
9274 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
9275 evaluation time of the original SAVE_EXPR is not necessarily the same at
9276 the time the new expression is evaluated. The only optimization of this
9277 sort that would be valid is changing
9279 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
9281 divided by 8 to
9283 SAVE_EXPR (I) * SAVE_EXPR (J)
9285 (where the same SAVE_EXPR (J) is used in the original and the
9286 transformed version). */
9288 static int
9289 multiple_of_p (tree type, tree top, tree bottom)
9291 if (operand_equal_p (top, bottom, 0))
9292 return 1;
9294 if (TREE_CODE (type) != INTEGER_TYPE)
9295 return 0;
9297 switch (TREE_CODE (top))
9299 case MULT_EXPR:
9300 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
9301 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
9303 case PLUS_EXPR:
9304 case MINUS_EXPR:
9305 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
9306 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
9308 case LSHIFT_EXPR:
9309 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
9311 tree op1, t1;
9313 op1 = TREE_OPERAND (top, 1);
9314 /* const_binop may not detect overflow correctly,
9315 so check for it explicitly here. */
9316 if (TYPE_PRECISION (TREE_TYPE (size_one_node))
9317 > TREE_INT_CST_LOW (op1)
9318 && TREE_INT_CST_HIGH (op1) == 0
9319 && 0 != (t1 = fold_convert (type,
9320 const_binop (LSHIFT_EXPR,
9321 size_one_node,
9322 op1, 0)))
9323 && ! TREE_OVERFLOW (t1))
9324 return multiple_of_p (type, t1, bottom);
9326 return 0;
9328 case NOP_EXPR:
9329 /* Can't handle conversions from non-integral or wider integral type. */
9330 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
9331 || (TYPE_PRECISION (type)
9332 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
9333 return 0;
9335 /* .. fall through ... */
9337 case SAVE_EXPR:
9338 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
9340 case INTEGER_CST:
9341 if (TREE_CODE (bottom) != INTEGER_CST
9342 || (TYPE_UNSIGNED (type)
9343 && (tree_int_cst_sgn (top) < 0
9344 || tree_int_cst_sgn (bottom) < 0)))
9345 return 0;
9346 return integer_zerop (const_binop (TRUNC_MOD_EXPR,
9347 top, bottom, 0));
9349 default:
9350 return 0;
9354 /* Return true if `t' is known to be non-negative. */
9357 tree_expr_nonnegative_p (tree t)
9359 switch (TREE_CODE (t))
9361 case ABS_EXPR:
9362 return 1;
9364 case INTEGER_CST:
9365 return tree_int_cst_sgn (t) >= 0;
9367 case REAL_CST:
9368 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
9370 case PLUS_EXPR:
9371 if (FLOAT_TYPE_P (TREE_TYPE (t)))
9372 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
9373 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
9375 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
9376 both unsigned and at least 2 bits shorter than the result. */
9377 if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
9378 && TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
9379 && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR)
9381 tree inner1 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
9382 tree inner2 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0));
9383 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
9384 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
9386 unsigned int prec = MAX (TYPE_PRECISION (inner1),
9387 TYPE_PRECISION (inner2)) + 1;
9388 return prec < TYPE_PRECISION (TREE_TYPE (t));
9391 break;
9393 case MULT_EXPR:
9394 if (FLOAT_TYPE_P (TREE_TYPE (t)))
9396 /* x * x for floating point x is always non-negative. */
9397 if (operand_equal_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1), 0))
9398 return 1;
9399 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
9400 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
9403 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
9404 both unsigned and their total bits is shorter than the result. */
9405 if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
9406 && TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
9407 && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR)
9409 tree inner1 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
9410 tree inner2 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0));
9411 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
9412 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
9413 return TYPE_PRECISION (inner1) + TYPE_PRECISION (inner2)
9414 < TYPE_PRECISION (TREE_TYPE (t));
9416 return 0;
9418 case TRUNC_DIV_EXPR:
9419 case CEIL_DIV_EXPR:
9420 case FLOOR_DIV_EXPR:
9421 case ROUND_DIV_EXPR:
9422 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
9423 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
9425 case TRUNC_MOD_EXPR:
9426 case CEIL_MOD_EXPR:
9427 case FLOOR_MOD_EXPR:
9428 case ROUND_MOD_EXPR:
9429 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
9431 case RDIV_EXPR:
9432 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
9433 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
9435 case BIT_AND_EXPR:
9436 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1))
9437 || tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
9438 case BIT_IOR_EXPR:
9439 case BIT_XOR_EXPR:
9440 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
9441 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
9443 case NOP_EXPR:
9445 tree inner_type = TREE_TYPE (TREE_OPERAND (t, 0));
9446 tree outer_type = TREE_TYPE (t);
9448 if (TREE_CODE (outer_type) == REAL_TYPE)
9450 if (TREE_CODE (inner_type) == REAL_TYPE)
9451 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
9452 if (TREE_CODE (inner_type) == INTEGER_TYPE)
9454 if (TYPE_UNSIGNED (inner_type))
9455 return 1;
9456 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
9459 else if (TREE_CODE (outer_type) == INTEGER_TYPE)
9461 if (TREE_CODE (inner_type) == REAL_TYPE)
9462 return tree_expr_nonnegative_p (TREE_OPERAND (t,0));
9463 if (TREE_CODE (inner_type) == INTEGER_TYPE)
9464 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
9465 && TYPE_UNSIGNED (inner_type);
9468 break;
9470 case COND_EXPR:
9471 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1))
9472 && tree_expr_nonnegative_p (TREE_OPERAND (t, 2));
9473 case COMPOUND_EXPR:
9474 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
9475 case MIN_EXPR:
9476 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
9477 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
9478 case MAX_EXPR:
9479 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
9480 || tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
9481 case MODIFY_EXPR:
9482 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
9483 case BIND_EXPR:
9484 return tree_expr_nonnegative_p (expr_last (TREE_OPERAND (t, 1)));
9485 case SAVE_EXPR:
9486 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
9487 case NON_LVALUE_EXPR:
9488 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
9489 case FLOAT_EXPR:
9490 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
9492 case TARGET_EXPR:
9494 tree temp = TARGET_EXPR_SLOT (t);
9495 t = TARGET_EXPR_INITIAL (t);
9497 /* If the initializer is non-void, then it's a normal expression
9498 that will be assigned to the slot. */
9499 if (!VOID_TYPE_P (t))
9500 return tree_expr_nonnegative_p (t);
9502 /* Otherwise, the initializer sets the slot in some way. One common
9503 way is an assignment statement at the end of the initializer. */
9504 while (1)
9506 if (TREE_CODE (t) == BIND_EXPR)
9507 t = expr_last (BIND_EXPR_BODY (t));
9508 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
9509 || TREE_CODE (t) == TRY_CATCH_EXPR)
9510 t = expr_last (TREE_OPERAND (t, 0));
9511 else if (TREE_CODE (t) == STATEMENT_LIST)
9512 t = expr_last (t);
9513 else
9514 break;
9516 if (TREE_CODE (t) == MODIFY_EXPR
9517 && TREE_OPERAND (t, 0) == temp)
9518 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
9520 return 0;
9523 case CALL_EXPR:
9525 tree fndecl = get_callee_fndecl (t);
9526 tree arglist = TREE_OPERAND (t, 1);
9527 if (fndecl
9528 && DECL_BUILT_IN (fndecl)
9529 && DECL_BUILT_IN_CLASS (fndecl) != BUILT_IN_MD)
9530 switch (DECL_FUNCTION_CODE (fndecl))
9532 #define CASE_BUILTIN_F(BUILT_IN_FN) \
9533 case BUILT_IN_FN: case BUILT_IN_FN##F: case BUILT_IN_FN##L:
9534 #define CASE_BUILTIN_I(BUILT_IN_FN) \
9535 case BUILT_IN_FN: case BUILT_IN_FN##L: case BUILT_IN_FN##LL:
9537 CASE_BUILTIN_F (BUILT_IN_ACOS)
9538 CASE_BUILTIN_F (BUILT_IN_ACOSH)
9539 CASE_BUILTIN_F (BUILT_IN_CABS)
9540 CASE_BUILTIN_F (BUILT_IN_COSH)
9541 CASE_BUILTIN_F (BUILT_IN_ERFC)
9542 CASE_BUILTIN_F (BUILT_IN_EXP)
9543 CASE_BUILTIN_F (BUILT_IN_EXP10)
9544 CASE_BUILTIN_F (BUILT_IN_EXP2)
9545 CASE_BUILTIN_F (BUILT_IN_FABS)
9546 CASE_BUILTIN_F (BUILT_IN_FDIM)
9547 CASE_BUILTIN_F (BUILT_IN_FREXP)
9548 CASE_BUILTIN_F (BUILT_IN_HYPOT)
9549 CASE_BUILTIN_F (BUILT_IN_POW10)
9550 CASE_BUILTIN_I (BUILT_IN_FFS)
9551 CASE_BUILTIN_I (BUILT_IN_PARITY)
9552 CASE_BUILTIN_I (BUILT_IN_POPCOUNT)
9553 /* Always true. */
9554 return 1;
9556 CASE_BUILTIN_F (BUILT_IN_SQRT)
9557 /* sqrt(-0.0) is -0.0. */
9558 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (t))))
9559 return 1;
9560 return tree_expr_nonnegative_p (TREE_VALUE (arglist));
9562 CASE_BUILTIN_F (BUILT_IN_ASINH)
9563 CASE_BUILTIN_F (BUILT_IN_ATAN)
9564 CASE_BUILTIN_F (BUILT_IN_ATANH)
9565 CASE_BUILTIN_F (BUILT_IN_CBRT)
9566 CASE_BUILTIN_F (BUILT_IN_CEIL)
9567 CASE_BUILTIN_F (BUILT_IN_ERF)
9568 CASE_BUILTIN_F (BUILT_IN_EXPM1)
9569 CASE_BUILTIN_F (BUILT_IN_FLOOR)
9570 CASE_BUILTIN_F (BUILT_IN_FMOD)
9571 CASE_BUILTIN_F (BUILT_IN_LDEXP)
9572 CASE_BUILTIN_F (BUILT_IN_LLRINT)
9573 CASE_BUILTIN_F (BUILT_IN_LLROUND)
9574 CASE_BUILTIN_F (BUILT_IN_LRINT)
9575 CASE_BUILTIN_F (BUILT_IN_LROUND)
9576 CASE_BUILTIN_F (BUILT_IN_MODF)
9577 CASE_BUILTIN_F (BUILT_IN_NEARBYINT)
9578 CASE_BUILTIN_F (BUILT_IN_POW)
9579 CASE_BUILTIN_F (BUILT_IN_RINT)
9580 CASE_BUILTIN_F (BUILT_IN_ROUND)
9581 CASE_BUILTIN_F (BUILT_IN_SIGNBIT)
9582 CASE_BUILTIN_F (BUILT_IN_SINH)
9583 CASE_BUILTIN_F (BUILT_IN_TANH)
9584 CASE_BUILTIN_F (BUILT_IN_TRUNC)
9585 /* True if the 1st argument is nonnegative. */
9586 return tree_expr_nonnegative_p (TREE_VALUE (arglist));
9588 CASE_BUILTIN_F (BUILT_IN_FMAX)
9589 /* True if the 1st OR 2nd arguments are nonnegative. */
9590 return tree_expr_nonnegative_p (TREE_VALUE (arglist))
9591 || tree_expr_nonnegative_p (TREE_VALUE (TREE_CHAIN (arglist)));
9593 CASE_BUILTIN_F (BUILT_IN_FMIN)
9594 /* True if the 1st AND 2nd arguments are nonnegative. */
9595 return tree_expr_nonnegative_p (TREE_VALUE (arglist))
9596 && tree_expr_nonnegative_p (TREE_VALUE (TREE_CHAIN (arglist)));
9598 CASE_BUILTIN_F (BUILT_IN_COPYSIGN)
9599 /* True if the 2nd argument is nonnegative. */
9600 return tree_expr_nonnegative_p (TREE_VALUE (TREE_CHAIN (arglist)));
9602 default:
9603 break;
9604 #undef CASE_BUILTIN_F
9605 #undef CASE_BUILTIN_I
9609 /* ... fall through ... */
9611 default:
9612 if (truth_value_p (TREE_CODE (t)))
9613 /* Truth values evaluate to 0 or 1, which is nonnegative. */
9614 return 1;
9617 /* We don't know sign of `t', so be conservative and return false. */
9618 return 0;
9621 /* Return true when T is an address and is known to be nonzero.
9622 For floating point we further ensure that T is not denormal.
9623 Similar logic is present in nonzero_address in rtlanal.h */
9625 static bool
9626 tree_expr_nonzero_p (tree t)
9628 tree type = TREE_TYPE (t);
9630 /* Doing something useful for floating point would need more work. */
9631 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
9632 return false;
9634 switch (TREE_CODE (t))
9636 case ABS_EXPR:
9637 if (!TYPE_UNSIGNED (type) && !flag_wrapv)
9638 return tree_expr_nonzero_p (TREE_OPERAND (t, 0));
9640 case INTEGER_CST:
9641 return !integer_zerop (t);
9643 case PLUS_EXPR:
9644 if (!TYPE_UNSIGNED (type) && !flag_wrapv)
9646 /* With the presence of negative values it is hard
9647 to say something. */
9648 if (!tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
9649 || !tree_expr_nonnegative_p (TREE_OPERAND (t, 1)))
9650 return false;
9651 /* One of operands must be positive and the other non-negative. */
9652 return (tree_expr_nonzero_p (TREE_OPERAND (t, 0))
9653 || tree_expr_nonzero_p (TREE_OPERAND (t, 1)));
9655 break;
9657 case MULT_EXPR:
9658 if (!TYPE_UNSIGNED (type) && !flag_wrapv)
9660 return (tree_expr_nonzero_p (TREE_OPERAND (t, 0))
9661 && tree_expr_nonzero_p (TREE_OPERAND (t, 1)));
9663 break;
9665 case NOP_EXPR:
9667 tree inner_type = TREE_TYPE (TREE_OPERAND (t, 0));
9668 tree outer_type = TREE_TYPE (t);
9670 return (TYPE_PRECISION (inner_type) >= TYPE_PRECISION (outer_type)
9671 && tree_expr_nonzero_p (TREE_OPERAND (t, 0)));
9673 break;
9675 case ADDR_EXPR:
9676 /* Weak declarations may link to NULL. */
9677 if (DECL_P (TREE_OPERAND (t, 0)))
9678 return !DECL_WEAK (TREE_OPERAND (t, 0));
9679 /* Constants and all other cases are never weak. */
9680 return true;
9682 case COND_EXPR:
9683 return (tree_expr_nonzero_p (TREE_OPERAND (t, 1))
9684 && tree_expr_nonzero_p (TREE_OPERAND (t, 2)));
9686 case MIN_EXPR:
9687 return (tree_expr_nonzero_p (TREE_OPERAND (t, 0))
9688 && tree_expr_nonzero_p (TREE_OPERAND (t, 1)));
9690 case MAX_EXPR:
9691 if (tree_expr_nonzero_p (TREE_OPERAND (t, 0)))
9693 /* When both operands are nonzero, then MAX must be too. */
9694 if (tree_expr_nonzero_p (TREE_OPERAND (t, 1)))
9695 return true;
9697 /* MAX where operand 0 is positive is positive. */
9698 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
9700 /* MAX where operand 1 is positive is positive. */
9701 else if (tree_expr_nonzero_p (TREE_OPERAND (t, 1))
9702 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1)))
9703 return true;
9704 break;
9706 case COMPOUND_EXPR:
9707 case MODIFY_EXPR:
9708 case BIND_EXPR:
9709 return tree_expr_nonzero_p (TREE_OPERAND (t, 1));
9711 case SAVE_EXPR:
9712 case NON_LVALUE_EXPR:
9713 return tree_expr_nonzero_p (TREE_OPERAND (t, 0));
9715 case BIT_IOR_EXPR:
9716 return tree_expr_nonzero_p (TREE_OPERAND (t, 1))
9717 || tree_expr_nonzero_p (TREE_OPERAND (t, 0));
9719 default:
9720 break;
9722 return false;
9725 /* Return true if `r' is known to be non-negative.
9726 Only handles constants at the moment. */
9729 rtl_expr_nonnegative_p (rtx r)
9731 switch (GET_CODE (r))
9733 case CONST_INT:
9734 return INTVAL (r) >= 0;
9736 case CONST_DOUBLE:
9737 if (GET_MODE (r) == VOIDmode)
9738 return CONST_DOUBLE_HIGH (r) >= 0;
9739 return 0;
9741 case CONST_VECTOR:
9743 int units, i;
9744 rtx elt;
9746 units = CONST_VECTOR_NUNITS (r);
9748 for (i = 0; i < units; ++i)
9750 elt = CONST_VECTOR_ELT (r, i);
9751 if (!rtl_expr_nonnegative_p (elt))
9752 return 0;
9755 return 1;
9758 case SYMBOL_REF:
9759 case LABEL_REF:
9760 /* These are always nonnegative. */
9761 return 1;
9763 default:
9764 return 0;
9769 /* See if we are applying CODE, a relational to the highest or lowest
9770 possible integer of TYPE. If so, then the result is a compile
9771 time constant. */
9773 static tree
9774 fold_relational_hi_lo (enum tree_code *code_p, const tree type, tree *op0_p,
9775 tree *op1_p)
9777 tree op0 = *op0_p;
9778 tree op1 = *op1_p;
9779 enum tree_code code = *code_p;
9780 int width = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (op1)));
9782 if (TREE_CODE (op1) == INTEGER_CST
9783 && ! TREE_CONSTANT_OVERFLOW (op1)
9784 && width <= HOST_BITS_PER_WIDE_INT
9785 && (INTEGRAL_TYPE_P (TREE_TYPE (op1))
9786 || POINTER_TYPE_P (TREE_TYPE (op1))))
9788 unsigned HOST_WIDE_INT signed_max;
9789 unsigned HOST_WIDE_INT max, min;
9791 signed_max = ((unsigned HOST_WIDE_INT) 1 << (width - 1)) - 1;
9793 if (TYPE_UNSIGNED (TREE_TYPE (op1)))
9795 max = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
9796 min = 0;
9798 else
9800 max = signed_max;
9801 min = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
9804 if (TREE_INT_CST_HIGH (op1) == 0
9805 && TREE_INT_CST_LOW (op1) == max)
9806 switch (code)
9808 case GT_EXPR:
9809 return omit_one_operand (type, integer_zero_node, op0);
9811 case GE_EXPR:
9812 *code_p = EQ_EXPR;
9813 break;
9814 case LE_EXPR:
9815 return omit_one_operand (type, integer_one_node, op0);
9817 case LT_EXPR:
9818 *code_p = NE_EXPR;
9819 break;
9821 /* The GE_EXPR and LT_EXPR cases above are not normally
9822 reached because of previous transformations. */
9824 default:
9825 break;
9827 else if (TREE_INT_CST_HIGH (op1) == 0
9828 && TREE_INT_CST_LOW (op1) == max - 1)
9829 switch (code)
9831 case GT_EXPR:
9832 *code_p = EQ_EXPR;
9833 *op1_p = const_binop (PLUS_EXPR, op1, integer_one_node, 0);
9834 break;
9835 case LE_EXPR:
9836 *code_p = NE_EXPR;
9837 *op1_p = const_binop (PLUS_EXPR, op1, integer_one_node, 0);
9838 break;
9839 default:
9840 break;
9842 else if (TREE_INT_CST_HIGH (op1) == (min ? -1 : 0)
9843 && TREE_INT_CST_LOW (op1) == min)
9844 switch (code)
9846 case LT_EXPR:
9847 return omit_one_operand (type, integer_zero_node, op0);
9849 case LE_EXPR:
9850 *code_p = EQ_EXPR;
9851 break;
9853 case GE_EXPR:
9854 return omit_one_operand (type, integer_one_node, op0);
9856 case GT_EXPR:
9857 *code_p = NE_EXPR;
9858 break;
9860 default:
9861 break;
9863 else if (TREE_INT_CST_HIGH (op1) == (min ? -1 : 0)
9864 && TREE_INT_CST_LOW (op1) == min + 1)
9865 switch (code)
9867 case GE_EXPR:
9868 *code_p = NE_EXPR;
9869 *op1_p = const_binop (MINUS_EXPR, op1, integer_one_node, 0);
9870 break;
9871 case LT_EXPR:
9872 *code_p = EQ_EXPR;
9873 *op1_p = const_binop (MINUS_EXPR, op1, integer_one_node, 0);
9874 break;
9875 default:
9876 break;
9879 else if (TREE_INT_CST_HIGH (op1) == 0
9880 && TREE_INT_CST_LOW (op1) == signed_max
9881 && TYPE_UNSIGNED (TREE_TYPE (op1))
9882 /* signed_type does not work on pointer types. */
9883 && INTEGRAL_TYPE_P (TREE_TYPE (op1)))
9885 /* The following case also applies to X < signed_max+1
9886 and X >= signed_max+1 because previous transformations. */
9887 if (code == LE_EXPR || code == GT_EXPR)
9889 tree st0, st1, exp, retval;
9890 st0 = lang_hooks.types.signed_type (TREE_TYPE (op0));
9891 st1 = lang_hooks.types.signed_type (TREE_TYPE (op1));
9893 exp = build2 (code == LE_EXPR ? GE_EXPR: LT_EXPR,
9894 type,
9895 fold_convert (st0, op0),
9896 fold_convert (st1, integer_zero_node));
9898 retval
9899 = nondestructive_fold_binary_to_constant (TREE_CODE (exp),
9900 TREE_TYPE (exp),
9901 TREE_OPERAND (exp, 0),
9902 TREE_OPERAND (exp, 1));
9904 /* If we are in gimple form, then returning EXP would create
9905 non-gimple expressions. Clearing it is safe and insures
9906 we do not allow a non-gimple expression to escape. */
9907 if (in_gimple_form)
9908 exp = NULL;
9910 return (retval ? retval : exp);
9915 return NULL_TREE;
9919 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
9920 attempt to fold the expression to a constant without modifying TYPE,
9921 OP0 or OP1.
9923 If the expression could be simplified to a constant, then return
9924 the constant. If the expression would not be simplified to a
9925 constant, then return NULL_TREE.
9927 Note this is primarily designed to be called after gimplification
9928 of the tree structures and when at least one operand is a constant.
9929 As a result of those simplifying assumptions this routine is far
9930 simpler than the generic fold routine. */
9932 tree
9933 nondestructive_fold_binary_to_constant (enum tree_code code, tree type,
9934 tree op0, tree op1)
9936 int wins = 1;
9937 tree subop0;
9938 tree subop1;
9939 tree tem;
9941 /* If this is a commutative operation, and ARG0 is a constant, move it
9942 to ARG1 to reduce the number of tests below. */
9943 if (commutative_tree_code (code)
9944 && (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST))
9946 tem = op0;
9947 op0 = op1;
9948 op1 = tem;
9951 /* If either operand is a complex type, extract its real component. */
9952 if (TREE_CODE (op0) == COMPLEX_CST)
9953 subop0 = TREE_REALPART (op0);
9954 else
9955 subop0 = op0;
9957 if (TREE_CODE (op1) == COMPLEX_CST)
9958 subop1 = TREE_REALPART (op1);
9959 else
9960 subop1 = op1;
9962 /* Note if either argument is not a real or integer constant.
9963 With a few exceptions, simplification is limited to cases
9964 where both arguments are constants. */
9965 if ((TREE_CODE (subop0) != INTEGER_CST
9966 && TREE_CODE (subop0) != REAL_CST)
9967 || (TREE_CODE (subop1) != INTEGER_CST
9968 && TREE_CODE (subop1) != REAL_CST))
9969 wins = 0;
9971 switch (code)
9973 case PLUS_EXPR:
9974 /* (plus (address) (const_int)) is a constant. */
9975 if (TREE_CODE (op0) == PLUS_EXPR
9976 && TREE_CODE (op1) == INTEGER_CST
9977 && (TREE_CODE (TREE_OPERAND (op0, 0)) == ADDR_EXPR
9978 || (TREE_CODE (TREE_OPERAND (op0, 0)) == NOP_EXPR
9979 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (op0, 0), 0))
9980 == ADDR_EXPR)))
9981 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
9983 return build2 (PLUS_EXPR, type, TREE_OPERAND (op0, 0),
9984 const_binop (PLUS_EXPR, op1,
9985 TREE_OPERAND (op0, 1), 0));
9987 case BIT_XOR_EXPR:
9989 binary:
9990 if (!wins)
9991 return NULL_TREE;
9993 /* Both arguments are constants. Simplify. */
9994 tem = const_binop (code, op0, op1, 0);
9995 if (tem != NULL_TREE)
9997 /* The return value should always have the same type as
9998 the original expression. */
9999 if (TREE_TYPE (tem) != type)
10000 tem = fold_convert (type, tem);
10002 return tem;
10004 return NULL_TREE;
10006 case MINUS_EXPR:
10007 /* Fold &x - &x. This can happen from &x.foo - &x.
10008 This is unsafe for certain floats even in non-IEEE formats.
10009 In IEEE, it is unsafe because it does wrong for NaNs.
10010 Also note that operand_equal_p is always false if an
10011 operand is volatile. */
10012 if (! FLOAT_TYPE_P (type) && operand_equal_p (op0, op1, 0))
10013 return fold_convert (type, integer_zero_node);
10015 goto binary;
10017 case MULT_EXPR:
10018 case BIT_AND_EXPR:
10019 /* Special case multiplication or bitwise AND where one argument
10020 is zero. */
10021 if (! FLOAT_TYPE_P (type) && integer_zerop (op1))
10022 return omit_one_operand (type, op1, op0);
10023 else
10024 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (op0)))
10025 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op0)))
10026 && real_zerop (op1))
10027 return omit_one_operand (type, op1, op0);
10029 goto binary;
10031 case BIT_IOR_EXPR:
10032 /* Special case when we know the result will be all ones. */
10033 if (integer_all_onesp (op1))
10034 return omit_one_operand (type, op1, op0);
10036 goto binary;
10038 case TRUNC_DIV_EXPR:
10039 case ROUND_DIV_EXPR:
10040 case FLOOR_DIV_EXPR:
10041 case CEIL_DIV_EXPR:
10042 case EXACT_DIV_EXPR:
10043 case TRUNC_MOD_EXPR:
10044 case ROUND_MOD_EXPR:
10045 case FLOOR_MOD_EXPR:
10046 case CEIL_MOD_EXPR:
10047 case RDIV_EXPR:
10048 /* Division by zero is undefined. */
10049 if (integer_zerop (op1))
10050 return NULL_TREE;
10052 if (TREE_CODE (op1) == REAL_CST
10053 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (op1)))
10054 && real_zerop (op1))
10055 return NULL_TREE;
10057 goto binary;
10059 case MIN_EXPR:
10060 if (INTEGRAL_TYPE_P (type)
10061 && operand_equal_p (op1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
10062 return omit_one_operand (type, op1, op0);
10064 goto binary;
10066 case MAX_EXPR:
10067 if (INTEGRAL_TYPE_P (type)
10068 && TYPE_MAX_VALUE (type)
10069 && operand_equal_p (op1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
10070 return omit_one_operand (type, op1, op0);
10072 goto binary;
10074 case RSHIFT_EXPR:
10075 /* Optimize -1 >> x for arithmetic right shifts. */
10076 if (integer_all_onesp (op0) && ! TYPE_UNSIGNED (type))
10077 return omit_one_operand (type, op0, op1);
10078 /* ... fall through ... */
10080 case LSHIFT_EXPR:
10081 if (integer_zerop (op0))
10082 return omit_one_operand (type, op0, op1);
10084 /* Since negative shift count is not well-defined, don't
10085 try to compute it in the compiler. */
10086 if (TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sgn (op1) < 0)
10087 return NULL_TREE;
10089 goto binary;
10091 case LROTATE_EXPR:
10092 case RROTATE_EXPR:
10093 /* -1 rotated either direction by any amount is still -1. */
10094 if (integer_all_onesp (op0))
10095 return omit_one_operand (type, op0, op1);
10097 /* 0 rotated either direction by any amount is still zero. */
10098 if (integer_zerop (op0))
10099 return omit_one_operand (type, op0, op1);
10101 goto binary;
10103 case COMPLEX_EXPR:
10104 if (wins)
10105 return build_complex (type, op0, op1);
10106 return NULL_TREE;
10108 case LT_EXPR:
10109 case LE_EXPR:
10110 case GT_EXPR:
10111 case GE_EXPR:
10112 case EQ_EXPR:
10113 case NE_EXPR:
10114 /* If one arg is a real or integer constant, put it last. */
10115 if ((TREE_CODE (op0) == INTEGER_CST
10116 && TREE_CODE (op1) != INTEGER_CST)
10117 || (TREE_CODE (op0) == REAL_CST
10118 && TREE_CODE (op0) != REAL_CST))
10120 tree temp;
10122 temp = op0;
10123 op0 = op1;
10124 op1 = temp;
10125 code = swap_tree_comparison (code);
10128 /* Change X >= C to X > (C - 1) and X < C to X <= (C - 1) if C > 0.
10129 This transformation affects the cases which are handled in later
10130 optimizations involving comparisons with non-negative constants. */
10131 if (TREE_CODE (op1) == INTEGER_CST
10132 && TREE_CODE (op0) != INTEGER_CST
10133 && tree_int_cst_sgn (op1) > 0)
10135 switch (code)
10137 case GE_EXPR:
10138 code = GT_EXPR;
10139 op1 = const_binop (MINUS_EXPR, op1, integer_one_node, 0);
10140 break;
10142 case LT_EXPR:
10143 code = LE_EXPR;
10144 op1 = const_binop (MINUS_EXPR, op1, integer_one_node, 0);
10145 break;
10147 default:
10148 break;
10152 tem = fold_relational_hi_lo (&code, type, &op0, &op1);
10153 if (tem)
10154 return tem;
10156 /* Fall through. */
10158 case ORDERED_EXPR:
10159 case UNORDERED_EXPR:
10160 case UNLT_EXPR:
10161 case UNLE_EXPR:
10162 case UNGT_EXPR:
10163 case UNGE_EXPR:
10164 case UNEQ_EXPR:
10165 case LTGT_EXPR:
10166 if (!wins)
10167 return NULL_TREE;
10169 return fold_relational_const (code, type, op0, op1);
10171 case RANGE_EXPR:
10172 /* This could probably be handled. */
10173 return NULL_TREE;
10175 case TRUTH_AND_EXPR:
10176 /* If second arg is constant zero, result is zero, but first arg
10177 must be evaluated. */
10178 if (integer_zerop (op1))
10179 return omit_one_operand (type, op1, op0);
10180 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
10181 case will be handled here. */
10182 if (integer_zerop (op0))
10183 return omit_one_operand (type, op0, op1);
10184 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
10185 return constant_boolean_node (true, type);
10186 return NULL_TREE;
10188 case TRUTH_OR_EXPR:
10189 /* If second arg is constant true, result is true, but we must
10190 evaluate first arg. */
10191 if (TREE_CODE (op1) == INTEGER_CST && ! integer_zerop (op1))
10192 return omit_one_operand (type, op1, op0);
10193 /* Likewise for first arg, but note this only occurs here for
10194 TRUTH_OR_EXPR. */
10195 if (TREE_CODE (op0) == INTEGER_CST && ! integer_zerop (op0))
10196 return omit_one_operand (type, op0, op1);
10197 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
10198 return constant_boolean_node (false, type);
10199 return NULL_TREE;
10201 case TRUTH_XOR_EXPR:
10202 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
10204 int x = ! integer_zerop (op0) ^ ! integer_zerop (op1);
10205 return constant_boolean_node (x, type);
10207 return NULL_TREE;
10209 default:
10210 return NULL_TREE;
10214 /* Given the components of a unary expression CODE, TYPE and OP0,
10215 attempt to fold the expression to a constant without modifying
10216 TYPE or OP0.
10218 If the expression could be simplified to a constant, then return
10219 the constant. If the expression would not be simplified to a
10220 constant, then return NULL_TREE.
10222 Note this is primarily designed to be called after gimplification
10223 of the tree structures and when op0 is a constant. As a result
10224 of those simplifying assumptions this routine is far simpler than
10225 the generic fold routine. */
10227 tree
10228 nondestructive_fold_unary_to_constant (enum tree_code code, tree type,
10229 tree op0)
10231 /* Make sure we have a suitable constant argument. */
10232 if (code == NOP_EXPR || code == FLOAT_EXPR || code == CONVERT_EXPR)
10234 tree subop;
10236 if (TREE_CODE (op0) == COMPLEX_CST)
10237 subop = TREE_REALPART (op0);
10238 else
10239 subop = op0;
10241 if (TREE_CODE (subop) != INTEGER_CST && TREE_CODE (subop) != REAL_CST)
10242 return NULL_TREE;
10245 switch (code)
10247 case NOP_EXPR:
10248 case FLOAT_EXPR:
10249 case CONVERT_EXPR:
10250 case FIX_TRUNC_EXPR:
10251 case FIX_FLOOR_EXPR:
10252 case FIX_CEIL_EXPR:
10253 return fold_convert_const (code, type, op0);
10255 case NEGATE_EXPR:
10256 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST)
10257 return fold_negate_const (op0, type);
10258 else
10259 return NULL_TREE;
10261 case ABS_EXPR:
10262 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST)
10263 return fold_abs_const (op0, type);
10264 else
10265 return NULL_TREE;
10267 case BIT_NOT_EXPR:
10268 if (TREE_CODE (op0) == INTEGER_CST)
10269 return fold_not_const (op0, type);
10270 else
10271 return NULL_TREE;
10273 case REALPART_EXPR:
10274 if (TREE_CODE (op0) == COMPLEX_CST)
10275 return TREE_REALPART (op0);
10276 else
10277 return NULL_TREE;
10279 case IMAGPART_EXPR:
10280 if (TREE_CODE (op0) == COMPLEX_CST)
10281 return TREE_IMAGPART (op0);
10282 else
10283 return NULL_TREE;
10285 case CONJ_EXPR:
10286 if (TREE_CODE (op0) == COMPLEX_CST
10287 && TREE_CODE (TREE_TYPE (op0)) == COMPLEX_TYPE)
10288 return build_complex (type, TREE_REALPART (op0),
10289 negate_expr (TREE_IMAGPART (op0)));
10290 return NULL_TREE;
10292 default:
10293 return NULL_TREE;
10297 /* If EXP represents referencing an element in a constant string
10298 (either via pointer arithmetic or array indexing), return the
10299 tree representing the value accessed, otherwise return NULL. */
10301 tree
10302 fold_read_from_constant_string (tree exp)
10304 if (TREE_CODE (exp) == INDIRECT_REF || TREE_CODE (exp) == ARRAY_REF)
10306 tree exp1 = TREE_OPERAND (exp, 0);
10307 tree index;
10308 tree string;
10310 if (TREE_CODE (exp) == INDIRECT_REF)
10311 string = string_constant (exp1, &index);
10312 else
10314 tree low_bound = array_ref_low_bound (exp);
10315 index = fold_convert (sizetype, TREE_OPERAND (exp, 1));
10317 /* Optimize the special-case of a zero lower bound.
10319 We convert the low_bound to sizetype to avoid some problems
10320 with constant folding. (E.g. suppose the lower bound is 1,
10321 and its mode is QI. Without the conversion,l (ARRAY
10322 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
10323 +INDEX), which becomes (ARRAY+255+INDEX). Opps!) */
10324 if (! integer_zerop (low_bound))
10325 index = size_diffop (index, fold_convert (sizetype, low_bound));
10327 string = exp1;
10330 if (string
10331 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (string))
10332 && TREE_CODE (string) == STRING_CST
10333 && TREE_CODE (index) == INTEGER_CST
10334 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
10335 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
10336 == MODE_INT)
10337 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
10338 return fold_convert (TREE_TYPE (exp),
10339 build_int_2 ((TREE_STRING_POINTER (string)
10340 [TREE_INT_CST_LOW (index)]), 0));
10342 return NULL;
10345 /* Return the tree for neg (ARG0) when ARG0 is known to be either
10346 an integer constant or real constant.
10348 TYPE is the type of the result. */
10350 static tree
10351 fold_negate_const (tree arg0, tree type)
10353 tree t = NULL_TREE;
10355 if (TREE_CODE (arg0) == INTEGER_CST)
10357 unsigned HOST_WIDE_INT low;
10358 HOST_WIDE_INT high;
10359 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
10360 TREE_INT_CST_HIGH (arg0),
10361 &low, &high);
10362 t = build_int_2 (low, high);
10363 TREE_TYPE (t) = type;
10364 t = force_fit_type (t, 1,
10365 (overflow | TREE_OVERFLOW (arg0))
10366 && !TYPE_UNSIGNED (type),
10367 TREE_CONSTANT_OVERFLOW (arg0));
10369 else if (TREE_CODE (arg0) == REAL_CST)
10370 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
10371 #ifdef ENABLE_CHECKING
10372 else
10373 abort ();
10374 #endif
10376 return t;
10379 /* Return the tree for abs (ARG0) when ARG0 is known to be either
10380 an integer constant or real constant.
10382 TYPE is the type of the result. */
10384 tree
10385 fold_abs_const (tree arg0, tree type)
10387 tree t = NULL_TREE;
10389 if (TREE_CODE (arg0) == INTEGER_CST)
10391 /* If the value is unsigned, then the absolute value is
10392 the same as the ordinary value. */
10393 if (TYPE_UNSIGNED (type))
10394 return arg0;
10395 /* Similarly, if the value is non-negative. */
10396 else if (INT_CST_LT (integer_minus_one_node, arg0))
10397 return arg0;
10398 /* If the value is negative, then the absolute value is
10399 its negation. */
10400 else
10402 unsigned HOST_WIDE_INT low;
10403 HOST_WIDE_INT high;
10404 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
10405 TREE_INT_CST_HIGH (arg0),
10406 &low, &high);
10407 t = build_int_2 (low, high);
10408 TREE_TYPE (t) = type;
10409 t = force_fit_type (t, -1, overflow | TREE_OVERFLOW (arg0),
10410 TREE_CONSTANT_OVERFLOW (arg0));
10411 return t;
10414 else if (TREE_CODE (arg0) == REAL_CST)
10416 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
10417 return build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
10418 else
10419 return arg0;
10421 #ifdef ENABLE_CHECKING
10422 else
10423 abort ();
10424 #endif
10426 return t;
10429 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
10430 constant. TYPE is the type of the result. */
10432 static tree
10433 fold_not_const (tree arg0, tree type)
10435 tree t = NULL_TREE;
10437 if (TREE_CODE (arg0) == INTEGER_CST)
10439 t = build_int_2 (~ TREE_INT_CST_LOW (arg0),
10440 ~ TREE_INT_CST_HIGH (arg0));
10441 TREE_TYPE (t) = type;
10442 t = force_fit_type (t, 0, TREE_OVERFLOW (arg0),
10443 TREE_CONSTANT_OVERFLOW (arg0));
10445 #ifdef ENABLE_CHECKING
10446 else
10447 abort ();
10448 #endif
10450 return t;
10453 /* Given CODE, a relational operator, the target type, TYPE and two
10454 constant operands OP0 and OP1, return the result of the
10455 relational operation. If the result is not a compile time
10456 constant, then return NULL_TREE. */
10458 static tree
10459 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
10461 int result, invert;
10463 /* From here on, the only cases we handle are when the result is
10464 known to be a constant. */
10466 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
10468 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
10469 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
10471 /* Handle the cases where either operand is a NaN. */
10472 if (real_isnan (c0) || real_isnan (c1))
10474 switch (code)
10476 case EQ_EXPR:
10477 case ORDERED_EXPR:
10478 result = 0;
10479 break;
10481 case NE_EXPR:
10482 case UNORDERED_EXPR:
10483 case UNLT_EXPR:
10484 case UNLE_EXPR:
10485 case UNGT_EXPR:
10486 case UNGE_EXPR:
10487 case UNEQ_EXPR:
10488 result = 1;
10489 break;
10491 case LT_EXPR:
10492 case LE_EXPR:
10493 case GT_EXPR:
10494 case GE_EXPR:
10495 case LTGT_EXPR:
10496 if (flag_trapping_math)
10497 return NULL_TREE;
10498 result = 0;
10499 break;
10501 default:
10502 abort ();
10505 return constant_boolean_node (result, type);
10508 return constant_boolean_node (real_compare (code, c0, c1), type);
10511 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
10513 To compute GT, swap the arguments and do LT.
10514 To compute GE, do LT and invert the result.
10515 To compute LE, swap the arguments, do LT and invert the result.
10516 To compute NE, do EQ and invert the result.
10518 Therefore, the code below must handle only EQ and LT. */
10520 if (code == LE_EXPR || code == GT_EXPR)
10522 tree tem = op0;
10523 op0 = op1;
10524 op1 = tem;
10525 code = swap_tree_comparison (code);
10528 /* Note that it is safe to invert for real values here because we
10529 have already handled the one case that it matters. */
10531 invert = 0;
10532 if (code == NE_EXPR || code == GE_EXPR)
10534 invert = 1;
10535 code = invert_tree_comparison (code, false);
10538 /* Compute a result for LT or EQ if args permit;
10539 Otherwise return T. */
10540 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
10542 if (code == EQ_EXPR)
10543 result = tree_int_cst_equal (op0, op1);
10544 else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
10545 result = INT_CST_LT_UNSIGNED (op0, op1);
10546 else
10547 result = INT_CST_LT (op0, op1);
10549 else
10550 return NULL_TREE;
10552 if (invert)
10553 result ^= 1;
10554 return constant_boolean_node (result, type);
10557 /* Build an expression for the address of T. Folds away INDIRECT_REF to
10558 avoid confusing the gimplify process. */
10560 tree
10561 build_fold_addr_expr_with_type (tree t, tree ptrtype)
10563 if (TREE_CODE (t) == INDIRECT_REF)
10565 t = TREE_OPERAND (t, 0);
10566 if (TREE_TYPE (t) != ptrtype)
10567 t = build1 (NOP_EXPR, ptrtype, t);
10569 else
10571 tree base = t;
10573 while (handled_component_p (base)
10574 || TREE_CODE (base) == REALPART_EXPR
10575 || TREE_CODE (base) == IMAGPART_EXPR)
10576 base = TREE_OPERAND (base, 0);
10577 if (DECL_P (base))
10578 TREE_ADDRESSABLE (base) = 1;
10580 t = build1 (ADDR_EXPR, ptrtype, t);
10583 return t;
10586 tree
10587 build_fold_addr_expr (tree t)
10589 return build_fold_addr_expr_with_type (t, build_pointer_type (TREE_TYPE (t)));
10592 /* Builds an expression for an indirection through T, simplifying some
10593 cases. */
10595 tree
10596 build_fold_indirect_ref (tree t)
10598 tree type = TREE_TYPE (TREE_TYPE (t));
10599 tree sub = t;
10600 tree subtype;
10602 STRIP_NOPS (sub);
10603 if (TREE_CODE (sub) == ADDR_EXPR)
10605 tree op = TREE_OPERAND (sub, 0);
10606 tree optype = TREE_TYPE (op);
10607 /* *&p => p */
10608 if (lang_hooks.types_compatible_p (type, optype))
10609 return op;
10610 /* *(foo *)&fooarray => fooarray[0] */
10611 else if (TREE_CODE (optype) == ARRAY_TYPE
10612 && lang_hooks.types_compatible_p (type, TREE_TYPE (optype)))
10613 return build4 (ARRAY_REF, type, op, size_zero_node, NULL_TREE, NULL_TREE);
10616 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
10617 subtype = TREE_TYPE (sub);
10618 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
10619 && lang_hooks.types_compatible_p (type, TREE_TYPE (TREE_TYPE (subtype))))
10621 sub = build_fold_indirect_ref (sub);
10622 return build4 (ARRAY_REF, type, sub, size_zero_node, NULL_TREE, NULL_TREE);
10625 return build1 (INDIRECT_REF, type, t);
10628 /* Strip non-trapping, non-side-effecting tree nodes from an expression
10629 whose result is ignored. The type of the returned tree need not be
10630 the same as the original expression. */
10632 tree
10633 fold_ignored_result (tree t)
10635 if (!TREE_SIDE_EFFECTS (t))
10636 return integer_zero_node;
10638 for (;;)
10639 switch (TREE_CODE_CLASS (TREE_CODE (t)))
10641 case '1':
10642 t = TREE_OPERAND (t, 0);
10643 break;
10645 case '2':
10646 case '<':
10647 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
10648 t = TREE_OPERAND (t, 0);
10649 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
10650 t = TREE_OPERAND (t, 1);
10651 else
10652 return t;
10653 break;
10655 case 'e':
10656 switch (TREE_CODE (t))
10658 case COMPOUND_EXPR:
10659 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
10660 return t;
10661 t = TREE_OPERAND (t, 0);
10662 break;
10664 case COND_EXPR:
10665 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
10666 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
10667 return t;
10668 t = TREE_OPERAND (t, 0);
10669 break;
10671 default:
10672 return t;
10674 break;
10676 default:
10677 return t;
10681 #include "gt-fold-const.h"