1 /* Fold a constant sub-tree into a single node for C-compiler
2 Copyright (C) 1987, 1988, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999,
3 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /*@@ This file should be rewritten to use an arbitrary precision
23 @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
24 @@ Perhaps the routines could also be used for bc/dc, and made a lib.
25 @@ The routines that translate from the ap rep should
26 @@ warn if precision et. al. is lost.
27 @@ This would also make life easier when this technology is used
28 @@ for cross-compilers. */
30 /* The entry points in this file are fold, size_int_wide, size_binop
31 and force_fit_type_double.
33 fold takes a tree as argument and returns a simplified tree.
35 size_binop takes a tree code for an arithmetic operation
36 and two operands that are trees, and produces a tree for the
37 result, assuming the type comes from `sizetype'.
39 size_int takes an integer value, and creates a tree constant
40 with type from `sizetype'.
42 force_fit_type_double takes a constant, an overflowable flag and a
43 prior overflow indicator. It forces the value to fit the type and
46 Note: Since the folders get called on non-gimple code as well as
47 gimple code, we need to handle GIMPLE tuples as well as their
48 corresponding tree equivalents. */
52 #include "coretypes.h"
57 #include "fixed-value.h"
66 #include "langhooks.h"
70 /* Nonzero if we are folding constants inside an initializer; zero
72 int folding_initializer = 0;
74 /* The following constants represent a bit based encoding of GCC's
75 comparison operators. This encoding simplifies transformations
76 on relational comparison operators, such as AND and OR. */
77 enum comparison_code {
96 static void encode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT, HOST_WIDE_INT);
97 static void decode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT *, HOST_WIDE_INT *);
98 static bool negate_mathfn_p (enum built_in_function);
99 static bool negate_expr_p (tree);
100 static tree negate_expr (tree);
101 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
102 static tree associate_trees (tree, tree, enum tree_code, tree);
103 static tree const_binop (enum tree_code, tree, tree, int);
104 static enum comparison_code comparison_to_compcode (enum tree_code);
105 static enum tree_code compcode_to_comparison (enum comparison_code);
106 static int operand_equal_for_comparison_p (tree, tree, tree);
107 static int twoval_comparison_p (tree, tree *, tree *, int *);
108 static tree eval_subst (tree, tree, tree, tree, tree);
109 static tree pedantic_omit_one_operand (tree, tree, tree);
110 static tree distribute_bit_expr (enum tree_code, tree, tree, tree);
111 static tree make_bit_field_ref (tree, tree, HOST_WIDE_INT, HOST_WIDE_INT, int);
112 static tree optimize_bit_field_compare (enum tree_code, tree, tree, tree);
113 static tree decode_field_reference (tree, HOST_WIDE_INT *, HOST_WIDE_INT *,
114 enum machine_mode *, int *, int *,
116 static int all_ones_mask_p (const_tree, int);
117 static tree sign_bit_p (tree, const_tree);
118 static int simple_operand_p (const_tree);
119 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
120 static tree range_predecessor (tree);
121 static tree range_successor (tree);
122 static tree make_range (tree, int *, tree *, tree *, bool *);
123 static tree build_range_check (tree, tree, int, tree, tree);
124 static int merge_ranges (int *, tree *, tree *, int, tree, tree, int, tree,
126 static tree fold_range_test (enum tree_code, tree, tree, tree);
127 static tree fold_cond_expr_with_comparison (tree, tree, tree, tree);
128 static tree unextend (tree, int, int, tree);
129 static tree fold_truthop (enum tree_code, tree, tree, tree);
130 static tree optimize_minmax_comparison (enum tree_code, tree, tree, tree);
131 static tree extract_muldiv (tree, tree, enum tree_code, tree, bool *);
132 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree, bool *);
133 static tree fold_binary_op_with_conditional_arg (enum tree_code, tree,
136 static tree fold_mathfn_compare (enum built_in_function, enum tree_code,
138 static tree fold_inf_compare (enum tree_code, tree, tree, tree);
139 static tree fold_div_compare (enum tree_code, tree, tree, tree);
140 static bool reorder_operands_p (const_tree, const_tree);
141 static tree fold_negate_const (tree, tree);
142 static tree fold_not_const (tree, tree);
143 static tree fold_relational_const (enum tree_code, tree, tree, tree);
146 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
147 overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
148 and SUM1. Then this yields nonzero if overflow occurred during the
151 Overflow occurs if A and B have the same sign, but A and SUM differ in
152 sign. Use `^' to test whether signs differ, and `< 0' to isolate the
154 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
156 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
157 We do that by representing the two-word integer in 4 words, with only
158 HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
159 number. The value of the word is LOWPART + HIGHPART * BASE. */
162 ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
163 #define HIGHPART(x) \
164 ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
165 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
167 /* Unpack a two-word integer into 4 words.
168 LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
169 WORDS points to the array of HOST_WIDE_INTs. */
172 encode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
174 words[0] = LOWPART (low);
175 words[1] = HIGHPART (low);
176 words[2] = LOWPART (hi);
177 words[3] = HIGHPART (hi);
180 /* Pack an array of 4 words into a two-word integer.
181 WORDS points to the array of words.
182 The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */
185 decode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT *low,
188 *low = words[0] + words[1] * BASE;
189 *hi = words[2] + words[3] * BASE;
192 /* Force the double-word integer L1, H1 to be within the range of the
193 integer type TYPE. Stores the properly truncated and sign-extended
194 double-word integer in *LV, *HV. Returns true if the operation
195 overflows, that is, argument and result are different. */
198 fit_double_type (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
199 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, const_tree type)
201 unsigned HOST_WIDE_INT low0 = l1;
202 HOST_WIDE_INT high0 = h1;
204 int sign_extended_type;
206 if (POINTER_TYPE_P (type)
207 || TREE_CODE (type) == OFFSET_TYPE)
210 prec = TYPE_PRECISION (type);
212 /* Size types *are* sign extended. */
213 sign_extended_type = (!TYPE_UNSIGNED (type)
214 || (TREE_CODE (type) == INTEGER_TYPE
215 && TYPE_IS_SIZETYPE (type)));
217 /* First clear all bits that are beyond the type's precision. */
218 if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
220 else if (prec > HOST_BITS_PER_WIDE_INT)
221 h1 &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
225 if (prec < HOST_BITS_PER_WIDE_INT)
226 l1 &= ~((HOST_WIDE_INT) (-1) << prec);
229 /* Then do sign extension if necessary. */
230 if (!sign_extended_type)
231 /* No sign extension */;
232 else if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
233 /* Correct width already. */;
234 else if (prec > HOST_BITS_PER_WIDE_INT)
236 /* Sign extend top half? */
237 if (h1 & ((unsigned HOST_WIDE_INT)1
238 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
239 h1 |= (HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT);
241 else if (prec == HOST_BITS_PER_WIDE_INT)
243 if ((HOST_WIDE_INT)l1 < 0)
248 /* Sign extend bottom half? */
249 if (l1 & ((unsigned HOST_WIDE_INT)1 << (prec - 1)))
252 l1 |= (HOST_WIDE_INT)(-1) << prec;
259 /* If the value didn't fit, signal overflow. */
260 return l1 != low0 || h1 != high0;
263 /* We force the double-int HIGH:LOW to the range of the type TYPE by
264 sign or zero extending it.
265 OVERFLOWABLE indicates if we are interested
266 in overflow of the value, when >0 we are only interested in signed
267 overflow, for <0 we are interested in any overflow. OVERFLOWED
268 indicates whether overflow has already occurred. CONST_OVERFLOWED
269 indicates whether constant overflow has already occurred. We force
270 T's value to be within range of T's type (by setting to 0 or 1 all
271 the bits outside the type's range). We set TREE_OVERFLOWED if,
272 OVERFLOWED is nonzero,
273 or OVERFLOWABLE is >0 and signed overflow occurs
274 or OVERFLOWABLE is <0 and any overflow occurs
275 We return a new tree node for the extended double-int. The node
276 is shared if no overflow flags are set. */
279 force_fit_type_double (tree type, unsigned HOST_WIDE_INT low,
280 HOST_WIDE_INT high, int overflowable,
283 int sign_extended_type;
286 /* Size types *are* sign extended. */
287 sign_extended_type = (!TYPE_UNSIGNED (type)
288 || (TREE_CODE (type) == INTEGER_TYPE
289 && TYPE_IS_SIZETYPE (type)));
291 overflow = fit_double_type (low, high, &low, &high, type);
293 /* If we need to set overflow flags, return a new unshared node. */
294 if (overflowed || overflow)
298 || (overflowable > 0 && sign_extended_type))
300 tree t = make_node (INTEGER_CST);
301 TREE_INT_CST_LOW (t) = low;
302 TREE_INT_CST_HIGH (t) = high;
303 TREE_TYPE (t) = type;
304 TREE_OVERFLOW (t) = 1;
309 /* Else build a shared node. */
310 return build_int_cst_wide (type, low, high);
313 /* Add two doubleword integers with doubleword result.
314 Return nonzero if the operation overflows according to UNSIGNED_P.
315 Each argument is given as two `HOST_WIDE_INT' pieces.
316 One argument is L1 and H1; the other, L2 and H2.
317 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
320 add_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
321 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
322 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
325 unsigned HOST_WIDE_INT l;
329 h = h1 + h2 + (l < l1);
335 return (unsigned HOST_WIDE_INT) h < (unsigned HOST_WIDE_INT) h1;
337 return OVERFLOW_SUM_SIGN (h1, h2, h);
340 /* Negate a doubleword integer with doubleword result.
341 Return nonzero if the operation overflows, assuming it's signed.
342 The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
343 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
346 neg_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
347 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
353 return (*hv & h1) < 0;
363 /* Multiply two doubleword integers with doubleword result.
364 Return nonzero if the operation overflows according to UNSIGNED_P.
365 Each argument is given as two `HOST_WIDE_INT' pieces.
366 One argument is L1 and H1; the other, L2 and H2.
367 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
370 mul_double_with_sign (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
371 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
372 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
375 HOST_WIDE_INT arg1[4];
376 HOST_WIDE_INT arg2[4];
377 HOST_WIDE_INT prod[4 * 2];
378 unsigned HOST_WIDE_INT carry;
380 unsigned HOST_WIDE_INT toplow, neglow;
381 HOST_WIDE_INT tophigh, neghigh;
383 encode (arg1, l1, h1);
384 encode (arg2, l2, h2);
386 memset (prod, 0, sizeof prod);
388 for (i = 0; i < 4; i++)
391 for (j = 0; j < 4; j++)
394 /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */
395 carry += arg1[i] * arg2[j];
396 /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */
398 prod[k] = LOWPART (carry);
399 carry = HIGHPART (carry);
404 decode (prod, lv, hv);
405 decode (prod + 4, &toplow, &tophigh);
407 /* Unsigned overflow is immediate. */
409 return (toplow | tophigh) != 0;
411 /* Check for signed overflow by calculating the signed representation of the
412 top half of the result; it should agree with the low half's sign bit. */
415 neg_double (l2, h2, &neglow, &neghigh);
416 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
420 neg_double (l1, h1, &neglow, &neghigh);
421 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
423 return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
426 /* Shift the doubleword integer in L1, H1 left by COUNT places
427 keeping only PREC bits of result.
428 Shift right if COUNT is negative.
429 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
430 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
433 lshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
434 HOST_WIDE_INT count, unsigned int prec,
435 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, int arith)
437 unsigned HOST_WIDE_INT signmask;
441 rshift_double (l1, h1, -count, prec, lv, hv, arith);
445 if (SHIFT_COUNT_TRUNCATED)
448 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
450 /* Shifting by the host word size is undefined according to the
451 ANSI standard, so we must handle this as a special case. */
455 else if (count >= HOST_BITS_PER_WIDE_INT)
457 *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
462 *hv = (((unsigned HOST_WIDE_INT) h1 << count)
463 | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
467 /* Sign extend all bits that are beyond the precision. */
469 signmask = -((prec > HOST_BITS_PER_WIDE_INT
470 ? ((unsigned HOST_WIDE_INT) *hv
471 >> (prec - HOST_BITS_PER_WIDE_INT - 1))
472 : (*lv >> (prec - 1))) & 1);
474 if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
476 else if (prec >= HOST_BITS_PER_WIDE_INT)
478 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
479 *hv |= signmask << (prec - HOST_BITS_PER_WIDE_INT);
484 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
485 *lv |= signmask << prec;
489 /* Shift the doubleword integer in L1, H1 right by COUNT places
490 keeping only PREC bits of result. COUNT must be positive.
491 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
492 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
495 rshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
496 HOST_WIDE_INT count, unsigned int prec,
497 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
500 unsigned HOST_WIDE_INT signmask;
503 ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
506 if (SHIFT_COUNT_TRUNCATED)
509 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
511 /* Shifting by the host word size is undefined according to the
512 ANSI standard, so we must handle this as a special case. */
516 else if (count >= HOST_BITS_PER_WIDE_INT)
519 *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
523 *hv = (unsigned HOST_WIDE_INT) h1 >> count;
525 | ((unsigned HOST_WIDE_INT) h1 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
528 /* Zero / sign extend all bits that are beyond the precision. */
530 if (count >= (HOST_WIDE_INT)prec)
535 else if ((prec - count) >= 2 * HOST_BITS_PER_WIDE_INT)
537 else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
539 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - count - HOST_BITS_PER_WIDE_INT));
540 *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
545 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << (prec - count));
546 *lv |= signmask << (prec - count);
550 /* Rotate the doubleword integer in L1, H1 left by COUNT places
551 keeping only PREC bits of result.
552 Rotate right if COUNT is negative.
553 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
556 lrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
557 HOST_WIDE_INT count, unsigned int prec,
558 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
560 unsigned HOST_WIDE_INT s1l, s2l;
561 HOST_WIDE_INT s1h, s2h;
567 lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
568 rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
573 /* Rotate the doubleword integer in L1, H1 left by COUNT places
574 keeping only PREC bits of result. COUNT must be positive.
575 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
578 rrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
579 HOST_WIDE_INT count, unsigned int prec,
580 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
582 unsigned HOST_WIDE_INT s1l, s2l;
583 HOST_WIDE_INT s1h, s2h;
589 rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
590 lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
595 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
596 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
597 CODE is a tree code for a kind of division, one of
598 TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
600 It controls how the quotient is rounded to an integer.
601 Return nonzero if the operation overflows.
602 UNS nonzero says do unsigned division. */
605 div_and_round_double (enum tree_code code, int uns,
606 unsigned HOST_WIDE_INT lnum_orig, /* num == numerator == dividend */
607 HOST_WIDE_INT hnum_orig,
608 unsigned HOST_WIDE_INT lden_orig, /* den == denominator == divisor */
609 HOST_WIDE_INT hden_orig,
610 unsigned HOST_WIDE_INT *lquo,
611 HOST_WIDE_INT *hquo, unsigned HOST_WIDE_INT *lrem,
615 HOST_WIDE_INT num[4 + 1]; /* extra element for scaling. */
616 HOST_WIDE_INT den[4], quo[4];
618 unsigned HOST_WIDE_INT work;
619 unsigned HOST_WIDE_INT carry = 0;
620 unsigned HOST_WIDE_INT lnum = lnum_orig;
621 HOST_WIDE_INT hnum = hnum_orig;
622 unsigned HOST_WIDE_INT lden = lden_orig;
623 HOST_WIDE_INT hden = hden_orig;
626 if (hden == 0 && lden == 0)
627 overflow = 1, lden = 1;
629 /* Calculate quotient sign and convert operands to unsigned. */
635 /* (minimum integer) / (-1) is the only overflow case. */
636 if (neg_double (lnum, hnum, &lnum, &hnum)
637 && ((HOST_WIDE_INT) lden & hden) == -1)
643 neg_double (lden, hden, &lden, &hden);
647 if (hnum == 0 && hden == 0)
648 { /* single precision */
650 /* This unsigned division rounds toward zero. */
656 { /* trivial case: dividend < divisor */
657 /* hden != 0 already checked. */
664 memset (quo, 0, sizeof quo);
666 memset (num, 0, sizeof num); /* to zero 9th element */
667 memset (den, 0, sizeof den);
669 encode (num, lnum, hnum);
670 encode (den, lden, hden);
672 /* Special code for when the divisor < BASE. */
673 if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
675 /* hnum != 0 already checked. */
676 for (i = 4 - 1; i >= 0; i--)
678 work = num[i] + carry * BASE;
679 quo[i] = work / lden;
685 /* Full double precision division,
686 with thanks to Don Knuth's "Seminumerical Algorithms". */
687 int num_hi_sig, den_hi_sig;
688 unsigned HOST_WIDE_INT quo_est, scale;
690 /* Find the highest nonzero divisor digit. */
691 for (i = 4 - 1;; i--)
698 /* Insure that the first digit of the divisor is at least BASE/2.
699 This is required by the quotient digit estimation algorithm. */
701 scale = BASE / (den[den_hi_sig] + 1);
703 { /* scale divisor and dividend */
705 for (i = 0; i <= 4 - 1; i++)
707 work = (num[i] * scale) + carry;
708 num[i] = LOWPART (work);
709 carry = HIGHPART (work);
714 for (i = 0; i <= 4 - 1; i++)
716 work = (den[i] * scale) + carry;
717 den[i] = LOWPART (work);
718 carry = HIGHPART (work);
719 if (den[i] != 0) den_hi_sig = i;
726 for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
728 /* Guess the next quotient digit, quo_est, by dividing the first
729 two remaining dividend digits by the high order quotient digit.
730 quo_est is never low and is at most 2 high. */
731 unsigned HOST_WIDE_INT tmp;
733 num_hi_sig = i + den_hi_sig + 1;
734 work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
735 if (num[num_hi_sig] != den[den_hi_sig])
736 quo_est = work / den[den_hi_sig];
740 /* Refine quo_est so it's usually correct, and at most one high. */
741 tmp = work - quo_est * den[den_hi_sig];
743 && (den[den_hi_sig - 1] * quo_est
744 > (tmp * BASE + num[num_hi_sig - 2])))
747 /* Try QUO_EST as the quotient digit, by multiplying the
748 divisor by QUO_EST and subtracting from the remaining dividend.
749 Keep in mind that QUO_EST is the I - 1st digit. */
752 for (j = 0; j <= den_hi_sig; j++)
754 work = quo_est * den[j] + carry;
755 carry = HIGHPART (work);
756 work = num[i + j] - LOWPART (work);
757 num[i + j] = LOWPART (work);
758 carry += HIGHPART (work) != 0;
761 /* If quo_est was high by one, then num[i] went negative and
762 we need to correct things. */
763 if (num[num_hi_sig] < (HOST_WIDE_INT) carry)
766 carry = 0; /* add divisor back in */
767 for (j = 0; j <= den_hi_sig; j++)
769 work = num[i + j] + den[j] + carry;
770 carry = HIGHPART (work);
771 num[i + j] = LOWPART (work);
774 num [num_hi_sig] += carry;
777 /* Store the quotient digit. */
782 decode (quo, lquo, hquo);
785 /* If result is negative, make it so. */
787 neg_double (*lquo, *hquo, lquo, hquo);
789 /* Compute trial remainder: rem = num - (quo * den) */
790 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
791 neg_double (*lrem, *hrem, lrem, hrem);
792 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
797 case TRUNC_MOD_EXPR: /* round toward zero */
798 case EXACT_DIV_EXPR: /* for this one, it shouldn't matter */
802 case FLOOR_MOD_EXPR: /* round toward negative infinity */
803 if (quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio < 0 && rem != 0 */
806 add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1,
814 case CEIL_MOD_EXPR: /* round toward positive infinity */
815 if (!quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio > 0 && rem != 0 */
817 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
825 case ROUND_MOD_EXPR: /* round to closest integer */
827 unsigned HOST_WIDE_INT labs_rem = *lrem;
828 HOST_WIDE_INT habs_rem = *hrem;
829 unsigned HOST_WIDE_INT labs_den = lden, ltwice;
830 HOST_WIDE_INT habs_den = hden, htwice;
832 /* Get absolute values. */
834 neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
836 neg_double (lden, hden, &labs_den, &habs_den);
838 /* If (2 * abs (lrem) >= abs (lden)), adjust the quotient. */
839 mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
840 labs_rem, habs_rem, <wice, &htwice);
842 if (((unsigned HOST_WIDE_INT) habs_den
843 < (unsigned HOST_WIDE_INT) htwice)
844 || (((unsigned HOST_WIDE_INT) habs_den
845 == (unsigned HOST_WIDE_INT) htwice)
846 && (labs_den <= ltwice)))
850 add_double (*lquo, *hquo,
851 (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
854 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
866 /* Compute true remainder: rem = num - (quo * den) */
867 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
868 neg_double (*lrem, *hrem, lrem, hrem);
869 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
873 /* If ARG2 divides ARG1 with zero remainder, carries out the division
874 of type CODE and returns the quotient.
875 Otherwise returns NULL_TREE. */
878 div_if_zero_remainder (enum tree_code code, const_tree arg1, const_tree arg2)
880 unsigned HOST_WIDE_INT int1l, int2l;
881 HOST_WIDE_INT int1h, int2h;
882 unsigned HOST_WIDE_INT quol, reml;
883 HOST_WIDE_INT quoh, remh;
884 tree type = TREE_TYPE (arg1);
885 int uns = TYPE_UNSIGNED (type);
887 int1l = TREE_INT_CST_LOW (arg1);
888 int1h = TREE_INT_CST_HIGH (arg1);
889 /* &obj[0] + -128 really should be compiled as &obj[-8] rather than
890 &obj[some_exotic_number]. */
891 if (POINTER_TYPE_P (type))
894 type = signed_type_for (type);
895 fit_double_type (int1l, int1h, &int1l, &int1h,
899 fit_double_type (int1l, int1h, &int1l, &int1h, type);
900 int2l = TREE_INT_CST_LOW (arg2);
901 int2h = TREE_INT_CST_HIGH (arg2);
903 div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
904 &quol, &quoh, &reml, &remh);
905 if (remh != 0 || reml != 0)
908 return build_int_cst_wide (type, quol, quoh);
911 /* This is nonzero if we should defer warnings about undefined
912 overflow. This facility exists because these warnings are a
913 special case. The code to estimate loop iterations does not want
914 to issue any warnings, since it works with expressions which do not
915 occur in user code. Various bits of cleanup code call fold(), but
916 only use the result if it has certain characteristics (e.g., is a
917 constant); that code only wants to issue a warning if the result is
920 static int fold_deferring_overflow_warnings;
922 /* If a warning about undefined overflow is deferred, this is the
923 warning. Note that this may cause us to turn two warnings into
924 one, but that is fine since it is sufficient to only give one
925 warning per expression. */
927 static const char* fold_deferred_overflow_warning;
929 /* If a warning about undefined overflow is deferred, this is the
930 level at which the warning should be emitted. */
932 static enum warn_strict_overflow_code fold_deferred_overflow_code;
934 /* Start deferring overflow warnings. We could use a stack here to
935 permit nested calls, but at present it is not necessary. */
938 fold_defer_overflow_warnings (void)
940 ++fold_deferring_overflow_warnings;
943 /* Stop deferring overflow warnings. If there is a pending warning,
944 and ISSUE is true, then issue the warning if appropriate. STMT is
945 the statement with which the warning should be associated (used for
946 location information); STMT may be NULL. CODE is the level of the
947 warning--a warn_strict_overflow_code value. This function will use
948 the smaller of CODE and the deferred code when deciding whether to
949 issue the warning. CODE may be zero to mean to always use the
953 fold_undefer_overflow_warnings (bool issue, const_gimple stmt, int code)
958 gcc_assert (fold_deferring_overflow_warnings > 0);
959 --fold_deferring_overflow_warnings;
960 if (fold_deferring_overflow_warnings > 0)
962 if (fold_deferred_overflow_warning != NULL
964 && code < (int) fold_deferred_overflow_code)
965 fold_deferred_overflow_code = code;
969 warnmsg = fold_deferred_overflow_warning;
970 fold_deferred_overflow_warning = NULL;
972 if (!issue || warnmsg == NULL)
975 if (gimple_no_warning_p (stmt))
978 /* Use the smallest code level when deciding to issue the
980 if (code == 0 || code > (int) fold_deferred_overflow_code)
981 code = fold_deferred_overflow_code;
983 if (!issue_strict_overflow_warning (code))
987 locus = input_location;
989 locus = gimple_location (stmt);
990 warning (OPT_Wstrict_overflow, "%H%s", &locus, warnmsg);
993 /* Stop deferring overflow warnings, ignoring any deferred
997 fold_undefer_and_ignore_overflow_warnings (void)
999 fold_undefer_overflow_warnings (false, NULL, 0);
1002 /* Whether we are deferring overflow warnings. */
1005 fold_deferring_overflow_warnings_p (void)
1007 return fold_deferring_overflow_warnings > 0;
1010 /* This is called when we fold something based on the fact that signed
1011 overflow is undefined. */
1014 fold_overflow_warning (const char* gmsgid, enum warn_strict_overflow_code wc)
1016 if (fold_deferring_overflow_warnings > 0)
1018 if (fold_deferred_overflow_warning == NULL
1019 || wc < fold_deferred_overflow_code)
1021 fold_deferred_overflow_warning = gmsgid;
1022 fold_deferred_overflow_code = wc;
1025 else if (issue_strict_overflow_warning (wc))
1026 warning (OPT_Wstrict_overflow, gmsgid);
1029 /* Return true if the built-in mathematical function specified by CODE
1030 is odd, i.e. -f(x) == f(-x). */
1033 negate_mathfn_p (enum built_in_function code)
1037 CASE_FLT_FN (BUILT_IN_ASIN):
1038 CASE_FLT_FN (BUILT_IN_ASINH):
1039 CASE_FLT_FN (BUILT_IN_ATAN):
1040 CASE_FLT_FN (BUILT_IN_ATANH):
1041 CASE_FLT_FN (BUILT_IN_CASIN):
1042 CASE_FLT_FN (BUILT_IN_CASINH):
1043 CASE_FLT_FN (BUILT_IN_CATAN):
1044 CASE_FLT_FN (BUILT_IN_CATANH):
1045 CASE_FLT_FN (BUILT_IN_CBRT):
1046 CASE_FLT_FN (BUILT_IN_CPROJ):
1047 CASE_FLT_FN (BUILT_IN_CSIN):
1048 CASE_FLT_FN (BUILT_IN_CSINH):
1049 CASE_FLT_FN (BUILT_IN_CTAN):
1050 CASE_FLT_FN (BUILT_IN_CTANH):
1051 CASE_FLT_FN (BUILT_IN_ERF):
1052 CASE_FLT_FN (BUILT_IN_LLROUND):
1053 CASE_FLT_FN (BUILT_IN_LROUND):
1054 CASE_FLT_FN (BUILT_IN_ROUND):
1055 CASE_FLT_FN (BUILT_IN_SIN):
1056 CASE_FLT_FN (BUILT_IN_SINH):
1057 CASE_FLT_FN (BUILT_IN_TAN):
1058 CASE_FLT_FN (BUILT_IN_TANH):
1059 CASE_FLT_FN (BUILT_IN_TRUNC):
1062 CASE_FLT_FN (BUILT_IN_LLRINT):
1063 CASE_FLT_FN (BUILT_IN_LRINT):
1064 CASE_FLT_FN (BUILT_IN_NEARBYINT):
1065 CASE_FLT_FN (BUILT_IN_RINT):
1066 return !flag_rounding_math;
1074 /* Check whether we may negate an integer constant T without causing
1078 may_negate_without_overflow_p (const_tree t)
1080 unsigned HOST_WIDE_INT val;
1084 gcc_assert (TREE_CODE (t) == INTEGER_CST);
1086 type = TREE_TYPE (t);
1087 if (TYPE_UNSIGNED (type))
1090 prec = TYPE_PRECISION (type);
1091 if (prec > HOST_BITS_PER_WIDE_INT)
1093 if (TREE_INT_CST_LOW (t) != 0)
1095 prec -= HOST_BITS_PER_WIDE_INT;
1096 val = TREE_INT_CST_HIGH (t);
1099 val = TREE_INT_CST_LOW (t);
1100 if (prec < HOST_BITS_PER_WIDE_INT)
1101 val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
1102 return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
1105 /* Determine whether an expression T can be cheaply negated using
1106 the function negate_expr without introducing undefined overflow. */
1109 negate_expr_p (tree t)
1116 type = TREE_TYPE (t);
1118 STRIP_SIGN_NOPS (t);
1119 switch (TREE_CODE (t))
1122 if (TYPE_OVERFLOW_WRAPS (type))
1125 /* Check that -CST will not overflow type. */
1126 return may_negate_without_overflow_p (t);
1128 return (INTEGRAL_TYPE_P (type)
1129 && TYPE_OVERFLOW_WRAPS (type));
1137 return negate_expr_p (TREE_REALPART (t))
1138 && negate_expr_p (TREE_IMAGPART (t));
1141 return negate_expr_p (TREE_OPERAND (t, 0))
1142 && negate_expr_p (TREE_OPERAND (t, 1));
1145 return negate_expr_p (TREE_OPERAND (t, 0));
1148 if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1149 || HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
1151 /* -(A + B) -> (-B) - A. */
1152 if (negate_expr_p (TREE_OPERAND (t, 1))
1153 && reorder_operands_p (TREE_OPERAND (t, 0),
1154 TREE_OPERAND (t, 1)))
1156 /* -(A + B) -> (-A) - B. */
1157 return negate_expr_p (TREE_OPERAND (t, 0));
1160 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
1161 return !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type))
1162 && !HONOR_SIGNED_ZEROS (TYPE_MODE (type))
1163 && reorder_operands_p (TREE_OPERAND (t, 0),
1164 TREE_OPERAND (t, 1));
1167 if (TYPE_UNSIGNED (TREE_TYPE (t)))
1173 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
1174 return negate_expr_p (TREE_OPERAND (t, 1))
1175 || negate_expr_p (TREE_OPERAND (t, 0));
1178 case TRUNC_DIV_EXPR:
1179 case ROUND_DIV_EXPR:
1180 case FLOOR_DIV_EXPR:
1182 case EXACT_DIV_EXPR:
1183 /* In general we can't negate A / B, because if A is INT_MIN and
1184 B is 1, we may turn this into INT_MIN / -1 which is undefined
1185 and actually traps on some architectures. But if overflow is
1186 undefined, we can negate, because - (INT_MIN / 1) is an
1188 if (INTEGRAL_TYPE_P (TREE_TYPE (t))
1189 && !TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t)))
1191 return negate_expr_p (TREE_OPERAND (t, 1))
1192 || negate_expr_p (TREE_OPERAND (t, 0));
1195 /* Negate -((double)float) as (double)(-float). */
1196 if (TREE_CODE (type) == REAL_TYPE)
1198 tree tem = strip_float_extensions (t);
1200 return negate_expr_p (tem);
1205 /* Negate -f(x) as f(-x). */
1206 if (negate_mathfn_p (builtin_mathfn_code (t)))
1207 return negate_expr_p (CALL_EXPR_ARG (t, 0));
1211 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
1212 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1214 tree op1 = TREE_OPERAND (t, 1);
1215 if (TREE_INT_CST_HIGH (op1) == 0
1216 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1217 == TREE_INT_CST_LOW (op1))
1228 /* Given T, an expression, return a folded tree for -T or NULL_TREE, if no
1229 simplification is possible.
1230 If negate_expr_p would return true for T, NULL_TREE will never be
1234 fold_negate_expr (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)) || flag_associative_math)
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, const_tree type1, const_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 || (MODE_COMPOSITE_P (mode) && !flag_unsafe_math_optimizations))
1889 && (inexact || !real_identical (&result, &value)))
1892 t = build_real (type, result);
1894 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1898 if (TREE_CODE (arg1) == FIXED_CST)
1900 FIXED_VALUE_TYPE f1;
1901 FIXED_VALUE_TYPE f2;
1902 FIXED_VALUE_TYPE result;
1907 /* The following codes are handled by fixed_arithmetic. */
1913 case TRUNC_DIV_EXPR:
1914 f2 = TREE_FIXED_CST (arg2);
1919 f2.data.high = TREE_INT_CST_HIGH (arg2);
1920 f2.data.low = TREE_INT_CST_LOW (arg2);
1928 f1 = TREE_FIXED_CST (arg1);
1929 type = TREE_TYPE (arg1);
1930 sat_p = TYPE_SATURATING (type);
1931 overflow_p = fixed_arithmetic (&result, code, &f1, &f2, sat_p);
1932 t = build_fixed (type, result);
1933 /* Propagate overflow flags. */
1934 if (overflow_p | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1935 TREE_OVERFLOW (t) = 1;
1939 if (TREE_CODE (arg1) == COMPLEX_CST)
1941 tree type = TREE_TYPE (arg1);
1942 tree r1 = TREE_REALPART (arg1);
1943 tree i1 = TREE_IMAGPART (arg1);
1944 tree r2 = TREE_REALPART (arg2);
1945 tree i2 = TREE_IMAGPART (arg2);
1952 real = const_binop (code, r1, r2, notrunc);
1953 imag = const_binop (code, i1, i2, notrunc);
1957 real = const_binop (MINUS_EXPR,
1958 const_binop (MULT_EXPR, r1, r2, notrunc),
1959 const_binop (MULT_EXPR, i1, i2, notrunc),
1961 imag = const_binop (PLUS_EXPR,
1962 const_binop (MULT_EXPR, r1, i2, notrunc),
1963 const_binop (MULT_EXPR, i1, r2, notrunc),
1970 = const_binop (PLUS_EXPR,
1971 const_binop (MULT_EXPR, r2, r2, notrunc),
1972 const_binop (MULT_EXPR, i2, i2, notrunc),
1975 = const_binop (PLUS_EXPR,
1976 const_binop (MULT_EXPR, r1, r2, notrunc),
1977 const_binop (MULT_EXPR, i1, i2, notrunc),
1980 = const_binop (MINUS_EXPR,
1981 const_binop (MULT_EXPR, i1, r2, notrunc),
1982 const_binop (MULT_EXPR, r1, i2, notrunc),
1985 if (INTEGRAL_TYPE_P (TREE_TYPE (r1)))
1986 code = TRUNC_DIV_EXPR;
1988 real = const_binop (code, t1, magsquared, notrunc);
1989 imag = const_binop (code, t2, magsquared, notrunc);
1998 return build_complex (type, real, imag);
2004 /* Create a size type INT_CST node with NUMBER sign extended. KIND
2005 indicates which particular sizetype to create. */
2008 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
2010 return build_int_cst (sizetype_tab[(int) kind], number);
2013 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
2014 is a tree code. The type of the result is taken from the operands.
2015 Both must be equivalent integer types, ala int_binop_types_match_p.
2016 If the operands are constant, so is the result. */
2019 size_binop (enum tree_code code, tree arg0, tree arg1)
2021 tree type = TREE_TYPE (arg0);
2023 if (arg0 == error_mark_node || arg1 == error_mark_node)
2024 return error_mark_node;
2026 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
2029 /* Handle the special case of two integer constants faster. */
2030 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2032 /* And some specific cases even faster than that. */
2033 if (code == PLUS_EXPR)
2035 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
2037 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2040 else if (code == MINUS_EXPR)
2042 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2045 else if (code == MULT_EXPR)
2047 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
2051 /* Handle general case of two integer constants. */
2052 return int_const_binop (code, arg0, arg1, 0);
2055 return fold_build2 (code, type, arg0, arg1);
2058 /* Given two values, either both of sizetype or both of bitsizetype,
2059 compute the difference between the two values. Return the value
2060 in signed type corresponding to the type of the operands. */
2063 size_diffop (tree arg0, tree arg1)
2065 tree type = TREE_TYPE (arg0);
2068 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
2071 /* If the type is already signed, just do the simple thing. */
2072 if (!TYPE_UNSIGNED (type))
2073 return size_binop (MINUS_EXPR, arg0, arg1);
2075 if (type == sizetype)
2077 else if (type == bitsizetype)
2078 ctype = sbitsizetype;
2080 ctype = signed_type_for (type);
2082 /* If either operand is not a constant, do the conversions to the signed
2083 type and subtract. The hardware will do the right thing with any
2084 overflow in the subtraction. */
2085 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2086 return size_binop (MINUS_EXPR, fold_convert (ctype, arg0),
2087 fold_convert (ctype, arg1));
2089 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2090 Otherwise, subtract the other way, convert to CTYPE (we know that can't
2091 overflow) and negate (which can't either). Special-case a result
2092 of zero while we're here. */
2093 if (tree_int_cst_equal (arg0, arg1))
2094 return build_int_cst (ctype, 0);
2095 else if (tree_int_cst_lt (arg1, arg0))
2096 return fold_convert (ctype, size_binop (MINUS_EXPR, arg0, arg1));
2098 return size_binop (MINUS_EXPR, build_int_cst (ctype, 0),
2099 fold_convert (ctype, size_binop (MINUS_EXPR,
2103 /* A subroutine of fold_convert_const handling conversions of an
2104 INTEGER_CST to another integer type. */
2107 fold_convert_const_int_from_int (tree type, const_tree arg1)
2111 /* Given an integer constant, make new constant with new type,
2112 appropriately sign-extended or truncated. */
2113 t = force_fit_type_double (type, TREE_INT_CST_LOW (arg1),
2114 TREE_INT_CST_HIGH (arg1),
2115 /* Don't set the overflow when
2116 converting from a pointer, */
2117 !POINTER_TYPE_P (TREE_TYPE (arg1))
2118 /* or to a sizetype with same signedness
2119 and the precision is unchanged.
2120 ??? sizetype is always sign-extended,
2121 but its signedness depends on the
2122 frontend. Thus we see spurious overflows
2123 here if we do not check this. */
2124 && !((TYPE_PRECISION (TREE_TYPE (arg1))
2125 == TYPE_PRECISION (type))
2126 && (TYPE_UNSIGNED (TREE_TYPE (arg1))
2127 == TYPE_UNSIGNED (type))
2128 && ((TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
2129 && TYPE_IS_SIZETYPE (TREE_TYPE (arg1)))
2130 || (TREE_CODE (type) == INTEGER_TYPE
2131 && TYPE_IS_SIZETYPE (type)))),
2132 (TREE_INT_CST_HIGH (arg1) < 0
2133 && (TYPE_UNSIGNED (type)
2134 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2135 | TREE_OVERFLOW (arg1));
2140 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2141 to an integer type. */
2144 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2149 /* The following code implements the floating point to integer
2150 conversion rules required by the Java Language Specification,
2151 that IEEE NaNs are mapped to zero and values that overflow
2152 the target precision saturate, i.e. values greater than
2153 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2154 are mapped to INT_MIN. These semantics are allowed by the
2155 C and C++ standards that simply state that the behavior of
2156 FP-to-integer conversion is unspecified upon overflow. */
2158 HOST_WIDE_INT high, low;
2160 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2164 case FIX_TRUNC_EXPR:
2165 real_trunc (&r, VOIDmode, &x);
2172 /* If R is NaN, return zero and show we have an overflow. */
2173 if (REAL_VALUE_ISNAN (r))
2180 /* See if R is less than the lower bound or greater than the
2185 tree lt = TYPE_MIN_VALUE (type);
2186 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2187 if (REAL_VALUES_LESS (r, l))
2190 high = TREE_INT_CST_HIGH (lt);
2191 low = TREE_INT_CST_LOW (lt);
2197 tree ut = TYPE_MAX_VALUE (type);
2200 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2201 if (REAL_VALUES_LESS (u, r))
2204 high = TREE_INT_CST_HIGH (ut);
2205 low = TREE_INT_CST_LOW (ut);
2211 REAL_VALUE_TO_INT (&low, &high, r);
2213 t = force_fit_type_double (type, low, high, -1,
2214 overflow | TREE_OVERFLOW (arg1));
2218 /* A subroutine of fold_convert_const handling conversions of a
2219 FIXED_CST to an integer type. */
2222 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2225 double_int temp, temp_trunc;
2228 /* Right shift FIXED_CST to temp by fbit. */
2229 temp = TREE_FIXED_CST (arg1).data;
2230 mode = TREE_FIXED_CST (arg1).mode;
2231 if (GET_MODE_FBIT (mode) < 2 * HOST_BITS_PER_WIDE_INT)
2233 lshift_double (temp.low, temp.high,
2234 - GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2235 &temp.low, &temp.high, SIGNED_FIXED_POINT_MODE_P (mode));
2237 /* Left shift temp to temp_trunc by fbit. */
2238 lshift_double (temp.low, temp.high,
2239 GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2240 &temp_trunc.low, &temp_trunc.high,
2241 SIGNED_FIXED_POINT_MODE_P (mode));
2248 temp_trunc.high = 0;
2251 /* If FIXED_CST is negative, we need to round the value toward 0.
2252 By checking if the fractional bits are not zero to add 1 to temp. */
2253 if (SIGNED_FIXED_POINT_MODE_P (mode) && temp_trunc.high < 0
2254 && !double_int_equal_p (TREE_FIXED_CST (arg1).data, temp_trunc))
2259 temp = double_int_add (temp, one);
2262 /* Given a fixed-point constant, make new constant with new type,
2263 appropriately sign-extended or truncated. */
2264 t = force_fit_type_double (type, temp.low, temp.high, -1,
2266 && (TYPE_UNSIGNED (type)
2267 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2268 | TREE_OVERFLOW (arg1));
2273 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2274 to another floating point type. */
2277 fold_convert_const_real_from_real (tree type, const_tree arg1)
2279 REAL_VALUE_TYPE value;
2282 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2283 t = build_real (type, value);
2285 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2289 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2290 to a floating point type. */
2293 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2295 REAL_VALUE_TYPE value;
2298 real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
2299 t = build_real (type, value);
2301 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2305 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2306 to another fixed-point type. */
2309 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2311 FIXED_VALUE_TYPE value;
2315 overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2316 TYPE_SATURATING (type));
2317 t = build_fixed (type, value);
2319 /* Propagate overflow flags. */
2320 if (overflow_p | TREE_OVERFLOW (arg1))
2321 TREE_OVERFLOW (t) = 1;
2325 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2326 to a fixed-point type. */
2329 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2331 FIXED_VALUE_TYPE value;
2335 overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type),
2336 TREE_INT_CST (arg1),
2337 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2338 TYPE_SATURATING (type));
2339 t = build_fixed (type, value);
2341 /* Propagate overflow flags. */
2342 if (overflow_p | TREE_OVERFLOW (arg1))
2343 TREE_OVERFLOW (t) = 1;
2347 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2348 to a fixed-point type. */
2351 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2353 FIXED_VALUE_TYPE value;
2357 overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2358 &TREE_REAL_CST (arg1),
2359 TYPE_SATURATING (type));
2360 t = build_fixed (type, value);
2362 /* Propagate overflow flags. */
2363 if (overflow_p | TREE_OVERFLOW (arg1))
2364 TREE_OVERFLOW (t) = 1;
2368 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2369 type TYPE. If no simplification can be done return NULL_TREE. */
2372 fold_convert_const (enum tree_code code, tree type, tree arg1)
2374 if (TREE_TYPE (arg1) == type)
2377 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2378 || TREE_CODE (type) == OFFSET_TYPE)
2380 if (TREE_CODE (arg1) == INTEGER_CST)
2381 return fold_convert_const_int_from_int (type, arg1);
2382 else if (TREE_CODE (arg1) == REAL_CST)
2383 return fold_convert_const_int_from_real (code, type, arg1);
2384 else if (TREE_CODE (arg1) == FIXED_CST)
2385 return fold_convert_const_int_from_fixed (type, arg1);
2387 else if (TREE_CODE (type) == REAL_TYPE)
2389 if (TREE_CODE (arg1) == INTEGER_CST)
2390 return build_real_from_int_cst (type, arg1);
2391 else if (TREE_CODE (arg1) == REAL_CST)
2392 return fold_convert_const_real_from_real (type, arg1);
2393 else if (TREE_CODE (arg1) == FIXED_CST)
2394 return fold_convert_const_real_from_fixed (type, arg1);
2396 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2398 if (TREE_CODE (arg1) == FIXED_CST)
2399 return fold_convert_const_fixed_from_fixed (type, arg1);
2400 else if (TREE_CODE (arg1) == INTEGER_CST)
2401 return fold_convert_const_fixed_from_int (type, arg1);
2402 else if (TREE_CODE (arg1) == REAL_CST)
2403 return fold_convert_const_fixed_from_real (type, arg1);
2408 /* Construct a vector of zero elements of vector type TYPE. */
2411 build_zero_vector (tree type)
2416 elem = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2417 units = TYPE_VECTOR_SUBPARTS (type);
2420 for (i = 0; i < units; i++)
2421 list = tree_cons (NULL_TREE, elem, list);
2422 return build_vector (type, list);
2425 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2428 fold_convertible_p (const_tree type, const_tree arg)
2430 tree orig = TREE_TYPE (arg);
2435 if (TREE_CODE (arg) == ERROR_MARK
2436 || TREE_CODE (type) == ERROR_MARK
2437 || TREE_CODE (orig) == ERROR_MARK)
2440 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2443 switch (TREE_CODE (type))
2445 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2446 case POINTER_TYPE: case REFERENCE_TYPE:
2448 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2449 || TREE_CODE (orig) == OFFSET_TYPE)
2451 return (TREE_CODE (orig) == VECTOR_TYPE
2452 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2455 case FIXED_POINT_TYPE:
2459 return TREE_CODE (type) == TREE_CODE (orig);
2466 /* Convert expression ARG to type TYPE. Used by the middle-end for
2467 simple conversions in preference to calling the front-end's convert. */
2470 fold_convert (tree type, tree arg)
2472 tree orig = TREE_TYPE (arg);
2478 if (TREE_CODE (arg) == ERROR_MARK
2479 || TREE_CODE (type) == ERROR_MARK
2480 || TREE_CODE (orig) == ERROR_MARK)
2481 return error_mark_node;
2483 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2484 return fold_build1 (NOP_EXPR, type, arg);
2486 switch (TREE_CODE (type))
2488 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2489 case POINTER_TYPE: case REFERENCE_TYPE:
2491 if (TREE_CODE (arg) == INTEGER_CST)
2493 tem = fold_convert_const (NOP_EXPR, type, arg);
2494 if (tem != NULL_TREE)
2497 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2498 || TREE_CODE (orig) == OFFSET_TYPE)
2499 return fold_build1 (NOP_EXPR, type, arg);
2500 if (TREE_CODE (orig) == COMPLEX_TYPE)
2502 tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2503 return fold_convert (type, tem);
2505 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2506 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2507 return fold_build1 (NOP_EXPR, type, arg);
2510 if (TREE_CODE (arg) == INTEGER_CST)
2512 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2513 if (tem != NULL_TREE)
2516 else if (TREE_CODE (arg) == REAL_CST)
2518 tem = fold_convert_const (NOP_EXPR, type, arg);
2519 if (tem != NULL_TREE)
2522 else if (TREE_CODE (arg) == FIXED_CST)
2524 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2525 if (tem != NULL_TREE)
2529 switch (TREE_CODE (orig))
2532 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2533 case POINTER_TYPE: case REFERENCE_TYPE:
2534 return fold_build1 (FLOAT_EXPR, type, arg);
2537 return fold_build1 (NOP_EXPR, type, arg);
2539 case FIXED_POINT_TYPE:
2540 return fold_build1 (FIXED_CONVERT_EXPR, type, arg);
2543 tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2544 return fold_convert (type, tem);
2550 case FIXED_POINT_TYPE:
2551 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2552 || TREE_CODE (arg) == REAL_CST)
2554 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2555 if (tem != NULL_TREE)
2559 switch (TREE_CODE (orig))
2561 case FIXED_POINT_TYPE:
2566 return fold_build1 (FIXED_CONVERT_EXPR, type, arg);
2569 tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2570 return fold_convert (type, tem);
2577 switch (TREE_CODE (orig))
2580 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2581 case POINTER_TYPE: case REFERENCE_TYPE:
2583 case FIXED_POINT_TYPE:
2584 return build2 (COMPLEX_EXPR, type,
2585 fold_convert (TREE_TYPE (type), arg),
2586 fold_convert (TREE_TYPE (type), integer_zero_node));
2591 if (TREE_CODE (arg) == COMPLEX_EXPR)
2593 rpart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 0));
2594 ipart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 1));
2595 return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2598 arg = save_expr (arg);
2599 rpart = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2600 ipart = fold_build1 (IMAGPART_EXPR, TREE_TYPE (orig), arg);
2601 rpart = fold_convert (TREE_TYPE (type), rpart);
2602 ipart = fold_convert (TREE_TYPE (type), ipart);
2603 return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2611 if (integer_zerop (arg))
2612 return build_zero_vector (type);
2613 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2614 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2615 || TREE_CODE (orig) == VECTOR_TYPE);
2616 return fold_build1 (VIEW_CONVERT_EXPR, type, arg);
2619 tem = fold_ignored_result (arg);
2620 if (TREE_CODE (tem) == MODIFY_EXPR)
2622 return fold_build1 (NOP_EXPR, type, tem);
2629 /* Return false if expr can be assumed not to be an lvalue, true
2633 maybe_lvalue_p (const_tree x)
2635 /* We only need to wrap lvalue tree codes. */
2636 switch (TREE_CODE (x))
2647 case ALIGN_INDIRECT_REF:
2648 case MISALIGNED_INDIRECT_REF:
2650 case ARRAY_RANGE_REF:
2656 case PREINCREMENT_EXPR:
2657 case PREDECREMENT_EXPR:
2659 case TRY_CATCH_EXPR:
2660 case WITH_CLEANUP_EXPR:
2671 /* Assume the worst for front-end tree codes. */
2672 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2680 /* Return an expr equal to X but certainly not valid as an lvalue. */
2685 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2690 if (! maybe_lvalue_p (x))
2692 return build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2695 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2696 Zero means allow extended lvalues. */
2698 int pedantic_lvalues;
2700 /* When pedantic, return an expr equal to X but certainly not valid as a
2701 pedantic lvalue. Otherwise, return X. */
2704 pedantic_non_lvalue (tree x)
2706 if (pedantic_lvalues)
2707 return non_lvalue (x);
2712 /* Given a tree comparison code, return the code that is the logical inverse
2713 of the given code. It is not safe to do this for floating-point
2714 comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2715 as well: if reversing the comparison is unsafe, return ERROR_MARK. */
2718 invert_tree_comparison (enum tree_code code, bool honor_nans)
2720 if (honor_nans && flag_trapping_math)
2730 return honor_nans ? UNLE_EXPR : LE_EXPR;
2732 return honor_nans ? UNLT_EXPR : LT_EXPR;
2734 return honor_nans ? UNGE_EXPR : GE_EXPR;
2736 return honor_nans ? UNGT_EXPR : GT_EXPR;
2750 return UNORDERED_EXPR;
2751 case UNORDERED_EXPR:
2752 return ORDERED_EXPR;
2758 /* Similar, but return the comparison that results if the operands are
2759 swapped. This is safe for floating-point. */
2762 swap_tree_comparison (enum tree_code code)
2769 case UNORDERED_EXPR:
2795 /* Convert a comparison tree code from an enum tree_code representation
2796 into a compcode bit-based encoding. This function is the inverse of
2797 compcode_to_comparison. */
2799 static enum comparison_code
2800 comparison_to_compcode (enum tree_code code)
2817 return COMPCODE_ORD;
2818 case UNORDERED_EXPR:
2819 return COMPCODE_UNORD;
2821 return COMPCODE_UNLT;
2823 return COMPCODE_UNEQ;
2825 return COMPCODE_UNLE;
2827 return COMPCODE_UNGT;
2829 return COMPCODE_LTGT;
2831 return COMPCODE_UNGE;
2837 /* Convert a compcode bit-based encoding of a comparison operator back
2838 to GCC's enum tree_code representation. This function is the
2839 inverse of comparison_to_compcode. */
2841 static enum tree_code
2842 compcode_to_comparison (enum comparison_code code)
2859 return ORDERED_EXPR;
2860 case COMPCODE_UNORD:
2861 return UNORDERED_EXPR;
2879 /* Return a tree for the comparison which is the combination of
2880 doing the AND or OR (depending on CODE) of the two operations LCODE
2881 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2882 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2883 if this makes the transformation invalid. */
2886 combine_comparisons (enum tree_code code, enum tree_code lcode,
2887 enum tree_code rcode, tree truth_type,
2888 tree ll_arg, tree lr_arg)
2890 bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2891 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2892 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2893 enum comparison_code compcode;
2897 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2898 compcode = lcompcode & rcompcode;
2901 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2902 compcode = lcompcode | rcompcode;
2911 /* Eliminate unordered comparisons, as well as LTGT and ORD
2912 which are not used unless the mode has NaNs. */
2913 compcode &= ~COMPCODE_UNORD;
2914 if (compcode == COMPCODE_LTGT)
2915 compcode = COMPCODE_NE;
2916 else if (compcode == COMPCODE_ORD)
2917 compcode = COMPCODE_TRUE;
2919 else if (flag_trapping_math)
2921 /* Check that the original operation and the optimized ones will trap
2922 under the same condition. */
2923 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2924 && (lcompcode != COMPCODE_EQ)
2925 && (lcompcode != COMPCODE_ORD);
2926 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2927 && (rcompcode != COMPCODE_EQ)
2928 && (rcompcode != COMPCODE_ORD);
2929 bool trap = (compcode & COMPCODE_UNORD) == 0
2930 && (compcode != COMPCODE_EQ)
2931 && (compcode != COMPCODE_ORD);
2933 /* In a short-circuited boolean expression the LHS might be
2934 such that the RHS, if evaluated, will never trap. For
2935 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2936 if neither x nor y is NaN. (This is a mixed blessing: for
2937 example, the expression above will never trap, hence
2938 optimizing it to x < y would be invalid). */
2939 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2940 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2943 /* If the comparison was short-circuited, and only the RHS
2944 trapped, we may now generate a spurious trap. */
2946 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2949 /* If we changed the conditions that cause a trap, we lose. */
2950 if ((ltrap || rtrap) != trap)
2954 if (compcode == COMPCODE_TRUE)
2955 return constant_boolean_node (true, truth_type);
2956 else if (compcode == COMPCODE_FALSE)
2957 return constant_boolean_node (false, truth_type);
2959 return fold_build2 (compcode_to_comparison (compcode),
2960 truth_type, ll_arg, lr_arg);
2963 /* Return nonzero if two operands (typically of the same tree node)
2964 are necessarily equal. If either argument has side-effects this
2965 function returns zero. FLAGS modifies behavior as follows:
2967 If OEP_ONLY_CONST is set, only return nonzero for constants.
2968 This function tests whether the operands are indistinguishable;
2969 it does not test whether they are equal using C's == operation.
2970 The distinction is important for IEEE floating point, because
2971 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2972 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2974 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2975 even though it may hold multiple values during a function.
2976 This is because a GCC tree node guarantees that nothing else is
2977 executed between the evaluation of its "operands" (which may often
2978 be evaluated in arbitrary order). Hence if the operands themselves
2979 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2980 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2981 unset means assuming isochronic (or instantaneous) tree equivalence.
2982 Unless comparing arbitrary expression trees, such as from different
2983 statements, this flag can usually be left unset.
2985 If OEP_PURE_SAME is set, then pure functions with identical arguments
2986 are considered the same. It is used when the caller has other ways
2987 to ensure that global memory is unchanged in between. */
2990 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
2992 /* If either is ERROR_MARK, they aren't equal. */
2993 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK)
2996 /* Check equality of integer constants before bailing out due to
2997 precision differences. */
2998 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2999 return tree_int_cst_equal (arg0, arg1);
3001 /* If both types don't have the same signedness, then we can't consider
3002 them equal. We must check this before the STRIP_NOPS calls
3003 because they may change the signedness of the arguments. As pointers
3004 strictly don't have a signedness, require either two pointers or
3005 two non-pointers as well. */
3006 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
3007 || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
3010 /* If both types don't have the same precision, then it is not safe
3012 if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
3018 /* In case both args are comparisons but with different comparison
3019 code, try to swap the comparison operands of one arg to produce
3020 a match and compare that variant. */
3021 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3022 && COMPARISON_CLASS_P (arg0)
3023 && COMPARISON_CLASS_P (arg1))
3025 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3027 if (TREE_CODE (arg0) == swap_code)
3028 return operand_equal_p (TREE_OPERAND (arg0, 0),
3029 TREE_OPERAND (arg1, 1), flags)
3030 && operand_equal_p (TREE_OPERAND (arg0, 1),
3031 TREE_OPERAND (arg1, 0), flags);
3034 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3035 /* This is needed for conversions and for COMPONENT_REF.
3036 Might as well play it safe and always test this. */
3037 || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
3038 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
3039 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
3042 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3043 We don't care about side effects in that case because the SAVE_EXPR
3044 takes care of that for us. In all other cases, two expressions are
3045 equal if they have no side effects. If we have two identical
3046 expressions with side effects that should be treated the same due
3047 to the only side effects being identical SAVE_EXPR's, that will
3048 be detected in the recursive calls below. */
3049 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3050 && (TREE_CODE (arg0) == SAVE_EXPR
3051 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3054 /* Next handle constant cases, those for which we can return 1 even
3055 if ONLY_CONST is set. */
3056 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3057 switch (TREE_CODE (arg0))
3060 return tree_int_cst_equal (arg0, arg1);
3063 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3064 TREE_FIXED_CST (arg1));
3067 if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
3068 TREE_REAL_CST (arg1)))
3072 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
3074 /* If we do not distinguish between signed and unsigned zero,
3075 consider them equal. */
3076 if (real_zerop (arg0) && real_zerop (arg1))
3085 v1 = TREE_VECTOR_CST_ELTS (arg0);
3086 v2 = TREE_VECTOR_CST_ELTS (arg1);
3089 if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
3092 v1 = TREE_CHAIN (v1);
3093 v2 = TREE_CHAIN (v2);
3100 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3102 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3106 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3107 && ! memcmp (TREE_STRING_POINTER (arg0),
3108 TREE_STRING_POINTER (arg1),
3109 TREE_STRING_LENGTH (arg0)));
3112 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3118 if (flags & OEP_ONLY_CONST)
3121 /* Define macros to test an operand from arg0 and arg1 for equality and a
3122 variant that allows null and views null as being different from any
3123 non-null value. In the latter case, if either is null, the both
3124 must be; otherwise, do the normal comparison. */
3125 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3126 TREE_OPERAND (arg1, N), flags)
3128 #define OP_SAME_WITH_NULL(N) \
3129 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3130 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3132 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3135 /* Two conversions are equal only if signedness and modes match. */
3136 switch (TREE_CODE (arg0))
3139 case FIX_TRUNC_EXPR:
3140 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3141 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3151 case tcc_comparison:
3153 if (OP_SAME (0) && OP_SAME (1))
3156 /* For commutative ops, allow the other order. */
3157 return (commutative_tree_code (TREE_CODE (arg0))
3158 && operand_equal_p (TREE_OPERAND (arg0, 0),
3159 TREE_OPERAND (arg1, 1), flags)
3160 && operand_equal_p (TREE_OPERAND (arg0, 1),
3161 TREE_OPERAND (arg1, 0), flags));
3164 /* If either of the pointer (or reference) expressions we are
3165 dereferencing contain a side effect, these cannot be equal. */
3166 if (TREE_SIDE_EFFECTS (arg0)
3167 || TREE_SIDE_EFFECTS (arg1))
3170 switch (TREE_CODE (arg0))
3173 case ALIGN_INDIRECT_REF:
3174 case MISALIGNED_INDIRECT_REF:
3180 case ARRAY_RANGE_REF:
3181 /* Operands 2 and 3 may be null.
3182 Compare the array index by value if it is constant first as we
3183 may have different types but same value here. */
3185 && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3186 TREE_OPERAND (arg1, 1))
3188 && OP_SAME_WITH_NULL (2)
3189 && OP_SAME_WITH_NULL (3));
3192 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3193 may be NULL when we're called to compare MEM_EXPRs. */
3194 return OP_SAME_WITH_NULL (0)
3196 && OP_SAME_WITH_NULL (2);
3199 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3205 case tcc_expression:
3206 switch (TREE_CODE (arg0))
3209 case TRUTH_NOT_EXPR:
3212 case TRUTH_ANDIF_EXPR:
3213 case TRUTH_ORIF_EXPR:
3214 return OP_SAME (0) && OP_SAME (1);
3216 case TRUTH_AND_EXPR:
3218 case TRUTH_XOR_EXPR:
3219 if (OP_SAME (0) && OP_SAME (1))
3222 /* Otherwise take into account this is a commutative operation. */
3223 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3224 TREE_OPERAND (arg1, 1), flags)
3225 && operand_equal_p (TREE_OPERAND (arg0, 1),
3226 TREE_OPERAND (arg1, 0), flags));
3229 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3236 switch (TREE_CODE (arg0))
3239 /* If the CALL_EXPRs call different functions, then they
3240 clearly can not be equal. */
3241 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3246 unsigned int cef = call_expr_flags (arg0);
3247 if (flags & OEP_PURE_SAME)
3248 cef &= ECF_CONST | ECF_PURE;
3255 /* Now see if all the arguments are the same. */
3257 const_call_expr_arg_iterator iter0, iter1;
3259 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3260 a1 = first_const_call_expr_arg (arg1, &iter1);
3262 a0 = next_const_call_expr_arg (&iter0),
3263 a1 = next_const_call_expr_arg (&iter1))
3264 if (! operand_equal_p (a0, a1, flags))
3267 /* If we get here and both argument lists are exhausted
3268 then the CALL_EXPRs are equal. */
3269 return ! (a0 || a1);
3275 case tcc_declaration:
3276 /* Consider __builtin_sqrt equal to sqrt. */
3277 return (TREE_CODE (arg0) == FUNCTION_DECL
3278 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3279 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3280 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3287 #undef OP_SAME_WITH_NULL
3290 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3291 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3293 When in doubt, return 0. */
3296 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3298 int unsignedp1, unsignedpo;
3299 tree primarg0, primarg1, primother;
3300 unsigned int correct_width;
3302 if (operand_equal_p (arg0, arg1, 0))
3305 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3306 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3309 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3310 and see if the inner values are the same. This removes any
3311 signedness comparison, which doesn't matter here. */
3312 primarg0 = arg0, primarg1 = arg1;
3313 STRIP_NOPS (primarg0);
3314 STRIP_NOPS (primarg1);
3315 if (operand_equal_p (primarg0, primarg1, 0))
3318 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3319 actual comparison operand, ARG0.
3321 First throw away any conversions to wider types
3322 already present in the operands. */
3324 primarg1 = get_narrower (arg1, &unsignedp1);
3325 primother = get_narrower (other, &unsignedpo);
3327 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3328 if (unsignedp1 == unsignedpo
3329 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3330 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3332 tree type = TREE_TYPE (arg0);
3334 /* Make sure shorter operand is extended the right way
3335 to match the longer operand. */
3336 primarg1 = fold_convert (signed_or_unsigned_type_for
3337 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3339 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3346 /* See if ARG is an expression that is either a comparison or is performing
3347 arithmetic on comparisons. The comparisons must only be comparing
3348 two different values, which will be stored in *CVAL1 and *CVAL2; if
3349 they are nonzero it means that some operands have already been found.
3350 No variables may be used anywhere else in the expression except in the
3351 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3352 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3354 If this is true, return 1. Otherwise, return zero. */
3357 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3359 enum tree_code code = TREE_CODE (arg);
3360 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3362 /* We can handle some of the tcc_expression cases here. */
3363 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3365 else if (tclass == tcc_expression
3366 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3367 || code == COMPOUND_EXPR))
3368 tclass = tcc_binary;
3370 else if (tclass == tcc_expression && code == SAVE_EXPR
3371 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3373 /* If we've already found a CVAL1 or CVAL2, this expression is
3374 two complex to handle. */
3375 if (*cval1 || *cval2)
3385 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3388 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3389 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3390 cval1, cval2, save_p));
3395 case tcc_expression:
3396 if (code == COND_EXPR)
3397 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3398 cval1, cval2, save_p)
3399 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3400 cval1, cval2, save_p)
3401 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3402 cval1, cval2, save_p));
3405 case tcc_comparison:
3406 /* First see if we can handle the first operand, then the second. For
3407 the second operand, we know *CVAL1 can't be zero. It must be that
3408 one side of the comparison is each of the values; test for the
3409 case where this isn't true by failing if the two operands
3412 if (operand_equal_p (TREE_OPERAND (arg, 0),
3413 TREE_OPERAND (arg, 1), 0))
3417 *cval1 = TREE_OPERAND (arg, 0);
3418 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3420 else if (*cval2 == 0)
3421 *cval2 = TREE_OPERAND (arg, 0);
3422 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3427 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3429 else if (*cval2 == 0)
3430 *cval2 = TREE_OPERAND (arg, 1);
3431 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3443 /* ARG is a tree that is known to contain just arithmetic operations and
3444 comparisons. Evaluate the operations in the tree substituting NEW0 for
3445 any occurrence of OLD0 as an operand of a comparison and likewise for
3449 eval_subst (tree arg, tree old0, tree new0, tree old1, tree new1)
3451 tree type = TREE_TYPE (arg);
3452 enum tree_code code = TREE_CODE (arg);
3453 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3455 /* We can handle some of the tcc_expression cases here. */
3456 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3458 else if (tclass == tcc_expression
3459 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3460 tclass = tcc_binary;
3465 return fold_build1 (code, type,
3466 eval_subst (TREE_OPERAND (arg, 0),
3467 old0, new0, old1, new1));
3470 return fold_build2 (code, type,
3471 eval_subst (TREE_OPERAND (arg, 0),
3472 old0, new0, old1, new1),
3473 eval_subst (TREE_OPERAND (arg, 1),
3474 old0, new0, old1, new1));
3476 case tcc_expression:
3480 return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
3483 return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
3486 return fold_build3 (code, type,
3487 eval_subst (TREE_OPERAND (arg, 0),
3488 old0, new0, old1, new1),
3489 eval_subst (TREE_OPERAND (arg, 1),
3490 old0, new0, old1, new1),
3491 eval_subst (TREE_OPERAND (arg, 2),
3492 old0, new0, old1, new1));
3496 /* Fall through - ??? */
3498 case tcc_comparison:
3500 tree arg0 = TREE_OPERAND (arg, 0);
3501 tree arg1 = TREE_OPERAND (arg, 1);
3503 /* We need to check both for exact equality and tree equality. The
3504 former will be true if the operand has a side-effect. In that
3505 case, we know the operand occurred exactly once. */
3507 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3509 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3512 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3514 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3517 return fold_build2 (code, type, arg0, arg1);
3525 /* Return a tree for the case when the result of an expression is RESULT
3526 converted to TYPE and OMITTED was previously an operand of the expression
3527 but is now not needed (e.g., we folded OMITTED * 0).
3529 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3530 the conversion of RESULT to TYPE. */
3533 omit_one_operand (tree type, tree result, tree omitted)
3535 tree t = fold_convert (type, result);
3537 /* If the resulting operand is an empty statement, just return the omitted
3538 statement casted to void. */
3539 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3540 return build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3542 if (TREE_SIDE_EFFECTS (omitted))
3543 return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3545 return non_lvalue (t);
3548 /* Similar, but call pedantic_non_lvalue instead of non_lvalue. */
3551 pedantic_omit_one_operand (tree type, tree result, tree omitted)
3553 tree t = fold_convert (type, result);
3555 /* If the resulting operand is an empty statement, just return the omitted
3556 statement casted to void. */
3557 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3558 return build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3560 if (TREE_SIDE_EFFECTS (omitted))
3561 return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3563 return pedantic_non_lvalue (t);
3566 /* Return a tree for the case when the result of an expression is RESULT
3567 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3568 of the expression but are now not needed.
3570 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3571 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3572 evaluated before OMITTED2. Otherwise, if neither has side effects,
3573 just do the conversion of RESULT to TYPE. */
3576 omit_two_operands (tree type, tree result, tree omitted1, tree omitted2)
3578 tree t = fold_convert (type, result);
3580 if (TREE_SIDE_EFFECTS (omitted2))
3581 t = build2 (COMPOUND_EXPR, type, omitted2, t);
3582 if (TREE_SIDE_EFFECTS (omitted1))
3583 t = build2 (COMPOUND_EXPR, type, omitted1, t);
3585 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue (t) : t;
3589 /* Return a simplified tree node for the truth-negation of ARG. This
3590 never alters ARG itself. We assume that ARG is an operation that
3591 returns a truth value (0 or 1).
3593 FIXME: one would think we would fold the result, but it causes
3594 problems with the dominator optimizer. */
3597 fold_truth_not_expr (tree arg)
3599 tree type = TREE_TYPE (arg);
3600 enum tree_code code = TREE_CODE (arg);
3602 /* If this is a comparison, we can simply invert it, except for
3603 floating-point non-equality comparisons, in which case we just
3604 enclose a TRUTH_NOT_EXPR around what we have. */
3606 if (TREE_CODE_CLASS (code) == tcc_comparison)
3608 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3609 if (FLOAT_TYPE_P (op_type)
3610 && flag_trapping_math
3611 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3612 && code != NE_EXPR && code != EQ_EXPR)
3616 code = invert_tree_comparison (code,
3617 HONOR_NANS (TYPE_MODE (op_type)));
3618 if (code == ERROR_MARK)
3621 return build2 (code, type,
3622 TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
3629 return constant_boolean_node (integer_zerop (arg), type);
3631 case TRUTH_AND_EXPR:
3632 return build2 (TRUTH_OR_EXPR, type,
3633 invert_truthvalue (TREE_OPERAND (arg, 0)),
3634 invert_truthvalue (TREE_OPERAND (arg, 1)));
3637 return build2 (TRUTH_AND_EXPR, type,
3638 invert_truthvalue (TREE_OPERAND (arg, 0)),
3639 invert_truthvalue (TREE_OPERAND (arg, 1)));
3641 case TRUTH_XOR_EXPR:
3642 /* Here we can invert either operand. We invert the first operand
3643 unless the second operand is a TRUTH_NOT_EXPR in which case our
3644 result is the XOR of the first operand with the inside of the
3645 negation of the second operand. */
3647 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3648 return build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3649 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3651 return build2 (TRUTH_XOR_EXPR, type,
3652 invert_truthvalue (TREE_OPERAND (arg, 0)),
3653 TREE_OPERAND (arg, 1));
3655 case TRUTH_ANDIF_EXPR:
3656 return build2 (TRUTH_ORIF_EXPR, type,
3657 invert_truthvalue (TREE_OPERAND (arg, 0)),
3658 invert_truthvalue (TREE_OPERAND (arg, 1)));
3660 case TRUTH_ORIF_EXPR:
3661 return build2 (TRUTH_ANDIF_EXPR, type,
3662 invert_truthvalue (TREE_OPERAND (arg, 0)),
3663 invert_truthvalue (TREE_OPERAND (arg, 1)));
3665 case TRUTH_NOT_EXPR:
3666 return TREE_OPERAND (arg, 0);
3670 tree arg1 = TREE_OPERAND (arg, 1);
3671 tree arg2 = TREE_OPERAND (arg, 2);
3672 /* A COND_EXPR may have a throw as one operand, which
3673 then has void type. Just leave void operands
3675 return build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
3676 VOID_TYPE_P (TREE_TYPE (arg1))
3677 ? arg1 : invert_truthvalue (arg1),
3678 VOID_TYPE_P (TREE_TYPE (arg2))
3679 ? arg2 : invert_truthvalue (arg2));
3683 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
3684 invert_truthvalue (TREE_OPERAND (arg, 1)));
3686 case NON_LVALUE_EXPR:
3687 return invert_truthvalue (TREE_OPERAND (arg, 0));
3690 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3691 return build1 (TRUTH_NOT_EXPR, type, arg);
3695 return build1 (TREE_CODE (arg), type,
3696 invert_truthvalue (TREE_OPERAND (arg, 0)));
3699 if (!integer_onep (TREE_OPERAND (arg, 1)))
3701 return build2 (EQ_EXPR, type, arg,
3702 build_int_cst (type, 0));
3705 return build1 (TRUTH_NOT_EXPR, type, arg);
3707 case CLEANUP_POINT_EXPR:
3708 return build1 (CLEANUP_POINT_EXPR, type,
3709 invert_truthvalue (TREE_OPERAND (arg, 0)));
3718 /* Return a simplified tree node for the truth-negation of ARG. This
3719 never alters ARG itself. We assume that ARG is an operation that
3720 returns a truth value (0 or 1).
3722 FIXME: one would think we would fold the result, but it causes
3723 problems with the dominator optimizer. */
3726 invert_truthvalue (tree arg)
3730 if (TREE_CODE (arg) == ERROR_MARK)
3733 tem = fold_truth_not_expr (arg);
3735 tem = build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
3740 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3741 operands are another bit-wise operation with a common input. If so,
3742 distribute the bit operations to save an operation and possibly two if
3743 constants are involved. For example, convert
3744 (A | B) & (A | C) into A | (B & C)
3745 Further simplification will occur if B and C are constants.
3747 If this optimization cannot be done, 0 will be returned. */
3750 distribute_bit_expr (enum tree_code code, tree type, tree arg0, tree arg1)
3755 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3756 || TREE_CODE (arg0) == code
3757 || (TREE_CODE (arg0) != BIT_AND_EXPR
3758 && TREE_CODE (arg0) != BIT_IOR_EXPR))
3761 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3763 common = TREE_OPERAND (arg0, 0);
3764 left = TREE_OPERAND (arg0, 1);
3765 right = TREE_OPERAND (arg1, 1);
3767 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3769 common = TREE_OPERAND (arg0, 0);
3770 left = TREE_OPERAND (arg0, 1);
3771 right = TREE_OPERAND (arg1, 0);
3773 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3775 common = TREE_OPERAND (arg0, 1);
3776 left = TREE_OPERAND (arg0, 0);
3777 right = TREE_OPERAND (arg1, 1);
3779 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3781 common = TREE_OPERAND (arg0, 1);
3782 left = TREE_OPERAND (arg0, 0);
3783 right = TREE_OPERAND (arg1, 0);
3788 common = fold_convert (type, common);
3789 left = fold_convert (type, left);
3790 right = fold_convert (type, right);
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, HOST_WIDE_INT bitsize,
3840 HOST_WIDE_INT bitpos, int unsignedp)
3842 tree result, bftype;
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);
3855 if (TYPE_PRECISION (bftype) != bitsize
3856 || TYPE_UNSIGNED (bftype) == !unsignedp)
3857 bftype = build_nonstandard_integer_type (bitsize, 0);
3859 result = build3 (BIT_FIELD_REF, bftype, inner,
3860 size_int (bitsize), bitsize_int (bitpos));
3863 result = fold_convert (type, result);
3868 /* Optimize a bit-field compare.
3870 There are two cases: First is a compare against a constant and the
3871 second is a comparison of two items where the fields are at the same
3872 bit position relative to the start of a chunk (byte, halfword, word)
3873 large enough to contain it. In these cases we can avoid the shift
3874 implicit in bitfield extractions.
3876 For constants, we emit a compare of the shifted constant with the
3877 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3878 compared. For two fields at the same position, we do the ANDs with the
3879 similar mask and compare the result of the ANDs.
3881 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3882 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3883 are the left and right operands of the comparison, respectively.
3885 If the optimization described above can be done, we return the resulting
3886 tree. Otherwise we return zero. */
3889 optimize_bit_field_compare (enum tree_code code, tree compare_type,
3892 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3893 tree type = TREE_TYPE (lhs);
3894 tree signed_type, unsigned_type;
3895 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3896 enum machine_mode lmode, rmode, nmode;
3897 int lunsignedp, runsignedp;
3898 int lvolatilep = 0, rvolatilep = 0;
3899 tree linner, rinner = NULL_TREE;
3903 /* Get all the information about the extractions being done. If the bit size
3904 if the same as the size of the underlying object, we aren't doing an
3905 extraction at all and so can do nothing. We also don't want to
3906 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3907 then will no longer be able to replace it. */
3908 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3909 &lunsignedp, &lvolatilep, false);
3910 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3911 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3916 /* If this is not a constant, we can only do something if bit positions,
3917 sizes, and signedness are the same. */
3918 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3919 &runsignedp, &rvolatilep, false);
3921 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3922 || lunsignedp != runsignedp || offset != 0
3923 || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
3927 /* See if we can find a mode to refer to this field. We should be able to,
3928 but fail if we can't. */
3929 nmode = get_best_mode (lbitsize, lbitpos,
3930 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3931 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3932 TYPE_ALIGN (TREE_TYPE (rinner))),
3933 word_mode, lvolatilep || rvolatilep);
3934 if (nmode == VOIDmode)
3937 /* Set signed and unsigned types of the precision of this mode for the
3939 signed_type = lang_hooks.types.type_for_mode (nmode, 0);
3940 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3942 /* Compute the bit position and size for the new reference and our offset
3943 within it. If the new reference is the same size as the original, we
3944 won't optimize anything, so return zero. */
3945 nbitsize = GET_MODE_BITSIZE (nmode);
3946 nbitpos = lbitpos & ~ (nbitsize - 1);
3948 if (nbitsize == lbitsize)
3951 if (BYTES_BIG_ENDIAN)
3952 lbitpos = nbitsize - lbitsize - lbitpos;
3954 /* Make the mask to be used against the extracted field. */
3955 mask = build_int_cst_type (unsigned_type, -1);
3956 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
3957 mask = const_binop (RSHIFT_EXPR, mask,
3958 size_int (nbitsize - lbitsize - lbitpos), 0);
3961 /* If not comparing with constant, just rework the comparison
3963 return fold_build2 (code, compare_type,
3964 fold_build2 (BIT_AND_EXPR, unsigned_type,
3965 make_bit_field_ref (linner,
3970 fold_build2 (BIT_AND_EXPR, unsigned_type,
3971 make_bit_field_ref (rinner,
3977 /* Otherwise, we are handling the constant case. See if the constant is too
3978 big for the field. Warn and return a tree of for 0 (false) if so. We do
3979 this not only for its own sake, but to avoid having to test for this
3980 error case below. If we didn't, we might generate wrong code.
3982 For unsigned fields, the constant shifted right by the field length should
3983 be all zero. For signed fields, the high-order bits should agree with
3988 if (! integer_zerop (const_binop (RSHIFT_EXPR,
3989 fold_convert (unsigned_type, rhs),
3990 size_int (lbitsize), 0)))
3992 warning (0, "comparison is always %d due to width of bit-field",
3994 return constant_boolean_node (code == NE_EXPR, compare_type);
3999 tree tem = const_binop (RSHIFT_EXPR, fold_convert (signed_type, rhs),
4000 size_int (lbitsize - 1), 0);
4001 if (! integer_zerop (tem) && ! integer_all_onesp (tem))
4003 warning (0, "comparison is always %d due to width of bit-field",
4005 return constant_boolean_node (code == NE_EXPR, compare_type);
4009 /* Single-bit compares should always be against zero. */
4010 if (lbitsize == 1 && ! integer_zerop (rhs))
4012 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
4013 rhs = build_int_cst (type, 0);
4016 /* Make a new bitfield reference, shift the constant over the
4017 appropriate number of bits and mask it with the computed mask
4018 (in case this was a signed field). If we changed it, make a new one. */
4019 lhs = make_bit_field_ref (linner, unsigned_type, nbitsize, nbitpos, 1);
4022 TREE_SIDE_EFFECTS (lhs) = 1;
4023 TREE_THIS_VOLATILE (lhs) = 1;
4026 rhs = const_binop (BIT_AND_EXPR,
4027 const_binop (LSHIFT_EXPR,
4028 fold_convert (unsigned_type, rhs),
4029 size_int (lbitpos), 0),
4032 return build2 (code, compare_type,
4033 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask),
4037 /* Subroutine for fold_truthop: decode a field reference.
4039 If EXP is a comparison reference, we return the innermost reference.
4041 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
4042 set to the starting bit number.
4044 If the innermost field can be completely contained in a mode-sized
4045 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
4047 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
4048 otherwise it is not changed.
4050 *PUNSIGNEDP is set to the signedness of the field.
4052 *PMASK is set to the mask used. This is either contained in a
4053 BIT_AND_EXPR or derived from the width of the field.
4055 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
4057 Return 0 if this is not a component reference or is one that we can't
4058 do anything with. */
4061 decode_field_reference (tree exp, HOST_WIDE_INT *pbitsize,
4062 HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
4063 int *punsignedp, int *pvolatilep,
4064 tree *pmask, tree *pand_mask)
4066 tree outer_type = 0;
4068 tree mask, inner, offset;
4070 unsigned int precision;
4072 /* All the optimizations using this function assume integer fields.
4073 There are problems with FP fields since the type_for_size call
4074 below can fail for, e.g., XFmode. */
4075 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
4078 /* We are interested in the bare arrangement of bits, so strip everything
4079 that doesn't affect the machine mode. However, record the type of the
4080 outermost expression if it may matter below. */
4081 if (CONVERT_EXPR_P (exp)
4082 || TREE_CODE (exp) == NON_LVALUE_EXPR)
4083 outer_type = TREE_TYPE (exp);
4086 if (TREE_CODE (exp) == BIT_AND_EXPR)
4088 and_mask = TREE_OPERAND (exp, 1);
4089 exp = TREE_OPERAND (exp, 0);
4090 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
4091 if (TREE_CODE (and_mask) != INTEGER_CST)
4095 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
4096 punsignedp, pvolatilep, false);
4097 if ((inner == exp && and_mask == 0)
4098 || *pbitsize < 0 || offset != 0
4099 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
4102 /* If the number of bits in the reference is the same as the bitsize of
4103 the outer type, then the outer type gives the signedness. Otherwise
4104 (in case of a small bitfield) the signedness is unchanged. */
4105 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
4106 *punsignedp = TYPE_UNSIGNED (outer_type);
4108 /* Compute the mask to access the bitfield. */
4109 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
4110 precision = TYPE_PRECISION (unsigned_type);
4112 mask = build_int_cst_type (unsigned_type, -1);
4114 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
4115 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
4117 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
4119 mask = fold_build2 (BIT_AND_EXPR, unsigned_type,
4120 fold_convert (unsigned_type, and_mask), mask);
4123 *pand_mask = and_mask;
4127 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
4131 all_ones_mask_p (const_tree mask, int size)
4133 tree type = TREE_TYPE (mask);
4134 unsigned int precision = TYPE_PRECISION (type);
4137 tmask = build_int_cst_type (signed_type_for (type), -1);
4140 tree_int_cst_equal (mask,
4141 const_binop (RSHIFT_EXPR,
4142 const_binop (LSHIFT_EXPR, tmask,
4143 size_int (precision - size),
4145 size_int (precision - size), 0));
4148 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
4149 represents the sign bit of EXP's type. If EXP represents a sign
4150 or zero extension, also test VAL against the unextended type.
4151 The return value is the (sub)expression whose sign bit is VAL,
4152 or NULL_TREE otherwise. */
4155 sign_bit_p (tree exp, const_tree val)
4157 unsigned HOST_WIDE_INT mask_lo, lo;
4158 HOST_WIDE_INT mask_hi, hi;
4162 /* Tree EXP must have an integral type. */
4163 t = TREE_TYPE (exp);
4164 if (! INTEGRAL_TYPE_P (t))
4167 /* Tree VAL must be an integer constant. */
4168 if (TREE_CODE (val) != INTEGER_CST
4169 || TREE_OVERFLOW (val))
4172 width = TYPE_PRECISION (t);
4173 if (width > HOST_BITS_PER_WIDE_INT)
4175 hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
4178 mask_hi = ((unsigned HOST_WIDE_INT) -1
4179 >> (2 * HOST_BITS_PER_WIDE_INT - width));
4185 lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
4188 mask_lo = ((unsigned HOST_WIDE_INT) -1
4189 >> (HOST_BITS_PER_WIDE_INT - width));
4192 /* We mask off those bits beyond TREE_TYPE (exp) so that we can
4193 treat VAL as if it were unsigned. */
4194 if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
4195 && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
4198 /* Handle extension from a narrower type. */
4199 if (TREE_CODE (exp) == NOP_EXPR
4200 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4201 return sign_bit_p (TREE_OPERAND (exp, 0), val);
4206 /* Subroutine for fold_truthop: determine if an operand is simple enough
4207 to be evaluated unconditionally. */
4210 simple_operand_p (const_tree exp)
4212 /* Strip any conversions that don't change the machine mode. */
4215 return (CONSTANT_CLASS_P (exp)
4216 || TREE_CODE (exp) == SSA_NAME
4218 && ! TREE_ADDRESSABLE (exp)
4219 && ! TREE_THIS_VOLATILE (exp)
4220 && ! DECL_NONLOCAL (exp)
4221 /* Don't regard global variables as simple. They may be
4222 allocated in ways unknown to the compiler (shared memory,
4223 #pragma weak, etc). */
4224 && ! TREE_PUBLIC (exp)
4225 && ! DECL_EXTERNAL (exp)
4226 /* Loading a static variable is unduly expensive, but global
4227 registers aren't expensive. */
4228 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4231 /* The following functions are subroutines to fold_range_test and allow it to
4232 try to change a logical combination of comparisons into a range test.
4235 X == 2 || X == 3 || X == 4 || X == 5
4239 (unsigned) (X - 2) <= 3
4241 We describe each set of comparisons as being either inside or outside
4242 a range, using a variable named like IN_P, and then describe the
4243 range with a lower and upper bound. If one of the bounds is omitted,
4244 it represents either the highest or lowest value of the type.
4246 In the comments below, we represent a range by two numbers in brackets
4247 preceded by a "+" to designate being inside that range, or a "-" to
4248 designate being outside that range, so the condition can be inverted by
4249 flipping the prefix. An omitted bound is represented by a "-". For
4250 example, "- [-, 10]" means being outside the range starting at the lowest
4251 possible value and ending at 10, in other words, being greater than 10.
4252 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4255 We set up things so that the missing bounds are handled in a consistent
4256 manner so neither a missing bound nor "true" and "false" need to be
4257 handled using a special case. */
4259 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4260 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4261 and UPPER1_P are nonzero if the respective argument is an upper bound
4262 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4263 must be specified for a comparison. ARG1 will be converted to ARG0's
4264 type if both are specified. */
4267 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4268 tree arg1, int upper1_p)
4274 /* If neither arg represents infinity, do the normal operation.
4275 Else, if not a comparison, return infinity. Else handle the special
4276 comparison rules. Note that most of the cases below won't occur, but
4277 are handled for consistency. */
4279 if (arg0 != 0 && arg1 != 0)
4281 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4282 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4284 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4287 if (TREE_CODE_CLASS (code) != tcc_comparison)
4290 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4291 for neither. In real maths, we cannot assume open ended ranges are
4292 the same. But, this is computer arithmetic, where numbers are finite.
4293 We can therefore make the transformation of any unbounded range with
4294 the value Z, Z being greater than any representable number. This permits
4295 us to treat unbounded ranges as equal. */
4296 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4297 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4301 result = sgn0 == sgn1;
4304 result = sgn0 != sgn1;
4307 result = sgn0 < sgn1;
4310 result = sgn0 <= sgn1;
4313 result = sgn0 > sgn1;
4316 result = sgn0 >= sgn1;
4322 return constant_boolean_node (result, type);
4325 /* Given EXP, a logical expression, set the range it is testing into
4326 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4327 actually being tested. *PLOW and *PHIGH will be made of the same
4328 type as the returned expression. If EXP is not a comparison, we
4329 will most likely not be returning a useful value and range. Set
4330 *STRICT_OVERFLOW_P to true if the return value is only valid
4331 because signed overflow is undefined; otherwise, do not change
4332 *STRICT_OVERFLOW_P. */
4335 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4336 bool *strict_overflow_p)
4338 enum tree_code code;
4339 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
4340 tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
4342 tree low, high, n_low, n_high;
4344 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4345 and see if we can refine the range. Some of the cases below may not
4346 happen, but it doesn't seem worth worrying about this. We "continue"
4347 the outer loop when we've changed something; otherwise we "break"
4348 the switch, which will "break" the while. */
4351 low = high = build_int_cst (TREE_TYPE (exp), 0);
4355 code = TREE_CODE (exp);
4356 exp_type = TREE_TYPE (exp);
4358 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4360 if (TREE_OPERAND_LENGTH (exp) > 0)
4361 arg0 = TREE_OPERAND (exp, 0);
4362 if (TREE_CODE_CLASS (code) == tcc_comparison
4363 || TREE_CODE_CLASS (code) == tcc_unary
4364 || TREE_CODE_CLASS (code) == tcc_binary)
4365 arg0_type = TREE_TYPE (arg0);
4366 if (TREE_CODE_CLASS (code) == tcc_binary
4367 || TREE_CODE_CLASS (code) == tcc_comparison
4368 || (TREE_CODE_CLASS (code) == tcc_expression
4369 && TREE_OPERAND_LENGTH (exp) > 1))
4370 arg1 = TREE_OPERAND (exp, 1);
4375 case TRUTH_NOT_EXPR:
4376 in_p = ! in_p, exp = arg0;
4379 case EQ_EXPR: case NE_EXPR:
4380 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4381 /* We can only do something if the range is testing for zero
4382 and if the second operand is an integer constant. Note that
4383 saying something is "in" the range we make is done by
4384 complementing IN_P since it will set in the initial case of
4385 being not equal to zero; "out" is leaving it alone. */
4386 if (low == 0 || high == 0
4387 || ! integer_zerop (low) || ! integer_zerop (high)
4388 || TREE_CODE (arg1) != INTEGER_CST)
4393 case NE_EXPR: /* - [c, c] */
4396 case EQ_EXPR: /* + [c, c] */
4397 in_p = ! in_p, low = high = arg1;
4399 case GT_EXPR: /* - [-, c] */
4400 low = 0, high = arg1;
4402 case GE_EXPR: /* + [c, -] */
4403 in_p = ! in_p, low = arg1, high = 0;
4405 case LT_EXPR: /* - [c, -] */
4406 low = arg1, high = 0;
4408 case LE_EXPR: /* + [-, c] */
4409 in_p = ! in_p, low = 0, high = arg1;
4415 /* If this is an unsigned comparison, we also know that EXP is
4416 greater than or equal to zero. We base the range tests we make
4417 on that fact, so we record it here so we can parse existing
4418 range tests. We test arg0_type since often the return type
4419 of, e.g. EQ_EXPR, is boolean. */
4420 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4422 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4424 build_int_cst (arg0_type, 0),
4428 in_p = n_in_p, low = n_low, high = n_high;
4430 /* If the high bound is missing, but we have a nonzero low
4431 bound, reverse the range so it goes from zero to the low bound
4433 if (high == 0 && low && ! integer_zerop (low))
4436 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4437 integer_one_node, 0);
4438 low = build_int_cst (arg0_type, 0);
4446 /* (-x) IN [a,b] -> x in [-b, -a] */
4447 n_low = range_binop (MINUS_EXPR, exp_type,
4448 build_int_cst (exp_type, 0),
4450 n_high = range_binop (MINUS_EXPR, exp_type,
4451 build_int_cst (exp_type, 0),
4453 low = n_low, high = n_high;
4459 exp = build2 (MINUS_EXPR, exp_type, negate_expr (arg0),
4460 build_int_cst (exp_type, 1));
4463 case PLUS_EXPR: case MINUS_EXPR:
4464 if (TREE_CODE (arg1) != INTEGER_CST)
4467 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4468 move a constant to the other side. */
4469 if (!TYPE_UNSIGNED (arg0_type)
4470 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4473 /* If EXP is signed, any overflow in the computation is undefined,
4474 so we don't worry about it so long as our computations on
4475 the bounds don't overflow. For unsigned, overflow is defined
4476 and this is exactly the right thing. */
4477 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4478 arg0_type, low, 0, arg1, 0);
4479 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4480 arg0_type, high, 1, arg1, 0);
4481 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4482 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4485 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4486 *strict_overflow_p = true;
4488 /* Check for an unsigned range which has wrapped around the maximum
4489 value thus making n_high < n_low, and normalize it. */
4490 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4492 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4493 integer_one_node, 0);
4494 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4495 integer_one_node, 0);
4497 /* If the range is of the form +/- [ x+1, x ], we won't
4498 be able to normalize it. But then, it represents the
4499 whole range or the empty set, so make it
4501 if (tree_int_cst_equal (n_low, low)
4502 && tree_int_cst_equal (n_high, high))
4508 low = n_low, high = n_high;
4513 CASE_CONVERT: case NON_LVALUE_EXPR:
4514 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4517 if (! INTEGRAL_TYPE_P (arg0_type)
4518 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4519 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4522 n_low = low, n_high = high;
4525 n_low = fold_convert (arg0_type, n_low);
4528 n_high = fold_convert (arg0_type, n_high);
4531 /* If we're converting arg0 from an unsigned type, to exp,
4532 a signed type, we will be doing the comparison as unsigned.
4533 The tests above have already verified that LOW and HIGH
4536 So we have to ensure that we will handle large unsigned
4537 values the same way that the current signed bounds treat
4540 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4544 /* For fixed-point modes, we need to pass the saturating flag
4545 as the 2nd parameter. */
4546 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4547 equiv_type = lang_hooks.types.type_for_mode
4548 (TYPE_MODE (arg0_type),
4549 TYPE_SATURATING (arg0_type));
4551 equiv_type = lang_hooks.types.type_for_mode
4552 (TYPE_MODE (arg0_type), 1);
4554 /* A range without an upper bound is, naturally, unbounded.
4555 Since convert would have cropped a very large value, use
4556 the max value for the destination type. */
4558 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4559 : TYPE_MAX_VALUE (arg0_type);
4561 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4562 high_positive = fold_build2 (RSHIFT_EXPR, arg0_type,
4563 fold_convert (arg0_type,
4565 build_int_cst (arg0_type, 1));
4567 /* If the low bound is specified, "and" the range with the
4568 range for which the original unsigned value will be
4572 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4573 1, n_low, n_high, 1,
4574 fold_convert (arg0_type,
4579 in_p = (n_in_p == in_p);
4583 /* Otherwise, "or" the range with the range of the input
4584 that will be interpreted as negative. */
4585 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4586 0, n_low, n_high, 1,
4587 fold_convert (arg0_type,
4592 in_p = (in_p != n_in_p);
4597 low = n_low, high = n_high;
4607 /* If EXP is a constant, we can evaluate whether this is true or false. */
4608 if (TREE_CODE (exp) == INTEGER_CST)
4610 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4612 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4618 *pin_p = in_p, *plow = low, *phigh = high;
4622 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4623 type, TYPE, return an expression to test if EXP is in (or out of, depending
4624 on IN_P) the range. Return 0 if the test couldn't be created. */
4627 build_range_check (tree type, tree exp, int in_p, tree low, tree high)
4629 tree etype = TREE_TYPE (exp);
4632 #ifdef HAVE_canonicalize_funcptr_for_compare
4633 /* Disable this optimization for function pointer expressions
4634 on targets that require function pointer canonicalization. */
4635 if (HAVE_canonicalize_funcptr_for_compare
4636 && TREE_CODE (etype) == POINTER_TYPE
4637 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4643 value = build_range_check (type, exp, 1, low, high);
4645 return invert_truthvalue (value);
4650 if (low == 0 && high == 0)
4651 return build_int_cst (type, 1);
4654 return fold_build2 (LE_EXPR, type, exp,
4655 fold_convert (etype, high));
4658 return fold_build2 (GE_EXPR, type, exp,
4659 fold_convert (etype, low));
4661 if (operand_equal_p (low, high, 0))
4662 return fold_build2 (EQ_EXPR, type, exp,
4663 fold_convert (etype, low));
4665 if (integer_zerop (low))
4667 if (! TYPE_UNSIGNED (etype))
4669 etype = unsigned_type_for (etype);
4670 high = fold_convert (etype, high);
4671 exp = fold_convert (etype, exp);
4673 return build_range_check (type, exp, 1, 0, high);
4676 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4677 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4679 unsigned HOST_WIDE_INT lo;
4683 prec = TYPE_PRECISION (etype);
4684 if (prec <= HOST_BITS_PER_WIDE_INT)
4687 lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4691 hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4692 lo = (unsigned HOST_WIDE_INT) -1;
4695 if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4697 if (TYPE_UNSIGNED (etype))
4699 tree signed_etype = signed_type_for (etype);
4700 if (TYPE_PRECISION (signed_etype) != TYPE_PRECISION (etype))
4702 = build_nonstandard_integer_type (TYPE_PRECISION (etype), 0);
4704 etype = signed_etype;
4705 exp = fold_convert (etype, exp);
4707 return fold_build2 (GT_EXPR, type, exp,
4708 build_int_cst (etype, 0));
4712 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4713 This requires wrap-around arithmetics for the type of the expression. */
4714 switch (TREE_CODE (etype))
4717 /* There is no requirement that LOW be within the range of ETYPE
4718 if the latter is a subtype. It must, however, be within the base
4719 type of ETYPE. So be sure we do the subtraction in that type. */
4720 if (TREE_TYPE (etype))
4721 etype = TREE_TYPE (etype);
4726 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4727 TYPE_UNSIGNED (etype));
4734 /* If we don't have wrap-around arithmetics upfront, try to force it. */
4735 if (TREE_CODE (etype) == INTEGER_TYPE
4736 && !TYPE_OVERFLOW_WRAPS (etype))
4738 tree utype, minv, maxv;
4740 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4741 for the type in question, as we rely on this here. */
4742 utype = unsigned_type_for (etype);
4743 maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
4744 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4745 integer_one_node, 1);
4746 minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
4748 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4755 high = fold_convert (etype, high);
4756 low = fold_convert (etype, low);
4757 exp = fold_convert (etype, exp);
4759 value = const_binop (MINUS_EXPR, high, low, 0);
4762 if (POINTER_TYPE_P (etype))
4764 if (value != 0 && !TREE_OVERFLOW (value))
4766 low = fold_convert (sizetype, low);
4767 low = fold_build1 (NEGATE_EXPR, sizetype, low);
4768 return build_range_check (type,
4769 fold_build2 (POINTER_PLUS_EXPR, etype, exp, low),
4770 1, build_int_cst (etype, 0), value);
4775 if (value != 0 && !TREE_OVERFLOW (value))
4776 return build_range_check (type,
4777 fold_build2 (MINUS_EXPR, etype, exp, low),
4778 1, build_int_cst (etype, 0), value);
4783 /* Return the predecessor of VAL in its type, handling the infinite case. */
4786 range_predecessor (tree val)
4788 tree type = TREE_TYPE (val);
4790 if (INTEGRAL_TYPE_P (type)
4791 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4794 return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4797 /* Return the successor of VAL in its type, handling the infinite case. */
4800 range_successor (tree val)
4802 tree type = TREE_TYPE (val);
4804 if (INTEGRAL_TYPE_P (type)
4805 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4808 return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4811 /* Given two ranges, see if we can merge them into one. Return 1 if we
4812 can, 0 if we can't. Set the output range into the specified parameters. */
4815 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4816 tree high0, int in1_p, tree low1, tree high1)
4824 int lowequal = ((low0 == 0 && low1 == 0)
4825 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4826 low0, 0, low1, 0)));
4827 int highequal = ((high0 == 0 && high1 == 0)
4828 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4829 high0, 1, high1, 1)));
4831 /* Make range 0 be the range that starts first, or ends last if they
4832 start at the same value. Swap them if it isn't. */
4833 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4836 && integer_onep (range_binop (GT_EXPR, integer_type_node,
4837 high1, 1, high0, 1))))
4839 temp = in0_p, in0_p = in1_p, in1_p = temp;
4840 tem = low0, low0 = low1, low1 = tem;
4841 tem = high0, high0 = high1, high1 = tem;
4844 /* Now flag two cases, whether the ranges are disjoint or whether the
4845 second range is totally subsumed in the first. Note that the tests
4846 below are simplified by the ones above. */
4847 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4848 high0, 1, low1, 0));
4849 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4850 high1, 1, high0, 1));
4852 /* We now have four cases, depending on whether we are including or
4853 excluding the two ranges. */
4856 /* If they don't overlap, the result is false. If the second range
4857 is a subset it is the result. Otherwise, the range is from the start
4858 of the second to the end of the first. */
4860 in_p = 0, low = high = 0;
4862 in_p = 1, low = low1, high = high1;
4864 in_p = 1, low = low1, high = high0;
4867 else if (in0_p && ! in1_p)
4869 /* If they don't overlap, the result is the first range. If they are
4870 equal, the result is false. If the second range is a subset of the
4871 first, and the ranges begin at the same place, we go from just after
4872 the end of the second range to the end of the first. If the second
4873 range is not a subset of the first, or if it is a subset and both
4874 ranges end at the same place, the range starts at the start of the
4875 first range and ends just before the second range.
4876 Otherwise, we can't describe this as a single range. */
4878 in_p = 1, low = low0, high = high0;
4879 else if (lowequal && highequal)
4880 in_p = 0, low = high = 0;
4881 else if (subset && lowequal)
4883 low = range_successor (high1);
4888 /* We are in the weird situation where high0 > high1 but
4889 high1 has no successor. Punt. */
4893 else if (! subset || highequal)
4896 high = range_predecessor (low1);
4900 /* low0 < low1 but low1 has no predecessor. Punt. */
4908 else if (! in0_p && in1_p)
4910 /* If they don't overlap, the result is the second range. If the second
4911 is a subset of the first, the result is false. Otherwise,
4912 the range starts just after the first range and ends at the
4913 end of the second. */
4915 in_p = 1, low = low1, high = high1;
4916 else if (subset || highequal)
4917 in_p = 0, low = high = 0;
4920 low = range_successor (high0);
4925 /* high1 > high0 but high0 has no successor. Punt. */
4933 /* The case where we are excluding both ranges. Here the complex case
4934 is if they don't overlap. In that case, the only time we have a
4935 range is if they are adjacent. If the second is a subset of the
4936 first, the result is the first. Otherwise, the range to exclude
4937 starts at the beginning of the first range and ends at the end of the
4941 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4942 range_successor (high0),
4944 in_p = 0, low = low0, high = high1;
4947 /* Canonicalize - [min, x] into - [-, x]. */
4948 if (low0 && TREE_CODE (low0) == INTEGER_CST)
4949 switch (TREE_CODE (TREE_TYPE (low0)))
4952 if (TYPE_PRECISION (TREE_TYPE (low0))
4953 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4957 if (tree_int_cst_equal (low0,
4958 TYPE_MIN_VALUE (TREE_TYPE (low0))))
4962 if (TYPE_UNSIGNED (TREE_TYPE (low0))
4963 && integer_zerop (low0))
4970 /* Canonicalize - [x, max] into - [x, -]. */
4971 if (high1 && TREE_CODE (high1) == INTEGER_CST)
4972 switch (TREE_CODE (TREE_TYPE (high1)))
4975 if (TYPE_PRECISION (TREE_TYPE (high1))
4976 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4980 if (tree_int_cst_equal (high1,
4981 TYPE_MAX_VALUE (TREE_TYPE (high1))))
4985 if (TYPE_UNSIGNED (TREE_TYPE (high1))
4986 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4988 integer_one_node, 1)))
4995 /* The ranges might be also adjacent between the maximum and
4996 minimum values of the given type. For
4997 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4998 return + [x + 1, y - 1]. */
4999 if (low0 == 0 && high1 == 0)
5001 low = range_successor (high0);
5002 high = range_predecessor (low1);
5003 if (low == 0 || high == 0)
5013 in_p = 0, low = low0, high = high0;
5015 in_p = 0, low = low0, high = high1;
5018 *pin_p = in_p, *plow = low, *phigh = high;
5023 /* Subroutine of fold, looking inside expressions of the form
5024 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
5025 of the COND_EXPR. This function is being used also to optimize
5026 A op B ? C : A, by reversing the comparison first.
5028 Return a folded expression whose code is not a COND_EXPR
5029 anymore, or NULL_TREE if no folding opportunity is found. */
5032 fold_cond_expr_with_comparison (tree type, tree arg0, tree arg1, tree arg2)
5034 enum tree_code comp_code = TREE_CODE (arg0);
5035 tree arg00 = TREE_OPERAND (arg0, 0);
5036 tree arg01 = TREE_OPERAND (arg0, 1);
5037 tree arg1_type = TREE_TYPE (arg1);
5043 /* If we have A op 0 ? A : -A, consider applying the following
5046 A == 0? A : -A same as -A
5047 A != 0? A : -A same as A
5048 A >= 0? A : -A same as abs (A)
5049 A > 0? A : -A same as abs (A)
5050 A <= 0? A : -A same as -abs (A)
5051 A < 0? A : -A same as -abs (A)
5053 None of these transformations work for modes with signed
5054 zeros. If A is +/-0, the first two transformations will
5055 change the sign of the result (from +0 to -0, or vice
5056 versa). The last four will fix the sign of the result,
5057 even though the original expressions could be positive or
5058 negative, depending on the sign of A.
5060 Note that all these transformations are correct if A is
5061 NaN, since the two alternatives (A and -A) are also NaNs. */
5062 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5063 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
5064 ? real_zerop (arg01)
5065 : integer_zerop (arg01))
5066 && ((TREE_CODE (arg2) == NEGATE_EXPR
5067 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
5068 /* In the case that A is of the form X-Y, '-A' (arg2) may
5069 have already been folded to Y-X, check for that. */
5070 || (TREE_CODE (arg1) == MINUS_EXPR
5071 && TREE_CODE (arg2) == MINUS_EXPR
5072 && operand_equal_p (TREE_OPERAND (arg1, 0),
5073 TREE_OPERAND (arg2, 1), 0)
5074 && operand_equal_p (TREE_OPERAND (arg1, 1),
5075 TREE_OPERAND (arg2, 0), 0))))
5080 tem = fold_convert (arg1_type, arg1);
5081 return pedantic_non_lvalue (fold_convert (type, negate_expr (tem)));
5084 return pedantic_non_lvalue (fold_convert (type, arg1));
5087 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 pedantic_non_lvalue (fold_convert (type, tem));
5099 if (flag_trapping_math)
5103 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
5104 arg1 = fold_convert (signed_type_for
5105 (TREE_TYPE (arg1)), arg1);
5106 tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
5107 return negate_expr (fold_convert (type, tem));
5109 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5113 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
5114 A == 0 ? A : 0 is always 0 unless A is -0. Note that
5115 both transformations are correct when A is NaN: A != 0
5116 is then true, and A == 0 is false. */
5118 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5119 && integer_zerop (arg01) && integer_zerop (arg2))
5121 if (comp_code == NE_EXPR)
5122 return pedantic_non_lvalue (fold_convert (type, arg1));
5123 else if (comp_code == EQ_EXPR)
5124 return build_int_cst (type, 0);
5127 /* Try some transformations of A op B ? A : B.
5129 A == B? A : B same as B
5130 A != B? A : B same as A
5131 A >= B? A : B same as max (A, B)
5132 A > B? A : B same as max (B, A)
5133 A <= B? A : B same as min (A, B)
5134 A < B? A : B same as min (B, A)
5136 As above, these transformations don't work in the presence
5137 of signed zeros. For example, if A and B are zeros of
5138 opposite sign, the first two transformations will change
5139 the sign of the result. In the last four, the original
5140 expressions give different results for (A=+0, B=-0) and
5141 (A=-0, B=+0), but the transformed expressions do not.
5143 The first two transformations are correct if either A or B
5144 is a NaN. In the first transformation, the condition will
5145 be false, and B will indeed be chosen. In the case of the
5146 second transformation, the condition A != B will be true,
5147 and A will be chosen.
5149 The conversions to max() and min() are not correct if B is
5150 a number and A is not. The conditions in the original
5151 expressions will be false, so all four give B. The min()
5152 and max() versions would give a NaN instead. */
5153 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
5154 && operand_equal_for_comparison_p (arg01, arg2, arg00)
5155 /* Avoid these transformations if the COND_EXPR may be used
5156 as an lvalue in the C++ front-end. PR c++/19199. */
5158 || (strcmp (lang_hooks.name, "GNU C++") != 0
5159 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
5160 || ! maybe_lvalue_p (arg1)
5161 || ! maybe_lvalue_p (arg2)))
5163 tree comp_op0 = arg00;
5164 tree comp_op1 = arg01;
5165 tree comp_type = TREE_TYPE (comp_op0);
5167 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
5168 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
5178 return pedantic_non_lvalue (fold_convert (type, arg2));
5180 return pedantic_non_lvalue (fold_convert (type, arg1));
5185 /* In C++ a ?: expression can be an lvalue, so put the
5186 operand which will be used if they are equal first
5187 so that we can convert this back to the
5188 corresponding COND_EXPR. */
5189 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5191 comp_op0 = fold_convert (comp_type, comp_op0);
5192 comp_op1 = fold_convert (comp_type, comp_op1);
5193 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
5194 ? fold_build2 (MIN_EXPR, comp_type, comp_op0, comp_op1)
5195 : fold_build2 (MIN_EXPR, comp_type, comp_op1, comp_op0);
5196 return pedantic_non_lvalue (fold_convert (type, tem));
5203 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5205 comp_op0 = fold_convert (comp_type, comp_op0);
5206 comp_op1 = fold_convert (comp_type, comp_op1);
5207 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5208 ? fold_build2 (MAX_EXPR, comp_type, comp_op0, comp_op1)
5209 : fold_build2 (MAX_EXPR, comp_type, comp_op1, comp_op0);
5210 return pedantic_non_lvalue (fold_convert (type, tem));
5214 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5215 return pedantic_non_lvalue (fold_convert (type, arg2));
5218 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5219 return pedantic_non_lvalue (fold_convert (type, arg1));
5222 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5227 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5228 we might still be able to simplify this. For example,
5229 if C1 is one less or one more than C2, this might have started
5230 out as a MIN or MAX and been transformed by this function.
5231 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
5233 if (INTEGRAL_TYPE_P (type)
5234 && TREE_CODE (arg01) == INTEGER_CST
5235 && TREE_CODE (arg2) == INTEGER_CST)
5239 /* We can replace A with C1 in this case. */
5240 arg1 = fold_convert (type, arg01);
5241 return fold_build3 (COND_EXPR, type, arg0, arg1, arg2);
5244 /* If C1 is C2 + 1, this is min(A, C2). */
5245 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5247 && operand_equal_p (arg01,
5248 const_binop (PLUS_EXPR, arg2,
5249 build_int_cst (type, 1), 0),
5251 return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
5253 fold_convert (type, arg1),
5258 /* If C1 is C2 - 1, this is min(A, C2). */
5259 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5261 && operand_equal_p (arg01,
5262 const_binop (MINUS_EXPR, arg2,
5263 build_int_cst (type, 1), 0),
5265 return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
5267 fold_convert (type, arg1),
5272 /* If C1 is C2 - 1, this is max(A, C2). */
5273 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5275 && operand_equal_p (arg01,
5276 const_binop (MINUS_EXPR, arg2,
5277 build_int_cst (type, 1), 0),
5279 return pedantic_non_lvalue (fold_build2 (MAX_EXPR,
5281 fold_convert (type, arg1),
5286 /* If C1 is C2 + 1, this is max(A, C2). */
5287 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5289 && operand_equal_p (arg01,
5290 const_binop (PLUS_EXPR, arg2,
5291 build_int_cst (type, 1), 0),
5293 return pedantic_non_lvalue (fold_build2 (MAX_EXPR,
5295 fold_convert (type, arg1),
5309 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5310 #define LOGICAL_OP_NON_SHORT_CIRCUIT \
5311 (BRANCH_COST (optimize_function_for_speed_p (cfun), \
5315 /* EXP is some logical combination of boolean tests. See if we can
5316 merge it into some range test. Return the new tree if so. */
5319 fold_range_test (enum tree_code code, tree type, tree op0, tree op1)
5321 int or_op = (code == TRUTH_ORIF_EXPR
5322 || code == TRUTH_OR_EXPR);
5323 int in0_p, in1_p, in_p;
5324 tree low0, low1, low, high0, high1, high;
5325 bool strict_overflow_p = false;
5326 tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5327 tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5329 const char * const warnmsg = G_("assuming signed overflow does not occur "
5330 "when simplifying range test");
5332 /* If this is an OR operation, invert both sides; we will invert
5333 again at the end. */
5335 in0_p = ! in0_p, in1_p = ! in1_p;
5337 /* If both expressions are the same, if we can merge the ranges, and we
5338 can build the range test, return it or it inverted. If one of the
5339 ranges is always true or always false, consider it to be the same
5340 expression as the other. */
5341 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5342 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5344 && 0 != (tem = (build_range_check (type,
5346 : rhs != 0 ? rhs : integer_zero_node,
5349 if (strict_overflow_p)
5350 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5351 return or_op ? invert_truthvalue (tem) : tem;
5354 /* On machines where the branch cost is expensive, if this is a
5355 short-circuited branch and the underlying object on both sides
5356 is the same, make a non-short-circuit operation. */
5357 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5358 && lhs != 0 && rhs != 0
5359 && (code == TRUTH_ANDIF_EXPR
5360 || code == TRUTH_ORIF_EXPR)
5361 && operand_equal_p (lhs, rhs, 0))
5363 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5364 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5365 which cases we can't do this. */
5366 if (simple_operand_p (lhs))
5367 return build2 (code == TRUTH_ANDIF_EXPR
5368 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5371 else if (lang_hooks.decls.global_bindings_p () == 0
5372 && ! CONTAINS_PLACEHOLDER_P (lhs))
5374 tree common = save_expr (lhs);
5376 if (0 != (lhs = build_range_check (type, common,
5377 or_op ? ! in0_p : in0_p,
5379 && (0 != (rhs = build_range_check (type, common,
5380 or_op ? ! in1_p : in1_p,
5383 if (strict_overflow_p)
5384 fold_overflow_warning (warnmsg,
5385 WARN_STRICT_OVERFLOW_COMPARISON);
5386 return build2 (code == TRUTH_ANDIF_EXPR
5387 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5396 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
5397 bit value. Arrange things so the extra bits will be set to zero if and
5398 only if C is signed-extended to its full width. If MASK is nonzero,
5399 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5402 unextend (tree c, int p, int unsignedp, tree mask)
5404 tree type = TREE_TYPE (c);
5405 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5408 if (p == modesize || unsignedp)
5411 /* We work by getting just the sign bit into the low-order bit, then
5412 into the high-order bit, then sign-extend. We then XOR that value
5414 temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
5415 temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
5417 /* We must use a signed type in order to get an arithmetic right shift.
5418 However, we must also avoid introducing accidental overflows, so that
5419 a subsequent call to integer_zerop will work. Hence we must
5420 do the type conversion here. At this point, the constant is either
5421 zero or one, and the conversion to a signed type can never overflow.
5422 We could get an overflow if this conversion is done anywhere else. */
5423 if (TYPE_UNSIGNED (type))
5424 temp = fold_convert (signed_type_for (type), temp);
5426 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
5427 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
5429 temp = const_binop (BIT_AND_EXPR, temp,
5430 fold_convert (TREE_TYPE (c), mask), 0);
5431 /* If necessary, convert the type back to match the type of C. */
5432 if (TYPE_UNSIGNED (type))
5433 temp = fold_convert (type, temp);
5435 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
5438 /* Find ways of folding logical expressions of LHS and RHS:
5439 Try to merge two comparisons to the same innermost item.
5440 Look for range tests like "ch >= '0' && ch <= '9'".
5441 Look for combinations of simple terms on machines with expensive branches
5442 and evaluate the RHS unconditionally.
5444 For example, if we have p->a == 2 && p->b == 4 and we can make an
5445 object large enough to span both A and B, we can do this with a comparison
5446 against the object ANDed with the a mask.
5448 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5449 operations to do this with one comparison.
5451 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5452 function and the one above.
5454 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5455 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5457 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5460 We return the simplified tree or 0 if no optimization is possible. */
5463 fold_truthop (enum tree_code code, tree truth_type, tree lhs, tree rhs)
5465 /* If this is the "or" of two comparisons, we can do something if
5466 the comparisons are NE_EXPR. If this is the "and", we can do something
5467 if the comparisons are EQ_EXPR. I.e.,
5468 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5470 WANTED_CODE is this operation code. For single bit fields, we can
5471 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5472 comparison for one-bit fields. */
5474 enum tree_code wanted_code;
5475 enum tree_code lcode, rcode;
5476 tree ll_arg, lr_arg, rl_arg, rr_arg;
5477 tree ll_inner, lr_inner, rl_inner, rr_inner;
5478 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5479 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5480 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
5481 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
5482 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5483 enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5484 enum machine_mode lnmode, rnmode;
5485 tree ll_mask, lr_mask, rl_mask, rr_mask;
5486 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5487 tree l_const, r_const;
5488 tree lntype, rntype, result;
5489 HOST_WIDE_INT first_bit, end_bit;
5491 tree orig_lhs = lhs, orig_rhs = rhs;
5492 enum tree_code orig_code = code;
5494 /* Start by getting the comparison codes. Fail if anything is volatile.
5495 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5496 it were surrounded with a NE_EXPR. */
5498 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5501 lcode = TREE_CODE (lhs);
5502 rcode = TREE_CODE (rhs);
5504 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5506 lhs = build2 (NE_EXPR, truth_type, lhs,
5507 build_int_cst (TREE_TYPE (lhs), 0));
5511 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5513 rhs = build2 (NE_EXPR, truth_type, rhs,
5514 build_int_cst (TREE_TYPE (rhs), 0));
5518 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5519 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5522 ll_arg = TREE_OPERAND (lhs, 0);
5523 lr_arg = TREE_OPERAND (lhs, 1);
5524 rl_arg = TREE_OPERAND (rhs, 0);
5525 rr_arg = TREE_OPERAND (rhs, 1);
5527 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5528 if (simple_operand_p (ll_arg)
5529 && simple_operand_p (lr_arg))
5532 if (operand_equal_p (ll_arg, rl_arg, 0)
5533 && operand_equal_p (lr_arg, rr_arg, 0))
5535 result = combine_comparisons (code, lcode, rcode,
5536 truth_type, ll_arg, lr_arg);
5540 else if (operand_equal_p (ll_arg, rr_arg, 0)
5541 && operand_equal_p (lr_arg, rl_arg, 0))
5543 result = combine_comparisons (code, lcode,
5544 swap_tree_comparison (rcode),
5545 truth_type, ll_arg, lr_arg);
5551 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5552 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5554 /* If the RHS can be evaluated unconditionally and its operands are
5555 simple, it wins to evaluate the RHS unconditionally on machines
5556 with expensive branches. In this case, this isn't a comparison
5557 that can be merged. Avoid doing this if the RHS is a floating-point
5558 comparison since those can trap. */
5560 if (BRANCH_COST (optimize_function_for_speed_p (cfun),
5562 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5563 && simple_operand_p (rl_arg)
5564 && simple_operand_p (rr_arg))
5566 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5567 if (code == TRUTH_OR_EXPR
5568 && lcode == NE_EXPR && integer_zerop (lr_arg)
5569 && rcode == NE_EXPR && integer_zerop (rr_arg)
5570 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5571 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5572 return build2 (NE_EXPR, truth_type,
5573 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5575 build_int_cst (TREE_TYPE (ll_arg), 0));
5577 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5578 if (code == TRUTH_AND_EXPR
5579 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5580 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5581 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5582 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5583 return build2 (EQ_EXPR, truth_type,
5584 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5586 build_int_cst (TREE_TYPE (ll_arg), 0));
5588 if (LOGICAL_OP_NON_SHORT_CIRCUIT)
5590 if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs)
5591 return build2 (code, truth_type, lhs, rhs);
5596 /* See if the comparisons can be merged. Then get all the parameters for
5599 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5600 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5604 ll_inner = decode_field_reference (ll_arg,
5605 &ll_bitsize, &ll_bitpos, &ll_mode,
5606 &ll_unsignedp, &volatilep, &ll_mask,
5608 lr_inner = decode_field_reference (lr_arg,
5609 &lr_bitsize, &lr_bitpos, &lr_mode,
5610 &lr_unsignedp, &volatilep, &lr_mask,
5612 rl_inner = decode_field_reference (rl_arg,
5613 &rl_bitsize, &rl_bitpos, &rl_mode,
5614 &rl_unsignedp, &volatilep, &rl_mask,
5616 rr_inner = decode_field_reference (rr_arg,
5617 &rr_bitsize, &rr_bitpos, &rr_mode,
5618 &rr_unsignedp, &volatilep, &rr_mask,
5621 /* It must be true that the inner operation on the lhs of each
5622 comparison must be the same if we are to be able to do anything.
5623 Then see if we have constants. If not, the same must be true for
5625 if (volatilep || ll_inner == 0 || rl_inner == 0
5626 || ! operand_equal_p (ll_inner, rl_inner, 0))
5629 if (TREE_CODE (lr_arg) == INTEGER_CST
5630 && TREE_CODE (rr_arg) == INTEGER_CST)
5631 l_const = lr_arg, r_const = rr_arg;
5632 else if (lr_inner == 0 || rr_inner == 0
5633 || ! operand_equal_p (lr_inner, rr_inner, 0))
5636 l_const = r_const = 0;
5638 /* If either comparison code is not correct for our logical operation,
5639 fail. However, we can convert a one-bit comparison against zero into
5640 the opposite comparison against that bit being set in the field. */
5642 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5643 if (lcode != wanted_code)
5645 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5647 /* Make the left operand unsigned, since we are only interested
5648 in the value of one bit. Otherwise we are doing the wrong
5657 /* This is analogous to the code for l_const above. */
5658 if (rcode != wanted_code)
5660 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5669 /* See if we can find a mode that contains both fields being compared on
5670 the left. If we can't, fail. Otherwise, update all constants and masks
5671 to be relative to a field of that size. */
5672 first_bit = MIN (ll_bitpos, rl_bitpos);
5673 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5674 lnmode = get_best_mode (end_bit - first_bit, first_bit,
5675 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5677 if (lnmode == VOIDmode)
5680 lnbitsize = GET_MODE_BITSIZE (lnmode);
5681 lnbitpos = first_bit & ~ (lnbitsize - 1);
5682 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5683 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5685 if (BYTES_BIG_ENDIAN)
5687 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5688 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5691 ll_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, ll_mask),
5692 size_int (xll_bitpos), 0);
5693 rl_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, rl_mask),
5694 size_int (xrl_bitpos), 0);
5698 l_const = fold_convert (lntype, l_const);
5699 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5700 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
5701 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5702 fold_build1 (BIT_NOT_EXPR,
5706 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5708 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5713 r_const = fold_convert (lntype, r_const);
5714 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5715 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
5716 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5717 fold_build1 (BIT_NOT_EXPR,
5721 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5723 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5727 /* If the right sides are not constant, do the same for it. Also,
5728 disallow this optimization if a size or signedness mismatch occurs
5729 between the left and right sides. */
5732 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
5733 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
5734 /* Make sure the two fields on the right
5735 correspond to the left without being swapped. */
5736 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
5739 first_bit = MIN (lr_bitpos, rr_bitpos);
5740 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
5741 rnmode = get_best_mode (end_bit - first_bit, first_bit,
5742 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
5744 if (rnmode == VOIDmode)
5747 rnbitsize = GET_MODE_BITSIZE (rnmode);
5748 rnbitpos = first_bit & ~ (rnbitsize - 1);
5749 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
5750 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
5752 if (BYTES_BIG_ENDIAN)
5754 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
5755 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
5758 lr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, lr_mask),
5759 size_int (xlr_bitpos), 0);
5760 rr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, rr_mask),
5761 size_int (xrr_bitpos), 0);
5763 /* Make a mask that corresponds to both fields being compared.
5764 Do this for both items being compared. If the operands are the
5765 same size and the bits being compared are in the same position
5766 then we can do this by masking both and comparing the masked
5768 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
5769 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
5770 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
5772 lhs = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
5773 ll_unsignedp || rl_unsignedp);
5774 if (! all_ones_mask_p (ll_mask, lnbitsize))
5775 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
5777 rhs = make_bit_field_ref (lr_inner, rntype, rnbitsize, rnbitpos,
5778 lr_unsignedp || rr_unsignedp);
5779 if (! all_ones_mask_p (lr_mask, rnbitsize))
5780 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
5782 return build2 (wanted_code, truth_type, lhs, rhs);
5785 /* There is still another way we can do something: If both pairs of
5786 fields being compared are adjacent, we may be able to make a wider
5787 field containing them both.
5789 Note that we still must mask the lhs/rhs expressions. Furthermore,
5790 the mask must be shifted to account for the shift done by
5791 make_bit_field_ref. */
5792 if ((ll_bitsize + ll_bitpos == rl_bitpos
5793 && lr_bitsize + lr_bitpos == rr_bitpos)
5794 || (ll_bitpos == rl_bitpos + rl_bitsize
5795 && lr_bitpos == rr_bitpos + rr_bitsize))
5799 lhs = make_bit_field_ref (ll_inner, lntype, ll_bitsize + rl_bitsize,
5800 MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
5801 rhs = make_bit_field_ref (lr_inner, rntype, lr_bitsize + rr_bitsize,
5802 MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
5804 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
5805 size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
5806 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
5807 size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
5809 /* Convert to the smaller type before masking out unwanted bits. */
5811 if (lntype != rntype)
5813 if (lnbitsize > rnbitsize)
5815 lhs = fold_convert (rntype, lhs);
5816 ll_mask = fold_convert (rntype, ll_mask);
5819 else if (lnbitsize < rnbitsize)
5821 rhs = fold_convert (lntype, rhs);
5822 lr_mask = fold_convert (lntype, lr_mask);
5827 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
5828 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
5830 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
5831 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
5833 return build2 (wanted_code, truth_type, lhs, rhs);
5839 /* Handle the case of comparisons with constants. If there is something in
5840 common between the masks, those bits of the constants must be the same.
5841 If not, the condition is always false. Test for this to avoid generating
5842 incorrect code below. */
5843 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
5844 if (! integer_zerop (result)
5845 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
5846 const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
5848 if (wanted_code == NE_EXPR)
5850 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5851 return constant_boolean_node (true, truth_type);
5855 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5856 return constant_boolean_node (false, truth_type);
5860 /* Construct the expression we will return. First get the component
5861 reference we will make. Unless the mask is all ones the width of
5862 that field, perform the mask operation. Then compare with the
5864 result = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
5865 ll_unsignedp || rl_unsignedp);
5867 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
5868 if (! all_ones_mask_p (ll_mask, lnbitsize))
5869 result = build2 (BIT_AND_EXPR, lntype, result, ll_mask);
5871 return build2 (wanted_code, truth_type, result,
5872 const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
5875 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5879 optimize_minmax_comparison (enum tree_code code, tree type, tree op0, tree op1)
5882 enum tree_code op_code;
5885 int consts_equal, consts_lt;
5888 STRIP_SIGN_NOPS (arg0);
5890 op_code = TREE_CODE (arg0);
5891 minmax_const = TREE_OPERAND (arg0, 1);
5892 comp_const = fold_convert (TREE_TYPE (arg0), op1);
5893 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5894 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5895 inner = TREE_OPERAND (arg0, 0);
5897 /* If something does not permit us to optimize, return the original tree. */
5898 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5899 || TREE_CODE (comp_const) != INTEGER_CST
5900 || TREE_OVERFLOW (comp_const)
5901 || TREE_CODE (minmax_const) != INTEGER_CST
5902 || TREE_OVERFLOW (minmax_const))
5905 /* Now handle all the various comparison codes. We only handle EQ_EXPR
5906 and GT_EXPR, doing the rest with recursive calls using logical
5910 case NE_EXPR: case LT_EXPR: case LE_EXPR:
5912 tree tem = optimize_minmax_comparison (invert_tree_comparison (code, false),
5915 return invert_truthvalue (tem);
5921 fold_build2 (TRUTH_ORIF_EXPR, type,
5922 optimize_minmax_comparison
5923 (EQ_EXPR, type, arg0, comp_const),
5924 optimize_minmax_comparison
5925 (GT_EXPR, type, arg0, comp_const));
5928 if (op_code == MAX_EXPR && consts_equal)
5929 /* MAX (X, 0) == 0 -> X <= 0 */
5930 return fold_build2 (LE_EXPR, type, inner, comp_const);
5932 else if (op_code == MAX_EXPR && consts_lt)
5933 /* MAX (X, 0) == 5 -> X == 5 */
5934 return fold_build2 (EQ_EXPR, type, inner, comp_const);
5936 else if (op_code == MAX_EXPR)
5937 /* MAX (X, 0) == -1 -> false */
5938 return omit_one_operand (type, integer_zero_node, inner);
5940 else if (consts_equal)
5941 /* MIN (X, 0) == 0 -> X >= 0 */
5942 return fold_build2 (GE_EXPR, type, inner, comp_const);
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 (EQ_EXPR, type, inner, comp_const);
5953 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5954 /* MAX (X, 0) > 0 -> X > 0
5955 MAX (X, 0) > 5 -> X > 5 */
5956 return fold_build2 (GT_EXPR, type, inner, comp_const);
5958 else if (op_code == MAX_EXPR)
5959 /* MAX (X, 0) > -1 -> true */
5960 return omit_one_operand (type, integer_one_node, inner);
5962 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5963 /* MIN (X, 0) > 0 -> false
5964 MIN (X, 0) > 5 -> false */
5965 return omit_one_operand (type, integer_zero_node, inner);
5968 /* MIN (X, 0) > -1 -> X > -1 */
5969 return fold_build2 (GT_EXPR, type, inner, comp_const);
5976 /* T is an integer expression that is being multiplied, divided, or taken a
5977 modulus (CODE says which and what kind of divide or modulus) by a
5978 constant C. See if we can eliminate that operation by folding it with
5979 other operations already in T. WIDE_TYPE, if non-null, is a type that
5980 should be used for the computation if wider than our type.
5982 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5983 (X * 2) + (Y * 4). We must, however, be assured that either the original
5984 expression would not overflow or that overflow is undefined for the type
5985 in the language in question.
5987 If we return a non-null expression, it is an equivalent form of the
5988 original computation, but need not be in the original type.
5990 We set *STRICT_OVERFLOW_P to true if the return values depends on
5991 signed overflow being undefined. Otherwise we do not change
5992 *STRICT_OVERFLOW_P. */
5995 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
5996 bool *strict_overflow_p)
5998 /* To avoid exponential search depth, refuse to allow recursion past
5999 three levels. Beyond that (1) it's highly unlikely that we'll find
6000 something interesting and (2) we've probably processed it before
6001 when we built the inner expression. */
6010 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
6017 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
6018 bool *strict_overflow_p)
6020 tree type = TREE_TYPE (t);
6021 enum tree_code tcode = TREE_CODE (t);
6022 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
6023 > GET_MODE_SIZE (TYPE_MODE (type)))
6024 ? wide_type : type);
6026 int same_p = tcode == code;
6027 tree op0 = NULL_TREE, op1 = NULL_TREE;
6028 bool sub_strict_overflow_p;
6030 /* Don't deal with constants of zero here; they confuse the code below. */
6031 if (integer_zerop (c))
6034 if (TREE_CODE_CLASS (tcode) == tcc_unary)
6035 op0 = TREE_OPERAND (t, 0);
6037 if (TREE_CODE_CLASS (tcode) == tcc_binary)
6038 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
6040 /* Note that we need not handle conditional operations here since fold
6041 already handles those cases. So just do arithmetic here. */
6045 /* For a constant, we can always simplify if we are a multiply
6046 or (for divide and modulus) if it is a multiple of our constant. */
6047 if (code == MULT_EXPR
6048 || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
6049 return const_binop (code, fold_convert (ctype, t),
6050 fold_convert (ctype, c), 0);
6053 CASE_CONVERT: case NON_LVALUE_EXPR:
6054 /* If op0 is an expression ... */
6055 if ((COMPARISON_CLASS_P (op0)
6056 || UNARY_CLASS_P (op0)
6057 || BINARY_CLASS_P (op0)
6058 || VL_EXP_CLASS_P (op0)
6059 || EXPRESSION_CLASS_P (op0))
6060 /* ... and has wrapping overflow, and its type is smaller
6061 than ctype, then we cannot pass through as widening. */
6062 && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
6063 && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
6064 && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
6065 && (TYPE_PRECISION (ctype)
6066 > TYPE_PRECISION (TREE_TYPE (op0))))
6067 /* ... or this is a truncation (t is narrower than op0),
6068 then we cannot pass through this narrowing. */
6069 || (TYPE_PRECISION (type)
6070 < TYPE_PRECISION (TREE_TYPE (op0)))
6071 /* ... or signedness changes for division or modulus,
6072 then we cannot pass through this conversion. */
6073 || (code != MULT_EXPR
6074 && (TYPE_UNSIGNED (ctype)
6075 != TYPE_UNSIGNED (TREE_TYPE (op0))))
6076 /* ... or has undefined overflow while the converted to
6077 type has not, we cannot do the operation in the inner type
6078 as that would introduce undefined overflow. */
6079 || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
6080 && !TYPE_OVERFLOW_UNDEFINED (type))))
6083 /* Pass the constant down and see if we can make a simplification. If
6084 we can, replace this expression with the inner simplification for
6085 possible later conversion to our or some other type. */
6086 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
6087 && TREE_CODE (t2) == INTEGER_CST
6088 && !TREE_OVERFLOW (t2)
6089 && (0 != (t1 = extract_muldiv (op0, t2, code,
6091 ? ctype : NULL_TREE,
6092 strict_overflow_p))))
6097 /* If widening the type changes it from signed to unsigned, then we
6098 must avoid building ABS_EXPR itself as unsigned. */
6099 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
6101 tree cstype = (*signed_type_for) (ctype);
6102 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
6105 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
6106 return fold_convert (ctype, t1);
6110 /* If the constant is negative, we cannot simplify this. */
6111 if (tree_int_cst_sgn (c) == -1)
6115 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
6117 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
6120 case MIN_EXPR: case MAX_EXPR:
6121 /* If widening the type changes the signedness, then we can't perform
6122 this optimization as that changes the result. */
6123 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
6126 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
6127 sub_strict_overflow_p = false;
6128 if ((t1 = extract_muldiv (op0, c, code, wide_type,
6129 &sub_strict_overflow_p)) != 0
6130 && (t2 = extract_muldiv (op1, c, code, wide_type,
6131 &sub_strict_overflow_p)) != 0)
6133 if (tree_int_cst_sgn (c) < 0)
6134 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
6135 if (sub_strict_overflow_p)
6136 *strict_overflow_p = true;
6137 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6138 fold_convert (ctype, t2));
6142 case LSHIFT_EXPR: case RSHIFT_EXPR:
6143 /* If the second operand is constant, this is a multiplication
6144 or floor division, by a power of two, so we can treat it that
6145 way unless the multiplier or divisor overflows. Signed
6146 left-shift overflow is implementation-defined rather than
6147 undefined in C90, so do not convert signed left shift into
6149 if (TREE_CODE (op1) == INTEGER_CST
6150 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
6151 /* const_binop may not detect overflow correctly,
6152 so check for it explicitly here. */
6153 && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
6154 && TREE_INT_CST_HIGH (op1) == 0
6155 && 0 != (t1 = fold_convert (ctype,
6156 const_binop (LSHIFT_EXPR,
6159 && !TREE_OVERFLOW (t1))
6160 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
6161 ? MULT_EXPR : FLOOR_DIV_EXPR,
6162 ctype, fold_convert (ctype, op0), t1),
6163 c, code, wide_type, strict_overflow_p);
6166 case PLUS_EXPR: case MINUS_EXPR:
6167 /* See if we can eliminate the operation on both sides. If we can, we
6168 can return a new PLUS or MINUS. If we can't, the only remaining
6169 cases where we can do anything are if the second operand is a
6171 sub_strict_overflow_p = false;
6172 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
6173 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
6174 if (t1 != 0 && t2 != 0
6175 && (code == MULT_EXPR
6176 /* If not multiplication, we can only do this if both operands
6177 are divisible by c. */
6178 || (multiple_of_p (ctype, op0, c)
6179 && multiple_of_p (ctype, op1, c))))
6181 if (sub_strict_overflow_p)
6182 *strict_overflow_p = true;
6183 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6184 fold_convert (ctype, t2));
6187 /* If this was a subtraction, negate OP1 and set it to be an addition.
6188 This simplifies the logic below. */
6189 if (tcode == MINUS_EXPR)
6190 tcode = PLUS_EXPR, op1 = negate_expr (op1);
6192 if (TREE_CODE (op1) != INTEGER_CST)
6195 /* If either OP1 or C are negative, this optimization is not safe for
6196 some of the division and remainder types while for others we need
6197 to change the code. */
6198 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
6200 if (code == CEIL_DIV_EXPR)
6201 code = FLOOR_DIV_EXPR;
6202 else if (code == FLOOR_DIV_EXPR)
6203 code = CEIL_DIV_EXPR;
6204 else if (code != MULT_EXPR
6205 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
6209 /* If it's a multiply or a division/modulus operation of a multiple
6210 of our constant, do the operation and verify it doesn't overflow. */
6211 if (code == MULT_EXPR
6212 || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6214 op1 = const_binop (code, fold_convert (ctype, op1),
6215 fold_convert (ctype, c), 0);
6216 /* We allow the constant to overflow with wrapping semantics. */
6218 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
6224 /* If we have an unsigned type is not a sizetype, we cannot widen
6225 the operation since it will change the result if the original
6226 computation overflowed. */
6227 if (TYPE_UNSIGNED (ctype)
6228 && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
6232 /* If we were able to eliminate our operation from the first side,
6233 apply our operation to the second side and reform the PLUS. */
6234 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
6235 return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
6237 /* The last case is if we are a multiply. In that case, we can
6238 apply the distributive law to commute the multiply and addition
6239 if the multiplication of the constants doesn't overflow. */
6240 if (code == MULT_EXPR)
6241 return fold_build2 (tcode, ctype,
6242 fold_build2 (code, ctype,
6243 fold_convert (ctype, op0),
6244 fold_convert (ctype, c)),
6250 /* We have a special case here if we are doing something like
6251 (C * 8) % 4 since we know that's zero. */
6252 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
6253 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
6254 /* If the multiplication can overflow we cannot optimize this.
6255 ??? Until we can properly mark individual operations as
6256 not overflowing we need to treat sizetype special here as
6257 stor-layout relies on this opimization to make
6258 DECL_FIELD_BIT_OFFSET always a constant. */
6259 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
6260 || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
6261 && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
6262 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
6263 && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6265 *strict_overflow_p = true;
6266 return omit_one_operand (type, integer_zero_node, op0);
6269 /* ... fall through ... */
6271 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
6272 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
6273 /* If we can extract our operation from the LHS, do so and return a
6274 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
6275 do something only if the second operand is a constant. */
6277 && (t1 = extract_muldiv (op0, c, code, wide_type,
6278 strict_overflow_p)) != 0)
6279 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
6280 fold_convert (ctype, op1));
6281 else if (tcode == MULT_EXPR && code == MULT_EXPR
6282 && (t1 = extract_muldiv (op1, c, code, wide_type,
6283 strict_overflow_p)) != 0)
6284 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6285 fold_convert (ctype, t1));
6286 else if (TREE_CODE (op1) != INTEGER_CST)
6289 /* If these are the same operation types, we can associate them
6290 assuming no overflow. */
6292 && 0 != (t1 = int_const_binop (MULT_EXPR, fold_convert (ctype, op1),
6293 fold_convert (ctype, c), 1))
6294 && 0 != (t1 = force_fit_type_double (ctype, TREE_INT_CST_LOW (t1),
6295 TREE_INT_CST_HIGH (t1),
6296 (TYPE_UNSIGNED (ctype)
6297 && tcode != MULT_EXPR) ? -1 : 1,
6298 TREE_OVERFLOW (t1)))
6299 && !TREE_OVERFLOW (t1))
6300 return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1);
6302 /* If these operations "cancel" each other, we have the main
6303 optimizations of this pass, which occur when either constant is a
6304 multiple of the other, in which case we replace this with either an
6305 operation or CODE or TCODE.
6307 If we have an unsigned type that is not a sizetype, we cannot do
6308 this since it will change the result if the original computation
6310 if ((TYPE_OVERFLOW_UNDEFINED (ctype)
6311 || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
6312 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
6313 || (tcode == MULT_EXPR
6314 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
6315 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
6316 && code != MULT_EXPR)))
6318 if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
6320 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6321 *strict_overflow_p = true;
6322 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6323 fold_convert (ctype,
6324 const_binop (TRUNC_DIV_EXPR,
6327 else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
6329 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6330 *strict_overflow_p = true;
6331 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6332 fold_convert (ctype,
6333 const_binop (TRUNC_DIV_EXPR,
6346 /* Return a node which has the indicated constant VALUE (either 0 or
6347 1), and is of the indicated TYPE. */
6350 constant_boolean_node (int value, tree type)
6352 if (type == integer_type_node)
6353 return value ? integer_one_node : integer_zero_node;
6354 else if (type == boolean_type_node)
6355 return value ? boolean_true_node : boolean_false_node;
6357 return build_int_cst (type, value);
6361 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6362 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6363 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6364 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6365 COND is the first argument to CODE; otherwise (as in the example
6366 given here), it is the second argument. TYPE is the type of the
6367 original expression. Return NULL_TREE if no simplification is
6371 fold_binary_op_with_conditional_arg (enum tree_code code,
6372 tree type, tree op0, tree op1,
6373 tree cond, tree arg, int cond_first_p)
6375 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6376 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6377 tree test, true_value, false_value;
6378 tree lhs = NULL_TREE;
6379 tree rhs = NULL_TREE;
6381 /* This transformation is only worthwhile if we don't have to wrap
6382 arg in a SAVE_EXPR, and the operation can be simplified on at least
6383 one of the branches once its pushed inside the COND_EXPR. */
6384 if (!TREE_CONSTANT (arg))
6387 if (TREE_CODE (cond) == COND_EXPR)
6389 test = TREE_OPERAND (cond, 0);
6390 true_value = TREE_OPERAND (cond, 1);
6391 false_value = TREE_OPERAND (cond, 2);
6392 /* If this operand throws an expression, then it does not make
6393 sense to try to perform a logical or arithmetic operation
6395 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6397 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6402 tree testtype = TREE_TYPE (cond);
6404 true_value = constant_boolean_node (true, testtype);
6405 false_value = constant_boolean_node (false, testtype);
6408 arg = fold_convert (arg_type, arg);
6411 true_value = fold_convert (cond_type, true_value);
6413 lhs = fold_build2 (code, type, true_value, arg);
6415 lhs = fold_build2 (code, type, arg, true_value);
6419 false_value = fold_convert (cond_type, false_value);
6421 rhs = fold_build2 (code, type, false_value, arg);
6423 rhs = fold_build2 (code, type, arg, false_value);
6426 test = fold_build3 (COND_EXPR, type, test, lhs, rhs);
6427 return fold_convert (type, test);
6431 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6433 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6434 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6435 ADDEND is the same as X.
6437 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6438 and finite. The problematic cases are when X is zero, and its mode
6439 has signed zeros. In the case of rounding towards -infinity,
6440 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6441 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6444 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6446 if (!real_zerop (addend))
6449 /* Don't allow the fold with -fsignaling-nans. */
6450 if (HONOR_SNANS (TYPE_MODE (type)))
6453 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6454 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6457 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6458 if (TREE_CODE (addend) == REAL_CST
6459 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6462 /* The mode has signed zeros, and we have to honor their sign.
6463 In this situation, there is only one case we can return true for.
6464 X - 0 is the same as X unless rounding towards -infinity is
6466 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6469 /* Subroutine of fold() that checks comparisons of built-in math
6470 functions against real constants.
6472 FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6473 operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR. TYPE
6474 is the type of the result and ARG0 and ARG1 are the operands of the
6475 comparison. ARG1 must be a TREE_REAL_CST.
6477 The function returns the constant folded tree if a simplification
6478 can be made, and NULL_TREE otherwise. */
6481 fold_mathfn_compare (enum built_in_function fcode, enum tree_code code,
6482 tree type, tree arg0, tree arg1)
6486 if (BUILTIN_SQRT_P (fcode))
6488 tree arg = CALL_EXPR_ARG (arg0, 0);
6489 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6491 c = TREE_REAL_CST (arg1);
6492 if (REAL_VALUE_NEGATIVE (c))
6494 /* sqrt(x) < y is always false, if y is negative. */
6495 if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6496 return omit_one_operand (type, integer_zero_node, arg);
6498 /* sqrt(x) > y is always true, if y is negative and we
6499 don't care about NaNs, i.e. negative values of x. */
6500 if (code == NE_EXPR || !HONOR_NANS (mode))
6501 return omit_one_operand (type, integer_one_node, arg);
6503 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
6504 return fold_build2 (GE_EXPR, type, arg,
6505 build_real (TREE_TYPE (arg), dconst0));
6507 else if (code == GT_EXPR || code == GE_EXPR)
6511 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6512 real_convert (&c2, mode, &c2);
6514 if (REAL_VALUE_ISINF (c2))
6516 /* sqrt(x) > y is x == +Inf, when y is very large. */
6517 if (HONOR_INFINITIES (mode))
6518 return fold_build2 (EQ_EXPR, type, arg,
6519 build_real (TREE_TYPE (arg), c2));
6521 /* sqrt(x) > y is always false, when y is very large
6522 and we don't care about infinities. */
6523 return omit_one_operand (type, integer_zero_node, arg);
6526 /* sqrt(x) > c is the same as x > c*c. */
6527 return fold_build2 (code, type, arg,
6528 build_real (TREE_TYPE (arg), c2));
6530 else if (code == LT_EXPR || code == LE_EXPR)
6534 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6535 real_convert (&c2, mode, &c2);
6537 if (REAL_VALUE_ISINF (c2))
6539 /* sqrt(x) < y is always true, when y is a very large
6540 value and we don't care about NaNs or Infinities. */
6541 if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6542 return omit_one_operand (type, integer_one_node, arg);
6544 /* sqrt(x) < y is x != +Inf when y is very large and we
6545 don't care about NaNs. */
6546 if (! HONOR_NANS (mode))
6547 return fold_build2 (NE_EXPR, type, arg,
6548 build_real (TREE_TYPE (arg), c2));
6550 /* sqrt(x) < y is x >= 0 when y is very large and we
6551 don't care about Infinities. */
6552 if (! HONOR_INFINITIES (mode))
6553 return fold_build2 (GE_EXPR, type, arg,
6554 build_real (TREE_TYPE (arg), dconst0));
6556 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
6557 if (lang_hooks.decls.global_bindings_p () != 0
6558 || CONTAINS_PLACEHOLDER_P (arg))
6561 arg = save_expr (arg);
6562 return fold_build2 (TRUTH_ANDIF_EXPR, type,
6563 fold_build2 (GE_EXPR, type, arg,
6564 build_real (TREE_TYPE (arg),
6566 fold_build2 (NE_EXPR, type, arg,
6567 build_real (TREE_TYPE (arg),
6571 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
6572 if (! HONOR_NANS (mode))
6573 return fold_build2 (code, type, arg,
6574 build_real (TREE_TYPE (arg), c2));
6576 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
6577 if (lang_hooks.decls.global_bindings_p () == 0
6578 && ! CONTAINS_PLACEHOLDER_P (arg))
6580 arg = save_expr (arg);
6581 return fold_build2 (TRUTH_ANDIF_EXPR, type,
6582 fold_build2 (GE_EXPR, type, arg,
6583 build_real (TREE_TYPE (arg),
6585 fold_build2 (code, type, arg,
6586 build_real (TREE_TYPE (arg),
6595 /* Subroutine of fold() that optimizes comparisons against Infinities,
6596 either +Inf or -Inf.
6598 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6599 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6600 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6602 The function returns the constant folded tree if a simplification
6603 can be made, and NULL_TREE otherwise. */
6606 fold_inf_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6608 enum machine_mode mode;
6609 REAL_VALUE_TYPE max;
6613 mode = TYPE_MODE (TREE_TYPE (arg0));
6615 /* For negative infinity swap the sense of the comparison. */
6616 neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6618 code = swap_tree_comparison (code);
6623 /* x > +Inf is always false, if with ignore sNANs. */
6624 if (HONOR_SNANS (mode))
6626 return omit_one_operand (type, integer_zero_node, arg0);
6629 /* x <= +Inf is always true, if we don't case about NaNs. */
6630 if (! HONOR_NANS (mode))
6631 return omit_one_operand (type, integer_one_node, arg0);
6633 /* x <= +Inf is the same as x == x, i.e. isfinite(x). */
6634 if (lang_hooks.decls.global_bindings_p () == 0
6635 && ! CONTAINS_PLACEHOLDER_P (arg0))
6637 arg0 = save_expr (arg0);
6638 return fold_build2 (EQ_EXPR, type, arg0, arg0);
6644 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
6645 real_maxval (&max, neg, mode);
6646 return fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6647 arg0, build_real (TREE_TYPE (arg0), max));
6650 /* x < +Inf is always equal to x <= DBL_MAX. */
6651 real_maxval (&max, neg, mode);
6652 return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6653 arg0, build_real (TREE_TYPE (arg0), max));
6656 /* x != +Inf is always equal to !(x > DBL_MAX). */
6657 real_maxval (&max, neg, mode);
6658 if (! HONOR_NANS (mode))
6659 return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6660 arg0, build_real (TREE_TYPE (arg0), max));
6662 temp = fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6663 arg0, build_real (TREE_TYPE (arg0), max));
6664 return fold_build1 (TRUTH_NOT_EXPR, type, temp);
6673 /* Subroutine of fold() that optimizes comparisons of a division by
6674 a nonzero integer constant against an integer constant, i.e.
6677 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6678 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6679 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6681 The function returns the constant folded tree if a simplification
6682 can be made, and NULL_TREE otherwise. */
6685 fold_div_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6687 tree prod, tmp, hi, lo;
6688 tree arg00 = TREE_OPERAND (arg0, 0);
6689 tree arg01 = TREE_OPERAND (arg0, 1);
6690 unsigned HOST_WIDE_INT lpart;
6691 HOST_WIDE_INT hpart;
6692 bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
6696 /* We have to do this the hard way to detect unsigned overflow.
6697 prod = int_const_binop (MULT_EXPR, arg01, arg1, 0); */
6698 overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
6699 TREE_INT_CST_HIGH (arg01),
6700 TREE_INT_CST_LOW (arg1),
6701 TREE_INT_CST_HIGH (arg1),
6702 &lpart, &hpart, unsigned_p);
6703 prod = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6705 neg_overflow = false;
6709 tmp = int_const_binop (MINUS_EXPR, arg01,
6710 build_int_cst (TREE_TYPE (arg01), 1), 0);
6713 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0). */
6714 overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
6715 TREE_INT_CST_HIGH (prod),
6716 TREE_INT_CST_LOW (tmp),
6717 TREE_INT_CST_HIGH (tmp),
6718 &lpart, &hpart, unsigned_p);
6719 hi = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6720 -1, overflow | TREE_OVERFLOW (prod));
6722 else if (tree_int_cst_sgn (arg01) >= 0)
6724 tmp = int_const_binop (MINUS_EXPR, arg01,
6725 build_int_cst (TREE_TYPE (arg01), 1), 0);
6726 switch (tree_int_cst_sgn (arg1))
6729 neg_overflow = true;
6730 lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6735 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6740 hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6750 /* A negative divisor reverses the relational operators. */
6751 code = swap_tree_comparison (code);
6753 tmp = int_const_binop (PLUS_EXPR, arg01,
6754 build_int_cst (TREE_TYPE (arg01), 1), 0);
6755 switch (tree_int_cst_sgn (arg1))
6758 hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6763 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6768 neg_overflow = true;
6769 lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6781 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6782 return omit_one_operand (type, integer_zero_node, arg00);
6783 if (TREE_OVERFLOW (hi))
6784 return fold_build2 (GE_EXPR, type, arg00, lo);
6785 if (TREE_OVERFLOW (lo))
6786 return fold_build2 (LE_EXPR, type, arg00, hi);
6787 return build_range_check (type, arg00, 1, lo, hi);
6790 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6791 return omit_one_operand (type, integer_one_node, arg00);
6792 if (TREE_OVERFLOW (hi))
6793 return fold_build2 (LT_EXPR, type, arg00, lo);
6794 if (TREE_OVERFLOW (lo))
6795 return fold_build2 (GT_EXPR, type, arg00, hi);
6796 return build_range_check (type, arg00, 0, lo, hi);
6799 if (TREE_OVERFLOW (lo))
6801 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6802 return omit_one_operand (type, tmp, arg00);
6804 return fold_build2 (LT_EXPR, type, arg00, lo);
6807 if (TREE_OVERFLOW (hi))
6809 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6810 return omit_one_operand (type, tmp, arg00);
6812 return fold_build2 (LE_EXPR, type, arg00, hi);
6815 if (TREE_OVERFLOW (hi))
6817 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6818 return omit_one_operand (type, tmp, arg00);
6820 return fold_build2 (GT_EXPR, type, arg00, hi);
6823 if (TREE_OVERFLOW (lo))
6825 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6826 return omit_one_operand (type, tmp, arg00);
6828 return fold_build2 (GE_EXPR, type, arg00, lo);
6838 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6839 equality/inequality test, then return a simplified form of the test
6840 using a sign testing. Otherwise return NULL. TYPE is the desired
6844 fold_single_bit_test_into_sign_test (enum tree_code code, tree arg0, tree arg1,
6847 /* If this is testing a single bit, we can optimize the test. */
6848 if ((code == NE_EXPR || code == EQ_EXPR)
6849 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6850 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6852 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6853 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6854 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6856 if (arg00 != NULL_TREE
6857 /* This is only a win if casting to a signed type is cheap,
6858 i.e. when arg00's type is not a partial mode. */
6859 && TYPE_PRECISION (TREE_TYPE (arg00))
6860 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
6862 tree stype = signed_type_for (TREE_TYPE (arg00));
6863 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6864 result_type, fold_convert (stype, arg00),
6865 build_int_cst (stype, 0));
6872 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6873 equality/inequality test, then return a simplified form of
6874 the test using shifts and logical operations. Otherwise return
6875 NULL. TYPE is the desired result type. */
6878 fold_single_bit_test (enum tree_code code, tree arg0, tree arg1,
6881 /* If this is testing a single bit, we can optimize the test. */
6882 if ((code == NE_EXPR || code == EQ_EXPR)
6883 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6884 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6886 tree inner = TREE_OPERAND (arg0, 0);
6887 tree type = TREE_TYPE (arg0);
6888 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6889 enum machine_mode operand_mode = TYPE_MODE (type);
6891 tree signed_type, unsigned_type, intermediate_type;
6894 /* First, see if we can fold the single bit test into a sign-bit
6896 tem = fold_single_bit_test_into_sign_test (code, arg0, arg1,
6901 /* Otherwise we have (A & C) != 0 where C is a single bit,
6902 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6903 Similarly for (A & C) == 0. */
6905 /* If INNER is a right shift of a constant and it plus BITNUM does
6906 not overflow, adjust BITNUM and INNER. */
6907 if (TREE_CODE (inner) == RSHIFT_EXPR
6908 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6909 && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
6910 && bitnum < TYPE_PRECISION (type)
6911 && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
6912 bitnum - TYPE_PRECISION (type)))
6914 bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
6915 inner = TREE_OPERAND (inner, 0);
6918 /* If we are going to be able to omit the AND below, we must do our
6919 operations as unsigned. If we must use the AND, we have a choice.
6920 Normally unsigned is faster, but for some machines signed is. */
6921 #ifdef LOAD_EXTEND_OP
6922 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6923 && !flag_syntax_only) ? 0 : 1;
6928 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6929 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6930 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6931 inner = fold_convert (intermediate_type, inner);
6934 inner = build2 (RSHIFT_EXPR, intermediate_type,
6935 inner, size_int (bitnum));
6937 one = build_int_cst (intermediate_type, 1);
6939 if (code == EQ_EXPR)
6940 inner = fold_build2 (BIT_XOR_EXPR, intermediate_type, inner, one);
6942 /* Put the AND last so it can combine with more things. */
6943 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6945 /* Make sure to return the proper type. */
6946 inner = fold_convert (result_type, inner);
6953 /* Check whether we are allowed to reorder operands arg0 and arg1,
6954 such that the evaluation of arg1 occurs before arg0. */
6957 reorder_operands_p (const_tree arg0, const_tree arg1)
6959 if (! flag_evaluation_order)
6961 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6963 return ! TREE_SIDE_EFFECTS (arg0)
6964 && ! TREE_SIDE_EFFECTS (arg1);
6967 /* Test whether it is preferable two swap two operands, ARG0 and
6968 ARG1, for example because ARG0 is an integer constant and ARG1
6969 isn't. If REORDER is true, only recommend swapping if we can
6970 evaluate the operands in reverse order. */
6973 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6975 STRIP_SIGN_NOPS (arg0);
6976 STRIP_SIGN_NOPS (arg1);
6978 if (TREE_CODE (arg1) == INTEGER_CST)
6980 if (TREE_CODE (arg0) == INTEGER_CST)
6983 if (TREE_CODE (arg1) == REAL_CST)
6985 if (TREE_CODE (arg0) == REAL_CST)
6988 if (TREE_CODE (arg1) == FIXED_CST)
6990 if (TREE_CODE (arg0) == FIXED_CST)
6993 if (TREE_CODE (arg1) == COMPLEX_CST)
6995 if (TREE_CODE (arg0) == COMPLEX_CST)
6998 if (TREE_CONSTANT (arg1))
7000 if (TREE_CONSTANT (arg0))
7003 if (optimize_function_for_size_p (cfun))
7006 if (reorder && flag_evaluation_order
7007 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
7010 /* It is preferable to swap two SSA_NAME to ensure a canonical form
7011 for commutative and comparison operators. Ensuring a canonical
7012 form allows the optimizers to find additional redundancies without
7013 having to explicitly check for both orderings. */
7014 if (TREE_CODE (arg0) == SSA_NAME
7015 && TREE_CODE (arg1) == SSA_NAME
7016 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
7019 /* Put SSA_NAMEs last. */
7020 if (TREE_CODE (arg1) == SSA_NAME)
7022 if (TREE_CODE (arg0) == SSA_NAME)
7025 /* Put variables last. */
7034 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
7035 ARG0 is extended to a wider type. */
7038 fold_widened_comparison (enum tree_code code, tree type, tree arg0, tree arg1)
7040 tree arg0_unw = get_unwidened (arg0, NULL_TREE);
7042 tree shorter_type, outer_type;
7046 if (arg0_unw == arg0)
7048 shorter_type = TREE_TYPE (arg0_unw);
7050 #ifdef HAVE_canonicalize_funcptr_for_compare
7051 /* Disable this optimization if we're casting a function pointer
7052 type on targets that require function pointer canonicalization. */
7053 if (HAVE_canonicalize_funcptr_for_compare
7054 && TREE_CODE (shorter_type) == POINTER_TYPE
7055 && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
7059 if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
7062 arg1_unw = get_unwidened (arg1, NULL_TREE);
7064 /* If possible, express the comparison in the shorter mode. */
7065 if ((code == EQ_EXPR || code == NE_EXPR
7066 || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
7067 && (TREE_TYPE (arg1_unw) == shorter_type
7068 || ((TYPE_PRECISION (shorter_type)
7069 >= TYPE_PRECISION (TREE_TYPE (arg1_unw)))
7070 && (TYPE_UNSIGNED (shorter_type)
7071 == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
7072 || (TREE_CODE (arg1_unw) == INTEGER_CST
7073 && (TREE_CODE (shorter_type) == INTEGER_TYPE
7074 || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
7075 && int_fits_type_p (arg1_unw, shorter_type))))
7076 return fold_build2 (code, type, arg0_unw,
7077 fold_convert (shorter_type, arg1_unw));
7079 if (TREE_CODE (arg1_unw) != INTEGER_CST
7080 || TREE_CODE (shorter_type) != INTEGER_TYPE
7081 || !int_fits_type_p (arg1_unw, shorter_type))
7084 /* If we are comparing with the integer that does not fit into the range
7085 of the shorter type, the result is known. */
7086 outer_type = TREE_TYPE (arg1_unw);
7087 min = lower_bound_in_type (outer_type, shorter_type);
7088 max = upper_bound_in_type (outer_type, shorter_type);
7090 above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7092 below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
7099 return omit_one_operand (type, integer_zero_node, arg0);
7104 return omit_one_operand (type, integer_one_node, arg0);
7110 return omit_one_operand (type, integer_one_node, arg0);
7112 return omit_one_operand (type, integer_zero_node, arg0);
7117 return omit_one_operand (type, integer_zero_node, arg0);
7119 return omit_one_operand (type, integer_one_node, arg0);
7128 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
7129 ARG0 just the signedness is changed. */
7132 fold_sign_changed_comparison (enum tree_code code, tree type,
7133 tree arg0, tree arg1)
7136 tree inner_type, outer_type;
7138 if (!CONVERT_EXPR_P (arg0))
7141 outer_type = TREE_TYPE (arg0);
7142 arg0_inner = TREE_OPERAND (arg0, 0);
7143 inner_type = TREE_TYPE (arg0_inner);
7145 #ifdef HAVE_canonicalize_funcptr_for_compare
7146 /* Disable this optimization if we're casting a function pointer
7147 type on targets that require function pointer canonicalization. */
7148 if (HAVE_canonicalize_funcptr_for_compare
7149 && TREE_CODE (inner_type) == POINTER_TYPE
7150 && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
7154 if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
7157 /* If the conversion is from an integral subtype to its basetype
7159 if (TREE_TYPE (inner_type) == outer_type)
7162 if (TREE_CODE (arg1) != INTEGER_CST
7163 && !(CONVERT_EXPR_P (arg1)
7164 && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
7167 if ((TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
7168 || POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type))
7173 if (TREE_CODE (arg1) == INTEGER_CST)
7174 arg1 = force_fit_type_double (inner_type, TREE_INT_CST_LOW (arg1),
7175 TREE_INT_CST_HIGH (arg1), 0,
7176 TREE_OVERFLOW (arg1));
7178 arg1 = fold_convert (inner_type, arg1);
7180 return fold_build2 (code, type, arg0_inner, arg1);
7183 /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
7184 step of the array. Reconstructs s and delta in the case of s * delta
7185 being an integer constant (and thus already folded).
7186 ADDR is the address. MULT is the multiplicative expression.
7187 If the function succeeds, the new address expression is returned. Otherwise
7188 NULL_TREE is returned. */
7191 try_move_mult_to_index (tree addr, tree op1)
7193 tree s, delta, step;
7194 tree ref = TREE_OPERAND (addr, 0), pref;
7199 /* Strip the nops that might be added when converting op1 to sizetype. */
7202 /* Canonicalize op1 into a possibly non-constant delta
7203 and an INTEGER_CST s. */
7204 if (TREE_CODE (op1) == MULT_EXPR)
7206 tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
7211 if (TREE_CODE (arg0) == INTEGER_CST)
7216 else if (TREE_CODE (arg1) == INTEGER_CST)
7224 else if (TREE_CODE (op1) == INTEGER_CST)
7231 /* Simulate we are delta * 1. */
7233 s = integer_one_node;
7236 for (;; ref = TREE_OPERAND (ref, 0))
7238 if (TREE_CODE (ref) == ARRAY_REF)
7240 /* Remember if this was a multi-dimensional array. */
7241 if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
7244 itype = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
7248 step = array_ref_element_size (ref);
7249 if (TREE_CODE (step) != INTEGER_CST)
7254 if (! tree_int_cst_equal (step, s))
7259 /* Try if delta is a multiple of step. */
7260 tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step);
7266 /* Only fold here if we can verify we do not overflow one
7267 dimension of a multi-dimensional array. */
7272 if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
7273 || !INTEGRAL_TYPE_P (itype)
7274 || !TYPE_MAX_VALUE (itype)
7275 || TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST)
7278 tmp = fold_binary (PLUS_EXPR, itype,
7279 fold_convert (itype,
7280 TREE_OPERAND (ref, 1)),
7281 fold_convert (itype, delta));
7283 || TREE_CODE (tmp) != INTEGER_CST
7284 || tree_int_cst_lt (TYPE_MAX_VALUE (itype), tmp))
7293 if (!handled_component_p (ref))
7297 /* We found the suitable array reference. So copy everything up to it,
7298 and replace the index. */
7300 pref = TREE_OPERAND (addr, 0);
7301 ret = copy_node (pref);
7306 pref = TREE_OPERAND (pref, 0);
7307 TREE_OPERAND (pos, 0) = copy_node (pref);
7308 pos = TREE_OPERAND (pos, 0);
7311 TREE_OPERAND (pos, 1) = fold_build2 (PLUS_EXPR, itype,
7312 fold_convert (itype,
7313 TREE_OPERAND (pos, 1)),
7314 fold_convert (itype, delta));
7316 return fold_build1 (ADDR_EXPR, TREE_TYPE (addr), ret);
7320 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
7321 means A >= Y && A != MAX, but in this case we know that
7322 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7325 fold_to_nonsharp_ineq_using_bound (tree ineq, tree bound)
7327 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
7329 if (TREE_CODE (bound) == LT_EXPR)
7330 a = TREE_OPERAND (bound, 0);
7331 else if (TREE_CODE (bound) == GT_EXPR)
7332 a = TREE_OPERAND (bound, 1);
7336 typea = TREE_TYPE (a);
7337 if (!INTEGRAL_TYPE_P (typea)
7338 && !POINTER_TYPE_P (typea))
7341 if (TREE_CODE (ineq) == LT_EXPR)
7343 a1 = TREE_OPERAND (ineq, 1);
7344 y = TREE_OPERAND (ineq, 0);
7346 else if (TREE_CODE (ineq) == GT_EXPR)
7348 a1 = TREE_OPERAND (ineq, 0);
7349 y = TREE_OPERAND (ineq, 1);
7354 if (TREE_TYPE (a1) != typea)
7357 if (POINTER_TYPE_P (typea))
7359 /* Convert the pointer types into integer before taking the difference. */
7360 tree ta = fold_convert (ssizetype, a);
7361 tree ta1 = fold_convert (ssizetype, a1);
7362 diff = fold_binary (MINUS_EXPR, ssizetype, ta1, ta);
7365 diff = fold_binary (MINUS_EXPR, typea, a1, a);
7367 if (!diff || !integer_onep (diff))
7370 return fold_build2 (GE_EXPR, type, a, y);
7373 /* Fold a sum or difference of at least one multiplication.
7374 Returns the folded tree or NULL if no simplification could be made. */
7377 fold_plusminus_mult_expr (enum tree_code code, tree type, tree arg0, tree arg1)
7379 tree arg00, arg01, arg10, arg11;
7380 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7382 /* (A * C) +- (B * C) -> (A+-B) * C.
7383 (A * C) +- A -> A * (C+-1).
7384 We are most concerned about the case where C is a constant,
7385 but other combinations show up during loop reduction. Since
7386 it is not difficult, try all four possibilities. */
7388 if (TREE_CODE (arg0) == MULT_EXPR)
7390 arg00 = TREE_OPERAND (arg0, 0);
7391 arg01 = TREE_OPERAND (arg0, 1);
7393 else if (TREE_CODE (arg0) == INTEGER_CST)
7395 arg00 = build_one_cst (type);
7400 /* We cannot generate constant 1 for fract. */
7401 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7404 arg01 = build_one_cst (type);
7406 if (TREE_CODE (arg1) == MULT_EXPR)
7408 arg10 = TREE_OPERAND (arg1, 0);
7409 arg11 = TREE_OPERAND (arg1, 1);
7411 else if (TREE_CODE (arg1) == INTEGER_CST)
7413 arg10 = build_one_cst (type);
7414 /* As we canonicalize A - 2 to A + -2 get rid of that sign for
7415 the purpose of this canonicalization. */
7416 if (TREE_INT_CST_HIGH (arg1) == -1
7417 && negate_expr_p (arg1)
7418 && code == PLUS_EXPR)
7420 arg11 = negate_expr (arg1);
7428 /* We cannot generate constant 1 for fract. */
7429 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7432 arg11 = build_one_cst (type);
7436 if (operand_equal_p (arg01, arg11, 0))
7437 same = arg01, alt0 = arg00, alt1 = arg10;
7438 else if (operand_equal_p (arg00, arg10, 0))
7439 same = arg00, alt0 = arg01, alt1 = arg11;
7440 else if (operand_equal_p (arg00, arg11, 0))
7441 same = arg00, alt0 = arg01, alt1 = arg10;
7442 else if (operand_equal_p (arg01, arg10, 0))
7443 same = arg01, alt0 = arg00, alt1 = arg11;
7445 /* No identical multiplicands; see if we can find a common
7446 power-of-two factor in non-power-of-two multiplies. This
7447 can help in multi-dimensional array access. */
7448 else if (host_integerp (arg01, 0)
7449 && host_integerp (arg11, 0))
7451 HOST_WIDE_INT int01, int11, tmp;
7454 int01 = TREE_INT_CST_LOW (arg01);
7455 int11 = TREE_INT_CST_LOW (arg11);
7457 /* Move min of absolute values to int11. */
7458 if ((int01 >= 0 ? int01 : -int01)
7459 < (int11 >= 0 ? int11 : -int11))
7461 tmp = int01, int01 = int11, int11 = tmp;
7462 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7469 if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0)
7471 alt0 = fold_build2 (MULT_EXPR, TREE_TYPE (arg00), arg00,
7472 build_int_cst (TREE_TYPE (arg00),
7477 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7482 return fold_build2 (MULT_EXPR, type,
7483 fold_build2 (code, type,
7484 fold_convert (type, alt0),
7485 fold_convert (type, alt1)),
7486 fold_convert (type, same));
7491 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7492 specified by EXPR into the buffer PTR of length LEN bytes.
7493 Return the number of bytes placed in the buffer, or zero
7497 native_encode_int (const_tree expr, unsigned char *ptr, int len)
7499 tree type = TREE_TYPE (expr);
7500 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7501 int byte, offset, word, words;
7502 unsigned char value;
7504 if (total_bytes > len)
7506 words = total_bytes / UNITS_PER_WORD;
7508 for (byte = 0; byte < total_bytes; byte++)
7510 int bitpos = byte * BITS_PER_UNIT;
7511 if (bitpos < HOST_BITS_PER_WIDE_INT)
7512 value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7514 value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7515 >> (bitpos - HOST_BITS_PER_WIDE_INT));
7517 if (total_bytes > UNITS_PER_WORD)
7519 word = byte / UNITS_PER_WORD;
7520 if (WORDS_BIG_ENDIAN)
7521 word = (words - 1) - word;
7522 offset = word * UNITS_PER_WORD;
7523 if (BYTES_BIG_ENDIAN)
7524 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7526 offset += byte % UNITS_PER_WORD;
7529 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7530 ptr[offset] = value;
7536 /* Subroutine of native_encode_expr. Encode the REAL_CST
7537 specified by EXPR into the buffer PTR of length LEN bytes.
7538 Return the number of bytes placed in the buffer, or zero
7542 native_encode_real (const_tree expr, unsigned char *ptr, int len)
7544 tree type = TREE_TYPE (expr);
7545 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7546 int byte, offset, word, words, bitpos;
7547 unsigned char value;
7549 /* There are always 32 bits in each long, no matter the size of
7550 the hosts long. We handle floating point representations with
7554 if (total_bytes > len)
7556 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7558 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7560 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7561 bitpos += BITS_PER_UNIT)
7563 byte = (bitpos / BITS_PER_UNIT) & 3;
7564 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7566 if (UNITS_PER_WORD < 4)
7568 word = byte / UNITS_PER_WORD;
7569 if (WORDS_BIG_ENDIAN)
7570 word = (words - 1) - word;
7571 offset = word * UNITS_PER_WORD;
7572 if (BYTES_BIG_ENDIAN)
7573 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7575 offset += byte % UNITS_PER_WORD;
7578 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7579 ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
7584 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7585 specified by EXPR into the buffer PTR of length LEN bytes.
7586 Return the number of bytes placed in the buffer, or zero
7590 native_encode_complex (const_tree expr, unsigned char *ptr, int len)
7595 part = TREE_REALPART (expr);
7596 rsize = native_encode_expr (part, ptr, len);
7599 part = TREE_IMAGPART (expr);
7600 isize = native_encode_expr (part, ptr+rsize, len-rsize);
7603 return rsize + isize;
7607 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7608 specified by EXPR into the buffer PTR of length LEN bytes.
7609 Return the number of bytes placed in the buffer, or zero
7613 native_encode_vector (const_tree expr, unsigned char *ptr, int len)
7615 int i, size, offset, count;
7616 tree itype, elem, elements;
7619 elements = TREE_VECTOR_CST_ELTS (expr);
7620 count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
7621 itype = TREE_TYPE (TREE_TYPE (expr));
7622 size = GET_MODE_SIZE (TYPE_MODE (itype));
7623 for (i = 0; i < count; i++)
7627 elem = TREE_VALUE (elements);
7628 elements = TREE_CHAIN (elements);
7635 if (native_encode_expr (elem, ptr+offset, len-offset) != size)
7640 if (offset + size > len)
7642 memset (ptr+offset, 0, size);
7650 /* Subroutine of native_encode_expr. Encode the STRING_CST
7651 specified by EXPR into the buffer PTR of length LEN bytes.
7652 Return the number of bytes placed in the buffer, or zero
7656 native_encode_string (const_tree expr, unsigned char *ptr, int len)
7658 tree type = TREE_TYPE (expr);
7659 HOST_WIDE_INT total_bytes;
7661 if (TREE_CODE (type) != ARRAY_TYPE
7662 || TREE_CODE (TREE_TYPE (type)) != INTEGER_TYPE
7663 || GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (type))) != BITS_PER_UNIT
7664 || !host_integerp (TYPE_SIZE_UNIT (type), 0))
7666 total_bytes = tree_low_cst (TYPE_SIZE_UNIT (type), 0);
7667 if (total_bytes > len)
7669 if (TREE_STRING_LENGTH (expr) < total_bytes)
7671 memcpy (ptr, TREE_STRING_POINTER (expr), TREE_STRING_LENGTH (expr));
7672 memset (ptr + TREE_STRING_LENGTH (expr), 0,
7673 total_bytes - TREE_STRING_LENGTH (expr));
7676 memcpy (ptr, TREE_STRING_POINTER (expr), total_bytes);
7681 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7682 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7683 buffer PTR of length LEN bytes. Return the number of bytes
7684 placed in the buffer, or zero upon failure. */
7687 native_encode_expr (const_tree expr, unsigned char *ptr, int len)
7689 switch (TREE_CODE (expr))
7692 return native_encode_int (expr, ptr, len);
7695 return native_encode_real (expr, ptr, len);
7698 return native_encode_complex (expr, ptr, len);
7701 return native_encode_vector (expr, ptr, len);
7704 return native_encode_string (expr, ptr, len);
7712 /* Subroutine of native_interpret_expr. Interpret the contents of
7713 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7714 If the buffer cannot be interpreted, return NULL_TREE. */
7717 native_interpret_int (tree type, const unsigned char *ptr, int len)
7719 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7720 int byte, offset, word, words;
7721 unsigned char value;
7722 unsigned int HOST_WIDE_INT lo = 0;
7723 HOST_WIDE_INT hi = 0;
7725 if (total_bytes > len)
7727 if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
7729 words = total_bytes / UNITS_PER_WORD;
7731 for (byte = 0; byte < total_bytes; byte++)
7733 int bitpos = byte * BITS_PER_UNIT;
7734 if (total_bytes > UNITS_PER_WORD)
7736 word = byte / UNITS_PER_WORD;
7737 if (WORDS_BIG_ENDIAN)
7738 word = (words - 1) - word;
7739 offset = word * UNITS_PER_WORD;
7740 if (BYTES_BIG_ENDIAN)
7741 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7743 offset += byte % UNITS_PER_WORD;
7746 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7747 value = ptr[offset];
7749 if (bitpos < HOST_BITS_PER_WIDE_INT)
7750 lo |= (unsigned HOST_WIDE_INT) value << bitpos;
7752 hi |= (unsigned HOST_WIDE_INT) value
7753 << (bitpos - HOST_BITS_PER_WIDE_INT);
7756 return build_int_cst_wide_type (type, lo, hi);
7760 /* Subroutine of native_interpret_expr. Interpret the contents of
7761 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7762 If the buffer cannot be interpreted, return NULL_TREE. */
7765 native_interpret_real (tree type, const unsigned char *ptr, int len)
7767 enum machine_mode mode = TYPE_MODE (type);
7768 int total_bytes = GET_MODE_SIZE (mode);
7769 int byte, offset, word, words, bitpos;
7770 unsigned char value;
7771 /* There are always 32 bits in each long, no matter the size of
7772 the hosts long. We handle floating point representations with
7777 total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7778 if (total_bytes > len || total_bytes > 24)
7780 words = (32 / BITS_PER_UNIT) / UNITS_PER_WORD;
7782 memset (tmp, 0, sizeof (tmp));
7783 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7784 bitpos += BITS_PER_UNIT)
7786 byte = (bitpos / BITS_PER_UNIT) & 3;
7787 if (UNITS_PER_WORD < 4)
7789 word = byte / UNITS_PER_WORD;
7790 if (WORDS_BIG_ENDIAN)
7791 word = (words - 1) - word;
7792 offset = word * UNITS_PER_WORD;
7793 if (BYTES_BIG_ENDIAN)
7794 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7796 offset += byte % UNITS_PER_WORD;
7799 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7800 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7802 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7805 real_from_target (&r, tmp, mode);
7806 return build_real (type, r);
7810 /* Subroutine of native_interpret_expr. Interpret the contents of
7811 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7812 If the buffer cannot be interpreted, return NULL_TREE. */
7815 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7817 tree etype, rpart, ipart;
7820 etype = TREE_TYPE (type);
7821 size = GET_MODE_SIZE (TYPE_MODE (etype));
7824 rpart = native_interpret_expr (etype, ptr, size);
7827 ipart = native_interpret_expr (etype, ptr+size, size);
7830 return build_complex (type, rpart, ipart);
7834 /* Subroutine of native_interpret_expr. Interpret the contents of
7835 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7836 If the buffer cannot be interpreted, return NULL_TREE. */
7839 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7841 tree etype, elem, elements;
7844 etype = TREE_TYPE (type);
7845 size = GET_MODE_SIZE (TYPE_MODE (etype));
7846 count = TYPE_VECTOR_SUBPARTS (type);
7847 if (size * count > len)
7850 elements = NULL_TREE;
7851 for (i = count - 1; i >= 0; i--)
7853 elem = native_interpret_expr (etype, ptr+(i*size), size);
7856 elements = tree_cons (NULL_TREE, elem, elements);
7858 return build_vector (type, elements);
7862 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7863 the buffer PTR of length LEN as a constant of type TYPE. For
7864 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7865 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7866 return NULL_TREE. */
7869 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7871 switch (TREE_CODE (type))
7876 return native_interpret_int (type, ptr, len);
7879 return native_interpret_real (type, ptr, len);
7882 return native_interpret_complex (type, ptr, len);
7885 return native_interpret_vector (type, ptr, len);
7893 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7894 TYPE at compile-time. If we're unable to perform the conversion
7895 return NULL_TREE. */
7898 fold_view_convert_expr (tree type, tree expr)
7900 /* We support up to 512-bit values (for V8DFmode). */
7901 unsigned char buffer[64];
7904 /* Check that the host and target are sane. */
7905 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7908 len = native_encode_expr (expr, buffer, sizeof (buffer));
7912 return native_interpret_expr (type, buffer, len);
7915 /* Build an expression for the address of T. Folds away INDIRECT_REF
7916 to avoid confusing the gimplify process. */
7919 build_fold_addr_expr_with_type (tree t, tree ptrtype)
7921 /* The size of the object is not relevant when talking about its address. */
7922 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7923 t = TREE_OPERAND (t, 0);
7925 /* Note: doesn't apply to ALIGN_INDIRECT_REF */
7926 if (TREE_CODE (t) == INDIRECT_REF
7927 || TREE_CODE (t) == MISALIGNED_INDIRECT_REF)
7929 t = TREE_OPERAND (t, 0);
7931 if (TREE_TYPE (t) != ptrtype)
7932 t = build1 (NOP_EXPR, ptrtype, t);
7935 t = build1 (ADDR_EXPR, ptrtype, t);
7940 /* Build an expression for the address of T. */
7943 build_fold_addr_expr (tree t)
7945 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7947 return build_fold_addr_expr_with_type (t, ptrtype);
7950 /* Fold a unary expression of code CODE and type TYPE with operand
7951 OP0. Return the folded expression if folding is successful.
7952 Otherwise, return NULL_TREE. */
7955 fold_unary (enum tree_code code, tree type, tree op0)
7959 enum tree_code_class kind = TREE_CODE_CLASS (code);
7961 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7962 && TREE_CODE_LENGTH (code) == 1);
7967 if (CONVERT_EXPR_CODE_P (code)
7968 || code == FLOAT_EXPR || code == ABS_EXPR)
7970 /* Don't use STRIP_NOPS, because signedness of argument type
7972 STRIP_SIGN_NOPS (arg0);
7976 /* Strip any conversions that don't change the mode. This
7977 is safe for every expression, except for a comparison
7978 expression because its signedness is derived from its
7981 Note that this is done as an internal manipulation within
7982 the constant folder, in order to find the simplest
7983 representation of the arguments so that their form can be
7984 studied. In any cases, the appropriate type conversions
7985 should be put back in the tree that will get out of the
7991 if (TREE_CODE_CLASS (code) == tcc_unary)
7993 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7994 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7995 fold_build1 (code, type,
7996 fold_convert (TREE_TYPE (op0),
7997 TREE_OPERAND (arg0, 1))));
7998 else if (TREE_CODE (arg0) == COND_EXPR)
8000 tree arg01 = TREE_OPERAND (arg0, 1);
8001 tree arg02 = TREE_OPERAND (arg0, 2);
8002 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
8003 arg01 = fold_build1 (code, type,
8004 fold_convert (TREE_TYPE (op0), arg01));
8005 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
8006 arg02 = fold_build1 (code, type,
8007 fold_convert (TREE_TYPE (op0), arg02));
8008 tem = fold_build3 (COND_EXPR, type, TREE_OPERAND (arg0, 0),
8011 /* If this was a conversion, and all we did was to move into
8012 inside the COND_EXPR, bring it back out. But leave it if
8013 it is a conversion from integer to integer and the
8014 result precision is no wider than a word since such a
8015 conversion is cheap and may be optimized away by combine,
8016 while it couldn't if it were outside the COND_EXPR. Then return
8017 so we don't get into an infinite recursion loop taking the
8018 conversion out and then back in. */
8020 if ((CONVERT_EXPR_CODE_P (code)
8021 || code == NON_LVALUE_EXPR)
8022 && TREE_CODE (tem) == COND_EXPR
8023 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
8024 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
8025 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
8026 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
8027 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
8028 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
8029 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8031 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
8032 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
8033 || flag_syntax_only))
8034 tem = build1 (code, type,
8036 TREE_TYPE (TREE_OPERAND
8037 (TREE_OPERAND (tem, 1), 0)),
8038 TREE_OPERAND (tem, 0),
8039 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
8040 TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
8043 else if (COMPARISON_CLASS_P (arg0))
8045 if (TREE_CODE (type) == BOOLEAN_TYPE)
8047 arg0 = copy_node (arg0);
8048 TREE_TYPE (arg0) = type;
8051 else if (TREE_CODE (type) != INTEGER_TYPE)
8052 return fold_build3 (COND_EXPR, type, arg0,
8053 fold_build1 (code, type,
8055 fold_build1 (code, type,
8056 integer_zero_node));
8063 /* Re-association barriers around constants and other re-association
8064 barriers can be removed. */
8065 if (CONSTANT_CLASS_P (op0)
8066 || TREE_CODE (op0) == PAREN_EXPR)
8067 return fold_convert (type, op0);
8072 case FIX_TRUNC_EXPR:
8073 if (TREE_TYPE (op0) == type)
8076 /* If we have (type) (a CMP b) and type is an integral type, return
8077 new expression involving the new type. */
8078 if (COMPARISON_CLASS_P (op0) && INTEGRAL_TYPE_P (type))
8079 return fold_build2 (TREE_CODE (op0), type, TREE_OPERAND (op0, 0),
8080 TREE_OPERAND (op0, 1));
8082 /* Handle cases of two conversions in a row. */
8083 if (CONVERT_EXPR_P (op0))
8085 tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
8086 tree inter_type = TREE_TYPE (op0);
8087 int inside_int = INTEGRAL_TYPE_P (inside_type);
8088 int inside_ptr = POINTER_TYPE_P (inside_type);
8089 int inside_float = FLOAT_TYPE_P (inside_type);
8090 int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
8091 unsigned int inside_prec = TYPE_PRECISION (inside_type);
8092 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
8093 int inter_int = INTEGRAL_TYPE_P (inter_type);
8094 int inter_ptr = POINTER_TYPE_P (inter_type);
8095 int inter_float = FLOAT_TYPE_P (inter_type);
8096 int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
8097 unsigned int inter_prec = TYPE_PRECISION (inter_type);
8098 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
8099 int final_int = INTEGRAL_TYPE_P (type);
8100 int final_ptr = POINTER_TYPE_P (type);
8101 int final_float = FLOAT_TYPE_P (type);
8102 int final_vec = TREE_CODE (type) == VECTOR_TYPE;
8103 unsigned int final_prec = TYPE_PRECISION (type);
8104 int final_unsignedp = TYPE_UNSIGNED (type);
8106 /* In addition to the cases of two conversions in a row
8107 handled below, if we are converting something to its own
8108 type via an object of identical or wider precision, neither
8109 conversion is needed. */
8110 if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
8111 && (((inter_int || inter_ptr) && final_int)
8112 || (inter_float && final_float))
8113 && inter_prec >= final_prec)
8114 return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8116 /* Likewise, if the intermediate and initial types are either both
8117 float or both integer, we don't need the middle conversion if the
8118 former is wider than the latter and doesn't change the signedness
8119 (for integers). Avoid this if the final type is a pointer since
8120 then we sometimes need the middle conversion. Likewise if the
8121 final type has a precision not equal to the size of its mode. */
8122 if (((inter_int && inside_int)
8123 || (inter_float && inside_float)
8124 || (inter_vec && inside_vec))
8125 && inter_prec >= inside_prec
8126 && (inter_float || inter_vec
8127 || inter_unsignedp == inside_unsignedp)
8128 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
8129 && TYPE_MODE (type) == TYPE_MODE (inter_type))
8131 && (! final_vec || inter_prec == inside_prec))
8132 return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8134 /* If we have a sign-extension of a zero-extended value, we can
8135 replace that by a single zero-extension. */
8136 if (inside_int && inter_int && final_int
8137 && inside_prec < inter_prec && inter_prec < final_prec
8138 && inside_unsignedp && !inter_unsignedp)
8139 return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8141 /* Two conversions in a row are not needed unless:
8142 - some conversion is floating-point (overstrict for now), or
8143 - some conversion is a vector (overstrict for now), or
8144 - the intermediate type is narrower than both initial and
8146 - the intermediate type and innermost type differ in signedness,
8147 and the outermost type is wider than the intermediate, or
8148 - the initial type is a pointer type and the precisions of the
8149 intermediate and final types differ, or
8150 - the final type is a pointer type and the precisions of the
8151 initial and intermediate types differ. */
8152 if (! inside_float && ! inter_float && ! final_float
8153 && ! inside_vec && ! inter_vec && ! final_vec
8154 && (inter_prec >= inside_prec || inter_prec >= final_prec)
8155 && ! (inside_int && inter_int
8156 && inter_unsignedp != inside_unsignedp
8157 && inter_prec < final_prec)
8158 && ((inter_unsignedp && inter_prec > inside_prec)
8159 == (final_unsignedp && final_prec > inter_prec))
8160 && ! (inside_ptr && inter_prec != final_prec)
8161 && ! (final_ptr && inside_prec != inter_prec)
8162 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
8163 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
8164 return fold_build1 (code, type, TREE_OPERAND (op0, 0));
8167 /* Handle (T *)&A.B.C for A being of type T and B and C
8168 living at offset zero. This occurs frequently in
8169 C++ upcasting and then accessing the base. */
8170 if (TREE_CODE (op0) == ADDR_EXPR
8171 && POINTER_TYPE_P (type)
8172 && handled_component_p (TREE_OPERAND (op0, 0)))
8174 HOST_WIDE_INT bitsize, bitpos;
8176 enum machine_mode mode;
8177 int unsignedp, volatilep;
8178 tree base = TREE_OPERAND (op0, 0);
8179 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
8180 &mode, &unsignedp, &volatilep, false);
8181 /* If the reference was to a (constant) zero offset, we can use
8182 the address of the base if it has the same base type
8183 as the result type. */
8184 if (! offset && bitpos == 0
8185 && TYPE_MAIN_VARIANT (TREE_TYPE (type))
8186 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
8187 return fold_convert (type, build_fold_addr_expr (base));
8190 if (TREE_CODE (op0) == MODIFY_EXPR
8191 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
8192 /* Detect assigning a bitfield. */
8193 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
8195 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
8197 /* Don't leave an assignment inside a conversion
8198 unless assigning a bitfield. */
8199 tem = fold_build1 (code, type, TREE_OPERAND (op0, 1));
8200 /* First do the assignment, then return converted constant. */
8201 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
8202 TREE_NO_WARNING (tem) = 1;
8203 TREE_USED (tem) = 1;
8207 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
8208 constants (if x has signed type, the sign bit cannot be set
8209 in c). This folds extension into the BIT_AND_EXPR.
8210 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
8211 very likely don't have maximal range for their precision and this
8212 transformation effectively doesn't preserve non-maximal ranges. */
8213 if (TREE_CODE (type) == INTEGER_TYPE
8214 && TREE_CODE (op0) == BIT_AND_EXPR
8215 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST
8216 /* Not if the conversion is to the sub-type. */
8217 && TREE_TYPE (type) != TREE_TYPE (op0))
8220 tree and0 = TREE_OPERAND (and, 0), and1 = TREE_OPERAND (and, 1);
8223 if (TYPE_UNSIGNED (TREE_TYPE (and))
8224 || (TYPE_PRECISION (type)
8225 <= TYPE_PRECISION (TREE_TYPE (and))))
8227 else if (TYPE_PRECISION (TREE_TYPE (and1))
8228 <= HOST_BITS_PER_WIDE_INT
8229 && host_integerp (and1, 1))
8231 unsigned HOST_WIDE_INT cst;
8233 cst = tree_low_cst (and1, 1);
8234 cst &= (HOST_WIDE_INT) -1
8235 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
8236 change = (cst == 0);
8237 #ifdef LOAD_EXTEND_OP
8239 && !flag_syntax_only
8240 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
8243 tree uns = unsigned_type_for (TREE_TYPE (and0));
8244 and0 = fold_convert (uns, and0);
8245 and1 = fold_convert (uns, and1);
8251 tem = force_fit_type_double (type, TREE_INT_CST_LOW (and1),
8252 TREE_INT_CST_HIGH (and1), 0,
8253 TREE_OVERFLOW (and1));
8254 return fold_build2 (BIT_AND_EXPR, type,
8255 fold_convert (type, and0), tem);
8259 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
8260 when one of the new casts will fold away. Conservatively we assume
8261 that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
8262 if (POINTER_TYPE_P (type)
8263 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
8264 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8265 || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
8266 || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
8268 tree arg00 = TREE_OPERAND (arg0, 0);
8269 tree arg01 = TREE_OPERAND (arg0, 1);
8271 return fold_build2 (TREE_CODE (arg0), type, fold_convert (type, arg00),
8272 fold_convert (sizetype, arg01));
8275 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
8276 of the same precision, and X is an integer type not narrower than
8277 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
8278 if (INTEGRAL_TYPE_P (type)
8279 && TREE_CODE (op0) == BIT_NOT_EXPR
8280 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8281 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
8282 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
8284 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
8285 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
8286 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
8287 return fold_build1 (BIT_NOT_EXPR, type, fold_convert (type, tem));
8290 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
8291 type of X and Y (integer types only). */
8292 if (INTEGRAL_TYPE_P (type)
8293 && TREE_CODE (op0) == MULT_EXPR
8294 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
8295 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
8297 /* Be careful not to introduce new overflows. */
8299 if (TYPE_OVERFLOW_WRAPS (type))
8302 mult_type = unsigned_type_for (type);
8304 if (TYPE_PRECISION (mult_type) < TYPE_PRECISION (TREE_TYPE (op0)))
8306 tem = fold_build2 (MULT_EXPR, mult_type,
8307 fold_convert (mult_type,
8308 TREE_OPERAND (op0, 0)),
8309 fold_convert (mult_type,
8310 TREE_OPERAND (op0, 1)));
8311 return fold_convert (type, tem);
8315 tem = fold_convert_const (code, type, op0);
8316 return tem ? tem : NULL_TREE;
8318 case FIXED_CONVERT_EXPR:
8319 tem = fold_convert_const (code, type, arg0);
8320 return tem ? tem : NULL_TREE;
8322 case VIEW_CONVERT_EXPR:
8323 if (TREE_TYPE (op0) == type)
8325 if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
8326 return fold_build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
8328 /* For integral conversions with the same precision or pointer
8329 conversions use a NOP_EXPR instead. */
8330 if ((INTEGRAL_TYPE_P (type)
8331 || POINTER_TYPE_P (type))
8332 && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8333 || POINTER_TYPE_P (TREE_TYPE (op0)))
8334 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0))
8335 /* Do not muck with VIEW_CONVERT_EXPRs that convert from
8336 a sub-type to its base type as generated by the Ada FE. */
8337 && !(INTEGRAL_TYPE_P (TREE_TYPE (op0))
8338 && TREE_TYPE (TREE_TYPE (op0))))
8339 return fold_convert (type, op0);
8341 /* Strip inner integral conversions that do not change the precision. */
8342 if (CONVERT_EXPR_P (op0)
8343 && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8344 || POINTER_TYPE_P (TREE_TYPE (op0)))
8345 && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))
8346 || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
8347 && (TYPE_PRECISION (TREE_TYPE (op0))
8348 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
8349 return fold_build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
8351 return fold_view_convert_expr (type, op0);
8354 tem = fold_negate_expr (arg0);
8356 return fold_convert (type, tem);
8360 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
8361 return fold_abs_const (arg0, type);
8362 else if (TREE_CODE (arg0) == NEGATE_EXPR)
8363 return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
8364 /* Convert fabs((double)float) into (double)fabsf(float). */
8365 else if (TREE_CODE (arg0) == NOP_EXPR
8366 && TREE_CODE (type) == REAL_TYPE)
8368 tree targ0 = strip_float_extensions (arg0);
8370 return fold_convert (type, fold_build1 (ABS_EXPR,
8374 /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on. */
8375 else if (TREE_CODE (arg0) == ABS_EXPR)
8377 else if (tree_expr_nonnegative_p (arg0))
8380 /* Strip sign ops from argument. */
8381 if (TREE_CODE (type) == REAL_TYPE)
8383 tem = fold_strip_sign_ops (arg0);
8385 return fold_build1 (ABS_EXPR, type, fold_convert (type, tem));
8390 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8391 return fold_convert (type, arg0);
8392 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8394 tree itype = TREE_TYPE (type);
8395 tree rpart = fold_convert (itype, TREE_OPERAND (arg0, 0));
8396 tree ipart = fold_convert (itype, TREE_OPERAND (arg0, 1));
8397 return fold_build2 (COMPLEX_EXPR, type, rpart, negate_expr (ipart));
8399 if (TREE_CODE (arg0) == COMPLEX_CST)
8401 tree itype = TREE_TYPE (type);
8402 tree rpart = fold_convert (itype, TREE_REALPART (arg0));
8403 tree ipart = fold_convert (itype, TREE_IMAGPART (arg0));
8404 return build_complex (type, rpart, negate_expr (ipart));
8406 if (TREE_CODE (arg0) == CONJ_EXPR)
8407 return fold_convert (type, TREE_OPERAND (arg0, 0));
8411 if (TREE_CODE (arg0) == INTEGER_CST)
8412 return fold_not_const (arg0, type);
8413 else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8414 return fold_convert (type, TREE_OPERAND (arg0, 0));
8415 /* Convert ~ (-A) to A - 1. */
8416 else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8417 return fold_build2 (MINUS_EXPR, type,
8418 fold_convert (type, TREE_OPERAND (arg0, 0)),
8419 build_int_cst (type, 1));
8420 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
8421 else if (INTEGRAL_TYPE_P (type)
8422 && ((TREE_CODE (arg0) == MINUS_EXPR
8423 && integer_onep (TREE_OPERAND (arg0, 1)))
8424 || (TREE_CODE (arg0) == PLUS_EXPR
8425 && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8426 return fold_build1 (NEGATE_EXPR, type,
8427 fold_convert (type, TREE_OPERAND (arg0, 0)));
8428 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
8429 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8430 && (tem = fold_unary (BIT_NOT_EXPR, type,
8432 TREE_OPERAND (arg0, 0)))))
8433 return fold_build2 (BIT_XOR_EXPR, type, tem,
8434 fold_convert (type, TREE_OPERAND (arg0, 1)));
8435 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8436 && (tem = fold_unary (BIT_NOT_EXPR, type,
8438 TREE_OPERAND (arg0, 1)))))
8439 return fold_build2 (BIT_XOR_EXPR, type,
8440 fold_convert (type, TREE_OPERAND (arg0, 0)), tem);
8441 /* Perform BIT_NOT_EXPR on each element individually. */
8442 else if (TREE_CODE (arg0) == VECTOR_CST)
8444 tree elements = TREE_VECTOR_CST_ELTS (arg0), elem, list = NULL_TREE;
8445 int count = TYPE_VECTOR_SUBPARTS (type), i;
8447 for (i = 0; i < count; i++)
8451 elem = TREE_VALUE (elements);
8452 elem = fold_unary (BIT_NOT_EXPR, TREE_TYPE (type), elem);
8453 if (elem == NULL_TREE)
8455 elements = TREE_CHAIN (elements);
8458 elem = build_int_cst (TREE_TYPE (type), -1);
8459 list = tree_cons (NULL_TREE, elem, list);
8462 return build_vector (type, nreverse (list));
8467 case TRUTH_NOT_EXPR:
8468 /* The argument to invert_truthvalue must have Boolean type. */
8469 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8470 arg0 = fold_convert (boolean_type_node, arg0);
8472 /* Note that the operand of this must be an int
8473 and its values must be 0 or 1.
8474 ("true" is a fixed value perhaps depending on the language,
8475 but we don't handle values other than 1 correctly yet.) */
8476 tem = fold_truth_not_expr (arg0);
8479 return fold_convert (type, tem);
8482 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8483 return fold_convert (type, arg0);
8484 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8485 return omit_one_operand (type, TREE_OPERAND (arg0, 0),
8486 TREE_OPERAND (arg0, 1));
8487 if (TREE_CODE (arg0) == COMPLEX_CST)
8488 return fold_convert (type, TREE_REALPART (arg0));
8489 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8491 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8492 tem = fold_build2 (TREE_CODE (arg0), itype,
8493 fold_build1 (REALPART_EXPR, itype,
8494 TREE_OPERAND (arg0, 0)),
8495 fold_build1 (REALPART_EXPR, itype,
8496 TREE_OPERAND (arg0, 1)));
8497 return fold_convert (type, tem);
8499 if (TREE_CODE (arg0) == CONJ_EXPR)
8501 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8502 tem = fold_build1 (REALPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8503 return fold_convert (type, tem);
8505 if (TREE_CODE (arg0) == CALL_EXPR)
8507 tree fn = get_callee_fndecl (arg0);
8508 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8509 switch (DECL_FUNCTION_CODE (fn))
8511 CASE_FLT_FN (BUILT_IN_CEXPI):
8512 fn = mathfn_built_in (type, BUILT_IN_COS);
8514 return build_call_expr (fn, 1, CALL_EXPR_ARG (arg0, 0));
8524 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8525 return fold_convert (type, integer_zero_node);
8526 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8527 return omit_one_operand (type, TREE_OPERAND (arg0, 1),
8528 TREE_OPERAND (arg0, 0));
8529 if (TREE_CODE (arg0) == COMPLEX_CST)
8530 return fold_convert (type, TREE_IMAGPART (arg0));
8531 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8533 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8534 tem = fold_build2 (TREE_CODE (arg0), itype,
8535 fold_build1 (IMAGPART_EXPR, itype,
8536 TREE_OPERAND (arg0, 0)),
8537 fold_build1 (IMAGPART_EXPR, itype,
8538 TREE_OPERAND (arg0, 1)));
8539 return fold_convert (type, tem);
8541 if (TREE_CODE (arg0) == CONJ_EXPR)
8543 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8544 tem = fold_build1 (IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8545 return fold_convert (type, negate_expr (tem));
8547 if (TREE_CODE (arg0) == CALL_EXPR)
8549 tree fn = get_callee_fndecl (arg0);
8550 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8551 switch (DECL_FUNCTION_CODE (fn))
8553 CASE_FLT_FN (BUILT_IN_CEXPI):
8554 fn = mathfn_built_in (type, BUILT_IN_SIN);
8556 return build_call_expr (fn, 1, CALL_EXPR_ARG (arg0, 0));
8567 } /* switch (code) */
8571 /* If the operation was a conversion do _not_ mark a resulting constant
8572 with TREE_OVERFLOW if the original constant was not. These conversions
8573 have implementation defined behavior and retaining the TREE_OVERFLOW
8574 flag here would confuse later passes such as VRP. */
8576 fold_unary_ignore_overflow (enum tree_code code, tree type, tree op0)
8578 tree res = fold_unary (code, type, op0);
8580 && TREE_CODE (res) == INTEGER_CST
8581 && TREE_CODE (op0) == INTEGER_CST
8582 && CONVERT_EXPR_CODE_P (code))
8583 TREE_OVERFLOW (res) = TREE_OVERFLOW (op0);
8588 /* Fold a binary expression of code CODE and type TYPE with operands
8589 OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8590 Return the folded expression if folding is successful. Otherwise,
8591 return NULL_TREE. */
8594 fold_minmax (enum tree_code code, tree type, tree op0, tree op1)
8596 enum tree_code compl_code;
8598 if (code == MIN_EXPR)
8599 compl_code = MAX_EXPR;
8600 else if (code == MAX_EXPR)
8601 compl_code = MIN_EXPR;
8605 /* MIN (MAX (a, b), b) == b. */
8606 if (TREE_CODE (op0) == compl_code
8607 && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8608 return omit_one_operand (type, op1, TREE_OPERAND (op0, 0));
8610 /* MIN (MAX (b, a), b) == b. */
8611 if (TREE_CODE (op0) == compl_code
8612 && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8613 && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8614 return omit_one_operand (type, op1, TREE_OPERAND (op0, 1));
8616 /* MIN (a, MAX (a, b)) == a. */
8617 if (TREE_CODE (op1) == compl_code
8618 && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8619 && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8620 return omit_one_operand (type, op0, TREE_OPERAND (op1, 1));
8622 /* MIN (a, MAX (b, a)) == a. */
8623 if (TREE_CODE (op1) == compl_code
8624 && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8625 && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8626 return omit_one_operand (type, op0, TREE_OPERAND (op1, 0));
8631 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8632 by changing CODE to reduce the magnitude of constants involved in
8633 ARG0 of the comparison.
8634 Returns a canonicalized comparison tree if a simplification was
8635 possible, otherwise returns NULL_TREE.
8636 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8637 valid if signed overflow is undefined. */
8640 maybe_canonicalize_comparison_1 (enum tree_code code, tree type,
8641 tree arg0, tree arg1,
8642 bool *strict_overflow_p)
8644 enum tree_code code0 = TREE_CODE (arg0);
8645 tree t, cst0 = NULL_TREE;
8649 /* Match A +- CST code arg1 and CST code arg1. We can change the
8650 first form only if overflow is undefined. */
8651 if (!((TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8652 /* In principle pointers also have undefined overflow behavior,
8653 but that causes problems elsewhere. */
8654 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8655 && (code0 == MINUS_EXPR
8656 || code0 == PLUS_EXPR)
8657 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8658 || code0 == INTEGER_CST))
8661 /* Identify the constant in arg0 and its sign. */
8662 if (code0 == INTEGER_CST)
8665 cst0 = TREE_OPERAND (arg0, 1);
8666 sgn0 = tree_int_cst_sgn (cst0);
8668 /* Overflowed constants and zero will cause problems. */
8669 if (integer_zerop (cst0)
8670 || TREE_OVERFLOW (cst0))
8673 /* See if we can reduce the magnitude of the constant in
8674 arg0 by changing the comparison code. */
8675 if (code0 == INTEGER_CST)
8677 /* CST <= arg1 -> CST-1 < arg1. */
8678 if (code == LE_EXPR && sgn0 == 1)
8680 /* -CST < arg1 -> -CST-1 <= arg1. */
8681 else if (code == LT_EXPR && sgn0 == -1)
8683 /* CST > arg1 -> CST-1 >= arg1. */
8684 else if (code == GT_EXPR && sgn0 == 1)
8686 /* -CST >= arg1 -> -CST-1 > arg1. */
8687 else if (code == GE_EXPR && sgn0 == -1)
8691 /* arg1 code' CST' might be more canonical. */
8696 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8698 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8700 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8701 else if (code == GT_EXPR
8702 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8704 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8705 else if (code == LE_EXPR
8706 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8708 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8709 else if (code == GE_EXPR
8710 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8714 *strict_overflow_p = true;
8717 /* Now build the constant reduced in magnitude. But not if that
8718 would produce one outside of its types range. */
8719 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8721 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8722 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8724 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8725 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8726 /* We cannot swap the comparison here as that would cause us to
8727 endlessly recurse. */
8730 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8731 cst0, build_int_cst (TREE_TYPE (cst0), 1), 0);
8732 if (code0 != INTEGER_CST)
8733 t = fold_build2 (code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8735 /* If swapping might yield to a more canonical form, do so. */
8737 return fold_build2 (swap_tree_comparison (code), type, arg1, t);
8739 return fold_build2 (code, type, t, arg1);
8742 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8743 overflow further. Try to decrease the magnitude of constants involved
8744 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8745 and put sole constants at the second argument position.
8746 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8749 maybe_canonicalize_comparison (enum tree_code code, tree type,
8750 tree arg0, tree arg1)
8753 bool strict_overflow_p;
8754 const char * const warnmsg = G_("assuming signed overflow does not occur "
8755 "when reducing constant in comparison");
8757 /* Try canonicalization by simplifying arg0. */
8758 strict_overflow_p = false;
8759 t = maybe_canonicalize_comparison_1 (code, type, arg0, arg1,
8760 &strict_overflow_p);
8763 if (strict_overflow_p)
8764 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8768 /* Try canonicalization by simplifying arg1 using the swapped
8770 code = swap_tree_comparison (code);
8771 strict_overflow_p = false;
8772 t = maybe_canonicalize_comparison_1 (code, type, arg1, arg0,
8773 &strict_overflow_p);
8774 if (t && strict_overflow_p)
8775 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8779 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8780 space. This is used to avoid issuing overflow warnings for
8781 expressions like &p->x which can not wrap. */
8784 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8786 unsigned HOST_WIDE_INT offset_low, total_low;
8787 HOST_WIDE_INT size, offset_high, total_high;
8789 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8795 if (offset == NULL_TREE)
8800 else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8804 offset_low = TREE_INT_CST_LOW (offset);
8805 offset_high = TREE_INT_CST_HIGH (offset);
8808 if (add_double_with_sign (offset_low, offset_high,
8809 bitpos / BITS_PER_UNIT, 0,
8810 &total_low, &total_high,
8814 if (total_high != 0)
8817 size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8821 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8823 if (TREE_CODE (base) == ADDR_EXPR)
8825 HOST_WIDE_INT base_size;
8827 base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8828 if (base_size > 0 && size < base_size)
8832 return total_low > (unsigned HOST_WIDE_INT) size;
8835 /* Subroutine of fold_binary. This routine performs all of the
8836 transformations that are common to the equality/inequality
8837 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8838 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8839 fold_binary should call fold_binary. Fold a comparison with
8840 tree code CODE and type TYPE with operands OP0 and OP1. Return
8841 the folded comparison or NULL_TREE. */
8844 fold_comparison (enum tree_code code, tree type, tree op0, tree op1)
8846 tree arg0, arg1, tem;
8851 STRIP_SIGN_NOPS (arg0);
8852 STRIP_SIGN_NOPS (arg1);
8854 tem = fold_relational_const (code, type, arg0, arg1);
8855 if (tem != NULL_TREE)
8858 /* If one arg is a real or integer constant, put it last. */
8859 if (tree_swap_operands_p (arg0, arg1, true))
8860 return fold_build2 (swap_tree_comparison (code), type, op1, op0);
8862 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1. */
8863 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8864 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8865 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8866 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
8867 && (TREE_CODE (arg1) == INTEGER_CST
8868 && !TREE_OVERFLOW (arg1)))
8870 tree const1 = TREE_OPERAND (arg0, 1);
8872 tree variable = TREE_OPERAND (arg0, 0);
8875 lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
8877 lhs = fold_build2 (lhs_add ? PLUS_EXPR : MINUS_EXPR,
8878 TREE_TYPE (arg1), const2, const1);
8880 /* If the constant operation overflowed this can be
8881 simplified as a comparison against INT_MAX/INT_MIN. */
8882 if (TREE_CODE (lhs) == INTEGER_CST
8883 && TREE_OVERFLOW (lhs))
8885 int const1_sgn = tree_int_cst_sgn (const1);
8886 enum tree_code code2 = code;
8888 /* Get the sign of the constant on the lhs if the
8889 operation were VARIABLE + CONST1. */
8890 if (TREE_CODE (arg0) == MINUS_EXPR)
8891 const1_sgn = -const1_sgn;
8893 /* The sign of the constant determines if we overflowed
8894 INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8895 Canonicalize to the INT_MIN overflow by swapping the comparison
8897 if (const1_sgn == -1)
8898 code2 = swap_tree_comparison (code);
8900 /* We now can look at the canonicalized case
8901 VARIABLE + 1 CODE2 INT_MIN
8902 and decide on the result. */
8903 if (code2 == LT_EXPR
8905 || code2 == EQ_EXPR)
8906 return omit_one_operand (type, boolean_false_node, variable);
8907 else if (code2 == NE_EXPR
8909 || code2 == GT_EXPR)
8910 return omit_one_operand (type, boolean_true_node, variable);
8913 if (TREE_CODE (lhs) == TREE_CODE (arg1)
8914 && (TREE_CODE (lhs) != INTEGER_CST
8915 || !TREE_OVERFLOW (lhs)))
8917 fold_overflow_warning (("assuming signed overflow does not occur "
8918 "when changing X +- C1 cmp C2 to "
8920 WARN_STRICT_OVERFLOW_COMPARISON);
8921 return fold_build2 (code, type, variable, lhs);
8925 /* For comparisons of pointers we can decompose it to a compile time
8926 comparison of the base objects and the offsets into the object.
8927 This requires at least one operand being an ADDR_EXPR or a
8928 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8929 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8930 && (TREE_CODE (arg0) == ADDR_EXPR
8931 || TREE_CODE (arg1) == ADDR_EXPR
8932 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8933 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8935 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8936 HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8937 enum machine_mode mode;
8938 int volatilep, unsignedp;
8939 bool indirect_base0 = false, indirect_base1 = false;
8941 /* Get base and offset for the access. Strip ADDR_EXPR for
8942 get_inner_reference, but put it back by stripping INDIRECT_REF
8943 off the base object if possible. indirect_baseN will be true
8944 if baseN is not an address but refers to the object itself. */
8946 if (TREE_CODE (arg0) == ADDR_EXPR)
8948 base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
8949 &bitsize, &bitpos0, &offset0, &mode,
8950 &unsignedp, &volatilep, false);
8951 if (TREE_CODE (base0) == INDIRECT_REF)
8952 base0 = TREE_OPERAND (base0, 0);
8954 indirect_base0 = true;
8956 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8958 base0 = TREE_OPERAND (arg0, 0);
8959 offset0 = TREE_OPERAND (arg0, 1);
8963 if (TREE_CODE (arg1) == ADDR_EXPR)
8965 base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
8966 &bitsize, &bitpos1, &offset1, &mode,
8967 &unsignedp, &volatilep, false);
8968 if (TREE_CODE (base1) == INDIRECT_REF)
8969 base1 = TREE_OPERAND (base1, 0);
8971 indirect_base1 = true;
8973 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8975 base1 = TREE_OPERAND (arg1, 0);
8976 offset1 = TREE_OPERAND (arg1, 1);
8979 /* If we have equivalent bases we might be able to simplify. */
8980 if (indirect_base0 == indirect_base1
8981 && operand_equal_p (base0, base1, 0))
8983 /* We can fold this expression to a constant if the non-constant
8984 offset parts are equal. */
8985 if ((offset0 == offset1
8986 || (offset0 && offset1
8987 && operand_equal_p (offset0, offset1, 0)))
8990 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8995 && bitpos0 != bitpos1
8996 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8997 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8998 fold_overflow_warning (("assuming pointer wraparound does not "
8999 "occur when comparing P +- C1 with "
9001 WARN_STRICT_OVERFLOW_CONDITIONAL);
9006 return constant_boolean_node (bitpos0 == bitpos1, type);
9008 return constant_boolean_node (bitpos0 != bitpos1, type);
9010 return constant_boolean_node (bitpos0 < bitpos1, type);
9012 return constant_boolean_node (bitpos0 <= bitpos1, type);
9014 return constant_boolean_node (bitpos0 >= bitpos1, type);
9016 return constant_boolean_node (bitpos0 > bitpos1, type);
9020 /* We can simplify the comparison to a comparison of the variable
9021 offset parts if the constant offset parts are equal.
9022 Be careful to use signed size type here because otherwise we
9023 mess with array offsets in the wrong way. This is possible
9024 because pointer arithmetic is restricted to retain within an
9025 object and overflow on pointer differences is undefined as of
9026 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
9027 else if (bitpos0 == bitpos1
9028 && ((code == EQ_EXPR || code == NE_EXPR)
9029 || POINTER_TYPE_OVERFLOW_UNDEFINED))
9031 tree signed_size_type_node;
9032 signed_size_type_node = signed_type_for (size_type_node);
9034 /* By converting to signed size type we cover middle-end pointer
9035 arithmetic which operates on unsigned pointer types of size
9036 type size and ARRAY_REF offsets which are properly sign or
9037 zero extended from their type in case it is narrower than
9039 if (offset0 == NULL_TREE)
9040 offset0 = build_int_cst (signed_size_type_node, 0);
9042 offset0 = fold_convert (signed_size_type_node, offset0);
9043 if (offset1 == NULL_TREE)
9044 offset1 = build_int_cst (signed_size_type_node, 0);
9046 offset1 = fold_convert (signed_size_type_node, offset1);
9050 && (pointer_may_wrap_p (base0, offset0, bitpos0)
9051 || pointer_may_wrap_p (base1, offset1, bitpos1)))
9052 fold_overflow_warning (("assuming pointer wraparound does not "
9053 "occur when comparing P +- C1 with "
9055 WARN_STRICT_OVERFLOW_COMPARISON);
9057 return fold_build2 (code, type, offset0, offset1);
9060 /* For non-equal bases we can simplify if they are addresses
9061 of local binding decls or constants. */
9062 else if (indirect_base0 && indirect_base1
9063 /* We know that !operand_equal_p (base0, base1, 0)
9064 because the if condition was false. But make
9065 sure two decls are not the same. */
9067 && TREE_CODE (arg0) == ADDR_EXPR
9068 && TREE_CODE (arg1) == ADDR_EXPR
9069 && (((TREE_CODE (base0) == VAR_DECL
9070 || TREE_CODE (base0) == PARM_DECL)
9071 && (targetm.binds_local_p (base0)
9072 || CONSTANT_CLASS_P (base1)))
9073 || CONSTANT_CLASS_P (base0))
9074 && (((TREE_CODE (base1) == VAR_DECL
9075 || TREE_CODE (base1) == PARM_DECL)
9076 && (targetm.binds_local_p (base1)
9077 || CONSTANT_CLASS_P (base0)))
9078 || CONSTANT_CLASS_P (base1)))
9080 if (code == EQ_EXPR)
9081 return omit_two_operands (type, boolean_false_node, arg0, arg1);
9082 else if (code == NE_EXPR)
9083 return omit_two_operands (type, boolean_true_node, arg0, arg1);
9085 /* For equal offsets we can simplify to a comparison of the
9087 else if (bitpos0 == bitpos1
9089 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
9091 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
9092 && ((offset0 == offset1)
9093 || (offset0 && offset1
9094 && operand_equal_p (offset0, offset1, 0))))
9097 base0 = build_fold_addr_expr (base0);
9099 base1 = build_fold_addr_expr (base1);
9100 return fold_build2 (code, type, base0, base1);
9104 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
9105 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
9106 the resulting offset is smaller in absolute value than the
9108 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9109 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
9110 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9111 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9112 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
9113 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9114 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
9116 tree const1 = TREE_OPERAND (arg0, 1);
9117 tree const2 = TREE_OPERAND (arg1, 1);
9118 tree variable1 = TREE_OPERAND (arg0, 0);
9119 tree variable2 = TREE_OPERAND (arg1, 0);
9121 const char * const warnmsg = G_("assuming signed overflow does not "
9122 "occur when combining constants around "
9125 /* Put the constant on the side where it doesn't overflow and is
9126 of lower absolute value than before. */
9127 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9128 ? MINUS_EXPR : PLUS_EXPR,
9130 if (!TREE_OVERFLOW (cst)
9131 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
9133 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9134 return fold_build2 (code, type,
9136 fold_build2 (TREE_CODE (arg1), TREE_TYPE (arg1),
9140 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
9141 ? MINUS_EXPR : PLUS_EXPR,
9143 if (!TREE_OVERFLOW (cst)
9144 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
9146 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
9147 return fold_build2 (code, type,
9148 fold_build2 (TREE_CODE (arg0), TREE_TYPE (arg0),
9154 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
9155 signed arithmetic case. That form is created by the compiler
9156 often enough for folding it to be of value. One example is in
9157 computing loop trip counts after Operator Strength Reduction. */
9158 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
9159 && TREE_CODE (arg0) == MULT_EXPR
9160 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9161 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
9162 && integer_zerop (arg1))
9164 tree const1 = TREE_OPERAND (arg0, 1);
9165 tree const2 = arg1; /* zero */
9166 tree variable1 = TREE_OPERAND (arg0, 0);
9167 enum tree_code cmp_code = code;
9169 gcc_assert (!integer_zerop (const1));
9171 fold_overflow_warning (("assuming signed overflow does not occur when "
9172 "eliminating multiplication in comparison "
9174 WARN_STRICT_OVERFLOW_COMPARISON);
9176 /* If const1 is negative we swap the sense of the comparison. */
9177 if (tree_int_cst_sgn (const1) < 0)
9178 cmp_code = swap_tree_comparison (cmp_code);
9180 return fold_build2 (cmp_code, type, variable1, const2);
9183 tem = maybe_canonicalize_comparison (code, type, op0, op1);
9187 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
9189 tree targ0 = strip_float_extensions (arg0);
9190 tree targ1 = strip_float_extensions (arg1);
9191 tree newtype = TREE_TYPE (targ0);
9193 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
9194 newtype = TREE_TYPE (targ1);
9196 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
9197 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
9198 return fold_build2 (code, type, fold_convert (newtype, targ0),
9199 fold_convert (newtype, targ1));
9201 /* (-a) CMP (-b) -> b CMP a */
9202 if (TREE_CODE (arg0) == NEGATE_EXPR
9203 && TREE_CODE (arg1) == NEGATE_EXPR)
9204 return fold_build2 (code, type, TREE_OPERAND (arg1, 0),
9205 TREE_OPERAND (arg0, 0));
9207 if (TREE_CODE (arg1) == REAL_CST)
9209 REAL_VALUE_TYPE cst;
9210 cst = TREE_REAL_CST (arg1);
9212 /* (-a) CMP CST -> a swap(CMP) (-CST) */
9213 if (TREE_CODE (arg0) == NEGATE_EXPR)
9214 return fold_build2 (swap_tree_comparison (code), type,
9215 TREE_OPERAND (arg0, 0),
9216 build_real (TREE_TYPE (arg1),
9217 REAL_VALUE_NEGATE (cst)));
9219 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
9220 /* a CMP (-0) -> a CMP 0 */
9221 if (REAL_VALUE_MINUS_ZERO (cst))
9222 return fold_build2 (code, type, arg0,
9223 build_real (TREE_TYPE (arg1), dconst0));
9225 /* x != NaN is always true, other ops are always false. */
9226 if (REAL_VALUE_ISNAN (cst)
9227 && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
9229 tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
9230 return omit_one_operand (type, tem, arg0);
9233 /* Fold comparisons against infinity. */
9234 if (REAL_VALUE_ISINF (cst)
9235 && MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1))))
9237 tem = fold_inf_compare (code, type, arg0, arg1);
9238 if (tem != NULL_TREE)
9243 /* If this is a comparison of a real constant with a PLUS_EXPR
9244 or a MINUS_EXPR of a real constant, we can convert it into a
9245 comparison with a revised real constant as long as no overflow
9246 occurs when unsafe_math_optimizations are enabled. */
9247 if (flag_unsafe_math_optimizations
9248 && TREE_CODE (arg1) == REAL_CST
9249 && (TREE_CODE (arg0) == PLUS_EXPR
9250 || TREE_CODE (arg0) == MINUS_EXPR)
9251 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
9252 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
9253 ? MINUS_EXPR : PLUS_EXPR,
9254 arg1, TREE_OPERAND (arg0, 1), 0))
9255 && !TREE_OVERFLOW (tem))
9256 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
9258 /* Likewise, we can simplify a comparison of a real constant with
9259 a MINUS_EXPR whose first operand is also a real constant, i.e.
9260 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
9261 floating-point types only if -fassociative-math is set. */
9262 if (flag_associative_math
9263 && TREE_CODE (arg1) == REAL_CST
9264 && TREE_CODE (arg0) == MINUS_EXPR
9265 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
9266 && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
9268 && !TREE_OVERFLOW (tem))
9269 return fold_build2 (swap_tree_comparison (code), type,
9270 TREE_OPERAND (arg0, 1), tem);
9272 /* Fold comparisons against built-in math functions. */
9273 if (TREE_CODE (arg1) == REAL_CST
9274 && flag_unsafe_math_optimizations
9275 && ! flag_errno_math)
9277 enum built_in_function fcode = builtin_mathfn_code (arg0);
9279 if (fcode != END_BUILTINS)
9281 tem = fold_mathfn_compare (fcode, code, type, arg0, arg1);
9282 if (tem != NULL_TREE)
9288 if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
9289 && CONVERT_EXPR_P (arg0))
9291 /* If we are widening one operand of an integer comparison,
9292 see if the other operand is similarly being widened. Perhaps we
9293 can do the comparison in the narrower type. */
9294 tem = fold_widened_comparison (code, type, arg0, arg1);
9298 /* Or if we are changing signedness. */
9299 tem = fold_sign_changed_comparison (code, type, arg0, arg1);
9304 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
9305 constant, we can simplify it. */
9306 if (TREE_CODE (arg1) == INTEGER_CST
9307 && (TREE_CODE (arg0) == MIN_EXPR
9308 || TREE_CODE (arg0) == MAX_EXPR)
9309 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9311 tem = optimize_minmax_comparison (code, type, op0, op1);
9316 /* Simplify comparison of something with itself. (For IEEE
9317 floating-point, we can only do some of these simplifications.) */
9318 if (operand_equal_p (arg0, arg1, 0))
9323 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9324 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9325 return constant_boolean_node (1, type);
9330 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9331 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9332 return constant_boolean_node (1, type);
9333 return fold_build2 (EQ_EXPR, type, arg0, arg1);
9336 /* For NE, we can only do this simplification if integer
9337 or we don't honor IEEE floating point NaNs. */
9338 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9339 && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9341 /* ... fall through ... */
9344 return constant_boolean_node (0, type);
9350 /* If we are comparing an expression that just has comparisons
9351 of two integer values, arithmetic expressions of those comparisons,
9352 and constants, we can simplify it. There are only three cases
9353 to check: the two values can either be equal, the first can be
9354 greater, or the second can be greater. Fold the expression for
9355 those three values. Since each value must be 0 or 1, we have
9356 eight possibilities, each of which corresponds to the constant 0
9357 or 1 or one of the six possible comparisons.
9359 This handles common cases like (a > b) == 0 but also handles
9360 expressions like ((x > y) - (y > x)) > 0, which supposedly
9361 occur in macroized code. */
9363 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9365 tree cval1 = 0, cval2 = 0;
9368 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9369 /* Don't handle degenerate cases here; they should already
9370 have been handled anyway. */
9371 && cval1 != 0 && cval2 != 0
9372 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9373 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9374 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9375 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9376 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9377 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9378 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9380 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9381 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9383 /* We can't just pass T to eval_subst in case cval1 or cval2
9384 was the same as ARG1. */
9387 = fold_build2 (code, type,
9388 eval_subst (arg0, cval1, maxval,
9392 = fold_build2 (code, type,
9393 eval_subst (arg0, cval1, maxval,
9397 = fold_build2 (code, type,
9398 eval_subst (arg0, cval1, minval,
9402 /* All three of these results should be 0 or 1. Confirm they are.
9403 Then use those values to select the proper code to use. */
9405 if (TREE_CODE (high_result) == INTEGER_CST
9406 && TREE_CODE (equal_result) == INTEGER_CST
9407 && TREE_CODE (low_result) == INTEGER_CST)
9409 /* Make a 3-bit mask with the high-order bit being the
9410 value for `>', the next for '=', and the low for '<'. */
9411 switch ((integer_onep (high_result) * 4)
9412 + (integer_onep (equal_result) * 2)
9413 + integer_onep (low_result))
9417 return omit_one_operand (type, integer_zero_node, arg0);
9438 return omit_one_operand (type, integer_one_node, arg0);
9442 return save_expr (build2 (code, type, cval1, cval2));
9443 return fold_build2 (code, type, cval1, cval2);
9448 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9449 into a single range test. */
9450 if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9451 || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9452 && TREE_CODE (arg1) == INTEGER_CST
9453 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9454 && !integer_zerop (TREE_OPERAND (arg0, 1))
9455 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9456 && !TREE_OVERFLOW (arg1))
9458 tem = fold_div_compare (code, type, arg0, arg1);
9459 if (tem != NULL_TREE)
9463 /* Fold ~X op ~Y as Y op X. */
9464 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9465 && TREE_CODE (arg1) == BIT_NOT_EXPR)
9467 tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9468 return fold_build2 (code, type,
9469 fold_convert (cmp_type, TREE_OPERAND (arg1, 0)),
9470 TREE_OPERAND (arg0, 0));
9473 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
9474 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9475 && TREE_CODE (arg1) == INTEGER_CST)
9477 tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9478 return fold_build2 (swap_tree_comparison (code), type,
9479 TREE_OPERAND (arg0, 0),
9480 fold_build1 (BIT_NOT_EXPR, cmp_type,
9481 fold_convert (cmp_type, arg1)));
9488 /* Subroutine of fold_binary. Optimize complex multiplications of the
9489 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
9490 argument EXPR represents the expression "z" of type TYPE. */
9493 fold_mult_zconjz (tree type, tree expr)
9495 tree itype = TREE_TYPE (type);
9496 tree rpart, ipart, tem;
9498 if (TREE_CODE (expr) == COMPLEX_EXPR)
9500 rpart = TREE_OPERAND (expr, 0);
9501 ipart = TREE_OPERAND (expr, 1);
9503 else if (TREE_CODE (expr) == COMPLEX_CST)
9505 rpart = TREE_REALPART (expr);
9506 ipart = TREE_IMAGPART (expr);
9510 expr = save_expr (expr);
9511 rpart = fold_build1 (REALPART_EXPR, itype, expr);
9512 ipart = fold_build1 (IMAGPART_EXPR, itype, expr);
9515 rpart = save_expr (rpart);
9516 ipart = save_expr (ipart);
9517 tem = fold_build2 (PLUS_EXPR, itype,
9518 fold_build2 (MULT_EXPR, itype, rpart, rpart),
9519 fold_build2 (MULT_EXPR, itype, ipart, ipart));
9520 return fold_build2 (COMPLEX_EXPR, type, tem,
9521 fold_convert (itype, integer_zero_node));
9525 /* Subroutine of fold_binary. If P is the value of EXPR, computes
9526 power-of-two M and (arbitrary) N such that M divides (P-N). This condition
9527 guarantees that P and N have the same least significant log2(M) bits.
9528 N is not otherwise constrained. In particular, N is not normalized to
9529 0 <= N < M as is common. In general, the precise value of P is unknown.
9530 M is chosen as large as possible such that constant N can be determined.
9532 Returns M and sets *RESIDUE to N.
9534 If ALLOW_FUNC_ALIGN is true, do take functions' DECL_ALIGN_UNIT into
9535 account. This is not always possible due to PR 35705.
9538 static unsigned HOST_WIDE_INT
9539 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue,
9540 bool allow_func_align)
9542 enum tree_code code;
9546 code = TREE_CODE (expr);
9547 if (code == ADDR_EXPR)
9549 expr = TREE_OPERAND (expr, 0);
9550 if (handled_component_p (expr))
9552 HOST_WIDE_INT bitsize, bitpos;
9554 enum machine_mode mode;
9555 int unsignedp, volatilep;
9557 expr = get_inner_reference (expr, &bitsize, &bitpos, &offset,
9558 &mode, &unsignedp, &volatilep, false);
9559 *residue = bitpos / BITS_PER_UNIT;
9562 if (TREE_CODE (offset) == INTEGER_CST)
9563 *residue += TREE_INT_CST_LOW (offset);
9565 /* We don't handle more complicated offset expressions. */
9571 && (allow_func_align || TREE_CODE (expr) != FUNCTION_DECL))
9572 return DECL_ALIGN_UNIT (expr);
9574 else if (code == POINTER_PLUS_EXPR)
9577 unsigned HOST_WIDE_INT modulus;
9578 enum tree_code inner_code;
9580 op0 = TREE_OPERAND (expr, 0);
9582 modulus = get_pointer_modulus_and_residue (op0, residue,
9585 op1 = TREE_OPERAND (expr, 1);
9587 inner_code = TREE_CODE (op1);
9588 if (inner_code == INTEGER_CST)
9590 *residue += TREE_INT_CST_LOW (op1);
9593 else if (inner_code == MULT_EXPR)
9595 op1 = TREE_OPERAND (op1, 1);
9596 if (TREE_CODE (op1) == INTEGER_CST)
9598 unsigned HOST_WIDE_INT align;
9600 /* Compute the greatest power-of-2 divisor of op1. */
9601 align = TREE_INT_CST_LOW (op1);
9604 /* If align is non-zero and less than *modulus, replace
9605 *modulus with align., If align is 0, then either op1 is 0
9606 or the greatest power-of-2 divisor of op1 doesn't fit in an
9607 unsigned HOST_WIDE_INT. In either case, no additional
9608 constraint is imposed. */
9610 modulus = MIN (modulus, align);
9617 /* If we get here, we were unable to determine anything useful about the
9623 /* Fold a binary expression of code CODE and type TYPE with operands
9624 OP0 and OP1. Return the folded expression if folding is
9625 successful. Otherwise, return NULL_TREE. */
9628 fold_binary (enum tree_code code, tree type, tree op0, tree op1)
9630 enum tree_code_class kind = TREE_CODE_CLASS (code);
9631 tree arg0, arg1, tem;
9632 tree t1 = NULL_TREE;
9633 bool strict_overflow_p;
9635 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9636 && TREE_CODE_LENGTH (code) == 2
9638 && op1 != NULL_TREE);
9643 /* Strip any conversions that don't change the mode. This is
9644 safe for every expression, except for a comparison expression
9645 because its signedness is derived from its operands. So, in
9646 the latter case, only strip conversions that don't change the
9647 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9650 Note that this is done as an internal manipulation within the
9651 constant folder, in order to find the simplest representation
9652 of the arguments so that their form can be studied. In any
9653 cases, the appropriate type conversions should be put back in
9654 the tree that will get out of the constant folder. */
9656 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9658 STRIP_SIGN_NOPS (arg0);
9659 STRIP_SIGN_NOPS (arg1);
9667 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9668 constant but we can't do arithmetic on them. */
9669 if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9670 || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
9671 || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
9672 || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
9673 || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
9674 || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
9676 if (kind == tcc_binary)
9678 /* Make sure type and arg0 have the same saturating flag. */
9679 gcc_assert (TYPE_SATURATING (type)
9680 == TYPE_SATURATING (TREE_TYPE (arg0)));
9681 tem = const_binop (code, arg0, arg1, 0);
9683 else if (kind == tcc_comparison)
9684 tem = fold_relational_const (code, type, arg0, arg1);
9688 if (tem != NULL_TREE)
9690 if (TREE_TYPE (tem) != type)
9691 tem = fold_convert (type, tem);
9696 /* If this is a commutative operation, and ARG0 is a constant, move it
9697 to ARG1 to reduce the number of tests below. */
9698 if (commutative_tree_code (code)
9699 && tree_swap_operands_p (arg0, arg1, true))
9700 return fold_build2 (code, type, op1, op0);
9702 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9704 First check for cases where an arithmetic operation is applied to a
9705 compound, conditional, or comparison operation. Push the arithmetic
9706 operation inside the compound or conditional to see if any folding
9707 can then be done. Convert comparison to conditional for this purpose.
9708 The also optimizes non-constant cases that used to be done in
9711 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9712 one of the operands is a comparison and the other is a comparison, a
9713 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9714 code below would make the expression more complex. Change it to a
9715 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9716 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9718 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9719 || code == EQ_EXPR || code == NE_EXPR)
9720 && ((truth_value_p (TREE_CODE (arg0))
9721 && (truth_value_p (TREE_CODE (arg1))
9722 || (TREE_CODE (arg1) == BIT_AND_EXPR
9723 && integer_onep (TREE_OPERAND (arg1, 1)))))
9724 || (truth_value_p (TREE_CODE (arg1))
9725 && (truth_value_p (TREE_CODE (arg0))
9726 || (TREE_CODE (arg0) == BIT_AND_EXPR
9727 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9729 tem = fold_build2 (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9730 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9733 fold_convert (boolean_type_node, arg0),
9734 fold_convert (boolean_type_node, arg1));
9736 if (code == EQ_EXPR)
9737 tem = invert_truthvalue (tem);
9739 return fold_convert (type, tem);
9742 if (TREE_CODE_CLASS (code) == tcc_binary
9743 || TREE_CODE_CLASS (code) == tcc_comparison)
9745 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9746 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9747 fold_build2 (code, type,
9748 fold_convert (TREE_TYPE (op0),
9749 TREE_OPERAND (arg0, 1)),
9751 if (TREE_CODE (arg1) == COMPOUND_EXPR
9752 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9753 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9754 fold_build2 (code, type, op0,
9755 fold_convert (TREE_TYPE (op1),
9756 TREE_OPERAND (arg1, 1))));
9758 if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
9760 tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
9762 /*cond_first_p=*/1);
9763 if (tem != NULL_TREE)
9767 if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
9769 tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
9771 /*cond_first_p=*/0);
9772 if (tem != NULL_TREE)
9779 case POINTER_PLUS_EXPR:
9780 /* 0 +p index -> (type)index */
9781 if (integer_zerop (arg0))
9782 return non_lvalue (fold_convert (type, arg1));
9784 /* PTR +p 0 -> PTR */
9785 if (integer_zerop (arg1))
9786 return non_lvalue (fold_convert (type, arg0));
9788 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9789 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9790 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9791 return fold_convert (type, fold_build2 (PLUS_EXPR, sizetype,
9792 fold_convert (sizetype, arg1),
9793 fold_convert (sizetype, arg0)));
9795 /* index +p PTR -> PTR +p index */
9796 if (POINTER_TYPE_P (TREE_TYPE (arg1))
9797 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9798 return fold_build2 (POINTER_PLUS_EXPR, type,
9799 fold_convert (type, arg1),
9800 fold_convert (sizetype, arg0));
9802 /* (PTR +p B) +p A -> PTR +p (B + A) */
9803 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9806 tree arg01 = fold_convert (sizetype, TREE_OPERAND (arg0, 1));
9807 tree arg00 = TREE_OPERAND (arg0, 0);
9808 inner = fold_build2 (PLUS_EXPR, sizetype,
9809 arg01, fold_convert (sizetype, arg1));
9810 return fold_convert (type,
9811 fold_build2 (POINTER_PLUS_EXPR,
9812 TREE_TYPE (arg00), arg00, inner));
9815 /* PTR_CST +p CST -> CST1 */
9816 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9817 return fold_build2 (PLUS_EXPR, type, arg0, fold_convert (type, arg1));
9819 /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
9820 of the array. Loop optimizer sometimes produce this type of
9822 if (TREE_CODE (arg0) == ADDR_EXPR)
9824 tem = try_move_mult_to_index (arg0, fold_convert (sizetype, arg1));
9826 return fold_convert (type, tem);
9832 /* A + (-B) -> A - B */
9833 if (TREE_CODE (arg1) == NEGATE_EXPR)
9834 return fold_build2 (MINUS_EXPR, type,
9835 fold_convert (type, arg0),
9836 fold_convert (type, TREE_OPERAND (arg1, 0)));
9837 /* (-A) + B -> B - A */
9838 if (TREE_CODE (arg0) == NEGATE_EXPR
9839 && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
9840 return fold_build2 (MINUS_EXPR, type,
9841 fold_convert (type, arg1),
9842 fold_convert (type, TREE_OPERAND (arg0, 0)));
9844 if (INTEGRAL_TYPE_P (type))
9846 /* Convert ~A + 1 to -A. */
9847 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9848 && integer_onep (arg1))
9849 return fold_build1 (NEGATE_EXPR, type,
9850 fold_convert (type, TREE_OPERAND (arg0, 0)));
9853 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9854 && !TYPE_OVERFLOW_TRAPS (type))
9856 tree tem = TREE_OPERAND (arg0, 0);
9859 if (operand_equal_p (tem, arg1, 0))
9861 t1 = build_int_cst_type (type, -1);
9862 return omit_one_operand (type, t1, arg1);
9867 if (TREE_CODE (arg1) == BIT_NOT_EXPR
9868 && !TYPE_OVERFLOW_TRAPS (type))
9870 tree tem = TREE_OPERAND (arg1, 0);
9873 if (operand_equal_p (arg0, tem, 0))
9875 t1 = build_int_cst_type (type, -1);
9876 return omit_one_operand (type, t1, arg0);
9880 /* X + (X / CST) * -CST is X % CST. */
9881 if (TREE_CODE (arg1) == MULT_EXPR
9882 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9883 && operand_equal_p (arg0,
9884 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9886 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9887 tree cst1 = TREE_OPERAND (arg1, 1);
9888 tree sum = fold_binary (PLUS_EXPR, TREE_TYPE (cst1), cst1, cst0);
9889 if (sum && integer_zerop (sum))
9890 return fold_convert (type,
9891 fold_build2 (TRUNC_MOD_EXPR,
9892 TREE_TYPE (arg0), arg0, cst0));
9896 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
9897 same or one. Make sure type is not saturating.
9898 fold_plusminus_mult_expr will re-associate. */
9899 if ((TREE_CODE (arg0) == MULT_EXPR
9900 || TREE_CODE (arg1) == MULT_EXPR)
9901 && !TYPE_SATURATING (type)
9902 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9904 tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
9909 if (! FLOAT_TYPE_P (type))
9911 if (integer_zerop (arg1))
9912 return non_lvalue (fold_convert (type, arg0));
9914 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
9915 with a constant, and the two constants have no bits in common,
9916 we should treat this as a BIT_IOR_EXPR since this may produce more
9918 if (TREE_CODE (arg0) == BIT_AND_EXPR
9919 && TREE_CODE (arg1) == BIT_AND_EXPR
9920 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9921 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9922 && integer_zerop (const_binop (BIT_AND_EXPR,
9923 TREE_OPERAND (arg0, 1),
9924 TREE_OPERAND (arg1, 1), 0)))
9926 code = BIT_IOR_EXPR;
9930 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9931 (plus (plus (mult) (mult)) (foo)) so that we can
9932 take advantage of the factoring cases below. */
9933 if (((TREE_CODE (arg0) == PLUS_EXPR
9934 || TREE_CODE (arg0) == MINUS_EXPR)
9935 && TREE_CODE (arg1) == MULT_EXPR)
9936 || ((TREE_CODE (arg1) == PLUS_EXPR
9937 || TREE_CODE (arg1) == MINUS_EXPR)
9938 && TREE_CODE (arg0) == MULT_EXPR))
9940 tree parg0, parg1, parg, marg;
9941 enum tree_code pcode;
9943 if (TREE_CODE (arg1) == MULT_EXPR)
9944 parg = arg0, marg = arg1;
9946 parg = arg1, marg = arg0;
9947 pcode = TREE_CODE (parg);
9948 parg0 = TREE_OPERAND (parg, 0);
9949 parg1 = TREE_OPERAND (parg, 1);
9953 if (TREE_CODE (parg0) == MULT_EXPR
9954 && TREE_CODE (parg1) != MULT_EXPR)
9955 return fold_build2 (pcode, type,
9956 fold_build2 (PLUS_EXPR, type,
9957 fold_convert (type, parg0),
9958 fold_convert (type, marg)),
9959 fold_convert (type, parg1));
9960 if (TREE_CODE (parg0) != MULT_EXPR
9961 && TREE_CODE (parg1) == MULT_EXPR)
9962 return fold_build2 (PLUS_EXPR, type,
9963 fold_convert (type, parg0),
9964 fold_build2 (pcode, type,
9965 fold_convert (type, marg),
9972 /* See if ARG1 is zero and X + ARG1 reduces to X. */
9973 if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
9974 return non_lvalue (fold_convert (type, arg0));
9976 /* Likewise if the operands are reversed. */
9977 if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
9978 return non_lvalue (fold_convert (type, arg1));
9980 /* Convert X + -C into X - C. */
9981 if (TREE_CODE (arg1) == REAL_CST
9982 && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
9984 tem = fold_negate_const (arg1, type);
9985 if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
9986 return fold_build2 (MINUS_EXPR, type,
9987 fold_convert (type, arg0),
9988 fold_convert (type, tem));
9991 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9992 to __complex__ ( x, y ). This is not the same for SNaNs or
9993 if signed zeros are involved. */
9994 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9995 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
9996 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9998 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9999 tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
10000 tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
10001 bool arg0rz = false, arg0iz = false;
10002 if ((arg0r && (arg0rz = real_zerop (arg0r)))
10003 || (arg0i && (arg0iz = real_zerop (arg0i))))
10005 tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
10006 tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
10007 if (arg0rz && arg1i && real_zerop (arg1i))
10009 tree rp = arg1r ? arg1r
10010 : build1 (REALPART_EXPR, rtype, arg1);
10011 tree ip = arg0i ? arg0i
10012 : build1 (IMAGPART_EXPR, rtype, arg0);
10013 return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10015 else if (arg0iz && arg1r && real_zerop (arg1r))
10017 tree rp = arg0r ? arg0r
10018 : build1 (REALPART_EXPR, rtype, arg0);
10019 tree ip = arg1i ? arg1i
10020 : build1 (IMAGPART_EXPR, rtype, arg1);
10021 return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10026 if (flag_unsafe_math_optimizations
10027 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10028 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10029 && (tem = distribute_real_division (code, type, arg0, arg1)))
10032 /* Convert x+x into x*2.0. */
10033 if (operand_equal_p (arg0, arg1, 0)
10034 && SCALAR_FLOAT_TYPE_P (type))
10035 return fold_build2 (MULT_EXPR, type, arg0,
10036 build_real (type, dconst2));
10038 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
10039 We associate floats only if the user has specified
10040 -fassociative-math. */
10041 if (flag_associative_math
10042 && TREE_CODE (arg1) == PLUS_EXPR
10043 && TREE_CODE (arg0) != MULT_EXPR)
10045 tree tree10 = TREE_OPERAND (arg1, 0);
10046 tree tree11 = TREE_OPERAND (arg1, 1);
10047 if (TREE_CODE (tree11) == MULT_EXPR
10048 && TREE_CODE (tree10) == MULT_EXPR)
10051 tree0 = fold_build2 (PLUS_EXPR, type, arg0, tree10);
10052 return fold_build2 (PLUS_EXPR, type, tree0, tree11);
10055 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
10056 We associate floats only if the user has specified
10057 -fassociative-math. */
10058 if (flag_associative_math
10059 && TREE_CODE (arg0) == PLUS_EXPR
10060 && TREE_CODE (arg1) != MULT_EXPR)
10062 tree tree00 = TREE_OPERAND (arg0, 0);
10063 tree tree01 = TREE_OPERAND (arg0, 1);
10064 if (TREE_CODE (tree01) == MULT_EXPR
10065 && TREE_CODE (tree00) == MULT_EXPR)
10068 tree0 = fold_build2 (PLUS_EXPR, type, tree01, arg1);
10069 return fold_build2 (PLUS_EXPR, type, tree00, tree0);
10075 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
10076 is a rotate of A by C1 bits. */
10077 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
10078 is a rotate of A by B bits. */
10080 enum tree_code code0, code1;
10082 code0 = TREE_CODE (arg0);
10083 code1 = TREE_CODE (arg1);
10084 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
10085 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
10086 && operand_equal_p (TREE_OPERAND (arg0, 0),
10087 TREE_OPERAND (arg1, 0), 0)
10088 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
10089 TYPE_UNSIGNED (rtype))
10090 /* Only create rotates in complete modes. Other cases are not
10091 expanded properly. */
10092 && TYPE_PRECISION (rtype) == GET_MODE_PRECISION (TYPE_MODE (rtype)))
10094 tree tree01, tree11;
10095 enum tree_code code01, code11;
10097 tree01 = TREE_OPERAND (arg0, 1);
10098 tree11 = TREE_OPERAND (arg1, 1);
10099 STRIP_NOPS (tree01);
10100 STRIP_NOPS (tree11);
10101 code01 = TREE_CODE (tree01);
10102 code11 = TREE_CODE (tree11);
10103 if (code01 == INTEGER_CST
10104 && code11 == INTEGER_CST
10105 && TREE_INT_CST_HIGH (tree01) == 0
10106 && TREE_INT_CST_HIGH (tree11) == 0
10107 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
10108 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
10109 return build2 (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
10110 code0 == LSHIFT_EXPR ? tree01 : tree11);
10111 else if (code11 == MINUS_EXPR)
10113 tree tree110, tree111;
10114 tree110 = TREE_OPERAND (tree11, 0);
10115 tree111 = TREE_OPERAND (tree11, 1);
10116 STRIP_NOPS (tree110);
10117 STRIP_NOPS (tree111);
10118 if (TREE_CODE (tree110) == INTEGER_CST
10119 && 0 == compare_tree_int (tree110,
10121 (TREE_TYPE (TREE_OPERAND
10123 && operand_equal_p (tree01, tree111, 0))
10124 return build2 ((code0 == LSHIFT_EXPR
10127 type, TREE_OPERAND (arg0, 0), tree01);
10129 else if (code01 == MINUS_EXPR)
10131 tree tree010, tree011;
10132 tree010 = TREE_OPERAND (tree01, 0);
10133 tree011 = TREE_OPERAND (tree01, 1);
10134 STRIP_NOPS (tree010);
10135 STRIP_NOPS (tree011);
10136 if (TREE_CODE (tree010) == INTEGER_CST
10137 && 0 == compare_tree_int (tree010,
10139 (TREE_TYPE (TREE_OPERAND
10141 && operand_equal_p (tree11, tree011, 0))
10142 return build2 ((code0 != LSHIFT_EXPR
10145 type, TREE_OPERAND (arg0, 0), tree11);
10151 /* In most languages, can't associate operations on floats through
10152 parentheses. Rather than remember where the parentheses were, we
10153 don't associate floats at all, unless the user has specified
10154 -fassociative-math.
10155 And, we need to make sure type is not saturating. */
10157 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
10158 && !TYPE_SATURATING (type))
10160 tree var0, con0, lit0, minus_lit0;
10161 tree var1, con1, lit1, minus_lit1;
10164 /* Split both trees into variables, constants, and literals. Then
10165 associate each group together, the constants with literals,
10166 then the result with variables. This increases the chances of
10167 literals being recombined later and of generating relocatable
10168 expressions for the sum of a constant and literal. */
10169 var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
10170 var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
10171 code == MINUS_EXPR);
10173 /* With undefined overflow we can only associate constants
10174 with one variable. */
10175 if (((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
10176 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
10182 if (TREE_CODE (tmp0) == NEGATE_EXPR)
10183 tmp0 = TREE_OPERAND (tmp0, 0);
10184 if (TREE_CODE (tmp1) == NEGATE_EXPR)
10185 tmp1 = TREE_OPERAND (tmp1, 0);
10186 /* The only case we can still associate with two variables
10187 is if they are the same, modulo negation. */
10188 if (!operand_equal_p (tmp0, tmp1, 0))
10192 /* Only do something if we found more than two objects. Otherwise,
10193 nothing has changed and we risk infinite recursion. */
10195 && (2 < ((var0 != 0) + (var1 != 0)
10196 + (con0 != 0) + (con1 != 0)
10197 + (lit0 != 0) + (lit1 != 0)
10198 + (minus_lit0 != 0) + (minus_lit1 != 0))))
10200 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
10201 if (code == MINUS_EXPR)
10204 var0 = associate_trees (var0, var1, code, type);
10205 con0 = associate_trees (con0, con1, code, type);
10206 lit0 = associate_trees (lit0, lit1, code, type);
10207 minus_lit0 = associate_trees (minus_lit0, minus_lit1, code, type);
10209 /* Preserve the MINUS_EXPR if the negative part of the literal is
10210 greater than the positive part. Otherwise, the multiplicative
10211 folding code (i.e extract_muldiv) may be fooled in case
10212 unsigned constants are subtracted, like in the following
10213 example: ((X*2 + 4) - 8U)/2. */
10214 if (minus_lit0 && lit0)
10216 if (TREE_CODE (lit0) == INTEGER_CST
10217 && TREE_CODE (minus_lit0) == INTEGER_CST
10218 && tree_int_cst_lt (lit0, minus_lit0))
10220 minus_lit0 = associate_trees (minus_lit0, lit0,
10226 lit0 = associate_trees (lit0, minus_lit0,
10234 return fold_convert (type,
10235 associate_trees (var0, minus_lit0,
10236 MINUS_EXPR, type));
10239 con0 = associate_trees (con0, minus_lit0,
10241 return fold_convert (type,
10242 associate_trees (var0, con0,
10247 con0 = associate_trees (con0, lit0, code, type);
10248 return fold_convert (type, associate_trees (var0, con0,
10256 /* Pointer simplifications for subtraction, simple reassociations. */
10257 if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
10259 /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
10260 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
10261 && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
10263 tree arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
10264 tree arg01 = fold_convert (type, TREE_OPERAND (arg0, 1));
10265 tree arg10 = fold_convert (type, TREE_OPERAND (arg1, 0));
10266 tree arg11 = fold_convert (type, TREE_OPERAND (arg1, 1));
10267 return fold_build2 (PLUS_EXPR, type,
10268 fold_build2 (MINUS_EXPR, type, arg00, arg10),
10269 fold_build2 (MINUS_EXPR, type, arg01, arg11));
10271 /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
10272 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
10274 tree arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
10275 tree arg01 = fold_convert (type, TREE_OPERAND (arg0, 1));
10276 tree tmp = fold_binary (MINUS_EXPR, type, arg00, fold_convert (type, arg1));
10278 return fold_build2 (PLUS_EXPR, type, tmp, arg01);
10281 /* A - (-B) -> A + B */
10282 if (TREE_CODE (arg1) == NEGATE_EXPR)
10283 return fold_build2 (PLUS_EXPR, type, op0,
10284 fold_convert (type, TREE_OPERAND (arg1, 0)));
10285 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
10286 if (TREE_CODE (arg0) == NEGATE_EXPR
10287 && (FLOAT_TYPE_P (type)
10288 || INTEGRAL_TYPE_P (type))
10289 && negate_expr_p (arg1)
10290 && reorder_operands_p (arg0, arg1))
10291 return fold_build2 (MINUS_EXPR, type,
10292 fold_convert (type, negate_expr (arg1)),
10293 fold_convert (type, TREE_OPERAND (arg0, 0)));
10294 /* Convert -A - 1 to ~A. */
10295 if (INTEGRAL_TYPE_P (type)
10296 && TREE_CODE (arg0) == NEGATE_EXPR
10297 && integer_onep (arg1)
10298 && !TYPE_OVERFLOW_TRAPS (type))
10299 return fold_build1 (BIT_NOT_EXPR, type,
10300 fold_convert (type, TREE_OPERAND (arg0, 0)));
10302 /* Convert -1 - A to ~A. */
10303 if (INTEGRAL_TYPE_P (type)
10304 && integer_all_onesp (arg0))
10305 return fold_build1 (BIT_NOT_EXPR, type, op1);
10308 /* X - (X / CST) * CST is X % CST. */
10309 if (INTEGRAL_TYPE_P (type)
10310 && TREE_CODE (arg1) == MULT_EXPR
10311 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
10312 && operand_equal_p (arg0,
10313 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
10314 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
10315 TREE_OPERAND (arg1, 1), 0))
10316 return fold_convert (type,
10317 fold_build2 (TRUNC_MOD_EXPR, TREE_TYPE (arg0),
10318 arg0, TREE_OPERAND (arg1, 1)));
10320 if (! FLOAT_TYPE_P (type))
10322 if (integer_zerop (arg0))
10323 return negate_expr (fold_convert (type, arg1));
10324 if (integer_zerop (arg1))
10325 return non_lvalue (fold_convert (type, arg0));
10327 /* Fold A - (A & B) into ~B & A. */
10328 if (!TREE_SIDE_EFFECTS (arg0)
10329 && TREE_CODE (arg1) == BIT_AND_EXPR)
10331 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
10333 tree arg10 = fold_convert (type, TREE_OPERAND (arg1, 0));
10334 return fold_build2 (BIT_AND_EXPR, type,
10335 fold_build1 (BIT_NOT_EXPR, type, arg10),
10336 fold_convert (type, arg0));
10338 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10340 tree arg11 = fold_convert (type, TREE_OPERAND (arg1, 1));
10341 return fold_build2 (BIT_AND_EXPR, type,
10342 fold_build1 (BIT_NOT_EXPR, type, arg11),
10343 fold_convert (type, arg0));
10347 /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
10348 any power of 2 minus 1. */
10349 if (TREE_CODE (arg0) == BIT_AND_EXPR
10350 && TREE_CODE (arg1) == BIT_AND_EXPR
10351 && operand_equal_p (TREE_OPERAND (arg0, 0),
10352 TREE_OPERAND (arg1, 0), 0))
10354 tree mask0 = TREE_OPERAND (arg0, 1);
10355 tree mask1 = TREE_OPERAND (arg1, 1);
10356 tree tem = fold_build1 (BIT_NOT_EXPR, type, mask0);
10358 if (operand_equal_p (tem, mask1, 0))
10360 tem = fold_build2 (BIT_XOR_EXPR, type,
10361 TREE_OPERAND (arg0, 0), mask1);
10362 return fold_build2 (MINUS_EXPR, type, tem, mask1);
10367 /* See if ARG1 is zero and X - ARG1 reduces to X. */
10368 else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
10369 return non_lvalue (fold_convert (type, arg0));
10371 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
10372 ARG0 is zero and X + ARG0 reduces to X, since that would mean
10373 (-ARG1 + ARG0) reduces to -ARG1. */
10374 else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10375 return negate_expr (fold_convert (type, arg1));
10377 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10378 __complex__ ( x, -y ). This is not the same for SNaNs or if
10379 signed zeros are involved. */
10380 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10381 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10382 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10384 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10385 tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
10386 tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
10387 bool arg0rz = false, arg0iz = false;
10388 if ((arg0r && (arg0rz = real_zerop (arg0r)))
10389 || (arg0i && (arg0iz = real_zerop (arg0i))))
10391 tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
10392 tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
10393 if (arg0rz && arg1i && real_zerop (arg1i))
10395 tree rp = fold_build1 (NEGATE_EXPR, rtype,
10397 : build1 (REALPART_EXPR, rtype, arg1));
10398 tree ip = arg0i ? arg0i
10399 : build1 (IMAGPART_EXPR, rtype, arg0);
10400 return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10402 else if (arg0iz && arg1r && real_zerop (arg1r))
10404 tree rp = arg0r ? arg0r
10405 : build1 (REALPART_EXPR, rtype, arg0);
10406 tree ip = fold_build1 (NEGATE_EXPR, rtype,
10408 : build1 (IMAGPART_EXPR, rtype, arg1));
10409 return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10414 /* Fold &x - &x. This can happen from &x.foo - &x.
10415 This is unsafe for certain floats even in non-IEEE formats.
10416 In IEEE, it is unsafe because it does wrong for NaNs.
10417 Also note that operand_equal_p is always false if an operand
10420 if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
10421 && operand_equal_p (arg0, arg1, 0))
10422 return fold_convert (type, integer_zero_node);
10424 /* A - B -> A + (-B) if B is easily negatable. */
10425 if (negate_expr_p (arg1)
10426 && ((FLOAT_TYPE_P (type)
10427 /* Avoid this transformation if B is a positive REAL_CST. */
10428 && (TREE_CODE (arg1) != REAL_CST
10429 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10430 || INTEGRAL_TYPE_P (type)))
10431 return fold_build2 (PLUS_EXPR, type,
10432 fold_convert (type, arg0),
10433 fold_convert (type, negate_expr (arg1)));
10435 /* Try folding difference of addresses. */
10437 HOST_WIDE_INT diff;
10439 if ((TREE_CODE (arg0) == ADDR_EXPR
10440 || TREE_CODE (arg1) == ADDR_EXPR)
10441 && ptr_difference_const (arg0, arg1, &diff))
10442 return build_int_cst_type (type, diff);
10445 /* Fold &a[i] - &a[j] to i-j. */
10446 if (TREE_CODE (arg0) == ADDR_EXPR
10447 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10448 && TREE_CODE (arg1) == ADDR_EXPR
10449 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10451 tree aref0 = TREE_OPERAND (arg0, 0);
10452 tree aref1 = TREE_OPERAND (arg1, 0);
10453 if (operand_equal_p (TREE_OPERAND (aref0, 0),
10454 TREE_OPERAND (aref1, 0), 0))
10456 tree op0 = fold_convert (type, TREE_OPERAND (aref0, 1));
10457 tree op1 = fold_convert (type, TREE_OPERAND (aref1, 1));
10458 tree esz = array_ref_element_size (aref0);
10459 tree diff = build2 (MINUS_EXPR, type, op0, op1);
10460 return fold_build2 (MULT_EXPR, type, diff,
10461 fold_convert (type, esz));
10466 if (flag_unsafe_math_optimizations
10467 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10468 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10469 && (tem = distribute_real_division (code, type, arg0, arg1)))
10472 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
10473 same or one. Make sure type is not saturating.
10474 fold_plusminus_mult_expr will re-associate. */
10475 if ((TREE_CODE (arg0) == MULT_EXPR
10476 || TREE_CODE (arg1) == MULT_EXPR)
10477 && !TYPE_SATURATING (type)
10478 && (!FLOAT_TYPE_P (type) || flag_associative_math))
10480 tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
10488 /* (-A) * (-B) -> A * B */
10489 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10490 return fold_build2 (MULT_EXPR, type,
10491 fold_convert (type, TREE_OPERAND (arg0, 0)),
10492 fold_convert (type, negate_expr (arg1)));
10493 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10494 return fold_build2 (MULT_EXPR, type,
10495 fold_convert (type, negate_expr (arg0)),
10496 fold_convert (type, TREE_OPERAND (arg1, 0)));
10498 if (! FLOAT_TYPE_P (type))
10500 if (integer_zerop (arg1))
10501 return omit_one_operand (type, arg1, arg0);
10502 if (integer_onep (arg1))
10503 return non_lvalue (fold_convert (type, arg0));
10504 /* Transform x * -1 into -x. Make sure to do the negation
10505 on the original operand with conversions not stripped
10506 because we can only strip non-sign-changing conversions. */
10507 if (integer_all_onesp (arg1))
10508 return fold_convert (type, negate_expr (op0));
10509 /* Transform x * -C into -x * C if x is easily negatable. */
10510 if (TREE_CODE (arg1) == INTEGER_CST
10511 && tree_int_cst_sgn (arg1) == -1
10512 && negate_expr_p (arg0)
10513 && (tem = negate_expr (arg1)) != arg1
10514 && !TREE_OVERFLOW (tem))
10515 return fold_build2 (MULT_EXPR, type,
10516 fold_convert (type, negate_expr (arg0)), tem);
10518 /* (a * (1 << b)) is (a << b) */
10519 if (TREE_CODE (arg1) == LSHIFT_EXPR
10520 && integer_onep (TREE_OPERAND (arg1, 0)))
10521 return fold_build2 (LSHIFT_EXPR, type, op0,
10522 TREE_OPERAND (arg1, 1));
10523 if (TREE_CODE (arg0) == LSHIFT_EXPR
10524 && integer_onep (TREE_OPERAND (arg0, 0)))
10525 return fold_build2 (LSHIFT_EXPR, type, op1,
10526 TREE_OPERAND (arg0, 1));
10528 /* (A + A) * C -> A * 2 * C */
10529 if (TREE_CODE (arg0) == PLUS_EXPR
10530 && TREE_CODE (arg1) == INTEGER_CST
10531 && operand_equal_p (TREE_OPERAND (arg0, 0),
10532 TREE_OPERAND (arg0, 1), 0))
10533 return fold_build2 (MULT_EXPR, type,
10534 omit_one_operand (type, TREE_OPERAND (arg0, 0),
10535 TREE_OPERAND (arg0, 1)),
10536 fold_build2 (MULT_EXPR, type,
10537 build_int_cst (type, 2) , arg1));
10539 strict_overflow_p = false;
10540 if (TREE_CODE (arg1) == INTEGER_CST
10541 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10542 &strict_overflow_p)))
10544 if (strict_overflow_p)
10545 fold_overflow_warning (("assuming signed overflow does not "
10546 "occur when simplifying "
10548 WARN_STRICT_OVERFLOW_MISC);
10549 return fold_convert (type, tem);
10552 /* Optimize z * conj(z) for integer complex numbers. */
10553 if (TREE_CODE (arg0) == CONJ_EXPR
10554 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10555 return fold_mult_zconjz (type, arg1);
10556 if (TREE_CODE (arg1) == CONJ_EXPR
10557 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10558 return fold_mult_zconjz (type, arg0);
10562 /* Maybe fold x * 0 to 0. The expressions aren't the same
10563 when x is NaN, since x * 0 is also NaN. Nor are they the
10564 same in modes with signed zeros, since multiplying a
10565 negative value by 0 gives -0, not +0. */
10566 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10567 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10568 && real_zerop (arg1))
10569 return omit_one_operand (type, arg1, arg0);
10570 /* In IEEE floating point, x*1 is not equivalent to x for snans. */
10571 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10572 && real_onep (arg1))
10573 return non_lvalue (fold_convert (type, arg0));
10575 /* Transform x * -1.0 into -x. */
10576 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10577 && real_minus_onep (arg1))
10578 return fold_convert (type, negate_expr (arg0));
10580 /* Convert (C1/X)*C2 into (C1*C2)/X. This transformation may change
10581 the result for floating point types due to rounding so it is applied
10582 only if -fassociative-math was specify. */
10583 if (flag_associative_math
10584 && TREE_CODE (arg0) == RDIV_EXPR
10585 && TREE_CODE (arg1) == REAL_CST
10586 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
10588 tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
10591 return fold_build2 (RDIV_EXPR, type, tem,
10592 TREE_OPERAND (arg0, 1));
10595 /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y. */
10596 if (operand_equal_p (arg0, arg1, 0))
10598 tree tem = fold_strip_sign_ops (arg0);
10599 if (tem != NULL_TREE)
10601 tem = fold_convert (type, tem);
10602 return fold_build2 (MULT_EXPR, type, tem, tem);
10606 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10607 This is not the same for NaNs or if signed zeros are
10609 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10610 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10611 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10612 && TREE_CODE (arg1) == COMPLEX_CST
10613 && real_zerop (TREE_REALPART (arg1)))
10615 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10616 if (real_onep (TREE_IMAGPART (arg1)))
10617 return fold_build2 (COMPLEX_EXPR, type,
10618 negate_expr (fold_build1 (IMAGPART_EXPR,
10620 fold_build1 (REALPART_EXPR, rtype, arg0));
10621 else if (real_minus_onep (TREE_IMAGPART (arg1)))
10622 return fold_build2 (COMPLEX_EXPR, type,
10623 fold_build1 (IMAGPART_EXPR, rtype, arg0),
10624 negate_expr (fold_build1 (REALPART_EXPR,
10628 /* Optimize z * conj(z) for floating point complex numbers.
10629 Guarded by flag_unsafe_math_optimizations as non-finite
10630 imaginary components don't produce scalar results. */
10631 if (flag_unsafe_math_optimizations
10632 && TREE_CODE (arg0) == CONJ_EXPR
10633 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10634 return fold_mult_zconjz (type, arg1);
10635 if (flag_unsafe_math_optimizations
10636 && TREE_CODE (arg1) == CONJ_EXPR
10637 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10638 return fold_mult_zconjz (type, arg0);
10640 if (flag_unsafe_math_optimizations)
10642 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10643 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10645 /* Optimizations of root(...)*root(...). */
10646 if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
10649 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10650 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10652 /* Optimize sqrt(x)*sqrt(x) as x. */
10653 if (BUILTIN_SQRT_P (fcode0)
10654 && operand_equal_p (arg00, arg10, 0)
10655 && ! HONOR_SNANS (TYPE_MODE (type)))
10658 /* Optimize root(x)*root(y) as root(x*y). */
10659 rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10660 arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
10661 return build_call_expr (rootfn, 1, arg);
10664 /* Optimize expN(x)*expN(y) as expN(x+y). */
10665 if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
10667 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10668 tree arg = fold_build2 (PLUS_EXPR, type,
10669 CALL_EXPR_ARG (arg0, 0),
10670 CALL_EXPR_ARG (arg1, 0));
10671 return build_call_expr (expfn, 1, arg);
10674 /* Optimizations of pow(...)*pow(...). */
10675 if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
10676 || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
10677 || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
10679 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10680 tree arg01 = CALL_EXPR_ARG (arg0, 1);
10681 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10682 tree arg11 = CALL_EXPR_ARG (arg1, 1);
10684 /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y). */
10685 if (operand_equal_p (arg01, arg11, 0))
10687 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10688 tree arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
10689 return build_call_expr (powfn, 2, arg, arg01);
10692 /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z). */
10693 if (operand_equal_p (arg00, arg10, 0))
10695 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10696 tree arg = fold_build2 (PLUS_EXPR, type, arg01, arg11);
10697 return build_call_expr (powfn, 2, arg00, arg);
10701 /* Optimize tan(x)*cos(x) as sin(x). */
10702 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
10703 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
10704 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
10705 || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
10706 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
10707 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
10708 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10709 CALL_EXPR_ARG (arg1, 0), 0))
10711 tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
10713 if (sinfn != NULL_TREE)
10714 return build_call_expr (sinfn, 1, CALL_EXPR_ARG (arg0, 0));
10717 /* Optimize x*pow(x,c) as pow(x,c+1). */
10718 if (fcode1 == BUILT_IN_POW
10719 || fcode1 == BUILT_IN_POWF
10720 || fcode1 == BUILT_IN_POWL)
10722 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10723 tree arg11 = CALL_EXPR_ARG (arg1, 1);
10724 if (TREE_CODE (arg11) == REAL_CST
10725 && !TREE_OVERFLOW (arg11)
10726 && operand_equal_p (arg0, arg10, 0))
10728 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10732 c = TREE_REAL_CST (arg11);
10733 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10734 arg = build_real (type, c);
10735 return build_call_expr (powfn, 2, arg0, arg);
10739 /* Optimize pow(x,c)*x as pow(x,c+1). */
10740 if (fcode0 == BUILT_IN_POW
10741 || fcode0 == BUILT_IN_POWF
10742 || fcode0 == BUILT_IN_POWL)
10744 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10745 tree arg01 = CALL_EXPR_ARG (arg0, 1);
10746 if (TREE_CODE (arg01) == REAL_CST
10747 && !TREE_OVERFLOW (arg01)
10748 && operand_equal_p (arg1, arg00, 0))
10750 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10754 c = TREE_REAL_CST (arg01);
10755 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10756 arg = build_real (type, c);
10757 return build_call_expr (powfn, 2, arg1, arg);
10761 /* Optimize x*x as pow(x,2.0), which is expanded as x*x. */
10762 if (optimize_function_for_speed_p (cfun)
10763 && operand_equal_p (arg0, arg1, 0))
10765 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
10769 tree arg = build_real (type, dconst2);
10770 return build_call_expr (powfn, 2, arg0, arg);
10779 if (integer_all_onesp (arg1))
10780 return omit_one_operand (type, arg1, arg0);
10781 if (integer_zerop (arg1))
10782 return non_lvalue (fold_convert (type, arg0));
10783 if (operand_equal_p (arg0, arg1, 0))
10784 return non_lvalue (fold_convert (type, arg0));
10786 /* ~X | X is -1. */
10787 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10788 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10790 t1 = fold_convert (type, integer_zero_node);
10791 t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10792 return omit_one_operand (type, t1, arg1);
10795 /* X | ~X is -1. */
10796 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10797 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10799 t1 = fold_convert (type, integer_zero_node);
10800 t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10801 return omit_one_operand (type, t1, arg0);
10804 /* Canonicalize (X & C1) | C2. */
10805 if (TREE_CODE (arg0) == BIT_AND_EXPR
10806 && TREE_CODE (arg1) == INTEGER_CST
10807 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10809 unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, hi3, lo3, mlo, mhi;
10810 int width = TYPE_PRECISION (type), w;
10811 hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
10812 lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
10813 hi2 = TREE_INT_CST_HIGH (arg1);
10814 lo2 = TREE_INT_CST_LOW (arg1);
10816 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
10817 if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
10818 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10820 if (width > HOST_BITS_PER_WIDE_INT)
10822 mhi = (unsigned HOST_WIDE_INT) -1
10823 >> (2 * HOST_BITS_PER_WIDE_INT - width);
10829 mlo = (unsigned HOST_WIDE_INT) -1
10830 >> (HOST_BITS_PER_WIDE_INT - width);
10833 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
10834 if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
10835 return fold_build2 (BIT_IOR_EXPR, type,
10836 TREE_OPERAND (arg0, 0), arg1);
10838 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10839 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10840 mode which allows further optimizations. */
10847 for (w = BITS_PER_UNIT;
10848 w <= width && w <= HOST_BITS_PER_WIDE_INT;
10851 unsigned HOST_WIDE_INT mask
10852 = (unsigned HOST_WIDE_INT) -1 >> (HOST_BITS_PER_WIDE_INT - w);
10853 if (((lo1 | lo2) & mask) == mask
10854 && (lo1 & ~mask) == 0 && hi1 == 0)
10861 if (hi3 != hi1 || lo3 != lo1)
10862 return fold_build2 (BIT_IOR_EXPR, type,
10863 fold_build2 (BIT_AND_EXPR, type,
10864 TREE_OPERAND (arg0, 0),
10865 build_int_cst_wide (type,
10870 /* (X & Y) | Y is (X, Y). */
10871 if (TREE_CODE (arg0) == BIT_AND_EXPR
10872 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10873 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10874 /* (X & Y) | X is (Y, X). */
10875 if (TREE_CODE (arg0) == BIT_AND_EXPR
10876 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10877 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10878 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
10879 /* X | (X & Y) is (Y, X). */
10880 if (TREE_CODE (arg1) == BIT_AND_EXPR
10881 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10882 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10883 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
10884 /* X | (Y & X) is (Y, X). */
10885 if (TREE_CODE (arg1) == BIT_AND_EXPR
10886 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10887 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10888 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
10890 t1 = distribute_bit_expr (code, type, arg0, arg1);
10891 if (t1 != NULL_TREE)
10894 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
10896 This results in more efficient code for machines without a NAND
10897 instruction. Combine will canonicalize to the first form
10898 which will allow use of NAND instructions provided by the
10899 backend if they exist. */
10900 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10901 && TREE_CODE (arg1) == BIT_NOT_EXPR)
10903 return fold_build1 (BIT_NOT_EXPR, type,
10904 build2 (BIT_AND_EXPR, type,
10905 fold_convert (type,
10906 TREE_OPERAND (arg0, 0)),
10907 fold_convert (type,
10908 TREE_OPERAND (arg1, 0))));
10911 /* See if this can be simplified into a rotate first. If that
10912 is unsuccessful continue in the association code. */
10916 if (integer_zerop (arg1))
10917 return non_lvalue (fold_convert (type, arg0));
10918 if (integer_all_onesp (arg1))
10919 return fold_build1 (BIT_NOT_EXPR, type, op0);
10920 if (operand_equal_p (arg0, arg1, 0))
10921 return omit_one_operand (type, integer_zero_node, arg0);
10923 /* ~X ^ X is -1. */
10924 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10925 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10927 t1 = fold_convert (type, integer_zero_node);
10928 t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10929 return omit_one_operand (type, t1, arg1);
10932 /* X ^ ~X is -1. */
10933 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10934 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10936 t1 = fold_convert (type, integer_zero_node);
10937 t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10938 return omit_one_operand (type, t1, arg0);
10941 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
10942 with a constant, and the two constants have no bits in common,
10943 we should treat this as a BIT_IOR_EXPR since this may produce more
10944 simplifications. */
10945 if (TREE_CODE (arg0) == BIT_AND_EXPR
10946 && TREE_CODE (arg1) == BIT_AND_EXPR
10947 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10948 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10949 && integer_zerop (const_binop (BIT_AND_EXPR,
10950 TREE_OPERAND (arg0, 1),
10951 TREE_OPERAND (arg1, 1), 0)))
10953 code = BIT_IOR_EXPR;
10957 /* (X | Y) ^ X -> Y & ~ X*/
10958 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10959 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10961 tree t2 = TREE_OPERAND (arg0, 1);
10962 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
10964 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10965 fold_convert (type, t1));
10969 /* (Y | X) ^ X -> Y & ~ X*/
10970 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10971 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10973 tree t2 = TREE_OPERAND (arg0, 0);
10974 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
10976 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10977 fold_convert (type, t1));
10981 /* X ^ (X | Y) -> Y & ~ X*/
10982 if (TREE_CODE (arg1) == BIT_IOR_EXPR
10983 && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
10985 tree t2 = TREE_OPERAND (arg1, 1);
10986 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
10988 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10989 fold_convert (type, t1));
10993 /* X ^ (Y | X) -> Y & ~ X*/
10994 if (TREE_CODE (arg1) == BIT_IOR_EXPR
10995 && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
10997 tree t2 = TREE_OPERAND (arg1, 0);
10998 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
11000 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
11001 fold_convert (type, t1));
11005 /* Convert ~X ^ ~Y to X ^ Y. */
11006 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11007 && TREE_CODE (arg1) == BIT_NOT_EXPR)
11008 return fold_build2 (code, type,
11009 fold_convert (type, TREE_OPERAND (arg0, 0)),
11010 fold_convert (type, TREE_OPERAND (arg1, 0)));
11012 /* Convert ~X ^ C to X ^ ~C. */
11013 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11014 && TREE_CODE (arg1) == INTEGER_CST)
11015 return fold_build2 (code, type,
11016 fold_convert (type, TREE_OPERAND (arg0, 0)),
11017 fold_build1 (BIT_NOT_EXPR, type, arg1));
11019 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
11020 if (TREE_CODE (arg0) == BIT_AND_EXPR
11021 && integer_onep (TREE_OPERAND (arg0, 1))
11022 && integer_onep (arg1))
11023 return fold_build2 (EQ_EXPR, type, arg0,
11024 build_int_cst (TREE_TYPE (arg0), 0));
11026 /* Fold (X & Y) ^ Y as ~X & Y. */
11027 if (TREE_CODE (arg0) == BIT_AND_EXPR
11028 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11030 tem = fold_convert (type, TREE_OPERAND (arg0, 0));
11031 return fold_build2 (BIT_AND_EXPR, type,
11032 fold_build1 (BIT_NOT_EXPR, type, tem),
11033 fold_convert (type, arg1));
11035 /* Fold (X & Y) ^ X as ~Y & X. */
11036 if (TREE_CODE (arg0) == BIT_AND_EXPR
11037 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11038 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11040 tem = fold_convert (type, TREE_OPERAND (arg0, 1));
11041 return fold_build2 (BIT_AND_EXPR, type,
11042 fold_build1 (BIT_NOT_EXPR, type, tem),
11043 fold_convert (type, arg1));
11045 /* Fold X ^ (X & Y) as X & ~Y. */
11046 if (TREE_CODE (arg1) == BIT_AND_EXPR
11047 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11049 tem = fold_convert (type, TREE_OPERAND (arg1, 1));
11050 return fold_build2 (BIT_AND_EXPR, type,
11051 fold_convert (type, arg0),
11052 fold_build1 (BIT_NOT_EXPR, type, tem));
11054 /* Fold X ^ (Y & X) as ~Y & X. */
11055 if (TREE_CODE (arg1) == BIT_AND_EXPR
11056 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11057 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11059 tem = fold_convert (type, TREE_OPERAND (arg1, 0));
11060 return fold_build2 (BIT_AND_EXPR, type,
11061 fold_build1 (BIT_NOT_EXPR, type, tem),
11062 fold_convert (type, arg0));
11065 /* See if this can be simplified into a rotate first. If that
11066 is unsuccessful continue in the association code. */
11070 if (integer_all_onesp (arg1))
11071 return non_lvalue (fold_convert (type, arg0));
11072 if (integer_zerop (arg1))
11073 return omit_one_operand (type, arg1, arg0);
11074 if (operand_equal_p (arg0, arg1, 0))
11075 return non_lvalue (fold_convert (type, arg0));
11077 /* ~X & X is always zero. */
11078 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11079 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11080 return omit_one_operand (type, integer_zero_node, arg1);
11082 /* X & ~X is always zero. */
11083 if (TREE_CODE (arg1) == BIT_NOT_EXPR
11084 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11085 return omit_one_operand (type, integer_zero_node, arg0);
11087 /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2). */
11088 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11089 && TREE_CODE (arg1) == INTEGER_CST
11090 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11092 tree tmp1 = fold_convert (type, arg1);
11093 tree tmp2 = fold_convert (type, TREE_OPERAND (arg0, 0));
11094 tree tmp3 = fold_convert (type, TREE_OPERAND (arg0, 1));
11095 tmp2 = fold_build2 (BIT_AND_EXPR, type, tmp2, tmp1);
11096 tmp3 = fold_build2 (BIT_AND_EXPR, type, tmp3, tmp1);
11097 return fold_convert (type,
11098 fold_build2 (BIT_IOR_EXPR, type, tmp2, tmp3));
11101 /* (X | Y) & Y is (X, Y). */
11102 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11103 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11104 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
11105 /* (X | Y) & X is (Y, X). */
11106 if (TREE_CODE (arg0) == BIT_IOR_EXPR
11107 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11108 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11109 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
11110 /* X & (X | Y) is (Y, X). */
11111 if (TREE_CODE (arg1) == BIT_IOR_EXPR
11112 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
11113 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
11114 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
11115 /* X & (Y | X) is (Y, X). */
11116 if (TREE_CODE (arg1) == BIT_IOR_EXPR
11117 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11118 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11119 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
11121 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
11122 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11123 && integer_onep (TREE_OPERAND (arg0, 1))
11124 && integer_onep (arg1))
11126 tem = TREE_OPERAND (arg0, 0);
11127 return fold_build2 (EQ_EXPR, type,
11128 fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
11129 build_int_cst (TREE_TYPE (tem), 1)),
11130 build_int_cst (TREE_TYPE (tem), 0));
11132 /* Fold ~X & 1 as (X & 1) == 0. */
11133 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11134 && integer_onep (arg1))
11136 tem = TREE_OPERAND (arg0, 0);
11137 return fold_build2 (EQ_EXPR, type,
11138 fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
11139 build_int_cst (TREE_TYPE (tem), 1)),
11140 build_int_cst (TREE_TYPE (tem), 0));
11143 /* Fold (X ^ Y) & Y as ~X & Y. */
11144 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11145 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11147 tem = fold_convert (type, TREE_OPERAND (arg0, 0));
11148 return fold_build2 (BIT_AND_EXPR, type,
11149 fold_build1 (BIT_NOT_EXPR, type, tem),
11150 fold_convert (type, arg1));
11152 /* Fold (X ^ Y) & X as ~Y & X. */
11153 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11154 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11155 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
11157 tem = fold_convert (type, TREE_OPERAND (arg0, 1));
11158 return fold_build2 (BIT_AND_EXPR, type,
11159 fold_build1 (BIT_NOT_EXPR, type, tem),
11160 fold_convert (type, arg1));
11162 /* Fold X & (X ^ Y) as X & ~Y. */
11163 if (TREE_CODE (arg1) == BIT_XOR_EXPR
11164 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11166 tem = fold_convert (type, TREE_OPERAND (arg1, 1));
11167 return fold_build2 (BIT_AND_EXPR, type,
11168 fold_convert (type, arg0),
11169 fold_build1 (BIT_NOT_EXPR, type, tem));
11171 /* Fold X & (Y ^ X) as ~Y & X. */
11172 if (TREE_CODE (arg1) == BIT_XOR_EXPR
11173 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
11174 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
11176 tem = fold_convert (type, TREE_OPERAND (arg1, 0));
11177 return fold_build2 (BIT_AND_EXPR, type,
11178 fold_build1 (BIT_NOT_EXPR, type, tem),
11179 fold_convert (type, arg0));
11182 t1 = distribute_bit_expr (code, type, arg0, arg1);
11183 if (t1 != NULL_TREE)
11185 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
11186 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
11187 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
11190 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
11192 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
11193 && (~TREE_INT_CST_LOW (arg1)
11194 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
11195 return fold_convert (type, TREE_OPERAND (arg0, 0));
11198 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
11200 This results in more efficient code for machines without a NOR
11201 instruction. Combine will canonicalize to the first form
11202 which will allow use of NOR instructions provided by the
11203 backend if they exist. */
11204 if (TREE_CODE (arg0) == BIT_NOT_EXPR
11205 && TREE_CODE (arg1) == BIT_NOT_EXPR)
11207 return fold_build1 (BIT_NOT_EXPR, type,
11208 build2 (BIT_IOR_EXPR, type,
11209 fold_convert (type,
11210 TREE_OPERAND (arg0, 0)),
11211 fold_convert (type,
11212 TREE_OPERAND (arg1, 0))));
11215 /* If arg0 is derived from the address of an object or function, we may
11216 be able to fold this expression using the object or function's
11218 if (POINTER_TYPE_P (TREE_TYPE (arg0)) && host_integerp (arg1, 1))
11220 unsigned HOST_WIDE_INT modulus, residue;
11221 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg1);
11223 modulus = get_pointer_modulus_and_residue (arg0, &residue,
11224 integer_onep (arg1));
11226 /* This works because modulus is a power of 2. If this weren't the
11227 case, we'd have to replace it by its greatest power-of-2
11228 divisor: modulus & -modulus. */
11230 return build_int_cst (type, residue & low);
11233 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
11234 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
11235 if the new mask might be further optimized. */
11236 if ((TREE_CODE (arg0) == LSHIFT_EXPR
11237 || TREE_CODE (arg0) == RSHIFT_EXPR)
11238 && host_integerp (TREE_OPERAND (arg0, 1), 1)
11239 && host_integerp (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)))
11240 && tree_low_cst (TREE_OPERAND (arg0, 1), 1)
11241 < TYPE_PRECISION (TREE_TYPE (arg0))
11242 && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT
11243 && tree_low_cst (TREE_OPERAND (arg0, 1), 1) > 0)
11245 unsigned int shiftc = tree_low_cst (TREE_OPERAND (arg0, 1), 1);
11246 unsigned HOST_WIDE_INT mask
11247 = tree_low_cst (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)));
11248 unsigned HOST_WIDE_INT newmask, zerobits = 0;
11249 tree shift_type = TREE_TYPE (arg0);
11251 if (TREE_CODE (arg0) == LSHIFT_EXPR)
11252 zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
11253 else if (TREE_CODE (arg0) == RSHIFT_EXPR
11254 && TYPE_PRECISION (TREE_TYPE (arg0))
11255 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg0))))
11257 unsigned int prec = TYPE_PRECISION (TREE_TYPE (arg0));
11258 tree arg00 = TREE_OPERAND (arg0, 0);
11259 /* See if more bits can be proven as zero because of
11261 if (TREE_CODE (arg00) == NOP_EXPR
11262 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0))))
11264 tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0));
11265 if (TYPE_PRECISION (inner_type)
11266 == GET_MODE_BITSIZE (TYPE_MODE (inner_type))
11267 && TYPE_PRECISION (inner_type) < prec)
11269 prec = TYPE_PRECISION (inner_type);
11270 /* See if we can shorten the right shift. */
11272 shift_type = inner_type;
11275 zerobits = ~(unsigned HOST_WIDE_INT) 0;
11276 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
11277 zerobits <<= prec - shiftc;
11278 /* For arithmetic shift if sign bit could be set, zerobits
11279 can contain actually sign bits, so no transformation is
11280 possible, unless MASK masks them all away. In that
11281 case the shift needs to be converted into logical shift. */
11282 if (!TYPE_UNSIGNED (TREE_TYPE (arg0))
11283 && prec == TYPE_PRECISION (TREE_TYPE (arg0)))
11285 if ((mask & zerobits) == 0)
11286 shift_type = unsigned_type_for (TREE_TYPE (arg0));
11292 /* ((X << 16) & 0xff00) is (X, 0). */
11293 if ((mask & zerobits) == mask)
11294 return omit_one_operand (type, build_int_cst (type, 0), arg0);
11296 newmask = mask | zerobits;
11297 if (newmask != mask && (newmask & (newmask + 1)) == 0)
11301 /* Only do the transformation if NEWMASK is some integer
11303 for (prec = BITS_PER_UNIT;
11304 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
11305 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
11307 if (prec < HOST_BITS_PER_WIDE_INT
11308 || newmask == ~(unsigned HOST_WIDE_INT) 0)
11310 if (shift_type != TREE_TYPE (arg0))
11312 tem = fold_build2 (TREE_CODE (arg0), shift_type,
11313 fold_convert (shift_type,
11314 TREE_OPERAND (arg0, 0)),
11315 TREE_OPERAND (arg0, 1));
11316 tem = fold_convert (type, tem);
11320 return fold_build2 (BIT_AND_EXPR, type, tem,
11321 build_int_cst_type (TREE_TYPE (op1),
11330 /* Don't touch a floating-point divide by zero unless the mode
11331 of the constant can represent infinity. */
11332 if (TREE_CODE (arg1) == REAL_CST
11333 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
11334 && real_zerop (arg1))
11337 /* Optimize A / A to 1.0 if we don't care about
11338 NaNs or Infinities. Skip the transformation
11339 for non-real operands. */
11340 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
11341 && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
11342 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
11343 && operand_equal_p (arg0, arg1, 0))
11345 tree r = build_real (TREE_TYPE (arg0), dconst1);
11347 return omit_two_operands (type, r, arg0, arg1);
11350 /* The complex version of the above A / A optimization. */
11351 if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
11352 && operand_equal_p (arg0, arg1, 0))
11354 tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
11355 if (! HONOR_NANS (TYPE_MODE (elem_type))
11356 && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
11358 tree r = build_real (elem_type, dconst1);
11359 /* omit_two_operands will call fold_convert for us. */
11360 return omit_two_operands (type, r, arg0, arg1);
11364 /* (-A) / (-B) -> A / B */
11365 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11366 return fold_build2 (RDIV_EXPR, type,
11367 TREE_OPERAND (arg0, 0),
11368 negate_expr (arg1));
11369 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11370 return fold_build2 (RDIV_EXPR, type,
11371 negate_expr (arg0),
11372 TREE_OPERAND (arg1, 0));
11374 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
11375 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11376 && real_onep (arg1))
11377 return non_lvalue (fold_convert (type, arg0));
11379 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
11380 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11381 && real_minus_onep (arg1))
11382 return non_lvalue (fold_convert (type, negate_expr (arg0)));
11384 /* If ARG1 is a constant, we can convert this to a multiply by the
11385 reciprocal. This does not have the same rounding properties,
11386 so only do this if -freciprocal-math. We can actually
11387 always safely do it if ARG1 is a power of two, but it's hard to
11388 tell if it is or not in a portable manner. */
11389 if (TREE_CODE (arg1) == REAL_CST)
11391 if (flag_reciprocal_math
11392 && 0 != (tem = const_binop (code, build_real (type, dconst1),
11394 return fold_build2 (MULT_EXPR, type, arg0, tem);
11395 /* Find the reciprocal if optimizing and the result is exact. */
11399 r = TREE_REAL_CST (arg1);
11400 if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
11402 tem = build_real (type, r);
11403 return fold_build2 (MULT_EXPR, type,
11404 fold_convert (type, arg0), tem);
11408 /* Convert A/B/C to A/(B*C). */
11409 if (flag_reciprocal_math
11410 && TREE_CODE (arg0) == RDIV_EXPR)
11411 return fold_build2 (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
11412 fold_build2 (MULT_EXPR, type,
11413 TREE_OPERAND (arg0, 1), arg1));
11415 /* Convert A/(B/C) to (A/B)*C. */
11416 if (flag_reciprocal_math
11417 && TREE_CODE (arg1) == RDIV_EXPR)
11418 return fold_build2 (MULT_EXPR, type,
11419 fold_build2 (RDIV_EXPR, type, arg0,
11420 TREE_OPERAND (arg1, 0)),
11421 TREE_OPERAND (arg1, 1));
11423 /* Convert C1/(X*C2) into (C1/C2)/X. */
11424 if (flag_reciprocal_math
11425 && TREE_CODE (arg1) == MULT_EXPR
11426 && TREE_CODE (arg0) == REAL_CST
11427 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
11429 tree tem = const_binop (RDIV_EXPR, arg0,
11430 TREE_OPERAND (arg1, 1), 0);
11432 return fold_build2 (RDIV_EXPR, type, tem,
11433 TREE_OPERAND (arg1, 0));
11436 if (flag_unsafe_math_optimizations)
11438 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11439 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11441 /* Optimize sin(x)/cos(x) as tan(x). */
11442 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
11443 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
11444 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
11445 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11446 CALL_EXPR_ARG (arg1, 0), 0))
11448 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11450 if (tanfn != NULL_TREE)
11451 return build_call_expr (tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11454 /* Optimize cos(x)/sin(x) as 1.0/tan(x). */
11455 if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11456 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11457 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
11458 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11459 CALL_EXPR_ARG (arg1, 0), 0))
11461 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11463 if (tanfn != NULL_TREE)
11465 tree tmp = build_call_expr (tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11466 return fold_build2 (RDIV_EXPR, type,
11467 build_real (type, dconst1), tmp);
11471 /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11472 NaNs or Infinities. */
11473 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11474 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11475 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11477 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11478 tree arg01 = CALL_EXPR_ARG (arg1, 0);
11480 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11481 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11482 && operand_equal_p (arg00, arg01, 0))
11484 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11486 if (cosfn != NULL_TREE)
11487 return build_call_expr (cosfn, 1, arg00);
11491 /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11492 NaNs or Infinities. */
11493 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11494 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11495 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11497 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11498 tree arg01 = CALL_EXPR_ARG (arg1, 0);
11500 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11501 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11502 && operand_equal_p (arg00, arg01, 0))
11504 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11506 if (cosfn != NULL_TREE)
11508 tree tmp = build_call_expr (cosfn, 1, arg00);
11509 return fold_build2 (RDIV_EXPR, type,
11510 build_real (type, dconst1),
11516 /* Optimize pow(x,c)/x as pow(x,c-1). */
11517 if (fcode0 == BUILT_IN_POW
11518 || fcode0 == BUILT_IN_POWF
11519 || fcode0 == BUILT_IN_POWL)
11521 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11522 tree arg01 = CALL_EXPR_ARG (arg0, 1);
11523 if (TREE_CODE (arg01) == REAL_CST
11524 && !TREE_OVERFLOW (arg01)
11525 && operand_equal_p (arg1, arg00, 0))
11527 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11531 c = TREE_REAL_CST (arg01);
11532 real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
11533 arg = build_real (type, c);
11534 return build_call_expr (powfn, 2, arg1, arg);
11538 /* Optimize a/root(b/c) into a*root(c/b). */
11539 if (BUILTIN_ROOT_P (fcode1))
11541 tree rootarg = CALL_EXPR_ARG (arg1, 0);
11543 if (TREE_CODE (rootarg) == RDIV_EXPR)
11545 tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11546 tree b = TREE_OPERAND (rootarg, 0);
11547 tree c = TREE_OPERAND (rootarg, 1);
11549 tree tmp = fold_build2 (RDIV_EXPR, type, c, b);
11551 tmp = build_call_expr (rootfn, 1, tmp);
11552 return fold_build2 (MULT_EXPR, type, arg0, tmp);
11556 /* Optimize x/expN(y) into x*expN(-y). */
11557 if (BUILTIN_EXPONENT_P (fcode1))
11559 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11560 tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
11561 arg1 = build_call_expr (expfn, 1, fold_convert (type, arg));
11562 return fold_build2 (MULT_EXPR, type, arg0, arg1);
11565 /* Optimize x/pow(y,z) into x*pow(y,-z). */
11566 if (fcode1 == BUILT_IN_POW
11567 || fcode1 == BUILT_IN_POWF
11568 || fcode1 == BUILT_IN_POWL)
11570 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11571 tree arg10 = CALL_EXPR_ARG (arg1, 0);
11572 tree arg11 = CALL_EXPR_ARG (arg1, 1);
11573 tree neg11 = fold_convert (type, negate_expr (arg11));
11574 arg1 = build_call_expr (powfn, 2, arg10, neg11);
11575 return fold_build2 (MULT_EXPR, type, arg0, arg1);
11580 case TRUNC_DIV_EXPR:
11581 case FLOOR_DIV_EXPR:
11582 /* Simplify A / (B << N) where A and B are positive and B is
11583 a power of 2, to A >> (N + log2(B)). */
11584 strict_overflow_p = false;
11585 if (TREE_CODE (arg1) == LSHIFT_EXPR
11586 && (TYPE_UNSIGNED (type)
11587 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11589 tree sval = TREE_OPERAND (arg1, 0);
11590 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11592 tree sh_cnt = TREE_OPERAND (arg1, 1);
11593 unsigned long pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
11595 if (strict_overflow_p)
11596 fold_overflow_warning (("assuming signed overflow does not "
11597 "occur when simplifying A / (B << N)"),
11598 WARN_STRICT_OVERFLOW_MISC);
11600 sh_cnt = fold_build2 (PLUS_EXPR, TREE_TYPE (sh_cnt),
11601 sh_cnt, build_int_cst (NULL_TREE, pow2));
11602 return fold_build2 (RSHIFT_EXPR, type,
11603 fold_convert (type, arg0), sh_cnt);
11607 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
11608 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
11609 if (INTEGRAL_TYPE_P (type)
11610 && TYPE_UNSIGNED (type)
11611 && code == FLOOR_DIV_EXPR)
11612 return fold_build2 (TRUNC_DIV_EXPR, type, op0, op1);
11616 case ROUND_DIV_EXPR:
11617 case CEIL_DIV_EXPR:
11618 case EXACT_DIV_EXPR:
11619 if (integer_onep (arg1))
11620 return non_lvalue (fold_convert (type, arg0));
11621 if (integer_zerop (arg1))
11623 /* X / -1 is -X. */
11624 if (!TYPE_UNSIGNED (type)
11625 && TREE_CODE (arg1) == INTEGER_CST
11626 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11627 && TREE_INT_CST_HIGH (arg1) == -1)
11628 return fold_convert (type, negate_expr (arg0));
11630 /* Convert -A / -B to A / B when the type is signed and overflow is
11632 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11633 && TREE_CODE (arg0) == NEGATE_EXPR
11634 && negate_expr_p (arg1))
11636 if (INTEGRAL_TYPE_P (type))
11637 fold_overflow_warning (("assuming signed overflow does not occur "
11638 "when distributing negation across "
11640 WARN_STRICT_OVERFLOW_MISC);
11641 return fold_build2 (code, type,
11642 fold_convert (type, TREE_OPERAND (arg0, 0)),
11643 fold_convert (type, negate_expr (arg1)));
11645 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11646 && TREE_CODE (arg1) == NEGATE_EXPR
11647 && negate_expr_p (arg0))
11649 if (INTEGRAL_TYPE_P (type))
11650 fold_overflow_warning (("assuming signed overflow does not occur "
11651 "when distributing negation across "
11653 WARN_STRICT_OVERFLOW_MISC);
11654 return fold_build2 (code, type,
11655 fold_convert (type, negate_expr (arg0)),
11656 fold_convert (type, TREE_OPERAND (arg1, 0)));
11659 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11660 operation, EXACT_DIV_EXPR.
11662 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11663 At one time others generated faster code, it's not clear if they do
11664 after the last round to changes to the DIV code in expmed.c. */
11665 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11666 && multiple_of_p (type, arg0, arg1))
11667 return fold_build2 (EXACT_DIV_EXPR, type, arg0, arg1);
11669 strict_overflow_p = false;
11670 if (TREE_CODE (arg1) == INTEGER_CST
11671 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11672 &strict_overflow_p)))
11674 if (strict_overflow_p)
11675 fold_overflow_warning (("assuming signed overflow does not occur "
11676 "when simplifying division"),
11677 WARN_STRICT_OVERFLOW_MISC);
11678 return fold_convert (type, tem);
11683 case CEIL_MOD_EXPR:
11684 case FLOOR_MOD_EXPR:
11685 case ROUND_MOD_EXPR:
11686 case TRUNC_MOD_EXPR:
11687 /* X % 1 is always zero, but be sure to preserve any side
11689 if (integer_onep (arg1))
11690 return omit_one_operand (type, integer_zero_node, arg0);
11692 /* X % 0, return X % 0 unchanged so that we can get the
11693 proper warnings and errors. */
11694 if (integer_zerop (arg1))
11697 /* 0 % X is always zero, but be sure to preserve any side
11698 effects in X. Place this after checking for X == 0. */
11699 if (integer_zerop (arg0))
11700 return omit_one_operand (type, integer_zero_node, arg1);
11702 /* X % -1 is zero. */
11703 if (!TYPE_UNSIGNED (type)
11704 && TREE_CODE (arg1) == INTEGER_CST
11705 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11706 && TREE_INT_CST_HIGH (arg1) == -1)
11707 return omit_one_operand (type, integer_zero_node, arg0);
11709 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
11710 i.e. "X % C" into "X & (C - 1)", if X and C are positive. */
11711 strict_overflow_p = false;
11712 if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
11713 && (TYPE_UNSIGNED (type)
11714 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11717 /* Also optimize A % (C << N) where C is a power of 2,
11718 to A & ((C << N) - 1). */
11719 if (TREE_CODE (arg1) == LSHIFT_EXPR)
11720 c = TREE_OPERAND (arg1, 0);
11722 if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
11724 tree mask = fold_build2 (MINUS_EXPR, TREE_TYPE (arg1), arg1,
11725 build_int_cst (TREE_TYPE (arg1), 1));
11726 if (strict_overflow_p)
11727 fold_overflow_warning (("assuming signed overflow does not "
11728 "occur when simplifying "
11729 "X % (power of two)"),
11730 WARN_STRICT_OVERFLOW_MISC);
11731 return fold_build2 (BIT_AND_EXPR, type,
11732 fold_convert (type, arg0),
11733 fold_convert (type, mask));
11737 /* X % -C is the same as X % C. */
11738 if (code == TRUNC_MOD_EXPR
11739 && !TYPE_UNSIGNED (type)
11740 && TREE_CODE (arg1) == INTEGER_CST
11741 && !TREE_OVERFLOW (arg1)
11742 && TREE_INT_CST_HIGH (arg1) < 0
11743 && !TYPE_OVERFLOW_TRAPS (type)
11744 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
11745 && !sign_bit_p (arg1, arg1))
11746 return fold_build2 (code, type, fold_convert (type, arg0),
11747 fold_convert (type, negate_expr (arg1)));
11749 /* X % -Y is the same as X % Y. */
11750 if (code == TRUNC_MOD_EXPR
11751 && !TYPE_UNSIGNED (type)
11752 && TREE_CODE (arg1) == NEGATE_EXPR
11753 && !TYPE_OVERFLOW_TRAPS (type))
11754 return fold_build2 (code, type, fold_convert (type, arg0),
11755 fold_convert (type, TREE_OPERAND (arg1, 0)));
11757 if (TREE_CODE (arg1) == INTEGER_CST
11758 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11759 &strict_overflow_p)))
11761 if (strict_overflow_p)
11762 fold_overflow_warning (("assuming signed overflow does not occur "
11763 "when simplifying modulus"),
11764 WARN_STRICT_OVERFLOW_MISC);
11765 return fold_convert (type, tem);
11772 if (integer_all_onesp (arg0))
11773 return omit_one_operand (type, arg0, arg1);
11777 /* Optimize -1 >> x for arithmetic right shifts. */
11778 if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type)
11779 && tree_expr_nonnegative_p (arg1))
11780 return omit_one_operand (type, arg0, arg1);
11781 /* ... fall through ... */
11785 if (integer_zerop (arg1))
11786 return non_lvalue (fold_convert (type, arg0));
11787 if (integer_zerop (arg0))
11788 return omit_one_operand (type, arg0, arg1);
11790 /* Since negative shift count is not well-defined,
11791 don't try to compute it in the compiler. */
11792 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11795 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
11796 if (TREE_CODE (op0) == code && host_integerp (arg1, false)
11797 && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11798 && host_integerp (TREE_OPERAND (arg0, 1), false)
11799 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11801 HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
11802 + TREE_INT_CST_LOW (arg1));
11804 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
11805 being well defined. */
11806 if (low >= TYPE_PRECISION (type))
11808 if (code == LROTATE_EXPR || code == RROTATE_EXPR)
11809 low = low % TYPE_PRECISION (type);
11810 else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
11811 return build_int_cst (type, 0);
11813 low = TYPE_PRECISION (type) - 1;
11816 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11817 build_int_cst (type, low));
11820 /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
11821 into x & ((unsigned)-1 >> c) for unsigned types. */
11822 if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
11823 || (TYPE_UNSIGNED (type)
11824 && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
11825 && host_integerp (arg1, false)
11826 && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11827 && host_integerp (TREE_OPERAND (arg0, 1), false)
11828 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11830 HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
11831 HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
11837 arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
11839 lshift = build_int_cst (type, -1);
11840 lshift = int_const_binop (code, lshift, arg1, 0);
11842 return fold_build2 (BIT_AND_EXPR, type, arg00, lshift);
11846 /* Rewrite an LROTATE_EXPR by a constant into an
11847 RROTATE_EXPR by a new constant. */
11848 if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
11850 tree tem = build_int_cst (TREE_TYPE (arg1),
11851 TYPE_PRECISION (type));
11852 tem = const_binop (MINUS_EXPR, tem, arg1, 0);
11853 return fold_build2 (RROTATE_EXPR, type, op0, tem);
11856 /* If we have a rotate of a bit operation with the rotate count and
11857 the second operand of the bit operation both constant,
11858 permute the two operations. */
11859 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11860 && (TREE_CODE (arg0) == BIT_AND_EXPR
11861 || TREE_CODE (arg0) == BIT_IOR_EXPR
11862 || TREE_CODE (arg0) == BIT_XOR_EXPR)
11863 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11864 return fold_build2 (TREE_CODE (arg0), type,
11865 fold_build2 (code, type,
11866 TREE_OPERAND (arg0, 0), arg1),
11867 fold_build2 (code, type,
11868 TREE_OPERAND (arg0, 1), arg1));
11870 /* Two consecutive rotates adding up to the precision of the
11871 type can be ignored. */
11872 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11873 && TREE_CODE (arg0) == RROTATE_EXPR
11874 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11875 && TREE_INT_CST_HIGH (arg1) == 0
11876 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
11877 && ((TREE_INT_CST_LOW (arg1)
11878 + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
11879 == (unsigned int) TYPE_PRECISION (type)))
11880 return TREE_OPERAND (arg0, 0);
11882 /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
11883 (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
11884 if the latter can be further optimized. */
11885 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
11886 && TREE_CODE (arg0) == BIT_AND_EXPR
11887 && TREE_CODE (arg1) == INTEGER_CST
11888 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11890 tree mask = fold_build2 (code, type,
11891 fold_convert (type, TREE_OPERAND (arg0, 1)),
11893 tree shift = fold_build2 (code, type,
11894 fold_convert (type, TREE_OPERAND (arg0, 0)),
11896 tem = fold_binary (BIT_AND_EXPR, type, shift, mask);
11904 if (operand_equal_p (arg0, arg1, 0))
11905 return omit_one_operand (type, arg0, arg1);
11906 if (INTEGRAL_TYPE_P (type)
11907 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
11908 return omit_one_operand (type, arg1, arg0);
11909 tem = fold_minmax (MIN_EXPR, type, arg0, arg1);
11915 if (operand_equal_p (arg0, arg1, 0))
11916 return omit_one_operand (type, arg0, arg1);
11917 if (INTEGRAL_TYPE_P (type)
11918 && TYPE_MAX_VALUE (type)
11919 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
11920 return omit_one_operand (type, arg1, arg0);
11921 tem = fold_minmax (MAX_EXPR, type, arg0, arg1);
11926 case TRUTH_ANDIF_EXPR:
11927 /* Note that the operands of this must be ints
11928 and their values must be 0 or 1.
11929 ("true" is a fixed value perhaps depending on the language.) */
11930 /* If first arg is constant zero, return it. */
11931 if (integer_zerop (arg0))
11932 return fold_convert (type, arg0);
11933 case TRUTH_AND_EXPR:
11934 /* If either arg is constant true, drop it. */
11935 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11936 return non_lvalue (fold_convert (type, arg1));
11937 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
11938 /* Preserve sequence points. */
11939 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11940 return non_lvalue (fold_convert (type, arg0));
11941 /* If second arg is constant zero, result is zero, but first arg
11942 must be evaluated. */
11943 if (integer_zerop (arg1))
11944 return omit_one_operand (type, arg1, arg0);
11945 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
11946 case will be handled here. */
11947 if (integer_zerop (arg0))
11948 return omit_one_operand (type, arg0, arg1);
11950 /* !X && X is always false. */
11951 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11952 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11953 return omit_one_operand (type, integer_zero_node, arg1);
11954 /* X && !X is always false. */
11955 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11956 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11957 return omit_one_operand (type, integer_zero_node, arg0);
11959 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
11960 means A >= Y && A != MAX, but in this case we know that
11963 if (!TREE_SIDE_EFFECTS (arg0)
11964 && !TREE_SIDE_EFFECTS (arg1))
11966 tem = fold_to_nonsharp_ineq_using_bound (arg0, arg1);
11967 if (tem && !operand_equal_p (tem, arg0, 0))
11968 return fold_build2 (code, type, tem, arg1);
11970 tem = fold_to_nonsharp_ineq_using_bound (arg1, arg0);
11971 if (tem && !operand_equal_p (tem, arg1, 0))
11972 return fold_build2 (code, type, arg0, tem);
11976 /* We only do these simplifications if we are optimizing. */
11980 /* Check for things like (A || B) && (A || C). We can convert this
11981 to A || (B && C). Note that either operator can be any of the four
11982 truth and/or operations and the transformation will still be
11983 valid. Also note that we only care about order for the
11984 ANDIF and ORIF operators. If B contains side effects, this
11985 might change the truth-value of A. */
11986 if (TREE_CODE (arg0) == TREE_CODE (arg1)
11987 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
11988 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
11989 || TREE_CODE (arg0) == TRUTH_AND_EXPR
11990 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
11991 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
11993 tree a00 = TREE_OPERAND (arg0, 0);
11994 tree a01 = TREE_OPERAND (arg0, 1);
11995 tree a10 = TREE_OPERAND (arg1, 0);
11996 tree a11 = TREE_OPERAND (arg1, 1);
11997 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
11998 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
11999 && (code == TRUTH_AND_EXPR
12000 || code == TRUTH_OR_EXPR));
12002 if (operand_equal_p (a00, a10, 0))
12003 return fold_build2 (TREE_CODE (arg0), type, a00,
12004 fold_build2 (code, type, a01, a11));
12005 else if (commutative && operand_equal_p (a00, a11, 0))
12006 return fold_build2 (TREE_CODE (arg0), type, a00,
12007 fold_build2 (code, type, a01, a10));
12008 else if (commutative && operand_equal_p (a01, a10, 0))
12009 return fold_build2 (TREE_CODE (arg0), type, a01,
12010 fold_build2 (code, type, a00, a11));
12012 /* This case if tricky because we must either have commutative
12013 operators or else A10 must not have side-effects. */
12015 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
12016 && operand_equal_p (a01, a11, 0))
12017 return fold_build2 (TREE_CODE (arg0), type,
12018 fold_build2 (code, type, a00, a10),
12022 /* See if we can build a range comparison. */
12023 if (0 != (tem = fold_range_test (code, type, op0, op1)))
12026 /* Check for the possibility of merging component references. If our
12027 lhs is another similar operation, try to merge its rhs with our
12028 rhs. Then try to merge our lhs and rhs. */
12029 if (TREE_CODE (arg0) == code
12030 && 0 != (tem = fold_truthop (code, type,
12031 TREE_OPERAND (arg0, 1), arg1)))
12032 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
12034 if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
12039 case TRUTH_ORIF_EXPR:
12040 /* Note that the operands of this must be ints
12041 and their values must be 0 or true.
12042 ("true" is a fixed value perhaps depending on the language.) */
12043 /* If first arg is constant true, return it. */
12044 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12045 return fold_convert (type, arg0);
12046 case TRUTH_OR_EXPR:
12047 /* If either arg is constant zero, drop it. */
12048 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
12049 return non_lvalue (fold_convert (type, arg1));
12050 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
12051 /* Preserve sequence points. */
12052 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
12053 return non_lvalue (fold_convert (type, arg0));
12054 /* If second arg is constant true, result is true, but we must
12055 evaluate first arg. */
12056 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
12057 return omit_one_operand (type, arg1, arg0);
12058 /* Likewise for first arg, but note this only occurs here for
12060 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
12061 return omit_one_operand (type, arg0, arg1);
12063 /* !X || X is always true. */
12064 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12065 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12066 return omit_one_operand (type, integer_one_node, arg1);
12067 /* X || !X is always true. */
12068 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12069 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12070 return omit_one_operand (type, integer_one_node, arg0);
12074 case TRUTH_XOR_EXPR:
12075 /* If the second arg is constant zero, drop it. */
12076 if (integer_zerop (arg1))
12077 return non_lvalue (fold_convert (type, arg0));
12078 /* If the second arg is constant true, this is a logical inversion. */
12079 if (integer_onep (arg1))
12081 /* Only call invert_truthvalue if operand is a truth value. */
12082 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
12083 tem = fold_build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
12085 tem = invert_truthvalue (arg0);
12086 return non_lvalue (fold_convert (type, tem));
12088 /* Identical arguments cancel to zero. */
12089 if (operand_equal_p (arg0, arg1, 0))
12090 return omit_one_operand (type, integer_zero_node, arg0);
12092 /* !X ^ X is always true. */
12093 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
12094 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
12095 return omit_one_operand (type, integer_one_node, arg1);
12097 /* X ^ !X is always true. */
12098 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
12099 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
12100 return omit_one_operand (type, integer_one_node, arg0);
12106 tem = fold_comparison (code, type, op0, op1);
12107 if (tem != NULL_TREE)
12110 /* bool_var != 0 becomes bool_var. */
12111 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12112 && code == NE_EXPR)
12113 return non_lvalue (fold_convert (type, arg0));
12115 /* bool_var == 1 becomes bool_var. */
12116 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12117 && code == EQ_EXPR)
12118 return non_lvalue (fold_convert (type, arg0));
12120 /* bool_var != 1 becomes !bool_var. */
12121 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
12122 && code == NE_EXPR)
12123 return fold_build1 (TRUTH_NOT_EXPR, type, fold_convert (type, arg0));
12125 /* bool_var == 0 becomes !bool_var. */
12126 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
12127 && code == EQ_EXPR)
12128 return fold_build1 (TRUTH_NOT_EXPR, type, fold_convert (type, arg0));
12130 /* If this is an equality comparison of the address of two non-weak,
12131 unaliased symbols neither of which are extern (since we do not
12132 have access to attributes for externs), then we know the result. */
12133 if (TREE_CODE (arg0) == ADDR_EXPR
12134 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
12135 && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
12136 && ! lookup_attribute ("alias",
12137 DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
12138 && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
12139 && TREE_CODE (arg1) == ADDR_EXPR
12140 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
12141 && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
12142 && ! lookup_attribute ("alias",
12143 DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
12144 && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
12146 /* We know that we're looking at the address of two
12147 non-weak, unaliased, static _DECL nodes.
12149 It is both wasteful and incorrect to call operand_equal_p
12150 to compare the two ADDR_EXPR nodes. It is wasteful in that
12151 all we need to do is test pointer equality for the arguments
12152 to the two ADDR_EXPR nodes. It is incorrect to use
12153 operand_equal_p as that function is NOT equivalent to a
12154 C equality test. It can in fact return false for two
12155 objects which would test as equal using the C equality
12157 bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
12158 return constant_boolean_node (equal
12159 ? code == EQ_EXPR : code != EQ_EXPR,
12163 /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
12164 a MINUS_EXPR of a constant, we can convert it into a comparison with
12165 a revised constant as long as no overflow occurs. */
12166 if (TREE_CODE (arg1) == INTEGER_CST
12167 && (TREE_CODE (arg0) == PLUS_EXPR
12168 || TREE_CODE (arg0) == MINUS_EXPR)
12169 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12170 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
12171 ? MINUS_EXPR : PLUS_EXPR,
12172 fold_convert (TREE_TYPE (arg0), arg1),
12173 TREE_OPERAND (arg0, 1), 0))
12174 && !TREE_OVERFLOW (tem))
12175 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
12177 /* Similarly for a NEGATE_EXPR. */
12178 if (TREE_CODE (arg0) == NEGATE_EXPR
12179 && TREE_CODE (arg1) == INTEGER_CST
12180 && 0 != (tem = negate_expr (arg1))
12181 && TREE_CODE (tem) == INTEGER_CST
12182 && !TREE_OVERFLOW (tem))
12183 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
12185 /* Similarly for a BIT_XOR_EXPR; X ^ C1 == C2 is X == (C1 ^ C2). */
12186 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12187 && TREE_CODE (arg1) == INTEGER_CST
12188 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12189 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12190 fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg0),
12191 fold_convert (TREE_TYPE (arg0), arg1),
12192 TREE_OPERAND (arg0, 1)));
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)) == INTEGER_CST
12198 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
12199 || POINTER_TYPE_P (TREE_TYPE (arg0))))
12201 tree cst = TREE_OPERAND (arg0, 1);
12203 if (code == EQ_EXPR
12204 && !integer_zerop (cst))
12205 return omit_two_operands (type, boolean_false_node,
12206 TREE_OPERAND (arg0, 0), arg1);
12208 return omit_two_operands (type, boolean_true_node,
12209 TREE_OPERAND (arg0, 0), arg1);
12212 /* If we have X - Y == 0, we can convert that to X == Y and similarly
12213 for !=. Don't do this for ordered comparisons due to overflow. */
12214 if (TREE_CODE (arg0) == MINUS_EXPR
12215 && integer_zerop (arg1))
12216 return fold_build2 (code, type,
12217 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
12219 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
12220 if (TREE_CODE (arg0) == ABS_EXPR
12221 && (integer_zerop (arg1) || real_zerop (arg1)))
12222 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), arg1);
12224 /* If this is an EQ or NE comparison with zero and ARG0 is
12225 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
12226 two operations, but the latter can be done in one less insn
12227 on machines that have only two-operand insns or on which a
12228 constant cannot be the first operand. */
12229 if (TREE_CODE (arg0) == BIT_AND_EXPR
12230 && integer_zerop (arg1))
12232 tree arg00 = TREE_OPERAND (arg0, 0);
12233 tree arg01 = TREE_OPERAND (arg0, 1);
12234 if (TREE_CODE (arg00) == LSHIFT_EXPR
12235 && integer_onep (TREE_OPERAND (arg00, 0)))
12237 tree tem = fold_build2 (RSHIFT_EXPR, TREE_TYPE (arg00),
12238 arg01, TREE_OPERAND (arg00, 1));
12239 tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12240 build_int_cst (TREE_TYPE (arg0), 1));
12241 return fold_build2 (code, type,
12242 fold_convert (TREE_TYPE (arg1), tem), arg1);
12244 else if (TREE_CODE (arg01) == LSHIFT_EXPR
12245 && integer_onep (TREE_OPERAND (arg01, 0)))
12247 tree tem = fold_build2 (RSHIFT_EXPR, TREE_TYPE (arg01),
12248 arg00, TREE_OPERAND (arg01, 1));
12249 tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0), tem,
12250 build_int_cst (TREE_TYPE (arg0), 1));
12251 return fold_build2 (code, type,
12252 fold_convert (TREE_TYPE (arg1), tem), arg1);
12256 /* If this is an NE or EQ comparison of zero against the result of a
12257 signed MOD operation whose second operand is a power of 2, make
12258 the MOD operation unsigned since it is simpler and equivalent. */
12259 if (integer_zerop (arg1)
12260 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
12261 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
12262 || TREE_CODE (arg0) == CEIL_MOD_EXPR
12263 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
12264 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
12265 && integer_pow2p (TREE_OPERAND (arg0, 1)))
12267 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
12268 tree newmod = fold_build2 (TREE_CODE (arg0), newtype,
12269 fold_convert (newtype,
12270 TREE_OPERAND (arg0, 0)),
12271 fold_convert (newtype,
12272 TREE_OPERAND (arg0, 1)));
12274 return fold_build2 (code, type, newmod,
12275 fold_convert (newtype, arg1));
12278 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
12279 C1 is a valid shift constant, and C2 is a power of two, i.e.
12281 if (TREE_CODE (arg0) == BIT_AND_EXPR
12282 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
12283 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
12285 && integer_pow2p (TREE_OPERAND (arg0, 1))
12286 && integer_zerop (arg1))
12288 tree itype = TREE_TYPE (arg0);
12289 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
12290 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
12292 /* Check for a valid shift count. */
12293 if (TREE_INT_CST_HIGH (arg001) == 0
12294 && TREE_INT_CST_LOW (arg001) < prec)
12296 tree arg01 = TREE_OPERAND (arg0, 1);
12297 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12298 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
12299 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
12300 can be rewritten as (X & (C2 << C1)) != 0. */
12301 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
12303 tem = fold_build2 (LSHIFT_EXPR, itype, arg01, arg001);
12304 tem = fold_build2 (BIT_AND_EXPR, itype, arg000, tem);
12305 return fold_build2 (code, type, tem, arg1);
12307 /* Otherwise, for signed (arithmetic) shifts,
12308 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
12309 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
12310 else if (!TYPE_UNSIGNED (itype))
12311 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
12312 arg000, build_int_cst (itype, 0));
12313 /* Otherwise, of unsigned (logical) shifts,
12314 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
12315 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
12317 return omit_one_operand (type,
12318 code == EQ_EXPR ? integer_one_node
12319 : integer_zero_node,
12324 /* If this is an NE comparison of zero with an AND of one, remove the
12325 comparison since the AND will give the correct value. */
12326 if (code == NE_EXPR
12327 && integer_zerop (arg1)
12328 && TREE_CODE (arg0) == BIT_AND_EXPR
12329 && integer_onep (TREE_OPERAND (arg0, 1)))
12330 return fold_convert (type, arg0);
12332 /* If we have (A & C) == C where C is a power of 2, convert this into
12333 (A & C) != 0. Similarly for NE_EXPR. */
12334 if (TREE_CODE (arg0) == BIT_AND_EXPR
12335 && integer_pow2p (TREE_OPERAND (arg0, 1))
12336 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12337 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12338 arg0, fold_convert (TREE_TYPE (arg0),
12339 integer_zero_node));
12341 /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
12342 bit, then fold the expression into A < 0 or A >= 0. */
12343 tem = fold_single_bit_test_into_sign_test (code, arg0, arg1, type);
12347 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
12348 Similarly for NE_EXPR. */
12349 if (TREE_CODE (arg0) == BIT_AND_EXPR
12350 && TREE_CODE (arg1) == INTEGER_CST
12351 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12353 tree notc = fold_build1 (BIT_NOT_EXPR,
12354 TREE_TYPE (TREE_OPERAND (arg0, 1)),
12355 TREE_OPERAND (arg0, 1));
12356 tree dandnotc = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12358 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12359 if (integer_nonzerop (dandnotc))
12360 return omit_one_operand (type, rslt, arg0);
12363 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
12364 Similarly for NE_EXPR. */
12365 if (TREE_CODE (arg0) == BIT_IOR_EXPR
12366 && TREE_CODE (arg1) == INTEGER_CST
12367 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12369 tree notd = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
12370 tree candnotd = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12371 TREE_OPERAND (arg0, 1), notd);
12372 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12373 if (integer_nonzerop (candnotd))
12374 return omit_one_operand (type, rslt, arg0);
12377 /* If this is a comparison of a field, we may be able to simplify it. */
12378 if ((TREE_CODE (arg0) == COMPONENT_REF
12379 || TREE_CODE (arg0) == BIT_FIELD_REF)
12380 /* Handle the constant case even without -O
12381 to make sure the warnings are given. */
12382 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
12384 t1 = optimize_bit_field_compare (code, type, arg0, arg1);
12389 /* Optimize comparisons of strlen vs zero to a compare of the
12390 first character of the string vs zero. To wit,
12391 strlen(ptr) == 0 => *ptr == 0
12392 strlen(ptr) != 0 => *ptr != 0
12393 Other cases should reduce to one of these two (or a constant)
12394 due to the return value of strlen being unsigned. */
12395 if (TREE_CODE (arg0) == CALL_EXPR
12396 && integer_zerop (arg1))
12398 tree fndecl = get_callee_fndecl (arg0);
12401 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
12402 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
12403 && call_expr_nargs (arg0) == 1
12404 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
12406 tree iref = build_fold_indirect_ref (CALL_EXPR_ARG (arg0, 0));
12407 return fold_build2 (code, type, iref,
12408 build_int_cst (TREE_TYPE (iref), 0));
12412 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12413 of X. Similarly fold (X >> C) == 0 into X >= 0. */
12414 if (TREE_CODE (arg0) == RSHIFT_EXPR
12415 && integer_zerop (arg1)
12416 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12418 tree arg00 = TREE_OPERAND (arg0, 0);
12419 tree arg01 = TREE_OPERAND (arg0, 1);
12420 tree itype = TREE_TYPE (arg00);
12421 if (TREE_INT_CST_HIGH (arg01) == 0
12422 && TREE_INT_CST_LOW (arg01)
12423 == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
12425 if (TYPE_UNSIGNED (itype))
12427 itype = signed_type_for (itype);
12428 arg00 = fold_convert (itype, arg00);
12430 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12431 type, arg00, build_int_cst (itype, 0));
12435 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
12436 if (integer_zerop (arg1)
12437 && TREE_CODE (arg0) == BIT_XOR_EXPR)
12438 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12439 TREE_OPERAND (arg0, 1));
12441 /* (X ^ Y) == Y becomes X == 0. We know that Y has no side-effects. */
12442 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12443 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12444 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12445 build_int_cst (TREE_TYPE (arg1), 0));
12446 /* Likewise (X ^ Y) == X becomes Y == 0. X has no side-effects. */
12447 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12448 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12449 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
12450 return fold_build2 (code, type, TREE_OPERAND (arg0, 1),
12451 build_int_cst (TREE_TYPE (arg1), 0));
12453 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
12454 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12455 && TREE_CODE (arg1) == INTEGER_CST
12456 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12457 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12458 fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg1),
12459 TREE_OPERAND (arg0, 1), arg1));
12461 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12462 (X & C) == 0 when C is a single bit. */
12463 if (TREE_CODE (arg0) == BIT_AND_EXPR
12464 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12465 && integer_zerop (arg1)
12466 && integer_pow2p (TREE_OPERAND (arg0, 1)))
12468 tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12469 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12470 TREE_OPERAND (arg0, 1));
12471 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12475 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12476 constant C is a power of two, i.e. a single bit. */
12477 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12478 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12479 && integer_zerop (arg1)
12480 && integer_pow2p (TREE_OPERAND (arg0, 1))
12481 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12482 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12484 tree arg00 = TREE_OPERAND (arg0, 0);
12485 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12486 arg00, build_int_cst (TREE_TYPE (arg00), 0));
12489 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12490 when is C is a power of two, i.e. a single bit. */
12491 if (TREE_CODE (arg0) == BIT_AND_EXPR
12492 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12493 && integer_zerop (arg1)
12494 && integer_pow2p (TREE_OPERAND (arg0, 1))
12495 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12496 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12498 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12499 tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg000),
12500 arg000, TREE_OPERAND (arg0, 1));
12501 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12502 tem, build_int_cst (TREE_TYPE (tem), 0));
12505 if (integer_zerop (arg1)
12506 && tree_expr_nonzero_p (arg0))
12508 tree res = constant_boolean_node (code==NE_EXPR, type);
12509 return omit_one_operand (type, res, arg0);
12512 /* Fold -X op -Y as X op Y, where op is eq/ne. */
12513 if (TREE_CODE (arg0) == NEGATE_EXPR
12514 && TREE_CODE (arg1) == NEGATE_EXPR)
12515 return fold_build2 (code, type,
12516 TREE_OPERAND (arg0, 0),
12517 TREE_OPERAND (arg1, 0));
12519 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
12520 if (TREE_CODE (arg0) == BIT_AND_EXPR
12521 && TREE_CODE (arg1) == BIT_AND_EXPR)
12523 tree arg00 = TREE_OPERAND (arg0, 0);
12524 tree arg01 = TREE_OPERAND (arg0, 1);
12525 tree arg10 = TREE_OPERAND (arg1, 0);
12526 tree arg11 = TREE_OPERAND (arg1, 1);
12527 tree itype = TREE_TYPE (arg0);
12529 if (operand_equal_p (arg01, arg11, 0))
12530 return fold_build2 (code, type,
12531 fold_build2 (BIT_AND_EXPR, itype,
12532 fold_build2 (BIT_XOR_EXPR, itype,
12535 build_int_cst (itype, 0));
12537 if (operand_equal_p (arg01, arg10, 0))
12538 return fold_build2 (code, type,
12539 fold_build2 (BIT_AND_EXPR, itype,
12540 fold_build2 (BIT_XOR_EXPR, itype,
12543 build_int_cst (itype, 0));
12545 if (operand_equal_p (arg00, arg11, 0))
12546 return fold_build2 (code, type,
12547 fold_build2 (BIT_AND_EXPR, itype,
12548 fold_build2 (BIT_XOR_EXPR, itype,
12551 build_int_cst (itype, 0));
12553 if (operand_equal_p (arg00, arg10, 0))
12554 return fold_build2 (code, type,
12555 fold_build2 (BIT_AND_EXPR, itype,
12556 fold_build2 (BIT_XOR_EXPR, itype,
12559 build_int_cst (itype, 0));
12562 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12563 && TREE_CODE (arg1) == BIT_XOR_EXPR)
12565 tree arg00 = TREE_OPERAND (arg0, 0);
12566 tree arg01 = TREE_OPERAND (arg0, 1);
12567 tree arg10 = TREE_OPERAND (arg1, 0);
12568 tree arg11 = TREE_OPERAND (arg1, 1);
12569 tree itype = TREE_TYPE (arg0);
12571 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12572 operand_equal_p guarantees no side-effects so we don't need
12573 to use omit_one_operand on Z. */
12574 if (operand_equal_p (arg01, arg11, 0))
12575 return fold_build2 (code, type, arg00, arg10);
12576 if (operand_equal_p (arg01, arg10, 0))
12577 return fold_build2 (code, type, arg00, arg11);
12578 if (operand_equal_p (arg00, arg11, 0))
12579 return fold_build2 (code, type, arg01, arg10);
12580 if (operand_equal_p (arg00, arg10, 0))
12581 return fold_build2 (code, type, arg01, arg11);
12583 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12584 if (TREE_CODE (arg01) == INTEGER_CST
12585 && TREE_CODE (arg11) == INTEGER_CST)
12586 return fold_build2 (code, type,
12587 fold_build2 (BIT_XOR_EXPR, itype, arg00,
12588 fold_build2 (BIT_XOR_EXPR, itype,
12593 /* Attempt to simplify equality/inequality comparisons of complex
12594 values. Only lower the comparison if the result is known or
12595 can be simplified to a single scalar comparison. */
12596 if ((TREE_CODE (arg0) == COMPLEX_EXPR
12597 || TREE_CODE (arg0) == COMPLEX_CST)
12598 && (TREE_CODE (arg1) == COMPLEX_EXPR
12599 || TREE_CODE (arg1) == COMPLEX_CST))
12601 tree real0, imag0, real1, imag1;
12604 if (TREE_CODE (arg0) == COMPLEX_EXPR)
12606 real0 = TREE_OPERAND (arg0, 0);
12607 imag0 = TREE_OPERAND (arg0, 1);
12611 real0 = TREE_REALPART (arg0);
12612 imag0 = TREE_IMAGPART (arg0);
12615 if (TREE_CODE (arg1) == COMPLEX_EXPR)
12617 real1 = TREE_OPERAND (arg1, 0);
12618 imag1 = TREE_OPERAND (arg1, 1);
12622 real1 = TREE_REALPART (arg1);
12623 imag1 = TREE_IMAGPART (arg1);
12626 rcond = fold_binary (code, type, real0, real1);
12627 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12629 if (integer_zerop (rcond))
12631 if (code == EQ_EXPR)
12632 return omit_two_operands (type, boolean_false_node,
12634 return fold_build2 (NE_EXPR, type, imag0, imag1);
12638 if (code == NE_EXPR)
12639 return omit_two_operands (type, boolean_true_node,
12641 return fold_build2 (EQ_EXPR, type, imag0, imag1);
12645 icond = fold_binary (code, type, imag0, imag1);
12646 if (icond && TREE_CODE (icond) == INTEGER_CST)
12648 if (integer_zerop (icond))
12650 if (code == EQ_EXPR)
12651 return omit_two_operands (type, boolean_false_node,
12653 return fold_build2 (NE_EXPR, type, real0, real1);
12657 if (code == NE_EXPR)
12658 return omit_two_operands (type, boolean_true_node,
12660 return fold_build2 (EQ_EXPR, type, real0, real1);
12671 tem = fold_comparison (code, type, op0, op1);
12672 if (tem != NULL_TREE)
12675 /* Transform comparisons of the form X +- C CMP X. */
12676 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12677 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12678 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12679 && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
12680 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12681 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
12683 tree arg01 = TREE_OPERAND (arg0, 1);
12684 enum tree_code code0 = TREE_CODE (arg0);
12687 if (TREE_CODE (arg01) == REAL_CST)
12688 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12690 is_positive = tree_int_cst_sgn (arg01);
12692 /* (X - c) > X becomes false. */
12693 if (code == GT_EXPR
12694 && ((code0 == MINUS_EXPR && is_positive >= 0)
12695 || (code0 == PLUS_EXPR && is_positive <= 0)))
12697 if (TREE_CODE (arg01) == INTEGER_CST
12698 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12699 fold_overflow_warning (("assuming signed overflow does not "
12700 "occur when assuming that (X - c) > X "
12701 "is always false"),
12702 WARN_STRICT_OVERFLOW_ALL);
12703 return constant_boolean_node (0, type);
12706 /* Likewise (X + c) < X becomes false. */
12707 if (code == LT_EXPR
12708 && ((code0 == PLUS_EXPR && is_positive >= 0)
12709 || (code0 == MINUS_EXPR && is_positive <= 0)))
12711 if (TREE_CODE (arg01) == INTEGER_CST
12712 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12713 fold_overflow_warning (("assuming signed overflow does not "
12714 "occur when assuming that "
12715 "(X + c) < X is always false"),
12716 WARN_STRICT_OVERFLOW_ALL);
12717 return constant_boolean_node (0, type);
12720 /* Convert (X - c) <= X to true. */
12721 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12723 && ((code0 == MINUS_EXPR && is_positive >= 0)
12724 || (code0 == PLUS_EXPR && is_positive <= 0)))
12726 if (TREE_CODE (arg01) == INTEGER_CST
12727 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12728 fold_overflow_warning (("assuming signed overflow does not "
12729 "occur when assuming that "
12730 "(X - c) <= X is always true"),
12731 WARN_STRICT_OVERFLOW_ALL);
12732 return constant_boolean_node (1, type);
12735 /* Convert (X + c) >= X to true. */
12736 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12738 && ((code0 == PLUS_EXPR && is_positive >= 0)
12739 || (code0 == MINUS_EXPR && is_positive <= 0)))
12741 if (TREE_CODE (arg01) == INTEGER_CST
12742 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12743 fold_overflow_warning (("assuming signed overflow does not "
12744 "occur when assuming that "
12745 "(X + c) >= X is always true"),
12746 WARN_STRICT_OVERFLOW_ALL);
12747 return constant_boolean_node (1, type);
12750 if (TREE_CODE (arg01) == INTEGER_CST)
12752 /* Convert X + c > X and X - c < X to true for integers. */
12753 if (code == GT_EXPR
12754 && ((code0 == PLUS_EXPR && is_positive > 0)
12755 || (code0 == MINUS_EXPR && is_positive < 0)))
12757 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12758 fold_overflow_warning (("assuming signed overflow does "
12759 "not occur when assuming that "
12760 "(X + c) > X is always true"),
12761 WARN_STRICT_OVERFLOW_ALL);
12762 return constant_boolean_node (1, type);
12765 if (code == LT_EXPR
12766 && ((code0 == MINUS_EXPR && is_positive > 0)
12767 || (code0 == PLUS_EXPR && is_positive < 0)))
12769 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12770 fold_overflow_warning (("assuming signed overflow does "
12771 "not occur when assuming that "
12772 "(X - c) < X is always true"),
12773 WARN_STRICT_OVERFLOW_ALL);
12774 return constant_boolean_node (1, type);
12777 /* Convert X + c <= X and X - c >= X to false for integers. */
12778 if (code == LE_EXPR
12779 && ((code0 == PLUS_EXPR && is_positive > 0)
12780 || (code0 == MINUS_EXPR && is_positive < 0)))
12782 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12783 fold_overflow_warning (("assuming signed overflow does "
12784 "not occur when assuming that "
12785 "(X + c) <= X is always false"),
12786 WARN_STRICT_OVERFLOW_ALL);
12787 return constant_boolean_node (0, type);
12790 if (code == GE_EXPR
12791 && ((code0 == MINUS_EXPR && is_positive > 0)
12792 || (code0 == PLUS_EXPR && is_positive < 0)))
12794 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12795 fold_overflow_warning (("assuming signed overflow does "
12796 "not occur when assuming that "
12797 "(X - c) >= X is always false"),
12798 WARN_STRICT_OVERFLOW_ALL);
12799 return constant_boolean_node (0, type);
12804 /* Comparisons with the highest or lowest possible integer of
12805 the specified precision will have known values. */
12807 tree arg1_type = TREE_TYPE (arg1);
12808 unsigned int width = TYPE_PRECISION (arg1_type);
12810 if (TREE_CODE (arg1) == INTEGER_CST
12811 && width <= 2 * HOST_BITS_PER_WIDE_INT
12812 && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
12814 HOST_WIDE_INT signed_max_hi;
12815 unsigned HOST_WIDE_INT signed_max_lo;
12816 unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
12818 if (width <= HOST_BITS_PER_WIDE_INT)
12820 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12825 if (TYPE_UNSIGNED (arg1_type))
12827 max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12833 max_lo = signed_max_lo;
12834 min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12840 width -= HOST_BITS_PER_WIDE_INT;
12841 signed_max_lo = -1;
12842 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12847 if (TYPE_UNSIGNED (arg1_type))
12849 max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12854 max_hi = signed_max_hi;
12855 min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12859 if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
12860 && TREE_INT_CST_LOW (arg1) == max_lo)
12864 return omit_one_operand (type, integer_zero_node, arg0);
12867 return fold_build2 (EQ_EXPR, type, op0, op1);
12870 return omit_one_operand (type, integer_one_node, arg0);
12873 return fold_build2 (NE_EXPR, type, op0, op1);
12875 /* The GE_EXPR and LT_EXPR cases above are not normally
12876 reached because of previous transformations. */
12881 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12883 && TREE_INT_CST_LOW (arg1) == max_lo - 1)
12887 arg1 = const_binop (PLUS_EXPR, arg1,
12888 build_int_cst (TREE_TYPE (arg1), 1), 0);
12889 return fold_build2 (EQ_EXPR, type,
12890 fold_convert (TREE_TYPE (arg1), arg0),
12893 arg1 = const_binop (PLUS_EXPR, arg1,
12894 build_int_cst (TREE_TYPE (arg1), 1), 0);
12895 return fold_build2 (NE_EXPR, type,
12896 fold_convert (TREE_TYPE (arg1), arg0),
12901 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12903 && TREE_INT_CST_LOW (arg1) == min_lo)
12907 return omit_one_operand (type, integer_zero_node, arg0);
12910 return fold_build2 (EQ_EXPR, type, op0, op1);
12913 return omit_one_operand (type, integer_one_node, arg0);
12916 return fold_build2 (NE_EXPR, type, op0, op1);
12921 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12923 && TREE_INT_CST_LOW (arg1) == min_lo + 1)
12927 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
12928 return fold_build2 (NE_EXPR, type,
12929 fold_convert (TREE_TYPE (arg1), arg0),
12932 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
12933 return fold_build2 (EQ_EXPR, type,
12934 fold_convert (TREE_TYPE (arg1), arg0),
12940 else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
12941 && TREE_INT_CST_LOW (arg1) == signed_max_lo
12942 && TYPE_UNSIGNED (arg1_type)
12943 /* We will flip the signedness of the comparison operator
12944 associated with the mode of arg1, so the sign bit is
12945 specified by this mode. Check that arg1 is the signed
12946 max associated with this sign bit. */
12947 && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
12948 /* signed_type does not work on pointer types. */
12949 && INTEGRAL_TYPE_P (arg1_type))
12951 /* The following case also applies to X < signed_max+1
12952 and X >= signed_max+1 because previous transformations. */
12953 if (code == LE_EXPR || code == GT_EXPR)
12956 st = signed_type_for (TREE_TYPE (arg1));
12957 return fold_build2 (code == LE_EXPR ? GE_EXPR : LT_EXPR,
12958 type, fold_convert (st, arg0),
12959 build_int_cst (st, 0));
12965 /* If we are comparing an ABS_EXPR with a constant, we can
12966 convert all the cases into explicit comparisons, but they may
12967 well not be faster than doing the ABS and one comparison.
12968 But ABS (X) <= C is a range comparison, which becomes a subtraction
12969 and a comparison, and is probably faster. */
12970 if (code == LE_EXPR
12971 && TREE_CODE (arg1) == INTEGER_CST
12972 && TREE_CODE (arg0) == ABS_EXPR
12973 && ! TREE_SIDE_EFFECTS (arg0)
12974 && (0 != (tem = negate_expr (arg1)))
12975 && TREE_CODE (tem) == INTEGER_CST
12976 && !TREE_OVERFLOW (tem))
12977 return fold_build2 (TRUTH_ANDIF_EXPR, type,
12978 build2 (GE_EXPR, type,
12979 TREE_OPERAND (arg0, 0), tem),
12980 build2 (LE_EXPR, type,
12981 TREE_OPERAND (arg0, 0), arg1));
12983 /* Convert ABS_EXPR<x> >= 0 to true. */
12984 strict_overflow_p = false;
12985 if (code == GE_EXPR
12986 && (integer_zerop (arg1)
12987 || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
12988 && real_zerop (arg1)))
12989 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12991 if (strict_overflow_p)
12992 fold_overflow_warning (("assuming signed overflow does not occur "
12993 "when simplifying comparison of "
12994 "absolute value and zero"),
12995 WARN_STRICT_OVERFLOW_CONDITIONAL);
12996 return omit_one_operand (type, integer_one_node, arg0);
12999 /* Convert ABS_EXPR<x> < 0 to false. */
13000 strict_overflow_p = false;
13001 if (code == LT_EXPR
13002 && (integer_zerop (arg1) || real_zerop (arg1))
13003 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
13005 if (strict_overflow_p)
13006 fold_overflow_warning (("assuming signed overflow does not occur "
13007 "when simplifying comparison of "
13008 "absolute value and zero"),
13009 WARN_STRICT_OVERFLOW_CONDITIONAL);
13010 return omit_one_operand (type, integer_zero_node, arg0);
13013 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
13014 and similarly for >= into !=. */
13015 if ((code == LT_EXPR || code == GE_EXPR)
13016 && TYPE_UNSIGNED (TREE_TYPE (arg0))
13017 && TREE_CODE (arg1) == LSHIFT_EXPR
13018 && integer_onep (TREE_OPERAND (arg1, 0)))
13019 return build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13020 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13021 TREE_OPERAND (arg1, 1)),
13022 build_int_cst (TREE_TYPE (arg0), 0));
13024 if ((code == LT_EXPR || code == GE_EXPR)
13025 && TYPE_UNSIGNED (TREE_TYPE (arg0))
13026 && CONVERT_EXPR_P (arg1)
13027 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
13028 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
13030 build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
13031 fold_convert (TREE_TYPE (arg0),
13032 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
13033 TREE_OPERAND (TREE_OPERAND (arg1, 0),
13035 build_int_cst (TREE_TYPE (arg0), 0));
13039 case UNORDERED_EXPR:
13047 if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
13049 t1 = fold_relational_const (code, type, arg0, arg1);
13050 if (t1 != NULL_TREE)
13054 /* If the first operand is NaN, the result is constant. */
13055 if (TREE_CODE (arg0) == REAL_CST
13056 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
13057 && (code != LTGT_EXPR || ! flag_trapping_math))
13059 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13060 ? integer_zero_node
13061 : integer_one_node;
13062 return omit_one_operand (type, t1, arg1);
13065 /* If the second operand is NaN, the result is constant. */
13066 if (TREE_CODE (arg1) == REAL_CST
13067 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
13068 && (code != LTGT_EXPR || ! flag_trapping_math))
13070 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
13071 ? integer_zero_node
13072 : integer_one_node;
13073 return omit_one_operand (type, t1, arg0);
13076 /* Simplify unordered comparison of something with itself. */
13077 if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
13078 && operand_equal_p (arg0, arg1, 0))
13079 return constant_boolean_node (1, type);
13081 if (code == LTGT_EXPR
13082 && !flag_trapping_math
13083 && operand_equal_p (arg0, arg1, 0))
13084 return constant_boolean_node (0, type);
13086 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
13088 tree targ0 = strip_float_extensions (arg0);
13089 tree targ1 = strip_float_extensions (arg1);
13090 tree newtype = TREE_TYPE (targ0);
13092 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
13093 newtype = TREE_TYPE (targ1);
13095 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
13096 return fold_build2 (code, type, fold_convert (newtype, targ0),
13097 fold_convert (newtype, targ1));
13102 case COMPOUND_EXPR:
13103 /* When pedantic, a compound expression can be neither an lvalue
13104 nor an integer constant expression. */
13105 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
13107 /* Don't let (0, 0) be null pointer constant. */
13108 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
13109 : fold_convert (type, arg1);
13110 return pedantic_non_lvalue (tem);
13113 if ((TREE_CODE (arg0) == REAL_CST
13114 && TREE_CODE (arg1) == REAL_CST)
13115 || (TREE_CODE (arg0) == INTEGER_CST
13116 && TREE_CODE (arg1) == INTEGER_CST))
13117 return build_complex (type, arg0, arg1);
13121 /* An ASSERT_EXPR should never be passed to fold_binary. */
13122 gcc_unreachable ();
13126 } /* switch (code) */
13129 /* Callback for walk_tree, looking for LABEL_EXPR.
13130 Returns tree TP if it is LABEL_EXPR. Otherwise it returns NULL_TREE.
13131 Do not check the sub-tree of GOTO_EXPR. */
13134 contains_label_1 (tree *tp,
13135 int *walk_subtrees,
13136 void *data ATTRIBUTE_UNUSED)
13138 switch (TREE_CODE (*tp))
13143 *walk_subtrees = 0;
13150 /* Checks whether the sub-tree ST contains a label LABEL_EXPR which is
13151 accessible from outside the sub-tree. Returns NULL_TREE if no
13152 addressable label is found. */
13155 contains_label_p (tree st)
13157 return (walk_tree (&st, contains_label_1 , NULL, NULL) != NULL_TREE);
13160 /* Fold a ternary expression of code CODE and type TYPE with operands
13161 OP0, OP1, and OP2. Return the folded expression if folding is
13162 successful. Otherwise, return NULL_TREE. */
13165 fold_ternary (enum tree_code code, tree type, tree op0, tree op1, tree op2)
13168 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
13169 enum tree_code_class kind = TREE_CODE_CLASS (code);
13171 gcc_assert (IS_EXPR_CODE_CLASS (kind)
13172 && TREE_CODE_LENGTH (code) == 3);
13174 /* Strip any conversions that don't change the mode. This is safe
13175 for every expression, except for a comparison expression because
13176 its signedness is derived from its operands. So, in the latter
13177 case, only strip conversions that don't change the signedness.
13179 Note that this is done as an internal manipulation within the
13180 constant folder, in order to find the simplest representation of
13181 the arguments so that their form can be studied. In any cases,
13182 the appropriate type conversions should be put back in the tree
13183 that will get out of the constant folder. */
13198 case COMPONENT_REF:
13199 if (TREE_CODE (arg0) == CONSTRUCTOR
13200 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
13202 unsigned HOST_WIDE_INT idx;
13204 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
13211 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
13212 so all simple results must be passed through pedantic_non_lvalue. */
13213 if (TREE_CODE (arg0) == INTEGER_CST)
13215 tree unused_op = integer_zerop (arg0) ? op1 : op2;
13216 tem = integer_zerop (arg0) ? op2 : op1;
13217 /* Only optimize constant conditions when the selected branch
13218 has the same type as the COND_EXPR. This avoids optimizing
13219 away "c ? x : throw", where the throw has a void type.
13220 Avoid throwing away that operand which contains label. */
13221 if ((!TREE_SIDE_EFFECTS (unused_op)
13222 || !contains_label_p (unused_op))
13223 && (! VOID_TYPE_P (TREE_TYPE (tem))
13224 || VOID_TYPE_P (type)))
13225 return pedantic_non_lvalue (tem);
13228 if (operand_equal_p (arg1, op2, 0))
13229 return pedantic_omit_one_operand (type, arg1, arg0);
13231 /* If we have A op B ? A : C, we may be able to convert this to a
13232 simpler expression, depending on the operation and the values
13233 of B and C. Signed zeros prevent all of these transformations,
13234 for reasons given above each one.
13236 Also try swapping the arguments and inverting the conditional. */
13237 if (COMPARISON_CLASS_P (arg0)
13238 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13239 arg1, TREE_OPERAND (arg0, 1))
13240 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
13242 tem = fold_cond_expr_with_comparison (type, arg0, op1, op2);
13247 if (COMPARISON_CLASS_P (arg0)
13248 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
13250 TREE_OPERAND (arg0, 1))
13251 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
13253 tem = fold_truth_not_expr (arg0);
13254 if (tem && COMPARISON_CLASS_P (tem))
13256 tem = fold_cond_expr_with_comparison (type, tem, op2, op1);
13262 /* If the second operand is simpler than the third, swap them
13263 since that produces better jump optimization results. */
13264 if (truth_value_p (TREE_CODE (arg0))
13265 && tree_swap_operands_p (op1, op2, false))
13267 /* See if this can be inverted. If it can't, possibly because
13268 it was a floating-point inequality comparison, don't do
13270 tem = fold_truth_not_expr (arg0);
13272 return fold_build3 (code, type, tem, op2, op1);
13275 /* Convert A ? 1 : 0 to simply A. */
13276 if (integer_onep (op1)
13277 && integer_zerop (op2)
13278 /* If we try to convert OP0 to our type, the
13279 call to fold will try to move the conversion inside
13280 a COND, which will recurse. In that case, the COND_EXPR
13281 is probably the best choice, so leave it alone. */
13282 && type == TREE_TYPE (arg0))
13283 return pedantic_non_lvalue (arg0);
13285 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
13286 over COND_EXPR in cases such as floating point comparisons. */
13287 if (integer_zerop (op1)
13288 && integer_onep (op2)
13289 && truth_value_p (TREE_CODE (arg0)))
13290 return pedantic_non_lvalue (fold_convert (type,
13291 invert_truthvalue (arg0)));
13293 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
13294 if (TREE_CODE (arg0) == LT_EXPR
13295 && integer_zerop (TREE_OPERAND (arg0, 1))
13296 && integer_zerop (op2)
13297 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
13299 /* sign_bit_p only checks ARG1 bits within A's precision.
13300 If <sign bit of A> has wider type than A, bits outside
13301 of A's precision in <sign bit of A> need to be checked.
13302 If they are all 0, this optimization needs to be done
13303 in unsigned A's type, if they are all 1 in signed A's type,
13304 otherwise this can't be done. */
13305 if (TYPE_PRECISION (TREE_TYPE (tem))
13306 < TYPE_PRECISION (TREE_TYPE (arg1))
13307 && TYPE_PRECISION (TREE_TYPE (tem))
13308 < TYPE_PRECISION (type))
13310 unsigned HOST_WIDE_INT mask_lo;
13311 HOST_WIDE_INT mask_hi;
13312 int inner_width, outer_width;
13315 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
13316 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
13317 if (outer_width > TYPE_PRECISION (type))
13318 outer_width = TYPE_PRECISION (type);
13320 if (outer_width > HOST_BITS_PER_WIDE_INT)
13322 mask_hi = ((unsigned HOST_WIDE_INT) -1
13323 >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
13329 mask_lo = ((unsigned HOST_WIDE_INT) -1
13330 >> (HOST_BITS_PER_WIDE_INT - outer_width));
13332 if (inner_width > HOST_BITS_PER_WIDE_INT)
13334 mask_hi &= ~((unsigned HOST_WIDE_INT) -1
13335 >> (HOST_BITS_PER_WIDE_INT - inner_width));
13339 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
13340 >> (HOST_BITS_PER_WIDE_INT - inner_width));
13342 if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
13343 && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
13345 tem_type = signed_type_for (TREE_TYPE (tem));
13346 tem = fold_convert (tem_type, tem);
13348 else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
13349 && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
13351 tem_type = unsigned_type_for (TREE_TYPE (tem));
13352 tem = fold_convert (tem_type, tem);
13359 return fold_convert (type,
13360 fold_build2 (BIT_AND_EXPR,
13361 TREE_TYPE (tem), tem,
13362 fold_convert (TREE_TYPE (tem),
13366 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
13367 already handled above. */
13368 if (TREE_CODE (arg0) == BIT_AND_EXPR
13369 && integer_onep (TREE_OPERAND (arg0, 1))
13370 && integer_zerop (op2)
13371 && integer_pow2p (arg1))
13373 tree tem = TREE_OPERAND (arg0, 0);
13375 if (TREE_CODE (tem) == RSHIFT_EXPR
13376 && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
13377 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
13378 TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
13379 return fold_build2 (BIT_AND_EXPR, type,
13380 TREE_OPERAND (tem, 0), arg1);
13383 /* A & N ? N : 0 is simply A & N if N is a power of two. This
13384 is probably obsolete because the first operand should be a
13385 truth value (that's why we have the two cases above), but let's
13386 leave it in until we can confirm this for all front-ends. */
13387 if (integer_zerop (op2)
13388 && TREE_CODE (arg0) == NE_EXPR
13389 && integer_zerop (TREE_OPERAND (arg0, 1))
13390 && integer_pow2p (arg1)
13391 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13392 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13393 arg1, OEP_ONLY_CONST))
13394 return pedantic_non_lvalue (fold_convert (type,
13395 TREE_OPERAND (arg0, 0)));
13397 /* Convert A ? B : 0 into A && B if A and B are truth values. */
13398 if (integer_zerop (op2)
13399 && truth_value_p (TREE_CODE (arg0))
13400 && truth_value_p (TREE_CODE (arg1)))
13401 return fold_build2 (TRUTH_ANDIF_EXPR, type,
13402 fold_convert (type, arg0),
13405 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
13406 if (integer_onep (op2)
13407 && truth_value_p (TREE_CODE (arg0))
13408 && truth_value_p (TREE_CODE (arg1)))
13410 /* Only perform transformation if ARG0 is easily inverted. */
13411 tem = fold_truth_not_expr (arg0);
13413 return fold_build2 (TRUTH_ORIF_EXPR, type,
13414 fold_convert (type, tem),
13418 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13419 if (integer_zerop (arg1)
13420 && truth_value_p (TREE_CODE (arg0))
13421 && truth_value_p (TREE_CODE (op2)))
13423 /* Only perform transformation if ARG0 is easily inverted. */
13424 tem = fold_truth_not_expr (arg0);
13426 return fold_build2 (TRUTH_ANDIF_EXPR, type,
13427 fold_convert (type, tem),
13431 /* Convert A ? 1 : B into A || B if A and B are truth values. */
13432 if (integer_onep (arg1)
13433 && truth_value_p (TREE_CODE (arg0))
13434 && truth_value_p (TREE_CODE (op2)))
13435 return fold_build2 (TRUTH_ORIF_EXPR, type,
13436 fold_convert (type, arg0),
13442 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
13443 of fold_ternary on them. */
13444 gcc_unreachable ();
13446 case BIT_FIELD_REF:
13447 if ((TREE_CODE (arg0) == VECTOR_CST
13448 || (TREE_CODE (arg0) == CONSTRUCTOR && TREE_CONSTANT (arg0)))
13449 && type == TREE_TYPE (TREE_TYPE (arg0)))
13451 unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
13452 unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
13455 && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
13456 && (idx % width) == 0
13457 && (idx = idx / width)
13458 < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
13460 tree elements = NULL_TREE;
13462 if (TREE_CODE (arg0) == VECTOR_CST)
13463 elements = TREE_VECTOR_CST_ELTS (arg0);
13466 unsigned HOST_WIDE_INT idx;
13469 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg0), idx, value)
13470 elements = tree_cons (NULL_TREE, value, elements);
13472 while (idx-- > 0 && elements)
13473 elements = TREE_CHAIN (elements);
13475 return TREE_VALUE (elements);
13477 return fold_convert (type, integer_zero_node);
13481 /* A bit-field-ref that referenced the full argument can be stripped. */
13482 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
13483 && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_low_cst (arg1, 1)
13484 && integer_zerop (op2))
13485 return fold_convert (type, arg0);
13491 } /* switch (code) */
13494 /* Perform constant folding and related simplification of EXPR.
13495 The related simplifications include x*1 => x, x*0 => 0, etc.,
13496 and application of the associative law.
13497 NOP_EXPR conversions may be removed freely (as long as we
13498 are careful not to change the type of the overall expression).
13499 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13500 but we can constant-fold them if they have constant operands. */
13502 #ifdef ENABLE_FOLD_CHECKING
13503 # define fold(x) fold_1 (x)
13504 static tree fold_1 (tree);
13510 const tree t = expr;
13511 enum tree_code code = TREE_CODE (t);
13512 enum tree_code_class kind = TREE_CODE_CLASS (code);
13515 /* Return right away if a constant. */
13516 if (kind == tcc_constant)
13519 /* CALL_EXPR-like objects with variable numbers of operands are
13520 treated specially. */
13521 if (kind == tcc_vl_exp)
13523 if (code == CALL_EXPR)
13525 tem = fold_call_expr (expr, false);
13526 return tem ? tem : expr;
13531 if (IS_EXPR_CODE_CLASS (kind))
13533 tree type = TREE_TYPE (t);
13534 tree op0, op1, op2;
13536 switch (TREE_CODE_LENGTH (code))
13539 op0 = TREE_OPERAND (t, 0);
13540 tem = fold_unary (code, type, op0);
13541 return tem ? tem : expr;
13543 op0 = TREE_OPERAND (t, 0);
13544 op1 = TREE_OPERAND (t, 1);
13545 tem = fold_binary (code, type, op0, op1);
13546 return tem ? tem : expr;
13548 op0 = TREE_OPERAND (t, 0);
13549 op1 = TREE_OPERAND (t, 1);
13550 op2 = TREE_OPERAND (t, 2);
13551 tem = fold_ternary (code, type, op0, op1, op2);
13552 return tem ? tem : expr;
13562 tree op0 = TREE_OPERAND (t, 0);
13563 tree op1 = TREE_OPERAND (t, 1);
13565 if (TREE_CODE (op1) == INTEGER_CST
13566 && TREE_CODE (op0) == CONSTRUCTOR
13567 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13569 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (op0);
13570 unsigned HOST_WIDE_INT end = VEC_length (constructor_elt, elts);
13571 unsigned HOST_WIDE_INT begin = 0;
13573 /* Find a matching index by means of a binary search. */
13574 while (begin != end)
13576 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
13577 tree index = VEC_index (constructor_elt, elts, middle)->index;
13579 if (TREE_CODE (index) == INTEGER_CST
13580 && tree_int_cst_lt (index, op1))
13581 begin = middle + 1;
13582 else if (TREE_CODE (index) == INTEGER_CST
13583 && tree_int_cst_lt (op1, index))
13585 else if (TREE_CODE (index) == RANGE_EXPR
13586 && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
13587 begin = middle + 1;
13588 else if (TREE_CODE (index) == RANGE_EXPR
13589 && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
13592 return VEC_index (constructor_elt, elts, middle)->value;
13600 return fold (DECL_INITIAL (t));
13604 } /* switch (code) */
13607 #ifdef ENABLE_FOLD_CHECKING
13610 static void fold_checksum_tree (const_tree, struct md5_ctx *, htab_t);
13611 static void fold_check_failed (const_tree, const_tree);
13612 void print_fold_checksum (const_tree);
13614 /* When --enable-checking=fold, compute a digest of expr before
13615 and after actual fold call to see if fold did not accidentally
13616 change original expr. */
13622 struct md5_ctx ctx;
13623 unsigned char checksum_before[16], checksum_after[16];
13626 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13627 md5_init_ctx (&ctx);
13628 fold_checksum_tree (expr, &ctx, ht);
13629 md5_finish_ctx (&ctx, checksum_before);
13632 ret = fold_1 (expr);
13634 md5_init_ctx (&ctx);
13635 fold_checksum_tree (expr, &ctx, ht);
13636 md5_finish_ctx (&ctx, checksum_after);
13639 if (memcmp (checksum_before, checksum_after, 16))
13640 fold_check_failed (expr, ret);
13646 print_fold_checksum (const_tree expr)
13648 struct md5_ctx ctx;
13649 unsigned char checksum[16], cnt;
13652 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13653 md5_init_ctx (&ctx);
13654 fold_checksum_tree (expr, &ctx, ht);
13655 md5_finish_ctx (&ctx, checksum);
13657 for (cnt = 0; cnt < 16; ++cnt)
13658 fprintf (stderr, "%02x", checksum[cnt]);
13659 putc ('\n', stderr);
13663 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13665 internal_error ("fold check: original tree changed by fold");
13669 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, htab_t ht)
13672 enum tree_code code;
13673 union tree_node buf;
13678 gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
13679 <= sizeof (struct tree_function_decl))
13680 && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
13683 slot = (const void **) htab_find_slot (ht, expr, INSERT);
13687 code = TREE_CODE (expr);
13688 if (TREE_CODE_CLASS (code) == tcc_declaration
13689 && DECL_ASSEMBLER_NAME_SET_P (expr))
13691 /* Allow DECL_ASSEMBLER_NAME to be modified. */
13692 memcpy ((char *) &buf, expr, tree_size (expr));
13693 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
13694 expr = (tree) &buf;
13696 else if (TREE_CODE_CLASS (code) == tcc_type
13697 && (TYPE_POINTER_TO (expr)
13698 || TYPE_REFERENCE_TO (expr)
13699 || TYPE_CACHED_VALUES_P (expr)
13700 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)
13701 || TYPE_NEXT_VARIANT (expr)))
13703 /* Allow these fields to be modified. */
13705 memcpy ((char *) &buf, expr, tree_size (expr));
13706 expr = tmp = (tree) &buf;
13707 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13708 TYPE_POINTER_TO (tmp) = NULL;
13709 TYPE_REFERENCE_TO (tmp) = NULL;
13710 TYPE_NEXT_VARIANT (tmp) = NULL;
13711 if (TYPE_CACHED_VALUES_P (tmp))
13713 TYPE_CACHED_VALUES_P (tmp) = 0;
13714 TYPE_CACHED_VALUES (tmp) = NULL;
13717 md5_process_bytes (expr, tree_size (expr), ctx);
13718 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13719 if (TREE_CODE_CLASS (code) != tcc_type
13720 && TREE_CODE_CLASS (code) != tcc_declaration
13721 && code != TREE_LIST
13722 && code != SSA_NAME)
13723 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13724 switch (TREE_CODE_CLASS (code))
13730 md5_process_bytes (TREE_STRING_POINTER (expr),
13731 TREE_STRING_LENGTH (expr), ctx);
13734 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13735 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13738 fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
13744 case tcc_exceptional:
13748 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13749 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13750 expr = TREE_CHAIN (expr);
13751 goto recursive_label;
13754 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13755 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13761 case tcc_expression:
13762 case tcc_reference:
13763 case tcc_comparison:
13766 case tcc_statement:
13768 len = TREE_OPERAND_LENGTH (expr);
13769 for (i = 0; i < len; ++i)
13770 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13772 case tcc_declaration:
13773 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13774 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13775 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13777 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13778 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13779 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13780 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13781 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13783 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
13784 fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
13786 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13788 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13789 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13790 fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
13794 if (TREE_CODE (expr) == ENUMERAL_TYPE)
13795 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13796 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13797 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13798 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13799 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13800 if (INTEGRAL_TYPE_P (expr)
13801 || SCALAR_FLOAT_TYPE_P (expr))
13803 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13804 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13806 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13807 if (TREE_CODE (expr) == RECORD_TYPE
13808 || TREE_CODE (expr) == UNION_TYPE
13809 || TREE_CODE (expr) == QUAL_UNION_TYPE)
13810 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13811 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13818 /* Helper function for outputting the checksum of a tree T. When
13819 debugging with gdb, you can "define mynext" to be "next" followed
13820 by "call debug_fold_checksum (op0)", then just trace down till the
13824 debug_fold_checksum (const_tree t)
13827 unsigned char checksum[16];
13828 struct md5_ctx ctx;
13829 htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13831 md5_init_ctx (&ctx);
13832 fold_checksum_tree (t, &ctx, ht);
13833 md5_finish_ctx (&ctx, checksum);
13836 for (i = 0; i < 16; i++)
13837 fprintf (stderr, "%d ", checksum[i]);
13839 fprintf (stderr, "\n");
13844 /* Fold a unary tree expression with code CODE of type TYPE with an
13845 operand OP0. Return a folded expression if successful. Otherwise,
13846 return a tree expression with code CODE of type TYPE with an
13850 fold_build1_stat (enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13853 #ifdef ENABLE_FOLD_CHECKING
13854 unsigned char checksum_before[16], checksum_after[16];
13855 struct md5_ctx ctx;
13858 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13859 md5_init_ctx (&ctx);
13860 fold_checksum_tree (op0, &ctx, ht);
13861 md5_finish_ctx (&ctx, checksum_before);
13865 tem = fold_unary (code, type, op0);
13867 tem = build1_stat (code, type, op0 PASS_MEM_STAT);
13869 #ifdef ENABLE_FOLD_CHECKING
13870 md5_init_ctx (&ctx);
13871 fold_checksum_tree (op0, &ctx, ht);
13872 md5_finish_ctx (&ctx, checksum_after);
13875 if (memcmp (checksum_before, checksum_after, 16))
13876 fold_check_failed (op0, tem);
13881 /* Fold a binary tree expression with code CODE of type TYPE with
13882 operands OP0 and OP1. Return a folded expression if successful.
13883 Otherwise, return a tree expression with code CODE of type TYPE
13884 with operands OP0 and OP1. */
13887 fold_build2_stat (enum tree_code code, tree type, tree op0, tree op1
13891 #ifdef ENABLE_FOLD_CHECKING
13892 unsigned char checksum_before_op0[16],
13893 checksum_before_op1[16],
13894 checksum_after_op0[16],
13895 checksum_after_op1[16];
13896 struct md5_ctx ctx;
13899 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13900 md5_init_ctx (&ctx);
13901 fold_checksum_tree (op0, &ctx, ht);
13902 md5_finish_ctx (&ctx, checksum_before_op0);
13905 md5_init_ctx (&ctx);
13906 fold_checksum_tree (op1, &ctx, ht);
13907 md5_finish_ctx (&ctx, checksum_before_op1);
13911 tem = fold_binary (code, type, op0, op1);
13913 tem = build2_stat (code, type, op0, op1 PASS_MEM_STAT);
13915 #ifdef ENABLE_FOLD_CHECKING
13916 md5_init_ctx (&ctx);
13917 fold_checksum_tree (op0, &ctx, ht);
13918 md5_finish_ctx (&ctx, checksum_after_op0);
13921 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13922 fold_check_failed (op0, tem);
13924 md5_init_ctx (&ctx);
13925 fold_checksum_tree (op1, &ctx, ht);
13926 md5_finish_ctx (&ctx, checksum_after_op1);
13929 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13930 fold_check_failed (op1, tem);
13935 /* Fold a ternary tree expression with code CODE of type TYPE with
13936 operands OP0, OP1, and OP2. Return a folded expression if
13937 successful. Otherwise, return a tree expression with code CODE of
13938 type TYPE with operands OP0, OP1, and OP2. */
13941 fold_build3_stat (enum tree_code code, tree type, tree op0, tree op1, tree op2
13945 #ifdef ENABLE_FOLD_CHECKING
13946 unsigned char checksum_before_op0[16],
13947 checksum_before_op1[16],
13948 checksum_before_op2[16],
13949 checksum_after_op0[16],
13950 checksum_after_op1[16],
13951 checksum_after_op2[16];
13952 struct md5_ctx ctx;
13955 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13956 md5_init_ctx (&ctx);
13957 fold_checksum_tree (op0, &ctx, ht);
13958 md5_finish_ctx (&ctx, checksum_before_op0);
13961 md5_init_ctx (&ctx);
13962 fold_checksum_tree (op1, &ctx, ht);
13963 md5_finish_ctx (&ctx, checksum_before_op1);
13966 md5_init_ctx (&ctx);
13967 fold_checksum_tree (op2, &ctx, ht);
13968 md5_finish_ctx (&ctx, checksum_before_op2);
13972 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
13973 tem = fold_ternary (code, type, op0, op1, op2);
13975 tem = build3_stat (code, type, op0, op1, op2 PASS_MEM_STAT);
13977 #ifdef ENABLE_FOLD_CHECKING
13978 md5_init_ctx (&ctx);
13979 fold_checksum_tree (op0, &ctx, ht);
13980 md5_finish_ctx (&ctx, checksum_after_op0);
13983 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13984 fold_check_failed (op0, tem);
13986 md5_init_ctx (&ctx);
13987 fold_checksum_tree (op1, &ctx, ht);
13988 md5_finish_ctx (&ctx, checksum_after_op1);
13991 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13992 fold_check_failed (op1, tem);
13994 md5_init_ctx (&ctx);
13995 fold_checksum_tree (op2, &ctx, ht);
13996 md5_finish_ctx (&ctx, checksum_after_op2);
13999 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
14000 fold_check_failed (op2, tem);
14005 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
14006 arguments in ARGARRAY, and a null static chain.
14007 Return a folded expression if successful. Otherwise, return a CALL_EXPR
14008 of type TYPE from the given operands as constructed by build_call_array. */
14011 fold_build_call_array (tree type, tree fn, int nargs, tree *argarray)
14014 #ifdef ENABLE_FOLD_CHECKING
14015 unsigned char checksum_before_fn[16],
14016 checksum_before_arglist[16],
14017 checksum_after_fn[16],
14018 checksum_after_arglist[16];
14019 struct md5_ctx ctx;
14023 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
14024 md5_init_ctx (&ctx);
14025 fold_checksum_tree (fn, &ctx, ht);
14026 md5_finish_ctx (&ctx, checksum_before_fn);
14029 md5_init_ctx (&ctx);
14030 for (i = 0; i < nargs; i++)
14031 fold_checksum_tree (argarray[i], &ctx, ht);
14032 md5_finish_ctx (&ctx, checksum_before_arglist);
14036 tem = fold_builtin_call_array (type, fn, nargs, argarray);
14038 #ifdef ENABLE_FOLD_CHECKING
14039 md5_init_ctx (&ctx);
14040 fold_checksum_tree (fn, &ctx, ht);
14041 md5_finish_ctx (&ctx, checksum_after_fn);
14044 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
14045 fold_check_failed (fn, tem);
14047 md5_init_ctx (&ctx);
14048 for (i = 0; i < nargs; i++)
14049 fold_checksum_tree (argarray[i], &ctx, ht);
14050 md5_finish_ctx (&ctx, checksum_after_arglist);
14053 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
14054 fold_check_failed (NULL_TREE, tem);
14059 /* Perform constant folding and related simplification of initializer
14060 expression EXPR. These behave identically to "fold_buildN" but ignore
14061 potential run-time traps and exceptions that fold must preserve. */
14063 #define START_FOLD_INIT \
14064 int saved_signaling_nans = flag_signaling_nans;\
14065 int saved_trapping_math = flag_trapping_math;\
14066 int saved_rounding_math = flag_rounding_math;\
14067 int saved_trapv = flag_trapv;\
14068 int saved_folding_initializer = folding_initializer;\
14069 flag_signaling_nans = 0;\
14070 flag_trapping_math = 0;\
14071 flag_rounding_math = 0;\
14073 folding_initializer = 1;
14075 #define END_FOLD_INIT \
14076 flag_signaling_nans = saved_signaling_nans;\
14077 flag_trapping_math = saved_trapping_math;\
14078 flag_rounding_math = saved_rounding_math;\
14079 flag_trapv = saved_trapv;\
14080 folding_initializer = saved_folding_initializer;
14083 fold_build1_initializer (enum tree_code code, tree type, tree op)
14088 result = fold_build1 (code, type, op);
14095 fold_build2_initializer (enum tree_code code, tree type, tree op0, tree op1)
14100 result = fold_build2 (code, type, op0, op1);
14107 fold_build3_initializer (enum tree_code code, tree type, tree op0, tree op1,
14113 result = fold_build3 (code, type, op0, op1, op2);
14120 fold_build_call_array_initializer (tree type, tree fn,
14121 int nargs, tree *argarray)
14126 result = fold_build_call_array (type, fn, nargs, argarray);
14132 #undef START_FOLD_INIT
14133 #undef END_FOLD_INIT
14135 /* Determine if first argument is a multiple of second argument. Return 0 if
14136 it is not, or we cannot easily determined it to be.
14138 An example of the sort of thing we care about (at this point; this routine
14139 could surely be made more general, and expanded to do what the *_DIV_EXPR's
14140 fold cases do now) is discovering that
14142 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14148 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
14150 This code also handles discovering that
14152 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
14154 is a multiple of 8 so we don't have to worry about dealing with a
14155 possible remainder.
14157 Note that we *look* inside a SAVE_EXPR only to determine how it was
14158 calculated; it is not safe for fold to do much of anything else with the
14159 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
14160 at run time. For example, the latter example above *cannot* be implemented
14161 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
14162 evaluation time of the original SAVE_EXPR is not necessarily the same at
14163 the time the new expression is evaluated. The only optimization of this
14164 sort that would be valid is changing
14166 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
14170 SAVE_EXPR (I) * SAVE_EXPR (J)
14172 (where the same SAVE_EXPR (J) is used in the original and the
14173 transformed version). */
14176 multiple_of_p (tree type, const_tree top, const_tree bottom)
14178 if (operand_equal_p (top, bottom, 0))
14181 if (TREE_CODE (type) != INTEGER_TYPE)
14184 switch (TREE_CODE (top))
14187 /* Bitwise and provides a power of two multiple. If the mask is
14188 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
14189 if (!integer_pow2p (bottom))
14194 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14195 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14199 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
14200 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
14203 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
14207 op1 = TREE_OPERAND (top, 1);
14208 /* const_binop may not detect overflow correctly,
14209 so check for it explicitly here. */
14210 if (TYPE_PRECISION (TREE_TYPE (size_one_node))
14211 > TREE_INT_CST_LOW (op1)
14212 && TREE_INT_CST_HIGH (op1) == 0
14213 && 0 != (t1 = fold_convert (type,
14214 const_binop (LSHIFT_EXPR,
14217 && !TREE_OVERFLOW (t1))
14218 return multiple_of_p (type, t1, bottom);
14223 /* Can't handle conversions from non-integral or wider integral type. */
14224 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
14225 || (TYPE_PRECISION (type)
14226 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
14229 /* .. fall through ... */
14232 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
14235 if (TREE_CODE (bottom) != INTEGER_CST
14236 || integer_zerop (bottom)
14237 || (TYPE_UNSIGNED (type)
14238 && (tree_int_cst_sgn (top) < 0
14239 || tree_int_cst_sgn (bottom) < 0)))
14241 return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
14249 /* Return true if CODE or TYPE is known to be non-negative. */
14252 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
14254 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
14255 && truth_value_p (code))
14256 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
14257 have a signed:1 type (where the value is -1 and 0). */
14262 /* Return true if (CODE OP0) is known to be non-negative. If the return
14263 value is based on the assumption that signed overflow is undefined,
14264 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14265 *STRICT_OVERFLOW_P. */
14268 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14269 bool *strict_overflow_p)
14271 if (TYPE_UNSIGNED (type))
14277 /* We can't return 1 if flag_wrapv is set because
14278 ABS_EXPR<INT_MIN> = INT_MIN. */
14279 if (!INTEGRAL_TYPE_P (type))
14281 if (TYPE_OVERFLOW_UNDEFINED (type))
14283 *strict_overflow_p = true;
14288 case NON_LVALUE_EXPR:
14290 case FIX_TRUNC_EXPR:
14291 return tree_expr_nonnegative_warnv_p (op0,
14292 strict_overflow_p);
14296 tree inner_type = TREE_TYPE (op0);
14297 tree outer_type = type;
14299 if (TREE_CODE (outer_type) == REAL_TYPE)
14301 if (TREE_CODE (inner_type) == REAL_TYPE)
14302 return tree_expr_nonnegative_warnv_p (op0,
14303 strict_overflow_p);
14304 if (TREE_CODE (inner_type) == INTEGER_TYPE)
14306 if (TYPE_UNSIGNED (inner_type))
14308 return tree_expr_nonnegative_warnv_p (op0,
14309 strict_overflow_p);
14312 else if (TREE_CODE (outer_type) == INTEGER_TYPE)
14314 if (TREE_CODE (inner_type) == REAL_TYPE)
14315 return tree_expr_nonnegative_warnv_p (op0,
14316 strict_overflow_p);
14317 if (TREE_CODE (inner_type) == INTEGER_TYPE)
14318 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
14319 && TYPE_UNSIGNED (inner_type);
14325 return tree_simple_nonnegative_warnv_p (code, type);
14328 /* We don't know sign of `t', so be conservative and return false. */
14332 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
14333 value is based on the assumption that signed overflow is undefined,
14334 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14335 *STRICT_OVERFLOW_P. */
14338 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
14339 tree op1, bool *strict_overflow_p)
14341 if (TYPE_UNSIGNED (type))
14346 case POINTER_PLUS_EXPR:
14348 if (FLOAT_TYPE_P (type))
14349 return (tree_expr_nonnegative_warnv_p (op0,
14351 && tree_expr_nonnegative_warnv_p (op1,
14352 strict_overflow_p));
14354 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
14355 both unsigned and at least 2 bits shorter than the result. */
14356 if (TREE_CODE (type) == INTEGER_TYPE
14357 && TREE_CODE (op0) == NOP_EXPR
14358 && TREE_CODE (op1) == NOP_EXPR)
14360 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14361 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14362 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14363 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14365 unsigned int prec = MAX (TYPE_PRECISION (inner1),
14366 TYPE_PRECISION (inner2)) + 1;
14367 return prec < TYPE_PRECISION (type);
14373 if (FLOAT_TYPE_P (type))
14375 /* x * x for floating point x is always non-negative. */
14376 if (operand_equal_p (op0, op1, 0))
14378 return (tree_expr_nonnegative_warnv_p (op0,
14380 && tree_expr_nonnegative_warnv_p (op1,
14381 strict_overflow_p));
14384 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14385 both unsigned and their total bits is shorter than the result. */
14386 if (TREE_CODE (type) == INTEGER_TYPE
14387 && (TREE_CODE (op0) == NOP_EXPR || TREE_CODE (op0) == INTEGER_CST)
14388 && (TREE_CODE (op1) == NOP_EXPR || TREE_CODE (op1) == INTEGER_CST))
14390 tree inner0 = (TREE_CODE (op0) == NOP_EXPR)
14391 ? TREE_TYPE (TREE_OPERAND (op0, 0))
14393 tree inner1 = (TREE_CODE (op1) == NOP_EXPR)
14394 ? TREE_TYPE (TREE_OPERAND (op1, 0))
14397 bool unsigned0 = TYPE_UNSIGNED (inner0);
14398 bool unsigned1 = TYPE_UNSIGNED (inner1);
14400 if (TREE_CODE (op0) == INTEGER_CST)
14401 unsigned0 = unsigned0 || tree_int_cst_sgn (op0) >= 0;
14403 if (TREE_CODE (op1) == INTEGER_CST)
14404 unsigned1 = unsigned1 || tree_int_cst_sgn (op1) >= 0;
14406 if (TREE_CODE (inner0) == INTEGER_TYPE && unsigned0
14407 && TREE_CODE (inner1) == INTEGER_TYPE && unsigned1)
14409 unsigned int precision0 = (TREE_CODE (op0) == INTEGER_CST)
14410 ? tree_int_cst_min_precision (op0, /*unsignedp=*/true)
14411 : TYPE_PRECISION (inner0);
14413 unsigned int precision1 = (TREE_CODE (op1) == INTEGER_CST)
14414 ? tree_int_cst_min_precision (op1, /*unsignedp=*/true)
14415 : TYPE_PRECISION (inner1);
14417 return precision0 + precision1 < TYPE_PRECISION (type);
14424 return (tree_expr_nonnegative_warnv_p (op0,
14426 || tree_expr_nonnegative_warnv_p (op1,
14427 strict_overflow_p));
14433 case TRUNC_DIV_EXPR:
14434 case CEIL_DIV_EXPR:
14435 case FLOOR_DIV_EXPR:
14436 case ROUND_DIV_EXPR:
14437 return (tree_expr_nonnegative_warnv_p (op0,
14439 && tree_expr_nonnegative_warnv_p (op1,
14440 strict_overflow_p));
14442 case TRUNC_MOD_EXPR:
14443 case CEIL_MOD_EXPR:
14444 case FLOOR_MOD_EXPR:
14445 case ROUND_MOD_EXPR:
14446 return tree_expr_nonnegative_warnv_p (op0,
14447 strict_overflow_p);
14449 return tree_simple_nonnegative_warnv_p (code, type);
14452 /* We don't know sign of `t', so be conservative and return false. */
14456 /* Return true if T is known to be non-negative. If the return
14457 value is based on the assumption that signed overflow is undefined,
14458 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14459 *STRICT_OVERFLOW_P. */
14462 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14464 if (TYPE_UNSIGNED (TREE_TYPE (t)))
14467 switch (TREE_CODE (t))
14470 return tree_int_cst_sgn (t) >= 0;
14473 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14476 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14479 return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14481 && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
14482 strict_overflow_p));
14484 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14487 /* We don't know sign of `t', so be conservative and return false. */
14491 /* Return true if T is known to be non-negative. If the return
14492 value is based on the assumption that signed overflow is undefined,
14493 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14494 *STRICT_OVERFLOW_P. */
14497 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
14498 tree arg0, tree arg1, bool *strict_overflow_p)
14500 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
14501 switch (DECL_FUNCTION_CODE (fndecl))
14503 CASE_FLT_FN (BUILT_IN_ACOS):
14504 CASE_FLT_FN (BUILT_IN_ACOSH):
14505 CASE_FLT_FN (BUILT_IN_CABS):
14506 CASE_FLT_FN (BUILT_IN_COSH):
14507 CASE_FLT_FN (BUILT_IN_ERFC):
14508 CASE_FLT_FN (BUILT_IN_EXP):
14509 CASE_FLT_FN (BUILT_IN_EXP10):
14510 CASE_FLT_FN (BUILT_IN_EXP2):
14511 CASE_FLT_FN (BUILT_IN_FABS):
14512 CASE_FLT_FN (BUILT_IN_FDIM):
14513 CASE_FLT_FN (BUILT_IN_HYPOT):
14514 CASE_FLT_FN (BUILT_IN_POW10):
14515 CASE_INT_FN (BUILT_IN_FFS):
14516 CASE_INT_FN (BUILT_IN_PARITY):
14517 CASE_INT_FN (BUILT_IN_POPCOUNT):
14518 case BUILT_IN_BSWAP32:
14519 case BUILT_IN_BSWAP64:
14523 CASE_FLT_FN (BUILT_IN_SQRT):
14524 /* sqrt(-0.0) is -0.0. */
14525 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
14527 return tree_expr_nonnegative_warnv_p (arg0,
14528 strict_overflow_p);
14530 CASE_FLT_FN (BUILT_IN_ASINH):
14531 CASE_FLT_FN (BUILT_IN_ATAN):
14532 CASE_FLT_FN (BUILT_IN_ATANH):
14533 CASE_FLT_FN (BUILT_IN_CBRT):
14534 CASE_FLT_FN (BUILT_IN_CEIL):
14535 CASE_FLT_FN (BUILT_IN_ERF):
14536 CASE_FLT_FN (BUILT_IN_EXPM1):
14537 CASE_FLT_FN (BUILT_IN_FLOOR):
14538 CASE_FLT_FN (BUILT_IN_FMOD):
14539 CASE_FLT_FN (BUILT_IN_FREXP):
14540 CASE_FLT_FN (BUILT_IN_LCEIL):
14541 CASE_FLT_FN (BUILT_IN_LDEXP):
14542 CASE_FLT_FN (BUILT_IN_LFLOOR):
14543 CASE_FLT_FN (BUILT_IN_LLCEIL):
14544 CASE_FLT_FN (BUILT_IN_LLFLOOR):
14545 CASE_FLT_FN (BUILT_IN_LLRINT):
14546 CASE_FLT_FN (BUILT_IN_LLROUND):
14547 CASE_FLT_FN (BUILT_IN_LRINT):
14548 CASE_FLT_FN (BUILT_IN_LROUND):
14549 CASE_FLT_FN (BUILT_IN_MODF):
14550 CASE_FLT_FN (BUILT_IN_NEARBYINT):
14551 CASE_FLT_FN (BUILT_IN_RINT):
14552 CASE_FLT_FN (BUILT_IN_ROUND):
14553 CASE_FLT_FN (BUILT_IN_SCALB):
14554 CASE_FLT_FN (BUILT_IN_SCALBLN):
14555 CASE_FLT_FN (BUILT_IN_SCALBN):
14556 CASE_FLT_FN (BUILT_IN_SIGNBIT):
14557 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
14558 CASE_FLT_FN (BUILT_IN_SINH):
14559 CASE_FLT_FN (BUILT_IN_TANH):
14560 CASE_FLT_FN (BUILT_IN_TRUNC):
14561 /* True if the 1st argument is nonnegative. */
14562 return tree_expr_nonnegative_warnv_p (arg0,
14563 strict_overflow_p);
14565 CASE_FLT_FN (BUILT_IN_FMAX):
14566 /* True if the 1st OR 2nd arguments are nonnegative. */
14567 return (tree_expr_nonnegative_warnv_p (arg0,
14569 || (tree_expr_nonnegative_warnv_p (arg1,
14570 strict_overflow_p)));
14572 CASE_FLT_FN (BUILT_IN_FMIN):
14573 /* True if the 1st AND 2nd arguments are nonnegative. */
14574 return (tree_expr_nonnegative_warnv_p (arg0,
14576 && (tree_expr_nonnegative_warnv_p (arg1,
14577 strict_overflow_p)));
14579 CASE_FLT_FN (BUILT_IN_COPYSIGN):
14580 /* True if the 2nd argument is nonnegative. */
14581 return tree_expr_nonnegative_warnv_p (arg1,
14582 strict_overflow_p);
14584 CASE_FLT_FN (BUILT_IN_POWI):
14585 /* True if the 1st argument is nonnegative or the second
14586 argument is an even integer. */
14587 if (TREE_CODE (arg1) == INTEGER_CST
14588 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14590 return tree_expr_nonnegative_warnv_p (arg0,
14591 strict_overflow_p);
14593 CASE_FLT_FN (BUILT_IN_POW):
14594 /* True if the 1st argument is nonnegative or the second
14595 argument is an even integer valued real. */
14596 if (TREE_CODE (arg1) == REAL_CST)
14601 c = TREE_REAL_CST (arg1);
14602 n = real_to_integer (&c);
14605 REAL_VALUE_TYPE cint;
14606 real_from_integer (&cint, VOIDmode, n,
14607 n < 0 ? -1 : 0, 0);
14608 if (real_identical (&c, &cint))
14612 return tree_expr_nonnegative_warnv_p (arg0,
14613 strict_overflow_p);
14618 return tree_simple_nonnegative_warnv_p (CALL_EXPR,
14622 /* Return true if T is known to be non-negative. If the return
14623 value is based on the assumption that signed overflow is undefined,
14624 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14625 *STRICT_OVERFLOW_P. */
14628 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14630 enum tree_code code = TREE_CODE (t);
14631 if (TYPE_UNSIGNED (TREE_TYPE (t)))
14638 tree temp = TARGET_EXPR_SLOT (t);
14639 t = TARGET_EXPR_INITIAL (t);
14641 /* If the initializer is non-void, then it's a normal expression
14642 that will be assigned to the slot. */
14643 if (!VOID_TYPE_P (t))
14644 return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
14646 /* Otherwise, the initializer sets the slot in some way. One common
14647 way is an assignment statement at the end of the initializer. */
14650 if (TREE_CODE (t) == BIND_EXPR)
14651 t = expr_last (BIND_EXPR_BODY (t));
14652 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
14653 || TREE_CODE (t) == TRY_CATCH_EXPR)
14654 t = expr_last (TREE_OPERAND (t, 0));
14655 else if (TREE_CODE (t) == STATEMENT_LIST)
14660 if (TREE_CODE (t) == MODIFY_EXPR
14661 && TREE_OPERAND (t, 0) == temp)
14662 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14663 strict_overflow_p);
14670 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
14671 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
14673 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
14674 get_callee_fndecl (t),
14677 strict_overflow_p);
14679 case COMPOUND_EXPR:
14681 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14682 strict_overflow_p);
14684 return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
14685 strict_overflow_p);
14687 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
14688 strict_overflow_p);
14691 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14695 /* We don't know sign of `t', so be conservative and return false. */
14699 /* Return true if T is known to be non-negative. If the return
14700 value is based on the assumption that signed overflow is undefined,
14701 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14702 *STRICT_OVERFLOW_P. */
14705 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14707 enum tree_code code;
14708 if (t == error_mark_node)
14711 code = TREE_CODE (t);
14712 switch (TREE_CODE_CLASS (code))
14715 case tcc_comparison:
14716 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14718 TREE_OPERAND (t, 0),
14719 TREE_OPERAND (t, 1),
14720 strict_overflow_p);
14723 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14725 TREE_OPERAND (t, 0),
14726 strict_overflow_p);
14729 case tcc_declaration:
14730 case tcc_reference:
14731 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14739 case TRUTH_AND_EXPR:
14740 case TRUTH_OR_EXPR:
14741 case TRUTH_XOR_EXPR:
14742 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14744 TREE_OPERAND (t, 0),
14745 TREE_OPERAND (t, 1),
14746 strict_overflow_p);
14747 case TRUTH_NOT_EXPR:
14748 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14750 TREE_OPERAND (t, 0),
14751 strict_overflow_p);
14758 case WITH_SIZE_EXPR:
14762 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14765 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
14769 /* Return true if `t' is known to be non-negative. Handle warnings
14770 about undefined signed overflow. */
14773 tree_expr_nonnegative_p (tree t)
14775 bool ret, strict_overflow_p;
14777 strict_overflow_p = false;
14778 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
14779 if (strict_overflow_p)
14780 fold_overflow_warning (("assuming signed overflow does not occur when "
14781 "determining that expression is always "
14783 WARN_STRICT_OVERFLOW_MISC);
14788 /* Return true when (CODE OP0) is an address and is known to be nonzero.
14789 For floating point we further ensure that T is not denormal.
14790 Similar logic is present in nonzero_address in rtlanal.h.
14792 If the return value is based on the assumption that signed overflow
14793 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14794 change *STRICT_OVERFLOW_P. */
14797 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
14798 bool *strict_overflow_p)
14803 return tree_expr_nonzero_warnv_p (op0,
14804 strict_overflow_p);
14808 tree inner_type = TREE_TYPE (op0);
14809 tree outer_type = type;
14811 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
14812 && tree_expr_nonzero_warnv_p (op0,
14813 strict_overflow_p));
14817 case NON_LVALUE_EXPR:
14818 return tree_expr_nonzero_warnv_p (op0,
14819 strict_overflow_p);
14828 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
14829 For floating point we further ensure that T is not denormal.
14830 Similar logic is present in nonzero_address in rtlanal.h.
14832 If the return value is based on the assumption that signed overflow
14833 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14834 change *STRICT_OVERFLOW_P. */
14837 tree_binary_nonzero_warnv_p (enum tree_code code,
14840 tree op1, bool *strict_overflow_p)
14842 bool sub_strict_overflow_p;
14845 case POINTER_PLUS_EXPR:
14847 if (TYPE_OVERFLOW_UNDEFINED (type))
14849 /* With the presence of negative values it is hard
14850 to say something. */
14851 sub_strict_overflow_p = false;
14852 if (!tree_expr_nonnegative_warnv_p (op0,
14853 &sub_strict_overflow_p)
14854 || !tree_expr_nonnegative_warnv_p (op1,
14855 &sub_strict_overflow_p))
14857 /* One of operands must be positive and the other non-negative. */
14858 /* We don't set *STRICT_OVERFLOW_P here: even if this value
14859 overflows, on a twos-complement machine the sum of two
14860 nonnegative numbers can never be zero. */
14861 return (tree_expr_nonzero_warnv_p (op0,
14863 || tree_expr_nonzero_warnv_p (op1,
14864 strict_overflow_p));
14869 if (TYPE_OVERFLOW_UNDEFINED (type))
14871 if (tree_expr_nonzero_warnv_p (op0,
14873 && tree_expr_nonzero_warnv_p (op1,
14874 strict_overflow_p))
14876 *strict_overflow_p = true;
14883 sub_strict_overflow_p = false;
14884 if (tree_expr_nonzero_warnv_p (op0,
14885 &sub_strict_overflow_p)
14886 && tree_expr_nonzero_warnv_p (op1,
14887 &sub_strict_overflow_p))
14889 if (sub_strict_overflow_p)
14890 *strict_overflow_p = true;
14895 sub_strict_overflow_p = false;
14896 if (tree_expr_nonzero_warnv_p (op0,
14897 &sub_strict_overflow_p))
14899 if (sub_strict_overflow_p)
14900 *strict_overflow_p = true;
14902 /* When both operands are nonzero, then MAX must be too. */
14903 if (tree_expr_nonzero_warnv_p (op1,
14904 strict_overflow_p))
14907 /* MAX where operand 0 is positive is positive. */
14908 return tree_expr_nonnegative_warnv_p (op0,
14909 strict_overflow_p);
14911 /* MAX where operand 1 is positive is positive. */
14912 else if (tree_expr_nonzero_warnv_p (op1,
14913 &sub_strict_overflow_p)
14914 && tree_expr_nonnegative_warnv_p (op1,
14915 &sub_strict_overflow_p))
14917 if (sub_strict_overflow_p)
14918 *strict_overflow_p = true;
14924 return (tree_expr_nonzero_warnv_p (op1,
14926 || tree_expr_nonzero_warnv_p (op0,
14927 strict_overflow_p));
14936 /* Return true when T is an address and is known to be nonzero.
14937 For floating point we further ensure that T is not denormal.
14938 Similar logic is present in nonzero_address in rtlanal.h.
14940 If the return value is based on the assumption that signed overflow
14941 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14942 change *STRICT_OVERFLOW_P. */
14945 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
14947 bool sub_strict_overflow_p;
14948 switch (TREE_CODE (t))
14951 return !integer_zerop (t);
14955 tree base = get_base_address (TREE_OPERAND (t, 0));
14960 /* Weak declarations may link to NULL. */
14961 if (VAR_OR_FUNCTION_DECL_P (base))
14962 return !DECL_WEAK (base);
14964 /* Constants are never weak. */
14965 if (CONSTANT_CLASS_P (base))
14972 sub_strict_overflow_p = false;
14973 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
14974 &sub_strict_overflow_p)
14975 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
14976 &sub_strict_overflow_p))
14978 if (sub_strict_overflow_p)
14979 *strict_overflow_p = true;
14990 /* Return true when T is an address and is known to be nonzero.
14991 For floating point we further ensure that T is not denormal.
14992 Similar logic is present in nonzero_address in rtlanal.h.
14994 If the return value is based on the assumption that signed overflow
14995 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14996 change *STRICT_OVERFLOW_P. */
14999 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
15001 tree type = TREE_TYPE (t);
15002 enum tree_code code;
15004 /* Doing something useful for floating point would need more work. */
15005 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
15008 code = TREE_CODE (t);
15009 switch (TREE_CODE_CLASS (code))
15012 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15013 strict_overflow_p);
15015 case tcc_comparison:
15016 return tree_binary_nonzero_warnv_p (code, type,
15017 TREE_OPERAND (t, 0),
15018 TREE_OPERAND (t, 1),
15019 strict_overflow_p);
15021 case tcc_declaration:
15022 case tcc_reference:
15023 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15031 case TRUTH_NOT_EXPR:
15032 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
15033 strict_overflow_p);
15035 case TRUTH_AND_EXPR:
15036 case TRUTH_OR_EXPR:
15037 case TRUTH_XOR_EXPR:
15038 return tree_binary_nonzero_warnv_p (code, type,
15039 TREE_OPERAND (t, 0),
15040 TREE_OPERAND (t, 1),
15041 strict_overflow_p);
15048 case WITH_SIZE_EXPR:
15052 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
15054 case COMPOUND_EXPR:
15057 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
15058 strict_overflow_p);
15061 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
15062 strict_overflow_p);
15065 return alloca_call_p (t);
15073 /* Return true when T is an address and is known to be nonzero.
15074 Handle warnings about undefined signed overflow. */
15077 tree_expr_nonzero_p (tree t)
15079 bool ret, strict_overflow_p;
15081 strict_overflow_p = false;
15082 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
15083 if (strict_overflow_p)
15084 fold_overflow_warning (("assuming signed overflow does not occur when "
15085 "determining that expression is always "
15087 WARN_STRICT_OVERFLOW_MISC);
15091 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
15092 attempt to fold the expression to a constant without modifying TYPE,
15095 If the expression could be simplified to a constant, then return
15096 the constant. If the expression would not be simplified to a
15097 constant, then return NULL_TREE. */
15100 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
15102 tree tem = fold_binary (code, type, op0, op1);
15103 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15106 /* Given the components of a unary expression CODE, TYPE and OP0,
15107 attempt to fold the expression to a constant without modifying
15110 If the expression could be simplified to a constant, then return
15111 the constant. If the expression would not be simplified to a
15112 constant, then return NULL_TREE. */
15115 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
15117 tree tem = fold_unary (code, type, op0);
15118 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
15121 /* If EXP represents referencing an element in a constant string
15122 (either via pointer arithmetic or array indexing), return the
15123 tree representing the value accessed, otherwise return NULL. */
15126 fold_read_from_constant_string (tree exp)
15128 if ((TREE_CODE (exp) == INDIRECT_REF
15129 || TREE_CODE (exp) == ARRAY_REF)
15130 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
15132 tree exp1 = TREE_OPERAND (exp, 0);
15136 if (TREE_CODE (exp) == INDIRECT_REF)
15137 string = string_constant (exp1, &index);
15140 tree low_bound = array_ref_low_bound (exp);
15141 index = fold_convert (sizetype, TREE_OPERAND (exp, 1));
15143 /* Optimize the special-case of a zero lower bound.
15145 We convert the low_bound to sizetype to avoid some problems
15146 with constant folding. (E.g. suppose the lower bound is 1,
15147 and its mode is QI. Without the conversion,l (ARRAY
15148 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
15149 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
15150 if (! integer_zerop (low_bound))
15151 index = size_diffop (index, fold_convert (sizetype, low_bound));
15157 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
15158 && TREE_CODE (string) == STRING_CST
15159 && TREE_CODE (index) == INTEGER_CST
15160 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
15161 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
15163 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
15164 return build_int_cst_type (TREE_TYPE (exp),
15165 (TREE_STRING_POINTER (string)
15166 [TREE_INT_CST_LOW (index)]));
15171 /* Return the tree for neg (ARG0) when ARG0 is known to be either
15172 an integer constant, real, or fixed-point constant.
15174 TYPE is the type of the result. */
15177 fold_negate_const (tree arg0, tree type)
15179 tree t = NULL_TREE;
15181 switch (TREE_CODE (arg0))
15185 unsigned HOST_WIDE_INT low;
15186 HOST_WIDE_INT high;
15187 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
15188 TREE_INT_CST_HIGH (arg0),
15190 t = force_fit_type_double (type, low, high, 1,
15191 (overflow | TREE_OVERFLOW (arg0))
15192 && !TYPE_UNSIGNED (type));
15197 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
15202 FIXED_VALUE_TYPE f;
15203 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
15204 &(TREE_FIXED_CST (arg0)), NULL,
15205 TYPE_SATURATING (type));
15206 t = build_fixed (type, f);
15207 /* Propagate overflow flags. */
15208 if (overflow_p | TREE_OVERFLOW (arg0))
15209 TREE_OVERFLOW (t) = 1;
15214 gcc_unreachable ();
15220 /* Return the tree for abs (ARG0) when ARG0 is known to be either
15221 an integer constant or real constant.
15223 TYPE is the type of the result. */
15226 fold_abs_const (tree arg0, tree type)
15228 tree t = NULL_TREE;
15230 switch (TREE_CODE (arg0))
15233 /* If the value is unsigned, then the absolute value is
15234 the same as the ordinary value. */
15235 if (TYPE_UNSIGNED (type))
15237 /* Similarly, if the value is non-negative. */
15238 else if (INT_CST_LT (integer_minus_one_node, arg0))
15240 /* If the value is negative, then the absolute value is
15244 unsigned HOST_WIDE_INT low;
15245 HOST_WIDE_INT high;
15246 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
15247 TREE_INT_CST_HIGH (arg0),
15249 t = force_fit_type_double (type, low, high, -1,
15250 overflow | TREE_OVERFLOW (arg0));
15255 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
15256 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
15262 gcc_unreachable ();
15268 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
15269 constant. TYPE is the type of the result. */
15272 fold_not_const (tree arg0, tree type)
15274 tree t = NULL_TREE;
15276 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
15278 t = force_fit_type_double (type, ~TREE_INT_CST_LOW (arg0),
15279 ~TREE_INT_CST_HIGH (arg0), 0,
15280 TREE_OVERFLOW (arg0));
15285 /* Given CODE, a relational operator, the target type, TYPE and two
15286 constant operands OP0 and OP1, return the result of the
15287 relational operation. If the result is not a compile time
15288 constant, then return NULL_TREE. */
15291 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
15293 int result, invert;
15295 /* From here on, the only cases we handle are when the result is
15296 known to be a constant. */
15298 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
15300 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
15301 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
15303 /* Handle the cases where either operand is a NaN. */
15304 if (real_isnan (c0) || real_isnan (c1))
15314 case UNORDERED_EXPR:
15328 if (flag_trapping_math)
15334 gcc_unreachable ();
15337 return constant_boolean_node (result, type);
15340 return constant_boolean_node (real_compare (code, c0, c1), type);
15343 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
15345 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
15346 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
15347 return constant_boolean_node (fixed_compare (code, c0, c1), type);
15350 /* Handle equality/inequality of complex constants. */
15351 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
15353 tree rcond = fold_relational_const (code, type,
15354 TREE_REALPART (op0),
15355 TREE_REALPART (op1));
15356 tree icond = fold_relational_const (code, type,
15357 TREE_IMAGPART (op0),
15358 TREE_IMAGPART (op1));
15359 if (code == EQ_EXPR)
15360 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
15361 else if (code == NE_EXPR)
15362 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
15367 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
15369 To compute GT, swap the arguments and do LT.
15370 To compute GE, do LT and invert the result.
15371 To compute LE, swap the arguments, do LT and invert the result.
15372 To compute NE, do EQ and invert the result.
15374 Therefore, the code below must handle only EQ and LT. */
15376 if (code == LE_EXPR || code == GT_EXPR)
15381 code = swap_tree_comparison (code);
15384 /* Note that it is safe to invert for real values here because we
15385 have already handled the one case that it matters. */
15388 if (code == NE_EXPR || code == GE_EXPR)
15391 code = invert_tree_comparison (code, false);
15394 /* Compute a result for LT or EQ if args permit;
15395 Otherwise return T. */
15396 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15398 if (code == EQ_EXPR)
15399 result = tree_int_cst_equal (op0, op1);
15400 else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
15401 result = INT_CST_LT_UNSIGNED (op0, op1);
15403 result = INT_CST_LT (op0, op1);
15410 return constant_boolean_node (result, type);
15413 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15414 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
15418 fold_build_cleanup_point_expr (tree type, tree expr)
15420 /* If the expression does not have side effects then we don't have to wrap
15421 it with a cleanup point expression. */
15422 if (!TREE_SIDE_EFFECTS (expr))
15425 /* If the expression is a return, check to see if the expression inside the
15426 return has no side effects or the right hand side of the modify expression
15427 inside the return. If either don't have side effects set we don't need to
15428 wrap the expression in a cleanup point expression. Note we don't check the
15429 left hand side of the modify because it should always be a return decl. */
15430 if (TREE_CODE (expr) == RETURN_EXPR)
15432 tree op = TREE_OPERAND (expr, 0);
15433 if (!op || !TREE_SIDE_EFFECTS (op))
15435 op = TREE_OPERAND (op, 1);
15436 if (!TREE_SIDE_EFFECTS (op))
15440 return build1 (CLEANUP_POINT_EXPR, type, expr);
15443 /* Given a pointer value OP0 and a type TYPE, return a simplified version
15444 of an indirection through OP0, or NULL_TREE if no simplification is
15448 fold_indirect_ref_1 (tree type, tree op0)
15454 subtype = TREE_TYPE (sub);
15455 if (!POINTER_TYPE_P (subtype))
15458 if (TREE_CODE (sub) == ADDR_EXPR)
15460 tree op = TREE_OPERAND (sub, 0);
15461 tree optype = TREE_TYPE (op);
15462 /* *&CONST_DECL -> to the value of the const decl. */
15463 if (TREE_CODE (op) == CONST_DECL)
15464 return DECL_INITIAL (op);
15465 /* *&p => p; make sure to handle *&"str"[cst] here. */
15466 if (type == optype)
15468 tree fop = fold_read_from_constant_string (op);
15474 /* *(foo *)&fooarray => fooarray[0] */
15475 else if (TREE_CODE (optype) == ARRAY_TYPE
15476 && type == TREE_TYPE (optype))
15478 tree type_domain = TYPE_DOMAIN (optype);
15479 tree min_val = size_zero_node;
15480 if (type_domain && TYPE_MIN_VALUE (type_domain))
15481 min_val = TYPE_MIN_VALUE (type_domain);
15482 return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
15484 /* *(foo *)&complexfoo => __real__ complexfoo */
15485 else if (TREE_CODE (optype) == COMPLEX_TYPE
15486 && type == TREE_TYPE (optype))
15487 return fold_build1 (REALPART_EXPR, type, op);
15488 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15489 else if (TREE_CODE (optype) == VECTOR_TYPE
15490 && type == TREE_TYPE (optype))
15492 tree part_width = TYPE_SIZE (type);
15493 tree index = bitsize_int (0);
15494 return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
15498 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15499 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15500 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15502 tree op00 = TREE_OPERAND (sub, 0);
15503 tree op01 = TREE_OPERAND (sub, 1);
15507 op00type = TREE_TYPE (op00);
15508 if (TREE_CODE (op00) == ADDR_EXPR
15509 && TREE_CODE (TREE_TYPE (op00type)) == VECTOR_TYPE
15510 && type == TREE_TYPE (TREE_TYPE (op00type)))
15512 HOST_WIDE_INT offset = tree_low_cst (op01, 0);
15513 tree part_width = TYPE_SIZE (type);
15514 unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT;
15515 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
15516 tree index = bitsize_int (indexi);
15518 if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (op00type)))
15519 return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (op00, 0),
15520 part_width, index);
15526 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
15527 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15528 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15530 tree op00 = TREE_OPERAND (sub, 0);
15531 tree op01 = TREE_OPERAND (sub, 1);
15535 op00type = TREE_TYPE (op00);
15536 if (TREE_CODE (op00) == ADDR_EXPR
15537 && TREE_CODE (TREE_TYPE (op00type)) == COMPLEX_TYPE
15538 && type == TREE_TYPE (TREE_TYPE (op00type)))
15540 tree size = TYPE_SIZE_UNIT (type);
15541 if (tree_int_cst_equal (size, op01))
15542 return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (op00, 0));
15546 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
15547 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
15548 && type == TREE_TYPE (TREE_TYPE (subtype)))
15551 tree min_val = size_zero_node;
15552 sub = build_fold_indirect_ref (sub);
15553 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
15554 if (type_domain && TYPE_MIN_VALUE (type_domain))
15555 min_val = TYPE_MIN_VALUE (type_domain);
15556 return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
15562 /* Builds an expression for an indirection through T, simplifying some
15566 build_fold_indirect_ref (tree t)
15568 tree type = TREE_TYPE (TREE_TYPE (t));
15569 tree sub = fold_indirect_ref_1 (type, t);
15574 return build1 (INDIRECT_REF, type, t);
15577 /* Given an INDIRECT_REF T, return either T or a simplified version. */
15580 fold_indirect_ref (tree t)
15582 tree sub = fold_indirect_ref_1 (TREE_TYPE (t), TREE_OPERAND (t, 0));
15590 /* Strip non-trapping, non-side-effecting tree nodes from an expression
15591 whose result is ignored. The type of the returned tree need not be
15592 the same as the original expression. */
15595 fold_ignored_result (tree t)
15597 if (!TREE_SIDE_EFFECTS (t))
15598 return integer_zero_node;
15601 switch (TREE_CODE_CLASS (TREE_CODE (t)))
15604 t = TREE_OPERAND (t, 0);
15608 case tcc_comparison:
15609 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15610 t = TREE_OPERAND (t, 0);
15611 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
15612 t = TREE_OPERAND (t, 1);
15617 case tcc_expression:
15618 switch (TREE_CODE (t))
15620 case COMPOUND_EXPR:
15621 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15623 t = TREE_OPERAND (t, 0);
15627 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
15628 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
15630 t = TREE_OPERAND (t, 0);
15643 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
15644 This can only be applied to objects of a sizetype. */
15647 round_up (tree value, int divisor)
15649 tree div = NULL_TREE;
15651 gcc_assert (divisor > 0);
15655 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
15656 have to do anything. Only do this when we are not given a const,
15657 because in that case, this check is more expensive than just
15659 if (TREE_CODE (value) != INTEGER_CST)
15661 div = build_int_cst (TREE_TYPE (value), divisor);
15663 if (multiple_of_p (TREE_TYPE (value), value, div))
15667 /* If divisor is a power of two, simplify this to bit manipulation. */
15668 if (divisor == (divisor & -divisor))
15670 if (TREE_CODE (value) == INTEGER_CST)
15672 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (value);
15673 unsigned HOST_WIDE_INT high;
15676 if ((low & (divisor - 1)) == 0)
15679 overflow_p = TREE_OVERFLOW (value);
15680 high = TREE_INT_CST_HIGH (value);
15681 low &= ~(divisor - 1);
15690 return force_fit_type_double (TREE_TYPE (value), low, high,
15697 t = build_int_cst (TREE_TYPE (value), divisor - 1);
15698 value = size_binop (PLUS_EXPR, value, t);
15699 t = build_int_cst (TREE_TYPE (value), -divisor);
15700 value = size_binop (BIT_AND_EXPR, value, t);
15706 div = build_int_cst (TREE_TYPE (value), divisor);
15707 value = size_binop (CEIL_DIV_EXPR, value, div);
15708 value = size_binop (MULT_EXPR, value, div);
15714 /* Likewise, but round down. */
15717 round_down (tree value, int divisor)
15719 tree div = NULL_TREE;
15721 gcc_assert (divisor > 0);
15725 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
15726 have to do anything. Only do this when we are not given a const,
15727 because in that case, this check is more expensive than just
15729 if (TREE_CODE (value) != INTEGER_CST)
15731 div = build_int_cst (TREE_TYPE (value), divisor);
15733 if (multiple_of_p (TREE_TYPE (value), value, div))
15737 /* If divisor is a power of two, simplify this to bit manipulation. */
15738 if (divisor == (divisor & -divisor))
15742 t = build_int_cst (TREE_TYPE (value), -divisor);
15743 value = size_binop (BIT_AND_EXPR, value, t);
15748 div = build_int_cst (TREE_TYPE (value), divisor);
15749 value = size_binop (FLOOR_DIV_EXPR, value, div);
15750 value = size_binop (MULT_EXPR, value, div);
15756 /* Returns the pointer to the base of the object addressed by EXP and
15757 extracts the information about the offset of the access, storing it
15758 to PBITPOS and POFFSET. */
15761 split_address_to_core_and_offset (tree exp,
15762 HOST_WIDE_INT *pbitpos, tree *poffset)
15765 enum machine_mode mode;
15766 int unsignedp, volatilep;
15767 HOST_WIDE_INT bitsize;
15769 if (TREE_CODE (exp) == ADDR_EXPR)
15771 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
15772 poffset, &mode, &unsignedp, &volatilep,
15774 core = build_fold_addr_expr (core);
15780 *poffset = NULL_TREE;
15786 /* Returns true if addresses of E1 and E2 differ by a constant, false
15787 otherwise. If they do, E1 - E2 is stored in *DIFF. */
15790 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
15793 HOST_WIDE_INT bitpos1, bitpos2;
15794 tree toffset1, toffset2, tdiff, type;
15796 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
15797 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
15799 if (bitpos1 % BITS_PER_UNIT != 0
15800 || bitpos2 % BITS_PER_UNIT != 0
15801 || !operand_equal_p (core1, core2, 0))
15804 if (toffset1 && toffset2)
15806 type = TREE_TYPE (toffset1);
15807 if (type != TREE_TYPE (toffset2))
15808 toffset2 = fold_convert (type, toffset2);
15810 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
15811 if (!cst_and_fits_in_hwi (tdiff))
15814 *diff = int_cst_value (tdiff);
15816 else if (toffset1 || toffset2)
15818 /* If only one of the offsets is non-constant, the difference cannot
15825 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
15829 /* Simplify the floating point expression EXP when the sign of the
15830 result is not significant. Return NULL_TREE if no simplification
15834 fold_strip_sign_ops (tree exp)
15838 switch (TREE_CODE (exp))
15842 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15843 return arg0 ? arg0 : TREE_OPERAND (exp, 0);
15847 if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
15849 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15850 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15851 if (arg0 != NULL_TREE || arg1 != NULL_TREE)
15852 return fold_build2 (TREE_CODE (exp), TREE_TYPE (exp),
15853 arg0 ? arg0 : TREE_OPERAND (exp, 0),
15854 arg1 ? arg1 : TREE_OPERAND (exp, 1));
15857 case COMPOUND_EXPR:
15858 arg0 = TREE_OPERAND (exp, 0);
15859 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15861 return fold_build2 (COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
15865 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15866 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
15868 return fold_build3 (COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
15869 arg0 ? arg0 : TREE_OPERAND (exp, 1),
15870 arg1 ? arg1 : TREE_OPERAND (exp, 2));
15875 const enum built_in_function fcode = builtin_mathfn_code (exp);
15878 CASE_FLT_FN (BUILT_IN_COPYSIGN):
15879 /* Strip copysign function call, return the 1st argument. */
15880 arg0 = CALL_EXPR_ARG (exp, 0);
15881 arg1 = CALL_EXPR_ARG (exp, 1);
15882 return omit_one_operand (TREE_TYPE (exp), arg0, arg1);
15885 /* Strip sign ops from the argument of "odd" math functions. */
15886 if (negate_mathfn_p (fcode))
15888 arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
15890 return build_call_expr (get_callee_fndecl (exp), 1, arg0);