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, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
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_double.
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_double takes a constant, an overflowable flag and a
43 prior overflow indicator. It forces the value to fit the type and
46 Note: Since the folders get called on non-gimple code as well as
47 gimple code, we need to handle GIMPLE tuples as well as their
48 corresponding tree equivalents. */
52 #include "coretypes.h"
57 #include "fixed-value.h"
66 #include "langhooks.h"
70 /* Nonzero if we are folding constants inside an initializer; zero
72 int folding_initializer = 0;
74 /* The following constants represent a bit based encoding of GCC's
75 comparison operators. This encoding simplifies transformations
76 on relational comparison operators, such as AND and OR. */
77 enum comparison_code {
96 static void encode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT, HOST_WIDE_INT);
97 static void decode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT *, HOST_WIDE_INT *);
98 static bool negate_mathfn_p (enum built_in_function);
99 static bool negate_expr_p (tree);
100 static tree negate_expr (tree);
101 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
102 static tree associate_trees (tree, tree, enum tree_code, tree);
103 static tree const_binop (enum tree_code, tree, tree, int);
104 static enum comparison_code comparison_to_compcode (enum tree_code);
105 static enum tree_code compcode_to_comparison (enum comparison_code);
106 static tree combine_comparisons (enum tree_code, enum tree_code,
107 enum tree_code, tree, tree, tree);
108 static int operand_equal_for_comparison_p (tree, tree, tree);
109 static int twoval_comparison_p (tree, tree *, tree *, int *);
110 static tree eval_subst (tree, tree, tree, tree, tree);
111 static tree pedantic_omit_one_operand (tree, tree, tree);
112 static tree distribute_bit_expr (enum tree_code, tree, tree, tree);
113 static tree make_bit_field_ref (tree, tree, HOST_WIDE_INT, HOST_WIDE_INT, int);
114 static tree optimize_bit_field_compare (enum tree_code, tree, tree, tree);
115 static tree decode_field_reference (tree, HOST_WIDE_INT *, HOST_WIDE_INT *,
116 enum machine_mode *, int *, int *,
118 static int all_ones_mask_p (const_tree, int);
119 static tree sign_bit_p (tree, const_tree);
120 static int simple_operand_p (const_tree);
121 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
122 static tree range_predecessor (tree);
123 static tree range_successor (tree);
124 static tree make_range (tree, int *, tree *, tree *, bool *);
125 static tree build_range_check (tree, tree, int, tree, tree);
126 static int merge_ranges (int *, tree *, tree *, int, tree, tree, int, tree,
128 static tree fold_range_test (enum tree_code, tree, tree, tree);
129 static tree fold_cond_expr_with_comparison (tree, tree, tree, tree);
130 static tree unextend (tree, int, int, tree);
131 static tree fold_truthop (enum tree_code, tree, tree, tree);
132 static tree optimize_minmax_comparison (enum tree_code, tree, tree, tree);
133 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
134 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
135 static tree fold_binary_op_with_conditional_arg (enum tree_code, tree,
138 static tree fold_mathfn_compare (enum built_in_function, enum tree_code,
140 static tree fold_inf_compare (enum tree_code, tree, tree, tree);
141 static tree fold_div_compare (enum tree_code, tree, tree, tree);
142 static bool reorder_operands_p (const_tree, const_tree);
143 static tree fold_negate_const (tree, tree);
144 static tree fold_not_const (tree, tree);
145 static tree fold_relational_const (enum tree_code, tree, tree, tree);
148 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
149 overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
150 and SUM1. Then this yields nonzero if overflow occurred during the
153 Overflow occurs if A and B have the same sign, but A and SUM differ in
154 sign. Use `^' to test whether signs differ, and `< 0' to isolate the
156 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
158 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
159 We do that by representing the two-word integer in 4 words, with only
160 HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
161 number. The value of the word is LOWPART + HIGHPART * BASE. */
164 ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
165 #define HIGHPART(x) \
166 ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
167 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
169 /* Unpack a two-word integer into 4 words.
170 LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
171 WORDS points to the array of HOST_WIDE_INTs. */
174 encode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
176 words[0] = LOWPART (low);
177 words[1] = HIGHPART (low);
178 words[2] = LOWPART (hi);
179 words[3] = HIGHPART (hi);
182 /* Pack an array of 4 words into a two-word integer.
183 WORDS points to the array of words.
184 The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */
187 decode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT *low,
190 *low = words[0] + words[1] * BASE;
191 *hi = words[2] + words[3] * BASE;
194 /* Force the double-word integer L1, H1 to be within the range of the
195 integer type TYPE. Stores the properly truncated and sign-extended
196 double-word integer in *LV, *HV. Returns true if the operation
197 overflows, that is, argument and result are different. */
200 fit_double_type (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
201 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, const_tree type)
203 unsigned HOST_WIDE_INT low0 = l1;
204 HOST_WIDE_INT high0 = h1;
206 int sign_extended_type;
208 if (POINTER_TYPE_P (type)
209 || TREE_CODE (type) == OFFSET_TYPE)
212 prec = TYPE_PRECISION (type);
214 /* Size types *are* sign extended. */
215 sign_extended_type = (!TYPE_UNSIGNED (type)
216 || (TREE_CODE (type) == INTEGER_TYPE
217 && TYPE_IS_SIZETYPE (type)));
219 /* First clear all bits that are beyond the type's precision. */
220 if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
222 else if (prec > HOST_BITS_PER_WIDE_INT)
223 h1 &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
227 if (prec < HOST_BITS_PER_WIDE_INT)
228 l1 &= ~((HOST_WIDE_INT) (-1) << prec);
231 /* Then do sign extension if necessary. */
232 if (!sign_extended_type)
233 /* No sign extension */;
234 else if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
235 /* Correct width already. */;
236 else if (prec > HOST_BITS_PER_WIDE_INT)
238 /* Sign extend top half? */
239 if (h1 & ((unsigned HOST_WIDE_INT)1
240 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
241 h1 |= (HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT);
243 else if (prec == HOST_BITS_PER_WIDE_INT)
245 if ((HOST_WIDE_INT)l1 < 0)
250 /* Sign extend bottom half? */
251 if (l1 & ((unsigned HOST_WIDE_INT)1 << (prec - 1)))
254 l1 |= (HOST_WIDE_INT)(-1) << prec;
261 /* If the value didn't fit, signal overflow. */
262 return l1 != low0 || h1 != high0;
265 /* We force the double-int HIGH:LOW to the range of the type TYPE by
266 sign or zero extending it.
267 OVERFLOWABLE indicates if we are interested
268 in overflow of the value, when >0 we are only interested in signed
269 overflow, for <0 we are interested in any overflow. OVERFLOWED
270 indicates whether overflow has already occurred. CONST_OVERFLOWED
271 indicates whether constant overflow has already occurred. We force
272 T's value to be within range of T's type (by setting to 0 or 1 all
273 the bits outside the type's range). We set TREE_OVERFLOWED if,
274 OVERFLOWED is nonzero,
275 or OVERFLOWABLE is >0 and signed overflow occurs
276 or OVERFLOWABLE is <0 and any overflow occurs
277 We return a new tree node for the extended double-int. The node
278 is shared if no overflow flags are set. */
281 force_fit_type_double (tree type, unsigned HOST_WIDE_INT low,
282 HOST_WIDE_INT high, int overflowable,
285 int sign_extended_type;
288 /* Size types *are* sign extended. */
289 sign_extended_type = (!TYPE_UNSIGNED (type)
290 || (TREE_CODE (type) == INTEGER_TYPE
291 && TYPE_IS_SIZETYPE (type)));
293 overflow = fit_double_type (low, high, &low, &high, type);
295 /* If we need to set overflow flags, return a new unshared node. */
296 if (overflowed || overflow)
300 || (overflowable > 0 && sign_extended_type))
302 tree t = make_node (INTEGER_CST);
303 TREE_INT_CST_LOW (t) = low;
304 TREE_INT_CST_HIGH (t) = high;
305 TREE_TYPE (t) = type;
306 TREE_OVERFLOW (t) = 1;
311 /* Else build a shared node. */
312 return build_int_cst_wide (type, low, high);
315 /* Add two doubleword integers with doubleword result.
316 Return nonzero if the operation overflows according to UNSIGNED_P.
317 Each argument is given as two `HOST_WIDE_INT' pieces.
318 One argument is L1 and H1; the other, L2 and H2.
319 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
322 add_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
323 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
324 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
327 unsigned HOST_WIDE_INT l;
331 h = h1 + h2 + (l < l1);
337 return (unsigned HOST_WIDE_INT) h < (unsigned HOST_WIDE_INT) h1;
339 return OVERFLOW_SUM_SIGN (h1, h2, h);
342 /* Negate a doubleword integer with doubleword result.
343 Return nonzero if the operation overflows, assuming it's signed.
344 The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
345 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
348 neg_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
349 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
355 return (*hv & h1) < 0;
365 /* Multiply two doubleword integers with doubleword result.
366 Return nonzero if the operation overflows according to UNSIGNED_P.
367 Each argument is given as two `HOST_WIDE_INT' pieces.
368 One argument is L1 and H1; the other, L2 and H2.
369 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
372 mul_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
373 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
374 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
377 HOST_WIDE_INT arg1[4];
378 HOST_WIDE_INT arg2[4];
379 HOST_WIDE_INT prod[4 * 2];
380 unsigned HOST_WIDE_INT carry;
382 unsigned HOST_WIDE_INT toplow, neglow;
383 HOST_WIDE_INT tophigh, neghigh;
385 encode (arg1, l1, h1);
386 encode (arg2, l2, h2);
388 memset (prod, 0, sizeof prod);
390 for (i = 0; i < 4; i++)
393 for (j = 0; j < 4; j++)
396 /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */
397 carry += arg1[i] * arg2[j];
398 /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */
400 prod[k] = LOWPART (carry);
401 carry = HIGHPART (carry);
406 decode (prod, lv, hv);
407 decode (prod + 4, &toplow, &tophigh);
409 /* Unsigned overflow is immediate. */
411 return (toplow | tophigh) != 0;
413 /* Check for signed overflow by calculating the signed representation of the
414 top half of the result; it should agree with the low half's sign bit. */
417 neg_double (l2, h2, &neglow, &neghigh);
418 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
422 neg_double (l1, h1, &neglow, &neghigh);
423 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
425 return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
428 /* Shift the doubleword integer in L1, H1 left by COUNT places
429 keeping only PREC bits of result.
430 Shift right if COUNT is negative.
431 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
432 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
435 lshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
436 HOST_WIDE_INT count, unsigned int prec,
437 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, int arith)
439 unsigned HOST_WIDE_INT signmask;
443 rshift_double (l1, h1, -count, prec, lv, hv, arith);
447 if (SHIFT_COUNT_TRUNCATED)
450 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
452 /* Shifting by the host word size is undefined according to the
453 ANSI standard, so we must handle this as a special case. */
457 else if (count >= HOST_BITS_PER_WIDE_INT)
459 *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
464 *hv = (((unsigned HOST_WIDE_INT) h1 << count)
465 | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
469 /* Sign extend all bits that are beyond the precision. */
471 signmask = -((prec > HOST_BITS_PER_WIDE_INT
472 ? ((unsigned HOST_WIDE_INT) *hv
473 >> (prec - HOST_BITS_PER_WIDE_INT - 1))
474 : (*lv >> (prec - 1))) & 1);
476 if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
478 else if (prec >= HOST_BITS_PER_WIDE_INT)
480 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
481 *hv |= signmask << (prec - HOST_BITS_PER_WIDE_INT);
486 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
487 *lv |= signmask << prec;
491 /* Shift the doubleword integer in L1, H1 right by COUNT places
492 keeping only PREC bits of result. COUNT must be positive.
493 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
494 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
497 rshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
498 HOST_WIDE_INT count, unsigned int prec,
499 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
502 unsigned HOST_WIDE_INT signmask;
505 ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
508 if (SHIFT_COUNT_TRUNCATED)
511 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
513 /* Shifting by the host word size is undefined according to the
514 ANSI standard, so we must handle this as a special case. */
518 else if (count >= HOST_BITS_PER_WIDE_INT)
521 *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
525 *hv = (unsigned HOST_WIDE_INT) h1 >> count;
527 | ((unsigned HOST_WIDE_INT) h1 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
530 /* Zero / sign extend all bits that are beyond the precision. */
532 if (count >= (HOST_WIDE_INT)prec)
537 else if ((prec - count) >= 2 * HOST_BITS_PER_WIDE_INT)
539 else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
541 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - count - HOST_BITS_PER_WIDE_INT));
542 *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
547 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << (prec - count));
548 *lv |= signmask << (prec - count);
552 /* Rotate the doubleword integer in L1, H1 left by COUNT places
553 keeping only PREC bits of result.
554 Rotate right if COUNT is negative.
555 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
558 lrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
559 HOST_WIDE_INT count, unsigned int prec,
560 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
562 unsigned HOST_WIDE_INT s1l, s2l;
563 HOST_WIDE_INT s1h, s2h;
569 lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
570 rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
575 /* Rotate the doubleword integer in L1, H1 left by COUNT places
576 keeping only PREC bits of result. COUNT must be positive.
577 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
580 rrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
581 HOST_WIDE_INT count, unsigned int prec,
582 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
584 unsigned HOST_WIDE_INT s1l, s2l;
585 HOST_WIDE_INT s1h, s2h;
591 rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
592 lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
597 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
598 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
599 CODE is a tree code for a kind of division, one of
600 TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
602 It controls how the quotient is rounded to an integer.
603 Return nonzero if the operation overflows.
604 UNS nonzero says do unsigned division. */
607 div_and_round_double (enum tree_code code, int uns,
608 unsigned HOST_WIDE_INT lnum_orig, /* num == numerator == dividend */
609 HOST_WIDE_INT hnum_orig,
610 unsigned HOST_WIDE_INT lden_orig, /* den == denominator == divisor */
611 HOST_WIDE_INT hden_orig,
612 unsigned HOST_WIDE_INT *lquo,
613 HOST_WIDE_INT *hquo, unsigned HOST_WIDE_INT *lrem,
617 HOST_WIDE_INT num[4 + 1]; /* extra element for scaling. */
618 HOST_WIDE_INT den[4], quo[4];
620 unsigned HOST_WIDE_INT work;
621 unsigned HOST_WIDE_INT carry = 0;
622 unsigned HOST_WIDE_INT lnum = lnum_orig;
623 HOST_WIDE_INT hnum = hnum_orig;
624 unsigned HOST_WIDE_INT lden = lden_orig;
625 HOST_WIDE_INT hden = hden_orig;
628 if (hden == 0 && lden == 0)
629 overflow = 1, lden = 1;
631 /* Calculate quotient sign and convert operands to unsigned. */
637 /* (minimum integer) / (-1) is the only overflow case. */
638 if (neg_double (lnum, hnum, &lnum, &hnum)
639 && ((HOST_WIDE_INT) lden & hden) == -1)
645 neg_double (lden, hden, &lden, &hden);
649 if (hnum == 0 && hden == 0)
650 { /* single precision */
652 /* This unsigned division rounds toward zero. */
658 { /* trivial case: dividend < divisor */
659 /* hden != 0 already checked. */
666 memset (quo, 0, sizeof quo);
668 memset (num, 0, sizeof num); /* to zero 9th element */
669 memset (den, 0, sizeof den);
671 encode (num, lnum, hnum);
672 encode (den, lden, hden);
674 /* Special code for when the divisor < BASE. */
675 if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
677 /* hnum != 0 already checked. */
678 for (i = 4 - 1; i >= 0; i--)
680 work = num[i] + carry * BASE;
681 quo[i] = work / lden;
687 /* Full double precision division,
688 with thanks to Don Knuth's "Seminumerical Algorithms". */
689 int num_hi_sig, den_hi_sig;
690 unsigned HOST_WIDE_INT quo_est, scale;
692 /* Find the highest nonzero divisor digit. */
693 for (i = 4 - 1;; i--)
700 /* Insure that the first digit of the divisor is at least BASE/2.
701 This is required by the quotient digit estimation algorithm. */
703 scale = BASE / (den[den_hi_sig] + 1);
705 { /* scale divisor and dividend */
707 for (i = 0; i <= 4 - 1; i++)
709 work = (num[i] * scale) + carry;
710 num[i] = LOWPART (work);
711 carry = HIGHPART (work);
716 for (i = 0; i <= 4 - 1; i++)
718 work = (den[i] * scale) + carry;
719 den[i] = LOWPART (work);
720 carry = HIGHPART (work);
721 if (den[i] != 0) den_hi_sig = i;
728 for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
730 /* Guess the next quotient digit, quo_est, by dividing the first
731 two remaining dividend digits by the high order quotient digit.
732 quo_est is never low and is at most 2 high. */
733 unsigned HOST_WIDE_INT tmp;
735 num_hi_sig = i + den_hi_sig + 1;
736 work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
737 if (num[num_hi_sig] != den[den_hi_sig])
738 quo_est = work / den[den_hi_sig];
742 /* Refine quo_est so it's usually correct, and at most one high. */
743 tmp = work - quo_est * den[den_hi_sig];
745 && (den[den_hi_sig - 1] * quo_est
746 > (tmp * BASE + num[num_hi_sig - 2])))
749 /* Try QUO_EST as the quotient digit, by multiplying the
750 divisor by QUO_EST and subtracting from the remaining dividend.
751 Keep in mind that QUO_EST is the I - 1st digit. */
754 for (j = 0; j <= den_hi_sig; j++)
756 work = quo_est * den[j] + carry;
757 carry = HIGHPART (work);
758 work = num[i + j] - LOWPART (work);
759 num[i + j] = LOWPART (work);
760 carry += HIGHPART (work) != 0;
763 /* If quo_est was high by one, then num[i] went negative and
764 we need to correct things. */
765 if (num[num_hi_sig] < (HOST_WIDE_INT) carry)
768 carry = 0; /* add divisor back in */
769 for (j = 0; j <= den_hi_sig; j++)
771 work = num[i + j] + den[j] + carry;
772 carry = HIGHPART (work);
773 num[i + j] = LOWPART (work);
776 num [num_hi_sig] += carry;
779 /* Store the quotient digit. */
784 decode (quo, lquo, hquo);
787 /* If result is negative, make it so. */
789 neg_double (*lquo, *hquo, lquo, hquo);
791 /* Compute trial remainder: rem = num - (quo * den) */
792 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
793 neg_double (*lrem, *hrem, lrem, hrem);
794 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
799 case TRUNC_MOD_EXPR: /* round toward zero */
800 case EXACT_DIV_EXPR: /* for this one, it shouldn't matter */
804 case FLOOR_MOD_EXPR: /* round toward negative infinity */
805 if (quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio < 0 && rem != 0 */
808 add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1,
816 case CEIL_MOD_EXPR: /* round toward positive infinity */
817 if (!quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio > 0 && rem != 0 */
819 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
827 case ROUND_MOD_EXPR: /* round to closest integer */
829 unsigned HOST_WIDE_INT labs_rem = *lrem;
830 HOST_WIDE_INT habs_rem = *hrem;
831 unsigned HOST_WIDE_INT labs_den = lden, ltwice;
832 HOST_WIDE_INT habs_den = hden, htwice;
834 /* Get absolute values. */
836 neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
838 neg_double (lden, hden, &labs_den, &habs_den);
840 /* If (2 * abs (lrem) >= abs (lden)), adjust the quotient. */
841 mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
842 labs_rem, habs_rem, <wice, &htwice);
844 if (((unsigned HOST_WIDE_INT) habs_den
845 < (unsigned HOST_WIDE_INT) htwice)
846 || (((unsigned HOST_WIDE_INT) habs_den
847 == (unsigned HOST_WIDE_INT) htwice)
848 && (labs_den <= ltwice)))
852 add_double (*lquo, *hquo,
853 (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
856 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
868 /* Compute true remainder: rem = num - (quo * den) */
869 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
870 neg_double (*lrem, *hrem, lrem, hrem);
871 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
875 /* If ARG2 divides ARG1 with zero remainder, carries out the division
876 of type CODE and returns the quotient.
877 Otherwise returns NULL_TREE. */
880 div_if_zero_remainder (enum tree_code code, const_tree arg1, const_tree arg2)
882 unsigned HOST_WIDE_INT int1l, int2l;
883 HOST_WIDE_INT int1h, int2h;
884 unsigned HOST_WIDE_INT quol, reml;
885 HOST_WIDE_INT quoh, remh;
886 tree type = TREE_TYPE (arg1);
887 int uns = TYPE_UNSIGNED (type);
889 int1l = TREE_INT_CST_LOW (arg1);
890 int1h = TREE_INT_CST_HIGH (arg1);
891 /* &obj[0] + -128 really should be compiled as &obj[-8] rather than
892 &obj[some_exotic_number]. */
893 if (POINTER_TYPE_P (type))
896 type = signed_type_for (type);
897 fit_double_type (int1l, int1h, &int1l, &int1h,
901 fit_double_type (int1l, int1h, &int1l, &int1h, type);
902 int2l = TREE_INT_CST_LOW (arg2);
903 int2h = TREE_INT_CST_HIGH (arg2);
905 div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
906 &quol, &quoh, &reml, &remh);
907 if (remh != 0 || reml != 0)
910 return build_int_cst_wide (type, quol, quoh);
913 /* This is nonzero if we should defer warnings about undefined
914 overflow. This facility exists because these warnings are a
915 special case. The code to estimate loop iterations does not want
916 to issue any warnings, since it works with expressions which do not
917 occur in user code. Various bits of cleanup code call fold(), but
918 only use the result if it has certain characteristics (e.g., is a
919 constant); that code only wants to issue a warning if the result is
922 static int fold_deferring_overflow_warnings;
924 /* If a warning about undefined overflow is deferred, this is the
925 warning. Note that this may cause us to turn two warnings into
926 one, but that is fine since it is sufficient to only give one
927 warning per expression. */
929 static const char* fold_deferred_overflow_warning;
931 /* If a warning about undefined overflow is deferred, this is the
932 level at which the warning should be emitted. */
934 static enum warn_strict_overflow_code fold_deferred_overflow_code;
936 /* Start deferring overflow warnings. We could use a stack here to
937 permit nested calls, but at present it is not necessary. */
940 fold_defer_overflow_warnings (void)
942 ++fold_deferring_overflow_warnings;
945 /* Stop deferring overflow warnings. If there is a pending warning,
946 and ISSUE is true, then issue the warning if appropriate. STMT is
947 the statement with which the warning should be associated (used for
948 location information); STMT may be NULL. CODE is the level of the
949 warning--a warn_strict_overflow_code value. This function will use
950 the smaller of CODE and the deferred code when deciding whether to
951 issue the warning. CODE may be zero to mean to always use the
955 fold_undefer_overflow_warnings (bool issue, const_gimple stmt, int code)
960 gcc_assert (fold_deferring_overflow_warnings > 0);
961 --fold_deferring_overflow_warnings;
962 if (fold_deferring_overflow_warnings > 0)
964 if (fold_deferred_overflow_warning != NULL
966 && code < (int) fold_deferred_overflow_code)
967 fold_deferred_overflow_code = code;
971 warnmsg = fold_deferred_overflow_warning;
972 fold_deferred_overflow_warning = NULL;
974 if (!issue || warnmsg == NULL)
977 if (gimple_no_warning_p (stmt))
980 /* Use the smallest code level when deciding to issue the
982 if (code == 0 || code > (int) fold_deferred_overflow_code)
983 code = fold_deferred_overflow_code;
985 if (!issue_strict_overflow_warning (code))
989 locus = input_location;
991 locus = gimple_location (stmt);
992 warning (OPT_Wstrict_overflow, "%H%s", &locus, warnmsg);
995 /* Stop deferring overflow warnings, ignoring any deferred
999 fold_undefer_and_ignore_overflow_warnings (void)
1001 fold_undefer_overflow_warnings (false, NULL, 0);
1004 /* Whether we are deferring overflow warnings. */
1007 fold_deferring_overflow_warnings_p (void)
1009 return fold_deferring_overflow_warnings > 0;
1012 /* This is called when we fold something based on the fact that signed
1013 overflow is undefined. */
1016 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
1018 if (fold_deferring_overflow_warnings > 0)
1020 if (fold_deferred_overflow_warning == NULL
1021 || wc < fold_deferred_overflow_code)
1023 fold_deferred_overflow_warning = gmsgid;
1024 fold_deferred_overflow_code = wc;
1027 else if (issue_strict_overflow_warning (wc))
1028 warning (OPT_Wstrict_overflow, gmsgid);
1031 /* Return true if the built-in mathematical function specified by CODE
1032 is odd, i.e. -f(x) == f(-x). */
1035 negate_mathfn_p (enum built_in_function code)
1039 CASE_FLT_FN (BUILT_IN_ASIN):
1040 CASE_FLT_FN (BUILT_IN_ASINH):
1041 CASE_FLT_FN (BUILT_IN_ATAN):
1042 CASE_FLT_FN (BUILT_IN_ATANH):
1043 CASE_FLT_FN (BUILT_IN_CASIN):
1044 CASE_FLT_FN (BUILT_IN_CASINH):
1045 CASE_FLT_FN (BUILT_IN_CATAN):
1046 CASE_FLT_FN (BUILT_IN_CATANH):
1047 CASE_FLT_FN (BUILT_IN_CBRT):
1048 CASE_FLT_FN (BUILT_IN_CPROJ):
1049 CASE_FLT_FN (BUILT_IN_CSIN):
1050 CASE_FLT_FN (BUILT_IN_CSINH):
1051 CASE_FLT_FN (BUILT_IN_CTAN):
1052 CASE_FLT_FN (BUILT_IN_CTANH):
1053 CASE_FLT_FN (BUILT_IN_ERF):
1054 CASE_FLT_FN (BUILT_IN_LLROUND):
1055 CASE_FLT_FN (BUILT_IN_LROUND):
1056 CASE_FLT_FN (BUILT_IN_ROUND):
1057 CASE_FLT_FN (BUILT_IN_SIN):
1058 CASE_FLT_FN (BUILT_IN_SINH):
1059 CASE_FLT_FN (BUILT_IN_TAN):
1060 CASE_FLT_FN (BUILT_IN_TANH):
1061 CASE_FLT_FN (BUILT_IN_TRUNC):
1064 CASE_FLT_FN (BUILT_IN_LLRINT):
1065 CASE_FLT_FN (BUILT_IN_LRINT):
1066 CASE_FLT_FN (BUILT_IN_NEARBYINT):
1067 CASE_FLT_FN (BUILT_IN_RINT):
1068 return !flag_rounding_math;
1076 /* Check whether we may negate an integer constant T without causing
1080 may_negate_without_overflow_p (const_tree t)
1082 unsigned HOST_WIDE_INT val;
1086 gcc_assert (TREE_CODE (t) == INTEGER_CST);
1088 type = TREE_TYPE (t);
1089 if (TYPE_UNSIGNED (type))
1092 prec = TYPE_PRECISION (type);
1093 if (prec > HOST_BITS_PER_WIDE_INT)
1095 if (TREE_INT_CST_LOW (t) != 0)
1097 prec -= HOST_BITS_PER_WIDE_INT;
1098 val = TREE_INT_CST_HIGH (t);
1101 val = TREE_INT_CST_LOW (t);
1102 if (prec < HOST_BITS_PER_WIDE_INT)
1103 val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
1104 return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
1107 /* Determine whether an expression T can be cheaply negated using
1108 the function negate_expr without introducing undefined overflow. */
1111 negate_expr_p (tree t)
1118 type = TREE_TYPE (t);
1120 STRIP_SIGN_NOPS (t);
1121 switch (TREE_CODE (t))
1124 if (TYPE_OVERFLOW_WRAPS (type))
1127 /* Check that -CST will not overflow type. */
1128 return may_negate_without_overflow_p (t);
1130 return (INTEGRAL_TYPE_P (type)
1131 && TYPE_OVERFLOW_WRAPS (type));
1139 return negate_expr_p (TREE_REALPART (t))
1140 && negate_expr_p (TREE_IMAGPART (t));
1143 return negate_expr_p (TREE_OPERAND (t, 0))
1144 && negate_expr_p (TREE_OPERAND (t, 1));
1147 return negate_expr_p (TREE_OPERAND (t, 0));
1150 if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1151 || HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
1153 /* -(A + B) -> (-B) - A. */
1154 if (negate_expr_p (TREE_OPERAND (t, 1))
1155 && reorder_operands_p (TREE_OPERAND (t, 0),
1156 TREE_OPERAND (t, 1)))
1158 /* -(A + B) -> (-A) - B. */
1159 return negate_expr_p (TREE_OPERAND (t, 0));
1162 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
1163 return !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1164 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
1165 && reorder_operands_p (TREE_OPERAND (t, 0),
1166 TREE_OPERAND (t, 1));
1169 if (TYPE_UNSIGNED (TREE_TYPE (t)))
1175 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
1176 return negate_expr_p (TREE_OPERAND (t, 1))
1177 || negate_expr_p (TREE_OPERAND (t, 0));
1180 case TRUNC_DIV_EXPR:
1181 case ROUND_DIV_EXPR:
1182 case FLOOR_DIV_EXPR:
1184 case EXACT_DIV_EXPR:
1185 /* In general we can't negate A / B, because if A is INT_MIN and
1186 B is 1, we may turn this into INT_MIN / -1 which is undefined
1187 and actually traps on some architectures. But if overflow is
1188 undefined, we can negate, because - (INT_MIN / 1) is an
1190 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
1191 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
1193 return negate_expr_p (TREE_OPERAND (t, 1))
1194 || negate_expr_p (TREE_OPERAND (t, 0));
1197 /* Negate -((double)float) as (double)(-float). */
1198 if (TREE_CODE (type) == REAL_TYPE)
1200 tree tem = strip_float_extensions (t);
1202 return negate_expr_p (tem);
1207 /* Negate -f(x) as f(-x). */
1208 if (negate_mathfn_p (builtin_mathfn_code (t)))
1209 return negate_expr_p (CALL_EXPR_ARG (t, 0));
1213 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
1214 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1216 tree op1 = TREE_OPERAND (t, 1);
1217 if (TREE_INT_CST_HIGH (op1) == 0
1218 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1219 == TREE_INT_CST_LOW (op1))
1230 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
1231 simplification is possible.
1232 If negate_expr_p would return true for T, NULL_TREE will never be
1236 fold_negate_expr (tree t)
1238 tree type = TREE_TYPE (t);
1241 switch (TREE_CODE (t))
1243 /* Convert - (~A) to A + 1. */
1245 if (INTEGRAL_TYPE_P (type))
1246 return fold_build2 (PLUS_EXPR, type, TREE_OPERAND (t, 0),
1247 build_int_cst (type, 1));
1251 tem = fold_negate_const (t, type);
1252 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
1253 || !TYPE_OVERFLOW_TRAPS (type))
1258 tem = fold_negate_const (t, type);
1259 /* Two's complement FP formats, such as c4x, may overflow. */
1260 if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1265 tem = fold_negate_const (t, type);
1270 tree rpart = negate_expr (TREE_REALPART (t));
1271 tree ipart = negate_expr (TREE_IMAGPART (t));
1273 if ((TREE_CODE (rpart) == REAL_CST
1274 && TREE_CODE (ipart) == REAL_CST)
1275 || (TREE_CODE (rpart) == INTEGER_CST
1276 && TREE_CODE (ipart) == INTEGER_CST))
1277 return build_complex (type, rpart, ipart);
1282 if (negate_expr_p (t))
1283 return fold_build2 (COMPLEX_EXPR, type,
1284 fold_negate_expr (TREE_OPERAND (t, 0)),
1285 fold_negate_expr (TREE_OPERAND (t, 1)));
1289 if (negate_expr_p (t))
1290 return fold_build1 (CONJ_EXPR, type,
1291 fold_negate_expr (TREE_OPERAND (t, 0)));
1295 return TREE_OPERAND (t, 0);
1298 if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1299 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
1301 /* -(A + B) -> (-B) - A. */
1302 if (negate_expr_p (TREE_OPERAND (t, 1))
1303 && reorder_operands_p (TREE_OPERAND (t, 0),
1304 TREE_OPERAND (t, 1)))
1306 tem = negate_expr (TREE_OPERAND (t, 1));
1307 return fold_build2 (MINUS_EXPR, type,
1308 tem, TREE_OPERAND (t, 0));
1311 /* -(A + B) -> (-A) - B. */
1312 if (negate_expr_p (TREE_OPERAND (t, 0)))
1314 tem = negate_expr (TREE_OPERAND (t, 0));
1315 return fold_build2 (MINUS_EXPR, type,
1316 tem, TREE_OPERAND (t, 1));
1322 /* - (A - B) -> B - A */
1323 if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1324 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
1325 && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
1326 return fold_build2 (MINUS_EXPR, type,
1327 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
1331 if (TYPE_UNSIGNED (type))
1337 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)))
1339 tem = TREE_OPERAND (t, 1);
1340 if (negate_expr_p (tem))
1341 return fold_build2 (TREE_CODE (t), type,
1342 TREE_OPERAND (t, 0), negate_expr (tem));
1343 tem = TREE_OPERAND (t, 0);
1344 if (negate_expr_p (tem))
1345 return fold_build2 (TREE_CODE (t), type,
1346 negate_expr (tem), TREE_OPERAND (t, 1));
1350 case TRUNC_DIV_EXPR:
1351 case ROUND_DIV_EXPR:
1352 case FLOOR_DIV_EXPR:
1354 case EXACT_DIV_EXPR:
1355 /* In general we can't negate A / B, because if A is INT_MIN and
1356 B is 1, we may turn this into INT_MIN / -1 which is undefined
1357 and actually traps on some architectures. But if overflow is
1358 undefined, we can negate, because - (INT_MIN / 1) is an
1360 if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
1362 const char * const warnmsg = G_("assuming signed overflow does not "
1363 "occur when negating a division");
1364 tem = TREE_OPERAND (t, 1);
1365 if (negate_expr_p (tem))
1367 if (INTEGRAL_TYPE_P (type)
1368 && (TREE_CODE (tem) != INTEGER_CST
1369 || integer_onep (tem)))
1370 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
1371 return fold_build2 (TREE_CODE (t), type,
1372 TREE_OPERAND (t, 0), negate_expr (tem));
1374 tem = TREE_OPERAND (t, 0);
1375 if (negate_expr_p (tem))
1377 if (INTEGRAL_TYPE_P (type)
1378 && (TREE_CODE (tem) != INTEGER_CST
1379 || tree_int_cst_equal (tem, TYPE_MIN_VALUE (type))))
1380 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
1381 return fold_build2 (TREE_CODE (t), type,
1382 negate_expr (tem), TREE_OPERAND (t, 1));
1388 /* Convert -((double)float) into (double)(-float). */
1389 if (TREE_CODE (type) == REAL_TYPE)
1391 tem = strip_float_extensions (t);
1392 if (tem != t && negate_expr_p (tem))
1393 return fold_convert (type, negate_expr (tem));
1398 /* Negate -f(x) as f(-x). */
1399 if (negate_mathfn_p (builtin_mathfn_code (t))
1400 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
1404 fndecl = get_callee_fndecl (t);
1405 arg = negate_expr (CALL_EXPR_ARG (t, 0));
1406 return build_call_expr (fndecl, 1, arg);
1411 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
1412 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1414 tree op1 = TREE_OPERAND (t, 1);
1415 if (TREE_INT_CST_HIGH (op1) == 0
1416 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1417 == TREE_INT_CST_LOW (op1))
1419 tree ntype = TYPE_UNSIGNED (type)
1420 ? signed_type_for (type)
1421 : unsigned_type_for (type);
1422 tree temp = fold_convert (ntype, TREE_OPERAND (t, 0));
1423 temp = fold_build2 (RSHIFT_EXPR, ntype, temp, op1);
1424 return fold_convert (type, temp);
1436 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
1437 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
1438 return NULL_TREE. */
1441 negate_expr (tree t)
1448 type = TREE_TYPE (t);
1449 STRIP_SIGN_NOPS (t);
1451 tem = fold_negate_expr (t);
1453 tem = build1 (NEGATE_EXPR, TREE_TYPE (t), t);
1454 return fold_convert (type, tem);
1457 /* Split a tree IN into a constant, literal and variable parts that could be
1458 combined with CODE to make IN. "constant" means an expression with
1459 TREE_CONSTANT but that isn't an actual constant. CODE must be a
1460 commutative arithmetic operation. Store the constant part into *CONP,
1461 the literal in *LITP and return the variable part. If a part isn't
1462 present, set it to null. If the tree does not decompose in this way,
1463 return the entire tree as the variable part and the other parts as null.
1465 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
1466 case, we negate an operand that was subtracted. Except if it is a
1467 literal for which we use *MINUS_LITP instead.
1469 If NEGATE_P is true, we are negating all of IN, again except a literal
1470 for which we use *MINUS_LITP instead.
1472 If IN is itself a literal or constant, return it as appropriate.
1474 Note that we do not guarantee that any of the three values will be the
1475 same type as IN, but they will have the same signedness and mode. */
1478 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
1479 tree *minus_litp, int negate_p)
1487 /* Strip any conversions that don't change the machine mode or signedness. */
1488 STRIP_SIGN_NOPS (in);
1490 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
1491 || TREE_CODE (in) == FIXED_CST)
1493 else if (TREE_CODE (in) == code
1494 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
1495 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
1496 /* We can associate addition and subtraction together (even
1497 though the C standard doesn't say so) for integers because
1498 the value is not affected. For reals, the value might be
1499 affected, so we can't. */
1500 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1501 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1503 tree op0 = TREE_OPERAND (in, 0);
1504 tree op1 = TREE_OPERAND (in, 1);
1505 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1506 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1508 /* First see if either of the operands is a literal, then a constant. */
1509 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
1510 || TREE_CODE (op0) == FIXED_CST)
1511 *litp = op0, op0 = 0;
1512 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
1513 || TREE_CODE (op1) == FIXED_CST)
1514 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1516 if (op0 != 0 && TREE_CONSTANT (op0))
1517 *conp = op0, op0 = 0;
1518 else if (op1 != 0 && TREE_CONSTANT (op1))
1519 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1521 /* If we haven't dealt with either operand, this is not a case we can
1522 decompose. Otherwise, VAR is either of the ones remaining, if any. */
1523 if (op0 != 0 && op1 != 0)
1528 var = op1, neg_var_p = neg1_p;
1530 /* Now do any needed negations. */
1532 *minus_litp = *litp, *litp = 0;
1534 *conp = negate_expr (*conp);
1536 var = negate_expr (var);
1538 else if (TREE_CONSTANT (in))
1546 *minus_litp = *litp, *litp = 0;
1547 else if (*minus_litp)
1548 *litp = *minus_litp, *minus_litp = 0;
1549 *conp = negate_expr (*conp);
1550 var = negate_expr (var);
1556 /* Re-associate trees split by the above function. T1 and T2 are either
1557 expressions to associate or null. Return the new expression, if any. If
1558 we build an operation, do it in TYPE and with CODE. */
1561 associate_trees (tree t1, tree t2, enum tree_code code, tree type)
1568 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1569 try to fold this since we will have infinite recursion. But do
1570 deal with any NEGATE_EXPRs. */
1571 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1572 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1574 if (code == PLUS_EXPR)
1576 if (TREE_CODE (t1) == NEGATE_EXPR)
1577 return build2 (MINUS_EXPR, type, fold_convert (type, t2),
1578 fold_convert (type, TREE_OPERAND (t1, 0)));
1579 else if (TREE_CODE (t2) == NEGATE_EXPR)
1580 return build2 (MINUS_EXPR, type, fold_convert (type, t1),
1581 fold_convert (type, TREE_OPERAND (t2, 0)));
1582 else if (integer_zerop (t2))
1583 return fold_convert (type, t1);
1585 else if (code == MINUS_EXPR)
1587 if (integer_zerop (t2))
1588 return fold_convert (type, t1);
1591 return build2 (code, type, fold_convert (type, t1),
1592 fold_convert (type, t2));
1595 return fold_build2 (code, type, fold_convert (type, t1),
1596 fold_convert (type, t2));
1599 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
1600 for use in int_const_binop, size_binop and size_diffop. */
1603 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
1605 if (TREE_CODE (type1) != INTEGER_TYPE && !POINTER_TYPE_P (type1))
1607 if (TREE_CODE (type2) != INTEGER_TYPE && !POINTER_TYPE_P (type2))
1622 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
1623 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1624 && TYPE_MODE (type1) == TYPE_MODE (type2);
1628 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1629 to produce a new constant. Return NULL_TREE if we don't know how
1630 to evaluate CODE at compile-time.
1632 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1635 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2, int notrunc)
1637 unsigned HOST_WIDE_INT int1l, int2l;
1638 HOST_WIDE_INT int1h, int2h;
1639 unsigned HOST_WIDE_INT low;
1641 unsigned HOST_WIDE_INT garbagel;
1642 HOST_WIDE_INT garbageh;
1644 tree type = TREE_TYPE (arg1);
1645 int uns = TYPE_UNSIGNED (type);
1647 = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
1650 int1l = TREE_INT_CST_LOW (arg1);
1651 int1h = TREE_INT_CST_HIGH (arg1);
1652 int2l = TREE_INT_CST_LOW (arg2);
1653 int2h = TREE_INT_CST_HIGH (arg2);
1658 low = int1l | int2l, hi = int1h | int2h;
1662 low = int1l ^ int2l, hi = int1h ^ int2h;
1666 low = int1l & int2l, hi = int1h & int2h;
1672 /* It's unclear from the C standard whether shifts can overflow.
1673 The following code ignores overflow; perhaps a C standard
1674 interpretation ruling is needed. */
1675 lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1682 lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1687 overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1691 neg_double (int2l, int2h, &low, &hi);
1692 add_double (int1l, int1h, low, hi, &low, &hi);
1693 overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
1697 overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1700 case TRUNC_DIV_EXPR:
1701 case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1702 case EXACT_DIV_EXPR:
1703 /* This is a shortcut for a common special case. */
1704 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1705 && !TREE_OVERFLOW (arg1)
1706 && !TREE_OVERFLOW (arg2)
1707 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1709 if (code == CEIL_DIV_EXPR)
1712 low = int1l / int2l, hi = 0;
1716 /* ... fall through ... */
1718 case ROUND_DIV_EXPR:
1719 if (int2h == 0 && int2l == 0)
1721 if (int2h == 0 && int2l == 1)
1723 low = int1l, hi = int1h;
1726 if (int1l == int2l && int1h == int2h
1727 && ! (int1l == 0 && int1h == 0))
1732 overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
1733 &low, &hi, &garbagel, &garbageh);
1736 case TRUNC_MOD_EXPR:
1737 case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1738 /* This is a shortcut for a common special case. */
1739 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1740 && !TREE_OVERFLOW (arg1)
1741 && !TREE_OVERFLOW (arg2)
1742 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1744 if (code == CEIL_MOD_EXPR)
1746 low = int1l % int2l, hi = 0;
1750 /* ... fall through ... */
1752 case ROUND_MOD_EXPR:
1753 if (int2h == 0 && int2l == 0)
1755 overflow = div_and_round_double (code, uns,
1756 int1l, int1h, int2l, int2h,
1757 &garbagel, &garbageh, &low, &hi);
1763 low = (((unsigned HOST_WIDE_INT) int1h
1764 < (unsigned HOST_WIDE_INT) int2h)
1765 || (((unsigned HOST_WIDE_INT) int1h
1766 == (unsigned HOST_WIDE_INT) int2h)
1769 low = (int1h < int2h
1770 || (int1h == int2h && int1l < int2l));
1772 if (low == (code == MIN_EXPR))
1773 low = int1l, hi = int1h;
1775 low = int2l, hi = int2h;
1784 t = build_int_cst_wide (TREE_TYPE (arg1), low, hi);
1786 /* Propagate overflow flags ourselves. */
1787 if (((!uns || is_sizetype) && overflow)
1788 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1791 TREE_OVERFLOW (t) = 1;
1795 t = force_fit_type_double (TREE_TYPE (arg1), low, hi, 1,
1796 ((!uns || is_sizetype) && overflow)
1797 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1802 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1803 constant. We assume ARG1 and ARG2 have the same data type, or at least
1804 are the same kind of constant and the same machine mode. Return zero if
1805 combining the constants is not allowed in the current operating mode.
1807 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1810 const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1812 /* Sanity check for the recursive cases. */
1819 if (TREE_CODE (arg1) == INTEGER_CST)
1820 return int_const_binop (code, arg1, arg2, notrunc);
1822 if (TREE_CODE (arg1) == REAL_CST)
1824 enum machine_mode mode;
1827 REAL_VALUE_TYPE value;
1828 REAL_VALUE_TYPE result;
1832 /* The following codes are handled by real_arithmetic. */
1847 d1 = TREE_REAL_CST (arg1);
1848 d2 = TREE_REAL_CST (arg2);
1850 type = TREE_TYPE (arg1);
1851 mode = TYPE_MODE (type);
1853 /* Don't perform operation if we honor signaling NaNs and
1854 either operand is a NaN. */
1855 if (HONOR_SNANS (mode)
1856 && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1859 /* Don't perform operation if it would raise a division
1860 by zero exception. */
1861 if (code == RDIV_EXPR
1862 && REAL_VALUES_EQUAL (d2, dconst0)
1863 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1866 /* If either operand is a NaN, just return it. Otherwise, set up
1867 for floating-point trap; we return an overflow. */
1868 if (REAL_VALUE_ISNAN (d1))
1870 else if (REAL_VALUE_ISNAN (d2))
1873 inexact = real_arithmetic (&value, code, &d1, &d2);
1874 real_convert (&result, mode, &value);
1876 /* Don't constant fold this floating point operation if
1877 the result has overflowed and flag_trapping_math. */
1878 if (flag_trapping_math
1879 && MODE_HAS_INFINITIES (mode)
1880 && REAL_VALUE_ISINF (result)
1881 && !REAL_VALUE_ISINF (d1)
1882 && !REAL_VALUE_ISINF (d2))
1885 /* Don't constant fold this floating point operation if the
1886 result may dependent upon the run-time rounding mode and
1887 flag_rounding_math is set, or if GCC's software emulation
1888 is unable to accurately represent the result. */
1889 if ((flag_rounding_math
1890 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1891 && (inexact || !real_identical (&result, &value)))
1894 t = build_real (type, result);
1896 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1900 if (TREE_CODE (arg1) == FIXED_CST)
1902 FIXED_VALUE_TYPE f1;
1903 FIXED_VALUE_TYPE f2;
1904 FIXED_VALUE_TYPE result;
1909 /* The following codes are handled by fixed_arithmetic. */
1915 case TRUNC_DIV_EXPR:
1916 f2 = TREE_FIXED_CST (arg2);
1921 f2.data.high = TREE_INT_CST_HIGH (arg2);
1922 f2.data.low = TREE_INT_CST_LOW (arg2);
1930 f1 = TREE_FIXED_CST (arg1);
1931 type = TREE_TYPE (arg1);
1932 sat_p = TYPE_SATURATING (type);
1933 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1934 t = build_fixed (type, result);
1935 /* Propagate overflow flags. */
1936 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1938 TREE_OVERFLOW (t) = 1;
1939 TREE_CONSTANT_OVERFLOW (t) = 1;
1941 else if (TREE_CONSTANT_OVERFLOW (arg1) | TREE_CONSTANT_OVERFLOW (arg2))
1942 TREE_CONSTANT_OVERFLOW (t) = 1;
1946 if (TREE_CODE (arg1) == COMPLEX_CST)
1948 tree type = TREE_TYPE (arg1);
1949 tree r1 = TREE_REALPART (arg1);
1950 tree i1 = TREE_IMAGPART (arg1);
1951 tree r2 = TREE_REALPART (arg2);
1952 tree i2 = TREE_IMAGPART (arg2);
1959 real = const_binop (code, r1, r2, notrunc);
1960 imag = const_binop (code, i1, i2, notrunc);
1964 real = const_binop (MINUS_EXPR,
1965 const_binop (MULT_EXPR, r1, r2, notrunc),
1966 const_binop (MULT_EXPR, i1, i2, notrunc),
1968 imag = const_binop (PLUS_EXPR,
1969 const_binop (MULT_EXPR, r1, i2, notrunc),
1970 const_binop (MULT_EXPR, i1, r2, notrunc),
1977 = const_binop (PLUS_EXPR,
1978 const_binop (MULT_EXPR, r2, r2, notrunc),
1979 const_binop (MULT_EXPR, i2, i2, notrunc),
1982 = const_binop (PLUS_EXPR,
1983 const_binop (MULT_EXPR, r1, r2, notrunc),
1984 const_binop (MULT_EXPR, i1, i2, notrunc),
1987 = const_binop (MINUS_EXPR,
1988 const_binop (MULT_EXPR, i1, r2, notrunc),
1989 const_binop (MULT_EXPR, r1, i2, notrunc),
1992 if (INTEGRAL_TYPE_P (TREE_TYPE (r1)))
1993 code = TRUNC_DIV_EXPR;
1995 real = const_binop (code, t1, magsquared, notrunc);
1996 imag = const_binop (code, t2, magsquared, notrunc);
2005 return build_complex (type, real, imag);
2011 /* Create a size type INT_CST node with NUMBER sign extended. KIND
2012 indicates which particular sizetype to create. */
2015 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
2017 return build_int_cst (sizetype_tab[(int) kind], number);
2020 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
2021 is a tree code. The type of the result is taken from the operands.
2022 Both must be equivalent integer types, ala int_binop_types_match_p.
2023 If the operands are constant, so is the result. */
2026 size_binop (enum tree_code code, tree arg0, tree arg1)
2028 tree type = TREE_TYPE (arg0);
2030 if (arg0 == error_mark_node || arg1 == error_mark_node)
2031 return error_mark_node;
2033 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
2036 /* Handle the special case of two integer constants faster. */
2037 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2039 /* And some specific cases even faster than that. */
2040 if (code == PLUS_EXPR)
2042 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
2044 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2047 else if (code == MINUS_EXPR)
2049 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2052 else if (code == MULT_EXPR)
2054 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
2058 /* Handle general case of two integer constants. */
2059 return int_const_binop (code, arg0, arg1, 0);
2062 return fold_build2 (code, type, arg0, arg1);
2065 /* Given two values, either both of sizetype or both of bitsizetype,
2066 compute the difference between the two values. Return the value
2067 in signed type corresponding to the type of the operands. */
2070 size_diffop (tree arg0, tree arg1)
2072 tree type = TREE_TYPE (arg0);
2075 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
2078 /* If the type is already signed, just do the simple thing. */
2079 if (!TYPE_UNSIGNED (type))
2080 return size_binop (MINUS_EXPR, arg0, arg1);
2082 if (type == sizetype)
2084 else if (type == bitsizetype)
2085 ctype = sbitsizetype;
2087 ctype = signed_type_for (type);
2089 /* If either operand is not a constant, do the conversions to the signed
2090 type and subtract. The hardware will do the right thing with any
2091 overflow in the subtraction. */
2092 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2093 return size_binop (MINUS_EXPR, fold_convert (ctype, arg0),
2094 fold_convert (ctype, arg1));
2096 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2097 Otherwise, subtract the other way, convert to CTYPE (we know that can't
2098 overflow) and negate (which can't either). Special-case a result
2099 of zero while we're here. */
2100 if (tree_int_cst_equal (arg0, arg1))
2101 return build_int_cst (ctype, 0);
2102 else if (tree_int_cst_lt (arg1, arg0))
2103 return fold_convert (ctype, size_binop (MINUS_EXPR, arg0, arg1));
2105 return size_binop (MINUS_EXPR, build_int_cst (ctype, 0),
2106 fold_convert (ctype, size_binop (MINUS_EXPR,
2110 /* A subroutine of fold_convert_const handling conversions of an
2111 INTEGER_CST to another integer type. */
2114 fold_convert_const_int_from_int (tree type, const_tree arg1)
2118 /* Given an integer constant, make new constant with new type,
2119 appropriately sign-extended or truncated. */
2120 t = force_fit_type_double (type, TREE_INT_CST_LOW (arg1),
2121 TREE_INT_CST_HIGH (arg1),
2122 /* Don't set the overflow when
2123 converting from a pointer, */
2124 !POINTER_TYPE_P (TREE_TYPE (arg1))
2125 /* or to a sizetype with same signedness
2126 and the precision is unchanged.
2127 ??? sizetype is always sign-extended,
2128 but its signedness depends on the
2129 frontend. Thus we see spurious overflows
2130 here if we do not check this. */
2131 && !((TYPE_PRECISION (TREE_TYPE (arg1))
2132 == TYPE_PRECISION (type))
2133 && (TYPE_UNSIGNED (TREE_TYPE (arg1))
2134 == TYPE_UNSIGNED (type))
2135 && ((TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
2136 && TYPE_IS_SIZETYPE (TREE_TYPE (arg1)))
2137 || (TREE_CODE (type) == INTEGER_TYPE
2138 && TYPE_IS_SIZETYPE (type)))),
2139 (TREE_INT_CST_HIGH (arg1) < 0
2140 && (TYPE_UNSIGNED (type)
2141 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2142 | TREE_OVERFLOW (arg1));
2147 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2148 to an integer type. */
2151 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2156 /* The following code implements the floating point to integer
2157 conversion rules required by the Java Language Specification,
2158 that IEEE NaNs are mapped to zero and values that overflow
2159 the target precision saturate, i.e. values greater than
2160 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2161 are mapped to INT_MIN. These semantics are allowed by the
2162 C and C++ standards that simply state that the behavior of
2163 FP-to-integer conversion is unspecified upon overflow. */
2165 HOST_WIDE_INT high, low;
2167 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2171 case FIX_TRUNC_EXPR:
2172 real_trunc (&r, VOIDmode, &x);
2179 /* If R is NaN, return zero and show we have an overflow. */
2180 if (REAL_VALUE_ISNAN (r))
2187 /* See if R is less than the lower bound or greater than the
2192 tree lt = TYPE_MIN_VALUE (type);
2193 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2194 if (REAL_VALUES_LESS (r, l))
2197 high = TREE_INT_CST_HIGH (lt);
2198 low = TREE_INT_CST_LOW (lt);
2204 tree ut = TYPE_MAX_VALUE (type);
2207 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2208 if (REAL_VALUES_LESS (u, r))
2211 high = TREE_INT_CST_HIGH (ut);
2212 low = TREE_INT_CST_LOW (ut);
2218 REAL_VALUE_TO_INT (&low, &high, r);
2220 t = force_fit_type_double (type, low, high, -1,
2221 overflow | TREE_OVERFLOW (arg1));
2225 /* A subroutine of fold_convert_const handling conversions of a
2226 FIXED_CST to an integer type. */
2229 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2232 double_int temp, temp_trunc;
2235 /* Right shift FIXED_CST to temp by fbit. */
2236 temp = TREE_FIXED_CST (arg1).data;
2237 mode = TREE_FIXED_CST (arg1).mode;
2238 if (GET_MODE_FBIT (mode) < 2 * HOST_BITS_PER_WIDE_INT)
2240 lshift_double (temp.low, temp.high,
2241 - GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2242 &temp.low, &temp.high, SIGNED_FIXED_POINT_MODE_P (mode));
2244 /* Left shift temp to temp_trunc by fbit. */
2245 lshift_double (temp.low, temp.high,
2246 GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2247 &temp_trunc.low, &temp_trunc.high,
2248 SIGNED_FIXED_POINT_MODE_P (mode));
2255 temp_trunc.high = 0;
2258 /* If FIXED_CST is negative, we need to round the value toward 0.
2259 By checking if the fractional bits are not zero to add 1 to temp. */
2260 if (SIGNED_FIXED_POINT_MODE_P (mode) && temp_trunc.high < 0
2261 && !double_int_equal_p (TREE_FIXED_CST (arg1).data, temp_trunc))
2266 temp = double_int_add (temp, one);
2269 /* Given a fixed-point constant, make new constant with new type,
2270 appropriately sign-extended or truncated. */
2271 t = force_fit_type_double (type, temp.low, temp.high, -1,
2273 && (TYPE_UNSIGNED (type)
2274 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2275 | TREE_OVERFLOW (arg1));
2280 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2281 to another floating point type. */
2284 fold_convert_const_real_from_real (tree type, const_tree arg1)
2286 REAL_VALUE_TYPE value;
2289 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2290 t = build_real (type, value);
2292 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2296 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2297 to a floating point type. */
2300 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2302 REAL_VALUE_TYPE value;
2305 real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
2306 t = build_real (type, value);
2308 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2309 TREE_CONSTANT_OVERFLOW (t)
2310 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
2314 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2315 to another fixed-point type. */
2318 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2320 FIXED_VALUE_TYPE value;
2324 overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2325 TYPE_SATURATING (type));
2326 t = build_fixed (type, value);
2328 /* Propagate overflow flags. */
2329 if (overflow_p | TREE_OVERFLOW (arg1))
2331 TREE_OVERFLOW (t) = 1;
2332 TREE_CONSTANT_OVERFLOW (t) = 1;
2334 else if (TREE_CONSTANT_OVERFLOW (arg1))
2335 TREE_CONSTANT_OVERFLOW (t) = 1;
2339 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2340 to a fixed-point type. */
2343 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2345 FIXED_VALUE_TYPE value;
2349 overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type),
2350 TREE_INT_CST (arg1),
2351 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2352 TYPE_SATURATING (type));
2353 t = build_fixed (type, value);
2355 /* Propagate overflow flags. */
2356 if (overflow_p | TREE_OVERFLOW (arg1))
2358 TREE_OVERFLOW (t) = 1;
2359 TREE_CONSTANT_OVERFLOW (t) = 1;
2361 else if (TREE_CONSTANT_OVERFLOW (arg1))
2362 TREE_CONSTANT_OVERFLOW (t) = 1;
2366 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2367 to a fixed-point type. */
2370 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2372 FIXED_VALUE_TYPE value;
2376 overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2377 &TREE_REAL_CST (arg1),
2378 TYPE_SATURATING (type));
2379 t = build_fixed (type, value);
2381 /* Propagate overflow flags. */
2382 if (overflow_p | TREE_OVERFLOW (arg1))
2384 TREE_OVERFLOW (t) = 1;
2385 TREE_CONSTANT_OVERFLOW (t) = 1;
2387 else if (TREE_CONSTANT_OVERFLOW (arg1))
2388 TREE_CONSTANT_OVERFLOW (t) = 1;
2392 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2393 type TYPE. If no simplification can be done return NULL_TREE. */
2396 fold_convert_const (enum tree_code code, tree type, tree arg1)
2398 if (TREE_TYPE (arg1) == type)
2401 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2402 || TREE_CODE (type) == OFFSET_TYPE)
2404 if (TREE_CODE (arg1) == INTEGER_CST)
2405 return fold_convert_const_int_from_int (type, arg1);
2406 else if (TREE_CODE (arg1) == REAL_CST)
2407 return fold_convert_const_int_from_real (code, type, arg1);
2408 else if (TREE_CODE (arg1) == FIXED_CST)
2409 return fold_convert_const_int_from_fixed (type, arg1);
2411 else if (TREE_CODE (type) == REAL_TYPE)
2413 if (TREE_CODE (arg1) == INTEGER_CST)
2414 return build_real_from_int_cst (type, arg1);
2415 else if (TREE_CODE (arg1) == REAL_CST)
2416 return fold_convert_const_real_from_real (type, arg1);
2417 else if (TREE_CODE (arg1) == FIXED_CST)
2418 return fold_convert_const_real_from_fixed (type, arg1);
2420 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2422 if (TREE_CODE (arg1) == FIXED_CST)
2423 return fold_convert_const_fixed_from_fixed (type, arg1);
2424 else if (TREE_CODE (arg1) == INTEGER_CST)
2425 return fold_convert_const_fixed_from_int (type, arg1);
2426 else if (TREE_CODE (arg1) == REAL_CST)
2427 return fold_convert_const_fixed_from_real (type, arg1);
2432 /* Construct a vector of zero elements of vector type TYPE. */
2435 build_zero_vector (tree type)
2440 elem = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2441 units = TYPE_VECTOR_SUBPARTS (type);
2444 for (i = 0; i < units; i++)
2445 list = tree_cons (NULL_TREE, elem, list);
2446 return build_vector (type, list);
2449 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2452 fold_convertible_p (const_tree type, const_tree arg)
2454 tree orig = TREE_TYPE (arg);
2459 if (TREE_CODE (arg) == ERROR_MARK
2460 || TREE_CODE (type) == ERROR_MARK
2461 || TREE_CODE (orig) == ERROR_MARK)
2464 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2467 switch (TREE_CODE (type))
2469 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2470 case POINTER_TYPE: case REFERENCE_TYPE:
2472 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2473 || TREE_CODE (orig) == OFFSET_TYPE)
2475 return (TREE_CODE (orig) == VECTOR_TYPE
2476 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2479 case FIXED_POINT_TYPE:
2483 return TREE_CODE (type) == TREE_CODE (orig);
2490 /* Convert expression ARG to type TYPE. Used by the middle-end for
2491 simple conversions in preference to calling the front-end's convert. */
2494 fold_convert (tree type, tree arg)
2496 tree orig = TREE_TYPE (arg);
2502 if (TREE_CODE (arg) == ERROR_MARK
2503 || TREE_CODE (type) == ERROR_MARK
2504 || TREE_CODE (orig) == ERROR_MARK)
2505 return error_mark_node;
2507 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2508 return fold_build1 (NOP_EXPR, type, arg);
2510 switch (TREE_CODE (type))
2512 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2513 case POINTER_TYPE: case REFERENCE_TYPE:
2515 if (TREE_CODE (arg) == INTEGER_CST)
2517 tem = fold_convert_const (NOP_EXPR, type, arg);
2518 if (tem != NULL_TREE)
2521 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2522 || TREE_CODE (orig) == OFFSET_TYPE)
2523 return fold_build1 (NOP_EXPR, type, arg);
2524 if (TREE_CODE (orig) == COMPLEX_TYPE)
2526 tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2527 return fold_convert (type, tem);
2529 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2530 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2531 return fold_build1 (NOP_EXPR, type, arg);
2534 if (TREE_CODE (arg) == INTEGER_CST)
2536 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2537 if (tem != NULL_TREE)
2540 else if (TREE_CODE (arg) == REAL_CST)
2542 tem = fold_convert_const (NOP_EXPR, type, arg);
2543 if (tem != NULL_TREE)
2546 else if (TREE_CODE (arg) == FIXED_CST)
2548 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2549 if (tem != NULL_TREE)
2553 switch (TREE_CODE (orig))
2556 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2557 case POINTER_TYPE: case REFERENCE_TYPE:
2558 return fold_build1 (FLOAT_EXPR, type, arg);
2561 return fold_build1 (NOP_EXPR, type, arg);
2563 case FIXED_POINT_TYPE:
2564 return fold_build1 (FIXED_CONVERT_EXPR, type, arg);
2567 tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2568 return fold_convert (type, tem);
2574 case FIXED_POINT_TYPE:
2575 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2576 || TREE_CODE (arg) == REAL_CST)
2578 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2579 if (tem != NULL_TREE)
2583 switch (TREE_CODE (orig))
2585 case FIXED_POINT_TYPE:
2590 return fold_build1 (FIXED_CONVERT_EXPR, type, arg);
2593 tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2594 return fold_convert (type, tem);
2601 switch (TREE_CODE (orig))
2604 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2605 case POINTER_TYPE: case REFERENCE_TYPE:
2607 case FIXED_POINT_TYPE:
2608 return build2 (COMPLEX_EXPR, type,
2609 fold_convert (TREE_TYPE (type), arg),
2610 fold_convert (TREE_TYPE (type), integer_zero_node));
2615 if (TREE_CODE (arg) == COMPLEX_EXPR)
2617 rpart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 0));
2618 ipart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 1));
2619 return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2622 arg = save_expr (arg);
2623 rpart = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2624 ipart = fold_build1 (IMAGPART_EXPR, TREE_TYPE (orig), arg);
2625 rpart = fold_convert (TREE_TYPE (type), rpart);
2626 ipart = fold_convert (TREE_TYPE (type), ipart);
2627 return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2635 if (integer_zerop (arg))
2636 return build_zero_vector (type);
2637 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2638 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2639 || TREE_CODE (orig) == VECTOR_TYPE);
2640 return fold_build1 (VIEW_CONVERT_EXPR, type, arg);
2643 tem = fold_ignored_result (arg);
2644 if (TREE_CODE (tem) == MODIFY_EXPR)
2646 return fold_build1 (NOP_EXPR, type, tem);
2653 /* Return false if expr can be assumed not to be an lvalue, true
2657 maybe_lvalue_p (const_tree x)
2659 /* We only need to wrap lvalue tree codes. */
2660 switch (TREE_CODE (x))
2671 case ALIGN_INDIRECT_REF:
2672 case MISALIGNED_INDIRECT_REF:
2674 case ARRAY_RANGE_REF:
2680 case PREINCREMENT_EXPR:
2681 case PREDECREMENT_EXPR:
2683 case TRY_CATCH_EXPR:
2684 case WITH_CLEANUP_EXPR:
2695 /* Assume the worst for front-end tree codes. */
2696 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2704 /* Return an expr equal to X but certainly not valid as an lvalue. */
2709 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2714 if (! maybe_lvalue_p (x))
2716 return build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2719 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2720 Zero means allow extended lvalues. */
2722 int pedantic_lvalues;
2724 /* When pedantic, return an expr equal to X but certainly not valid as a
2725 pedantic lvalue. Otherwise, return X. */
2728 pedantic_non_lvalue (tree x)
2730 if (pedantic_lvalues)
2731 return non_lvalue (x);
2736 /* Given a tree comparison code, return the code that is the logical inverse
2737 of the given code. It is not safe to do this for floating-point
2738 comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2739 as well: if reversing the comparison is unsafe, return ERROR_MARK. */
2742 invert_tree_comparison (enum tree_code code, bool honor_nans)
2744 if (honor_nans && flag_trapping_math)
2754 return honor_nans ? UNLE_EXPR : LE_EXPR;
2756 return honor_nans ? UNLT_EXPR : LT_EXPR;
2758 return honor_nans ? UNGE_EXPR : GE_EXPR;
2760 return honor_nans ? UNGT_EXPR : GT_EXPR;
2774 return UNORDERED_EXPR;
2775 case UNORDERED_EXPR:
2776 return ORDERED_EXPR;
2782 /* Similar, but return the comparison that results if the operands are
2783 swapped. This is safe for floating-point. */
2786 swap_tree_comparison (enum tree_code code)
2793 case UNORDERED_EXPR:
2819 /* Convert a comparison tree code from an enum tree_code representation
2820 into a compcode bit-based encoding. This function is the inverse of
2821 compcode_to_comparison. */
2823 static enum comparison_code
2824 comparison_to_compcode (enum tree_code code)
2841 return COMPCODE_ORD;
2842 case UNORDERED_EXPR:
2843 return COMPCODE_UNORD;
2845 return COMPCODE_UNLT;
2847 return COMPCODE_UNEQ;
2849 return COMPCODE_UNLE;
2851 return COMPCODE_UNGT;
2853 return COMPCODE_LTGT;
2855 return COMPCODE_UNGE;
2861 /* Convert a compcode bit-based encoding of a comparison operator back
2862 to GCC's enum tree_code representation. This function is the
2863 inverse of comparison_to_compcode. */
2865 static enum tree_code
2866 compcode_to_comparison (enum comparison_code code)
2883 return ORDERED_EXPR;
2884 case COMPCODE_UNORD:
2885 return UNORDERED_EXPR;
2903 /* Return a tree for the comparison which is the combination of
2904 doing the AND or OR (depending on CODE) of the two operations LCODE
2905 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2906 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2907 if this makes the transformation invalid. */
2910 combine_comparisons (enum tree_code code, enum tree_code lcode,
2911 enum tree_code rcode, tree truth_type,
2912 tree ll_arg, tree lr_arg)
2914 bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2915 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2916 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2917 enum comparison_code compcode;
2921 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2922 compcode = lcompcode & rcompcode;
2925 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2926 compcode = lcompcode | rcompcode;
2935 /* Eliminate unordered comparisons, as well as LTGT and ORD
2936 which are not used unless the mode has NaNs. */
2937 compcode &= ~COMPCODE_UNORD;
2938 if (compcode == COMPCODE_LTGT)
2939 compcode = COMPCODE_NE;
2940 else if (compcode == COMPCODE_ORD)
2941 compcode = COMPCODE_TRUE;
2943 else if (flag_trapping_math)
2945 /* Check that the original operation and the optimized ones will trap
2946 under the same condition. */
2947 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2948 && (lcompcode != COMPCODE_EQ)
2949 && (lcompcode != COMPCODE_ORD);
2950 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2951 && (rcompcode != COMPCODE_EQ)
2952 && (rcompcode != COMPCODE_ORD);
2953 bool trap = (compcode & COMPCODE_UNORD) == 0
2954 && (compcode != COMPCODE_EQ)
2955 && (compcode != COMPCODE_ORD);
2957 /* In a short-circuited boolean expression the LHS might be
2958 such that the RHS, if evaluated, will never trap. For
2959 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2960 if neither x nor y is NaN. (This is a mixed blessing: for
2961 example, the expression above will never trap, hence
2962 optimizing it to x < y would be invalid). */
2963 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2964 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2967 /* If the comparison was short-circuited, and only the RHS
2968 trapped, we may now generate a spurious trap. */
2970 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2973 /* If we changed the conditions that cause a trap, we lose. */
2974 if ((ltrap || rtrap) != trap)
2978 if (compcode == COMPCODE_TRUE)
2979 return constant_boolean_node (true, truth_type);
2980 else if (compcode == COMPCODE_FALSE)
2981 return constant_boolean_node (false, truth_type);
2983 return fold_build2 (compcode_to_comparison (compcode),
2984 truth_type, ll_arg, lr_arg);
2987 /* Return nonzero if two operands (typically of the same tree node)
2988 are necessarily equal. If either argument has side-effects this
2989 function returns zero. FLAGS modifies behavior as follows:
2991 If OEP_ONLY_CONST is set, only return nonzero for constants.
2992 This function tests whether the operands are indistinguishable;
2993 it does not test whether they are equal using C's == operation.
2994 The distinction is important for IEEE floating point, because
2995 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2996 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2998 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2999 even though it may hold multiple values during a function.
3000 This is because a GCC tree node guarantees that nothing else is
3001 executed between the evaluation of its "operands" (which may often
3002 be evaluated in arbitrary order). Hence if the operands themselves
3003 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
3004 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
3005 unset means assuming isochronic (or instantaneous) tree equivalence.
3006 Unless comparing arbitrary expression trees, such as from different
3007 statements, this flag can usually be left unset.
3009 If OEP_PURE_SAME is set, then pure functions with identical arguments
3010 are considered the same. It is used when the caller has other ways
3011 to ensure that global memory is unchanged in between. */
3014 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
3016 /* If either is ERROR_MARK, they aren't equal. */
3017 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK)
3020 /* Check equality of integer constants before bailing out due to
3021 precision differences. */
3022 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3023 return tree_int_cst_equal (arg0, arg1);
3025 /* If both types don't have the same signedness, then we can't consider
3026 them equal. We must check this before the STRIP_NOPS calls
3027 because they may change the signedness of the arguments. As pointers
3028 strictly don't have a signedness, require either two pointers or
3029 two non-pointers as well. */
3030 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
3031 || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
3034 /* If both types don't have the same precision, then it is not safe
3036 if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
3042 /* In case both args are comparisons but with different comparison
3043 code, try to swap the comparison operands of one arg to produce
3044 a match and compare that variant. */
3045 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3046 && COMPARISON_CLASS_P (arg0)
3047 && COMPARISON_CLASS_P (arg1))
3049 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3051 if (TREE_CODE (arg0) == swap_code)
3052 return operand_equal_p (TREE_OPERAND (arg0, 0),
3053 TREE_OPERAND (arg1, 1), flags)
3054 && operand_equal_p (TREE_OPERAND (arg0, 1),
3055 TREE_OPERAND (arg1, 0), flags);
3058 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3059 /* This is needed for conversions and for COMPONENT_REF.
3060 Might as well play it safe and always test this. */
3061 || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
3062 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
3063 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
3066 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3067 We don't care about side effects in that case because the SAVE_EXPR
3068 takes care of that for us. In all other cases, two expressions are
3069 equal if they have no side effects. If we have two identical
3070 expressions with side effects that should be treated the same due
3071 to the only side effects being identical SAVE_EXPR's, that will
3072 be detected in the recursive calls below. */
3073 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3074 && (TREE_CODE (arg0) == SAVE_EXPR
3075 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3078 /* Next handle constant cases, those for which we can return 1 even
3079 if ONLY_CONST is set. */
3080 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3081 switch (TREE_CODE (arg0))
3084 return tree_int_cst_equal (arg0, arg1);
3087 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3088 TREE_FIXED_CST (arg1));
3091 if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
3092 TREE_REAL_CST (arg1)))
3096 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
3098 /* If we do not distinguish between signed and unsigned zero,
3099 consider them equal. */
3100 if (real_zerop (arg0) && real_zerop (arg1))
3109 v1 = TREE_VECTOR_CST_ELTS (arg0);
3110 v2 = TREE_VECTOR_CST_ELTS (arg1);
3113 if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
3116 v1 = TREE_CHAIN (v1);
3117 v2 = TREE_CHAIN (v2);
3124 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3126 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3130 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3131 && ! memcmp (TREE_STRING_POINTER (arg0),
3132 TREE_STRING_POINTER (arg1),
3133 TREE_STRING_LENGTH (arg0)));
3136 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3142 if (flags & OEP_ONLY_CONST)
3145 /* Define macros to test an operand from arg0 and arg1 for equality and a
3146 variant that allows null and views null as being different from any
3147 non-null value. In the latter case, if either is null, the both
3148 must be; otherwise, do the normal comparison. */
3149 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3150 TREE_OPERAND (arg1, N), flags)
3152 #define OP_SAME_WITH_NULL(N) \
3153 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3154 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3156 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3159 /* Two conversions are equal only if signedness and modes match. */
3160 switch (TREE_CODE (arg0))
3163 case FIX_TRUNC_EXPR:
3164 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3165 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3175 case tcc_comparison:
3177 if (OP_SAME (0) && OP_SAME (1))
3180 /* For commutative ops, allow the other order. */
3181 return (commutative_tree_code (TREE_CODE (arg0))
3182 && operand_equal_p (TREE_OPERAND (arg0, 0),
3183 TREE_OPERAND (arg1, 1), flags)
3184 && operand_equal_p (TREE_OPERAND (arg0, 1),
3185 TREE_OPERAND (arg1, 0), flags));
3188 /* If either of the pointer (or reference) expressions we are
3189 dereferencing contain a side effect, these cannot be equal. */
3190 if (TREE_SIDE_EFFECTS (arg0)
3191 || TREE_SIDE_EFFECTS (arg1))
3194 switch (TREE_CODE (arg0))
3197 case ALIGN_INDIRECT_REF:
3198 case MISALIGNED_INDIRECT_REF:
3204 case ARRAY_RANGE_REF:
3205 /* Operands 2 and 3 may be null.
3206 Compare the array index by value if it is constant first as we
3207 may have different types but same value here. */
3209 && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3210 TREE_OPERAND (arg1, 1))
3212 && OP_SAME_WITH_NULL (2)
3213 && OP_SAME_WITH_NULL (3));
3216 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3217 may be NULL when we're called to compare MEM_EXPRs. */
3218 return OP_SAME_WITH_NULL (0)
3220 && OP_SAME_WITH_NULL (2);
3223 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3229 case tcc_expression:
3230 switch (TREE_CODE (arg0))
3233 case TRUTH_NOT_EXPR:
3236 case TRUTH_ANDIF_EXPR:
3237 case TRUTH_ORIF_EXPR:
3238 return OP_SAME (0) && OP_SAME (1);
3240 case TRUTH_AND_EXPR:
3242 case TRUTH_XOR_EXPR:
3243 if (OP_SAME (0) && OP_SAME (1))
3246 /* Otherwise take into account this is a commutative operation. */
3247 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3248 TREE_OPERAND (arg1, 1), flags)
3249 && operand_equal_p (TREE_OPERAND (arg0, 1),
3250 TREE_OPERAND (arg1, 0), flags));
3253 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3260 switch (TREE_CODE (arg0))
3263 /* If the CALL_EXPRs call different functions, then they
3264 clearly can not be equal. */
3265 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3270 unsigned int cef = call_expr_flags (arg0);
3271 if (flags & OEP_PURE_SAME)
3272 cef &= ECF_CONST | ECF_PURE;
3279 /* Now see if all the arguments are the same. */
3281 const_call_expr_arg_iterator iter0, iter1;
3283 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3284 a1 = first_const_call_expr_arg (arg1, &iter1);
3286 a0 = next_const_call_expr_arg (&iter0),
3287 a1 = next_const_call_expr_arg (&iter1))
3288 if (! operand_equal_p (a0, a1, flags))
3291 /* If we get here and both argument lists are exhausted
3292 then the CALL_EXPRs are equal. */
3293 return ! (a0 || a1);
3299 case tcc_declaration:
3300 /* Consider __builtin_sqrt equal to sqrt. */
3301 return (TREE_CODE (arg0) == FUNCTION_DECL
3302 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3303 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3304 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3311 #undef OP_SAME_WITH_NULL
3314 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3315 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3317 When in doubt, return 0. */
3320 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3322 int unsignedp1, unsignedpo;
3323 tree primarg0, primarg1, primother;
3324 unsigned int correct_width;
3326 if (operand_equal_p (arg0, arg1, 0))
3329 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3330 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3333 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3334 and see if the inner values are the same. This removes any
3335 signedness comparison, which doesn't matter here. */
3336 primarg0 = arg0, primarg1 = arg1;
3337 STRIP_NOPS (primarg0);
3338 STRIP_NOPS (primarg1);
3339 if (operand_equal_p (primarg0, primarg1, 0))
3342 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3343 actual comparison operand, ARG0.
3345 First throw away any conversions to wider types
3346 already present in the operands. */
3348 primarg1 = get_narrower (arg1, &unsignedp1);
3349 primother = get_narrower (other, &unsignedpo);
3351 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3352 if (unsignedp1 == unsignedpo
3353 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3354 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3356 tree type = TREE_TYPE (arg0);
3358 /* Make sure shorter operand is extended the right way
3359 to match the longer operand. */
3360 primarg1 = fold_convert (signed_or_unsigned_type_for
3361 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3363 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3370 /* See if ARG is an expression that is either a comparison or is performing
3371 arithmetic on comparisons. The comparisons must only be comparing
3372 two different values, which will be stored in *CVAL1 and *CVAL2; if
3373 they are nonzero it means that some operands have already been found.
3374 No variables may be used anywhere else in the expression except in the
3375 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3376 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3378 If this is true, return 1. Otherwise, return zero. */
3381 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3383 enum tree_code code = TREE_CODE (arg);
3384 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3386 /* We can handle some of the tcc_expression cases here. */
3387 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3389 else if (tclass == tcc_expression
3390 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3391 || code == COMPOUND_EXPR))
3392 tclass = tcc_binary;
3394 else if (tclass == tcc_expression && code == SAVE_EXPR
3395 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3397 /* If we've already found a CVAL1 or CVAL2, this expression is
3398 two complex to handle. */
3399 if (*cval1 || *cval2)
3409 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3412 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3413 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3414 cval1, cval2, save_p));
3419 case tcc_expression:
3420 if (code == COND_EXPR)
3421 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3422 cval1, cval2, save_p)
3423 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3424 cval1, cval2, save_p)
3425 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3426 cval1, cval2, save_p));
3429 case tcc_comparison:
3430 /* First see if we can handle the first operand, then the second. For
3431 the second operand, we know *CVAL1 can't be zero. It must be that
3432 one side of the comparison is each of the values; test for the
3433 case where this isn't true by failing if the two operands
3436 if (operand_equal_p (TREE_OPERAND (arg, 0),
3437 TREE_OPERAND (arg, 1), 0))
3441 *cval1 = TREE_OPERAND (arg, 0);
3442 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3444 else if (*cval2 == 0)
3445 *cval2 = TREE_OPERAND (arg, 0);
3446 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3451 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3453 else if (*cval2 == 0)
3454 *cval2 = TREE_OPERAND (arg, 1);
3455 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3467 /* ARG is a tree that is known to contain just arithmetic operations and
3468 comparisons. Evaluate the operations in the tree substituting NEW0 for
3469 any occurrence of OLD0 as an operand of a comparison and likewise for
3473 eval_subst (tree arg, tree old0, tree new0, tree old1, tree new1)
3475 tree type = TREE_TYPE (arg);
3476 enum tree_code code = TREE_CODE (arg);
3477 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3479 /* We can handle some of the tcc_expression cases here. */
3480 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3482 else if (tclass == tcc_expression
3483 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3484 tclass = tcc_binary;
3489 return fold_build1 (code, type,
3490 eval_subst (TREE_OPERAND (arg, 0),
3491 old0, new0, old1, new1));
3494 return fold_build2 (code, type,
3495 eval_subst (TREE_OPERAND (arg, 0),
3496 old0, new0, old1, new1),
3497 eval_subst (TREE_OPERAND (arg, 1),
3498 old0, new0, old1, new1));
3500 case tcc_expression:
3504 return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
3507 return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
3510 return fold_build3 (code, type,
3511 eval_subst (TREE_OPERAND (arg, 0),
3512 old0, new0, old1, new1),
3513 eval_subst (TREE_OPERAND (arg, 1),
3514 old0, new0, old1, new1),
3515 eval_subst (TREE_OPERAND (arg, 2),
3516 old0, new0, old1, new1));
3520 /* Fall through - ??? */
3522 case tcc_comparison:
3524 tree arg0 = TREE_OPERAND (arg, 0);
3525 tree arg1 = TREE_OPERAND (arg, 1);
3527 /* We need to check both for exact equality and tree equality. The
3528 former will be true if the operand has a side-effect. In that
3529 case, we know the operand occurred exactly once. */
3531 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3533 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3536 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3538 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3541 return fold_build2 (code, type, arg0, arg1);
3549 /* Return a tree for the case when the result of an expression is RESULT
3550 converted to TYPE and OMITTED was previously an operand of the expression
3551 but is now not needed (e.g., we folded OMITTED * 0).
3553 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3554 the conversion of RESULT to TYPE. */
3557 omit_one_operand (tree type, tree result, tree omitted)
3559 tree t = fold_convert (type, result);
3561 /* If the resulting operand is an empty statement, just return the omitted
3562 statement casted to void. */
3563 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3564 return build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3566 if (TREE_SIDE_EFFECTS (omitted))
3567 return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3569 return non_lvalue (t);
3572 /* Similar, but call pedantic_non_lvalue instead of non_lvalue. */
3575 pedantic_omit_one_operand (tree type, tree result, tree omitted)
3577 tree t = fold_convert (type, result);
3579 /* If the resulting operand is an empty statement, just return the omitted
3580 statement casted to void. */
3581 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3582 return build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3584 if (TREE_SIDE_EFFECTS (omitted))
3585 return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3587 return pedantic_non_lvalue (t);
3590 /* Return a tree for the case when the result of an expression is RESULT
3591 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3592 of the expression but are now not needed.
3594 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3595 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3596 evaluated before OMITTED2. Otherwise, if neither has side effects,
3597 just do the conversion of RESULT to TYPE. */
3600 omit_two_operands (tree type, tree result, tree omitted1, tree omitted2)
3602 tree t = fold_convert (type, result);
3604 if (TREE_SIDE_EFFECTS (omitted2))
3605 t = build2 (COMPOUND_EXPR, type, omitted2, t);
3606 if (TREE_SIDE_EFFECTS (omitted1))
3607 t = build2 (COMPOUND_EXPR, type, omitted1, t);
3609 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue (t) : t;
3613 /* Return a simplified tree node for the truth-negation of ARG. This
3614 never alters ARG itself. We assume that ARG is an operation that
3615 returns a truth value (0 or 1).
3617 FIXME: one would think we would fold the result, but it causes
3618 problems with the dominator optimizer. */
3621 fold_truth_not_expr (tree arg)
3623 tree type = TREE_TYPE (arg);
3624 enum tree_code code = TREE_CODE (arg);
3626 /* If this is a comparison, we can simply invert it, except for
3627 floating-point non-equality comparisons, in which case we just
3628 enclose a TRUTH_NOT_EXPR around what we have. */
3630 if (TREE_CODE_CLASS (code) == tcc_comparison)
3632 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3633 if (FLOAT_TYPE_P (op_type)
3634 && flag_trapping_math
3635 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3636 && code != NE_EXPR && code != EQ_EXPR)
3640 code = invert_tree_comparison (code,
3641 HONOR_NANS (TYPE_MODE (op_type)));
3642 if (code == ERROR_MARK)
3645 return build2 (code, type,
3646 TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
3653 return constant_boolean_node (integer_zerop (arg), type);
3655 case TRUTH_AND_EXPR:
3656 return build2 (TRUTH_OR_EXPR, type,
3657 invert_truthvalue (TREE_OPERAND (arg, 0)),
3658 invert_truthvalue (TREE_OPERAND (arg, 1)));
3661 return build2 (TRUTH_AND_EXPR, type,
3662 invert_truthvalue (TREE_OPERAND (arg, 0)),
3663 invert_truthvalue (TREE_OPERAND (arg, 1)));
3665 case TRUTH_XOR_EXPR:
3666 /* Here we can invert either operand. We invert the first operand
3667 unless the second operand is a TRUTH_NOT_EXPR in which case our
3668 result is the XOR of the first operand with the inside of the
3669 negation of the second operand. */
3671 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3672 return build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3673 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3675 return build2 (TRUTH_XOR_EXPR, type,
3676 invert_truthvalue (TREE_OPERAND (arg, 0)),
3677 TREE_OPERAND (arg, 1));
3679 case TRUTH_ANDIF_EXPR:
3680 return build2 (TRUTH_ORIF_EXPR, type,
3681 invert_truthvalue (TREE_OPERAND (arg, 0)),
3682 invert_truthvalue (TREE_OPERAND (arg, 1)));
3684 case TRUTH_ORIF_EXPR:
3685 return build2 (TRUTH_ANDIF_EXPR, type,
3686 invert_truthvalue (TREE_OPERAND (arg, 0)),
3687 invert_truthvalue (TREE_OPERAND (arg, 1)));
3689 case TRUTH_NOT_EXPR:
3690 return TREE_OPERAND (arg, 0);
3694 tree arg1 = TREE_OPERAND (arg, 1);
3695 tree arg2 = TREE_OPERAND (arg, 2);
3696 /* A COND_EXPR may have a throw as one operand, which
3697 then has void type. Just leave void operands
3699 return build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
3700 VOID_TYPE_P (TREE_TYPE (arg1))
3701 ? arg1 : invert_truthvalue (arg1),
3702 VOID_TYPE_P (TREE_TYPE (arg2))
3703 ? arg2 : invert_truthvalue (arg2));
3707 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
3708 invert_truthvalue (TREE_OPERAND (arg, 1)));
3710 case NON_LVALUE_EXPR:
3711 return invert_truthvalue (TREE_OPERAND (arg, 0));
3714 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3715 return build1 (TRUTH_NOT_EXPR, type, arg);
3719 return build1 (TREE_CODE (arg), type,
3720 invert_truthvalue (TREE_OPERAND (arg, 0)));
3723 if (!integer_onep (TREE_OPERAND (arg, 1)))
3725 return build2 (EQ_EXPR, type, arg,
3726 build_int_cst (type, 0));
3729 return build1 (TRUTH_NOT_EXPR, type, arg);
3731 case CLEANUP_POINT_EXPR:
3732 return build1 (CLEANUP_POINT_EXPR, type,
3733 invert_truthvalue (TREE_OPERAND (arg, 0)));
3742 /* Return a simplified tree node for the truth-negation of ARG. This
3743 never alters ARG itself. We assume that ARG is an operation that
3744 returns a truth value (0 or 1).
3746 FIXME: one would think we would fold the result, but it causes
3747 problems with the dominator optimizer. */
3750 invert_truthvalue (tree arg)
3754 if (TREE_CODE (arg) == ERROR_MARK)
3757 tem = fold_truth_not_expr (arg);
3759 tem = build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
3764 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3765 operands are another bit-wise operation with a common input. If so,
3766 distribute the bit operations to save an operation and possibly two if
3767 constants are involved. For example, convert
3768 (A | B) & (A | C) into A | (B & C)
3769 Further simplification will occur if B and C are constants.
3771 If this optimization cannot be done, 0 will be returned. */
3774 distribute_bit_expr (enum tree_code code, tree type, tree arg0, tree arg1)
3779 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3780 || TREE_CODE (arg0) == code
3781 || (TREE_CODE (arg0) != BIT_AND_EXPR
3782 && TREE_CODE (arg0) != BIT_IOR_EXPR))
3785 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3787 common = TREE_OPERAND (arg0, 0);
3788 left = TREE_OPERAND (arg0, 1);
3789 right = TREE_OPERAND (arg1, 1);
3791 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3793 common = TREE_OPERAND (arg0, 0);
3794 left = TREE_OPERAND (arg0, 1);
3795 right = TREE_OPERAND (arg1, 0);
3797 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3799 common = TREE_OPERAND (arg0, 1);
3800 left = TREE_OPERAND (arg0, 0);
3801 right = TREE_OPERAND (arg1, 1);
3803 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3805 common = TREE_OPERAND (arg0, 1);
3806 left = TREE_OPERAND (arg0, 0);
3807 right = TREE_OPERAND (arg1, 0);
3812 common = fold_convert (type, common);
3813 left = fold_convert (type, left);
3814 right = fold_convert (type, right);
3815 return fold_build2 (TREE_CODE (arg0), type, common,
3816 fold_build2 (code, type, left, right));
3819 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3820 with code CODE. This optimization is unsafe. */
3822 distribute_real_division (enum tree_code code, tree type, tree arg0, tree arg1)
3824 bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3825 bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3827 /* (A / C) +- (B / C) -> (A +- B) / C. */
3829 && operand_equal_p (TREE_OPERAND (arg0, 1),
3830 TREE_OPERAND (arg1, 1), 0))
3831 return fold_build2 (mul0 ? MULT_EXPR : RDIV_EXPR, type,
3832 fold_build2 (code, type,
3833 TREE_OPERAND (arg0, 0),
3834 TREE_OPERAND (arg1, 0)),
3835 TREE_OPERAND (arg0, 1));
3837 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3838 if (operand_equal_p (TREE_OPERAND (arg0, 0),
3839 TREE_OPERAND (arg1, 0), 0)
3840 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3841 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3843 REAL_VALUE_TYPE r0, r1;
3844 r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3845 r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3847 real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3849 real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3850 real_arithmetic (&r0, code, &r0, &r1);
3851 return fold_build2 (MULT_EXPR, type,
3852 TREE_OPERAND (arg0, 0),
3853 build_real (type, r0));
3859 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3860 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero. */
3863 make_bit_field_ref (tree inner, tree type, HOST_WIDE_INT bitsize,
3864 HOST_WIDE_INT bitpos, int unsignedp)
3866 tree result, bftype;
3870 tree size = TYPE_SIZE (TREE_TYPE (inner));
3871 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3872 || POINTER_TYPE_P (TREE_TYPE (inner)))
3873 && host_integerp (size, 0)
3874 && tree_low_cst (size, 0) == bitsize)
3875 return fold_convert (type, inner);
3879 if (TYPE_PRECISION (bftype) != bitsize
3880 || TYPE_UNSIGNED (bftype) == !unsignedp)
3881 bftype = build_nonstandard_integer_type (bitsize, 0);
3883 result = build3 (BIT_FIELD_REF, bftype, inner,
3884 size_int (bitsize), bitsize_int (bitpos));
3887 result = fold_convert (type, result);
3892 /* Optimize a bit-field compare.
3894 There are two cases: First is a compare against a constant and the
3895 second is a comparison of two items where the fields are at the same
3896 bit position relative to the start of a chunk (byte, halfword, word)
3897 large enough to contain it. In these cases we can avoid the shift
3898 implicit in bitfield extractions.
3900 For constants, we emit a compare of the shifted constant with the
3901 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3902 compared. For two fields at the same position, we do the ANDs with the
3903 similar mask and compare the result of the ANDs.
3905 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3906 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3907 are the left and right operands of the comparison, respectively.
3909 If the optimization described above can be done, we return the resulting
3910 tree. Otherwise we return zero. */
3913 optimize_bit_field_compare (enum tree_code code, tree compare_type,
3916 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3917 tree type = TREE_TYPE (lhs);
3918 tree signed_type, unsigned_type;
3919 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3920 enum machine_mode lmode, rmode, nmode;
3921 int lunsignedp, runsignedp;
3922 int lvolatilep = 0, rvolatilep = 0;
3923 tree linner, rinner = NULL_TREE;
3927 /* Get all the information about the extractions being done. If the bit size
3928 if the same as the size of the underlying object, we aren't doing an
3929 extraction at all and so can do nothing. We also don't want to
3930 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3931 then will no longer be able to replace it. */
3932 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3933 &lunsignedp, &lvolatilep, false);
3934 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3935 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3940 /* If this is not a constant, we can only do something if bit positions,
3941 sizes, and signedness are the same. */
3942 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3943 &runsignedp, &rvolatilep, false);
3945 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3946 || lunsignedp != runsignedp || offset != 0
3947 || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
3951 /* See if we can find a mode to refer to this field. We should be able to,
3952 but fail if we can't. */
3953 nmode = get_best_mode (lbitsize, lbitpos,
3954 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3955 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3956 TYPE_ALIGN (TREE_TYPE (rinner))),
3957 word_mode, lvolatilep || rvolatilep);
3958 if (nmode == VOIDmode)
3961 /* Set signed and unsigned types of the precision of this mode for the
3963 signed_type = lang_hooks.types.type_for_mode (nmode, 0);
3964 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3966 /* Compute the bit position and size for the new reference and our offset
3967 within it. If the new reference is the same size as the original, we
3968 won't optimize anything, so return zero. */
3969 nbitsize = GET_MODE_BITSIZE (nmode);
3970 nbitpos = lbitpos & ~ (nbitsize - 1);
3972 if (nbitsize == lbitsize)
3975 if (BYTES_BIG_ENDIAN)
3976 lbitpos = nbitsize - lbitsize - lbitpos;
3978 /* Make the mask to be used against the extracted field. */
3979 mask = build_int_cst_type (unsigned_type, -1);
3980 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
3981 mask = const_binop (RSHIFT_EXPR, mask,
3982 size_int (nbitsize - lbitsize - lbitpos), 0);
3985 /* If not comparing with constant, just rework the comparison
3987 return fold_build2 (code, compare_type,
3988 fold_build2 (BIT_AND_EXPR, unsigned_type,
3989 make_bit_field_ref (linner,
3994 fold_build2 (BIT_AND_EXPR, unsigned_type,
3995 make_bit_field_ref (rinner,
4001 /* Otherwise, we are handling the constant case. See if the constant is too
4002 big for the field. Warn and return a tree of for 0 (false) if so. We do
4003 this not only for its own sake, but to avoid having to test for this
4004 error case below. If we didn't, we might generate wrong code.
4006 For unsigned fields, the constant shifted right by the field length should
4007 be all zero. For signed fields, the high-order bits should agree with
4012 if (! integer_zerop (const_binop (RSHIFT_EXPR,
4013 fold_convert (unsigned_type, rhs),
4014 size_int (lbitsize), 0)))
4016 warning (0, "comparison is always %d due to width of bit-field",
4018 return constant_boolean_node (code == NE_EXPR, compare_type);
4023 tree tem = const_binop (RSHIFT_EXPR, fold_convert (signed_type, rhs),
4024 size_int (lbitsize - 1), 0);
4025 if (! integer_zerop (tem) && ! integer_all_onesp (tem))
4027 warning (0, "comparison is always %d due to width of bit-field",
4029 return constant_boolean_node (code == NE_EXPR, compare_type);
4033 /* Single-bit compares should always be against zero. */
4034 if (lbitsize == 1 && ! integer_zerop (rhs))
4036 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4037 rhs = build_int_cst (type, 0);
4040 /* Make a new bitfield reference, shift the constant over the
4041 appropriate number of bits and mask it with the computed mask
4042 (in case this was a signed field). If we changed it, make a new one. */
4043 lhs = make_bit_field_ref (linner, unsigned_type, nbitsize, nbitpos, 1);
4046 TREE_SIDE_EFFECTS (lhs) = 1;
4047 TREE_THIS_VOLATILE (lhs) = 1;
4050 rhs = const_binop (BIT_AND_EXPR,
4051 const_binop (LSHIFT_EXPR,
4052 fold_convert (unsigned_type, rhs),
4053 size_int (lbitpos), 0),
4056 return build2 (code, compare_type,
4057 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask),
4061 /* Subroutine for fold_truthop: decode a field reference.
4063 If EXP is a comparison reference, we return the innermost reference.
4065 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4066 set to the starting bit number.
4068 If the innermost field can be completely contained in a mode-sized
4069 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4071 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4072 otherwise it is not changed.
4074 *PUNSIGNEDP is set to the signedness of the field.
4076 *PMASK is set to the mask used. This is either contained in a
4077 BIT_AND_EXPR or derived from the width of the field.
4079 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4081 Return 0 if this is not a component reference or is one that we can't
4082 do anything with. */
4085 decode_field_reference (tree exp, HOST_WIDE_INT *pbitsize,
4086 HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
4087 int *punsignedp, int *pvolatilep,
4088 tree *pmask, tree *pand_mask)
4090 tree outer_type = 0;
4092 tree mask, inner, offset;
4094 unsigned int precision;
4096 /* All the optimizations using this function assume integer fields.
4097 There are problems with FP fields since the type_for_size call
4098 below can fail for, e.g., XFmode. */
4099 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4102 /* We are interested in the bare arrangement of bits, so strip everything
4103 that doesn't affect the machine mode. However, record the type of the
4104 outermost expression if it may matter below. */
4105 if (CONVERT_EXPR_P (exp)
4106 || TREE_CODE (exp) == NON_LVALUE_EXPR)
4107 outer_type = TREE_TYPE (exp);
4110 if (TREE_CODE (exp) == BIT_AND_EXPR)
4112 and_mask = TREE_OPERAND (exp, 1);
4113 exp = TREE_OPERAND (exp, 0);
4114 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4115 if (TREE_CODE (and_mask) != INTEGER_CST)
4119 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
4120 punsignedp, pvolatilep, false);
4121 if ((inner == exp && and_mask == 0)
4122 || *pbitsize < 0 || offset != 0
4123 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
4126 /* If the number of bits in the reference is the same as the bitsize of
4127 the outer type, then the outer type gives the signedness. Otherwise
4128 (in case of a small bitfield) the signedness is unchanged. */
4129 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4130 *punsignedp = TYPE_UNSIGNED (outer_type);
4132 /* Compute the mask to access the bitfield. */
4133 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4134 precision = TYPE_PRECISION (unsigned_type);
4136 mask = build_int_cst_type (unsigned_type, -1);
4138 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
4139 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
4141 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4143 mask = fold_build2 (BIT_AND_EXPR, unsigned_type,
4144 fold_convert (unsigned_type, and_mask), mask);
4147 *pand_mask = and_mask;
4151 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4155 all_ones_mask_p (const_tree mask, int size)
4157 tree type = TREE_TYPE (mask);
4158 unsigned int precision = TYPE_PRECISION (type);
4161 tmask = build_int_cst_type (signed_type_for (type), -1);
4164 tree_int_cst_equal (mask,
4165 const_binop (RSHIFT_EXPR,
4166 const_binop (LSHIFT_EXPR, tmask,
4167 size_int (precision - size),
4169 size_int (precision - size), 0));
4172 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4173 represents the sign bit of EXP's type. If EXP represents a sign
4174 or zero extension, also test VAL against the unextended type.
4175 The return value is the (sub)expression whose sign bit is VAL,
4176 or NULL_TREE otherwise. */
4179 sign_bit_p (tree exp, const_tree val)
4181 unsigned HOST_WIDE_INT mask_lo, lo;
4182 HOST_WIDE_INT mask_hi, hi;
4186 /* Tree EXP must have an integral type. */
4187 t = TREE_TYPE (exp);
4188 if (! INTEGRAL_TYPE_P (t))
4191 /* Tree VAL must be an integer constant. */
4192 if (TREE_CODE (val) != INTEGER_CST
4193 || TREE_OVERFLOW (val))
4196 width = TYPE_PRECISION (t);
4197 if (width > HOST_BITS_PER_WIDE_INT)
4199 hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
4202 mask_hi = ((unsigned HOST_WIDE_INT) -1
4203 >> (2 * HOST_BITS_PER_WIDE_INT - width));
4209 lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
4212 mask_lo = ((unsigned HOST_WIDE_INT) -1
4213 >> (HOST_BITS_PER_WIDE_INT - width));
4216 /* We mask off those bits beyond TREE_TYPE (exp) so that we can
4217 treat VAL as if it were unsigned. */
4218 if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
4219 && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
4222 /* Handle extension from a narrower type. */
4223 if (TREE_CODE (exp) == NOP_EXPR
4224 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4225 return sign_bit_p (TREE_OPERAND (exp, 0), val);
4230 /* Subroutine for fold_truthop: determine if an operand is simple enough
4231 to be evaluated unconditionally. */
4234 simple_operand_p (const_tree exp)
4236 /* Strip any conversions that don't change the machine mode. */
4239 return (CONSTANT_CLASS_P (exp)
4240 || TREE_CODE (exp) == SSA_NAME
4242 && ! TREE_ADDRESSABLE (exp)
4243 && ! TREE_THIS_VOLATILE (exp)
4244 && ! DECL_NONLOCAL (exp)
4245 /* Don't regard global variables as simple. They may be
4246 allocated in ways unknown to the compiler (shared memory,
4247 #pragma weak, etc). */
4248 && ! TREE_PUBLIC (exp)
4249 && ! DECL_EXTERNAL (exp)
4250 /* Loading a static variable is unduly expensive, but global
4251 registers aren't expensive. */
4252 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4255 /* The following functions are subroutines to fold_range_test and allow it to
4256 try to change a logical combination of comparisons into a range test.
4259 X == 2 || X == 3 || X == 4 || X == 5
4263 (unsigned) (X - 2) <= 3
4265 We describe each set of comparisons as being either inside or outside
4266 a range, using a variable named like IN_P, and then describe the
4267 range with a lower and upper bound. If one of the bounds is omitted,
4268 it represents either the highest or lowest value of the type.
4270 In the comments below, we represent a range by two numbers in brackets
4271 preceded by a "+" to designate being inside that range, or a "-" to
4272 designate being outside that range, so the condition can be inverted by
4273 flipping the prefix. An omitted bound is represented by a "-". For
4274 example, "- [-, 10]" means being outside the range starting at the lowest
4275 possible value and ending at 10, in other words, being greater than 10.
4276 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4279 We set up things so that the missing bounds are handled in a consistent
4280 manner so neither a missing bound nor "true" and "false" need to be
4281 handled using a special case. */
4283 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4284 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4285 and UPPER1_P are nonzero if the respective argument is an upper bound
4286 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4287 must be specified for a comparison. ARG1 will be converted to ARG0's
4288 type if both are specified. */
4291 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4292 tree arg1, int upper1_p)
4298 /* If neither arg represents infinity, do the normal operation.
4299 Else, if not a comparison, return infinity. Else handle the special
4300 comparison rules. Note that most of the cases below won't occur, but
4301 are handled for consistency. */
4303 if (arg0 != 0 && arg1 != 0)
4305 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4306 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4308 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4311 if (TREE_CODE_CLASS (code) != tcc_comparison)
4314 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4315 for neither. In real maths, we cannot assume open ended ranges are
4316 the same. But, this is computer arithmetic, where numbers are finite.
4317 We can therefore make the transformation of any unbounded range with
4318 the value Z, Z being greater than any representable number. This permits
4319 us to treat unbounded ranges as equal. */
4320 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4321 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4325 result = sgn0 == sgn1;
4328 result = sgn0 != sgn1;
4331 result = sgn0 < sgn1;
4334 result = sgn0 <= sgn1;
4337 result = sgn0 > sgn1;
4340 result = sgn0 >= sgn1;
4346 return constant_boolean_node (result, type);
4349 /* Given EXP, a logical expression, set the range it is testing into
4350 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4351 actually being tested. *PLOW and *PHIGH will be made of the same
4352 type as the returned expression. If EXP is not a comparison, we
4353 will most likely not be returning a useful value and range. Set
4354 *STRICT_OVERFLOW_P to true if the return value is only valid
4355 because signed overflow is undefined; otherwise, do not change
4356 *STRICT_OVERFLOW_P. */
4359 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4360 bool *strict_overflow_p)
4362 enum tree_code code;
4363 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
4364 tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
4366 tree low, high, n_low, n_high;
4368 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4369 and see if we can refine the range. Some of the cases below may not
4370 happen, but it doesn't seem worth worrying about this. We "continue"
4371 the outer loop when we've changed something; otherwise we "break"
4372 the switch, which will "break" the while. */
4375 low = high = build_int_cst (TREE_TYPE (exp), 0);
4379 code = TREE_CODE (exp);
4380 exp_type = TREE_TYPE (exp);
4382 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4384 if (TREE_OPERAND_LENGTH (exp) > 0)
4385 arg0 = TREE_OPERAND (exp, 0);
4386 if (TREE_CODE_CLASS (code) == tcc_comparison
4387 || TREE_CODE_CLASS (code) == tcc_unary
4388 || TREE_CODE_CLASS (code) == tcc_binary)
4389 arg0_type = TREE_TYPE (arg0);
4390 if (TREE_CODE_CLASS (code) == tcc_binary
4391 || TREE_CODE_CLASS (code) == tcc_comparison
4392 || (TREE_CODE_CLASS (code) == tcc_expression
4393 && TREE_OPERAND_LENGTH (exp) > 1))
4394 arg1 = TREE_OPERAND (exp, 1);
4399 case TRUTH_NOT_EXPR:
4400 in_p = ! in_p, exp = arg0;
4403 case EQ_EXPR: case NE_EXPR:
4404 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4405 /* We can only do something if the range is testing for zero
4406 and if the second operand is an integer constant. Note that
4407 saying something is "in" the range we make is done by
4408 complementing IN_P since it will set in the initial case of
4409 being not equal to zero; "out" is leaving it alone. */
4410 if (low == 0 || high == 0
4411 || ! integer_zerop (low) || ! integer_zerop (high)
4412 || TREE_CODE (arg1) != INTEGER_CST)
4417 case NE_EXPR: /* - [c, c] */
4420 case EQ_EXPR: /* + [c, c] */
4421 in_p = ! in_p, low = high = arg1;
4423 case GT_EXPR: /* - [-, c] */
4424 low = 0, high = arg1;
4426 case GE_EXPR: /* + [c, -] */
4427 in_p = ! in_p, low = arg1, high = 0;
4429 case LT_EXPR: /* - [c, -] */
4430 low = arg1, high = 0;
4432 case LE_EXPR: /* + [-, c] */
4433 in_p = ! in_p, low = 0, high = arg1;
4439 /* If this is an unsigned comparison, we also know that EXP is
4440 greater than or equal to zero. We base the range tests we make
4441 on that fact, so we record it here so we can parse existing
4442 range tests. We test arg0_type since often the return type
4443 of, e.g. EQ_EXPR, is boolean. */
4444 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4446 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4448 build_int_cst (arg0_type, 0),
4452 in_p = n_in_p, low = n_low, high = n_high;
4454 /* If the high bound is missing, but we have a nonzero low
4455 bound, reverse the range so it goes from zero to the low bound
4457 if (high == 0 && low && ! integer_zerop (low))
4460 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4461 integer_one_node, 0);
4462 low = build_int_cst (arg0_type, 0);
4470 /* (-x) IN [a,b] -> x in [-b, -a] */
4471 n_low = range_binop (MINUS_EXPR, exp_type,
4472 build_int_cst (exp_type, 0),
4474 n_high = range_binop (MINUS_EXPR, exp_type,
4475 build_int_cst (exp_type, 0),
4477 low = n_low, high = n_high;
4483 exp = build2 (MINUS_EXPR, exp_type, negate_expr (arg0),
4484 build_int_cst (exp_type, 1));
4487 case PLUS_EXPR: case MINUS_EXPR:
4488 if (TREE_CODE (arg1) != INTEGER_CST)
4491 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4492 move a constant to the other side. */
4493 if (!TYPE_UNSIGNED (arg0_type)
4494 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4497 /* If EXP is signed, any overflow in the computation is undefined,
4498 so we don't worry about it so long as our computations on
4499 the bounds don't overflow. For unsigned, overflow is defined
4500 and this is exactly the right thing. */
4501 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4502 arg0_type, low, 0, arg1, 0);
4503 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4504 arg0_type, high, 1, arg1, 0);
4505 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4506 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4509 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4510 *strict_overflow_p = true;
4512 /* Check for an unsigned range which has wrapped around the maximum
4513 value thus making n_high < n_low, and normalize it. */
4514 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4516 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4517 integer_one_node, 0);
4518 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4519 integer_one_node, 0);
4521 /* If the range is of the form +/- [ x+1, x ], we won't
4522 be able to normalize it. But then, it represents the
4523 whole range or the empty set, so make it
4525 if (tree_int_cst_equal (n_low, low)
4526 && tree_int_cst_equal (n_high, high))
4532 low = n_low, high = n_high;
4537 CASE_CONVERT: case NON_LVALUE_EXPR:
4538 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4541 if (! INTEGRAL_TYPE_P (arg0_type)
4542 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4543 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4546 n_low = low, n_high = high;
4549 n_low = fold_convert (arg0_type, n_low);
4552 n_high = fold_convert (arg0_type, n_high);
4555 /* If we're converting arg0 from an unsigned type, to exp,
4556 a signed type, we will be doing the comparison as unsigned.
4557 The tests above have already verified that LOW and HIGH
4560 So we have to ensure that we will handle large unsigned
4561 values the same way that the current signed bounds treat
4564 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4568 /* For fixed-point modes, we need to pass the saturating flag
4569 as the 2nd parameter. */
4570 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4571 equiv_type = lang_hooks.types.type_for_mode
4572 (TYPE_MODE (arg0_type),
4573 TYPE_SATURATING (arg0_type));
4575 equiv_type = lang_hooks.types.type_for_mode
4576 (TYPE_MODE (arg0_type), 1);
4578 /* A range without an upper bound is, naturally, unbounded.
4579 Since convert would have cropped a very large value, use
4580 the max value for the destination type. */
4582 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4583 : TYPE_MAX_VALUE (arg0_type);
4585 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4586 high_positive = fold_build2 (RSHIFT_EXPR, arg0_type,
4587 fold_convert (arg0_type,
4589 build_int_cst (arg0_type, 1));
4591 /* If the low bound is specified, "and" the range with the
4592 range for which the original unsigned value will be
4596 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4597 1, n_low, n_high, 1,
4598 fold_convert (arg0_type,
4603 in_p = (n_in_p == in_p);
4607 /* Otherwise, "or" the range with the range of the input
4608 that will be interpreted as negative. */
4609 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4610 0, n_low, n_high, 1,
4611 fold_convert (arg0_type,
4616 in_p = (in_p != n_in_p);
4621 low = n_low, high = n_high;
4631 /* If EXP is a constant, we can evaluate whether this is true or false. */
4632 if (TREE_CODE (exp) == INTEGER_CST)
4634 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4636 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4642 *pin_p = in_p, *plow = low, *phigh = high;
4646 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4647 type, TYPE, return an expression to test if EXP is in (or out of, depending
4648 on IN_P) the range. Return 0 if the test couldn't be created. */
4651 build_range_check (tree type, tree exp, int in_p, tree low, tree high)
4653 tree etype = TREE_TYPE (exp);
4656 #ifdef HAVE_canonicalize_funcptr_for_compare
4657 /* Disable this optimization for function pointer expressions
4658 on targets that require function pointer canonicalization. */
4659 if (HAVE_canonicalize_funcptr_for_compare
4660 && TREE_CODE (etype) == POINTER_TYPE
4661 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4667 value = build_range_check (type, exp, 1, low, high);
4669 return invert_truthvalue (value);
4674 if (low == 0 && high == 0)
4675 return build_int_cst (type, 1);
4678 return fold_build2 (LE_EXPR, type, exp,
4679 fold_convert (etype, high));
4682 return fold_build2 (GE_EXPR, type, exp,
4683 fold_convert (etype, low));
4685 if (operand_equal_p (low, high, 0))
4686 return fold_build2 (EQ_EXPR, type, exp,
4687 fold_convert (etype, low));
4689 if (integer_zerop (low))
4691 if (! TYPE_UNSIGNED (etype))
4693 etype = unsigned_type_for (etype);
4694 high = fold_convert (etype, high);
4695 exp = fold_convert (etype, exp);
4697 return build_range_check (type, exp, 1, 0, high);
4700 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4701 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4703 unsigned HOST_WIDE_INT lo;
4707 prec = TYPE_PRECISION (etype);
4708 if (prec <= HOST_BITS_PER_WIDE_INT)
4711 lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4715 hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4716 lo = (unsigned HOST_WIDE_INT) -1;
4719 if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4721 if (TYPE_UNSIGNED (etype))
4723 tree signed_etype = signed_type_for (etype);
4724 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4726 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4728 etype = signed_etype;
4729 exp = fold_convert (etype, exp);
4731 return fold_build2 (GT_EXPR, type, exp,
4732 build_int_cst (etype, 0));
4736 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4737 This requires wrap-around arithmetics for the type of the expression. */
4738 switch (TREE_CODE (etype))
4741 /* There is no requirement that LOW be within the range of ETYPE
4742 if the latter is a subtype. It must, however, be within the base
4743 type of ETYPE. So be sure we do the subtraction in that type. */
4744 if (TREE_TYPE (etype))
4745 etype = TREE_TYPE (etype);
4750 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4751 TYPE_UNSIGNED (etype));
4758 /* If we don't have wrap-around arithmetics upfront, try to force it. */
4759 if (TREE_CODE (etype) == INTEGER_TYPE
4760 && !TYPE_OVERFLOW_WRAPS (etype))
4762 tree utype, minv, maxv;
4764 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4765 for the type in question, as we rely on this here. */
4766 utype = unsigned_type_for (etype);
4767 maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
4768 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4769 integer_one_node, 1);
4770 minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
4772 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4779 high = fold_convert (etype, high);
4780 low = fold_convert (etype, low);
4781 exp = fold_convert (etype, exp);
4783 value = const_binop (MINUS_EXPR, high, low, 0);
4786 if (POINTER_TYPE_P (etype))
4788 if (value != 0 && !TREE_OVERFLOW (value))
4790 low = fold_convert (sizetype, low);
4791 low = fold_build1 (NEGATE_EXPR, sizetype, low);
4792 return build_range_check (type,
4793 fold_build2 (POINTER_PLUS_EXPR, etype, exp, low),
4794 1, build_int_cst (etype, 0), value);
4799 if (value != 0 && !TREE_OVERFLOW (value))
4800 return build_range_check (type,
4801 fold_build2 (MINUS_EXPR, etype, exp, low),
4802 1, build_int_cst (etype, 0), value);
4807 /* Return the predecessor of VAL in its type, handling the infinite case. */
4810 range_predecessor (tree val)
4812 tree type = TREE_TYPE (val);
4814 if (INTEGRAL_TYPE_P (type)
4815 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4818 return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4821 /* Return the successor of VAL in its type, handling the infinite case. */
4824 range_successor (tree val)
4826 tree type = TREE_TYPE (val);
4828 if (INTEGRAL_TYPE_P (type)
4829 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4832 return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4835 /* Given two ranges, see if we can merge them into one. Return 1 if we
4836 can, 0 if we can't. Set the output range into the specified parameters. */
4839 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4840 tree high0, int in1_p, tree low1, tree high1)
4848 int lowequal = ((low0 == 0 && low1 == 0)
4849 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4850 low0, 0, low1, 0)));
4851 int highequal = ((high0 == 0 && high1 == 0)
4852 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4853 high0, 1, high1, 1)));
4855 /* Make range 0 be the range that starts first, or ends last if they
4856 start at the same value. Swap them if it isn't. */
4857 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4860 && integer_onep (range_binop (GT_EXPR, integer_type_node,
4861 high1, 1, high0, 1))))
4863 temp = in0_p, in0_p = in1_p, in1_p = temp;
4864 tem = low0, low0 = low1, low1 = tem;
4865 tem = high0, high0 = high1, high1 = tem;
4868 /* Now flag two cases, whether the ranges are disjoint or whether the
4869 second range is totally subsumed in the first. Note that the tests
4870 below are simplified by the ones above. */
4871 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4872 high0, 1, low1, 0));
4873 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4874 high1, 1, high0, 1));
4876 /* We now have four cases, depending on whether we are including or
4877 excluding the two ranges. */
4880 /* If they don't overlap, the result is false. If the second range
4881 is a subset it is the result. Otherwise, the range is from the start
4882 of the second to the end of the first. */
4884 in_p = 0, low = high = 0;
4886 in_p = 1, low = low1, high = high1;
4888 in_p = 1, low = low1, high = high0;
4891 else if (in0_p && ! in1_p)
4893 /* If they don't overlap, the result is the first range. If they are
4894 equal, the result is false. If the second range is a subset of the
4895 first, and the ranges begin at the same place, we go from just after
4896 the end of the second range to the end of the first. If the second
4897 range is not a subset of the first, or if it is a subset and both
4898 ranges end at the same place, the range starts at the start of the
4899 first range and ends just before the second range.
4900 Otherwise, we can't describe this as a single range. */
4902 in_p = 1, low = low0, high = high0;
4903 else if (lowequal && highequal)
4904 in_p = 0, low = high = 0;
4905 else if (subset && lowequal)
4907 low = range_successor (high1);
4912 /* We are in the weird situation where high0 > high1 but
4913 high1 has no successor. Punt. */
4917 else if (! subset || highequal)
4920 high = range_predecessor (low1);
4924 /* low0 < low1 but low1 has no predecessor. Punt. */
4932 else if (! in0_p && in1_p)
4934 /* If they don't overlap, the result is the second range. If the second
4935 is a subset of the first, the result is false. Otherwise,
4936 the range starts just after the first range and ends at the
4937 end of the second. */
4939 in_p = 1, low = low1, high = high1;
4940 else if (subset || highequal)
4941 in_p = 0, low = high = 0;
4944 low = range_successor (high0);
4949 /* high1 > high0 but high0 has no successor. Punt. */
4957 /* The case where we are excluding both ranges. Here the complex case
4958 is if they don't overlap. In that case, the only time we have a
4959 range is if they are adjacent. If the second is a subset of the
4960 first, the result is the first. Otherwise, the range to exclude
4961 starts at the beginning of the first range and ends at the end of the
4965 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4966 range_successor (high0),
4968 in_p = 0, low = low0, high = high1;
4971 /* Canonicalize - [min, x] into - [-, x]. */
4972 if (low0 && TREE_CODE (low0) == INTEGER_CST)
4973 switch (TREE_CODE (TREE_TYPE (low0)))
4976 if (TYPE_PRECISION (TREE_TYPE (low0))
4977 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4981 if (tree_int_cst_equal (low0,
4982 TYPE_MIN_VALUE (TREE_TYPE (low0))))
4986 if (TYPE_UNSIGNED (TREE_TYPE (low0))
4987 && integer_zerop (low0))
4994 /* Canonicalize - [x, max] into - [x, -]. */
4995 if (high1 && TREE_CODE (high1) == INTEGER_CST)
4996 switch (TREE_CODE (TREE_TYPE (high1)))
4999 if (TYPE_PRECISION (TREE_TYPE (high1))
5000 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
5004 if (tree_int_cst_equal (high1,
5005 TYPE_MAX_VALUE (TREE_TYPE (high1))))
5009 if (TYPE_UNSIGNED (TREE_TYPE (high1))
5010 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5012 integer_one_node, 1)))
5019 /* The ranges might be also adjacent between the maximum and
5020 minimum values of the given type. For
5021 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5022 return + [x + 1, y - 1]. */
5023 if (low0 == 0 && high1 == 0)
5025 low = range_successor (high0);
5026 high = range_predecessor (low1);
5027 if (low == 0 || high == 0)
5037 in_p = 0, low = low0, high = high0;
5039 in_p = 0, low = low0, high = high1;
5042 *pin_p = in_p, *plow = low, *phigh = high;
5047 /* Subroutine of fold, looking inside expressions of the form
5048 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5049 of the COND_EXPR. This function is being used also to optimize
5050 A op B ? C : A, by reversing the comparison first.
5052 Return a folded expression whose code is not a COND_EXPR
5053 anymore, or NULL_TREE if no folding opportunity is found. */
5056 fold_cond_expr_with_comparison (tree type, tree arg0, tree arg1, tree arg2)
5058 enum tree_code comp_code = TREE_CODE (arg0);
5059 tree arg00 = TREE_OPERAND (arg0, 0);
5060 tree arg01 = TREE_OPERAND (arg0, 1);
5061 tree arg1_type = TREE_TYPE (arg1);
5067 /* If we have A op 0 ? A : -A, consider applying the following
5070 A == 0? A : -A same as -A
5071 A != 0? A : -A same as A
5072 A >= 0? A : -A same as abs (A)
5073 A > 0? A : -A same as abs (A)
5074 A <= 0? A : -A same as -abs (A)
5075 A < 0? A : -A same as -abs (A)
5077 None of these transformations work for modes with signed
5078 zeros. If A is +/-0, the first two transformations will
5079 change the sign of the result (from +0 to -0, or vice
5080 versa). The last four will fix the sign of the result,
5081 even though the original expressions could be positive or
5082 negative, depending on the sign of A.
5084 Note that all these transformations are correct if A is
5085 NaN, since the two alternatives (A and -A) are also NaNs. */
5086 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5087 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5088 ? real_zerop (arg01)
5089 : integer_zerop (arg01))
5090 && ((TREE_CODE (arg2) == NEGATE_EXPR
5091 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5092 /* In the case that A is of the form X-Y, '-A' (arg2) may
5093 have already been folded to Y-X, check for that. */
5094 || (TREE_CODE (arg1) == MINUS_EXPR
5095 && TREE_CODE (arg2) == MINUS_EXPR
5096 && operand_equal_p (TREE_OPERAND (arg1, 0),
5097 TREE_OPERAND (arg2, 1), 0)
5098 && operand_equal_p (TREE_OPERAND (arg1, 1),
5099 TREE_OPERAND (arg2, 0), 0))))
5104 tem = fold_convert (arg1_type, arg1);
5105 return pedantic_non_lvalue (fold_convert (type, negate_expr (tem)));
5108 return pedantic_non_lvalue (fold_convert (type, arg1));
5111 if (flag_trapping_math)
5116 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5117 arg1 = fold_convert (signed_type_for
5118 (TREE_TYPE (arg1)), arg1);
5119 tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
5120 return pedantic_non_lvalue (fold_convert (type, tem));
5123 if (flag_trapping_math)
5127 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5128 arg1 = fold_convert (signed_type_for
5129 (TREE_TYPE (arg1)), arg1);
5130 tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
5131 return negate_expr (fold_convert (type, tem));
5133 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5137 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5138 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5139 both transformations are correct when A is NaN: A != 0
5140 is then true, and A == 0 is false. */
5142 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5143 && integer_zerop (arg01) && integer_zerop (arg2))
5145 if (comp_code == NE_EXPR)
5146 return pedantic_non_lvalue (fold_convert (type, arg1));
5147 else if (comp_code == EQ_EXPR)
5148 return build_int_cst (type, 0);
5151 /* Try some transformations of A op B ? A : B.
5153 A == B? A : B same as B
5154 A != B? A : B same as A
5155 A >= B? A : B same as max (A, B)
5156 A > B? A : B same as max (B, A)
5157 A <= B? A : B same as min (A, B)
5158 A < B? A : B same as min (B, A)
5160 As above, these transformations don't work in the presence
5161 of signed zeros. For example, if A and B are zeros of
5162 opposite sign, the first two transformations will change
5163 the sign of the result. In the last four, the original
5164 expressions give different results for (A=+0, B=-0) and
5165 (A=-0, B=+0), but the transformed expressions do not.
5167 The first two transformations are correct if either A or B
5168 is a NaN. In the first transformation, the condition will
5169 be false, and B will indeed be chosen. In the case of the
5170 second transformation, the condition A != B will be true,
5171 and A will be chosen.
5173 The conversions to max() and min() are not correct if B is
5174 a number and A is not. The conditions in the original
5175 expressions will be false, so all four give B. The min()
5176 and max() versions would give a NaN instead. */
5177 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5178 && operand_equal_for_comparison_p (arg01, arg2, arg00)
5179 /* Avoid these transformations if the COND_EXPR may be used
5180 as an lvalue in the C++ front-end. PR c++/19199. */
5182 || (strcmp (lang_hooks.name, "GNU C++") != 0
5183 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5184 || ! maybe_lvalue_p (arg1)
5185 || ! maybe_lvalue_p (arg2)))
5187 tree comp_op0 = arg00;
5188 tree comp_op1 = arg01;
5189 tree comp_type = TREE_TYPE (comp_op0);
5191 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
5192 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
5202 return pedantic_non_lvalue (fold_convert (type, arg2));
5204 return pedantic_non_lvalue (fold_convert (type, arg1));
5209 /* In C++ a ?: expression can be an lvalue, so put the
5210 operand which will be used if they are equal first
5211 so that we can convert this back to the
5212 corresponding COND_EXPR. */
5213 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5215 comp_op0 = fold_convert (comp_type, comp_op0);
5216 comp_op1 = fold_convert (comp_type, comp_op1);
5217 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5218 ? fold_build2 (MIN_EXPR, comp_type, comp_op0, comp_op1)
5219 : fold_build2 (MIN_EXPR, comp_type, comp_op1, comp_op0);
5220 return pedantic_non_lvalue (fold_convert (type, tem));
5227 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5229 comp_op0 = fold_convert (comp_type, comp_op0);
5230 comp_op1 = fold_convert (comp_type, comp_op1);
5231 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5232 ? fold_build2 (MAX_EXPR, comp_type, comp_op0, comp_op1)
5233 : fold_build2 (MAX_EXPR, comp_type, comp_op1, comp_op0);
5234 return pedantic_non_lvalue (fold_convert (type, tem));
5238 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5239 return pedantic_non_lvalue (fold_convert (type, arg2));
5242 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5243 return pedantic_non_lvalue (fold_convert (type, arg1));
5246 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5251 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5252 we might still be able to simplify this. For example,
5253 if C1 is one less or one more than C2, this might have started
5254 out as a MIN or MAX and been transformed by this function.
5255 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
5257 if (INTEGRAL_TYPE_P (type)
5258 && TREE_CODE (arg01) == INTEGER_CST
5259 && TREE_CODE (arg2) == INTEGER_CST)
5263 /* We can replace A with C1 in this case. */
5264 arg1 = fold_convert (type, arg01);
5265 return fold_build3 (COND_EXPR, type, arg0, arg1, arg2);
5268 /* If C1 is C2 + 1, this is min(A, C2). */
5269 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5271 && operand_equal_p (arg01,
5272 const_binop (PLUS_EXPR, arg2,
5273 build_int_cst (type, 1), 0),
5275 return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
5277 fold_convert (type, arg1),
5282 /* If C1 is C2 - 1, this is min(A, C2). */
5283 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5285 && operand_equal_p (arg01,
5286 const_binop (MINUS_EXPR, arg2,
5287 build_int_cst (type, 1), 0),
5289 return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
5291 fold_convert (type, arg1),
5296 /* If C1 is C2 - 1, this is max(A, C2). */
5297 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5299 && operand_equal_p (arg01,
5300 const_binop (MINUS_EXPR, arg2,
5301 build_int_cst (type, 1), 0),
5303 return pedantic_non_lvalue (fold_build2 (MAX_EXPR,
5305 fold_convert (type, arg1),
5310 /* If C1 is C2 + 1, this is max(A, C2). */
5311 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5313 && operand_equal_p (arg01,
5314 const_binop (PLUS_EXPR, arg2,
5315 build_int_cst (type, 1), 0),
5317 return pedantic_non_lvalue (fold_build2 (MAX_EXPR,
5319 fold_convert (type, arg1),
5333 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5334 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5335 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5339 /* EXP is some logical combination of boolean tests. See if we can
5340 merge it into some range test. Return the new tree if so. */
5343 fold_range_test (enum tree_code code, tree type, tree op0, tree op1)
5345 int or_op = (code == TRUTH_ORIF_EXPR
5346 || code == TRUTH_OR_EXPR);
5347 int in0_p, in1_p, in_p;
5348 tree low0, low1, low, high0, high1, high;
5349 bool strict_overflow_p = false;
5350 tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5351 tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5353 const char * const warnmsg = G_("assuming signed overflow does not occur "
5354 "when simplifying range test");
5356 /* If this is an OR operation, invert both sides; we will invert
5357 again at the end. */
5359 in0_p = ! in0_p, in1_p = ! in1_p;
5361 /* If both expressions are the same, if we can merge the ranges, and we
5362 can build the range test, return it or it inverted. If one of the
5363 ranges is always true or always false, consider it to be the same
5364 expression as the other. */
5365 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5366 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5368 && 0 != (tem = (build_range_check (type,
5370 : rhs != 0 ? rhs : integer_zero_node,
5373 if (strict_overflow_p)
5374 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5375 return or_op ? invert_truthvalue (tem) : tem;
5378 /* On machines where the branch cost is expensive, if this is a
5379 short-circuited branch and the underlying object on both sides
5380 is the same, make a non-short-circuit operation. */
5381 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5382 && lhs != 0 && rhs != 0
5383 && (code == TRUTH_ANDIF_EXPR
5384 || code == TRUTH_ORIF_EXPR)
5385 && operand_equal_p (lhs, rhs, 0))
5387 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5388 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5389 which cases we can't do this. */
5390 if (simple_operand_p (lhs))
5391 return build2 (code == TRUTH_ANDIF_EXPR
5392 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5395 else if (lang_hooks.decls.global_bindings_p () == 0
5396 && ! CONTAINS_PLACEHOLDER_P (lhs))
5398 tree common = save_expr (lhs);
5400 if (0 != (lhs = build_range_check (type, common,
5401 or_op ? ! in0_p : in0_p,
5403 && (0 != (rhs = build_range_check (type, common,
5404 or_op ? ! in1_p : in1_p,
5407 if (strict_overflow_p)
5408 fold_overflow_warning (warnmsg,
5409 WARN_STRICT_OVERFLOW_COMPARISON);
5410 return build2 (code == TRUTH_ANDIF_EXPR
5411 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5420 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
5421 bit value. Arrange things so the extra bits will be set to zero if and
5422 only if C is signed-extended to its full width. If MASK is nonzero,
5423 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5426 unextend (tree c, int p, int unsignedp, tree mask)
5428 tree type = TREE_TYPE (c);
5429 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5432 if (p == modesize || unsignedp)
5435 /* We work by getting just the sign bit into the low-order bit, then
5436 into the high-order bit, then sign-extend. We then XOR that value
5438 temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
5439 temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
5441 /* We must use a signed type in order to get an arithmetic right shift.
5442 However, we must also avoid introducing accidental overflows, so that
5443 a subsequent call to integer_zerop will work. Hence we must
5444 do the type conversion here. At this point, the constant is either
5445 zero or one, and the conversion to a signed type can never overflow.
5446 We could get an overflow if this conversion is done anywhere else. */
5447 if (TYPE_UNSIGNED (type))
5448 temp = fold_convert (signed_type_for (type), temp);
5450 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
5451 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
5453 temp = const_binop (BIT_AND_EXPR, temp,
5454 fold_convert (TREE_TYPE (c), mask), 0);
5455 /* If necessary, convert the type back to match the type of C. */
5456 if (TYPE_UNSIGNED (type))
5457 temp = fold_convert (type, temp);
5459 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
5462 /* Find ways of folding logical expressions of LHS and RHS:
5463 Try to merge two comparisons to the same innermost item.
5464 Look for range tests like "ch >= '0' && ch <= '9'".
5465 Look for combinations of simple terms on machines with expensive branches
5466 and evaluate the RHS unconditionally.
5468 For example, if we have p->a == 2 && p->b == 4 and we can make an
5469 object large enough to span both A and B, we can do this with a comparison
5470 against the object ANDed with the a mask.
5472 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5473 operations to do this with one comparison.
5475 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5476 function and the one above.
5478 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5479 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5481 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5484 We return the simplified tree or 0 if no optimization is possible. */
5487 fold_truthop (enum tree_code code, tree truth_type, tree lhs, tree rhs)
5489 /* If this is the "or" of two comparisons, we can do something if
5490 the comparisons are NE_EXPR. If this is the "and", we can do something
5491 if the comparisons are EQ_EXPR. I.e.,
5492 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5494 WANTED_CODE is this operation code. For single bit fields, we can
5495 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5496 comparison for one-bit fields. */
5498 enum tree_code wanted_code;
5499 enum tree_code lcode, rcode;
5500 tree ll_arg, lr_arg, rl_arg, rr_arg;
5501 tree ll_inner, lr_inner, rl_inner, rr_inner;
5502 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5503 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5504 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5505 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5506 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5507 enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5508 enum machine_mode lnmode, rnmode;
5509 tree ll_mask, lr_mask, rl_mask, rr_mask;
5510 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5511 tree l_const, r_const;
5512 tree lntype, rntype, result;
5513 HOST_WIDE_INT first_bit, end_bit;
5515 tree orig_lhs = lhs, orig_rhs = rhs;
5516 enum tree_code orig_code = code;
5518 /* Start by getting the comparison codes. Fail if anything is volatile.
5519 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5520 it were surrounded with a NE_EXPR. */
5522 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5525 lcode = TREE_CODE (lhs);
5526 rcode = TREE_CODE (rhs);
5528 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5530 lhs = build2 (NE_EXPR, truth_type, lhs,
5531 build_int_cst (TREE_TYPE (lhs), 0));
5535 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5537 rhs = build2 (NE_EXPR, truth_type, rhs,
5538 build_int_cst (TREE_TYPE (rhs), 0));
5542 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5543 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5546 ll_arg = TREE_OPERAND (lhs, 0);
5547 lr_arg = TREE_OPERAND (lhs, 1);
5548 rl_arg = TREE_OPERAND (rhs, 0);
5549 rr_arg = TREE_OPERAND (rhs, 1);
5551 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5552 if (simple_operand_p (ll_arg)
5553 && simple_operand_p (lr_arg))
5556 if (operand_equal_p (ll_arg, rl_arg, 0)
5557 && operand_equal_p (lr_arg, rr_arg, 0))
5559 result = combine_comparisons (code, lcode, rcode,
5560 truth_type, ll_arg, lr_arg);
5564 else if (operand_equal_p (ll_arg, rr_arg, 0)
5565 && operand_equal_p (lr_arg, rl_arg, 0))
5567 result = combine_comparisons (code, lcode,
5568 swap_tree_comparison (rcode),
5569 truth_type, ll_arg, lr_arg);
5575 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5576 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5578 /* If the RHS can be evaluated unconditionally and its operands are
5579 simple, it wins to evaluate the RHS unconditionally on machines
5580 with expensive branches. In this case, this isn't a comparison
5581 that can be merged. Avoid doing this if the RHS is a floating-point
5582 comparison since those can trap. */
5584 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5586 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5587 && simple_operand_p (rl_arg)
5588 && simple_operand_p (rr_arg))
5590 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5591 if (code == TRUTH_OR_EXPR
5592 && lcode == NE_EXPR && integer_zerop (lr_arg)
5593 && rcode == NE_EXPR && integer_zerop (rr_arg)
5594 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5595 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5596 return build2 (NE_EXPR, truth_type,
5597 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5599 build_int_cst (TREE_TYPE (ll_arg), 0));
5601 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5602 if (code == TRUTH_AND_EXPR
5603 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5604 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5605 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5606 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5607 return build2 (EQ_EXPR, truth_type,
5608 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5610 build_int_cst (TREE_TYPE (ll_arg), 0));
5612 if (LOGICAL_OP_NON_SHORT_CIRCUIT)
5614 if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs)
5615 return build2 (code, truth_type, lhs, rhs);
5620 /* See if the comparisons can be merged. Then get all the parameters for
5623 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5624 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5628 ll_inner = decode_field_reference (ll_arg,
5629 &ll_bitsize, &ll_bitpos, &ll_mode,
5630 &ll_unsignedp, &volatilep, &ll_mask,
5632 lr_inner = decode_field_reference (lr_arg,
5633 &lr_bitsize, &lr_bitpos, &lr_mode,
5634 &lr_unsignedp, &volatilep, &lr_mask,
5636 rl_inner = decode_field_reference (rl_arg,
5637 &rl_bitsize, &rl_bitpos, &rl_mode,
5638 &rl_unsignedp, &volatilep, &rl_mask,
5640 rr_inner = decode_field_reference (rr_arg,
5641 &rr_bitsize, &rr_bitpos, &rr_mode,
5642 &rr_unsignedp, &volatilep, &rr_mask,
5645 /* It must be true that the inner operation on the lhs of each
5646 comparison must be the same if we are to be able to do anything.
5647 Then see if we have constants. If not, the same must be true for
5649 if (volatilep || ll_inner == 0 || rl_inner == 0
5650 || ! operand_equal_p (ll_inner, rl_inner, 0))
5653 if (TREE_CODE (lr_arg) == INTEGER_CST
5654 && TREE_CODE (rr_arg) == INTEGER_CST)
5655 l_const = lr_arg, r_const = rr_arg;
5656 else if (lr_inner == 0 || rr_inner == 0
5657 || ! operand_equal_p (lr_inner, rr_inner, 0))
5660 l_const = r_const = 0;
5662 /* If either comparison code is not correct for our logical operation,
5663 fail. However, we can convert a one-bit comparison against zero into
5664 the opposite comparison against that bit being set in the field. */
5666 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5667 if (lcode != wanted_code)
5669 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5671 /* Make the left operand unsigned, since we are only interested
5672 in the value of one bit. Otherwise we are doing the wrong
5681 /* This is analogous to the code for l_const above. */
5682 if (rcode != wanted_code)
5684 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5693 /* See if we can find a mode that contains both fields being compared on
5694 the left. If we can't, fail. Otherwise, update all constants and masks
5695 to be relative to a field of that size. */
5696 first_bit = MIN (ll_bitpos, rl_bitpos);
5697 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5698 lnmode = get_best_mode (end_bit - first_bit, first_bit,
5699 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5701 if (lnmode == VOIDmode)
5704 lnbitsize = GET_MODE_BITSIZE (lnmode);
5705 lnbitpos = first_bit & ~ (lnbitsize - 1);
5706 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5707 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5709 if (BYTES_BIG_ENDIAN)
5711 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5712 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5715 ll_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, ll_mask),
5716 size_int (xll_bitpos), 0);
5717 rl_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, rl_mask),
5718 size_int (xrl_bitpos), 0);
5722 l_const = fold_convert (lntype, l_const);
5723 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5724 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
5725 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5726 fold_build1 (BIT_NOT_EXPR,
5730 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5732 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5737 r_const = fold_convert (lntype, r_const);
5738 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5739 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
5740 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5741 fold_build1 (BIT_NOT_EXPR,
5745 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5747 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5751 /* If the right sides are not constant, do the same for it. Also,
5752 disallow this optimization if a size or signedness mismatch occurs
5753 between the left and right sides. */
5756 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5757 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5758 /* Make sure the two fields on the right
5759 correspond to the left without being swapped. */
5760 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5763 first_bit = MIN (lr_bitpos, rr_bitpos);
5764 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5765 rnmode = get_best_mode (end_bit - first_bit, first_bit,
5766 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5768 if (rnmode == VOIDmode)
5771 rnbitsize = GET_MODE_BITSIZE (rnmode);
5772 rnbitpos = first_bit & ~ (rnbitsize - 1);
5773 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5774 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5776 if (BYTES_BIG_ENDIAN)
5778 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5779 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5782 lr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, lr_mask),
5783 size_int (xlr_bitpos), 0);
5784 rr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, rr_mask),
5785 size_int (xrr_bitpos), 0);
5787 /* Make a mask that corresponds to both fields being compared.
5788 Do this for both items being compared. If the operands are the
5789 same size and the bits being compared are in the same position
5790 then we can do this by masking both and comparing the masked
5792 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
5793 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
5794 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5796 lhs = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
5797 ll_unsignedp || rl_unsignedp);
5798 if (! all_ones_mask_p (ll_mask, lnbitsize))
5799 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5801 rhs = make_bit_field_ref (lr_inner, rntype, rnbitsize, rnbitpos,
5802 lr_unsignedp || rr_unsignedp);
5803 if (! all_ones_mask_p (lr_mask, rnbitsize))
5804 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5806 return build2 (wanted_code, truth_type, lhs, rhs);
5809 /* There is still another way we can do something: If both pairs of
5810 fields being compared are adjacent, we may be able to make a wider
5811 field containing them both.
5813 Note that we still must mask the lhs/rhs expressions. Furthermore,
5814 the mask must be shifted to account for the shift done by
5815 make_bit_field_ref. */
5816 if ((ll_bitsize + ll_bitpos == rl_bitpos
5817 && lr_bitsize + lr_bitpos == rr_bitpos)
5818 || (ll_bitpos == rl_bitpos + rl_bitsize
5819 && lr_bitpos == rr_bitpos + rr_bitsize))
5823 lhs = make_bit_field_ref (ll_inner, lntype, ll_bitsize + rl_bitsize,
5824 MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
5825 rhs = make_bit_field_ref (lr_inner, rntype, lr_bitsize + rr_bitsize,
5826 MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
5828 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5829 size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
5830 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5831 size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
5833 /* Convert to the smaller type before masking out unwanted bits. */
5835 if (lntype != rntype)
5837 if (lnbitsize > rnbitsize)
5839 lhs = fold_convert (rntype, lhs);
5840 ll_mask = fold_convert (rntype, ll_mask);
5843 else if (lnbitsize < rnbitsize)
5845 rhs = fold_convert (lntype, rhs);
5846 lr_mask = fold_convert (lntype, lr_mask);
5851 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5852 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5854 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5855 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5857 return build2 (wanted_code, truth_type, lhs, rhs);
5863 /* Handle the case of comparisons with constants. If there is something in
5864 common between the masks, those bits of the constants must be the same.
5865 If not, the condition is always false. Test for this to avoid generating
5866 incorrect code below. */
5867 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
5868 if (! integer_zerop (result)
5869 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
5870 const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
5872 if (wanted_code == NE_EXPR)
5874 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5875 return constant_boolean_node (true, truth_type);
5879 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5880 return constant_boolean_node (false, truth_type);
5884 /* Construct the expression we will return. First get the component
5885 reference we will make. Unless the mask is all ones the width of
5886 that field, perform the mask operation. Then compare with the
5888 result = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
5889 ll_unsignedp || rl_unsignedp);
5891 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
5892 if (! all_ones_mask_p (ll_mask, lnbitsize))
5893 result = build2 (BIT_AND_EXPR, lntype, result, ll_mask);
5895 return build2 (wanted_code, truth_type, result,
5896 const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
5899 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5903 optimize_minmax_comparison (enum tree_code code, tree type, tree op0, tree op1)
5906 enum tree_code op_code;
5909 int consts_equal, consts_lt;
5912 STRIP_SIGN_NOPS (arg0);
5914 op_code = TREE_CODE (arg0);
5915 minmax_const = TREE_OPERAND (arg0, 1);
5916 comp_const = fold_convert (TREE_TYPE (arg0), op1);
5917 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5918 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5919 inner = TREE_OPERAND (arg0, 0);
5921 /* If something does not permit us to optimize, return the original tree. */
5922 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5923 || TREE_CODE (comp_const) != INTEGER_CST
5924 || TREE_OVERFLOW (comp_const)
5925 || TREE_CODE (minmax_const) != INTEGER_CST
5926 || TREE_OVERFLOW (minmax_const))
5929 /* Now handle all the various comparison codes. We only handle EQ_EXPR
5930 and GT_EXPR, doing the rest with recursive calls using logical
5934 case NE_EXPR: case LT_EXPR: case LE_EXPR:
5936 tree tem = optimize_minmax_comparison (invert_tree_comparison (code, false),
5939 return invert_truthvalue (tem);
5945 fold_build2 (TRUTH_ORIF_EXPR, type,
5946 optimize_minmax_comparison
5947 (EQ_EXPR, type, arg0, comp_const),
5948 optimize_minmax_comparison
5949 (GT_EXPR, type, arg0, comp_const));
5952 if (op_code == MAX_EXPR && consts_equal)
5953 /* MAX (X, 0) == 0 -> X <= 0 */
5954 return fold_build2 (LE_EXPR, type, inner, comp_const);
5956 else if (op_code == MAX_EXPR && consts_lt)
5957 /* MAX (X, 0) == 5 -> X == 5 */
5958 return fold_build2 (EQ_EXPR, type, inner, comp_const);
5960 else if (op_code == MAX_EXPR)
5961 /* MAX (X, 0) == -1 -> false */
5962 return omit_one_operand (type, integer_zero_node, inner);
5964 else if (consts_equal)
5965 /* MIN (X, 0) == 0 -> X >= 0 */
5966 return fold_build2 (GE_EXPR, type, inner, comp_const);
5969 /* MIN (X, 0) == 5 -> false */
5970 return omit_one_operand (type, integer_zero_node, inner);
5973 /* MIN (X, 0) == -1 -> X == -1 */
5974 return fold_build2 (EQ_EXPR, type, inner, comp_const);
5977 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5978 /* MAX (X, 0) > 0 -> X > 0
5979 MAX (X, 0) > 5 -> X > 5 */
5980 return fold_build2 (GT_EXPR, type, inner, comp_const);
5982 else if (op_code == MAX_EXPR)
5983 /* MAX (X, 0) > -1 -> true */
5984 return omit_one_operand (type, integer_one_node, inner);
5986 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5987 /* MIN (X, 0) > 0 -> false
5988 MIN (X, 0) > 5 -> false */
5989 return omit_one_operand (type, integer_zero_node, inner);
5992 /* MIN (X, 0) > -1 -> X > -1 */
5993 return fold_build2 (GT_EXPR, type, inner, comp_const);
6000 /* T is an integer expression that is being multiplied, divided, or taken a
6001 modulus (CODE says which and what kind of divide or modulus) by a
6002 constant C. See if we can eliminate that operation by folding it with
6003 other operations already in T. WIDE_TYPE, if non-null, is a type that
6004 should be used for the computation if wider than our type.
6006 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6007 (X * 2) + (Y * 4). We must, however, be assured that either the original
6008 expression would not overflow or that overflow is undefined for the type
6009 in the language in question.
6011 If we return a non-null expression, it is an equivalent form of the
6012 original computation, but need not be in the original type.
6014 We set *STRICT_OVERFLOW_P to true if the return values depends on
6015 signed overflow being undefined. Otherwise we do not change
6016 *STRICT_OVERFLOW_P. */
6019 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6020 bool *strict_overflow_p)
6022 /* To avoid exponential search depth, refuse to allow recursion past
6023 three levels. Beyond that (1) it's highly unlikely that we'll find
6024 something interesting and (2) we've probably processed it before
6025 when we built the inner expression. */
6034 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6041 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6042 bool *strict_overflow_p)
6044 tree type = TREE_TYPE (t);
6045 enum tree_code tcode = TREE_CODE (t);
6046 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
6047 > GET_MODE_SIZE (TYPE_MODE (type)))
6048 ? wide_type : type);
6050 int same_p = tcode == code;
6051 tree op0 = NULL_TREE, op1 = NULL_TREE;
6052 bool sub_strict_overflow_p;
6054 /* Don't deal with constants of zero here; they confuse the code below. */
6055 if (integer_zerop (c))
6058 if (TREE_CODE_CLASS (tcode) == tcc_unary)
6059 op0 = TREE_OPERAND (t, 0);
6061 if (TREE_CODE_CLASS (tcode) == tcc_binary)
6062 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6064 /* Note that we need not handle conditional operations here since fold
6065 already handles those cases. So just do arithmetic here. */
6069 /* For a constant, we can always simplify if we are a multiply
6070 or (for divide and modulus) if it is a multiple of our constant. */
6071 if (code == MULT_EXPR
6072 || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
6073 return const_binop (code, fold_convert (ctype, t),
6074 fold_convert (ctype, c), 0);
6077 CASE_CONVERT: case NON_LVALUE_EXPR:
6078 /* If op0 is an expression ... */
6079 if ((COMPARISON_CLASS_P (op0)
6080 || UNARY_CLASS_P (op0)
6081 || BINARY_CLASS_P (op0)
6082 || VL_EXP_CLASS_P (op0)
6083 || EXPRESSION_CLASS_P (op0))
6084 /* ... and has wrapping overflow, and its type is smaller
6085 than ctype, then we cannot pass through as widening. */
6086 && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
6087 && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
6088 && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
6089 && (TYPE_PRECISION (ctype)
6090 > TYPE_PRECISION (TREE_TYPE (op0))))
6091 /* ... or this is a truncation (t is narrower than op0),
6092 then we cannot pass through this narrowing. */
6093 || (TYPE_PRECISION (type)
6094 < TYPE_PRECISION (TREE_TYPE (op0)))
6095 /* ... or signedness changes for division or modulus,
6096 then we cannot pass through this conversion. */
6097 || (code != MULT_EXPR
6098 && (TYPE_UNSIGNED (ctype)
6099 != TYPE_UNSIGNED (TREE_TYPE (op0))))
6100 /* ... or has undefined overflow while the converted to
6101 type has not, we cannot do the operation in the inner type
6102 as that would introduce undefined overflow. */
6103 || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
6104 && !TYPE_OVERFLOW_UNDEFINED (type))))
6107 /* Pass the constant down and see if we can make a simplification. If
6108 we can, replace this expression with the inner simplification for
6109 possible later conversion to our or some other type. */
6110 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6111 && TREE_CODE (t2) == INTEGER_CST
6112 && !TREE_OVERFLOW (t2)
6113 && (0 != (t1 = extract_muldiv (op0, t2, code,
6115 ? ctype : NULL_TREE,
6116 strict_overflow_p))))
6121 /* If widening the type changes it from signed to unsigned, then we
6122 must avoid building ABS_EXPR itself as unsigned. */
6123 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6125 tree cstype = (*signed_type_for) (ctype);
6126 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6129 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6130 return fold_convert (ctype, t1);
6134 /* If the constant is negative, we cannot simplify this. */
6135 if (tree_int_cst_sgn (c) == -1)
6139 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6141 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6144 case MIN_EXPR: case MAX_EXPR:
6145 /* If widening the type changes the signedness, then we can't perform
6146 this optimization as that changes the result. */
6147 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6150 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6151 sub_strict_overflow_p = false;
6152 if ((t1 = extract_muldiv (op0, c, code, wide_type,
6153 &sub_strict_overflow_p)) != 0
6154 && (t2 = extract_muldiv (op1, c, code, wide_type,
6155 &sub_strict_overflow_p)) != 0)
6157 if (tree_int_cst_sgn (c) < 0)
6158 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6159 if (sub_strict_overflow_p)
6160 *strict_overflow_p = true;
6161 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6162 fold_convert (ctype, t2));
6166 case LSHIFT_EXPR: case RSHIFT_EXPR:
6167 /* If the second operand is constant, this is a multiplication
6168 or floor division, by a power of two, so we can treat it that
6169 way unless the multiplier or divisor overflows. Signed
6170 left-shift overflow is implementation-defined rather than
6171 undefined in C90, so do not convert signed left shift into
6173 if (TREE_CODE (op1) == INTEGER_CST
6174 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6175 /* const_binop may not detect overflow correctly,
6176 so check for it explicitly here. */
6177 && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
6178 && TREE_INT_CST_HIGH (op1) == 0
6179 && 0 != (t1 = fold_convert (ctype,
6180 const_binop (LSHIFT_EXPR,
6183 && !TREE_OVERFLOW (t1))
6184 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6185 ? MULT_EXPR : FLOOR_DIV_EXPR,
6186 ctype, fold_convert (ctype, op0), t1),
6187 c, code, wide_type, strict_overflow_p);
6190 case PLUS_EXPR: case MINUS_EXPR:
6191 /* See if we can eliminate the operation on both sides. If we can, we
6192 can return a new PLUS or MINUS. If we can't, the only remaining
6193 cases where we can do anything are if the second operand is a
6195 sub_strict_overflow_p = false;
6196 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6197 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6198 if (t1 != 0 && t2 != 0
6199 && (code == MULT_EXPR
6200 /* If not multiplication, we can only do this if both operands
6201 are divisible by c. */
6202 || (multiple_of_p (ctype, op0, c)
6203 && multiple_of_p (ctype, op1, c))))
6205 if (sub_strict_overflow_p)
6206 *strict_overflow_p = true;
6207 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6208 fold_convert (ctype, t2));
6211 /* If this was a subtraction, negate OP1 and set it to be an addition.
6212 This simplifies the logic below. */
6213 if (tcode == MINUS_EXPR)
6214 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6216 if (TREE_CODE (op1) != INTEGER_CST)
6219 /* If either OP1 or C are negative, this optimization is not safe for
6220 some of the division and remainder types while for others we need
6221 to change the code. */
6222 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6224 if (code == CEIL_DIV_EXPR)
6225 code = FLOOR_DIV_EXPR;
6226 else if (code == FLOOR_DIV_EXPR)
6227 code = CEIL_DIV_EXPR;
6228 else if (code != MULT_EXPR
6229 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6233 /* If it's a multiply or a division/modulus operation of a multiple
6234 of our constant, do the operation and verify it doesn't overflow. */
6235 if (code == MULT_EXPR
6236 || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6238 op1 = const_binop (code, fold_convert (ctype, op1),
6239 fold_convert (ctype, c), 0);
6240 /* We allow the constant to overflow with wrapping semantics. */
6242 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6248 /* If we have an unsigned type is not a sizetype, we cannot widen
6249 the operation since it will change the result if the original
6250 computation overflowed. */
6251 if (TYPE_UNSIGNED (ctype)
6252 && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
6256 /* If we were able to eliminate our operation from the first side,
6257 apply our operation to the second side and reform the PLUS. */
6258 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
6259 return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
6261 /* The last case is if we are a multiply. In that case, we can
6262 apply the distributive law to commute the multiply and addition
6263 if the multiplication of the constants doesn't overflow. */
6264 if (code == MULT_EXPR)
6265 return fold_build2 (tcode, ctype,
6266 fold_build2 (code, ctype,
6267 fold_convert (ctype, op0),
6268 fold_convert (ctype, c)),
6274 /* We have a special case here if we are doing something like
6275 (C * 8) % 4 since we know that's zero. */
6276 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6277 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6278 /* If the multiplication can overflow we cannot optimize this.
6279 ??? Until we can properly mark individual operations as
6280 not overflowing we need to treat sizetype special here as
6281 stor-layout relies on this opimization to make
6282 DECL_FIELD_BIT_OFFSET always a constant. */
6283 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6284 || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
6285 && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
6286 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6287 && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6289 *strict_overflow_p = true;
6290 return omit_one_operand (type, integer_zero_node, op0);
6293 /* ... fall through ... */
6295 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6296 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6297 /* If we can extract our operation from the LHS, do so and return a
6298 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6299 do something only if the second operand is a constant. */
6301 && (t1 = extract_muldiv (op0, c, code, wide_type,
6302 strict_overflow_p)) != 0)
6303 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6304 fold_convert (ctype, op1));
6305 else if (tcode == MULT_EXPR && code == MULT_EXPR
6306 && (t1 = extract_muldiv (op1, c, code, wide_type,
6307 strict_overflow_p)) != 0)
6308 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6309 fold_convert (ctype, t1));
6310 else if (TREE_CODE (op1) != INTEGER_CST)
6313 /* If these are the same operation types, we can associate them
6314 assuming no overflow. */
6316 && 0 != (t1 = int_const_binop (MULT_EXPR, fold_convert (ctype, op1),
6317 fold_convert (ctype, c), 1))
6318 && 0 != (t1 = force_fit_type_double (ctype, TREE_INT_CST_LOW (t1),
6319 TREE_INT_CST_HIGH (t1),
6320 (TYPE_UNSIGNED (ctype)
6321 && tcode != MULT_EXPR) ? -1 : 1,
6322 TREE_OVERFLOW (t1)))
6323 && !TREE_OVERFLOW (t1))
6324 return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1);
6326 /* If these operations "cancel" each other, we have the main
6327 optimizations of this pass, which occur when either constant is a
6328 multiple of the other, in which case we replace this with either an
6329 operation or CODE or TCODE.
6331 If we have an unsigned type that is not a sizetype, we cannot do
6332 this since it will change the result if the original computation
6334 if ((TYPE_OVERFLOW_UNDEFINED (ctype)
6335 || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
6336 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6337 || (tcode == MULT_EXPR
6338 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6339 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6340 && code != MULT_EXPR)))
6342 if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6344 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6345 *strict_overflow_p = true;
6346 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6347 fold_convert (ctype,
6348 const_binop (TRUNC_DIV_EXPR,
6351 else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
6353 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6354 *strict_overflow_p = true;
6355 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6356 fold_convert (ctype,
6357 const_binop (TRUNC_DIV_EXPR,
6370 /* Return a node which has the indicated constant VALUE (either 0 or
6371 1), and is of the indicated TYPE. */
6374 constant_boolean_node (int value, tree type)
6376 if (type == integer_type_node)
6377 return value ? integer_one_node : integer_zero_node;
6378 else if (type == boolean_type_node)
6379 return value ? boolean_true_node : boolean_false_node;
6381 return build_int_cst (type, value);
6385 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6386 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6387 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6388 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6389 COND is the first argument to CODE; otherwise (as in the example
6390 given here), it is the second argument. TYPE is the type of the
6391 original expression. Return NULL_TREE if no simplification is
6395 fold_binary_op_with_conditional_arg (enum tree_code code,
6396 tree type, tree op0, tree op1,
6397 tree cond, tree arg, int cond_first_p)
6399 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6400 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6401 tree test, true_value, false_value;
6402 tree lhs = NULL_TREE;
6403 tree rhs = NULL_TREE;
6405 /* This transformation is only worthwhile if we don't have to wrap
6406 arg in a SAVE_EXPR, and the operation can be simplified on at least
6407 one of the branches once its pushed inside the COND_EXPR. */
6408 if (!TREE_CONSTANT (arg))
6411 if (TREE_CODE (cond) == COND_EXPR)
6413 test = TREE_OPERAND (cond, 0);
6414 true_value = TREE_OPERAND (cond, 1);
6415 false_value = TREE_OPERAND (cond, 2);
6416 /* If this operand throws an expression, then it does not make
6417 sense to try to perform a logical or arithmetic operation
6419 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6421 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6426 tree testtype = TREE_TYPE (cond);
6428 true_value = constant_boolean_node (true, testtype);
6429 false_value = constant_boolean_node (false, testtype);
6432 arg = fold_convert (arg_type, arg);
6435 true_value = fold_convert (cond_type, true_value);
6437 lhs = fold_build2 (code, type, true_value, arg);
6439 lhs = fold_build2 (code, type, arg, true_value);
6443 false_value = fold_convert (cond_type, false_value);
6445 rhs = fold_build2 (code, type, false_value, arg);
6447 rhs = fold_build2 (code, type, arg, false_value);
6450 test = fold_build3 (COND_EXPR, type, test, lhs, rhs);
6451 return fold_convert (type, test);
6455 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6457 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6458 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6459 ADDEND is the same as X.
6461 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6462 and finite. The problematic cases are when X is zero, and its mode
6463 has signed zeros. In the case of rounding towards -infinity,
6464 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6465 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6468 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6470 if (!real_zerop (addend))
6473 /* Don't allow the fold with -fsignaling-nans. */
6474 if (HONOR_SNANS (TYPE_MODE (type)))
6477 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6478 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6481 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6482 if (TREE_CODE (addend) == REAL_CST
6483 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6486 /* The mode has signed zeros, and we have to honor their sign.
6487 In this situation, there is only one case we can return true for.
6488 X - 0 is the same as X unless rounding towards -infinity is
6490 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6493 /* Subroutine of fold() that checks comparisons of built-in math
6494 functions against real constants.
6496 FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6497 operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR. TYPE
6498 is the type of the result and ARG0 and ARG1 are the operands of the
6499 comparison. ARG1 must be a TREE_REAL_CST.
6501 The function returns the constant folded tree if a simplification
6502 can be made, and NULL_TREE otherwise. */
6505 fold_mathfn_compare (enum built_in_function fcode, enum tree_code code,
6506 tree type, tree arg0, tree arg1)
6510 if (BUILTIN_SQRT_P (fcode))
6512 tree arg = CALL_EXPR_ARG (arg0, 0);
6513 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6515 c = TREE_REAL_CST (arg1);
6516 if (REAL_VALUE_NEGATIVE (c))
6518 /* sqrt(x) < y is always false, if y is negative. */
6519 if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6520 return omit_one_operand (type, integer_zero_node, arg);
6522 /* sqrt(x) > y is always true, if y is negative and we
6523 don't care about NaNs, i.e. negative values of x. */
6524 if (code == NE_EXPR || !HONOR_NANS (mode))
6525 return omit_one_operand (type, integer_one_node, arg);
6527 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
6528 return fold_build2 (GE_EXPR, type, arg,
6529 build_real (TREE_TYPE (arg), dconst0));
6531 else if (code == GT_EXPR || code == GE_EXPR)
6535 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6536 real_convert (&c2, mode, &c2);
6538 if (REAL_VALUE_ISINF (c2))
6540 /* sqrt(x) > y is x == +Inf, when y is very large. */
6541 if (HONOR_INFINITIES (mode))
6542 return fold_build2 (EQ_EXPR, type, arg,
6543 build_real (TREE_TYPE (arg), c2));
6545 /* sqrt(x) > y is always false, when y is very large
6546 and we don't care about infinities. */
6547 return omit_one_operand (type, integer_zero_node, arg);
6550 /* sqrt(x) > c is the same as x > c*c. */
6551 return fold_build2 (code, type, arg,
6552 build_real (TREE_TYPE (arg), c2));
6554 else if (code == LT_EXPR || code == LE_EXPR)
6558 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6559 real_convert (&c2, mode, &c2);
6561 if (REAL_VALUE_ISINF (c2))
6563 /* sqrt(x) < y is always true, when y is a very large
6564 value and we don't care about NaNs or Infinities. */
6565 if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6566 return omit_one_operand (type, integer_one_node, arg);
6568 /* sqrt(x) < y is x != +Inf when y is very large and we
6569 don't care about NaNs. */
6570 if (! HONOR_NANS (mode))
6571 return fold_build2 (NE_EXPR, type, arg,
6572 build_real (TREE_TYPE (arg), c2));
6574 /* sqrt(x) < y is x >= 0 when y is very large and we
6575 don't care about Infinities. */
6576 if (! HONOR_INFINITIES (mode))
6577 return fold_build2 (GE_EXPR, type, arg,
6578 build_real (TREE_TYPE (arg), dconst0));
6580 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
6581 if (lang_hooks.decls.global_bindings_p () != 0
6582 || CONTAINS_PLACEHOLDER_P (arg))
6585 arg = save_expr (arg);
6586 return fold_build2 (TRUTH_ANDIF_EXPR, type,
6587 fold_build2 (GE_EXPR, type, arg,
6588 build_real (TREE_TYPE (arg),
6590 fold_build2 (NE_EXPR, type, arg,
6591 build_real (TREE_TYPE (arg),
6595 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
6596 if (! HONOR_NANS (mode))
6597 return fold_build2 (code, type, arg,
6598 build_real (TREE_TYPE (arg), c2));
6600 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
6601 if (lang_hooks.decls.global_bindings_p () == 0
6602 && ! CONTAINS_PLACEHOLDER_P (arg))
6604 arg = save_expr (arg);
6605 return fold_build2 (TRUTH_ANDIF_EXPR, type,
6606 fold_build2 (GE_EXPR, type, arg,
6607 build_real (TREE_TYPE (arg),
6609 fold_build2 (code, type, arg,
6610 build_real (TREE_TYPE (arg),
6619 /* Subroutine of fold() that optimizes comparisons against Infinities,
6620 either +Inf or -Inf.
6622 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6623 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6624 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6626 The function returns the constant folded tree if a simplification
6627 can be made, and NULL_TREE otherwise. */
6630 fold_inf_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6632 enum machine_mode mode;
6633 REAL_VALUE_TYPE max;
6637 mode = TYPE_MODE (TREE_TYPE (arg0));
6639 /* For negative infinity swap the sense of the comparison. */
6640 neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6642 code = swap_tree_comparison (code);
6647 /* x > +Inf is always false, if with ignore sNANs. */
6648 if (HONOR_SNANS (mode))
6650 return omit_one_operand (type, integer_zero_node, arg0);
6653 /* x <= +Inf is always true, if we don't case about NaNs. */
6654 if (! HONOR_NANS (mode))
6655 return omit_one_operand (type, integer_one_node, arg0);
6657 /* x <= +Inf is the same as x == x, i.e. isfinite(x). */
6658 if (lang_hooks.decls.global_bindings_p () == 0
6659 && ! CONTAINS_PLACEHOLDER_P (arg0))
6661 arg0 = save_expr (arg0);
6662 return fold_build2 (EQ_EXPR, type, arg0, arg0);
6668 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
6669 real_maxval (&max, neg, mode);
6670 return fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6671 arg0, build_real (TREE_TYPE (arg0), max));
6674 /* x < +Inf is always equal to x <= DBL_MAX. */
6675 real_maxval (&max, neg, mode);
6676 return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6677 arg0, build_real (TREE_TYPE (arg0), max));
6680 /* x != +Inf is always equal to !(x > DBL_MAX). */
6681 real_maxval (&max, neg, mode);
6682 if (! HONOR_NANS (mode))
6683 return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6684 arg0, build_real (TREE_TYPE (arg0), max));
6686 temp = fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6687 arg0, build_real (TREE_TYPE (arg0), max));
6688 return fold_build1 (TRUTH_NOT_EXPR, type, temp);
6697 /* Subroutine of fold() that optimizes comparisons of a division by
6698 a nonzero integer constant against an integer constant, i.e.
6701 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6702 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6703 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6705 The function returns the constant folded tree if a simplification
6706 can be made, and NULL_TREE otherwise. */
6709 fold_div_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6711 tree prod, tmp, hi, lo;
6712 tree arg00 = TREE_OPERAND (arg0, 0);
6713 tree arg01 = TREE_OPERAND (arg0, 1);
6714 unsigned HOST_WIDE_INT lpart;
6715 HOST_WIDE_INT hpart;
6716 bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
6720 /* We have to do this the hard way to detect unsigned overflow.
6721 prod = int_const_binop (MULT_EXPR, arg01, arg1, 0); */
6722 overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
6723 TREE_INT_CST_HIGH (arg01),
6724 TREE_INT_CST_LOW (arg1),
6725 TREE_INT_CST_HIGH (arg1),
6726 &lpart, &hpart, unsigned_p);
6727 prod = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6729 neg_overflow = false;
6733 tmp = int_const_binop (MINUS_EXPR, arg01,
6734 build_int_cst (TREE_TYPE (arg01), 1), 0);
6737 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0). */
6738 overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
6739 TREE_INT_CST_HIGH (prod),
6740 TREE_INT_CST_LOW (tmp),
6741 TREE_INT_CST_HIGH (tmp),
6742 &lpart, &hpart, unsigned_p);
6743 hi = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6744 -1, overflow | TREE_OVERFLOW (prod));
6746 else if (tree_int_cst_sgn (arg01) >= 0)
6748 tmp = int_const_binop (MINUS_EXPR, arg01,
6749 build_int_cst (TREE_TYPE (arg01), 1), 0);
6750 switch (tree_int_cst_sgn (arg1))
6753 neg_overflow = true;
6754 lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6759 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6764 hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6774 /* A negative divisor reverses the relational operators. */
6775 code = swap_tree_comparison (code);
6777 tmp = int_const_binop (PLUS_EXPR, arg01,
6778 build_int_cst (TREE_TYPE (arg01), 1), 0);
6779 switch (tree_int_cst_sgn (arg1))
6782 hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6787 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6792 neg_overflow = true;
6793 lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6805 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6806 return omit_one_operand (type, integer_zero_node, arg00);
6807 if (TREE_OVERFLOW (hi))
6808 return fold_build2 (GE_EXPR, type, arg00, lo);
6809 if (TREE_OVERFLOW (lo))
6810 return fold_build2 (LE_EXPR, type, arg00, hi);
6811 return build_range_check (type, arg00, 1, lo, hi);
6814 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6815 return omit_one_operand (type, integer_one_node, arg00);
6816 if (TREE_OVERFLOW (hi))
6817 return fold_build2 (LT_EXPR, type, arg00, lo);
6818 if (TREE_OVERFLOW (lo))
6819 return fold_build2 (GT_EXPR, type, arg00, hi);
6820 return build_range_check (type, arg00, 0, lo, hi);
6823 if (TREE_OVERFLOW (lo))
6825 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6826 return omit_one_operand (type, tmp, arg00);
6828 return fold_build2 (LT_EXPR, type, arg00, lo);
6831 if (TREE_OVERFLOW (hi))
6833 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6834 return omit_one_operand (type, tmp, arg00);
6836 return fold_build2 (LE_EXPR, type, arg00, hi);
6839 if (TREE_OVERFLOW (hi))
6841 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6842 return omit_one_operand (type, tmp, arg00);
6844 return fold_build2 (GT_EXPR, type, arg00, hi);
6847 if (TREE_OVERFLOW (lo))
6849 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6850 return omit_one_operand (type, tmp, arg00);
6852 return fold_build2 (GE_EXPR, type, arg00, lo);
6862 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6863 equality/inequality test, then return a simplified form of the test
6864 using a sign testing. Otherwise return NULL. TYPE is the desired
6868 fold_single_bit_test_into_sign_test (enum tree_code code, tree arg0, tree arg1,
6871 /* If this is testing a single bit, we can optimize the test. */
6872 if ((code == NE_EXPR || code == EQ_EXPR)
6873 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6874 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6876 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6877 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6878 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6880 if (arg00 != NULL_TREE
6881 /* This is only a win if casting to a signed type is cheap,
6882 i.e. when arg00's type is not a partial mode. */
6883 && TYPE_PRECISION (TREE_TYPE (arg00))
6884 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
6886 tree stype = signed_type_for (TREE_TYPE (arg00));
6887 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6888 result_type, fold_convert (stype, arg00),
6889 build_int_cst (stype, 0));
6896 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6897 equality/inequality test, then return a simplified form of
6898 the test using shifts and logical operations. Otherwise return
6899 NULL. TYPE is the desired result type. */
6902 fold_single_bit_test (enum tree_code code, tree arg0, tree arg1,
6905 /* If this is testing a single bit, we can optimize the test. */
6906 if ((code == NE_EXPR || code == EQ_EXPR)
6907 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6908 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6910 tree inner = TREE_OPERAND (arg0, 0);
6911 tree type = TREE_TYPE (arg0);
6912 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6913 enum machine_mode operand_mode = TYPE_MODE (type);
6915 tree signed_type, unsigned_type, intermediate_type;
6918 /* First, see if we can fold the single bit test into a sign-bit
6920 tem = fold_single_bit_test_into_sign_test (code, arg0, arg1,
6925 /* Otherwise we have (A & C) != 0 where C is a single bit,
6926 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6927 Similarly for (A & C) == 0. */
6929 /* If INNER is a right shift of a constant and it plus BITNUM does
6930 not overflow, adjust BITNUM and INNER. */
6931 if (TREE_CODE (inner) == RSHIFT_EXPR
6932 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6933 && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
6934 && bitnum < TYPE_PRECISION (type)
6935 && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
6936 bitnum - TYPE_PRECISION (type)))
6938 bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
6939 inner = TREE_OPERAND (inner, 0);
6942 /* If we are going to be able to omit the AND below, we must do our
6943 operations as unsigned. If we must use the AND, we have a choice.
6944 Normally unsigned is faster, but for some machines signed is. */
6945 #ifdef LOAD_EXTEND_OP
6946 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6947 && !flag_syntax_only) ? 0 : 1;
6952 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6953 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6954 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6955 inner = fold_convert (intermediate_type, inner);
6958 inner = build2 (RSHIFT_EXPR, intermediate_type,
6959 inner, size_int (bitnum));
6961 one = build_int_cst (intermediate_type, 1);
6963 if (code == EQ_EXPR)
6964 inner = fold_build2 (BIT_XOR_EXPR, intermediate_type, inner, one);
6966 /* Put the AND last so it can combine with more things. */
6967 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6969 /* Make sure to return the proper type. */
6970 inner = fold_convert (result_type, inner);
6977 /* Check whether we are allowed to reorder operands arg0 and arg1,
6978 such that the evaluation of arg1 occurs before arg0. */
6981 reorder_operands_p (const_tree arg0, const_tree arg1)
6983 if (! flag_evaluation_order)
6985 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6987 return ! TREE_SIDE_EFFECTS (arg0)
6988 && ! TREE_SIDE_EFFECTS (arg1);
6991 /* Test whether it is preferable two swap two operands, ARG0 and
6992 ARG1, for example because ARG0 is an integer constant and ARG1
6993 isn't. If REORDER is true, only recommend swapping if we can
6994 evaluate the operands in reverse order. */
6997 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6999 STRIP_SIGN_NOPS (arg0);
7000 STRIP_SIGN_NOPS (arg1);
7002 if (TREE_CODE (arg1) == INTEGER_CST)
7004 if (TREE_CODE (arg0) == INTEGER_CST)
7007 if (TREE_CODE (arg1) == REAL_CST)
7009 if (TREE_CODE (arg0) == REAL_CST)
7012 if (TREE_CODE (arg1) == FIXED_CST)
7014 if (TREE_CODE (arg0) == FIXED_CST)
7017 if (TREE_CODE (arg1) == COMPLEX_CST)
7019 if (TREE_CODE (arg0) == COMPLEX_CST)
7022 if (TREE_CONSTANT (arg1))
7024 if (TREE_CONSTANT (arg0))
7027 if (optimize_function_for_size_p (cfun))
7030 if (reorder && flag_evaluation_order
7031 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
7034 /* It is preferable to swap two SSA_NAME to ensure a canonical form
7035 for commutative and comparison operators. Ensuring a canonical
7036 form allows the optimizers to find additional redundancies without
7037 having to explicitly check for both orderings. */
7038 if (TREE_CODE (arg0) == SSA_NAME
7039 && TREE_CODE (arg1) == SSA_NAME
7040 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
7043 /* Put SSA_NAMEs last. */
7044 if (TREE_CODE (arg1) == SSA_NAME)
7046 if (TREE_CODE (arg0) == SSA_NAME)
7049 /* Put variables last. */
7058 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
7059 ARG0 is extended to a wider type. */
7062 fold_widened_comparison (enum tree_code code, tree type, tree arg0, tree arg1)
7064 tree arg0_unw = get_unwidened (arg0, NULL_TREE);
7066 tree shorter_type, outer_type;
7070 if (arg0_unw == arg0)
7072 shorter_type = TREE_TYPE (arg0_unw);
7074 #ifdef HAVE_canonicalize_funcptr_for_compare
7075 /* Disable this optimization if we're casting a function pointer
7076 type on targets that require function pointer canonicalization. */
7077 if (HAVE_canonicalize_funcptr_for_compare
7078 && TREE_CODE (shorter_type) == POINTER_TYPE
7079 && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
7083 if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
7086 arg1_unw = get_unwidened (arg1, NULL_TREE);
7088 /* If possible, express the comparison in the shorter mode. */
7089 if ((code == EQ_EXPR || code == NE_EXPR
7090 || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
7091 && (TREE_TYPE (arg1_unw) == shorter_type
7092 || ((TYPE_PRECISION (shorter_type)
7093 >= TYPE_PRECISION (TREE_TYPE (arg1_unw)))
7094 && (TYPE_UNSIGNED (shorter_type)
7095 == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
7096 || (TREE_CODE (arg1_unw) == INTEGER_CST
7097 && (TREE_CODE (shorter_type) == INTEGER_TYPE
7098 || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
7099 && int_fits_type_p (arg1_unw, shorter_type))))
7100 return fold_build2 (code, type, arg0_unw,
7101 fold_convert (shorter_type, arg1_unw));
7103 if (TREE_CODE (arg1_unw) != INTEGER_CST
7104 || TREE_CODE (shorter_type) != INTEGER_TYPE
7105 || !int_fits_type_p (arg1_unw, shorter_type))
7108 /* If we are comparing with the integer that does not fit into the range
7109 of the shorter type, the result is known. */
7110 outer_type = TREE_TYPE (arg1_unw);
7111 min = lower_bound_in_type (outer_type, shorter_type);
7112 max = upper_bound_in_type (outer_type, shorter_type);
7114 above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7116 below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7123 return omit_one_operand (type, integer_zero_node, arg0);
7128 return omit_one_operand (type, integer_one_node, arg0);
7134 return omit_one_operand (type, integer_one_node, arg0);
7136 return omit_one_operand (type, integer_zero_node, arg0);
7141 return omit_one_operand (type, integer_zero_node, arg0);
7143 return omit_one_operand (type, integer_one_node, arg0);
7152 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
7153 ARG0 just the signedness is changed. */
7156 fold_sign_changed_comparison (enum tree_code code, tree type,
7157 tree arg0, tree arg1)
7160 tree inner_type, outer_type;
7162 if (!CONVERT_EXPR_P (arg0))
7165 outer_type = TREE_TYPE (arg0);
7166 arg0_inner = TREE_OPERAND (arg0, 0);
7167 inner_type = TREE_TYPE (arg0_inner);
7169 #ifdef HAVE_canonicalize_funcptr_for_compare
7170 /* Disable this optimization if we're casting a function pointer
7171 type on targets that require function pointer canonicalization. */
7172 if (HAVE_canonicalize_funcptr_for_compare
7173 && TREE_CODE (inner_type) == POINTER_TYPE
7174 && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
7178 if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
7181 /* If the conversion is from an integral subtype to its basetype
7183 if (TREE_TYPE (inner_type) == outer_type)
7186 if (TREE_CODE (arg1) != INTEGER_CST
7187 && !(CONVERT_EXPR_P (arg1)
7188 && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
7191 if ((TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
7192 || POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type))
7197 if (TREE_CODE (arg1) == INTEGER_CST)
7198 arg1 = force_fit_type_double (inner_type, TREE_INT_CST_LOW (arg1),
7199 TREE_INT_CST_HIGH (arg1), 0,
7200 TREE_OVERFLOW (arg1));
7202 arg1 = fold_convert (inner_type, arg1);
7204 return fold_build2 (code, type, arg0_inner, arg1);
7207 /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
7208 step of the array. Reconstructs s and delta in the case of s * delta
7209 being an integer constant (and thus already folded).
7210 ADDR is the address. MULT is the multiplicative expression.
7211 If the function succeeds, the new address expression is returned. Otherwise
7212 NULL_TREE is returned. */
7215 try_move_mult_to_index (tree addr, tree op1)
7217 tree s, delta, step;
7218 tree ref = TREE_OPERAND (addr, 0), pref;
7223 /* Strip the nops that might be added when converting op1 to sizetype. */
7226 /* Canonicalize op1 into a possibly non-constant delta
7227 and an INTEGER_CST s. */
7228 if (TREE_CODE (op1) == MULT_EXPR)
7230 tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
7235 if (TREE_CODE (arg0) == INTEGER_CST)
7240 else if (TREE_CODE (arg1) == INTEGER_CST)
7248 else if (TREE_CODE (op1) == INTEGER_CST)
7255 /* Simulate we are delta * 1. */
7257 s = integer_one_node;
7260 for (;; ref = TREE_OPERAND (ref, 0))
7262 if (TREE_CODE (ref) == ARRAY_REF)
7264 /* Remember if this was a multi-dimensional array. */
7265 if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
7268 itype = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
7272 step = array_ref_element_size (ref);
7273 if (TREE_CODE (step) != INTEGER_CST)
7278 if (! tree_int_cst_equal (step, s))
7283 /* Try if delta is a multiple of step. */
7284 tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step);
7290 /* Only fold here if we can verify we do not overflow one
7291 dimension of a multi-dimensional array. */
7296 if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
7297 || !INTEGRAL_TYPE_P (itype)
7298 || !TYPE_MAX_VALUE (itype)
7299 || TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST)
7302 tmp = fold_binary (PLUS_EXPR, itype,
7303 fold_convert (itype,
7304 TREE_OPERAND (ref, 1)),
7305 fold_convert (itype, delta));
7307 || TREE_CODE (tmp) != INTEGER_CST
7308 || tree_int_cst_lt (TYPE_MAX_VALUE (itype), tmp))
7317 if (!handled_component_p (ref))
7321 /* We found the suitable array reference. So copy everything up to it,
7322 and replace the index. */
7324 pref = TREE_OPERAND (addr, 0);
7325 ret = copy_node (pref);
7330 pref = TREE_OPERAND (pref, 0);
7331 TREE_OPERAND (pos, 0) = copy_node (pref);
7332 pos = TREE_OPERAND (pos, 0);
7335 TREE_OPERAND (pos, 1) = fold_build2 (PLUS_EXPR, itype,
7336 fold_convert (itype,
7337 TREE_OPERAND (pos, 1)),
7338 fold_convert (itype, delta));
7340 return fold_build1 (ADDR_EXPR, TREE_TYPE (addr), ret);
7344 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
7345 means A >= Y && A != MAX, but in this case we know that
7346 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7349 fold_to_nonsharp_ineq_using_bound (tree ineq, tree bound)
7351 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
7353 if (TREE_CODE (bound) == LT_EXPR)
7354 a = TREE_OPERAND (bound, 0);
7355 else if (TREE_CODE (bound) == GT_EXPR)
7356 a = TREE_OPERAND (bound, 1);
7360 typea = TREE_TYPE (a);
7361 if (!INTEGRAL_TYPE_P (typea)
7362 && !POINTER_TYPE_P (typea))
7365 if (TREE_CODE (ineq) == LT_EXPR)
7367 a1 = TREE_OPERAND (ineq, 1);
7368 y = TREE_OPERAND (ineq, 0);
7370 else if (TREE_CODE (ineq) == GT_EXPR)
7372 a1 = TREE_OPERAND (ineq, 0);
7373 y = TREE_OPERAND (ineq, 1);
7378 if (TREE_TYPE (a1) != typea)
7381 if (POINTER_TYPE_P (typea))
7383 /* Convert the pointer types into integer before taking the difference. */
7384 tree ta = fold_convert (ssizetype, a);
7385 tree ta1 = fold_convert (ssizetype, a1);
7386 diff = fold_binary (MINUS_EXPR, ssizetype, ta1, ta);
7389 diff = fold_binary (MINUS_EXPR, typea, a1, a);
7391 if (!diff || !integer_onep (diff))
7394 return fold_build2 (GE_EXPR, type, a, y);
7397 /* Fold a sum or difference of at least one multiplication.
7398 Returns the folded tree or NULL if no simplification could be made. */
7401 fold_plusminus_mult_expr (enum tree_code code, tree type, tree arg0, tree arg1)
7403 tree arg00, arg01, arg10, arg11;
7404 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7406 /* (A * C) +- (B * C) -> (A+-B) * C.
7407 (A * C) +- A -> A * (C+-1).
7408 We are most concerned about the case where C is a constant,
7409 but other combinations show up during loop reduction. Since
7410 it is not difficult, try all four possibilities. */
7412 if (TREE_CODE (arg0) == MULT_EXPR)
7414 arg00 = TREE_OPERAND (arg0, 0);
7415 arg01 = TREE_OPERAND (arg0, 1);
7417 else if (TREE_CODE (arg0) == INTEGER_CST)
7419 arg00 = build_one_cst (type);
7424 /* We cannot generate constant 1 for fract. */
7425 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7428 arg01 = build_one_cst (type);
7430 if (TREE_CODE (arg1) == MULT_EXPR)
7432 arg10 = TREE_OPERAND (arg1, 0);
7433 arg11 = TREE_OPERAND (arg1, 1);
7435 else if (TREE_CODE (arg1) == INTEGER_CST)
7437 arg10 = build_one_cst (type);
7438 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7439 the purpose of this canonicalization. */
7440 if (TREE_INT_CST_HIGH (arg1) == -1
7441 && negate_expr_p (arg1)
7442 && code == PLUS_EXPR)
7444 arg11 = negate_expr (arg1);
7452 /* We cannot generate constant 1 for fract. */
7453 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7456 arg11 = build_one_cst (type);
7460 if (operand_equal_p (arg01, arg11, 0))
7461 same = arg01, alt0 = arg00, alt1 = arg10;
7462 else if (operand_equal_p (arg00, arg10, 0))
7463 same = arg00, alt0 = arg01, alt1 = arg11;
7464 else if (operand_equal_p (arg00, arg11, 0))
7465 same = arg00, alt0 = arg01, alt1 = arg10;
7466 else if (operand_equal_p (arg01, arg10, 0))
7467 same = arg01, alt0 = arg00, alt1 = arg11;
7469 /* No identical multiplicands; see if we can find a common
7470 power-of-two factor in non-power-of-two multiplies. This
7471 can help in multi-dimensional array access. */
7472 else if (host_integerp (arg01, 0)
7473 && host_integerp (arg11, 0))
7475 HOST_WIDE_INT int01, int11, tmp;
7478 int01 = TREE_INT_CST_LOW (arg01);
7479 int11 = TREE_INT_CST_LOW (arg11);
7481 /* Move min of absolute values to int11. */
7482 if ((int01 >= 0 ? int01 : -int01)
7483 < (int11 >= 0 ? int11 : -int11))
7485 tmp = int01, int01 = int11, int11 = tmp;
7486 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7493 if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0)
7495 alt0 = fold_build2 (MULT_EXPR, TREE_TYPE (arg00), arg00,
7496 build_int_cst (TREE_TYPE (arg00),
7501 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7506 return fold_build2 (MULT_EXPR, type,
7507 fold_build2 (code, type,
7508 fold_convert (type, alt0),
7509 fold_convert (type, alt1)),
7510 fold_convert (type, same));
7515 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7516 specified by EXPR into the buffer PTR of length LEN bytes.
7517 Return the number of bytes placed in the buffer, or zero
7521 native_encode_int (const_tree expr, unsigned char *ptr, int len)
7523 tree type = TREE_TYPE (expr);
7524 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7525 int byte, offset, word, words;
7526 unsigned char value;
7528 if (total_bytes > len)
7530 words = total_bytes / UNITS_PER_WORD;
7532 for (byte = 0; byte < total_bytes; byte++)
7534 int bitpos = byte * BITS_PER_UNIT;
7535 if (bitpos < HOST_BITS_PER_WIDE_INT)
7536 value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7538 value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7539 >> (bitpos - HOST_BITS_PER_WIDE_INT));
7541 if (total_bytes > UNITS_PER_WORD)
7543 word = byte / UNITS_PER_WORD;
7544 if (WORDS_BIG_ENDIAN)
7545 word = (words - 1) - word;
7546 offset = word * UNITS_PER_WORD;
7547 if (BYTES_BIG_ENDIAN)
7548 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7550 offset += byte % UNITS_PER_WORD;
7553 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7554 ptr[offset] = value;
7560 /* Subroutine of native_encode_expr. Encode the REAL_CST
7561 specified by EXPR into the buffer PTR of length LEN bytes.
7562 Return the number of bytes placed in the buffer, or zero
7566 native_encode_real (const_tree expr, unsigned char *ptr, int len)
7568 tree type = TREE_TYPE (expr);
7569 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7570 int byte, offset, word, words, bitpos;
7571 unsigned char value;
7573 /* There are always 32 bits in each long, no matter the size of
7574 the hosts long. We handle floating point representations with
7578 if (total_bytes > len)
7580 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7582 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7584 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7585 bitpos += BITS_PER_UNIT)
7587 byte = (bitpos / BITS_PER_UNIT) & 3;
7588 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7590 if (UNITS_PER_WORD < 4)
7592 word = byte / UNITS_PER_WORD;
7593 if (WORDS_BIG_ENDIAN)
7594 word = (words - 1) - word;
7595 offset = word * UNITS_PER_WORD;
7596 if (BYTES_BIG_ENDIAN)
7597 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7599 offset += byte % UNITS_PER_WORD;
7602 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7603 ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
7608 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7609 specified by EXPR into the buffer PTR of length LEN bytes.
7610 Return the number of bytes placed in the buffer, or zero
7614 native_encode_complex (const_tree expr, unsigned char *ptr, int len)
7619 part = TREE_REALPART (expr);
7620 rsize = native_encode_expr (part, ptr, len);
7623 part = TREE_IMAGPART (expr);
7624 isize = native_encode_expr (part, ptr+rsize, len-rsize);
7627 return rsize + isize;
7631 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7632 specified by EXPR into the buffer PTR of length LEN bytes.
7633 Return the number of bytes placed in the buffer, or zero
7637 native_encode_vector (const_tree expr, unsigned char *ptr, int len)
7639 int i, size, offset, count;
7640 tree itype, elem, elements;
7643 elements = TREE_VECTOR_CST_ELTS (expr);
7644 count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
7645 itype = TREE_TYPE (TREE_TYPE (expr));
7646 size = GET_MODE_SIZE (TYPE_MODE (itype));
7647 for (i = 0; i < count; i++)
7651 elem = TREE_VALUE (elements);
7652 elements = TREE_CHAIN (elements);
7659 if (native_encode_expr (elem, ptr+offset, len-offset) != size)
7664 if (offset + size > len)
7666 memset (ptr+offset, 0, size);
7674 /* Subroutine of native_encode_expr. Encode the STRING_CST
7675 specified by EXPR into the buffer PTR of length LEN bytes.
7676 Return the number of bytes placed in the buffer, or zero
7680 native_encode_string (const_tree expr, unsigned char *ptr, int len)
7682 tree type = TREE_TYPE (expr);
7683 HOST_WIDE_INT total_bytes;
7685 if (TREE_CODE (type) != ARRAY_TYPE
7686 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7687 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7688 || !host_integerp (TYPE_SIZE_UNIT (type), 0))
7690 total_bytes = tree_low_cst (TYPE_SIZE_UNIT (type), 0);
7691 if (total_bytes > len)
7693 if (TREE_STRING_LENGTH (expr) < total_bytes)
7695 memcpy (ptr, TREE_STRING_POINTER (expr), TREE_STRING_LENGTH (expr));
7696 memset (ptr + TREE_STRING_LENGTH (expr), 0,
7697 total_bytes - TREE_STRING_LENGTH (expr));
7700 memcpy (ptr, TREE_STRING_POINTER (expr), total_bytes);
7705 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7706 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7707 buffer PTR of length LEN bytes. Return the number of bytes
7708 placed in the buffer, or zero upon failure. */
7711 native_encode_expr (const_tree expr, unsigned char *ptr, int len)
7713 switch (TREE_CODE (expr))
7716 return native_encode_int (expr, ptr, len);
7719 return native_encode_real (expr, ptr, len);
7722 return native_encode_complex (expr, ptr, len);
7725 return native_encode_vector (expr, ptr, len);
7728 return native_encode_string (expr, ptr, len);
7736 /* Subroutine of native_interpret_expr. Interpret the contents of
7737 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7738 If the buffer cannot be interpreted, return NULL_TREE. */
7741 native_interpret_int (tree type, const unsigned char *ptr, int len)
7743 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7744 int byte, offset, word, words;
7745 unsigned char value;
7746 unsigned int HOST_WIDE_INT lo = 0;
7747 HOST_WIDE_INT hi = 0;
7749 if (total_bytes > len)
7751 if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
7753 words = total_bytes / UNITS_PER_WORD;
7755 for (byte = 0; byte < total_bytes; byte++)
7757 int bitpos = byte * BITS_PER_UNIT;
7758 if (total_bytes > UNITS_PER_WORD)
7760 word = byte / UNITS_PER_WORD;
7761 if (WORDS_BIG_ENDIAN)
7762 word = (words - 1) - word;
7763 offset = word * UNITS_PER_WORD;
7764 if (BYTES_BIG_ENDIAN)
7765 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7767 offset += byte % UNITS_PER_WORD;
7770 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7771 value = ptr[offset];
7773 if (bitpos < HOST_BITS_PER_WIDE_INT)
7774 lo |= (unsigned HOST_WIDE_INT) value << bitpos;
7776 hi |= (unsigned HOST_WIDE_INT) value
7777 << (bitpos - HOST_BITS_PER_WIDE_INT);
7780 return build_int_cst_wide_type (type, lo, hi);
7784 /* Subroutine of native_interpret_expr. Interpret the contents of
7785 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7786 If the buffer cannot be interpreted, return NULL_TREE. */
7789 native_interpret_real (tree type, const unsigned char *ptr, int len)
7791 enum machine_mode mode = TYPE_MODE (type);
7792 int total_bytes = GET_MODE_SIZE (mode);
7793 int byte, offset, word, words, bitpos;
7794 unsigned char value;
7795 /* There are always 32 bits in each long, no matter the size of
7796 the hosts long. We handle floating point representations with
7801 total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7802 if (total_bytes > len || total_bytes > 24)
7804 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7806 memset (tmp, 0, sizeof (tmp));
7807 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7808 bitpos += BITS_PER_UNIT)
7810 byte = (bitpos / BITS_PER_UNIT) & 3;
7811 if (UNITS_PER_WORD < 4)
7813 word = byte / UNITS_PER_WORD;
7814 if (WORDS_BIG_ENDIAN)
7815 word = (words - 1) - word;
7816 offset = word * UNITS_PER_WORD;
7817 if (BYTES_BIG_ENDIAN)
7818 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7820 offset += byte % UNITS_PER_WORD;
7823 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7824 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7826 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7829 real_from_target (&r, tmp, mode);
7830 return build_real (type, r);
7834 /* Subroutine of native_interpret_expr. Interpret the contents of
7835 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7836 If the buffer cannot be interpreted, return NULL_TREE. */
7839 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7841 tree etype, rpart, ipart;
7844 etype = TREE_TYPE (type);
7845 size = GET_MODE_SIZE (TYPE_MODE (etype));
7848 rpart = native_interpret_expr (etype, ptr, size);
7851 ipart = native_interpret_expr (etype, ptr+size, size);
7854 return build_complex (type, rpart, ipart);
7858 /* Subroutine of native_interpret_expr. Interpret the contents of
7859 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7860 If the buffer cannot be interpreted, return NULL_TREE. */
7863 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7865 tree etype, elem, elements;
7868 etype = TREE_TYPE (type);
7869 size = GET_MODE_SIZE (TYPE_MODE (etype));
7870 count = TYPE_VECTOR_SUBPARTS (type);
7871 if (size * count > len)
7874 elements = NULL_TREE;
7875 for (i = count - 1; i >= 0; i--)
7877 elem = native_interpret_expr (etype, ptr+(i*size), size);
7880 elements = tree_cons (NULL_TREE, elem, elements);
7882 return build_vector (type, elements);
7886 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7887 the buffer PTR of length LEN as a constant of type TYPE. For
7888 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7889 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7890 return NULL_TREE. */
7893 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7895 switch (TREE_CODE (type))
7900 return native_interpret_int (type, ptr, len);
7903 return native_interpret_real (type, ptr, len);
7906 return native_interpret_complex (type, ptr, len);
7909 return native_interpret_vector (type, ptr, len);
7917 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7918 TYPE at compile-time. If we're unable to perform the conversion
7919 return NULL_TREE. */
7922 fold_view_convert_expr (tree type, tree expr)
7924 /* We support up to 512-bit values (for V8DFmode). */
7925 unsigned char buffer[64];
7928 /* Check that the host and target are sane. */
7929 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7932 len = native_encode_expr (expr, buffer, sizeof (buffer));
7936 return native_interpret_expr (type, buffer, len);
7939 /* Build an expression for the address of T. Folds away INDIRECT_REF
7940 to avoid confusing the gimplify process. When IN_FOLD is true
7941 avoid modifications of T. */
7944 build_fold_addr_expr_with_type_1 (tree t, tree ptrtype, bool in_fold)
7946 /* The size of the object is not relevant when talking about its address. */
7947 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7948 t = TREE_OPERAND (t, 0);
7950 /* Note: doesn't apply to ALIGN_INDIRECT_REF */
7951 if (TREE_CODE (t) == INDIRECT_REF
7952 || TREE_CODE (t) == MISALIGNED_INDIRECT_REF)
7954 t = TREE_OPERAND (t, 0);
7956 if (TREE_TYPE (t) != ptrtype)
7957 t = build1 (NOP_EXPR, ptrtype, t);
7963 while (handled_component_p (base))
7964 base = TREE_OPERAND (base, 0);
7967 TREE_ADDRESSABLE (base) = 1;
7969 t = build1 (ADDR_EXPR, ptrtype, t);
7972 t = build1 (ADDR_EXPR, ptrtype, t);
7977 /* Build an expression for the address of T with type PTRTYPE. This
7978 function modifies the input parameter 'T' by sometimes setting the
7979 TREE_ADDRESSABLE flag. */
7982 build_fold_addr_expr_with_type (tree t, tree ptrtype)
7984 return build_fold_addr_expr_with_type_1 (t, ptrtype, false);
7987 /* Build an expression for the address of T. This function modifies
7988 the input parameter 'T' by sometimes setting the TREE_ADDRESSABLE
7989 flag. When called from fold functions, use fold_addr_expr instead. */
7992 build_fold_addr_expr (tree t)
7994 return build_fold_addr_expr_with_type_1 (t,
7995 build_pointer_type (TREE_TYPE (t)),
7999 /* Same as build_fold_addr_expr, builds an expression for the address
8000 of T, but avoids touching the input node 't'. Fold functions
8001 should use this version. */
8004 fold_addr_expr (tree t)
8006 tree ptrtype = build_pointer_type (TREE_TYPE (t));
8008 return build_fold_addr_expr_with_type_1 (t, ptrtype, true);
8011 /* Fold a unary expression of code CODE and type TYPE with operand
8012 OP0. Return the folded expression if folding is successful.
8013 Otherwise, return NULL_TREE. */
8016 fold_unary (enum tree_code code, tree type, tree op0)
8020 enum tree_code_class kind = TREE_CODE_CLASS (code);
8022 gcc_assert (IS_EXPR_CODE_CLASS (kind)
8023 && TREE_CODE_LENGTH (code) == 1);
8028 if (CONVERT_EXPR_CODE_P (code)
8029 || code == FLOAT_EXPR || code == ABS_EXPR)
8031 /* Don't use STRIP_NOPS, because signedness of argument type
8033 STRIP_SIGN_NOPS (arg0);
8037 /* Strip any conversions that don't change the mode. This
8038 is safe for every expression, except for a comparison
8039 expression because its signedness is derived from its
8042 Note that this is done as an internal manipulation within
8043 the constant folder, in order to find the simplest
8044 representation of the arguments so that their form can be
8045 studied. In any cases, the appropriate type conversions
8046 should be put back in the tree that will get out of the
8052 if (TREE_CODE_CLASS (code) == tcc_unary)
8054 if (TREE_CODE (arg0) == COMPOUND_EXPR)
8055 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
8056 fold_build1 (code, type,
8057 fold_convert (TREE_TYPE (op0),
8058 TREE_OPERAND (arg0, 1))));
8059 else if (TREE_CODE (arg0) == COND_EXPR)
8061 tree arg01 = TREE_OPERAND (arg0, 1);
8062 tree arg02 = TREE_OPERAND (arg0, 2);
8063 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
8064 arg01 = fold_build1 (code, type,
8065 fold_convert (TREE_TYPE (op0), arg01));
8066 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
8067 arg02 = fold_build1 (code, type,
8068 fold_convert (TREE_TYPE (op0), arg02));
8069 tem = fold_build3 (COND_EXPR, type, TREE_OPERAND (arg0, 0),
8072 /* If this was a conversion, and all we did was to move into
8073 inside the COND_EXPR, bring it back out. But leave it if
8074 it is a conversion from integer to integer and the
8075 result precision is no wider than a word since such a
8076 conversion is cheap and may be optimized away by combine,
8077 while it couldn't if it were outside the COND_EXPR. Then return
8078 so we don't get into an infinite recursion loop taking the
8079 conversion out and then back in. */
8081 if ((CONVERT_EXPR_CODE_P (code)
8082 || code == NON_LVALUE_EXPR)
8083 && TREE_CODE (tem) == COND_EXPR
8084 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
8085 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
8086 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
8087 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
8088 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
8089 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
8090 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8092 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
8093 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
8094 || flag_syntax_only))
8095 tem = build1 (code, type,
8097 TREE_TYPE (TREE_OPERAND
8098 (TREE_OPERAND (tem, 1), 0)),
8099 TREE_OPERAND (tem, 0),
8100 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
8101 TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
8104 else if (COMPARISON_CLASS_P (arg0))
8106 if (TREE_CODE (type) == BOOLEAN_TYPE)
8108 arg0 = copy_node (arg0);
8109 TREE_TYPE (arg0) = type;
8112 else if (TREE_CODE (type) != INTEGER_TYPE)
8113 return fold_build3 (COND_EXPR, type, arg0,
8114 fold_build1 (code, type,
8116 fold_build1 (code, type,
8117 integer_zero_node));
8124 /* Re-association barriers around constants and other re-association
8125 barriers can be removed. */
8126 if (CONSTANT_CLASS_P (op0)
8127 || TREE_CODE (op0) == PAREN_EXPR)
8128 return fold_convert (type, op0);
8133 case FIX_TRUNC_EXPR:
8134 if (TREE_TYPE (op0) == type)
8137 /* If we have (type) (a CMP b) and type is an integral type, return
8138 new expression involving the new type. */
8139 if (COMPARISON_CLASS_P (op0) && INTEGRAL_TYPE_P (type))
8140 return fold_build2 (TREE_CODE (op0), type, TREE_OPERAND (op0, 0),
8141 TREE_OPERAND (op0, 1));
8143 /* Handle cases of two conversions in a row. */
8144 if (CONVERT_EXPR_P (op0))
8146 tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
8147 tree inter_type = TREE_TYPE (op0);
8148 int inside_int = INTEGRAL_TYPE_P (inside_type);
8149 int inside_ptr = POINTER_TYPE_P (inside_type);
8150 int inside_float = FLOAT_TYPE_P (inside_type);
8151 int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
8152 unsigned int inside_prec = TYPE_PRECISION (inside_type);
8153 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
8154 int inter_int = INTEGRAL_TYPE_P (inter_type);
8155 int inter_ptr = POINTER_TYPE_P (inter_type);
8156 int inter_float = FLOAT_TYPE_P (inter_type);
8157 int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
8158 unsigned int inter_prec = TYPE_PRECISION (inter_type);
8159 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
8160 int final_int = INTEGRAL_TYPE_P (type);
8161 int final_ptr = POINTER_TYPE_P (type);
8162 int final_float = FLOAT_TYPE_P (type);
8163 int final_vec = TREE_CODE (type) == VECTOR_TYPE;
8164 unsigned int final_prec = TYPE_PRECISION (type);
8165 int final_unsignedp = TYPE_UNSIGNED (type);
8167 /* In addition to the cases of two conversions in a row
8168 handled below, if we are converting something to its own
8169 type via an object of identical or wider precision, neither
8170 conversion is needed. */
8171 if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
8172 && (((inter_int || inter_ptr) && final_int)
8173 || (inter_float && final_float))
8174 && inter_prec >= final_prec)
8175 return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8177 /* Likewise, if the intermediate and final types are either both
8178 float or both integer, we don't need the middle conversion if
8179 it is wider than the final type and doesn't change the signedness
8180 (for integers). Avoid this if the final type is a pointer
8181 since then we sometimes need the inner conversion. Likewise if
8182 the outer has a precision not equal to the size of its mode. */
8183 if (((inter_int && inside_int)
8184 || (inter_float && inside_float)
8185 || (inter_vec && inside_vec))
8186 && inter_prec >= inside_prec
8187 && (inter_float || inter_vec
8188 || inter_unsignedp == inside_unsignedp)
8189 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
8190 && TYPE_MODE (type) == TYPE_MODE (inter_type))
8192 && (! final_vec || inter_prec == inside_prec))
8193 return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8195 /* If we have a sign-extension of a zero-extended value, we can
8196 replace that by a single zero-extension. */
8197 if (inside_int && inter_int && final_int
8198 && inside_prec < inter_prec && inter_prec < final_prec
8199 && inside_unsignedp && !inter_unsignedp)
8200 return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8202 /* Two conversions in a row are not needed unless:
8203 - some conversion is floating-point (overstrict for now), or
8204 - some conversion is a vector (overstrict for now), or
8205 - the intermediate type is narrower than both initial and
8207 - the intermediate type and innermost type differ in signedness,
8208 and the outermost type is wider than the intermediate, or
8209 - the initial type is a pointer type and the precisions of the
8210 intermediate and final types differ, or
8211 - the final type is a pointer type and the precisions of the
8212 initial and intermediate types differ. */
8213 if (! inside_float && ! inter_float && ! final_float
8214 && ! inside_vec && ! inter_vec && ! final_vec
8215 && (inter_prec >= inside_prec || inter_prec >= final_prec)
8216 && ! (inside_int && inter_int
8217 && inter_unsignedp != inside_unsignedp
8218 && inter_prec < final_prec)
8219 && ((inter_unsignedp && inter_prec > inside_prec)
8220 == (final_unsignedp && final_prec > inter_prec))
8221 && ! (inside_ptr && inter_prec != final_prec)
8222 && ! (final_ptr && inside_prec != inter_prec)
8223 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
8224 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
8225 return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8228 /* Handle (T *)&A.B.C for A being of type T and B and C
8229 living at offset zero. This occurs frequently in
8230 C++ upcasting and then accessing the base. */
8231 if (TREE_CODE (op0) == ADDR_EXPR
8232 && POINTER_TYPE_P (type)
8233 && handled_component_p (TREE_OPERAND (op0, 0)))
8235 HOST_WIDE_INT bitsize, bitpos;
8237 enum machine_mode mode;
8238 int unsignedp, volatilep;
8239 tree base = TREE_OPERAND (op0, 0);
8240 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
8241 &mode, &unsignedp, &volatilep, false);
8242 /* If the reference was to a (constant) zero offset, we can use
8243 the address of the base if it has the same base type
8244 as the result type. */
8245 if (! offset && bitpos == 0
8246 && TYPE_MAIN_VARIANT (TREE_TYPE (type))
8247 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
8248 return fold_convert (type, fold_addr_expr (base));
8251 if (TREE_CODE (op0) == MODIFY_EXPR
8252 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
8253 /* Detect assigning a bitfield. */
8254 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
8256 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
8258 /* Don't leave an assignment inside a conversion
8259 unless assigning a bitfield. */
8260 tem = fold_build1 (code, type, TREE_OPERAND (op0, 1));
8261 /* First do the assignment, then return converted constant. */
8262 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
8263 TREE_NO_WARNING (tem) = 1;
8264 TREE_USED (tem) = 1;
8268 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
8269 constants (if x has signed type, the sign bit cannot be set
8270 in c). This folds extension into the BIT_AND_EXPR.
8271 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
8272 very likely don't have maximal range for their precision and this
8273 transformation effectively doesn't preserve non-maximal ranges. */
8274 if (TREE_CODE (type) == INTEGER_TYPE
8275 && TREE_CODE (op0) == BIT_AND_EXPR
8276 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST
8277 /* Not if the conversion is to the sub-type. */
8278 && TREE_TYPE (type) != TREE_TYPE (op0))
8281 tree and0 = TREE_OPERAND (and, 0), and1 = TREE_OPERAND (and, 1);
8284 if (TYPE_UNSIGNED (TREE_TYPE (and))
8285 || (TYPE_PRECISION (type)
8286 <= TYPE_PRECISION (TREE_TYPE (and))))
8288 else if (TYPE_PRECISION (TREE_TYPE (and1))
8289 <= HOST_BITS_PER_WIDE_INT
8290 && host_integerp (and1, 1))
8292 unsigned HOST_WIDE_INT cst;
8294 cst = tree_low_cst (and1, 1);
8295 cst &= (HOST_WIDE_INT) -1
8296 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
8297 change = (cst == 0);
8298 #ifdef LOAD_EXTEND_OP
8300 && !flag_syntax_only
8301 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
8304 tree uns = unsigned_type_for (TREE_TYPE (and0));
8305 and0 = fold_convert (uns, and0);
8306 and1 = fold_convert (uns, and1);
8312 tem = force_fit_type_double (type, TREE_INT_CST_LOW (and1),
8313 TREE_INT_CST_HIGH (and1), 0,
8314 TREE_OVERFLOW (and1));
8315 return fold_build2 (BIT_AND_EXPR, type,
8316 fold_convert (type, and0), tem);
8320 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
8321 when one of the new casts will fold away. Conservatively we assume
8322 that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
8323 if (POINTER_TYPE_P (type)
8324 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
8325 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8326 || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
8327 || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
8329 tree arg00 = TREE_OPERAND (arg0, 0);
8330 tree arg01 = TREE_OPERAND (arg0, 1);
8332 return fold_build2 (TREE_CODE (arg0), type, fold_convert (type, arg00),
8333 fold_convert (sizetype, arg01));
8336 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
8337 of the same precision, and X is an integer type not narrower than
8338 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
8339 if (INTEGRAL_TYPE_P (type)
8340 && TREE_CODE (op0) == BIT_NOT_EXPR
8341 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8342 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
8343 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8345 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
8346 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8347 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
8348 return fold_build1 (BIT_NOT_EXPR, type, fold_convert (type, tem));
8351 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
8352 type of X and Y (integer types only). */
8353 if (INTEGRAL_TYPE_P (type)
8354 && TREE_CODE (op0) == MULT_EXPR
8355 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8356 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
8358 /* Be careful not to introduce new overflows. */
8360 if (TYPE_OVERFLOW_WRAPS (type))
8363 mult_type = unsigned_type_for (type);
8365 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
8367 tem = fold_build2 (MULT_EXPR, mult_type,
8368 fold_convert (mult_type,
8369 TREE_OPERAND (op0, 0)),
8370 fold_convert (mult_type,
8371 TREE_OPERAND (op0, 1)));
8372 return fold_convert (type, tem);
8376 tem = fold_convert_const (code, type, op0);
8377 return tem ? tem : NULL_TREE;
8379 case FIXED_CONVERT_EXPR:
8380 tem = fold_convert_const (code, type, arg0);
8381 return tem ? tem : NULL_TREE;
8383 case VIEW_CONVERT_EXPR:
8384 if (TREE_TYPE (op0) == type)
8386 if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
8387 return fold_build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
8389 /* For integral conversions with the same precision or pointer
8390 conversions use a NOP_EXPR instead. */
8391 if ((INTEGRAL_TYPE_P (type)
8392 || POINTER_TYPE_P (type))
8393 && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8394 || POINTER_TYPE_P (TREE_TYPE (op0)))
8395 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0))
8396 /* Do not muck with VIEW_CONVERT_EXPRs that convert from
8397 a sub-type to its base type as generated by the Ada FE. */
8398 && !(INTEGRAL_TYPE_P (TREE_TYPE (op0))
8399 && TREE_TYPE (TREE_TYPE (op0))))
8400 return fold_convert (type, op0);
8402 /* Strip inner integral conversions that do not change the precision. */
8403 if (CONVERT_EXPR_P (op0)
8404 && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8405 || POINTER_TYPE_P (TREE_TYPE (op0)))
8406 && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))
8407 || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
8408 && (TYPE_PRECISION (TREE_TYPE (op0))
8409 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
8410 return fold_build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
8412 return fold_view_convert_expr (type, op0);
8415 tem = fold_negate_expr (arg0);
8417 return fold_convert (type, tem);
8421 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
8422 return fold_abs_const (arg0, type);
8423 else if (TREE_CODE (arg0) == NEGATE_EXPR)
8424 return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
8425 /* Convert fabs((double)float) into (double)fabsf(float). */
8426 else if (TREE_CODE (arg0) == NOP_EXPR
8427 && TREE_CODE (type) == REAL_TYPE)
8429 tree targ0 = strip_float_extensions (arg0);
8431 return fold_convert (type, fold_build1 (ABS_EXPR,
8435 /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on. */
8436 else if (TREE_CODE (arg0) == ABS_EXPR)
8438 else if (tree_expr_nonnegative_p (arg0))
8441 /* Strip sign ops from argument. */
8442 if (TREE_CODE (type) == REAL_TYPE)
8444 tem = fold_strip_sign_ops (arg0);
8446 return fold_build1 (ABS_EXPR, type, fold_convert (type, tem));
8451 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8452 return fold_convert (type, arg0);
8453 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8455 tree itype = TREE_TYPE (type);
8456 tree rpart = fold_convert (itype, TREE_OPERAND (arg0, 0));
8457 tree ipart = fold_convert (itype, TREE_OPERAND (arg0, 1));
8458 return fold_build2 (COMPLEX_EXPR, type, rpart, negate_expr (ipart));
8460 if (TREE_CODE (arg0) == COMPLEX_CST)
8462 tree itype = TREE_TYPE (type);
8463 tree rpart = fold_convert (itype, TREE_REALPART (arg0));
8464 tree ipart = fold_convert (itype, TREE_IMAGPART (arg0));
8465 return build_complex (type, rpart, negate_expr (ipart));
8467 if (TREE_CODE (arg0) == CONJ_EXPR)
8468 return fold_convert (type, TREE_OPERAND (arg0, 0));
8472 if (TREE_CODE (arg0) == INTEGER_CST)
8473 return fold_not_const (arg0, type);
8474 else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8475 return fold_convert (type, TREE_OPERAND (arg0, 0));
8476 /* Convert ~ (-A) to A - 1. */
8477 else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8478 return fold_build2 (MINUS_EXPR, type,
8479 fold_convert (type, TREE_OPERAND (arg0, 0)),
8480 build_int_cst (type, 1));
8481 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
8482 else if (INTEGRAL_TYPE_P (type)
8483 && ((TREE_CODE (arg0) == MINUS_EXPR
8484 && integer_onep (TREE_OPERAND (arg0, 1)))
8485 || (TREE_CODE (arg0) == PLUS_EXPR
8486 && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8487 return fold_build1 (NEGATE_EXPR, type,
8488 fold_convert (type, TREE_OPERAND (arg0, 0)));
8489 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
8490 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8491 && (tem = fold_unary (BIT_NOT_EXPR, type,
8493 TREE_OPERAND (arg0, 0)))))
8494 return fold_build2 (BIT_XOR_EXPR, type, tem,
8495 fold_convert (type, TREE_OPERAND (arg0, 1)));
8496 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8497 && (tem = fold_unary (BIT_NOT_EXPR, type,
8499 TREE_OPERAND (arg0, 1)))))
8500 return fold_build2 (BIT_XOR_EXPR, type,
8501 fold_convert (type, TREE_OPERAND (arg0, 0)), tem);
8502 /* Perform BIT_NOT_EXPR on each element individually. */
8503 else if (TREE_CODE (arg0) == VECTOR_CST)
8505 tree elements = TREE_VECTOR_CST_ELTS (arg0), elem, list = NULL_TREE;
8506 int count = TYPE_VECTOR_SUBPARTS (type), i;
8508 for (i = 0; i < count; i++)
8512 elem = TREE_VALUE (elements);
8513 elem = fold_unary (BIT_NOT_EXPR, TREE_TYPE (type), elem);
8514 if (elem == NULL_TREE)
8516 elements = TREE_CHAIN (elements);
8519 elem = build_int_cst (TREE_TYPE (type), -1);
8520 list = tree_cons (NULL_TREE, elem, list);
8523 return build_vector (type, nreverse (list));
8528 case TRUTH_NOT_EXPR:
8529 /* The argument to invert_truthvalue must have Boolean type. */
8530 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8531 arg0 = fold_convert (boolean_type_node, arg0);
8533 /* Note that the operand of this must be an int
8534 and its values must be 0 or 1.
8535 ("true" is a fixed value perhaps depending on the language,
8536 but we don't handle values other than 1 correctly yet.) */
8537 tem = fold_truth_not_expr (arg0);
8540 return fold_convert (type, tem);
8543 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8544 return fold_convert (type, arg0);
8545 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8546 return omit_one_operand (type, TREE_OPERAND (arg0, 0),
8547 TREE_OPERAND (arg0, 1));
8548 if (TREE_CODE (arg0) == COMPLEX_CST)
8549 return fold_convert (type, TREE_REALPART (arg0));
8550 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8552 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8553 tem = fold_build2 (TREE_CODE (arg0), itype,
8554 fold_build1 (REALPART_EXPR, itype,
8555 TREE_OPERAND (arg0, 0)),
8556 fold_build1 (REALPART_EXPR, itype,
8557 TREE_OPERAND (arg0, 1)));
8558 return fold_convert (type, tem);
8560 if (TREE_CODE (arg0) == CONJ_EXPR)
8562 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8563 tem = fold_build1 (REALPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8564 return fold_convert (type, tem);
8566 if (TREE_CODE (arg0) == CALL_EXPR)
8568 tree fn = get_callee_fndecl (arg0);
8569 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8570 switch (DECL_FUNCTION_CODE (fn))
8572 CASE_FLT_FN (BUILT_IN_CEXPI):
8573 fn = mathfn_built_in (type, BUILT_IN_COS);
8575 return build_call_expr (fn, 1, CALL_EXPR_ARG (arg0, 0));
8585 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8586 return fold_convert (type, integer_zero_node);
8587 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8588 return omit_one_operand (type, TREE_OPERAND (arg0, 1),
8589 TREE_OPERAND (arg0, 0));
8590 if (TREE_CODE (arg0) == COMPLEX_CST)
8591 return fold_convert (type, TREE_IMAGPART (arg0));
8592 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8594 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8595 tem = fold_build2 (TREE_CODE (arg0), itype,
8596 fold_build1 (IMAGPART_EXPR, itype,
8597 TREE_OPERAND (arg0, 0)),
8598 fold_build1 (IMAGPART_EXPR, itype,
8599 TREE_OPERAND (arg0, 1)));
8600 return fold_convert (type, tem);
8602 if (TREE_CODE (arg0) == CONJ_EXPR)
8604 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8605 tem = fold_build1 (IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8606 return fold_convert (type, negate_expr (tem));
8608 if (TREE_CODE (arg0) == CALL_EXPR)
8610 tree fn = get_callee_fndecl (arg0);
8611 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8612 switch (DECL_FUNCTION_CODE (fn))
8614 CASE_FLT_FN (BUILT_IN_CEXPI):
8615 fn = mathfn_built_in (type, BUILT_IN_SIN);
8617 return build_call_expr (fn, 1, CALL_EXPR_ARG (arg0, 0));
8628 } /* switch (code) */
8631 /* Fold a binary expression of code CODE and type TYPE with operands
8632 OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8633 Return the folded expression if folding is successful. Otherwise,
8634 return NULL_TREE. */
8637 fold_minmax (enum tree_code code, tree type, tree op0, tree op1)
8639 enum tree_code compl_code;
8641 if (code == MIN_EXPR)
8642 compl_code = MAX_EXPR;
8643 else if (code == MAX_EXPR)
8644 compl_code = MIN_EXPR;
8648 /* MIN (MAX (a, b), b) == b. */
8649 if (TREE_CODE (op0) == compl_code
8650 && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8651 return omit_one_operand (type, op1, TREE_OPERAND (op0, 0));
8653 /* MIN (MAX (b, a), b) == b. */
8654 if (TREE_CODE (op0) == compl_code
8655 && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8656 && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8657 return omit_one_operand (type, op1, TREE_OPERAND (op0, 1));
8659 /* MIN (a, MAX (a, b)) == a. */
8660 if (TREE_CODE (op1) == compl_code
8661 && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8662 && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8663 return omit_one_operand (type, op0, TREE_OPERAND (op1, 1));
8665 /* MIN (a, MAX (b, a)) == a. */
8666 if (TREE_CODE (op1) == compl_code
8667 && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8668 && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8669 return omit_one_operand (type, op0, TREE_OPERAND (op1, 0));
8674 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8675 by changing CODE to reduce the magnitude of constants involved in
8676 ARG0 of the comparison.
8677 Returns a canonicalized comparison tree if a simplification was
8678 possible, otherwise returns NULL_TREE.
8679 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8680 valid if signed overflow is undefined. */
8683 maybe_canonicalize_comparison_1 (enum tree_code code, tree type,
8684 tree arg0, tree arg1,
8685 bool *strict_overflow_p)
8687 enum tree_code code0 = TREE_CODE (arg0);
8688 tree t, cst0 = NULL_TREE;
8692 /* Match A +- CST code arg1 and CST code arg1. We can change the
8693 first form only if overflow is undefined. */
8694 if (!((TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8695 /* In principle pointers also have undefined overflow behavior,
8696 but that causes problems elsewhere. */
8697 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8698 && (code0 == MINUS_EXPR
8699 || code0 == PLUS_EXPR)
8700 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8701 || code0 == INTEGER_CST))
8704 /* Identify the constant in arg0 and its sign. */
8705 if (code0 == INTEGER_CST)
8708 cst0 = TREE_OPERAND (arg0, 1);
8709 sgn0 = tree_int_cst_sgn (cst0);
8711 /* Overflowed constants and zero will cause problems. */
8712 if (integer_zerop (cst0)
8713 || TREE_OVERFLOW (cst0))
8716 /* See if we can reduce the magnitude of the constant in
8717 arg0 by changing the comparison code. */
8718 if (code0 == INTEGER_CST)
8720 /* CST <= arg1 -> CST-1 < arg1. */
8721 if (code == LE_EXPR && sgn0 == 1)
8723 /* -CST < arg1 -> -CST-1 <= arg1. */
8724 else if (code == LT_EXPR && sgn0 == -1)
8726 /* CST > arg1 -> CST-1 >= arg1. */
8727 else if (code == GT_EXPR && sgn0 == 1)
8729 /* -CST >= arg1 -> -CST-1 > arg1. */
8730 else if (code == GE_EXPR && sgn0 == -1)
8734 /* arg1 code' CST' might be more canonical. */
8739 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8741 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8743 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8744 else if (code == GT_EXPR
8745 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8747 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8748 else if (code == LE_EXPR
8749 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8751 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8752 else if (code == GE_EXPR
8753 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8757 *strict_overflow_p = true;
8760 /* Now build the constant reduced in magnitude. But not if that
8761 would produce one outside of its types range. */
8762 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8764 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8765 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8767 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8768 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8769 /* We cannot swap the comparison here as that would cause us to
8770 endlessly recurse. */
8773 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8774 cst0, build_int_cst (TREE_TYPE (cst0), 1), 0);
8775 if (code0 != INTEGER_CST)
8776 t = fold_build2 (code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8778 /* If swapping might yield to a more canonical form, do so. */
8780 return fold_build2 (swap_tree_comparison (code), type, arg1, t);
8782 return fold_build2 (code, type, t, arg1);
8785 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8786 overflow further. Try to decrease the magnitude of constants involved
8787 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8788 and put sole constants at the second argument position.
8789 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8792 maybe_canonicalize_comparison (enum tree_code code, tree type,
8793 tree arg0, tree arg1)
8796 bool strict_overflow_p;
8797 const char * const warnmsg = G_("assuming signed overflow does not occur "
8798 "when reducing constant in comparison");
8800 /* Try canonicalization by simplifying arg0. */
8801 strict_overflow_p = false;
8802 t = maybe_canonicalize_comparison_1 (code, type, arg0, arg1,
8803 &strict_overflow_p);
8806 if (strict_overflow_p)
8807 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8811 /* Try canonicalization by simplifying arg1 using the swapped
8813 code = swap_tree_comparison (code);
8814 strict_overflow_p = false;
8815 t = maybe_canonicalize_comparison_1 (code, type, arg1, arg0,
8816 &strict_overflow_p);
8817 if (t && strict_overflow_p)
8818 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8822 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8823 space. This is used to avoid issuing overflow warnings for
8824 expressions like &p->x which can not wrap. */
8827 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8829 unsigned HOST_WIDE_INT offset_low, total_low;
8830 HOST_WIDE_INT size, offset_high, total_high;
8832 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8838 if (offset == NULL_TREE)
8843 else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8847 offset_low = TREE_INT_CST_LOW (offset);
8848 offset_high = TREE_INT_CST_HIGH (offset);
8851 if (add_double_with_sign (offset_low, offset_high,
8852 bitpos / BITS_PER_UNIT, 0,
8853 &total_low, &total_high,
8857 if (total_high != 0)
8860 size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8864 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8866 if (TREE_CODE (base) == ADDR_EXPR)
8868 HOST_WIDE_INT base_size;
8870 base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8871 if (base_size > 0 && size < base_size)
8875 return total_low > (unsigned HOST_WIDE_INT) size;
8878 /* Subroutine of fold_binary. This routine performs all of the
8879 transformations that are common to the equality/inequality
8880 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8881 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8882 fold_binary should call fold_binary. Fold a comparison with
8883 tree code CODE and type TYPE with operands OP0 and OP1. Return
8884 the folded comparison or NULL_TREE. */
8887 fold_comparison (enum tree_code code, tree type, tree op0, tree op1)
8889 tree arg0, arg1, tem;
8894 STRIP_SIGN_NOPS (arg0);
8895 STRIP_SIGN_NOPS (arg1);
8897 tem = fold_relational_const (code, type, arg0, arg1);
8898 if (tem != NULL_TREE)
8901 /* If one arg is a real or integer constant, put it last. */
8902 if (tree_swap_operands_p (arg0, arg1, true))
8903 return fold_build2 (swap_tree_comparison (code), type, op1, op0);
8905 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1. */
8906 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8907 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8908 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8909 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
8910 && (TREE_CODE (arg1) == INTEGER_CST
8911 && !TREE_OVERFLOW (arg1)))
8913 tree const1 = TREE_OPERAND (arg0, 1);
8915 tree variable = TREE_OPERAND (arg0, 0);
8918 lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
8920 lhs = fold_build2 (lhs_add ? PLUS_EXPR : MINUS_EXPR,
8921 TREE_TYPE (arg1), const2, const1);
8923 /* If the constant operation overflowed this can be
8924 simplified as a comparison against INT_MAX/INT_MIN. */
8925 if (TREE_CODE (lhs) == INTEGER_CST
8926 && TREE_OVERFLOW (lhs))
8928 int const1_sgn = tree_int_cst_sgn (const1);
8929 enum tree_code code2 = code;
8931 /* Get the sign of the constant on the lhs if the
8932 operation were VARIABLE + CONST1. */
8933 if (TREE_CODE (arg0) == MINUS_EXPR)
8934 const1_sgn = -const1_sgn;
8936 /* The sign of the constant determines if we overflowed
8937 INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8938 Canonicalize to the INT_MIN overflow by swapping the comparison
8940 if (const1_sgn == -1)
8941 code2 = swap_tree_comparison (code);
8943 /* We now can look at the canonicalized case
8944 VARIABLE + 1 CODE2 INT_MIN
8945 and decide on the result. */
8946 if (code2 == LT_EXPR
8948 || code2 == EQ_EXPR)
8949 return omit_one_operand (type, boolean_false_node, variable);
8950 else if (code2 == NE_EXPR
8952 || code2 == GT_EXPR)
8953 return omit_one_operand (type, boolean_true_node, variable);
8956 if (TREE_CODE (lhs) == TREE_CODE (arg1)
8957 && (TREE_CODE (lhs) != INTEGER_CST
8958 || !TREE_OVERFLOW (lhs)))
8960 fold_overflow_warning (("assuming signed overflow does not occur "
8961 "when changing X +- C1 cmp C2 to "
8963 WARN_STRICT_OVERFLOW_COMPARISON);
8964 return fold_build2 (code, type, variable, lhs);
8968 /* For comparisons of pointers we can decompose it to a compile time
8969 comparison of the base objects and the offsets into the object.
8970 This requires at least one operand being an ADDR_EXPR or a
8971 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8972 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8973 && (TREE_CODE (arg0) == ADDR_EXPR
8974 || TREE_CODE (arg1) == ADDR_EXPR
8975 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8976 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8978 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8979 HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8980 enum machine_mode mode;
8981 int volatilep, unsignedp;
8982 bool indirect_base0 = false, indirect_base1 = false;
8984 /* Get base and offset for the access. Strip ADDR_EXPR for
8985 get_inner_reference, but put it back by stripping INDIRECT_REF
8986 off the base object if possible. indirect_baseN will be true
8987 if baseN is not an address but refers to the object itself. */
8989 if (TREE_CODE (arg0) == ADDR_EXPR)
8991 base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
8992 &bitsize, &bitpos0, &offset0, &mode,
8993 &unsignedp, &volatilep, false);
8994 if (TREE_CODE (base0) == INDIRECT_REF)
8995 base0 = TREE_OPERAND (base0, 0);
8997 indirect_base0 = true;
8999 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9001 base0 = TREE_OPERAND (arg0, 0);
9002 offset0 = TREE_OPERAND (arg0, 1);
9006 if (TREE_CODE (arg1) == ADDR_EXPR)
9008 base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
9009 &bitsize, &bitpos1, &offset1, &mode,
9010 &unsignedp, &volatilep, false);
9011 if (TREE_CODE (base1) == INDIRECT_REF)
9012 base1 = TREE_OPERAND (base1, 0);
9014 indirect_base1 = true;
9016 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
9018 base1 = TREE_OPERAND (arg1, 0);
9019 offset1 = TREE_OPERAND (arg1, 1);
9022 /* If we have equivalent bases we might be able to simplify. */
9023 if (indirect_base0 == indirect_base1
9024 && operand_equal_p (base0, base1, 0))
9026 /* We can fold this expression to a constant if the non-constant
9027 offset parts are equal. */
9028 if ((offset0 == offset1
9029 || (offset0 && offset1
9030 && operand_equal_p (offset0, offset1, 0)))
9033 || POINTER_TYPE_OVERFLOW_UNDEFINED))
9038 && bitpos0 != bitpos1
9039 && (pointer_may_wrap_p (base0, offset0, bitpos0)
9040 || pointer_may_wrap_p (base1, offset1, bitpos1)))
9041 fold_overflow_warning (("assuming pointer wraparound does not "
9042 "occur when comparing P +- C1 with "
9044 WARN_STRICT_OVERFLOW_CONDITIONAL);
9049 return constant_boolean_node (bitpos0 == bitpos1, type);
9051 return constant_boolean_node (bitpos0 != bitpos1, type);
9053 return constant_boolean_node (bitpos0 < bitpos1, type);
9055 return constant_boolean_node (bitpos0 <= bitpos1, type);
9057 return constant_boolean_node (bitpos0 >= bitpos1, type);
9059 return constant_boolean_node (bitpos0 > bitpos1, type);
9063 /* We can simplify the comparison to a comparison of the variable
9064 offset parts if the constant offset parts are equal.
9065 Be careful to use signed size type here because otherwise we
9066 mess with array offsets in the wrong way. This is possible
9067 because pointer arithmetic is restricted to retain within an
9068 object and overflow on pointer differences is undefined as of
9069 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
9070 else if (bitpos0 == bitpos1
9071 && ((code == EQ_EXPR || code == NE_EXPR)
9072 || POINTER_TYPE_OVERFLOW_UNDEFINED))
9074 tree signed_size_type_node;
9075 signed_size_type_node = signed_type_for (size_type_node);
9077 /* By converting to signed size type we cover middle-end pointer
9078 arithmetic which operates on unsigned pointer types of size
9079 type size and ARRAY_REF offsets which are properly sign or
9080 zero extended from their type in case it is narrower than
9082 if (offset0 == NULL_TREE)
9083 offset0 = build_int_cst (signed_size_type_node, 0);
9085 offset0 = fold_convert (signed_size_type_node, offset0);
9086 if (offset1 == NULL_TREE)
9087 offset1 = build_int_cst (signed_size_type_node, 0);
9089 offset1 = fold_convert (signed_size_type_node, offset1);
9093 && (pointer_may_wrap_p (base0, offset0, bitpos0)
9094 || pointer_may_wrap_p (base1, offset1, bitpos1)))
9095 fold_overflow_warning (("assuming pointer wraparound does not "
9096 "occur when comparing P +- C1 with "
9098 WARN_STRICT_OVERFLOW_COMPARISON);
9100 return fold_build2 (code, type, offset0, offset1);
9103 /* For non-equal bases we can simplify if they are addresses
9104 of local binding decls or constants. */
9105 else if (indirect_base0 && indirect_base1
9106 /* We know that !operand_equal_p (base0, base1, 0)
9107 because the if condition was false. But make
9108 sure two decls are not the same. */
9110 && TREE_CODE (arg0) == ADDR_EXPR
9111 && TREE_CODE (arg1) == ADDR_EXPR
9112 && (((TREE_CODE (base0) == VAR_DECL
9113 || TREE_CODE (base0) == PARM_DECL)
9114 && (targetm.binds_local_p (base0)
9115 || CONSTANT_CLASS_P (base1)))
9116 || CONSTANT_CLASS_P (base0))
9117 && (((TREE_CODE (base1) == VAR_DECL
9118 || TREE_CODE (base1) == PARM_DECL)
9119 && (targetm.binds_local_p (base1)
9120 || CONSTANT_CLASS_P (base0)))
9121 || CONSTANT_CLASS_P (base1)))
9123 if (code == EQ_EXPR)
9124 return omit_two_operands (type, boolean_false_node, arg0, arg1);
9125 else if (code == NE_EXPR)
9126 return omit_two_operands (type, boolean_true_node, arg0, arg1);
9128 /* For equal offsets we can simplify to a comparison of the
9130 else if (bitpos0 == bitpos1
9132 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
9134 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
9135 && ((offset0 == offset1)
9136 || (offset0 && offset1
9137 && operand_equal_p (offset0, offset1, 0))))
9140 base0 = fold_addr_expr (base0);
9142 base1 = fold_addr_expr (base1);
9143 return fold_build2 (code, type, base0, base1);
9147 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
9148 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
9149 the resulting offset is smaller in absolute value than the
9151 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9152 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
9153 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9154 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9155 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
9156 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9157 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
9159 tree const1 = TREE_OPERAND (arg0, 1);
9160 tree const2 = TREE_OPERAND (arg1, 1);
9161 tree variable1 = TREE_OPERAND (arg0, 0);
9162 tree variable2 = TREE_OPERAND (arg1, 0);
9164 const char * const warnmsg = G_("assuming signed overflow does not "
9165 "occur when combining constants around "
9168 /* Put the constant on the side where it doesn't overflow and is
9169 of lower absolute value than before. */
9170 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9171 ? MINUS_EXPR : PLUS_EXPR,
9173 if (!TREE_OVERFLOW (cst)
9174 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
9176 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9177 return fold_build2 (code, type,
9179 fold_build2 (TREE_CODE (arg1), TREE_TYPE (arg1),
9183 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9184 ? MINUS_EXPR : PLUS_EXPR,
9186 if (!TREE_OVERFLOW (cst)
9187 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
9189 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9190 return fold_build2 (code, type,
9191 fold_build2 (TREE_CODE (arg0), TREE_TYPE (arg0),
9197 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
9198 signed arithmetic case. That form is created by the compiler
9199 often enough for folding it to be of value. One example is in
9200 computing loop trip counts after Operator Strength Reduction. */
9201 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9202 && TREE_CODE (arg0) == MULT_EXPR
9203 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9204 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9205 && integer_zerop (arg1))
9207 tree const1 = TREE_OPERAND (arg0, 1);
9208 tree const2 = arg1; /* zero */
9209 tree variable1 = TREE_OPERAND (arg0, 0);
9210 enum tree_code cmp_code = code;
9212 gcc_assert (!integer_zerop (const1));
9214 fold_overflow_warning (("assuming signed overflow does not occur when "
9215 "eliminating multiplication in comparison "
9217 WARN_STRICT_OVERFLOW_COMPARISON);
9219 /* If const1 is negative we swap the sense of the comparison. */
9220 if (tree_int_cst_sgn (const1) < 0)
9221 cmp_code = swap_tree_comparison (cmp_code);
9223 return fold_build2 (cmp_code, type, variable1, const2);
9226 tem = maybe_canonicalize_comparison (code, type, op0, op1);
9230 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
9232 tree targ0 = strip_float_extensions (arg0);
9233 tree targ1 = strip_float_extensions (arg1);
9234 tree newtype = TREE_TYPE (targ0);
9236 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
9237 newtype = TREE_TYPE (targ1);
9239 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
9240 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
9241 return fold_build2 (code, type, fold_convert (newtype, targ0),
9242 fold_convert (newtype, targ1));
9244 /* (-a) CMP (-b) -> b CMP a */
9245 if (TREE_CODE (arg0) == NEGATE_EXPR
9246 && TREE_CODE (arg1) == NEGATE_EXPR)
9247 return fold_build2 (code, type, TREE_OPERAND (arg1, 0),
9248 TREE_OPERAND (arg0, 0));
9250 if (TREE_CODE (arg1) == REAL_CST)
9252 REAL_VALUE_TYPE cst;
9253 cst = TREE_REAL_CST (arg1);
9255 /* (-a) CMP CST -> a swap(CMP) (-CST) */
9256 if (TREE_CODE (arg0) == NEGATE_EXPR)
9257 return fold_build2 (swap_tree_comparison (code), type,
9258 TREE_OPERAND (arg0, 0),
9259 build_real (TREE_TYPE (arg1),
9260 REAL_VALUE_NEGATE (cst)));
9262 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
9263 /* a CMP (-0) -> a CMP 0 */
9264 if (REAL_VALUE_MINUS_ZERO (cst))
9265 return fold_build2 (code, type, arg0,
9266 build_real (TREE_TYPE (arg1), dconst0));
9268 /* x != NaN is always true, other ops are always false. */
9269 if (REAL_VALUE_ISNAN (cst)
9270 && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
9272 tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
9273 return omit_one_operand (type, tem, arg0);
9276 /* Fold comparisons against infinity. */
9277 if (REAL_VALUE_ISINF (cst))
9279 tem = fold_inf_compare (code, type, arg0, arg1);
9280 if (tem != NULL_TREE)
9285 /* If this is a comparison of a real constant with a PLUS_EXPR
9286 or a MINUS_EXPR of a real constant, we can convert it into a
9287 comparison with a revised real constant as long as no overflow
9288 occurs when unsafe_math_optimizations are enabled. */
9289 if (flag_unsafe_math_optimizations
9290 && TREE_CODE (arg1) == REAL_CST
9291 && (TREE_CODE (arg0) == PLUS_EXPR
9292 || TREE_CODE (arg0) == MINUS_EXPR)
9293 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
9294 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
9295 ? MINUS_EXPR : PLUS_EXPR,
9296 arg1, TREE_OPERAND (arg0, 1), 0))
9297 && !TREE_OVERFLOW (tem))
9298 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
9300 /* Likewise, we can simplify a comparison of a real constant with
9301 a MINUS_EXPR whose first operand is also a real constant, i.e.
9302 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
9303 floating-point types only if -fassociative-math is set. */
9304 if (flag_associative_math
9305 && TREE_CODE (arg1) == REAL_CST
9306 && TREE_CODE (arg0) == MINUS_EXPR
9307 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
9308 && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
9310 && !TREE_OVERFLOW (tem))
9311 return fold_build2 (swap_tree_comparison (code), type,
9312 TREE_OPERAND (arg0, 1), tem);
9314 /* Fold comparisons against built-in math functions. */
9315 if (TREE_CODE (arg1) == REAL_CST
9316 && flag_unsafe_math_optimizations
9317 && ! flag_errno_math)
9319 enum built_in_function fcode = builtin_mathfn_code (arg0);
9321 if (fcode != END_BUILTINS)
9323 tem = fold_mathfn_compare (fcode, code, type, arg0, arg1);
9324 if (tem != NULL_TREE)
9330 if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
9331 && CONVERT_EXPR_P (arg0))
9333 /* If we are widening one operand of an integer comparison,
9334 see if the other operand is similarly being widened. Perhaps we
9335 can do the comparison in the narrower type. */
9336 tem = fold_widened_comparison (code, type, arg0, arg1);
9340 /* Or if we are changing signedness. */
9341 tem = fold_sign_changed_comparison (code, type, arg0, arg1);
9346 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
9347 constant, we can simplify it. */
9348 if (TREE_CODE (arg1) == INTEGER_CST
9349 && (TREE_CODE (arg0) == MIN_EXPR
9350 || TREE_CODE (arg0) == MAX_EXPR)
9351 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9353 tem = optimize_minmax_comparison (code, type, op0, op1);
9358 /* Simplify comparison of something with itself. (For IEEE
9359 floating-point, we can only do some of these simplifications.) */
9360 if (operand_equal_p (arg0, arg1, 0))
9365 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9366 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9367 return constant_boolean_node (1, type);
9372 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9373 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9374 return constant_boolean_node (1, type);
9375 return fold_build2 (EQ_EXPR, type, arg0, arg1);
9378 /* For NE, we can only do this simplification if integer
9379 or we don't honor IEEE floating point NaNs. */
9380 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9381 && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9383 /* ... fall through ... */
9386 return constant_boolean_node (0, type);
9392 /* If we are comparing an expression that just has comparisons
9393 of two integer values, arithmetic expressions of those comparisons,
9394 and constants, we can simplify it. There are only three cases
9395 to check: the two values can either be equal, the first can be
9396 greater, or the second can be greater. Fold the expression for
9397 those three values. Since each value must be 0 or 1, we have
9398 eight possibilities, each of which corresponds to the constant 0
9399 or 1 or one of the six possible comparisons.
9401 This handles common cases like (a > b) == 0 but also handles
9402 expressions like ((x > y) - (y > x)) > 0, which supposedly
9403 occur in macroized code. */
9405 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9407 tree cval1 = 0, cval2 = 0;
9410 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9411 /* Don't handle degenerate cases here; they should already
9412 have been handled anyway. */
9413 && cval1 != 0 && cval2 != 0
9414 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9415 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9416 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9417 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9418 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9419 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9420 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9422 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9423 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9425 /* We can't just pass T to eval_subst in case cval1 or cval2
9426 was the same as ARG1. */
9429 = fold_build2 (code, type,
9430 eval_subst (arg0, cval1, maxval,
9434 = fold_build2 (code, type,
9435 eval_subst (arg0, cval1, maxval,
9439 = fold_build2 (code, type,
9440 eval_subst (arg0, cval1, minval,
9444 /* All three of these results should be 0 or 1. Confirm they are.
9445 Then use those values to select the proper code to use. */
9447 if (TREE_CODE (high_result) == INTEGER_CST
9448 && TREE_CODE (equal_result) == INTEGER_CST
9449 && TREE_CODE (low_result) == INTEGER_CST)
9451 /* Make a 3-bit mask with the high-order bit being the
9452 value for `>', the next for '=', and the low for '<'. */
9453 switch ((integer_onep (high_result) * 4)
9454 + (integer_onep (equal_result) * 2)
9455 + integer_onep (low_result))
9459 return omit_one_operand (type, integer_zero_node, arg0);
9480 return omit_one_operand (type, integer_one_node, arg0);
9484 return save_expr (build2 (code, type, cval1, cval2));
9485 return fold_build2 (code, type, cval1, cval2);
9490 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9491 into a single range test. */
9492 if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9493 || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9494 && TREE_CODE (arg1) == INTEGER_CST
9495 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9496 && !integer_zerop (TREE_OPERAND (arg0, 1))
9497 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9498 && !TREE_OVERFLOW (arg1))
9500 tem = fold_div_compare (code, type, arg0, arg1);
9501 if (tem != NULL_TREE)
9505 /* Fold ~X op ~Y as Y op X. */
9506 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9507 && TREE_CODE (arg1) == BIT_NOT_EXPR)
9509 tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9510 return fold_build2 (code, type,
9511 fold_convert (cmp_type, TREE_OPERAND (arg1, 0)),
9512 TREE_OPERAND (arg0, 0));
9515 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
9516 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9517 && TREE_CODE (arg1) == INTEGER_CST)
9519 tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9520 return fold_build2 (swap_tree_comparison (code), type,
9521 TREE_OPERAND (arg0, 0),
9522 fold_build1 (BIT_NOT_EXPR, cmp_type,
9523 fold_convert (cmp_type, arg1)));
9530 /* Subroutine of fold_binary. Optimize complex multiplications of the
9531 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
9532 argument EXPR represents the expression "z" of type TYPE. */
9535 fold_mult_zconjz (tree type, tree expr)
9537 tree itype = TREE_TYPE (type);
9538 tree rpart, ipart, tem;
9540 if (TREE_CODE (expr) == COMPLEX_EXPR)
9542 rpart = TREE_OPERAND (expr, 0);
9543 ipart = TREE_OPERAND (expr, 1);
9545 else if (TREE_CODE (expr) == COMPLEX_CST)
9547 rpart = TREE_REALPART (expr);
9548 ipart = TREE_IMAGPART (expr);
9552 expr = save_expr (expr);
9553 rpart = fold_build1 (REALPART_EXPR, itype, expr);
9554 ipart = fold_build1 (IMAGPART_EXPR, itype, expr);
9557 rpart = save_expr (rpart);
9558 ipart = save_expr (ipart);
9559 tem = fold_build2 (PLUS_EXPR, itype,
9560 fold_build2 (MULT_EXPR, itype, rpart, rpart),
9561 fold_build2 (MULT_EXPR, itype, ipart, ipart));
9562 return fold_build2 (COMPLEX_EXPR, type, tem,
9563 fold_convert (itype, integer_zero_node));
9567 /* Subroutine of fold_binary. If P is the value of EXPR, computes
9568 power-of-two M and (arbitrary) N such that M divides (P-N). This condition
9569 guarantees that P and N have the same least significant log2(M) bits.
9570 N is not otherwise constrained. In particular, N is not normalized to
9571 0 <= N < M as is common. In general, the precise value of P is unknown.
9572 M is chosen as large as possible such that constant N can be determined.
9574 Returns M and sets *RESIDUE to N. */
9576 static unsigned HOST_WIDE_INT
9577 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue)
9579 enum tree_code code;
9583 code = TREE_CODE (expr);
9584 if (code == ADDR_EXPR)
9586 expr = TREE_OPERAND (expr, 0);
9587 if (handled_component_p (expr))
9589 HOST_WIDE_INT bitsize, bitpos;
9591 enum machine_mode mode;
9592 int unsignedp, volatilep;
9594 expr = get_inner_reference (expr, &bitsize, &bitpos, &offset,
9595 &mode, &unsignedp, &volatilep, false);
9596 *residue = bitpos / BITS_PER_UNIT;
9599 if (TREE_CODE (offset) == INTEGER_CST)
9600 *residue += TREE_INT_CST_LOW (offset);
9602 /* We don't handle more complicated offset expressions. */
9607 if (DECL_P (expr) && TREE_CODE (expr) != FUNCTION_DECL)
9608 return DECL_ALIGN_UNIT (expr);
9610 else if (code == POINTER_PLUS_EXPR)
9613 unsigned HOST_WIDE_INT modulus;
9614 enum tree_code inner_code;
9616 op0 = TREE_OPERAND (expr, 0);
9618 modulus = get_pointer_modulus_and_residue (op0, residue);
9620 op1 = TREE_OPERAND (expr, 1);
9622 inner_code = TREE_CODE (op1);
9623 if (inner_code == INTEGER_CST)
9625 *residue += TREE_INT_CST_LOW (op1);
9628 else if (inner_code == MULT_EXPR)
9630 op1 = TREE_OPERAND (op1, 1);
9631 if (TREE_CODE (op1) == INTEGER_CST)
9633 unsigned HOST_WIDE_INT align;
9635 /* Compute the greatest power-of-2 divisor of op1. */
9636 align = TREE_INT_CST_LOW (op1);
9639 /* If align is non-zero and less than *modulus, replace
9640 *modulus with align., If align is 0, then either op1 is 0
9641 or the greatest power-of-2 divisor of op1 doesn't fit in an
9642 unsigned HOST_WIDE_INT. In either case, no additional
9643 constraint is imposed. */
9645 modulus = MIN (modulus, align);
9652 /* If we get here, we were unable to determine anything useful about the
9658 /* Fold a binary expression of code CODE and type TYPE with operands
9659 OP0 and OP1. Return the folded expression if folding is
9660 successful. Otherwise, return NULL_TREE. */
9663 fold_binary (enum tree_code code, tree type, tree op0, tree op1)
9665 enum tree_code_class kind = TREE_CODE_CLASS (code);
9666 tree arg0, arg1, tem;
9667 tree t1 = NULL_TREE;
9668 bool strict_overflow_p;
9670 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9671 && TREE_CODE_LENGTH (code) == 2
9673 && op1 != NULL_TREE);
9678 /* Strip any conversions that don't change the mode. This is
9679 safe for every expression, except for a comparison expression
9680 because its signedness is derived from its operands. So, in
9681 the latter case, only strip conversions that don't change the
9682 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9685 Note that this is done as an internal manipulation within the
9686 constant folder, in order to find the simplest representation
9687 of the arguments so that their form can be studied. In any
9688 cases, the appropriate type conversions should be put back in
9689 the tree that will get out of the constant folder. */
9691 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9693 STRIP_SIGN_NOPS (arg0);
9694 STRIP_SIGN_NOPS (arg1);
9702 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9703 constant but we can't do arithmetic on them. */
9704 if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9705 || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
9706 || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
9707 || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
9708 || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
9709 || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
9711 if (kind == tcc_binary)
9713 /* Make sure type and arg0 have the same saturating flag. */
9714 gcc_assert (TYPE_SATURATING (type)
9715 == TYPE_SATURATING (TREE_TYPE (arg0)));
9716 tem = const_binop (code, arg0, arg1, 0);
9718 else if (kind == tcc_comparison)
9719 tem = fold_relational_const (code, type, arg0, arg1);
9723 if (tem != NULL_TREE)
9725 if (TREE_TYPE (tem) != type)
9726 tem = fold_convert (type, tem);
9731 /* If this is a commutative operation, and ARG0 is a constant, move it
9732 to ARG1 to reduce the number of tests below. */
9733 if (commutative_tree_code (code)
9734 && tree_swap_operands_p (arg0, arg1, true))
9735 return fold_build2 (code, type, op1, op0);
9737 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9739 First check for cases where an arithmetic operation is applied to a
9740 compound, conditional, or comparison operation. Push the arithmetic
9741 operation inside the compound or conditional to see if any folding
9742 can then be done. Convert comparison to conditional for this purpose.
9743 The also optimizes non-constant cases that used to be done in
9746 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9747 one of the operands is a comparison and the other is a comparison, a
9748 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9749 code below would make the expression more complex. Change it to a
9750 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9751 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9753 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9754 || code == EQ_EXPR || code == NE_EXPR)
9755 && ((truth_value_p (TREE_CODE (arg0))
9756 && (truth_value_p (TREE_CODE (arg1))
9757 || (TREE_CODE (arg1) == BIT_AND_EXPR
9758 && integer_onep (TREE_OPERAND (arg1, 1)))))
9759 || (truth_value_p (TREE_CODE (arg1))
9760 && (truth_value_p (TREE_CODE (arg0))
9761 || (TREE_CODE (arg0) == BIT_AND_EXPR
9762 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9764 tem = fold_build2 (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9765 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9768 fold_convert (boolean_type_node, arg0),
9769 fold_convert (boolean_type_node, arg1));
9771 if (code == EQ_EXPR)
9772 tem = invert_truthvalue (tem);
9774 return fold_convert (type, tem);
9777 if (TREE_CODE_CLASS (code) == tcc_binary
9778 || TREE_CODE_CLASS (code) == tcc_comparison)
9780 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9781 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9782 fold_build2 (code, type,
9783 fold_convert (TREE_TYPE (op0),
9784 TREE_OPERAND (arg0, 1)),
9786 if (TREE_CODE (arg1) == COMPOUND_EXPR
9787 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9788 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9789 fold_build2 (code, type, op0,
9790 fold_convert (TREE_TYPE (op1),
9791 TREE_OPERAND (arg1, 1))));
9793 if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
9795 tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
9797 /*cond_first_p=*/1);
9798 if (tem != NULL_TREE)
9802 if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
9804 tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
9806 /*cond_first_p=*/0);
9807 if (tem != NULL_TREE)
9814 case POINTER_PLUS_EXPR:
9815 /* 0 +p index -> (type)index */
9816 if (integer_zerop (arg0))
9817 return non_lvalue (fold_convert (type, arg1));
9819 /* PTR +p 0 -> PTR */
9820 if (integer_zerop (arg1))
9821 return non_lvalue (fold_convert (type, arg0));
9823 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9824 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9825 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9826 return fold_convert (type, fold_build2 (PLUS_EXPR, sizetype,
9827 fold_convert (sizetype, arg1),
9828 fold_convert (sizetype, arg0)));
9830 /* index +p PTR -> PTR +p index */
9831 if (POINTER_TYPE_P (TREE_TYPE (arg1))
9832 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9833 return fold_build2 (POINTER_PLUS_EXPR, type,
9834 fold_convert (type, arg1),
9835 fold_convert (sizetype, arg0));
9837 /* (PTR +p B) +p A -> PTR +p (B + A) */
9838 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9841 tree arg01 = fold_convert (sizetype, TREE_OPERAND (arg0, 1));
9842 tree arg00 = TREE_OPERAND (arg0, 0);
9843 inner = fold_build2 (PLUS_EXPR, sizetype,
9844 arg01, fold_convert (sizetype, arg1));
9845 return fold_convert (type,
9846 fold_build2 (POINTER_PLUS_EXPR,
9847 TREE_TYPE (arg00), arg00, inner));
9850 /* PTR_CST +p CST -> CST1 */
9851 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9852 return fold_build2 (PLUS_EXPR, type, arg0, fold_convert (type, arg1));
9854 /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
9855 of the array. Loop optimizer sometimes produce this type of
9857 if (TREE_CODE (arg0) == ADDR_EXPR)
9859 tem = try_move_mult_to_index (arg0, fold_convert (sizetype, arg1));
9861 return fold_convert (type, tem);
9867 /* PTR + INT -> (INT)(PTR p+ INT) */
9868 if (POINTER_TYPE_P (TREE_TYPE (arg0))
9869 && INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
9870 return fold_convert (type, fold_build2 (POINTER_PLUS_EXPR,
9873 fold_convert (sizetype, arg1)));
9874 /* INT + PTR -> (INT)(PTR p+ INT) */
9875 if (POINTER_TYPE_P (TREE_TYPE (arg1))
9876 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9877 return fold_convert (type, fold_build2 (POINTER_PLUS_EXPR,
9880 fold_convert (sizetype, arg0)));
9881 /* A + (-B) -> A - B */
9882 if (TREE_CODE (arg1) == NEGATE_EXPR)
9883 return fold_build2 (MINUS_EXPR, type,
9884 fold_convert (type, arg0),
9885 fold_convert (type, TREE_OPERAND (arg1, 0)));
9886 /* (-A) + B -> B - A */
9887 if (TREE_CODE (arg0) == NEGATE_EXPR
9888 && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
9889 return fold_build2 (MINUS_EXPR, type,
9890 fold_convert (type, arg1),
9891 fold_convert (type, TREE_OPERAND (arg0, 0)));
9893 if (INTEGRAL_TYPE_P (type))
9895 /* Convert ~A + 1 to -A. */
9896 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9897 && integer_onep (arg1))
9898 return fold_build1 (NEGATE_EXPR, type,
9899 fold_convert (type, TREE_OPERAND (arg0, 0)));
9902 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9903 && !TYPE_OVERFLOW_TRAPS (type))
9905 tree tem = TREE_OPERAND (arg0, 0);
9908 if (operand_equal_p (tem, arg1, 0))
9910 t1 = build_int_cst_type (type, -1);
9911 return omit_one_operand (type, t1, arg1);
9916 if (TREE_CODE (arg1) == BIT_NOT_EXPR
9917 && !TYPE_OVERFLOW_TRAPS (type))
9919 tree tem = TREE_OPERAND (arg1, 0);
9922 if (operand_equal_p (arg0, tem, 0))
9924 t1 = build_int_cst_type (type, -1);
9925 return omit_one_operand (type, t1, arg0);
9929 /* X + (X / CST) * -CST is X % CST. */
9930 if (TREE_CODE (arg1) == MULT_EXPR
9931 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9932 && operand_equal_p (arg0,
9933 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9935 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9936 tree cst1 = TREE_OPERAND (arg1, 1);
9937 tree sum = fold_binary (PLUS_EXPR, TREE_TYPE (cst1), cst1, cst0);
9938 if (sum && integer_zerop (sum))
9939 return fold_convert (type,
9940 fold_build2 (TRUNC_MOD_EXPR,
9941 TREE_TYPE (arg0), arg0, cst0));
9945 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
9946 same or one. Make sure type is not saturating.
9947 fold_plusminus_mult_expr will re-associate. */
9948 if ((TREE_CODE (arg0) == MULT_EXPR
9949 || TREE_CODE (arg1) == MULT_EXPR)
9950 && !TYPE_SATURATING (type)
9951 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9953 tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
9958 if (! FLOAT_TYPE_P (type))
9960 if (integer_zerop (arg1))
9961 return non_lvalue (fold_convert (type, arg0));
9963 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
9964 with a constant, and the two constants have no bits in common,
9965 we should treat this as a BIT_IOR_EXPR since this may produce more
9967 if (TREE_CODE (arg0) == BIT_AND_EXPR
9968 && TREE_CODE (arg1) == BIT_AND_EXPR
9969 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9970 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9971 && integer_zerop (const_binop (BIT_AND_EXPR,
9972 TREE_OPERAND (arg0, 1),
9973 TREE_OPERAND (arg1, 1), 0)))
9975 code = BIT_IOR_EXPR;
9979 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9980 (plus (plus (mult) (mult)) (foo)) so that we can
9981 take advantage of the factoring cases below. */
9982 if (((TREE_CODE (arg0) == PLUS_EXPR
9983 || TREE_CODE (arg0) == MINUS_EXPR)
9984 && TREE_CODE (arg1) == MULT_EXPR)
9985 || ((TREE_CODE (arg1) == PLUS_EXPR
9986 || TREE_CODE (arg1) == MINUS_EXPR)
9987 && TREE_CODE (arg0) == MULT_EXPR))
9989 tree parg0, parg1, parg, marg;
9990 enum tree_code pcode;
9992 if (TREE_CODE (arg1) == MULT_EXPR)
9993 parg = arg0, marg = arg1;
9995 parg = arg1, marg = arg0;
9996 pcode = TREE_CODE (parg);
9997 parg0 = TREE_OPERAND (parg, 0);
9998 parg1 = TREE_OPERAND (parg, 1);
10000 STRIP_NOPS (parg1);
10002 if (TREE_CODE (parg0) == MULT_EXPR
10003 && TREE_CODE (parg1) != MULT_EXPR)
10004 return fold_build2 (pcode, type,
10005 fold_build2 (PLUS_EXPR, type,
10006 fold_convert (type, parg0),
10007 fold_convert (type, marg)),
10008 fold_convert (type, parg1));
10009 if (TREE_CODE (parg0) != MULT_EXPR
10010 && TREE_CODE (parg1) == MULT_EXPR)
10011 return fold_build2 (PLUS_EXPR, type,
10012 fold_convert (type, parg0),
10013 fold_build2 (pcode, type,
10014 fold_convert (type, marg),
10015 fold_convert (type,
10021 /* See if ARG1 is zero and X + ARG1 reduces to X. */
10022 if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
10023 return non_lvalue (fold_convert (type, arg0));
10025 /* Likewise if the operands are reversed. */
10026 if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10027 return non_lvalue (fold_convert (type, arg1));
10029 /* Convert X + -C into X - C. */
10030 if (TREE_CODE (arg1) == REAL_CST
10031 && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
10033 tem = fold_negate_const (arg1, type);
10034 if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
10035 return fold_build2 (MINUS_EXPR, type,
10036 fold_convert (type, arg0),
10037 fold_convert (type, tem));
10040 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
10041 to __complex__ ( x, y ). This is not the same for SNaNs or
10042 if signed zeros are involved. */
10043 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10044 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10045 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10047 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10048 tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
10049 tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
10050 bool arg0rz = false, arg0iz = false;
10051 if ((arg0r && (arg0rz = real_zerop (arg0r)))
10052 || (arg0i && (arg0iz = real_zerop (arg0i))))
10054 tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
10055 tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
10056 if (arg0rz && arg1i && real_zerop (arg1i))
10058 tree rp = arg1r ? arg1r
10059 : build1 (REALPART_EXPR, rtype, arg1);
10060 tree ip = arg0i ? arg0i
10061 : build1 (IMAGPART_EXPR, rtype, arg0);
10062 return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10064 else if (arg0iz && arg1r && real_zerop (arg1r))
10066 tree rp = arg0r ? arg0r
10067 : build1 (REALPART_EXPR, rtype, arg0);
10068 tree ip = arg1i ? arg1i
10069 : build1 (IMAGPART_EXPR, rtype, arg1);
10070 return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10075 if (flag_unsafe_math_optimizations
10076 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10077 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10078 && (tem = distribute_real_division (code, type, arg0, arg1)))
10081 /* Convert x+x into x*2.0. */
10082 if (operand_equal_p (arg0, arg1, 0)
10083 && SCALAR_FLOAT_TYPE_P (type))
10084 return fold_build2 (MULT_EXPR, type, arg0,
10085 build_real (type, dconst2));
10087 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
10088 We associate floats only if the user has specified
10089 -fassociative-math. */
10090 if (flag_associative_math
10091 && TREE_CODE (arg1) == PLUS_EXPR
10092 && TREE_CODE (arg0) != MULT_EXPR)
10094 tree tree10 = TREE_OPERAND (arg1, 0);
10095 tree tree11 = TREE_OPERAND (arg1, 1);
10096 if (TREE_CODE (tree11) == MULT_EXPR
10097 && TREE_CODE (tree10) == MULT_EXPR)
10100 tree0 = fold_build2 (PLUS_EXPR, type, arg0, tree10);
10101 return fold_build2 (PLUS_EXPR, type, tree0, tree11);
10104 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
10105 We associate floats only if the user has specified
10106 -fassociative-math. */
10107 if (flag_associative_math
10108 && TREE_CODE (arg0) == PLUS_EXPR
10109 && TREE_CODE (arg1) != MULT_EXPR)
10111 tree tree00 = TREE_OPERAND (arg0, 0);
10112 tree tree01 = TREE_OPERAND (arg0, 1);
10113 if (TREE_CODE (tree01) == MULT_EXPR
10114 && TREE_CODE (tree00) == MULT_EXPR)
10117 tree0 = fold_build2 (PLUS_EXPR, type, tree01, arg1);
10118 return fold_build2 (PLUS_EXPR, type, tree00, tree0);
10124 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
10125 is a rotate of A by C1 bits. */
10126 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
10127 is a rotate of A by B bits. */
10129 enum tree_code code0, code1;
10131 code0 = TREE_CODE (arg0);
10132 code1 = TREE_CODE (arg1);
10133 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
10134 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
10135 && operand_equal_p (TREE_OPERAND (arg0, 0),
10136 TREE_OPERAND (arg1, 0), 0)
10137 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
10138 TYPE_UNSIGNED (rtype))
10139 /* Only create rotates in complete modes. Other cases are not
10140 expanded properly. */
10141 && TYPE_PRECISION (rtype) == GET_MODE_PRECISION (TYPE_MODE (rtype)))
10143 tree tree01, tree11;
10144 enum tree_code code01, code11;
10146 tree01 = TREE_OPERAND (arg0, 1);
10147 tree11 = TREE_OPERAND (arg1, 1);
10148 STRIP_NOPS (tree01);
10149 STRIP_NOPS (tree11);
10150 code01 = TREE_CODE (tree01);
10151 code11 = TREE_CODE (tree11);
10152 if (code01 == INTEGER_CST
10153 && code11 == INTEGER_CST
10154 && TREE_INT_CST_HIGH (tree01) == 0
10155 && TREE_INT_CST_HIGH (tree11) == 0
10156 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
10157 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
10158 return build2 (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
10159 code0 == LSHIFT_EXPR ? tree01 : tree11);
10160 else if (code11 == MINUS_EXPR)
10162 tree tree110, tree111;
10163 tree110 = TREE_OPERAND (tree11, 0);
10164 tree111 = TREE_OPERAND (tree11, 1);
10165 STRIP_NOPS (tree110);
10166 STRIP_NOPS (tree111);
10167 if (TREE_CODE (tree110) == INTEGER_CST
10168 && 0 == compare_tree_int (tree110,
10170 (TREE_TYPE (TREE_OPERAND
10172 && operand_equal_p (tree01, tree111, 0))
10173 return build2 ((code0 == LSHIFT_EXPR
10176 type, TREE_OPERAND (arg0, 0), tree01);
10178 else if (code01 == MINUS_EXPR)
10180 tree tree010, tree011;
10181 tree010 = TREE_OPERAND (tree01, 0);
10182 tree011 = TREE_OPERAND (tree01, 1);
10183 STRIP_NOPS (tree010);
10184 STRIP_NOPS (tree011);
10185 if (TREE_CODE (tree010) == INTEGER_CST
10186 && 0 == compare_tree_int (tree010,
10188 (TREE_TYPE (TREE_OPERAND
10190 && operand_equal_p (tree11, tree011, 0))
10191 return build2 ((code0 != LSHIFT_EXPR
10194 type, TREE_OPERAND (arg0, 0), tree11);
10200 /* In most languages, can't associate operations on floats through
10201 parentheses. Rather than remember where the parentheses were, we
10202 don't associate floats at all, unless the user has specified
10203 -fassociative-math.
10204 And, we need to make sure type is not saturating. */
10206 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
10207 && !TYPE_SATURATING (type))
10209 tree var0, con0, lit0, minus_lit0;
10210 tree var1, con1, lit1, minus_lit1;
10213 /* Split both trees into variables, constants, and literals. Then
10214 associate each group together, the constants with literals,
10215 then the result with variables. This increases the chances of
10216 literals being recombined later and of generating relocatable
10217 expressions for the sum of a constant and literal. */
10218 var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
10219 var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
10220 code == MINUS_EXPR);
10222 /* With undefined overflow we can only associate constants
10223 with one variable. */
10224 if (((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
10225 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
10231 if (TREE_CODE (tmp0) == NEGATE_EXPR)
10232 tmp0 = TREE_OPERAND (tmp0, 0);
10233 if (TREE_CODE (tmp1) == NEGATE_EXPR)
10234 tmp1 = TREE_OPERAND (tmp1, 0);
10235 /* The only case we can still associate with two variables
10236 is if they are the same, modulo negation. */
10237 if (!operand_equal_p (tmp0, tmp1, 0))
10241 /* Only do something if we found more than two objects. Otherwise,
10242 nothing has changed and we risk infinite recursion. */
10244 && (2 < ((var0 != 0) + (var1 != 0)
10245 + (con0 != 0) + (con1 != 0)
10246 + (lit0 != 0) + (lit1 != 0)
10247 + (minus_lit0 != 0) + (minus_lit1 != 0))))
10249 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
10250 if (code == MINUS_EXPR)
10253 var0 = associate_trees (var0, var1, code, type);
10254 con0 = associate_trees (con0, con1, code, type);
10255 lit0 = associate_trees (lit0, lit1, code, type);
10256 minus_lit0 = associate_trees (minus_lit0, minus_lit1, code, type);
10258 /* Preserve the MINUS_EXPR if the negative part of the literal is
10259 greater than the positive part. Otherwise, the multiplicative
10260 folding code (i.e extract_muldiv) may be fooled in case
10261 unsigned constants are subtracted, like in the following
10262 example: ((X*2 + 4) - 8U)/2. */
10263 if (minus_lit0 && lit0)
10265 if (TREE_CODE (lit0) == INTEGER_CST
10266 && TREE_CODE (minus_lit0) == INTEGER_CST
10267 && tree_int_cst_lt (lit0, minus_lit0))
10269 minus_lit0 = associate_trees (minus_lit0, lit0,
10275 lit0 = associate_trees (lit0, minus_lit0,
10283 return fold_convert (type,
10284 associate_trees (var0, minus_lit0,
10285 MINUS_EXPR, type));
10288 con0 = associate_trees (con0, minus_lit0,
10290 return fold_convert (type,
10291 associate_trees (var0, con0,
10296 con0 = associate_trees (con0, lit0, code, type);
10297 return fold_convert (type, associate_trees (var0, con0,
10305 /* Pointer simplifications for subtraction, simple reassociations. */
10306 if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
10308 /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
10309 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
10310 && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10312 tree arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
10313 tree arg01 = fold_convert (type, TREE_OPERAND (arg0, 1));
10314 tree arg10 = fold_convert (type, TREE_OPERAND (arg1, 0));
10315 tree arg11 = fold_convert (type, TREE_OPERAND (arg1, 1));
10316 return fold_build2 (PLUS_EXPR, type,
10317 fold_build2 (MINUS_EXPR, type, arg00, arg10),
10318 fold_build2 (MINUS_EXPR, type, arg01, arg11));
10320 /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
10321 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10323 tree arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
10324 tree arg01 = fold_convert (type, TREE_OPERAND (arg0, 1));
10325 tree tmp = fold_binary (MINUS_EXPR, type, arg00, fold_convert (type, arg1));
10327 return fold_build2 (PLUS_EXPR, type, tmp, arg01);
10330 /* A - (-B) -> A + B */
10331 if (TREE_CODE (arg1) == NEGATE_EXPR)
10332 return fold_build2 (PLUS_EXPR, type, op0,
10333 fold_convert (type, TREE_OPERAND (arg1, 0)));
10334 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
10335 if (TREE_CODE (arg0) == NEGATE_EXPR
10336 && (FLOAT_TYPE_P (type)
10337 || INTEGRAL_TYPE_P (type))
10338 && negate_expr_p (arg1)
10339 && reorder_operands_p (arg0, arg1))
10340 return fold_build2 (MINUS_EXPR, type,
10341 fold_convert (type, negate_expr (arg1)),
10342 fold_convert (type, TREE_OPERAND (arg0, 0)));
10343 /* Convert -A - 1 to ~A. */
10344 if (INTEGRAL_TYPE_P (type)
10345 && TREE_CODE (arg0) == NEGATE_EXPR
10346 && integer_onep (arg1)
10347 && !TYPE_OVERFLOW_TRAPS (type))
10348 return fold_build1 (BIT_NOT_EXPR, type,
10349 fold_convert (type, TREE_OPERAND (arg0, 0)));
10351 /* Convert -1 - A to ~A. */
10352 if (INTEGRAL_TYPE_P (type)
10353 && integer_all_onesp (arg0))
10354 return fold_build1 (BIT_NOT_EXPR, type, op1);
10357 /* X - (X / CST) * CST is X % CST. */
10358 if (INTEGRAL_TYPE_P (type)
10359 && TREE_CODE (arg1) == MULT_EXPR
10360 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10361 && operand_equal_p (arg0,
10362 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
10363 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
10364 TREE_OPERAND (arg1, 1), 0))
10365 return fold_convert (type,
10366 fold_build2 (TRUNC_MOD_EXPR, TREE_TYPE (arg0),
10367 arg0, TREE_OPERAND (arg1, 1)));
10369 if (! FLOAT_TYPE_P (type))
10371 if (integer_zerop (arg0))
10372 return negate_expr (fold_convert (type, arg1));
10373 if (integer_zerop (arg1))
10374 return non_lvalue (fold_convert (type, arg0));
10376 /* Fold A - (A & B) into ~B & A. */
10377 if (!TREE_SIDE_EFFECTS (arg0)
10378 && TREE_CODE (arg1) == BIT_AND_EXPR)
10380 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
10382 tree arg10 = fold_convert (type, TREE_OPERAND (arg1, 0));
10383 return fold_build2 (BIT_AND_EXPR, type,
10384 fold_build1 (BIT_NOT_EXPR, type, arg10),
10385 fold_convert (type, arg0));
10387 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10389 tree arg11 = fold_convert (type, TREE_OPERAND (arg1, 1));
10390 return fold_build2 (BIT_AND_EXPR, type,
10391 fold_build1 (BIT_NOT_EXPR, type, arg11),
10392 fold_convert (type, arg0));
10396 /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10397 any power of 2 minus 1. */
10398 if (TREE_CODE (arg0) == BIT_AND_EXPR
10399 && TREE_CODE (arg1) == BIT_AND_EXPR
10400 && operand_equal_p (TREE_OPERAND (arg0, 0),
10401 TREE_OPERAND (arg1, 0), 0))
10403 tree mask0 = TREE_OPERAND (arg0, 1);
10404 tree mask1 = TREE_OPERAND (arg1, 1);
10405 tree tem = fold_build1 (BIT_NOT_EXPR, type, mask0);
10407 if (operand_equal_p (tem, mask1, 0))
10409 tem = fold_build2 (BIT_XOR_EXPR, type,
10410 TREE_OPERAND (arg0, 0), mask1);
10411 return fold_build2 (MINUS_EXPR, type, tem, mask1);
10416 /* See if ARG1 is zero and X - ARG1 reduces to X. */
10417 else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
10418 return non_lvalue (fold_convert (type, arg0));
10420 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
10421 ARG0 is zero and X + ARG0 reduces to X, since that would mean
10422 (-ARG1 + ARG0) reduces to -ARG1. */
10423 else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10424 return negate_expr (fold_convert (type, arg1));
10426 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10427 __complex__ ( x, -y ). This is not the same for SNaNs or if
10428 signed zeros are involved. */
10429 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10430 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10431 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10433 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10434 tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
10435 tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
10436 bool arg0rz = false, arg0iz = false;
10437 if ((arg0r && (arg0rz = real_zerop (arg0r)))
10438 || (arg0i && (arg0iz = real_zerop (arg0i))))
10440 tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
10441 tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
10442 if (arg0rz && arg1i && real_zerop (arg1i))
10444 tree rp = fold_build1 (NEGATE_EXPR, rtype,
10446 : build1 (REALPART_EXPR, rtype, arg1));
10447 tree ip = arg0i ? arg0i
10448 : build1 (IMAGPART_EXPR, rtype, arg0);
10449 return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10451 else if (arg0iz && arg1r && real_zerop (arg1r))
10453 tree rp = arg0r ? arg0r
10454 : build1 (REALPART_EXPR, rtype, arg0);
10455 tree ip = fold_build1 (NEGATE_EXPR, rtype,
10457 : build1 (IMAGPART_EXPR, rtype, arg1));
10458 return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10463 /* Fold &x - &x. This can happen from &x.foo - &x.
10464 This is unsafe for certain floats even in non-IEEE formats.
10465 In IEEE, it is unsafe because it does wrong for NaNs.
10466 Also note that operand_equal_p is always false if an operand
10469 if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
10470 && operand_equal_p (arg0, arg1, 0))
10471 return fold_convert (type, integer_zero_node);
10473 /* A - B -> A + (-B) if B is easily negatable. */
10474 if (negate_expr_p (arg1)
10475 && ((FLOAT_TYPE_P (type)
10476 /* Avoid this transformation if B is a positive REAL_CST. */
10477 && (TREE_CODE (arg1) != REAL_CST
10478 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10479 || INTEGRAL_TYPE_P (type)))
10480 return fold_build2 (PLUS_EXPR, type,
10481 fold_convert (type, arg0),
10482 fold_convert (type, negate_expr (arg1)));
10484 /* Try folding difference of addresses. */
10486 HOST_WIDE_INT diff;
10488 if ((TREE_CODE (arg0) == ADDR_EXPR
10489 || TREE_CODE (arg1) == ADDR_EXPR)
10490 && ptr_difference_const (arg0, arg1, &diff))
10491 return build_int_cst_type (type, diff);
10494 /* Fold &a[i] - &a[j] to i-j. */
10495 if (TREE_CODE (arg0) == ADDR_EXPR
10496 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10497 && TREE_CODE (arg1) == ADDR_EXPR
10498 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10500 tree aref0 = TREE_OPERAND (arg0, 0);
10501 tree aref1 = TREE_OPERAND (arg1, 0);
10502 if (operand_equal_p (TREE_OPERAND (aref0, 0),
10503 TREE_OPERAND (aref1, 0), 0))
10505 tree op0 = fold_convert (type, TREE_OPERAND (aref0, 1));
10506 tree op1 = fold_convert (type, TREE_OPERAND (aref1, 1));
10507 tree esz = array_ref_element_size (aref0);
10508 tree diff = build2 (MINUS_EXPR, type, op0, op1);
10509 return fold_build2 (MULT_EXPR, type, diff,
10510 fold_convert (type, esz));
10515 if (flag_unsafe_math_optimizations
10516 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10517 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10518 && (tem = distribute_real_division (code, type, arg0, arg1)))
10521 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
10522 same or one. Make sure type is not saturating.
10523 fold_plusminus_mult_expr will re-associate. */
10524 if ((TREE_CODE (arg0) == MULT_EXPR
10525 || TREE_CODE (arg1) == MULT_EXPR)
10526 && !TYPE_SATURATING (type)
10527 && (!FLOAT_TYPE_P (type) || flag_associative_math))
10529 tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
10537 /* (-A) * (-B) -> A * B */
10538 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10539 return fold_build2 (MULT_EXPR, type,
10540 fold_convert (type, TREE_OPERAND (arg0, 0)),
10541 fold_convert (type, negate_expr (arg1)));
10542 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10543 return fold_build2 (MULT_EXPR, type,
10544 fold_convert (type, negate_expr (arg0)),
10545 fold_convert (type, TREE_OPERAND (arg1, 0)));
10547 if (! FLOAT_TYPE_P (type))
10549 if (integer_zerop (arg1))
10550 return omit_one_operand (type, arg1, arg0);
10551 if (integer_onep (arg1))
10552 return non_lvalue (fold_convert (type, arg0));
10553 /* Transform x * -1 into -x. Make sure to do the negation
10554 on the original operand with conversions not stripped
10555 because we can only strip non-sign-changing conversions. */
10556 if (integer_all_onesp (arg1))
10557 return fold_convert (type, negate_expr (op0));
10558 /* Transform x * -C into -x * C if x is easily negatable. */
10559 if (TREE_CODE (arg1) == INTEGER_CST
10560 && tree_int_cst_sgn (arg1) == -1
10561 && negate_expr_p (arg0)
10562 && (tem = negate_expr (arg1)) != arg1
10563 && !TREE_OVERFLOW (tem))
10564 return fold_build2 (MULT_EXPR, type,
10565 fold_convert (type, negate_expr (arg0)), tem);
10567 /* (a * (1 << b)) is (a << b) */
10568 if (TREE_CODE (arg1) == LSHIFT_EXPR
10569 && integer_onep (TREE_OPERAND (arg1, 0)))
10570 return fold_build2 (LSHIFT_EXPR, type, op0,
10571 TREE_OPERAND (arg1, 1));
10572 if (TREE_CODE (arg0) == LSHIFT_EXPR
10573 && integer_onep (TREE_OPERAND (arg0, 0)))
10574 return fold_build2 (LSHIFT_EXPR, type, op1,
10575 TREE_OPERAND (arg0, 1));
10577 /* (A + A) * C -> A * 2 * C */
10578 if (TREE_CODE (arg0) == PLUS_EXPR
10579 && TREE_CODE (arg1) == INTEGER_CST
10580 && operand_equal_p (TREE_OPERAND (arg0, 0),
10581 TREE_OPERAND (arg0, 1), 0))
10582 return fold_build2 (MULT_EXPR, type,
10583 omit_one_operand (type, TREE_OPERAND (arg0, 0),
10584 TREE_OPERAND (arg0, 1)),
10585 fold_build2 (MULT_EXPR, type,
10586 build_int_cst (type, 2) , arg1));
10588 strict_overflow_p = false;
10589 if (TREE_CODE (arg1) == INTEGER_CST
10590 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10591 &strict_overflow_p)))
10593 if (strict_overflow_p)
10594 fold_overflow_warning (("assuming signed overflow does not "
10595 "occur when simplifying "
10597 WARN_STRICT_OVERFLOW_MISC);
10598 return fold_convert (type, tem);
10601 /* Optimize z * conj(z) for integer complex numbers. */
10602 if (TREE_CODE (arg0) == CONJ_EXPR
10603 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10604 return fold_mult_zconjz (type, arg1);
10605 if (TREE_CODE (arg1) == CONJ_EXPR
10606 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10607 return fold_mult_zconjz (type, arg0);
10611 /* Maybe fold x * 0 to 0. The expressions aren't the same
10612 when x is NaN, since x * 0 is also NaN. Nor are they the
10613 same in modes with signed zeros, since multiplying a
10614 negative value by 0 gives -0, not +0. */
10615 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10616 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10617 && real_zerop (arg1))
10618 return omit_one_operand (type, arg1, arg0);
10619 /* In IEEE floating point, x*1 is not equivalent to x for snans. */
10620 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10621 && real_onep (arg1))
10622 return non_lvalue (fold_convert (type, arg0));
10624 /* Transform x * -1.0 into -x. */
10625 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10626 && real_minus_onep (arg1))
10627 return fold_convert (type, negate_expr (arg0));
10629 /* Convert (C1/X)*C2 into (C1*C2)/X. This transformation may change
10630 the result for floating point types due to rounding so it is applied
10631 only if -fassociative-math was specify. */
10632 if (flag_associative_math
10633 && TREE_CODE (arg0) == RDIV_EXPR
10634 && TREE_CODE (arg1) == REAL_CST
10635 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
10637 tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
10640 return fold_build2 (RDIV_EXPR, type, tem,
10641 TREE_OPERAND (arg0, 1));
10644 /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y. */
10645 if (operand_equal_p (arg0, arg1, 0))
10647 tree tem = fold_strip_sign_ops (arg0);
10648 if (tem != NULL_TREE)
10650 tem = fold_convert (type, tem);
10651 return fold_build2 (MULT_EXPR, type, tem, tem);
10655 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10656 This is not the same for NaNs or if signed zeros are
10658 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10659 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10660 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10661 && TREE_CODE (arg1) == COMPLEX_CST
10662 && real_zerop (TREE_REALPART (arg1)))
10664 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10665 if (real_onep (TREE_IMAGPART (arg1)))
10666 return fold_build2 (COMPLEX_EXPR, type,
10667 negate_expr (fold_build1 (IMAGPART_EXPR,
10669 fold_build1 (REALPART_EXPR, rtype, arg0));
10670 else if (real_minus_onep (TREE_IMAGPART (arg1)))
10671 return fold_build2 (COMPLEX_EXPR, type,
10672 fold_build1 (IMAGPART_EXPR, rtype, arg0),
10673 negate_expr (fold_build1 (REALPART_EXPR,
10677 /* Optimize z * conj(z) for floating point complex numbers.
10678 Guarded by flag_unsafe_math_optimizations as non-finite
10679 imaginary components don't produce scalar results. */
10680 if (flag_unsafe_math_optimizations
10681 && TREE_CODE (arg0) == CONJ_EXPR
10682 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10683 return fold_mult_zconjz (type, arg1);
10684 if (flag_unsafe_math_optimizations
10685 && TREE_CODE (arg1) == CONJ_EXPR
10686 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10687 return fold_mult_zconjz (type, arg0);
10689 if (flag_unsafe_math_optimizations)
10691 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10692 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10694 /* Optimizations of root(...)*root(...). */
10695 if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
10698 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10699 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10701 /* Optimize sqrt(x)*sqrt(x) as x. */
10702 if (BUILTIN_SQRT_P (fcode0)
10703 && operand_equal_p (arg00, arg10, 0)
10704 && ! HONOR_SNANS (TYPE_MODE (type)))
10707 /* Optimize root(x)*root(y) as root(x*y). */
10708 rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10709 arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
10710 return build_call_expr (rootfn, 1, arg);
10713 /* Optimize expN(x)*expN(y) as expN(x+y). */
10714 if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
10716 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10717 tree arg = fold_build2 (PLUS_EXPR, type,
10718 CALL_EXPR_ARG (arg0, 0),
10719 CALL_EXPR_ARG (arg1, 0));
10720 return build_call_expr (expfn, 1, arg);
10723 /* Optimizations of pow(...)*pow(...). */
10724 if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
10725 || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
10726 || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
10728 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10729 tree arg01 = CALL_EXPR_ARG (arg0, 1);
10730 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10731 tree arg11 = CALL_EXPR_ARG (arg1, 1);
10733 /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y). */
10734 if (operand_equal_p (arg01, arg11, 0))
10736 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10737 tree arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
10738 return build_call_expr (powfn, 2, arg, arg01);
10741 /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z). */
10742 if (operand_equal_p (arg00, arg10, 0))
10744 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10745 tree arg = fold_build2 (PLUS_EXPR, type, arg01, arg11);
10746 return build_call_expr (powfn, 2, arg00, arg);
10750 /* Optimize tan(x)*cos(x) as sin(x). */
10751 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
10752 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
10753 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
10754 || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
10755 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
10756 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
10757 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10758 CALL_EXPR_ARG (arg1, 0), 0))
10760 tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
10762 if (sinfn != NULL_TREE)
10763 return build_call_expr (sinfn, 1, CALL_EXPR_ARG (arg0, 0));
10766 /* Optimize x*pow(x,c) as pow(x,c+1). */
10767 if (fcode1 == BUILT_IN_POW
10768 || fcode1 == BUILT_IN_POWF
10769 || fcode1 == BUILT_IN_POWL)
10771 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10772 tree arg11 = CALL_EXPR_ARG (arg1, 1);
10773 if (TREE_CODE (arg11) == REAL_CST
10774 && !TREE_OVERFLOW (arg11)
10775 && operand_equal_p (arg0, arg10, 0))
10777 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10781 c = TREE_REAL_CST (arg11);
10782 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10783 arg = build_real (type, c);
10784 return build_call_expr (powfn, 2, arg0, arg);
10788 /* Optimize pow(x,c)*x as pow(x,c+1). */
10789 if (fcode0 == BUILT_IN_POW
10790 || fcode0 == BUILT_IN_POWF
10791 || fcode0 == BUILT_IN_POWL)
10793 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10794 tree arg01 = CALL_EXPR_ARG (arg0, 1);
10795 if (TREE_CODE (arg01) == REAL_CST
10796 && !TREE_OVERFLOW (arg01)
10797 && operand_equal_p (arg1, arg00, 0))
10799 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10803 c = TREE_REAL_CST (arg01);
10804 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10805 arg = build_real (type, c);
10806 return build_call_expr (powfn, 2, arg1, arg);
10810 /* Optimize x*x as pow(x,2.0), which is expanded as x*x. */
10811 if (optimize_function_for_speed_p (cfun)
10812 && operand_equal_p (arg0, arg1, 0))
10814 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
10818 tree arg = build_real (type, dconst2);
10819 return build_call_expr (powfn, 2, arg0, arg);
10828 if (integer_all_onesp (arg1))
10829 return omit_one_operand (type, arg1, arg0);
10830 if (integer_zerop (arg1))
10831 return non_lvalue (fold_convert (type, arg0));
10832 if (operand_equal_p (arg0, arg1, 0))
10833 return non_lvalue (fold_convert (type, arg0));
10835 /* ~X | X is -1. */
10836 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10837 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10839 t1 = fold_convert (type, integer_zero_node);
10840 t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10841 return omit_one_operand (type, t1, arg1);
10844 /* X | ~X is -1. */
10845 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10846 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10848 t1 = fold_convert (type, integer_zero_node);
10849 t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10850 return omit_one_operand (type, t1, arg0);
10853 /* Canonicalize (X & C1) | C2. */
10854 if (TREE_CODE (arg0) == BIT_AND_EXPR
10855 && TREE_CODE (arg1) == INTEGER_CST
10856 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10858 unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, hi3, lo3, mlo, mhi;
10859 int width = TYPE_PRECISION (type), w;
10860 hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
10861 lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
10862 hi2 = TREE_INT_CST_HIGH (arg1);
10863 lo2 = TREE_INT_CST_LOW (arg1);
10865 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
10866 if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
10867 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10869 if (width > HOST_BITS_PER_WIDE_INT)
10871 mhi = (unsigned HOST_WIDE_INT) -1
10872 >> (2 * HOST_BITS_PER_WIDE_INT - width);
10878 mlo = (unsigned HOST_WIDE_INT) -1
10879 >> (HOST_BITS_PER_WIDE_INT - width);
10882 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
10883 if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
10884 return fold_build2 (BIT_IOR_EXPR, type,
10885 TREE_OPERAND (arg0, 0), arg1);
10887 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10888 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10889 mode which allows further optimizations. */
10896 for (w = BITS_PER_UNIT;
10897 w <= width && w <= HOST_BITS_PER_WIDE_INT;
10900 unsigned HOST_WIDE_INT mask
10901 = (unsigned HOST_WIDE_INT) -1 >> (HOST_BITS_PER_WIDE_INT - w);
10902 if (((lo1 | lo2) & mask) == mask
10903 && (lo1 & ~mask) == 0 && hi1 == 0)
10910 if (hi3 != hi1 || lo3 != lo1)
10911 return fold_build2 (BIT_IOR_EXPR, type,
10912 fold_build2 (BIT_AND_EXPR, type,
10913 TREE_OPERAND (arg0, 0),
10914 build_int_cst_wide (type,
10919 /* (X & Y) | Y is (X, Y). */
10920 if (TREE_CODE (arg0) == BIT_AND_EXPR
10921 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10922 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10923 /* (X & Y) | X is (Y, X). */
10924 if (TREE_CODE (arg0) == BIT_AND_EXPR
10925 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10926 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10927 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
10928 /* X | (X & Y) is (Y, X). */
10929 if (TREE_CODE (arg1) == BIT_AND_EXPR
10930 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10931 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10932 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
10933 /* X | (Y & X) is (Y, X). */
10934 if (TREE_CODE (arg1) == BIT_AND_EXPR
10935 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10936 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10937 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
10939 t1 = distribute_bit_expr (code, type, arg0, arg1);
10940 if (t1 != NULL_TREE)
10943 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
10945 This results in more efficient code for machines without a NAND
10946 instruction. Combine will canonicalize to the first form
10947 which will allow use of NAND instructions provided by the
10948 backend if they exist. */
10949 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10950 && TREE_CODE (arg1) == BIT_NOT_EXPR)
10952 return fold_build1 (BIT_NOT_EXPR, type,
10953 build2 (BIT_AND_EXPR, type,
10954 fold_convert (type,
10955 TREE_OPERAND (arg0, 0)),
10956 fold_convert (type,
10957 TREE_OPERAND (arg1, 0))));
10960 /* See if this can be simplified into a rotate first. If that
10961 is unsuccessful continue in the association code. */
10965 if (integer_zerop (arg1))
10966 return non_lvalue (fold_convert (type, arg0));
10967 if (integer_all_onesp (arg1))
10968 return fold_build1 (BIT_NOT_EXPR, type, op0);
10969 if (operand_equal_p (arg0, arg1, 0))
10970 return omit_one_operand (type, integer_zero_node, arg0);
10972 /* ~X ^ X is -1. */
10973 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10974 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10976 t1 = fold_convert (type, integer_zero_node);
10977 t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10978 return omit_one_operand (type, t1, arg1);
10981 /* X ^ ~X is -1. */
10982 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10983 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10985 t1 = fold_convert (type, integer_zero_node);
10986 t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10987 return omit_one_operand (type, t1, arg0);
10990 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
10991 with a constant, and the two constants have no bits in common,
10992 we should treat this as a BIT_IOR_EXPR since this may produce more
10993 simplifications. */
10994 if (TREE_CODE (arg0) == BIT_AND_EXPR
10995 && TREE_CODE (arg1) == BIT_AND_EXPR
10996 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10997 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10998 && integer_zerop (const_binop (BIT_AND_EXPR,
10999 TREE_OPERAND (arg0, 1),
11000 TREE_OPERAND (arg1, 1), 0)))
11002 code = BIT_IOR_EXPR;
11006 /* (X | Y) ^ X -> Y & ~ X*/
11007 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11008 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11010 tree t2 = TREE_OPERAND (arg0, 1);
11011 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
11013 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
11014 fold_convert (type, t1));
11018 /* (Y | X) ^ X -> Y & ~ X*/
11019 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11020 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11022 tree t2 = TREE_OPERAND (arg0, 0);
11023 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
11025 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
11026 fold_convert (type, t1));
11030 /* X ^ (X | Y) -> Y & ~ X*/
11031 if (TREE_CODE (arg1) == BIT_IOR_EXPR
11032 && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
11034 tree t2 = TREE_OPERAND (arg1, 1);
11035 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
11037 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
11038 fold_convert (type, t1));
11042 /* X ^ (Y | X) -> Y & ~ X*/
11043 if (TREE_CODE (arg1) == BIT_IOR_EXPR
11044 && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
11046 tree t2 = TREE_OPERAND (arg1, 0);
11047 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
11049 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
11050 fold_convert (type, t1));
11054 /* Convert ~X ^ ~Y to X ^ Y. */
11055 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11056 && TREE_CODE (arg1) == BIT_NOT_EXPR)
11057 return fold_build2 (code, type,
11058 fold_convert (type, TREE_OPERAND (arg0, 0)),
11059 fold_convert (type, TREE_OPERAND (arg1, 0)));
11061 /* Convert ~X ^ C to X ^ ~C. */
11062 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11063 && TREE_CODE (arg1) == INTEGER_CST)
11064 return fold_build2 (code, type,
11065 fold_convert (type, TREE_OPERAND (arg0, 0)),
11066 fold_build1 (BIT_NOT_EXPR, type, arg1));
11068 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
11069 if (TREE_CODE (arg0) == BIT_AND_EXPR
11070 && integer_onep (TREE_OPERAND (arg0, 1))
11071 && integer_onep (arg1))
11072 return fold_build2 (EQ_EXPR, type, arg0,
11073 build_int_cst (TREE_TYPE (arg0), 0));
11075 /* Fold (X & Y) ^ Y as ~X & Y. */
11076 if (TREE_CODE (arg0) == BIT_AND_EXPR
11077 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11079 tem = fold_convert (type, TREE_OPERAND (arg0, 0));
11080 return fold_build2 (BIT_AND_EXPR, type,
11081 fold_build1 (BIT_NOT_EXPR, type, tem),
11082 fold_convert (type, arg1));
11084 /* Fold (X & Y) ^ X as ~Y & X. */
11085 if (TREE_CODE (arg0) == BIT_AND_EXPR
11086 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11087 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11089 tem = fold_convert (type, TREE_OPERAND (arg0, 1));
11090 return fold_build2 (BIT_AND_EXPR, type,
11091 fold_build1 (BIT_NOT_EXPR, type, tem),
11092 fold_convert (type, arg1));
11094 /* Fold X ^ (X & Y) as X & ~Y. */
11095 if (TREE_CODE (arg1) == BIT_AND_EXPR
11096 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11098 tem = fold_convert (type, TREE_OPERAND (arg1, 1));
11099 return fold_build2 (BIT_AND_EXPR, type,
11100 fold_convert (type, arg0),
11101 fold_build1 (BIT_NOT_EXPR, type, tem));
11103 /* Fold X ^ (Y & X) as ~Y & X. */
11104 if (TREE_CODE (arg1) == BIT_AND_EXPR
11105 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11106 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11108 tem = fold_convert (type, TREE_OPERAND (arg1, 0));
11109 return fold_build2 (BIT_AND_EXPR, type,
11110 fold_build1 (BIT_NOT_EXPR, type, tem),
11111 fold_convert (type, arg0));
11114 /* See if this can be simplified into a rotate first. If that
11115 is unsuccessful continue in the association code. */
11119 if (integer_all_onesp (arg1))
11120 return non_lvalue (fold_convert (type, arg0));
11121 if (integer_zerop (arg1))
11122 return omit_one_operand (type, arg1, arg0);
11123 if (operand_equal_p (arg0, arg1, 0))
11124 return non_lvalue (fold_convert (type, arg0));
11126 /* ~X & X is always zero. */
11127 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11128 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11129 return omit_one_operand (type, integer_zero_node, arg1);
11131 /* X & ~X is always zero. */
11132 if (TREE_CODE (arg1) == BIT_NOT_EXPR
11133 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11134 return omit_one_operand (type, integer_zero_node, arg0);
11136 /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2). */
11137 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11138 && TREE_CODE (arg1) == INTEGER_CST
11139 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11141 tree tmp1 = fold_convert (type, arg1);
11142 tree tmp2 = fold_convert (type, TREE_OPERAND (arg0, 0));
11143 tree tmp3 = fold_convert (type, TREE_OPERAND (arg0, 1));
11144 tmp2 = fold_build2 (BIT_AND_EXPR, type, tmp2, tmp1);
11145 tmp3 = fold_build2 (BIT_AND_EXPR, type, tmp3, tmp1);
11146 return fold_convert (type,
11147 fold_build2 (BIT_IOR_EXPR, type, tmp2, tmp3));
11150 /* (X | Y) & Y is (X, Y). */
11151 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11152 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11153 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
11154 /* (X | Y) & X is (Y, X). */
11155 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11156 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11157 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11158 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
11159 /* X & (X | Y) is (Y, X). */
11160 if (TREE_CODE (arg1) == BIT_IOR_EXPR
11161 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
11162 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
11163 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
11164 /* X & (Y | X) is (Y, X). */
11165 if (TREE_CODE (arg1) == BIT_IOR_EXPR
11166 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11167 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11168 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
11170 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
11171 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11172 && integer_onep (TREE_OPERAND (arg0, 1))
11173 && integer_onep (arg1))
11175 tem = TREE_OPERAND (arg0, 0);
11176 return fold_build2 (EQ_EXPR, type,
11177 fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
11178 build_int_cst (TREE_TYPE (tem), 1)),
11179 build_int_cst (TREE_TYPE (tem), 0));
11181 /* Fold ~X & 1 as (X & 1) == 0. */
11182 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11183 && integer_onep (arg1))
11185 tem = TREE_OPERAND (arg0, 0);
11186 return fold_build2 (EQ_EXPR, type,
11187 fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
11188 build_int_cst (TREE_TYPE (tem), 1)),
11189 build_int_cst (TREE_TYPE (tem), 0));
11192 /* Fold (X ^ Y) & Y as ~X & Y. */
11193 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11194 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11196 tem = fold_convert (type, TREE_OPERAND (arg0, 0));
11197 return fold_build2 (BIT_AND_EXPR, type,
11198 fold_build1 (BIT_NOT_EXPR, type, tem),
11199 fold_convert (type, arg1));
11201 /* Fold (X ^ Y) & X as ~Y & X. */
11202 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11203 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11204 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11206 tem = fold_convert (type, TREE_OPERAND (arg0, 1));
11207 return fold_build2 (BIT_AND_EXPR, type,
11208 fold_build1 (BIT_NOT_EXPR, type, tem),
11209 fold_convert (type, arg1));
11211 /* Fold X & (X ^ Y) as X & ~Y. */
11212 if (TREE_CODE (arg1) == BIT_XOR_EXPR
11213 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11215 tem = fold_convert (type, TREE_OPERAND (arg1, 1));
11216 return fold_build2 (BIT_AND_EXPR, type,
11217 fold_convert (type, arg0),
11218 fold_build1 (BIT_NOT_EXPR, type, tem));
11220 /* Fold X & (Y ^ X) as ~Y & X. */
11221 if (TREE_CODE (arg1) == BIT_XOR_EXPR
11222 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11223 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11225 tem = fold_convert (type, TREE_OPERAND (arg1, 0));
11226 return fold_build2 (BIT_AND_EXPR, type,
11227 fold_build1 (BIT_NOT_EXPR, type, tem),
11228 fold_convert (type, arg0));
11231 t1 = distribute_bit_expr (code, type, arg0, arg1);
11232 if (t1 != NULL_TREE)
11234 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11235 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11236 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11239 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11241 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
11242 && (~TREE_INT_CST_LOW (arg1)
11243 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
11244 return fold_convert (type, TREE_OPERAND (arg0, 0));
11247 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
11249 This results in more efficient code for machines without a NOR
11250 instruction. Combine will canonicalize to the first form
11251 which will allow use of NOR instructions provided by the
11252 backend if they exist. */
11253 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11254 && TREE_CODE (arg1) == BIT_NOT_EXPR)
11256 return fold_build1 (BIT_NOT_EXPR, type,
11257 build2 (BIT_IOR_EXPR, type,
11258 fold_convert (type,
11259 TREE_OPERAND (arg0, 0)),
11260 fold_convert (type,
11261 TREE_OPERAND (arg1, 0))));
11264 /* If arg0 is derived from the address of an object or function, we may
11265 be able to fold this expression using the object or function's
11267 if (POINTER_TYPE_P (TREE_TYPE (arg0)) && host_integerp (arg1, 1))
11269 unsigned HOST_WIDE_INT modulus, residue;
11270 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg1);
11272 modulus = get_pointer_modulus_and_residue (arg0, &residue);
11274 /* This works because modulus is a power of 2. If this weren't the
11275 case, we'd have to replace it by its greatest power-of-2
11276 divisor: modulus & -modulus. */
11278 return build_int_cst (type, residue & low);
11281 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
11282 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
11283 if the new mask might be further optimized. */
11284 if ((TREE_CODE (arg0) == LSHIFT_EXPR
11285 || TREE_CODE (arg0) == RSHIFT_EXPR)
11286 && host_integerp (TREE_OPERAND (arg0, 1), 1)
11287 && host_integerp (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)))
11288 && tree_low_cst (TREE_OPERAND (arg0, 1), 1)
11289 < TYPE_PRECISION (TREE_TYPE (arg0))
11290 && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT
11291 && tree_low_cst (TREE_OPERAND (arg0, 1), 1) > 0)
11293 unsigned int shiftc = tree_low_cst (TREE_OPERAND (arg0, 1), 1);
11294 unsigned HOST_WIDE_INT mask
11295 = tree_low_cst (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)));
11296 unsigned HOST_WIDE_INT newmask, zerobits = 0;
11297 tree shift_type = TREE_TYPE (arg0);
11299 if (TREE_CODE (arg0) == LSHIFT_EXPR)
11300 zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
11301 else if (TREE_CODE (arg0) == RSHIFT_EXPR
11302 && TYPE_PRECISION (TREE_TYPE (arg0))
11303 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg0))))
11305 unsigned int prec = TYPE_PRECISION (TREE_TYPE (arg0));
11306 tree arg00 = TREE_OPERAND (arg0, 0);
11307 /* See if more bits can be proven as zero because of
11309 if (TREE_CODE (arg00) == NOP_EXPR
11310 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0))))
11312 tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0));
11313 if (TYPE_PRECISION (inner_type)
11314 == GET_MODE_BITSIZE (TYPE_MODE (inner_type))
11315 && TYPE_PRECISION (inner_type) < prec)
11317 prec = TYPE_PRECISION (inner_type);
11318 /* See if we can shorten the right shift. */
11320 shift_type = inner_type;
11323 zerobits = ~(unsigned HOST_WIDE_INT) 0;
11324 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
11325 zerobits <<= prec - shiftc;
11326 /* For arithmetic shift if sign bit could be set, zerobits
11327 can contain actually sign bits, so no transformation is
11328 possible, unless MASK masks them all away. In that
11329 case the shift needs to be converted into logical shift. */
11330 if (!TYPE_UNSIGNED (TREE_TYPE (arg0))
11331 && prec == TYPE_PRECISION (TREE_TYPE (arg0)))
11333 if ((mask & zerobits) == 0)
11334 shift_type = unsigned_type_for (TREE_TYPE (arg0));
11340 /* ((X << 16) & 0xff00) is (X, 0). */
11341 if ((mask & zerobits) == mask)
11342 return omit_one_operand (type, build_int_cst (type, 0), arg0);
11344 newmask = mask | zerobits;
11345 if (newmask != mask && (newmask & (newmask + 1)) == 0)
11349 /* Only do the transformation if NEWMASK is some integer
11351 for (prec = BITS_PER_UNIT;
11352 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
11353 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
11355 if (prec < HOST_BITS_PER_WIDE_INT
11356 || newmask == ~(unsigned HOST_WIDE_INT) 0)
11358 if (shift_type != TREE_TYPE (arg0))
11360 tem = fold_build2 (TREE_CODE (arg0), shift_type,
11361 fold_convert (shift_type,
11362 TREE_OPERAND (arg0, 0)),
11363 TREE_OPERAND (arg0, 1));
11364 tem = fold_convert (type, tem);
11368 return fold_build2 (BIT_AND_EXPR, type, tem,
11369 build_int_cst_type (TREE_TYPE (op1),
11378 /* Don't touch a floating-point divide by zero unless the mode
11379 of the constant can represent infinity. */
11380 if (TREE_CODE (arg1) == REAL_CST
11381 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11382 && real_zerop (arg1))
11385 /* Optimize A / A to 1.0 if we don't care about
11386 NaNs or Infinities. Skip the transformation
11387 for non-real operands. */
11388 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
11389 && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11390 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
11391 && operand_equal_p (arg0, arg1, 0))
11393 tree r = build_real (TREE_TYPE (arg0), dconst1);
11395 return omit_two_operands (type, r, arg0, arg1);
11398 /* The complex version of the above A / A optimization. */
11399 if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11400 && operand_equal_p (arg0, arg1, 0))
11402 tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
11403 if (! HONOR_NANS (TYPE_MODE (elem_type))
11404 && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
11406 tree r = build_real (elem_type, dconst1);
11407 /* omit_two_operands will call fold_convert for us. */
11408 return omit_two_operands (type, r, arg0, arg1);
11412 /* (-A) / (-B) -> A / B */
11413 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11414 return fold_build2 (RDIV_EXPR, type,
11415 TREE_OPERAND (arg0, 0),
11416 negate_expr (arg1));
11417 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11418 return fold_build2 (RDIV_EXPR, type,
11419 negate_expr (arg0),
11420 TREE_OPERAND (arg1, 0));
11422 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
11423 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11424 && real_onep (arg1))
11425 return non_lvalue (fold_convert (type, arg0));
11427 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
11428 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11429 && real_minus_onep (arg1))
11430 return non_lvalue (fold_convert (type, negate_expr (arg0)));
11432 /* If ARG1 is a constant, we can convert this to a multiply by the
11433 reciprocal. This does not have the same rounding properties,
11434 so only do this if -freciprocal-math. We can actually
11435 always safely do it if ARG1 is a power of two, but it's hard to
11436 tell if it is or not in a portable manner. */
11437 if (TREE_CODE (arg1) == REAL_CST)
11439 if (flag_reciprocal_math
11440 && 0 != (tem = const_binop (code, build_real (type, dconst1),
11442 return fold_build2 (MULT_EXPR, type, arg0, tem);
11443 /* Find the reciprocal if optimizing and the result is exact. */
11447 r = TREE_REAL_CST (arg1);
11448 if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
11450 tem = build_real (type, r);
11451 return fold_build2 (MULT_EXPR, type,
11452 fold_convert (type, arg0), tem);
11456 /* Convert A/B/C to A/(B*C). */
11457 if (flag_reciprocal_math
11458 && TREE_CODE (arg0) == RDIV_EXPR)
11459 return fold_build2 (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
11460 fold_build2 (MULT_EXPR, type,
11461 TREE_OPERAND (arg0, 1), arg1));
11463 /* Convert A/(B/C) to (A/B)*C. */
11464 if (flag_reciprocal_math
11465 && TREE_CODE (arg1) == RDIV_EXPR)
11466 return fold_build2 (MULT_EXPR, type,
11467 fold_build2 (RDIV_EXPR, type, arg0,
11468 TREE_OPERAND (arg1, 0)),
11469 TREE_OPERAND (arg1, 1));
11471 /* Convert C1/(X*C2) into (C1/C2)/X. */
11472 if (flag_reciprocal_math
11473 && TREE_CODE (arg1) == MULT_EXPR
11474 && TREE_CODE (arg0) == REAL_CST
11475 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
11477 tree tem = const_binop (RDIV_EXPR, arg0,
11478 TREE_OPERAND (arg1, 1), 0);
11480 return fold_build2 (RDIV_EXPR, type, tem,
11481 TREE_OPERAND (arg1, 0));
11484 if (flag_unsafe_math_optimizations)
11486 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11487 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11489 /* Optimize sin(x)/cos(x) as tan(x). */
11490 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
11491 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
11492 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
11493 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11494 CALL_EXPR_ARG (arg1, 0), 0))
11496 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11498 if (tanfn != NULL_TREE)
11499 return build_call_expr (tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11502 /* Optimize cos(x)/sin(x) as 1.0/tan(x). */
11503 if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11504 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11505 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
11506 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11507 CALL_EXPR_ARG (arg1, 0), 0))
11509 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11511 if (tanfn != NULL_TREE)
11513 tree tmp = build_call_expr (tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11514 return fold_build2 (RDIV_EXPR, type,
11515 build_real (type, dconst1), tmp);
11519 /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11520 NaNs or Infinities. */
11521 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11522 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11523 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11525 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11526 tree arg01 = CALL_EXPR_ARG (arg1, 0);
11528 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11529 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11530 && operand_equal_p (arg00, arg01, 0))
11532 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11534 if (cosfn != NULL_TREE)
11535 return build_call_expr (cosfn, 1, arg00);
11539 /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11540 NaNs or Infinities. */
11541 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11542 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11543 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11545 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11546 tree arg01 = CALL_EXPR_ARG (arg1, 0);
11548 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11549 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11550 && operand_equal_p (arg00, arg01, 0))
11552 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11554 if (cosfn != NULL_TREE)
11556 tree tmp = build_call_expr (cosfn, 1, arg00);
11557 return fold_build2 (RDIV_EXPR, type,
11558 build_real (type, dconst1),
11564 /* Optimize pow(x,c)/x as pow(x,c-1). */
11565 if (fcode0 == BUILT_IN_POW
11566 || fcode0 == BUILT_IN_POWF
11567 || fcode0 == BUILT_IN_POWL)
11569 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11570 tree arg01 = CALL_EXPR_ARG (arg0, 1);
11571 if (TREE_CODE (arg01) == REAL_CST
11572 && !TREE_OVERFLOW (arg01)
11573 && operand_equal_p (arg1, arg00, 0))
11575 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11579 c = TREE_REAL_CST (arg01);
11580 real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
11581 arg = build_real (type, c);
11582 return build_call_expr (powfn, 2, arg1, arg);
11586 /* Optimize a/root(b/c) into a*root(c/b). */
11587 if (BUILTIN_ROOT_P (fcode1))
11589 tree rootarg = CALL_EXPR_ARG (arg1, 0);
11591 if (TREE_CODE (rootarg) == RDIV_EXPR)
11593 tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11594 tree b = TREE_OPERAND (rootarg, 0);
11595 tree c = TREE_OPERAND (rootarg, 1);
11597 tree tmp = fold_build2 (RDIV_EXPR, type, c, b);
11599 tmp = build_call_expr (rootfn, 1, tmp);
11600 return fold_build2 (MULT_EXPR, type, arg0, tmp);
11604 /* Optimize x/expN(y) into x*expN(-y). */
11605 if (BUILTIN_EXPONENT_P (fcode1))
11607 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11608 tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
11609 arg1 = build_call_expr (expfn, 1, fold_convert (type, arg));
11610 return fold_build2 (MULT_EXPR, type, arg0, arg1);
11613 /* Optimize x/pow(y,z) into x*pow(y,-z). */
11614 if (fcode1 == BUILT_IN_POW
11615 || fcode1 == BUILT_IN_POWF
11616 || fcode1 == BUILT_IN_POWL)
11618 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11619 tree arg10 = CALL_EXPR_ARG (arg1, 0);
11620 tree arg11 = CALL_EXPR_ARG (arg1, 1);
11621 tree neg11 = fold_convert (type, negate_expr (arg11));
11622 arg1 = build_call_expr (powfn, 2, arg10, neg11);
11623 return fold_build2 (MULT_EXPR, type, arg0, arg1);
11628 case TRUNC_DIV_EXPR:
11629 case FLOOR_DIV_EXPR:
11630 /* Simplify A / (B << N) where A and B are positive and B is
11631 a power of 2, to A >> (N + log2(B)). */
11632 strict_overflow_p = false;
11633 if (TREE_CODE (arg1) == LSHIFT_EXPR
11634 && (TYPE_UNSIGNED (type)
11635 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11637 tree sval = TREE_OPERAND (arg1, 0);
11638 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11640 tree sh_cnt = TREE_OPERAND (arg1, 1);
11641 unsigned long pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
11643 if (strict_overflow_p)
11644 fold_overflow_warning (("assuming signed overflow does not "
11645 "occur when simplifying A / (B << N)"),
11646 WARN_STRICT_OVERFLOW_MISC);
11648 sh_cnt = fold_build2 (PLUS_EXPR, TREE_TYPE (sh_cnt),
11649 sh_cnt, build_int_cst (NULL_TREE, pow2));
11650 return fold_build2 (RSHIFT_EXPR, type,
11651 fold_convert (type, arg0), sh_cnt);
11655 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
11656 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
11657 if (INTEGRAL_TYPE_P (type)
11658 && TYPE_UNSIGNED (type)
11659 && code == FLOOR_DIV_EXPR)
11660 return fold_build2 (TRUNC_DIV_EXPR, type, op0, op1);
11664 case ROUND_DIV_EXPR:
11665 case CEIL_DIV_EXPR:
11666 case EXACT_DIV_EXPR:
11667 if (integer_onep (arg1))
11668 return non_lvalue (fold_convert (type, arg0));
11669 if (integer_zerop (arg1))
11671 /* X / -1 is -X. */
11672 if (!TYPE_UNSIGNED (type)
11673 && TREE_CODE (arg1) == INTEGER_CST
11674 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11675 && TREE_INT_CST_HIGH (arg1) == -1)
11676 return fold_convert (type, negate_expr (arg0));
11678 /* Convert -A / -B to A / B when the type is signed and overflow is
11680 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11681 && TREE_CODE (arg0) == NEGATE_EXPR
11682 && negate_expr_p (arg1))
11684 if (INTEGRAL_TYPE_P (type))
11685 fold_overflow_warning (("assuming signed overflow does not occur "
11686 "when distributing negation across "
11688 WARN_STRICT_OVERFLOW_MISC);
11689 return fold_build2 (code, type,
11690 fold_convert (type, TREE_OPERAND (arg0, 0)),
11691 fold_convert (type, negate_expr (arg1)));
11693 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11694 && TREE_CODE (arg1) == NEGATE_EXPR
11695 && negate_expr_p (arg0))
11697 if (INTEGRAL_TYPE_P (type))
11698 fold_overflow_warning (("assuming signed overflow does not occur "
11699 "when distributing negation across "
11701 WARN_STRICT_OVERFLOW_MISC);
11702 return fold_build2 (code, type,
11703 fold_convert (type, negate_expr (arg0)),
11704 fold_convert (type, TREE_OPERAND (arg1, 0)));
11707 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11708 operation, EXACT_DIV_EXPR.
11710 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11711 At one time others generated faster code, it's not clear if they do
11712 after the last round to changes to the DIV code in expmed.c. */
11713 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11714 && multiple_of_p (type, arg0, arg1))
11715 return fold_build2 (EXACT_DIV_EXPR, type, arg0, arg1);
11717 strict_overflow_p = false;
11718 if (TREE_CODE (arg1) == INTEGER_CST
11719 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11720 &strict_overflow_p)))
11722 if (strict_overflow_p)
11723 fold_overflow_warning (("assuming signed overflow does not occur "
11724 "when simplifying division"),
11725 WARN_STRICT_OVERFLOW_MISC);
11726 return fold_convert (type, tem);
11731 case CEIL_MOD_EXPR:
11732 case FLOOR_MOD_EXPR:
11733 case ROUND_MOD_EXPR:
11734 case TRUNC_MOD_EXPR:
11735 /* X % 1 is always zero, but be sure to preserve any side
11737 if (integer_onep (arg1))
11738 return omit_one_operand (type, integer_zero_node, arg0);
11740 /* X % 0, return X % 0 unchanged so that we can get the
11741 proper warnings and errors. */
11742 if (integer_zerop (arg1))
11745 /* 0 % X is always zero, but be sure to preserve any side
11746 effects in X. Place this after checking for X == 0. */
11747 if (integer_zerop (arg0))
11748 return omit_one_operand (type, integer_zero_node, arg1);
11750 /* X % -1 is zero. */
11751 if (!TYPE_UNSIGNED (type)
11752 && TREE_CODE (arg1) == INTEGER_CST
11753 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11754 && TREE_INT_CST_HIGH (arg1) == -1)
11755 return omit_one_operand (type, integer_zero_node, arg0);
11757 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
11758 i.e. "X % C" into "X & (C - 1)", if X and C are positive. */
11759 strict_overflow_p = false;
11760 if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
11761 && (TYPE_UNSIGNED (type)
11762 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11765 /* Also optimize A % (C << N) where C is a power of 2,
11766 to A & ((C << N) - 1). */
11767 if (TREE_CODE (arg1) == LSHIFT_EXPR)
11768 c = TREE_OPERAND (arg1, 0);
11770 if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
11772 tree mask = fold_build2 (MINUS_EXPR, TREE_TYPE (arg1), arg1,
11773 build_int_cst (TREE_TYPE (arg1), 1));
11774 if (strict_overflow_p)
11775 fold_overflow_warning (("assuming signed overflow does not "
11776 "occur when simplifying "
11777 "X % (power of two)"),
11778 WARN_STRICT_OVERFLOW_MISC);
11779 return fold_build2 (BIT_AND_EXPR, type,
11780 fold_convert (type, arg0),
11781 fold_convert (type, mask));
11785 /* X % -C is the same as X % C. */
11786 if (code == TRUNC_MOD_EXPR
11787 && !TYPE_UNSIGNED (type)
11788 && TREE_CODE (arg1) == INTEGER_CST
11789 && !TREE_OVERFLOW (arg1)
11790 && TREE_INT_CST_HIGH (arg1) < 0
11791 && !TYPE_OVERFLOW_TRAPS (type)
11792 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
11793 && !sign_bit_p (arg1, arg1))
11794 return fold_build2 (code, type, fold_convert (type, arg0),
11795 fold_convert (type, negate_expr (arg1)));
11797 /* X % -Y is the same as X % Y. */
11798 if (code == TRUNC_MOD_EXPR
11799 && !TYPE_UNSIGNED (type)
11800 && TREE_CODE (arg1) == NEGATE_EXPR
11801 && !TYPE_OVERFLOW_TRAPS (type))
11802 return fold_build2 (code, type, fold_convert (type, arg0),
11803 fold_convert (type, TREE_OPERAND (arg1, 0)));
11805 if (TREE_CODE (arg1) == INTEGER_CST
11806 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11807 &strict_overflow_p)))
11809 if (strict_overflow_p)
11810 fold_overflow_warning (("assuming signed overflow does not occur "
11811 "when simplifying modulus"),
11812 WARN_STRICT_OVERFLOW_MISC);
11813 return fold_convert (type, tem);
11820 if (integer_all_onesp (arg0))
11821 return omit_one_operand (type, arg0, arg1);
11825 /* Optimize -1 >> x for arithmetic right shifts. */
11826 if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type)
11827 && tree_expr_nonnegative_p (arg1))
11828 return omit_one_operand (type, arg0, arg1);
11829 /* ... fall through ... */
11833 if (integer_zerop (arg1))
11834 return non_lvalue (fold_convert (type, arg0));
11835 if (integer_zerop (arg0))
11836 return omit_one_operand (type, arg0, arg1);
11838 /* Since negative shift count is not well-defined,
11839 don't try to compute it in the compiler. */
11840 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11843 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
11844 if (TREE_CODE (op0) == code && host_integerp (arg1, false)
11845 && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11846 && host_integerp (TREE_OPERAND (arg0, 1), false)
11847 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11849 HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
11850 + TREE_INT_CST_LOW (arg1));
11852 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
11853 being well defined. */
11854 if (low >= TYPE_PRECISION (type))
11856 if (code == LROTATE_EXPR || code == RROTATE_EXPR)
11857 low = low % TYPE_PRECISION (type);
11858 else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
11859 return build_int_cst (type, 0);
11861 low = TYPE_PRECISION (type) - 1;
11864 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11865 build_int_cst (type, low));
11868 /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
11869 into x & ((unsigned)-1 >> c) for unsigned types. */
11870 if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
11871 || (TYPE_UNSIGNED (type)
11872 && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
11873 && host_integerp (arg1, false)
11874 && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11875 && host_integerp (TREE_OPERAND (arg0, 1), false)
11876 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11878 HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
11879 HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
11885 arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
11887 lshift = build_int_cst (type, -1);
11888 lshift = int_const_binop (code, lshift, arg1, 0);
11890 return fold_build2 (BIT_AND_EXPR, type, arg00, lshift);
11894 /* Rewrite an LROTATE_EXPR by a constant into an
11895 RROTATE_EXPR by a new constant. */
11896 if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
11898 tree tem = build_int_cst (TREE_TYPE (arg1),
11899 TYPE_PRECISION (type));
11900 tem = const_binop (MINUS_EXPR, tem, arg1, 0);
11901 return fold_build2 (RROTATE_EXPR, type, op0, tem);
11904 /* If we have a rotate of a bit operation with the rotate count and
11905 the second operand of the bit operation both constant,
11906 permute the two operations. */
11907 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11908 && (TREE_CODE (arg0) == BIT_AND_EXPR
11909 || TREE_CODE (arg0) == BIT_IOR_EXPR
11910 || TREE_CODE (arg0) == BIT_XOR_EXPR)
11911 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11912 return fold_build2 (TREE_CODE (arg0), type,
11913 fold_build2 (code, type,
11914 TREE_OPERAND (arg0, 0), arg1),
11915 fold_build2 (code, type,
11916 TREE_OPERAND (arg0, 1), arg1));
11918 /* Two consecutive rotates adding up to the precision of the
11919 type can be ignored. */
11920 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11921 && TREE_CODE (arg0) == RROTATE_EXPR
11922 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11923 && TREE_INT_CST_HIGH (arg1) == 0
11924 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
11925 && ((TREE_INT_CST_LOW (arg1)
11926 + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
11927 == (unsigned int) TYPE_PRECISION (type)))
11928 return TREE_OPERAND (arg0, 0);
11930 /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
11931 (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
11932 if the latter can be further optimized. */
11933 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
11934 && TREE_CODE (arg0) == BIT_AND_EXPR
11935 && TREE_CODE (arg1) == INTEGER_CST
11936 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11938 tree mask = fold_build2 (code, type,
11939 fold_convert (type, TREE_OPERAND (arg0, 1)),
11941 tree shift = fold_build2 (code, type,
11942 fold_convert (type, TREE_OPERAND (arg0, 0)),
11944 tem = fold_binary (BIT_AND_EXPR, type, shift, mask);
11952 if (operand_equal_p (arg0, arg1, 0))
11953 return omit_one_operand (type, arg0, arg1);
11954 if (INTEGRAL_TYPE_P (type)
11955 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
11956 return omit_one_operand (type, arg1, arg0);
11957 tem = fold_minmax (MIN_EXPR, type, arg0, arg1);
11963 if (operand_equal_p (arg0, arg1, 0))
11964 return omit_one_operand (type, arg0, arg1);
11965 if (INTEGRAL_TYPE_P (type)
11966 && TYPE_MAX_VALUE (type)
11967 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
11968 return omit_one_operand (type, arg1, arg0);
11969 tem = fold_minmax (MAX_EXPR, type, arg0, arg1);
11974 case TRUTH_ANDIF_EXPR:
11975 /* Note that the operands of this must be ints
11976 and their values must be 0 or 1.
11977 ("true" is a fixed value perhaps depending on the language.) */
11978 /* If first arg is constant zero, return it. */
11979 if (integer_zerop (arg0))
11980 return fold_convert (type, arg0);
11981 case TRUTH_AND_EXPR:
11982 /* If either arg is constant true, drop it. */
11983 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11984 return non_lvalue (fold_convert (type, arg1));
11985 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
11986 /* Preserve sequence points. */
11987 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11988 return non_lvalue (fold_convert (type, arg0));
11989 /* If second arg is constant zero, result is zero, but first arg
11990 must be evaluated. */
11991 if (integer_zerop (arg1))
11992 return omit_one_operand (type, arg1, arg0);
11993 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
11994 case will be handled here. */
11995 if (integer_zerop (arg0))
11996 return omit_one_operand (type, arg0, arg1);
11998 /* !X && X is always false. */
11999 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12000 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12001 return omit_one_operand (type, integer_zero_node, arg1);
12002 /* X && !X is always false. */
12003 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12004 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12005 return omit_one_operand (type, integer_zero_node, arg0);
12007 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
12008 means A >= Y && A != MAX, but in this case we know that
12011 if (!TREE_SIDE_EFFECTS (arg0)
12012 && !TREE_SIDE_EFFECTS (arg1))
12014 tem = fold_to_nonsharp_ineq_using_bound (arg0, arg1);
12015 if (tem && !operand_equal_p (tem, arg0, 0))
12016 return fold_build2 (code, type, tem, arg1);
12018 tem = fold_to_nonsharp_ineq_using_bound (arg1, arg0);
12019 if (tem && !operand_equal_p (tem, arg1, 0))
12020 return fold_build2 (code, type, arg0, tem);
12024 /* We only do these simplifications if we are optimizing. */
12028 /* Check for things like (A || B) && (A || C). We can convert this
12029 to A || (B && C). Note that either operator can be any of the four
12030 truth and/or operations and the transformation will still be
12031 valid. Also note that we only care about order for the
12032 ANDIF and ORIF operators. If B contains side effects, this
12033 might change the truth-value of A. */
12034 if (TREE_CODE (arg0) == TREE_CODE (arg1)
12035 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
12036 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
12037 || TREE_CODE (arg0) == TRUTH_AND_EXPR
12038 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
12039 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
12041 tree a00 = TREE_OPERAND (arg0, 0);
12042 tree a01 = TREE_OPERAND (arg0, 1);
12043 tree a10 = TREE_OPERAND (arg1, 0);
12044 tree a11 = TREE_OPERAND (arg1, 1);
12045 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
12046 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
12047 && (code == TRUTH_AND_EXPR
12048 || code == TRUTH_OR_EXPR));
12050 if (operand_equal_p (a00, a10, 0))
12051 return fold_build2 (TREE_CODE (arg0), type, a00,
12052 fold_build2 (code, type, a01, a11));
12053 else if (commutative && operand_equal_p (a00, a11, 0))
12054 return fold_build2 (TREE_CODE (arg0), type, a00,
12055 fold_build2 (code, type, a01, a10));
12056 else if (commutative && operand_equal_p (a01, a10, 0))
12057 return fold_build2 (TREE_CODE (arg0), type, a01,
12058 fold_build2 (code, type, a00, a11));
12060 /* This case if tricky because we must either have commutative
12061 operators or else A10 must not have side-effects. */
12063 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
12064 && operand_equal_p (a01, a11, 0))
12065 return fold_build2 (TREE_CODE (arg0), type,
12066 fold_build2 (code, type, a00, a10),
12070 /* See if we can build a range comparison. */
12071 if (0 != (tem = fold_range_test (code, type, op0, op1)))
12074 /* Check for the possibility of merging component references. If our
12075 lhs is another similar operation, try to merge its rhs with our
12076 rhs. Then try to merge our lhs and rhs. */
12077 if (TREE_CODE (arg0) == code
12078 && 0 != (tem = fold_truthop (code, type,
12079 TREE_OPERAND (arg0, 1), arg1)))
12080 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
12082 if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
12087 case TRUTH_ORIF_EXPR:
12088 /* Note that the operands of this must be ints
12089 and their values must be 0 or true.
12090 ("true" is a fixed value perhaps depending on the language.) */
12091 /* If first arg is constant true, return it. */
12092 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12093 return fold_convert (type, arg0);
12094 case TRUTH_OR_EXPR:
12095 /* If either arg is constant zero, drop it. */
12096 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12097 return non_lvalue (fold_convert (type, arg1));
12098 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12099 /* Preserve sequence points. */
12100 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12101 return non_lvalue (fold_convert (type, arg0));
12102 /* If second arg is constant true, result is true, but we must
12103 evaluate first arg. */
12104 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12105 return omit_one_operand (type, arg1, arg0);
12106 /* Likewise for first arg, but note this only occurs here for
12108 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12109 return omit_one_operand (type, arg0, arg1);
12111 /* !X || X is always true. */
12112 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12113 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12114 return omit_one_operand (type, integer_one_node, arg1);
12115 /* X || !X is always true. */
12116 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12117 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12118 return omit_one_operand (type, integer_one_node, arg0);
12122 case TRUTH_XOR_EXPR:
12123 /* If the second arg is constant zero, drop it. */
12124 if (integer_zerop (arg1))
12125 return non_lvalue (fold_convert (type, arg0));
12126 /* If the second arg is constant true, this is a logical inversion. */
12127 if (integer_onep (arg1))
12129 /* Only call invert_truthvalue if operand is a truth value. */
12130 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
12131 tem = fold_build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
12133 tem = invert_truthvalue (arg0);
12134 return non_lvalue (fold_convert (type, tem));
12136 /* Identical arguments cancel to zero. */
12137 if (operand_equal_p (arg0, arg1, 0))
12138 return omit_one_operand (type, integer_zero_node, arg0);
12140 /* !X ^ X is always true. */
12141 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12142 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12143 return omit_one_operand (type, integer_one_node, arg1);
12145 /* X ^ !X is always true. */
12146 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12147 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12148 return omit_one_operand (type, integer_one_node, arg0);
12154 tem = fold_comparison (code, type, op0, op1);
12155 if (tem != NULL_TREE)
12158 /* bool_var != 0 becomes bool_var. */
12159 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12160 && code == NE_EXPR)
12161 return non_lvalue (fold_convert (type, arg0));
12163 /* bool_var == 1 becomes bool_var. */
12164 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12165 && code == EQ_EXPR)
12166 return non_lvalue (fold_convert (type, arg0));
12168 /* bool_var != 1 becomes !bool_var. */
12169 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12170 && code == NE_EXPR)
12171 return fold_build1 (TRUTH_NOT_EXPR, type, fold_convert (type, arg0));
12173 /* bool_var == 0 becomes !bool_var. */
12174 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12175 && code == EQ_EXPR)
12176 return fold_build1 (TRUTH_NOT_EXPR, type, fold_convert (type, arg0));
12178 /* If this is an equality comparison of the address of two non-weak,
12179 unaliased symbols neither of which are extern (since we do not
12180 have access to attributes for externs), then we know the result. */
12181 if (TREE_CODE (arg0) == ADDR_EXPR
12182 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
12183 && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
12184 && ! lookup_attribute ("alias",
12185 DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
12186 && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
12187 && TREE_CODE (arg1) == ADDR_EXPR
12188 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
12189 && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
12190 && ! lookup_attribute ("alias",
12191 DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
12192 && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
12194 /* We know that we're looking at the address of two
12195 non-weak, unaliased, static _DECL nodes.
12197 It is both wasteful and incorrect to call operand_equal_p
12198 to compare the two ADDR_EXPR nodes. It is wasteful in that
12199 all we need to do is test pointer equality for the arguments
12200 to the two ADDR_EXPR nodes. It is incorrect to use
12201 operand_equal_p as that function is NOT equivalent to a
12202 C equality test. It can in fact return false for two
12203 objects which would test as equal using the C equality
12205 bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
12206 return constant_boolean_node (equal
12207 ? code == EQ_EXPR : code != EQ_EXPR,
12211 /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
12212 a MINUS_EXPR of a constant, we can convert it into a comparison with
12213 a revised constant as long as no overflow occurs. */
12214 if (TREE_CODE (arg1) == INTEGER_CST
12215 && (TREE_CODE (arg0) == PLUS_EXPR
12216 || TREE_CODE (arg0) == MINUS_EXPR)
12217 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12218 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
12219 ? MINUS_EXPR : PLUS_EXPR,
12220 fold_convert (TREE_TYPE (arg0), arg1),
12221 TREE_OPERAND (arg0, 1), 0))
12222 && !TREE_OVERFLOW (tem))
12223 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
12225 /* Similarly for a NEGATE_EXPR. */
12226 if (TREE_CODE (arg0) == NEGATE_EXPR
12227 && TREE_CODE (arg1) == INTEGER_CST
12228 && 0 != (tem = negate_expr (arg1))
12229 && TREE_CODE (tem) == INTEGER_CST
12230 && !TREE_OVERFLOW (tem))
12231 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
12233 /* Similarly for a BIT_XOR_EXPR; X ^ C1 == C2 is X == (C1 ^ C2). */
12234 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12235 && TREE_CODE (arg1) == INTEGER_CST
12236 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12237 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12238 fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg0),
12239 fold_convert (TREE_TYPE (arg0), arg1),
12240 TREE_OPERAND (arg0, 1)));
12242 /* Transform comparisons of the form X +- C CMP X. */
12243 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12244 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12245 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12246 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12247 || POINTER_TYPE_P (TREE_TYPE (arg0))))
12249 tree cst = TREE_OPERAND (arg0, 1);
12251 if (code == EQ_EXPR
12252 && !integer_zerop (cst))
12253 return omit_two_operands (type, boolean_false_node,
12254 TREE_OPERAND (arg0, 0), arg1);
12256 return omit_two_operands (type, boolean_true_node,
12257 TREE_OPERAND (arg0, 0), arg1);
12260 /* If we have X - Y == 0, we can convert that to X == Y and similarly
12261 for !=. Don't do this for ordered comparisons due to overflow. */
12262 if (TREE_CODE (arg0) == MINUS_EXPR
12263 && integer_zerop (arg1))
12264 return fold_build2 (code, type,
12265 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
12267 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
12268 if (TREE_CODE (arg0) == ABS_EXPR
12269 && (integer_zerop (arg1) || real_zerop (arg1)))
12270 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), arg1);
12272 /* If this is an EQ or NE comparison with zero and ARG0 is
12273 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12274 two operations, but the latter can be done in one less insn
12275 on machines that have only two-operand insns or on which a
12276 constant cannot be the first operand. */
12277 if (TREE_CODE (arg0) == BIT_AND_EXPR
12278 && integer_zerop (arg1))
12280 tree arg00 = TREE_OPERAND (arg0, 0);
12281 tree arg01 = TREE_OPERAND (arg0, 1);
12282 if (TREE_CODE (arg00) == LSHIFT_EXPR
12283 && integer_onep (TREE_OPERAND (arg00, 0)))
12285 tree tem = fold_build2 (RSHIFT_EXPR, TREE_TYPE (arg00),
12286 arg01, TREE_OPERAND (arg00, 1));
12287 tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12288 build_int_cst (TREE_TYPE (arg0), 1));
12289 return fold_build2 (code, type,
12290 fold_convert (TREE_TYPE (arg1), tem), arg1);
12292 else if (TREE_CODE (arg01) == LSHIFT_EXPR
12293 && integer_onep (TREE_OPERAND (arg01, 0)))
12295 tree tem = fold_build2 (RSHIFT_EXPR, TREE_TYPE (arg01),
12296 arg00, TREE_OPERAND (arg01, 1));
12297 tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12298 build_int_cst (TREE_TYPE (arg0), 1));
12299 return fold_build2 (code, type,
12300 fold_convert (TREE_TYPE (arg1), tem), arg1);
12304 /* If this is an NE or EQ comparison of zero against the result of a
12305 signed MOD operation whose second operand is a power of 2, make
12306 the MOD operation unsigned since it is simpler and equivalent. */
12307 if (integer_zerop (arg1)
12308 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
12309 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
12310 || TREE_CODE (arg0) == CEIL_MOD_EXPR
12311 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
12312 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
12313 && integer_pow2p (TREE_OPERAND (arg0, 1)))
12315 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
12316 tree newmod = fold_build2 (TREE_CODE (arg0), newtype,
12317 fold_convert (newtype,
12318 TREE_OPERAND (arg0, 0)),
12319 fold_convert (newtype,
12320 TREE_OPERAND (arg0, 1)));
12322 return fold_build2 (code, type, newmod,
12323 fold_convert (newtype, arg1));
12326 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
12327 C1 is a valid shift constant, and C2 is a power of two, i.e.
12329 if (TREE_CODE (arg0) == BIT_AND_EXPR
12330 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
12331 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
12333 && integer_pow2p (TREE_OPERAND (arg0, 1))
12334 && integer_zerop (arg1))
12336 tree itype = TREE_TYPE (arg0);
12337 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
12338 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
12340 /* Check for a valid shift count. */
12341 if (TREE_INT_CST_HIGH (arg001) == 0
12342 && TREE_INT_CST_LOW (arg001) < prec)
12344 tree arg01 = TREE_OPERAND (arg0, 1);
12345 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12346 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
12347 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
12348 can be rewritten as (X & (C2 << C1)) != 0. */
12349 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
12351 tem = fold_build2 (LSHIFT_EXPR, itype, arg01, arg001);
12352 tem = fold_build2 (BIT_AND_EXPR, itype, arg000, tem);
12353 return fold_build2 (code, type, tem, arg1);
12355 /* Otherwise, for signed (arithmetic) shifts,
12356 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
12357 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
12358 else if (!TYPE_UNSIGNED (itype))
12359 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
12360 arg000, build_int_cst (itype, 0));
12361 /* Otherwise, of unsigned (logical) shifts,
12362 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
12363 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
12365 return omit_one_operand (type,
12366 code == EQ_EXPR ? integer_one_node
12367 : integer_zero_node,
12372 /* If this is an NE comparison of zero with an AND of one, remove the
12373 comparison since the AND will give the correct value. */
12374 if (code == NE_EXPR
12375 && integer_zerop (arg1)
12376 && TREE_CODE (arg0) == BIT_AND_EXPR
12377 && integer_onep (TREE_OPERAND (arg0, 1)))
12378 return fold_convert (type, arg0);
12380 /* If we have (A & C) == C where C is a power of 2, convert this into
12381 (A & C) != 0. Similarly for NE_EXPR. */
12382 if (TREE_CODE (arg0) == BIT_AND_EXPR
12383 && integer_pow2p (TREE_OPERAND (arg0, 1))
12384 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12385 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12386 arg0, fold_convert (TREE_TYPE (arg0),
12387 integer_zero_node));
12389 /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
12390 bit, then fold the expression into A < 0 or A >= 0. */
12391 tem = fold_single_bit_test_into_sign_test (code, arg0, arg1, type);
12395 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
12396 Similarly for NE_EXPR. */
12397 if (TREE_CODE (arg0) == BIT_AND_EXPR
12398 && TREE_CODE (arg1) == INTEGER_CST
12399 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12401 tree notc = fold_build1 (BIT_NOT_EXPR,
12402 TREE_TYPE (TREE_OPERAND (arg0, 1)),
12403 TREE_OPERAND (arg0, 1));
12404 tree dandnotc = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12406 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12407 if (integer_nonzerop (dandnotc))
12408 return omit_one_operand (type, rslt, arg0);
12411 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
12412 Similarly for NE_EXPR. */
12413 if (TREE_CODE (arg0) == BIT_IOR_EXPR
12414 && TREE_CODE (arg1) == INTEGER_CST
12415 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12417 tree notd = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
12418 tree candnotd = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12419 TREE_OPERAND (arg0, 1), notd);
12420 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12421 if (integer_nonzerop (candnotd))
12422 return omit_one_operand (type, rslt, arg0);
12425 /* If this is a comparison of a field, we may be able to simplify it. */
12426 if ((TREE_CODE (arg0) == COMPONENT_REF
12427 || TREE_CODE (arg0) == BIT_FIELD_REF)
12428 /* Handle the constant case even without -O
12429 to make sure the warnings are given. */
12430 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12432 t1 = optimize_bit_field_compare (code, type, arg0, arg1);
12437 /* Optimize comparisons of strlen vs zero to a compare of the
12438 first character of the string vs zero. To wit,
12439 strlen(ptr) == 0 => *ptr == 0
12440 strlen(ptr) != 0 => *ptr != 0
12441 Other cases should reduce to one of these two (or a constant)
12442 due to the return value of strlen being unsigned. */
12443 if (TREE_CODE (arg0) == CALL_EXPR
12444 && integer_zerop (arg1))
12446 tree fndecl = get_callee_fndecl (arg0);
12449 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
12450 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
12451 && call_expr_nargs (arg0) == 1
12452 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
12454 tree iref = build_fold_indirect_ref (CALL_EXPR_ARG (arg0, 0));
12455 return fold_build2 (code, type, iref,
12456 build_int_cst (TREE_TYPE (iref), 0));
12460 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12461 of X. Similarly fold (X >> C) == 0 into X >= 0. */
12462 if (TREE_CODE (arg0) == RSHIFT_EXPR
12463 && integer_zerop (arg1)
12464 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12466 tree arg00 = TREE_OPERAND (arg0, 0);
12467 tree arg01 = TREE_OPERAND (arg0, 1);
12468 tree itype = TREE_TYPE (arg00);
12469 if (TREE_INT_CST_HIGH (arg01) == 0
12470 && TREE_INT_CST_LOW (arg01)
12471 == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
12473 if (TYPE_UNSIGNED (itype))
12475 itype = signed_type_for (itype);
12476 arg00 = fold_convert (itype, arg00);
12478 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12479 type, arg00, build_int_cst (itype, 0));
12483 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
12484 if (integer_zerop (arg1)
12485 && TREE_CODE (arg0) == BIT_XOR_EXPR)
12486 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12487 TREE_OPERAND (arg0, 1));
12489 /* (X ^ Y) == Y becomes X == 0. We know that Y has no side-effects. */
12490 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12491 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12492 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12493 build_int_cst (TREE_TYPE (arg1), 0));
12494 /* Likewise (X ^ Y) == X becomes Y == 0. X has no side-effects. */
12495 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12496 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12497 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
12498 return fold_build2 (code, type, TREE_OPERAND (arg0, 1),
12499 build_int_cst (TREE_TYPE (arg1), 0));
12501 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
12502 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12503 && TREE_CODE (arg1) == INTEGER_CST
12504 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12505 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12506 fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg1),
12507 TREE_OPERAND (arg0, 1), arg1));
12509 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12510 (X & C) == 0 when C is a single bit. */
12511 if (TREE_CODE (arg0) == BIT_AND_EXPR
12512 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12513 && integer_zerop (arg1)
12514 && integer_pow2p (TREE_OPERAND (arg0, 1)))
12516 tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12517 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12518 TREE_OPERAND (arg0, 1));
12519 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12523 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12524 constant C is a power of two, i.e. a single bit. */
12525 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12526 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12527 && integer_zerop (arg1)
12528 && integer_pow2p (TREE_OPERAND (arg0, 1))
12529 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12530 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12532 tree arg00 = TREE_OPERAND (arg0, 0);
12533 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12534 arg00, build_int_cst (TREE_TYPE (arg00), 0));
12537 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12538 when is C is a power of two, i.e. a single bit. */
12539 if (TREE_CODE (arg0) == BIT_AND_EXPR
12540 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12541 && integer_zerop (arg1)
12542 && integer_pow2p (TREE_OPERAND (arg0, 1))
12543 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12544 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12546 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12547 tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg000),
12548 arg000, TREE_OPERAND (arg0, 1));
12549 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12550 tem, build_int_cst (TREE_TYPE (tem), 0));
12553 if (integer_zerop (arg1)
12554 && tree_expr_nonzero_p (arg0))
12556 tree res = constant_boolean_node (code==NE_EXPR, type);
12557 return omit_one_operand (type, res, arg0);
12560 /* Fold -X op -Y as X op Y, where op is eq/ne. */
12561 if (TREE_CODE (arg0) == NEGATE_EXPR
12562 && TREE_CODE (arg1) == NEGATE_EXPR)
12563 return fold_build2 (code, type,
12564 TREE_OPERAND (arg0, 0),
12565 TREE_OPERAND (arg1, 0));
12567 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
12568 if (TREE_CODE (arg0) == BIT_AND_EXPR
12569 && TREE_CODE (arg1) == BIT_AND_EXPR)
12571 tree arg00 = TREE_OPERAND (arg0, 0);
12572 tree arg01 = TREE_OPERAND (arg0, 1);
12573 tree arg10 = TREE_OPERAND (arg1, 0);
12574 tree arg11 = TREE_OPERAND (arg1, 1);
12575 tree itype = TREE_TYPE (arg0);
12577 if (operand_equal_p (arg01, arg11, 0))
12578 return fold_build2 (code, type,
12579 fold_build2 (BIT_AND_EXPR, itype,
12580 fold_build2 (BIT_XOR_EXPR, itype,
12583 build_int_cst (itype, 0));
12585 if (operand_equal_p (arg01, arg10, 0))
12586 return fold_build2 (code, type,
12587 fold_build2 (BIT_AND_EXPR, itype,
12588 fold_build2 (BIT_XOR_EXPR, itype,
12591 build_int_cst (itype, 0));
12593 if (operand_equal_p (arg00, arg11, 0))
12594 return fold_build2 (code, type,
12595 fold_build2 (BIT_AND_EXPR, itype,
12596 fold_build2 (BIT_XOR_EXPR, itype,
12599 build_int_cst (itype, 0));
12601 if (operand_equal_p (arg00, arg10, 0))
12602 return fold_build2 (code, type,
12603 fold_build2 (BIT_AND_EXPR, itype,
12604 fold_build2 (BIT_XOR_EXPR, itype,
12607 build_int_cst (itype, 0));
12610 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12611 && TREE_CODE (arg1) == BIT_XOR_EXPR)
12613 tree arg00 = TREE_OPERAND (arg0, 0);
12614 tree arg01 = TREE_OPERAND (arg0, 1);
12615 tree arg10 = TREE_OPERAND (arg1, 0);
12616 tree arg11 = TREE_OPERAND (arg1, 1);
12617 tree itype = TREE_TYPE (arg0);
12619 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12620 operand_equal_p guarantees no side-effects so we don't need
12621 to use omit_one_operand on Z. */
12622 if (operand_equal_p (arg01, arg11, 0))
12623 return fold_build2 (code, type, arg00, arg10);
12624 if (operand_equal_p (arg01, arg10, 0))
12625 return fold_build2 (code, type, arg00, arg11);
12626 if (operand_equal_p (arg00, arg11, 0))
12627 return fold_build2 (code, type, arg01, arg10);
12628 if (operand_equal_p (arg00, arg10, 0))
12629 return fold_build2 (code, type, arg01, arg11);
12631 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12632 if (TREE_CODE (arg01) == INTEGER_CST
12633 && TREE_CODE (arg11) == INTEGER_CST)
12634 return fold_build2 (code, type,
12635 fold_build2 (BIT_XOR_EXPR, itype, arg00,
12636 fold_build2 (BIT_XOR_EXPR, itype,
12641 /* Attempt to simplify equality/inequality comparisons of complex
12642 values. Only lower the comparison if the result is known or
12643 can be simplified to a single scalar comparison. */
12644 if ((TREE_CODE (arg0) == COMPLEX_EXPR
12645 || TREE_CODE (arg0) == COMPLEX_CST)
12646 && (TREE_CODE (arg1) == COMPLEX_EXPR
12647 || TREE_CODE (arg1) == COMPLEX_CST))
12649 tree real0, imag0, real1, imag1;
12652 if (TREE_CODE (arg0) == COMPLEX_EXPR)
12654 real0 = TREE_OPERAND (arg0, 0);
12655 imag0 = TREE_OPERAND (arg0, 1);
12659 real0 = TREE_REALPART (arg0);
12660 imag0 = TREE_IMAGPART (arg0);
12663 if (TREE_CODE (arg1) == COMPLEX_EXPR)
12665 real1 = TREE_OPERAND (arg1, 0);
12666 imag1 = TREE_OPERAND (arg1, 1);
12670 real1 = TREE_REALPART (arg1);
12671 imag1 = TREE_IMAGPART (arg1);
12674 rcond = fold_binary (code, type, real0, real1);
12675 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12677 if (integer_zerop (rcond))
12679 if (code == EQ_EXPR)
12680 return omit_two_operands (type, boolean_false_node,
12682 return fold_build2 (NE_EXPR, type, imag0, imag1);
12686 if (code == NE_EXPR)
12687 return omit_two_operands (type, boolean_true_node,
12689 return fold_build2 (EQ_EXPR, type, imag0, imag1);
12693 icond = fold_binary (code, type, imag0, imag1);
12694 if (icond && TREE_CODE (icond) == INTEGER_CST)
12696 if (integer_zerop (icond))
12698 if (code == EQ_EXPR)
12699 return omit_two_operands (type, boolean_false_node,
12701 return fold_build2 (NE_EXPR, type, real0, real1);
12705 if (code == NE_EXPR)
12706 return omit_two_operands (type, boolean_true_node,
12708 return fold_build2 (EQ_EXPR, type, real0, real1);
12719 tem = fold_comparison (code, type, op0, op1);
12720 if (tem != NULL_TREE)
12723 /* Transform comparisons of the form X +- C CMP X. */
12724 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12725 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12726 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12727 && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
12728 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12729 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
12731 tree arg01 = TREE_OPERAND (arg0, 1);
12732 enum tree_code code0 = TREE_CODE (arg0);
12735 if (TREE_CODE (arg01) == REAL_CST)
12736 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12738 is_positive = tree_int_cst_sgn (arg01);
12740 /* (X - c) > X becomes false. */
12741 if (code == GT_EXPR
12742 && ((code0 == MINUS_EXPR && is_positive >= 0)
12743 || (code0 == PLUS_EXPR && is_positive <= 0)))
12745 if (TREE_CODE (arg01) == INTEGER_CST
12746 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12747 fold_overflow_warning (("assuming signed overflow does not "
12748 "occur when assuming that (X - c) > X "
12749 "is always false"),
12750 WARN_STRICT_OVERFLOW_ALL);
12751 return constant_boolean_node (0, type);
12754 /* Likewise (X + c) < X becomes false. */
12755 if (code == LT_EXPR
12756 && ((code0 == PLUS_EXPR && is_positive >= 0)
12757 || (code0 == MINUS_EXPR && is_positive <= 0)))
12759 if (TREE_CODE (arg01) == INTEGER_CST
12760 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12761 fold_overflow_warning (("assuming signed overflow does not "
12762 "occur when assuming that "
12763 "(X + c) < X is always false"),
12764 WARN_STRICT_OVERFLOW_ALL);
12765 return constant_boolean_node (0, type);
12768 /* Convert (X - c) <= X to true. */
12769 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12771 && ((code0 == MINUS_EXPR && is_positive >= 0)
12772 || (code0 == PLUS_EXPR && is_positive <= 0)))
12774 if (TREE_CODE (arg01) == INTEGER_CST
12775 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12776 fold_overflow_warning (("assuming signed overflow does not "
12777 "occur when assuming that "
12778 "(X - c) <= X is always true"),
12779 WARN_STRICT_OVERFLOW_ALL);
12780 return constant_boolean_node (1, type);
12783 /* Convert (X + c) >= X to true. */
12784 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12786 && ((code0 == PLUS_EXPR && is_positive >= 0)
12787 || (code0 == MINUS_EXPR && is_positive <= 0)))
12789 if (TREE_CODE (arg01) == INTEGER_CST
12790 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12791 fold_overflow_warning (("assuming signed overflow does not "
12792 "occur when assuming that "
12793 "(X + c) >= X is always true"),
12794 WARN_STRICT_OVERFLOW_ALL);
12795 return constant_boolean_node (1, type);
12798 if (TREE_CODE (arg01) == INTEGER_CST)
12800 /* Convert X + c > X and X - c < X to true for integers. */
12801 if (code == GT_EXPR
12802 && ((code0 == PLUS_EXPR && is_positive > 0)
12803 || (code0 == MINUS_EXPR && is_positive < 0)))
12805 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12806 fold_overflow_warning (("assuming signed overflow does "
12807 "not occur when assuming that "
12808 "(X + c) > X is always true"),
12809 WARN_STRICT_OVERFLOW_ALL);
12810 return constant_boolean_node (1, type);
12813 if (code == LT_EXPR
12814 && ((code0 == MINUS_EXPR && is_positive > 0)
12815 || (code0 == PLUS_EXPR && is_positive < 0)))
12817 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12818 fold_overflow_warning (("assuming signed overflow does "
12819 "not occur when assuming that "
12820 "(X - c) < X is always true"),
12821 WARN_STRICT_OVERFLOW_ALL);
12822 return constant_boolean_node (1, type);
12825 /* Convert X + c <= X and X - c >= X to false for integers. */
12826 if (code == LE_EXPR
12827 && ((code0 == PLUS_EXPR && is_positive > 0)
12828 || (code0 == MINUS_EXPR && is_positive < 0)))
12830 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12831 fold_overflow_warning (("assuming signed overflow does "
12832 "not occur when assuming that "
12833 "(X + c) <= X is always false"),
12834 WARN_STRICT_OVERFLOW_ALL);
12835 return constant_boolean_node (0, type);
12838 if (code == GE_EXPR
12839 && ((code0 == MINUS_EXPR && is_positive > 0)
12840 || (code0 == PLUS_EXPR && is_positive < 0)))
12842 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12843 fold_overflow_warning (("assuming signed overflow does "
12844 "not occur when assuming that "
12845 "(X - c) >= X is always false"),
12846 WARN_STRICT_OVERFLOW_ALL);
12847 return constant_boolean_node (0, type);
12852 /* Comparisons with the highest or lowest possible integer of
12853 the specified precision will have known values. */
12855 tree arg1_type = TREE_TYPE (arg1);
12856 unsigned int width = TYPE_PRECISION (arg1_type);
12858 if (TREE_CODE (arg1) == INTEGER_CST
12859 && width <= 2 * HOST_BITS_PER_WIDE_INT
12860 && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
12862 HOST_WIDE_INT signed_max_hi;
12863 unsigned HOST_WIDE_INT signed_max_lo;
12864 unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
12866 if (width <= HOST_BITS_PER_WIDE_INT)
12868 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12873 if (TYPE_UNSIGNED (arg1_type))
12875 max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12881 max_lo = signed_max_lo;
12882 min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12888 width -= HOST_BITS_PER_WIDE_INT;
12889 signed_max_lo = -1;
12890 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12895 if (TYPE_UNSIGNED (arg1_type))
12897 max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12902 max_hi = signed_max_hi;
12903 min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12907 if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
12908 && TREE_INT_CST_LOW (arg1) == max_lo)
12912 return omit_one_operand (type, integer_zero_node, arg0);
12915 return fold_build2 (EQ_EXPR, type, op0, op1);
12918 return omit_one_operand (type, integer_one_node, arg0);
12921 return fold_build2 (NE_EXPR, type, op0, op1);
12923 /* The GE_EXPR and LT_EXPR cases above are not normally
12924 reached because of previous transformations. */
12929 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12931 && TREE_INT_CST_LOW (arg1) == max_lo - 1)
12935 arg1 = const_binop (PLUS_EXPR, arg1,
12936 build_int_cst (TREE_TYPE (arg1), 1), 0);
12937 return fold_build2 (EQ_EXPR, type,
12938 fold_convert (TREE_TYPE (arg1), arg0),
12941 arg1 = const_binop (PLUS_EXPR, arg1,
12942 build_int_cst (TREE_TYPE (arg1), 1), 0);
12943 return fold_build2 (NE_EXPR, type,
12944 fold_convert (TREE_TYPE (arg1), arg0),
12949 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12951 && TREE_INT_CST_LOW (arg1) == min_lo)
12955 return omit_one_operand (type, integer_zero_node, arg0);
12958 return fold_build2 (EQ_EXPR, type, op0, op1);
12961 return omit_one_operand (type, integer_one_node, arg0);
12964 return fold_build2 (NE_EXPR, type, op0, op1);
12969 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12971 && TREE_INT_CST_LOW (arg1) == min_lo + 1)
12975 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
12976 return fold_build2 (NE_EXPR, type,
12977 fold_convert (TREE_TYPE (arg1), arg0),
12980 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
12981 return fold_build2 (EQ_EXPR, type,
12982 fold_convert (TREE_TYPE (arg1), arg0),
12988 else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
12989 && TREE_INT_CST_LOW (arg1) == signed_max_lo
12990 && TYPE_UNSIGNED (arg1_type)
12991 /* We will flip the signedness of the comparison operator
12992 associated with the mode of arg1, so the sign bit is
12993 specified by this mode. Check that arg1 is the signed
12994 max associated with this sign bit. */
12995 && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
12996 /* signed_type does not work on pointer types. */
12997 && INTEGRAL_TYPE_P (arg1_type))
12999 /* The following case also applies to X < signed_max+1
13000 and X >= signed_max+1 because previous transformations. */
13001 if (code == LE_EXPR || code == GT_EXPR)
13004 st = signed_type_for (TREE_TYPE (arg1));
13005 return fold_build2 (code == LE_EXPR ? GE_EXPR : LT_EXPR,
13006 type, fold_convert (st, arg0),
13007 build_int_cst (st, 0));
13013 /* If we are comparing an ABS_EXPR with a constant, we can
13014 convert all the cases into explicit comparisons, but they may
13015 well not be faster than doing the ABS and one comparison.
13016 But ABS (X) <= C is a range comparison, which becomes a subtraction
13017 and a comparison, and is probably faster. */
13018 if (code == LE_EXPR
13019 && TREE_CODE (arg1) == INTEGER_CST
13020 && TREE_CODE (arg0) == ABS_EXPR
13021 && ! TREE_SIDE_EFFECTS (arg0)
13022 && (0 != (tem = negate_expr (arg1)))
13023 && TREE_CODE (tem) == INTEGER_CST
13024 && !TREE_OVERFLOW (tem))
13025 return fold_build2 (TRUTH_ANDIF_EXPR, type,
13026 build2 (GE_EXPR, type,
13027 TREE_OPERAND (arg0, 0), tem),
13028 build2 (LE_EXPR, type,
13029 TREE_OPERAND (arg0, 0), arg1));
13031 /* Convert ABS_EXPR<x> >= 0 to true. */
13032 strict_overflow_p = false;
13033 if (code == GE_EXPR
13034 && (integer_zerop (arg1)
13035 || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
13036 && real_zerop (arg1)))
13037 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13039 if (strict_overflow_p)
13040 fold_overflow_warning (("assuming signed overflow does not occur "
13041 "when simplifying comparison of "
13042 "absolute value and zero"),
13043 WARN_STRICT_OVERFLOW_CONDITIONAL);
13044 return omit_one_operand (type, integer_one_node, arg0);
13047 /* Convert ABS_EXPR<x> < 0 to false. */
13048 strict_overflow_p = false;
13049 if (code == LT_EXPR
13050 && (integer_zerop (arg1) || real_zerop (arg1))
13051 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13053 if (strict_overflow_p)
13054 fold_overflow_warning (("assuming signed overflow does not occur "
13055 "when simplifying comparison of "
13056 "absolute value and zero"),
13057 WARN_STRICT_OVERFLOW_CONDITIONAL);
13058 return omit_one_operand (type, integer_zero_node, arg0);
13061 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
13062 and similarly for >= into !=. */
13063 if ((code == LT_EXPR || code == GE_EXPR)
13064 && TYPE_UNSIGNED (TREE_TYPE (arg0))
13065 && TREE_CODE (arg1) == LSHIFT_EXPR
13066 && integer_onep (TREE_OPERAND (arg1, 0)))
13067 return build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13068 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13069 TREE_OPERAND (arg1, 1)),
13070 build_int_cst (TREE_TYPE (arg0), 0));
13072 if ((code == LT_EXPR || code == GE_EXPR)
13073 && TYPE_UNSIGNED (TREE_TYPE (arg0))
13074 && CONVERT_EXPR_P (arg1)
13075 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
13076 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
13078 build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13079 fold_convert (TREE_TYPE (arg0),
13080 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13081 TREE_OPERAND (TREE_OPERAND (arg1, 0),
13083 build_int_cst (TREE_TYPE (arg0), 0));
13087 case UNORDERED_EXPR:
13095 if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
13097 t1 = fold_relational_const (code, type, arg0, arg1);
13098 if (t1 != NULL_TREE)
13102 /* If the first operand is NaN, the result is constant. */
13103 if (TREE_CODE (arg0) == REAL_CST
13104 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
13105 && (code != LTGT_EXPR || ! flag_trapping_math))
13107 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13108 ? integer_zero_node
13109 : integer_one_node;
13110 return omit_one_operand (type, t1, arg1);
13113 /* If the second operand is NaN, the result is constant. */
13114 if (TREE_CODE (arg1) == REAL_CST
13115 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
13116 && (code != LTGT_EXPR || ! flag_trapping_math))
13118 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13119 ? integer_zero_node
13120 : integer_one_node;
13121 return omit_one_operand (type, t1, arg0);
13124 /* Simplify unordered comparison of something with itself. */
13125 if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
13126 && operand_equal_p (arg0, arg1, 0))
13127 return constant_boolean_node (1, type);
13129 if (code == LTGT_EXPR
13130 && !flag_trapping_math
13131 && operand_equal_p (arg0, arg1, 0))
13132 return constant_boolean_node (0, type);
13134 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
13136 tree targ0 = strip_float_extensions (arg0);
13137 tree targ1 = strip_float_extensions (arg1);
13138 tree newtype = TREE_TYPE (targ0);
13140 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
13141 newtype = TREE_TYPE (targ1);
13143 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
13144 return fold_build2 (code, type, fold_convert (newtype, targ0),
13145 fold_convert (newtype, targ1));
13150 case COMPOUND_EXPR:
13151 /* When pedantic, a compound expression can be neither an lvalue
13152 nor an integer constant expression. */
13153 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
13155 /* Don't let (0, 0) be null pointer constant. */
13156 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
13157 : fold_convert (type, arg1);
13158 return pedantic_non_lvalue (tem);
13161 if ((TREE_CODE (arg0) == REAL_CST
13162 && TREE_CODE (arg1) == REAL_CST)
13163 || (TREE_CODE (arg0) == INTEGER_CST
13164 && TREE_CODE (arg1) == INTEGER_CST))
13165 return build_complex (type, arg0, arg1);
13169 /* An ASSERT_EXPR should never be passed to fold_binary. */
13170 gcc_unreachable ();
13174 } /* switch (code) */
13177 /* Callback for walk_tree, looking for LABEL_EXPR.
13178 Returns tree TP if it is LABEL_EXPR. Otherwise it returns NULL_TREE.
13179 Do not check the sub-tree of GOTO_EXPR. */
13182 contains_label_1 (tree *tp,
13183 int *walk_subtrees,
13184 void *data ATTRIBUTE_UNUSED)
13186 switch (TREE_CODE (*tp))
13191 *walk_subtrees = 0;
13198 /* Checks whether the sub-tree ST contains a label LABEL_EXPR which is
13199 accessible from outside the sub-tree. Returns NULL_TREE if no
13200 addressable label is found. */
13203 contains_label_p (tree st)
13205 return (walk_tree (&st, contains_label_1 , NULL, NULL) != NULL_TREE);
13208 /* Fold a ternary expression of code CODE and type TYPE with operands
13209 OP0, OP1, and OP2. Return the folded expression if folding is
13210 successful. Otherwise, return NULL_TREE. */
13213 fold_ternary (enum tree_code code, tree type, tree op0, tree op1, tree op2)
13216 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
13217 enum tree_code_class kind = TREE_CODE_CLASS (code);
13219 gcc_assert (IS_EXPR_CODE_CLASS (kind)
13220 && TREE_CODE_LENGTH (code) == 3);
13222 /* Strip any conversions that don't change the mode. This is safe
13223 for every expression, except for a comparison expression because
13224 its signedness is derived from its operands. So, in the latter
13225 case, only strip conversions that don't change the signedness.
13227 Note that this is done as an internal manipulation within the
13228 constant folder, in order to find the simplest representation of
13229 the arguments so that their form can be studied. In any cases,
13230 the appropriate type conversions should be put back in the tree
13231 that will get out of the constant folder. */
13246 case COMPONENT_REF:
13247 if (TREE_CODE (arg0) == CONSTRUCTOR
13248 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
13250 unsigned HOST_WIDE_INT idx;
13252 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
13259 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
13260 so all simple results must be passed through pedantic_non_lvalue. */
13261 if (TREE_CODE (arg0) == INTEGER_CST)
13263 tree unused_op = integer_zerop (arg0) ? op1 : op2;
13264 tem = integer_zerop (arg0) ? op2 : op1;
13265 /* Only optimize constant conditions when the selected branch
13266 has the same type as the COND_EXPR. This avoids optimizing
13267 away "c ? x : throw", where the throw has a void type.
13268 Avoid throwing away that operand which contains label. */
13269 if ((!TREE_SIDE_EFFECTS (unused_op)
13270 || !contains_label_p (unused_op))
13271 && (! VOID_TYPE_P (TREE_TYPE (tem))
13272 || VOID_TYPE_P (type)))
13273 return pedantic_non_lvalue (tem);
13276 if (operand_equal_p (arg1, op2, 0))
13277 return pedantic_omit_one_operand (type, arg1, arg0);
13279 /* If we have A op B ? A : C, we may be able to convert this to a
13280 simpler expression, depending on the operation and the values
13281 of B and C. Signed zeros prevent all of these transformations,
13282 for reasons given above each one.
13284 Also try swapping the arguments and inverting the conditional. */
13285 if (COMPARISON_CLASS_P (arg0)
13286 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13287 arg1, TREE_OPERAND (arg0, 1))
13288 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
13290 tem = fold_cond_expr_with_comparison (type, arg0, op1, op2);
13295 if (COMPARISON_CLASS_P (arg0)
13296 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13298 TREE_OPERAND (arg0, 1))
13299 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
13301 tem = fold_truth_not_expr (arg0);
13302 if (tem && COMPARISON_CLASS_P (tem))
13304 tem = fold_cond_expr_with_comparison (type, tem, op2, op1);
13310 /* If the second operand is simpler than the third, swap them
13311 since that produces better jump optimization results. */
13312 if (truth_value_p (TREE_CODE (arg0))
13313 && tree_swap_operands_p (op1, op2, false))
13315 /* See if this can be inverted. If it can't, possibly because
13316 it was a floating-point inequality comparison, don't do
13318 tem = fold_truth_not_expr (arg0);
13320 return fold_build3 (code, type, tem, op2, op1);
13323 /* Convert A ? 1 : 0 to simply A. */
13324 if (integer_onep (op1)
13325 && integer_zerop (op2)
13326 /* If we try to convert OP0 to our type, the
13327 call to fold will try to move the conversion inside
13328 a COND, which will recurse. In that case, the COND_EXPR
13329 is probably the best choice, so leave it alone. */
13330 && type == TREE_TYPE (arg0))
13331 return pedantic_non_lvalue (arg0);
13333 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
13334 over COND_EXPR in cases such as floating point comparisons. */
13335 if (integer_zerop (op1)
13336 && integer_onep (op2)
13337 && truth_value_p (TREE_CODE (arg0)))
13338 return pedantic_non_lvalue (fold_convert (type,
13339 invert_truthvalue (arg0)));
13341 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
13342 if (TREE_CODE (arg0) == LT_EXPR
13343 && integer_zerop (TREE_OPERAND (arg0, 1))
13344 && integer_zerop (op2)
13345 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13347 /* sign_bit_p only checks ARG1 bits within A's precision.
13348 If <sign bit of A> has wider type than A, bits outside
13349 of A's precision in <sign bit of A> need to be checked.
13350 If they are all 0, this optimization needs to be done
13351 in unsigned A's type, if they are all 1 in signed A's type,
13352 otherwise this can't be done. */
13353 if (TYPE_PRECISION (TREE_TYPE (tem))
13354 < TYPE_PRECISION (TREE_TYPE (arg1))
13355 && TYPE_PRECISION (TREE_TYPE (tem))
13356 < TYPE_PRECISION (type))
13358 unsigned HOST_WIDE_INT mask_lo;
13359 HOST_WIDE_INT mask_hi;
13360 int inner_width, outer_width;
13363 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13364 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13365 if (outer_width > TYPE_PRECISION (type))
13366 outer_width = TYPE_PRECISION (type);
13368 if (outer_width > HOST_BITS_PER_WIDE_INT)
13370 mask_hi = ((unsigned HOST_WIDE_INT) -1
13371 >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
13377 mask_lo = ((unsigned HOST_WIDE_INT) -1
13378 >> (HOST_BITS_PER_WIDE_INT - outer_width));
13380 if (inner_width > HOST_BITS_PER_WIDE_INT)
13382 mask_hi &= ~((unsigned HOST_WIDE_INT) -1
13383 >> (HOST_BITS_PER_WIDE_INT - inner_width));
13387 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
13388 >> (HOST_BITS_PER_WIDE_INT - inner_width));
13390 if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
13391 && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
13393 tem_type = signed_type_for (TREE_TYPE (tem));
13394 tem = fold_convert (tem_type, tem);
13396 else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
13397 && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
13399 tem_type = unsigned_type_for (TREE_TYPE (tem));
13400 tem = fold_convert (tem_type, tem);
13407 return fold_convert (type,
13408 fold_build2 (BIT_AND_EXPR,
13409 TREE_TYPE (tem), tem,
13410 fold_convert (TREE_TYPE (tem),
13414 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
13415 already handled above. */
13416 if (TREE_CODE (arg0) == BIT_AND_EXPR
13417 && integer_onep (TREE_OPERAND (arg0, 1))
13418 && integer_zerop (op2)
13419 && integer_pow2p (arg1))
13421 tree tem = TREE_OPERAND (arg0, 0);
13423 if (TREE_CODE (tem) == RSHIFT_EXPR
13424 && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
13425 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
13426 TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
13427 return fold_build2 (BIT_AND_EXPR, type,
13428 TREE_OPERAND (tem, 0), arg1);
13431 /* A & N ? N : 0 is simply A & N if N is a power of two. This
13432 is probably obsolete because the first operand should be a
13433 truth value (that's why we have the two cases above), but let's
13434 leave it in until we can confirm this for all front-ends. */
13435 if (integer_zerop (op2)
13436 && TREE_CODE (arg0) == NE_EXPR
13437 && integer_zerop (TREE_OPERAND (arg0, 1))
13438 && integer_pow2p (arg1)
13439 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13440 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13441 arg1, OEP_ONLY_CONST))
13442 return pedantic_non_lvalue (fold_convert (type,
13443 TREE_OPERAND (arg0, 0)));
13445 /* Convert A ? B : 0 into A && B if A and B are truth values. */
13446 if (integer_zerop (op2)
13447 && truth_value_p (TREE_CODE (arg0))
13448 && truth_value_p (TREE_CODE (arg1)))
13449 return fold_build2 (TRUTH_ANDIF_EXPR, type,
13450 fold_convert (type, arg0),
13453 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
13454 if (integer_onep (op2)
13455 && truth_value_p (TREE_CODE (arg0))
13456 && truth_value_p (TREE_CODE (arg1)))
13458 /* Only perform transformation if ARG0 is easily inverted. */
13459 tem = fold_truth_not_expr (arg0);
13461 return fold_build2 (TRUTH_ORIF_EXPR, type,
13462 fold_convert (type, tem),
13466 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13467 if (integer_zerop (arg1)
13468 && truth_value_p (TREE_CODE (arg0))
13469 && truth_value_p (TREE_CODE (op2)))
13471 /* Only perform transformation if ARG0 is easily inverted. */
13472 tem = fold_truth_not_expr (arg0);
13474 return fold_build2 (TRUTH_ANDIF_EXPR, type,
13475 fold_convert (type, tem),
13479 /* Convert A ? 1 : B into A || B if A and B are truth values. */
13480 if (integer_onep (arg1)
13481 && truth_value_p (TREE_CODE (arg0))
13482 && truth_value_p (TREE_CODE (op2)))
13483 return fold_build2 (TRUTH_ORIF_EXPR, type,
13484 fold_convert (type, arg0),
13490 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
13491 of fold_ternary on them. */
13492 gcc_unreachable ();
13494 case BIT_FIELD_REF:
13495 if ((TREE_CODE (arg0) == VECTOR_CST
13496 || (TREE_CODE (arg0) == CONSTRUCTOR && TREE_CONSTANT (arg0)))
13497 && type == TREE_TYPE (TREE_TYPE (arg0)))
13499 unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
13500 unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
13503 && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
13504 && (idx % width) == 0
13505 && (idx = idx / width)
13506 < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
13508 tree elements = NULL_TREE;
13510 if (TREE_CODE (arg0) == VECTOR_CST)
13511 elements = TREE_VECTOR_CST_ELTS (arg0);
13514 unsigned HOST_WIDE_INT idx;
13517 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg0), idx, value)
13518 elements = tree_cons (NULL_TREE, value, elements);
13520 while (idx-- > 0 && elements)
13521 elements = TREE_CHAIN (elements);
13523 return TREE_VALUE (elements);
13525 return fold_convert (type, integer_zero_node);
13529 /* A bit-field-ref that referenced the full argument can be stripped. */
13530 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
13531 && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_low_cst (arg1, 1)
13532 && integer_zerop (op2))
13533 return fold_convert (type, arg0);
13539 } /* switch (code) */
13542 /* Perform constant folding and related simplification of EXPR.
13543 The related simplifications include x*1 => x, x*0 => 0, etc.,
13544 and application of the associative law.
13545 NOP_EXPR conversions may be removed freely (as long as we
13546 are careful not to change the type of the overall expression).
13547 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13548 but we can constant-fold them if they have constant operands. */
13550 #ifdef ENABLE_FOLD_CHECKING
13551 # define fold(x) fold_1 (x)
13552 static tree fold_1 (tree);
13558 const tree t = expr;
13559 enum tree_code code = TREE_CODE (t);
13560 enum tree_code_class kind = TREE_CODE_CLASS (code);
13563 /* Return right away if a constant. */
13564 if (kind == tcc_constant)
13567 /* CALL_EXPR-like objects with variable numbers of operands are
13568 treated specially. */
13569 if (kind == tcc_vl_exp)
13571 if (code == CALL_EXPR)
13573 tem = fold_call_expr (expr, false);
13574 return tem ? tem : expr;
13579 if (IS_EXPR_CODE_CLASS (kind))
13581 tree type = TREE_TYPE (t);
13582 tree op0, op1, op2;
13584 switch (TREE_CODE_LENGTH (code))
13587 op0 = TREE_OPERAND (t, 0);
13588 tem = fold_unary (code, type, op0);
13589 return tem ? tem : expr;
13591 op0 = TREE_OPERAND (t, 0);
13592 op1 = TREE_OPERAND (t, 1);
13593 tem = fold_binary (code, type, op0, op1);
13594 return tem ? tem : expr;
13596 op0 = TREE_OPERAND (t, 0);
13597 op1 = TREE_OPERAND (t, 1);
13598 op2 = TREE_OPERAND (t, 2);
13599 tem = fold_ternary (code, type, op0, op1, op2);
13600 return tem ? tem : expr;
13610 tree op0 = TREE_OPERAND (t, 0);
13611 tree op1 = TREE_OPERAND (t, 1);
13613 if (TREE_CODE (op1) == INTEGER_CST
13614 && TREE_CODE (op0) == CONSTRUCTOR
13615 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13617 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (op0);
13618 unsigned HOST_WIDE_INT end = VEC_length (constructor_elt, elts);
13619 unsigned HOST_WIDE_INT begin = 0;
13621 /* Find a matching index by means of a binary search. */
13622 while (begin != end)
13624 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
13625 tree index = VEC_index (constructor_elt, elts, middle)->index;
13627 if (TREE_CODE (index) == INTEGER_CST
13628 && tree_int_cst_lt (index, op1))
13629 begin = middle + 1;
13630 else if (TREE_CODE (index) == INTEGER_CST
13631 && tree_int_cst_lt (op1, index))
13633 else if (TREE_CODE (index) == RANGE_EXPR
13634 && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
13635 begin = middle + 1;
13636 else if (TREE_CODE (index) == RANGE_EXPR
13637 && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
13640 return VEC_index (constructor_elt, elts, middle)->value;
13648 return fold (DECL_INITIAL (t));
13652 } /* switch (code) */
13655 #ifdef ENABLE_FOLD_CHECKING
13658 static void fold_checksum_tree (const_tree, struct md5_ctx *, htab_t);
13659 static void fold_check_failed (const_tree, const_tree);
13660 void print_fold_checksum (const_tree);
13662 /* When --enable-checking=fold, compute a digest of expr before
13663 and after actual fold call to see if fold did not accidentally
13664 change original expr. */
13670 struct md5_ctx ctx;
13671 unsigned char checksum_before[16], checksum_after[16];
13674 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13675 md5_init_ctx (&ctx);
13676 fold_checksum_tree (expr, &ctx, ht);
13677 md5_finish_ctx (&ctx, checksum_before);
13680 ret = fold_1 (expr);
13682 md5_init_ctx (&ctx);
13683 fold_checksum_tree (expr, &ctx, ht);
13684 md5_finish_ctx (&ctx, checksum_after);
13687 if (memcmp (checksum_before, checksum_after, 16))
13688 fold_check_failed (expr, ret);
13694 print_fold_checksum (const_tree expr)
13696 struct md5_ctx ctx;
13697 unsigned char checksum[16], cnt;
13700 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13701 md5_init_ctx (&ctx);
13702 fold_checksum_tree (expr, &ctx, ht);
13703 md5_finish_ctx (&ctx, checksum);
13705 for (cnt = 0; cnt < 16; ++cnt)
13706 fprintf (stderr, "%02x", checksum[cnt]);
13707 putc ('\n', stderr);
13711 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13713 internal_error ("fold check: original tree changed by fold");
13717 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, htab_t ht)
13720 enum tree_code code;
13721 union tree_node buf;
13726 gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
13727 <= sizeof (struct tree_function_decl))
13728 && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
13731 slot = (const void **) htab_find_slot (ht, expr, INSERT);
13735 code = TREE_CODE (expr);
13736 if (TREE_CODE_CLASS (code) == tcc_declaration
13737 && DECL_ASSEMBLER_NAME_SET_P (expr))
13739 /* Allow DECL_ASSEMBLER_NAME to be modified. */
13740 memcpy ((char *) &buf, expr, tree_size (expr));
13741 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
13742 expr = (tree) &buf;
13744 else if (TREE_CODE_CLASS (code) == tcc_type
13745 && (TYPE_POINTER_TO (expr)
13746 || TYPE_REFERENCE_TO (expr)
13747 || TYPE_CACHED_VALUES_P (expr)
13748 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13749 || TYPE_NEXT_VARIANT (expr)))
13751 /* Allow these fields to be modified. */
13753 memcpy ((char *) &buf, expr, tree_size (expr));
13754 expr = tmp = (tree) &buf;
13755 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13756 TYPE_POINTER_TO (tmp) = NULL;
13757 TYPE_REFERENCE_TO (tmp) = NULL;
13758 TYPE_NEXT_VARIANT (tmp) = NULL;
13759 if (TYPE_CACHED_VALUES_P (tmp))
13761 TYPE_CACHED_VALUES_P (tmp) = 0;
13762 TYPE_CACHED_VALUES (tmp) = NULL;
13765 md5_process_bytes (expr, tree_size (expr), ctx);
13766 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13767 if (TREE_CODE_CLASS (code) != tcc_type
13768 && TREE_CODE_CLASS (code) != tcc_declaration
13769 && code != TREE_LIST
13770 && code != SSA_NAME)
13771 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13772 switch (TREE_CODE_CLASS (code))
13778 md5_process_bytes (TREE_STRING_POINTER (expr),
13779 TREE_STRING_LENGTH (expr), ctx);
13782 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13783 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13786 fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
13792 case tcc_exceptional:
13796 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13797 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13798 expr = TREE_CHAIN (expr);
13799 goto recursive_label;
13802 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13803 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13809 case tcc_expression:
13810 case tcc_reference:
13811 case tcc_comparison:
13814 case tcc_statement:
13816 len = TREE_OPERAND_LENGTH (expr);
13817 for (i = 0; i < len; ++i)
13818 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13820 case tcc_declaration:
13821 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13822 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13823 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13825 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13826 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13827 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13828 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13829 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13831 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
13832 fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
13834 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13836 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13837 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13838 fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
13842 if (TREE_CODE (expr) == ENUMERAL_TYPE)
13843 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13844 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13845 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13846 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13847 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13848 if (INTEGRAL_TYPE_P (expr)
13849 || SCALAR_FLOAT_TYPE_P (expr))
13851 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13852 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13854 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13855 if (TREE_CODE (expr) == RECORD_TYPE
13856 || TREE_CODE (expr) == UNION_TYPE
13857 || TREE_CODE (expr) == QUAL_UNION_TYPE)
13858 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13859 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13866 /* Helper function for outputting the checksum of a tree T. When
13867 debugging with gdb, you can "define mynext" to be "next" followed
13868 by "call debug_fold_checksum (op0)", then just trace down till the
13872 debug_fold_checksum (const_tree t)
13875 unsigned char checksum[16];
13876 struct md5_ctx ctx;
13877 htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13879 md5_init_ctx (&ctx);
13880 fold_checksum_tree (t, &ctx, ht);
13881 md5_finish_ctx (&ctx, checksum);
13884 for (i = 0; i < 16; i++)
13885 fprintf (stderr, "%d ", checksum[i]);
13887 fprintf (stderr, "\n");
13892 /* Fold a unary tree expression with code CODE of type TYPE with an
13893 operand OP0. Return a folded expression if successful. Otherwise,
13894 return a tree expression with code CODE of type TYPE with an
13898 fold_build1_stat (enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13901 #ifdef ENABLE_FOLD_CHECKING
13902 unsigned char checksum_before[16], checksum_after[16];
13903 struct md5_ctx ctx;
13906 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13907 md5_init_ctx (&ctx);
13908 fold_checksum_tree (op0, &ctx, ht);
13909 md5_finish_ctx (&ctx, checksum_before);
13913 tem = fold_unary (code, type, op0);
13915 tem = build1_stat (code, type, op0 PASS_MEM_STAT);
13917 #ifdef ENABLE_FOLD_CHECKING
13918 md5_init_ctx (&ctx);
13919 fold_checksum_tree (op0, &ctx, ht);
13920 md5_finish_ctx (&ctx, checksum_after);
13923 if (memcmp (checksum_before, checksum_after, 16))
13924 fold_check_failed (op0, tem);
13929 /* Fold a binary tree expression with code CODE of type TYPE with
13930 operands OP0 and OP1. Return a folded expression if successful.
13931 Otherwise, return a tree expression with code CODE of type TYPE
13932 with operands OP0 and OP1. */
13935 fold_build2_stat (enum tree_code code, tree type, tree op0, tree op1
13939 #ifdef ENABLE_FOLD_CHECKING
13940 unsigned char checksum_before_op0[16],
13941 checksum_before_op1[16],
13942 checksum_after_op0[16],
13943 checksum_after_op1[16];
13944 struct md5_ctx ctx;
13947 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13948 md5_init_ctx (&ctx);
13949 fold_checksum_tree (op0, &ctx, ht);
13950 md5_finish_ctx (&ctx, checksum_before_op0);
13953 md5_init_ctx (&ctx);
13954 fold_checksum_tree (op1, &ctx, ht);
13955 md5_finish_ctx (&ctx, checksum_before_op1);
13959 tem = fold_binary (code, type, op0, op1);
13961 tem = build2_stat (code, type, op0, op1 PASS_MEM_STAT);
13963 #ifdef ENABLE_FOLD_CHECKING
13964 md5_init_ctx (&ctx);
13965 fold_checksum_tree (op0, &ctx, ht);
13966 md5_finish_ctx (&ctx, checksum_after_op0);
13969 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13970 fold_check_failed (op0, tem);
13972 md5_init_ctx (&ctx);
13973 fold_checksum_tree (op1, &ctx, ht);
13974 md5_finish_ctx (&ctx, checksum_after_op1);
13977 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13978 fold_check_failed (op1, tem);
13983 /* Fold a ternary tree expression with code CODE of type TYPE with
13984 operands OP0, OP1, and OP2. Return a folded expression if
13985 successful. Otherwise, return a tree expression with code CODE of
13986 type TYPE with operands OP0, OP1, and OP2. */
13989 fold_build3_stat (enum tree_code code, tree type, tree op0, tree op1, tree op2
13993 #ifdef ENABLE_FOLD_CHECKING
13994 unsigned char checksum_before_op0[16],
13995 checksum_before_op1[16],
13996 checksum_before_op2[16],
13997 checksum_after_op0[16],
13998 checksum_after_op1[16],
13999 checksum_after_op2[16];
14000 struct md5_ctx ctx;
14003 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14004 md5_init_ctx (&ctx);
14005 fold_checksum_tree (op0, &ctx, ht);
14006 md5_finish_ctx (&ctx, checksum_before_op0);
14009 md5_init_ctx (&ctx);
14010 fold_checksum_tree (op1, &ctx, ht);
14011 md5_finish_ctx (&ctx, checksum_before_op1);
14014 md5_init_ctx (&ctx);
14015 fold_checksum_tree (op2, &ctx, ht);
14016 md5_finish_ctx (&ctx, checksum_before_op2);
14020 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
14021 tem = fold_ternary (code, type, op0, op1, op2);
14023 tem = build3_stat (code, type, op0, op1, op2 PASS_MEM_STAT);
14025 #ifdef ENABLE_FOLD_CHECKING
14026 md5_init_ctx (&ctx);
14027 fold_checksum_tree (op0, &ctx, ht);
14028 md5_finish_ctx (&ctx, checksum_after_op0);
14031 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14032 fold_check_failed (op0, tem);
14034 md5_init_ctx (&ctx);
14035 fold_checksum_tree (op1, &ctx, ht);
14036 md5_finish_ctx (&ctx, checksum_after_op1);
14039 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14040 fold_check_failed (op1, tem);
14042 md5_init_ctx (&ctx);
14043 fold_checksum_tree (op2, &ctx, ht);
14044 md5_finish_ctx (&ctx, checksum_after_op2);
14047 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
14048 fold_check_failed (op2, tem);
14053 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14054 arguments in ARGARRAY, and a null static chain.
14055 Return a folded expression if successful. Otherwise, return a CALL_EXPR
14056 of type TYPE from the given operands as constructed by build_call_array. */
14059 fold_build_call_array (tree type, tree fn, int nargs, tree *argarray)
14062 #ifdef ENABLE_FOLD_CHECKING
14063 unsigned char checksum_before_fn[16],
14064 checksum_before_arglist[16],
14065 checksum_after_fn[16],
14066 checksum_after_arglist[16];
14067 struct md5_ctx ctx;
14071 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14072 md5_init_ctx (&ctx);
14073 fold_checksum_tree (fn, &ctx, ht);
14074 md5_finish_ctx (&ctx, checksum_before_fn);
14077 md5_init_ctx (&ctx);
14078 for (i = 0; i < nargs; i++)
14079 fold_checksum_tree (argarray[i], &ctx, ht);
14080 md5_finish_ctx (&ctx, checksum_before_arglist);
14084 tem = fold_builtin_call_array (type, fn, nargs, argarray);
14086 #ifdef ENABLE_FOLD_CHECKING
14087 md5_init_ctx (&ctx);
14088 fold_checksum_tree (fn, &ctx, ht);
14089 md5_finish_ctx (&ctx, checksum_after_fn);
14092 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
14093 fold_check_failed (fn, tem);
14095 md5_init_ctx (&ctx);
14096 for (i = 0; i < nargs; i++)
14097 fold_checksum_tree (argarray[i], &ctx, ht);
14098 md5_finish_ctx (&ctx, checksum_after_arglist);
14101 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
14102 fold_check_failed (NULL_TREE, tem);
14107 /* Perform constant folding and related simplification of initializer
14108 expression EXPR. These behave identically to "fold_buildN" but ignore
14109 potential run-time traps and exceptions that fold must preserve. */
14111 #define START_FOLD_INIT \
14112 int saved_signaling_nans = flag_signaling_nans;\
14113 int saved_trapping_math = flag_trapping_math;\
14114 int saved_rounding_math = flag_rounding_math;\
14115 int saved_trapv = flag_trapv;\
14116 int saved_folding_initializer = folding_initializer;\
14117 flag_signaling_nans = 0;\
14118 flag_trapping_math = 0;\
14119 flag_rounding_math = 0;\
14121 folding_initializer = 1;
14123 #define END_FOLD_INIT \
14124 flag_signaling_nans = saved_signaling_nans;\
14125 flag_trapping_math = saved_trapping_math;\
14126 flag_rounding_math = saved_rounding_math;\
14127 flag_trapv = saved_trapv;\
14128 folding_initializer = saved_folding_initializer;
14131 fold_build1_initializer (enum tree_code code, tree type, tree op)
14136 result = fold_build1 (code, type, op);
14143 fold_build2_initializer (enum tree_code code, tree type, tree op0, tree op1)
14148 result = fold_build2 (code, type, op0, op1);
14155 fold_build3_initializer (enum tree_code code, tree type, tree op0, tree op1,
14161 result = fold_build3 (code, type, op0, op1, op2);
14168 fold_build_call_array_initializer (tree type, tree fn,
14169 int nargs, tree *argarray)
14174 result = fold_build_call_array (type, fn, nargs, argarray);
14180 #undef START_FOLD_INIT
14181 #undef END_FOLD_INIT
14183 /* Determine if first argument is a multiple of second argument. Return 0 if
14184 it is not, or we cannot easily determined it to be.
14186 An example of the sort of thing we care about (at this point; this routine
14187 could surely be made more general, and expanded to do what the *_DIV_EXPR's
14188 fold cases do now) is discovering that
14190 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14196 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14198 This code also handles discovering that
14200 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14202 is a multiple of 8 so we don't have to worry about dealing with a
14203 possible remainder.
14205 Note that we *look* inside a SAVE_EXPR only to determine how it was
14206 calculated; it is not safe for fold to do much of anything else with the
14207 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14208 at run time. For example, the latter example above *cannot* be implemented
14209 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14210 evaluation time of the original SAVE_EXPR is not necessarily the same at
14211 the time the new expression is evaluated. The only optimization of this
14212 sort that would be valid is changing
14214 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14218 SAVE_EXPR (I) * SAVE_EXPR (J)
14220 (where the same SAVE_EXPR (J) is used in the original and the
14221 transformed version). */
14224 multiple_of_p (tree type, const_tree top, const_tree bottom)
14226 if (operand_equal_p (top, bottom, 0))
14229 if (TREE_CODE (type) != INTEGER_TYPE)
14232 switch (TREE_CODE (top))
14235 /* Bitwise and provides a power of two multiple. If the mask is
14236 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
14237 if (!integer_pow2p (bottom))
14242 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14243 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14247 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14248 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14251 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14255 op1 = TREE_OPERAND (top, 1);
14256 /* const_binop may not detect overflow correctly,
14257 so check for it explicitly here. */
14258 if (TYPE_PRECISION (TREE_TYPE (size_one_node))
14259 > TREE_INT_CST_LOW (op1)
14260 && TREE_INT_CST_HIGH (op1) == 0
14261 && 0 != (t1 = fold_convert (type,
14262 const_binop (LSHIFT_EXPR,
14265 && !TREE_OVERFLOW (t1))
14266 return multiple_of_p (type, t1, bottom);
14271 /* Can't handle conversions from non-integral or wider integral type. */
14272 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14273 || (TYPE_PRECISION (type)
14274 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14277 /* .. fall through ... */
14280 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
14283 if (TREE_CODE (bottom) != INTEGER_CST
14284 || integer_zerop (bottom)
14285 || (TYPE_UNSIGNED (type)
14286 && (tree_int_cst_sgn (top) < 0
14287 || tree_int_cst_sgn (bottom) < 0)))
14289 return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
14297 /* Return true if CODE or TYPE is known to be non-negative. */
14300 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14302 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14303 && truth_value_p (code))
14304 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14305 have a signed:1 type (where the value is -1 and 0). */
14310 /* Return true if (CODE OP0) is known to be non-negative. If the return
14311 value is based on the assumption that signed overflow is undefined,
14312 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14313 *STRICT_OVERFLOW_P. */
14316 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14317 bool *strict_overflow_p)
14319 if (TYPE_UNSIGNED (type))
14325 /* We can't return 1 if flag_wrapv is set because
14326 ABS_EXPR<INT_MIN> = INT_MIN. */
14327 if (!INTEGRAL_TYPE_P (type))
14329 if (TYPE_OVERFLOW_UNDEFINED (type))
14331 *strict_overflow_p = true;
14336 case NON_LVALUE_EXPR:
14338 case FIX_TRUNC_EXPR:
14339 return tree_expr_nonnegative_warnv_p (op0,
14340 strict_overflow_p);
14344 tree inner_type = TREE_TYPE (op0);
14345 tree outer_type = type;
14347 if (TREE_CODE (outer_type) == REAL_TYPE)
14349 if (TREE_CODE (inner_type) == REAL_TYPE)
14350 return tree_expr_nonnegative_warnv_p (op0,
14351 strict_overflow_p);
14352 if (TREE_CODE (inner_type) == INTEGER_TYPE)
14354 if (TYPE_UNSIGNED (inner_type))
14356 return tree_expr_nonnegative_warnv_p (op0,
14357 strict_overflow_p);
14360 else if (TREE_CODE (outer_type) == INTEGER_TYPE)
14362 if (TREE_CODE (inner_type) == REAL_TYPE)
14363 return tree_expr_nonnegative_warnv_p (op0,
14364 strict_overflow_p);
14365 if (TREE_CODE (inner_type) == INTEGER_TYPE)
14366 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14367 && TYPE_UNSIGNED (inner_type);
14373 return tree_simple_nonnegative_warnv_p (code, type);
14376 /* We don't know sign of `t', so be conservative and return false. */
14380 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
14381 value is based on the assumption that signed overflow is undefined,
14382 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14383 *STRICT_OVERFLOW_P. */
14386 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14387 tree op1, bool *strict_overflow_p)
14389 if (TYPE_UNSIGNED (type))
14394 case POINTER_PLUS_EXPR:
14396 if (FLOAT_TYPE_P (type))
14397 return (tree_expr_nonnegative_warnv_p (op0,
14399 && tree_expr_nonnegative_warnv_p (op1,
14400 strict_overflow_p));
14402 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14403 both unsigned and at least 2 bits shorter than the result. */
14404 if (TREE_CODE (type) == INTEGER_TYPE
14405 && TREE_CODE (op0) == NOP_EXPR
14406 && TREE_CODE (op1) == NOP_EXPR)
14408 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14409 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14410 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14411 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14413 unsigned int prec = MAX (TYPE_PRECISION (inner1),
14414 TYPE_PRECISION (inner2)) + 1;
14415 return prec < TYPE_PRECISION (type);
14421 if (FLOAT_TYPE_P (type))
14423 /* x * x for floating point x is always non-negative. */
14424 if (operand_equal_p (op0, op1, 0))
14426 return (tree_expr_nonnegative_warnv_p (op0,
14428 && tree_expr_nonnegative_warnv_p (op1,
14429 strict_overflow_p));
14432 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14433 both unsigned and their total bits is shorter than the result. */
14434 if (TREE_CODE (type) == INTEGER_TYPE
14435 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14436 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14438 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14439 ? TREE_TYPE (TREE_OPERAND (op0, 0))
14441 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14442 ? TREE_TYPE (TREE_OPERAND (op1, 0))
14445 bool unsigned0 = TYPE_UNSIGNED (inner0);
14446 bool unsigned1 = TYPE_UNSIGNED (inner1);
14448 if (TREE_CODE (op0) == INTEGER_CST)
14449 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14451 if (TREE_CODE (op1) == INTEGER_CST)
14452 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14454 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14455 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14457 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14458 ? tree_int_cst_min_precision (op0, /*unsignedp=*/true)
14459 : TYPE_PRECISION (inner0);
14461 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14462 ? tree_int_cst_min_precision (op1, /*unsignedp=*/true)
14463 : TYPE_PRECISION (inner1);
14465 return precision0 + precision1 < TYPE_PRECISION (type);
14472 return (tree_expr_nonnegative_warnv_p (op0,
14474 || tree_expr_nonnegative_warnv_p (op1,
14475 strict_overflow_p));
14481 case TRUNC_DIV_EXPR:
14482 case CEIL_DIV_EXPR:
14483 case FLOOR_DIV_EXPR:
14484 case ROUND_DIV_EXPR:
14485 return (tree_expr_nonnegative_warnv_p (op0,
14487 && tree_expr_nonnegative_warnv_p (op1,
14488 strict_overflow_p));
14490 case TRUNC_MOD_EXPR:
14491 case CEIL_MOD_EXPR:
14492 case FLOOR_MOD_EXPR:
14493 case ROUND_MOD_EXPR:
14494 return tree_expr_nonnegative_warnv_p (op0,
14495 strict_overflow_p);
14497 return tree_simple_nonnegative_warnv_p (code, type);
14500 /* We don't know sign of `t', so be conservative and return false. */
14504 /* Return true if T is known to be non-negative. If the return
14505 value is based on the assumption that signed overflow is undefined,
14506 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14507 *STRICT_OVERFLOW_P. */
14510 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14512 if (TYPE_UNSIGNED (TREE_TYPE (t)))
14515 switch (TREE_CODE (t))
14518 return tree_int_cst_sgn (t) >= 0;
14521 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14524 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14527 return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14529 && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
14530 strict_overflow_p));
14532 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14535 /* We don't know sign of `t', so be conservative and return false. */
14539 /* Return true if T is known to be non-negative. If the return
14540 value is based on the assumption that signed overflow is undefined,
14541 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14542 *STRICT_OVERFLOW_P. */
14545 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
14546 tree arg0, tree arg1, bool *strict_overflow_p)
14548 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
14549 switch (DECL_FUNCTION_CODE (fndecl))
14551 CASE_FLT_FN (BUILT_IN_ACOS):
14552 CASE_FLT_FN (BUILT_IN_ACOSH):
14553 CASE_FLT_FN (BUILT_IN_CABS):
14554 CASE_FLT_FN (BUILT_IN_COSH):
14555 CASE_FLT_FN (BUILT_IN_ERFC):
14556 CASE_FLT_FN (BUILT_IN_EXP):
14557 CASE_FLT_FN (BUILT_IN_EXP10):
14558 CASE_FLT_FN (BUILT_IN_EXP2):
14559 CASE_FLT_FN (BUILT_IN_FABS):
14560 CASE_FLT_FN (BUILT_IN_FDIM):
14561 CASE_FLT_FN (BUILT_IN_HYPOT):
14562 CASE_FLT_FN (BUILT_IN_POW10):
14563 CASE_INT_FN (BUILT_IN_FFS):
14564 CASE_INT_FN (BUILT_IN_PARITY):
14565 CASE_INT_FN (BUILT_IN_POPCOUNT):
14566 case BUILT_IN_BSWAP32:
14567 case BUILT_IN_BSWAP64:
14571 CASE_FLT_FN (BUILT_IN_SQRT):
14572 /* sqrt(-0.0) is -0.0. */
14573 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
14575 return tree_expr_nonnegative_warnv_p (arg0,
14576 strict_overflow_p);
14578 CASE_FLT_FN (BUILT_IN_ASINH):
14579 CASE_FLT_FN (BUILT_IN_ATAN):
14580 CASE_FLT_FN (BUILT_IN_ATANH):
14581 CASE_FLT_FN (BUILT_IN_CBRT):
14582 CASE_FLT_FN (BUILT_IN_CEIL):
14583 CASE_FLT_FN (BUILT_IN_ERF):
14584 CASE_FLT_FN (BUILT_IN_EXPM1):
14585 CASE_FLT_FN (BUILT_IN_FLOOR):
14586 CASE_FLT_FN (BUILT_IN_FMOD):
14587 CASE_FLT_FN (BUILT_IN_FREXP):
14588 CASE_FLT_FN (BUILT_IN_LCEIL):
14589 CASE_FLT_FN (BUILT_IN_LDEXP):
14590 CASE_FLT_FN (BUILT_IN_LFLOOR):
14591 CASE_FLT_FN (BUILT_IN_LLCEIL):
14592 CASE_FLT_FN (BUILT_IN_LLFLOOR):
14593 CASE_FLT_FN (BUILT_IN_LLRINT):
14594 CASE_FLT_FN (BUILT_IN_LLROUND):
14595 CASE_FLT_FN (BUILT_IN_LRINT):
14596 CASE_FLT_FN (BUILT_IN_LROUND):
14597 CASE_FLT_FN (BUILT_IN_MODF):
14598 CASE_FLT_FN (BUILT_IN_NEARBYINT):
14599 CASE_FLT_FN (BUILT_IN_RINT):
14600 CASE_FLT_FN (BUILT_IN_ROUND):
14601 CASE_FLT_FN (BUILT_IN_SCALB):
14602 CASE_FLT_FN (BUILT_IN_SCALBLN):
14603 CASE_FLT_FN (BUILT_IN_SCALBN):
14604 CASE_FLT_FN (BUILT_IN_SIGNBIT):
14605 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
14606 CASE_FLT_FN (BUILT_IN_SINH):
14607 CASE_FLT_FN (BUILT_IN_TANH):
14608 CASE_FLT_FN (BUILT_IN_TRUNC):
14609 /* True if the 1st argument is nonnegative. */
14610 return tree_expr_nonnegative_warnv_p (arg0,
14611 strict_overflow_p);
14613 CASE_FLT_FN (BUILT_IN_FMAX):
14614 /* True if the 1st OR 2nd arguments are nonnegative. */
14615 return (tree_expr_nonnegative_warnv_p (arg0,
14617 || (tree_expr_nonnegative_warnv_p (arg1,
14618 strict_overflow_p)));
14620 CASE_FLT_FN (BUILT_IN_FMIN):
14621 /* True if the 1st AND 2nd arguments are nonnegative. */
14622 return (tree_expr_nonnegative_warnv_p (arg0,
14624 && (tree_expr_nonnegative_warnv_p (arg1,
14625 strict_overflow_p)));
14627 CASE_FLT_FN (BUILT_IN_COPYSIGN):
14628 /* True if the 2nd argument is nonnegative. */
14629 return tree_expr_nonnegative_warnv_p (arg1,
14630 strict_overflow_p);
14632 CASE_FLT_FN (BUILT_IN_POWI):
14633 /* True if the 1st argument is nonnegative or the second
14634 argument is an even integer. */
14635 if (TREE_CODE (arg1) == INTEGER_CST
14636 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14638 return tree_expr_nonnegative_warnv_p (arg0,
14639 strict_overflow_p);
14641 CASE_FLT_FN (BUILT_IN_POW):
14642 /* True if the 1st argument is nonnegative or the second
14643 argument is an even integer valued real. */
14644 if (TREE_CODE (arg1) == REAL_CST)
14649 c = TREE_REAL_CST (arg1);
14650 n = real_to_integer (&c);
14653 REAL_VALUE_TYPE cint;
14654 real_from_integer (&cint, VOIDmode, n,
14655 n < 0 ? -1 : 0, 0);
14656 if (real_identical (&c, &cint))
14660 return tree_expr_nonnegative_warnv_p (arg0,
14661 strict_overflow_p);
14666 return tree_simple_nonnegative_warnv_p (CALL_EXPR,
14670 /* Return true if T is known to be non-negative. If the return
14671 value is based on the assumption that signed overflow is undefined,
14672 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14673 *STRICT_OVERFLOW_P. */
14676 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14678 enum tree_code code = TREE_CODE (t);
14679 if (TYPE_UNSIGNED (TREE_TYPE (t)))
14686 tree temp = TARGET_EXPR_SLOT (t);
14687 t = TARGET_EXPR_INITIAL (t);
14689 /* If the initializer is non-void, then it's a normal expression
14690 that will be assigned to the slot. */
14691 if (!VOID_TYPE_P (t))
14692 return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
14694 /* Otherwise, the initializer sets the slot in some way. One common
14695 way is an assignment statement at the end of the initializer. */
14698 if (TREE_CODE (t) == BIND_EXPR)
14699 t = expr_last (BIND_EXPR_BODY (t));
14700 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
14701 || TREE_CODE (t) == TRY_CATCH_EXPR)
14702 t = expr_last (TREE_OPERAND (t, 0));
14703 else if (TREE_CODE (t) == STATEMENT_LIST)
14708 if (TREE_CODE (t) == MODIFY_EXPR
14709 && TREE_OPERAND (t, 0) == temp)
14710 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14711 strict_overflow_p);
14718 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
14719 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
14721 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
14722 get_callee_fndecl (t),
14725 strict_overflow_p);
14727 case COMPOUND_EXPR:
14729 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14730 strict_overflow_p);
14732 return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
14733 strict_overflow_p);
14735 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
14736 strict_overflow_p);
14739 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14743 /* We don't know sign of `t', so be conservative and return false. */
14747 /* Return true if T is known to be non-negative. If the return
14748 value is based on the assumption that signed overflow is undefined,
14749 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14750 *STRICT_OVERFLOW_P. */
14753 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14755 enum tree_code code;
14756 if (t == error_mark_node)
14759 code = TREE_CODE (t);
14760 switch (TREE_CODE_CLASS (code))
14763 case tcc_comparison:
14764 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14766 TREE_OPERAND (t, 0),
14767 TREE_OPERAND (t, 1),
14768 strict_overflow_p);
14771 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14773 TREE_OPERAND (t, 0),
14774 strict_overflow_p);
14777 case tcc_declaration:
14778 case tcc_reference:
14779 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14787 case TRUTH_AND_EXPR:
14788 case TRUTH_OR_EXPR:
14789 case TRUTH_XOR_EXPR:
14790 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14792 TREE_OPERAND (t, 0),
14793 TREE_OPERAND (t, 1),
14794 strict_overflow_p);
14795 case TRUTH_NOT_EXPR:
14796 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14798 TREE_OPERAND (t, 0),
14799 strict_overflow_p);
14806 case WITH_SIZE_EXPR:
14810 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14813 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
14817 /* Return true if `t' is known to be non-negative. Handle warnings
14818 about undefined signed overflow. */
14821 tree_expr_nonnegative_p (tree t)
14823 bool ret, strict_overflow_p;
14825 strict_overflow_p = false;
14826 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
14827 if (strict_overflow_p)
14828 fold_overflow_warning (("assuming signed overflow does not occur when "
14829 "determining that expression is always "
14831 WARN_STRICT_OVERFLOW_MISC);
14836 /* Return true when (CODE OP0) is an address and is known to be nonzero.
14837 For floating point we further ensure that T is not denormal.
14838 Similar logic is present in nonzero_address in rtlanal.h.
14840 If the return value is based on the assumption that signed overflow
14841 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14842 change *STRICT_OVERFLOW_P. */
14845 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
14846 bool *strict_overflow_p)
14851 return tree_expr_nonzero_warnv_p (op0,
14852 strict_overflow_p);
14856 tree inner_type = TREE_TYPE (op0);
14857 tree outer_type = type;
14859 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
14860 && tree_expr_nonzero_warnv_p (op0,
14861 strict_overflow_p));
14865 case NON_LVALUE_EXPR:
14866 return tree_expr_nonzero_warnv_p (op0,
14867 strict_overflow_p);
14876 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
14877 For floating point we further ensure that T is not denormal.
14878 Similar logic is present in nonzero_address in rtlanal.h.
14880 If the return value is based on the assumption that signed overflow
14881 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14882 change *STRICT_OVERFLOW_P. */
14885 tree_binary_nonzero_warnv_p (enum tree_code code,
14888 tree op1, bool *strict_overflow_p)
14890 bool sub_strict_overflow_p;
14893 case POINTER_PLUS_EXPR:
14895 if (TYPE_OVERFLOW_UNDEFINED (type))
14897 /* With the presence of negative values it is hard
14898 to say something. */
14899 sub_strict_overflow_p = false;
14900 if (!tree_expr_nonnegative_warnv_p (op0,
14901 &sub_strict_overflow_p)
14902 || !tree_expr_nonnegative_warnv_p (op1,
14903 &sub_strict_overflow_p))
14905 /* One of operands must be positive and the other non-negative. */
14906 /* We don't set *STRICT_OVERFLOW_P here: even if this value
14907 overflows, on a twos-complement machine the sum of two
14908 nonnegative numbers can never be zero. */
14909 return (tree_expr_nonzero_warnv_p (op0,
14911 || tree_expr_nonzero_warnv_p (op1,
14912 strict_overflow_p));
14917 if (TYPE_OVERFLOW_UNDEFINED (type))
14919 if (tree_expr_nonzero_warnv_p (op0,
14921 && tree_expr_nonzero_warnv_p (op1,
14922 strict_overflow_p))
14924 *strict_overflow_p = true;
14931 sub_strict_overflow_p = false;
14932 if (tree_expr_nonzero_warnv_p (op0,
14933 &sub_strict_overflow_p)
14934 && tree_expr_nonzero_warnv_p (op1,
14935 &sub_strict_overflow_p))
14937 if (sub_strict_overflow_p)
14938 *strict_overflow_p = true;
14943 sub_strict_overflow_p = false;
14944 if (tree_expr_nonzero_warnv_p (op0,
14945 &sub_strict_overflow_p))
14947 if (sub_strict_overflow_p)
14948 *strict_overflow_p = true;
14950 /* When both operands are nonzero, then MAX must be too. */
14951 if (tree_expr_nonzero_warnv_p (op1,
14952 strict_overflow_p))
14955 /* MAX where operand 0 is positive is positive. */
14956 return tree_expr_nonnegative_warnv_p (op0,
14957 strict_overflow_p);
14959 /* MAX where operand 1 is positive is positive. */
14960 else if (tree_expr_nonzero_warnv_p (op1,
14961 &sub_strict_overflow_p)
14962 && tree_expr_nonnegative_warnv_p (op1,
14963 &sub_strict_overflow_p))
14965 if (sub_strict_overflow_p)
14966 *strict_overflow_p = true;
14972 return (tree_expr_nonzero_warnv_p (op1,
14974 || tree_expr_nonzero_warnv_p (op0,
14975 strict_overflow_p));
14984 /* Return true when T is an address and is known to be nonzero.
14985 For floating point we further ensure that T is not denormal.
14986 Similar logic is present in nonzero_address in rtlanal.h.
14988 If the return value is based on the assumption that signed overflow
14989 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14990 change *STRICT_OVERFLOW_P. */
14993 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
14995 bool sub_strict_overflow_p;
14996 switch (TREE_CODE (t))
14999 return !integer_zerop (t);
15003 tree base = get_base_address (TREE_OPERAND (t, 0));
15008 /* Weak declarations may link to NULL. */
15009 if (VAR_OR_FUNCTION_DECL_P (base))
15010 return !DECL_WEAK (base);
15012 /* Constants are never weak. */
15013 if (CONSTANT_CLASS_P (base))
15020 sub_strict_overflow_p = false;
15021 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15022 &sub_strict_overflow_p)
15023 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
15024 &sub_strict_overflow_p))
15026 if (sub_strict_overflow_p)
15027 *strict_overflow_p = true;
15038 /* Return true when T is an address and is known to be nonzero.
15039 For floating point we further ensure that T is not denormal.
15040 Similar logic is present in nonzero_address in rtlanal.h.
15042 If the return value is based on the assumption that signed overflow
15043 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15044 change *STRICT_OVERFLOW_P. */
15047 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15049 tree type = TREE_TYPE (t);
15050 enum tree_code code;
15052 /* Doing something useful for floating point would need more work. */
15053 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
15056 code = TREE_CODE (t);
15057 switch (TREE_CODE_CLASS (code))
15060 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15061 strict_overflow_p);
15063 case tcc_comparison:
15064 return tree_binary_nonzero_warnv_p (code, type,
15065 TREE_OPERAND (t, 0),
15066 TREE_OPERAND (t, 1),
15067 strict_overflow_p);
15069 case tcc_declaration:
15070 case tcc_reference:
15071 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15079 case TRUTH_NOT_EXPR:
15080 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15081 strict_overflow_p);
15083 case TRUTH_AND_EXPR:
15084 case TRUTH_OR_EXPR:
15085 case TRUTH_XOR_EXPR:
15086 return tree_binary_nonzero_warnv_p (code, type,
15087 TREE_OPERAND (t, 0),
15088 TREE_OPERAND (t, 1),
15089 strict_overflow_p);
15096 case WITH_SIZE_EXPR:
15100 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15102 case COMPOUND_EXPR:
15105 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15106 strict_overflow_p);
15109 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
15110 strict_overflow_p);
15113 return alloca_call_p (t);
15121 /* Return true when T is an address and is known to be nonzero.
15122 Handle warnings about undefined signed overflow. */
15125 tree_expr_nonzero_p (tree t)
15127 bool ret, strict_overflow_p;
15129 strict_overflow_p = false;
15130 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
15131 if (strict_overflow_p)
15132 fold_overflow_warning (("assuming signed overflow does not occur when "
15133 "determining that expression is always "
15135 WARN_STRICT_OVERFLOW_MISC);
15139 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15140 attempt to fold the expression to a constant without modifying TYPE,
15143 If the expression could be simplified to a constant, then return
15144 the constant. If the expression would not be simplified to a
15145 constant, then return NULL_TREE. */
15148 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15150 tree tem = fold_binary (code, type, op0, op1);
15151 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15154 /* Given the components of a unary expression CODE, TYPE and OP0,
15155 attempt to fold the expression to a constant without modifying
15158 If the expression could be simplified to a constant, then return
15159 the constant. If the expression would not be simplified to a
15160 constant, then return NULL_TREE. */
15163 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15165 tree tem = fold_unary (code, type, op0);
15166 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15169 /* If EXP represents referencing an element in a constant string
15170 (either via pointer arithmetic or array indexing), return the
15171 tree representing the value accessed, otherwise return NULL. */
15174 fold_read_from_constant_string (tree exp)
15176 if ((TREE_CODE (exp) == INDIRECT_REF
15177 || TREE_CODE (exp) == ARRAY_REF)
15178 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15180 tree exp1 = TREE_OPERAND (exp, 0);
15184 if (TREE_CODE (exp) == INDIRECT_REF)
15185 string = string_constant (exp1, &index);
15188 tree low_bound = array_ref_low_bound (exp);
15189 index = fold_convert (sizetype, TREE_OPERAND (exp, 1));
15191 /* Optimize the special-case of a zero lower bound.
15193 We convert the low_bound to sizetype to avoid some problems
15194 with constant folding. (E.g. suppose the lower bound is 1,
15195 and its mode is QI. Without the conversion,l (ARRAY
15196 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15197 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
15198 if (! integer_zerop (low_bound))
15199 index = size_diffop (index, fold_convert (sizetype, low_bound));
15205 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15206 && TREE_CODE (string) == STRING_CST
15207 && TREE_CODE (index) == INTEGER_CST
15208 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15209 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
15211 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
15212 return build_int_cst_type (TREE_TYPE (exp),
15213 (TREE_STRING_POINTER (string)
15214 [TREE_INT_CST_LOW (index)]));
15219 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15220 an integer constant, real, or fixed-point constant.
15222 TYPE is the type of the result. */
15225 fold_negate_const (tree arg0, tree type)
15227 tree t = NULL_TREE;
15229 switch (TREE_CODE (arg0))
15233 unsigned HOST_WIDE_INT low;
15234 HOST_WIDE_INT high;
15235 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
15236 TREE_INT_CST_HIGH (arg0),
15238 t = force_fit_type_double (type, low, high, 1,
15239 (overflow | TREE_OVERFLOW (arg0))
15240 && !TYPE_UNSIGNED (type));
15245 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
15250 FIXED_VALUE_TYPE f;
15251 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15252 &(TREE_FIXED_CST (arg0)), NULL,
15253 TYPE_SATURATING (type));
15254 t = build_fixed (type, f);
15255 /* Propagate overflow flags. */
15256 if (overflow_p | TREE_OVERFLOW (arg0))
15258 TREE_OVERFLOW (t) = 1;
15259 TREE_CONSTANT_OVERFLOW (t) = 1;
15261 else if (TREE_CONSTANT_OVERFLOW (arg0))
15262 TREE_CONSTANT_OVERFLOW (t) = 1;
15267 gcc_unreachable ();
15273 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15274 an integer constant or real constant.
15276 TYPE is the type of the result. */
15279 fold_abs_const (tree arg0, tree type)
15281 tree t = NULL_TREE;
15283 switch (TREE_CODE (arg0))
15286 /* If the value is unsigned, then the absolute value is
15287 the same as the ordinary value. */
15288 if (TYPE_UNSIGNED (type))
15290 /* Similarly, if the value is non-negative. */
15291 else if (INT_CST_LT (integer_minus_one_node, arg0))
15293 /* If the value is negative, then the absolute value is
15297 unsigned HOST_WIDE_INT low;
15298 HOST_WIDE_INT high;
15299 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
15300 TREE_INT_CST_HIGH (arg0),
15302 t = force_fit_type_double (type, low, high, -1,
15303 overflow | TREE_OVERFLOW (arg0));
15308 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15309 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
15315 gcc_unreachable ();
15321 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15322 constant. TYPE is the type of the result. */
15325 fold_not_const (tree arg0, tree type)
15327 tree t = NULL_TREE;
15329 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15331 t = force_fit_type_double (type, ~TREE_INT_CST_LOW (arg0),
15332 ~TREE_INT_CST_HIGH (arg0), 0,
15333 TREE_OVERFLOW (arg0));
15338 /* Given CODE, a relational operator, the target type, TYPE and two
15339 constant operands OP0 and OP1, return the result of the
15340 relational operation. If the result is not a compile time
15341 constant, then return NULL_TREE. */
15344 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15346 int result, invert;
15348 /* From here on, the only cases we handle are when the result is
15349 known to be a constant. */
15351 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15353 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15354 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15356 /* Handle the cases where either operand is a NaN. */
15357 if (real_isnan (c0) || real_isnan (c1))
15367 case UNORDERED_EXPR:
15381 if (flag_trapping_math)
15387 gcc_unreachable ();
15390 return constant_boolean_node (result, type);
15393 return constant_boolean_node (real_compare (code, c0, c1), type);
15396 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15398 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15399 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15400 return constant_boolean_node (fixed_compare (code, c0, c1), type);
15403 /* Handle equality/inequality of complex constants. */
15404 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15406 tree rcond = fold_relational_const (code, type,
15407 TREE_REALPART (op0),
15408 TREE_REALPART (op1));
15409 tree icond = fold_relational_const (code, type,
15410 TREE_IMAGPART (op0),
15411 TREE_IMAGPART (op1));
15412 if (code == EQ_EXPR)
15413 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15414 else if (code == NE_EXPR)
15415 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15420 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15422 To compute GT, swap the arguments and do LT.
15423 To compute GE, do LT and invert the result.
15424 To compute LE, swap the arguments, do LT and invert the result.
15425 To compute NE, do EQ and invert the result.
15427 Therefore, the code below must handle only EQ and LT. */
15429 if (code == LE_EXPR || code == GT_EXPR)
15434 code = swap_tree_comparison (code);
15437 /* Note that it is safe to invert for real values here because we
15438 have already handled the one case that it matters. */
15441 if (code == NE_EXPR || code == GE_EXPR)
15444 code = invert_tree_comparison (code, false);
15447 /* Compute a result for LT or EQ if args permit;
15448 Otherwise return T. */
15449 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15451 if (code == EQ_EXPR)
15452 result = tree_int_cst_equal (op0, op1);
15453 else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
15454 result = INT_CST_LT_UNSIGNED (op0, op1);
15456 result = INT_CST_LT (op0, op1);
15463 return constant_boolean_node (result, type);
15466 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15467 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
15471 fold_build_cleanup_point_expr (tree type, tree expr)
15473 /* If the expression does not have side effects then we don't have to wrap
15474 it with a cleanup point expression. */
15475 if (!TREE_SIDE_EFFECTS (expr))
15478 /* If the expression is a return, check to see if the expression inside the
15479 return has no side effects or the right hand side of the modify expression
15480 inside the return. If either don't have side effects set we don't need to
15481 wrap the expression in a cleanup point expression. Note we don't check the
15482 left hand side of the modify because it should always be a return decl. */
15483 if (TREE_CODE (expr) == RETURN_EXPR)
15485 tree op = TREE_OPERAND (expr, 0);
15486 if (!op || !TREE_SIDE_EFFECTS (op))
15488 op = TREE_OPERAND (op, 1);
15489 if (!TREE_SIDE_EFFECTS (op))
15493 return build1 (CLEANUP_POINT_EXPR, type, expr);
15496 /* Given a pointer value OP0 and a type TYPE, return a simplified version
15497 of an indirection through OP0, or NULL_TREE if no simplification is
15501 fold_indirect_ref_1 (tree type, tree op0)
15507 subtype = TREE_TYPE (sub);
15508 if (!POINTER_TYPE_P (subtype))
15511 if (TREE_CODE (sub) == ADDR_EXPR)
15513 tree op = TREE_OPERAND (sub, 0);
15514 tree optype = TREE_TYPE (op);
15515 /* *&CONST_DECL -> to the value of the const decl. */
15516 if (TREE_CODE (op) == CONST_DECL)
15517 return DECL_INITIAL (op);
15518 /* *&p => p; make sure to handle *&"str"[cst] here. */
15519 if (type == optype)
15521 tree fop = fold_read_from_constant_string (op);
15527 /* *(foo *)&fooarray => fooarray[0] */
15528 else if (TREE_CODE (optype) == ARRAY_TYPE
15529 && type == TREE_TYPE (optype))
15531 tree type_domain = TYPE_DOMAIN (optype);
15532 tree min_val = size_zero_node;
15533 if (type_domain && TYPE_MIN_VALUE (type_domain))
15534 min_val = TYPE_MIN_VALUE (type_domain);
15535 return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
15537 /* *(foo *)&complexfoo => __real__ complexfoo */
15538 else if (TREE_CODE (optype) == COMPLEX_TYPE
15539 && type == TREE_TYPE (optype))
15540 return fold_build1 (REALPART_EXPR, type, op);
15541 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15542 else if (TREE_CODE (optype) == VECTOR_TYPE
15543 && type == TREE_TYPE (optype))
15545 tree part_width = TYPE_SIZE (type);
15546 tree index = bitsize_int (0);
15547 return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
15551 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15552 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15553 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15555 tree op00 = TREE_OPERAND (sub, 0);
15556 tree op01 = TREE_OPERAND (sub, 1);
15560 op00type = TREE_TYPE (op00);
15561 if (TREE_CODE (op00) == ADDR_EXPR
15562 && TREE_CODE (TREE_TYPE (op00type)) == VECTOR_TYPE
15563 && type == TREE_TYPE (TREE_TYPE (op00type)))
15565 HOST_WIDE_INT offset = tree_low_cst (op01, 0);
15566 tree part_width = TYPE_SIZE (type);
15567 unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT;
15568 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
15569 tree index = bitsize_int (indexi);
15571 if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (op00type)))
15572 return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (op00, 0),
15573 part_width, index);
15579 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
15580 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15581 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15583 tree op00 = TREE_OPERAND (sub, 0);
15584 tree op01 = TREE_OPERAND (sub, 1);
15588 op00type = TREE_TYPE (op00);
15589 if (TREE_CODE (op00) == ADDR_EXPR
15590 && TREE_CODE (TREE_TYPE (op00type)) == COMPLEX_TYPE
15591 && type == TREE_TYPE (TREE_TYPE (op00type)))
15593 tree size = TYPE_SIZE_UNIT (type);
15594 if (tree_int_cst_equal (size, op01))
15595 return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (op00, 0));
15599 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
15600 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
15601 && type == TREE_TYPE (TREE_TYPE (subtype)))
15604 tree min_val = size_zero_node;
15605 sub = build_fold_indirect_ref (sub);
15606 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
15607 if (type_domain && TYPE_MIN_VALUE (type_domain))
15608 min_val = TYPE_MIN_VALUE (type_domain);
15609 return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
15615 /* Builds an expression for an indirection through T, simplifying some
15619 build_fold_indirect_ref (tree t)
15621 tree type = TREE_TYPE (TREE_TYPE (t));
15622 tree sub = fold_indirect_ref_1 (type, t);
15627 return build1 (INDIRECT_REF, type, t);
15630 /* Given an INDIRECT_REF T, return either T or a simplified version. */
15633 fold_indirect_ref (tree t)
15635 tree sub = fold_indirect_ref_1 (TREE_TYPE (t), TREE_OPERAND (t, 0));
15643 /* Strip non-trapping, non-side-effecting tree nodes from an expression
15644 whose result is ignored. The type of the returned tree need not be
15645 the same as the original expression. */
15648 fold_ignored_result (tree t)
15650 if (!TREE_SIDE_EFFECTS (t))
15651 return integer_zero_node;
15654 switch (TREE_CODE_CLASS (TREE_CODE (t)))
15657 t = TREE_OPERAND (t, 0);
15661 case tcc_comparison:
15662 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15663 t = TREE_OPERAND (t, 0);
15664 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
15665 t = TREE_OPERAND (t, 1);
15670 case tcc_expression:
15671 switch (TREE_CODE (t))
15673 case COMPOUND_EXPR:
15674 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15676 t = TREE_OPERAND (t, 0);
15680 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
15681 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
15683 t = TREE_OPERAND (t, 0);
15696 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
15697 This can only be applied to objects of a sizetype. */
15700 round_up (tree value, int divisor)
15702 tree div = NULL_TREE;
15704 gcc_assert (divisor > 0);
15708 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
15709 have to do anything. Only do this when we are not given a const,
15710 because in that case, this check is more expensive than just
15712 if (TREE_CODE (value) != INTEGER_CST)
15714 div = build_int_cst (TREE_TYPE (value), divisor);
15716 if (multiple_of_p (TREE_TYPE (value), value, div))
15720 /* If divisor is a power of two, simplify this to bit manipulation. */
15721 if (divisor == (divisor & -divisor))
15723 if (TREE_CODE (value) == INTEGER_CST)
15725 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (value);
15726 unsigned HOST_WIDE_INT high;
15729 if ((low & (divisor - 1)) == 0)
15732 overflow_p = TREE_OVERFLOW (value);
15733 high = TREE_INT_CST_HIGH (value);
15734 low &= ~(divisor - 1);
15743 return force_fit_type_double (TREE_TYPE (value), low, high,
15750 t = build_int_cst (TREE_TYPE (value), divisor - 1);
15751 value = size_binop (PLUS_EXPR, value, t);
15752 t = build_int_cst (TREE_TYPE (value), -divisor);
15753 value = size_binop (BIT_AND_EXPR, value, t);
15759 div = build_int_cst (TREE_TYPE (value), divisor);
15760 value = size_binop (CEIL_DIV_EXPR, value, div);
15761 value = size_binop (MULT_EXPR, value, div);
15767 /* Likewise, but round down. */
15770 round_down (tree value, int divisor)
15772 tree div = NULL_TREE;
15774 gcc_assert (divisor > 0);
15778 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
15779 have to do anything. Only do this when we are not given a const,
15780 because in that case, this check is more expensive than just
15782 if (TREE_CODE (value) != INTEGER_CST)
15784 div = build_int_cst (TREE_TYPE (value), divisor);
15786 if (multiple_of_p (TREE_TYPE (value), value, div))
15790 /* If divisor is a power of two, simplify this to bit manipulation. */
15791 if (divisor == (divisor & -divisor))
15795 t = build_int_cst (TREE_TYPE (value), -divisor);
15796 value = size_binop (BIT_AND_EXPR, value, t);
15801 div = build_int_cst (TREE_TYPE (value), divisor);
15802 value = size_binop (FLOOR_DIV_EXPR, value, div);
15803 value = size_binop (MULT_EXPR, value, div);
15809 /* Returns the pointer to the base of the object addressed by EXP and
15810 extracts the information about the offset of the access, storing it
15811 to PBITPOS and POFFSET. */
15814 split_address_to_core_and_offset (tree exp,
15815 HOST_WIDE_INT *pbitpos, tree *poffset)
15818 enum machine_mode mode;
15819 int unsignedp, volatilep;
15820 HOST_WIDE_INT bitsize;
15822 if (TREE_CODE (exp) == ADDR_EXPR)
15824 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
15825 poffset, &mode, &unsignedp, &volatilep,
15827 core = fold_addr_expr (core);
15833 *poffset = NULL_TREE;
15839 /* Returns true if addresses of E1 and E2 differ by a constant, false
15840 otherwise. If they do, E1 - E2 is stored in *DIFF. */
15843 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
15846 HOST_WIDE_INT bitpos1, bitpos2;
15847 tree toffset1, toffset2, tdiff, type;
15849 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
15850 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
15852 if (bitpos1 % BITS_PER_UNIT != 0
15853 || bitpos2 % BITS_PER_UNIT != 0
15854 || !operand_equal_p (core1, core2, 0))
15857 if (toffset1 && toffset2)
15859 type = TREE_TYPE (toffset1);
15860 if (type != TREE_TYPE (toffset2))
15861 toffset2 = fold_convert (type, toffset2);
15863 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
15864 if (!cst_and_fits_in_hwi (tdiff))
15867 *diff = int_cst_value (tdiff);
15869 else if (toffset1 || toffset2)
15871 /* If only one of the offsets is non-constant, the difference cannot
15878 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
15882 /* Simplify the floating point expression EXP when the sign of the
15883 result is not significant. Return NULL_TREE if no simplification
15887 fold_strip_sign_ops (tree exp)
15891 switch (TREE_CODE (exp))
15895 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15896 return arg0 ? arg0 : TREE_OPERAND (exp, 0);
15900 if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
15902 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15903 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15904 if (arg0 != NULL_TREE || arg1 != NULL_TREE)
15905 return fold_build2 (TREE_CODE (exp), TREE_TYPE (exp),
15906 arg0 ? arg0 : TREE_OPERAND (exp, 0),
15907 arg1 ? arg1 : TREE_OPERAND (exp, 1));
15910 case COMPOUND_EXPR:
15911 arg0 = TREE_OPERAND (exp, 0);
15912 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15914 return fold_build2 (COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
15918 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15919 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
15921 return fold_build3 (COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
15922 arg0 ? arg0 : TREE_OPERAND (exp, 1),
15923 arg1 ? arg1 : TREE_OPERAND (exp, 2));
15928 const enum built_in_function fcode = builtin_mathfn_code (exp);
15931 CASE_FLT_FN (BUILT_IN_COPYSIGN):
15932 /* Strip copysign function call, return the 1st argument. */
15933 arg0 = CALL_EXPR_ARG (exp, 0);
15934 arg1 = CALL_EXPR_ARG (exp, 1);
15935 return omit_one_operand (TREE_TYPE (exp), arg0, arg1);
15938 /* Strip sign ops from the argument of "odd" math functions. */
15939 if (negate_mathfn_p (fcode))
15941 arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
15943 return build_call_expr (get_callee_fndecl (exp), 1, arg0);