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 Free Software Foundation, Inc.
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING. If not, write to the Free
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA
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
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 takes a constant, an overflowable flag and prior
43 overflow indicators. It forces the value to fit the type and sets
44 TREE_OVERFLOW and TREE_CONSTANT_OVERFLOW as appropriate. */
48 #include "coretypes.h"
59 #include "langhooks.h"
62 /* The following constants represent a bit based encoding of GCC's
63 comparison operators. This encoding simplifies transformations
64 on relational comparison operators, such as AND and OR. */
65 enum comparison_code {
84 static void encode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT, HOST_WIDE_INT);
85 static void decode (HOST_WIDE_INT *, unsigned HOST_WIDE_INT *, HOST_WIDE_INT *);
86 static bool negate_mathfn_p (enum built_in_function);
87 static bool negate_expr_p (tree);
88 static tree negate_expr (tree);
89 static tree split_tree (tree, enum tree_code, tree *, tree *, tree *, int);
90 static tree associate_trees (tree, tree, enum tree_code, tree);
91 static tree const_binop (enum tree_code, tree, tree, int);
92 static enum tree_code invert_tree_comparison (enum tree_code, bool);
93 static enum comparison_code comparison_to_compcode (enum tree_code);
94 static enum tree_code compcode_to_comparison (enum comparison_code);
95 static tree combine_comparisons (enum tree_code, enum tree_code,
96 enum tree_code, tree, tree, tree);
97 static int truth_value_p (enum tree_code);
98 static int operand_equal_for_comparison_p (tree, tree, tree);
99 static int twoval_comparison_p (tree, tree *, tree *, int *);
100 static tree eval_subst (tree, tree, tree, tree, tree);
101 static tree pedantic_omit_one_operand (tree, tree, tree);
102 static tree distribute_bit_expr (enum tree_code, tree, tree, tree);
103 static tree make_bit_field_ref (tree, tree, int, int, int);
104 static tree optimize_bit_field_compare (enum tree_code, tree, tree, tree);
105 static tree decode_field_reference (tree, HOST_WIDE_INT *, HOST_WIDE_INT *,
106 enum machine_mode *, int *, int *,
108 static int all_ones_mask_p (tree, int);
109 static tree sign_bit_p (tree, tree);
110 static int simple_operand_p (tree);
111 static tree range_binop (enum tree_code, tree, tree, int, tree, int);
112 static tree make_range (tree, int *, tree *, tree *);
113 static tree build_range_check (tree, tree, int, tree, tree);
114 static int merge_ranges (int *, tree *, tree *, int, tree, tree, int, tree,
116 static tree fold_range_test (enum tree_code, tree, tree, tree);
117 static tree fold_cond_expr_with_comparison (tree, tree, tree, tree);
118 static tree unextend (tree, int, int, tree);
119 static tree fold_truthop (enum tree_code, tree, tree, tree);
120 static tree optimize_minmax_comparison (enum tree_code, tree, tree, tree);
121 static tree extract_muldiv (tree, tree, enum tree_code, tree);
122 static tree extract_muldiv_1 (tree, tree, enum tree_code, tree);
123 static int multiple_of_p (tree, tree, tree);
124 static tree fold_binary_op_with_conditional_arg (enum tree_code, tree,
127 static bool fold_real_zero_addition_p (tree, tree, int);
128 static tree fold_mathfn_compare (enum built_in_function, enum tree_code,
130 static tree fold_inf_compare (enum tree_code, tree, tree, tree);
131 static tree fold_div_compare (enum tree_code, tree, tree, tree);
132 static bool reorder_operands_p (tree, tree);
133 static tree fold_negate_const (tree, tree);
134 static tree fold_not_const (tree, tree);
135 static tree fold_relational_const (enum tree_code, tree, tree, tree);
136 static tree fold_relational_hi_lo (enum tree_code *, const tree,
138 static bool tree_expr_nonzero_p (tree);
140 /* We know that A1 + B1 = SUM1, using 2's complement arithmetic and ignoring
141 overflow. Suppose A, B and SUM have the same respective signs as A1, B1,
142 and SUM1. Then this yields nonzero if overflow occurred during the
145 Overflow occurs if A and B have the same sign, but A and SUM differ in
146 sign. Use `^' to test whether signs differ, and `< 0' to isolate the
148 #define OVERFLOW_SUM_SIGN(a, b, sum) ((~((a) ^ (b)) & ((a) ^ (sum))) < 0)
150 /* To do constant folding on INTEGER_CST nodes requires two-word arithmetic.
151 We do that by representing the two-word integer in 4 words, with only
152 HOST_BITS_PER_WIDE_INT / 2 bits stored in each word, as a positive
153 number. The value of the word is LOWPART + HIGHPART * BASE. */
156 ((x) & (((unsigned HOST_WIDE_INT) 1 << (HOST_BITS_PER_WIDE_INT / 2)) - 1))
157 #define HIGHPART(x) \
158 ((unsigned HOST_WIDE_INT) (x) >> HOST_BITS_PER_WIDE_INT / 2)
159 #define BASE ((unsigned HOST_WIDE_INT) 1 << HOST_BITS_PER_WIDE_INT / 2)
161 /* Unpack a two-word integer into 4 words.
162 LOW and HI are the integer, as two `HOST_WIDE_INT' pieces.
163 WORDS points to the array of HOST_WIDE_INTs. */
166 encode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT low, HOST_WIDE_INT hi)
168 words[0] = LOWPART (low);
169 words[1] = HIGHPART (low);
170 words[2] = LOWPART (hi);
171 words[3] = HIGHPART (hi);
174 /* Pack an array of 4 words into a two-word integer.
175 WORDS points to the array of words.
176 The integer is stored into *LOW and *HI as two `HOST_WIDE_INT' pieces. */
179 decode (HOST_WIDE_INT *words, unsigned HOST_WIDE_INT *low,
182 *low = words[0] + words[1] * BASE;
183 *hi = words[2] + words[3] * BASE;
186 /* T is an INT_CST node. OVERFLOWABLE indicates if we are interested
187 in overflow of the value, when >0 we are only interested in signed
188 overflow, for <0 we are interested in any overflow. OVERFLOWED
189 indicates whether overflow has already occurred. CONST_OVERFLOWED
190 indicates whether constant overflow has already occurred. We force
191 T's value to be within range of T's type (by setting to 0 or 1 all
192 the bits outside the type's range). We set TREE_OVERFLOWED if,
193 OVERFLOWED is nonzero,
194 or OVERFLOWABLE is >0 and signed overflow occurs
195 or OVERFLOWABLE is <0 and any overflow occurs
196 We set TREE_CONSTANT_OVERFLOWED if,
197 CONST_OVERFLOWED is nonzero
198 or we set TREE_OVERFLOWED.
199 We return either the original T, or a copy. */
202 force_fit_type (tree t, int overflowable,
203 bool overflowed, bool overflowed_const)
205 unsigned HOST_WIDE_INT low;
208 int sign_extended_type;
210 gcc_assert (TREE_CODE (t) == INTEGER_CST);
212 low = TREE_INT_CST_LOW (t);
213 high = TREE_INT_CST_HIGH (t);
215 if (POINTER_TYPE_P (TREE_TYPE (t))
216 || TREE_CODE (TREE_TYPE (t)) == OFFSET_TYPE)
219 prec = TYPE_PRECISION (TREE_TYPE (t));
220 /* Size types *are* sign extended. */
221 sign_extended_type = (!TYPE_UNSIGNED (TREE_TYPE (t))
222 || (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
223 && TYPE_IS_SIZETYPE (TREE_TYPE (t))));
225 /* First clear all bits that are beyond the type's precision. */
227 if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
229 else if (prec > HOST_BITS_PER_WIDE_INT)
230 high &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
234 if (prec < HOST_BITS_PER_WIDE_INT)
235 low &= ~((HOST_WIDE_INT) (-1) << prec);
238 if (!sign_extended_type)
239 /* No sign extension */;
240 else if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
241 /* Correct width already. */;
242 else if (prec > HOST_BITS_PER_WIDE_INT)
244 /* Sign extend top half? */
245 if (high & ((unsigned HOST_WIDE_INT)1
246 << (prec - HOST_BITS_PER_WIDE_INT - 1)))
247 high |= (HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT);
249 else if (prec == HOST_BITS_PER_WIDE_INT)
251 if ((HOST_WIDE_INT)low < 0)
256 /* Sign extend bottom half? */
257 if (low & ((unsigned HOST_WIDE_INT)1 << (prec - 1)))
260 low |= (HOST_WIDE_INT)(-1) << prec;
264 /* If the value changed, return a new node. */
265 if (overflowed || overflowed_const
266 || low != TREE_INT_CST_LOW (t) || high != TREE_INT_CST_HIGH (t))
268 t = build_int_cst_wide (TREE_TYPE (t), low, high);
272 || (overflowable > 0 && sign_extended_type))
275 TREE_OVERFLOW (t) = 1;
276 TREE_CONSTANT_OVERFLOW (t) = 1;
278 else if (overflowed_const)
281 TREE_CONSTANT_OVERFLOW (t) = 1;
288 /* Add two doubleword integers with doubleword result.
289 Each argument is given as two `HOST_WIDE_INT' pieces.
290 One argument is L1 and H1; the other, L2 and H2.
291 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
294 add_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
295 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
296 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
298 unsigned HOST_WIDE_INT l;
302 h = h1 + h2 + (l < l1);
306 return OVERFLOW_SUM_SIGN (h1, h2, h);
309 /* Negate a doubleword integer with doubleword result.
310 Return nonzero if the operation overflows, assuming it's signed.
311 The argument is given as two `HOST_WIDE_INT' pieces in L1 and H1.
312 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
315 neg_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
316 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
322 return (*hv & h1) < 0;
332 /* Multiply two doubleword integers with doubleword result.
333 Return nonzero if the operation overflows, assuming it's signed.
334 Each argument is given as two `HOST_WIDE_INT' pieces.
335 One argument is L1 and H1; the other, L2 and H2.
336 The value is stored as two `HOST_WIDE_INT' pieces in *LV and *HV. */
339 mul_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
340 unsigned HOST_WIDE_INT l2, HOST_WIDE_INT h2,
341 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
343 HOST_WIDE_INT arg1[4];
344 HOST_WIDE_INT arg2[4];
345 HOST_WIDE_INT prod[4 * 2];
346 unsigned HOST_WIDE_INT carry;
348 unsigned HOST_WIDE_INT toplow, neglow;
349 HOST_WIDE_INT tophigh, neghigh;
351 encode (arg1, l1, h1);
352 encode (arg2, l2, h2);
354 memset (prod, 0, sizeof prod);
356 for (i = 0; i < 4; i++)
359 for (j = 0; j < 4; j++)
362 /* This product is <= 0xFFFE0001, the sum <= 0xFFFF0000. */
363 carry += arg1[i] * arg2[j];
364 /* Since prod[p] < 0xFFFF, this sum <= 0xFFFFFFFF. */
366 prod[k] = LOWPART (carry);
367 carry = HIGHPART (carry);
372 decode (prod, lv, hv); /* This ignores prod[4] through prod[4*2-1] */
374 /* Check for overflow by calculating the top half of the answer in full;
375 it should agree with the low half's sign bit. */
376 decode (prod + 4, &toplow, &tophigh);
379 neg_double (l2, h2, &neglow, &neghigh);
380 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
384 neg_double (l1, h1, &neglow, &neghigh);
385 add_double (neglow, neghigh, toplow, tophigh, &toplow, &tophigh);
387 return (*hv < 0 ? ~(toplow & tophigh) : toplow | tophigh) != 0;
390 /* Shift the doubleword integer in L1, H1 left by COUNT places
391 keeping only PREC bits of result.
392 Shift right if COUNT is negative.
393 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
394 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
397 lshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
398 HOST_WIDE_INT count, unsigned int prec,
399 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv, int arith)
401 unsigned HOST_WIDE_INT signmask;
405 rshift_double (l1, h1, -count, prec, lv, hv, arith);
409 if (SHIFT_COUNT_TRUNCATED)
412 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
414 /* Shifting by the host word size is undefined according to the
415 ANSI standard, so we must handle this as a special case. */
419 else if (count >= HOST_BITS_PER_WIDE_INT)
421 *hv = l1 << (count - HOST_BITS_PER_WIDE_INT);
426 *hv = (((unsigned HOST_WIDE_INT) h1 << count)
427 | (l1 >> (HOST_BITS_PER_WIDE_INT - count - 1) >> 1));
431 /* Sign extend all bits that are beyond the precision. */
433 signmask = -((prec > HOST_BITS_PER_WIDE_INT
434 ? ((unsigned HOST_WIDE_INT) *hv
435 >> (prec - HOST_BITS_PER_WIDE_INT - 1))
436 : (*lv >> (prec - 1))) & 1);
438 if (prec >= 2 * HOST_BITS_PER_WIDE_INT)
440 else if (prec >= HOST_BITS_PER_WIDE_INT)
442 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - HOST_BITS_PER_WIDE_INT));
443 *hv |= signmask << (prec - HOST_BITS_PER_WIDE_INT);
448 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << prec);
449 *lv |= signmask << prec;
453 /* Shift the doubleword integer in L1, H1 right by COUNT places
454 keeping only PREC bits of result. COUNT must be positive.
455 ARITH nonzero specifies arithmetic shifting; otherwise use logical shift.
456 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
459 rshift_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
460 HOST_WIDE_INT count, unsigned int prec,
461 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv,
464 unsigned HOST_WIDE_INT signmask;
467 ? -((unsigned HOST_WIDE_INT) h1 >> (HOST_BITS_PER_WIDE_INT - 1))
470 if (SHIFT_COUNT_TRUNCATED)
473 if (count >= 2 * HOST_BITS_PER_WIDE_INT)
475 /* Shifting by the host word size is undefined according to the
476 ANSI standard, so we must handle this as a special case. */
480 else if (count >= HOST_BITS_PER_WIDE_INT)
483 *lv = (unsigned HOST_WIDE_INT) h1 >> (count - HOST_BITS_PER_WIDE_INT);
487 *hv = (unsigned HOST_WIDE_INT) h1 >> count;
489 | ((unsigned HOST_WIDE_INT) h1 << (HOST_BITS_PER_WIDE_INT - count - 1) << 1));
492 /* Zero / sign extend all bits that are beyond the precision. */
494 if (count >= (HOST_WIDE_INT)prec)
499 else if ((prec - count) >= 2 * HOST_BITS_PER_WIDE_INT)
501 else if ((prec - count) >= HOST_BITS_PER_WIDE_INT)
503 *hv &= ~((HOST_WIDE_INT) (-1) << (prec - count - HOST_BITS_PER_WIDE_INT));
504 *hv |= signmask << (prec - count - HOST_BITS_PER_WIDE_INT);
509 *lv &= ~((unsigned HOST_WIDE_INT) (-1) << (prec - count));
510 *lv |= signmask << (prec - count);
514 /* Rotate the doubleword integer in L1, H1 left by COUNT places
515 keeping only PREC bits of result.
516 Rotate right if COUNT is negative.
517 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
520 lrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
521 HOST_WIDE_INT count, unsigned int prec,
522 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
524 unsigned HOST_WIDE_INT s1l, s2l;
525 HOST_WIDE_INT s1h, s2h;
531 lshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
532 rshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
537 /* Rotate the doubleword integer in L1, H1 left by COUNT places
538 keeping only PREC bits of result. COUNT must be positive.
539 Store the value as two `HOST_WIDE_INT' pieces in *LV and *HV. */
542 rrotate_double (unsigned HOST_WIDE_INT l1, HOST_WIDE_INT h1,
543 HOST_WIDE_INT count, unsigned int prec,
544 unsigned HOST_WIDE_INT *lv, HOST_WIDE_INT *hv)
546 unsigned HOST_WIDE_INT s1l, s2l;
547 HOST_WIDE_INT s1h, s2h;
553 rshift_double (l1, h1, count, prec, &s1l, &s1h, 0);
554 lshift_double (l1, h1, prec - count, prec, &s2l, &s2h, 0);
559 /* Divide doubleword integer LNUM, HNUM by doubleword integer LDEN, HDEN
560 for a quotient (stored in *LQUO, *HQUO) and remainder (in *LREM, *HREM).
561 CODE is a tree code for a kind of division, one of
562 TRUNC_DIV_EXPR, FLOOR_DIV_EXPR, CEIL_DIV_EXPR, ROUND_DIV_EXPR
564 It controls how the quotient is rounded to an integer.
565 Return nonzero if the operation overflows.
566 UNS nonzero says do unsigned division. */
569 div_and_round_double (enum tree_code code, int uns,
570 unsigned HOST_WIDE_INT lnum_orig, /* num == numerator == dividend */
571 HOST_WIDE_INT hnum_orig,
572 unsigned HOST_WIDE_INT lden_orig, /* den == denominator == divisor */
573 HOST_WIDE_INT hden_orig,
574 unsigned HOST_WIDE_INT *lquo,
575 HOST_WIDE_INT *hquo, unsigned HOST_WIDE_INT *lrem,
579 HOST_WIDE_INT num[4 + 1]; /* extra element for scaling. */
580 HOST_WIDE_INT den[4], quo[4];
582 unsigned HOST_WIDE_INT work;
583 unsigned HOST_WIDE_INT carry = 0;
584 unsigned HOST_WIDE_INT lnum = lnum_orig;
585 HOST_WIDE_INT hnum = hnum_orig;
586 unsigned HOST_WIDE_INT lden = lden_orig;
587 HOST_WIDE_INT hden = hden_orig;
590 if (hden == 0 && lden == 0)
591 overflow = 1, lden = 1;
593 /* Calculate quotient sign and convert operands to unsigned. */
599 /* (minimum integer) / (-1) is the only overflow case. */
600 if (neg_double (lnum, hnum, &lnum, &hnum)
601 && ((HOST_WIDE_INT) lden & hden) == -1)
607 neg_double (lden, hden, &lden, &hden);
611 if (hnum == 0 && hden == 0)
612 { /* single precision */
614 /* This unsigned division rounds toward zero. */
620 { /* trivial case: dividend < divisor */
621 /* hden != 0 already checked. */
628 memset (quo, 0, sizeof quo);
630 memset (num, 0, sizeof num); /* to zero 9th element */
631 memset (den, 0, sizeof den);
633 encode (num, lnum, hnum);
634 encode (den, lden, hden);
636 /* Special code for when the divisor < BASE. */
637 if (hden == 0 && lden < (unsigned HOST_WIDE_INT) BASE)
639 /* hnum != 0 already checked. */
640 for (i = 4 - 1; i >= 0; i--)
642 work = num[i] + carry * BASE;
643 quo[i] = work / lden;
649 /* Full double precision division,
650 with thanks to Don Knuth's "Seminumerical Algorithms". */
651 int num_hi_sig, den_hi_sig;
652 unsigned HOST_WIDE_INT quo_est, scale;
654 /* Find the highest nonzero divisor digit. */
655 for (i = 4 - 1;; i--)
662 /* Insure that the first digit of the divisor is at least BASE/2.
663 This is required by the quotient digit estimation algorithm. */
665 scale = BASE / (den[den_hi_sig] + 1);
667 { /* scale divisor and dividend */
669 for (i = 0; i <= 4 - 1; i++)
671 work = (num[i] * scale) + carry;
672 num[i] = LOWPART (work);
673 carry = HIGHPART (work);
678 for (i = 0; i <= 4 - 1; i++)
680 work = (den[i] * scale) + carry;
681 den[i] = LOWPART (work);
682 carry = HIGHPART (work);
683 if (den[i] != 0) den_hi_sig = i;
690 for (i = num_hi_sig - den_hi_sig - 1; i >= 0; i--)
692 /* Guess the next quotient digit, quo_est, by dividing the first
693 two remaining dividend digits by the high order quotient digit.
694 quo_est is never low and is at most 2 high. */
695 unsigned HOST_WIDE_INT tmp;
697 num_hi_sig = i + den_hi_sig + 1;
698 work = num[num_hi_sig] * BASE + num[num_hi_sig - 1];
699 if (num[num_hi_sig] != den[den_hi_sig])
700 quo_est = work / den[den_hi_sig];
704 /* Refine quo_est so it's usually correct, and at most one high. */
705 tmp = work - quo_est * den[den_hi_sig];
707 && (den[den_hi_sig - 1] * quo_est
708 > (tmp * BASE + num[num_hi_sig - 2])))
711 /* Try QUO_EST as the quotient digit, by multiplying the
712 divisor by QUO_EST and subtracting from the remaining dividend.
713 Keep in mind that QUO_EST is the I - 1st digit. */
716 for (j = 0; j <= den_hi_sig; j++)
718 work = quo_est * den[j] + carry;
719 carry = HIGHPART (work);
720 work = num[i + j] - LOWPART (work);
721 num[i + j] = LOWPART (work);
722 carry += HIGHPART (work) != 0;
725 /* If quo_est was high by one, then num[i] went negative and
726 we need to correct things. */
727 if (num[num_hi_sig] < (HOST_WIDE_INT) carry)
730 carry = 0; /* add divisor back in */
731 for (j = 0; j <= den_hi_sig; j++)
733 work = num[i + j] + den[j] + carry;
734 carry = HIGHPART (work);
735 num[i + j] = LOWPART (work);
738 num [num_hi_sig] += carry;
741 /* Store the quotient digit. */
746 decode (quo, lquo, hquo);
749 /* If result is negative, make it so. */
751 neg_double (*lquo, *hquo, lquo, hquo);
753 /* Compute trial remainder: rem = num - (quo * den) */
754 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
755 neg_double (*lrem, *hrem, lrem, hrem);
756 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
761 case TRUNC_MOD_EXPR: /* round toward zero */
762 case EXACT_DIV_EXPR: /* for this one, it shouldn't matter */
766 case FLOOR_MOD_EXPR: /* round toward negative infinity */
767 if (quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio < 0 && rem != 0 */
770 add_double (*lquo, *hquo, (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1,
778 case CEIL_MOD_EXPR: /* round toward positive infinity */
779 if (!quo_neg && (*lrem != 0 || *hrem != 0)) /* ratio > 0 && rem != 0 */
781 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
789 case ROUND_MOD_EXPR: /* round to closest integer */
791 unsigned HOST_WIDE_INT labs_rem = *lrem;
792 HOST_WIDE_INT habs_rem = *hrem;
793 unsigned HOST_WIDE_INT labs_den = lden, ltwice;
794 HOST_WIDE_INT habs_den = hden, htwice;
796 /* Get absolute values. */
798 neg_double (*lrem, *hrem, &labs_rem, &habs_rem);
800 neg_double (lden, hden, &labs_den, &habs_den);
802 /* If (2 * abs (lrem) >= abs (lden)) */
803 mul_double ((HOST_WIDE_INT) 2, (HOST_WIDE_INT) 0,
804 labs_rem, habs_rem, <wice, &htwice);
806 if (((unsigned HOST_WIDE_INT) habs_den
807 < (unsigned HOST_WIDE_INT) htwice)
808 || (((unsigned HOST_WIDE_INT) habs_den
809 == (unsigned HOST_WIDE_INT) htwice)
810 && (labs_den < ltwice)))
814 add_double (*lquo, *hquo,
815 (HOST_WIDE_INT) -1, (HOST_WIDE_INT) -1, lquo, hquo);
818 add_double (*lquo, *hquo, (HOST_WIDE_INT) 1, (HOST_WIDE_INT) 0,
830 /* Compute true remainder: rem = num - (quo * den) */
831 mul_double (*lquo, *hquo, lden_orig, hden_orig, lrem, hrem);
832 neg_double (*lrem, *hrem, lrem, hrem);
833 add_double (lnum_orig, hnum_orig, *lrem, *hrem, lrem, hrem);
837 /* Return true if built-in mathematical function specified by CODE
838 preserves the sign of it argument, i.e. -f(x) == f(-x). */
841 negate_mathfn_p (enum built_in_function code)
865 /* Check whether we may negate an integer constant T without causing
869 may_negate_without_overflow_p (tree t)
871 unsigned HOST_WIDE_INT val;
875 gcc_assert (TREE_CODE (t) == INTEGER_CST);
877 type = TREE_TYPE (t);
878 if (TYPE_UNSIGNED (type))
881 prec = TYPE_PRECISION (type);
882 if (prec > HOST_BITS_PER_WIDE_INT)
884 if (TREE_INT_CST_LOW (t) != 0)
886 prec -= HOST_BITS_PER_WIDE_INT;
887 val = TREE_INT_CST_HIGH (t);
890 val = TREE_INT_CST_LOW (t);
891 if (prec < HOST_BITS_PER_WIDE_INT)
892 val &= ((unsigned HOST_WIDE_INT) 1 << prec) - 1;
893 return val != ((unsigned HOST_WIDE_INT) 1 << (prec - 1));
896 /* Determine whether an expression T can be cheaply negated using
897 the function negate_expr. */
900 negate_expr_p (tree t)
907 type = TREE_TYPE (t);
910 switch (TREE_CODE (t))
913 if (TYPE_UNSIGNED (type) || ! flag_trapv)
916 /* Check that -CST will not overflow type. */
917 return may_negate_without_overflow_p (t);
924 return negate_expr_p (TREE_REALPART (t))
925 && negate_expr_p (TREE_IMAGPART (t));
928 if (FLOAT_TYPE_P (type) && !flag_unsafe_math_optimizations)
930 /* -(A + B) -> (-B) - A. */
931 if (negate_expr_p (TREE_OPERAND (t, 1))
932 && reorder_operands_p (TREE_OPERAND (t, 0),
933 TREE_OPERAND (t, 1)))
935 /* -(A + B) -> (-A) - B. */
936 return negate_expr_p (TREE_OPERAND (t, 0));
939 /* We can't turn -(A-B) into B-A when we honor signed zeros. */
940 return (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
941 && reorder_operands_p (TREE_OPERAND (t, 0),
942 TREE_OPERAND (t, 1));
945 if (TYPE_UNSIGNED (TREE_TYPE (t)))
951 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
952 return negate_expr_p (TREE_OPERAND (t, 1))
953 || negate_expr_p (TREE_OPERAND (t, 0));
957 /* Negate -((double)float) as (double)(-float). */
958 if (TREE_CODE (type) == REAL_TYPE)
960 tree tem = strip_float_extensions (t);
962 return negate_expr_p (tem);
967 /* Negate -f(x) as f(-x). */
968 if (negate_mathfn_p (builtin_mathfn_code (t)))
969 return negate_expr_p (TREE_VALUE (TREE_OPERAND (t, 1)));
973 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
974 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
976 tree op1 = TREE_OPERAND (t, 1);
977 if (TREE_INT_CST_HIGH (op1) == 0
978 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
979 == TREE_INT_CST_LOW (op1))
990 /* Given T, an expression, return the negation of T. Allow for T to be
991 null, in which case return null. */
1002 type = TREE_TYPE (t);
1003 STRIP_SIGN_NOPS (t);
1005 switch (TREE_CODE (t))
1008 tem = fold_negate_const (t, type);
1009 if (! TREE_OVERFLOW (tem)
1010 || TYPE_UNSIGNED (type)
1016 tem = fold_negate_const (t, type);
1017 /* Two's complement FP formats, such as c4x, may overflow. */
1018 if (! TREE_OVERFLOW (tem) || ! flag_trapping_math)
1019 return fold_convert (type, tem);
1024 tree rpart = negate_expr (TREE_REALPART (t));
1025 tree ipart = negate_expr (TREE_IMAGPART (t));
1027 if ((TREE_CODE (rpart) == REAL_CST
1028 && TREE_CODE (ipart) == REAL_CST)
1029 || (TREE_CODE (rpart) == INTEGER_CST
1030 && TREE_CODE (ipart) == INTEGER_CST))
1031 return build_complex (type, rpart, ipart);
1036 return fold_convert (type, TREE_OPERAND (t, 0));
1039 if (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
1041 /* -(A + B) -> (-B) - A. */
1042 if (negate_expr_p (TREE_OPERAND (t, 1))
1043 && reorder_operands_p (TREE_OPERAND (t, 0),
1044 TREE_OPERAND (t, 1)))
1046 tem = negate_expr (TREE_OPERAND (t, 1));
1047 tem = fold (build2 (MINUS_EXPR, TREE_TYPE (t),
1048 tem, TREE_OPERAND (t, 0)));
1049 return fold_convert (type, tem);
1052 /* -(A + B) -> (-A) - B. */
1053 if (negate_expr_p (TREE_OPERAND (t, 0)))
1055 tem = negate_expr (TREE_OPERAND (t, 0));
1056 tem = fold (build2 (MINUS_EXPR, TREE_TYPE (t),
1057 tem, TREE_OPERAND (t, 1)));
1058 return fold_convert (type, tem);
1064 /* - (A - B) -> B - A */
1065 if ((! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
1066 && reorder_operands_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1)))
1067 return fold_convert (type,
1068 fold (build2 (MINUS_EXPR, TREE_TYPE (t),
1069 TREE_OPERAND (t, 1),
1070 TREE_OPERAND (t, 0))));
1074 if (TYPE_UNSIGNED (TREE_TYPE (t)))
1080 if (! HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (t))))
1082 tem = TREE_OPERAND (t, 1);
1083 if (negate_expr_p (tem))
1084 return fold_convert (type,
1085 fold (build2 (TREE_CODE (t), TREE_TYPE (t),
1086 TREE_OPERAND (t, 0),
1087 negate_expr (tem))));
1088 tem = TREE_OPERAND (t, 0);
1089 if (negate_expr_p (tem))
1090 return fold_convert (type,
1091 fold (build2 (TREE_CODE (t), TREE_TYPE (t),
1093 TREE_OPERAND (t, 1))));
1098 /* Convert -((double)float) into (double)(-float). */
1099 if (TREE_CODE (type) == REAL_TYPE)
1101 tem = strip_float_extensions (t);
1102 if (tem != t && negate_expr_p (tem))
1103 return fold_convert (type, negate_expr (tem));
1108 /* Negate -f(x) as f(-x). */
1109 if (negate_mathfn_p (builtin_mathfn_code (t))
1110 && negate_expr_p (TREE_VALUE (TREE_OPERAND (t, 1))))
1112 tree fndecl, arg, arglist;
1114 fndecl = get_callee_fndecl (t);
1115 arg = negate_expr (TREE_VALUE (TREE_OPERAND (t, 1)));
1116 arglist = build_tree_list (NULL_TREE, arg);
1117 return build_function_call_expr (fndecl, arglist);
1122 /* Optimize -((int)x >> 31) into (unsigned)x >> 31. */
1123 if (TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST)
1125 tree op1 = TREE_OPERAND (t, 1);
1126 if (TREE_INT_CST_HIGH (op1) == 0
1127 && (unsigned HOST_WIDE_INT) (TYPE_PRECISION (type) - 1)
1128 == TREE_INT_CST_LOW (op1))
1130 tree ntype = TYPE_UNSIGNED (type)
1131 ? lang_hooks.types.signed_type (type)
1132 : lang_hooks.types.unsigned_type (type);
1133 tree temp = fold_convert (ntype, TREE_OPERAND (t, 0));
1134 temp = fold (build2 (RSHIFT_EXPR, ntype, temp, op1));
1135 return fold_convert (type, temp);
1144 tem = fold (build1 (NEGATE_EXPR, TREE_TYPE (t), t));
1145 return fold_convert (type, tem);
1148 /* Split a tree IN into a constant, literal and variable parts that could be
1149 combined with CODE to make IN. "constant" means an expression with
1150 TREE_CONSTANT but that isn't an actual constant. CODE must be a
1151 commutative arithmetic operation. Store the constant part into *CONP,
1152 the literal in *LITP and return the variable part. If a part isn't
1153 present, set it to null. If the tree does not decompose in this way,
1154 return the entire tree as the variable part and the other parts as null.
1156 If CODE is PLUS_EXPR we also split trees that use MINUS_EXPR. In that
1157 case, we negate an operand that was subtracted. Except if it is a
1158 literal for which we use *MINUS_LITP instead.
1160 If NEGATE_P is true, we are negating all of IN, again except a literal
1161 for which we use *MINUS_LITP instead.
1163 If IN is itself a literal or constant, return it as appropriate.
1165 Note that we do not guarantee that any of the three values will be the
1166 same type as IN, but they will have the same signedness and mode. */
1169 split_tree (tree in, enum tree_code code, tree *conp, tree *litp,
1170 tree *minus_litp, int negate_p)
1178 /* Strip any conversions that don't change the machine mode or signedness. */
1179 STRIP_SIGN_NOPS (in);
1181 if (TREE_CODE (in) == INTEGER_CST || TREE_CODE (in) == REAL_CST)
1183 else if (TREE_CODE (in) == code
1184 || (! FLOAT_TYPE_P (TREE_TYPE (in))
1185 /* We can associate addition and subtraction together (even
1186 though the C standard doesn't say so) for integers because
1187 the value is not affected. For reals, the value might be
1188 affected, so we can't. */
1189 && ((code == PLUS_EXPR && TREE_CODE (in) == MINUS_EXPR)
1190 || (code == MINUS_EXPR && TREE_CODE (in) == PLUS_EXPR))))
1192 tree op0 = TREE_OPERAND (in, 0);
1193 tree op1 = TREE_OPERAND (in, 1);
1194 int neg1_p = TREE_CODE (in) == MINUS_EXPR;
1195 int neg_litp_p = 0, neg_conp_p = 0, neg_var_p = 0;
1197 /* First see if either of the operands is a literal, then a constant. */
1198 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST)
1199 *litp = op0, op0 = 0;
1200 else if (TREE_CODE (op1) == INTEGER_CST || TREE_CODE (op1) == REAL_CST)
1201 *litp = op1, neg_litp_p = neg1_p, op1 = 0;
1203 if (op0 != 0 && TREE_CONSTANT (op0))
1204 *conp = op0, op0 = 0;
1205 else if (op1 != 0 && TREE_CONSTANT (op1))
1206 *conp = op1, neg_conp_p = neg1_p, op1 = 0;
1208 /* If we haven't dealt with either operand, this is not a case we can
1209 decompose. Otherwise, VAR is either of the ones remaining, if any. */
1210 if (op0 != 0 && op1 != 0)
1215 var = op1, neg_var_p = neg1_p;
1217 /* Now do any needed negations. */
1219 *minus_litp = *litp, *litp = 0;
1221 *conp = negate_expr (*conp);
1223 var = negate_expr (var);
1225 else if (TREE_CONSTANT (in))
1233 *minus_litp = *litp, *litp = 0;
1234 else if (*minus_litp)
1235 *litp = *minus_litp, *minus_litp = 0;
1236 *conp = negate_expr (*conp);
1237 var = negate_expr (var);
1243 /* Re-associate trees split by the above function. T1 and T2 are either
1244 expressions to associate or null. Return the new expression, if any. If
1245 we build an operation, do it in TYPE and with CODE. */
1248 associate_trees (tree t1, tree t2, enum tree_code code, tree type)
1255 /* If either input is CODE, a PLUS_EXPR, or a MINUS_EXPR, don't
1256 try to fold this since we will have infinite recursion. But do
1257 deal with any NEGATE_EXPRs. */
1258 if (TREE_CODE (t1) == code || TREE_CODE (t2) == code
1259 || TREE_CODE (t1) == MINUS_EXPR || TREE_CODE (t2) == MINUS_EXPR)
1261 if (code == PLUS_EXPR)
1263 if (TREE_CODE (t1) == NEGATE_EXPR)
1264 return build2 (MINUS_EXPR, type, fold_convert (type, t2),
1265 fold_convert (type, TREE_OPERAND (t1, 0)));
1266 else if (TREE_CODE (t2) == NEGATE_EXPR)
1267 return build2 (MINUS_EXPR, type, fold_convert (type, t1),
1268 fold_convert (type, TREE_OPERAND (t2, 0)));
1269 else if (integer_zerop (t2))
1270 return fold_convert (type, t1);
1272 else if (code == MINUS_EXPR)
1274 if (integer_zerop (t2))
1275 return fold_convert (type, t1);
1278 return build2 (code, type, fold_convert (type, t1),
1279 fold_convert (type, t2));
1282 return fold (build2 (code, type, fold_convert (type, t1),
1283 fold_convert (type, t2)));
1286 /* Combine two integer constants ARG1 and ARG2 under operation CODE
1287 to produce a new constant.
1289 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1292 int_const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1294 unsigned HOST_WIDE_INT int1l, int2l;
1295 HOST_WIDE_INT int1h, int2h;
1296 unsigned HOST_WIDE_INT low;
1298 unsigned HOST_WIDE_INT garbagel;
1299 HOST_WIDE_INT garbageh;
1301 tree type = TREE_TYPE (arg1);
1302 int uns = TYPE_UNSIGNED (type);
1304 = (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type));
1306 int no_overflow = 0;
1308 int1l = TREE_INT_CST_LOW (arg1);
1309 int1h = TREE_INT_CST_HIGH (arg1);
1310 int2l = TREE_INT_CST_LOW (arg2);
1311 int2h = TREE_INT_CST_HIGH (arg2);
1316 low = int1l | int2l, hi = int1h | int2h;
1320 low = int1l ^ int2l, hi = int1h ^ int2h;
1324 low = int1l & int2l, hi = int1h & int2h;
1330 /* It's unclear from the C standard whether shifts can overflow.
1331 The following code ignores overflow; perhaps a C standard
1332 interpretation ruling is needed. */
1333 lshift_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1341 lrotate_double (int1l, int1h, int2l, TYPE_PRECISION (type),
1346 overflow = add_double (int1l, int1h, int2l, int2h, &low, &hi);
1350 neg_double (int2l, int2h, &low, &hi);
1351 add_double (int1l, int1h, low, hi, &low, &hi);
1352 overflow = OVERFLOW_SUM_SIGN (hi, int2h, int1h);
1356 overflow = mul_double (int1l, int1h, int2l, int2h, &low, &hi);
1359 case TRUNC_DIV_EXPR:
1360 case FLOOR_DIV_EXPR: case CEIL_DIV_EXPR:
1361 case EXACT_DIV_EXPR:
1362 /* This is a shortcut for a common special case. */
1363 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1364 && ! TREE_CONSTANT_OVERFLOW (arg1)
1365 && ! TREE_CONSTANT_OVERFLOW (arg2)
1366 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1368 if (code == CEIL_DIV_EXPR)
1371 low = int1l / int2l, hi = 0;
1375 /* ... fall through ... */
1377 case ROUND_DIV_EXPR:
1378 if (int2h == 0 && int2l == 1)
1380 low = int1l, hi = int1h;
1383 if (int1l == int2l && int1h == int2h
1384 && ! (int1l == 0 && int1h == 0))
1389 overflow = div_and_round_double (code, uns, int1l, int1h, int2l, int2h,
1390 &low, &hi, &garbagel, &garbageh);
1393 case TRUNC_MOD_EXPR:
1394 case FLOOR_MOD_EXPR: case CEIL_MOD_EXPR:
1395 /* This is a shortcut for a common special case. */
1396 if (int2h == 0 && (HOST_WIDE_INT) int2l > 0
1397 && ! TREE_CONSTANT_OVERFLOW (arg1)
1398 && ! TREE_CONSTANT_OVERFLOW (arg2)
1399 && int1h == 0 && (HOST_WIDE_INT) int1l >= 0)
1401 if (code == CEIL_MOD_EXPR)
1403 low = int1l % int2l, hi = 0;
1407 /* ... fall through ... */
1409 case ROUND_MOD_EXPR:
1410 overflow = div_and_round_double (code, uns,
1411 int1l, int1h, int2l, int2h,
1412 &garbagel, &garbageh, &low, &hi);
1418 low = (((unsigned HOST_WIDE_INT) int1h
1419 < (unsigned HOST_WIDE_INT) int2h)
1420 || (((unsigned HOST_WIDE_INT) int1h
1421 == (unsigned HOST_WIDE_INT) int2h)
1424 low = (int1h < int2h
1425 || (int1h == int2h && int1l < int2l));
1427 if (low == (code == MIN_EXPR))
1428 low = int1l, hi = int1h;
1430 low = int2l, hi = int2h;
1437 t = build_int_cst_wide (TREE_TYPE (arg1), low, hi);
1441 /* Propagate overflow flags ourselves. */
1442 if (((!uns || is_sizetype) && overflow)
1443 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2))
1446 TREE_OVERFLOW (t) = 1;
1447 TREE_CONSTANT_OVERFLOW (t) = 1;
1449 else if (TREE_CONSTANT_OVERFLOW (arg1) | TREE_CONSTANT_OVERFLOW (arg2))
1452 TREE_CONSTANT_OVERFLOW (t) = 1;
1456 t = force_fit_type (t, 1,
1457 ((!uns || is_sizetype) && overflow)
1458 | TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2),
1459 TREE_CONSTANT_OVERFLOW (arg1)
1460 | TREE_CONSTANT_OVERFLOW (arg2));
1465 /* Combine two constants ARG1 and ARG2 under operation CODE to produce a new
1466 constant. We assume ARG1 and ARG2 have the same data type, or at least
1467 are the same kind of constant and the same machine mode.
1469 If NOTRUNC is nonzero, do not truncate the result to fit the data type. */
1472 const_binop (enum tree_code code, tree arg1, tree arg2, int notrunc)
1477 if (TREE_CODE (arg1) == INTEGER_CST)
1478 return int_const_binop (code, arg1, arg2, notrunc);
1480 if (TREE_CODE (arg1) == REAL_CST)
1482 enum machine_mode mode;
1485 REAL_VALUE_TYPE value;
1486 REAL_VALUE_TYPE result;
1490 d1 = TREE_REAL_CST (arg1);
1491 d2 = TREE_REAL_CST (arg2);
1493 type = TREE_TYPE (arg1);
1494 mode = TYPE_MODE (type);
1496 /* Don't perform operation if we honor signaling NaNs and
1497 either operand is a NaN. */
1498 if (HONOR_SNANS (mode)
1499 && (REAL_VALUE_ISNAN (d1) || REAL_VALUE_ISNAN (d2)))
1502 /* Don't perform operation if it would raise a division
1503 by zero exception. */
1504 if (code == RDIV_EXPR
1505 && REAL_VALUES_EQUAL (d2, dconst0)
1506 && (flag_trapping_math || ! MODE_HAS_INFINITIES (mode)))
1509 /* If either operand is a NaN, just return it. Otherwise, set up
1510 for floating-point trap; we return an overflow. */
1511 if (REAL_VALUE_ISNAN (d1))
1513 else if (REAL_VALUE_ISNAN (d2))
1516 inexact = real_arithmetic (&value, code, &d1, &d2);
1517 real_convert (&result, mode, &value);
1519 /* Don't constant fold this floating point operation if the
1520 result may dependent upon the run-time rounding mode and
1521 flag_rounding_math is set, or if GCC's software emulation
1522 is unable to accurately represent the result. */
1524 if ((flag_rounding_math
1525 || (REAL_MODE_FORMAT_COMPOSITE_P (mode)
1526 && !flag_unsafe_math_optimizations))
1527 && (inexact || !real_identical (&result, &value)))
1530 t = build_real (type, result);
1532 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1) | TREE_OVERFLOW (arg2);
1533 TREE_CONSTANT_OVERFLOW (t)
1535 | TREE_CONSTANT_OVERFLOW (arg1)
1536 | TREE_CONSTANT_OVERFLOW (arg2);
1539 if (TREE_CODE (arg1) == COMPLEX_CST)
1541 tree type = TREE_TYPE (arg1);
1542 tree r1 = TREE_REALPART (arg1);
1543 tree i1 = TREE_IMAGPART (arg1);
1544 tree r2 = TREE_REALPART (arg2);
1545 tree i2 = TREE_IMAGPART (arg2);
1551 t = build_complex (type,
1552 const_binop (PLUS_EXPR, r1, r2, notrunc),
1553 const_binop (PLUS_EXPR, i1, i2, notrunc));
1557 t = build_complex (type,
1558 const_binop (MINUS_EXPR, r1, r2, notrunc),
1559 const_binop (MINUS_EXPR, i1, i2, notrunc));
1563 t = build_complex (type,
1564 const_binop (MINUS_EXPR,
1565 const_binop (MULT_EXPR,
1567 const_binop (MULT_EXPR,
1570 const_binop (PLUS_EXPR,
1571 const_binop (MULT_EXPR,
1573 const_binop (MULT_EXPR,
1581 = const_binop (PLUS_EXPR,
1582 const_binop (MULT_EXPR, r2, r2, notrunc),
1583 const_binop (MULT_EXPR, i2, i2, notrunc),
1586 t = build_complex (type,
1588 (INTEGRAL_TYPE_P (TREE_TYPE (r1))
1589 ? TRUNC_DIV_EXPR : RDIV_EXPR,
1590 const_binop (PLUS_EXPR,
1591 const_binop (MULT_EXPR, r1, r2,
1593 const_binop (MULT_EXPR, i1, i2,
1596 magsquared, notrunc),
1598 (INTEGRAL_TYPE_P (TREE_TYPE (r1))
1599 ? TRUNC_DIV_EXPR : RDIV_EXPR,
1600 const_binop (MINUS_EXPR,
1601 const_binop (MULT_EXPR, i1, r2,
1603 const_binop (MULT_EXPR, r1, i2,
1606 magsquared, notrunc));
1618 /* Create a size type INT_CST node with NUMBER sign extended. KIND
1619 indicates which particular sizetype to create. */
1622 size_int_kind (HOST_WIDE_INT number, enum size_type_kind kind)
1624 return build_int_cst (sizetype_tab[(int) kind], number);
1627 /* Combine operands OP1 and OP2 with arithmetic operation CODE. CODE
1628 is a tree code. The type of the result is taken from the operands.
1629 Both must be the same type integer type and it must be a size type.
1630 If the operands are constant, so is the result. */
1633 size_binop (enum tree_code code, tree arg0, tree arg1)
1635 tree type = TREE_TYPE (arg0);
1637 gcc_assert (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type)
1638 && type == TREE_TYPE (arg1));
1640 /* Handle the special case of two integer constants faster. */
1641 if (TREE_CODE (arg0) == INTEGER_CST && TREE_CODE (arg1) == INTEGER_CST)
1643 /* And some specific cases even faster than that. */
1644 if (code == PLUS_EXPR && integer_zerop (arg0))
1646 else if ((code == MINUS_EXPR || code == PLUS_EXPR)
1647 && integer_zerop (arg1))
1649 else if (code == MULT_EXPR && integer_onep (arg0))
1652 /* Handle general case of two integer constants. */
1653 return int_const_binop (code, arg0, arg1, 0);
1656 if (arg0 == error_mark_node || arg1 == error_mark_node)
1657 return error_mark_node;
1659 return fold (build2 (code, type, arg0, arg1));
1662 /* Given two values, either both of sizetype or both of bitsizetype,
1663 compute the difference between the two values. Return the value
1664 in signed type corresponding to the type of the operands. */
1667 size_diffop (tree arg0, tree arg1)
1669 tree type = TREE_TYPE (arg0);
1672 gcc_assert (TREE_CODE (type) == INTEGER_TYPE && TYPE_IS_SIZETYPE (type)
1673 && type == TREE_TYPE (arg1));
1675 /* If the type is already signed, just do the simple thing. */
1676 if (!TYPE_UNSIGNED (type))
1677 return size_binop (MINUS_EXPR, arg0, arg1);
1679 ctype = type == bitsizetype ? sbitsizetype : ssizetype;
1681 /* If either operand is not a constant, do the conversions to the signed
1682 type and subtract. The hardware will do the right thing with any
1683 overflow in the subtraction. */
1684 if (TREE_CODE (arg0) != INTEGER_CST || TREE_CODE (arg1) != INTEGER_CST)
1685 return size_binop (MINUS_EXPR, fold_convert (ctype, arg0),
1686 fold_convert (ctype, arg1));
1688 /* If ARG0 is larger than ARG1, subtract and return the result in CTYPE.
1689 Otherwise, subtract the other way, convert to CTYPE (we know that can't
1690 overflow) and negate (which can't either). Special-case a result
1691 of zero while we're here. */
1692 if (tree_int_cst_equal (arg0, arg1))
1693 return fold_convert (ctype, integer_zero_node);
1694 else if (tree_int_cst_lt (arg1, arg0))
1695 return fold_convert (ctype, size_binop (MINUS_EXPR, arg0, arg1));
1697 return size_binop (MINUS_EXPR, fold_convert (ctype, integer_zero_node),
1698 fold_convert (ctype, size_binop (MINUS_EXPR,
1702 /* A subroutine of fold_convert_const handling conversions of an
1703 INTEGER_CST to another integer type. */
1706 fold_convert_const_int_from_int (tree type, tree arg1)
1710 /* Given an integer constant, make new constant with new type,
1711 appropriately sign-extended or truncated. */
1712 t = build_int_cst_wide (type, TREE_INT_CST_LOW (arg1),
1713 TREE_INT_CST_HIGH (arg1));
1715 t = force_fit_type (t,
1716 /* Don't set the overflow when
1717 converting a pointer */
1718 !POINTER_TYPE_P (TREE_TYPE (arg1)),
1719 (TREE_INT_CST_HIGH (arg1) < 0
1720 && (TYPE_UNSIGNED (type)
1721 < TYPE_UNSIGNED (TREE_TYPE (arg1))))
1722 | TREE_OVERFLOW (arg1),
1723 TREE_CONSTANT_OVERFLOW (arg1));
1728 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1729 to an integer type. */
1732 fold_convert_const_int_from_real (enum tree_code code, tree type, tree arg1)
1737 /* The following code implements the floating point to integer
1738 conversion rules required by the Java Language Specification,
1739 that IEEE NaNs are mapped to zero and values that overflow
1740 the target precision saturate, i.e. values greater than
1741 INT_MAX are mapped to INT_MAX, and values less than INT_MIN
1742 are mapped to INT_MIN. These semantics are allowed by the
1743 C and C++ standards that simply state that the behavior of
1744 FP-to-integer conversion is unspecified upon overflow. */
1746 HOST_WIDE_INT high, low;
1748 REAL_VALUE_TYPE x = TREE_REAL_CST (arg1);
1752 case FIX_TRUNC_EXPR:
1753 real_trunc (&r, VOIDmode, &x);
1757 real_ceil (&r, VOIDmode, &x);
1760 case FIX_FLOOR_EXPR:
1761 real_floor (&r, VOIDmode, &x);
1764 case FIX_ROUND_EXPR:
1765 real_round (&r, VOIDmode, &x);
1772 /* If R is NaN, return zero and show we have an overflow. */
1773 if (REAL_VALUE_ISNAN (r))
1780 /* See if R is less than the lower bound or greater than the
1785 tree lt = TYPE_MIN_VALUE (type);
1786 REAL_VALUE_TYPE l = real_value_from_int_cst (NULL_TREE, lt);
1787 if (REAL_VALUES_LESS (r, l))
1790 high = TREE_INT_CST_HIGH (lt);
1791 low = TREE_INT_CST_LOW (lt);
1797 tree ut = TYPE_MAX_VALUE (type);
1800 REAL_VALUE_TYPE u = real_value_from_int_cst (NULL_TREE, ut);
1801 if (REAL_VALUES_LESS (u, r))
1804 high = TREE_INT_CST_HIGH (ut);
1805 low = TREE_INT_CST_LOW (ut);
1811 REAL_VALUE_TO_INT (&low, &high, r);
1813 t = build_int_cst_wide (type, low, high);
1815 t = force_fit_type (t, -1, overflow | TREE_OVERFLOW (arg1),
1816 TREE_CONSTANT_OVERFLOW (arg1));
1820 /* A subroutine of fold_convert_const handling conversions a REAL_CST
1821 to another floating point type. */
1824 fold_convert_const_real_from_real (tree type, tree arg1)
1826 REAL_VALUE_TYPE value;
1829 real_convert (&value, TYPE_MODE (type), &TREE_REAL_CST (arg1));
1830 t = build_real (type, value);
1832 TREE_OVERFLOW (t) = TREE_OVERFLOW (arg1);
1833 TREE_CONSTANT_OVERFLOW (t)
1834 = TREE_OVERFLOW (t) | TREE_CONSTANT_OVERFLOW (arg1);
1838 /* Attempt to fold type conversion operation CODE of expression ARG1 to
1839 type TYPE. If no simplification can be done return NULL_TREE. */
1842 fold_convert_const (enum tree_code code, tree type, tree arg1)
1844 if (TREE_TYPE (arg1) == type)
1847 if (POINTER_TYPE_P (type) || INTEGRAL_TYPE_P (type))
1849 if (TREE_CODE (arg1) == INTEGER_CST)
1850 return fold_convert_const_int_from_int (type, arg1);
1851 else if (TREE_CODE (arg1) == REAL_CST)
1852 return fold_convert_const_int_from_real (code, type, arg1);
1854 else if (TREE_CODE (type) == REAL_TYPE)
1856 if (TREE_CODE (arg1) == INTEGER_CST)
1857 return build_real_from_int_cst (type, arg1);
1858 if (TREE_CODE (arg1) == REAL_CST)
1859 return fold_convert_const_real_from_real (type, arg1);
1864 /* Construct a vector of zero elements of vector type TYPE. */
1867 build_zero_vector (tree type)
1872 elem = fold_convert_const (NOP_EXPR, TREE_TYPE (type), integer_zero_node);
1873 units = TYPE_VECTOR_SUBPARTS (type);
1876 for (i = 0; i < units; i++)
1877 list = tree_cons (NULL_TREE, elem, list);
1878 return build_vector (type, list);
1881 /* Convert expression ARG to type TYPE. Used by the middle-end for
1882 simple conversions in preference to calling the front-end's convert. */
1885 fold_convert (tree type, tree arg)
1887 tree orig = TREE_TYPE (arg);
1893 if (TREE_CODE (arg) == ERROR_MARK
1894 || TREE_CODE (type) == ERROR_MARK
1895 || TREE_CODE (orig) == ERROR_MARK)
1896 return error_mark_node;
1898 if (TYPE_MAIN_VARIANT (type) == TYPE_MAIN_VARIANT (orig)
1899 || lang_hooks.types_compatible_p (TYPE_MAIN_VARIANT (type),
1900 TYPE_MAIN_VARIANT (orig)))
1901 return fold (build1 (NOP_EXPR, type, arg));
1903 switch (TREE_CODE (type))
1905 case INTEGER_TYPE: case CHAR_TYPE: case ENUMERAL_TYPE: case BOOLEAN_TYPE:
1906 case POINTER_TYPE: case REFERENCE_TYPE:
1908 if (TREE_CODE (arg) == INTEGER_CST)
1910 tem = fold_convert_const (NOP_EXPR, type, arg);
1911 if (tem != NULL_TREE)
1914 if (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1915 || TREE_CODE (orig) == OFFSET_TYPE)
1916 return fold (build1 (NOP_EXPR, type, arg));
1917 if (TREE_CODE (orig) == COMPLEX_TYPE)
1919 tem = fold (build1 (REALPART_EXPR, TREE_TYPE (orig), arg));
1920 return fold_convert (type, tem);
1922 gcc_assert (TREE_CODE (orig) == VECTOR_TYPE
1923 && tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
1924 return fold (build1 (NOP_EXPR, type, arg));
1927 if (TREE_CODE (arg) == INTEGER_CST)
1929 tem = fold_convert_const (FLOAT_EXPR, type, arg);
1930 if (tem != NULL_TREE)
1933 else if (TREE_CODE (arg) == REAL_CST)
1935 tem = fold_convert_const (NOP_EXPR, type, arg);
1936 if (tem != NULL_TREE)
1940 switch (TREE_CODE (orig))
1942 case INTEGER_TYPE: case CHAR_TYPE:
1943 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
1944 case POINTER_TYPE: case REFERENCE_TYPE:
1945 return fold (build1 (FLOAT_EXPR, type, arg));
1948 return fold (build1 (flag_float_store ? CONVERT_EXPR : NOP_EXPR,
1952 tem = fold (build1 (REALPART_EXPR, TREE_TYPE (orig), arg));
1953 return fold_convert (type, tem);
1960 switch (TREE_CODE (orig))
1962 case INTEGER_TYPE: case CHAR_TYPE:
1963 case BOOLEAN_TYPE: case ENUMERAL_TYPE:
1964 case POINTER_TYPE: case REFERENCE_TYPE:
1966 return build2 (COMPLEX_EXPR, type,
1967 fold_convert (TREE_TYPE (type), arg),
1968 fold_convert (TREE_TYPE (type), integer_zero_node));
1973 if (TREE_CODE (arg) == COMPLEX_EXPR)
1975 rpart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 0));
1976 ipart = fold_convert (TREE_TYPE (type), TREE_OPERAND (arg, 1));
1977 return fold (build2 (COMPLEX_EXPR, type, rpart, ipart));
1980 arg = save_expr (arg);
1981 rpart = fold (build1 (REALPART_EXPR, TREE_TYPE (orig), arg));
1982 ipart = fold (build1 (IMAGPART_EXPR, TREE_TYPE (orig), arg));
1983 rpart = fold_convert (TREE_TYPE (type), rpart);
1984 ipart = fold_convert (TREE_TYPE (type), ipart);
1985 return fold (build2 (COMPLEX_EXPR, type, rpart, ipart));
1993 if (integer_zerop (arg))
1994 return build_zero_vector (type);
1995 gcc_assert (tree_int_cst_equal (TYPE_SIZE (type), TYPE_SIZE (orig)));
1996 gcc_assert (INTEGRAL_TYPE_P (orig) || POINTER_TYPE_P (orig)
1997 || TREE_CODE (orig) == VECTOR_TYPE);
1998 return fold (build1 (NOP_EXPR, type, arg));
2001 return fold (build1 (CONVERT_EXPR, type, fold_ignored_result (arg)));
2008 /* Return an expr equal to X but certainly not valid as an lvalue. */
2013 /* While we are in GIMPLE, NON_LVALUE_EXPR doesn't mean anything to
2018 /* We only need to wrap lvalue tree codes. */
2019 switch (TREE_CODE (x))
2030 case ALIGN_INDIRECT_REF:
2031 case MISALIGNED_INDIRECT_REF:
2033 case ARRAY_RANGE_REF:
2039 case PREINCREMENT_EXPR:
2040 case PREDECREMENT_EXPR:
2042 case TRY_CATCH_EXPR:
2043 case WITH_CLEANUP_EXPR:
2054 /* Assume the worst for front-end tree codes. */
2055 if ((int)TREE_CODE (x) >= NUM_TREE_CODES)
2059 return build1 (NON_LVALUE_EXPR, TREE_TYPE (x), x);
2062 /* Nonzero means lvalues are limited to those valid in pedantic ANSI C.
2063 Zero means allow extended lvalues. */
2065 int pedantic_lvalues;
2067 /* When pedantic, return an expr equal to X but certainly not valid as a
2068 pedantic lvalue. Otherwise, return X. */
2071 pedantic_non_lvalue (tree x)
2073 if (pedantic_lvalues)
2074 return non_lvalue (x);
2079 /* Given a tree comparison code, return the code that is the logical inverse
2080 of the given code. It is not safe to do this for floating-point
2081 comparisons, except for NE_EXPR and EQ_EXPR, so we receive a machine mode
2082 as well: if reversing the comparison is unsafe, return ERROR_MARK. */
2084 static enum tree_code
2085 invert_tree_comparison (enum tree_code code, bool honor_nans)
2087 if (honor_nans && flag_trapping_math)
2097 return honor_nans ? UNLE_EXPR : LE_EXPR;
2099 return honor_nans ? UNLT_EXPR : LT_EXPR;
2101 return honor_nans ? UNGE_EXPR : GE_EXPR;
2103 return honor_nans ? UNGT_EXPR : GT_EXPR;
2117 return UNORDERED_EXPR;
2118 case UNORDERED_EXPR:
2119 return ORDERED_EXPR;
2125 /* Similar, but return the comparison that results if the operands are
2126 swapped. This is safe for floating-point. */
2129 swap_tree_comparison (enum tree_code code)
2150 /* Convert a comparison tree code from an enum tree_code representation
2151 into a compcode bit-based encoding. This function is the inverse of
2152 compcode_to_comparison. */
2154 static enum comparison_code
2155 comparison_to_compcode (enum tree_code code)
2172 return COMPCODE_ORD;
2173 case UNORDERED_EXPR:
2174 return COMPCODE_UNORD;
2176 return COMPCODE_UNLT;
2178 return COMPCODE_UNEQ;
2180 return COMPCODE_UNLE;
2182 return COMPCODE_UNGT;
2184 return COMPCODE_LTGT;
2186 return COMPCODE_UNGE;
2192 /* Convert a compcode bit-based encoding of a comparison operator back
2193 to GCC's enum tree_code representation. This function is the
2194 inverse of comparison_to_compcode. */
2196 static enum tree_code
2197 compcode_to_comparison (enum comparison_code code)
2214 return ORDERED_EXPR;
2215 case COMPCODE_UNORD:
2216 return UNORDERED_EXPR;
2234 /* Return a tree for the comparison which is the combination of
2235 doing the AND or OR (depending on CODE) of the two operations LCODE
2236 and RCODE on the identical operands LL_ARG and LR_ARG. Take into account
2237 the possibility of trapping if the mode has NaNs, and return NULL_TREE
2238 if this makes the transformation invalid. */
2241 combine_comparisons (enum tree_code code, enum tree_code lcode,
2242 enum tree_code rcode, tree truth_type,
2243 tree ll_arg, tree lr_arg)
2245 bool honor_nans = HONOR_NANS (TYPE_MODE (TREE_TYPE (ll_arg)));
2246 enum comparison_code lcompcode = comparison_to_compcode (lcode);
2247 enum comparison_code rcompcode = comparison_to_compcode (rcode);
2248 enum comparison_code compcode;
2252 case TRUTH_AND_EXPR: case TRUTH_ANDIF_EXPR:
2253 compcode = lcompcode & rcompcode;
2256 case TRUTH_OR_EXPR: case TRUTH_ORIF_EXPR:
2257 compcode = lcompcode | rcompcode;
2266 /* Eliminate unordered comparisons, as well as LTGT and ORD
2267 which are not used unless the mode has NaNs. */
2268 compcode &= ~COMPCODE_UNORD;
2269 if (compcode == COMPCODE_LTGT)
2270 compcode = COMPCODE_NE;
2271 else if (compcode == COMPCODE_ORD)
2272 compcode = COMPCODE_TRUE;
2274 else if (flag_trapping_math)
2276 /* Check that the original operation and the optimized ones will trap
2277 under the same condition. */
2278 bool ltrap = (lcompcode & COMPCODE_UNORD) == 0
2279 && (lcompcode != COMPCODE_EQ)
2280 && (lcompcode != COMPCODE_ORD);
2281 bool rtrap = (rcompcode & COMPCODE_UNORD) == 0
2282 && (rcompcode != COMPCODE_EQ)
2283 && (rcompcode != COMPCODE_ORD);
2284 bool trap = (compcode & COMPCODE_UNORD) == 0
2285 && (compcode != COMPCODE_EQ)
2286 && (compcode != COMPCODE_ORD);
2288 /* In a short-circuited boolean expression the LHS might be
2289 such that the RHS, if evaluated, will never trap. For
2290 example, in ORD (x, y) && (x < y), we evaluate the RHS only
2291 if neither x nor y is NaN. (This is a mixed blessing: for
2292 example, the expression above will never trap, hence
2293 optimizing it to x < y would be invalid). */
2294 if ((code == TRUTH_ORIF_EXPR && (lcompcode & COMPCODE_UNORD))
2295 || (code == TRUTH_ANDIF_EXPR && !(lcompcode & COMPCODE_UNORD)))
2298 /* If the comparison was short-circuited, and only the RHS
2299 trapped, we may now generate a spurious trap. */
2301 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2304 /* If we changed the conditions that cause a trap, we lose. */
2305 if ((ltrap || rtrap) != trap)
2309 if (compcode == COMPCODE_TRUE)
2310 return constant_boolean_node (true, truth_type);
2311 else if (compcode == COMPCODE_FALSE)
2312 return constant_boolean_node (false, truth_type);
2314 return fold (build2 (compcode_to_comparison (compcode),
2315 truth_type, ll_arg, lr_arg));
2318 /* Return nonzero if CODE is a tree code that represents a truth value. */
2321 truth_value_p (enum tree_code code)
2323 return (TREE_CODE_CLASS (code) == tcc_comparison
2324 || code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR
2325 || code == TRUTH_OR_EXPR || code == TRUTH_ORIF_EXPR
2326 || code == TRUTH_XOR_EXPR || code == TRUTH_NOT_EXPR);
2329 /* Return nonzero if two operands (typically of the same tree node)
2330 are necessarily equal. If either argument has side-effects this
2331 function returns zero. FLAGS modifies behavior as follows:
2333 If OEP_ONLY_CONST is set, only return nonzero for constants.
2334 This function tests whether the operands are indistinguishable;
2335 it does not test whether they are equal using C's == operation.
2336 The distinction is important for IEEE floating point, because
2337 (1) -0.0 and 0.0 are distinguishable, but -0.0==0.0, and
2338 (2) two NaNs may be indistinguishable, but NaN!=NaN.
2340 If OEP_ONLY_CONST is unset, a VAR_DECL is considered equal to itself
2341 even though it may hold multiple values during a function.
2342 This is because a GCC tree node guarantees that nothing else is
2343 executed between the evaluation of its "operands" (which may often
2344 be evaluated in arbitrary order). Hence if the operands themselves
2345 don't side-effect, the VAR_DECLs, PARM_DECLs etc... must hold the
2346 same value in each operand/subexpression. Hence leaving OEP_ONLY_CONST
2347 unset means assuming isochronic (or instantaneous) tree equivalence.
2348 Unless comparing arbitrary expression trees, such as from different
2349 statements, this flag can usually be left unset.
2351 If OEP_PURE_SAME is set, then pure functions with identical arguments
2352 are considered the same. It is used when the caller has other ways
2353 to ensure that global memory is unchanged in between. */
2356 operand_equal_p (tree arg0, tree arg1, unsigned int flags)
2358 /* If either is ERROR_MARK, they aren't equal. */
2359 if (TREE_CODE (arg0) == ERROR_MARK || TREE_CODE (arg1) == ERROR_MARK)
2362 /* If both types don't have the same signedness, then we can't consider
2363 them equal. We must check this before the STRIP_NOPS calls
2364 because they may change the signedness of the arguments. */
2365 if (TYPE_UNSIGNED (TREE_TYPE (arg0)) != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2371 if (TREE_CODE (arg0) != TREE_CODE (arg1)
2372 /* This is needed for conversions and for COMPONENT_REF.
2373 Might as well play it safe and always test this. */
2374 || TREE_CODE (TREE_TYPE (arg0)) == ERROR_MARK
2375 || TREE_CODE (TREE_TYPE (arg1)) == ERROR_MARK
2376 || TYPE_MODE (TREE_TYPE (arg0)) != TYPE_MODE (TREE_TYPE (arg1)))
2379 /* If ARG0 and ARG1 are the same SAVE_EXPR, they are necessarily equal.
2380 We don't care about side effects in that case because the SAVE_EXPR
2381 takes care of that for us. In all other cases, two expressions are
2382 equal if they have no side effects. If we have two identical
2383 expressions with side effects that should be treated the same due
2384 to the only side effects being identical SAVE_EXPR's, that will
2385 be detected in the recursive calls below. */
2386 if (arg0 == arg1 && ! (flags & OEP_ONLY_CONST)
2387 && (TREE_CODE (arg0) == SAVE_EXPR
2388 || (! TREE_SIDE_EFFECTS (arg0) && ! TREE_SIDE_EFFECTS (arg1))))
2391 /* Next handle constant cases, those for which we can return 1 even
2392 if ONLY_CONST is set. */
2393 if (TREE_CONSTANT (arg0) && TREE_CONSTANT (arg1))
2394 switch (TREE_CODE (arg0))
2397 return (! TREE_CONSTANT_OVERFLOW (arg0)
2398 && ! TREE_CONSTANT_OVERFLOW (arg1)
2399 && tree_int_cst_equal (arg0, arg1));
2402 return (! TREE_CONSTANT_OVERFLOW (arg0)
2403 && ! TREE_CONSTANT_OVERFLOW (arg1)
2404 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (arg0),
2405 TREE_REAL_CST (arg1)));
2411 if (TREE_CONSTANT_OVERFLOW (arg0)
2412 || TREE_CONSTANT_OVERFLOW (arg1))
2415 v1 = TREE_VECTOR_CST_ELTS (arg0);
2416 v2 = TREE_VECTOR_CST_ELTS (arg1);
2419 if (!operand_equal_p (TREE_VALUE (v1), TREE_VALUE (v2),
2422 v1 = TREE_CHAIN (v1);
2423 v2 = TREE_CHAIN (v2);
2430 return (operand_equal_p (TREE_REALPART (arg0), TREE_REALPART (arg1),
2432 && operand_equal_p (TREE_IMAGPART (arg0), TREE_IMAGPART (arg1),
2436 return (TREE_STRING_LENGTH (arg0) == TREE_STRING_LENGTH (arg1)
2437 && ! memcmp (TREE_STRING_POINTER (arg0),
2438 TREE_STRING_POINTER (arg1),
2439 TREE_STRING_LENGTH (arg0)));
2442 return operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0),
2448 if (flags & OEP_ONLY_CONST)
2451 /* Define macros to test an operand from arg0 and arg1 for equality and a
2452 variant that allows null and views null as being different from any
2453 non-null value. In the latter case, if either is null, the both
2454 must be; otherwise, do the normal comparison. */
2455 #define OP_SAME(N) operand_equal_p (TREE_OPERAND (arg0, N), \
2456 TREE_OPERAND (arg1, N), flags)
2458 #define OP_SAME_WITH_NULL(N) \
2459 ((!TREE_OPERAND (arg0, N) || !TREE_OPERAND (arg1, N)) \
2460 ? TREE_OPERAND (arg0, N) == TREE_OPERAND (arg1, N) : OP_SAME (N))
2462 switch (TREE_CODE_CLASS (TREE_CODE (arg0)))
2465 /* Two conversions are equal only if signedness and modes match. */
2466 switch (TREE_CODE (arg0))
2471 case FIX_TRUNC_EXPR:
2472 case FIX_FLOOR_EXPR:
2473 case FIX_ROUND_EXPR:
2474 if (TYPE_UNSIGNED (TREE_TYPE (arg0))
2475 != TYPE_UNSIGNED (TREE_TYPE (arg1)))
2485 case tcc_comparison:
2487 if (OP_SAME (0) && OP_SAME (1))
2490 /* For commutative ops, allow the other order. */
2491 return (commutative_tree_code (TREE_CODE (arg0))
2492 && operand_equal_p (TREE_OPERAND (arg0, 0),
2493 TREE_OPERAND (arg1, 1), flags)
2494 && operand_equal_p (TREE_OPERAND (arg0, 1),
2495 TREE_OPERAND (arg1, 0), flags));
2498 /* If either of the pointer (or reference) expressions we are
2499 dereferencing contain a side effect, these cannot be equal. */
2500 if (TREE_SIDE_EFFECTS (arg0)
2501 || TREE_SIDE_EFFECTS (arg1))
2504 switch (TREE_CODE (arg0))
2507 case ALIGN_INDIRECT_REF:
2508 case MISALIGNED_INDIRECT_REF:
2514 case ARRAY_RANGE_REF:
2515 /* Operands 2 and 3 may be null. */
2518 && OP_SAME_WITH_NULL (2)
2519 && OP_SAME_WITH_NULL (3));
2522 /* Handle operand 2 the same as for ARRAY_REF. */
2523 return OP_SAME (0) && OP_SAME (1) && OP_SAME_WITH_NULL (2);
2526 return OP_SAME (0) && OP_SAME (1) && OP_SAME (2);
2532 case tcc_expression:
2533 switch (TREE_CODE (arg0))
2536 case TRUTH_NOT_EXPR:
2539 case TRUTH_ANDIF_EXPR:
2540 case TRUTH_ORIF_EXPR:
2541 return OP_SAME (0) && OP_SAME (1);
2543 case TRUTH_AND_EXPR:
2545 case TRUTH_XOR_EXPR:
2546 if (OP_SAME (0) && OP_SAME (1))
2549 /* Otherwise take into account this is a commutative operation. */
2550 return (operand_equal_p (TREE_OPERAND (arg0, 0),
2551 TREE_OPERAND (arg1, 1), flags)
2552 && operand_equal_p (TREE_OPERAND (arg0, 1),
2553 TREE_OPERAND (arg1, 0), flags));
2556 /* If the CALL_EXPRs call different functions, then they
2557 clearly can not be equal. */
2562 unsigned int cef = call_expr_flags (arg0);
2563 if (flags & OEP_PURE_SAME)
2564 cef &= ECF_CONST | ECF_PURE;
2571 /* Now see if all the arguments are the same. operand_equal_p
2572 does not handle TREE_LIST, so we walk the operands here
2573 feeding them to operand_equal_p. */
2574 arg0 = TREE_OPERAND (arg0, 1);
2575 arg1 = TREE_OPERAND (arg1, 1);
2576 while (arg0 && arg1)
2578 if (! operand_equal_p (TREE_VALUE (arg0), TREE_VALUE (arg1),
2582 arg0 = TREE_CHAIN (arg0);
2583 arg1 = TREE_CHAIN (arg1);
2586 /* If we get here and both argument lists are exhausted
2587 then the CALL_EXPRs are equal. */
2588 return ! (arg0 || arg1);
2594 case tcc_declaration:
2595 /* Consider __builtin_sqrt equal to sqrt. */
2596 return (TREE_CODE (arg0) == FUNCTION_DECL
2597 && DECL_BUILT_IN (arg0) && DECL_BUILT_IN (arg1)
2598 && DECL_BUILT_IN_CLASS (arg0) == DECL_BUILT_IN_CLASS (arg1)
2599 && DECL_FUNCTION_CODE (arg0) == DECL_FUNCTION_CODE (arg1));
2606 #undef OP_SAME_WITH_NULL
2609 /* Similar to operand_equal_p, but see if ARG0 might have been made by
2610 shorten_compare from ARG1 when ARG1 was being compared with OTHER.
2612 When in doubt, return 0. */
2615 operand_equal_for_comparison_p (tree arg0, tree arg1, tree other)
2617 int unsignedp1, unsignedpo;
2618 tree primarg0, primarg1, primother;
2619 unsigned int correct_width;
2621 if (operand_equal_p (arg0, arg1, 0))
2624 if (! INTEGRAL_TYPE_P (TREE_TYPE (arg0))
2625 || ! INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
2628 /* Discard any conversions that don't change the modes of ARG0 and ARG1
2629 and see if the inner values are the same. This removes any
2630 signedness comparison, which doesn't matter here. */
2631 primarg0 = arg0, primarg1 = arg1;
2632 STRIP_NOPS (primarg0);
2633 STRIP_NOPS (primarg1);
2634 if (operand_equal_p (primarg0, primarg1, 0))
2637 /* Duplicate what shorten_compare does to ARG1 and see if that gives the
2638 actual comparison operand, ARG0.
2640 First throw away any conversions to wider types
2641 already present in the operands. */
2643 primarg1 = get_narrower (arg1, &unsignedp1);
2644 primother = get_narrower (other, &unsignedpo);
2646 correct_width = TYPE_PRECISION (TREE_TYPE (arg1));
2647 if (unsignedp1 == unsignedpo
2648 && TYPE_PRECISION (TREE_TYPE (primarg1)) < correct_width
2649 && TYPE_PRECISION (TREE_TYPE (primother)) < correct_width)
2651 tree type = TREE_TYPE (arg0);
2653 /* Make sure shorter operand is extended the right way
2654 to match the longer operand. */
2655 primarg1 = fold_convert (lang_hooks.types.signed_or_unsigned_type
2656 (unsignedp1, TREE_TYPE (primarg1)), primarg1);
2658 if (operand_equal_p (arg0, fold_convert (type, primarg1), 0))
2665 /* See if ARG is an expression that is either a comparison or is performing
2666 arithmetic on comparisons. The comparisons must only be comparing
2667 two different values, which will be stored in *CVAL1 and *CVAL2; if
2668 they are nonzero it means that some operands have already been found.
2669 No variables may be used anywhere else in the expression except in the
2670 comparisons. If SAVE_P is true it means we removed a SAVE_EXPR around
2671 the expression and save_expr needs to be called with CVAL1 and CVAL2.
2673 If this is true, return 1. Otherwise, return zero. */
2676 twoval_comparison_p (tree arg, tree *cval1, tree *cval2, int *save_p)
2678 enum tree_code code = TREE_CODE (arg);
2679 enum tree_code_class class = TREE_CODE_CLASS (code);
2681 /* We can handle some of the tcc_expression cases here. */
2682 if (class == tcc_expression && code == TRUTH_NOT_EXPR)
2684 else if (class == tcc_expression
2685 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR
2686 || code == COMPOUND_EXPR))
2689 else if (class == tcc_expression && code == SAVE_EXPR
2690 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg, 0)))
2692 /* If we've already found a CVAL1 or CVAL2, this expression is
2693 two complex to handle. */
2694 if (*cval1 || *cval2)
2704 return twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p);
2707 return (twoval_comparison_p (TREE_OPERAND (arg, 0), cval1, cval2, save_p)
2708 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2709 cval1, cval2, save_p));
2714 case tcc_expression:
2715 if (code == COND_EXPR)
2716 return (twoval_comparison_p (TREE_OPERAND (arg, 0),
2717 cval1, cval2, save_p)
2718 && twoval_comparison_p (TREE_OPERAND (arg, 1),
2719 cval1, cval2, save_p)
2720 && twoval_comparison_p (TREE_OPERAND (arg, 2),
2721 cval1, cval2, save_p));
2724 case tcc_comparison:
2725 /* First see if we can handle the first operand, then the second. For
2726 the second operand, we know *CVAL1 can't be zero. It must be that
2727 one side of the comparison is each of the values; test for the
2728 case where this isn't true by failing if the two operands
2731 if (operand_equal_p (TREE_OPERAND (arg, 0),
2732 TREE_OPERAND (arg, 1), 0))
2736 *cval1 = TREE_OPERAND (arg, 0);
2737 else if (operand_equal_p (*cval1, TREE_OPERAND (arg, 0), 0))
2739 else if (*cval2 == 0)
2740 *cval2 = TREE_OPERAND (arg, 0);
2741 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 0), 0))
2746 if (operand_equal_p (*cval1, TREE_OPERAND (arg, 1), 0))
2748 else if (*cval2 == 0)
2749 *cval2 = TREE_OPERAND (arg, 1);
2750 else if (operand_equal_p (*cval2, TREE_OPERAND (arg, 1), 0))
2762 /* ARG is a tree that is known to contain just arithmetic operations and
2763 comparisons. Evaluate the operations in the tree substituting NEW0 for
2764 any occurrence of OLD0 as an operand of a comparison and likewise for
2768 eval_subst (tree arg, tree old0, tree new0, tree old1, tree new1)
2770 tree type = TREE_TYPE (arg);
2771 enum tree_code code = TREE_CODE (arg);
2772 enum tree_code_class class = TREE_CODE_CLASS (code);
2774 /* We can handle some of the tcc_expression cases here. */
2775 if (class == tcc_expression && code == TRUTH_NOT_EXPR)
2777 else if (class == tcc_expression
2778 && (code == TRUTH_ANDIF_EXPR || code == TRUTH_ORIF_EXPR))
2784 return fold (build1 (code, type,
2785 eval_subst (TREE_OPERAND (arg, 0),
2786 old0, new0, old1, new1)));
2789 return fold (build2 (code, type,
2790 eval_subst (TREE_OPERAND (arg, 0),
2791 old0, new0, old1, new1),
2792 eval_subst (TREE_OPERAND (arg, 1),
2793 old0, new0, old1, new1)));
2795 case tcc_expression:
2799 return eval_subst (TREE_OPERAND (arg, 0), old0, new0, old1, new1);
2802 return eval_subst (TREE_OPERAND (arg, 1), old0, new0, old1, new1);
2805 return fold (build3 (code, type,
2806 eval_subst (TREE_OPERAND (arg, 0),
2807 old0, new0, old1, new1),
2808 eval_subst (TREE_OPERAND (arg, 1),
2809 old0, new0, old1, new1),
2810 eval_subst (TREE_OPERAND (arg, 2),
2811 old0, new0, old1, new1)));
2815 /* Fall through - ??? */
2817 case tcc_comparison:
2819 tree arg0 = TREE_OPERAND (arg, 0);
2820 tree arg1 = TREE_OPERAND (arg, 1);
2822 /* We need to check both for exact equality and tree equality. The
2823 former will be true if the operand has a side-effect. In that
2824 case, we know the operand occurred exactly once. */
2826 if (arg0 == old0 || operand_equal_p (arg0, old0, 0))
2828 else if (arg0 == old1 || operand_equal_p (arg0, old1, 0))
2831 if (arg1 == old0 || operand_equal_p (arg1, old0, 0))
2833 else if (arg1 == old1 || operand_equal_p (arg1, old1, 0))
2836 return fold (build2 (code, type, arg0, arg1));
2844 /* Return a tree for the case when the result of an expression is RESULT
2845 converted to TYPE and OMITTED was previously an operand of the expression
2846 but is now not needed (e.g., we folded OMITTED * 0).
2848 If OMITTED has side effects, we must evaluate it. Otherwise, just do
2849 the conversion of RESULT to TYPE. */
2852 omit_one_operand (tree type, tree result, tree omitted)
2854 tree t = fold_convert (type, result);
2856 if (TREE_SIDE_EFFECTS (omitted))
2857 return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
2859 return non_lvalue (t);
2862 /* Similar, but call pedantic_non_lvalue instead of non_lvalue. */
2865 pedantic_omit_one_operand (tree type, tree result, tree omitted)
2867 tree t = fold_convert (type, result);
2869 if (TREE_SIDE_EFFECTS (omitted))
2870 return build2 (COMPOUND_EXPR, type, fold_ignored_result (omitted), t);
2872 return pedantic_non_lvalue (t);
2875 /* Return a tree for the case when the result of an expression is RESULT
2876 converted to TYPE and OMITTED1 and OMITTED2 were previously operands
2877 of the expression but are now not needed.
2879 If OMITTED1 or OMITTED2 has side effects, they must be evaluated.
2880 If both OMITTED1 and OMITTED2 have side effects, OMITTED1 is
2881 evaluated before OMITTED2. Otherwise, if neither has side effects,
2882 just do the conversion of RESULT to TYPE. */
2885 omit_two_operands (tree type, tree result, tree omitted1, tree omitted2)
2887 tree t = fold_convert (type, result);
2889 if (TREE_SIDE_EFFECTS (omitted2))
2890 t = build2 (COMPOUND_EXPR, type, omitted2, t);
2891 if (TREE_SIDE_EFFECTS (omitted1))
2892 t = build2 (COMPOUND_EXPR, type, omitted1, t);
2894 return TREE_CODE (t) != COMPOUND_EXPR ? non_lvalue (t) : t;
2898 /* Return a simplified tree node for the truth-negation of ARG. This
2899 never alters ARG itself. We assume that ARG is an operation that
2900 returns a truth value (0 or 1).
2902 FIXME: one would think we would fold the result, but it causes
2903 problems with the dominator optimizer. */
2905 invert_truthvalue (tree arg)
2907 tree type = TREE_TYPE (arg);
2908 enum tree_code code = TREE_CODE (arg);
2910 if (code == ERROR_MARK)
2913 /* If this is a comparison, we can simply invert it, except for
2914 floating-point non-equality comparisons, in which case we just
2915 enclose a TRUTH_NOT_EXPR around what we have. */
2917 if (TREE_CODE_CLASS (code) == tcc_comparison)
2919 tree op_type = TREE_TYPE (TREE_OPERAND (arg, 0));
2920 if (FLOAT_TYPE_P (op_type)
2921 && flag_trapping_math
2922 && code != ORDERED_EXPR && code != UNORDERED_EXPR
2923 && code != NE_EXPR && code != EQ_EXPR)
2924 return build1 (TRUTH_NOT_EXPR, type, arg);
2927 code = invert_tree_comparison (code,
2928 HONOR_NANS (TYPE_MODE (op_type)));
2929 if (code == ERROR_MARK)
2930 return build1 (TRUTH_NOT_EXPR, type, arg);
2932 return build2 (code, type,
2933 TREE_OPERAND (arg, 0), TREE_OPERAND (arg, 1));
2940 return constant_boolean_node (integer_zerop (arg), type);
2942 case TRUTH_AND_EXPR:
2943 return build2 (TRUTH_OR_EXPR, type,
2944 invert_truthvalue (TREE_OPERAND (arg, 0)),
2945 invert_truthvalue (TREE_OPERAND (arg, 1)));
2948 return build2 (TRUTH_AND_EXPR, type,
2949 invert_truthvalue (TREE_OPERAND (arg, 0)),
2950 invert_truthvalue (TREE_OPERAND (arg, 1)));
2952 case TRUTH_XOR_EXPR:
2953 /* Here we can invert either operand. We invert the first operand
2954 unless the second operand is a TRUTH_NOT_EXPR in which case our
2955 result is the XOR of the first operand with the inside of the
2956 negation of the second operand. */
2958 if (TREE_CODE (TREE_OPERAND (arg, 1)) == TRUTH_NOT_EXPR)
2959 return build2 (TRUTH_XOR_EXPR, type, TREE_OPERAND (arg, 0),
2960 TREE_OPERAND (TREE_OPERAND (arg, 1), 0));
2962 return build2 (TRUTH_XOR_EXPR, type,
2963 invert_truthvalue (TREE_OPERAND (arg, 0)),
2964 TREE_OPERAND (arg, 1));
2966 case TRUTH_ANDIF_EXPR:
2967 return build2 (TRUTH_ORIF_EXPR, type,
2968 invert_truthvalue (TREE_OPERAND (arg, 0)),
2969 invert_truthvalue (TREE_OPERAND (arg, 1)));
2971 case TRUTH_ORIF_EXPR:
2972 return build2 (TRUTH_ANDIF_EXPR, type,
2973 invert_truthvalue (TREE_OPERAND (arg, 0)),
2974 invert_truthvalue (TREE_OPERAND (arg, 1)));
2976 case TRUTH_NOT_EXPR:
2977 return TREE_OPERAND (arg, 0);
2980 return build3 (COND_EXPR, type, TREE_OPERAND (arg, 0),
2981 invert_truthvalue (TREE_OPERAND (arg, 1)),
2982 invert_truthvalue (TREE_OPERAND (arg, 2)));
2985 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg, 0),
2986 invert_truthvalue (TREE_OPERAND (arg, 1)));
2988 case NON_LVALUE_EXPR:
2989 return invert_truthvalue (TREE_OPERAND (arg, 0));
2992 if (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE)
2997 return build1 (TREE_CODE (arg), type,
2998 invert_truthvalue (TREE_OPERAND (arg, 0)));
3001 if (!integer_onep (TREE_OPERAND (arg, 1)))
3003 return build2 (EQ_EXPR, type, arg,
3004 fold_convert (type, integer_zero_node));
3007 return build1 (TRUTH_NOT_EXPR, type, arg);
3009 case CLEANUP_POINT_EXPR:
3010 return build1 (CLEANUP_POINT_EXPR, type,
3011 invert_truthvalue (TREE_OPERAND (arg, 0)));
3016 gcc_assert (TREE_CODE (TREE_TYPE (arg)) == BOOLEAN_TYPE);
3017 return build1 (TRUTH_NOT_EXPR, type, arg);
3020 /* Given a bit-wise operation CODE applied to ARG0 and ARG1, see if both
3021 operands are another bit-wise operation with a common input. If so,
3022 distribute the bit operations to save an operation and possibly two if
3023 constants are involved. For example, convert
3024 (A | B) & (A | C) into A | (B & C)
3025 Further simplification will occur if B and C are constants.
3027 If this optimization cannot be done, 0 will be returned. */
3030 distribute_bit_expr (enum tree_code code, tree type, tree arg0, tree arg1)
3035 if (TREE_CODE (arg0) != TREE_CODE (arg1)
3036 || TREE_CODE (arg0) == code
3037 || (TREE_CODE (arg0) != BIT_AND_EXPR
3038 && TREE_CODE (arg0) != BIT_IOR_EXPR))
3041 if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 0), 0))
3043 common = TREE_OPERAND (arg0, 0);
3044 left = TREE_OPERAND (arg0, 1);
3045 right = TREE_OPERAND (arg1, 1);
3047 else if (operand_equal_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg1, 1), 0))
3049 common = TREE_OPERAND (arg0, 0);
3050 left = TREE_OPERAND (arg0, 1);
3051 right = TREE_OPERAND (arg1, 0);
3053 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 0), 0))
3055 common = TREE_OPERAND (arg0, 1);
3056 left = TREE_OPERAND (arg0, 0);
3057 right = TREE_OPERAND (arg1, 1);
3059 else if (operand_equal_p (TREE_OPERAND (arg0, 1), TREE_OPERAND (arg1, 1), 0))
3061 common = TREE_OPERAND (arg0, 1);
3062 left = TREE_OPERAND (arg0, 0);
3063 right = TREE_OPERAND (arg1, 0);
3068 return fold (build2 (TREE_CODE (arg0), type, common,
3069 fold (build2 (code, type, left, right))));
3072 /* Return a BIT_FIELD_REF of type TYPE to refer to BITSIZE bits of INNER
3073 starting at BITPOS. The field is unsigned if UNSIGNEDP is nonzero. */
3076 make_bit_field_ref (tree inner, tree type, int bitsize, int bitpos,
3083 tree size = TYPE_SIZE (TREE_TYPE (inner));
3084 if ((INTEGRAL_TYPE_P (TREE_TYPE (inner))
3085 || POINTER_TYPE_P (TREE_TYPE (inner)))
3086 && host_integerp (size, 0)
3087 && tree_low_cst (size, 0) == bitsize)
3088 return fold_convert (type, inner);
3091 result = build3 (BIT_FIELD_REF, type, inner,
3092 size_int (bitsize), bitsize_int (bitpos));
3094 BIT_FIELD_REF_UNSIGNED (result) = unsignedp;
3099 /* Optimize a bit-field compare.
3101 There are two cases: First is a compare against a constant and the
3102 second is a comparison of two items where the fields are at the same
3103 bit position relative to the start of a chunk (byte, halfword, word)
3104 large enough to contain it. In these cases we can avoid the shift
3105 implicit in bitfield extractions.
3107 For constants, we emit a compare of the shifted constant with the
3108 BIT_AND_EXPR of a mask and a byte, halfword, or word of the operand being
3109 compared. For two fields at the same position, we do the ANDs with the
3110 similar mask and compare the result of the ANDs.
3112 CODE is the comparison code, known to be either NE_EXPR or EQ_EXPR.
3113 COMPARE_TYPE is the type of the comparison, and LHS and RHS
3114 are the left and right operands of the comparison, respectively.
3116 If the optimization described above can be done, we return the resulting
3117 tree. Otherwise we return zero. */
3120 optimize_bit_field_compare (enum tree_code code, tree compare_type,
3123 HOST_WIDE_INT lbitpos, lbitsize, rbitpos, rbitsize, nbitpos, nbitsize;
3124 tree type = TREE_TYPE (lhs);
3125 tree signed_type, unsigned_type;
3126 int const_p = TREE_CODE (rhs) == INTEGER_CST;
3127 enum machine_mode lmode, rmode, nmode;
3128 int lunsignedp, runsignedp;
3129 int lvolatilep = 0, rvolatilep = 0;
3130 tree linner, rinner = NULL_TREE;
3134 /* Get all the information about the extractions being done. If the bit size
3135 if the same as the size of the underlying object, we aren't doing an
3136 extraction at all and so can do nothing. We also don't want to
3137 do anything if the inner expression is a PLACEHOLDER_EXPR since we
3138 then will no longer be able to replace it. */
3139 linner = get_inner_reference (lhs, &lbitsize, &lbitpos, &offset, &lmode,
3140 &lunsignedp, &lvolatilep, false);
3141 if (linner == lhs || lbitsize == GET_MODE_BITSIZE (lmode) || lbitsize < 0
3142 || offset != 0 || TREE_CODE (linner) == PLACEHOLDER_EXPR)
3147 /* If this is not a constant, we can only do something if bit positions,
3148 sizes, and signedness are the same. */
3149 rinner = get_inner_reference (rhs, &rbitsize, &rbitpos, &offset, &rmode,
3150 &runsignedp, &rvolatilep, false);
3152 if (rinner == rhs || lbitpos != rbitpos || lbitsize != rbitsize
3153 || lunsignedp != runsignedp || offset != 0
3154 || TREE_CODE (rinner) == PLACEHOLDER_EXPR)
3158 /* See if we can find a mode to refer to this field. We should be able to,
3159 but fail if we can't. */
3160 nmode = get_best_mode (lbitsize, lbitpos,
3161 const_p ? TYPE_ALIGN (TREE_TYPE (linner))
3162 : MIN (TYPE_ALIGN (TREE_TYPE (linner)),
3163 TYPE_ALIGN (TREE_TYPE (rinner))),
3164 word_mode, lvolatilep || rvolatilep);
3165 if (nmode == VOIDmode)
3168 /* Set signed and unsigned types of the precision of this mode for the
3170 signed_type = lang_hooks.types.type_for_mode (nmode, 0);
3171 unsigned_type = lang_hooks.types.type_for_mode (nmode, 1);
3173 /* Compute the bit position and size for the new reference and our offset
3174 within it. If the new reference is the same size as the original, we
3175 won't optimize anything, so return zero. */
3176 nbitsize = GET_MODE_BITSIZE (nmode);
3177 nbitpos = lbitpos & ~ (nbitsize - 1);
3179 if (nbitsize == lbitsize)
3182 if (BYTES_BIG_ENDIAN)
3183 lbitpos = nbitsize - lbitsize - lbitpos;
3185 /* Make the mask to be used against the extracted field. */
3186 mask = build_int_cst (unsigned_type, -1);
3187 mask = force_fit_type (mask, 0, false, false);
3188 mask = fold_convert (unsigned_type, mask);
3189 mask = const_binop (LSHIFT_EXPR, mask, size_int (nbitsize - lbitsize), 0);
3190 mask = const_binop (RSHIFT_EXPR, mask,
3191 size_int (nbitsize - lbitsize - lbitpos), 0);
3194 /* If not comparing with constant, just rework the comparison
3196 return build2 (code, compare_type,
3197 build2 (BIT_AND_EXPR, unsigned_type,
3198 make_bit_field_ref (linner, unsigned_type,
3199 nbitsize, nbitpos, 1),
3201 build2 (BIT_AND_EXPR, unsigned_type,
3202 make_bit_field_ref (rinner, unsigned_type,
3203 nbitsize, nbitpos, 1),
3206 /* Otherwise, we are handling the constant case. See if the constant is too
3207 big for the field. Warn and return a tree of for 0 (false) if so. We do
3208 this not only for its own sake, but to avoid having to test for this
3209 error case below. If we didn't, we might generate wrong code.
3211 For unsigned fields, the constant shifted right by the field length should
3212 be all zero. For signed fields, the high-order bits should agree with
3217 if (! integer_zerop (const_binop (RSHIFT_EXPR,
3218 fold_convert (unsigned_type, rhs),
3219 size_int (lbitsize), 0)))
3221 warning ("comparison is always %d due to width of bit-field",
3223 return constant_boolean_node (code == NE_EXPR, compare_type);
3228 tree tem = const_binop (RSHIFT_EXPR, fold_convert (signed_type, rhs),
3229 size_int (lbitsize - 1), 0);
3230 if (! integer_zerop (tem) && ! integer_all_onesp (tem))
3232 warning ("comparison is always %d due to width of bit-field",
3234 return constant_boolean_node (code == NE_EXPR, compare_type);
3238 /* Single-bit compares should always be against zero. */
3239 if (lbitsize == 1 && ! integer_zerop (rhs))
3241 code = code == EQ_EXPR ? NE_EXPR : EQ_EXPR;
3242 rhs = fold_convert (type, integer_zero_node);
3245 /* Make a new bitfield reference, shift the constant over the
3246 appropriate number of bits and mask it with the computed mask
3247 (in case this was a signed field). If we changed it, make a new one. */
3248 lhs = make_bit_field_ref (linner, unsigned_type, nbitsize, nbitpos, 1);
3251 TREE_SIDE_EFFECTS (lhs) = 1;
3252 TREE_THIS_VOLATILE (lhs) = 1;
3255 rhs = fold (const_binop (BIT_AND_EXPR,
3256 const_binop (LSHIFT_EXPR,
3257 fold_convert (unsigned_type, rhs),
3258 size_int (lbitpos), 0),
3261 return build2 (code, compare_type,
3262 build2 (BIT_AND_EXPR, unsigned_type, lhs, mask),
3266 /* Subroutine for fold_truthop: decode a field reference.
3268 If EXP is a comparison reference, we return the innermost reference.
3270 *PBITSIZE is set to the number of bits in the reference, *PBITPOS is
3271 set to the starting bit number.
3273 If the innermost field can be completely contained in a mode-sized
3274 unit, *PMODE is set to that mode. Otherwise, it is set to VOIDmode.
3276 *PVOLATILEP is set to 1 if the any expression encountered is volatile;
3277 otherwise it is not changed.
3279 *PUNSIGNEDP is set to the signedness of the field.
3281 *PMASK is set to the mask used. This is either contained in a
3282 BIT_AND_EXPR or derived from the width of the field.
3284 *PAND_MASK is set to the mask found in a BIT_AND_EXPR, if any.
3286 Return 0 if this is not a component reference or is one that we can't
3287 do anything with. */
3290 decode_field_reference (tree exp, HOST_WIDE_INT *pbitsize,
3291 HOST_WIDE_INT *pbitpos, enum machine_mode *pmode,
3292 int *punsignedp, int *pvolatilep,
3293 tree *pmask, tree *pand_mask)
3295 tree outer_type = 0;
3297 tree mask, inner, offset;
3299 unsigned int precision;
3301 /* All the optimizations using this function assume integer fields.
3302 There are problems with FP fields since the type_for_size call
3303 below can fail for, e.g., XFmode. */
3304 if (! INTEGRAL_TYPE_P (TREE_TYPE (exp)))
3307 /* We are interested in the bare arrangement of bits, so strip everything
3308 that doesn't affect the machine mode. However, record the type of the
3309 outermost expression if it may matter below. */
3310 if (TREE_CODE (exp) == NOP_EXPR
3311 || TREE_CODE (exp) == CONVERT_EXPR
3312 || TREE_CODE (exp) == NON_LVALUE_EXPR)
3313 outer_type = TREE_TYPE (exp);
3316 if (TREE_CODE (exp) == BIT_AND_EXPR)
3318 and_mask = TREE_OPERAND (exp, 1);
3319 exp = TREE_OPERAND (exp, 0);
3320 STRIP_NOPS (exp); STRIP_NOPS (and_mask);
3321 if (TREE_CODE (and_mask) != INTEGER_CST)
3325 inner = get_inner_reference (exp, pbitsize, pbitpos, &offset, pmode,
3326 punsignedp, pvolatilep, false);
3327 if ((inner == exp && and_mask == 0)
3328 || *pbitsize < 0 || offset != 0
3329 || TREE_CODE (inner) == PLACEHOLDER_EXPR)
3332 /* If the number of bits in the reference is the same as the bitsize of
3333 the outer type, then the outer type gives the signedness. Otherwise
3334 (in case of a small bitfield) the signedness is unchanged. */
3335 if (outer_type && *pbitsize == TYPE_PRECISION (outer_type))
3336 *punsignedp = TYPE_UNSIGNED (outer_type);
3338 /* Compute the mask to access the bitfield. */
3339 unsigned_type = lang_hooks.types.type_for_size (*pbitsize, 1);
3340 precision = TYPE_PRECISION (unsigned_type);
3342 mask = build_int_cst (unsigned_type, -1);
3343 mask = force_fit_type (mask, 0, false, false);
3345 mask = const_binop (LSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3346 mask = const_binop (RSHIFT_EXPR, mask, size_int (precision - *pbitsize), 0);
3348 /* Merge it with the mask we found in the BIT_AND_EXPR, if any. */
3350 mask = fold (build2 (BIT_AND_EXPR, unsigned_type,
3351 fold_convert (unsigned_type, and_mask), mask));
3354 *pand_mask = and_mask;
3358 /* Return nonzero if MASK represents a mask of SIZE ones in the low-order
3362 all_ones_mask_p (tree mask, int size)
3364 tree type = TREE_TYPE (mask);
3365 unsigned int precision = TYPE_PRECISION (type);
3368 tmask = build_int_cst (lang_hooks.types.signed_type (type), -1);
3369 tmask = force_fit_type (tmask, 0, false, false);
3372 tree_int_cst_equal (mask,
3373 const_binop (RSHIFT_EXPR,
3374 const_binop (LSHIFT_EXPR, tmask,
3375 size_int (precision - size),
3377 size_int (precision - size), 0));
3380 /* Subroutine for fold: determine if VAL is the INTEGER_CONST that
3381 represents the sign bit of EXP's type. If EXP represents a sign
3382 or zero extension, also test VAL against the unextended type.
3383 The return value is the (sub)expression whose sign bit is VAL,
3384 or NULL_TREE otherwise. */
3387 sign_bit_p (tree exp, tree val)
3389 unsigned HOST_WIDE_INT mask_lo, lo;
3390 HOST_WIDE_INT mask_hi, hi;
3394 /* Tree EXP must have an integral type. */
3395 t = TREE_TYPE (exp);
3396 if (! INTEGRAL_TYPE_P (t))
3399 /* Tree VAL must be an integer constant. */
3400 if (TREE_CODE (val) != INTEGER_CST
3401 || TREE_CONSTANT_OVERFLOW (val))
3404 width = TYPE_PRECISION (t);
3405 if (width > HOST_BITS_PER_WIDE_INT)
3407 hi = (unsigned HOST_WIDE_INT) 1 << (width - HOST_BITS_PER_WIDE_INT - 1);
3410 mask_hi = ((unsigned HOST_WIDE_INT) -1
3411 >> (2 * HOST_BITS_PER_WIDE_INT - width));
3417 lo = (unsigned HOST_WIDE_INT) 1 << (width - 1);
3420 mask_lo = ((unsigned HOST_WIDE_INT) -1
3421 >> (HOST_BITS_PER_WIDE_INT - width));
3424 /* We mask off those bits beyond TREE_TYPE (exp) so that we can
3425 treat VAL as if it were unsigned. */
3426 if ((TREE_INT_CST_HIGH (val) & mask_hi) == hi
3427 && (TREE_INT_CST_LOW (val) & mask_lo) == lo)
3430 /* Handle extension from a narrower type. */
3431 if (TREE_CODE (exp) == NOP_EXPR
3432 && TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (exp, 0))) < width)
3433 return sign_bit_p (TREE_OPERAND (exp, 0), val);
3438 /* Subroutine for fold_truthop: determine if an operand is simple enough
3439 to be evaluated unconditionally. */
3442 simple_operand_p (tree exp)
3444 /* Strip any conversions that don't change the machine mode. */
3447 return (CONSTANT_CLASS_P (exp)
3448 || TREE_CODE (exp) == SSA_NAME
3450 && ! TREE_ADDRESSABLE (exp)
3451 && ! TREE_THIS_VOLATILE (exp)
3452 && ! DECL_NONLOCAL (exp)
3453 /* Don't regard global variables as simple. They may be
3454 allocated in ways unknown to the compiler (shared memory,
3455 #pragma weak, etc). */
3456 && ! TREE_PUBLIC (exp)
3457 && ! DECL_EXTERNAL (exp)
3458 /* Loading a static variable is unduly expensive, but global
3459 registers aren't expensive. */
3460 && (! TREE_STATIC (exp) || DECL_REGISTER (exp))));
3463 /* The following functions are subroutines to fold_range_test and allow it to
3464 try to change a logical combination of comparisons into a range test.
3467 X == 2 || X == 3 || X == 4 || X == 5
3471 (unsigned) (X - 2) <= 3
3473 We describe each set of comparisons as being either inside or outside
3474 a range, using a variable named like IN_P, and then describe the
3475 range with a lower and upper bound. If one of the bounds is omitted,
3476 it represents either the highest or lowest value of the type.
3478 In the comments below, we represent a range by two numbers in brackets
3479 preceded by a "+" to designate being inside that range, or a "-" to
3480 designate being outside that range, so the condition can be inverted by
3481 flipping the prefix. An omitted bound is represented by a "-". For
3482 example, "- [-, 10]" means being outside the range starting at the lowest
3483 possible value and ending at 10, in other words, being greater than 10.
3484 The range "+ [-, -]" is always true and hence the range "- [-, -]" is
3487 We set up things so that the missing bounds are handled in a consistent
3488 manner so neither a missing bound nor "true" and "false" need to be
3489 handled using a special case. */
3491 /* Return the result of applying CODE to ARG0 and ARG1, but handle the case
3492 of ARG0 and/or ARG1 being omitted, meaning an unlimited range. UPPER0_P
3493 and UPPER1_P are nonzero if the respective argument is an upper bound
3494 and zero for a lower. TYPE, if nonzero, is the type of the result; it
3495 must be specified for a comparison. ARG1 will be converted to ARG0's
3496 type if both are specified. */
3499 range_binop (enum tree_code code, tree type, tree arg0, int upper0_p,
3500 tree arg1, int upper1_p)
3506 /* If neither arg represents infinity, do the normal operation.
3507 Else, if not a comparison, return infinity. Else handle the special
3508 comparison rules. Note that most of the cases below won't occur, but
3509 are handled for consistency. */
3511 if (arg0 != 0 && arg1 != 0)
3513 tem = fold (build2 (code, type != 0 ? type : TREE_TYPE (arg0),
3514 arg0, fold_convert (TREE_TYPE (arg0), arg1)));
3516 return TREE_CODE (tem) == INTEGER_CST ? tem : 0;
3519 if (TREE_CODE_CLASS (code) != tcc_comparison)
3522 /* Set SGN[01] to -1 if ARG[01] is a lower bound, 1 for upper, and 0
3523 for neither. In real maths, we cannot assume open ended ranges are
3524 the same. But, this is computer arithmetic, where numbers are finite.
3525 We can therefore make the transformation of any unbounded range with
3526 the value Z, Z being greater than any representable number. This permits
3527 us to treat unbounded ranges as equal. */
3528 sgn0 = arg0 != 0 ? 0 : (upper0_p ? 1 : -1);
3529 sgn1 = arg1 != 0 ? 0 : (upper1_p ? 1 : -1);
3533 result = sgn0 == sgn1;
3536 result = sgn0 != sgn1;
3539 result = sgn0 < sgn1;
3542 result = sgn0 <= sgn1;
3545 result = sgn0 > sgn1;
3548 result = sgn0 >= sgn1;
3554 return constant_boolean_node (result, type);
3557 /* Given EXP, a logical expression, set the range it is testing into
3558 variables denoted by PIN_P, PLOW, and PHIGH. Return the expression
3559 actually being tested. *PLOW and *PHIGH will be made of the same type
3560 as the returned expression. If EXP is not a comparison, we will most
3561 likely not be returning a useful value and range. */
3564 make_range (tree exp, int *pin_p, tree *plow, tree *phigh)
3566 enum tree_code code;
3567 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
3568 tree exp_type = NULL_TREE, arg0_type = NULL_TREE;
3570 tree low, high, n_low, n_high;
3572 /* Start with simply saying "EXP != 0" and then look at the code of EXP
3573 and see if we can refine the range. Some of the cases below may not
3574 happen, but it doesn't seem worth worrying about this. We "continue"
3575 the outer loop when we've changed something; otherwise we "break"
3576 the switch, which will "break" the while. */
3579 low = high = fold_convert (TREE_TYPE (exp), integer_zero_node);
3583 code = TREE_CODE (exp);
3584 exp_type = TREE_TYPE (exp);
3586 if (IS_EXPR_CODE_CLASS (TREE_CODE_CLASS (code)))
3588 if (TREE_CODE_LENGTH (code) > 0)
3589 arg0 = TREE_OPERAND (exp, 0);
3590 if (TREE_CODE_CLASS (code) == tcc_comparison
3591 || TREE_CODE_CLASS (code) == tcc_unary
3592 || TREE_CODE_CLASS (code) == tcc_binary)
3593 arg0_type = TREE_TYPE (arg0);
3594 if (TREE_CODE_CLASS (code) == tcc_binary
3595 || TREE_CODE_CLASS (code) == tcc_comparison
3596 || (TREE_CODE_CLASS (code) == tcc_expression
3597 && TREE_CODE_LENGTH (code) > 1))
3598 arg1 = TREE_OPERAND (exp, 1);
3603 case TRUTH_NOT_EXPR:
3604 in_p = ! in_p, exp = arg0;
3607 case EQ_EXPR: case NE_EXPR:
3608 case LT_EXPR: case LE_EXPR: case GE_EXPR: case GT_EXPR:
3609 /* We can only do something if the range is testing for zero
3610 and if the second operand is an integer constant. Note that
3611 saying something is "in" the range we make is done by
3612 complementing IN_P since it will set in the initial case of
3613 being not equal to zero; "out" is leaving it alone. */
3614 if (low == 0 || high == 0
3615 || ! integer_zerop (low) || ! integer_zerop (high)
3616 || TREE_CODE (arg1) != INTEGER_CST)
3621 case NE_EXPR: /* - [c, c] */
3624 case EQ_EXPR: /* + [c, c] */
3625 in_p = ! in_p, low = high = arg1;
3627 case GT_EXPR: /* - [-, c] */
3628 low = 0, high = arg1;
3630 case GE_EXPR: /* + [c, -] */
3631 in_p = ! in_p, low = arg1, high = 0;
3633 case LT_EXPR: /* - [c, -] */
3634 low = arg1, high = 0;
3636 case LE_EXPR: /* + [-, c] */
3637 in_p = ! in_p, low = 0, high = arg1;
3643 /* If this is an unsigned comparison, we also know that EXP is
3644 greater than or equal to zero. We base the range tests we make
3645 on that fact, so we record it here so we can parse existing
3646 range tests. We test arg0_type since often the return type
3647 of, e.g. EQ_EXPR, is boolean. */
3648 if (TYPE_UNSIGNED (arg0_type) && (low == 0 || high == 0))
3650 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3652 fold_convert (arg0_type, integer_zero_node),
3656 in_p = n_in_p, low = n_low, high = n_high;
3658 /* If the high bound is missing, but we have a nonzero low
3659 bound, reverse the range so it goes from zero to the low bound
3661 if (high == 0 && low && ! integer_zerop (low))
3664 high = range_binop (MINUS_EXPR, NULL_TREE, low, 0,
3665 integer_one_node, 0);
3666 low = fold_convert (arg0_type, integer_zero_node);
3674 /* (-x) IN [a,b] -> x in [-b, -a] */
3675 n_low = range_binop (MINUS_EXPR, exp_type,
3676 fold_convert (exp_type, integer_zero_node),
3678 n_high = range_binop (MINUS_EXPR, exp_type,
3679 fold_convert (exp_type, integer_zero_node),
3681 low = n_low, high = n_high;
3687 exp = build2 (MINUS_EXPR, exp_type, negate_expr (arg0),
3688 fold_convert (exp_type, integer_one_node));
3691 case PLUS_EXPR: case MINUS_EXPR:
3692 if (TREE_CODE (arg1) != INTEGER_CST)
3695 /* If EXP is signed, any overflow in the computation is undefined,
3696 so we don't worry about it so long as our computations on
3697 the bounds don't overflow. For unsigned, overflow is defined
3698 and this is exactly the right thing. */
3699 n_low = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3700 arg0_type, low, 0, arg1, 0);
3701 n_high = range_binop (code == MINUS_EXPR ? PLUS_EXPR : MINUS_EXPR,
3702 arg0_type, high, 1, arg1, 0);
3703 if ((n_low != 0 && TREE_OVERFLOW (n_low))
3704 || (n_high != 0 && TREE_OVERFLOW (n_high)))
3707 /* Check for an unsigned range which has wrapped around the maximum
3708 value thus making n_high < n_low, and normalize it. */
3709 if (n_low && n_high && tree_int_cst_lt (n_high, n_low))
3711 low = range_binop (PLUS_EXPR, arg0_type, n_high, 0,
3712 integer_one_node, 0);
3713 high = range_binop (MINUS_EXPR, arg0_type, n_low, 0,
3714 integer_one_node, 0);
3716 /* If the range is of the form +/- [ x+1, x ], we won't
3717 be able to normalize it. But then, it represents the
3718 whole range or the empty set, so make it
3720 if (tree_int_cst_equal (n_low, low)
3721 && tree_int_cst_equal (n_high, high))
3727 low = n_low, high = n_high;
3732 case NOP_EXPR: case NON_LVALUE_EXPR: case CONVERT_EXPR:
3733 if (TYPE_PRECISION (arg0_type) > TYPE_PRECISION (exp_type))
3736 if (! INTEGRAL_TYPE_P (arg0_type)
3737 || (low != 0 && ! int_fits_type_p (low, arg0_type))
3738 || (high != 0 && ! int_fits_type_p (high, arg0_type)))
3741 n_low = low, n_high = high;
3744 n_low = fold_convert (arg0_type, n_low);
3747 n_high = fold_convert (arg0_type, n_high);
3750 /* If we're converting arg0 from an unsigned type, to exp,
3751 a signed type, we will be doing the comparison as unsigned.
3752 The tests above have already verified that LOW and HIGH
3755 So we have to ensure that we will handle large unsigned
3756 values the same way that the current signed bounds treat
3759 if (!TYPE_UNSIGNED (exp_type) && TYPE_UNSIGNED (arg0_type))
3762 tree equiv_type = lang_hooks.types.type_for_mode
3763 (TYPE_MODE (arg0_type), 1);
3765 /* A range without an upper bound is, naturally, unbounded.
3766 Since convert would have cropped a very large value, use
3767 the max value for the destination type. */
3769 = TYPE_MAX_VALUE (equiv_type) ? TYPE_MAX_VALUE (equiv_type)
3770 : TYPE_MAX_VALUE (arg0_type);
3772 if (TYPE_PRECISION (exp_type) == TYPE_PRECISION (arg0_type))
3773 high_positive = fold (build2 (RSHIFT_EXPR, arg0_type,
3774 fold_convert (arg0_type,
3776 fold_convert (arg0_type,
3777 integer_one_node)));
3779 /* If the low bound is specified, "and" the range with the
3780 range for which the original unsigned value will be
3784 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3785 1, n_low, n_high, 1,
3786 fold_convert (arg0_type,
3791 in_p = (n_in_p == in_p);
3795 /* Otherwise, "or" the range with the range of the input
3796 that will be interpreted as negative. */
3797 if (! merge_ranges (&n_in_p, &n_low, &n_high,
3798 0, n_low, n_high, 1,
3799 fold_convert (arg0_type,
3804 in_p = (in_p != n_in_p);
3809 low = n_low, high = n_high;
3819 /* If EXP is a constant, we can evaluate whether this is true or false. */
3820 if (TREE_CODE (exp) == INTEGER_CST)
3822 in_p = in_p == (integer_onep (range_binop (GE_EXPR, integer_type_node,
3824 && integer_onep (range_binop (LE_EXPR, integer_type_node,
3830 *pin_p = in_p, *plow = low, *phigh = high;
3834 /* Given a range, LOW, HIGH, and IN_P, an expression, EXP, and a result
3835 type, TYPE, return an expression to test if EXP is in (or out of, depending
3836 on IN_P) the range. Return 0 if the test couldn't be created. */
3839 build_range_check (tree type, tree exp, int in_p, tree low, tree high)
3841 tree etype = TREE_TYPE (exp);
3846 value = build_range_check (type, exp, 1, low, high);
3848 return invert_truthvalue (value);
3853 if (low == 0 && high == 0)
3854 return fold_convert (type, integer_one_node);
3857 return fold (build2 (LE_EXPR, type, exp, high));
3860 return fold (build2 (GE_EXPR, type, exp, low));
3862 if (operand_equal_p (low, high, 0))
3863 return fold (build2 (EQ_EXPR, type, exp, low));
3865 if (integer_zerop (low))
3867 if (! TYPE_UNSIGNED (etype))
3869 etype = lang_hooks.types.unsigned_type (etype);
3870 high = fold_convert (etype, high);
3871 exp = fold_convert (etype, exp);
3873 return build_range_check (type, exp, 1, 0, high);
3876 /* Optimize (c>=1) && (c<=127) into (signed char)c > 0. */
3877 if (integer_onep (low) && TREE_CODE (high) == INTEGER_CST)
3879 unsigned HOST_WIDE_INT lo;
3883 prec = TYPE_PRECISION (etype);
3884 if (prec <= HOST_BITS_PER_WIDE_INT)
3887 lo = ((unsigned HOST_WIDE_INT) 1 << (prec - 1)) - 1;
3891 hi = ((HOST_WIDE_INT) 1 << (prec - HOST_BITS_PER_WIDE_INT - 1)) - 1;
3892 lo = (unsigned HOST_WIDE_INT) -1;
3895 if (TREE_INT_CST_HIGH (high) == hi && TREE_INT_CST_LOW (high) == lo)
3897 if (TYPE_UNSIGNED (etype))
3899 etype = lang_hooks.types.signed_type (etype);
3900 exp = fold_convert (etype, exp);
3902 return fold (build2 (GT_EXPR, type, exp,
3903 fold_convert (etype, integer_zero_node)));
3907 value = const_binop (MINUS_EXPR, high, low, 0);
3908 if (value != 0 && TREE_OVERFLOW (value) && ! TYPE_UNSIGNED (etype))
3910 tree utype, minv, maxv;
3912 /* Check if (unsigned) INT_MAX + 1 == (unsigned) INT_MIN
3913 for the type in question, as we rely on this here. */
3914 switch (TREE_CODE (etype))
3919 utype = lang_hooks.types.unsigned_type (etype);
3920 maxv = fold_convert (utype, TYPE_MAX_VALUE (etype));
3921 maxv = range_binop (PLUS_EXPR, NULL_TREE, maxv, 1,
3922 integer_one_node, 1);
3923 minv = fold_convert (utype, TYPE_MIN_VALUE (etype));
3924 if (integer_zerop (range_binop (NE_EXPR, integer_type_node,
3928 high = fold_convert (etype, high);
3929 low = fold_convert (etype, low);
3930 exp = fold_convert (etype, exp);
3931 value = const_binop (MINUS_EXPR, high, low, 0);
3939 if (value != 0 && ! TREE_OVERFLOW (value))
3940 return build_range_check (type,
3941 fold (build2 (MINUS_EXPR, etype, exp, low)),
3942 1, fold_convert (etype, integer_zero_node),
3948 /* Given two ranges, see if we can merge them into one. Return 1 if we
3949 can, 0 if we can't. Set the output range into the specified parameters. */
3952 merge_ranges (int *pin_p, tree *plow, tree *phigh, int in0_p, tree low0,
3953 tree high0, int in1_p, tree low1, tree high1)
3961 int lowequal = ((low0 == 0 && low1 == 0)
3962 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
3963 low0, 0, low1, 0)));
3964 int highequal = ((high0 == 0 && high1 == 0)
3965 || integer_onep (range_binop (EQ_EXPR, integer_type_node,
3966 high0, 1, high1, 1)));
3968 /* Make range 0 be the range that starts first, or ends last if they
3969 start at the same value. Swap them if it isn't. */
3970 if (integer_onep (range_binop (GT_EXPR, integer_type_node,
3973 && integer_onep (range_binop (GT_EXPR, integer_type_node,
3974 high1, 1, high0, 1))))
3976 temp = in0_p, in0_p = in1_p, in1_p = temp;
3977 tem = low0, low0 = low1, low1 = tem;
3978 tem = high0, high0 = high1, high1 = tem;
3981 /* Now flag two cases, whether the ranges are disjoint or whether the
3982 second range is totally subsumed in the first. Note that the tests
3983 below are simplified by the ones above. */
3984 no_overlap = integer_onep (range_binop (LT_EXPR, integer_type_node,
3985 high0, 1, low1, 0));
3986 subset = integer_onep (range_binop (LE_EXPR, integer_type_node,
3987 high1, 1, high0, 1));
3989 /* We now have four cases, depending on whether we are including or
3990 excluding the two ranges. */
3993 /* If they don't overlap, the result is false. If the second range
3994 is a subset it is the result. Otherwise, the range is from the start
3995 of the second to the end of the first. */
3997 in_p = 0, low = high = 0;
3999 in_p = 1, low = low1, high = high1;
4001 in_p = 1, low = low1, high = high0;
4004 else if (in0_p && ! in1_p)
4006 /* If they don't overlap, the result is the first range. If they are
4007 equal, the result is false. If the second range is a subset of the
4008 first, and the ranges begin at the same place, we go from just after
4009 the end of the first range to the end of the second. If the second
4010 range is not a subset of the first, or if it is a subset and both
4011 ranges end at the same place, the range starts at the start of the
4012 first range and ends just before the second range.
4013 Otherwise, we can't describe this as a single range. */
4015 in_p = 1, low = low0, high = high0;
4016 else if (lowequal && highequal)
4017 in_p = 0, low = high = 0;
4018 else if (subset && lowequal)
4020 in_p = 1, high = high0;
4021 low = range_binop (PLUS_EXPR, NULL_TREE, high1, 0,
4022 integer_one_node, 0);
4024 else if (! subset || highequal)
4026 in_p = 1, low = low0;
4027 high = range_binop (MINUS_EXPR, NULL_TREE, low1, 0,
4028 integer_one_node, 0);
4034 else if (! in0_p && in1_p)
4036 /* If they don't overlap, the result is the second range. If the second
4037 is a subset of the first, the result is false. Otherwise,
4038 the range starts just after the first range and ends at the
4039 end of the second. */
4041 in_p = 1, low = low1, high = high1;
4042 else if (subset || highequal)
4043 in_p = 0, low = high = 0;
4046 in_p = 1, high = high1;
4047 low = range_binop (PLUS_EXPR, NULL_TREE, high0, 1,
4048 integer_one_node, 0);
4054 /* The case where we are excluding both ranges. Here the complex case
4055 is if they don't overlap. In that case, the only time we have a
4056 range is if they are adjacent. If the second is a subset of the
4057 first, the result is the first. Otherwise, the range to exclude
4058 starts at the beginning of the first range and ends at the end of the
4062 if (integer_onep (range_binop (EQ_EXPR, integer_type_node,
4063 range_binop (PLUS_EXPR, NULL_TREE,
4065 integer_one_node, 1),
4067 in_p = 0, low = low0, high = high1;
4070 /* Canonicalize - [min, x] into - [-, x]. */
4071 if (low0 && TREE_CODE (low0) == INTEGER_CST)
4072 switch (TREE_CODE (TREE_TYPE (low0)))
4075 if (TYPE_PRECISION (TREE_TYPE (low0))
4076 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (low0))))
4081 if (tree_int_cst_equal (low0,
4082 TYPE_MIN_VALUE (TREE_TYPE (low0))))
4086 if (TYPE_UNSIGNED (TREE_TYPE (low0))
4087 && integer_zerop (low0))
4094 /* Canonicalize - [x, max] into - [x, -]. */
4095 if (high1 && TREE_CODE (high1) == INTEGER_CST)
4096 switch (TREE_CODE (TREE_TYPE (high1)))
4099 if (TYPE_PRECISION (TREE_TYPE (high1))
4100 != GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (high1))))
4105 if (tree_int_cst_equal (high1,
4106 TYPE_MAX_VALUE (TREE_TYPE (high1))))
4110 if (TYPE_UNSIGNED (TREE_TYPE (high1))
4111 && integer_zerop (range_binop (PLUS_EXPR, NULL_TREE,
4113 integer_one_node, 1)))
4120 /* The ranges might be also adjacent between the maximum and
4121 minimum values of the given type. For
4122 - [{min,-}, x] and - [y, {max,-}] ranges where x + 1 < y
4123 return + [x + 1, y - 1]. */
4124 if (low0 == 0 && high1 == 0)
4126 low = range_binop (PLUS_EXPR, NULL_TREE, high0, 1,
4127 integer_one_node, 1);
4128 high = range_binop (MINUS_EXPR, NULL_TREE, low1, 0,
4129 integer_one_node, 0);
4130 if (low == 0 || high == 0)
4140 in_p = 0, low = low0, high = high0;
4142 in_p = 0, low = low0, high = high1;
4145 *pin_p = in_p, *plow = low, *phigh = high;
4150 /* Subroutine of fold, looking inside expressions of the form
4151 A op B ? A : C, where ARG0, ARG1 and ARG2 are the three operands
4152 of the COND_EXPR. This function is being used also to optimize
4153 A op B ? C : A, by reversing the comparison first.
4155 Return a folded expression whose code is not a COND_EXPR
4156 anymore, or NULL_TREE if no folding opportunity is found. */
4159 fold_cond_expr_with_comparison (tree type, tree arg0, tree arg1, tree arg2)
4161 enum tree_code comp_code = TREE_CODE (arg0);
4162 tree arg00 = TREE_OPERAND (arg0, 0);
4163 tree arg01 = TREE_OPERAND (arg0, 1);
4164 tree arg1_type = TREE_TYPE (arg1);
4170 /* If we have A op 0 ? A : -A, consider applying the following
4173 A == 0? A : -A same as -A
4174 A != 0? A : -A same as A
4175 A >= 0? A : -A same as abs (A)
4176 A > 0? A : -A same as abs (A)
4177 A <= 0? A : -A same as -abs (A)
4178 A < 0? A : -A same as -abs (A)
4180 None of these transformations work for modes with signed
4181 zeros. If A is +/-0, the first two transformations will
4182 change the sign of the result (from +0 to -0, or vice
4183 versa). The last four will fix the sign of the result,
4184 even though the original expressions could be positive or
4185 negative, depending on the sign of A.
4187 Note that all these transformations are correct if A is
4188 NaN, since the two alternatives (A and -A) are also NaNs. */
4189 if ((FLOAT_TYPE_P (TREE_TYPE (arg01))
4190 ? real_zerop (arg01)
4191 : integer_zerop (arg01))
4192 && TREE_CODE (arg2) == NEGATE_EXPR
4193 && operand_equal_p (TREE_OPERAND (arg2, 0), arg1, 0))
4198 tem = fold_convert (arg1_type, arg1);
4199 return pedantic_non_lvalue (fold_convert (type, negate_expr (tem)));
4202 return pedantic_non_lvalue (fold_convert (type, arg1));
4205 if (flag_trapping_math)
4210 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4211 arg1 = fold_convert (lang_hooks.types.signed_type
4212 (TREE_TYPE (arg1)), arg1);
4213 tem = fold (build1 (ABS_EXPR, TREE_TYPE (arg1), arg1));
4214 return pedantic_non_lvalue (fold_convert (type, tem));
4217 if (flag_trapping_math)
4221 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
4222 arg1 = fold_convert (lang_hooks.types.signed_type
4223 (TREE_TYPE (arg1)), arg1);
4224 tem = fold (build1 (ABS_EXPR, TREE_TYPE (arg1), arg1));
4225 return negate_expr (fold_convert (type, tem));
4227 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4231 /* A != 0 ? A : 0 is simply A, unless A is -0. Likewise
4232 A == 0 ? A : 0 is always 0 unless A is -0. Note that
4233 both transformations are correct when A is NaN: A != 0
4234 is then true, and A == 0 is false. */
4236 if (integer_zerop (arg01) && integer_zerop (arg2))
4238 if (comp_code == NE_EXPR)
4239 return pedantic_non_lvalue (fold_convert (type, arg1));
4240 else if (comp_code == EQ_EXPR)
4241 return fold_convert (type, integer_zero_node);
4244 /* Try some transformations of A op B ? A : B.
4246 A == B? A : B same as B
4247 A != B? A : B same as A
4248 A >= B? A : B same as max (A, B)
4249 A > B? A : B same as max (B, A)
4250 A <= B? A : B same as min (A, B)
4251 A < B? A : B same as min (B, A)
4253 As above, these transformations don't work in the presence
4254 of signed zeros. For example, if A and B are zeros of
4255 opposite sign, the first two transformations will change
4256 the sign of the result. In the last four, the original
4257 expressions give different results for (A=+0, B=-0) and
4258 (A=-0, B=+0), but the transformed expressions do not.
4260 The first two transformations are correct if either A or B
4261 is a NaN. In the first transformation, the condition will
4262 be false, and B will indeed be chosen. In the case of the
4263 second transformation, the condition A != B will be true,
4264 and A will be chosen.
4266 The conversions to max() and min() are not correct if B is
4267 a number and A is not. The conditions in the original
4268 expressions will be false, so all four give B. The min()
4269 and max() versions would give a NaN instead. */
4270 if (operand_equal_for_comparison_p (arg01, arg2, arg00))
4272 tree comp_op0 = arg00;
4273 tree comp_op1 = arg01;
4274 tree comp_type = TREE_TYPE (comp_op0);
4276 /* Avoid adding NOP_EXPRs in case this is an lvalue. */
4277 if (TYPE_MAIN_VARIANT (comp_type) == TYPE_MAIN_VARIANT (type))
4287 return pedantic_non_lvalue (fold_convert (type, arg2));
4289 return pedantic_non_lvalue (fold_convert (type, arg1));
4294 /* In C++ a ?: expression can be an lvalue, so put the
4295 operand which will be used if they are equal first
4296 so that we can convert this back to the
4297 corresponding COND_EXPR. */
4298 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4300 comp_op0 = fold_convert (comp_type, comp_op0);
4301 comp_op1 = fold_convert (comp_type, comp_op1);
4302 tem = (comp_code == LE_EXPR || comp_code == UNLE_EXPR)
4303 ? fold (build2 (MIN_EXPR, comp_type, comp_op0, comp_op1))
4304 : fold (build2 (MIN_EXPR, comp_type, comp_op1, comp_op0));
4305 return pedantic_non_lvalue (fold_convert (type, tem));
4312 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4314 comp_op0 = fold_convert (comp_type, comp_op0);
4315 comp_op1 = fold_convert (comp_type, comp_op1);
4316 tem = (comp_code == GE_EXPR || comp_code == UNGE_EXPR)
4317 ? fold (build2 (MAX_EXPR, comp_type, comp_op0, comp_op1))
4318 : fold (build2 (MAX_EXPR, comp_type, comp_op1, comp_op0));
4319 return pedantic_non_lvalue (fold_convert (type, tem));
4323 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4324 return pedantic_non_lvalue (fold_convert (type, arg2));
4327 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg1))))
4328 return pedantic_non_lvalue (fold_convert (type, arg1));
4331 gcc_assert (TREE_CODE_CLASS (comp_code) == tcc_comparison);
4336 /* If this is A op C1 ? A : C2 with C1 and C2 constant integers,
4337 we might still be able to simplify this. For example,
4338 if C1 is one less or one more than C2, this might have started
4339 out as a MIN or MAX and been transformed by this function.
4340 Only good for INTEGER_TYPEs, because we need TYPE_MAX_VALUE. */
4342 if (INTEGRAL_TYPE_P (type)
4343 && TREE_CODE (arg01) == INTEGER_CST
4344 && TREE_CODE (arg2) == INTEGER_CST)
4348 /* We can replace A with C1 in this case. */
4349 arg1 = fold_convert (type, arg01);
4350 return fold (build3 (COND_EXPR, type, arg0, arg1, arg2));
4353 /* If C1 is C2 + 1, this is min(A, C2). */
4354 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4356 && operand_equal_p (arg01,
4357 const_binop (PLUS_EXPR, arg2,
4358 integer_one_node, 0),
4360 return pedantic_non_lvalue (fold (build2 (MIN_EXPR,
4361 type, arg1, arg2)));
4365 /* If C1 is C2 - 1, this is min(A, C2). */
4366 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4368 && operand_equal_p (arg01,
4369 const_binop (MINUS_EXPR, arg2,
4370 integer_one_node, 0),
4372 return pedantic_non_lvalue (fold (build2 (MIN_EXPR,
4373 type, arg1, arg2)));
4377 /* If C1 is C2 - 1, this is max(A, C2). */
4378 if (! operand_equal_p (arg2, TYPE_MIN_VALUE (type),
4380 && operand_equal_p (arg01,
4381 const_binop (MINUS_EXPR, arg2,
4382 integer_one_node, 0),
4384 return pedantic_non_lvalue (fold (build2 (MAX_EXPR,
4385 type, arg1, arg2)));
4389 /* If C1 is C2 + 1, this is max(A, C2). */
4390 if (! operand_equal_p (arg2, TYPE_MAX_VALUE (type),
4392 && operand_equal_p (arg01,
4393 const_binop (PLUS_EXPR, arg2,
4394 integer_one_node, 0),
4396 return pedantic_non_lvalue (fold (build2 (MAX_EXPR,
4397 type, arg1, arg2)));
4410 #ifndef LOGICAL_OP_NON_SHORT_CIRCUIT
4411 #define LOGICAL_OP_NON_SHORT_CIRCUIT (BRANCH_COST >= 2)
4414 /* EXP is some logical combination of boolean tests. See if we can
4415 merge it into some range test. Return the new tree if so. */
4418 fold_range_test (enum tree_code code, tree type, tree op0, tree op1)
4420 int or_op = (code == TRUTH_ORIF_EXPR
4421 || code == TRUTH_OR_EXPR);
4422 int in0_p, in1_p, in_p;
4423 tree low0, low1, low, high0, high1, high;
4424 tree lhs = make_range (op0, &in0_p, &low0, &high0);
4425 tree rhs = make_range (op1, &in1_p, &low1, &high1);
4428 /* If this is an OR operation, invert both sides; we will invert
4429 again at the end. */
4431 in0_p = ! in0_p, in1_p = ! in1_p;
4433 /* If both expressions are the same, if we can merge the ranges, and we
4434 can build the range test, return it or it inverted. If one of the
4435 ranges is always true or always false, consider it to be the same
4436 expression as the other. */
4437 if ((lhs == 0 || rhs == 0 || operand_equal_p (lhs, rhs, 0))
4438 && merge_ranges (&in_p, &low, &high, in0_p, low0, high0,
4440 && 0 != (tem = (build_range_check (type,
4442 : rhs != 0 ? rhs : integer_zero_node,
4444 return or_op ? invert_truthvalue (tem) : tem;
4446 /* On machines where the branch cost is expensive, if this is a
4447 short-circuited branch and the underlying object on both sides
4448 is the same, make a non-short-circuit operation. */
4449 else if (LOGICAL_OP_NON_SHORT_CIRCUIT
4450 && lhs != 0 && rhs != 0
4451 && (code == TRUTH_ANDIF_EXPR
4452 || code == TRUTH_ORIF_EXPR)
4453 && operand_equal_p (lhs, rhs, 0))
4455 /* If simple enough, just rewrite. Otherwise, make a SAVE_EXPR
4456 unless we are at top level or LHS contains a PLACEHOLDER_EXPR, in
4457 which cases we can't do this. */
4458 if (simple_operand_p (lhs))
4459 return build2 (code == TRUTH_ANDIF_EXPR
4460 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4463 else if (lang_hooks.decls.global_bindings_p () == 0
4464 && ! CONTAINS_PLACEHOLDER_P (lhs))
4466 tree common = save_expr (lhs);
4468 if (0 != (lhs = build_range_check (type, common,
4469 or_op ? ! in0_p : in0_p,
4471 && (0 != (rhs = build_range_check (type, common,
4472 or_op ? ! in1_p : in1_p,
4474 return build2 (code == TRUTH_ANDIF_EXPR
4475 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR,
4483 /* Subroutine for fold_truthop: C is an INTEGER_CST interpreted as a P
4484 bit value. Arrange things so the extra bits will be set to zero if and
4485 only if C is signed-extended to its full width. If MASK is nonzero,
4486 it is an INTEGER_CST that should be AND'ed with the extra bits. */
4489 unextend (tree c, int p, int unsignedp, tree mask)
4491 tree type = TREE_TYPE (c);
4492 int modesize = GET_MODE_BITSIZE (TYPE_MODE (type));
4495 if (p == modesize || unsignedp)
4498 /* We work by getting just the sign bit into the low-order bit, then
4499 into the high-order bit, then sign-extend. We then XOR that value
4501 temp = const_binop (RSHIFT_EXPR, c, size_int (p - 1), 0);
4502 temp = const_binop (BIT_AND_EXPR, temp, size_int (1), 0);
4504 /* We must use a signed type in order to get an arithmetic right shift.
4505 However, we must also avoid introducing accidental overflows, so that
4506 a subsequent call to integer_zerop will work. Hence we must
4507 do the type conversion here. At this point, the constant is either
4508 zero or one, and the conversion to a signed type can never overflow.
4509 We could get an overflow if this conversion is done anywhere else. */
4510 if (TYPE_UNSIGNED (type))
4511 temp = fold_convert (lang_hooks.types.signed_type (type), temp);
4513 temp = const_binop (LSHIFT_EXPR, temp, size_int (modesize - 1), 0);
4514 temp = const_binop (RSHIFT_EXPR, temp, size_int (modesize - p - 1), 0);
4516 temp = const_binop (BIT_AND_EXPR, temp,
4517 fold_convert (TREE_TYPE (c), mask), 0);
4518 /* If necessary, convert the type back to match the type of C. */
4519 if (TYPE_UNSIGNED (type))
4520 temp = fold_convert (type, temp);
4522 return fold_convert (type, const_binop (BIT_XOR_EXPR, c, temp, 0));
4525 /* Find ways of folding logical expressions of LHS and RHS:
4526 Try to merge two comparisons to the same innermost item.
4527 Look for range tests like "ch >= '0' && ch <= '9'".
4528 Look for combinations of simple terms on machines with expensive branches
4529 and evaluate the RHS unconditionally.
4531 For example, if we have p->a == 2 && p->b == 4 and we can make an
4532 object large enough to span both A and B, we can do this with a comparison
4533 against the object ANDed with the a mask.
4535 If we have p->a == q->a && p->b == q->b, we may be able to use bit masking
4536 operations to do this with one comparison.
4538 We check for both normal comparisons and the BIT_AND_EXPRs made this by
4539 function and the one above.
4541 CODE is the logical operation being done. It can be TRUTH_ANDIF_EXPR,
4542 TRUTH_AND_EXPR, TRUTH_ORIF_EXPR, or TRUTH_OR_EXPR.
4544 TRUTH_TYPE is the type of the logical operand and LHS and RHS are its
4547 We return the simplified tree or 0 if no optimization is possible. */
4550 fold_truthop (enum tree_code code, tree truth_type, tree lhs, tree rhs)
4552 /* If this is the "or" of two comparisons, we can do something if
4553 the comparisons are NE_EXPR. If this is the "and", we can do something
4554 if the comparisons are EQ_EXPR. I.e.,
4555 (a->b == 2 && a->c == 4) can become (a->new == NEW).
4557 WANTED_CODE is this operation code. For single bit fields, we can
4558 convert EQ_EXPR to NE_EXPR so we need not reject the "wrong"
4559 comparison for one-bit fields. */
4561 enum tree_code wanted_code;
4562 enum tree_code lcode, rcode;
4563 tree ll_arg, lr_arg, rl_arg, rr_arg;
4564 tree ll_inner, lr_inner, rl_inner, rr_inner;
4565 HOST_WIDE_INT ll_bitsize, ll_bitpos, lr_bitsize, lr_bitpos;
4566 HOST_WIDE_INT rl_bitsize, rl_bitpos, rr_bitsize, rr_bitpos;
4567 HOST_WIDE_INT xll_bitpos, xlr_bitpos, xrl_bitpos, xrr_bitpos;
4568 HOST_WIDE_INT lnbitsize, lnbitpos, rnbitsize, rnbitpos;
4569 int ll_unsignedp, lr_unsignedp, rl_unsignedp, rr_unsignedp;
4570 enum machine_mode ll_mode, lr_mode, rl_mode, rr_mode;
4571 enum machine_mode lnmode, rnmode;
4572 tree ll_mask, lr_mask, rl_mask, rr_mask;
4573 tree ll_and_mask, lr_and_mask, rl_and_mask, rr_and_mask;
4574 tree l_const, r_const;
4575 tree lntype, rntype, result;
4576 int first_bit, end_bit;
4579 /* Start by getting the comparison codes. Fail if anything is volatile.
4580 If one operand is a BIT_AND_EXPR with the constant one, treat it as if
4581 it were surrounded with a NE_EXPR. */
4583 if (TREE_SIDE_EFFECTS (lhs) || TREE_SIDE_EFFECTS (rhs))
4586 lcode = TREE_CODE (lhs);
4587 rcode = TREE_CODE (rhs);
4589 if (lcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (lhs, 1)))
4591 lhs = build2 (NE_EXPR, truth_type, lhs,
4592 fold_convert (TREE_TYPE (lhs), integer_zero_node));
4596 if (rcode == BIT_AND_EXPR && integer_onep (TREE_OPERAND (rhs, 1)))
4598 rhs = build2 (NE_EXPR, truth_type, rhs,
4599 fold_convert (TREE_TYPE (rhs), integer_zero_node));
4603 if (TREE_CODE_CLASS (lcode) != tcc_comparison
4604 || TREE_CODE_CLASS (rcode) != tcc_comparison)
4607 ll_arg = TREE_OPERAND (lhs, 0);
4608 lr_arg = TREE_OPERAND (lhs, 1);
4609 rl_arg = TREE_OPERAND (rhs, 0);
4610 rr_arg = TREE_OPERAND (rhs, 1);
4612 /* Simplify (x<y) && (x==y) into (x<=y) and related optimizations. */
4613 if (simple_operand_p (ll_arg)
4614 && simple_operand_p (lr_arg))
4617 if (operand_equal_p (ll_arg, rl_arg, 0)
4618 && operand_equal_p (lr_arg, rr_arg, 0))
4620 result = combine_comparisons (code, lcode, rcode,
4621 truth_type, ll_arg, lr_arg);
4625 else if (operand_equal_p (ll_arg, rr_arg, 0)
4626 && operand_equal_p (lr_arg, rl_arg, 0))
4628 result = combine_comparisons (code, lcode,
4629 swap_tree_comparison (rcode),
4630 truth_type, ll_arg, lr_arg);
4636 code = ((code == TRUTH_AND_EXPR || code == TRUTH_ANDIF_EXPR)
4637 ? TRUTH_AND_EXPR : TRUTH_OR_EXPR);
4639 /* If the RHS can be evaluated unconditionally and its operands are
4640 simple, it wins to evaluate the RHS unconditionally on machines
4641 with expensive branches. In this case, this isn't a comparison
4642 that can be merged. Avoid doing this if the RHS is a floating-point
4643 comparison since those can trap. */
4645 if (BRANCH_COST >= 2
4646 && ! FLOAT_TYPE_P (TREE_TYPE (rl_arg))
4647 && simple_operand_p (rl_arg)
4648 && simple_operand_p (rr_arg))
4650 /* Convert (a != 0) || (b != 0) into (a | b) != 0. */
4651 if (code == TRUTH_OR_EXPR
4652 && lcode == NE_EXPR && integer_zerop (lr_arg)
4653 && rcode == NE_EXPR && integer_zerop (rr_arg)
4654 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg))
4655 return build2 (NE_EXPR, truth_type,
4656 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
4658 fold_convert (TREE_TYPE (ll_arg), integer_zero_node));
4660 /* Convert (a == 0) && (b == 0) into (a | b) == 0. */
4661 if (code == TRUTH_AND_EXPR
4662 && lcode == EQ_EXPR && integer_zerop (lr_arg)
4663 && rcode == EQ_EXPR && integer_zerop (rr_arg)
4664 && TREE_TYPE (ll_arg) == TREE_TYPE (rl_arg))
4665 return build2 (EQ_EXPR, truth_type,
4666 build2 (BIT_IOR_EXPR, TREE_TYPE (ll_arg),
4668 fold_convert (TREE_TYPE (ll_arg), integer_zero_node));
4670 if (LOGICAL_OP_NON_SHORT_CIRCUIT)
4671 return build2 (code, truth_type, lhs, rhs);
4674 /* See if the comparisons can be merged. Then get all the parameters for
4677 if ((lcode != EQ_EXPR && lcode != NE_EXPR)
4678 || (rcode != EQ_EXPR && rcode != NE_EXPR))
4682 ll_inner = decode_field_reference (ll_arg,
4683 &ll_bitsize, &ll_bitpos, &ll_mode,
4684 &ll_unsignedp, &volatilep, &ll_mask,
4686 lr_inner = decode_field_reference (lr_arg,
4687 &lr_bitsize, &lr_bitpos, &lr_mode,
4688 &lr_unsignedp, &volatilep, &lr_mask,
4690 rl_inner = decode_field_reference (rl_arg,
4691 &rl_bitsize, &rl_bitpos, &rl_mode,
4692 &rl_unsignedp, &volatilep, &rl_mask,
4694 rr_inner = decode_field_reference (rr_arg,
4695 &rr_bitsize, &rr_bitpos, &rr_mode,
4696 &rr_unsignedp, &volatilep, &rr_mask,
4699 /* It must be true that the inner operation on the lhs of each
4700 comparison must be the same if we are to be able to do anything.
4701 Then see if we have constants. If not, the same must be true for
4703 if (volatilep || ll_inner == 0 || rl_inner == 0
4704 || ! operand_equal_p (ll_inner, rl_inner, 0))
4707 if (TREE_CODE (lr_arg) == INTEGER_CST
4708 && TREE_CODE (rr_arg) == INTEGER_CST)
4709 l_const = lr_arg, r_const = rr_arg;
4710 else if (lr_inner == 0 || rr_inner == 0
4711 || ! operand_equal_p (lr_inner, rr_inner, 0))
4714 l_const = r_const = 0;
4716 /* If either comparison code is not correct for our logical operation,
4717 fail. However, we can convert a one-bit comparison against zero into
4718 the opposite comparison against that bit being set in the field. */
4720 wanted_code = (code == TRUTH_AND_EXPR ? EQ_EXPR : NE_EXPR);
4721 if (lcode != wanted_code)
4723 if (l_const && integer_zerop (l_const) && integer_pow2p (ll_mask))
4725 /* Make the left operand unsigned, since we are only interested
4726 in the value of one bit. Otherwise we are doing the wrong
4735 /* This is analogous to the code for l_const above. */
4736 if (rcode != wanted_code)
4738 if (r_const && integer_zerop (r_const) && integer_pow2p (rl_mask))
4747 /* After this point all optimizations will generate bit-field
4748 references, which we might not want. */
4749 if (! lang_hooks.can_use_bit_fields_p ())
4752 /* See if we can find a mode that contains both fields being compared on
4753 the left. If we can't, fail. Otherwise, update all constants and masks
4754 to be relative to a field of that size. */
4755 first_bit = MIN (ll_bitpos, rl_bitpos);
4756 end_bit = MAX (ll_bitpos + ll_bitsize, rl_bitpos + rl_bitsize);
4757 lnmode = get_best_mode (end_bit - first_bit, first_bit,
4758 TYPE_ALIGN (TREE_TYPE (ll_inner)), word_mode,
4760 if (lnmode == VOIDmode)
4763 lnbitsize = GET_MODE_BITSIZE (lnmode);
4764 lnbitpos = first_bit & ~ (lnbitsize - 1);
4765 lntype = lang_hooks.types.type_for_size (lnbitsize, 1);
4766 xll_bitpos = ll_bitpos - lnbitpos, xrl_bitpos = rl_bitpos - lnbitpos;
4768 if (BYTES_BIG_ENDIAN)
4770 xll_bitpos = lnbitsize - xll_bitpos - ll_bitsize;
4771 xrl_bitpos = lnbitsize - xrl_bitpos - rl_bitsize;
4774 ll_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, ll_mask),
4775 size_int (xll_bitpos), 0);
4776 rl_mask = const_binop (LSHIFT_EXPR, fold_convert (lntype, rl_mask),
4777 size_int (xrl_bitpos), 0);
4781 l_const = fold_convert (lntype, l_const);
4782 l_const = unextend (l_const, ll_bitsize, ll_unsignedp, ll_and_mask);
4783 l_const = const_binop (LSHIFT_EXPR, l_const, size_int (xll_bitpos), 0);
4784 if (! integer_zerop (const_binop (BIT_AND_EXPR, l_const,
4785 fold (build1 (BIT_NOT_EXPR,
4789 warning ("comparison is always %d", wanted_code == NE_EXPR);
4791 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
4796 r_const = fold_convert (lntype, r_const);
4797 r_const = unextend (r_const, rl_bitsize, rl_unsignedp, rl_and_mask);
4798 r_const = const_binop (LSHIFT_EXPR, r_const, size_int (xrl_bitpos), 0);
4799 if (! integer_zerop (const_binop (BIT_AND_EXPR, r_const,
4800 fold (build1 (BIT_NOT_EXPR,
4804 warning ("comparison is always %d", wanted_code == NE_EXPR);
4806 return constant_boolean_node (wanted_code == NE_EXPR, truth_type);
4810 /* If the right sides are not constant, do the same for it. Also,
4811 disallow this optimization if a size or signedness mismatch occurs
4812 between the left and right sides. */
4815 if (ll_bitsize != lr_bitsize || rl_bitsize != rr_bitsize
4816 || ll_unsignedp != lr_unsignedp || rl_unsignedp != rr_unsignedp
4817 /* Make sure the two fields on the right
4818 correspond to the left without being swapped. */
4819 || ll_bitpos - rl_bitpos != lr_bitpos - rr_bitpos)
4822 first_bit = MIN (lr_bitpos, rr_bitpos);
4823 end_bit = MAX (lr_bitpos + lr_bitsize, rr_bitpos + rr_bitsize);
4824 rnmode = get_best_mode (end_bit - first_bit, first_bit,
4825 TYPE_ALIGN (TREE_TYPE (lr_inner)), word_mode,
4827 if (rnmode == VOIDmode)
4830 rnbitsize = GET_MODE_BITSIZE (rnmode);
4831 rnbitpos = first_bit & ~ (rnbitsize - 1);
4832 rntype = lang_hooks.types.type_for_size (rnbitsize, 1);
4833 xlr_bitpos = lr_bitpos - rnbitpos, xrr_bitpos = rr_bitpos - rnbitpos;
4835 if (BYTES_BIG_ENDIAN)
4837 xlr_bitpos = rnbitsize - xlr_bitpos - lr_bitsize;
4838 xrr_bitpos = rnbitsize - xrr_bitpos - rr_bitsize;
4841 lr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, lr_mask),
4842 size_int (xlr_bitpos), 0);
4843 rr_mask = const_binop (LSHIFT_EXPR, fold_convert (rntype, rr_mask),
4844 size_int (xrr_bitpos), 0);
4846 /* Make a mask that corresponds to both fields being compared.
4847 Do this for both items being compared. If the operands are the
4848 same size and the bits being compared are in the same position
4849 then we can do this by masking both and comparing the masked
4851 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
4852 lr_mask = const_binop (BIT_IOR_EXPR, lr_mask, rr_mask, 0);
4853 if (lnbitsize == rnbitsize && xll_bitpos == xlr_bitpos)
4855 lhs = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
4856 ll_unsignedp || rl_unsignedp);
4857 if (! all_ones_mask_p (ll_mask, lnbitsize))
4858 lhs = build2 (BIT_AND_EXPR, lntype, lhs, ll_mask);
4860 rhs = make_bit_field_ref (lr_inner, rntype, rnbitsize, rnbitpos,
4861 lr_unsignedp || rr_unsignedp);
4862 if (! all_ones_mask_p (lr_mask, rnbitsize))
4863 rhs = build2 (BIT_AND_EXPR, rntype, rhs, lr_mask);
4865 return build2 (wanted_code, truth_type, lhs, rhs);
4868 /* There is still another way we can do something: If both pairs of
4869 fields being compared are adjacent, we may be able to make a wider
4870 field containing them both.
4872 Note that we still must mask the lhs/rhs expressions. Furthermore,
4873 the mask must be shifted to account for the shift done by
4874 make_bit_field_ref. */
4875 if ((ll_bitsize + ll_bitpos == rl_bitpos
4876 && lr_bitsize + lr_bitpos == rr_bitpos)
4877 || (ll_bitpos == rl_bitpos + rl_bitsize
4878 && lr_bitpos == rr_bitpos + rr_bitsize))
4882 lhs = make_bit_field_ref (ll_inner, lntype, ll_bitsize + rl_bitsize,
4883 MIN (ll_bitpos, rl_bitpos), ll_unsignedp);
4884 rhs = make_bit_field_ref (lr_inner, rntype, lr_bitsize + rr_bitsize,
4885 MIN (lr_bitpos, rr_bitpos), lr_unsignedp);
4887 ll_mask = const_binop (RSHIFT_EXPR, ll_mask,
4888 size_int (MIN (xll_bitpos, xrl_bitpos)), 0);
4889 lr_mask = const_binop (RSHIFT_EXPR, lr_mask,
4890 size_int (MIN (xlr_bitpos, xrr_bitpos)), 0);
4892 /* Convert to the smaller type before masking out unwanted bits. */
4894 if (lntype != rntype)
4896 if (lnbitsize > rnbitsize)
4898 lhs = fold_convert (rntype, lhs);
4899 ll_mask = fold_convert (rntype, ll_mask);
4902 else if (lnbitsize < rnbitsize)
4904 rhs = fold_convert (lntype, rhs);
4905 lr_mask = fold_convert (lntype, lr_mask);
4910 if (! all_ones_mask_p (ll_mask, ll_bitsize + rl_bitsize))
4911 lhs = build2 (BIT_AND_EXPR, type, lhs, ll_mask);
4913 if (! all_ones_mask_p (lr_mask, lr_bitsize + rr_bitsize))
4914 rhs = build2 (BIT_AND_EXPR, type, rhs, lr_mask);
4916 return build2 (wanted_code, truth_type, lhs, rhs);
4922 /* Handle the case of comparisons with constants. If there is something in
4923 common between the masks, those bits of the constants must be the same.
4924 If not, the condition is always false. Test for this to avoid generating
4925 incorrect code below. */
4926 result = const_binop (BIT_AND_EXPR, ll_mask, rl_mask, 0);
4927 if (! integer_zerop (result)
4928 && simple_cst_equal (const_binop (BIT_AND_EXPR, result, l_const, 0),
4929 const_binop (BIT_AND_EXPR, result, r_const, 0)) != 1)
4931 if (wanted_code == NE_EXPR)
4933 warning ("%<or%> of unmatched not-equal tests is always 1");
4934 return constant_boolean_node (true, truth_type);
4938 warning ("%<and%> of mutually exclusive equal-tests is always 0");
4939 return constant_boolean_node (false, truth_type);
4943 /* Construct the expression we will return. First get the component
4944 reference we will make. Unless the mask is all ones the width of
4945 that field, perform the mask operation. Then compare with the
4947 result = make_bit_field_ref (ll_inner, lntype, lnbitsize, lnbitpos,
4948 ll_unsignedp || rl_unsignedp);
4950 ll_mask = const_binop (BIT_IOR_EXPR, ll_mask, rl_mask, 0);
4951 if (! all_ones_mask_p (ll_mask, lnbitsize))
4952 result = build2 (BIT_AND_EXPR, lntype, result, ll_mask);
4954 return build2 (wanted_code, truth_type, result,
4955 const_binop (BIT_IOR_EXPR, l_const, r_const, 0));
4958 /* Optimize T, which is a comparison of a MIN_EXPR or MAX_EXPR with a
4962 optimize_minmax_comparison (enum tree_code code, tree type, tree op0, tree op1)
4965 enum tree_code op_code;
4966 tree comp_const = op1;
4968 int consts_equal, consts_lt;
4971 STRIP_SIGN_NOPS (arg0);
4973 op_code = TREE_CODE (arg0);
4974 minmax_const = TREE_OPERAND (arg0, 1);
4975 consts_equal = tree_int_cst_equal (minmax_const, comp_const);
4976 consts_lt = tree_int_cst_lt (minmax_const, comp_const);
4977 inner = TREE_OPERAND (arg0, 0);
4979 /* If something does not permit us to optimize, return the original tree. */
4980 if ((op_code != MIN_EXPR && op_code != MAX_EXPR)
4981 || TREE_CODE (comp_const) != INTEGER_CST
4982 || TREE_CONSTANT_OVERFLOW (comp_const)
4983 || TREE_CODE (minmax_const) != INTEGER_CST
4984 || TREE_CONSTANT_OVERFLOW (minmax_const))
4987 /* Now handle all the various comparison codes. We only handle EQ_EXPR
4988 and GT_EXPR, doing the rest with recursive calls using logical
4992 case NE_EXPR: case LT_EXPR: case LE_EXPR:
4994 /* FIXME: We should be able to invert code without building a
4995 scratch tree node, but doing so would require us to
4996 duplicate a part of invert_truthvalue here. */
4997 tree tem = invert_truthvalue (build2 (code, type, op0, op1));
4998 tem = optimize_minmax_comparison (TREE_CODE (tem),
5000 TREE_OPERAND (tem, 0),
5001 TREE_OPERAND (tem, 1));
5002 return invert_truthvalue (tem);
5007 fold (build2 (TRUTH_ORIF_EXPR, type,
5008 optimize_minmax_comparison
5009 (EQ_EXPR, type, arg0, comp_const),
5010 optimize_minmax_comparison
5011 (GT_EXPR, type, arg0, comp_const)));
5014 if (op_code == MAX_EXPR && consts_equal)
5015 /* MAX (X, 0) == 0 -> X <= 0 */
5016 return fold (build2 (LE_EXPR, type, inner, comp_const));
5018 else if (op_code == MAX_EXPR && consts_lt)
5019 /* MAX (X, 0) == 5 -> X == 5 */
5020 return fold (build2 (EQ_EXPR, type, inner, comp_const));
5022 else if (op_code == MAX_EXPR)
5023 /* MAX (X, 0) == -1 -> false */
5024 return omit_one_operand (type, integer_zero_node, inner);
5026 else if (consts_equal)
5027 /* MIN (X, 0) == 0 -> X >= 0 */
5028 return fold (build2 (GE_EXPR, type, inner, comp_const));
5031 /* MIN (X, 0) == 5 -> false */
5032 return omit_one_operand (type, integer_zero_node, inner);
5035 /* MIN (X, 0) == -1 -> X == -1 */
5036 return fold (build2 (EQ_EXPR, type, inner, comp_const));
5039 if (op_code == MAX_EXPR && (consts_equal || consts_lt))
5040 /* MAX (X, 0) > 0 -> X > 0
5041 MAX (X, 0) > 5 -> X > 5 */
5042 return fold (build2 (GT_EXPR, type, inner, comp_const));
5044 else if (op_code == MAX_EXPR)
5045 /* MAX (X, 0) > -1 -> true */
5046 return omit_one_operand (type, integer_one_node, inner);
5048 else if (op_code == MIN_EXPR && (consts_equal || consts_lt))
5049 /* MIN (X, 0) > 0 -> false
5050 MIN (X, 0) > 5 -> false */
5051 return omit_one_operand (type, integer_zero_node, inner);
5054 /* MIN (X, 0) > -1 -> X > -1 */
5055 return fold (build2 (GT_EXPR, type, inner, comp_const));
5062 /* T is an integer expression that is being multiplied, divided, or taken a
5063 modulus (CODE says which and what kind of divide or modulus) by a
5064 constant C. See if we can eliminate that operation by folding it with
5065 other operations already in T. WIDE_TYPE, if non-null, is a type that
5066 should be used for the computation if wider than our type.
5068 For example, if we are dividing (X * 8) + (Y * 16) by 4, we can return
5069 (X * 2) + (Y * 4). We must, however, be assured that either the original
5070 expression would not overflow or that overflow is undefined for the type
5071 in the language in question.
5073 We also canonicalize (X + 7) * 4 into X * 4 + 28 in the hope that either
5074 the machine has a multiply-accumulate insn or that this is part of an
5075 addressing calculation.
5077 If we return a non-null expression, it is an equivalent form of the
5078 original computation, but need not be in the original type. */
5081 extract_muldiv (tree t, tree c, enum tree_code code, tree wide_type)
5083 /* To avoid exponential search depth, refuse to allow recursion past
5084 three levels. Beyond that (1) it's highly unlikely that we'll find
5085 something interesting and (2) we've probably processed it before
5086 when we built the inner expression. */
5095 ret = extract_muldiv_1 (t, c, code, wide_type);
5102 extract_muldiv_1 (tree t, tree c, enum tree_code code, tree wide_type)
5104 tree type = TREE_TYPE (t);
5105 enum tree_code tcode = TREE_CODE (t);
5106 tree ctype = (wide_type != 0 && (GET_MODE_SIZE (TYPE_MODE (wide_type))
5107 > GET_MODE_SIZE (TYPE_MODE (type)))
5108 ? wide_type : type);
5110 int same_p = tcode == code;
5111 tree op0 = NULL_TREE, op1 = NULL_TREE;
5113 /* Don't deal with constants of zero here; they confuse the code below. */
5114 if (integer_zerop (c))
5117 if (TREE_CODE_CLASS (tcode) == tcc_unary)
5118 op0 = TREE_OPERAND (t, 0);
5120 if (TREE_CODE_CLASS (tcode) == tcc_binary)
5121 op0 = TREE_OPERAND (t, 0), op1 = TREE_OPERAND (t, 1);
5123 /* Note that we need not handle conditional operations here since fold
5124 already handles those cases. So just do arithmetic here. */
5128 /* For a constant, we can always simplify if we are a multiply
5129 or (for divide and modulus) if it is a multiple of our constant. */
5130 if (code == MULT_EXPR
5131 || integer_zerop (const_binop (TRUNC_MOD_EXPR, t, c, 0)))
5132 return const_binop (code, fold_convert (ctype, t),
5133 fold_convert (ctype, c), 0);
5136 case CONVERT_EXPR: case NON_LVALUE_EXPR: case NOP_EXPR:
5137 /* If op0 is an expression ... */
5138 if ((COMPARISON_CLASS_P (op0)
5139 || UNARY_CLASS_P (op0)
5140 || BINARY_CLASS_P (op0)
5141 || EXPRESSION_CLASS_P (op0))
5142 /* ... and is unsigned, and its type is smaller than ctype,
5143 then we cannot pass through as widening. */
5144 && ((TYPE_UNSIGNED (TREE_TYPE (op0))
5145 && ! (TREE_CODE (TREE_TYPE (op0)) == INTEGER_TYPE
5146 && TYPE_IS_SIZETYPE (TREE_TYPE (op0)))
5147 && (GET_MODE_SIZE (TYPE_MODE (ctype))
5148 > GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0)))))
5149 /* ... or this is a truncation (t is narrower than op0),
5150 then we cannot pass through this narrowing. */
5151 || (GET_MODE_SIZE (TYPE_MODE (type))
5152 < GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (op0))))
5153 /* ... or signedness changes for division or modulus,
5154 then we cannot pass through this conversion. */
5155 || (code != MULT_EXPR
5156 && (TYPE_UNSIGNED (ctype)
5157 != TYPE_UNSIGNED (TREE_TYPE (op0))))))
5160 /* Pass the constant down and see if we can make a simplification. If
5161 we can, replace this expression with the inner simplification for
5162 possible later conversion to our or some other type. */
5163 if ((t2 = fold_convert (TREE_TYPE (op0), c)) != 0
5164 && TREE_CODE (t2) == INTEGER_CST
5165 && ! TREE_CONSTANT_OVERFLOW (t2)
5166 && (0 != (t1 = extract_muldiv (op0, t2, code,
5168 ? ctype : NULL_TREE))))
5173 /* If widening the type changes it from signed to unsigned, then we
5174 must avoid building ABS_EXPR itself as unsigned. */
5175 if (TYPE_UNSIGNED (ctype) && !TYPE_UNSIGNED (type))
5177 tree cstype = (*lang_hooks.types.signed_type) (ctype);
5178 if ((t1 = extract_muldiv (op0, c, code, cstype)) != 0)
5180 t1 = fold (build1 (tcode, cstype, fold_convert (cstype, t1)));
5181 return fold_convert (ctype, t1);
5187 if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
5188 return fold (build1 (tcode, ctype, fold_convert (ctype, t1)));
5191 case MIN_EXPR: case MAX_EXPR:
5192 /* If widening the type changes the signedness, then we can't perform
5193 this optimization as that changes the result. */
5194 if (TYPE_UNSIGNED (ctype) != TYPE_UNSIGNED (type))
5197 /* MIN (a, b) / 5 -> MIN (a / 5, b / 5) */
5198 if ((t1 = extract_muldiv (op0, c, code, wide_type)) != 0
5199 && (t2 = extract_muldiv (op1, c, code, wide_type)) != 0)
5201 if (tree_int_cst_sgn (c) < 0)
5202 tcode = (tcode == MIN_EXPR ? MAX_EXPR : MIN_EXPR);
5204 return fold (build2 (tcode, ctype, fold_convert (ctype, t1),
5205 fold_convert (ctype, t2)));
5209 case LSHIFT_EXPR: case RSHIFT_EXPR:
5210 /* If the second operand is constant, this is a multiplication
5211 or floor division, by a power of two, so we can treat it that
5212 way unless the multiplier or divisor overflows. Signed
5213 left-shift overflow is implementation-defined rather than
5214 undefined in C90, so do not convert signed left shift into
5216 if (TREE_CODE (op1) == INTEGER_CST
5217 && (tcode == RSHIFT_EXPR || TYPE_UNSIGNED (TREE_TYPE (op0)))
5218 /* const_binop may not detect overflow correctly,
5219 so check for it explicitly here. */
5220 && TYPE_PRECISION (TREE_TYPE (size_one_node)) > TREE_INT_CST_LOW (op1)
5221 && TREE_INT_CST_HIGH (op1) == 0
5222 && 0 != (t1 = fold_convert (ctype,
5223 const_binop (LSHIFT_EXPR,
5226 && ! TREE_OVERFLOW (t1))
5227 return extract_muldiv (build2 (tcode == LSHIFT_EXPR
5228 ? MULT_EXPR : FLOOR_DIV_EXPR,
5229 ctype, fold_convert (ctype, op0), t1),
5230 c, code, wide_type);
5233 case PLUS_EXPR: case MINUS_EXPR:
5234 /* See if we can eliminate the operation on both sides. If we can, we
5235 can return a new PLUS or MINUS. If we can't, the only remaining
5236 cases where we can do anything are if the second operand is a
5238 t1 = extract_muldiv (op0, c, code, wide_type);
5239 t2 = extract_muldiv (op1, c, code, wide_type);
5240 if (t1 != 0 && t2 != 0
5241 && (code == MULT_EXPR
5242 /* If not multiplication, we can only do this if both operands
5243 are divisible by c. */
5244 || (multiple_of_p (ctype, op0, c)
5245 && multiple_of_p (ctype, op1, c))))
5246 return fold (build2 (tcode, ctype, fold_convert (ctype, t1),
5247 fold_convert (ctype, t2)));
5249 /* If this was a subtraction, negate OP1 and set it to be an addition.
5250 This simplifies the logic below. */
5251 if (tcode == MINUS_EXPR)
5252 tcode = PLUS_EXPR, op1 = negate_expr (op1);
5254 if (TREE_CODE (op1) != INTEGER_CST)
5257 /* If either OP1 or C are negative, this optimization is not safe for
5258 some of the division and remainder types while for others we need
5259 to change the code. */
5260 if (tree_int_cst_sgn (op1) < 0 || tree_int_cst_sgn (c) < 0)
5262 if (code == CEIL_DIV_EXPR)
5263 code = FLOOR_DIV_EXPR;
5264 else if (code == FLOOR_DIV_EXPR)
5265 code = CEIL_DIV_EXPR;
5266 else if (code != MULT_EXPR
5267 && code != CEIL_MOD_EXPR && code != FLOOR_MOD_EXPR)
5271 /* If it's a multiply or a division/modulus operation of a multiple
5272 of our constant, do the operation and verify it doesn't overflow. */
5273 if (code == MULT_EXPR
5274 || integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5276 op1 = const_binop (code, fold_convert (ctype, op1),
5277 fold_convert (ctype, c), 0);
5278 /* We allow the constant to overflow with wrapping semantics. */
5280 || (TREE_OVERFLOW (op1) && ! flag_wrapv))
5286 /* If we have an unsigned type is not a sizetype, we cannot widen
5287 the operation since it will change the result if the original
5288 computation overflowed. */
5289 if (TYPE_UNSIGNED (ctype)
5290 && ! (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype))
5294 /* If we were able to eliminate our operation from the first side,
5295 apply our operation to the second side and reform the PLUS. */
5296 if (t1 != 0 && (TREE_CODE (t1) != code || code == MULT_EXPR))
5297 return fold (build2 (tcode, ctype, fold_convert (ctype, t1), op1));
5299 /* The last case is if we are a multiply. In that case, we can
5300 apply the distributive law to commute the multiply and addition
5301 if the multiplication of the constants doesn't overflow. */
5302 if (code == MULT_EXPR)
5303 return fold (build2 (tcode, ctype,
5304 fold (build2 (code, ctype,
5305 fold_convert (ctype, op0),
5306 fold_convert (ctype, c))),
5312 /* We have a special case here if we are doing something like
5313 (C * 8) % 4 since we know that's zero. */
5314 if ((code == TRUNC_MOD_EXPR || code == CEIL_MOD_EXPR
5315 || code == FLOOR_MOD_EXPR || code == ROUND_MOD_EXPR)
5316 && TREE_CODE (TREE_OPERAND (t, 1)) == INTEGER_CST
5317 && integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5318 return omit_one_operand (type, integer_zero_node, op0);
5320 /* ... fall through ... */
5322 case TRUNC_DIV_EXPR: case CEIL_DIV_EXPR: case FLOOR_DIV_EXPR:
5323 case ROUND_DIV_EXPR: case EXACT_DIV_EXPR:
5324 /* If we can extract our operation from the LHS, do so and return a
5325 new operation. Likewise for the RHS from a MULT_EXPR. Otherwise,
5326 do something only if the second operand is a constant. */
5328 && (t1 = extract_muldiv (op0, c, code, wide_type)) != 0)
5329 return fold (build2 (tcode, ctype, fold_convert (ctype, t1),
5330 fold_convert (ctype, op1)));
5331 else if (tcode == MULT_EXPR && code == MULT_EXPR
5332 && (t1 = extract_muldiv (op1, c, code, wide_type)) != 0)
5333 return fold (build2 (tcode, ctype, fold_convert (ctype, op0),
5334 fold_convert (ctype, t1)));
5335 else if (TREE_CODE (op1) != INTEGER_CST)
5338 /* If these are the same operation types, we can associate them
5339 assuming no overflow. */
5341 && 0 != (t1 = const_binop (MULT_EXPR, fold_convert (ctype, op1),
5342 fold_convert (ctype, c), 0))
5343 && ! TREE_OVERFLOW (t1))
5344 return fold (build2 (tcode, ctype, fold_convert (ctype, op0), t1));
5346 /* If these operations "cancel" each other, we have the main
5347 optimizations of this pass, which occur when either constant is a
5348 multiple of the other, in which case we replace this with either an
5349 operation or CODE or TCODE.
5351 If we have an unsigned type that is not a sizetype, we cannot do
5352 this since it will change the result if the original computation
5354 if ((! TYPE_UNSIGNED (ctype)
5355 || (TREE_CODE (ctype) == INTEGER_TYPE && TYPE_IS_SIZETYPE (ctype)))
5357 && ((code == MULT_EXPR && tcode == EXACT_DIV_EXPR)
5358 || (tcode == MULT_EXPR
5359 && code != TRUNC_MOD_EXPR && code != CEIL_MOD_EXPR
5360 && code != FLOOR_MOD_EXPR && code != ROUND_MOD_EXPR)))
5362 if (integer_zerop (const_binop (TRUNC_MOD_EXPR, op1, c, 0)))
5363 return fold (build2 (tcode, ctype, fold_convert (ctype, op0),
5364 fold_convert (ctype,
5365 const_binop (TRUNC_DIV_EXPR,
5367 else if (integer_zerop (const_binop (TRUNC_MOD_EXPR, c, op1, 0)))
5368 return fold (build2 (code, ctype, fold_convert (ctype, op0),
5369 fold_convert (ctype,
5370 const_binop (TRUNC_DIV_EXPR,
5382 /* Return a node which has the indicated constant VALUE (either 0 or
5383 1), and is of the indicated TYPE. */
5386 constant_boolean_node (int value, tree type)
5388 if (type == integer_type_node)
5389 return value ? integer_one_node : integer_zero_node;
5390 else if (type == boolean_type_node)
5391 return value ? boolean_true_node : boolean_false_node;
5393 return build_int_cst (type, value);
5397 /* Return true if expr looks like an ARRAY_REF and set base and
5398 offset to the appropriate trees. If there is no offset,
5399 offset is set to NULL_TREE. */
5402 extract_array_ref (tree expr, tree *base, tree *offset)
5404 /* We have to be careful with stripping nops as with the
5405 base type the meaning of the offset can change. */
5406 tree inner_expr = expr;
5407 STRIP_NOPS (inner_expr);
5408 /* One canonical form is a PLUS_EXPR with the first
5409 argument being an ADDR_EXPR with a possible NOP_EXPR
5411 if (TREE_CODE (expr) == PLUS_EXPR)
5413 tree op0 = TREE_OPERAND (expr, 0);
5415 if (TREE_CODE (op0) == ADDR_EXPR)
5417 *base = TREE_OPERAND (expr, 0);
5418 *offset = TREE_OPERAND (expr, 1);
5422 /* Other canonical form is an ADDR_EXPR of an ARRAY_REF,
5423 which we transform into an ADDR_EXPR with appropriate
5424 offset. For other arguments to the ADDR_EXPR we assume
5425 zero offset and as such do not care about the ADDR_EXPR
5426 type and strip possible nops from it. */
5427 else if (TREE_CODE (inner_expr) == ADDR_EXPR)
5429 tree op0 = TREE_OPERAND (inner_expr, 0);
5430 if (TREE_CODE (op0) == ARRAY_REF)
5432 *base = build_fold_addr_expr (TREE_OPERAND (op0, 0));
5433 *offset = TREE_OPERAND (op0, 1);
5438 *offset = NULL_TREE;
5447 /* Transform `a + (b ? x : y)' into `b ? (a + x) : (a + y)'.
5448 Transform, `a + (x < y)' into `(x < y) ? (a + 1) : (a + 0)'. Here
5449 CODE corresponds to the `+', COND to the `(b ? x : y)' or `(x < y)'
5450 expression, and ARG to `a'. If COND_FIRST_P is nonzero, then the
5451 COND is the first argument to CODE; otherwise (as in the example
5452 given here), it is the second argument. TYPE is the type of the
5453 original expression. Return NULL_TREE if no simplification is
5457 fold_binary_op_with_conditional_arg (enum tree_code code,
5458 tree type, tree op0, tree op1,
5459 tree cond, tree arg, int cond_first_p)
5461 tree cond_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
5462 tree arg_type = cond_first_p ? TREE_TYPE (op0) : TREE_TYPE (op1);
5463 tree test, true_value, false_value;
5464 tree lhs = NULL_TREE;
5465 tree rhs = NULL_TREE;
5467 /* This transformation is only worthwhile if we don't have to wrap
5468 arg in a SAVE_EXPR, and the operation can be simplified on at least
5469 one of the branches once its pushed inside the COND_EXPR. */
5470 if (!TREE_CONSTANT (arg))
5473 if (TREE_CODE (cond) == COND_EXPR)
5475 test = TREE_OPERAND (cond, 0);
5476 true_value = TREE_OPERAND (cond, 1);
5477 false_value = TREE_OPERAND (cond, 2);
5478 /* If this operand throws an expression, then it does not make
5479 sense to try to perform a logical or arithmetic operation
5481 if (VOID_TYPE_P (TREE_TYPE (true_value)))
5483 if (VOID_TYPE_P (TREE_TYPE (false_value)))
5488 tree testtype = TREE_TYPE (cond);
5490 true_value = constant_boolean_node (true, testtype);
5491 false_value = constant_boolean_node (false, testtype);
5494 arg = fold_convert (arg_type, arg);
5497 true_value = fold_convert (cond_type, true_value);
5498 lhs = fold (cond_first_p ? build2 (code, type, true_value, arg)
5499 : build2 (code, type, arg, true_value));
5503 false_value = fold_convert (cond_type, false_value);
5504 rhs = fold (cond_first_p ? build2 (code, type, false_value, arg)
5505 : build2 (code, type, arg, false_value));
5508 test = fold (build3 (COND_EXPR, type, test, lhs, rhs));
5509 return fold_convert (type, test);
5513 /* Subroutine of fold() that checks for the addition of +/- 0.0.
5515 If !NEGATE, return true if ADDEND is +/-0.0 and, for all X of type
5516 TYPE, X + ADDEND is the same as X. If NEGATE, return true if X -
5517 ADDEND is the same as X.
5519 X + 0 and X - 0 both give X when X is NaN, infinite, or nonzero
5520 and finite. The problematic cases are when X is zero, and its mode
5521 has signed zeros. In the case of rounding towards -infinity,
5522 X - 0 is not the same as X because 0 - 0 is -0. In other rounding
5523 modes, X + 0 is not the same as X because -0 + 0 is 0. */
5526 fold_real_zero_addition_p (tree type, tree addend, int negate)
5528 if (!real_zerop (addend))
5531 /* Don't allow the fold with -fsignaling-nans. */
5532 if (HONOR_SNANS (TYPE_MODE (type)))
5535 /* Allow the fold if zeros aren't signed, or their sign isn't important. */
5536 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (type)))
5539 /* Treat x + -0 as x - 0 and x - -0 as x + 0. */
5540 if (TREE_CODE (addend) == REAL_CST
5541 && REAL_VALUE_MINUS_ZERO (TREE_REAL_CST (addend)))
5544 /* The mode has signed zeros, and we have to honor their sign.
5545 In this situation, there is only one case we can return true for.
5546 X - 0 is the same as X unless rounding towards -infinity is
5548 return negate && !HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (type));
5551 /* Subroutine of fold() that checks comparisons of built-in math
5552 functions against real constants.
5554 FCODE is the DECL_FUNCTION_CODE of the built-in, CODE is the comparison
5555 operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR, GE_EXPR or LE_EXPR. TYPE
5556 is the type of the result and ARG0 and ARG1 are the operands of the
5557 comparison. ARG1 must be a TREE_REAL_CST.
5559 The function returns the constant folded tree if a simplification
5560 can be made, and NULL_TREE otherwise. */
5563 fold_mathfn_compare (enum built_in_function fcode, enum tree_code code,
5564 tree type, tree arg0, tree arg1)
5568 if (BUILTIN_SQRT_P (fcode))
5570 tree arg = TREE_VALUE (TREE_OPERAND (arg0, 1));
5571 enum machine_mode mode = TYPE_MODE (TREE_TYPE (arg0));
5573 c = TREE_REAL_CST (arg1);
5574 if (REAL_VALUE_NEGATIVE (c))
5576 /* sqrt(x) < y is always false, if y is negative. */
5577 if (code == EQ_EXPR || code == LT_EXPR || code == LE_EXPR)
5578 return omit_one_operand (type, integer_zero_node, arg);
5580 /* sqrt(x) > y is always true, if y is negative and we
5581 don't care about NaNs, i.e. negative values of x. */
5582 if (code == NE_EXPR || !HONOR_NANS (mode))
5583 return omit_one_operand (type, integer_one_node, arg);
5585 /* sqrt(x) > y is the same as x >= 0, if y is negative. */
5586 return fold (build2 (GE_EXPR, type, arg,
5587 build_real (TREE_TYPE (arg), dconst0)));
5589 else if (code == GT_EXPR || code == GE_EXPR)
5593 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
5594 real_convert (&c2, mode, &c2);
5596 if (REAL_VALUE_ISINF (c2))
5598 /* sqrt(x) > y is x == +Inf, when y is very large. */
5599 if (HONOR_INFINITIES (mode))
5600 return fold (build2 (EQ_EXPR, type, arg,
5601 build_real (TREE_TYPE (arg), c2)));
5603 /* sqrt(x) > y is always false, when y is very large
5604 and we don't care about infinities. */
5605 return omit_one_operand (type, integer_zero_node, arg);
5608 /* sqrt(x) > c is the same as x > c*c. */
5609 return fold (build2 (code, type, arg,
5610 build_real (TREE_TYPE (arg), c2)));
5612 else if (code == LT_EXPR || code == LE_EXPR)
5616 REAL_ARITHMETIC (c2, MULT_EXPR, c, c);
5617 real_convert (&c2, mode, &c2);
5619 if (REAL_VALUE_ISINF (c2))
5621 /* sqrt(x) < y is always true, when y is a very large
5622 value and we don't care about NaNs or Infinities. */
5623 if (! HONOR_NANS (mode) && ! HONOR_INFINITIES (mode))
5624 return omit_one_operand (type, integer_one_node, arg);
5626 /* sqrt(x) < y is x != +Inf when y is very large and we
5627 don't care about NaNs. */
5628 if (! HONOR_NANS (mode))
5629 return fold (build2 (NE_EXPR, type, arg,
5630 build_real (TREE_TYPE (arg), c2)));
5632 /* sqrt(x) < y is x >= 0 when y is very large and we
5633 don't care about Infinities. */
5634 if (! HONOR_INFINITIES (mode))
5635 return fold (build2 (GE_EXPR, type, arg,
5636 build_real (TREE_TYPE (arg), dconst0)));
5638 /* sqrt(x) < y is x >= 0 && x != +Inf, when y is large. */
5639 if (lang_hooks.decls.global_bindings_p () != 0
5640 || CONTAINS_PLACEHOLDER_P (arg))
5643 arg = save_expr (arg);
5644 return fold (build2 (TRUTH_ANDIF_EXPR, type,
5645 fold (build2 (GE_EXPR, type, arg,
5646 build_real (TREE_TYPE (arg),
5648 fold (build2 (NE_EXPR, type, arg,
5649 build_real (TREE_TYPE (arg),
5653 /* sqrt(x) < c is the same as x < c*c, if we ignore NaNs. */
5654 if (! HONOR_NANS (mode))
5655 return fold (build2 (code, type, arg,
5656 build_real (TREE_TYPE (arg), c2)));
5658 /* sqrt(x) < c is the same as x >= 0 && x < c*c. */
5659 if (lang_hooks.decls.global_bindings_p () == 0
5660 && ! CONTAINS_PLACEHOLDER_P (arg))
5662 arg = save_expr (arg);
5663 return fold (build2 (TRUTH_ANDIF_EXPR, type,
5664 fold (build2 (GE_EXPR, type, arg,
5665 build_real (TREE_TYPE (arg),
5667 fold (build2 (code, type, arg,
5668 build_real (TREE_TYPE (arg),
5677 /* Subroutine of fold() that optimizes comparisons against Infinities,
5678 either +Inf or -Inf.
5680 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
5681 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
5682 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
5684 The function returns the constant folded tree if a simplification
5685 can be made, and NULL_TREE otherwise. */
5688 fold_inf_compare (enum tree_code code, tree type, tree arg0, tree arg1)
5690 enum machine_mode mode;
5691 REAL_VALUE_TYPE max;
5695 mode = TYPE_MODE (TREE_TYPE (arg0));
5697 /* For negative infinity swap the sense of the comparison. */
5698 neg = REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1));
5700 code = swap_tree_comparison (code);
5705 /* x > +Inf is always false, if with ignore sNANs. */
5706 if (HONOR_SNANS (mode))
5708 return omit_one_operand (type, integer_zero_node, arg0);
5711 /* x <= +Inf is always true, if we don't case about NaNs. */
5712 if (! HONOR_NANS (mode))
5713 return omit_one_operand (type, integer_one_node, arg0);
5715 /* x <= +Inf is the same as x == x, i.e. isfinite(x). */
5716 if (lang_hooks.decls.global_bindings_p () == 0
5717 && ! CONTAINS_PLACEHOLDER_P (arg0))
5719 arg0 = save_expr (arg0);
5720 return fold (build2 (EQ_EXPR, type, arg0, arg0));
5726 /* x == +Inf and x >= +Inf are always equal to x > DBL_MAX. */
5727 real_maxval (&max, neg, mode);
5728 return fold (build2 (neg ? LT_EXPR : GT_EXPR, type,
5729 arg0, build_real (TREE_TYPE (arg0), max)));
5732 /* x < +Inf is always equal to x <= DBL_MAX. */
5733 real_maxval (&max, neg, mode);
5734 return fold (build2 (neg ? GE_EXPR : LE_EXPR, type,
5735 arg0, build_real (TREE_TYPE (arg0), max)));
5738 /* x != +Inf is always equal to !(x > DBL_MAX). */
5739 real_maxval (&max, neg, mode);
5740 if (! HONOR_NANS (mode))
5741 return fold (build2 (neg ? GE_EXPR : LE_EXPR, type,
5742 arg0, build_real (TREE_TYPE (arg0), max)));
5744 /* The transformation below creates non-gimple code and thus is
5745 not appropriate if we are in gimple form. */
5749 temp = fold (build2 (neg ? LT_EXPR : GT_EXPR, type,
5750 arg0, build_real (TREE_TYPE (arg0), max)));
5751 return fold (build1 (TRUTH_NOT_EXPR, type, temp));
5760 /* Subroutine of fold() that optimizes comparisons of a division by
5761 a nonzero integer constant against an integer constant, i.e.
5764 CODE is the comparison operator: EQ_EXPR, NE_EXPR, GT_EXPR, LT_EXPR,
5765 GE_EXPR or LE_EXPR. TYPE is the type of the result and ARG0 and ARG1
5766 are the operands of the comparison. ARG1 must be a TREE_REAL_CST.
5768 The function returns the constant folded tree if a simplification
5769 can be made, and NULL_TREE otherwise. */
5772 fold_div_compare (enum tree_code code, tree type, tree arg0, tree arg1)
5774 tree prod, tmp, hi, lo;
5775 tree arg00 = TREE_OPERAND (arg0, 0);
5776 tree arg01 = TREE_OPERAND (arg0, 1);
5777 unsigned HOST_WIDE_INT lpart;
5778 HOST_WIDE_INT hpart;
5781 /* We have to do this the hard way to detect unsigned overflow.
5782 prod = int_const_binop (MULT_EXPR, arg01, arg1, 0); */
5783 overflow = mul_double (TREE_INT_CST_LOW (arg01),
5784 TREE_INT_CST_HIGH (arg01),
5785 TREE_INT_CST_LOW (arg1),
5786 TREE_INT_CST_HIGH (arg1), &lpart, &hpart);
5787 prod = build_int_cst_wide (TREE_TYPE (arg00), lpart, hpart);
5788 prod = force_fit_type (prod, -1, overflow, false);
5790 if (TYPE_UNSIGNED (TREE_TYPE (arg0)))
5792 tmp = int_const_binop (MINUS_EXPR, arg01, integer_one_node, 0);
5795 /* Likewise hi = int_const_binop (PLUS_EXPR, prod, tmp, 0). */
5796 overflow = add_double (TREE_INT_CST_LOW (prod),
5797 TREE_INT_CST_HIGH (prod),
5798 TREE_INT_CST_LOW (tmp),
5799 TREE_INT_CST_HIGH (tmp),
5801 hi = build_int_cst_wide (TREE_TYPE (arg00), lpart, hpart);
5802 hi = force_fit_type (hi, -1, overflow | TREE_OVERFLOW (prod),
5803 TREE_CONSTANT_OVERFLOW (prod));
5805 else if (tree_int_cst_sgn (arg01) >= 0)
5807 tmp = int_const_binop (MINUS_EXPR, arg01, integer_one_node, 0);
5808 switch (tree_int_cst_sgn (arg1))
5811 lo = int_const_binop (MINUS_EXPR, prod, tmp, 0);
5816 lo = fold_negate_const (tmp, TREE_TYPE (arg0));
5821 hi = int_const_binop (PLUS_EXPR, prod, tmp, 0);
5831 /* A negative divisor reverses the relational operators. */
5832 code = swap_tree_comparison (code);
5834 tmp = int_const_binop (PLUS_EXPR, arg01, integer_one_node, 0);
5835 switch (tree_int_cst_sgn (arg1))
5838 hi = int_const_binop (MINUS_EXPR, prod, tmp, 0);
5843 hi = fold_negate_const (tmp, TREE_TYPE (arg0));
5848 lo = int_const_binop (PLUS_EXPR, prod, tmp, 0);
5860 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
5861 return omit_one_operand (type, integer_zero_node, arg00);
5862 if (TREE_OVERFLOW (hi))
5863 return fold (build2 (GE_EXPR, type, arg00, lo));
5864 if (TREE_OVERFLOW (lo))
5865 return fold (build2 (LE_EXPR, type, arg00, hi));
5866 return build_range_check (type, arg00, 1, lo, hi);
5869 if (TREE_OVERFLOW (lo) && TREE_OVERFLOW (hi))
5870 return omit_one_operand (type, integer_one_node, arg00);
5871 if (TREE_OVERFLOW (hi))
5872 return fold (build2 (LT_EXPR, type, arg00, lo));
5873 if (TREE_OVERFLOW (lo))
5874 return fold (build2 (GT_EXPR, type, arg00, hi));
5875 return build_range_check (type, arg00, 0, lo, hi);
5878 if (TREE_OVERFLOW (lo))
5879 return omit_one_operand (type, integer_zero_node, arg00);
5880 return fold (build2 (LT_EXPR, type, arg00, lo));
5883 if (TREE_OVERFLOW (hi))
5884 return omit_one_operand (type, integer_one_node, arg00);
5885 return fold (build2 (LE_EXPR, type, arg00, hi));
5888 if (TREE_OVERFLOW (hi))
5889 return omit_one_operand (type, integer_zero_node, arg00);
5890 return fold (build2 (GT_EXPR, type, arg00, hi));
5893 if (TREE_OVERFLOW (lo))
5894 return omit_one_operand (type, integer_one_node, arg00);
5895 return fold (build2 (GE_EXPR, type, arg00, lo));
5905 /* If CODE with arguments ARG0 and ARG1 represents a single bit
5906 equality/inequality test, then return a simplified form of
5907 the test using shifts and logical operations. Otherwise return
5908 NULL. TYPE is the desired result type. */
5911 fold_single_bit_test (enum tree_code code, tree arg0, tree arg1,
5914 /* If this is testing a single bit, we can optimize the test. */
5915 if ((code == NE_EXPR || code == EQ_EXPR)
5916 && TREE_CODE (arg0) == BIT_AND_EXPR && integer_zerop (arg1)
5917 && integer_pow2p (TREE_OPERAND (arg0, 1)))
5919 tree inner = TREE_OPERAND (arg0, 0);
5920 tree type = TREE_TYPE (arg0);
5921 int bitnum = tree_log2 (TREE_OPERAND (arg0, 1));
5922 enum machine_mode operand_mode = TYPE_MODE (type);
5924 tree signed_type, unsigned_type, intermediate_type;
5927 /* If we have (A & C) != 0 where C is the sign bit of A, convert
5928 this into A < 0. Similarly for (A & C) == 0 into A >= 0. */
5929 arg00 = sign_bit_p (TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1));
5930 if (arg00 != NULL_TREE
5931 /* This is only a win if casting to a signed type is cheap,
5932 i.e. when arg00's type is not a partial mode. */
5933 && TYPE_PRECISION (TREE_TYPE (arg00))
5934 == GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg00))))
5936 tree stype = lang_hooks.types.signed_type (TREE_TYPE (arg00));
5937 return fold (build2 (code == EQ_EXPR ? GE_EXPR : LT_EXPR,
5938 result_type, fold_convert (stype, arg00),
5939 fold_convert (stype, integer_zero_node)));
5942 /* Otherwise we have (A & C) != 0 where C is a single bit,
5943 convert that into ((A >> C2) & 1). Where C2 = log2(C).
5944 Similarly for (A & C) == 0. */
5946 /* If INNER is a right shift of a constant and it plus BITNUM does
5947 not overflow, adjust BITNUM and INNER. */
5948 if (TREE_CODE (inner) == RSHIFT_EXPR
5949 && TREE_CODE (TREE_OPERAND (inner, 1)) == INTEGER_CST
5950 && TREE_INT_CST_HIGH (TREE_OPERAND (inner, 1)) == 0
5951 && bitnum < TYPE_PRECISION (type)
5952 && 0 > compare_tree_int (TREE_OPERAND (inner, 1),
5953 bitnum - TYPE_PRECISION (type)))
5955 bitnum += TREE_INT_CST_LOW (TREE_OPERAND (inner, 1));
5956 inner = TREE_OPERAND (inner, 0);
5959 /* If we are going to be able to omit the AND below, we must do our
5960 operations as unsigned. If we must use the AND, we have a choice.
5961 Normally unsigned is faster, but for some machines signed is. */
5962 #ifdef LOAD_EXTEND_OP
5963 ops_unsigned = (LOAD_EXTEND_OP (operand_mode) == SIGN_EXTEND
5964 && !flag_syntax_only) ? 0 : 1;
5969 signed_type = lang_hooks.types.type_for_mode (operand_mode, 0);
5970 unsigned_type = lang_hooks.types.type_for_mode (operand_mode, 1);
5971 intermediate_type = ops_unsigned ? unsigned_type : signed_type;
5972 inner = fold_convert (intermediate_type, inner);
5975 inner = build2 (RSHIFT_EXPR, intermediate_type,
5976 inner, size_int (bitnum));
5978 if (code == EQ_EXPR)
5979 inner = fold (build2 (BIT_XOR_EXPR, intermediate_type,
5980 inner, integer_one_node));
5982 /* Put the AND last so it can combine with more things. */
5983 inner = build2 (BIT_AND_EXPR, intermediate_type,
5984 inner, integer_one_node);
5986 /* Make sure to return the proper type. */
5987 inner = fold_convert (result_type, inner);
5994 /* Check whether we are allowed to reorder operands arg0 and arg1,
5995 such that the evaluation of arg1 occurs before arg0. */
5998 reorder_operands_p (tree arg0, tree arg1)
6000 if (! flag_evaluation_order)
6002 if (TREE_CONSTANT (arg0) || TREE_CONSTANT (arg1))
6004 return ! TREE_SIDE_EFFECTS (arg0)
6005 && ! TREE_SIDE_EFFECTS (arg1);
6008 /* Test whether it is preferable two swap two operands, ARG0 and
6009 ARG1, for example because ARG0 is an integer constant and ARG1
6010 isn't. If REORDER is true, only recommend swapping if we can
6011 evaluate the operands in reverse order. */
6014 tree_swap_operands_p (tree arg0, tree arg1, bool reorder)
6016 STRIP_SIGN_NOPS (arg0);
6017 STRIP_SIGN_NOPS (arg1);
6019 if (TREE_CODE (arg1) == INTEGER_CST)
6021 if (TREE_CODE (arg0) == INTEGER_CST)
6024 if (TREE_CODE (arg1) == REAL_CST)
6026 if (TREE_CODE (arg0) == REAL_CST)
6029 if (TREE_CODE (arg1) == COMPLEX_CST)
6031 if (TREE_CODE (arg0) == COMPLEX_CST)
6034 if (TREE_CONSTANT (arg1))
6036 if (TREE_CONSTANT (arg0))
6042 if (reorder && flag_evaluation_order
6043 && (TREE_SIDE_EFFECTS (arg0) || TREE_SIDE_EFFECTS (arg1)))
6051 /* It is preferable to swap two SSA_NAME to ensure a canonical form
6052 for commutative and comparison operators. Ensuring a canonical
6053 form allows the optimizers to find additional redundancies without
6054 having to explicitly check for both orderings. */
6055 if (TREE_CODE (arg0) == SSA_NAME
6056 && TREE_CODE (arg1) == SSA_NAME
6057 && SSA_NAME_VERSION (arg0) > SSA_NAME_VERSION (arg1))
6063 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where
6064 ARG0 is extended to a wider type. */
6067 fold_widened_comparison (enum tree_code code, tree type, tree arg0, tree arg1)
6069 tree arg0_unw = get_unwidened (arg0, NULL_TREE);
6071 tree shorter_type, outer_type;
6075 if (arg0_unw == arg0)
6077 shorter_type = TREE_TYPE (arg0_unw);
6079 if (TYPE_PRECISION (TREE_TYPE (arg0)) <= TYPE_PRECISION (shorter_type))
6082 arg1_unw = get_unwidened (arg1, shorter_type);
6086 /* If possible, express the comparison in the shorter mode. */
6087 if ((code == EQ_EXPR || code == NE_EXPR
6088 || TYPE_UNSIGNED (TREE_TYPE (arg0)) == TYPE_UNSIGNED (shorter_type))
6089 && (TREE_TYPE (arg1_unw) == shorter_type
6090 || (TREE_CODE (arg1_unw) == INTEGER_CST
6091 && TREE_CODE (shorter_type) == INTEGER_TYPE
6092 && int_fits_type_p (arg1_unw, shorter_type))))
6093 return fold (build (code, type, arg0_unw,
6094 fold_convert (shorter_type, arg1_unw)));
6096 if (TREE_CODE (arg1_unw) != INTEGER_CST)
6099 /* If we are comparing with the integer that does not fit into the range
6100 of the shorter type, the result is known. */
6101 outer_type = TREE_TYPE (arg1_unw);
6102 min = lower_bound_in_type (outer_type, shorter_type);
6103 max = upper_bound_in_type (outer_type, shorter_type);
6105 above = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6107 below = integer_nonzerop (fold_relational_const (LT_EXPR, type,
6114 return omit_one_operand (type, integer_zero_node, arg0);
6119 return omit_one_operand (type, integer_one_node, arg0);
6125 return omit_one_operand (type, integer_one_node, arg0);
6127 return omit_one_operand (type, integer_zero_node, arg0);
6132 return omit_one_operand (type, integer_zero_node, arg0);
6134 return omit_one_operand (type, integer_one_node, arg0);
6143 /* Fold comparison ARG0 CODE ARG1 (with result in TYPE), where for
6144 ARG0 just the signedness is changed. */
6147 fold_sign_changed_comparison (enum tree_code code, tree type,
6148 tree arg0, tree arg1)
6150 tree arg0_inner, tmp;
6151 tree inner_type, outer_type;
6153 if (TREE_CODE (arg0) != NOP_EXPR)
6156 outer_type = TREE_TYPE (arg0);
6157 arg0_inner = TREE_OPERAND (arg0, 0);
6158 inner_type = TREE_TYPE (arg0_inner);
6160 if (TYPE_PRECISION (inner_type) != TYPE_PRECISION (outer_type))
6163 if (TREE_CODE (arg1) != INTEGER_CST
6164 && !(TREE_CODE (arg1) == NOP_EXPR
6165 && TREE_TYPE (TREE_OPERAND (arg1, 0)) == inner_type))
6168 if (TYPE_UNSIGNED (inner_type) != TYPE_UNSIGNED (outer_type)
6173 if (TREE_CODE (arg1) == INTEGER_CST)
6175 tmp = build_int_cst_wide (inner_type,
6176 TREE_INT_CST_LOW (arg1),
6177 TREE_INT_CST_HIGH (arg1));
6178 arg1 = force_fit_type (tmp, 0,
6179 TREE_OVERFLOW (arg1),
6180 TREE_CONSTANT_OVERFLOW (arg1));
6183 arg1 = fold_convert (inner_type, arg1);
6185 return fold (build (code, type, arg0_inner, arg1));
6188 /* Tries to replace &a[idx] CODE s * delta with &a[idx CODE delta], if s is
6189 step of the array. ADDR is the address. MULT is the multiplicative expression.
6190 If the function succeeds, the new address expression is returned. Otherwise
6191 NULL_TREE is returned. */
6194 try_move_mult_to_index (enum tree_code code, tree addr, tree mult)
6196 tree s, delta, step;
6197 tree arg0 = TREE_OPERAND (mult, 0), arg1 = TREE_OPERAND (mult, 1);
6198 tree ref = TREE_OPERAND (addr, 0), pref;
6205 if (TREE_CODE (arg0) == INTEGER_CST)
6210 else if (TREE_CODE (arg1) == INTEGER_CST)
6218 for (;; ref = TREE_OPERAND (ref, 0))
6220 if (TREE_CODE (ref) == ARRAY_REF)
6222 step = array_ref_element_size (ref);
6224 if (TREE_CODE (step) != INTEGER_CST)
6227 itype = TREE_TYPE (step);
6229 /* If the type sizes do not match, we might run into problems
6230 when one of them would overflow. */
6231 if (TYPE_PRECISION (itype) != TYPE_PRECISION (TREE_TYPE (s)))
6234 if (!operand_equal_p (step, fold_convert (itype, s), 0))
6237 delta = fold_convert (itype, delta);
6241 if (!handled_component_p (ref))
6245 /* We found the suitable array reference. So copy everything up to it,
6246 and replace the index. */
6248 pref = TREE_OPERAND (addr, 0);
6249 ret = copy_node (pref);
6254 pref = TREE_OPERAND (pref, 0);
6255 TREE_OPERAND (pos, 0) = copy_node (pref);
6256 pos = TREE_OPERAND (pos, 0);
6259 TREE_OPERAND (pos, 1) = fold (build2 (code, itype,
6260 TREE_OPERAND (pos, 1),
6263 return build1 (ADDR_EXPR, TREE_TYPE (addr), ret);
6267 /* Fold A < X && A + 1 > Y to A < X && A >= Y. Normally A + 1 > Y
6268 means A >= Y && A != MAX, but in this case we know that
6269 A < X <= MAX. INEQ is A + 1 > Y, BOUND is A < X. */
6272 fold_to_nonsharp_ineq_using_bound (tree ineq, tree bound)
6274 tree a, typea, type = TREE_TYPE (ineq), a1, diff, y;
6276 if (TREE_CODE (bound) == LT_EXPR)
6277 a = TREE_OPERAND (bound, 0);
6278 else if (TREE_CODE (bound) == GT_EXPR)
6279 a = TREE_OPERAND (bound, 1);
6283 typea = TREE_TYPE (a);
6284 if (!INTEGRAL_TYPE_P (typea)
6285 && !POINTER_TYPE_P (typea))
6288 if (TREE_CODE (ineq) == LT_EXPR)
6290 a1 = TREE_OPERAND (ineq, 1);
6291 y = TREE_OPERAND (ineq, 0);
6293 else if (TREE_CODE (ineq) == GT_EXPR)
6295 a1 = TREE_OPERAND (ineq, 0);
6296 y = TREE_OPERAND (ineq, 1);
6301 if (TREE_TYPE (a1) != typea)
6304 diff = fold (build2 (MINUS_EXPR, typea, a1, a));
6305 if (!integer_onep (diff))
6308 return fold (build2 (GE_EXPR, type, a, y));
6311 /* Fold complex addition when both components are accessible by parts.
6312 Return non-null if successful. CODE should be PLUS_EXPR for addition,
6313 or MINUS_EXPR for subtraction. */
6316 fold_complex_add (tree type, tree ac, tree bc, enum tree_code code)
6318 tree ar, ai, br, bi, rr, ri, inner_type;
6320 if (TREE_CODE (ac) == COMPLEX_EXPR)
6321 ar = TREE_OPERAND (ac, 0), ai = TREE_OPERAND (ac, 1);
6322 else if (TREE_CODE (ac) == COMPLEX_CST)
6323 ar = TREE_REALPART (ac), ai = TREE_IMAGPART (ac);
6327 if (TREE_CODE (bc) == COMPLEX_EXPR)
6328 br = TREE_OPERAND (bc, 0), bi = TREE_OPERAND (bc, 1);
6329 else if (TREE_CODE (bc) == COMPLEX_CST)
6330 br = TREE_REALPART (bc), bi = TREE_IMAGPART (bc);
6334 inner_type = TREE_TYPE (type);
6336 rr = fold (build2 (code, inner_type, ar, br));
6337 ri = fold (build2 (code, inner_type, ai, bi));
6339 return fold (build2 (COMPLEX_EXPR, type, rr, ri));
6342 /* Perform some simplifications of complex multiplication when one or more
6343 of the components are constants or zeros. Return non-null if successful. */
6346 fold_complex_mult_parts (tree type, tree ar, tree ai, tree br, tree bi)
6348 tree rr, ri, inner_type, zero;
6349 bool ar0, ai0, br0, bi0, bi1;
6351 inner_type = TREE_TYPE (type);
6354 if (SCALAR_FLOAT_TYPE_P (inner_type))
6356 ar0 = ai0 = br0 = bi0 = bi1 = false;
6358 /* We're only interested in +0.0 here, thus we don't use real_zerop. */
6360 if (TREE_CODE (ar) == REAL_CST
6361 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (ar), dconst0))
6362 ar0 = true, zero = ar;
6364 if (TREE_CODE (ai) == REAL_CST
6365 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (ai), dconst0))
6366 ai0 = true, zero = ai;
6368 if (TREE_CODE (br) == REAL_CST
6369 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (br), dconst0))
6370 br0 = true, zero = br;
6372 if (TREE_CODE (bi) == REAL_CST)
6374 if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (bi), dconst0))
6375 bi0 = true, zero = bi;
6376 else if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (bi), dconst1))
6382 ar0 = integer_zerop (ar);
6385 ai0 = integer_zerop (ai);
6388 br0 = integer_zerop (br);
6391 bi0 = integer_zerop (bi);
6398 bi1 = integer_onep (bi);
6401 /* We won't optimize anything below unless something is zero. */
6405 if (ai0 && br0 && bi1)
6410 else if (ai0 && bi0)
6412 rr = fold (build2 (MULT_EXPR, inner_type, ar, br));
6415 else if (ai0 && br0)
6418 ri = fold (build2 (MULT_EXPR, inner_type, ar, bi));
6420 else if (ar0 && bi0)
6423 ri = fold (build2 (MULT_EXPR, inner_type, ai, br));
6425 else if (ar0 && br0)
6427 rr = fold (build2 (MULT_EXPR, inner_type, ai, bi));
6428 rr = fold (build1 (NEGATE_EXPR, inner_type, rr));
6433 rr = fold (build2 (MULT_EXPR, inner_type, ar, br));
6434 ri = fold (build2 (MULT_EXPR, inner_type, ai, br));
6438 rr = fold (build2 (MULT_EXPR, inner_type, ar, br));
6439 ri = fold (build2 (MULT_EXPR, inner_type, ar, bi));
6443 rr = fold (build2 (MULT_EXPR, inner_type, ai, bi));
6444 rr = fold (build1 (NEGATE_EXPR, inner_type, rr));
6445 ri = fold (build2 (MULT_EXPR, inner_type, ar, bi));
6449 rr = fold (build2 (MULT_EXPR, inner_type, ai, bi));
6450 rr = fold (build1 (NEGATE_EXPR, inner_type, rr));
6451 ri = fold (build2 (MULT_EXPR, inner_type, ai, br));
6456 return fold (build2 (COMPLEX_EXPR, type, rr, ri));
6460 fold_complex_mult (tree type, tree ac, tree bc)
6462 tree ar, ai, br, bi;
6464 if (TREE_CODE (ac) == COMPLEX_EXPR)
6465 ar = TREE_OPERAND (ac, 0), ai = TREE_OPERAND (ac, 1);
6466 else if (TREE_CODE (ac) == COMPLEX_CST)
6467 ar = TREE_REALPART (ac), ai = TREE_IMAGPART (ac);
6471 if (TREE_CODE (bc) == COMPLEX_EXPR)
6472 br = TREE_OPERAND (bc, 0), bi = TREE_OPERAND (bc, 1);
6473 else if (TREE_CODE (bc) == COMPLEX_CST)
6474 br = TREE_REALPART (bc), bi = TREE_IMAGPART (bc);
6478 return fold_complex_mult_parts (type, ar, ai, br, bi);
6481 /* Perform some simplifications of complex division when one or more of
6482 the components are constants or zeros. Return non-null if successful. */
6485 fold_complex_div_parts (tree type, tree ar, tree ai, tree br, tree bi,
6486 enum tree_code code)
6488 tree rr, ri, inner_type, zero;
6489 bool ar0, ai0, br0, bi0, bi1;
6491 inner_type = TREE_TYPE (type);
6494 if (SCALAR_FLOAT_TYPE_P (inner_type))
6496 ar0 = ai0 = br0 = bi0 = bi1 = false;
6498 /* We're only interested in +0.0 here, thus we don't use real_zerop. */
6500 if (TREE_CODE (ar) == REAL_CST
6501 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (ar), dconst0))
6502 ar0 = true, zero = ar;
6504 if (TREE_CODE (ai) == REAL_CST
6505 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (ai), dconst0))
6506 ai0 = true, zero = ai;
6508 if (TREE_CODE (br) == REAL_CST
6509 && REAL_VALUES_IDENTICAL (TREE_REAL_CST (br), dconst0))
6510 br0 = true, zero = br;
6512 if (TREE_CODE (bi) == REAL_CST)
6514 if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (bi), dconst0))
6515 bi0 = true, zero = bi;
6516 else if (REAL_VALUES_IDENTICAL (TREE_REAL_CST (bi), dconst1))
6522 ar0 = integer_zerop (ar);
6525 ai0 = integer_zerop (ai);
6528 br0 = integer_zerop (br);
6531 bi0 = integer_zerop (bi);
6538 bi1 = integer_onep (bi);
6541 /* We won't optimize anything below unless something is zero. */
6547 rr = fold (build2 (code, inner_type, ar, br));
6550 else if (ai0 && br0)
6553 ri = fold (build2 (code, inner_type, ar, bi));
6554 ri = fold (build1 (NEGATE_EXPR, inner_type, ri));
6556 else if (ar0 && bi0)
6559 ri = fold (build2 (code, inner_type, ai, br));
6561 else if (ar0 && br0)
6563 rr = fold (build2 (code, inner_type, ai, bi));
6568 rr = fold (build2 (code, inner_type, ar, br));
6569 ri = fold (build2 (code, inner_type, ai, br));
6573 rr = fold (build2 (code, inner_type, ai, bi));
6574 ri = fold (build2 (code, inner_type, ar, bi));
6575 ri = fold (build1 (NEGATE_EXPR, inner_type, ri));
6580 return fold (build2 (COMPLEX_EXPR, type, rr, ri));
6584 fold_complex_div (tree type, tree ac, tree bc, enum tree_code code)
6586 tree ar, ai, br, bi;
6588 if (TREE_CODE (ac) == COMPLEX_EXPR)
6589 ar = TREE_OPERAND (ac, 0), ai = TREE_OPERAND (ac, 1);
6590 else if (TREE_CODE (ac) == COMPLEX_CST)
6591 ar = TREE_REALPART (ac), ai = TREE_IMAGPART (ac);
6595 if (TREE_CODE (bc) == COMPLEX_EXPR)
6596 br = TREE_OPERAND (bc, 0), bi = TREE_OPERAND (bc, 1);
6597 else if (TREE_CODE (bc) == COMPLEX_CST)
6598 br = TREE_REALPART (bc), bi = TREE_IMAGPART (bc);
6602 return fold_complex_div_parts (type, ar, ai, br, bi, code);
6605 /* Fold a unary expression EXPR. Return the folded expression if
6606 folding is successful. Otherwise, return the original
6610 fold_unary (tree expr)
6612 const tree t = expr;
6613 const tree type = TREE_TYPE (expr);
6616 enum tree_code code = TREE_CODE (t);
6617 enum tree_code_class kind = TREE_CODE_CLASS (code);
6619 gcc_assert (IS_EXPR_CODE_CLASS (kind)
6620 && TREE_CODE_LENGTH (code) == 1);
6623 arg0 = op0 = TREE_OPERAND (t, 0);
6626 if (code == NOP_EXPR || code == FLOAT_EXPR || code == CONVERT_EXPR)
6628 /* Don't use STRIP_NOPS, because signedness of argument type matters. */
6629 STRIP_SIGN_NOPS (arg0);
6633 /* Strip any conversions that don't change the mode. This
6634 is safe for every expression, except for a comparison
6635 expression because its signedness is derived from its
6638 Note that this is done as an internal manipulation within
6639 the constant folder, in order to find the simplest
6640 representation of the arguments so that their form can be
6641 studied. In any cases, the appropriate type conversions
6642 should be put back in the tree that will get out of the
6648 if (TREE_CODE_CLASS (code) == tcc_unary)
6650 if (TREE_CODE (arg0) == COMPOUND_EXPR)
6651 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
6652 fold (build1 (code, type, TREE_OPERAND (arg0, 1))));
6653 else if (TREE_CODE (arg0) == COND_EXPR)
6655 tree arg01 = TREE_OPERAND (arg0, 1);
6656 tree arg02 = TREE_OPERAND (arg0, 2);
6657 if (! VOID_TYPE_P (TREE_TYPE (arg01)))
6658 arg01 = fold (build1 (code, type, arg01));
6659 if (! VOID_TYPE_P (TREE_TYPE (arg02)))
6660 arg02 = fold (build1 (code, type, arg02));
6661 tem = fold (build3 (COND_EXPR, type, TREE_OPERAND (arg0, 0),
6664 /* If this was a conversion, and all we did was to move into
6665 inside the COND_EXPR, bring it back out. But leave it if
6666 it is a conversion from integer to integer and the
6667 result precision is no wider than a word since such a
6668 conversion is cheap and may be optimized away by combine,
6669 while it couldn't if it were outside the COND_EXPR. Then return
6670 so we don't get into an infinite recursion loop taking the
6671 conversion out and then back in. */
6673 if ((code == NOP_EXPR || code == CONVERT_EXPR
6674 || code == NON_LVALUE_EXPR)
6675 && TREE_CODE (tem) == COND_EXPR
6676 && TREE_CODE (TREE_OPERAND (tem, 1)) == code
6677 && TREE_CODE (TREE_OPERAND (tem, 2)) == code
6678 && ! VOID_TYPE_P (TREE_OPERAND (tem, 1))
6679 && ! VOID_TYPE_P (TREE_OPERAND (tem, 2))
6680 && (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))
6681 == TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 2), 0)))
6682 && (! (INTEGRAL_TYPE_P (TREE_TYPE (tem))
6684 (TREE_TYPE (TREE_OPERAND (TREE_OPERAND (tem, 1), 0))))
6685 && TYPE_PRECISION (TREE_TYPE (tem)) <= BITS_PER_WORD)
6686 || flag_syntax_only))
6687 tem = build1 (code, type,
6689 TREE_TYPE (TREE_OPERAND
6690 (TREE_OPERAND (tem, 1), 0)),
6691 TREE_OPERAND (tem, 0),
6692 TREE_OPERAND (TREE_OPERAND (tem, 1), 0),
6693 TREE_OPERAND (TREE_OPERAND (tem, 2), 0)));
6696 else if (COMPARISON_CLASS_P (arg0))
6698 if (TREE_CODE (type) == BOOLEAN_TYPE)
6700 arg0 = copy_node (arg0);
6701 TREE_TYPE (arg0) = type;
6704 else if (TREE_CODE (type) != INTEGER_TYPE)
6705 return fold (build3 (COND_EXPR, type, arg0,
6706 fold (build1 (code, type,
6708 fold (build1 (code, type,
6709 integer_zero_node))));
6718 case FIX_TRUNC_EXPR:
6720 case FIX_FLOOR_EXPR:
6721 case FIX_ROUND_EXPR:
6722 if (TREE_TYPE (op0) == type)
6725 /* Handle cases of two conversions in a row. */
6726 if (TREE_CODE (op0) == NOP_EXPR
6727 || TREE_CODE (op0) == CONVERT_EXPR)
6729 tree inside_type = TREE_TYPE (TREE_OPERAND (op0, 0));
6730 tree inter_type = TREE_TYPE (op0);
6731 int inside_int = INTEGRAL_TYPE_P (inside_type);
6732 int inside_ptr = POINTER_TYPE_P (inside_type);
6733 int inside_float = FLOAT_TYPE_P (inside_type);
6734 unsigned int inside_prec = TYPE_PRECISION (inside_type);
6735 int inside_unsignedp = TYPE_UNSIGNED (inside_type);
6736 int inter_int = INTEGRAL_TYPE_P (inter_type);
6737 int inter_ptr = POINTER_TYPE_P (inter_type);
6738 int inter_float = FLOAT_TYPE_P (inter_type);
6739 unsigned int inter_prec = TYPE_PRECISION (inter_type);
6740 int inter_unsignedp = TYPE_UNSIGNED (inter_type);
6741 int final_int = INTEGRAL_TYPE_P (type);
6742 int final_ptr = POINTER_TYPE_P (type);
6743 int final_float = FLOAT_TYPE_P (type);
6744 unsigned int final_prec = TYPE_PRECISION (type);
6745 int final_unsignedp = TYPE_UNSIGNED (type);
6747 /* In addition to the cases of two conversions in a row
6748 handled below, if we are converting something to its own
6749 type via an object of identical or wider precision, neither
6750 conversion is needed. */
6751 if (TYPE_MAIN_VARIANT (inside_type) == TYPE_MAIN_VARIANT (type)
6752 && ((inter_int && final_int) || (inter_float && final_float))
6753 && inter_prec >= final_prec)
6754 return fold (build1 (code, type, TREE_OPERAND (op0, 0)));
6756 /* Likewise, if the intermediate and final types are either both
6757 float or both integer, we don't need the middle conversion if
6758 it is wider than the final type and doesn't change the signedness
6759 (for integers). Avoid this if the final type is a pointer
6760 since then we sometimes need the inner conversion. Likewise if
6761 the outer has a precision not equal to the size of its mode. */
6762 if ((((inter_int || inter_ptr) && (inside_int || inside_ptr))
6763 || (inter_float && inside_float))
6764 && inter_prec >= inside_prec
6765 && (inter_float || inter_unsignedp == inside_unsignedp)
6766 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
6767 && TYPE_MODE (type) == TYPE_MODE (inter_type))
6769 return fold (build1 (code, type, TREE_OPERAND (op0, 0)));
6771 /* If we have a sign-extension of a zero-extended value, we can
6772 replace that by a single zero-extension. */
6773 if (inside_int && inter_int && final_int
6774 && inside_prec < inter_prec && inter_prec < final_prec
6775 && inside_unsignedp && !inter_unsignedp)
6776 return fold (build1 (code, type, TREE_OPERAND (op0, 0)));
6778 /* Two conversions in a row are not needed unless:
6779 - some conversion is floating-point (overstrict for now), or
6780 - the intermediate type is narrower than both initial and
6782 - the intermediate type and innermost type differ in signedness,
6783 and the outermost type is wider than the intermediate, or
6784 - the initial type is a pointer type and the precisions of the
6785 intermediate and final types differ, or
6786 - the final type is a pointer type and the precisions of the
6787 initial and intermediate types differ. */
6788 if (! inside_float && ! inter_float && ! final_float
6789 && (inter_prec > inside_prec || inter_prec > final_prec)
6790 && ! (inside_int && inter_int
6791 && inter_unsignedp != inside_unsignedp
6792 && inter_prec < final_prec)
6793 && ((inter_unsignedp && inter_prec > inside_prec)
6794 == (final_unsignedp && final_prec > inter_prec))
6795 && ! (inside_ptr && inter_prec != final_prec)
6796 && ! (final_ptr && inside_prec != inter_prec)
6797 && ! (final_prec != GET_MODE_BITSIZE (TYPE_MODE (type))
6798 && TYPE_MODE (type) == TYPE_MODE (inter_type))
6800 return fold (build1 (code, type, TREE_OPERAND (op0, 0)));
6803 if (TREE_CODE (op0) == MODIFY_EXPR
6804 && TREE_CONSTANT (TREE_OPERAND (op0, 1))
6805 /* Detect assigning a bitfield. */
6806 && !(TREE_CODE (TREE_OPERAND (op0, 0)) == COMPONENT_REF
6807 && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (op0, 0), 1))))
6809 /* Don't leave an assignment inside a conversion
6810 unless assigning a bitfield. */
6811 tem = build1 (code, type, TREE_OPERAND (op0, 1));
6812 /* First do the assignment, then return converted constant. */
6813 tem = build2 (COMPOUND_EXPR, TREE_TYPE (tem), op0, fold (tem));
6814 TREE_NO_WARNING (tem) = 1;
6815 TREE_USED (tem) = 1;
6819 /* Convert (T)(x & c) into (T)x & (T)c, if c is an integer
6820 constants (if x has signed type, the sign bit cannot be set
6821 in c). This folds extension into the BIT_AND_EXPR. */
6822 if (INTEGRAL_TYPE_P (type)
6823 && TREE_CODE (type) != BOOLEAN_TYPE
6824 && TREE_CODE (op0) == BIT_AND_EXPR
6825 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
6828 tree and0 = TREE_OPERAND (and, 0), and1 = TREE_OPERAND (and, 1);
6831 if (TYPE_UNSIGNED (TREE_TYPE (and))
6832 || (TYPE_PRECISION (type)
6833 <= TYPE_PRECISION (TREE_TYPE (and))))
6835 else if (TYPE_PRECISION (TREE_TYPE (and1))
6836 <= HOST_BITS_PER_WIDE_INT
6837 && host_integerp (and1, 1))
6839 unsigned HOST_WIDE_INT cst;
6841 cst = tree_low_cst (and1, 1);
6842 cst &= (HOST_WIDE_INT) -1
6843 << (TYPE_PRECISION (TREE_TYPE (and1)) - 1);
6844 change = (cst == 0);
6845 #ifdef LOAD_EXTEND_OP
6847 && !flag_syntax_only
6848 && (LOAD_EXTEND_OP (TYPE_MODE (TREE_TYPE (and0)))
6851 tree uns = lang_hooks.types.unsigned_type (TREE_TYPE (and0));
6852 and0 = fold_convert (uns, and0);
6853 and1 = fold_convert (uns, and1);
6859 tem = build_int_cst_wide (type, TREE_INT_CST_LOW (and1),
6860 TREE_INT_CST_HIGH (and1));
6861 tem = force_fit_type (tem, 0, TREE_OVERFLOW (and1),
6862 TREE_CONSTANT_OVERFLOW (and1));
6863 return fold (build2 (BIT_AND_EXPR, type,
6864 fold_convert (type, and0), tem));
6868 /* Convert (T1)((T2)X op Y) into (T1)X op Y, for pointer types T1 and
6869 T2 being pointers to types of the same size. */
6870 if (POINTER_TYPE_P (type)
6871 && BINARY_CLASS_P (arg0)
6872 && TREE_CODE (TREE_OPERAND (arg0, 0)) == NOP_EXPR
6873 && POINTER_TYPE_P (TREE_TYPE (TREE_OPERAND (arg0, 0))))
6875 tree arg00 = TREE_OPERAND (arg0, 0);
6877 tree t1 = TREE_TYPE (arg00);
6878 tree tt0 = TREE_TYPE (t0);
6879 tree tt1 = TREE_TYPE (t1);
6880 tree s0 = TYPE_SIZE (tt0);
6881 tree s1 = TYPE_SIZE (tt1);
6883 if (s0 && s1 && operand_equal_p (s0, s1, OEP_ONLY_CONST))
6884 return build2 (TREE_CODE (arg0), t0, fold_convert (t0, arg00),
6885 TREE_OPERAND (arg0, 1));
6888 tem = fold_convert_const (code, type, arg0);
6889 return tem ? tem : t;
6891 case VIEW_CONVERT_EXPR:
6892 if (TREE_CODE (op0) == VIEW_CONVERT_EXPR)
6893 return build1 (VIEW_CONVERT_EXPR, type, TREE_OPERAND (op0, 0));
6897 if (negate_expr_p (arg0))
6898 return fold_convert (type, negate_expr (arg0));
6899 /* Convert - (~A) to A + 1. */
6900 if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == BIT_NOT_EXPR)
6901 return fold (build2 (PLUS_EXPR, type, TREE_OPERAND (arg0, 0),
6902 build_int_cst (type, 1)));
6906 if (TREE_CODE (arg0) == INTEGER_CST || TREE_CODE (arg0) == REAL_CST)
6907 return fold_abs_const (arg0, type);
6908 else if (TREE_CODE (arg0) == NEGATE_EXPR)
6909 return fold (build1 (ABS_EXPR, type, TREE_OPERAND (arg0, 0)));
6910 /* Convert fabs((double)float) into (double)fabsf(float). */
6911 else if (TREE_CODE (arg0) == NOP_EXPR
6912 && TREE_CODE (type) == REAL_TYPE)
6914 tree targ0 = strip_float_extensions (arg0);
6916 return fold_convert (type, fold (build1 (ABS_EXPR,
6920 else if (tree_expr_nonnegative_p (arg0))
6923 /* Strip sign ops from argument. */
6924 if (TREE_CODE (type) == REAL_TYPE)
6926 tem = fold_strip_sign_ops (arg0);
6928 return fold (build1 (ABS_EXPR, type, fold_convert (type, tem)));
6933 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
6934 return fold_convert (type, arg0);
6935 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
6936 return build2 (COMPLEX_EXPR, type,
6937 TREE_OPERAND (arg0, 0),
6938 negate_expr (TREE_OPERAND (arg0, 1)));
6939 else if (TREE_CODE (arg0) == COMPLEX_CST)
6940 return build_complex (type, TREE_REALPART (arg0),
6941 negate_expr (TREE_IMAGPART (arg0)));
6942 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
6943 return fold (build2 (TREE_CODE (arg0), type,
6944 fold (build1 (CONJ_EXPR, type,
6945 TREE_OPERAND (arg0, 0))),
6946 fold (build1 (CONJ_EXPR, type,
6947 TREE_OPERAND (arg0, 1)))));
6948 else if (TREE_CODE (arg0) == CONJ_EXPR)
6949 return TREE_OPERAND (arg0, 0);
6953 if (TREE_CODE (arg0) == INTEGER_CST)
6954 return fold_not_const (arg0, type);
6955 else if (TREE_CODE (arg0) == BIT_NOT_EXPR)
6956 return TREE_OPERAND (arg0, 0);
6957 /* Convert ~ (-A) to A - 1. */
6958 else if (INTEGRAL_TYPE_P (type) && TREE_CODE (arg0) == NEGATE_EXPR)
6959 return fold (build2 (MINUS_EXPR, type, TREE_OPERAND (arg0, 0),
6960 build_int_cst (type, 1)));
6961 /* Convert ~ (A - 1) or ~ (A + -1) to -A. */
6962 else if (INTEGRAL_TYPE_P (type)
6963 && ((TREE_CODE (arg0) == MINUS_EXPR
6964 && integer_onep (TREE_OPERAND (arg0, 1)))
6965 || (TREE_CODE (arg0) == PLUS_EXPR
6966 && integer_all_onesp (TREE_OPERAND (arg0, 1)))))
6967 return fold (build1 (NEGATE_EXPR, type, TREE_OPERAND (arg0, 0)));
6970 case TRUTH_NOT_EXPR:
6971 /* The argument to invert_truthvalue must have Boolean type. */
6972 if (TREE_CODE (TREE_TYPE (arg0)) != BOOLEAN_TYPE)
6973 arg0 = fold_convert (boolean_type_node, arg0);
6975 /* Note that the operand of this must be an int
6976 and its values must be 0 or 1.
6977 ("true" is a fixed value perhaps depending on the language,
6978 but we don't handle values other than 1 correctly yet.) */
6979 tem = invert_truthvalue (arg0);
6980 /* Avoid infinite recursion. */
6981 if (TREE_CODE (tem) == TRUTH_NOT_EXPR)
6983 return fold_convert (type, tem);
6986 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
6988 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
6989 return omit_one_operand (type, TREE_OPERAND (arg0, 0),
6990 TREE_OPERAND (arg0, 1));
6991 else if (TREE_CODE (arg0) == COMPLEX_CST)
6992 return TREE_REALPART (arg0);
6993 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
6994 return fold (build2 (TREE_CODE (arg0), type,
6995 fold (build1 (REALPART_EXPR, type,
6996 TREE_OPERAND (arg0, 0))),
6997 fold (build1 (REALPART_EXPR, type,
6998 TREE_OPERAND (arg0, 1)))));
7002 if (TREE_CODE (TREE_TYPE (arg0)) != COMPLEX_TYPE)
7003 return fold_convert (type, integer_zero_node);
7004 else if (TREE_CODE (arg0) == COMPLEX_EXPR)
7005 return omit_one_operand (type, TREE_OPERAND (arg0, 1),
7006 TREE_OPERAND (arg0, 0));
7007 else if (TREE_CODE (arg0) == COMPLEX_CST)
7008 return TREE_IMAGPART (arg0);
7009 else if (TREE_CODE (arg0) == PLUS_EXPR || TREE_CODE (arg0) == MINUS_EXPR)
7010 return fold (build2 (TREE_CODE (arg0), type,
7011 fold (build1 (IMAGPART_EXPR, type,
7012 TREE_OPERAND (arg0, 0))),
7013 fold (build1 (IMAGPART_EXPR, type,
7014 TREE_OPERAND (arg0, 1)))));
7019 } /* switch (code) */
7022 /* Fold a binary expression EXPR. Return the folded expression if
7023 folding is successful. Otherwise, return the original
7027 fold_binary (tree expr)
7029 const tree t = expr;
7030 const tree type = TREE_TYPE (expr);
7031 tree t1 = NULL_TREE;
7034 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
7035 enum tree_code code = TREE_CODE (t);
7036 enum tree_code_class kind = TREE_CODE_CLASS (code);
7038 /* WINS will be nonzero when the switch is done
7039 if all operands are constant. */
7042 gcc_assert (IS_EXPR_CODE_CLASS (kind)
7043 && TREE_CODE_LENGTH (code) == 2);
7045 arg0 = op0 = TREE_OPERAND (t, 0);
7046 arg1 = op1 = TREE_OPERAND (t, 1);
7052 /* Strip any conversions that don't change the mode. This is
7053 safe for every expression, except for a comparison expression
7054 because its signedness is derived from its operands. So, in
7055 the latter case, only strip conversions that don't change the
7058 Note that this is done as an internal manipulation within the
7059 constant folder, in order to find the simplest representation
7060 of the arguments so that their form can be studied. In any
7061 cases, the appropriate type conversions should be put back in
7062 the tree that will get out of the constant folder. */
7063 if (kind == tcc_comparison)
7064 STRIP_SIGN_NOPS (arg0);
7068 if (TREE_CODE (arg0) == COMPLEX_CST)
7069 subop = TREE_REALPART (arg0);
7073 if (TREE_CODE (subop) != INTEGER_CST
7074 && TREE_CODE (subop) != REAL_CST)
7075 /* Note that TREE_CONSTANT isn't enough:
7076 static var addresses are constant but we can't
7077 do arithmetic on them. */
7085 /* Strip any conversions that don't change the mode. This is
7086 safe for every expression, except for a comparison expression
7087 because its signedness is derived from its operands. So, in
7088 the latter case, only strip conversions that don't change the
7091 Note that this is done as an internal manipulation within the
7092 constant folder, in order to find the simplest representation
7093 of the arguments so that their form can be studied. In any
7094 cases, the appropriate type conversions should be put back in
7095 the tree that will get out of the constant folder. */
7096 if (kind == tcc_comparison)
7097 STRIP_SIGN_NOPS (arg1);
7101 if (TREE_CODE (arg1) == COMPLEX_CST)
7102 subop = TREE_REALPART (arg1);
7106 if (TREE_CODE (subop) != INTEGER_CST
7107 && TREE_CODE (subop) != REAL_CST)
7108 /* Note that TREE_CONSTANT isn't enough:
7109 static var addresses are constant but we can't
7110 do arithmetic on them. */
7114 /* If this is a commutative operation, and ARG0 is a constant, move it
7115 to ARG1 to reduce the number of tests below. */
7116 if (commutative_tree_code (code)
7117 && tree_swap_operands_p (arg0, arg1, true))
7118 return fold (build2 (code, type, op1, op0));
7120 /* Now WINS is set as described above,
7121 ARG0 is the first operand of EXPR,
7122 and ARG1 is the second operand (if it has more than one operand).
7124 First check for cases where an arithmetic operation is applied to a
7125 compound, conditional, or comparison operation. Push the arithmetic
7126 operation inside the compound or conditional to see if any folding
7127 can then be done. Convert comparison to conditional for this purpose.
7128 The also optimizes non-constant cases that used to be done in
7131 Before we do that, see if this is a BIT_AND_EXPR or a BIT_IOR_EXPR,
7132 one of the operands is a comparison and the other is a comparison, a
7133 BIT_AND_EXPR with the constant 1, or a truth value. In that case, the
7134 code below would make the expression more complex. Change it to a
7135 TRUTH_{AND,OR}_EXPR. Likewise, convert a similar NE_EXPR to
7136 TRUTH_XOR_EXPR and an EQ_EXPR to the inversion of a TRUTH_XOR_EXPR. */
7138 if ((code == BIT_AND_EXPR || code == BIT_IOR_EXPR
7139 || code == EQ_EXPR || code == NE_EXPR)
7140 && ((truth_value_p (TREE_CODE (arg0))
7141 && (truth_value_p (TREE_CODE (arg1))
7142 || (TREE_CODE (arg1) == BIT_AND_EXPR
7143 && integer_onep (TREE_OPERAND (arg1, 1)))))
7144 || (truth_value_p (TREE_CODE (arg1))
7145 && (truth_value_p (TREE_CODE (arg0))
7146 || (TREE_CODE (arg0) == BIT_AND_EXPR
7147 && integer_onep (TREE_OPERAND (arg0, 1)))))))
7149 tem = fold (build2 (code == BIT_AND_EXPR ? TRUTH_AND_EXPR
7150 : code == BIT_IOR_EXPR ? TRUTH_OR_EXPR
7152 type, fold_convert (boolean_type_node, arg0),
7153 fold_convert (boolean_type_node, arg1)));
7155 if (code == EQ_EXPR)
7156 tem = invert_truthvalue (tem);
7161 if (TREE_CODE_CLASS (code) == tcc_comparison
7162 && TREE_CODE (arg0) == COMPOUND_EXPR)
7163 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7164 fold (build2 (code, type, TREE_OPERAND (arg0, 1), arg1)));
7165 else if (TREE_CODE_CLASS (code) == tcc_comparison
7166 && TREE_CODE (arg1) == COMPOUND_EXPR)
7167 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
7168 fold (build2 (code, type, arg0, TREE_OPERAND (arg1, 1))));
7169 else if (TREE_CODE_CLASS (code) == tcc_binary
7170 || TREE_CODE_CLASS (code) == tcc_comparison)
7172 if (TREE_CODE (arg0) == COMPOUND_EXPR)
7173 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg0, 0),
7174 fold (build2 (code, type, TREE_OPERAND (arg0, 1),
7176 if (TREE_CODE (arg1) == COMPOUND_EXPR
7177 && reorder_operands_p (arg0, TREE_OPERAND (arg1, 0)))
7178 return build2 (COMPOUND_EXPR, type, TREE_OPERAND (arg1, 0),
7179 fold (build2 (code, type,
7180 arg0, TREE_OPERAND (arg1, 1))));
7182 if (TREE_CODE (arg0) == COND_EXPR || COMPARISON_CLASS_P (arg0))
7184 tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
7186 /*cond_first_p=*/1);
7187 if (tem != NULL_TREE)
7191 if (TREE_CODE (arg1) == COND_EXPR || COMPARISON_CLASS_P (arg1))
7193 tem = fold_binary_op_with_conditional_arg (code, type, op0, op1,
7195 /*cond_first_p=*/0);
7196 if (tem != NULL_TREE)
7204 /* A + (-B) -> A - B */
7205 if (TREE_CODE (arg1) == NEGATE_EXPR)
7206 return fold (build2 (MINUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
7207 /* (-A) + B -> B - A */
7208 if (TREE_CODE (arg0) == NEGATE_EXPR
7209 && reorder_operands_p (TREE_OPERAND (arg0, 0), arg1))
7210 return fold (build2 (MINUS_EXPR, type, arg1, TREE_OPERAND (arg0, 0)));
7212 if (TREE_CODE (type) == COMPLEX_TYPE)
7214 tem = fold_complex_add (type, arg0, arg1, PLUS_EXPR);
7219 if (! FLOAT_TYPE_P (type))
7221 if (integer_zerop (arg1))
7222 return non_lvalue (fold_convert (type, arg0));
7224 /* If we are adding two BIT_AND_EXPR's, both of which are and'ing
7225 with a constant, and the two constants have no bits in common,
7226 we should treat this as a BIT_IOR_EXPR since this may produce more
7228 if (TREE_CODE (arg0) == BIT_AND_EXPR
7229 && TREE_CODE (arg1) == BIT_AND_EXPR
7230 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
7231 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
7232 && integer_zerop (const_binop (BIT_AND_EXPR,
7233 TREE_OPERAND (arg0, 1),
7234 TREE_OPERAND (arg1, 1), 0)))
7236 code = BIT_IOR_EXPR;
7240 /* Reassociate (plus (plus (mult) (foo)) (mult)) as
7241 (plus (plus (mult) (mult)) (foo)) so that we can
7242 take advantage of the factoring cases below. */
7243 if (((TREE_CODE (arg0) == PLUS_EXPR
7244 || TREE_CODE (arg0) == MINUS_EXPR)
7245 && TREE_CODE (arg1) == MULT_EXPR)
7246 || ((TREE_CODE (arg1) == PLUS_EXPR
7247 || TREE_CODE (arg1) == MINUS_EXPR)
7248 && TREE_CODE (arg0) == MULT_EXPR))
7250 tree parg0, parg1, parg, marg;
7251 enum tree_code pcode;
7253 if (TREE_CODE (arg1) == MULT_EXPR)
7254 parg = arg0, marg = arg1;
7256 parg = arg1, marg = arg0;
7257 pcode = TREE_CODE (parg);
7258 parg0 = TREE_OPERAND (parg, 0);
7259 parg1 = TREE_OPERAND (parg, 1);
7263 if (TREE_CODE (parg0) == MULT_EXPR
7264 && TREE_CODE (parg1) != MULT_EXPR)
7265 return fold (build2 (pcode, type,
7266 fold (build2 (PLUS_EXPR, type,
7267 fold_convert (type, parg0),
7268 fold_convert (type, marg))),
7269 fold_convert (type, parg1)));
7270 if (TREE_CODE (parg0) != MULT_EXPR
7271 && TREE_CODE (parg1) == MULT_EXPR)
7272 return fold (build2 (PLUS_EXPR, type,
7273 fold_convert (type, parg0),
7274 fold (build2 (pcode, type,
7275 fold_convert (type, marg),
7280 if (TREE_CODE (arg0) == MULT_EXPR && TREE_CODE (arg1) == MULT_EXPR)
7282 tree arg00, arg01, arg10, arg11;
7283 tree alt0 = NULL_TREE, alt1 = NULL_TREE, same;
7285 /* (A * C) + (B * C) -> (A+B) * C.
7286 We are most concerned about the case where C is a constant,
7287 but other combinations show up during loop reduction. Since
7288 it is not difficult, try all four possibilities. */
7290 arg00 = TREE_OPERAND (arg0, 0);
7291 arg01 = TREE_OPERAND (arg0, 1);
7292 arg10 = TREE_OPERAND (arg1, 0);
7293 arg11 = TREE_OPERAND (arg1, 1);
7296 if (operand_equal_p (arg01, arg11, 0))
7297 same = arg01, alt0 = arg00, alt1 = arg10;
7298 else if (operand_equal_p (arg00, arg10, 0))
7299 same = arg00, alt0 = arg01, alt1 = arg11;
7300 else if (operand_equal_p (arg00, arg11, 0))
7301 same = arg00, alt0 = arg01, alt1 = arg10;
7302 else if (operand_equal_p (arg01, arg10, 0))
7303 same = arg01, alt0 = arg00, alt1 = arg11;
7305 /* No identical multiplicands; see if we can find a common
7306 power-of-two factor in non-power-of-two multiplies. This
7307 can help in multi-dimensional array access. */
7308 else if (TREE_CODE (arg01) == INTEGER_CST
7309 && TREE_CODE (arg11) == INTEGER_CST
7310 && TREE_INT_CST_HIGH (arg01) == 0
7311 && TREE_INT_CST_HIGH (arg11) == 0)
7313 HOST_WIDE_INT int01, int11, tmp;
7314 int01 = TREE_INT_CST_LOW (arg01);
7315 int11 = TREE_INT_CST_LOW (arg11);
7317 /* Move min of absolute values to int11. */
7318 if ((int01 >= 0 ? int01 : -int01)
7319 < (int11 >= 0 ? int11 : -int11))
7321 tmp = int01, int01 = int11, int11 = tmp;
7322 alt0 = arg00, arg00 = arg10, arg10 = alt0;
7323 alt0 = arg01, arg01 = arg11, arg11 = alt0;
7326 if (exact_log2 (int11) > 0 && int01 % int11 == 0)
7328 alt0 = fold (build2 (MULT_EXPR, type, arg00,
7329 build_int_cst (NULL_TREE,
7337 return fold (build2 (MULT_EXPR, type,
7338 fold (build2 (PLUS_EXPR, type,
7339 fold_convert (type, alt0),
7340 fold_convert (type, alt1))),
7344 /* Try replacing &a[i1] + c * i2 with &a[i1 + i2], if c is step
7345 of the array. Loop optimizer sometimes produce this type of
7347 if (TREE_CODE (arg0) == ADDR_EXPR
7348 && TREE_CODE (arg1) == MULT_EXPR)
7350 tem = try_move_mult_to_index (PLUS_EXPR, arg0, arg1);
7352 return fold_convert (type, fold (tem));
7354 else if (TREE_CODE (arg1) == ADDR_EXPR
7355 && TREE_CODE (arg0) == MULT_EXPR)
7357 tem = try_move_mult_to_index (PLUS_EXPR, arg1, arg0);
7359 return fold_convert (type, fold (tem));
7364 /* See if ARG1 is zero and X + ARG1 reduces to X. */
7365 if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 0))
7366 return non_lvalue (fold_convert (type, arg0));
7368 /* Likewise if the operands are reversed. */
7369 if (fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
7370 return non_lvalue (fold_convert (type, arg1));
7372 /* Convert X + -C into X - C. */
7373 if (TREE_CODE (arg1) == REAL_CST
7374 && REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1)))
7376 tem = fold_negate_const (arg1, type);
7377 if (!TREE_OVERFLOW (arg1) || !flag_trapping_math)
7378 return fold (build2 (MINUS_EXPR, type,
7379 fold_convert (type, arg0),
7380 fold_convert (type, tem)));
7383 /* Convert x+x into x*2.0. */
7384 if (operand_equal_p (arg0, arg1, 0)
7385 && SCALAR_FLOAT_TYPE_P (type))
7386 return fold (build2 (MULT_EXPR, type, arg0,
7387 build_real (type, dconst2)));
7389 /* Convert x*c+x into x*(c+1). */
7390 if (flag_unsafe_math_optimizations
7391 && TREE_CODE (arg0) == MULT_EXPR
7392 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
7393 && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg0, 1))
7394 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
7398 c = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
7399 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
7400 return fold (build2 (MULT_EXPR, type, arg1,
7401 build_real (type, c)));
7404 /* Convert x+x*c into x*(c+1). */
7405 if (flag_unsafe_math_optimizations
7406 && TREE_CODE (arg1) == MULT_EXPR
7407 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST
7408 && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg1, 1))
7409 && operand_equal_p (TREE_OPERAND (arg1, 0), arg0, 0))
7413 c = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
7414 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
7415 return fold (build2 (MULT_EXPR, type, arg0,
7416 build_real (type, c)));
7419 /* Convert x*c1+x*c2 into x*(c1+c2). */
7420 if (flag_unsafe_math_optimizations
7421 && TREE_CODE (arg0) == MULT_EXPR
7422 && TREE_CODE (arg1) == MULT_EXPR
7423 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
7424 && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg0, 1))
7425 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST
7426 && ! TREE_CONSTANT_OVERFLOW (TREE_OPERAND (arg1, 1))
7427 && operand_equal_p (TREE_OPERAND (arg0, 0),
7428 TREE_OPERAND (arg1, 0), 0))
7430 REAL_VALUE_TYPE c1, c2;
7432 c1 = TREE_REAL_CST (TREE_OPERAND (arg0, 1));
7433 c2 = TREE_REAL_CST (TREE_OPERAND (arg1, 1));
7434 real_arithmetic (&c1, PLUS_EXPR, &c1, &c2);
7435 return fold (build2 (MULT_EXPR, type,
7436 TREE_OPERAND (arg0, 0),
7437 build_real (type, c1)));
7439 /* Convert a + (b*c + d*e) into (a + b*c) + d*e. */
7440 if (flag_unsafe_math_optimizations
7441 && TREE_CODE (arg1) == PLUS_EXPR
7442 && TREE_CODE (arg0) != MULT_EXPR)
7444 tree tree10 = TREE_OPERAND (arg1, 0);
7445 tree tree11 = TREE_OPERAND (arg1, 1);
7446 if (TREE_CODE (tree11) == MULT_EXPR
7447 && TREE_CODE (tree10) == MULT_EXPR)
7450 tree0 = fold (build2 (PLUS_EXPR, type, arg0, tree10));
7451 return fold (build2 (PLUS_EXPR, type, tree0, tree11));
7454 /* Convert (b*c + d*e) + a into b*c + (d*e +a). */
7455 if (flag_unsafe_math_optimizations
7456 && TREE_CODE (arg0) == PLUS_EXPR
7457 && TREE_CODE (arg1) != MULT_EXPR)
7459 tree tree00 = TREE_OPERAND (arg0, 0);
7460 tree tree01 = TREE_OPERAND (arg0, 1);
7461 if (TREE_CODE (tree01) == MULT_EXPR
7462 && TREE_CODE (tree00) == MULT_EXPR)
7465 tree0 = fold (build2 (PLUS_EXPR, type, tree01, arg1));
7466 return fold (build2 (PLUS_EXPR, type, tree00, tree0));
7472 /* (A << C1) + (A >> C2) if A is unsigned and C1+C2 is the size of A
7473 is a rotate of A by C1 bits. */
7474 /* (A << B) + (A >> (Z - B)) if A is unsigned and Z is the size of A
7475 is a rotate of A by B bits. */
7477 enum tree_code code0, code1;
7478 code0 = TREE_CODE (arg0);
7479 code1 = TREE_CODE (arg1);
7480 if (((code0 == RSHIFT_EXPR && code1 == LSHIFT_EXPR)
7481 || (code1 == RSHIFT_EXPR && code0 == LSHIFT_EXPR))
7482 && operand_equal_p (TREE_OPERAND (arg0, 0),
7483 TREE_OPERAND (arg1, 0), 0)
7484 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
7486 tree tree01, tree11;
7487 enum tree_code code01, code11;
7489 tree01 = TREE_OPERAND (arg0, 1);
7490 tree11 = TREE_OPERAND (arg1, 1);
7491 STRIP_NOPS (tree01);
7492 STRIP_NOPS (tree11);
7493 code01 = TREE_CODE (tree01);
7494 code11 = TREE_CODE (tree11);
7495 if (code01 == INTEGER_CST
7496 && code11 == INTEGER_CST
7497 && TREE_INT_CST_HIGH (tree01) == 0
7498 && TREE_INT_CST_HIGH (tree11) == 0
7499 && ((TREE_INT_CST_LOW (tree01) + TREE_INT_CST_LOW (tree11))
7500 == TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)))))
7501 return build2 (LROTATE_EXPR, type, TREE_OPERAND (arg0, 0),
7502 code0 == LSHIFT_EXPR ? tree01 : tree11);
7503 else if (code11 == MINUS_EXPR)
7505 tree tree110, tree111;
7506 tree110 = TREE_OPERAND (tree11, 0);
7507 tree111 = TREE_OPERAND (tree11, 1);
7508 STRIP_NOPS (tree110);
7509 STRIP_NOPS (tree111);
7510 if (TREE_CODE (tree110) == INTEGER_CST
7511 && 0 == compare_tree_int (tree110,
7513 (TREE_TYPE (TREE_OPERAND
7515 && operand_equal_p (tree01, tree111, 0))
7516 return build2 ((code0 == LSHIFT_EXPR
7519 type, TREE_OPERAND (arg0, 0), tree01);
7521 else if (code01 == MINUS_EXPR)
7523 tree tree010, tree011;
7524 tree010 = TREE_OPERAND (tree01, 0);
7525 tree011 = TREE_OPERAND (tree01, 1);
7526 STRIP_NOPS (tree010);
7527 STRIP_NOPS (tree011);
7528 if (TREE_CODE (tree010) == INTEGER_CST
7529 && 0 == compare_tree_int (tree010,
7531 (TREE_TYPE (TREE_OPERAND
7533 && operand_equal_p (tree11, tree011, 0))
7534 return build2 ((code0 != LSHIFT_EXPR
7537 type, TREE_OPERAND (arg0, 0), tree11);
7543 /* In most languages, can't associate operations on floats through
7544 parentheses. Rather than remember where the parentheses were, we
7545 don't associate floats at all, unless the user has specified
7546 -funsafe-math-optimizations. */
7549 && (! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations))
7551 tree var0, con0, lit0, minus_lit0;
7552 tree var1, con1, lit1, minus_lit1;
7554 /* Split both trees into variables, constants, and literals. Then
7555 associate each group together, the constants with literals,
7556 then the result with variables. This increases the chances of
7557 literals being recombined later and of generating relocatable
7558 expressions for the sum of a constant and literal. */
7559 var0 = split_tree (arg0, code, &con0, &lit0, &minus_lit0, 0);
7560 var1 = split_tree (arg1, code, &con1, &lit1, &minus_lit1,
7561 code == MINUS_EXPR);
7563 /* Only do something if we found more than two objects. Otherwise,
7564 nothing has changed and we risk infinite recursion. */
7565 if (2 < ((var0 != 0) + (var1 != 0)
7566 + (con0 != 0) + (con1 != 0)
7567 + (lit0 != 0) + (lit1 != 0)
7568 + (minus_lit0 != 0) + (minus_lit1 != 0)))
7570 /* Recombine MINUS_EXPR operands by using PLUS_EXPR. */
7571 if (code == MINUS_EXPR)
7574 var0 = associate_trees (var0, var1, code, type);
7575 con0 = associate_trees (con0, con1, code, type);
7576 lit0 = associate_trees (lit0, lit1, code, type);
7577 minus_lit0 = associate_trees (minus_lit0, minus_lit1, code, type);
7579 /* Preserve the MINUS_EXPR if the negative part of the literal is
7580 greater than the positive part. Otherwise, the multiplicative
7581 folding code (i.e extract_muldiv) may be fooled in case
7582 unsigned constants are subtracted, like in the following
7583 example: ((X*2 + 4) - 8U)/2. */
7584 if (minus_lit0 && lit0)
7586 if (TREE_CODE (lit0) == INTEGER_CST
7587 && TREE_CODE (minus_lit0) == INTEGER_CST
7588 && tree_int_cst_lt (lit0, minus_lit0))
7590 minus_lit0 = associate_trees (minus_lit0, lit0,
7596 lit0 = associate_trees (lit0, minus_lit0,
7604 return fold_convert (type,
7605 associate_trees (var0, minus_lit0,
7609 con0 = associate_trees (con0, minus_lit0,
7611 return fold_convert (type,
7612 associate_trees (var0, con0,
7617 con0 = associate_trees (con0, lit0, code, type);
7618 return fold_convert (type, associate_trees (var0, con0,
7625 t1 = const_binop (code, arg0, arg1, 0);
7626 if (t1 != NULL_TREE)
7628 /* The return value should always have
7629 the same type as the original expression. */
7630 if (TREE_TYPE (t1) != type)
7631 t1 = fold_convert (type, t1);
7638 /* A - (-B) -> A + B */
7639 if (TREE_CODE (arg1) == NEGATE_EXPR)
7640 return fold (build2 (PLUS_EXPR, type, arg0, TREE_OPERAND (arg1, 0)));
7641 /* (-A) - B -> (-B) - A where B is easily negated and we can swap. */
7642 if (TREE_CODE (arg0) == NEGATE_EXPR
7643 && (FLOAT_TYPE_P (type)
7644 || (INTEGRAL_TYPE_P (type) && flag_wrapv && !flag_trapv))
7645 && negate_expr_p (arg1)
7646 && reorder_operands_p (arg0, arg1))
7647 return fold (build2 (MINUS_EXPR, type, negate_expr (arg1),
7648 TREE_OPERAND (arg0, 0)));
7650 if (TREE_CODE (type) == COMPLEX_TYPE)
7652 tem = fold_complex_add (type, arg0, arg1, MINUS_EXPR);
7657 if (! FLOAT_TYPE_P (type))
7659 if (! wins && integer_zerop (arg0))
7660 return negate_expr (fold_convert (type, arg1));
7661 if (integer_zerop (arg1))
7662 return non_lvalue (fold_convert (type, arg0));
7664 /* Fold A - (A & B) into ~B & A. */
7665 if (!TREE_SIDE_EFFECTS (arg0)
7666 && TREE_CODE (arg1) == BIT_AND_EXPR)
7668 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 1), 0))
7669 return fold (build2 (BIT_AND_EXPR, type,
7670 fold (build1 (BIT_NOT_EXPR, type,
7671 TREE_OPERAND (arg1, 0))),
7673 if (operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
7674 return fold (build2 (BIT_AND_EXPR, type,
7675 fold (build1 (BIT_NOT_EXPR, type,
7676 TREE_OPERAND (arg1, 1))),
7680 /* Fold (A & ~B) - (A & B) into (A ^ B) - B, where B is
7681 any power of 2 minus 1. */
7682 if (TREE_CODE (arg0) == BIT_AND_EXPR
7683 && TREE_CODE (arg1) == BIT_AND_EXPR
7684 && operand_equal_p (TREE_OPERAND (arg0, 0),
7685 TREE_OPERAND (arg1, 0), 0))
7687 tree mask0 = TREE_OPERAND (arg0, 1);
7688 tree mask1 = TREE_OPERAND (arg1, 1);
7689 tree tem = fold (build1 (BIT_NOT_EXPR, type, mask0));
7691 if (operand_equal_p (tem, mask1, 0))
7693 tem = fold (build2 (BIT_XOR_EXPR, type,
7694 TREE_OPERAND (arg0, 0), mask1));
7695 return fold (build2 (MINUS_EXPR, type, tem, mask1));
7700 /* See if ARG1 is zero and X - ARG1 reduces to X. */
7701 else if (fold_real_zero_addition_p (TREE_TYPE (arg0), arg1, 1))
7702 return non_lvalue (fold_convert (type, arg0));
7704 /* (ARG0 - ARG1) is the same as (-ARG1 + ARG0). So check whether
7705 ARG0 is zero and X + ARG0 reduces to X, since that would mean
7706 (-ARG1 + ARG0) reduces to -ARG1. */
7707 else if (!wins && fold_real_zero_addition_p (TREE_TYPE (arg1), arg0, 0))
7708 return negate_expr (fold_convert (type, arg1));
7710 /* Fold &x - &x. This can happen from &x.foo - &x.
7711 This is unsafe for certain floats even in non-IEEE formats.
7712 In IEEE, it is unsafe because it does wrong for NaNs.
7713 Also note that operand_equal_p is always false if an operand
7716 if ((! FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations)
7717 && operand_equal_p (arg0, arg1, 0))
7718 return fold_convert (type, integer_zero_node);
7720 /* A - B -> A + (-B) if B is easily negatable. */
7721 if (!wins && negate_expr_p (arg1)
7722 && ((FLOAT_TYPE_P (type)
7723 /* Avoid this transformation if B is a positive REAL_CST. */
7724 && (TREE_CODE (arg1) != REAL_CST
7725 || REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg1))))
7726 || (INTEGRAL_TYPE_P (type) && flag_wrapv && !flag_trapv)))
7727 return fold (build2 (PLUS_EXPR, type, arg0, negate_expr (arg1)));
7729 /* Try folding difference of addresses. */
7733 if ((TREE_CODE (arg0) == ADDR_EXPR
7734 || TREE_CODE (arg1) == ADDR_EXPR)
7735 && ptr_difference_const (arg0, arg1, &diff))
7736 return build_int_cst_type (type, diff);
7739 /* Try replacing &a[i1] - c * i2 with &a[i1 - i2], if c is step
7740 of the array. Loop optimizer sometimes produce this type of
7742 if (TREE_CODE (arg0) == ADDR_EXPR
7743 && TREE_CODE (arg1) == MULT_EXPR)
7745 tem = try_move_mult_to_index (MINUS_EXPR, arg0, arg1);
7747 return fold_convert (type, fold (tem));
7750 if (TREE_CODE (arg0) == MULT_EXPR
7751 && TREE_CODE (arg1) == MULT_EXPR
7752 && (!FLOAT_TYPE_P (type) || flag_unsafe_math_optimizations))
7754 /* (A * C) - (B * C) -> (A-B) * C. */
7755 if (operand_equal_p (TREE_OPERAND (arg0, 1),
7756 TREE_OPERAND (arg1, 1), 0))
7757 return fold (build2 (MULT_EXPR, type,
7758 fold (build2 (MINUS_EXPR, type,
7759 TREE_OPERAND (arg0, 0),
7760 TREE_OPERAND (arg1, 0))),
7761 TREE_OPERAND (arg0, 1)));
7762 /* (A * C1) - (A * C2) -> A * (C1-C2). */
7763 if (operand_equal_p (TREE_OPERAND (arg0, 0),
7764 TREE_OPERAND (arg1, 0), 0))
7765 return fold (build2 (MULT_EXPR, type,
7766 TREE_OPERAND (arg0, 0),
7767 fold (build2 (MINUS_EXPR, type,
7768 TREE_OPERAND (arg0, 1),
7769 TREE_OPERAND (arg1, 1)))));
7775 /* (-A) * (-B) -> A * B */
7776 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
7777 return fold (build2 (MULT_EXPR, type,
7778 TREE_OPERAND (arg0, 0),
7779 negate_expr (arg1)));
7780 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
7781 return fold (build2 (MULT_EXPR, type,
7783 TREE_OPERAND (arg1, 0)));
7785 if (TREE_CODE (type) == COMPLEX_TYPE)
7787 tem = fold_complex_mult (type, arg0, arg1);
7792 if (! FLOAT_TYPE_P (type))
7794 if (integer_zerop (arg1))
7795 return omit_one_operand (type, arg1, arg0);
7796 if (integer_onep (arg1))
7797 return non_lvalue (fold_convert (type, arg0));
7799 /* (a * (1 << b)) is (a << b) */
7800 if (TREE_CODE (arg1) == LSHIFT_EXPR
7801 && integer_onep (TREE_OPERAND (arg1, 0)))
7802 return fold (build2 (LSHIFT_EXPR, type, arg0,
7803 TREE_OPERAND (arg1, 1)));
7804 if (TREE_CODE (arg0) == LSHIFT_EXPR
7805 && integer_onep (TREE_OPERAND (arg0, 0)))
7806 return fold (build2 (LSHIFT_EXPR, type, arg1,
7807 TREE_OPERAND (arg0, 1)));
7809 if (TREE_CODE (arg1) == INTEGER_CST
7810 && 0 != (tem = extract_muldiv (op0,
7811 fold_convert (type, arg1),
7813 return fold_convert (type, tem);
7818 /* Maybe fold x * 0 to 0. The expressions aren't the same
7819 when x is NaN, since x * 0 is also NaN. Nor are they the
7820 same in modes with signed zeros, since multiplying a
7821 negative value by 0 gives -0, not +0. */
7822 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
7823 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg0)))
7824 && real_zerop (arg1))
7825 return omit_one_operand (type, arg1, arg0);
7826 /* In IEEE floating point, x*1 is not equivalent to x for snans. */
7827 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
7828 && real_onep (arg1))
7829 return non_lvalue (fold_convert (type, arg0));
7831 /* Transform x * -1.0 into -x. */
7832 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
7833 && real_minus_onep (arg1))
7834 return fold_convert (type, negate_expr (arg0));
7836 /* Convert (C1/X)*C2 into (C1*C2)/X. */
7837 if (flag_unsafe_math_optimizations
7838 && TREE_CODE (arg0) == RDIV_EXPR
7839 && TREE_CODE (arg1) == REAL_CST
7840 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST)
7842 tree tem = const_binop (MULT_EXPR, TREE_OPERAND (arg0, 0),
7845 return fold (build2 (RDIV_EXPR, type, tem,
7846 TREE_OPERAND (arg0, 1)));
7849 /* Strip sign operations from X in X*X, i.e. -Y*-Y -> Y*Y. */
7850 if (operand_equal_p (arg0, arg1, 0))
7852 tree tem = fold_strip_sign_ops (arg0);
7853 if (tem != NULL_TREE)
7855 tem = fold_convert (type, tem);
7856 return fold (build2 (MULT_EXPR, type, tem, tem));
7860 if (flag_unsafe_math_optimizations)
7862 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
7863 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
7865 /* Optimizations of root(...)*root(...). */
7866 if (fcode0 == fcode1 && BUILTIN_ROOT_P (fcode0))
7868 tree rootfn, arg, arglist;
7869 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7870 tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
7872 /* Optimize sqrt(x)*sqrt(x) as x. */
7873 if (BUILTIN_SQRT_P (fcode0)
7874 && operand_equal_p (arg00, arg10, 0)
7875 && ! HONOR_SNANS (TYPE_MODE (type)))
7878 /* Optimize root(x)*root(y) as root(x*y). */
7879 rootfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
7880 arg = fold (build2 (MULT_EXPR, type, arg00, arg10));
7881 arglist = build_tree_list (NULL_TREE, arg);
7882 return build_function_call_expr (rootfn, arglist);
7885 /* Optimize expN(x)*expN(y) as expN(x+y). */
7886 if (fcode0 == fcode1 && BUILTIN_EXPONENT_P (fcode0))
7888 tree expfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
7889 tree arg = build2 (PLUS_EXPR, type,
7890 TREE_VALUE (TREE_OPERAND (arg0, 1)),
7891 TREE_VALUE (TREE_OPERAND (arg1, 1)));
7892 tree arglist = build_tree_list (NULL_TREE, fold (arg));
7893 return build_function_call_expr (expfn, arglist);
7896 /* Optimizations of pow(...)*pow(...). */
7897 if ((fcode0 == BUILT_IN_POW && fcode1 == BUILT_IN_POW)
7898 || (fcode0 == BUILT_IN_POWF && fcode1 == BUILT_IN_POWF)
7899 || (fcode0 == BUILT_IN_POWL && fcode1 == BUILT_IN_POWL))
7901 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7902 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0,
7904 tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
7905 tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1,
7908 /* Optimize pow(x,y)*pow(z,y) as pow(x*z,y). */
7909 if (operand_equal_p (arg01, arg11, 0))
7911 tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
7912 tree arg = build2 (MULT_EXPR, type, arg00, arg10);
7913 tree arglist = tree_cons (NULL_TREE, fold (arg),
7914 build_tree_list (NULL_TREE,
7916 return build_function_call_expr (powfn, arglist);
7919 /* Optimize pow(x,y)*pow(x,z) as pow(x,y+z). */
7920 if (operand_equal_p (arg00, arg10, 0))
7922 tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
7923 tree arg = fold (build2 (PLUS_EXPR, type, arg01, arg11));
7924 tree arglist = tree_cons (NULL_TREE, arg00,
7925 build_tree_list (NULL_TREE,
7927 return build_function_call_expr (powfn, arglist);
7931 /* Optimize tan(x)*cos(x) as sin(x). */
7932 if (((fcode0 == BUILT_IN_TAN && fcode1 == BUILT_IN_COS)
7933 || (fcode0 == BUILT_IN_TANF && fcode1 == BUILT_IN_COSF)
7934 || (fcode0 == BUILT_IN_TANL && fcode1 == BUILT_IN_COSL)
7935 || (fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_TAN)
7936 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_TANF)
7937 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_TANL))
7938 && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
7939 TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
7941 tree sinfn = mathfn_built_in (type, BUILT_IN_SIN);
7943 if (sinfn != NULL_TREE)
7944 return build_function_call_expr (sinfn,
7945 TREE_OPERAND (arg0, 1));
7948 /* Optimize x*pow(x,c) as pow(x,c+1). */
7949 if (fcode1 == BUILT_IN_POW
7950 || fcode1 == BUILT_IN_POWF
7951 || fcode1 == BUILT_IN_POWL)
7953 tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
7954 tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1,
7956 if (TREE_CODE (arg11) == REAL_CST
7957 && ! TREE_CONSTANT_OVERFLOW (arg11)
7958 && operand_equal_p (arg0, arg10, 0))
7960 tree powfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
7964 c = TREE_REAL_CST (arg11);
7965 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
7966 arg = build_real (type, c);
7967 arglist = build_tree_list (NULL_TREE, arg);
7968 arglist = tree_cons (NULL_TREE, arg0, arglist);
7969 return build_function_call_expr (powfn, arglist);
7973 /* Optimize pow(x,c)*x as pow(x,c+1). */
7974 if (fcode0 == BUILT_IN_POW
7975 || fcode0 == BUILT_IN_POWF
7976 || fcode0 == BUILT_IN_POWL)
7978 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
7979 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0,
7981 if (TREE_CODE (arg01) == REAL_CST
7982 && ! TREE_CONSTANT_OVERFLOW (arg01)
7983 && operand_equal_p (arg1, arg00, 0))
7985 tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
7989 c = TREE_REAL_CST (arg01);
7990 real_arithmetic (&c, PLUS_EXPR, &c, &dconst1);
7991 arg = build_real (type, c);
7992 arglist = build_tree_list (NULL_TREE, arg);
7993 arglist = tree_cons (NULL_TREE, arg1, arglist);
7994 return build_function_call_expr (powfn, arglist);
7998 /* Optimize x*x as pow(x,2.0), which is expanded as x*x. */
8000 && operand_equal_p (arg0, arg1, 0))
8002 tree powfn = mathfn_built_in (type, BUILT_IN_POW);
8006 tree arg = build_real (type, dconst2);
8007 tree arglist = build_tree_list (NULL_TREE, arg);
8008 arglist = tree_cons (NULL_TREE, arg0, arglist);
8009 return build_function_call_expr (powfn, arglist);
8018 if (integer_all_onesp (arg1))
8019 return omit_one_operand (type, arg1, arg0);
8020 if (integer_zerop (arg1))
8021 return non_lvalue (fold_convert (type, arg0));
8022 if (operand_equal_p (arg0, arg1, 0))
8023 return non_lvalue (fold_convert (type, arg0));
8026 if (TREE_CODE (arg0) == BIT_NOT_EXPR
8027 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
8029 t1 = build_int_cst (type, -1);
8030 t1 = force_fit_type (t1, 0, false, false);
8031 return omit_one_operand (type, t1, arg1);
8035 if (TREE_CODE (arg1) == BIT_NOT_EXPR
8036 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
8038 t1 = build_int_cst (type, -1);
8039 t1 = force_fit_type (t1, 0, false, false);
8040 return omit_one_operand (type, t1, arg0);
8043 t1 = distribute_bit_expr (code, type, arg0, arg1);
8044 if (t1 != NULL_TREE)
8047 /* Convert (or (not arg0) (not arg1)) to (not (and (arg0) (arg1))).
8049 This results in more efficient code for machines without a NAND
8050 instruction. Combine will canonicalize to the first form
8051 which will allow use of NAND instructions provided by the
8052 backend if they exist. */
8053 if (TREE_CODE (arg0) == BIT_NOT_EXPR
8054 && TREE_CODE (arg1) == BIT_NOT_EXPR)
8056 return fold (build1 (BIT_NOT_EXPR, type,
8057 build2 (BIT_AND_EXPR, type,
8058 TREE_OPERAND (arg0, 0),
8059 TREE_OPERAND (arg1, 0))));
8062 /* See if this can be simplified into a rotate first. If that
8063 is unsuccessful continue in the association code. */
8067 if (integer_zerop (arg1))
8068 return non_lvalue (fold_convert (type, arg0));
8069 if (integer_all_onesp (arg1))
8070 return fold (build1 (BIT_NOT_EXPR, type, arg0));
8071 if (operand_equal_p (arg0, arg1, 0))
8072 return omit_one_operand (type, integer_zero_node, arg0);
8075 if (TREE_CODE (arg0) == BIT_NOT_EXPR
8076 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
8078 t1 = build_int_cst (type, -1);
8079 t1 = force_fit_type (t1, 0, false, false);
8080 return omit_one_operand (type, t1, arg1);
8084 if (TREE_CODE (arg1) == BIT_NOT_EXPR
8085 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
8087 t1 = build_int_cst (type, -1);
8088 t1 = force_fit_type (t1, 0, false, false);
8089 return omit_one_operand (type, t1, arg0);
8092 /* If we are XORing two BIT_AND_EXPR's, both of which are and'ing
8093 with a constant, and the two constants have no bits in common,
8094 we should treat this as a BIT_IOR_EXPR since this may produce more
8096 if (TREE_CODE (arg0) == BIT_AND_EXPR
8097 && TREE_CODE (arg1) == BIT_AND_EXPR
8098 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8099 && TREE_CODE (TREE_OPERAND (arg1, 1)) == INTEGER_CST
8100 && integer_zerop (const_binop (BIT_AND_EXPR,
8101 TREE_OPERAND (arg0, 1),
8102 TREE_OPERAND (arg1, 1), 0)))
8104 code = BIT_IOR_EXPR;
8108 /* See if this can be simplified into a rotate first. If that
8109 is unsuccessful continue in the association code. */
8113 if (integer_all_onesp (arg1))
8114 return non_lvalue (fold_convert (type, arg0));
8115 if (integer_zerop (arg1))
8116 return omit_one_operand (type, arg1, arg0);
8117 if (operand_equal_p (arg0, arg1, 0))
8118 return non_lvalue (fold_convert (type, arg0));
8120 /* ~X & X is always zero. */
8121 if (TREE_CODE (arg0) == BIT_NOT_EXPR
8122 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
8123 return omit_one_operand (type, integer_zero_node, arg1);
8125 /* X & ~X is always zero. */
8126 if (TREE_CODE (arg1) == BIT_NOT_EXPR
8127 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
8128 return omit_one_operand (type, integer_zero_node, arg0);
8130 t1 = distribute_bit_expr (code, type, arg0, arg1);
8131 if (t1 != NULL_TREE)
8133 /* Simplify ((int)c & 0377) into (int)c, if c is unsigned char. */
8134 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) == NOP_EXPR
8135 && TYPE_UNSIGNED (TREE_TYPE (TREE_OPERAND (arg0, 0))))
8138 = TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (arg0, 0)));
8140 if (prec < BITS_PER_WORD && prec < HOST_BITS_PER_WIDE_INT
8141 && (~TREE_INT_CST_LOW (arg1)
8142 & (((HOST_WIDE_INT) 1 << prec) - 1)) == 0)
8143 return fold_convert (type, TREE_OPERAND (arg0, 0));
8146 /* Convert (and (not arg0) (not arg1)) to (not (or (arg0) (arg1))).
8148 This results in more efficient code for machines without a NOR
8149 instruction. Combine will canonicalize to the first form
8150 which will allow use of NOR instructions provided by the
8151 backend if they exist. */
8152 if (TREE_CODE (arg0) == BIT_NOT_EXPR
8153 && TREE_CODE (arg1) == BIT_NOT_EXPR)
8155 return fold (build1 (BIT_NOT_EXPR, type,
8156 build2 (BIT_IOR_EXPR, type,
8157 TREE_OPERAND (arg0, 0),
8158 TREE_OPERAND (arg1, 0))));
8164 /* Don't touch a floating-point divide by zero unless the mode
8165 of the constant can represent infinity. */
8166 if (TREE_CODE (arg1) == REAL_CST
8167 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (arg1)))
8168 && real_zerop (arg1))
8171 /* (-A) / (-B) -> A / B */
8172 if (TREE_CODE (arg0) == NEGATE_EXPR && negate_expr_p (arg1))
8173 return fold (build2 (RDIV_EXPR, type,
8174 TREE_OPERAND (arg0, 0),
8175 negate_expr (arg1)));
8176 if (TREE_CODE (arg1) == NEGATE_EXPR && negate_expr_p (arg0))
8177 return fold (build2 (RDIV_EXPR, type,
8179 TREE_OPERAND (arg1, 0)));
8181 /* In IEEE floating point, x/1 is not equivalent to x for snans. */
8182 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
8183 && real_onep (arg1))
8184 return non_lvalue (fold_convert (type, arg0));
8186 /* In IEEE floating point, x/-1 is not equivalent to -x for snans. */
8187 if (!HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg0)))
8188 && real_minus_onep (arg1))
8189 return non_lvalue (fold_convert (type, negate_expr (arg0)));
8191 /* If ARG1 is a constant, we can convert this to a multiply by the
8192 reciprocal. This does not have the same rounding properties,
8193 so only do this if -funsafe-math-optimizations. We can actually
8194 always safely do it if ARG1 is a power of two, but it's hard to
8195 tell if it is or not in a portable manner. */
8196 if (TREE_CODE (arg1) == REAL_CST)
8198 if (flag_unsafe_math_optimizations
8199 && 0 != (tem = const_binop (code, build_real (type, dconst1),
8201 return fold (build2 (MULT_EXPR, type, arg0, tem));
8202 /* Find the reciprocal if optimizing and the result is exact. */
8206 r = TREE_REAL_CST (arg1);
8207 if (exact_real_inverse (TYPE_MODE(TREE_TYPE(arg0)), &r))
8209 tem = build_real (type, r);
8210 return fold (build2 (MULT_EXPR, type, arg0, tem));
8214 /* Convert A/B/C to A/(B*C). */
8215 if (flag_unsafe_math_optimizations
8216 && TREE_CODE (arg0) == RDIV_EXPR)
8217 return fold (build2 (RDIV_EXPR, type, TREE_OPERAND (arg0, 0),
8218 fold (build2 (MULT_EXPR, type,
8219 TREE_OPERAND (arg0, 1), arg1))));
8221 /* Convert A/(B/C) to (A/B)*C. */
8222 if (flag_unsafe_math_optimizations
8223 && TREE_CODE (arg1) == RDIV_EXPR)
8224 return fold (build2 (MULT_EXPR, type,
8225 fold (build2 (RDIV_EXPR, type, arg0,
8226 TREE_OPERAND (arg1, 0))),
8227 TREE_OPERAND (arg1, 1)));
8229 /* Convert C1/(X*C2) into (C1/C2)/X. */
8230 if (flag_unsafe_math_optimizations
8231 && TREE_CODE (arg1) == MULT_EXPR
8232 && TREE_CODE (arg0) == REAL_CST
8233 && TREE_CODE (TREE_OPERAND (arg1, 1)) == REAL_CST)
8235 tree tem = const_binop (RDIV_EXPR, arg0,
8236 TREE_OPERAND (arg1, 1), 0);
8238 return fold (build2 (RDIV_EXPR, type, tem,
8239 TREE_OPERAND (arg1, 0)));
8242 if (TREE_CODE (type) == COMPLEX_TYPE)
8244 tem = fold_complex_div (type, arg0, arg1, code);
8249 if (flag_unsafe_math_optimizations)
8251 enum built_in_function fcode = builtin_mathfn_code (arg1);
8252 /* Optimize x/expN(y) into x*expN(-y). */
8253 if (BUILTIN_EXPONENT_P (fcode))
8255 tree expfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
8256 tree arg = negate_expr (TREE_VALUE (TREE_OPERAND (arg1, 1)));
8257 tree arglist = build_tree_list (NULL_TREE,
8258 fold_convert (type, arg));
8259 arg1 = build_function_call_expr (expfn, arglist);
8260 return fold (build2 (MULT_EXPR, type, arg0, arg1));
8263 /* Optimize x/pow(y,z) into x*pow(y,-z). */
8264 if (fcode == BUILT_IN_POW
8265 || fcode == BUILT_IN_POWF
8266 || fcode == BUILT_IN_POWL)
8268 tree powfn = TREE_OPERAND (TREE_OPERAND (arg1, 0), 0);
8269 tree arg10 = TREE_VALUE (TREE_OPERAND (arg1, 1));
8270 tree arg11 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg1, 1)));
8271 tree neg11 = fold_convert (type, negate_expr (arg11));
8272 tree arglist = tree_cons(NULL_TREE, arg10,
8273 build_tree_list (NULL_TREE, neg11));
8274 arg1 = build_function_call_expr (powfn, arglist);
8275 return fold (build2 (MULT_EXPR, type, arg0, arg1));
8279 if (flag_unsafe_math_optimizations)
8281 enum built_in_function fcode0 = builtin_mathfn_code (arg0);
8282 enum built_in_function fcode1 = builtin_mathfn_code (arg1);
8284 /* Optimize sin(x)/cos(x) as tan(x). */
8285 if (((fcode0 == BUILT_IN_SIN && fcode1 == BUILT_IN_COS)
8286 || (fcode0 == BUILT_IN_SINF && fcode1 == BUILT_IN_COSF)
8287 || (fcode0 == BUILT_IN_SINL && fcode1 == BUILT_IN_COSL))
8288 && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
8289 TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
8291 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
8293 if (tanfn != NULL_TREE)
8294 return build_function_call_expr (tanfn,
8295 TREE_OPERAND (arg0, 1));
8298 /* Optimize cos(x)/sin(x) as 1.0/tan(x). */
8299 if (((fcode0 == BUILT_IN_COS && fcode1 == BUILT_IN_SIN)
8300 || (fcode0 == BUILT_IN_COSF && fcode1 == BUILT_IN_SINF)
8301 || (fcode0 == BUILT_IN_COSL && fcode1 == BUILT_IN_SINL))
8302 && operand_equal_p (TREE_VALUE (TREE_OPERAND (arg0, 1)),
8303 TREE_VALUE (TREE_OPERAND (arg1, 1)), 0))
8305 tree tanfn = mathfn_built_in (type, BUILT_IN_TAN);
8307 if (tanfn != NULL_TREE)
8309 tree tmp = TREE_OPERAND (arg0, 1);
8310 tmp = build_function_call_expr (tanfn, tmp);
8311 return fold (build2 (RDIV_EXPR, type,
8312 build_real (type, dconst1), tmp));
8316 /* Optimize pow(x,c)/x as pow(x,c-1). */
8317 if (fcode0 == BUILT_IN_POW
8318 || fcode0 == BUILT_IN_POWF
8319 || fcode0 == BUILT_IN_POWL)
8321 tree arg00 = TREE_VALUE (TREE_OPERAND (arg0, 1));
8322 tree arg01 = TREE_VALUE (TREE_CHAIN (TREE_OPERAND (arg0, 1)));
8323 if (TREE_CODE (arg01) == REAL_CST
8324 && ! TREE_CONSTANT_OVERFLOW (arg01)
8325 && operand_equal_p (arg1, arg00, 0))
8327 tree powfn = TREE_OPERAND (TREE_OPERAND (arg0, 0), 0);
8331 c = TREE_REAL_CST (arg01);
8332 real_arithmetic (&c, MINUS_EXPR, &c, &dconst1);
8333 arg = build_real (type, c);
8334 arglist = build_tree_list (NULL_TREE, arg);
8335 arglist = tree_cons (NULL_TREE, arg1, arglist);
8336 return build_function_call_expr (powfn, arglist);
8342 case TRUNC_DIV_EXPR:
8343 case ROUND_DIV_EXPR:
8344 case FLOOR_DIV_EXPR:
8346 case EXACT_DIV_EXPR:
8347 if (integer_onep (arg1))
8348 return non_lvalue (fold_convert (type, arg0));
8349 if (integer_zerop (arg1))
8352 if (!TYPE_UNSIGNED (type)
8353 && TREE_CODE (arg1) == INTEGER_CST
8354 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
8355 && TREE_INT_CST_HIGH (arg1) == -1)
8356 return fold_convert (type, negate_expr (arg0));
8358 /* If arg0 is a multiple of arg1, then rewrite to the fastest div
8359 operation, EXACT_DIV_EXPR.
8361 Note that only CEIL_DIV_EXPR and FLOOR_DIV_EXPR are rewritten now.
8362 At one time others generated faster code, it's not clear if they do
8363 after the last round to changes to the DIV code in expmed.c. */
8364 if ((code == CEIL_DIV_EXPR || code == FLOOR_DIV_EXPR)
8365 && multiple_of_p (type, arg0, arg1))
8366 return fold (build2 (EXACT_DIV_EXPR, type, arg0, arg1));
8368 if (TREE_CODE (arg1) == INTEGER_CST
8369 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE)))
8370 return fold_convert (type, tem);
8372 if (TREE_CODE (type) == COMPLEX_TYPE)
8374 tem = fold_complex_div (type, arg0, arg1, code);
8381 case FLOOR_MOD_EXPR:
8382 case ROUND_MOD_EXPR:
8383 case TRUNC_MOD_EXPR:
8384 /* X % 1 is always zero, but be sure to preserve any side
8386 if (integer_onep (arg1))
8387 return omit_one_operand (type, integer_zero_node, arg0);
8389 /* X % 0, return X % 0 unchanged so that we can get the
8390 proper warnings and errors. */
8391 if (integer_zerop (arg1))
8394 /* 0 % X is always zero, but be sure to preserve any side
8395 effects in X. Place this after checking for X == 0. */
8396 if (integer_zerop (arg0))
8397 return omit_one_operand (type, integer_zero_node, arg1);
8399 /* X % -1 is zero. */
8400 if (!TYPE_UNSIGNED (type)
8401 && TREE_CODE (arg1) == INTEGER_CST
8402 && TREE_INT_CST_LOW (arg1) == (unsigned HOST_WIDE_INT) -1
8403 && TREE_INT_CST_HIGH (arg1) == -1)
8404 return omit_one_operand (type, integer_zero_node, arg0);
8406 /* Optimize unsigned TRUNC_MOD_EXPR by a power of two into a
8407 BIT_AND_EXPR, i.e. "X % C" into "X & C2". */
8408 if (code == TRUNC_MOD_EXPR
8409 && TYPE_UNSIGNED (type)
8410 && integer_pow2p (arg1))
8412 unsigned HOST_WIDE_INT high, low;
8416 l = tree_log2 (arg1);
8417 if (l >= HOST_BITS_PER_WIDE_INT)
8419 high = ((unsigned HOST_WIDE_INT) 1
8420 << (l - HOST_BITS_PER_WIDE_INT)) - 1;
8426 low = ((unsigned HOST_WIDE_INT) 1 << l) - 1;
8429 mask = build_int_cst_wide (type, low, high);
8430 return fold (build2 (BIT_AND_EXPR, type,
8431 fold_convert (type, arg0), mask));
8434 /* X % -C is the same as X % C. */
8435 if (code == TRUNC_MOD_EXPR
8436 && !TYPE_UNSIGNED (type)
8437 && TREE_CODE (arg1) == INTEGER_CST
8438 && TREE_INT_CST_HIGH (arg1) < 0
8440 /* Avoid this transformation if C is INT_MIN, i.e. C == -C. */
8441 && !sign_bit_p (arg1, arg1))
8442 return fold (build2 (code, type, fold_convert (type, arg0),
8443 fold_convert (type, negate_expr (arg1))));
8445 /* X % -Y is the same as X % Y. */
8446 if (code == TRUNC_MOD_EXPR
8447 && !TYPE_UNSIGNED (type)
8448 && TREE_CODE (arg1) == NEGATE_EXPR
8450 return fold (build2 (code, type, fold_convert (type, arg0),
8451 fold_convert (type, TREE_OPERAND (arg1, 0))));
8453 if (TREE_CODE (arg1) == INTEGER_CST
8454 && 0 != (tem = extract_muldiv (op0, arg1, code, NULL_TREE)))
8455 return fold_convert (type, tem);
8461 if (integer_all_onesp (arg0))
8462 return omit_one_operand (type, arg0, arg1);
8466 /* Optimize -1 >> x for arithmetic right shifts. */
8467 if (integer_all_onesp (arg0) && !TYPE_UNSIGNED (type))
8468 return omit_one_operand (type, arg0, arg1);
8469 /* ... fall through ... */
8473 if (integer_zerop (arg1))
8474 return non_lvalue (fold_convert (type, arg0));
8475 if (integer_zerop (arg0))
8476 return omit_one_operand (type, arg0, arg1);
8478 /* Since negative shift count is not well-defined,
8479 don't try to compute it in the compiler. */
8480 if (TREE_CODE (arg1) == INTEGER_CST && tree_int_cst_sgn (arg1) < 0)
8482 /* Rewrite an LROTATE_EXPR by a constant into an
8483 RROTATE_EXPR by a new constant. */
8484 if (code == LROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST)
8486 tree tem = build_int_cst (NULL_TREE,
8487 GET_MODE_BITSIZE (TYPE_MODE (type)));
8488 tem = fold_convert (TREE_TYPE (arg1), tem);
8489 tem = const_binop (MINUS_EXPR, tem, arg1, 0);
8490 return fold (build2 (RROTATE_EXPR, type, arg0, tem));
8493 /* If we have a rotate of a bit operation with the rotate count and
8494 the second operand of the bit operation both constant,
8495 permute the two operations. */
8496 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
8497 && (TREE_CODE (arg0) == BIT_AND_EXPR
8498 || TREE_CODE (arg0) == BIT_IOR_EXPR
8499 || TREE_CODE (arg0) == BIT_XOR_EXPR)
8500 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
8501 return fold (build2 (TREE_CODE (arg0), type,
8502 fold (build2 (code, type,
8503 TREE_OPERAND (arg0, 0), arg1)),
8504 fold (build2 (code, type,
8505 TREE_OPERAND (arg0, 1), arg1))));
8507 /* Two consecutive rotates adding up to the width of the mode can
8509 if (code == RROTATE_EXPR && TREE_CODE (arg1) == INTEGER_CST
8510 && TREE_CODE (arg0) == RROTATE_EXPR
8511 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
8512 && TREE_INT_CST_HIGH (arg1) == 0
8513 && TREE_INT_CST_HIGH (TREE_OPERAND (arg0, 1)) == 0
8514 && ((TREE_INT_CST_LOW (arg1)
8515 + TREE_INT_CST_LOW (TREE_OPERAND (arg0, 1)))
8516 == (unsigned int) GET_MODE_BITSIZE (TYPE_MODE (type))))
8517 return TREE_OPERAND (arg0, 0);
8522 if (operand_equal_p (arg0, arg1, 0))
8523 return omit_one_operand (type, arg0, arg1);
8524 if (INTEGRAL_TYPE_P (type)
8525 && operand_equal_p (arg1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
8526 return omit_one_operand (type, arg1, arg0);
8530 if (operand_equal_p (arg0, arg1, 0))
8531 return omit_one_operand (type, arg0, arg1);
8532 if (INTEGRAL_TYPE_P (type)
8533 && TYPE_MAX_VALUE (type)
8534 && operand_equal_p (arg1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
8535 return omit_one_operand (type, arg1, arg0);
8538 case TRUTH_ANDIF_EXPR:
8539 /* Note that the operands of this must be ints
8540 and their values must be 0 or 1.
8541 ("true" is a fixed value perhaps depending on the language.) */
8542 /* If first arg is constant zero, return it. */
8543 if (integer_zerop (arg0))
8544 return fold_convert (type, arg0);
8545 case TRUTH_AND_EXPR:
8546 /* If either arg is constant true, drop it. */
8547 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
8548 return non_lvalue (fold_convert (type, arg1));
8549 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1)
8550 /* Preserve sequence points. */
8551 && (code != TRUTH_ANDIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
8552 return non_lvalue (fold_convert (type, arg0));
8553 /* If second arg is constant zero, result is zero, but first arg
8554 must be evaluated. */
8555 if (integer_zerop (arg1))
8556 return omit_one_operand (type, arg1, arg0);
8557 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
8558 case will be handled here. */
8559 if (integer_zerop (arg0))
8560 return omit_one_operand (type, arg0, arg1);
8562 /* !X && X is always false. */
8563 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
8564 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
8565 return omit_one_operand (type, integer_zero_node, arg1);
8566 /* X && !X is always false. */
8567 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
8568 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
8569 return omit_one_operand (type, integer_zero_node, arg0);
8571 /* A < X && A + 1 > Y ==> A < X && A >= Y. Normally A + 1 > Y
8572 means A >= Y && A != MAX, but in this case we know that
8575 if (!TREE_SIDE_EFFECTS (arg0)
8576 && !TREE_SIDE_EFFECTS (arg1))
8578 tem = fold_to_nonsharp_ineq_using_bound (arg0, arg1);
8580 return fold (build2 (code, type, tem, arg1));
8582 tem = fold_to_nonsharp_ineq_using_bound (arg1, arg0);
8584 return fold (build2 (code, type, arg0, tem));
8588 /* We only do these simplifications if we are optimizing. */
8592 /* Check for things like (A || B) && (A || C). We can convert this
8593 to A || (B && C). Note that either operator can be any of the four
8594 truth and/or operations and the transformation will still be
8595 valid. Also note that we only care about order for the
8596 ANDIF and ORIF operators. If B contains side effects, this
8597 might change the truth-value of A. */
8598 if (TREE_CODE (arg0) == TREE_CODE (arg1)
8599 && (TREE_CODE (arg0) == TRUTH_ANDIF_EXPR
8600 || TREE_CODE (arg0) == TRUTH_ORIF_EXPR
8601 || TREE_CODE (arg0) == TRUTH_AND_EXPR
8602 || TREE_CODE (arg0) == TRUTH_OR_EXPR)
8603 && ! TREE_SIDE_EFFECTS (TREE_OPERAND (arg0, 1)))
8605 tree a00 = TREE_OPERAND (arg0, 0);
8606 tree a01 = TREE_OPERAND (arg0, 1);
8607 tree a10 = TREE_OPERAND (arg1, 0);
8608 tree a11 = TREE_OPERAND (arg1, 1);
8609 int commutative = ((TREE_CODE (arg0) == TRUTH_OR_EXPR
8610 || TREE_CODE (arg0) == TRUTH_AND_EXPR)
8611 && (code == TRUTH_AND_EXPR
8612 || code == TRUTH_OR_EXPR));
8614 if (operand_equal_p (a00, a10, 0))
8615 return fold (build2 (TREE_CODE (arg0), type, a00,
8616 fold (build2 (code, type, a01, a11))));
8617 else if (commutative && operand_equal_p (a00, a11, 0))
8618 return fold (build2 (TREE_CODE (arg0), type, a00,
8619 fold (build2 (code, type, a01, a10))));
8620 else if (commutative && operand_equal_p (a01, a10, 0))
8621 return fold (build2 (TREE_CODE (arg0), type, a01,
8622 fold (build2 (code, type, a00, a11))));
8624 /* This case if tricky because we must either have commutative
8625 operators or else A10 must not have side-effects. */
8627 else if ((commutative || ! TREE_SIDE_EFFECTS (a10))
8628 && operand_equal_p (a01, a11, 0))
8629 return fold (build2 (TREE_CODE (arg0), type,
8630 fold (build2 (code, type, a00, a10)),
8634 /* See if we can build a range comparison. */
8635 if (0 != (tem = fold_range_test (code, type, op0, op1)))
8638 /* Check for the possibility of merging component references. If our
8639 lhs is another similar operation, try to merge its rhs with our
8640 rhs. Then try to merge our lhs and rhs. */
8641 if (TREE_CODE (arg0) == code
8642 && 0 != (tem = fold_truthop (code, type,
8643 TREE_OPERAND (arg0, 1), arg1)))
8644 return fold (build2 (code, type, TREE_OPERAND (arg0, 0), tem));
8646 if ((tem = fold_truthop (code, type, arg0, arg1)) != 0)
8651 case TRUTH_ORIF_EXPR:
8652 /* Note that the operands of this must be ints
8653 and their values must be 0 or true.
8654 ("true" is a fixed value perhaps depending on the language.) */
8655 /* If first arg is constant true, return it. */
8656 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
8657 return fold_convert (type, arg0);
8659 /* If either arg is constant zero, drop it. */
8660 if (TREE_CODE (arg0) == INTEGER_CST && integer_zerop (arg0))
8661 return non_lvalue (fold_convert (type, arg1));
8662 if (TREE_CODE (arg1) == INTEGER_CST && integer_zerop (arg1)
8663 /* Preserve sequence points. */
8664 && (code != TRUTH_ORIF_EXPR || ! TREE_SIDE_EFFECTS (arg0)))
8665 return non_lvalue (fold_convert (type, arg0));
8666 /* If second arg is constant true, result is true, but we must
8667 evaluate first arg. */
8668 if (TREE_CODE (arg1) == INTEGER_CST && ! integer_zerop (arg1))
8669 return omit_one_operand (type, arg1, arg0);
8670 /* Likewise for first arg, but note this only occurs here for
8672 if (TREE_CODE (arg0) == INTEGER_CST && ! integer_zerop (arg0))
8673 return omit_one_operand (type, arg0, arg1);
8675 /* !X || X is always true. */
8676 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
8677 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
8678 return omit_one_operand (type, integer_one_node, arg1);
8679 /* X || !X is always true. */
8680 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
8681 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
8682 return omit_one_operand (type, integer_one_node, arg0);
8686 case TRUTH_XOR_EXPR:
8687 /* If the second arg is constant zero, drop it. */
8688 if (integer_zerop (arg1))
8689 return non_lvalue (fold_convert (type, arg0));
8690 /* If the second arg is constant true, this is a logical inversion. */
8691 if (integer_onep (arg1))
8692 return non_lvalue (fold_convert (type, invert_truthvalue (arg0)));
8693 /* Identical arguments cancel to zero. */
8694 if (operand_equal_p (arg0, arg1, 0))
8695 return omit_one_operand (type, integer_zero_node, arg0);
8697 /* !X ^ X is always true. */
8698 if (TREE_CODE (arg0) == TRUTH_NOT_EXPR
8699 && operand_equal_p (TREE_OPERAND (arg0, 0), arg1, 0))
8700 return omit_one_operand (type, integer_one_node, arg1);
8702 /* X ^ !X is always true. */
8703 if (TREE_CODE (arg1) == TRUTH_NOT_EXPR
8704 && operand_equal_p (arg0, TREE_OPERAND (arg1, 0), 0))
8705 return omit_one_operand (type, integer_one_node, arg0);
8715 /* If one arg is a real or integer constant, put it last. */
8716 if (tree_swap_operands_p (arg0, arg1, true))
8717 return fold (build2 (swap_tree_comparison (code), type, arg1, arg0));
8719 /* If this is an equality comparison of the address of a non-weak
8720 object against zero, then we know the result. */
8721 if ((code == EQ_EXPR || code == NE_EXPR)
8722 && TREE_CODE (arg0) == ADDR_EXPR
8723 && DECL_P (TREE_OPERAND (arg0, 0))
8724 && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
8725 && integer_zerop (arg1))
8726 return constant_boolean_node (code != EQ_EXPR, type);
8728 /* If this is an equality comparison of the address of two non-weak,
8729 unaliased symbols neither of which are extern (since we do not
8730 have access to attributes for externs), then we know the result. */
8731 if ((code == EQ_EXPR || code == NE_EXPR)
8732 && TREE_CODE (arg0) == ADDR_EXPR
8733 && DECL_P (TREE_OPERAND (arg0, 0))
8734 && ! DECL_WEAK (TREE_OPERAND (arg0, 0))
8735 && ! lookup_attribute ("alias",
8736 DECL_ATTRIBUTES (TREE_OPERAND (arg0, 0)))
8737 && ! DECL_EXTERNAL (TREE_OPERAND (arg0, 0))
8738 && TREE_CODE (arg1) == ADDR_EXPR
8739 && DECL_P (TREE_OPERAND (arg1, 0))
8740 && ! DECL_WEAK (TREE_OPERAND (arg1, 0))
8741 && ! lookup_attribute ("alias",
8742 DECL_ATTRIBUTES (TREE_OPERAND (arg1, 0)))
8743 && ! DECL_EXTERNAL (TREE_OPERAND (arg1, 0)))
8744 return constant_boolean_node (operand_equal_p (arg0, arg1, 0)
8745 ? code == EQ_EXPR : code != EQ_EXPR,
8748 /* If this is a comparison of two exprs that look like an
8749 ARRAY_REF of the same object, then we can fold this to a
8750 comparison of the two offsets. */
8751 if (TREE_CODE_CLASS (code) == tcc_comparison)
8753 tree base0, offset0, base1, offset1;
8755 if (extract_array_ref (arg0, &base0, &offset0)
8756 && extract_array_ref (arg1, &base1, &offset1)
8757 && operand_equal_p (base0, base1, 0))
8759 if (offset0 == NULL_TREE
8760 && offset1 == NULL_TREE)
8762 offset0 = integer_zero_node;
8763 offset1 = integer_zero_node;
8765 else if (offset0 == NULL_TREE)
8766 offset0 = build_int_cst (TREE_TYPE (offset1), 0);
8767 else if (offset1 == NULL_TREE)
8768 offset1 = build_int_cst (TREE_TYPE (offset0), 0);
8770 if (TREE_TYPE (offset0) == TREE_TYPE (offset1))
8771 return fold (build2 (code, type, offset0, offset1));
8775 if (FLOAT_TYPE_P (TREE_TYPE (arg0)))
8777 tree targ0 = strip_float_extensions (arg0);
8778 tree targ1 = strip_float_extensions (arg1);
8779 tree newtype = TREE_TYPE (targ0);
8781 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
8782 newtype = TREE_TYPE (targ1);
8784 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
8785 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
8786 return fold (build2 (code, type, fold_convert (newtype, targ0),
8787 fold_convert (newtype, targ1)));
8789 /* (-a) CMP (-b) -> b CMP a */
8790 if (TREE_CODE (arg0) == NEGATE_EXPR
8791 && TREE_CODE (arg1) == NEGATE_EXPR)
8792 return fold (build2 (code, type, TREE_OPERAND (arg1, 0),
8793 TREE_OPERAND (arg0, 0)));
8795 if (TREE_CODE (arg1) == REAL_CST)
8797 REAL_VALUE_TYPE cst;
8798 cst = TREE_REAL_CST (arg1);
8800 /* (-a) CMP CST -> a swap(CMP) (-CST) */
8801 if (TREE_CODE (arg0) == NEGATE_EXPR)
8803 fold (build2 (swap_tree_comparison (code), type,
8804 TREE_OPERAND (arg0, 0),
8805 build_real (TREE_TYPE (arg1),
8806 REAL_VALUE_NEGATE (cst))));
8808 /* IEEE doesn't distinguish +0 and -0 in comparisons. */
8809 /* a CMP (-0) -> a CMP 0 */
8810 if (REAL_VALUE_MINUS_ZERO (cst))
8811 return fold (build2 (code, type, arg0,
8812 build_real (TREE_TYPE (arg1), dconst0)));
8814 /* x != NaN is always true, other ops are always false. */
8815 if (REAL_VALUE_ISNAN (cst)
8816 && ! HONOR_SNANS (TYPE_MODE (TREE_TYPE (arg1))))
8818 tem = (code == NE_EXPR) ? integer_one_node : integer_zero_node;
8819 return omit_one_operand (type, tem, arg0);
8822 /* Fold comparisons against infinity. */
8823 if (REAL_VALUE_ISINF (cst))
8825 tem = fold_inf_compare (code, type, arg0, arg1);
8826 if (tem != NULL_TREE)
8831 /* If this is a comparison of a real constant with a PLUS_EXPR
8832 or a MINUS_EXPR of a real constant, we can convert it into a
8833 comparison with a revised real constant as long as no overflow
8834 occurs when unsafe_math_optimizations are enabled. */
8835 if (flag_unsafe_math_optimizations
8836 && TREE_CODE (arg1) == REAL_CST
8837 && (TREE_CODE (arg0) == PLUS_EXPR
8838 || TREE_CODE (arg0) == MINUS_EXPR)
8839 && TREE_CODE (TREE_OPERAND (arg0, 1)) == REAL_CST
8840 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
8841 ? MINUS_EXPR : PLUS_EXPR,
8842 arg1, TREE_OPERAND (arg0, 1), 0))
8843 && ! TREE_CONSTANT_OVERFLOW (tem))
8844 return fold (build2 (code, type, TREE_OPERAND (arg0, 0), tem));
8846 /* Likewise, we can simplify a comparison of a real constant with
8847 a MINUS_EXPR whose first operand is also a real constant, i.e.
8848 (c1 - x) < c2 becomes x > c1-c2. */
8849 if (flag_unsafe_math_optimizations
8850 && TREE_CODE (arg1) == REAL_CST
8851 && TREE_CODE (arg0) == MINUS_EXPR
8852 && TREE_CODE (TREE_OPERAND (arg0, 0)) == REAL_CST
8853 && 0 != (tem = const_binop (MINUS_EXPR, TREE_OPERAND (arg0, 0),
8855 && ! TREE_CONSTANT_OVERFLOW (tem))
8856 return fold (build2 (swap_tree_comparison (code), type,
8857 TREE_OPERAND (arg0, 1), tem));
8859 /* Fold comparisons against built-in math functions. */
8860 if (TREE_CODE (arg1) == REAL_CST
8861 && flag_unsafe_math_optimizations
8862 && ! flag_errno_math)
8864 enum built_in_function fcode = builtin_mathfn_code (arg0);
8866 if (fcode != END_BUILTINS)
8868 tem = fold_mathfn_compare (fcode, code, type, arg0, arg1);
8869 if (tem != NULL_TREE)
8875 /* Convert foo++ == CONST into ++foo == CONST + INCR. */
8876 if (TREE_CONSTANT (arg1)
8877 && (TREE_CODE (arg0) == POSTINCREMENT_EXPR
8878 || TREE_CODE (arg0) == POSTDECREMENT_EXPR)
8879 /* This optimization is invalid for ordered comparisons
8880 if CONST+INCR overflows or if foo+incr might overflow.
8881 This optimization is invalid for floating point due to rounding.
8882 For pointer types we assume overflow doesn't happen. */
8883 && (POINTER_TYPE_P (TREE_TYPE (arg0))
8884 || (INTEGRAL_TYPE_P (TREE_TYPE (arg0))
8885 && (code == EQ_EXPR || code == NE_EXPR))))
8887 tree varop, newconst;
8889 if (TREE_CODE (arg0) == POSTINCREMENT_EXPR)
8891 newconst = fold (build2 (PLUS_EXPR, TREE_TYPE (arg0),
8892 arg1, TREE_OPERAND (arg0, 1)));
8893 varop = build2 (PREINCREMENT_EXPR, TREE_TYPE (arg0),
8894 TREE_OPERAND (arg0, 0),
8895 TREE_OPERAND (arg0, 1));
8899 newconst = fold (build2 (MINUS_EXPR, TREE_TYPE (arg0),
8900 arg1, TREE_OPERAND (arg0, 1)));
8901 varop = build2 (PREDECREMENT_EXPR, TREE_TYPE (arg0),
8902 TREE_OPERAND (arg0, 0),
8903 TREE_OPERAND (arg0, 1));
8907 /* If VAROP is a reference to a bitfield, we must mask
8908 the constant by the width of the field. */
8909 if (TREE_CODE (TREE_OPERAND (varop, 0)) == COMPONENT_REF
8910 && DECL_BIT_FIELD (TREE_OPERAND (TREE_OPERAND (varop, 0), 1))
8911 && host_integerp (DECL_SIZE (TREE_OPERAND
8912 (TREE_OPERAND (varop, 0), 1)), 1))
8914 tree fielddecl = TREE_OPERAND (TREE_OPERAND (varop, 0), 1);
8915 HOST_WIDE_INT size = tree_low_cst (DECL_SIZE (fielddecl), 1);
8916 tree folded_compare, shift;
8918 /* First check whether the comparison would come out
8919 always the same. If we don't do that we would
8920 change the meaning with the masking. */
8921 folded_compare = fold (build2 (code, type,
8922 TREE_OPERAND (varop, 0), arg1));
8923 if (integer_zerop (folded_compare)
8924 || integer_onep (folded_compare))
8925 return omit_one_operand (type, folded_compare, varop);
8927 shift = build_int_cst (NULL_TREE,
8928 TYPE_PRECISION (TREE_TYPE (varop)) - size);
8929 shift = fold_convert (TREE_TYPE (varop), shift);
8930 newconst = fold (build2 (LSHIFT_EXPR, TREE_TYPE (varop),
8932 newconst = fold (build2 (RSHIFT_EXPR, TREE_TYPE (varop),
8936 return fold (build2 (code, type, varop, newconst));
8939 /* Change X >= C to X > (C - 1) and X < C to X <= (C - 1) if C > 0.
8940 This transformation affects the cases which are handled in later
8941 optimizations involving comparisons with non-negative constants. */
8942 if (TREE_CODE (arg1) == INTEGER_CST
8943 && TREE_CODE (arg0) != INTEGER_CST
8944 && tree_int_cst_sgn (arg1) > 0)
8949 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
8950 return fold (build2 (GT_EXPR, type, arg0, arg1));
8953 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
8954 return fold (build2 (LE_EXPR, type, arg0, arg1));
8961 /* Comparisons with the highest or lowest possible integer of
8962 the specified size will have known values.
8964 This is quite similar to fold_relational_hi_lo, however,
8965 attempts to share the code have been nothing but trouble. */
8967 int width = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (arg1)));
8969 if (TREE_CODE (arg1) == INTEGER_CST
8970 && ! TREE_CONSTANT_OVERFLOW (arg1)
8971 && width <= 2 * HOST_BITS_PER_WIDE_INT
8972 && (INTEGRAL_TYPE_P (TREE_TYPE (arg1))
8973 || POINTER_TYPE_P (TREE_TYPE (arg1))))
8975 HOST_WIDE_INT signed_max_hi;
8976 unsigned HOST_WIDE_INT signed_max_lo;
8977 unsigned HOST_WIDE_INT max_hi, max_lo, min_hi, min_lo;
8979 if (width <= HOST_BITS_PER_WIDE_INT)
8981 signed_max_lo = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
8986 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
8988 max_lo = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
8994 max_lo = signed_max_lo;
8995 min_lo = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
9001 width -= HOST_BITS_PER_WIDE_INT;
9003 signed_max_hi = ((unsigned HOST_WIDE_INT) 1 << (width - 1))
9008 if (TYPE_UNSIGNED (TREE_TYPE (arg1)))
9010 max_hi = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
9015 max_hi = signed_max_hi;
9016 min_hi = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
9020 if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1) == max_hi
9021 && TREE_INT_CST_LOW (arg1) == max_lo)
9025 return omit_one_operand (type, integer_zero_node, arg0);
9028 return fold (build2 (EQ_EXPR, type, arg0, arg1));
9031 return omit_one_operand (type, integer_one_node, arg0);
9034 return fold (build2 (NE_EXPR, type, arg0, arg1));
9036 /* The GE_EXPR and LT_EXPR cases above are not normally
9037 reached because of previous transformations. */
9042 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
9044 && TREE_INT_CST_LOW (arg1) == max_lo - 1)
9048 arg1 = const_binop (PLUS_EXPR, arg1, integer_one_node, 0);
9049 return fold (build2 (EQ_EXPR, type, arg0, arg1));
9051 arg1 = const_binop (PLUS_EXPR, arg1, integer_one_node, 0);
9052 return fold (build2 (NE_EXPR, type, arg0, arg1));
9056 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
9058 && TREE_INT_CST_LOW (arg1) == min_lo)
9062 return omit_one_operand (type, integer_zero_node, arg0);
9065 return fold (build2 (EQ_EXPR, type, arg0, arg1));
9068 return omit_one_operand (type, integer_one_node, arg0);
9071 return fold (build2 (NE_EXPR, type, arg0, arg1));
9076 else if ((unsigned HOST_WIDE_INT) TREE_INT_CST_HIGH (arg1)
9078 && TREE_INT_CST_LOW (arg1) == min_lo + 1)
9082 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
9083 return fold (build2 (NE_EXPR, type, arg0, arg1));
9085 arg1 = const_binop (MINUS_EXPR, arg1, integer_one_node, 0);
9086 return fold (build2 (EQ_EXPR, type, arg0, arg1));
9091 else if (!in_gimple_form
9092 && TREE_INT_CST_HIGH (arg1) == signed_max_hi
9093 && TREE_INT_CST_LOW (arg1) == signed_max_lo
9094 && TYPE_UNSIGNED (TREE_TYPE (arg1))
9095 /* signed_type does not work on pointer types. */
9096 && INTEGRAL_TYPE_P (TREE_TYPE (arg1)))
9098 /* The following case also applies to X < signed_max+1
9099 and X >= signed_max+1 because previous transformations. */
9100 if (code == LE_EXPR || code == GT_EXPR)
9103 st0 = lang_hooks.types.signed_type (TREE_TYPE (arg0));
9104 st1 = lang_hooks.types.signed_type (TREE_TYPE (arg1));
9106 (build2 (code == LE_EXPR ? GE_EXPR: LT_EXPR,
9107 type, fold_convert (st0, arg0),
9108 fold_convert (st1, integer_zero_node)));
9114 /* If this is an EQ or NE comparison of a constant with a PLUS_EXPR or
9115 a MINUS_EXPR of a constant, we can convert it into a comparison with
9116 a revised constant as long as no overflow occurs. */
9117 if ((code == EQ_EXPR || code == NE_EXPR)
9118 && TREE_CODE (arg1) == INTEGER_CST
9119 && (TREE_CODE (arg0) == PLUS_EXPR
9120 || TREE_CODE (arg0) == MINUS_EXPR)
9121 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9122 && 0 != (tem = const_binop (TREE_CODE (arg0) == PLUS_EXPR
9123 ? MINUS_EXPR : PLUS_EXPR,
9124 arg1, TREE_OPERAND (arg0, 1), 0))
9125 && ! TREE_CONSTANT_OVERFLOW (tem))
9126 return fold (build2 (code, type, TREE_OPERAND (arg0, 0), tem));
9128 /* Similarly for a NEGATE_EXPR. */
9129 else if ((code == EQ_EXPR || code == NE_EXPR)
9130 && TREE_CODE (arg0) == NEGATE_EXPR
9131 && TREE_CODE (arg1) == INTEGER_CST
9132 && 0 != (tem = negate_expr (arg1))
9133 && TREE_CODE (tem) == INTEGER_CST
9134 && ! TREE_CONSTANT_OVERFLOW (tem))
9135 return fold (build2 (code, type, TREE_OPERAND (arg0, 0), tem));
9137 /* If we have X - Y == 0, we can convert that to X == Y and similarly
9138 for !=. Don't do this for ordered comparisons due to overflow. */
9139 else if ((code == NE_EXPR || code == EQ_EXPR)
9140 && integer_zerop (arg1) && TREE_CODE (arg0) == MINUS_EXPR)
9141 return fold (build2 (code, type,
9142 TREE_OPERAND (arg0, 0), TREE_OPERAND (arg0, 1)));
9144 else if (TREE_CODE (TREE_TYPE (arg0)) == INTEGER_TYPE
9145 && TREE_CODE (arg0) == NOP_EXPR)
9147 /* If we are widening one operand of an integer comparison,
9148 see if the other operand is similarly being widened. Perhaps we
9149 can do the comparison in the narrower type. */
9150 tem = fold_widened_comparison (code, type, arg0, arg1);
9154 /* Or if we are changing signedness. */
9155 tem = fold_sign_changed_comparison (code, type, arg0, arg1);
9160 /* If this is comparing a constant with a MIN_EXPR or a MAX_EXPR of a
9161 constant, we can simplify it. */
9162 else if (TREE_CODE (arg1) == INTEGER_CST
9163 && (TREE_CODE (arg0) == MIN_EXPR
9164 || TREE_CODE (arg0) == MAX_EXPR)
9165 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9167 tem = optimize_minmax_comparison (code, type, op0, op1);
9174 /* If we are comparing an ABS_EXPR with a constant, we can
9175 convert all the cases into explicit comparisons, but they may
9176 well not be faster than doing the ABS and one comparison.
9177 But ABS (X) <= C is a range comparison, which becomes a subtraction
9178 and a comparison, and is probably faster. */
9179 else if (code == LE_EXPR && TREE_CODE (arg1) == INTEGER_CST
9180 && TREE_CODE (arg0) == ABS_EXPR
9181 && ! TREE_SIDE_EFFECTS (arg0)
9182 && (0 != (tem = negate_expr (arg1)))
9183 && TREE_CODE (tem) == INTEGER_CST
9184 && ! TREE_CONSTANT_OVERFLOW (tem))
9185 return fold (build2 (TRUTH_ANDIF_EXPR, type,
9186 build2 (GE_EXPR, type,
9187 TREE_OPERAND (arg0, 0), tem),
9188 build2 (LE_EXPR, type,
9189 TREE_OPERAND (arg0, 0), arg1)));
9191 /* Convert ABS_EXPR<x> >= 0 to true. */
9192 else if (code == GE_EXPR
9193 && tree_expr_nonnegative_p (arg0)
9194 && (integer_zerop (arg1)
9195 || (! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0)))
9196 && real_zerop (arg1))))
9197 return omit_one_operand (type, integer_one_node, arg0);
9199 /* Convert ABS_EXPR<x> < 0 to false. */
9200 else if (code == LT_EXPR
9201 && tree_expr_nonnegative_p (arg0)
9202 && (integer_zerop (arg1) || real_zerop (arg1)))
9203 return omit_one_operand (type, integer_zero_node, arg0);
9205 /* Convert ABS_EXPR<x> == 0 or ABS_EXPR<x> != 0 to x == 0 or x != 0. */
9206 else if ((code == EQ_EXPR || code == NE_EXPR)
9207 && TREE_CODE (arg0) == ABS_EXPR
9208 && (integer_zerop (arg1) || real_zerop (arg1)))
9209 return fold (build2 (code, type, TREE_OPERAND (arg0, 0), arg1));
9211 /* If this is an EQ or NE comparison with zero and ARG0 is
9212 (1 << foo) & bar, convert it to (bar >> foo) & 1. Both require
9213 two operations, but the latter can be done in one less insn
9214 on machines that have only two-operand insns or on which a
9215 constant cannot be the first operand. */
9216 if (integer_zerop (arg1) && (code == EQ_EXPR || code == NE_EXPR)
9217 && TREE_CODE (arg0) == BIT_AND_EXPR)
9219 tree arg00 = TREE_OPERAND (arg0, 0);
9220 tree arg01 = TREE_OPERAND (arg0, 1);
9221 if (TREE_CODE (arg00) == LSHIFT_EXPR
9222 && integer_onep (TREE_OPERAND (arg00, 0)))
9224 fold (build2 (code, type,
9225 build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
9226 build2 (RSHIFT_EXPR, TREE_TYPE (arg00),
9227 arg01, TREE_OPERAND (arg00, 1)),
9228 fold_convert (TREE_TYPE (arg0),
9231 else if (TREE_CODE (TREE_OPERAND (arg0, 1)) == LSHIFT_EXPR
9232 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg0, 1), 0)))
9234 fold (build2 (code, type,
9235 build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
9236 build2 (RSHIFT_EXPR, TREE_TYPE (arg01),
9237 arg00, TREE_OPERAND (arg01, 1)),
9238 fold_convert (TREE_TYPE (arg0),
9243 /* If this is an NE or EQ comparison of zero against the result of a
9244 signed MOD operation whose second operand is a power of 2, make
9245 the MOD operation unsigned since it is simpler and equivalent. */
9246 if ((code == NE_EXPR || code == EQ_EXPR)
9247 && integer_zerop (arg1)
9248 && !TYPE_UNSIGNED (TREE_TYPE (arg0))
9249 && (TREE_CODE (arg0) == TRUNC_MOD_EXPR
9250 || TREE_CODE (arg0) == CEIL_MOD_EXPR
9251 || TREE_CODE (arg0) == FLOOR_MOD_EXPR
9252 || TREE_CODE (arg0) == ROUND_MOD_EXPR)
9253 && integer_pow2p (TREE_OPERAND (arg0, 1)))
9255 tree newtype = lang_hooks.types.unsigned_type (TREE_TYPE (arg0));
9256 tree newmod = fold (build2 (TREE_CODE (arg0), newtype,
9257 fold_convert (newtype,
9258 TREE_OPERAND (arg0, 0)),
9259 fold_convert (newtype,
9260 TREE_OPERAND (arg0, 1))));
9262 return fold (build2 (code, type, newmod,
9263 fold_convert (newtype, arg1)));
9266 /* If this is an NE comparison of zero with an AND of one, remove the
9267 comparison since the AND will give the correct value. */
9268 if (code == NE_EXPR && integer_zerop (arg1)
9269 && TREE_CODE (arg0) == BIT_AND_EXPR
9270 && integer_onep (TREE_OPERAND (arg0, 1)))
9271 return fold_convert (type, arg0);
9273 /* If we have (A & C) == C where C is a power of 2, convert this into
9274 (A & C) != 0. Similarly for NE_EXPR. */
9275 if ((code == EQ_EXPR || code == NE_EXPR)
9276 && TREE_CODE (arg0) == BIT_AND_EXPR
9277 && integer_pow2p (TREE_OPERAND (arg0, 1))
9278 && operand_equal_p (TREE_OPERAND (arg0, 1), arg1, 0))
9279 return fold (build2 (code == EQ_EXPR ? NE_EXPR : EQ_EXPR, type,
9280 arg0, fold_convert (TREE_TYPE (arg0),
9281 integer_zero_node)));
9283 /* If we have (A & C) != 0 or (A & C) == 0 and C is a power of
9284 2, then fold the expression into shifts and logical operations. */
9285 tem = fold_single_bit_test (code, arg0, arg1, type);
9289 /* If we have (A & C) == D where D & ~C != 0, convert this into 0.
9290 Similarly for NE_EXPR. */
9291 if ((code == EQ_EXPR || code == NE_EXPR)
9292 && TREE_CODE (arg0) == BIT_AND_EXPR
9293 && TREE_CODE (arg1) == INTEGER_CST
9294 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9296 tree notc = fold (build1 (BIT_NOT_EXPR,
9297 TREE_TYPE (TREE_OPERAND (arg0, 1)),
9298 TREE_OPERAND (arg0, 1)));
9299 tree dandnotc = fold (build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
9301 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
9302 if (integer_nonzerop (dandnotc))
9303 return omit_one_operand (type, rslt, arg0);
9306 /* If we have (A | C) == D where C & ~D != 0, convert this into 0.
9307 Similarly for NE_EXPR. */
9308 if ((code == EQ_EXPR || code == NE_EXPR)
9309 && TREE_CODE (arg0) == BIT_IOR_EXPR
9310 && TREE_CODE (arg1) == INTEGER_CST
9311 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST)
9313 tree notd = fold (build1 (BIT_NOT_EXPR, TREE_TYPE (arg1), arg1));
9314 tree candnotd = fold (build2 (BIT_AND_EXPR, TREE_TYPE (arg0),
9315 TREE_OPERAND (arg0, 1), notd));
9316 tree rslt = code == EQ_EXPR ? integer_zero_node : integer_one_node;
9317 if (integer_nonzerop (candnotd))
9318 return omit_one_operand (type, rslt, arg0);
9321 /* If X is unsigned, convert X < (1 << Y) into X >> Y == 0
9322 and similarly for >= into !=. */
9323 if ((code == LT_EXPR || code == GE_EXPR)
9324 && TYPE_UNSIGNED (TREE_TYPE (arg0))
9325 && TREE_CODE (arg1) == LSHIFT_EXPR
9326 && integer_onep (TREE_OPERAND (arg1, 0)))
9327 return build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
9328 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
9329 TREE_OPERAND (arg1, 1)),
9330 fold_convert (TREE_TYPE (arg0), integer_zero_node));
9332 else if ((code == LT_EXPR || code == GE_EXPR)
9333 && TYPE_UNSIGNED (TREE_TYPE (arg0))
9334 && (TREE_CODE (arg1) == NOP_EXPR
9335 || TREE_CODE (arg1) == CONVERT_EXPR)
9336 && TREE_CODE (TREE_OPERAND (arg1, 0)) == LSHIFT_EXPR
9337 && integer_onep (TREE_OPERAND (TREE_OPERAND (arg1, 0), 0)))
9339 build2 (code == LT_EXPR ? EQ_EXPR : NE_EXPR, type,
9340 fold_convert (TREE_TYPE (arg0),
9341 build2 (RSHIFT_EXPR, TREE_TYPE (arg0), arg0,
9342 TREE_OPERAND (TREE_OPERAND (arg1, 0),
9344 fold_convert (TREE_TYPE (arg0), integer_zero_node));
9346 /* Simplify comparison of something with itself. (For IEEE
9347 floating-point, we can only do some of these simplifications.) */
9348 if (operand_equal_p (arg0, arg1, 0))
9353 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9354 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9355 return constant_boolean_node (1, type);
9360 if (! FLOAT_TYPE_P (TREE_TYPE (arg0))
9361 || ! HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9362 return constant_boolean_node (1, type);
9363 return fold (build2 (EQ_EXPR, type, arg0, arg1));
9366 /* For NE, we can only do this simplification if integer
9367 or we don't honor IEEE floating point NaNs. */
9368 if (FLOAT_TYPE_P (TREE_TYPE (arg0))
9369 && HONOR_NANS (TYPE_MODE (TREE_TYPE (arg0))))
9371 /* ... fall through ... */
9374 return constant_boolean_node (0, type);
9380 /* If we are comparing an expression that just has comparisons
9381 of two integer values, arithmetic expressions of those comparisons,
9382 and constants, we can simplify it. There are only three cases
9383 to check: the two values can either be equal, the first can be
9384 greater, or the second can be greater. Fold the expression for
9385 those three values. Since each value must be 0 or 1, we have
9386 eight possibilities, each of which corresponds to the constant 0
9387 or 1 or one of the six possible comparisons.
9389 This handles common cases like (a > b) == 0 but also handles
9390 expressions like ((x > y) - (y > x)) > 0, which supposedly
9391 occur in macroized code. */
9393 if (TREE_CODE (arg1) == INTEGER_CST && TREE_CODE (arg0) != INTEGER_CST)
9395 tree cval1 = 0, cval2 = 0;
9398 if (twoval_comparison_p (arg0, &cval1, &cval2, &save_p)
9399 /* Don't handle degenerate cases here; they should already
9400 have been handled anyway. */
9401 && cval1 != 0 && cval2 != 0
9402 && ! (TREE_CONSTANT (cval1) && TREE_CONSTANT (cval2))
9403 && TREE_TYPE (cval1) == TREE_TYPE (cval2)
9404 && INTEGRAL_TYPE_P (TREE_TYPE (cval1))
9405 && TYPE_MAX_VALUE (TREE_TYPE (cval1))
9406 && TYPE_MAX_VALUE (TREE_TYPE (cval2))
9407 && ! operand_equal_p (TYPE_MIN_VALUE (TREE_TYPE (cval1)),
9408 TYPE_MAX_VALUE (TREE_TYPE (cval2)), 0))
9410 tree maxval = TYPE_MAX_VALUE (TREE_TYPE (cval1));
9411 tree minval = TYPE_MIN_VALUE (TREE_TYPE (cval1));
9413 /* We can't just pass T to eval_subst in case cval1 or cval2
9414 was the same as ARG1. */
9417 = fold (build2 (code, type,
9418 eval_subst (arg0, cval1, maxval,
9422 = fold (build2 (code, type,
9423 eval_subst (arg0, cval1, maxval,
9427 = fold (build2 (code, type,
9428 eval_subst (arg0, cval1, minval,
9432 /* All three of these results should be 0 or 1. Confirm they
9433 are. Then use those values to select the proper code
9436 if ((integer_zerop (high_result)
9437 || integer_onep (high_result))
9438 && (integer_zerop (equal_result)
9439 || integer_onep (equal_result))
9440 && (integer_zerop (low_result)
9441 || integer_onep (low_result)))
9443 /* Make a 3-bit mask with the high-order bit being the
9444 value for `>', the next for '=', and the low for '<'. */
9445 switch ((integer_onep (high_result) * 4)
9446 + (integer_onep (equal_result) * 2)
9447 + integer_onep (low_result))
9451 return omit_one_operand (type, integer_zero_node, arg0);
9472 return omit_one_operand (type, integer_one_node, arg0);
9475 tem = build2 (code, type, cval1, cval2);
9477 return save_expr (tem);
9484 /* If this is a comparison of a field, we may be able to simplify it. */
9485 if (((TREE_CODE (arg0) == COMPONENT_REF
9486 && lang_hooks.can_use_bit_fields_p ())
9487 || TREE_CODE (arg0) == BIT_FIELD_REF)
9488 && (code == EQ_EXPR || code == NE_EXPR)
9489 /* Handle the constant case even without -O
9490 to make sure the warnings are given. */
9491 && (optimize || TREE_CODE (arg1) == INTEGER_CST))
9493 t1 = optimize_bit_field_compare (code, type, arg0, arg1);
9498 /* If this is a comparison of complex values and either or both sides
9499 are a COMPLEX_EXPR or COMPLEX_CST, it is best to split up the
9500 comparisons and join them with a TRUTH_ANDIF_EXPR or TRUTH_ORIF_EXPR.
9501 This may prevent needless evaluations. */
9502 if ((code == EQ_EXPR || code == NE_EXPR)
9503 && TREE_CODE (TREE_TYPE (arg0)) == COMPLEX_TYPE
9504 && (TREE_CODE (arg0) == COMPLEX_EXPR
9505 || TREE_CODE (arg1) == COMPLEX_EXPR
9506 || TREE_CODE (arg0) == COMPLEX_CST
9507 || TREE_CODE (arg1) == COMPLEX_CST))
9509 tree subtype = TREE_TYPE (TREE_TYPE (arg0));
9510 tree real0, imag0, real1, imag1;
9512 arg0 = save_expr (arg0);
9513 arg1 = save_expr (arg1);
9514 real0 = fold (build1 (REALPART_EXPR, subtype, arg0));
9515 imag0 = fold (build1 (IMAGPART_EXPR, subtype, arg0));
9516 real1 = fold (build1 (REALPART_EXPR, subtype, arg1));
9517 imag1 = fold (build1 (IMAGPART_EXPR, subtype, arg1));
9519 return fold (build2 ((code == EQ_EXPR ? TRUTH_ANDIF_EXPR
9522 fold (build2 (code, type, real0, real1)),
9523 fold (build2 (code, type, imag0, imag1))));
9526 /* Optimize comparisons of strlen vs zero to a compare of the
9527 first character of the string vs zero. To wit,
9528 strlen(ptr) == 0 => *ptr == 0
9529 strlen(ptr) != 0 => *ptr != 0
9530 Other cases should reduce to one of these two (or a constant)
9531 due to the return value of strlen being unsigned. */
9532 if ((code == EQ_EXPR || code == NE_EXPR)
9533 && integer_zerop (arg1)
9534 && TREE_CODE (arg0) == CALL_EXPR)
9536 tree fndecl = get_callee_fndecl (arg0);
9540 && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL
9541 && DECL_FUNCTION_CODE (fndecl) == BUILT_IN_STRLEN
9542 && (arglist = TREE_OPERAND (arg0, 1))
9543 && TREE_CODE (TREE_TYPE (TREE_VALUE (arglist))) == POINTER_TYPE
9544 && ! TREE_CHAIN (arglist))
9545 return fold (build2 (code, type,
9546 build1 (INDIRECT_REF, char_type_node,
9547 TREE_VALUE (arglist)),
9548 fold_convert (char_type_node,
9549 integer_zero_node)));
9552 /* We can fold X/C1 op C2 where C1 and C2 are integer constants
9553 into a single range test. */
9554 if ((TREE_CODE (arg0) == TRUNC_DIV_EXPR
9555 || TREE_CODE (arg0) == EXACT_DIV_EXPR)
9556 && TREE_CODE (arg1) == INTEGER_CST
9557 && TREE_CODE (TREE_OPERAND (arg0, 1)) == INTEGER_CST
9558 && !integer_zerop (TREE_OPERAND (arg0, 1))
9559 && !TREE_OVERFLOW (TREE_OPERAND (arg0, 1))
9560 && !TREE_OVERFLOW (arg1))
9562 t1 = fold_div_compare (code, type, arg0, arg1);
9563 if (t1 != NULL_TREE)
9567 if ((code == EQ_EXPR || code == NE_EXPR)
9568 && !TREE_SIDE_EFFECTS (arg0)
9569 && integer_zerop (arg1)
9570 && tree_expr_nonzero_p (arg0))
9571 return constant_boolean_node (code==NE_EXPR, type);
9573 t1 = fold_relational_const (code, type, arg0, arg1);
9574 return t1 == NULL_TREE ? t : t1;
9576 case UNORDERED_EXPR:
9584 if (TREE_CODE (arg0) == REAL_CST && TREE_CODE (arg1) == REAL_CST)
9586 t1 = fold_relational_const (code, type, arg0, arg1);
9587 if (t1 != NULL_TREE)
9591 /* If the first operand is NaN, the result is constant. */
9592 if (TREE_CODE (arg0) == REAL_CST
9593 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg0))
9594 && (code != LTGT_EXPR || ! flag_trapping_math))
9596 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
9599 return omit_one_operand (type, t1, arg1);
9602 /* If the second operand is NaN, the result is constant. */
9603 if (TREE_CODE (arg1) == REAL_CST
9604 && REAL_VALUE_ISNAN (TREE_REAL_CST (arg1))
9605 && (code != LTGT_EXPR || ! flag_trapping_math))
9607 t1 = (code == ORDERED_EXPR || code == LTGT_EXPR)
9610 return omit_one_operand (type, t1, arg0);
9613 /* Simplify unordered comparison of something with itself. */
9614 if ((code == UNLE_EXPR || code == UNGE_EXPR || code == UNEQ_EXPR)
9615 && operand_equal_p (arg0, arg1, 0))
9616 return constant_boolean_node (1, type);
9618 if (code == LTGT_EXPR
9619 && !flag_trapping_math
9620 && operand_equal_p (arg0, arg1, 0))
9621 return constant_boolean_node (0, type);
9623 /* Fold (double)float1 CMP (double)float2 into float1 CMP float2. */
9625 tree targ0 = strip_float_extensions (arg0);
9626 tree targ1 = strip_float_extensions (arg1);
9627 tree newtype = TREE_TYPE (targ0);
9629 if (TYPE_PRECISION (TREE_TYPE (targ1)) > TYPE_PRECISION (newtype))
9630 newtype = TREE_TYPE (targ1);
9632 if (TYPE_PRECISION (newtype) < TYPE_PRECISION (TREE_TYPE (arg0)))
9633 return fold (build2 (code, type, fold_convert (newtype, targ0),
9634 fold_convert (newtype, targ1)));
9640 /* When pedantic, a compound expression can be neither an lvalue
9641 nor an integer constant expression. */
9642 if (TREE_SIDE_EFFECTS (arg0) || TREE_CONSTANT (arg1))
9644 /* Don't let (0, 0) be null pointer constant. */
9645 tem = integer_zerop (arg1) ? build1 (NOP_EXPR, type, arg1)
9646 : fold_convert (type, arg1);
9647 return pedantic_non_lvalue (tem);
9651 return build_complex (type, arg0, arg1);
9656 } /* switch (code) */
9659 /* Fold a ternary expression EXPR. Return the folded expression if
9660 folding is successful. Otherwise, return the original
9664 fold_ternary (tree expr)
9666 const tree t = expr;
9667 const tree type = TREE_TYPE (expr);
9670 tree arg0 = NULL_TREE, arg1 = NULL_TREE;
9671 enum tree_code code = TREE_CODE (t);
9672 enum tree_code_class kind = TREE_CODE_CLASS (code);
9674 gcc_assert (IS_EXPR_CODE_CLASS (kind)
9675 && TREE_CODE_LENGTH (code) == 3);
9677 op0 = TREE_OPERAND (t, 0);
9678 op1 = TREE_OPERAND (t, 1);
9679 op2 = TREE_OPERAND (t, 2);
9681 /* Strip any conversions that don't change the mode. This is safe
9682 for every expression, except for a comparison expression because
9683 its signedness is derived from its operands. So, in the latter
9684 case, only strip conversions that don't change the signedness.
9686 Note that this is done as an internal manipulation within the
9687 constant folder, in order to find the simplest representation of
9688 the arguments so that their form can be studied. In any cases,
9689 the appropriate type conversions should be put back in the tree
9690 that will get out of the constant folder. */
9706 if (TREE_CODE (arg0) == CONSTRUCTOR
9707 && ! type_contains_placeholder_p (TREE_TYPE (arg0)))
9709 tree m = purpose_member (arg1, CONSTRUCTOR_ELTS (arg0));
9711 return TREE_VALUE (m);
9716 /* Pedantic ANSI C says that a conditional expression is never an lvalue,
9717 so all simple results must be passed through pedantic_non_lvalue. */
9718 if (TREE_CODE (arg0) == INTEGER_CST)
9720 tem = integer_zerop (arg0) ? op2 : op1;
9721 /* Only optimize constant conditions when the selected branch
9722 has the same type as the COND_EXPR. This avoids optimizing
9723 away "c ? x : throw", where the throw has a void type. */
9724 if (! VOID_TYPE_P (TREE_TYPE (tem))
9725 || VOID_TYPE_P (type))
9726 return pedantic_non_lvalue (tem);
9729 if (operand_equal_p (arg1, op2, 0))
9730 return pedantic_omit_one_operand (type, arg1, arg0);
9732 /* If we have A op B ? A : C, we may be able to convert this to a
9733 simpler expression, depending on the operation and the values
9734 of B and C. Signed zeros prevent all of these transformations,
9735 for reasons given above each one.
9737 Also try swapping the arguments and inverting the conditional. */
9738 if (COMPARISON_CLASS_P (arg0)
9739 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
9740 arg1, TREE_OPERAND (arg0, 1))
9741 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (arg1))))
9743 tem = fold_cond_expr_with_comparison (type, arg0, op1, op2);
9748 if (COMPARISON_CLASS_P (arg0)
9749 && operand_equal_for_comparison_p (TREE_OPERAND (arg0, 0),
9751 TREE_OPERAND (arg0, 1))
9752 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op2))))
9754 tem = invert_truthvalue (arg0);
9755 if (COMPARISON_CLASS_P (tem))
9757 tem = fold_cond_expr_with_comparison (type, tem, op2, op1);
9763 /* If the second operand is simpler than the third, swap them
9764 since that produces better jump optimization results. */
9765 if (tree_swap_operands_p (op1, op2, false))
9767 /* See if this can be inverted. If it can't, possibly because
9768 it was a floating-point inequality comparison, don't do
9770 tem = invert_truthvalue (arg0);
9772 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
9773 return fold (build3 (code, type, tem, op2, op1));
9776 /* Convert A ? 1 : 0 to simply A. */
9777 if (integer_onep (op1)
9778 && integer_zerop (op2)
9779 /* If we try to convert OP0 to our type, the
9780 call to fold will try to move the conversion inside
9781 a COND, which will recurse. In that case, the COND_EXPR
9782 is probably the best choice, so leave it alone. */
9783 && type == TREE_TYPE (arg0))
9784 return pedantic_non_lvalue (arg0);
9786 /* Convert A ? 0 : 1 to !A. This prefers the use of NOT_EXPR
9787 over COND_EXPR in cases such as floating point comparisons. */
9788 if (integer_zerop (op1)
9789 && integer_onep (op2)
9790 && truth_value_p (TREE_CODE (arg0)))
9791 return pedantic_non_lvalue (fold_convert (type,
9792 invert_truthvalue (arg0)));
9794 /* A < 0 ? <sign bit of A> : 0 is simply (A & <sign bit of A>). */
9795 if (TREE_CODE (arg0) == LT_EXPR
9796 && integer_zerop (TREE_OPERAND (arg0, 1))
9797 && integer_zerop (op2)
9798 && (tem = sign_bit_p (TREE_OPERAND (arg0, 0), arg1)))
9799 return fold_convert (type, fold (build2 (BIT_AND_EXPR,
9800 TREE_TYPE (tem), tem, arg1)));
9802 /* (A >> N) & 1 ? (1 << N) : 0 is simply A & (1 << N). A & 1 was
9803 already handled above. */
9804 if (TREE_CODE (arg0) == BIT_AND_EXPR
9805 && integer_onep (TREE_OPERAND (arg0, 1))
9806 && integer_zerop (op2)
9807 && integer_pow2p (arg1))
9809 tree tem = TREE_OPERAND (arg0, 0);
9811 if (TREE_CODE (tem) == RSHIFT_EXPR
9812 && TREE_CODE (TREE_OPERAND (tem, 1)) == INTEGER_CST
9813 && (unsigned HOST_WIDE_INT) tree_log2 (arg1) ==
9814 TREE_INT_CST_LOW (TREE_OPERAND (tem, 1)))
9815 return fold (build2 (BIT_AND_EXPR, type,
9816 TREE_OPERAND (tem, 0), arg1));
9819 /* A & N ? N : 0 is simply A & N if N is a power of two. This
9820 is probably obsolete because the first operand should be a
9821 truth value (that's why we have the two cases above), but let's
9822 leave it in until we can confirm this for all front-ends. */
9823 if (integer_zerop (op2)
9824 && TREE_CODE (arg0) == NE_EXPR
9825 && integer_zerop (TREE_OPERAND (arg0, 1))
9826 && integer_pow2p (arg1)
9827 && TREE_CODE (TREE_OPERAND (arg0, 0)) == BIT_AND_EXPR
9828 && operand_equal_p (TREE_OPERAND (TREE_OPERAND (arg0, 0), 1),
9829 arg1, OEP_ONLY_CONST))
9830 return pedantic_non_lvalue (fold_convert (type,
9831 TREE_OPERAND (arg0, 0)));
9833 /* Convert A ? B : 0 into A && B if A and B are truth values. */
9834 if (integer_zerop (op2)
9835 && truth_value_p (TREE_CODE (arg0))
9836 && truth_value_p (TREE_CODE (arg1)))
9837 return fold (build2 (TRUTH_ANDIF_EXPR, type, arg0, arg1));
9839 /* Convert A ? B : 1 into !A || B if A and B are truth values. */
9840 if (integer_onep (op2)
9841 && truth_value_p (TREE_CODE (arg0))
9842 && truth_value_p (TREE_CODE (arg1)))
9844 /* Only perform transformation if ARG0 is easily inverted. */
9845 tem = invert_truthvalue (arg0);
9846 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
9847 return fold (build2 (TRUTH_ORIF_EXPR, type, tem, arg1));
9850 /* Convert A ? 0 : B into !A && B if A and B are truth values. */
9851 if (integer_zerop (arg1)
9852 && truth_value_p (TREE_CODE (arg0))
9853 && truth_value_p (TREE_CODE (op2)))
9855 /* Only perform transformation if ARG0 is easily inverted. */
9856 tem = invert_truthvalue (arg0);
9857 if (TREE_CODE (tem) != TRUTH_NOT_EXPR)
9858 return fold (build2 (TRUTH_ANDIF_EXPR, type, tem, op2));
9861 /* Convert A ? 1 : B into A || B if A and B are truth values. */
9862 if (integer_onep (arg1)
9863 && truth_value_p (TREE_CODE (arg0))
9864 && truth_value_p (TREE_CODE (op2)))
9865 return fold (build2 (TRUTH_ORIF_EXPR, type, arg0, op2));
9870 /* Check for a built-in function. */
9871 if (TREE_CODE (op0) == ADDR_EXPR
9872 && TREE_CODE (TREE_OPERAND (op0, 0)) == FUNCTION_DECL
9873 && DECL_BUILT_IN (TREE_OPERAND (op0, 0)))
9875 tree tmp = fold_builtin (t, false);
9883 } /* switch (code) */
9886 /* Perform constant folding and related simplification of EXPR.
9887 The related simplifications include x*1 => x, x*0 => 0, etc.,
9888 and application of the associative law.
9889 NOP_EXPR conversions may be removed freely (as long as we
9890 are careful not to change the type of the overall expression).
9891 We cannot simplify through a CONVERT_EXPR, FIX_EXPR or FLOAT_EXPR,
9892 but we can constant-fold them if they have constant operands. */
9894 #ifdef ENABLE_FOLD_CHECKING
9895 # define fold(x) fold_1 (x)
9896 static tree fold_1 (tree);
9902 const tree t = expr;
9903 enum tree_code code = TREE_CODE (t);
9904 enum tree_code_class kind = TREE_CODE_CLASS (code);
9906 /* Return right away if a constant. */
9907 if (kind == tcc_constant)
9910 if (IS_EXPR_CODE_CLASS (kind))
9912 switch (TREE_CODE_LENGTH (code))
9915 return fold_unary (expr);
9917 return fold_binary (expr);
9919 return fold_ternary (expr);
9928 return fold (DECL_INITIAL (t));
9932 } /* switch (code) */
9935 #ifdef ENABLE_FOLD_CHECKING
9938 static void fold_checksum_tree (tree, struct md5_ctx *, htab_t);
9939 static void fold_check_failed (tree, tree);
9940 void print_fold_checksum (tree);
9942 /* When --enable-checking=fold, compute a digest of expr before
9943 and after actual fold call to see if fold did not accidentally
9944 change original expr. */
9951 unsigned char checksum_before[16], checksum_after[16];
9954 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
9955 md5_init_ctx (&ctx);
9956 fold_checksum_tree (expr, &ctx, ht);
9957 md5_finish_ctx (&ctx, checksum_before);
9960 ret = fold_1 (expr);
9962 md5_init_ctx (&ctx);
9963 fold_checksum_tree (expr, &ctx, ht);
9964 md5_finish_ctx (&ctx, checksum_after);
9967 if (memcmp (checksum_before, checksum_after, 16))
9968 fold_check_failed (expr, ret);
9974 print_fold_checksum (tree expr)
9977 unsigned char checksum[16], cnt;
9980 ht = htab_create (32, htab_hash_pointer, htab_eq_pointer, NULL);
9981 md5_init_ctx (&ctx);
9982 fold_checksum_tree (expr, &ctx, ht);
9983 md5_finish_ctx (&ctx, checksum);
9985 for (cnt = 0; cnt < 16; ++cnt)
9986 fprintf (stderr, "%02x", checksum[cnt]);
9987 putc ('\n', stderr);
9991 fold_check_failed (tree expr ATTRIBUTE_UNUSED, tree ret ATTRIBUTE_UNUSED)
9993 internal_error ("fold check: original tree changed by fold");
9997 fold_checksum_tree (tree expr, struct md5_ctx *ctx, htab_t ht)
10000 enum tree_code code;
10001 char buf[sizeof (struct tree_decl)];
10004 gcc_assert ((sizeof (struct tree_exp) + 5 * sizeof (tree)
10005 <= sizeof (struct tree_decl))
10006 && sizeof (struct tree_type) <= sizeof (struct tree_decl));
10009 slot = htab_find_slot (ht, expr, INSERT);
10013 code = TREE_CODE (expr);
10014 if (TREE_CODE_CLASS (code) == tcc_declaration
10015 && DECL_ASSEMBLER_NAME_SET_P (expr))
10017 /* Allow DECL_ASSEMBLER_NAME to be modified. */
10018 memcpy (buf, expr, tree_size (expr));
10020 SET_DECL_ASSEMBLER_NAME (expr, NULL);
10022 else if (TREE_CODE_CLASS (code) == tcc_type
10023 && (TYPE_POINTER_TO (expr) || TYPE_REFERENCE_TO (expr)
10024 || TYPE_CACHED_VALUES_P (expr)))
10026 /* Allow these fields to be modified. */
10027 memcpy (buf, expr, tree_size (expr));
10029 TYPE_POINTER_TO (expr) = NULL;
10030 TYPE_REFERENCE_TO (expr) = NULL;
10031 TYPE_CACHED_VALUES_P (expr) = 0;
10032 TYPE_CACHED_VALUES (expr) = NULL;
10034 md5_process_bytes (expr, tree_size (expr), ctx);
10035 fold_checksum_tree (TREE_TYPE (expr), ctx, ht);
10036 if (TREE_CODE_CLASS (code) != tcc_type
10037 && TREE_CODE_CLASS (code) != tcc_declaration)
10038 fold_checksum_tree (TREE_CHAIN (expr), ctx, ht);
10039 switch (TREE_CODE_CLASS (code))
10045 md5_process_bytes (TREE_STRING_POINTER (expr),
10046 TREE_STRING_LENGTH (expr), ctx);
10049 fold_checksum_tree (TREE_REALPART (expr), ctx, ht);
10050 fold_checksum_tree (TREE_IMAGPART (expr), ctx, ht);
10053 fold_checksum_tree (TREE_VECTOR_CST_ELTS (expr), ctx, ht);
10059 case tcc_exceptional:
10063 fold_checksum_tree (TREE_PURPOSE (expr), ctx, ht);
10064 fold_checksum_tree (TREE_VALUE (expr), ctx, ht);
10067 for (i = 0; i < TREE_VEC_LENGTH (expr); ++i)
10068 fold_checksum_tree (TREE_VEC_ELT (expr, i), ctx, ht);
10074 case tcc_expression:
10075 case tcc_reference:
10076 case tcc_comparison:
10079 case tcc_statement:
10080 len = TREE_CODE_LENGTH (code);
10081 for (i = 0; i < len; ++i)
10082 fold_checksum_tree (TREE_OPERAND (expr, i), ctx, ht);
10084 case tcc_declaration:
10085 fold_checksum_tree (DECL_SIZE (expr), ctx, ht);
10086 fold_checksum_tree (DECL_SIZE_UNIT (expr), ctx, ht);
10087 fold_checksum_tree (DECL_NAME (expr), ctx, ht);
10088 fold_checksum_tree (DECL_CONTEXT (expr), ctx, ht);
10089 fold_checksum_tree (DECL_ARGUMENTS (expr), ctx, ht);
10090 fold_checksum_tree (DECL_RESULT_FLD (expr), ctx, ht);
10091 fold_checksum_tree (DECL_INITIAL (expr), ctx, ht);
10092 fold_checksum_tree (DECL_ABSTRACT_ORIGIN (expr), ctx, ht);
10093 fold_checksum_tree (DECL_SECTION_NAME (expr), ctx, ht);
10094 fold_checksum_tree (DECL_ATTRIBUTES (expr), ctx, ht);
10095 fold_checksum_tree (DECL_VINDEX (expr), ctx, ht);
10098 if (TREE_CODE (expr) == ENUMERAL_TYPE)
10099 fold_checksum_tree (TYPE_VALUES (expr), ctx, ht);
10100 fold_checksum_tree (TYPE_SIZE (expr), ctx, ht);
10101 fold_checksum_tree (TYPE_SIZE_UNIT (expr), ctx, ht);
10102 fold_checksum_tree (TYPE_ATTRIBUTES (expr), ctx, ht);
10103 fold_checksum_tree (TYPE_NAME (expr), ctx, ht);
10104 if (INTEGRAL_TYPE_P (expr)
10105 || SCALAR_FLOAT_TYPE_P (expr))
10107 fold_checksum_tree (TYPE_MIN_VALUE (expr), ctx, ht);
10108 fold_checksum_tree (TYPE_MAX_VALUE (expr), ctx, ht);
10110 fold_checksum_tree (TYPE_MAIN_VARIANT (expr), ctx, ht);
10111 if (TREE_CODE (expr) == RECORD_TYPE
10112 || TREE_CODE (expr) == UNION_TYPE
10113 || TREE_CODE (expr) == QUAL_UNION_TYPE)
10114 fold_checksum_tree (TYPE_BINFO (expr), ctx, ht);
10115 fold_checksum_tree (TYPE_CONTEXT (expr), ctx, ht);
10124 /* Perform constant folding and related simplification of initializer
10125 expression EXPR. This behaves identically to "fold" but ignores
10126 potential run-time traps and exceptions that fold must preserve. */
10129 fold_initializer (tree expr)
10131 int saved_signaling_nans = flag_signaling_nans;
10132 int saved_trapping_math = flag_trapping_math;
10133 int saved_rounding_math = flag_rounding_math;
10134 int saved_trapv = flag_trapv;
10137 flag_signaling_nans = 0;
10138 flag_trapping_math = 0;
10139 flag_rounding_math = 0;
10142 result = fold (expr);
10144 flag_signaling_nans = saved_signaling_nans;
10145 flag_trapping_math = saved_trapping_math;
10146 flag_rounding_math = saved_rounding_math;
10147 flag_trapv = saved_trapv;
10152 /* Determine if first argument is a multiple of second argument. Return 0 if
10153 it is not, or we cannot easily determined it to be.
10155 An example of the sort of thing we care about (at this point; this routine
10156 could surely be made more general, and expanded to do what the *_DIV_EXPR's
10157 fold cases do now) is discovering that
10159 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
10165 when we know that the two SAVE_EXPR (J * 8) nodes are the same node.
10167 This code also handles discovering that
10169 SAVE_EXPR (I) * SAVE_EXPR (J * 8)
10171 is a multiple of 8 so we don't have to worry about dealing with a
10172 possible remainder.
10174 Note that we *look* inside a SAVE_EXPR only to determine how it was
10175 calculated; it is not safe for fold to do much of anything else with the
10176 internals of a SAVE_EXPR, since it cannot know when it will be evaluated
10177 at run time. For example, the latter example above *cannot* be implemented
10178 as SAVE_EXPR (I) * J or any variant thereof, since the value of J at
10179 evaluation time of the original SAVE_EXPR is not necessarily the same at
10180 the time the new expression is evaluated. The only optimization of this
10181 sort that would be valid is changing
10183 SAVE_EXPR (I) * SAVE_EXPR (SAVE_EXPR (J) * 8)
10187 SAVE_EXPR (I) * SAVE_EXPR (J)
10189 (where the same SAVE_EXPR (J) is used in the original and the
10190 transformed version). */
10193 multiple_of_p (tree type, tree top, tree bottom)
10195 if (operand_equal_p (top, bottom, 0))
10198 if (TREE_CODE (type) != INTEGER_TYPE)
10201 switch (TREE_CODE (top))
10204 /* Bitwise and provides a power of two multiple. If the mask is
10205 a multiple of BOTTOM then TOP is a multiple of BOTTOM. */
10206 if (!integer_pow2p (bottom))
10211 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
10212 || multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
10216 return (multiple_of_p (type, TREE_OPERAND (top, 0), bottom)
10217 && multiple_of_p (type, TREE_OPERAND (top, 1), bottom));
10220 if (TREE_CODE (TREE_OPERAND (top, 1)) == INTEGER_CST)
10224 op1 = TREE_OPERAND (top, 1);
10225 /* const_binop may not detect overflow correctly,
10226 so check for it explicitly here. */
10227 if (TYPE_PRECISION (TREE_TYPE (size_one_node))
10228 > TREE_INT_CST_LOW (op1)
10229 && TREE_INT_CST_HIGH (op1) == 0
10230 && 0 != (t1 = fold_convert (type,
10231 const_binop (LSHIFT_EXPR,
10234 && ! TREE_OVERFLOW (t1))
10235 return multiple_of_p (type, t1, bottom);
10240 /* Can't handle conversions from non-integral or wider integral type. */
10241 if ((TREE_CODE (TREE_TYPE (TREE_OPERAND (top, 0))) != INTEGER_TYPE)
10242 || (TYPE_PRECISION (type)
10243 < TYPE_PRECISION (TREE_TYPE (TREE_OPERAND (top, 0)))))
10246 /* .. fall through ... */
10249 return multiple_of_p (type, TREE_OPERAND (top, 0), bottom);
10252 if (TREE_CODE (bottom) != INTEGER_CST
10253 || (TYPE_UNSIGNED (type)
10254 && (tree_int_cst_sgn (top) < 0
10255 || tree_int_cst_sgn (bottom) < 0)))
10257 return integer_zerop (const_binop (TRUNC_MOD_EXPR,
10265 /* Return true if `t' is known to be non-negative. */
10268 tree_expr_nonnegative_p (tree t)
10270 switch (TREE_CODE (t))
10276 return tree_int_cst_sgn (t) >= 0;
10279 return ! REAL_VALUE_NEGATIVE (TREE_REAL_CST (t));
10282 if (FLOAT_TYPE_P (TREE_TYPE (t)))
10283 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
10284 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
10286 /* zero_extend(x) + zero_extend(y) is non-negative if x and y are
10287 both unsigned and at least 2 bits shorter than the result. */
10288 if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
10289 && TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
10290 && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR)
10292 tree inner1 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
10293 tree inner2 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0));
10294 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
10295 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
10297 unsigned int prec = MAX (TYPE_PRECISION (inner1),
10298 TYPE_PRECISION (inner2)) + 1;
10299 return prec < TYPE_PRECISION (TREE_TYPE (t));
10305 if (FLOAT_TYPE_P (TREE_TYPE (t)))
10307 /* x * x for floating point x is always non-negative. */
10308 if (operand_equal_p (TREE_OPERAND (t, 0), TREE_OPERAND (t, 1), 0))
10310 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
10311 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
10314 /* zero_extend(x) * zero_extend(y) is non-negative if x and y are
10315 both unsigned and their total bits is shorter than the result. */
10316 if (TREE_CODE (TREE_TYPE (t)) == INTEGER_TYPE
10317 && TREE_CODE (TREE_OPERAND (t, 0)) == NOP_EXPR
10318 && TREE_CODE (TREE_OPERAND (t, 1)) == NOP_EXPR)
10320 tree inner1 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 0), 0));
10321 tree inner2 = TREE_TYPE (TREE_OPERAND (TREE_OPERAND (t, 1), 0));
10322 if (TREE_CODE (inner1) == INTEGER_TYPE && TYPE_UNSIGNED (inner1)
10323 && TREE_CODE (inner2) == INTEGER_TYPE && TYPE_UNSIGNED (inner2))
10324 return TYPE_PRECISION (inner1) + TYPE_PRECISION (inner2)
10325 < TYPE_PRECISION (TREE_TYPE (t));
10329 case TRUNC_DIV_EXPR:
10330 case CEIL_DIV_EXPR:
10331 case FLOOR_DIV_EXPR:
10332 case ROUND_DIV_EXPR:
10333 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
10334 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
10336 case TRUNC_MOD_EXPR:
10337 case CEIL_MOD_EXPR:
10338 case FLOOR_MOD_EXPR:
10339 case ROUND_MOD_EXPR:
10340 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
10343 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
10344 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
10347 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1))
10348 || tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
10351 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
10352 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
10356 tree inner_type = TREE_TYPE (TREE_OPERAND (t, 0));
10357 tree outer_type = TREE_TYPE (t);
10359 if (TREE_CODE (outer_type) == REAL_TYPE)
10361 if (TREE_CODE (inner_type) == REAL_TYPE)
10362 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
10363 if (TREE_CODE (inner_type) == INTEGER_TYPE)
10365 if (TYPE_UNSIGNED (inner_type))
10367 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
10370 else if (TREE_CODE (outer_type) == INTEGER_TYPE)
10372 if (TREE_CODE (inner_type) == REAL_TYPE)
10373 return tree_expr_nonnegative_p (TREE_OPERAND (t,0));
10374 if (TREE_CODE (inner_type) == INTEGER_TYPE)
10375 return TYPE_PRECISION (inner_type) < TYPE_PRECISION (outer_type)
10376 && TYPE_UNSIGNED (inner_type);
10382 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1))
10383 && tree_expr_nonnegative_p (TREE_OPERAND (t, 2));
10384 case COMPOUND_EXPR:
10385 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
10387 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
10388 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
10390 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
10391 || tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
10393 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
10395 return tree_expr_nonnegative_p (expr_last (TREE_OPERAND (t, 1)));
10397 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
10398 case NON_LVALUE_EXPR:
10399 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
10401 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
10405 tree temp = TARGET_EXPR_SLOT (t);
10406 t = TARGET_EXPR_INITIAL (t);
10408 /* If the initializer is non-void, then it's a normal expression
10409 that will be assigned to the slot. */
10410 if (!VOID_TYPE_P (t))
10411 return tree_expr_nonnegative_p (t);
10413 /* Otherwise, the initializer sets the slot in some way. One common
10414 way is an assignment statement at the end of the initializer. */
10417 if (TREE_CODE (t) == BIND_EXPR)
10418 t = expr_last (BIND_EXPR_BODY (t));
10419 else if (TREE_CODE (t) == TRY_FINALLY_EXPR
10420 || TREE_CODE (t) == TRY_CATCH_EXPR)
10421 t = expr_last (TREE_OPERAND (t, 0));
10422 else if (TREE_CODE (t) == STATEMENT_LIST)
10427 if (TREE_CODE (t) == MODIFY_EXPR
10428 && TREE_OPERAND (t, 0) == temp)
10429 return tree_expr_nonnegative_p (TREE_OPERAND (t, 1));
10436 tree fndecl = get_callee_fndecl (t);
10437 tree arglist = TREE_OPERAND (t, 1);
10438 if (fndecl && DECL_BUILT_IN_CLASS (fndecl) == BUILT_IN_NORMAL)
10439 switch (DECL_FUNCTION_CODE (fndecl))
10441 #define CASE_BUILTIN_F(BUILT_IN_FN) \
10442 case BUILT_IN_FN: case BUILT_IN_FN##F: case BUILT_IN_FN##L:
10443 #define CASE_BUILTIN_I(BUILT_IN_FN) \
10444 case BUILT_IN_FN: case BUILT_IN_FN##L: case BUILT_IN_FN##LL:
10446 CASE_BUILTIN_F (BUILT_IN_ACOS)
10447 CASE_BUILTIN_F (BUILT_IN_ACOSH)
10448 CASE_BUILTIN_F (BUILT_IN_CABS)
10449 CASE_BUILTIN_F (BUILT_IN_COSH)
10450 CASE_BUILTIN_F (BUILT_IN_ERFC)
10451 CASE_BUILTIN_F (BUILT_IN_EXP)
10452 CASE_BUILTIN_F (BUILT_IN_EXP10)
10453 CASE_BUILTIN_F (BUILT_IN_EXP2)
10454 CASE_BUILTIN_F (BUILT_IN_FABS)
10455 CASE_BUILTIN_F (BUILT_IN_FDIM)
10456 CASE_BUILTIN_F (BUILT_IN_FREXP)
10457 CASE_BUILTIN_F (BUILT_IN_HYPOT)
10458 CASE_BUILTIN_F (BUILT_IN_POW10)
10459 CASE_BUILTIN_I (BUILT_IN_FFS)
10460 CASE_BUILTIN_I (BUILT_IN_PARITY)
10461 CASE_BUILTIN_I (BUILT_IN_POPCOUNT)
10465 CASE_BUILTIN_F (BUILT_IN_SQRT)
10466 /* sqrt(-0.0) is -0.0. */
10467 if (!HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (t))))
10469 return tree_expr_nonnegative_p (TREE_VALUE (arglist));
10471 CASE_BUILTIN_F (BUILT_IN_ASINH)
10472 CASE_BUILTIN_F (BUILT_IN_ATAN)
10473 CASE_BUILTIN_F (BUILT_IN_ATANH)
10474 CASE_BUILTIN_F (BUILT_IN_CBRT)
10475 CASE_BUILTIN_F (BUILT_IN_CEIL)
10476 CASE_BUILTIN_F (BUILT_IN_ERF)
10477 CASE_BUILTIN_F (BUILT_IN_EXPM1)
10478 CASE_BUILTIN_F (BUILT_IN_FLOOR)
10479 CASE_BUILTIN_F (BUILT_IN_FMOD)
10480 CASE_BUILTIN_F (BUILT_IN_LDEXP)
10481 CASE_BUILTIN_F (BUILT_IN_LLRINT)
10482 CASE_BUILTIN_F (BUILT_IN_LLROUND)
10483 CASE_BUILTIN_F (BUILT_IN_LRINT)
10484 CASE_BUILTIN_F (BUILT_IN_LROUND)
10485 CASE_BUILTIN_F (BUILT_IN_MODF)
10486 CASE_BUILTIN_F (BUILT_IN_NEARBYINT)
10487 CASE_BUILTIN_F (BUILT_IN_POW)
10488 CASE_BUILTIN_F (BUILT_IN_RINT)
10489 CASE_BUILTIN_F (BUILT_IN_ROUND)
10490 CASE_BUILTIN_F (BUILT_IN_SIGNBIT)
10491 CASE_BUILTIN_F (BUILT_IN_SINH)
10492 CASE_BUILTIN_F (BUILT_IN_TANH)
10493 CASE_BUILTIN_F (BUILT_IN_TRUNC)
10494 /* True if the 1st argument is nonnegative. */
10495 return tree_expr_nonnegative_p (TREE_VALUE (arglist));
10497 CASE_BUILTIN_F (BUILT_IN_FMAX)
10498 /* True if the 1st OR 2nd arguments are nonnegative. */
10499 return tree_expr_nonnegative_p (TREE_VALUE (arglist))
10500 || tree_expr_nonnegative_p (TREE_VALUE (TREE_CHAIN (arglist)));
10502 CASE_BUILTIN_F (BUILT_IN_FMIN)
10503 /* True if the 1st AND 2nd arguments are nonnegative. */
10504 return tree_expr_nonnegative_p (TREE_VALUE (arglist))
10505 && tree_expr_nonnegative_p (TREE_VALUE (TREE_CHAIN (arglist)));
10507 CASE_BUILTIN_F (BUILT_IN_COPYSIGN)
10508 /* True if the 2nd argument is nonnegative. */
10509 return tree_expr_nonnegative_p (TREE_VALUE (TREE_CHAIN (arglist)));
10513 #undef CASE_BUILTIN_F
10514 #undef CASE_BUILTIN_I
10518 /* ... fall through ... */
10521 if (truth_value_p (TREE_CODE (t)))
10522 /* Truth values evaluate to 0 or 1, which is nonnegative. */
10526 /* We don't know sign of `t', so be conservative and return false. */
10530 /* Return true when T is an address and is known to be nonzero.
10531 For floating point we further ensure that T is not denormal.
10532 Similar logic is present in nonzero_address in rtlanal.h. */
10535 tree_expr_nonzero_p (tree t)
10537 tree type = TREE_TYPE (t);
10539 /* Doing something useful for floating point would need more work. */
10540 if (!INTEGRAL_TYPE_P (type) && !POINTER_TYPE_P (type))
10543 switch (TREE_CODE (t))
10546 if (!TYPE_UNSIGNED (type) && !flag_wrapv)
10547 return tree_expr_nonzero_p (TREE_OPERAND (t, 0));
10550 /* We used to test for !integer_zerop here. This does not work correctly
10551 if TREE_CONSTANT_OVERFLOW (t). */
10552 return (TREE_INT_CST_LOW (t) != 0
10553 || TREE_INT_CST_HIGH (t) != 0);
10556 if (!TYPE_UNSIGNED (type) && !flag_wrapv)
10558 /* With the presence of negative values it is hard
10559 to say something. */
10560 if (!tree_expr_nonnegative_p (TREE_OPERAND (t, 0))
10561 || !tree_expr_nonnegative_p (TREE_OPERAND (t, 1)))
10563 /* One of operands must be positive and the other non-negative. */
10564 return (tree_expr_nonzero_p (TREE_OPERAND (t, 0))
10565 || tree_expr_nonzero_p (TREE_OPERAND (t, 1)));
10570 if (!TYPE_UNSIGNED (type) && !flag_wrapv)
10572 return (tree_expr_nonzero_p (TREE_OPERAND (t, 0))
10573 && tree_expr_nonzero_p (TREE_OPERAND (t, 1)));
10579 tree inner_type = TREE_TYPE (TREE_OPERAND (t, 0));
10580 tree outer_type = TREE_TYPE (t);
10582 return (TYPE_PRECISION (inner_type) >= TYPE_PRECISION (outer_type)
10583 && tree_expr_nonzero_p (TREE_OPERAND (t, 0)));
10589 tree base = get_base_address (TREE_OPERAND (t, 0));
10594 /* Weak declarations may link to NULL. */
10596 return !DECL_WEAK (base);
10598 /* Constants are never weak. */
10599 if (CONSTANT_CLASS_P (base))
10606 return (tree_expr_nonzero_p (TREE_OPERAND (t, 1))
10607 && tree_expr_nonzero_p (TREE_OPERAND (t, 2)));
10610 return (tree_expr_nonzero_p (TREE_OPERAND (t, 0))
10611 && tree_expr_nonzero_p (TREE_OPERAND (t, 1)));
10614 if (tree_expr_nonzero_p (TREE_OPERAND (t, 0)))
10616 /* When both operands are nonzero, then MAX must be too. */
10617 if (tree_expr_nonzero_p (TREE_OPERAND (t, 1)))
10620 /* MAX where operand 0 is positive is positive. */
10621 return tree_expr_nonnegative_p (TREE_OPERAND (t, 0));
10623 /* MAX where operand 1 is positive is positive. */
10624 else if (tree_expr_nonzero_p (TREE_OPERAND (t, 1))
10625 && tree_expr_nonnegative_p (TREE_OPERAND (t, 1)))
10629 case COMPOUND_EXPR:
10632 return tree_expr_nonzero_p (TREE_OPERAND (t, 1));
10635 case NON_LVALUE_EXPR:
10636 return tree_expr_nonzero_p (TREE_OPERAND (t, 0));
10639 return tree_expr_nonzero_p (TREE_OPERAND (t, 1))
10640 || tree_expr_nonzero_p (TREE_OPERAND (t, 0));
10648 /* See if we are applying CODE, a relational to the highest or lowest
10649 possible integer of TYPE. If so, then the result is a compile
10653 fold_relational_hi_lo (enum tree_code *code_p, const tree type, tree *op0_p,
10658 enum tree_code code = *code_p;
10659 int width = GET_MODE_BITSIZE (TYPE_MODE (TREE_TYPE (op1)));
10661 if (TREE_CODE (op1) == INTEGER_CST
10662 && ! TREE_CONSTANT_OVERFLOW (op1)
10663 && width <= HOST_BITS_PER_WIDE_INT
10664 && (INTEGRAL_TYPE_P (TREE_TYPE (op1))
10665 || POINTER_TYPE_P (TREE_TYPE (op1))))
10667 unsigned HOST_WIDE_INT signed_max;
10668 unsigned HOST_WIDE_INT max, min;
10670 signed_max = ((unsigned HOST_WIDE_INT) 1 << (width - 1)) - 1;
10672 if (TYPE_UNSIGNED (TREE_TYPE (op1)))
10674 max = ((unsigned HOST_WIDE_INT) 2 << (width - 1)) - 1;
10680 min = ((unsigned HOST_WIDE_INT) -1 << (width - 1));
10683 if (TREE_INT_CST_HIGH (op1) == 0
10684 && TREE_INT_CST_LOW (op1) == max)
10688 return omit_one_operand (type, integer_zero_node, op0);
10694 return omit_one_operand (type, integer_one_node, op0);
10700 /* The GE_EXPR and LT_EXPR cases above are not normally
10701 reached because of previous transformations. */
10706 else if (TREE_INT_CST_HIGH (op1) == 0
10707 && TREE_INT_CST_LOW (op1) == max - 1)
10712 *op1_p = const_binop (PLUS_EXPR, op1, integer_one_node, 0);
10716 *op1_p = const_binop (PLUS_EXPR, op1, integer_one_node, 0);
10721 else if (TREE_INT_CST_HIGH (op1) == (min ? -1 : 0)
10722 && TREE_INT_CST_LOW (op1) == min)
10726 return omit_one_operand (type, integer_zero_node, op0);
10733 return omit_one_operand (type, integer_one_node, op0);
10742 else if (TREE_INT_CST_HIGH (op1) == (min ? -1 : 0)
10743 && TREE_INT_CST_LOW (op1) == min + 1)
10748 *op1_p = const_binop (MINUS_EXPR, op1, integer_one_node, 0);
10752 *op1_p = const_binop (MINUS_EXPR, op1, integer_one_node, 0);
10758 else if (TREE_INT_CST_HIGH (op1) == 0
10759 && TREE_INT_CST_LOW (op1) == signed_max
10760 && TYPE_UNSIGNED (TREE_TYPE (op1))
10761 /* signed_type does not work on pointer types. */
10762 && INTEGRAL_TYPE_P (TREE_TYPE (op1)))
10764 /* The following case also applies to X < signed_max+1
10765 and X >= signed_max+1 because previous transformations. */
10766 if (code == LE_EXPR || code == GT_EXPR)
10768 tree st0, st1, exp, retval;
10769 st0 = lang_hooks.types.signed_type (TREE_TYPE (op0));
10770 st1 = lang_hooks.types.signed_type (TREE_TYPE (op1));
10772 exp = build2 (code == LE_EXPR ? GE_EXPR: LT_EXPR,
10774 fold_convert (st0, op0),
10775 fold_convert (st1, integer_zero_node));
10777 retval = fold_binary_to_constant (TREE_CODE (exp),
10779 TREE_OPERAND (exp, 0),
10780 TREE_OPERAND (exp, 1));
10782 /* If we are in gimple form, then returning EXP would create
10783 non-gimple expressions. Clearing it is safe and insures
10784 we do not allow a non-gimple expression to escape. */
10785 if (in_gimple_form)
10788 return (retval ? retval : exp);
10797 /* Given the components of a binary expression CODE, TYPE, OP0 and OP1,
10798 attempt to fold the expression to a constant without modifying TYPE,
10801 If the expression could be simplified to a constant, then return
10802 the constant. If the expression would not be simplified to a
10803 constant, then return NULL_TREE.
10805 Note this is primarily designed to be called after gimplification
10806 of the tree structures and when at least one operand is a constant.
10807 As a result of those simplifying assumptions this routine is far
10808 simpler than the generic fold routine. */
10811 fold_binary_to_constant (enum tree_code code, tree type, tree op0, tree op1)
10818 /* If this is a commutative operation, and ARG0 is a constant, move it
10819 to ARG1 to reduce the number of tests below. */
10820 if (commutative_tree_code (code)
10821 && (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST))
10828 /* If either operand is a complex type, extract its real component. */
10829 if (TREE_CODE (op0) == COMPLEX_CST)
10830 subop0 = TREE_REALPART (op0);
10834 if (TREE_CODE (op1) == COMPLEX_CST)
10835 subop1 = TREE_REALPART (op1);
10839 /* Note if either argument is not a real or integer constant.
10840 With a few exceptions, simplification is limited to cases
10841 where both arguments are constants. */
10842 if ((TREE_CODE (subop0) != INTEGER_CST
10843 && TREE_CODE (subop0) != REAL_CST)
10844 || (TREE_CODE (subop1) != INTEGER_CST
10845 && TREE_CODE (subop1) != REAL_CST))
10851 /* (plus (address) (const_int)) is a constant. */
10852 if (TREE_CODE (op0) == PLUS_EXPR
10853 && TREE_CODE (op1) == INTEGER_CST
10854 && (TREE_CODE (TREE_OPERAND (op0, 0)) == ADDR_EXPR
10855 || (TREE_CODE (TREE_OPERAND (op0, 0)) == NOP_EXPR
10856 && (TREE_CODE (TREE_OPERAND (TREE_OPERAND (op0, 0), 0))
10858 && TREE_CODE (TREE_OPERAND (op0, 1)) == INTEGER_CST)
10860 return build2 (PLUS_EXPR, type, TREE_OPERAND (op0, 0),
10861 const_binop (PLUS_EXPR, op1,
10862 TREE_OPERAND (op0, 1), 0));
10870 /* Both arguments are constants. Simplify. */
10871 tem = const_binop (code, op0, op1, 0);
10872 if (tem != NULL_TREE)
10874 /* The return value should always have the same type as
10875 the original expression. */
10876 if (TREE_TYPE (tem) != type)
10877 tem = fold_convert (type, tem);
10884 /* Fold &x - &x. This can happen from &x.foo - &x.
10885 This is unsafe for certain floats even in non-IEEE formats.
10886 In IEEE, it is unsafe because it does wrong for NaNs.
10887 Also note that operand_equal_p is always false if an
10888 operand is volatile. */
10889 if (! FLOAT_TYPE_P (type) && operand_equal_p (op0, op1, 0))
10890 return fold_convert (type, integer_zero_node);
10896 /* Special case multiplication or bitwise AND where one argument
10898 if (! FLOAT_TYPE_P (type) && integer_zerop (op1))
10899 return omit_one_operand (type, op1, op0);
10901 if (!HONOR_NANS (TYPE_MODE (TREE_TYPE (op0)))
10902 && !HONOR_SIGNED_ZEROS (TYPE_MODE (TREE_TYPE (op0)))
10903 && real_zerop (op1))
10904 return omit_one_operand (type, op1, op0);
10909 /* Special case when we know the result will be all ones. */
10910 if (integer_all_onesp (op1))
10911 return omit_one_operand (type, op1, op0);
10915 case TRUNC_DIV_EXPR:
10916 case ROUND_DIV_EXPR:
10917 case FLOOR_DIV_EXPR:
10918 case CEIL_DIV_EXPR:
10919 case EXACT_DIV_EXPR:
10920 case TRUNC_MOD_EXPR:
10921 case ROUND_MOD_EXPR:
10922 case FLOOR_MOD_EXPR:
10923 case CEIL_MOD_EXPR:
10925 /* Division by zero is undefined. */
10926 if (integer_zerop (op1))
10929 if (TREE_CODE (op1) == REAL_CST
10930 && !MODE_HAS_INFINITIES (TYPE_MODE (TREE_TYPE (op1)))
10931 && real_zerop (op1))
10937 if (INTEGRAL_TYPE_P (type)
10938 && operand_equal_p (op1, TYPE_MIN_VALUE (type), OEP_ONLY_CONST))
10939 return omit_one_operand (type, op1, op0);
10944 if (INTEGRAL_TYPE_P (type)
10945 && TYPE_MAX_VALUE (type)
10946 && operand_equal_p (op1, TYPE_MAX_VALUE (type), OEP_ONLY_CONST))
10947 return omit_one_operand (type, op1, op0);
10952 /* Optimize -1 >> x for arithmetic right shifts. */
10953 if (integer_all_onesp (op0) && ! TYPE_UNSIGNED (type))
10954 return omit_one_operand (type, op0, op1);
10955 /* ... fall through ... */
10958 if (integer_zerop (op0))
10959 return omit_one_operand (type, op0, op1);
10961 /* Since negative shift count is not well-defined, don't
10962 try to compute it in the compiler. */
10963 if (TREE_CODE (op1) == INTEGER_CST && tree_int_cst_sgn (op1) < 0)
10970 /* -1 rotated either direction by any amount is still -1. */
10971 if (integer_all_onesp (op0))
10972 return omit_one_operand (type, op0, op1);
10974 /* 0 rotated either direction by any amount is still zero. */
10975 if (integer_zerop (op0))
10976 return omit_one_operand (type, op0, op1);
10982 return build_complex (type, op0, op1);
10991 /* If one arg is a real or integer constant, put it last. */
10992 if ((TREE_CODE (op0) == INTEGER_CST
10993 && TREE_CODE (op1) != INTEGER_CST)
10994 || (TREE_CODE (op0) == REAL_CST
10995 && TREE_CODE (op0) != REAL_CST))
11002 code = swap_tree_comparison (code);
11005 /* Change X >= C to X > (C - 1) and X < C to X <= (C - 1) if C > 0.
11006 This transformation affects the cases which are handled in later
11007 optimizations involving comparisons with non-negative constants. */
11008 if (TREE_CODE (op1) == INTEGER_CST
11009 && TREE_CODE (op0) != INTEGER_CST
11010 && tree_int_cst_sgn (op1) > 0)
11016 op1 = const_binop (MINUS_EXPR, op1, integer_one_node, 0);
11021 op1 = const_binop (MINUS_EXPR, op1, integer_one_node, 0);
11029 tem = fold_relational_hi_lo (&code, type, &op0, &op1);
11033 /* Fall through. */
11036 case UNORDERED_EXPR:
11046 return fold_relational_const (code, type, op0, op1);
11049 /* This could probably be handled. */
11052 case TRUTH_AND_EXPR:
11053 /* If second arg is constant zero, result is zero, but first arg
11054 must be evaluated. */
11055 if (integer_zerop (op1))
11056 return omit_one_operand (type, op1, op0);
11057 /* Likewise for first arg, but note that only the TRUTH_AND_EXPR
11058 case will be handled here. */
11059 if (integer_zerop (op0))
11060 return omit_one_operand (type, op0, op1);
11061 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
11062 return constant_boolean_node (true, type);
11065 case TRUTH_OR_EXPR:
11066 /* If second arg is constant true, result is true, but we must
11067 evaluate first arg. */
11068 if (TREE_CODE (op1) == INTEGER_CST && ! integer_zerop (op1))
11069 return omit_one_operand (type, op1, op0);
11070 /* Likewise for first arg, but note this only occurs here for
11072 if (TREE_CODE (op0) == INTEGER_CST && ! integer_zerop (op0))
11073 return omit_one_operand (type, op0, op1);
11074 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
11075 return constant_boolean_node (false, type);
11078 case TRUTH_XOR_EXPR:
11079 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
11081 int x = ! integer_zerop (op0) ^ ! integer_zerop (op1);
11082 return constant_boolean_node (x, type);
11091 /* Given the components of a unary expression CODE, TYPE and OP0,
11092 attempt to fold the expression to a constant without modifying
11095 If the expression could be simplified to a constant, then return
11096 the constant. If the expression would not be simplified to a
11097 constant, then return NULL_TREE.
11099 Note this is primarily designed to be called after gimplification
11100 of the tree structures and when op0 is a constant. As a result
11101 of those simplifying assumptions this routine is far simpler than
11102 the generic fold routine. */
11105 fold_unary_to_constant (enum tree_code code, tree type, tree op0)
11107 /* Make sure we have a suitable constant argument. */
11108 if (code == NOP_EXPR || code == FLOAT_EXPR || code == CONVERT_EXPR)
11112 if (TREE_CODE (op0) == COMPLEX_CST)
11113 subop = TREE_REALPART (op0);
11117 if (TREE_CODE (subop) != INTEGER_CST && TREE_CODE (subop) != REAL_CST)
11126 case FIX_TRUNC_EXPR:
11127 case FIX_FLOOR_EXPR:
11128 case FIX_CEIL_EXPR:
11129 return fold_convert_const (code, type, op0);
11132 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST)
11133 return fold_negate_const (op0, type);
11138 if (TREE_CODE (op0) == INTEGER_CST || TREE_CODE (op0) == REAL_CST)
11139 return fold_abs_const (op0, type);
11144 if (TREE_CODE (op0) == INTEGER_CST)
11145 return fold_not_const (op0, type);
11149 case REALPART_EXPR:
11150 if (TREE_CODE (op0) == COMPLEX_CST)
11151 return TREE_REALPART (op0);
11155 case IMAGPART_EXPR:
11156 if (TREE_CODE (op0) == COMPLEX_CST)
11157 return TREE_IMAGPART (op0);
11162 if (TREE_CODE (op0) == COMPLEX_CST
11163 && TREE_CODE (TREE_TYPE (op0)) == COMPLEX_TYPE)
11164 return build_complex (type, TREE_REALPART (op0),
11165 negate_expr (TREE_IMAGPART (op0)));
11173 /* If EXP represents referencing an element in a constant string
11174 (either via pointer arithmetic or array indexing), return the
11175 tree representing the value accessed, otherwise return NULL. */
11178 fold_read_from_constant_string (tree exp)
11180 if (TREE_CODE (exp) == INDIRECT_REF || TREE_CODE (exp) == ARRAY_REF)
11182 tree exp1 = TREE_OPERAND (exp, 0);
11186 if (TREE_CODE (exp) == INDIRECT_REF)
11187 string = string_constant (exp1, &index);
11190 tree low_bound = array_ref_low_bound (exp);
11191 index = fold_convert (sizetype, TREE_OPERAND (exp, 1));
11193 /* Optimize the special-case of a zero lower bound.
11195 We convert the low_bound to sizetype to avoid some problems
11196 with constant folding. (E.g. suppose the lower bound is 1,
11197 and its mode is QI. Without the conversion,l (ARRAY
11198 +(INDEX-(unsigned char)1)) becomes ((ARRAY+(-(unsigned char)1))
11199 +INDEX), which becomes (ARRAY+255+INDEX). Opps!) */
11200 if (! integer_zerop (low_bound))
11201 index = size_diffop (index, fold_convert (sizetype, low_bound));
11207 && TREE_TYPE (exp) == TREE_TYPE (TREE_TYPE (string))
11208 && TREE_CODE (string) == STRING_CST
11209 && TREE_CODE (index) == INTEGER_CST
11210 && compare_tree_int (index, TREE_STRING_LENGTH (string)) < 0
11211 && (GET_MODE_CLASS (TYPE_MODE (TREE_TYPE (TREE_TYPE (string))))
11213 && (GET_MODE_SIZE (TYPE_MODE (TREE_TYPE (TREE_TYPE (string)))) == 1))
11214 return fold_convert (TREE_TYPE (exp),
11215 build_int_cst (NULL_TREE,
11216 (TREE_STRING_POINTER (string)
11217 [TREE_INT_CST_LOW (index)])));
11222 /* Return the tree for neg (ARG0) when ARG0 is known to be either
11223 an integer constant or real constant.
11225 TYPE is the type of the result. */
11228 fold_negate_const (tree arg0, tree type)
11230 tree t = NULL_TREE;
11232 switch (TREE_CODE (arg0))
11236 unsigned HOST_WIDE_INT low;
11237 HOST_WIDE_INT high;
11238 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
11239 TREE_INT_CST_HIGH (arg0),
11241 t = build_int_cst_wide (type, low, high);
11242 t = force_fit_type (t, 1,
11243 (overflow | TREE_OVERFLOW (arg0))
11244 && !TYPE_UNSIGNED (type),
11245 TREE_CONSTANT_OVERFLOW (arg0));
11250 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
11254 gcc_unreachable ();
11260 /* Return the tree for abs (ARG0) when ARG0 is known to be either
11261 an integer constant or real constant.
11263 TYPE is the type of the result. */
11266 fold_abs_const (tree arg0, tree type)
11268 tree t = NULL_TREE;
11270 switch (TREE_CODE (arg0))
11273 /* If the value is unsigned, then the absolute value is
11274 the same as the ordinary value. */
11275 if (TYPE_UNSIGNED (type))
11277 /* Similarly, if the value is non-negative. */
11278 else if (INT_CST_LT (integer_minus_one_node, arg0))
11280 /* If the value is negative, then the absolute value is
11284 unsigned HOST_WIDE_INT low;
11285 HOST_WIDE_INT high;
11286 int overflow = neg_double (TREE_INT_CST_LOW (arg0),
11287 TREE_INT_CST_HIGH (arg0),
11289 t = build_int_cst_wide (type, low, high);
11290 t = force_fit_type (t, -1, overflow | TREE_OVERFLOW (arg0),
11291 TREE_CONSTANT_OVERFLOW (arg0));
11296 if (REAL_VALUE_NEGATIVE (TREE_REAL_CST (arg0)))
11297 t = build_real (type, REAL_VALUE_NEGATE (TREE_REAL_CST (arg0)));
11303 gcc_unreachable ();
11309 /* Return the tree for not (ARG0) when ARG0 is known to be an integer
11310 constant. TYPE is the type of the result. */
11313 fold_not_const (tree arg0, tree type)
11315 tree t = NULL_TREE;
11317 gcc_assert (TREE_CODE (arg0) == INTEGER_CST);
11319 t = build_int_cst_wide (type,
11320 ~ TREE_INT_CST_LOW (arg0),
11321 ~ TREE_INT_CST_HIGH (arg0));
11322 t = force_fit_type (t, 0, TREE_OVERFLOW (arg0),
11323 TREE_CONSTANT_OVERFLOW (arg0));
11328 /* Given CODE, a relational operator, the target type, TYPE and two
11329 constant operands OP0 and OP1, return the result of the
11330 relational operation. If the result is not a compile time
11331 constant, then return NULL_TREE. */
11334 fold_relational_const (enum tree_code code, tree type, tree op0, tree op1)
11336 int result, invert;
11338 /* From here on, the only cases we handle are when the result is
11339 known to be a constant. */
11341 if (TREE_CODE (op0) == REAL_CST && TREE_CODE (op1) == REAL_CST)
11343 const REAL_VALUE_TYPE *c0 = TREE_REAL_CST_PTR (op0);
11344 const REAL_VALUE_TYPE *c1 = TREE_REAL_CST_PTR (op1);
11346 /* Handle the cases where either operand is a NaN. */
11347 if (real_isnan (c0) || real_isnan (c1))
11357 case UNORDERED_EXPR:
11371 if (flag_trapping_math)
11377 gcc_unreachable ();
11380 return constant_boolean_node (result, type);
11383 return constant_boolean_node (real_compare (code, c0, c1), type);
11386 /* From here on we only handle LT, LE, GT, GE, EQ and NE.
11388 To compute GT, swap the arguments and do LT.
11389 To compute GE, do LT and invert the result.
11390 To compute LE, swap the arguments, do LT and invert the result.
11391 To compute NE, do EQ and invert the result.
11393 Therefore, the code below must handle only EQ and LT. */
11395 if (code == LE_EXPR || code == GT_EXPR)
11400 code = swap_tree_comparison (code);
11403 /* Note that it is safe to invert for real values here because we
11404 have already handled the one case that it matters. */
11407 if (code == NE_EXPR || code == GE_EXPR)
11410 code = invert_tree_comparison (code, false);
11413 /* Compute a result for LT or EQ if args permit;
11414 Otherwise return T. */
11415 if (TREE_CODE (op0) == INTEGER_CST && TREE_CODE (op1) == INTEGER_CST)
11417 if (code == EQ_EXPR)
11418 result = tree_int_cst_equal (op0, op1);
11419 else if (TYPE_UNSIGNED (TREE_TYPE (op0)))
11420 result = INT_CST_LT_UNSIGNED (op0, op1);
11422 result = INT_CST_LT (op0, op1);
11429 return constant_boolean_node (result, type);
11432 /* Build an expression for the a clean point containing EXPR with type TYPE.
11433 Don't build a cleanup point expression for EXPR which don't have side
11437 fold_build_cleanup_point_expr (tree type, tree expr)
11439 /* If the expression does not have side effects then we don't have to wrap
11440 it with a cleanup point expression. */
11441 if (!TREE_SIDE_EFFECTS (expr))
11444 /* If the expression is a return, check to see if the expression inside the
11445 return has no side effects or the right hand side of the modify expression
11446 inside the return. If either don't have side effects set we don't need to
11447 wrap the expression in a cleanup point expression. Note we don't check the
11448 left hand side of the modify because it should always be a return decl. */
11449 if (TREE_CODE (expr) == RETURN_EXPR)
11451 tree op = TREE_OPERAND (expr, 0);
11452 if (!op || !TREE_SIDE_EFFECTS (op))
11454 op = TREE_OPERAND (op, 1);
11455 if (!TREE_SIDE_EFFECTS (op))
11459 return build1 (CLEANUP_POINT_EXPR, type, expr);
11462 /* Build an expression for the address of T. Folds away INDIRECT_REF to
11463 avoid confusing the gimplify process. */
11466 build_fold_addr_expr_with_type (tree t, tree ptrtype)
11468 /* The size of the object is not relevant when talking about its address. */
11469 if (TREE_CODE (t) == WITH_SIZE_EXPR)
11470 t = TREE_OPERAND (t, 0);
11472 /* Note: doesn't apply to ALIGN_INDIRECT_REF */
11473 if (TREE_CODE (t) == INDIRECT_REF
11474 || TREE_CODE (t) == MISALIGNED_INDIRECT_REF)
11476 t = TREE_OPERAND (t, 0);
11477 if (TREE_TYPE (t) != ptrtype)
11478 t = build1 (NOP_EXPR, ptrtype, t);
11484 while (handled_component_p (base))
11485 base = TREE_OPERAND (base, 0);
11487 TREE_ADDRESSABLE (base) = 1;
11489 t = build1 (ADDR_EXPR, ptrtype, t);
11496 build_fold_addr_expr (tree t)
11498 return build_fold_addr_expr_with_type (t, build_pointer_type (TREE_TYPE (t)));
11501 /* Given a pointer value T, return a simplified version of an indirection
11502 through T, or NULL_TREE if no simplification is possible. */
11505 fold_indirect_ref_1 (tree t)
11507 tree type = TREE_TYPE (TREE_TYPE (t));
11512 subtype = TREE_TYPE (sub);
11513 if (!POINTER_TYPE_P (subtype))
11516 if (TREE_CODE (sub) == ADDR_EXPR)
11518 tree op = TREE_OPERAND (sub, 0);
11519 tree optype = TREE_TYPE (op);
11521 if (lang_hooks.types_compatible_p (type, optype))
11523 /* *(foo *)&fooarray => fooarray[0] */
11524 else if (TREE_CODE (optype) == ARRAY_TYPE
11525 && lang_hooks.types_compatible_p (type, TREE_TYPE (optype)))
11527 tree type_domain = TYPE_DOMAIN (optype);
11528 tree min_val = size_zero_node;
11529 if (type_domain && TYPE_MIN_VALUE (type_domain))
11530 min_val = TYPE_MIN_VALUE (type_domain);
11531 return build4 (ARRAY_REF, type, op, min_val, NULL_TREE, NULL_TREE);
11535 /* *(foo *)fooarrptr => (*fooarrptr)[0] */
11536 if (TREE_CODE (TREE_TYPE (subtype)) == ARRAY_TYPE
11537 && lang_hooks.types_compatible_p (type, TREE_TYPE (TREE_TYPE (subtype))))
11540 tree min_val = size_zero_node;
11541 sub = build_fold_indirect_ref (sub);
11542 type_domain = TYPE_DOMAIN (TREE_TYPE (sub));
11543 if (type_domain && TYPE_MIN_VALUE (type_domain))
11544 min_val = TYPE_MIN_VALUE (type_domain);
11545 return build4 (ARRAY_REF, type, sub, min_val, NULL_TREE, NULL_TREE);
11551 /* Builds an expression for an indirection through T, simplifying some
11555 build_fold_indirect_ref (tree t)
11557 tree sub = fold_indirect_ref_1 (t);
11562 return build1 (INDIRECT_REF, TREE_TYPE (TREE_TYPE (t)), t);
11565 /* Given an INDIRECT_REF T, return either T or a simplified version. */
11568 fold_indirect_ref (tree t)
11570 tree sub = fold_indirect_ref_1 (TREE_OPERAND (t, 0));
11578 /* Strip non-trapping, non-side-effecting tree nodes from an expression
11579 whose result is ignored. The type of the returned tree need not be
11580 the same as the original expression. */
11583 fold_ignored_result (tree t)
11585 if (!TREE_SIDE_EFFECTS (t))
11586 return integer_zero_node;
11589 switch (TREE_CODE_CLASS (TREE_CODE (t)))
11592 t = TREE_OPERAND (t, 0);
11596 case tcc_comparison:
11597 if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
11598 t = TREE_OPERAND (t, 0);
11599 else if (!TREE_SIDE_EFFECTS (TREE_OPERAND (t, 0)))
11600 t = TREE_OPERAND (t, 1);
11605 case tcc_expression:
11606 switch (TREE_CODE (t))
11608 case COMPOUND_EXPR:
11609 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1)))
11611 t = TREE_OPERAND (t, 0);
11615 if (TREE_SIDE_EFFECTS (TREE_OPERAND (t, 1))
11616 || TREE_SIDE_EFFECTS (TREE_OPERAND (t, 2)))
11618 t = TREE_OPERAND (t, 0);
11631 /* Return the value of VALUE, rounded up to a multiple of DIVISOR.
11632 This can only be applied to objects of a sizetype. */
11635 round_up (tree value, int divisor)
11637 tree div = NULL_TREE;
11639 gcc_assert (divisor > 0);
11643 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
11644 have to do anything. Only do this when we are not given a const,
11645 because in that case, this check is more expensive than just
11647 if (TREE_CODE (value) != INTEGER_CST)
11649 div = build_int_cst (TREE_TYPE (value), divisor);
11651 if (multiple_of_p (TREE_TYPE (value), value, div))
11655 /* If divisor is a power of two, simplify this to bit manipulation. */
11656 if (divisor == (divisor & -divisor))
11660 t = build_int_cst (TREE_TYPE (value), divisor - 1);
11661 value = size_binop (PLUS_EXPR, value, t);
11662 t = build_int_cst (TREE_TYPE (value), -divisor);
11663 value = size_binop (BIT_AND_EXPR, value, t);
11668 div = build_int_cst (TREE_TYPE (value), divisor);
11669 value = size_binop (CEIL_DIV_EXPR, value, div);
11670 value = size_binop (MULT_EXPR, value, div);
11676 /* Likewise, but round down. */
11679 round_down (tree value, int divisor)
11681 tree div = NULL_TREE;
11683 gcc_assert (divisor > 0);
11687 /* See if VALUE is already a multiple of DIVISOR. If so, we don't
11688 have to do anything. Only do this when we are not given a const,
11689 because in that case, this check is more expensive than just
11691 if (TREE_CODE (value) != INTEGER_CST)
11693 div = build_int_cst (TREE_TYPE (value), divisor);
11695 if (multiple_of_p (TREE_TYPE (value), value, div))
11699 /* If divisor is a power of two, simplify this to bit manipulation. */
11700 if (divisor == (divisor & -divisor))
11704 t = build_int_cst (TREE_TYPE (value), -divisor);
11705 value = size_binop (BIT_AND_EXPR, value, t);
11710 div = build_int_cst (TREE_TYPE (value), divisor);
11711 value = size_binop (FLOOR_DIV_EXPR, value, div);
11712 value = size_binop (MULT_EXPR, value, div);
11718 /* Returns the pointer to the base of the object addressed by EXP and
11719 extracts the information about the offset of the access, storing it
11720 to PBITPOS and POFFSET. */
11723 split_address_to_core_and_offset (tree exp,
11724 HOST_WIDE_INT *pbitpos, tree *poffset)
11727 enum machine_mode mode;
11728 int unsignedp, volatilep;
11729 HOST_WIDE_INT bitsize;
11731 if (TREE_CODE (exp) == ADDR_EXPR)
11733 core = get_inner_reference (TREE_OPERAND (exp, 0), &bitsize, pbitpos,
11734 poffset, &mode, &unsignedp, &volatilep,
11737 if (TREE_CODE (core) == INDIRECT_REF)
11738 core = TREE_OPERAND (core, 0);
11744 *poffset = NULL_TREE;
11750 /* Returns true if addresses of E1 and E2 differ by a constant, false
11751 otherwise. If they do, E1 - E2 is stored in *DIFF. */
11754 ptr_difference_const (tree e1, tree e2, HOST_WIDE_INT *diff)
11757 HOST_WIDE_INT bitpos1, bitpos2;
11758 tree toffset1, toffset2, tdiff, type;
11760 core1 = split_address_to_core_and_offset (e1, &bitpos1, &toffset1);
11761 core2 = split_address_to_core_and_offset (e2, &bitpos2, &toffset2);
11763 if (bitpos1 % BITS_PER_UNIT != 0
11764 || bitpos2 % BITS_PER_UNIT != 0
11765 || !operand_equal_p (core1, core2, 0))
11768 if (toffset1 && toffset2)
11770 type = TREE_TYPE (toffset1);
11771 if (type != TREE_TYPE (toffset2))
11772 toffset2 = fold_convert (type, toffset2);
11774 tdiff = fold (build2 (MINUS_EXPR, type, toffset1, toffset2));
11775 if (!host_integerp (tdiff, 0))
11778 *diff = tree_low_cst (tdiff, 0);
11780 else if (toffset1 || toffset2)
11782 /* If only one of the offsets is non-constant, the difference cannot
11789 *diff += (bitpos1 - bitpos2) / BITS_PER_UNIT;
11793 /* Simplify the floating point expression EXP when the sign of the
11794 result is not significant. Return NULL_TREE if no simplification
11798 fold_strip_sign_ops (tree exp)
11802 switch (TREE_CODE (exp))
11806 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
11807 return arg0 ? arg0 : TREE_OPERAND (exp, 0);
11811 if (HONOR_SIGN_DEPENDENT_ROUNDING (TYPE_MODE (TREE_TYPE (exp))))
11813 arg0 = fold_strip_sign_ops (TREE_OPERAND (exp, 0));
11814 arg1 = fold_strip_sign_ops (TREE_OPERAND (exp, 1));
11815 if (arg0 != NULL_TREE || arg1 != NULL_TREE)
11816 return fold (build2 (TREE_CODE (exp), TREE_TYPE (exp),
11817 arg0 ? arg0 : TREE_OPERAND (exp, 0),
11818 arg1 ? arg1 : TREE_OPERAND (exp, 1)));