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
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"
65 #include "langhooks.h"
68 /* Nonzero if we are folding constants inside an initializer; zero
70 int folding_initializer = 0;
72 /* The following constants represent a bit based encoding of GCC's
73 comparison operators. This encoding simplifies transformations
74 on relational comparison operators, such as AND and OR. */
75 enum comparison_code {
94 static void encode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT, HOST_WIDE_INT);
95 static void decode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT *, HOST_WIDE_INT *);
96 static bool negate_mathfn_p (enum built_in_function);
97 static bool negate_expr_p (tree);
98 static tree negate_expr (tree);
99 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
100 static tree associate_trees (tree, tree, enum tree_code, tree);
101 static tree const_binop (enum tree_code, tree, tree, int);
102 static enum comparison_code comparison_to_compcode (enum tree_code);
103 static enum tree_code compcode_to_comparison (enum comparison_code);
104 static tree combine_comparisons (enum tree_code, enum tree_code,
105 enum tree_code, tree, tree, tree);
106 static int truth_value_p (enum tree_code);
107 static int operand_equal_for_comparison_p (tree, tree, tree);
108 static int twoval_comparison_p (tree, tree *, tree *, int *);
109 static tree eval_subst (tree, tree, tree, tree, tree);
110 static tree pedantic_omit_one_operand (tree, tree, tree);
111 static tree distribute_bit_expr (enum tree_code, tree, tree, tree);
112 static tree make_bit_field_ref (tree, tree, int, int, int);
113 static tree optimize_bit_field_compare (enum tree_code, tree, tree, tree);
114 static tree decode_field_reference (tree, HOST_WIDE_INT *, HOST_WIDE_INT *,
115 enum machine_mode *, int *, int *,
117 static int all_ones_mask_p (tree, int);
118 static tree sign_bit_p (tree, tree);
119 static int simple_operand_p (tree);
120 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
121 static tree range_predecessor (tree);
122 static tree range_successor (tree);
123 static tree make_range (tree, int *, tree *, tree *, bool *);
124 static tree build_range_check (tree, tree, int, tree, tree);
125 static int merge_ranges (int *, tree *, tree *, int, tree, tree, int, tree,
127 static tree fold_range_test (enum tree_code, tree, tree, tree);
128 static tree fold_cond_expr_with_comparison (tree, tree, tree, tree);
129 static tree unextend (tree, int, int, tree);
130 static tree fold_truthop (enum tree_code, tree, tree, tree);
131 static tree optimize_minmax_comparison (enum tree_code, tree, tree, tree);
132 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
133 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
134 static tree fold_binary_op_with_conditional_arg (enum tree_code, tree,
137 static bool fold_real_zero_addition_p (tree, tree, int);
138 static tree fold_mathfn_compare (enum built_in_function, enum tree_code,
140 static tree fold_inf_compare (enum tree_code, tree, tree, tree);
141 static tree fold_div_compare (enum tree_code, tree, tree, tree);
142 static bool reorder_operands_p (tree, tree);
143 static tree fold_negate_const (tree, tree);
144 static tree fold_not_const (tree, tree);
145 static tree fold_relational_const (enum tree_code, tree, tree, tree);
148 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
149 overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
150 and SUM1. Then this yields nonzero if overflow occurred during the
153 Overflow occurs if A and B have the same sign, but A and SUM differ in
154 sign. Use `^' to test whether signs differ, and `< 0' to isolate the
156 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
158 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
159 We do that by representing the two-word integer in 4 words, with only
160 HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
161 number. The value of the word is LOWPART + HIGHPART * BASE. */
164 ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
165 #define HIGHPART(x) \
166 ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
167 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
169 /* Unpack a two-word integer into 4 words.
170 LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
171 WORDS points to the array of HOST_WIDE_INTs. */
174 encode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
176 words[0] = LOWPART (low);
177 words[1] = HIGHPART (low);
178 words[2] = LOWPART (hi);
179 words[3] = HIGHPART (hi);
182 /* Pack an array of 4 words into a two-word integer.
183 WORDS points to the array of words.
184 The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */
187 decode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT *low,
190 *low = words[0] + words[1] * BASE;
191 *hi = words[2] + words[3] * BASE;
194 /* Force the double-word integer L1, H1 to be within the range of the
195 integer type TYPE. Stores the properly truncated and sign-extended
196 double-word integer in *LV, *HV. Returns true if the operation
197 overflows, that is, argument and result are different. */
200 fit_double_type (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
201 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, const_tree type)
203 unsigned HOST_WIDE_INT low0 = l1;
204 HOST_WIDE_INT high0 = h1;
206 int sign_extended_type;
208 if (POINTER_TYPE_P (type)
209 || TREE_CODE (type) == OFFSET_TYPE)
212 prec = TYPE_PRECISION (type);
214 /* Size types *are* sign extended. */
215 sign_extended_type = (!TYPE_UNSIGNED (type)
216 || (TREE_CODE (type) == INTEGER_TYPE
217 && TYPE_IS_SIZETYPE (type)));
219 /* First clear all bits that are beyond the type's precision. */
220 if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
222 else if (prec > HOST_BITS_PER_WIDE_INT)
223 h1 &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
227 if (prec < HOST_BITS_PER_WIDE_INT)
228 l1 &= ~((HOST_WIDE_INT) (-1) << prec);
231 /* Then do sign extension if necessary. */
232 if (!sign_extended_type)
233 /* No sign extension */;
234 else if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
235 /* Correct width already. */;
236 else if (prec > HOST_BITS_PER_WIDE_INT)
238 /* Sign extend top half? */
239 if (h1 & ((unsigned HOST_WIDE_INT)1
240 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
241 h1 |= (HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT);
243 else if (prec == HOST_BITS_PER_WIDE_INT)
245 if ((HOST_WIDE_INT)l1 < 0)
250 /* Sign extend bottom half? */
251 if (l1 & ((unsigned HOST_WIDE_INT)1 << (prec - 1)))
254 l1 |= (HOST_WIDE_INT)(-1) << prec;
261 /* If the value didn't fit, signal overflow. */
262 return l1 != low0 || h1 != high0;
265 /* We force the double-int HIGH:LOW to the range of the type TYPE by
266 sign or zero extending it.
267 OVERFLOWABLE indicates if we are interested
268 in overflow of the value, when >0 we are only interested in signed
269 overflow, for <0 we are interested in any overflow. OVERFLOWED
270 indicates whether overflow has already occurred. CONST_OVERFLOWED
271 indicates whether constant overflow has already occurred. We force
272 T's value to be within range of T's type (by setting to 0 or 1 all
273 the bits outside the type's range). We set TREE_OVERFLOWED if,
274 OVERFLOWED is nonzero,
275 or OVERFLOWABLE is >0 and signed overflow occurs
276 or OVERFLOWABLE is <0 and any overflow occurs
277 We return a new tree node for the extended double-int. The node
278 is shared if no overflow flags are set. */
281 force_fit_type_double (tree type, unsigned HOST_WIDE_INT low,
282 HOST_WIDE_INT high, int overflowable,
285 int sign_extended_type;
288 /* Size types *are* sign extended. */
289 sign_extended_type = (!TYPE_UNSIGNED (type)
290 || (TREE_CODE (type) == INTEGER_TYPE
291 && TYPE_IS_SIZETYPE (type)));
293 overflow = fit_double_type (low, high, &low, &high, type);
295 /* If we need to set overflow flags, return a new unshared node. */
296 if (overflowed || overflow)
300 || (overflowable > 0 && sign_extended_type))
302 tree t = make_node (INTEGER_CST);
303 TREE_INT_CST_LOW (t) = low;
304 TREE_INT_CST_HIGH (t) = high;
305 TREE_TYPE (t) = type;
306 TREE_OVERFLOW (t) = 1;
311 /* Else build a shared node. */
312 return build_int_cst_wide (type, low, high);
315 /* Add two doubleword integers with doubleword result.
316 Return nonzero if the operation overflows according to UNSIGNED_P.
317 Each argument is given as two `HOST_WIDE_INT' pieces.
318 One argument is L1 and H1; the other, L2 and H2.
319 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
322 add_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
323 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
324 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
327 unsigned HOST_WIDE_INT l;
331 h = h1 + h2 + (l < l1);
337 return (unsigned HOST_WIDE_INT) h < (unsigned HOST_WIDE_INT) h1;
339 return OVERFLOW_SUM_SIGN (h1, h2, h);
342 /* Negate a doubleword integer with doubleword result.
343 Return nonzero if the operation overflows, assuming it's signed.
344 The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
345 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
348 neg_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
349 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
355 return (*hv & h1) < 0;
365 /* Multiply two doubleword integers with doubleword result.
366 Return nonzero if the operation overflows according to UNSIGNED_P.
367 Each argument is given as two `HOST_WIDE_INT' pieces.
368 One argument is L1 and H1; the other, L2 and H2.
369 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
372 mul_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
373 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
374 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
377 HOST_WIDE_INT arg1[4];
378 HOST_WIDE_INT arg2[4];
379 HOST_WIDE_INT prod[4 * 2];
380 unsigned HOST_WIDE_INT carry;
382 unsigned HOST_WIDE_INT toplow, neglow;
383 HOST_WIDE_INT tophigh, neghigh;
385 encode (arg1, l1, h1);
386 encode (arg2, l2, h2);
388 memset (prod, 0, sizeof prod);
390 for (i = 0; i < 4; i++)
393 for (j = 0; j < 4; j++)
396 /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */
397 carry += arg1[i] * arg2[j];
398 /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */
400 prod[k] = LOWPART (carry);
401 carry = HIGHPART (carry);
406 decode (prod, lv, hv);
407 decode (prod + 4, &toplow, &tophigh);
409 /* Unsigned overflow is immediate. */
411 return (toplow | tophigh) != 0;
413 /* Check for signed overflow by calculating the signed representation of the
414 top half of the result; it should agree with the low half's sign bit. */
417 neg_double (l2, h2, &neglow, &neghigh);
418 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
422 neg_double (l1, h1, &neglow, &neghigh);
423 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
425 return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
428 /* Shift the doubleword integer in L1, H1 left by COUNT places
429 keeping only PREC bits of result.
430 Shift right if COUNT is negative.
431 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
432 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
435 lshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
436 HOST_WIDE_INT count, unsigned int prec,
437 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, int arith)
439 unsigned HOST_WIDE_INT signmask;
443 rshift_double (l1, h1, -count, prec, lv, hv, arith);
447 if (SHIFT_COUNT_TRUNCATED)
450 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
452 /* Shifting by the host word size is undefined according to the
453 ANSI standard, so we must handle this as a special case. */
457 else if (count >= HOST_BITS_PER_WIDE_INT)
459 *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
464 *hv = (((unsigned HOST_WIDE_INT) h1 << count)
465 | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
469 /* Sign extend all bits that are beyond the precision. */
471 signmask = -((prec > HOST_BITS_PER_WIDE_INT
472 ? ((unsigned HOST_WIDE_INT) *hv
473 >> (prec - HOST_BITS_PER_WIDE_INT - 1))
474 : (*lv >> (prec - 1))) & 1);
476 if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
478 else if (prec >= HOST_BITS_PER_WIDE_INT)
480 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
481 *hv |= signmask << (prec - HOST_BITS_PER_WIDE_INT);
486 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
487 *lv |= signmask << prec;
491 /* Shift the doubleword integer in L1, H1 right by COUNT places
492 keeping only PREC bits of result. COUNT must be positive.
493 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
494 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
497 rshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
498 HOST_WIDE_INT count, unsigned int prec,
499 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
502 unsigned HOST_WIDE_INT signmask;
505 ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
508 if (SHIFT_COUNT_TRUNCATED)
511 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
513 /* Shifting by the host word size is undefined according to the
514 ANSI standard, so we must handle this as a special case. */
518 else if (count >= HOST_BITS_PER_WIDE_INT)
521 *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
525 *hv = (unsigned HOST_WIDE_INT) h1 >> count;
527 | ((unsigned HOST_WIDE_INT) h1 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
530 /* Zero / sign extend all bits that are beyond the precision. */
532 if (count >= (HOST_WIDE_INT)prec)
537 else if ((prec - count) >= 2 * HOST_BITS_PER_WIDE_INT)
539 else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
541 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - count - HOST_BITS_PER_WIDE_INT));
542 *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
547 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << (prec - count));
548 *lv |= signmask << (prec - count);
552 /* Rotate the doubleword integer in L1, H1 left by COUNT places
553 keeping only PREC bits of result.
554 Rotate right if COUNT is negative.
555 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
558 lrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
559 HOST_WIDE_INT count, unsigned int prec,
560 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
562 unsigned HOST_WIDE_INT s1l, s2l;
563 HOST_WIDE_INT s1h, s2h;
569 lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
570 rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
575 /* Rotate the doubleword integer in L1, H1 left by COUNT places
576 keeping only PREC bits of result. COUNT must be positive.
577 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
580 rrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
581 HOST_WIDE_INT count, unsigned int prec,
582 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
584 unsigned HOST_WIDE_INT s1l, s2l;
585 HOST_WIDE_INT s1h, s2h;
591 rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
592 lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
597 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
598 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
599 CODE is a tree code for a kind of division, one of
600 TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
602 It controls how the quotient is rounded to an integer.
603 Return nonzero if the operation overflows.
604 UNS nonzero says do unsigned division. */
607 div_and_round_double (enum tree_code code, int uns,
608 unsigned HOST_WIDE_INT lnum_orig, /* num == numerator == dividend */
609 HOST_WIDE_INT hnum_orig,
610 unsigned HOST_WIDE_INT lden_orig, /* den == denominator == divisor */
611 HOST_WIDE_INT hden_orig,
612 unsigned HOST_WIDE_INT *lquo,
613 HOST_WIDE_INT *hquo, unsigned HOST_WIDE_INT *lrem,
617 HOST_WIDE_INT num[4 + 1]; /* extra element for scaling. */
618 HOST_WIDE_INT den[4], quo[4];
620 unsigned HOST_WIDE_INT work;
621 unsigned HOST_WIDE_INT carry = 0;
622 unsigned HOST_WIDE_INT lnum = lnum_orig;
623 HOST_WIDE_INT hnum = hnum_orig;
624 unsigned HOST_WIDE_INT lden = lden_orig;
625 HOST_WIDE_INT hden = hden_orig;
628 if (hden == 0 && lden == 0)
629 overflow = 1, lden = 1;
631 /* Calculate quotient sign and convert operands to unsigned. */
637 /* (minimum integer) / (-1) is the only overflow case. */
638 if (neg_double (lnum, hnum, &lnum, &hnum)
639 && ((HOST_WIDE_INT) lden & hden) == -1)
645 neg_double (lden, hden, &lden, &hden);
649 if (hnum == 0 && hden == 0)
650 { /* single precision */
652 /* This unsigned division rounds toward zero. */
658 { /* trivial case: dividend < divisor */
659 /* hden != 0 already checked. */
666 memset (quo, 0, sizeof quo);
668 memset (num, 0, sizeof num); /* to zero 9th element */
669 memset (den, 0, sizeof den);
671 encode (num, lnum, hnum);
672 encode (den, lden, hden);
674 /* Special code for when the divisor < BASE. */
675 if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
677 /* hnum != 0 already checked. */
678 for (i = 4 - 1; i >= 0; i--)
680 work = num[i] + carry * BASE;
681 quo[i] = work / lden;
687 /* Full double precision division,
688 with thanks to Don Knuth's "Seminumerical Algorithms". */
689 int num_hi_sig, den_hi_sig;
690 unsigned HOST_WIDE_INT quo_est, scale;
692 /* Find the highest nonzero divisor digit. */
693 for (i = 4 - 1;; i--)
700 /* Insure that the first digit of the divisor is at least BASE/2.
701 This is required by the quotient digit estimation algorithm. */
703 scale = BASE / (den[den_hi_sig] + 1);
705 { /* scale divisor and dividend */
707 for (i = 0; i <= 4 - 1; i++)
709 work = (num[i] * scale) + carry;
710 num[i] = LOWPART (work);
711 carry = HIGHPART (work);
716 for (i = 0; i <= 4 - 1; i++)
718 work = (den[i] * scale) + carry;
719 den[i] = LOWPART (work);
720 carry = HIGHPART (work);
721 if (den[i] != 0) den_hi_sig = i;
728 for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
730 /* Guess the next quotient digit, quo_est, by dividing the first
731 two remaining dividend digits by the high order quotient digit.
732 quo_est is never low and is at most 2 high. */
733 unsigned HOST_WIDE_INT tmp;
735 num_hi_sig = i + den_hi_sig + 1;
736 work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
737 if (num[num_hi_sig] != den[den_hi_sig])
738 quo_est = work / den[den_hi_sig];
742 /* Refine quo_est so it's usually correct, and at most one high. */
743 tmp = work - quo_est * den[den_hi_sig];
745 && (den[den_hi_sig - 1] * quo_est
746 > (tmp * BASE + num[num_hi_sig - 2])))
749 /* Try QUO_EST as the quotient digit, by multiplying the
750 divisor by QUO_EST and subtracting from the remaining dividend.
751 Keep in mind that QUO_EST is the I - 1st digit. */
754 for (j = 0; j <= den_hi_sig; j++)
756 work = quo_est * den[j] + carry;
757 carry = HIGHPART (work);
758 work = num[i + j] - LOWPART (work);
759 num[i + j] = LOWPART (work);
760 carry += HIGHPART (work) != 0;
763 /* If quo_est was high by one, then num[i] went negative and
764 we need to correct things. */
765 if (num[num_hi_sig] < (HOST_WIDE_INT) carry)
768 carry = 0; /* add divisor back in */
769 for (j = 0; j <= den_hi_sig; j++)
771 work = num[i + j] + den[j] + carry;
772 carry = HIGHPART (work);
773 num[i + j] = LOWPART (work);
776 num [num_hi_sig] += carry;
779 /* Store the quotient digit. */
784 decode (quo, lquo, hquo);
787 /* If result is negative, make it so. */
789 neg_double (*lquo, *hquo, lquo, hquo);
791 /* Compute trial remainder: rem = num - (quo * den) */
792 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
793 neg_double (*lrem, *hrem, lrem, hrem);
794 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
799 case TRUNC_MOD_EXPR: /* round toward zero */
800 case EXACT_DIV_EXPR: /* for this one, it shouldn't matter */
804 case FLOOR_MOD_EXPR: /* round toward negative infinity */
805 if (quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio < 0 && rem != 0 */
808 add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1,
816 case CEIL_MOD_EXPR: /* round toward positive infinity */
817 if (!quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio > 0 && rem != 0 */
819 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
827 case ROUND_MOD_EXPR: /* round to closest integer */
829 unsigned HOST_WIDE_INT labs_rem = *lrem;
830 HOST_WIDE_INT habs_rem = *hrem;
831 unsigned HOST_WIDE_INT labs_den = lden, ltwice;
832 HOST_WIDE_INT habs_den = hden, htwice;
834 /* Get absolute values. */
836 neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
838 neg_double (lden, hden, &labs_den, &habs_den);
840 /* If (2 * abs (lrem) >= abs (lden)) */
841 mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
842 labs_rem, habs_rem, <wice, &htwice);
844 if (((unsigned HOST_WIDE_INT) habs_den
845 < (unsigned HOST_WIDE_INT) htwice)
846 || (((unsigned HOST_WIDE_INT) habs_den
847 == (unsigned HOST_WIDE_INT) htwice)
848 && (labs_den < ltwice)))
852 add_double (*lquo, *hquo,
853 (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
856 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
868 /* Compute true remainder: rem = num - (quo * den) */
869 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
870 neg_double (*lrem, *hrem, lrem, hrem);
871 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
875 /* If ARG2 divides ARG1 with zero remainder, carries out the division
876 of type CODE and returns the quotient.
877 Otherwise returns NULL_TREE. */
880 div_if_zero_remainder (enum tree_code code, tree arg1, tree arg2)
882 unsigned HOST_WIDE_INT int1l, int2l;
883 HOST_WIDE_INT int1h, int2h;
884 unsigned HOST_WIDE_INT quol, reml;
885 HOST_WIDE_INT quoh, remh;
886 tree type = TREE_TYPE (arg1);
887 int uns = TYPE_UNSIGNED (type);
889 int1l = TREE_INT_CST_LOW (arg1);
890 int1h = TREE_INT_CST_HIGH (arg1);
891 /* &obj[0] + -128 really should be compiled as &obj[-8] rather than
892 &obj[some_exotic_number]. */
893 if (POINTER_TYPE_P (type))
896 type = signed_type_for (type);
897 fit_double_type (int1l, int1h, &int1l, &int1h,
901 fit_double_type (int1l, int1h, &int1l, &int1h, type);
902 int2l = TREE_INT_CST_LOW (arg2);
903 int2h = TREE_INT_CST_HIGH (arg2);
905 div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
906 &quol, &quoh, &reml, &remh);
907 if (remh != 0 || reml != 0)
910 return build_int_cst_wide (type, quol, quoh);
913 /* This is nonzero if we should defer warnings about undefined
914 overflow. This facility exists because these warnings are a
915 special case. The code to estimate loop iterations does not want
916 to issue any warnings, since it works with expressions which do not
917 occur in user code. Various bits of cleanup code call fold(), but
918 only use the result if it has certain characteristics (e.g., is a
919 constant); that code only wants to issue a warning if the result is
922 static int fold_deferring_overflow_warnings;
924 /* If a warning about undefined overflow is deferred, this is the
925 warning. Note that this may cause us to turn two warnings into
926 one, but that is fine since it is sufficient to only give one
927 warning per expression. */
929 static const char* fold_deferred_overflow_warning;
931 /* If a warning about undefined overflow is deferred, this is the
932 level at which the warning should be emitted. */
934 static enum warn_strict_overflow_code fold_deferred_overflow_code;
936 /* Start deferring overflow warnings. We could use a stack here to
937 permit nested calls, but at present it is not necessary. */
940 fold_defer_overflow_warnings (void)
942 ++fold_deferring_overflow_warnings;
945 /* Stop deferring overflow warnings. If there is a pending warning,
946 and ISSUE is true, then issue the warning if appropriate. STMT is
947 the statement with which the warning should be associated (used for
948 location information); STMT may be NULL. CODE is the level of the
949 warning--a warn_strict_overflow_code value. This function will use
950 the smaller of CODE and the deferred code when deciding whether to
951 issue the warning. CODE may be zero to mean to always use the
955 fold_undefer_overflow_warnings (bool issue, tree stmt, int code)
960 gcc_assert (fold_deferring_overflow_warnings > 0);
961 --fold_deferring_overflow_warnings;
962 if (fold_deferring_overflow_warnings > 0)
964 if (fold_deferred_overflow_warning != NULL
966 && code < (int) fold_deferred_overflow_code)
967 fold_deferred_overflow_code = code;
971 warnmsg = fold_deferred_overflow_warning;
972 fold_deferred_overflow_warning = NULL;
974 if (!issue || warnmsg == NULL)
977 /* Use the smallest code level when deciding to issue the
979 if (code == 0 || code > (int) fold_deferred_overflow_code)
980 code = fold_deferred_overflow_code;
982 if (!issue_strict_overflow_warning (code))
985 if (stmt == NULL_TREE || !expr_has_location (stmt))
986 locus = input_location;
988 locus = expr_location (stmt);
989 warning (OPT_Wstrict_overflow, "%H%s", &locus, warnmsg);
992 /* Stop deferring overflow warnings, ignoring any deferred
996 fold_undefer_and_ignore_overflow_warnings (void)
998 fold_undefer_overflow_warnings (false, NULL_TREE, 0);
1001 /* Whether we are deferring overflow warnings. */
1004 fold_deferring_overflow_warnings_p (void)
1006 return fold_deferring_overflow_warnings > 0;
1009 /* This is called when we fold something based on the fact that signed
1010 overflow is undefined. */
1013 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
1015 gcc_assert (!flag_wrapv && !flag_trapv);
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 (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 (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 (COMPLEX_EXPR, type,
1282 fold_negate_expr (TREE_OPERAND (t, 0)),
1283 fold_negate_expr (TREE_OPERAND (t, 1)));
1287 if (negate_expr_p (t))
1288 return fold_build1 (CONJ_EXPR, type,
1289 fold_negate_expr (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 (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 (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 (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 (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 (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 (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 (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 (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 (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 (ntype, TREE_OPERAND (t, 0));
1421 temp = fold_build2 (RSHIFT_EXPR, ntype, temp, op1);
1422 return fold_convert (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)
1446 type = TREE_TYPE (t);
1447 STRIP_SIGN_NOPS (t);
1449 tem = fold_negate_expr (t);
1451 tem = build1 (NEGATE_EXPR, TREE_TYPE (t), t);
1452 return fold_convert (type, tem);
1455 /* Split a tree IN into a constant, literal and variable parts that could be
1456 combined with CODE to make IN. "constant" means an expression with
1457 TREE_CONSTANT but that isn't an actual constant. CODE must be a
1458 commutative arithmetic operation. Store the constant part into *CONP,
1459 the literal in *LITP and return the variable part. If a part isn't
1460 present, set it to null. If the tree does not decompose in this way,
1461 return the entire tree as the variable part and the other parts as null.
1463 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
1464 case, we negate an operand that was subtracted. Except if it is a
1465 literal for which we use *MINUS_LITP instead.
1467 If NEGATE_P is true, we are negating all of IN, again except a literal
1468 for which we use *MINUS_LITP instead.
1470 If IN is itself a literal or constant, return it as appropriate.
1472 Note that we do not guarantee that any of the three values will be the
1473 same type as IN, but they will have the same signedness and mode. */
1476 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
1477 tree *minus_litp, int negate_p)
1485 /* Strip any conversions that don't change the machine mode or signedness. */
1486 STRIP_SIGN_NOPS (in);
1488 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST
1489 || TREE_CODE (in) == FIXED_CST)
1491 else if (TREE_CODE (in) == code
1492 || (! FLOAT_TYPE_P (TREE_TYPE (in))
1493 && ! SAT_FIXED_POINT_TYPE_P (TREE_TYPE (in))
1494 /* We can associate addition and subtraction together (even
1495 though the C standard doesn't say so) for integers because
1496 the value is not affected. For reals, the value might be
1497 affected, so we can't. */
1498 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1499 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1501 tree op0 = TREE_OPERAND (in, 0);
1502 tree op1 = TREE_OPERAND (in, 1);
1503 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1504 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1506 /* First see if either of the operands is a literal, then a constant. */
1507 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST
1508 || TREE_CODE (op0) == FIXED_CST)
1509 *litp = op0, op0 = 0;
1510 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST
1511 || TREE_CODE (op1) == FIXED_CST)
1512 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1514 if (op0 != 0 && TREE_CONSTANT (op0))
1515 *conp = op0, op0 = 0;
1516 else if (op1 != 0 && TREE_CONSTANT (op1))
1517 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1519 /* If we haven't dealt with either operand, this is not a case we can
1520 decompose. Otherwise, VAR is either of the ones remaining, if any. */
1521 if (op0 != 0 && op1 != 0)
1526 var = op1, neg_var_p = neg1_p;
1528 /* Now do any needed negations. */
1530 *minus_litp = *litp, *litp = 0;
1532 *conp = negate_expr (*conp);
1534 var = negate_expr (var);
1536 else if (TREE_CONSTANT (in))
1544 *minus_litp = *litp, *litp = 0;
1545 else if (*minus_litp)
1546 *litp = *minus_litp, *minus_litp = 0;
1547 *conp = negate_expr (*conp);
1548 var = negate_expr (var);
1554 /* Re-associate trees split by the above function. T1 and T2 are either
1555 expressions to associate or null. Return the new expression, if any. If
1556 we build an operation, do it in TYPE and with CODE. */
1559 associate_trees (tree t1, tree t2, enum tree_code code, tree type)
1566 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1567 try to fold this since we will have infinite recursion. But do
1568 deal with any NEGATE_EXPRs. */
1569 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1570 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1572 if (code == PLUS_EXPR)
1574 if (TREE_CODE (t1) == NEGATE_EXPR)
1575 return build2 (MINUS_EXPR, type, fold_convert (type, t2),
1576 fold_convert (type, TREE_OPERAND (t1, 0)));
1577 else if (TREE_CODE (t2) == NEGATE_EXPR)
1578 return build2 (MINUS_EXPR, type, fold_convert (type, t1),
1579 fold_convert (type, TREE_OPERAND (t2, 0)));
1580 else if (integer_zerop (t2))
1581 return fold_convert (type, t1);
1583 else if (code == MINUS_EXPR)
1585 if (integer_zerop (t2))
1586 return fold_convert (type, t1);
1589 return build2 (code, type, fold_convert (type, t1),
1590 fold_convert (type, t2));
1593 return fold_build2 (code, type, fold_convert (type, t1),
1594 fold_convert (type, t2));
1597 /* Check whether TYPE1 and TYPE2 are equivalent integer types, suitable
1598 for use in int_const_binop, size_binop and size_diffop. */
1601 int_binop_types_match_p (enum tree_code code, tree type1, tree type2)
1603 if (TREE_CODE (type1) != INTEGER_TYPE && !POINTER_TYPE_P (type1))
1605 if (TREE_CODE (type2) != INTEGER_TYPE && !POINTER_TYPE_P (type2))
1620 return TYPE_UNSIGNED (type1) == TYPE_UNSIGNED (type2)
1621 && TYPE_PRECISION (type1) == TYPE_PRECISION (type2)
1622 && TYPE_MODE (type1) == TYPE_MODE (type2);
1626 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1627 to produce a new constant. Return NULL_TREE if we don't know how
1628 to evaluate CODE at compile-time.
1630 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1633 int_const_binop (enum tree_code code, const_tree arg1, const_tree arg2, int notrunc)
1635 unsigned HOST_WIDE_INT int1l, int2l;
1636 HOST_WIDE_INT int1h, int2h;
1637 unsigned HOST_WIDE_INT low;
1639 unsigned HOST_WIDE_INT garbagel;
1640 HOST_WIDE_INT garbageh;
1642 tree type = TREE_TYPE (arg1);
1643 int uns = TYPE_UNSIGNED (type);
1645 = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
1648 int1l = TREE_INT_CST_LOW (arg1);
1649 int1h = TREE_INT_CST_HIGH (arg1);
1650 int2l = TREE_INT_CST_LOW (arg2);
1651 int2h = TREE_INT_CST_HIGH (arg2);
1656 low = int1l | int2l, hi = int1h | int2h;
1660 low = int1l ^ int2l, hi = int1h ^ int2h;
1664 low = int1l & int2l, hi = int1h & int2h;
1670 /* It's unclear from the C standard whether shifts can overflow.
1671 The following code ignores overflow; perhaps a C standard
1672 interpretation ruling is needed. */
1673 lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1680 lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1685 overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1689 neg_double (int2l, int2h, &low, &hi);
1690 add_double (int1l, int1h, low, hi, &low, &hi);
1691 overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
1695 overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1698 case TRUNC_DIV_EXPR:
1699 case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1700 case EXACT_DIV_EXPR:
1701 /* This is a shortcut for a common special case. */
1702 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1703 && !TREE_OVERFLOW (arg1)
1704 && !TREE_OVERFLOW (arg2)
1705 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1707 if (code == CEIL_DIV_EXPR)
1710 low = int1l / int2l, hi = 0;
1714 /* ... fall through ... */
1716 case ROUND_DIV_EXPR:
1717 if (int2h == 0 && int2l == 0)
1719 if (int2h == 0 && int2l == 1)
1721 low = int1l, hi = int1h;
1724 if (int1l == int2l && int1h == int2h
1725 && ! (int1l == 0 && int1h == 0))
1730 overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
1731 &low, &hi, &garbagel, &garbageh);
1734 case TRUNC_MOD_EXPR:
1735 case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1736 /* This is a shortcut for a common special case. */
1737 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1738 && !TREE_OVERFLOW (arg1)
1739 && !TREE_OVERFLOW (arg2)
1740 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1742 if (code == CEIL_MOD_EXPR)
1744 low = int1l % int2l, hi = 0;
1748 /* ... fall through ... */
1750 case ROUND_MOD_EXPR:
1751 if (int2h == 0 && int2l == 0)
1753 overflow = div_and_round_double (code, uns,
1754 int1l, int1h, int2l, int2h,
1755 &garbagel, &garbageh, &low, &hi);
1761 low = (((unsigned HOST_WIDE_INT) int1h
1762 < (unsigned HOST_WIDE_INT) int2h)
1763 || (((unsigned HOST_WIDE_INT) int1h
1764 == (unsigned HOST_WIDE_INT) int2h)
1767 low = (int1h < int2h
1768 || (int1h == int2h && int1l < int2l));
1770 if (low == (code == MIN_EXPR))
1771 low = int1l, hi = int1h;
1773 low = int2l, hi = int2h;
1782 t = build_int_cst_wide (TREE_TYPE (arg1), low, hi);
1784 /* Propagate overflow flags ourselves. */
1785 if (((!uns || is_sizetype) && overflow)
1786 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1789 TREE_OVERFLOW (t) = 1;
1793 t = force_fit_type_double (TREE_TYPE (arg1), low, hi, 1,
1794 ((!uns || is_sizetype) && overflow)
1795 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2));
1800 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1801 constant. We assume ARG1 and ARG2 have the same data type, or at least
1802 are the same kind of constant and the same machine mode. Return zero if
1803 combining the constants is not allowed in the current operating mode.
1805 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1808 const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1810 /* Sanity check for the recursive cases. */
1817 if (TREE_CODE (arg1) == INTEGER_CST)
1818 return int_const_binop (code, arg1, arg2, notrunc);
1820 if (TREE_CODE (arg1) == REAL_CST)
1822 enum machine_mode mode;
1825 REAL_VALUE_TYPE value;
1826 REAL_VALUE_TYPE result;
1830 /* The following codes are handled by real_arithmetic. */
1845 d1 = TREE_REAL_CST (arg1);
1846 d2 = TREE_REAL_CST (arg2);
1848 type = TREE_TYPE (arg1);
1849 mode = TYPE_MODE (type);
1851 /* Don't perform operation if we honor signaling NaNs and
1852 either operand is a NaN. */
1853 if (HONOR_SNANS (mode)
1854 && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1857 /* Don't perform operation if it would raise a division
1858 by zero exception. */
1859 if (code == RDIV_EXPR
1860 && REAL_VALUES_EQUAL (d2, dconst0)
1861 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1864 /* If either operand is a NaN, just return it. Otherwise, set up
1865 for floating-point trap; we return an overflow. */
1866 if (REAL_VALUE_ISNAN (d1))
1868 else if (REAL_VALUE_ISNAN (d2))
1871 inexact = real_arithmetic (&value, code, &d1, &d2);
1872 real_convert (&result, mode, &value);
1874 /* Don't constant fold this floating point operation if
1875 the result has overflowed and flag_trapping_math. */
1876 if (flag_trapping_math
1877 && MODE_HAS_INFINITIES (mode)
1878 && REAL_VALUE_ISINF (result)
1879 && !REAL_VALUE_ISINF (d1)
1880 && !REAL_VALUE_ISINF (d2))
1883 /* Don't constant fold this floating point operation if the
1884 result may dependent upon the run-time rounding mode and
1885 flag_rounding_math is set, or if GCC's software emulation
1886 is unable to accurately represent the result. */
1887 if ((flag_rounding_math
1888 || (REAL_MODE_FORMAT_COMPOSITE_P (mode)
1889 && !flag_unsafe_math_optimizations))
1890 && (inexact || !real_identical (&result, &value)))
1893 t = build_real (type, result);
1895 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1899 if (TREE_CODE (arg1) == FIXED_CST)
1901 FIXED_VALUE_TYPE f1;
1902 FIXED_VALUE_TYPE f2;
1903 FIXED_VALUE_TYPE result;
1908 /* The following codes are handled by fixed_arithmetic. */
1914 case TRUNC_DIV_EXPR:
1915 f2 = TREE_FIXED_CST (arg2);
1920 f2.data.high = TREE_INT_CST_HIGH (arg2);
1921 f2.data.low = TREE_INT_CST_LOW (arg2);
1929 f1 = TREE_FIXED_CST (arg1);
1930 type = TREE_TYPE (arg1);
1931 sat_p = TYPE_SATURATING (type);
1932 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1933 t = build_fixed (type, result);
1934 /* Propagate overflow flags. */
1935 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1937 TREE_OVERFLOW (t) = 1;
1938 TREE_CONSTANT_OVERFLOW (t) = 1;
1940 else if (TREE_CONSTANT_OVERFLOW (arg1) | TREE_CONSTANT_OVERFLOW (arg2))
1941 TREE_CONSTANT_OVERFLOW (t) = 1;
1945 if (TREE_CODE (arg1) == COMPLEX_CST)
1947 tree type = TREE_TYPE (arg1);
1948 tree r1 = TREE_REALPART (arg1);
1949 tree i1 = TREE_IMAGPART (arg1);
1950 tree r2 = TREE_REALPART (arg2);
1951 tree i2 = TREE_IMAGPART (arg2);
1958 real = const_binop (code, r1, r2, notrunc);
1959 imag = const_binop (code, i1, i2, notrunc);
1963 real = const_binop (MINUS_EXPR,
1964 const_binop (MULT_EXPR, r1, r2, notrunc),
1965 const_binop (MULT_EXPR, i1, i2, notrunc),
1967 imag = const_binop (PLUS_EXPR,
1968 const_binop (MULT_EXPR, r1, i2, notrunc),
1969 const_binop (MULT_EXPR, i1, r2, notrunc),
1976 = const_binop (PLUS_EXPR,
1977 const_binop (MULT_EXPR, r2, r2, notrunc),
1978 const_binop (MULT_EXPR, i2, i2, notrunc),
1981 = const_binop (PLUS_EXPR,
1982 const_binop (MULT_EXPR, r1, r2, notrunc),
1983 const_binop (MULT_EXPR, i1, i2, notrunc),
1986 = const_binop (MINUS_EXPR,
1987 const_binop (MULT_EXPR, i1, r2, notrunc),
1988 const_binop (MULT_EXPR, r1, i2, notrunc),
1991 if (INTEGRAL_TYPE_P (TREE_TYPE (r1)))
1992 code = TRUNC_DIV_EXPR;
1994 real = const_binop (code, t1, magsquared, notrunc);
1995 imag = const_binop (code, t2, magsquared, notrunc);
2004 return build_complex (type, real, imag);
2010 /* Create a size type INT_CST node with NUMBER sign extended. KIND
2011 indicates which particular sizetype to create. */
2014 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
2016 return build_int_cst (sizetype_tab[(int) kind], number);
2019 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
2020 is a tree code. The type of the result is taken from the operands.
2021 Both must be equivalent integer types, ala int_binop_types_match_p.
2022 If the operands are constant, so is the result. */
2025 size_binop (enum tree_code code, tree arg0, tree arg1)
2027 tree type = TREE_TYPE (arg0);
2029 if (arg0 == error_mark_node || arg1 == error_mark_node)
2030 return error_mark_node;
2032 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
2035 /* Handle the special case of two integer constants faster. */
2036 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2038 /* And some specific cases even faster than that. */
2039 if (code == PLUS_EXPR)
2041 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
2043 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2046 else if (code == MINUS_EXPR)
2048 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2051 else if (code == MULT_EXPR)
2053 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
2057 /* Handle general case of two integer constants. */
2058 return int_const_binop (code, arg0, arg1, 0);
2061 return fold_build2 (code, type, arg0, arg1);
2064 /* Given two values, either both of sizetype or both of bitsizetype,
2065 compute the difference between the two values. Return the value
2066 in signed type corresponding to the type of the operands. */
2069 size_diffop (tree arg0, tree arg1)
2071 tree type = TREE_TYPE (arg0);
2074 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
2077 /* If the type is already signed, just do the simple thing. */
2078 if (!TYPE_UNSIGNED (type))
2079 return size_binop (MINUS_EXPR, arg0, arg1);
2081 if (type == sizetype)
2083 else if (type == bitsizetype)
2084 ctype = sbitsizetype;
2086 ctype = signed_type_for (type);
2088 /* If either operand is not a constant, do the conversions to the signed
2089 type and subtract. The hardware will do the right thing with any
2090 overflow in the subtraction. */
2091 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2092 return size_binop (MINUS_EXPR, fold_convert (ctype, arg0),
2093 fold_convert (ctype, arg1));
2095 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2096 Otherwise, subtract the other way, convert to CTYPE (we know that can't
2097 overflow) and negate (which can't either). Special-case a result
2098 of zero while we're here. */
2099 if (tree_int_cst_equal (arg0, arg1))
2100 return build_int_cst (ctype, 0);
2101 else if (tree_int_cst_lt (arg1, arg0))
2102 return fold_convert (ctype, size_binop (MINUS_EXPR, arg0, arg1));
2104 return size_binop (MINUS_EXPR, build_int_cst (ctype, 0),
2105 fold_convert (ctype, size_binop (MINUS_EXPR,
2109 /* A subroutine of fold_convert_const handling conversions of an
2110 INTEGER_CST to another integer type. */
2113 fold_convert_const_int_from_int (tree type, tree arg1)
2117 /* Given an integer constant, make new constant with new type,
2118 appropriately sign-extended or truncated. */
2119 t = force_fit_type_double (type, TREE_INT_CST_LOW (arg1),
2120 TREE_INT_CST_HIGH (arg1),
2121 /* Don't set the overflow when
2122 converting a pointer */
2123 !POINTER_TYPE_P (TREE_TYPE (arg1)),
2124 (TREE_INT_CST_HIGH (arg1) < 0
2125 && (TYPE_UNSIGNED (type)
2126 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2127 | TREE_OVERFLOW (arg1));
2132 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2133 to an integer type. */
2136 fold_convert_const_int_from_real (enum tree_code code, tree type, tree arg1)
2141 /* The following code implements the floating point to integer
2142 conversion rules required by the Java Language Specification,
2143 that IEEE NaNs are mapped to zero and values that overflow
2144 the target precision saturate, i.e. values greater than
2145 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2146 are mapped to INT_MIN. These semantics are allowed by the
2147 C and C++ standards that simply state that the behavior of
2148 FP-to-integer conversion is unspecified upon overflow. */
2150 HOST_WIDE_INT high, low;
2152 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2156 case FIX_TRUNC_EXPR:
2157 real_trunc (&r, VOIDmode, &x);
2164 /* If R is NaN, return zero and show we have an overflow. */
2165 if (REAL_VALUE_ISNAN (r))
2172 /* See if R is less than the lower bound or greater than the
2177 tree lt = TYPE_MIN_VALUE (type);
2178 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2179 if (REAL_VALUES_LESS (r, l))
2182 high = TREE_INT_CST_HIGH (lt);
2183 low = TREE_INT_CST_LOW (lt);
2189 tree ut = TYPE_MAX_VALUE (type);
2192 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2193 if (REAL_VALUES_LESS (u, r))
2196 high = TREE_INT_CST_HIGH (ut);
2197 low = TREE_INT_CST_LOW (ut);
2203 REAL_VALUE_TO_INT (&low, &high, r);
2205 t = force_fit_type_double (type, low, high, -1,
2206 overflow | TREE_OVERFLOW (arg1));
2210 /* A subroutine of fold_convert_const handling conversions of a
2211 FIXED_CST to an integer type. */
2214 fold_convert_const_int_from_fixed (tree type, tree arg1)
2217 double_int temp, temp_trunc;
2220 /* Right shift FIXED_CST to temp by fbit. */
2221 temp = TREE_FIXED_CST (arg1).data;
2222 mode = TREE_FIXED_CST (arg1).mode;
2223 if (GET_MODE_FBIT (mode) < 2 * HOST_BITS_PER_WIDE_INT)
2225 lshift_double (temp.low, temp.high,
2226 - GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2227 &temp.low, &temp.high, SIGNED_FIXED_POINT_MODE_P (mode));
2229 /* Left shift temp to temp_trunc by fbit. */
2230 lshift_double (temp.low, temp.high,
2231 GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2232 &temp_trunc.low, &temp_trunc.high,
2233 SIGNED_FIXED_POINT_MODE_P (mode));
2240 temp_trunc.high = 0;
2243 /* If FIXED_CST is negative, we need to round the value toward 0.
2244 By checking if the fractional bits are not zero to add 1 to temp. */
2245 if (SIGNED_FIXED_POINT_MODE_P (mode) && temp_trunc.high < 0
2246 && !double_int_equal_p (TREE_FIXED_CST (arg1).data, temp_trunc))
2251 temp = double_int_add (temp, one);
2254 /* Given a fixed-point constant, make new constant with new type,
2255 appropriately sign-extended or truncated. */
2256 t = force_fit_type_double (type, temp.low, temp.high, -1,
2258 && (TYPE_UNSIGNED (type)
2259 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2260 | TREE_OVERFLOW (arg1));
2265 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2266 to another floating point type. */
2269 fold_convert_const_real_from_real (tree type, tree arg1)
2271 REAL_VALUE_TYPE value;
2274 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2275 t = build_real (type, value);
2277 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2281 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2282 to a floating point type. */
2285 fold_convert_const_real_from_fixed (tree type, tree arg1)
2287 REAL_VALUE_TYPE value;
2290 real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
2291 t = build_real (type, value);
2293 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2294 TREE_CONSTANT_OVERFLOW (t)
2295 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
2299 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2300 to another fixed-point type. */
2303 fold_convert_const_fixed_from_fixed (tree type, tree arg1)
2305 FIXED_VALUE_TYPE value;
2309 overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2310 TYPE_SATURATING (type));
2311 t = build_fixed (type, value);
2313 /* Propagate overflow flags. */
2314 if (overflow_p | TREE_OVERFLOW (arg1))
2316 TREE_OVERFLOW (t) = 1;
2317 TREE_CONSTANT_OVERFLOW (t) = 1;
2319 else if (TREE_CONSTANT_OVERFLOW (arg1))
2320 TREE_CONSTANT_OVERFLOW (t) = 1;
2324 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2325 to a fixed-point type. */
2328 fold_convert_const_fixed_from_int (tree type, tree arg1)
2330 FIXED_VALUE_TYPE value;
2334 overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type),
2335 TREE_INT_CST (arg1),
2336 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2337 TYPE_SATURATING (type));
2338 t = build_fixed (type, value);
2340 /* Propagate overflow flags. */
2341 if (overflow_p | TREE_OVERFLOW (arg1))
2343 TREE_OVERFLOW (t) = 1;
2344 TREE_CONSTANT_OVERFLOW (t) = 1;
2346 else if (TREE_CONSTANT_OVERFLOW (arg1))
2347 TREE_CONSTANT_OVERFLOW (t) = 1;
2351 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2352 to a fixed-point type. */
2355 fold_convert_const_fixed_from_real (tree type, tree arg1)
2357 FIXED_VALUE_TYPE value;
2361 overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2362 &TREE_REAL_CST (arg1),
2363 TYPE_SATURATING (type));
2364 t = build_fixed (type, value);
2366 /* Propagate overflow flags. */
2367 if (overflow_p | TREE_OVERFLOW (arg1))
2369 TREE_OVERFLOW (t) = 1;
2370 TREE_CONSTANT_OVERFLOW (t) = 1;
2372 else if (TREE_CONSTANT_OVERFLOW (arg1))
2373 TREE_CONSTANT_OVERFLOW (t) = 1;
2377 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2378 type TYPE. If no simplification can be done return NULL_TREE. */
2381 fold_convert_const (enum tree_code code, tree type, tree arg1)
2383 if (TREE_TYPE (arg1) == type)
2386 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
2388 if (TREE_CODE (arg1) == INTEGER_CST)
2389 return fold_convert_const_int_from_int (type, arg1);
2390 else if (TREE_CODE (arg1) == REAL_CST)
2391 return fold_convert_const_int_from_real (code, type, arg1);
2392 else if (TREE_CODE (arg1) == FIXED_CST)
2393 return fold_convert_const_int_from_fixed (type, arg1);
2395 else if (TREE_CODE (type) == REAL_TYPE)
2397 if (TREE_CODE (arg1) == INTEGER_CST)
2398 return build_real_from_int_cst (type, arg1);
2399 else if (TREE_CODE (arg1) == REAL_CST)
2400 return fold_convert_const_real_from_real (type, arg1);
2401 else if (TREE_CODE (arg1) == FIXED_CST)
2402 return fold_convert_const_real_from_fixed (type, arg1);
2404 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2406 if (TREE_CODE (arg1) == FIXED_CST)
2407 return fold_convert_const_fixed_from_fixed (type, arg1);
2408 else if (TREE_CODE (arg1) == INTEGER_CST)
2409 return fold_convert_const_fixed_from_int (type, arg1);
2410 else if (TREE_CODE (arg1) == REAL_CST)
2411 return fold_convert_const_fixed_from_real (type, arg1);
2416 /* Construct a vector of zero elements of vector type TYPE. */
2419 build_zero_vector (tree type)
2424 elem = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2425 units = TYPE_VECTOR_SUBPARTS (type);
2428 for (i = 0; i < units; i++)
2429 list = tree_cons (NULL_TREE, elem, list);
2430 return build_vector (type, list);
2433 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2436 fold_convertible_p (const_tree type, const_tree arg)
2438 tree orig = TREE_TYPE (arg);
2443 if (TREE_CODE (arg) == ERROR_MARK
2444 || TREE_CODE (type) == ERROR_MARK
2445 || TREE_CODE (orig) == ERROR_MARK)
2448 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2451 switch (TREE_CODE (type))
2453 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2454 case POINTER_TYPE: case REFERENCE_TYPE:
2456 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2457 || TREE_CODE (orig) == OFFSET_TYPE)
2459 return (TREE_CODE (orig) == VECTOR_TYPE
2460 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2463 return TREE_CODE (type) == TREE_CODE (orig);
2467 /* Convert expression ARG to type TYPE. Used by the middle-end for
2468 simple conversions in preference to calling the front-end's convert. */
2471 fold_convert (tree type, tree arg)
2473 tree orig = TREE_TYPE (arg);
2479 if (TREE_CODE (arg) == ERROR_MARK
2480 || TREE_CODE (type) == ERROR_MARK
2481 || TREE_CODE (orig) == ERROR_MARK)
2482 return error_mark_node;
2484 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2485 return fold_build1 (NOP_EXPR, type, arg);
2487 switch (TREE_CODE (type))
2489 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2490 case POINTER_TYPE: case REFERENCE_TYPE:
2492 if (TREE_CODE (arg) == INTEGER_CST)
2494 tem = fold_convert_const (NOP_EXPR, type, arg);
2495 if (tem != NULL_TREE)
2498 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2499 || TREE_CODE (orig) == OFFSET_TYPE)
2500 return fold_build1 (NOP_EXPR, type, arg);
2501 if (TREE_CODE (orig) == COMPLEX_TYPE)
2503 tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2504 return fold_convert (type, tem);
2506 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2507 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2508 return fold_build1 (NOP_EXPR, type, arg);
2511 if (TREE_CODE (arg) == INTEGER_CST)
2513 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2514 if (tem != NULL_TREE)
2517 else if (TREE_CODE (arg) == REAL_CST)
2519 tem = fold_convert_const (NOP_EXPR, type, arg);
2520 if (tem != NULL_TREE)
2523 else if (TREE_CODE (arg) == FIXED_CST)
2525 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2526 if (tem != NULL_TREE)
2530 switch (TREE_CODE (orig))
2533 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2534 case POINTER_TYPE: case REFERENCE_TYPE:
2535 return fold_build1 (FLOAT_EXPR, type, arg);
2538 return fold_build1 (NOP_EXPR, type, arg);
2540 case FIXED_POINT_TYPE:
2541 return fold_build1 (FIXED_CONVERT_EXPR, type, arg);
2544 tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2545 return fold_convert (type, tem);
2551 case FIXED_POINT_TYPE:
2552 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2553 || TREE_CODE (arg) == REAL_CST)
2555 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2556 if (tem != NULL_TREE)
2560 switch (TREE_CODE (orig))
2562 case FIXED_POINT_TYPE:
2567 return fold_build1 (FIXED_CONVERT_EXPR, type, arg);
2570 tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2571 return fold_convert (type, tem);
2578 switch (TREE_CODE (orig))
2581 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2582 case POINTER_TYPE: case REFERENCE_TYPE:
2584 case FIXED_POINT_TYPE:
2585 return build2 (COMPLEX_EXPR, type,
2586 fold_convert (TREE_TYPE (type), arg),
2587 fold_convert (TREE_TYPE (type), integer_zero_node));
2592 if (TREE_CODE (arg) == COMPLEX_EXPR)
2594 rpart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 0));
2595 ipart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 1));
2596 return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2599 arg = save_expr (arg);
2600 rpart = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2601 ipart = fold_build1 (IMAGPART_EXPR, TREE_TYPE (orig), arg);
2602 rpart = fold_convert (TREE_TYPE (type), rpart);
2603 ipart = fold_convert (TREE_TYPE (type), ipart);
2604 return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2612 if (integer_zerop (arg))
2613 return build_zero_vector (type);
2614 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2615 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2616 || TREE_CODE (orig) == VECTOR_TYPE);
2617 return fold_build1 (VIEW_CONVERT_EXPR, type, arg);
2620 tem = fold_ignored_result (arg);
2621 if (TREE_CODE (tem) == GIMPLE_MODIFY_STMT)
2623 return fold_build1 (NOP_EXPR, type, tem);
2630 /* Return false if expr can be assumed not to be an lvalue, true
2634 maybe_lvalue_p (tree x)
2636 /* We only need to wrap lvalue tree codes. */
2637 switch (TREE_CODE (x))
2648 case ALIGN_INDIRECT_REF:
2649 case MISALIGNED_INDIRECT_REF:
2651 case ARRAY_RANGE_REF:
2657 case PREINCREMENT_EXPR:
2658 case PREDECREMENT_EXPR:
2660 case TRY_CATCH_EXPR:
2661 case WITH_CLEANUP_EXPR:
2664 case GIMPLE_MODIFY_STMT:
2673 /* Assume the worst for front-end tree codes. */
2674 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2682 /* Return an expr equal to X but certainly not valid as an lvalue. */
2687 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2692 if (! maybe_lvalue_p (x))
2694 return build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2697 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2698 Zero means allow extended lvalues. */
2700 int pedantic_lvalues;
2702 /* When pedantic, return an expr equal to X but certainly not valid as a
2703 pedantic lvalue. Otherwise, return X. */
2706 pedantic_non_lvalue (tree x)
2708 if (pedantic_lvalues)
2709 return non_lvalue (x);
2714 /* Given a tree comparison code, return the code that is the logical inverse
2715 of the given code. It is not safe to do this for floating-point
2716 comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2717 as well: if reversing the comparison is unsafe, return ERROR_MARK. */
2720 invert_tree_comparison (enum tree_code code, bool honor_nans)
2722 if (honor_nans && flag_trapping_math)
2732 return honor_nans ? UNLE_EXPR : LE_EXPR;
2734 return honor_nans ? UNLT_EXPR : LT_EXPR;
2736 return honor_nans ? UNGE_EXPR : GE_EXPR;
2738 return honor_nans ? UNGT_EXPR : GT_EXPR;
2752 return UNORDERED_EXPR;
2753 case UNORDERED_EXPR:
2754 return ORDERED_EXPR;
2760 /* Similar, but return the comparison that results if the operands are
2761 swapped. This is safe for floating-point. */
2764 swap_tree_comparison (enum tree_code code)
2771 case UNORDERED_EXPR:
2797 /* Convert a comparison tree code from an enum tree_code representation
2798 into a compcode bit-based encoding. This function is the inverse of
2799 compcode_to_comparison. */
2801 static enum comparison_code
2802 comparison_to_compcode (enum tree_code code)
2819 return COMPCODE_ORD;
2820 case UNORDERED_EXPR:
2821 return COMPCODE_UNORD;
2823 return COMPCODE_UNLT;
2825 return COMPCODE_UNEQ;
2827 return COMPCODE_UNLE;
2829 return COMPCODE_UNGT;
2831 return COMPCODE_LTGT;
2833 return COMPCODE_UNGE;
2839 /* Convert a compcode bit-based encoding of a comparison operator back
2840 to GCC's enum tree_code representation. This function is the
2841 inverse of comparison_to_compcode. */
2843 static enum tree_code
2844 compcode_to_comparison (enum comparison_code code)
2861 return ORDERED_EXPR;
2862 case COMPCODE_UNORD:
2863 return UNORDERED_EXPR;
2881 /* Return a tree for the comparison which is the combination of
2882 doing the AND or OR (depending on CODE) of the two operations LCODE
2883 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2884 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2885 if this makes the transformation invalid. */
2888 combine_comparisons (enum tree_code code, enum tree_code lcode,
2889 enum tree_code rcode, tree truth_type,
2890 tree ll_arg, tree lr_arg)
2892 bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2893 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2894 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2895 enum comparison_code compcode;
2899 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2900 compcode = lcompcode & rcompcode;
2903 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2904 compcode = lcompcode | rcompcode;
2913 /* Eliminate unordered comparisons, as well as LTGT and ORD
2914 which are not used unless the mode has NaNs. */
2915 compcode &= ~COMPCODE_UNORD;
2916 if (compcode == COMPCODE_LTGT)
2917 compcode = COMPCODE_NE;
2918 else if (compcode == COMPCODE_ORD)
2919 compcode = COMPCODE_TRUE;
2921 else if (flag_trapping_math)
2923 /* Check that the original operation and the optimized ones will trap
2924 under the same condition. */
2925 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2926 && (lcompcode != COMPCODE_EQ)
2927 && (lcompcode != COMPCODE_ORD);
2928 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2929 && (rcompcode != COMPCODE_EQ)
2930 && (rcompcode != COMPCODE_ORD);
2931 bool trap = (compcode & COMPCODE_UNORD) == 0
2932 && (compcode != COMPCODE_EQ)
2933 && (compcode != COMPCODE_ORD);
2935 /* In a short-circuited boolean expression the LHS might be
2936 such that the RHS, if evaluated, will never trap. For
2937 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2938 if neither x nor y is NaN. (This is a mixed blessing: for
2939 example, the expression above will never trap, hence
2940 optimizing it to x < y would be invalid). */
2941 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2942 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2945 /* If the comparison was short-circuited, and only the RHS
2946 trapped, we may now generate a spurious trap. */
2948 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2951 /* If we changed the conditions that cause a trap, we lose. */
2952 if ((ltrap || rtrap) != trap)
2956 if (compcode == COMPCODE_TRUE)
2957 return constant_boolean_node (true, truth_type);
2958 else if (compcode == COMPCODE_FALSE)
2959 return constant_boolean_node (false, truth_type);
2961 return fold_build2 (compcode_to_comparison (compcode),
2962 truth_type, ll_arg, lr_arg);
2965 /* Return nonzero if CODE is a tree code that represents a truth value. */
2968 truth_value_p (enum tree_code code)
2970 return (TREE_CODE_CLASS (code) == tcc_comparison
2971 || code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
2972 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
2973 || code == TRUTH_XOR_EXPR || code == TRUTH_NOT_EXPR);
2976 /* Return nonzero if two operands (typically of the same tree node)
2977 are necessarily equal. If either argument has side-effects this
2978 function returns zero. FLAGS modifies behavior as follows:
2980 If OEP_ONLY_CONST is set, only return nonzero for constants.
2981 This function tests whether the operands are indistinguishable;
2982 it does not test whether they are equal using C's == operation.
2983 The distinction is important for IEEE floating point, because
2984 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2985 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2987 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2988 even though it may hold multiple values during a function.
2989 This is because a GCC tree node guarantees that nothing else is
2990 executed between the evaluation of its "operands" (which may often
2991 be evaluated in arbitrary order). Hence if the operands themselves
2992 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2993 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2994 unset means assuming isochronic (or instantaneous) tree equivalence.
2995 Unless comparing arbitrary expression trees, such as from different
2996 statements, this flag can usually be left unset.
2998 If OEP_PURE_SAME is set, then pure functions with identical arguments
2999 are considered the same. It is used when the caller has other ways
3000 to ensure that global memory is unchanged in between. */
3003 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
3005 /* If either is ERROR_MARK, they aren't equal. */
3006 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK)
3009 /* If both types don't have the same signedness, then we can't consider
3010 them equal. We must check this before the STRIP_NOPS calls
3011 because they may change the signedness of the arguments. */
3012 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3015 /* If both types don't have the same precision, then it is not safe
3017 if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
3023 /* In case both args are comparisons but with different comparison
3024 code, try to swap the comparison operands of one arg to produce
3025 a match and compare that variant. */
3026 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3027 && COMPARISON_CLASS_P (arg0)
3028 && COMPARISON_CLASS_P (arg1))
3030 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3032 if (TREE_CODE (arg0) == swap_code)
3033 return operand_equal_p (TREE_OPERAND (arg0, 0),
3034 TREE_OPERAND (arg1, 1), flags)
3035 && operand_equal_p (TREE_OPERAND (arg0, 1),
3036 TREE_OPERAND (arg1, 0), flags);
3039 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3040 /* This is needed for conversions and for COMPONENT_REF.
3041 Might as well play it safe and always test this. */
3042 || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
3043 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
3044 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
3047 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3048 We don't care about side effects in that case because the SAVE_EXPR
3049 takes care of that for us. In all other cases, two expressions are
3050 equal if they have no side effects. If we have two identical
3051 expressions with side effects that should be treated the same due
3052 to the only side effects being identical SAVE_EXPR's, that will
3053 be detected in the recursive calls below. */
3054 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3055 && (TREE_CODE (arg0) == SAVE_EXPR
3056 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3059 /* Next handle constant cases, those for which we can return 1 even
3060 if ONLY_CONST is set. */
3061 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3062 switch (TREE_CODE (arg0))
3065 return tree_int_cst_equal (arg0, arg1);
3068 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3069 TREE_FIXED_CST (arg1));
3072 if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
3073 TREE_REAL_CST (arg1)))
3077 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
3079 /* If we do not distinguish between signed and unsigned zero,
3080 consider them equal. */
3081 if (real_zerop (arg0) && real_zerop (arg1))
3090 v1 = TREE_VECTOR_CST_ELTS (arg0);
3091 v2 = TREE_VECTOR_CST_ELTS (arg1);
3094 if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
3097 v1 = TREE_CHAIN (v1);
3098 v2 = TREE_CHAIN (v2);
3105 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3107 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3111 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3112 && ! memcmp (TREE_STRING_POINTER (arg0),
3113 TREE_STRING_POINTER (arg1),
3114 TREE_STRING_LENGTH (arg0)));
3117 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3123 if (flags & OEP_ONLY_CONST)
3126 /* Define macros to test an operand from arg0 and arg1 for equality and a
3127 variant that allows null and views null as being different from any
3128 non-null value. In the latter case, if either is null, the both
3129 must be; otherwise, do the normal comparison. */
3130 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3131 TREE_OPERAND (arg1, N), flags)
3133 #define OP_SAME_WITH_NULL(N) \
3134 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3135 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3137 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3140 /* Two conversions are equal only if signedness and modes match. */
3141 switch (TREE_CODE (arg0))
3145 case FIX_TRUNC_EXPR:
3146 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3147 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3157 case tcc_comparison:
3159 if (OP_SAME (0) && OP_SAME (1))
3162 /* For commutative ops, allow the other order. */
3163 return (commutative_tree_code (TREE_CODE (arg0))
3164 && operand_equal_p (TREE_OPERAND (arg0, 0),
3165 TREE_OPERAND (arg1, 1), flags)
3166 && operand_equal_p (TREE_OPERAND (arg0, 1),
3167 TREE_OPERAND (arg1, 0), flags));
3170 /* If either of the pointer (or reference) expressions we are
3171 dereferencing contain a side effect, these cannot be equal. */
3172 if (TREE_SIDE_EFFECTS (arg0)
3173 || TREE_SIDE_EFFECTS (arg1))
3176 switch (TREE_CODE (arg0))
3179 case ALIGN_INDIRECT_REF:
3180 case MISALIGNED_INDIRECT_REF:
3186 case ARRAY_RANGE_REF:
3187 /* Operands 2 and 3 may be null.
3188 Compare the array index by value if it is constant first as we
3189 may have different types but same value here. */
3191 && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3192 TREE_OPERAND (arg1, 1))
3194 && OP_SAME_WITH_NULL (2)
3195 && OP_SAME_WITH_NULL (3));
3198 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3199 may be NULL when we're called to compare MEM_EXPRs. */
3200 return OP_SAME_WITH_NULL (0)
3202 && OP_SAME_WITH_NULL (2);
3205 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3211 case tcc_expression:
3212 switch (TREE_CODE (arg0))
3215 case TRUTH_NOT_EXPR:
3218 case TRUTH_ANDIF_EXPR:
3219 case TRUTH_ORIF_EXPR:
3220 return OP_SAME (0) && OP_SAME (1);
3222 case TRUTH_AND_EXPR:
3224 case TRUTH_XOR_EXPR:
3225 if (OP_SAME (0) && OP_SAME (1))
3228 /* Otherwise take into account this is a commutative operation. */
3229 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3230 TREE_OPERAND (arg1, 1), flags)
3231 && operand_equal_p (TREE_OPERAND (arg0, 1),
3232 TREE_OPERAND (arg1, 0), flags));
3239 switch (TREE_CODE (arg0))
3242 /* If the CALL_EXPRs call different functions, then they
3243 clearly can not be equal. */
3244 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3249 unsigned int cef = call_expr_flags (arg0);
3250 if (flags & OEP_PURE_SAME)
3251 cef &= ECF_CONST | ECF_PURE;
3258 /* Now see if all the arguments are the same. */
3260 const_call_expr_arg_iterator iter0, iter1;
3262 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3263 a1 = first_const_call_expr_arg (arg1, &iter1);
3265 a0 = next_const_call_expr_arg (&iter0),
3266 a1 = next_const_call_expr_arg (&iter1))
3267 if (! operand_equal_p (a0, a1, flags))
3270 /* If we get here and both argument lists are exhausted
3271 then the CALL_EXPRs are equal. */
3272 return ! (a0 || a1);
3278 case tcc_declaration:
3279 /* Consider __builtin_sqrt equal to sqrt. */
3280 return (TREE_CODE (arg0) == FUNCTION_DECL
3281 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3282 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3283 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3290 #undef OP_SAME_WITH_NULL
3293 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3294 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3296 When in doubt, return 0. */
3299 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3301 int unsignedp1, unsignedpo;
3302 tree primarg0, primarg1, primother;
3303 unsigned int correct_width;
3305 if (operand_equal_p (arg0, arg1, 0))
3308 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3309 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3312 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3313 and see if the inner values are the same. This removes any
3314 signedness comparison, which doesn't matter here. */
3315 primarg0 = arg0, primarg1 = arg1;
3316 STRIP_NOPS (primarg0);
3317 STRIP_NOPS (primarg1);
3318 if (operand_equal_p (primarg0, primarg1, 0))
3321 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3322 actual comparison operand, ARG0.
3324 First throw away any conversions to wider types
3325 already present in the operands. */
3327 primarg1 = get_narrower (arg1, &unsignedp1);
3328 primother = get_narrower (other, &unsignedpo);
3330 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3331 if (unsignedp1 == unsignedpo
3332 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3333 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3335 tree type = TREE_TYPE (arg0);
3337 /* Make sure shorter operand is extended the right way
3338 to match the longer operand. */
3339 primarg1 = fold_convert (signed_or_unsigned_type_for
3340 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3342 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3349 /* See if ARG is an expression that is either a comparison or is performing
3350 arithmetic on comparisons. The comparisons must only be comparing
3351 two different values, which will be stored in *CVAL1 and *CVAL2; if
3352 they are nonzero it means that some operands have already been found.
3353 No variables may be used anywhere else in the expression except in the
3354 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3355 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3357 If this is true, return 1. Otherwise, return zero. */
3360 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3362 enum tree_code code = TREE_CODE (arg);
3363 enum tree_code_class class = TREE_CODE_CLASS (code);
3365 /* We can handle some of the tcc_expression cases here. */
3366 if (class == tcc_expression && code == TRUTH_NOT_EXPR)
3368 else if (class == tcc_expression
3369 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3370 || code == COMPOUND_EXPR))
3373 else if (class == tcc_expression && code == SAVE_EXPR
3374 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3376 /* If we've already found a CVAL1 or CVAL2, this expression is
3377 two complex to handle. */
3378 if (*cval1 || *cval2)
3388 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3391 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3392 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3393 cval1, cval2, save_p));
3398 case tcc_expression:
3399 if (code == COND_EXPR)
3400 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3401 cval1, cval2, save_p)
3402 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3403 cval1, cval2, save_p)
3404 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3405 cval1, cval2, save_p));
3408 case tcc_comparison:
3409 /* First see if we can handle the first operand, then the second. For
3410 the second operand, we know *CVAL1 can't be zero. It must be that
3411 one side of the comparison is each of the values; test for the
3412 case where this isn't true by failing if the two operands
3415 if (operand_equal_p (TREE_OPERAND (arg, 0),
3416 TREE_OPERAND (arg, 1), 0))
3420 *cval1 = TREE_OPERAND (arg, 0);
3421 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3423 else if (*cval2 == 0)
3424 *cval2 = TREE_OPERAND (arg, 0);
3425 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3430 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3432 else if (*cval2 == 0)
3433 *cval2 = TREE_OPERAND (arg, 1);
3434 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3446 /* ARG is a tree that is known to contain just arithmetic operations and
3447 comparisons. Evaluate the operations in the tree substituting NEW0 for
3448 any occurrence of OLD0 as an operand of a comparison and likewise for
3452 eval_subst (tree arg, tree old0, tree new0, tree old1, tree new1)
3454 tree type = TREE_TYPE (arg);
3455 enum tree_code code = TREE_CODE (arg);
3456 enum tree_code_class class = TREE_CODE_CLASS (code);
3458 /* We can handle some of the tcc_expression cases here. */
3459 if (class == tcc_expression && code == TRUTH_NOT_EXPR)
3461 else if (class == tcc_expression
3462 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3468 return fold_build1 (code, type,
3469 eval_subst (TREE_OPERAND (arg, 0),
3470 old0, new0, old1, new1));
3473 return fold_build2 (code, type,
3474 eval_subst (TREE_OPERAND (arg, 0),
3475 old0, new0, old1, new1),
3476 eval_subst (TREE_OPERAND (arg, 1),
3477 old0, new0, old1, new1));
3479 case tcc_expression:
3483 return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
3486 return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
3489 return fold_build3 (code, type,
3490 eval_subst (TREE_OPERAND (arg, 0),
3491 old0, new0, old1, new1),
3492 eval_subst (TREE_OPERAND (arg, 1),
3493 old0, new0, old1, new1),
3494 eval_subst (TREE_OPERAND (arg, 2),
3495 old0, new0, old1, new1));
3499 /* Fall through - ??? */
3501 case tcc_comparison:
3503 tree arg0 = TREE_OPERAND (arg, 0);
3504 tree arg1 = TREE_OPERAND (arg, 1);
3506 /* We need to check both for exact equality and tree equality. The
3507 former will be true if the operand has a side-effect. In that
3508 case, we know the operand occurred exactly once. */
3510 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3512 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3515 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3517 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3520 return fold_build2 (code, type, arg0, arg1);
3528 /* Return a tree for the case when the result of an expression is RESULT
3529 converted to TYPE and OMITTED was previously an operand of the expression
3530 but is now not needed (e.g., we folded OMITTED * 0).
3532 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3533 the conversion of RESULT to TYPE. */
3536 omit_one_operand (tree type, tree result, tree omitted)
3538 tree t = fold_convert (type, result);
3540 /* If the resulting operand is an empty statement, just return the ommited
3541 statement casted to void. */
3542 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3543 return build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3545 if (TREE_SIDE_EFFECTS (omitted))
3546 return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3548 return non_lvalue (t);
3551 /* Similar, but call pedantic_non_lvalue instead of non_lvalue. */
3554 pedantic_omit_one_operand (tree type, tree result, tree omitted)
3556 tree t = fold_convert (type, result);
3558 /* If the resulting operand is an empty statement, just return the ommited
3559 statement casted to void. */
3560 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3561 return build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3563 if (TREE_SIDE_EFFECTS (omitted))
3564 return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3566 return pedantic_non_lvalue (t);
3569 /* Return a tree for the case when the result of an expression is RESULT
3570 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3571 of the expression but are now not needed.
3573 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3574 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3575 evaluated before OMITTED2. Otherwise, if neither has side effects,
3576 just do the conversion of RESULT to TYPE. */
3579 omit_two_operands (tree type, tree result, tree omitted1, tree omitted2)
3581 tree t = fold_convert (type, result);
3583 if (TREE_SIDE_EFFECTS (omitted2))
3584 t = build2 (COMPOUND_EXPR, type, omitted2, t);
3585 if (TREE_SIDE_EFFECTS (omitted1))
3586 t = build2 (COMPOUND_EXPR, type, omitted1, t);
3588 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue (t) : t;
3592 /* Return a simplified tree node for the truth-negation of ARG. This
3593 never alters ARG itself. We assume that ARG is an operation that
3594 returns a truth value (0 or 1).
3596 FIXME: one would think we would fold the result, but it causes
3597 problems with the dominator optimizer. */
3600 fold_truth_not_expr (tree arg)
3602 tree type = TREE_TYPE (arg);
3603 enum tree_code code = TREE_CODE (arg);
3605 /* If this is a comparison, we can simply invert it, except for
3606 floating-point non-equality comparisons, in which case we just
3607 enclose a TRUTH_NOT_EXPR around what we have. */
3609 if (TREE_CODE_CLASS (code) == tcc_comparison)
3611 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3612 if (FLOAT_TYPE_P (op_type)
3613 && flag_trapping_math
3614 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3615 && code != NE_EXPR && code != EQ_EXPR)
3619 code = invert_tree_comparison (code,
3620 HONOR_NANS (TYPE_MODE (op_type)));
3621 if (code == ERROR_MARK)
3624 return build2 (code, type,
3625 TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
3632 return constant_boolean_node (integer_zerop (arg), type);
3634 case TRUTH_AND_EXPR:
3635 return build2 (TRUTH_OR_EXPR, type,
3636 invert_truthvalue (TREE_OPERAND (arg, 0)),
3637 invert_truthvalue (TREE_OPERAND (arg, 1)));
3640 return build2 (TRUTH_AND_EXPR, type,
3641 invert_truthvalue (TREE_OPERAND (arg, 0)),
3642 invert_truthvalue (TREE_OPERAND (arg, 1)));
3644 case TRUTH_XOR_EXPR:
3645 /* Here we can invert either operand. We invert the first operand
3646 unless the second operand is a TRUTH_NOT_EXPR in which case our
3647 result is the XOR of the first operand with the inside of the
3648 negation of the second operand. */
3650 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3651 return build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3652 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3654 return build2 (TRUTH_XOR_EXPR, type,
3655 invert_truthvalue (TREE_OPERAND (arg, 0)),
3656 TREE_OPERAND (arg, 1));
3658 case TRUTH_ANDIF_EXPR:
3659 return build2 (TRUTH_ORIF_EXPR, type,
3660 invert_truthvalue (TREE_OPERAND (arg, 0)),
3661 invert_truthvalue (TREE_OPERAND (arg, 1)));
3663 case TRUTH_ORIF_EXPR:
3664 return build2 (TRUTH_ANDIF_EXPR, type,
3665 invert_truthvalue (TREE_OPERAND (arg, 0)),
3666 invert_truthvalue (TREE_OPERAND (arg, 1)));
3668 case TRUTH_NOT_EXPR:
3669 return TREE_OPERAND (arg, 0);
3673 tree arg1 = TREE_OPERAND (arg, 1);
3674 tree arg2 = TREE_OPERAND (arg, 2);
3675 /* A COND_EXPR may have a throw as one operand, which
3676 then has void type. Just leave void operands
3678 return build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
3679 VOID_TYPE_P (TREE_TYPE (arg1))
3680 ? arg1 : invert_truthvalue (arg1),
3681 VOID_TYPE_P (TREE_TYPE (arg2))
3682 ? arg2 : invert_truthvalue (arg2));
3686 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
3687 invert_truthvalue (TREE_OPERAND (arg, 1)));
3689 case NON_LVALUE_EXPR:
3690 return invert_truthvalue (TREE_OPERAND (arg, 0));
3693 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3694 return build1 (TRUTH_NOT_EXPR, type, arg);
3698 return build1 (TREE_CODE (arg), type,
3699 invert_truthvalue (TREE_OPERAND (arg, 0)));
3702 if (!integer_onep (TREE_OPERAND (arg, 1)))
3704 return build2 (EQ_EXPR, type, arg,
3705 build_int_cst (type, 0));
3708 return build1 (TRUTH_NOT_EXPR, type, arg);
3710 case CLEANUP_POINT_EXPR:
3711 return build1 (CLEANUP_POINT_EXPR, type,
3712 invert_truthvalue (TREE_OPERAND (arg, 0)));
3721 /* Return a simplified tree node for the truth-negation of ARG. This
3722 never alters ARG itself. We assume that ARG is an operation that
3723 returns a truth value (0 or 1).
3725 FIXME: one would think we would fold the result, but it causes
3726 problems with the dominator optimizer. */
3729 invert_truthvalue (tree arg)
3733 if (TREE_CODE (arg) == ERROR_MARK)
3736 tem = fold_truth_not_expr (arg);
3738 tem = build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
3743 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3744 operands are another bit-wise operation with a common input. If so,
3745 distribute the bit operations to save an operation and possibly two if
3746 constants are involved. For example, convert
3747 (A | B) & (A | C) into A | (B & C)
3748 Further simplification will occur if B and C are constants.
3750 If this optimization cannot be done, 0 will be returned. */
3753 distribute_bit_expr (enum tree_code code, tree type, tree arg0, tree arg1)
3758 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3759 || TREE_CODE (arg0) == code
3760 || (TREE_CODE (arg0) != BIT_AND_EXPR
3761 && TREE_CODE (arg0) != BIT_IOR_EXPR))
3764 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3766 common = TREE_OPERAND (arg0, 0);
3767 left = TREE_OPERAND (arg0, 1);
3768 right = TREE_OPERAND (arg1, 1);
3770 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3772 common = TREE_OPERAND (arg0, 0);
3773 left = TREE_OPERAND (arg0, 1);
3774 right = TREE_OPERAND (arg1, 0);
3776 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3778 common = TREE_OPERAND (arg0, 1);
3779 left = TREE_OPERAND (arg0, 0);
3780 right = TREE_OPERAND (arg1, 1);
3782 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3784 common = TREE_OPERAND (arg0, 1);
3785 left = TREE_OPERAND (arg0, 0);
3786 right = TREE_OPERAND (arg1, 0);
3791 return fold_build2 (TREE_CODE (arg0), type, common,
3792 fold_build2 (code, type, left, right));
3795 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3796 with code CODE. This optimization is unsafe. */
3798 distribute_real_division (enum tree_code code, tree type, tree arg0, tree arg1)
3800 bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3801 bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3803 /* (A / C) +- (B / C) -> (A +- B) / C. */
3805 && operand_equal_p (TREE_OPERAND (arg0, 1),
3806 TREE_OPERAND (arg1, 1), 0))
3807 return fold_build2 (mul0 ? MULT_EXPR : RDIV_EXPR, type,
3808 fold_build2 (code, type,
3809 TREE_OPERAND (arg0, 0),
3810 TREE_OPERAND (arg1, 0)),
3811 TREE_OPERAND (arg0, 1));
3813 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3814 if (operand_equal_p (TREE_OPERAND (arg0, 0),
3815 TREE_OPERAND (arg1, 0), 0)
3816 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3817 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3819 REAL_VALUE_TYPE r0, r1;
3820 r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3821 r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3823 real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3825 real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3826 real_arithmetic (&r0, code, &r0, &r1);
3827 return fold_build2 (MULT_EXPR, type,
3828 TREE_OPERAND (arg0, 0),
3829 build_real (type, r0));
3835 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3836 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero. */
3839 make_bit_field_ref (tree inner, tree type, int bitsize, int bitpos,
3846 tree size = TYPE_SIZE (TREE_TYPE (inner));
3847 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3848 || POINTER_TYPE_P (TREE_TYPE (inner)))
3849 && host_integerp (size, 0)
3850 && tree_low_cst (size, 0) == bitsize)
3851 return fold_convert (type, inner);
3854 result = build3 (BIT_FIELD_REF, type, inner,
3855 size_int (bitsize), bitsize_int (bitpos));
3857 BIT_FIELD_REF_UNSIGNED (result) = unsignedp;
3862 /* Optimize a bit-field compare.
3864 There are two cases: First is a compare against a constant and the
3865 second is a comparison of two items where the fields are at the same
3866 bit position relative to the start of a chunk (byte, halfword, word)
3867 large enough to contain it. In these cases we can avoid the shift
3868 implicit in bitfield extractions.
3870 For constants, we emit a compare of the shifted constant with the
3871 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3872 compared. For two fields at the same position, we do the ANDs with the
3873 similar mask and compare the result of the ANDs.
3875 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3876 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3877 are the left and right operands of the comparison, respectively.
3879 If the optimization described above can be done, we return the resulting
3880 tree. Otherwise we return zero. */
3883 optimize_bit_field_compare (enum tree_code code, tree compare_type,
3886 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3887 tree type = TREE_TYPE (lhs);
3888 tree signed_type, unsigned_type;
3889 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3890 enum machine_mode lmode, rmode, nmode;
3891 int lunsignedp, runsignedp;
3892 int lvolatilep = 0, rvolatilep = 0;
3893 tree linner, rinner = NULL_TREE;
3897 /* Get all the information about the extractions being done. If the bit size
3898 if the same as the size of the underlying object, we aren't doing an
3899 extraction at all and so can do nothing. We also don't want to
3900 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3901 then will no longer be able to replace it. */
3902 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3903 &lunsignedp, &lvolatilep, false);
3904 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3905 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3910 /* If this is not a constant, we can only do something if bit positions,
3911 sizes, and signedness are the same. */
3912 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3913 &runsignedp, &rvolatilep, false);
3915 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3916 || lunsignedp != runsignedp || offset != 0
3917 || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
3921 /* See if we can find a mode to refer to this field. We should be able to,
3922 but fail if we can't. */
3923 nmode = get_best_mode (lbitsize, lbitpos,
3924 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3925 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3926 TYPE_ALIGN (TREE_TYPE (rinner))),
3927 word_mode, lvolatilep || rvolatilep);
3928 if (nmode == VOIDmode)
3931 /* Set signed and unsigned types of the precision of this mode for the
3933 signed_type = lang_hooks.types.type_for_mode (nmode, 0);
3934 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3936 /* Compute the bit position and size for the new reference and our offset
3937 within it. If the new reference is the same size as the original, we
3938 won't optimize anything, so return zero. */
3939 nbitsize = GET_MODE_BITSIZE (nmode);
3940 nbitpos = lbitpos & ~ (nbitsize - 1);
3942 if (nbitsize == lbitsize)
3945 if (BYTES_BIG_ENDIAN)
3946 lbitpos = nbitsize - lbitsize - lbitpos;
3948 /* Make the mask to be used against the extracted field. */
3949 mask = build_int_cst_type (unsigned_type, -1);
3950 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
3951 mask = const_binop (RSHIFT_EXPR, mask,
3952 size_int (nbitsize - lbitsize - lbitpos), 0);
3955 /* If not comparing with constant, just rework the comparison
3957 return fold_build2 (code, compare_type,
3958 fold_build2 (BIT_AND_EXPR, unsigned_type,
3959 make_bit_field_ref (linner,
3964 fold_build2 (BIT_AND_EXPR, unsigned_type,
3965 make_bit_field_ref (rinner,
3971 /* Otherwise, we are handling the constant case. See if the constant is too
3972 big for the field. Warn and return a tree of for 0 (false) if so. We do
3973 this not only for its own sake, but to avoid having to test for this
3974 error case below. If we didn't, we might generate wrong code.
3976 For unsigned fields, the constant shifted right by the field length should
3977 be all zero. For signed fields, the high-order bits should agree with
3982 if (! integer_zerop (const_binop (RSHIFT_EXPR,
3983 fold_convert (unsigned_type, rhs),
3984 size_int (lbitsize), 0)))
3986 warning (0, "comparison is always %d due to width of bit-field",
3988 return constant_boolean_node (code == NE_EXPR, compare_type);
3993 tree tem = const_binop (RSHIFT_EXPR, fold_convert (signed_type, rhs),
3994 size_int (lbitsize - 1), 0);
3995 if (! integer_zerop (tem) && ! integer_all_onesp (tem))
3997 warning (0, "comparison is always %d due to width of bit-field",
3999 return constant_boolean_node (code == NE_EXPR, compare_type);
4003 /* Single-bit compares should always be against zero. */
4004 if (lbitsize == 1 && ! integer_zerop (rhs))
4006 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4007 rhs = build_int_cst (type, 0);
4010 /* Make a new bitfield reference, shift the constant over the
4011 appropriate number of bits and mask it with the computed mask
4012 (in case this was a signed field). If we changed it, make a new one. */
4013 lhs = make_bit_field_ref (linner, unsigned_type, nbitsize, nbitpos, 1);
4016 TREE_SIDE_EFFECTS (lhs) = 1;
4017 TREE_THIS_VOLATILE (lhs) = 1;
4020 rhs = const_binop (BIT_AND_EXPR,
4021 const_binop (LSHIFT_EXPR,
4022 fold_convert (unsigned_type, rhs),
4023 size_int (lbitpos), 0),
4026 return build2 (code, compare_type,
4027 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask),
4031 /* Subroutine for fold_truthop: decode a field reference.
4033 If EXP is a comparison reference, we return the innermost reference.
4035 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4036 set to the starting bit number.
4038 If the innermost field can be completely contained in a mode-sized
4039 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4041 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4042 otherwise it is not changed.
4044 *PUNSIGNEDP is set to the signedness of the field.
4046 *PMASK is set to the mask used. This is either contained in a
4047 BIT_AND_EXPR or derived from the width of the field.
4049 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4051 Return 0 if this is not a component reference or is one that we can't
4052 do anything with. */
4055 decode_field_reference (tree exp, HOST_WIDE_INT *pbitsize,
4056 HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
4057 int *punsignedp, int *pvolatilep,
4058 tree *pmask, tree *pand_mask)
4060 tree outer_type = 0;
4062 tree mask, inner, offset;
4064 unsigned int precision;
4066 /* All the optimizations using this function assume integer fields.
4067 There are problems with FP fields since the type_for_size call
4068 below can fail for, e.g., XFmode. */
4069 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4072 /* We are interested in the bare arrangement of bits, so strip everything
4073 that doesn't affect the machine mode. However, record the type of the
4074 outermost expression if it may matter below. */
4075 if (TREE_CODE (exp) == NOP_EXPR
4076 || TREE_CODE (exp) == CONVERT_EXPR
4077 || TREE_CODE (exp) == NON_LVALUE_EXPR)
4078 outer_type = TREE_TYPE (exp);
4081 if (TREE_CODE (exp) == BIT_AND_EXPR)
4083 and_mask = TREE_OPERAND (exp, 1);
4084 exp = TREE_OPERAND (exp, 0);
4085 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4086 if (TREE_CODE (and_mask) != INTEGER_CST)
4090 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
4091 punsignedp, pvolatilep, false);
4092 if ((inner == exp && and_mask == 0)
4093 || *pbitsize < 0 || offset != 0
4094 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
4097 /* If the number of bits in the reference is the same as the bitsize of
4098 the outer type, then the outer type gives the signedness. Otherwise
4099 (in case of a small bitfield) the signedness is unchanged. */
4100 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4101 *punsignedp = TYPE_UNSIGNED (outer_type);
4103 /* Compute the mask to access the bitfield. */
4104 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4105 precision = TYPE_PRECISION (unsigned_type);
4107 mask = build_int_cst_type (unsigned_type, -1);
4109 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
4110 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
4112 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4114 mask = fold_build2 (BIT_AND_EXPR, unsigned_type,
4115 fold_convert (unsigned_type, and_mask), mask);
4118 *pand_mask = and_mask;
4122 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4126 all_ones_mask_p (tree mask, int size)
4128 tree type = TREE_TYPE (mask);
4129 unsigned int precision = TYPE_PRECISION (type);
4132 tmask = build_int_cst_type (signed_type_for (type), -1);
4135 tree_int_cst_equal (mask,
4136 const_binop (RSHIFT_EXPR,
4137 const_binop (LSHIFT_EXPR, tmask,
4138 size_int (precision - size),
4140 size_int (precision - size), 0));
4143 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4144 represents the sign bit of EXP's type. If EXP represents a sign
4145 or zero extension, also test VAL against the unextended type.
4146 The return value is the (sub)expression whose sign bit is VAL,
4147 or NULL_TREE otherwise. */
4150 sign_bit_p (tree exp, tree val)
4152 unsigned HOST_WIDE_INT mask_lo, lo;
4153 HOST_WIDE_INT mask_hi, hi;
4157 /* Tree EXP must have an integral type. */
4158 t = TREE_TYPE (exp);
4159 if (! INTEGRAL_TYPE_P (t))
4162 /* Tree VAL must be an integer constant. */
4163 if (TREE_CODE (val) != INTEGER_CST
4164 || TREE_OVERFLOW (val))
4167 width = TYPE_PRECISION (t);
4168 if (width > HOST_BITS_PER_WIDE_INT)
4170 hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
4173 mask_hi = ((unsigned HOST_WIDE_INT) -1
4174 >> (2 * HOST_BITS_PER_WIDE_INT - width));
4180 lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
4183 mask_lo = ((unsigned HOST_WIDE_INT) -1
4184 >> (HOST_BITS_PER_WIDE_INT - width));
4187 /* We mask off those bits beyond TREE_TYPE (exp) so that we can
4188 treat VAL as if it were unsigned. */
4189 if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
4190 && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
4193 /* Handle extension from a narrower type. */
4194 if (TREE_CODE (exp) == NOP_EXPR
4195 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4196 return sign_bit_p (TREE_OPERAND (exp, 0), val);
4201 /* Subroutine for fold_truthop: determine if an operand is simple enough
4202 to be evaluated unconditionally. */
4205 simple_operand_p (tree exp)
4207 /* Strip any conversions that don't change the machine mode. */
4210 return (CONSTANT_CLASS_P (exp)
4211 || TREE_CODE (exp) == SSA_NAME
4213 && ! TREE_ADDRESSABLE (exp)
4214 && ! TREE_THIS_VOLATILE (exp)
4215 && ! DECL_NONLOCAL (exp)
4216 /* Don't regard global variables as simple. They may be
4217 allocated in ways unknown to the compiler (shared memory,
4218 #pragma weak, etc). */
4219 && ! TREE_PUBLIC (exp)
4220 && ! DECL_EXTERNAL (exp)
4221 /* Loading a static variable is unduly expensive, but global
4222 registers aren't expensive. */
4223 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4226 /* The following functions are subroutines to fold_range_test and allow it to
4227 try to change a logical combination of comparisons into a range test.
4230 X == 2 || X == 3 || X == 4 || X == 5
4234 (unsigned) (X - 2) <= 3
4236 We describe each set of comparisons as being either inside or outside
4237 a range, using a variable named like IN_P, and then describe the
4238 range with a lower and upper bound. If one of the bounds is omitted,
4239 it represents either the highest or lowest value of the type.
4241 In the comments below, we represent a range by two numbers in brackets
4242 preceded by a "+" to designate being inside that range, or a "-" to
4243 designate being outside that range, so the condition can be inverted by
4244 flipping the prefix. An omitted bound is represented by a "-". For
4245 example, "- [-, 10]" means being outside the range starting at the lowest
4246 possible value and ending at 10, in other words, being greater than 10.
4247 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4250 We set up things so that the missing bounds are handled in a consistent
4251 manner so neither a missing bound nor "true" and "false" need to be
4252 handled using a special case. */
4254 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4255 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4256 and UPPER1_P are nonzero if the respective argument is an upper bound
4257 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4258 must be specified for a comparison. ARG1 will be converted to ARG0's
4259 type if both are specified. */
4262 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4263 tree arg1, int upper1_p)
4269 /* If neither arg represents infinity, do the normal operation.
4270 Else, if not a comparison, return infinity. Else handle the special
4271 comparison rules. Note that most of the cases below won't occur, but
4272 are handled for consistency. */
4274 if (arg0 != 0 && arg1 != 0)
4276 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4277 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4279 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4282 if (TREE_CODE_CLASS (code) != tcc_comparison)
4285 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4286 for neither. In real maths, we cannot assume open ended ranges are
4287 the same. But, this is computer arithmetic, where numbers are finite.
4288 We can therefore make the transformation of any unbounded range with
4289 the value Z, Z being greater than any representable number. This permits
4290 us to treat unbounded ranges as equal. */
4291 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4292 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4296 result = sgn0 == sgn1;
4299 result = sgn0 != sgn1;
4302 result = sgn0 < sgn1;
4305 result = sgn0 <= sgn1;
4308 result = sgn0 > sgn1;
4311 result = sgn0 >= sgn1;
4317 return constant_boolean_node (result, type);
4320 /* Given EXP, a logical expression, set the range it is testing into
4321 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4322 actually being tested. *PLOW and *PHIGH will be made of the same
4323 type as the returned expression. If EXP is not a comparison, we
4324 will most likely not be returning a useful value and range. Set
4325 *STRICT_OVERFLOW_P to true if the return value is only valid
4326 because signed overflow is undefined; otherwise, do not change
4327 *STRICT_OVERFLOW_P. */
4330 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4331 bool *strict_overflow_p)
4333 enum tree_code code;
4334 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
4335 tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
4337 tree low, high, n_low, n_high;
4339 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4340 and see if we can refine the range. Some of the cases below may not
4341 happen, but it doesn't seem worth worrying about this. We "continue"
4342 the outer loop when we've changed something; otherwise we "break"
4343 the switch, which will "break" the while. */
4346 low = high = build_int_cst (TREE_TYPE (exp), 0);
4350 code = TREE_CODE (exp);
4351 exp_type = TREE_TYPE (exp);
4353 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4355 if (TREE_OPERAND_LENGTH (exp) > 0)
4356 arg0 = TREE_OPERAND (exp, 0);
4357 if (TREE_CODE_CLASS (code) == tcc_comparison
4358 || TREE_CODE_CLASS (code) == tcc_unary
4359 || TREE_CODE_CLASS (code) == tcc_binary)
4360 arg0_type = TREE_TYPE (arg0);
4361 if (TREE_CODE_CLASS (code) == tcc_binary
4362 || TREE_CODE_CLASS (code) == tcc_comparison
4363 || (TREE_CODE_CLASS (code) == tcc_expression
4364 && TREE_OPERAND_LENGTH (exp) > 1))
4365 arg1 = TREE_OPERAND (exp, 1);
4370 case TRUTH_NOT_EXPR:
4371 in_p = ! in_p, exp = arg0;
4374 case EQ_EXPR: case NE_EXPR:
4375 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4376 /* We can only do something if the range is testing for zero
4377 and if the second operand is an integer constant. Note that
4378 saying something is "in" the range we make is done by
4379 complementing IN_P since it will set in the initial case of
4380 being not equal to zero; "out" is leaving it alone. */
4381 if (low == 0 || high == 0
4382 || ! integer_zerop (low) || ! integer_zerop (high)
4383 || TREE_CODE (arg1) != INTEGER_CST)
4388 case NE_EXPR: /* - [c, c] */
4391 case EQ_EXPR: /* + [c, c] */
4392 in_p = ! in_p, low = high = arg1;
4394 case GT_EXPR: /* - [-, c] */
4395 low = 0, high = arg1;
4397 case GE_EXPR: /* + [c, -] */
4398 in_p = ! in_p, low = arg1, high = 0;
4400 case LT_EXPR: /* - [c, -] */
4401 low = arg1, high = 0;
4403 case LE_EXPR: /* + [-, c] */
4404 in_p = ! in_p, low = 0, high = arg1;
4410 /* If this is an unsigned comparison, we also know that EXP is
4411 greater than or equal to zero. We base the range tests we make
4412 on that fact, so we record it here so we can parse existing
4413 range tests. We test arg0_type since often the return type
4414 of, e.g. EQ_EXPR, is boolean. */
4415 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4417 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4419 build_int_cst (arg0_type, 0),
4423 in_p = n_in_p, low = n_low, high = n_high;
4425 /* If the high bound is missing, but we have a nonzero low
4426 bound, reverse the range so it goes from zero to the low bound
4428 if (high == 0 && low && ! integer_zerop (low))
4431 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4432 integer_one_node, 0);
4433 low = build_int_cst (arg0_type, 0);
4441 /* (-x) IN [a,b] -> x in [-b, -a] */
4442 n_low = range_binop (MINUS_EXPR, exp_type,
4443 build_int_cst (exp_type, 0),
4445 n_high = range_binop (MINUS_EXPR, exp_type,
4446 build_int_cst (exp_type, 0),
4448 low = n_low, high = n_high;
4454 exp = build2 (MINUS_EXPR, exp_type, negate_expr (arg0),
4455 build_int_cst (exp_type, 1));
4458 case PLUS_EXPR: case MINUS_EXPR:
4459 if (TREE_CODE (arg1) != INTEGER_CST)
4462 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4463 move a constant to the other side. */
4464 if (!TYPE_UNSIGNED (arg0_type)
4465 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4468 /* If EXP is signed, any overflow in the computation is undefined,
4469 so we don't worry about it so long as our computations on
4470 the bounds don't overflow. For unsigned, overflow is defined
4471 and this is exactly the right thing. */
4472 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4473 arg0_type, low, 0, arg1, 0);
4474 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4475 arg0_type, high, 1, arg1, 0);
4476 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4477 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4480 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4481 *strict_overflow_p = true;
4483 /* Check for an unsigned range which has wrapped around the maximum
4484 value thus making n_high < n_low, and normalize it. */
4485 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4487 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4488 integer_one_node, 0);
4489 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4490 integer_one_node, 0);
4492 /* If the range is of the form +/- [ x+1, x ], we won't
4493 be able to normalize it. But then, it represents the
4494 whole range or the empty set, so make it
4496 if (tree_int_cst_equal (n_low, low)
4497 && tree_int_cst_equal (n_high, high))
4503 low = n_low, high = n_high;
4508 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
4509 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4512 if (! INTEGRAL_TYPE_P (arg0_type)
4513 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4514 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4517 n_low = low, n_high = high;
4520 n_low = fold_convert (arg0_type, n_low);
4523 n_high = fold_convert (arg0_type, n_high);
4526 /* If we're converting arg0 from an unsigned type, to exp,
4527 a signed type, we will be doing the comparison as unsigned.
4528 The tests above have already verified that LOW and HIGH
4531 So we have to ensure that we will handle large unsigned
4532 values the same way that the current signed bounds treat
4535 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4539 /* For fixed-point modes, we need to pass the saturating flag
4540 as the 2nd parameter. */
4541 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4542 equiv_type = lang_hooks.types.type_for_mode
4543 (TYPE_MODE (arg0_type),
4544 TYPE_SATURATING (arg0_type));
4546 equiv_type = lang_hooks.types.type_for_mode
4547 (TYPE_MODE (arg0_type), 1);
4549 /* A range without an upper bound is, naturally, unbounded.
4550 Since convert would have cropped a very large value, use
4551 the max value for the destination type. */
4553 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4554 : TYPE_MAX_VALUE (arg0_type);
4556 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4557 high_positive = fold_build2 (RSHIFT_EXPR, arg0_type,
4558 fold_convert (arg0_type,
4560 build_int_cst (arg0_type, 1));
4562 /* If the low bound is specified, "and" the range with the
4563 range for which the original unsigned value will be
4567 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4568 1, n_low, n_high, 1,
4569 fold_convert (arg0_type,
4574 in_p = (n_in_p == in_p);
4578 /* Otherwise, "or" the range with the range of the input
4579 that will be interpreted as negative. */
4580 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4581 0, n_low, n_high, 1,
4582 fold_convert (arg0_type,
4587 in_p = (in_p != n_in_p);
4592 low = n_low, high = n_high;
4602 /* If EXP is a constant, we can evaluate whether this is true or false. */
4603 if (TREE_CODE (exp) == INTEGER_CST)
4605 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4607 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4613 *pin_p = in_p, *plow = low, *phigh = high;
4617 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4618 type, TYPE, return an expression to test if EXP is in (or out of, depending
4619 on IN_P) the range. Return 0 if the test couldn't be created. */
4622 build_range_check (tree type, tree exp, int in_p, tree low, tree high)
4624 tree etype = TREE_TYPE (exp);
4627 #ifdef HAVE_canonicalize_funcptr_for_compare
4628 /* Disable this optimization for function pointer expressions
4629 on targets that require function pointer canonicalization. */
4630 if (HAVE_canonicalize_funcptr_for_compare
4631 && TREE_CODE (etype) == POINTER_TYPE
4632 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4638 value = build_range_check (type, exp, 1, low, high);
4640 return invert_truthvalue (value);
4645 if (low == 0 && high == 0)
4646 return build_int_cst (type, 1);
4649 return fold_build2 (LE_EXPR, type, exp,
4650 fold_convert (etype, high));
4653 return fold_build2 (GE_EXPR, type, exp,
4654 fold_convert (etype, low));
4656 if (operand_equal_p (low, high, 0))
4657 return fold_build2 (EQ_EXPR, type, exp,
4658 fold_convert (etype, low));
4660 if (integer_zerop (low))
4662 if (! TYPE_UNSIGNED (etype))
4664 etype = unsigned_type_for (etype);
4665 high = fold_convert (etype, high);
4666 exp = fold_convert (etype, exp);
4668 return build_range_check (type, exp, 1, 0, high);
4671 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4672 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4674 unsigned HOST_WIDE_INT lo;
4678 prec = TYPE_PRECISION (etype);
4679 if (prec <= HOST_BITS_PER_WIDE_INT)
4682 lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4686 hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4687 lo = (unsigned HOST_WIDE_INT) -1;
4690 if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4692 if (TYPE_UNSIGNED (etype))
4694 etype = signed_type_for (etype);
4695 exp = fold_convert (etype, exp);
4697 return fold_build2 (GT_EXPR, type, exp,
4698 build_int_cst (etype, 0));
4702 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4703 This requires wrap-around arithmetics for the type of the expression. */
4704 switch (TREE_CODE (etype))
4707 /* There is no requirement that LOW be within the range of ETYPE
4708 if the latter is a subtype. It must, however, be within the base
4709 type of ETYPE. So be sure we do the subtraction in that type. */
4710 if (TREE_TYPE (etype))
4711 etype = TREE_TYPE (etype);
4716 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4717 TYPE_UNSIGNED (etype));
4724 /* If we don't have wrap-around arithmetics upfront, try to force it. */
4725 if (TREE_CODE (etype) == INTEGER_TYPE
4726 && !TYPE_OVERFLOW_WRAPS (etype))
4728 tree utype, minv, maxv;
4730 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4731 for the type in question, as we rely on this here. */
4732 utype = unsigned_type_for (etype);
4733 maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
4734 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4735 integer_one_node, 1);
4736 minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
4738 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4745 high = fold_convert (etype, high);
4746 low = fold_convert (etype, low);
4747 exp = fold_convert (etype, exp);
4749 value = const_binop (MINUS_EXPR, high, low, 0);
4752 if (POINTER_TYPE_P (etype))
4754 if (value != 0 && !TREE_OVERFLOW (value))
4756 low = fold_convert (sizetype, low);
4757 low = fold_build1 (NEGATE_EXPR, sizetype, low);
4758 return build_range_check (type,
4759 fold_build2 (POINTER_PLUS_EXPR, etype, exp, low),
4760 1, build_int_cst (etype, 0), value);
4765 if (value != 0 && !TREE_OVERFLOW (value))
4766 return build_range_check (type,
4767 fold_build2 (MINUS_EXPR, etype, exp, low),
4768 1, build_int_cst (etype, 0), value);
4773 /* Return the predecessor of VAL in its type, handling the infinite case. */
4776 range_predecessor (tree val)
4778 tree type = TREE_TYPE (val);
4780 if (INTEGRAL_TYPE_P (type)
4781 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4784 return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4787 /* Return the successor of VAL in its type, handling the infinite case. */
4790 range_successor (tree val)
4792 tree type = TREE_TYPE (val);
4794 if (INTEGRAL_TYPE_P (type)
4795 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4798 return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4801 /* Given two ranges, see if we can merge them into one. Return 1 if we
4802 can, 0 if we can't. Set the output range into the specified parameters. */
4805 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4806 tree high0, int in1_p, tree low1, tree high1)
4814 int lowequal = ((low0 == 0 && low1 == 0)
4815 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4816 low0, 0, low1, 0)));
4817 int highequal = ((high0 == 0 && high1 == 0)
4818 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4819 high0, 1, high1, 1)));
4821 /* Make range 0 be the range that starts first, or ends last if they
4822 start at the same value. Swap them if it isn't. */
4823 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4826 && integer_onep (range_binop (GT_EXPR, integer_type_node,
4827 high1, 1, high0, 1))))
4829 temp = in0_p, in0_p = in1_p, in1_p = temp;
4830 tem = low0, low0 = low1, low1 = tem;
4831 tem = high0, high0 = high1, high1 = tem;
4834 /* Now flag two cases, whether the ranges are disjoint or whether the
4835 second range is totally subsumed in the first. Note that the tests
4836 below are simplified by the ones above. */
4837 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4838 high0, 1, low1, 0));
4839 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4840 high1, 1, high0, 1));
4842 /* We now have four cases, depending on whether we are including or
4843 excluding the two ranges. */
4846 /* If they don't overlap, the result is false. If the second range
4847 is a subset it is the result. Otherwise, the range is from the start
4848 of the second to the end of the first. */
4850 in_p = 0, low = high = 0;
4852 in_p = 1, low = low1, high = high1;
4854 in_p = 1, low = low1, high = high0;
4857 else if (in0_p && ! in1_p)
4859 /* If they don't overlap, the result is the first range. If they are
4860 equal, the result is false. If the second range is a subset of the
4861 first, and the ranges begin at the same place, we go from just after
4862 the end of the second range to the end of the first. If the second
4863 range is not a subset of the first, or if it is a subset and both
4864 ranges end at the same place, the range starts at the start of the
4865 first range and ends just before the second range.
4866 Otherwise, we can't describe this as a single range. */
4868 in_p = 1, low = low0, high = high0;
4869 else if (lowequal && highequal)
4870 in_p = 0, low = high = 0;
4871 else if (subset && lowequal)
4873 low = range_successor (high1);
4878 /* We are in the weird situation where high0 > high1 but
4879 high1 has no successor. Punt. */
4883 else if (! subset || highequal)
4886 high = range_predecessor (low1);
4890 /* low0 < low1 but low1 has no predecessor. Punt. */
4898 else if (! in0_p && in1_p)
4900 /* If they don't overlap, the result is the second range. If the second
4901 is a subset of the first, the result is false. Otherwise,
4902 the range starts just after the first range and ends at the
4903 end of the second. */
4905 in_p = 1, low = low1, high = high1;
4906 else if (subset || highequal)
4907 in_p = 0, low = high = 0;
4910 low = range_successor (high0);
4915 /* high1 > high0 but high0 has no successor. Punt. */
4923 /* The case where we are excluding both ranges. Here the complex case
4924 is if they don't overlap. In that case, the only time we have a
4925 range is if they are adjacent. If the second is a subset of the
4926 first, the result is the first. Otherwise, the range to exclude
4927 starts at the beginning of the first range and ends at the end of the
4931 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4932 range_successor (high0),
4934 in_p = 0, low = low0, high = high1;
4937 /* Canonicalize - [min, x] into - [-, x]. */
4938 if (low0 && TREE_CODE (low0) == INTEGER_CST)
4939 switch (TREE_CODE (TREE_TYPE (low0)))
4942 if (TYPE_PRECISION (TREE_TYPE (low0))
4943 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4947 if (tree_int_cst_equal (low0,
4948 TYPE_MIN_VALUE (TREE_TYPE (low0))))
4952 if (TYPE_UNSIGNED (TREE_TYPE (low0))
4953 && integer_zerop (low0))
4960 /* Canonicalize - [x, max] into - [x, -]. */
4961 if (high1 && TREE_CODE (high1) == INTEGER_CST)
4962 switch (TREE_CODE (TREE_TYPE (high1)))
4965 if (TYPE_PRECISION (TREE_TYPE (high1))
4966 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4970 if (tree_int_cst_equal (high1,
4971 TYPE_MAX_VALUE (TREE_TYPE (high1))))
4975 if (TYPE_UNSIGNED (TREE_TYPE (high1))
4976 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4978 integer_one_node, 1)))
4985 /* The ranges might be also adjacent between the maximum and
4986 minimum values of the given type. For
4987 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4988 return + [x + 1, y - 1]. */
4989 if (low0 == 0 && high1 == 0)
4991 low = range_successor (high0);
4992 high = range_predecessor (low1);
4993 if (low == 0 || high == 0)
5003 in_p = 0, low = low0, high = high0;
5005 in_p = 0, low = low0, high = high1;
5008 *pin_p = in_p, *plow = low, *phigh = high;
5013 /* Subroutine of fold, looking inside expressions of the form
5014 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5015 of the COND_EXPR. This function is being used also to optimize
5016 A op B ? C : A, by reversing the comparison first.
5018 Return a folded expression whose code is not a COND_EXPR
5019 anymore, or NULL_TREE if no folding opportunity is found. */
5022 fold_cond_expr_with_comparison (tree type, tree arg0, tree arg1, tree arg2)
5024 enum tree_code comp_code = TREE_CODE (arg0);
5025 tree arg00 = TREE_OPERAND (arg0, 0);
5026 tree arg01 = TREE_OPERAND (arg0, 1);
5027 tree arg1_type = TREE_TYPE (arg1);
5033 /* If we have A op 0 ? A : -A, consider applying the following
5036 A == 0? A : -A same as -A
5037 A != 0? A : -A same as A
5038 A >= 0? A : -A same as abs (A)
5039 A > 0? A : -A same as abs (A)
5040 A <= 0? A : -A same as -abs (A)
5041 A < 0? A : -A same as -abs (A)
5043 None of these transformations work for modes with signed
5044 zeros. If A is +/-0, the first two transformations will
5045 change the sign of the result (from +0 to -0, or vice
5046 versa). The last four will fix the sign of the result,
5047 even though the original expressions could be positive or
5048 negative, depending on the sign of A.
5050 Note that all these transformations are correct if A is
5051 NaN, since the two alternatives (A and -A) are also NaNs. */
5052 if ((FLOAT_TYPE_P (TREE_TYPE (arg01))
5053 ? real_zerop (arg01)
5054 : integer_zerop (arg01))
5055 && ((TREE_CODE (arg2) == NEGATE_EXPR
5056 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5057 /* In the case that A is of the form X-Y, '-A' (arg2) may
5058 have already been folded to Y-X, check for that. */
5059 || (TREE_CODE (arg1) == MINUS_EXPR
5060 && TREE_CODE (arg2) == MINUS_EXPR
5061 && operand_equal_p (TREE_OPERAND (arg1, 0),
5062 TREE_OPERAND (arg2, 1), 0)
5063 && operand_equal_p (TREE_OPERAND (arg1, 1),
5064 TREE_OPERAND (arg2, 0), 0))))
5069 tem = fold_convert (arg1_type, arg1);
5070 return pedantic_non_lvalue (fold_convert (type, negate_expr (tem)));
5073 return pedantic_non_lvalue (fold_convert (type, arg1));
5076 if (flag_trapping_math)
5081 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5082 arg1 = fold_convert (signed_type_for
5083 (TREE_TYPE (arg1)), arg1);
5084 tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
5085 return pedantic_non_lvalue (fold_convert (type, tem));
5088 if (flag_trapping_math)
5092 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5093 arg1 = fold_convert (signed_type_for
5094 (TREE_TYPE (arg1)), arg1);
5095 tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
5096 return negate_expr (fold_convert (type, tem));
5098 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5102 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5103 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5104 both transformations are correct when A is NaN: A != 0
5105 is then true, and A == 0 is false. */
5107 if (integer_zerop (arg01) && integer_zerop (arg2))
5109 if (comp_code == NE_EXPR)
5110 return pedantic_non_lvalue (fold_convert (type, arg1));
5111 else if (comp_code == EQ_EXPR)
5112 return build_int_cst (type, 0);
5115 /* Try some transformations of A op B ? A : B.
5117 A == B? A : B same as B
5118 A != B? A : B same as A
5119 A >= B? A : B same as max (A, B)
5120 A > B? A : B same as max (B, A)
5121 A <= B? A : B same as min (A, B)
5122 A < B? A : B same as min (B, A)
5124 As above, these transformations don't work in the presence
5125 of signed zeros. For example, if A and B are zeros of
5126 opposite sign, the first two transformations will change
5127 the sign of the result. In the last four, the original
5128 expressions give different results for (A=+0, B=-0) and
5129 (A=-0, B=+0), but the transformed expressions do not.
5131 The first two transformations are correct if either A or B
5132 is a NaN. In the first transformation, the condition will
5133 be false, and B will indeed be chosen. In the case of the
5134 second transformation, the condition A != B will be true,
5135 and A will be chosen.
5137 The conversions to max() and min() are not correct if B is
5138 a number and A is not. The conditions in the original
5139 expressions will be false, so all four give B. The min()
5140 and max() versions would give a NaN instead. */
5141 if (operand_equal_for_comparison_p (arg01, arg2, arg00)
5142 /* Avoid these transformations if the COND_EXPR may be used
5143 as an lvalue in the C++ front-end. PR c++/19199. */
5145 || (strcmp (lang_hooks.name, "GNU C++") != 0
5146 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5147 || ! maybe_lvalue_p (arg1)
5148 || ! maybe_lvalue_p (arg2)))
5150 tree comp_op0 = arg00;
5151 tree comp_op1 = arg01;
5152 tree comp_type = TREE_TYPE (comp_op0);
5154 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
5155 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
5165 return pedantic_non_lvalue (fold_convert (type, arg2));
5167 return pedantic_non_lvalue (fold_convert (type, arg1));
5172 /* In C++ a ?: expression can be an lvalue, so put the
5173 operand which will be used if they are equal first
5174 so that we can convert this back to the
5175 corresponding COND_EXPR. */
5176 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5178 comp_op0 = fold_convert (comp_type, comp_op0);
5179 comp_op1 = fold_convert (comp_type, comp_op1);
5180 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5181 ? fold_build2 (MIN_EXPR, comp_type, comp_op0, comp_op1)
5182 : fold_build2 (MIN_EXPR, comp_type, comp_op1, comp_op0);
5183 return pedantic_non_lvalue (fold_convert (type, tem));
5190 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5192 comp_op0 = fold_convert (comp_type, comp_op0);
5193 comp_op1 = fold_convert (comp_type, comp_op1);
5194 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5195 ? fold_build2 (MAX_EXPR, comp_type, comp_op0, comp_op1)
5196 : fold_build2 (MAX_EXPR, comp_type, comp_op1, comp_op0);
5197 return pedantic_non_lvalue (fold_convert (type, tem));
5201 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5202 return pedantic_non_lvalue (fold_convert (type, arg2));
5205 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5206 return pedantic_non_lvalue (fold_convert (type, arg1));
5209 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5214 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5215 we might still be able to simplify this. For example,
5216 if C1 is one less or one more than C2, this might have started
5217 out as a MIN or MAX and been transformed by this function.
5218 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
5220 if (INTEGRAL_TYPE_P (type)
5221 && TREE_CODE (arg01) == INTEGER_CST
5222 && TREE_CODE (arg2) == INTEGER_CST)
5226 /* We can replace A with C1 in this case. */
5227 arg1 = fold_convert (type, arg01);
5228 return fold_build3 (COND_EXPR, type, arg0, arg1, arg2);
5231 /* If C1 is C2 + 1, this is min(A, C2). */
5232 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5234 && operand_equal_p (arg01,
5235 const_binop (PLUS_EXPR, arg2,
5236 build_int_cst (type, 1), 0),
5238 return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
5240 fold_convert (type, arg1),
5245 /* If C1 is C2 - 1, this is min(A, C2). */
5246 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5248 && operand_equal_p (arg01,
5249 const_binop (MINUS_EXPR, arg2,
5250 build_int_cst (type, 1), 0),
5252 return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
5254 fold_convert (type, arg1),
5259 /* If C1 is C2 - 1, this is max(A, C2). */
5260 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5262 && operand_equal_p (arg01,
5263 const_binop (MINUS_EXPR, arg2,
5264 build_int_cst (type, 1), 0),
5266 return pedantic_non_lvalue (fold_build2 (MAX_EXPR,
5268 fold_convert (type, arg1),
5273 /* If C1 is C2 + 1, this is max(A, C2). */
5274 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5276 && operand_equal_p (arg01,
5277 const_binop (PLUS_EXPR, arg2,
5278 build_int_cst (type, 1), 0),
5280 return pedantic_non_lvalue (fold_build2 (MAX_EXPR,
5282 fold_convert (type, arg1),
5296 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5297 #define LOGICAL_OP_NON_SHORT_CIRCUIT (BRANCH_COST >= 2)
5300 /* EXP is some logical combination of boolean tests. See if we can
5301 merge it into some range test. Return the new tree if so. */
5304 fold_range_test (enum tree_code code, tree type, tree op0, tree op1)
5306 int or_op = (code == TRUTH_ORIF_EXPR
5307 || code == TRUTH_OR_EXPR);
5308 int in0_p, in1_p, in_p;
5309 tree low0, low1, low, high0, high1, high;
5310 bool strict_overflow_p = false;
5311 tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5312 tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5314 const char * const warnmsg = G_("assuming signed overflow does not occur "
5315 "when simplifying range test");
5317 /* If this is an OR operation, invert both sides; we will invert
5318 again at the end. */
5320 in0_p = ! in0_p, in1_p = ! in1_p;
5322 /* If both expressions are the same, if we can merge the ranges, and we
5323 can build the range test, return it or it inverted. If one of the
5324 ranges is always true or always false, consider it to be the same
5325 expression as the other. */
5326 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5327 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5329 && 0 != (tem = (build_range_check (type,
5331 : rhs != 0 ? rhs : integer_zero_node,
5334 if (strict_overflow_p)
5335 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5336 return or_op ? invert_truthvalue (tem) : tem;
5339 /* On machines where the branch cost is expensive, if this is a
5340 short-circuited branch and the underlying object on both sides
5341 is the same, make a non-short-circuit operation. */
5342 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5343 && lhs != 0 && rhs != 0
5344 && (code == TRUTH_ANDIF_EXPR
5345 || code == TRUTH_ORIF_EXPR)
5346 && operand_equal_p (lhs, rhs, 0))
5348 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5349 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5350 which cases we can't do this. */
5351 if (simple_operand_p (lhs))
5352 return build2 (code == TRUTH_ANDIF_EXPR
5353 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5356 else if (lang_hooks.decls.global_bindings_p () == 0
5357 && ! CONTAINS_PLACEHOLDER_P (lhs))
5359 tree common = save_expr (lhs);
5361 if (0 != (lhs = build_range_check (type, common,
5362 or_op ? ! in0_p : in0_p,
5364 && (0 != (rhs = build_range_check (type, common,
5365 or_op ? ! in1_p : in1_p,
5368 if (strict_overflow_p)
5369 fold_overflow_warning (warnmsg,
5370 WARN_STRICT_OVERFLOW_COMPARISON);
5371 return build2 (code == TRUTH_ANDIF_EXPR
5372 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5381 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
5382 bit value. Arrange things so the extra bits will be set to zero if and
5383 only if C is signed-extended to its full width. If MASK is nonzero,
5384 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5387 unextend (tree c, int p, int unsignedp, tree mask)
5389 tree type = TREE_TYPE (c);
5390 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5393 if (p == modesize || unsignedp)
5396 /* We work by getting just the sign bit into the low-order bit, then
5397 into the high-order bit, then sign-extend. We then XOR that value
5399 temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
5400 temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
5402 /* We must use a signed type in order to get an arithmetic right shift.
5403 However, we must also avoid introducing accidental overflows, so that
5404 a subsequent call to integer_zerop will work. Hence we must
5405 do the type conversion here. At this point, the constant is either
5406 zero or one, and the conversion to a signed type can never overflow.
5407 We could get an overflow if this conversion is done anywhere else. */
5408 if (TYPE_UNSIGNED (type))
5409 temp = fold_convert (signed_type_for (type), temp);
5411 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
5412 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
5414 temp = const_binop (BIT_AND_EXPR, temp,
5415 fold_convert (TREE_TYPE (c), mask), 0);
5416 /* If necessary, convert the type back to match the type of C. */
5417 if (TYPE_UNSIGNED (type))
5418 temp = fold_convert (type, temp);
5420 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
5423 /* Find ways of folding logical expressions of LHS and RHS:
5424 Try to merge two comparisons to the same innermost item.
5425 Look for range tests like "ch >= '0' && ch <= '9'".
5426 Look for combinations of simple terms on machines with expensive branches
5427 and evaluate the RHS unconditionally.
5429 For example, if we have p->a == 2 && p->b == 4 and we can make an
5430 object large enough to span both A and B, we can do this with a comparison
5431 against the object ANDed with the a mask.
5433 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5434 operations to do this with one comparison.
5436 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5437 function and the one above.
5439 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5440 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5442 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5445 We return the simplified tree or 0 if no optimization is possible. */
5448 fold_truthop (enum tree_code code, tree truth_type, tree lhs, tree rhs)
5450 /* If this is the "or" of two comparisons, we can do something if
5451 the comparisons are NE_EXPR. If this is the "and", we can do something
5452 if the comparisons are EQ_EXPR. I.e.,
5453 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5455 WANTED_CODE is this operation code. For single bit fields, we can
5456 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5457 comparison for one-bit fields. */
5459 enum tree_code wanted_code;
5460 enum tree_code lcode, rcode;
5461 tree ll_arg, lr_arg, rl_arg, rr_arg;
5462 tree ll_inner, lr_inner, rl_inner, rr_inner;
5463 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5464 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5465 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5466 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5467 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5468 enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5469 enum machine_mode lnmode, rnmode;
5470 tree ll_mask, lr_mask, rl_mask, rr_mask;
5471 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5472 tree l_const, r_const;
5473 tree lntype, rntype, result;
5474 int first_bit, end_bit;
5476 tree orig_lhs = lhs, orig_rhs = rhs;
5477 enum tree_code orig_code = code;
5479 /* Start by getting the comparison codes. Fail if anything is volatile.
5480 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5481 it were surrounded with a NE_EXPR. */
5483 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5486 lcode = TREE_CODE (lhs);
5487 rcode = TREE_CODE (rhs);
5489 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5491 lhs = build2 (NE_EXPR, truth_type, lhs,
5492 build_int_cst (TREE_TYPE (lhs), 0));
5496 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5498 rhs = build2 (NE_EXPR, truth_type, rhs,
5499 build_int_cst (TREE_TYPE (rhs), 0));
5503 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5504 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5507 ll_arg = TREE_OPERAND (lhs, 0);
5508 lr_arg = TREE_OPERAND (lhs, 1);
5509 rl_arg = TREE_OPERAND (rhs, 0);
5510 rr_arg = TREE_OPERAND (rhs, 1);
5512 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5513 if (simple_operand_p (ll_arg)
5514 && simple_operand_p (lr_arg))
5517 if (operand_equal_p (ll_arg, rl_arg, 0)
5518 && operand_equal_p (lr_arg, rr_arg, 0))
5520 result = combine_comparisons (code, lcode, rcode,
5521 truth_type, ll_arg, lr_arg);
5525 else if (operand_equal_p (ll_arg, rr_arg, 0)
5526 && operand_equal_p (lr_arg, rl_arg, 0))
5528 result = combine_comparisons (code, lcode,
5529 swap_tree_comparison (rcode),
5530 truth_type, ll_arg, lr_arg);
5536 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5537 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5539 /* If the RHS can be evaluated unconditionally and its operands are
5540 simple, it wins to evaluate the RHS unconditionally on machines
5541 with expensive branches. In this case, this isn't a comparison
5542 that can be merged. Avoid doing this if the RHS is a floating-point
5543 comparison since those can trap. */
5545 if (BRANCH_COST >= 2
5546 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5547 && simple_operand_p (rl_arg)
5548 && simple_operand_p (rr_arg))
5550 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5551 if (code == TRUTH_OR_EXPR
5552 && lcode == NE_EXPR && integer_zerop (lr_arg)
5553 && rcode == NE_EXPR && integer_zerop (rr_arg)
5554 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg))
5555 return build2 (NE_EXPR, truth_type,
5556 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5558 build_int_cst (TREE_TYPE (ll_arg), 0));
5560 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5561 if (code == TRUTH_AND_EXPR
5562 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5563 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5564 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg))
5565 return build2 (EQ_EXPR, truth_type,
5566 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5568 build_int_cst (TREE_TYPE (ll_arg), 0));
5570 if (LOGICAL_OP_NON_SHORT_CIRCUIT)
5572 if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs)
5573 return build2 (code, truth_type, lhs, rhs);
5578 /* See if the comparisons can be merged. Then get all the parameters for
5581 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5582 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5586 ll_inner = decode_field_reference (ll_arg,
5587 &ll_bitsize, &ll_bitpos, &ll_mode,
5588 &ll_unsignedp, &volatilep, &ll_mask,
5590 lr_inner = decode_field_reference (lr_arg,
5591 &lr_bitsize, &lr_bitpos, &lr_mode,
5592 &lr_unsignedp, &volatilep, &lr_mask,
5594 rl_inner = decode_field_reference (rl_arg,
5595 &rl_bitsize, &rl_bitpos, &rl_mode,
5596 &rl_unsignedp, &volatilep, &rl_mask,
5598 rr_inner = decode_field_reference (rr_arg,
5599 &rr_bitsize, &rr_bitpos, &rr_mode,
5600 &rr_unsignedp, &volatilep, &rr_mask,
5603 /* It must be true that the inner operation on the lhs of each
5604 comparison must be the same if we are to be able to do anything.
5605 Then see if we have constants. If not, the same must be true for
5607 if (volatilep || ll_inner == 0 || rl_inner == 0
5608 || ! operand_equal_p (ll_inner, rl_inner, 0))
5611 if (TREE_CODE (lr_arg) == INTEGER_CST
5612 && TREE_CODE (rr_arg) == INTEGER_CST)
5613 l_const = lr_arg, r_const = rr_arg;
5614 else if (lr_inner == 0 || rr_inner == 0
5615 || ! operand_equal_p (lr_inner, rr_inner, 0))
5618 l_const = r_const = 0;
5620 /* If either comparison code is not correct for our logical operation,
5621 fail. However, we can convert a one-bit comparison against zero into
5622 the opposite comparison against that bit being set in the field. */
5624 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5625 if (lcode != wanted_code)
5627 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5629 /* Make the left operand unsigned, since we are only interested
5630 in the value of one bit. Otherwise we are doing the wrong
5639 /* This is analogous to the code for l_const above. */
5640 if (rcode != wanted_code)
5642 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5651 /* See if we can find a mode that contains both fields being compared on
5652 the left. If we can't, fail. Otherwise, update all constants and masks
5653 to be relative to a field of that size. */
5654 first_bit = MIN (ll_bitpos, rl_bitpos);
5655 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5656 lnmode = get_best_mode (end_bit - first_bit, first_bit,
5657 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5659 if (lnmode == VOIDmode)
5662 lnbitsize = GET_MODE_BITSIZE (lnmode);
5663 lnbitpos = first_bit & ~ (lnbitsize - 1);
5664 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5665 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5667 if (BYTES_BIG_ENDIAN)
5669 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5670 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5673 ll_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, ll_mask),
5674 size_int (xll_bitpos), 0);
5675 rl_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, rl_mask),
5676 size_int (xrl_bitpos), 0);
5680 l_const = fold_convert (lntype, l_const);
5681 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5682 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
5683 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5684 fold_build1 (BIT_NOT_EXPR,
5688 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5690 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5695 r_const = fold_convert (lntype, r_const);
5696 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5697 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
5698 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5699 fold_build1 (BIT_NOT_EXPR,
5703 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5705 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5709 /* If the right sides are not constant, do the same for it. Also,
5710 disallow this optimization if a size or signedness mismatch occurs
5711 between the left and right sides. */
5714 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5715 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5716 /* Make sure the two fields on the right
5717 correspond to the left without being swapped. */
5718 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5721 first_bit = MIN (lr_bitpos, rr_bitpos);
5722 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5723 rnmode = get_best_mode (end_bit - first_bit, first_bit,
5724 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5726 if (rnmode == VOIDmode)
5729 rnbitsize = GET_MODE_BITSIZE (rnmode);
5730 rnbitpos = first_bit & ~ (rnbitsize - 1);
5731 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5732 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5734 if (BYTES_BIG_ENDIAN)
5736 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5737 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5740 lr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, lr_mask),
5741 size_int (xlr_bitpos), 0);
5742 rr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, rr_mask),
5743 size_int (xrr_bitpos), 0);
5745 /* Make a mask that corresponds to both fields being compared.
5746 Do this for both items being compared. If the operands are the
5747 same size and the bits being compared are in the same position
5748 then we can do this by masking both and comparing the masked
5750 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
5751 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
5752 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5754 lhs = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
5755 ll_unsignedp || rl_unsignedp);
5756 if (! all_ones_mask_p (ll_mask, lnbitsize))
5757 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5759 rhs = make_bit_field_ref (lr_inner, rntype, rnbitsize, rnbitpos,
5760 lr_unsignedp || rr_unsignedp);
5761 if (! all_ones_mask_p (lr_mask, rnbitsize))
5762 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5764 return build2 (wanted_code, truth_type, lhs, rhs);
5767 /* There is still another way we can do something: If both pairs of
5768 fields being compared are adjacent, we may be able to make a wider
5769 field containing them both.
5771 Note that we still must mask the lhs/rhs expressions. Furthermore,
5772 the mask must be shifted to account for the shift done by
5773 make_bit_field_ref. */
5774 if ((ll_bitsize + ll_bitpos == rl_bitpos
5775 && lr_bitsize + lr_bitpos == rr_bitpos)
5776 || (ll_bitpos == rl_bitpos + rl_bitsize
5777 && lr_bitpos == rr_bitpos + rr_bitsize))
5781 lhs = make_bit_field_ref (ll_inner, lntype, ll_bitsize + rl_bitsize,
5782 MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
5783 rhs = make_bit_field_ref (lr_inner, rntype, lr_bitsize + rr_bitsize,
5784 MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
5786 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5787 size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
5788 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5789 size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
5791 /* Convert to the smaller type before masking out unwanted bits. */
5793 if (lntype != rntype)
5795 if (lnbitsize > rnbitsize)
5797 lhs = fold_convert (rntype, lhs);
5798 ll_mask = fold_convert (rntype, ll_mask);
5801 else if (lnbitsize < rnbitsize)
5803 rhs = fold_convert (lntype, rhs);
5804 lr_mask = fold_convert (lntype, lr_mask);
5809 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5810 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5812 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5813 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5815 return build2 (wanted_code, truth_type, lhs, rhs);
5821 /* Handle the case of comparisons with constants. If there is something in
5822 common between the masks, those bits of the constants must be the same.
5823 If not, the condition is always false. Test for this to avoid generating
5824 incorrect code below. */
5825 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
5826 if (! integer_zerop (result)
5827 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
5828 const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
5830 if (wanted_code == NE_EXPR)
5832 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5833 return constant_boolean_node (true, truth_type);
5837 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5838 return constant_boolean_node (false, truth_type);
5842 /* Construct the expression we will return. First get the component
5843 reference we will make. Unless the mask is all ones the width of
5844 that field, perform the mask operation. Then compare with the
5846 result = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
5847 ll_unsignedp || rl_unsignedp);
5849 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
5850 if (! all_ones_mask_p (ll_mask, lnbitsize))
5851 result = build2 (BIT_AND_EXPR, lntype, result, ll_mask);
5853 return build2 (wanted_code, truth_type, result,
5854 const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
5857 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5861 optimize_minmax_comparison (enum tree_code code, tree type, tree op0, tree op1)
5864 enum tree_code op_code;
5865 tree comp_const = op1;
5867 int consts_equal, consts_lt;
5870 STRIP_SIGN_NOPS (arg0);
5872 op_code = TREE_CODE (arg0);
5873 minmax_const = TREE_OPERAND (arg0, 1);
5874 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5875 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5876 inner = TREE_OPERAND (arg0, 0);
5878 /* If something does not permit us to optimize, return the original tree. */
5879 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5880 || TREE_CODE (comp_const) != INTEGER_CST
5881 || TREE_OVERFLOW (comp_const)
5882 || TREE_CODE (minmax_const) != INTEGER_CST
5883 || TREE_OVERFLOW (minmax_const))
5886 /* Now handle all the various comparison codes. We only handle EQ_EXPR
5887 and GT_EXPR, doing the rest with recursive calls using logical
5891 case NE_EXPR: case LT_EXPR: case LE_EXPR:
5893 tree tem = optimize_minmax_comparison (invert_tree_comparison (code, false),
5896 return invert_truthvalue (tem);
5902 fold_build2 (TRUTH_ORIF_EXPR, type,
5903 optimize_minmax_comparison
5904 (EQ_EXPR, type, arg0, comp_const),
5905 optimize_minmax_comparison
5906 (GT_EXPR, type, arg0, comp_const));
5909 if (op_code == MAX_EXPR && consts_equal)
5910 /* MAX (X, 0) == 0 -> X <= 0 */
5911 return fold_build2 (LE_EXPR, type, inner, comp_const);
5913 else if (op_code == MAX_EXPR && consts_lt)
5914 /* MAX (X, 0) == 5 -> X == 5 */
5915 return fold_build2 (EQ_EXPR, type, inner, comp_const);
5917 else if (op_code == MAX_EXPR)
5918 /* MAX (X, 0) == -1 -> false */
5919 return omit_one_operand (type, integer_zero_node, inner);
5921 else if (consts_equal)
5922 /* MIN (X, 0) == 0 -> X >= 0 */
5923 return fold_build2 (GE_EXPR, type, inner, comp_const);
5926 /* MIN (X, 0) == 5 -> false */
5927 return omit_one_operand (type, integer_zero_node, inner);
5930 /* MIN (X, 0) == -1 -> X == -1 */
5931 return fold_build2 (EQ_EXPR, type, inner, comp_const);
5934 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5935 /* MAX (X, 0) > 0 -> X > 0
5936 MAX (X, 0) > 5 -> X > 5 */
5937 return fold_build2 (GT_EXPR, type, inner, comp_const);
5939 else if (op_code == MAX_EXPR)
5940 /* MAX (X, 0) > -1 -> true */
5941 return omit_one_operand (type, integer_one_node, inner);
5943 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5944 /* MIN (X, 0) > 0 -> false
5945 MIN (X, 0) > 5 -> false */
5946 return omit_one_operand (type, integer_zero_node, inner);
5949 /* MIN (X, 0) > -1 -> X > -1 */
5950 return fold_build2 (GT_EXPR, type, inner, comp_const);
5957 /* T is an integer expression that is being multiplied, divided, or taken a
5958 modulus (CODE says which and what kind of divide or modulus) by a
5959 constant C. See if we can eliminate that operation by folding it with
5960 other operations already in T. WIDE_TYPE, if non-null, is a type that
5961 should be used for the computation if wider than our type.
5963 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5964 (X * 2) + (Y * 4). We must, however, be assured that either the original
5965 expression would not overflow or that overflow is undefined for the type
5966 in the language in question.
5968 We also canonicalize (X + 7) * 4 into X * 4 + 28 in the hope that either
5969 the machine has a multiply-accumulate insn or that this is part of an
5970 addressing calculation.
5972 If we return a non-null expression, it is an equivalent form of the
5973 original computation, but need not be in the original type.
5975 We set *STRICT_OVERFLOW_P to true if the return values depends on
5976 signed overflow being undefined. Otherwise we do not change
5977 *STRICT_OVERFLOW_P. */
5980 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
5981 bool *strict_overflow_p)
5983 /* To avoid exponential search depth, refuse to allow recursion past
5984 three levels. Beyond that (1) it's highly unlikely that we'll find
5985 something interesting and (2) we've probably processed it before
5986 when we built the inner expression. */
5995 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6002 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6003 bool *strict_overflow_p)
6005 tree type = TREE_TYPE (t);
6006 enum tree_code tcode = TREE_CODE (t);
6007 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
6008 > GET_MODE_SIZE (TYPE_MODE (type)))
6009 ? wide_type : type);
6011 int same_p = tcode == code;
6012 tree op0 = NULL_TREE, op1 = NULL_TREE;
6013 bool sub_strict_overflow_p;
6015 /* Don't deal with constants of zero here; they confuse the code below. */
6016 if (integer_zerop (c))
6019 if (TREE_CODE_CLASS (tcode) == tcc_unary)
6020 op0 = TREE_OPERAND (t, 0);
6022 if (TREE_CODE_CLASS (tcode) == tcc_binary)
6023 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6025 /* Note that we need not handle conditional operations here since fold
6026 already handles those cases. So just do arithmetic here. */
6030 /* For a constant, we can always simplify if we are a multiply
6031 or (for divide and modulus) if it is a multiple of our constant. */
6032 if (code == MULT_EXPR
6033 || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
6034 return const_binop (code, fold_convert (ctype, t),
6035 fold_convert (ctype, c), 0);
6038 case CONVERT_EXPR: case NON_LVALUE_EXPR: case NOP_EXPR:
6039 /* If op0 is an expression ... */
6040 if ((COMPARISON_CLASS_P (op0)
6041 || UNARY_CLASS_P (op0)
6042 || BINARY_CLASS_P (op0)
6043 || VL_EXP_CLASS_P (op0)
6044 || EXPRESSION_CLASS_P (op0))
6045 /* ... and is unsigned, and its type is smaller than ctype,
6046 then we cannot pass through as widening. */
6047 && ((TYPE_UNSIGNED (TREE_TYPE (op0))
6048 && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
6049 && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
6050 && (GET_MODE_SIZE (TYPE_MODE (ctype))
6051 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))))
6052 /* ... or this is a truncation (t is narrower than op0),
6053 then we cannot pass through this narrowing. */
6054 || (GET_MODE_SIZE (TYPE_MODE (type))
6055 < GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0))))
6056 /* ... or signedness changes for division or modulus,
6057 then we cannot pass through this conversion. */
6058 || (code != MULT_EXPR
6059 && (TYPE_UNSIGNED (ctype)
6060 != TYPE_UNSIGNED (TREE_TYPE (op0))))))
6063 /* Pass the constant down and see if we can make a simplification. If
6064 we can, replace this expression with the inner simplification for
6065 possible later conversion to our or some other type. */
6066 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6067 && TREE_CODE (t2) == INTEGER_CST
6068 && !TREE_OVERFLOW (t2)
6069 && (0 != (t1 = extract_muldiv (op0, t2, code,
6071 ? ctype : NULL_TREE,
6072 strict_overflow_p))))
6077 /* If widening the type changes it from signed to unsigned, then we
6078 must avoid building ABS_EXPR itself as unsigned. */
6079 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6081 tree cstype = (*signed_type_for) (ctype);
6082 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6085 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6086 return fold_convert (ctype, t1);
6092 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6094 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6097 case MIN_EXPR: case MAX_EXPR:
6098 /* If widening the type changes the signedness, then we can't perform
6099 this optimization as that changes the result. */
6100 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6103 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6104 sub_strict_overflow_p = false;
6105 if ((t1 = extract_muldiv (op0, c, code, wide_type,
6106 &sub_strict_overflow_p)) != 0
6107 && (t2 = extract_muldiv (op1, c, code, wide_type,
6108 &sub_strict_overflow_p)) != 0)
6110 if (tree_int_cst_sgn (c) < 0)
6111 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6112 if (sub_strict_overflow_p)
6113 *strict_overflow_p = true;
6114 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6115 fold_convert (ctype, t2));
6119 case LSHIFT_EXPR: case RSHIFT_EXPR:
6120 /* If the second operand is constant, this is a multiplication
6121 or floor division, by a power of two, so we can treat it that
6122 way unless the multiplier or divisor overflows. Signed
6123 left-shift overflow is implementation-defined rather than
6124 undefined in C90, so do not convert signed left shift into
6126 if (TREE_CODE (op1) == INTEGER_CST
6127 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6128 /* const_binop may not detect overflow correctly,
6129 so check for it explicitly here. */
6130 && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
6131 && TREE_INT_CST_HIGH (op1) == 0
6132 && 0 != (t1 = fold_convert (ctype,
6133 const_binop (LSHIFT_EXPR,
6136 && !TREE_OVERFLOW (t1))
6137 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6138 ? MULT_EXPR : FLOOR_DIV_EXPR,
6139 ctype, fold_convert (ctype, op0), t1),
6140 c, code, wide_type, strict_overflow_p);
6143 case PLUS_EXPR: case MINUS_EXPR:
6144 /* See if we can eliminate the operation on both sides. If we can, we
6145 can return a new PLUS or MINUS. If we can't, the only remaining
6146 cases where we can do anything are if the second operand is a
6148 sub_strict_overflow_p = false;
6149 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6150 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6151 if (t1 != 0 && t2 != 0
6152 && (code == MULT_EXPR
6153 /* If not multiplication, we can only do this if both operands
6154 are divisible by c. */
6155 || (multiple_of_p (ctype, op0, c)
6156 && multiple_of_p (ctype, op1, c))))
6158 if (sub_strict_overflow_p)
6159 *strict_overflow_p = true;
6160 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6161 fold_convert (ctype, t2));
6164 /* If this was a subtraction, negate OP1 and set it to be an addition.
6165 This simplifies the logic below. */
6166 if (tcode == MINUS_EXPR)
6167 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6169 if (TREE_CODE (op1) != INTEGER_CST)
6172 /* If either OP1 or C are negative, this optimization is not safe for
6173 some of the division and remainder types while for others we need
6174 to change the code. */
6175 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6177 if (code == CEIL_DIV_EXPR)
6178 code = FLOOR_DIV_EXPR;
6179 else if (code == FLOOR_DIV_EXPR)
6180 code = CEIL_DIV_EXPR;
6181 else if (code != MULT_EXPR
6182 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6186 /* If it's a multiply or a division/modulus operation of a multiple
6187 of our constant, do the operation and verify it doesn't overflow. */
6188 if (code == MULT_EXPR
6189 || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6191 op1 = const_binop (code, fold_convert (ctype, op1),
6192 fold_convert (ctype, c), 0);
6193 /* We allow the constant to overflow with wrapping semantics. */
6195 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6201 /* If we have an unsigned type is not a sizetype, we cannot widen
6202 the operation since it will change the result if the original
6203 computation overflowed. */
6204 if (TYPE_UNSIGNED (ctype)
6205 && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
6209 /* If we were able to eliminate our operation from the first side,
6210 apply our operation to the second side and reform the PLUS. */
6211 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
6212 return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
6214 /* The last case is if we are a multiply. In that case, we can
6215 apply the distributive law to commute the multiply and addition
6216 if the multiplication of the constants doesn't overflow. */
6217 if (code == MULT_EXPR)
6218 return fold_build2 (tcode, ctype,
6219 fold_build2 (code, ctype,
6220 fold_convert (ctype, op0),
6221 fold_convert (ctype, c)),
6227 /* We have a special case here if we are doing something like
6228 (C * 8) % 4 since we know that's zero. */
6229 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6230 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6231 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6232 && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6233 return omit_one_operand (type, integer_zero_node, op0);
6235 /* ... fall through ... */
6237 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6238 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6239 /* If we can extract our operation from the LHS, do so and return a
6240 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6241 do something only if the second operand is a constant. */
6243 && (t1 = extract_muldiv (op0, c, code, wide_type,
6244 strict_overflow_p)) != 0)
6245 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6246 fold_convert (ctype, op1));
6247 else if (tcode == MULT_EXPR && code == MULT_EXPR
6248 && (t1 = extract_muldiv (op1, c, code, wide_type,
6249 strict_overflow_p)) != 0)
6250 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6251 fold_convert (ctype, t1));
6252 else if (TREE_CODE (op1) != INTEGER_CST)
6255 /* If these are the same operation types, we can associate them
6256 assuming no overflow. */
6258 && 0 != (t1 = const_binop (MULT_EXPR, fold_convert (ctype, op1),
6259 fold_convert (ctype, c), 0))
6260 && !TREE_OVERFLOW (t1))
6261 return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1);
6263 /* If these operations "cancel" each other, we have the main
6264 optimizations of this pass, which occur when either constant is a
6265 multiple of the other, in which case we replace this with either an
6266 operation or CODE or TCODE.
6268 If we have an unsigned type that is not a sizetype, we cannot do
6269 this since it will change the result if the original computation
6271 if ((TYPE_OVERFLOW_UNDEFINED (ctype)
6272 || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
6273 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6274 || (tcode == MULT_EXPR
6275 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6276 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR)))
6278 if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6280 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6281 *strict_overflow_p = true;
6282 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6283 fold_convert (ctype,
6284 const_binop (TRUNC_DIV_EXPR,
6287 else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
6289 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6290 *strict_overflow_p = true;
6291 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6292 fold_convert (ctype,
6293 const_binop (TRUNC_DIV_EXPR,
6306 /* Return a node which has the indicated constant VALUE (either 0 or
6307 1), and is of the indicated TYPE. */
6310 constant_boolean_node (int value, tree type)
6312 if (type == integer_type_node)
6313 return value ? integer_one_node : integer_zero_node;
6314 else if (type == boolean_type_node)
6315 return value ? boolean_true_node : boolean_false_node;
6317 return build_int_cst (type, value);
6321 /* Return true if expr looks like an ARRAY_REF and set base and
6322 offset to the appropriate trees. If there is no offset,
6323 offset is set to NULL_TREE. Base will be canonicalized to
6324 something you can get the element type from using
6325 TREE_TYPE (TREE_TYPE (base)). Offset will be the offset
6326 in bytes to the base in sizetype. */
6329 extract_array_ref (tree expr, tree *base, tree *offset)
6331 /* One canonical form is a PLUS_EXPR with the first
6332 argument being an ADDR_EXPR with a possible NOP_EXPR
6334 if (TREE_CODE (expr) == POINTER_PLUS_EXPR)
6336 tree op0 = TREE_OPERAND (expr, 0);
6337 tree inner_base, dummy1;
6338 /* Strip NOP_EXPRs here because the C frontends and/or
6339 folders present us (int *)&x.a p+ 4 possibly. */
6341 if (extract_array_ref (op0, &inner_base, &dummy1))
6344 *offset = fold_convert (sizetype, TREE_OPERAND (expr, 1));
6345 if (dummy1 != NULL_TREE)
6346 *offset = fold_build2 (PLUS_EXPR, sizetype,
6351 /* Other canonical form is an ADDR_EXPR of an ARRAY_REF,
6352 which we transform into an ADDR_EXPR with appropriate
6353 offset. For other arguments to the ADDR_EXPR we assume
6354 zero offset and as such do not care about the ADDR_EXPR
6355 type and strip possible nops from it. */
6356 else if (TREE_CODE (expr) == ADDR_EXPR)
6358 tree op0 = TREE_OPERAND (expr, 0);
6359 if (TREE_CODE (op0) == ARRAY_REF)
6361 tree idx = TREE_OPERAND (op0, 1);
6362 *base = TREE_OPERAND (op0, 0);
6363 *offset = fold_build2 (MULT_EXPR, TREE_TYPE (idx), idx,
6364 array_ref_element_size (op0));
6365 *offset = fold_convert (sizetype, *offset);
6369 /* Handle array-to-pointer decay as &a. */
6370 if (TREE_CODE (TREE_TYPE (op0)) == ARRAY_TYPE)
6371 *base = TREE_OPERAND (expr, 0);
6374 *offset = NULL_TREE;
6378 /* The next canonical form is a VAR_DECL with POINTER_TYPE. */
6379 else if (SSA_VAR_P (expr)
6380 && TREE_CODE (TREE_TYPE (expr)) == POINTER_TYPE)
6383 *offset = NULL_TREE;
6391 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6392 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6393 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6394 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6395 COND is the first argument to CODE; otherwise (as in the example
6396 given here), it is the second argument. TYPE is the type of the
6397 original expression. Return NULL_TREE if no simplification is
6401 fold_binary_op_with_conditional_arg (enum tree_code code,
6402 tree type, tree op0, tree op1,
6403 tree cond, tree arg, int cond_first_p)
6405 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6406 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6407 tree test, true_value, false_value;
6408 tree lhs = NULL_TREE;
6409 tree rhs = NULL_TREE;
6411 /* This transformation is only worthwhile if we don't have to wrap
6412 arg in a SAVE_EXPR, and the operation can be simplified on at least
6413 one of the branches once its pushed inside the COND_EXPR. */
6414 if (!TREE_CONSTANT (arg))
6417 if (TREE_CODE (cond) == COND_EXPR)
6419 test = TREE_OPERAND (cond, 0);
6420 true_value = TREE_OPERAND (cond, 1);
6421 false_value = TREE_OPERAND (cond, 2);
6422 /* If this operand throws an expression, then it does not make
6423 sense to try to perform a logical or arithmetic operation
6425 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6427 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6432 tree testtype = TREE_TYPE (cond);
6434 true_value = constant_boolean_node (true, testtype);
6435 false_value = constant_boolean_node (false, testtype);
6438 arg = fold_convert (arg_type, arg);
6441 true_value = fold_convert (cond_type, true_value);
6443 lhs = fold_build2 (code, type, true_value, arg);
6445 lhs = fold_build2 (code, type, arg, true_value);
6449 false_value = fold_convert (cond_type, false_value);
6451 rhs = fold_build2 (code, type, false_value, arg);
6453 rhs = fold_build2 (code, type, arg, false_value);
6456 test = fold_build3 (COND_EXPR, type, test, lhs, rhs);
6457 return fold_convert (type, test);
6461 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6463 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6464 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6465 ADDEND is the same as X.
6467 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6468 and finite. The problematic cases are when X is zero, and its mode
6469 has signed zeros. In the case of rounding towards -infinity,
6470 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6471 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6474 fold_real_zero_addition_p (tree type, tree addend, int negate)
6476 if (!real_zerop (addend))
6479 /* Don't allow the fold with -fsignaling-nans. */
6480 if (HONOR_SNANS (TYPE_MODE (type)))
6483 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6484 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6487 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6488 if (TREE_CODE (addend) == REAL_CST
6489 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6492 /* The mode has signed zeros, and we have to honor their sign.
6493 In this situation, there is only one case we can return true for.
6494 X - 0 is the same as X unless rounding towards -infinity is
6496 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6499 /* Subroutine of fold() that checks comparisons of built-in math
6500 functions against real constants.
6502 FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6503 operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR. TYPE
6504 is the type of the result and ARG0 and ARG1 are the operands of the
6505 comparison. ARG1 must be a TREE_REAL_CST.
6507 The function returns the constant folded tree if a simplification
6508 can be made, and NULL_TREE otherwise. */
6511 fold_mathfn_compare (enum built_in_function fcode, enum tree_code code,
6512 tree type, tree arg0, tree arg1)
6516 if (BUILTIN_SQRT_P (fcode))
6518 tree arg = CALL_EXPR_ARG (arg0, 0);
6519 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6521 c = TREE_REAL_CST (arg1);
6522 if (REAL_VALUE_NEGATIVE (c))
6524 /* sqrt(x) < y is always false, if y is negative. */
6525 if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6526 return omit_one_operand (type, integer_zero_node, arg);
6528 /* sqrt(x) > y is always true, if y is negative and we
6529 don't care about NaNs, i.e. negative values of x. */
6530 if (code == NE_EXPR || !HONOR_NANS (mode))
6531 return omit_one_operand (type, integer_one_node, arg);
6533 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
6534 return fold_build2 (GE_EXPR, type, arg,
6535 build_real (TREE_TYPE (arg), dconst0));
6537 else if (code == GT_EXPR || code == GE_EXPR)
6541 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6542 real_convert (&c2, mode, &c2);
6544 if (REAL_VALUE_ISINF (c2))
6546 /* sqrt(x) > y is x == +Inf, when y is very large. */
6547 if (HONOR_INFINITIES (mode))
6548 return fold_build2 (EQ_EXPR, type, arg,
6549 build_real (TREE_TYPE (arg), c2));
6551 /* sqrt(x) > y is always false, when y is very large
6552 and we don't care about infinities. */
6553 return omit_one_operand (type, integer_zero_node, arg);
6556 /* sqrt(x) > c is the same as x > c*c. */
6557 return fold_build2 (code, type, arg,
6558 build_real (TREE_TYPE (arg), c2));
6560 else if (code == LT_EXPR || code == LE_EXPR)
6564 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6565 real_convert (&c2, mode, &c2);
6567 if (REAL_VALUE_ISINF (c2))
6569 /* sqrt(x) < y is always true, when y is a very large
6570 value and we don't care about NaNs or Infinities. */
6571 if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6572 return omit_one_operand (type, integer_one_node, arg);
6574 /* sqrt(x) < y is x != +Inf when y is very large and we
6575 don't care about NaNs. */
6576 if (! HONOR_NANS (mode))
6577 return fold_build2 (NE_EXPR, type, arg,
6578 build_real (TREE_TYPE (arg), c2));
6580 /* sqrt(x) < y is x >= 0 when y is very large and we
6581 don't care about Infinities. */
6582 if (! HONOR_INFINITIES (mode))
6583 return fold_build2 (GE_EXPR, type, arg,
6584 build_real (TREE_TYPE (arg), dconst0));
6586 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
6587 if (lang_hooks.decls.global_bindings_p () != 0
6588 || CONTAINS_PLACEHOLDER_P (arg))
6591 arg = save_expr (arg);
6592 return fold_build2 (TRUTH_ANDIF_EXPR, type,
6593 fold_build2 (GE_EXPR, type, arg,
6594 build_real (TREE_TYPE (arg),
6596 fold_build2 (NE_EXPR, type, arg,
6597 build_real (TREE_TYPE (arg),
6601 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
6602 if (! HONOR_NANS (mode))
6603 return fold_build2 (code, type, arg,
6604 build_real (TREE_TYPE (arg), c2));
6606 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
6607 if (lang_hooks.decls.global_bindings_p () == 0
6608 && ! CONTAINS_PLACEHOLDER_P (arg))
6610 arg = save_expr (arg);
6611 return fold_build2 (TRUTH_ANDIF_EXPR, type,
6612 fold_build2 (GE_EXPR, type, arg,
6613 build_real (TREE_TYPE (arg),
6615 fold_build2 (code, type, arg,
6616 build_real (TREE_TYPE (arg),
6625 /* Subroutine of fold() that optimizes comparisons against Infinities,
6626 either +Inf or -Inf.
6628 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6629 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6630 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6632 The function returns the constant folded tree if a simplification
6633 can be made, and NULL_TREE otherwise. */
6636 fold_inf_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6638 enum machine_mode mode;
6639 REAL_VALUE_TYPE max;
6643 mode = TYPE_MODE (TREE_TYPE (arg0));
6645 /* For negative infinity swap the sense of the comparison. */
6646 neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6648 code = swap_tree_comparison (code);
6653 /* x > +Inf is always false, if with ignore sNANs. */
6654 if (HONOR_SNANS (mode))
6656 return omit_one_operand (type, integer_zero_node, arg0);
6659 /* x <= +Inf is always true, if we don't case about NaNs. */
6660 if (! HONOR_NANS (mode))
6661 return omit_one_operand (type, integer_one_node, arg0);
6663 /* x <= +Inf is the same as x == x, i.e. isfinite(x). */
6664 if (lang_hooks.decls.global_bindings_p () == 0
6665 && ! CONTAINS_PLACEHOLDER_P (arg0))
6667 arg0 = save_expr (arg0);
6668 return fold_build2 (EQ_EXPR, type, arg0, arg0);
6674 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
6675 real_maxval (&max, neg, mode);
6676 return fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6677 arg0, build_real (TREE_TYPE (arg0), max));
6680 /* x < +Inf is always equal to x <= DBL_MAX. */
6681 real_maxval (&max, neg, mode);
6682 return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6683 arg0, build_real (TREE_TYPE (arg0), max));
6686 /* x != +Inf is always equal to !(x > DBL_MAX). */
6687 real_maxval (&max, neg, mode);
6688 if (! HONOR_NANS (mode))
6689 return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6690 arg0, build_real (TREE_TYPE (arg0), max));
6692 temp = fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6693 arg0, build_real (TREE_TYPE (arg0), max));
6694 return fold_build1 (TRUTH_NOT_EXPR, type, temp);
6703 /* Subroutine of fold() that optimizes comparisons of a division by
6704 a nonzero integer constant against an integer constant, i.e.
6707 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6708 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6709 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6711 The function returns the constant folded tree if a simplification
6712 can be made, and NULL_TREE otherwise. */
6715 fold_div_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6717 tree prod, tmp, hi, lo;
6718 tree arg00 = TREE_OPERAND (arg0, 0);
6719 tree arg01 = TREE_OPERAND (arg0, 1);
6720 unsigned HOST_WIDE_INT lpart;
6721 HOST_WIDE_INT hpart;
6722 bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
6726 /* We have to do this the hard way to detect unsigned overflow.
6727 prod = int_const_binop (MULT_EXPR, arg01, arg1, 0); */
6728 overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
6729 TREE_INT_CST_HIGH (arg01),
6730 TREE_INT_CST_LOW (arg1),
6731 TREE_INT_CST_HIGH (arg1),
6732 &lpart, &hpart, unsigned_p);
6733 prod = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6735 neg_overflow = false;
6739 tmp = int_const_binop (MINUS_EXPR, arg01,
6740 build_int_cst (TREE_TYPE (arg01), 1), 0);
6743 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0). */
6744 overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
6745 TREE_INT_CST_HIGH (prod),
6746 TREE_INT_CST_LOW (tmp),
6747 TREE_INT_CST_HIGH (tmp),
6748 &lpart, &hpart, unsigned_p);
6749 hi = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6750 -1, overflow | TREE_OVERFLOW (prod));
6752 else if (tree_int_cst_sgn (arg01) >= 0)
6754 tmp = int_const_binop (MINUS_EXPR, arg01,
6755 build_int_cst (TREE_TYPE (arg01), 1), 0);
6756 switch (tree_int_cst_sgn (arg1))
6759 neg_overflow = true;
6760 lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6765 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6770 hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6780 /* A negative divisor reverses the relational operators. */
6781 code = swap_tree_comparison (code);
6783 tmp = int_const_binop (PLUS_EXPR, arg01,
6784 build_int_cst (TREE_TYPE (arg01), 1), 0);
6785 switch (tree_int_cst_sgn (arg1))
6788 hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6793 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6798 neg_overflow = true;
6799 lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6811 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6812 return omit_one_operand (type, integer_zero_node, arg00);
6813 if (TREE_OVERFLOW (hi))
6814 return fold_build2 (GE_EXPR, type, arg00, lo);
6815 if (TREE_OVERFLOW (lo))
6816 return fold_build2 (LE_EXPR, type, arg00, hi);
6817 return build_range_check (type, arg00, 1, lo, hi);
6820 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6821 return omit_one_operand (type, integer_one_node, arg00);
6822 if (TREE_OVERFLOW (hi))
6823 return fold_build2 (LT_EXPR, type, arg00, lo);
6824 if (TREE_OVERFLOW (lo))
6825 return fold_build2 (GT_EXPR, type, arg00, hi);
6826 return build_range_check (type, arg00, 0, lo, hi);
6829 if (TREE_OVERFLOW (lo))
6831 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6832 return omit_one_operand (type, tmp, arg00);
6834 return fold_build2 (LT_EXPR, type, arg00, lo);
6837 if (TREE_OVERFLOW (hi))
6839 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6840 return omit_one_operand (type, tmp, arg00);
6842 return fold_build2 (LE_EXPR, type, arg00, hi);
6845 if (TREE_OVERFLOW (hi))
6847 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6848 return omit_one_operand (type, tmp, arg00);
6850 return fold_build2 (GT_EXPR, type, arg00, hi);
6853 if (TREE_OVERFLOW (lo))
6855 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6856 return omit_one_operand (type, tmp, arg00);
6858 return fold_build2 (GE_EXPR, type, arg00, lo);
6868 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6869 equality/inequality test, then return a simplified form of the test
6870 using a sign testing. Otherwise return NULL. TYPE is the desired
6874 fold_single_bit_test_into_sign_test (enum tree_code code, tree arg0, tree arg1,
6877 /* If this is testing a single bit, we can optimize the test. */
6878 if ((code == NE_EXPR || code == EQ_EXPR)
6879 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6880 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6882 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6883 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6884 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6886 if (arg00 != NULL_TREE
6887 /* This is only a win if casting to a signed type is cheap,
6888 i.e. when arg00's type is not a partial mode. */
6889 && TYPE_PRECISION (TREE_TYPE (arg00))
6890 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
6892 tree stype = signed_type_for (TREE_TYPE (arg00));
6893 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6894 result_type, fold_convert (stype, arg00),
6895 build_int_cst (stype, 0));
6902 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6903 equality/inequality test, then return a simplified form of
6904 the test using shifts and logical operations. Otherwise return
6905 NULL. TYPE is the desired result type. */
6908 fold_single_bit_test (enum tree_code code, tree arg0, tree arg1,
6911 /* If this is testing a single bit, we can optimize the test. */
6912 if ((code == NE_EXPR || code == EQ_EXPR)
6913 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6914 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6916 tree inner = TREE_OPERAND (arg0, 0);
6917 tree type = TREE_TYPE (arg0);
6918 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6919 enum machine_mode operand_mode = TYPE_MODE (type);
6921 tree signed_type, unsigned_type, intermediate_type;
6924 /* First, see if we can fold the single bit test into a sign-bit
6926 tem = fold_single_bit_test_into_sign_test (code, arg0, arg1,
6931 /* Otherwise we have (A & C) != 0 where C is a single bit,
6932 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6933 Similarly for (A & C) == 0. */
6935 /* If INNER is a right shift of a constant and it plus BITNUM does
6936 not overflow, adjust BITNUM and INNER. */
6937 if (TREE_CODE (inner) == RSHIFT_EXPR
6938 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6939 && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
6940 && bitnum < TYPE_PRECISION (type)
6941 && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
6942 bitnum - TYPE_PRECISION (type)))
6944 bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
6945 inner = TREE_OPERAND (inner, 0);
6948 /* If we are going to be able to omit the AND below, we must do our
6949 operations as unsigned. If we must use the AND, we have a choice.
6950 Normally unsigned is faster, but for some machines signed is. */
6951 #ifdef LOAD_EXTEND_OP
6952 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6953 && !flag_syntax_only) ? 0 : 1;
6958 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6959 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6960 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6961 inner = fold_convert (intermediate_type, inner);
6964 inner = build2 (RSHIFT_EXPR, intermediate_type,
6965 inner, size_int (bitnum));
6967 one = build_int_cst (intermediate_type, 1);
6969 if (code == EQ_EXPR)
6970 inner = fold_build2 (BIT_XOR_EXPR, intermediate_type, inner, one);
6972 /* Put the AND last so it can combine with more things. */
6973 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6975 /* Make sure to return the proper type. */
6976 inner = fold_convert (result_type, inner);
6983 /* Check whether we are allowed to reorder operands arg0 and arg1,
6984 such that the evaluation of arg1 occurs before arg0. */
6987 reorder_operands_p (tree arg0, tree arg1)
6989 if (! flag_evaluation_order)
6991 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6993 return ! TREE_SIDE_EFFECTS (arg0)
6994 && ! TREE_SIDE_EFFECTS (arg1);
6997 /* Test whether it is preferable two swap two operands, ARG0 and
6998 ARG1, for example because ARG0 is an integer constant and ARG1
6999 isn't. If REORDER is true, only recommend swapping if we can
7000 evaluate the operands in reverse order. */
7003 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
7005 STRIP_SIGN_NOPS (arg0);
7006 STRIP_SIGN_NOPS (arg1);
7008 if (TREE_CODE (arg1) == INTEGER_CST)
7010 if (TREE_CODE (arg0) == INTEGER_CST)
7013 if (TREE_CODE (arg1) == REAL_CST)
7015 if (TREE_CODE (arg0) == REAL_CST)
7018 if (TREE_CODE (arg1) == FIXED_CST)
7020 if (TREE_CODE (arg0) == FIXED_CST)
7023 if (TREE_CODE (arg1) == COMPLEX_CST)
7025 if (TREE_CODE (arg0) == COMPLEX_CST)
7028 if (TREE_CONSTANT (arg1))
7030 if (TREE_CONSTANT (arg0))
7036 if (reorder && flag_evaluation_order
7037 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
7040 /* It is preferable to swap two SSA_NAME to ensure a canonical form
7041 for commutative and comparison operators. Ensuring a canonical
7042 form allows the optimizers to find additional redundancies without
7043 having to explicitly check for both orderings. */
7044 if (TREE_CODE (arg0) == SSA_NAME
7045 && TREE_CODE (arg1) == SSA_NAME
7046 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
7049 /* Put SSA_NAMEs last. */
7050 if (TREE_CODE (arg1) == SSA_NAME)
7052 if (TREE_CODE (arg0) == SSA_NAME)
7055 /* Put variables last. */
7064 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
7065 ARG0 is extended to a wider type. */
7068 fold_widened_comparison (enum tree_code code, tree type, tree arg0, tree arg1)
7070 tree arg0_unw = get_unwidened (arg0, NULL_TREE);
7072 tree shorter_type, outer_type;
7076 if (arg0_unw == arg0)
7078 shorter_type = TREE_TYPE (arg0_unw);
7080 #ifdef HAVE_canonicalize_funcptr_for_compare
7081 /* Disable this optimization if we're casting a function pointer
7082 type on targets that require function pointer canonicalization. */
7083 if (HAVE_canonicalize_funcptr_for_compare
7084 && TREE_CODE (shorter_type) == POINTER_TYPE
7085 && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
7089 if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
7092 arg1_unw = get_unwidened (arg1, shorter_type);
7094 /* If possible, express the comparison in the shorter mode. */
7095 if ((code == EQ_EXPR || code == NE_EXPR
7096 || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
7097 && (TREE_TYPE (arg1_unw) == shorter_type
7098 || (TREE_CODE (arg1_unw) == INTEGER_CST
7099 && (TREE_CODE (shorter_type) == INTEGER_TYPE
7100 || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
7101 && int_fits_type_p (arg1_unw, shorter_type))))
7102 return fold_build2 (code, type, arg0_unw,
7103 fold_convert (shorter_type, arg1_unw));
7105 if (TREE_CODE (arg1_unw) != INTEGER_CST
7106 || TREE_CODE (shorter_type) != INTEGER_TYPE
7107 || !int_fits_type_p (arg1_unw, shorter_type))
7110 /* If we are comparing with the integer that does not fit into the range
7111 of the shorter type, the result is known. */
7112 outer_type = TREE_TYPE (arg1_unw);
7113 min = lower_bound_in_type (outer_type, shorter_type);
7114 max = upper_bound_in_type (outer_type, shorter_type);
7116 above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7118 below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7125 return omit_one_operand (type, integer_zero_node, arg0);
7130 return omit_one_operand (type, integer_one_node, arg0);
7136 return omit_one_operand (type, integer_one_node, arg0);
7138 return omit_one_operand (type, integer_zero_node, arg0);
7143 return omit_one_operand (type, integer_zero_node, arg0);
7145 return omit_one_operand (type, integer_one_node, arg0);
7154 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
7155 ARG0 just the signedness is changed. */
7158 fold_sign_changed_comparison (enum tree_code code, tree type,
7159 tree arg0, tree arg1)
7162 tree inner_type, outer_type;
7164 if (TREE_CODE (arg0) != NOP_EXPR
7165 && TREE_CODE (arg0) != CONVERT_EXPR)
7168 outer_type = TREE_TYPE (arg0);
7169 arg0_inner = TREE_OPERAND (arg0, 0);
7170 inner_type = TREE_TYPE (arg0_inner);
7172 #ifdef HAVE_canonicalize_funcptr_for_compare
7173 /* Disable this optimization if we're casting a function pointer
7174 type on targets that require function pointer canonicalization. */
7175 if (HAVE_canonicalize_funcptr_for_compare
7176 && TREE_CODE (inner_type) == POINTER_TYPE
7177 && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
7181 if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
7184 if (TREE_CODE (arg1) != INTEGER_CST
7185 && !((TREE_CODE (arg1) == NOP_EXPR
7186 || TREE_CODE (arg1) == CONVERT_EXPR)
7187 && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
7190 if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
7195 if (TREE_CODE (arg1) == INTEGER_CST)
7196 arg1 = force_fit_type_double (inner_type, TREE_INT_CST_LOW (arg1),
7197 TREE_INT_CST_HIGH (arg1), 0,
7198 TREE_OVERFLOW (arg1));
7200 arg1 = fold_convert (inner_type, arg1);
7202 return fold_build2 (code, type, arg0_inner, arg1);
7205 /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
7206 step of the array. Reconstructs s and delta in the case of s * delta
7207 being an integer constant (and thus already folded).
7208 ADDR is the address. MULT is the multiplicative expression.
7209 If the function succeeds, the new address expression is returned. Otherwise
7210 NULL_TREE is returned. */
7213 try_move_mult_to_index (tree addr, tree op1)
7215 tree s, delta, step;
7216 tree ref = TREE_OPERAND (addr, 0), pref;
7221 /* Strip the nops that might be added when converting op1 to sizetype. */
7224 /* Canonicalize op1 into a possibly non-constant delta
7225 and an INTEGER_CST s. */
7226 if (TREE_CODE (op1) == MULT_EXPR)
7228 tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
7233 if (TREE_CODE (arg0) == INTEGER_CST)
7238 else if (TREE_CODE (arg1) == INTEGER_CST)
7246 else if (TREE_CODE (op1) == INTEGER_CST)
7253 /* Simulate we are delta * 1. */
7255 s = integer_one_node;
7258 for (;; ref = TREE_OPERAND (ref, 0))
7260 if (TREE_CODE (ref) == ARRAY_REF)
7262 /* Remember if this was a multi-dimensional array. */
7263 if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
7266 itype = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
7270 step = array_ref_element_size (ref);
7271 if (TREE_CODE (step) != INTEGER_CST)
7276 if (! tree_int_cst_equal (step, s))
7281 /* Try if delta is a multiple of step. */
7282 tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, delta, step);
7288 /* Only fold here if we can verify we do not overflow one
7289 dimension of a multi-dimensional array. */
7294 if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
7295 || !INTEGRAL_TYPE_P (itype)
7296 || !TYPE_MAX_VALUE (itype)
7297 || TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST)
7300 tmp = fold_binary (PLUS_EXPR, itype,
7301 fold_convert (itype,
7302 TREE_OPERAND (ref, 1)),
7303 fold_convert (itype, delta));
7305 || TREE_CODE (tmp) != INTEGER_CST
7306 || tree_int_cst_lt (TYPE_MAX_VALUE (itype), tmp))
7315 if (!handled_component_p (ref))
7319 /* We found the suitable array reference. So copy everything up to it,
7320 and replace the index. */
7322 pref = TREE_OPERAND (addr, 0);
7323 ret = copy_node (pref);
7328 pref = TREE_OPERAND (pref, 0);
7329 TREE_OPERAND (pos, 0) = copy_node (pref);
7330 pos = TREE_OPERAND (pos, 0);
7333 TREE_OPERAND (pos, 1) = fold_build2 (PLUS_EXPR, itype,
7334 fold_convert (itype,
7335 TREE_OPERAND (pos, 1)),
7336 fold_convert (itype, delta));
7338 return fold_build1 (ADDR_EXPR, TREE_TYPE (addr), ret);
7342 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
7343 means A >= Y && A != MAX, but in this case we know that
7344 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7347 fold_to_nonsharp_ineq_using_bound (tree ineq, tree bound)
7349 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
7351 if (TREE_CODE (bound) == LT_EXPR)
7352 a = TREE_OPERAND (bound, 0);
7353 else if (TREE_CODE (bound) == GT_EXPR)
7354 a = TREE_OPERAND (bound, 1);
7358 typea = TREE_TYPE (a);
7359 if (!INTEGRAL_TYPE_P (typea)
7360 && !POINTER_TYPE_P (typea))
7363 if (TREE_CODE (ineq) == LT_EXPR)
7365 a1 = TREE_OPERAND (ineq, 1);
7366 y = TREE_OPERAND (ineq, 0);
7368 else if (TREE_CODE (ineq) == GT_EXPR)
7370 a1 = TREE_OPERAND (ineq, 0);
7371 y = TREE_OPERAND (ineq, 1);
7376 if (TREE_TYPE (a1) != typea)
7379 if (POINTER_TYPE_P (typea))
7381 /* Convert the pointer types into integer before taking the difference. */
7382 tree ta = fold_convert (ssizetype, a);
7383 tree ta1 = fold_convert (ssizetype, a1);
7384 diff = fold_binary (MINUS_EXPR, ssizetype, ta1, ta);
7387 diff = fold_binary (MINUS_EXPR, typea, a1, a);
7389 if (!diff || !integer_onep (diff))
7392 return fold_build2 (GE_EXPR, type, a, y);
7395 /* Fold a sum or difference of at least one multiplication.
7396 Returns the folded tree or NULL if no simplification could be made. */
7399 fold_plusminus_mult_expr (enum tree_code code, tree type, tree arg0, tree arg1)
7401 tree arg00, arg01, arg10, arg11;
7402 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7404 /* (A * C) +- (B * C) -> (A+-B) * C.
7405 (A * C) +- A -> A * (C+-1).
7406 We are most concerned about the case where C is a constant,
7407 but other combinations show up during loop reduction. Since
7408 it is not difficult, try all four possibilities. */
7410 if (TREE_CODE (arg0) == MULT_EXPR)
7412 arg00 = TREE_OPERAND (arg0, 0);
7413 arg01 = TREE_OPERAND (arg0, 1);
7415 else if (TREE_CODE (arg0) == INTEGER_CST)
7417 arg00 = build_one_cst (type);
7422 /* We cannot generate constant 1 for fract. */
7423 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7426 arg01 = build_one_cst (type);
7428 if (TREE_CODE (arg1) == MULT_EXPR)
7430 arg10 = TREE_OPERAND (arg1, 0);
7431 arg11 = TREE_OPERAND (arg1, 1);
7433 else if (TREE_CODE (arg1) == INTEGER_CST)
7435 arg10 = build_one_cst (type);
7440 /* We cannot generate constant 1 for fract. */
7441 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7444 arg11 = build_one_cst (type);
7448 if (operand_equal_p (arg01, arg11, 0))
7449 same = arg01, alt0 = arg00, alt1 = arg10;
7450 else if (operand_equal_p (arg00, arg10, 0))
7451 same = arg00, alt0 = arg01, alt1 = arg11;
7452 else if (operand_equal_p (arg00, arg11, 0))
7453 same = arg00, alt0 = arg01, alt1 = arg10;
7454 else if (operand_equal_p (arg01, arg10, 0))
7455 same = arg01, alt0 = arg00, alt1 = arg11;
7457 /* No identical multiplicands; see if we can find a common
7458 power-of-two factor in non-power-of-two multiplies. This
7459 can help in multi-dimensional array access. */
7460 else if (host_integerp (arg01, 0)
7461 && host_integerp (arg11, 0))
7463 HOST_WIDE_INT int01, int11, tmp;
7466 int01 = TREE_INT_CST_LOW (arg01);
7467 int11 = TREE_INT_CST_LOW (arg11);
7469 /* Move min of absolute values to int11. */
7470 if ((int01 >= 0 ? int01 : -int01)
7471 < (int11 >= 0 ? int11 : -int11))
7473 tmp = int01, int01 = int11, int11 = tmp;
7474 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7481 if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0)
7483 alt0 = fold_build2 (MULT_EXPR, TREE_TYPE (arg00), arg00,
7484 build_int_cst (TREE_TYPE (arg00),
7489 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7494 return fold_build2 (MULT_EXPR, type,
7495 fold_build2 (code, type,
7496 fold_convert (type, alt0),
7497 fold_convert (type, alt1)),
7498 fold_convert (type, same));
7503 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7504 specified by EXPR into the buffer PTR of length LEN bytes.
7505 Return the number of bytes placed in the buffer, or zero
7509 native_encode_int (const_tree expr, unsigned char *ptr, int len)
7511 tree type = TREE_TYPE (expr);
7512 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7513 int byte, offset, word, words;
7514 unsigned char value;
7516 if (total_bytes > len)
7518 words = total_bytes / UNITS_PER_WORD;
7520 for (byte = 0; byte < total_bytes; byte++)
7522 int bitpos = byte * BITS_PER_UNIT;
7523 if (bitpos < HOST_BITS_PER_WIDE_INT)
7524 value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7526 value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7527 >> (bitpos - HOST_BITS_PER_WIDE_INT));
7529 if (total_bytes > UNITS_PER_WORD)
7531 word = byte / UNITS_PER_WORD;
7532 if (WORDS_BIG_ENDIAN)
7533 word = (words - 1) - word;
7534 offset = word * UNITS_PER_WORD;
7535 if (BYTES_BIG_ENDIAN)
7536 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7538 offset += byte % UNITS_PER_WORD;
7541 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7542 ptr[offset] = value;
7548 /* Subroutine of native_encode_expr. Encode the REAL_CST
7549 specified by EXPR into the buffer PTR of length LEN bytes.
7550 Return the number of bytes placed in the buffer, or zero
7554 native_encode_real (const_tree expr, unsigned char *ptr, int len)
7556 tree type = TREE_TYPE (expr);
7557 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7558 int byte, offset, word, words, bitpos;
7559 unsigned char value;
7561 /* There are always 32 bits in each long, no matter the size of
7562 the hosts long. We handle floating point representations with
7566 if (total_bytes > len)
7568 words = 32 / UNITS_PER_WORD;
7570 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7572 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7573 bitpos += BITS_PER_UNIT)
7575 byte = (bitpos / BITS_PER_UNIT) & 3;
7576 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7578 if (UNITS_PER_WORD < 4)
7580 word = byte / UNITS_PER_WORD;
7581 if (WORDS_BIG_ENDIAN)
7582 word = (words - 1) - word;
7583 offset = word * UNITS_PER_WORD;
7584 if (BYTES_BIG_ENDIAN)
7585 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7587 offset += byte % UNITS_PER_WORD;
7590 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7591 ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
7596 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7597 specified by EXPR into the buffer PTR of length LEN bytes.
7598 Return the number of bytes placed in the buffer, or zero
7602 native_encode_complex (const_tree expr, unsigned char *ptr, int len)
7607 part = TREE_REALPART (expr);
7608 rsize = native_encode_expr (part, ptr, len);
7611 part = TREE_IMAGPART (expr);
7612 isize = native_encode_expr (part, ptr+rsize, len-rsize);
7615 return rsize + isize;
7619 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7620 specified by EXPR into the buffer PTR of length LEN bytes.
7621 Return the number of bytes placed in the buffer, or zero
7625 native_encode_vector (const_tree expr, unsigned char *ptr, int len)
7627 int i, size, offset, count;
7628 tree itype, elem, elements;
7631 elements = TREE_VECTOR_CST_ELTS (expr);
7632 count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
7633 itype = TREE_TYPE (TREE_TYPE (expr));
7634 size = GET_MODE_SIZE (TYPE_MODE (itype));
7635 for (i = 0; i < count; i++)
7639 elem = TREE_VALUE (elements);
7640 elements = TREE_CHAIN (elements);
7647 if (native_encode_expr (elem, ptr+offset, len-offset) != size)
7652 if (offset + size > len)
7654 memset (ptr+offset, 0, size);
7662 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7663 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7664 buffer PTR of length LEN bytes. Return the number of bytes
7665 placed in the buffer, or zero upon failure. */
7668 native_encode_expr (const_tree expr, unsigned char *ptr, int len)
7670 switch (TREE_CODE (expr))
7673 return native_encode_int (expr, ptr, len);
7676 return native_encode_real (expr, ptr, len);
7679 return native_encode_complex (expr, ptr, len);
7682 return native_encode_vector (expr, ptr, len);
7690 /* Subroutine of native_interpret_expr. Interpret the contents of
7691 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7692 If the buffer cannot be interpreted, return NULL_TREE. */
7695 native_interpret_int (tree type, const unsigned char *ptr, int len)
7697 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7698 int byte, offset, word, words;
7699 unsigned char value;
7700 unsigned int HOST_WIDE_INT lo = 0;
7701 HOST_WIDE_INT hi = 0;
7703 if (total_bytes > len)
7705 if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
7707 words = total_bytes / UNITS_PER_WORD;
7709 for (byte = 0; byte < total_bytes; byte++)
7711 int bitpos = byte * BITS_PER_UNIT;
7712 if (total_bytes > UNITS_PER_WORD)
7714 word = byte / UNITS_PER_WORD;
7715 if (WORDS_BIG_ENDIAN)
7716 word = (words - 1) - word;
7717 offset = word * UNITS_PER_WORD;
7718 if (BYTES_BIG_ENDIAN)
7719 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7721 offset += byte % UNITS_PER_WORD;
7724 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7725 value = ptr[offset];
7727 if (bitpos < HOST_BITS_PER_WIDE_INT)
7728 lo |= (unsigned HOST_WIDE_INT) value << bitpos;
7730 hi |= (unsigned HOST_WIDE_INT) value
7731 << (bitpos - HOST_BITS_PER_WIDE_INT);
7734 return build_int_cst_wide_type (type, lo, hi);
7738 /* Subroutine of native_interpret_expr. Interpret the contents of
7739 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7740 If the buffer cannot be interpreted, return NULL_TREE. */
7743 native_interpret_real (tree type, const unsigned char *ptr, int len)
7745 enum machine_mode mode = TYPE_MODE (type);
7746 int total_bytes = GET_MODE_SIZE (mode);
7747 int byte, offset, word, words, bitpos;
7748 unsigned char value;
7749 /* There are always 32 bits in each long, no matter the size of
7750 the hosts long. We handle floating point representations with
7755 total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7756 if (total_bytes > len || total_bytes > 24)
7758 words = 32 / UNITS_PER_WORD;
7760 memset (tmp, 0, sizeof (tmp));
7761 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7762 bitpos += BITS_PER_UNIT)
7764 byte = (bitpos / BITS_PER_UNIT) & 3;
7765 if (UNITS_PER_WORD < 4)
7767 word = byte / UNITS_PER_WORD;
7768 if (WORDS_BIG_ENDIAN)
7769 word = (words - 1) - word;
7770 offset = word * UNITS_PER_WORD;
7771 if (BYTES_BIG_ENDIAN)
7772 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7774 offset += byte % UNITS_PER_WORD;
7777 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7778 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7780 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7783 real_from_target (&r, tmp, mode);
7784 return build_real (type, r);
7788 /* Subroutine of native_interpret_expr. Interpret the contents of
7789 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7790 If the buffer cannot be interpreted, return NULL_TREE. */
7793 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7795 tree etype, rpart, ipart;
7798 etype = TREE_TYPE (type);
7799 size = GET_MODE_SIZE (TYPE_MODE (etype));
7802 rpart = native_interpret_expr (etype, ptr, size);
7805 ipart = native_interpret_expr (etype, ptr+size, size);
7808 return build_complex (type, rpart, ipart);
7812 /* Subroutine of native_interpret_expr. Interpret the contents of
7813 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7814 If the buffer cannot be interpreted, return NULL_TREE. */
7817 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7819 tree etype, elem, elements;
7822 etype = TREE_TYPE (type);
7823 size = GET_MODE_SIZE (TYPE_MODE (etype));
7824 count = TYPE_VECTOR_SUBPARTS (type);
7825 if (size * count > len)
7828 elements = NULL_TREE;
7829 for (i = count - 1; i >= 0; i--)
7831 elem = native_interpret_expr (etype, ptr+(i*size), size);
7834 elements = tree_cons (NULL_TREE, elem, elements);
7836 return build_vector (type, elements);
7840 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7841 the buffer PTR of length LEN as a constant of type TYPE. For
7842 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7843 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7844 return NULL_TREE. */
7847 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7849 switch (TREE_CODE (type))
7854 return native_interpret_int (type, ptr, len);
7857 return native_interpret_real (type, ptr, len);
7860 return native_interpret_complex (type, ptr, len);
7863 return native_interpret_vector (type, ptr, len);
7871 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7872 TYPE at compile-time. If we're unable to perform the conversion
7873 return NULL_TREE. */
7876 fold_view_convert_expr (tree type, tree expr)
7878 /* We support up to 512-bit values (for V8DFmode). */
7879 unsigned char buffer[64];
7882 /* Check that the host and target are sane. */
7883 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7886 len = native_encode_expr (expr, buffer, sizeof (buffer));
7890 return native_interpret_expr (type, buffer, len);
7893 /* Build an expression for the address of T. Folds away INDIRECT_REF
7894 to avoid confusing the gimplify process. When IN_FOLD is true
7895 avoid modifications of T. */
7898 build_fold_addr_expr_with_type_1 (tree t, tree ptrtype, bool in_fold)
7900 /* The size of the object is not relevant when talking about its address. */
7901 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7902 t = TREE_OPERAND (t, 0);
7904 /* Note: doesn't apply to ALIGN_INDIRECT_REF */
7905 if (TREE_CODE (t) == INDIRECT_REF
7906 || TREE_CODE (t) == MISALIGNED_INDIRECT_REF)
7908 t = TREE_OPERAND (t, 0);
7910 if (TREE_TYPE (t) != ptrtype)
7911 t = build1 (NOP_EXPR, ptrtype, t);
7917 while (handled_component_p (base))
7918 base = TREE_OPERAND (base, 0);
7921 TREE_ADDRESSABLE (base) = 1;
7923 t = build1 (ADDR_EXPR, ptrtype, t);
7926 t = build1 (ADDR_EXPR, ptrtype, t);
7931 /* Build an expression for the address of T with type PTRTYPE. This
7932 function modifies the input parameter 'T' by sometimes setting the
7933 TREE_ADDRESSABLE flag. */
7936 build_fold_addr_expr_with_type (tree t, tree ptrtype)
7938 return build_fold_addr_expr_with_type_1 (t, ptrtype, false);
7941 /* Build an expression for the address of T. This function modifies
7942 the input parameter 'T' by sometimes setting the TREE_ADDRESSABLE
7943 flag. When called from fold functions, use fold_addr_expr instead. */
7946 build_fold_addr_expr (tree t)
7948 return build_fold_addr_expr_with_type_1 (t,
7949 build_pointer_type (TREE_TYPE (t)),
7953 /* Same as build_fold_addr_expr, builds an expression for the address
7954 of T, but avoids touching the input node 't'. Fold functions
7955 should use this version. */
7958 fold_addr_expr (tree t)
7960 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7962 return build_fold_addr_expr_with_type_1 (t, ptrtype, true);
7965 /* Fold a unary expression of code CODE and type TYPE with operand
7966 OP0. Return the folded expression if folding is successful.
7967 Otherwise, return NULL_TREE. */
7970 fold_unary (enum tree_code code, tree type, tree op0)
7974 enum tree_code_class kind = TREE_CODE_CLASS (code);
7976 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7977 && TREE_CODE_LENGTH (code) == 1);
7982 if (code == NOP_EXPR || code == CONVERT_EXPR
7983 || code == FLOAT_EXPR || code == ABS_EXPR)
7985 /* Don't use STRIP_NOPS, because signedness of argument type
7987 STRIP_SIGN_NOPS (arg0);
7991 /* Strip any conversions that don't change the mode. This
7992 is safe for every expression, except for a comparison
7993 expression because its signedness is derived from its
7996 Note that this is done as an internal manipulation within
7997 the constant folder, in order to find the simplest
7998 representation of the arguments so that their form can be
7999 studied. In any cases, the appropriate type conversions
8000 should be put back in the tree that will get out of the
8006 if (TREE_CODE_CLASS (code) == tcc_unary)
8008 if (TREE_CODE (arg0) == COMPOUND_EXPR)
8009 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
8010 fold_build1 (code, type, TREE_OPERAND (arg0, 1)));
8011 else if (TREE_CODE (arg0) == COND_EXPR)
8013 tree arg01 = TREE_OPERAND (arg0, 1);
8014 tree arg02 = TREE_OPERAND (arg0, 2);
8015 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
8016 arg01 = fold_build1 (code, type, arg01);
8017 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
8018 arg02 = fold_build1 (code, type, arg02);
8019 tem = fold_build3 (COND_EXPR, type, TREE_OPERAND (arg0, 0),
8022 /* If this was a conversion, and all we did was to move into
8023 inside the COND_EXPR, bring it back out. But leave it if
8024 it is a conversion from integer to integer and the
8025 result precision is no wider than a word since such a
8026 conversion is cheap and may be optimized away by combine,
8027 while it couldn't if it were outside the COND_EXPR. Then return
8028 so we don't get into an infinite recursion loop taking the
8029 conversion out and then back in. */
8031 if ((code == NOP_EXPR || code == CONVERT_EXPR
8032 || code == NON_LVALUE_EXPR)
8033 && TREE_CODE (tem) == COND_EXPR
8034 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
8035 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
8036 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
8037 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
8038 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
8039 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
8040 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8042 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
8043 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
8044 || flag_syntax_only))
8045 tem = build1 (code, type,
8047 TREE_TYPE (TREE_OPERAND
8048 (TREE_OPERAND (tem, 1), 0)),
8049 TREE_OPERAND (tem, 0),
8050 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
8051 TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
8054 else if (COMPARISON_CLASS_P (arg0))
8056 if (TREE_CODE (type) == BOOLEAN_TYPE)
8058 arg0 = copy_node (arg0);
8059 TREE_TYPE (arg0) = type;
8062 else if (TREE_CODE (type) != INTEGER_TYPE)
8063 return fold_build3 (COND_EXPR, type, arg0,
8064 fold_build1 (code, type,
8066 fold_build1 (code, type,
8067 integer_zero_node));
8076 case FIX_TRUNC_EXPR:
8077 if (TREE_TYPE (op0) == type)
8080 /* If we have (type) (a CMP b) and type is an integral type, return
8081 new expression involving the new type. */
8082 if (COMPARISON_CLASS_P (op0) && INTEGRAL_TYPE_P (type))
8083 return fold_build2 (TREE_CODE (op0), type, TREE_OPERAND (op0, 0),
8084 TREE_OPERAND (op0, 1));
8086 /* Handle cases of two conversions in a row. */
8087 if (TREE_CODE (op0) == NOP_EXPR
8088 || TREE_CODE (op0) == CONVERT_EXPR)
8090 tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
8091 tree inter_type = TREE_TYPE (op0);
8092 int inside_int = INTEGRAL_TYPE_P (inside_type);
8093 int inside_ptr = POINTER_TYPE_P (inside_type);
8094 int inside_float = FLOAT_TYPE_P (inside_type);
8095 int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
8096 unsigned int inside_prec = TYPE_PRECISION (inside_type);
8097 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
8098 int inter_int = INTEGRAL_TYPE_P (inter_type);
8099 int inter_ptr = POINTER_TYPE_P (inter_type);
8100 int inter_float = FLOAT_TYPE_P (inter_type);
8101 int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
8102 unsigned int inter_prec = TYPE_PRECISION (inter_type);
8103 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
8104 int final_int = INTEGRAL_TYPE_P (type);
8105 int final_ptr = POINTER_TYPE_P (type);
8106 int final_float = FLOAT_TYPE_P (type);
8107 int final_vec = TREE_CODE (type) == VECTOR_TYPE;
8108 unsigned int final_prec = TYPE_PRECISION (type);
8109 int final_unsignedp = TYPE_UNSIGNED (type);
8111 /* In addition to the cases of two conversions in a row
8112 handled below, if we are converting something to its own
8113 type via an object of identical or wider precision, neither
8114 conversion is needed. */
8115 if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
8116 && (((inter_int || inter_ptr) && final_int)
8117 || (inter_float && final_float))
8118 && inter_prec >= final_prec)
8119 return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8121 /* Likewise, if the intermediate and final types are either both
8122 float or both integer, we don't need the middle conversion if
8123 it is wider than the final type and doesn't change the signedness
8124 (for integers). Avoid this if the final type is a pointer
8125 since then we sometimes need the inner conversion. Likewise if
8126 the outer has a precision not equal to the size of its mode. */
8127 if ((((inter_int || inter_ptr) && (inside_int || inside_ptr))
8128 || (inter_float && inside_float)
8129 || (inter_vec && inside_vec))
8130 && inter_prec >= inside_prec
8131 && (inter_float || inter_vec
8132 || inter_unsignedp == inside_unsignedp)
8133 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
8134 && TYPE_MODE (type) == TYPE_MODE (inter_type))
8136 && (! final_vec || inter_prec == inside_prec))
8137 return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8139 /* If we have a sign-extension of a zero-extended value, we can
8140 replace that by a single zero-extension. */
8141 if (inside_int && inter_int && final_int
8142 && inside_prec < inter_prec && inter_prec < final_prec
8143 && inside_unsignedp && !inter_unsignedp)
8144 return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8146 /* Two conversions in a row are not needed unless:
8147 - some conversion is floating-point (overstrict for now), or
8148 - some conversion is a vector (overstrict for now), or
8149 - the intermediate type is narrower than both initial and
8151 - the intermediate type and innermost type differ in signedness,
8152 and the outermost type is wider than the intermediate, or
8153 - the initial type is a pointer type and the precisions of the
8154 intermediate and final types differ, or
8155 - the final type is a pointer type and the precisions of the
8156 initial and intermediate types differ.
8157 - the final type is a pointer type and the initial type not
8158 - the initial type is a pointer to an array and the final type
8160 if (! inside_float && ! inter_float && ! final_float
8161 && ! inside_vec && ! inter_vec && ! final_vec
8162 && (inter_prec >= inside_prec || inter_prec >= final_prec)
8163 && ! (inside_int && inter_int
8164 && inter_unsignedp != inside_unsignedp
8165 && inter_prec < final_prec)
8166 && ((inter_unsignedp && inter_prec > inside_prec)
8167 == (final_unsignedp && final_prec > inter_prec))
8168 && ! (inside_ptr && inter_prec != final_prec)
8169 && ! (final_ptr && inside_prec != inter_prec)
8170 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
8171 && TYPE_MODE (type) == TYPE_MODE (inter_type))
8172 && final_ptr == inside_ptr
8174 && TREE_CODE (TREE_TYPE (inside_type)) == ARRAY_TYPE
8175 && TREE_CODE (TREE_TYPE (type)) != ARRAY_TYPE))
8176 return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8179 /* Handle (T *)&A.B.C for A being of type T and B and C
8180 living at offset zero. This occurs frequently in
8181 C++ upcasting and then accessing the base. */
8182 if (TREE_CODE (op0) == ADDR_EXPR
8183 && POINTER_TYPE_P (type)
8184 && handled_component_p (TREE_OPERAND (op0, 0)))
8186 HOST_WIDE_INT bitsize, bitpos;
8188 enum machine_mode mode;
8189 int unsignedp, volatilep;
8190 tree base = TREE_OPERAND (op0, 0);
8191 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
8192 &mode, &unsignedp, &volatilep, false);
8193 /* If the reference was to a (constant) zero offset, we can use
8194 the address of the base if it has the same base type
8195 as the result type. */
8196 if (! offset && bitpos == 0
8197 && TYPE_MAIN_VARIANT (TREE_TYPE (type))
8198 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
8199 return fold_convert (type, fold_addr_expr (base));
8202 if ((TREE_CODE (op0) == MODIFY_EXPR
8203 || TREE_CODE (op0) == GIMPLE_MODIFY_STMT)
8204 && TREE_CONSTANT (GENERIC_TREE_OPERAND (op0, 1))
8205 /* Detect assigning a bitfield. */
8206 && !(TREE_CODE (GENERIC_TREE_OPERAND (op0, 0)) == COMPONENT_REF
8208 (TREE_OPERAND (GENERIC_TREE_OPERAND (op0, 0), 1))))
8210 /* Don't leave an assignment inside a conversion
8211 unless assigning a bitfield. */
8212 tem = fold_build1 (code, type, GENERIC_TREE_OPERAND (op0, 1));
8213 /* First do the assignment, then return converted constant. */
8214 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
8215 TREE_NO_WARNING (tem) = 1;
8216 TREE_USED (tem) = 1;
8220 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
8221 constants (if x has signed type, the sign bit cannot be set
8222 in c). This folds extension into the BIT_AND_EXPR. */
8223 if (INTEGRAL_TYPE_P (type)
8224 && TREE_CODE (type) != BOOLEAN_TYPE
8225 && TREE_CODE (op0) == BIT_AND_EXPR
8226 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
8229 tree and0 = TREE_OPERAND (and, 0), and1 = TREE_OPERAND (and, 1);
8232 if (TYPE_UNSIGNED (TREE_TYPE (and))
8233 || (TYPE_PRECISION (type)
8234 <= TYPE_PRECISION (TREE_TYPE (and))))
8236 else if (TYPE_PRECISION (TREE_TYPE (and1))
8237 <= HOST_BITS_PER_WIDE_INT
8238 && host_integerp (and1, 1))
8240 unsigned HOST_WIDE_INT cst;
8242 cst = tree_low_cst (and1, 1);
8243 cst &= (HOST_WIDE_INT) -1
8244 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
8245 change = (cst == 0);
8246 #ifdef LOAD_EXTEND_OP
8248 && !flag_syntax_only
8249 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
8252 tree uns = unsigned_type_for (TREE_TYPE (and0));
8253 and0 = fold_convert (uns, and0);
8254 and1 = fold_convert (uns, and1);
8260 tem = force_fit_type_double (type, TREE_INT_CST_LOW (and1),
8261 TREE_INT_CST_HIGH (and1), 0,
8262 TREE_OVERFLOW (and1));
8263 return fold_build2 (BIT_AND_EXPR, type,
8264 fold_convert (type, and0), tem);
8268 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
8269 when one of the new casts will fold away. Conservatively we assume
8270 that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
8271 if (POINTER_TYPE_P (type)
8272 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
8273 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8274 || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
8275 || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
8277 tree arg00 = TREE_OPERAND (arg0, 0);
8278 tree arg01 = TREE_OPERAND (arg0, 1);
8280 return fold_build2 (TREE_CODE (arg0), type, fold_convert (type, arg00),
8281 fold_convert (sizetype, arg01));
8284 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
8285 of the same precision, and X is an integer type not narrower than
8286 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
8287 if (INTEGRAL_TYPE_P (type)
8288 && TREE_CODE (op0) == BIT_NOT_EXPR
8289 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8290 && (TREE_CODE (TREE_OPERAND (op0, 0)) == NOP_EXPR
8291 || TREE_CODE (TREE_OPERAND (op0, 0)) == CONVERT_EXPR)
8292 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8294 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
8295 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8296 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
8297 return fold_build1 (BIT_NOT_EXPR, type, fold_convert (type, tem));
8300 tem = fold_convert_const (code, type, op0);
8301 return tem ? tem : NULL_TREE;
8303 case FIXED_CONVERT_EXPR:
8304 tem = fold_convert_const (code, type, arg0);
8305 return tem ? tem : NULL_TREE;
8307 case VIEW_CONVERT_EXPR:
8308 if (TREE_TYPE (op0) == type)
8310 if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
8311 return fold_build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
8312 return fold_view_convert_expr (type, op0);
8315 tem = fold_negate_expr (arg0);
8317 return fold_convert (type, tem);
8321 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
8322 return fold_abs_const (arg0, type);
8323 else if (TREE_CODE (arg0) == NEGATE_EXPR)
8324 return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
8325 /* Convert fabs((double)float) into (double)fabsf(float). */
8326 else if (TREE_CODE (arg0) == NOP_EXPR
8327 && TREE_CODE (type) == REAL_TYPE)
8329 tree targ0 = strip_float_extensions (arg0);
8331 return fold_convert (type, fold_build1 (ABS_EXPR,
8335 /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on. */
8336 else if (TREE_CODE (arg0) == ABS_EXPR)
8338 else if (tree_expr_nonnegative_p (arg0))
8341 /* Strip sign ops from argument. */
8342 if (TREE_CODE (type) == REAL_TYPE)
8344 tem = fold_strip_sign_ops (arg0);
8346 return fold_build1 (ABS_EXPR, type, fold_convert (type, tem));
8351 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8352 return fold_convert (type, arg0);
8353 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8355 tree itype = TREE_TYPE (type);
8356 tree rpart = fold_convert (itype, TREE_OPERAND (arg0, 0));
8357 tree ipart = fold_convert (itype, TREE_OPERAND (arg0, 1));
8358 return fold_build2 (COMPLEX_EXPR, type, rpart, negate_expr (ipart));
8360 if (TREE_CODE (arg0) == COMPLEX_CST)
8362 tree itype = TREE_TYPE (type);
8363 tree rpart = fold_convert (itype, TREE_REALPART (arg0));
8364 tree ipart = fold_convert (itype, TREE_IMAGPART (arg0));
8365 return build_complex (type, rpart, negate_expr (ipart));
8367 if (TREE_CODE (arg0) == CONJ_EXPR)
8368 return fold_convert (type, TREE_OPERAND (arg0, 0));
8372 if (TREE_CODE (arg0) == INTEGER_CST)
8373 return fold_not_const (arg0, type);
8374 else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8375 return TREE_OPERAND (arg0, 0);
8376 /* Convert ~ (-A) to A - 1. */
8377 else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8378 return fold_build2 (MINUS_EXPR, type, TREE_OPERAND (arg0, 0),
8379 build_int_cst (type, 1));
8380 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
8381 else if (INTEGRAL_TYPE_P (type)
8382 && ((TREE_CODE (arg0) == MINUS_EXPR
8383 && integer_onep (TREE_OPERAND (arg0, 1)))
8384 || (TREE_CODE (arg0) == PLUS_EXPR
8385 && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8386 return fold_build1 (NEGATE_EXPR, type, TREE_OPERAND (arg0, 0));
8387 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
8388 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8389 && (tem = fold_unary (BIT_NOT_EXPR, type,
8391 TREE_OPERAND (arg0, 0)))))
8392 return fold_build2 (BIT_XOR_EXPR, type, tem,
8393 fold_convert (type, TREE_OPERAND (arg0, 1)));
8394 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8395 && (tem = fold_unary (BIT_NOT_EXPR, type,
8397 TREE_OPERAND (arg0, 1)))))
8398 return fold_build2 (BIT_XOR_EXPR, type,
8399 fold_convert (type, TREE_OPERAND (arg0, 0)), tem);
8403 case TRUTH_NOT_EXPR:
8404 /* The argument to invert_truthvalue must have Boolean type. */
8405 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8406 arg0 = fold_convert (boolean_type_node, arg0);
8408 /* Note that the operand of this must be an int
8409 and its values must be 0 or 1.
8410 ("true" is a fixed value perhaps depending on the language,
8411 but we don't handle values other than 1 correctly yet.) */
8412 tem = fold_truth_not_expr (arg0);
8415 return fold_convert (type, tem);
8418 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8419 return fold_convert (type, arg0);
8420 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8421 return omit_one_operand (type, TREE_OPERAND (arg0, 0),
8422 TREE_OPERAND (arg0, 1));
8423 if (TREE_CODE (arg0) == COMPLEX_CST)
8424 return fold_convert (type, TREE_REALPART (arg0));
8425 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8427 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8428 tem = fold_build2 (TREE_CODE (arg0), itype,
8429 fold_build1 (REALPART_EXPR, itype,
8430 TREE_OPERAND (arg0, 0)),
8431 fold_build1 (REALPART_EXPR, itype,
8432 TREE_OPERAND (arg0, 1)));
8433 return fold_convert (type, tem);
8435 if (TREE_CODE (arg0) == CONJ_EXPR)
8437 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8438 tem = fold_build1 (REALPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8439 return fold_convert (type, tem);
8441 if (TREE_CODE (arg0) == CALL_EXPR)
8443 tree fn = get_callee_fndecl (arg0);
8444 if (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8445 switch (DECL_FUNCTION_CODE (fn))
8447 CASE_FLT_FN (BUILT_IN_CEXPI):
8448 fn = mathfn_built_in (type, BUILT_IN_COS);
8450 return build_call_expr (fn, 1, CALL_EXPR_ARG (arg0, 0));
8460 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8461 return fold_convert (type, integer_zero_node);
8462 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8463 return omit_one_operand (type, TREE_OPERAND (arg0, 1),
8464 TREE_OPERAND (arg0, 0));
8465 if (TREE_CODE (arg0) == COMPLEX_CST)
8466 return fold_convert (type, TREE_IMAGPART (arg0));
8467 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8469 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8470 tem = fold_build2 (TREE_CODE (arg0), itype,
8471 fold_build1 (IMAGPART_EXPR, itype,
8472 TREE_OPERAND (arg0, 0)),
8473 fold_build1 (IMAGPART_EXPR, itype,
8474 TREE_OPERAND (arg0, 1)));
8475 return fold_convert (type, tem);
8477 if (TREE_CODE (arg0) == CONJ_EXPR)
8479 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8480 tem = fold_build1 (IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8481 return fold_convert (type, negate_expr (tem));
8483 if (TREE_CODE (arg0) == CALL_EXPR)
8485 tree fn = get_callee_fndecl (arg0);
8486 if (DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8487 switch (DECL_FUNCTION_CODE (fn))
8489 CASE_FLT_FN (BUILT_IN_CEXPI):
8490 fn = mathfn_built_in (type, BUILT_IN_SIN);
8492 return build_call_expr (fn, 1, CALL_EXPR_ARG (arg0, 0));
8503 } /* switch (code) */
8506 /* Fold a binary expression of code CODE and type TYPE with operands
8507 OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8508 Return the folded expression if folding is successful. Otherwise,
8509 return NULL_TREE. */
8512 fold_minmax (enum tree_code code, tree type, tree op0, tree op1)
8514 enum tree_code compl_code;
8516 if (code == MIN_EXPR)
8517 compl_code = MAX_EXPR;
8518 else if (code == MAX_EXPR)
8519 compl_code = MIN_EXPR;
8523 /* MIN (MAX (a, b), b) == b. */
8524 if (TREE_CODE (op0) == compl_code
8525 && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8526 return omit_one_operand (type, op1, TREE_OPERAND (op0, 0));
8528 /* MIN (MAX (b, a), b) == b. */
8529 if (TREE_CODE (op0) == compl_code
8530 && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8531 && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8532 return omit_one_operand (type, op1, TREE_OPERAND (op0, 1));
8534 /* MIN (a, MAX (a, b)) == a. */
8535 if (TREE_CODE (op1) == compl_code
8536 && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8537 && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8538 return omit_one_operand (type, op0, TREE_OPERAND (op1, 1));
8540 /* MIN (a, MAX (b, a)) == a. */
8541 if (TREE_CODE (op1) == compl_code
8542 && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8543 && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8544 return omit_one_operand (type, op0, TREE_OPERAND (op1, 0));
8549 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8550 by changing CODE to reduce the magnitude of constants involved in
8551 ARG0 of the comparison.
8552 Returns a canonicalized comparison tree if a simplification was
8553 possible, otherwise returns NULL_TREE.
8554 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8555 valid if signed overflow is undefined. */
8558 maybe_canonicalize_comparison_1 (enum tree_code code, tree type,
8559 tree arg0, tree arg1,
8560 bool *strict_overflow_p)
8562 enum tree_code code0 = TREE_CODE (arg0);
8563 tree t, cst0 = NULL_TREE;
8567 /* Match A +- CST code arg1 and CST code arg1. */
8568 if (!(((code0 == MINUS_EXPR
8569 || code0 == PLUS_EXPR)
8570 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8571 || code0 == INTEGER_CST))
8574 /* Identify the constant in arg0 and its sign. */
8575 if (code0 == INTEGER_CST)
8578 cst0 = TREE_OPERAND (arg0, 1);
8579 sgn0 = tree_int_cst_sgn (cst0);
8581 /* Overflowed constants and zero will cause problems. */
8582 if (integer_zerop (cst0)
8583 || TREE_OVERFLOW (cst0))
8586 /* See if we can reduce the magnitude of the constant in
8587 arg0 by changing the comparison code. */
8588 if (code0 == INTEGER_CST)
8590 /* CST <= arg1 -> CST-1 < arg1. */
8591 if (code == LE_EXPR && sgn0 == 1)
8593 /* -CST < arg1 -> -CST-1 <= arg1. */
8594 else if (code == LT_EXPR && sgn0 == -1)
8596 /* CST > arg1 -> CST-1 >= arg1. */
8597 else if (code == GT_EXPR && sgn0 == 1)
8599 /* -CST >= arg1 -> -CST-1 > arg1. */
8600 else if (code == GE_EXPR && sgn0 == -1)
8604 /* arg1 code' CST' might be more canonical. */
8609 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8611 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8613 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8614 else if (code == GT_EXPR
8615 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8617 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8618 else if (code == LE_EXPR
8619 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8621 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8622 else if (code == GE_EXPR
8623 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8627 *strict_overflow_p = true;
8630 /* Now build the constant reduced in magnitude. */
8631 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8632 cst0, build_int_cst (TREE_TYPE (cst0), 1), 0);
8633 if (code0 != INTEGER_CST)
8634 t = fold_build2 (code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8636 /* If swapping might yield to a more canonical form, do so. */
8638 return fold_build2 (swap_tree_comparison (code), type, arg1, t);
8640 return fold_build2 (code, type, t, arg1);
8643 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8644 overflow further. Try to decrease the magnitude of constants involved
8645 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8646 and put sole constants at the second argument position.
8647 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8650 maybe_canonicalize_comparison (enum tree_code code, tree type,
8651 tree arg0, tree arg1)
8654 bool strict_overflow_p;
8655 const char * const warnmsg = G_("assuming signed overflow does not occur "
8656 "when reducing constant in comparison");
8658 /* In principle pointers also have undefined overflow behavior,
8659 but that causes problems elsewhere. */
8660 if (!TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8661 || POINTER_TYPE_P (TREE_TYPE (arg0)))
8664 /* Try canonicalization by simplifying arg0. */
8665 strict_overflow_p = false;
8666 t = maybe_canonicalize_comparison_1 (code, type, arg0, arg1,
8667 &strict_overflow_p);
8670 if (strict_overflow_p)
8671 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8675 /* Try canonicalization by simplifying arg1 using the swapped
8677 code = swap_tree_comparison (code);
8678 strict_overflow_p = false;
8679 t = maybe_canonicalize_comparison_1 (code, type, arg1, arg0,
8680 &strict_overflow_p);
8681 if (t && strict_overflow_p)
8682 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8686 /* Subroutine of fold_binary. This routine performs all of the
8687 transformations that are common to the equality/inequality
8688 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8689 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8690 fold_binary should call fold_binary. Fold a comparison with
8691 tree code CODE and type TYPE with operands OP0 and OP1. Return
8692 the folded comparison or NULL_TREE. */
8695 fold_comparison (enum tree_code code, tree type, tree op0, tree op1)
8697 tree arg0, arg1, tem;
8702 STRIP_SIGN_NOPS (arg0);
8703 STRIP_SIGN_NOPS (arg1);
8705 tem = fold_relational_const (code, type, arg0, arg1);
8706 if (tem != NULL_TREE)
8709 /* If one arg is a real or integer constant, put it last. */
8710 if (tree_swap_operands_p (arg0, arg1, true))
8711 return fold_build2 (swap_tree_comparison (code), type, op1, op0);
8713 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1. */
8714 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8715 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8716 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8717 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
8718 && (TREE_CODE (arg1) == INTEGER_CST
8719 && !TREE_OVERFLOW (arg1)))
8721 tree const1 = TREE_OPERAND (arg0, 1);
8723 tree variable = TREE_OPERAND (arg0, 0);
8726 lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
8728 lhs = fold_build2 (lhs_add ? PLUS_EXPR : MINUS_EXPR,
8729 TREE_TYPE (arg1), const2, const1);
8731 /* If the constant operation overflowed this can be
8732 simplified as a comparison against INT_MAX/INT_MIN. */
8733 if (TREE_CODE (lhs) == INTEGER_CST
8734 && TREE_OVERFLOW (lhs))
8736 int const1_sgn = tree_int_cst_sgn (const1);
8737 enum tree_code code2 = code;
8739 /* Get the sign of the constant on the lhs if the
8740 operation were VARIABLE + CONST1. */
8741 if (TREE_CODE (arg0) == MINUS_EXPR)
8742 const1_sgn = -const1_sgn;
8744 /* The sign of the constant determines if we overflowed
8745 INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8746 Canonicalize to the INT_MIN overflow by swapping the comparison
8748 if (const1_sgn == -1)
8749 code2 = swap_tree_comparison (code);
8751 /* We now can look at the canonicalized case
8752 VARIABLE + 1 CODE2 INT_MIN
8753 and decide on the result. */
8754 if (code2 == LT_EXPR
8756 || code2 == EQ_EXPR)
8757 return omit_one_operand (type, boolean_false_node, variable);
8758 else if (code2 == NE_EXPR
8760 || code2 == GT_EXPR)
8761 return omit_one_operand (type, boolean_true_node, variable);
8764 if (TREE_CODE (lhs) == TREE_CODE (arg1)
8765 && (TREE_CODE (lhs) != INTEGER_CST
8766 || !TREE_OVERFLOW (lhs)))
8768 fold_overflow_warning (("assuming signed overflow does not occur "
8769 "when changing X +- C1 cmp C2 to "
8771 WARN_STRICT_OVERFLOW_COMPARISON);
8772 return fold_build2 (code, type, variable, lhs);
8776 /* For comparisons of pointers we can decompose it to a compile time
8777 comparison of the base objects and the offsets into the object.
8778 This requires at least one operand being an ADDR_EXPR to do more
8779 than the operand_equal_p test below. */
8780 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8781 && (TREE_CODE (arg0) == ADDR_EXPR
8782 || TREE_CODE (arg1) == ADDR_EXPR))
8784 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8785 HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8786 enum machine_mode mode;
8787 int volatilep, unsignedp;
8788 bool indirect_base0 = false;
8790 /* Get base and offset for the access. Strip ADDR_EXPR for
8791 get_inner_reference, but put it back by stripping INDIRECT_REF
8792 off the base object if possible. */
8794 if (TREE_CODE (arg0) == ADDR_EXPR)
8796 base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
8797 &bitsize, &bitpos0, &offset0, &mode,
8798 &unsignedp, &volatilep, false);
8799 if (TREE_CODE (base0) == INDIRECT_REF)
8800 base0 = TREE_OPERAND (base0, 0);
8802 indirect_base0 = true;
8806 if (TREE_CODE (arg1) == ADDR_EXPR)
8808 base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
8809 &bitsize, &bitpos1, &offset1, &mode,
8810 &unsignedp, &volatilep, false);
8811 /* We have to make sure to have an indirect/non-indirect base1
8812 just the same as we did for base0. */
8813 if (TREE_CODE (base1) == INDIRECT_REF
8815 base1 = TREE_OPERAND (base1, 0);
8816 else if (!indirect_base0)
8819 else if (indirect_base0)
8822 /* If we have equivalent bases we might be able to simplify. */
8824 && operand_equal_p (base0, base1, 0))
8826 /* We can fold this expression to a constant if the non-constant
8827 offset parts are equal. */
8828 if (offset0 == offset1
8829 || (offset0 && offset1
8830 && operand_equal_p (offset0, offset1, 0)))
8835 return build_int_cst (boolean_type_node, bitpos0 == bitpos1);
8837 return build_int_cst (boolean_type_node, bitpos0 != bitpos1);
8839 return build_int_cst (boolean_type_node, bitpos0 < bitpos1);
8841 return build_int_cst (boolean_type_node, bitpos0 <= bitpos1);
8843 return build_int_cst (boolean_type_node, bitpos0 >= bitpos1);
8845 return build_int_cst (boolean_type_node, bitpos0 > bitpos1);
8849 /* We can simplify the comparison to a comparison of the variable
8850 offset parts if the constant offset parts are equal.
8851 Be careful to use signed size type here because otherwise we
8852 mess with array offsets in the wrong way. This is possible
8853 because pointer arithmetic is restricted to retain within an
8854 object and overflow on pointer differences is undefined as of
8855 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8856 else if (bitpos0 == bitpos1)
8858 tree signed_size_type_node;
8859 signed_size_type_node = signed_type_for (size_type_node);
8861 /* By converting to signed size type we cover middle-end pointer
8862 arithmetic which operates on unsigned pointer types of size
8863 type size and ARRAY_REF offsets which are properly sign or
8864 zero extended from their type in case it is narrower than
8866 if (offset0 == NULL_TREE)
8867 offset0 = build_int_cst (signed_size_type_node, 0);
8869 offset0 = fold_convert (signed_size_type_node, offset0);
8870 if (offset1 == NULL_TREE)
8871 offset1 = build_int_cst (signed_size_type_node, 0);
8873 offset1 = fold_convert (signed_size_type_node, offset1);
8875 return fold_build2 (code, type, offset0, offset1);
8880 /* If this is a comparison of two exprs that look like an ARRAY_REF of the
8881 same object, then we can fold this to a comparison of the two offsets in
8882 signed size type. This is possible because pointer arithmetic is
8883 restricted to retain within an object and overflow on pointer differences
8884 is undefined as of 6.5.6/8 and /9 with respect to the signed ptrdiff_t.
8886 We check flag_wrapv directly because pointers types are unsigned,
8887 and therefore TYPE_OVERFLOW_WRAPS returns true for them. That is
8888 normally what we want to avoid certain odd overflow cases, but
8890 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8892 && !TYPE_OVERFLOW_TRAPS (TREE_TYPE (arg0)))
8894 tree base0, offset0, base1, offset1;
8896 if (extract_array_ref (arg0, &base0, &offset0)
8897 && extract_array_ref (arg1, &base1, &offset1)
8898 && operand_equal_p (base0, base1, 0))
8900 tree signed_size_type_node;
8901 signed_size_type_node = signed_type_for (size_type_node);
8903 /* By converting to signed size type we cover middle-end pointer
8904 arithmetic which operates on unsigned pointer types of size
8905 type size and ARRAY_REF offsets which are properly sign or
8906 zero extended from their type in case it is narrower than
8908 if (offset0 == NULL_TREE)
8909 offset0 = build_int_cst (signed_size_type_node, 0);
8911 offset0 = fold_convert (signed_size_type_node, offset0);
8912 if (offset1 == NULL_TREE)
8913 offset1 = build_int_cst (signed_size_type_node, 0);
8915 offset1 = fold_convert (signed_size_type_node, offset1);
8917 return fold_build2 (code, type, offset0, offset1);
8921 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8922 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8923 the resulting offset is smaller in absolute value than the
8925 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8926 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8927 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8928 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8929 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8930 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8931 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8933 tree const1 = TREE_OPERAND (arg0, 1);
8934 tree const2 = TREE_OPERAND (arg1, 1);
8935 tree variable1 = TREE_OPERAND (arg0, 0);
8936 tree variable2 = TREE_OPERAND (arg1, 0);
8938 const char * const warnmsg = G_("assuming signed overflow does not "
8939 "occur when combining constants around "
8942 /* Put the constant on the side where it doesn't overflow and is
8943 of lower absolute value than before. */
8944 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8945 ? MINUS_EXPR : PLUS_EXPR,
8947 if (!TREE_OVERFLOW (cst)
8948 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
8950 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8951 return fold_build2 (code, type,
8953 fold_build2 (TREE_CODE (arg1), TREE_TYPE (arg1),
8957 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8958 ? MINUS_EXPR : PLUS_EXPR,
8960 if (!TREE_OVERFLOW (cst)
8961 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
8963 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8964 return fold_build2 (code, type,
8965 fold_build2 (TREE_CODE (arg0), TREE_TYPE (arg0),
8971 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
8972 signed arithmetic case. That form is created by the compiler
8973 often enough for folding it to be of value. One example is in
8974 computing loop trip counts after Operator Strength Reduction. */
8975 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8976 && TREE_CODE (arg0) == MULT_EXPR
8977 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8978 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8979 && integer_zerop (arg1))
8981 tree const1 = TREE_OPERAND (arg0, 1);
8982 tree const2 = arg1; /* zero */
8983 tree variable1 = TREE_OPERAND (arg0, 0);
8984 enum tree_code cmp_code = code;
8986 gcc_assert (!integer_zerop (const1));
8988 fold_overflow_warning (("assuming signed overflow does not occur when "
8989 "eliminating multiplication in comparison "
8991 WARN_STRICT_OVERFLOW_COMPARISON);
8993 /* If const1 is negative we swap the sense of the comparison. */
8994 if (tree_int_cst_sgn (const1) < 0)
8995 cmp_code = swap_tree_comparison (cmp_code);
8997 return fold_build2 (cmp_code, type, variable1, const2);
9000 tem = maybe_canonicalize_comparison (code, type, op0, op1);
9004 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
9006 tree targ0 = strip_float_extensions (arg0);
9007 tree targ1 = strip_float_extensions (arg1);
9008 tree newtype = TREE_TYPE (targ0);
9010 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
9011 newtype = TREE_TYPE (targ1);
9013 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
9014 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
9015 return fold_build2 (code, type, fold_convert (newtype, targ0),
9016 fold_convert (newtype, targ1));
9018 /* (-a) CMP (-b) -> b CMP a */
9019 if (TREE_CODE (arg0) == NEGATE_EXPR
9020 && TREE_CODE (arg1) == NEGATE_EXPR)
9021 return fold_build2 (code, type, TREE_OPERAND (arg1, 0),
9022 TREE_OPERAND (arg0, 0));
9024 if (TREE_CODE (arg1) == REAL_CST)
9026 REAL_VALUE_TYPE cst;
9027 cst = TREE_REAL_CST (arg1);
9029 /* (-a) CMP CST -> a swap(CMP) (-CST) */
9030 if (TREE_CODE (arg0) == NEGATE_EXPR)
9031 return fold_build2 (swap_tree_comparison (code), type,
9032 TREE_OPERAND (arg0, 0),
9033 build_real (TREE_TYPE (arg1),
9034 REAL_VALUE_NEGATE (cst)));
9036 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
9037 /* a CMP (-0) -> a CMP 0 */
9038 if (REAL_VALUE_MINUS_ZERO (cst))
9039 return fold_build2 (code, type, arg0,
9040 build_real (TREE_TYPE (arg1), dconst0));
9042 /* x != NaN is always true, other ops are always false. */
9043 if (REAL_VALUE_ISNAN (cst)
9044 && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
9046 tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
9047 return omit_one_operand (type, tem, arg0);
9050 /* Fold comparisons against infinity. */
9051 if (REAL_VALUE_ISINF (cst))
9053 tem = fold_inf_compare (code, type, arg0, arg1);
9054 if (tem != NULL_TREE)
9059 /* If this is a comparison of a real constant with a PLUS_EXPR
9060 or a MINUS_EXPR of a real constant, we can convert it into a
9061 comparison with a revised real constant as long as no overflow
9062 occurs when unsafe_math_optimizations are enabled. */
9063 if (flag_unsafe_math_optimizations
9064 && TREE_CODE (arg1) == REAL_CST
9065 && (TREE_CODE (arg0) == PLUS_EXPR
9066 || TREE_CODE (arg0) == MINUS_EXPR)
9067 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
9068 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
9069 ? MINUS_EXPR : PLUS_EXPR,
9070 arg1, TREE_OPERAND (arg0, 1), 0))
9071 && !TREE_OVERFLOW (tem))
9072 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
9074 /* Likewise, we can simplify a comparison of a real constant with
9075 a MINUS_EXPR whose first operand is also a real constant, i.e.
9076 (c1 - x) < c2 becomes x > c1-c2. */
9077 if (flag_unsafe_math_optimizations
9078 && TREE_CODE (arg1) == REAL_CST
9079 && TREE_CODE (arg0) == MINUS_EXPR
9080 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
9081 && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
9083 && !TREE_OVERFLOW (tem))
9084 return fold_build2 (swap_tree_comparison (code), type,
9085 TREE_OPERAND (arg0, 1), tem);
9087 /* Fold comparisons against built-in math functions. */
9088 if (TREE_CODE (arg1) == REAL_CST
9089 && flag_unsafe_math_optimizations
9090 && ! flag_errno_math)
9092 enum built_in_function fcode = builtin_mathfn_code (arg0);
9094 if (fcode != END_BUILTINS)
9096 tem = fold_mathfn_compare (fcode, code, type, arg0, arg1);
9097 if (tem != NULL_TREE)
9103 if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
9104 && (TREE_CODE (arg0) == NOP_EXPR
9105 || TREE_CODE (arg0) == CONVERT_EXPR))
9107 /* If we are widening one operand of an integer comparison,
9108 see if the other operand is similarly being widened. Perhaps we
9109 can do the comparison in the narrower type. */
9110 tem = fold_widened_comparison (code, type, arg0, arg1);
9114 /* Or if we are changing signedness. */
9115 tem = fold_sign_changed_comparison (code, type, arg0, arg1);
9120 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
9121 constant, we can simplify it. */
9122 if (TREE_CODE (arg1) == INTEGER_CST
9123 && (TREE_CODE (arg0) == MIN_EXPR
9124 || TREE_CODE (arg0) == MAX_EXPR)
9125 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9127 tem = optimize_minmax_comparison (code, type, op0, op1);
9132 /* Simplify comparison of something with itself. (For IEEE
9133 floating-point, we can only do some of these simplifications.) */
9134 if (operand_equal_p (arg0, arg1, 0))
9139 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9140 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9141 return constant_boolean_node (1, type);
9146 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9147 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9148 return constant_boolean_node (1, type);
9149 return fold_build2 (EQ_EXPR, type, arg0, arg1);
9152 /* For NE, we can only do this simplification if integer
9153 or we don't honor IEEE floating point NaNs. */
9154 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9155 && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9157 /* ... fall through ... */
9160 return constant_boolean_node (0, type);
9166 /* If we are comparing an expression that just has comparisons
9167 of two integer values, arithmetic expressions of those comparisons,
9168 and constants, we can simplify it. There are only three cases
9169 to check: the two values can either be equal, the first can be
9170 greater, or the second can be greater. Fold the expression for
9171 those three values. Since each value must be 0 or 1, we have
9172 eight possibilities, each of which corresponds to the constant 0
9173 or 1 or one of the six possible comparisons.
9175 This handles common cases like (a > b) == 0 but also handles
9176 expressions like ((x > y) - (y > x)) > 0, which supposedly
9177 occur in macroized code. */
9179 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9181 tree cval1 = 0, cval2 = 0;
9184 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9185 /* Don't handle degenerate cases here; they should already
9186 have been handled anyway. */
9187 && cval1 != 0 && cval2 != 0
9188 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9189 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9190 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9191 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9192 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9193 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9194 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9196 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9197 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9199 /* We can't just pass T to eval_subst in case cval1 or cval2
9200 was the same as ARG1. */
9203 = fold_build2 (code, type,
9204 eval_subst (arg0, cval1, maxval,
9208 = fold_build2 (code, type,
9209 eval_subst (arg0, cval1, maxval,
9213 = fold_build2 (code, type,
9214 eval_subst (arg0, cval1, minval,
9218 /* All three of these results should be 0 or 1. Confirm they are.
9219 Then use those values to select the proper code to use. */
9221 if (TREE_CODE (high_result) == INTEGER_CST
9222 && TREE_CODE (equal_result) == INTEGER_CST
9223 && TREE_CODE (low_result) == INTEGER_CST)
9225 /* Make a 3-bit mask with the high-order bit being the
9226 value for `>', the next for '=', and the low for '<'. */
9227 switch ((integer_onep (high_result) * 4)
9228 + (integer_onep (equal_result) * 2)
9229 + integer_onep (low_result))
9233 return omit_one_operand (type, integer_zero_node, arg0);
9254 return omit_one_operand (type, integer_one_node, arg0);
9258 return save_expr (build2 (code, type, cval1, cval2));
9259 return fold_build2 (code, type, cval1, cval2);
9264 /* Fold a comparison of the address of COMPONENT_REFs with the same
9265 type and component to a comparison of the address of the base
9266 object. In short, &x->a OP &y->a to x OP y and
9267 &x->a OP &y.a to x OP &y */
9268 if (TREE_CODE (arg0) == ADDR_EXPR
9269 && TREE_CODE (TREE_OPERAND (arg0, 0)) == COMPONENT_REF
9270 && TREE_CODE (arg1) == ADDR_EXPR
9271 && TREE_CODE (TREE_OPERAND (arg1, 0)) == COMPONENT_REF)
9273 tree cref0 = TREE_OPERAND (arg0, 0);
9274 tree cref1 = TREE_OPERAND (arg1, 0);
9275 if (TREE_OPERAND (cref0, 1) == TREE_OPERAND (cref1, 1))
9277 tree op0 = TREE_OPERAND (cref0, 0);
9278 tree op1 = TREE_OPERAND (cref1, 0);
9279 return fold_build2 (code, type,
9280 fold_addr_expr (op0),
9281 fold_addr_expr (op1));
9285 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9286 into a single range test. */
9287 if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9288 || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9289 && TREE_CODE (arg1) == INTEGER_CST
9290 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9291 && !integer_zerop (TREE_OPERAND (arg0, 1))
9292 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9293 && !TREE_OVERFLOW (arg1))
9295 tem = fold_div_compare (code, type, arg0, arg1);
9296 if (tem != NULL_TREE)
9300 /* Fold ~X op ~Y as Y op X. */
9301 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9302 && TREE_CODE (arg1) == BIT_NOT_EXPR)
9304 tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9305 return fold_build2 (code, type,
9306 fold_convert (cmp_type, TREE_OPERAND (arg1, 0)),
9307 TREE_OPERAND (arg0, 0));
9310 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
9311 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9312 && TREE_CODE (arg1) == INTEGER_CST)
9314 tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9315 return fold_build2 (swap_tree_comparison (code), type,
9316 TREE_OPERAND (arg0, 0),
9317 fold_build1 (BIT_NOT_EXPR, cmp_type,
9318 fold_convert (cmp_type, arg1)));
9325 /* Subroutine of fold_binary. Optimize complex multiplications of the
9326 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
9327 argument EXPR represents the expression "z" of type TYPE. */
9330 fold_mult_zconjz (tree type, tree expr)
9332 tree itype = TREE_TYPE (type);
9333 tree rpart, ipart, tem;
9335 if (TREE_CODE (expr) == COMPLEX_EXPR)
9337 rpart = TREE_OPERAND (expr, 0);
9338 ipart = TREE_OPERAND (expr, 1);
9340 else if (TREE_CODE (expr) == COMPLEX_CST)
9342 rpart = TREE_REALPART (expr);
9343 ipart = TREE_IMAGPART (expr);
9347 expr = save_expr (expr);
9348 rpart = fold_build1 (REALPART_EXPR, itype, expr);
9349 ipart = fold_build1 (IMAGPART_EXPR, itype, expr);
9352 rpart = save_expr (rpart);
9353 ipart = save_expr (ipart);
9354 tem = fold_build2 (PLUS_EXPR, itype,
9355 fold_build2 (MULT_EXPR, itype, rpart, rpart),
9356 fold_build2 (MULT_EXPR, itype, ipart, ipart));
9357 return fold_build2 (COMPLEX_EXPR, type, tem,
9358 fold_convert (itype, integer_zero_node));
9362 /* Fold a binary expression of code CODE and type TYPE with operands
9363 OP0 and OP1. Return the folded expression if folding is
9364 successful. Otherwise, return NULL_TREE. */
9367 fold_binary (enum tree_code code, tree type, tree op0, tree op1)
9369 enum tree_code_class kind = TREE_CODE_CLASS (code);
9370 tree arg0, arg1, tem;
9371 tree t1 = NULL_TREE;
9372 bool strict_overflow_p;
9374 gcc_assert ((IS_EXPR_CODE_CLASS (kind)
9375 || IS_GIMPLE_STMT_CODE_CLASS (kind))
9376 && TREE_CODE_LENGTH (code) == 2
9378 && op1 != NULL_TREE);
9383 /* Strip any conversions that don't change the mode. This is
9384 safe for every expression, except for a comparison expression
9385 because its signedness is derived from its operands. So, in
9386 the latter case, only strip conversions that don't change the
9389 Note that this is done as an internal manipulation within the
9390 constant folder, in order to find the simplest representation
9391 of the arguments so that their form can be studied. In any
9392 cases, the appropriate type conversions should be put back in
9393 the tree that will get out of the constant folder. */
9395 if (kind == tcc_comparison)
9397 STRIP_SIGN_NOPS (arg0);
9398 STRIP_SIGN_NOPS (arg1);
9406 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9407 constant but we can't do arithmetic on them. */
9408 if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9409 || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
9410 || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
9411 || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
9412 || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
9413 || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
9415 if (kind == tcc_binary)
9417 /* Make sure type and arg0 have the same saturating flag. */
9418 gcc_assert (TYPE_SATURATING (type)
9419 == TYPE_SATURATING (TREE_TYPE (arg0)));
9420 tem = const_binop (code, arg0, arg1, 0);
9422 else if (kind == tcc_comparison)
9423 tem = fold_relational_const (code, type, arg0, arg1);
9427 if (tem != NULL_TREE)
9429 if (TREE_TYPE (tem) != type)
9430 tem = fold_convert (type, tem);
9435 /* If this is a commutative operation, and ARG0 is a constant, move it
9436 to ARG1 to reduce the number of tests below. */
9437 if (commutative_tree_code (code)
9438 && tree_swap_operands_p (arg0, arg1, true))
9439 return fold_build2 (code, type, op1, op0);
9441 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9443 First check for cases where an arithmetic operation is applied to a
9444 compound, conditional, or comparison operation. Push the arithmetic
9445 operation inside the compound or conditional to see if any folding
9446 can then be done. Convert comparison to conditional for this purpose.
9447 The also optimizes non-constant cases that used to be done in
9450 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9451 one of the operands is a comparison and the other is a comparison, a
9452 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9453 code below would make the expression more complex. Change it to a
9454 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9455 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9457 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9458 || code == EQ_EXPR || code == NE_EXPR)
9459 && ((truth_value_p (TREE_CODE (arg0))
9460 && (truth_value_p (TREE_CODE (arg1))
9461 || (TREE_CODE (arg1) == BIT_AND_EXPR
9462 && integer_onep (TREE_OPERAND (arg1, 1)))))
9463 || (truth_value_p (TREE_CODE (arg1))
9464 && (truth_value_p (TREE_CODE (arg0))
9465 || (TREE_CODE (arg0) == BIT_AND_EXPR
9466 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9468 tem = fold_build2 (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9469 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9472 fold_convert (boolean_type_node, arg0),
9473 fold_convert (boolean_type_node, arg1));
9475 if (code == EQ_EXPR)
9476 tem = invert_truthvalue (tem);
9478 return fold_convert (type, tem);
9481 if (TREE_CODE_CLASS (code) == tcc_binary
9482 || TREE_CODE_CLASS (code) == tcc_comparison)
9484 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9485 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9486 fold_build2 (code, type,
9487 TREE_OPERAND (arg0, 1), op1));
9488 if (TREE_CODE (arg1) == COMPOUND_EXPR
9489 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9490 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9491 fold_build2 (code, type,
9492 op0, TREE_OPERAND (arg1, 1)));
9494 if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
9496 tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
9498 /*cond_first_p=*/1);
9499 if (tem != NULL_TREE)
9503 if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
9505 tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
9507 /*cond_first_p=*/0);
9508 if (tem != NULL_TREE)
9515 case POINTER_PLUS_EXPR:
9516 /* 0 +p index -> (type)index */
9517 if (integer_zerop (arg0))
9518 return non_lvalue (fold_convert (type, arg1));
9520 /* PTR +p 0 -> PTR */
9521 if (integer_zerop (arg1))
9522 return non_lvalue (fold_convert (type, arg0));
9524 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9525 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9526 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9527 return fold_convert (type, fold_build2 (PLUS_EXPR, sizetype,
9528 fold_convert (sizetype, arg1),
9529 fold_convert (sizetype, arg0)));
9531 /* (PTR +p B) +p A -> PTR +p (B + A) */
9532 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9535 tree arg01 = fold_convert (sizetype, TREE_OPERAND (arg0, 1));
9536 tree arg00 = TREE_OPERAND (arg0, 0);
9537 inner = fold_build2 (PLUS_EXPR, sizetype,
9538 arg01, fold_convert (sizetype, arg1));
9539 return fold_convert (type,
9540 fold_build2 (POINTER_PLUS_EXPR,
9541 TREE_TYPE (arg00), arg00, inner));
9544 /* PTR_CST +p CST -> CST1 */
9545 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9546 return fold_build2 (PLUS_EXPR, type, arg0, fold_convert (type, arg1));
9548 /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
9549 of the array. Loop optimizer sometimes produce this type of
9551 if (TREE_CODE (arg0) == ADDR_EXPR)
9553 tem = try_move_mult_to_index (arg0, fold_convert (sizetype, arg1));
9555 return fold_convert (type, tem);
9561 /* PTR + INT -> (INT)(PTR p+ INT) */
9562 if (POINTER_TYPE_P (TREE_TYPE (arg0))
9563 && INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
9564 return fold_convert (type, fold_build2 (POINTER_PLUS_EXPR,
9567 fold_convert (sizetype, arg1)));
9568 /* INT + PTR -> (INT)(PTR p+ INT) */
9569 if (POINTER_TYPE_P (TREE_TYPE (arg1))
9570 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9571 return fold_convert (type, fold_build2 (POINTER_PLUS_EXPR,
9574 fold_convert (sizetype, arg0)));
9575 /* A + (-B) -> A - B */
9576 if (TREE_CODE (arg1) == NEGATE_EXPR)
9577 return fold_build2 (MINUS_EXPR, type,
9578 fold_convert (type, arg0),
9579 fold_convert (type, TREE_OPERAND (arg1, 0)));
9580 /* (-A) + B -> B - A */
9581 if (TREE_CODE (arg0) == NEGATE_EXPR
9582 && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
9583 return fold_build2 (MINUS_EXPR, type,
9584 fold_convert (type, arg1),
9585 fold_convert (type, TREE_OPERAND (arg0, 0)));
9587 if (INTEGRAL_TYPE_P (type))
9589 /* Convert ~A + 1 to -A. */
9590 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9591 && integer_onep (arg1))
9592 return fold_build1 (NEGATE_EXPR, type, TREE_OPERAND (arg0, 0));
9595 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9596 && !TYPE_OVERFLOW_TRAPS (type))
9598 tree tem = TREE_OPERAND (arg0, 0);
9601 if (operand_equal_p (tem, arg1, 0))
9603 t1 = build_int_cst_type (type, -1);
9604 return omit_one_operand (type, t1, arg1);
9609 if (TREE_CODE (arg1) == BIT_NOT_EXPR
9610 && !TYPE_OVERFLOW_TRAPS (type))
9612 tree tem = TREE_OPERAND (arg1, 0);
9615 if (operand_equal_p (arg0, tem, 0))
9617 t1 = build_int_cst_type (type, -1);
9618 return omit_one_operand (type, t1, arg0);
9623 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
9624 same or one. Make sure type is not saturating. */
9625 if ((TREE_CODE (arg0) == MULT_EXPR
9626 || TREE_CODE (arg1) == MULT_EXPR)
9627 && !TYPE_SATURATING (type)
9628 && (!FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations))
9630 tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
9635 if (! FLOAT_TYPE_P (type))
9637 if (integer_zerop (arg1))
9638 return non_lvalue (fold_convert (type, arg0));
9640 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
9641 with a constant, and the two constants have no bits in common,
9642 we should treat this as a BIT_IOR_EXPR since this may produce more
9644 if (TREE_CODE (arg0) == BIT_AND_EXPR
9645 && TREE_CODE (arg1) == BIT_AND_EXPR
9646 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9647 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9648 && integer_zerop (const_binop (BIT_AND_EXPR,
9649 TREE_OPERAND (arg0, 1),
9650 TREE_OPERAND (arg1, 1), 0)))
9652 code = BIT_IOR_EXPR;
9656 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9657 (plus (plus (mult) (mult)) (foo)) so that we can
9658 take advantage of the factoring cases below. */
9659 if (((TREE_CODE (arg0) == PLUS_EXPR
9660 || TREE_CODE (arg0) == MINUS_EXPR)
9661 && TREE_CODE (arg1) == MULT_EXPR)
9662 || ((TREE_CODE (arg1) == PLUS_EXPR
9663 || TREE_CODE (arg1) == MINUS_EXPR)
9664 && TREE_CODE (arg0) == MULT_EXPR))
9666 tree parg0, parg1, parg, marg;
9667 enum tree_code pcode;
9669 if (TREE_CODE (arg1) == MULT_EXPR)
9670 parg = arg0, marg = arg1;
9672 parg = arg1, marg = arg0;
9673 pcode = TREE_CODE (parg);
9674 parg0 = TREE_OPERAND (parg, 0);
9675 parg1 = TREE_OPERAND (parg, 1);
9679 if (TREE_CODE (parg0) == MULT_EXPR
9680 && TREE_CODE (parg1) != MULT_EXPR)
9681 return fold_build2 (pcode, type,
9682 fold_build2 (PLUS_EXPR, type,
9683 fold_convert (type, parg0),
9684 fold_convert (type, marg)),
9685 fold_convert (type, parg1));
9686 if (TREE_CODE (parg0) != MULT_EXPR
9687 && TREE_CODE (parg1) == MULT_EXPR)
9688 return fold_build2 (PLUS_EXPR, type,
9689 fold_convert (type, parg0),
9690 fold_build2 (pcode, type,
9691 fold_convert (type, marg),
9698 /* See if ARG1 is zero and X + ARG1 reduces to X. */
9699 if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
9700 return non_lvalue (fold_convert (type, arg0));
9702 /* Likewise if the operands are reversed. */
9703 if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
9704 return non_lvalue (fold_convert (type, arg1));
9706 /* Convert X + -C into X - C. */
9707 if (TREE_CODE (arg1) == REAL_CST
9708 && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
9710 tem = fold_negate_const (arg1, type);
9711 if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
9712 return fold_build2 (MINUS_EXPR, type,
9713 fold_convert (type, arg0),
9714 fold_convert (type, tem));
9717 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9718 to __complex__ ( x, y ). This is not the same for SNaNs or
9719 if signed zeros are involved. */
9720 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9721 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
9722 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9724 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9725 tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
9726 tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
9727 bool arg0rz = false, arg0iz = false;
9728 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9729 || (arg0i && (arg0iz = real_zerop (arg0i))))
9731 tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
9732 tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
9733 if (arg0rz && arg1i && real_zerop (arg1i))
9735 tree rp = arg1r ? arg1r
9736 : build1 (REALPART_EXPR, rtype, arg1);
9737 tree ip = arg0i ? arg0i
9738 : build1 (IMAGPART_EXPR, rtype, arg0);
9739 return fold_build2 (COMPLEX_EXPR, type, rp, ip);
9741 else if (arg0iz && arg1r && real_zerop (arg1r))
9743 tree rp = arg0r ? arg0r
9744 : build1 (REALPART_EXPR, rtype, arg0);
9745 tree ip = arg1i ? arg1i
9746 : build1 (IMAGPART_EXPR, rtype, arg1);
9747 return fold_build2 (COMPLEX_EXPR, type, rp, ip);
9752 if (flag_unsafe_math_optimizations
9753 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9754 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9755 && (tem = distribute_real_division (code, type, arg0, arg1)))
9758 /* Convert x+x into x*2.0. */
9759 if (operand_equal_p (arg0, arg1, 0)
9760 && SCALAR_FLOAT_TYPE_P (type))
9761 return fold_build2 (MULT_EXPR, type, arg0,
9762 build_real (type, dconst2));
9764 /* Convert a + (b*c + d*e) into (a + b*c) + d*e. */
9765 if (flag_unsafe_math_optimizations
9766 && TREE_CODE (arg1) == PLUS_EXPR
9767 && TREE_CODE (arg0) != MULT_EXPR)
9769 tree tree10 = TREE_OPERAND (arg1, 0);
9770 tree tree11 = TREE_OPERAND (arg1, 1);
9771 if (TREE_CODE (tree11) == MULT_EXPR
9772 && TREE_CODE (tree10) == MULT_EXPR)
9775 tree0 = fold_build2 (PLUS_EXPR, type, arg0, tree10);
9776 return fold_build2 (PLUS_EXPR, type, tree0, tree11);
9779 /* Convert (b*c + d*e) + a into b*c + (d*e +a). */
9780 if (flag_unsafe_math_optimizations
9781 && TREE_CODE (arg0) == PLUS_EXPR
9782 && TREE_CODE (arg1) != MULT_EXPR)
9784 tree tree00 = TREE_OPERAND (arg0, 0);
9785 tree tree01 = TREE_OPERAND (arg0, 1);
9786 if (TREE_CODE (tree01) == MULT_EXPR
9787 && TREE_CODE (tree00) == MULT_EXPR)
9790 tree0 = fold_build2 (PLUS_EXPR, type, tree01, arg1);
9791 return fold_build2 (PLUS_EXPR, type, tree00, tree0);
9797 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9798 is a rotate of A by C1 bits. */
9799 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9800 is a rotate of A by B bits. */
9802 enum tree_code code0, code1;
9803 code0 = TREE_CODE (arg0);
9804 code1 = TREE_CODE (arg1);
9805 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9806 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9807 && operand_equal_p (TREE_OPERAND (arg0, 0),
9808 TREE_OPERAND (arg1, 0), 0)
9809 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
9811 tree tree01, tree11;
9812 enum tree_code code01, code11;
9814 tree01 = TREE_OPERAND (arg0, 1);
9815 tree11 = TREE_OPERAND (arg1, 1);
9816 STRIP_NOPS (tree01);
9817 STRIP_NOPS (tree11);
9818 code01 = TREE_CODE (tree01);
9819 code11 = TREE_CODE (tree11);
9820 if (code01 == INTEGER_CST
9821 && code11 == INTEGER_CST
9822 && TREE_INT_CST_HIGH (tree01) == 0
9823 && TREE_INT_CST_HIGH (tree11) == 0
9824 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
9825 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
9826 return build2 (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
9827 code0 == LSHIFT_EXPR ? tree01 : tree11);
9828 else if (code11 == MINUS_EXPR)
9830 tree tree110, tree111;
9831 tree110 = TREE_OPERAND (tree11, 0);
9832 tree111 = TREE_OPERAND (tree11, 1);
9833 STRIP_NOPS (tree110);
9834 STRIP_NOPS (tree111);
9835 if (TREE_CODE (tree110) == INTEGER_CST
9836 && 0 == compare_tree_int (tree110,
9838 (TREE_TYPE (TREE_OPERAND
9840 && operand_equal_p (tree01, tree111, 0))
9841 return build2 ((code0 == LSHIFT_EXPR
9844 type, TREE_OPERAND (arg0, 0), tree01);
9846 else if (code01 == MINUS_EXPR)
9848 tree tree010, tree011;
9849 tree010 = TREE_OPERAND (tree01, 0);
9850 tree011 = TREE_OPERAND (tree01, 1);
9851 STRIP_NOPS (tree010);
9852 STRIP_NOPS (tree011);
9853 if (TREE_CODE (tree010) == INTEGER_CST
9854 && 0 == compare_tree_int (tree010,
9856 (TREE_TYPE (TREE_OPERAND
9858 && operand_equal_p (tree11, tree011, 0))
9859 return build2 ((code0 != LSHIFT_EXPR
9862 type, TREE_OPERAND (arg0, 0), tree11);
9868 /* In most languages, can't associate operations on floats through
9869 parentheses. Rather than remember where the parentheses were, we
9870 don't associate floats at all, unless the user has specified
9871 -funsafe-math-optimizations.
9872 And, we need to make sure type is not saturating. */
9874 if ((! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
9875 && !TYPE_SATURATING (type))
9877 tree var0, con0, lit0, minus_lit0;
9878 tree var1, con1, lit1, minus_lit1;
9881 /* Split both trees into variables, constants, and literals. Then
9882 associate each group together, the constants with literals,
9883 then the result with variables. This increases the chances of
9884 literals being recombined later and of generating relocatable
9885 expressions for the sum of a constant and literal. */
9886 var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
9887 var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
9888 code == MINUS_EXPR);
9890 /* With undefined overflow we can only associate constants
9891 with one variable. */
9892 if ((POINTER_TYPE_P (type)
9893 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
9899 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9900 tmp0 = TREE_OPERAND (tmp0, 0);
9901 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9902 tmp1 = TREE_OPERAND (tmp1, 0);
9903 /* The only case we can still associate with two variables
9904 is if they are the same, modulo negation. */
9905 if (!operand_equal_p (tmp0, tmp1, 0))
9909 /* Only do something if we found more than two objects. Otherwise,
9910 nothing has changed and we risk infinite recursion. */
9912 && (2 < ((var0 != 0) + (var1 != 0)
9913 + (con0 != 0) + (con1 != 0)
9914 + (lit0 != 0) + (lit1 != 0)
9915 + (minus_lit0 != 0) + (minus_lit1 != 0))))
9917 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9918 if (code == MINUS_EXPR)
9921 var0 = associate_trees (var0, var1, code, type);
9922 con0 = associate_trees (con0, con1, code, type);
9923 lit0 = associate_trees (lit0, lit1, code, type);
9924 minus_lit0 = associate_trees (minus_lit0, minus_lit1, code, type);
9926 /* Preserve the MINUS_EXPR if the negative part of the literal is
9927 greater than the positive part. Otherwise, the multiplicative
9928 folding code (i.e extract_muldiv) may be fooled in case
9929 unsigned constants are subtracted, like in the following
9930 example: ((X*2 + 4) - 8U)/2. */
9931 if (minus_lit0 && lit0)
9933 if (TREE_CODE (lit0) == INTEGER_CST
9934 && TREE_CODE (minus_lit0) == INTEGER_CST
9935 && tree_int_cst_lt (lit0, minus_lit0))
9937 minus_lit0 = associate_trees (minus_lit0, lit0,
9943 lit0 = associate_trees (lit0, minus_lit0,
9951 return fold_convert (type,
9952 associate_trees (var0, minus_lit0,
9956 con0 = associate_trees (con0, minus_lit0,
9958 return fold_convert (type,
9959 associate_trees (var0, con0,
9964 con0 = associate_trees (con0, lit0, code, type);
9965 return fold_convert (type, associate_trees (var0, con0,
9973 /* Pointer simplifications for subtraction, simple reassociations. */
9974 if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
9976 /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
9977 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
9978 && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
9980 tree arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
9981 tree arg01 = fold_convert (type, TREE_OPERAND (arg0, 1));
9982 tree arg10 = fold_convert (type, TREE_OPERAND (arg1, 0));
9983 tree arg11 = fold_convert (type, TREE_OPERAND (arg1, 1));
9984 return fold_build2 (PLUS_EXPR, type,
9985 fold_build2 (MINUS_EXPR, type, arg00, arg10),
9986 fold_build2 (MINUS_EXPR, type, arg01, arg11));
9988 /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
9989 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9991 tree arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
9992 tree arg01 = fold_convert (type, TREE_OPERAND (arg0, 1));
9993 tree tmp = fold_binary (MINUS_EXPR, type, arg00, fold_convert (type, arg1));
9995 return fold_build2 (PLUS_EXPR, type, tmp, arg01);
9998 /* A - (-B) -> A + B */
9999 if (TREE_CODE (arg1) == NEGATE_EXPR)
10000 return fold_build2 (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0));
10001 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
10002 if (TREE_CODE (arg0) == NEGATE_EXPR
10003 && (FLOAT_TYPE_P (type)
10004 || INTEGRAL_TYPE_P (type))
10005 && negate_expr_p (arg1)
10006 && reorder_operands_p (arg0, arg1))
10007 return fold_build2 (MINUS_EXPR, type, negate_expr (arg1),
10008 TREE_OPERAND (arg0, 0));
10009 /* Convert -A - 1 to ~A. */
10010 if (INTEGRAL_TYPE_P (type)
10011 && TREE_CODE (arg0) == NEGATE_EXPR
10012 && integer_onep (arg1)
10013 && !TYPE_OVERFLOW_TRAPS (type))
10014 return fold_build1 (BIT_NOT_EXPR, type,
10015 fold_convert (type, TREE_OPERAND (arg0, 0)));
10017 /* Convert -1 - A to ~A. */
10018 if (INTEGRAL_TYPE_P (type)
10019 && integer_all_onesp (arg0))
10020 return fold_build1 (BIT_NOT_EXPR, type, op1);
10022 if (! FLOAT_TYPE_P (type))
10024 if (integer_zerop (arg0))
10025 return negate_expr (fold_convert (type, arg1));
10026 if (integer_zerop (arg1))
10027 return non_lvalue (fold_convert (type, arg0));
10029 /* Fold A - (A & B) into ~B & A. */
10030 if (!TREE_SIDE_EFFECTS (arg0)
10031 && TREE_CODE (arg1) == BIT_AND_EXPR)
10033 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
10035 tree arg10 = fold_convert (type, TREE_OPERAND (arg1, 0));
10036 return fold_build2 (BIT_AND_EXPR, type,
10037 fold_build1 (BIT_NOT_EXPR, type, arg10),
10038 fold_convert (type, arg0));
10040 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10042 tree arg11 = fold_convert (type, TREE_OPERAND (arg1, 1));
10043 return fold_build2 (BIT_AND_EXPR, type,
10044 fold_build1 (BIT_NOT_EXPR, type, arg11),
10045 fold_convert (type, arg0));
10049 /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10050 any power of 2 minus 1. */
10051 if (TREE_CODE (arg0) == BIT_AND_EXPR
10052 && TREE_CODE (arg1) == BIT_AND_EXPR
10053 && operand_equal_p (TREE_OPERAND (arg0, 0),
10054 TREE_OPERAND (arg1, 0), 0))
10056 tree mask0 = TREE_OPERAND (arg0, 1);
10057 tree mask1 = TREE_OPERAND (arg1, 1);
10058 tree tem = fold_build1 (BIT_NOT_EXPR, type, mask0);
10060 if (operand_equal_p (tem, mask1, 0))
10062 tem = fold_build2 (BIT_XOR_EXPR, type,
10063 TREE_OPERAND (arg0, 0), mask1);
10064 return fold_build2 (MINUS_EXPR, type, tem, mask1);
10069 /* See if ARG1 is zero and X - ARG1 reduces to X. */
10070 else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
10071 return non_lvalue (fold_convert (type, arg0));
10073 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
10074 ARG0 is zero and X + ARG0 reduces to X, since that would mean
10075 (-ARG1 + ARG0) reduces to -ARG1. */
10076 else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10077 return negate_expr (fold_convert (type, arg1));
10079 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10080 __complex__ ( x, -y ). This is not the same for SNaNs or if
10081 signed zeros are involved. */
10082 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10083 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10084 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10086 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10087 tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
10088 tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
10089 bool arg0rz = false, arg0iz = false;
10090 if ((arg0r && (arg0rz = real_zerop (arg0r)))
10091 || (arg0i && (arg0iz = real_zerop (arg0i))))
10093 tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
10094 tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
10095 if (arg0rz && arg1i && real_zerop (arg1i))
10097 tree rp = fold_build1 (NEGATE_EXPR, rtype,
10099 : build1 (REALPART_EXPR, rtype, arg1));
10100 tree ip = arg0i ? arg0i
10101 : build1 (IMAGPART_EXPR, rtype, arg0);
10102 return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10104 else if (arg0iz && arg1r && real_zerop (arg1r))
10106 tree rp = arg0r ? arg0r
10107 : build1 (REALPART_EXPR, rtype, arg0);
10108 tree ip = fold_build1 (NEGATE_EXPR, rtype,
10110 : build1 (IMAGPART_EXPR, rtype, arg1));
10111 return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10116 /* Fold &x - &x. This can happen from &x.foo - &x.
10117 This is unsafe for certain floats even in non-IEEE formats.
10118 In IEEE, it is unsafe because it does wrong for NaNs.
10119 Also note that operand_equal_p is always false if an operand
10122 if ((! FLOAT_TYPE_P (type)
10123 || (flag_unsafe_math_optimizations
10124 && !HONOR_NANS (TYPE_MODE (type))
10125 && !HONOR_INFINITIES (TYPE_MODE (type))))
10126 && operand_equal_p (arg0, arg1, 0))
10127 return fold_convert (type, integer_zero_node);
10129 /* A - B -> A + (-B) if B is easily negatable. */
10130 if (negate_expr_p (arg1)
10131 && ((FLOAT_TYPE_P (type)
10132 /* Avoid this transformation if B is a positive REAL_CST. */
10133 && (TREE_CODE (arg1) != REAL_CST
10134 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10135 || INTEGRAL_TYPE_P (type)))
10136 return fold_build2 (PLUS_EXPR, type,
10137 fold_convert (type, arg0),
10138 fold_convert (type, negate_expr (arg1)));
10140 /* Try folding difference of addresses. */
10142 HOST_WIDE_INT diff;
10144 if ((TREE_CODE (arg0) == ADDR_EXPR
10145 || TREE_CODE (arg1) == ADDR_EXPR)
10146 && ptr_difference_const (arg0, arg1, &diff))
10147 return build_int_cst_type (type, diff);
10150 /* Fold &a[i] - &a[j] to i-j. */
10151 if (TREE_CODE (arg0) == ADDR_EXPR
10152 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10153 && TREE_CODE (arg1) == ADDR_EXPR
10154 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10156 tree aref0 = TREE_OPERAND (arg0, 0);
10157 tree aref1 = TREE_OPERAND (arg1, 0);
10158 if (operand_equal_p (TREE_OPERAND (aref0, 0),
10159 TREE_OPERAND (aref1, 0), 0))
10161 tree op0 = fold_convert (type, TREE_OPERAND (aref0, 1));
10162 tree op1 = fold_convert (type, TREE_OPERAND (aref1, 1));
10163 tree esz = array_ref_element_size (aref0);
10164 tree diff = build2 (MINUS_EXPR, type, op0, op1);
10165 return fold_build2 (MULT_EXPR, type, diff,
10166 fold_convert (type, esz));
10171 if (flag_unsafe_math_optimizations
10172 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10173 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10174 && (tem = distribute_real_division (code, type, arg0, arg1)))
10177 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
10178 same or one. Make sure type is not saturating. */
10179 if ((TREE_CODE (arg0) == MULT_EXPR
10180 || TREE_CODE (arg1) == MULT_EXPR)
10181 && !TYPE_SATURATING (type)
10182 && (!FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations))
10184 tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
10192 /* (-A) * (-B) -> A * B */
10193 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10194 return fold_build2 (MULT_EXPR, type,
10195 fold_convert (type, TREE_OPERAND (arg0, 0)),
10196 fold_convert (type, negate_expr (arg1)));
10197 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10198 return fold_build2 (MULT_EXPR, type,
10199 fold_convert (type, negate_expr (arg0)),
10200 fold_convert (type, TREE_OPERAND (arg1, 0)));
10202 if (! FLOAT_TYPE_P (type))
10204 if (integer_zerop (arg1))
10205 return omit_one_operand (type, arg1, arg0);
10206 if (integer_onep (arg1))
10207 return non_lvalue (fold_convert (type, arg0));
10208 /* Transform x * -1 into -x. */
10209 if (integer_all_onesp (arg1))
10210 return fold_convert (type, negate_expr (arg0));
10211 /* Transform x * -C into -x * C if x is easily negatable. */
10212 if (TREE_CODE (arg1) == INTEGER_CST
10213 && tree_int_cst_sgn (arg1) == -1
10214 && negate_expr_p (arg0)
10215 && (tem = negate_expr (arg1)) != arg1
10216 && !TREE_OVERFLOW (tem))
10217 return fold_build2 (MULT_EXPR, type,
10218 negate_expr (arg0), tem);
10220 /* (a * (1 << b)) is (a << b) */
10221 if (TREE_CODE (arg1) == LSHIFT_EXPR
10222 && integer_onep (TREE_OPERAND (arg1, 0)))
10223 return fold_build2 (LSHIFT_EXPR, type, arg0,
10224 TREE_OPERAND (arg1, 1));
10225 if (TREE_CODE (arg0) == LSHIFT_EXPR
10226 && integer_onep (TREE_OPERAND (arg0, 0)))
10227 return fold_build2 (LSHIFT_EXPR, type, arg1,
10228 TREE_OPERAND (arg0, 1));
10230 strict_overflow_p = false;
10231 if (TREE_CODE (arg1) == INTEGER_CST
10232 && 0 != (tem = extract_muldiv (op0,
10233 fold_convert (type, arg1),
10235 &strict_overflow_p)))
10237 if (strict_overflow_p)
10238 fold_overflow_warning (("assuming signed overflow does not "
10239 "occur when simplifying "
10241 WARN_STRICT_OVERFLOW_MISC);
10242 return fold_convert (type, tem);
10245 /* Optimize z * conj(z) for integer complex numbers. */
10246 if (TREE_CODE (arg0) == CONJ_EXPR
10247 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10248 return fold_mult_zconjz (type, arg1);
10249 if (TREE_CODE (arg1) == CONJ_EXPR
10250 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10251 return fold_mult_zconjz (type, arg0);
10255 /* Maybe fold x * 0 to 0. The expressions aren't the same
10256 when x is NaN, since x * 0 is also NaN. Nor are they the
10257 same in modes with signed zeros, since multiplying a
10258 negative value by 0 gives -0, not +0. */
10259 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10260 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10261 && real_zerop (arg1))
10262 return omit_one_operand (type, arg1, arg0);
10263 /* In IEEE floating point, x*1 is not equivalent to x for snans. */
10264 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10265 && real_onep (arg1))
10266 return non_lvalue (fold_convert (type, arg0));
10268 /* Transform x * -1.0 into -x. */
10269 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10270 && real_minus_onep (arg1))
10271 return fold_convert (type, negate_expr (arg0));
10273 /* Convert (C1/X)*C2 into (C1*C2)/X. */
10274 if (flag_unsafe_math_optimizations
10275 && TREE_CODE (arg0) == RDIV_EXPR
10276 && TREE_CODE (arg1) == REAL_CST
10277 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
10279 tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
10282 return fold_build2 (RDIV_EXPR, type, tem,
10283 TREE_OPERAND (arg0, 1));
10286 /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y. */
10287 if (operand_equal_p (arg0, arg1, 0))
10289 tree tem = fold_strip_sign_ops (arg0);
10290 if (tem != NULL_TREE)
10292 tem = fold_convert (type, tem);
10293 return fold_build2 (MULT_EXPR, type, tem, tem);
10297 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10298 This is not the same for NaNs or if signed zeros are
10300 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10301 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10302 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10303 && TREE_CODE (arg1) == COMPLEX_CST
10304 && real_zerop (TREE_REALPART (arg1)))
10306 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10307 if (real_onep (TREE_IMAGPART (arg1)))
10308 return fold_build2 (COMPLEX_EXPR, type,
10309 negate_expr (fold_build1 (IMAGPART_EXPR,
10311 fold_build1 (REALPART_EXPR, rtype, arg0));
10312 else if (real_minus_onep (TREE_IMAGPART (arg1)))
10313 return fold_build2 (COMPLEX_EXPR, type,
10314 fold_build1 (IMAGPART_EXPR, rtype, arg0),
10315 negate_expr (fold_build1 (REALPART_EXPR,
10319 /* Optimize z * conj(z) for floating point complex numbers.
10320 Guarded by flag_unsafe_math_optimizations as non-finite
10321 imaginary components don't produce scalar results. */
10322 if (flag_unsafe_math_optimizations
10323 && TREE_CODE (arg0) == CONJ_EXPR
10324 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10325 return fold_mult_zconjz (type, arg1);
10326 if (flag_unsafe_math_optimizations
10327 && TREE_CODE (arg1) == CONJ_EXPR
10328 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10329 return fold_mult_zconjz (type, arg0);
10331 if (flag_unsafe_math_optimizations)
10333 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10334 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10336 /* Optimizations of root(...)*root(...). */
10337 if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
10340 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10341 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10343 /* Optimize sqrt(x)*sqrt(x) as x. */
10344 if (BUILTIN_SQRT_P (fcode0)
10345 && operand_equal_p (arg00, arg10, 0)
10346 && ! HONOR_SNANS (TYPE_MODE (type)))
10349 /* Optimize root(x)*root(y) as root(x*y). */
10350 rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10351 arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
10352 return build_call_expr (rootfn, 1, arg);
10355 /* Optimize expN(x)*expN(y) as expN(x+y). */
10356 if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
10358 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10359 tree arg = fold_build2 (PLUS_EXPR, type,
10360 CALL_EXPR_ARG (arg0, 0),
10361 CALL_EXPR_ARG (arg1, 0));
10362 return build_call_expr (expfn, 1, arg);
10365 /* Optimizations of pow(...)*pow(...). */
10366 if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
10367 || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
10368 || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
10370 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10371 tree arg01 = CALL_EXPR_ARG (arg0, 1);
10372 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10373 tree arg11 = CALL_EXPR_ARG (arg1, 1);
10375 /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y). */
10376 if (operand_equal_p (arg01, arg11, 0))
10378 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10379 tree arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
10380 return build_call_expr (powfn, 2, arg, arg01);
10383 /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z). */
10384 if (operand_equal_p (arg00, arg10, 0))
10386 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10387 tree arg = fold_build2 (PLUS_EXPR, type, arg01, arg11);
10388 return build_call_expr (powfn, 2, arg00, arg);
10392 /* Optimize tan(x)*cos(x) as sin(x). */
10393 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
10394 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
10395 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
10396 || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
10397 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
10398 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
10399 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10400 CALL_EXPR_ARG (arg1, 0), 0))
10402 tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
10404 if (sinfn != NULL_TREE)
10405 return build_call_expr (sinfn, 1, CALL_EXPR_ARG (arg0, 0));
10408 /* Optimize x*pow(x,c) as pow(x,c+1). */
10409 if (fcode1 == BUILT_IN_POW
10410 || fcode1 == BUILT_IN_POWF
10411 || fcode1 == BUILT_IN_POWL)
10413 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10414 tree arg11 = CALL_EXPR_ARG (arg1, 1);
10415 if (TREE_CODE (arg11) == REAL_CST
10416 && !TREE_OVERFLOW (arg11)
10417 && operand_equal_p (arg0, arg10, 0))
10419 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10423 c = TREE_REAL_CST (arg11);
10424 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10425 arg = build_real (type, c);
10426 return build_call_expr (powfn, 2, arg0, arg);
10430 /* Optimize pow(x,c)*x as pow(x,c+1). */
10431 if (fcode0 == BUILT_IN_POW
10432 || fcode0 == BUILT_IN_POWF
10433 || fcode0 == BUILT_IN_POWL)
10435 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10436 tree arg01 = CALL_EXPR_ARG (arg0, 1);
10437 if (TREE_CODE (arg01) == REAL_CST
10438 && !TREE_OVERFLOW (arg01)
10439 && operand_equal_p (arg1, arg00, 0))
10441 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10445 c = TREE_REAL_CST (arg01);
10446 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10447 arg = build_real (type, c);
10448 return build_call_expr (powfn, 2, arg1, arg);
10452 /* Optimize x*x as pow(x,2.0), which is expanded as x*x. */
10453 if (! optimize_size
10454 && operand_equal_p (arg0, arg1, 0))
10456 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
10460 tree arg = build_real (type, dconst2);
10461 return build_call_expr (powfn, 2, arg0, arg);
10470 if (integer_all_onesp (arg1))
10471 return omit_one_operand (type, arg1, arg0);
10472 if (integer_zerop (arg1))
10473 return non_lvalue (fold_convert (type, arg0));
10474 if (operand_equal_p (arg0, arg1, 0))
10475 return non_lvalue (fold_convert (type, arg0));
10477 /* ~X | X is -1. */
10478 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10479 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10481 t1 = build_int_cst_type (type, -1);
10482 return omit_one_operand (type, t1, arg1);
10485 /* X | ~X is -1. */
10486 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10487 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10489 t1 = build_int_cst_type (type, -1);
10490 return omit_one_operand (type, t1, arg0);
10493 /* Canonicalize (X & C1) | C2. */
10494 if (TREE_CODE (arg0) == BIT_AND_EXPR
10495 && TREE_CODE (arg1) == INTEGER_CST
10496 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10498 unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, mlo, mhi;
10499 int width = TYPE_PRECISION (type);
10500 hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
10501 lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
10502 hi2 = TREE_INT_CST_HIGH (arg1);
10503 lo2 = TREE_INT_CST_LOW (arg1);
10505 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
10506 if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
10507 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10509 if (width > HOST_BITS_PER_WIDE_INT)
10511 mhi = (unsigned HOST_WIDE_INT) -1
10512 >> (2 * HOST_BITS_PER_WIDE_INT - width);
10518 mlo = (unsigned HOST_WIDE_INT) -1
10519 >> (HOST_BITS_PER_WIDE_INT - width);
10522 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
10523 if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
10524 return fold_build2 (BIT_IOR_EXPR, type,
10525 TREE_OPERAND (arg0, 0), arg1);
10527 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2. */
10530 if ((hi1 & ~hi2) != hi1 || (lo1 & ~lo2) != lo1)
10531 return fold_build2 (BIT_IOR_EXPR, type,
10532 fold_build2 (BIT_AND_EXPR, type,
10533 TREE_OPERAND (arg0, 0),
10534 build_int_cst_wide (type,
10540 /* (X & Y) | Y is (X, Y). */
10541 if (TREE_CODE (arg0) == BIT_AND_EXPR
10542 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10543 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10544 /* (X & Y) | X is (Y, X). */
10545 if (TREE_CODE (arg0) == BIT_AND_EXPR
10546 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10547 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10548 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
10549 /* X | (X & Y) is (Y, X). */
10550 if (TREE_CODE (arg1) == BIT_AND_EXPR
10551 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10552 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10553 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
10554 /* X | (Y & X) is (Y, X). */
10555 if (TREE_CODE (arg1) == BIT_AND_EXPR
10556 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10557 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10558 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
10560 t1 = distribute_bit_expr (code, type, arg0, arg1);
10561 if (t1 != NULL_TREE)
10564 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
10566 This results in more efficient code for machines without a NAND
10567 instruction. Combine will canonicalize to the first form
10568 which will allow use of NAND instructions provided by the
10569 backend if they exist. */
10570 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10571 && TREE_CODE (arg1) == BIT_NOT_EXPR)
10573 return fold_build1 (BIT_NOT_EXPR, type,
10574 build2 (BIT_AND_EXPR, type,
10575 TREE_OPERAND (arg0, 0),
10576 TREE_OPERAND (arg1, 0)));
10579 /* See if this can be simplified into a rotate first. If that
10580 is unsuccessful continue in the association code. */
10584 if (integer_zerop (arg1))
10585 return non_lvalue (fold_convert (type, arg0));
10586 if (integer_all_onesp (arg1))
10587 return fold_build1 (BIT_NOT_EXPR, type, op0);
10588 if (operand_equal_p (arg0, arg1, 0))
10589 return omit_one_operand (type, integer_zero_node, arg0);
10591 /* ~X ^ X is -1. */
10592 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10593 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10595 t1 = build_int_cst_type (type, -1);
10596 return omit_one_operand (type, t1, arg1);
10599 /* X ^ ~X is -1. */
10600 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10601 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10603 t1 = build_int_cst_type (type, -1);
10604 return omit_one_operand (type, t1, arg0);
10607 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
10608 with a constant, and the two constants have no bits in common,
10609 we should treat this as a BIT_IOR_EXPR since this may produce more
10610 simplifications. */
10611 if (TREE_CODE (arg0) == BIT_AND_EXPR
10612 && TREE_CODE (arg1) == BIT_AND_EXPR
10613 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10614 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10615 && integer_zerop (const_binop (BIT_AND_EXPR,
10616 TREE_OPERAND (arg0, 1),
10617 TREE_OPERAND (arg1, 1), 0)))
10619 code = BIT_IOR_EXPR;
10623 /* (X | Y) ^ X -> Y & ~ X*/
10624 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10625 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10627 tree t2 = TREE_OPERAND (arg0, 1);
10628 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
10630 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10631 fold_convert (type, t1));
10635 /* (Y | X) ^ X -> Y & ~ X*/
10636 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10637 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10639 tree t2 = TREE_OPERAND (arg0, 0);
10640 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
10642 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10643 fold_convert (type, t1));
10647 /* X ^ (X | Y) -> Y & ~ X*/
10648 if (TREE_CODE (arg1) == BIT_IOR_EXPR
10649 && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
10651 tree t2 = TREE_OPERAND (arg1, 1);
10652 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
10654 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10655 fold_convert (type, t1));
10659 /* X ^ (Y | X) -> Y & ~ X*/
10660 if (TREE_CODE (arg1) == BIT_IOR_EXPR
10661 && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
10663 tree t2 = TREE_OPERAND (arg1, 0);
10664 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
10666 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10667 fold_convert (type, t1));
10671 /* Convert ~X ^ ~Y to X ^ Y. */
10672 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10673 && TREE_CODE (arg1) == BIT_NOT_EXPR)
10674 return fold_build2 (code, type,
10675 fold_convert (type, TREE_OPERAND (arg0, 0)),
10676 fold_convert (type, TREE_OPERAND (arg1, 0)));
10678 /* Convert ~X ^ C to X ^ ~C. */
10679 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10680 && TREE_CODE (arg1) == INTEGER_CST)
10681 return fold_build2 (code, type,
10682 fold_convert (type, TREE_OPERAND (arg0, 0)),
10683 fold_build1 (BIT_NOT_EXPR, type, arg1));
10685 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
10686 if (TREE_CODE (arg0) == BIT_AND_EXPR
10687 && integer_onep (TREE_OPERAND (arg0, 1))
10688 && integer_onep (arg1))
10689 return fold_build2 (EQ_EXPR, type, arg0,
10690 build_int_cst (TREE_TYPE (arg0), 0));
10692 /* Fold (X & Y) ^ Y as ~X & Y. */
10693 if (TREE_CODE (arg0) == BIT_AND_EXPR
10694 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10696 tem = fold_convert (type, TREE_OPERAND (arg0, 0));
10697 return fold_build2 (BIT_AND_EXPR, type,
10698 fold_build1 (BIT_NOT_EXPR, type, tem),
10699 fold_convert (type, arg1));
10701 /* Fold (X & Y) ^ X as ~Y & X. */
10702 if (TREE_CODE (arg0) == BIT_AND_EXPR
10703 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10704 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10706 tem = fold_convert (type, TREE_OPERAND (arg0, 1));
10707 return fold_build2 (BIT_AND_EXPR, type,
10708 fold_build1 (BIT_NOT_EXPR, type, tem),
10709 fold_convert (type, arg1));
10711 /* Fold X ^ (X & Y) as X & ~Y. */
10712 if (TREE_CODE (arg1) == BIT_AND_EXPR
10713 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10715 tem = fold_convert (type, TREE_OPERAND (arg1, 1));
10716 return fold_build2 (BIT_AND_EXPR, type,
10717 fold_convert (type, arg0),
10718 fold_build1 (BIT_NOT_EXPR, type, tem));
10720 /* Fold X ^ (Y & X) as ~Y & X. */
10721 if (TREE_CODE (arg1) == BIT_AND_EXPR
10722 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10723 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10725 tem = fold_convert (type, TREE_OPERAND (arg1, 0));
10726 return fold_build2 (BIT_AND_EXPR, type,
10727 fold_build1 (BIT_NOT_EXPR, type, tem),
10728 fold_convert (type, arg0));
10731 /* See if this can be simplified into a rotate first. If that
10732 is unsuccessful continue in the association code. */
10736 if (integer_all_onesp (arg1))
10737 return non_lvalue (fold_convert (type, arg0));
10738 if (integer_zerop (arg1))
10739 return omit_one_operand (type, arg1, arg0);
10740 if (operand_equal_p (arg0, arg1, 0))
10741 return non_lvalue (fold_convert (type, arg0));
10743 /* ~X & X is always zero. */
10744 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10745 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10746 return omit_one_operand (type, integer_zero_node, arg1);
10748 /* X & ~X is always zero. */
10749 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10750 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10751 return omit_one_operand (type, integer_zero_node, arg0);
10753 /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2). */
10754 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10755 && TREE_CODE (arg1) == INTEGER_CST
10756 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10757 return fold_build2 (BIT_IOR_EXPR, type,
10758 fold_build2 (BIT_AND_EXPR, type,
10759 TREE_OPERAND (arg0, 0), arg1),
10760 fold_build2 (BIT_AND_EXPR, type,
10761 TREE_OPERAND (arg0, 1), arg1));
10763 /* (X | Y) & Y is (X, Y). */
10764 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10765 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10766 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10767 /* (X | Y) & X is (Y, X). */
10768 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10769 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10770 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10771 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
10772 /* X & (X | Y) is (Y, X). */
10773 if (TREE_CODE (arg1) == BIT_IOR_EXPR
10774 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10775 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10776 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
10777 /* X & (Y | X) is (Y, X). */
10778 if (TREE_CODE (arg1) == BIT_IOR_EXPR
10779 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10780 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10781 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
10783 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
10784 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10785 && integer_onep (TREE_OPERAND (arg0, 1))
10786 && integer_onep (arg1))
10788 tem = TREE_OPERAND (arg0, 0);
10789 return fold_build2 (EQ_EXPR, type,
10790 fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
10791 build_int_cst (TREE_TYPE (tem), 1)),
10792 build_int_cst (TREE_TYPE (tem), 0));
10794 /* Fold ~X & 1 as (X & 1) == 0. */
10795 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10796 && integer_onep (arg1))
10798 tem = TREE_OPERAND (arg0, 0);
10799 return fold_build2 (EQ_EXPR, type,
10800 fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
10801 build_int_cst (TREE_TYPE (tem), 1)),
10802 build_int_cst (TREE_TYPE (tem), 0));
10805 /* Fold (X ^ Y) & Y as ~X & Y. */
10806 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10807 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10809 tem = fold_convert (type, TREE_OPERAND (arg0, 0));
10810 return fold_build2 (BIT_AND_EXPR, type,
10811 fold_build1 (BIT_NOT_EXPR, type, tem),
10812 fold_convert (type, arg1));
10814 /* Fold (X ^ Y) & X as ~Y & X. */
10815 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10816 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10817 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10819 tem = fold_convert (type, TREE_OPERAND (arg0, 1));
10820 return fold_build2 (BIT_AND_EXPR, type,
10821 fold_build1 (BIT_NOT_EXPR, type, tem),
10822 fold_convert (type, arg1));
10824 /* Fold X & (X ^ Y) as X & ~Y. */
10825 if (TREE_CODE (arg1) == BIT_XOR_EXPR
10826 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10828 tem = fold_convert (type, TREE_OPERAND (arg1, 1));
10829 return fold_build2 (BIT_AND_EXPR, type,
10830 fold_convert (type, arg0),
10831 fold_build1 (BIT_NOT_EXPR, type, tem));
10833 /* Fold X & (Y ^ X) as ~Y & X. */
10834 if (TREE_CODE (arg1) == BIT_XOR_EXPR
10835 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10836 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10838 tem = fold_convert (type, TREE_OPERAND (arg1, 0));
10839 return fold_build2 (BIT_AND_EXPR, type,
10840 fold_build1 (BIT_NOT_EXPR, type, tem),
10841 fold_convert (type, arg0));
10844 t1 = distribute_bit_expr (code, type, arg0, arg1);
10845 if (t1 != NULL_TREE)
10847 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10848 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10849 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10852 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10854 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
10855 && (~TREE_INT_CST_LOW (arg1)
10856 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
10857 return fold_convert (type, TREE_OPERAND (arg0, 0));
10860 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
10862 This results in more efficient code for machines without a NOR
10863 instruction. Combine will canonicalize to the first form
10864 which will allow use of NOR instructions provided by the
10865 backend if they exist. */
10866 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10867 && TREE_CODE (arg1) == BIT_NOT_EXPR)
10869 return fold_build1 (BIT_NOT_EXPR, type,
10870 build2 (BIT_IOR_EXPR, type,
10871 TREE_OPERAND (arg0, 0),
10872 TREE_OPERAND (arg1, 0)));
10878 /* Don't touch a floating-point divide by zero unless the mode
10879 of the constant can represent infinity. */
10880 if (TREE_CODE (arg1) == REAL_CST
10881 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10882 && real_zerop (arg1))
10885 /* Optimize A / A to 1.0 if we don't care about
10886 NaNs or Infinities. Skip the transformation
10887 for non-real operands. */
10888 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
10889 && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10890 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
10891 && operand_equal_p (arg0, arg1, 0))
10893 tree r = build_real (TREE_TYPE (arg0), dconst1);
10895 return omit_two_operands (type, r, arg0, arg1);
10898 /* The complex version of the above A / A optimization. */
10899 if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10900 && operand_equal_p (arg0, arg1, 0))
10902 tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
10903 if (! HONOR_NANS (TYPE_MODE (elem_type))
10904 && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
10906 tree r = build_real (elem_type, dconst1);
10907 /* omit_two_operands will call fold_convert for us. */
10908 return omit_two_operands (type, r, arg0, arg1);
10912 /* (-A) / (-B) -> A / B */
10913 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10914 return fold_build2 (RDIV_EXPR, type,
10915 TREE_OPERAND (arg0, 0),
10916 negate_expr (arg1));
10917 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10918 return fold_build2 (RDIV_EXPR, type,
10919 negate_expr (arg0),
10920 TREE_OPERAND (arg1, 0));
10922 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
10923 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10924 && real_onep (arg1))
10925 return non_lvalue (fold_convert (type, arg0));
10927 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
10928 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10929 && real_minus_onep (arg1))
10930 return non_lvalue (fold_convert (type, negate_expr (arg0)));
10932 /* If ARG1 is a constant, we can convert this to a multiply by the
10933 reciprocal. This does not have the same rounding properties,
10934 so only do this if -funsafe-math-optimizations. We can actually
10935 always safely do it if ARG1 is a power of two, but it's hard to
10936 tell if it is or not in a portable manner. */
10937 if (TREE_CODE (arg1) == REAL_CST)
10939 if (flag_unsafe_math_optimizations
10940 && 0 != (tem = const_binop (code, build_real (type, dconst1),
10942 return fold_build2 (MULT_EXPR, type, arg0, tem);
10943 /* Find the reciprocal if optimizing and the result is exact. */
10947 r = TREE_REAL_CST (arg1);
10948 if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
10950 tem = build_real (type, r);
10951 return fold_build2 (MULT_EXPR, type,
10952 fold_convert (type, arg0), tem);
10956 /* Convert A/B/C to A/(B*C). */
10957 if (flag_unsafe_math_optimizations
10958 && TREE_CODE (arg0) == RDIV_EXPR)
10959 return fold_build2 (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
10960 fold_build2 (MULT_EXPR, type,
10961 TREE_OPERAND (arg0, 1), arg1));
10963 /* Convert A/(B/C) to (A/B)*C. */
10964 if (flag_unsafe_math_optimizations
10965 && TREE_CODE (arg1) == RDIV_EXPR)
10966 return fold_build2 (MULT_EXPR, type,
10967 fold_build2 (RDIV_EXPR, type, arg0,
10968 TREE_OPERAND (arg1, 0)),
10969 TREE_OPERAND (arg1, 1));
10971 /* Convert C1/(X*C2) into (C1/C2)/X. */
10972 if (flag_unsafe_math_optimizations
10973 && TREE_CODE (arg1) == MULT_EXPR
10974 && TREE_CODE (arg0) == REAL_CST
10975 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
10977 tree tem = const_binop (RDIV_EXPR, arg0,
10978 TREE_OPERAND (arg1, 1), 0);
10980 return fold_build2 (RDIV_EXPR, type, tem,
10981 TREE_OPERAND (arg1, 0));
10984 if (flag_unsafe_math_optimizations)
10986 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10987 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10989 /* Optimize sin(x)/cos(x) as tan(x). */
10990 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
10991 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
10992 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
10993 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10994 CALL_EXPR_ARG (arg1, 0), 0))
10996 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
10998 if (tanfn != NULL_TREE)
10999 return build_call_expr (tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11002 /* Optimize cos(x)/sin(x) as 1.0/tan(x). */
11003 if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11004 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11005 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
11006 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11007 CALL_EXPR_ARG (arg1, 0), 0))
11009 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11011 if (tanfn != NULL_TREE)
11013 tree tmp = build_call_expr (tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11014 return fold_build2 (RDIV_EXPR, type,
11015 build_real (type, dconst1), tmp);
11019 /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11020 NaNs or Infinities. */
11021 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11022 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11023 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11025 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11026 tree arg01 = CALL_EXPR_ARG (arg1, 0);
11028 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11029 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11030 && operand_equal_p (arg00, arg01, 0))
11032 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11034 if (cosfn != NULL_TREE)
11035 return build_call_expr (cosfn, 1, arg00);
11039 /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11040 NaNs or Infinities. */
11041 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11042 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11043 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11045 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11046 tree arg01 = CALL_EXPR_ARG (arg1, 0);
11048 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11049 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11050 && operand_equal_p (arg00, arg01, 0))
11052 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11054 if (cosfn != NULL_TREE)
11056 tree tmp = build_call_expr (cosfn, 1, arg00);
11057 return fold_build2 (RDIV_EXPR, type,
11058 build_real (type, dconst1),
11064 /* Optimize pow(x,c)/x as pow(x,c-1). */
11065 if (fcode0 == BUILT_IN_POW
11066 || fcode0 == BUILT_IN_POWF
11067 || fcode0 == BUILT_IN_POWL)
11069 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11070 tree arg01 = CALL_EXPR_ARG (arg0, 1);
11071 if (TREE_CODE (arg01) == REAL_CST
11072 && !TREE_OVERFLOW (arg01)
11073 && operand_equal_p (arg1, arg00, 0))
11075 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11079 c = TREE_REAL_CST (arg01);
11080 real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
11081 arg = build_real (type, c);
11082 return build_call_expr (powfn, 2, arg1, arg);
11086 /* Optimize a/root(b/c) into a*root(c/b). */
11087 if (BUILTIN_ROOT_P (fcode1))
11089 tree rootarg = CALL_EXPR_ARG (arg1, 0);
11091 if (TREE_CODE (rootarg) == RDIV_EXPR)
11093 tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11094 tree b = TREE_OPERAND (rootarg, 0);
11095 tree c = TREE_OPERAND (rootarg, 1);
11097 tree tmp = fold_build2 (RDIV_EXPR, type, c, b);
11099 tmp = build_call_expr (rootfn, 1, tmp);
11100 return fold_build2 (MULT_EXPR, type, arg0, tmp);
11104 /* Optimize x/expN(y) into x*expN(-y). */
11105 if (BUILTIN_EXPONENT_P (fcode1))
11107 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11108 tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
11109 arg1 = build_call_expr (expfn, 1, fold_convert (type, arg));
11110 return fold_build2 (MULT_EXPR, type, arg0, arg1);
11113 /* Optimize x/pow(y,z) into x*pow(y,-z). */
11114 if (fcode1 == BUILT_IN_POW
11115 || fcode1 == BUILT_IN_POWF
11116 || fcode1 == BUILT_IN_POWL)
11118 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11119 tree arg10 = CALL_EXPR_ARG (arg1, 0);
11120 tree arg11 = CALL_EXPR_ARG (arg1, 1);
11121 tree neg11 = fold_convert (type, negate_expr (arg11));
11122 arg1 = build_call_expr (powfn, 2, arg10, neg11);
11123 return fold_build2 (MULT_EXPR, type, arg0, arg1);
11128 case TRUNC_DIV_EXPR:
11129 case FLOOR_DIV_EXPR:
11130 /* Simplify A / (B << N) where A and B are positive and B is
11131 a power of 2, to A >> (N + log2(B)). */
11132 strict_overflow_p = false;
11133 if (TREE_CODE (arg1) == LSHIFT_EXPR
11134 && (TYPE_UNSIGNED (type)
11135 || tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p)))
11137 tree sval = TREE_OPERAND (arg1, 0);
11138 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11140 tree sh_cnt = TREE_OPERAND (arg1, 1);
11141 unsigned long pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
11143 if (strict_overflow_p)
11144 fold_overflow_warning (("assuming signed overflow does not "
11145 "occur when simplifying A / (B << N)"),
11146 WARN_STRICT_OVERFLOW_MISC);
11148 sh_cnt = fold_build2 (PLUS_EXPR, TREE_TYPE (sh_cnt),
11149 sh_cnt, build_int_cst (NULL_TREE, pow2));
11150 return fold_build2 (RSHIFT_EXPR, type,
11151 fold_convert (type, arg0), sh_cnt);
11156 case ROUND_DIV_EXPR:
11157 case CEIL_DIV_EXPR:
11158 case EXACT_DIV_EXPR:
11159 if (integer_onep (arg1))
11160 return non_lvalue (fold_convert (type, arg0));
11161 if (integer_zerop (arg1))
11163 /* X / -1 is -X. */
11164 if (!TYPE_UNSIGNED (type)
11165 && TREE_CODE (arg1) == INTEGER_CST
11166 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11167 && TREE_INT_CST_HIGH (arg1) == -1)
11168 return fold_convert (type, negate_expr (arg0));
11170 /* Convert -A / -B to A / B when the type is signed and overflow is
11172 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11173 && TREE_CODE (arg0) == NEGATE_EXPR
11174 && negate_expr_p (arg1))
11176 if (INTEGRAL_TYPE_P (type))
11177 fold_overflow_warning (("assuming signed overflow does not occur "
11178 "when distributing negation across "
11180 WARN_STRICT_OVERFLOW_MISC);
11181 return fold_build2 (code, type,
11182 fold_convert (type, TREE_OPERAND (arg0, 0)),
11183 negate_expr (arg1));
11185 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11186 && TREE_CODE (arg1) == NEGATE_EXPR
11187 && negate_expr_p (arg0))
11189 if (INTEGRAL_TYPE_P (type))
11190 fold_overflow_warning (("assuming signed overflow does not occur "
11191 "when distributing negation across "
11193 WARN_STRICT_OVERFLOW_MISC);
11194 return fold_build2 (code, type, negate_expr (arg0),
11195 TREE_OPERAND (arg1, 0));
11198 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11199 operation, EXACT_DIV_EXPR.
11201 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11202 At one time others generated faster code, it's not clear if they do
11203 after the last round to changes to the DIV code in expmed.c. */
11204 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11205 && multiple_of_p (type, arg0, arg1))
11206 return fold_build2 (EXACT_DIV_EXPR, type, arg0, arg1);
11208 strict_overflow_p = false;
11209 if (TREE_CODE (arg1) == INTEGER_CST
11210 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11211 &strict_overflow_p)))
11213 if (strict_overflow_p)
11214 fold_overflow_warning (("assuming signed overflow does not occur "
11215 "when simplifying division"),
11216 WARN_STRICT_OVERFLOW_MISC);
11217 return fold_convert (type, tem);
11222 case CEIL_MOD_EXPR:
11223 case FLOOR_MOD_EXPR:
11224 case ROUND_MOD_EXPR:
11225 case TRUNC_MOD_EXPR:
11226 /* X % 1 is always zero, but be sure to preserve any side
11228 if (integer_onep (arg1))
11229 return omit_one_operand (type, integer_zero_node, arg0);
11231 /* X % 0, return X % 0 unchanged so that we can get the
11232 proper warnings and errors. */
11233 if (integer_zerop (arg1))
11236 /* 0 % X is always zero, but be sure to preserve any side
11237 effects in X. Place this after checking for X == 0. */
11238 if (integer_zerop (arg0))
11239 return omit_one_operand (type, integer_zero_node, arg1);
11241 /* X % -1 is zero. */
11242 if (!TYPE_UNSIGNED (type)
11243 && TREE_CODE (arg1) == INTEGER_CST
11244 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11245 && TREE_INT_CST_HIGH (arg1) == -1)
11246 return omit_one_operand (type, integer_zero_node, arg0);
11248 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
11249 i.e. "X % C" into "X & (C - 1)", if X and C are positive. */
11250 strict_overflow_p = false;
11251 if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
11252 && (TYPE_UNSIGNED (type)
11253 || tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p)))
11256 /* Also optimize A % (C << N) where C is a power of 2,
11257 to A & ((C << N) - 1). */
11258 if (TREE_CODE (arg1) == LSHIFT_EXPR)
11259 c = TREE_OPERAND (arg1, 0);
11261 if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
11263 tree mask = fold_build2 (MINUS_EXPR, TREE_TYPE (arg1), arg1,
11264 build_int_cst (TREE_TYPE (arg1), 1));
11265 if (strict_overflow_p)
11266 fold_overflow_warning (("assuming signed overflow does not "
11267 "occur when simplifying "
11268 "X % (power of two)"),
11269 WARN_STRICT_OVERFLOW_MISC);
11270 return fold_build2 (BIT_AND_EXPR, type,
11271 fold_convert (type, arg0),
11272 fold_convert (type, mask));
11276 /* X % -C is the same as X % C. */
11277 if (code == TRUNC_MOD_EXPR
11278 && !TYPE_UNSIGNED (type)
11279 && TREE_CODE (arg1) == INTEGER_CST
11280 && !TREE_OVERFLOW (arg1)
11281 && TREE_INT_CST_HIGH (arg1) < 0
11282 && !TYPE_OVERFLOW_TRAPS (type)
11283 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
11284 && !sign_bit_p (arg1, arg1))
11285 return fold_build2 (code, type, fold_convert (type, arg0),
11286 fold_convert (type, negate_expr (arg1)));
11288 /* X % -Y is the same as X % Y. */
11289 if (code == TRUNC_MOD_EXPR
11290 && !TYPE_UNSIGNED (type)
11291 && TREE_CODE (arg1) == NEGATE_EXPR
11292 && !TYPE_OVERFLOW_TRAPS (type))
11293 return fold_build2 (code, type, fold_convert (type, arg0),
11294 fold_convert (type, TREE_OPERAND (arg1, 0)));
11296 if (TREE_CODE (arg1) == INTEGER_CST
11297 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11298 &strict_overflow_p)))
11300 if (strict_overflow_p)
11301 fold_overflow_warning (("assuming signed overflow does not occur "
11302 "when simplifying modulos"),
11303 WARN_STRICT_OVERFLOW_MISC);
11304 return fold_convert (type, tem);
11311 if (integer_all_onesp (arg0))
11312 return omit_one_operand (type, arg0, arg1);
11316 /* Optimize -1 >> x for arithmetic right shifts. */
11317 if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type))
11318 return omit_one_operand (type, arg0, arg1);
11319 /* ... fall through ... */
11323 if (integer_zerop (arg1))
11324 return non_lvalue (fold_convert (type, arg0));
11325 if (integer_zerop (arg0))
11326 return omit_one_operand (type, arg0, arg1);
11328 /* Since negative shift count is not well-defined,
11329 don't try to compute it in the compiler. */
11330 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11333 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
11334 if (TREE_CODE (op0) == code && host_integerp (arg1, false)
11335 && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11336 && host_integerp (TREE_OPERAND (arg0, 1), false)
11337 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11339 HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
11340 + TREE_INT_CST_LOW (arg1));
11342 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
11343 being well defined. */
11344 if (low >= TYPE_PRECISION (type))
11346 if (code == LROTATE_EXPR || code == RROTATE_EXPR)
11347 low = low % TYPE_PRECISION (type);
11348 else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
11349 return build_int_cst (type, 0);
11351 low = TYPE_PRECISION (type) - 1;
11354 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11355 build_int_cst (type, low));
11358 /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
11359 into x & ((unsigned)-1 >> c) for unsigned types. */
11360 if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
11361 || (TYPE_UNSIGNED (type)
11362 && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
11363 && host_integerp (arg1, false)
11364 && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11365 && host_integerp (TREE_OPERAND (arg0, 1), false)
11366 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11368 HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
11369 HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
11375 arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
11377 lshift = build_int_cst (type, -1);
11378 lshift = int_const_binop (code, lshift, arg1, 0);
11380 return fold_build2 (BIT_AND_EXPR, type, arg00, lshift);
11384 /* Rewrite an LROTATE_EXPR by a constant into an
11385 RROTATE_EXPR by a new constant. */
11386 if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
11388 tree tem = build_int_cst (TREE_TYPE (arg1),
11389 GET_MODE_BITSIZE (TYPE_MODE (type)));
11390 tem = const_binop (MINUS_EXPR, tem, arg1, 0);
11391 return fold_build2 (RROTATE_EXPR, type, arg0, tem);
11394 /* If we have a rotate of a bit operation with the rotate count and
11395 the second operand of the bit operation both constant,
11396 permute the two operations. */
11397 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11398 && (TREE_CODE (arg0) == BIT_AND_EXPR
11399 || TREE_CODE (arg0) == BIT_IOR_EXPR
11400 || TREE_CODE (arg0) == BIT_XOR_EXPR)
11401 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11402 return fold_build2 (TREE_CODE (arg0), type,
11403 fold_build2 (code, type,
11404 TREE_OPERAND (arg0, 0), arg1),
11405 fold_build2 (code, type,
11406 TREE_OPERAND (arg0, 1), arg1));
11408 /* Two consecutive rotates adding up to the width of the mode can
11410 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11411 && TREE_CODE (arg0) == RROTATE_EXPR
11412 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11413 && TREE_INT_CST_HIGH (arg1) == 0
11414 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
11415 && ((TREE_INT_CST_LOW (arg1)
11416 + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
11417 == (unsigned int) GET_MODE_BITSIZE (TYPE_MODE (type))))
11418 return TREE_OPERAND (arg0, 0);
11423 if (operand_equal_p (arg0, arg1, 0))
11424 return omit_one_operand (type, arg0, arg1);
11425 if (INTEGRAL_TYPE_P (type)
11426 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
11427 return omit_one_operand (type, arg1, arg0);
11428 tem = fold_minmax (MIN_EXPR, type, arg0, arg1);
11434 if (operand_equal_p (arg0, arg1, 0))
11435 return omit_one_operand (type, arg0, arg1);
11436 if (INTEGRAL_TYPE_P (type)
11437 && TYPE_MAX_VALUE (type)
11438 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
11439 return omit_one_operand (type, arg1, arg0);
11440 tem = fold_minmax (MAX_EXPR, type, arg0, arg1);
11445 case TRUTH_ANDIF_EXPR:
11446 /* Note that the operands of this must be ints
11447 and their values must be 0 or 1.
11448 ("true" is a fixed value perhaps depending on the language.) */
11449 /* If first arg is constant zero, return it. */
11450 if (integer_zerop (arg0))
11451 return fold_convert (type, arg0);
11452 case TRUTH_AND_EXPR:
11453 /* If either arg is constant true, drop it. */
11454 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11455 return non_lvalue (fold_convert (type, arg1));
11456 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
11457 /* Preserve sequence points. */
11458 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11459 return non_lvalue (fold_convert (type, arg0));
11460 /* If second arg is constant zero, result is zero, but first arg
11461 must be evaluated. */
11462 if (integer_zerop (arg1))
11463 return omit_one_operand (type, arg1, arg0);
11464 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
11465 case will be handled here. */
11466 if (integer_zerop (arg0))
11467 return omit_one_operand (type, arg0, arg1);
11469 /* !X && X is always false. */
11470 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11471 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11472 return omit_one_operand (type, integer_zero_node, arg1);
11473 /* X && !X is always false. */
11474 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11475 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11476 return omit_one_operand (type, integer_zero_node, arg0);
11478 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
11479 means A >= Y && A != MAX, but in this case we know that
11482 if (!TREE_SIDE_EFFECTS (arg0)
11483 && !TREE_SIDE_EFFECTS (arg1))
11485 tem = fold_to_nonsharp_ineq_using_bound (arg0, arg1);
11486 if (tem && !operand_equal_p (tem, arg0, 0))
11487 return fold_build2 (code, type, tem, arg1);
11489 tem = fold_to_nonsharp_ineq_using_bound (arg1, arg0);
11490 if (tem && !operand_equal_p (tem, arg1, 0))
11491 return fold_build2 (code, type, arg0, tem);
11495 /* We only do these simplifications if we are optimizing. */
11499 /* Check for things like (A || B) && (A || C). We can convert this
11500 to A || (B && C). Note that either operator can be any of the four
11501 truth and/or operations and the transformation will still be
11502 valid. Also note that we only care about order for the
11503 ANDIF and ORIF operators. If B contains side effects, this
11504 might change the truth-value of A. */
11505 if (TREE_CODE (arg0) == TREE_CODE (arg1)
11506 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
11507 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
11508 || TREE_CODE (arg0) == TRUTH_AND_EXPR
11509 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
11510 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
11512 tree a00 = TREE_OPERAND (arg0, 0);
11513 tree a01 = TREE_OPERAND (arg0, 1);
11514 tree a10 = TREE_OPERAND (arg1, 0);
11515 tree a11 = TREE_OPERAND (arg1, 1);
11516 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
11517 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
11518 && (code == TRUTH_AND_EXPR
11519 || code == TRUTH_OR_EXPR));
11521 if (operand_equal_p (a00, a10, 0))
11522 return fold_build2 (TREE_CODE (arg0), type, a00,
11523 fold_build2 (code, type, a01, a11));
11524 else if (commutative && operand_equal_p (a00, a11, 0))
11525 return fold_build2 (TREE_CODE (arg0), type, a00,
11526 fold_build2 (code, type, a01, a10));
11527 else if (commutative && operand_equal_p (a01, a10, 0))
11528 return fold_build2 (TREE_CODE (arg0), type, a01,
11529 fold_build2 (code, type, a00, a11));
11531 /* This case if tricky because we must either have commutative
11532 operators or else A10 must not have side-effects. */
11534 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
11535 && operand_equal_p (a01, a11, 0))
11536 return fold_build2 (TREE_CODE (arg0), type,
11537 fold_build2 (code, type, a00, a10),
11541 /* See if we can build a range comparison. */
11542 if (0 != (tem = fold_range_test (code, type, op0, op1)))
11545 /* Check for the possibility of merging component references. If our
11546 lhs is another similar operation, try to merge its rhs with our
11547 rhs. Then try to merge our lhs and rhs. */
11548 if (TREE_CODE (arg0) == code
11549 && 0 != (tem = fold_truthop (code, type,
11550 TREE_OPERAND (arg0, 1), arg1)))
11551 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
11553 if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
11558 case TRUTH_ORIF_EXPR:
11559 /* Note that the operands of this must be ints
11560 and their values must be 0 or true.
11561 ("true" is a fixed value perhaps depending on the language.) */
11562 /* If first arg is constant true, return it. */
11563 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11564 return fold_convert (type, arg0);
11565 case TRUTH_OR_EXPR:
11566 /* If either arg is constant zero, drop it. */
11567 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
11568 return non_lvalue (fold_convert (type, arg1));
11569 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
11570 /* Preserve sequence points. */
11571 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11572 return non_lvalue (fold_convert (type, arg0));
11573 /* If second arg is constant true, result is true, but we must
11574 evaluate first arg. */
11575 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
11576 return omit_one_operand (type, arg1, arg0);
11577 /* Likewise for first arg, but note this only occurs here for
11579 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11580 return omit_one_operand (type, arg0, arg1);
11582 /* !X || X is always true. */
11583 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11584 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11585 return omit_one_operand (type, integer_one_node, arg1);
11586 /* X || !X is always true. */
11587 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11588 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11589 return omit_one_operand (type, integer_one_node, arg0);
11593 case TRUTH_XOR_EXPR:
11594 /* If the second arg is constant zero, drop it. */
11595 if (integer_zerop (arg1))
11596 return non_lvalue (fold_convert (type, arg0));
11597 /* If the second arg is constant true, this is a logical inversion. */
11598 if (integer_onep (arg1))
11600 /* Only call invert_truthvalue if operand is a truth value. */
11601 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
11602 tem = fold_build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
11604 tem = invert_truthvalue (arg0);
11605 return non_lvalue (fold_convert (type, tem));
11607 /* Identical arguments cancel to zero. */
11608 if (operand_equal_p (arg0, arg1, 0))
11609 return omit_one_operand (type, integer_zero_node, arg0);
11611 /* !X ^ X is always true. */
11612 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11613 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11614 return omit_one_operand (type, integer_one_node, arg1);
11616 /* X ^ !X is always true. */
11617 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11618 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11619 return omit_one_operand (type, integer_one_node, arg0);
11625 tem = fold_comparison (code, type, op0, op1);
11626 if (tem != NULL_TREE)
11629 /* bool_var != 0 becomes bool_var. */
11630 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
11631 && code == NE_EXPR)
11632 return non_lvalue (fold_convert (type, arg0));
11634 /* bool_var == 1 becomes bool_var. */
11635 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
11636 && code == EQ_EXPR)
11637 return non_lvalue (fold_convert (type, arg0));
11639 /* bool_var != 1 becomes !bool_var. */
11640 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
11641 && code == NE_EXPR)
11642 return fold_build1 (TRUTH_NOT_EXPR, type, arg0);
11644 /* bool_var == 0 becomes !bool_var. */
11645 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
11646 && code == EQ_EXPR)
11647 return fold_build1 (TRUTH_NOT_EXPR, type, arg0);
11649 /* If this is an equality comparison of the address of two non-weak,
11650 unaliased symbols neither of which are extern (since we do not
11651 have access to attributes for externs), then we know the result. */
11652 if (TREE_CODE (arg0) == ADDR_EXPR
11653 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
11654 && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
11655 && ! lookup_attribute ("alias",
11656 DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
11657 && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
11658 && TREE_CODE (arg1) == ADDR_EXPR
11659 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
11660 && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
11661 && ! lookup_attribute ("alias",
11662 DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
11663 && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
11665 /* We know that we're looking at the address of two
11666 non-weak, unaliased, static _DECL nodes.
11668 It is both wasteful and incorrect to call operand_equal_p
11669 to compare the two ADDR_EXPR nodes. It is wasteful in that
11670 all we need to do is test pointer equality for the arguments
11671 to the two ADDR_EXPR nodes. It is incorrect to use
11672 operand_equal_p as that function is NOT equivalent to a
11673 C equality test. It can in fact return false for two
11674 objects which would test as equal using the C equality
11676 bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
11677 return constant_boolean_node (equal
11678 ? code == EQ_EXPR : code != EQ_EXPR,
11682 /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
11683 a MINUS_EXPR of a constant, we can convert it into a comparison with
11684 a revised constant as long as no overflow occurs. */
11685 if (TREE_CODE (arg1) == INTEGER_CST
11686 && (TREE_CODE (arg0) == PLUS_EXPR
11687 || TREE_CODE (arg0) == MINUS_EXPR)
11688 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11689 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
11690 ? MINUS_EXPR : PLUS_EXPR,
11691 fold_convert (TREE_TYPE (arg0), arg1),
11692 TREE_OPERAND (arg0, 1), 0))
11693 && !TREE_OVERFLOW (tem))
11694 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
11696 /* Similarly for a NEGATE_EXPR. */
11697 if (TREE_CODE (arg0) == NEGATE_EXPR
11698 && TREE_CODE (arg1) == INTEGER_CST
11699 && 0 != (tem = negate_expr (arg1))
11700 && TREE_CODE (tem) == INTEGER_CST
11701 && !TREE_OVERFLOW (tem))
11702 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
11704 /* Similarly for a BIT_XOR_EXPR; X ^ C1 == C2 is X == (C1 ^ C2). */
11705 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11706 && TREE_CODE (arg1) == INTEGER_CST
11707 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11708 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11709 fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg0),
11710 fold_convert (TREE_TYPE (arg0), arg1),
11711 TREE_OPERAND (arg0, 1)));
11713 /* Transform comparisons of the form X +- C CMP X. */
11714 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
11715 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11716 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11717 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11718 || POINTER_TYPE_P (TREE_TYPE (arg0))))
11720 tree cst = TREE_OPERAND (arg0, 1);
11722 if (code == EQ_EXPR
11723 && !integer_zerop (cst))
11724 return omit_two_operands (type, boolean_false_node,
11725 TREE_OPERAND (arg0, 0), arg1);
11727 return omit_two_operands (type, boolean_true_node,
11728 TREE_OPERAND (arg0, 0), arg1);
11731 /* If we have X - Y == 0, we can convert that to X == Y and similarly
11732 for !=. Don't do this for ordered comparisons due to overflow. */
11733 if (TREE_CODE (arg0) == MINUS_EXPR
11734 && integer_zerop (arg1))
11735 return fold_build2 (code, type,
11736 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
11738 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
11739 if (TREE_CODE (arg0) == ABS_EXPR
11740 && (integer_zerop (arg1) || real_zerop (arg1)))
11741 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), arg1);
11743 /* If this is an EQ or NE comparison with zero and ARG0 is
11744 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
11745 two operations, but the latter can be done in one less insn
11746 on machines that have only two-operand insns or on which a
11747 constant cannot be the first operand. */
11748 if (TREE_CODE (arg0) == BIT_AND_EXPR
11749 && integer_zerop (arg1))
11751 tree arg00 = TREE_OPERAND (arg0, 0);
11752 tree arg01 = TREE_OPERAND (arg0, 1);
11753 if (TREE_CODE (arg00) == LSHIFT_EXPR
11754 && integer_onep (TREE_OPERAND (arg00, 0)))
11756 fold_build2 (code, type,
11757 build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
11758 build2 (RSHIFT_EXPR, TREE_TYPE (arg00),
11759 arg01, TREE_OPERAND (arg00, 1)),
11760 fold_convert (TREE_TYPE (arg0),
11761 integer_one_node)),
11763 else if (TREE_CODE (TREE_OPERAND (arg0, 1)) == LSHIFT_EXPR
11764 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 1), 0)))
11766 fold_build2 (code, type,
11767 build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
11768 build2 (RSHIFT_EXPR, TREE_TYPE (arg01),
11769 arg00, TREE_OPERAND (arg01, 1)),
11770 fold_convert (TREE_TYPE (arg0),
11771 integer_one_node)),
11775 /* If this is an NE or EQ comparison of zero against the result of a
11776 signed MOD operation whose second operand is a power of 2, make
11777 the MOD operation unsigned since it is simpler and equivalent. */
11778 if (integer_zerop (arg1)
11779 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
11780 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
11781 || TREE_CODE (arg0) == CEIL_MOD_EXPR
11782 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
11783 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
11784 && integer_pow2p (TREE_OPERAND (arg0, 1)))
11786 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
11787 tree newmod = fold_build2 (TREE_CODE (arg0), newtype,
11788 fold_convert (newtype,
11789 TREE_OPERAND (arg0, 0)),
11790 fold_convert (newtype,
11791 TREE_OPERAND (arg0, 1)));
11793 return fold_build2 (code, type, newmod,
11794 fold_convert (newtype, arg1));
11797 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
11798 C1 is a valid shift constant, and C2 is a power of two, i.e.
11800 if (TREE_CODE (arg0) == BIT_AND_EXPR
11801 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
11802 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
11804 && integer_pow2p (TREE_OPERAND (arg0, 1))
11805 && integer_zerop (arg1))
11807 tree itype = TREE_TYPE (arg0);
11808 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
11809 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
11811 /* Check for a valid shift count. */
11812 if (TREE_INT_CST_HIGH (arg001) == 0
11813 && TREE_INT_CST_LOW (arg001) < prec)
11815 tree arg01 = TREE_OPERAND (arg0, 1);
11816 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
11817 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
11818 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
11819 can be rewritten as (X & (C2 << C1)) != 0. */
11820 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
11822 tem = fold_build2 (LSHIFT_EXPR, itype, arg01, arg001);
11823 tem = fold_build2 (BIT_AND_EXPR, itype, arg000, tem);
11824 return fold_build2 (code, type, tem, arg1);
11826 /* Otherwise, for signed (arithmetic) shifts,
11827 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
11828 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
11829 else if (!TYPE_UNSIGNED (itype))
11830 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
11831 arg000, build_int_cst (itype, 0));
11832 /* Otherwise, of unsigned (logical) shifts,
11833 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
11834 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
11836 return omit_one_operand (type,
11837 code == EQ_EXPR ? integer_one_node
11838 : integer_zero_node,
11843 /* If this is an NE comparison of zero with an AND of one, remove the
11844 comparison since the AND will give the correct value. */
11845 if (code == NE_EXPR
11846 && integer_zerop (arg1)
11847 && TREE_CODE (arg0) == BIT_AND_EXPR
11848 && integer_onep (TREE_OPERAND (arg0, 1)))
11849 return fold_convert (type, arg0);
11851 /* If we have (A & C) == C where C is a power of 2, convert this into
11852 (A & C) != 0. Similarly for NE_EXPR. */
11853 if (TREE_CODE (arg0) == BIT_AND_EXPR
11854 && integer_pow2p (TREE_OPERAND (arg0, 1))
11855 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11856 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
11857 arg0, fold_convert (TREE_TYPE (arg0),
11858 integer_zero_node));
11860 /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
11861 bit, then fold the expression into A < 0 or A >= 0. */
11862 tem = fold_single_bit_test_into_sign_test (code, arg0, arg1, type);
11866 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
11867 Similarly for NE_EXPR. */
11868 if (TREE_CODE (arg0) == BIT_AND_EXPR
11869 && TREE_CODE (arg1) == INTEGER_CST
11870 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11872 tree notc = fold_build1 (BIT_NOT_EXPR,
11873 TREE_TYPE (TREE_OPERAND (arg0, 1)),
11874 TREE_OPERAND (arg0, 1));
11875 tree dandnotc = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
11877 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
11878 if (integer_nonzerop (dandnotc))
11879 return omit_one_operand (type, rslt, arg0);
11882 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
11883 Similarly for NE_EXPR. */
11884 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11885 && TREE_CODE (arg1) == INTEGER_CST
11886 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11888 tree notd = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
11889 tree candnotd = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
11890 TREE_OPERAND (arg0, 1), notd);
11891 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
11892 if (integer_nonzerop (candnotd))
11893 return omit_one_operand (type, rslt, arg0);
11896 /* If this is a comparison of a field, we may be able to simplify it. */
11897 if ((TREE_CODE (arg0) == COMPONENT_REF
11898 || TREE_CODE (arg0) == BIT_FIELD_REF)
11899 /* Handle the constant case even without -O
11900 to make sure the warnings are given. */
11901 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
11903 t1 = optimize_bit_field_compare (code, type, arg0, arg1);
11908 /* Optimize comparisons of strlen vs zero to a compare of the
11909 first character of the string vs zero. To wit,
11910 strlen(ptr) == 0 => *ptr == 0
11911 strlen(ptr) != 0 => *ptr != 0
11912 Other cases should reduce to one of these two (or a constant)
11913 due to the return value of strlen being unsigned. */
11914 if (TREE_CODE (arg0) == CALL_EXPR
11915 && integer_zerop (arg1))
11917 tree fndecl = get_callee_fndecl (arg0);
11920 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
11921 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
11922 && call_expr_nargs (arg0) == 1
11923 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
11925 tree iref = build_fold_indirect_ref (CALL_EXPR_ARG (arg0, 0));
11926 return fold_build2 (code, type, iref,
11927 build_int_cst (TREE_TYPE (iref), 0));
11931 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
11932 of X. Similarly fold (X >> C) == 0 into X >= 0. */
11933 if (TREE_CODE (arg0) == RSHIFT_EXPR
11934 && integer_zerop (arg1)
11935 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11937 tree arg00 = TREE_OPERAND (arg0, 0);
11938 tree arg01 = TREE_OPERAND (arg0, 1);
11939 tree itype = TREE_TYPE (arg00);
11940 if (TREE_INT_CST_HIGH (arg01) == 0
11941 && TREE_INT_CST_LOW (arg01)
11942 == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
11944 if (TYPE_UNSIGNED (itype))
11946 itype = signed_type_for (itype);
11947 arg00 = fold_convert (itype, arg00);
11949 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
11950 type, arg00, build_int_cst (itype, 0));
11954 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
11955 if (integer_zerop (arg1)
11956 && TREE_CODE (arg0) == BIT_XOR_EXPR)
11957 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11958 TREE_OPERAND (arg0, 1));
11960 /* (X ^ Y) == Y becomes X == 0. We know that Y has no side-effects. */
11961 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11962 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11963 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11964 build_int_cst (TREE_TYPE (arg1), 0));
11965 /* Likewise (X ^ Y) == X becomes Y == 0. X has no side-effects. */
11966 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11967 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11968 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11969 return fold_build2 (code, type, TREE_OPERAND (arg0, 1),
11970 build_int_cst (TREE_TYPE (arg1), 0));
11972 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
11973 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11974 && TREE_CODE (arg1) == INTEGER_CST
11975 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11976 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11977 fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg1),
11978 TREE_OPERAND (arg0, 1), arg1));
11980 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
11981 (X & C) == 0 when C is a single bit. */
11982 if (TREE_CODE (arg0) == BIT_AND_EXPR
11983 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
11984 && integer_zerop (arg1)
11985 && integer_pow2p (TREE_OPERAND (arg0, 1)))
11987 tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
11988 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
11989 TREE_OPERAND (arg0, 1));
11990 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
11994 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
11995 constant C is a power of two, i.e. a single bit. */
11996 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11997 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
11998 && integer_zerop (arg1)
11999 && integer_pow2p (TREE_OPERAND (arg0, 1))
12000 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12001 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12003 tree arg00 = TREE_OPERAND (arg0, 0);
12004 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12005 arg00, build_int_cst (TREE_TYPE (arg00), 0));
12008 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12009 when is C is a power of two, i.e. a single bit. */
12010 if (TREE_CODE (arg0) == BIT_AND_EXPR
12011 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12012 && integer_zerop (arg1)
12013 && integer_pow2p (TREE_OPERAND (arg0, 1))
12014 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12015 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12017 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12018 tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg000),
12019 arg000, TREE_OPERAND (arg0, 1));
12020 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12021 tem, build_int_cst (TREE_TYPE (tem), 0));
12024 if (integer_zerop (arg1)
12025 && tree_expr_nonzero_p (arg0))
12027 tree res = constant_boolean_node (code==NE_EXPR, type);
12028 return omit_one_operand (type, res, arg0);
12031 /* Fold -X op -Y as X op Y, where op is eq/ne. */
12032 if (TREE_CODE (arg0) == NEGATE_EXPR
12033 && TREE_CODE (arg1) == NEGATE_EXPR)
12034 return fold_build2 (code, type,
12035 TREE_OPERAND (arg0, 0),
12036 TREE_OPERAND (arg1, 0));
12038 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
12039 if (TREE_CODE (arg0) == BIT_AND_EXPR
12040 && TREE_CODE (arg1) == BIT_AND_EXPR)
12042 tree arg00 = TREE_OPERAND (arg0, 0);
12043 tree arg01 = TREE_OPERAND (arg0, 1);
12044 tree arg10 = TREE_OPERAND (arg1, 0);
12045 tree arg11 = TREE_OPERAND (arg1, 1);
12046 tree itype = TREE_TYPE (arg0);
12048 if (operand_equal_p (arg01, arg11, 0))
12049 return fold_build2 (code, type,
12050 fold_build2 (BIT_AND_EXPR, itype,
12051 fold_build2 (BIT_XOR_EXPR, itype,
12054 build_int_cst (itype, 0));
12056 if (operand_equal_p (arg01, arg10, 0))
12057 return fold_build2 (code, type,
12058 fold_build2 (BIT_AND_EXPR, itype,
12059 fold_build2 (BIT_XOR_EXPR, itype,
12062 build_int_cst (itype, 0));
12064 if (operand_equal_p (arg00, arg11, 0))
12065 return fold_build2 (code, type,
12066 fold_build2 (BIT_AND_EXPR, itype,
12067 fold_build2 (BIT_XOR_EXPR, itype,
12070 build_int_cst (itype, 0));
12072 if (operand_equal_p (arg00, arg10, 0))
12073 return fold_build2 (code, type,
12074 fold_build2 (BIT_AND_EXPR, itype,
12075 fold_build2 (BIT_XOR_EXPR, itype,
12078 build_int_cst (itype, 0));
12081 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12082 && TREE_CODE (arg1) == BIT_XOR_EXPR)
12084 tree arg00 = TREE_OPERAND (arg0, 0);
12085 tree arg01 = TREE_OPERAND (arg0, 1);
12086 tree arg10 = TREE_OPERAND (arg1, 0);
12087 tree arg11 = TREE_OPERAND (arg1, 1);
12088 tree itype = TREE_TYPE (arg0);
12090 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12091 operand_equal_p guarantees no side-effects so we don't need
12092 to use omit_one_operand on Z. */
12093 if (operand_equal_p (arg01, arg11, 0))
12094 return fold_build2 (code, type, arg00, arg10);
12095 if (operand_equal_p (arg01, arg10, 0))
12096 return fold_build2 (code, type, arg00, arg11);
12097 if (operand_equal_p (arg00, arg11, 0))
12098 return fold_build2 (code, type, arg01, arg10);
12099 if (operand_equal_p (arg00, arg10, 0))
12100 return fold_build2 (code, type, arg01, arg11);
12102 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12103 if (TREE_CODE (arg01) == INTEGER_CST
12104 && TREE_CODE (arg11) == INTEGER_CST)
12105 return fold_build2 (code, type,
12106 fold_build2 (BIT_XOR_EXPR, itype, arg00,
12107 fold_build2 (BIT_XOR_EXPR, itype,
12112 /* Attempt to simplify equality/inequality comparisons of complex
12113 values. Only lower the comparison if the result is known or
12114 can be simplified to a single scalar comparison. */
12115 if ((TREE_CODE (arg0) == COMPLEX_EXPR
12116 || TREE_CODE (arg0) == COMPLEX_CST)
12117 && (TREE_CODE (arg1) == COMPLEX_EXPR
12118 || TREE_CODE (arg1) == COMPLEX_CST))
12120 tree real0, imag0, real1, imag1;
12123 if (TREE_CODE (arg0) == COMPLEX_EXPR)
12125 real0 = TREE_OPERAND (arg0, 0);
12126 imag0 = TREE_OPERAND (arg0, 1);
12130 real0 = TREE_REALPART (arg0);
12131 imag0 = TREE_IMAGPART (arg0);
12134 if (TREE_CODE (arg1) == COMPLEX_EXPR)
12136 real1 = TREE_OPERAND (arg1, 0);
12137 imag1 = TREE_OPERAND (arg1, 1);
12141 real1 = TREE_REALPART (arg1);
12142 imag1 = TREE_IMAGPART (arg1);
12145 rcond = fold_binary (code, type, real0, real1);
12146 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12148 if (integer_zerop (rcond))
12150 if (code == EQ_EXPR)
12151 return omit_two_operands (type, boolean_false_node,
12153 return fold_build2 (NE_EXPR, type, imag0, imag1);
12157 if (code == NE_EXPR)
12158 return omit_two_operands (type, boolean_true_node,
12160 return fold_build2 (EQ_EXPR, type, imag0, imag1);
12164 icond = fold_binary (code, type, imag0, imag1);
12165 if (icond && TREE_CODE (icond) == INTEGER_CST)
12167 if (integer_zerop (icond))
12169 if (code == EQ_EXPR)
12170 return omit_two_operands (type, boolean_false_node,
12172 return fold_build2 (NE_EXPR, type, real0, real1);
12176 if (code == NE_EXPR)
12177 return omit_two_operands (type, boolean_true_node,
12179 return fold_build2 (EQ_EXPR, type, real0, real1);
12190 tem = fold_comparison (code, type, op0, op1);
12191 if (tem != NULL_TREE)
12194 /* Transform comparisons of the form X +- C CMP X. */
12195 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12196 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12197 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12198 && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
12199 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12200 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
12202 tree arg01 = TREE_OPERAND (arg0, 1);
12203 enum tree_code code0 = TREE_CODE (arg0);
12206 if (TREE_CODE (arg01) == REAL_CST)
12207 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12209 is_positive = tree_int_cst_sgn (arg01);
12211 /* (X - c) > X becomes false. */
12212 if (code == GT_EXPR
12213 && ((code0 == MINUS_EXPR && is_positive >= 0)
12214 || (code0 == PLUS_EXPR && is_positive <= 0)))
12216 if (TREE_CODE (arg01) == INTEGER_CST
12217 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12218 fold_overflow_warning (("assuming signed overflow does not "
12219 "occur when assuming that (X - c) > X "
12220 "is always false"),
12221 WARN_STRICT_OVERFLOW_ALL);
12222 return constant_boolean_node (0, type);
12225 /* Likewise (X + c) < X becomes false. */
12226 if (code == LT_EXPR
12227 && ((code0 == PLUS_EXPR && is_positive >= 0)
12228 || (code0 == MINUS_EXPR && is_positive <= 0)))
12230 if (TREE_CODE (arg01) == INTEGER_CST
12231 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12232 fold_overflow_warning (("assuming signed overflow does not "
12233 "occur when assuming that "
12234 "(X + c) < X is always false"),
12235 WARN_STRICT_OVERFLOW_ALL);
12236 return constant_boolean_node (0, type);
12239 /* Convert (X - c) <= X to true. */
12240 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12242 && ((code0 == MINUS_EXPR && is_positive >= 0)
12243 || (code0 == PLUS_EXPR && is_positive <= 0)))
12245 if (TREE_CODE (arg01) == INTEGER_CST
12246 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12247 fold_overflow_warning (("assuming signed overflow does not "
12248 "occur when assuming that "
12249 "(X - c) <= X is always true"),
12250 WARN_STRICT_OVERFLOW_ALL);
12251 return constant_boolean_node (1, type);
12254 /* Convert (X + c) >= X to true. */
12255 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12257 && ((code0 == PLUS_EXPR && is_positive >= 0)
12258 || (code0 == MINUS_EXPR && is_positive <= 0)))
12260 if (TREE_CODE (arg01) == INTEGER_CST
12261 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12262 fold_overflow_warning (("assuming signed overflow does not "
12263 "occur when assuming that "
12264 "(X + c) >= X is always true"),
12265 WARN_STRICT_OVERFLOW_ALL);
12266 return constant_boolean_node (1, type);
12269 if (TREE_CODE (arg01) == INTEGER_CST)
12271 /* Convert X + c > X and X - c < X to true for integers. */
12272 if (code == GT_EXPR
12273 && ((code0 == PLUS_EXPR && is_positive > 0)
12274 || (code0 == MINUS_EXPR && is_positive < 0)))
12276 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12277 fold_overflow_warning (("assuming signed overflow does "
12278 "not occur when assuming that "
12279 "(X + c) > X is always true"),
12280 WARN_STRICT_OVERFLOW_ALL);
12281 return constant_boolean_node (1, type);
12284 if (code == LT_EXPR
12285 && ((code0 == MINUS_EXPR && is_positive > 0)
12286 || (code0 == PLUS_EXPR && is_positive < 0)))
12288 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12289 fold_overflow_warning (("assuming signed overflow does "
12290 "not occur when assuming that "
12291 "(X - c) < X is always true"),
12292 WARN_STRICT_OVERFLOW_ALL);
12293 return constant_boolean_node (1, type);
12296 /* Convert X + c <= X and X - c >= X to false for integers. */
12297 if (code == LE_EXPR
12298 && ((code0 == PLUS_EXPR && is_positive > 0)
12299 || (code0 == MINUS_EXPR && is_positive < 0)))
12301 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12302 fold_overflow_warning (("assuming signed overflow does "
12303 "not occur when assuming that "
12304 "(X + c) <= X is always false"),
12305 WARN_STRICT_OVERFLOW_ALL);
12306 return constant_boolean_node (0, type);
12309 if (code == GE_EXPR
12310 && ((code0 == MINUS_EXPR && is_positive > 0)
12311 || (code0 == PLUS_EXPR && is_positive < 0)))
12313 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12314 fold_overflow_warning (("assuming signed overflow does "
12315 "not occur when assuming that "
12316 "(X - c) >= X is always false"),
12317 WARN_STRICT_OVERFLOW_ALL);
12318 return constant_boolean_node (0, type);
12323 /* Change X >= C to X > (C - 1) and X < C to X <= (C - 1) if C > 0.
12324 This transformation affects the cases which are handled in later
12325 optimizations involving comparisons with non-negative constants. */
12326 if (TREE_CODE (arg1) == INTEGER_CST
12327 && TREE_CODE (arg0) != INTEGER_CST
12328 && tree_int_cst_sgn (arg1) > 0)
12330 if (code == GE_EXPR)
12332 arg1 = const_binop (MINUS_EXPR, arg1,
12333 build_int_cst (TREE_TYPE (arg1), 1), 0);
12334 return fold_build2 (GT_EXPR, type, arg0,
12335 fold_convert (TREE_TYPE (arg0), arg1));
12337 if (code == LT_EXPR)
12339 arg1 = const_binop (MINUS_EXPR, arg1,
12340 build_int_cst (TREE_TYPE (arg1), 1), 0);
12341 return fold_build2 (LE_EXPR, type, arg0,
12342 fold_convert (TREE_TYPE (arg0), arg1));
12346 /* Comparisons with the highest or lowest possible integer of
12347 the specified precision will have known values. */
12349 tree arg1_type = TREE_TYPE (arg1);
12350 unsigned int width = TYPE_PRECISION (arg1_type);
12352 if (TREE_CODE (arg1) == INTEGER_CST
12353 && !TREE_OVERFLOW (arg1)
12354 && width <= 2 * HOST_BITS_PER_WIDE_INT
12355 && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
12357 HOST_WIDE_INT signed_max_hi;
12358 unsigned HOST_WIDE_INT signed_max_lo;
12359 unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
12361 if (width <= HOST_BITS_PER_WIDE_INT)
12363 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12368 if (TYPE_UNSIGNED (arg1_type))
12370 max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12376 max_lo = signed_max_lo;
12377 min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12383 width -= HOST_BITS_PER_WIDE_INT;
12384 signed_max_lo = -1;
12385 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12390 if (TYPE_UNSIGNED (arg1_type))
12392 max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12397 max_hi = signed_max_hi;
12398 min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12402 if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
12403 && TREE_INT_CST_LOW (arg1) == max_lo)
12407 return omit_one_operand (type, integer_zero_node, arg0);
12410 return fold_build2 (EQ_EXPR, type, op0, op1);
12413 return omit_one_operand (type, integer_one_node, arg0);
12416 return fold_build2 (NE_EXPR, type, op0, op1);
12418 /* The GE_EXPR and LT_EXPR cases above are not normally
12419 reached because of previous transformations. */
12424 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12426 && TREE_INT_CST_LOW (arg1) == max_lo - 1)
12430 arg1 = const_binop (PLUS_EXPR, arg1,
12431 build_int_cst (TREE_TYPE (arg1), 1), 0);
12432 return fold_build2 (EQ_EXPR, type,
12433 fold_convert (TREE_TYPE (arg1), arg0),
12436 arg1 = const_binop (PLUS_EXPR, arg1,
12437 build_int_cst (TREE_TYPE (arg1), 1), 0);
12438 return fold_build2 (NE_EXPR, type,
12439 fold_convert (TREE_TYPE (arg1), arg0),
12444 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12446 && TREE_INT_CST_LOW (arg1) == min_lo)
12450 return omit_one_operand (type, integer_zero_node, arg0);
12453 return fold_build2 (EQ_EXPR, type, op0, op1);
12456 return omit_one_operand (type, integer_one_node, arg0);
12459 return fold_build2 (NE_EXPR, type, op0, op1);
12464 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12466 && TREE_INT_CST_LOW (arg1) == min_lo + 1)
12470 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
12471 return fold_build2 (NE_EXPR, type,
12472 fold_convert (TREE_TYPE (arg1), arg0),
12475 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
12476 return fold_build2 (EQ_EXPR, type,
12477 fold_convert (TREE_TYPE (arg1), arg0),
12483 else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
12484 && TREE_INT_CST_LOW (arg1) == signed_max_lo
12485 && TYPE_UNSIGNED (arg1_type)
12486 /* We will flip the signedness of the comparison operator
12487 associated with the mode of arg1, so the sign bit is
12488 specified by this mode. Check that arg1 is the signed
12489 max associated with this sign bit. */
12490 && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
12491 /* signed_type does not work on pointer types. */
12492 && INTEGRAL_TYPE_P (arg1_type))
12494 /* The following case also applies to X < signed_max+1
12495 and X >= signed_max+1 because previous transformations. */
12496 if (code == LE_EXPR || code == GT_EXPR)
12499 st = signed_type_for (TREE_TYPE (arg1));
12500 return fold_build2 (code == LE_EXPR ? GE_EXPR : LT_EXPR,
12501 type, fold_convert (st, arg0),
12502 build_int_cst (st, 0));
12508 /* If we are comparing an ABS_EXPR with a constant, we can
12509 convert all the cases into explicit comparisons, but they may
12510 well not be faster than doing the ABS and one comparison.
12511 But ABS (X) <= C is a range comparison, which becomes a subtraction
12512 and a comparison, and is probably faster. */
12513 if (code == LE_EXPR
12514 && TREE_CODE (arg1) == INTEGER_CST
12515 && TREE_CODE (arg0) == ABS_EXPR
12516 && ! TREE_SIDE_EFFECTS (arg0)
12517 && (0 != (tem = negate_expr (arg1)))
12518 && TREE_CODE (tem) == INTEGER_CST
12519 && !TREE_OVERFLOW (tem))
12520 return fold_build2 (TRUTH_ANDIF_EXPR, type,
12521 build2 (GE_EXPR, type,
12522 TREE_OPERAND (arg0, 0), tem),
12523 build2 (LE_EXPR, type,
12524 TREE_OPERAND (arg0, 0), arg1));
12526 /* Convert ABS_EXPR<x> >= 0 to true. */
12527 strict_overflow_p = false;
12528 if (code == GE_EXPR
12529 && (integer_zerop (arg1)
12530 || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
12531 && real_zerop (arg1)))
12532 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12534 if (strict_overflow_p)
12535 fold_overflow_warning (("assuming signed overflow does not occur "
12536 "when simplifying comparison of "
12537 "absolute value and zero"),
12538 WARN_STRICT_OVERFLOW_CONDITIONAL);
12539 return omit_one_operand (type, integer_one_node, arg0);
12542 /* Convert ABS_EXPR<x> < 0 to false. */
12543 strict_overflow_p = false;
12544 if (code == LT_EXPR
12545 && (integer_zerop (arg1) || real_zerop (arg1))
12546 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12548 if (strict_overflow_p)
12549 fold_overflow_warning (("assuming signed overflow does not occur "
12550 "when simplifying comparison of "
12551 "absolute value and zero"),
12552 WARN_STRICT_OVERFLOW_CONDITIONAL);
12553 return omit_one_operand (type, integer_zero_node, arg0);
12556 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12557 and similarly for >= into !=. */
12558 if ((code == LT_EXPR || code == GE_EXPR)
12559 && TYPE_UNSIGNED (TREE_TYPE (arg0))
12560 && TREE_CODE (arg1) == LSHIFT_EXPR
12561 && integer_onep (TREE_OPERAND (arg1, 0)))
12562 return build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12563 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12564 TREE_OPERAND (arg1, 1)),
12565 build_int_cst (TREE_TYPE (arg0), 0));
12567 if ((code == LT_EXPR || code == GE_EXPR)
12568 && TYPE_UNSIGNED (TREE_TYPE (arg0))
12569 && (TREE_CODE (arg1) == NOP_EXPR
12570 || TREE_CODE (arg1) == CONVERT_EXPR)
12571 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
12572 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
12574 build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12575 fold_convert (TREE_TYPE (arg0),
12576 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12577 TREE_OPERAND (TREE_OPERAND (arg1, 0),
12579 build_int_cst (TREE_TYPE (arg0), 0));
12583 case UNORDERED_EXPR:
12591 if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
12593 t1 = fold_relational_const (code, type, arg0, arg1);
12594 if (t1 != NULL_TREE)
12598 /* If the first operand is NaN, the result is constant. */
12599 if (TREE_CODE (arg0) == REAL_CST
12600 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
12601 && (code != LTGT_EXPR || ! flag_trapping_math))
12603 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
12604 ? integer_zero_node
12605 : integer_one_node;
12606 return omit_one_operand (type, t1, arg1);
12609 /* If the second operand is NaN, the result is constant. */
12610 if (TREE_CODE (arg1) == REAL_CST
12611 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
12612 && (code != LTGT_EXPR || ! flag_trapping_math))
12614 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
12615 ? integer_zero_node
12616 : integer_one_node;
12617 return omit_one_operand (type, t1, arg0);
12620 /* Simplify unordered comparison of something with itself. */
12621 if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
12622 && operand_equal_p (arg0, arg1, 0))
12623 return constant_boolean_node (1, type);
12625 if (code == LTGT_EXPR
12626 && !flag_trapping_math
12627 && operand_equal_p (arg0, arg1, 0))
12628 return constant_boolean_node (0, type);
12630 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
12632 tree targ0 = strip_float_extensions (arg0);
12633 tree targ1 = strip_float_extensions (arg1);
12634 tree newtype = TREE_TYPE (targ0);
12636 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
12637 newtype = TREE_TYPE (targ1);
12639 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
12640 return fold_build2 (code, type, fold_convert (newtype, targ0),
12641 fold_convert (newtype, targ1));
12646 case COMPOUND_EXPR:
12647 /* When pedantic, a compound expression can be neither an lvalue
12648 nor an integer constant expression. */
12649 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
12651 /* Don't let (0, 0) be null pointer constant. */
12652 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
12653 : fold_convert (type, arg1);
12654 return pedantic_non_lvalue (tem);
12657 if ((TREE_CODE (arg0) == REAL_CST
12658 && TREE_CODE (arg1) == REAL_CST)
12659 || (TREE_CODE (arg0) == INTEGER_CST
12660 && TREE_CODE (arg1) == INTEGER_CST))
12661 return build_complex (type, arg0, arg1);
12665 /* An ASSERT_EXPR should never be passed to fold_binary. */
12666 gcc_unreachable ();
12670 } /* switch (code) */
12673 /* Callback for walk_tree, looking for LABEL_EXPR.
12674 Returns tree TP if it is LABEL_EXPR. Otherwise it returns NULL_TREE.
12675 Do not check the sub-tree of GOTO_EXPR. */
12678 contains_label_1 (tree *tp,
12679 int *walk_subtrees,
12680 void *data ATTRIBUTE_UNUSED)
12682 switch (TREE_CODE (*tp))
12687 *walk_subtrees = 0;
12694 /* Checks whether the sub-tree ST contains a label LABEL_EXPR which is
12695 accessible from outside the sub-tree. Returns NULL_TREE if no
12696 addressable label is found. */
12699 contains_label_p (tree st)
12701 return (walk_tree (&st, contains_label_1 , NULL, NULL) != NULL_TREE);
12704 /* Fold a ternary expression of code CODE and type TYPE with operands
12705 OP0, OP1, and OP2. Return the folded expression if folding is
12706 successful. Otherwise, return NULL_TREE. */
12709 fold_ternary (enum tree_code code, tree type, tree op0, tree op1, tree op2)
12712 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
12713 enum tree_code_class kind = TREE_CODE_CLASS (code);
12715 gcc_assert (IS_EXPR_CODE_CLASS (kind)
12716 && TREE_CODE_LENGTH (code) == 3);
12718 /* Strip any conversions that don't change the mode. This is safe
12719 for every expression, except for a comparison expression because
12720 its signedness is derived from its operands. So, in the latter
12721 case, only strip conversions that don't change the signedness.
12723 Note that this is done as an internal manipulation within the
12724 constant folder, in order to find the simplest representation of
12725 the arguments so that their form can be studied. In any cases,
12726 the appropriate type conversions should be put back in the tree
12727 that will get out of the constant folder. */
12742 case COMPONENT_REF:
12743 if (TREE_CODE (arg0) == CONSTRUCTOR
12744 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
12746 unsigned HOST_WIDE_INT idx;
12748 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
12755 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
12756 so all simple results must be passed through pedantic_non_lvalue. */
12757 if (TREE_CODE (arg0) == INTEGER_CST)
12759 tree unused_op = integer_zerop (arg0) ? op1 : op2;
12760 tem = integer_zerop (arg0) ? op2 : op1;
12761 /* Only optimize constant conditions when the selected branch
12762 has the same type as the COND_EXPR. This avoids optimizing
12763 away "c ? x : throw", where the throw has a void type.
12764 Avoid throwing away that operand which contains label. */
12765 if ((!TREE_SIDE_EFFECTS (unused_op)
12766 || !contains_label_p (unused_op))
12767 && (! VOID_TYPE_P (TREE_TYPE (tem))
12768 || VOID_TYPE_P (type)))
12769 return pedantic_non_lvalue (tem);
12772 if (operand_equal_p (arg1, op2, 0))
12773 return pedantic_omit_one_operand (type, arg1, arg0);
12775 /* If we have A op B ? A : C, we may be able to convert this to a
12776 simpler expression, depending on the operation and the values
12777 of B and C. Signed zeros prevent all of these transformations,
12778 for reasons given above each one.
12780 Also try swapping the arguments and inverting the conditional. */
12781 if (COMPARISON_CLASS_P (arg0)
12782 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
12783 arg1, TREE_OPERAND (arg0, 1))
12784 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
12786 tem = fold_cond_expr_with_comparison (type, arg0, op1, op2);
12791 if (COMPARISON_CLASS_P (arg0)
12792 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
12794 TREE_OPERAND (arg0, 1))
12795 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
12797 tem = fold_truth_not_expr (arg0);
12798 if (tem && COMPARISON_CLASS_P (tem))
12800 tem = fold_cond_expr_with_comparison (type, tem, op2, op1);
12806 /* If the second operand is simpler than the third, swap them
12807 since that produces better jump optimization results. */
12808 if (truth_value_p (TREE_CODE (arg0))
12809 && tree_swap_operands_p (op1, op2, false))
12811 /* See if this can be inverted. If it can't, possibly because
12812 it was a floating-point inequality comparison, don't do
12814 tem = fold_truth_not_expr (arg0);
12816 return fold_build3 (code, type, tem, op2, op1);
12819 /* Convert A ? 1 : 0 to simply A. */
12820 if (integer_onep (op1)
12821 && integer_zerop (op2)
12822 /* If we try to convert OP0 to our type, the
12823 call to fold will try to move the conversion inside
12824 a COND, which will recurse. In that case, the COND_EXPR
12825 is probably the best choice, so leave it alone. */
12826 && type == TREE_TYPE (arg0))
12827 return pedantic_non_lvalue (arg0);
12829 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
12830 over COND_EXPR in cases such as floating point comparisons. */
12831 if (integer_zerop (op1)
12832 && integer_onep (op2)
12833 && truth_value_p (TREE_CODE (arg0)))
12834 return pedantic_non_lvalue (fold_convert (type,
12835 invert_truthvalue (arg0)));
12837 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
12838 if (TREE_CODE (arg0) == LT_EXPR
12839 && integer_zerop (TREE_OPERAND (arg0, 1))
12840 && integer_zerop (op2)
12841 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
12843 /* sign_bit_p only checks ARG1 bits within A's precision.
12844 If <sign bit of A> has wider type than A, bits outside
12845 of A's precision in <sign bit of A> need to be checked.
12846 If they are all 0, this optimization needs to be done
12847 in unsigned A's type, if they are all 1 in signed A's type,
12848 otherwise this can't be done. */
12849 if (TYPE_PRECISION (TREE_TYPE (tem))
12850 < TYPE_PRECISION (TREE_TYPE (arg1))
12851 && TYPE_PRECISION (TREE_TYPE (tem))
12852 < TYPE_PRECISION (type))
12854 unsigned HOST_WIDE_INT mask_lo;
12855 HOST_WIDE_INT mask_hi;
12856 int inner_width, outer_width;
12859 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
12860 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
12861 if (outer_width > TYPE_PRECISION (type))
12862 outer_width = TYPE_PRECISION (type);
12864 if (outer_width > HOST_BITS_PER_WIDE_INT)
12866 mask_hi = ((unsigned HOST_WIDE_INT) -1
12867 >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
12873 mask_lo = ((unsigned HOST_WIDE_INT) -1
12874 >> (HOST_BITS_PER_WIDE_INT - outer_width));
12876 if (inner_width > HOST_BITS_PER_WIDE_INT)
12878 mask_hi &= ~((unsigned HOST_WIDE_INT) -1
12879 >> (HOST_BITS_PER_WIDE_INT - inner_width));
12883 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
12884 >> (HOST_BITS_PER_WIDE_INT - inner_width));
12886 if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
12887 && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
12889 tem_type = signed_type_for (TREE_TYPE (tem));
12890 tem = fold_convert (tem_type, tem);
12892 else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
12893 && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
12895 tem_type = unsigned_type_for (TREE_TYPE (tem));
12896 tem = fold_convert (tem_type, tem);
12903 return fold_convert (type,
12904 fold_build2 (BIT_AND_EXPR,
12905 TREE_TYPE (tem), tem,
12906 fold_convert (TREE_TYPE (tem),
12910 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
12911 already handled above. */
12912 if (TREE_CODE (arg0) == BIT_AND_EXPR
12913 && integer_onep (TREE_OPERAND (arg0, 1))
12914 && integer_zerop (op2)
12915 && integer_pow2p (arg1))
12917 tree tem = TREE_OPERAND (arg0, 0);
12919 if (TREE_CODE (tem) == RSHIFT_EXPR
12920 && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
12921 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
12922 TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
12923 return fold_build2 (BIT_AND_EXPR, type,
12924 TREE_OPERAND (tem, 0), arg1);
12927 /* A & N ? N : 0 is simply A & N if N is a power of two. This
12928 is probably obsolete because the first operand should be a
12929 truth value (that's why we have the two cases above), but let's
12930 leave it in until we can confirm this for all front-ends. */
12931 if (integer_zerop (op2)
12932 && TREE_CODE (arg0) == NE_EXPR
12933 && integer_zerop (TREE_OPERAND (arg0, 1))
12934 && integer_pow2p (arg1)
12935 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12936 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12937 arg1, OEP_ONLY_CONST))
12938 return pedantic_non_lvalue (fold_convert (type,
12939 TREE_OPERAND (arg0, 0)));
12941 /* Convert A ? B : 0 into A && B if A and B are truth values. */
12942 if (integer_zerop (op2)
12943 && truth_value_p (TREE_CODE (arg0))
12944 && truth_value_p (TREE_CODE (arg1)))
12945 return fold_build2 (TRUTH_ANDIF_EXPR, type,
12946 fold_convert (type, arg0),
12949 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
12950 if (integer_onep (op2)
12951 && truth_value_p (TREE_CODE (arg0))
12952 && truth_value_p (TREE_CODE (arg1)))
12954 /* Only perform transformation if ARG0 is easily inverted. */
12955 tem = fold_truth_not_expr (arg0);
12957 return fold_build2 (TRUTH_ORIF_EXPR, type,
12958 fold_convert (type, tem),
12962 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
12963 if (integer_zerop (arg1)
12964 && truth_value_p (TREE_CODE (arg0))
12965 && truth_value_p (TREE_CODE (op2)))
12967 /* Only perform transformation if ARG0 is easily inverted. */
12968 tem = fold_truth_not_expr (arg0);
12970 return fold_build2 (TRUTH_ANDIF_EXPR, type,
12971 fold_convert (type, tem),
12975 /* Convert A ? 1 : B into A || B if A and B are truth values. */
12976 if (integer_onep (arg1)
12977 && truth_value_p (TREE_CODE (arg0))
12978 && truth_value_p (TREE_CODE (op2)))
12979 return fold_build2 (TRUTH_ORIF_EXPR, type,
12980 fold_convert (type, arg0),
12986 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
12987 of fold_ternary on them. */
12988 gcc_unreachable ();
12990 case BIT_FIELD_REF:
12991 if ((TREE_CODE (arg0) == VECTOR_CST
12992 || (TREE_CODE (arg0) == CONSTRUCTOR && TREE_CONSTANT (arg0)))
12993 && type == TREE_TYPE (TREE_TYPE (arg0))
12994 && host_integerp (arg1, 1)
12995 && host_integerp (op2, 1))
12997 unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
12998 unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
13001 && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
13002 && (idx % width) == 0
13003 && (idx = idx / width)
13004 < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
13006 tree elements = NULL_TREE;
13008 if (TREE_CODE (arg0) == VECTOR_CST)
13009 elements = TREE_VECTOR_CST_ELTS (arg0);
13012 unsigned HOST_WIDE_INT idx;
13015 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg0), idx, value)
13016 elements = tree_cons (NULL_TREE, value, elements);
13018 while (idx-- > 0 && elements)
13019 elements = TREE_CHAIN (elements);
13021 return TREE_VALUE (elements);
13023 return fold_convert (type, integer_zero_node);
13030 } /* switch (code) */
13033 /* Perform constant folding and related simplification of EXPR.
13034 The related simplifications include x*1 => x, x*0 => 0, etc.,
13035 and application of the associative law.
13036 NOP_EXPR conversions may be removed freely (as long as we
13037 are careful not to change the type of the overall expression).
13038 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13039 but we can constant-fold them if they have constant operands. */
13041 #ifdef ENABLE_FOLD_CHECKING
13042 # define fold(x) fold_1 (x)
13043 static tree fold_1 (tree);
13049 const tree t = expr;
13050 enum tree_code code = TREE_CODE (t);
13051 enum tree_code_class kind = TREE_CODE_CLASS (code);
13054 /* Return right away if a constant. */
13055 if (kind == tcc_constant)
13058 /* CALL_EXPR-like objects with variable numbers of operands are
13059 treated specially. */
13060 if (kind == tcc_vl_exp)
13062 if (code == CALL_EXPR)
13064 tem = fold_call_expr (expr, false);
13065 return tem ? tem : expr;
13070 if (IS_EXPR_CODE_CLASS (kind)
13071 || IS_GIMPLE_STMT_CODE_CLASS (kind))
13073 tree type = TREE_TYPE (t);
13074 tree op0, op1, op2;
13076 switch (TREE_CODE_LENGTH (code))
13079 op0 = TREE_OPERAND (t, 0);
13080 tem = fold_unary (code, type, op0);
13081 return tem ? tem : expr;
13083 op0 = TREE_OPERAND (t, 0);
13084 op1 = TREE_OPERAND (t, 1);
13085 tem = fold_binary (code, type, op0, op1);
13086 return tem ? tem : expr;
13088 op0 = TREE_OPERAND (t, 0);
13089 op1 = TREE_OPERAND (t, 1);
13090 op2 = TREE_OPERAND (t, 2);
13091 tem = fold_ternary (code, type, op0, op1, op2);
13092 return tem ? tem : expr;
13101 return fold (DECL_INITIAL (t));
13105 } /* switch (code) */
13108 #ifdef ENABLE_FOLD_CHECKING
13111 static void fold_checksum_tree (tree, struct md5_ctx *, htab_t);
13112 static void fold_check_failed (tree, tree);
13113 void print_fold_checksum (tree);
13115 /* When --enable-checking=fold, compute a digest of expr before
13116 and after actual fold call to see if fold did not accidentally
13117 change original expr. */
13123 struct md5_ctx ctx;
13124 unsigned char checksum_before[16], checksum_after[16];
13127 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13128 md5_init_ctx (&ctx);
13129 fold_checksum_tree (expr, &ctx, ht);
13130 md5_finish_ctx (&ctx, checksum_before);
13133 ret = fold_1 (expr);
13135 md5_init_ctx (&ctx);
13136 fold_checksum_tree (expr, &ctx, ht);
13137 md5_finish_ctx (&ctx, checksum_after);
13140 if (memcmp (checksum_before, checksum_after, 16))
13141 fold_check_failed (expr, ret);
13147 print_fold_checksum (tree expr)
13149 struct md5_ctx ctx;
13150 unsigned char checksum[16], cnt;
13153 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13154 md5_init_ctx (&ctx);
13155 fold_checksum_tree (expr, &ctx, ht);
13156 md5_finish_ctx (&ctx, checksum);
13158 for (cnt = 0; cnt < 16; ++cnt)
13159 fprintf (stderr, "%02x", checksum[cnt]);
13160 putc ('\n', stderr);
13164 fold_check_failed (tree expr ATTRIBUTE_UNUSED, tree ret ATTRIBUTE_UNUSED)
13166 internal_error ("fold check: original tree changed by fold");
13170 fold_checksum_tree (tree expr, struct md5_ctx *ctx, htab_t ht)
13173 enum tree_code code;
13174 struct tree_function_decl buf;
13179 gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
13180 <= sizeof (struct tree_function_decl))
13181 && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
13184 slot = htab_find_slot (ht, expr, INSERT);
13188 code = TREE_CODE (expr);
13189 if (TREE_CODE_CLASS (code) == tcc_declaration
13190 && DECL_ASSEMBLER_NAME_SET_P (expr))
13192 /* Allow DECL_ASSEMBLER_NAME to be modified. */
13193 memcpy ((char *) &buf, expr, tree_size (expr));
13194 expr = (tree) &buf;
13195 SET_DECL_ASSEMBLER_NAME (expr, NULL);
13197 else if (TREE_CODE_CLASS (code) == tcc_type
13198 && (TYPE_POINTER_TO (expr) || TYPE_REFERENCE_TO (expr)
13199 || TYPE_CACHED_VALUES_P (expr)
13200 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)))
13202 /* Allow these fields to be modified. */
13203 memcpy ((char *) &buf, expr, tree_size (expr));
13204 expr = (tree) &buf;
13205 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr) = 0;
13206 TYPE_POINTER_TO (expr) = NULL;
13207 TYPE_REFERENCE_TO (expr) = NULL;
13208 if (TYPE_CACHED_VALUES_P (expr))
13210 TYPE_CACHED_VALUES_P (expr) = 0;
13211 TYPE_CACHED_VALUES (expr) = NULL;
13214 md5_process_bytes (expr, tree_size (expr), ctx);
13215 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13216 if (TREE_CODE_CLASS (code) != tcc_type
13217 && TREE_CODE_CLASS (code) != tcc_declaration
13218 && code != TREE_LIST
13219 && code != SSA_NAME)
13220 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13221 switch (TREE_CODE_CLASS (code))
13227 md5_process_bytes (TREE_STRING_POINTER (expr),
13228 TREE_STRING_LENGTH (expr), ctx);
13231 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13232 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13235 fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
13241 case tcc_exceptional:
13245 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13246 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13247 expr = TREE_CHAIN (expr);
13248 goto recursive_label;
13251 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13252 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13258 case tcc_expression:
13259 case tcc_reference:
13260 case tcc_comparison:
13263 case tcc_statement:
13265 len = TREE_OPERAND_LENGTH (expr);
13266 for (i = 0; i < len; ++i)
13267 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13269 case tcc_declaration:
13270 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13271 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13272 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13274 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13275 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13276 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13277 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13278 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13280 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
13281 fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
13283 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13285 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13286 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13287 fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
13291 if (TREE_CODE (expr) == ENUMERAL_TYPE)
13292 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13293 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13294 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13295 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13296 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13297 if (INTEGRAL_TYPE_P (expr)
13298 || SCALAR_FLOAT_TYPE_P (expr))
13300 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13301 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13303 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13304 if (TREE_CODE (expr) == RECORD_TYPE
13305 || TREE_CODE (expr) == UNION_TYPE
13306 || TREE_CODE (expr) == QUAL_UNION_TYPE)
13307 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13308 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13315 /* Helper function for outputting the checksum of a tree T. When
13316 debugging with gdb, you can "define mynext" to be "next" followed
13317 by "call debug_fold_checksum (op0)", then just trace down till the
13321 debug_fold_checksum (tree t)
13324 unsigned char checksum[16];
13325 struct md5_ctx ctx;
13326 htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13328 md5_init_ctx (&ctx);
13329 fold_checksum_tree (t, &ctx, ht);
13330 md5_finish_ctx (&ctx, checksum);
13333 for (i = 0; i < 16; i++)
13334 fprintf (stderr, "%d ", checksum[i]);
13336 fprintf (stderr, "\n");
13341 /* Fold a unary tree expression with code CODE of type TYPE with an
13342 operand OP0. Return a folded expression if successful. Otherwise,
13343 return a tree expression with code CODE of type TYPE with an
13347 fold_build1_stat (enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13350 #ifdef ENABLE_FOLD_CHECKING
13351 unsigned char checksum_before[16], checksum_after[16];
13352 struct md5_ctx ctx;
13355 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13356 md5_init_ctx (&ctx);
13357 fold_checksum_tree (op0, &ctx, ht);
13358 md5_finish_ctx (&ctx, checksum_before);
13362 tem = fold_unary (code, type, op0);
13364 tem = build1_stat (code, type, op0 PASS_MEM_STAT);
13366 #ifdef ENABLE_FOLD_CHECKING
13367 md5_init_ctx (&ctx);
13368 fold_checksum_tree (op0, &ctx, ht);
13369 md5_finish_ctx (&ctx, checksum_after);
13372 if (memcmp (checksum_before, checksum_after, 16))
13373 fold_check_failed (op0, tem);
13378 /* Fold a binary tree expression with code CODE of type TYPE with
13379 operands OP0 and OP1. Return a folded expression if successful.
13380 Otherwise, return a tree expression with code CODE of type TYPE
13381 with operands OP0 and OP1. */
13384 fold_build2_stat (enum tree_code code, tree type, tree op0, tree op1
13388 #ifdef ENABLE_FOLD_CHECKING
13389 unsigned char checksum_before_op0[16],
13390 checksum_before_op1[16],
13391 checksum_after_op0[16],
13392 checksum_after_op1[16];
13393 struct md5_ctx ctx;
13396 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13397 md5_init_ctx (&ctx);
13398 fold_checksum_tree (op0, &ctx, ht);
13399 md5_finish_ctx (&ctx, checksum_before_op0);
13402 md5_init_ctx (&ctx);
13403 fold_checksum_tree (op1, &ctx, ht);
13404 md5_finish_ctx (&ctx, checksum_before_op1);
13408 tem = fold_binary (code, type, op0, op1);
13410 tem = build2_stat (code, type, op0, op1 PASS_MEM_STAT);
13412 #ifdef ENABLE_FOLD_CHECKING
13413 md5_init_ctx (&ctx);
13414 fold_checksum_tree (op0, &ctx, ht);
13415 md5_finish_ctx (&ctx, checksum_after_op0);
13418 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13419 fold_check_failed (op0, tem);
13421 md5_init_ctx (&ctx);
13422 fold_checksum_tree (op1, &ctx, ht);
13423 md5_finish_ctx (&ctx, checksum_after_op1);
13426 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13427 fold_check_failed (op1, tem);
13432 /* Fold a ternary tree expression with code CODE of type TYPE with
13433 operands OP0, OP1, and OP2. Return a folded expression if
13434 successful. Otherwise, return a tree expression with code CODE of
13435 type TYPE with operands OP0, OP1, and OP2. */
13438 fold_build3_stat (enum tree_code code, tree type, tree op0, tree op1, tree op2
13442 #ifdef ENABLE_FOLD_CHECKING
13443 unsigned char checksum_before_op0[16],
13444 checksum_before_op1[16],
13445 checksum_before_op2[16],
13446 checksum_after_op0[16],
13447 checksum_after_op1[16],
13448 checksum_after_op2[16];
13449 struct md5_ctx ctx;
13452 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13453 md5_init_ctx (&ctx);
13454 fold_checksum_tree (op0, &ctx, ht);
13455 md5_finish_ctx (&ctx, checksum_before_op0);
13458 md5_init_ctx (&ctx);
13459 fold_checksum_tree (op1, &ctx, ht);
13460 md5_finish_ctx (&ctx, checksum_before_op1);
13463 md5_init_ctx (&ctx);
13464 fold_checksum_tree (op2, &ctx, ht);
13465 md5_finish_ctx (&ctx, checksum_before_op2);
13469 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
13470 tem = fold_ternary (code, type, op0, op1, op2);
13472 tem = build3_stat (code, type, op0, op1, op2 PASS_MEM_STAT);
13474 #ifdef ENABLE_FOLD_CHECKING
13475 md5_init_ctx (&ctx);
13476 fold_checksum_tree (op0, &ctx, ht);
13477 md5_finish_ctx (&ctx, checksum_after_op0);
13480 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13481 fold_check_failed (op0, tem);
13483 md5_init_ctx (&ctx);
13484 fold_checksum_tree (op1, &ctx, ht);
13485 md5_finish_ctx (&ctx, checksum_after_op1);
13488 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13489 fold_check_failed (op1, tem);
13491 md5_init_ctx (&ctx);
13492 fold_checksum_tree (op2, &ctx, ht);
13493 md5_finish_ctx (&ctx, checksum_after_op2);
13496 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
13497 fold_check_failed (op2, tem);
13502 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
13503 arguments in ARGARRAY, and a null static chain.
13504 Return a folded expression if successful. Otherwise, return a CALL_EXPR
13505 of type TYPE from the given operands as constructed by build_call_array. */
13508 fold_build_call_array (tree type, tree fn, int nargs, tree *argarray)
13511 #ifdef ENABLE_FOLD_CHECKING
13512 unsigned char checksum_before_fn[16],
13513 checksum_before_arglist[16],
13514 checksum_after_fn[16],
13515 checksum_after_arglist[16];
13516 struct md5_ctx ctx;
13520 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13521 md5_init_ctx (&ctx);
13522 fold_checksum_tree (fn, &ctx, ht);
13523 md5_finish_ctx (&ctx, checksum_before_fn);
13526 md5_init_ctx (&ctx);
13527 for (i = 0; i < nargs; i++)
13528 fold_checksum_tree (argarray[i], &ctx, ht);
13529 md5_finish_ctx (&ctx, checksum_before_arglist);
13533 tem = fold_builtin_call_array (type, fn, nargs, argarray);
13535 #ifdef ENABLE_FOLD_CHECKING
13536 md5_init_ctx (&ctx);
13537 fold_checksum_tree (fn, &ctx, ht);
13538 md5_finish_ctx (&ctx, checksum_after_fn);
13541 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
13542 fold_check_failed (fn, tem);
13544 md5_init_ctx (&ctx);
13545 for (i = 0; i < nargs; i++)
13546 fold_checksum_tree (argarray[i], &ctx, ht);
13547 md5_finish_ctx (&ctx, checksum_after_arglist);
13550 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
13551 fold_check_failed (NULL_TREE, tem);
13556 /* Perform constant folding and related simplification of initializer
13557 expression EXPR. These behave identically to "fold_buildN" but ignore
13558 potential run-time traps and exceptions that fold must preserve. */
13560 #define START_FOLD_INIT \
13561 int saved_signaling_nans = flag_signaling_nans;\
13562 int saved_trapping_math = flag_trapping_math;\
13563 int saved_rounding_math = flag_rounding_math;\
13564 int saved_trapv = flag_trapv;\
13565 int saved_folding_initializer = folding_initializer;\
13566 flag_signaling_nans = 0;\
13567 flag_trapping_math = 0;\
13568 flag_rounding_math = 0;\
13570 folding_initializer = 1;
13572 #define END_FOLD_INIT \
13573 flag_signaling_nans = saved_signaling_nans;\
13574 flag_trapping_math = saved_trapping_math;\
13575 flag_rounding_math = saved_rounding_math;\
13576 flag_trapv = saved_trapv;\
13577 folding_initializer = saved_folding_initializer;
13580 fold_build1_initializer (enum tree_code code, tree type, tree op)
13585 result = fold_build1 (code, type, op);
13592 fold_build2_initializer (enum tree_code code, tree type, tree op0, tree op1)
13597 result = fold_build2 (code, type, op0, op1);
13604 fold_build3_initializer (enum tree_code code, tree type, tree op0, tree op1,
13610 result = fold_build3 (code, type, op0, op1, op2);
13617 fold_build_call_array_initializer (tree type, tree fn,
13618 int nargs, tree *argarray)
13623 result = fold_build_call_array (type, fn, nargs, argarray);
13629 #undef START_FOLD_INIT
13630 #undef END_FOLD_INIT
13632 /* Determine if first argument is a multiple of second argument. Return 0 if
13633 it is not, or we cannot easily determined it to be.
13635 An example of the sort of thing we care about (at this point; this routine
13636 could surely be made more general, and expanded to do what the *_DIV_EXPR's
13637 fold cases do now) is discovering that
13639 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
13645 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
13647 This code also handles discovering that
13649 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
13651 is a multiple of 8 so we don't have to worry about dealing with a
13652 possible remainder.
13654 Note that we *look* inside a SAVE_EXPR only to determine how it was
13655 calculated; it is not safe for fold to do much of anything else with the
13656 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
13657 at run time. For example, the latter example above *cannot* be implemented
13658 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
13659 evaluation time of the original SAVE_EXPR is not necessarily the same at
13660 the time the new expression is evaluated. The only optimization of this
13661 sort that would be valid is changing
13663 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
13667 SAVE_EXPR (I) * SAVE_EXPR (J)
13669 (where the same SAVE_EXPR (J) is used in the original and the
13670 transformed version). */
13673 multiple_of_p (tree type, tree top, tree bottom)
13675 if (operand_equal_p (top, bottom, 0))
13678 if (TREE_CODE (type) != INTEGER_TYPE)
13681 switch (TREE_CODE (top))
13684 /* Bitwise and provides a power of two multiple. If the mask is
13685 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
13686 if (!integer_pow2p (bottom))
13691 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
13692 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
13696 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
13697 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
13700 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
13704 op1 = TREE_OPERAND (top, 1);
13705 /* const_binop may not detect overflow correctly,
13706 so check for it explicitly here. */
13707 if (TYPE_PRECISION (TREE_TYPE (size_one_node))
13708 > TREE_INT_CST_LOW (op1)
13709 && TREE_INT_CST_HIGH (op1) == 0
13710 && 0 != (t1 = fold_convert (type,
13711 const_binop (LSHIFT_EXPR,
13714 && !TREE_OVERFLOW (t1))
13715 return multiple_of_p (type, t1, bottom);
13720 /* Can't handle conversions from non-integral or wider integral type. */
13721 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
13722 || (TYPE_PRECISION (type)
13723 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
13726 /* .. fall through ... */
13729 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
13732 if (TREE_CODE (bottom) != INTEGER_CST
13733 || integer_zerop (bottom)
13734 || (TYPE_UNSIGNED (type)
13735 && (tree_int_cst_sgn (top) < 0
13736 || tree_int_cst_sgn (bottom) < 0)))
13738 return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
13746 /* Return true if `t' is known to be non-negative. If the return
13747 value is based on the assumption that signed overflow is undefined,
13748 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13749 *STRICT_OVERFLOW_P. */
13752 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
13754 if (t == error_mark_node)
13757 if (TYPE_UNSIGNED (TREE_TYPE (t)))
13760 switch (TREE_CODE (t))
13763 /* Query VRP to see if it has recorded any information about
13764 the range of this object. */
13765 return ssa_name_nonnegative_p (t);
13768 /* We can't return 1 if flag_wrapv is set because
13769 ABS_EXPR<INT_MIN> = INT_MIN. */
13770 if (!INTEGRAL_TYPE_P (TREE_TYPE (t)))
13772 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
13774 *strict_overflow_p = true;
13780 return tree_int_cst_sgn (t) >= 0;
13783 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
13786 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
13788 case POINTER_PLUS_EXPR:
13790 if (FLOAT_TYPE_P (TREE_TYPE (t)))
13791 return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
13793 && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
13794 strict_overflow_p));
13796 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
13797 both unsigned and at least 2 bits shorter than the result. */
13798 if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
13799 && TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
13800 && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR)
13802 tree inner1 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
13803 tree inner2 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0));
13804 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
13805 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
13807 unsigned int prec = MAX (TYPE_PRECISION (inner1),
13808 TYPE_PRECISION (inner2)) + 1;
13809 return prec < TYPE_PRECISION (TREE_TYPE (t));
13815 if (FLOAT_TYPE_P (TREE_TYPE (t)))
13817 /* x * x for floating point x is always non-negative. */
13818 if (operand_equal_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1), 0))
13820 return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
13822 && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
13823 strict_overflow_p));
13826 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
13827 both unsigned and their total bits is shorter than the result. */
13828 if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
13829 && TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
13830 && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR)
13832 tree inner1 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
13833 tree inner2 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0));
13834 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
13835 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
13836 return TYPE_PRECISION (inner1) + TYPE_PRECISION (inner2)
13837 < TYPE_PRECISION (TREE_TYPE (t));
13843 return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
13845 || tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
13846 strict_overflow_p));
13852 case TRUNC_DIV_EXPR:
13853 case CEIL_DIV_EXPR:
13854 case FLOOR_DIV_EXPR:
13855 case ROUND_DIV_EXPR:
13856 return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
13858 && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
13859 strict_overflow_p));
13861 case TRUNC_MOD_EXPR:
13862 case CEIL_MOD_EXPR:
13863 case FLOOR_MOD_EXPR:
13864 case ROUND_MOD_EXPR:
13866 case NON_LVALUE_EXPR:
13868 case FIX_TRUNC_EXPR:
13869 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
13870 strict_overflow_p);
13872 case COMPOUND_EXPR:
13874 case GIMPLE_MODIFY_STMT:
13875 return tree_expr_nonnegative_warnv_p (GENERIC_TREE_OPERAND (t, 1),
13876 strict_overflow_p);
13879 return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
13880 strict_overflow_p);
13883 return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
13885 && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
13886 strict_overflow_p));
13890 tree inner_type = TREE_TYPE (TREE_OPERAND (t, 0));
13891 tree outer_type = TREE_TYPE (t);
13893 if (TREE_CODE (outer_type) == REAL_TYPE)
13895 if (TREE_CODE (inner_type) == REAL_TYPE)
13896 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
13897 strict_overflow_p);
13898 if (TREE_CODE (inner_type) == INTEGER_TYPE)
13900 if (TYPE_UNSIGNED (inner_type))
13902 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
13903 strict_overflow_p);
13906 else if (TREE_CODE (outer_type) == INTEGER_TYPE)
13908 if (TREE_CODE (inner_type) == REAL_TYPE)
13909 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t,0),
13910 strict_overflow_p);
13911 if (TREE_CODE (inner_type) == INTEGER_TYPE)
13912 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
13913 && TYPE_UNSIGNED (inner_type);
13920 tree temp = TARGET_EXPR_SLOT (t);
13921 t = TARGET_EXPR_INITIAL (t);
13923 /* If the initializer is non-void, then it's a normal expression
13924 that will be assigned to the slot. */
13925 if (!VOID_TYPE_P (t))
13926 return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
13928 /* Otherwise, the initializer sets the slot in some way. One common
13929 way is an assignment statement at the end of the initializer. */
13932 if (TREE_CODE (t) == BIND_EXPR)
13933 t = expr_last (BIND_EXPR_BODY (t));
13934 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
13935 || TREE_CODE (t) == TRY_CATCH_EXPR)
13936 t = expr_last (TREE_OPERAND (t, 0));
13937 else if (TREE_CODE (t) == STATEMENT_LIST)
13942 if ((TREE_CODE (t) == MODIFY_EXPR
13943 || TREE_CODE (t) == GIMPLE_MODIFY_STMT)
13944 && GENERIC_TREE_OPERAND (t, 0) == temp)
13945 return tree_expr_nonnegative_warnv_p (GENERIC_TREE_OPERAND (t, 1),
13946 strict_overflow_p);
13953 tree fndecl = get_callee_fndecl (t);
13954 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
13955 switch (DECL_FUNCTION_CODE (fndecl))
13957 CASE_FLT_FN (BUILT_IN_ACOS):
13958 CASE_FLT_FN (BUILT_IN_ACOSH):
13959 CASE_FLT_FN (BUILT_IN_CABS):
13960 CASE_FLT_FN (BUILT_IN_COSH):
13961 CASE_FLT_FN (BUILT_IN_ERFC):
13962 CASE_FLT_FN (BUILT_IN_EXP):
13963 CASE_FLT_FN (BUILT_IN_EXP10):
13964 CASE_FLT_FN (BUILT_IN_EXP2):
13965 CASE_FLT_FN (BUILT_IN_FABS):
13966 CASE_FLT_FN (BUILT_IN_FDIM):
13967 CASE_FLT_FN (BUILT_IN_HYPOT):
13968 CASE_FLT_FN (BUILT_IN_POW10):
13969 CASE_INT_FN (BUILT_IN_FFS):
13970 CASE_INT_FN (BUILT_IN_PARITY):
13971 CASE_INT_FN (BUILT_IN_POPCOUNT):
13972 case BUILT_IN_BSWAP32:
13973 case BUILT_IN_BSWAP64:
13977 CASE_FLT_FN (BUILT_IN_SQRT):
13978 /* sqrt(-0.0) is -0.0. */
13979 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (t))))
13981 return tree_expr_nonnegative_warnv_p (CALL_EXPR_ARG (t, 0),
13982 strict_overflow_p);
13984 CASE_FLT_FN (BUILT_IN_ASINH):
13985 CASE_FLT_FN (BUILT_IN_ATAN):
13986 CASE_FLT_FN (BUILT_IN_ATANH):
13987 CASE_FLT_FN (BUILT_IN_CBRT):
13988 CASE_FLT_FN (BUILT_IN_CEIL):
13989 CASE_FLT_FN (BUILT_IN_ERF):
13990 CASE_FLT_FN (BUILT_IN_EXPM1):
13991 CASE_FLT_FN (BUILT_IN_FLOOR):
13992 CASE_FLT_FN (BUILT_IN_FMOD):
13993 CASE_FLT_FN (BUILT_IN_FREXP):
13994 CASE_FLT_FN (BUILT_IN_LCEIL):
13995 CASE_FLT_FN (BUILT_IN_LDEXP):
13996 CASE_FLT_FN (BUILT_IN_LFLOOR):
13997 CASE_FLT_FN (BUILT_IN_LLCEIL):
13998 CASE_FLT_FN (BUILT_IN_LLFLOOR):
13999 CASE_FLT_FN (BUILT_IN_LLRINT):
14000 CASE_FLT_FN (BUILT_IN_LLROUND):
14001 CASE_FLT_FN (BUILT_IN_LRINT):
14002 CASE_FLT_FN (BUILT_IN_LROUND):
14003 CASE_FLT_FN (BUILT_IN_MODF):
14004 CASE_FLT_FN (BUILT_IN_NEARBYINT):
14005 CASE_FLT_FN (BUILT_IN_RINT):
14006 CASE_FLT_FN (BUILT_IN_ROUND):
14007 CASE_FLT_FN (BUILT_IN_SCALB):
14008 CASE_FLT_FN (BUILT_IN_SCALBLN):
14009 CASE_FLT_FN (BUILT_IN_SCALBN):
14010 CASE_FLT_FN (BUILT_IN_SIGNBIT):
14011 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
14012 CASE_FLT_FN (BUILT_IN_SINH):
14013 CASE_FLT_FN (BUILT_IN_TANH):
14014 CASE_FLT_FN (BUILT_IN_TRUNC):
14015 /* True if the 1st argument is nonnegative. */
14016 return tree_expr_nonnegative_warnv_p (CALL_EXPR_ARG (t, 0),
14017 strict_overflow_p);
14019 CASE_FLT_FN (BUILT_IN_FMAX):
14020 /* True if the 1st OR 2nd arguments are nonnegative. */
14021 return (tree_expr_nonnegative_warnv_p (CALL_EXPR_ARG (t, 0),
14023 || (tree_expr_nonnegative_warnv_p (CALL_EXPR_ARG (t, 1),
14024 strict_overflow_p)));
14026 CASE_FLT_FN (BUILT_IN_FMIN):
14027 /* True if the 1st AND 2nd arguments are nonnegative. */
14028 return (tree_expr_nonnegative_warnv_p (CALL_EXPR_ARG (t, 0),
14030 && (tree_expr_nonnegative_warnv_p (CALL_EXPR_ARG (t, 1),
14031 strict_overflow_p)));
14033 CASE_FLT_FN (BUILT_IN_COPYSIGN):
14034 /* True if the 2nd argument is nonnegative. */
14035 return tree_expr_nonnegative_warnv_p (CALL_EXPR_ARG (t, 1),
14036 strict_overflow_p);
14038 CASE_FLT_FN (BUILT_IN_POWI):
14039 /* True if the 1st argument is nonnegative or the second
14040 argument is an even integer. */
14041 if (TREE_CODE (CALL_EXPR_ARG (t, 1)) == INTEGER_CST)
14043 tree arg1 = CALL_EXPR_ARG (t, 1);
14044 if ((TREE_INT_CST_LOW (arg1) & 1) == 0)
14047 return tree_expr_nonnegative_warnv_p (CALL_EXPR_ARG (t, 0),
14048 strict_overflow_p);
14050 CASE_FLT_FN (BUILT_IN_POW):
14051 /* True if the 1st argument is nonnegative or the second
14052 argument is an even integer valued real. */
14053 if (TREE_CODE (CALL_EXPR_ARG (t, 1)) == REAL_CST)
14058 c = TREE_REAL_CST (CALL_EXPR_ARG (t, 1));
14059 n = real_to_integer (&c);
14062 REAL_VALUE_TYPE cint;
14063 real_from_integer (&cint, VOIDmode, n,
14064 n < 0 ? -1 : 0, 0);
14065 if (real_identical (&c, &cint))
14069 return tree_expr_nonnegative_warnv_p (CALL_EXPR_ARG (t, 0),
14070 strict_overflow_p);
14077 /* ... fall through ... */
14081 tree type = TREE_TYPE (t);
14082 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14083 && truth_value_p (TREE_CODE (t)))
14084 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14085 have a signed:1 type (where the value is -1 and 0). */
14090 /* We don't know sign of `t', so be conservative and return false. */
14094 /* Return true if `t' is known to be non-negative. Handle warnings
14095 about undefined signed overflow. */
14098 tree_expr_nonnegative_p (tree t)
14100 bool ret, strict_overflow_p;
14102 strict_overflow_p = false;
14103 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
14104 if (strict_overflow_p)
14105 fold_overflow_warning (("assuming signed overflow does not occur when "
14106 "determining that expression is always "
14108 WARN_STRICT_OVERFLOW_MISC);
14112 /* Return true when T is an address and is known to be nonzero.
14113 For floating point we further ensure that T is not denormal.
14114 Similar logic is present in nonzero_address in rtlanal.h.
14116 If the return value is based on the assumption that signed overflow
14117 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14118 change *STRICT_OVERFLOW_P. */
14121 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
14123 tree type = TREE_TYPE (t);
14124 bool sub_strict_overflow_p;
14126 /* Doing something useful for floating point would need more work. */
14127 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
14130 switch (TREE_CODE (t))
14133 /* Query VRP to see if it has recorded any information about
14134 the range of this object. */
14135 return ssa_name_nonzero_p (t);
14138 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
14139 strict_overflow_p);
14142 return !integer_zerop (t);
14144 case POINTER_PLUS_EXPR:
14146 if (TYPE_OVERFLOW_UNDEFINED (type))
14148 /* With the presence of negative values it is hard
14149 to say something. */
14150 sub_strict_overflow_p = false;
14151 if (!tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
14152 &sub_strict_overflow_p)
14153 || !tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14154 &sub_strict_overflow_p))
14156 /* One of operands must be positive and the other non-negative. */
14157 /* We don't set *STRICT_OVERFLOW_P here: even if this value
14158 overflows, on a twos-complement machine the sum of two
14159 nonnegative numbers can never be zero. */
14160 return (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
14162 || tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
14163 strict_overflow_p));
14168 if (TYPE_OVERFLOW_UNDEFINED (type))
14170 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
14172 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
14173 strict_overflow_p))
14175 *strict_overflow_p = true;
14183 tree inner_type = TREE_TYPE (TREE_OPERAND (t, 0));
14184 tree outer_type = TREE_TYPE (t);
14186 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
14187 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
14188 strict_overflow_p));
14194 tree base = get_base_address (TREE_OPERAND (t, 0));
14199 /* Weak declarations may link to NULL. */
14200 if (VAR_OR_FUNCTION_DECL_P (base))
14201 return !DECL_WEAK (base);
14203 /* Constants are never weak. */
14204 if (CONSTANT_CLASS_P (base))
14211 sub_strict_overflow_p = false;
14212 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
14213 &sub_strict_overflow_p)
14214 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
14215 &sub_strict_overflow_p))
14217 if (sub_strict_overflow_p)
14218 *strict_overflow_p = true;
14224 sub_strict_overflow_p = false;
14225 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
14226 &sub_strict_overflow_p)
14227 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
14228 &sub_strict_overflow_p))
14230 if (sub_strict_overflow_p)
14231 *strict_overflow_p = true;
14236 sub_strict_overflow_p = false;
14237 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
14238 &sub_strict_overflow_p))
14240 if (sub_strict_overflow_p)
14241 *strict_overflow_p = true;
14243 /* When both operands are nonzero, then MAX must be too. */
14244 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
14245 strict_overflow_p))
14248 /* MAX where operand 0 is positive is positive. */
14249 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
14250 strict_overflow_p);
14252 /* MAX where operand 1 is positive is positive. */
14253 else if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
14254 &sub_strict_overflow_p)
14255 && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14256 &sub_strict_overflow_p))
14258 if (sub_strict_overflow_p)
14259 *strict_overflow_p = true;
14264 case COMPOUND_EXPR:
14266 case GIMPLE_MODIFY_STMT:
14268 return tree_expr_nonzero_warnv_p (GENERIC_TREE_OPERAND (t, 1),
14269 strict_overflow_p);
14272 case NON_LVALUE_EXPR:
14273 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
14274 strict_overflow_p);
14277 return (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
14279 || tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
14280 strict_overflow_p));
14283 return alloca_call_p (t);
14291 /* Return true when T is an address and is known to be nonzero.
14292 Handle warnings about undefined signed overflow. */
14295 tree_expr_nonzero_p (tree t)
14297 bool ret, strict_overflow_p;
14299 strict_overflow_p = false;
14300 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
14301 if (strict_overflow_p)
14302 fold_overflow_warning (("assuming signed overflow does not occur when "
14303 "determining that expression is always "
14305 WARN_STRICT_OVERFLOW_MISC);
14309 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
14310 attempt to fold the expression to a constant without modifying TYPE,
14313 If the expression could be simplified to a constant, then return
14314 the constant. If the expression would not be simplified to a
14315 constant, then return NULL_TREE. */
14318 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
14320 tree tem = fold_binary (code, type, op0, op1);
14321 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
14324 /* Given the components of a unary expression CODE, TYPE and OP0,
14325 attempt to fold the expression to a constant without modifying
14328 If the expression could be simplified to a constant, then return
14329 the constant. If the expression would not be simplified to a
14330 constant, then return NULL_TREE. */
14333 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
14335 tree tem = fold_unary (code, type, op0);
14336 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
14339 /* If EXP represents referencing an element in a constant string
14340 (either via pointer arithmetic or array indexing), return the
14341 tree representing the value accessed, otherwise return NULL. */
14344 fold_read_from_constant_string (tree exp)
14346 if ((TREE_CODE (exp) == INDIRECT_REF
14347 || TREE_CODE (exp) == ARRAY_REF)
14348 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
14350 tree exp1 = TREE_OPERAND (exp, 0);
14354 if (TREE_CODE (exp) == INDIRECT_REF)
14355 string = string_constant (exp1, &index);
14358 tree low_bound = array_ref_low_bound (exp);
14359 index = fold_convert (sizetype, TREE_OPERAND (exp, 1));
14361 /* Optimize the special-case of a zero lower bound.
14363 We convert the low_bound to sizetype to avoid some problems
14364 with constant folding. (E.g. suppose the lower bound is 1,
14365 and its mode is QI. Without the conversion,l (ARRAY
14366 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
14367 +INDEX), which becomes (ARRAY+255+INDEX). Opps!) */
14368 if (! integer_zerop (low_bound))
14369 index = size_diffop (index, fold_convert (sizetype, low_bound));
14375 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
14376 && TREE_CODE (string) == STRING_CST
14377 && TREE_CODE (index) == INTEGER_CST
14378 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
14379 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
14381 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
14382 return build_int_cst_type (TREE_TYPE (exp),
14383 (TREE_STRING_POINTER (string)
14384 [TREE_INT_CST_LOW (index)]));
14389 /* Return the tree for neg (ARG0) when ARG0 is known to be either
14390 an integer constant, real, or fixed-point constant.
14392 TYPE is the type of the result. */
14395 fold_negate_const (tree arg0, tree type)
14397 tree t = NULL_TREE;
14399 switch (TREE_CODE (arg0))
14403 unsigned HOST_WIDE_INT low;
14404 HOST_WIDE_INT high;
14405 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
14406 TREE_INT_CST_HIGH (arg0),
14408 t = force_fit_type_double (type, low, high, 1,
14409 (overflow | TREE_OVERFLOW (arg0))
14410 && !TYPE_UNSIGNED (type));
14415 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
14420 FIXED_VALUE_TYPE f;
14421 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
14422 &(TREE_FIXED_CST (arg0)), NULL,
14423 TYPE_SATURATING (type));
14424 t = build_fixed (type, f);
14425 /* Propagate overflow flags. */
14426 if (overflow_p | TREE_OVERFLOW (arg0))
14428 TREE_OVERFLOW (t) = 1;
14429 TREE_CONSTANT_OVERFLOW (t) = 1;
14431 else if (TREE_CONSTANT_OVERFLOW (arg0))
14432 TREE_CONSTANT_OVERFLOW (t) = 1;
14437 gcc_unreachable ();
14443 /* Return the tree for abs (ARG0) when ARG0 is known to be either
14444 an integer constant or real constant.
14446 TYPE is the type of the result. */
14449 fold_abs_const (tree arg0, tree type)
14451 tree t = NULL_TREE;
14453 switch (TREE_CODE (arg0))
14456 /* If the value is unsigned, then the absolute value is
14457 the same as the ordinary value. */
14458 if (TYPE_UNSIGNED (type))
14460 /* Similarly, if the value is non-negative. */
14461 else if (INT_CST_LT (integer_minus_one_node, arg0))
14463 /* If the value is negative, then the absolute value is
14467 unsigned HOST_WIDE_INT low;
14468 HOST_WIDE_INT high;
14469 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
14470 TREE_INT_CST_HIGH (arg0),
14472 t = force_fit_type_double (type, low, high, -1,
14473 overflow | TREE_OVERFLOW (arg0));
14478 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
14479 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
14485 gcc_unreachable ();
14491 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
14492 constant. TYPE is the type of the result. */
14495 fold_not_const (tree arg0, tree type)
14497 tree t = NULL_TREE;
14499 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
14501 t = force_fit_type_double (type, ~TREE_INT_CST_LOW (arg0),
14502 ~TREE_INT_CST_HIGH (arg0), 0,
14503 TREE_OVERFLOW (arg0));
14508 /* Given CODE, a relational operator, the target type, TYPE and two
14509 constant operands OP0 and OP1, return the result of the
14510 relational operation. If the result is not a compile time
14511 constant, then return NULL_TREE. */
14514 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
14516 int result, invert;
14518 /* From here on, the only cases we handle are when the result is
14519 known to be a constant. */
14521 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
14523 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
14524 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
14526 /* Handle the cases where either operand is a NaN. */
14527 if (real_isnan (c0) || real_isnan (c1))
14537 case UNORDERED_EXPR:
14551 if (flag_trapping_math)
14557 gcc_unreachable ();
14560 return constant_boolean_node (result, type);
14563 return constant_boolean_node (real_compare (code, c0, c1), type);
14566 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
14568 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
14569 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
14570 return constant_boolean_node (fixed_compare (code, c0, c1), type);
14573 /* Handle equality/inequality of complex constants. */
14574 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
14576 tree rcond = fold_relational_const (code, type,
14577 TREE_REALPART (op0),
14578 TREE_REALPART (op1));
14579 tree icond = fold_relational_const (code, type,
14580 TREE_IMAGPART (op0),
14581 TREE_IMAGPART (op1));
14582 if (code == EQ_EXPR)
14583 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
14584 else if (code == NE_EXPR)
14585 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
14590 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
14592 To compute GT, swap the arguments and do LT.
14593 To compute GE, do LT and invert the result.
14594 To compute LE, swap the arguments, do LT and invert the result.
14595 To compute NE, do EQ and invert the result.
14597 Therefore, the code below must handle only EQ and LT. */
14599 if (code == LE_EXPR || code == GT_EXPR)
14604 code = swap_tree_comparison (code);
14607 /* Note that it is safe to invert for real values here because we
14608 have already handled the one case that it matters. */
14611 if (code == NE_EXPR || code == GE_EXPR)
14614 code = invert_tree_comparison (code, false);
14617 /* Compute a result for LT or EQ if args permit;
14618 Otherwise return T. */
14619 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
14621 if (code == EQ_EXPR)
14622 result = tree_int_cst_equal (op0, op1);
14623 else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
14624 result = INT_CST_LT_UNSIGNED (op0, op1);
14626 result = INT_CST_LT (op0, op1);
14633 return constant_boolean_node (result, type);
14636 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
14637 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
14641 fold_build_cleanup_point_expr (tree type, tree expr)
14643 /* If the expression does not have side effects then we don't have to wrap
14644 it with a cleanup point expression. */
14645 if (!TREE_SIDE_EFFECTS (expr))
14648 /* If the expression is a return, check to see if the expression inside the
14649 return has no side effects or the right hand side of the modify expression
14650 inside the return. If either don't have side effects set we don't need to
14651 wrap the expression in a cleanup point expression. Note we don't check the
14652 left hand side of the modify because it should always be a return decl. */
14653 if (TREE_CODE (expr) == RETURN_EXPR)
14655 tree op = TREE_OPERAND (expr, 0);
14656 if (!op || !TREE_SIDE_EFFECTS (op))
14658 op = TREE_OPERAND (op, 1);
14659 if (!TREE_SIDE_EFFECTS (op))
14663 return build1 (CLEANUP_POINT_EXPR, type, expr);
14666 /* Given a pointer value OP0 and a type TYPE, return a simplified version
14667 of an indirection through OP0, or NULL_TREE if no simplification is
14671 fold_indirect_ref_1 (tree type, tree op0)
14677 subtype = TREE_TYPE (sub);
14678 if (!POINTER_TYPE_P (subtype))
14681 if (TREE_CODE (sub) == ADDR_EXPR)
14683 tree op = TREE_OPERAND (sub, 0);
14684 tree optype = TREE_TYPE (op);
14685 /* *&CONST_DECL -> to the value of the const decl. */
14686 if (TREE_CODE (op) == CONST_DECL)
14687 return DECL_INITIAL (op);
14688 /* *&p => p; make sure to handle *&"str"[cst] here. */
14689 if (type == optype)
14691 tree fop = fold_read_from_constant_string (op);
14697 /* *(foo *)&fooarray => fooarray[0] */
14698 else if (TREE_CODE (optype) == ARRAY_TYPE
14699 && type == TREE_TYPE (optype))
14701 tree type_domain = TYPE_DOMAIN (optype);
14702 tree min_val = size_zero_node;
14703 if (type_domain && TYPE_MIN_VALUE (type_domain))
14704 min_val = TYPE_MIN_VALUE (type_domain);
14705 return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
14707 /* *(foo *)&complexfoo => __real__ complexfoo */
14708 else if (TREE_CODE (optype) == COMPLEX_TYPE
14709 && type == TREE_TYPE (optype))
14710 return fold_build1 (REALPART_EXPR, type, op);
14711 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
14712 else if (TREE_CODE (optype) == VECTOR_TYPE
14713 && type == TREE_TYPE (optype))
14715 tree part_width = TYPE_SIZE (type);
14716 tree index = bitsize_int (0);
14717 return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
14721 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
14722 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
14723 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
14725 tree op00 = TREE_OPERAND (sub, 0);
14726 tree op01 = TREE_OPERAND (sub, 1);
14730 op00type = TREE_TYPE (op00);
14731 if (TREE_CODE (op00) == ADDR_EXPR
14732 && TREE_CODE (TREE_TYPE (op00type)) == COMPLEX_TYPE
14733 && type == TREE_TYPE (TREE_TYPE (op00type)))
14735 tree size = TYPE_SIZE_UNIT (type);
14736 if (tree_int_cst_equal (size, op01))
14737 return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (op00, 0));
14741 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
14742 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
14743 && type == TREE_TYPE (TREE_TYPE (subtype)))
14746 tree min_val = size_zero_node;
14747 sub = build_fold_indirect_ref (sub);
14748 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
14749 if (type_domain && TYPE_MIN_VALUE (type_domain))
14750 min_val = TYPE_MIN_VALUE (type_domain);
14751 return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
14757 /* Builds an expression for an indirection through T, simplifying some
14761 build_fold_indirect_ref (tree t)
14763 tree type = TREE_TYPE (TREE_TYPE (t));
14764 tree sub = fold_indirect_ref_1 (type, t);
14769 return build1 (INDIRECT_REF, type, t);
14772 /* Given an INDIRECT_REF T, return either T or a simplified version. */
14775 fold_indirect_ref (tree t)
14777 tree sub = fold_indirect_ref_1 (TREE_TYPE (t), TREE_OPERAND (t, 0));
14785 /* Strip non-trapping, non-side-effecting tree nodes from an expression
14786 whose result is ignored. The type of the returned tree need not be
14787 the same as the original expression. */
14790 fold_ignored_result (tree t)
14792 if (!TREE_SIDE_EFFECTS (t))
14793 return integer_zero_node;
14796 switch (TREE_CODE_CLASS (TREE_CODE (t)))
14799 t = TREE_OPERAND (t, 0);
14803 case tcc_comparison:
14804 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14805 t = TREE_OPERAND (t, 0);
14806 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
14807 t = TREE_OPERAND (t, 1);
14812 case tcc_expression:
14813 switch (TREE_CODE (t))
14815 case COMPOUND_EXPR:
14816 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
14818 t = TREE_OPERAND (t, 0);
14822 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
14823 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
14825 t = TREE_OPERAND (t, 0);
14838 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
14839 This can only be applied to objects of a sizetype. */
14842 round_up (tree value, int divisor)
14844 tree div = NULL_TREE;
14846 gcc_assert (divisor > 0);
14850 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14851 have to do anything. Only do this when we are not given a const,
14852 because in that case, this check is more expensive than just
14854 if (TREE_CODE (value) != INTEGER_CST)
14856 div = build_int_cst (TREE_TYPE (value), divisor);
14858 if (multiple_of_p (TREE_TYPE (value), value, div))
14862 /* If divisor is a power of two, simplify this to bit manipulation. */
14863 if (divisor == (divisor & -divisor))
14865 if (TREE_CODE (value) == INTEGER_CST)
14867 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (value);
14868 unsigned HOST_WIDE_INT high;
14871 if ((low & (divisor - 1)) == 0)
14874 overflow_p = TREE_OVERFLOW (value);
14875 high = TREE_INT_CST_HIGH (value);
14876 low &= ~(divisor - 1);
14885 return force_fit_type_double (TREE_TYPE (value), low, high,
14892 t = build_int_cst (TREE_TYPE (value), divisor - 1);
14893 value = size_binop (PLUS_EXPR, value, t);
14894 t = build_int_cst (TREE_TYPE (value), -divisor);
14895 value = size_binop (BIT_AND_EXPR, value, t);
14901 div = build_int_cst (TREE_TYPE (value), divisor);
14902 value = size_binop (CEIL_DIV_EXPR, value, div);
14903 value = size_binop (MULT_EXPR, value, div);
14909 /* Likewise, but round down. */
14912 round_down (tree value, int divisor)
14914 tree div = NULL_TREE;
14916 gcc_assert (divisor > 0);
14920 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
14921 have to do anything. Only do this when we are not given a const,
14922 because in that case, this check is more expensive than just
14924 if (TREE_CODE (value) != INTEGER_CST)
14926 div = build_int_cst (TREE_TYPE (value), divisor);
14928 if (multiple_of_p (TREE_TYPE (value), value, div))
14932 /* If divisor is a power of two, simplify this to bit manipulation. */
14933 if (divisor == (divisor & -divisor))
14937 t = build_int_cst (TREE_TYPE (value), -divisor);
14938 value = size_binop (BIT_AND_EXPR, value, t);
14943 div = build_int_cst (TREE_TYPE (value), divisor);
14944 value = size_binop (FLOOR_DIV_EXPR, value, div);
14945 value = size_binop (MULT_EXPR, value, div);
14951 /* Returns the pointer to the base of the object addressed by EXP and
14952 extracts the information about the offset of the access, storing it
14953 to PBITPOS and POFFSET. */
14956 split_address_to_core_and_offset (tree exp,
14957 HOST_WIDE_INT *pbitpos, tree *poffset)
14960 enum machine_mode mode;
14961 int unsignedp, volatilep;
14962 HOST_WIDE_INT bitsize;
14964 if (TREE_CODE (exp) == ADDR_EXPR)
14966 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
14967 poffset, &mode, &unsignedp, &volatilep,
14969 core = fold_addr_expr (core);
14975 *poffset = NULL_TREE;
14981 /* Returns true if addresses of E1 and E2 differ by a constant, false
14982 otherwise. If they do, E1 - E2 is stored in *DIFF. */
14985 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
14988 HOST_WIDE_INT bitpos1, bitpos2;
14989 tree toffset1, toffset2, tdiff, type;
14991 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
14992 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
14994 if (bitpos1 % BITS_PER_UNIT != 0
14995 || bitpos2 % BITS_PER_UNIT != 0
14996 || !operand_equal_p (core1, core2, 0))
14999 if (toffset1 && toffset2)
15001 type = TREE_TYPE (toffset1);
15002 if (type != TREE_TYPE (toffset2))
15003 toffset2 = fold_convert (type, toffset2);
15005 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
15006 if (!cst_and_fits_in_hwi (tdiff))
15009 *diff = int_cst_value (tdiff);
15011 else if (toffset1 || toffset2)
15013 /* If only one of the offsets is non-constant, the difference cannot
15020 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
15024 /* Simplify the floating point expression EXP when the sign of the
15025 result is not significant. Return NULL_TREE if no simplification
15029 fold_strip_sign_ops (tree exp)
15033 switch (TREE_CODE (exp))
15037 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15038 return arg0 ? arg0 : TREE_OPERAND (exp, 0);
15042 if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
15044 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15045 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15046 if (arg0 != NULL_TREE || arg1 != NULL_TREE)
15047 return fold_build2 (TREE_CODE (exp), TREE_TYPE (exp),
15048 arg0 ? arg0 : TREE_OPERAND (exp, 0),
15049 arg1 ? arg1 : TREE_OPERAND (exp, 1));
15052 case COMPOUND_EXPR:
15053 arg0 = TREE_OPERAND (exp, 0);
15054 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15056 return fold_build2 (COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
15060 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15061 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
15063 return fold_build3 (COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
15064 arg0 ? arg0 : TREE_OPERAND (exp, 1),
15065 arg1 ? arg1 : TREE_OPERAND (exp, 2));
15070 const enum built_in_function fcode = builtin_mathfn_code (exp);
15073 CASE_FLT_FN (BUILT_IN_COPYSIGN):
15074 /* Strip copysign function call, return the 1st argument. */
15075 arg0 = CALL_EXPR_ARG (exp, 0);
15076 arg1 = CALL_EXPR_ARG (exp, 1);
15077 return omit_one_operand (TREE_TYPE (exp), arg0, arg1);
15080 /* Strip sign ops from the argument of "odd" math functions. */
15081 if (negate_mathfn_p (fcode))
15083 arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
15085 return build_call_expr (get_callee_fndecl (exp), 1, arg0);