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 (location_t, 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 int operand_equal_for_comparison_p (tree, tree, tree);
107 static int twoval_comparison_p (tree, tree *, tree *, int *);
108 static tree eval_subst (location_t, tree, tree, tree, tree, tree);
109 static tree pedantic_omit_one_operand_loc (location_t, tree, tree, tree);
110 static tree distribute_bit_expr (location_t, enum tree_code, tree, tree, tree);
111 static tree make_bit_field_ref (location_t, tree, tree,
112 HOST_WIDE_INT, HOST_WIDE_INT, int);
113 static tree optimize_bit_field_compare (location_t, enum tree_code,
115 static tree decode_field_reference (location_t, tree, HOST_WIDE_INT *,
117 enum machine_mode *, int *, int *,
119 static int all_ones_mask_p (const_tree, int);
120 static tree sign_bit_p (tree, const_tree);
121 static int simple_operand_p (const_tree);
122 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
123 static tree range_predecessor (tree);
124 static tree range_successor (tree);
125 extern tree make_range (tree, int *, tree *, tree *, bool *);
126 extern bool merge_ranges (int *, tree *, tree *, int, tree, tree, int,
128 static tree fold_range_test (location_t, enum tree_code, tree, tree, tree);
129 static tree fold_cond_expr_with_comparison (location_t, tree, tree, tree, tree);
130 static tree unextend (tree, int, int, tree);
131 static tree fold_truthop (location_t, enum tree_code, tree, tree, tree);
132 static tree optimize_minmax_comparison (location_t, enum tree_code,
134 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
135 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
136 static tree fold_binary_op_with_conditional_arg (location_t,
137 enum tree_code, tree,
140 static tree fold_mathfn_compare (location_t,
141 enum built_in_function, enum tree_code,
143 static tree fold_inf_compare (location_t, enum tree_code, tree, tree, tree);
144 static tree fold_div_compare (location_t, enum tree_code, tree, tree, tree);
145 static bool reorder_operands_p (const_tree, const_tree);
146 static tree fold_negate_const (tree, tree);
147 static tree fold_not_const (tree, tree);
148 static tree fold_relational_const (enum tree_code, tree, tree, tree);
149 static tree fold_convert_const (enum tree_code, tree, tree);
152 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
153 overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
154 and SUM1. Then this yields nonzero if overflow occurred during the
157 Overflow occurs if A and B have the same sign, but A and SUM differ in
158 sign. Use `^' to test whether signs differ, and `< 0' to isolate the
160 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
162 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
163 We do that by representing the two-word integer in 4 words, with only
164 HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
165 number. The value of the word is LOWPART + HIGHPART * BASE. */
168 ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
169 #define HIGHPART(x) \
170 ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
171 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
173 /* Unpack a two-word integer into 4 words.
174 LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
175 WORDS points to the array of HOST_WIDE_INTs. */
178 encode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
180 words[0] = LOWPART (low);
181 words[1] = HIGHPART (low);
182 words[2] = LOWPART (hi);
183 words[3] = HIGHPART (hi);
186 /* Pack an array of 4 words into a two-word integer.
187 WORDS points to the array of words.
188 The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */
191 decode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT *low,
194 *low = words[0] + words[1] * BASE;
195 *hi = words[2] + words[3] * BASE;
198 /* Force the double-word integer L1, H1 to be within the range of the
199 integer type TYPE. Stores the properly truncated and sign-extended
200 double-word integer in *LV, *HV. Returns true if the operation
201 overflows, that is, argument and result are different. */
204 fit_double_type (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
205 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, const_tree type)
207 unsigned HOST_WIDE_INT low0 = l1;
208 HOST_WIDE_INT high0 = h1;
209 unsigned int prec = TYPE_PRECISION (type);
210 int sign_extended_type;
212 /* Size types *are* sign extended. */
213 sign_extended_type = (!TYPE_UNSIGNED (type)
214 || (TREE_CODE (type) == INTEGER_TYPE
215 && TYPE_IS_SIZETYPE (type)));
217 /* First clear all bits that are beyond the type's precision. */
218 if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
220 else if (prec > HOST_BITS_PER_WIDE_INT)
221 h1 &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
225 if (prec < HOST_BITS_PER_WIDE_INT)
226 l1 &= ~((HOST_WIDE_INT) (-1) << prec);
229 /* Then do sign extension if necessary. */
230 if (!sign_extended_type)
231 /* No sign extension */;
232 else if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
233 /* Correct width already. */;
234 else if (prec > HOST_BITS_PER_WIDE_INT)
236 /* Sign extend top half? */
237 if (h1 & ((unsigned HOST_WIDE_INT)1
238 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
239 h1 |= (HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT);
241 else if (prec == HOST_BITS_PER_WIDE_INT)
243 if ((HOST_WIDE_INT)l1 < 0)
248 /* Sign extend bottom half? */
249 if (l1 & ((unsigned HOST_WIDE_INT)1 << (prec - 1)))
252 l1 |= (HOST_WIDE_INT)(-1) << prec;
259 /* If the value didn't fit, signal overflow. */
260 return l1 != low0 || h1 != high0;
263 /* We force the double-int HIGH:LOW to the range of the type TYPE by
264 sign or zero extending it.
265 OVERFLOWABLE indicates if we are interested
266 in overflow of the value, when >0 we are only interested in signed
267 overflow, for <0 we are interested in any overflow. OVERFLOWED
268 indicates whether overflow has already occurred. CONST_OVERFLOWED
269 indicates whether constant overflow has already occurred. We force
270 T's value to be within range of T's type (by setting to 0 or 1 all
271 the bits outside the type's range). We set TREE_OVERFLOWED if,
272 OVERFLOWED is nonzero,
273 or OVERFLOWABLE is >0 and signed overflow occurs
274 or OVERFLOWABLE is <0 and any overflow occurs
275 We return a new tree node for the extended double-int. The node
276 is shared if no overflow flags are set. */
279 force_fit_type_double (tree type, unsigned HOST_WIDE_INT low,
280 HOST_WIDE_INT high, int overflowable,
283 int sign_extended_type;
286 /* Size types *are* sign extended. */
287 sign_extended_type = (!TYPE_UNSIGNED (type)
288 || (TREE_CODE (type) == INTEGER_TYPE
289 && TYPE_IS_SIZETYPE (type)));
291 overflow = fit_double_type (low, high, &low, &high, type);
293 /* If we need to set overflow flags, return a new unshared node. */
294 if (overflowed || overflow)
298 || (overflowable > 0 && sign_extended_type))
300 tree t = make_node (INTEGER_CST);
301 TREE_INT_CST_LOW (t) = low;
302 TREE_INT_CST_HIGH (t) = high;
303 TREE_TYPE (t) = type;
304 TREE_OVERFLOW (t) = 1;
309 /* Else build a shared node. */
310 return build_int_cst_wide (type, low, high);
313 /* Add two doubleword integers with doubleword result.
314 Return nonzero if the operation overflows according to UNSIGNED_P.
315 Each argument is given as two `HOST_WIDE_INT' pieces.
316 One argument is L1 and H1; the other, L2 and H2.
317 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
320 add_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
321 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
322 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
325 unsigned HOST_WIDE_INT l;
329 h = (HOST_WIDE_INT) ((unsigned HOST_WIDE_INT) h1
330 + (unsigned HOST_WIDE_INT) h2
337 return ((unsigned HOST_WIDE_INT) h < (unsigned HOST_WIDE_INT) h1
341 return OVERFLOW_SUM_SIGN (h1, h2, h);
344 /* Negate a doubleword integer with doubleword result.
345 Return nonzero if the operation overflows, assuming it's signed.
346 The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
347 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
350 neg_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
351 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
357 return (*hv & h1) < 0;
367 /* Multiply two doubleword integers with doubleword result.
368 Return nonzero if the operation overflows according to UNSIGNED_P.
369 Each argument is given as two `HOST_WIDE_INT' pieces.
370 One argument is L1 and H1; the other, L2 and H2.
371 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
374 mul_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
375 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
376 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
379 HOST_WIDE_INT arg1[4];
380 HOST_WIDE_INT arg2[4];
381 HOST_WIDE_INT prod[4 * 2];
382 unsigned HOST_WIDE_INT carry;
384 unsigned HOST_WIDE_INT toplow, neglow;
385 HOST_WIDE_INT tophigh, neghigh;
387 encode (arg1, l1, h1);
388 encode (arg2, l2, h2);
390 memset (prod, 0, sizeof prod);
392 for (i = 0; i < 4; i++)
395 for (j = 0; j < 4; j++)
398 /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */
399 carry += arg1[i] * arg2[j];
400 /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */
402 prod[k] = LOWPART (carry);
403 carry = HIGHPART (carry);
408 decode (prod, lv, hv);
409 decode (prod + 4, &toplow, &tophigh);
411 /* Unsigned overflow is immediate. */
413 return (toplow | tophigh) != 0;
415 /* Check for signed overflow by calculating the signed representation of the
416 top half of the result; it should agree with the low half's sign bit. */
419 neg_double (l2, h2, &neglow, &neghigh);
420 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
424 neg_double (l1, h1, &neglow, &neghigh);
425 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
427 return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
430 /* Shift the doubleword integer in L1, H1 left by COUNT places
431 keeping only PREC bits of result.
432 Shift right if COUNT is negative.
433 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
434 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
437 lshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
438 HOST_WIDE_INT count, unsigned int prec,
439 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, int arith)
441 unsigned HOST_WIDE_INT signmask;
445 rshift_double (l1, h1, -count, prec, lv, hv, arith);
449 if (SHIFT_COUNT_TRUNCATED)
452 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
454 /* Shifting by the host word size is undefined according to the
455 ANSI standard, so we must handle this as a special case. */
459 else if (count >= HOST_BITS_PER_WIDE_INT)
461 *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
466 *hv = (((unsigned HOST_WIDE_INT) h1 << count)
467 | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
471 /* Sign extend all bits that are beyond the precision. */
473 signmask = -((prec > HOST_BITS_PER_WIDE_INT
474 ? ((unsigned HOST_WIDE_INT) *hv
475 >> (prec - HOST_BITS_PER_WIDE_INT - 1))
476 : (*lv >> (prec - 1))) & 1);
478 if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
480 else if (prec >= HOST_BITS_PER_WIDE_INT)
482 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
483 *hv |= signmask << (prec - HOST_BITS_PER_WIDE_INT);
488 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
489 *lv |= signmask << prec;
493 /* Shift the doubleword integer in L1, H1 right by COUNT places
494 keeping only PREC bits of result. COUNT must be positive.
495 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
496 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
499 rshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
500 HOST_WIDE_INT count, unsigned int prec,
501 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
504 unsigned HOST_WIDE_INT signmask;
507 ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
510 if (SHIFT_COUNT_TRUNCATED)
513 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
515 /* Shifting by the host word size is undefined according to the
516 ANSI standard, so we must handle this as a special case. */
520 else if (count >= HOST_BITS_PER_WIDE_INT)
523 *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
527 *hv = (unsigned HOST_WIDE_INT) h1 >> count;
529 | ((unsigned HOST_WIDE_INT) h1 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
532 /* Zero / sign extend all bits that are beyond the precision. */
534 if (count >= (HOST_WIDE_INT)prec)
539 else if ((prec - count) >= 2 * HOST_BITS_PER_WIDE_INT)
541 else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
543 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - count - HOST_BITS_PER_WIDE_INT));
544 *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
549 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << (prec - count));
550 *lv |= signmask << (prec - count);
554 /* Rotate the doubleword integer in L1, H1 left by COUNT places
555 keeping only PREC bits of result.
556 Rotate right if COUNT is negative.
557 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
560 lrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
561 HOST_WIDE_INT count, unsigned int prec,
562 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
564 unsigned HOST_WIDE_INT s1l, s2l;
565 HOST_WIDE_INT s1h, s2h;
571 lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
572 rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
577 /* Rotate the doubleword integer in L1, H1 left by COUNT places
578 keeping only PREC bits of result. COUNT must be positive.
579 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
582 rrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
583 HOST_WIDE_INT count, unsigned int prec,
584 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
586 unsigned HOST_WIDE_INT s1l, s2l;
587 HOST_WIDE_INT s1h, s2h;
593 rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
594 lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
599 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
600 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
601 CODE is a tree code for a kind of division, one of
602 TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
604 It controls how the quotient is rounded to an integer.
605 Return nonzero if the operation overflows.
606 UNS nonzero says do unsigned division. */
609 div_and_round_double (enum tree_code code, int uns,
610 unsigned HOST_WIDE_INT lnum_orig, /* num == numerator == dividend */
611 HOST_WIDE_INT hnum_orig,
612 unsigned HOST_WIDE_INT lden_orig, /* den == denominator == divisor */
613 HOST_WIDE_INT hden_orig,
614 unsigned HOST_WIDE_INT *lquo,
615 HOST_WIDE_INT *hquo, unsigned HOST_WIDE_INT *lrem,
619 HOST_WIDE_INT num[4 + 1]; /* extra element for scaling. */
620 HOST_WIDE_INT den[4], quo[4];
622 unsigned HOST_WIDE_INT work;
623 unsigned HOST_WIDE_INT carry = 0;
624 unsigned HOST_WIDE_INT lnum = lnum_orig;
625 HOST_WIDE_INT hnum = hnum_orig;
626 unsigned HOST_WIDE_INT lden = lden_orig;
627 HOST_WIDE_INT hden = hden_orig;
630 if (hden == 0 && lden == 0)
631 overflow = 1, lden = 1;
633 /* Calculate quotient sign and convert operands to unsigned. */
639 /* (minimum integer) / (-1) is the only overflow case. */
640 if (neg_double (lnum, hnum, &lnum, &hnum)
641 && ((HOST_WIDE_INT) lden & hden) == -1)
647 neg_double (lden, hden, &lden, &hden);
651 if (hnum == 0 && hden == 0)
652 { /* single precision */
654 /* This unsigned division rounds toward zero. */
660 { /* trivial case: dividend < divisor */
661 /* hden != 0 already checked. */
668 memset (quo, 0, sizeof quo);
670 memset (num, 0, sizeof num); /* to zero 9th element */
671 memset (den, 0, sizeof den);
673 encode (num, lnum, hnum);
674 encode (den, lden, hden);
676 /* Special code for when the divisor < BASE. */
677 if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
679 /* hnum != 0 already checked. */
680 for (i = 4 - 1; i >= 0; i--)
682 work = num[i] + carry * BASE;
683 quo[i] = work / lden;
689 /* Full double precision division,
690 with thanks to Don Knuth's "Seminumerical Algorithms". */
691 int num_hi_sig, den_hi_sig;
692 unsigned HOST_WIDE_INT quo_est, scale;
694 /* Find the highest nonzero divisor digit. */
695 for (i = 4 - 1;; i--)
702 /* Insure that the first digit of the divisor is at least BASE/2.
703 This is required by the quotient digit estimation algorithm. */
705 scale = BASE / (den[den_hi_sig] + 1);
707 { /* scale divisor and dividend */
709 for (i = 0; i <= 4 - 1; i++)
711 work = (num[i] * scale) + carry;
712 num[i] = LOWPART (work);
713 carry = HIGHPART (work);
718 for (i = 0; i <= 4 - 1; i++)
720 work = (den[i] * scale) + carry;
721 den[i] = LOWPART (work);
722 carry = HIGHPART (work);
723 if (den[i] != 0) den_hi_sig = i;
730 for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
732 /* Guess the next quotient digit, quo_est, by dividing the first
733 two remaining dividend digits by the high order quotient digit.
734 quo_est is never low and is at most 2 high. */
735 unsigned HOST_WIDE_INT tmp;
737 num_hi_sig = i + den_hi_sig + 1;
738 work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
739 if (num[num_hi_sig] != den[den_hi_sig])
740 quo_est = work / den[den_hi_sig];
744 /* Refine quo_est so it's usually correct, and at most one high. */
745 tmp = work - quo_est * den[den_hi_sig];
747 && (den[den_hi_sig - 1] * quo_est
748 > (tmp * BASE + num[num_hi_sig - 2])))
751 /* Try QUO_EST as the quotient digit, by multiplying the
752 divisor by QUO_EST and subtracting from the remaining dividend.
753 Keep in mind that QUO_EST is the I - 1st digit. */
756 for (j = 0; j <= den_hi_sig; j++)
758 work = quo_est * den[j] + carry;
759 carry = HIGHPART (work);
760 work = num[i + j] - LOWPART (work);
761 num[i + j] = LOWPART (work);
762 carry += HIGHPART (work) != 0;
765 /* If quo_est was high by one, then num[i] went negative and
766 we need to correct things. */
767 if (num[num_hi_sig] < (HOST_WIDE_INT) carry)
770 carry = 0; /* add divisor back in */
771 for (j = 0; j <= den_hi_sig; j++)
773 work = num[i + j] + den[j] + carry;
774 carry = HIGHPART (work);
775 num[i + j] = LOWPART (work);
778 num [num_hi_sig] += carry;
781 /* Store the quotient digit. */
786 decode (quo, lquo, hquo);
789 /* If result is negative, make it so. */
791 neg_double (*lquo, *hquo, lquo, hquo);
793 /* Compute trial remainder: rem = num - (quo * den) */
794 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
795 neg_double (*lrem, *hrem, lrem, hrem);
796 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
801 case TRUNC_MOD_EXPR: /* round toward zero */
802 case EXACT_DIV_EXPR: /* for this one, it shouldn't matter */
806 case FLOOR_MOD_EXPR: /* round toward negative infinity */
807 if (quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio < 0 && rem != 0 */
810 add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1,
818 case CEIL_MOD_EXPR: /* round toward positive infinity */
819 if (!quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio > 0 && rem != 0 */
821 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
829 case ROUND_MOD_EXPR: /* round to closest integer */
831 unsigned HOST_WIDE_INT labs_rem = *lrem;
832 HOST_WIDE_INT habs_rem = *hrem;
833 unsigned HOST_WIDE_INT labs_den = lden, ltwice;
834 HOST_WIDE_INT habs_den = hden, htwice;
836 /* Get absolute values. */
838 neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
840 neg_double (lden, hden, &labs_den, &habs_den);
842 /* If (2 * abs (lrem) >= abs (lden)), adjust the quotient. */
843 mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
844 labs_rem, habs_rem, <wice, &htwice);
846 if (((unsigned HOST_WIDE_INT) habs_den
847 < (unsigned HOST_WIDE_INT) htwice)
848 || (((unsigned HOST_WIDE_INT) habs_den
849 == (unsigned HOST_WIDE_INT) htwice)
850 && (labs_den <= ltwice)))
854 add_double (*lquo, *hquo,
855 (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
858 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
870 /* Compute true remainder: rem = num - (quo * den) */
871 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
872 neg_double (*lrem, *hrem, lrem, hrem);
873 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
877 /* If ARG2 divides ARG1 with zero remainder, carries out the division
878 of type CODE and returns the quotient.
879 Otherwise returns NULL_TREE. */
882 div_if_zero_remainder (enum tree_code code, const_tree arg1, const_tree arg2)
884 unsigned HOST_WIDE_INT int1l, int2l;
885 HOST_WIDE_INT int1h, int2h;
886 unsigned HOST_WIDE_INT quol, reml;
887 HOST_WIDE_INT quoh, remh;
890 /* The sign of the division is according to operand two, that
891 does the correct thing for POINTER_PLUS_EXPR where we want
892 a signed division. */
893 uns = TYPE_UNSIGNED (TREE_TYPE (arg2));
894 if (TREE_CODE (TREE_TYPE (arg2)) == INTEGER_TYPE
895 && TYPE_IS_SIZETYPE (TREE_TYPE (arg2)))
898 int1l = TREE_INT_CST_LOW (arg1);
899 int1h = TREE_INT_CST_HIGH (arg1);
900 int2l = TREE_INT_CST_LOW (arg2);
901 int2h = TREE_INT_CST_HIGH (arg2);
903 div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
904 &quol, &quoh, &reml, &remh);
905 if (remh != 0 || reml != 0)
908 return build_int_cst_wide (TREE_TYPE (arg1), quol, quoh);
911 /* This is nonzero if we should defer warnings about undefined
912 overflow. This facility exists because these warnings are a
913 special case. The code to estimate loop iterations does not want
914 to issue any warnings, since it works with expressions which do not
915 occur in user code. Various bits of cleanup code call fold(), but
916 only use the result if it has certain characteristics (e.g., is a
917 constant); that code only wants to issue a warning if the result is
920 static int fold_deferring_overflow_warnings;
922 /* If a warning about undefined overflow is deferred, this is the
923 warning. Note that this may cause us to turn two warnings into
924 one, but that is fine since it is sufficient to only give one
925 warning per expression. */
927 static const char* fold_deferred_overflow_warning;
929 /* If a warning about undefined overflow is deferred, this is the
930 level at which the warning should be emitted. */
932 static enum warn_strict_overflow_code fold_deferred_overflow_code;
934 /* Start deferring overflow warnings. We could use a stack here to
935 permit nested calls, but at present it is not necessary. */
938 fold_defer_overflow_warnings (void)
940 ++fold_deferring_overflow_warnings;
943 /* Stop deferring overflow warnings. If there is a pending warning,
944 and ISSUE is true, then issue the warning if appropriate. STMT is
945 the statement with which the warning should be associated (used for
946 location information); STMT may be NULL. CODE is the level of the
947 warning--a warn_strict_overflow_code value. This function will use
948 the smaller of CODE and the deferred code when deciding whether to
949 issue the warning. CODE may be zero to mean to always use the
953 fold_undefer_overflow_warnings (bool issue, const_gimple stmt, int code)
958 gcc_assert (fold_deferring_overflow_warnings > 0);
959 --fold_deferring_overflow_warnings;
960 if (fold_deferring_overflow_warnings > 0)
962 if (fold_deferred_overflow_warning != NULL
964 && code < (int) fold_deferred_overflow_code)
965 fold_deferred_overflow_code = (enum warn_strict_overflow_code) code;
969 warnmsg = fold_deferred_overflow_warning;
970 fold_deferred_overflow_warning = NULL;
972 if (!issue || warnmsg == NULL)
975 if (gimple_no_warning_p (stmt))
978 /* Use the smallest code level when deciding to issue the
980 if (code == 0 || code > (int) fold_deferred_overflow_code)
981 code = fold_deferred_overflow_code;
983 if (!issue_strict_overflow_warning (code))
987 locus = input_location;
989 locus = gimple_location (stmt);
990 warning_at (locus, OPT_Wstrict_overflow, "%s", warnmsg);
993 /* Stop deferring overflow warnings, ignoring any deferred
997 fold_undefer_and_ignore_overflow_warnings (void)
999 fold_undefer_overflow_warnings (false, NULL, 0);
1002 /* Whether we are deferring overflow warnings. */
1005 fold_deferring_overflow_warnings_p (void)
1007 return fold_deferring_overflow_warnings > 0;
1010 /* This is called when we fold something based on the fact that signed
1011 overflow is undefined. */
1014 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
1016 if (fold_deferring_overflow_warnings > 0)
1018 if (fold_deferred_overflow_warning == NULL
1019 || wc < fold_deferred_overflow_code)
1021 fold_deferred_overflow_warning = gmsgid;
1022 fold_deferred_overflow_code = wc;
1025 else if (issue_strict_overflow_warning (wc))
1026 warning (OPT_Wstrict_overflow, gmsgid);
1029 /* Return true if the built-in mathematical function specified by CODE
1030 is odd, i.e. -f(x) == f(-x). */
1033 negate_mathfn_p (enum built_in_function code)
1037 CASE_FLT_FN (BUILT_IN_ASIN):
1038 CASE_FLT_FN (BUILT_IN_ASINH):
1039 CASE_FLT_FN (BUILT_IN_ATAN):
1040 CASE_FLT_FN (BUILT_IN_ATANH):
1041 CASE_FLT_FN (BUILT_IN_CASIN):
1042 CASE_FLT_FN (BUILT_IN_CASINH):
1043 CASE_FLT_FN (BUILT_IN_CATAN):
1044 CASE_FLT_FN (BUILT_IN_CATANH):
1045 CASE_FLT_FN (BUILT_IN_CBRT):
1046 CASE_FLT_FN (BUILT_IN_CPROJ):
1047 CASE_FLT_FN (BUILT_IN_CSIN):
1048 CASE_FLT_FN (BUILT_IN_CSINH):
1049 CASE_FLT_FN (BUILT_IN_CTAN):
1050 CASE_FLT_FN (BUILT_IN_CTANH):
1051 CASE_FLT_FN (BUILT_IN_ERF):
1052 CASE_FLT_FN (BUILT_IN_LLROUND):
1053 CASE_FLT_FN (BUILT_IN_LROUND):
1054 CASE_FLT_FN (BUILT_IN_ROUND):
1055 CASE_FLT_FN (BUILT_IN_SIN):
1056 CASE_FLT_FN (BUILT_IN_SINH):
1057 CASE_FLT_FN (BUILT_IN_TAN):
1058 CASE_FLT_FN (BUILT_IN_TANH):
1059 CASE_FLT_FN (BUILT_IN_TRUNC):
1062 CASE_FLT_FN (BUILT_IN_LLRINT):
1063 CASE_FLT_FN (BUILT_IN_LRINT):
1064 CASE_FLT_FN (BUILT_IN_NEARBYINT):
1065 CASE_FLT_FN (BUILT_IN_RINT):
1066 return !flag_rounding_math;
1074 /* Check whether we may negate an integer constant T without causing
1078 may_negate_without_overflow_p (const_tree t)
1080 unsigned HOST_WIDE_INT val;
1084 gcc_assert (TREE_CODE (t) == INTEGER_CST);
1086 type = TREE_TYPE (t);
1087 if (TYPE_UNSIGNED (type))
1090 prec = TYPE_PRECISION (type);
1091 if (prec > HOST_BITS_PER_WIDE_INT)
1093 if (TREE_INT_CST_LOW (t) != 0)
1095 prec -= HOST_BITS_PER_WIDE_INT;
1096 val = TREE_INT_CST_HIGH (t);
1099 val = TREE_INT_CST_LOW (t);
1100 if (prec < HOST_BITS_PER_WIDE_INT)
1101 val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
1102 return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
1105 /* Determine whether an expression T can be cheaply negated using
1106 the function negate_expr without introducing undefined overflow. */
1109 negate_expr_p (tree t)
1116 type = TREE_TYPE (t);
1118 STRIP_SIGN_NOPS (t);
1119 switch (TREE_CODE (t))
1122 if (TYPE_OVERFLOW_WRAPS (type))
1125 /* Check that -CST will not overflow type. */
1126 return may_negate_without_overflow_p (t);
1128 return (INTEGRAL_TYPE_P (type)
1129 && TYPE_OVERFLOW_WRAPS (type));
1137 return negate_expr_p (TREE_REALPART (t))
1138 && negate_expr_p (TREE_IMAGPART (t));
1141 return negate_expr_p (TREE_OPERAND (t, 0))
1142 && negate_expr_p (TREE_OPERAND (t, 1));
1145 return negate_expr_p (TREE_OPERAND (t, 0));
1148 if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1149 || HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
1151 /* -(A + B) -> (-B) - A. */
1152 if (negate_expr_p (TREE_OPERAND (t, 1))
1153 && reorder_operands_p (TREE_OPERAND (t, 0),
1154 TREE_OPERAND (t, 1)))
1156 /* -(A + B) -> (-A) - B. */
1157 return negate_expr_p (TREE_OPERAND (t, 0));
1160 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
1161 return !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1162 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
1163 && reorder_operands_p (TREE_OPERAND (t, 0),
1164 TREE_OPERAND (t, 1));
1167 if (TYPE_UNSIGNED (TREE_TYPE (t)))
1173 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
1174 return negate_expr_p (TREE_OPERAND (t, 1))
1175 || negate_expr_p (TREE_OPERAND (t, 0));
1178 case TRUNC_DIV_EXPR:
1179 case ROUND_DIV_EXPR:
1180 case FLOOR_DIV_EXPR:
1182 case EXACT_DIV_EXPR:
1183 /* In general we can't negate A / B, because if A is INT_MIN and
1184 B is 1, we may turn this into INT_MIN / -1 which is undefined
1185 and actually traps on some architectures. But if overflow is
1186 undefined, we can negate, because - (INT_MIN / 1) is an
1188 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
1189 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
1191 return negate_expr_p (TREE_OPERAND (t, 1))
1192 || negate_expr_p (TREE_OPERAND (t, 0));
1195 /* Negate -((double)float) as (double)(-float). */
1196 if (TREE_CODE (type) == REAL_TYPE)
1198 tree tem = strip_float_extensions (t);
1200 return negate_expr_p (tem);
1205 /* Negate -f(x) as f(-x). */
1206 if (negate_mathfn_p (builtin_mathfn_code (t)))
1207 return negate_expr_p (CALL_EXPR_ARG (t, 0));
1211 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
1212 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1214 tree op1 = TREE_OPERAND (t, 1);
1215 if (TREE_INT_CST_HIGH (op1) == 0
1216 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1217 == TREE_INT_CST_LOW (op1))
1228 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
1229 simplification is possible.
1230 If negate_expr_p would return true for T, NULL_TREE will never be
1234 fold_negate_expr (location_t loc, tree t)
1236 tree type = TREE_TYPE (t);
1239 switch (TREE_CODE (t))
1241 /* Convert - (~A) to A + 1. */
1243 if (INTEGRAL_TYPE_P (type))
1244 return fold_build2_loc (loc, PLUS_EXPR, type, TREE_OPERAND (t, 0),
1245 build_int_cst (type, 1));
1249 tem = fold_negate_const (t, type);
1250 if (TREE_OVERFLOW (tem) == TREE_OVERFLOW (t)
1251 || !TYPE_OVERFLOW_TRAPS (type))
1256 tem = fold_negate_const (t, type);
1257 /* Two's complement FP formats, such as c4x, may overflow. */
1258 if (!TREE_OVERFLOW (tem) || !flag_trapping_math)
1263 tem = fold_negate_const (t, type);
1268 tree rpart = negate_expr (TREE_REALPART (t));
1269 tree ipart = negate_expr (TREE_IMAGPART (t));
1271 if ((TREE_CODE (rpart) == REAL_CST
1272 && TREE_CODE (ipart) == REAL_CST)
1273 || (TREE_CODE (rpart) == INTEGER_CST
1274 && TREE_CODE (ipart) == INTEGER_CST))
1275 return build_complex (type, rpart, ipart);
1280 if (negate_expr_p (t))
1281 return fold_build2_loc (loc, COMPLEX_EXPR, type,
1282 fold_negate_expr (loc, TREE_OPERAND (t, 0)),
1283 fold_negate_expr (loc, TREE_OPERAND (t, 1)));
1287 if (negate_expr_p (t))
1288 return fold_build1_loc (loc, CONJ_EXPR, type,
1289 fold_negate_expr (loc, TREE_OPERAND (t, 0)));
1293 return TREE_OPERAND (t, 0);
1296 if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1297 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
1299 /* -(A + B) -> (-B) - A. */
1300 if (negate_expr_p (TREE_OPERAND (t, 1))
1301 && reorder_operands_p (TREE_OPERAND (t, 0),
1302 TREE_OPERAND (t, 1)))
1304 tem = negate_expr (TREE_OPERAND (t, 1));
1305 return fold_build2_loc (loc, MINUS_EXPR, type,
1306 tem, TREE_OPERAND (t, 0));
1309 /* -(A + B) -> (-A) - B. */
1310 if (negate_expr_p (TREE_OPERAND (t, 0)))
1312 tem = negate_expr (TREE_OPERAND (t, 0));
1313 return fold_build2_loc (loc, MINUS_EXPR, type,
1314 tem, TREE_OPERAND (t, 1));
1320 /* - (A - B) -> B - A */
1321 if (!HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1322 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
1323 && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
1324 return fold_build2_loc (loc, MINUS_EXPR, type,
1325 TREE_OPERAND (t, 1), TREE_OPERAND (t, 0));
1329 if (TYPE_UNSIGNED (type))
1335 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type)))
1337 tem = TREE_OPERAND (t, 1);
1338 if (negate_expr_p (tem))
1339 return fold_build2_loc (loc, TREE_CODE (t), type,
1340 TREE_OPERAND (t, 0), negate_expr (tem));
1341 tem = TREE_OPERAND (t, 0);
1342 if (negate_expr_p (tem))
1343 return fold_build2_loc (loc, TREE_CODE (t), type,
1344 negate_expr (tem), TREE_OPERAND (t, 1));
1348 case TRUNC_DIV_EXPR:
1349 case ROUND_DIV_EXPR:
1350 case FLOOR_DIV_EXPR:
1352 case EXACT_DIV_EXPR:
1353 /* In general we can't negate A / B, because if A is INT_MIN and
1354 B is 1, we may turn this into INT_MIN / -1 which is undefined
1355 and actually traps on some architectures. But if overflow is
1356 undefined, we can negate, because - (INT_MIN / 1) is an
1358 if (!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
1360 const char * const warnmsg = G_("assuming signed overflow does not "
1361 "occur when negating a division");
1362 tem = TREE_OPERAND (t, 1);
1363 if (negate_expr_p (tem))
1365 if (INTEGRAL_TYPE_P (type)
1366 && (TREE_CODE (tem) != INTEGER_CST
1367 || integer_onep (tem)))
1368 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
1369 return fold_build2_loc (loc, TREE_CODE (t), type,
1370 TREE_OPERAND (t, 0), negate_expr (tem));
1372 tem = TREE_OPERAND (t, 0);
1373 if (negate_expr_p (tem))
1375 if (INTEGRAL_TYPE_P (type)
1376 && (TREE_CODE (tem) != INTEGER_CST
1377 || tree_int_cst_equal (tem, TYPE_MIN_VALUE (type))))
1378 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MISC);
1379 return fold_build2_loc (loc, TREE_CODE (t), type,
1380 negate_expr (tem), TREE_OPERAND (t, 1));
1386 /* Convert -((double)float) into (double)(-float). */
1387 if (TREE_CODE (type) == REAL_TYPE)
1389 tem = strip_float_extensions (t);
1390 if (tem != t && negate_expr_p (tem))
1391 return fold_convert_loc (loc, type, negate_expr (tem));
1396 /* Negate -f(x) as f(-x). */
1397 if (negate_mathfn_p (builtin_mathfn_code (t))
1398 && negate_expr_p (CALL_EXPR_ARG (t, 0)))
1402 fndecl = get_callee_fndecl (t);
1403 arg = negate_expr (CALL_EXPR_ARG (t, 0));
1404 return build_call_expr_loc (loc, fndecl, 1, arg);
1409 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
1410 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1412 tree op1 = TREE_OPERAND (t, 1);
1413 if (TREE_INT_CST_HIGH (op1) == 0
1414 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1415 == TREE_INT_CST_LOW (op1))
1417 tree ntype = TYPE_UNSIGNED (type)
1418 ? signed_type_for (type)
1419 : unsigned_type_for (type);
1420 tree temp = fold_convert_loc (loc, ntype, TREE_OPERAND (t, 0));
1421 temp = fold_build2_loc (loc, RSHIFT_EXPR, ntype, temp, op1);
1422 return fold_convert_loc (loc, type, temp);
1434 /* Like fold_negate_expr, but return a NEGATE_EXPR tree, if T can not be
1435 negated in a simpler way. Also allow for T to be NULL_TREE, in which case
1436 return NULL_TREE. */
1439 negate_expr (tree t)
1447 loc = EXPR_LOCATION (t);
1448 type = TREE_TYPE (t);
1449 STRIP_SIGN_NOPS (t);
1451 tem = fold_negate_expr (loc, t);
1454 tem = build1 (NEGATE_EXPR, TREE_TYPE (t), t);
1455 SET_EXPR_LOCATION (tem, loc);
1457 return fold_convert_loc (loc, type, tem);
1460 /* Split a tree IN into a constant, literal and variable parts that could be
1461 combined with CODE to make IN. "constant" means an expression with
1462 TREE_CONSTANT but that isn't an actual constant. CODE must be a
1463 commutative arithmetic operation. Store the constant part into *CONP,
1464 the literal in *LITP and return the variable part. If a part isn't
1465 present, set it to null. If the tree does not decompose in this way,
1466 return the entire tree as the variable part and the other parts as null.
1468 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
1469 case, we negate an operand that was subtracted. Except if it is a
1470 literal for which we use *MINUS_LITP instead.
1472 If NEGATE_P is true, we are negating all of IN, again except a literal
1473 for which we use *MINUS_LITP instead.
1475 If IN is itself a literal or constant, return it as appropriate.
1477 Note that we do not guarantee that any of the three values will be the
1478 same type as IN, but they will have the same signedness and mode. */
1481 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
1482 tree *minus_litp, int negate_p)
1490 /* Strip any conversions that don't change the machine mode or signedness. */
1491 STRIP_SIGN_NOPS (in);
1493 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
1494 || TREE_CODE (in) == FIXED_CST)
1496 else if (TREE_CODE (in) == code
1497 || ((! FLOAT_TYPE_P (TREE_TYPE (in)) || flag_associative_math)
1498 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
1499 /* We can associate addition and subtraction together (even
1500 though the C standard doesn't say so) for integers because
1501 the value is not affected. For reals, the value might be
1502 affected, so we can't. */
1503 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1504 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1506 tree op0 = TREE_OPERAND (in, 0);
1507 tree op1 = TREE_OPERAND (in, 1);
1508 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1509 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1511 /* First see if either of the operands is a literal, then a constant. */
1512 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
1513 || TREE_CODE (op0) == FIXED_CST)
1514 *litp = op0, op0 = 0;
1515 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
1516 || TREE_CODE (op1) == FIXED_CST)
1517 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1519 if (op0 != 0 && TREE_CONSTANT (op0))
1520 *conp = op0, op0 = 0;
1521 else if (op1 != 0 && TREE_CONSTANT (op1))
1522 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1524 /* If we haven't dealt with either operand, this is not a case we can
1525 decompose. Otherwise, VAR is either of the ones remaining, if any. */
1526 if (op0 != 0 && op1 != 0)
1531 var = op1, neg_var_p = neg1_p;
1533 /* Now do any needed negations. */
1535 *minus_litp = *litp, *litp = 0;
1537 *conp = negate_expr (*conp);
1539 var = negate_expr (var);
1541 else if (TREE_CONSTANT (in))
1549 *minus_litp = *litp, *litp = 0;
1550 else if (*minus_litp)
1551 *litp = *minus_litp, *minus_litp = 0;
1552 *conp = negate_expr (*conp);
1553 var = negate_expr (var);
1559 /* Re-associate trees split by the above function. T1 and T2 are
1560 either expressions to associate or null. Return the new
1561 expression, if any. LOC is the location of the new expression. If
1562 we build an operation, do it in TYPE and with CODE. */
1565 associate_trees (location_t loc, tree t1, tree t2, enum tree_code code, tree type)
1574 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1575 try to fold this since we will have infinite recursion. But do
1576 deal with any NEGATE_EXPRs. */
1577 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1578 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1580 if (code == PLUS_EXPR)
1582 if (TREE_CODE (t1) == NEGATE_EXPR)
1583 tem = build2 (MINUS_EXPR, type, fold_convert_loc (loc, type, t2),
1584 fold_convert_loc (loc, type, TREE_OPERAND (t1, 0)));
1585 else if (TREE_CODE (t2) == NEGATE_EXPR)
1586 tem = build2 (MINUS_EXPR, type, fold_convert_loc (loc, type, t1),
1587 fold_convert_loc (loc, type, TREE_OPERAND (t2, 0)));
1588 else if (integer_zerop (t2))
1589 return fold_convert_loc (loc, type, t1);
1591 else if (code == MINUS_EXPR)
1593 if (integer_zerop (t2))
1594 return fold_convert_loc (loc, type, t1);
1597 tem = build2 (code, type, fold_convert_loc (loc, type, t1),
1598 fold_convert_loc (loc, type, t2));
1599 goto associate_trees_exit;
1602 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, t1),
1603 fold_convert_loc (loc, type, t2));
1604 associate_trees_exit:
1605 protected_set_expr_location (tem, loc);
1609 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
1610 for use in int_const_binop, size_binop and size_diffop. */
1613 int_binop_types_match_p (enum tree_code code, const_tree type1, const_tree type2)
1615 if (TREE_CODE (type1) != INTEGER_TYPE && !POINTER_TYPE_P (type1))
1617 if (TREE_CODE (type2) != INTEGER_TYPE && !POINTER_TYPE_P (type2))
1632 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
1633 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1634 && TYPE_MODE (type1) == TYPE_MODE (type2);
1638 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1639 to produce a new constant. Return NULL_TREE if we don't know how
1640 to evaluate CODE at compile-time.
1642 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1645 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2, int notrunc)
1647 unsigned HOST_WIDE_INT int1l, int2l;
1648 HOST_WIDE_INT int1h, int2h;
1649 unsigned HOST_WIDE_INT low;
1651 unsigned HOST_WIDE_INT garbagel;
1652 HOST_WIDE_INT garbageh;
1654 tree type = TREE_TYPE (arg1);
1655 int uns = TYPE_UNSIGNED (type);
1657 = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
1660 int1l = TREE_INT_CST_LOW (arg1);
1661 int1h = TREE_INT_CST_HIGH (arg1);
1662 int2l = TREE_INT_CST_LOW (arg2);
1663 int2h = TREE_INT_CST_HIGH (arg2);
1668 low = int1l | int2l, hi = int1h | int2h;
1672 low = int1l ^ int2l, hi = int1h ^ int2h;
1676 low = int1l & int2l, hi = int1h & int2h;
1682 /* It's unclear from the C standard whether shifts can overflow.
1683 The following code ignores overflow; perhaps a C standard
1684 interpretation ruling is needed. */
1685 lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1692 lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1697 overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1701 neg_double (int2l, int2h, &low, &hi);
1702 add_double (int1l, int1h, low, hi, &low, &hi);
1703 overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
1707 overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1710 case TRUNC_DIV_EXPR:
1711 case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1712 case EXACT_DIV_EXPR:
1713 /* This is a shortcut for a common special case. */
1714 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1715 && !TREE_OVERFLOW (arg1)
1716 && !TREE_OVERFLOW (arg2)
1717 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1719 if (code == CEIL_DIV_EXPR)
1722 low = int1l / int2l, hi = 0;
1726 /* ... fall through ... */
1728 case ROUND_DIV_EXPR:
1729 if (int2h == 0 && int2l == 0)
1731 if (int2h == 0 && int2l == 1)
1733 low = int1l, hi = int1h;
1736 if (int1l == int2l && int1h == int2h
1737 && ! (int1l == 0 && int1h == 0))
1742 overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
1743 &low, &hi, &garbagel, &garbageh);
1746 case TRUNC_MOD_EXPR:
1747 case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1748 /* This is a shortcut for a common special case. */
1749 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1750 && !TREE_OVERFLOW (arg1)
1751 && !TREE_OVERFLOW (arg2)
1752 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1754 if (code == CEIL_MOD_EXPR)
1756 low = int1l % int2l, hi = 0;
1760 /* ... fall through ... */
1762 case ROUND_MOD_EXPR:
1763 if (int2h == 0 && int2l == 0)
1765 overflow = div_and_round_double (code, uns,
1766 int1l, int1h, int2l, int2h,
1767 &garbagel, &garbageh, &low, &hi);
1773 low = (((unsigned HOST_WIDE_INT) int1h
1774 < (unsigned HOST_WIDE_INT) int2h)
1775 || (((unsigned HOST_WIDE_INT) int1h
1776 == (unsigned HOST_WIDE_INT) int2h)
1779 low = (int1h < int2h
1780 || (int1h == int2h && int1l < int2l));
1782 if (low == (code == MIN_EXPR))
1783 low = int1l, hi = int1h;
1785 low = int2l, hi = int2h;
1794 t = build_int_cst_wide (TREE_TYPE (arg1), low, hi);
1796 /* Propagate overflow flags ourselves. */
1797 if (((!uns || is_sizetype) && overflow)
1798 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1801 TREE_OVERFLOW (t) = 1;
1805 t = force_fit_type_double (TREE_TYPE (arg1), low, hi, 1,
1806 ((!uns || is_sizetype) && overflow)
1807 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1812 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1813 constant. We assume ARG1 and ARG2 have the same data type, or at least
1814 are the same kind of constant and the same machine mode. Return zero if
1815 combining the constants is not allowed in the current operating mode.
1817 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1820 const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1822 /* Sanity check for the recursive cases. */
1829 if (TREE_CODE (arg1) == INTEGER_CST)
1830 return int_const_binop (code, arg1, arg2, notrunc);
1832 if (TREE_CODE (arg1) == REAL_CST)
1834 enum machine_mode mode;
1837 REAL_VALUE_TYPE value;
1838 REAL_VALUE_TYPE result;
1842 /* The following codes are handled by real_arithmetic. */
1857 d1 = TREE_REAL_CST (arg1);
1858 d2 = TREE_REAL_CST (arg2);
1860 type = TREE_TYPE (arg1);
1861 mode = TYPE_MODE (type);
1863 /* Don't perform operation if we honor signaling NaNs and
1864 either operand is a NaN. */
1865 if (HONOR_SNANS (mode)
1866 && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1869 /* Don't perform operation if it would raise a division
1870 by zero exception. */
1871 if (code == RDIV_EXPR
1872 && REAL_VALUES_EQUAL (d2, dconst0)
1873 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1876 /* If either operand is a NaN, just return it. Otherwise, set up
1877 for floating-point trap; we return an overflow. */
1878 if (REAL_VALUE_ISNAN (d1))
1880 else if (REAL_VALUE_ISNAN (d2))
1883 inexact = real_arithmetic (&value, code, &d1, &d2);
1884 real_convert (&result, mode, &value);
1886 /* Don't constant fold this floating point operation if
1887 the result has overflowed and flag_trapping_math. */
1888 if (flag_trapping_math
1889 && MODE_HAS_INFINITIES (mode)
1890 && REAL_VALUE_ISINF (result)
1891 && !REAL_VALUE_ISINF (d1)
1892 && !REAL_VALUE_ISINF (d2))
1895 /* Don't constant fold this floating point operation if the
1896 result may dependent upon the run-time rounding mode and
1897 flag_rounding_math is set, or if GCC's software emulation
1898 is unable to accurately represent the result. */
1899 if ((flag_rounding_math
1900 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1901 && (inexact || !real_identical (&result, &value)))
1904 t = build_real (type, result);
1906 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1910 if (TREE_CODE (arg1) == FIXED_CST)
1912 FIXED_VALUE_TYPE f1;
1913 FIXED_VALUE_TYPE f2;
1914 FIXED_VALUE_TYPE result;
1919 /* The following codes are handled by fixed_arithmetic. */
1925 case TRUNC_DIV_EXPR:
1926 f2 = TREE_FIXED_CST (arg2);
1931 f2.data.high = TREE_INT_CST_HIGH (arg2);
1932 f2.data.low = TREE_INT_CST_LOW (arg2);
1940 f1 = TREE_FIXED_CST (arg1);
1941 type = TREE_TYPE (arg1);
1942 sat_p = TYPE_SATURATING (type);
1943 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1944 t = build_fixed (type, result);
1945 /* Propagate overflow flags. */
1946 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1947 TREE_OVERFLOW (t) = 1;
1951 if (TREE_CODE (arg1) == COMPLEX_CST)
1953 tree type = TREE_TYPE (arg1);
1954 tree r1 = TREE_REALPART (arg1);
1955 tree i1 = TREE_IMAGPART (arg1);
1956 tree r2 = TREE_REALPART (arg2);
1957 tree i2 = TREE_IMAGPART (arg2);
1964 real = const_binop (code, r1, r2, notrunc);
1965 imag = const_binop (code, i1, i2, notrunc);
1969 if (COMPLEX_FLOAT_TYPE_P (type))
1970 return do_mpc_arg2 (arg1, arg2, type,
1971 /* do_nonfinite= */ folding_initializer,
1974 real = const_binop (MINUS_EXPR,
1975 const_binop (MULT_EXPR, r1, r2, notrunc),
1976 const_binop (MULT_EXPR, i1, i2, notrunc),
1978 imag = const_binop (PLUS_EXPR,
1979 const_binop (MULT_EXPR, r1, i2, notrunc),
1980 const_binop (MULT_EXPR, i1, r2, notrunc),
1985 if (COMPLEX_FLOAT_TYPE_P (type))
1986 return do_mpc_arg2 (arg1, arg2, type,
1987 /* do_nonfinite= */ folding_initializer,
1990 case TRUNC_DIV_EXPR:
1992 case FLOOR_DIV_EXPR:
1993 case ROUND_DIV_EXPR:
1994 if (flag_complex_method == 0)
1996 /* Keep this algorithm in sync with
1997 tree-complex.c:expand_complex_div_straight().
1999 Expand complex division to scalars, straightforward algorithm.
2000 a / b = ((ar*br + ai*bi)/t) + i((ai*br - ar*bi)/t)
2004 = const_binop (PLUS_EXPR,
2005 const_binop (MULT_EXPR, r2, r2, notrunc),
2006 const_binop (MULT_EXPR, i2, i2, notrunc),
2009 = const_binop (PLUS_EXPR,
2010 const_binop (MULT_EXPR, r1, r2, notrunc),
2011 const_binop (MULT_EXPR, i1, i2, notrunc),
2014 = const_binop (MINUS_EXPR,
2015 const_binop (MULT_EXPR, i1, r2, notrunc),
2016 const_binop (MULT_EXPR, r1, i2, notrunc),
2019 real = const_binop (code, t1, magsquared, notrunc);
2020 imag = const_binop (code, t2, magsquared, notrunc);
2024 /* Keep this algorithm in sync with
2025 tree-complex.c:expand_complex_div_wide().
2027 Expand complex division to scalars, modified algorithm to minimize
2028 overflow with wide input ranges. */
2029 tree compare = fold_build2 (LT_EXPR, boolean_type_node,
2030 fold_abs_const (r2, TREE_TYPE (type)),
2031 fold_abs_const (i2, TREE_TYPE (type)));
2033 if (integer_nonzerop (compare))
2035 /* In the TRUE branch, we compute
2037 div = (br * ratio) + bi;
2038 tr = (ar * ratio) + ai;
2039 ti = (ai * ratio) - ar;
2042 tree ratio = const_binop (code, r2, i2, notrunc);
2043 tree div = const_binop (PLUS_EXPR, i2,
2044 const_binop (MULT_EXPR, r2, ratio,
2047 real = const_binop (MULT_EXPR, r1, ratio, notrunc);
2048 real = const_binop (PLUS_EXPR, real, i1, notrunc);
2049 real = const_binop (code, real, div, notrunc);
2051 imag = const_binop (MULT_EXPR, i1, ratio, notrunc);
2052 imag = const_binop (MINUS_EXPR, imag, r1, notrunc);
2053 imag = const_binop (code, imag, div, notrunc);
2057 /* In the FALSE branch, we compute
2059 divisor = (d * ratio) + c;
2060 tr = (b * ratio) + a;
2061 ti = b - (a * ratio);
2064 tree ratio = const_binop (code, i2, r2, notrunc);
2065 tree div = const_binop (PLUS_EXPR, r2,
2066 const_binop (MULT_EXPR, i2, ratio,
2070 real = const_binop (MULT_EXPR, i1, ratio, notrunc);
2071 real = const_binop (PLUS_EXPR, real, r1, notrunc);
2072 real = const_binop (code, real, div, notrunc);
2074 imag = const_binop (MULT_EXPR, r1, ratio, notrunc);
2075 imag = const_binop (MINUS_EXPR, i1, imag, notrunc);
2076 imag = const_binop (code, imag, div, notrunc);
2086 return build_complex (type, real, imag);
2089 if (TREE_CODE (arg1) == VECTOR_CST)
2091 tree type = TREE_TYPE(arg1);
2092 int count = TYPE_VECTOR_SUBPARTS (type), i;
2093 tree elements1, elements2, list = NULL_TREE;
2095 if(TREE_CODE(arg2) != VECTOR_CST)
2098 elements1 = TREE_VECTOR_CST_ELTS (arg1);
2099 elements2 = TREE_VECTOR_CST_ELTS (arg2);
2101 for (i = 0; i < count; i++)
2103 tree elem1, elem2, elem;
2105 /* The trailing elements can be empty and should be treated as 0 */
2107 elem1 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2110 elem1 = TREE_VALUE(elements1);
2111 elements1 = TREE_CHAIN (elements1);
2115 elem2 = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2118 elem2 = TREE_VALUE(elements2);
2119 elements2 = TREE_CHAIN (elements2);
2122 elem = const_binop (code, elem1, elem2, notrunc);
2124 /* It is possible that const_binop cannot handle the given
2125 code and return NULL_TREE */
2126 if(elem == NULL_TREE)
2129 list = tree_cons (NULL_TREE, elem, list);
2131 return build_vector(type, nreverse(list));
2136 /* Create a size type INT_CST node with NUMBER sign extended. KIND
2137 indicates which particular sizetype to create. */
2140 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
2142 return build_int_cst (sizetype_tab[(int) kind], number);
2145 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
2146 is a tree code. The type of the result is taken from the operands.
2147 Both must be equivalent integer types, ala int_binop_types_match_p.
2148 If the operands are constant, so is the result. */
2151 size_binop_loc (location_t loc, enum tree_code code, tree arg0, tree arg1)
2153 tree type = TREE_TYPE (arg0);
2155 if (arg0 == error_mark_node || arg1 == error_mark_node)
2156 return error_mark_node;
2158 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
2161 /* Handle the special case of two integer constants faster. */
2162 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2164 /* And some specific cases even faster than that. */
2165 if (code == PLUS_EXPR)
2167 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
2169 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2172 else if (code == MINUS_EXPR)
2174 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2177 else if (code == MULT_EXPR)
2179 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
2183 /* Handle general case of two integer constants. */
2184 return int_const_binop (code, arg0, arg1, 0);
2187 return fold_build2_loc (loc, code, type, arg0, arg1);
2190 /* Given two values, either both of sizetype or both of bitsizetype,
2191 compute the difference between the two values. Return the value
2192 in signed type corresponding to the type of the operands. */
2195 size_diffop_loc (location_t loc, tree arg0, tree arg1)
2197 tree type = TREE_TYPE (arg0);
2200 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
2203 /* If the type is already signed, just do the simple thing. */
2204 if (!TYPE_UNSIGNED (type))
2205 return size_binop_loc (loc, MINUS_EXPR, arg0, arg1);
2207 if (type == sizetype)
2209 else if (type == bitsizetype)
2210 ctype = sbitsizetype;
2212 ctype = signed_type_for (type);
2214 /* If either operand is not a constant, do the conversions to the signed
2215 type and subtract. The hardware will do the right thing with any
2216 overflow in the subtraction. */
2217 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2218 return size_binop_loc (loc, MINUS_EXPR,
2219 fold_convert_loc (loc, ctype, arg0),
2220 fold_convert_loc (loc, ctype, arg1));
2222 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2223 Otherwise, subtract the other way, convert to CTYPE (we know that can't
2224 overflow) and negate (which can't either). Special-case a result
2225 of zero while we're here. */
2226 if (tree_int_cst_equal (arg0, arg1))
2227 return build_int_cst (ctype, 0);
2228 else if (tree_int_cst_lt (arg1, arg0))
2229 return fold_convert_loc (loc, ctype,
2230 size_binop_loc (loc, MINUS_EXPR, arg0, arg1));
2232 return size_binop_loc (loc, MINUS_EXPR, build_int_cst (ctype, 0),
2233 fold_convert_loc (loc, ctype,
2234 size_binop_loc (loc,
2239 /* A subroutine of fold_convert_const handling conversions of an
2240 INTEGER_CST to another integer type. */
2243 fold_convert_const_int_from_int (tree type, const_tree arg1)
2247 /* Given an integer constant, make new constant with new type,
2248 appropriately sign-extended or truncated. */
2249 t = force_fit_type_double (type, TREE_INT_CST_LOW (arg1),
2250 TREE_INT_CST_HIGH (arg1),
2251 /* Don't set the overflow when
2252 converting from a pointer, */
2253 !POINTER_TYPE_P (TREE_TYPE (arg1))
2254 /* or to a sizetype with same signedness
2255 and the precision is unchanged.
2256 ??? sizetype is always sign-extended,
2257 but its signedness depends on the
2258 frontend. Thus we see spurious overflows
2259 here if we do not check this. */
2260 && !((TYPE_PRECISION (TREE_TYPE (arg1))
2261 == TYPE_PRECISION (type))
2262 && (TYPE_UNSIGNED (TREE_TYPE (arg1))
2263 == TYPE_UNSIGNED (type))
2264 && ((TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
2265 && TYPE_IS_SIZETYPE (TREE_TYPE (arg1)))
2266 || (TREE_CODE (type) == INTEGER_TYPE
2267 && TYPE_IS_SIZETYPE (type)))),
2268 (TREE_INT_CST_HIGH (arg1) < 0
2269 && (TYPE_UNSIGNED (type)
2270 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2271 | TREE_OVERFLOW (arg1));
2276 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2277 to an integer type. */
2280 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2285 /* The following code implements the floating point to integer
2286 conversion rules required by the Java Language Specification,
2287 that IEEE NaNs are mapped to zero and values that overflow
2288 the target precision saturate, i.e. values greater than
2289 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2290 are mapped to INT_MIN. These semantics are allowed by the
2291 C and C++ standards that simply state that the behavior of
2292 FP-to-integer conversion is unspecified upon overflow. */
2294 HOST_WIDE_INT high, low;
2296 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2300 case FIX_TRUNC_EXPR:
2301 real_trunc (&r, VOIDmode, &x);
2308 /* If R is NaN, return zero and show we have an overflow. */
2309 if (REAL_VALUE_ISNAN (r))
2316 /* See if R is less than the lower bound or greater than the
2321 tree lt = TYPE_MIN_VALUE (type);
2322 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2323 if (REAL_VALUES_LESS (r, l))
2326 high = TREE_INT_CST_HIGH (lt);
2327 low = TREE_INT_CST_LOW (lt);
2333 tree ut = TYPE_MAX_VALUE (type);
2336 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2337 if (REAL_VALUES_LESS (u, r))
2340 high = TREE_INT_CST_HIGH (ut);
2341 low = TREE_INT_CST_LOW (ut);
2347 REAL_VALUE_TO_INT (&low, &high, r);
2349 t = force_fit_type_double (type, low, high, -1,
2350 overflow | TREE_OVERFLOW (arg1));
2354 /* A subroutine of fold_convert_const handling conversions of a
2355 FIXED_CST to an integer type. */
2358 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2361 double_int temp, temp_trunc;
2364 /* Right shift FIXED_CST to temp by fbit. */
2365 temp = TREE_FIXED_CST (arg1).data;
2366 mode = TREE_FIXED_CST (arg1).mode;
2367 if (GET_MODE_FBIT (mode) < 2 * HOST_BITS_PER_WIDE_INT)
2369 lshift_double (temp.low, temp.high,
2370 - GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2371 &temp.low, &temp.high, SIGNED_FIXED_POINT_MODE_P (mode));
2373 /* Left shift temp to temp_trunc by fbit. */
2374 lshift_double (temp.low, temp.high,
2375 GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2376 &temp_trunc.low, &temp_trunc.high,
2377 SIGNED_FIXED_POINT_MODE_P (mode));
2384 temp_trunc.high = 0;
2387 /* If FIXED_CST is negative, we need to round the value toward 0.
2388 By checking if the fractional bits are not zero to add 1 to temp. */
2389 if (SIGNED_FIXED_POINT_MODE_P (mode) && temp_trunc.high < 0
2390 && !double_int_equal_p (TREE_FIXED_CST (arg1).data, temp_trunc))
2395 temp = double_int_add (temp, one);
2398 /* Given a fixed-point constant, make new constant with new type,
2399 appropriately sign-extended or truncated. */
2400 t = force_fit_type_double (type, temp.low, temp.high, -1,
2402 && (TYPE_UNSIGNED (type)
2403 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2404 | TREE_OVERFLOW (arg1));
2409 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2410 to another floating point type. */
2413 fold_convert_const_real_from_real (tree type, const_tree arg1)
2415 REAL_VALUE_TYPE value;
2418 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2419 t = build_real (type, value);
2421 /* If converting an infinity or NAN to a representation that doesn't
2422 have one, set the overflow bit so that we can produce some kind of
2423 error message at the appropriate point if necessary. It's not the
2424 most user-friendly message, but it's better than nothing. */
2425 if (REAL_VALUE_ISINF (TREE_REAL_CST (arg1))
2426 && !MODE_HAS_INFINITIES (TYPE_MODE (type)))
2427 TREE_OVERFLOW (t) = 1;
2428 else if (REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
2429 && !MODE_HAS_NANS (TYPE_MODE (type)))
2430 TREE_OVERFLOW (t) = 1;
2431 /* Regular overflow, conversion produced an infinity in a mode that
2432 can't represent them. */
2433 else if (!MODE_HAS_INFINITIES (TYPE_MODE (type))
2434 && REAL_VALUE_ISINF (value)
2435 && !REAL_VALUE_ISINF (TREE_REAL_CST (arg1)))
2436 TREE_OVERFLOW (t) = 1;
2438 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2442 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2443 to a floating point type. */
2446 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2448 REAL_VALUE_TYPE value;
2451 real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
2452 t = build_real (type, value);
2454 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2458 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2459 to another fixed-point type. */
2462 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2464 FIXED_VALUE_TYPE value;
2468 overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2469 TYPE_SATURATING (type));
2470 t = build_fixed (type, value);
2472 /* Propagate overflow flags. */
2473 if (overflow_p | TREE_OVERFLOW (arg1))
2474 TREE_OVERFLOW (t) = 1;
2478 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2479 to a fixed-point type. */
2482 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2484 FIXED_VALUE_TYPE value;
2488 overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type),
2489 TREE_INT_CST (arg1),
2490 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2491 TYPE_SATURATING (type));
2492 t = build_fixed (type, value);
2494 /* Propagate overflow flags. */
2495 if (overflow_p | TREE_OVERFLOW (arg1))
2496 TREE_OVERFLOW (t) = 1;
2500 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2501 to a fixed-point type. */
2504 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2506 FIXED_VALUE_TYPE value;
2510 overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2511 &TREE_REAL_CST (arg1),
2512 TYPE_SATURATING (type));
2513 t = build_fixed (type, value);
2515 /* Propagate overflow flags. */
2516 if (overflow_p | TREE_OVERFLOW (arg1))
2517 TREE_OVERFLOW (t) = 1;
2521 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2522 type TYPE. If no simplification can be done return NULL_TREE. */
2525 fold_convert_const (enum tree_code code, tree type, tree arg1)
2527 if (TREE_TYPE (arg1) == type)
2530 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2531 || TREE_CODE (type) == OFFSET_TYPE)
2533 if (TREE_CODE (arg1) == INTEGER_CST)
2534 return fold_convert_const_int_from_int (type, arg1);
2535 else if (TREE_CODE (arg1) == REAL_CST)
2536 return fold_convert_const_int_from_real (code, type, arg1);
2537 else if (TREE_CODE (arg1) == FIXED_CST)
2538 return fold_convert_const_int_from_fixed (type, arg1);
2540 else if (TREE_CODE (type) == REAL_TYPE)
2542 if (TREE_CODE (arg1) == INTEGER_CST)
2543 return build_real_from_int_cst (type, arg1);
2544 else if (TREE_CODE (arg1) == REAL_CST)
2545 return fold_convert_const_real_from_real (type, arg1);
2546 else if (TREE_CODE (arg1) == FIXED_CST)
2547 return fold_convert_const_real_from_fixed (type, arg1);
2549 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2551 if (TREE_CODE (arg1) == FIXED_CST)
2552 return fold_convert_const_fixed_from_fixed (type, arg1);
2553 else if (TREE_CODE (arg1) == INTEGER_CST)
2554 return fold_convert_const_fixed_from_int (type, arg1);
2555 else if (TREE_CODE (arg1) == REAL_CST)
2556 return fold_convert_const_fixed_from_real (type, arg1);
2561 /* Construct a vector of zero elements of vector type TYPE. */
2564 build_zero_vector (tree type)
2569 elem = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2570 units = TYPE_VECTOR_SUBPARTS (type);
2573 for (i = 0; i < units; i++)
2574 list = tree_cons (NULL_TREE, elem, list);
2575 return build_vector (type, list);
2578 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2581 fold_convertible_p (const_tree type, const_tree arg)
2583 tree orig = TREE_TYPE (arg);
2588 if (TREE_CODE (arg) == ERROR_MARK
2589 || TREE_CODE (type) == ERROR_MARK
2590 || TREE_CODE (orig) == ERROR_MARK)
2593 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2596 switch (TREE_CODE (type))
2598 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2599 case POINTER_TYPE: case REFERENCE_TYPE:
2601 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2602 || TREE_CODE (orig) == OFFSET_TYPE)
2604 return (TREE_CODE (orig) == VECTOR_TYPE
2605 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2608 case FIXED_POINT_TYPE:
2612 return TREE_CODE (type) == TREE_CODE (orig);
2619 /* Convert expression ARG to type TYPE. Used by the middle-end for
2620 simple conversions in preference to calling the front-end's convert. */
2623 fold_convert_loc (location_t loc, tree type, tree arg)
2625 tree orig = TREE_TYPE (arg);
2631 if (TREE_CODE (arg) == ERROR_MARK
2632 || TREE_CODE (type) == ERROR_MARK
2633 || TREE_CODE (orig) == ERROR_MARK)
2634 return error_mark_node;
2636 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2637 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2639 switch (TREE_CODE (type))
2642 case REFERENCE_TYPE:
2643 /* Handle conversions between pointers to different address spaces. */
2644 if (POINTER_TYPE_P (orig)
2645 && (TYPE_ADDR_SPACE (TREE_TYPE (type))
2646 != TYPE_ADDR_SPACE (TREE_TYPE (orig))))
2647 return fold_build1_loc (loc, ADDR_SPACE_CONVERT_EXPR, type, arg);
2650 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2652 if (TREE_CODE (arg) == INTEGER_CST)
2654 tem = fold_convert_const (NOP_EXPR, type, arg);
2655 if (tem != NULL_TREE)
2658 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2659 || TREE_CODE (orig) == OFFSET_TYPE)
2660 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2661 if (TREE_CODE (orig) == COMPLEX_TYPE)
2662 return fold_convert_loc (loc, type,
2663 fold_build1_loc (loc, REALPART_EXPR,
2664 TREE_TYPE (orig), arg));
2665 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2666 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2667 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2670 if (TREE_CODE (arg) == INTEGER_CST)
2672 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2673 if (tem != NULL_TREE)
2676 else if (TREE_CODE (arg) == REAL_CST)
2678 tem = fold_convert_const (NOP_EXPR, type, arg);
2679 if (tem != NULL_TREE)
2682 else if (TREE_CODE (arg) == FIXED_CST)
2684 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2685 if (tem != NULL_TREE)
2689 switch (TREE_CODE (orig))
2692 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2693 case POINTER_TYPE: case REFERENCE_TYPE:
2694 return fold_build1_loc (loc, FLOAT_EXPR, type, arg);
2697 return fold_build1_loc (loc, NOP_EXPR, type, arg);
2699 case FIXED_POINT_TYPE:
2700 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2703 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2704 return fold_convert_loc (loc, type, tem);
2710 case FIXED_POINT_TYPE:
2711 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2712 || TREE_CODE (arg) == REAL_CST)
2714 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2715 if (tem != NULL_TREE)
2716 goto fold_convert_exit;
2719 switch (TREE_CODE (orig))
2721 case FIXED_POINT_TYPE:
2726 return fold_build1_loc (loc, FIXED_CONVERT_EXPR, type, arg);
2729 tem = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2730 return fold_convert_loc (loc, type, tem);
2737 switch (TREE_CODE (orig))
2740 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2741 case POINTER_TYPE: case REFERENCE_TYPE:
2743 case FIXED_POINT_TYPE:
2744 return fold_build2_loc (loc, COMPLEX_EXPR, type,
2745 fold_convert_loc (loc, TREE_TYPE (type), arg),
2746 fold_convert_loc (loc, TREE_TYPE (type),
2747 integer_zero_node));
2752 if (TREE_CODE (arg) == COMPLEX_EXPR)
2754 rpart = fold_convert_loc (loc, TREE_TYPE (type),
2755 TREE_OPERAND (arg, 0));
2756 ipart = fold_convert_loc (loc, TREE_TYPE (type),
2757 TREE_OPERAND (arg, 1));
2758 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2761 arg = save_expr (arg);
2762 rpart = fold_build1_loc (loc, REALPART_EXPR, TREE_TYPE (orig), arg);
2763 ipart = fold_build1_loc (loc, IMAGPART_EXPR, TREE_TYPE (orig), arg);
2764 rpart = fold_convert_loc (loc, TREE_TYPE (type), rpart);
2765 ipart = fold_convert_loc (loc, TREE_TYPE (type), ipart);
2766 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart, ipart);
2774 if (integer_zerop (arg))
2775 return build_zero_vector (type);
2776 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2777 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2778 || TREE_CODE (orig) == VECTOR_TYPE);
2779 return fold_build1_loc (loc, VIEW_CONVERT_EXPR, type, arg);
2782 tem = fold_ignored_result (arg);
2783 if (TREE_CODE (tem) == MODIFY_EXPR)
2784 goto fold_convert_exit;
2785 return fold_build1_loc (loc, NOP_EXPR, type, tem);
2791 protected_set_expr_location (tem, loc);
2795 /* Return false if expr can be assumed not to be an lvalue, true
2799 maybe_lvalue_p (const_tree x)
2801 /* We only need to wrap lvalue tree codes. */
2802 switch (TREE_CODE (x))
2813 case ALIGN_INDIRECT_REF:
2814 case MISALIGNED_INDIRECT_REF:
2816 case ARRAY_RANGE_REF:
2822 case PREINCREMENT_EXPR:
2823 case PREDECREMENT_EXPR:
2825 case TRY_CATCH_EXPR:
2826 case WITH_CLEANUP_EXPR:
2835 /* Assume the worst for front-end tree codes. */
2836 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2844 /* Return an expr equal to X but certainly not valid as an lvalue. */
2847 non_lvalue_loc (location_t loc, tree x)
2849 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2854 if (! maybe_lvalue_p (x))
2856 x = build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2857 SET_EXPR_LOCATION (x, loc);
2861 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2862 Zero means allow extended lvalues. */
2864 int pedantic_lvalues;
2866 /* When pedantic, return an expr equal to X but certainly not valid as a
2867 pedantic lvalue. Otherwise, return X. */
2870 pedantic_non_lvalue_loc (location_t loc, tree x)
2872 if (pedantic_lvalues)
2873 return non_lvalue_loc (loc, x);
2874 protected_set_expr_location (x, loc);
2878 /* Given a tree comparison code, return the code that is the logical inverse
2879 of the given code. It is not safe to do this for floating-point
2880 comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2881 as well: if reversing the comparison is unsafe, return ERROR_MARK. */
2884 invert_tree_comparison (enum tree_code code, bool honor_nans)
2886 if (honor_nans && flag_trapping_math)
2896 return honor_nans ? UNLE_EXPR : LE_EXPR;
2898 return honor_nans ? UNLT_EXPR : LT_EXPR;
2900 return honor_nans ? UNGE_EXPR : GE_EXPR;
2902 return honor_nans ? UNGT_EXPR : GT_EXPR;
2916 return UNORDERED_EXPR;
2917 case UNORDERED_EXPR:
2918 return ORDERED_EXPR;
2924 /* Similar, but return the comparison that results if the operands are
2925 swapped. This is safe for floating-point. */
2928 swap_tree_comparison (enum tree_code code)
2935 case UNORDERED_EXPR:
2961 /* Convert a comparison tree code from an enum tree_code representation
2962 into a compcode bit-based encoding. This function is the inverse of
2963 compcode_to_comparison. */
2965 static enum comparison_code
2966 comparison_to_compcode (enum tree_code code)
2983 return COMPCODE_ORD;
2984 case UNORDERED_EXPR:
2985 return COMPCODE_UNORD;
2987 return COMPCODE_UNLT;
2989 return COMPCODE_UNEQ;
2991 return COMPCODE_UNLE;
2993 return COMPCODE_UNGT;
2995 return COMPCODE_LTGT;
2997 return COMPCODE_UNGE;
3003 /* Convert a compcode bit-based encoding of a comparison operator back
3004 to GCC's enum tree_code representation. This function is the
3005 inverse of comparison_to_compcode. */
3007 static enum tree_code
3008 compcode_to_comparison (enum comparison_code code)
3025 return ORDERED_EXPR;
3026 case COMPCODE_UNORD:
3027 return UNORDERED_EXPR;
3045 /* Return a tree for the comparison which is the combination of
3046 doing the AND or OR (depending on CODE) of the two operations LCODE
3047 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
3048 the possibility of trapping if the mode has NaNs, and return NULL_TREE
3049 if this makes the transformation invalid. */
3052 combine_comparisons (location_t loc,
3053 enum tree_code code, enum tree_code lcode,
3054 enum tree_code rcode, tree truth_type,
3055 tree ll_arg, tree lr_arg)
3057 bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
3058 enum comparison_code lcompcode = comparison_to_compcode (lcode);
3059 enum comparison_code rcompcode = comparison_to_compcode (rcode);
3064 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
3065 compcode = lcompcode & rcompcode;
3068 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
3069 compcode = lcompcode | rcompcode;
3078 /* Eliminate unordered comparisons, as well as LTGT and ORD
3079 which are not used unless the mode has NaNs. */
3080 compcode &= ~COMPCODE_UNORD;
3081 if (compcode == COMPCODE_LTGT)
3082 compcode = COMPCODE_NE;
3083 else if (compcode == COMPCODE_ORD)
3084 compcode = COMPCODE_TRUE;
3086 else if (flag_trapping_math)
3088 /* Check that the original operation and the optimized ones will trap
3089 under the same condition. */
3090 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
3091 && (lcompcode != COMPCODE_EQ)
3092 && (lcompcode != COMPCODE_ORD);
3093 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
3094 && (rcompcode != COMPCODE_EQ)
3095 && (rcompcode != COMPCODE_ORD);
3096 bool trap = (compcode & COMPCODE_UNORD) == 0
3097 && (compcode != COMPCODE_EQ)
3098 && (compcode != COMPCODE_ORD);
3100 /* In a short-circuited boolean expression the LHS might be
3101 such that the RHS, if evaluated, will never trap. For
3102 example, in ORD (x, y) && (x < y), we evaluate the RHS only
3103 if neither x nor y is NaN. (This is a mixed blessing: for
3104 example, the expression above will never trap, hence
3105 optimizing it to x < y would be invalid). */
3106 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
3107 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
3110 /* If the comparison was short-circuited, and only the RHS
3111 trapped, we may now generate a spurious trap. */
3113 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3116 /* If we changed the conditions that cause a trap, we lose. */
3117 if ((ltrap || rtrap) != trap)
3121 if (compcode == COMPCODE_TRUE)
3122 return constant_boolean_node (true, truth_type);
3123 else if (compcode == COMPCODE_FALSE)
3124 return constant_boolean_node (false, truth_type);
3127 enum tree_code tcode;
3129 tcode = compcode_to_comparison ((enum comparison_code) compcode);
3130 return fold_build2_loc (loc, tcode, truth_type, ll_arg, lr_arg);
3134 /* Return nonzero if two operands (typically of the same tree node)
3135 are necessarily equal. If either argument has side-effects this
3136 function returns zero. FLAGS modifies behavior as follows:
3138 If OEP_ONLY_CONST is set, only return nonzero for constants.
3139 This function tests whether the operands are indistinguishable;
3140 it does not test whether they are equal using C's == operation.
3141 The distinction is important for IEEE floating point, because
3142 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
3143 (2) two NaNs may be indistinguishable, but NaN!=NaN.
3145 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
3146 even though it may hold multiple values during a function.
3147 This is because a GCC tree node guarantees that nothing else is
3148 executed between the evaluation of its "operands" (which may often
3149 be evaluated in arbitrary order). Hence if the operands themselves
3150 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
3151 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
3152 unset means assuming isochronic (or instantaneous) tree equivalence.
3153 Unless comparing arbitrary expression trees, such as from different
3154 statements, this flag can usually be left unset.
3156 If OEP_PURE_SAME is set, then pure functions with identical arguments
3157 are considered the same. It is used when the caller has other ways
3158 to ensure that global memory is unchanged in between. */
3161 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
3163 /* If either is ERROR_MARK, they aren't equal. */
3164 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK)
3167 /* Check equality of integer constants before bailing out due to
3168 precision differences. */
3169 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3170 return tree_int_cst_equal (arg0, arg1);
3172 /* If both types don't have the same signedness, then we can't consider
3173 them equal. We must check this before the STRIP_NOPS calls
3174 because they may change the signedness of the arguments. As pointers
3175 strictly don't have a signedness, require either two pointers or
3176 two non-pointers as well. */
3177 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
3178 || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
3181 /* We cannot consider pointers to different address space equal. */
3182 if (POINTER_TYPE_P (TREE_TYPE (arg0)) && POINTER_TYPE_P (TREE_TYPE (arg1))
3183 && (TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg0)))
3184 != TYPE_ADDR_SPACE (TREE_TYPE (TREE_TYPE (arg1)))))
3187 /* If both types don't have the same precision, then it is not safe
3189 if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
3195 /* In case both args are comparisons but with different comparison
3196 code, try to swap the comparison operands of one arg to produce
3197 a match and compare that variant. */
3198 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3199 && COMPARISON_CLASS_P (arg0)
3200 && COMPARISON_CLASS_P (arg1))
3202 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3204 if (TREE_CODE (arg0) == swap_code)
3205 return operand_equal_p (TREE_OPERAND (arg0, 0),
3206 TREE_OPERAND (arg1, 1), flags)
3207 && operand_equal_p (TREE_OPERAND (arg0, 1),
3208 TREE_OPERAND (arg1, 0), flags);
3211 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3212 /* This is needed for conversions and for COMPONENT_REF.
3213 Might as well play it safe and always test this. */
3214 || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
3215 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
3216 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
3219 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3220 We don't care about side effects in that case because the SAVE_EXPR
3221 takes care of that for us. In all other cases, two expressions are
3222 equal if they have no side effects. If we have two identical
3223 expressions with side effects that should be treated the same due
3224 to the only side effects being identical SAVE_EXPR's, that will
3225 be detected in the recursive calls below. */
3226 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3227 && (TREE_CODE (arg0) == SAVE_EXPR
3228 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3231 /* Next handle constant cases, those for which we can return 1 even
3232 if ONLY_CONST is set. */
3233 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3234 switch (TREE_CODE (arg0))
3237 return tree_int_cst_equal (arg0, arg1);
3240 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3241 TREE_FIXED_CST (arg1));
3244 if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
3245 TREE_REAL_CST (arg1)))
3249 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
3251 /* If we do not distinguish between signed and unsigned zero,
3252 consider them equal. */
3253 if (real_zerop (arg0) && real_zerop (arg1))
3262 v1 = TREE_VECTOR_CST_ELTS (arg0);
3263 v2 = TREE_VECTOR_CST_ELTS (arg1);
3266 if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
3269 v1 = TREE_CHAIN (v1);
3270 v2 = TREE_CHAIN (v2);
3277 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3279 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3283 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3284 && ! memcmp (TREE_STRING_POINTER (arg0),
3285 TREE_STRING_POINTER (arg1),
3286 TREE_STRING_LENGTH (arg0)));
3289 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3295 if (flags & OEP_ONLY_CONST)
3298 /* Define macros to test an operand from arg0 and arg1 for equality and a
3299 variant that allows null and views null as being different from any
3300 non-null value. In the latter case, if either is null, the both
3301 must be; otherwise, do the normal comparison. */
3302 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3303 TREE_OPERAND (arg1, N), flags)
3305 #define OP_SAME_WITH_NULL(N) \
3306 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3307 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3309 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3312 /* Two conversions are equal only if signedness and modes match. */
3313 switch (TREE_CODE (arg0))
3316 case FIX_TRUNC_EXPR:
3317 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3318 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3328 case tcc_comparison:
3330 if (OP_SAME (0) && OP_SAME (1))
3333 /* For commutative ops, allow the other order. */
3334 return (commutative_tree_code (TREE_CODE (arg0))
3335 && operand_equal_p (TREE_OPERAND (arg0, 0),
3336 TREE_OPERAND (arg1, 1), flags)
3337 && operand_equal_p (TREE_OPERAND (arg0, 1),
3338 TREE_OPERAND (arg1, 0), flags));
3341 /* If either of the pointer (or reference) expressions we are
3342 dereferencing contain a side effect, these cannot be equal. */
3343 if (TREE_SIDE_EFFECTS (arg0)
3344 || TREE_SIDE_EFFECTS (arg1))
3347 switch (TREE_CODE (arg0))
3350 case ALIGN_INDIRECT_REF:
3351 case MISALIGNED_INDIRECT_REF:
3357 case ARRAY_RANGE_REF:
3358 /* Operands 2 and 3 may be null.
3359 Compare the array index by value if it is constant first as we
3360 may have different types but same value here. */
3362 && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3363 TREE_OPERAND (arg1, 1))
3365 && OP_SAME_WITH_NULL (2)
3366 && OP_SAME_WITH_NULL (3));
3369 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3370 may be NULL when we're called to compare MEM_EXPRs. */
3371 return OP_SAME_WITH_NULL (0)
3373 && OP_SAME_WITH_NULL (2);
3376 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3382 case tcc_expression:
3383 switch (TREE_CODE (arg0))
3386 case TRUTH_NOT_EXPR:
3389 case TRUTH_ANDIF_EXPR:
3390 case TRUTH_ORIF_EXPR:
3391 return OP_SAME (0) && OP_SAME (1);
3393 case TRUTH_AND_EXPR:
3395 case TRUTH_XOR_EXPR:
3396 if (OP_SAME (0) && OP_SAME (1))
3399 /* Otherwise take into account this is a commutative operation. */
3400 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3401 TREE_OPERAND (arg1, 1), flags)
3402 && operand_equal_p (TREE_OPERAND (arg0, 1),
3403 TREE_OPERAND (arg1, 0), flags));
3406 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3413 switch (TREE_CODE (arg0))
3416 /* If the CALL_EXPRs call different functions, then they
3417 clearly can not be equal. */
3418 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3423 unsigned int cef = call_expr_flags (arg0);
3424 if (flags & OEP_PURE_SAME)
3425 cef &= ECF_CONST | ECF_PURE;
3432 /* Now see if all the arguments are the same. */
3434 const_call_expr_arg_iterator iter0, iter1;
3436 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3437 a1 = first_const_call_expr_arg (arg1, &iter1);
3439 a0 = next_const_call_expr_arg (&iter0),
3440 a1 = next_const_call_expr_arg (&iter1))
3441 if (! operand_equal_p (a0, a1, flags))
3444 /* If we get here and both argument lists are exhausted
3445 then the CALL_EXPRs are equal. */
3446 return ! (a0 || a1);
3452 case tcc_declaration:
3453 /* Consider __builtin_sqrt equal to sqrt. */
3454 return (TREE_CODE (arg0) == FUNCTION_DECL
3455 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3456 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3457 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3464 #undef OP_SAME_WITH_NULL
3467 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3468 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3470 When in doubt, return 0. */
3473 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3475 int unsignedp1, unsignedpo;
3476 tree primarg0, primarg1, primother;
3477 unsigned int correct_width;
3479 if (operand_equal_p (arg0, arg1, 0))
3482 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3483 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3486 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3487 and see if the inner values are the same. This removes any
3488 signedness comparison, which doesn't matter here. */
3489 primarg0 = arg0, primarg1 = arg1;
3490 STRIP_NOPS (primarg0);
3491 STRIP_NOPS (primarg1);
3492 if (operand_equal_p (primarg0, primarg1, 0))
3495 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3496 actual comparison operand, ARG0.
3498 First throw away any conversions to wider types
3499 already present in the operands. */
3501 primarg1 = get_narrower (arg1, &unsignedp1);
3502 primother = get_narrower (other, &unsignedpo);
3504 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3505 if (unsignedp1 == unsignedpo
3506 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3507 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3509 tree type = TREE_TYPE (arg0);
3511 /* Make sure shorter operand is extended the right way
3512 to match the longer operand. */
3513 primarg1 = fold_convert (signed_or_unsigned_type_for
3514 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3516 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3523 /* See if ARG is an expression that is either a comparison or is performing
3524 arithmetic on comparisons. The comparisons must only be comparing
3525 two different values, which will be stored in *CVAL1 and *CVAL2; if
3526 they are nonzero it means that some operands have already been found.
3527 No variables may be used anywhere else in the expression except in the
3528 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3529 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3531 If this is true, return 1. Otherwise, return zero. */
3534 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3536 enum tree_code code = TREE_CODE (arg);
3537 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3539 /* We can handle some of the tcc_expression cases here. */
3540 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3542 else if (tclass == tcc_expression
3543 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3544 || code == COMPOUND_EXPR))
3545 tclass = tcc_binary;
3547 else if (tclass == tcc_expression && code == SAVE_EXPR
3548 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3550 /* If we've already found a CVAL1 or CVAL2, this expression is
3551 two complex to handle. */
3552 if (*cval1 || *cval2)
3562 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3565 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3566 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3567 cval1, cval2, save_p));
3572 case tcc_expression:
3573 if (code == COND_EXPR)
3574 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3575 cval1, cval2, save_p)
3576 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3577 cval1, cval2, save_p)
3578 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3579 cval1, cval2, save_p));
3582 case tcc_comparison:
3583 /* First see if we can handle the first operand, then the second. For
3584 the second operand, we know *CVAL1 can't be zero. It must be that
3585 one side of the comparison is each of the values; test for the
3586 case where this isn't true by failing if the two operands
3589 if (operand_equal_p (TREE_OPERAND (arg, 0),
3590 TREE_OPERAND (arg, 1), 0))
3594 *cval1 = TREE_OPERAND (arg, 0);
3595 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3597 else if (*cval2 == 0)
3598 *cval2 = TREE_OPERAND (arg, 0);
3599 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3604 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3606 else if (*cval2 == 0)
3607 *cval2 = TREE_OPERAND (arg, 1);
3608 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3620 /* ARG is a tree that is known to contain just arithmetic operations and
3621 comparisons. Evaluate the operations in the tree substituting NEW0 for
3622 any occurrence of OLD0 as an operand of a comparison and likewise for
3626 eval_subst (location_t loc, tree arg, tree old0, tree new0,
3627 tree old1, tree new1)
3629 tree type = TREE_TYPE (arg);
3630 enum tree_code code = TREE_CODE (arg);
3631 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3633 /* We can handle some of the tcc_expression cases here. */
3634 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3636 else if (tclass == tcc_expression
3637 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3638 tclass = tcc_binary;
3643 return fold_build1_loc (loc, code, type,
3644 eval_subst (loc, TREE_OPERAND (arg, 0),
3645 old0, new0, old1, new1));
3648 return fold_build2_loc (loc, code, type,
3649 eval_subst (loc, TREE_OPERAND (arg, 0),
3650 old0, new0, old1, new1),
3651 eval_subst (loc, TREE_OPERAND (arg, 1),
3652 old0, new0, old1, new1));
3654 case tcc_expression:
3658 return eval_subst (loc, TREE_OPERAND (arg, 0), old0, new0,
3662 return eval_subst (loc, TREE_OPERAND (arg, 1), old0, new0,
3666 return fold_build3_loc (loc, code, type,
3667 eval_subst (loc, TREE_OPERAND (arg, 0),
3668 old0, new0, old1, new1),
3669 eval_subst (loc, TREE_OPERAND (arg, 1),
3670 old0, new0, old1, new1),
3671 eval_subst (loc, TREE_OPERAND (arg, 2),
3672 old0, new0, old1, new1));
3676 /* Fall through - ??? */
3678 case tcc_comparison:
3680 tree arg0 = TREE_OPERAND (arg, 0);
3681 tree arg1 = TREE_OPERAND (arg, 1);
3683 /* We need to check both for exact equality and tree equality. The
3684 former will be true if the operand has a side-effect. In that
3685 case, we know the operand occurred exactly once. */
3687 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3689 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3692 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3694 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3697 return fold_build2_loc (loc, code, type, arg0, arg1);
3705 /* Return a tree for the case when the result of an expression is RESULT
3706 converted to TYPE and OMITTED was previously an operand of the expression
3707 but is now not needed (e.g., we folded OMITTED * 0).
3709 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3710 the conversion of RESULT to TYPE. */
3713 omit_one_operand_loc (location_t loc, tree type, tree result, tree omitted)
3715 tree t = fold_convert_loc (loc, type, result);
3717 /* If the resulting operand is an empty statement, just return the omitted
3718 statement casted to void. */
3719 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3721 t = build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3722 goto omit_one_operand_exit;
3725 if (TREE_SIDE_EFFECTS (omitted))
3727 t = build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3728 goto omit_one_operand_exit;
3731 return non_lvalue_loc (loc, t);
3733 omit_one_operand_exit:
3734 protected_set_expr_location (t, loc);
3738 /* Similar, but call pedantic_non_lvalue instead of non_lvalue. */
3741 pedantic_omit_one_operand_loc (location_t loc, tree type, tree result,
3744 tree t = fold_convert_loc (loc, type, result);
3746 /* If the resulting operand is an empty statement, just return the omitted
3747 statement casted to void. */
3748 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3750 t = build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3751 goto pedantic_omit_one_operand_exit;
3754 if (TREE_SIDE_EFFECTS (omitted))
3756 t = build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3757 goto pedantic_omit_one_operand_exit;
3760 return pedantic_non_lvalue_loc (loc, t);
3762 pedantic_omit_one_operand_exit:
3763 protected_set_expr_location (t, loc);
3767 /* Return a tree for the case when the result of an expression is RESULT
3768 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3769 of the expression but are now not needed.
3771 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3772 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3773 evaluated before OMITTED2. Otherwise, if neither has side effects,
3774 just do the conversion of RESULT to TYPE. */
3777 omit_two_operands_loc (location_t loc, tree type, tree result,
3778 tree omitted1, tree omitted2)
3780 tree t = fold_convert_loc (loc, type, result);
3782 if (TREE_SIDE_EFFECTS (omitted2))
3784 t = build2 (COMPOUND_EXPR, type, omitted2, t);
3785 SET_EXPR_LOCATION (t, loc);
3787 if (TREE_SIDE_EFFECTS (omitted1))
3789 t = build2 (COMPOUND_EXPR, type, omitted1, t);
3790 SET_EXPR_LOCATION (t, loc);
3793 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue_loc (loc, t) : t;
3797 /* Return a simplified tree node for the truth-negation of ARG. This
3798 never alters ARG itself. We assume that ARG is an operation that
3799 returns a truth value (0 or 1).
3801 FIXME: one would think we would fold the result, but it causes
3802 problems with the dominator optimizer. */
3805 fold_truth_not_expr (location_t loc, tree arg)
3807 tree t, type = TREE_TYPE (arg);
3808 enum tree_code code = TREE_CODE (arg);
3809 location_t loc1, loc2;
3811 /* If this is a comparison, we can simply invert it, except for
3812 floating-point non-equality comparisons, in which case we just
3813 enclose a TRUTH_NOT_EXPR around what we have. */
3815 if (TREE_CODE_CLASS (code) == tcc_comparison)
3817 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3818 if (FLOAT_TYPE_P (op_type)
3819 && flag_trapping_math
3820 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3821 && code != NE_EXPR && code != EQ_EXPR)
3824 code = invert_tree_comparison (code, HONOR_NANS (TYPE_MODE (op_type)));
3825 if (code == ERROR_MARK)
3828 t = build2 (code, type, TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
3829 SET_EXPR_LOCATION (t, loc);
3836 return constant_boolean_node (integer_zerop (arg), type);
3838 case TRUTH_AND_EXPR:
3839 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3840 loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3841 if (loc1 == UNKNOWN_LOCATION)
3843 if (loc2 == UNKNOWN_LOCATION)
3845 t = build2 (TRUTH_OR_EXPR, type,
3846 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3847 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3851 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3852 loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3853 if (loc1 == UNKNOWN_LOCATION)
3855 if (loc2 == UNKNOWN_LOCATION)
3857 t = build2 (TRUTH_AND_EXPR, type,
3858 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3859 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3862 case TRUTH_XOR_EXPR:
3863 /* Here we can invert either operand. We invert the first operand
3864 unless the second operand is a TRUTH_NOT_EXPR in which case our
3865 result is the XOR of the first operand with the inside of the
3866 negation of the second operand. */
3868 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3869 t = build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3870 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3872 t = build2 (TRUTH_XOR_EXPR, type,
3873 invert_truthvalue_loc (loc, TREE_OPERAND (arg, 0)),
3874 TREE_OPERAND (arg, 1));
3877 case TRUTH_ANDIF_EXPR:
3878 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3879 loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3880 if (loc1 == UNKNOWN_LOCATION)
3882 if (loc2 == UNKNOWN_LOCATION)
3884 t = build2 (TRUTH_ORIF_EXPR, type,
3885 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3886 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3889 case TRUTH_ORIF_EXPR:
3890 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3891 loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3892 if (loc1 == UNKNOWN_LOCATION)
3894 if (loc2 == UNKNOWN_LOCATION)
3896 t = build2 (TRUTH_ANDIF_EXPR, type,
3897 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)),
3898 invert_truthvalue_loc (loc2, TREE_OPERAND (arg, 1)));
3901 case TRUTH_NOT_EXPR:
3902 return TREE_OPERAND (arg, 0);
3906 tree arg1 = TREE_OPERAND (arg, 1);
3907 tree arg2 = TREE_OPERAND (arg, 2);
3909 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3910 loc2 = EXPR_LOCATION (TREE_OPERAND (arg, 2));
3911 if (loc1 == UNKNOWN_LOCATION)
3913 if (loc2 == UNKNOWN_LOCATION)
3916 /* A COND_EXPR may have a throw as one operand, which
3917 then has void type. Just leave void operands
3919 t = build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
3920 VOID_TYPE_P (TREE_TYPE (arg1))
3921 ? arg1 : invert_truthvalue_loc (loc1, arg1),
3922 VOID_TYPE_P (TREE_TYPE (arg2))
3923 ? arg2 : invert_truthvalue_loc (loc2, arg2));
3928 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 1));
3929 if (loc1 == UNKNOWN_LOCATION)
3931 t = build2 (COMPOUND_EXPR, type,
3932 TREE_OPERAND (arg, 0),
3933 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 1)));
3936 case NON_LVALUE_EXPR:
3937 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3938 if (loc1 == UNKNOWN_LOCATION)
3940 return invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0));
3943 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3945 t = build1 (TRUTH_NOT_EXPR, type, arg);
3949 /* ... fall through ... */
3952 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3953 if (loc1 == UNKNOWN_LOCATION)
3955 t = build1 (TREE_CODE (arg), type,
3956 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3960 if (!integer_onep (TREE_OPERAND (arg, 1)))
3962 t = build2 (EQ_EXPR, type, arg, build_int_cst (type, 0));
3966 t = build1 (TRUTH_NOT_EXPR, type, arg);
3969 case CLEANUP_POINT_EXPR:
3970 loc1 = EXPR_LOCATION (TREE_OPERAND (arg, 0));
3971 if (loc1 == UNKNOWN_LOCATION)
3973 t = build1 (CLEANUP_POINT_EXPR, type,
3974 invert_truthvalue_loc (loc1, TREE_OPERAND (arg, 0)));
3983 SET_EXPR_LOCATION (t, loc);
3988 /* Return a simplified tree node for the truth-negation of ARG. This
3989 never alters ARG itself. We assume that ARG is an operation that
3990 returns a truth value (0 or 1).
3992 FIXME: one would think we would fold the result, but it causes
3993 problems with the dominator optimizer. */
3996 invert_truthvalue_loc (location_t loc, tree arg)
4000 if (TREE_CODE (arg) == ERROR_MARK)
4003 tem = fold_truth_not_expr (loc, arg);
4006 tem = build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
4007 SET_EXPR_LOCATION (tem, loc);
4013 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
4014 operands are another bit-wise operation with a common input. If so,
4015 distribute the bit operations to save an operation and possibly two if
4016 constants are involved. For example, convert
4017 (A | B) & (A | C) into A | (B & C)
4018 Further simplification will occur if B and C are constants.
4020 If this optimization cannot be done, 0 will be returned. */
4023 distribute_bit_expr (location_t loc, enum tree_code code, tree type,
4024 tree arg0, tree arg1)
4029 if (TREE_CODE (arg0) != TREE_CODE (arg1)
4030 || TREE_CODE (arg0) == code
4031 || (TREE_CODE (arg0) != BIT_AND_EXPR
4032 && TREE_CODE (arg0) != BIT_IOR_EXPR))
4035 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
4037 common = TREE_OPERAND (arg0, 0);
4038 left = TREE_OPERAND (arg0, 1);
4039 right = TREE_OPERAND (arg1, 1);
4041 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
4043 common = TREE_OPERAND (arg0, 0);
4044 left = TREE_OPERAND (arg0, 1);
4045 right = TREE_OPERAND (arg1, 0);
4047 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
4049 common = TREE_OPERAND (arg0, 1);
4050 left = TREE_OPERAND (arg0, 0);
4051 right = TREE_OPERAND (arg1, 1);
4053 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
4055 common = TREE_OPERAND (arg0, 1);
4056 left = TREE_OPERAND (arg0, 0);
4057 right = TREE_OPERAND (arg1, 0);
4062 common = fold_convert_loc (loc, type, common);
4063 left = fold_convert_loc (loc, type, left);
4064 right = fold_convert_loc (loc, type, right);
4065 return fold_build2_loc (loc, TREE_CODE (arg0), type, common,
4066 fold_build2_loc (loc, code, type, left, right));
4069 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
4070 with code CODE. This optimization is unsafe. */
4072 distribute_real_division (location_t loc, enum tree_code code, tree type,
4073 tree arg0, tree arg1)
4075 bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
4076 bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
4078 /* (A / C) +- (B / C) -> (A +- B) / C. */
4080 && operand_equal_p (TREE_OPERAND (arg0, 1),
4081 TREE_OPERAND (arg1, 1), 0))
4082 return fold_build2_loc (loc, mul0 ? MULT_EXPR : RDIV_EXPR, type,
4083 fold_build2_loc (loc, code, type,
4084 TREE_OPERAND (arg0, 0),
4085 TREE_OPERAND (arg1, 0)),
4086 TREE_OPERAND (arg0, 1));
4088 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
4089 if (operand_equal_p (TREE_OPERAND (arg0, 0),
4090 TREE_OPERAND (arg1, 0), 0)
4091 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
4092 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
4094 REAL_VALUE_TYPE r0, r1;
4095 r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
4096 r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
4098 real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
4100 real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
4101 real_arithmetic (&r0, code, &r0, &r1);
4102 return fold_build2_loc (loc, MULT_EXPR, type,
4103 TREE_OPERAND (arg0, 0),
4104 build_real (type, r0));
4110 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
4111 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero. */
4114 make_bit_field_ref (location_t loc, tree inner, tree type,
4115 HOST_WIDE_INT bitsize, HOST_WIDE_INT bitpos, int unsignedp)
4117 tree result, bftype;
4121 tree size = TYPE_SIZE (TREE_TYPE (inner));
4122 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
4123 || POINTER_TYPE_P (TREE_TYPE (inner)))
4124 && host_integerp (size, 0)
4125 && tree_low_cst (size, 0) == bitsize)
4126 return fold_convert_loc (loc, type, inner);
4130 if (TYPE_PRECISION (bftype) != bitsize
4131 || TYPE_UNSIGNED (bftype) == !unsignedp)
4132 bftype = build_nonstandard_integer_type (bitsize, 0);
4134 result = build3 (BIT_FIELD_REF, bftype, inner,
4135 size_int (bitsize), bitsize_int (bitpos));
4136 SET_EXPR_LOCATION (result, loc);
4139 result = fold_convert_loc (loc, type, result);
4144 /* Optimize a bit-field compare.
4146 There are two cases: First is a compare against a constant and the
4147 second is a comparison of two items where the fields are at the same
4148 bit position relative to the start of a chunk (byte, halfword, word)
4149 large enough to contain it. In these cases we can avoid the shift
4150 implicit in bitfield extractions.
4152 For constants, we emit a compare of the shifted constant with the
4153 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
4154 compared. For two fields at the same position, we do the ANDs with the
4155 similar mask and compare the result of the ANDs.
4157 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
4158 COMPARE_TYPE is the type of the comparison, and LHS and RHS
4159 are the left and right operands of the comparison, respectively.
4161 If the optimization described above can be done, we return the resulting
4162 tree. Otherwise we return zero. */
4165 optimize_bit_field_compare (location_t loc, enum tree_code code,
4166 tree compare_type, tree lhs, tree rhs)
4168 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
4169 tree type = TREE_TYPE (lhs);
4170 tree signed_type, unsigned_type;
4171 int const_p = TREE_CODE (rhs) == INTEGER_CST;
4172 enum machine_mode lmode, rmode, nmode;
4173 int lunsignedp, runsignedp;
4174 int lvolatilep = 0, rvolatilep = 0;
4175 tree linner, rinner = NULL_TREE;
4179 /* Get all the information about the extractions being done. If the bit size
4180 if the same as the size of the underlying object, we aren't doing an
4181 extraction at all and so can do nothing. We also don't want to
4182 do anything if the inner expression is a PLACEHOLDER_EXPR since we
4183 then will no longer be able to replace it. */
4184 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
4185 &lunsignedp, &lvolatilep, false);
4186 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
4187 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
4192 /* If this is not a constant, we can only do something if bit positions,
4193 sizes, and signedness are the same. */
4194 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
4195 &runsignedp, &rvolatilep, false);
4197 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
4198 || lunsignedp != runsignedp || offset != 0
4199 || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
4203 /* See if we can find a mode to refer to this field. We should be able to,
4204 but fail if we can't. */
4205 nmode = get_best_mode (lbitsize, lbitpos,
4206 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
4207 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
4208 TYPE_ALIGN (TREE_TYPE (rinner))),
4209 word_mode, lvolatilep || rvolatilep);
4210 if (nmode == VOIDmode)
4213 /* Set signed and unsigned types of the precision of this mode for the
4215 signed_type = lang_hooks.types.type_for_mode (nmode, 0);
4216 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
4218 /* Compute the bit position and size for the new reference and our offset
4219 within it. If the new reference is the same size as the original, we
4220 won't optimize anything, so return zero. */
4221 nbitsize = GET_MODE_BITSIZE (nmode);
4222 nbitpos = lbitpos & ~ (nbitsize - 1);
4224 if (nbitsize == lbitsize)
4227 if (BYTES_BIG_ENDIAN)
4228 lbitpos = nbitsize - lbitsize - lbitpos;
4230 /* Make the mask to be used against the extracted field. */
4231 mask = build_int_cst_type (unsigned_type, -1);
4232 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
4233 mask = const_binop (RSHIFT_EXPR, mask,
4234 size_int (nbitsize - lbitsize - lbitpos), 0);
4237 /* If not comparing with constant, just rework the comparison
4239 return fold_build2_loc (loc, code, compare_type,
4240 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4241 make_bit_field_ref (loc, linner,
4246 fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4247 make_bit_field_ref (loc, rinner,
4253 /* Otherwise, we are handling the constant case. See if the constant is too
4254 big for the field. Warn and return a tree of for 0 (false) if so. We do
4255 this not only for its own sake, but to avoid having to test for this
4256 error case below. If we didn't, we might generate wrong code.
4258 For unsigned fields, the constant shifted right by the field length should
4259 be all zero. For signed fields, the high-order bits should agree with
4264 if (! integer_zerop (const_binop (RSHIFT_EXPR,
4265 fold_convert_loc (loc,
4266 unsigned_type, rhs),
4267 size_int (lbitsize), 0)))
4269 warning (0, "comparison is always %d due to width of bit-field",
4271 return constant_boolean_node (code == NE_EXPR, compare_type);
4276 tree tem = const_binop (RSHIFT_EXPR,
4277 fold_convert_loc (loc, signed_type, rhs),
4278 size_int (lbitsize - 1), 0);
4279 if (! integer_zerop (tem) && ! integer_all_onesp (tem))
4281 warning (0, "comparison is always %d due to width of bit-field",
4283 return constant_boolean_node (code == NE_EXPR, compare_type);
4287 /* Single-bit compares should always be against zero. */
4288 if (lbitsize == 1 && ! integer_zerop (rhs))
4290 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4291 rhs = build_int_cst (type, 0);
4294 /* Make a new bitfield reference, shift the constant over the
4295 appropriate number of bits and mask it with the computed mask
4296 (in case this was a signed field). If we changed it, make a new one. */
4297 lhs = make_bit_field_ref (loc, linner, unsigned_type, nbitsize, nbitpos, 1);
4300 TREE_SIDE_EFFECTS (lhs) = 1;
4301 TREE_THIS_VOLATILE (lhs) = 1;
4304 rhs = const_binop (BIT_AND_EXPR,
4305 const_binop (LSHIFT_EXPR,
4306 fold_convert_loc (loc, unsigned_type, rhs),
4307 size_int (lbitpos), 0),
4310 lhs = build2 (code, compare_type,
4311 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask),
4313 SET_EXPR_LOCATION (lhs, loc);
4317 /* Subroutine for fold_truthop: decode a field reference.
4319 If EXP is a comparison reference, we return the innermost reference.
4321 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4322 set to the starting bit number.
4324 If the innermost field can be completely contained in a mode-sized
4325 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4327 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4328 otherwise it is not changed.
4330 *PUNSIGNEDP is set to the signedness of the field.
4332 *PMASK is set to the mask used. This is either contained in a
4333 BIT_AND_EXPR or derived from the width of the field.
4335 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4337 Return 0 if this is not a component reference or is one that we can't
4338 do anything with. */
4341 decode_field_reference (location_t loc, tree exp, HOST_WIDE_INT *pbitsize,
4342 HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
4343 int *punsignedp, int *pvolatilep,
4344 tree *pmask, tree *pand_mask)
4346 tree outer_type = 0;
4348 tree mask, inner, offset;
4350 unsigned int precision;
4352 /* All the optimizations using this function assume integer fields.
4353 There are problems with FP fields since the type_for_size call
4354 below can fail for, e.g., XFmode. */
4355 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4358 /* We are interested in the bare arrangement of bits, so strip everything
4359 that doesn't affect the machine mode. However, record the type of the
4360 outermost expression if it may matter below. */
4361 if (CONVERT_EXPR_P (exp)
4362 || TREE_CODE (exp) == NON_LVALUE_EXPR)
4363 outer_type = TREE_TYPE (exp);
4366 if (TREE_CODE (exp) == BIT_AND_EXPR)
4368 and_mask = TREE_OPERAND (exp, 1);
4369 exp = TREE_OPERAND (exp, 0);
4370 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4371 if (TREE_CODE (and_mask) != INTEGER_CST)
4375 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
4376 punsignedp, pvolatilep, false);
4377 if ((inner == exp && and_mask == 0)
4378 || *pbitsize < 0 || offset != 0
4379 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
4382 /* If the number of bits in the reference is the same as the bitsize of
4383 the outer type, then the outer type gives the signedness. Otherwise
4384 (in case of a small bitfield) the signedness is unchanged. */
4385 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4386 *punsignedp = TYPE_UNSIGNED (outer_type);
4388 /* Compute the mask to access the bitfield. */
4389 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4390 precision = TYPE_PRECISION (unsigned_type);
4392 mask = build_int_cst_type (unsigned_type, -1);
4394 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
4395 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
4397 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4399 mask = fold_build2_loc (loc, BIT_AND_EXPR, unsigned_type,
4400 fold_convert_loc (loc, unsigned_type, and_mask), mask);
4403 *pand_mask = and_mask;
4407 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4411 all_ones_mask_p (const_tree mask, int size)
4413 tree type = TREE_TYPE (mask);
4414 unsigned int precision = TYPE_PRECISION (type);
4417 tmask = build_int_cst_type (signed_type_for (type), -1);
4420 tree_int_cst_equal (mask,
4421 const_binop (RSHIFT_EXPR,
4422 const_binop (LSHIFT_EXPR, tmask,
4423 size_int (precision - size),
4425 size_int (precision - size), 0));
4428 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4429 represents the sign bit of EXP's type. If EXP represents a sign
4430 or zero extension, also test VAL against the unextended type.
4431 The return value is the (sub)expression whose sign bit is VAL,
4432 or NULL_TREE otherwise. */
4435 sign_bit_p (tree exp, const_tree val)
4437 unsigned HOST_WIDE_INT mask_lo, lo;
4438 HOST_WIDE_INT mask_hi, hi;
4442 /* Tree EXP must have an integral type. */
4443 t = TREE_TYPE (exp);
4444 if (! INTEGRAL_TYPE_P (t))
4447 /* Tree VAL must be an integer constant. */
4448 if (TREE_CODE (val) != INTEGER_CST
4449 || TREE_OVERFLOW (val))
4452 width = TYPE_PRECISION (t);
4453 if (width > HOST_BITS_PER_WIDE_INT)
4455 hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
4458 mask_hi = ((unsigned HOST_WIDE_INT) -1
4459 >> (2 * HOST_BITS_PER_WIDE_INT - width));
4465 lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
4468 mask_lo = ((unsigned HOST_WIDE_INT) -1
4469 >> (HOST_BITS_PER_WIDE_INT - width));
4472 /* We mask off those bits beyond TREE_TYPE (exp) so that we can
4473 treat VAL as if it were unsigned. */
4474 if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
4475 && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
4478 /* Handle extension from a narrower type. */
4479 if (TREE_CODE (exp) == NOP_EXPR
4480 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4481 return sign_bit_p (TREE_OPERAND (exp, 0), val);
4486 /* Subroutine for fold_truthop: determine if an operand is simple enough
4487 to be evaluated unconditionally. */
4490 simple_operand_p (const_tree exp)
4492 /* Strip any conversions that don't change the machine mode. */
4495 return (CONSTANT_CLASS_P (exp)
4496 || TREE_CODE (exp) == SSA_NAME
4498 && ! TREE_ADDRESSABLE (exp)
4499 && ! TREE_THIS_VOLATILE (exp)
4500 && ! DECL_NONLOCAL (exp)
4501 /* Don't regard global variables as simple. They may be
4502 allocated in ways unknown to the compiler (shared memory,
4503 #pragma weak, etc). */
4504 && ! TREE_PUBLIC (exp)
4505 && ! DECL_EXTERNAL (exp)
4506 /* Loading a static variable is unduly expensive, but global
4507 registers aren't expensive. */
4508 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4511 /* The following functions are subroutines to fold_range_test and allow it to
4512 try to change a logical combination of comparisons into a range test.
4515 X == 2 || X == 3 || X == 4 || X == 5
4519 (unsigned) (X - 2) <= 3
4521 We describe each set of comparisons as being either inside or outside
4522 a range, using a variable named like IN_P, and then describe the
4523 range with a lower and upper bound. If one of the bounds is omitted,
4524 it represents either the highest or lowest value of the type.
4526 In the comments below, we represent a range by two numbers in brackets
4527 preceded by a "+" to designate being inside that range, or a "-" to
4528 designate being outside that range, so the condition can be inverted by
4529 flipping the prefix. An omitted bound is represented by a "-". For
4530 example, "- [-, 10]" means being outside the range starting at the lowest
4531 possible value and ending at 10, in other words, being greater than 10.
4532 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4535 We set up things so that the missing bounds are handled in a consistent
4536 manner so neither a missing bound nor "true" and "false" need to be
4537 handled using a special case. */
4539 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4540 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4541 and UPPER1_P are nonzero if the respective argument is an upper bound
4542 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4543 must be specified for a comparison. ARG1 will be converted to ARG0's
4544 type if both are specified. */
4547 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4548 tree arg1, int upper1_p)
4554 /* If neither arg represents infinity, do the normal operation.
4555 Else, if not a comparison, return infinity. Else handle the special
4556 comparison rules. Note that most of the cases below won't occur, but
4557 are handled for consistency. */
4559 if (arg0 != 0 && arg1 != 0)
4561 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4562 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4564 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4567 if (TREE_CODE_CLASS (code) != tcc_comparison)
4570 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4571 for neither. In real maths, we cannot assume open ended ranges are
4572 the same. But, this is computer arithmetic, where numbers are finite.
4573 We can therefore make the transformation of any unbounded range with
4574 the value Z, Z being greater than any representable number. This permits
4575 us to treat unbounded ranges as equal. */
4576 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4577 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4581 result = sgn0 == sgn1;
4584 result = sgn0 != sgn1;
4587 result = sgn0 < sgn1;
4590 result = sgn0 <= sgn1;
4593 result = sgn0 > sgn1;
4596 result = sgn0 >= sgn1;
4602 return constant_boolean_node (result, type);
4605 /* Given EXP, a logical expression, set the range it is testing into
4606 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4607 actually being tested. *PLOW and *PHIGH will be made of the same
4608 type as the returned expression. If EXP is not a comparison, we
4609 will most likely not be returning a useful value and range. Set
4610 *STRICT_OVERFLOW_P to true if the return value is only valid
4611 because signed overflow is undefined; otherwise, do not change
4612 *STRICT_OVERFLOW_P. */
4615 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4616 bool *strict_overflow_p)
4618 enum tree_code code;
4619 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
4620 tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
4622 tree low, high, n_low, n_high;
4623 location_t loc = EXPR_LOCATION (exp);
4625 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4626 and see if we can refine the range. Some of the cases below may not
4627 happen, but it doesn't seem worth worrying about this. We "continue"
4628 the outer loop when we've changed something; otherwise we "break"
4629 the switch, which will "break" the while. */
4632 low = high = build_int_cst (TREE_TYPE (exp), 0);
4636 code = TREE_CODE (exp);
4637 exp_type = TREE_TYPE (exp);
4639 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4641 if (TREE_OPERAND_LENGTH (exp) > 0)
4642 arg0 = TREE_OPERAND (exp, 0);
4643 if (TREE_CODE_CLASS (code) == tcc_comparison
4644 || TREE_CODE_CLASS (code) == tcc_unary
4645 || TREE_CODE_CLASS (code) == tcc_binary)
4646 arg0_type = TREE_TYPE (arg0);
4647 if (TREE_CODE_CLASS (code) == tcc_binary
4648 || TREE_CODE_CLASS (code) == tcc_comparison
4649 || (TREE_CODE_CLASS (code) == tcc_expression
4650 && TREE_OPERAND_LENGTH (exp) > 1))
4651 arg1 = TREE_OPERAND (exp, 1);
4656 case TRUTH_NOT_EXPR:
4657 in_p = ! in_p, exp = arg0;
4660 case EQ_EXPR: case NE_EXPR:
4661 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4662 /* We can only do something if the range is testing for zero
4663 and if the second operand is an integer constant. Note that
4664 saying something is "in" the range we make is done by
4665 complementing IN_P since it will set in the initial case of
4666 being not equal to zero; "out" is leaving it alone. */
4667 if (low == 0 || high == 0
4668 || ! integer_zerop (low) || ! integer_zerop (high)
4669 || TREE_CODE (arg1) != INTEGER_CST)
4674 case NE_EXPR: /* - [c, c] */
4677 case EQ_EXPR: /* + [c, c] */
4678 in_p = ! in_p, low = high = arg1;
4680 case GT_EXPR: /* - [-, c] */
4681 low = 0, high = arg1;
4683 case GE_EXPR: /* + [c, -] */
4684 in_p = ! in_p, low = arg1, high = 0;
4686 case LT_EXPR: /* - [c, -] */
4687 low = arg1, high = 0;
4689 case LE_EXPR: /* + [-, c] */
4690 in_p = ! in_p, low = 0, high = arg1;
4696 /* If this is an unsigned comparison, we also know that EXP is
4697 greater than or equal to zero. We base the range tests we make
4698 on that fact, so we record it here so we can parse existing
4699 range tests. We test arg0_type since often the return type
4700 of, e.g. EQ_EXPR, is boolean. */
4701 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4703 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4705 build_int_cst (arg0_type, 0),
4709 in_p = n_in_p, low = n_low, high = n_high;
4711 /* If the high bound is missing, but we have a nonzero low
4712 bound, reverse the range so it goes from zero to the low bound
4714 if (high == 0 && low && ! integer_zerop (low))
4717 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4718 integer_one_node, 0);
4719 low = build_int_cst (arg0_type, 0);
4727 /* (-x) IN [a,b] -> x in [-b, -a] */
4728 n_low = range_binop (MINUS_EXPR, exp_type,
4729 build_int_cst (exp_type, 0),
4731 n_high = range_binop (MINUS_EXPR, exp_type,
4732 build_int_cst (exp_type, 0),
4734 low = n_low, high = n_high;
4740 exp = build2 (MINUS_EXPR, exp_type, negate_expr (arg0),
4741 build_int_cst (exp_type, 1));
4742 SET_EXPR_LOCATION (exp, loc);
4745 case PLUS_EXPR: case MINUS_EXPR:
4746 if (TREE_CODE (arg1) != INTEGER_CST)
4749 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4750 move a constant to the other side. */
4751 if (!TYPE_UNSIGNED (arg0_type)
4752 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4755 /* If EXP is signed, any overflow in the computation is undefined,
4756 so we don't worry about it so long as our computations on
4757 the bounds don't overflow. For unsigned, overflow is defined
4758 and this is exactly the right thing. */
4759 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4760 arg0_type, low, 0, arg1, 0);
4761 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4762 arg0_type, high, 1, arg1, 0);
4763 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4764 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4767 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4768 *strict_overflow_p = true;
4770 /* Check for an unsigned range which has wrapped around the maximum
4771 value thus making n_high < n_low, and normalize it. */
4772 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4774 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4775 integer_one_node, 0);
4776 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4777 integer_one_node, 0);
4779 /* If the range is of the form +/- [ x+1, x ], we won't
4780 be able to normalize it. But then, it represents the
4781 whole range or the empty set, so make it
4783 if (tree_int_cst_equal (n_low, low)
4784 && tree_int_cst_equal (n_high, high))
4790 low = n_low, high = n_high;
4795 CASE_CONVERT: case NON_LVALUE_EXPR:
4796 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4799 if (! INTEGRAL_TYPE_P (arg0_type)
4800 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4801 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4804 n_low = low, n_high = high;
4807 n_low = fold_convert_loc (loc, arg0_type, n_low);
4810 n_high = fold_convert_loc (loc, arg0_type, n_high);
4813 /* If we're converting arg0 from an unsigned type, to exp,
4814 a signed type, we will be doing the comparison as unsigned.
4815 The tests above have already verified that LOW and HIGH
4818 So we have to ensure that we will handle large unsigned
4819 values the same way that the current signed bounds treat
4822 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4826 /* For fixed-point modes, we need to pass the saturating flag
4827 as the 2nd parameter. */
4828 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4829 equiv_type = lang_hooks.types.type_for_mode
4830 (TYPE_MODE (arg0_type),
4831 TYPE_SATURATING (arg0_type));
4833 equiv_type = lang_hooks.types.type_for_mode
4834 (TYPE_MODE (arg0_type), 1);
4836 /* A range without an upper bound is, naturally, unbounded.
4837 Since convert would have cropped a very large value, use
4838 the max value for the destination type. */
4840 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4841 : TYPE_MAX_VALUE (arg0_type);
4843 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4844 high_positive = fold_build2_loc (loc, RSHIFT_EXPR, arg0_type,
4845 fold_convert_loc (loc, arg0_type,
4847 build_int_cst (arg0_type, 1));
4849 /* If the low bound is specified, "and" the range with the
4850 range for which the original unsigned value will be
4854 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4855 1, n_low, n_high, 1,
4856 fold_convert_loc (loc, arg0_type,
4861 in_p = (n_in_p == in_p);
4865 /* Otherwise, "or" the range with the range of the input
4866 that will be interpreted as negative. */
4867 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4868 0, n_low, n_high, 1,
4869 fold_convert_loc (loc, arg0_type,
4874 in_p = (in_p != n_in_p);
4879 low = n_low, high = n_high;
4889 /* If EXP is a constant, we can evaluate whether this is true or false. */
4890 if (TREE_CODE (exp) == INTEGER_CST)
4892 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4894 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4900 *pin_p = in_p, *plow = low, *phigh = high;
4904 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4905 type, TYPE, return an expression to test if EXP is in (or out of, depending
4906 on IN_P) the range. Return 0 if the test couldn't be created. */
4909 build_range_check (location_t loc, tree type, tree exp, int in_p,
4910 tree low, tree high)
4912 tree etype = TREE_TYPE (exp), value;
4914 #ifdef HAVE_canonicalize_funcptr_for_compare
4915 /* Disable this optimization for function pointer expressions
4916 on targets that require function pointer canonicalization. */
4917 if (HAVE_canonicalize_funcptr_for_compare
4918 && TREE_CODE (etype) == POINTER_TYPE
4919 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4925 value = build_range_check (loc, type, exp, 1, low, high);
4927 return invert_truthvalue_loc (loc, value);
4932 if (low == 0 && high == 0)
4933 return build_int_cst (type, 1);
4936 return fold_build2_loc (loc, LE_EXPR, type, exp,
4937 fold_convert_loc (loc, etype, high));
4940 return fold_build2_loc (loc, GE_EXPR, type, exp,
4941 fold_convert_loc (loc, etype, low));
4943 if (operand_equal_p (low, high, 0))
4944 return fold_build2_loc (loc, EQ_EXPR, type, exp,
4945 fold_convert_loc (loc, etype, low));
4947 if (integer_zerop (low))
4949 if (! TYPE_UNSIGNED (etype))
4951 etype = unsigned_type_for (etype);
4952 high = fold_convert_loc (loc, etype, high);
4953 exp = fold_convert_loc (loc, etype, exp);
4955 return build_range_check (loc, type, exp, 1, 0, high);
4958 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4959 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4961 unsigned HOST_WIDE_INT lo;
4965 prec = TYPE_PRECISION (etype);
4966 if (prec <= HOST_BITS_PER_WIDE_INT)
4969 lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4973 hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4974 lo = (unsigned HOST_WIDE_INT) -1;
4977 if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4979 if (TYPE_UNSIGNED (etype))
4981 tree signed_etype = signed_type_for (etype);
4982 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4984 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4986 etype = signed_etype;
4987 exp = fold_convert_loc (loc, etype, exp);
4989 return fold_build2_loc (loc, GT_EXPR, type, exp,
4990 build_int_cst (etype, 0));
4994 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4995 This requires wrap-around arithmetics for the type of the expression.
4996 First make sure that arithmetics in this type is valid, then make sure
4997 that it wraps around. */
4998 if (TREE_CODE (etype) == ENUMERAL_TYPE || TREE_CODE (etype) == BOOLEAN_TYPE)
4999 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
5000 TYPE_UNSIGNED (etype));
5002 if (TREE_CODE (etype) == INTEGER_TYPE && !TYPE_OVERFLOW_WRAPS (etype))
5004 tree utype, minv, maxv;
5006 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
5007 for the type in question, as we rely on this here. */
5008 utype = unsigned_type_for (etype);
5009 maxv = fold_convert_loc (loc, utype, TYPE_MAX_VALUE (etype));
5010 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
5011 integer_one_node, 1);
5012 minv = fold_convert_loc (loc, utype, TYPE_MIN_VALUE (etype));
5014 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
5021 high = fold_convert_loc (loc, etype, high);
5022 low = fold_convert_loc (loc, etype, low);
5023 exp = fold_convert_loc (loc, etype, exp);
5025 value = const_binop (MINUS_EXPR, high, low, 0);
5028 if (POINTER_TYPE_P (etype))
5030 if (value != 0 && !TREE_OVERFLOW (value))
5032 low = fold_convert_loc (loc, sizetype, low);
5033 low = fold_build1_loc (loc, NEGATE_EXPR, sizetype, low);
5034 return build_range_check (loc, type,
5035 fold_build2_loc (loc, POINTER_PLUS_EXPR,
5037 1, build_int_cst (etype, 0), value);
5042 if (value != 0 && !TREE_OVERFLOW (value))
5043 return build_range_check (loc, type,
5044 fold_build2_loc (loc, MINUS_EXPR, etype, exp, low),
5045 1, build_int_cst (etype, 0), value);
5050 /* Return the predecessor of VAL in its type, handling the infinite case. */
5053 range_predecessor (tree val)
5055 tree type = TREE_TYPE (val);
5057 if (INTEGRAL_TYPE_P (type)
5058 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
5061 return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
5064 /* Return the successor of VAL in its type, handling the infinite case. */
5067 range_successor (tree val)
5069 tree type = TREE_TYPE (val);
5071 if (INTEGRAL_TYPE_P (type)
5072 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
5075 return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
5078 /* Given two ranges, see if we can merge them into one. Return 1 if we
5079 can, 0 if we can't. Set the output range into the specified parameters. */
5082 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
5083 tree high0, int in1_p, tree low1, tree high1)
5091 int lowequal = ((low0 == 0 && low1 == 0)
5092 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5093 low0, 0, low1, 0)));
5094 int highequal = ((high0 == 0 && high1 == 0)
5095 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
5096 high0, 1, high1, 1)));
5098 /* Make range 0 be the range that starts first, or ends last if they
5099 start at the same value. Swap them if it isn't. */
5100 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
5103 && integer_onep (range_binop (GT_EXPR, integer_type_node,
5104 high1, 1, high0, 1))))
5106 temp = in0_p, in0_p = in1_p, in1_p = temp;
5107 tem = low0, low0 = low1, low1 = tem;
5108 tem = high0, high0 = high1, high1 = tem;
5111 /* Now flag two cases, whether the ranges are disjoint or whether the
5112 second range is totally subsumed in the first. Note that the tests
5113 below are simplified by the ones above. */
5114 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
5115 high0, 1, low1, 0));
5116 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
5117 high1, 1, high0, 1));
5119 /* We now have four cases, depending on whether we are including or
5120 excluding the two ranges. */
5123 /* If they don't overlap, the result is false. If the second range
5124 is a subset it is the result. Otherwise, the range is from the start
5125 of the second to the end of the first. */
5127 in_p = 0, low = high = 0;
5129 in_p = 1, low = low1, high = high1;
5131 in_p = 1, low = low1, high = high0;
5134 else if (in0_p && ! in1_p)
5136 /* If they don't overlap, the result is the first range. If they are
5137 equal, the result is false. If the second range is a subset of the
5138 first, and the ranges begin at the same place, we go from just after
5139 the end of the second range to the end of the first. If the second
5140 range is not a subset of the first, or if it is a subset and both
5141 ranges end at the same place, the range starts at the start of the
5142 first range and ends just before the second range.
5143 Otherwise, we can't describe this as a single range. */
5145 in_p = 1, low = low0, high = high0;
5146 else if (lowequal && highequal)
5147 in_p = 0, low = high = 0;
5148 else if (subset && lowequal)
5150 low = range_successor (high1);
5155 /* We are in the weird situation where high0 > high1 but
5156 high1 has no successor. Punt. */
5160 else if (! subset || highequal)
5163 high = range_predecessor (low1);
5167 /* low0 < low1 but low1 has no predecessor. Punt. */
5175 else if (! in0_p && in1_p)
5177 /* If they don't overlap, the result is the second range. If the second
5178 is a subset of the first, the result is false. Otherwise,
5179 the range starts just after the first range and ends at the
5180 end of the second. */
5182 in_p = 1, low = low1, high = high1;
5183 else if (subset || highequal)
5184 in_p = 0, low = high = 0;
5187 low = range_successor (high0);
5192 /* high1 > high0 but high0 has no successor. Punt. */
5200 /* The case where we are excluding both ranges. Here the complex case
5201 is if they don't overlap. In that case, the only time we have a
5202 range is if they are adjacent. If the second is a subset of the
5203 first, the result is the first. Otherwise, the range to exclude
5204 starts at the beginning of the first range and ends at the end of the
5208 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
5209 range_successor (high0),
5211 in_p = 0, low = low0, high = high1;
5214 /* Canonicalize - [min, x] into - [-, x]. */
5215 if (low0 && TREE_CODE (low0) == INTEGER_CST)
5216 switch (TREE_CODE (TREE_TYPE (low0)))
5219 if (TYPE_PRECISION (TREE_TYPE (low0))
5220 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
5224 if (tree_int_cst_equal (low0,
5225 TYPE_MIN_VALUE (TREE_TYPE (low0))))
5229 if (TYPE_UNSIGNED (TREE_TYPE (low0))
5230 && integer_zerop (low0))
5237 /* Canonicalize - [x, max] into - [x, -]. */
5238 if (high1 && TREE_CODE (high1) == INTEGER_CST)
5239 switch (TREE_CODE (TREE_TYPE (high1)))
5242 if (TYPE_PRECISION (TREE_TYPE (high1))
5243 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
5247 if (tree_int_cst_equal (high1,
5248 TYPE_MAX_VALUE (TREE_TYPE (high1))))
5252 if (TYPE_UNSIGNED (TREE_TYPE (high1))
5253 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
5255 integer_one_node, 1)))
5262 /* The ranges might be also adjacent between the maximum and
5263 minimum values of the given type. For
5264 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
5265 return + [x + 1, y - 1]. */
5266 if (low0 == 0 && high1 == 0)
5268 low = range_successor (high0);
5269 high = range_predecessor (low1);
5270 if (low == 0 || high == 0)
5280 in_p = 0, low = low0, high = high0;
5282 in_p = 0, low = low0, high = high1;
5285 *pin_p = in_p, *plow = low, *phigh = high;
5290 /* Subroutine of fold, looking inside expressions of the form
5291 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5292 of the COND_EXPR. This function is being used also to optimize
5293 A op B ? C : A, by reversing the comparison first.
5295 Return a folded expression whose code is not a COND_EXPR
5296 anymore, or NULL_TREE if no folding opportunity is found. */
5299 fold_cond_expr_with_comparison (location_t loc, tree type,
5300 tree arg0, tree arg1, tree arg2)
5302 enum tree_code comp_code = TREE_CODE (arg0);
5303 tree arg00 = TREE_OPERAND (arg0, 0);
5304 tree arg01 = TREE_OPERAND (arg0, 1);
5305 tree arg1_type = TREE_TYPE (arg1);
5311 /* If we have A op 0 ? A : -A, consider applying the following
5314 A == 0? A : -A same as -A
5315 A != 0? A : -A same as A
5316 A >= 0? A : -A same as abs (A)
5317 A > 0? A : -A same as abs (A)
5318 A <= 0? A : -A same as -abs (A)
5319 A < 0? A : -A same as -abs (A)
5321 None of these transformations work for modes with signed
5322 zeros. If A is +/-0, the first two transformations will
5323 change the sign of the result (from +0 to -0, or vice
5324 versa). The last four will fix the sign of the result,
5325 even though the original expressions could be positive or
5326 negative, depending on the sign of A.
5328 Note that all these transformations are correct if A is
5329 NaN, since the two alternatives (A and -A) are also NaNs. */
5330 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5331 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5332 ? real_zerop (arg01)
5333 : integer_zerop (arg01))
5334 && ((TREE_CODE (arg2) == NEGATE_EXPR
5335 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5336 /* In the case that A is of the form X-Y, '-A' (arg2) may
5337 have already been folded to Y-X, check for that. */
5338 || (TREE_CODE (arg1) == MINUS_EXPR
5339 && TREE_CODE (arg2) == MINUS_EXPR
5340 && operand_equal_p (TREE_OPERAND (arg1, 0),
5341 TREE_OPERAND (arg2, 1), 0)
5342 && operand_equal_p (TREE_OPERAND (arg1, 1),
5343 TREE_OPERAND (arg2, 0), 0))))
5348 tem = fold_convert_loc (loc, arg1_type, arg1);
5349 return pedantic_non_lvalue_loc (loc,
5350 fold_convert_loc (loc, type,
5351 negate_expr (tem)));
5354 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5357 if (flag_trapping_math)
5362 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5363 arg1 = fold_convert_loc (loc, signed_type_for
5364 (TREE_TYPE (arg1)), arg1);
5365 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5366 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5369 if (flag_trapping_math)
5373 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5374 arg1 = fold_convert_loc (loc, signed_type_for
5375 (TREE_TYPE (arg1)), arg1);
5376 tem = fold_build1_loc (loc, ABS_EXPR, TREE_TYPE (arg1), arg1);
5377 return negate_expr (fold_convert_loc (loc, type, tem));
5379 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5383 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5384 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5385 both transformations are correct when A is NaN: A != 0
5386 is then true, and A == 0 is false. */
5388 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5389 && integer_zerop (arg01) && integer_zerop (arg2))
5391 if (comp_code == NE_EXPR)
5392 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5393 else if (comp_code == EQ_EXPR)
5394 return build_int_cst (type, 0);
5397 /* Try some transformations of A op B ? A : B.
5399 A == B? A : B same as B
5400 A != B? A : B same as A
5401 A >= B? A : B same as max (A, B)
5402 A > B? A : B same as max (B, A)
5403 A <= B? A : B same as min (A, B)
5404 A < B? A : B same as min (B, A)
5406 As above, these transformations don't work in the presence
5407 of signed zeros. For example, if A and B are zeros of
5408 opposite sign, the first two transformations will change
5409 the sign of the result. In the last four, the original
5410 expressions give different results for (A=+0, B=-0) and
5411 (A=-0, B=+0), but the transformed expressions do not.
5413 The first two transformations are correct if either A or B
5414 is a NaN. In the first transformation, the condition will
5415 be false, and B will indeed be chosen. In the case of the
5416 second transformation, the condition A != B will be true,
5417 and A will be chosen.
5419 The conversions to max() and min() are not correct if B is
5420 a number and A is not. The conditions in the original
5421 expressions will be false, so all four give B. The min()
5422 and max() versions would give a NaN instead. */
5423 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5424 && operand_equal_for_comparison_p (arg01, arg2, arg00)
5425 /* Avoid these transformations if the COND_EXPR may be used
5426 as an lvalue in the C++ front-end. PR c++/19199. */
5428 || (strcmp (lang_hooks.name, "GNU C++") != 0
5429 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5430 || ! maybe_lvalue_p (arg1)
5431 || ! maybe_lvalue_p (arg2)))
5433 tree comp_op0 = arg00;
5434 tree comp_op1 = arg01;
5435 tree comp_type = TREE_TYPE (comp_op0);
5437 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
5438 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
5448 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg2));
5450 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
5455 /* In C++ a ?: expression can be an lvalue, so put the
5456 operand which will be used if they are equal first
5457 so that we can convert this back to the
5458 corresponding COND_EXPR. */
5459 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5461 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5462 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5463 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5464 ? fold_build2_loc (loc, MIN_EXPR, comp_type, comp_op0, comp_op1)
5465 : fold_build2_loc (loc, MIN_EXPR, comp_type,
5466 comp_op1, comp_op0);
5467 return pedantic_non_lvalue_loc (loc,
5468 fold_convert_loc (loc, type, tem));
5475 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5477 comp_op0 = fold_convert_loc (loc, comp_type, comp_op0);
5478 comp_op1 = fold_convert_loc (loc, comp_type, comp_op1);
5479 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5480 ? fold_build2_loc (loc, MAX_EXPR, comp_type, comp_op0, comp_op1)
5481 : fold_build2_loc (loc, MAX_EXPR, comp_type,
5482 comp_op1, comp_op0);
5483 return pedantic_non_lvalue_loc (loc,
5484 fold_convert_loc (loc, type, tem));
5488 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5489 return pedantic_non_lvalue_loc (loc,
5490 fold_convert_loc (loc, type, arg2));
5493 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5494 return pedantic_non_lvalue_loc (loc,
5495 fold_convert_loc (loc, type, arg1));
5498 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5503 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5504 we might still be able to simplify this. For example,
5505 if C1 is one less or one more than C2, this might have started
5506 out as a MIN or MAX and been transformed by this function.
5507 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
5509 if (INTEGRAL_TYPE_P (type)
5510 && TREE_CODE (arg01) == INTEGER_CST
5511 && TREE_CODE (arg2) == INTEGER_CST)
5515 if (TREE_CODE (arg1) == INTEGER_CST)
5517 /* We can replace A with C1 in this case. */
5518 arg1 = fold_convert_loc (loc, type, arg01);
5519 return fold_build3_loc (loc, COND_EXPR, type, arg0, arg1, arg2);
5522 /* If C1 is C2 + 1, this is min(A, C2), but use ARG00's type for
5523 MIN_EXPR, to preserve the signedness of the comparison. */
5524 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5526 && operand_equal_p (arg01,
5527 const_binop (PLUS_EXPR, arg2,
5528 build_int_cst (type, 1), 0),
5531 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5532 fold_convert_loc (loc, TREE_TYPE (arg00),
5534 return pedantic_non_lvalue_loc (loc,
5535 fold_convert_loc (loc, type, tem));
5540 /* If C1 is C2 - 1, this is min(A, C2), with the same care
5542 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5544 && operand_equal_p (arg01,
5545 const_binop (MINUS_EXPR, arg2,
5546 build_int_cst (type, 1), 0),
5549 tem = fold_build2_loc (loc, MIN_EXPR, TREE_TYPE (arg00), arg00,
5550 fold_convert_loc (loc, TREE_TYPE (arg00),
5552 return pedantic_non_lvalue_loc (loc,
5553 fold_convert_loc (loc, type, tem));
5558 /* If C1 is C2 - 1, this is max(A, C2), but use ARG00's type for
5559 MAX_EXPR, to preserve the signedness of the comparison. */
5560 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5562 && operand_equal_p (arg01,
5563 const_binop (MINUS_EXPR, arg2,
5564 build_int_cst (type, 1), 0),
5567 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5568 fold_convert_loc (loc, TREE_TYPE (arg00),
5570 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5575 /* If C1 is C2 + 1, this is max(A, C2), with the same care as above. */
5576 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5578 && operand_equal_p (arg01,
5579 const_binop (PLUS_EXPR, arg2,
5580 build_int_cst (type, 1), 0),
5583 tem = fold_build2_loc (loc, MAX_EXPR, TREE_TYPE (arg00), arg00,
5584 fold_convert_loc (loc, TREE_TYPE (arg00),
5586 return pedantic_non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
5600 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5601 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5602 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5606 /* EXP is some logical combination of boolean tests. See if we can
5607 merge it into some range test. Return the new tree if so. */
5610 fold_range_test (location_t loc, enum tree_code code, tree type,
5613 int or_op = (code == TRUTH_ORIF_EXPR
5614 || code == TRUTH_OR_EXPR);
5615 int in0_p, in1_p, in_p;
5616 tree low0, low1, low, high0, high1, high;
5617 bool strict_overflow_p = false;
5618 tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5619 tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5621 const char * const warnmsg = G_("assuming signed overflow does not occur "
5622 "when simplifying range test");
5624 /* If this is an OR operation, invert both sides; we will invert
5625 again at the end. */
5627 in0_p = ! in0_p, in1_p = ! in1_p;
5629 /* If both expressions are the same, if we can merge the ranges, and we
5630 can build the range test, return it or it inverted. If one of the
5631 ranges is always true or always false, consider it to be the same
5632 expression as the other. */
5633 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5634 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5636 && 0 != (tem = (build_range_check (UNKNOWN_LOCATION, type,
5638 : rhs != 0 ? rhs : integer_zero_node,
5641 if (strict_overflow_p)
5642 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5643 return or_op ? invert_truthvalue_loc (loc, tem) : tem;
5646 /* On machines where the branch cost is expensive, if this is a
5647 short-circuited branch and the underlying object on both sides
5648 is the same, make a non-short-circuit operation. */
5649 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5650 && lhs != 0 && rhs != 0
5651 && (code == TRUTH_ANDIF_EXPR
5652 || code == TRUTH_ORIF_EXPR)
5653 && operand_equal_p (lhs, rhs, 0))
5655 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5656 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5657 which cases we can't do this. */
5658 if (simple_operand_p (lhs))
5660 tem = build2 (code == TRUTH_ANDIF_EXPR
5661 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5663 SET_EXPR_LOCATION (tem, loc);
5667 else if (lang_hooks.decls.global_bindings_p () == 0
5668 && ! CONTAINS_PLACEHOLDER_P (lhs))
5670 tree common = save_expr (lhs);
5672 if (0 != (lhs = build_range_check (loc, type, common,
5673 or_op ? ! in0_p : in0_p,
5675 && (0 != (rhs = build_range_check (loc, type, common,
5676 or_op ? ! in1_p : in1_p,
5679 if (strict_overflow_p)
5680 fold_overflow_warning (warnmsg,
5681 WARN_STRICT_OVERFLOW_COMPARISON);
5682 tem = build2 (code == TRUTH_ANDIF_EXPR
5683 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5685 SET_EXPR_LOCATION (tem, loc);
5694 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
5695 bit value. Arrange things so the extra bits will be set to zero if and
5696 only if C is signed-extended to its full width. If MASK is nonzero,
5697 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5700 unextend (tree c, int p, int unsignedp, tree mask)
5702 tree type = TREE_TYPE (c);
5703 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5706 if (p == modesize || unsignedp)
5709 /* We work by getting just the sign bit into the low-order bit, then
5710 into the high-order bit, then sign-extend. We then XOR that value
5712 temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
5713 temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
5715 /* We must use a signed type in order to get an arithmetic right shift.
5716 However, we must also avoid introducing accidental overflows, so that
5717 a subsequent call to integer_zerop will work. Hence we must
5718 do the type conversion here. At this point, the constant is either
5719 zero or one, and the conversion to a signed type can never overflow.
5720 We could get an overflow if this conversion is done anywhere else. */
5721 if (TYPE_UNSIGNED (type))
5722 temp = fold_convert (signed_type_for (type), temp);
5724 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
5725 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
5727 temp = const_binop (BIT_AND_EXPR, temp,
5728 fold_convert (TREE_TYPE (c), mask),
5730 /* If necessary, convert the type back to match the type of C. */
5731 if (TYPE_UNSIGNED (type))
5732 temp = fold_convert (type, temp);
5734 return fold_convert (type,
5735 const_binop (BIT_XOR_EXPR, c, temp, 0));
5738 /* Find ways of folding logical expressions of LHS and RHS:
5739 Try to merge two comparisons to the same innermost item.
5740 Look for range tests like "ch >= '0' && ch <= '9'".
5741 Look for combinations of simple terms on machines with expensive branches
5742 and evaluate the RHS unconditionally.
5744 For example, if we have p->a == 2 && p->b == 4 and we can make an
5745 object large enough to span both A and B, we can do this with a comparison
5746 against the object ANDed with the a mask.
5748 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5749 operations to do this with one comparison.
5751 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5752 function and the one above.
5754 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5755 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5757 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5760 We return the simplified tree or 0 if no optimization is possible. */
5763 fold_truthop (location_t loc, enum tree_code code, tree truth_type,
5766 /* If this is the "or" of two comparisons, we can do something if
5767 the comparisons are NE_EXPR. If this is the "and", we can do something
5768 if the comparisons are EQ_EXPR. I.e.,
5769 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5771 WANTED_CODE is this operation code. For single bit fields, we can
5772 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5773 comparison for one-bit fields. */
5775 enum tree_code wanted_code;
5776 enum tree_code lcode, rcode;
5777 tree ll_arg, lr_arg, rl_arg, rr_arg;
5778 tree ll_inner, lr_inner, rl_inner, rr_inner;
5779 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5780 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5781 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5782 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5783 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5784 enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5785 enum machine_mode lnmode, rnmode;
5786 tree ll_mask, lr_mask, rl_mask, rr_mask;
5787 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5788 tree l_const, r_const;
5789 tree lntype, rntype, result;
5790 HOST_WIDE_INT first_bit, end_bit;
5792 tree orig_lhs = lhs, orig_rhs = rhs;
5793 enum tree_code orig_code = code;
5795 /* Start by getting the comparison codes. Fail if anything is volatile.
5796 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5797 it were surrounded with a NE_EXPR. */
5799 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5802 lcode = TREE_CODE (lhs);
5803 rcode = TREE_CODE (rhs);
5805 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5807 lhs = build2 (NE_EXPR, truth_type, lhs,
5808 build_int_cst (TREE_TYPE (lhs), 0));
5812 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5814 rhs = build2 (NE_EXPR, truth_type, rhs,
5815 build_int_cst (TREE_TYPE (rhs), 0));
5819 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5820 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5823 ll_arg = TREE_OPERAND (lhs, 0);
5824 lr_arg = TREE_OPERAND (lhs, 1);
5825 rl_arg = TREE_OPERAND (rhs, 0);
5826 rr_arg = TREE_OPERAND (rhs, 1);
5828 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5829 if (simple_operand_p (ll_arg)
5830 && simple_operand_p (lr_arg))
5833 if (operand_equal_p (ll_arg, rl_arg, 0)
5834 && operand_equal_p (lr_arg, rr_arg, 0))
5836 result = combine_comparisons (loc, code, lcode, rcode,
5837 truth_type, ll_arg, lr_arg);
5841 else if (operand_equal_p (ll_arg, rr_arg, 0)
5842 && operand_equal_p (lr_arg, rl_arg, 0))
5844 result = combine_comparisons (loc, code, lcode,
5845 swap_tree_comparison (rcode),
5846 truth_type, ll_arg, lr_arg);
5852 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5853 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5855 /* If the RHS can be evaluated unconditionally and its operands are
5856 simple, it wins to evaluate the RHS unconditionally on machines
5857 with expensive branches. In this case, this isn't a comparison
5858 that can be merged. Avoid doing this if the RHS is a floating-point
5859 comparison since those can trap. */
5861 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5863 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5864 && simple_operand_p (rl_arg)
5865 && simple_operand_p (rr_arg))
5867 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5868 if (code == TRUTH_OR_EXPR
5869 && lcode == NE_EXPR && integer_zerop (lr_arg)
5870 && rcode == NE_EXPR && integer_zerop (rr_arg)
5871 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5872 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5874 result = build2 (NE_EXPR, truth_type,
5875 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5877 build_int_cst (TREE_TYPE (ll_arg), 0));
5878 goto fold_truthop_exit;
5881 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5882 if (code == TRUTH_AND_EXPR
5883 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5884 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5885 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5886 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5888 result = build2 (EQ_EXPR, truth_type,
5889 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5891 build_int_cst (TREE_TYPE (ll_arg), 0));
5892 goto fold_truthop_exit;
5895 if (LOGICAL_OP_NON_SHORT_CIRCUIT)
5897 if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs)
5899 result = build2 (code, truth_type, lhs, rhs);
5900 goto fold_truthop_exit;
5906 /* See if the comparisons can be merged. Then get all the parameters for
5909 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5910 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5914 ll_inner = decode_field_reference (loc, ll_arg,
5915 &ll_bitsize, &ll_bitpos, &ll_mode,
5916 &ll_unsignedp, &volatilep, &ll_mask,
5918 lr_inner = decode_field_reference (loc, lr_arg,
5919 &lr_bitsize, &lr_bitpos, &lr_mode,
5920 &lr_unsignedp, &volatilep, &lr_mask,
5922 rl_inner = decode_field_reference (loc, rl_arg,
5923 &rl_bitsize, &rl_bitpos, &rl_mode,
5924 &rl_unsignedp, &volatilep, &rl_mask,
5926 rr_inner = decode_field_reference (loc, rr_arg,
5927 &rr_bitsize, &rr_bitpos, &rr_mode,
5928 &rr_unsignedp, &volatilep, &rr_mask,
5931 /* It must be true that the inner operation on the lhs of each
5932 comparison must be the same if we are to be able to do anything.
5933 Then see if we have constants. If not, the same must be true for
5935 if (volatilep || ll_inner == 0 || rl_inner == 0
5936 || ! operand_equal_p (ll_inner, rl_inner, 0))
5939 if (TREE_CODE (lr_arg) == INTEGER_CST
5940 && TREE_CODE (rr_arg) == INTEGER_CST)
5941 l_const = lr_arg, r_const = rr_arg;
5942 else if (lr_inner == 0 || rr_inner == 0
5943 || ! operand_equal_p (lr_inner, rr_inner, 0))
5946 l_const = r_const = 0;
5948 /* If either comparison code is not correct for our logical operation,
5949 fail. However, we can convert a one-bit comparison against zero into
5950 the opposite comparison against that bit being set in the field. */
5952 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5953 if (lcode != wanted_code)
5955 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5957 /* Make the left operand unsigned, since we are only interested
5958 in the value of one bit. Otherwise we are doing the wrong
5967 /* This is analogous to the code for l_const above. */
5968 if (rcode != wanted_code)
5970 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5979 /* See if we can find a mode that contains both fields being compared on
5980 the left. If we can't, fail. Otherwise, update all constants and masks
5981 to be relative to a field of that size. */
5982 first_bit = MIN (ll_bitpos, rl_bitpos);
5983 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5984 lnmode = get_best_mode (end_bit - first_bit, first_bit,
5985 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5987 if (lnmode == VOIDmode)
5990 lnbitsize = GET_MODE_BITSIZE (lnmode);
5991 lnbitpos = first_bit & ~ (lnbitsize - 1);
5992 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5993 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5995 if (BYTES_BIG_ENDIAN)
5997 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5998 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
6001 ll_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, ll_mask),
6002 size_int (xll_bitpos), 0);
6003 rl_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc, lntype, rl_mask),
6004 size_int (xrl_bitpos), 0);
6008 l_const = fold_convert_loc (loc, lntype, l_const);
6009 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
6010 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
6011 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
6012 fold_build1_loc (loc, BIT_NOT_EXPR,
6016 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
6018 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
6023 r_const = fold_convert_loc (loc, lntype, r_const);
6024 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
6025 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
6026 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
6027 fold_build1_loc (loc, BIT_NOT_EXPR,
6031 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
6033 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
6037 /* If the right sides are not constant, do the same for it. Also,
6038 disallow this optimization if a size or signedness mismatch occurs
6039 between the left and right sides. */
6042 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
6043 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
6044 /* Make sure the two fields on the right
6045 correspond to the left without being swapped. */
6046 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
6049 first_bit = MIN (lr_bitpos, rr_bitpos);
6050 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
6051 rnmode = get_best_mode (end_bit - first_bit, first_bit,
6052 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
6054 if (rnmode == VOIDmode)
6057 rnbitsize = GET_MODE_BITSIZE (rnmode);
6058 rnbitpos = first_bit & ~ (rnbitsize - 1);
6059 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
6060 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
6062 if (BYTES_BIG_ENDIAN)
6064 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
6065 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
6068 lr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
6070 size_int (xlr_bitpos), 0);
6071 rr_mask = const_binop (LSHIFT_EXPR, fold_convert_loc (loc,
6073 size_int (xrr_bitpos), 0);
6075 /* Make a mask that corresponds to both fields being compared.
6076 Do this for both items being compared. If the operands are the
6077 same size and the bits being compared are in the same position
6078 then we can do this by masking both and comparing the masked
6080 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
6081 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
6082 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
6084 lhs = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
6085 ll_unsignedp || rl_unsignedp);
6086 if (! all_ones_mask_p (ll_mask, lnbitsize))
6087 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
6089 rhs = make_bit_field_ref (loc, lr_inner, rntype, rnbitsize, rnbitpos,
6090 lr_unsignedp || rr_unsignedp);
6091 if (! all_ones_mask_p (lr_mask, rnbitsize))
6092 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
6094 result = build2 (wanted_code, truth_type, lhs, rhs);
6095 goto fold_truthop_exit;
6098 /* There is still another way we can do something: If both pairs of
6099 fields being compared are adjacent, we may be able to make a wider
6100 field containing them both.
6102 Note that we still must mask the lhs/rhs expressions. Furthermore,
6103 the mask must be shifted to account for the shift done by
6104 make_bit_field_ref. */
6105 if ((ll_bitsize + ll_bitpos == rl_bitpos
6106 && lr_bitsize + lr_bitpos == rr_bitpos)
6107 || (ll_bitpos == rl_bitpos + rl_bitsize
6108 && lr_bitpos == rr_bitpos + rr_bitsize))
6112 lhs = make_bit_field_ref (loc, ll_inner, lntype,
6113 ll_bitsize + rl_bitsize,
6114 MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
6115 rhs = make_bit_field_ref (loc, lr_inner, rntype,
6116 lr_bitsize + rr_bitsize,
6117 MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
6119 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
6120 size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
6121 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
6122 size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
6124 /* Convert to the smaller type before masking out unwanted bits. */
6126 if (lntype != rntype)
6128 if (lnbitsize > rnbitsize)
6130 lhs = fold_convert_loc (loc, rntype, lhs);
6131 ll_mask = fold_convert_loc (loc, rntype, ll_mask);
6134 else if (lnbitsize < rnbitsize)
6136 rhs = fold_convert_loc (loc, lntype, rhs);
6137 lr_mask = fold_convert_loc (loc, lntype, lr_mask);
6142 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
6143 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
6145 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
6146 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
6148 result = build2 (wanted_code, truth_type, lhs, rhs);
6149 goto fold_truthop_exit;
6155 /* Handle the case of comparisons with constants. If there is something in
6156 common between the masks, those bits of the constants must be the same.
6157 If not, the condition is always false. Test for this to avoid generating
6158 incorrect code below. */
6159 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
6160 if (! integer_zerop (result)
6161 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
6162 const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
6164 if (wanted_code == NE_EXPR)
6166 warning (0, "%<or%> of unmatched not-equal tests is always 1");
6167 return constant_boolean_node (true, truth_type);
6171 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
6172 return constant_boolean_node (false, truth_type);
6176 /* Construct the expression we will return. First get the component
6177 reference we will make. Unless the mask is all ones the width of
6178 that field, perform the mask operation. Then compare with the
6180 result = make_bit_field_ref (loc, ll_inner, lntype, lnbitsize, lnbitpos,
6181 ll_unsignedp || rl_unsignedp);
6183 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
6184 if (! all_ones_mask_p (ll_mask, lnbitsize))
6186 result = build2 (BIT_AND_EXPR, lntype, result, ll_mask);
6187 SET_EXPR_LOCATION (result, loc);
6190 result = build2 (wanted_code, truth_type, result,
6191 const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
6194 SET_EXPR_LOCATION (result, loc);
6198 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
6202 optimize_minmax_comparison (location_t loc, enum tree_code code, tree type,
6206 enum tree_code op_code;
6209 int consts_equal, consts_lt;
6212 STRIP_SIGN_NOPS (arg0);
6214 op_code = TREE_CODE (arg0);
6215 minmax_const = TREE_OPERAND (arg0, 1);
6216 comp_const = fold_convert_loc (loc, TREE_TYPE (arg0), op1);
6217 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
6218 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
6219 inner = TREE_OPERAND (arg0, 0);
6221 /* If something does not permit us to optimize, return the original tree. */
6222 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
6223 || TREE_CODE (comp_const) != INTEGER_CST
6224 || TREE_OVERFLOW (comp_const)
6225 || TREE_CODE (minmax_const) != INTEGER_CST
6226 || TREE_OVERFLOW (minmax_const))
6229 /* Now handle all the various comparison codes. We only handle EQ_EXPR
6230 and GT_EXPR, doing the rest with recursive calls using logical
6234 case NE_EXPR: case LT_EXPR: case LE_EXPR:
6237 = optimize_minmax_comparison (loc,
6238 invert_tree_comparison (code, false),
6241 return invert_truthvalue_loc (loc, tem);
6247 fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
6248 optimize_minmax_comparison
6249 (loc, EQ_EXPR, type, arg0, comp_const),
6250 optimize_minmax_comparison
6251 (loc, GT_EXPR, type, arg0, comp_const));
6254 if (op_code == MAX_EXPR && consts_equal)
6255 /* MAX (X, 0) == 0 -> X <= 0 */
6256 return fold_build2_loc (loc, LE_EXPR, type, inner, comp_const);
6258 else if (op_code == MAX_EXPR && consts_lt)
6259 /* MAX (X, 0) == 5 -> X == 5 */
6260 return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
6262 else if (op_code == MAX_EXPR)
6263 /* MAX (X, 0) == -1 -> false */
6264 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
6266 else if (consts_equal)
6267 /* MIN (X, 0) == 0 -> X >= 0 */
6268 return fold_build2_loc (loc, GE_EXPR, type, inner, comp_const);
6271 /* MIN (X, 0) == 5 -> false */
6272 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
6275 /* MIN (X, 0) == -1 -> X == -1 */
6276 return fold_build2_loc (loc, EQ_EXPR, type, inner, comp_const);
6279 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
6280 /* MAX (X, 0) > 0 -> X > 0
6281 MAX (X, 0) > 5 -> X > 5 */
6282 return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
6284 else if (op_code == MAX_EXPR)
6285 /* MAX (X, 0) > -1 -> true */
6286 return omit_one_operand_loc (loc, type, integer_one_node, inner);
6288 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
6289 /* MIN (X, 0) > 0 -> false
6290 MIN (X, 0) > 5 -> false */
6291 return omit_one_operand_loc (loc, type, integer_zero_node, inner);
6294 /* MIN (X, 0) > -1 -> X > -1 */
6295 return fold_build2_loc (loc, GT_EXPR, type, inner, comp_const);
6302 /* T is an integer expression that is being multiplied, divided, or taken a
6303 modulus (CODE says which and what kind of divide or modulus) by a
6304 constant C. See if we can eliminate that operation by folding it with
6305 other operations already in T. WIDE_TYPE, if non-null, is a type that
6306 should be used for the computation if wider than our type.
6308 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
6309 (X * 2) + (Y * 4). We must, however, be assured that either the original
6310 expression would not overflow or that overflow is undefined for the type
6311 in the language in question.
6313 If we return a non-null expression, it is an equivalent form of the
6314 original computation, but need not be in the original type.
6316 We set *STRICT_OVERFLOW_P to true if the return values depends on
6317 signed overflow being undefined. Otherwise we do not change
6318 *STRICT_OVERFLOW_P. */
6321 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
6322 bool *strict_overflow_p)
6324 /* To avoid exponential search depth, refuse to allow recursion past
6325 three levels. Beyond that (1) it's highly unlikely that we'll find
6326 something interesting and (2) we've probably processed it before
6327 when we built the inner expression. */
6336 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6343 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6344 bool *strict_overflow_p)
6346 tree type = TREE_TYPE (t);
6347 enum tree_code tcode = TREE_CODE (t);
6348 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
6349 > GET_MODE_SIZE (TYPE_MODE (type)))
6350 ? wide_type : type);
6352 int same_p = tcode == code;
6353 tree op0 = NULL_TREE, op1 = NULL_TREE;
6354 bool sub_strict_overflow_p;
6356 /* Don't deal with constants of zero here; they confuse the code below. */
6357 if (integer_zerop (c))
6360 if (TREE_CODE_CLASS (tcode) == tcc_unary)
6361 op0 = TREE_OPERAND (t, 0);
6363 if (TREE_CODE_CLASS (tcode) == tcc_binary)
6364 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6366 /* Note that we need not handle conditional operations here since fold
6367 already handles those cases. So just do arithmetic here. */
6371 /* For a constant, we can always simplify if we are a multiply
6372 or (for divide and modulus) if it is a multiple of our constant. */
6373 if (code == MULT_EXPR
6374 || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
6375 return const_binop (code, fold_convert (ctype, t),
6376 fold_convert (ctype, c), 0);
6379 CASE_CONVERT: case NON_LVALUE_EXPR:
6380 /* If op0 is an expression ... */
6381 if ((COMPARISON_CLASS_P (op0)
6382 || UNARY_CLASS_P (op0)
6383 || BINARY_CLASS_P (op0)
6384 || VL_EXP_CLASS_P (op0)
6385 || EXPRESSION_CLASS_P (op0))
6386 /* ... and has wrapping overflow, and its type is smaller
6387 than ctype, then we cannot pass through as widening. */
6388 && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
6389 && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
6390 && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
6391 && (TYPE_PRECISION (ctype)
6392 > TYPE_PRECISION (TREE_TYPE (op0))))
6393 /* ... or this is a truncation (t is narrower than op0),
6394 then we cannot pass through this narrowing. */
6395 || (TYPE_PRECISION (type)
6396 < TYPE_PRECISION (TREE_TYPE (op0)))
6397 /* ... or signedness changes for division or modulus,
6398 then we cannot pass through this conversion. */
6399 || (code != MULT_EXPR
6400 && (TYPE_UNSIGNED (ctype)
6401 != TYPE_UNSIGNED (TREE_TYPE (op0))))
6402 /* ... or has undefined overflow while the converted to
6403 type has not, we cannot do the operation in the inner type
6404 as that would introduce undefined overflow. */
6405 || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
6406 && !TYPE_OVERFLOW_UNDEFINED (type))))
6409 /* Pass the constant down and see if we can make a simplification. If
6410 we can, replace this expression with the inner simplification for
6411 possible later conversion to our or some other type. */
6412 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6413 && TREE_CODE (t2) == INTEGER_CST
6414 && !TREE_OVERFLOW (t2)
6415 && (0 != (t1 = extract_muldiv (op0, t2, code,
6417 ? ctype : NULL_TREE,
6418 strict_overflow_p))))
6423 /* If widening the type changes it from signed to unsigned, then we
6424 must avoid building ABS_EXPR itself as unsigned. */
6425 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6427 tree cstype = (*signed_type_for) (ctype);
6428 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6431 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6432 return fold_convert (ctype, t1);
6436 /* If the constant is negative, we cannot simplify this. */
6437 if (tree_int_cst_sgn (c) == -1)
6441 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6443 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6446 case MIN_EXPR: case MAX_EXPR:
6447 /* If widening the type changes the signedness, then we can't perform
6448 this optimization as that changes the result. */
6449 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6452 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6453 sub_strict_overflow_p = false;
6454 if ((t1 = extract_muldiv (op0, c, code, wide_type,
6455 &sub_strict_overflow_p)) != 0
6456 && (t2 = extract_muldiv (op1, c, code, wide_type,
6457 &sub_strict_overflow_p)) != 0)
6459 if (tree_int_cst_sgn (c) < 0)
6460 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6461 if (sub_strict_overflow_p)
6462 *strict_overflow_p = true;
6463 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6464 fold_convert (ctype, t2));
6468 case LSHIFT_EXPR: case RSHIFT_EXPR:
6469 /* If the second operand is constant, this is a multiplication
6470 or floor division, by a power of two, so we can treat it that
6471 way unless the multiplier or divisor overflows. Signed
6472 left-shift overflow is implementation-defined rather than
6473 undefined in C90, so do not convert signed left shift into
6475 if (TREE_CODE (op1) == INTEGER_CST
6476 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6477 /* const_binop may not detect overflow correctly,
6478 so check for it explicitly here. */
6479 && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
6480 && TREE_INT_CST_HIGH (op1) == 0
6481 && 0 != (t1 = fold_convert (ctype,
6482 const_binop (LSHIFT_EXPR,
6485 && !TREE_OVERFLOW (t1))
6486 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6487 ? MULT_EXPR : FLOOR_DIV_EXPR,
6489 fold_convert (ctype, op0),
6491 c, code, wide_type, strict_overflow_p);
6494 case PLUS_EXPR: case MINUS_EXPR:
6495 /* See if we can eliminate the operation on both sides. If we can, we
6496 can return a new PLUS or MINUS. If we can't, the only remaining
6497 cases where we can do anything are if the second operand is a
6499 sub_strict_overflow_p = false;
6500 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6501 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6502 if (t1 != 0 && t2 != 0
6503 && (code == MULT_EXPR
6504 /* If not multiplication, we can only do this if both operands
6505 are divisible by c. */
6506 || (multiple_of_p (ctype, op0, c)
6507 && multiple_of_p (ctype, op1, c))))
6509 if (sub_strict_overflow_p)
6510 *strict_overflow_p = true;
6511 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6512 fold_convert (ctype, t2));
6515 /* If this was a subtraction, negate OP1 and set it to be an addition.
6516 This simplifies the logic below. */
6517 if (tcode == MINUS_EXPR)
6519 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6520 /* If OP1 was not easily negatable, the constant may be OP0. */
6521 if (TREE_CODE (op0) == INTEGER_CST)
6532 if (TREE_CODE (op1) != INTEGER_CST)
6535 /* If either OP1 or C are negative, this optimization is not safe for
6536 some of the division and remainder types while for others we need
6537 to change the code. */
6538 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6540 if (code == CEIL_DIV_EXPR)
6541 code = FLOOR_DIV_EXPR;
6542 else if (code == FLOOR_DIV_EXPR)
6543 code = CEIL_DIV_EXPR;
6544 else if (code != MULT_EXPR
6545 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6549 /* If it's a multiply or a division/modulus operation of a multiple
6550 of our constant, do the operation and verify it doesn't overflow. */
6551 if (code == MULT_EXPR
6552 || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6554 op1 = const_binop (code, fold_convert (ctype, op1),
6555 fold_convert (ctype, c), 0);
6556 /* We allow the constant to overflow with wrapping semantics. */
6558 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6564 /* If we have an unsigned type is not a sizetype, we cannot widen
6565 the operation since it will change the result if the original
6566 computation overflowed. */
6567 if (TYPE_UNSIGNED (ctype)
6568 && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
6572 /* If we were able to eliminate our operation from the first side,
6573 apply our operation to the second side and reform the PLUS. */
6574 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
6575 return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
6577 /* The last case is if we are a multiply. In that case, we can
6578 apply the distributive law to commute the multiply and addition
6579 if the multiplication of the constants doesn't overflow. */
6580 if (code == MULT_EXPR)
6581 return fold_build2 (tcode, ctype,
6582 fold_build2 (code, ctype,
6583 fold_convert (ctype, op0),
6584 fold_convert (ctype, c)),
6590 /* We have a special case here if we are doing something like
6591 (C * 8) % 4 since we know that's zero. */
6592 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6593 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6594 /* If the multiplication can overflow we cannot optimize this.
6595 ??? Until we can properly mark individual operations as
6596 not overflowing we need to treat sizetype special here as
6597 stor-layout relies on this opimization to make
6598 DECL_FIELD_BIT_OFFSET always a constant. */
6599 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6600 || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
6601 && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
6602 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6603 && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6605 *strict_overflow_p = true;
6606 return omit_one_operand (type, integer_zero_node, op0);
6609 /* ... fall through ... */
6611 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6612 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6613 /* If we can extract our operation from the LHS, do so and return a
6614 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6615 do something only if the second operand is a constant. */
6617 && (t1 = extract_muldiv (op0, c, code, wide_type,
6618 strict_overflow_p)) != 0)
6619 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6620 fold_convert (ctype, op1));
6621 else if (tcode == MULT_EXPR && code == MULT_EXPR
6622 && (t1 = extract_muldiv (op1, c, code, wide_type,
6623 strict_overflow_p)) != 0)
6624 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6625 fold_convert (ctype, t1));
6626 else if (TREE_CODE (op1) != INTEGER_CST)
6629 /* If these are the same operation types, we can associate them
6630 assuming no overflow. */
6632 && 0 != (t1 = int_const_binop (MULT_EXPR,
6633 fold_convert (ctype, op1),
6634 fold_convert (ctype, c), 1))
6635 && 0 != (t1 = force_fit_type_double (ctype, TREE_INT_CST_LOW (t1),
6636 TREE_INT_CST_HIGH (t1),
6637 (TYPE_UNSIGNED (ctype)
6638 && tcode != MULT_EXPR) ? -1 : 1,
6639 TREE_OVERFLOW (t1)))
6640 && !TREE_OVERFLOW (t1))
6641 return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1);
6643 /* If these operations "cancel" each other, we have the main
6644 optimizations of this pass, which occur when either constant is a
6645 multiple of the other, in which case we replace this with either an
6646 operation or CODE or TCODE.
6648 If we have an unsigned type that is not a sizetype, we cannot do
6649 this since it will change the result if the original computation
6651 if ((TYPE_OVERFLOW_UNDEFINED (ctype)
6652 || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
6653 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6654 || (tcode == MULT_EXPR
6655 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6656 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6657 && code != MULT_EXPR)))
6659 if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6661 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6662 *strict_overflow_p = true;
6663 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6664 fold_convert (ctype,
6665 const_binop (TRUNC_DIV_EXPR,
6668 else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
6670 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6671 *strict_overflow_p = true;
6672 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6673 fold_convert (ctype,
6674 const_binop (TRUNC_DIV_EXPR,
6687 /* Return a node which has the indicated constant VALUE (either 0 or
6688 1), and is of the indicated TYPE. */
6691 constant_boolean_node (int value, tree type)
6693 if (type == integer_type_node)
6694 return value ? integer_one_node : integer_zero_node;
6695 else if (type == boolean_type_node)
6696 return value ? boolean_true_node : boolean_false_node;
6698 return build_int_cst (type, value);
6702 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6703 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6704 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6705 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6706 COND is the first argument to CODE; otherwise (as in the example
6707 given here), it is the second argument. TYPE is the type of the
6708 original expression. Return NULL_TREE if no simplification is
6712 fold_binary_op_with_conditional_arg (location_t loc,
6713 enum tree_code code,
6714 tree type, tree op0, tree op1,
6715 tree cond, tree arg, int cond_first_p)
6717 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6718 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6719 tree test, true_value, false_value;
6720 tree lhs = NULL_TREE;
6721 tree rhs = NULL_TREE;
6723 /* This transformation is only worthwhile if we don't have to wrap
6724 arg in a SAVE_EXPR, and the operation can be simplified on at least
6725 one of the branches once its pushed inside the COND_EXPR. */
6726 if (!TREE_CONSTANT (arg))
6729 if (TREE_CODE (cond) == COND_EXPR)
6731 test = TREE_OPERAND (cond, 0);
6732 true_value = TREE_OPERAND (cond, 1);
6733 false_value = TREE_OPERAND (cond, 2);
6734 /* If this operand throws an expression, then it does not make
6735 sense to try to perform a logical or arithmetic operation
6737 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6739 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6744 tree testtype = TREE_TYPE (cond);
6746 true_value = constant_boolean_node (true, testtype);
6747 false_value = constant_boolean_node (false, testtype);
6750 arg = fold_convert_loc (loc, arg_type, arg);
6753 true_value = fold_convert_loc (loc, cond_type, true_value);
6755 lhs = fold_build2_loc (loc, code, type, true_value, arg);
6757 lhs = fold_build2_loc (loc, code, type, arg, true_value);
6761 false_value = fold_convert_loc (loc, cond_type, false_value);
6763 rhs = fold_build2_loc (loc, code, type, false_value, arg);
6765 rhs = fold_build2_loc (loc, code, type, arg, false_value);
6768 test = fold_build3_loc (loc, COND_EXPR, type, test, lhs, rhs);
6769 return fold_convert_loc (loc, type, test);
6773 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6775 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6776 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6777 ADDEND is the same as X.
6779 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6780 and finite. The problematic cases are when X is zero, and its mode
6781 has signed zeros. In the case of rounding towards -infinity,
6782 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6783 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6786 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6788 if (!real_zerop (addend))
6791 /* Don't allow the fold with -fsignaling-nans. */
6792 if (HONOR_SNANS (TYPE_MODE (type)))
6795 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6796 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6799 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6800 if (TREE_CODE (addend) == REAL_CST
6801 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6804 /* The mode has signed zeros, and we have to honor their sign.
6805 In this situation, there is only one case we can return true for.
6806 X - 0 is the same as X unless rounding towards -infinity is
6808 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6811 /* Subroutine of fold() that checks comparisons of built-in math
6812 functions against real constants.
6814 FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6815 operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR. TYPE
6816 is the type of the result and ARG0 and ARG1 are the operands of the
6817 comparison. ARG1 must be a TREE_REAL_CST.
6819 The function returns the constant folded tree if a simplification
6820 can be made, and NULL_TREE otherwise. */
6823 fold_mathfn_compare (location_t loc,
6824 enum built_in_function fcode, enum tree_code code,
6825 tree type, tree arg0, tree arg1)
6829 if (BUILTIN_SQRT_P (fcode))
6831 tree arg = CALL_EXPR_ARG (arg0, 0);
6832 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6834 c = TREE_REAL_CST (arg1);
6835 if (REAL_VALUE_NEGATIVE (c))
6837 /* sqrt(x) < y is always false, if y is negative. */
6838 if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6839 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6841 /* sqrt(x) > y is always true, if y is negative and we
6842 don't care about NaNs, i.e. negative values of x. */
6843 if (code == NE_EXPR || !HONOR_NANS (mode))
6844 return omit_one_operand_loc (loc, type, integer_one_node, arg);
6846 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
6847 return fold_build2_loc (loc, GE_EXPR, type, arg,
6848 build_real (TREE_TYPE (arg), dconst0));
6850 else if (code == GT_EXPR || code == GE_EXPR)
6854 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6855 real_convert (&c2, mode, &c2);
6857 if (REAL_VALUE_ISINF (c2))
6859 /* sqrt(x) > y is x == +Inf, when y is very large. */
6860 if (HONOR_INFINITIES (mode))
6861 return fold_build2_loc (loc, EQ_EXPR, type, arg,
6862 build_real (TREE_TYPE (arg), c2));
6864 /* sqrt(x) > y is always false, when y is very large
6865 and we don't care about infinities. */
6866 return omit_one_operand_loc (loc, type, integer_zero_node, arg);
6869 /* sqrt(x) > c is the same as x > c*c. */
6870 return fold_build2_loc (loc, code, type, arg,
6871 build_real (TREE_TYPE (arg), c2));
6873 else if (code == LT_EXPR || code == LE_EXPR)
6877 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6878 real_convert (&c2, mode, &c2);
6880 if (REAL_VALUE_ISINF (c2))
6882 /* sqrt(x) < y is always true, when y is a very large
6883 value and we don't care about NaNs or Infinities. */
6884 if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6885 return omit_one_operand_loc (loc, type, integer_one_node, arg);
6887 /* sqrt(x) < y is x != +Inf when y is very large and we
6888 don't care about NaNs. */
6889 if (! HONOR_NANS (mode))
6890 return fold_build2_loc (loc, NE_EXPR, type, arg,
6891 build_real (TREE_TYPE (arg), c2));
6893 /* sqrt(x) < y is x >= 0 when y is very large and we
6894 don't care about Infinities. */
6895 if (! HONOR_INFINITIES (mode))
6896 return fold_build2_loc (loc, GE_EXPR, type, arg,
6897 build_real (TREE_TYPE (arg), dconst0));
6899 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
6900 if (lang_hooks.decls.global_bindings_p () != 0
6901 || CONTAINS_PLACEHOLDER_P (arg))
6904 arg = save_expr (arg);
6905 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6906 fold_build2_loc (loc, GE_EXPR, type, arg,
6907 build_real (TREE_TYPE (arg),
6909 fold_build2_loc (loc, NE_EXPR, type, arg,
6910 build_real (TREE_TYPE (arg),
6914 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
6915 if (! HONOR_NANS (mode))
6916 return fold_build2_loc (loc, code, type, arg,
6917 build_real (TREE_TYPE (arg), c2));
6919 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
6920 if (lang_hooks.decls.global_bindings_p () == 0
6921 && ! CONTAINS_PLACEHOLDER_P (arg))
6923 arg = save_expr (arg);
6924 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
6925 fold_build2_loc (loc, GE_EXPR, type, arg,
6926 build_real (TREE_TYPE (arg),
6928 fold_build2_loc (loc, code, type, arg,
6929 build_real (TREE_TYPE (arg),
6938 /* Subroutine of fold() that optimizes comparisons against Infinities,
6939 either +Inf or -Inf.
6941 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6942 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6943 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6945 The function returns the constant folded tree if a simplification
6946 can be made, and NULL_TREE otherwise. */
6949 fold_inf_compare (location_t loc, enum tree_code code, tree type,
6950 tree arg0, tree arg1)
6952 enum machine_mode mode;
6953 REAL_VALUE_TYPE max;
6957 mode = TYPE_MODE (TREE_TYPE (arg0));
6959 /* For negative infinity swap the sense of the comparison. */
6960 neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6962 code = swap_tree_comparison (code);
6967 /* x > +Inf is always false, if with ignore sNANs. */
6968 if (HONOR_SNANS (mode))
6970 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
6973 /* x <= +Inf is always true, if we don't case about NaNs. */
6974 if (! HONOR_NANS (mode))
6975 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
6977 /* x <= +Inf is the same as x == x, i.e. isfinite(x). */
6978 if (lang_hooks.decls.global_bindings_p () == 0
6979 && ! CONTAINS_PLACEHOLDER_P (arg0))
6981 arg0 = save_expr (arg0);
6982 return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg0);
6988 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
6989 real_maxval (&max, neg, mode);
6990 return fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
6991 arg0, build_real (TREE_TYPE (arg0), max));
6994 /* x < +Inf is always equal to x <= DBL_MAX. */
6995 real_maxval (&max, neg, mode);
6996 return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
6997 arg0, build_real (TREE_TYPE (arg0), max));
7000 /* x != +Inf is always equal to !(x > DBL_MAX). */
7001 real_maxval (&max, neg, mode);
7002 if (! HONOR_NANS (mode))
7003 return fold_build2_loc (loc, neg ? GE_EXPR : LE_EXPR, type,
7004 arg0, build_real (TREE_TYPE (arg0), max));
7006 temp = fold_build2_loc (loc, neg ? LT_EXPR : GT_EXPR, type,
7007 arg0, build_real (TREE_TYPE (arg0), max));
7008 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type, temp);
7017 /* Subroutine of fold() that optimizes comparisons of a division by
7018 a nonzero integer constant against an integer constant, i.e.
7021 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
7022 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
7023 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
7025 The function returns the constant folded tree if a simplification
7026 can be made, and NULL_TREE otherwise. */
7029 fold_div_compare (location_t loc,
7030 enum tree_code code, tree type, tree arg0, tree arg1)
7032 tree prod, tmp, hi, lo;
7033 tree arg00 = TREE_OPERAND (arg0, 0);
7034 tree arg01 = TREE_OPERAND (arg0, 1);
7035 unsigned HOST_WIDE_INT lpart;
7036 HOST_WIDE_INT hpart;
7037 bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
7041 /* We have to do this the hard way to detect unsigned overflow.
7042 prod = int_const_binop (MULT_EXPR, arg01, arg1, 0); */
7043 overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
7044 TREE_INT_CST_HIGH (arg01),
7045 TREE_INT_CST_LOW (arg1),
7046 TREE_INT_CST_HIGH (arg1),
7047 &lpart, &hpart, unsigned_p);
7048 prod = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
7050 neg_overflow = false;
7054 tmp = int_const_binop (MINUS_EXPR, arg01,
7055 build_int_cst (TREE_TYPE (arg01), 1), 0);
7058 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0). */
7059 overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
7060 TREE_INT_CST_HIGH (prod),
7061 TREE_INT_CST_LOW (tmp),
7062 TREE_INT_CST_HIGH (tmp),
7063 &lpart, &hpart, unsigned_p);
7064 hi = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
7065 -1, overflow | TREE_OVERFLOW (prod));
7067 else if (tree_int_cst_sgn (arg01) >= 0)
7069 tmp = int_const_binop (MINUS_EXPR, arg01,
7070 build_int_cst (TREE_TYPE (arg01), 1), 0);
7071 switch (tree_int_cst_sgn (arg1))
7074 neg_overflow = true;
7075 lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
7080 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
7085 hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
7095 /* A negative divisor reverses the relational operators. */
7096 code = swap_tree_comparison (code);
7098 tmp = int_const_binop (PLUS_EXPR, arg01,
7099 build_int_cst (TREE_TYPE (arg01), 1), 0);
7100 switch (tree_int_cst_sgn (arg1))
7103 hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
7108 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
7113 neg_overflow = true;
7114 lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
7126 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
7127 return omit_one_operand_loc (loc, type, integer_zero_node, arg00);
7128 if (TREE_OVERFLOW (hi))
7129 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
7130 if (TREE_OVERFLOW (lo))
7131 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
7132 return build_range_check (loc, type, arg00, 1, lo, hi);
7135 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
7136 return omit_one_operand_loc (loc, type, integer_one_node, arg00);
7137 if (TREE_OVERFLOW (hi))
7138 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
7139 if (TREE_OVERFLOW (lo))
7140 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
7141 return build_range_check (loc, type, arg00, 0, lo, hi);
7144 if (TREE_OVERFLOW (lo))
7146 tmp = neg_overflow ? integer_zero_node : integer_one_node;
7147 return omit_one_operand_loc (loc, type, tmp, arg00);
7149 return fold_build2_loc (loc, LT_EXPR, type, arg00, lo);
7152 if (TREE_OVERFLOW (hi))
7154 tmp = neg_overflow ? integer_zero_node : integer_one_node;
7155 return omit_one_operand_loc (loc, type, tmp, arg00);
7157 return fold_build2_loc (loc, LE_EXPR, type, arg00, hi);
7160 if (TREE_OVERFLOW (hi))
7162 tmp = neg_overflow ? integer_one_node : integer_zero_node;
7163 return omit_one_operand_loc (loc, type, tmp, arg00);
7165 return fold_build2_loc (loc, GT_EXPR, type, arg00, hi);
7168 if (TREE_OVERFLOW (lo))
7170 tmp = neg_overflow ? integer_one_node : integer_zero_node;
7171 return omit_one_operand_loc (loc, type, tmp, arg00);
7173 return fold_build2_loc (loc, GE_EXPR, type, arg00, lo);
7183 /* If CODE with arguments ARG0 and ARG1 represents a single bit
7184 equality/inequality test, then return a simplified form of the test
7185 using a sign testing. Otherwise return NULL. TYPE is the desired
7189 fold_single_bit_test_into_sign_test (location_t loc,
7190 enum tree_code code, tree arg0, tree arg1,
7193 /* If this is testing a single bit, we can optimize the test. */
7194 if ((code == NE_EXPR || code == EQ_EXPR)
7195 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
7196 && integer_pow2p (TREE_OPERAND (arg0, 1)))
7198 /* If we have (A & C) != 0 where C is the sign bit of A, convert
7199 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
7200 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
7202 if (arg00 != NULL_TREE
7203 /* This is only a win if casting to a signed type is cheap,
7204 i.e. when arg00's type is not a partial mode. */
7205 && TYPE_PRECISION (TREE_TYPE (arg00))
7206 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
7208 tree stype = signed_type_for (TREE_TYPE (arg00));
7209 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
7211 fold_convert_loc (loc, stype, arg00),
7212 build_int_cst (stype, 0));
7219 /* If CODE with arguments ARG0 and ARG1 represents a single bit
7220 equality/inequality test, then return a simplified form of
7221 the test using shifts and logical operations. Otherwise return
7222 NULL. TYPE is the desired result type. */
7225 fold_single_bit_test (location_t loc, enum tree_code code,
7226 tree arg0, tree arg1, tree result_type)
7228 /* If this is testing a single bit, we can optimize the test. */
7229 if ((code == NE_EXPR || code == EQ_EXPR)
7230 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
7231 && integer_pow2p (TREE_OPERAND (arg0, 1)))
7233 tree inner = TREE_OPERAND (arg0, 0);
7234 tree type = TREE_TYPE (arg0);
7235 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
7236 enum machine_mode operand_mode = TYPE_MODE (type);
7238 tree signed_type, unsigned_type, intermediate_type;
7241 /* First, see if we can fold the single bit test into a sign-bit
7243 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1,
7248 /* Otherwise we have (A & C) != 0 where C is a single bit,
7249 convert that into ((A >> C2) & 1). Where C2 = log2(C).
7250 Similarly for (A & C) == 0. */
7252 /* If INNER is a right shift of a constant and it plus BITNUM does
7253 not overflow, adjust BITNUM and INNER. */
7254 if (TREE_CODE (inner) == RSHIFT_EXPR
7255 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
7256 && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
7257 && bitnum < TYPE_PRECISION (type)
7258 && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
7259 bitnum - TYPE_PRECISION (type)))
7261 bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
7262 inner = TREE_OPERAND (inner, 0);
7265 /* If we are going to be able to omit the AND below, we must do our
7266 operations as unsigned. If we must use the AND, we have a choice.
7267 Normally unsigned is faster, but for some machines signed is. */
7268 #ifdef LOAD_EXTEND_OP
7269 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
7270 && !flag_syntax_only) ? 0 : 1;
7275 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
7276 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
7277 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
7278 inner = fold_convert_loc (loc, intermediate_type, inner);
7281 inner = build2 (RSHIFT_EXPR, intermediate_type,
7282 inner, size_int (bitnum));
7284 one = build_int_cst (intermediate_type, 1);
7286 if (code == EQ_EXPR)
7287 inner = fold_build2_loc (loc, BIT_XOR_EXPR, intermediate_type, inner, one);
7289 /* Put the AND last so it can combine with more things. */
7290 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
7292 /* Make sure to return the proper type. */
7293 inner = fold_convert_loc (loc, result_type, inner);
7300 /* Check whether we are allowed to reorder operands arg0 and arg1,
7301 such that the evaluation of arg1 occurs before arg0. */
7304 reorder_operands_p (const_tree arg0, const_tree arg1)
7306 if (! flag_evaluation_order)
7308 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
7310 return ! TREE_SIDE_EFFECTS (arg0)
7311 && ! TREE_SIDE_EFFECTS (arg1);
7314 /* Test whether it is preferable two swap two operands, ARG0 and
7315 ARG1, for example because ARG0 is an integer constant and ARG1
7316 isn't. If REORDER is true, only recommend swapping if we can
7317 evaluate the operands in reverse order. */
7320 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
7322 STRIP_SIGN_NOPS (arg0);
7323 STRIP_SIGN_NOPS (arg1);
7325 if (TREE_CODE (arg1) == INTEGER_CST)
7327 if (TREE_CODE (arg0) == INTEGER_CST)
7330 if (TREE_CODE (arg1) == REAL_CST)
7332 if (TREE_CODE (arg0) == REAL_CST)
7335 if (TREE_CODE (arg1) == FIXED_CST)
7337 if (TREE_CODE (arg0) == FIXED_CST)
7340 if (TREE_CODE (arg1) == COMPLEX_CST)
7342 if (TREE_CODE (arg0) == COMPLEX_CST)
7345 if (TREE_CONSTANT (arg1))
7347 if (TREE_CONSTANT (arg0))
7350 if (optimize_function_for_size_p (cfun))
7353 if (reorder && flag_evaluation_order
7354 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
7357 /* It is preferable to swap two SSA_NAME to ensure a canonical form
7358 for commutative and comparison operators. Ensuring a canonical
7359 form allows the optimizers to find additional redundancies without
7360 having to explicitly check for both orderings. */
7361 if (TREE_CODE (arg0) == SSA_NAME
7362 && TREE_CODE (arg1) == SSA_NAME
7363 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
7366 /* Put SSA_NAMEs last. */
7367 if (TREE_CODE (arg1) == SSA_NAME)
7369 if (TREE_CODE (arg0) == SSA_NAME)
7372 /* Put variables last. */
7381 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
7382 ARG0 is extended to a wider type. */
7385 fold_widened_comparison (location_t loc, enum tree_code code,
7386 tree type, tree arg0, tree arg1)
7388 tree arg0_unw = get_unwidened (arg0, NULL_TREE);
7390 tree shorter_type, outer_type;
7394 if (arg0_unw == arg0)
7396 shorter_type = TREE_TYPE (arg0_unw);
7398 #ifdef HAVE_canonicalize_funcptr_for_compare
7399 /* Disable this optimization if we're casting a function pointer
7400 type on targets that require function pointer canonicalization. */
7401 if (HAVE_canonicalize_funcptr_for_compare
7402 && TREE_CODE (shorter_type) == POINTER_TYPE
7403 && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
7407 if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
7410 arg1_unw = get_unwidened (arg1, NULL_TREE);
7412 /* If possible, express the comparison in the shorter mode. */
7413 if ((code == EQ_EXPR || code == NE_EXPR
7414 || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
7415 && (TREE_TYPE (arg1_unw) == shorter_type
7416 || ((TYPE_PRECISION (shorter_type)
7417 >= TYPE_PRECISION (TREE_TYPE (arg1_unw)))
7418 && (TYPE_UNSIGNED (shorter_type)
7419 == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
7420 || (TREE_CODE (arg1_unw) == INTEGER_CST
7421 && (TREE_CODE (shorter_type) == INTEGER_TYPE
7422 || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
7423 && int_fits_type_p (arg1_unw, shorter_type))))
7424 return fold_build2_loc (loc, code, type, arg0_unw,
7425 fold_convert_loc (loc, shorter_type, arg1_unw));
7427 if (TREE_CODE (arg1_unw) != INTEGER_CST
7428 || TREE_CODE (shorter_type) != INTEGER_TYPE
7429 || !int_fits_type_p (arg1_unw, shorter_type))
7432 /* If we are comparing with the integer that does not fit into the range
7433 of the shorter type, the result is known. */
7434 outer_type = TREE_TYPE (arg1_unw);
7435 min = lower_bound_in_type (outer_type, shorter_type);
7436 max = upper_bound_in_type (outer_type, shorter_type);
7438 above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7440 below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7447 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
7452 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
7458 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
7460 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
7465 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
7467 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
7476 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
7477 ARG0 just the signedness is changed. */
7480 fold_sign_changed_comparison (location_t loc, enum tree_code code, tree type,
7481 tree arg0, tree arg1)
7484 tree inner_type, outer_type;
7486 if (!CONVERT_EXPR_P (arg0))
7489 outer_type = TREE_TYPE (arg0);
7490 arg0_inner = TREE_OPERAND (arg0, 0);
7491 inner_type = TREE_TYPE (arg0_inner);
7493 #ifdef HAVE_canonicalize_funcptr_for_compare
7494 /* Disable this optimization if we're casting a function pointer
7495 type on targets that require function pointer canonicalization. */
7496 if (HAVE_canonicalize_funcptr_for_compare
7497 && TREE_CODE (inner_type) == POINTER_TYPE
7498 && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
7502 if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
7505 if (TREE_CODE (arg1) != INTEGER_CST
7506 && !(CONVERT_EXPR_P (arg1)
7507 && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
7510 if ((TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
7511 || POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type))
7516 if (TREE_CODE (arg1) == INTEGER_CST)
7517 arg1 = force_fit_type_double (inner_type, TREE_INT_CST_LOW (arg1),
7518 TREE_INT_CST_HIGH (arg1), 0,
7519 TREE_OVERFLOW (arg1));
7521 arg1 = fold_convert_loc (loc, inner_type, arg1);
7523 return fold_build2_loc (loc, code, type, arg0_inner, arg1);
7526 /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
7527 step of the array. Reconstructs s and delta in the case of s *
7528 delta being an integer constant (and thus already folded). ADDR is
7529 the address. MULT is the multiplicative expression. If the
7530 function succeeds, the new address expression is returned.
7531 Otherwise NULL_TREE is returned. LOC is the location of the
7532 resulting expression. */
7535 try_move_mult_to_index (location_t loc, tree addr, tree op1)
7537 tree s, delta, step;
7538 tree ref = TREE_OPERAND (addr, 0), pref;
7543 /* Strip the nops that might be added when converting op1 to sizetype. */
7546 /* Canonicalize op1 into a possibly non-constant delta
7547 and an INTEGER_CST s. */
7548 if (TREE_CODE (op1) == MULT_EXPR)
7550 tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
7555 if (TREE_CODE (arg0) == INTEGER_CST)
7560 else if (TREE_CODE (arg1) == INTEGER_CST)
7568 else if (TREE_CODE (op1) == INTEGER_CST)
7575 /* Simulate we are delta * 1. */
7577 s = integer_one_node;
7580 for (;; ref = TREE_OPERAND (ref, 0))
7582 if (TREE_CODE (ref) == ARRAY_REF)
7586 /* Remember if this was a multi-dimensional array. */
7587 if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
7590 domain = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
7593 itype = TREE_TYPE (domain);
7595 step = array_ref_element_size (ref);
7596 if (TREE_CODE (step) != INTEGER_CST)
7601 if (! tree_int_cst_equal (step, s))
7606 /* Try if delta is a multiple of step. */
7607 tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step);
7613 /* Only fold here if we can verify we do not overflow one
7614 dimension of a multi-dimensional array. */
7619 if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
7620 || !TYPE_MAX_VALUE (domain)
7621 || TREE_CODE (TYPE_MAX_VALUE (domain)) != INTEGER_CST)
7624 tmp = fold_binary_loc (loc, PLUS_EXPR, itype,
7625 fold_convert_loc (loc, itype,
7626 TREE_OPERAND (ref, 1)),
7627 fold_convert_loc (loc, itype, delta));
7629 || TREE_CODE (tmp) != INTEGER_CST
7630 || tree_int_cst_lt (TYPE_MAX_VALUE (domain), tmp))
7639 if (!handled_component_p (ref))
7643 /* We found the suitable array reference. So copy everything up to it,
7644 and replace the index. */
7646 pref = TREE_OPERAND (addr, 0);
7647 ret = copy_node (pref);
7648 SET_EXPR_LOCATION (ret, loc);
7653 pref = TREE_OPERAND (pref, 0);
7654 TREE_OPERAND (pos, 0) = copy_node (pref);
7655 pos = TREE_OPERAND (pos, 0);
7658 TREE_OPERAND (pos, 1) = fold_build2_loc (loc, PLUS_EXPR, itype,
7659 fold_convert_loc (loc, itype,
7660 TREE_OPERAND (pos, 1)),
7661 fold_convert_loc (loc, itype, delta));
7663 return fold_build1_loc (loc, ADDR_EXPR, TREE_TYPE (addr), ret);
7667 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
7668 means A >= Y && A != MAX, but in this case we know that
7669 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7672 fold_to_nonsharp_ineq_using_bound (location_t loc, tree ineq, tree bound)
7674 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
7676 if (TREE_CODE (bound) == LT_EXPR)
7677 a = TREE_OPERAND (bound, 0);
7678 else if (TREE_CODE (bound) == GT_EXPR)
7679 a = TREE_OPERAND (bound, 1);
7683 typea = TREE_TYPE (a);
7684 if (!INTEGRAL_TYPE_P (typea)
7685 && !POINTER_TYPE_P (typea))
7688 if (TREE_CODE (ineq) == LT_EXPR)
7690 a1 = TREE_OPERAND (ineq, 1);
7691 y = TREE_OPERAND (ineq, 0);
7693 else if (TREE_CODE (ineq) == GT_EXPR)
7695 a1 = TREE_OPERAND (ineq, 0);
7696 y = TREE_OPERAND (ineq, 1);
7701 if (TREE_TYPE (a1) != typea)
7704 if (POINTER_TYPE_P (typea))
7706 /* Convert the pointer types into integer before taking the difference. */
7707 tree ta = fold_convert_loc (loc, ssizetype, a);
7708 tree ta1 = fold_convert_loc (loc, ssizetype, a1);
7709 diff = fold_binary_loc (loc, MINUS_EXPR, ssizetype, ta1, ta);
7712 diff = fold_binary_loc (loc, MINUS_EXPR, typea, a1, a);
7714 if (!diff || !integer_onep (diff))
7717 return fold_build2_loc (loc, GE_EXPR, type, a, y);
7720 /* Fold a sum or difference of at least one multiplication.
7721 Returns the folded tree or NULL if no simplification could be made. */
7724 fold_plusminus_mult_expr (location_t loc, enum tree_code code, tree type,
7725 tree arg0, tree arg1)
7727 tree arg00, arg01, arg10, arg11;
7728 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7730 /* (A * C) +- (B * C) -> (A+-B) * C.
7731 (A * C) +- A -> A * (C+-1).
7732 We are most concerned about the case where C is a constant,
7733 but other combinations show up during loop reduction. Since
7734 it is not difficult, try all four possibilities. */
7736 if (TREE_CODE (arg0) == MULT_EXPR)
7738 arg00 = TREE_OPERAND (arg0, 0);
7739 arg01 = TREE_OPERAND (arg0, 1);
7741 else if (TREE_CODE (arg0) == INTEGER_CST)
7743 arg00 = build_one_cst (type);
7748 /* We cannot generate constant 1 for fract. */
7749 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7752 arg01 = build_one_cst (type);
7754 if (TREE_CODE (arg1) == MULT_EXPR)
7756 arg10 = TREE_OPERAND (arg1, 0);
7757 arg11 = TREE_OPERAND (arg1, 1);
7759 else if (TREE_CODE (arg1) == INTEGER_CST)
7761 arg10 = build_one_cst (type);
7762 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7763 the purpose of this canonicalization. */
7764 if (TREE_INT_CST_HIGH (arg1) == -1
7765 && negate_expr_p (arg1)
7766 && code == PLUS_EXPR)
7768 arg11 = negate_expr (arg1);
7776 /* We cannot generate constant 1 for fract. */
7777 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7780 arg11 = build_one_cst (type);
7784 if (operand_equal_p (arg01, arg11, 0))
7785 same = arg01, alt0 = arg00, alt1 = arg10;
7786 else if (operand_equal_p (arg00, arg10, 0))
7787 same = arg00, alt0 = arg01, alt1 = arg11;
7788 else if (operand_equal_p (arg00, arg11, 0))
7789 same = arg00, alt0 = arg01, alt1 = arg10;
7790 else if (operand_equal_p (arg01, arg10, 0))
7791 same = arg01, alt0 = arg00, alt1 = arg11;
7793 /* No identical multiplicands; see if we can find a common
7794 power-of-two factor in non-power-of-two multiplies. This
7795 can help in multi-dimensional array access. */
7796 else if (host_integerp (arg01, 0)
7797 && host_integerp (arg11, 0))
7799 HOST_WIDE_INT int01, int11, tmp;
7802 int01 = TREE_INT_CST_LOW (arg01);
7803 int11 = TREE_INT_CST_LOW (arg11);
7805 /* Move min of absolute values to int11. */
7806 if ((int01 >= 0 ? int01 : -int01)
7807 < (int11 >= 0 ? int11 : -int11))
7809 tmp = int01, int01 = int11, int11 = tmp;
7810 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7817 if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0
7818 /* The remainder should not be a constant, otherwise we
7819 end up folding i * 4 + 2 to (i * 2 + 1) * 2 which has
7820 increased the number of multiplications necessary. */
7821 && TREE_CODE (arg10) != INTEGER_CST)
7823 alt0 = fold_build2_loc (loc, MULT_EXPR, TREE_TYPE (arg00), arg00,
7824 build_int_cst (TREE_TYPE (arg00),
7829 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7834 return fold_build2_loc (loc, MULT_EXPR, type,
7835 fold_build2_loc (loc, code, type,
7836 fold_convert_loc (loc, type, alt0),
7837 fold_convert_loc (loc, type, alt1)),
7838 fold_convert_loc (loc, type, same));
7843 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7844 specified by EXPR into the buffer PTR of length LEN bytes.
7845 Return the number of bytes placed in the buffer, or zero
7849 native_encode_int (const_tree expr, unsigned char *ptr, int len)
7851 tree type = TREE_TYPE (expr);
7852 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7853 int byte, offset, word, words;
7854 unsigned char value;
7856 if (total_bytes > len)
7858 words = total_bytes / UNITS_PER_WORD;
7860 for (byte = 0; byte < total_bytes; byte++)
7862 int bitpos = byte * BITS_PER_UNIT;
7863 if (bitpos < HOST_BITS_PER_WIDE_INT)
7864 value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7866 value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7867 >> (bitpos - HOST_BITS_PER_WIDE_INT));
7869 if (total_bytes > UNITS_PER_WORD)
7871 word = byte / UNITS_PER_WORD;
7872 if (WORDS_BIG_ENDIAN)
7873 word = (words - 1) - word;
7874 offset = word * UNITS_PER_WORD;
7875 if (BYTES_BIG_ENDIAN)
7876 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7878 offset += byte % UNITS_PER_WORD;
7881 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7882 ptr[offset] = value;
7888 /* Subroutine of native_encode_expr. Encode the REAL_CST
7889 specified by EXPR into the buffer PTR of length LEN bytes.
7890 Return the number of bytes placed in the buffer, or zero
7894 native_encode_real (const_tree expr, unsigned char *ptr, int len)
7896 tree type = TREE_TYPE (expr);
7897 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7898 int byte, offset, word, words, bitpos;
7899 unsigned char value;
7901 /* There are always 32 bits in each long, no matter the size of
7902 the hosts long. We handle floating point representations with
7906 if (total_bytes > len)
7908 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7910 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7912 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7913 bitpos += BITS_PER_UNIT)
7915 byte = (bitpos / BITS_PER_UNIT) & 3;
7916 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7918 if (UNITS_PER_WORD < 4)
7920 word = byte / UNITS_PER_WORD;
7921 if (WORDS_BIG_ENDIAN)
7922 word = (words - 1) - word;
7923 offset = word * UNITS_PER_WORD;
7924 if (BYTES_BIG_ENDIAN)
7925 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7927 offset += byte % UNITS_PER_WORD;
7930 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7931 ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
7936 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7937 specified by EXPR into the buffer PTR of length LEN bytes.
7938 Return the number of bytes placed in the buffer, or zero
7942 native_encode_complex (const_tree expr, unsigned char *ptr, int len)
7947 part = TREE_REALPART (expr);
7948 rsize = native_encode_expr (part, ptr, len);
7951 part = TREE_IMAGPART (expr);
7952 isize = native_encode_expr (part, ptr+rsize, len-rsize);
7955 return rsize + isize;
7959 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7960 specified by EXPR into the buffer PTR of length LEN bytes.
7961 Return the number of bytes placed in the buffer, or zero
7965 native_encode_vector (const_tree expr, unsigned char *ptr, int len)
7967 int i, size, offset, count;
7968 tree itype, elem, elements;
7971 elements = TREE_VECTOR_CST_ELTS (expr);
7972 count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
7973 itype = TREE_TYPE (TREE_TYPE (expr));
7974 size = GET_MODE_SIZE (TYPE_MODE (itype));
7975 for (i = 0; i < count; i++)
7979 elem = TREE_VALUE (elements);
7980 elements = TREE_CHAIN (elements);
7987 if (native_encode_expr (elem, ptr+offset, len-offset) != size)
7992 if (offset + size > len)
7994 memset (ptr+offset, 0, size);
8002 /* Subroutine of native_encode_expr. Encode the STRING_CST
8003 specified by EXPR into the buffer PTR of length LEN bytes.
8004 Return the number of bytes placed in the buffer, or zero
8008 native_encode_string (const_tree expr, unsigned char *ptr, int len)
8010 tree type = TREE_TYPE (expr);
8011 HOST_WIDE_INT total_bytes;
8013 if (TREE_CODE (type) != ARRAY_TYPE
8014 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
8015 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
8016 || !host_integerp (TYPE_SIZE_UNIT (type), 0))
8018 total_bytes = tree_low_cst (TYPE_SIZE_UNIT (type), 0);
8019 if (total_bytes > len)
8021 if (TREE_STRING_LENGTH (expr) < total_bytes)
8023 memcpy (ptr, TREE_STRING_POINTER (expr), TREE_STRING_LENGTH (expr));
8024 memset (ptr + TREE_STRING_LENGTH (expr), 0,
8025 total_bytes - TREE_STRING_LENGTH (expr));
8028 memcpy (ptr, TREE_STRING_POINTER (expr), total_bytes);
8033 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
8034 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
8035 buffer PTR of length LEN bytes. Return the number of bytes
8036 placed in the buffer, or zero upon failure. */
8039 native_encode_expr (const_tree expr, unsigned char *ptr, int len)
8041 switch (TREE_CODE (expr))
8044 return native_encode_int (expr, ptr, len);
8047 return native_encode_real (expr, ptr, len);
8050 return native_encode_complex (expr, ptr, len);
8053 return native_encode_vector (expr, ptr, len);
8056 return native_encode_string (expr, ptr, len);
8064 /* Subroutine of native_interpret_expr. Interpret the contents of
8065 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
8066 If the buffer cannot be interpreted, return NULL_TREE. */
8069 native_interpret_int (tree type, const unsigned char *ptr, int len)
8071 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
8072 int byte, offset, word, words;
8073 unsigned char value;
8074 unsigned int HOST_WIDE_INT lo = 0;
8075 HOST_WIDE_INT hi = 0;
8077 if (total_bytes > len)
8079 if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
8081 words = total_bytes / UNITS_PER_WORD;
8083 for (byte = 0; byte < total_bytes; byte++)
8085 int bitpos = byte * BITS_PER_UNIT;
8086 if (total_bytes > UNITS_PER_WORD)
8088 word = byte / UNITS_PER_WORD;
8089 if (WORDS_BIG_ENDIAN)
8090 word = (words - 1) - word;
8091 offset = word * UNITS_PER_WORD;
8092 if (BYTES_BIG_ENDIAN)
8093 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
8095 offset += byte % UNITS_PER_WORD;
8098 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
8099 value = ptr[offset];
8101 if (bitpos < HOST_BITS_PER_WIDE_INT)
8102 lo |= (unsigned HOST_WIDE_INT) value << bitpos;
8104 hi |= (unsigned HOST_WIDE_INT) value
8105 << (bitpos - HOST_BITS_PER_WIDE_INT);
8108 return build_int_cst_wide_type (type, lo, hi);
8112 /* Subroutine of native_interpret_expr. Interpret the contents of
8113 the buffer PTR of length LEN as a REAL_CST of type TYPE.
8114 If the buffer cannot be interpreted, return NULL_TREE. */
8117 native_interpret_real (tree type, const unsigned char *ptr, int len)
8119 enum machine_mode mode = TYPE_MODE (type);
8120 int total_bytes = GET_MODE_SIZE (mode);
8121 int byte, offset, word, words, bitpos;
8122 unsigned char value;
8123 /* There are always 32 bits in each long, no matter the size of
8124 the hosts long. We handle floating point representations with
8129 total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
8130 if (total_bytes > len || total_bytes > 24)
8132 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
8134 memset (tmp, 0, sizeof (tmp));
8135 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
8136 bitpos += BITS_PER_UNIT)
8138 byte = (bitpos / BITS_PER_UNIT) & 3;
8139 if (UNITS_PER_WORD < 4)
8141 word = byte / UNITS_PER_WORD;
8142 if (WORDS_BIG_ENDIAN)
8143 word = (words - 1) - word;
8144 offset = word * UNITS_PER_WORD;
8145 if (BYTES_BIG_ENDIAN)
8146 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
8148 offset += byte % UNITS_PER_WORD;
8151 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
8152 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
8154 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
8157 real_from_target (&r, tmp, mode);
8158 return build_real (type, r);
8162 /* Subroutine of native_interpret_expr. Interpret the contents of
8163 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
8164 If the buffer cannot be interpreted, return NULL_TREE. */
8167 native_interpret_complex (tree type, const unsigned char *ptr, int len)
8169 tree etype, rpart, ipart;
8172 etype = TREE_TYPE (type);
8173 size = GET_MODE_SIZE (TYPE_MODE (etype));
8176 rpart = native_interpret_expr (etype, ptr, size);
8179 ipart = native_interpret_expr (etype, ptr+size, size);
8182 return build_complex (type, rpart, ipart);
8186 /* Subroutine of native_interpret_expr. Interpret the contents of
8187 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
8188 If the buffer cannot be interpreted, return NULL_TREE. */
8191 native_interpret_vector (tree type, const unsigned char *ptr, int len)
8193 tree etype, elem, elements;
8196 etype = TREE_TYPE (type);
8197 size = GET_MODE_SIZE (TYPE_MODE (etype));
8198 count = TYPE_VECTOR_SUBPARTS (type);
8199 if (size * count > len)
8202 elements = NULL_TREE;
8203 for (i = count - 1; i >= 0; i--)
8205 elem = native_interpret_expr (etype, ptr+(i*size), size);
8208 elements = tree_cons (NULL_TREE, elem, elements);
8210 return build_vector (type, elements);
8214 /* Subroutine of fold_view_convert_expr. Interpret the contents of
8215 the buffer PTR of length LEN as a constant of type TYPE. For
8216 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
8217 we return a REAL_CST, etc... If the buffer cannot be interpreted,
8218 return NULL_TREE. */
8221 native_interpret_expr (tree type, const unsigned char *ptr, int len)
8223 switch (TREE_CODE (type))
8228 return native_interpret_int (type, ptr, len);
8231 return native_interpret_real (type, ptr, len);
8234 return native_interpret_complex (type, ptr, len);
8237 return native_interpret_vector (type, ptr, len);
8245 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
8246 TYPE at compile-time. If we're unable to perform the conversion
8247 return NULL_TREE. */
8250 fold_view_convert_expr (tree type, tree expr)
8252 /* We support up to 512-bit values (for V8DFmode). */
8253 unsigned char buffer[64];
8256 /* Check that the host and target are sane. */
8257 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
8260 len = native_encode_expr (expr, buffer, sizeof (buffer));
8264 return native_interpret_expr (type, buffer, len);
8267 /* Build an expression for the address of T. Folds away INDIRECT_REF
8268 to avoid confusing the gimplify process. */
8271 build_fold_addr_expr_with_type_loc (location_t loc, tree t, tree ptrtype)
8273 /* The size of the object is not relevant when talking about its address. */
8274 if (TREE_CODE (t) == WITH_SIZE_EXPR)
8275 t = TREE_OPERAND (t, 0);
8277 /* Note: doesn't apply to ALIGN_INDIRECT_REF */
8278 if (TREE_CODE (t) == INDIRECT_REF
8279 || TREE_CODE (t) == MISALIGNED_INDIRECT_REF)
8281 t = TREE_OPERAND (t, 0);
8283 if (TREE_TYPE (t) != ptrtype)
8285 t = build1 (NOP_EXPR, ptrtype, t);
8286 SET_EXPR_LOCATION (t, loc);
8289 else if (TREE_CODE (t) == VIEW_CONVERT_EXPR)
8291 t = build_fold_addr_expr_loc (loc, TREE_OPERAND (t, 0));
8293 if (TREE_TYPE (t) != ptrtype)
8294 t = fold_convert_loc (loc, ptrtype, t);
8298 t = build1 (ADDR_EXPR, ptrtype, t);
8299 SET_EXPR_LOCATION (t, loc);
8305 /* Build an expression for the address of T. */
8308 build_fold_addr_expr_loc (location_t loc, tree t)
8310 tree ptrtype = build_pointer_type (TREE_TYPE (t));
8312 return build_fold_addr_expr_with_type_loc (loc, t, ptrtype);
8315 /* Fold a unary expression of code CODE and type TYPE with operand
8316 OP0. Return the folded expression if folding is successful.
8317 Otherwise, return NULL_TREE. */
8320 fold_unary_loc (location_t loc, enum tree_code code, tree type, tree op0)
8324 enum tree_code_class kind = TREE_CODE_CLASS (code);
8326 gcc_assert (IS_EXPR_CODE_CLASS (kind)
8327 && TREE_CODE_LENGTH (code) == 1);
8332 if (CONVERT_EXPR_CODE_P (code)
8333 || code == FLOAT_EXPR || code == ABS_EXPR)
8335 /* Don't use STRIP_NOPS, because signedness of argument type
8337 STRIP_SIGN_NOPS (arg0);
8341 /* Strip any conversions that don't change the mode. This
8342 is safe for every expression, except for a comparison
8343 expression because its signedness is derived from its
8346 Note that this is done as an internal manipulation within
8347 the constant folder, in order to find the simplest
8348 representation of the arguments so that their form can be
8349 studied. In any cases, the appropriate type conversions
8350 should be put back in the tree that will get out of the
8356 if (TREE_CODE_CLASS (code) == tcc_unary)
8358 if (TREE_CODE (arg0) == COMPOUND_EXPR)
8359 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
8360 fold_build1_loc (loc, code, type,
8361 fold_convert_loc (loc, TREE_TYPE (op0),
8362 TREE_OPERAND (arg0, 1))));
8363 else if (TREE_CODE (arg0) == COND_EXPR)
8365 tree arg01 = TREE_OPERAND (arg0, 1);
8366 tree arg02 = TREE_OPERAND (arg0, 2);
8367 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
8368 arg01 = fold_build1_loc (loc, code, type,
8369 fold_convert_loc (loc,
8370 TREE_TYPE (op0), arg01));
8371 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
8372 arg02 = fold_build1_loc (loc, code, type,
8373 fold_convert_loc (loc,
8374 TREE_TYPE (op0), arg02));
8375 tem = fold_build3_loc (loc, COND_EXPR, type, TREE_OPERAND (arg0, 0),
8378 /* If this was a conversion, and all we did was to move into
8379 inside the COND_EXPR, bring it back out. But leave it if
8380 it is a conversion from integer to integer and the
8381 result precision is no wider than a word since such a
8382 conversion is cheap and may be optimized away by combine,
8383 while it couldn't if it were outside the COND_EXPR. Then return
8384 so we don't get into an infinite recursion loop taking the
8385 conversion out and then back in. */
8387 if ((CONVERT_EXPR_CODE_P (code)
8388 || code == NON_LVALUE_EXPR)
8389 && TREE_CODE (tem) == COND_EXPR
8390 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
8391 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
8392 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
8393 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
8394 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
8395 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
8396 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8398 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
8399 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
8400 || flag_syntax_only))
8402 tem = build1 (code, type,
8404 TREE_TYPE (TREE_OPERAND
8405 (TREE_OPERAND (tem, 1), 0)),
8406 TREE_OPERAND (tem, 0),
8407 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
8408 TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
8409 SET_EXPR_LOCATION (tem, loc);
8413 else if (COMPARISON_CLASS_P (arg0))
8415 if (TREE_CODE (type) == BOOLEAN_TYPE)
8417 arg0 = copy_node (arg0);
8418 TREE_TYPE (arg0) = type;
8421 else if (TREE_CODE (type) != INTEGER_TYPE)
8422 return fold_build3_loc (loc, COND_EXPR, type, arg0,
8423 fold_build1_loc (loc, code, type,
8425 fold_build1_loc (loc, code, type,
8426 integer_zero_node));
8433 /* Re-association barriers around constants and other re-association
8434 barriers can be removed. */
8435 if (CONSTANT_CLASS_P (op0)
8436 || TREE_CODE (op0) == PAREN_EXPR)
8437 return fold_convert_loc (loc, type, op0);
8442 case FIX_TRUNC_EXPR:
8443 if (TREE_TYPE (op0) == type)
8446 /* If we have (type) (a CMP b) and type is an integral type, return
8447 new expression involving the new type. */
8448 if (COMPARISON_CLASS_P (op0) && INTEGRAL_TYPE_P (type))
8449 return fold_build2_loc (loc, TREE_CODE (op0), type, TREE_OPERAND (op0, 0),
8450 TREE_OPERAND (op0, 1));
8452 /* Handle cases of two conversions in a row. */
8453 if (CONVERT_EXPR_P (op0))
8455 tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
8456 tree inter_type = TREE_TYPE (op0);
8457 int inside_int = INTEGRAL_TYPE_P (inside_type);
8458 int inside_ptr = POINTER_TYPE_P (inside_type);
8459 int inside_float = FLOAT_TYPE_P (inside_type);
8460 int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
8461 unsigned int inside_prec = TYPE_PRECISION (inside_type);
8462 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
8463 int inter_int = INTEGRAL_TYPE_P (inter_type);
8464 int inter_ptr = POINTER_TYPE_P (inter_type);
8465 int inter_float = FLOAT_TYPE_P (inter_type);
8466 int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
8467 unsigned int inter_prec = TYPE_PRECISION (inter_type);
8468 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
8469 int final_int = INTEGRAL_TYPE_P (type);
8470 int final_ptr = POINTER_TYPE_P (type);
8471 int final_float = FLOAT_TYPE_P (type);
8472 int final_vec = TREE_CODE (type) == VECTOR_TYPE;
8473 unsigned int final_prec = TYPE_PRECISION (type);
8474 int final_unsignedp = TYPE_UNSIGNED (type);
8476 /* In addition to the cases of two conversions in a row
8477 handled below, if we are converting something to its own
8478 type via an object of identical or wider precision, neither
8479 conversion is needed. */
8480 if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
8481 && (((inter_int || inter_ptr) && final_int)
8482 || (inter_float && final_float))
8483 && inter_prec >= final_prec)
8484 return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
8486 /* Likewise, if the intermediate and initial types are either both
8487 float or both integer, we don't need the middle conversion if the
8488 former is wider than the latter and doesn't change the signedness
8489 (for integers). Avoid this if the final type is a pointer since
8490 then we sometimes need the middle conversion. Likewise if the
8491 final type has a precision not equal to the size of its mode. */
8492 if (((inter_int && inside_int)
8493 || (inter_float && inside_float)
8494 || (inter_vec && inside_vec))
8495 && inter_prec >= inside_prec
8496 && (inter_float || inter_vec
8497 || inter_unsignedp == inside_unsignedp)
8498 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
8499 && TYPE_MODE (type) == TYPE_MODE (inter_type))
8501 && (! final_vec || inter_prec == inside_prec))
8502 return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
8504 /* If we have a sign-extension of a zero-extended value, we can
8505 replace that by a single zero-extension. */
8506 if (inside_int && inter_int && final_int
8507 && inside_prec < inter_prec && inter_prec < final_prec
8508 && inside_unsignedp && !inter_unsignedp)
8509 return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
8511 /* Two conversions in a row are not needed unless:
8512 - some conversion is floating-point (overstrict for now), or
8513 - some conversion is a vector (overstrict for now), or
8514 - the intermediate type is narrower than both initial and
8516 - the intermediate type and innermost type differ in signedness,
8517 and the outermost type is wider than the intermediate, or
8518 - the initial type is a pointer type and the precisions of the
8519 intermediate and final types differ, or
8520 - the final type is a pointer type and the precisions of the
8521 initial and intermediate types differ. */
8522 if (! inside_float && ! inter_float && ! final_float
8523 && ! inside_vec && ! inter_vec && ! final_vec
8524 && (inter_prec >= inside_prec || inter_prec >= final_prec)
8525 && ! (inside_int && inter_int
8526 && inter_unsignedp != inside_unsignedp
8527 && inter_prec < final_prec)
8528 && ((inter_unsignedp && inter_prec > inside_prec)
8529 == (final_unsignedp && final_prec > inter_prec))
8530 && ! (inside_ptr && inter_prec != final_prec)
8531 && ! (final_ptr && inside_prec != inter_prec)
8532 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
8533 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
8534 return fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 0));
8537 /* Handle (T *)&A.B.C for A being of type T and B and C
8538 living at offset zero. This occurs frequently in
8539 C++ upcasting and then accessing the base. */
8540 if (TREE_CODE (op0) == ADDR_EXPR
8541 && POINTER_TYPE_P (type)
8542 && handled_component_p (TREE_OPERAND (op0, 0)))
8544 HOST_WIDE_INT bitsize, bitpos;
8546 enum machine_mode mode;
8547 int unsignedp, volatilep;
8548 tree base = TREE_OPERAND (op0, 0);
8549 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
8550 &mode, &unsignedp, &volatilep, false);
8551 /* If the reference was to a (constant) zero offset, we can use
8552 the address of the base if it has the same base type
8553 as the result type. */
8554 if (! offset && bitpos == 0
8555 && TYPE_MAIN_VARIANT (TREE_TYPE (type))
8556 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
8557 return fold_convert_loc (loc, type,
8558 build_fold_addr_expr_loc (loc, base));
8561 if (TREE_CODE (op0) == MODIFY_EXPR
8562 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
8563 /* Detect assigning a bitfield. */
8564 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
8566 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
8568 /* Don't leave an assignment inside a conversion
8569 unless assigning a bitfield. */
8570 tem = fold_build1_loc (loc, code, type, TREE_OPERAND (op0, 1));
8571 /* First do the assignment, then return converted constant. */
8572 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
8573 TREE_NO_WARNING (tem) = 1;
8574 TREE_USED (tem) = 1;
8575 SET_EXPR_LOCATION (tem, loc);
8579 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
8580 constants (if x has signed type, the sign bit cannot be set
8581 in c). This folds extension into the BIT_AND_EXPR.
8582 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
8583 very likely don't have maximal range for their precision and this
8584 transformation effectively doesn't preserve non-maximal ranges. */
8585 if (TREE_CODE (type) == INTEGER_TYPE
8586 && TREE_CODE (op0) == BIT_AND_EXPR
8587 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
8589 tree and_expr = op0;
8590 tree and0 = TREE_OPERAND (and_expr, 0);
8591 tree and1 = TREE_OPERAND (and_expr, 1);
8594 if (TYPE_UNSIGNED (TREE_TYPE (and_expr))
8595 || (TYPE_PRECISION (type)
8596 <= TYPE_PRECISION (TREE_TYPE (and_expr))))
8598 else if (TYPE_PRECISION (TREE_TYPE (and1))
8599 <= HOST_BITS_PER_WIDE_INT
8600 && host_integerp (and1, 1))
8602 unsigned HOST_WIDE_INT cst;
8604 cst = tree_low_cst (and1, 1);
8605 cst &= (HOST_WIDE_INT) -1
8606 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
8607 change = (cst == 0);
8608 #ifdef LOAD_EXTEND_OP
8610 && !flag_syntax_only
8611 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
8614 tree uns = unsigned_type_for (TREE_TYPE (and0));
8615 and0 = fold_convert_loc (loc, uns, and0);
8616 and1 = fold_convert_loc (loc, uns, and1);
8622 tem = force_fit_type_double (type, TREE_INT_CST_LOW (and1),
8623 TREE_INT_CST_HIGH (and1), 0,
8624 TREE_OVERFLOW (and1));
8625 return fold_build2_loc (loc, BIT_AND_EXPR, type,
8626 fold_convert_loc (loc, type, and0), tem);
8630 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
8631 when one of the new casts will fold away. Conservatively we assume
8632 that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
8633 if (POINTER_TYPE_P (type)
8634 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
8635 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8636 || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
8637 || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
8639 tree arg00 = TREE_OPERAND (arg0, 0);
8640 tree arg01 = TREE_OPERAND (arg0, 1);
8642 return fold_build2_loc (loc,
8643 TREE_CODE (arg0), type,
8644 fold_convert_loc (loc, type, arg00),
8645 fold_convert_loc (loc, sizetype, arg01));
8648 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
8649 of the same precision, and X is an integer type not narrower than
8650 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
8651 if (INTEGRAL_TYPE_P (type)
8652 && TREE_CODE (op0) == BIT_NOT_EXPR
8653 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8654 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
8655 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8657 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
8658 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8659 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
8660 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
8661 fold_convert_loc (loc, type, tem));
8664 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
8665 type of X and Y (integer types only). */
8666 if (INTEGRAL_TYPE_P (type)
8667 && TREE_CODE (op0) == MULT_EXPR
8668 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8669 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
8671 /* Be careful not to introduce new overflows. */
8673 if (TYPE_OVERFLOW_WRAPS (type))
8676 mult_type = unsigned_type_for (type);
8678 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
8680 tem = fold_build2_loc (loc, MULT_EXPR, mult_type,
8681 fold_convert_loc (loc, mult_type,
8682 TREE_OPERAND (op0, 0)),
8683 fold_convert_loc (loc, mult_type,
8684 TREE_OPERAND (op0, 1)));
8685 return fold_convert_loc (loc, type, tem);
8689 tem = fold_convert_const (code, type, op0);
8690 return tem ? tem : NULL_TREE;
8692 case ADDR_SPACE_CONVERT_EXPR:
8693 if (integer_zerop (arg0))
8694 return fold_convert_const (code, type, arg0);
8697 case FIXED_CONVERT_EXPR:
8698 tem = fold_convert_const (code, type, arg0);
8699 return tem ? tem : NULL_TREE;
8701 case VIEW_CONVERT_EXPR:
8702 if (TREE_TYPE (op0) == type)
8704 if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
8705 return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
8706 type, TREE_OPERAND (op0, 0));
8708 /* For integral conversions with the same precision or pointer
8709 conversions use a NOP_EXPR instead. */
8710 if ((INTEGRAL_TYPE_P (type)
8711 || POINTER_TYPE_P (type))
8712 && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8713 || POINTER_TYPE_P (TREE_TYPE (op0)))
8714 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8715 return fold_convert_loc (loc, type, op0);
8717 /* Strip inner integral conversions that do not change the precision. */
8718 if (CONVERT_EXPR_P (op0)
8719 && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8720 || POINTER_TYPE_P (TREE_TYPE (op0)))
8721 && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))
8722 || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
8723 && (TYPE_PRECISION (TREE_TYPE (op0))
8724 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
8725 return fold_build1_loc (loc, VIEW_CONVERT_EXPR,
8726 type, TREE_OPERAND (op0, 0));
8728 return fold_view_convert_expr (type, op0);
8731 tem = fold_negate_expr (loc, arg0);
8733 return fold_convert_loc (loc, type, tem);
8737 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
8738 return fold_abs_const (arg0, type);
8739 else if (TREE_CODE (arg0) == NEGATE_EXPR)
8740 return fold_build1_loc (loc, ABS_EXPR, type, TREE_OPERAND (arg0, 0));
8741 /* Convert fabs((double)float) into (double)fabsf(float). */
8742 else if (TREE_CODE (arg0) == NOP_EXPR
8743 && TREE_CODE (type) == REAL_TYPE)
8745 tree targ0 = strip_float_extensions (arg0);
8747 return fold_convert_loc (loc, type,
8748 fold_build1_loc (loc, ABS_EXPR,
8752 /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on. */
8753 else if (TREE_CODE (arg0) == ABS_EXPR)
8755 else if (tree_expr_nonnegative_p (arg0))
8758 /* Strip sign ops from argument. */
8759 if (TREE_CODE (type) == REAL_TYPE)
8761 tem = fold_strip_sign_ops (arg0);
8763 return fold_build1_loc (loc, ABS_EXPR, type,
8764 fold_convert_loc (loc, type, tem));
8769 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8770 return fold_convert_loc (loc, type, arg0);
8771 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8773 tree itype = TREE_TYPE (type);
8774 tree rpart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 0));
8775 tree ipart = fold_convert_loc (loc, itype, TREE_OPERAND (arg0, 1));
8776 return fold_build2_loc (loc, COMPLEX_EXPR, type, rpart,
8777 negate_expr (ipart));
8779 if (TREE_CODE (arg0) == COMPLEX_CST)
8781 tree itype = TREE_TYPE (type);
8782 tree rpart = fold_convert_loc (loc, itype, TREE_REALPART (arg0));
8783 tree ipart = fold_convert_loc (loc, itype, TREE_IMAGPART (arg0));
8784 return build_complex (type, rpart, negate_expr (ipart));
8786 if (TREE_CODE (arg0) == CONJ_EXPR)
8787 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8791 if (TREE_CODE (arg0) == INTEGER_CST)
8792 return fold_not_const (arg0, type);
8793 else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8794 return fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
8795 /* Convert ~ (-A) to A - 1. */
8796 else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8797 return fold_build2_loc (loc, MINUS_EXPR, type,
8798 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0)),
8799 build_int_cst (type, 1));
8800 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
8801 else if (INTEGRAL_TYPE_P (type)
8802 && ((TREE_CODE (arg0) == MINUS_EXPR
8803 && integer_onep (TREE_OPERAND (arg0, 1)))
8804 || (TREE_CODE (arg0) == PLUS_EXPR
8805 && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8806 return fold_build1_loc (loc, NEGATE_EXPR, type,
8807 fold_convert_loc (loc, type,
8808 TREE_OPERAND (arg0, 0)));
8809 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
8810 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8811 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8812 fold_convert_loc (loc, type,
8813 TREE_OPERAND (arg0, 0)))))
8814 return fold_build2_loc (loc, BIT_XOR_EXPR, type, tem,
8815 fold_convert_loc (loc, type,
8816 TREE_OPERAND (arg0, 1)));
8817 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8818 && (tem = fold_unary_loc (loc, BIT_NOT_EXPR, type,
8819 fold_convert_loc (loc, type,
8820 TREE_OPERAND (arg0, 1)))))
8821 return fold_build2_loc (loc, BIT_XOR_EXPR, type,
8822 fold_convert_loc (loc, type,
8823 TREE_OPERAND (arg0, 0)), tem);
8824 /* Perform BIT_NOT_EXPR on each element individually. */
8825 else if (TREE_CODE (arg0) == VECTOR_CST)
8827 tree elements = TREE_VECTOR_CST_ELTS (arg0), elem, list = NULL_TREE;
8828 int count = TYPE_VECTOR_SUBPARTS (type), i;
8830 for (i = 0; i < count; i++)
8834 elem = TREE_VALUE (elements);
8835 elem = fold_unary_loc (loc, BIT_NOT_EXPR, TREE_TYPE (type), elem);
8836 if (elem == NULL_TREE)
8838 elements = TREE_CHAIN (elements);
8841 elem = build_int_cst (TREE_TYPE (type), -1);
8842 list = tree_cons (NULL_TREE, elem, list);
8845 return build_vector (type, nreverse (list));
8850 case TRUTH_NOT_EXPR:
8851 /* The argument to invert_truthvalue must have Boolean type. */
8852 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8853 arg0 = fold_convert_loc (loc, boolean_type_node, arg0);
8855 /* Note that the operand of this must be an int
8856 and its values must be 0 or 1.
8857 ("true" is a fixed value perhaps depending on the language,
8858 but we don't handle values other than 1 correctly yet.) */
8859 tem = fold_truth_not_expr (loc, arg0);
8862 return fold_convert_loc (loc, type, tem);
8865 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8866 return fold_convert_loc (loc, type, arg0);
8867 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8868 return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 0),
8869 TREE_OPERAND (arg0, 1));
8870 if (TREE_CODE (arg0) == COMPLEX_CST)
8871 return fold_convert_loc (loc, type, TREE_REALPART (arg0));
8872 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8874 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8875 tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8876 fold_build1_loc (loc, REALPART_EXPR, itype,
8877 TREE_OPERAND (arg0, 0)),
8878 fold_build1_loc (loc, REALPART_EXPR, itype,
8879 TREE_OPERAND (arg0, 1)));
8880 return fold_convert_loc (loc, type, tem);
8882 if (TREE_CODE (arg0) == CONJ_EXPR)
8884 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8885 tem = fold_build1_loc (loc, REALPART_EXPR, itype,
8886 TREE_OPERAND (arg0, 0));
8887 return fold_convert_loc (loc, type, tem);
8889 if (TREE_CODE (arg0) == CALL_EXPR)
8891 tree fn = get_callee_fndecl (arg0);
8892 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8893 switch (DECL_FUNCTION_CODE (fn))
8895 CASE_FLT_FN (BUILT_IN_CEXPI):
8896 fn = mathfn_built_in (type, BUILT_IN_COS);
8898 return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8908 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8909 return fold_convert_loc (loc, type, integer_zero_node);
8910 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8911 return omit_one_operand_loc (loc, type, TREE_OPERAND (arg0, 1),
8912 TREE_OPERAND (arg0, 0));
8913 if (TREE_CODE (arg0) == COMPLEX_CST)
8914 return fold_convert_loc (loc, type, TREE_IMAGPART (arg0));
8915 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8917 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8918 tem = fold_build2_loc (loc, TREE_CODE (arg0), itype,
8919 fold_build1_loc (loc, IMAGPART_EXPR, itype,
8920 TREE_OPERAND (arg0, 0)),
8921 fold_build1_loc (loc, IMAGPART_EXPR, itype,
8922 TREE_OPERAND (arg0, 1)));
8923 return fold_convert_loc (loc, type, tem);
8925 if (TREE_CODE (arg0) == CONJ_EXPR)
8927 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8928 tem = fold_build1_loc (loc, IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8929 return fold_convert_loc (loc, type, negate_expr (tem));
8931 if (TREE_CODE (arg0) == CALL_EXPR)
8933 tree fn = get_callee_fndecl (arg0);
8934 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8935 switch (DECL_FUNCTION_CODE (fn))
8937 CASE_FLT_FN (BUILT_IN_CEXPI):
8938 fn = mathfn_built_in (type, BUILT_IN_SIN);
8940 return build_call_expr_loc (loc, fn, 1, CALL_EXPR_ARG (arg0, 0));
8950 /* Fold *&X to X if X is an lvalue. */
8951 if (TREE_CODE (op0) == ADDR_EXPR)
8953 tree op00 = TREE_OPERAND (op0, 0);
8954 if ((TREE_CODE (op00) == VAR_DECL
8955 || TREE_CODE (op00) == PARM_DECL
8956 || TREE_CODE (op00) == RESULT_DECL)
8957 && !TREE_READONLY (op00))
8964 } /* switch (code) */
8968 /* If the operation was a conversion do _not_ mark a resulting constant
8969 with TREE_OVERFLOW if the original constant was not. These conversions
8970 have implementation defined behavior and retaining the TREE_OVERFLOW
8971 flag here would confuse later passes such as VRP. */
8973 fold_unary_ignore_overflow_loc (location_t loc, enum tree_code code,
8974 tree type, tree op0)
8976 tree res = fold_unary_loc (loc, code, type, op0);
8978 && TREE_CODE (res) == INTEGER_CST
8979 && TREE_CODE (op0) == INTEGER_CST
8980 && CONVERT_EXPR_CODE_P (code))
8981 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8986 /* Fold a binary expression of code CODE and type TYPE with operands
8987 OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8988 Return the folded expression if folding is successful. Otherwise,
8989 return NULL_TREE. */
8992 fold_minmax (location_t loc, enum tree_code code, tree type, tree op0, tree op1)
8994 enum tree_code compl_code;
8996 if (code == MIN_EXPR)
8997 compl_code = MAX_EXPR;
8998 else if (code == MAX_EXPR)
8999 compl_code = MIN_EXPR;
9003 /* MIN (MAX (a, b), b) == b. */
9004 if (TREE_CODE (op0) == compl_code
9005 && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
9006 return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 0));
9008 /* MIN (MAX (b, a), b) == b. */
9009 if (TREE_CODE (op0) == compl_code
9010 && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
9011 && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
9012 return omit_one_operand_loc (loc, type, op1, TREE_OPERAND (op0, 1));
9014 /* MIN (a, MAX (a, b)) == a. */
9015 if (TREE_CODE (op1) == compl_code
9016 && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
9017 && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
9018 return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 1));
9020 /* MIN (a, MAX (b, a)) == a. */
9021 if (TREE_CODE (op1) == compl_code
9022 && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
9023 && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
9024 return omit_one_operand_loc (loc, type, op0, TREE_OPERAND (op1, 0));
9029 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
9030 by changing CODE to reduce the magnitude of constants involved in
9031 ARG0 of the comparison.
9032 Returns a canonicalized comparison tree if a simplification was
9033 possible, otherwise returns NULL_TREE.
9034 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
9035 valid if signed overflow is undefined. */
9038 maybe_canonicalize_comparison_1 (location_t loc, enum tree_code code, tree type,
9039 tree arg0, tree arg1,
9040 bool *strict_overflow_p)
9042 enum tree_code code0 = TREE_CODE (arg0);
9043 tree t, cst0 = NULL_TREE;
9047 /* Match A +- CST code arg1 and CST code arg1. We can change the
9048 first form only if overflow is undefined. */
9049 if (!((TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9050 /* In principle pointers also have undefined overflow behavior,
9051 but that causes problems elsewhere. */
9052 && !POINTER_TYPE_P (TREE_TYPE (arg0))
9053 && (code0 == MINUS_EXPR
9054 || code0 == PLUS_EXPR)
9055 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9056 || code0 == INTEGER_CST))
9059 /* Identify the constant in arg0 and its sign. */
9060 if (code0 == INTEGER_CST)
9063 cst0 = TREE_OPERAND (arg0, 1);
9064 sgn0 = tree_int_cst_sgn (cst0);
9066 /* Overflowed constants and zero will cause problems. */
9067 if (integer_zerop (cst0)
9068 || TREE_OVERFLOW (cst0))
9071 /* See if we can reduce the magnitude of the constant in
9072 arg0 by changing the comparison code. */
9073 if (code0 == INTEGER_CST)
9075 /* CST <= arg1 -> CST-1 < arg1. */
9076 if (code == LE_EXPR && sgn0 == 1)
9078 /* -CST < arg1 -> -CST-1 <= arg1. */
9079 else if (code == LT_EXPR && sgn0 == -1)
9081 /* CST > arg1 -> CST-1 >= arg1. */
9082 else if (code == GT_EXPR && sgn0 == 1)
9084 /* -CST >= arg1 -> -CST-1 > arg1. */
9085 else if (code == GE_EXPR && sgn0 == -1)
9089 /* arg1 code' CST' might be more canonical. */
9094 /* A - CST < arg1 -> A - CST-1 <= arg1. */
9096 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9098 /* A + CST > arg1 -> A + CST-1 >= arg1. */
9099 else if (code == GT_EXPR
9100 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9102 /* A + CST <= arg1 -> A + CST-1 < arg1. */
9103 else if (code == LE_EXPR
9104 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
9106 /* A - CST >= arg1 -> A - CST-1 > arg1. */
9107 else if (code == GE_EXPR
9108 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
9112 *strict_overflow_p = true;
9115 /* Now build the constant reduced in magnitude. But not if that
9116 would produce one outside of its types range. */
9117 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
9119 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
9120 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
9122 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
9123 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
9124 /* We cannot swap the comparison here as that would cause us to
9125 endlessly recurse. */
9128 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
9129 cst0, build_int_cst (TREE_TYPE (cst0), 1), 0);
9130 if (code0 != INTEGER_CST)
9131 t = fold_build2_loc (loc, code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
9133 /* If swapping might yield to a more canonical form, do so. */
9135 return fold_build2_loc (loc, swap_tree_comparison (code), type, arg1, t);
9137 return fold_build2_loc (loc, code, type, t, arg1);
9140 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
9141 overflow further. Try to decrease the magnitude of constants involved
9142 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
9143 and put sole constants at the second argument position.
9144 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
9147 maybe_canonicalize_comparison (location_t loc, enum tree_code code, tree type,
9148 tree arg0, tree arg1)
9151 bool strict_overflow_p;
9152 const char * const warnmsg = G_("assuming signed overflow does not occur "
9153 "when reducing constant in comparison");
9155 /* Try canonicalization by simplifying arg0. */
9156 strict_overflow_p = false;
9157 t = maybe_canonicalize_comparison_1 (loc, code, type, arg0, arg1,
9158 &strict_overflow_p);
9161 if (strict_overflow_p)
9162 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
9166 /* Try canonicalization by simplifying arg1 using the swapped
9168 code = swap_tree_comparison (code);
9169 strict_overflow_p = false;
9170 t = maybe_canonicalize_comparison_1 (loc, code, type, arg1, arg0,
9171 &strict_overflow_p);
9172 if (t && strict_overflow_p)
9173 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
9177 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
9178 space. This is used to avoid issuing overflow warnings for
9179 expressions like &p->x which can not wrap. */
9182 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
9184 unsigned HOST_WIDE_INT offset_low, total_low;
9185 HOST_WIDE_INT size, offset_high, total_high;
9187 if (!POINTER_TYPE_P (TREE_TYPE (base)))
9193 if (offset == NULL_TREE)
9198 else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
9202 offset_low = TREE_INT_CST_LOW (offset);
9203 offset_high = TREE_INT_CST_HIGH (offset);
9206 if (add_double_with_sign (offset_low, offset_high,
9207 bitpos / BITS_PER_UNIT, 0,
9208 &total_low, &total_high,
9212 if (total_high != 0)
9215 size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
9219 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
9221 if (TREE_CODE (base) == ADDR_EXPR)
9223 HOST_WIDE_INT base_size;
9225 base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
9226 if (base_size > 0 && size < base_size)
9230 return total_low > (unsigned HOST_WIDE_INT) size;
9233 /* Subroutine of fold_binary. This routine performs all of the
9234 transformations that are common to the equality/inequality
9235 operators (EQ_EXPR and NE_EXPR) and the ordering operators
9236 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
9237 fold_binary should call fold_binary. Fold a comparison with
9238 tree code CODE and type TYPE with operands OP0 and OP1. Return
9239 the folded comparison or NULL_TREE. */
9242 fold_comparison (location_t loc, enum tree_code code, tree type,
9245 tree arg0, arg1, tem;
9250 STRIP_SIGN_NOPS (arg0);
9251 STRIP_SIGN_NOPS (arg1);
9253 tem = fold_relational_const (code, type, arg0, arg1);
9254 if (tem != NULL_TREE)
9257 /* If one arg is a real or integer constant, put it last. */
9258 if (tree_swap_operands_p (arg0, arg1, true))
9259 return fold_build2_loc (loc, swap_tree_comparison (code), type, op1, op0);
9261 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1. */
9262 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
9263 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9264 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9265 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
9266 && (TREE_CODE (arg1) == INTEGER_CST
9267 && !TREE_OVERFLOW (arg1)))
9269 tree const1 = TREE_OPERAND (arg0, 1);
9271 tree variable = TREE_OPERAND (arg0, 0);
9274 lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
9276 lhs = fold_build2_loc (loc, lhs_add ? PLUS_EXPR : MINUS_EXPR,
9277 TREE_TYPE (arg1), const2, const1);
9279 /* If the constant operation overflowed this can be
9280 simplified as a comparison against INT_MAX/INT_MIN. */
9281 if (TREE_CODE (lhs) == INTEGER_CST
9282 && TREE_OVERFLOW (lhs))
9284 int const1_sgn = tree_int_cst_sgn (const1);
9285 enum tree_code code2 = code;
9287 /* Get the sign of the constant on the lhs if the
9288 operation were VARIABLE + CONST1. */
9289 if (TREE_CODE (arg0) == MINUS_EXPR)
9290 const1_sgn = -const1_sgn;
9292 /* The sign of the constant determines if we overflowed
9293 INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
9294 Canonicalize to the INT_MIN overflow by swapping the comparison
9296 if (const1_sgn == -1)
9297 code2 = swap_tree_comparison (code);
9299 /* We now can look at the canonicalized case
9300 VARIABLE + 1 CODE2 INT_MIN
9301 and decide on the result. */
9302 if (code2 == LT_EXPR
9304 || code2 == EQ_EXPR)
9305 return omit_one_operand_loc (loc, type, boolean_false_node, variable);
9306 else if (code2 == NE_EXPR
9308 || code2 == GT_EXPR)
9309 return omit_one_operand_loc (loc, type, boolean_true_node, variable);
9312 if (TREE_CODE (lhs) == TREE_CODE (arg1)
9313 && (TREE_CODE (lhs) != INTEGER_CST
9314 || !TREE_OVERFLOW (lhs)))
9316 fold_overflow_warning (("assuming signed overflow does not occur "
9317 "when changing X +- C1 cmp C2 to "
9319 WARN_STRICT_OVERFLOW_COMPARISON);
9320 return fold_build2_loc (loc, code, type, variable, lhs);
9324 /* For comparisons of pointers we can decompose it to a compile time
9325 comparison of the base objects and the offsets into the object.
9326 This requires at least one operand being an ADDR_EXPR or a
9327 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
9328 if (POINTER_TYPE_P (TREE_TYPE (arg0))
9329 && (TREE_CODE (arg0) == ADDR_EXPR
9330 || TREE_CODE (arg1) == ADDR_EXPR
9331 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
9332 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
9334 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
9335 HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
9336 enum machine_mode mode;
9337 int volatilep, unsignedp;
9338 bool indirect_base0 = false, indirect_base1 = false;
9340 /* Get base and offset for the access. Strip ADDR_EXPR for
9341 get_inner_reference, but put it back by stripping INDIRECT_REF
9342 off the base object if possible. indirect_baseN will be true
9343 if baseN is not an address but refers to the object itself. */
9345 if (TREE_CODE (arg0) == ADDR_EXPR)
9347 base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
9348 &bitsize, &bitpos0, &offset0, &mode,
9349 &unsignedp, &volatilep, false);
9350 if (TREE_CODE (base0) == INDIRECT_REF)
9351 base0 = TREE_OPERAND (base0, 0);
9353 indirect_base0 = true;
9355 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9357 base0 = TREE_OPERAND (arg0, 0);
9358 offset0 = TREE_OPERAND (arg0, 1);
9362 if (TREE_CODE (arg1) == ADDR_EXPR)
9364 base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
9365 &bitsize, &bitpos1, &offset1, &mode,
9366 &unsignedp, &volatilep, false);
9367 if (TREE_CODE (base1) == INDIRECT_REF)
9368 base1 = TREE_OPERAND (base1, 0);
9370 indirect_base1 = true;
9372 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
9374 base1 = TREE_OPERAND (arg1, 0);
9375 offset1 = TREE_OPERAND (arg1, 1);
9378 /* If we have equivalent bases we might be able to simplify. */
9379 if (indirect_base0 == indirect_base1
9380 && operand_equal_p (base0, base1, 0))
9382 /* We can fold this expression to a constant if the non-constant
9383 offset parts are equal. */
9384 if ((offset0 == offset1
9385 || (offset0 && offset1
9386 && operand_equal_p (offset0, offset1, 0)))
9389 || POINTER_TYPE_OVERFLOW_UNDEFINED))
9394 && bitpos0 != bitpos1
9395 && (pointer_may_wrap_p (base0, offset0, bitpos0)
9396 || pointer_may_wrap_p (base1, offset1, bitpos1)))
9397 fold_overflow_warning (("assuming pointer wraparound does not "
9398 "occur when comparing P +- C1 with "
9400 WARN_STRICT_OVERFLOW_CONDITIONAL);
9405 return constant_boolean_node (bitpos0 == bitpos1, type);
9407 return constant_boolean_node (bitpos0 != bitpos1, type);
9409 return constant_boolean_node (bitpos0 < bitpos1, type);
9411 return constant_boolean_node (bitpos0 <= bitpos1, type);
9413 return constant_boolean_node (bitpos0 >= bitpos1, type);
9415 return constant_boolean_node (bitpos0 > bitpos1, type);
9419 /* We can simplify the comparison to a comparison of the variable
9420 offset parts if the constant offset parts are equal.
9421 Be careful to use signed size type here because otherwise we
9422 mess with array offsets in the wrong way. This is possible
9423 because pointer arithmetic is restricted to retain within an
9424 object and overflow on pointer differences is undefined as of
9425 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
9426 else if (bitpos0 == bitpos1
9427 && ((code == EQ_EXPR || code == NE_EXPR)
9428 || POINTER_TYPE_OVERFLOW_UNDEFINED))
9430 tree signed_size_type_node;
9431 signed_size_type_node = signed_type_for (size_type_node);
9433 /* By converting to signed size type we cover middle-end pointer
9434 arithmetic which operates on unsigned pointer types of size
9435 type size and ARRAY_REF offsets which are properly sign or
9436 zero extended from their type in case it is narrower than
9438 if (offset0 == NULL_TREE)
9439 offset0 = build_int_cst (signed_size_type_node, 0);
9441 offset0 = fold_convert_loc (loc, signed_size_type_node,
9443 if (offset1 == NULL_TREE)
9444 offset1 = build_int_cst (signed_size_type_node, 0);
9446 offset1 = fold_convert_loc (loc, signed_size_type_node,
9451 && (pointer_may_wrap_p (base0, offset0, bitpos0)
9452 || pointer_may_wrap_p (base1, offset1, bitpos1)))
9453 fold_overflow_warning (("assuming pointer wraparound does not "
9454 "occur when comparing P +- C1 with "
9456 WARN_STRICT_OVERFLOW_COMPARISON);
9458 return fold_build2_loc (loc, code, type, offset0, offset1);
9461 /* For non-equal bases we can simplify if they are addresses
9462 of local binding decls or constants. */
9463 else if (indirect_base0 && indirect_base1
9464 /* We know that !operand_equal_p (base0, base1, 0)
9465 because the if condition was false. But make
9466 sure two decls are not the same. */
9468 && TREE_CODE (arg0) == ADDR_EXPR
9469 && TREE_CODE (arg1) == ADDR_EXPR
9470 && (((TREE_CODE (base0) == VAR_DECL
9471 || TREE_CODE (base0) == PARM_DECL)
9472 && (targetm.binds_local_p (base0)
9473 || CONSTANT_CLASS_P (base1)))
9474 || CONSTANT_CLASS_P (base0))
9475 && (((TREE_CODE (base1) == VAR_DECL
9476 || TREE_CODE (base1) == PARM_DECL)
9477 && (targetm.binds_local_p (base1)
9478 || CONSTANT_CLASS_P (base0)))
9479 || CONSTANT_CLASS_P (base1)))
9481 if (code == EQ_EXPR)
9482 return omit_two_operands_loc (loc, type, boolean_false_node,
9484 else if (code == NE_EXPR)
9485 return omit_two_operands_loc (loc, type, boolean_true_node,
9488 /* For equal offsets we can simplify to a comparison of the
9490 else if (bitpos0 == bitpos1
9492 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
9494 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
9495 && ((offset0 == offset1)
9496 || (offset0 && offset1
9497 && operand_equal_p (offset0, offset1, 0))))
9500 base0 = build_fold_addr_expr_loc (loc, base0);
9502 base1 = build_fold_addr_expr_loc (loc, base1);
9503 return fold_build2_loc (loc, code, type, base0, base1);
9507 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
9508 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
9509 the resulting offset is smaller in absolute value than the
9511 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9512 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
9513 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9514 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9515 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
9516 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9517 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
9519 tree const1 = TREE_OPERAND (arg0, 1);
9520 tree const2 = TREE_OPERAND (arg1, 1);
9521 tree variable1 = TREE_OPERAND (arg0, 0);
9522 tree variable2 = TREE_OPERAND (arg1, 0);
9524 const char * const warnmsg = G_("assuming signed overflow does not "
9525 "occur when combining constants around "
9528 /* Put the constant on the side where it doesn't overflow and is
9529 of lower absolute value than before. */
9530 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9531 ? MINUS_EXPR : PLUS_EXPR,
9533 if (!TREE_OVERFLOW (cst)
9534 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
9536 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9537 return fold_build2_loc (loc, code, type,
9539 fold_build2_loc (loc,
9540 TREE_CODE (arg1), TREE_TYPE (arg1),
9544 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9545 ? MINUS_EXPR : PLUS_EXPR,
9547 if (!TREE_OVERFLOW (cst)
9548 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
9550 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9551 return fold_build2_loc (loc, code, type,
9552 fold_build2_loc (loc, TREE_CODE (arg0), TREE_TYPE (arg0),
9558 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
9559 signed arithmetic case. That form is created by the compiler
9560 often enough for folding it to be of value. One example is in
9561 computing loop trip counts after Operator Strength Reduction. */
9562 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9563 && TREE_CODE (arg0) == MULT_EXPR
9564 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9565 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9566 && integer_zerop (arg1))
9568 tree const1 = TREE_OPERAND (arg0, 1);
9569 tree const2 = arg1; /* zero */
9570 tree variable1 = TREE_OPERAND (arg0, 0);
9571 enum tree_code cmp_code = code;
9573 gcc_assert (!integer_zerop (const1));
9575 fold_overflow_warning (("assuming signed overflow does not occur when "
9576 "eliminating multiplication in comparison "
9578 WARN_STRICT_OVERFLOW_COMPARISON);
9580 /* If const1 is negative we swap the sense of the comparison. */
9581 if (tree_int_cst_sgn (const1) < 0)
9582 cmp_code = swap_tree_comparison (cmp_code);
9584 return fold_build2_loc (loc, cmp_code, type, variable1, const2);
9587 tem = maybe_canonicalize_comparison (loc, code, type, op0, op1);
9591 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
9593 tree targ0 = strip_float_extensions (arg0);
9594 tree targ1 = strip_float_extensions (arg1);
9595 tree newtype = TREE_TYPE (targ0);
9597 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
9598 newtype = TREE_TYPE (targ1);
9600 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
9601 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
9602 return fold_build2_loc (loc, code, type,
9603 fold_convert_loc (loc, newtype, targ0),
9604 fold_convert_loc (loc, newtype, targ1));
9606 /* (-a) CMP (-b) -> b CMP a */
9607 if (TREE_CODE (arg0) == NEGATE_EXPR
9608 && TREE_CODE (arg1) == NEGATE_EXPR)
9609 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg1, 0),
9610 TREE_OPERAND (arg0, 0));
9612 if (TREE_CODE (arg1) == REAL_CST)
9614 REAL_VALUE_TYPE cst;
9615 cst = TREE_REAL_CST (arg1);
9617 /* (-a) CMP CST -> a swap(CMP) (-CST) */
9618 if (TREE_CODE (arg0) == NEGATE_EXPR)
9619 return fold_build2_loc (loc, swap_tree_comparison (code), type,
9620 TREE_OPERAND (arg0, 0),
9621 build_real (TREE_TYPE (arg1),
9622 REAL_VALUE_NEGATE (cst)));
9624 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
9625 /* a CMP (-0) -> a CMP 0 */
9626 if (REAL_VALUE_MINUS_ZERO (cst))
9627 return fold_build2_loc (loc, code, type, arg0,
9628 build_real (TREE_TYPE (arg1), dconst0));
9630 /* x != NaN is always true, other ops are always false. */
9631 if (REAL_VALUE_ISNAN (cst)
9632 && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
9634 tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
9635 return omit_one_operand_loc (loc, type, tem, arg0);
9638 /* Fold comparisons against infinity. */
9639 if (REAL_VALUE_ISINF (cst)
9640 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1))))
9642 tem = fold_inf_compare (loc, code, type, arg0, arg1);
9643 if (tem != NULL_TREE)
9648 /* If this is a comparison of a real constant with a PLUS_EXPR
9649 or a MINUS_EXPR of a real constant, we can convert it into a
9650 comparison with a revised real constant as long as no overflow
9651 occurs when unsafe_math_optimizations are enabled. */
9652 if (flag_unsafe_math_optimizations
9653 && TREE_CODE (arg1) == REAL_CST
9654 && (TREE_CODE (arg0) == PLUS_EXPR
9655 || TREE_CODE (arg0) == MINUS_EXPR)
9656 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
9657 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
9658 ? MINUS_EXPR : PLUS_EXPR,
9659 arg1, TREE_OPERAND (arg0, 1), 0))
9660 && !TREE_OVERFLOW (tem))
9661 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
9663 /* Likewise, we can simplify a comparison of a real constant with
9664 a MINUS_EXPR whose first operand is also a real constant, i.e.
9665 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
9666 floating-point types only if -fassociative-math is set. */
9667 if (flag_associative_math
9668 && TREE_CODE (arg1) == REAL_CST
9669 && TREE_CODE (arg0) == MINUS_EXPR
9670 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
9671 && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
9673 && !TREE_OVERFLOW (tem))
9674 return fold_build2_loc (loc, swap_tree_comparison (code), type,
9675 TREE_OPERAND (arg0, 1), tem);
9677 /* Fold comparisons against built-in math functions. */
9678 if (TREE_CODE (arg1) == REAL_CST
9679 && flag_unsafe_math_optimizations
9680 && ! flag_errno_math)
9682 enum built_in_function fcode = builtin_mathfn_code (arg0);
9684 if (fcode != END_BUILTINS)
9686 tem = fold_mathfn_compare (loc, fcode, code, type, arg0, arg1);
9687 if (tem != NULL_TREE)
9693 if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
9694 && CONVERT_EXPR_P (arg0))
9696 /* If we are widening one operand of an integer comparison,
9697 see if the other operand is similarly being widened. Perhaps we
9698 can do the comparison in the narrower type. */
9699 tem = fold_widened_comparison (loc, code, type, arg0, arg1);
9703 /* Or if we are changing signedness. */
9704 tem = fold_sign_changed_comparison (loc, code, type, arg0, arg1);
9709 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
9710 constant, we can simplify it. */
9711 if (TREE_CODE (arg1) == INTEGER_CST
9712 && (TREE_CODE (arg0) == MIN_EXPR
9713 || TREE_CODE (arg0) == MAX_EXPR)
9714 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9716 tem = optimize_minmax_comparison (loc, code, type, op0, op1);
9721 /* Simplify comparison of something with itself. (For IEEE
9722 floating-point, we can only do some of these simplifications.) */
9723 if (operand_equal_p (arg0, arg1, 0))
9728 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9729 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9730 return constant_boolean_node (1, type);
9735 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9736 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9737 return constant_boolean_node (1, type);
9738 return fold_build2_loc (loc, EQ_EXPR, type, arg0, arg1);
9741 /* For NE, we can only do this simplification if integer
9742 or we don't honor IEEE floating point NaNs. */
9743 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9744 && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9746 /* ... fall through ... */
9749 return constant_boolean_node (0, type);
9755 /* If we are comparing an expression that just has comparisons
9756 of two integer values, arithmetic expressions of those comparisons,
9757 and constants, we can simplify it. There are only three cases
9758 to check: the two values can either be equal, the first can be
9759 greater, or the second can be greater. Fold the expression for
9760 those three values. Since each value must be 0 or 1, we have
9761 eight possibilities, each of which corresponds to the constant 0
9762 or 1 or one of the six possible comparisons.
9764 This handles common cases like (a > b) == 0 but also handles
9765 expressions like ((x > y) - (y > x)) > 0, which supposedly
9766 occur in macroized code. */
9768 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9770 tree cval1 = 0, cval2 = 0;
9773 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9774 /* Don't handle degenerate cases here; they should already
9775 have been handled anyway. */
9776 && cval1 != 0 && cval2 != 0
9777 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9778 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9779 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9780 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9781 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9782 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9783 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9785 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9786 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9788 /* We can't just pass T to eval_subst in case cval1 or cval2
9789 was the same as ARG1. */
9792 = fold_build2_loc (loc, code, type,
9793 eval_subst (loc, arg0, cval1, maxval,
9797 = fold_build2_loc (loc, code, type,
9798 eval_subst (loc, arg0, cval1, maxval,
9802 = fold_build2_loc (loc, code, type,
9803 eval_subst (loc, arg0, cval1, minval,
9807 /* All three of these results should be 0 or 1. Confirm they are.
9808 Then use those values to select the proper code to use. */
9810 if (TREE_CODE (high_result) == INTEGER_CST
9811 && TREE_CODE (equal_result) == INTEGER_CST
9812 && TREE_CODE (low_result) == INTEGER_CST)
9814 /* Make a 3-bit mask with the high-order bit being the
9815 value for `>', the next for '=', and the low for '<'. */
9816 switch ((integer_onep (high_result) * 4)
9817 + (integer_onep (equal_result) * 2)
9818 + integer_onep (low_result))
9822 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
9843 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
9848 tem = save_expr (build2 (code, type, cval1, cval2));
9849 SET_EXPR_LOCATION (tem, loc);
9852 return fold_build2_loc (loc, code, type, cval1, cval2);
9857 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9858 into a single range test. */
9859 if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9860 || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9861 && TREE_CODE (arg1) == INTEGER_CST
9862 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9863 && !integer_zerop (TREE_OPERAND (arg0, 1))
9864 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9865 && !TREE_OVERFLOW (arg1))
9867 tem = fold_div_compare (loc, code, type, arg0, arg1);
9868 if (tem != NULL_TREE)
9872 /* Fold ~X op ~Y as Y op X. */
9873 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9874 && TREE_CODE (arg1) == BIT_NOT_EXPR)
9876 tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9877 return fold_build2_loc (loc, code, type,
9878 fold_convert_loc (loc, cmp_type,
9879 TREE_OPERAND (arg1, 0)),
9880 TREE_OPERAND (arg0, 0));
9883 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
9884 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9885 && TREE_CODE (arg1) == INTEGER_CST)
9887 tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9888 return fold_build2_loc (loc, swap_tree_comparison (code), type,
9889 TREE_OPERAND (arg0, 0),
9890 fold_build1_loc (loc, BIT_NOT_EXPR, cmp_type,
9891 fold_convert_loc (loc, cmp_type, arg1)));
9898 /* Subroutine of fold_binary. Optimize complex multiplications of the
9899 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
9900 argument EXPR represents the expression "z" of type TYPE. */
9903 fold_mult_zconjz (location_t loc, tree type, tree expr)
9905 tree itype = TREE_TYPE (type);
9906 tree rpart, ipart, tem;
9908 if (TREE_CODE (expr) == COMPLEX_EXPR)
9910 rpart = TREE_OPERAND (expr, 0);
9911 ipart = TREE_OPERAND (expr, 1);
9913 else if (TREE_CODE (expr) == COMPLEX_CST)
9915 rpart = TREE_REALPART (expr);
9916 ipart = TREE_IMAGPART (expr);
9920 expr = save_expr (expr);
9921 rpart = fold_build1_loc (loc, REALPART_EXPR, itype, expr);
9922 ipart = fold_build1_loc (loc, IMAGPART_EXPR, itype, expr);
9925 rpart = save_expr (rpart);
9926 ipart = save_expr (ipart);
9927 tem = fold_build2_loc (loc, PLUS_EXPR, itype,
9928 fold_build2_loc (loc, MULT_EXPR, itype, rpart, rpart),
9929 fold_build2_loc (loc, MULT_EXPR, itype, ipart, ipart));
9930 return fold_build2_loc (loc, COMPLEX_EXPR, type, tem,
9931 fold_convert_loc (loc, itype, integer_zero_node));
9935 /* Subroutine of fold_binary. If P is the value of EXPR, computes
9936 power-of-two M and (arbitrary) N such that M divides (P-N). This condition
9937 guarantees that P and N have the same least significant log2(M) bits.
9938 N is not otherwise constrained. In particular, N is not normalized to
9939 0 <= N < M as is common. In general, the precise value of P is unknown.
9940 M is chosen as large as possible such that constant N can be determined.
9942 Returns M and sets *RESIDUE to N.
9944 If ALLOW_FUNC_ALIGN is true, do take functions' DECL_ALIGN_UNIT into
9945 account. This is not always possible due to PR 35705.
9948 static unsigned HOST_WIDE_INT
9949 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue,
9950 bool allow_func_align)
9952 enum tree_code code;
9956 code = TREE_CODE (expr);
9957 if (code == ADDR_EXPR)
9959 expr = TREE_OPERAND (expr, 0);
9960 if (handled_component_p (expr))
9962 HOST_WIDE_INT bitsize, bitpos;
9964 enum machine_mode mode;
9965 int unsignedp, volatilep;
9967 expr = get_inner_reference (expr, &bitsize, &bitpos, &offset,
9968 &mode, &unsignedp, &volatilep, false);
9969 *residue = bitpos / BITS_PER_UNIT;
9972 if (TREE_CODE (offset) == INTEGER_CST)
9973 *residue += TREE_INT_CST_LOW (offset);
9975 /* We don't handle more complicated offset expressions. */
9981 && (allow_func_align || TREE_CODE (expr) != FUNCTION_DECL))
9982 return DECL_ALIGN_UNIT (expr);
9984 else if (code == POINTER_PLUS_EXPR)
9987 unsigned HOST_WIDE_INT modulus;
9988 enum tree_code inner_code;
9990 op0 = TREE_OPERAND (expr, 0);
9992 modulus = get_pointer_modulus_and_residue (op0, residue,
9995 op1 = TREE_OPERAND (expr, 1);
9997 inner_code = TREE_CODE (op1);
9998 if (inner_code == INTEGER_CST)
10000 *residue += TREE_INT_CST_LOW (op1);
10003 else if (inner_code == MULT_EXPR)
10005 op1 = TREE_OPERAND (op1, 1);
10006 if (TREE_CODE (op1) == INTEGER_CST)
10008 unsigned HOST_WIDE_INT align;
10010 /* Compute the greatest power-of-2 divisor of op1. */
10011 align = TREE_INT_CST_LOW (op1);
10014 /* If align is non-zero and less than *modulus, replace
10015 *modulus with align., If align is 0, then either op1 is 0
10016 or the greatest power-of-2 divisor of op1 doesn't fit in an
10017 unsigned HOST_WIDE_INT. In either case, no additional
10018 constraint is imposed. */
10020 modulus = MIN (modulus, align);
10027 /* If we get here, we were unable to determine anything useful about the
10033 /* Fold a binary expression of code CODE and type TYPE with operands
10034 OP0 and OP1. LOC is the location of the resulting expression.
10035 Return the folded expression if folding is successful. Otherwise,
10036 return NULL_TREE. */
10039 fold_binary_loc (location_t loc,
10040 enum tree_code code, tree type, tree op0, tree op1)
10042 enum tree_code_class kind = TREE_CODE_CLASS (code);
10043 tree arg0, arg1, tem;
10044 tree t1 = NULL_TREE;
10045 bool strict_overflow_p;
10047 gcc_assert (IS_EXPR_CODE_CLASS (kind)
10048 && TREE_CODE_LENGTH (code) == 2
10049 && op0 != NULL_TREE
10050 && op1 != NULL_TREE);
10055 /* Strip any conversions that don't change the mode. This is
10056 safe for every expression, except for a comparison expression
10057 because its signedness is derived from its operands. So, in
10058 the latter case, only strip conversions that don't change the
10059 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
10062 Note that this is done as an internal manipulation within the
10063 constant folder, in order to find the simplest representation
10064 of the arguments so that their form can be studied. In any
10065 cases, the appropriate type conversions should be put back in
10066 the tree that will get out of the constant folder. */
10068 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
10070 STRIP_SIGN_NOPS (arg0);
10071 STRIP_SIGN_NOPS (arg1);
10079 /* Note that TREE_CONSTANT isn't enough: static var addresses are
10080 constant but we can't do arithmetic on them. */
10081 if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
10082 || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
10083 || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
10084 || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
10085 || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
10086 || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
10088 if (kind == tcc_binary)
10090 /* Make sure type and arg0 have the same saturating flag. */
10091 gcc_assert (TYPE_SATURATING (type)
10092 == TYPE_SATURATING (TREE_TYPE (arg0)));
10093 tem = const_binop (code, arg0, arg1, 0);
10095 else if (kind == tcc_comparison)
10096 tem = fold_relational_const (code, type, arg0, arg1);
10100 if (tem != NULL_TREE)
10102 if (TREE_TYPE (tem) != type)
10103 tem = fold_convert_loc (loc, type, tem);
10108 /* If this is a commutative operation, and ARG0 is a constant, move it
10109 to ARG1 to reduce the number of tests below. */
10110 if (commutative_tree_code (code)
10111 && tree_swap_operands_p (arg0, arg1, true))
10112 return fold_build2_loc (loc, code, type, op1, op0);
10114 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
10116 First check for cases where an arithmetic operation is applied to a
10117 compound, conditional, or comparison operation. Push the arithmetic
10118 operation inside the compound or conditional to see if any folding
10119 can then be done. Convert comparison to conditional for this purpose.
10120 The also optimizes non-constant cases that used to be done in
10123 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
10124 one of the operands is a comparison and the other is a comparison, a
10125 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
10126 code below would make the expression more complex. Change it to a
10127 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
10128 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
10130 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
10131 || code == EQ_EXPR || code == NE_EXPR)
10132 && ((truth_value_p (TREE_CODE (arg0))
10133 && (truth_value_p (TREE_CODE (arg1))
10134 || (TREE_CODE (arg1) == BIT_AND_EXPR
10135 && integer_onep (TREE_OPERAND (arg1, 1)))))
10136 || (truth_value_p (TREE_CODE (arg1))
10137 && (truth_value_p (TREE_CODE (arg0))
10138 || (TREE_CODE (arg0) == BIT_AND_EXPR
10139 && integer_onep (TREE_OPERAND (arg0, 1)))))))
10141 tem = fold_build2_loc (loc, code == BIT_AND_EXPR ? TRUTH_AND_EXPR
10142 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
10145 fold_convert_loc (loc, boolean_type_node, arg0),
10146 fold_convert_loc (loc, boolean_type_node, arg1));
10148 if (code == EQ_EXPR)
10149 tem = invert_truthvalue_loc (loc, tem);
10151 return fold_convert_loc (loc, type, tem);
10154 if (TREE_CODE_CLASS (code) == tcc_binary
10155 || TREE_CODE_CLASS (code) == tcc_comparison)
10157 if (TREE_CODE (arg0) == COMPOUND_EXPR)
10159 tem = fold_build2_loc (loc, code, type,
10160 fold_convert_loc (loc, TREE_TYPE (op0),
10161 TREE_OPERAND (arg0, 1)), op1);
10162 tem = build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0), tem);
10163 goto fold_binary_exit;
10165 if (TREE_CODE (arg1) == COMPOUND_EXPR
10166 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10168 tem = fold_build2_loc (loc, code, type, op0,
10169 fold_convert_loc (loc, TREE_TYPE (op1),
10170 TREE_OPERAND (arg1, 1)));
10171 tem = build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0), tem);
10172 goto fold_binary_exit;
10175 if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
10177 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
10179 /*cond_first_p=*/1);
10180 if (tem != NULL_TREE)
10184 if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
10186 tem = fold_binary_op_with_conditional_arg (loc, code, type, op0, op1,
10188 /*cond_first_p=*/0);
10189 if (tem != NULL_TREE)
10196 case POINTER_PLUS_EXPR:
10197 /* 0 +p index -> (type)index */
10198 if (integer_zerop (arg0))
10199 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10201 /* PTR +p 0 -> PTR */
10202 if (integer_zerop (arg1))
10203 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10205 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
10206 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
10207 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
10208 return fold_convert_loc (loc, type,
10209 fold_build2_loc (loc, PLUS_EXPR, sizetype,
10210 fold_convert_loc (loc, sizetype,
10212 fold_convert_loc (loc, sizetype,
10215 /* index +p PTR -> PTR +p index */
10216 if (POINTER_TYPE_P (TREE_TYPE (arg1))
10217 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
10218 return fold_build2_loc (loc, POINTER_PLUS_EXPR, type,
10219 fold_convert_loc (loc, type, arg1),
10220 fold_convert_loc (loc, sizetype, arg0));
10222 /* (PTR +p B) +p A -> PTR +p (B + A) */
10223 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10226 tree arg01 = fold_convert_loc (loc, sizetype, TREE_OPERAND (arg0, 1));
10227 tree arg00 = TREE_OPERAND (arg0, 0);
10228 inner = fold_build2_loc (loc, PLUS_EXPR, sizetype,
10229 arg01, fold_convert_loc (loc, sizetype, arg1));
10230 return fold_convert_loc (loc, type,
10231 fold_build2_loc (loc, POINTER_PLUS_EXPR,
10236 /* PTR_CST +p CST -> CST1 */
10237 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
10238 return fold_build2_loc (loc, PLUS_EXPR, type, arg0,
10239 fold_convert_loc (loc, type, arg1));
10241 /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
10242 of the array. Loop optimizer sometimes produce this type of
10244 if (TREE_CODE (arg0) == ADDR_EXPR)
10246 tem = try_move_mult_to_index (loc, arg0,
10247 fold_convert_loc (loc, sizetype, arg1));
10249 return fold_convert_loc (loc, type, tem);
10255 /* A + (-B) -> A - B */
10256 if (TREE_CODE (arg1) == NEGATE_EXPR)
10257 return fold_build2_loc (loc, MINUS_EXPR, type,
10258 fold_convert_loc (loc, type, arg0),
10259 fold_convert_loc (loc, type,
10260 TREE_OPERAND (arg1, 0)));
10261 /* (-A) + B -> B - A */
10262 if (TREE_CODE (arg0) == NEGATE_EXPR
10263 && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
10264 return fold_build2_loc (loc, MINUS_EXPR, type,
10265 fold_convert_loc (loc, type, arg1),
10266 fold_convert_loc (loc, type,
10267 TREE_OPERAND (arg0, 0)));
10269 if (INTEGRAL_TYPE_P (type))
10271 /* Convert ~A + 1 to -A. */
10272 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10273 && integer_onep (arg1))
10274 return fold_build1_loc (loc, NEGATE_EXPR, type,
10275 fold_convert_loc (loc, type,
10276 TREE_OPERAND (arg0, 0)));
10278 /* ~X + X is -1. */
10279 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10280 && !TYPE_OVERFLOW_TRAPS (type))
10282 tree tem = TREE_OPERAND (arg0, 0);
10285 if (operand_equal_p (tem, arg1, 0))
10287 t1 = build_int_cst_type (type, -1);
10288 return omit_one_operand_loc (loc, type, t1, arg1);
10292 /* X + ~X is -1. */
10293 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10294 && !TYPE_OVERFLOW_TRAPS (type))
10296 tree tem = TREE_OPERAND (arg1, 0);
10299 if (operand_equal_p (arg0, tem, 0))
10301 t1 = build_int_cst_type (type, -1);
10302 return omit_one_operand_loc (loc, type, t1, arg0);
10306 /* X + (X / CST) * -CST is X % CST. */
10307 if (TREE_CODE (arg1) == MULT_EXPR
10308 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10309 && operand_equal_p (arg0,
10310 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
10312 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
10313 tree cst1 = TREE_OPERAND (arg1, 1);
10314 tree sum = fold_binary_loc (loc, PLUS_EXPR, TREE_TYPE (cst1),
10316 if (sum && integer_zerop (sum))
10317 return fold_convert_loc (loc, type,
10318 fold_build2_loc (loc, TRUNC_MOD_EXPR,
10319 TREE_TYPE (arg0), arg0,
10324 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
10325 same or one. Make sure type is not saturating.
10326 fold_plusminus_mult_expr will re-associate. */
10327 if ((TREE_CODE (arg0) == MULT_EXPR
10328 || TREE_CODE (arg1) == MULT_EXPR)
10329 && !TYPE_SATURATING (type)
10330 && (!FLOAT_TYPE_P (type) || flag_associative_math))
10332 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10337 if (! FLOAT_TYPE_P (type))
10339 if (integer_zerop (arg1))
10340 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10342 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
10343 with a constant, and the two constants have no bits in common,
10344 we should treat this as a BIT_IOR_EXPR since this may produce more
10345 simplifications. */
10346 if (TREE_CODE (arg0) == BIT_AND_EXPR
10347 && TREE_CODE (arg1) == BIT_AND_EXPR
10348 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10349 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10350 && integer_zerop (const_binop (BIT_AND_EXPR,
10351 TREE_OPERAND (arg0, 1),
10352 TREE_OPERAND (arg1, 1), 0)))
10354 code = BIT_IOR_EXPR;
10358 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
10359 (plus (plus (mult) (mult)) (foo)) so that we can
10360 take advantage of the factoring cases below. */
10361 if (((TREE_CODE (arg0) == PLUS_EXPR
10362 || TREE_CODE (arg0) == MINUS_EXPR)
10363 && TREE_CODE (arg1) == MULT_EXPR)
10364 || ((TREE_CODE (arg1) == PLUS_EXPR
10365 || TREE_CODE (arg1) == MINUS_EXPR)
10366 && TREE_CODE (arg0) == MULT_EXPR))
10368 tree parg0, parg1, parg, marg;
10369 enum tree_code pcode;
10371 if (TREE_CODE (arg1) == MULT_EXPR)
10372 parg = arg0, marg = arg1;
10374 parg = arg1, marg = arg0;
10375 pcode = TREE_CODE (parg);
10376 parg0 = TREE_OPERAND (parg, 0);
10377 parg1 = TREE_OPERAND (parg, 1);
10378 STRIP_NOPS (parg0);
10379 STRIP_NOPS (parg1);
10381 if (TREE_CODE (parg0) == MULT_EXPR
10382 && TREE_CODE (parg1) != MULT_EXPR)
10383 return fold_build2_loc (loc, pcode, type,
10384 fold_build2_loc (loc, PLUS_EXPR, type,
10385 fold_convert_loc (loc, type,
10387 fold_convert_loc (loc, type,
10389 fold_convert_loc (loc, type, parg1));
10390 if (TREE_CODE (parg0) != MULT_EXPR
10391 && TREE_CODE (parg1) == MULT_EXPR)
10393 fold_build2_loc (loc, PLUS_EXPR, type,
10394 fold_convert_loc (loc, type, parg0),
10395 fold_build2_loc (loc, pcode, type,
10396 fold_convert_loc (loc, type, marg),
10397 fold_convert_loc (loc, type,
10403 /* See if ARG1 is zero and X + ARG1 reduces to X. */
10404 if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
10405 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10407 /* Likewise if the operands are reversed. */
10408 if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10409 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
10411 /* Convert X + -C into X - C. */
10412 if (TREE_CODE (arg1) == REAL_CST
10413 && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
10415 tem = fold_negate_const (arg1, type);
10416 if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
10417 return fold_build2_loc (loc, MINUS_EXPR, type,
10418 fold_convert_loc (loc, type, arg0),
10419 fold_convert_loc (loc, type, tem));
10422 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
10423 to __complex__ ( x, y ). This is not the same for SNaNs or
10424 if signed zeros are involved. */
10425 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10426 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10427 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10429 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10430 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10431 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10432 bool arg0rz = false, arg0iz = false;
10433 if ((arg0r && (arg0rz = real_zerop (arg0r)))
10434 || (arg0i && (arg0iz = real_zerop (arg0i))))
10436 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10437 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10438 if (arg0rz && arg1i && real_zerop (arg1i))
10440 tree rp = arg1r ? arg1r
10441 : build1 (REALPART_EXPR, rtype, arg1);
10442 tree ip = arg0i ? arg0i
10443 : build1 (IMAGPART_EXPR, rtype, arg0);
10444 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10446 else if (arg0iz && arg1r && real_zerop (arg1r))
10448 tree rp = arg0r ? arg0r
10449 : build1 (REALPART_EXPR, rtype, arg0);
10450 tree ip = arg1i ? arg1i
10451 : build1 (IMAGPART_EXPR, rtype, arg1);
10452 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10457 if (flag_unsafe_math_optimizations
10458 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10459 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10460 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
10463 /* Convert x+x into x*2.0. */
10464 if (operand_equal_p (arg0, arg1, 0)
10465 && SCALAR_FLOAT_TYPE_P (type))
10466 return fold_build2_loc (loc, MULT_EXPR, type, arg0,
10467 build_real (type, dconst2));
10469 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
10470 We associate floats only if the user has specified
10471 -fassociative-math. */
10472 if (flag_associative_math
10473 && TREE_CODE (arg1) == PLUS_EXPR
10474 && TREE_CODE (arg0) != MULT_EXPR)
10476 tree tree10 = TREE_OPERAND (arg1, 0);
10477 tree tree11 = TREE_OPERAND (arg1, 1);
10478 if (TREE_CODE (tree11) == MULT_EXPR
10479 && TREE_CODE (tree10) == MULT_EXPR)
10482 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, arg0, tree10);
10483 return fold_build2_loc (loc, PLUS_EXPR, type, tree0, tree11);
10486 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
10487 We associate floats only if the user has specified
10488 -fassociative-math. */
10489 if (flag_associative_math
10490 && TREE_CODE (arg0) == PLUS_EXPR
10491 && TREE_CODE (arg1) != MULT_EXPR)
10493 tree tree00 = TREE_OPERAND (arg0, 0);
10494 tree tree01 = TREE_OPERAND (arg0, 1);
10495 if (TREE_CODE (tree01) == MULT_EXPR
10496 && TREE_CODE (tree00) == MULT_EXPR)
10499 tree0 = fold_build2_loc (loc, PLUS_EXPR, type, tree01, arg1);
10500 return fold_build2_loc (loc, PLUS_EXPR, type, tree00, tree0);
10506 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
10507 is a rotate of A by C1 bits. */
10508 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
10509 is a rotate of A by B bits. */
10511 enum tree_code code0, code1;
10513 code0 = TREE_CODE (arg0);
10514 code1 = TREE_CODE (arg1);
10515 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
10516 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
10517 && operand_equal_p (TREE_OPERAND (arg0, 0),
10518 TREE_OPERAND (arg1, 0), 0)
10519 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
10520 TYPE_UNSIGNED (rtype))
10521 /* Only create rotates in complete modes. Other cases are not
10522 expanded properly. */
10523 && TYPE_PRECISION (rtype) == GET_MODE_PRECISION (TYPE_MODE (rtype)))
10525 tree tree01, tree11;
10526 enum tree_code code01, code11;
10528 tree01 = TREE_OPERAND (arg0, 1);
10529 tree11 = TREE_OPERAND (arg1, 1);
10530 STRIP_NOPS (tree01);
10531 STRIP_NOPS (tree11);
10532 code01 = TREE_CODE (tree01);
10533 code11 = TREE_CODE (tree11);
10534 if (code01 == INTEGER_CST
10535 && code11 == INTEGER_CST
10536 && TREE_INT_CST_HIGH (tree01) == 0
10537 && TREE_INT_CST_HIGH (tree11) == 0
10538 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
10539 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
10541 tem = build2 (LROTATE_EXPR,
10542 TREE_TYPE (TREE_OPERAND (arg0, 0)),
10543 TREE_OPERAND (arg0, 0),
10544 code0 == LSHIFT_EXPR
10545 ? tree01 : tree11);
10546 SET_EXPR_LOCATION (tem, loc);
10547 return fold_convert_loc (loc, type, tem);
10549 else if (code11 == MINUS_EXPR)
10551 tree tree110, tree111;
10552 tree110 = TREE_OPERAND (tree11, 0);
10553 tree111 = TREE_OPERAND (tree11, 1);
10554 STRIP_NOPS (tree110);
10555 STRIP_NOPS (tree111);
10556 if (TREE_CODE (tree110) == INTEGER_CST
10557 && 0 == compare_tree_int (tree110,
10559 (TREE_TYPE (TREE_OPERAND
10561 && operand_equal_p (tree01, tree111, 0))
10563 fold_convert_loc (loc, type,
10564 build2 ((code0 == LSHIFT_EXPR
10567 TREE_TYPE (TREE_OPERAND (arg0, 0)),
10568 TREE_OPERAND (arg0, 0), tree01));
10570 else if (code01 == MINUS_EXPR)
10572 tree tree010, tree011;
10573 tree010 = TREE_OPERAND (tree01, 0);
10574 tree011 = TREE_OPERAND (tree01, 1);
10575 STRIP_NOPS (tree010);
10576 STRIP_NOPS (tree011);
10577 if (TREE_CODE (tree010) == INTEGER_CST
10578 && 0 == compare_tree_int (tree010,
10580 (TREE_TYPE (TREE_OPERAND
10582 && operand_equal_p (tree11, tree011, 0))
10583 return fold_convert_loc
10585 build2 ((code0 != LSHIFT_EXPR
10588 TREE_TYPE (TREE_OPERAND (arg0, 0)),
10589 TREE_OPERAND (arg0, 0), tree11));
10595 /* In most languages, can't associate operations on floats through
10596 parentheses. Rather than remember where the parentheses were, we
10597 don't associate floats at all, unless the user has specified
10598 -fassociative-math.
10599 And, we need to make sure type is not saturating. */
10601 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
10602 && !TYPE_SATURATING (type))
10604 tree var0, con0, lit0, minus_lit0;
10605 tree var1, con1, lit1, minus_lit1;
10608 /* Split both trees into variables, constants, and literals. Then
10609 associate each group together, the constants with literals,
10610 then the result with variables. This increases the chances of
10611 literals being recombined later and of generating relocatable
10612 expressions for the sum of a constant and literal. */
10613 var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
10614 var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
10615 code == MINUS_EXPR);
10617 /* With undefined overflow we can only associate constants
10618 with one variable. */
10619 if (((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
10620 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
10626 if (TREE_CODE (tmp0) == NEGATE_EXPR)
10627 tmp0 = TREE_OPERAND (tmp0, 0);
10628 if (TREE_CODE (tmp1) == NEGATE_EXPR)
10629 tmp1 = TREE_OPERAND (tmp1, 0);
10630 /* The only case we can still associate with two variables
10631 is if they are the same, modulo negation. */
10632 if (!operand_equal_p (tmp0, tmp1, 0))
10636 /* Only do something if we found more than two objects. Otherwise,
10637 nothing has changed and we risk infinite recursion. */
10639 && (2 < ((var0 != 0) + (var1 != 0)
10640 + (con0 != 0) + (con1 != 0)
10641 + (lit0 != 0) + (lit1 != 0)
10642 + (minus_lit0 != 0) + (minus_lit1 != 0))))
10644 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
10645 if (code == MINUS_EXPR)
10648 var0 = associate_trees (loc, var0, var1, code, type);
10649 con0 = associate_trees (loc, con0, con1, code, type);
10650 lit0 = associate_trees (loc, lit0, lit1, code, type);
10651 minus_lit0 = associate_trees (loc, minus_lit0, minus_lit1, code, type);
10653 /* Preserve the MINUS_EXPR if the negative part of the literal is
10654 greater than the positive part. Otherwise, the multiplicative
10655 folding code (i.e extract_muldiv) may be fooled in case
10656 unsigned constants are subtracted, like in the following
10657 example: ((X*2 + 4) - 8U)/2. */
10658 if (minus_lit0 && lit0)
10660 if (TREE_CODE (lit0) == INTEGER_CST
10661 && TREE_CODE (minus_lit0) == INTEGER_CST
10662 && tree_int_cst_lt (lit0, minus_lit0))
10664 minus_lit0 = associate_trees (loc, minus_lit0, lit0,
10670 lit0 = associate_trees (loc, lit0, minus_lit0,
10679 fold_convert_loc (loc, type,
10680 associate_trees (loc, var0, minus_lit0,
10681 MINUS_EXPR, type));
10684 con0 = associate_trees (loc, con0, minus_lit0,
10687 fold_convert_loc (loc, type,
10688 associate_trees (loc, var0, con0,
10693 con0 = associate_trees (loc, con0, lit0, code, type);
10695 fold_convert_loc (loc, type, associate_trees (loc, var0, con0,
10703 /* Pointer simplifications for subtraction, simple reassociations. */
10704 if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
10706 /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
10707 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
10708 && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10710 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10711 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10712 tree arg10 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
10713 tree arg11 = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
10714 return fold_build2_loc (loc, PLUS_EXPR, type,
10715 fold_build2_loc (loc, MINUS_EXPR, type,
10717 fold_build2_loc (loc, MINUS_EXPR, type,
10720 /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
10721 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10723 tree arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
10724 tree arg01 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
10725 tree tmp = fold_binary_loc (loc, MINUS_EXPR, type, arg00,
10726 fold_convert_loc (loc, type, arg1));
10728 return fold_build2_loc (loc, PLUS_EXPR, type, tmp, arg01);
10731 /* A - (-B) -> A + B */
10732 if (TREE_CODE (arg1) == NEGATE_EXPR)
10733 return fold_build2_loc (loc, PLUS_EXPR, type, op0,
10734 fold_convert_loc (loc, type,
10735 TREE_OPERAND (arg1, 0)));
10736 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
10737 if (TREE_CODE (arg0) == NEGATE_EXPR
10738 && (FLOAT_TYPE_P (type)
10739 || INTEGRAL_TYPE_P (type))
10740 && negate_expr_p (arg1)
10741 && reorder_operands_p (arg0, arg1))
10742 return fold_build2_loc (loc, MINUS_EXPR, type,
10743 fold_convert_loc (loc, type,
10744 negate_expr (arg1)),
10745 fold_convert_loc (loc, type,
10746 TREE_OPERAND (arg0, 0)));
10747 /* Convert -A - 1 to ~A. */
10748 if (INTEGRAL_TYPE_P (type)
10749 && TREE_CODE (arg0) == NEGATE_EXPR
10750 && integer_onep (arg1)
10751 && !TYPE_OVERFLOW_TRAPS (type))
10752 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
10753 fold_convert_loc (loc, type,
10754 TREE_OPERAND (arg0, 0)));
10756 /* Convert -1 - A to ~A. */
10757 if (INTEGRAL_TYPE_P (type)
10758 && integer_all_onesp (arg0))
10759 return fold_build1_loc (loc, BIT_NOT_EXPR, type, op1);
10762 /* X - (X / CST) * CST is X % CST. */
10763 if (INTEGRAL_TYPE_P (type)
10764 && TREE_CODE (arg1) == MULT_EXPR
10765 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10766 && operand_equal_p (arg0,
10767 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
10768 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
10769 TREE_OPERAND (arg1, 1), 0))
10771 fold_convert_loc (loc, type,
10772 fold_build2_loc (loc, TRUNC_MOD_EXPR, TREE_TYPE (arg0),
10773 arg0, TREE_OPERAND (arg1, 1)));
10775 if (! FLOAT_TYPE_P (type))
10777 if (integer_zerop (arg0))
10778 return negate_expr (fold_convert_loc (loc, type, arg1));
10779 if (integer_zerop (arg1))
10780 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10782 /* Fold A - (A & B) into ~B & A. */
10783 if (!TREE_SIDE_EFFECTS (arg0)
10784 && TREE_CODE (arg1) == BIT_AND_EXPR)
10786 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
10788 tree arg10 = fold_convert_loc (loc, type,
10789 TREE_OPERAND (arg1, 0));
10790 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10791 fold_build1_loc (loc, BIT_NOT_EXPR,
10793 fold_convert_loc (loc, type, arg0));
10795 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10797 tree arg11 = fold_convert_loc (loc,
10798 type, TREE_OPERAND (arg1, 1));
10799 return fold_build2_loc (loc, BIT_AND_EXPR, type,
10800 fold_build1_loc (loc, BIT_NOT_EXPR,
10802 fold_convert_loc (loc, type, arg0));
10806 /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10807 any power of 2 minus 1. */
10808 if (TREE_CODE (arg0) == BIT_AND_EXPR
10809 && TREE_CODE (arg1) == BIT_AND_EXPR
10810 && operand_equal_p (TREE_OPERAND (arg0, 0),
10811 TREE_OPERAND (arg1, 0), 0))
10813 tree mask0 = TREE_OPERAND (arg0, 1);
10814 tree mask1 = TREE_OPERAND (arg1, 1);
10815 tree tem = fold_build1_loc (loc, BIT_NOT_EXPR, type, mask0);
10817 if (operand_equal_p (tem, mask1, 0))
10819 tem = fold_build2_loc (loc, BIT_XOR_EXPR, type,
10820 TREE_OPERAND (arg0, 0), mask1);
10821 return fold_build2_loc (loc, MINUS_EXPR, type, tem, mask1);
10826 /* See if ARG1 is zero and X - ARG1 reduces to X. */
10827 else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
10828 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10830 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
10831 ARG0 is zero and X + ARG0 reduces to X, since that would mean
10832 (-ARG1 + ARG0) reduces to -ARG1. */
10833 else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10834 return negate_expr (fold_convert_loc (loc, type, arg1));
10836 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10837 __complex__ ( x, -y ). This is not the same for SNaNs or if
10838 signed zeros are involved. */
10839 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10840 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10841 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10843 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10844 tree arg0r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg0);
10845 tree arg0i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg0);
10846 bool arg0rz = false, arg0iz = false;
10847 if ((arg0r && (arg0rz = real_zerop (arg0r)))
10848 || (arg0i && (arg0iz = real_zerop (arg0i))))
10850 tree arg1r = fold_unary_loc (loc, REALPART_EXPR, rtype, arg1);
10851 tree arg1i = fold_unary_loc (loc, IMAGPART_EXPR, rtype, arg1);
10852 if (arg0rz && arg1i && real_zerop (arg1i))
10854 tree rp = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10856 : build1 (REALPART_EXPR, rtype, arg1));
10857 tree ip = arg0i ? arg0i
10858 : build1 (IMAGPART_EXPR, rtype, arg0);
10859 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10861 else if (arg0iz && arg1r && real_zerop (arg1r))
10863 tree rp = arg0r ? arg0r
10864 : build1 (REALPART_EXPR, rtype, arg0);
10865 tree ip = fold_build1_loc (loc, NEGATE_EXPR, rtype,
10867 : build1 (IMAGPART_EXPR, rtype, arg1));
10868 return fold_build2_loc (loc, COMPLEX_EXPR, type, rp, ip);
10873 /* Fold &x - &x. This can happen from &x.foo - &x.
10874 This is unsafe for certain floats even in non-IEEE formats.
10875 In IEEE, it is unsafe because it does wrong for NaNs.
10876 Also note that operand_equal_p is always false if an operand
10879 if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
10880 && operand_equal_p (arg0, arg1, 0))
10881 return fold_convert_loc (loc, type, integer_zero_node);
10883 /* A - B -> A + (-B) if B is easily negatable. */
10884 if (negate_expr_p (arg1)
10885 && ((FLOAT_TYPE_P (type)
10886 /* Avoid this transformation if B is a positive REAL_CST. */
10887 && (TREE_CODE (arg1) != REAL_CST
10888 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10889 || INTEGRAL_TYPE_P (type)))
10890 return fold_build2_loc (loc, PLUS_EXPR, type,
10891 fold_convert_loc (loc, type, arg0),
10892 fold_convert_loc (loc, type,
10893 negate_expr (arg1)));
10895 /* Try folding difference of addresses. */
10897 HOST_WIDE_INT diff;
10899 if ((TREE_CODE (arg0) == ADDR_EXPR
10900 || TREE_CODE (arg1) == ADDR_EXPR)
10901 && ptr_difference_const (arg0, arg1, &diff))
10902 return build_int_cst_type (type, diff);
10905 /* Fold &a[i] - &a[j] to i-j. */
10906 if (TREE_CODE (arg0) == ADDR_EXPR
10907 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10908 && TREE_CODE (arg1) == ADDR_EXPR
10909 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10911 tree aref0 = TREE_OPERAND (arg0, 0);
10912 tree aref1 = TREE_OPERAND (arg1, 0);
10913 if (operand_equal_p (TREE_OPERAND (aref0, 0),
10914 TREE_OPERAND (aref1, 0), 0))
10916 tree op0 = fold_convert_loc (loc, type, TREE_OPERAND (aref0, 1));
10917 tree op1 = fold_convert_loc (loc, type, TREE_OPERAND (aref1, 1));
10918 tree esz = array_ref_element_size (aref0);
10919 tree diff = build2 (MINUS_EXPR, type, op0, op1);
10920 return fold_build2_loc (loc, MULT_EXPR, type, diff,
10921 fold_convert_loc (loc, type, esz));
10926 if (FLOAT_TYPE_P (type)
10927 && flag_unsafe_math_optimizations
10928 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10929 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10930 && (tem = distribute_real_division (loc, code, type, arg0, arg1)))
10933 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
10934 same or one. Make sure type is not saturating.
10935 fold_plusminus_mult_expr will re-associate. */
10936 if ((TREE_CODE (arg0) == MULT_EXPR
10937 || TREE_CODE (arg1) == MULT_EXPR)
10938 && !TYPE_SATURATING (type)
10939 && (!FLOAT_TYPE_P (type) || flag_associative_math))
10941 tree tem = fold_plusminus_mult_expr (loc, code, type, arg0, arg1);
10949 /* (-A) * (-B) -> A * B */
10950 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10951 return fold_build2_loc (loc, MULT_EXPR, type,
10952 fold_convert_loc (loc, type,
10953 TREE_OPERAND (arg0, 0)),
10954 fold_convert_loc (loc, type,
10955 negate_expr (arg1)));
10956 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10957 return fold_build2_loc (loc, MULT_EXPR, type,
10958 fold_convert_loc (loc, type,
10959 negate_expr (arg0)),
10960 fold_convert_loc (loc, type,
10961 TREE_OPERAND (arg1, 0)));
10963 if (! FLOAT_TYPE_P (type))
10965 if (integer_zerop (arg1))
10966 return omit_one_operand_loc (loc, type, arg1, arg0);
10967 if (integer_onep (arg1))
10968 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
10969 /* Transform x * -1 into -x. Make sure to do the negation
10970 on the original operand with conversions not stripped
10971 because we can only strip non-sign-changing conversions. */
10972 if (integer_all_onesp (arg1))
10973 return fold_convert_loc (loc, type, negate_expr (op0));
10974 /* Transform x * -C into -x * C if x is easily negatable. */
10975 if (TREE_CODE (arg1) == INTEGER_CST
10976 && tree_int_cst_sgn (arg1) == -1
10977 && negate_expr_p (arg0)
10978 && (tem = negate_expr (arg1)) != arg1
10979 && !TREE_OVERFLOW (tem))
10980 return fold_build2_loc (loc, MULT_EXPR, type,
10981 fold_convert_loc (loc, type,
10982 negate_expr (arg0)),
10985 /* (a * (1 << b)) is (a << b) */
10986 if (TREE_CODE (arg1) == LSHIFT_EXPR
10987 && integer_onep (TREE_OPERAND (arg1, 0)))
10988 return fold_build2_loc (loc, LSHIFT_EXPR, type, op0,
10989 TREE_OPERAND (arg1, 1));
10990 if (TREE_CODE (arg0) == LSHIFT_EXPR
10991 && integer_onep (TREE_OPERAND (arg0, 0)))
10992 return fold_build2_loc (loc, LSHIFT_EXPR, type, op1,
10993 TREE_OPERAND (arg0, 1));
10995 /* (A + A) * C -> A * 2 * C */
10996 if (TREE_CODE (arg0) == PLUS_EXPR
10997 && TREE_CODE (arg1) == INTEGER_CST
10998 && operand_equal_p (TREE_OPERAND (arg0, 0),
10999 TREE_OPERAND (arg0, 1), 0))
11000 return fold_build2_loc (loc, MULT_EXPR, type,
11001 omit_one_operand_loc (loc, type,
11002 TREE_OPERAND (arg0, 0),
11003 TREE_OPERAND (arg0, 1)),
11004 fold_build2_loc (loc, MULT_EXPR, type,
11005 build_int_cst (type, 2) , arg1));
11007 strict_overflow_p = false;
11008 if (TREE_CODE (arg1) == INTEGER_CST
11009 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11010 &strict_overflow_p)))
11012 if (strict_overflow_p)
11013 fold_overflow_warning (("assuming signed overflow does not "
11014 "occur when simplifying "
11016 WARN_STRICT_OVERFLOW_MISC);
11017 return fold_convert_loc (loc, type, tem);
11020 /* Optimize z * conj(z) for integer complex numbers. */
11021 if (TREE_CODE (arg0) == CONJ_EXPR
11022 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11023 return fold_mult_zconjz (loc, type, arg1);
11024 if (TREE_CODE (arg1) == CONJ_EXPR
11025 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11026 return fold_mult_zconjz (loc, type, arg0);
11030 /* Maybe fold x * 0 to 0. The expressions aren't the same
11031 when x is NaN, since x * 0 is also NaN. Nor are they the
11032 same in modes with signed zeros, since multiplying a
11033 negative value by 0 gives -0, not +0. */
11034 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11035 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
11036 && real_zerop (arg1))
11037 return omit_one_operand_loc (loc, type, arg1, arg0);
11038 /* In IEEE floating point, x*1 is not equivalent to x for snans.
11039 Likewise for complex arithmetic with signed zeros. */
11040 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11041 && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
11042 || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11043 && real_onep (arg1))
11044 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11046 /* Transform x * -1.0 into -x. */
11047 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11048 && (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
11049 || !COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
11050 && real_minus_onep (arg1))
11051 return fold_convert_loc (loc, type, negate_expr (arg0));
11053 /* Convert (C1/X)*C2 into (C1*C2)/X. This transformation may change
11054 the result for floating point types due to rounding so it is applied
11055 only if -fassociative-math was specify. */
11056 if (flag_associative_math
11057 && TREE_CODE (arg0) == RDIV_EXPR
11058 && TREE_CODE (arg1) == REAL_CST
11059 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
11061 tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
11064 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
11065 TREE_OPERAND (arg0, 1));
11068 /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y. */
11069 if (operand_equal_p (arg0, arg1, 0))
11071 tree tem = fold_strip_sign_ops (arg0);
11072 if (tem != NULL_TREE)
11074 tem = fold_convert_loc (loc, type, tem);
11075 return fold_build2_loc (loc, MULT_EXPR, type, tem, tem);
11079 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
11080 This is not the same for NaNs or if signed zeros are
11082 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11083 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
11084 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11085 && TREE_CODE (arg1) == COMPLEX_CST
11086 && real_zerop (TREE_REALPART (arg1)))
11088 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
11089 if (real_onep (TREE_IMAGPART (arg1)))
11091 fold_build2_loc (loc, COMPLEX_EXPR, type,
11092 negate_expr (fold_build1_loc (loc, IMAGPART_EXPR,
11094 fold_build1_loc (loc, REALPART_EXPR, rtype, arg0));
11095 else if (real_minus_onep (TREE_IMAGPART (arg1)))
11097 fold_build2_loc (loc, COMPLEX_EXPR, type,
11098 fold_build1_loc (loc, IMAGPART_EXPR, rtype, arg0),
11099 negate_expr (fold_build1_loc (loc, REALPART_EXPR,
11103 /* Optimize z * conj(z) for floating point complex numbers.
11104 Guarded by flag_unsafe_math_optimizations as non-finite
11105 imaginary components don't produce scalar results. */
11106 if (flag_unsafe_math_optimizations
11107 && TREE_CODE (arg0) == CONJ_EXPR
11108 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11109 return fold_mult_zconjz (loc, type, arg1);
11110 if (flag_unsafe_math_optimizations
11111 && TREE_CODE (arg1) == CONJ_EXPR
11112 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11113 return fold_mult_zconjz (loc, type, arg0);
11115 if (flag_unsafe_math_optimizations)
11117 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11118 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11120 /* Optimizations of root(...)*root(...). */
11121 if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
11124 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11125 tree arg10 = CALL_EXPR_ARG (arg1, 0);
11127 /* Optimize sqrt(x)*sqrt(x) as x. */
11128 if (BUILTIN_SQRT_P (fcode0)
11129 && operand_equal_p (arg00, arg10, 0)
11130 && ! HONOR_SNANS (TYPE_MODE (type)))
11133 /* Optimize root(x)*root(y) as root(x*y). */
11134 rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11135 arg = fold_build2_loc (loc, MULT_EXPR, type, arg00, arg10);
11136 return build_call_expr_loc (loc, rootfn, 1, arg);
11139 /* Optimize expN(x)*expN(y) as expN(x+y). */
11140 if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
11142 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11143 tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
11144 CALL_EXPR_ARG (arg0, 0),
11145 CALL_EXPR_ARG (arg1, 0));
11146 return build_call_expr_loc (loc, expfn, 1, arg);
11149 /* Optimizations of pow(...)*pow(...). */
11150 if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
11151 || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
11152 || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
11154 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11155 tree arg01 = CALL_EXPR_ARG (arg0, 1);
11156 tree arg10 = CALL_EXPR_ARG (arg1, 0);
11157 tree arg11 = CALL_EXPR_ARG (arg1, 1);
11159 /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y). */
11160 if (operand_equal_p (arg01, arg11, 0))
11162 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11163 tree arg = fold_build2_loc (loc, MULT_EXPR, type,
11165 return build_call_expr_loc (loc, powfn, 2, arg, arg01);
11168 /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z). */
11169 if (operand_equal_p (arg00, arg10, 0))
11171 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11172 tree arg = fold_build2_loc (loc, PLUS_EXPR, type,
11174 return build_call_expr_loc (loc, powfn, 2, arg00, arg);
11178 /* Optimize tan(x)*cos(x) as sin(x). */
11179 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
11180 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
11181 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
11182 || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
11183 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
11184 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
11185 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11186 CALL_EXPR_ARG (arg1, 0), 0))
11188 tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
11190 if (sinfn != NULL_TREE)
11191 return build_call_expr_loc (loc, sinfn, 1,
11192 CALL_EXPR_ARG (arg0, 0));
11195 /* Optimize x*pow(x,c) as pow(x,c+1). */
11196 if (fcode1 == BUILT_IN_POW
11197 || fcode1 == BUILT_IN_POWF
11198 || fcode1 == BUILT_IN_POWL)
11200 tree arg10 = CALL_EXPR_ARG (arg1, 0);
11201 tree arg11 = CALL_EXPR_ARG (arg1, 1);
11202 if (TREE_CODE (arg11) == REAL_CST
11203 && !TREE_OVERFLOW (arg11)
11204 && operand_equal_p (arg0, arg10, 0))
11206 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11210 c = TREE_REAL_CST (arg11);
11211 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
11212 arg = build_real (type, c);
11213 return build_call_expr_loc (loc, powfn, 2, arg0, arg);
11217 /* Optimize pow(x,c)*x as pow(x,c+1). */
11218 if (fcode0 == BUILT_IN_POW
11219 || fcode0 == BUILT_IN_POWF
11220 || fcode0 == BUILT_IN_POWL)
11222 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11223 tree arg01 = CALL_EXPR_ARG (arg0, 1);
11224 if (TREE_CODE (arg01) == REAL_CST
11225 && !TREE_OVERFLOW (arg01)
11226 && operand_equal_p (arg1, arg00, 0))
11228 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11232 c = TREE_REAL_CST (arg01);
11233 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
11234 arg = build_real (type, c);
11235 return build_call_expr_loc (loc, powfn, 2, arg1, arg);
11239 /* Optimize x*x as pow(x,2.0), which is expanded as x*x. */
11240 if (optimize_function_for_speed_p (cfun)
11241 && operand_equal_p (arg0, arg1, 0))
11243 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
11247 tree arg = build_real (type, dconst2);
11248 return build_call_expr_loc (loc, powfn, 2, arg0, arg);
11257 if (integer_all_onesp (arg1))
11258 return omit_one_operand_loc (loc, type, arg1, arg0);
11259 if (integer_zerop (arg1))
11260 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11261 if (operand_equal_p (arg0, arg1, 0))
11262 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11264 /* ~X | X is -1. */
11265 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11266 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11268 t1 = fold_convert_loc (loc, type, integer_zero_node);
11269 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11270 return omit_one_operand_loc (loc, type, t1, arg1);
11273 /* X | ~X is -1. */
11274 if (TREE_CODE (arg1) == BIT_NOT_EXPR
11275 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11277 t1 = fold_convert_loc (loc, type, integer_zero_node);
11278 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11279 return omit_one_operand_loc (loc, type, t1, arg0);
11282 /* Canonicalize (X & C1) | C2. */
11283 if (TREE_CODE (arg0) == BIT_AND_EXPR
11284 && TREE_CODE (arg1) == INTEGER_CST
11285 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11287 unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, hi3, lo3, mlo, mhi;
11288 int width = TYPE_PRECISION (type), w;
11289 hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
11290 lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
11291 hi2 = TREE_INT_CST_HIGH (arg1);
11292 lo2 = TREE_INT_CST_LOW (arg1);
11294 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
11295 if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
11296 return omit_one_operand_loc (loc, type, arg1,
11297 TREE_OPERAND (arg0, 0));
11299 if (width > HOST_BITS_PER_WIDE_INT)
11301 mhi = (unsigned HOST_WIDE_INT) -1
11302 >> (2 * HOST_BITS_PER_WIDE_INT - width);
11308 mlo = (unsigned HOST_WIDE_INT) -1
11309 >> (HOST_BITS_PER_WIDE_INT - width);
11312 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
11313 if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
11314 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
11315 TREE_OPERAND (arg0, 0), arg1);
11317 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
11318 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
11319 mode which allows further optimizations. */
11326 for (w = BITS_PER_UNIT;
11327 w <= width && w <= HOST_BITS_PER_WIDE_INT;
11330 unsigned HOST_WIDE_INT mask
11331 = (unsigned HOST_WIDE_INT) -1 >> (HOST_BITS_PER_WIDE_INT - w);
11332 if (((lo1 | lo2) & mask) == mask
11333 && (lo1 & ~mask) == 0 && hi1 == 0)
11340 if (hi3 != hi1 || lo3 != lo1)
11341 return fold_build2_loc (loc, BIT_IOR_EXPR, type,
11342 fold_build2_loc (loc, BIT_AND_EXPR, type,
11343 TREE_OPERAND (arg0, 0),
11344 build_int_cst_wide (type,
11349 /* (X & Y) | Y is (X, Y). */
11350 if (TREE_CODE (arg0) == BIT_AND_EXPR
11351 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11352 return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
11353 /* (X & Y) | X is (Y, X). */
11354 if (TREE_CODE (arg0) == BIT_AND_EXPR
11355 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11356 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11357 return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
11358 /* X | (X & Y) is (Y, X). */
11359 if (TREE_CODE (arg1) == BIT_AND_EXPR
11360 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
11361 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
11362 return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
11363 /* X | (Y & X) is (Y, X). */
11364 if (TREE_CODE (arg1) == BIT_AND_EXPR
11365 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11366 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11367 return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
11369 t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
11370 if (t1 != NULL_TREE)
11373 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
11375 This results in more efficient code for machines without a NAND
11376 instruction. Combine will canonicalize to the first form
11377 which will allow use of NAND instructions provided by the
11378 backend if they exist. */
11379 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11380 && TREE_CODE (arg1) == BIT_NOT_EXPR)
11383 fold_build1_loc (loc, BIT_NOT_EXPR, type,
11384 build2 (BIT_AND_EXPR, type,
11385 fold_convert_loc (loc, type,
11386 TREE_OPERAND (arg0, 0)),
11387 fold_convert_loc (loc, type,
11388 TREE_OPERAND (arg1, 0))));
11391 /* See if this can be simplified into a rotate first. If that
11392 is unsuccessful continue in the association code. */
11396 if (integer_zerop (arg1))
11397 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11398 if (integer_all_onesp (arg1))
11399 return fold_build1_loc (loc, BIT_NOT_EXPR, type, op0);
11400 if (operand_equal_p (arg0, arg1, 0))
11401 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11403 /* ~X ^ X is -1. */
11404 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11405 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11407 t1 = fold_convert_loc (loc, type, integer_zero_node);
11408 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11409 return omit_one_operand_loc (loc, type, t1, arg1);
11412 /* X ^ ~X is -1. */
11413 if (TREE_CODE (arg1) == BIT_NOT_EXPR
11414 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11416 t1 = fold_convert_loc (loc, type, integer_zero_node);
11417 t1 = fold_unary_loc (loc, BIT_NOT_EXPR, type, t1);
11418 return omit_one_operand_loc (loc, type, t1, arg0);
11421 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
11422 with a constant, and the two constants have no bits in common,
11423 we should treat this as a BIT_IOR_EXPR since this may produce more
11424 simplifications. */
11425 if (TREE_CODE (arg0) == BIT_AND_EXPR
11426 && TREE_CODE (arg1) == BIT_AND_EXPR
11427 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11428 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
11429 && integer_zerop (const_binop (BIT_AND_EXPR,
11430 TREE_OPERAND (arg0, 1),
11431 TREE_OPERAND (arg1, 1), 0)))
11433 code = BIT_IOR_EXPR;
11437 /* (X | Y) ^ X -> Y & ~ X*/
11438 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11439 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11441 tree t2 = TREE_OPERAND (arg0, 1);
11442 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
11444 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11445 fold_convert_loc (loc, type, t2),
11446 fold_convert_loc (loc, type, t1));
11450 /* (Y | X) ^ X -> Y & ~ X*/
11451 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11452 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11454 tree t2 = TREE_OPERAND (arg0, 0);
11455 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1),
11457 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11458 fold_convert_loc (loc, type, t2),
11459 fold_convert_loc (loc, type, t1));
11463 /* X ^ (X | Y) -> Y & ~ X*/
11464 if (TREE_CODE (arg1) == BIT_IOR_EXPR
11465 && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
11467 tree t2 = TREE_OPERAND (arg1, 1);
11468 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
11470 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11471 fold_convert_loc (loc, type, t2),
11472 fold_convert_loc (loc, type, t1));
11476 /* X ^ (Y | X) -> Y & ~ X*/
11477 if (TREE_CODE (arg1) == BIT_IOR_EXPR
11478 && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
11480 tree t2 = TREE_OPERAND (arg1, 0);
11481 t1 = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg0),
11483 t1 = fold_build2_loc (loc, BIT_AND_EXPR, type,
11484 fold_convert_loc (loc, type, t2),
11485 fold_convert_loc (loc, type, t1));
11489 /* Convert ~X ^ ~Y to X ^ Y. */
11490 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11491 && TREE_CODE (arg1) == BIT_NOT_EXPR)
11492 return fold_build2_loc (loc, code, type,
11493 fold_convert_loc (loc, type,
11494 TREE_OPERAND (arg0, 0)),
11495 fold_convert_loc (loc, type,
11496 TREE_OPERAND (arg1, 0)));
11498 /* Convert ~X ^ C to X ^ ~C. */
11499 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11500 && TREE_CODE (arg1) == INTEGER_CST)
11501 return fold_build2_loc (loc, code, type,
11502 fold_convert_loc (loc, type,
11503 TREE_OPERAND (arg0, 0)),
11504 fold_build1_loc (loc, BIT_NOT_EXPR, type, arg1));
11506 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
11507 if (TREE_CODE (arg0) == BIT_AND_EXPR
11508 && integer_onep (TREE_OPERAND (arg0, 1))
11509 && integer_onep (arg1))
11510 return fold_build2_loc (loc, EQ_EXPR, type, arg0,
11511 build_int_cst (TREE_TYPE (arg0), 0));
11513 /* Fold (X & Y) ^ Y as ~X & Y. */
11514 if (TREE_CODE (arg0) == BIT_AND_EXPR
11515 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11517 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11518 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11519 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11520 fold_convert_loc (loc, type, arg1));
11522 /* Fold (X & Y) ^ X as ~Y & X. */
11523 if (TREE_CODE (arg0) == BIT_AND_EXPR
11524 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11525 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11527 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11528 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11529 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11530 fold_convert_loc (loc, type, arg1));
11532 /* Fold X ^ (X & Y) as X & ~Y. */
11533 if (TREE_CODE (arg1) == BIT_AND_EXPR
11534 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11536 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11537 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11538 fold_convert_loc (loc, type, arg0),
11539 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
11541 /* Fold X ^ (Y & X) as ~Y & X. */
11542 if (TREE_CODE (arg1) == BIT_AND_EXPR
11543 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11544 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11546 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11547 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11548 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11549 fold_convert_loc (loc, type, arg0));
11552 /* See if this can be simplified into a rotate first. If that
11553 is unsuccessful continue in the association code. */
11557 if (integer_all_onesp (arg1))
11558 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11559 if (integer_zerop (arg1))
11560 return omit_one_operand_loc (loc, type, arg1, arg0);
11561 if (operand_equal_p (arg0, arg1, 0))
11562 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11564 /* ~X & X is always zero. */
11565 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11566 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11567 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
11569 /* X & ~X is always zero. */
11570 if (TREE_CODE (arg1) == BIT_NOT_EXPR
11571 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11572 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
11574 /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2). */
11575 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11576 && TREE_CODE (arg1) == INTEGER_CST
11577 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11579 tree tmp1 = fold_convert_loc (loc, type, arg1);
11580 tree tmp2 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11581 tree tmp3 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11582 tmp2 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp2, tmp1);
11583 tmp3 = fold_build2_loc (loc, BIT_AND_EXPR, type, tmp3, tmp1);
11585 fold_convert_loc (loc, type,
11586 fold_build2_loc (loc, BIT_IOR_EXPR,
11587 type, tmp2, tmp3));
11590 /* (X | Y) & Y is (X, Y). */
11591 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11592 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11593 return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 0));
11594 /* (X | Y) & X is (Y, X). */
11595 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11596 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11597 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11598 return omit_one_operand_loc (loc, type, arg1, TREE_OPERAND (arg0, 1));
11599 /* X & (X | Y) is (Y, X). */
11600 if (TREE_CODE (arg1) == BIT_IOR_EXPR
11601 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
11602 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
11603 return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 1));
11604 /* X & (Y | X) is (Y, X). */
11605 if (TREE_CODE (arg1) == BIT_IOR_EXPR
11606 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11607 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11608 return omit_one_operand_loc (loc, type, arg0, TREE_OPERAND (arg1, 0));
11610 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
11611 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11612 && integer_onep (TREE_OPERAND (arg0, 1))
11613 && integer_onep (arg1))
11615 tem = TREE_OPERAND (arg0, 0);
11616 return fold_build2_loc (loc, EQ_EXPR, type,
11617 fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem,
11618 build_int_cst (TREE_TYPE (tem), 1)),
11619 build_int_cst (TREE_TYPE (tem), 0));
11621 /* Fold ~X & 1 as (X & 1) == 0. */
11622 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11623 && integer_onep (arg1))
11625 tem = TREE_OPERAND (arg0, 0);
11626 return fold_build2_loc (loc, EQ_EXPR, type,
11627 fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (tem), tem,
11628 build_int_cst (TREE_TYPE (tem), 1)),
11629 build_int_cst (TREE_TYPE (tem), 0));
11632 /* Fold (X ^ Y) & Y as ~X & Y. */
11633 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11634 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11636 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11637 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11638 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11639 fold_convert_loc (loc, type, arg1));
11641 /* Fold (X ^ Y) & X as ~Y & X. */
11642 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11643 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11644 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11646 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 1));
11647 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11648 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11649 fold_convert_loc (loc, type, arg1));
11651 /* Fold X & (X ^ Y) as X & ~Y. */
11652 if (TREE_CODE (arg1) == BIT_XOR_EXPR
11653 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11655 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 1));
11656 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11657 fold_convert_loc (loc, type, arg0),
11658 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem));
11660 /* Fold X & (Y ^ X) as ~Y & X. */
11661 if (TREE_CODE (arg1) == BIT_XOR_EXPR
11662 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11663 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11665 tem = fold_convert_loc (loc, type, TREE_OPERAND (arg1, 0));
11666 return fold_build2_loc (loc, BIT_AND_EXPR, type,
11667 fold_build1_loc (loc, BIT_NOT_EXPR, type, tem),
11668 fold_convert_loc (loc, type, arg0));
11671 t1 = distribute_bit_expr (loc, code, type, arg0, arg1);
11672 if (t1 != NULL_TREE)
11674 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11675 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11676 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11679 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11681 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
11682 && (~TREE_INT_CST_LOW (arg1)
11683 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
11685 fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
11688 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
11690 This results in more efficient code for machines without a NOR
11691 instruction. Combine will canonicalize to the first form
11692 which will allow use of NOR instructions provided by the
11693 backend if they exist. */
11694 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11695 && TREE_CODE (arg1) == BIT_NOT_EXPR)
11697 return fold_build1_loc (loc, BIT_NOT_EXPR, type,
11698 build2 (BIT_IOR_EXPR, type,
11699 fold_convert_loc (loc, type,
11700 TREE_OPERAND (arg0, 0)),
11701 fold_convert_loc (loc, type,
11702 TREE_OPERAND (arg1, 0))));
11705 /* If arg0 is derived from the address of an object or function, we may
11706 be able to fold this expression using the object or function's
11708 if (POINTER_TYPE_P (TREE_TYPE (arg0)) && host_integerp (arg1, 1))
11710 unsigned HOST_WIDE_INT modulus, residue;
11711 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg1);
11713 modulus = get_pointer_modulus_and_residue (arg0, &residue,
11714 integer_onep (arg1));
11716 /* This works because modulus is a power of 2. If this weren't the
11717 case, we'd have to replace it by its greatest power-of-2
11718 divisor: modulus & -modulus. */
11720 return build_int_cst (type, residue & low);
11723 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
11724 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
11725 if the new mask might be further optimized. */
11726 if ((TREE_CODE (arg0) == LSHIFT_EXPR
11727 || TREE_CODE (arg0) == RSHIFT_EXPR)
11728 && host_integerp (TREE_OPERAND (arg0, 1), 1)
11729 && host_integerp (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)))
11730 && tree_low_cst (TREE_OPERAND (arg0, 1), 1)
11731 < TYPE_PRECISION (TREE_TYPE (arg0))
11732 && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT
11733 && tree_low_cst (TREE_OPERAND (arg0, 1), 1) > 0)
11735 unsigned int shiftc = tree_low_cst (TREE_OPERAND (arg0, 1), 1);
11736 unsigned HOST_WIDE_INT mask
11737 = tree_low_cst (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)));
11738 unsigned HOST_WIDE_INT newmask, zerobits = 0;
11739 tree shift_type = TREE_TYPE (arg0);
11741 if (TREE_CODE (arg0) == LSHIFT_EXPR)
11742 zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
11743 else if (TREE_CODE (arg0) == RSHIFT_EXPR
11744 && TYPE_PRECISION (TREE_TYPE (arg0))
11745 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg0))))
11747 unsigned int prec = TYPE_PRECISION (TREE_TYPE (arg0));
11748 tree arg00 = TREE_OPERAND (arg0, 0);
11749 /* See if more bits can be proven as zero because of
11751 if (TREE_CODE (arg00) == NOP_EXPR
11752 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0))))
11754 tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0));
11755 if (TYPE_PRECISION (inner_type)
11756 == GET_MODE_BITSIZE (TYPE_MODE (inner_type))
11757 && TYPE_PRECISION (inner_type) < prec)
11759 prec = TYPE_PRECISION (inner_type);
11760 /* See if we can shorten the right shift. */
11762 shift_type = inner_type;
11765 zerobits = ~(unsigned HOST_WIDE_INT) 0;
11766 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
11767 zerobits <<= prec - shiftc;
11768 /* For arithmetic shift if sign bit could be set, zerobits
11769 can contain actually sign bits, so no transformation is
11770 possible, unless MASK masks them all away. In that
11771 case the shift needs to be converted into logical shift. */
11772 if (!TYPE_UNSIGNED (TREE_TYPE (arg0))
11773 && prec == TYPE_PRECISION (TREE_TYPE (arg0)))
11775 if ((mask & zerobits) == 0)
11776 shift_type = unsigned_type_for (TREE_TYPE (arg0));
11782 /* ((X << 16) & 0xff00) is (X, 0). */
11783 if ((mask & zerobits) == mask)
11784 return omit_one_operand_loc (loc, type,
11785 build_int_cst (type, 0), arg0);
11787 newmask = mask | zerobits;
11788 if (newmask != mask && (newmask & (newmask + 1)) == 0)
11792 /* Only do the transformation if NEWMASK is some integer
11794 for (prec = BITS_PER_UNIT;
11795 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
11796 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
11798 if (prec < HOST_BITS_PER_WIDE_INT
11799 || newmask == ~(unsigned HOST_WIDE_INT) 0)
11803 if (shift_type != TREE_TYPE (arg0))
11805 tem = fold_build2_loc (loc, TREE_CODE (arg0), shift_type,
11806 fold_convert_loc (loc, shift_type,
11807 TREE_OPERAND (arg0, 0)),
11808 TREE_OPERAND (arg0, 1));
11809 tem = fold_convert_loc (loc, type, tem);
11813 newmaskt = build_int_cst_type (TREE_TYPE (op1), newmask);
11814 if (!tree_int_cst_equal (newmaskt, arg1))
11815 return fold_build2_loc (loc, BIT_AND_EXPR, type, tem, newmaskt);
11823 /* Don't touch a floating-point divide by zero unless the mode
11824 of the constant can represent infinity. */
11825 if (TREE_CODE (arg1) == REAL_CST
11826 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11827 && real_zerop (arg1))
11830 /* Optimize A / A to 1.0 if we don't care about
11831 NaNs or Infinities. Skip the transformation
11832 for non-real operands. */
11833 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
11834 && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11835 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
11836 && operand_equal_p (arg0, arg1, 0))
11838 tree r = build_real (TREE_TYPE (arg0), dconst1);
11840 return omit_two_operands_loc (loc, type, r, arg0, arg1);
11843 /* The complex version of the above A / A optimization. */
11844 if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11845 && operand_equal_p (arg0, arg1, 0))
11847 tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
11848 if (! HONOR_NANS (TYPE_MODE (elem_type))
11849 && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
11851 tree r = build_real (elem_type, dconst1);
11852 /* omit_two_operands will call fold_convert for us. */
11853 return omit_two_operands_loc (loc, type, r, arg0, arg1);
11857 /* (-A) / (-B) -> A / B */
11858 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11859 return fold_build2_loc (loc, RDIV_EXPR, type,
11860 TREE_OPERAND (arg0, 0),
11861 negate_expr (arg1));
11862 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11863 return fold_build2_loc (loc, RDIV_EXPR, type,
11864 negate_expr (arg0),
11865 TREE_OPERAND (arg1, 0));
11867 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
11868 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11869 && real_onep (arg1))
11870 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
11872 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
11873 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11874 && real_minus_onep (arg1))
11875 return non_lvalue_loc (loc, fold_convert_loc (loc, type,
11876 negate_expr (arg0)));
11878 /* If ARG1 is a constant, we can convert this to a multiply by the
11879 reciprocal. This does not have the same rounding properties,
11880 so only do this if -freciprocal-math. We can actually
11881 always safely do it if ARG1 is a power of two, but it's hard to
11882 tell if it is or not in a portable manner. */
11883 if (TREE_CODE (arg1) == REAL_CST)
11885 if (flag_reciprocal_math
11886 && 0 != (tem = const_binop (code, build_real (type, dconst1),
11888 return fold_build2_loc (loc, MULT_EXPR, type, arg0, tem);
11889 /* Find the reciprocal if optimizing and the result is exact. */
11893 r = TREE_REAL_CST (arg1);
11894 if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
11896 tem = build_real (type, r);
11897 return fold_build2_loc (loc, MULT_EXPR, type,
11898 fold_convert_loc (loc, type, arg0), tem);
11902 /* Convert A/B/C to A/(B*C). */
11903 if (flag_reciprocal_math
11904 && TREE_CODE (arg0) == RDIV_EXPR)
11905 return fold_build2_loc (loc, RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
11906 fold_build2_loc (loc, MULT_EXPR, type,
11907 TREE_OPERAND (arg0, 1), arg1));
11909 /* Convert A/(B/C) to (A/B)*C. */
11910 if (flag_reciprocal_math
11911 && TREE_CODE (arg1) == RDIV_EXPR)
11912 return fold_build2_loc (loc, MULT_EXPR, type,
11913 fold_build2_loc (loc, RDIV_EXPR, type, arg0,
11914 TREE_OPERAND (arg1, 0)),
11915 TREE_OPERAND (arg1, 1));
11917 /* Convert C1/(X*C2) into (C1/C2)/X. */
11918 if (flag_reciprocal_math
11919 && TREE_CODE (arg1) == MULT_EXPR
11920 && TREE_CODE (arg0) == REAL_CST
11921 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
11923 tree tem = const_binop (RDIV_EXPR, arg0,
11924 TREE_OPERAND (arg1, 1), 0);
11926 return fold_build2_loc (loc, RDIV_EXPR, type, tem,
11927 TREE_OPERAND (arg1, 0));
11930 if (flag_unsafe_math_optimizations)
11932 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11933 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11935 /* Optimize sin(x)/cos(x) as tan(x). */
11936 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
11937 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
11938 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
11939 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11940 CALL_EXPR_ARG (arg1, 0), 0))
11942 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11944 if (tanfn != NULL_TREE)
11945 return build_call_expr_loc (loc, tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11948 /* Optimize cos(x)/sin(x) as 1.0/tan(x). */
11949 if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11950 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11951 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
11952 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11953 CALL_EXPR_ARG (arg1, 0), 0))
11955 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11957 if (tanfn != NULL_TREE)
11959 tree tmp = build_call_expr_loc (loc, tanfn, 1,
11960 CALL_EXPR_ARG (arg0, 0));
11961 return fold_build2_loc (loc, RDIV_EXPR, type,
11962 build_real (type, dconst1), tmp);
11966 /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11967 NaNs or Infinities. */
11968 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11969 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11970 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11972 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11973 tree arg01 = CALL_EXPR_ARG (arg1, 0);
11975 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11976 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11977 && operand_equal_p (arg00, arg01, 0))
11979 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11981 if (cosfn != NULL_TREE)
11982 return build_call_expr_loc (loc, cosfn, 1, arg00);
11986 /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11987 NaNs or Infinities. */
11988 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11989 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11990 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11992 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11993 tree arg01 = CALL_EXPR_ARG (arg1, 0);
11995 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11996 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11997 && operand_equal_p (arg00, arg01, 0))
11999 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
12001 if (cosfn != NULL_TREE)
12003 tree tmp = build_call_expr_loc (loc, cosfn, 1, arg00);
12004 return fold_build2_loc (loc, RDIV_EXPR, type,
12005 build_real (type, dconst1),
12011 /* Optimize pow(x,c)/x as pow(x,c-1). */
12012 if (fcode0 == BUILT_IN_POW
12013 || fcode0 == BUILT_IN_POWF
12014 || fcode0 == BUILT_IN_POWL)
12016 tree arg00 = CALL_EXPR_ARG (arg0, 0);
12017 tree arg01 = CALL_EXPR_ARG (arg0, 1);
12018 if (TREE_CODE (arg01) == REAL_CST
12019 && !TREE_OVERFLOW (arg01)
12020 && operand_equal_p (arg1, arg00, 0))
12022 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
12026 c = TREE_REAL_CST (arg01);
12027 real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
12028 arg = build_real (type, c);
12029 return build_call_expr_loc (loc, powfn, 2, arg1, arg);
12033 /* Optimize a/root(b/c) into a*root(c/b). */
12034 if (BUILTIN_ROOT_P (fcode1))
12036 tree rootarg = CALL_EXPR_ARG (arg1, 0);
12038 if (TREE_CODE (rootarg) == RDIV_EXPR)
12040 tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
12041 tree b = TREE_OPERAND (rootarg, 0);
12042 tree c = TREE_OPERAND (rootarg, 1);
12044 tree tmp = fold_build2_loc (loc, RDIV_EXPR, type, c, b);
12046 tmp = build_call_expr_loc (loc, rootfn, 1, tmp);
12047 return fold_build2_loc (loc, MULT_EXPR, type, arg0, tmp);
12051 /* Optimize x/expN(y) into x*expN(-y). */
12052 if (BUILTIN_EXPONENT_P (fcode1))
12054 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
12055 tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
12056 arg1 = build_call_expr_loc (loc,
12058 fold_convert_loc (loc, type, arg));
12059 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
12062 /* Optimize x/pow(y,z) into x*pow(y,-z). */
12063 if (fcode1 == BUILT_IN_POW
12064 || fcode1 == BUILT_IN_POWF
12065 || fcode1 == BUILT_IN_POWL)
12067 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
12068 tree arg10 = CALL_EXPR_ARG (arg1, 0);
12069 tree arg11 = CALL_EXPR_ARG (arg1, 1);
12070 tree neg11 = fold_convert_loc (loc, type,
12071 negate_expr (arg11));
12072 arg1 = build_call_expr_loc (loc, powfn, 2, arg10, neg11);
12073 return fold_build2_loc (loc, MULT_EXPR, type, arg0, arg1);
12078 case TRUNC_DIV_EXPR:
12079 case FLOOR_DIV_EXPR:
12080 /* Simplify A / (B << N) where A and B are positive and B is
12081 a power of 2, to A >> (N + log2(B)). */
12082 strict_overflow_p = false;
12083 if (TREE_CODE (arg1) == LSHIFT_EXPR
12084 && (TYPE_UNSIGNED (type)
12085 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
12087 tree sval = TREE_OPERAND (arg1, 0);
12088 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
12090 tree sh_cnt = TREE_OPERAND (arg1, 1);
12091 unsigned long pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
12093 if (strict_overflow_p)
12094 fold_overflow_warning (("assuming signed overflow does not "
12095 "occur when simplifying A / (B << N)"),
12096 WARN_STRICT_OVERFLOW_MISC);
12098 sh_cnt = fold_build2_loc (loc, PLUS_EXPR, TREE_TYPE (sh_cnt),
12099 sh_cnt, build_int_cst (NULL_TREE, pow2));
12100 return fold_build2_loc (loc, RSHIFT_EXPR, type,
12101 fold_convert_loc (loc, type, arg0), sh_cnt);
12105 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
12106 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
12107 if (INTEGRAL_TYPE_P (type)
12108 && TYPE_UNSIGNED (type)
12109 && code == FLOOR_DIV_EXPR)
12110 return fold_build2_loc (loc, TRUNC_DIV_EXPR, type, op0, op1);
12114 case ROUND_DIV_EXPR:
12115 case CEIL_DIV_EXPR:
12116 case EXACT_DIV_EXPR:
12117 if (integer_onep (arg1))
12118 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12119 if (integer_zerop (arg1))
12121 /* X / -1 is -X. */
12122 if (!TYPE_UNSIGNED (type)
12123 && TREE_CODE (arg1) == INTEGER_CST
12124 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
12125 && TREE_INT_CST_HIGH (arg1) == -1)
12126 return fold_convert_loc (loc, type, negate_expr (arg0));
12128 /* Convert -A / -B to A / B when the type is signed and overflow is
12130 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12131 && TREE_CODE (arg0) == NEGATE_EXPR
12132 && negate_expr_p (arg1))
12134 if (INTEGRAL_TYPE_P (type))
12135 fold_overflow_warning (("assuming signed overflow does not occur "
12136 "when distributing negation across "
12138 WARN_STRICT_OVERFLOW_MISC);
12139 return fold_build2_loc (loc, code, type,
12140 fold_convert_loc (loc, type,
12141 TREE_OPERAND (arg0, 0)),
12142 fold_convert_loc (loc, type,
12143 negate_expr (arg1)));
12145 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
12146 && TREE_CODE (arg1) == NEGATE_EXPR
12147 && negate_expr_p (arg0))
12149 if (INTEGRAL_TYPE_P (type))
12150 fold_overflow_warning (("assuming signed overflow does not occur "
12151 "when distributing negation across "
12153 WARN_STRICT_OVERFLOW_MISC);
12154 return fold_build2_loc (loc, code, type,
12155 fold_convert_loc (loc, type,
12156 negate_expr (arg0)),
12157 fold_convert_loc (loc, type,
12158 TREE_OPERAND (arg1, 0)));
12161 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
12162 operation, EXACT_DIV_EXPR.
12164 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
12165 At one time others generated faster code, it's not clear if they do
12166 after the last round to changes to the DIV code in expmed.c. */
12167 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
12168 && multiple_of_p (type, arg0, arg1))
12169 return fold_build2_loc (loc, EXACT_DIV_EXPR, type, arg0, arg1);
12171 strict_overflow_p = false;
12172 if (TREE_CODE (arg1) == INTEGER_CST
12173 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
12174 &strict_overflow_p)))
12176 if (strict_overflow_p)
12177 fold_overflow_warning (("assuming signed overflow does not occur "
12178 "when simplifying division"),
12179 WARN_STRICT_OVERFLOW_MISC);
12180 return fold_convert_loc (loc, type, tem);
12185 case CEIL_MOD_EXPR:
12186 case FLOOR_MOD_EXPR:
12187 case ROUND_MOD_EXPR:
12188 case TRUNC_MOD_EXPR:
12189 /* X % 1 is always zero, but be sure to preserve any side
12191 if (integer_onep (arg1))
12192 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12194 /* X % 0, return X % 0 unchanged so that we can get the
12195 proper warnings and errors. */
12196 if (integer_zerop (arg1))
12199 /* 0 % X is always zero, but be sure to preserve any side
12200 effects in X. Place this after checking for X == 0. */
12201 if (integer_zerop (arg0))
12202 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
12204 /* X % -1 is zero. */
12205 if (!TYPE_UNSIGNED (type)
12206 && TREE_CODE (arg1) == INTEGER_CST
12207 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
12208 && TREE_INT_CST_HIGH (arg1) == -1)
12209 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12211 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
12212 i.e. "X % C" into "X & (C - 1)", if X and C are positive. */
12213 strict_overflow_p = false;
12214 if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
12215 && (TYPE_UNSIGNED (type)
12216 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
12219 /* Also optimize A % (C << N) where C is a power of 2,
12220 to A & ((C << N) - 1). */
12221 if (TREE_CODE (arg1) == LSHIFT_EXPR)
12222 c = TREE_OPERAND (arg1, 0);
12224 if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
12226 tree mask = fold_build2_loc (loc, MINUS_EXPR, TREE_TYPE (arg1), arg1,
12227 build_int_cst (TREE_TYPE (arg1), 1));
12228 if (strict_overflow_p)
12229 fold_overflow_warning (("assuming signed overflow does not "
12230 "occur when simplifying "
12231 "X % (power of two)"),
12232 WARN_STRICT_OVERFLOW_MISC);
12233 return fold_build2_loc (loc, BIT_AND_EXPR, type,
12234 fold_convert_loc (loc, type, arg0),
12235 fold_convert_loc (loc, type, mask));
12239 /* X % -C is the same as X % C. */
12240 if (code == TRUNC_MOD_EXPR
12241 && !TYPE_UNSIGNED (type)
12242 && TREE_CODE (arg1) == INTEGER_CST
12243 && !TREE_OVERFLOW (arg1)
12244 && TREE_INT_CST_HIGH (arg1) < 0
12245 && !TYPE_OVERFLOW_TRAPS (type)
12246 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
12247 && !sign_bit_p (arg1, arg1))
12248 return fold_build2_loc (loc, code, type,
12249 fold_convert_loc (loc, type, arg0),
12250 fold_convert_loc (loc, type,
12251 negate_expr (arg1)));
12253 /* X % -Y is the same as X % Y. */
12254 if (code == TRUNC_MOD_EXPR
12255 && !TYPE_UNSIGNED (type)
12256 && TREE_CODE (arg1) == NEGATE_EXPR
12257 && !TYPE_OVERFLOW_TRAPS (type))
12258 return fold_build2_loc (loc, code, type, fold_convert_loc (loc, type, arg0),
12259 fold_convert_loc (loc, type,
12260 TREE_OPERAND (arg1, 0)));
12262 if (TREE_CODE (arg1) == INTEGER_CST
12263 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
12264 &strict_overflow_p)))
12266 if (strict_overflow_p)
12267 fold_overflow_warning (("assuming signed overflow does not occur "
12268 "when simplifying modulus"),
12269 WARN_STRICT_OVERFLOW_MISC);
12270 return fold_convert_loc (loc, type, tem);
12277 if (integer_all_onesp (arg0))
12278 return omit_one_operand_loc (loc, type, arg0, arg1);
12282 /* Optimize -1 >> x for arithmetic right shifts. */
12283 if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type)
12284 && tree_expr_nonnegative_p (arg1))
12285 return omit_one_operand_loc (loc, type, arg0, arg1);
12286 /* ... fall through ... */
12290 if (integer_zerop (arg1))
12291 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12292 if (integer_zerop (arg0))
12293 return omit_one_operand_loc (loc, type, arg0, arg1);
12295 /* Since negative shift count is not well-defined,
12296 don't try to compute it in the compiler. */
12297 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
12300 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
12301 if (TREE_CODE (op0) == code && host_integerp (arg1, false)
12302 && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
12303 && host_integerp (TREE_OPERAND (arg0, 1), false)
12304 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
12306 HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
12307 + TREE_INT_CST_LOW (arg1));
12309 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
12310 being well defined. */
12311 if (low >= TYPE_PRECISION (type))
12313 if (code == LROTATE_EXPR || code == RROTATE_EXPR)
12314 low = low % TYPE_PRECISION (type);
12315 else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
12316 return omit_one_operand_loc (loc, type, build_int_cst (type, 0),
12317 TREE_OPERAND (arg0, 0));
12319 low = TYPE_PRECISION (type) - 1;
12322 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12323 build_int_cst (type, low));
12326 /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
12327 into x & ((unsigned)-1 >> c) for unsigned types. */
12328 if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
12329 || (TYPE_UNSIGNED (type)
12330 && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
12331 && host_integerp (arg1, false)
12332 && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
12333 && host_integerp (TREE_OPERAND (arg0, 1), false)
12334 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
12336 HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
12337 HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
12343 arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
12345 lshift = build_int_cst (type, -1);
12346 lshift = int_const_binop (code, lshift, arg1, 0);
12348 return fold_build2_loc (loc, BIT_AND_EXPR, type, arg00, lshift);
12352 /* Rewrite an LROTATE_EXPR by a constant into an
12353 RROTATE_EXPR by a new constant. */
12354 if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
12356 tree tem = build_int_cst (TREE_TYPE (arg1),
12357 TYPE_PRECISION (type));
12358 tem = const_binop (MINUS_EXPR, tem, arg1, 0);
12359 return fold_build2_loc (loc, RROTATE_EXPR, type, op0, tem);
12362 /* If we have a rotate of a bit operation with the rotate count and
12363 the second operand of the bit operation both constant,
12364 permute the two operations. */
12365 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
12366 && (TREE_CODE (arg0) == BIT_AND_EXPR
12367 || TREE_CODE (arg0) == BIT_IOR_EXPR
12368 || TREE_CODE (arg0) == BIT_XOR_EXPR)
12369 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12370 return fold_build2_loc (loc, TREE_CODE (arg0), type,
12371 fold_build2_loc (loc, code, type,
12372 TREE_OPERAND (arg0, 0), arg1),
12373 fold_build2_loc (loc, code, type,
12374 TREE_OPERAND (arg0, 1), arg1));
12376 /* Two consecutive rotates adding up to the precision of the
12377 type can be ignored. */
12378 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
12379 && TREE_CODE (arg0) == RROTATE_EXPR
12380 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12381 && TREE_INT_CST_HIGH (arg1) == 0
12382 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
12383 && ((TREE_INT_CST_LOW (arg1)
12384 + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
12385 == (unsigned int) TYPE_PRECISION (type)))
12386 return TREE_OPERAND (arg0, 0);
12388 /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
12389 (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
12390 if the latter can be further optimized. */
12391 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
12392 && TREE_CODE (arg0) == BIT_AND_EXPR
12393 && TREE_CODE (arg1) == INTEGER_CST
12394 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12396 tree mask = fold_build2_loc (loc, code, type,
12397 fold_convert_loc (loc, type,
12398 TREE_OPERAND (arg0, 1)),
12400 tree shift = fold_build2_loc (loc, code, type,
12401 fold_convert_loc (loc, type,
12402 TREE_OPERAND (arg0, 0)),
12404 tem = fold_binary_loc (loc, BIT_AND_EXPR, type, shift, mask);
12412 if (operand_equal_p (arg0, arg1, 0))
12413 return omit_one_operand_loc (loc, type, arg0, arg1);
12414 if (INTEGRAL_TYPE_P (type)
12415 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
12416 return omit_one_operand_loc (loc, type, arg1, arg0);
12417 tem = fold_minmax (loc, MIN_EXPR, type, arg0, arg1);
12423 if (operand_equal_p (arg0, arg1, 0))
12424 return omit_one_operand_loc (loc, type, arg0, arg1);
12425 if (INTEGRAL_TYPE_P (type)
12426 && TYPE_MAX_VALUE (type)
12427 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
12428 return omit_one_operand_loc (loc, type, arg1, arg0);
12429 tem = fold_minmax (loc, MAX_EXPR, type, arg0, arg1);
12434 case TRUTH_ANDIF_EXPR:
12435 /* Note that the operands of this must be ints
12436 and their values must be 0 or 1.
12437 ("true" is a fixed value perhaps depending on the language.) */
12438 /* If first arg is constant zero, return it. */
12439 if (integer_zerop (arg0))
12440 return fold_convert_loc (loc, type, arg0);
12441 case TRUTH_AND_EXPR:
12442 /* If either arg is constant true, drop it. */
12443 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12444 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12445 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
12446 /* Preserve sequence points. */
12447 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12448 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12449 /* If second arg is constant zero, result is zero, but first arg
12450 must be evaluated. */
12451 if (integer_zerop (arg1))
12452 return omit_one_operand_loc (loc, type, arg1, arg0);
12453 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
12454 case will be handled here. */
12455 if (integer_zerop (arg0))
12456 return omit_one_operand_loc (loc, type, arg0, arg1);
12458 /* !X && X is always false. */
12459 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12460 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12461 return omit_one_operand_loc (loc, type, integer_zero_node, arg1);
12462 /* X && !X is always false. */
12463 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12464 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12465 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12467 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
12468 means A >= Y && A != MAX, but in this case we know that
12471 if (!TREE_SIDE_EFFECTS (arg0)
12472 && !TREE_SIDE_EFFECTS (arg1))
12474 tem = fold_to_nonsharp_ineq_using_bound (loc, arg0, arg1);
12475 if (tem && !operand_equal_p (tem, arg0, 0))
12476 return fold_build2_loc (loc, code, type, tem, arg1);
12478 tem = fold_to_nonsharp_ineq_using_bound (loc, arg1, arg0);
12479 if (tem && !operand_equal_p (tem, arg1, 0))
12480 return fold_build2_loc (loc, code, type, arg0, tem);
12484 /* We only do these simplifications if we are optimizing. */
12488 /* Check for things like (A || B) && (A || C). We can convert this
12489 to A || (B && C). Note that either operator can be any of the four
12490 truth and/or operations and the transformation will still be
12491 valid. Also note that we only care about order for the
12492 ANDIF and ORIF operators. If B contains side effects, this
12493 might change the truth-value of A. */
12494 if (TREE_CODE (arg0) == TREE_CODE (arg1)
12495 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
12496 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
12497 || TREE_CODE (arg0) == TRUTH_AND_EXPR
12498 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
12499 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
12501 tree a00 = TREE_OPERAND (arg0, 0);
12502 tree a01 = TREE_OPERAND (arg0, 1);
12503 tree a10 = TREE_OPERAND (arg1, 0);
12504 tree a11 = TREE_OPERAND (arg1, 1);
12505 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
12506 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
12507 && (code == TRUTH_AND_EXPR
12508 || code == TRUTH_OR_EXPR));
12510 if (operand_equal_p (a00, a10, 0))
12511 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
12512 fold_build2_loc (loc, code, type, a01, a11));
12513 else if (commutative && operand_equal_p (a00, a11, 0))
12514 return fold_build2_loc (loc, TREE_CODE (arg0), type, a00,
12515 fold_build2_loc (loc, code, type, a01, a10));
12516 else if (commutative && operand_equal_p (a01, a10, 0))
12517 return fold_build2_loc (loc, TREE_CODE (arg0), type, a01,
12518 fold_build2_loc (loc, code, type, a00, a11));
12520 /* This case if tricky because we must either have commutative
12521 operators or else A10 must not have side-effects. */
12523 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
12524 && operand_equal_p (a01, a11, 0))
12525 return fold_build2_loc (loc, TREE_CODE (arg0), type,
12526 fold_build2_loc (loc, code, type, a00, a10),
12530 /* See if we can build a range comparison. */
12531 if (0 != (tem = fold_range_test (loc, code, type, op0, op1)))
12534 /* Check for the possibility of merging component references. If our
12535 lhs is another similar operation, try to merge its rhs with our
12536 rhs. Then try to merge our lhs and rhs. */
12537 if (TREE_CODE (arg0) == code
12538 && 0 != (tem = fold_truthop (loc, code, type,
12539 TREE_OPERAND (arg0, 1), arg1)))
12540 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12542 if ((tem = fold_truthop (loc, code, type, arg0, arg1)) != 0)
12547 case TRUTH_ORIF_EXPR:
12548 /* Note that the operands of this must be ints
12549 and their values must be 0 or true.
12550 ("true" is a fixed value perhaps depending on the language.) */
12551 /* If first arg is constant true, return it. */
12552 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12553 return fold_convert_loc (loc, type, arg0);
12554 case TRUTH_OR_EXPR:
12555 /* If either arg is constant zero, drop it. */
12556 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12557 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg1));
12558 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12559 /* Preserve sequence points. */
12560 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12561 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12562 /* If second arg is constant true, result is true, but we must
12563 evaluate first arg. */
12564 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12565 return omit_one_operand_loc (loc, type, arg1, arg0);
12566 /* Likewise for first arg, but note this only occurs here for
12568 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12569 return omit_one_operand_loc (loc, type, arg0, arg1);
12571 /* !X || X is always true. */
12572 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12573 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12574 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12575 /* X || !X is always true. */
12576 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12577 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12578 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12582 case TRUTH_XOR_EXPR:
12583 /* If the second arg is constant zero, drop it. */
12584 if (integer_zerop (arg1))
12585 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12586 /* If the second arg is constant true, this is a logical inversion. */
12587 if (integer_onep (arg1))
12589 /* Only call invert_truthvalue if operand is a truth value. */
12590 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
12591 tem = fold_build1_loc (loc, TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
12593 tem = invert_truthvalue_loc (loc, arg0);
12594 return non_lvalue_loc (loc, fold_convert_loc (loc, type, tem));
12596 /* Identical arguments cancel to zero. */
12597 if (operand_equal_p (arg0, arg1, 0))
12598 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
12600 /* !X ^ X is always true. */
12601 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12602 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12603 return omit_one_operand_loc (loc, type, integer_one_node, arg1);
12605 /* X ^ !X is always true. */
12606 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12607 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12608 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
12614 tem = fold_comparison (loc, code, type, op0, op1);
12615 if (tem != NULL_TREE)
12618 /* bool_var != 0 becomes bool_var. */
12619 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12620 && code == NE_EXPR)
12621 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12623 /* bool_var == 1 becomes bool_var. */
12624 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12625 && code == EQ_EXPR)
12626 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12628 /* bool_var != 1 becomes !bool_var. */
12629 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12630 && code == NE_EXPR)
12631 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
12632 fold_convert_loc (loc, type, arg0));
12634 /* bool_var == 0 becomes !bool_var. */
12635 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12636 && code == EQ_EXPR)
12637 return fold_build1_loc (loc, TRUTH_NOT_EXPR, type,
12638 fold_convert_loc (loc, type, arg0));
12640 /* !exp != 0 becomes !exp */
12641 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR && integer_zerop (arg1)
12642 && code == NE_EXPR)
12643 return non_lvalue_loc (loc, fold_convert_loc (loc, type, arg0));
12645 /* If this is an equality comparison of the address of two non-weak,
12646 unaliased symbols neither of which are extern (since we do not
12647 have access to attributes for externs), then we know the result. */
12648 if (TREE_CODE (arg0) == ADDR_EXPR
12649 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
12650 && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
12651 && ! lookup_attribute ("alias",
12652 DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
12653 && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
12654 && TREE_CODE (arg1) == ADDR_EXPR
12655 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
12656 && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
12657 && ! lookup_attribute ("alias",
12658 DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
12659 && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
12661 /* We know that we're looking at the address of two
12662 non-weak, unaliased, static _DECL nodes.
12664 It is both wasteful and incorrect to call operand_equal_p
12665 to compare the two ADDR_EXPR nodes. It is wasteful in that
12666 all we need to do is test pointer equality for the arguments
12667 to the two ADDR_EXPR nodes. It is incorrect to use
12668 operand_equal_p as that function is NOT equivalent to a
12669 C equality test. It can in fact return false for two
12670 objects which would test as equal using the C equality
12672 bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
12673 return constant_boolean_node (equal
12674 ? code == EQ_EXPR : code != EQ_EXPR,
12678 /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
12679 a MINUS_EXPR of a constant, we can convert it into a comparison with
12680 a revised constant as long as no overflow occurs. */
12681 if (TREE_CODE (arg1) == INTEGER_CST
12682 && (TREE_CODE (arg0) == PLUS_EXPR
12683 || TREE_CODE (arg0) == MINUS_EXPR)
12684 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12685 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
12686 ? MINUS_EXPR : PLUS_EXPR,
12687 fold_convert_loc (loc, TREE_TYPE (arg0),
12689 TREE_OPERAND (arg0, 1), 0))
12690 && !TREE_OVERFLOW (tem))
12691 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12693 /* Similarly for a NEGATE_EXPR. */
12694 if (TREE_CODE (arg0) == NEGATE_EXPR
12695 && TREE_CODE (arg1) == INTEGER_CST
12696 && 0 != (tem = negate_expr (arg1))
12697 && TREE_CODE (tem) == INTEGER_CST
12698 && !TREE_OVERFLOW (tem))
12699 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), tem);
12701 /* Similarly for a BIT_XOR_EXPR; X ^ C1 == C2 is X == (C1 ^ C2). */
12702 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12703 && TREE_CODE (arg1) == INTEGER_CST
12704 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12705 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12706 fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg0),
12707 fold_convert_loc (loc,
12710 TREE_OPERAND (arg0, 1)));
12712 /* Transform comparisons of the form X +- Y CMP X to Y CMP 0. */
12713 if ((TREE_CODE (arg0) == PLUS_EXPR
12714 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
12715 || TREE_CODE (arg0) == MINUS_EXPR)
12716 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12717 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12718 || POINTER_TYPE_P (TREE_TYPE (arg0))))
12720 tree val = TREE_OPERAND (arg0, 1);
12721 return omit_two_operands_loc (loc, type,
12722 fold_build2_loc (loc, code, type,
12724 build_int_cst (TREE_TYPE (val),
12726 TREE_OPERAND (arg0, 0), arg1);
12729 /* Transform comparisons of the form C - X CMP X if C % 2 == 1. */
12730 if (TREE_CODE (arg0) == MINUS_EXPR
12731 && TREE_CODE (TREE_OPERAND (arg0, 0)) == INTEGER_CST
12732 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0)
12733 && (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 0)) & 1) == 1)
12735 return omit_two_operands_loc (loc, type,
12737 ? boolean_true_node : boolean_false_node,
12738 TREE_OPERAND (arg0, 1), arg1);
12741 /* If we have X - Y == 0, we can convert that to X == Y and similarly
12742 for !=. Don't do this for ordered comparisons due to overflow. */
12743 if (TREE_CODE (arg0) == MINUS_EXPR
12744 && integer_zerop (arg1))
12745 return fold_build2_loc (loc, code, type,
12746 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
12748 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
12749 if (TREE_CODE (arg0) == ABS_EXPR
12750 && (integer_zerop (arg1) || real_zerop (arg1)))
12751 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0), arg1);
12753 /* If this is an EQ or NE comparison with zero and ARG0 is
12754 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12755 two operations, but the latter can be done in one less insn
12756 on machines that have only two-operand insns or on which a
12757 constant cannot be the first operand. */
12758 if (TREE_CODE (arg0) == BIT_AND_EXPR
12759 && integer_zerop (arg1))
12761 tree arg00 = TREE_OPERAND (arg0, 0);
12762 tree arg01 = TREE_OPERAND (arg0, 1);
12763 if (TREE_CODE (arg00) == LSHIFT_EXPR
12764 && integer_onep (TREE_OPERAND (arg00, 0)))
12766 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg00),
12767 arg01, TREE_OPERAND (arg00, 1));
12768 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12769 build_int_cst (TREE_TYPE (arg0), 1));
12770 return fold_build2_loc (loc, code, type,
12771 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12774 else if (TREE_CODE (arg01) == LSHIFT_EXPR
12775 && integer_onep (TREE_OPERAND (arg01, 0)))
12777 tree tem = fold_build2_loc (loc, RSHIFT_EXPR, TREE_TYPE (arg01),
12778 arg00, TREE_OPERAND (arg01, 1));
12779 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12780 build_int_cst (TREE_TYPE (arg0), 1));
12781 return fold_build2_loc (loc, code, type,
12782 fold_convert_loc (loc, TREE_TYPE (arg1), tem),
12787 /* If this is an NE or EQ comparison of zero against the result of a
12788 signed MOD operation whose second operand is a power of 2, make
12789 the MOD operation unsigned since it is simpler and equivalent. */
12790 if (integer_zerop (arg1)
12791 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
12792 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
12793 || TREE_CODE (arg0) == CEIL_MOD_EXPR
12794 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
12795 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
12796 && integer_pow2p (TREE_OPERAND (arg0, 1)))
12798 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
12799 tree newmod = fold_build2_loc (loc, TREE_CODE (arg0), newtype,
12800 fold_convert_loc (loc, newtype,
12801 TREE_OPERAND (arg0, 0)),
12802 fold_convert_loc (loc, newtype,
12803 TREE_OPERAND (arg0, 1)));
12805 return fold_build2_loc (loc, code, type, newmod,
12806 fold_convert_loc (loc, newtype, arg1));
12809 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
12810 C1 is a valid shift constant, and C2 is a power of two, i.e.
12812 if (TREE_CODE (arg0) == BIT_AND_EXPR
12813 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
12814 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
12816 && integer_pow2p (TREE_OPERAND (arg0, 1))
12817 && integer_zerop (arg1))
12819 tree itype = TREE_TYPE (arg0);
12820 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
12821 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
12823 /* Check for a valid shift count. */
12824 if (TREE_INT_CST_HIGH (arg001) == 0
12825 && TREE_INT_CST_LOW (arg001) < prec)
12827 tree arg01 = TREE_OPERAND (arg0, 1);
12828 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12829 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
12830 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
12831 can be rewritten as (X & (C2 << C1)) != 0. */
12832 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
12834 tem = fold_build2_loc (loc, LSHIFT_EXPR, itype, arg01, arg001);
12835 tem = fold_build2_loc (loc, BIT_AND_EXPR, itype, arg000, tem);
12836 return fold_build2_loc (loc, code, type, tem, arg1);
12838 /* Otherwise, for signed (arithmetic) shifts,
12839 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
12840 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
12841 else if (!TYPE_UNSIGNED (itype))
12842 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
12843 arg000, build_int_cst (itype, 0));
12844 /* Otherwise, of unsigned (logical) shifts,
12845 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
12846 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
12848 return omit_one_operand_loc (loc, type,
12849 code == EQ_EXPR ? integer_one_node
12850 : integer_zero_node,
12855 /* If this is an NE comparison of zero with an AND of one, remove the
12856 comparison since the AND will give the correct value. */
12857 if (code == NE_EXPR
12858 && integer_zerop (arg1)
12859 && TREE_CODE (arg0) == BIT_AND_EXPR
12860 && integer_onep (TREE_OPERAND (arg0, 1)))
12861 return fold_convert_loc (loc, type, arg0);
12863 /* If we have (A & C) == C where C is a power of 2, convert this into
12864 (A & C) != 0. Similarly for NE_EXPR. */
12865 if (TREE_CODE (arg0) == BIT_AND_EXPR
12866 && integer_pow2p (TREE_OPERAND (arg0, 1))
12867 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12868 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12869 arg0, fold_convert_loc (loc, TREE_TYPE (arg0),
12870 integer_zero_node));
12872 /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
12873 bit, then fold the expression into A < 0 or A >= 0. */
12874 tem = fold_single_bit_test_into_sign_test (loc, code, arg0, arg1, type);
12878 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
12879 Similarly for NE_EXPR. */
12880 if (TREE_CODE (arg0) == BIT_AND_EXPR
12881 && TREE_CODE (arg1) == INTEGER_CST
12882 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12884 tree notc = fold_build1_loc (loc, BIT_NOT_EXPR,
12885 TREE_TYPE (TREE_OPERAND (arg0, 1)),
12886 TREE_OPERAND (arg0, 1));
12887 tree dandnotc = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12889 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12890 if (integer_nonzerop (dandnotc))
12891 return omit_one_operand_loc (loc, type, rslt, arg0);
12894 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
12895 Similarly for NE_EXPR. */
12896 if (TREE_CODE (arg0) == BIT_IOR_EXPR
12897 && TREE_CODE (arg1) == INTEGER_CST
12898 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12900 tree notd = fold_build1_loc (loc, BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
12901 tree candnotd = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
12902 TREE_OPERAND (arg0, 1), notd);
12903 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12904 if (integer_nonzerop (candnotd))
12905 return omit_one_operand_loc (loc, type, rslt, arg0);
12908 /* If this is a comparison of a field, we may be able to simplify it. */
12909 if ((TREE_CODE (arg0) == COMPONENT_REF
12910 || TREE_CODE (arg0) == BIT_FIELD_REF)
12911 /* Handle the constant case even without -O
12912 to make sure the warnings are given. */
12913 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12915 t1 = optimize_bit_field_compare (loc, code, type, arg0, arg1);
12920 /* Optimize comparisons of strlen vs zero to a compare of the
12921 first character of the string vs zero. To wit,
12922 strlen(ptr) == 0 => *ptr == 0
12923 strlen(ptr) != 0 => *ptr != 0
12924 Other cases should reduce to one of these two (or a constant)
12925 due to the return value of strlen being unsigned. */
12926 if (TREE_CODE (arg0) == CALL_EXPR
12927 && integer_zerop (arg1))
12929 tree fndecl = get_callee_fndecl (arg0);
12932 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
12933 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
12934 && call_expr_nargs (arg0) == 1
12935 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
12937 tree iref = build_fold_indirect_ref_loc (loc,
12938 CALL_EXPR_ARG (arg0, 0));
12939 return fold_build2_loc (loc, code, type, iref,
12940 build_int_cst (TREE_TYPE (iref), 0));
12944 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12945 of X. Similarly fold (X >> C) == 0 into X >= 0. */
12946 if (TREE_CODE (arg0) == RSHIFT_EXPR
12947 && integer_zerop (arg1)
12948 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12950 tree arg00 = TREE_OPERAND (arg0, 0);
12951 tree arg01 = TREE_OPERAND (arg0, 1);
12952 tree itype = TREE_TYPE (arg00);
12953 if (TREE_INT_CST_HIGH (arg01) == 0
12954 && TREE_INT_CST_LOW (arg01)
12955 == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
12957 if (TYPE_UNSIGNED (itype))
12959 itype = signed_type_for (itype);
12960 arg00 = fold_convert_loc (loc, itype, arg00);
12962 return fold_build2_loc (loc, code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12963 type, arg00, build_int_cst (itype, 0));
12967 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
12968 if (integer_zerop (arg1)
12969 && TREE_CODE (arg0) == BIT_XOR_EXPR)
12970 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12971 TREE_OPERAND (arg0, 1));
12973 /* (X ^ Y) == Y becomes X == 0. We know that Y has no side-effects. */
12974 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12975 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12976 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12977 build_int_cst (TREE_TYPE (arg1), 0));
12978 /* Likewise (X ^ Y) == X becomes Y == 0. X has no side-effects. */
12979 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12980 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12981 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
12982 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 1),
12983 build_int_cst (TREE_TYPE (arg1), 0));
12985 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
12986 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12987 && TREE_CODE (arg1) == INTEGER_CST
12988 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12989 return fold_build2_loc (loc, code, type, TREE_OPERAND (arg0, 0),
12990 fold_build2_loc (loc, BIT_XOR_EXPR, TREE_TYPE (arg1),
12991 TREE_OPERAND (arg0, 1), arg1));
12993 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12994 (X & C) == 0 when C is a single bit. */
12995 if (TREE_CODE (arg0) == BIT_AND_EXPR
12996 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12997 && integer_zerop (arg1)
12998 && integer_pow2p (TREE_OPERAND (arg0, 1)))
13000 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg0),
13001 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
13002 TREE_OPERAND (arg0, 1));
13003 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
13007 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
13008 constant C is a power of two, i.e. a single bit. */
13009 if (TREE_CODE (arg0) == BIT_XOR_EXPR
13010 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13011 && integer_zerop (arg1)
13012 && integer_pow2p (TREE_OPERAND (arg0, 1))
13013 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13014 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
13016 tree arg00 = TREE_OPERAND (arg0, 0);
13017 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
13018 arg00, build_int_cst (TREE_TYPE (arg00), 0));
13021 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
13022 when is C is a power of two, i.e. a single bit. */
13023 if (TREE_CODE (arg0) == BIT_AND_EXPR
13024 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
13025 && integer_zerop (arg1)
13026 && integer_pow2p (TREE_OPERAND (arg0, 1))
13027 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13028 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
13030 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
13031 tem = fold_build2_loc (loc, BIT_AND_EXPR, TREE_TYPE (arg000),
13032 arg000, TREE_OPERAND (arg0, 1));
13033 return fold_build2_loc (loc, code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
13034 tem, build_int_cst (TREE_TYPE (tem), 0));
13037 if (integer_zerop (arg1)
13038 && tree_expr_nonzero_p (arg0))
13040 tree res = constant_boolean_node (code==NE_EXPR, type);
13041 return omit_one_operand_loc (loc, type, res, arg0);
13044 /* Fold -X op -Y as X op Y, where op is eq/ne. */
13045 if (TREE_CODE (arg0) == NEGATE_EXPR
13046 && TREE_CODE (arg1) == NEGATE_EXPR)
13047 return fold_build2_loc (loc, code, type,
13048 TREE_OPERAND (arg0, 0),
13049 TREE_OPERAND (arg1, 0));
13051 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
13052 if (TREE_CODE (arg0) == BIT_AND_EXPR
13053 && TREE_CODE (arg1) == BIT_AND_EXPR)
13055 tree arg00 = TREE_OPERAND (arg0, 0);
13056 tree arg01 = TREE_OPERAND (arg0, 1);
13057 tree arg10 = TREE_OPERAND (arg1, 0);
13058 tree arg11 = TREE_OPERAND (arg1, 1);
13059 tree itype = TREE_TYPE (arg0);
13061 if (operand_equal_p (arg01, arg11, 0))
13062 return fold_build2_loc (loc, code, type,
13063 fold_build2_loc (loc, BIT_AND_EXPR, itype,
13064 fold_build2_loc (loc,
13065 BIT_XOR_EXPR, itype,
13068 build_int_cst (itype, 0));
13070 if (operand_equal_p (arg01, arg10, 0))
13071 return fold_build2_loc (loc, code, type,
13072 fold_build2_loc (loc, BIT_AND_EXPR, itype,
13073 fold_build2_loc (loc,
13074 BIT_XOR_EXPR, itype,
13077 build_int_cst (itype, 0));
13079 if (operand_equal_p (arg00, arg11, 0))
13080 return fold_build2_loc (loc, code, type,
13081 fold_build2_loc (loc, BIT_AND_EXPR, itype,
13082 fold_build2_loc (loc,
13083 BIT_XOR_EXPR, itype,
13086 build_int_cst (itype, 0));
13088 if (operand_equal_p (arg00, arg10, 0))
13089 return fold_build2_loc (loc, code, type,
13090 fold_build2_loc (loc, BIT_AND_EXPR, itype,
13091 fold_build2_loc (loc,
13092 BIT_XOR_EXPR, itype,
13095 build_int_cst (itype, 0));
13098 if (TREE_CODE (arg0) == BIT_XOR_EXPR
13099 && TREE_CODE (arg1) == BIT_XOR_EXPR)
13101 tree arg00 = TREE_OPERAND (arg0, 0);
13102 tree arg01 = TREE_OPERAND (arg0, 1);
13103 tree arg10 = TREE_OPERAND (arg1, 0);
13104 tree arg11 = TREE_OPERAND (arg1, 1);
13105 tree itype = TREE_TYPE (arg0);
13107 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
13108 operand_equal_p guarantees no side-effects so we don't need
13109 to use omit_one_operand on Z. */
13110 if (operand_equal_p (arg01, arg11, 0))
13111 return fold_build2_loc (loc, code, type, arg00, arg10);
13112 if (operand_equal_p (arg01, arg10, 0))
13113 return fold_build2_loc (loc, code, type, arg00, arg11);
13114 if (operand_equal_p (arg00, arg11, 0))
13115 return fold_build2_loc (loc, code, type, arg01, arg10);
13116 if (operand_equal_p (arg00, arg10, 0))
13117 return fold_build2_loc (loc, code, type, arg01, arg11);
13119 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
13120 if (TREE_CODE (arg01) == INTEGER_CST
13121 && TREE_CODE (arg11) == INTEGER_CST)
13122 return fold_build2_loc (loc, code, type,
13123 fold_build2_loc (loc, BIT_XOR_EXPR, itype, arg00,
13124 fold_build2_loc (loc,
13125 BIT_XOR_EXPR, itype,
13130 /* Attempt to simplify equality/inequality comparisons of complex
13131 values. Only lower the comparison if the result is known or
13132 can be simplified to a single scalar comparison. */
13133 if ((TREE_CODE (arg0) == COMPLEX_EXPR
13134 || TREE_CODE (arg0) == COMPLEX_CST)
13135 && (TREE_CODE (arg1) == COMPLEX_EXPR
13136 || TREE_CODE (arg1) == COMPLEX_CST))
13138 tree real0, imag0, real1, imag1;
13141 if (TREE_CODE (arg0) == COMPLEX_EXPR)
13143 real0 = TREE_OPERAND (arg0, 0);
13144 imag0 = TREE_OPERAND (arg0, 1);
13148 real0 = TREE_REALPART (arg0);
13149 imag0 = TREE_IMAGPART (arg0);
13152 if (TREE_CODE (arg1) == COMPLEX_EXPR)
13154 real1 = TREE_OPERAND (arg1, 0);
13155 imag1 = TREE_OPERAND (arg1, 1);
13159 real1 = TREE_REALPART (arg1);
13160 imag1 = TREE_IMAGPART (arg1);
13163 rcond = fold_binary_loc (loc, code, type, real0, real1);
13164 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
13166 if (integer_zerop (rcond))
13168 if (code == EQ_EXPR)
13169 return omit_two_operands_loc (loc, type, boolean_false_node,
13171 return fold_build2_loc (loc, NE_EXPR, type, imag0, imag1);
13175 if (code == NE_EXPR)
13176 return omit_two_operands_loc (loc, type, boolean_true_node,
13178 return fold_build2_loc (loc, EQ_EXPR, type, imag0, imag1);
13182 icond = fold_binary_loc (loc, code, type, imag0, imag1);
13183 if (icond && TREE_CODE (icond) == INTEGER_CST)
13185 if (integer_zerop (icond))
13187 if (code == EQ_EXPR)
13188 return omit_two_operands_loc (loc, type, boolean_false_node,
13190 return fold_build2_loc (loc, NE_EXPR, type, real0, real1);
13194 if (code == NE_EXPR)
13195 return omit_two_operands_loc (loc, type, boolean_true_node,
13197 return fold_build2_loc (loc, EQ_EXPR, type, real0, real1);
13208 tem = fold_comparison (loc, code, type, op0, op1);
13209 if (tem != NULL_TREE)
13212 /* Transform comparisons of the form X +- C CMP X. */
13213 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
13214 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
13215 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
13216 && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
13217 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
13218 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
13220 tree arg01 = TREE_OPERAND (arg0, 1);
13221 enum tree_code code0 = TREE_CODE (arg0);
13224 if (TREE_CODE (arg01) == REAL_CST)
13225 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
13227 is_positive = tree_int_cst_sgn (arg01);
13229 /* (X - c) > X becomes false. */
13230 if (code == GT_EXPR
13231 && ((code0 == MINUS_EXPR && is_positive >= 0)
13232 || (code0 == PLUS_EXPR && is_positive <= 0)))
13234 if (TREE_CODE (arg01) == INTEGER_CST
13235 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13236 fold_overflow_warning (("assuming signed overflow does not "
13237 "occur when assuming that (X - c) > X "
13238 "is always false"),
13239 WARN_STRICT_OVERFLOW_ALL);
13240 return constant_boolean_node (0, type);
13243 /* Likewise (X + c) < X becomes false. */
13244 if (code == LT_EXPR
13245 && ((code0 == PLUS_EXPR && is_positive >= 0)
13246 || (code0 == MINUS_EXPR && is_positive <= 0)))
13248 if (TREE_CODE (arg01) == INTEGER_CST
13249 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13250 fold_overflow_warning (("assuming signed overflow does not "
13251 "occur when assuming that "
13252 "(X + c) < X is always false"),
13253 WARN_STRICT_OVERFLOW_ALL);
13254 return constant_boolean_node (0, type);
13257 /* Convert (X - c) <= X to true. */
13258 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
13260 && ((code0 == MINUS_EXPR && is_positive >= 0)
13261 || (code0 == PLUS_EXPR && is_positive <= 0)))
13263 if (TREE_CODE (arg01) == INTEGER_CST
13264 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13265 fold_overflow_warning (("assuming signed overflow does not "
13266 "occur when assuming that "
13267 "(X - c) <= X is always true"),
13268 WARN_STRICT_OVERFLOW_ALL);
13269 return constant_boolean_node (1, type);
13272 /* Convert (X + c) >= X to true. */
13273 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
13275 && ((code0 == PLUS_EXPR && is_positive >= 0)
13276 || (code0 == MINUS_EXPR && is_positive <= 0)))
13278 if (TREE_CODE (arg01) == INTEGER_CST
13279 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13280 fold_overflow_warning (("assuming signed overflow does not "
13281 "occur when assuming that "
13282 "(X + c) >= X is always true"),
13283 WARN_STRICT_OVERFLOW_ALL);
13284 return constant_boolean_node (1, type);
13287 if (TREE_CODE (arg01) == INTEGER_CST)
13289 /* Convert X + c > X and X - c < X to true for integers. */
13290 if (code == GT_EXPR
13291 && ((code0 == PLUS_EXPR && is_positive > 0)
13292 || (code0 == MINUS_EXPR && is_positive < 0)))
13294 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13295 fold_overflow_warning (("assuming signed overflow does "
13296 "not occur when assuming that "
13297 "(X + c) > X is always true"),
13298 WARN_STRICT_OVERFLOW_ALL);
13299 return constant_boolean_node (1, type);
13302 if (code == LT_EXPR
13303 && ((code0 == MINUS_EXPR && is_positive > 0)
13304 || (code0 == PLUS_EXPR && is_positive < 0)))
13306 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13307 fold_overflow_warning (("assuming signed overflow does "
13308 "not occur when assuming that "
13309 "(X - c) < X is always true"),
13310 WARN_STRICT_OVERFLOW_ALL);
13311 return constant_boolean_node (1, type);
13314 /* Convert X + c <= X and X - c >= X to false for integers. */
13315 if (code == LE_EXPR
13316 && ((code0 == PLUS_EXPR && is_positive > 0)
13317 || (code0 == MINUS_EXPR && is_positive < 0)))
13319 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13320 fold_overflow_warning (("assuming signed overflow does "
13321 "not occur when assuming that "
13322 "(X + c) <= X is always false"),
13323 WARN_STRICT_OVERFLOW_ALL);
13324 return constant_boolean_node (0, type);
13327 if (code == GE_EXPR
13328 && ((code0 == MINUS_EXPR && is_positive > 0)
13329 || (code0 == PLUS_EXPR && is_positive < 0)))
13331 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
13332 fold_overflow_warning (("assuming signed overflow does "
13333 "not occur when assuming that "
13334 "(X - c) >= X is always false"),
13335 WARN_STRICT_OVERFLOW_ALL);
13336 return constant_boolean_node (0, type);
13341 /* Comparisons with the highest or lowest possible integer of
13342 the specified precision will have known values. */
13344 tree arg1_type = TREE_TYPE (arg1);
13345 unsigned int width = TYPE_PRECISION (arg1_type);
13347 if (TREE_CODE (arg1) == INTEGER_CST
13348 && width <= 2 * HOST_BITS_PER_WIDE_INT
13349 && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
13351 HOST_WIDE_INT signed_max_hi;
13352 unsigned HOST_WIDE_INT signed_max_lo;
13353 unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
13355 if (width <= HOST_BITS_PER_WIDE_INT)
13357 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
13362 if (TYPE_UNSIGNED (arg1_type))
13364 max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
13370 max_lo = signed_max_lo;
13371 min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
13377 width -= HOST_BITS_PER_WIDE_INT;
13378 signed_max_lo = -1;
13379 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
13384 if (TYPE_UNSIGNED (arg1_type))
13386 max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
13391 max_hi = signed_max_hi;
13392 min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
13396 if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
13397 && TREE_INT_CST_LOW (arg1) == max_lo)
13401 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13404 return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
13407 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13410 return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
13412 /* The GE_EXPR and LT_EXPR cases above are not normally
13413 reached because of previous transformations. */
13418 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
13420 && TREE_INT_CST_LOW (arg1) == max_lo - 1)
13424 arg1 = const_binop (PLUS_EXPR, arg1,
13425 build_int_cst (TREE_TYPE (arg1), 1), 0);
13426 return fold_build2_loc (loc, EQ_EXPR, type,
13427 fold_convert_loc (loc,
13428 TREE_TYPE (arg1), arg0),
13431 arg1 = const_binop (PLUS_EXPR, arg1,
13432 build_int_cst (TREE_TYPE (arg1), 1), 0);
13433 return fold_build2_loc (loc, NE_EXPR, type,
13434 fold_convert_loc (loc, TREE_TYPE (arg1),
13440 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
13442 && TREE_INT_CST_LOW (arg1) == min_lo)
13446 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13449 return fold_build2_loc (loc, EQ_EXPR, type, op0, op1);
13452 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13455 return fold_build2_loc (loc, NE_EXPR, type, op0, op1);
13460 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
13462 && TREE_INT_CST_LOW (arg1) == min_lo + 1)
13466 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
13467 return fold_build2_loc (loc, NE_EXPR, type,
13468 fold_convert_loc (loc,
13469 TREE_TYPE (arg1), arg0),
13472 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
13473 return fold_build2_loc (loc, EQ_EXPR, type,
13474 fold_convert_loc (loc, TREE_TYPE (arg1),
13481 else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
13482 && TREE_INT_CST_LOW (arg1) == signed_max_lo
13483 && TYPE_UNSIGNED (arg1_type)
13484 /* We will flip the signedness of the comparison operator
13485 associated with the mode of arg1, so the sign bit is
13486 specified by this mode. Check that arg1 is the signed
13487 max associated with this sign bit. */
13488 && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
13489 /* signed_type does not work on pointer types. */
13490 && INTEGRAL_TYPE_P (arg1_type))
13492 /* The following case also applies to X < signed_max+1
13493 and X >= signed_max+1 because previous transformations. */
13494 if (code == LE_EXPR || code == GT_EXPR)
13497 st = signed_type_for (TREE_TYPE (arg1));
13498 return fold_build2_loc (loc,
13499 code == LE_EXPR ? GE_EXPR : LT_EXPR,
13500 type, fold_convert_loc (loc, st, arg0),
13501 build_int_cst (st, 0));
13507 /* If we are comparing an ABS_EXPR with a constant, we can
13508 convert all the cases into explicit comparisons, but they may
13509 well not be faster than doing the ABS and one comparison.
13510 But ABS (X) <= C is a range comparison, which becomes a subtraction
13511 and a comparison, and is probably faster. */
13512 if (code == LE_EXPR
13513 && TREE_CODE (arg1) == INTEGER_CST
13514 && TREE_CODE (arg0) == ABS_EXPR
13515 && ! TREE_SIDE_EFFECTS (arg0)
13516 && (0 != (tem = negate_expr (arg1)))
13517 && TREE_CODE (tem) == INTEGER_CST
13518 && !TREE_OVERFLOW (tem))
13519 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13520 build2 (GE_EXPR, type,
13521 TREE_OPERAND (arg0, 0), tem),
13522 build2 (LE_EXPR, type,
13523 TREE_OPERAND (arg0, 0), arg1));
13525 /* Convert ABS_EXPR<x> >= 0 to true. */
13526 strict_overflow_p = false;
13527 if (code == GE_EXPR
13528 && (integer_zerop (arg1)
13529 || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
13530 && real_zerop (arg1)))
13531 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13533 if (strict_overflow_p)
13534 fold_overflow_warning (("assuming signed overflow does not occur "
13535 "when simplifying comparison of "
13536 "absolute value and zero"),
13537 WARN_STRICT_OVERFLOW_CONDITIONAL);
13538 return omit_one_operand_loc (loc, type, integer_one_node, arg0);
13541 /* Convert ABS_EXPR<x> < 0 to false. */
13542 strict_overflow_p = false;
13543 if (code == LT_EXPR
13544 && (integer_zerop (arg1) || real_zerop (arg1))
13545 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13547 if (strict_overflow_p)
13548 fold_overflow_warning (("assuming signed overflow does not occur "
13549 "when simplifying comparison of "
13550 "absolute value and zero"),
13551 WARN_STRICT_OVERFLOW_CONDITIONAL);
13552 return omit_one_operand_loc (loc, type, integer_zero_node, arg0);
13555 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
13556 and similarly for >= into !=. */
13557 if ((code == LT_EXPR || code == GE_EXPR)
13558 && TYPE_UNSIGNED (TREE_TYPE (arg0))
13559 && TREE_CODE (arg1) == LSHIFT_EXPR
13560 && integer_onep (TREE_OPERAND (arg1, 0)))
13562 tem = build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13563 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13564 TREE_OPERAND (arg1, 1)),
13565 build_int_cst (TREE_TYPE (arg0), 0));
13566 goto fold_binary_exit;
13569 if ((code == LT_EXPR || code == GE_EXPR)
13570 && TYPE_UNSIGNED (TREE_TYPE (arg0))
13571 && CONVERT_EXPR_P (arg1)
13572 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
13573 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
13575 tem = build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13576 fold_convert_loc (loc, TREE_TYPE (arg0),
13577 build2 (RSHIFT_EXPR,
13578 TREE_TYPE (arg0), arg0,
13579 TREE_OPERAND (TREE_OPERAND (arg1, 0),
13581 build_int_cst (TREE_TYPE (arg0), 0));
13582 goto fold_binary_exit;
13587 case UNORDERED_EXPR:
13595 if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
13597 t1 = fold_relational_const (code, type, arg0, arg1);
13598 if (t1 != NULL_TREE)
13602 /* If the first operand is NaN, the result is constant. */
13603 if (TREE_CODE (arg0) == REAL_CST
13604 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
13605 && (code != LTGT_EXPR || ! flag_trapping_math))
13607 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13608 ? integer_zero_node
13609 : integer_one_node;
13610 return omit_one_operand_loc (loc, type, t1, arg1);
13613 /* If the second operand is NaN, the result is constant. */
13614 if (TREE_CODE (arg1) == REAL_CST
13615 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
13616 && (code != LTGT_EXPR || ! flag_trapping_math))
13618 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13619 ? integer_zero_node
13620 : integer_one_node;
13621 return omit_one_operand_loc (loc, type, t1, arg0);
13624 /* Simplify unordered comparison of something with itself. */
13625 if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
13626 && operand_equal_p (arg0, arg1, 0))
13627 return constant_boolean_node (1, type);
13629 if (code == LTGT_EXPR
13630 && !flag_trapping_math
13631 && operand_equal_p (arg0, arg1, 0))
13632 return constant_boolean_node (0, type);
13634 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
13636 tree targ0 = strip_float_extensions (arg0);
13637 tree targ1 = strip_float_extensions (arg1);
13638 tree newtype = TREE_TYPE (targ0);
13640 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
13641 newtype = TREE_TYPE (targ1);
13643 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
13644 return fold_build2_loc (loc, code, type,
13645 fold_convert_loc (loc, newtype, targ0),
13646 fold_convert_loc (loc, newtype, targ1));
13651 case COMPOUND_EXPR:
13652 /* When pedantic, a compound expression can be neither an lvalue
13653 nor an integer constant expression. */
13654 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
13656 /* Don't let (0, 0) be null pointer constant. */
13657 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
13658 : fold_convert_loc (loc, type, arg1);
13659 return pedantic_non_lvalue_loc (loc, tem);
13662 if ((TREE_CODE (arg0) == REAL_CST
13663 && TREE_CODE (arg1) == REAL_CST)
13664 || (TREE_CODE (arg0) == INTEGER_CST
13665 && TREE_CODE (arg1) == INTEGER_CST))
13666 return build_complex (type, arg0, arg1);
13670 /* An ASSERT_EXPR should never be passed to fold_binary. */
13671 gcc_unreachable ();
13675 } /* switch (code) */
13677 protected_set_expr_location (tem, loc);
13681 /* Callback for walk_tree, looking for LABEL_EXPR. Return *TP if it is
13682 a LABEL_EXPR; otherwise return NULL_TREE. Do not check the subtrees
13686 contains_label_1 (tree *tp, int *walk_subtrees, void *data ATTRIBUTE_UNUSED)
13688 switch (TREE_CODE (*tp))
13694 *walk_subtrees = 0;
13696 /* ... fall through ... */
13703 /* Return whether the sub-tree ST contains a label which is accessible from
13704 outside the sub-tree. */
13707 contains_label_p (tree st)
13710 (walk_tree_without_duplicates (&st, contains_label_1 , NULL) != NULL_TREE);
13713 /* Fold a ternary expression of code CODE and type TYPE with operands
13714 OP0, OP1, and OP2. Return the folded expression if folding is
13715 successful. Otherwise, return NULL_TREE. */
13718 fold_ternary_loc (location_t loc, enum tree_code code, tree type,
13719 tree op0, tree op1, tree op2)
13722 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
13723 enum tree_code_class kind = TREE_CODE_CLASS (code);
13725 gcc_assert (IS_EXPR_CODE_CLASS (kind)
13726 && TREE_CODE_LENGTH (code) == 3);
13728 /* Strip any conversions that don't change the mode. This is safe
13729 for every expression, except for a comparison expression because
13730 its signedness is derived from its operands. So, in the latter
13731 case, only strip conversions that don't change the signedness.
13733 Note that this is done as an internal manipulation within the
13734 constant folder, in order to find the simplest representation of
13735 the arguments so that their form can be studied. In any cases,
13736 the appropriate type conversions should be put back in the tree
13737 that will get out of the constant folder. */
13752 case COMPONENT_REF:
13753 if (TREE_CODE (arg0) == CONSTRUCTOR
13754 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
13756 unsigned HOST_WIDE_INT idx;
13758 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
13765 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
13766 so all simple results must be passed through pedantic_non_lvalue. */
13767 if (TREE_CODE (arg0) == INTEGER_CST)
13769 tree unused_op = integer_zerop (arg0) ? op1 : op2;
13770 tem = integer_zerop (arg0) ? op2 : op1;
13771 /* Only optimize constant conditions when the selected branch
13772 has the same type as the COND_EXPR. This avoids optimizing
13773 away "c ? x : throw", where the throw has a void type.
13774 Avoid throwing away that operand which contains label. */
13775 if ((!TREE_SIDE_EFFECTS (unused_op)
13776 || !contains_label_p (unused_op))
13777 && (! VOID_TYPE_P (TREE_TYPE (tem))
13778 || VOID_TYPE_P (type)))
13779 return pedantic_non_lvalue_loc (loc, tem);
13782 if (operand_equal_p (arg1, op2, 0))
13783 return pedantic_omit_one_operand_loc (loc, type, arg1, arg0);
13785 /* If we have A op B ? A : C, we may be able to convert this to a
13786 simpler expression, depending on the operation and the values
13787 of B and C. Signed zeros prevent all of these transformations,
13788 for reasons given above each one.
13790 Also try swapping the arguments and inverting the conditional. */
13791 if (COMPARISON_CLASS_P (arg0)
13792 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13793 arg1, TREE_OPERAND (arg0, 1))
13794 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
13796 tem = fold_cond_expr_with_comparison (loc, type, arg0, op1, op2);
13801 if (COMPARISON_CLASS_P (arg0)
13802 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13804 TREE_OPERAND (arg0, 1))
13805 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
13807 tem = fold_truth_not_expr (loc, arg0);
13808 if (tem && COMPARISON_CLASS_P (tem))
13810 tem = fold_cond_expr_with_comparison (loc, type, tem, op2, op1);
13816 /* If the second operand is simpler than the third, swap them
13817 since that produces better jump optimization results. */
13818 if (truth_value_p (TREE_CODE (arg0))
13819 && tree_swap_operands_p (op1, op2, false))
13821 /* See if this can be inverted. If it can't, possibly because
13822 it was a floating-point inequality comparison, don't do
13824 tem = fold_truth_not_expr (loc, arg0);
13826 return fold_build3_loc (loc, code, type, tem, op2, op1);
13829 /* Convert A ? 1 : 0 to simply A. */
13830 if (integer_onep (op1)
13831 && integer_zerop (op2)
13832 /* If we try to convert OP0 to our type, the
13833 call to fold will try to move the conversion inside
13834 a COND, which will recurse. In that case, the COND_EXPR
13835 is probably the best choice, so leave it alone. */
13836 && type == TREE_TYPE (arg0))
13837 return pedantic_non_lvalue_loc (loc, arg0);
13839 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
13840 over COND_EXPR in cases such as floating point comparisons. */
13841 if (integer_zerop (op1)
13842 && integer_onep (op2)
13843 && truth_value_p (TREE_CODE (arg0)))
13844 return pedantic_non_lvalue_loc (loc,
13845 fold_convert_loc (loc, type,
13846 invert_truthvalue_loc (loc,
13849 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
13850 if (TREE_CODE (arg0) == LT_EXPR
13851 && integer_zerop (TREE_OPERAND (arg0, 1))
13852 && integer_zerop (op2)
13853 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13855 /* sign_bit_p only checks ARG1 bits within A's precision.
13856 If <sign bit of A> has wider type than A, bits outside
13857 of A's precision in <sign bit of A> need to be checked.
13858 If they are all 0, this optimization needs to be done
13859 in unsigned A's type, if they are all 1 in signed A's type,
13860 otherwise this can't be done. */
13861 if (TYPE_PRECISION (TREE_TYPE (tem))
13862 < TYPE_PRECISION (TREE_TYPE (arg1))
13863 && TYPE_PRECISION (TREE_TYPE (tem))
13864 < TYPE_PRECISION (type))
13866 unsigned HOST_WIDE_INT mask_lo;
13867 HOST_WIDE_INT mask_hi;
13868 int inner_width, outer_width;
13871 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13872 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13873 if (outer_width > TYPE_PRECISION (type))
13874 outer_width = TYPE_PRECISION (type);
13876 if (outer_width > HOST_BITS_PER_WIDE_INT)
13878 mask_hi = ((unsigned HOST_WIDE_INT) -1
13879 >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
13885 mask_lo = ((unsigned HOST_WIDE_INT) -1
13886 >> (HOST_BITS_PER_WIDE_INT - outer_width));
13888 if (inner_width > HOST_BITS_PER_WIDE_INT)
13890 mask_hi &= ~((unsigned HOST_WIDE_INT) -1
13891 >> (HOST_BITS_PER_WIDE_INT - inner_width));
13895 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
13896 >> (HOST_BITS_PER_WIDE_INT - inner_width));
13898 if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
13899 && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
13901 tem_type = signed_type_for (TREE_TYPE (tem));
13902 tem = fold_convert_loc (loc, tem_type, tem);
13904 else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
13905 && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
13907 tem_type = unsigned_type_for (TREE_TYPE (tem));
13908 tem = fold_convert_loc (loc, tem_type, tem);
13916 fold_convert_loc (loc, type,
13917 fold_build2_loc (loc, BIT_AND_EXPR,
13918 TREE_TYPE (tem), tem,
13919 fold_convert_loc (loc,
13924 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
13925 already handled above. */
13926 if (TREE_CODE (arg0) == BIT_AND_EXPR
13927 && integer_onep (TREE_OPERAND (arg0, 1))
13928 && integer_zerop (op2)
13929 && integer_pow2p (arg1))
13931 tree tem = TREE_OPERAND (arg0, 0);
13933 if (TREE_CODE (tem) == RSHIFT_EXPR
13934 && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
13935 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
13936 TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
13937 return fold_build2_loc (loc, BIT_AND_EXPR, type,
13938 TREE_OPERAND (tem, 0), arg1);
13941 /* A & N ? N : 0 is simply A & N if N is a power of two. This
13942 is probably obsolete because the first operand should be a
13943 truth value (that's why we have the two cases above), but let's
13944 leave it in until we can confirm this for all front-ends. */
13945 if (integer_zerop (op2)
13946 && TREE_CODE (arg0) == NE_EXPR
13947 && integer_zerop (TREE_OPERAND (arg0, 1))
13948 && integer_pow2p (arg1)
13949 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13950 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13951 arg1, OEP_ONLY_CONST))
13952 return pedantic_non_lvalue_loc (loc,
13953 fold_convert_loc (loc, type,
13954 TREE_OPERAND (arg0, 0)));
13956 /* Convert A ? B : 0 into A && B if A and B are truth values. */
13957 if (integer_zerop (op2)
13958 && truth_value_p (TREE_CODE (arg0))
13959 && truth_value_p (TREE_CODE (arg1)))
13960 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13961 fold_convert_loc (loc, type, arg0),
13964 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
13965 if (integer_onep (op2)
13966 && truth_value_p (TREE_CODE (arg0))
13967 && truth_value_p (TREE_CODE (arg1)))
13969 /* Only perform transformation if ARG0 is easily inverted. */
13970 tem = fold_truth_not_expr (loc, arg0);
13972 return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
13973 fold_convert_loc (loc, type, tem),
13977 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13978 if (integer_zerop (arg1)
13979 && truth_value_p (TREE_CODE (arg0))
13980 && truth_value_p (TREE_CODE (op2)))
13982 /* Only perform transformation if ARG0 is easily inverted. */
13983 tem = fold_truth_not_expr (loc, arg0);
13985 return fold_build2_loc (loc, TRUTH_ANDIF_EXPR, type,
13986 fold_convert_loc (loc, type, tem),
13990 /* Convert A ? 1 : B into A || B if A and B are truth values. */
13991 if (integer_onep (arg1)
13992 && truth_value_p (TREE_CODE (arg0))
13993 && truth_value_p (TREE_CODE (op2)))
13994 return fold_build2_loc (loc, TRUTH_ORIF_EXPR, type,
13995 fold_convert_loc (loc, type, arg0),
14001 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
14002 of fold_ternary on them. */
14003 gcc_unreachable ();
14005 case BIT_FIELD_REF:
14006 if ((TREE_CODE (arg0) == VECTOR_CST
14007 || (TREE_CODE (arg0) == CONSTRUCTOR && TREE_CONSTANT (arg0)))
14008 && type == TREE_TYPE (TREE_TYPE (arg0)))
14010 unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
14011 unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
14014 && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
14015 && (idx % width) == 0
14016 && (idx = idx / width)
14017 < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
14019 tree elements = NULL_TREE;
14021 if (TREE_CODE (arg0) == VECTOR_CST)
14022 elements = TREE_VECTOR_CST_ELTS (arg0);
14025 unsigned HOST_WIDE_INT idx;
14028 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg0), idx, value)
14029 elements = tree_cons (NULL_TREE, value, elements);
14031 while (idx-- > 0 && elements)
14032 elements = TREE_CHAIN (elements);
14034 return TREE_VALUE (elements);
14036 return fold_convert_loc (loc, type, integer_zero_node);
14040 /* A bit-field-ref that referenced the full argument can be stripped. */
14041 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
14042 && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_low_cst (arg1, 1)
14043 && integer_zerop (op2))
14044 return fold_convert_loc (loc, type, arg0);
14050 } /* switch (code) */
14053 /* Perform constant folding and related simplification of EXPR.
14054 The related simplifications include x*1 => x, x*0 => 0, etc.,
14055 and application of the associative law.
14056 NOP_EXPR conversions may be removed freely (as long as we
14057 are careful not to change the type of the overall expression).
14058 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
14059 but we can constant-fold them if they have constant operands. */
14061 #ifdef ENABLE_FOLD_CHECKING
14062 # define fold(x) fold_1 (x)
14063 static tree fold_1 (tree);
14069 const tree t = expr;
14070 enum tree_code code = TREE_CODE (t);
14071 enum tree_code_class kind = TREE_CODE_CLASS (code);
14073 location_t loc = EXPR_LOCATION (expr);
14075 /* Return right away if a constant. */
14076 if (kind == tcc_constant)
14079 /* CALL_EXPR-like objects with variable numbers of operands are
14080 treated specially. */
14081 if (kind == tcc_vl_exp)
14083 if (code == CALL_EXPR)
14085 tem = fold_call_expr (loc, expr, false);
14086 return tem ? tem : expr;
14091 if (IS_EXPR_CODE_CLASS (kind))
14093 tree type = TREE_TYPE (t);
14094 tree op0, op1, op2;
14096 switch (TREE_CODE_LENGTH (code))
14099 op0 = TREE_OPERAND (t, 0);
14100 tem = fold_unary_loc (loc, code, type, op0);
14101 return tem ? tem : expr;
14103 op0 = TREE_OPERAND (t, 0);
14104 op1 = TREE_OPERAND (t, 1);
14105 tem = fold_binary_loc (loc, code, type, op0, op1);
14106 return tem ? tem : expr;
14108 op0 = TREE_OPERAND (t, 0);
14109 op1 = TREE_OPERAND (t, 1);
14110 op2 = TREE_OPERAND (t, 2);
14111 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
14112 return tem ? tem : expr;
14122 tree op0 = TREE_OPERAND (t, 0);
14123 tree op1 = TREE_OPERAND (t, 1);
14125 if (TREE_CODE (op1) == INTEGER_CST
14126 && TREE_CODE (op0) == CONSTRUCTOR
14127 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
14129 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (op0);
14130 unsigned HOST_WIDE_INT end = VEC_length (constructor_elt, elts);
14131 unsigned HOST_WIDE_INT begin = 0;
14133 /* Find a matching index by means of a binary search. */
14134 while (begin != end)
14136 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
14137 tree index = VEC_index (constructor_elt, elts, middle)->index;
14139 if (TREE_CODE (index) == INTEGER_CST
14140 && tree_int_cst_lt (index, op1))
14141 begin = middle + 1;
14142 else if (TREE_CODE (index) == INTEGER_CST
14143 && tree_int_cst_lt (op1, index))
14145 else if (TREE_CODE (index) == RANGE_EXPR
14146 && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
14147 begin = middle + 1;
14148 else if (TREE_CODE (index) == RANGE_EXPR
14149 && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
14152 return VEC_index (constructor_elt, elts, middle)->value;
14160 return fold (DECL_INITIAL (t));
14164 } /* switch (code) */
14167 #ifdef ENABLE_FOLD_CHECKING
14170 static void fold_checksum_tree (const_tree, struct md5_ctx *, htab_t);
14171 static void fold_check_failed (const_tree, const_tree);
14172 void print_fold_checksum (const_tree);
14174 /* When --enable-checking=fold, compute a digest of expr before
14175 and after actual fold call to see if fold did not accidentally
14176 change original expr. */
14182 struct md5_ctx ctx;
14183 unsigned char checksum_before[16], checksum_after[16];
14186 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14187 md5_init_ctx (&ctx);
14188 fold_checksum_tree (expr, &ctx, ht);
14189 md5_finish_ctx (&ctx, checksum_before);
14192 ret = fold_1 (expr);
14194 md5_init_ctx (&ctx);
14195 fold_checksum_tree (expr, &ctx, ht);
14196 md5_finish_ctx (&ctx, checksum_after);
14199 if (memcmp (checksum_before, checksum_after, 16))
14200 fold_check_failed (expr, ret);
14206 print_fold_checksum (const_tree expr)
14208 struct md5_ctx ctx;
14209 unsigned char checksum[16], cnt;
14212 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14213 md5_init_ctx (&ctx);
14214 fold_checksum_tree (expr, &ctx, ht);
14215 md5_finish_ctx (&ctx, checksum);
14217 for (cnt = 0; cnt < 16; ++cnt)
14218 fprintf (stderr, "%02x", checksum[cnt]);
14219 putc ('\n', stderr);
14223 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
14225 internal_error ("fold check: original tree changed by fold");
14229 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, htab_t ht)
14232 enum tree_code code;
14233 union tree_node buf;
14238 gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
14239 <= sizeof (struct tree_function_decl))
14240 && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
14243 slot = (const void **) htab_find_slot (ht, expr, INSERT);
14247 code = TREE_CODE (expr);
14248 if (TREE_CODE_CLASS (code) == tcc_declaration
14249 && DECL_ASSEMBLER_NAME_SET_P (expr))
14251 /* Allow DECL_ASSEMBLER_NAME to be modified. */
14252 memcpy ((char *) &buf, expr, tree_size (expr));
14253 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
14254 expr = (tree) &buf;
14256 else if (TREE_CODE_CLASS (code) == tcc_type
14257 && (TYPE_POINTER_TO (expr)
14258 || TYPE_REFERENCE_TO (expr)
14259 || TYPE_CACHED_VALUES_P (expr)
14260 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
14261 || TYPE_NEXT_VARIANT (expr)))
14263 /* Allow these fields to be modified. */
14265 memcpy ((char *) &buf, expr, tree_size (expr));
14266 expr = tmp = (tree) &buf;
14267 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
14268 TYPE_POINTER_TO (tmp) = NULL;
14269 TYPE_REFERENCE_TO (tmp) = NULL;
14270 TYPE_NEXT_VARIANT (tmp) = NULL;
14271 if (TYPE_CACHED_VALUES_P (tmp))
14273 TYPE_CACHED_VALUES_P (tmp) = 0;
14274 TYPE_CACHED_VALUES (tmp) = NULL;
14277 md5_process_bytes (expr, tree_size (expr), ctx);
14278 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
14279 if (TREE_CODE_CLASS (code) != tcc_type
14280 && TREE_CODE_CLASS (code) != tcc_declaration
14281 && code != TREE_LIST
14282 && code != SSA_NAME)
14283 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
14284 switch (TREE_CODE_CLASS (code))
14290 md5_process_bytes (TREE_STRING_POINTER (expr),
14291 TREE_STRING_LENGTH (expr), ctx);
14294 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
14295 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
14298 fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
14304 case tcc_exceptional:
14308 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
14309 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
14310 expr = TREE_CHAIN (expr);
14311 goto recursive_label;
14314 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
14315 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
14321 case tcc_expression:
14322 case tcc_reference:
14323 case tcc_comparison:
14326 case tcc_statement:
14328 len = TREE_OPERAND_LENGTH (expr);
14329 for (i = 0; i < len; ++i)
14330 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
14332 case tcc_declaration:
14333 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
14334 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
14335 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
14337 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
14338 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
14339 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
14340 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
14341 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
14343 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
14344 fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
14346 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
14348 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
14349 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
14350 fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
14354 if (TREE_CODE (expr) == ENUMERAL_TYPE)
14355 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
14356 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
14357 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
14358 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
14359 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
14360 if (INTEGRAL_TYPE_P (expr)
14361 || SCALAR_FLOAT_TYPE_P (expr))
14363 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
14364 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
14366 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
14367 if (TREE_CODE (expr) == RECORD_TYPE
14368 || TREE_CODE (expr) == UNION_TYPE
14369 || TREE_CODE (expr) == QUAL_UNION_TYPE)
14370 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
14371 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
14378 /* Helper function for outputting the checksum of a tree T. When
14379 debugging with gdb, you can "define mynext" to be "next" followed
14380 by "call debug_fold_checksum (op0)", then just trace down till the
14384 debug_fold_checksum (const_tree t)
14387 unsigned char checksum[16];
14388 struct md5_ctx ctx;
14389 htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14391 md5_init_ctx (&ctx);
14392 fold_checksum_tree (t, &ctx, ht);
14393 md5_finish_ctx (&ctx, checksum);
14396 for (i = 0; i < 16; i++)
14397 fprintf (stderr, "%d ", checksum[i]);
14399 fprintf (stderr, "\n");
14404 /* Fold a unary tree expression with code CODE of type TYPE with an
14405 operand OP0. LOC is the location of the resulting expression.
14406 Return a folded expression if successful. Otherwise, return a tree
14407 expression with code CODE of type TYPE with an operand OP0. */
14410 fold_build1_stat_loc (location_t loc,
14411 enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
14414 #ifdef ENABLE_FOLD_CHECKING
14415 unsigned char checksum_before[16], checksum_after[16];
14416 struct md5_ctx ctx;
14419 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14420 md5_init_ctx (&ctx);
14421 fold_checksum_tree (op0, &ctx, ht);
14422 md5_finish_ctx (&ctx, checksum_before);
14426 tem = fold_unary_loc (loc, code, type, op0);
14429 tem = build1_stat (code, type, op0 PASS_MEM_STAT);
14430 SET_EXPR_LOCATION (tem, loc);
14433 #ifdef ENABLE_FOLD_CHECKING
14434 md5_init_ctx (&ctx);
14435 fold_checksum_tree (op0, &ctx, ht);
14436 md5_finish_ctx (&ctx, checksum_after);
14439 if (memcmp (checksum_before, checksum_after, 16))
14440 fold_check_failed (op0, tem);
14445 /* Fold a binary tree expression with code CODE of type TYPE with
14446 operands OP0 and OP1. LOC is the location of the resulting
14447 expression. Return a folded expression if successful. Otherwise,
14448 return a tree expression with code CODE of type TYPE with operands
14452 fold_build2_stat_loc (location_t loc,
14453 enum tree_code code, tree type, tree op0, tree op1
14457 #ifdef ENABLE_FOLD_CHECKING
14458 unsigned char checksum_before_op0[16],
14459 checksum_before_op1[16],
14460 checksum_after_op0[16],
14461 checksum_after_op1[16];
14462 struct md5_ctx ctx;
14465 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14466 md5_init_ctx (&ctx);
14467 fold_checksum_tree (op0, &ctx, ht);
14468 md5_finish_ctx (&ctx, checksum_before_op0);
14471 md5_init_ctx (&ctx);
14472 fold_checksum_tree (op1, &ctx, ht);
14473 md5_finish_ctx (&ctx, checksum_before_op1);
14477 tem = fold_binary_loc (loc, code, type, op0, op1);
14480 tem = build2_stat (code, type, op0, op1 PASS_MEM_STAT);
14481 SET_EXPR_LOCATION (tem, loc);
14484 #ifdef ENABLE_FOLD_CHECKING
14485 md5_init_ctx (&ctx);
14486 fold_checksum_tree (op0, &ctx, ht);
14487 md5_finish_ctx (&ctx, checksum_after_op0);
14490 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14491 fold_check_failed (op0, tem);
14493 md5_init_ctx (&ctx);
14494 fold_checksum_tree (op1, &ctx, ht);
14495 md5_finish_ctx (&ctx, checksum_after_op1);
14498 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14499 fold_check_failed (op1, tem);
14504 /* Fold a ternary tree expression with code CODE of type TYPE with
14505 operands OP0, OP1, and OP2. Return a folded expression if
14506 successful. Otherwise, return a tree expression with code CODE of
14507 type TYPE with operands OP0, OP1, and OP2. */
14510 fold_build3_stat_loc (location_t loc, enum tree_code code, tree type,
14511 tree op0, tree op1, tree op2 MEM_STAT_DECL)
14514 #ifdef ENABLE_FOLD_CHECKING
14515 unsigned char checksum_before_op0[16],
14516 checksum_before_op1[16],
14517 checksum_before_op2[16],
14518 checksum_after_op0[16],
14519 checksum_after_op1[16],
14520 checksum_after_op2[16];
14521 struct md5_ctx ctx;
14524 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14525 md5_init_ctx (&ctx);
14526 fold_checksum_tree (op0, &ctx, ht);
14527 md5_finish_ctx (&ctx, checksum_before_op0);
14530 md5_init_ctx (&ctx);
14531 fold_checksum_tree (op1, &ctx, ht);
14532 md5_finish_ctx (&ctx, checksum_before_op1);
14535 md5_init_ctx (&ctx);
14536 fold_checksum_tree (op2, &ctx, ht);
14537 md5_finish_ctx (&ctx, checksum_before_op2);
14541 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
14542 tem = fold_ternary_loc (loc, code, type, op0, op1, op2);
14545 tem = build3_stat (code, type, op0, op1, op2 PASS_MEM_STAT);
14546 SET_EXPR_LOCATION (tem, loc);
14549 #ifdef ENABLE_FOLD_CHECKING
14550 md5_init_ctx (&ctx);
14551 fold_checksum_tree (op0, &ctx, ht);
14552 md5_finish_ctx (&ctx, checksum_after_op0);
14555 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
14556 fold_check_failed (op0, tem);
14558 md5_init_ctx (&ctx);
14559 fold_checksum_tree (op1, &ctx, ht);
14560 md5_finish_ctx (&ctx, checksum_after_op1);
14563 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
14564 fold_check_failed (op1, tem);
14566 md5_init_ctx (&ctx);
14567 fold_checksum_tree (op2, &ctx, ht);
14568 md5_finish_ctx (&ctx, checksum_after_op2);
14571 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
14572 fold_check_failed (op2, tem);
14577 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14578 arguments in ARGARRAY, and a null static chain.
14579 Return a folded expression if successful. Otherwise, return a CALL_EXPR
14580 of type TYPE from the given operands as constructed by build_call_array. */
14583 fold_build_call_array_loc (location_t loc, tree type, tree fn,
14584 int nargs, tree *argarray)
14587 #ifdef ENABLE_FOLD_CHECKING
14588 unsigned char checksum_before_fn[16],
14589 checksum_before_arglist[16],
14590 checksum_after_fn[16],
14591 checksum_after_arglist[16];
14592 struct md5_ctx ctx;
14596 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14597 md5_init_ctx (&ctx);
14598 fold_checksum_tree (fn, &ctx, ht);
14599 md5_finish_ctx (&ctx, checksum_before_fn);
14602 md5_init_ctx (&ctx);
14603 for (i = 0; i < nargs; i++)
14604 fold_checksum_tree (argarray[i], &ctx, ht);
14605 md5_finish_ctx (&ctx, checksum_before_arglist);
14609 tem = fold_builtin_call_array (loc, type, fn, nargs, argarray);
14611 #ifdef ENABLE_FOLD_CHECKING
14612 md5_init_ctx (&ctx);
14613 fold_checksum_tree (fn, &ctx, ht);
14614 md5_finish_ctx (&ctx, checksum_after_fn);
14617 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
14618 fold_check_failed (fn, tem);
14620 md5_init_ctx (&ctx);
14621 for (i = 0; i < nargs; i++)
14622 fold_checksum_tree (argarray[i], &ctx, ht);
14623 md5_finish_ctx (&ctx, checksum_after_arglist);
14626 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
14627 fold_check_failed (NULL_TREE, tem);
14632 /* Perform constant folding and related simplification of initializer
14633 expression EXPR. These behave identically to "fold_buildN" but ignore
14634 potential run-time traps and exceptions that fold must preserve. */
14636 #define START_FOLD_INIT \
14637 int saved_signaling_nans = flag_signaling_nans;\
14638 int saved_trapping_math = flag_trapping_math;\
14639 int saved_rounding_math = flag_rounding_math;\
14640 int saved_trapv = flag_trapv;\
14641 int saved_folding_initializer = folding_initializer;\
14642 flag_signaling_nans = 0;\
14643 flag_trapping_math = 0;\
14644 flag_rounding_math = 0;\
14646 folding_initializer = 1;
14648 #define END_FOLD_INIT \
14649 flag_signaling_nans = saved_signaling_nans;\
14650 flag_trapping_math = saved_trapping_math;\
14651 flag_rounding_math = saved_rounding_math;\
14652 flag_trapv = saved_trapv;\
14653 folding_initializer = saved_folding_initializer;
14656 fold_build1_initializer_loc (location_t loc, enum tree_code code,
14657 tree type, tree op)
14662 result = fold_build1_loc (loc, code, type, op);
14669 fold_build2_initializer_loc (location_t loc, enum tree_code code,
14670 tree type, tree op0, tree op1)
14675 result = fold_build2_loc (loc, code, type, op0, op1);
14682 fold_build3_initializer_loc (location_t loc, enum tree_code code,
14683 tree type, tree op0, tree op1, tree op2)
14688 result = fold_build3_loc (loc, code, type, op0, op1, op2);
14695 fold_build_call_array_initializer_loc (location_t loc, tree type, tree fn,
14696 int nargs, tree *argarray)
14701 result = fold_build_call_array_loc (loc, type, fn, nargs, argarray);
14707 #undef START_FOLD_INIT
14708 #undef END_FOLD_INIT
14710 /* Determine if first argument is a multiple of second argument. Return 0 if
14711 it is not, or we cannot easily determined it to be.
14713 An example of the sort of thing we care about (at this point; this routine
14714 could surely be made more general, and expanded to do what the *_DIV_EXPR's
14715 fold cases do now) is discovering that
14717 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14723 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14725 This code also handles discovering that
14727 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14729 is a multiple of 8 so we don't have to worry about dealing with a
14730 possible remainder.
14732 Note that we *look* inside a SAVE_EXPR only to determine how it was
14733 calculated; it is not safe for fold to do much of anything else with the
14734 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14735 at run time. For example, the latter example above *cannot* be implemented
14736 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14737 evaluation time of the original SAVE_EXPR is not necessarily the same at
14738 the time the new expression is evaluated. The only optimization of this
14739 sort that would be valid is changing
14741 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14745 SAVE_EXPR (I) * SAVE_EXPR (J)
14747 (where the same SAVE_EXPR (J) is used in the original and the
14748 transformed version). */
14751 multiple_of_p (tree type, const_tree top, const_tree bottom)
14753 if (operand_equal_p (top, bottom, 0))
14756 if (TREE_CODE (type) != INTEGER_TYPE)
14759 switch (TREE_CODE (top))
14762 /* Bitwise and provides a power of two multiple. If the mask is
14763 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
14764 if (!integer_pow2p (bottom))
14769 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14770 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14774 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14775 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14778 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14782 op1 = TREE_OPERAND (top, 1);
14783 /* const_binop may not detect overflow correctly,
14784 so check for it explicitly here. */
14785 if (TYPE_PRECISION (TREE_TYPE (size_one_node))
14786 > TREE_INT_CST_LOW (op1)
14787 && TREE_INT_CST_HIGH (op1) == 0
14788 && 0 != (t1 = fold_convert (type,
14789 const_binop (LSHIFT_EXPR,
14792 && !TREE_OVERFLOW (t1))
14793 return multiple_of_p (type, t1, bottom);
14798 /* Can't handle conversions from non-integral or wider integral type. */
14799 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14800 || (TYPE_PRECISION (type)
14801 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14804 /* .. fall through ... */
14807 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
14810 if (TREE_CODE (bottom) != INTEGER_CST
14811 || integer_zerop (bottom)
14812 || (TYPE_UNSIGNED (type)
14813 && (tree_int_cst_sgn (top) < 0
14814 || tree_int_cst_sgn (bottom) < 0)))
14816 return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
14824 /* Return true if CODE or TYPE is known to be non-negative. */
14827 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14829 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14830 && truth_value_p (code))
14831 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14832 have a signed:1 type (where the value is -1 and 0). */
14837 /* Return true if (CODE OP0) is known to be non-negative. If the return
14838 value is based on the assumption that signed overflow is undefined,
14839 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14840 *STRICT_OVERFLOW_P. */
14843 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14844 bool *strict_overflow_p)
14846 if (TYPE_UNSIGNED (type))
14852 /* We can't return 1 if flag_wrapv is set because
14853 ABS_EXPR<INT_MIN> = INT_MIN. */
14854 if (!INTEGRAL_TYPE_P (type))
14856 if (TYPE_OVERFLOW_UNDEFINED (type))
14858 *strict_overflow_p = true;
14863 case NON_LVALUE_EXPR:
14865 case FIX_TRUNC_EXPR:
14866 return tree_expr_nonnegative_warnv_p (op0,
14867 strict_overflow_p);
14871 tree inner_type = TREE_TYPE (op0);
14872 tree outer_type = type;
14874 if (TREE_CODE (outer_type) == REAL_TYPE)
14876 if (TREE_CODE (inner_type) == REAL_TYPE)
14877 return tree_expr_nonnegative_warnv_p (op0,
14878 strict_overflow_p);
14879 if (TREE_CODE (inner_type) == INTEGER_TYPE)
14881 if (TYPE_UNSIGNED (inner_type))
14883 return tree_expr_nonnegative_warnv_p (op0,
14884 strict_overflow_p);
14887 else if (TREE_CODE (outer_type) == INTEGER_TYPE)
14889 if (TREE_CODE (inner_type) == REAL_TYPE)
14890 return tree_expr_nonnegative_warnv_p (op0,
14891 strict_overflow_p);
14892 if (TREE_CODE (inner_type) == INTEGER_TYPE)
14893 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14894 && TYPE_UNSIGNED (inner_type);
14900 return tree_simple_nonnegative_warnv_p (code, type);
14903 /* We don't know sign of `t', so be conservative and return false. */
14907 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
14908 value is based on the assumption that signed overflow is undefined,
14909 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14910 *STRICT_OVERFLOW_P. */
14913 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14914 tree op1, bool *strict_overflow_p)
14916 if (TYPE_UNSIGNED (type))
14921 case POINTER_PLUS_EXPR:
14923 if (FLOAT_TYPE_P (type))
14924 return (tree_expr_nonnegative_warnv_p (op0,
14926 && tree_expr_nonnegative_warnv_p (op1,
14927 strict_overflow_p));
14929 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14930 both unsigned and at least 2 bits shorter than the result. */
14931 if (TREE_CODE (type) == INTEGER_TYPE
14932 && TREE_CODE (op0) == NOP_EXPR
14933 && TREE_CODE (op1) == NOP_EXPR)
14935 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14936 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14937 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14938 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14940 unsigned int prec = MAX (TYPE_PRECISION (inner1),
14941 TYPE_PRECISION (inner2)) + 1;
14942 return prec < TYPE_PRECISION (type);
14948 if (FLOAT_TYPE_P (type))
14950 /* x * x for floating point x is always non-negative. */
14951 if (operand_equal_p (op0, op1, 0))
14953 return (tree_expr_nonnegative_warnv_p (op0,
14955 && tree_expr_nonnegative_warnv_p (op1,
14956 strict_overflow_p));
14959 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14960 both unsigned and their total bits is shorter than the result. */
14961 if (TREE_CODE (type) == INTEGER_TYPE
14962 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14963 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14965 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14966 ? TREE_TYPE (TREE_OPERAND (op0, 0))
14968 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14969 ? TREE_TYPE (TREE_OPERAND (op1, 0))
14972 bool unsigned0 = TYPE_UNSIGNED (inner0);
14973 bool unsigned1 = TYPE_UNSIGNED (inner1);
14975 if (TREE_CODE (op0) == INTEGER_CST)
14976 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14978 if (TREE_CODE (op1) == INTEGER_CST)
14979 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14981 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14982 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14984 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14985 ? tree_int_cst_min_precision (op0, /*unsignedp=*/true)
14986 : TYPE_PRECISION (inner0);
14988 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14989 ? tree_int_cst_min_precision (op1, /*unsignedp=*/true)
14990 : TYPE_PRECISION (inner1);
14992 return precision0 + precision1 < TYPE_PRECISION (type);
14999 return (tree_expr_nonnegative_warnv_p (op0,
15001 || tree_expr_nonnegative_warnv_p (op1,
15002 strict_overflow_p));
15008 case TRUNC_DIV_EXPR:
15009 case CEIL_DIV_EXPR:
15010 case FLOOR_DIV_EXPR:
15011 case ROUND_DIV_EXPR:
15012 return (tree_expr_nonnegative_warnv_p (op0,
15014 && tree_expr_nonnegative_warnv_p (op1,
15015 strict_overflow_p));
15017 case TRUNC_MOD_EXPR:
15018 case CEIL_MOD_EXPR:
15019 case FLOOR_MOD_EXPR:
15020 case ROUND_MOD_EXPR:
15021 return tree_expr_nonnegative_warnv_p (op0,
15022 strict_overflow_p);
15024 return tree_simple_nonnegative_warnv_p (code, type);
15027 /* We don't know sign of `t', so be conservative and return false. */
15031 /* Return true if T is known to be non-negative. If the return
15032 value is based on the assumption that signed overflow is undefined,
15033 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15034 *STRICT_OVERFLOW_P. */
15037 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
15039 if (TYPE_UNSIGNED (TREE_TYPE (t)))
15042 switch (TREE_CODE (t))
15045 return tree_int_cst_sgn (t) >= 0;
15048 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
15051 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
15054 return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
15056 && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
15057 strict_overflow_p));
15059 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
15062 /* We don't know sign of `t', so be conservative and return false. */
15066 /* Return true if T is known to be non-negative. If the return
15067 value is based on the assumption that signed overflow is undefined,
15068 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15069 *STRICT_OVERFLOW_P. */
15072 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
15073 tree arg0, tree arg1, bool *strict_overflow_p)
15075 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
15076 switch (DECL_FUNCTION_CODE (fndecl))
15078 CASE_FLT_FN (BUILT_IN_ACOS):
15079 CASE_FLT_FN (BUILT_IN_ACOSH):
15080 CASE_FLT_FN (BUILT_IN_CABS):
15081 CASE_FLT_FN (BUILT_IN_COSH):
15082 CASE_FLT_FN (BUILT_IN_ERFC):
15083 CASE_FLT_FN (BUILT_IN_EXP):
15084 CASE_FLT_FN (BUILT_IN_EXP10):
15085 CASE_FLT_FN (BUILT_IN_EXP2):
15086 CASE_FLT_FN (BUILT_IN_FABS):
15087 CASE_FLT_FN (BUILT_IN_FDIM):
15088 CASE_FLT_FN (BUILT_IN_HYPOT):
15089 CASE_FLT_FN (BUILT_IN_POW10):
15090 CASE_INT_FN (BUILT_IN_FFS):
15091 CASE_INT_FN (BUILT_IN_PARITY):
15092 CASE_INT_FN (BUILT_IN_POPCOUNT):
15093 case BUILT_IN_BSWAP32:
15094 case BUILT_IN_BSWAP64:
15098 CASE_FLT_FN (BUILT_IN_SQRT):
15099 /* sqrt(-0.0) is -0.0. */
15100 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
15102 return tree_expr_nonnegative_warnv_p (arg0,
15103 strict_overflow_p);
15105 CASE_FLT_FN (BUILT_IN_ASINH):
15106 CASE_FLT_FN (BUILT_IN_ATAN):
15107 CASE_FLT_FN (BUILT_IN_ATANH):
15108 CASE_FLT_FN (BUILT_IN_CBRT):
15109 CASE_FLT_FN (BUILT_IN_CEIL):
15110 CASE_FLT_FN (BUILT_IN_ERF):
15111 CASE_FLT_FN (BUILT_IN_EXPM1):
15112 CASE_FLT_FN (BUILT_IN_FLOOR):
15113 CASE_FLT_FN (BUILT_IN_FMOD):
15114 CASE_FLT_FN (BUILT_IN_FREXP):
15115 CASE_FLT_FN (BUILT_IN_LCEIL):
15116 CASE_FLT_FN (BUILT_IN_LDEXP):
15117 CASE_FLT_FN (BUILT_IN_LFLOOR):
15118 CASE_FLT_FN (BUILT_IN_LLCEIL):
15119 CASE_FLT_FN (BUILT_IN_LLFLOOR):
15120 CASE_FLT_FN (BUILT_IN_LLRINT):
15121 CASE_FLT_FN (BUILT_IN_LLROUND):
15122 CASE_FLT_FN (BUILT_IN_LRINT):
15123 CASE_FLT_FN (BUILT_IN_LROUND):
15124 CASE_FLT_FN (BUILT_IN_MODF):
15125 CASE_FLT_FN (BUILT_IN_NEARBYINT):
15126 CASE_FLT_FN (BUILT_IN_RINT):
15127 CASE_FLT_FN (BUILT_IN_ROUND):
15128 CASE_FLT_FN (BUILT_IN_SCALB):
15129 CASE_FLT_FN (BUILT_IN_SCALBLN):
15130 CASE_FLT_FN (BUILT_IN_SCALBN):
15131 CASE_FLT_FN (BUILT_IN_SIGNBIT):
15132 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
15133 CASE_FLT_FN (BUILT_IN_SINH):
15134 CASE_FLT_FN (BUILT_IN_TANH):
15135 CASE_FLT_FN (BUILT_IN_TRUNC):
15136 /* True if the 1st argument is nonnegative. */
15137 return tree_expr_nonnegative_warnv_p (arg0,
15138 strict_overflow_p);
15140 CASE_FLT_FN (BUILT_IN_FMAX):
15141 /* True if the 1st OR 2nd arguments are nonnegative. */
15142 return (tree_expr_nonnegative_warnv_p (arg0,
15144 || (tree_expr_nonnegative_warnv_p (arg1,
15145 strict_overflow_p)));
15147 CASE_FLT_FN (BUILT_IN_FMIN):
15148 /* True if the 1st AND 2nd arguments are nonnegative. */
15149 return (tree_expr_nonnegative_warnv_p (arg0,
15151 && (tree_expr_nonnegative_warnv_p (arg1,
15152 strict_overflow_p)));
15154 CASE_FLT_FN (BUILT_IN_COPYSIGN):
15155 /* True if the 2nd argument is nonnegative. */
15156 return tree_expr_nonnegative_warnv_p (arg1,
15157 strict_overflow_p);
15159 CASE_FLT_FN (BUILT_IN_POWI):
15160 /* True if the 1st argument is nonnegative or the second
15161 argument is an even integer. */
15162 if (TREE_CODE (arg1) == INTEGER_CST
15163 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
15165 return tree_expr_nonnegative_warnv_p (arg0,
15166 strict_overflow_p);
15168 CASE_FLT_FN (BUILT_IN_POW):
15169 /* True if the 1st argument is nonnegative or the second
15170 argument is an even integer valued real. */
15171 if (TREE_CODE (arg1) == REAL_CST)
15176 c = TREE_REAL_CST (arg1);
15177 n = real_to_integer (&c);
15180 REAL_VALUE_TYPE cint;
15181 real_from_integer (&cint, VOIDmode, n,
15182 n < 0 ? -1 : 0, 0);
15183 if (real_identical (&c, &cint))
15187 return tree_expr_nonnegative_warnv_p (arg0,
15188 strict_overflow_p);
15193 return tree_simple_nonnegative_warnv_p (CALL_EXPR,
15197 /* Return true if T is known to be non-negative. If the return
15198 value is based on the assumption that signed overflow is undefined,
15199 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15200 *STRICT_OVERFLOW_P. */
15203 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
15205 enum tree_code code = TREE_CODE (t);
15206 if (TYPE_UNSIGNED (TREE_TYPE (t)))
15213 tree temp = TARGET_EXPR_SLOT (t);
15214 t = TARGET_EXPR_INITIAL (t);
15216 /* If the initializer is non-void, then it's a normal expression
15217 that will be assigned to the slot. */
15218 if (!VOID_TYPE_P (t))
15219 return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
15221 /* Otherwise, the initializer sets the slot in some way. One common
15222 way is an assignment statement at the end of the initializer. */
15225 if (TREE_CODE (t) == BIND_EXPR)
15226 t = expr_last (BIND_EXPR_BODY (t));
15227 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
15228 || TREE_CODE (t) == TRY_CATCH_EXPR)
15229 t = expr_last (TREE_OPERAND (t, 0));
15230 else if (TREE_CODE (t) == STATEMENT_LIST)
15235 if (TREE_CODE (t) == MODIFY_EXPR
15236 && TREE_OPERAND (t, 0) == temp)
15237 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
15238 strict_overflow_p);
15245 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
15246 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
15248 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
15249 get_callee_fndecl (t),
15252 strict_overflow_p);
15254 case COMPOUND_EXPR:
15256 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
15257 strict_overflow_p);
15259 return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
15260 strict_overflow_p);
15262 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
15263 strict_overflow_p);
15266 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
15270 /* We don't know sign of `t', so be conservative and return false. */
15274 /* Return true if T is known to be non-negative. If the return
15275 value is based on the assumption that signed overflow is undefined,
15276 set *STRICT_OVERFLOW_P to true; otherwise, don't change
15277 *STRICT_OVERFLOW_P. */
15280 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
15282 enum tree_code code;
15283 if (t == error_mark_node)
15286 code = TREE_CODE (t);
15287 switch (TREE_CODE_CLASS (code))
15290 case tcc_comparison:
15291 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
15293 TREE_OPERAND (t, 0),
15294 TREE_OPERAND (t, 1),
15295 strict_overflow_p);
15298 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
15300 TREE_OPERAND (t, 0),
15301 strict_overflow_p);
15304 case tcc_declaration:
15305 case tcc_reference:
15306 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
15314 case TRUTH_AND_EXPR:
15315 case TRUTH_OR_EXPR:
15316 case TRUTH_XOR_EXPR:
15317 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
15319 TREE_OPERAND (t, 0),
15320 TREE_OPERAND (t, 1),
15321 strict_overflow_p);
15322 case TRUTH_NOT_EXPR:
15323 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
15325 TREE_OPERAND (t, 0),
15326 strict_overflow_p);
15333 case WITH_SIZE_EXPR:
15335 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
15338 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
15342 /* Return true if `t' is known to be non-negative. Handle warnings
15343 about undefined signed overflow. */
15346 tree_expr_nonnegative_p (tree t)
15348 bool ret, strict_overflow_p;
15350 strict_overflow_p = false;
15351 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
15352 if (strict_overflow_p)
15353 fold_overflow_warning (("assuming signed overflow does not occur when "
15354 "determining that expression is always "
15356 WARN_STRICT_OVERFLOW_MISC);
15361 /* Return true when (CODE OP0) is an address and is known to be nonzero.
15362 For floating point we further ensure that T is not denormal.
15363 Similar logic is present in nonzero_address in rtlanal.h.
15365 If the return value is based on the assumption that signed overflow
15366 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15367 change *STRICT_OVERFLOW_P. */
15370 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
15371 bool *strict_overflow_p)
15376 return tree_expr_nonzero_warnv_p (op0,
15377 strict_overflow_p);
15381 tree inner_type = TREE_TYPE (op0);
15382 tree outer_type = type;
15384 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
15385 && tree_expr_nonzero_warnv_p (op0,
15386 strict_overflow_p));
15390 case NON_LVALUE_EXPR:
15391 return tree_expr_nonzero_warnv_p (op0,
15392 strict_overflow_p);
15401 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
15402 For floating point we further ensure that T is not denormal.
15403 Similar logic is present in nonzero_address in rtlanal.h.
15405 If the return value is based on the assumption that signed overflow
15406 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15407 change *STRICT_OVERFLOW_P. */
15410 tree_binary_nonzero_warnv_p (enum tree_code code,
15413 tree op1, bool *strict_overflow_p)
15415 bool sub_strict_overflow_p;
15418 case POINTER_PLUS_EXPR:
15420 if (TYPE_OVERFLOW_UNDEFINED (type))
15422 /* With the presence of negative values it is hard
15423 to say something. */
15424 sub_strict_overflow_p = false;
15425 if (!tree_expr_nonnegative_warnv_p (op0,
15426 &sub_strict_overflow_p)
15427 || !tree_expr_nonnegative_warnv_p (op1,
15428 &sub_strict_overflow_p))
15430 /* One of operands must be positive and the other non-negative. */
15431 /* We don't set *STRICT_OVERFLOW_P here: even if this value
15432 overflows, on a twos-complement machine the sum of two
15433 nonnegative numbers can never be zero. */
15434 return (tree_expr_nonzero_warnv_p (op0,
15436 || tree_expr_nonzero_warnv_p (op1,
15437 strict_overflow_p));
15442 if (TYPE_OVERFLOW_UNDEFINED (type))
15444 if (tree_expr_nonzero_warnv_p (op0,
15446 && tree_expr_nonzero_warnv_p (op1,
15447 strict_overflow_p))
15449 *strict_overflow_p = true;
15456 sub_strict_overflow_p = false;
15457 if (tree_expr_nonzero_warnv_p (op0,
15458 &sub_strict_overflow_p)
15459 && tree_expr_nonzero_warnv_p (op1,
15460 &sub_strict_overflow_p))
15462 if (sub_strict_overflow_p)
15463 *strict_overflow_p = true;
15468 sub_strict_overflow_p = false;
15469 if (tree_expr_nonzero_warnv_p (op0,
15470 &sub_strict_overflow_p))
15472 if (sub_strict_overflow_p)
15473 *strict_overflow_p = true;
15475 /* When both operands are nonzero, then MAX must be too. */
15476 if (tree_expr_nonzero_warnv_p (op1,
15477 strict_overflow_p))
15480 /* MAX where operand 0 is positive is positive. */
15481 return tree_expr_nonnegative_warnv_p (op0,
15482 strict_overflow_p);
15484 /* MAX where operand 1 is positive is positive. */
15485 else if (tree_expr_nonzero_warnv_p (op1,
15486 &sub_strict_overflow_p)
15487 && tree_expr_nonnegative_warnv_p (op1,
15488 &sub_strict_overflow_p))
15490 if (sub_strict_overflow_p)
15491 *strict_overflow_p = true;
15497 return (tree_expr_nonzero_warnv_p (op1,
15499 || tree_expr_nonzero_warnv_p (op0,
15500 strict_overflow_p));
15509 /* Return true when T is an address and is known to be nonzero.
15510 For floating point we further ensure that T is not denormal.
15511 Similar logic is present in nonzero_address in rtlanal.h.
15513 If the return value is based on the assumption that signed overflow
15514 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15515 change *STRICT_OVERFLOW_P. */
15518 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15520 bool sub_strict_overflow_p;
15521 switch (TREE_CODE (t))
15524 return !integer_zerop (t);
15528 tree base = get_base_address (TREE_OPERAND (t, 0));
15533 /* Weak declarations may link to NULL. Other things may also be NULL
15534 so protect with -fdelete-null-pointer-checks; but not variables
15535 allocated on the stack. */
15537 && (flag_delete_null_pointer_checks
15538 || (TREE_CODE (base) == VAR_DECL && !TREE_STATIC (base))))
15539 return !VAR_OR_FUNCTION_DECL_P (base) || !DECL_WEAK (base);
15541 /* Constants are never weak. */
15542 if (CONSTANT_CLASS_P (base))
15549 sub_strict_overflow_p = false;
15550 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15551 &sub_strict_overflow_p)
15552 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
15553 &sub_strict_overflow_p))
15555 if (sub_strict_overflow_p)
15556 *strict_overflow_p = true;
15567 /* Return true when T is an address and is known to be nonzero.
15568 For floating point we further ensure that T is not denormal.
15569 Similar logic is present in nonzero_address in rtlanal.h.
15571 If the return value is based on the assumption that signed overflow
15572 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
15573 change *STRICT_OVERFLOW_P. */
15576 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15578 tree type = TREE_TYPE (t);
15579 enum tree_code code;
15581 /* Doing something useful for floating point would need more work. */
15582 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
15585 code = TREE_CODE (t);
15586 switch (TREE_CODE_CLASS (code))
15589 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15590 strict_overflow_p);
15592 case tcc_comparison:
15593 return tree_binary_nonzero_warnv_p (code, type,
15594 TREE_OPERAND (t, 0),
15595 TREE_OPERAND (t, 1),
15596 strict_overflow_p);
15598 case tcc_declaration:
15599 case tcc_reference:
15600 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15608 case TRUTH_NOT_EXPR:
15609 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15610 strict_overflow_p);
15612 case TRUTH_AND_EXPR:
15613 case TRUTH_OR_EXPR:
15614 case TRUTH_XOR_EXPR:
15615 return tree_binary_nonzero_warnv_p (code, type,
15616 TREE_OPERAND (t, 0),
15617 TREE_OPERAND (t, 1),
15618 strict_overflow_p);
15625 case WITH_SIZE_EXPR:
15627 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15629 case COMPOUND_EXPR:
15632 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15633 strict_overflow_p);
15636 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
15637 strict_overflow_p);
15640 return alloca_call_p (t);
15648 /* Return true when T is an address and is known to be nonzero.
15649 Handle warnings about undefined signed overflow. */
15652 tree_expr_nonzero_p (tree t)
15654 bool ret, strict_overflow_p;
15656 strict_overflow_p = false;
15657 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
15658 if (strict_overflow_p)
15659 fold_overflow_warning (("assuming signed overflow does not occur when "
15660 "determining that expression is always "
15662 WARN_STRICT_OVERFLOW_MISC);
15666 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15667 attempt to fold the expression to a constant without modifying TYPE,
15670 If the expression could be simplified to a constant, then return
15671 the constant. If the expression would not be simplified to a
15672 constant, then return NULL_TREE. */
15675 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15677 tree tem = fold_binary (code, type, op0, op1);
15678 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15681 /* Given the components of a unary expression CODE, TYPE and OP0,
15682 attempt to fold the expression to a constant without modifying
15685 If the expression could be simplified to a constant, then return
15686 the constant. If the expression would not be simplified to a
15687 constant, then return NULL_TREE. */
15690 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15692 tree tem = fold_unary (code, type, op0);
15693 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15696 /* If EXP represents referencing an element in a constant string
15697 (either via pointer arithmetic or array indexing), return the
15698 tree representing the value accessed, otherwise return NULL. */
15701 fold_read_from_constant_string (tree exp)
15703 if ((TREE_CODE (exp) == INDIRECT_REF
15704 || TREE_CODE (exp) == ARRAY_REF)
15705 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15707 tree exp1 = TREE_OPERAND (exp, 0);
15710 location_t loc = EXPR_LOCATION (exp);
15712 if (TREE_CODE (exp) == INDIRECT_REF)
15713 string = string_constant (exp1, &index);
15716 tree low_bound = array_ref_low_bound (exp);
15717 index = fold_convert_loc (loc, sizetype, TREE_OPERAND (exp, 1));
15719 /* Optimize the special-case of a zero lower bound.
15721 We convert the low_bound to sizetype to avoid some problems
15722 with constant folding. (E.g. suppose the lower bound is 1,
15723 and its mode is QI. Without the conversion,l (ARRAY
15724 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15725 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
15726 if (! integer_zerop (low_bound))
15727 index = size_diffop_loc (loc, index,
15728 fold_convert_loc (loc, sizetype, low_bound));
15734 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15735 && TREE_CODE (string) == STRING_CST
15736 && TREE_CODE (index) == INTEGER_CST
15737 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15738 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
15740 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
15741 return build_int_cst_type (TREE_TYPE (exp),
15742 (TREE_STRING_POINTER (string)
15743 [TREE_INT_CST_LOW (index)]));
15748 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15749 an integer constant, real, or fixed-point constant.
15751 TYPE is the type of the result. */
15754 fold_negate_const (tree arg0, tree type)
15756 tree t = NULL_TREE;
15758 switch (TREE_CODE (arg0))
15762 unsigned HOST_WIDE_INT low;
15763 HOST_WIDE_INT high;
15764 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
15765 TREE_INT_CST_HIGH (arg0),
15767 t = force_fit_type_double (type, low, high, 1,
15768 (overflow | TREE_OVERFLOW (arg0))
15769 && !TYPE_UNSIGNED (type));
15774 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
15779 FIXED_VALUE_TYPE f;
15780 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15781 &(TREE_FIXED_CST (arg0)), NULL,
15782 TYPE_SATURATING (type));
15783 t = build_fixed (type, f);
15784 /* Propagate overflow flags. */
15785 if (overflow_p | TREE_OVERFLOW (arg0))
15786 TREE_OVERFLOW (t) = 1;
15791 gcc_unreachable ();
15797 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15798 an integer constant or real constant.
15800 TYPE is the type of the result. */
15803 fold_abs_const (tree arg0, tree type)
15805 tree t = NULL_TREE;
15807 switch (TREE_CODE (arg0))
15810 /* If the value is unsigned, then the absolute value is
15811 the same as the ordinary value. */
15812 if (TYPE_UNSIGNED (type))
15814 /* Similarly, if the value is non-negative. */
15815 else if (INT_CST_LT (integer_minus_one_node, arg0))
15817 /* If the value is negative, then the absolute value is
15821 unsigned HOST_WIDE_INT low;
15822 HOST_WIDE_INT high;
15823 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
15824 TREE_INT_CST_HIGH (arg0),
15826 t = force_fit_type_double (type, low, high, -1,
15827 overflow | TREE_OVERFLOW (arg0));
15832 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15833 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
15839 gcc_unreachable ();
15845 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15846 constant. TYPE is the type of the result. */
15849 fold_not_const (tree arg0, tree type)
15851 tree t = NULL_TREE;
15853 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15855 t = force_fit_type_double (type, ~TREE_INT_CST_LOW (arg0),
15856 ~TREE_INT_CST_HIGH (arg0), 0,
15857 TREE_OVERFLOW (arg0));
15862 /* Given CODE, a relational operator, the target type, TYPE and two
15863 constant operands OP0 and OP1, return the result of the
15864 relational operation. If the result is not a compile time
15865 constant, then return NULL_TREE. */
15868 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15870 int result, invert;
15872 /* From here on, the only cases we handle are when the result is
15873 known to be a constant. */
15875 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15877 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15878 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15880 /* Handle the cases where either operand is a NaN. */
15881 if (real_isnan (c0) || real_isnan (c1))
15891 case UNORDERED_EXPR:
15905 if (flag_trapping_math)
15911 gcc_unreachable ();
15914 return constant_boolean_node (result, type);
15917 return constant_boolean_node (real_compare (code, c0, c1), type);
15920 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15922 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15923 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15924 return constant_boolean_node (fixed_compare (code, c0, c1), type);
15927 /* Handle equality/inequality of complex constants. */
15928 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15930 tree rcond = fold_relational_const (code, type,
15931 TREE_REALPART (op0),
15932 TREE_REALPART (op1));
15933 tree icond = fold_relational_const (code, type,
15934 TREE_IMAGPART (op0),
15935 TREE_IMAGPART (op1));
15936 if (code == EQ_EXPR)
15937 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15938 else if (code == NE_EXPR)
15939 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15944 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15946 To compute GT, swap the arguments and do LT.
15947 To compute GE, do LT and invert the result.
15948 To compute LE, swap the arguments, do LT and invert the result.
15949 To compute NE, do EQ and invert the result.
15951 Therefore, the code below must handle only EQ and LT. */
15953 if (code == LE_EXPR || code == GT_EXPR)
15958 code = swap_tree_comparison (code);
15961 /* Note that it is safe to invert for real values here because we
15962 have already handled the one case that it matters. */
15965 if (code == NE_EXPR || code == GE_EXPR)
15968 code = invert_tree_comparison (code, false);
15971 /* Compute a result for LT or EQ if args permit;
15972 Otherwise return T. */
15973 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15975 if (code == EQ_EXPR)
15976 result = tree_int_cst_equal (op0, op1);
15977 else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
15978 result = INT_CST_LT_UNSIGNED (op0, op1);
15980 result = INT_CST_LT (op0, op1);
15987 return constant_boolean_node (result, type);
15990 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15991 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
15995 fold_build_cleanup_point_expr (tree type, tree expr)
15997 /* If the expression does not have side effects then we don't have to wrap
15998 it with a cleanup point expression. */
15999 if (!TREE_SIDE_EFFECTS (expr))
16002 /* If the expression is a return, check to see if the expression inside the
16003 return has no side effects or the right hand side of the modify expression
16004 inside the return. If either don't have side effects set we don't need to
16005 wrap the expression in a cleanup point expression. Note we don't check the
16006 left hand side of the modify because it should always be a return decl. */
16007 if (TREE_CODE (expr) == RETURN_EXPR)
16009 tree op = TREE_OPERAND (expr, 0);
16010 if (!op || !TREE_SIDE_EFFECTS (op))
16012 op = TREE_OPERAND (op, 1);
16013 if (!TREE_SIDE_EFFECTS (op))
16017 return build1 (CLEANUP_POINT_EXPR, type, expr);
16020 /* Given a pointer value OP0 and a type TYPE, return a simplified version
16021 of an indirection through OP0, or NULL_TREE if no simplification is
16025 fold_indirect_ref_1 (location_t loc, tree type, tree op0)
16031 subtype = TREE_TYPE (sub);
16032 if (!POINTER_TYPE_P (subtype))
16035 if (TREE_CODE (sub) == ADDR_EXPR)
16037 tree op = TREE_OPERAND (sub, 0);
16038 tree optype = TREE_TYPE (op);
16039 /* *&CONST_DECL -> to the value of the const decl. */
16040 if (TREE_CODE (op) == CONST_DECL)
16041 return DECL_INITIAL (op);
16042 /* *&p => p; make sure to handle *&"str"[cst] here. */
16043 if (type == optype)
16045 tree fop = fold_read_from_constant_string (op);
16051 /* *(foo *)&fooarray => fooarray[0] */
16052 else if (TREE_CODE (optype) == ARRAY_TYPE
16053 && type == TREE_TYPE (optype))
16055 tree type_domain = TYPE_DOMAIN (optype);
16056 tree min_val = size_zero_node;
16057 if (type_domain && TYPE_MIN_VALUE (type_domain))
16058 min_val = TYPE_MIN_VALUE (type_domain);
16059 op0 = build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
16060 SET_EXPR_LOCATION (op0, loc);
16063 /* *(foo *)&complexfoo => __real__ complexfoo */
16064 else if (TREE_CODE (optype) == COMPLEX_TYPE
16065 && type == TREE_TYPE (optype))
16066 return fold_build1_loc (loc, REALPART_EXPR, type, op);
16067 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
16068 else if (TREE_CODE (optype) == VECTOR_TYPE
16069 && type == TREE_TYPE (optype))
16071 tree part_width = TYPE_SIZE (type);
16072 tree index = bitsize_int (0);
16073 return fold_build3_loc (loc, BIT_FIELD_REF, type, op, part_width, index);
16077 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
16078 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
16079 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
16081 tree op00 = TREE_OPERAND (sub, 0);
16082 tree op01 = TREE_OPERAND (sub, 1);
16086 op00type = TREE_TYPE (op00);
16087 if (TREE_CODE (op00) == ADDR_EXPR
16088 && TREE_CODE (TREE_TYPE (op00type)) == VECTOR_TYPE
16089 && type == TREE_TYPE (TREE_TYPE (op00type)))
16091 HOST_WIDE_INT offset = tree_low_cst (op01, 0);
16092 tree part_width = TYPE_SIZE (type);
16093 unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT;
16094 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
16095 tree index = bitsize_int (indexi);
16097 if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (op00type)))
16098 return fold_build3_loc (loc,
16099 BIT_FIELD_REF, type, TREE_OPERAND (op00, 0),
16100 part_width, index);
16106 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
16107 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
16108 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
16110 tree op00 = TREE_OPERAND (sub, 0);
16111 tree op01 = TREE_OPERAND (sub, 1);
16115 op00type = TREE_TYPE (op00);
16116 if (TREE_CODE (op00) == ADDR_EXPR
16117 && TREE_CODE (TREE_TYPE (op00type)) == COMPLEX_TYPE
16118 && type == TREE_TYPE (TREE_TYPE (op00type)))
16120 tree size = TYPE_SIZE_UNIT (type);
16121 if (tree_int_cst_equal (size, op01))
16122 return fold_build1_loc (loc, IMAGPART_EXPR, type,
16123 TREE_OPERAND (op00, 0));
16127 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
16128 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
16129 && type == TREE_TYPE (TREE_TYPE (subtype)))
16132 tree min_val = size_zero_node;
16133 sub = build_fold_indirect_ref_loc (loc, sub);
16134 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
16135 if (type_domain && TYPE_MIN_VALUE (type_domain))
16136 min_val = TYPE_MIN_VALUE (type_domain);
16137 op0 = build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
16138 SET_EXPR_LOCATION (op0, loc);
16145 /* Builds an expression for an indirection through T, simplifying some
16149 build_fold_indirect_ref_loc (location_t loc, tree t)
16151 tree type = TREE_TYPE (TREE_TYPE (t));
16152 tree sub = fold_indirect_ref_1 (loc, type, t);
16157 t = build1 (INDIRECT_REF, type, t);
16158 SET_EXPR_LOCATION (t, loc);
16162 /* Given an INDIRECT_REF T, return either T or a simplified version. */
16165 fold_indirect_ref_loc (location_t loc, tree t)
16167 tree sub = fold_indirect_ref_1 (loc, TREE_TYPE (t), TREE_OPERAND (t, 0));
16175 /* Strip non-trapping, non-side-effecting tree nodes from an expression
16176 whose result is ignored. The type of the returned tree need not be
16177 the same as the original expression. */
16180 fold_ignored_result (tree t)
16182 if (!TREE_SIDE_EFFECTS (t))
16183 return integer_zero_node;
16186 switch (TREE_CODE_CLASS (TREE_CODE (t)))
16189 t = TREE_OPERAND (t, 0);
16193 case tcc_comparison:
16194 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16195 t = TREE_OPERAND (t, 0);
16196 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
16197 t = TREE_OPERAND (t, 1);
16202 case tcc_expression:
16203 switch (TREE_CODE (t))
16205 case COMPOUND_EXPR:
16206 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
16208 t = TREE_OPERAND (t, 0);
16212 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
16213 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
16215 t = TREE_OPERAND (t, 0);
16228 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
16229 This can only be applied to objects of a sizetype. */
16232 round_up_loc (location_t loc, tree value, int divisor)
16234 tree div = NULL_TREE;
16236 gcc_assert (divisor > 0);
16240 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16241 have to do anything. Only do this when we are not given a const,
16242 because in that case, this check is more expensive than just
16244 if (TREE_CODE (value) != INTEGER_CST)
16246 div = build_int_cst (TREE_TYPE (value), divisor);
16248 if (multiple_of_p (TREE_TYPE (value), value, div))
16252 /* If divisor is a power of two, simplify this to bit manipulation. */
16253 if (divisor == (divisor & -divisor))
16255 if (TREE_CODE (value) == INTEGER_CST)
16257 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (value);
16258 unsigned HOST_WIDE_INT high;
16261 if ((low & (divisor - 1)) == 0)
16264 overflow_p = TREE_OVERFLOW (value);
16265 high = TREE_INT_CST_HIGH (value);
16266 low &= ~(divisor - 1);
16275 return force_fit_type_double (TREE_TYPE (value), low, high,
16282 t = build_int_cst (TREE_TYPE (value), divisor - 1);
16283 value = size_binop_loc (loc, PLUS_EXPR, value, t);
16284 t = build_int_cst (TREE_TYPE (value), -divisor);
16285 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16291 div = build_int_cst (TREE_TYPE (value), divisor);
16292 value = size_binop_loc (loc, CEIL_DIV_EXPR, value, div);
16293 value = size_binop_loc (loc, MULT_EXPR, value, div);
16299 /* Likewise, but round down. */
16302 round_down_loc (location_t loc, tree value, int divisor)
16304 tree div = NULL_TREE;
16306 gcc_assert (divisor > 0);
16310 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
16311 have to do anything. Only do this when we are not given a const,
16312 because in that case, this check is more expensive than just
16314 if (TREE_CODE (value) != INTEGER_CST)
16316 div = build_int_cst (TREE_TYPE (value), divisor);
16318 if (multiple_of_p (TREE_TYPE (value), value, div))
16322 /* If divisor is a power of two, simplify this to bit manipulation. */
16323 if (divisor == (divisor & -divisor))
16327 t = build_int_cst (TREE_TYPE (value), -divisor);
16328 value = size_binop_loc (loc, BIT_AND_EXPR, value, t);
16333 div = build_int_cst (TREE_TYPE (value), divisor);
16334 value = size_binop_loc (loc, FLOOR_DIV_EXPR, value, div);
16335 value = size_binop_loc (loc, MULT_EXPR, value, div);
16341 /* Returns the pointer to the base of the object addressed by EXP and
16342 extracts the information about the offset of the access, storing it
16343 to PBITPOS and POFFSET. */
16346 split_address_to_core_and_offset (tree exp,
16347 HOST_WIDE_INT *pbitpos, tree *poffset)
16350 enum machine_mode mode;
16351 int unsignedp, volatilep;
16352 HOST_WIDE_INT bitsize;
16353 location_t loc = EXPR_LOCATION (exp);
16355 if (TREE_CODE (exp) == ADDR_EXPR)
16357 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
16358 poffset, &mode, &unsignedp, &volatilep,
16360 core = build_fold_addr_expr_loc (loc, core);
16366 *poffset = NULL_TREE;
16372 /* Returns true if addresses of E1 and E2 differ by a constant, false
16373 otherwise. If they do, E1 - E2 is stored in *DIFF. */
16376 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
16379 HOST_WIDE_INT bitpos1, bitpos2;
16380 tree toffset1, toffset2, tdiff, type;
16382 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
16383 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
16385 if (bitpos1 % BITS_PER_UNIT != 0
16386 || bitpos2 % BITS_PER_UNIT != 0
16387 || !operand_equal_p (core1, core2, 0))
16390 if (toffset1 && toffset2)
16392 type = TREE_TYPE (toffset1);
16393 if (type != TREE_TYPE (toffset2))
16394 toffset2 = fold_convert (type, toffset2);
16396 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
16397 if (!cst_and_fits_in_hwi (tdiff))
16400 *diff = int_cst_value (tdiff);
16402 else if (toffset1 || toffset2)
16404 /* If only one of the offsets is non-constant, the difference cannot
16411 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
16415 /* Simplify the floating point expression EXP when the sign of the
16416 result is not significant. Return NULL_TREE if no simplification
16420 fold_strip_sign_ops (tree exp)
16423 location_t loc = EXPR_LOCATION (exp);
16425 switch (TREE_CODE (exp))
16429 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
16430 return arg0 ? arg0 : TREE_OPERAND (exp, 0);
16434 if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
16436 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
16437 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16438 if (arg0 != NULL_TREE || arg1 != NULL_TREE)
16439 return fold_build2_loc (loc, TREE_CODE (exp), TREE_TYPE (exp),
16440 arg0 ? arg0 : TREE_OPERAND (exp, 0),
16441 arg1 ? arg1 : TREE_OPERAND (exp, 1));
16444 case COMPOUND_EXPR:
16445 arg0 = TREE_OPERAND (exp, 0);
16446 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16448 return fold_build2_loc (loc, COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
16452 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
16453 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
16455 return fold_build3_loc (loc,
16456 COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
16457 arg0 ? arg0 : TREE_OPERAND (exp, 1),
16458 arg1 ? arg1 : TREE_OPERAND (exp, 2));
16463 const enum built_in_function fcode = builtin_mathfn_code (exp);
16466 CASE_FLT_FN (BUILT_IN_COPYSIGN):
16467 /* Strip copysign function call, return the 1st argument. */
16468 arg0 = CALL_EXPR_ARG (exp, 0);
16469 arg1 = CALL_EXPR_ARG (exp, 1);
16470 return omit_one_operand_loc (loc, TREE_TYPE (exp), arg0, arg1);
16473 /* Strip sign ops from the argument of "odd" math functions. */
16474 if (negate_mathfn_p (fcode))
16476 arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
16478 return build_call_expr_loc (loc, get_callee_fndecl (exp), 1, arg0);