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
4 Free Software Foundation, Inc.
6 This file is part of GCC.
8 GCC is free software; you can redistribute it and/or modify it under
9 the terms of the GNU General Public License as published by the Free
10 Software Foundation; either version 3, or (at your option) any later
13 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
14 WARRANTY; without even the implied warranty of MERCHANTABILITY or
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3. If not see
20 <http://www.gnu.org/licenses/>. */
22 /*@@ This file should be rewritten to use an arbitrary precision
23 @@ representation for "struct tree_int_cst" and "struct tree_real_cst".
24 @@ Perhaps the routines could also be used for bc/dc, and made a lib.
25 @@ The routines that translate from the ap rep should
26 @@ warn if precision et. al. is lost.
27 @@ This would also make life easier when this technology is used
28 @@ for cross-compilers. */
30 /* The entry points in this file are fold, size_int_wide, size_binop
31 and force_fit_type_double.
33 fold takes a tree as argument and returns a simplified tree.
35 size_binop takes a tree code for an arithmetic operation
36 and two operands that are trees, and produces a tree for the
37 result, assuming the type comes from `sizetype'.
39 size_int takes an integer value, and creates a tree constant
40 with type from `sizetype'.
42 force_fit_type_double takes a constant, an overflowable flag and a
43 prior overflow indicator. It forces the value to fit the type and
46 Note: Since the folders get called on non-gimple code as well as
47 gimple code, we need to handle GIMPLE tuples as well as their
48 corresponding tree equivalents. */
52 #include "coretypes.h"
57 #include "fixed-value.h"
66 #include "langhooks.h"
70 /* Nonzero if we are folding constants inside an initializer; zero
72 int folding_initializer = 0;
74 /* The following constants represent a bit based encoding of GCC's
75 comparison operators. This encoding simplifies transformations
76 on relational comparison operators, such as AND and OR. */
77 enum comparison_code {
96 static void encode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT, HOST_WIDE_INT);
97 static void decode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT *, HOST_WIDE_INT *);
98 static bool negate_mathfn_p (enum built_in_function);
99 static bool negate_expr_p (tree);
100 static tree negate_expr (tree);
101 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
102 static tree associate_trees (tree, tree, enum tree_code, tree);
103 static tree const_binop (enum tree_code, tree, tree, int);
104 static enum comparison_code comparison_to_compcode (enum tree_code);
105 static enum tree_code compcode_to_comparison (enum comparison_code);
106 static tree combine_comparisons (enum tree_code, enum tree_code,
107 enum tree_code, tree, tree, tree);
108 static int truth_value_p (enum tree_code);
109 static int operand_equal_for_comparison_p (tree, tree, tree);
110 static int twoval_comparison_p (tree, tree *, tree *, int *);
111 static tree eval_subst (tree, tree, tree, tree, tree);
112 static tree pedantic_omit_one_operand (tree, tree, tree);
113 static tree distribute_bit_expr (enum tree_code, tree, tree, tree);
114 static tree decode_field_reference (tree, HOST_WIDE_INT *, HOST_WIDE_INT *,
115 enum machine_mode *, int *, int *,
117 static 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)) */
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))
1936 TREE_OVERFLOW (t) = 1;
1937 TREE_CONSTANT_OVERFLOW (t) = 1;
1939 else if (TREE_CONSTANT_OVERFLOW (arg1) | TREE_CONSTANT_OVERFLOW (arg2))
1940 TREE_CONSTANT_OVERFLOW (t) = 1;
1944 if (TREE_CODE (arg1) == COMPLEX_CST)
1946 tree type = TREE_TYPE (arg1);
1947 tree r1 = TREE_REALPART (arg1);
1948 tree i1 = TREE_IMAGPART (arg1);
1949 tree r2 = TREE_REALPART (arg2);
1950 tree i2 = TREE_IMAGPART (arg2);
1957 real = const_binop (code, r1, r2, notrunc);
1958 imag = const_binop (code, i1, i2, notrunc);
1962 real = const_binop (MINUS_EXPR,
1963 const_binop (MULT_EXPR, r1, r2, notrunc),
1964 const_binop (MULT_EXPR, i1, i2, notrunc),
1966 imag = const_binop (PLUS_EXPR,
1967 const_binop (MULT_EXPR, r1, i2, notrunc),
1968 const_binop (MULT_EXPR, i1, r2, notrunc),
1975 = const_binop (PLUS_EXPR,
1976 const_binop (MULT_EXPR, r2, r2, notrunc),
1977 const_binop (MULT_EXPR, i2, i2, notrunc),
1980 = const_binop (PLUS_EXPR,
1981 const_binop (MULT_EXPR, r1, r2, notrunc),
1982 const_binop (MULT_EXPR, i1, i2, notrunc),
1985 = const_binop (MINUS_EXPR,
1986 const_binop (MULT_EXPR, i1, r2, notrunc),
1987 const_binop (MULT_EXPR, r1, i2, notrunc),
1990 if (INTEGRAL_TYPE_P (TREE_TYPE (r1)))
1991 code = TRUNC_DIV_EXPR;
1993 real = const_binop (code, t1, magsquared, notrunc);
1994 imag = const_binop (code, t2, magsquared, notrunc);
2003 return build_complex (type, real, imag);
2009 /* Create a size type INT_CST node with NUMBER sign extended. KIND
2010 indicates which particular sizetype to create. */
2013 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
2015 return build_int_cst (sizetype_tab[(int) kind], number);
2018 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
2019 is a tree code. The type of the result is taken from the operands.
2020 Both must be equivalent integer types, ala int_binop_types_match_p.
2021 If the operands are constant, so is the result. */
2024 size_binop (enum tree_code code, tree arg0, tree arg1)
2026 tree type = TREE_TYPE (arg0);
2028 if (arg0 == error_mark_node || arg1 == error_mark_node)
2029 return error_mark_node;
2031 gcc_assert (int_binop_types_match_p (code, TREE_TYPE (arg0),
2034 /* Handle the special case of two integer constants faster. */
2035 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
2037 /* And some specific cases even faster than that. */
2038 if (code == PLUS_EXPR)
2040 if (integer_zerop (arg0) && !TREE_OVERFLOW (arg0))
2042 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2045 else if (code == MINUS_EXPR)
2047 if (integer_zerop (arg1) && !TREE_OVERFLOW (arg1))
2050 else if (code == MULT_EXPR)
2052 if (integer_onep (arg0) && !TREE_OVERFLOW (arg0))
2056 /* Handle general case of two integer constants. */
2057 return int_const_binop (code, arg0, arg1, 0);
2060 return fold_build2 (code, type, arg0, arg1);
2063 /* Given two values, either both of sizetype or both of bitsizetype,
2064 compute the difference between the two values. Return the value
2065 in signed type corresponding to the type of the operands. */
2068 size_diffop (tree arg0, tree arg1)
2070 tree type = TREE_TYPE (arg0);
2073 gcc_assert (int_binop_types_match_p (MINUS_EXPR, TREE_TYPE (arg0),
2076 /* If the type is already signed, just do the simple thing. */
2077 if (!TYPE_UNSIGNED (type))
2078 return size_binop (MINUS_EXPR, arg0, arg1);
2080 if (type == sizetype)
2082 else if (type == bitsizetype)
2083 ctype = sbitsizetype;
2085 ctype = signed_type_for (type);
2087 /* If either operand is not a constant, do the conversions to the signed
2088 type and subtract. The hardware will do the right thing with any
2089 overflow in the subtraction. */
2090 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
2091 return size_binop (MINUS_EXPR, fold_convert (ctype, arg0),
2092 fold_convert (ctype, arg1));
2094 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
2095 Otherwise, subtract the other way, convert to CTYPE (we know that can't
2096 overflow) and negate (which can't either). Special-case a result
2097 of zero while we're here. */
2098 if (tree_int_cst_equal (arg0, arg1))
2099 return build_int_cst (ctype, 0);
2100 else if (tree_int_cst_lt (arg1, arg0))
2101 return fold_convert (ctype, size_binop (MINUS_EXPR, arg0, arg1));
2103 return size_binop (MINUS_EXPR, build_int_cst (ctype, 0),
2104 fold_convert (ctype, size_binop (MINUS_EXPR,
2108 /* A subroutine of fold_convert_const handling conversions of an
2109 INTEGER_CST to another integer type. */
2112 fold_convert_const_int_from_int (tree type, const_tree arg1)
2116 /* Given an integer constant, make new constant with new type,
2117 appropriately sign-extended or truncated. */
2118 t = force_fit_type_double (type, TREE_INT_CST_LOW (arg1),
2119 TREE_INT_CST_HIGH (arg1),
2120 /* Don't set the overflow when
2121 converting from a pointer, */
2122 !POINTER_TYPE_P (TREE_TYPE (arg1))
2123 /* or to a sizetype with same signedness
2124 and the precision is unchanged.
2125 ??? sizetype is always sign-extended,
2126 but its signedness depends on the
2127 frontend. Thus we see spurious overflows
2128 here if we do not check this. */
2129 && !((TYPE_PRECISION (TREE_TYPE (arg1))
2130 == TYPE_PRECISION (type))
2131 && (TYPE_UNSIGNED (TREE_TYPE (arg1))
2132 == TYPE_UNSIGNED (type))
2133 && ((TREE_CODE (TREE_TYPE (arg1)) == INTEGER_TYPE
2134 && TYPE_IS_SIZETYPE (TREE_TYPE (arg1)))
2135 || (TREE_CODE (type) == INTEGER_TYPE
2136 && TYPE_IS_SIZETYPE (type)))),
2137 (TREE_INT_CST_HIGH (arg1) < 0
2138 && (TYPE_UNSIGNED (type)
2139 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2140 | TREE_OVERFLOW (arg1));
2145 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2146 to an integer type. */
2149 fold_convert_const_int_from_real (enum tree_code code, tree type, const_tree arg1)
2154 /* The following code implements the floating point to integer
2155 conversion rules required by the Java Language Specification,
2156 that IEEE NaNs are mapped to zero and values that overflow
2157 the target precision saturate, i.e. values greater than
2158 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
2159 are mapped to INT_MIN. These semantics are allowed by the
2160 C and C++ standards that simply state that the behavior of
2161 FP-to-integer conversion is unspecified upon overflow. */
2163 HOST_WIDE_INT high, low;
2165 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
2169 case FIX_TRUNC_EXPR:
2170 real_trunc (&r, VOIDmode, &x);
2177 /* If R is NaN, return zero and show we have an overflow. */
2178 if (REAL_VALUE_ISNAN (r))
2185 /* See if R is less than the lower bound or greater than the
2190 tree lt = TYPE_MIN_VALUE (type);
2191 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
2192 if (REAL_VALUES_LESS (r, l))
2195 high = TREE_INT_CST_HIGH (lt);
2196 low = TREE_INT_CST_LOW (lt);
2202 tree ut = TYPE_MAX_VALUE (type);
2205 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
2206 if (REAL_VALUES_LESS (u, r))
2209 high = TREE_INT_CST_HIGH (ut);
2210 low = TREE_INT_CST_LOW (ut);
2216 REAL_VALUE_TO_INT (&low, &high, r);
2218 t = force_fit_type_double (type, low, high, -1,
2219 overflow | TREE_OVERFLOW (arg1));
2223 /* A subroutine of fold_convert_const handling conversions of a
2224 FIXED_CST to an integer type. */
2227 fold_convert_const_int_from_fixed (tree type, const_tree arg1)
2230 double_int temp, temp_trunc;
2233 /* Right shift FIXED_CST to temp by fbit. */
2234 temp = TREE_FIXED_CST (arg1).data;
2235 mode = TREE_FIXED_CST (arg1).mode;
2236 if (GET_MODE_FBIT (mode) < 2 * HOST_BITS_PER_WIDE_INT)
2238 lshift_double (temp.low, temp.high,
2239 - GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2240 &temp.low, &temp.high, SIGNED_FIXED_POINT_MODE_P (mode));
2242 /* Left shift temp to temp_trunc by fbit. */
2243 lshift_double (temp.low, temp.high,
2244 GET_MODE_FBIT (mode), 2 * HOST_BITS_PER_WIDE_INT,
2245 &temp_trunc.low, &temp_trunc.high,
2246 SIGNED_FIXED_POINT_MODE_P (mode));
2253 temp_trunc.high = 0;
2256 /* If FIXED_CST is negative, we need to round the value toward 0.
2257 By checking if the fractional bits are not zero to add 1 to temp. */
2258 if (SIGNED_FIXED_POINT_MODE_P (mode) && temp_trunc.high < 0
2259 && !double_int_equal_p (TREE_FIXED_CST (arg1).data, temp_trunc))
2264 temp = double_int_add (temp, one);
2267 /* Given a fixed-point constant, make new constant with new type,
2268 appropriately sign-extended or truncated. */
2269 t = force_fit_type_double (type, temp.low, temp.high, -1,
2271 && (TYPE_UNSIGNED (type)
2272 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
2273 | TREE_OVERFLOW (arg1));
2278 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2279 to another floating point type. */
2282 fold_convert_const_real_from_real (tree type, const_tree arg1)
2284 REAL_VALUE_TYPE value;
2287 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
2288 t = build_real (type, value);
2290 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2294 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2295 to a floating point type. */
2298 fold_convert_const_real_from_fixed (tree type, const_tree arg1)
2300 REAL_VALUE_TYPE value;
2303 real_convert_from_fixed (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1));
2304 t = build_real (type, value);
2306 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
2307 TREE_CONSTANT_OVERFLOW (t)
2308 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
2312 /* A subroutine of fold_convert_const handling conversions a FIXED_CST
2313 to another fixed-point type. */
2316 fold_convert_const_fixed_from_fixed (tree type, const_tree arg1)
2318 FIXED_VALUE_TYPE value;
2322 overflow_p = fixed_convert (&value, TYPE_MODE (type), &TREE_FIXED_CST (arg1),
2323 TYPE_SATURATING (type));
2324 t = build_fixed (type, value);
2326 /* Propagate overflow flags. */
2327 if (overflow_p | TREE_OVERFLOW (arg1))
2329 TREE_OVERFLOW (t) = 1;
2330 TREE_CONSTANT_OVERFLOW (t) = 1;
2332 else if (TREE_CONSTANT_OVERFLOW (arg1))
2333 TREE_CONSTANT_OVERFLOW (t) = 1;
2337 /* A subroutine of fold_convert_const handling conversions an INTEGER_CST
2338 to a fixed-point type. */
2341 fold_convert_const_fixed_from_int (tree type, const_tree arg1)
2343 FIXED_VALUE_TYPE value;
2347 overflow_p = fixed_convert_from_int (&value, TYPE_MODE (type),
2348 TREE_INT_CST (arg1),
2349 TYPE_UNSIGNED (TREE_TYPE (arg1)),
2350 TYPE_SATURATING (type));
2351 t = build_fixed (type, value);
2353 /* Propagate overflow flags. */
2354 if (overflow_p | TREE_OVERFLOW (arg1))
2356 TREE_OVERFLOW (t) = 1;
2357 TREE_CONSTANT_OVERFLOW (t) = 1;
2359 else if (TREE_CONSTANT_OVERFLOW (arg1))
2360 TREE_CONSTANT_OVERFLOW (t) = 1;
2364 /* A subroutine of fold_convert_const handling conversions a REAL_CST
2365 to a fixed-point type. */
2368 fold_convert_const_fixed_from_real (tree type, const_tree arg1)
2370 FIXED_VALUE_TYPE value;
2374 overflow_p = fixed_convert_from_real (&value, TYPE_MODE (type),
2375 &TREE_REAL_CST (arg1),
2376 TYPE_SATURATING (type));
2377 t = build_fixed (type, value);
2379 /* Propagate overflow flags. */
2380 if (overflow_p | TREE_OVERFLOW (arg1))
2382 TREE_OVERFLOW (t) = 1;
2383 TREE_CONSTANT_OVERFLOW (t) = 1;
2385 else if (TREE_CONSTANT_OVERFLOW (arg1))
2386 TREE_CONSTANT_OVERFLOW (t) = 1;
2390 /* Attempt to fold type conversion operation CODE of expression ARG1 to
2391 type TYPE. If no simplification can be done return NULL_TREE. */
2394 fold_convert_const (enum tree_code code, tree type, tree arg1)
2396 if (TREE_TYPE (arg1) == type)
2399 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type)
2400 || TREE_CODE (type) == OFFSET_TYPE)
2402 if (TREE_CODE (arg1) == INTEGER_CST)
2403 return fold_convert_const_int_from_int (type, arg1);
2404 else if (TREE_CODE (arg1) == REAL_CST)
2405 return fold_convert_const_int_from_real (code, type, arg1);
2406 else if (TREE_CODE (arg1) == FIXED_CST)
2407 return fold_convert_const_int_from_fixed (type, arg1);
2409 else if (TREE_CODE (type) == REAL_TYPE)
2411 if (TREE_CODE (arg1) == INTEGER_CST)
2412 return build_real_from_int_cst (type, arg1);
2413 else if (TREE_CODE (arg1) == REAL_CST)
2414 return fold_convert_const_real_from_real (type, arg1);
2415 else if (TREE_CODE (arg1) == FIXED_CST)
2416 return fold_convert_const_real_from_fixed (type, arg1);
2418 else if (TREE_CODE (type) == FIXED_POINT_TYPE)
2420 if (TREE_CODE (arg1) == FIXED_CST)
2421 return fold_convert_const_fixed_from_fixed (type, arg1);
2422 else if (TREE_CODE (arg1) == INTEGER_CST)
2423 return fold_convert_const_fixed_from_int (type, arg1);
2424 else if (TREE_CODE (arg1) == REAL_CST)
2425 return fold_convert_const_fixed_from_real (type, arg1);
2430 /* Construct a vector of zero elements of vector type TYPE. */
2433 build_zero_vector (tree type)
2438 elem = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
2439 units = TYPE_VECTOR_SUBPARTS (type);
2442 for (i = 0; i < units; i++)
2443 list = tree_cons (NULL_TREE, elem, list);
2444 return build_vector (type, list);
2447 /* Returns true, if ARG is convertible to TYPE using a NOP_EXPR. */
2450 fold_convertible_p (const_tree type, const_tree arg)
2452 tree orig = TREE_TYPE (arg);
2457 if (TREE_CODE (arg) == ERROR_MARK
2458 || TREE_CODE (type) == ERROR_MARK
2459 || TREE_CODE (orig) == ERROR_MARK)
2462 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2465 switch (TREE_CODE (type))
2467 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2468 case POINTER_TYPE: case REFERENCE_TYPE:
2470 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2471 || TREE_CODE (orig) == OFFSET_TYPE)
2473 return (TREE_CODE (orig) == VECTOR_TYPE
2474 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2477 case FIXED_POINT_TYPE:
2481 return TREE_CODE (type) == TREE_CODE (orig);
2488 /* Convert expression ARG to type TYPE. Used by the middle-end for
2489 simple conversions in preference to calling the front-end's convert. */
2492 fold_convert (tree type, tree arg)
2494 tree orig = TREE_TYPE (arg);
2500 if (TREE_CODE (arg) == ERROR_MARK
2501 || TREE_CODE (type) == ERROR_MARK
2502 || TREE_CODE (orig) == ERROR_MARK)
2503 return error_mark_node;
2505 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig))
2506 return fold_build1 (NOP_EXPR, type, arg);
2508 switch (TREE_CODE (type))
2510 case INTEGER_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
2511 case POINTER_TYPE: case REFERENCE_TYPE:
2513 if (TREE_CODE (arg) == INTEGER_CST)
2515 tem = fold_convert_const (NOP_EXPR, type, arg);
2516 if (tem != NULL_TREE)
2519 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2520 || TREE_CODE (orig) == OFFSET_TYPE)
2521 return fold_build1 (NOP_EXPR, type, arg);
2522 if (TREE_CODE (orig) == COMPLEX_TYPE)
2524 tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2525 return fold_convert (type, tem);
2527 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
2528 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2529 return fold_build1 (NOP_EXPR, type, arg);
2532 if (TREE_CODE (arg) == INTEGER_CST)
2534 tem = fold_convert_const (FLOAT_EXPR, type, arg);
2535 if (tem != NULL_TREE)
2538 else if (TREE_CODE (arg) == REAL_CST)
2540 tem = fold_convert_const (NOP_EXPR, type, arg);
2541 if (tem != NULL_TREE)
2544 else if (TREE_CODE (arg) == FIXED_CST)
2546 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2547 if (tem != NULL_TREE)
2551 switch (TREE_CODE (orig))
2554 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2555 case POINTER_TYPE: case REFERENCE_TYPE:
2556 return fold_build1 (FLOAT_EXPR, type, arg);
2559 return fold_build1 (NOP_EXPR, type, arg);
2561 case FIXED_POINT_TYPE:
2562 return fold_build1 (FIXED_CONVERT_EXPR, type, arg);
2565 tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2566 return fold_convert (type, tem);
2572 case FIXED_POINT_TYPE:
2573 if (TREE_CODE (arg) == FIXED_CST || TREE_CODE (arg) == INTEGER_CST
2574 || TREE_CODE (arg) == REAL_CST)
2576 tem = fold_convert_const (FIXED_CONVERT_EXPR, type, arg);
2577 if (tem != NULL_TREE)
2581 switch (TREE_CODE (orig))
2583 case FIXED_POINT_TYPE:
2588 return fold_build1 (FIXED_CONVERT_EXPR, type, arg);
2591 tem = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2592 return fold_convert (type, tem);
2599 switch (TREE_CODE (orig))
2602 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
2603 case POINTER_TYPE: case REFERENCE_TYPE:
2605 case FIXED_POINT_TYPE:
2606 return build2 (COMPLEX_EXPR, type,
2607 fold_convert (TREE_TYPE (type), arg),
2608 fold_convert (TREE_TYPE (type), integer_zero_node));
2613 if (TREE_CODE (arg) == COMPLEX_EXPR)
2615 rpart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 0));
2616 ipart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 1));
2617 return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2620 arg = save_expr (arg);
2621 rpart = fold_build1 (REALPART_EXPR, TREE_TYPE (orig), arg);
2622 ipart = fold_build1 (IMAGPART_EXPR, TREE_TYPE (orig), arg);
2623 rpart = fold_convert (TREE_TYPE (type), rpart);
2624 ipart = fold_convert (TREE_TYPE (type), ipart);
2625 return fold_build2 (COMPLEX_EXPR, type, rpart, ipart);
2633 if (integer_zerop (arg))
2634 return build_zero_vector (type);
2635 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
2636 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
2637 || TREE_CODE (orig) == VECTOR_TYPE);
2638 return fold_build1 (VIEW_CONVERT_EXPR, type, arg);
2641 tem = fold_ignored_result (arg);
2642 if (TREE_CODE (tem) == MODIFY_EXPR)
2644 return fold_build1 (NOP_EXPR, type, tem);
2651 /* Return false if expr can be assumed not to be an lvalue, true
2655 maybe_lvalue_p (const_tree x)
2657 /* We only need to wrap lvalue tree codes. */
2658 switch (TREE_CODE (x))
2669 case ALIGN_INDIRECT_REF:
2670 case MISALIGNED_INDIRECT_REF:
2672 case ARRAY_RANGE_REF:
2678 case PREINCREMENT_EXPR:
2679 case PREDECREMENT_EXPR:
2681 case TRY_CATCH_EXPR:
2682 case WITH_CLEANUP_EXPR:
2693 /* Assume the worst for front-end tree codes. */
2694 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2702 /* Return an expr equal to X but certainly not valid as an lvalue. */
2707 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2712 if (! maybe_lvalue_p (x))
2714 return build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2717 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2718 Zero means allow extended lvalues. */
2720 int pedantic_lvalues;
2722 /* When pedantic, return an expr equal to X but certainly not valid as a
2723 pedantic lvalue. Otherwise, return X. */
2726 pedantic_non_lvalue (tree x)
2728 if (pedantic_lvalues)
2729 return non_lvalue (x);
2734 /* Given a tree comparison code, return the code that is the logical inverse
2735 of the given code. It is not safe to do this for floating-point
2736 comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2737 as well: if reversing the comparison is unsafe, return ERROR_MARK. */
2740 invert_tree_comparison (enum tree_code code, bool honor_nans)
2742 if (honor_nans && flag_trapping_math)
2752 return honor_nans ? UNLE_EXPR : LE_EXPR;
2754 return honor_nans ? UNLT_EXPR : LT_EXPR;
2756 return honor_nans ? UNGE_EXPR : GE_EXPR;
2758 return honor_nans ? UNGT_EXPR : GT_EXPR;
2772 return UNORDERED_EXPR;
2773 case UNORDERED_EXPR:
2774 return ORDERED_EXPR;
2780 /* Similar, but return the comparison that results if the operands are
2781 swapped. This is safe for floating-point. */
2784 swap_tree_comparison (enum tree_code code)
2791 case UNORDERED_EXPR:
2817 /* Convert a comparison tree code from an enum tree_code representation
2818 into a compcode bit-based encoding. This function is the inverse of
2819 compcode_to_comparison. */
2821 static enum comparison_code
2822 comparison_to_compcode (enum tree_code code)
2839 return COMPCODE_ORD;
2840 case UNORDERED_EXPR:
2841 return COMPCODE_UNORD;
2843 return COMPCODE_UNLT;
2845 return COMPCODE_UNEQ;
2847 return COMPCODE_UNLE;
2849 return COMPCODE_UNGT;
2851 return COMPCODE_LTGT;
2853 return COMPCODE_UNGE;
2859 /* Convert a compcode bit-based encoding of a comparison operator back
2860 to GCC's enum tree_code representation. This function is the
2861 inverse of comparison_to_compcode. */
2863 static enum tree_code
2864 compcode_to_comparison (enum comparison_code code)
2881 return ORDERED_EXPR;
2882 case COMPCODE_UNORD:
2883 return UNORDERED_EXPR;
2901 /* Return a tree for the comparison which is the combination of
2902 doing the AND or OR (depending on CODE) of the two operations LCODE
2903 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2904 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2905 if this makes the transformation invalid. */
2908 combine_comparisons (enum tree_code code, enum tree_code lcode,
2909 enum tree_code rcode, tree truth_type,
2910 tree ll_arg, tree lr_arg)
2912 bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2913 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2914 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2915 enum comparison_code compcode;
2919 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2920 compcode = lcompcode & rcompcode;
2923 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2924 compcode = lcompcode | rcompcode;
2933 /* Eliminate unordered comparisons, as well as LTGT and ORD
2934 which are not used unless the mode has NaNs. */
2935 compcode &= ~COMPCODE_UNORD;
2936 if (compcode == COMPCODE_LTGT)
2937 compcode = COMPCODE_NE;
2938 else if (compcode == COMPCODE_ORD)
2939 compcode = COMPCODE_TRUE;
2941 else if (flag_trapping_math)
2943 /* Check that the original operation and the optimized ones will trap
2944 under the same condition. */
2945 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2946 && (lcompcode != COMPCODE_EQ)
2947 && (lcompcode != COMPCODE_ORD);
2948 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2949 && (rcompcode != COMPCODE_EQ)
2950 && (rcompcode != COMPCODE_ORD);
2951 bool trap = (compcode & COMPCODE_UNORD) == 0
2952 && (compcode != COMPCODE_EQ)
2953 && (compcode != COMPCODE_ORD);
2955 /* In a short-circuited boolean expression the LHS might be
2956 such that the RHS, if evaluated, will never trap. For
2957 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2958 if neither x nor y is NaN. (This is a mixed blessing: for
2959 example, the expression above will never trap, hence
2960 optimizing it to x < y would be invalid). */
2961 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2962 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2965 /* If the comparison was short-circuited, and only the RHS
2966 trapped, we may now generate a spurious trap. */
2968 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2971 /* If we changed the conditions that cause a trap, we lose. */
2972 if ((ltrap || rtrap) != trap)
2976 if (compcode == COMPCODE_TRUE)
2977 return constant_boolean_node (true, truth_type);
2978 else if (compcode == COMPCODE_FALSE)
2979 return constant_boolean_node (false, truth_type);
2981 return fold_build2 (compcode_to_comparison (compcode),
2982 truth_type, ll_arg, lr_arg);
2985 /* Return nonzero if CODE is a tree code that represents a truth value. */
2988 truth_value_p (enum tree_code code)
2990 return (TREE_CODE_CLASS (code) == tcc_comparison
2991 || code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
2992 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
2993 || code == TRUTH_XOR_EXPR || code == TRUTH_NOT_EXPR);
2996 /* Return nonzero if two operands (typically of the same tree node)
2997 are necessarily equal. If either argument has side-effects this
2998 function returns zero. FLAGS modifies behavior as follows:
3000 If OEP_ONLY_CONST is set, only return nonzero for constants.
3001 This function tests whether the operands are indistinguishable;
3002 it does not test whether they are equal using C's == operation.
3003 The distinction is important for IEEE floating point, because
3004 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
3005 (2) two NaNs may be indistinguishable, but NaN!=NaN.
3007 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
3008 even though it may hold multiple values during a function.
3009 This is because a GCC tree node guarantees that nothing else is
3010 executed between the evaluation of its "operands" (which may often
3011 be evaluated in arbitrary order). Hence if the operands themselves
3012 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
3013 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
3014 unset means assuming isochronic (or instantaneous) tree equivalence.
3015 Unless comparing arbitrary expression trees, such as from different
3016 statements, this flag can usually be left unset.
3018 If OEP_PURE_SAME is set, then pure functions with identical arguments
3019 are considered the same. It is used when the caller has other ways
3020 to ensure that global memory is unchanged in between. */
3023 operand_equal_p (const_tree arg0, const_tree arg1, unsigned int flags)
3025 /* If either is ERROR_MARK, they aren't equal. */
3026 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK)
3029 /* Check equality of integer constants before bailing out due to
3030 precision differences. */
3031 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
3032 return tree_int_cst_equal (arg0, arg1);
3034 /* If both types don't have the same signedness, then we can't consider
3035 them equal. We must check this before the STRIP_NOPS calls
3036 because they may change the signedness of the arguments. As pointers
3037 strictly don't have a signedness, require either two pointers or
3038 two non-pointers as well. */
3039 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1))
3040 || POINTER_TYPE_P (TREE_TYPE (arg0)) != POINTER_TYPE_P (TREE_TYPE (arg1)))
3043 /* If both types don't have the same precision, then it is not safe
3045 if (TYPE_PRECISION (TREE_TYPE (arg0)) != TYPE_PRECISION (TREE_TYPE (arg1)))
3051 /* In case both args are comparisons but with different comparison
3052 code, try to swap the comparison operands of one arg to produce
3053 a match and compare that variant. */
3054 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3055 && COMPARISON_CLASS_P (arg0)
3056 && COMPARISON_CLASS_P (arg1))
3058 enum tree_code swap_code = swap_tree_comparison (TREE_CODE (arg1));
3060 if (TREE_CODE (arg0) == swap_code)
3061 return operand_equal_p (TREE_OPERAND (arg0, 0),
3062 TREE_OPERAND (arg1, 1), flags)
3063 && operand_equal_p (TREE_OPERAND (arg0, 1),
3064 TREE_OPERAND (arg1, 0), flags);
3067 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3068 /* This is needed for conversions and for COMPONENT_REF.
3069 Might as well play it safe and always test this. */
3070 || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
3071 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
3072 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
3075 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
3076 We don't care about side effects in that case because the SAVE_EXPR
3077 takes care of that for us. In all other cases, two expressions are
3078 equal if they have no side effects. If we have two identical
3079 expressions with side effects that should be treated the same due
3080 to the only side effects being identical SAVE_EXPR's, that will
3081 be detected in the recursive calls below. */
3082 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
3083 && (TREE_CODE (arg0) == SAVE_EXPR
3084 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
3087 /* Next handle constant cases, those for which we can return 1 even
3088 if ONLY_CONST is set. */
3089 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
3090 switch (TREE_CODE (arg0))
3093 return tree_int_cst_equal (arg0, arg1);
3096 return FIXED_VALUES_IDENTICAL (TREE_FIXED_CST (arg0),
3097 TREE_FIXED_CST (arg1));
3100 if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
3101 TREE_REAL_CST (arg1)))
3105 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0))))
3107 /* If we do not distinguish between signed and unsigned zero,
3108 consider them equal. */
3109 if (real_zerop (arg0) && real_zerop (arg1))
3118 v1 = TREE_VECTOR_CST_ELTS (arg0);
3119 v2 = TREE_VECTOR_CST_ELTS (arg1);
3122 if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
3125 v1 = TREE_CHAIN (v1);
3126 v2 = TREE_CHAIN (v2);
3133 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
3135 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
3139 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
3140 && ! memcmp (TREE_STRING_POINTER (arg0),
3141 TREE_STRING_POINTER (arg1),
3142 TREE_STRING_LENGTH (arg0)));
3145 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
3151 if (flags & OEP_ONLY_CONST)
3154 /* Define macros to test an operand from arg0 and arg1 for equality and a
3155 variant that allows null and views null as being different from any
3156 non-null value. In the latter case, if either is null, the both
3157 must be; otherwise, do the normal comparison. */
3158 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
3159 TREE_OPERAND (arg1, N), flags)
3161 #define OP_SAME_WITH_NULL(N) \
3162 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
3163 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
3165 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
3168 /* Two conversions are equal only if signedness and modes match. */
3169 switch (TREE_CODE (arg0))
3172 case FIX_TRUNC_EXPR:
3173 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
3174 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
3184 case tcc_comparison:
3186 if (OP_SAME (0) && OP_SAME (1))
3189 /* For commutative ops, allow the other order. */
3190 return (commutative_tree_code (TREE_CODE (arg0))
3191 && operand_equal_p (TREE_OPERAND (arg0, 0),
3192 TREE_OPERAND (arg1, 1), flags)
3193 && operand_equal_p (TREE_OPERAND (arg0, 1),
3194 TREE_OPERAND (arg1, 0), flags));
3197 /* If either of the pointer (or reference) expressions we are
3198 dereferencing contain a side effect, these cannot be equal. */
3199 if (TREE_SIDE_EFFECTS (arg0)
3200 || TREE_SIDE_EFFECTS (arg1))
3203 switch (TREE_CODE (arg0))
3206 case ALIGN_INDIRECT_REF:
3207 case MISALIGNED_INDIRECT_REF:
3213 case ARRAY_RANGE_REF:
3214 /* Operands 2 and 3 may be null.
3215 Compare the array index by value if it is constant first as we
3216 may have different types but same value here. */
3218 && (tree_int_cst_equal (TREE_OPERAND (arg0, 1),
3219 TREE_OPERAND (arg1, 1))
3221 && OP_SAME_WITH_NULL (2)
3222 && OP_SAME_WITH_NULL (3));
3225 /* Handle operand 2 the same as for ARRAY_REF. Operand 0
3226 may be NULL when we're called to compare MEM_EXPRs. */
3227 return OP_SAME_WITH_NULL (0)
3229 && OP_SAME_WITH_NULL (2);
3232 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3238 case tcc_expression:
3239 switch (TREE_CODE (arg0))
3242 case TRUTH_NOT_EXPR:
3245 case TRUTH_ANDIF_EXPR:
3246 case TRUTH_ORIF_EXPR:
3247 return OP_SAME (0) && OP_SAME (1);
3249 case TRUTH_AND_EXPR:
3251 case TRUTH_XOR_EXPR:
3252 if (OP_SAME (0) && OP_SAME (1))
3255 /* Otherwise take into account this is a commutative operation. */
3256 return (operand_equal_p (TREE_OPERAND (arg0, 0),
3257 TREE_OPERAND (arg1, 1), flags)
3258 && operand_equal_p (TREE_OPERAND (arg0, 1),
3259 TREE_OPERAND (arg1, 0), flags));
3262 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
3269 switch (TREE_CODE (arg0))
3272 /* If the CALL_EXPRs call different functions, then they
3273 clearly can not be equal. */
3274 if (! operand_equal_p (CALL_EXPR_FN (arg0), CALL_EXPR_FN (arg1),
3279 unsigned int cef = call_expr_flags (arg0);
3280 if (flags & OEP_PURE_SAME)
3281 cef &= ECF_CONST | ECF_PURE;
3288 /* Now see if all the arguments are the same. */
3290 const_call_expr_arg_iterator iter0, iter1;
3292 for (a0 = first_const_call_expr_arg (arg0, &iter0),
3293 a1 = first_const_call_expr_arg (arg1, &iter1);
3295 a0 = next_const_call_expr_arg (&iter0),
3296 a1 = next_const_call_expr_arg (&iter1))
3297 if (! operand_equal_p (a0, a1, flags))
3300 /* If we get here and both argument lists are exhausted
3301 then the CALL_EXPRs are equal. */
3302 return ! (a0 || a1);
3308 case tcc_declaration:
3309 /* Consider __builtin_sqrt equal to sqrt. */
3310 return (TREE_CODE (arg0) == FUNCTION_DECL
3311 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
3312 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
3313 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
3320 #undef OP_SAME_WITH_NULL
3323 /* Similar to operand_equal_p, but see if ARG0 might have been made by
3324 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
3326 When in doubt, return 0. */
3329 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
3331 int unsignedp1, unsignedpo;
3332 tree primarg0, primarg1, primother;
3333 unsigned int correct_width;
3335 if (operand_equal_p (arg0, arg1, 0))
3338 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
3339 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
3342 /* Discard any conversions that don't change the modes of ARG0 and ARG1
3343 and see if the inner values are the same. This removes any
3344 signedness comparison, which doesn't matter here. */
3345 primarg0 = arg0, primarg1 = arg1;
3346 STRIP_NOPS (primarg0);
3347 STRIP_NOPS (primarg1);
3348 if (operand_equal_p (primarg0, primarg1, 0))
3351 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
3352 actual comparison operand, ARG0.
3354 First throw away any conversions to wider types
3355 already present in the operands. */
3357 primarg1 = get_narrower (arg1, &unsignedp1);
3358 primother = get_narrower (other, &unsignedpo);
3360 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
3361 if (unsignedp1 == unsignedpo
3362 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
3363 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
3365 tree type = TREE_TYPE (arg0);
3367 /* Make sure shorter operand is extended the right way
3368 to match the longer operand. */
3369 primarg1 = fold_convert (signed_or_unsigned_type_for
3370 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
3372 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
3379 /* See if ARG is an expression that is either a comparison or is performing
3380 arithmetic on comparisons. The comparisons must only be comparing
3381 two different values, which will be stored in *CVAL1 and *CVAL2; if
3382 they are nonzero it means that some operands have already been found.
3383 No variables may be used anywhere else in the expression except in the
3384 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
3385 the expression and save_expr needs to be called with CVAL1 and CVAL2.
3387 If this is true, return 1. Otherwise, return zero. */
3390 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
3392 enum tree_code code = TREE_CODE (arg);
3393 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3395 /* We can handle some of the tcc_expression cases here. */
3396 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3398 else if (tclass == tcc_expression
3399 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
3400 || code == COMPOUND_EXPR))
3401 tclass = tcc_binary;
3403 else if (tclass == tcc_expression && code == SAVE_EXPR
3404 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
3406 /* If we've already found a CVAL1 or CVAL2, this expression is
3407 two complex to handle. */
3408 if (*cval1 || *cval2)
3418 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
3421 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
3422 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3423 cval1, cval2, save_p));
3428 case tcc_expression:
3429 if (code == COND_EXPR)
3430 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
3431 cval1, cval2, save_p)
3432 && twoval_comparison_p (TREE_OPERAND (arg, 1),
3433 cval1, cval2, save_p)
3434 && twoval_comparison_p (TREE_OPERAND (arg, 2),
3435 cval1, cval2, save_p));
3438 case tcc_comparison:
3439 /* First see if we can handle the first operand, then the second. For
3440 the second operand, we know *CVAL1 can't be zero. It must be that
3441 one side of the comparison is each of the values; test for the
3442 case where this isn't true by failing if the two operands
3445 if (operand_equal_p (TREE_OPERAND (arg, 0),
3446 TREE_OPERAND (arg, 1), 0))
3450 *cval1 = TREE_OPERAND (arg, 0);
3451 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
3453 else if (*cval2 == 0)
3454 *cval2 = TREE_OPERAND (arg, 0);
3455 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
3460 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
3462 else if (*cval2 == 0)
3463 *cval2 = TREE_OPERAND (arg, 1);
3464 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
3476 /* ARG is a tree that is known to contain just arithmetic operations and
3477 comparisons. Evaluate the operations in the tree substituting NEW0 for
3478 any occurrence of OLD0 as an operand of a comparison and likewise for
3482 eval_subst (tree arg, tree old0, tree new0, tree old1, tree new1)
3484 tree type = TREE_TYPE (arg);
3485 enum tree_code code = TREE_CODE (arg);
3486 enum tree_code_class tclass = TREE_CODE_CLASS (code);
3488 /* We can handle some of the tcc_expression cases here. */
3489 if (tclass == tcc_expression && code == TRUTH_NOT_EXPR)
3491 else if (tclass == tcc_expression
3492 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
3493 tclass = tcc_binary;
3498 return fold_build1 (code, type,
3499 eval_subst (TREE_OPERAND (arg, 0),
3500 old0, new0, old1, new1));
3503 return fold_build2 (code, type,
3504 eval_subst (TREE_OPERAND (arg, 0),
3505 old0, new0, old1, new1),
3506 eval_subst (TREE_OPERAND (arg, 1),
3507 old0, new0, old1, new1));
3509 case tcc_expression:
3513 return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
3516 return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
3519 return fold_build3 (code, type,
3520 eval_subst (TREE_OPERAND (arg, 0),
3521 old0, new0, old1, new1),
3522 eval_subst (TREE_OPERAND (arg, 1),
3523 old0, new0, old1, new1),
3524 eval_subst (TREE_OPERAND (arg, 2),
3525 old0, new0, old1, new1));
3529 /* Fall through - ??? */
3531 case tcc_comparison:
3533 tree arg0 = TREE_OPERAND (arg, 0);
3534 tree arg1 = TREE_OPERAND (arg, 1);
3536 /* We need to check both for exact equality and tree equality. The
3537 former will be true if the operand has a side-effect. In that
3538 case, we know the operand occurred exactly once. */
3540 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
3542 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
3545 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
3547 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
3550 return fold_build2 (code, type, arg0, arg1);
3558 /* Return a tree for the case when the result of an expression is RESULT
3559 converted to TYPE and OMITTED was previously an operand of the expression
3560 but is now not needed (e.g., we folded OMITTED * 0).
3562 If OMITTED has side effects, we must evaluate it. Otherwise, just do
3563 the conversion of RESULT to TYPE. */
3566 omit_one_operand (tree type, tree result, tree omitted)
3568 tree t = fold_convert (type, result);
3570 /* If the resulting operand is an empty statement, just return the omitted
3571 statement casted to void. */
3572 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3573 return build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3575 if (TREE_SIDE_EFFECTS (omitted))
3576 return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3578 return non_lvalue (t);
3581 /* Similar, but call pedantic_non_lvalue instead of non_lvalue. */
3584 pedantic_omit_one_operand (tree type, tree result, tree omitted)
3586 tree t = fold_convert (type, result);
3588 /* If the resulting operand is an empty statement, just return the omitted
3589 statement casted to void. */
3590 if (IS_EMPTY_STMT (t) && TREE_SIDE_EFFECTS (omitted))
3591 return build1 (NOP_EXPR, void_type_node, fold_ignored_result (omitted));
3593 if (TREE_SIDE_EFFECTS (omitted))
3594 return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
3596 return pedantic_non_lvalue (t);
3599 /* Return a tree for the case when the result of an expression is RESULT
3600 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
3601 of the expression but are now not needed.
3603 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
3604 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
3605 evaluated before OMITTED2. Otherwise, if neither has side effects,
3606 just do the conversion of RESULT to TYPE. */
3609 omit_two_operands (tree type, tree result, tree omitted1, tree omitted2)
3611 tree t = fold_convert (type, result);
3613 if (TREE_SIDE_EFFECTS (omitted2))
3614 t = build2 (COMPOUND_EXPR, type, omitted2, t);
3615 if (TREE_SIDE_EFFECTS (omitted1))
3616 t = build2 (COMPOUND_EXPR, type, omitted1, t);
3618 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue (t) : t;
3622 /* Return a simplified tree node for the truth-negation of ARG. This
3623 never alters ARG itself. We assume that ARG is an operation that
3624 returns a truth value (0 or 1).
3626 FIXME: one would think we would fold the result, but it causes
3627 problems with the dominator optimizer. */
3630 fold_truth_not_expr (tree arg)
3632 tree type = TREE_TYPE (arg);
3633 enum tree_code code = TREE_CODE (arg);
3635 /* If this is a comparison, we can simply invert it, except for
3636 floating-point non-equality comparisons, in which case we just
3637 enclose a TRUTH_NOT_EXPR around what we have. */
3639 if (TREE_CODE_CLASS (code) == tcc_comparison)
3641 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
3642 if (FLOAT_TYPE_P (op_type)
3643 && flag_trapping_math
3644 && code != ORDERED_EXPR && code != UNORDERED_EXPR
3645 && code != NE_EXPR && code != EQ_EXPR)
3649 code = invert_tree_comparison (code,
3650 HONOR_NANS (TYPE_MODE (op_type)));
3651 if (code == ERROR_MARK)
3654 return build2 (code, type,
3655 TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
3662 return constant_boolean_node (integer_zerop (arg), type);
3664 case TRUTH_AND_EXPR:
3665 return build2 (TRUTH_OR_EXPR, type,
3666 invert_truthvalue (TREE_OPERAND (arg, 0)),
3667 invert_truthvalue (TREE_OPERAND (arg, 1)));
3670 return build2 (TRUTH_AND_EXPR, type,
3671 invert_truthvalue (TREE_OPERAND (arg, 0)),
3672 invert_truthvalue (TREE_OPERAND (arg, 1)));
3674 case TRUTH_XOR_EXPR:
3675 /* Here we can invert either operand. We invert the first operand
3676 unless the second operand is a TRUTH_NOT_EXPR in which case our
3677 result is the XOR of the first operand with the inside of the
3678 negation of the second operand. */
3680 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
3681 return build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
3682 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
3684 return build2 (TRUTH_XOR_EXPR, type,
3685 invert_truthvalue (TREE_OPERAND (arg, 0)),
3686 TREE_OPERAND (arg, 1));
3688 case TRUTH_ANDIF_EXPR:
3689 return build2 (TRUTH_ORIF_EXPR, type,
3690 invert_truthvalue (TREE_OPERAND (arg, 0)),
3691 invert_truthvalue (TREE_OPERAND (arg, 1)));
3693 case TRUTH_ORIF_EXPR:
3694 return build2 (TRUTH_ANDIF_EXPR, type,
3695 invert_truthvalue (TREE_OPERAND (arg, 0)),
3696 invert_truthvalue (TREE_OPERAND (arg, 1)));
3698 case TRUTH_NOT_EXPR:
3699 return TREE_OPERAND (arg, 0);
3703 tree arg1 = TREE_OPERAND (arg, 1);
3704 tree arg2 = TREE_OPERAND (arg, 2);
3705 /* A COND_EXPR may have a throw as one operand, which
3706 then has void type. Just leave void operands
3708 return build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
3709 VOID_TYPE_P (TREE_TYPE (arg1))
3710 ? arg1 : invert_truthvalue (arg1),
3711 VOID_TYPE_P (TREE_TYPE (arg2))
3712 ? arg2 : invert_truthvalue (arg2));
3716 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
3717 invert_truthvalue (TREE_OPERAND (arg, 1)));
3719 case NON_LVALUE_EXPR:
3720 return invert_truthvalue (TREE_OPERAND (arg, 0));
3723 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
3724 return build1 (TRUTH_NOT_EXPR, type, arg);
3728 return build1 (TREE_CODE (arg), type,
3729 invert_truthvalue (TREE_OPERAND (arg, 0)));
3732 if (!integer_onep (TREE_OPERAND (arg, 1)))
3734 return build2 (EQ_EXPR, type, arg,
3735 build_int_cst (type, 0));
3738 return build1 (TRUTH_NOT_EXPR, type, arg);
3740 case CLEANUP_POINT_EXPR:
3741 return build1 (CLEANUP_POINT_EXPR, type,
3742 invert_truthvalue (TREE_OPERAND (arg, 0)));
3751 /* Return a simplified tree node for the truth-negation of ARG. This
3752 never alters ARG itself. We assume that ARG is an operation that
3753 returns a truth value (0 or 1).
3755 FIXME: one would think we would fold the result, but it causes
3756 problems with the dominator optimizer. */
3759 invert_truthvalue (tree arg)
3763 if (TREE_CODE (arg) == ERROR_MARK)
3766 tem = fold_truth_not_expr (arg);
3768 tem = build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg), arg);
3773 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3774 operands are another bit-wise operation with a common input. If so,
3775 distribute the bit operations to save an operation and possibly two if
3776 constants are involved. For example, convert
3777 (A | B) & (A | C) into A | (B & C)
3778 Further simplification will occur if B and C are constants.
3780 If this optimization cannot be done, 0 will be returned. */
3783 distribute_bit_expr (enum tree_code code, tree type, tree arg0, tree arg1)
3788 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3789 || TREE_CODE (arg0) == code
3790 || (TREE_CODE (arg0) != BIT_AND_EXPR
3791 && TREE_CODE (arg0) != BIT_IOR_EXPR))
3794 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3796 common = TREE_OPERAND (arg0, 0);
3797 left = TREE_OPERAND (arg0, 1);
3798 right = TREE_OPERAND (arg1, 1);
3800 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3802 common = TREE_OPERAND (arg0, 0);
3803 left = TREE_OPERAND (arg0, 1);
3804 right = TREE_OPERAND (arg1, 0);
3806 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3808 common = TREE_OPERAND (arg0, 1);
3809 left = TREE_OPERAND (arg0, 0);
3810 right = TREE_OPERAND (arg1, 1);
3812 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3814 common = TREE_OPERAND (arg0, 1);
3815 left = TREE_OPERAND (arg0, 0);
3816 right = TREE_OPERAND (arg1, 0);
3821 return fold_build2 (TREE_CODE (arg0), type, common,
3822 fold_build2 (code, type, left, right));
3825 /* Knowing that ARG0 and ARG1 are both RDIV_EXPRs, simplify a binary operation
3826 with code CODE. This optimization is unsafe. */
3828 distribute_real_division (enum tree_code code, tree type, tree arg0, tree arg1)
3830 bool mul0 = TREE_CODE (arg0) == MULT_EXPR;
3831 bool mul1 = TREE_CODE (arg1) == MULT_EXPR;
3833 /* (A / C) +- (B / C) -> (A +- B) / C. */
3835 && operand_equal_p (TREE_OPERAND (arg0, 1),
3836 TREE_OPERAND (arg1, 1), 0))
3837 return fold_build2 (mul0 ? MULT_EXPR : RDIV_EXPR, type,
3838 fold_build2 (code, type,
3839 TREE_OPERAND (arg0, 0),
3840 TREE_OPERAND (arg1, 0)),
3841 TREE_OPERAND (arg0, 1));
3843 /* (A / C1) +- (A / C2) -> A * (1 / C1 +- 1 / C2). */
3844 if (operand_equal_p (TREE_OPERAND (arg0, 0),
3845 TREE_OPERAND (arg1, 0), 0)
3846 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
3847 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
3849 REAL_VALUE_TYPE r0, r1;
3850 r0 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
3851 r1 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
3853 real_arithmetic (&r0, RDIV_EXPR, &dconst1, &r0);
3855 real_arithmetic (&r1, RDIV_EXPR, &dconst1, &r1);
3856 real_arithmetic (&r0, code, &r0, &r1);
3857 return fold_build2 (MULT_EXPR, type,
3858 TREE_OPERAND (arg0, 0),
3859 build_real (type, r0));
3865 /* Subroutine for fold_truthop: decode a field reference.
3867 If EXP is a comparison reference, we return the innermost reference.
3869 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3870 set to the starting bit number.
3872 If the innermost field can be completely contained in a mode-sized
3873 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
3875 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3876 otherwise it is not changed.
3878 *PUNSIGNEDP is set to the signedness of the field.
3880 *PMASK is set to the mask used. This is either contained in a
3881 BIT_AND_EXPR or derived from the width of the field.
3883 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3885 Return 0 if this is not a component reference or is one that we can't
3886 do anything with. */
3889 decode_field_reference (tree exp, HOST_WIDE_INT *pbitsize,
3890 HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
3891 int *punsignedp, int *pvolatilep,
3892 tree *pmask, tree *pand_mask)
3894 tree outer_type = 0;
3896 tree mask, inner, offset;
3898 unsigned int precision;
3900 /* All the optimizations using this function assume integer fields.
3901 There are problems with FP fields since the type_for_size call
3902 below can fail for, e.g., XFmode. */
3903 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3906 /* We are interested in the bare arrangement of bits, so strip everything
3907 that doesn't affect the machine mode. However, record the type of the
3908 outermost expression if it may matter below. */
3909 if (CONVERT_EXPR_P (exp)
3910 || TREE_CODE (exp) == NON_LVALUE_EXPR)
3911 outer_type = TREE_TYPE (exp);
3914 if (TREE_CODE (exp) == BIT_AND_EXPR)
3916 and_mask = TREE_OPERAND (exp, 1);
3917 exp = TREE_OPERAND (exp, 0);
3918 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3919 if (TREE_CODE (and_mask) != INTEGER_CST)
3923 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3924 punsignedp, pvolatilep, false);
3925 if ((inner == exp && and_mask == 0)
3926 || *pbitsize < 0 || offset != 0
3927 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3930 /* If the number of bits in the reference is the same as the bitsize of
3931 the outer type, then the outer type gives the signedness. Otherwise
3932 (in case of a small bitfield) the signedness is unchanged. */
3933 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
3934 *punsignedp = TYPE_UNSIGNED (outer_type);
3936 /* Compute the mask to access the bitfield. */
3937 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
3938 precision = TYPE_PRECISION (unsigned_type);
3940 mask = build_int_cst_type (unsigned_type, -1);
3942 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3943 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3945 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
3947 mask = fold_build2 (BIT_AND_EXPR, unsigned_type,
3948 fold_convert (unsigned_type, and_mask), mask);
3951 *pand_mask = and_mask;
3955 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3956 represents the sign bit of EXP's type. If EXP represents a sign
3957 or zero extension, also test VAL against the unextended type.
3958 The return value is the (sub)expression whose sign bit is VAL,
3959 or NULL_TREE otherwise. */
3962 sign_bit_p (tree exp, const_tree val)
3964 unsigned HOST_WIDE_INT mask_lo, lo;
3965 HOST_WIDE_INT mask_hi, hi;
3969 /* Tree EXP must have an integral type. */
3970 t = TREE_TYPE (exp);
3971 if (! INTEGRAL_TYPE_P (t))
3974 /* Tree VAL must be an integer constant. */
3975 if (TREE_CODE (val) != INTEGER_CST
3976 || TREE_OVERFLOW (val))
3979 width = TYPE_PRECISION (t);
3980 if (width > HOST_BITS_PER_WIDE_INT)
3982 hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
3985 mask_hi = ((unsigned HOST_WIDE_INT) -1
3986 >> (2 * HOST_BITS_PER_WIDE_INT - width));
3992 lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
3995 mask_lo = ((unsigned HOST_WIDE_INT) -1
3996 >> (HOST_BITS_PER_WIDE_INT - width));
3999 /* We mask off those bits beyond TREE_TYPE (exp) so that we can
4000 treat VAL as if it were unsigned. */
4001 if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
4002 && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
4005 /* Handle extension from a narrower type. */
4006 if (TREE_CODE (exp) == NOP_EXPR
4007 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
4008 return sign_bit_p (TREE_OPERAND (exp, 0), val);
4013 /* Subroutine for fold_truthop: determine if an operand is simple enough
4014 to be evaluated unconditionally. */
4017 simple_operand_p (const_tree exp)
4019 /* Strip any conversions that don't change the machine mode. */
4022 return (CONSTANT_CLASS_P (exp)
4023 || TREE_CODE (exp) == SSA_NAME
4025 && ! TREE_ADDRESSABLE (exp)
4026 && ! TREE_THIS_VOLATILE (exp)
4027 && ! DECL_NONLOCAL (exp)
4028 /* Don't regard global variables as simple. They may be
4029 allocated in ways unknown to the compiler (shared memory,
4030 #pragma weak, etc). */
4031 && ! TREE_PUBLIC (exp)
4032 && ! DECL_EXTERNAL (exp)
4033 /* Loading a static variable is unduly expensive, but global
4034 registers aren't expensive. */
4035 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
4038 /* The following functions are subroutines to fold_range_test and allow it to
4039 try to change a logical combination of comparisons into a range test.
4042 X == 2 || X == 3 || X == 4 || X == 5
4046 (unsigned) (X - 2) <= 3
4048 We describe each set of comparisons as being either inside or outside
4049 a range, using a variable named like IN_P, and then describe the
4050 range with a lower and upper bound. If one of the bounds is omitted,
4051 it represents either the highest or lowest value of the type.
4053 In the comments below, we represent a range by two numbers in brackets
4054 preceded by a "+" to designate being inside that range, or a "-" to
4055 designate being outside that range, so the condition can be inverted by
4056 flipping the prefix. An omitted bound is represented by a "-". For
4057 example, "- [-, 10]" means being outside the range starting at the lowest
4058 possible value and ending at 10, in other words, being greater than 10.
4059 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
4062 We set up things so that the missing bounds are handled in a consistent
4063 manner so neither a missing bound nor "true" and "false" need to be
4064 handled using a special case. */
4066 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
4067 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
4068 and UPPER1_P are nonzero if the respective argument is an upper bound
4069 and zero for a lower. TYPE, if nonzero, is the type of the result; it
4070 must be specified for a comparison. ARG1 will be converted to ARG0's
4071 type if both are specified. */
4074 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
4075 tree arg1, int upper1_p)
4081 /* If neither arg represents infinity, do the normal operation.
4082 Else, if not a comparison, return infinity. Else handle the special
4083 comparison rules. Note that most of the cases below won't occur, but
4084 are handled for consistency. */
4086 if (arg0 != 0 && arg1 != 0)
4088 tem = fold_build2 (code, type != 0 ? type : TREE_TYPE (arg0),
4089 arg0, fold_convert (TREE_TYPE (arg0), arg1));
4091 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
4094 if (TREE_CODE_CLASS (code) != tcc_comparison)
4097 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
4098 for neither. In real maths, we cannot assume open ended ranges are
4099 the same. But, this is computer arithmetic, where numbers are finite.
4100 We can therefore make the transformation of any unbounded range with
4101 the value Z, Z being greater than any representable number. This permits
4102 us to treat unbounded ranges as equal. */
4103 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
4104 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
4108 result = sgn0 == sgn1;
4111 result = sgn0 != sgn1;
4114 result = sgn0 < sgn1;
4117 result = sgn0 <= sgn1;
4120 result = sgn0 > sgn1;
4123 result = sgn0 >= sgn1;
4129 return constant_boolean_node (result, type);
4132 /* Given EXP, a logical expression, set the range it is testing into
4133 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
4134 actually being tested. *PLOW and *PHIGH will be made of the same
4135 type as the returned expression. If EXP is not a comparison, we
4136 will most likely not be returning a useful value and range. Set
4137 *STRICT_OVERFLOW_P to true if the return value is only valid
4138 because signed overflow is undefined; otherwise, do not change
4139 *STRICT_OVERFLOW_P. */
4142 make_range (tree exp, int *pin_p, tree *plow, tree *phigh,
4143 bool *strict_overflow_p)
4145 enum tree_code code;
4146 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
4147 tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
4149 tree low, high, n_low, n_high;
4151 /* Start with simply saying "EXP != 0" and then look at the code of EXP
4152 and see if we can refine the range. Some of the cases below may not
4153 happen, but it doesn't seem worth worrying about this. We "continue"
4154 the outer loop when we've changed something; otherwise we "break"
4155 the switch, which will "break" the while. */
4158 low = high = build_int_cst (TREE_TYPE (exp), 0);
4162 code = TREE_CODE (exp);
4163 exp_type = TREE_TYPE (exp);
4165 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
4167 if (TREE_OPERAND_LENGTH (exp) > 0)
4168 arg0 = TREE_OPERAND (exp, 0);
4169 if (TREE_CODE_CLASS (code) == tcc_comparison
4170 || TREE_CODE_CLASS (code) == tcc_unary
4171 || TREE_CODE_CLASS (code) == tcc_binary)
4172 arg0_type = TREE_TYPE (arg0);
4173 if (TREE_CODE_CLASS (code) == tcc_binary
4174 || TREE_CODE_CLASS (code) == tcc_comparison
4175 || (TREE_CODE_CLASS (code) == tcc_expression
4176 && TREE_OPERAND_LENGTH (exp) > 1))
4177 arg1 = TREE_OPERAND (exp, 1);
4182 case TRUTH_NOT_EXPR:
4183 in_p = ! in_p, exp = arg0;
4186 case EQ_EXPR: case NE_EXPR:
4187 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
4188 /* We can only do something if the range is testing for zero
4189 and if the second operand is an integer constant. Note that
4190 saying something is "in" the range we make is done by
4191 complementing IN_P since it will set in the initial case of
4192 being not equal to zero; "out" is leaving it alone. */
4193 if (low == 0 || high == 0
4194 || ! integer_zerop (low) || ! integer_zerop (high)
4195 || TREE_CODE (arg1) != INTEGER_CST)
4200 case NE_EXPR: /* - [c, c] */
4203 case EQ_EXPR: /* + [c, c] */
4204 in_p = ! in_p, low = high = arg1;
4206 case GT_EXPR: /* - [-, c] */
4207 low = 0, high = arg1;
4209 case GE_EXPR: /* + [c, -] */
4210 in_p = ! in_p, low = arg1, high = 0;
4212 case LT_EXPR: /* - [c, -] */
4213 low = arg1, high = 0;
4215 case LE_EXPR: /* + [-, c] */
4216 in_p = ! in_p, low = 0, high = arg1;
4222 /* If this is an unsigned comparison, we also know that EXP is
4223 greater than or equal to zero. We base the range tests we make
4224 on that fact, so we record it here so we can parse existing
4225 range tests. We test arg0_type since often the return type
4226 of, e.g. EQ_EXPR, is boolean. */
4227 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
4229 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4231 build_int_cst (arg0_type, 0),
4235 in_p = n_in_p, low = n_low, high = n_high;
4237 /* If the high bound is missing, but we have a nonzero low
4238 bound, reverse the range so it goes from zero to the low bound
4240 if (high == 0 && low && ! integer_zerop (low))
4243 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
4244 integer_one_node, 0);
4245 low = build_int_cst (arg0_type, 0);
4253 /* (-x) IN [a,b] -> x in [-b, -a] */
4254 n_low = range_binop (MINUS_EXPR, exp_type,
4255 build_int_cst (exp_type, 0),
4257 n_high = range_binop (MINUS_EXPR, exp_type,
4258 build_int_cst (exp_type, 0),
4260 low = n_low, high = n_high;
4266 exp = build2 (MINUS_EXPR, exp_type, negate_expr (arg0),
4267 build_int_cst (exp_type, 1));
4270 case PLUS_EXPR: case MINUS_EXPR:
4271 if (TREE_CODE (arg1) != INTEGER_CST)
4274 /* If flag_wrapv and ARG0_TYPE is signed, then we cannot
4275 move a constant to the other side. */
4276 if (!TYPE_UNSIGNED (arg0_type)
4277 && !TYPE_OVERFLOW_UNDEFINED (arg0_type))
4280 /* If EXP is signed, any overflow in the computation is undefined,
4281 so we don't worry about it so long as our computations on
4282 the bounds don't overflow. For unsigned, overflow is defined
4283 and this is exactly the right thing. */
4284 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4285 arg0_type, low, 0, arg1, 0);
4286 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
4287 arg0_type, high, 1, arg1, 0);
4288 if ((n_low != 0 && TREE_OVERFLOW (n_low))
4289 || (n_high != 0 && TREE_OVERFLOW (n_high)))
4292 if (TYPE_OVERFLOW_UNDEFINED (arg0_type))
4293 *strict_overflow_p = true;
4295 /* Check for an unsigned range which has wrapped around the maximum
4296 value thus making n_high < n_low, and normalize it. */
4297 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
4299 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
4300 integer_one_node, 0);
4301 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
4302 integer_one_node, 0);
4304 /* If the range is of the form +/- [ x+1, x ], we won't
4305 be able to normalize it. But then, it represents the
4306 whole range or the empty set, so make it
4308 if (tree_int_cst_equal (n_low, low)
4309 && tree_int_cst_equal (n_high, high))
4315 low = n_low, high = n_high;
4320 CASE_CONVERT: case NON_LVALUE_EXPR:
4321 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
4324 if (! INTEGRAL_TYPE_P (arg0_type)
4325 || (low != 0 && ! int_fits_type_p (low, arg0_type))
4326 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
4329 n_low = low, n_high = high;
4332 n_low = fold_convert (arg0_type, n_low);
4335 n_high = fold_convert (arg0_type, n_high);
4338 /* If we're converting arg0 from an unsigned type, to exp,
4339 a signed type, we will be doing the comparison as unsigned.
4340 The tests above have already verified that LOW and HIGH
4343 So we have to ensure that we will handle large unsigned
4344 values the same way that the current signed bounds treat
4347 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
4351 /* For fixed-point modes, we need to pass the saturating flag
4352 as the 2nd parameter. */
4353 if (ALL_FIXED_POINT_MODE_P (TYPE_MODE (arg0_type)))
4354 equiv_type = lang_hooks.types.type_for_mode
4355 (TYPE_MODE (arg0_type),
4356 TYPE_SATURATING (arg0_type));
4358 equiv_type = lang_hooks.types.type_for_mode
4359 (TYPE_MODE (arg0_type), 1);
4361 /* A range without an upper bound is, naturally, unbounded.
4362 Since convert would have cropped a very large value, use
4363 the max value for the destination type. */
4365 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
4366 : TYPE_MAX_VALUE (arg0_type);
4368 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
4369 high_positive = fold_build2 (RSHIFT_EXPR, arg0_type,
4370 fold_convert (arg0_type,
4372 build_int_cst (arg0_type, 1));
4374 /* If the low bound is specified, "and" the range with the
4375 range for which the original unsigned value will be
4379 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4380 1, n_low, n_high, 1,
4381 fold_convert (arg0_type,
4386 in_p = (n_in_p == in_p);
4390 /* Otherwise, "or" the range with the range of the input
4391 that will be interpreted as negative. */
4392 if (! merge_ranges (&n_in_p, &n_low, &n_high,
4393 0, n_low, n_high, 1,
4394 fold_convert (arg0_type,
4399 in_p = (in_p != n_in_p);
4404 low = n_low, high = n_high;
4414 /* If EXP is a constant, we can evaluate whether this is true or false. */
4415 if (TREE_CODE (exp) == INTEGER_CST)
4417 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
4419 && integer_onep (range_binop (LE_EXPR, integer_type_node,
4425 *pin_p = in_p, *plow = low, *phigh = high;
4429 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
4430 type, TYPE, return an expression to test if EXP is in (or out of, depending
4431 on IN_P) the range. Return 0 if the test couldn't be created. */
4434 build_range_check (tree type, tree exp, int in_p, tree low, tree high)
4436 tree etype = TREE_TYPE (exp);
4439 #ifdef HAVE_canonicalize_funcptr_for_compare
4440 /* Disable this optimization for function pointer expressions
4441 on targets that require function pointer canonicalization. */
4442 if (HAVE_canonicalize_funcptr_for_compare
4443 && TREE_CODE (etype) == POINTER_TYPE
4444 && TREE_CODE (TREE_TYPE (etype)) == FUNCTION_TYPE)
4450 value = build_range_check (type, exp, 1, low, high);
4452 return invert_truthvalue (value);
4457 if (low == 0 && high == 0)
4458 return build_int_cst (type, 1);
4461 return fold_build2 (LE_EXPR, type, exp,
4462 fold_convert (etype, high));
4465 return fold_build2 (GE_EXPR, type, exp,
4466 fold_convert (etype, low));
4468 if (operand_equal_p (low, high, 0))
4469 return fold_build2 (EQ_EXPR, type, exp,
4470 fold_convert (etype, low));
4472 if (integer_zerop (low))
4474 if (! TYPE_UNSIGNED (etype))
4476 etype = unsigned_type_for (etype);
4477 high = fold_convert (etype, high);
4478 exp = fold_convert (etype, exp);
4480 return build_range_check (type, exp, 1, 0, high);
4483 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
4484 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
4486 unsigned HOST_WIDE_INT lo;
4490 prec = TYPE_PRECISION (etype);
4491 if (prec <= HOST_BITS_PER_WIDE_INT)
4494 lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
4498 hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
4499 lo = (unsigned HOST_WIDE_INT) -1;
4502 if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
4504 if (TYPE_UNSIGNED (etype))
4506 etype = signed_type_for (etype);
4507 exp = fold_convert (etype, exp);
4509 return fold_build2 (GT_EXPR, type, exp,
4510 build_int_cst (etype, 0));
4514 /* Optimize (c>=low) && (c<=high) into (c-low>=0) && (c-low<=high-low).
4515 This requires wrap-around arithmetics for the type of the expression. */
4516 switch (TREE_CODE (etype))
4519 /* There is no requirement that LOW be within the range of ETYPE
4520 if the latter is a subtype. It must, however, be within the base
4521 type of ETYPE. So be sure we do the subtraction in that type. */
4522 if (TREE_TYPE (etype))
4523 etype = TREE_TYPE (etype);
4528 etype = lang_hooks.types.type_for_size (TYPE_PRECISION (etype),
4529 TYPE_UNSIGNED (etype));
4536 /* If we don't have wrap-around arithmetics upfront, try to force it. */
4537 if (TREE_CODE (etype) == INTEGER_TYPE
4538 && !TYPE_OVERFLOW_WRAPS (etype))
4540 tree utype, minv, maxv;
4542 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
4543 for the type in question, as we rely on this here. */
4544 utype = unsigned_type_for (etype);
4545 maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
4546 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
4547 integer_one_node, 1);
4548 minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
4550 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
4557 high = fold_convert (etype, high);
4558 low = fold_convert (etype, low);
4559 exp = fold_convert (etype, exp);
4561 value = const_binop (MINUS_EXPR, high, low, 0);
4564 if (POINTER_TYPE_P (etype))
4566 if (value != 0 && !TREE_OVERFLOW (value))
4568 low = fold_convert (sizetype, low);
4569 low = fold_build1 (NEGATE_EXPR, sizetype, low);
4570 return build_range_check (type,
4571 fold_build2 (POINTER_PLUS_EXPR, etype, exp, low),
4572 1, build_int_cst (etype, 0), value);
4577 if (value != 0 && !TREE_OVERFLOW (value))
4578 return build_range_check (type,
4579 fold_build2 (MINUS_EXPR, etype, exp, low),
4580 1, build_int_cst (etype, 0), value);
4585 /* Return the predecessor of VAL in its type, handling the infinite case. */
4588 range_predecessor (tree val)
4590 tree type = TREE_TYPE (val);
4592 if (INTEGRAL_TYPE_P (type)
4593 && operand_equal_p (val, TYPE_MIN_VALUE (type), 0))
4596 return range_binop (MINUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4599 /* Return the successor of VAL in its type, handling the infinite case. */
4602 range_successor (tree val)
4604 tree type = TREE_TYPE (val);
4606 if (INTEGRAL_TYPE_P (type)
4607 && operand_equal_p (val, TYPE_MAX_VALUE (type), 0))
4610 return range_binop (PLUS_EXPR, NULL_TREE, val, 0, integer_one_node, 0);
4613 /* Given two ranges, see if we can merge them into one. Return 1 if we
4614 can, 0 if we can't. Set the output range into the specified parameters. */
4617 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
4618 tree high0, int in1_p, tree low1, tree high1)
4626 int lowequal = ((low0 == 0 && low1 == 0)
4627 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4628 low0, 0, low1, 0)));
4629 int highequal = ((high0 == 0 && high1 == 0)
4630 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
4631 high0, 1, high1, 1)));
4633 /* Make range 0 be the range that starts first, or ends last if they
4634 start at the same value. Swap them if it isn't. */
4635 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
4638 && integer_onep (range_binop (GT_EXPR, integer_type_node,
4639 high1, 1, high0, 1))))
4641 temp = in0_p, in0_p = in1_p, in1_p = temp;
4642 tem = low0, low0 = low1, low1 = tem;
4643 tem = high0, high0 = high1, high1 = tem;
4646 /* Now flag two cases, whether the ranges are disjoint or whether the
4647 second range is totally subsumed in the first. Note that the tests
4648 below are simplified by the ones above. */
4649 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
4650 high0, 1, low1, 0));
4651 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
4652 high1, 1, high0, 1));
4654 /* We now have four cases, depending on whether we are including or
4655 excluding the two ranges. */
4658 /* If they don't overlap, the result is false. If the second range
4659 is a subset it is the result. Otherwise, the range is from the start
4660 of the second to the end of the first. */
4662 in_p = 0, low = high = 0;
4664 in_p = 1, low = low1, high = high1;
4666 in_p = 1, low = low1, high = high0;
4669 else if (in0_p && ! in1_p)
4671 /* If they don't overlap, the result is the first range. If they are
4672 equal, the result is false. If the second range is a subset of the
4673 first, and the ranges begin at the same place, we go from just after
4674 the end of the second range to the end of the first. If the second
4675 range is not a subset of the first, or if it is a subset and both
4676 ranges end at the same place, the range starts at the start of the
4677 first range and ends just before the second range.
4678 Otherwise, we can't describe this as a single range. */
4680 in_p = 1, low = low0, high = high0;
4681 else if (lowequal && highequal)
4682 in_p = 0, low = high = 0;
4683 else if (subset && lowequal)
4685 low = range_successor (high1);
4690 /* We are in the weird situation where high0 > high1 but
4691 high1 has no successor. Punt. */
4695 else if (! subset || highequal)
4698 high = range_predecessor (low1);
4702 /* low0 < low1 but low1 has no predecessor. Punt. */
4710 else if (! in0_p && in1_p)
4712 /* If they don't overlap, the result is the second range. If the second
4713 is a subset of the first, the result is false. Otherwise,
4714 the range starts just after the first range and ends at the
4715 end of the second. */
4717 in_p = 1, low = low1, high = high1;
4718 else if (subset || highequal)
4719 in_p = 0, low = high = 0;
4722 low = range_successor (high0);
4727 /* high1 > high0 but high0 has no successor. Punt. */
4735 /* The case where we are excluding both ranges. Here the complex case
4736 is if they don't overlap. In that case, the only time we have a
4737 range is if they are adjacent. If the second is a subset of the
4738 first, the result is the first. Otherwise, the range to exclude
4739 starts at the beginning of the first range and ends at the end of the
4743 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4744 range_successor (high0),
4746 in_p = 0, low = low0, high = high1;
4749 /* Canonicalize - [min, x] into - [-, x]. */
4750 if (low0 && TREE_CODE (low0) == INTEGER_CST)
4751 switch (TREE_CODE (TREE_TYPE (low0)))
4754 if (TYPE_PRECISION (TREE_TYPE (low0))
4755 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4759 if (tree_int_cst_equal (low0,
4760 TYPE_MIN_VALUE (TREE_TYPE (low0))))
4764 if (TYPE_UNSIGNED (TREE_TYPE (low0))
4765 && integer_zerop (low0))
4772 /* Canonicalize - [x, max] into - [x, -]. */
4773 if (high1 && TREE_CODE (high1) == INTEGER_CST)
4774 switch (TREE_CODE (TREE_TYPE (high1)))
4777 if (TYPE_PRECISION (TREE_TYPE (high1))
4778 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4782 if (tree_int_cst_equal (high1,
4783 TYPE_MAX_VALUE (TREE_TYPE (high1))))
4787 if (TYPE_UNSIGNED (TREE_TYPE (high1))
4788 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4790 integer_one_node, 1)))
4797 /* The ranges might be also adjacent between the maximum and
4798 minimum values of the given type. For
4799 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4800 return + [x + 1, y - 1]. */
4801 if (low0 == 0 && high1 == 0)
4803 low = range_successor (high0);
4804 high = range_predecessor (low1);
4805 if (low == 0 || high == 0)
4815 in_p = 0, low = low0, high = high0;
4817 in_p = 0, low = low0, high = high1;
4820 *pin_p = in_p, *plow = low, *phigh = high;
4825 /* Subroutine of fold, looking inside expressions of the form
4826 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4827 of the COND_EXPR. This function is being used also to optimize
4828 A op B ? C : A, by reversing the comparison first.
4830 Return a folded expression whose code is not a COND_EXPR
4831 anymore, or NULL_TREE if no folding opportunity is found. */
4834 fold_cond_expr_with_comparison (tree type, tree arg0, tree arg1, tree arg2)
4836 enum tree_code comp_code = TREE_CODE (arg0);
4837 tree arg00 = TREE_OPERAND (arg0, 0);
4838 tree arg01 = TREE_OPERAND (arg0, 1);
4839 tree arg1_type = TREE_TYPE (arg1);
4845 /* If we have A op 0 ? A : -A, consider applying the following
4848 A == 0? A : -A same as -A
4849 A != 0? A : -A same as A
4850 A >= 0? A : -A same as abs (A)
4851 A > 0? A : -A same as abs (A)
4852 A <= 0? A : -A same as -abs (A)
4853 A < 0? A : -A same as -abs (A)
4855 None of these transformations work for modes with signed
4856 zeros. If A is +/-0, the first two transformations will
4857 change the sign of the result (from +0 to -0, or vice
4858 versa). The last four will fix the sign of the result,
4859 even though the original expressions could be positive or
4860 negative, depending on the sign of A.
4862 Note that all these transformations are correct if A is
4863 NaN, since the two alternatives (A and -A) are also NaNs. */
4864 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4865 && (FLOAT_TYPE_P (TREE_TYPE (arg01))
4866 ? real_zerop (arg01)
4867 : integer_zerop (arg01))
4868 && ((TREE_CODE (arg2) == NEGATE_EXPR
4869 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4870 /* In the case that A is of the form X-Y, '-A' (arg2) may
4871 have already been folded to Y-X, check for that. */
4872 || (TREE_CODE (arg1) == MINUS_EXPR
4873 && TREE_CODE (arg2) == MINUS_EXPR
4874 && operand_equal_p (TREE_OPERAND (arg1, 0),
4875 TREE_OPERAND (arg2, 1), 0)
4876 && operand_equal_p (TREE_OPERAND (arg1, 1),
4877 TREE_OPERAND (arg2, 0), 0))))
4882 tem = fold_convert (arg1_type, arg1);
4883 return pedantic_non_lvalue (fold_convert (type, negate_expr (tem)));
4886 return pedantic_non_lvalue (fold_convert (type, arg1));
4889 if (flag_trapping_math)
4894 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4895 arg1 = fold_convert (signed_type_for
4896 (TREE_TYPE (arg1)), arg1);
4897 tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
4898 return pedantic_non_lvalue (fold_convert (type, tem));
4901 if (flag_trapping_math)
4905 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4906 arg1 = fold_convert (signed_type_for
4907 (TREE_TYPE (arg1)), arg1);
4908 tem = fold_build1 (ABS_EXPR, TREE_TYPE (arg1), arg1);
4909 return negate_expr (fold_convert (type, tem));
4911 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4915 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
4916 A == 0 ? A : 0 is always 0 unless A is -0. Note that
4917 both transformations are correct when A is NaN: A != 0
4918 is then true, and A == 0 is false. */
4920 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4921 && integer_zerop (arg01) && integer_zerop (arg2))
4923 if (comp_code == NE_EXPR)
4924 return pedantic_non_lvalue (fold_convert (type, arg1));
4925 else if (comp_code == EQ_EXPR)
4926 return build_int_cst (type, 0);
4929 /* Try some transformations of A op B ? A : B.
4931 A == B? A : B same as B
4932 A != B? A : B same as A
4933 A >= B? A : B same as max (A, B)
4934 A > B? A : B same as max (B, A)
4935 A <= B? A : B same as min (A, B)
4936 A < B? A : B same as min (B, A)
4938 As above, these transformations don't work in the presence
4939 of signed zeros. For example, if A and B are zeros of
4940 opposite sign, the first two transformations will change
4941 the sign of the result. In the last four, the original
4942 expressions give different results for (A=+0, B=-0) and
4943 (A=-0, B=+0), but the transformed expressions do not.
4945 The first two transformations are correct if either A or B
4946 is a NaN. In the first transformation, the condition will
4947 be false, and B will indeed be chosen. In the case of the
4948 second transformation, the condition A != B will be true,
4949 and A will be chosen.
4951 The conversions to max() and min() are not correct if B is
4952 a number and A is not. The conditions in the original
4953 expressions will be false, so all four give B. The min()
4954 and max() versions would give a NaN instead. */
4955 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type))
4956 && operand_equal_for_comparison_p (arg01, arg2, arg00)
4957 /* Avoid these transformations if the COND_EXPR may be used
4958 as an lvalue in the C++ front-end. PR c++/19199. */
4960 || (strcmp (lang_hooks.name, "GNU C++") != 0
4961 && strcmp (lang_hooks.name, "GNU Objective-C++") != 0)
4962 || ! maybe_lvalue_p (arg1)
4963 || ! maybe_lvalue_p (arg2)))
4965 tree comp_op0 = arg00;
4966 tree comp_op1 = arg01;
4967 tree comp_type = TREE_TYPE (comp_op0);
4969 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
4970 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
4980 return pedantic_non_lvalue (fold_convert (type, arg2));
4982 return pedantic_non_lvalue (fold_convert (type, arg1));
4987 /* In C++ a ?: expression can be an lvalue, so put the
4988 operand which will be used if they are equal first
4989 so that we can convert this back to the
4990 corresponding COND_EXPR. */
4991 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4993 comp_op0 = fold_convert (comp_type, comp_op0);
4994 comp_op1 = fold_convert (comp_type, comp_op1);
4995 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
4996 ? fold_build2 (MIN_EXPR, comp_type, comp_op0, comp_op1)
4997 : fold_build2 (MIN_EXPR, comp_type, comp_op1, comp_op0);
4998 return pedantic_non_lvalue (fold_convert (type, tem));
5005 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5007 comp_op0 = fold_convert (comp_type, comp_op0);
5008 comp_op1 = fold_convert (comp_type, comp_op1);
5009 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
5010 ? fold_build2 (MAX_EXPR, comp_type, comp_op0, comp_op1)
5011 : fold_build2 (MAX_EXPR, comp_type, comp_op1, comp_op0);
5012 return pedantic_non_lvalue (fold_convert (type, tem));
5016 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5017 return pedantic_non_lvalue (fold_convert (type, arg2));
5020 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
5021 return pedantic_non_lvalue (fold_convert (type, arg1));
5024 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
5029 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
5030 we might still be able to simplify this. For example,
5031 if C1 is one less or one more than C2, this might have started
5032 out as a MIN or MAX and been transformed by this function.
5033 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
5035 if (INTEGRAL_TYPE_P (type)
5036 && TREE_CODE (arg01) == INTEGER_CST
5037 && TREE_CODE (arg2) == INTEGER_CST)
5041 /* We can replace A with C1 in this case. */
5042 arg1 = fold_convert (type, arg01);
5043 return fold_build3 (COND_EXPR, type, arg0, arg1, arg2);
5046 /* If C1 is C2 + 1, this is min(A, C2). */
5047 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5049 && operand_equal_p (arg01,
5050 const_binop (PLUS_EXPR, arg2,
5051 build_int_cst (type, 1), 0),
5053 return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
5055 fold_convert (type, arg1),
5060 /* If C1 is C2 - 1, this is min(A, C2). */
5061 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5063 && operand_equal_p (arg01,
5064 const_binop (MINUS_EXPR, arg2,
5065 build_int_cst (type, 1), 0),
5067 return pedantic_non_lvalue (fold_build2 (MIN_EXPR,
5069 fold_convert (type, arg1),
5074 /* If C1 is C2 - 1, this is max(A, C2). */
5075 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
5077 && operand_equal_p (arg01,
5078 const_binop (MINUS_EXPR, arg2,
5079 build_int_cst (type, 1), 0),
5081 return pedantic_non_lvalue (fold_build2 (MAX_EXPR,
5083 fold_convert (type, arg1),
5088 /* If C1 is C2 + 1, this is max(A, C2). */
5089 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
5091 && operand_equal_p (arg01,
5092 const_binop (PLUS_EXPR, arg2,
5093 build_int_cst (type, 1), 0),
5095 return pedantic_non_lvalue (fold_build2 (MAX_EXPR,
5097 fold_convert (type, arg1),
5111 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
5112 #define LOGICAL_OP_NON_SHORT_CIRCUIT (BRANCH_COST >= 2)
5115 /* EXP is some logical combination of boolean tests. See if we can
5116 merge it into some range test. Return the new tree if so. */
5119 fold_range_test (enum tree_code code, tree type, tree op0, tree op1)
5121 int or_op = (code == TRUTH_ORIF_EXPR
5122 || code == TRUTH_OR_EXPR);
5123 int in0_p, in1_p, in_p;
5124 tree low0, low1, low, high0, high1, high;
5125 bool strict_overflow_p = false;
5126 tree lhs = make_range (op0, &in0_p, &low0, &high0, &strict_overflow_p);
5127 tree rhs = make_range (op1, &in1_p, &low1, &high1, &strict_overflow_p);
5129 const char * const warnmsg = G_("assuming signed overflow does not occur "
5130 "when simplifying range test");
5132 /* If this is an OR operation, invert both sides; we will invert
5133 again at the end. */
5135 in0_p = ! in0_p, in1_p = ! in1_p;
5137 /* If both expressions are the same, if we can merge the ranges, and we
5138 can build the range test, return it or it inverted. If one of the
5139 ranges is always true or always false, consider it to be the same
5140 expression as the other. */
5141 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
5142 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
5144 && 0 != (tem = (build_range_check (type,
5146 : rhs != 0 ? rhs : integer_zero_node,
5149 if (strict_overflow_p)
5150 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
5151 return or_op ? invert_truthvalue (tem) : tem;
5154 /* On machines where the branch cost is expensive, if this is a
5155 short-circuited branch and the underlying object on both sides
5156 is the same, make a non-short-circuit operation. */
5157 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
5158 && lhs != 0 && rhs != 0
5159 && (code == TRUTH_ANDIF_EXPR
5160 || code == TRUTH_ORIF_EXPR)
5161 && operand_equal_p (lhs, rhs, 0))
5163 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
5164 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
5165 which cases we can't do this. */
5166 if (simple_operand_p (lhs))
5167 return build2 (code == TRUTH_ANDIF_EXPR
5168 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5171 else if (lang_hooks.decls.global_bindings_p () == 0
5172 && ! CONTAINS_PLACEHOLDER_P (lhs))
5174 tree common = save_expr (lhs);
5176 if (0 != (lhs = build_range_check (type, common,
5177 or_op ? ! in0_p : in0_p,
5179 && (0 != (rhs = build_range_check (type, common,
5180 or_op ? ! in1_p : in1_p,
5183 if (strict_overflow_p)
5184 fold_overflow_warning (warnmsg,
5185 WARN_STRICT_OVERFLOW_COMPARISON);
5186 return build2 (code == TRUTH_ANDIF_EXPR
5187 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
5196 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
5197 bit value. Arrange things so the extra bits will be set to zero if and
5198 only if C is signed-extended to its full width. If MASK is nonzero,
5199 it is an INTEGER_CST that should be AND'ed with the extra bits. */
5202 unextend (tree c, int p, int unsignedp, tree mask)
5204 tree type = TREE_TYPE (c);
5205 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
5208 if (p == modesize || unsignedp)
5211 /* We work by getting just the sign bit into the low-order bit, then
5212 into the high-order bit, then sign-extend. We then XOR that value
5214 temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
5215 temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
5217 /* We must use a signed type in order to get an arithmetic right shift.
5218 However, we must also avoid introducing accidental overflows, so that
5219 a subsequent call to integer_zerop will work. Hence we must
5220 do the type conversion here. At this point, the constant is either
5221 zero or one, and the conversion to a signed type can never overflow.
5222 We could get an overflow if this conversion is done anywhere else. */
5223 if (TYPE_UNSIGNED (type))
5224 temp = fold_convert (signed_type_for (type), temp);
5226 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
5227 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
5229 temp = const_binop (BIT_AND_EXPR, temp,
5230 fold_convert (TREE_TYPE (c), mask), 0);
5231 /* If necessary, convert the type back to match the type of C. */
5232 if (TYPE_UNSIGNED (type))
5233 temp = fold_convert (type, temp);
5235 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
5238 /* Find ways of folding logical expressions of LHS and RHS:
5239 Try to merge two comparisons to the same innermost item.
5240 Look for range tests like "ch >= '0' && ch <= '9'".
5241 Look for combinations of simple terms on machines with expensive branches
5242 and evaluate the RHS unconditionally.
5244 For example, if we have p->a == 2 && p->b == 4 and we can make an
5245 object large enough to span both A and B, we can do this with a comparison
5246 against the object ANDed with the a mask.
5248 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
5249 operations to do this with one comparison.
5251 We check for both normal comparisons and the BIT_AND_EXPRs made this by
5252 function and the one above.
5254 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
5255 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
5257 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
5260 We return the simplified tree or 0 if no optimization is possible. */
5263 fold_truthop (enum tree_code code, tree truth_type, tree lhs, tree rhs)
5265 /* If this is the "or" of two comparisons, we can do something if
5266 the comparisons are NE_EXPR. If this is the "and", we can do something
5267 if the comparisons are EQ_EXPR. I.e.,
5268 (a->b == 2 && a->c == 4) can become (a->new == NEW).
5270 WANTED_CODE is this operation code. For single bit fields, we can
5271 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
5272 comparison for one-bit fields. */
5274 enum tree_code wanted_code;
5275 enum tree_code lcode, rcode;
5276 tree ll_arg, lr_arg, rl_arg, rr_arg;
5277 tree ll_inner, lr_inner, rl_inner, rr_inner;
5278 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
5279 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
5280 HOST_WIDE_INT xll_bitpos, xrl_bitpos;
5281 HOST_WIDE_INT lnbitsize, lnbitpos;
5282 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
5283 enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
5284 enum machine_mode lnmode;
5285 tree ll_mask, lr_mask, rl_mask, rr_mask;
5286 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
5287 tree l_const, r_const;
5288 tree lntype, result;
5289 int first_bit, end_bit;
5291 tree orig_lhs = lhs, orig_rhs = rhs;
5292 enum tree_code orig_code = code;
5294 /* Start by getting the comparison codes. Fail if anything is volatile.
5295 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
5296 it were surrounded with a NE_EXPR. */
5298 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
5301 lcode = TREE_CODE (lhs);
5302 rcode = TREE_CODE (rhs);
5304 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
5306 lhs = build2 (NE_EXPR, truth_type, lhs,
5307 build_int_cst (TREE_TYPE (lhs), 0));
5311 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
5313 rhs = build2 (NE_EXPR, truth_type, rhs,
5314 build_int_cst (TREE_TYPE (rhs), 0));
5318 if (TREE_CODE_CLASS (lcode) != tcc_comparison
5319 || TREE_CODE_CLASS (rcode) != tcc_comparison)
5322 ll_arg = TREE_OPERAND (lhs, 0);
5323 lr_arg = TREE_OPERAND (lhs, 1);
5324 rl_arg = TREE_OPERAND (rhs, 0);
5325 rr_arg = TREE_OPERAND (rhs, 1);
5327 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
5328 if (simple_operand_p (ll_arg)
5329 && simple_operand_p (lr_arg))
5332 if (operand_equal_p (ll_arg, rl_arg, 0)
5333 && operand_equal_p (lr_arg, rr_arg, 0))
5335 result = combine_comparisons (code, lcode, rcode,
5336 truth_type, ll_arg, lr_arg);
5340 else if (operand_equal_p (ll_arg, rr_arg, 0)
5341 && operand_equal_p (lr_arg, rl_arg, 0))
5343 result = combine_comparisons (code, lcode,
5344 swap_tree_comparison (rcode),
5345 truth_type, ll_arg, lr_arg);
5351 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
5352 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
5354 /* If the RHS can be evaluated unconditionally and its operands are
5355 simple, it wins to evaluate the RHS unconditionally on machines
5356 with expensive branches. In this case, this isn't a comparison
5357 that can be merged. Avoid doing this if the RHS is a floating-point
5358 comparison since those can trap. */
5360 if (BRANCH_COST >= 2
5361 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
5362 && simple_operand_p (rl_arg)
5363 && simple_operand_p (rr_arg))
5365 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
5366 if (code == TRUTH_OR_EXPR
5367 && lcode == NE_EXPR && integer_zerop (lr_arg)
5368 && rcode == NE_EXPR && integer_zerop (rr_arg)
5369 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5370 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5371 return build2 (NE_EXPR, truth_type,
5372 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5374 build_int_cst (TREE_TYPE (ll_arg), 0));
5376 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
5377 if (code == TRUTH_AND_EXPR
5378 && lcode == EQ_EXPR && integer_zerop (lr_arg)
5379 && rcode == EQ_EXPR && integer_zerop (rr_arg)
5380 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg)
5381 && INTEGRAL_TYPE_P (TREE_TYPE (ll_arg)))
5382 return build2 (EQ_EXPR, truth_type,
5383 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
5385 build_int_cst (TREE_TYPE (ll_arg), 0));
5387 if (LOGICAL_OP_NON_SHORT_CIRCUIT)
5389 if (code != orig_code || lhs != orig_lhs || rhs != orig_rhs)
5390 return build2 (code, truth_type, lhs, rhs);
5395 /* See if the comparisons can be merged. Then get all the parameters for
5398 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
5399 || (rcode != EQ_EXPR && rcode != NE_EXPR))
5403 ll_inner = decode_field_reference (ll_arg,
5404 &ll_bitsize, &ll_bitpos, &ll_mode,
5405 &ll_unsignedp, &volatilep, &ll_mask,
5407 lr_inner = decode_field_reference (lr_arg,
5408 &lr_bitsize, &lr_bitpos, &lr_mode,
5409 &lr_unsignedp, &volatilep, &lr_mask,
5411 rl_inner = decode_field_reference (rl_arg,
5412 &rl_bitsize, &rl_bitpos, &rl_mode,
5413 &rl_unsignedp, &volatilep, &rl_mask,
5415 rr_inner = decode_field_reference (rr_arg,
5416 &rr_bitsize, &rr_bitpos, &rr_mode,
5417 &rr_unsignedp, &volatilep, &rr_mask,
5420 /* It must be true that the inner operation on the lhs of each
5421 comparison must be the same if we are to be able to do anything.
5422 Then see if we have constants. If not, the same must be true for
5424 if (volatilep || ll_inner == 0 || rl_inner == 0
5425 || ! operand_equal_p (ll_inner, rl_inner, 0))
5428 if (TREE_CODE (lr_arg) == INTEGER_CST
5429 && TREE_CODE (rr_arg) == INTEGER_CST)
5430 l_const = lr_arg, r_const = rr_arg;
5431 else if (lr_inner == 0 || rr_inner == 0
5432 || ! operand_equal_p (lr_inner, rr_inner, 0))
5435 l_const = r_const = 0;
5437 /* If either comparison code is not correct for our logical operation,
5438 fail. However, we can convert a one-bit comparison against zero into
5439 the opposite comparison against that bit being set in the field. */
5441 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
5442 if (lcode != wanted_code)
5444 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
5446 /* Make the left operand unsigned, since we are only interested
5447 in the value of one bit. Otherwise we are doing the wrong
5456 /* This is analogous to the code for l_const above. */
5457 if (rcode != wanted_code)
5459 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
5468 /* See if we can find a mode that contains both fields being compared on
5469 the left. If we can't, fail. Otherwise, update all constants and masks
5470 to be relative to a field of that size. */
5471 first_bit = MIN (ll_bitpos, rl_bitpos);
5472 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
5473 lnmode = get_best_mode (end_bit - first_bit, first_bit,
5474 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
5476 if (lnmode == VOIDmode)
5479 lnbitsize = GET_MODE_BITSIZE (lnmode);
5480 lnbitpos = first_bit & ~ (lnbitsize - 1);
5481 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
5482 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
5484 if (BYTES_BIG_ENDIAN)
5486 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
5487 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
5490 ll_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, ll_mask),
5491 size_int (xll_bitpos), 0);
5492 rl_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, rl_mask),
5493 size_int (xrl_bitpos), 0);
5497 l_const = fold_convert (lntype, l_const);
5498 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
5499 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
5500 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
5501 fold_build1 (BIT_NOT_EXPR,
5505 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5507 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5512 r_const = fold_convert (lntype, r_const);
5513 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
5514 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
5515 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
5516 fold_build1 (BIT_NOT_EXPR,
5520 warning (0, "comparison is always %d", wanted_code == NE_EXPR);
5522 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
5526 /* Handle the case of comparisons with constants. If there is something in
5527 common between the masks, those bits of the constants must be the same.
5528 If not, the condition is always false. Test for this to avoid generating
5529 incorrect code below. */
5530 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
5531 if (! integer_zerop (result)
5532 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
5533 const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
5535 if (wanted_code == NE_EXPR)
5537 warning (0, "%<or%> of unmatched not-equal tests is always 1");
5538 return constant_boolean_node (true, truth_type);
5542 warning (0, "%<and%> of mutually exclusive equal-tests is always 0");
5543 return constant_boolean_node (false, truth_type);
5550 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
5554 optimize_minmax_comparison (enum tree_code code, tree type, tree op0, tree op1)
5557 enum tree_code op_code;
5560 int consts_equal, consts_lt;
5563 STRIP_SIGN_NOPS (arg0);
5565 op_code = TREE_CODE (arg0);
5566 minmax_const = TREE_OPERAND (arg0, 1);
5567 comp_const = fold_convert (TREE_TYPE (arg0), op1);
5568 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
5569 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
5570 inner = TREE_OPERAND (arg0, 0);
5572 /* If something does not permit us to optimize, return the original tree. */
5573 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
5574 || TREE_CODE (comp_const) != INTEGER_CST
5575 || TREE_OVERFLOW (comp_const)
5576 || TREE_CODE (minmax_const) != INTEGER_CST
5577 || TREE_OVERFLOW (minmax_const))
5580 /* Now handle all the various comparison codes. We only handle EQ_EXPR
5581 and GT_EXPR, doing the rest with recursive calls using logical
5585 case NE_EXPR: case LT_EXPR: case LE_EXPR:
5587 tree tem = optimize_minmax_comparison (invert_tree_comparison (code, false),
5590 return invert_truthvalue (tem);
5596 fold_build2 (TRUTH_ORIF_EXPR, type,
5597 optimize_minmax_comparison
5598 (EQ_EXPR, type, arg0, comp_const),
5599 optimize_minmax_comparison
5600 (GT_EXPR, type, arg0, comp_const));
5603 if (op_code == MAX_EXPR && consts_equal)
5604 /* MAX (X, 0) == 0 -> X <= 0 */
5605 return fold_build2 (LE_EXPR, type, inner, comp_const);
5607 else if (op_code == MAX_EXPR && consts_lt)
5608 /* MAX (X, 0) == 5 -> X == 5 */
5609 return fold_build2 (EQ_EXPR, type, inner, comp_const);
5611 else if (op_code == MAX_EXPR)
5612 /* MAX (X, 0) == -1 -> false */
5613 return omit_one_operand (type, integer_zero_node, inner);
5615 else if (consts_equal)
5616 /* MIN (X, 0) == 0 -> X >= 0 */
5617 return fold_build2 (GE_EXPR, type, inner, comp_const);
5620 /* MIN (X, 0) == 5 -> false */
5621 return omit_one_operand (type, integer_zero_node, inner);
5624 /* MIN (X, 0) == -1 -> X == -1 */
5625 return fold_build2 (EQ_EXPR, type, inner, comp_const);
5628 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5629 /* MAX (X, 0) > 0 -> X > 0
5630 MAX (X, 0) > 5 -> X > 5 */
5631 return fold_build2 (GT_EXPR, type, inner, comp_const);
5633 else if (op_code == MAX_EXPR)
5634 /* MAX (X, 0) > -1 -> true */
5635 return omit_one_operand (type, integer_one_node, inner);
5637 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5638 /* MIN (X, 0) > 0 -> false
5639 MIN (X, 0) > 5 -> false */
5640 return omit_one_operand (type, integer_zero_node, inner);
5643 /* MIN (X, 0) > -1 -> X > -1 */
5644 return fold_build2 (GT_EXPR, type, inner, comp_const);
5651 /* T is an integer expression that is being multiplied, divided, or taken a
5652 modulus (CODE says which and what kind of divide or modulus) by a
5653 constant C. See if we can eliminate that operation by folding it with
5654 other operations already in T. WIDE_TYPE, if non-null, is a type that
5655 should be used for the computation if wider than our type.
5657 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5658 (X * 2) + (Y * 4). We must, however, be assured that either the original
5659 expression would not overflow or that overflow is undefined for the type
5660 in the language in question.
5662 We also canonicalize (X + 7) * 4 into X * 4 + 28 in the hope that either
5663 the machine has a multiply-accumulate insn or that this is part of an
5664 addressing calculation.
5666 If we return a non-null expression, it is an equivalent form of the
5667 original computation, but need not be in the original type.
5669 We set *STRICT_OVERFLOW_P to true if the return values depends on
5670 signed overflow being undefined. Otherwise we do not change
5671 *STRICT_OVERFLOW_P. */
5674 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type,
5675 bool *strict_overflow_p)
5677 /* To avoid exponential search depth, refuse to allow recursion past
5678 three levels. Beyond that (1) it's highly unlikely that we'll find
5679 something interesting and (2) we've probably processed it before
5680 when we built the inner expression. */
5689 ret = extract_muldiv_1 (t, c, code, wide_type, strict_overflow_p);
5696 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type,
5697 bool *strict_overflow_p)
5699 tree type = TREE_TYPE (t);
5700 enum tree_code tcode = TREE_CODE (t);
5701 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
5702 > GET_MODE_SIZE (TYPE_MODE (type)))
5703 ? wide_type : type);
5705 int same_p = tcode == code;
5706 tree op0 = NULL_TREE, op1 = NULL_TREE;
5707 bool sub_strict_overflow_p;
5709 /* Don't deal with constants of zero here; they confuse the code below. */
5710 if (integer_zerop (c))
5713 if (TREE_CODE_CLASS (tcode) == tcc_unary)
5714 op0 = TREE_OPERAND (t, 0);
5716 if (TREE_CODE_CLASS (tcode) == tcc_binary)
5717 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
5719 /* Note that we need not handle conditional operations here since fold
5720 already handles those cases. So just do arithmetic here. */
5724 /* For a constant, we can always simplify if we are a multiply
5725 or (for divide and modulus) if it is a multiple of our constant. */
5726 if (code == MULT_EXPR
5727 || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
5728 return const_binop (code, fold_convert (ctype, t),
5729 fold_convert (ctype, c), 0);
5732 CASE_CONVERT: case NON_LVALUE_EXPR:
5733 /* If op0 is an expression ... */
5734 if ((COMPARISON_CLASS_P (op0)
5735 || UNARY_CLASS_P (op0)
5736 || BINARY_CLASS_P (op0)
5737 || VL_EXP_CLASS_P (op0)
5738 || EXPRESSION_CLASS_P (op0))
5739 /* ... and has wrapping overflow, and its type is smaller
5740 than ctype, then we cannot pass through as widening. */
5741 && ((TYPE_OVERFLOW_WRAPS (TREE_TYPE (op0))
5742 && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
5743 && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
5744 && (TYPE_PRECISION (ctype)
5745 > TYPE_PRECISION (TREE_TYPE (op0))))
5746 /* ... or this is a truncation (t is narrower than op0),
5747 then we cannot pass through this narrowing. */
5748 || (TYPE_PRECISION (type)
5749 < TYPE_PRECISION (TREE_TYPE (op0)))
5750 /* ... or signedness changes for division or modulus,
5751 then we cannot pass through this conversion. */
5752 || (code != MULT_EXPR
5753 && (TYPE_UNSIGNED (ctype)
5754 != TYPE_UNSIGNED (TREE_TYPE (op0))))
5755 /* ... or has undefined overflow while the converted to
5756 type has not, we cannot do the operation in the inner type
5757 as that would introduce undefined overflow. */
5758 || (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (op0))
5759 && !TYPE_OVERFLOW_UNDEFINED (type))))
5762 /* Pass the constant down and see if we can make a simplification. If
5763 we can, replace this expression with the inner simplification for
5764 possible later conversion to our or some other type. */
5765 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
5766 && TREE_CODE (t2) == INTEGER_CST
5767 && !TREE_OVERFLOW (t2)
5768 && (0 != (t1 = extract_muldiv (op0, t2, code,
5770 ? ctype : NULL_TREE,
5771 strict_overflow_p))))
5776 /* If widening the type changes it from signed to unsigned, then we
5777 must avoid building ABS_EXPR itself as unsigned. */
5778 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
5780 tree cstype = (*signed_type_for) (ctype);
5781 if ((t1 = extract_muldiv (op0, c, code, cstype, strict_overflow_p))
5784 t1 = fold_build1 (tcode, cstype, fold_convert (cstype, t1));
5785 return fold_convert (ctype, t1);
5789 /* If the constant is negative, we cannot simplify this. */
5790 if (tree_int_cst_sgn (c) == -1)
5794 if ((t1 = extract_muldiv (op0, c, code, wide_type, strict_overflow_p))
5796 return fold_build1 (tcode, ctype, fold_convert (ctype, t1));
5799 case MIN_EXPR: case MAX_EXPR:
5800 /* If widening the type changes the signedness, then we can't perform
5801 this optimization as that changes the result. */
5802 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
5805 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
5806 sub_strict_overflow_p = false;
5807 if ((t1 = extract_muldiv (op0, c, code, wide_type,
5808 &sub_strict_overflow_p)) != 0
5809 && (t2 = extract_muldiv (op1, c, code, wide_type,
5810 &sub_strict_overflow_p)) != 0)
5812 if (tree_int_cst_sgn (c) < 0)
5813 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
5814 if (sub_strict_overflow_p)
5815 *strict_overflow_p = true;
5816 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5817 fold_convert (ctype, t2));
5821 case LSHIFT_EXPR: case RSHIFT_EXPR:
5822 /* If the second operand is constant, this is a multiplication
5823 or floor division, by a power of two, so we can treat it that
5824 way unless the multiplier or divisor overflows. Signed
5825 left-shift overflow is implementation-defined rather than
5826 undefined in C90, so do not convert signed left shift into
5828 if (TREE_CODE (op1) == INTEGER_CST
5829 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
5830 /* const_binop may not detect overflow correctly,
5831 so check for it explicitly here. */
5832 && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
5833 && TREE_INT_CST_HIGH (op1) == 0
5834 && 0 != (t1 = fold_convert (ctype,
5835 const_binop (LSHIFT_EXPR,
5838 && !TREE_OVERFLOW (t1))
5839 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
5840 ? MULT_EXPR : FLOOR_DIV_EXPR,
5841 ctype, fold_convert (ctype, op0), t1),
5842 c, code, wide_type, strict_overflow_p);
5845 case PLUS_EXPR: case MINUS_EXPR:
5846 /* See if we can eliminate the operation on both sides. If we can, we
5847 can return a new PLUS or MINUS. If we can't, the only remaining
5848 cases where we can do anything are if the second operand is a
5850 sub_strict_overflow_p = false;
5851 t1 = extract_muldiv (op0, c, code, wide_type, &sub_strict_overflow_p);
5852 t2 = extract_muldiv (op1, c, code, wide_type, &sub_strict_overflow_p);
5853 if (t1 != 0 && t2 != 0
5854 && (code == MULT_EXPR
5855 /* If not multiplication, we can only do this if both operands
5856 are divisible by c. */
5857 || (multiple_of_p (ctype, op0, c)
5858 && multiple_of_p (ctype, op1, c))))
5860 if (sub_strict_overflow_p)
5861 *strict_overflow_p = true;
5862 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5863 fold_convert (ctype, t2));
5866 /* If this was a subtraction, negate OP1 and set it to be an addition.
5867 This simplifies the logic below. */
5868 if (tcode == MINUS_EXPR)
5869 tcode = PLUS_EXPR, op1 = negate_expr (op1);
5871 if (TREE_CODE (op1) != INTEGER_CST)
5874 /* If either OP1 or C are negative, this optimization is not safe for
5875 some of the division and remainder types while for others we need
5876 to change the code. */
5877 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
5879 if (code == CEIL_DIV_EXPR)
5880 code = FLOOR_DIV_EXPR;
5881 else if (code == FLOOR_DIV_EXPR)
5882 code = CEIL_DIV_EXPR;
5883 else if (code != MULT_EXPR
5884 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
5888 /* If it's a multiply or a division/modulus operation of a multiple
5889 of our constant, do the operation and verify it doesn't overflow. */
5890 if (code == MULT_EXPR
5891 || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5893 op1 = const_binop (code, fold_convert (ctype, op1),
5894 fold_convert (ctype, c), 0);
5895 /* We allow the constant to overflow with wrapping semantics. */
5897 || (TREE_OVERFLOW (op1) && !TYPE_OVERFLOW_WRAPS (ctype)))
5903 /* If we have an unsigned type is not a sizetype, we cannot widen
5904 the operation since it will change the result if the original
5905 computation overflowed. */
5906 if (TYPE_UNSIGNED (ctype)
5907 && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
5911 /* If we were able to eliminate our operation from the first side,
5912 apply our operation to the second side and reform the PLUS. */
5913 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
5914 return fold_build2 (tcode, ctype, fold_convert (ctype, t1), op1);
5916 /* The last case is if we are a multiply. In that case, we can
5917 apply the distributive law to commute the multiply and addition
5918 if the multiplication of the constants doesn't overflow. */
5919 if (code == MULT_EXPR)
5920 return fold_build2 (tcode, ctype,
5921 fold_build2 (code, ctype,
5922 fold_convert (ctype, op0),
5923 fold_convert (ctype, c)),
5929 /* We have a special case here if we are doing something like
5930 (C * 8) % 4 since we know that's zero. */
5931 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
5932 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
5933 /* If the multiplication can overflow we cannot optimize this.
5934 ??? Until we can properly mark individual operations as
5935 not overflowing we need to treat sizetype special here as
5936 stor-layout relies on this opimization to make
5937 DECL_FIELD_BIT_OFFSET always a constant. */
5938 && (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (t))
5939 || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
5940 && TYPE_IS_SIZETYPE (TREE_TYPE (t))))
5941 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
5942 && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5944 *strict_overflow_p = true;
5945 return omit_one_operand (type, integer_zero_node, op0);
5948 /* ... fall through ... */
5950 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
5951 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
5952 /* If we can extract our operation from the LHS, do so and return a
5953 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
5954 do something only if the second operand is a constant. */
5956 && (t1 = extract_muldiv (op0, c, code, wide_type,
5957 strict_overflow_p)) != 0)
5958 return fold_build2 (tcode, ctype, fold_convert (ctype, t1),
5959 fold_convert (ctype, op1));
5960 else if (tcode == MULT_EXPR && code == MULT_EXPR
5961 && (t1 = extract_muldiv (op1, c, code, wide_type,
5962 strict_overflow_p)) != 0)
5963 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
5964 fold_convert (ctype, t1));
5965 else if (TREE_CODE (op1) != INTEGER_CST)
5968 /* If these are the same operation types, we can associate them
5969 assuming no overflow. */
5971 && 0 != (t1 = int_const_binop (MULT_EXPR, fold_convert (ctype, op1),
5972 fold_convert (ctype, c), 1))
5973 && 0 != (t1 = force_fit_type_double (ctype, TREE_INT_CST_LOW (t1),
5974 TREE_INT_CST_HIGH (t1),
5975 (TYPE_UNSIGNED (ctype)
5976 && tcode != MULT_EXPR) ? -1 : 1,
5977 TREE_OVERFLOW (t1)))
5978 && !TREE_OVERFLOW (t1))
5979 return fold_build2 (tcode, ctype, fold_convert (ctype, op0), t1);
5981 /* If these operations "cancel" each other, we have the main
5982 optimizations of this pass, which occur when either constant is a
5983 multiple of the other, in which case we replace this with either an
5984 operation or CODE or TCODE.
5986 If we have an unsigned type that is not a sizetype, we cannot do
5987 this since it will change the result if the original computation
5989 if ((TYPE_OVERFLOW_UNDEFINED (ctype)
5990 || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
5991 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
5992 || (tcode == MULT_EXPR
5993 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
5994 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR
5995 && code != MULT_EXPR)))
5997 if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5999 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6000 *strict_overflow_p = true;
6001 return fold_build2 (tcode, ctype, fold_convert (ctype, op0),
6002 fold_convert (ctype,
6003 const_binop (TRUNC_DIV_EXPR,
6006 else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
6008 if (TYPE_OVERFLOW_UNDEFINED (ctype))
6009 *strict_overflow_p = true;
6010 return fold_build2 (code, ctype, fold_convert (ctype, op0),
6011 fold_convert (ctype,
6012 const_binop (TRUNC_DIV_EXPR,
6025 /* Return a node which has the indicated constant VALUE (either 0 or
6026 1), and is of the indicated TYPE. */
6029 constant_boolean_node (int value, tree type)
6031 if (type == integer_type_node)
6032 return value ? integer_one_node : integer_zero_node;
6033 else if (type == boolean_type_node)
6034 return value ? boolean_true_node : boolean_false_node;
6036 return build_int_cst (type, value);
6040 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
6041 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
6042 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
6043 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
6044 COND is the first argument to CODE; otherwise (as in the example
6045 given here), it is the second argument. TYPE is the type of the
6046 original expression. Return NULL_TREE if no simplification is
6050 fold_binary_op_with_conditional_arg (enum tree_code code,
6051 tree type, tree op0, tree op1,
6052 tree cond, tree arg, int cond_first_p)
6054 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
6055 tree arg_type = cond_first_p ? TREE_TYPE (op1) : TREE_TYPE (op0);
6056 tree test, true_value, false_value;
6057 tree lhs = NULL_TREE;
6058 tree rhs = NULL_TREE;
6060 /* This transformation is only worthwhile if we don't have to wrap
6061 arg in a SAVE_EXPR, and the operation can be simplified on at least
6062 one of the branches once its pushed inside the COND_EXPR. */
6063 if (!TREE_CONSTANT (arg))
6066 if (TREE_CODE (cond) == COND_EXPR)
6068 test = TREE_OPERAND (cond, 0);
6069 true_value = TREE_OPERAND (cond, 1);
6070 false_value = TREE_OPERAND (cond, 2);
6071 /* If this operand throws an expression, then it does not make
6072 sense to try to perform a logical or arithmetic operation
6074 if (VOID_TYPE_P (TREE_TYPE (true_value)))
6076 if (VOID_TYPE_P (TREE_TYPE (false_value)))
6081 tree testtype = TREE_TYPE (cond);
6083 true_value = constant_boolean_node (true, testtype);
6084 false_value = constant_boolean_node (false, testtype);
6087 arg = fold_convert (arg_type, arg);
6090 true_value = fold_convert (cond_type, true_value);
6092 lhs = fold_build2 (code, type, true_value, arg);
6094 lhs = fold_build2 (code, type, arg, true_value);
6098 false_value = fold_convert (cond_type, false_value);
6100 rhs = fold_build2 (code, type, false_value, arg);
6102 rhs = fold_build2 (code, type, arg, false_value);
6105 test = fold_build3 (COND_EXPR, type, test, lhs, rhs);
6106 return fold_convert (type, test);
6110 /* Subroutine of fold() that checks for the addition of +/- 0.0.
6112 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
6113 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
6114 ADDEND is the same as X.
6116 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
6117 and finite. The problematic cases are when X is zero, and its mode
6118 has signed zeros. In the case of rounding towards -infinity,
6119 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
6120 modes, X + 0 is not the same as X because -0 + 0 is 0. */
6123 fold_real_zero_addition_p (const_tree type, const_tree addend, int negate)
6125 if (!real_zerop (addend))
6128 /* Don't allow the fold with -fsignaling-nans. */
6129 if (HONOR_SNANS (TYPE_MODE (type)))
6132 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
6133 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
6136 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
6137 if (TREE_CODE (addend) == REAL_CST
6138 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
6141 /* The mode has signed zeros, and we have to honor their sign.
6142 In this situation, there is only one case we can return true for.
6143 X - 0 is the same as X unless rounding towards -infinity is
6145 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
6148 /* Subroutine of fold() that checks comparisons of built-in math
6149 functions against real constants.
6151 FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
6152 operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR. TYPE
6153 is the type of the result and ARG0 and ARG1 are the operands of the
6154 comparison. ARG1 must be a TREE_REAL_CST.
6156 The function returns the constant folded tree if a simplification
6157 can be made, and NULL_TREE otherwise. */
6160 fold_mathfn_compare (enum built_in_function fcode, enum tree_code code,
6161 tree type, tree arg0, tree arg1)
6165 if (BUILTIN_SQRT_P (fcode))
6167 tree arg = CALL_EXPR_ARG (arg0, 0);
6168 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
6170 c = TREE_REAL_CST (arg1);
6171 if (REAL_VALUE_NEGATIVE (c))
6173 /* sqrt(x) < y is always false, if y is negative. */
6174 if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
6175 return omit_one_operand (type, integer_zero_node, arg);
6177 /* sqrt(x) > y is always true, if y is negative and we
6178 don't care about NaNs, i.e. negative values of x. */
6179 if (code == NE_EXPR || !HONOR_NANS (mode))
6180 return omit_one_operand (type, integer_one_node, arg);
6182 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
6183 return fold_build2 (GE_EXPR, type, arg,
6184 build_real (TREE_TYPE (arg), dconst0));
6186 else if (code == GT_EXPR || code == GE_EXPR)
6190 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6191 real_convert (&c2, mode, &c2);
6193 if (REAL_VALUE_ISINF (c2))
6195 /* sqrt(x) > y is x == +Inf, when y is very large. */
6196 if (HONOR_INFINITIES (mode))
6197 return fold_build2 (EQ_EXPR, type, arg,
6198 build_real (TREE_TYPE (arg), c2));
6200 /* sqrt(x) > y is always false, when y is very large
6201 and we don't care about infinities. */
6202 return omit_one_operand (type, integer_zero_node, arg);
6205 /* sqrt(x) > c is the same as x > c*c. */
6206 return fold_build2 (code, type, arg,
6207 build_real (TREE_TYPE (arg), c2));
6209 else if (code == LT_EXPR || code == LE_EXPR)
6213 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
6214 real_convert (&c2, mode, &c2);
6216 if (REAL_VALUE_ISINF (c2))
6218 /* sqrt(x) < y is always true, when y is a very large
6219 value and we don't care about NaNs or Infinities. */
6220 if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
6221 return omit_one_operand (type, integer_one_node, arg);
6223 /* sqrt(x) < y is x != +Inf when y is very large and we
6224 don't care about NaNs. */
6225 if (! HONOR_NANS (mode))
6226 return fold_build2 (NE_EXPR, type, arg,
6227 build_real (TREE_TYPE (arg), c2));
6229 /* sqrt(x) < y is x >= 0 when y is very large and we
6230 don't care about Infinities. */
6231 if (! HONOR_INFINITIES (mode))
6232 return fold_build2 (GE_EXPR, type, arg,
6233 build_real (TREE_TYPE (arg), dconst0));
6235 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
6236 if (lang_hooks.decls.global_bindings_p () != 0
6237 || CONTAINS_PLACEHOLDER_P (arg))
6240 arg = save_expr (arg);
6241 return fold_build2 (TRUTH_ANDIF_EXPR, type,
6242 fold_build2 (GE_EXPR, type, arg,
6243 build_real (TREE_TYPE (arg),
6245 fold_build2 (NE_EXPR, type, arg,
6246 build_real (TREE_TYPE (arg),
6250 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
6251 if (! HONOR_NANS (mode))
6252 return fold_build2 (code, type, arg,
6253 build_real (TREE_TYPE (arg), c2));
6255 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
6256 if (lang_hooks.decls.global_bindings_p () == 0
6257 && ! CONTAINS_PLACEHOLDER_P (arg))
6259 arg = save_expr (arg);
6260 return fold_build2 (TRUTH_ANDIF_EXPR, type,
6261 fold_build2 (GE_EXPR, type, arg,
6262 build_real (TREE_TYPE (arg),
6264 fold_build2 (code, type, arg,
6265 build_real (TREE_TYPE (arg),
6274 /* Subroutine of fold() that optimizes comparisons against Infinities,
6275 either +Inf or -Inf.
6277 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6278 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6279 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6281 The function returns the constant folded tree if a simplification
6282 can be made, and NULL_TREE otherwise. */
6285 fold_inf_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6287 enum machine_mode mode;
6288 REAL_VALUE_TYPE max;
6292 mode = TYPE_MODE (TREE_TYPE (arg0));
6294 /* For negative infinity swap the sense of the comparison. */
6295 neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
6297 code = swap_tree_comparison (code);
6302 /* x > +Inf is always false, if with ignore sNANs. */
6303 if (HONOR_SNANS (mode))
6305 return omit_one_operand (type, integer_zero_node, arg0);
6308 /* x <= +Inf is always true, if we don't case about NaNs. */
6309 if (! HONOR_NANS (mode))
6310 return omit_one_operand (type, integer_one_node, arg0);
6312 /* x <= +Inf is the same as x == x, i.e. isfinite(x). */
6313 if (lang_hooks.decls.global_bindings_p () == 0
6314 && ! CONTAINS_PLACEHOLDER_P (arg0))
6316 arg0 = save_expr (arg0);
6317 return fold_build2 (EQ_EXPR, type, arg0, arg0);
6323 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
6324 real_maxval (&max, neg, mode);
6325 return fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6326 arg0, build_real (TREE_TYPE (arg0), max));
6329 /* x < +Inf is always equal to x <= DBL_MAX. */
6330 real_maxval (&max, neg, mode);
6331 return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6332 arg0, build_real (TREE_TYPE (arg0), max));
6335 /* x != +Inf is always equal to !(x > DBL_MAX). */
6336 real_maxval (&max, neg, mode);
6337 if (! HONOR_NANS (mode))
6338 return fold_build2 (neg ? GE_EXPR : LE_EXPR, type,
6339 arg0, build_real (TREE_TYPE (arg0), max));
6341 temp = fold_build2 (neg ? LT_EXPR : GT_EXPR, type,
6342 arg0, build_real (TREE_TYPE (arg0), max));
6343 return fold_build1 (TRUTH_NOT_EXPR, type, temp);
6352 /* Subroutine of fold() that optimizes comparisons of a division by
6353 a nonzero integer constant against an integer constant, i.e.
6356 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
6357 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
6358 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
6360 The function returns the constant folded tree if a simplification
6361 can be made, and NULL_TREE otherwise. */
6364 fold_div_compare (enum tree_code code, tree type, tree arg0, tree arg1)
6366 tree prod, tmp, hi, lo;
6367 tree arg00 = TREE_OPERAND (arg0, 0);
6368 tree arg01 = TREE_OPERAND (arg0, 1);
6369 unsigned HOST_WIDE_INT lpart;
6370 HOST_WIDE_INT hpart;
6371 bool unsigned_p = TYPE_UNSIGNED (TREE_TYPE (arg0));
6375 /* We have to do this the hard way to detect unsigned overflow.
6376 prod = int_const_binop (MULT_EXPR, arg01, arg1, 0); */
6377 overflow = mul_double_with_sign (TREE_INT_CST_LOW (arg01),
6378 TREE_INT_CST_HIGH (arg01),
6379 TREE_INT_CST_LOW (arg1),
6380 TREE_INT_CST_HIGH (arg1),
6381 &lpart, &hpart, unsigned_p);
6382 prod = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6384 neg_overflow = false;
6388 tmp = int_const_binop (MINUS_EXPR, arg01,
6389 build_int_cst (TREE_TYPE (arg01), 1), 0);
6392 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0). */
6393 overflow = add_double_with_sign (TREE_INT_CST_LOW (prod),
6394 TREE_INT_CST_HIGH (prod),
6395 TREE_INT_CST_LOW (tmp),
6396 TREE_INT_CST_HIGH (tmp),
6397 &lpart, &hpart, unsigned_p);
6398 hi = force_fit_type_double (TREE_TYPE (arg00), lpart, hpart,
6399 -1, overflow | TREE_OVERFLOW (prod));
6401 else if (tree_int_cst_sgn (arg01) >= 0)
6403 tmp = int_const_binop (MINUS_EXPR, arg01,
6404 build_int_cst (TREE_TYPE (arg01), 1), 0);
6405 switch (tree_int_cst_sgn (arg1))
6408 neg_overflow = true;
6409 lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6414 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
6419 hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6429 /* A negative divisor reverses the relational operators. */
6430 code = swap_tree_comparison (code);
6432 tmp = int_const_binop (PLUS_EXPR, arg01,
6433 build_int_cst (TREE_TYPE (arg01), 1), 0);
6434 switch (tree_int_cst_sgn (arg1))
6437 hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
6442 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
6447 neg_overflow = true;
6448 lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
6460 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6461 return omit_one_operand (type, integer_zero_node, arg00);
6462 if (TREE_OVERFLOW (hi))
6463 return fold_build2 (GE_EXPR, type, arg00, lo);
6464 if (TREE_OVERFLOW (lo))
6465 return fold_build2 (LE_EXPR, type, arg00, hi);
6466 return build_range_check (type, arg00, 1, lo, hi);
6469 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
6470 return omit_one_operand (type, integer_one_node, arg00);
6471 if (TREE_OVERFLOW (hi))
6472 return fold_build2 (LT_EXPR, type, arg00, lo);
6473 if (TREE_OVERFLOW (lo))
6474 return fold_build2 (GT_EXPR, type, arg00, hi);
6475 return build_range_check (type, arg00, 0, lo, hi);
6478 if (TREE_OVERFLOW (lo))
6480 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6481 return omit_one_operand (type, tmp, arg00);
6483 return fold_build2 (LT_EXPR, type, arg00, lo);
6486 if (TREE_OVERFLOW (hi))
6488 tmp = neg_overflow ? integer_zero_node : integer_one_node;
6489 return omit_one_operand (type, tmp, arg00);
6491 return fold_build2 (LE_EXPR, type, arg00, hi);
6494 if (TREE_OVERFLOW (hi))
6496 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6497 return omit_one_operand (type, tmp, arg00);
6499 return fold_build2 (GT_EXPR, type, arg00, hi);
6502 if (TREE_OVERFLOW (lo))
6504 tmp = neg_overflow ? integer_one_node : integer_zero_node;
6505 return omit_one_operand (type, tmp, arg00);
6507 return fold_build2 (GE_EXPR, type, arg00, lo);
6517 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6518 equality/inequality test, then return a simplified form of the test
6519 using a sign testing. Otherwise return NULL. TYPE is the desired
6523 fold_single_bit_test_into_sign_test (enum tree_code code, tree arg0, tree arg1,
6526 /* If this is testing a single bit, we can optimize the test. */
6527 if ((code == NE_EXPR || code == EQ_EXPR)
6528 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6529 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6531 /* If we have (A & C) != 0 where C is the sign bit of A, convert
6532 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
6533 tree arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
6535 if (arg00 != NULL_TREE
6536 /* This is only a win if casting to a signed type is cheap,
6537 i.e. when arg00's type is not a partial mode. */
6538 && TYPE_PRECISION (TREE_TYPE (arg00))
6539 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
6541 tree stype = signed_type_for (TREE_TYPE (arg00));
6542 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
6543 result_type, fold_convert (stype, arg00),
6544 build_int_cst (stype, 0));
6551 /* If CODE with arguments ARG0 and ARG1 represents a single bit
6552 equality/inequality test, then return a simplified form of
6553 the test using shifts and logical operations. Otherwise return
6554 NULL. TYPE is the desired result type. */
6557 fold_single_bit_test (enum tree_code code, tree arg0, tree arg1,
6560 /* If this is testing a single bit, we can optimize the test. */
6561 if ((code == NE_EXPR || code == EQ_EXPR)
6562 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
6563 && integer_pow2p (TREE_OPERAND (arg0, 1)))
6565 tree inner = TREE_OPERAND (arg0, 0);
6566 tree type = TREE_TYPE (arg0);
6567 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
6568 enum machine_mode operand_mode = TYPE_MODE (type);
6570 tree signed_type, unsigned_type, intermediate_type;
6573 /* First, see if we can fold the single bit test into a sign-bit
6575 tem = fold_single_bit_test_into_sign_test (code, arg0, arg1,
6580 /* Otherwise we have (A & C) != 0 where C is a single bit,
6581 convert that into ((A >> C2) & 1). Where C2 = log2(C).
6582 Similarly for (A & C) == 0. */
6584 /* If INNER is a right shift of a constant and it plus BITNUM does
6585 not overflow, adjust BITNUM and INNER. */
6586 if (TREE_CODE (inner) == RSHIFT_EXPR
6587 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
6588 && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
6589 && bitnum < TYPE_PRECISION (type)
6590 && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
6591 bitnum - TYPE_PRECISION (type)))
6593 bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
6594 inner = TREE_OPERAND (inner, 0);
6597 /* If we are going to be able to omit the AND below, we must do our
6598 operations as unsigned. If we must use the AND, we have a choice.
6599 Normally unsigned is faster, but for some machines signed is. */
6600 #ifdef LOAD_EXTEND_OP
6601 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
6602 && !flag_syntax_only) ? 0 : 1;
6607 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
6608 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
6609 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
6610 inner = fold_convert (intermediate_type, inner);
6613 inner = build2 (RSHIFT_EXPR, intermediate_type,
6614 inner, size_int (bitnum));
6616 one = build_int_cst (intermediate_type, 1);
6618 if (code == EQ_EXPR)
6619 inner = fold_build2 (BIT_XOR_EXPR, intermediate_type, inner, one);
6621 /* Put the AND last so it can combine with more things. */
6622 inner = build2 (BIT_AND_EXPR, intermediate_type, inner, one);
6624 /* Make sure to return the proper type. */
6625 inner = fold_convert (result_type, inner);
6632 /* Check whether we are allowed to reorder operands arg0 and arg1,
6633 such that the evaluation of arg1 occurs before arg0. */
6636 reorder_operands_p (const_tree arg0, const_tree arg1)
6638 if (! flag_evaluation_order)
6640 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6642 return ! TREE_SIDE_EFFECTS (arg0)
6643 && ! TREE_SIDE_EFFECTS (arg1);
6646 /* Test whether it is preferable two swap two operands, ARG0 and
6647 ARG1, for example because ARG0 is an integer constant and ARG1
6648 isn't. If REORDER is true, only recommend swapping if we can
6649 evaluate the operands in reverse order. */
6652 tree_swap_operands_p (const_tree arg0, const_tree arg1, bool reorder)
6654 STRIP_SIGN_NOPS (arg0);
6655 STRIP_SIGN_NOPS (arg1);
6657 if (TREE_CODE (arg1) == INTEGER_CST)
6659 if (TREE_CODE (arg0) == INTEGER_CST)
6662 if (TREE_CODE (arg1) == REAL_CST)
6664 if (TREE_CODE (arg0) == REAL_CST)
6667 if (TREE_CODE (arg1) == FIXED_CST)
6669 if (TREE_CODE (arg0) == FIXED_CST)
6672 if (TREE_CODE (arg1) == COMPLEX_CST)
6674 if (TREE_CODE (arg0) == COMPLEX_CST)
6677 if (TREE_CONSTANT (arg1))
6679 if (TREE_CONSTANT (arg0))
6682 if (cfun && optimize_function_for_size_p (cfun))
6685 if (reorder && flag_evaluation_order
6686 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6689 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6690 for commutative and comparison operators. Ensuring a canonical
6691 form allows the optimizers to find additional redundancies without
6692 having to explicitly check for both orderings. */
6693 if (TREE_CODE (arg0) == SSA_NAME
6694 && TREE_CODE (arg1) == SSA_NAME
6695 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6698 /* Put SSA_NAMEs last. */
6699 if (TREE_CODE (arg1) == SSA_NAME)
6701 if (TREE_CODE (arg0) == SSA_NAME)
6704 /* Put variables last. */
6713 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
6714 ARG0 is extended to a wider type. */
6717 fold_widened_comparison (enum tree_code code, tree type, tree arg0, tree arg1)
6719 tree arg0_unw = get_unwidened (arg0, NULL_TREE);
6721 tree shorter_type, outer_type;
6725 if (arg0_unw == arg0)
6727 shorter_type = TREE_TYPE (arg0_unw);
6729 #ifdef HAVE_canonicalize_funcptr_for_compare
6730 /* Disable this optimization if we're casting a function pointer
6731 type on targets that require function pointer canonicalization. */
6732 if (HAVE_canonicalize_funcptr_for_compare
6733 && TREE_CODE (shorter_type) == POINTER_TYPE
6734 && TREE_CODE (TREE_TYPE (shorter_type)) == FUNCTION_TYPE)
6738 if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
6741 arg1_unw = get_unwidened (arg1, NULL_TREE);
6743 /* If possible, express the comparison in the shorter mode. */
6744 if ((code == EQ_EXPR || code == NE_EXPR
6745 || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
6746 && (TREE_TYPE (arg1_unw) == shorter_type
6747 || ((TYPE_PRECISION (shorter_type)
6748 >= TYPE_PRECISION (TREE_TYPE (arg1_unw)))
6749 && (TYPE_UNSIGNED (shorter_type)
6750 == TYPE_UNSIGNED (TREE_TYPE (arg1_unw))))
6751 || (TREE_CODE (arg1_unw) == INTEGER_CST
6752 && (TREE_CODE (shorter_type) == INTEGER_TYPE
6753 || TREE_CODE (shorter_type) == BOOLEAN_TYPE)
6754 && int_fits_type_p (arg1_unw, shorter_type))))
6755 return fold_build2 (code, type, arg0_unw,
6756 fold_convert (shorter_type, arg1_unw));
6758 if (TREE_CODE (arg1_unw) != INTEGER_CST
6759 || TREE_CODE (shorter_type) != INTEGER_TYPE
6760 || !int_fits_type_p (arg1_unw, shorter_type))
6763 /* If we are comparing with the integer that does not fit into the range
6764 of the shorter type, the result is known. */
6765 outer_type = TREE_TYPE (arg1_unw);
6766 min = lower_bound_in_type (outer_type, shorter_type);
6767 max = upper_bound_in_type (outer_type, shorter_type);
6769 above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6771 below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6778 return omit_one_operand (type, integer_zero_node, arg0);
6783 return omit_one_operand (type, integer_one_node, arg0);
6789 return omit_one_operand (type, integer_one_node, arg0);
6791 return omit_one_operand (type, integer_zero_node, arg0);
6796 return omit_one_operand (type, integer_zero_node, arg0);
6798 return omit_one_operand (type, integer_one_node, arg0);
6807 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
6808 ARG0 just the signedness is changed. */
6811 fold_sign_changed_comparison (enum tree_code code, tree type,
6812 tree arg0, tree arg1)
6815 tree inner_type, outer_type;
6817 if (!CONVERT_EXPR_P (arg0))
6820 outer_type = TREE_TYPE (arg0);
6821 arg0_inner = TREE_OPERAND (arg0, 0);
6822 inner_type = TREE_TYPE (arg0_inner);
6824 #ifdef HAVE_canonicalize_funcptr_for_compare
6825 /* Disable this optimization if we're casting a function pointer
6826 type on targets that require function pointer canonicalization. */
6827 if (HAVE_canonicalize_funcptr_for_compare
6828 && TREE_CODE (inner_type) == POINTER_TYPE
6829 && TREE_CODE (TREE_TYPE (inner_type)) == FUNCTION_TYPE)
6833 if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
6836 /* If the conversion is from an integral subtype to its basetype
6838 if (TREE_TYPE (inner_type) == outer_type)
6841 if (TREE_CODE (arg1) != INTEGER_CST
6842 && !(CONVERT_EXPR_P (arg1)
6843 && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
6846 if ((TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
6847 || POINTER_TYPE_P (inner_type) != POINTER_TYPE_P (outer_type))
6852 if (TREE_CODE (arg1) == INTEGER_CST)
6853 arg1 = force_fit_type_double (inner_type, TREE_INT_CST_LOW (arg1),
6854 TREE_INT_CST_HIGH (arg1), 0,
6855 TREE_OVERFLOW (arg1));
6857 arg1 = fold_convert (inner_type, arg1);
6859 return fold_build2 (code, type, arg0_inner, arg1);
6862 /* Tries to replace &a[idx] p+ s * delta with &a[idx + delta], if s is
6863 step of the array. Reconstructs s and delta in the case of s * delta
6864 being an integer constant (and thus already folded).
6865 ADDR is the address. MULT is the multiplicative expression.
6866 If the function succeeds, the new address expression is returned. Otherwise
6867 NULL_TREE is returned. */
6870 try_move_mult_to_index (tree addr, tree op1)
6872 tree s, delta, step;
6873 tree ref = TREE_OPERAND (addr, 0), pref;
6878 /* Strip the nops that might be added when converting op1 to sizetype. */
6881 /* Canonicalize op1 into a possibly non-constant delta
6882 and an INTEGER_CST s. */
6883 if (TREE_CODE (op1) == MULT_EXPR)
6885 tree arg0 = TREE_OPERAND (op1, 0), arg1 = TREE_OPERAND (op1, 1);
6890 if (TREE_CODE (arg0) == INTEGER_CST)
6895 else if (TREE_CODE (arg1) == INTEGER_CST)
6903 else if (TREE_CODE (op1) == INTEGER_CST)
6910 /* Simulate we are delta * 1. */
6912 s = integer_one_node;
6915 for (;; ref = TREE_OPERAND (ref, 0))
6917 if (TREE_CODE (ref) == ARRAY_REF)
6919 /* Remember if this was a multi-dimensional array. */
6920 if (TREE_CODE (TREE_OPERAND (ref, 0)) == ARRAY_REF)
6923 itype = TYPE_DOMAIN (TREE_TYPE (TREE_OPERAND (ref, 0)));
6927 step = array_ref_element_size (ref);
6928 if (TREE_CODE (step) != INTEGER_CST)
6933 if (! tree_int_cst_equal (step, s))
6938 /* Try if delta is a multiple of step. */
6939 tree tmp = div_if_zero_remainder (EXACT_DIV_EXPR, op1, step);
6945 /* Only fold here if we can verify we do not overflow one
6946 dimension of a multi-dimensional array. */
6951 if (TREE_CODE (TREE_OPERAND (ref, 1)) != INTEGER_CST
6952 || !INTEGRAL_TYPE_P (itype)
6953 || !TYPE_MAX_VALUE (itype)
6954 || TREE_CODE (TYPE_MAX_VALUE (itype)) != INTEGER_CST)
6957 tmp = fold_binary (PLUS_EXPR, itype,
6958 fold_convert (itype,
6959 TREE_OPERAND (ref, 1)),
6960 fold_convert (itype, delta));
6962 || TREE_CODE (tmp) != INTEGER_CST
6963 || tree_int_cst_lt (TYPE_MAX_VALUE (itype), tmp))
6972 if (!handled_component_p (ref))
6976 /* We found the suitable array reference. So copy everything up to it,
6977 and replace the index. */
6979 pref = TREE_OPERAND (addr, 0);
6980 ret = copy_node (pref);
6985 pref = TREE_OPERAND (pref, 0);
6986 TREE_OPERAND (pos, 0) = copy_node (pref);
6987 pos = TREE_OPERAND (pos, 0);
6990 TREE_OPERAND (pos, 1) = fold_build2 (PLUS_EXPR, itype,
6991 fold_convert (itype,
6992 TREE_OPERAND (pos, 1)),
6993 fold_convert (itype, delta));
6995 return fold_build1 (ADDR_EXPR, TREE_TYPE (addr), ret);
6999 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
7000 means A >= Y && A != MAX, but in this case we know that
7001 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
7004 fold_to_nonsharp_ineq_using_bound (tree ineq, tree bound)
7006 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
7008 if (TREE_CODE (bound) == LT_EXPR)
7009 a = TREE_OPERAND (bound, 0);
7010 else if (TREE_CODE (bound) == GT_EXPR)
7011 a = TREE_OPERAND (bound, 1);
7015 typea = TREE_TYPE (a);
7016 if (!INTEGRAL_TYPE_P (typea)
7017 && !POINTER_TYPE_P (typea))
7020 if (TREE_CODE (ineq) == LT_EXPR)
7022 a1 = TREE_OPERAND (ineq, 1);
7023 y = TREE_OPERAND (ineq, 0);
7025 else if (TREE_CODE (ineq) == GT_EXPR)
7027 a1 = TREE_OPERAND (ineq, 0);
7028 y = TREE_OPERAND (ineq, 1);
7033 if (TREE_TYPE (a1) != typea)
7036 if (POINTER_TYPE_P (typea))
7038 /* Convert the pointer types into integer before taking the difference. */
7039 tree ta = fold_convert (ssizetype, a);
7040 tree ta1 = fold_convert (ssizetype, a1);
7041 diff = fold_binary (MINUS_EXPR, ssizetype, ta1, ta);
7044 diff = fold_binary (MINUS_EXPR, typea, a1, a);
7046 if (!diff || !integer_onep (diff))
7049 return fold_build2 (GE_EXPR, type, a, y);
7052 /* Fold a sum or difference of at least one multiplication.
7053 Returns the folded tree or NULL if no simplification could be made. */
7056 fold_plusminus_mult_expr (enum tree_code code, tree type, tree arg0, tree arg1)
7058 tree arg00, arg01, arg10, arg11;
7059 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7061 /* (A * C) +- (B * C) -> (A+-B) * C.
7062 (A * C) +- A -> A * (C+-1).
7063 We are most concerned about the case where C is a constant,
7064 but other combinations show up during loop reduction. Since
7065 it is not difficult, try all four possibilities. */
7067 if (TREE_CODE (arg0) == MULT_EXPR)
7069 arg00 = TREE_OPERAND (arg0, 0);
7070 arg01 = TREE_OPERAND (arg0, 1);
7072 else if (TREE_CODE (arg0) == INTEGER_CST)
7074 arg00 = build_one_cst (type);
7079 /* We cannot generate constant 1 for fract. */
7080 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7083 arg01 = build_one_cst (type);
7085 if (TREE_CODE (arg1) == MULT_EXPR)
7087 arg10 = TREE_OPERAND (arg1, 0);
7088 arg11 = TREE_OPERAND (arg1, 1);
7090 else if (TREE_CODE (arg1) == INTEGER_CST)
7092 arg10 = build_one_cst (type);
7097 /* We cannot generate constant 1 for fract. */
7098 if (ALL_FRACT_MODE_P (TYPE_MODE (type)))
7101 arg11 = build_one_cst (type);
7105 if (operand_equal_p (arg01, arg11, 0))
7106 same = arg01, alt0 = arg00, alt1 = arg10;
7107 else if (operand_equal_p (arg00, arg10, 0))
7108 same = arg00, alt0 = arg01, alt1 = arg11;
7109 else if (operand_equal_p (arg00, arg11, 0))
7110 same = arg00, alt0 = arg01, alt1 = arg10;
7111 else if (operand_equal_p (arg01, arg10, 0))
7112 same = arg01, alt0 = arg00, alt1 = arg11;
7114 /* No identical multiplicands; see if we can find a common
7115 power-of-two factor in non-power-of-two multiplies. This
7116 can help in multi-dimensional array access. */
7117 else if (host_integerp (arg01, 0)
7118 && host_integerp (arg11, 0))
7120 HOST_WIDE_INT int01, int11, tmp;
7123 int01 = TREE_INT_CST_LOW (arg01);
7124 int11 = TREE_INT_CST_LOW (arg11);
7126 /* Move min of absolute values to int11. */
7127 if ((int01 >= 0 ? int01 : -int01)
7128 < (int11 >= 0 ? int11 : -int11))
7130 tmp = int01, int01 = int11, int11 = tmp;
7131 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7138 if (exact_log2 (abs (int11)) > 0 && int01 % int11 == 0)
7140 alt0 = fold_build2 (MULT_EXPR, TREE_TYPE (arg00), arg00,
7141 build_int_cst (TREE_TYPE (arg00),
7146 maybe_same = alt0, alt0 = alt1, alt1 = maybe_same;
7151 return fold_build2 (MULT_EXPR, type,
7152 fold_build2 (code, type,
7153 fold_convert (type, alt0),
7154 fold_convert (type, alt1)),
7155 fold_convert (type, same));
7160 /* Subroutine of native_encode_expr. Encode the INTEGER_CST
7161 specified by EXPR into the buffer PTR of length LEN bytes.
7162 Return the number of bytes placed in the buffer, or zero
7166 native_encode_int (const_tree expr, unsigned char *ptr, int len)
7168 tree type = TREE_TYPE (expr);
7169 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7170 int byte, offset, word, words;
7171 unsigned char value;
7173 if (total_bytes > len)
7175 words = total_bytes / UNITS_PER_WORD;
7177 for (byte = 0; byte < total_bytes; byte++)
7179 int bitpos = byte * BITS_PER_UNIT;
7180 if (bitpos < HOST_BITS_PER_WIDE_INT)
7181 value = (unsigned char) (TREE_INT_CST_LOW (expr) >> bitpos);
7183 value = (unsigned char) (TREE_INT_CST_HIGH (expr)
7184 >> (bitpos - HOST_BITS_PER_WIDE_INT));
7186 if (total_bytes > UNITS_PER_WORD)
7188 word = byte / UNITS_PER_WORD;
7189 if (WORDS_BIG_ENDIAN)
7190 word = (words - 1) - word;
7191 offset = word * UNITS_PER_WORD;
7192 if (BYTES_BIG_ENDIAN)
7193 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7195 offset += byte % UNITS_PER_WORD;
7198 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7199 ptr[offset] = value;
7205 /* Subroutine of native_encode_expr. Encode the REAL_CST
7206 specified by EXPR into the buffer PTR of length LEN bytes.
7207 Return the number of bytes placed in the buffer, or zero
7211 native_encode_real (const_tree expr, unsigned char *ptr, int len)
7213 tree type = TREE_TYPE (expr);
7214 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7215 int byte, offset, word, words, bitpos;
7216 unsigned char value;
7218 /* There are always 32 bits in each long, no matter the size of
7219 the hosts long. We handle floating point representations with
7223 if (total_bytes > len)
7225 words = 32 / UNITS_PER_WORD;
7227 real_to_target (tmp, TREE_REAL_CST_PTR (expr), TYPE_MODE (type));
7229 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7230 bitpos += BITS_PER_UNIT)
7232 byte = (bitpos / BITS_PER_UNIT) & 3;
7233 value = (unsigned char) (tmp[bitpos / 32] >> (bitpos & 31));
7235 if (UNITS_PER_WORD < 4)
7237 word = byte / UNITS_PER_WORD;
7238 if (WORDS_BIG_ENDIAN)
7239 word = (words - 1) - word;
7240 offset = word * UNITS_PER_WORD;
7241 if (BYTES_BIG_ENDIAN)
7242 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7244 offset += byte % UNITS_PER_WORD;
7247 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7248 ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)] = value;
7253 /* Subroutine of native_encode_expr. Encode the COMPLEX_CST
7254 specified by EXPR into the buffer PTR of length LEN bytes.
7255 Return the number of bytes placed in the buffer, or zero
7259 native_encode_complex (const_tree expr, unsigned char *ptr, int len)
7264 part = TREE_REALPART (expr);
7265 rsize = native_encode_expr (part, ptr, len);
7268 part = TREE_IMAGPART (expr);
7269 isize = native_encode_expr (part, ptr+rsize, len-rsize);
7272 return rsize + isize;
7276 /* Subroutine of native_encode_expr. Encode the VECTOR_CST
7277 specified by EXPR into the buffer PTR of length LEN bytes.
7278 Return the number of bytes placed in the buffer, or zero
7282 native_encode_vector (const_tree expr, unsigned char *ptr, int len)
7284 int i, size, offset, count;
7285 tree itype, elem, elements;
7288 elements = TREE_VECTOR_CST_ELTS (expr);
7289 count = TYPE_VECTOR_SUBPARTS (TREE_TYPE (expr));
7290 itype = TREE_TYPE (TREE_TYPE (expr));
7291 size = GET_MODE_SIZE (TYPE_MODE (itype));
7292 for (i = 0; i < count; i++)
7296 elem = TREE_VALUE (elements);
7297 elements = TREE_CHAIN (elements);
7304 if (native_encode_expr (elem, ptr+offset, len-offset) != size)
7309 if (offset + size > len)
7311 memset (ptr+offset, 0, size);
7319 /* Subroutine of fold_view_convert_expr. Encode the INTEGER_CST,
7320 REAL_CST, COMPLEX_CST or VECTOR_CST specified by EXPR into the
7321 buffer PTR of length LEN bytes. Return the number of bytes
7322 placed in the buffer, or zero upon failure. */
7325 native_encode_expr (const_tree expr, unsigned char *ptr, int len)
7327 switch (TREE_CODE (expr))
7330 return native_encode_int (expr, ptr, len);
7333 return native_encode_real (expr, ptr, len);
7336 return native_encode_complex (expr, ptr, len);
7339 return native_encode_vector (expr, ptr, len);
7347 /* Subroutine of native_interpret_expr. Interpret the contents of
7348 the buffer PTR of length LEN as an INTEGER_CST of type TYPE.
7349 If the buffer cannot be interpreted, return NULL_TREE. */
7352 native_interpret_int (tree type, const unsigned char *ptr, int len)
7354 int total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7355 int byte, offset, word, words;
7356 unsigned char value;
7357 unsigned int HOST_WIDE_INT lo = 0;
7358 HOST_WIDE_INT hi = 0;
7360 if (total_bytes > len)
7362 if (total_bytes * BITS_PER_UNIT > 2 * HOST_BITS_PER_WIDE_INT)
7364 words = total_bytes / UNITS_PER_WORD;
7366 for (byte = 0; byte < total_bytes; byte++)
7368 int bitpos = byte * BITS_PER_UNIT;
7369 if (total_bytes > UNITS_PER_WORD)
7371 word = byte / UNITS_PER_WORD;
7372 if (WORDS_BIG_ENDIAN)
7373 word = (words - 1) - word;
7374 offset = word * UNITS_PER_WORD;
7375 if (BYTES_BIG_ENDIAN)
7376 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7378 offset += byte % UNITS_PER_WORD;
7381 offset = BYTES_BIG_ENDIAN ? (total_bytes - 1) - byte : byte;
7382 value = ptr[offset];
7384 if (bitpos < HOST_BITS_PER_WIDE_INT)
7385 lo |= (unsigned HOST_WIDE_INT) value << bitpos;
7387 hi |= (unsigned HOST_WIDE_INT) value
7388 << (bitpos - HOST_BITS_PER_WIDE_INT);
7391 return build_int_cst_wide_type (type, lo, hi);
7395 /* Subroutine of native_interpret_expr. Interpret the contents of
7396 the buffer PTR of length LEN as a REAL_CST of type TYPE.
7397 If the buffer cannot be interpreted, return NULL_TREE. */
7400 native_interpret_real (tree type, const unsigned char *ptr, int len)
7402 enum machine_mode mode = TYPE_MODE (type);
7403 int total_bytes = GET_MODE_SIZE (mode);
7404 int byte, offset, word, words, bitpos;
7405 unsigned char value;
7406 /* There are always 32 bits in each long, no matter the size of
7407 the hosts long. We handle floating point representations with
7412 total_bytes = GET_MODE_SIZE (TYPE_MODE (type));
7413 if (total_bytes > len || total_bytes > 24)
7415 words = 32 / UNITS_PER_WORD;
7417 memset (tmp, 0, sizeof (tmp));
7418 for (bitpos = 0; bitpos < total_bytes * BITS_PER_UNIT;
7419 bitpos += BITS_PER_UNIT)
7421 byte = (bitpos / BITS_PER_UNIT) & 3;
7422 if (UNITS_PER_WORD < 4)
7424 word = byte / UNITS_PER_WORD;
7425 if (WORDS_BIG_ENDIAN)
7426 word = (words - 1) - word;
7427 offset = word * UNITS_PER_WORD;
7428 if (BYTES_BIG_ENDIAN)
7429 offset += (UNITS_PER_WORD - 1) - (byte % UNITS_PER_WORD);
7431 offset += byte % UNITS_PER_WORD;
7434 offset = BYTES_BIG_ENDIAN ? 3 - byte : byte;
7435 value = ptr[offset + ((bitpos / BITS_PER_UNIT) & ~3)];
7437 tmp[bitpos / 32] |= (unsigned long)value << (bitpos & 31);
7440 real_from_target (&r, tmp, mode);
7441 return build_real (type, r);
7445 /* Subroutine of native_interpret_expr. Interpret the contents of
7446 the buffer PTR of length LEN as a COMPLEX_CST of type TYPE.
7447 If the buffer cannot be interpreted, return NULL_TREE. */
7450 native_interpret_complex (tree type, const unsigned char *ptr, int len)
7452 tree etype, rpart, ipart;
7455 etype = TREE_TYPE (type);
7456 size = GET_MODE_SIZE (TYPE_MODE (etype));
7459 rpart = native_interpret_expr (etype, ptr, size);
7462 ipart = native_interpret_expr (etype, ptr+size, size);
7465 return build_complex (type, rpart, ipart);
7469 /* Subroutine of native_interpret_expr. Interpret the contents of
7470 the buffer PTR of length LEN as a VECTOR_CST of type TYPE.
7471 If the buffer cannot be interpreted, return NULL_TREE. */
7474 native_interpret_vector (tree type, const unsigned char *ptr, int len)
7476 tree etype, elem, elements;
7479 etype = TREE_TYPE (type);
7480 size = GET_MODE_SIZE (TYPE_MODE (etype));
7481 count = TYPE_VECTOR_SUBPARTS (type);
7482 if (size * count > len)
7485 elements = NULL_TREE;
7486 for (i = count - 1; i >= 0; i--)
7488 elem = native_interpret_expr (etype, ptr+(i*size), size);
7491 elements = tree_cons (NULL_TREE, elem, elements);
7493 return build_vector (type, elements);
7497 /* Subroutine of fold_view_convert_expr. Interpret the contents of
7498 the buffer PTR of length LEN as a constant of type TYPE. For
7499 INTEGRAL_TYPE_P we return an INTEGER_CST, for SCALAR_FLOAT_TYPE_P
7500 we return a REAL_CST, etc... If the buffer cannot be interpreted,
7501 return NULL_TREE. */
7504 native_interpret_expr (tree type, const unsigned char *ptr, int len)
7506 switch (TREE_CODE (type))
7511 return native_interpret_int (type, ptr, len);
7514 return native_interpret_real (type, ptr, len);
7517 return native_interpret_complex (type, ptr, len);
7520 return native_interpret_vector (type, ptr, len);
7528 /* Fold a VIEW_CONVERT_EXPR of a constant expression EXPR to type
7529 TYPE at compile-time. If we're unable to perform the conversion
7530 return NULL_TREE. */
7533 fold_view_convert_expr (tree type, tree expr)
7535 /* We support up to 512-bit values (for V8DFmode). */
7536 unsigned char buffer[64];
7539 /* Check that the host and target are sane. */
7540 if (CHAR_BIT != 8 || BITS_PER_UNIT != 8)
7543 len = native_encode_expr (expr, buffer, sizeof (buffer));
7547 return native_interpret_expr (type, buffer, len);
7550 /* Build an expression for the address of T. Folds away INDIRECT_REF
7551 to avoid confusing the gimplify process. When IN_FOLD is true
7552 avoid modifications of T. */
7555 build_fold_addr_expr_with_type_1 (tree t, tree ptrtype, bool in_fold)
7557 /* The size of the object is not relevant when talking about its address. */
7558 if (TREE_CODE (t) == WITH_SIZE_EXPR)
7559 t = TREE_OPERAND (t, 0);
7561 /* Note: doesn't apply to ALIGN_INDIRECT_REF */
7562 if (TREE_CODE (t) == INDIRECT_REF
7563 || TREE_CODE (t) == MISALIGNED_INDIRECT_REF)
7565 t = TREE_OPERAND (t, 0);
7567 if (TREE_TYPE (t) != ptrtype)
7568 t = build1 (NOP_EXPR, ptrtype, t);
7574 while (handled_component_p (base))
7575 base = TREE_OPERAND (base, 0);
7578 TREE_ADDRESSABLE (base) = 1;
7580 t = build1 (ADDR_EXPR, ptrtype, t);
7583 t = build1 (ADDR_EXPR, ptrtype, t);
7588 /* Build an expression for the address of T with type PTRTYPE. This
7589 function modifies the input parameter 'T' by sometimes setting the
7590 TREE_ADDRESSABLE flag. */
7593 build_fold_addr_expr_with_type (tree t, tree ptrtype)
7595 return build_fold_addr_expr_with_type_1 (t, ptrtype, false);
7598 /* Build an expression for the address of T. This function modifies
7599 the input parameter 'T' by sometimes setting the TREE_ADDRESSABLE
7600 flag. When called from fold functions, use fold_addr_expr instead. */
7603 build_fold_addr_expr (tree t)
7605 return build_fold_addr_expr_with_type_1 (t,
7606 build_pointer_type (TREE_TYPE (t)),
7610 /* Same as build_fold_addr_expr, builds an expression for the address
7611 of T, but avoids touching the input node 't'. Fold functions
7612 should use this version. */
7615 fold_addr_expr (tree t)
7617 tree ptrtype = build_pointer_type (TREE_TYPE (t));
7619 return build_fold_addr_expr_with_type_1 (t, ptrtype, true);
7622 /* Fold a unary expression of code CODE and type TYPE with operand
7623 OP0. Return the folded expression if folding is successful.
7624 Otherwise, return NULL_TREE. */
7627 fold_unary (enum tree_code code, tree type, tree op0)
7631 enum tree_code_class kind = TREE_CODE_CLASS (code);
7633 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7634 && TREE_CODE_LENGTH (code) == 1);
7639 if (CONVERT_EXPR_CODE_P (code)
7640 || code == FLOAT_EXPR || code == ABS_EXPR)
7642 /* Don't use STRIP_NOPS, because signedness of argument type
7644 STRIP_SIGN_NOPS (arg0);
7648 /* Strip any conversions that don't change the mode. This
7649 is safe for every expression, except for a comparison
7650 expression because its signedness is derived from its
7653 Note that this is done as an internal manipulation within
7654 the constant folder, in order to find the simplest
7655 representation of the arguments so that their form can be
7656 studied. In any cases, the appropriate type conversions
7657 should be put back in the tree that will get out of the
7663 if (TREE_CODE_CLASS (code) == tcc_unary)
7665 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7666 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7667 fold_build1 (code, type, TREE_OPERAND (arg0, 1)));
7668 else if (TREE_CODE (arg0) == COND_EXPR)
7670 tree arg01 = TREE_OPERAND (arg0, 1);
7671 tree arg02 = TREE_OPERAND (arg0, 2);
7672 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
7673 arg01 = fold_build1 (code, type, arg01);
7674 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
7675 arg02 = fold_build1 (code, type, arg02);
7676 tem = fold_build3 (COND_EXPR, type, TREE_OPERAND (arg0, 0),
7679 /* If this was a conversion, and all we did was to move into
7680 inside the COND_EXPR, bring it back out. But leave it if
7681 it is a conversion from integer to integer and the
7682 result precision is no wider than a word since such a
7683 conversion is cheap and may be optimized away by combine,
7684 while it couldn't if it were outside the COND_EXPR. Then return
7685 so we don't get into an infinite recursion loop taking the
7686 conversion out and then back in. */
7688 if ((CONVERT_EXPR_CODE_P (code)
7689 || code == NON_LVALUE_EXPR)
7690 && TREE_CODE (tem) == COND_EXPR
7691 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
7692 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
7693 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
7694 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
7695 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
7696 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
7697 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7699 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
7700 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
7701 || flag_syntax_only))
7702 tem = build1 (code, type,
7704 TREE_TYPE (TREE_OPERAND
7705 (TREE_OPERAND (tem, 1), 0)),
7706 TREE_OPERAND (tem, 0),
7707 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
7708 TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
7711 else if (COMPARISON_CLASS_P (arg0))
7713 if (TREE_CODE (type) == BOOLEAN_TYPE)
7715 arg0 = copy_node (arg0);
7716 TREE_TYPE (arg0) = type;
7719 else if (TREE_CODE (type) != INTEGER_TYPE)
7720 return fold_build3 (COND_EXPR, type, arg0,
7721 fold_build1 (code, type,
7723 fold_build1 (code, type,
7724 integer_zero_node));
7731 /* Re-association barriers around constants and other re-association
7732 barriers can be removed. */
7733 if (CONSTANT_CLASS_P (op0)
7734 || TREE_CODE (op0) == PAREN_EXPR)
7735 return fold_convert (type, op0);
7740 case FIX_TRUNC_EXPR:
7741 if (TREE_TYPE (op0) == type)
7744 /* If we have (type) (a CMP b) and type is an integral type, return
7745 new expression involving the new type. */
7746 if (COMPARISON_CLASS_P (op0) && INTEGRAL_TYPE_P (type))
7747 return fold_build2 (TREE_CODE (op0), type, TREE_OPERAND (op0, 0),
7748 TREE_OPERAND (op0, 1));
7750 /* Handle cases of two conversions in a row. */
7751 if (CONVERT_EXPR_P (op0))
7753 tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
7754 tree inter_type = TREE_TYPE (op0);
7755 int inside_int = INTEGRAL_TYPE_P (inside_type);
7756 int inside_ptr = POINTER_TYPE_P (inside_type);
7757 int inside_float = FLOAT_TYPE_P (inside_type);
7758 int inside_vec = TREE_CODE (inside_type) == VECTOR_TYPE;
7759 unsigned int inside_prec = TYPE_PRECISION (inside_type);
7760 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
7761 int inter_int = INTEGRAL_TYPE_P (inter_type);
7762 int inter_ptr = POINTER_TYPE_P (inter_type);
7763 int inter_float = FLOAT_TYPE_P (inter_type);
7764 int inter_vec = TREE_CODE (inter_type) == VECTOR_TYPE;
7765 unsigned int inter_prec = TYPE_PRECISION (inter_type);
7766 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
7767 int final_int = INTEGRAL_TYPE_P (type);
7768 int final_ptr = POINTER_TYPE_P (type);
7769 int final_float = FLOAT_TYPE_P (type);
7770 int final_vec = TREE_CODE (type) == VECTOR_TYPE;
7771 unsigned int final_prec = TYPE_PRECISION (type);
7772 int final_unsignedp = TYPE_UNSIGNED (type);
7774 /* In addition to the cases of two conversions in a row
7775 handled below, if we are converting something to its own
7776 type via an object of identical or wider precision, neither
7777 conversion is needed. */
7778 if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
7779 && (((inter_int || inter_ptr) && final_int)
7780 || (inter_float && final_float))
7781 && inter_prec >= final_prec)
7782 return fold_build1 (code, type, TREE_OPERAND (op0, 0));
7784 /* Likewise, if the intermediate and final types are either both
7785 float or both integer, we don't need the middle conversion if
7786 it is wider than the final type and doesn't change the signedness
7787 (for integers). Avoid this if the final type is a pointer
7788 since then we sometimes need the inner conversion. Likewise if
7789 the outer has a precision not equal to the size of its mode. */
7790 if (((inter_int && inside_int)
7791 || (inter_float && inside_float)
7792 || (inter_vec && inside_vec))
7793 && inter_prec >= inside_prec
7794 && (inter_float || inter_vec
7795 || inter_unsignedp == inside_unsignedp)
7796 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7797 && TYPE_MODE (type) == TYPE_MODE (inter_type))
7799 && (! final_vec || inter_prec == inside_prec))
7800 return fold_build1 (code, type, TREE_OPERAND (op0, 0));
7802 /* If we have a sign-extension of a zero-extended value, we can
7803 replace that by a single zero-extension. */
7804 if (inside_int && inter_int && final_int
7805 && inside_prec < inter_prec && inter_prec < final_prec
7806 && inside_unsignedp && !inter_unsignedp)
7807 return fold_build1 (code, type, TREE_OPERAND (op0, 0));
7809 /* Two conversions in a row are not needed unless:
7810 - some conversion is floating-point (overstrict for now), or
7811 - some conversion is a vector (overstrict for now), or
7812 - the intermediate type is narrower than both initial and
7814 - the intermediate type and innermost type differ in signedness,
7815 and the outermost type is wider than the intermediate, or
7816 - the initial type is a pointer type and the precisions of the
7817 intermediate and final types differ, or
7818 - the final type is a pointer type and the precisions of the
7819 initial and intermediate types differ. */
7820 if (! inside_float && ! inter_float && ! final_float
7821 && ! inside_vec && ! inter_vec && ! final_vec
7822 && (inter_prec >= inside_prec || inter_prec >= final_prec)
7823 && ! (inside_int && inter_int
7824 && inter_unsignedp != inside_unsignedp
7825 && inter_prec < final_prec)
7826 && ((inter_unsignedp && inter_prec > inside_prec)
7827 == (final_unsignedp && final_prec > inter_prec))
7828 && ! (inside_ptr && inter_prec != final_prec)
7829 && ! (final_ptr && inside_prec != inter_prec)
7830 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
7831 && TYPE_MODE (type) == TYPE_MODE (inter_type)))
7832 return fold_build1 (code, type, TREE_OPERAND (op0, 0));
7835 /* Handle (T *)&A.B.C for A being of type T and B and C
7836 living at offset zero. This occurs frequently in
7837 C++ upcasting and then accessing the base. */
7838 if (TREE_CODE (op0) == ADDR_EXPR
7839 && POINTER_TYPE_P (type)
7840 && handled_component_p (TREE_OPERAND (op0, 0)))
7842 HOST_WIDE_INT bitsize, bitpos;
7844 enum machine_mode mode;
7845 int unsignedp, volatilep;
7846 tree base = TREE_OPERAND (op0, 0);
7847 base = get_inner_reference (base, &bitsize, &bitpos, &offset,
7848 &mode, &unsignedp, &volatilep, false);
7849 /* If the reference was to a (constant) zero offset, we can use
7850 the address of the base if it has the same base type
7851 as the result type. */
7852 if (! offset && bitpos == 0
7853 && TYPE_MAIN_VARIANT (TREE_TYPE (type))
7854 == TYPE_MAIN_VARIANT (TREE_TYPE (base)))
7855 return fold_convert (type, fold_addr_expr (base));
7858 if (TREE_CODE (op0) == MODIFY_EXPR
7859 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
7860 /* Detect assigning a bitfield. */
7861 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
7863 (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
7865 /* Don't leave an assignment inside a conversion
7866 unless assigning a bitfield. */
7867 tem = fold_build1 (code, type, TREE_OPERAND (op0, 1));
7868 /* First do the assignment, then return converted constant. */
7869 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), op0, tem);
7870 TREE_NO_WARNING (tem) = 1;
7871 TREE_USED (tem) = 1;
7875 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
7876 constants (if x has signed type, the sign bit cannot be set
7877 in c). This folds extension into the BIT_AND_EXPR.
7878 ??? We don't do it for BOOLEAN_TYPE or ENUMERAL_TYPE because they
7879 very likely don't have maximal range for their precision and this
7880 transformation effectively doesn't preserve non-maximal ranges. */
7881 if (TREE_CODE (type) == INTEGER_TYPE
7882 && TREE_CODE (op0) == BIT_AND_EXPR
7883 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
7886 tree and0 = TREE_OPERAND (and, 0), and1 = TREE_OPERAND (and, 1);
7889 if (TYPE_UNSIGNED (TREE_TYPE (and))
7890 || (TYPE_PRECISION (type)
7891 <= TYPE_PRECISION (TREE_TYPE (and))))
7893 else if (TYPE_PRECISION (TREE_TYPE (and1))
7894 <= HOST_BITS_PER_WIDE_INT
7895 && host_integerp (and1, 1))
7897 unsigned HOST_WIDE_INT cst;
7899 cst = tree_low_cst (and1, 1);
7900 cst &= (HOST_WIDE_INT) -1
7901 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
7902 change = (cst == 0);
7903 #ifdef LOAD_EXTEND_OP
7905 && !flag_syntax_only
7906 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
7909 tree uns = unsigned_type_for (TREE_TYPE (and0));
7910 and0 = fold_convert (uns, and0);
7911 and1 = fold_convert (uns, and1);
7917 tem = force_fit_type_double (type, TREE_INT_CST_LOW (and1),
7918 TREE_INT_CST_HIGH (and1), 0,
7919 TREE_OVERFLOW (and1));
7920 return fold_build2 (BIT_AND_EXPR, type,
7921 fold_convert (type, and0), tem);
7925 /* Convert (T1)(X p+ Y) into ((T1)X p+ Y), for pointer type,
7926 when one of the new casts will fold away. Conservatively we assume
7927 that this happens when X or Y is NOP_EXPR or Y is INTEGER_CST. */
7928 if (POINTER_TYPE_P (type)
7929 && TREE_CODE (arg0) == POINTER_PLUS_EXPR
7930 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7931 || TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
7932 || TREE_CODE (TREE_OPERAND (arg0, 1)) == NOP_EXPR))
7934 tree arg00 = TREE_OPERAND (arg0, 0);
7935 tree arg01 = TREE_OPERAND (arg0, 1);
7937 return fold_build2 (TREE_CODE (arg0), type, fold_convert (type, arg00),
7938 fold_convert (sizetype, arg01));
7941 /* Convert (T1)(~(T2)X) into ~(T1)X if T1 and T2 are integral types
7942 of the same precision, and X is an integer type not narrower than
7943 types T1 or T2, i.e. the cast (T2)X isn't an extension. */
7944 if (INTEGRAL_TYPE_P (type)
7945 && TREE_CODE (op0) == BIT_NOT_EXPR
7946 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7947 && CONVERT_EXPR_P (TREE_OPERAND (op0, 0))
7948 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0)))
7950 tem = TREE_OPERAND (TREE_OPERAND (op0, 0), 0);
7951 if (INTEGRAL_TYPE_P (TREE_TYPE (tem))
7952 && TYPE_PRECISION (type) <= TYPE_PRECISION (TREE_TYPE (tem)))
7953 return fold_build1 (BIT_NOT_EXPR, type, fold_convert (type, tem));
7956 /* Convert (T1)(X * Y) into (T1)X * (T1)Y if T1 is narrower than the
7957 type of X and Y (integer types only). */
7958 if (INTEGRAL_TYPE_P (type)
7959 && TREE_CODE (op0) == MULT_EXPR
7960 && INTEGRAL_TYPE_P (TREE_TYPE (op0))
7961 && TYPE_PRECISION (type) < TYPE_PRECISION (TREE_TYPE (op0)))
7963 /* Be careful not to introduce new overflows. */
7965 if (TYPE_OVERFLOW_WRAPS (type))
7968 mult_type = unsigned_type_for (type);
7970 tem = fold_build2 (MULT_EXPR, mult_type,
7971 fold_convert (mult_type, TREE_OPERAND (op0, 0)),
7972 fold_convert (mult_type, TREE_OPERAND (op0, 1)));
7973 return fold_convert (type, tem);
7976 tem = fold_convert_const (code, type, op0);
7977 return tem ? tem : NULL_TREE;
7979 case FIXED_CONVERT_EXPR:
7980 tem = fold_convert_const (code, type, arg0);
7981 return tem ? tem : NULL_TREE;
7983 case VIEW_CONVERT_EXPR:
7984 if (TREE_TYPE (op0) == type)
7986 if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
7987 return fold_build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
7989 /* For integral conversions with the same precision or pointer
7990 conversions use a NOP_EXPR instead. */
7991 if ((INTEGRAL_TYPE_P (type)
7992 || POINTER_TYPE_P (type))
7993 && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
7994 || POINTER_TYPE_P (TREE_TYPE (op0)))
7995 && TYPE_PRECISION (type) == TYPE_PRECISION (TREE_TYPE (op0))
7996 /* Do not muck with VIEW_CONVERT_EXPRs that convert from
7997 a sub-type to its base type as generated by the Ada FE. */
7998 && !(INTEGRAL_TYPE_P (TREE_TYPE (op0))
7999 && TREE_TYPE (TREE_TYPE (op0))))
8000 return fold_convert (type, op0);
8002 /* Strip inner integral conversions that do not change the precision. */
8003 if (CONVERT_EXPR_P (op0)
8004 && (INTEGRAL_TYPE_P (TREE_TYPE (op0))
8005 || POINTER_TYPE_P (TREE_TYPE (op0)))
8006 && (INTEGRAL_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0)))
8007 || POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (op0, 0))))
8008 && (TYPE_PRECISION (TREE_TYPE (op0))
8009 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (op0, 0)))))
8010 return fold_build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
8012 return fold_view_convert_expr (type, op0);
8015 tem = fold_negate_expr (arg0);
8017 return fold_convert (type, tem);
8021 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
8022 return fold_abs_const (arg0, type);
8023 else if (TREE_CODE (arg0) == NEGATE_EXPR)
8024 return fold_build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0));
8025 /* Convert fabs((double)float) into (double)fabsf(float). */
8026 else if (TREE_CODE (arg0) == NOP_EXPR
8027 && TREE_CODE (type) == REAL_TYPE)
8029 tree targ0 = strip_float_extensions (arg0);
8031 return fold_convert (type, fold_build1 (ABS_EXPR,
8035 /* ABS_EXPR<ABS_EXPR<x>> = ABS_EXPR<x> even if flag_wrapv is on. */
8036 else if (TREE_CODE (arg0) == ABS_EXPR)
8038 else if (tree_expr_nonnegative_p (arg0))
8041 /* Strip sign ops from argument. */
8042 if (TREE_CODE (type) == REAL_TYPE)
8044 tem = fold_strip_sign_ops (arg0);
8046 return fold_build1 (ABS_EXPR, type, fold_convert (type, tem));
8051 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8052 return fold_convert (type, arg0);
8053 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8055 tree itype = TREE_TYPE (type);
8056 tree rpart = fold_convert (itype, TREE_OPERAND (arg0, 0));
8057 tree ipart = fold_convert (itype, TREE_OPERAND (arg0, 1));
8058 return fold_build2 (COMPLEX_EXPR, type, rpart, negate_expr (ipart));
8060 if (TREE_CODE (arg0) == COMPLEX_CST)
8062 tree itype = TREE_TYPE (type);
8063 tree rpart = fold_convert (itype, TREE_REALPART (arg0));
8064 tree ipart = fold_convert (itype, TREE_IMAGPART (arg0));
8065 return build_complex (type, rpart, negate_expr (ipart));
8067 if (TREE_CODE (arg0) == CONJ_EXPR)
8068 return fold_convert (type, TREE_OPERAND (arg0, 0));
8072 if (TREE_CODE (arg0) == INTEGER_CST)
8073 return fold_not_const (arg0, type);
8074 else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
8075 return fold_convert (type, TREE_OPERAND (arg0, 0));
8076 /* Convert ~ (-A) to A - 1. */
8077 else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
8078 return fold_build2 (MINUS_EXPR, type,
8079 fold_convert (type, TREE_OPERAND (arg0, 0)),
8080 build_int_cst (type, 1));
8081 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
8082 else if (INTEGRAL_TYPE_P (type)
8083 && ((TREE_CODE (arg0) == MINUS_EXPR
8084 && integer_onep (TREE_OPERAND (arg0, 1)))
8085 || (TREE_CODE (arg0) == PLUS_EXPR
8086 && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
8087 return fold_build1 (NEGATE_EXPR, type,
8088 fold_convert (type, TREE_OPERAND (arg0, 0)));
8089 /* Convert ~(X ^ Y) to ~X ^ Y or X ^ ~Y if ~X or ~Y simplify. */
8090 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8091 && (tem = fold_unary (BIT_NOT_EXPR, type,
8093 TREE_OPERAND (arg0, 0)))))
8094 return fold_build2 (BIT_XOR_EXPR, type, tem,
8095 fold_convert (type, TREE_OPERAND (arg0, 1)));
8096 else if (TREE_CODE (arg0) == BIT_XOR_EXPR
8097 && (tem = fold_unary (BIT_NOT_EXPR, type,
8099 TREE_OPERAND (arg0, 1)))))
8100 return fold_build2 (BIT_XOR_EXPR, type,
8101 fold_convert (type, TREE_OPERAND (arg0, 0)), tem);
8102 /* Perform BIT_NOT_EXPR on each element individually. */
8103 else if (TREE_CODE (arg0) == VECTOR_CST)
8105 tree elements = TREE_VECTOR_CST_ELTS (arg0), elem, list = NULL_TREE;
8106 int count = TYPE_VECTOR_SUBPARTS (type), i;
8108 for (i = 0; i < count; i++)
8112 elem = TREE_VALUE (elements);
8113 elem = fold_unary (BIT_NOT_EXPR, TREE_TYPE (type), elem);
8114 if (elem == NULL_TREE)
8116 elements = TREE_CHAIN (elements);
8119 elem = build_int_cst (TREE_TYPE (type), -1);
8120 list = tree_cons (NULL_TREE, elem, list);
8123 return build_vector (type, nreverse (list));
8128 case TRUTH_NOT_EXPR:
8129 /* The argument to invert_truthvalue must have Boolean type. */
8130 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
8131 arg0 = fold_convert (boolean_type_node, arg0);
8133 /* Note that the operand of this must be an int
8134 and its values must be 0 or 1.
8135 ("true" is a fixed value perhaps depending on the language,
8136 but we don't handle values other than 1 correctly yet.) */
8137 tem = fold_truth_not_expr (arg0);
8140 return fold_convert (type, tem);
8143 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8144 return fold_convert (type, arg0);
8145 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8146 return omit_one_operand (type, TREE_OPERAND (arg0, 0),
8147 TREE_OPERAND (arg0, 1));
8148 if (TREE_CODE (arg0) == COMPLEX_CST)
8149 return fold_convert (type, TREE_REALPART (arg0));
8150 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8152 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8153 tem = fold_build2 (TREE_CODE (arg0), itype,
8154 fold_build1 (REALPART_EXPR, itype,
8155 TREE_OPERAND (arg0, 0)),
8156 fold_build1 (REALPART_EXPR, itype,
8157 TREE_OPERAND (arg0, 1)));
8158 return fold_convert (type, tem);
8160 if (TREE_CODE (arg0) == CONJ_EXPR)
8162 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8163 tem = fold_build1 (REALPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8164 return fold_convert (type, tem);
8166 if (TREE_CODE (arg0) == CALL_EXPR)
8168 tree fn = get_callee_fndecl (arg0);
8169 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8170 switch (DECL_FUNCTION_CODE (fn))
8172 CASE_FLT_FN (BUILT_IN_CEXPI):
8173 fn = mathfn_built_in (type, BUILT_IN_COS);
8175 return build_call_expr (fn, 1, CALL_EXPR_ARG (arg0, 0));
8185 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
8186 return fold_convert (type, integer_zero_node);
8187 if (TREE_CODE (arg0) == COMPLEX_EXPR)
8188 return omit_one_operand (type, TREE_OPERAND (arg0, 1),
8189 TREE_OPERAND (arg0, 0));
8190 if (TREE_CODE (arg0) == COMPLEX_CST)
8191 return fold_convert (type, TREE_IMAGPART (arg0));
8192 if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8194 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8195 tem = fold_build2 (TREE_CODE (arg0), itype,
8196 fold_build1 (IMAGPART_EXPR, itype,
8197 TREE_OPERAND (arg0, 0)),
8198 fold_build1 (IMAGPART_EXPR, itype,
8199 TREE_OPERAND (arg0, 1)));
8200 return fold_convert (type, tem);
8202 if (TREE_CODE (arg0) == CONJ_EXPR)
8204 tree itype = TREE_TYPE (TREE_TYPE (arg0));
8205 tem = fold_build1 (IMAGPART_EXPR, itype, TREE_OPERAND (arg0, 0));
8206 return fold_convert (type, negate_expr (tem));
8208 if (TREE_CODE (arg0) == CALL_EXPR)
8210 tree fn = get_callee_fndecl (arg0);
8211 if (fn && DECL_BUILT_IN_CLASS (fn) == BUILT_IN_NORMAL)
8212 switch (DECL_FUNCTION_CODE (fn))
8214 CASE_FLT_FN (BUILT_IN_CEXPI):
8215 fn = mathfn_built_in (type, BUILT_IN_SIN);
8217 return build_call_expr (fn, 1, CALL_EXPR_ARG (arg0, 0));
8228 } /* switch (code) */
8231 /* Fold a binary expression of code CODE and type TYPE with operands
8232 OP0 and OP1, containing either a MIN-MAX or a MAX-MIN combination.
8233 Return the folded expression if folding is successful. Otherwise,
8234 return NULL_TREE. */
8237 fold_minmax (enum tree_code code, tree type, tree op0, tree op1)
8239 enum tree_code compl_code;
8241 if (code == MIN_EXPR)
8242 compl_code = MAX_EXPR;
8243 else if (code == MAX_EXPR)
8244 compl_code = MIN_EXPR;
8248 /* MIN (MAX (a, b), b) == b. */
8249 if (TREE_CODE (op0) == compl_code
8250 && operand_equal_p (TREE_OPERAND (op0, 1), op1, 0))
8251 return omit_one_operand (type, op1, TREE_OPERAND (op0, 0));
8253 /* MIN (MAX (b, a), b) == b. */
8254 if (TREE_CODE (op0) == compl_code
8255 && operand_equal_p (TREE_OPERAND (op0, 0), op1, 0)
8256 && reorder_operands_p (TREE_OPERAND (op0, 1), op1))
8257 return omit_one_operand (type, op1, TREE_OPERAND (op0, 1));
8259 /* MIN (a, MAX (a, b)) == a. */
8260 if (TREE_CODE (op1) == compl_code
8261 && operand_equal_p (op0, TREE_OPERAND (op1, 0), 0)
8262 && reorder_operands_p (op0, TREE_OPERAND (op1, 1)))
8263 return omit_one_operand (type, op0, TREE_OPERAND (op1, 1));
8265 /* MIN (a, MAX (b, a)) == a. */
8266 if (TREE_CODE (op1) == compl_code
8267 && operand_equal_p (op0, TREE_OPERAND (op1, 1), 0)
8268 && reorder_operands_p (op0, TREE_OPERAND (op1, 0)))
8269 return omit_one_operand (type, op0, TREE_OPERAND (op1, 0));
8274 /* Helper that tries to canonicalize the comparison ARG0 CODE ARG1
8275 by changing CODE to reduce the magnitude of constants involved in
8276 ARG0 of the comparison.
8277 Returns a canonicalized comparison tree if a simplification was
8278 possible, otherwise returns NULL_TREE.
8279 Set *STRICT_OVERFLOW_P to true if the canonicalization is only
8280 valid if signed overflow is undefined. */
8283 maybe_canonicalize_comparison_1 (enum tree_code code, tree type,
8284 tree arg0, tree arg1,
8285 bool *strict_overflow_p)
8287 enum tree_code code0 = TREE_CODE (arg0);
8288 tree t, cst0 = NULL_TREE;
8292 /* Match A +- CST code arg1 and CST code arg1. We can change the
8293 first form only if overflow is undefined. */
8294 if (!((TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8295 /* In principle pointers also have undefined overflow behavior,
8296 but that causes problems elsewhere. */
8297 && !POINTER_TYPE_P (TREE_TYPE (arg0))
8298 && (code0 == MINUS_EXPR
8299 || code0 == PLUS_EXPR)
8300 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8301 || code0 == INTEGER_CST))
8304 /* Identify the constant in arg0 and its sign. */
8305 if (code0 == INTEGER_CST)
8308 cst0 = TREE_OPERAND (arg0, 1);
8309 sgn0 = tree_int_cst_sgn (cst0);
8311 /* Overflowed constants and zero will cause problems. */
8312 if (integer_zerop (cst0)
8313 || TREE_OVERFLOW (cst0))
8316 /* See if we can reduce the magnitude of the constant in
8317 arg0 by changing the comparison code. */
8318 if (code0 == INTEGER_CST)
8320 /* CST <= arg1 -> CST-1 < arg1. */
8321 if (code == LE_EXPR && sgn0 == 1)
8323 /* -CST < arg1 -> -CST-1 <= arg1. */
8324 else if (code == LT_EXPR && sgn0 == -1)
8326 /* CST > arg1 -> CST-1 >= arg1. */
8327 else if (code == GT_EXPR && sgn0 == 1)
8329 /* -CST >= arg1 -> -CST-1 > arg1. */
8330 else if (code == GE_EXPR && sgn0 == -1)
8334 /* arg1 code' CST' might be more canonical. */
8339 /* A - CST < arg1 -> A - CST-1 <= arg1. */
8341 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8343 /* A + CST > arg1 -> A + CST-1 >= arg1. */
8344 else if (code == GT_EXPR
8345 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8347 /* A + CST <= arg1 -> A + CST-1 < arg1. */
8348 else if (code == LE_EXPR
8349 && code0 == ((sgn0 == -1) ? MINUS_EXPR : PLUS_EXPR))
8351 /* A - CST >= arg1 -> A - CST-1 > arg1. */
8352 else if (code == GE_EXPR
8353 && code0 == ((sgn0 == -1) ? PLUS_EXPR : MINUS_EXPR))
8357 *strict_overflow_p = true;
8360 /* Now build the constant reduced in magnitude. But not if that
8361 would produce one outside of its types range. */
8362 if (INTEGRAL_TYPE_P (TREE_TYPE (cst0))
8364 && TYPE_MIN_VALUE (TREE_TYPE (cst0))
8365 && tree_int_cst_equal (cst0, TYPE_MIN_VALUE (TREE_TYPE (cst0))))
8367 && TYPE_MAX_VALUE (TREE_TYPE (cst0))
8368 && tree_int_cst_equal (cst0, TYPE_MAX_VALUE (TREE_TYPE (cst0))))))
8369 /* We cannot swap the comparison here as that would cause us to
8370 endlessly recurse. */
8373 t = int_const_binop (sgn0 == -1 ? PLUS_EXPR : MINUS_EXPR,
8374 cst0, build_int_cst (TREE_TYPE (cst0), 1), 0);
8375 if (code0 != INTEGER_CST)
8376 t = fold_build2 (code0, TREE_TYPE (arg0), TREE_OPERAND (arg0, 0), t);
8378 /* If swapping might yield to a more canonical form, do so. */
8380 return fold_build2 (swap_tree_comparison (code), type, arg1, t);
8382 return fold_build2 (code, type, t, arg1);
8385 /* Canonicalize the comparison ARG0 CODE ARG1 with type TYPE with undefined
8386 overflow further. Try to decrease the magnitude of constants involved
8387 by changing LE_EXPR and GE_EXPR to LT_EXPR and GT_EXPR or vice versa
8388 and put sole constants at the second argument position.
8389 Returns the canonicalized tree if changed, otherwise NULL_TREE. */
8392 maybe_canonicalize_comparison (enum tree_code code, tree type,
8393 tree arg0, tree arg1)
8396 bool strict_overflow_p;
8397 const char * const warnmsg = G_("assuming signed overflow does not occur "
8398 "when reducing constant in comparison");
8400 /* Try canonicalization by simplifying arg0. */
8401 strict_overflow_p = false;
8402 t = maybe_canonicalize_comparison_1 (code, type, arg0, arg1,
8403 &strict_overflow_p);
8406 if (strict_overflow_p)
8407 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8411 /* Try canonicalization by simplifying arg1 using the swapped
8413 code = swap_tree_comparison (code);
8414 strict_overflow_p = false;
8415 t = maybe_canonicalize_comparison_1 (code, type, arg1, arg0,
8416 &strict_overflow_p);
8417 if (t && strict_overflow_p)
8418 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_MAGNITUDE);
8422 /* Return whether BASE + OFFSET + BITPOS may wrap around the address
8423 space. This is used to avoid issuing overflow warnings for
8424 expressions like &p->x which can not wrap. */
8427 pointer_may_wrap_p (tree base, tree offset, HOST_WIDE_INT bitpos)
8429 unsigned HOST_WIDE_INT offset_low, total_low;
8430 HOST_WIDE_INT size, offset_high, total_high;
8432 if (!POINTER_TYPE_P (TREE_TYPE (base)))
8438 if (offset == NULL_TREE)
8443 else if (TREE_CODE (offset) != INTEGER_CST || TREE_OVERFLOW (offset))
8447 offset_low = TREE_INT_CST_LOW (offset);
8448 offset_high = TREE_INT_CST_HIGH (offset);
8451 if (add_double_with_sign (offset_low, offset_high,
8452 bitpos / BITS_PER_UNIT, 0,
8453 &total_low, &total_high,
8457 if (total_high != 0)
8460 size = int_size_in_bytes (TREE_TYPE (TREE_TYPE (base)));
8464 /* We can do slightly better for SIZE if we have an ADDR_EXPR of an
8466 if (TREE_CODE (base) == ADDR_EXPR)
8468 HOST_WIDE_INT base_size;
8470 base_size = int_size_in_bytes (TREE_TYPE (TREE_OPERAND (base, 0)));
8471 if (base_size > 0 && size < base_size)
8475 return total_low > (unsigned HOST_WIDE_INT) size;
8478 /* Subroutine of fold_binary. This routine performs all of the
8479 transformations that are common to the equality/inequality
8480 operators (EQ_EXPR and NE_EXPR) and the ordering operators
8481 (LT_EXPR, LE_EXPR, GE_EXPR and GT_EXPR). Callers other than
8482 fold_binary should call fold_binary. Fold a comparison with
8483 tree code CODE and type TYPE with operands OP0 and OP1. Return
8484 the folded comparison or NULL_TREE. */
8487 fold_comparison (enum tree_code code, tree type, tree op0, tree op1)
8489 tree arg0, arg1, tem;
8494 STRIP_SIGN_NOPS (arg0);
8495 STRIP_SIGN_NOPS (arg1);
8497 tem = fold_relational_const (code, type, arg0, arg1);
8498 if (tem != NULL_TREE)
8501 /* If one arg is a real or integer constant, put it last. */
8502 if (tree_swap_operands_p (arg0, arg1, true))
8503 return fold_build2 (swap_tree_comparison (code), type, op1, op0);
8505 /* Transform comparisons of the form X +- C1 CMP C2 to X CMP C2 +- C1. */
8506 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8507 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8508 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
8509 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
8510 && (TREE_CODE (arg1) == INTEGER_CST
8511 && !TREE_OVERFLOW (arg1)))
8513 tree const1 = TREE_OPERAND (arg0, 1);
8515 tree variable = TREE_OPERAND (arg0, 0);
8518 lhs_add = TREE_CODE (arg0) != PLUS_EXPR;
8520 lhs = fold_build2 (lhs_add ? PLUS_EXPR : MINUS_EXPR,
8521 TREE_TYPE (arg1), const2, const1);
8523 /* If the constant operation overflowed this can be
8524 simplified as a comparison against INT_MAX/INT_MIN. */
8525 if (TREE_CODE (lhs) == INTEGER_CST
8526 && TREE_OVERFLOW (lhs))
8528 int const1_sgn = tree_int_cst_sgn (const1);
8529 enum tree_code code2 = code;
8531 /* Get the sign of the constant on the lhs if the
8532 operation were VARIABLE + CONST1. */
8533 if (TREE_CODE (arg0) == MINUS_EXPR)
8534 const1_sgn = -const1_sgn;
8536 /* The sign of the constant determines if we overflowed
8537 INT_MAX (const1_sgn == -1) or INT_MIN (const1_sgn == 1).
8538 Canonicalize to the INT_MIN overflow by swapping the comparison
8540 if (const1_sgn == -1)
8541 code2 = swap_tree_comparison (code);
8543 /* We now can look at the canonicalized case
8544 VARIABLE + 1 CODE2 INT_MIN
8545 and decide on the result. */
8546 if (code2 == LT_EXPR
8548 || code2 == EQ_EXPR)
8549 return omit_one_operand (type, boolean_false_node, variable);
8550 else if (code2 == NE_EXPR
8552 || code2 == GT_EXPR)
8553 return omit_one_operand (type, boolean_true_node, variable);
8556 if (TREE_CODE (lhs) == TREE_CODE (arg1)
8557 && (TREE_CODE (lhs) != INTEGER_CST
8558 || !TREE_OVERFLOW (lhs)))
8560 fold_overflow_warning (("assuming signed overflow does not occur "
8561 "when changing X +- C1 cmp C2 to "
8563 WARN_STRICT_OVERFLOW_COMPARISON);
8564 return fold_build2 (code, type, variable, lhs);
8568 /* For comparisons of pointers we can decompose it to a compile time
8569 comparison of the base objects and the offsets into the object.
8570 This requires at least one operand being an ADDR_EXPR or a
8571 POINTER_PLUS_EXPR to do more than the operand_equal_p test below. */
8572 if (POINTER_TYPE_P (TREE_TYPE (arg0))
8573 && (TREE_CODE (arg0) == ADDR_EXPR
8574 || TREE_CODE (arg1) == ADDR_EXPR
8575 || TREE_CODE (arg0) == POINTER_PLUS_EXPR
8576 || TREE_CODE (arg1) == POINTER_PLUS_EXPR))
8578 tree base0, base1, offset0 = NULL_TREE, offset1 = NULL_TREE;
8579 HOST_WIDE_INT bitsize, bitpos0 = 0, bitpos1 = 0;
8580 enum machine_mode mode;
8581 int volatilep, unsignedp;
8582 bool indirect_base0 = false, indirect_base1 = false;
8584 /* Get base and offset for the access. Strip ADDR_EXPR for
8585 get_inner_reference, but put it back by stripping INDIRECT_REF
8586 off the base object if possible. indirect_baseN will be true
8587 if baseN is not an address but refers to the object itself. */
8589 if (TREE_CODE (arg0) == ADDR_EXPR)
8591 base0 = get_inner_reference (TREE_OPERAND (arg0, 0),
8592 &bitsize, &bitpos0, &offset0, &mode,
8593 &unsignedp, &volatilep, false);
8594 if (TREE_CODE (base0) == INDIRECT_REF)
8595 base0 = TREE_OPERAND (base0, 0);
8597 indirect_base0 = true;
8599 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
8601 base0 = TREE_OPERAND (arg0, 0);
8602 offset0 = TREE_OPERAND (arg0, 1);
8606 if (TREE_CODE (arg1) == ADDR_EXPR)
8608 base1 = get_inner_reference (TREE_OPERAND (arg1, 0),
8609 &bitsize, &bitpos1, &offset1, &mode,
8610 &unsignedp, &volatilep, false);
8611 if (TREE_CODE (base1) == INDIRECT_REF)
8612 base1 = TREE_OPERAND (base1, 0);
8614 indirect_base1 = true;
8616 else if (TREE_CODE (arg1) == POINTER_PLUS_EXPR)
8618 base1 = TREE_OPERAND (arg1, 0);
8619 offset1 = TREE_OPERAND (arg1, 1);
8622 /* If we have equivalent bases we might be able to simplify. */
8623 if (indirect_base0 == indirect_base1
8624 && operand_equal_p (base0, base1, 0))
8626 /* We can fold this expression to a constant if the non-constant
8627 offset parts are equal. */
8628 if ((offset0 == offset1
8629 || (offset0 && offset1
8630 && operand_equal_p (offset0, offset1, 0)))
8633 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8638 && bitpos0 != bitpos1
8639 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8640 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8641 fold_overflow_warning (("assuming pointer wraparound does not "
8642 "occur when comparing P +- C1 with "
8644 WARN_STRICT_OVERFLOW_CONDITIONAL);
8649 return build_int_cst (boolean_type_node, bitpos0 == bitpos1);
8651 return build_int_cst (boolean_type_node, bitpos0 != bitpos1);
8653 return build_int_cst (boolean_type_node, bitpos0 < bitpos1);
8655 return build_int_cst (boolean_type_node, bitpos0 <= bitpos1);
8657 return build_int_cst (boolean_type_node, bitpos0 >= bitpos1);
8659 return build_int_cst (boolean_type_node, bitpos0 > bitpos1);
8663 /* We can simplify the comparison to a comparison of the variable
8664 offset parts if the constant offset parts are equal.
8665 Be careful to use signed size type here because otherwise we
8666 mess with array offsets in the wrong way. This is possible
8667 because pointer arithmetic is restricted to retain within an
8668 object and overflow on pointer differences is undefined as of
8669 6.5.6/8 and /9 with respect to the signed ptrdiff_t. */
8670 else if (bitpos0 == bitpos1
8671 && ((code == EQ_EXPR || code == NE_EXPR)
8672 || POINTER_TYPE_OVERFLOW_UNDEFINED))
8674 tree signed_size_type_node;
8675 signed_size_type_node = signed_type_for (size_type_node);
8677 /* By converting to signed size type we cover middle-end pointer
8678 arithmetic which operates on unsigned pointer types of size
8679 type size and ARRAY_REF offsets which are properly sign or
8680 zero extended from their type in case it is narrower than
8682 if (offset0 == NULL_TREE)
8683 offset0 = build_int_cst (signed_size_type_node, 0);
8685 offset0 = fold_convert (signed_size_type_node, offset0);
8686 if (offset1 == NULL_TREE)
8687 offset1 = build_int_cst (signed_size_type_node, 0);
8689 offset1 = fold_convert (signed_size_type_node, offset1);
8693 && (pointer_may_wrap_p (base0, offset0, bitpos0)
8694 || pointer_may_wrap_p (base1, offset1, bitpos1)))
8695 fold_overflow_warning (("assuming pointer wraparound does not "
8696 "occur when comparing P +- C1 with "
8698 WARN_STRICT_OVERFLOW_COMPARISON);
8700 return fold_build2 (code, type, offset0, offset1);
8703 /* For non-equal bases we can simplify if they are addresses
8704 of local binding decls or constants. */
8705 else if (indirect_base0 && indirect_base1
8706 /* We know that !operand_equal_p (base0, base1, 0)
8707 because the if condition was false. But make
8708 sure two decls are not the same. */
8710 && TREE_CODE (arg0) == ADDR_EXPR
8711 && TREE_CODE (arg1) == ADDR_EXPR
8712 && (((TREE_CODE (base0) == VAR_DECL
8713 || TREE_CODE (base0) == PARM_DECL)
8714 && (targetm.binds_local_p (base0)
8715 || CONSTANT_CLASS_P (base1)))
8716 || CONSTANT_CLASS_P (base0))
8717 && (((TREE_CODE (base1) == VAR_DECL
8718 || TREE_CODE (base1) == PARM_DECL)
8719 && (targetm.binds_local_p (base1)
8720 || CONSTANT_CLASS_P (base0)))
8721 || CONSTANT_CLASS_P (base1)))
8723 if (code == EQ_EXPR)
8724 return omit_two_operands (type, boolean_false_node, arg0, arg1);
8725 else if (code == NE_EXPR)
8726 return omit_two_operands (type, boolean_true_node, arg0, arg1);
8728 /* For equal offsets we can simplify to a comparison of the
8730 else if (bitpos0 == bitpos1
8732 ? base0 != TREE_OPERAND (arg0, 0) : base0 != arg0)
8734 ? base1 != TREE_OPERAND (arg1, 0) : base1 != arg1)
8735 && ((offset0 == offset1)
8736 || (offset0 && offset1
8737 && operand_equal_p (offset0, offset1, 0))))
8740 base0 = fold_addr_expr (base0);
8742 base1 = fold_addr_expr (base1);
8743 return fold_build2 (code, type, base0, base1);
8747 /* Transform comparisons of the form X +- C1 CMP Y +- C2 to
8748 X CMP Y +- C2 +- C1 for signed X, Y. This is valid if
8749 the resulting offset is smaller in absolute value than the
8751 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8752 && (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
8753 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8754 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8755 && (TREE_CODE (arg1) == PLUS_EXPR || TREE_CODE (arg1) == MINUS_EXPR)
8756 && (TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8757 && !TREE_OVERFLOW (TREE_OPERAND (arg1, 1))))
8759 tree const1 = TREE_OPERAND (arg0, 1);
8760 tree const2 = TREE_OPERAND (arg1, 1);
8761 tree variable1 = TREE_OPERAND (arg0, 0);
8762 tree variable2 = TREE_OPERAND (arg1, 0);
8764 const char * const warnmsg = G_("assuming signed overflow does not "
8765 "occur when combining constants around "
8768 /* Put the constant on the side where it doesn't overflow and is
8769 of lower absolute value than before. */
8770 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8771 ? MINUS_EXPR : PLUS_EXPR,
8773 if (!TREE_OVERFLOW (cst)
8774 && tree_int_cst_compare (const2, cst) == tree_int_cst_sgn (const2))
8776 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8777 return fold_build2 (code, type,
8779 fold_build2 (TREE_CODE (arg1), TREE_TYPE (arg1),
8783 cst = int_const_binop (TREE_CODE (arg0) == TREE_CODE (arg1)
8784 ? MINUS_EXPR : PLUS_EXPR,
8786 if (!TREE_OVERFLOW (cst)
8787 && tree_int_cst_compare (const1, cst) == tree_int_cst_sgn (const1))
8789 fold_overflow_warning (warnmsg, WARN_STRICT_OVERFLOW_COMPARISON);
8790 return fold_build2 (code, type,
8791 fold_build2 (TREE_CODE (arg0), TREE_TYPE (arg0),
8797 /* Transform comparisons of the form X * C1 CMP 0 to X CMP 0 in the
8798 signed arithmetic case. That form is created by the compiler
8799 often enough for folding it to be of value. One example is in
8800 computing loop trip counts after Operator Strength Reduction. */
8801 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg0))
8802 && TREE_CODE (arg0) == MULT_EXPR
8803 && (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8804 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1)))
8805 && integer_zerop (arg1))
8807 tree const1 = TREE_OPERAND (arg0, 1);
8808 tree const2 = arg1; /* zero */
8809 tree variable1 = TREE_OPERAND (arg0, 0);
8810 enum tree_code cmp_code = code;
8812 gcc_assert (!integer_zerop (const1));
8814 fold_overflow_warning (("assuming signed overflow does not occur when "
8815 "eliminating multiplication in comparison "
8817 WARN_STRICT_OVERFLOW_COMPARISON);
8819 /* If const1 is negative we swap the sense of the comparison. */
8820 if (tree_int_cst_sgn (const1) < 0)
8821 cmp_code = swap_tree_comparison (cmp_code);
8823 return fold_build2 (cmp_code, type, variable1, const2);
8826 tem = maybe_canonicalize_comparison (code, type, op0, op1);
8830 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
8832 tree targ0 = strip_float_extensions (arg0);
8833 tree targ1 = strip_float_extensions (arg1);
8834 tree newtype = TREE_TYPE (targ0);
8836 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
8837 newtype = TREE_TYPE (targ1);
8839 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
8840 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
8841 return fold_build2 (code, type, fold_convert (newtype, targ0),
8842 fold_convert (newtype, targ1));
8844 /* (-a) CMP (-b) -> b CMP a */
8845 if (TREE_CODE (arg0) == NEGATE_EXPR
8846 && TREE_CODE (arg1) == NEGATE_EXPR)
8847 return fold_build2 (code, type, TREE_OPERAND (arg1, 0),
8848 TREE_OPERAND (arg0, 0));
8850 if (TREE_CODE (arg1) == REAL_CST)
8852 REAL_VALUE_TYPE cst;
8853 cst = TREE_REAL_CST (arg1);
8855 /* (-a) CMP CST -> a swap(CMP) (-CST) */
8856 if (TREE_CODE (arg0) == NEGATE_EXPR)
8857 return fold_build2 (swap_tree_comparison (code), type,
8858 TREE_OPERAND (arg0, 0),
8859 build_real (TREE_TYPE (arg1),
8860 REAL_VALUE_NEGATE (cst)));
8862 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
8863 /* a CMP (-0) -> a CMP 0 */
8864 if (REAL_VALUE_MINUS_ZERO (cst))
8865 return fold_build2 (code, type, arg0,
8866 build_real (TREE_TYPE (arg1), dconst0));
8868 /* x != NaN is always true, other ops are always false. */
8869 if (REAL_VALUE_ISNAN (cst)
8870 && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
8872 tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
8873 return omit_one_operand (type, tem, arg0);
8876 /* Fold comparisons against infinity. */
8877 if (REAL_VALUE_ISINF (cst))
8879 tem = fold_inf_compare (code, type, arg0, arg1);
8880 if (tem != NULL_TREE)
8885 /* If this is a comparison of a real constant with a PLUS_EXPR
8886 or a MINUS_EXPR of a real constant, we can convert it into a
8887 comparison with a revised real constant as long as no overflow
8888 occurs when unsafe_math_optimizations are enabled. */
8889 if (flag_unsafe_math_optimizations
8890 && TREE_CODE (arg1) == REAL_CST
8891 && (TREE_CODE (arg0) == PLUS_EXPR
8892 || TREE_CODE (arg0) == MINUS_EXPR)
8893 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
8894 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
8895 ? MINUS_EXPR : PLUS_EXPR,
8896 arg1, TREE_OPERAND (arg0, 1), 0))
8897 && !TREE_OVERFLOW (tem))
8898 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
8900 /* Likewise, we can simplify a comparison of a real constant with
8901 a MINUS_EXPR whose first operand is also a real constant, i.e.
8902 (c1 - x) < c2 becomes x > c1-c2. Reordering is allowed on
8903 floating-point types only if -fassociative-math is set. */
8904 if (flag_associative_math
8905 && TREE_CODE (arg1) == REAL_CST
8906 && TREE_CODE (arg0) == MINUS_EXPR
8907 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
8908 && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
8910 && !TREE_OVERFLOW (tem))
8911 return fold_build2 (swap_tree_comparison (code), type,
8912 TREE_OPERAND (arg0, 1), tem);
8914 /* Fold comparisons against built-in math functions. */
8915 if (TREE_CODE (arg1) == REAL_CST
8916 && flag_unsafe_math_optimizations
8917 && ! flag_errno_math)
8919 enum built_in_function fcode = builtin_mathfn_code (arg0);
8921 if (fcode != END_BUILTINS)
8923 tem = fold_mathfn_compare (fcode, code, type, arg0, arg1);
8924 if (tem != NULL_TREE)
8930 if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
8931 && CONVERT_EXPR_P (arg0))
8933 /* If we are widening one operand of an integer comparison,
8934 see if the other operand is similarly being widened. Perhaps we
8935 can do the comparison in the narrower type. */
8936 tem = fold_widened_comparison (code, type, arg0, arg1);
8940 /* Or if we are changing signedness. */
8941 tem = fold_sign_changed_comparison (code, type, arg0, arg1);
8946 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
8947 constant, we can simplify it. */
8948 if (TREE_CODE (arg1) == INTEGER_CST
8949 && (TREE_CODE (arg0) == MIN_EXPR
8950 || TREE_CODE (arg0) == MAX_EXPR)
8951 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8953 tem = optimize_minmax_comparison (code, type, op0, op1);
8958 /* Simplify comparison of something with itself. (For IEEE
8959 floating-point, we can only do some of these simplifications.) */
8960 if (operand_equal_p (arg0, arg1, 0))
8965 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
8966 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
8967 return constant_boolean_node (1, type);
8972 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
8973 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
8974 return constant_boolean_node (1, type);
8975 return fold_build2 (EQ_EXPR, type, arg0, arg1);
8978 /* For NE, we can only do this simplification if integer
8979 or we don't honor IEEE floating point NaNs. */
8980 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
8981 && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
8983 /* ... fall through ... */
8986 return constant_boolean_node (0, type);
8992 /* If we are comparing an expression that just has comparisons
8993 of two integer values, arithmetic expressions of those comparisons,
8994 and constants, we can simplify it. There are only three cases
8995 to check: the two values can either be equal, the first can be
8996 greater, or the second can be greater. Fold the expression for
8997 those three values. Since each value must be 0 or 1, we have
8998 eight possibilities, each of which corresponds to the constant 0
8999 or 1 or one of the six possible comparisons.
9001 This handles common cases like (a > b) == 0 but also handles
9002 expressions like ((x > y) - (y > x)) > 0, which supposedly
9003 occur in macroized code. */
9005 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9007 tree cval1 = 0, cval2 = 0;
9010 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9011 /* Don't handle degenerate cases here; they should already
9012 have been handled anyway. */
9013 && cval1 != 0 && cval2 != 0
9014 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9015 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9016 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9017 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9018 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9019 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9020 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9022 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9023 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9025 /* We can't just pass T to eval_subst in case cval1 or cval2
9026 was the same as ARG1. */
9029 = fold_build2 (code, type,
9030 eval_subst (arg0, cval1, maxval,
9034 = fold_build2 (code, type,
9035 eval_subst (arg0, cval1, maxval,
9039 = fold_build2 (code, type,
9040 eval_subst (arg0, cval1, minval,
9044 /* All three of these results should be 0 or 1. Confirm they are.
9045 Then use those values to select the proper code to use. */
9047 if (TREE_CODE (high_result) == INTEGER_CST
9048 && TREE_CODE (equal_result) == INTEGER_CST
9049 && TREE_CODE (low_result) == INTEGER_CST)
9051 /* Make a 3-bit mask with the high-order bit being the
9052 value for `>', the next for '=', and the low for '<'. */
9053 switch ((integer_onep (high_result) * 4)
9054 + (integer_onep (equal_result) * 2)
9055 + integer_onep (low_result))
9059 return omit_one_operand (type, integer_zero_node, arg0);
9080 return omit_one_operand (type, integer_one_node, arg0);
9084 return save_expr (build2 (code, type, cval1, cval2));
9085 return fold_build2 (code, type, cval1, cval2);
9090 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9091 into a single range test. */
9092 if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9093 || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9094 && TREE_CODE (arg1) == INTEGER_CST
9095 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9096 && !integer_zerop (TREE_OPERAND (arg0, 1))
9097 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9098 && !TREE_OVERFLOW (arg1))
9100 tem = fold_div_compare (code, type, arg0, arg1);
9101 if (tem != NULL_TREE)
9105 /* Fold ~X op ~Y as Y op X. */
9106 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9107 && TREE_CODE (arg1) == BIT_NOT_EXPR)
9109 tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9110 return fold_build2 (code, type,
9111 fold_convert (cmp_type, TREE_OPERAND (arg1, 0)),
9112 TREE_OPERAND (arg0, 0));
9115 /* Fold ~X op C as X op' ~C, where op' is the swapped comparison. */
9116 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9117 && TREE_CODE (arg1) == INTEGER_CST)
9119 tree cmp_type = TREE_TYPE (TREE_OPERAND (arg0, 0));
9120 return fold_build2 (swap_tree_comparison (code), type,
9121 TREE_OPERAND (arg0, 0),
9122 fold_build1 (BIT_NOT_EXPR, cmp_type,
9123 fold_convert (cmp_type, arg1)));
9130 /* Subroutine of fold_binary. Optimize complex multiplications of the
9131 form z * conj(z), as pow(realpart(z),2) + pow(imagpart(z),2). The
9132 argument EXPR represents the expression "z" of type TYPE. */
9135 fold_mult_zconjz (tree type, tree expr)
9137 tree itype = TREE_TYPE (type);
9138 tree rpart, ipart, tem;
9140 if (TREE_CODE (expr) == COMPLEX_EXPR)
9142 rpart = TREE_OPERAND (expr, 0);
9143 ipart = TREE_OPERAND (expr, 1);
9145 else if (TREE_CODE (expr) == COMPLEX_CST)
9147 rpart = TREE_REALPART (expr);
9148 ipart = TREE_IMAGPART (expr);
9152 expr = save_expr (expr);
9153 rpart = fold_build1 (REALPART_EXPR, itype, expr);
9154 ipart = fold_build1 (IMAGPART_EXPR, itype, expr);
9157 rpart = save_expr (rpart);
9158 ipart = save_expr (ipart);
9159 tem = fold_build2 (PLUS_EXPR, itype,
9160 fold_build2 (MULT_EXPR, itype, rpart, rpart),
9161 fold_build2 (MULT_EXPR, itype, ipart, ipart));
9162 return fold_build2 (COMPLEX_EXPR, type, tem,
9163 fold_convert (itype, integer_zero_node));
9167 /* Subroutine of fold_binary. If P is the value of EXPR, computes
9168 power-of-two M and (arbitrary) N such that M divides (P-N). This condition
9169 guarantees that P and N have the same least significant log2(M) bits.
9170 N is not otherwise constrained. In particular, N is not normalized to
9171 0 <= N < M as is common. In general, the precise value of P is unknown.
9172 M is chosen as large as possible such that constant N can be determined.
9174 Returns M and sets *RESIDUE to N. */
9176 static unsigned HOST_WIDE_INT
9177 get_pointer_modulus_and_residue (tree expr, unsigned HOST_WIDE_INT *residue)
9179 enum tree_code code;
9183 code = TREE_CODE (expr);
9184 if (code == ADDR_EXPR)
9186 expr = TREE_OPERAND (expr, 0);
9187 if (handled_component_p (expr))
9189 HOST_WIDE_INT bitsize, bitpos;
9191 enum machine_mode mode;
9192 int unsignedp, volatilep;
9194 expr = get_inner_reference (expr, &bitsize, &bitpos, &offset,
9195 &mode, &unsignedp, &volatilep, false);
9196 *residue = bitpos / BITS_PER_UNIT;
9199 if (TREE_CODE (offset) == INTEGER_CST)
9200 *residue += TREE_INT_CST_LOW (offset);
9202 /* We don't handle more complicated offset expressions. */
9207 if (DECL_P (expr) && TREE_CODE (expr) != FUNCTION_DECL)
9208 return DECL_ALIGN_UNIT (expr);
9210 else if (code == POINTER_PLUS_EXPR)
9213 unsigned HOST_WIDE_INT modulus;
9214 enum tree_code inner_code;
9216 op0 = TREE_OPERAND (expr, 0);
9218 modulus = get_pointer_modulus_and_residue (op0, residue);
9220 op1 = TREE_OPERAND (expr, 1);
9222 inner_code = TREE_CODE (op1);
9223 if (inner_code == INTEGER_CST)
9225 *residue += TREE_INT_CST_LOW (op1);
9228 else if (inner_code == MULT_EXPR)
9230 op1 = TREE_OPERAND (op1, 1);
9231 if (TREE_CODE (op1) == INTEGER_CST)
9233 unsigned HOST_WIDE_INT align;
9235 /* Compute the greatest power-of-2 divisor of op1. */
9236 align = TREE_INT_CST_LOW (op1);
9239 /* If align is non-zero and less than *modulus, replace
9240 *modulus with align., If align is 0, then either op1 is 0
9241 or the greatest power-of-2 divisor of op1 doesn't fit in an
9242 unsigned HOST_WIDE_INT. In either case, no additional
9243 constraint is imposed. */
9245 modulus = MIN (modulus, align);
9252 /* If we get here, we were unable to determine anything useful about the
9258 /* Fold a binary expression of code CODE and type TYPE with operands
9259 OP0 and OP1. Return the folded expression if folding is
9260 successful. Otherwise, return NULL_TREE. */
9263 fold_binary (enum tree_code code, tree type, tree op0, tree op1)
9265 enum tree_code_class kind = TREE_CODE_CLASS (code);
9266 tree arg0, arg1, tem;
9267 tree t1 = NULL_TREE;
9268 bool strict_overflow_p;
9270 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9271 && TREE_CODE_LENGTH (code) == 2
9273 && op1 != NULL_TREE);
9278 /* Strip any conversions that don't change the mode. This is
9279 safe for every expression, except for a comparison expression
9280 because its signedness is derived from its operands. So, in
9281 the latter case, only strip conversions that don't change the
9282 signedness. MIN_EXPR/MAX_EXPR also need signedness of arguments
9285 Note that this is done as an internal manipulation within the
9286 constant folder, in order to find the simplest representation
9287 of the arguments so that their form can be studied. In any
9288 cases, the appropriate type conversions should be put back in
9289 the tree that will get out of the constant folder. */
9291 if (kind == tcc_comparison || code == MIN_EXPR || code == MAX_EXPR)
9293 STRIP_SIGN_NOPS (arg0);
9294 STRIP_SIGN_NOPS (arg1);
9302 /* Note that TREE_CONSTANT isn't enough: static var addresses are
9303 constant but we can't do arithmetic on them. */
9304 if ((TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9305 || (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
9306 || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == FIXED_CST)
9307 || (TREE_CODE (arg0) == FIXED_CST && TREE_CODE (arg1) == INTEGER_CST)
9308 || (TREE_CODE (arg0) == COMPLEX_CST && TREE_CODE (arg1) == COMPLEX_CST)
9309 || (TREE_CODE (arg0) == VECTOR_CST && TREE_CODE (arg1) == VECTOR_CST))
9311 if (kind == tcc_binary)
9313 /* Make sure type and arg0 have the same saturating flag. */
9314 gcc_assert (TYPE_SATURATING (type)
9315 == TYPE_SATURATING (TREE_TYPE (arg0)));
9316 tem = const_binop (code, arg0, arg1, 0);
9318 else if (kind == tcc_comparison)
9319 tem = fold_relational_const (code, type, arg0, arg1);
9323 if (tem != NULL_TREE)
9325 if (TREE_TYPE (tem) != type)
9326 tem = fold_convert (type, tem);
9331 /* If this is a commutative operation, and ARG0 is a constant, move it
9332 to ARG1 to reduce the number of tests below. */
9333 if (commutative_tree_code (code)
9334 && tree_swap_operands_p (arg0, arg1, true))
9335 return fold_build2 (code, type, op1, op0);
9337 /* ARG0 is the first operand of EXPR, and ARG1 is the second operand.
9339 First check for cases where an arithmetic operation is applied to a
9340 compound, conditional, or comparison operation. Push the arithmetic
9341 operation inside the compound or conditional to see if any folding
9342 can then be done. Convert comparison to conditional for this purpose.
9343 The also optimizes non-constant cases that used to be done in
9346 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
9347 one of the operands is a comparison and the other is a comparison, a
9348 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
9349 code below would make the expression more complex. Change it to a
9350 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
9351 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
9353 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
9354 || code == EQ_EXPR || code == NE_EXPR)
9355 && ((truth_value_p (TREE_CODE (arg0))
9356 && (truth_value_p (TREE_CODE (arg1))
9357 || (TREE_CODE (arg1) == BIT_AND_EXPR
9358 && integer_onep (TREE_OPERAND (arg1, 1)))))
9359 || (truth_value_p (TREE_CODE (arg1))
9360 && (truth_value_p (TREE_CODE (arg0))
9361 || (TREE_CODE (arg0) == BIT_AND_EXPR
9362 && integer_onep (TREE_OPERAND (arg0, 1)))))))
9364 tem = fold_build2 (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
9365 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
9368 fold_convert (boolean_type_node, arg0),
9369 fold_convert (boolean_type_node, arg1));
9371 if (code == EQ_EXPR)
9372 tem = invert_truthvalue (tem);
9374 return fold_convert (type, tem);
9377 if (TREE_CODE_CLASS (code) == tcc_binary
9378 || TREE_CODE_CLASS (code) == tcc_comparison)
9380 if (TREE_CODE (arg0) == COMPOUND_EXPR)
9381 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
9382 fold_build2 (code, type,
9383 fold_convert (TREE_TYPE (op0),
9384 TREE_OPERAND (arg0, 1)),
9386 if (TREE_CODE (arg1) == COMPOUND_EXPR
9387 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
9388 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
9389 fold_build2 (code, type, op0,
9390 fold_convert (TREE_TYPE (op1),
9391 TREE_OPERAND (arg1, 1))));
9393 if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
9395 tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
9397 /*cond_first_p=*/1);
9398 if (tem != NULL_TREE)
9402 if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
9404 tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
9406 /*cond_first_p=*/0);
9407 if (tem != NULL_TREE)
9414 case POINTER_PLUS_EXPR:
9415 /* 0 +p index -> (type)index */
9416 if (integer_zerop (arg0))
9417 return non_lvalue (fold_convert (type, arg1));
9419 /* PTR +p 0 -> PTR */
9420 if (integer_zerop (arg1))
9421 return non_lvalue (fold_convert (type, arg0));
9423 /* INT +p INT -> (PTR)(INT + INT). Stripping types allows for this. */
9424 if (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
9425 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9426 return fold_convert (type, fold_build2 (PLUS_EXPR, sizetype,
9427 fold_convert (sizetype, arg1),
9428 fold_convert (sizetype, arg0)));
9430 /* index +p PTR -> PTR +p index */
9431 if (POINTER_TYPE_P (TREE_TYPE (arg1))
9432 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9433 return fold_build2 (POINTER_PLUS_EXPR, type,
9434 fold_convert (type, arg1),
9435 fold_convert (sizetype, arg0));
9437 /* (PTR +p B) +p A -> PTR +p (B + A) */
9438 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9441 tree arg01 = fold_convert (sizetype, TREE_OPERAND (arg0, 1));
9442 tree arg00 = TREE_OPERAND (arg0, 0);
9443 inner = fold_build2 (PLUS_EXPR, sizetype,
9444 arg01, fold_convert (sizetype, arg1));
9445 return fold_convert (type,
9446 fold_build2 (POINTER_PLUS_EXPR,
9447 TREE_TYPE (arg00), arg00, inner));
9450 /* PTR_CST +p CST -> CST1 */
9451 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
9452 return fold_build2 (PLUS_EXPR, type, arg0, fold_convert (type, arg1));
9454 /* Try replacing &a[i1] +p c * i2 with &a[i1 + i2], if c is step
9455 of the array. Loop optimizer sometimes produce this type of
9457 if (TREE_CODE (arg0) == ADDR_EXPR)
9459 tem = try_move_mult_to_index (arg0, fold_convert (sizetype, arg1));
9461 return fold_convert (type, tem);
9467 /* PTR + INT -> (INT)(PTR p+ INT) */
9468 if (POINTER_TYPE_P (TREE_TYPE (arg0))
9469 && INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
9470 return fold_convert (type, fold_build2 (POINTER_PLUS_EXPR,
9473 fold_convert (sizetype, arg1)));
9474 /* INT + PTR -> (INT)(PTR p+ INT) */
9475 if (POINTER_TYPE_P (TREE_TYPE (arg1))
9476 && INTEGRAL_TYPE_P (TREE_TYPE (arg0)))
9477 return fold_convert (type, fold_build2 (POINTER_PLUS_EXPR,
9480 fold_convert (sizetype, arg0)));
9481 /* A + (-B) -> A - B */
9482 if (TREE_CODE (arg1) == NEGATE_EXPR)
9483 return fold_build2 (MINUS_EXPR, type,
9484 fold_convert (type, arg0),
9485 fold_convert (type, TREE_OPERAND (arg1, 0)));
9486 /* (-A) + B -> B - A */
9487 if (TREE_CODE (arg0) == NEGATE_EXPR
9488 && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
9489 return fold_build2 (MINUS_EXPR, type,
9490 fold_convert (type, arg1),
9491 fold_convert (type, TREE_OPERAND (arg0, 0)));
9493 if (INTEGRAL_TYPE_P (type))
9495 /* Convert ~A + 1 to -A. */
9496 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9497 && integer_onep (arg1))
9498 return fold_build1 (NEGATE_EXPR, type, TREE_OPERAND (arg0, 0));
9501 if (TREE_CODE (arg0) == BIT_NOT_EXPR
9502 && !TYPE_OVERFLOW_TRAPS (type))
9504 tree tem = TREE_OPERAND (arg0, 0);
9507 if (operand_equal_p (tem, arg1, 0))
9509 t1 = build_int_cst_type (type, -1);
9510 return omit_one_operand (type, t1, arg1);
9515 if (TREE_CODE (arg1) == BIT_NOT_EXPR
9516 && !TYPE_OVERFLOW_TRAPS (type))
9518 tree tem = TREE_OPERAND (arg1, 0);
9521 if (operand_equal_p (arg0, tem, 0))
9523 t1 = build_int_cst_type (type, -1);
9524 return omit_one_operand (type, t1, arg0);
9528 /* X + (X / CST) * -CST is X % CST. */
9529 if (TREE_CODE (arg1) == MULT_EXPR
9530 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9531 && operand_equal_p (arg0,
9532 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0))
9534 tree cst0 = TREE_OPERAND (TREE_OPERAND (arg1, 0), 1);
9535 tree cst1 = TREE_OPERAND (arg1, 1);
9536 tree sum = fold_binary (PLUS_EXPR, TREE_TYPE (cst1), cst1, cst0);
9537 if (sum && integer_zerop (sum))
9538 return fold_convert (type,
9539 fold_build2 (TRUNC_MOD_EXPR,
9540 TREE_TYPE (arg0), arg0, cst0));
9544 /* Handle (A1 * C1) + (A2 * C2) with A1, A2 or C1, C2 being the
9545 same or one. Make sure type is not saturating.
9546 fold_plusminus_mult_expr will re-associate. */
9547 if ((TREE_CODE (arg0) == MULT_EXPR
9548 || TREE_CODE (arg1) == MULT_EXPR)
9549 && !TYPE_SATURATING (type)
9550 && (!FLOAT_TYPE_P (type) || flag_associative_math))
9552 tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
9557 if (! FLOAT_TYPE_P (type))
9559 if (integer_zerop (arg1))
9560 return non_lvalue (fold_convert (type, arg0));
9562 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
9563 with a constant, and the two constants have no bits in common,
9564 we should treat this as a BIT_IOR_EXPR since this may produce more
9566 if (TREE_CODE (arg0) == BIT_AND_EXPR
9567 && TREE_CODE (arg1) == BIT_AND_EXPR
9568 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9569 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
9570 && integer_zerop (const_binop (BIT_AND_EXPR,
9571 TREE_OPERAND (arg0, 1),
9572 TREE_OPERAND (arg1, 1), 0)))
9574 code = BIT_IOR_EXPR;
9578 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
9579 (plus (plus (mult) (mult)) (foo)) so that we can
9580 take advantage of the factoring cases below. */
9581 if (((TREE_CODE (arg0) == PLUS_EXPR
9582 || TREE_CODE (arg0) == MINUS_EXPR)
9583 && TREE_CODE (arg1) == MULT_EXPR)
9584 || ((TREE_CODE (arg1) == PLUS_EXPR
9585 || TREE_CODE (arg1) == MINUS_EXPR)
9586 && TREE_CODE (arg0) == MULT_EXPR))
9588 tree parg0, parg1, parg, marg;
9589 enum tree_code pcode;
9591 if (TREE_CODE (arg1) == MULT_EXPR)
9592 parg = arg0, marg = arg1;
9594 parg = arg1, marg = arg0;
9595 pcode = TREE_CODE (parg);
9596 parg0 = TREE_OPERAND (parg, 0);
9597 parg1 = TREE_OPERAND (parg, 1);
9601 if (TREE_CODE (parg0) == MULT_EXPR
9602 && TREE_CODE (parg1) != MULT_EXPR)
9603 return fold_build2 (pcode, type,
9604 fold_build2 (PLUS_EXPR, type,
9605 fold_convert (type, parg0),
9606 fold_convert (type, marg)),
9607 fold_convert (type, parg1));
9608 if (TREE_CODE (parg0) != MULT_EXPR
9609 && TREE_CODE (parg1) == MULT_EXPR)
9610 return fold_build2 (PLUS_EXPR, type,
9611 fold_convert (type, parg0),
9612 fold_build2 (pcode, type,
9613 fold_convert (type, marg),
9620 /* See if ARG1 is zero and X + ARG1 reduces to X. */
9621 if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
9622 return non_lvalue (fold_convert (type, arg0));
9624 /* Likewise if the operands are reversed. */
9625 if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
9626 return non_lvalue (fold_convert (type, arg1));
9628 /* Convert X + -C into X - C. */
9629 if (TREE_CODE (arg1) == REAL_CST
9630 && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
9632 tem = fold_negate_const (arg1, type);
9633 if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
9634 return fold_build2 (MINUS_EXPR, type,
9635 fold_convert (type, arg0),
9636 fold_convert (type, tem));
9639 /* Fold __complex__ ( x, 0 ) + __complex__ ( 0, y )
9640 to __complex__ ( x, y ). This is not the same for SNaNs or
9641 if signed zeros are involved. */
9642 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
9643 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
9644 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
9646 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
9647 tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
9648 tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
9649 bool arg0rz = false, arg0iz = false;
9650 if ((arg0r && (arg0rz = real_zerop (arg0r)))
9651 || (arg0i && (arg0iz = real_zerop (arg0i))))
9653 tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
9654 tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
9655 if (arg0rz && arg1i && real_zerop (arg1i))
9657 tree rp = arg1r ? arg1r
9658 : build1 (REALPART_EXPR, rtype, arg1);
9659 tree ip = arg0i ? arg0i
9660 : build1 (IMAGPART_EXPR, rtype, arg0);
9661 return fold_build2 (COMPLEX_EXPR, type, rp, ip);
9663 else if (arg0iz && arg1r && real_zerop (arg1r))
9665 tree rp = arg0r ? arg0r
9666 : build1 (REALPART_EXPR, rtype, arg0);
9667 tree ip = arg1i ? arg1i
9668 : build1 (IMAGPART_EXPR, rtype, arg1);
9669 return fold_build2 (COMPLEX_EXPR, type, rp, ip);
9674 if (flag_unsafe_math_optimizations
9675 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
9676 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
9677 && (tem = distribute_real_division (code, type, arg0, arg1)))
9680 /* Convert x+x into x*2.0. */
9681 if (operand_equal_p (arg0, arg1, 0)
9682 && SCALAR_FLOAT_TYPE_P (type))
9683 return fold_build2 (MULT_EXPR, type, arg0,
9684 build_real (type, dconst2));
9686 /* Convert a + (b*c + d*e) into (a + b*c) + d*e.
9687 We associate floats only if the user has specified
9688 -fassociative-math. */
9689 if (flag_associative_math
9690 && TREE_CODE (arg1) == PLUS_EXPR
9691 && TREE_CODE (arg0) != MULT_EXPR)
9693 tree tree10 = TREE_OPERAND (arg1, 0);
9694 tree tree11 = TREE_OPERAND (arg1, 1);
9695 if (TREE_CODE (tree11) == MULT_EXPR
9696 && TREE_CODE (tree10) == MULT_EXPR)
9699 tree0 = fold_build2 (PLUS_EXPR, type, arg0, tree10);
9700 return fold_build2 (PLUS_EXPR, type, tree0, tree11);
9703 /* Convert (b*c + d*e) + a into b*c + (d*e +a).
9704 We associate floats only if the user has specified
9705 -fassociative-math. */
9706 if (flag_associative_math
9707 && TREE_CODE (arg0) == PLUS_EXPR
9708 && TREE_CODE (arg1) != MULT_EXPR)
9710 tree tree00 = TREE_OPERAND (arg0, 0);
9711 tree tree01 = TREE_OPERAND (arg0, 1);
9712 if (TREE_CODE (tree01) == MULT_EXPR
9713 && TREE_CODE (tree00) == MULT_EXPR)
9716 tree0 = fold_build2 (PLUS_EXPR, type, tree01, arg1);
9717 return fold_build2 (PLUS_EXPR, type, tree00, tree0);
9723 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
9724 is a rotate of A by C1 bits. */
9725 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
9726 is a rotate of A by B bits. */
9728 enum tree_code code0, code1;
9730 code0 = TREE_CODE (arg0);
9731 code1 = TREE_CODE (arg1);
9732 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
9733 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
9734 && operand_equal_p (TREE_OPERAND (arg0, 0),
9735 TREE_OPERAND (arg1, 0), 0)
9736 && (rtype = TREE_TYPE (TREE_OPERAND (arg0, 0)),
9737 TYPE_UNSIGNED (rtype))
9738 /* Only create rotates in complete modes. Other cases are not
9739 expanded properly. */
9740 && TYPE_PRECISION (rtype) == GET_MODE_PRECISION (TYPE_MODE (rtype)))
9742 tree tree01, tree11;
9743 enum tree_code code01, code11;
9745 tree01 = TREE_OPERAND (arg0, 1);
9746 tree11 = TREE_OPERAND (arg1, 1);
9747 STRIP_NOPS (tree01);
9748 STRIP_NOPS (tree11);
9749 code01 = TREE_CODE (tree01);
9750 code11 = TREE_CODE (tree11);
9751 if (code01 == INTEGER_CST
9752 && code11 == INTEGER_CST
9753 && TREE_INT_CST_HIGH (tree01) == 0
9754 && TREE_INT_CST_HIGH (tree11) == 0
9755 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
9756 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
9757 return build2 (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
9758 code0 == LSHIFT_EXPR ? tree01 : tree11);
9759 else if (code11 == MINUS_EXPR)
9761 tree tree110, tree111;
9762 tree110 = TREE_OPERAND (tree11, 0);
9763 tree111 = TREE_OPERAND (tree11, 1);
9764 STRIP_NOPS (tree110);
9765 STRIP_NOPS (tree111);
9766 if (TREE_CODE (tree110) == INTEGER_CST
9767 && 0 == compare_tree_int (tree110,
9769 (TREE_TYPE (TREE_OPERAND
9771 && operand_equal_p (tree01, tree111, 0))
9772 return build2 ((code0 == LSHIFT_EXPR
9775 type, TREE_OPERAND (arg0, 0), tree01);
9777 else if (code01 == MINUS_EXPR)
9779 tree tree010, tree011;
9780 tree010 = TREE_OPERAND (tree01, 0);
9781 tree011 = TREE_OPERAND (tree01, 1);
9782 STRIP_NOPS (tree010);
9783 STRIP_NOPS (tree011);
9784 if (TREE_CODE (tree010) == INTEGER_CST
9785 && 0 == compare_tree_int (tree010,
9787 (TREE_TYPE (TREE_OPERAND
9789 && operand_equal_p (tree11, tree011, 0))
9790 return build2 ((code0 != LSHIFT_EXPR
9793 type, TREE_OPERAND (arg0, 0), tree11);
9799 /* In most languages, can't associate operations on floats through
9800 parentheses. Rather than remember where the parentheses were, we
9801 don't associate floats at all, unless the user has specified
9803 And, we need to make sure type is not saturating. */
9805 if ((! FLOAT_TYPE_P (type) || flag_associative_math)
9806 && !TYPE_SATURATING (type))
9808 tree var0, con0, lit0, minus_lit0;
9809 tree var1, con1, lit1, minus_lit1;
9812 /* Split both trees into variables, constants, and literals. Then
9813 associate each group together, the constants with literals,
9814 then the result with variables. This increases the chances of
9815 literals being recombined later and of generating relocatable
9816 expressions for the sum of a constant and literal. */
9817 var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
9818 var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
9819 code == MINUS_EXPR);
9821 /* With undefined overflow we can only associate constants
9822 with one variable. */
9823 if (((POINTER_TYPE_P (type) && POINTER_TYPE_OVERFLOW_UNDEFINED)
9824 || (INTEGRAL_TYPE_P (type) && !TYPE_OVERFLOW_WRAPS (type)))
9830 if (TREE_CODE (tmp0) == NEGATE_EXPR)
9831 tmp0 = TREE_OPERAND (tmp0, 0);
9832 if (TREE_CODE (tmp1) == NEGATE_EXPR)
9833 tmp1 = TREE_OPERAND (tmp1, 0);
9834 /* The only case we can still associate with two variables
9835 is if they are the same, modulo negation. */
9836 if (!operand_equal_p (tmp0, tmp1, 0))
9840 /* Only do something if we found more than two objects. Otherwise,
9841 nothing has changed and we risk infinite recursion. */
9843 && (2 < ((var0 != 0) + (var1 != 0)
9844 + (con0 != 0) + (con1 != 0)
9845 + (lit0 != 0) + (lit1 != 0)
9846 + (minus_lit0 != 0) + (minus_lit1 != 0))))
9848 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
9849 if (code == MINUS_EXPR)
9852 var0 = associate_trees (var0, var1, code, type);
9853 con0 = associate_trees (con0, con1, code, type);
9854 lit0 = associate_trees (lit0, lit1, code, type);
9855 minus_lit0 = associate_trees (minus_lit0, minus_lit1, code, type);
9857 /* Preserve the MINUS_EXPR if the negative part of the literal is
9858 greater than the positive part. Otherwise, the multiplicative
9859 folding code (i.e extract_muldiv) may be fooled in case
9860 unsigned constants are subtracted, like in the following
9861 example: ((X*2 + 4) - 8U)/2. */
9862 if (minus_lit0 && lit0)
9864 if (TREE_CODE (lit0) == INTEGER_CST
9865 && TREE_CODE (minus_lit0) == INTEGER_CST
9866 && tree_int_cst_lt (lit0, minus_lit0))
9868 minus_lit0 = associate_trees (minus_lit0, lit0,
9874 lit0 = associate_trees (lit0, minus_lit0,
9882 return fold_convert (type,
9883 associate_trees (var0, minus_lit0,
9887 con0 = associate_trees (con0, minus_lit0,
9889 return fold_convert (type,
9890 associate_trees (var0, con0,
9895 con0 = associate_trees (con0, lit0, code, type);
9896 return fold_convert (type, associate_trees (var0, con0,
9904 /* Pointer simplifications for subtraction, simple reassociations. */
9905 if (POINTER_TYPE_P (TREE_TYPE (arg1)) && POINTER_TYPE_P (TREE_TYPE (arg0)))
9907 /* (PTR0 p+ A) - (PTR1 p+ B) -> (PTR0 - PTR1) + (A - B) */
9908 if (TREE_CODE (arg0) == POINTER_PLUS_EXPR
9909 && TREE_CODE (arg1) == POINTER_PLUS_EXPR)
9911 tree arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
9912 tree arg01 = fold_convert (type, TREE_OPERAND (arg0, 1));
9913 tree arg10 = fold_convert (type, TREE_OPERAND (arg1, 0));
9914 tree arg11 = fold_convert (type, TREE_OPERAND (arg1, 1));
9915 return fold_build2 (PLUS_EXPR, type,
9916 fold_build2 (MINUS_EXPR, type, arg00, arg10),
9917 fold_build2 (MINUS_EXPR, type, arg01, arg11));
9919 /* (PTR0 p+ A) - PTR1 -> (PTR0 - PTR1) + A, assuming PTR0 - PTR1 simplifies. */
9920 else if (TREE_CODE (arg0) == POINTER_PLUS_EXPR)
9922 tree arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
9923 tree arg01 = fold_convert (type, TREE_OPERAND (arg0, 1));
9924 tree tmp = fold_binary (MINUS_EXPR, type, arg00, fold_convert (type, arg1));
9926 return fold_build2 (PLUS_EXPR, type, tmp, arg01);
9929 /* A - (-B) -> A + B */
9930 if (TREE_CODE (arg1) == NEGATE_EXPR)
9931 return fold_build2 (PLUS_EXPR, type, op0,
9932 fold_convert (type, TREE_OPERAND (arg1, 0)));
9933 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
9934 if (TREE_CODE (arg0) == NEGATE_EXPR
9935 && (FLOAT_TYPE_P (type)
9936 || INTEGRAL_TYPE_P (type))
9937 && negate_expr_p (arg1)
9938 && reorder_operands_p (arg0, arg1))
9939 return fold_build2 (MINUS_EXPR, type,
9940 fold_convert (type, negate_expr (arg1)),
9941 fold_convert (type, TREE_OPERAND (arg0, 0)));
9942 /* Convert -A - 1 to ~A. */
9943 if (INTEGRAL_TYPE_P (type)
9944 && TREE_CODE (arg0) == NEGATE_EXPR
9945 && integer_onep (arg1)
9946 && !TYPE_OVERFLOW_TRAPS (type))
9947 return fold_build1 (BIT_NOT_EXPR, type,
9948 fold_convert (type, TREE_OPERAND (arg0, 0)));
9950 /* Convert -1 - A to ~A. */
9951 if (INTEGRAL_TYPE_P (type)
9952 && integer_all_onesp (arg0))
9953 return fold_build1 (BIT_NOT_EXPR, type, op1);
9956 /* X - (X / CST) * CST is X % CST. */
9957 if (INTEGRAL_TYPE_P (type)
9958 && TREE_CODE (arg1) == MULT_EXPR
9959 && TREE_CODE (TREE_OPERAND (arg1, 0)) == TRUNC_DIV_EXPR
9960 && operand_equal_p (arg0,
9961 TREE_OPERAND (TREE_OPERAND (arg1, 0), 0), 0)
9962 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg1, 0), 1),
9963 TREE_OPERAND (arg1, 1), 0))
9964 return fold_convert (type,
9965 fold_build2 (TRUNC_MOD_EXPR, TREE_TYPE (arg0),
9966 arg0, TREE_OPERAND (arg1, 1)));
9968 if (! FLOAT_TYPE_P (type))
9970 if (integer_zerop (arg0))
9971 return negate_expr (fold_convert (type, arg1));
9972 if (integer_zerop (arg1))
9973 return non_lvalue (fold_convert (type, arg0));
9975 /* Fold A - (A & B) into ~B & A. */
9976 if (!TREE_SIDE_EFFECTS (arg0)
9977 && TREE_CODE (arg1) == BIT_AND_EXPR)
9979 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
9981 tree arg10 = fold_convert (type, TREE_OPERAND (arg1, 0));
9982 return fold_build2 (BIT_AND_EXPR, type,
9983 fold_build1 (BIT_NOT_EXPR, type, arg10),
9984 fold_convert (type, arg0));
9986 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
9988 tree arg11 = fold_convert (type, TREE_OPERAND (arg1, 1));
9989 return fold_build2 (BIT_AND_EXPR, type,
9990 fold_build1 (BIT_NOT_EXPR, type, arg11),
9991 fold_convert (type, arg0));
9995 /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
9996 any power of 2 minus 1. */
9997 if (TREE_CODE (arg0) == BIT_AND_EXPR
9998 && TREE_CODE (arg1) == BIT_AND_EXPR
9999 && operand_equal_p (TREE_OPERAND (arg0, 0),
10000 TREE_OPERAND (arg1, 0), 0))
10002 tree mask0 = TREE_OPERAND (arg0, 1);
10003 tree mask1 = TREE_OPERAND (arg1, 1);
10004 tree tem = fold_build1 (BIT_NOT_EXPR, type, mask0);
10006 if (operand_equal_p (tem, mask1, 0))
10008 tem = fold_build2 (BIT_XOR_EXPR, type,
10009 TREE_OPERAND (arg0, 0), mask1);
10010 return fold_build2 (MINUS_EXPR, type, tem, mask1);
10015 /* See if ARG1 is zero and X - ARG1 reduces to X. */
10016 else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
10017 return non_lvalue (fold_convert (type, arg0));
10019 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
10020 ARG0 is zero and X + ARG0 reduces to X, since that would mean
10021 (-ARG1 + ARG0) reduces to -ARG1. */
10022 else if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
10023 return negate_expr (fold_convert (type, arg1));
10025 /* Fold __complex__ ( x, 0 ) - __complex__ ( 0, y ) to
10026 __complex__ ( x, -y ). This is not the same for SNaNs or if
10027 signed zeros are involved. */
10028 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10029 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10030 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0)))
10032 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10033 tree arg0r = fold_unary (REALPART_EXPR, rtype, arg0);
10034 tree arg0i = fold_unary (IMAGPART_EXPR, rtype, arg0);
10035 bool arg0rz = false, arg0iz = false;
10036 if ((arg0r && (arg0rz = real_zerop (arg0r)))
10037 || (arg0i && (arg0iz = real_zerop (arg0i))))
10039 tree arg1r = fold_unary (REALPART_EXPR, rtype, arg1);
10040 tree arg1i = fold_unary (IMAGPART_EXPR, rtype, arg1);
10041 if (arg0rz && arg1i && real_zerop (arg1i))
10043 tree rp = fold_build1 (NEGATE_EXPR, rtype,
10045 : build1 (REALPART_EXPR, rtype, arg1));
10046 tree ip = arg0i ? arg0i
10047 : build1 (IMAGPART_EXPR, rtype, arg0);
10048 return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10050 else if (arg0iz && arg1r && real_zerop (arg1r))
10052 tree rp = arg0r ? arg0r
10053 : build1 (REALPART_EXPR, rtype, arg0);
10054 tree ip = fold_build1 (NEGATE_EXPR, rtype,
10056 : build1 (IMAGPART_EXPR, rtype, arg1));
10057 return fold_build2 (COMPLEX_EXPR, type, rp, ip);
10062 /* Fold &x - &x. This can happen from &x.foo - &x.
10063 This is unsafe for certain floats even in non-IEEE formats.
10064 In IEEE, it is unsafe because it does wrong for NaNs.
10065 Also note that operand_equal_p is always false if an operand
10068 if ((!FLOAT_TYPE_P (type) || !HONOR_NANS (TYPE_MODE (type)))
10069 && operand_equal_p (arg0, arg1, 0))
10070 return fold_convert (type, integer_zero_node);
10072 /* A - B -> A + (-B) if B is easily negatable. */
10073 if (negate_expr_p (arg1)
10074 && ((FLOAT_TYPE_P (type)
10075 /* Avoid this transformation if B is a positive REAL_CST. */
10076 && (TREE_CODE (arg1) != REAL_CST
10077 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
10078 || INTEGRAL_TYPE_P (type)))
10079 return fold_build2 (PLUS_EXPR, type,
10080 fold_convert (type, arg0),
10081 fold_convert (type, negate_expr (arg1)));
10083 /* Try folding difference of addresses. */
10085 HOST_WIDE_INT diff;
10087 if ((TREE_CODE (arg0) == ADDR_EXPR
10088 || TREE_CODE (arg1) == ADDR_EXPR)
10089 && ptr_difference_const (arg0, arg1, &diff))
10090 return build_int_cst_type (type, diff);
10093 /* Fold &a[i] - &a[j] to i-j. */
10094 if (TREE_CODE (arg0) == ADDR_EXPR
10095 && TREE_CODE (TREE_OPERAND (arg0, 0)) == ARRAY_REF
10096 && TREE_CODE (arg1) == ADDR_EXPR
10097 && TREE_CODE (TREE_OPERAND (arg1, 0)) == ARRAY_REF)
10099 tree aref0 = TREE_OPERAND (arg0, 0);
10100 tree aref1 = TREE_OPERAND (arg1, 0);
10101 if (operand_equal_p (TREE_OPERAND (aref0, 0),
10102 TREE_OPERAND (aref1, 0), 0))
10104 tree op0 = fold_convert (type, TREE_OPERAND (aref0, 1));
10105 tree op1 = fold_convert (type, TREE_OPERAND (aref1, 1));
10106 tree esz = array_ref_element_size (aref0);
10107 tree diff = build2 (MINUS_EXPR, type, op0, op1);
10108 return fold_build2 (MULT_EXPR, type, diff,
10109 fold_convert (type, esz));
10114 if (flag_unsafe_math_optimizations
10115 && (TREE_CODE (arg0) == RDIV_EXPR || TREE_CODE (arg0) == MULT_EXPR)
10116 && (TREE_CODE (arg1) == RDIV_EXPR || TREE_CODE (arg1) == MULT_EXPR)
10117 && (tem = distribute_real_division (code, type, arg0, arg1)))
10120 /* Handle (A1 * C1) - (A2 * C2) with A1, A2 or C1, C2 being the
10121 same or one. Make sure type is not saturating.
10122 fold_plusminus_mult_expr will re-associate. */
10123 if ((TREE_CODE (arg0) == MULT_EXPR
10124 || TREE_CODE (arg1) == MULT_EXPR)
10125 && !TYPE_SATURATING (type)
10126 && (!FLOAT_TYPE_P (type) || flag_associative_math))
10128 tree tem = fold_plusminus_mult_expr (code, type, arg0, arg1);
10136 /* (-A) * (-B) -> A * B */
10137 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
10138 return fold_build2 (MULT_EXPR, type,
10139 fold_convert (type, TREE_OPERAND (arg0, 0)),
10140 fold_convert (type, negate_expr (arg1)));
10141 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
10142 return fold_build2 (MULT_EXPR, type,
10143 fold_convert (type, negate_expr (arg0)),
10144 fold_convert (type, TREE_OPERAND (arg1, 0)));
10146 if (! FLOAT_TYPE_P (type))
10148 if (integer_zerop (arg1))
10149 return omit_one_operand (type, arg1, arg0);
10150 if (integer_onep (arg1))
10151 return non_lvalue (fold_convert (type, arg0));
10152 /* Transform x * -1 into -x. Make sure to do the negation
10153 on the original operand with conversions not stripped
10154 because we can only strip non-sign-changing conversions. */
10155 if (integer_all_onesp (arg1))
10156 return fold_convert (type, negate_expr (op0));
10157 /* Transform x * -C into -x * C if x is easily negatable. */
10158 if (TREE_CODE (arg1) == INTEGER_CST
10159 && tree_int_cst_sgn (arg1) == -1
10160 && negate_expr_p (arg0)
10161 && (tem = negate_expr (arg1)) != arg1
10162 && !TREE_OVERFLOW (tem))
10163 return fold_build2 (MULT_EXPR, type,
10164 fold_convert (type, negate_expr (arg0)), tem);
10166 /* (a * (1 << b)) is (a << b) */
10167 if (TREE_CODE (arg1) == LSHIFT_EXPR
10168 && integer_onep (TREE_OPERAND (arg1, 0)))
10169 return fold_build2 (LSHIFT_EXPR, type, op0,
10170 TREE_OPERAND (arg1, 1));
10171 if (TREE_CODE (arg0) == LSHIFT_EXPR
10172 && integer_onep (TREE_OPERAND (arg0, 0)))
10173 return fold_build2 (LSHIFT_EXPR, type, op1,
10174 TREE_OPERAND (arg0, 1));
10176 /* (A + A) * C -> A * 2 * C */
10177 if (TREE_CODE (arg0) == PLUS_EXPR
10178 && TREE_CODE (arg1) == INTEGER_CST
10179 && operand_equal_p (TREE_OPERAND (arg0, 0),
10180 TREE_OPERAND (arg0, 1), 0))
10181 return fold_build2 (MULT_EXPR, type,
10182 omit_one_operand (type, TREE_OPERAND (arg0, 0),
10183 TREE_OPERAND (arg0, 1)),
10184 fold_build2 (MULT_EXPR, type,
10185 build_int_cst (type, 2) , arg1));
10187 strict_overflow_p = false;
10188 if (TREE_CODE (arg1) == INTEGER_CST
10189 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
10190 &strict_overflow_p)))
10192 if (strict_overflow_p)
10193 fold_overflow_warning (("assuming signed overflow does not "
10194 "occur when simplifying "
10196 WARN_STRICT_OVERFLOW_MISC);
10197 return fold_convert (type, tem);
10200 /* Optimize z * conj(z) for integer complex numbers. */
10201 if (TREE_CODE (arg0) == CONJ_EXPR
10202 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10203 return fold_mult_zconjz (type, arg1);
10204 if (TREE_CODE (arg1) == CONJ_EXPR
10205 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10206 return fold_mult_zconjz (type, arg0);
10210 /* Maybe fold x * 0 to 0. The expressions aren't the same
10211 when x is NaN, since x * 0 is also NaN. Nor are they the
10212 same in modes with signed zeros, since multiplying a
10213 negative value by 0 gives -0, not +0. */
10214 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10215 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10216 && real_zerop (arg1))
10217 return omit_one_operand (type, arg1, arg0);
10218 /* In IEEE floating point, x*1 is not equivalent to x for snans. */
10219 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10220 && real_onep (arg1))
10221 return non_lvalue (fold_convert (type, arg0));
10223 /* Transform x * -1.0 into -x. */
10224 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
10225 && real_minus_onep (arg1))
10226 return fold_convert (type, negate_expr (arg0));
10228 /* Convert (C1/X)*C2 into (C1*C2)/X. This transformation may change
10229 the result for floating point types due to rounding so it is applied
10230 only if -fassociative-math was specify. */
10231 if (flag_associative_math
10232 && TREE_CODE (arg0) == RDIV_EXPR
10233 && TREE_CODE (arg1) == REAL_CST
10234 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
10236 tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
10239 return fold_build2 (RDIV_EXPR, type, tem,
10240 TREE_OPERAND (arg0, 1));
10243 /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y. */
10244 if (operand_equal_p (arg0, arg1, 0))
10246 tree tem = fold_strip_sign_ops (arg0);
10247 if (tem != NULL_TREE)
10249 tem = fold_convert (type, tem);
10250 return fold_build2 (MULT_EXPR, type, tem, tem);
10254 /* Fold z * +-I to __complex__ (-+__imag z, +-__real z).
10255 This is not the same for NaNs or if signed zeros are
10257 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10258 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
10259 && COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10260 && TREE_CODE (arg1) == COMPLEX_CST
10261 && real_zerop (TREE_REALPART (arg1)))
10263 tree rtype = TREE_TYPE (TREE_TYPE (arg0));
10264 if (real_onep (TREE_IMAGPART (arg1)))
10265 return fold_build2 (COMPLEX_EXPR, type,
10266 negate_expr (fold_build1 (IMAGPART_EXPR,
10268 fold_build1 (REALPART_EXPR, rtype, arg0));
10269 else if (real_minus_onep (TREE_IMAGPART (arg1)))
10270 return fold_build2 (COMPLEX_EXPR, type,
10271 fold_build1 (IMAGPART_EXPR, rtype, arg0),
10272 negate_expr (fold_build1 (REALPART_EXPR,
10276 /* Optimize z * conj(z) for floating point complex numbers.
10277 Guarded by flag_unsafe_math_optimizations as non-finite
10278 imaginary components don't produce scalar results. */
10279 if (flag_unsafe_math_optimizations
10280 && TREE_CODE (arg0) == CONJ_EXPR
10281 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10282 return fold_mult_zconjz (type, arg1);
10283 if (flag_unsafe_math_optimizations
10284 && TREE_CODE (arg1) == CONJ_EXPR
10285 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10286 return fold_mult_zconjz (type, arg0);
10288 if (flag_unsafe_math_optimizations)
10290 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
10291 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
10293 /* Optimizations of root(...)*root(...). */
10294 if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
10297 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10298 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10300 /* Optimize sqrt(x)*sqrt(x) as x. */
10301 if (BUILTIN_SQRT_P (fcode0)
10302 && operand_equal_p (arg00, arg10, 0)
10303 && ! HONOR_SNANS (TYPE_MODE (type)))
10306 /* Optimize root(x)*root(y) as root(x*y). */
10307 rootfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10308 arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
10309 return build_call_expr (rootfn, 1, arg);
10312 /* Optimize expN(x)*expN(y) as expN(x+y). */
10313 if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
10315 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10316 tree arg = fold_build2 (PLUS_EXPR, type,
10317 CALL_EXPR_ARG (arg0, 0),
10318 CALL_EXPR_ARG (arg1, 0));
10319 return build_call_expr (expfn, 1, arg);
10322 /* Optimizations of pow(...)*pow(...). */
10323 if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
10324 || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
10325 || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
10327 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10328 tree arg01 = CALL_EXPR_ARG (arg0, 1);
10329 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10330 tree arg11 = CALL_EXPR_ARG (arg1, 1);
10332 /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y). */
10333 if (operand_equal_p (arg01, arg11, 0))
10335 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10336 tree arg = fold_build2 (MULT_EXPR, type, arg00, arg10);
10337 return build_call_expr (powfn, 2, arg, arg01);
10340 /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z). */
10341 if (operand_equal_p (arg00, arg10, 0))
10343 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10344 tree arg = fold_build2 (PLUS_EXPR, type, arg01, arg11);
10345 return build_call_expr (powfn, 2, arg00, arg);
10349 /* Optimize tan(x)*cos(x) as sin(x). */
10350 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
10351 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
10352 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
10353 || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
10354 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
10355 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
10356 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
10357 CALL_EXPR_ARG (arg1, 0), 0))
10359 tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
10361 if (sinfn != NULL_TREE)
10362 return build_call_expr (sinfn, 1, CALL_EXPR_ARG (arg0, 0));
10365 /* Optimize x*pow(x,c) as pow(x,c+1). */
10366 if (fcode1 == BUILT_IN_POW
10367 || fcode1 == BUILT_IN_POWF
10368 || fcode1 == BUILT_IN_POWL)
10370 tree arg10 = CALL_EXPR_ARG (arg1, 0);
10371 tree arg11 = CALL_EXPR_ARG (arg1, 1);
10372 if (TREE_CODE (arg11) == REAL_CST
10373 && !TREE_OVERFLOW (arg11)
10374 && operand_equal_p (arg0, arg10, 0))
10376 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
10380 c = TREE_REAL_CST (arg11);
10381 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10382 arg = build_real (type, c);
10383 return build_call_expr (powfn, 2, arg0, arg);
10387 /* Optimize pow(x,c)*x as pow(x,c+1). */
10388 if (fcode0 == BUILT_IN_POW
10389 || fcode0 == BUILT_IN_POWF
10390 || fcode0 == BUILT_IN_POWL)
10392 tree arg00 = CALL_EXPR_ARG (arg0, 0);
10393 tree arg01 = CALL_EXPR_ARG (arg0, 1);
10394 if (TREE_CODE (arg01) == REAL_CST
10395 && !TREE_OVERFLOW (arg01)
10396 && operand_equal_p (arg1, arg00, 0))
10398 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
10402 c = TREE_REAL_CST (arg01);
10403 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
10404 arg = build_real (type, c);
10405 return build_call_expr (powfn, 2, arg1, arg);
10409 /* Optimize x*x as pow(x,2.0), which is expanded as x*x. */
10410 if (optimize_function_for_speed_p (cfun)
10411 && operand_equal_p (arg0, arg1, 0))
10413 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
10417 tree arg = build_real (type, dconst2);
10418 return build_call_expr (powfn, 2, arg0, arg);
10427 if (integer_all_onesp (arg1))
10428 return omit_one_operand (type, arg1, arg0);
10429 if (integer_zerop (arg1))
10430 return non_lvalue (fold_convert (type, arg0));
10431 if (operand_equal_p (arg0, arg1, 0))
10432 return non_lvalue (fold_convert (type, arg0));
10434 /* ~X | X is -1. */
10435 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10436 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10438 t1 = fold_convert (type, integer_zero_node);
10439 t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10440 return omit_one_operand (type, t1, arg1);
10443 /* X | ~X is -1. */
10444 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10445 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10447 t1 = fold_convert (type, integer_zero_node);
10448 t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10449 return omit_one_operand (type, t1, arg0);
10452 /* Canonicalize (X & C1) | C2. */
10453 if (TREE_CODE (arg0) == BIT_AND_EXPR
10454 && TREE_CODE (arg1) == INTEGER_CST
10455 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10457 unsigned HOST_WIDE_INT hi1, lo1, hi2, lo2, hi3, lo3, mlo, mhi;
10458 int width = TYPE_PRECISION (type), w;
10459 hi1 = TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1));
10460 lo1 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
10461 hi2 = TREE_INT_CST_HIGH (arg1);
10462 lo2 = TREE_INT_CST_LOW (arg1);
10464 /* If (C1&C2) == C1, then (X&C1)|C2 becomes (X,C2). */
10465 if ((hi1 & hi2) == hi1 && (lo1 & lo2) == lo1)
10466 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10468 if (width > HOST_BITS_PER_WIDE_INT)
10470 mhi = (unsigned HOST_WIDE_INT) -1
10471 >> (2 * HOST_BITS_PER_WIDE_INT - width);
10477 mlo = (unsigned HOST_WIDE_INT) -1
10478 >> (HOST_BITS_PER_WIDE_INT - width);
10481 /* If (C1|C2) == ~0 then (X&C1)|C2 becomes X|C2. */
10482 if ((~(hi1 | hi2) & mhi) == 0 && (~(lo1 | lo2) & mlo) == 0)
10483 return fold_build2 (BIT_IOR_EXPR, type,
10484 TREE_OPERAND (arg0, 0), arg1);
10486 /* Minimize the number of bits set in C1, i.e. C1 := C1 & ~C2,
10487 unless (C1 & ~C2) | (C2 & C3) for some C3 is a mask of some
10488 mode which allows further optimizations. */
10495 for (w = BITS_PER_UNIT;
10496 w <= width && w <= HOST_BITS_PER_WIDE_INT;
10499 unsigned HOST_WIDE_INT mask
10500 = (unsigned HOST_WIDE_INT) -1 >> (HOST_BITS_PER_WIDE_INT - w);
10501 if (((lo1 | lo2) & mask) == mask
10502 && (lo1 & ~mask) == 0 && hi1 == 0)
10509 if (hi3 != hi1 || lo3 != lo1)
10510 return fold_build2 (BIT_IOR_EXPR, type,
10511 fold_build2 (BIT_AND_EXPR, type,
10512 TREE_OPERAND (arg0, 0),
10513 build_int_cst_wide (type,
10518 /* (X & Y) | Y is (X, Y). */
10519 if (TREE_CODE (arg0) == BIT_AND_EXPR
10520 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10521 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10522 /* (X & Y) | X is (Y, X). */
10523 if (TREE_CODE (arg0) == BIT_AND_EXPR
10524 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10525 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10526 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
10527 /* X | (X & Y) is (Y, X). */
10528 if (TREE_CODE (arg1) == BIT_AND_EXPR
10529 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10530 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10531 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
10532 /* X | (Y & X) is (Y, X). */
10533 if (TREE_CODE (arg1) == BIT_AND_EXPR
10534 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10535 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10536 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
10538 t1 = distribute_bit_expr (code, type, arg0, arg1);
10539 if (t1 != NULL_TREE)
10542 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
10544 This results in more efficient code for machines without a NAND
10545 instruction. Combine will canonicalize to the first form
10546 which will allow use of NAND instructions provided by the
10547 backend if they exist. */
10548 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10549 && TREE_CODE (arg1) == BIT_NOT_EXPR)
10551 return fold_build1 (BIT_NOT_EXPR, type,
10552 build2 (BIT_AND_EXPR, type,
10553 fold_convert (type,
10554 TREE_OPERAND (arg0, 0)),
10555 fold_convert (type,
10556 TREE_OPERAND (arg1, 0))));
10559 /* See if this can be simplified into a rotate first. If that
10560 is unsuccessful continue in the association code. */
10564 if (integer_zerop (arg1))
10565 return non_lvalue (fold_convert (type, arg0));
10566 if (integer_all_onesp (arg1))
10567 return fold_build1 (BIT_NOT_EXPR, type, op0);
10568 if (operand_equal_p (arg0, arg1, 0))
10569 return omit_one_operand (type, integer_zero_node, arg0);
10571 /* ~X ^ X is -1. */
10572 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10573 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10575 t1 = fold_convert (type, integer_zero_node);
10576 t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10577 return omit_one_operand (type, t1, arg1);
10580 /* X ^ ~X is -1. */
10581 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10582 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10584 t1 = fold_convert (type, integer_zero_node);
10585 t1 = fold_unary (BIT_NOT_EXPR, type, t1);
10586 return omit_one_operand (type, t1, arg0);
10589 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
10590 with a constant, and the two constants have no bits in common,
10591 we should treat this as a BIT_IOR_EXPR since this may produce more
10592 simplifications. */
10593 if (TREE_CODE (arg0) == BIT_AND_EXPR
10594 && TREE_CODE (arg1) == BIT_AND_EXPR
10595 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
10596 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
10597 && integer_zerop (const_binop (BIT_AND_EXPR,
10598 TREE_OPERAND (arg0, 1),
10599 TREE_OPERAND (arg1, 1), 0)))
10601 code = BIT_IOR_EXPR;
10605 /* (X | Y) ^ X -> Y & ~ X*/
10606 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10607 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10609 tree t2 = TREE_OPERAND (arg0, 1);
10610 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
10612 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10613 fold_convert (type, t1));
10617 /* (Y | X) ^ X -> Y & ~ X*/
10618 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10619 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10621 tree t2 = TREE_OPERAND (arg0, 0);
10622 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1),
10624 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10625 fold_convert (type, t1));
10629 /* X ^ (X | Y) -> Y & ~ X*/
10630 if (TREE_CODE (arg1) == BIT_IOR_EXPR
10631 && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
10633 tree t2 = TREE_OPERAND (arg1, 1);
10634 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
10636 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10637 fold_convert (type, t1));
10641 /* X ^ (Y | X) -> Y & ~ X*/
10642 if (TREE_CODE (arg1) == BIT_IOR_EXPR
10643 && operand_equal_p (TREE_OPERAND (arg1, 1), arg0, 0))
10645 tree t2 = TREE_OPERAND (arg1, 0);
10646 t1 = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg0),
10648 t1 = fold_build2 (BIT_AND_EXPR, type, fold_convert (type, t2),
10649 fold_convert (type, t1));
10653 /* Convert ~X ^ ~Y to X ^ Y. */
10654 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10655 && TREE_CODE (arg1) == BIT_NOT_EXPR)
10656 return fold_build2 (code, type,
10657 fold_convert (type, TREE_OPERAND (arg0, 0)),
10658 fold_convert (type, TREE_OPERAND (arg1, 0)));
10660 /* Convert ~X ^ C to X ^ ~C. */
10661 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10662 && TREE_CODE (arg1) == INTEGER_CST)
10663 return fold_build2 (code, type,
10664 fold_convert (type, TREE_OPERAND (arg0, 0)),
10665 fold_build1 (BIT_NOT_EXPR, type, arg1));
10667 /* Fold (X & 1) ^ 1 as (X & 1) == 0. */
10668 if (TREE_CODE (arg0) == BIT_AND_EXPR
10669 && integer_onep (TREE_OPERAND (arg0, 1))
10670 && integer_onep (arg1))
10671 return fold_build2 (EQ_EXPR, type, arg0,
10672 build_int_cst (TREE_TYPE (arg0), 0));
10674 /* Fold (X & Y) ^ Y as ~X & Y. */
10675 if (TREE_CODE (arg0) == BIT_AND_EXPR
10676 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10678 tem = fold_convert (type, TREE_OPERAND (arg0, 0));
10679 return fold_build2 (BIT_AND_EXPR, type,
10680 fold_build1 (BIT_NOT_EXPR, type, tem),
10681 fold_convert (type, arg1));
10683 /* Fold (X & Y) ^ X as ~Y & X. */
10684 if (TREE_CODE (arg0) == BIT_AND_EXPR
10685 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10686 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10688 tem = fold_convert (type, TREE_OPERAND (arg0, 1));
10689 return fold_build2 (BIT_AND_EXPR, type,
10690 fold_build1 (BIT_NOT_EXPR, type, tem),
10691 fold_convert (type, arg1));
10693 /* Fold X ^ (X & Y) as X & ~Y. */
10694 if (TREE_CODE (arg1) == BIT_AND_EXPR
10695 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10697 tem = fold_convert (type, TREE_OPERAND (arg1, 1));
10698 return fold_build2 (BIT_AND_EXPR, type,
10699 fold_convert (type, arg0),
10700 fold_build1 (BIT_NOT_EXPR, type, tem));
10702 /* Fold X ^ (Y & X) as ~Y & X. */
10703 if (TREE_CODE (arg1) == BIT_AND_EXPR
10704 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10705 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10707 tem = fold_convert (type, TREE_OPERAND (arg1, 0));
10708 return fold_build2 (BIT_AND_EXPR, type,
10709 fold_build1 (BIT_NOT_EXPR, type, tem),
10710 fold_convert (type, arg0));
10713 /* See if this can be simplified into a rotate first. If that
10714 is unsuccessful continue in the association code. */
10718 if (integer_all_onesp (arg1))
10719 return non_lvalue (fold_convert (type, arg0));
10720 if (integer_zerop (arg1))
10721 return omit_one_operand (type, arg1, arg0);
10722 if (operand_equal_p (arg0, arg1, 0))
10723 return non_lvalue (fold_convert (type, arg0));
10725 /* ~X & X is always zero. */
10726 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10727 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
10728 return omit_one_operand (type, integer_zero_node, arg1);
10730 /* X & ~X is always zero. */
10731 if (TREE_CODE (arg1) == BIT_NOT_EXPR
10732 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10733 return omit_one_operand (type, integer_zero_node, arg0);
10735 /* Canonicalize (X | C1) & C2 as (X & C2) | (C1 & C2). */
10736 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10737 && TREE_CODE (arg1) == INTEGER_CST
10738 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
10740 tree tmp1 = fold_convert (type, arg1);
10741 tree tmp2 = fold_convert (type, TREE_OPERAND (arg0, 0));
10742 tree tmp3 = fold_convert (type, TREE_OPERAND (arg0, 1));
10743 tmp2 = fold_build2 (BIT_AND_EXPR, type, tmp2, tmp1);
10744 tmp3 = fold_build2 (BIT_AND_EXPR, type, tmp3, tmp1);
10745 return fold_convert (type,
10746 fold_build2 (BIT_IOR_EXPR, type, tmp2, tmp3));
10749 /* (X | Y) & Y is (X, Y). */
10750 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10751 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10752 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 0));
10753 /* (X | Y) & X is (Y, X). */
10754 if (TREE_CODE (arg0) == BIT_IOR_EXPR
10755 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10756 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10757 return omit_one_operand (type, arg1, TREE_OPERAND (arg0, 1));
10758 /* X & (X | Y) is (Y, X). */
10759 if (TREE_CODE (arg1) == BIT_IOR_EXPR
10760 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0)
10761 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 1)))
10762 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 1));
10763 /* X & (Y | X) is (Y, X). */
10764 if (TREE_CODE (arg1) == BIT_IOR_EXPR
10765 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10766 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10767 return omit_one_operand (type, arg0, TREE_OPERAND (arg1, 0));
10769 /* Fold (X ^ 1) & 1 as (X & 1) == 0. */
10770 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10771 && integer_onep (TREE_OPERAND (arg0, 1))
10772 && integer_onep (arg1))
10774 tem = TREE_OPERAND (arg0, 0);
10775 return fold_build2 (EQ_EXPR, type,
10776 fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
10777 build_int_cst (TREE_TYPE (tem), 1)),
10778 build_int_cst (TREE_TYPE (tem), 0));
10780 /* Fold ~X & 1 as (X & 1) == 0. */
10781 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10782 && integer_onep (arg1))
10784 tem = TREE_OPERAND (arg0, 0);
10785 return fold_build2 (EQ_EXPR, type,
10786 fold_build2 (BIT_AND_EXPR, TREE_TYPE (tem), tem,
10787 build_int_cst (TREE_TYPE (tem), 1)),
10788 build_int_cst (TREE_TYPE (tem), 0));
10791 /* Fold (X ^ Y) & Y as ~X & Y. */
10792 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10793 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
10795 tem = fold_convert (type, TREE_OPERAND (arg0, 0));
10796 return fold_build2 (BIT_AND_EXPR, type,
10797 fold_build1 (BIT_NOT_EXPR, type, tem),
10798 fold_convert (type, arg1));
10800 /* Fold (X ^ Y) & X as ~Y & X. */
10801 if (TREE_CODE (arg0) == BIT_XOR_EXPR
10802 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
10803 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
10805 tem = fold_convert (type, TREE_OPERAND (arg0, 1));
10806 return fold_build2 (BIT_AND_EXPR, type,
10807 fold_build1 (BIT_NOT_EXPR, type, tem),
10808 fold_convert (type, arg1));
10810 /* Fold X & (X ^ Y) as X & ~Y. */
10811 if (TREE_CODE (arg1) == BIT_XOR_EXPR
10812 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
10814 tem = fold_convert (type, TREE_OPERAND (arg1, 1));
10815 return fold_build2 (BIT_AND_EXPR, type,
10816 fold_convert (type, arg0),
10817 fold_build1 (BIT_NOT_EXPR, type, tem));
10819 /* Fold X & (Y ^ X) as ~Y & X. */
10820 if (TREE_CODE (arg1) == BIT_XOR_EXPR
10821 && operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0)
10822 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
10824 tem = fold_convert (type, TREE_OPERAND (arg1, 0));
10825 return fold_build2 (BIT_AND_EXPR, type,
10826 fold_build1 (BIT_NOT_EXPR, type, tem),
10827 fold_convert (type, arg0));
10830 t1 = distribute_bit_expr (code, type, arg0, arg1);
10831 if (t1 != NULL_TREE)
10833 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
10834 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
10835 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
10838 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
10840 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
10841 && (~TREE_INT_CST_LOW (arg1)
10842 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
10843 return fold_convert (type, TREE_OPERAND (arg0, 0));
10846 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
10848 This results in more efficient code for machines without a NOR
10849 instruction. Combine will canonicalize to the first form
10850 which will allow use of NOR instructions provided by the
10851 backend if they exist. */
10852 if (TREE_CODE (arg0) == BIT_NOT_EXPR
10853 && TREE_CODE (arg1) == BIT_NOT_EXPR)
10855 return fold_build1 (BIT_NOT_EXPR, type,
10856 build2 (BIT_IOR_EXPR, type,
10857 fold_convert (type,
10858 TREE_OPERAND (arg0, 0)),
10859 fold_convert (type,
10860 TREE_OPERAND (arg1, 0))));
10863 /* If arg0 is derived from the address of an object or function, we may
10864 be able to fold this expression using the object or function's
10866 if (POINTER_TYPE_P (TREE_TYPE (arg0)) && host_integerp (arg1, 1))
10868 unsigned HOST_WIDE_INT modulus, residue;
10869 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (arg1);
10871 modulus = get_pointer_modulus_and_residue (arg0, &residue);
10873 /* This works because modulus is a power of 2. If this weren't the
10874 case, we'd have to replace it by its greatest power-of-2
10875 divisor: modulus & -modulus. */
10877 return build_int_cst (type, residue & low);
10880 /* Fold (X << C1) & C2 into (X << C1) & (C2 | ((1 << C1) - 1))
10881 (X >> C1) & C2 into (X >> C1) & (C2 | ~((type) -1 >> C1))
10882 if the new mask might be further optimized. */
10883 if ((TREE_CODE (arg0) == LSHIFT_EXPR
10884 || TREE_CODE (arg0) == RSHIFT_EXPR)
10885 && host_integerp (TREE_OPERAND (arg0, 1), 1)
10886 && host_integerp (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)))
10887 && tree_low_cst (TREE_OPERAND (arg0, 1), 1)
10888 < TYPE_PRECISION (TREE_TYPE (arg0))
10889 && TYPE_PRECISION (TREE_TYPE (arg0)) <= HOST_BITS_PER_WIDE_INT
10890 && tree_low_cst (TREE_OPERAND (arg0, 1), 1) > 0)
10892 unsigned int shiftc = tree_low_cst (TREE_OPERAND (arg0, 1), 1);
10893 unsigned HOST_WIDE_INT mask
10894 = tree_low_cst (arg1, TYPE_UNSIGNED (TREE_TYPE (arg1)));
10895 unsigned HOST_WIDE_INT newmask, zerobits = 0;
10896 tree shift_type = TREE_TYPE (arg0);
10898 if (TREE_CODE (arg0) == LSHIFT_EXPR)
10899 zerobits = ((((unsigned HOST_WIDE_INT) 1) << shiftc) - 1);
10900 else if (TREE_CODE (arg0) == RSHIFT_EXPR
10901 && TYPE_PRECISION (TREE_TYPE (arg0))
10902 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg0))))
10904 unsigned int prec = TYPE_PRECISION (TREE_TYPE (arg0));
10905 tree arg00 = TREE_OPERAND (arg0, 0);
10906 /* See if more bits can be proven as zero because of
10908 if (TREE_CODE (arg00) == NOP_EXPR
10909 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg00, 0))))
10911 tree inner_type = TREE_TYPE (TREE_OPERAND (arg00, 0));
10912 if (TYPE_PRECISION (inner_type)
10913 == GET_MODE_BITSIZE (TYPE_MODE (inner_type))
10914 && TYPE_PRECISION (inner_type) < prec)
10916 prec = TYPE_PRECISION (inner_type);
10917 /* See if we can shorten the right shift. */
10919 shift_type = inner_type;
10922 zerobits = ~(unsigned HOST_WIDE_INT) 0;
10923 zerobits >>= HOST_BITS_PER_WIDE_INT - shiftc;
10924 zerobits <<= prec - shiftc;
10925 /* For arithmetic shift if sign bit could be set, zerobits
10926 can contain actually sign bits, so no transformation is
10927 possible, unless MASK masks them all away. In that
10928 case the shift needs to be converted into logical shift. */
10929 if (!TYPE_UNSIGNED (TREE_TYPE (arg0))
10930 && prec == TYPE_PRECISION (TREE_TYPE (arg0)))
10932 if ((mask & zerobits) == 0)
10933 shift_type = unsigned_type_for (TREE_TYPE (arg0));
10939 /* ((X << 16) & 0xff00) is (X, 0). */
10940 if ((mask & zerobits) == mask)
10941 return omit_one_operand (type, build_int_cst (type, 0), arg0);
10943 newmask = mask | zerobits;
10944 if (newmask != mask && (newmask & (newmask + 1)) == 0)
10948 /* Only do the transformation if NEWMASK is some integer
10950 for (prec = BITS_PER_UNIT;
10951 prec < HOST_BITS_PER_WIDE_INT; prec <<= 1)
10952 if (newmask == (((unsigned HOST_WIDE_INT) 1) << prec) - 1)
10954 if (prec < HOST_BITS_PER_WIDE_INT
10955 || newmask == ~(unsigned HOST_WIDE_INT) 0)
10957 if (shift_type != TREE_TYPE (arg0))
10959 tem = fold_build2 (TREE_CODE (arg0), shift_type,
10960 fold_convert (shift_type,
10961 TREE_OPERAND (arg0, 0)),
10962 TREE_OPERAND (arg0, 1));
10963 tem = fold_convert (type, tem);
10967 return fold_build2 (BIT_AND_EXPR, type, tem,
10968 build_int_cst_type (TREE_TYPE (op1),
10977 /* Don't touch a floating-point divide by zero unless the mode
10978 of the constant can represent infinity. */
10979 if (TREE_CODE (arg1) == REAL_CST
10980 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
10981 && real_zerop (arg1))
10984 /* Optimize A / A to 1.0 if we don't care about
10985 NaNs or Infinities. Skip the transformation
10986 for non-real operands. */
10987 if (SCALAR_FLOAT_TYPE_P (TREE_TYPE (arg0))
10988 && ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
10989 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg0)))
10990 && operand_equal_p (arg0, arg1, 0))
10992 tree r = build_real (TREE_TYPE (arg0), dconst1);
10994 return omit_two_operands (type, r, arg0, arg1);
10997 /* The complex version of the above A / A optimization. */
10998 if (COMPLEX_FLOAT_TYPE_P (TREE_TYPE (arg0))
10999 && operand_equal_p (arg0, arg1, 0))
11001 tree elem_type = TREE_TYPE (TREE_TYPE (arg0));
11002 if (! HONOR_NANS (TYPE_MODE (elem_type))
11003 && ! HONOR_INFINITIES (TYPE_MODE (elem_type)))
11005 tree r = build_real (elem_type, dconst1);
11006 /* omit_two_operands will call fold_convert for us. */
11007 return omit_two_operands (type, r, arg0, arg1);
11011 /* (-A) / (-B) -> A / B */
11012 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
11013 return fold_build2 (RDIV_EXPR, type,
11014 TREE_OPERAND (arg0, 0),
11015 negate_expr (arg1));
11016 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
11017 return fold_build2 (RDIV_EXPR, type,
11018 negate_expr (arg0),
11019 TREE_OPERAND (arg1, 0));
11021 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
11022 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11023 && real_onep (arg1))
11024 return non_lvalue (fold_convert (type, arg0));
11026 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
11027 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
11028 && real_minus_onep (arg1))
11029 return non_lvalue (fold_convert (type, negate_expr (arg0)));
11031 /* If ARG1 is a constant, we can convert this to a multiply by the
11032 reciprocal. This does not have the same rounding properties,
11033 so only do this if -freciprocal-math. We can actually
11034 always safely do it if ARG1 is a power of two, but it's hard to
11035 tell if it is or not in a portable manner. */
11036 if (TREE_CODE (arg1) == REAL_CST)
11038 if (flag_reciprocal_math
11039 && 0 != (tem = const_binop (code, build_real (type, dconst1),
11041 return fold_build2 (MULT_EXPR, type, arg0, tem);
11042 /* Find the reciprocal if optimizing and the result is exact. */
11046 r = TREE_REAL_CST (arg1);
11047 if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
11049 tem = build_real (type, r);
11050 return fold_build2 (MULT_EXPR, type,
11051 fold_convert (type, arg0), tem);
11055 /* Convert A/B/C to A/(B*C). */
11056 if (flag_reciprocal_math
11057 && TREE_CODE (arg0) == RDIV_EXPR)
11058 return fold_build2 (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
11059 fold_build2 (MULT_EXPR, type,
11060 TREE_OPERAND (arg0, 1), arg1));
11062 /* Convert A/(B/C) to (A/B)*C. */
11063 if (flag_reciprocal_math
11064 && TREE_CODE (arg1) == RDIV_EXPR)
11065 return fold_build2 (MULT_EXPR, type,
11066 fold_build2 (RDIV_EXPR, type, arg0,
11067 TREE_OPERAND (arg1, 0)),
11068 TREE_OPERAND (arg1, 1));
11070 /* Convert C1/(X*C2) into (C1/C2)/X. */
11071 if (flag_reciprocal_math
11072 && TREE_CODE (arg1) == MULT_EXPR
11073 && TREE_CODE (arg0) == REAL_CST
11074 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
11076 tree tem = const_binop (RDIV_EXPR, arg0,
11077 TREE_OPERAND (arg1, 1), 0);
11079 return fold_build2 (RDIV_EXPR, type, tem,
11080 TREE_OPERAND (arg1, 0));
11083 if (flag_unsafe_math_optimizations)
11085 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
11086 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
11088 /* Optimize sin(x)/cos(x) as tan(x). */
11089 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
11090 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
11091 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
11092 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11093 CALL_EXPR_ARG (arg1, 0), 0))
11095 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11097 if (tanfn != NULL_TREE)
11098 return build_call_expr (tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11101 /* Optimize cos(x)/sin(x) as 1.0/tan(x). */
11102 if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
11103 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
11104 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
11105 && operand_equal_p (CALL_EXPR_ARG (arg0, 0),
11106 CALL_EXPR_ARG (arg1, 0), 0))
11108 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
11110 if (tanfn != NULL_TREE)
11112 tree tmp = build_call_expr (tanfn, 1, CALL_EXPR_ARG (arg0, 0));
11113 return fold_build2 (RDIV_EXPR, type,
11114 build_real (type, dconst1), tmp);
11118 /* Optimize sin(x)/tan(x) as cos(x) if we don't care about
11119 NaNs or Infinities. */
11120 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_TAN)
11121 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_TANF)
11122 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_TANL)))
11124 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11125 tree arg01 = CALL_EXPR_ARG (arg1, 0);
11127 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11128 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11129 && operand_equal_p (arg00, arg01, 0))
11131 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11133 if (cosfn != NULL_TREE)
11134 return build_call_expr (cosfn, 1, arg00);
11138 /* Optimize tan(x)/sin(x) as 1.0/cos(x) if we don't care about
11139 NaNs or Infinities. */
11140 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_SIN)
11141 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_SINF)
11142 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_SINL)))
11144 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11145 tree arg01 = CALL_EXPR_ARG (arg1, 0);
11147 if (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg00)))
11148 && ! HONOR_INFINITIES (TYPE_MODE (TREE_TYPE (arg00)))
11149 && operand_equal_p (arg00, arg01, 0))
11151 tree cosfn = mathfn_built_in (type, BUILT_IN_COS);
11153 if (cosfn != NULL_TREE)
11155 tree tmp = build_call_expr (cosfn, 1, arg00);
11156 return fold_build2 (RDIV_EXPR, type,
11157 build_real (type, dconst1),
11163 /* Optimize pow(x,c)/x as pow(x,c-1). */
11164 if (fcode0 == BUILT_IN_POW
11165 || fcode0 == BUILT_IN_POWF
11166 || fcode0 == BUILT_IN_POWL)
11168 tree arg00 = CALL_EXPR_ARG (arg0, 0);
11169 tree arg01 = CALL_EXPR_ARG (arg0, 1);
11170 if (TREE_CODE (arg01) == REAL_CST
11171 && !TREE_OVERFLOW (arg01)
11172 && operand_equal_p (arg1, arg00, 0))
11174 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg0), 0);
11178 c = TREE_REAL_CST (arg01);
11179 real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
11180 arg = build_real (type, c);
11181 return build_call_expr (powfn, 2, arg1, arg);
11185 /* Optimize a/root(b/c) into a*root(c/b). */
11186 if (BUILTIN_ROOT_P (fcode1))
11188 tree rootarg = CALL_EXPR_ARG (arg1, 0);
11190 if (TREE_CODE (rootarg) == RDIV_EXPR)
11192 tree rootfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11193 tree b = TREE_OPERAND (rootarg, 0);
11194 tree c = TREE_OPERAND (rootarg, 1);
11196 tree tmp = fold_build2 (RDIV_EXPR, type, c, b);
11198 tmp = build_call_expr (rootfn, 1, tmp);
11199 return fold_build2 (MULT_EXPR, type, arg0, tmp);
11203 /* Optimize x/expN(y) into x*expN(-y). */
11204 if (BUILTIN_EXPONENT_P (fcode1))
11206 tree expfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11207 tree arg = negate_expr (CALL_EXPR_ARG (arg1, 0));
11208 arg1 = build_call_expr (expfn, 1, fold_convert (type, arg));
11209 return fold_build2 (MULT_EXPR, type, arg0, arg1);
11212 /* Optimize x/pow(y,z) into x*pow(y,-z). */
11213 if (fcode1 == BUILT_IN_POW
11214 || fcode1 == BUILT_IN_POWF
11215 || fcode1 == BUILT_IN_POWL)
11217 tree powfn = TREE_OPERAND (CALL_EXPR_FN (arg1), 0);
11218 tree arg10 = CALL_EXPR_ARG (arg1, 0);
11219 tree arg11 = CALL_EXPR_ARG (arg1, 1);
11220 tree neg11 = fold_convert (type, negate_expr (arg11));
11221 arg1 = build_call_expr (powfn, 2, arg10, neg11);
11222 return fold_build2 (MULT_EXPR, type, arg0, arg1);
11227 case TRUNC_DIV_EXPR:
11228 case FLOOR_DIV_EXPR:
11229 /* Simplify A / (B << N) where A and B are positive and B is
11230 a power of 2, to A >> (N + log2(B)). */
11231 strict_overflow_p = false;
11232 if (TREE_CODE (arg1) == LSHIFT_EXPR
11233 && (TYPE_UNSIGNED (type)
11234 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11236 tree sval = TREE_OPERAND (arg1, 0);
11237 if (integer_pow2p (sval) && tree_int_cst_sgn (sval) > 0)
11239 tree sh_cnt = TREE_OPERAND (arg1, 1);
11240 unsigned long pow2 = exact_log2 (TREE_INT_CST_LOW (sval));
11242 if (strict_overflow_p)
11243 fold_overflow_warning (("assuming signed overflow does not "
11244 "occur when simplifying A / (B << N)"),
11245 WARN_STRICT_OVERFLOW_MISC);
11247 sh_cnt = fold_build2 (PLUS_EXPR, TREE_TYPE (sh_cnt),
11248 sh_cnt, build_int_cst (NULL_TREE, pow2));
11249 return fold_build2 (RSHIFT_EXPR, type,
11250 fold_convert (type, arg0), sh_cnt);
11254 /* For unsigned integral types, FLOOR_DIV_EXPR is the same as
11255 TRUNC_DIV_EXPR. Rewrite into the latter in this case. */
11256 if (INTEGRAL_TYPE_P (type)
11257 && TYPE_UNSIGNED (type)
11258 && code == FLOOR_DIV_EXPR)
11259 return fold_build2 (TRUNC_DIV_EXPR, type, op0, op1);
11263 case ROUND_DIV_EXPR:
11264 case CEIL_DIV_EXPR:
11265 case EXACT_DIV_EXPR:
11266 if (integer_onep (arg1))
11267 return non_lvalue (fold_convert (type, arg0));
11268 if (integer_zerop (arg1))
11270 /* X / -1 is -X. */
11271 if (!TYPE_UNSIGNED (type)
11272 && TREE_CODE (arg1) == INTEGER_CST
11273 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11274 && TREE_INT_CST_HIGH (arg1) == -1)
11275 return fold_convert (type, negate_expr (arg0));
11277 /* Convert -A / -B to A / B when the type is signed and overflow is
11279 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11280 && TREE_CODE (arg0) == NEGATE_EXPR
11281 && negate_expr_p (arg1))
11283 if (INTEGRAL_TYPE_P (type))
11284 fold_overflow_warning (("assuming signed overflow does not occur "
11285 "when distributing negation across "
11287 WARN_STRICT_OVERFLOW_MISC);
11288 return fold_build2 (code, type,
11289 fold_convert (type, TREE_OPERAND (arg0, 0)),
11290 negate_expr (arg1));
11292 if ((!INTEGRAL_TYPE_P (type) || TYPE_OVERFLOW_UNDEFINED (type))
11293 && TREE_CODE (arg1) == NEGATE_EXPR
11294 && negate_expr_p (arg0))
11296 if (INTEGRAL_TYPE_P (type))
11297 fold_overflow_warning (("assuming signed overflow does not occur "
11298 "when distributing negation across "
11300 WARN_STRICT_OVERFLOW_MISC);
11301 return fold_build2 (code, type, negate_expr (arg0),
11302 TREE_OPERAND (arg1, 0));
11305 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
11306 operation, EXACT_DIV_EXPR.
11308 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
11309 At one time others generated faster code, it's not clear if they do
11310 after the last round to changes to the DIV code in expmed.c. */
11311 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
11312 && multiple_of_p (type, arg0, arg1))
11313 return fold_build2 (EXACT_DIV_EXPR, type, arg0, arg1);
11315 strict_overflow_p = false;
11316 if (TREE_CODE (arg1) == INTEGER_CST
11317 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11318 &strict_overflow_p)))
11320 if (strict_overflow_p)
11321 fold_overflow_warning (("assuming signed overflow does not occur "
11322 "when simplifying division"),
11323 WARN_STRICT_OVERFLOW_MISC);
11324 return fold_convert (type, tem);
11329 case CEIL_MOD_EXPR:
11330 case FLOOR_MOD_EXPR:
11331 case ROUND_MOD_EXPR:
11332 case TRUNC_MOD_EXPR:
11333 /* X % 1 is always zero, but be sure to preserve any side
11335 if (integer_onep (arg1))
11336 return omit_one_operand (type, integer_zero_node, arg0);
11338 /* X % 0, return X % 0 unchanged so that we can get the
11339 proper warnings and errors. */
11340 if (integer_zerop (arg1))
11343 /* 0 % X is always zero, but be sure to preserve any side
11344 effects in X. Place this after checking for X == 0. */
11345 if (integer_zerop (arg0))
11346 return omit_one_operand (type, integer_zero_node, arg1);
11348 /* X % -1 is zero. */
11349 if (!TYPE_UNSIGNED (type)
11350 && TREE_CODE (arg1) == INTEGER_CST
11351 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
11352 && TREE_INT_CST_HIGH (arg1) == -1)
11353 return omit_one_operand (type, integer_zero_node, arg0);
11355 /* Optimize TRUNC_MOD_EXPR by a power of two into a BIT_AND_EXPR,
11356 i.e. "X % C" into "X & (C - 1)", if X and C are positive. */
11357 strict_overflow_p = false;
11358 if ((code == TRUNC_MOD_EXPR || code == FLOOR_MOD_EXPR)
11359 && (TYPE_UNSIGNED (type)
11360 || tree_expr_nonnegative_warnv_p (op0, &strict_overflow_p)))
11363 /* Also optimize A % (C << N) where C is a power of 2,
11364 to A & ((C << N) - 1). */
11365 if (TREE_CODE (arg1) == LSHIFT_EXPR)
11366 c = TREE_OPERAND (arg1, 0);
11368 if (integer_pow2p (c) && tree_int_cst_sgn (c) > 0)
11370 tree mask = fold_build2 (MINUS_EXPR, TREE_TYPE (arg1), arg1,
11371 build_int_cst (TREE_TYPE (arg1), 1));
11372 if (strict_overflow_p)
11373 fold_overflow_warning (("assuming signed overflow does not "
11374 "occur when simplifying "
11375 "X % (power of two)"),
11376 WARN_STRICT_OVERFLOW_MISC);
11377 return fold_build2 (BIT_AND_EXPR, type,
11378 fold_convert (type, arg0),
11379 fold_convert (type, mask));
11383 /* X % -C is the same as X % C. */
11384 if (code == TRUNC_MOD_EXPR
11385 && !TYPE_UNSIGNED (type)
11386 && TREE_CODE (arg1) == INTEGER_CST
11387 && !TREE_OVERFLOW (arg1)
11388 && TREE_INT_CST_HIGH (arg1) < 0
11389 && !TYPE_OVERFLOW_TRAPS (type)
11390 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
11391 && !sign_bit_p (arg1, arg1))
11392 return fold_build2 (code, type, fold_convert (type, arg0),
11393 fold_convert (type, negate_expr (arg1)));
11395 /* X % -Y is the same as X % Y. */
11396 if (code == TRUNC_MOD_EXPR
11397 && !TYPE_UNSIGNED (type)
11398 && TREE_CODE (arg1) == NEGATE_EXPR
11399 && !TYPE_OVERFLOW_TRAPS (type))
11400 return fold_build2 (code, type, fold_convert (type, arg0),
11401 fold_convert (type, TREE_OPERAND (arg1, 0)));
11403 if (TREE_CODE (arg1) == INTEGER_CST
11404 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE,
11405 &strict_overflow_p)))
11407 if (strict_overflow_p)
11408 fold_overflow_warning (("assuming signed overflow does not occur "
11409 "when simplifying modulus"),
11410 WARN_STRICT_OVERFLOW_MISC);
11411 return fold_convert (type, tem);
11418 if (integer_all_onesp (arg0))
11419 return omit_one_operand (type, arg0, arg1);
11423 /* Optimize -1 >> x for arithmetic right shifts. */
11424 if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type))
11425 return omit_one_operand (type, arg0, arg1);
11426 /* ... fall through ... */
11430 if (integer_zerop (arg1))
11431 return non_lvalue (fold_convert (type, arg0));
11432 if (integer_zerop (arg0))
11433 return omit_one_operand (type, arg0, arg1);
11435 /* Since negative shift count is not well-defined,
11436 don't try to compute it in the compiler. */
11437 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
11440 /* Turn (a OP c1) OP c2 into a OP (c1+c2). */
11441 if (TREE_CODE (op0) == code && host_integerp (arg1, false)
11442 && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11443 && host_integerp (TREE_OPERAND (arg0, 1), false)
11444 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11446 HOST_WIDE_INT low = (TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1))
11447 + TREE_INT_CST_LOW (arg1));
11449 /* Deal with a OP (c1 + c2) being undefined but (a OP c1) OP c2
11450 being well defined. */
11451 if (low >= TYPE_PRECISION (type))
11453 if (code == LROTATE_EXPR || code == RROTATE_EXPR)
11454 low = low % TYPE_PRECISION (type);
11455 else if (TYPE_UNSIGNED (type) || code == LSHIFT_EXPR)
11456 return build_int_cst (type, 0);
11458 low = TYPE_PRECISION (type) - 1;
11461 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11462 build_int_cst (type, low));
11465 /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
11466 into x & ((unsigned)-1 >> c) for unsigned types. */
11467 if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
11468 || (TYPE_UNSIGNED (type)
11469 && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
11470 && host_integerp (arg1, false)
11471 && TREE_INT_CST_LOW (arg1) < TYPE_PRECISION (type)
11472 && host_integerp (TREE_OPERAND (arg0, 1), false)
11473 && TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)) < TYPE_PRECISION (type))
11475 HOST_WIDE_INT low0 = TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1));
11476 HOST_WIDE_INT low1 = TREE_INT_CST_LOW (arg1);
11482 arg00 = fold_convert (type, TREE_OPERAND (arg0, 0));
11484 lshift = build_int_cst (type, -1);
11485 lshift = int_const_binop (code, lshift, arg1, 0);
11487 return fold_build2 (BIT_AND_EXPR, type, arg00, lshift);
11491 /* Rewrite an LROTATE_EXPR by a constant into an
11492 RROTATE_EXPR by a new constant. */
11493 if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
11495 tree tem = build_int_cst (TREE_TYPE (arg1),
11496 TYPE_PRECISION (type));
11497 tem = const_binop (MINUS_EXPR, tem, arg1, 0);
11498 return fold_build2 (RROTATE_EXPR, type, op0, tem);
11501 /* If we have a rotate of a bit operation with the rotate count and
11502 the second operand of the bit operation both constant,
11503 permute the two operations. */
11504 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11505 && (TREE_CODE (arg0) == BIT_AND_EXPR
11506 || TREE_CODE (arg0) == BIT_IOR_EXPR
11507 || TREE_CODE (arg0) == BIT_XOR_EXPR)
11508 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11509 return fold_build2 (TREE_CODE (arg0), type,
11510 fold_build2 (code, type,
11511 TREE_OPERAND (arg0, 0), arg1),
11512 fold_build2 (code, type,
11513 TREE_OPERAND (arg0, 1), arg1));
11515 /* Two consecutive rotates adding up to the precision of the
11516 type can be ignored. */
11517 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
11518 && TREE_CODE (arg0) == RROTATE_EXPR
11519 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11520 && TREE_INT_CST_HIGH (arg1) == 0
11521 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
11522 && ((TREE_INT_CST_LOW (arg1)
11523 + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
11524 == (unsigned int) TYPE_PRECISION (type)))
11525 return TREE_OPERAND (arg0, 0);
11527 /* Fold (X & C2) << C1 into (X << C1) & (C2 << C1)
11528 (X & C2) >> C1 into (X >> C1) & (C2 >> C1)
11529 if the latter can be further optimized. */
11530 if ((code == LSHIFT_EXPR || code == RSHIFT_EXPR)
11531 && TREE_CODE (arg0) == BIT_AND_EXPR
11532 && TREE_CODE (arg1) == INTEGER_CST
11533 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11535 tree mask = fold_build2 (code, type,
11536 fold_convert (type, TREE_OPERAND (arg0, 1)),
11538 tree shift = fold_build2 (code, type,
11539 fold_convert (type, TREE_OPERAND (arg0, 0)),
11541 tem = fold_binary (BIT_AND_EXPR, type, shift, mask);
11549 if (operand_equal_p (arg0, arg1, 0))
11550 return omit_one_operand (type, arg0, arg1);
11551 if (INTEGRAL_TYPE_P (type)
11552 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
11553 return omit_one_operand (type, arg1, arg0);
11554 tem = fold_minmax (MIN_EXPR, type, arg0, arg1);
11560 if (operand_equal_p (arg0, arg1, 0))
11561 return omit_one_operand (type, arg0, arg1);
11562 if (INTEGRAL_TYPE_P (type)
11563 && TYPE_MAX_VALUE (type)
11564 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
11565 return omit_one_operand (type, arg1, arg0);
11566 tem = fold_minmax (MAX_EXPR, type, arg0, arg1);
11571 case TRUTH_ANDIF_EXPR:
11572 /* Note that the operands of this must be ints
11573 and their values must be 0 or 1.
11574 ("true" is a fixed value perhaps depending on the language.) */
11575 /* If first arg is constant zero, return it. */
11576 if (integer_zerop (arg0))
11577 return fold_convert (type, arg0);
11578 case TRUTH_AND_EXPR:
11579 /* If either arg is constant true, drop it. */
11580 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11581 return non_lvalue (fold_convert (type, arg1));
11582 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
11583 /* Preserve sequence points. */
11584 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11585 return non_lvalue (fold_convert (type, arg0));
11586 /* If second arg is constant zero, result is zero, but first arg
11587 must be evaluated. */
11588 if (integer_zerop (arg1))
11589 return omit_one_operand (type, arg1, arg0);
11590 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
11591 case will be handled here. */
11592 if (integer_zerop (arg0))
11593 return omit_one_operand (type, arg0, arg1);
11595 /* !X && X is always false. */
11596 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11597 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11598 return omit_one_operand (type, integer_zero_node, arg1);
11599 /* X && !X is always false. */
11600 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11601 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11602 return omit_one_operand (type, integer_zero_node, arg0);
11604 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
11605 means A >= Y && A != MAX, but in this case we know that
11608 if (!TREE_SIDE_EFFECTS (arg0)
11609 && !TREE_SIDE_EFFECTS (arg1))
11611 tem = fold_to_nonsharp_ineq_using_bound (arg0, arg1);
11612 if (tem && !operand_equal_p (tem, arg0, 0))
11613 return fold_build2 (code, type, tem, arg1);
11615 tem = fold_to_nonsharp_ineq_using_bound (arg1, arg0);
11616 if (tem && !operand_equal_p (tem, arg1, 0))
11617 return fold_build2 (code, type, arg0, tem);
11621 /* We only do these simplifications if we are optimizing. */
11625 /* Check for things like (A || B) && (A || C). We can convert this
11626 to A || (B && C). Note that either operator can be any of the four
11627 truth and/or operations and the transformation will still be
11628 valid. Also note that we only care about order for the
11629 ANDIF and ORIF operators. If B contains side effects, this
11630 might change the truth-value of A. */
11631 if (TREE_CODE (arg0) == TREE_CODE (arg1)
11632 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
11633 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
11634 || TREE_CODE (arg0) == TRUTH_AND_EXPR
11635 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
11636 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
11638 tree a00 = TREE_OPERAND (arg0, 0);
11639 tree a01 = TREE_OPERAND (arg0, 1);
11640 tree a10 = TREE_OPERAND (arg1, 0);
11641 tree a11 = TREE_OPERAND (arg1, 1);
11642 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
11643 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
11644 && (code == TRUTH_AND_EXPR
11645 || code == TRUTH_OR_EXPR));
11647 if (operand_equal_p (a00, a10, 0))
11648 return fold_build2 (TREE_CODE (arg0), type, a00,
11649 fold_build2 (code, type, a01, a11));
11650 else if (commutative && operand_equal_p (a00, a11, 0))
11651 return fold_build2 (TREE_CODE (arg0), type, a00,
11652 fold_build2 (code, type, a01, a10));
11653 else if (commutative && operand_equal_p (a01, a10, 0))
11654 return fold_build2 (TREE_CODE (arg0), type, a01,
11655 fold_build2 (code, type, a00, a11));
11657 /* This case if tricky because we must either have commutative
11658 operators or else A10 must not have side-effects. */
11660 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
11661 && operand_equal_p (a01, a11, 0))
11662 return fold_build2 (TREE_CODE (arg0), type,
11663 fold_build2 (code, type, a00, a10),
11667 /* See if we can build a range comparison. */
11668 if (0 != (tem = fold_range_test (code, type, op0, op1)))
11671 /* Check for the possibility of merging component references. If our
11672 lhs is another similar operation, try to merge its rhs with our
11673 rhs. Then try to merge our lhs and rhs. */
11674 if (TREE_CODE (arg0) == code
11675 && 0 != (tem = fold_truthop (code, type,
11676 TREE_OPERAND (arg0, 1), arg1)))
11677 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
11679 if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
11684 case TRUTH_ORIF_EXPR:
11685 /* Note that the operands of this must be ints
11686 and their values must be 0 or true.
11687 ("true" is a fixed value perhaps depending on the language.) */
11688 /* If first arg is constant true, return it. */
11689 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11690 return fold_convert (type, arg0);
11691 case TRUTH_OR_EXPR:
11692 /* If either arg is constant zero, drop it. */
11693 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
11694 return non_lvalue (fold_convert (type, arg1));
11695 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
11696 /* Preserve sequence points. */
11697 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
11698 return non_lvalue (fold_convert (type, arg0));
11699 /* If second arg is constant true, result is true, but we must
11700 evaluate first arg. */
11701 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
11702 return omit_one_operand (type, arg1, arg0);
11703 /* Likewise for first arg, but note this only occurs here for
11705 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
11706 return omit_one_operand (type, arg0, arg1);
11708 /* !X || X is always true. */
11709 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11710 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11711 return omit_one_operand (type, integer_one_node, arg1);
11712 /* X || !X is always true. */
11713 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11714 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11715 return omit_one_operand (type, integer_one_node, arg0);
11719 case TRUTH_XOR_EXPR:
11720 /* If the second arg is constant zero, drop it. */
11721 if (integer_zerop (arg1))
11722 return non_lvalue (fold_convert (type, arg0));
11723 /* If the second arg is constant true, this is a logical inversion. */
11724 if (integer_onep (arg1))
11726 /* Only call invert_truthvalue if operand is a truth value. */
11727 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
11728 tem = fold_build1 (TRUTH_NOT_EXPR, TREE_TYPE (arg0), arg0);
11730 tem = invert_truthvalue (arg0);
11731 return non_lvalue (fold_convert (type, tem));
11733 /* Identical arguments cancel to zero. */
11734 if (operand_equal_p (arg0, arg1, 0))
11735 return omit_one_operand (type, integer_zero_node, arg0);
11737 /* !X ^ X is always true. */
11738 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
11739 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
11740 return omit_one_operand (type, integer_one_node, arg1);
11742 /* X ^ !X is always true. */
11743 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
11744 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
11745 return omit_one_operand (type, integer_one_node, arg0);
11751 tem = fold_comparison (code, type, op0, op1);
11752 if (tem != NULL_TREE)
11755 /* bool_var != 0 becomes bool_var. */
11756 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
11757 && code == NE_EXPR)
11758 return non_lvalue (fold_convert (type, arg0));
11760 /* bool_var == 1 becomes bool_var. */
11761 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
11762 && code == EQ_EXPR)
11763 return non_lvalue (fold_convert (type, arg0));
11765 /* bool_var != 1 becomes !bool_var. */
11766 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_onep (arg1)
11767 && code == NE_EXPR)
11768 return fold_build1 (TRUTH_NOT_EXPR, type, fold_convert (type, arg0));
11770 /* bool_var == 0 becomes !bool_var. */
11771 if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE && integer_zerop (arg1)
11772 && code == EQ_EXPR)
11773 return fold_build1 (TRUTH_NOT_EXPR, type, fold_convert (type, arg0));
11775 /* If this is an equality comparison of the address of two non-weak,
11776 unaliased symbols neither of which are extern (since we do not
11777 have access to attributes for externs), then we know the result. */
11778 if (TREE_CODE (arg0) == ADDR_EXPR
11779 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg0, 0))
11780 && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
11781 && ! lookup_attribute ("alias",
11782 DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
11783 && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
11784 && TREE_CODE (arg1) == ADDR_EXPR
11785 && VAR_OR_FUNCTION_DECL_P (TREE_OPERAND (arg1, 0))
11786 && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
11787 && ! lookup_attribute ("alias",
11788 DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
11789 && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
11791 /* We know that we're looking at the address of two
11792 non-weak, unaliased, static _DECL nodes.
11794 It is both wasteful and incorrect to call operand_equal_p
11795 to compare the two ADDR_EXPR nodes. It is wasteful in that
11796 all we need to do is test pointer equality for the arguments
11797 to the two ADDR_EXPR nodes. It is incorrect to use
11798 operand_equal_p as that function is NOT equivalent to a
11799 C equality test. It can in fact return false for two
11800 objects which would test as equal using the C equality
11802 bool equal = TREE_OPERAND (arg0, 0) == TREE_OPERAND (arg1, 0);
11803 return constant_boolean_node (equal
11804 ? code == EQ_EXPR : code != EQ_EXPR,
11808 /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
11809 a MINUS_EXPR of a constant, we can convert it into a comparison with
11810 a revised constant as long as no overflow occurs. */
11811 if (TREE_CODE (arg1) == INTEGER_CST
11812 && (TREE_CODE (arg0) == PLUS_EXPR
11813 || TREE_CODE (arg0) == MINUS_EXPR)
11814 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11815 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
11816 ? MINUS_EXPR : PLUS_EXPR,
11817 fold_convert (TREE_TYPE (arg0), arg1),
11818 TREE_OPERAND (arg0, 1), 0))
11819 && !TREE_OVERFLOW (tem))
11820 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
11822 /* Similarly for a NEGATE_EXPR. */
11823 if (TREE_CODE (arg0) == NEGATE_EXPR
11824 && TREE_CODE (arg1) == INTEGER_CST
11825 && 0 != (tem = negate_expr (arg1))
11826 && TREE_CODE (tem) == INTEGER_CST
11827 && !TREE_OVERFLOW (tem))
11828 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), tem);
11830 /* Similarly for a BIT_XOR_EXPR; X ^ C1 == C2 is X == (C1 ^ C2). */
11831 if (TREE_CODE (arg0) == BIT_XOR_EXPR
11832 && TREE_CODE (arg1) == INTEGER_CST
11833 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11834 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
11835 fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg0),
11836 fold_convert (TREE_TYPE (arg0), arg1),
11837 TREE_OPERAND (arg0, 1)));
11839 /* Transform comparisons of the form X +- C CMP X. */
11840 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
11841 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
11842 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
11843 && (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
11844 || POINTER_TYPE_P (TREE_TYPE (arg0))))
11846 tree cst = TREE_OPERAND (arg0, 1);
11848 if (code == EQ_EXPR
11849 && !integer_zerop (cst))
11850 return omit_two_operands (type, boolean_false_node,
11851 TREE_OPERAND (arg0, 0), arg1);
11853 return omit_two_operands (type, boolean_true_node,
11854 TREE_OPERAND (arg0, 0), arg1);
11857 /* If we have X - Y == 0, we can convert that to X == Y and similarly
11858 for !=. Don't do this for ordered comparisons due to overflow. */
11859 if (TREE_CODE (arg0) == MINUS_EXPR
11860 && integer_zerop (arg1))
11861 return fold_build2 (code, type,
11862 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
11864 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
11865 if (TREE_CODE (arg0) == ABS_EXPR
11866 && (integer_zerop (arg1) || real_zerop (arg1)))
11867 return fold_build2 (code, type, TREE_OPERAND (arg0, 0), arg1);
11869 /* If this is an EQ or NE comparison with zero and ARG0 is
11870 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
11871 two operations, but the latter can be done in one less insn
11872 on machines that have only two-operand insns or on which a
11873 constant cannot be the first operand. */
11874 if (TREE_CODE (arg0) == BIT_AND_EXPR
11875 && integer_zerop (arg1))
11877 tree arg00 = TREE_OPERAND (arg0, 0);
11878 tree arg01 = TREE_OPERAND (arg0, 1);
11879 if (TREE_CODE (arg00) == LSHIFT_EXPR
11880 && integer_onep (TREE_OPERAND (arg00, 0)))
11882 tree tem = fold_build2 (RSHIFT_EXPR, TREE_TYPE (arg00),
11883 arg01, TREE_OPERAND (arg00, 1));
11884 tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0), tem,
11885 build_int_cst (TREE_TYPE (arg0), 1));
11886 return fold_build2 (code, type,
11887 fold_convert (TREE_TYPE (arg1), tem), arg1);
11889 else if (TREE_CODE (arg01) == LSHIFT_EXPR
11890 && integer_onep (TREE_OPERAND (arg01, 0)))
11892 tree tem = fold_build2 (RSHIFT_EXPR, TREE_TYPE (arg01),
11893 arg00, TREE_OPERAND (arg01, 1));
11894 tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0), tem,
11895 build_int_cst (TREE_TYPE (arg0), 1));
11896 return fold_build2 (code, type,
11897 fold_convert (TREE_TYPE (arg1), tem), arg1);
11901 /* If this is an NE or EQ comparison of zero against the result of a
11902 signed MOD operation whose second operand is a power of 2, make
11903 the MOD operation unsigned since it is simpler and equivalent. */
11904 if (integer_zerop (arg1)
11905 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
11906 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
11907 || TREE_CODE (arg0) == CEIL_MOD_EXPR
11908 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
11909 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
11910 && integer_pow2p (TREE_OPERAND (arg0, 1)))
11912 tree newtype = unsigned_type_for (TREE_TYPE (arg0));
11913 tree newmod = fold_build2 (TREE_CODE (arg0), newtype,
11914 fold_convert (newtype,
11915 TREE_OPERAND (arg0, 0)),
11916 fold_convert (newtype,
11917 TREE_OPERAND (arg0, 1)));
11919 return fold_build2 (code, type, newmod,
11920 fold_convert (newtype, arg1));
11923 /* Fold ((X >> C1) & C2) == 0 and ((X >> C1) & C2) != 0 where
11924 C1 is a valid shift constant, and C2 is a power of two, i.e.
11926 if (TREE_CODE (arg0) == BIT_AND_EXPR
11927 && TREE_CODE (TREE_OPERAND (arg0, 0)) == RSHIFT_EXPR
11928 && TREE_CODE (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1))
11930 && integer_pow2p (TREE_OPERAND (arg0, 1))
11931 && integer_zerop (arg1))
11933 tree itype = TREE_TYPE (arg0);
11934 unsigned HOST_WIDE_INT prec = TYPE_PRECISION (itype);
11935 tree arg001 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 1);
11937 /* Check for a valid shift count. */
11938 if (TREE_INT_CST_HIGH (arg001) == 0
11939 && TREE_INT_CST_LOW (arg001) < prec)
11941 tree arg01 = TREE_OPERAND (arg0, 1);
11942 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
11943 unsigned HOST_WIDE_INT log2 = tree_log2 (arg01);
11944 /* If (C2 << C1) doesn't overflow, then ((X >> C1) & C2) != 0
11945 can be rewritten as (X & (C2 << C1)) != 0. */
11946 if ((log2 + TREE_INT_CST_LOW (arg001)) < prec)
11948 tem = fold_build2 (LSHIFT_EXPR, itype, arg01, arg001);
11949 tem = fold_build2 (BIT_AND_EXPR, itype, arg000, tem);
11950 return fold_build2 (code, type, tem, arg1);
11952 /* Otherwise, for signed (arithmetic) shifts,
11953 ((X >> C1) & C2) != 0 is rewritten as X < 0, and
11954 ((X >> C1) & C2) == 0 is rewritten as X >= 0. */
11955 else if (!TYPE_UNSIGNED (itype))
11956 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR, type,
11957 arg000, build_int_cst (itype, 0));
11958 /* Otherwise, of unsigned (logical) shifts,
11959 ((X >> C1) & C2) != 0 is rewritten as (X,false), and
11960 ((X >> C1) & C2) == 0 is rewritten as (X,true). */
11962 return omit_one_operand (type,
11963 code == EQ_EXPR ? integer_one_node
11964 : integer_zero_node,
11969 /* If this is an NE comparison of zero with an AND of one, remove the
11970 comparison since the AND will give the correct value. */
11971 if (code == NE_EXPR
11972 && integer_zerop (arg1)
11973 && TREE_CODE (arg0) == BIT_AND_EXPR
11974 && integer_onep (TREE_OPERAND (arg0, 1)))
11975 return fold_convert (type, arg0);
11977 /* If we have (A & C) == C where C is a power of 2, convert this into
11978 (A & C) != 0. Similarly for NE_EXPR. */
11979 if (TREE_CODE (arg0) == BIT_AND_EXPR
11980 && integer_pow2p (TREE_OPERAND (arg0, 1))
11981 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
11982 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
11983 arg0, fold_convert (TREE_TYPE (arg0),
11984 integer_zero_node));
11986 /* If we have (A & C) != 0 or (A & C) == 0 and C is the sign
11987 bit, then fold the expression into A < 0 or A >= 0. */
11988 tem = fold_single_bit_test_into_sign_test (code, arg0, arg1, type);
11992 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
11993 Similarly for NE_EXPR. */
11994 if (TREE_CODE (arg0) == BIT_AND_EXPR
11995 && TREE_CODE (arg1) == INTEGER_CST
11996 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
11998 tree notc = fold_build1 (BIT_NOT_EXPR,
11999 TREE_TYPE (TREE_OPERAND (arg0, 1)),
12000 TREE_OPERAND (arg0, 1));
12001 tree dandnotc = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12003 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12004 if (integer_nonzerop (dandnotc))
12005 return omit_one_operand (type, rslt, arg0);
12008 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
12009 Similarly for NE_EXPR. */
12010 if (TREE_CODE (arg0) == BIT_IOR_EXPR
12011 && TREE_CODE (arg1) == INTEGER_CST
12012 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12014 tree notd = fold_build1 (BIT_NOT_EXPR, TREE_TYPE (arg1), arg1);
12015 tree candnotd = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12016 TREE_OPERAND (arg0, 1), notd);
12017 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
12018 if (integer_nonzerop (candnotd))
12019 return omit_one_operand (type, rslt, arg0);
12022 /* Optimize comparisons of strlen vs zero to a compare of the
12023 first character of the string vs zero. To wit,
12024 strlen(ptr) == 0 => *ptr == 0
12025 strlen(ptr) != 0 => *ptr != 0
12026 Other cases should reduce to one of these two (or a constant)
12027 due to the return value of strlen being unsigned. */
12028 if (TREE_CODE (arg0) == CALL_EXPR
12029 && integer_zerop (arg1))
12031 tree fndecl = get_callee_fndecl (arg0);
12034 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
12035 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
12036 && call_expr_nargs (arg0) == 1
12037 && TREE_CODE (TREE_TYPE (CALL_EXPR_ARG (arg0, 0))) == POINTER_TYPE)
12039 tree iref = build_fold_indirect_ref (CALL_EXPR_ARG (arg0, 0));
12040 return fold_build2 (code, type, iref,
12041 build_int_cst (TREE_TYPE (iref), 0));
12045 /* Fold (X >> C) != 0 into X < 0 if C is one less than the width
12046 of X. Similarly fold (X >> C) == 0 into X >= 0. */
12047 if (TREE_CODE (arg0) == RSHIFT_EXPR
12048 && integer_zerop (arg1)
12049 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12051 tree arg00 = TREE_OPERAND (arg0, 0);
12052 tree arg01 = TREE_OPERAND (arg0, 1);
12053 tree itype = TREE_TYPE (arg00);
12054 if (TREE_INT_CST_HIGH (arg01) == 0
12055 && TREE_INT_CST_LOW (arg01)
12056 == (unsigned HOST_WIDE_INT) (TYPE_PRECISION (itype) - 1))
12058 if (TYPE_UNSIGNED (itype))
12060 itype = signed_type_for (itype);
12061 arg00 = fold_convert (itype, arg00);
12063 return fold_build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
12064 type, arg00, build_int_cst (itype, 0));
12068 /* (X ^ Y) == 0 becomes X == Y, and (X ^ Y) != 0 becomes X != Y. */
12069 if (integer_zerop (arg1)
12070 && TREE_CODE (arg0) == BIT_XOR_EXPR)
12071 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12072 TREE_OPERAND (arg0, 1));
12074 /* (X ^ Y) == Y becomes X == 0. We know that Y has no side-effects. */
12075 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12076 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
12077 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12078 build_int_cst (TREE_TYPE (arg1), 0));
12079 /* Likewise (X ^ Y) == X becomes Y == 0. X has no side-effects. */
12080 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12081 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12082 && reorder_operands_p (TREE_OPERAND (arg0, 1), arg1))
12083 return fold_build2 (code, type, TREE_OPERAND (arg0, 1),
12084 build_int_cst (TREE_TYPE (arg1), 0));
12086 /* (X ^ C1) op C2 can be rewritten as X op (C1 ^ C2). */
12087 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12088 && TREE_CODE (arg1) == INTEGER_CST
12089 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
12090 return fold_build2 (code, type, TREE_OPERAND (arg0, 0),
12091 fold_build2 (BIT_XOR_EXPR, TREE_TYPE (arg1),
12092 TREE_OPERAND (arg0, 1), arg1));
12094 /* Fold (~X & C) == 0 into (X & C) != 0 and (~X & C) != 0 into
12095 (X & C) == 0 when C is a single bit. */
12096 if (TREE_CODE (arg0) == BIT_AND_EXPR
12097 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_NOT_EXPR
12098 && integer_zerop (arg1)
12099 && integer_pow2p (TREE_OPERAND (arg0, 1)))
12101 tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
12102 TREE_OPERAND (TREE_OPERAND (arg0, 0), 0),
12103 TREE_OPERAND (arg0, 1));
12104 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR,
12108 /* Fold ((X & C) ^ C) eq/ne 0 into (X & C) ne/eq 0, when the
12109 constant C is a power of two, i.e. a single bit. */
12110 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12111 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
12112 && integer_zerop (arg1)
12113 && integer_pow2p (TREE_OPERAND (arg0, 1))
12114 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12115 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12117 tree arg00 = TREE_OPERAND (arg0, 0);
12118 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12119 arg00, build_int_cst (TREE_TYPE (arg00), 0));
12122 /* Likewise, fold ((X ^ C) & C) eq/ne 0 into (X & C) ne/eq 0,
12123 when is C is a power of two, i.e. a single bit. */
12124 if (TREE_CODE (arg0) == BIT_AND_EXPR
12125 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_XOR_EXPR
12126 && integer_zerop (arg1)
12127 && integer_pow2p (TREE_OPERAND (arg0, 1))
12128 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
12129 TREE_OPERAND (arg0, 1), OEP_ONLY_CONST))
12131 tree arg000 = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
12132 tem = fold_build2 (BIT_AND_EXPR, TREE_TYPE (arg000),
12133 arg000, TREE_OPERAND (arg0, 1));
12134 return fold_build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
12135 tem, build_int_cst (TREE_TYPE (tem), 0));
12138 if (integer_zerop (arg1)
12139 && tree_expr_nonzero_p (arg0))
12141 tree res = constant_boolean_node (code==NE_EXPR, type);
12142 return omit_one_operand (type, res, arg0);
12145 /* Fold -X op -Y as X op Y, where op is eq/ne. */
12146 if (TREE_CODE (arg0) == NEGATE_EXPR
12147 && TREE_CODE (arg1) == NEGATE_EXPR)
12148 return fold_build2 (code, type,
12149 TREE_OPERAND (arg0, 0),
12150 TREE_OPERAND (arg1, 0));
12152 /* Fold (X & C) op (Y & C) as (X ^ Y) & C op 0", and symmetries. */
12153 if (TREE_CODE (arg0) == BIT_AND_EXPR
12154 && TREE_CODE (arg1) == BIT_AND_EXPR)
12156 tree arg00 = TREE_OPERAND (arg0, 0);
12157 tree arg01 = TREE_OPERAND (arg0, 1);
12158 tree arg10 = TREE_OPERAND (arg1, 0);
12159 tree arg11 = TREE_OPERAND (arg1, 1);
12160 tree itype = TREE_TYPE (arg0);
12162 if (operand_equal_p (arg01, arg11, 0))
12163 return fold_build2 (code, type,
12164 fold_build2 (BIT_AND_EXPR, itype,
12165 fold_build2 (BIT_XOR_EXPR, itype,
12168 build_int_cst (itype, 0));
12170 if (operand_equal_p (arg01, arg10, 0))
12171 return fold_build2 (code, type,
12172 fold_build2 (BIT_AND_EXPR, itype,
12173 fold_build2 (BIT_XOR_EXPR, itype,
12176 build_int_cst (itype, 0));
12178 if (operand_equal_p (arg00, arg11, 0))
12179 return fold_build2 (code, type,
12180 fold_build2 (BIT_AND_EXPR, itype,
12181 fold_build2 (BIT_XOR_EXPR, itype,
12184 build_int_cst (itype, 0));
12186 if (operand_equal_p (arg00, arg10, 0))
12187 return fold_build2 (code, type,
12188 fold_build2 (BIT_AND_EXPR, itype,
12189 fold_build2 (BIT_XOR_EXPR, itype,
12192 build_int_cst (itype, 0));
12195 if (TREE_CODE (arg0) == BIT_XOR_EXPR
12196 && TREE_CODE (arg1) == BIT_XOR_EXPR)
12198 tree arg00 = TREE_OPERAND (arg0, 0);
12199 tree arg01 = TREE_OPERAND (arg0, 1);
12200 tree arg10 = TREE_OPERAND (arg1, 0);
12201 tree arg11 = TREE_OPERAND (arg1, 1);
12202 tree itype = TREE_TYPE (arg0);
12204 /* Optimize (X ^ Z) op (Y ^ Z) as X op Y, and symmetries.
12205 operand_equal_p guarantees no side-effects so we don't need
12206 to use omit_one_operand on Z. */
12207 if (operand_equal_p (arg01, arg11, 0))
12208 return fold_build2 (code, type, arg00, arg10);
12209 if (operand_equal_p (arg01, arg10, 0))
12210 return fold_build2 (code, type, arg00, arg11);
12211 if (operand_equal_p (arg00, arg11, 0))
12212 return fold_build2 (code, type, arg01, arg10);
12213 if (operand_equal_p (arg00, arg10, 0))
12214 return fold_build2 (code, type, arg01, arg11);
12216 /* Optimize (X ^ C1) op (Y ^ C2) as (X ^ (C1 ^ C2)) op Y. */
12217 if (TREE_CODE (arg01) == INTEGER_CST
12218 && TREE_CODE (arg11) == INTEGER_CST)
12219 return fold_build2 (code, type,
12220 fold_build2 (BIT_XOR_EXPR, itype, arg00,
12221 fold_build2 (BIT_XOR_EXPR, itype,
12226 /* Attempt to simplify equality/inequality comparisons of complex
12227 values. Only lower the comparison if the result is known or
12228 can be simplified to a single scalar comparison. */
12229 if ((TREE_CODE (arg0) == COMPLEX_EXPR
12230 || TREE_CODE (arg0) == COMPLEX_CST)
12231 && (TREE_CODE (arg1) == COMPLEX_EXPR
12232 || TREE_CODE (arg1) == COMPLEX_CST))
12234 tree real0, imag0, real1, imag1;
12237 if (TREE_CODE (arg0) == COMPLEX_EXPR)
12239 real0 = TREE_OPERAND (arg0, 0);
12240 imag0 = TREE_OPERAND (arg0, 1);
12244 real0 = TREE_REALPART (arg0);
12245 imag0 = TREE_IMAGPART (arg0);
12248 if (TREE_CODE (arg1) == COMPLEX_EXPR)
12250 real1 = TREE_OPERAND (arg1, 0);
12251 imag1 = TREE_OPERAND (arg1, 1);
12255 real1 = TREE_REALPART (arg1);
12256 imag1 = TREE_IMAGPART (arg1);
12259 rcond = fold_binary (code, type, real0, real1);
12260 if (rcond && TREE_CODE (rcond) == INTEGER_CST)
12262 if (integer_zerop (rcond))
12264 if (code == EQ_EXPR)
12265 return omit_two_operands (type, boolean_false_node,
12267 return fold_build2 (NE_EXPR, type, imag0, imag1);
12271 if (code == NE_EXPR)
12272 return omit_two_operands (type, boolean_true_node,
12274 return fold_build2 (EQ_EXPR, type, imag0, imag1);
12278 icond = fold_binary (code, type, imag0, imag1);
12279 if (icond && TREE_CODE (icond) == INTEGER_CST)
12281 if (integer_zerop (icond))
12283 if (code == EQ_EXPR)
12284 return omit_two_operands (type, boolean_false_node,
12286 return fold_build2 (NE_EXPR, type, real0, real1);
12290 if (code == NE_EXPR)
12291 return omit_two_operands (type, boolean_true_node,
12293 return fold_build2 (EQ_EXPR, type, real0, real1);
12304 tem = fold_comparison (code, type, op0, op1);
12305 if (tem != NULL_TREE)
12308 /* Transform comparisons of the form X +- C CMP X. */
12309 if ((TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
12310 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0)
12311 && ((TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
12312 && !HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0))))
12313 || (TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
12314 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))))
12316 tree arg01 = TREE_OPERAND (arg0, 1);
12317 enum tree_code code0 = TREE_CODE (arg0);
12320 if (TREE_CODE (arg01) == REAL_CST)
12321 is_positive = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg01)) ? -1 : 1;
12323 is_positive = tree_int_cst_sgn (arg01);
12325 /* (X - c) > X becomes false. */
12326 if (code == GT_EXPR
12327 && ((code0 == MINUS_EXPR && is_positive >= 0)
12328 || (code0 == PLUS_EXPR && is_positive <= 0)))
12330 if (TREE_CODE (arg01) == INTEGER_CST
12331 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12332 fold_overflow_warning (("assuming signed overflow does not "
12333 "occur when assuming that (X - c) > X "
12334 "is always false"),
12335 WARN_STRICT_OVERFLOW_ALL);
12336 return constant_boolean_node (0, type);
12339 /* Likewise (X + c) < X becomes false. */
12340 if (code == LT_EXPR
12341 && ((code0 == PLUS_EXPR && is_positive >= 0)
12342 || (code0 == MINUS_EXPR && is_positive <= 0)))
12344 if (TREE_CODE (arg01) == INTEGER_CST
12345 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12346 fold_overflow_warning (("assuming signed overflow does not "
12347 "occur when assuming that "
12348 "(X + c) < X is always false"),
12349 WARN_STRICT_OVERFLOW_ALL);
12350 return constant_boolean_node (0, type);
12353 /* Convert (X - c) <= X to true. */
12354 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12356 && ((code0 == MINUS_EXPR && is_positive >= 0)
12357 || (code0 == PLUS_EXPR && is_positive <= 0)))
12359 if (TREE_CODE (arg01) == INTEGER_CST
12360 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12361 fold_overflow_warning (("assuming signed overflow does not "
12362 "occur when assuming that "
12363 "(X - c) <= X is always true"),
12364 WARN_STRICT_OVERFLOW_ALL);
12365 return constant_boolean_node (1, type);
12368 /* Convert (X + c) >= X to true. */
12369 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1)))
12371 && ((code0 == PLUS_EXPR && is_positive >= 0)
12372 || (code0 == MINUS_EXPR && is_positive <= 0)))
12374 if (TREE_CODE (arg01) == INTEGER_CST
12375 && TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12376 fold_overflow_warning (("assuming signed overflow does not "
12377 "occur when assuming that "
12378 "(X + c) >= X is always true"),
12379 WARN_STRICT_OVERFLOW_ALL);
12380 return constant_boolean_node (1, type);
12383 if (TREE_CODE (arg01) == INTEGER_CST)
12385 /* Convert X + c > X and X - c < X to true for integers. */
12386 if (code == GT_EXPR
12387 && ((code0 == PLUS_EXPR && is_positive > 0)
12388 || (code0 == MINUS_EXPR && is_positive < 0)))
12390 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12391 fold_overflow_warning (("assuming signed overflow does "
12392 "not occur when assuming that "
12393 "(X + c) > X is always true"),
12394 WARN_STRICT_OVERFLOW_ALL);
12395 return constant_boolean_node (1, type);
12398 if (code == LT_EXPR
12399 && ((code0 == MINUS_EXPR && is_positive > 0)
12400 || (code0 == PLUS_EXPR && is_positive < 0)))
12402 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12403 fold_overflow_warning (("assuming signed overflow does "
12404 "not occur when assuming that "
12405 "(X - c) < X is always true"),
12406 WARN_STRICT_OVERFLOW_ALL);
12407 return constant_boolean_node (1, type);
12410 /* Convert X + c <= X and X - c >= X to false for integers. */
12411 if (code == LE_EXPR
12412 && ((code0 == PLUS_EXPR && is_positive > 0)
12413 || (code0 == MINUS_EXPR && is_positive < 0)))
12415 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12416 fold_overflow_warning (("assuming signed overflow does "
12417 "not occur when assuming that "
12418 "(X + c) <= X is always false"),
12419 WARN_STRICT_OVERFLOW_ALL);
12420 return constant_boolean_node (0, type);
12423 if (code == GE_EXPR
12424 && ((code0 == MINUS_EXPR && is_positive > 0)
12425 || (code0 == PLUS_EXPR && is_positive < 0)))
12427 if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (arg1)))
12428 fold_overflow_warning (("assuming signed overflow does "
12429 "not occur when assuming that "
12430 "(X - c) >= X is always false"),
12431 WARN_STRICT_OVERFLOW_ALL);
12432 return constant_boolean_node (0, type);
12437 /* Comparisons with the highest or lowest possible integer of
12438 the specified precision will have known values. */
12440 tree arg1_type = TREE_TYPE (arg1);
12441 unsigned int width = TYPE_PRECISION (arg1_type);
12443 if (TREE_CODE (arg1) == INTEGER_CST
12444 && !TREE_OVERFLOW (arg1)
12445 && width <= 2 * HOST_BITS_PER_WIDE_INT
12446 && (INTEGRAL_TYPE_P (arg1_type) || POINTER_TYPE_P (arg1_type)))
12448 HOST_WIDE_INT signed_max_hi;
12449 unsigned HOST_WIDE_INT signed_max_lo;
12450 unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
12452 if (width <= HOST_BITS_PER_WIDE_INT)
12454 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12459 if (TYPE_UNSIGNED (arg1_type))
12461 max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12467 max_lo = signed_max_lo;
12468 min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12474 width -= HOST_BITS_PER_WIDE_INT;
12475 signed_max_lo = -1;
12476 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
12481 if (TYPE_UNSIGNED (arg1_type))
12483 max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
12488 max_hi = signed_max_hi;
12489 min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
12493 if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
12494 && TREE_INT_CST_LOW (arg1) == max_lo)
12498 return omit_one_operand (type, integer_zero_node, arg0);
12501 return fold_build2 (EQ_EXPR, type, op0, op1);
12504 return omit_one_operand (type, integer_one_node, arg0);
12507 return fold_build2 (NE_EXPR, type, op0, op1);
12509 /* The GE_EXPR and LT_EXPR cases above are not normally
12510 reached because of previous transformations. */
12515 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12517 && TREE_INT_CST_LOW (arg1) == max_lo - 1)
12521 arg1 = const_binop (PLUS_EXPR, arg1,
12522 build_int_cst (TREE_TYPE (arg1), 1), 0);
12523 return fold_build2 (EQ_EXPR, type,
12524 fold_convert (TREE_TYPE (arg1), arg0),
12527 arg1 = const_binop (PLUS_EXPR, arg1,
12528 build_int_cst (TREE_TYPE (arg1), 1), 0);
12529 return fold_build2 (NE_EXPR, type,
12530 fold_convert (TREE_TYPE (arg1), arg0),
12535 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12537 && TREE_INT_CST_LOW (arg1) == min_lo)
12541 return omit_one_operand (type, integer_zero_node, arg0);
12544 return fold_build2 (EQ_EXPR, type, op0, op1);
12547 return omit_one_operand (type, integer_one_node, arg0);
12550 return fold_build2 (NE_EXPR, type, op0, op1);
12555 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
12557 && TREE_INT_CST_LOW (arg1) == min_lo + 1)
12561 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
12562 return fold_build2 (NE_EXPR, type,
12563 fold_convert (TREE_TYPE (arg1), arg0),
12566 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
12567 return fold_build2 (EQ_EXPR, type,
12568 fold_convert (TREE_TYPE (arg1), arg0),
12574 else if (TREE_INT_CST_HIGH (arg1) == signed_max_hi
12575 && TREE_INT_CST_LOW (arg1) == signed_max_lo
12576 && TYPE_UNSIGNED (arg1_type)
12577 /* We will flip the signedness of the comparison operator
12578 associated with the mode of arg1, so the sign bit is
12579 specified by this mode. Check that arg1 is the signed
12580 max associated with this sign bit. */
12581 && width == GET_MODE_BITSIZE (TYPE_MODE (arg1_type))
12582 /* signed_type does not work on pointer types. */
12583 && INTEGRAL_TYPE_P (arg1_type))
12585 /* The following case also applies to X < signed_max+1
12586 and X >= signed_max+1 because previous transformations. */
12587 if (code == LE_EXPR || code == GT_EXPR)
12590 st = signed_type_for (TREE_TYPE (arg1));
12591 return fold_build2 (code == LE_EXPR ? GE_EXPR : LT_EXPR,
12592 type, fold_convert (st, arg0),
12593 build_int_cst (st, 0));
12599 /* If we are comparing an ABS_EXPR with a constant, we can
12600 convert all the cases into explicit comparisons, but they may
12601 well not be faster than doing the ABS and one comparison.
12602 But ABS (X) <= C is a range comparison, which becomes a subtraction
12603 and a comparison, and is probably faster. */
12604 if (code == LE_EXPR
12605 && TREE_CODE (arg1) == INTEGER_CST
12606 && TREE_CODE (arg0) == ABS_EXPR
12607 && ! TREE_SIDE_EFFECTS (arg0)
12608 && (0 != (tem = negate_expr (arg1)))
12609 && TREE_CODE (tem) == INTEGER_CST
12610 && !TREE_OVERFLOW (tem))
12611 return fold_build2 (TRUTH_ANDIF_EXPR, type,
12612 build2 (GE_EXPR, type,
12613 TREE_OPERAND (arg0, 0), tem),
12614 build2 (LE_EXPR, type,
12615 TREE_OPERAND (arg0, 0), arg1));
12617 /* Convert ABS_EXPR<x> >= 0 to true. */
12618 strict_overflow_p = false;
12619 if (code == GE_EXPR
12620 && (integer_zerop (arg1)
12621 || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
12622 && real_zerop (arg1)))
12623 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12625 if (strict_overflow_p)
12626 fold_overflow_warning (("assuming signed overflow does not occur "
12627 "when simplifying comparison of "
12628 "absolute value and zero"),
12629 WARN_STRICT_OVERFLOW_CONDITIONAL);
12630 return omit_one_operand (type, integer_one_node, arg0);
12633 /* Convert ABS_EXPR<x> < 0 to false. */
12634 strict_overflow_p = false;
12635 if (code == LT_EXPR
12636 && (integer_zerop (arg1) || real_zerop (arg1))
12637 && tree_expr_nonnegative_warnv_p (arg0, &strict_overflow_p))
12639 if (strict_overflow_p)
12640 fold_overflow_warning (("assuming signed overflow does not occur "
12641 "when simplifying comparison of "
12642 "absolute value and zero"),
12643 WARN_STRICT_OVERFLOW_CONDITIONAL);
12644 return omit_one_operand (type, integer_zero_node, arg0);
12647 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
12648 and similarly for >= into !=. */
12649 if ((code == LT_EXPR || code == GE_EXPR)
12650 && TYPE_UNSIGNED (TREE_TYPE (arg0))
12651 && TREE_CODE (arg1) == LSHIFT_EXPR
12652 && integer_onep (TREE_OPERAND (arg1, 0)))
12653 return build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12654 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12655 TREE_OPERAND (arg1, 1)),
12656 build_int_cst (TREE_TYPE (arg0), 0));
12658 if ((code == LT_EXPR || code == GE_EXPR)
12659 && TYPE_UNSIGNED (TREE_TYPE (arg0))
12660 && CONVERT_EXPR_P (arg1)
12661 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
12662 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
12664 build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
12665 fold_convert (TREE_TYPE (arg0),
12666 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
12667 TREE_OPERAND (TREE_OPERAND (arg1, 0),
12669 build_int_cst (TREE_TYPE (arg0), 0));
12673 case UNORDERED_EXPR:
12681 if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
12683 t1 = fold_relational_const (code, type, arg0, arg1);
12684 if (t1 != NULL_TREE)
12688 /* If the first operand is NaN, the result is constant. */
12689 if (TREE_CODE (arg0) == REAL_CST
12690 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
12691 && (code != LTGT_EXPR || ! flag_trapping_math))
12693 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
12694 ? integer_zero_node
12695 : integer_one_node;
12696 return omit_one_operand (type, t1, arg1);
12699 /* If the second operand is NaN, the result is constant. */
12700 if (TREE_CODE (arg1) == REAL_CST
12701 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
12702 && (code != LTGT_EXPR || ! flag_trapping_math))
12704 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
12705 ? integer_zero_node
12706 : integer_one_node;
12707 return omit_one_operand (type, t1, arg0);
12710 /* Simplify unordered comparison of something with itself. */
12711 if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
12712 && operand_equal_p (arg0, arg1, 0))
12713 return constant_boolean_node (1, type);
12715 if (code == LTGT_EXPR
12716 && !flag_trapping_math
12717 && operand_equal_p (arg0, arg1, 0))
12718 return constant_boolean_node (0, type);
12720 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
12722 tree targ0 = strip_float_extensions (arg0);
12723 tree targ1 = strip_float_extensions (arg1);
12724 tree newtype = TREE_TYPE (targ0);
12726 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
12727 newtype = TREE_TYPE (targ1);
12729 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
12730 return fold_build2 (code, type, fold_convert (newtype, targ0),
12731 fold_convert (newtype, targ1));
12736 case COMPOUND_EXPR:
12737 /* When pedantic, a compound expression can be neither an lvalue
12738 nor an integer constant expression. */
12739 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
12741 /* Don't let (0, 0) be null pointer constant. */
12742 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
12743 : fold_convert (type, arg1);
12744 return pedantic_non_lvalue (tem);
12747 if ((TREE_CODE (arg0) == REAL_CST
12748 && TREE_CODE (arg1) == REAL_CST)
12749 || (TREE_CODE (arg0) == INTEGER_CST
12750 && TREE_CODE (arg1) == INTEGER_CST))
12751 return build_complex (type, arg0, arg1);
12755 /* An ASSERT_EXPR should never be passed to fold_binary. */
12756 gcc_unreachable ();
12760 } /* switch (code) */
12763 /* Callback for walk_tree, looking for LABEL_EXPR.
12764 Returns tree TP if it is LABEL_EXPR. Otherwise it returns NULL_TREE.
12765 Do not check the sub-tree of GOTO_EXPR. */
12768 contains_label_1 (tree *tp,
12769 int *walk_subtrees,
12770 void *data ATTRIBUTE_UNUSED)
12772 switch (TREE_CODE (*tp))
12777 *walk_subtrees = 0;
12784 /* Checks whether the sub-tree ST contains a label LABEL_EXPR which is
12785 accessible from outside the sub-tree. Returns NULL_TREE if no
12786 addressable label is found. */
12789 contains_label_p (tree st)
12791 return (walk_tree (&st, contains_label_1 , NULL, NULL) != NULL_TREE);
12794 /* Fold a ternary expression of code CODE and type TYPE with operands
12795 OP0, OP1, and OP2. Return the folded expression if folding is
12796 successful. Otherwise, return NULL_TREE. */
12799 fold_ternary (enum tree_code code, tree type, tree op0, tree op1, tree op2)
12802 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
12803 enum tree_code_class kind = TREE_CODE_CLASS (code);
12805 gcc_assert (IS_EXPR_CODE_CLASS (kind)
12806 && TREE_CODE_LENGTH (code) == 3);
12808 /* Strip any conversions that don't change the mode. This is safe
12809 for every expression, except for a comparison expression because
12810 its signedness is derived from its operands. So, in the latter
12811 case, only strip conversions that don't change the signedness.
12813 Note that this is done as an internal manipulation within the
12814 constant folder, in order to find the simplest representation of
12815 the arguments so that their form can be studied. In any cases,
12816 the appropriate type conversions should be put back in the tree
12817 that will get out of the constant folder. */
12832 case COMPONENT_REF:
12833 if (TREE_CODE (arg0) == CONSTRUCTOR
12834 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
12836 unsigned HOST_WIDE_INT idx;
12838 FOR_EACH_CONSTRUCTOR_ELT (CONSTRUCTOR_ELTS (arg0), idx, field, value)
12845 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
12846 so all simple results must be passed through pedantic_non_lvalue. */
12847 if (TREE_CODE (arg0) == INTEGER_CST)
12849 tree unused_op = integer_zerop (arg0) ? op1 : op2;
12850 tem = integer_zerop (arg0) ? op2 : op1;
12851 /* Only optimize constant conditions when the selected branch
12852 has the same type as the COND_EXPR. This avoids optimizing
12853 away "c ? x : throw", where the throw has a void type.
12854 Avoid throwing away that operand which contains label. */
12855 if ((!TREE_SIDE_EFFECTS (unused_op)
12856 || !contains_label_p (unused_op))
12857 && (! VOID_TYPE_P (TREE_TYPE (tem))
12858 || VOID_TYPE_P (type)))
12859 return pedantic_non_lvalue (tem);
12862 if (operand_equal_p (arg1, op2, 0))
12863 return pedantic_omit_one_operand (type, arg1, arg0);
12865 /* If we have A op B ? A : C, we may be able to convert this to a
12866 simpler expression, depending on the operation and the values
12867 of B and C. Signed zeros prevent all of these transformations,
12868 for reasons given above each one.
12870 Also try swapping the arguments and inverting the conditional. */
12871 if (COMPARISON_CLASS_P (arg0)
12872 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
12873 arg1, TREE_OPERAND (arg0, 1))
12874 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
12876 tem = fold_cond_expr_with_comparison (type, arg0, op1, op2);
12881 if (COMPARISON_CLASS_P (arg0)
12882 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
12884 TREE_OPERAND (arg0, 1))
12885 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
12887 tem = fold_truth_not_expr (arg0);
12888 if (tem && COMPARISON_CLASS_P (tem))
12890 tem = fold_cond_expr_with_comparison (type, tem, op2, op1);
12896 /* If the second operand is simpler than the third, swap them
12897 since that produces better jump optimization results. */
12898 if (truth_value_p (TREE_CODE (arg0))
12899 && tree_swap_operands_p (op1, op2, false))
12901 /* See if this can be inverted. If it can't, possibly because
12902 it was a floating-point inequality comparison, don't do
12904 tem = fold_truth_not_expr (arg0);
12906 return fold_build3 (code, type, tem, op2, op1);
12909 /* Convert A ? 1 : 0 to simply A. */
12910 if (integer_onep (op1)
12911 && integer_zerop (op2)
12912 /* If we try to convert OP0 to our type, the
12913 call to fold will try to move the conversion inside
12914 a COND, which will recurse. In that case, the COND_EXPR
12915 is probably the best choice, so leave it alone. */
12916 && type == TREE_TYPE (arg0))
12917 return pedantic_non_lvalue (arg0);
12919 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
12920 over COND_EXPR in cases such as floating point comparisons. */
12921 if (integer_zerop (op1)
12922 && integer_onep (op2)
12923 && truth_value_p (TREE_CODE (arg0)))
12924 return pedantic_non_lvalue (fold_convert (type,
12925 invert_truthvalue (arg0)));
12927 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
12928 if (TREE_CODE (arg0) == LT_EXPR
12929 && integer_zerop (TREE_OPERAND (arg0, 1))
12930 && integer_zerop (op2)
12931 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
12933 /* sign_bit_p only checks ARG1 bits within A's precision.
12934 If <sign bit of A> has wider type than A, bits outside
12935 of A's precision in <sign bit of A> need to be checked.
12936 If they are all 0, this optimization needs to be done
12937 in unsigned A's type, if they are all 1 in signed A's type,
12938 otherwise this can't be done. */
12939 if (TYPE_PRECISION (TREE_TYPE (tem))
12940 < TYPE_PRECISION (TREE_TYPE (arg1))
12941 && TYPE_PRECISION (TREE_TYPE (tem))
12942 < TYPE_PRECISION (type))
12944 unsigned HOST_WIDE_INT mask_lo;
12945 HOST_WIDE_INT mask_hi;
12946 int inner_width, outer_width;
12949 inner_width = TYPE_PRECISION (TREE_TYPE (tem));
12950 outer_width = TYPE_PRECISION (TREE_TYPE (arg1));
12951 if (outer_width > TYPE_PRECISION (type))
12952 outer_width = TYPE_PRECISION (type);
12954 if (outer_width > HOST_BITS_PER_WIDE_INT)
12956 mask_hi = ((unsigned HOST_WIDE_INT) -1
12957 >> (2 * HOST_BITS_PER_WIDE_INT - outer_width));
12963 mask_lo = ((unsigned HOST_WIDE_INT) -1
12964 >> (HOST_BITS_PER_WIDE_INT - outer_width));
12966 if (inner_width > HOST_BITS_PER_WIDE_INT)
12968 mask_hi &= ~((unsigned HOST_WIDE_INT) -1
12969 >> (HOST_BITS_PER_WIDE_INT - inner_width));
12973 mask_lo &= ~((unsigned HOST_WIDE_INT) -1
12974 >> (HOST_BITS_PER_WIDE_INT - inner_width));
12976 if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == mask_hi
12977 && (TREE_INT_CST_LOW (arg1) & mask_lo) == mask_lo)
12979 tem_type = signed_type_for (TREE_TYPE (tem));
12980 tem = fold_convert (tem_type, tem);
12982 else if ((TREE_INT_CST_HIGH (arg1) & mask_hi) == 0
12983 && (TREE_INT_CST_LOW (arg1) & mask_lo) == 0)
12985 tem_type = unsigned_type_for (TREE_TYPE (tem));
12986 tem = fold_convert (tem_type, tem);
12993 return fold_convert (type,
12994 fold_build2 (BIT_AND_EXPR,
12995 TREE_TYPE (tem), tem,
12996 fold_convert (TREE_TYPE (tem),
13000 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
13001 already handled above. */
13002 if (TREE_CODE (arg0) == BIT_AND_EXPR
13003 && integer_onep (TREE_OPERAND (arg0, 1))
13004 && integer_zerop (op2)
13005 && integer_pow2p (arg1))
13007 tree tem = TREE_OPERAND (arg0, 0);
13009 if (TREE_CODE (tem) == RSHIFT_EXPR
13010 && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
13011 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
13012 TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
13013 return fold_build2 (BIT_AND_EXPR, type,
13014 TREE_OPERAND (tem, 0), arg1);
13017 /* A & N ? N : 0 is simply A & N if N is a power of two. This
13018 is probably obsolete because the first operand should be a
13019 truth value (that's why we have the two cases above), but let's
13020 leave it in until we can confirm this for all front-ends. */
13021 if (integer_zerop (op2)
13022 && TREE_CODE (arg0) == NE_EXPR
13023 && integer_zerop (TREE_OPERAND (arg0, 1))
13024 && integer_pow2p (arg1)
13025 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
13026 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
13027 arg1, OEP_ONLY_CONST))
13028 return pedantic_non_lvalue (fold_convert (type,
13029 TREE_OPERAND (arg0, 0)));
13031 /* Convert A ? B : 0 into A && B if A and B are truth values. */
13032 if (integer_zerop (op2)
13033 && truth_value_p (TREE_CODE (arg0))
13034 && truth_value_p (TREE_CODE (arg1)))
13035 return fold_build2 (TRUTH_ANDIF_EXPR, type,
13036 fold_convert (type, arg0),
13039 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
13040 if (integer_onep (op2)
13041 && truth_value_p (TREE_CODE (arg0))
13042 && truth_value_p (TREE_CODE (arg1)))
13044 /* Only perform transformation if ARG0 is easily inverted. */
13045 tem = fold_truth_not_expr (arg0);
13047 return fold_build2 (TRUTH_ORIF_EXPR, type,
13048 fold_convert (type, tem),
13052 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
13053 if (integer_zerop (arg1)
13054 && truth_value_p (TREE_CODE (arg0))
13055 && truth_value_p (TREE_CODE (op2)))
13057 /* Only perform transformation if ARG0 is easily inverted. */
13058 tem = fold_truth_not_expr (arg0);
13060 return fold_build2 (TRUTH_ANDIF_EXPR, type,
13061 fold_convert (type, tem),
13065 /* Convert A ? 1 : B into A || B if A and B are truth values. */
13066 if (integer_onep (arg1)
13067 && truth_value_p (TREE_CODE (arg0))
13068 && truth_value_p (TREE_CODE (op2)))
13069 return fold_build2 (TRUTH_ORIF_EXPR, type,
13070 fold_convert (type, arg0),
13076 /* CALL_EXPRs used to be ternary exprs. Catch any mistaken uses
13077 of fold_ternary on them. */
13078 gcc_unreachable ();
13080 case BIT_FIELD_REF:
13081 if ((TREE_CODE (arg0) == VECTOR_CST
13082 || (TREE_CODE (arg0) == CONSTRUCTOR && TREE_CONSTANT (arg0)))
13083 && type == TREE_TYPE (TREE_TYPE (arg0)))
13085 unsigned HOST_WIDE_INT width = tree_low_cst (arg1, 1);
13086 unsigned HOST_WIDE_INT idx = tree_low_cst (op2, 1);
13089 && simple_cst_equal (arg1, TYPE_SIZE (type)) == 1
13090 && (idx % width) == 0
13091 && (idx = idx / width)
13092 < TYPE_VECTOR_SUBPARTS (TREE_TYPE (arg0)))
13094 tree elements = NULL_TREE;
13096 if (TREE_CODE (arg0) == VECTOR_CST)
13097 elements = TREE_VECTOR_CST_ELTS (arg0);
13100 unsigned HOST_WIDE_INT idx;
13103 FOR_EACH_CONSTRUCTOR_VALUE (CONSTRUCTOR_ELTS (arg0), idx, value)
13104 elements = tree_cons (NULL_TREE, value, elements);
13106 while (idx-- > 0 && elements)
13107 elements = TREE_CHAIN (elements);
13109 return TREE_VALUE (elements);
13111 return fold_convert (type, integer_zero_node);
13115 /* A bit-field-ref that referenced the full argument can be stripped. */
13116 if (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
13117 && TYPE_PRECISION (TREE_TYPE (arg0)) == tree_low_cst (arg1, 1)
13118 && integer_zerop (op2))
13119 return fold_convert (type, arg0);
13125 } /* switch (code) */
13128 /* Perform constant folding and related simplification of EXPR.
13129 The related simplifications include x*1 => x, x*0 => 0, etc.,
13130 and application of the associative law.
13131 NOP_EXPR conversions may be removed freely (as long as we
13132 are careful not to change the type of the overall expression).
13133 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
13134 but we can constant-fold them if they have constant operands. */
13136 #ifdef ENABLE_FOLD_CHECKING
13137 # define fold(x) fold_1 (x)
13138 static tree fold_1 (tree);
13144 const tree t = expr;
13145 enum tree_code code = TREE_CODE (t);
13146 enum tree_code_class kind = TREE_CODE_CLASS (code);
13149 /* Return right away if a constant. */
13150 if (kind == tcc_constant)
13153 /* CALL_EXPR-like objects with variable numbers of operands are
13154 treated specially. */
13155 if (kind == tcc_vl_exp)
13157 if (code == CALL_EXPR)
13159 tem = fold_call_expr (expr, false);
13160 return tem ? tem : expr;
13165 if (IS_EXPR_CODE_CLASS (kind))
13167 tree type = TREE_TYPE (t);
13168 tree op0, op1, op2;
13170 switch (TREE_CODE_LENGTH (code))
13173 op0 = TREE_OPERAND (t, 0);
13174 tem = fold_unary (code, type, op0);
13175 return tem ? tem : expr;
13177 op0 = TREE_OPERAND (t, 0);
13178 op1 = TREE_OPERAND (t, 1);
13179 tem = fold_binary (code, type, op0, op1);
13180 return tem ? tem : expr;
13182 op0 = TREE_OPERAND (t, 0);
13183 op1 = TREE_OPERAND (t, 1);
13184 op2 = TREE_OPERAND (t, 2);
13185 tem = fold_ternary (code, type, op0, op1, op2);
13186 return tem ? tem : expr;
13196 tree op0 = TREE_OPERAND (t, 0);
13197 tree op1 = TREE_OPERAND (t, 1);
13199 if (TREE_CODE (op1) == INTEGER_CST
13200 && TREE_CODE (op0) == CONSTRUCTOR
13201 && ! type_contains_placeholder_p (TREE_TYPE (op0)))
13203 VEC(constructor_elt,gc) *elts = CONSTRUCTOR_ELTS (op0);
13204 unsigned HOST_WIDE_INT end = VEC_length (constructor_elt, elts);
13205 unsigned HOST_WIDE_INT begin = 0;
13207 /* Find a matching index by means of a binary search. */
13208 while (begin != end)
13210 unsigned HOST_WIDE_INT middle = (begin + end) / 2;
13211 tree index = VEC_index (constructor_elt, elts, middle)->index;
13213 if (TREE_CODE (index) == INTEGER_CST
13214 && tree_int_cst_lt (index, op1))
13215 begin = middle + 1;
13216 else if (TREE_CODE (index) == INTEGER_CST
13217 && tree_int_cst_lt (op1, index))
13219 else if (TREE_CODE (index) == RANGE_EXPR
13220 && tree_int_cst_lt (TREE_OPERAND (index, 1), op1))
13221 begin = middle + 1;
13222 else if (TREE_CODE (index) == RANGE_EXPR
13223 && tree_int_cst_lt (op1, TREE_OPERAND (index, 0)))
13226 return VEC_index (constructor_elt, elts, middle)->value;
13234 return fold (DECL_INITIAL (t));
13238 } /* switch (code) */
13241 #ifdef ENABLE_FOLD_CHECKING
13244 static void fold_checksum_tree (const_tree, struct md5_ctx *, htab_t);
13245 static void fold_check_failed (const_tree, const_tree);
13246 void print_fold_checksum (const_tree);
13248 /* When --enable-checking=fold, compute a digest of expr before
13249 and after actual fold call to see if fold did not accidentally
13250 change original expr. */
13256 struct md5_ctx ctx;
13257 unsigned char checksum_before[16], checksum_after[16];
13260 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13261 md5_init_ctx (&ctx);
13262 fold_checksum_tree (expr, &ctx, ht);
13263 md5_finish_ctx (&ctx, checksum_before);
13266 ret = fold_1 (expr);
13268 md5_init_ctx (&ctx);
13269 fold_checksum_tree (expr, &ctx, ht);
13270 md5_finish_ctx (&ctx, checksum_after);
13273 if (memcmp (checksum_before, checksum_after, 16))
13274 fold_check_failed (expr, ret);
13280 print_fold_checksum (const_tree expr)
13282 struct md5_ctx ctx;
13283 unsigned char checksum[16], cnt;
13286 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13287 md5_init_ctx (&ctx);
13288 fold_checksum_tree (expr, &ctx, ht);
13289 md5_finish_ctx (&ctx, checksum);
13291 for (cnt = 0; cnt < 16; ++cnt)
13292 fprintf (stderr, "%02x", checksum[cnt]);
13293 putc ('\n', stderr);
13297 fold_check_failed (const_tree expr ATTRIBUTE_UNUSED, const_tree ret ATTRIBUTE_UNUSED)
13299 internal_error ("fold check: original tree changed by fold");
13303 fold_checksum_tree (const_tree expr, struct md5_ctx *ctx, htab_t ht)
13306 enum tree_code code;
13307 struct tree_function_decl buf;
13312 gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
13313 <= sizeof (struct tree_function_decl))
13314 && sizeof (struct tree_type) <= sizeof (struct tree_function_decl));
13317 slot = (const void **) htab_find_slot (ht, expr, INSERT);
13321 code = TREE_CODE (expr);
13322 if (TREE_CODE_CLASS (code) == tcc_declaration
13323 && DECL_ASSEMBLER_NAME_SET_P (expr))
13325 /* Allow DECL_ASSEMBLER_NAME to be modified. */
13326 memcpy ((char *) &buf, expr, tree_size (expr));
13327 SET_DECL_ASSEMBLER_NAME ((tree)&buf, NULL);
13328 expr = (tree) &buf;
13330 else if (TREE_CODE_CLASS (code) == tcc_type
13331 && (TYPE_POINTER_TO (expr) || TYPE_REFERENCE_TO (expr)
13332 || TYPE_CACHED_VALUES_P (expr)
13333 || TYPE_CONTAINS_PLACEHOLDER_INTERNAL (expr)))
13335 /* Allow these fields to be modified. */
13337 memcpy ((char *) &buf, expr, tree_size (expr));
13338 expr = tmp = (tree) &buf;
13339 TYPE_CONTAINS_PLACEHOLDER_INTERNAL (tmp) = 0;
13340 TYPE_POINTER_TO (tmp) = NULL;
13341 TYPE_REFERENCE_TO (tmp) = NULL;
13342 if (TYPE_CACHED_VALUES_P (tmp))
13344 TYPE_CACHED_VALUES_P (tmp) = 0;
13345 TYPE_CACHED_VALUES (tmp) = NULL;
13348 md5_process_bytes (expr, tree_size (expr), ctx);
13349 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
13350 if (TREE_CODE_CLASS (code) != tcc_type
13351 && TREE_CODE_CLASS (code) != tcc_declaration
13352 && code != TREE_LIST
13353 && code != SSA_NAME)
13354 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
13355 switch (TREE_CODE_CLASS (code))
13361 md5_process_bytes (TREE_STRING_POINTER (expr),
13362 TREE_STRING_LENGTH (expr), ctx);
13365 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
13366 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
13369 fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
13375 case tcc_exceptional:
13379 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
13380 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
13381 expr = TREE_CHAIN (expr);
13382 goto recursive_label;
13385 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
13386 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
13392 case tcc_expression:
13393 case tcc_reference:
13394 case tcc_comparison:
13397 case tcc_statement:
13399 len = TREE_OPERAND_LENGTH (expr);
13400 for (i = 0; i < len; ++i)
13401 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
13403 case tcc_declaration:
13404 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
13405 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
13406 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_COMMON))
13408 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
13409 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
13410 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
13411 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
13412 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
13414 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_WITH_VIS))
13415 fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
13417 if (CODE_CONTAINS_STRUCT (TREE_CODE (expr), TS_DECL_NON_COMMON))
13419 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
13420 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
13421 fold_checksum_tree (DECL_ARGUMENT_FLD (expr), ctx, ht);
13425 if (TREE_CODE (expr) == ENUMERAL_TYPE)
13426 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
13427 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
13428 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
13429 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
13430 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
13431 if (INTEGRAL_TYPE_P (expr)
13432 || SCALAR_FLOAT_TYPE_P (expr))
13434 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
13435 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
13437 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
13438 if (TREE_CODE (expr) == RECORD_TYPE
13439 || TREE_CODE (expr) == UNION_TYPE
13440 || TREE_CODE (expr) == QUAL_UNION_TYPE)
13441 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
13442 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
13449 /* Helper function for outputting the checksum of a tree T. When
13450 debugging with gdb, you can "define mynext" to be "next" followed
13451 by "call debug_fold_checksum (op0)", then just trace down till the
13455 debug_fold_checksum (const_tree t)
13458 unsigned char checksum[16];
13459 struct md5_ctx ctx;
13460 htab_t ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13462 md5_init_ctx (&ctx);
13463 fold_checksum_tree (t, &ctx, ht);
13464 md5_finish_ctx (&ctx, checksum);
13467 for (i = 0; i < 16; i++)
13468 fprintf (stderr, "%d ", checksum[i]);
13470 fprintf (stderr, "\n");
13475 /* Fold a unary tree expression with code CODE of type TYPE with an
13476 operand OP0. Return a folded expression if successful. Otherwise,
13477 return a tree expression with code CODE of type TYPE with an
13481 fold_build1_stat (enum tree_code code, tree type, tree op0 MEM_STAT_DECL)
13484 #ifdef ENABLE_FOLD_CHECKING
13485 unsigned char checksum_before[16], checksum_after[16];
13486 struct md5_ctx ctx;
13489 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13490 md5_init_ctx (&ctx);
13491 fold_checksum_tree (op0, &ctx, ht);
13492 md5_finish_ctx (&ctx, checksum_before);
13496 tem = fold_unary (code, type, op0);
13498 tem = build1_stat (code, type, op0 PASS_MEM_STAT);
13500 #ifdef ENABLE_FOLD_CHECKING
13501 md5_init_ctx (&ctx);
13502 fold_checksum_tree (op0, &ctx, ht);
13503 md5_finish_ctx (&ctx, checksum_after);
13506 if (memcmp (checksum_before, checksum_after, 16))
13507 fold_check_failed (op0, tem);
13512 /* Fold a binary tree expression with code CODE of type TYPE with
13513 operands OP0 and OP1. Return a folded expression if successful.
13514 Otherwise, return a tree expression with code CODE of type TYPE
13515 with operands OP0 and OP1. */
13518 fold_build2_stat (enum tree_code code, tree type, tree op0, tree op1
13522 #ifdef ENABLE_FOLD_CHECKING
13523 unsigned char checksum_before_op0[16],
13524 checksum_before_op1[16],
13525 checksum_after_op0[16],
13526 checksum_after_op1[16];
13527 struct md5_ctx ctx;
13530 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13531 md5_init_ctx (&ctx);
13532 fold_checksum_tree (op0, &ctx, ht);
13533 md5_finish_ctx (&ctx, checksum_before_op0);
13536 md5_init_ctx (&ctx);
13537 fold_checksum_tree (op1, &ctx, ht);
13538 md5_finish_ctx (&ctx, checksum_before_op1);
13542 tem = fold_binary (code, type, op0, op1);
13544 tem = build2_stat (code, type, op0, op1 PASS_MEM_STAT);
13546 #ifdef ENABLE_FOLD_CHECKING
13547 md5_init_ctx (&ctx);
13548 fold_checksum_tree (op0, &ctx, ht);
13549 md5_finish_ctx (&ctx, checksum_after_op0);
13552 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13553 fold_check_failed (op0, tem);
13555 md5_init_ctx (&ctx);
13556 fold_checksum_tree (op1, &ctx, ht);
13557 md5_finish_ctx (&ctx, checksum_after_op1);
13560 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13561 fold_check_failed (op1, tem);
13566 /* Fold a ternary tree expression with code CODE of type TYPE with
13567 operands OP0, OP1, and OP2. Return a folded expression if
13568 successful. Otherwise, return a tree expression with code CODE of
13569 type TYPE with operands OP0, OP1, and OP2. */
13572 fold_build3_stat (enum tree_code code, tree type, tree op0, tree op1, tree op2
13576 #ifdef ENABLE_FOLD_CHECKING
13577 unsigned char checksum_before_op0[16],
13578 checksum_before_op1[16],
13579 checksum_before_op2[16],
13580 checksum_after_op0[16],
13581 checksum_after_op1[16],
13582 checksum_after_op2[16];
13583 struct md5_ctx ctx;
13586 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13587 md5_init_ctx (&ctx);
13588 fold_checksum_tree (op0, &ctx, ht);
13589 md5_finish_ctx (&ctx, checksum_before_op0);
13592 md5_init_ctx (&ctx);
13593 fold_checksum_tree (op1, &ctx, ht);
13594 md5_finish_ctx (&ctx, checksum_before_op1);
13597 md5_init_ctx (&ctx);
13598 fold_checksum_tree (op2, &ctx, ht);
13599 md5_finish_ctx (&ctx, checksum_before_op2);
13603 gcc_assert (TREE_CODE_CLASS (code) != tcc_vl_exp);
13604 tem = fold_ternary (code, type, op0, op1, op2);
13606 tem = build3_stat (code, type, op0, op1, op2 PASS_MEM_STAT);
13608 #ifdef ENABLE_FOLD_CHECKING
13609 md5_init_ctx (&ctx);
13610 fold_checksum_tree (op0, &ctx, ht);
13611 md5_finish_ctx (&ctx, checksum_after_op0);
13614 if (memcmp (checksum_before_op0, checksum_after_op0, 16))
13615 fold_check_failed (op0, tem);
13617 md5_init_ctx (&ctx);
13618 fold_checksum_tree (op1, &ctx, ht);
13619 md5_finish_ctx (&ctx, checksum_after_op1);
13622 if (memcmp (checksum_before_op1, checksum_after_op1, 16))
13623 fold_check_failed (op1, tem);
13625 md5_init_ctx (&ctx);
13626 fold_checksum_tree (op2, &ctx, ht);
13627 md5_finish_ctx (&ctx, checksum_after_op2);
13630 if (memcmp (checksum_before_op2, checksum_after_op2, 16))
13631 fold_check_failed (op2, tem);
13636 /* Fold a CALL_EXPR expression of type TYPE with operands FN and NARGS
13637 arguments in ARGARRAY, and a null static chain.
13638 Return a folded expression if successful. Otherwise, return a CALL_EXPR
13639 of type TYPE from the given operands as constructed by build_call_array. */
13642 fold_build_call_array (tree type, tree fn, int nargs, tree *argarray)
13645 #ifdef ENABLE_FOLD_CHECKING
13646 unsigned char checksum_before_fn[16],
13647 checksum_before_arglist[16],
13648 checksum_after_fn[16],
13649 checksum_after_arglist[16];
13650 struct md5_ctx ctx;
13654 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
13655 md5_init_ctx (&ctx);
13656 fold_checksum_tree (fn, &ctx, ht);
13657 md5_finish_ctx (&ctx, checksum_before_fn);
13660 md5_init_ctx (&ctx);
13661 for (i = 0; i < nargs; i++)
13662 fold_checksum_tree (argarray[i], &ctx, ht);
13663 md5_finish_ctx (&ctx, checksum_before_arglist);
13667 tem = fold_builtin_call_array (type, fn, nargs, argarray);
13669 #ifdef ENABLE_FOLD_CHECKING
13670 md5_init_ctx (&ctx);
13671 fold_checksum_tree (fn, &ctx, ht);
13672 md5_finish_ctx (&ctx, checksum_after_fn);
13675 if (memcmp (checksum_before_fn, checksum_after_fn, 16))
13676 fold_check_failed (fn, tem);
13678 md5_init_ctx (&ctx);
13679 for (i = 0; i < nargs; i++)
13680 fold_checksum_tree (argarray[i], &ctx, ht);
13681 md5_finish_ctx (&ctx, checksum_after_arglist);
13684 if (memcmp (checksum_before_arglist, checksum_after_arglist, 16))
13685 fold_check_failed (NULL_TREE, tem);
13690 /* Perform constant folding and related simplification of initializer
13691 expression EXPR. These behave identically to "fold_buildN" but ignore
13692 potential run-time traps and exceptions that fold must preserve. */
13694 #define START_FOLD_INIT \
13695 int saved_signaling_nans = flag_signaling_nans;\
13696 int saved_trapping_math = flag_trapping_math;\
13697 int saved_rounding_math = flag_rounding_math;\
13698 int saved_trapv = flag_trapv;\
13699 int saved_folding_initializer = folding_initializer;\
13700 flag_signaling_nans = 0;\
13701 flag_trapping_math = 0;\
13702 flag_rounding_math = 0;\
13704 folding_initializer = 1;
13706 #define END_FOLD_INIT \
13707 flag_signaling_nans = saved_signaling_nans;\
13708 flag_trapping_math = saved_trapping_math;\
13709 flag_rounding_math = saved_rounding_math;\
13710 flag_trapv = saved_trapv;\
13711 folding_initializer = saved_folding_initializer;
13714 fold_build1_initializer (enum tree_code code, tree type, tree op)
13719 result = fold_build1 (code, type, op);
13726 fold_build2_initializer (enum tree_code code, tree type, tree op0, tree op1)
13731 result = fold_build2 (code, type, op0, op1);
13738 fold_build3_initializer (enum tree_code code, tree type, tree op0, tree op1,
13744 result = fold_build3 (code, type, op0, op1, op2);
13751 fold_build_call_array_initializer (tree type, tree fn,
13752 int nargs, tree *argarray)
13757 result = fold_build_call_array (type, fn, nargs, argarray);
13763 #undef START_FOLD_INIT
13764 #undef END_FOLD_INIT
13766 /* Determine if first argument is a multiple of second argument. Return 0 if
13767 it is not, or we cannot easily determined it to be.
13769 An example of the sort of thing we care about (at this point; this routine
13770 could surely be made more general, and expanded to do what the *_DIV_EXPR's
13771 fold cases do now) is discovering that
13773 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
13779 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
13781 This code also handles discovering that
13783 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
13785 is a multiple of 8 so we don't have to worry about dealing with a
13786 possible remainder.
13788 Note that we *look* inside a SAVE_EXPR only to determine how it was
13789 calculated; it is not safe for fold to do much of anything else with the
13790 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
13791 at run time. For example, the latter example above *cannot* be implemented
13792 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
13793 evaluation time of the original SAVE_EXPR is not necessarily the same at
13794 the time the new expression is evaluated. The only optimization of this
13795 sort that would be valid is changing
13797 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
13801 SAVE_EXPR (I) * SAVE_EXPR (J)
13803 (where the same SAVE_EXPR (J) is used in the original and the
13804 transformed version). */
13807 multiple_of_p (tree type, const_tree top, const_tree bottom)
13809 if (operand_equal_p (top, bottom, 0))
13812 if (TREE_CODE (type) != INTEGER_TYPE)
13815 switch (TREE_CODE (top))
13818 /* Bitwise and provides a power of two multiple. If the mask is
13819 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
13820 if (!integer_pow2p (bottom))
13825 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
13826 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
13830 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
13831 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
13834 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
13838 op1 = TREE_OPERAND (top, 1);
13839 /* const_binop may not detect overflow correctly,
13840 so check for it explicitly here. */
13841 if (TYPE_PRECISION (TREE_TYPE (size_one_node))
13842 > TREE_INT_CST_LOW (op1)
13843 && TREE_INT_CST_HIGH (op1) == 0
13844 && 0 != (t1 = fold_convert (type,
13845 const_binop (LSHIFT_EXPR,
13848 && !TREE_OVERFLOW (t1))
13849 return multiple_of_p (type, t1, bottom);
13854 /* Can't handle conversions from non-integral or wider integral type. */
13855 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
13856 || (TYPE_PRECISION (type)
13857 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
13860 /* .. fall through ... */
13863 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
13866 if (TREE_CODE (bottom) != INTEGER_CST
13867 || integer_zerop (bottom)
13868 || (TYPE_UNSIGNED (type)
13869 && (tree_int_cst_sgn (top) < 0
13870 || tree_int_cst_sgn (bottom) < 0)))
13872 return integer_zerop (int_const_binop (TRUNC_MOD_EXPR,
13880 /* Return true if CODE or TYPE is known to be non-negative. */
13883 tree_simple_nonnegative_warnv_p (enum tree_code code, tree type)
13885 if ((TYPE_PRECISION (type) != 1 || TYPE_UNSIGNED (type))
13886 && truth_value_p (code))
13887 /* Truth values evaluate to 0 or 1, which is nonnegative unless we
13888 have a signed:1 type (where the value is -1 and 0). */
13893 /* Return true if (CODE OP0) is known to be non-negative. If the return
13894 value is based on the assumption that signed overflow is undefined,
13895 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13896 *STRICT_OVERFLOW_P. */
13899 tree_unary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
13900 bool *strict_overflow_p)
13902 if (TYPE_UNSIGNED (type))
13908 /* We can't return 1 if flag_wrapv is set because
13909 ABS_EXPR<INT_MIN> = INT_MIN. */
13910 if (!INTEGRAL_TYPE_P (type))
13912 if (TYPE_OVERFLOW_UNDEFINED (type))
13914 *strict_overflow_p = true;
13919 case NON_LVALUE_EXPR:
13921 case FIX_TRUNC_EXPR:
13922 return tree_expr_nonnegative_warnv_p (op0,
13923 strict_overflow_p);
13927 tree inner_type = TREE_TYPE (op0);
13928 tree outer_type = type;
13930 if (TREE_CODE (outer_type) == REAL_TYPE)
13932 if (TREE_CODE (inner_type) == REAL_TYPE)
13933 return tree_expr_nonnegative_warnv_p (op0,
13934 strict_overflow_p);
13935 if (TREE_CODE (inner_type) == INTEGER_TYPE)
13937 if (TYPE_UNSIGNED (inner_type))
13939 return tree_expr_nonnegative_warnv_p (op0,
13940 strict_overflow_p);
13943 else if (TREE_CODE (outer_type) == INTEGER_TYPE)
13945 if (TREE_CODE (inner_type) == REAL_TYPE)
13946 return tree_expr_nonnegative_warnv_p (op0,
13947 strict_overflow_p);
13948 if (TREE_CODE (inner_type) == INTEGER_TYPE)
13949 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
13950 && TYPE_UNSIGNED (inner_type);
13956 return tree_simple_nonnegative_warnv_p (code, type);
13959 /* We don't know sign of `t', so be conservative and return false. */
13963 /* Return true if (CODE OP0 OP1) is known to be non-negative. If the return
13964 value is based on the assumption that signed overflow is undefined,
13965 set *STRICT_OVERFLOW_P to true; otherwise, don't change
13966 *STRICT_OVERFLOW_P. */
13969 tree_binary_nonnegative_warnv_p (enum tree_code code, tree type, tree op0,
13970 tree op1, bool *strict_overflow_p)
13972 if (TYPE_UNSIGNED (type))
13977 case POINTER_PLUS_EXPR:
13979 if (FLOAT_TYPE_P (type))
13980 return (tree_expr_nonnegative_warnv_p (op0,
13982 && tree_expr_nonnegative_warnv_p (op1,
13983 strict_overflow_p));
13985 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
13986 both unsigned and at least 2 bits shorter than the result. */
13987 if (TREE_CODE (type) == INTEGER_TYPE
13988 && TREE_CODE (op0) == NOP_EXPR
13989 && TREE_CODE (op1) == NOP_EXPR)
13991 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
13992 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
13993 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
13994 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
13996 unsigned int prec = MAX (TYPE_PRECISION (inner1),
13997 TYPE_PRECISION (inner2)) + 1;
13998 return prec < TYPE_PRECISION (type);
14004 if (FLOAT_TYPE_P (type))
14006 /* x * x for floating point x is always non-negative. */
14007 if (operand_equal_p (op0, op1, 0))
14009 return (tree_expr_nonnegative_warnv_p (op0,
14011 && tree_expr_nonnegative_warnv_p (op1,
14012 strict_overflow_p));
14015 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
14016 both unsigned and their total bits is shorter than the result. */
14017 if (TREE_CODE (type) == INTEGER_TYPE
14018 && TREE_CODE (op0) == NOP_EXPR
14019 && TREE_CODE (op1) == NOP_EXPR)
14021 tree inner1 = TREE_TYPE (TREE_OPERAND (op0, 0));
14022 tree inner2 = TREE_TYPE (TREE_OPERAND (op1, 0));
14023 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
14024 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
14025 return TYPE_PRECISION (inner1) + TYPE_PRECISION (inner2)
14026 < TYPE_PRECISION (type);
14032 return (tree_expr_nonnegative_warnv_p (op0,
14034 || tree_expr_nonnegative_warnv_p (op1,
14035 strict_overflow_p));
14041 case TRUNC_DIV_EXPR:
14042 case CEIL_DIV_EXPR:
14043 case FLOOR_DIV_EXPR:
14044 case ROUND_DIV_EXPR:
14045 return (tree_expr_nonnegative_warnv_p (op0,
14047 && tree_expr_nonnegative_warnv_p (op1,
14048 strict_overflow_p));
14050 case TRUNC_MOD_EXPR:
14051 case CEIL_MOD_EXPR:
14052 case FLOOR_MOD_EXPR:
14053 case ROUND_MOD_EXPR:
14054 return tree_expr_nonnegative_warnv_p (op0,
14055 strict_overflow_p);
14057 return tree_simple_nonnegative_warnv_p (code, type);
14060 /* We don't know sign of `t', so be conservative and return false. */
14064 /* Return true if T is known to be non-negative. If the return
14065 value is based on the assumption that signed overflow is undefined,
14066 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14067 *STRICT_OVERFLOW_P. */
14070 tree_single_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14072 if (TYPE_UNSIGNED (TREE_TYPE (t)))
14075 switch (TREE_CODE (t))
14078 return tree_int_cst_sgn (t) >= 0;
14081 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
14084 return ! FIXED_VALUE_NEGATIVE (TREE_FIXED_CST (t));
14087 return (tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14089 && tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 2),
14090 strict_overflow_p));
14092 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14095 /* We don't know sign of `t', so be conservative and return false. */
14099 /* Return true if T is known to be non-negative. If the return
14100 value is based on the assumption that signed overflow is undefined,
14101 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14102 *STRICT_OVERFLOW_P. */
14105 tree_call_nonnegative_warnv_p (tree type, tree fndecl,
14106 tree arg0, tree arg1, bool *strict_overflow_p)
14108 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
14109 switch (DECL_FUNCTION_CODE (fndecl))
14111 CASE_FLT_FN (BUILT_IN_ACOS):
14112 CASE_FLT_FN (BUILT_IN_ACOSH):
14113 CASE_FLT_FN (BUILT_IN_CABS):
14114 CASE_FLT_FN (BUILT_IN_COSH):
14115 CASE_FLT_FN (BUILT_IN_ERFC):
14116 CASE_FLT_FN (BUILT_IN_EXP):
14117 CASE_FLT_FN (BUILT_IN_EXP10):
14118 CASE_FLT_FN (BUILT_IN_EXP2):
14119 CASE_FLT_FN (BUILT_IN_FABS):
14120 CASE_FLT_FN (BUILT_IN_FDIM):
14121 CASE_FLT_FN (BUILT_IN_HYPOT):
14122 CASE_FLT_FN (BUILT_IN_POW10):
14123 CASE_INT_FN (BUILT_IN_FFS):
14124 CASE_INT_FN (BUILT_IN_PARITY):
14125 CASE_INT_FN (BUILT_IN_POPCOUNT):
14126 case BUILT_IN_BSWAP32:
14127 case BUILT_IN_BSWAP64:
14131 CASE_FLT_FN (BUILT_IN_SQRT):
14132 /* sqrt(-0.0) is -0.0. */
14133 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
14135 return tree_expr_nonnegative_warnv_p (arg0,
14136 strict_overflow_p);
14138 CASE_FLT_FN (BUILT_IN_ASINH):
14139 CASE_FLT_FN (BUILT_IN_ATAN):
14140 CASE_FLT_FN (BUILT_IN_ATANH):
14141 CASE_FLT_FN (BUILT_IN_CBRT):
14142 CASE_FLT_FN (BUILT_IN_CEIL):
14143 CASE_FLT_FN (BUILT_IN_ERF):
14144 CASE_FLT_FN (BUILT_IN_EXPM1):
14145 CASE_FLT_FN (BUILT_IN_FLOOR):
14146 CASE_FLT_FN (BUILT_IN_FMOD):
14147 CASE_FLT_FN (BUILT_IN_FREXP):
14148 CASE_FLT_FN (BUILT_IN_LCEIL):
14149 CASE_FLT_FN (BUILT_IN_LDEXP):
14150 CASE_FLT_FN (BUILT_IN_LFLOOR):
14151 CASE_FLT_FN (BUILT_IN_LLCEIL):
14152 CASE_FLT_FN (BUILT_IN_LLFLOOR):
14153 CASE_FLT_FN (BUILT_IN_LLRINT):
14154 CASE_FLT_FN (BUILT_IN_LLROUND):
14155 CASE_FLT_FN (BUILT_IN_LRINT):
14156 CASE_FLT_FN (BUILT_IN_LROUND):
14157 CASE_FLT_FN (BUILT_IN_MODF):
14158 CASE_FLT_FN (BUILT_IN_NEARBYINT):
14159 CASE_FLT_FN (BUILT_IN_RINT):
14160 CASE_FLT_FN (BUILT_IN_ROUND):
14161 CASE_FLT_FN (BUILT_IN_SCALB):
14162 CASE_FLT_FN (BUILT_IN_SCALBLN):
14163 CASE_FLT_FN (BUILT_IN_SCALBN):
14164 CASE_FLT_FN (BUILT_IN_SIGNBIT):
14165 CASE_FLT_FN (BUILT_IN_SIGNIFICAND):
14166 CASE_FLT_FN (BUILT_IN_SINH):
14167 CASE_FLT_FN (BUILT_IN_TANH):
14168 CASE_FLT_FN (BUILT_IN_TRUNC):
14169 /* True if the 1st argument is nonnegative. */
14170 return tree_expr_nonnegative_warnv_p (arg0,
14171 strict_overflow_p);
14173 CASE_FLT_FN (BUILT_IN_FMAX):
14174 /* True if the 1st OR 2nd arguments are nonnegative. */
14175 return (tree_expr_nonnegative_warnv_p (arg0,
14177 || (tree_expr_nonnegative_warnv_p (arg1,
14178 strict_overflow_p)));
14180 CASE_FLT_FN (BUILT_IN_FMIN):
14181 /* True if the 1st AND 2nd arguments are nonnegative. */
14182 return (tree_expr_nonnegative_warnv_p (arg0,
14184 && (tree_expr_nonnegative_warnv_p (arg1,
14185 strict_overflow_p)));
14187 CASE_FLT_FN (BUILT_IN_COPYSIGN):
14188 /* True if the 2nd argument is nonnegative. */
14189 return tree_expr_nonnegative_warnv_p (arg1,
14190 strict_overflow_p);
14192 CASE_FLT_FN (BUILT_IN_POWI):
14193 /* True if the 1st argument is nonnegative or the second
14194 argument is an even integer. */
14195 if (TREE_CODE (arg1) == INTEGER_CST
14196 && (TREE_INT_CST_LOW (arg1) & 1) == 0)
14198 return tree_expr_nonnegative_warnv_p (arg0,
14199 strict_overflow_p);
14201 CASE_FLT_FN (BUILT_IN_POW):
14202 /* True if the 1st argument is nonnegative or the second
14203 argument is an even integer valued real. */
14204 if (TREE_CODE (arg1) == REAL_CST)
14209 c = TREE_REAL_CST (arg1);
14210 n = real_to_integer (&c);
14213 REAL_VALUE_TYPE cint;
14214 real_from_integer (&cint, VOIDmode, n,
14215 n < 0 ? -1 : 0, 0);
14216 if (real_identical (&c, &cint))
14220 return tree_expr_nonnegative_warnv_p (arg0,
14221 strict_overflow_p);
14226 return tree_simple_nonnegative_warnv_p (CALL_EXPR,
14230 /* Return true if T is known to be non-negative. If the return
14231 value is based on the assumption that signed overflow is undefined,
14232 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14233 *STRICT_OVERFLOW_P. */
14236 tree_invalid_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14238 enum tree_code code = TREE_CODE (t);
14239 if (TYPE_UNSIGNED (TREE_TYPE (t)))
14246 tree temp = TARGET_EXPR_SLOT (t);
14247 t = TARGET_EXPR_INITIAL (t);
14249 /* If the initializer is non-void, then it's a normal expression
14250 that will be assigned to the slot. */
14251 if (!VOID_TYPE_P (t))
14252 return tree_expr_nonnegative_warnv_p (t, strict_overflow_p);
14254 /* Otherwise, the initializer sets the slot in some way. One common
14255 way is an assignment statement at the end of the initializer. */
14258 if (TREE_CODE (t) == BIND_EXPR)
14259 t = expr_last (BIND_EXPR_BODY (t));
14260 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
14261 || TREE_CODE (t) == TRY_CATCH_EXPR)
14262 t = expr_last (TREE_OPERAND (t, 0));
14263 else if (TREE_CODE (t) == STATEMENT_LIST)
14268 if (TREE_CODE (t) == MODIFY_EXPR
14269 && TREE_OPERAND (t, 0) == temp)
14270 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14271 strict_overflow_p);
14278 tree arg0 = call_expr_nargs (t) > 0 ? CALL_EXPR_ARG (t, 0) : NULL_TREE;
14279 tree arg1 = call_expr_nargs (t) > 1 ? CALL_EXPR_ARG (t, 1) : NULL_TREE;
14281 return tree_call_nonnegative_warnv_p (TREE_TYPE (t),
14282 get_callee_fndecl (t),
14285 strict_overflow_p);
14287 case COMPOUND_EXPR:
14289 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 1),
14290 strict_overflow_p);
14292 return tree_expr_nonnegative_warnv_p (expr_last (TREE_OPERAND (t, 1)),
14293 strict_overflow_p);
14295 return tree_expr_nonnegative_warnv_p (TREE_OPERAND (t, 0),
14296 strict_overflow_p);
14299 return tree_simple_nonnegative_warnv_p (TREE_CODE (t),
14303 /* We don't know sign of `t', so be conservative and return false. */
14307 /* Return true if T is known to be non-negative. If the return
14308 value is based on the assumption that signed overflow is undefined,
14309 set *STRICT_OVERFLOW_P to true; otherwise, don't change
14310 *STRICT_OVERFLOW_P. */
14313 tree_expr_nonnegative_warnv_p (tree t, bool *strict_overflow_p)
14315 enum tree_code code;
14316 if (t == error_mark_node)
14319 code = TREE_CODE (t);
14320 switch (TREE_CODE_CLASS (code))
14323 case tcc_comparison:
14324 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14326 TREE_OPERAND (t, 0),
14327 TREE_OPERAND (t, 1),
14328 strict_overflow_p);
14331 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14333 TREE_OPERAND (t, 0),
14334 strict_overflow_p);
14337 case tcc_declaration:
14338 case tcc_reference:
14339 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14347 case TRUTH_AND_EXPR:
14348 case TRUTH_OR_EXPR:
14349 case TRUTH_XOR_EXPR:
14350 return tree_binary_nonnegative_warnv_p (TREE_CODE (t),
14352 TREE_OPERAND (t, 0),
14353 TREE_OPERAND (t, 1),
14354 strict_overflow_p);
14355 case TRUTH_NOT_EXPR:
14356 return tree_unary_nonnegative_warnv_p (TREE_CODE (t),
14358 TREE_OPERAND (t, 0),
14359 strict_overflow_p);
14366 case WITH_SIZE_EXPR:
14370 return tree_single_nonnegative_warnv_p (t, strict_overflow_p);
14373 return tree_invalid_nonnegative_warnv_p (t, strict_overflow_p);
14377 /* Return true if `t' is known to be non-negative. Handle warnings
14378 about undefined signed overflow. */
14381 tree_expr_nonnegative_p (tree t)
14383 bool ret, strict_overflow_p;
14385 strict_overflow_p = false;
14386 ret = tree_expr_nonnegative_warnv_p (t, &strict_overflow_p);
14387 if (strict_overflow_p)
14388 fold_overflow_warning (("assuming signed overflow does not occur when "
14389 "determining that expression is always "
14391 WARN_STRICT_OVERFLOW_MISC);
14396 /* Return true when (CODE OP0) is an address and is known to be nonzero.
14397 For floating point we further ensure that T is not denormal.
14398 Similar logic is present in nonzero_address in rtlanal.h.
14400 If the return value is based on the assumption that signed overflow
14401 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14402 change *STRICT_OVERFLOW_P. */
14405 tree_unary_nonzero_warnv_p (enum tree_code code, tree type, tree op0,
14406 bool *strict_overflow_p)
14411 return tree_expr_nonzero_warnv_p (op0,
14412 strict_overflow_p);
14416 tree inner_type = TREE_TYPE (op0);
14417 tree outer_type = type;
14419 return (TYPE_PRECISION (outer_type) >= TYPE_PRECISION (inner_type)
14420 && tree_expr_nonzero_warnv_p (op0,
14421 strict_overflow_p));
14425 case NON_LVALUE_EXPR:
14426 return tree_expr_nonzero_warnv_p (op0,
14427 strict_overflow_p);
14436 /* Return true when (CODE OP0 OP1) is an address and is known to be nonzero.
14437 For floating point we further ensure that T is not denormal.
14438 Similar logic is present in nonzero_address in rtlanal.h.
14440 If the return value is based on the assumption that signed overflow
14441 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14442 change *STRICT_OVERFLOW_P. */
14445 tree_binary_nonzero_warnv_p (enum tree_code code,
14448 tree op1, bool *strict_overflow_p)
14450 bool sub_strict_overflow_p;
14453 case POINTER_PLUS_EXPR:
14455 if (TYPE_OVERFLOW_UNDEFINED (type))
14457 /* With the presence of negative values it is hard
14458 to say something. */
14459 sub_strict_overflow_p = false;
14460 if (!tree_expr_nonnegative_warnv_p (op0,
14461 &sub_strict_overflow_p)
14462 || !tree_expr_nonnegative_warnv_p (op1,
14463 &sub_strict_overflow_p))
14465 /* One of operands must be positive and the other non-negative. */
14466 /* We don't set *STRICT_OVERFLOW_P here: even if this value
14467 overflows, on a twos-complement machine the sum of two
14468 nonnegative numbers can never be zero. */
14469 return (tree_expr_nonzero_warnv_p (op0,
14471 || tree_expr_nonzero_warnv_p (op1,
14472 strict_overflow_p));
14477 if (TYPE_OVERFLOW_UNDEFINED (type))
14479 if (tree_expr_nonzero_warnv_p (op0,
14481 && tree_expr_nonzero_warnv_p (op1,
14482 strict_overflow_p))
14484 *strict_overflow_p = true;
14491 sub_strict_overflow_p = false;
14492 if (tree_expr_nonzero_warnv_p (op0,
14493 &sub_strict_overflow_p)
14494 && tree_expr_nonzero_warnv_p (op1,
14495 &sub_strict_overflow_p))
14497 if (sub_strict_overflow_p)
14498 *strict_overflow_p = true;
14503 sub_strict_overflow_p = false;
14504 if (tree_expr_nonzero_warnv_p (op0,
14505 &sub_strict_overflow_p))
14507 if (sub_strict_overflow_p)
14508 *strict_overflow_p = true;
14510 /* When both operands are nonzero, then MAX must be too. */
14511 if (tree_expr_nonzero_warnv_p (op1,
14512 strict_overflow_p))
14515 /* MAX where operand 0 is positive is positive. */
14516 return tree_expr_nonnegative_warnv_p (op0,
14517 strict_overflow_p);
14519 /* MAX where operand 1 is positive is positive. */
14520 else if (tree_expr_nonzero_warnv_p (op1,
14521 &sub_strict_overflow_p)
14522 && tree_expr_nonnegative_warnv_p (op1,
14523 &sub_strict_overflow_p))
14525 if (sub_strict_overflow_p)
14526 *strict_overflow_p = true;
14532 return (tree_expr_nonzero_warnv_p (op1,
14534 || tree_expr_nonzero_warnv_p (op0,
14535 strict_overflow_p));
14544 /* Return true when T is an address and is known to be nonzero.
14545 For floating point we further ensure that T is not denormal.
14546 Similar logic is present in nonzero_address in rtlanal.h.
14548 If the return value is based on the assumption that signed overflow
14549 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14550 change *STRICT_OVERFLOW_P. */
14553 tree_single_nonzero_warnv_p (tree t, bool *strict_overflow_p)
14555 bool sub_strict_overflow_p;
14556 switch (TREE_CODE (t))
14559 return !integer_zerop (t);
14563 tree base = get_base_address (TREE_OPERAND (t, 0));
14568 /* Weak declarations may link to NULL. */
14569 if (VAR_OR_FUNCTION_DECL_P (base))
14570 return !DECL_WEAK (base);
14572 /* Constants are never weak. */
14573 if (CONSTANT_CLASS_P (base))
14580 sub_strict_overflow_p = false;
14581 if (tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
14582 &sub_strict_overflow_p)
14583 && tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 2),
14584 &sub_strict_overflow_p))
14586 if (sub_strict_overflow_p)
14587 *strict_overflow_p = true;
14598 /* Return true when T is an address and is known to be nonzero.
14599 For floating point we further ensure that T is not denormal.
14600 Similar logic is present in nonzero_address in rtlanal.h.
14602 If the return value is based on the assumption that signed overflow
14603 is undefined, set *STRICT_OVERFLOW_P to true; otherwise, don't
14604 change *STRICT_OVERFLOW_P. */
14607 tree_expr_nonzero_warnv_p (tree t, bool *strict_overflow_p)
14609 tree type = TREE_TYPE (t);
14610 enum tree_code code;
14612 /* Doing something useful for floating point would need more work. */
14613 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
14616 code = TREE_CODE (t);
14617 switch (TREE_CODE_CLASS (code))
14620 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
14621 strict_overflow_p);
14623 case tcc_comparison:
14624 return tree_binary_nonzero_warnv_p (code, type,
14625 TREE_OPERAND (t, 0),
14626 TREE_OPERAND (t, 1),
14627 strict_overflow_p);
14629 case tcc_declaration:
14630 case tcc_reference:
14631 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
14639 case TRUTH_NOT_EXPR:
14640 return tree_unary_nonzero_warnv_p (code, type, TREE_OPERAND (t, 0),
14641 strict_overflow_p);
14643 case TRUTH_AND_EXPR:
14644 case TRUTH_OR_EXPR:
14645 case TRUTH_XOR_EXPR:
14646 return tree_binary_nonzero_warnv_p (code, type,
14647 TREE_OPERAND (t, 0),
14648 TREE_OPERAND (t, 1),
14649 strict_overflow_p);
14656 case WITH_SIZE_EXPR:
14660 return tree_single_nonzero_warnv_p (t, strict_overflow_p);
14662 case COMPOUND_EXPR:
14665 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 1),
14666 strict_overflow_p);
14669 return tree_expr_nonzero_warnv_p (TREE_OPERAND (t, 0),
14670 strict_overflow_p);
14673 return alloca_call_p (t);
14681 /* Return true when T is an address and is known to be nonzero.
14682 Handle warnings about undefined signed overflow. */
14685 tree_expr_nonzero_p (tree t)
14687 bool ret, strict_overflow_p;
14689 strict_overflow_p = false;
14690 ret = tree_expr_nonzero_warnv_p (t, &strict_overflow_p);
14691 if (strict_overflow_p)
14692 fold_overflow_warning (("assuming signed overflow does not occur when "
14693 "determining that expression is always "
14695 WARN_STRICT_OVERFLOW_MISC);
14699 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
14700 attempt to fold the expression to a constant without modifying TYPE,
14703 If the expression could be simplified to a constant, then return
14704 the constant. If the expression would not be simplified to a
14705 constant, then return NULL_TREE. */
14708 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
14710 tree tem = fold_binary (code, type, op0, op1);
14711 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
14714 /* Given the components of a unary expression CODE, TYPE and OP0,
14715 attempt to fold the expression to a constant without modifying
14718 If the expression could be simplified to a constant, then return
14719 the constant. If the expression would not be simplified to a
14720 constant, then return NULL_TREE. */
14723 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
14725 tree tem = fold_unary (code, type, op0);
14726 return (tem && TREE_CONSTANT (tem)) ? tem : NULL_TREE;
14729 /* If EXP represents referencing an element in a constant string
14730 (either via pointer arithmetic or array indexing), return the
14731 tree representing the value accessed, otherwise return NULL. */
14734 fold_read_from_constant_string (tree exp)
14736 if ((TREE_CODE (exp) == INDIRECT_REF
14737 || TREE_CODE (exp) == ARRAY_REF)
14738 && TREE_CODE (TREE_TYPE (exp)) == INTEGER_TYPE)
14740 tree exp1 = TREE_OPERAND (exp, 0);
14744 if (TREE_CODE (exp) == INDIRECT_REF)
14745 string = string_constant (exp1, &index);
14748 tree low_bound = array_ref_low_bound (exp);
14749 index = fold_convert (sizetype, TREE_OPERAND (exp, 1));
14751 /* Optimize the special-case of a zero lower bound.
14753 We convert the low_bound to sizetype to avoid some problems
14754 with constant folding. (E.g. suppose the lower bound is 1,
14755 and its mode is QI. Without the conversion,l (ARRAY
14756 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
14757 +INDEX), which becomes (ARRAY+255+INDEX). Oops!) */
14758 if (! integer_zerop (low_bound))
14759 index = size_diffop (index, fold_convert (sizetype, low_bound));
14765 && TYPE_MODE (TREE_TYPE (exp)) == TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))
14766 && TREE_CODE (string) == STRING_CST
14767 && TREE_CODE (index) == INTEGER_CST
14768 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
14769 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
14771 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
14772 return build_int_cst_type (TREE_TYPE (exp),
14773 (TREE_STRING_POINTER (string)
14774 [TREE_INT_CST_LOW (index)]));
14779 /* Return the tree for neg (ARG0) when ARG0 is known to be either
14780 an integer constant, real, or fixed-point constant.
14782 TYPE is the type of the result. */
14785 fold_negate_const (tree arg0, tree type)
14787 tree t = NULL_TREE;
14789 switch (TREE_CODE (arg0))
14793 unsigned HOST_WIDE_INT low;
14794 HOST_WIDE_INT high;
14795 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
14796 TREE_INT_CST_HIGH (arg0),
14798 t = force_fit_type_double (type, low, high, 1,
14799 (overflow | TREE_OVERFLOW (arg0))
14800 && !TYPE_UNSIGNED (type));
14805 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
14810 FIXED_VALUE_TYPE f;
14811 bool overflow_p = fixed_arithmetic (&f, NEGATE_EXPR,
14812 &(TREE_FIXED_CST (arg0)), NULL,
14813 TYPE_SATURATING (type));
14814 t = build_fixed (type, f);
14815 /* Propagate overflow flags. */
14816 if (overflow_p | TREE_OVERFLOW (arg0))
14818 TREE_OVERFLOW (t) = 1;
14819 TREE_CONSTANT_OVERFLOW (t) = 1;
14821 else if (TREE_CONSTANT_OVERFLOW (arg0))
14822 TREE_CONSTANT_OVERFLOW (t) = 1;
14827 gcc_unreachable ();
14833 /* Return the tree for abs (ARG0) when ARG0 is known to be either
14834 an integer constant or real constant.
14836 TYPE is the type of the result. */
14839 fold_abs_const (tree arg0, tree type)
14841 tree t = NULL_TREE;
14843 switch (TREE_CODE (arg0))
14846 /* If the value is unsigned, then the absolute value is
14847 the same as the ordinary value. */
14848 if (TYPE_UNSIGNED (type))
14850 /* Similarly, if the value is non-negative. */
14851 else if (INT_CST_LT (integer_minus_one_node, arg0))
14853 /* If the value is negative, then the absolute value is
14857 unsigned HOST_WIDE_INT low;
14858 HOST_WIDE_INT high;
14859 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
14860 TREE_INT_CST_HIGH (arg0),
14862 t = force_fit_type_double (type, low, high, -1,
14863 overflow | TREE_OVERFLOW (arg0));
14868 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
14869 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
14875 gcc_unreachable ();
14881 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
14882 constant. TYPE is the type of the result. */
14885 fold_not_const (tree arg0, tree type)
14887 tree t = NULL_TREE;
14889 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
14891 t = force_fit_type_double (type, ~TREE_INT_CST_LOW (arg0),
14892 ~TREE_INT_CST_HIGH (arg0), 0,
14893 TREE_OVERFLOW (arg0));
14898 /* Given CODE, a relational operator, the target type, TYPE and two
14899 constant operands OP0 and OP1, return the result of the
14900 relational operation. If the result is not a compile time
14901 constant, then return NULL_TREE. */
14904 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
14906 int result, invert;
14908 /* From here on, the only cases we handle are when the result is
14909 known to be a constant. */
14911 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
14913 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
14914 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
14916 /* Handle the cases where either operand is a NaN. */
14917 if (real_isnan (c0) || real_isnan (c1))
14927 case UNORDERED_EXPR:
14941 if (flag_trapping_math)
14947 gcc_unreachable ();
14950 return constant_boolean_node (result, type);
14953 return constant_boolean_node (real_compare (code, c0, c1), type);
14956 if (TREE_CODE (op0) == FIXED_CST && TREE_CODE (op1) == FIXED_CST)
14958 const FIXED_VALUE_TYPE *c0 = TREE_FIXED_CST_PTR (op0);
14959 const FIXED_VALUE_TYPE *c1 = TREE_FIXED_CST_PTR (op1);
14960 return constant_boolean_node (fixed_compare (code, c0, c1), type);
14963 /* Handle equality/inequality of complex constants. */
14964 if (TREE_CODE (op0) == COMPLEX_CST && TREE_CODE (op1) == COMPLEX_CST)
14966 tree rcond = fold_relational_const (code, type,
14967 TREE_REALPART (op0),
14968 TREE_REALPART (op1));
14969 tree icond = fold_relational_const (code, type,
14970 TREE_IMAGPART (op0),
14971 TREE_IMAGPART (op1));
14972 if (code == EQ_EXPR)
14973 return fold_build2 (TRUTH_ANDIF_EXPR, type, rcond, icond);
14974 else if (code == NE_EXPR)
14975 return fold_build2 (TRUTH_ORIF_EXPR, type, rcond, icond);
14980 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
14982 To compute GT, swap the arguments and do LT.
14983 To compute GE, do LT and invert the result.
14984 To compute LE, swap the arguments, do LT and invert the result.
14985 To compute NE, do EQ and invert the result.
14987 Therefore, the code below must handle only EQ and LT. */
14989 if (code == LE_EXPR || code == GT_EXPR)
14994 code = swap_tree_comparison (code);
14997 /* Note that it is safe to invert for real values here because we
14998 have already handled the one case that it matters. */
15001 if (code == NE_EXPR || code == GE_EXPR)
15004 code = invert_tree_comparison (code, false);
15007 /* Compute a result for LT or EQ if args permit;
15008 Otherwise return T. */
15009 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
15011 if (code == EQ_EXPR)
15012 result = tree_int_cst_equal (op0, op1);
15013 else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
15014 result = INT_CST_LT_UNSIGNED (op0, op1);
15016 result = INT_CST_LT (op0, op1);
15023 return constant_boolean_node (result, type);
15026 /* If necessary, return a CLEANUP_POINT_EXPR for EXPR with the
15027 indicated TYPE. If no CLEANUP_POINT_EXPR is necessary, return EXPR
15031 fold_build_cleanup_point_expr (tree type, tree expr)
15033 /* If the expression does not have side effects then we don't have to wrap
15034 it with a cleanup point expression. */
15035 if (!TREE_SIDE_EFFECTS (expr))
15038 /* If the expression is a return, check to see if the expression inside the
15039 return has no side effects or the right hand side of the modify expression
15040 inside the return. If either don't have side effects set we don't need to
15041 wrap the expression in a cleanup point expression. Note we don't check the
15042 left hand side of the modify because it should always be a return decl. */
15043 if (TREE_CODE (expr) == RETURN_EXPR)
15045 tree op = TREE_OPERAND (expr, 0);
15046 if (!op || !TREE_SIDE_EFFECTS (op))
15048 op = TREE_OPERAND (op, 1);
15049 if (!TREE_SIDE_EFFECTS (op))
15053 return build1 (CLEANUP_POINT_EXPR, type, expr);
15056 /* Given a pointer value OP0 and a type TYPE, return a simplified version
15057 of an indirection through OP0, or NULL_TREE if no simplification is
15061 fold_indirect_ref_1 (tree type, tree op0)
15067 subtype = TREE_TYPE (sub);
15068 if (!POINTER_TYPE_P (subtype))
15071 if (TREE_CODE (sub) == ADDR_EXPR)
15073 tree op = TREE_OPERAND (sub, 0);
15074 tree optype = TREE_TYPE (op);
15075 /* *&CONST_DECL -> to the value of the const decl. */
15076 if (TREE_CODE (op) == CONST_DECL)
15077 return DECL_INITIAL (op);
15078 /* *&p => p; make sure to handle *&"str"[cst] here. */
15079 if (type == optype)
15081 tree fop = fold_read_from_constant_string (op);
15087 /* *(foo *)&fooarray => fooarray[0] */
15088 else if (TREE_CODE (optype) == ARRAY_TYPE
15089 && type == TREE_TYPE (optype))
15091 tree type_domain = TYPE_DOMAIN (optype);
15092 tree min_val = size_zero_node;
15093 if (type_domain && TYPE_MIN_VALUE (type_domain))
15094 min_val = TYPE_MIN_VALUE (type_domain);
15095 return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
15097 /* *(foo *)&complexfoo => __real__ complexfoo */
15098 else if (TREE_CODE (optype) == COMPLEX_TYPE
15099 && type == TREE_TYPE (optype))
15100 return fold_build1 (REALPART_EXPR, type, op);
15101 /* *(foo *)&vectorfoo => BIT_FIELD_REF<vectorfoo,...> */
15102 else if (TREE_CODE (optype) == VECTOR_TYPE
15103 && type == TREE_TYPE (optype))
15105 tree part_width = TYPE_SIZE (type);
15106 tree index = bitsize_int (0);
15107 return fold_build3 (BIT_FIELD_REF, type, op, part_width, index);
15111 /* ((foo*)&vectorfoo)[1] => BIT_FIELD_REF<vectorfoo,...> */
15112 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15113 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15115 tree op00 = TREE_OPERAND (sub, 0);
15116 tree op01 = TREE_OPERAND (sub, 1);
15120 op00type = TREE_TYPE (op00);
15121 if (TREE_CODE (op00) == ADDR_EXPR
15122 && TREE_CODE (TREE_TYPE (op00type)) == VECTOR_TYPE
15123 && type == TREE_TYPE (TREE_TYPE (op00type)))
15125 HOST_WIDE_INT offset = tree_low_cst (op01, 0);
15126 tree part_width = TYPE_SIZE (type);
15127 unsigned HOST_WIDE_INT part_widthi = tree_low_cst (part_width, 0)/BITS_PER_UNIT;
15128 unsigned HOST_WIDE_INT indexi = offset * BITS_PER_UNIT;
15129 tree index = bitsize_int (indexi);
15131 if (offset/part_widthi <= TYPE_VECTOR_SUBPARTS (TREE_TYPE (op00type)))
15132 return fold_build3 (BIT_FIELD_REF, type, TREE_OPERAND (op00, 0),
15133 part_width, index);
15139 /* ((foo*)&complexfoo)[1] => __imag__ complexfoo */
15140 if (TREE_CODE (sub) == POINTER_PLUS_EXPR
15141 && TREE_CODE (TREE_OPERAND (sub, 1)) == INTEGER_CST)
15143 tree op00 = TREE_OPERAND (sub, 0);
15144 tree op01 = TREE_OPERAND (sub, 1);
15148 op00type = TREE_TYPE (op00);
15149 if (TREE_CODE (op00) == ADDR_EXPR
15150 && TREE_CODE (TREE_TYPE (op00type)) == COMPLEX_TYPE
15151 && type == TREE_TYPE (TREE_TYPE (op00type)))
15153 tree size = TYPE_SIZE_UNIT (type);
15154 if (tree_int_cst_equal (size, op01))
15155 return fold_build1 (IMAGPART_EXPR, type, TREE_OPERAND (op00, 0));
15159 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
15160 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
15161 && type == TREE_TYPE (TREE_TYPE (subtype)))
15164 tree min_val = size_zero_node;
15165 sub = build_fold_indirect_ref (sub);
15166 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
15167 if (type_domain && TYPE_MIN_VALUE (type_domain))
15168 min_val = TYPE_MIN_VALUE (type_domain);
15169 return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
15175 /* Builds an expression for an indirection through T, simplifying some
15179 build_fold_indirect_ref (tree t)
15181 tree type = TREE_TYPE (TREE_TYPE (t));
15182 tree sub = fold_indirect_ref_1 (type, t);
15187 return build1 (INDIRECT_REF, type, t);
15190 /* Given an INDIRECT_REF T, return either T or a simplified version. */
15193 fold_indirect_ref (tree t)
15195 tree sub = fold_indirect_ref_1 (TREE_TYPE (t), TREE_OPERAND (t, 0));
15203 /* Strip non-trapping, non-side-effecting tree nodes from an expression
15204 whose result is ignored. The type of the returned tree need not be
15205 the same as the original expression. */
15208 fold_ignored_result (tree t)
15210 if (!TREE_SIDE_EFFECTS (t))
15211 return integer_zero_node;
15214 switch (TREE_CODE_CLASS (TREE_CODE (t)))
15217 t = TREE_OPERAND (t, 0);
15221 case tcc_comparison:
15222 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15223 t = TREE_OPERAND (t, 0);
15224 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
15225 t = TREE_OPERAND (t, 1);
15230 case tcc_expression:
15231 switch (TREE_CODE (t))
15233 case COMPOUND_EXPR:
15234 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
15236 t = TREE_OPERAND (t, 0);
15240 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
15241 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
15243 t = TREE_OPERAND (t, 0);
15256 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
15257 This can only be applied to objects of a sizetype. */
15260 round_up (tree value, int divisor)
15262 tree div = NULL_TREE;
15264 gcc_assert (divisor > 0);
15268 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
15269 have to do anything. Only do this when we are not given a const,
15270 because in that case, this check is more expensive than just
15272 if (TREE_CODE (value) != INTEGER_CST)
15274 div = build_int_cst (TREE_TYPE (value), divisor);
15276 if (multiple_of_p (TREE_TYPE (value), value, div))
15280 /* If divisor is a power of two, simplify this to bit manipulation. */
15281 if (divisor == (divisor & -divisor))
15283 if (TREE_CODE (value) == INTEGER_CST)
15285 unsigned HOST_WIDE_INT low = TREE_INT_CST_LOW (value);
15286 unsigned HOST_WIDE_INT high;
15289 if ((low & (divisor - 1)) == 0)
15292 overflow_p = TREE_OVERFLOW (value);
15293 high = TREE_INT_CST_HIGH (value);
15294 low &= ~(divisor - 1);
15303 return force_fit_type_double (TREE_TYPE (value), low, high,
15310 t = build_int_cst (TREE_TYPE (value), divisor - 1);
15311 value = size_binop (PLUS_EXPR, value, t);
15312 t = build_int_cst (TREE_TYPE (value), -divisor);
15313 value = size_binop (BIT_AND_EXPR, value, t);
15319 div = build_int_cst (TREE_TYPE (value), divisor);
15320 value = size_binop (CEIL_DIV_EXPR, value, div);
15321 value = size_binop (MULT_EXPR, value, div);
15327 /* Likewise, but round down. */
15330 round_down (tree value, int divisor)
15332 tree div = NULL_TREE;
15334 gcc_assert (divisor > 0);
15338 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
15339 have to do anything. Only do this when we are not given a const,
15340 because in that case, this check is more expensive than just
15342 if (TREE_CODE (value) != INTEGER_CST)
15344 div = build_int_cst (TREE_TYPE (value), divisor);
15346 if (multiple_of_p (TREE_TYPE (value), value, div))
15350 /* If divisor is a power of two, simplify this to bit manipulation. */
15351 if (divisor == (divisor & -divisor))
15355 t = build_int_cst (TREE_TYPE (value), -divisor);
15356 value = size_binop (BIT_AND_EXPR, value, t);
15361 div = build_int_cst (TREE_TYPE (value), divisor);
15362 value = size_binop (FLOOR_DIV_EXPR, value, div);
15363 value = size_binop (MULT_EXPR, value, div);
15369 /* Returns the pointer to the base of the object addressed by EXP and
15370 extracts the information about the offset of the access, storing it
15371 to PBITPOS and POFFSET. */
15374 split_address_to_core_and_offset (tree exp,
15375 HOST_WIDE_INT *pbitpos, tree *poffset)
15378 enum machine_mode mode;
15379 int unsignedp, volatilep;
15380 HOST_WIDE_INT bitsize;
15382 if (TREE_CODE (exp) == ADDR_EXPR)
15384 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
15385 poffset, &mode, &unsignedp, &volatilep,
15387 core = fold_addr_expr (core);
15393 *poffset = NULL_TREE;
15399 /* Returns true if addresses of E1 and E2 differ by a constant, false
15400 otherwise. If they do, E1 - E2 is stored in *DIFF. */
15403 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
15406 HOST_WIDE_INT bitpos1, bitpos2;
15407 tree toffset1, toffset2, tdiff, type;
15409 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
15410 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
15412 if (bitpos1 % BITS_PER_UNIT != 0
15413 || bitpos2 % BITS_PER_UNIT != 0
15414 || !operand_equal_p (core1, core2, 0))
15417 if (toffset1 && toffset2)
15419 type = TREE_TYPE (toffset1);
15420 if (type != TREE_TYPE (toffset2))
15421 toffset2 = fold_convert (type, toffset2);
15423 tdiff = fold_build2 (MINUS_EXPR, type, toffset1, toffset2);
15424 if (!cst_and_fits_in_hwi (tdiff))
15427 *diff = int_cst_value (tdiff);
15429 else if (toffset1 || toffset2)
15431 /* If only one of the offsets is non-constant, the difference cannot
15438 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
15442 /* Simplify the floating point expression EXP when the sign of the
15443 result is not significant. Return NULL_TREE if no simplification
15447 fold_strip_sign_ops (tree exp)
15451 switch (TREE_CODE (exp))
15455 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15456 return arg0 ? arg0 : TREE_OPERAND (exp, 0);
15460 if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
15462 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
15463 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15464 if (arg0 != NULL_TREE || arg1 != NULL_TREE)
15465 return fold_build2 (TREE_CODE (exp), TREE_TYPE (exp),
15466 arg0 ? arg0 : TREE_OPERAND (exp, 0),
15467 arg1 ? arg1 : TREE_OPERAND (exp, 1));
15470 case COMPOUND_EXPR:
15471 arg0 = TREE_OPERAND (exp, 0);
15472 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15474 return fold_build2 (COMPOUND_EXPR, TREE_TYPE (exp), arg0, arg1);
15478 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
15479 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 2));
15481 return fold_build3 (COND_EXPR, TREE_TYPE (exp), TREE_OPERAND (exp, 0),
15482 arg0 ? arg0 : TREE_OPERAND (exp, 1),
15483 arg1 ? arg1 : TREE_OPERAND (exp, 2));
15488 const enum built_in_function fcode = builtin_mathfn_code (exp);
15491 CASE_FLT_FN (BUILT_IN_COPYSIGN):
15492 /* Strip copysign function call, return the 1st argument. */
15493 arg0 = CALL_EXPR_ARG (exp, 0);
15494 arg1 = CALL_EXPR_ARG (exp, 1);
15495 return omit_one_operand (TREE_TYPE (exp), arg0, arg1);
15498 /* Strip sign ops from the argument of "odd" math functions. */
15499 if (negate_mathfn_p (fcode))
15501 arg0 = fold_strip_sign_ops (CALL_EXPR_ARG (exp, 0));
15503 return build_call_expr (get_callee_fndecl (exp), 1, arg0);